当前位置:网站首页>Algorithm -- find and maximum length k subsequence (kotlin)
Algorithm -- find and maximum length k subsequence (kotlin)
2022-06-24 09:37:00 【Xiaomi technology Android R & D caoxinyu】
subject
Input :nums = [-1,-2,3,4], k = 3
Output :[-1,3,4]
explain :
Subsequences have maximum sum :-1 + 3 + 4 = 6 .
Example 3:
Input :nums = [3,4,3,3], k = 2
Output :[3,4]
explain :
Subsequences have maximum sum :3 + 4 = 7 .
Another feasible subsequence is [4, 3] .
Tips :
1 <= nums.length <= 1000
-105 <= nums[i] <= 105
1 <= k <= nums.length
source : Power button (LeetCode)
link :https://leetcode.cn/problems/find-subsequence-of-length-k-with-the-largest-sum
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
resolvent
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
}
summary
1. This question uses map Don't fit , Because there are duplicate numbers , But you can use two-dimensional arrays to assist
2. Don't underestimate this question , At that time, I read the solution of the problem and worked it out in a limited time
边栏推荐
- threejs的点光源+环境光
- Learn Tai Chi Maker - esp8226 (12) esp8266 multitasking
- Tools
- Weekly recommended short video: talk about "meta universe" with a serious attitude
- P6698-[BalticOI 2020 Day2]病毒【AC自动机,dp,SPFA】
- Thinkphp5清除runtime下的cache缓存,temp缓存,log缓存
- leetcode--字符串
- P6117-[JOI 2019 Final]コイン集め【贪心】
- Reasons for the failure of digital transformation and the way to success
- latex公式及表格识别
猜你喜欢

Numpy NP in numpy c_ And np r_ Explain in detail

Zero foundation self-study SQL course | sub query

Learning Tai Chi Maker - esp8226 (XIII) OTA

深入解析 Apache BookKeeper 系列:第三篇——读取原理

【gdb调试工具】| 如何在多线程、多进程以及正在运行的程序下调试

jupyter入门常见的几个坑:

LeetCode: 240. 搜索二维矩阵 II

Oracle 12c升级至19c后ORA-28000错误

June 13-19, 2022 AI industry weekly (issue 102): career development

2022.6.13-6.19 AI行业周刊(第102期):职业发展
随机推荐
How to locate lock waiting in Dameng database
Tools
Event registration Apache pulsar x kubesphere online meetup hot registration
读CVPR 2022目标检测论文得到的亿点点启发
LeetCode: 240. 搜索二维矩阵 II
CDGA|到底怎么才能做好数据治理呢?
ggplot2颜色设置总结
Squid proxy application
Longest public prefix of leetcode
tp5 使用post接收数组数据时报variable type error: array错误的解决方法
二十、处理器调度(RR时间片轮转,MLFQ多级反馈队列,CFS完全公平调度器,优先级翻转;多处理器调度)
PHP封装一个文件上传类(支持单文件多文件上传)
文献调研报告
小白学习MySQL - 增量统计SQL的需求
PhpStrom代码格式化设置
牛客网 十进制整数转十六进制字符串
Why is LNX of e equal to X
Turn to: CEO of Samsung Electronics: all decisions should start from recognizing yourself
WindowManager 简单悬浮框的实现
Groovy通过withCredentials获取Jenkins凭据