当前位置:网站首页>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
边栏推荐
- Android Practical Development - Kotlin Tutorial (Introduction - Login Function Implementation 3.3)
- Summary of domestic environments supported by SuperMap
- 2022-08-04 The sixth group, hidden from spring, study notes
- 静态方法获取配置文件数据
- J9 Digital Currency: What is the creator economy of web3?
- .NET Application -- Helloworld (C#)
- MRTK3开发Hololens应用-手势拖拽、旋转 、缩放物体实现
- presto启动成功后出现2022-08-04T17:50:58.296+0800 ERROR Announcer-3 io.airlift.discovery.client.Announcer
- sql怎么找字段里所有数据为空的字段
- The Tanabata copywriting you want has been sorted out for you!
猜你喜欢
Use SuperMap iDesktopX data migration tool to migrate map documents and symbols
The second council meeting of the Dragon Lizard Community was successfully held!Director general election, 4 special consultants joined
.NET应用程序--Helloworld(C#)
[Solved] Unity Coroutine coroutine is not executed effectively
如何在WordPress中添加特定类别的小工具
Step by step how to perform data risk assessment
2022高处安装、维护、拆除考试题模拟考试题库及在线模拟考试
结构体初解
开发Hololens遇到The type or namespace name ‘HandMeshVertex‘ could not be found..
2022 High-level installation, maintenance, and removal of exam questions mock exam question bank and online mock exam
随机推荐
Is your data safe in this hyperconnected world?
Why is the pca component not associated
Burp installation and proxy settings
leetcode-每日一题1403. 非递增顺序的最小子序列(贪心)
腾讯云【Hiflow】新时代自动化工具
The problem of lack of dynamic library "libtinfo.so.5" in ksql application under UOS system
Android实战开发-Kotlin教程(入门篇-登录功能实现 3.3)
[Storage] Dawning Storage DS800-G35 ISCSI maps each LUN to the server
The linear table lookup
Android Practical Development - Kotlin Tutorial (Introduction - Login Function Implementation 3.3)
Distributed systems revisited: there will never be a perfect consistency scheme...
开发Hololens遇到The type or namespace name ‘HandMeshVertex‘ could not be found..
Linux下常见的开源数据库,你知道几个?
Simple description of linked list and simple implementation of code
dmp (dump) dump file
private package
Details such as compiling pretreatment
After the large pixel panorama is completed, what are the promotion methods?
告白数字化转型时代,时速云镌刻价值新起点
rpc-remote procedure call demo