From 4102d1a13606cae9d28eb192b5d3ab6a8e6eb707 Mon Sep 17 00:00:00 2001 From: jinhyungrhee Date: Wed, 7 May 2025 11:57:38 +0900 Subject: [PATCH 1/6] add solution of valid-parentheses --- valid-parentheses/jinhyungrhee.java | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 valid-parentheses/jinhyungrhee.java diff --git a/valid-parentheses/jinhyungrhee.java b/valid-parentheses/jinhyungrhee.java new file mode 100644 index 000000000..d229148d0 --- /dev/null +++ b/valid-parentheses/jinhyungrhee.java @@ -0,0 +1,27 @@ +import java.util.*; +class Solution { + public boolean isValid(String s) { + + Deque stack = new ArrayDeque<>(); + Map table = new HashMap<>(); + table.put(')', '('); + table.put(']', '['); + table.put('}', '{'); + + for (int i = 0; i < s.length(); i++) { + if (table.containsKey(s.charAt(i))) { + + if ((table.get(s.charAt(i))).equals(stack.peek())) { + stack.pop(); + } else { + stack.push(s.charAt(i)); + } + + } else { + stack.push(s.charAt(i)); + } + } + + return stack.isEmpty(); + } +} From f3230f5aba2652dbbbc31668592caf6ba730af45 Mon Sep 17 00:00:00 2001 From: jinhyungrhee Date: Fri, 9 May 2025 15:02:54 +0900 Subject: [PATCH 2/6] add solution of design-add-and-search-words-data-structure --- .../jinhyungrhee.java | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 design-add-and-search-words-data-structure/jinhyungrhee.java diff --git a/design-add-and-search-words-data-structure/jinhyungrhee.java b/design-add-and-search-words-data-structure/jinhyungrhee.java new file mode 100644 index 000000000..6b58cd939 --- /dev/null +++ b/design-add-and-search-words-data-structure/jinhyungrhee.java @@ -0,0 +1,56 @@ +class TrieNode { + boolean word; + TrieNode[] children; + + TrieNode() { + this.word = false; + this.children = new TrieNode[27]; + } +} + +class WordDictionary { + TrieNode root; + public WordDictionary() { + this.root = new TrieNode(); + } + + public void addWord(String word) { + + TrieNode curr = root; + for (char c : word.toCharArray()) { + if (curr.children[c - 'a'] == null) { + curr.children[c - 'a'] = new TrieNode(); + } + curr = curr.children[c - 'a']; + } + curr.word = true; + } + + public boolean search(String word) { + return dfs(word, 0, root); + } + + public boolean dfs(String word, int index, TrieNode node) { + + if (index == word.length()) { + return node.word; + } + + char c = word.charAt(index); + + if (c == '.') { + for (TrieNode child : node.children) { + if (child != null && dfs(word, index + 1, child)) { + return true; + } + } + return false; + + } else { + + TrieNode next = node.children[c - 'a']; + return next != null && dfs(word, index + 1, next); + + } + } +} From d8fac4be7f8545a4466910f03742fefd3dc6fae6 Mon Sep 17 00:00:00 2001 From: jinhyungrhee Date: Fri, 9 May 2025 15:04:11 +0900 Subject: [PATCH 3/6] add solution of longest-increasing-subsequence --- .../jinhyungrhee.java | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 longest-increasing-subsequence/jinhyungrhee.java diff --git a/longest-increasing-subsequence/jinhyungrhee.java b/longest-increasing-subsequence/jinhyungrhee.java new file mode 100644 index 000000000..f2f79283d --- /dev/null +++ b/longest-increasing-subsequence/jinhyungrhee.java @@ -0,0 +1,23 @@ +import java.util.*; +class Solution { + public int lengthOfLIS(int[] nums) { + + int[] dp = new int[nums.length]; + Arrays.fill(dp, 1); + + for (int i = 1 ; i < nums.length; i++) { + for (int j = i; j >= 0; j--) { + if (nums[j] < nums[i]) { + dp[i] = Math.max(dp[i], dp[j] + 1); + } + } + } + + int maxVal = 0; + for (int num : dp) { + if (num > maxVal) maxVal = num; + } + + return maxVal; + } +} From 380b1ae63499453ee4cc4d852bdaa6023292e161 Mon Sep 17 00:00:00 2001 From: jinhyungrhee Date: Sat, 10 May 2025 11:38:52 +0900 Subject: [PATCH 4/6] add solution of spiral-matrix --- spiral-matrix/jinhyungrhee.java | 49 +++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 spiral-matrix/jinhyungrhee.java diff --git a/spiral-matrix/jinhyungrhee.java b/spiral-matrix/jinhyungrhee.java new file mode 100644 index 000000000..da8b2b9e0 --- /dev/null +++ b/spiral-matrix/jinhyungrhee.java @@ -0,0 +1,49 @@ +import java.util.*; +class Solution { + public List spiralOrder(int[][] matrix) { + + List result = new ArrayList<>(); + + int left = 0; + int right = matrix[0].length - 1; + int top = 0; + int bottom = matrix.length - 1; + + while (left <= right && top <= bottom) { + + // 위쪽 행을 순회한 후, 상단 경계를 1 증가 + for (int y = left; y <= right; y++) { + result.add(matrix[top][y]); + } + top++; + + // 상단 인덱스가 하단 인덱스 보다 커지면 순회 중단 + if (top > bottom) break; + + // 우측 열을 순회한 후, 우측 경계를 1 감소 + for (int x = top; x <= bottom; x++) { + result.add(matrix[x][right]); + } + right--; + + // 우측 인덱스가 좌측 인덱스보다 작아지면 순회 중단 + if (right < left) break; + + // 아래쪽 행을 순회한 후, 하단 경계를 1 감소 + for (int y = right; y >= left; y--) { + result.add(matrix[bottom][y]); + } + bottom--; + + // 왼쪽 열을 순회한 후, 좌측 경계를 1 증가 + for (int x = bottom; x >= top; x--) { + result.add(matrix[x][left]); + } + left++; + + } + return result; + + } + +} From 8ad2f399945f7c77b1b0e695b50466d72c6ef5cc Mon Sep 17 00:00:00 2001 From: jinhyungrhee Date: Sat, 10 May 2025 13:04:41 +0900 Subject: [PATCH 5/6] add solution of container-with-most-water --- container-with-most-water/jinhyungrhee.java | 32 +++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 container-with-most-water/jinhyungrhee.java diff --git a/container-with-most-water/jinhyungrhee.java b/container-with-most-water/jinhyungrhee.java new file mode 100644 index 000000000..ab87840c3 --- /dev/null +++ b/container-with-most-water/jinhyungrhee.java @@ -0,0 +1,32 @@ +class Solution { + + /** + * time-complexity : O(n) + * space-complexity : O(1) + */ + + public int maxArea(int[] height) { + + int left = 0; + int right = height.length - 1; + + int w = 0, h = 0, currSize = 0, maxSize = 0; + + while (left < right) { + + w = right - left; + h = Math.min(height[left], height[right]); + + currSize = w * h; + maxSize = Math.max(currSize, maxSize); + + if (height[left] < height[right]) { + left++; + } else { + right--; + } + + } + return maxSize; + } +} From 4f627c150115a3544c5b89c21b6b7ee079c338b9 Mon Sep 17 00:00:00 2001 From: jinhyungrhee Date: Sat, 10 May 2025 14:44:52 +0900 Subject: [PATCH 6/6] add time-complexity and space-complexity --- .../jinhyungrhee.java | 19 +++++++++++++++++++ .../jinhyungrhee.java | 4 ++++ spiral-matrix/jinhyungrhee.java | 4 ++++ valid-parentheses/jinhyungrhee.java | 4 ++++ 4 files changed, 31 insertions(+) diff --git a/design-add-and-search-words-data-structure/jinhyungrhee.java b/design-add-and-search-words-data-structure/jinhyungrhee.java index 6b58cd939..1cf377293 100644 --- a/design-add-and-search-words-data-structure/jinhyungrhee.java +++ b/design-add-and-search-words-data-structure/jinhyungrhee.java @@ -9,11 +9,22 @@ class TrieNode { } class WordDictionary { + /** + * space-complexity : O(N * L) (w.c) + * - N : 단어 수, L : 평균 길이 + * - 단어 하나 추가 시 최대 L개의 TrieNode 생성 + * - (w.c) 모든 단어가 중복 없이 추가되면 (N*L)개의 노드 필요 + */ TrieNode root; public WordDictionary() { this.root = new TrieNode(); } + /** + * addWord() Time-complexity : O(L) + * - 각 문자마다 TrieNode를 따라 내려가며 필요한 경우 새 노드 생성 + * - 한 단어당 최대 L개의 노드 생성 및 접근 + */ public void addWord(String word) { TrieNode curr = root; @@ -26,6 +37,14 @@ public void addWord(String word) { curr.word = true; } + /** + * search() Time-complexity : + * - '.'가 없는 경우 : O(L) + * -> 일반적인 문자는 한 경로만 탐색 + * - '.'가 있는 경우 : O(26^L) (w.c) + * -> 현재 노드의 모든 자식에 대해 DFS 수행 + * + */ public boolean search(String word) { return dfs(word, 0, root); } diff --git a/longest-increasing-subsequence/jinhyungrhee.java b/longest-increasing-subsequence/jinhyungrhee.java index f2f79283d..e5d5a6b64 100644 --- a/longest-increasing-subsequence/jinhyungrhee.java +++ b/longest-increasing-subsequence/jinhyungrhee.java @@ -1,5 +1,9 @@ import java.util.*; class Solution { + /** + * time-complexity : O(n^2) + * space-complexity : O(n) + */ public int lengthOfLIS(int[] nums) { int[] dp = new int[nums.length]; diff --git a/spiral-matrix/jinhyungrhee.java b/spiral-matrix/jinhyungrhee.java index da8b2b9e0..b119725af 100644 --- a/spiral-matrix/jinhyungrhee.java +++ b/spiral-matrix/jinhyungrhee.java @@ -1,5 +1,9 @@ import java.util.*; class Solution { + /** + * time-complexity : O(n * m) + * space-complexity : O(1) (excluding the output List) + */ public List spiralOrder(int[][] matrix) { List result = new ArrayList<>(); diff --git a/valid-parentheses/jinhyungrhee.java b/valid-parentheses/jinhyungrhee.java index d229148d0..a60a05a74 100644 --- a/valid-parentheses/jinhyungrhee.java +++ b/valid-parentheses/jinhyungrhee.java @@ -1,5 +1,9 @@ import java.util.*; class Solution { + /** + * time-complexity : O(n) + * space-complexity : O(n) + */ public boolean isValid(String s) { Deque stack = new ArrayDeque<>();