diff --git a/README.md b/README.md index cd01b4e5..59c9147e 100644 --- a/README.md +++ b/README.md @@ -699,4 +699,5 @@ LeetCode |1399|[Count Largest Group](https://leetcode.com/problems/count-largest-group/)|c|[c++](./src/1399-Count-Largest-Group/1399.cpp)|[python](./src/1399-Count-Largest-Group/1399.py)|[go](./src/1399-Count-Largest-Group/1399.go)|[js](./src/1399-Count-Largest-Group/1399.js)|[java](./src/1399-Count-Largest-Group/1399.java)|Easy| |1400|[Construct K Palindrome Strings](https://leetcode.com/problems/construct-k-palindrome-strings/)|c|[c++](./src/1400-Construct-K-Palindrome-Strings/1400.cpp)|[python](./src/1400-Construct-K-Palindrome-Strings/1400.py)|[go](./src/1400-Construct-K-Palindrome-Strings/1400.go)|[js](./src/1400-Construct-K-Palindrome-Strings/1400.js)|[java](./src/1400-Construct-K-Palindrome-Strings/1400.java)|Medium| |1401|[Circle and Rectangle Overlapping](https://leetcode.com/problems/circle-and-rectangle-overlapping/)|c|[c++](./src/1401-Circle-and-Rectangle-Overlapping/1401.cpp)|[python](./src/1401-Circle-and-Rectangle-Overlapping/1401.py)|[go](./src/1401-Circle-and-Rectangle-Overlapping/1401.go)|[js](./src/1401-Circle-and-Rectangle-Overlapping/1401.js)|[java](./src/1401-Circle-and-Rectangle-Overlapping/1401.java)|Medium| -|1402|[Reducing Dishes](https://leetcode.com/problems/reducing-dishes/)|c|[c++](./src/1402-Reducing-Dishes/1402.cpp)|[python](./src/1402-Reducing-Dishes/1402.py)|[go](./src/1402-Reducing-Dishes/1402.go)|[js](./src/1402-Reducing-Dishes/1402.js)|[java](./src/1402-Reducing-Dishes/1402.java)|Hard| \ No newline at end of file +|1402|[Reducing Dishes](https://leetcode.com/problems/reducing-dishes/)|c|[c++](./src/1402-Reducing-Dishes/1402.cpp)|[python](./src/1402-Reducing-Dishes/1402.py)|[go](./src/1402-Reducing-Dishes/1402.go)|[js](./src/1402-Reducing-Dishes/1402.js)|[java](./src/1402-Reducing-Dishes/1402.java)|Hard| +|1403|[Minimum Subsequence in Non-Increasing Order](https://leetcode.com/problems/minimum-subsequence-in-non-increasing-order/)|c|[c++](./src/1403-Minimum-Subsequence-in-Non-Increasing-Order/1403.cpp)|[python](./src/1403-Minimum-Subsequence-in-Non-Increasing-Order/1403.py)|[go](./src/1403-Minimum-Subsequence-in-Non-Increasing-Order/1403.go)|[js](./src/1403-Minimum-Subsequence-in-Non-Increasing-Order/1403.js)|[java](./src/1403-Minimum-Subsequence-in-Non-Increasing-Order/1403.java)|Easy| \ No newline at end of file diff --git a/src/1403-Minimum-Subsequence-in-Non-Increasing-Order/1403.cpp b/src/1403-Minimum-Subsequence-in-Non-Increasing-Order/1403.cpp new file mode 100644 index 00000000..52670fce --- /dev/null +++ b/src/1403-Minimum-Subsequence-in-Non-Increasing-Order/1403.cpp @@ -0,0 +1,15 @@ +class Solution { +public: + vector minSubsequence(vector& nums) { + sort(nums.begin(), nums.end()); + vector 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; + } +}; \ No newline at end of file diff --git a/src/1403-Minimum-Subsequence-in-Non-Increasing-Order/1403.go b/src/1403-Minimum-Subsequence-in-Non-Increasing-Order/1403.go new file mode 100644 index 00000000..215b7518 --- /dev/null +++ b/src/1403-Minimum-Subsequence-in-Non-Increasing-Order/1403.go @@ -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 +} \ No newline at end of file diff --git a/src/1403-Minimum-Subsequence-in-Non-Increasing-Order/1403.java b/src/1403-Minimum-Subsequence-in-Non-Increasing-Order/1403.java new file mode 100644 index 00000000..e9c6cbb7 --- /dev/null +++ b/src/1403-Minimum-Subsequence-in-Non-Increasing-Order/1403.java @@ -0,0 +1,17 @@ +class Solution { + public List minSubsequence(int[] nums) { + Arrays.sort(nums); + List 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; + } +} \ No newline at end of file diff --git a/src/1403-Minimum-Subsequence-in-Non-Increasing-Order/1403.js b/src/1403-Minimum-Subsequence-in-Non-Increasing-Order/1403.js new file mode 100644 index 00000000..8345fe02 --- /dev/null +++ b/src/1403-Minimum-Subsequence-in-Non-Increasing-Order/1403.js @@ -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; +}; \ No newline at end of file diff --git a/src/1403-Minimum-Subsequence-in-Non-Increasing-Order/1403.py b/src/1403-Minimum-Subsequence-in-Non-Increasing-Order/1403.py new file mode 100644 index 00000000..f20949b7 --- /dev/null +++ b/src/1403-Minimum-Subsequence-in-Non-Increasing-Order/1403.py @@ -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 \ No newline at end of file diff --git a/src/addProb.py b/src/addProb.py index 6cfd1a05..f0e567bd 100644 --- a/src/addProb.py +++ b/src/addProb.py @@ -2,10 +2,10 @@ import os, bisect # 题目名称 -name = "Reducing Dishes" -ID = 1402 -url = "https://leetcode.com/problems/reducing-dishes/" -difficult = "Hard" +name = "Minimum Subsequence in Non-Increasing Order" +ID = 1403 +url = "https://leetcode.com/problems/minimum-subsequence-in-non-increasing-order/" +difficult = "Easy" prog = ['c', 'cpp', 'py', 'go', 'js', 'java']