当前位置:网站首页>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
边栏推荐
- tree table lookup
- 大像素全景制作完成后,推广方式有哪些?
- How to simulate the background API call scene, very detailed!
- You may use special comments to disable some warnings. Three ways to report errors
- A small tool to transfer files using QR code - QFileTrans 1.2.0.1
- Why is the pca component not associated
- Burp installation and proxy settings
- Common open source databases under Linux, how many do you know?
- presto启动成功后出现2022-08-04T17:50:58.296+0800 ERROR Announcer-3 io.airlift.discovery.client.Announcer
- YYGH-13-Customer Service Center
猜你喜欢

In 2022, you still can't "low code"?Data science can also play with Low-Code!

Beidou no. 3 short message terminal high slope in open-pit mine monitoring programme

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

毕设-基于SSM房屋租赁管理系统

为什么pca分量没有关联

冒泡排序与快速排序

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

How to sort multiple fields and multiple values in sql statement

dmp (dump) dump file

冰蝎V4.0攻击来袭,安全狗产品可全面检测
随机推荐
Ffmpeg - sources analysis
QT MV\MVC structure
Queue Topic: Recent Requests
告白数字化转型时代,时速云镌刻价值新起点
The usage of try...catch and finally in js
ffmpeg 枚举decoders, encoders 分析
YYGH-13-客服中心
2022-08-04T17:50:58.296+0800 ERROR Announcer-3 io.airlift.discovery.client.Announcer appears after successful startup of presto
The problem of lack of dynamic library "libtinfo.so.5" in ksql application under UOS system
Burp installation and proxy settings
High Item 02 Information System Project Management Fundamentals
Call Alibaba Cloud oss and sms services
今年七夕,「情蔬」比礼物更有爱
21天学习挑战赛(2)图解设备树的使用
语法基础(变量、输入输出、表达式与顺序语句)
语法基础(变量、输入输出、表达式与顺序语句完成情况)
Never put off till tomorrow what you can put - house lease management system based on the SSM
presto启动成功后出现2022-08-04T17:50:58.296+0800 ERROR Announcer-3 io.airlift.discovery.client.Announcer
undo problem
Confessing the era of digital transformation, Speed Cloud engraves a new starting point for value