Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Completed Design-1 both the questions #2281

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions Design-1.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
62 changes: 62 additions & 0 deletions MinStack.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Time Complexity : O(N)
// Space Complexity : O(n)
// Did this code successfully run on Leetcode : yes
// Any problem you faced while coding this : no

import java.util.*;
class MinStack {

// List<Integer> ls;
// int size = 0;
Stack<Integer> st; // Stack to store the elements
Stack<Integer> minSt; // Stack to store the corresponding minimum elements of the stack elements
int min; // minimum

public MinStack() {
// ls = new ArrayList<>();
this.min = Integer.MAX_VALUE;
this.st = new Stack<>();
this.minSt = new Stack<>();
this.minSt.push(this.min); // push to the minimum stack, the maxium integer value to compare with other integer values
}

public void push(int val) {
// ls.add(size++, val);
st.push(val);
min = Math.min(min, val); //Compare the current element with the previous minimum value
minSt.push(min);
}

public void pop() {
// ls.remove(--size);
st.pop();
minSt.pop(); // pop the minimum value
min = minSt.peek();
}

public int top() {
// return ls.get(size - 1);
return st.peek(); // returns value at the top of the stack
}

public int getMin() {
// int min = Integer.MAX_VALUE;
// for(int item: ls){
// if(item < min){
// min = item;
// }
// }
// return min;

return min; // returns minimum value
}
}

/**
* Your MinStack object will be instantiated and called as such:
* MinStack obj = new MinStack();
* obj.push(val);
* obj.pop();
* int param_3 = obj.top();
* int param_4 = obj.getMin();
*/
68 changes: 68 additions & 0 deletions MyHashSet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Time Complexity : O(1)
// Space Complexity : O(N)
// Did this code successfully run on Leetcode : yes
// Any problem you faced while coding this : no

class MyHashSet {

boolean arr[][]; // pointers
int buckets;
int bucketItems;

public MyHashSet() {
// arr = new boolean[1000001];
this.buckets = 1000; // square root of 1000000
this.bucketItems = 1000;
this.arr = new boolean[buckets][]; // initialize the pointer with the bucket size
}

public int bucket(int key){
return key%buckets; // hashing % to get the bucket
}

public int bucketItem(int key){
return key/bucketItems; // hashing / to get the space in the bucket
}

public void add(int key) {
// arr[key] = true;
int bucket = bucket(key);
if(arr[bucket] == null){
if(bucket == 0){
arr[bucket] = new boolean[bucketItems+1];
}else{
arr[bucket] = new boolean[bucketItems];
}
}
int bucketItem = bucketItem(key);
arr[bucket][bucketItem] = true;
}

public void remove(int key) {
// arr[key] = false;
int bucket = bucket(key);
if(arr[bucket] == null){
return;
}
int bucketItem = bucketItem(key);
arr[bucket][bucketItem] = false;
}

public boolean contains(int key) {
// return arr[key];
int bucket = bucket(key);
if(arr[bucket] == null){
return false;
}
int bucketItem = bucketItem(key);
return arr[bucket][bucketItem];
}
}

/**
* Your MyHashSet object will be instantiated and called as such:
* MyHashSet obj = new MyHashSet();
* obj.add(key);
* obj.remove(key);
* boolean param_3 = obj.contains(key);
*/