当前位置:网站首页>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
边栏推荐
- ffmpeg pixel format basics
- Principle and Technology of Virtual Memory
- CPDA|How Operators Learn Data Analysis (SQL) from Negative Foundations
- MRTK3开发Hololens应用-手势拖拽、旋转 、缩放物体实现
- Tencent Cloud [Hiflow] New Era Automation Tool
- QStyle platform style
- 剑指Offer--找出数组中重复的数字(三种解法)
- 2022-08-04T17:50:58.296+0800 ERROR Announcer-3 io.airlift.discovery.client.Announcer appears after successful startup of presto
- 语法基础(变量、输入输出、表达式与顺序语句完成情况)
- Dameng 8 database export and import
猜你喜欢

Use SuperMap iDesktopX data migration tool to migrate ArcGIS data

使用二维码传输文件的小工具 - QFileTrans 1.2.0.1

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

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

用CH341A烧录外挂Flash (W25Q16JV)

Getting Started with Kubernetes Networking

冰蝎V4.0攻击来袭,安全狗产品可全面检测

引领数字医学高地,中山医院探索打造未来医院“新范式”

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

The usage of try...catch and finally in js
随机推荐
YYGH-13-Customer Service Center
告白数字化转型时代,时速云镌刻价值新起点
(十一)元类
sql怎么找字段里所有数据为空的字段
2022.8.4-----leetcode.1403
QT MV\MVC structure
YYGH-13-客服中心
人人都在说的数据中台,你需要关注的核心特点是什么?
今年七夕,「情蔬」比礼物更有爱
2022-08-04T17:50:58.296+0800 ERROR Announcer-3 io.airlift.discovery.client.Announcer appears after successful startup of presto
Ffmpeg - sources analysis
AI + Small Nucleic Acid Drugs | Eleven Completes $22 Million Seed Round Financing
leetcode - a subtree of another tree
sql server installation prompts that the username does not exist
达梦8数据库导出导入
STM32 uses stm32cubemx LL library series tutorial
剑指Offer--找出数组中重复的数字(三种解法)
【软件测试】自动化测试之unittest框架
QT language file production
rpc-remote procedure call demo