当前位置:网站首页>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) 不需要额外再申请空间
边栏推荐
- 你要的七夕文案,已为您整理好!
- Leading the highland of digital medicine, Zhongshan Hospital explores to create a "new paradigm" for future hospitals
- The pit of std::string::find return value
- 2022-08-04 The sixth group, hidden from spring, study notes
- From "useable" to "easy to use", domestic software is self-controllable and continues to advance
- Question about #sql shell#, how to solve it?
- ffmpeg 像素格式基础知识
- Countdown to 2 days|Cloud native Meetup Guangzhou Station, waiting for you!
- Cloud Native (32) | Introduction to Platform Storage System in Kubernetes
- Thinking (88): Use protobuf custom options for multi-version management of data
猜你喜欢

Cloud Native (32) | Introduction to Platform Storage System in Kubernetes

如何在WordPress中添加特定类别的小工具

Matlab drawing 3

QT language file production

Step by step how to perform data risk assessment

The usage of try...catch and finally in js

word column notes

龙蜥社区第二届理事大会圆满召开!理事换届选举、4 位特约顾问加入

Beyond YOLO5-Face | YOLO-FaceV2 officially open source Trick+ academic point full

毕设-基于SSM房屋租赁管理系统
随机推荐
private package
【滤波跟踪】基于matlab无迹卡尔曼滤波惯性导航+DVL组合导航【含Matlab源码 2019期】
1667. Fix names in tables
数学-求和符号的性质
Never put off till tomorrow what you can put - house lease management system based on the SSM
2022.8.4-----leetcode.1403
leetcode - symmetric binary tree
Data storage practice based on left-order traversal
2022了你还不会『低代码』?数据科学也能玩转Low-Code啦!
(十一)元类
冰蝎V4.0攻击来袭,安全狗产品可全面检测
Multithreading (2)
A small tool to transfer files using QR code - QFileTrans 1.2.0.1
word分栏小记
Snapback - same tree
引领数字医学高地,中山医院探索打造未来医院“新范式”
YYGH-13-客服中心
沃谈小知识 |“远程透传”那点事儿
Turn: Charles Handy: Who you are is more important than what you do
Step by step how to perform data risk assessment