当前位置:网站首页>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) 不需要额外再申请空间
边栏推荐
- The pit of std::string::find return value
- leetcode - a subtree of another tree
- 语法基础(变量、输入输出、表达式与顺序语句)
- 2022-08-04 第六小组 瞒春 学习笔记
- The usage of try...catch and finally in js
- How to Add Category-Specific Widgets in WordPress
- 开发Hololens遇到The type or namespace name ‘HandMeshVertex‘ could not be found..
- [Qixi Festival] Romantic Tanabata, code teaser.Turn love into a gorgeous three-dimensional scene and surprise her (him)!(send code)
- 毕设-基于SSM房屋租赁管理系统
- .NET Application -- Helloworld (C#)
猜你喜欢

你要的七夕文案,已为您整理好!

用Unity发布APP到Hololens2无坑教程

The second council meeting of the Dragon Lizard Community was successfully held!Director general election, 4 special consultants joined

How to sort multiple fields and multiple values in sql statement

冒泡排序与快速排序
![[论文笔记] MapReduce: Simplified Data Processing on Large Clusters](/img/89/8adef42b0cfd154e6fa7205afaeade.png)
[论文笔记] MapReduce: Simplified Data Processing on Large Clusters

Leading the highland of digital medicine, Zhongshan Hospital explores to create a "new paradigm" for future hospitals

Countdown to 2 days|Cloud native Meetup Guangzhou Station, waiting for you!

Apache DolphinScheduler, a new generation of distributed workflow task scheduling platform in practice - Medium

基于生长的棋盘格角点检测方法
随机推荐
Bubble Sort and Quick Sort
【七夕节】浪漫七夕,代码传情。将爱意变成绚烂的立体场景,给她(他)一个惊喜!(送代码)
Snapback - same tree
ffmpeg 像素格式基础知识
627. Change of gender
语法基础(变量、输入输出、表达式与顺序语句)
The problem of lack of dynamic library "libtinfo.so.5" in ksql application under UOS system
905. 区间选点
Chinese characters to Pinyin
Intersection of Boolean Operations in SuperMap iDesktop.Net - Repairing Complex Models with Topological Errors
presto启动成功后出现2022-08-04T17:50:58.296+0800 ERROR Announcer-3 io.airlift.discovery.client.Announcer
引领数字医学高地,中山医院探索打造未来医院“新范式”
高项 02 信息系统项目管理基础
人人都在说的数据中台,你需要关注的核心特点是什么?
从“能用”到“好用” 国产软件自主可控持续推进
龙蜥社区第二届理事大会圆满召开!理事换届选举、4 位特约顾问加入
冒泡排序与快速排序
[Qixi Festival] Romantic Tanabata, code teaser.Turn love into a gorgeous three-dimensional scene and surprise her (him)!(send code)
QT MV\MVC structure
The linear table lookup