Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions Hard/42 Trapping Rain Water.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# 42. Trapping Rain Water

## Intuition

The key insight is that the amount of water trapped at any position depends on the minimum of the maximum heights on both sides. We can use two pointers approach to track the maximum heights from both ends and calculate the trapped water accordingly.

## Approach

1. Use two pointers (left and right) starting from both ends of the array
2. Keep track of the maximum heights seen from both sides (minL and minR)
3. Move the pointer pointing to the smaller height
4. For each position:
- If current height is greater than or equal to the maximum height seen so far, update the maximum
- Otherwise, add the difference between maximum height and current height to the result
5. Continue until left pointer meets right pointer

## Complexity

- Time complexity: O(n)
- Space complexity: O(1)

## Keywords

- Two Pointers

## Code

```go
func trap(height []int) int {
if len(height) < 2 {
return 0
}
left, right, ret, minL, minR := 0, len(height) - 1, 0, math.MinInt, math.MinInt
for left < right {
if height[left] < height[right] {
if height[left] >= minL {
minL = height[left]
} else {
ret += minL - height[left]
}
left += 1
} else {
if height[right] >= minR {
minR = height[right]
} else {
ret += minR - height[right]
}
right -= 1
}
}
return ret
}
```
101 changes: 101 additions & 0 deletions Medium/855 Exam Room.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
# 855. Exam Room

## Intuition

When a new student enters, we need to find the seat that maximizes the minimum distance to any other student. When a student leaves, we need to efficiently remove them from the seating arrangement.

## Approach

1. Use a sorted array to maintain the current seating arrangement
2. For the `Seat()` operation:
- If the room is empty, place the first student at seat 0
- Otherwise, find the maximum gap between existing students
- Consider three cases:
a. Gap at the beginning (seat 0)
b. Gaps between existing students
c. Gap at the end (last seat)
- Insert the new student at the position that creates the maximum minimum distance
3. For the `Leave()` operation:
- Use binary search to find the student's position
- Remove them from the array while maintaining the sorted order

## Complexity

- Time complexity:
- Seat(): O(n)
- Leave(): O(log n) for binary search
- Space complexity: O(n)

## Keywords

- Binary Search

## Code

```go
type ExamRoom struct {
seats int
students []int
}


func Constructor(n int) ExamRoom {
return ExamRoom{
seats: n,
students: make([]int, 0),
}
}


func (e *ExamRoom) Seat() int {
if len(e.students) == 0 {
e.students = append(e.students, 0)
return 0
}

seat, distance, idx := 0, e.students[0], 0

for i := 0; i < len(e.students) - 1; i += 1 {
curDistance := (e.students[i + 1] - e.students[i]) / 2
if curDistance > distance {
distance, seat, idx = curDistance, e.students[i] + curDistance, i + 1
}
}

if e.seats - 1 - e.students[len(e.students) - 1] > distance {
distance = e.seats - 1 - e.students[len(e.students) - 1]
seat, idx = e.seats - 1, len(e.students)
}

e.students = append(e.students, 0)
copy(e.students[idx + 1:], e.students[idx:])
e.students[idx] = seat
return seat
}


func (e *ExamRoom) Leave(p int) {
l, r := 0, len(e.students) - 1
for l <= r {
m := (l + r) >> 1
if e.students[m] == p {
e.students = append(e.students[:m], e.students[m + 1:]...)
return
}

if e.students[m] < p {
l = m + 1
} else {
r = m - 1
}
}
}


/**
* Your ExamRoom object will be instantiated and called as such:
* obj := Constructor(n);
* param_1 := obj.Seat();
* obj.Leave(p);
*/
```
65 changes: 65 additions & 0 deletions Medium/875 Koko Eating Bananas.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# 875. Koko Eating Bananas

## Intuition

We can use binary search to find the optimal speed since:

1. If Koko can finish bananas at speed k, she can definitely finish them at any speed > k
2. If Koko cannot finish bananas at speed k, she definitely cannot finish them at any speed < k

## Approach

1. Use binary search to find the minimum eating speed
2. The search range is from 1 to the maximum pile size
3. For each speed k:
- Check if Koko can finish all piles within h hours
- If pile size ≤ k, it takes 1 hour
- If pile size > k, it takes (pile size / k) hours, rounded up
4. Return the minimum speed that allows Koko to finish all bananas

## Complexity

- Time complexity: O(n * log M)
- Space complexity: O(1)

## Keywords

- Binary Search

## Code

```go
func check(mid, h int, piles []int) bool {
for _, pile := range piles {
if pile <= mid {
h -= 1
} else {
if pile % mid != 0 {
h -= 1
}
h -= (pile / mid)
}
if h < 0 {
return false
}
}
return true
}

func minEatingSpeed(piles []int, h int) int {
maxPile := 0
for _, pile := range piles {
maxPile = max(maxPile, pile)
}
l, r := 1, maxPile
for l <= r {
mid := (l + r) / 2
if check(mid, h, piles) {
r = mid - 1
} else {
l = mid + 1
}
}
return l
}
```
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
| 24 | Swap Nodes in Pairs | [go](/Medium/24%20Swap%20Nodes%20in%20Pairs.md) | M |
| 25 | Reverse Nodes in k-Group | [go](/Hard/25%20Reverse%20Nodes%20in%20k-Group.md) | H |
| 38 | Count and Say | [go](/Medium/38%20Count%20and%20Say.md) | M |
| 42 | Trapping Rain Water | [go](/Hard/42%20Trapping%20Rain%20Water.md) | H |
| 44 | Wildcard Matching | [go](/Hard/44%20Wildcard%20Matching.md) | H |
| 45 | Jump Game II | [go](/Medium/45%20Jump%20Game%20II.md) | M |
| 46 | Permutations | [go](/Medium/46%20Permutations.md) | M |
Expand All @@ -35,11 +36,13 @@
| 685 | Redundant Connection II | [go](/Hard/685%20Redundant%20Connection%20II.md) | H |
| 752 | Open the Lock | [go](/Medium/752%20Open%20the%20Lock.md) | M |
| 757 | Set Intersection Size At Least Two | [go](/Hard/757%20Set%20Intersection%20Size%20At%20Least%20Two.md) | H |
| 815 | Bus Routes | [go](/Hard/815%20Bus%20Routes.md) | H |
| 847 | Shortest Path Visiting All Nodes | [go](/Hard/847%20Shortest%20Path%20Visiting%20All%20Nodes.md) | H |
| 765 | Couples Holding Hands | [go](/Hard/765%20Couples%20Holding%20Hands.md) | H |
| 815 | Bus Routes | [go](/Hard/815%20Bus%20Routes.md) | H |
| 834 | Sum of Distances in Tree | [go](/Hard/834%20Sum%20of%20Distances%20in%20Tree.md) | H |
| 840 | Magic Squares In Grid | [go](/Medium/840%20Magic%20Squares%20In%20Grid.md) | M |
| 847 | Shortest Path Visiting All Nodes | [go](/Hard/847%20Shortest%20Path%20Visiting%20All%20Nodes.md) | H |
| 855 | Exam Room | [go](/Medium/855%20Exam%20Room.md) | M |
| 875 | Koko Eating Bananas | [go](/Medium/875%20Koko%20Eating%20Bananas.md) | M |
| 881 | Boats to Save People | [go](/Medium/881%20Boats%20to%20Save%20People.md) | M |
| 912 | Sort an Array | [go](/Medium/912%20Sort%20an%20Array.md) | M |
| 948 | Bag of Tokens | [go](/Medium/948%20Bag%20Of%20Tokens.md) | M |
Expand Down
Binary file added Week11/42 AC.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
53 changes: 53 additions & 0 deletions Week11/42 Trapping Rain Water.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# 42. Trapping Rain Water

## Intuition

The key insight is that the amount of water trapped at any position depends on the minimum of the maximum heights on both sides. We can use two pointers approach to track the maximum heights from both ends and calculate the trapped water accordingly.

## Approach

1. Use two pointers (left and right) starting from both ends of the array
2. Keep track of the maximum heights seen from both sides (minL and minR)
3. Move the pointer pointing to the smaller height
4. For each position:
- If current height is greater than or equal to the maximum height seen so far, update the maximum
- Otherwise, add the difference between maximum height and current height to the result
5. Continue until left pointer meets right pointer

## Complexity

- Time complexity: O(n)
- Space complexity: O(1)

## Keywords

- Two Pointers

## Code

```go
func trap(height []int) int {
if len(height) < 2 {
return 0
}
left, right, ret, minL, minR := 0, len(height) - 1, 0, math.MinInt, math.MinInt
for left < right {
if height[left] < height[right] {
if height[left] >= minL {
minL = height[left]
} else {
ret += minL - height[left]
}
left += 1
} else {
if height[right] >= minR {
minR = height[right]
} else {
ret += minR - height[right]
}
right -= 1
}
}
return ret
}
```
Binary file added Week11/855 AC.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading