From 35db71c3fd9a0818c86996a2154e0911bb73e7a2 Mon Sep 17 00:00:00 2001 From: ktony Date: Sun, 22 Sep 2024 14:30:04 -0400 Subject: [PATCH 1/5] Reverse Linked List --- reverse-linked-list/TonyKim9401.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 reverse-linked-list/TonyKim9401.java diff --git a/reverse-linked-list/TonyKim9401.java b/reverse-linked-list/TonyKim9401.java new file mode 100644 index 00000000..70b754b7 --- /dev/null +++ b/reverse-linked-list/TonyKim9401.java @@ -0,0 +1,16 @@ +// TC: O(n) +// -> visit all elements of head +// SC: O(1) +// -> constant space complexity +class Solution { + public ListNode reverseList(ListNode head) { + ListNode node = null; + while (head != null) { + ListNode temp = head.next; + head.next = node; + node = head; + head = temp; + } + return node; + } +} From 4554647f8f936aa2dbec4a052285a85383bad236 Mon Sep 17 00:00:00 2001 From: ktony Date: Mon, 23 Sep 2024 17:18:59 -0400 Subject: [PATCH 2/5] Longest Substring Without Repeating Characters --- .../TonyKim9401.java | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 longest-substring-without-repeating-characters/TonyKim9401.java diff --git a/longest-substring-without-repeating-characters/TonyKim9401.java b/longest-substring-without-repeating-characters/TonyKim9401.java new file mode 100644 index 00000000..0d9fe866 --- /dev/null +++ b/longest-substring-without-repeating-characters/TonyKim9401.java @@ -0,0 +1,25 @@ +// TC: O(n^2) +// -> all elements can be retrived multiple times in the worst case +// SC: O(1) +// -> since declare, no more increase or decrease +class Solution { + public int lengthOfLongestSubstring(String s) { + int max = 0; + int count = 0; + boolean[] checkList = new boolean[128]; + + for (int i = 0; i < s.length(); i++) { + int idx = s.charAt(i); + if (checkList[idx]) { + max = Math.max(max, count); + i -= count; + count = 0; + checkList = new boolean[128]; + } else { + count += 1; + checkList[idx] = true; + } + } + return max = Math.max(max, count); + } +} From cd5c7ae814fce096ace1a967f6eb081498abe055 Mon Sep 17 00:00:00 2001 From: ktony Date: Wed, 25 Sep 2024 13:56:16 -0400 Subject: [PATCH 3/5] Number Of Islands --- number-of-islands/TonyKim9401.java | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 number-of-islands/TonyKim9401.java diff --git a/number-of-islands/TonyKim9401.java b/number-of-islands/TonyKim9401.java new file mode 100644 index 00000000..cb531fee --- /dev/null +++ b/number-of-islands/TonyKim9401.java @@ -0,0 +1,28 @@ +// TC: O(n * m) +// retrieve all elemetns, grid.length * grid[0].length +// SC: O(n * m( +// need to change all elements from 1 to 0 in the worst case +class Solution { + int output = 0; + public int numIslands(char[][] grid) { + for (int i = 0; i < grid.length; i++) { + for (int j = 0; j < grid[0].length; j++) { + if (grid[i][j] == '1') { + output += 1; + countIslands(i, j, grid); + } + } + } + return output; + } + + private void countIslands(int i, int j, char[][] grid) { + if (i < 0 || i >= grid.length || j < 0 || j >= grid[0].length) return; + if (grid[i][j] == '0') return; + grid[i][j] = '0'; + countIslands(i+1, j, grid); + countIslands(i-1, j, grid); + countIslands(i, j+1, grid); + countIslands(i, j-1, grid); + } +} From 836d3a2c752a956522b40c0ab3a2ac87ddd25614 Mon Sep 17 00:00:00 2001 From: ktony Date: Thu, 26 Sep 2024 14:29:21 -0400 Subject: [PATCH 4/5] Unique Paths --- unique-paths/TonyKim9401.java | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 unique-paths/TonyKim9401.java diff --git a/unique-paths/TonyKim9401.java b/unique-paths/TonyKim9401.java new file mode 100644 index 00000000..13ec71d5 --- /dev/null +++ b/unique-paths/TonyKim9401.java @@ -0,0 +1,18 @@ +// TC: O (m * n) +// SC: O (m * n) +// -> need to retrieve all elements +class Solution { + public int uniquePaths(int m, int n) { + int[][] dp = new int[m][n]; + + for (int i = 0; i < m; i++) dp[i][0] = 1; + for (int j = 0; j < n; j++) dp[0][j] = 1; + + for (int i = 1; i < m; i++) { + for (int j = 1; j < n; j++) { + dp[i][j] = dp[i - 1][j] + dp[i][j - 1]; + } + } + return dp[m-1][n-1]; + } +} From 0b8e4720f0366d92c2727fdaf53c2408d544fb36 Mon Sep 17 00:00:00 2001 From: ktony Date: Thu, 26 Sep 2024 14:29:35 -0400 Subject: [PATCH 5/5] Set Matrix Zeros --- set-matrix-zeroes/TonyKim9401.java | 32 ++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 set-matrix-zeroes/TonyKim9401.java diff --git a/set-matrix-zeroes/TonyKim9401.java b/set-matrix-zeroes/TonyKim9401.java new file mode 100644 index 00000000..710f6292 --- /dev/null +++ b/set-matrix-zeroes/TonyKim9401.java @@ -0,0 +1,32 @@ +// TC: O(n^2) +// SC: O(1) +class Solution { + public void setZeroes(int[][] matrix) { + boolean firstRow = false, firstCol = false; + + for (int i = 0; i < matrix.length; i++) { + for (int j = 0; j < matrix[0].length; j++) { + if (matrix[i][j] == 0) { + if (i == 0) firstRow = true; + if (j == 0) firstCol = true; + matrix[0][j] = 0; + matrix[i][0] = 0; + } + } + } + + for (int i = 1; i < matrix.length; i++) { + for (int j = 1; j < matrix[0].length; j++) { + if (matrix[i][0] == 0 || matrix[0][j] == 0) matrix[i][j] = 0; + } + } + + if (firstRow) { + for (int j = 0; j < matrix[0].length; j++) matrix[0][j] = 0; + } + + if (firstCol) { + for (int i = 0; i < matrix.length; i++) matrix[i][0] = 0; + } + } +}