当前位置:网站首页>算法--找到和最大的长度为 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.不要小看此题,我当时看了题解才在限定时间内做出来
边栏推荐
- ggplot2颜色设置总结
- Time Series Data Augmentation for Deep Learning: A Survey 之论文阅读
- Software system dependency analysis
- 牛客网 十进制整数转十六进制字符串
- Epidemic situation, unemployment, 2022, we shouted to lie down!
- LeetCode: 240. 搜索二维矩阵 II
- 12、 Demonstration of all function realization effects
- P6698-[BalticOI 2020 Day2]病毒【AC自动机,dp,SPFA】
- Ggplot2 color setting summary
- cookie加密 4 rpc方法确定cookie加密
猜你喜欢

深入了解 border

Xiaobai needs to learn MySQL - incremental statistical SQL

NETRCA: AN EFFECTIVE NETWORK FAULT CAUSE LOCALIZATION之论文阅读

R 椭圆随机点产生并画图

零基础自学SQL课程 | 相关子查询

Event registration Apache pulsar x kubesphere online meetup hot registration
![[ES6 breakthrough] promise is comparable to native custom encapsulation (10000 words)](/img/b3/b156d75c7b4f03580c449f8499cd74.png)
[ES6 breakthrough] promise is comparable to native custom encapsulation (10000 words)

ApplicationContextInitializer的三种使用方法
Depens:*** but it is not going to be installed

Summary of medical image open source datasets (II)
随机推荐
最新Windows下Go语言开发环境搭建+GoLand配置
ggplot2颜色设置总结
Applet wx show
【自定义Endpoint 及实现原理】
R ellipse random point generation and drawing
threejs的 mmd模型加载+轮廓加载+动画加载+音频加载+相机动画加载+ammojs加载 gltf模型的加载 +gltf的反射调整
Jincang KFS replicator installation (oracle-kes)
谈谈数字化转型晓知识
L01_ How is an SQL query executed?
CF566E-Restoring Map【bitset】
CF566E-Restoring Map【bitset】
leetcode--链表
读CVPR 2022目标检测论文得到的亿点点启发
Linux MySQL installation
Weekly recommended short video: talk about "meta universe" with a serious attitude
e的lnx为什么等于x
Zero foundation self-study SQL course | syntax sequence and execution sequence of SQL statements
深度学习论文阅读目标检测篇(七)中英对照版:YOLOv4《Optimal Speed and Accuracy of Object Detection》
牛客网 字符串变形
Lu Qi: I am most optimistic about these four major technology trends