当前位置:网站首页>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) 不需要额外再申请空间
边栏推荐
- Package zip is not available, but is referred to by another package.
- torch.roll()
- 2022.8.4-----leetcode.1403
- rpc-remote procedure call demo
- STM32 uses stm32cubemx LL library series tutorial
- 队列题目:最近的请求次数
- 调用阿里云oss和sms服务
- Lexicon - the maximum depth of a binary tree
- 剑指Offer--找出数组中重复的数字(三种解法)
- 【 genius_platform software platform development 】 : seventy-six vs the preprocessor definitions written cow force!!!!!!!!!!(in the other groups conding personnel told so cow force configuration to can
猜你喜欢

Everyone in China said data, you need to focus on core characteristic is what?

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

word分栏小记

QT language file production

J9 Digital Currency: What is the creator economy of web3?

QT MV\MVC structure

How Jin Cang database correctness verification platform installation file

Data storage practice based on left-order traversal
![[论文笔记] MapReduce: Simplified Data Processing on Large Clusters](/img/89/8adef42b0cfd154e6fa7205afaeade.png)
[论文笔记] MapReduce: Simplified Data Processing on Large Clusters

Details such as compiling pretreatment
随机推荐
2022-08-04 第六小组 瞒春 学习笔记
Beyond YOLO5-Face | YOLO-FaceV2 officially open source Trick+ academic point full
QT MV\MVC structure
How to sort multiple fields and multiple values in sql statement
Syntax basics (variables, input and output, expressions and sequential statements)
How Jin Cang database correctness verification platform installation file
Is your data safe in this hyperconnected world?
人人都在说的数据中台,你需要关注的核心特点是什么?
沃谈小知识 |“远程透传”那点事儿
.NET Application -- Helloworld (C#)
Dynamic management of massive service instances
mysql没法Execute 大拿们求解
In 2022, you still can't "low code"?Data science can also play with Low-Code!
tree table lookup
Likou - preorder traversal, inorder traversal, postorder traversal of binary tree
Data storage practice based on left-order traversal
How to transfer a single node of Youxuan database to a cluster
PostgreSQL数据库 用navicat 打开表结构的时候报错 cannot update secondarysnapshot during a parallel operation 怎么解决?
The problem of lack of dynamic library "libtinfo.so.5" in ksql application under UOS system
引领数字医学高地,中山医院探索打造未来医院“新范式”