当前位置:网站首页>LeetCode简单题之找到和最大的长度为 K 的子序列
LeetCode简单题之找到和最大的长度为 K 的子序列
2022-07-31 02:56:00 【·星辰大海】
题目
给你一个整数数组 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)
解题思路
这个题所要求的结果数组在原数组中是不需要连续的,这样就大大降低了题目的难度,所以第一步可以先将给定的数组排序,然后取出最大的几个元素和它们的下标,然后在截取符合条件的结果并返回正确的在原数组中的先后顺序。
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]

当然这道题其实是不需要完全将排序做完的,最好的方法就是用选择排序将符合条件的k个选出来后便不再继续排序。
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)]

边栏推荐
- STM32CUBEMX开发GD32F303(11)----ADC在DMA模式下扫描多个通道
- Hanyuan Hi-Tech 8-channel HDMI integrated multi-service high-definition video optical transceiver 8-channel HDMI video + 8-channel two-way audio + 8-channel 485 data + 8-channel E1 + 32-channel teleph
- 跨专业考研难度大?“上岸”成功率低?这份实用攻略请收下!
- Intel's software and hardware optimization empowers Neusoft to accelerate the arrival of the era of smart medical care
- 7. List of private messages
- SQL injection Less46 (injection after order by + rand() Boolean blind injection)
- 关于 mysql8.0数据库中主键位id,使用replace插入id为0时,实际id插入后自增导致数据重复插入 的解决方法
- 10. Redis implements likes (Set) and obtains the total number of likes
- JS function this context runtime syntax parentheses array IIFE timer delay self.backup context call apply
- 4、敏感词过滤(前缀树)
猜你喜欢

【Bank Series Phase 1】People's Bank of China

AI在医疗影像设备全流程应用

Chapter 9 SVM实践

Discourse Custom Header Links

Why is String immutable?

Office automation case: how to automatically generate period data?

STM32CUBEMX develops GD32F303 (11) ---- ADC scans multiple channels in DMA mode

SQL注入 Less46(order by后的注入+rand()布尔盲注)

什么是分布式锁?实现分布式锁的三种方式

String为什么不可变?
随机推荐
Mysql 45讲学习笔记(二十四)MYSQL主从一致
SQL注入 Less54(限制次数的SQL注入+union注入)
mycat的主从关系 垂直分库 水平分表 以及mycat分片联表查询的配置详解(mysql5.7系列)
Multilingual settings of php website (IP address distinguishes domestic and foreign)
Chapter 9 SVM Practice
知识蒸馏7:知识蒸馏代码详解
Chapter 9 SVM实践
CMOS和TTL的区别?
VS QT——ui不显示新添加成员(控件)||代码无提示
JS 函数 this上下文 运行时点语法 圆括号 数组 IIFE 定时器 延时器 self.备份上下文 call apply
Installation of mysql5.7.37 under CentOS7 [perfect solution]
关于 mysql8.0数据库中主键位id,使用replace插入id为0时,实际id插入后自增导致数据重复插入 的解决方法
Intranet Infiltration - Privilege Escalation
冒泡排序、选择排序、直接插入排序、二分法查找
execsnoop tool
Unity3D Button mouse hover enter and mouse hover exit button events
Modbus on AT32 MCU
SQL injection Less47 (error injection) and Less49 (time blind injection)
CefSharp入门-winform
SQL injection Less54 (limited number of SQL injection + union injection)