LeetCode | 455. Assign Cookies, 376. Wiggle Subsequence, 53. Maximum Subarray

文章讨论了三个LeetCode问题:分配饼干给满足条件的孩子、寻找最长摆动子序列以及求最大子数组和。介绍了如何用贪心策略解决分配饼干问题,以及如何找到具有最大局部最优解的摆动子序列和最大连续正负差的子数组。
摘要由CSDN通过智能技术生成

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 in s larger than g[cnt], add 1 to cnt.

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 and preDiff to store the difference between the next element and current element and the difference between the current element and previous element. Initialize cnt 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 to curDiff.

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 and result, save the larger value to result.
    • If sum less than 0, we need to abandon the current sum and set sum to 0. Because adding a negative to the next value will result in a smaller sum compared to the sum without the current element.

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;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值