当前位置:网站首页>算法--找到和最大的长度为 K 的子序列(Kotlin)
算法--找到和最大的长度为 K 的子序列(Kotlin)
2022-06-24 08:21:00 【小米科技Android 研发曹新雨】
题目
输入:nums = [-1,-2,3,4], k = 3
输出:[-1,3,4]
解释:
子序列有最大和:-1 + 3 + 4 = 6 。
示例 3:
输入:nums = [3,4,3,3], k = 2
输出:[3,4]
解释:
子序列有最大和:3 + 4 = 7 。
另一个可行的子序列为 [4, 3] 。
提示:
1 <= nums.length <= 1000
-105 <= nums[i] <= 105
1 <= k <= nums.length
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/find-subsequence-of-length-k-with-the-largest-sum
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解决方法
fun maxSubsequence(nums: IntArray, k: Int): IntArray {
var pair = Array(nums.size) {
Array(2) {
0 } }
nums.forEachIndexed {
index, i ->
run {
pair[index][0] = nums[index]
pair[index][1] = index
}
}
Arrays.sort(pair,0,pair.size) {
o1, o2 -> if (o1[0] > o2[0]) -1 else 1 }
Arrays.sort(pair,0,k) {
o1, o2 -> if (o1[1] > o2[1]) 1 else -1 }
var result = IntArray(k)
for (i in 0 until k){
result[i] = pair[i][0]
}
return result
}
总结
1.此题用map 不合适,因为有重复数字,但是可以用二维数组进行辅助
2.不要小看此题,我当时看了题解才在限定时间内做出来
边栏推荐
- P6698-[BalticOI 2020 Day2]病毒【AC自动机,dp,SPFA】
- 学习太极创客 — ESP8226 (十三)OTA
- The printed object is [object object]. Solution
- 谈谈数字化转型晓知识
- Cdga | how can we do well in data governance?
- threejs的 mmd模型加载+轮廓加载+动画加载+音频加载+相机动画加载+ammojs加载 gltf模型的加载 +gltf的反射调整
- PHP使用递归和非递归方式实现创建多级文件夹
- 2020 China's provinces and cities, three-level linkage data, data agencies (data from the official website of the National Bureau of Statistics)
- P6698-[balticoi 2020 day2] virus [AC automata, DP, SPFA]
- Zero foundation self-study SQL course | having clause
猜你喜欢
荐书丨《好奇心的秘密》:一个针尖上可以站多少跳舞的小天使?
Every (), map (), forearch () methods. There are objects in the array
Reasons for the failure of digital transformation and the way to success
ThinkPHP5多语言切换项目实战
PRCT-1400 : 未能执行 getcrshome解决方法
2022.06.23 (traversal of lc_144,94145\
e的lnx为什么等于x
Learn Tai Chi Maker - esp8226 (12) esp8266 multitasking
12、 Demonstration of all function realization effects
Webrtc series - network transmission 5: select the optimal connection switching
随机推荐
Learn Tai Chi Maker - esp8226 (12) esp8266 multitasking
Some common pitfalls in getting started with jupyter:
Xiaobai needs to learn MySQL - incremental statistical SQL
Linux MySQL installation
NLP-D59-nlp比赛D28—我想,也好—阶段总结—心态调整
RISC-V架构下 FPU Context 的动态保存和恢复
threejs的点光源+环境光
【Redis實現秒殺業務①】秒殺流程概述|基本業務實現
When programmers are asked if they can repair computers... | daily anecdotes
Zero foundation self-study SQL course | syntax sequence and execution sequence of SQL statements
Rpiplay implementation of raspberry pie airplay projector
【Eureka注册中心】
2020 China's provinces and cities, three-level linkage data, data agencies (data from the official website of the National Bureau of Statistics)
Cdga | how can we do well in data governance?
学习太极创客 — ESP8226 (十二)ESP8266 多任务处理
June 13-19, 2022 AI industry weekly (issue 102): career development
读CVPR 2022目标检测论文得到的亿点点启发
2021-05-20computed and watch applications and differences
Learning Tai Chi Maker - esp8226 (XIII) OTA
Go language project development practice directory