当前位置:网站首页>leetcode-每日一题1403. 非递增顺序的最小子序列(贪心)
leetcode-每日一题1403. 非递增顺序的最小子序列(贪心)
2022-08-05 03:19:00 【lin钟一】

题目链接:https://leetcode.cn/problems/minimum-subsequence-in-non-increasing-order/
思路
方法一、贪心
题目的大致意思就是把数组分成两个序列,一个序列元素之和严格大于另一个序列元素之和,且满足前面的序列元素最大、长度最短两个要求,所以很简单,我们只需要把数组从大到小排序一遍,计算整个数组之和,然后贪心去取数组的最大值,直到序列元素之和严格大于另一个序列元素之和
代码示例
func minSubsequence(nums []int) []int {
sort.Slice(nums, func(i, j int) bool {
return nums[i] > nums[j]})
tot := 0
for _, num := range nums {
tot += num
}
for i, sum := 0, 0; ; i++ {
sum += nums[i]
if sum > tot-sum {
return nums[:i+1]
}
}
}

复杂度分析
- 时间复杂度:O(n logn) 其中n时数组长度,对数组进行排序需要O(n logn)的时间,对数组进行遍历需要O(n)的时间
- 空间复杂度:O(1) 不需要额外再申请空间
边栏推荐
- undo problem
- IJCAI2022 | DictBert: Pre-trained Language Models with Contrastive Learning for Dictionary Description Knowledge Augmentation
- Flink 1.15.1 Cluster Construction (StandaloneSession)
- 2022高处安装、维护、拆除考试题模拟考试题库及在线模拟考试
- Bubble Sort and Quick Sort
- mysql没法Execute 大拿们求解
- Android实战开发-Kotlin教程(入门篇-登录功能实现 3.3)
- 论治理与创新,2022 开放原子全球开源峰会 OpenAnolis 分论坛圆满落幕
- 【软件测试】自动化测试之unittest框架
- How to transfer a single node of Youxuan database to a cluster
猜你喜欢

Talking about data security governance and privacy computing

腾讯云【Hiflow】新时代自动化工具

Bubble Sort and Quick Sort

Why did they choose to fall in love with AI?

【七夕节】浪漫七夕,代码传情。将爱意变成绚烂的立体场景,给她(他)一个惊喜!(送代码)

链表的简单描述及代码的简单实现

21 Days Learning Challenge (2) Use of Graphical Device Trees

【滤波跟踪】基于matlab无迹卡尔曼滤波惯性导航+DVL组合导航【含Matlab源码 2019期】

论治理与创新,2022 开放原子全球开源峰会 OpenAnolis 分论坛圆满落幕

MRTK3开发Hololens应用-手势拖拽、旋转 、缩放物体实现
随机推荐
Talking about data security governance and privacy computing
In 2022, you still can't "low code"?Data science can also play with Low-Code!
905. 区间选点
Dynamic management of massive service instances
Native js realizes the effect of selecting and canceling all the multi-select boxes
沃谈小知识 |“远程透传”那点事儿
On governance and innovation, the 2022 OpenAtom Global Open Source Summit OpenAnolis sub-forum came to a successful conclusion
The linear table lookup
Study Notes-----Left-biased Tree
AI + Small Nucleic Acid Drugs | Eleven Completes $22 Million Seed Round Financing
Common open source databases under Linux, how many do you know?
Use SuperMap iDesktopX data migration tool to migrate ArcGIS data
你要的七夕文案,已为您整理好!
Why is the pca component not associated
sql server 安装提示用户名不存在
word column notes
High Item 02 Information System Project Management Fundamentals
How to Add Category-Specific Widgets in WordPress
Hash table lookup (hash table)
21天学习挑战赛(2)图解设备树的使用