From bee8786be21920a11b8f6114a2755f128495ebb7 Mon Sep 17 00:00:00 2001 From: Neha Mishra Date: Wed, 6 Nov 2019 20:41:18 +0530 Subject: [PATCH 1/6] ImplementQueueUsingStackCompleted_HashSetPending --- QueueUsingStack.java | 62 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 QueueUsingStack.java diff --git a/QueueUsingStack.java b/QueueUsingStack.java new file mode 100644 index 00000000..9ed248cf --- /dev/null +++ b/QueueUsingStack.java @@ -0,0 +1,62 @@ +// Time Complexity :1 +// Space Complexity :O(n) +// Did this code successfully run on Leetcode :Yes +// Any problem you faced while coding this :No +//this is new commeng------------------- + +// Your code here along with comments explaining your approach + +class MyQueue { + //creating two sttacks to keep track of minimum w.r.t the respective element + Stack s1=new Stack<>(); + Stack s2=new Stack<>(); + + /** Initialize your data structure here. */ + public MyQueue() { + + } + + /** Push element x to the back of queue. */ + public void push(int x) { + //simply push the element in s1 + s1.push(x); + } + + /** Removes the element from in front of queue and returns that element. */ + public int pop() { + if(s2.isEmpty()) + { //if s1 is not empty,push all the elements of s1 in s2 + while(!s1.isEmpty()) + {s2.push(s1.pop());} + } + //return and pop the top of the s2 stack + return s2.pop(); + } + + /** Get the front element. */ + public int peek() { + if(s2.isEmpty()) + { //if s1 is not empty,push all the elements of s1 in s2 + while(!s1.isEmpty()) + {s2.push(s1.pop());} + } + //return top of the s2 stack + return s2.peek(); + } + + /** Returns whether the queue is empty. */ + public boolean empty() { + //if both the stacks are empty return true + return (s1.isEmpty() && s2.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 da17dbe8a48f872c39f3c57dec6070c1128dc521 Mon Sep 17 00:00:00 2001 From: Neha Mishra Date: Thu, 7 Nov 2019 12:12:36 +0530 Subject: [PATCH 2/6] Tried_QueUsingStack_And_DesignHashSet --- HashSet.java | 114 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 HashSet.java diff --git a/HashSet.java b/HashSet.java new file mode 100644 index 00000000..acb49926 --- /dev/null +++ b/HashSet.java @@ -0,0 +1,114 @@ +// Time Complexity : +//mthods::add:O(n) +//remove:O(n) +//contains:O(n) +// Space Complexity :O(n) +// Did this code successfully run on Leetcode :Yes +// Any problem you faced while coding this :I was getting null pointer exception in contains method and type incompatible exception while assigning values in wrong manner +//this is new commeng------------------- + +// Your code here along with comments explaining your approach + +class MyHashSet { + //an array node of the objexts of class ListNode was created + ListNode[] node=new ListNode[1000000]; + + /** Initialize your data structure here. */ + private class ListNode + { //ListNode class contains attributes key and next + int key; + ListNode next; + //key was initialised in the constructor + ListNode(int key) + { + this.key=key; + } + + } + public MyHashSet() + { + + } + //to find index of the given key we are using hashCode as follows + int index(int key) + { + return Integer.hashCode(key)%node.length; + } + //a method is made to find the location previous than the node to be found in parameters + ListNode find(ListNode head,int key) + { //curr pointer is assigned on the starting of the array and prev is null + ListNode curr=head; + ListNode prev=null; + //while curr is not null and curr key is equal to the required value,loop is continued + while(curr!=null && curr.key!=key) + {//curr is assigned to prev + prev=curr; + //curr is incremented + curr=curr.next; + } + //at end of the loop,it gives node previous to the key to be found + return prev; + } + + public void add(int key) + { //index of the key is stored in integer i + int i=index(key); + //if the node is null,we'll create a new node having key=-1 + if(node[i]==null) + { + node[i]=new ListNode(-1); + } + //pointer previous to the node is found + ListNode prev=find(node[i],key); + //if next to the prev is null,A new listnode is inserted at the end + if(prev.next==null) + { + prev.next=new ListNode(key); + } + else + { //key is updated to its value + prev.next.key=key; + } + } + + + public void remove(int key) + { //index is found + int i=index(key); + //if node is not there, do nothing + if(node[i]==null) + {return;} + //prev to the node is found + ListNode prev=find(node[i],key); + + //if the node is null, do nothing + if(prev.next==null){return;} + //we point the pointer to next to the node to be removed + prev.next=prev.next.next; + } + + /** Returns true if this set contains the specified element */ + public boolean contains(int key) + { + //index is found + int i=index(key); + //if it is not there return false + if(node[i]==null) + {return false;} + //previous to the node is found + ListNode prev=find(node[i],key); + //if next to the node is not null,check for the value and if it is there,return true else false + if( prev.next!=null) + { if(prev.next.key==key ) + {return true;}} + return false; + } +} + +/** + * Your MyHashSet object will be instantiated and called as such: + * MyHashSet obj = new MyHashSet(); + * obj.add(key); + * obj.remove(key); + * boolean param_3 = obj.contains(key); + */ \ No newline at end of file From 5be112b6a627557de0d0080b19487cae5cbcced5 Mon Sep 17 00:00:00 2001 From: Neha Mishra Date: Fri, 8 Nov 2019 14:40:36 +0530 Subject: [PATCH 3/6] Completed_Design2 --- HashSetUsingDoubleHashing.java | 73 ++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 HashSetUsingDoubleHashing.java diff --git a/HashSetUsingDoubleHashing.java b/HashSetUsingDoubleHashing.java new file mode 100644 index 00000000..3e2b2230 --- /dev/null +++ b/HashSetUsingDoubleHashing.java @@ -0,0 +1,73 @@ +// Time Complexity :1 +// Space Complexity :O(n) +// Did this code successfully run on Leetcode :Yes +// Any problem you faced while coding this :I was getting index out of bound exception and null pointer exception also +//this is new commeng------------------- + +// Your code here along with comments explaining your approach +// +class MyHashSet { + + //initialising an integer variable buckets to 1000 + //initialising an integer variable bucketItems to 1000 + int buckets=1000; + int bucketItems=1000; + //making a two dimensional array if buckets and bucketItems + boolean[][] storage=new boolean[buckets][]; + //method to return the value of bucket + private int bucket(int value) + {//return position of the value in main array + return value%buckets; + } + //method to return the value of bucketItem + private int bucketItem(int value) + { //return position of the value in nested array + return value/buckets; + } + /** Initialize your data structure here. */ + public MyHashSet() { + + } + + public void add(int value) + { //get the positions of the value element + int bucket=bucket(value); + int bucketItem=bucketItem(value); + //if yhe main list is null, make a new element in the array + if(storage[bucket]==null) + {storage[bucket]=new boolean[bucketItems]; + } + //give the value in nested array as true + storage[bucket][bucketItem]=true; + } + + public void remove(int value) + { //get the positions of the value element + int bucket=bucket(value); + int bucketItem=bucketItem(value); + //if the position is not empty + if(storage[bucket]!=null) + //give the complete position(nested array) as false + {storage[bucket][bucketItem]=false; + } + + } + + /** Returns true if this set contains the specified element */ + public boolean contains(int value) + { //get the positions of the value element + int bucket=bucket(value); + int bucketItem=bucketItem(value); + //if the position is not empty return true either false + return storage[bucket]!=null && storage[bucket][bucketItem]; + + } +} + +/** + * Your MyHashSet object will be instantiated and called as such: + * MyHashSet obj = new MyHashSet(); + * obj.add(key); + * obj.remove(key); + * boolean param_3 = obj.contains(key); + */ \ No newline at end of file From 912084f51c76b0211280982a308ffd6aa6f329db Mon Sep 17 00:00:00 2001 From: Neha Mishra Date: Tue, 26 Apr 2022 20:55:59 +0530 Subject: [PATCH 4/6] completed hashset problem --- HashSet.java | 134 ++++++++++++++------------------------------------- 1 file changed, 36 insertions(+), 98 deletions(-) diff --git a/HashSet.java b/HashSet.java index acb49926..1ef9cb8f 100644 --- a/HashSet.java +++ b/HashSet.java @@ -1,107 +1,45 @@ -// Time Complexity : -//mthods::add:O(n) -//remove:O(n) -//contains:O(n) -// Space Complexity :O(n) -// Did this code successfully run on Leetcode :Yes -// Any problem you faced while coding this :I was getting null pointer exception in contains method and type incompatible exception while assigning values in wrong manner -//this is new commeng------------------- - -// Your code here along with comments explaining your approach +// Time Complexity :add,remove,contains->O(1); +// Space Complexity :O(n); where n is no of elements +// Did this code successfully run on Leetcode :yes +// Any problem you faced while coding this :tried to make array as int array then realised we can use boolean class MyHashSet { - //an array node of the objexts of class ListNode was created - ListNode[] node=new ListNode[1000000]; - - /** Initialize your data structure here. */ - private class ListNode - { //ListNode class contains attributes key and next - int key; - ListNode next; - //key was initialised in the constructor - ListNode(int key) - { - this.key=key; - } - - } - public MyHashSet() - { - - } - //to find index of the given key we are using hashCode as follows - int index(int key) - { - return Integer.hashCode(key)%node.length; + public int keyList = 1000;// taking keyList Size + public int keyItem = 1001;// taking nested array size as 1001 as item 1000000 will be on //index + // arr[0][1000] + boolean[][] hashSet = new boolean[keyList][];// initializing list + + public MyHashSet() { + } - //a method is made to find the location previous than the node to be found in parameters - ListNode find(ListNode head,int key) - { //curr pointer is assigned on the starting of the array and prev is null - ListNode curr=head; - ListNode prev=null; - //while curr is not null and curr key is equal to the required value,loop is continued - while(curr!=null && curr.key!=key) - {//curr is assigned to prev - prev=curr; - //curr is incremented - curr=curr.next; + + public void add(int key) { + int x = key % keyList;// calculating index using hash function + int y = key / keyList; + if (hashSet[x] == null) { + hashSet[x] = new boolean[keyItem];// initializing nested array } - //at end of the loop,it gives node previous to the key to be found - return prev; + hashSet[x][y] = true;// setting value to true + } - - public void add(int key) - { //index of the key is stored in integer i - int i=index(key); - //if the node is null,we'll create a new node having key=-1 - if(node[i]==null) - { - node[i]=new ListNode(-1); - } - //pointer previous to the node is found - ListNode prev=find(node[i],key); - //if next to the prev is null,A new listnode is inserted at the end - if(prev.next==null) - { - prev.next=new ListNode(key); - } - else - { //key is updated to its value - prev.next.key=key; + + public void remove(int key) { + int x = key % keyList; + int y = key / keyList; + if (hashSet[x] != null) { + hashSet[x][y] = false;// removing element by setting it to false at calculated //index } + } - - - public void remove(int key) - { //index is found - int i=index(key); - //if node is not there, do nothing - if(node[i]==null) - {return;} - //prev to the node is found - ListNode prev=find(node[i],key); - - //if the node is null, do nothing - if(prev.next==null){return;} - //we point the pointer to next to the node to be removed - prev.next=prev.next.next; - } - - /** Returns true if this set contains the specified element */ - public boolean contains(int key) - { - //index is found - int i=index(key); - //if it is not there return false - if(node[i]==null) - {return false;} - //previous to the node is found - ListNode prev=find(node[i],key); - //if next to the node is not null,check for the value and if it is there,return true else false - if( prev.next!=null) - { if(prev.next.key==key ) - {return true;}} - return false; + + public boolean contains(int key) { + int x = key % keyList; + int y = key / keyList; + if (hashSet[x] != null) { + return hashSet[x][y];// if nested array is not null return its status + } + return false;// else it will automatically be false + } } @@ -111,4 +49,4 @@ public boolean contains(int key) * obj.add(key); * obj.remove(key); * boolean param_3 = obj.contains(key); - */ \ No newline at end of file + */ From 45a81351a1e077c230aa03987663e1e8d21fbf5a Mon Sep 17 00:00:00 2001 From: Neha Mishra Date: Tue, 26 Apr 2022 22:30:18 +0530 Subject: [PATCH 5/6] Delete HashSetUsingDoubleHashing.java --- HashSetUsingDoubleHashing.java | 73 ---------------------------------- 1 file changed, 73 deletions(-) delete mode 100644 HashSetUsingDoubleHashing.java diff --git a/HashSetUsingDoubleHashing.java b/HashSetUsingDoubleHashing.java deleted file mode 100644 index 3e2b2230..00000000 --- a/HashSetUsingDoubleHashing.java +++ /dev/null @@ -1,73 +0,0 @@ -// Time Complexity :1 -// Space Complexity :O(n) -// Did this code successfully run on Leetcode :Yes -// Any problem you faced while coding this :I was getting index out of bound exception and null pointer exception also -//this is new commeng------------------- - -// Your code here along with comments explaining your approach -// -class MyHashSet { - - //initialising an integer variable buckets to 1000 - //initialising an integer variable bucketItems to 1000 - int buckets=1000; - int bucketItems=1000; - //making a two dimensional array if buckets and bucketItems - boolean[][] storage=new boolean[buckets][]; - //method to return the value of bucket - private int bucket(int value) - {//return position of the value in main array - return value%buckets; - } - //method to return the value of bucketItem - private int bucketItem(int value) - { //return position of the value in nested array - return value/buckets; - } - /** Initialize your data structure here. */ - public MyHashSet() { - - } - - public void add(int value) - { //get the positions of the value element - int bucket=bucket(value); - int bucketItem=bucketItem(value); - //if yhe main list is null, make a new element in the array - if(storage[bucket]==null) - {storage[bucket]=new boolean[bucketItems]; - } - //give the value in nested array as true - storage[bucket][bucketItem]=true; - } - - public void remove(int value) - { //get the positions of the value element - int bucket=bucket(value); - int bucketItem=bucketItem(value); - //if the position is not empty - if(storage[bucket]!=null) - //give the complete position(nested array) as false - {storage[bucket][bucketItem]=false; - } - - } - - /** Returns true if this set contains the specified element */ - public boolean contains(int value) - { //get the positions of the value element - int bucket=bucket(value); - int bucketItem=bucketItem(value); - //if the position is not empty return true either false - return storage[bucket]!=null && storage[bucket][bucketItem]; - - } -} - -/** - * Your MyHashSet object will be instantiated and called as such: - * MyHashSet obj = new MyHashSet(); - * obj.add(key); - * obj.remove(key); - * boolean param_3 = obj.contains(key); - */ \ No newline at end of file From a9c3a7b2b30412dcf45e01202f155fdd78497613 Mon Sep 17 00:00:00 2001 From: Neha Mishra Date: Tue, 26 Apr 2022 22:30:58 +0530 Subject: [PATCH 6/6] Delete QueueUsingStack.java --- QueueUsingStack.java | 62 -------------------------------------------- 1 file changed, 62 deletions(-) delete mode 100644 QueueUsingStack.java diff --git a/QueueUsingStack.java b/QueueUsingStack.java deleted file mode 100644 index 9ed248cf..00000000 --- a/QueueUsingStack.java +++ /dev/null @@ -1,62 +0,0 @@ -// Time Complexity :1 -// Space Complexity :O(n) -// Did this code successfully run on Leetcode :Yes -// Any problem you faced while coding this :No -//this is new commeng------------------- - -// Your code here along with comments explaining your approach - -class MyQueue { - //creating two sttacks to keep track of minimum w.r.t the respective element - Stack s1=new Stack<>(); - Stack s2=new Stack<>(); - - /** Initialize your data structure here. */ - public MyQueue() { - - } - - /** Push element x to the back of queue. */ - public void push(int x) { - //simply push the element in s1 - s1.push(x); - } - - /** Removes the element from in front of queue and returns that element. */ - public int pop() { - if(s2.isEmpty()) - { //if s1 is not empty,push all the elements of s1 in s2 - while(!s1.isEmpty()) - {s2.push(s1.pop());} - } - //return and pop the top of the s2 stack - return s2.pop(); - } - - /** Get the front element. */ - public int peek() { - if(s2.isEmpty()) - { //if s1 is not empty,push all the elements of s1 in s2 - while(!s1.isEmpty()) - {s2.push(s1.pop());} - } - //return top of the s2 stack - return s2.peek(); - } - - /** Returns whether the queue is empty. */ - public boolean empty() { - //if both the stacks are empty return true - return (s1.isEmpty() && s2.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