当前位置:网站首页>0 dynamic programming leetcode918. Maximum sum of circular subarrays
0 dynamic programming leetcode918. Maximum sum of circular subarrays
2022-07-23 12:56:00 【18 ARU】
918. The maximum sum of the ring subarray
describe
Given a length of n An array of circular integers nums , return nums Non empty of Subarray Maximum possible and .
Ring array It means that the end of the array will be connected with the beginning in a ring . Formally , nums[i] The next element of this is nums[(i + 1) % n] , nums[i] The previous element of is nums[(i - 1 + n) % n] .
Subarray Can only contain fixed buffers at most nums Once for each element in the . Formally , For subarrays nums[i], nums[i + 1], …, nums[j] , non-existent i <= k1, k2 <= j among k1 % n == k2 % n .
Example 1:
Input :nums = [1,-2,3,-2]
Output :3
explain : From subarray [3] Get the maximum and 3
Example 2:
Input :nums = [5,-3,5]
Output :10
explain : From subarray [5,5] Get the maximum and 5 + 5 = 10
Example 3:
Input :nums = [3,-2,2,-3]
Output :3
explain : From subarray [3] and [3,-2,2] You can get the maximum and 3
Tips :
n == nums.length
1 <= n <= 3 * 104
-3 * 104 <= nums[i] <= 3 * 104
source : Power button (LeetCode)
link :https://leetcode.cn/problems/maximum-sum-circular-subarray
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
analysis
Compared with non ring arrays, there is a case of the largest sum of subarrays spliced together from beginning to end , Reverse this situation , That is, when the array sum is unchanged , Find the sum of the minimal array , Then the rest is the largest subarray and .
In addition, we should also consider the case that all numbers are negative , The smallest array is the largest number .
class Solution {
public int maxSubarraySumCircular(int[] nums) {
int n = nums.length;
int[] dp = new int[n];
dp[0] = nums[0];
int ans = nums[0];
int min = nums[0];
int sum = nums[0];
int maxest = nums[0];
for (int i = 1; i < n; i++) {
dp[i] = dp[i-1] < 0 ? nums[i] : nums[i] + dp[i-1];
ans = Math.max(ans,dp[i]);
sum += nums[i];
maxest = Math.max(maxest,nums[i]);
}
Arrays.fill(dp,0);
dp[0] = nums[0];
for (int i = 1; i < n; i++) {
dp[i] = dp[i-1] > 0 ? nums[i] : nums[i] + dp[i-1];
min = Math.min(min,dp[i]);
}
if (Math.max(ans,sum-min) != 0) {
return Math.max(ans,sum-min);
}
return maxest;
}
}
边栏推荐
- 强一致性和弱一致性的分析思路以及分布式场景的并发技巧
- Simple use of psutil monitoring
- How to solve too many if statements
- Unity3d: special effect object pool, timeout delete GameObject in the pool, GC weight
- Hcip --- OSPF details
- InheritableThreadLocal与阿里的TransmittableThreadLocal设计思路解析
- 学习日记(路由与交换技术)——浮动静态路由和缺省路由
- Hcip - first experiment
- 【无标题】
- Explain the flow control mechanism and congestion control mechanism of TCP in detail
猜你喜欢

MySQL性能优化,索引优化

C #: quick sort. If there is the same number, it will be ignored, and then continue the previous search direction to find the next number that meets the requirements for replacement

C # custom stack

Understanding of LSM tree (log structured merge tree)

浅做一下思科实验吧!

Hcip --- HCIA knowledge review (I)

Unity3d HD rendering pipeline cannot play video on the model

学习日记——(路由与交换技术)动态路由(rip协议)和静态路由

vlan配置实例学习记录

FTP实验及概述
随机推荐
制作本地apt源离线安装
手动配置DHCP服务
C language can also write Plants vs. Zombies
Learning diary (routing and switching technology) -- floating static routing and default routing
Hcip --- OSPF details
OSPF 多区域配置实例学习记录
Unity shader missing problem
Hcip --- mGRE comprehensive experiment
2小时1000人吃饭,要多少台座椅?
第一类错误离我们有多远
Gameframework: resource hot code analysis, check version information, download version files, verify version files, get the number of updated files, download files, taskpool
HCIP-HCIA知识回顾(二)
jenkins用到的插件
Analysis ideas of strong consistency and weak consistency and concurrency skills of distributed scenarios
Hcip - HDLC and PPP protocols
三层交换配置实例学习记录
Analysis of Internet Protocol (II)
openvpn部署
Unity3d+moba+ skill indicator (I)
Article on the basic technology needed to build hybrid app