Greedy
Greedy for Beginners https://leetcode.com/discuss/general-discussion/669996/greedy-for-beginners-problems-sample-solutions
Last updated
Was this helpful?
Greedy for Beginners https://leetcode.com/discuss/general-discussion/669996/greedy-for-beginners-problems-sample-solutions
Last updated
Was this helpful?
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.
T: O(n^2)
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.