Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Leetcode 918. Maximum Sum Circular Subarray #185

Open
Woodyiiiiiii opened this issue Jan 18, 2023 · 0 comments
Open

Leetcode 918. Maximum Sum Circular Subarray #185

Woodyiiiiiii opened this issue Jan 18, 2023 · 0 comments

Comments

@Woodyiiiiiii
Copy link
Owner

Woodyiiiiiii commented Jan 18, 2023

这道题我初始想法是使用滑动窗口,但显然,没有限制条件和移动,没法写出来。

那么这题其实要有点No.53 Maximum Subarray的基础,那道题最简便的方法是使用Kadane's Algorithm求解数组中最大的和的子数组。

这题稍微绕了下弯,因为题目要求是circular array,所以最大的和的子数组有两种模式

  1. 连续子数组
  2. 首尾两端不相交的子数组

第一种直接用53题的思路可以求出来,第二种可以用逆向思维,求出最小数组的和然后用总和相减即可。注意如果第二种情况子数组就是数组本身,则说明数组所有元素都是non-positive,那么就不用考虑第二种情况。

class Solution {
    public int maxSubarraySumCircular(int[] nums) {
        int preMin = 0, preMax = 0, min = Integer.MAX_VALUE, max = Integer.MIN_VALUE;
        // use Kadane's algorithm to find the max subarray sum and min subarray sum
        for (int num : nums) {
            preMin = Math.min(preMin + num, num);
            min = Math.min(min, preMin);
            preMax = Math.max(preMax + num, num);
            max = Math.max(max, preMax);
        }
        int sum = Arrays.stream(nums).sum();
        return sum == min ? max : Math.max(max, sum - min);
    }
}

**总结kadane算法: **

  • kadane算法一般用于求子数组最大值和/乘法的情况
  • 一般结构是从左到右遍历,稍微有点贪心和DP的思想,每次用max和min判断目前循环的最值

可以用到kadane's algorithm算法的类似题目:


# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant