1374. Generate a String With Characters That Have Odd Counts
If n is even, use one a and fill the rest with b; if n is odd, use all a.
Time complexity: O(n), space complexity: O(n).
When writing code before, I never paid too much attention to checking input validity. That is because LeetCode itself constrains the input. But in real interviews, interviewers sometimes care about your assumptions and checks for input, since input in actual production is always unpredictable.
Observation: blue bulbs are always a continuous prefix, which means the blue state can be represented by the highest blue index, and this index is monotonically non-decreasing. So we only need to compare whether it is equal to the maximum index of a turned-on bulb. Each time a bulb is turned on, try to increase this blue_index.
classSolution { public: intnumOfMinutes(int n, int headID, vector<int>& manager, vector<int>& informTime){ unordered_map<int, vector<int>> graphs; for (int i = 0; i < manager.size(); ++i) { graphs[manager[i]].push_back(i); } if (graphs[-1].empty()) return0; int head = graphs[-1][0]; function<int(int)> dfs = [&](int index) -> int { if (graphs[index].empty()) return0; else { int ans = 0; for (int subordinate : graphs[index]) { ans = max(ans, dfs(subordinate)); } return ans + informTime[index]; } }; returndfs(head); } };
1377. Frog Position After T Seconds
Also DFS. One thing to note is that we are looking for the position after exactly t seconds, so it is possible to pass through target, in which case the probability is 0.