当前位置:网站首页>Increasing leetcode - a daily topic 1403. The order of the boy sequence (greed)
Increasing leetcode - a daily topic 1403. The order of the boy sequence (greed)
2022-08-05 03:24:00 【Lin Zhongyi】
Title link: https://leetcode.cn/problems/minimum-subsequence-in-non-increasing-order/
Ideas
Method 1, Greedy
The general meaning of the title is to divide the array into two sequences. The sum of the elements of one sequence is strictly greater than the sum of the elements of the other sequence, and the two requirements of the previous sequence the largest element and the shortest length are met.So it's very simple, we only need to sort the array from large to small, calculate the sum of the entire array, and then greedily take the maximum value of the array until the sum of the sequence elements is strictly greater than the sum of the other sequence elements
Code Example
func minSubsequence(nums []int) []int {sort.Slice(nums, func(i, j int) bool { return nums[i] > nums[j]})tot := 0for _, num := range nums {tot += num}for i, sum := 0, 0; ; i++ {sum += nums[i]if sum > tot-sum {return nums[:i+1]}}}
Complexity Analysis
- Time complexity: O(n logn) where n is the length of the array, sorting the array takes O(n logn) time, and traversing the array takes O(n) time
- Space complexity: O(1) No need to apply for additional space
边栏推荐
- 2022.8.4-----leetcode.1403
- Countdown to 2 days|Cloud native Meetup Guangzhou Station, waiting for you!
- 用Unity发布APP到Hololens2无坑教程
- 21天学习挑战赛(2)图解设备树的使用
- [Qixi Festival] Romantic Tanabata, code teaser.Turn love into a gorgeous three-dimensional scene and surprise her (him)!(send code)
- 905. 区间选点
- Kubernetes 网络入门
- YYGH-13-Customer Service Center
- 【 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
- Solve the problem of port occupancy Port xxxx was already in use
猜你喜欢
How to Add Category-Specific Widgets in WordPress
tree table lookup
Matlab drawing 3
presto启动成功后出现2022-08-04T17:50:58.296+0800 ERROR Announcer-3 io.airlift.discovery.client.Announcer
2022高处安装、维护、拆除考试题模拟考试题库及在线模拟考试
A small tool to transfer files using QR code - QFileTrans 1.2.0.1
Intersection of Boolean Operations in SuperMap iDesktop.Net - Repairing Complex Models with Topological Errors
[Qixi Festival] Romantic Tanabata, code teaser.Turn love into a gorgeous three-dimensional scene and surprise her (him)!(send code)
使用二维码传输文件的小工具 - QFileTrans 1.2.0.1
public static <T> List<T> asList(T... a) 原型是怎么回事?
随机推荐
Use Unity to publish APP to Hololens2 without pit tutorial
After the large pixel panorama is completed, what are the promotion methods?
IJCAI2022 | DictBert: Pre-trained Language Models with Contrastive Learning for Dictionary Description Knowledge Augmentation
语法基础(变量、输入输出、表达式与顺序语句)
21天学习挑战赛(2)图解设备树的使用
QT: The Magical QVarient
Linux下常见的开源数据库,你知道几个?
2022-08-04 The sixth group, hidden from spring, study notes
2022 High-level installation, maintenance, and removal of exam questions mock exam question bank and online mock exam
Leading the highland of digital medicine, Zhongshan Hospital explores to create a "new paradigm" for future hospitals
龙蜥社区第二届理事大会圆满召开!理事换届选举、4 位特约顾问加入
presto启动成功后出现2022-08-04T17:50:58.296+0800 ERROR Announcer-3 io.airlift.discovery.client.Announcer
(十一)元类
Use @Mapper to query the partition status of oracle and report an error
【滤波跟踪】基于matlab无迹卡尔曼滤波惯性导航+DVL组合导航【含Matlab源码 2019期】
rpc-remote procedure call demo
Why did they choose to fall in love with AI?
[论文笔记] MapReduce: Simplified Data Processing on Large Clusters
【七夕节】浪漫七夕,代码传情。将爱意变成绚烂的立体场景,给她(他)一个惊喜!(送代码)
[Storage] Dawning Storage DS800-G35 ISCSI maps each LUN to the server