forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
771f3a9
commit cac187e
Showing
7 changed files
with
81 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
src/1403-Minimum-Subsequence-in-Non-Increasing-Order/1403.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
class Solution { | ||
public: | ||
vector<int> minSubsequence(vector<int>& nums) { | ||
sort(nums.begin(), nums.end()); | ||
vector<int> res; | ||
int cur = 0, t = accumulate(nums.begin(), nums.end(), 0) / 2; | ||
|
||
for (int i = nums.size() - 1; ~i; i--) { | ||
cur += nums[i]; | ||
res.emplace_back(nums[i]); | ||
if (cur > t) return res; | ||
} | ||
return res; | ||
} | ||
}; |
19 changes: 19 additions & 0 deletions
19
src/1403-Minimum-Subsequence-in-Non-Increasing-Order/1403.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
func minSubsequence(nums []int) []int { | ||
sort.Ints(nums) | ||
res := make([]int, 0) | ||
cur, t := 0, 0 | ||
|
||
for _, i := range nums { | ||
t += i | ||
} | ||
t >>= 1 | ||
|
||
for i := len(nums) - 1; i >= 0; i-- { | ||
cur += nums[i] | ||
res = append(res, nums[i]) | ||
if cur > t { | ||
return res | ||
} | ||
} | ||
return res | ||
} |
17 changes: 17 additions & 0 deletions
17
src/1403-Minimum-Subsequence-in-Non-Increasing-Order/1403.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
class Solution { | ||
public List<Integer> minSubsequence(int[] nums) { | ||
Arrays.sort(nums); | ||
List<Integer> res = new ArrayList(); | ||
int cur = 0, t = 0; | ||
|
||
for (int i : nums) t += i; | ||
t /= 2; | ||
|
||
for (int i = nums.length - 1; i >= 0; i--) { | ||
cur += nums[i]; | ||
res.add(nums[i]); | ||
if (cur > t) return res; | ||
} | ||
return res; | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/1403-Minimum-Subsequence-in-Non-Increasing-Order/1403.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
var minSubsequence = function(nums) { | ||
nums.sort((a, b) => a - b); | ||
let res = [], cur = 0, t = 0; | ||
|
||
for (let i of nums) t += i; | ||
t >>= 1; | ||
|
||
for (let i = nums.length - 1; i >= 0; i--) { | ||
cur += nums[i]; | ||
res.push(nums[i]); | ||
if (cur > t) return res; | ||
} | ||
return res; | ||
}; |
10 changes: 10 additions & 0 deletions
10
src/1403-Minimum-Subsequence-in-Non-Increasing-Order/1403.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
class Solution: | ||
def minSubsequence(self, nums: List[int]) -> List[int]: | ||
nums.sort(reverse=True) | ||
res, cur, t = [], 0, sum(nums) / 2 | ||
|
||
for i in nums: | ||
cur += i | ||
res.append(i) | ||
if cur > t: return res | ||
return res |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters