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

Done Design 1 #2296

Open
wants to merge 2 commits 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
47 changes: 47 additions & 0 deletions MinStack.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@

// Time Complexity : O(1)
// Space Complexity :0(n)
// Did this code successfully run on Leetcode :Yes
// Any problem you faced while coding this :No

//Implementing Min Stack using two Stack objects.

import java.util.Stack;

class MinStack {

Stack<Integer> stack;
Stack<Integer> minStack;
int min;

public MinStack() {
this.min = Integer.MAX_VALUE;
this.stack = new Stack<>();
this.minStack = new Stack<>();
}

public void push(int val) {
stack.push(val);
// check if the current push is less than min and then push the min to
// min stack. Min stack now contains previous min.
if (min >= val) {
minStack.push(min);
min = val;
}
}

public void pop() {
int poppedValue = stack.pop();
if (poppedValue == min) {
min = minStack.pop();
}
}

public int top() {
return stack.peek();
}

public int getMin() {
return min;
}
}
57 changes: 54 additions & 3 deletions Sample.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,58 @@
// Time Complexity :
// Time Complexity : O(1)
// Space Complexity :
// Did this code successfully run on Leetcode :
// Did this code successfully run on Leetcode :Yes
// Any problem you faced while coding this :

// Implementing Hashset using double hashing strategy. Using %length and /length for hashing. Nesting the boolean array to handle
// the collisions.

// Your code here along with comments explaining your approach
class MyHashSet {

private boolean[][] storage;
private int bucketSize;
private int bucketItemsSize;

private int bucketIndex(int key) {
return key % bucketSize;
}

private int bucketItemIndex(int key) {
return key / bucketItemsSize;
}

public MyHashSet() {
this.bucketSize = 1000; // square root of possible inputs.
this.bucketItemsSize = 1000;
this.storage = new boolean[bucketSize][]; // the nested array is added only if required.
}

public void add(int key) {
int bucket = bucketIndex(key);
if (storage[bucket] == null) {
if (bucket == 0) {
storage[bucket] = new boolean[bucketItemsSize + 1];// handling the the collisions at 0th index.
} else {
storage[bucket] = new boolean[bucketItemsSize];
}
}
int bucketItem = bucketItemIndex(key);
storage[bucket][bucketItem] = true;
}

public void remove(int key) {
int bucket = bucketIndex(key);
if (storage[bucket] == null)
return;
int bucketItem = bucketItemIndex(key);
storage[bucket][bucketItem] = false;
}

public boolean contains(int key) {
int bucket = bucketIndex(key);
if (storage[bucket] == null)
return false;
int bucketItem = bucketItemIndex(key);
return storage[bucket][bucketItem];

}
}