File tree Expand file tree Collapse file tree 6 files changed +212
-2
lines changed
solution/1400-1499/1403.Minimum Subsequence in Non-Increasing Order Expand file tree Collapse file tree 6 files changed +212
-2
lines changed Original file line number Diff line number Diff line change 49
49
50
50
<!-- 这里可写通用的实现逻辑 -->
51
51
52
+ ** 方法一:排序**
53
+
52
54
<!-- tabs:start -->
53
55
54
56
### ** Python3**
55
57
56
58
<!-- 这里可写当前语言的特殊实现逻辑 -->
57
59
58
60
``` python
59
-
61
+ class Solution :
62
+ def minSubsequence (self , nums : List[int ]) -> List[int ]:
63
+ nums.sort(reverse = True )
64
+ s = sum (nums)
65
+ ans = []
66
+ t = 0
67
+ for v in nums:
68
+ ans.append(v)
69
+ t += v
70
+ if t > s - t:
71
+ break
72
+ return ans
60
73
```
61
74
62
75
### ** Java**
63
76
64
77
<!-- 这里可写当前语言的特殊实现逻辑 -->
65
78
66
79
``` java
80
+ class Solution {
81
+ public List<Integer > minSubsequence (int [] nums ) {
82
+ Arrays . sort(nums);
83
+ List<Integer > ans = new ArrayList<> ();
84
+ int s = 0 ;
85
+ for (int v : nums) {
86
+ s += v;
87
+ }
88
+ int t = 0 ;
89
+ for (int i = nums. length - 1 ; i >= 0 ; -- i) {
90
+ t += nums[i];
91
+ ans. add(nums[i]);
92
+ if (t > s - t) {
93
+ break ;
94
+ }
95
+ }
96
+ return ans;
97
+ }
98
+ }
99
+ ```
100
+
101
+ ### ** C++**
102
+
103
+ ``` cpp
104
+ class Solution {
105
+ public:
106
+ vector<int > minSubsequence(vector<int >& nums) {
107
+ sort(nums.begin(), nums.end());
108
+ int s = 0;
109
+ for (int v : nums) s += v;
110
+ int t = 0;
111
+ vector<int > ans;
112
+ for (int i = nums.size() - 1; ~ i; --i)
113
+ {
114
+ t += nums[ i] ;
115
+ ans.push_back(nums[ i] );
116
+ if (t > s - t) break;
117
+ }
118
+ return ans;
119
+ }
120
+ };
121
+ ```
67
122
123
+ ### **Go**
124
+
125
+ ```go
126
+ func minSubsequence(nums []int) []int {
127
+ sort.Ints(nums)
128
+ s, t := 0, 0
129
+ for _, v := range nums {
130
+ s += v
131
+ }
132
+ ans := []int{}
133
+ for i := len(nums) - 1; i >= 0; i-- {
134
+ t += nums[i]
135
+ ans = append(ans, nums[i])
136
+ if t > s-t {
137
+ break
138
+ }
139
+ }
140
+ return ans
141
+ }
68
142
```
69
143
70
144
### ** ...**
Original file line number Diff line number Diff line change 62
62
### ** Python3**
63
63
64
64
``` python
65
-
65
+ class Solution :
66
+ def minSubsequence (self , nums : List[int ]) -> List[int ]:
67
+ nums.sort(reverse = True )
68
+ s = sum (nums)
69
+ ans = []
70
+ t = 0
71
+ for v in nums:
72
+ ans.append(v)
73
+ t += v
74
+ if t > s - t:
75
+ break
76
+ return ans
66
77
```
67
78
68
79
### ** Java**
69
80
70
81
``` java
82
+ class Solution {
83
+ public List<Integer > minSubsequence (int [] nums ) {
84
+ Arrays . sort(nums);
85
+ List<Integer > ans = new ArrayList<> ();
86
+ int s = 0 ;
87
+ for (int v : nums) {
88
+ s += v;
89
+ }
90
+ int t = 0 ;
91
+ for (int i = nums. length - 1 ; i >= 0 ; -- i) {
92
+ t += nums[i];
93
+ ans. add(nums[i]);
94
+ if (t > s - t) {
95
+ break ;
96
+ }
97
+ }
98
+ return ans;
99
+ }
100
+ }
101
+ ```
102
+
103
+ ### ** C++**
104
+
105
+ ``` cpp
106
+ class Solution {
107
+ public:
108
+ vector<int > minSubsequence(vector<int >& nums) {
109
+ sort(nums.begin(), nums.end());
110
+ int s = 0;
111
+ for (int v : nums) s += v;
112
+ int t = 0;
113
+ vector<int > ans;
114
+ for (int i = nums.size() - 1; ~ i; --i)
115
+ {
116
+ t += nums[ i] ;
117
+ ans.push_back(nums[ i] );
118
+ if (t > s - t) break;
119
+ }
120
+ return ans;
121
+ }
122
+ };
123
+ ```
71
124
125
+ ### **Go**
126
+
127
+ ```go
128
+ func minSubsequence(nums []int) []int {
129
+ sort.Ints(nums)
130
+ s, t := 0, 0
131
+ for _, v := range nums {
132
+ s += v
133
+ }
134
+ ans := []int{}
135
+ for i := len(nums) - 1; i >= 0; i-- {
136
+ t += nums[i]
137
+ ans = append(ans, nums[i])
138
+ if t > s-t {
139
+ break
140
+ }
141
+ }
142
+ return ans
143
+ }
72
144
```
73
145
74
146
### ** ...**
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ vector<int > minSubsequence (vector<int >& nums) {
4
+ sort (nums.begin (), nums.end ());
5
+ int s = 0 ;
6
+ for (int v : nums) s += v;
7
+ int t = 0 ;
8
+ vector<int > ans;
9
+ for (int i = nums.size () - 1 ; ~i; --i)
10
+ {
11
+ t += nums[i];
12
+ ans.push_back (nums[i]);
13
+ if (t > s - t) break ;
14
+ }
15
+ return ans;
16
+ }
17
+ };
Original file line number Diff line number Diff line change
1
+ func minSubsequence (nums []int ) []int {
2
+ sort .Ints (nums )
3
+ s , t := 0 , 0
4
+ for _ , v := range nums {
5
+ s += v
6
+ }
7
+ ans := []int {}
8
+ for i := len (nums ) - 1 ; i >= 0 ; i -- {
9
+ t += nums [i ]
10
+ ans = append (ans , nums [i ])
11
+ if t > s - t {
12
+ break
13
+ }
14
+ }
15
+ return ans
16
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public List <Integer > minSubsequence (int [] nums ) {
3
+ Arrays .sort (nums );
4
+ List <Integer > ans = new ArrayList <>();
5
+ int s = 0 ;
6
+ for (int v : nums ) {
7
+ s += v ;
8
+ }
9
+ int t = 0 ;
10
+ for (int i = nums .length - 1 ; i >= 0 ; --i ) {
11
+ t += nums [i ];
12
+ ans .add (nums [i ]);
13
+ if (t > s - t ) {
14
+ break ;
15
+ }
16
+ }
17
+ return ans ;
18
+ }
19
+ }
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def minSubsequence (self , nums : List [int ]) -> List [int ]:
3
+ nums .sort (reverse = True )
4
+ s = sum (nums )
5
+ ans = []
6
+ t = 0
7
+ for v in nums :
8
+ ans .append (v )
9
+ t += v
10
+ if t > s - t :
11
+ break
12
+ return ans
You can’t perform that action at this time.
0 commit comments