当前位置:网站首页>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) 不需要额外再申请空间
边栏推荐
- Why did they choose to fall in love with AI?
- [论文笔记] MapReduce: Simplified Data Processing on Large Clusters
- 【七夕节】浪漫七夕,代码传情。将爱意变成绚烂的立体场景,给她(他)一个惊喜!(送代码)
- Object.defineProperty monitors data changes in real time and updates the page
- Principle and Technology of Virtual Memory
- 金仓数据库如何验证安装文件平台正确性
- 十五. 实战——mysql建库建表 字符集 和 排序规则
- Review 51 MCU
- 惨遭打脸:字节某部门竟有这么多测试员
- QT MV\MVC structure
猜你喜欢
.NET应用程序--Helloworld(C#)
Use SuperMap iDesktopX data migration tool to migrate ArcGIS data
mysql can't Execute, please solve it
金仓数据库如何验证安装文件平台正确性
为什么pca分量没有关联
How Jin Cang database correctness verification platform installation file
The linear table lookup
人人都在说的数据中台,你需要关注的核心特点是什么?
A small tool to transfer files using QR code - QFileTrans 1.2.0.1
How to sort multiple fields and multiple values in sql statement
随机推荐
语法基础(变量、输入输出、表达式与顺序语句完成情况)
【已解决】Unity Coroutinue 协程未有效执行的问题
ASP.NET application--Hello World
The usage of try...catch and finally in js
Study Notes-----Left-biased Tree
leetcode - symmetric binary tree
private封装
【Daily Training】1403. Minimum Subsequence in Non-Increasing Order
(11) Metaclass
On governance and innovation, the 2022 OpenAtom Global Open Source Summit OpenAnolis sub-forum came to a successful conclusion
Talking about data security governance and privacy computing
Turn: Charles Handy: Who you are is more important than what you do
腾讯云【Hiflow】新时代自动化工具
How Jin Cang database correctness verification platform installation file
Programmer's Tanabata Romantic Moment
Object.defineProperty monitors data changes in real time and updates the page
如何在WordPress中添加特定类别的小工具
(十一)元类
Use @Mapper to query the partition status of oracle and report an error
1667. Fix names in tables