From 51d3c7047db46cde9c905e86ce09072bdf3fa73a Mon Sep 17 00:00:00 2001 From: Jeongwon Na Date: Wed, 3 Sep 2025 15:11:52 +0200 Subject: [PATCH 1/4] solved week 07 --- .../njngwn.java | 24 +++++++++++++++ number-of-islands/njngwn.java | 30 +++++++++++++++++++ set-matrix-zeroes/njngwn.java | 28 +++++++++++++++++ unique-paths/njngwn.java | 19 ++++++++++++ 4 files changed, 101 insertions(+) create mode 100644 longest-substring-without-repeating-characters/njngwn.java create mode 100644 number-of-islands/njngwn.java create mode 100644 set-matrix-zeroes/njngwn.java create mode 100644 unique-paths/njngwn.java diff --git a/longest-substring-without-repeating-characters/njngwn.java b/longest-substring-without-repeating-characters/njngwn.java new file mode 100644 index 000000000..c6b87adfc --- /dev/null +++ b/longest-substring-without-repeating-characters/njngwn.java @@ -0,0 +1,24 @@ +// Time Complexity: O(n), n: s.length() +// Space Complexity: O(n), n: s.length() +class Solution { + public int lengthOfLongestSubstring(String s) { + int begin = 0; + int end = 0; + int maxLength = 0; + + Set letterSet = new HashSet<>(); + while (end < s.length()) { + // if the letter is not in the hashset, then add it into hashset and move end pointer + if (!letterSet.contains(s.charAt(end))) { + letterSet.add(s.charAt(end)); + ++end; + maxLength = Math.max(maxLength, end - begin); // update maxlength + } else { // if the letter is in the hashset, then remove it and move begin pointer + letterSet.remove(s.charAt(begin)); + ++begin; + } + } + + return maxLength; + } +} \ No newline at end of file diff --git a/number-of-islands/njngwn.java b/number-of-islands/njngwn.java new file mode 100644 index 000000000..4a310122a --- /dev/null +++ b/number-of-islands/njngwn.java @@ -0,0 +1,30 @@ +// Time Complexity: O(m*n), m: number of row, n: number of column +// Space Complexity: O(m*n), m: number of row, n: number of column, because of call stack +class Solution { + void findLand(char[][] grid, int row, int col) { + if (row < 0 || row > grid.length-1 || col < 0 || col > grid[0].length-1) return; + if (grid[row][col] == '0') return; + + grid[row][col] = '0'; // make element '0'(water) + + findLand(grid, row-1, col); + findLand(grid, row+1, col); + findLand(grid, row, col-1); + findLand(grid, row, col+1); + } + + public int numIslands(char[][] grid) { + int num = 0; + + for (int i = 0; i < grid.length; ++i) { + for (int j = 0; j < grid[0].length; ++j) { + if (grid[i][j] == '1') { + findLand(grid, i, j); + num++; + } + } + } + + return num; + } +} \ No newline at end of file diff --git a/set-matrix-zeroes/njngwn.java b/set-matrix-zeroes/njngwn.java new file mode 100644 index 000000000..b57420763 --- /dev/null +++ b/set-matrix-zeroes/njngwn.java @@ -0,0 +1,28 @@ +// Time Complexity: O(m*n) +// Space Complexity: O(m+n) +class Solution { + public void setZeroes(int[][] matrix) { + ArrayList rowList = new ArrayList<>(); + ArrayList colList = new ArrayList<>(); + + for (int m = 0; m < matrix.length; ++m) { + for (int n = 0; n < matrix[0].length; ++n) { + if (matrix[m][n] != 0) continue; + if (!rowList.contains(m)) rowList.add(m); + if (!colList.contains(n)) colList.add(n); + } + } + + for (Integer row : rowList) { + for (int n = 0; n < matrix[0].length; ++n) { + matrix[row][n] = 0; + } + } + + for (Integer col : colList) { + for (int n = 0; n < matrix.length; ++n) { + matrix[n][col] = 0; + } + } + } +} diff --git a/unique-paths/njngwn.java b/unique-paths/njngwn.java new file mode 100644 index 000000000..096dc97ee --- /dev/null +++ b/unique-paths/njngwn.java @@ -0,0 +1,19 @@ +// Time Complexity: O(m*n), m: number of row, n: number of column +// Space Complexity: O(m*n), m: number of row, n: number of column +class Solution { + public int uniquePaths(int m, int n) { + int[][] pathMap = new int[m][n]; + + for (int i = 0; i < m; ++i) { + for (int j = 0; j < n; ++j) { + if (i == 0 || j == 0) { + pathMap[i][j] = 1; + } else { + pathMap[i][j] = pathMap[i-1][j] + pathMap[i][j-1]; + } + } + } + + return pathMap[m-1][n-1]; + } +} From 7f88af97e06d0e1e0273113ef2bb274b30d70adb Mon Sep 17 00:00:00 2001 From: Jeongwon Na Date: Wed, 3 Sep 2025 15:27:55 +0200 Subject: [PATCH 2/4] add last line --- longest-substring-without-repeating-characters/njngwn.java | 2 +- number-of-islands/njngwn.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/longest-substring-without-repeating-characters/njngwn.java b/longest-substring-without-repeating-characters/njngwn.java index c6b87adfc..d31b689ba 100644 --- a/longest-substring-without-repeating-characters/njngwn.java +++ b/longest-substring-without-repeating-characters/njngwn.java @@ -21,4 +21,4 @@ public int lengthOfLongestSubstring(String s) { return maxLength; } -} \ No newline at end of file +} diff --git a/number-of-islands/njngwn.java b/number-of-islands/njngwn.java index 4a310122a..b375a61bf 100644 --- a/number-of-islands/njngwn.java +++ b/number-of-islands/njngwn.java @@ -27,4 +27,4 @@ public int numIslands(char[][] grid) { return num; } -} \ No newline at end of file +} From f5ab6f7a04ba4d3890c87f654c5c648d5694ef69 Mon Sep 17 00:00:00 2001 From: Jeongwon Na Date: Wed, 3 Sep 2025 15:34:05 +0200 Subject: [PATCH 3/4] Revert "solution: reverse-linked-list using iteration" This reverts commit 75361b79f09b46c21d955827a0a700f1942677b3. --- reverse-linked-list/njngwn.java | 27 --------------------------- 1 file changed, 27 deletions(-) delete mode 100644 reverse-linked-list/njngwn.java diff --git a/reverse-linked-list/njngwn.java b/reverse-linked-list/njngwn.java deleted file mode 100644 index 94ee5ac9d..000000000 --- a/reverse-linked-list/njngwn.java +++ /dev/null @@ -1,27 +0,0 @@ -/** - * 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; - } -} From 13cd15b3ae7d4ca807577a0cad61db10ed344263 Mon Sep 17 00:00:00 2001 From: Jeongwon Na Date: Wed, 3 Sep 2025 15:35:38 +0200 Subject: [PATCH 4/4] solution: reverse linked list --- 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; + } +}