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
65 changes: 65 additions & 0 deletions HashMap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Time Complexity : O(1)
// Space Complexity : O(1)->no extra space
// Did this code successfully run on Leetcode : Yes
class MyHashMap {

//Use yesterday's concept double hashing for HAshSet
private int[][] array;
public MyHashMap() {
array = new int[1000][];//nearest sqaure root
}

private int _hash1(int x)
{
return x%1000;
}

private int _hash2(int x)
{
return x/1000;
}
//O(1)
public void put(int key, int value) {
int primary = _hash1(key);
if(array[primary]==null)
{
if (primary==0)
{
array[primary] = new int[1001];
}
else{
array[primary] = new int[1000];
}
Arrays.fill(array[primary],-1);
}
int sec = _hash2(key);
array[primary][sec] = value;
}

//O(1)
public int get(int key) {
int primary = _hash1(key);
int sec = _hash2(key);
if (array[primary] == null) {
return -1;
}
return array[primary][sec];
}

//O(1)
public void remove(int key) {
int primary = _hash1(key);
int sec = _hash2(key);
if (array[primary] != null) {
array[primary][sec] = -1;
}
}
}

/**
* Your MyHashMap object will be instantiated and called as such:
* MyHashMap obj = new MyHashMap();
* obj.put(key,value);
* int param_2 = obj.get(key);
* obj.remove(key);
*/
126 changes: 122 additions & 4 deletions Sample.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,125 @@
// Time Complexity :
// Space Complexity :
// Did this code successfully run on Leetcode :
// Any problem you faced while coding this :
// Time Complexity : O(n2)
// Space Complexity : O(n2)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : TLE


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

private Stack<Integer> stack;
public MyQueue() {
stack = new Stack<Integer>();
}
//Tried to maintain FIFO order in Stack
//For each operation need O(n) space, O(n) time
public void push(int x) {
if(stack.isEmpty())
{
stack.push(x);
return;
}
//Temp stack
Stack<Integer> temp = new Stack<Integer>();
//Empty original Stack and push to temp
while(!stack.isEmpty())
{
int val = stack.pop();
temp.push(val);
}
//Once emptied push to original
//so Last values goes to last
stack.push(x);
//Empty temp stack and push to original
while(!temp.isEmpty())
{
int val = stack.pop();
stack.push(val);
}
}

public int pop() {
return stack.pop();
}

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

public boolean empty() {
return stack.isEmpty();
}
}

/**
* Your MyQueue object will be instantiated and called as such:
* MyQueue obj = new MyQueue();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.peek();
* boolean param_4 = obj.empty();
*/


//Optimized
// Time Complexity : amoratized O(n)
// Space Complexity : amoratized O(n)
// Did this code successfully run on Leetcode : Yes
class MyQueue {

private Stack<Integer> stackIn;
private Stack<Integer> stackOut;
public MyQueue() {
stackIn = new Stack<Integer>();
stackOut = new Stack<Integer>();
}

//O(1)
public void push(int x) {
stackIn.push(x);
}

//Only transfer to stackOut if empty
//StackOut maintians pop order for Queue
//Same for peek
//O(1)
public int pop() {
if(stackOut.isEmpty())
{
while(!stackIn.isEmpty())
{
int val = stackIn.pop();
stackOut.push(val);
}
return stackOut.pop();
}
return stackOut.pop();
}

//O(1)
public int peek() {
if(stackOut.isEmpty())
{
while(!stackIn.isEmpty())
{
int val = stackIn.pop();
stackOut.push(val);
}
return stackOut.peek();
}
return stackOut.peek();
}

public boolean empty() {
return stackIn.isEmpty() && stackOut.isEmpty();
}
}

/**
* Your MyQueue object will be instantiated and called as such:
* MyQueue obj = new MyQueue();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.peek();
* boolean param_4 = obj.empty();
*/