From 3e7fe6b624742d228fdd649d85ae708e3b2fb6c9 Mon Sep 17 00:00:00 2001 From: sabyasachi bisoyi Date: Wed, 24 Dec 2025 16:09:45 -0800 Subject: [PATCH] day 2 sample problems --- HashMap.java | 65 ++++++++++++++++++++++++++ Sample.java | 126 +++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 187 insertions(+), 4 deletions(-) create mode 100644 HashMap.java diff --git a/HashMap.java b/HashMap.java new file mode 100644 index 00000000..24a0b58c --- /dev/null +++ b/HashMap.java @@ -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); + */ \ No newline at end of file diff --git a/Sample.java b/Sample.java index 1739a9cb..33faa93c 100644 --- a/Sample.java +++ b/Sample.java @@ -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 stack; + public MyQueue() { + stack = new Stack(); + } + //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 temp = new Stack(); + //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 stackIn; + private Stack stackOut; + public MyQueue() { + stackIn = new Stack(); + stackOut = new Stack(); + } + + //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(); + */