Skip to content

Commit 4c5e27c

Browse files
authored
Merge pull request #1880 from yhkee0404/main
[yhkee0404] WEEK 07 solutions
2 parents 08e5a69 + ca75dba commit 4c5e27c

File tree

5 files changed

+140
-0
lines changed

5 files changed

+140
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import scala.util.control.Breaks._
2+
3+
object Solution {
4+
def lengthOfLongestSubstring(s: String): Int = {
5+
var ans = 0
6+
val visited = Array.fill(128)(false) // S(n) = O(1)
7+
var i = 0
8+
var j = 0
9+
while (j != s.length) {
10+
breakable { // but no continue in scala!
11+
while (j != s.length) { // T(n) = O(n)
12+
if (visited(s(j))) {
13+
break
14+
}
15+
visited(s(j)) = true
16+
j += 1
17+
}
18+
}
19+
ans = Math.max(ans, j - i)
20+
21+
visited(s(i)) = false
22+
i += 1
23+
}
24+
ans
25+
}
26+
}

number-of-islands/yhkee0404.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
var DRCS = [][]int{
2+
{0, -1,},
3+
{0, 1,},
4+
{-1, 0,},
5+
{1, 0,},
6+
}
7+
8+
func numIslands(grid [][]byte) int {
9+
visited := make([][]bool, len(grid)) // S(n) = O(n)
10+
for i, row := range grid {
11+
visited[i] = make([]bool, len(row))
12+
}
13+
ans := 0
14+
stack := [][]int{}
15+
for i, row := range grid {
16+
for j, c := range row {
17+
if visited[i][j] || c == '0' {
18+
continue
19+
}
20+
ans++
21+
visited[i][j] = true
22+
stack = append(stack, []int{i, j})
23+
for len(stack) != 0 { // T(n) = O(n)
24+
u := stack[len(stack) - 1]
25+
stack = stack[: len(stack) - 1]
26+
for _, drc := range DRCS {
27+
x := u[0] + drc[0]
28+
y := u[1] + drc[1]
29+
if x == -1 || x == len(grid) || y == -1 || y == len(row) ||
30+
grid[x][y] == '0' || visited[x][y] {
31+
continue
32+
}
33+
visited[x][y] = true
34+
stack = append(stack, []int{x, y})
35+
}
36+
}
37+
}
38+
}
39+
return ans
40+
}

reverse-linked-list/yhkee0404.dart

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* class ListNode {
4+
* int val;
5+
* ListNode? next;
6+
* ListNode([this.val = 0, this.next]);
7+
* }
8+
*/
9+
class Solution {
10+
ListNode? reverseList(ListNode? head) {
11+
var u = null; // S(n) = O(1)
12+
while (head != null) { // T(n) = O(n)
13+
final temp = head.next;
14+
head.next = u;
15+
u = head;
16+
head = temp;
17+
}
18+
return u;
19+
}
20+
}

set-matrix-zeroes/yhkee0404.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
impl Solution {
2+
pub fn set_zeroes(matrix: &mut Vec<Vec<i32>>) {
3+
let m = matrix.len();
4+
let n = matrix.first().unwrap().len();
5+
let erased_row_0 = (0..n).any(|i| matrix[0][i] == 0);
6+
let erased_col_0 = (0..m).any(|i| matrix[i][0] == 0);
7+
for i in 1..m {
8+
for j in 1..n {
9+
if matrix[i][j] == 0 {
10+
*matrix[i].first_mut().unwrap() = 0;
11+
matrix.first_mut().unwrap()[j] = 0;
12+
}
13+
}
14+
}
15+
for i in 1..m {
16+
if *matrix[i].first().unwrap() != 0 {
17+
continue;
18+
}
19+
for j in 0..n {
20+
matrix[i][j] = 0;
21+
}
22+
}
23+
for j in 1..n {
24+
if matrix.first().unwrap()[j] != 0 {
25+
continue;
26+
}
27+
for i in 0..m {
28+
matrix[i][j] = 0;
29+
}
30+
}
31+
if erased_row_0 {
32+
for j in 0..n {
33+
matrix[0][j] = 0;
34+
}
35+
}
36+
if erased_col_0 {
37+
for i in 0..m {
38+
matrix[i][0] = 0;
39+
}
40+
}
41+
}
42+
}

unique-paths/yhkee0404.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
fun uniquePaths(m: Int, n: Int): Int {
3+
val dp = List(m + 1) {MutableList(n + 1) {0}}
4+
dp[0][1] = 1
5+
for (i in 1..m) {
6+
for (j in 1..n) {
7+
dp[i][j] = dp[i][j - 1] + dp[i - 1][j]
8+
}
9+
}
10+
return dp.last().last()
11+
}
12+
}

0 commit comments

Comments
 (0)