Skip to content

Commit 51d3c70

Browse files
author
Jeongwon Na
committed
solved week 07
1 parent 75361b7 commit 51d3c70

File tree

4 files changed

+101
-0
lines changed

4 files changed

+101
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Time Complexity: O(n), n: s.length()
2+
// Space Complexity: O(n), n: s.length()
3+
class Solution {
4+
public int lengthOfLongestSubstring(String s) {
5+
int begin = 0;
6+
int end = 0;
7+
int maxLength = 0;
8+
9+
Set<Character> letterSet = new HashSet<>();
10+
while (end < s.length()) {
11+
// if the letter is not in the hashset, then add it into hashset and move end pointer
12+
if (!letterSet.contains(s.charAt(end))) {
13+
letterSet.add(s.charAt(end));
14+
++end;
15+
maxLength = Math.max(maxLength, end - begin); // update maxlength
16+
} else { // if the letter is in the hashset, then remove it and move begin pointer
17+
letterSet.remove(s.charAt(begin));
18+
++begin;
19+
}
20+
}
21+
22+
return maxLength;
23+
}
24+
}

number-of-islands/njngwn.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Time Complexity: O(m*n), m: number of row, n: number of column
2+
// Space Complexity: O(m*n), m: number of row, n: number of column, because of call stack
3+
class Solution {
4+
void findLand(char[][] grid, int row, int col) {
5+
if (row < 0 || row > grid.length-1 || col < 0 || col > grid[0].length-1) return;
6+
if (grid[row][col] == '0') return;
7+
8+
grid[row][col] = '0'; // make element '0'(water)
9+
10+
findLand(grid, row-1, col);
11+
findLand(grid, row+1, col);
12+
findLand(grid, row, col-1);
13+
findLand(grid, row, col+1);
14+
}
15+
16+
public int numIslands(char[][] grid) {
17+
int num = 0;
18+
19+
for (int i = 0; i < grid.length; ++i) {
20+
for (int j = 0; j < grid[0].length; ++j) {
21+
if (grid[i][j] == '1') {
22+
findLand(grid, i, j);
23+
num++;
24+
}
25+
}
26+
}
27+
28+
return num;
29+
}
30+
}

set-matrix-zeroes/njngwn.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Time Complexity: O(m*n)
2+
// Space Complexity: O(m+n)
3+
class Solution {
4+
public void setZeroes(int[][] matrix) {
5+
ArrayList<Integer> rowList = new ArrayList<>();
6+
ArrayList<Integer> colList = new ArrayList<>();
7+
8+
for (int m = 0; m < matrix.length; ++m) {
9+
for (int n = 0; n < matrix[0].length; ++n) {
10+
if (matrix[m][n] != 0) continue;
11+
if (!rowList.contains(m)) rowList.add(m);
12+
if (!colList.contains(n)) colList.add(n);
13+
}
14+
}
15+
16+
for (Integer row : rowList) {
17+
for (int n = 0; n < matrix[0].length; ++n) {
18+
matrix[row][n] = 0;
19+
}
20+
}
21+
22+
for (Integer col : colList) {
23+
for (int n = 0; n < matrix.length; ++n) {
24+
matrix[n][col] = 0;
25+
}
26+
}
27+
}
28+
}

unique-paths/njngwn.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Time Complexity: O(m*n), m: number of row, n: number of column
2+
// Space Complexity: O(m*n), m: number of row, n: number of column
3+
class Solution {
4+
public int uniquePaths(int m, int n) {
5+
int[][] pathMap = new int[m][n];
6+
7+
for (int i = 0; i < m; ++i) {
8+
for (int j = 0; j < n; ++j) {
9+
if (i == 0 || j == 0) {
10+
pathMap[i][j] = 1;
11+
} else {
12+
pathMap[i][j] = pathMap[i-1][j] + pathMap[i][j-1];
13+
}
14+
}
15+
}
16+
17+
return pathMap[m-1][n-1];
18+
}
19+
}

0 commit comments

Comments
 (0)