当前位置:网站首页>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)]

边栏推荐
- 【C语言】三子棋(经典解法+一览图)
- 分布式系统架构需要解决的问题
- 字体压缩神器font-spider的使用
- IDEA 注释报红解决
- 观察者模式
- JetPack组件Databinding
- Word/Excel fixed table size, when filling in the content, the table does not change with the cell content
- CorelDRAW2022 streamlined Asia Pacific new features in detail
- 8、统一处理异常(控制器通知@ControllerAdvice全局配置类、@ExceptionHandler统一处理异常)
- STM32 problem collection
猜你喜欢
![[Compilation principle] Lexical analysis program design principle and implementation](/img/eb/035234a402edf18beb7e2f82ebec17.png)
[Compilation principle] Lexical analysis program design principle and implementation

Multilingual settings of php website (IP address distinguishes domestic and foreign)

【C语言】求两个整数m和n的最大公因数和最小公倍数之和一般方法,经典解法

8. Unified exception handling (controller notifies @ControllerAdvice global configuration class, @ExceptionHandler handles exceptions uniformly)

MultipartFile file upload

LeetCode简单题之两个数组间的距离值

接口测试关键技术

Why is String immutable?

Crypto Firms Offer Offer To Theft Hackers: Keep A Little, Give The Rest

共模电感的仿真应用来了,满满的干货送给大家
随机推荐
CorelDRAW2022精简亚太新增功能详细介绍
10、Redis实现点赞(Set)和获取总点赞数
MultipartFile文件上传
SQALE 是什么
Word/Excel fixed table size, when filling in the content, the table does not change with the cell content
【CocosCreator 3.5】CocosCreator 获取网络状态
Intel's software and hardware optimization empowers Neusoft to accelerate the arrival of the era of smart medical care
【C语言基础】解决C语言error: expected ‘;‘, ‘,‘ or ‘)‘ before ‘&‘ token
软件积累 -- 截图软件ScreenToGif
YOLOV5 study notes (2) - environment installation + operation + training
你们程序员为什么不靠自己的项目谋生?而必须为其他人打工?
4. Sensitive word filtering (prefix tree)
7. List of private messages
跨专业考研难度大?“上岸”成功率低?这份实用攻略请收下!
11. Redis implements follow, unfollow, and follow and follower lists
SQL injection Less54 (limited number of SQL injection + union injection)
【CV项目调试】CUDNN_CONVOLUTION_FWD_SPECIFY_WORKSPACE_LIMIT问题
【C语言】三子棋(经典解法+一览图)
一份高质量的测试用例如何养成?
Mysql 45讲学习笔记(二十三)MYSQL怎么保证数据不丢