Journey of LeetCode|DAY 27
Preface
This is a new day to start my Greedy Algorithm journey.
Learn something new and keep reviewing what I learnt before.
1. Assign Cookies
LeetCode Link: 455. Assign Cookies
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:
Input: g = [1,2,3], s = [1,1]
Output: 1
Explanation: You have 3 children and 2 cookies. The greed factors of 3 children are 1, 2, 3.
And even though you have 2 cookies, since their size is both 1, you could only make the child whose greed factor is 1 content.
You need to output 1.
Example 2:
Input: g = [1,2], s = [1,2,3]
Output: 2
Explanation: You have 2 children and 3 cookies. The greed factors of 2 children are 1, 2.
You have 3 cookies and their sizes are big enough to gratify all of the children,
You need to output 2.
Constraints:
1 <= g.length <= 3 * 104
0 <= s.length <= 3 * 104
1 <= g[i], s[j] <= 231 - 1
Analysis and Solution
Traverse
LeetCode C++ as followings Traverse
class Solution {
public:
int findContentChildren(vector<int>& g, vector<int>& s) {
sort(g.begin(), g.end());//sort greed factor
sort(s.begin(), s.end());//sort cookies
int index = s.size() - 1; // The index of the cookie array
int result = 0;
for (int i = g.size() - 1; i >= 0; i--) { // iterate over greed factor
if (index >= 0 && s[index] >= g[i]) { // iterate over cookies
result++;//save to result
index--;
}
}
return result;
}
};
2. Wiggle Subsequence
LeetCode Link: 376. Wiggle Subsequence
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:
Input: nums = [1,7,4,9,2,5]
Output: 6
Explanation: The entire sequence is a wiggle sequence with differences (6, -3, 5, -7, 3).
Example 2:
Input: nums = [1,17,5,10,13,15,10,5,16,8]
Output: 7
Explanation: There are several subsequences that achieve this length.
One is [1, 17, 10, 13, 10, 16, 8] with differences (16, -7, 3, -3, 6, -8).
Example 3:
Input: nums = [1,2,3,4,5,6,7,8,9]
Output: 2
Constraints:
1 <= nums.length <= 1000
0 <= nums[i] <= 1000
Follow up: Could you solve this in O(n) time?
Analysis and Solution
Traverse
LeetCode C++ as followings Traverse
class Solution {
public:
int wiggleMaxLength(vector<int>& nums) {
if (nums.size() <= 1) return nums.size();
int curDiff = 0; // current difference of two value
int preDiff = 0; // previous difference of two value
int result = 1; // Record the number of peaks, there is a peak on the far right of the default sequence
for (int i = 0; i < nums.size() - 1; i++) {//traverse nums
curDiff = nums[i + 1] - nums[i];
// peak appears
if ((preDiff <= 0 && curDiff > 0) || (preDiff >= 0 && curDiff < 0)) {
result++;
preDiff = curDiff; // Attention Here, only update the prediff when the Wiggle changes
}
}
return result;
}
};
3. Maximum Subarray
LeetCode Link: 53. Maximum Subarray
Given an integer array nums, find the subarray with the largest sum, and return its sum.
Example 1:
Input: nums = [-2,1,-3,4,-1,2,1,-5,4]
Output: 6
Explanation: The subarray [4,-1,2,1] has the largest sum 6.
Example 2:
Input: nums = [1]
Output: 1
Explanation: The subarray [1] has the largest sum 1.
Example 3:
Input: nums = [5,4,-1,7,8]
Output: 23
Explanation: The subarray [5,4,-1,7,8] has the largest sum 23.
Constraints:
1 <= nums.length <= 105
-104 <= nums[i] <= 104
Follow up: If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle.
Analysis and Solution
Violent Solution
LeetCode C++ as followings Violent Solution
class Solution {
public:
int maxSubArray(vector<int>& nums) {
int result = INT32_MIN;
int count = 0;
for (int i = 0; i < nums.size(); i++) { // set the start position
count = 0;
for (int j = i; j < nums.size(); j++) { // Each time it traverses from the starting position i to find the maximum value
count += nums[j];
result = count > result ? count : result;
}
}
return result;
}
};
OMG, time limit exceeded!
Greedy Solution
LeetCode C++ as followings Greedy Solution
class Solution {
public:
int maxSubArray(vector<int>& nums) {
int result = INT32_MIN;
int count = 0;
for (int i = 0; i < nums.size(); i++) {
count += nums[i];
if (count > result) { // Take the maximum value accumulated in the interval (equivalent to continuously determining the termination position of the largest subsequence)
result = count;
}
if (count <= 0) count = 0; // It is equivalent to resetting the starting position of the largest subsequence, because the sum must be lowered when encountering a negative number
}
return result;
}
};