From 80e97d9dc0d22ffc999f08de96d0f5d3d497c053 Mon Sep 17 00:00:00 2001 From: sureshb18 Date: Sat, 7 Feb 2026 18:56:35 -0600 Subject: [PATCH 1/3] Update Sample.java --- Sample.java | 45 +++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 41 insertions(+), 4 deletions(-) diff --git a/Sample.java b/Sample.java index 1739a9cb..62707481 100644 --- a/Sample.java +++ b/Sample.java @@ -1,7 +1,44 @@ -// 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) +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : // Your code here along with comments explaining your approach + +// Approach + +// 1) create boolean array of given size +// 2) add true to boolean array if adding an element +// 3) add false to boolean array if deleting +// 4) return value of the key + +class MyHashSet { + + private boolean[] data; + + public MyHashSet() { + data = new boolean[1000001]; + + } + + public void add(int key) { + data[key] = true; + } + + public void remove(int key) { + data[key] = false; + } + + public boolean contains(int key) { + return data[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 From e39aa00afe0c6ce90d04e42f4471a2c85f4af20d Mon Sep 17 00:00:00 2001 From: sureshb18 Date: Sat, 7 Feb 2026 18:57:24 -0600 Subject: [PATCH 2/3] Submit Design-1 --- .classpath | 6 ++++++ .gitignore | 1 + .project | 17 +++++++++++++++++ 3 files changed, 24 insertions(+) create mode 100644 .classpath create mode 100644 .gitignore create mode 100644 .project diff --git a/.classpath b/.classpath new file mode 100644 index 00000000..3f3893af --- /dev/null +++ b/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..4691fa86 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/MyHashSet.class diff --git a/.project b/.project new file mode 100644 index 00000000..4b98c917 --- /dev/null +++ b/.project @@ -0,0 +1,17 @@ + + + Design-1 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + From c04954bb84a93cfd883775bca0cacaeb208f8601 Mon Sep 17 00:00:00 2001 From: sureshb18 Date: Sat, 7 Feb 2026 20:18:52 -0600 Subject: [PATCH 3/3] Optimal solution --- Sample.java | 56 ++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 47 insertions(+), 9 deletions(-) diff --git a/Sample.java b/Sample.java index 62707481..f1186932 100644 --- a/Sample.java +++ b/Sample.java @@ -15,23 +15,61 @@ class MyHashSet { - private boolean[] data; +// private boolean[] data; +// +// public MyHashSet() { +// data = new boolean[1000001]; +// +// } +// +// public void add(int key) { +// data[key] = true; +// } +// +// public void remove(int key) { +// data[key] = false; +// } +// +// public boolean contains(int key) { +// return data[key]; +// } + + //Optimal solutions + + private int primaryBuckets = 1000; + private int secondaryBuckets = 1000; + private boolean[][] storage; public MyHashSet() { - data = new boolean[1000001]; - + storage = new boolean[primaryBuckets][]; } - + + private int getPrimaryHash(int key) { + return key % primaryBuckets; + } + + private int getSecondaryHash(int key) { + return key / secondaryBuckets; + } + public void add(int key) { - data[key] = true; + int p = getPrimaryHash(key); + if (storage[p] == null) { + storage[p] = new boolean[p == 0 ? secondaryBuckets + 1 : secondaryBuckets]; + } + storage[p][getSecondaryHash(key)] = true; } - + public void remove(int key) { - data[key] = false; + int p = getPrimaryHash(key); + if (storage[p] != null) { + storage[p][getSecondaryHash(key)] = false; + } } - + public boolean contains(int key) { - return data[key]; + int p = getPrimaryHash(key); + return storage[p] != null && storage[p][getSecondaryHash(key)]; } }