Skip to content

Commit 801f70e

Browse files
committed
Update problem listings and some notes
1 parent da2ebb1 commit 801f70e

File tree

40 files changed

+2058
-396
lines changed

40 files changed

+2058
-396
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# [1052. Grumpy Bookstore Owner](https://leetcode.com/problems/grumpy-bookstore-owner/)
2+
3+
## Sliding Window
4+
We sum up the customers who are satisfied without the owner being grumpy, and then we use a fixed-size (`minutes`) sliding window to find the maximum number of customers who are satisfied with the owner being grumpy.
5+
6+
Please note that the window keeps track of the number of customers who are satisfied with the owner being grumpy.
7+
8+
```js
9+
10+
index 0, 1, 2, 3, 4, 5, 6, 7
11+
customer: 1, 0, 1, 2, 1, 1, 7, 5
12+
grumpy: 0, 1, 0, 1, 0, 1, 0, 1
13+
base: 1, 1, 1, 7
14+
extra: ^^^^^^^^ ->
15+
maxExtra: 1, 5
16+
```
17+
18+
```kotlin
19+
fun maxSatisfied(customers: IntArray, grumpy: IntArray, minutes: Int): Int {
20+
val n = customers.size
21+
var base = 0
22+
var extra = 0
23+
var maxExtra = 0
24+
for (i in customers.indices) {
25+
if (grumpy[i] == 0) {
26+
base += customers[i]
27+
}
28+
else {
29+
extra += customers[i]
30+
}
31+
32+
if (i >= minutes && grumpy[i - minutes] == 1) {
33+
extra -= customers[i - minutes]
34+
}
35+
if (i >= minutes - 1) {
36+
maxExtra = max(maxExtra, extra)
37+
}
38+
}
39+
return base + maxExtra
40+
}
41+
```
42+
43+
## Pitfalls
44+
- Forgetting checking if `grumpy[i - minutes] == 1` when shrinking `extra`.
45+
- Mixing up base satisfaction vs extra.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# [1145. Binary Tree Coloring Game](https://leetcode.com/problems/binary-tree-coloring-game/)
2+
3+
## Key Insights
4+
Player 1 colors any node `x` first, and player 2 then colors any other uncolored node `y` adjacent to `x`. Once player 1 colors node x, the tree is logically split into 3 disjoint regions:
5+
6+
- Left subtree of `x`
7+
- Right subtree of `x`
8+
- Parent subtree of `x`
9+
10+
```js
11+
[parent of x]
12+
|
13+
x
14+
/ \
15+
[left subtree] [right subtree]
16+
```
17+
18+
Play 2 can choose one of the 3 subtrees to color, and play 1 can only color the rest of the 2 subtrees. Player 2 will take over that chosen subtree, and player 1 will not be able to color any node in that chosen subtree by player 2.
19+
20+
So if player 2 chosen subtree > `n / 2`, then player 1 get < `n / 2` nodes to color, and player 2 wins.
21+
22+
## Postorder Traversal
23+
We just calculate the node of each subtree and identify the 3 choices for player 2.
24+
25+
```kotlin
26+
private var xSum = 0
27+
private var xLeftSum = 0
28+
private var xRightSum = 0
29+
30+
fun btreeGameWinningMove(root: TreeNode?, n: Int, x: Int): Boolean {
31+
val total = sum(root, x)
32+
return (total - xSum) > n / 2 || xLeftSum > n / 2 || xRightSum > n / 2
33+
}
34+
35+
private fun sum(root: TreeNode?, x: Int): Int {
36+
if (root == null) return 0
37+
val left = sum(root.left, x)
38+
val right = sum(root.right, x)
39+
val total = 1 + left + right
40+
if (root.`val` == x) {
41+
xSum = total
42+
xLeftSum = left
43+
xRightSum = right
44+
}
45+
return total
46+
}
47+
```

0 commit comments

Comments
 (0)