首页 > 其他分享 >LeetCode 918. Maximum Sum Circular Subarray

LeetCode 918. Maximum Sum Circular Subarray

时间:2024-05-17 10:41:15浏览次数:19  
标签:nums int max sum globalMax Maximum 918 Subarray Sum

原题链接在这里:https://leetcode.com/problems/maximum-sum-circular-subarray/description/

题目:

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 <= k1k2 <= 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 * 104
  • -3 * 104 <= nums[i] <= 3 * 104

题解:

To find the max sum of circular subarray, there are two contitions.

First is max sum occurs in the middle of array.

Second is max sum occurs with tail + head.

For the second part, max(prefix + suffix) = max(sum - middle part) = sum - min(middle part).

Note: when all the nums are negative, sum - min(middle part) = 0, but we can't accept empty subset. Thus when globalMax <= 0, just take the globalMax.

Time Complexity: O(n). n = nums.length.

Space: O(1).

AC Java:

 1 class Solution {
 2     public int maxSubarraySumCircular(int[] nums) {
 3         if(nums == null || nums.length == 0){
 4             return 0;
 5         }
 6 
 7         int sum = 0;
 8         int n = nums.length;
 9         int localMax = 0;
10         int globalMax = nums[0];
11         int localMin = 0;
12         int globalMin = nums[0];
13         for(int num : nums){
14             localMax = Math.max(localMax + num, num);
15             globalMax = Math.max(globalMax, localMax);
16             localMin = Math.min(localMin + num, num);
17             globalMin = Math.min(globalMin, localMin);
18             sum += num;
19         }
20 
21         return globalMax > 0 ? Math.max(globalMax, sum - globalMin) : globalMax;
22     }
23 }

类似Maximum Subarray.

标签:nums,int,max,sum,globalMax,Maximum,918,Subarray,Sum
From: https://www.cnblogs.com/Dylan-Java-NYC/p/18197444

相关文章

  • Summer '24:不容错过的Salesforce Flow 10大新功能!
    Flow是整个Salesforce平台自动化的未来。自从WorkflowRules和ProcessBuilders被淘汰,Salesforce将重点放在了Flow上,一直在将大量资源用于开发Flow创新,本次Summer'24中Flow也有不少亮眼的更新!01FlowCreationWizard和Flow类型创建Flow与以前有所不同。你看到的第一步只是让......
  • Minimizing the Sum
    题目链接https://codeforces.com/problemset/problem/1969/C分析分析样例就可以知道这不是一道贪心题,所以我们可以采用dp寻常的dp一般都是从左向右,但是这样就会导致变成的值可能在左边,比如32221所以我们换一种dp方式,注意到k的范围很小,长度为n的序列在n-1次操作就可以变成......
  • CF1965D Missing Subarray Sum题解
    题目链接点击打开链接题目解法感觉一点都不好想\fn因为最后的序列\(a\)是回文的,所以只有以中心点对称的子段和出现次数为奇数,其他都为偶数考虑有没有什么知道所有子段和的做法,可以方便推出缺失一个的答案令\(g_{l,r}\)为\(l\)到\(r\)的子段和知道\(g_{1,n}\)删......
  • 64 - Minimum Path Sum 最小路径和
    64-MinimumPathSum最小路径和问题描述Givenamxngridfilledwithnon-negativenumbers,findapathfromtoplefttobottomright,whichminimizesthesumofallnumbersalongitspath.Note:Youcanonlymoveeitherdownorrightatanypointinti......
  • AtCoder Beginner Contest 351 E - Jump Distance Sum
    题目链接Hint1:只能斜着走,容易想到黑白棋盘,\((x+y)\%2==0\)位于一个团,\((x+y)\%2==1\)位于另一个团,分别求和。Hint2:\(dist=max(|x1-x2|,|y1-y2|)\),这是切比雪夫距离,将坐标系倾斜\(45^{\circ}\),改为曼哈顿距离\(dist=|x1-x2|+|y1-y2|\),即\(X=x+y,Y=x-y\),这样就可以将横纵坐标......
  • skipped: maximum number of running instances reached (1)
    Python的 apscheduler今天出现skipped:maximumnumberofrunninginstancesreached(1)问题产生的原因:设置了大量的任务,而APScheduler无法同时处理所有任务解决方法:调整APScheduler使用的线程池大小来增加并发处理任务的能力fromapscheduler.schedulers......
  • LeetCode 3009. Maximum Number of Intersections on the Chart
    原题链接在这里:https://leetcode.com/problems/maximum-number-of-intersections-on-the-chart/description/题目:Thereisalinechartconsistingof n pointsconnectedbylinesegments.Youaregivena 1-indexed integerarray y.The kth pointhascoordinates......
  • Rocketmq 不同的topic要配不同的consumegroup
    Rocketmq不同的topic要配不同的consumegroup使用Rocketmq一定要注意,如果项目中要订阅两个topic,一定要保证consumeGroup是两个不同的。这是因为,Consumer会定期发送心跳,默认是30s一次。心跳会像全部broker发送,心跳包内容包括groupname,topicname1。然后broker端会......
  • 53_Maximum Subarray-最大子数组
    问题描述Givenanintegerarray nums,findthe subarray withthelargestsum,andreturn itssum.给定一个数组nums,找到一个子数组。使它的和最大,返回子数组例子Input:nums=[-2,1,-3,4,-1,2,1,-5,4]Output:6Explanation:子数组[4,-1,2,1]有最大的和6.基......
  • LeetCode 1186. Maximum Subarray Sum with One Deletion
    原题链接在这里:https://leetcode.com/problems/maximum-subarray-sum-with-one-deletion/description/题目:Givenanarrayofintegers,returnthemaximumsumfora non-empty subarray(contiguouselements)withatmostoneelementdeletion. Inotherwords,youwa......