-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #485 from TonyKim9401/main
[TONY] WEEK 07 Solutions
- Loading branch information
Showing
5 changed files
with
119 additions
and
0 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
longest-substring-without-repeating-characters/TonyKim9401.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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]; | ||
} | ||
} |