Skip to content

Commit be61df3

Browse files
adding in my written notes for 2045
1 parent 43ea190 commit be61df3

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

problems/2045/jeremymanning.md

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,22 @@
3333
- Then we just need to implement this modified version of Dijkstra's algorithm (i.e., breadth first search) to find the second minimum time needed to get from vertex 1 to $n$.
3434
- One potential edge case: what if there is only 1 path from vertex 1 to $n$? Then I think to get the second minimum time, we would need to double back along the trip between the nearest vertices (accounting for signal status)-- e.g., we'd need to add an extra loop (for some adjacent vertices `i` and `j`): `... --> i --> j --> i --> j --> ...`.
3535

36+
### Other notes
37+
- For each branch of the path we take, we need to track how long it took to get there and where we are "now"
38+
- Given the current time, we can compute the current signal status and update the next travel time accordingly
39+
- To store both the "minimum" and "second minimum" times, we can create a matrix of tuples: `min_times[i][j][0]` has the minimum time path from `i` to `j`, and `min_times[i][j][1]` has the second minimum time from `i` to `j`:
40+
- If we find a better time than `min_times[i][j][0]`:
41+
- `min_times[i][j][1] = `min_times[i][j][0]`
42+
- `min_times[i][j][0] = new_best_time`
43+
- We can use a list of lists to represent the graph:
44+
- `graph[i]` stores the edges originating at `i`
45+
- Initialize all edges to empty lists
46+
- The travel time computation is organized around green/red cycles:
47+
- Each cycle takes `cycle = 2 * change` minutes
48+
- The signals start as green; each `2 * change` minutes they return to green
49+
- If (after rounding down to the nearest multiple of `change`) the current time (`current_time`) is an *odd* multiple of `change` (i.e., the lights are currently red; `(current_time // change) % 2 == 1`), we need to wait until the lights turn green, which will take `wait_time = cycle - (current_time % cycle)` minutes. The new time is `wait_time + time + current_time`.
50+
- If (after rounding down to the nearest multiple of `change`) the current time (`current_time`) is an *even* multiple of `change` (i.e., the lights are currently green; `(current_time // change) % 2 == 0`), then we can leave right away (`wait_time` is 0), so the new time is `time + current_time`.
51+
3652
## Attempted solution(s)
3753
```python
3854
class Solution:
@@ -66,11 +82,11 @@ class Solution:
6682
for neighbor in graph[vertex]:
6783
next_time = computeTravelTime(curr_time)
6884

69-
if next_time < min_times[neighbor][0]:
85+
if next_time < min_times[neighbor][0]: # demote minimum time to second minimum time and update minimum time
7086
min_times[neighbor][1] = min_times[neighbor][0]
7187
min_times[neighbor][0] = next_time
7288
queue.append((next_time, neighbor))
73-
elif min_times[neighbor][0] < next_time < min_times[neighbor][1]:
89+
elif min_times[neighbor][0] < next_time < min_times[neighbor][1]: # update second minimum time
7490
min_times[neighbor][1] = next_time
7591
queue.append((next_time, neighbor))
7692

0 commit comments

Comments
 (0)