当前位置:网站首页>算法--连续数列(Kotlin)
算法--连续数列(Kotlin)
2022-07-26 13:17:00 【小米科技Android 研发曹新雨】
题目
给定一个整数数组,找出总和最大的连续数列,并返回总和。
示例:
输入: [-2,1,-3,4,-1,2,1,-5,4]
输出: 6
解释: 连续子数组 [4,-1,2,1] 的和最大,为 6。
进阶:
如果你已经实现复杂度为 O(n) 的解法,尝试使用更为精妙的分治法求解。
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/contiguous-sequence-lcci
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解决思路

解决方法
fun maxSubArray(nums: IntArray): Int {
val dp = Array(nums.size) {
0 }
var max = if (nums.isNotEmpty()) nums[0] else 0
nums.forEachIndexed {
index, i ->
if (index >= 1) {
dp[index] = i.coerceAtLeast(dp[index - 1] + i)
} else {
dp[index] = i
}
max = max.coerceAtLeast(dp[index])
}
return max
}
总结
1.别看简单 做了三遍才过
2.思路不够清晰
边栏推荐
- B+ tree index use (6) leftmost principle -- MySQL from entry to proficiency (18)
- Flutter textfield sets the height and automatically wraps lines, and the rounded border removes the underline
- [typescript] typescript common types (Part 2)
- 1312_ Apply 7z command for compression and decompression
- Kubelet CRI container runtime
- B+树索引使用(7)匹配列前缀,匹配值范围(十九)
- B+ tree (4) joint index -- MySQL from entry to proficiency (16)
- B+ tree index uses (7) matching column prefix, matching value range (19)
- Use grid to realize left, middle and right layout, and the middle content is adaptive
- B+树索引使用(6)最左原则 --mysql从入门到精通(十八)
猜你喜欢

Probability theory and mathematical statistics

The best engineer was "forced" away by you like this!

One stroke problem (Chinese postman problem)

panic: Error 1045: Access denied for user ‘root‘@‘117.61.242.215‘ (using password: YES)

学习pinia 介绍-State-Getters-Actions-Plugins
![[flower carving hands-on] interesting and fun music visualization series small project (13) -- organic rod column lamp](/img/d4/7b9c7c99d46661e1be2963a342dd18.jpg)
[flower carving hands-on] interesting and fun music visualization series small project (13) -- organic rod column lamp

Kubernetes Flannel:HOST-GW模式

概率论与数理统计

Flutter multi-channel packaging operation

File upload and download performance test based on the locust framework
随机推荐
Square root of leetcode 69. x
目标检测网络R-CNN 系列
Example of establishing socket communication with Siemens PLC based on C # open TCP communication
How to face scientific and technological unemployment?
AI theory knowledge map 1 Foundation
Bitwise and how to write SQL
C regards type as generic type T and uses it as generic type of method
解决远程主机无法连接mysql数据库的问题
Sword finger offer (21): push in and pop-up sequence of stack
关于自动重复调用接口的一种实现方式-反射
概率论与数理统计
Sword finger offer (IX): abnormal jumping steps
如何面对科技性失业?
多线程使用不当导致的 OOM
LeetCode 1523. 在区间范围内统计奇数数目
Flutter textfield sets the height and automatically wraps lines, and the rounded border removes the underline
From January to June, China's ADAS suppliers accounted for 9%, and another parts giant comprehensively laid out the new smart drive track
基于C#实现的学生考试系统
Kubernetes flannel: host-gw mode
Version of NDK matched the requested version 21.0.6113669. versions available locally: 2