Greedy

Greedy for Beginners https://leetcode.com/discuss/general-discussion/669996/greedy-for-beginners-problems-sample-solutions

Add all slope of between the peeks and valleys when the second number is larger than the first one.

T: O(n) S: O(1)

Maintain the farest point it can jump, if i is larger than farest it means it is not possible to jump to i.

T: O(n) S: O(1)

Initiate the maximum position that one could reach starting from the current index i or before: max_pos = nums[0]. Initiate number of steps: at least one, if array has more than 1 cell.

Iterate over number of elements in the input array:

  • If max_step < i, one needs one more jump: jumps += 1. To minimize the number of jumps, choose the longest possible one: max_steps = max_pos.

  • Update max_pos = max(max_pos, i + nums[i]).

"The algorithm is pretty easy to understand. Imagine we take a tour around this circle, the only condition that we can complete this trip is to have more fuel provided than costed in total. That's what the first loop does.

If we do have more fuel provided than costed, that means we can always find a start point around this circle that we could complete the journey with an empty tank. Hence, we check from the beginning of the array, if we can gain more fuel at the current station, we will maintain the start point, else, which means we will burn out of oil before reaching to the next station, we will start over at the next station." @pigwall

T: O(n) S: O(1)

Record all last indicies of each character. Consider the first label, say it's 'a'. The first partition must include the last occurrence of 'a'. However, before the last 'a' there might be a different label makes the partition bigger. For example, in "abccaddbeffe", the minimum first partition is "abccaddb". This gives us the idea for the algorithm: For each letter encountered, process the last occurrence of that letter, extending the current partition [anchor, j] appropriately.

Narrow down the answer by guessing the score and update the list to ONLY keep the ones that have exact x matches with word. In this way, we narrow the candidates after we call master.guess(), and guarantee that secret is in candidates left. Full explanation.

T: O(n^2)

316. Remove Duplicate Letters

1029. Two City Scheduling

1419. Minimum Number of Frogs Croaking

1353. Maximum Number of Events That Can Be Attended

Let the current day be ii. At each day, we perform the following steps:

  • Add to the candidate queue (the min-heap) all meetings whose start day is less than or equal to ii. At this point, the heap contains all meetings available to attend on day ii or earlier.

  • Remove from the heap all meetings whose end day is less than ii, as they can no longer be attended.

  • If the heap is not empty, we attend the meeting with the earliest end time (which is at the top of the heap), increment the count of attended meetings by 11, and remove it from the heap.

Finally, return the total number of meetings attended.

1005. Maximize Sum Of Array After K Negations

Last updated

Was this helpful?