当前位置:网站首页>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
边栏推荐
- HCIP第十一天比较(BGP的配置、发布)
- LeetCode 2119. 反转两次的数字
- panic: Error 1045: Access denied for user ‘root‘@‘117.61.242.215‘ (using password: YES)
- Mysql数据目录(1)---数据库结构(二十四)
- 基于ASP.NET的某高校学院档案管理系统
- Implementation of SAP ABAP daemon
- B+树索引使用(6)最左原则 --mysql从入门到精通(十八)
- JSON数据传递参数&日期型参数传递
- Extra (5) - MySQL execution plan (51)
- A college archives management system based on asp.net
猜你喜欢
![[typescript] typescript common types (Part 1)](/img/80/5c8c51b92d3a9d76f38beba7be0aa6.png)
[typescript] typescript common types (Part 1)

How to build a customer-centric product blueprint: suggestions from the chief technology officer

This article explains the FS file module and path module in nodejs in detail

3D modeling and rendering based on B é zier curve

One stroke problem (Chinese postman problem)

同站攻击(相关域攻击)论文阅读 Can I Take Your Subdomain?Exploring Same-Site Attacks in the Modern Web

银行业客户体验管理现状与优化策略分析

学习pinia 介绍-State-Getters-Actions-Plugins
![[5gc] what is 5g slice? How does 5g slice work?](/img/8c/52ba57d6a18133e97fa00b6a7cf8bc.png)
[5gc] what is 5g slice? How does 5g slice work?

Kubelet CRI 容器运行时
随机推荐
Kubernetes flannel: host-gw mode
LeetCode 263.丑数
Photoshop(CC2020)未完
Flutter multi-channel packaging operation
终极套娃 2.0 | 云原生交付的封装
The child component triggers the defineemits of the parent component: the child component passes values to the parent component
异步线程池在开发中的使用
Some practical operations of vector
基于Bézier曲线的三维造型与渲染
【C语言学习者必会的题目集锦1】巩固基础,稳步提高
B+ tree index use (9) grouping, back to table, overlay index (21)
Emotion analysis model based on Bert
B+树(4)联合索引 --mysql从入门到精通(十六)
B+ tree (5) introduction to MyISAM -- MySQL from getting started to mastering (17)
Implementation of SAP ABAP daemon
MySQL data directory (2) -- table data structure (XXV)
如何构建以客户为中心的产品蓝图:来自首席技术官的建议
基于Locust框架进行文件上传下载性能测试
Kubelet CRI container runtime
同站攻击(相关域攻击)论文阅读 Can I Take Your Subdomain?Exploring Same-Site Attacks in the Modern Web