File tree Expand file tree Collapse file tree 9 files changed +277
-27
lines changed
0800-0899/0870.Advantage Shuffle
1800-1899/1800.Maximum Ascending Subarray Sum Expand file tree Collapse file tree 9 files changed +277
-27
lines changed Original file line number Diff line number Diff line change @@ -151,6 +151,57 @@ func advantageCount(nums1 []int, nums2 []int) []int {
151
151
}
152
152
```
153
153
154
+ ### ** TypeScript**
155
+
156
+ ``` ts
157
+ function advantageCount(nums1 : number [], nums2 : number []): number [] {
158
+ const n = nums1 .length ;
159
+ const idx = Array .from ({ length: n }, (_ , i ) => i );
160
+ idx .sort ((i , j ) => nums2 [i ] - nums2 [j ]);
161
+ nums1 .sort ((a , b ) => a - b );
162
+
163
+ const ans = new Array (n ).fill (0 );
164
+ let left = 0 ;
165
+ let right = n - 1 ;
166
+ for (let i = 0 ; i < n ; i ++ ) {
167
+ if (nums1 [i ] > nums2 [idx [left ]]) {
168
+ ans [idx [left ]] = nums1 [i ];
169
+ left ++ ;
170
+ } else {
171
+ ans [idx [right ]] = nums1 [i ];
172
+ right -- ;
173
+ }
174
+ }
175
+ return ans ;
176
+ }
177
+ ```
178
+
179
+ ### ** Rust**
180
+
181
+ ``` rust
182
+ impl Solution {
183
+ pub fn advantage_count (mut nums1 : Vec <i32 >, nums2 : Vec <i32 >) -> Vec <i32 > {
184
+ let n = nums1 . len ();
185
+ let mut idx = (0 .. n ). collect :: <Vec <usize >>();
186
+ idx . sort_by (| & i , & j | nums2 [i ]. cmp (& nums2 [j ]));
187
+ nums1 . sort ();
188
+ let mut res = vec! [0 ; n ];
189
+ let mut left = 0 ;
190
+ let mut right = n - 1 ;
191
+ for & num in nums1 . iter () {
192
+ if num > nums2 [idx [left ]] {
193
+ res [idx [left ]] = num ;
194
+ left += 1 ;
195
+ } else {
196
+ res [idx [right ]] = num ;
197
+ right -= 1 ;
198
+ }
199
+ }
200
+ res
201
+ }
202
+ }
203
+ ```
204
+
154
205
### ** ...**
155
206
156
207
```
Original file line number Diff line number Diff line change @@ -128,6 +128,57 @@ func advantageCount(nums1 []int, nums2 []int) []int {
128
128
}
129
129
```
130
130
131
+ ### ** TypeScript**
132
+
133
+ ``` ts
134
+ function advantageCount(nums1 : number [], nums2 : number []): number [] {
135
+ const n = nums1 .length ;
136
+ const idx = Array .from ({ length: n }, (_ , i ) => i );
137
+ idx .sort ((i , j ) => nums2 [i ] - nums2 [j ]);
138
+ nums1 .sort ((a , b ) => a - b );
139
+
140
+ const ans = new Array (n ).fill (0 );
141
+ let left = 0 ;
142
+ let right = n - 1 ;
143
+ for (let i = 0 ; i < n ; i ++ ) {
144
+ if (nums1 [i ] > nums2 [idx [left ]]) {
145
+ ans [idx [left ]] = nums1 [i ];
146
+ left ++ ;
147
+ } else {
148
+ ans [idx [right ]] = nums1 [i ];
149
+ right -- ;
150
+ }
151
+ }
152
+ return ans ;
153
+ }
154
+ ```
155
+
156
+ ### ** Rust**
157
+
158
+ ``` rust
159
+ impl Solution {
160
+ pub fn advantage_count (mut nums1 : Vec <i32 >, nums2 : Vec <i32 >) -> Vec <i32 > {
161
+ let n = nums1 . len ();
162
+ let mut idx = (0 .. n ). collect :: <Vec <usize >>();
163
+ idx . sort_by (| & i , & j | nums2 [i ]. cmp (& nums2 [j ]));
164
+ nums1 . sort ();
165
+ let mut res = vec! [0 ; n ];
166
+ let mut left = 0 ;
167
+ let mut right = n - 1 ;
168
+ for & num in nums1 . iter () {
169
+ if num > nums2 [idx [left ]] {
170
+ res [idx [left ]] = num ;
171
+ left += 1 ;
172
+ } else {
173
+ res [idx [right ]] = num ;
174
+ right -= 1 ;
175
+ }
176
+ }
177
+ res
178
+ }
179
+ }
180
+ ```
181
+
131
182
### ** ...**
132
183
133
184
```
Original file line number Diff line number Diff line change
1
+ impl Solution {
2
+ pub fn advantage_count ( mut nums1 : Vec < i32 > , nums2 : Vec < i32 > ) -> Vec < i32 > {
3
+ let n = nums1. len ( ) ;
4
+ let mut idx = ( 0 ..n) . collect :: < Vec < usize > > ( ) ;
5
+ idx. sort_by ( |& i, & j| nums2[ i] . cmp ( & nums2[ j] ) ) ;
6
+ nums1. sort ( ) ;
7
+ let mut res = vec ! [ 0 ; n] ;
8
+ let mut left = 0 ;
9
+ let mut right = n - 1 ;
10
+ for & num in nums1. iter ( ) {
11
+ if num > nums2[ idx[ left] ] {
12
+ res[ idx[ left] ] = num;
13
+ left += 1 ;
14
+ } else {
15
+ res[ idx[ right] ] = num;
16
+ right -= 1 ;
17
+ }
18
+ }
19
+ res
20
+ }
21
+ }
Original file line number Diff line number Diff line change
1
+ function advantageCount ( nums1 : number [ ] , nums2 : number [ ] ) : number [ ] {
2
+ const n = nums1 . length ;
3
+ const idx = Array . from ( { length : n } , ( _ , i ) => i ) ;
4
+ idx . sort ( ( i , j ) => nums2 [ i ] - nums2 [ j ] ) ;
5
+ nums1 . sort ( ( a , b ) => a - b ) ;
6
+
7
+ const ans = new Array ( n ) . fill ( 0 ) ;
8
+ let left = 0 ;
9
+ let right = n - 1 ;
10
+ for ( let i = 0 ; i < n ; i ++ ) {
11
+ if ( nums1 [ i ] > nums2 [ idx [ left ] ] ) {
12
+ ans [ idx [ left ] ] = nums1 [ i ] ;
13
+ left ++ ;
14
+ } else {
15
+ ans [ idx [ right ] ] = nums1 [ i ] ;
16
+ right -- ;
17
+ }
18
+ }
19
+ return ans ;
20
+ }
Original file line number Diff line number Diff line change @@ -149,21 +149,60 @@ func maxAscendingSum(nums []int) int {
149
149
}
150
150
```
151
151
152
+ ### ** C**
153
+
154
+ ``` c
155
+ #define max (a,b ) (((a) > (b)) ? (a) : (b))
156
+
157
+ int maxAscendingSum (int* nums, int numsSize){
158
+ int res = nums[ 0] ;
159
+ int sum = nums[ 0] ;
160
+ for (int i = 1; i < numsSize; i++) {
161
+ if (nums[ i - 1] >= nums[ i] ) {
162
+ res = max(res, sum);
163
+ sum = 0;
164
+ }
165
+ sum += nums[ i] ;
166
+ }
167
+ return max(res, sum);
168
+ }
169
+ ```
170
+
152
171
### **TypeScript**
153
172
154
173
```ts
155
174
function maxAscendingSum(nums: number[]): number {
156
- let ans = 0 ;
157
- let t = 0 ;
158
- for (let i = 0 ; i < nums .length ; ++ i ) {
159
- if (i == 0 || nums [i ] > nums [i - 1 ]) {
160
- t += nums [i ];
161
- ans = Math .max (ans , t );
162
- } else {
163
- t = nums [i ];
175
+ const n = nums.length;
176
+ let res = nums[0];
177
+ let sum = nums[0];
178
+ for (let i = 1; i < n; i++) {
179
+ if (nums[i] <= nums[i - 1]) {
180
+ res = Math.max(res, sum);
181
+ sum = 0;
182
+ }
183
+ sum += nums[i];
184
+ }
185
+ return Math.max(res, sum);
186
+ }
187
+ ```
188
+
189
+ ### ** Rust**
190
+
191
+ ``` rust
192
+ impl Solution {
193
+ pub fn max_ascending_sum (nums : Vec <i32 >) -> i32 {
194
+ let n = nums . len ();
195
+ let mut res = nums [0 ];
196
+ let mut sum = nums [0 ];
197
+ for i in 1 .. n {
198
+ if nums [i - 1 ] >= nums [i ] {
199
+ res = res . max (sum );
200
+ sum = 0 ;
201
+ }
202
+ sum += nums [i ];
164
203
}
204
+ res . max (sum )
165
205
}
166
- return ans ;
167
206
}
168
207
```
169
208
Original file line number Diff line number Diff line change @@ -120,21 +120,60 @@ func maxAscendingSum(nums []int) int {
120
120
}
121
121
```
122
122
123
+ ### ** C**
124
+
125
+ ``` c
126
+ #define max (a,b ) (((a) > (b)) ? (a) : (b))
127
+
128
+ int maxAscendingSum (int* nums, int numsSize){
129
+ int res = nums[ 0] ;
130
+ int sum = nums[ 0] ;
131
+ for (int i = 1; i < numsSize; i++) {
132
+ if (nums[ i - 1] >= nums[ i] ) {
133
+ res = max(res, sum);
134
+ sum = 0;
135
+ }
136
+ sum += nums[ i] ;
137
+ }
138
+ return max(res, sum);
139
+ }
140
+ ```
141
+
123
142
### **TypeScript**
124
143
125
144
```ts
126
145
function maxAscendingSum(nums: number[]): number {
127
- let ans = 0 ;
128
- let t = 0 ;
129
- for (let i = 0 ; i < nums .length ; ++ i ) {
130
- if (i == 0 || nums [i ] > nums [i - 1 ]) {
131
- t += nums [i ];
132
- ans = Math .max (ans , t );
133
- } else {
134
- t = nums [i ];
146
+ const n = nums.length;
147
+ let res = nums[0];
148
+ let sum = nums[0];
149
+ for (let i = 1; i < n; i++) {
150
+ if (nums[i] <= nums[i - 1]) {
151
+ res = Math.max(res, sum);
152
+ sum = 0;
153
+ }
154
+ sum += nums[i];
155
+ }
156
+ return Math.max(res, sum);
157
+ }
158
+ ```
159
+
160
+ ### ** Rust**
161
+
162
+ ``` rust
163
+ impl Solution {
164
+ pub fn max_ascending_sum (nums : Vec <i32 >) -> i32 {
165
+ let n = nums . len ();
166
+ let mut res = nums [0 ];
167
+ let mut sum = nums [0 ];
168
+ for i in 1 .. n {
169
+ if nums [i - 1 ] >= nums [i ] {
170
+ res = res . max (sum );
171
+ sum = 0 ;
172
+ }
173
+ sum += nums [i ];
135
174
}
175
+ res . max (sum )
136
176
}
137
- return ans ;
138
177
}
139
178
```
140
179
Original file line number Diff line number Diff line change
1
+ #define max (a ,b ) (((a) > (b)) ? (a) : (b))
2
+
3
+ int maxAscendingSum (int * nums , int numsSize ){
4
+ int res = nums [0 ];
5
+ int sum = nums [0 ];
6
+ for (int i = 1 ; i < numsSize ; i ++ ) {
7
+ if (nums [i - 1 ] >= nums [i ]) {
8
+ res = max (res , sum );
9
+ sum = 0 ;
10
+ }
11
+ sum += nums [i ];
12
+ }
13
+ return max (res , sum );
14
+ }
Original file line number Diff line number Diff line change
1
+ impl Solution {
2
+ pub fn max_ascending_sum ( nums : Vec < i32 > ) -> i32 {
3
+ let n = nums. len ( ) ;
4
+ let mut res = nums[ 0 ] ;
5
+ let mut sum = nums[ 0 ] ;
6
+ for i in 1 ..n {
7
+ if nums[ i - 1 ] >= nums[ i] {
8
+ res = res. max ( sum) ;
9
+ sum = 0 ;
10
+ }
11
+ sum += nums[ i] ;
12
+ }
13
+ res. max ( sum)
14
+ }
15
+ }
Original file line number Diff line number Diff line change 1
1
function maxAscendingSum ( nums : number [ ] ) : number {
2
- let ans = 0 ;
3
- let t = 0 ;
4
- for ( let i = 0 ; i < nums . length ; ++ i ) {
5
- if ( i == 0 || nums [ i ] > nums [ i - 1 ] ) {
6
- t += nums [ i ] ;
7
- ans = Math . max ( ans , t ) ;
8
- } else {
9
- t = nums [ i ] ;
2
+ const n = nums . length ;
3
+ let res = nums [ 0 ] ;
4
+ let sum = nums [ 0 ] ;
5
+ for ( let i = 1 ; i < n ; i ++ ) {
6
+ if ( nums [ i ] <= nums [ i - 1 ] ) {
7
+ res = Math . max ( res , sum ) ;
8
+ sum = 0 ;
10
9
}
10
+ sum += nums [ i ] ;
11
11
}
12
- return ans ;
12
+ return Math . max ( res , sum ) ;
13
13
}
You can’t perform that action at this time.
0 commit comments