Skip to content
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
70 changes: 70 additions & 0 deletions Problem1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Time Complexity :O(1)
// Space Complexity : O(n)
// Did this code successfully run on Leetcode :yes
// Any problem you faced while coding this :no


// Your code here along with comments explaining your approach
/* we are implementing HashSet which is 1D boolean array with primary and secondary hashing to store keys.
we have followed double hashing technique to avoid collisions.
add, remove, and contains operations ad implemented with O(1) time complexity.
time - O(1)
space - O(n)*/

class MyHashSet {

//length of primary and nested arrs
private int buckets;
private int bucketItems;
private boolean [][]storage;

//primary bucket
private int hash1(int key){
return key%buckets;
}

//nested array bucket
private int hash2(int key){
return key/bucketItems;
}

public MyHashSet() {
this.buckets=1000;
this.bucketItems=1000;
this.storage = new boolean[buckets][];
}

public void add(int key) {
int bucket = hash1(key);
//check for nested array
if(storage[bucket]==null){
//only for 0 element edge case to set 1000000 value
if(bucket==0){
storage[bucket]= new boolean[bucketItems +1];
}
else{
storage[bucket]=new boolean[bucketItems];
}
}
int bucketItem = hash2(key);
storage[bucket][bucketItem]=true;
}

public void remove(int key) {
int bucket = hash1(key);
int bucketItem = hash2(key);
//check for nested array
if(storage[bucket]==null) return;
storage[bucket][bucketItem]=false;
}

public boolean contains(int key) {
int bucket =hash1(key);
int bucketItem = hash2(key);
//check for nested array
if(storage[bucket] == null) {
return false;
}
return storage[bucket][bucketItem];
}
}
58 changes: 58 additions & 0 deletions Problem2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Time Complexity :O(1)
// Space Complexity : O(n)
// Did this code successfully run on Leetcode :yes
// Any problem you faced while coding this :no

// Your code here along with comments explaining your approach
/*
In this two arrays are used one to store values and one to track the minimum at each position.
When I push a value, I also store the smallest value seen so far
This will let me get the minimum element in constant time.
time - O(1)
space - O(n)
*/

class MinStack {
int[] stack;
int[] minStack;
int top;

public MinStack() {
stack = new int[10000];
minStack = new int[10000];
top = -1;
}

public void push(int val) {
top++;
stack[top] = val;
// if first element, it is the minimum by default
if (top == 0) {
minStack[top] = val;
} //else, store the smaller value between the current element and the previous minimum so far
else {
minStack[top] = Math.min(val, minStack[top - 1]);
}
}

public void pop() {
top--;
}

public int top() {
return stack[top];
}

public int getMin() {
return minStack[top];
}
}

/**
* 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();
*/
7 changes: 0 additions & 7 deletions Sample.java

This file was deleted.