diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 00000000..13566b81 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/Design-1.iml b/.idea/Design-1.iml new file mode 100644 index 00000000..d6ebd480 --- /dev/null +++ b/.idea/Design-1.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 00000000..639900d1 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 00000000..cc065d00 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 00000000..35eb1ddf --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Problem3.java b/Problem3.java new file mode 100644 index 00000000..84e90062 --- /dev/null +++ b/Problem3.java @@ -0,0 +1,40 @@ +// Time Complexity: O(1) +// Space Complexity: O(N) + +class MinStack { + private ArrayList st; + + public MinStack() { + st = new ArrayList<>(); + } + + public void push(int val) { + int[] top = st.isEmpty() ? new int[]{val, val} : st.get(st.size() - 1); + int min_value = top[1]; + if (min_value > val) { + min_value = val; + } + st.add(new int[]{val, min_value}); + } + + public void pop() { + st.remove(st.size()-1); + } + + public int top() { + return st.isEmpty() ? -1 : st.get(st.size() - 1)[0]; + } + + public int getMin() { + return st.isEmpty() ? -1 : st.get(st.size() - 1)[1]; + } +} + +/** + * 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(); + */ \ No newline at end of file diff --git a/Sample.java b/Sample.java index 1739a9cb..ee038383 100644 --- a/Sample.java +++ b/Sample.java @@ -1,7 +1,34 @@ -// Time Complexity : -// Space Complexity : -// Did this code successfully run on Leetcode : -// Any problem you faced while coding this : +// Time Complexity : O(1) +// Space Complexity : O(1) -> O(1000001) +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : Initialized the boolean set with only 100 as the limit // Your code here along with comments explaining your approach +class MyHashSet { + boolean[] set; + + public MyHashSet() { + set = new boolean[1000001]; + } + + public void add(int key) { + set[key] = true; + } + + public void remove(int key) { + set[key] = false; + } + + public boolean contains(int key) { + return set[key]; + } +} + +/** + * 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