Skip to content

Commit 5bc8cac

Browse files
authored
Merge pull request #1912 from yhkee0404/main
[yhkee0404] WEEK 09 solutions
2 parents 0ed76b3 + 0b49d70 commit 5bc8cac

File tree

5 files changed

+146
-0
lines changed

5 files changed

+146
-0
lines changed

linked-list-cycle/yhkee0404.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* type ListNode struct {
4+
* Val int
5+
* Next *ListNode
6+
* }
7+
*/
8+
func hasCycle(head *ListNode) bool {
9+
u := head
10+
v := head
11+
for u != nil && v != nil { // T(n) = O(n), S(n) = O(1)
12+
u = u.Next
13+
if (u == nil) {
14+
break
15+
}
16+
u = u.Next // https://hyp.is/gLsBIOHBEe6E_9M9T5MJNw/yhkee0404.github.io/posts/algorithms/leetcode/linked-list-cycle/
17+
v = v.Next
18+
if u == v {
19+
return true
20+
}
21+
}
22+
return false
23+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
impl Solution {
2+
pub fn max_product(nums: Vec<i32>) -> i32 {
3+
let mut neg_abs_min = 1;
4+
let mut total = 1;
5+
let mut ans = *nums.first()
6+
.unwrap();
7+
for num in nums {
8+
if num == 0 {
9+
neg_abs_min = 1;
10+
total = 1;
11+
ans = ans.max(0);
12+
continue
13+
}
14+
total *= num;
15+
ans = ans.max(total);
16+
if total < 0 {
17+
if neg_abs_min == 1 {
18+
neg_abs_min = total;
19+
} else {
20+
ans = ans.max(total / neg_abs_min)
21+
}
22+
}
23+
}
24+
ans
25+
}
26+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class Solution {
2+
fun minWindow(s: String, t: String): String { // T(m, n) = S(m, n) = O(m + n)
3+
val counterT = (
4+
t
5+
.groupingBy {it} // O(n)
6+
.eachCount()
7+
)
8+
var l = -1
9+
var r = -1
10+
val counterS = mutableMapOf<Char, Int>()
11+
var i = -1
12+
(
13+
s
14+
.withIndex()
15+
.forEach { // O(m)
16+
counterS.compute(it.value) {k, v -> if (v == null) 1 else v + 1}
17+
while (i != it.index && counterS[s[i + 1]]!! > counterT.getOrDefault(s[i + 1], 0)) { // O(m)
18+
counterS.compute(s[++i]) {k, v -> v!! - 1}
19+
}
20+
if (
21+
counterT
22+
.all {(k, v) -> counterS.getOrDefault(k, 0) >= v} // O(n)
23+
) {
24+
if (r == -1 || r - l > it.index - i) {
25+
l = i
26+
r = it.index
27+
}
28+
}
29+
}
30+
)
31+
return s.substring(l + 1, r + 1) // O(m)
32+
}
33+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
object Solution {
2+
val _DRCS = Array(
3+
Array(-1, 0),
4+
Array(0, -1),
5+
Array(0, 1),
6+
Array(1, 0),
7+
)
8+
def pacificAtlantic(heights: Array[Array[Int]]): List[List[Int]] = {
9+
val visited = Array.fill[Array[Int]](heights.length)(
10+
Array.fill[Int](heights.head.length)(0)
11+
)
12+
for (i <- heights.indices) {
13+
dfs(heights, visited, i, 0, 1)
14+
dfs(heights, visited, i, heights.head.length - 1, 2)
15+
}
16+
for (i <- heights.head.indices) {
17+
dfs(heights, visited, 0, i, 1)
18+
dfs(heights, visited, heights.length - 1, i, 2)
19+
}
20+
(
21+
for {
22+
r <- visited.indices
23+
c <- visited.head.indices
24+
if visited(r)(c) == 3
25+
} yield List(r, c)
26+
).toList
27+
}
28+
def dfs(heights: Array[Array[Int]], visited: Array[Array[Int]], r: Int, c: Int, v: Int): Unit = {
29+
visited(r)(c) |= v
30+
_DRCS.map { case Array(dr, dc) => (r + dr, c + dc) }
31+
.filter { case (nr, nc) => nr != -1 && nr != heights.length && nc != -1 && nc != heights.head.length }
32+
.filter { case (nr, nc) => (visited(nr)(nc) & v) == 0 && heights(r)(c) <= heights(nr)(nc) }
33+
.foreach { case (nr, nc) =>
34+
dfs(heights, visited, nr, nc, v)
35+
}
36+
}
37+
}

sum-of-two-integers/yhkee0404.dart

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
int getSum(int a, int b) {
3+
int ans = 0;
4+
// for (int i = 0, carry = 0, ai, bi, u = 1; i != 64 && (carry | a | b) != 0; i++, a >>>= 1, b >>>= 1, u <<= 1) { // significand 53 bits but u has only 1 bit
5+
for (int i = 0, carry = 0, ai, bi, u = 1; i != 32 && (carry | a | b) != 0; i++, a >>= 1, b >>= 1, u <<= 1) {
6+
ai = a & 1;
7+
bi = b & 1;
8+
if ((ai & bi) == 1) {
9+
if (carry == 1) {
10+
ans |= u;
11+
}
12+
carry = 1;
13+
} else if ((ai ^ bi) == 1) {
14+
if (carry == 0) {
15+
ans |= u;
16+
}
17+
} else {
18+
if (carry == 1) {
19+
ans |= u;
20+
}
21+
carry = 0;
22+
}
23+
}
24+
// return ans;
25+
return (ans & 0x80000000) != 0 ? ans | ~ 0xFFFFFFFF : ans;
26+
}
27+
}

0 commit comments

Comments
 (0)