From 8f5a1ad70d263e161eb1cbe45260881bb4a635a5 Mon Sep 17 00:00:00 2001 From: chjung99 Date: Sat, 17 Jan 2026 17:29:45 +0900 Subject: [PATCH 1/5] design-add-and-search-words-data-structure --- .../chjung99.java | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 design-add-and-search-words-data-structure/chjung99.java diff --git a/design-add-and-search-words-data-structure/chjung99.java b/design-add-and-search-words-data-structure/chjung99.java new file mode 100644 index 0000000000..4eb19b8557 --- /dev/null +++ b/design-add-and-search-words-data-structure/chjung99.java @@ -0,0 +1,51 @@ +class WordDictionary { + Node rootNode; + public WordDictionary() { + this.rootNode = new Node(); + } + + public void addWord(String word) { + Node node = rootNode; + + for (int i = 0; i < word.length(); i++) { + node = node.childNode.computeIfAbsent(word.charAt(i), key -> new Node()); + } + node.isEndOfWord = true; + } + + public boolean search(String word) { + return dfs(rootNode, word, 0); + } + + public boolean dfs(Node node, String word, int depth) { + if (depth == word.length()) return node.isEndOfWord; + if (word.charAt(depth) == '.') { + for (Node nextNode: node.childNode.values()) { + if (dfs(nextNode, word, depth + 1)) return true; + } + return false; + } else { + Node nextNode = node.childNode.getOrDefault(word.charAt(depth), null); + if (nextNode == null) return false; + return dfs(nextNode, word, depth+1); + } + } + + static class Node { + Map childNode; + boolean isEndOfWord; + + public Node() { + this.childNode = new HashMap<>(); + this.isEndOfWord = false; + } + } +} + +/** + * Your WordDictionary object will be instantiated and called as such: + * WordDictionary obj = new WordDictionary(); + * obj.addWord(word); + * boolean param_2 = obj.search(word); + */ + From 3e0d7a77a42fafb3d54222714d2fe9ba60779335 Mon Sep 17 00:00:00 2001 From: chjung99 Date: Sat, 17 Jan 2026 17:29:59 +0900 Subject: [PATCH 2/5] house-robber-ii --- house-robber-ii/chjung99.java | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 house-robber-ii/chjung99.java diff --git a/house-robber-ii/chjung99.java b/house-robber-ii/chjung99.java new file mode 100644 index 0000000000..3a7a0d7268 --- /dev/null +++ b/house-robber-ii/chjung99.java @@ -0,0 +1,30 @@ +class Solution { + public int rob(int[] nums) { + int n = nums.length; + if (n==1) { + return nums[0]; + } + return Math.max( + robLinear(Arrays.copyOfRange(nums, 1, n)), + robLinear(Arrays.copyOfRange(nums, 0, n-1)) + ); + } + + public int robLinear(int[] nums) { + int n = nums.length; + int[] dp = new int[n]; + + dp[0] = nums[0]; + if (n > 1) { + dp[1] = Math.max(nums[0], nums[1]); + } + + for (int i = 2; i < n; i++) { + dp[i] = Math.max(dp[i-2] + nums[i], dp[i-1]); + } + + return dp[n-1]; + } +} + + From 6f2ffd48d2cf00197110e82b54f3e84e98080c88 Mon Sep 17 00:00:00 2001 From: chjung99 Date: Sat, 17 Jan 2026 17:30:14 +0900 Subject: [PATCH 3/5] house-robber --- house-robber/chjung99.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 house-robber/chjung99.java diff --git a/house-robber/chjung99.java b/house-robber/chjung99.java new file mode 100644 index 0000000000..b81dcc9634 --- /dev/null +++ b/house-robber/chjung99.java @@ -0,0 +1,17 @@ +class Solution { + public int rob(int[] nums) { + int n = nums.length; + int[] dp = new int[n]; + dp[0] = nums[0]; + if (n >= 2) { + dp[1] = Math.max(nums[0], nums[1]); + } + + for (int i=2; i Date: Sat, 17 Jan 2026 17:30:27 +0900 Subject: [PATCH 4/5] implement-trie-prefix-tree --- implement-trie-prefix-tree/chjung99.java | 57 ++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 implement-trie-prefix-tree/chjung99.java diff --git a/implement-trie-prefix-tree/chjung99.java b/implement-trie-prefix-tree/chjung99.java new file mode 100644 index 0000000000..8bdaef44b0 --- /dev/null +++ b/implement-trie-prefix-tree/chjung99.java @@ -0,0 +1,57 @@ +class Trie { + Node rootNode = new Node(); + + public Trie() { + + } + + public void insert(String word) { + Node node = this.rootNode; + + for (int i = 0; i < word.length(); i++) { + node = node.childNode.computeIfAbsent(word.charAt(i), key -> new Node()); + } + node.isEndOfWord = true; + } + + public boolean search(String word) { + Node node = this.rootNode; + + for (int i = 0; i < word.length(); i++) { + node = node.childNode.getOrDefault(word.charAt(i), null); + if (node == null) return false; + } + return node.isEndOfWord; + } + + public boolean startsWith(String prefix) { + Node node = this.rootNode; + + for (int i = 0; i < prefix.length(); i++) { + node = node.childNode.getOrDefault(prefix.charAt(i), null); + if (node == null) return false; + } + return true; + } + + static class Node { + Map childNode = new HashMap<>(); + boolean isEndOfWord = false; + + public Node(){ + + } + } +} + +/** + * Your Trie object will be instantiated and called as such: + * Trie obj = new Trie(); + * obj.insert(word); + * boolean param_2 = obj.search(word); + * boolean param_3 = obj.startsWith(prefix); + */ + + + + From 354d8433ef7d611566029b2e59a3116d5f380c20 Mon Sep 17 00:00:00 2001 From: chjung99 Date: Sat, 17 Jan 2026 17:30:43 +0900 Subject: [PATCH 5/5] number-of-islands --- number-of-islands/chjung99.java | 56 +++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 number-of-islands/chjung99.java diff --git a/number-of-islands/chjung99.java b/number-of-islands/chjung99.java new file mode 100644 index 0000000000..0ecb25d27e --- /dev/null +++ b/number-of-islands/chjung99.java @@ -0,0 +1,56 @@ +class Solution { + int n; + int m; + int[] dx = new int[]{1, 0, -1, 0}; + int[] dy = new int[]{0, 1, 0, -1}; + + public int numIslands(char[][] grid) { + n = grid.length; + m = grid[0].length; + boolean[][] visit = new boolean[n][m]; + int cnt = 0; + + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++){ + if (!visit[i][j] && grid[i][j] == '1') { + cnt ++; + bfs(i, j, grid, visit); + } + } + } + return cnt; + } + + public void bfs(int cx, int cy, char[][] grid, boolean[][] visit) { + Deque deque = new ArrayDeque<>(); + visit[cx][cy] = true; + deque.add(new Point(cx, cy)); + + while (!deque.isEmpty()) { + Point cur = deque.poll(); + for (int i = 0; i < 4; i++) { + int nx = cur.x + dx[i]; + int ny = cur.y + dy[i]; + + if (outOfRange(nx, ny) || visit[nx][ny] || grid[nx][ny] == '0') continue; + deque.add(new Point(nx,ny)); + visit[nx][ny] = true; + } + } + } + + public boolean outOfRange(int x, int y) { + return ((x < 0 || x >= n) || (y < 0 || y >= m)); + } + + static class Point{ + int x; + int y; + public Point(int x, int y) { + this.x = x; + this.y = y; + } + } +} + +