Ranking: 429 / 13808 ðŸ˜‹

# Q1. 1486. XOR Operation in an Array

Given an integer n and an integer start.

Define an array nums where nums[i] = start + 2*i (0-indexed) and n == nums.length.

Return the bitwise XOR of all elements of nums.

Very straight forward.

1 | class Solution: |

# Q2. 1487. Making File Names Unique

Too long. See description in link

Code is easy. It might takes a while to understand the question.

1 | class Solution: |

# Q3 1488. Avoid Flood in The City

Your country has an infinite number of lakes. Initially, all the lakes are empty, but when it rains over the nth lake, the nth lake becomes full of water. If it rains over a lake which is full of water, there will be a flood. Your goal is to avoid the flood in any lake.

Your country has an infinite number of lakes. Initially, all the lakes are empty, but when it rains over the nth lake, the nth lake becomes full of water. If it rains over a lake which is full of water, there will be a flood. Your goal is to avoid the flood in any lake.Given an integer array rains where: d

- rains[i] > 0 means there will be rains over the rains[i] lake.
- rains[i] == 0 means there are no rains this day and you can choose one lake this day and dry it.
Return an array ans where:

- ans.length == rains.length .
- ans[i] == -1 if rains[i] > 0.
- ans[i] is the lake you choose to dry in the ith day if rains[i] == 0.
If there are multiple valid answers return any of them. If it is impossible to avoid flood return an empty array.

Notice that if you chose to dry a full lake, it becomes empty, but if you chose to dry an empty lake, nothing changes. (see example 4)

## Note

- Failed 3 times to cover all corner cases.

## Idea

- When there are no rains, append that day to a array. By nature, that array is sorted.
- Using a dictionary to track which lack is full and when it was filled. {lack: day_it_get_filled}
- When we found a lack is going to be flood, use binary search to find the earliest day we can use to dry that lake.

1 | class Solution: |

# Q4. 1489. Find Critical and Pseudo-Critical Edges in Minimum Spanning Tree

Too long. See description in link

Such a nice question. I learnt a lot from this one.

## Intuition

Iterator each edge

```
1. If the cost of MST without this edge increased, this is a critical edge .
2. elif the cost of MST without this edge does not change, this is a Pseudo-Critical edge
```

## Minimal Spanning Tree

### Kruskalâ€™s algorithm

1 | class Kruskal: |

### Primâ€™s algorithm

1 | class Prim: |

### Solution

1 | # A decent implementation based on Kruskal's algorithm |

1 | from heapq import heappush, heappop |

# Summary

- OMG! I want to be as strong as Aoxiang Cui
- Kruskalâ€™s algorithm is easier to implement during contest/interview
- Primâ€™s algorithm can be implemented with
`heapq`

in python. Red-Black tree is not needed.