当前位置:网站首页>Algorithm -- continuous sequence (kotlin)
Algorithm -- continuous sequence (kotlin)
2022-07-26 13:24:00 【Xiaomi technology Android research and development caoxinyu】
subject
Given an array of integers , Find the sequence with the largest sum , And return the sum .
Example :
Input : [-2,1,-3,4,-1,2,1,-5,4]
Output : 6
explain : Continuous subarray [4,-1,2,1] And the biggest , by 6.
Advanced :
If you have implemented complexity as O(n) Solution method , Try to use a more sophisticated divide and conquer method to solve .
source : Power button (LeetCode)
link :https://leetcode.cn/problems/contiguous-sequence-lcci
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Solutions

resolvent
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
}
summary
1. Don't look at simplicity I did it three times before I passed
2. Not clear thinking
边栏推荐
- 【花雕动手做】有趣好玩的音乐可视化系列小项目(12)---米管快速节奏灯
- Golang端口扫描设计
- 华为机考 ~ 偏移量实现字符串加密
- Implementation of SAP ABAP daemon
- Sword finger offer (VII): Fibonacci sequence
- Tupu 3D visual national style design | collision between technology and culture "cool" spark“
- HCIP第十一天比较(BGP的配置、发布)
- Leetcode 263. ugly number
- B+ tree index use (9) grouping, back to table, overlay index (21)
- 为什么要做“密评”?
猜你喜欢

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

牛客刷SQL---2

多线程使用不当导致的 OOM

目标检测网络R-CNN 系列
![[5g] what are Cu and Du in 5g?](/img/5b/3453ade88ded4593edfcba9beb8ada.jpg)
[5g] what are Cu and Du in 5g?

基于Bézier曲线的三维造型与渲染

Implementation of SAP ABAP daemon

AI theory knowledge map 1 Foundation

3D modeling and rendering based on B é zier curve

1312_适用7z命令进行压缩与解压
随机推荐
12-GuliMall 后台管理中商品系统的品牌管理
HCIP第十二天笔记整理(BGP联邦、选路规则)
Activity.onStop() 延迟10秒?精彩绝伦的排查历程
PostgreSQL official website download error
We were tossed all night by a Kong performance bug
Activity. Onstop() delay 10 seconds? Wonderful investigation process
Tupu 3D visual national style design | collision between technology and culture "cool" spark“
With 8 years of product experience, I have summarized these practical experience of continuous and efficient research and development
Is the account opened by flush safe?
《Kotlin系列》之MVVM架构封装(kotlin+mvvm)
Can MySQL customize variable parameter storage functions?
B+ tree (5) introduction to MyISAM -- MySQL from getting started to mastering (17)
Can I take your subdomain? Exploring Same-Site Attacks in the Modern Web
Reflection, an implementation of automatic repeated call interface
Elementary notes of binary tree
[5gc] what is 5g slice? How does 5g slice work?
7-25 0-1 backpack (50 points)
Slam 02. overall framework
如何面对科技性失业?
Flutter integrated Aurora push