From ba08f52ddb91eb57f013d65cbdda082ca0c2ea80 Mon Sep 17 00:00:00 2001 From: Jeongwon Na Date: Sun, 24 Aug 2025 12:45:19 +0200 Subject: [PATCH 01/10] solution: valid parentheses --- valid-parentheses/njngwn.java | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 valid-parentheses/njngwn.java diff --git a/valid-parentheses/njngwn.java b/valid-parentheses/njngwn.java new file mode 100644 index 000000000..3c18e535d --- /dev/null +++ b/valid-parentheses/njngwn.java @@ -0,0 +1,24 @@ +// Time Complexity: O(n), n: s.length +// Space Complexity: O(n), n: s.length (worst case: s="(((((((") +class Solution { + public boolean isValid(String s) { + Stack bracketStack = new Stack<>(); + + for (char ch : s.toCharArray()) { + if (ch == '(' || ch == '{' || ch == '[') { // open bracket + bracketStack.push(ch); + } else { // close bracket + if (bracketStack.empty()) { + return false; + } + + char sp = bracketStack.pop(); + if (!((sp == '(' && ch == ')') || (sp == '{' && ch == '}') || (sp == '[' && ch == ']'))) { + return false; + } + } + } + + return bracketStack.empty(); + } +} From e037d9e11fde6922fd7a2f584d67a3d1faf6d74a Mon Sep 17 00:00:00 2001 From: Jeongwon Na Date: Sun, 24 Aug 2025 12:46:34 +0200 Subject: [PATCH 02/10] solution: container with most water --- container-with-most-water/njngwn.java | 28 +++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 container-with-most-water/njngwn.java diff --git a/container-with-most-water/njngwn.java b/container-with-most-water/njngwn.java new file mode 100644 index 000000000..4d4f8c795 --- /dev/null +++ b/container-with-most-water/njngwn.java @@ -0,0 +1,28 @@ +// Time Complexity: O(n), n: height.length +// Space Complexity: O(1) +class Solution { + public int maxArea(int[] height) { + int maxWaterAmount = 0; + int leftLineIdx = 0; + int rightLineIdx = height.length-1; + + while (leftLineIdx < rightLineIdx) { + int leftHeight = height[leftLineIdx]; + int rightHeight = height[rightLineIdx]; + int tempAmount = 0; + + if (leftHeight < rightHeight) { + tempAmount = leftHeight * (rightLineIdx - leftLineIdx); + leftLineIdx++; + } else { + tempAmount = rightHeight * (rightLineIdx - leftLineIdx); + rightLineIdx--; + } + + // update maximum amount + maxWaterAmount = tempAmount > maxWaterAmount ? tempAmount : maxWaterAmount; + } + + return maxWaterAmount; + } +} \ No newline at end of file From 1bd18b66ba8bc55156251c62d34066b242a5f87d Mon Sep 17 00:00:00 2001 From: Jeongwon Na Date: Sun, 24 Aug 2025 12:47:50 +0200 Subject: [PATCH 03/10] solution: design add and search words and data structure --- .../njngwn.java | 63 +++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 design-add-and-search-words-data-structure/njngwn.java diff --git a/design-add-and-search-words-data-structure/njngwn.java b/design-add-and-search-words-data-structure/njngwn.java new file mode 100644 index 000000000..e0cf70096 --- /dev/null +++ b/design-add-and-search-words-data-structure/njngwn.java @@ -0,0 +1,63 @@ +class WordDictionary { + private static class CharDictionary { + HashMap charMap; + boolean isEnd; + + private CharDictionary() { + this.charMap = new HashMap<>(); + this.isEnd = false; + } + } + + private CharDictionary rootNode; + + public WordDictionary() { + this.rootNode = new CharDictionary(); + } + + public void addWord(String word) { + CharDictionary currentNode = this.rootNode; + + for (char ch : word.toCharArray()) { + if (!currentNode.charMap.containsKey(ch)) { + currentNode.charMap.put(ch, new CharDictionary()); + } + currentNode = currentNode.charMap.get(ch); + } + currentNode.isEnd = true; + } + + public boolean search(String word) { + return searchRecursive(word, this.rootNode, 0); + } + + private boolean searchRecursive(String word, CharDictionary node, int index) { + // Base case + if (index == word.length()) { + return node.isEnd; + } + + char ch = word.charAt(index); + + if (ch == '.') { + for (CharDictionary childNode : node.charMap.values()) { + if (searchRecursive(word, childNode, index + 1)) { + return true; + } + } + return false; + } else { + if (!node.charMap.containsKey(ch)) { + return false; + } + return searchRecursive(word, node.charMap.get(ch), index + 1); + } + } +} + +/** + * Your WordDictionary object will be instantiated and called as such: + * WordDictionary obj = new WordDictionary(); + * obj.addWord(word); + * boolean param_2 = obj.search(word); + */ \ No newline at end of file From f395df8b21d63c0a989f3059462e4c3532e7fc40 Mon Sep 17 00:00:00 2001 From: Jeongwon Na Date: Mon, 25 Aug 2025 21:29:34 +0200 Subject: [PATCH 04/10] add inline --- design-add-and-search-words-data-structure/njngwn.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/design-add-and-search-words-data-structure/njngwn.java b/design-add-and-search-words-data-structure/njngwn.java index e0cf70096..f636fd40b 100644 --- a/design-add-and-search-words-data-structure/njngwn.java +++ b/design-add-and-search-words-data-structure/njngwn.java @@ -60,4 +60,4 @@ private boolean searchRecursive(String word, CharDictionary node, int index) { * WordDictionary obj = new WordDictionary(); * obj.addWord(word); * boolean param_2 = obj.search(word); - */ \ No newline at end of file + */ From 93385ae55aeaa57dded602d225a069f59883ec0c Mon Sep 17 00:00:00 2001 From: Jeongwon Na Date: Mon, 25 Aug 2025 21:30:50 +0200 Subject: [PATCH 05/10] add inline --- container-with-most-water/njngwn.java | 2 +- leetcode-study.iml | 85 +++++++++++++++++++++++++++ 2 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 leetcode-study.iml diff --git a/container-with-most-water/njngwn.java b/container-with-most-water/njngwn.java index 4d4f8c795..579e8073d 100644 --- a/container-with-most-water/njngwn.java +++ b/container-with-most-water/njngwn.java @@ -25,4 +25,4 @@ public int maxArea(int[] height) { return maxWaterAmount; } -} \ No newline at end of file +} diff --git a/leetcode-study.iml b/leetcode-study.iml new file mode 100644 index 000000000..5ea48dee0 --- /dev/null +++ b/leetcode-study.iml @@ -0,0 +1,85 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file From a69f7b5b3ced6089d7cdb24a2eb5499c3887ea1e Mon Sep 17 00:00:00 2001 From: Jeongwon Na Date: Mon, 25 Aug 2025 21:35:25 +0200 Subject: [PATCH 06/10] Revert "add inline" This reverts commit 93385ae55aeaa57dded602d225a069f59883ec0c. --- container-with-most-water/njngwn.java | 2 +- leetcode-study.iml | 85 --------------------------- 2 files changed, 1 insertion(+), 86 deletions(-) delete mode 100644 leetcode-study.iml diff --git a/container-with-most-water/njngwn.java b/container-with-most-water/njngwn.java index 579e8073d..4d4f8c795 100644 --- a/container-with-most-water/njngwn.java +++ b/container-with-most-water/njngwn.java @@ -25,4 +25,4 @@ public int maxArea(int[] height) { return maxWaterAmount; } -} +} \ No newline at end of file diff --git a/leetcode-study.iml b/leetcode-study.iml deleted file mode 100644 index 5ea48dee0..000000000 --- a/leetcode-study.iml +++ /dev/null @@ -1,85 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file From f3b689d614e86bd3efc7f730c7e191b4bb9e57ac Mon Sep 17 00:00:00 2001 From: Jeongwon Na Date: Mon, 25 Aug 2025 21:37:27 +0200 Subject: [PATCH 07/10] add inline --- container-with-most-water/njngwn.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/container-with-most-water/njngwn.java b/container-with-most-water/njngwn.java index 4d4f8c795..579e8073d 100644 --- a/container-with-most-water/njngwn.java +++ b/container-with-most-water/njngwn.java @@ -25,4 +25,4 @@ public int maxArea(int[] height) { return maxWaterAmount; } -} \ No newline at end of file +} From 621834d58a50f5c2bb873b4412e26c1f96a8ab96 Mon Sep 17 00:00:00 2001 From: Jeongwon Na Date: Fri, 29 Aug 2025 18:51:27 +0200 Subject: [PATCH 08/10] solution: longest increasing subsequence --- longest-increasing-subsequence/njngwn.java | 23 ++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 longest-increasing-subsequence/njngwn.java diff --git a/longest-increasing-subsequence/njngwn.java b/longest-increasing-subsequence/njngwn.java new file mode 100644 index 000000000..654a9d340 --- /dev/null +++ b/longest-increasing-subsequence/njngwn.java @@ -0,0 +1,23 @@ +// time complexity: O(nlogn), n: nums.length (logn because of binary search) +// space complexity: O(n), n: nums.length +class Solution { + public int lengthOfLIS(int[] nums) { + ArrayList incSeqList = new ArrayList(); // dp + incSeqList.add(nums[0]); + + for (int num : nums) { + if (num > incSeqList.get(incSeqList.size()-1)) { + // add element to incSeqLit + incSeqList.add(num); + } else { + int idx = Collections.binarySearch(incSeqList, num); + if (idx < 0) { // idx returns -(insertedPos + 1) + int insertedIdx = -(idx + 1); + incSeqList.set(insertedIdx, num); + } + } + } + + return incSeqList.size(); + } +} From 5bc4b2685438774eefc0dc3985067a07be225a8b Mon Sep 17 00:00:00 2001 From: Jeongwon Na Date: Fri, 29 Aug 2025 19:19:46 +0200 Subject: [PATCH 09/10] solution: spiral matrix --- spiral-matrix/njngwn.java | 44 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 spiral-matrix/njngwn.java diff --git a/spiral-matrix/njngwn.java b/spiral-matrix/njngwn.java new file mode 100644 index 000000000..60190a138 --- /dev/null +++ b/spiral-matrix/njngwn.java @@ -0,0 +1,44 @@ +// Time Complexity: O(m+n), m: matrix.length, n: matrix[0].length +// Space Complexity: O(m*n), m: matrix.length, n: matrix[0].length, because of arraylist for output +class Solution { + public List spiralOrder(int[][] matrix) { + int rowMin = 0; + int rowMax = matrix.length-1; + int colMin = 0; + int colMax = matrix[0].length-1; + + ArrayList orderedElements = new ArrayList<>(); + + while (rowMin <= rowMax && colMin <= colMax) { + // left to right + for (int col = colMin; col <= colMax; ++col) { + orderedElements.add(matrix[rowMin][col]); + } + rowMin++; + + // top to bottom + for (int row = rowMin; row <= rowMax; ++row) { + orderedElements.add(matrix[row][colMax]); + } + colMax--; + + // right to left + if (rowMin <= rowMax) { + for (int col = colMax; col >= colMin; --col) { + orderedElements.add(matrix[rowMax][col]); + } + } + rowMax--; + + // bottom to top + if (colMin <= colMax) { + for (int row = rowMax; row >= rowMin; --row) { + orderedElements.add(matrix[row][colMin]); + } + } + colMin++; + } + + return orderedElements; + } +} From 75361b79f09b46c21d955827a0a700f1942677b3 Mon Sep 17 00:00:00 2001 From: Jeongwon Na Date: Sun, 31 Aug 2025 12:08:36 +0200 Subject: [PATCH 10/10] solution: reverse-linked-list using iteration --- reverse-linked-list/njngwn.java | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 reverse-linked-list/njngwn.java diff --git a/reverse-linked-list/njngwn.java b/reverse-linked-list/njngwn.java new file mode 100644 index 000000000..94ee5ac9d --- /dev/null +++ b/reverse-linked-list/njngwn.java @@ -0,0 +1,27 @@ +/** + * Definition for singly-linked list. + * public class ListNode { + * int val; + * ListNode next; + * ListNode() {} + * ListNode(int val) { this.val = val; } + * ListNode(int val, ListNode next) { this.val = val; this.next = next; } + * } + */ +// Time Complexity: O(n) +// Sprace Compexity: O(1) +class Solution { + public ListNode reverseList(ListNode head) { + ListNode prevNode = null; + ListNode currNode = head; + + while (currNode != null) { + ListNode nextNode = currNode.next; + currNode.next = prevNode; + prevNode = currNode; + currNode = nextNode; + } + + return prevNode; + } +}