LeetCode //C - 918. Maximum Sum Circular Subarray

这篇具有很好参考价值的文章主要介绍了LeetCode //C - 918. Maximum Sum Circular Subarray。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

918. Maximum Sum Circular Subarray

Given a circular integer array nums of length n, return the maximum possible sum of a non-empty subarray of nums.

A circular array means the end of the array connects to the beginning of the array. Formally, the next element of nums[i] is nums[(i + 1) % n] and the previous element of nums[i] is nums[(i - 1 + n) % n].

A subarray may only include each element of the fixed buffer nums at most once. Formally, for a subarray nums[i], nums[i + 1], …, nums[j], there does not exist i <= k1, k2 <= j with k1 % n == k2 % n.
 

Example 1:

Input: nums = [1,-2,3,-2]
Output: 3
Explanation: Subarray [3] has maximum sum 3.

Example 2:

Input: nums = [5,-3,5]
Output: 10
Explanation: Subarray [5,5] has maximum sum 5 + 5 = 10.

Example 3:

Input: nums = [-3,-2,-3]
Output: -2
Explanation: Subarray [-2] has maximum sum -2.

Constraints:
  • n == nums.length
  • 1 < = n < = 3 ∗ 1 0 4 1 <= n <= 3 * 10^4 1<=n<=3104
  • − 3 ∗ 1 0 4 < = n u m s [ i ] < = 3 ∗ 1 0 4 -3 * 10^4 <= nums[i] <= 3 * 10^4 3104<=nums[i]<=3104

From: LeetCode
Link: 918. Maximum Sum Circular Subarray


Solution:

Ideas:

There are two possible scenarios for the maximum sum subarray in a circular array:

  1. The maximum sum subarray is similar to a regular array, i.e., it does not wrap around.
  2. The maximum sum subarray wraps around the end to the beginning of the array.

For the first scenario, we can use Kadane’s algorithm directly. But for the second scenario, we need a different approach.

If the maximum sum subarray wraps around, then there’s a continuous subarray at the opposite part of the array that has the minimum sum. Think of it as “taking away” the minimum sum part from the total to get the maximum circular sum.

Given this, we can use a similar approach to Kadane’s algorithm to find both:文章来源地址https://www.toymoban.com/news/detail-723496.html

  1. The maximum subarray sum (for the first scenario).
  2. The minimum subarray sum (to help with the second scenario).
Code:
int max(int a, int b) {
    return a > b ? a : b;
}

int min(int a, int b) {
    return a < b ? a : b;
}

int maxSubarraySumCircular(int* nums, int numsSize) {
    if (!nums || numsSize == 0) return 0;

    int total = 0, maxSum = -30000, curMax = 0, minSum = 30000, curMin = 0;

    for (int i = 0; i < numsSize; i++) {
        curMax = max(curMax + nums[i], nums[i]);
        maxSum = max(maxSum, curMax);
        
        curMin = min(curMin + nums[i], nums[i]);
        minSum = min(minSum, curMin);
        
        total += nums[i];
    }

    if (maxSum > 0) {
        return max(maxSum, total - minSum);
    } else {
        return maxSum;
    }
}

到了这里,关于LeetCode //C - 918. Maximum Sum Circular Subarray的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包赞助服务器费用

相关文章

  • LeetCode //C - 2130. Maximum Twin Sum of a Linked List

    LeetCode //C - 2130. Maximum Twin Sum of a Linked List

    In a linked list of size n, where n is even, the i t h i^{th} i t h node (0-indexed) of the linked list is known as the twin of the ( n − 1 − i ) t h (n-1-i)^{th} ( n − 1 − i ) t h node, if 0 = i = (n / 2) - 1. For example, if n = 4, then node 0 is the twin of node 3, and node 1 is the twin of node 2. These are the only nodes with twins for n = 4. Th

    2024年01月17日
    浏览(44)
  • Leetcode 3007. Maximum Number That Sum of the Prices Is Less Than or Equal to K

    Leetcode 3007. Maximum Number That Sum of the Prices Is Less Than or Equal to K 1. 解题思路 2. 代码实现 题目链接:3007. Maximum Number That Sum of the Prices Is Less Than or Equal to K 这一题我的思路上就是一个二分的思路,先确定一个上下界,然后不断通过二分来找到最大的price不超过k的值。 因此,剩下的

    2024年01月20日
    浏览(10)
  • Leetcode978. Longest Turbulent Subarray

    给定一个数组,找到最长的turbulent子数组的长度 turbulent子数组就是一增一减交替 其实这题也很简单,就是直接找子数组,不满足条件了,就重新开始算 时间复杂度 O ( n ) O(n) O ( n )

    2024年02月08日
    浏览(10)
  • Leetcode | Kadane Algo | 53. 918.

    53. Maximum Subarray 如果cur_sum大于零,可以晋级到下一个元素,因为正数只会让之后的和更大。如果cursum小于零,那就把他reset成0,让下一个元素从头开始。在此过程中一直记录global max 918. Maximum Sum Circular Subarray 两种求和方式: 按照正常的区间,不circular,就是53的思路即可。

    2024年02月14日
    浏览(9)
  • LeetCode 2490. Circular Sentence【字符串】简单

    本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,

    2024年02月12日
    浏览(6)
  • LeetCode 2090. K Radius Subarray Averages【前缀和,滑动窗口,数组】中等

    LeetCode 2090. K Radius Subarray Averages【前缀和,滑动窗口,数组】中等

    本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,

    2024年02月11日
    浏览(13)
  • LeetCode_动态规划_中等_918.环形子数组的最大和

    给定一个长度为 n 的环形整数数组 nums ,返回 nums 的非空子数组的最大可能和。 环形数组意味着数组的末端将会与开头相连呈环状。形式上, nums[i] 的下一个元素是 nums[(i + 1) % n] , nums[i] 的前一个元素是 nums[(i - 1 + n) % n] 。 子数组最多只能包含固定缓冲区 nums 中的每个元素

    2024年02月09日
    浏览(8)
  • Leetcode 3115. Maximum Prime Difference

    Leetcode 3115. Maximum Prime Difference 1. 解题思路 2. 代码实现 题目链接:3115. Maximum Prime Difference 这一题思路上非常的直接,就是找到数组当中所有的质数的位置,然后去首尾两个计算位置的距离即可。 因此,问题也就变成了一个判断任意一个数是不是素数的问题,这个就老生常谈

    2024年04月16日
    浏览(6)
  • 1143. Longest Common Subsequence 1035. Uncrossed Lines 53. Maximum Subarray

    1143. Longest Common Subsequence 1035. Uncrossed Lines 53. Maximum Subarray

    1143. Longest Common Subsequence Given two strings  text1  and  text2 , return  the length of their longest  common subsequence .  If there is no  common subsequence , return  0 . A  subsequence  of a string is a new string generated from the original string with some characters (can be none) deleted without changing the relative order of the remai

    2024年02月03日
    浏览(12)
  • LeetCode454. 4Sum II

    Given four integer arrays nums1, nums2, nums3, and nums4 all of length n, return the number of tuples (i, j, k, l) such that: 0 = i, j, k, l n nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0 Example 1: Input: nums1 = [1,2], nums2 = [-2,-1], nums3 = [-1,2], nums4 = [0,2] Output: 2 Explanation: The two tuples are: (0, 0, 0, 1) - nums1[0] + nums2[0] + nums3[0] +

    2024年02月06日
    浏览(10)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包