455. Assign Cookies
Link: https://leetcode.com/problems/assign-cookies/
Description
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.
Approachd
To maximize the number of content children, we can first assign the possible smallest cookie to the child with small greed factor, to avoid the waste of the cookies.
- Initialize a variable
cnt
to store the number of content children. - Sort the two arrays in ascending order.
- Loop through
s
:- If
cnt
less than the number of children and the number of the elment ins
larger thang[cnt]
, add 1 tocnt
.
- If
Solution
class Solution {
public int findContentChildren(int[] g, int[] s) {
Arrays.sort(g);
Arrays.sort(s);
int cnt = 0;
for (int i = 0; i < s.length; i++) {
if (cnt < g.length && g[cnt] <= s[i])
cnt++;
}
return cnt;
}
}
376. Wiggle Subsequence
Link: https://leetcode.com/problems/wiggle-subsequence/
Description
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
.
Approach
To find the longest wiggle subsequence, we need to find the situation that contains the most local optimums. To obtain the local optimum, we need to delete the redundant elements (element not at the start and end) in a monotonous subarray.
- Initialize
curDiff
andpreDiff
to store the difference between the next element and current element and the difference between the current element and previous element. Initializecnt
to store the number of the subsequence. - Loop through the sequence, starting from the second element:
- If the two differences have opposite signs, it means the current element is part of the valid wiggle subsequence and update the value of
preDiff
tocurDiff
.
- If the two differences have opposite signs, it means the current element is part of the valid wiggle subsequence and update the value of
Solution
class Solution {
public int wiggleMaxLength(int[] nums) {
int preDiff = 0;
int curDiff = 0;
int cnt = 1;
for (int i = 0; i < nums.length - 1; i++) {
curDiff = nums[i + 1] - nums[i];
if ((preDiff <= 0 && curDiff > 0) || (preDiff >= 0 && curDiff < 0)) {
cnt++;
preDiff = curDiff;
}
}
return cnt;
}
}
53. Maximum Subarray
Link: https://leetcode.com/problems/maximum-subarray/
Description
Given an integer array nums
, find the subarray with the largest sum, and return its sum.
Approach
- Initialize
sum
to store the sum of the current processing subarray,result
to sotre the maximum subrray: - Loop through
num
:- Add the current element to
sum
. - Compare
sum
andresult
, save the larger value toresult
. - If
sum
less than 0, we need to abandon the current sum and setsum
to 0. Because adding a negative to the next value will result in a smaller sum compared to the sum without the current element.
- Add the current element to
Solution
class Solution {
public int maxSubArray(int[] nums) {
int sum = 0;
int result = Integer.MIN_VALUE;
for (int i = 0; i < nums.length; i++) {
sum += nums[i];
result = Math.max(sum, result);
if (sum < 0)
sum = 0;
}
return result;
}
}