当前位置:网站首页>算法--连续数列(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 (3) clustered index, secondary index -- MySQL from entry to proficiency (XV)
- Guys, how can CDC Oracle set the reading from the specified SCN number, or how to set the read-only full archive, not to read fast
- flutter多渠道打包运行
- B+树挑选索引(2)---mysql从入门到精通(二十三)
- 二叉树的初阶笔记
- B+树挑选索引(1)---mysql从入门到精通(二十二)
- Slam 02. overall framework
- 同站攻击(相关域攻击)论文阅读 Can I Take Your Subdomain?Exploring Same-Site Attacks in the Modern Web
- JVM: what does the class loading subsystem do? What is it made of? What eight part essay do you need to remember?
- 同花顺开的账户安全吗?
猜你喜欢

Niuke brush sql---2

Kubelet CRI 容器运行时

C regards type as generic type T and uses it as generic type of method

Chat system based on webrtc and websocket

【TypeScript】TypeScript常用类型(上篇)

8 年产品经验,我总结了这些持续高效研发实践经验 · 研发篇

基于BERT的情感分析模型

Kubernetes flannel: host-gw mode

12 brand management of commodity system in gulimall background management

维度灾难 维数灾难 暂记
随机推荐
JSON格式执行计划(6)—mysql执行计划(五十二)
【花雕动手做】有趣好玩的音乐可视化系列小项目(12)---米管快速节奏灯
key&key_len&ref&filtered(4)—mysql执行计划(五十)
panic: Error 1045: Access denied for user ‘root‘@‘117.61.242.215‘ (using password: YES)
Px2rem loader converts PX into REM and adapts to mobile vant UI and other frameworks
StreamNative 团队文化:一家“透明”的公司
[5g] what are Cu and Du in 5g?
Probability theory and mathematical statistics
【TypeScript】TypeScript常用类型(上篇)
B+树(5)myISAM简介 --mysql从入门到精通(十七)
Mysql数据目录(2)---表数据结构(二十五)
B+树挑选索引(2)---mysql从入门到精通(二十三)
Kubernetes Flannel:HOST-GW模式
Qualcomm once again "bet" on Zhongke Chuangda to challenge the full stack solution of intelligent driving software and hardware
JSON数据传递参数&日期型参数传递
Dimension disaster dimension disaster suspense
[flower carving hands-on] interesting and fun music visualization series small project (13) -- organic rod column lamp
Shutter background graying effect, how transparency, gray mask
《Kotlin系列》之MVVM架构封装(kotlin+mvvm)
[applet] why can't the onreachbottom event be triggered? (one second)