当前位置:网站首页>LeetCode simple problem to find the subsequence of length K with the largest sum
LeetCode simple problem to find the subsequence of length K with the largest sum
2022-07-31 03:04:00 【·The sea of stars】
题目
给你一个整数数组 nums 和一个整数 k .你需要找到 nums 中长度为 k 的 子序列 ,且这个子序列的 和最大 .
请你返回 任意 一个长度为 k 的整数子序列.
子序列 定义为从一个数组里删除一些元素后,不改变剩下元素的顺序得到的数组.
示例 1:
输入:nums = [2,1,3,3], k = 2
输出:[3,3]
解释:
子序列有最大和:3 + 3 = 6 .
示例 2:
输入: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
-10^5 <= nums[i] <= 10 ^5
1 <= k <= nums.length
来源:力扣(LeetCode)
解题思路
The result array required by this question does not need to be contiguous in the original array,This greatly reduces the difficulty of the problem,So the first step is to sort the given array first,Then take the largest elements and their subscripts,Then intercept the results that meet the conditions and return the correct sequence in the original array.
class Solution:
def maxSubsequence(self, nums: List[int], k: int) -> List[int]:
new_nums=sorted(enumerate(nums),key=lambda x:-x[1])
ans=sorted(new_nums[0:k],key=lambda x:x[0])
return [i[1] for i in ans]
Of course, this question does not need to be completely sorted,The best way to do this is to use selection sort to match the conditionskAfter one is selected, it will not continue to sort.
class Solution:
def maxSubsequence(self, nums: List[int], k: int) -> List[int]:
ans=[]
for i in range(k):
MAX,index=-float('inf'),-1
for j in enumerate(nums):
if j[1]!=None:
if j[1]>MAX:
MAX,index=j[1],j[0]
ans.append((index,MAX))
nums[index]=None
return [i[1] for i in sorted(ans)]
边栏推荐
- 公司官网建站笔记(六):域名进行公安备案并将备案号显示在网页底部
- 冒泡排序、选择排序、直接插入排序、二分法查找
- TCP详解(三)
- Mycat's master-slave relationship, vertical sub-database, horizontal sub-table, and detailed configuration of mycat fragmented table query (mysql5.7 series)
- Local area network computer hardware information collection tool
- 多线程下类对象的服务承诺探讨
- 解析小结—自用
- 下载jar包的好地方
- f.grid_sample
- 【CocosCreator 3.5】CocosCreator 获取网络状态
猜你喜欢
软件积累 -- 截图软件ScreenToGif
华为分布式存储FusionStorage知识点总结【面试篇】
Discourse Custom Header Links
LeetCode中等题之分数加减运算
Discourse 自定义头部链接(Custom Header Links)
Project (5) - Small target detection tph-yolov5
编译Hudi
Problems that need to be solved in distributed system architecture
TCP详解(二)
Basic learning about Redis related content
随机推荐
return in try-catch
8、统一处理异常(控制器通知@ControllerAdvice全局配置类、@ExceptionHandler统一处理异常)
15. Website Statistics
Moxa NPort 设备缺陷可能使关键基础设施遭受破坏性攻击
WebSocket Session is null
SQL注入 Less47(报错注入) 和Less49(时间盲注)
分布式系统架构需要解决的问题
The whole process scheduling, MySQL and Sqoop
Several common errors when using MP
Multilingual settings of php website (IP address distinguishes domestic and foreign)
YOLOV5 study notes (2) - environment installation + operation + training
接口测试关键技术
The simulation application of common mode inductance is here, full of dry goods for everyone
Software accumulation -- Screenshot software ScreenToGif
SQL注入 Less54(限制次数的SQL注入+union注入)
公司官网建站笔记(六):域名进行公安备案并将备案号显示在网页底部
2022 Nioke Multi-School League Game 4 Solution
TCP/IP四层模型
冒泡排序、选择排序、直接插入排序、二分法查找
局域网电脑硬件信息收集工具