From ebc1d7746041882c3ef8669adcf213d74321b62f Mon Sep 17 00:00:00 2001 From: tanvipandhre <37935899+tanvipandhre@users.noreply.github.com> Date: Thu, 13 Nov 2025 22:39:50 -0500 Subject: [PATCH 1/4] Enhance README with Hashmap design details Added explanation of storage array and linked list implementation details for the Hashmap design. --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index ff1cd0e0..ac3deb5e 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,9 @@ Explain your approach in **three sentences only** at top of your code ## Problem 2: Design Hashmap (https://leetcode.com/problems/design-hashmap/) - +we are using an storage array of Node w 10,000 and Linkedlist of 100 so traveling linkedlist gets faster. +create a find function with 2 pointers prev,curr that iterates over the linkedlist to search for the key and return the prev Node. +put() has 2 scenarios - if key already exists or create fresh Node, delete() - if key does not exist or if it is in middle. From cd48bcf38e49b1264394d080955393e8a959cc81 Mon Sep 17 00:00:00 2001 From: tanvipandhre <37935899+tanvipandhre@users.noreply.github.com> Date: Fri, 14 Nov 2025 15:04:41 -0500 Subject: [PATCH 2/4] Day3- implement-queue-using-stacks, Design-hashmap --- Design-hashmap.java | 79 +++++++++++++++++++++++++++++++ Implement-queue-using-stacks.java | 44 +++++++++++++++++ 2 files changed, 123 insertions(+) create mode 100644 Design-hashmap.java create mode 100644 Implement-queue-using-stacks.java diff --git a/Design-hashmap.java b/Design-hashmap.java new file mode 100644 index 00000000..8d25e499 --- /dev/null +++ b/Design-hashmap.java @@ -0,0 +1,79 @@ +class MyHashMap { + class Node{ + int key; + int val; + Node next; + Node(int key, int val){ + this.key = key; + this.val = val; + } + } + public Node[] storage; // we are keeping array of nodes + + private int hash(int key){ + return key% 10000; + } + + public MyHashMap() { + this.storage = new Node[10000]; //we do it 10,000 instead of 10,000 + } + + private Node find(Node head, int key){ + Node prev = null; + Node curr = head; + while(curr != null && curr.key != key){ //for iteration of linkedlist + prev = curr; + curr = curr.next; + } + return prev; + } + + public void put(int key, int value) { + int idx = hash(key); + if(storage[idx] == null){ // there is no element, initialize a dummy node + storage[idx] = new Node(-1,-1); + } + Node prev = find(storage[idx], key); + //if curr key already exists in list + if(prev.next != null) + prev.next.val = value; + //else create a fresh node + else + prev.next = new Node(key, value); + + } + + public int get(int key) { + int idx = hash(key); + if(storage[idx] == null){ //if it is not initialized only then nothing is there + return -1; + } + Node prev = find(storage[idx], key); + if(prev.next!=null){ //curr key exists + return prev.next.val; + } + return -1; + } + + public void remove(int key) { + int idx = hash(key); + if(storage[idx] == null){ // there is no element simply return + return; + } + Node prev = find(storage[idx], key); + if(prev.next==null) return; //last node to be deleted + //to delete in middle + Node curr = prev.next; + prev.next = prev.next.next; + curr.next = null; + + } +} + +/** + * 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/Implement-queue-using-stacks.java b/Implement-queue-using-stacks.java new file mode 100644 index 00000000..73f9095a --- /dev/null +++ b/Implement-queue-using-stacks.java @@ -0,0 +1,44 @@ +class MyQueue { + private Stack in; + private Stack out; + public MyQueue() { + this.in = new Stack<>(); + this.out = new Stack<>(); + } + + public void push(int x) { + in.push(x); //simple + } + //if out stack is empty pop element from instack and push is in outstack + public int pop() { + + if(out.isEmpty()){ + while(!in.isEmpty()){ + out.push(in.pop()); + } + } + return out.pop(); + } + + public int peek() { + if(out.isEmpty()){ + while(!in.isEmpty()){ + out.push(in.pop()); + } + } + return out.peek(); + } + + public boolean empty() { + return in.isEmpty() && out.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(); + */ \ No newline at end of file From 0cd6ff9c51d08086cd634598dede2b3c5134ff64 Mon Sep 17 00:00:00 2001 From: tanvipandhre <37935899+tanvipandhre@users.noreply.github.com> Date: Fri, 14 Nov 2025 15:09:39 -0500 Subject: [PATCH 3/4] Update README with problem explanations --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index ac3deb5e..c1432747 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,8 @@ Explain your approach in **three sentences only** at top of your code ## Problem 1: (https://leetcode.com/problems/implement-queue-using-stacks/) - +Use 2 stacks instack & outstack +For peep() and pop() - if outstack is empty, then util the instack is empty try pushing values from instack to outstack then pop element from outstack. ## Problem 2: Design Hashmap (https://leetcode.com/problems/design-hashmap/) From 1bb3dddcd88694153c0010d359f3f3f5073bb7e5 Mon Sep 17 00:00:00 2001 From: tanvipandhre <37935899+tanvipandhre@users.noreply.github.com> Date: Tue, 18 Nov 2025 23:09:29 -0500 Subject: [PATCH 4/4] Day6 - groupAnagrams, Isomorphic, Design Hashmap --- Design-hashmap.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Design-hashmap.java b/Design-hashmap.java index 8d25e499..68cbdeaf 100644 --- a/Design-hashmap.java +++ b/Design-hashmap.java @@ -61,7 +61,7 @@ public void remove(int key) { return; } Node prev = find(storage[idx], key); - if(prev.next==null) return; //last node to be deleted + if(prev.next==null) return; //curr doest not exist then simple return //to delete in middle Node curr = prev.next; prev.next = prev.next.next;