Skip to content

Commit

Permalink
Merge pull request #485 from TonyKim9401/main
Browse files Browse the repository at this point in the history
[TONY] WEEK 07 Solutions
  • Loading branch information
TonyKim9401 authored Sep 27, 2024
2 parents 72015a4 + 0b8e472 commit c127512
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 0 deletions.
25 changes: 25 additions & 0 deletions longest-substring-without-repeating-characters/TonyKim9401.java
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);
}
}
28 changes: 28 additions & 0 deletions number-of-islands/TonyKim9401.java
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);
}
}
16 changes: 16 additions & 0 deletions reverse-linked-list/TonyKim9401.java
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;
}
}
32 changes: 32 additions & 0 deletions set-matrix-zeroes/TonyKim9401.java
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;
}
}
}
18 changes: 18 additions & 0 deletions unique-paths/TonyKim9401.java
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];
}
}

0 comments on commit c127512

Please sign in to comment.