当前位置:网站首页>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
边栏推荐
- Webrtc series - network transmission 5: select the optimal connection switching
- An open source monitoring data collector that can monitor everything
- June 13-19, 2022 AI industry weekly (issue 102): career development
- 深入解析 Apache BookKeeper 系列:第三篇——读取原理
- L01_ How is an SQL query executed?
- 医学图像开源数据集汇总(二)
- The printed object is [object object]. Solution
- [bug] @jsonformat has a problem that the date is less than one day when it is used
- Oracle数据文件头SCN不一致处理方法
- Ggplot2 color setting summary
猜你喜欢
Inspiration from reading CVPR 2022 target detection paper
Depens:*** but it is not going to be installed
LeetCode: 240. 搜索二维矩阵 II
文献调研报告
PTA monkey chooses King (Joseph Ring problem)
Oracle数据文件头SCN不一致处理方法
Support vector machine (SVC, nusvc, linearsvc)
如何让社交媒体成为跨境电商驱动力?这款独立站工具不能错过!
【gdb调试工具】| 如何在多线程、多进程以及正在运行的程序下调试
实战剖析:app扫码登陆实现原理(app+网页端详细逻辑)附源码
随机推荐
What do you mean by waiting for insurance records? Where should I go for filing?
tp5 使用post接收数组数据时报variable type error: array错误的解决方法
Squid proxy application
Groovy通过withCredentials获取Jenkins凭据
The printed object is [object object]. Solution
Thinkphp5清除runtime下的cache缓存,temp缓存,log缓存
软件系统依赖关系分析
浮点数表示法(总结自CS61C和CMU CSAPP)
Target of cmake command_ compile_ options
Oracle查看数据文件头SCN信息
P6698-[BalticOI 2020 Day2]病毒【AC自动机,dp,SPFA】
Support vector machine (SVC, nusvc, linearsvc)
Webrtc series - network transmission 5: select the optimal connection switching
L01_一条SQL查询语句是如何执行的?
When to use RDD and dataframe/dataset
EasyExcel单sheet页与多sheet页写出
LeetCode: 240. 搜索二维矩阵 II
2020 China's provinces and cities, three-level linkage data, data agencies (data from the official website of the National Bureau of Statistics)
Why is LNX of e equal to X
Cmake命令之target_compile_options