Greedy
[toc]
Greedy Algorithm
The greedy algorithm generally consists of the following four steps:
- Divide the problem into several subproblems: Break down the main problem into smaller, manageable subproblems.
- Find a suitable greedy strategy: Identify an appropriate greedy strategy that will help in making the optimal choice at each step.
- Solve each subproblem optimally: Apply the greedy strategy to solve each subproblem and find the local optimum.
- Stack the local optima to form the global optimum: Combine the local optimum solutions of the subproblems to build the global optimum solution.
455. Assign CookiesSolved
Easy
Assume you are an awesome parent and want to give your children some cookies. But, you should give each child at most one cookie.
Each child i
has a greed factor g[i]
, which is the minimum size of a cookie that the child will be content with; and each cookie j
has a size s[j]
. If s[j] >= g[i]
, we can assign the cookie j
to the child i
, and the child i
will be content. Your goal is to maximize the number of your content children and output the maximum number.
Example 1:
1 | Input: g = [1,2,3], s = [1,1] |
1 | class Solution { |
376. Wiggle Subsequence
😭😭Medium
A wiggle sequence is a sequence where the differences between successive numbers strictly alternate between positive and negative. The first difference (if one exists) may be either positive or negative. A sequence with one element and a sequence with two non-equal elements are trivially wiggle sequences.
- For example,
[1, 7, 4, 9, 2, 5]
is a wiggle sequence because the differences(6, -3, 5, -7, 3)
alternate between positive and negative. - In contrast,
[1, 4, 7, 2, 5]
and[1, 7, 4, 5, 5]
are not wiggle sequences. The first is not because its first two differences are positive, and the second is not because its last difference is zero.
A subsequence is obtained by deleting some elements (possibly zero) from the original sequence, leaving the remaining elements in their original order.
Given an integer array nums
, return the length of the longest wiggle subsequence of nums
.
Example 1:
1 | Input: nums = [1,7,4,9,2,5] |
1 | class Solution { |
53. Maximum Subarray
😭😍Medium
Given an integer array nums
, find the
subarray
with the largest sum, and return its sum.
Example 1:
1 | Input: nums = [-2,1,-3,4,-1,2,1,-5,4] |
1 | class Solution { |
122. Best Time to Buy and Sell Stock II DAY30
😭😄😍Medium
You are given an integer array prices
where prices[i]
is the price of a given stock on the ith
day.
On each day, you may decide to buy and/or sell the stock. You can only hold at most one share of the stock at any time. However, you can buy it then immediately sell it on the same day.
Find and return the maximum profit you can achieve.
Example 1:
1 | Input: prices = [7,1,5,3,6,4] |
1 | // 贪心思路 |
55. Jump Game
😭😭覆盖范围
Medium
You are given an integer array nums
. You are initially positioned at the array’s first index, and each element in the array represents your maximum jump length at that position.
Return true
if you can reach the last index, or false
otherwise.
Example 1:
1 | Input: nums = [2,3,1,1,4] |
1 | class Solution { |
The algorithm works by maintaining and updating a coverRange
that tracks the maximum index that can be reached starting from any index in the array. By iterating through the array and updating coverRange
accordingly, it checks if the end of the array can be reached from the beginning. This approach efficiently determines if jumping through the array is feasible to reach the end.
45. Jump Game II
😭😭 覆盖范围
Medium
You are given a 0-indexed array of integers nums
of length n
. You are initially positioned at nums[0]
.
Each element nums[i]
represents the maximum length of a forward jump from index i
. In other words, if you are at nums[i]
, you can jump to any nums[i + j]
where:
0 <= j <= nums[i]
andi + j < n
Return the minimum number of jumps to reach nums[n - 1]
. The test cases are generated such that you can reach nums[n - 1]
.
Example 1:
1 | Input: nums = [2,3,1,1,4] |
1 | class Solution { |
这里需要统计两个覆盖范围,当前这一步的最大覆盖和下一步最大覆盖。
图中覆盖范围的意义在于,只要红色的区域,最多两步一定可以到!(不用管具体怎么跳,反正一定可以跳到)
https://programmercarl.com/0045.%E8%B7%B3%E8%B7%83%E6%B8%B8%E6%88%8FII.html
1005. Maximize Sum Of Array After K Negations
Easy
Given an integer array nums
and an integer k
, modify the array in the following way:
- choose an index
i
and replacenums[i]
with-nums[i]
.
You should apply this process exactly k
times. You may choose the same index i
multiple times.
Return the largest possible sum of the array after modifying it in this way.
Example 1:
1 | Input: nums = [4,2,3], k = 1 |
1 | class Solution { |
Sort the array by absolute value in descending order.
Iterate through the array from the beginning and convert negative numbers to positive, decreasing
K
each time.If
K
is still greater than 0, repeatedly change the smallest element’s sign untilK
is exhausted.Calculate the sum of the array.