Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b74d5af

Browse files
committed
feat: add solutions to lc problem: No.2554
No.2554.Maximum Number of Integers to Choose From a Range I
1 parent 7391828 commit b74d5af

File tree

5 files changed

+204
-0
lines changed

5 files changed

+204
-0
lines changed

‎solution/2500-2599/2554.Maximum Number of Integers to Choose From a Range I/README.md‎

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,79 @@ func maxCount(banned []int, n int, maxSum int) (ans int) {
280280
}
281281
```
282282

283+
### **TypeScript**
284+
285+
```ts
286+
function maxCount(banned: number[], n: number, maxSum: number): number {
287+
const set = new Set(banned);
288+
let sum = 0;
289+
let ans = 0;
290+
for (let i = 1; i <= n; i++) {
291+
if (i + sum > maxSum) {
292+
break;
293+
}
294+
if (set.has(i)) {
295+
continue;
296+
}
297+
sum += i;
298+
ans++;
299+
}
300+
return ans;
301+
}
302+
```
303+
304+
### **Rust**
305+
306+
```rust
307+
use std::collections::HashSet;
308+
impl Solution {
309+
pub fn max_count(banned: Vec<i32>, n: i32, max_sum: i32) -> i32 {
310+
let mut set = banned.into_iter().collect::<HashSet<i32>>();
311+
let mut sum = 0;
312+
let mut ans = 0;
313+
for i in 1..=n {
314+
if sum + i > max_sum {
315+
break;
316+
}
317+
if set.contains(&i) {
318+
continue;
319+
}
320+
sum += i;
321+
ans += 1;
322+
}
323+
ans
324+
}
325+
}
326+
```
327+
328+
### **C**
329+
330+
```c
331+
int cmp(const void *a, const void *b) {
332+
return *(int *) a - *(int *) b;
333+
}
334+
335+
int maxCount(int *banned, int bannedSize, int n, int maxSum) {
336+
qsort(banned, bannedSize, sizeof(int), cmp);
337+
int sum = 0;
338+
int ans = 0;
339+
for (int i = 1, j = 0; i <= n; i++) {
340+
if (sum + i > maxSum) {
341+
break;
342+
}
343+
if (j < bannedSize && i == banned[j]) {
344+
while (j < bannedSize && i == banned[j]) {
345+
j++;
346+
}
347+
} else {
348+
sum += i;
349+
ans++;
350+
}
351+
}
352+
return ans;
353+
}
354+
```
355+
283356
### **...**
284357
285358
```

‎solution/2500-2599/2554.Maximum Number of Integers to Choose From a Range I/README_EN.md‎

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,79 @@ func maxCount(banned []int, n int, maxSum int) (ans int) {
251251
}
252252
```
253253

254+
### **TypeScript**
255+
256+
```ts
257+
function maxCount(banned: number[], n: number, maxSum: number): number {
258+
const set = new Set(banned);
259+
let sum = 0;
260+
let ans = 0;
261+
for (let i = 1; i <= n; i++) {
262+
if (i + sum > maxSum) {
263+
break;
264+
}
265+
if (set.has(i)) {
266+
continue;
267+
}
268+
sum += i;
269+
ans++;
270+
}
271+
return ans;
272+
}
273+
```
274+
275+
### **Rust**
276+
277+
```rust
278+
use std::collections::HashSet;
279+
impl Solution {
280+
pub fn max_count(banned: Vec<i32>, n: i32, max_sum: i32) -> i32 {
281+
let mut set = banned.into_iter().collect::<HashSet<i32>>();
282+
let mut sum = 0;
283+
let mut ans = 0;
284+
for i in 1..=n {
285+
if sum + i > max_sum {
286+
break;
287+
}
288+
if set.contains(&i) {
289+
continue;
290+
}
291+
sum += i;
292+
ans += 1;
293+
}
294+
ans
295+
}
296+
}
297+
```
298+
299+
### **C**
300+
301+
```c
302+
int cmp(const void *a, const void *b) {
303+
return *(int *) a - *(int *) b;
304+
}
305+
306+
int maxCount(int *banned, int bannedSize, int n, int maxSum) {
307+
qsort(banned, bannedSize, sizeof(int), cmp);
308+
int sum = 0;
309+
int ans = 0;
310+
for (int i = 1, j = 0; i <= n; i++) {
311+
if (sum + i > maxSum) {
312+
break;
313+
}
314+
if (j < bannedSize && i == banned[j]) {
315+
while (j < bannedSize && i == banned[j]) {
316+
j++;
317+
}
318+
} else {
319+
sum += i;
320+
ans++;
321+
}
322+
}
323+
return ans;
324+
}
325+
```
326+
254327
### **...**
255328
256329
```
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
int cmp(const void *a, const void *b) {
2+
return *(int *) a - *(int *) b;
3+
}
4+
5+
int maxCount(int *banned, int bannedSize, int n, int maxSum) {
6+
qsort(banned, bannedSize, sizeof(int), cmp);
7+
int sum = 0;
8+
int ans = 0;
9+
for (int i = 1, j = 0; i <= n; i++) {
10+
if (sum + i > maxSum) {
11+
break;
12+
}
13+
if (j < bannedSize && i == banned[j]) {
14+
while (j < bannedSize && i == banned[j]) {
15+
j++;
16+
}
17+
} else {
18+
sum += i;
19+
ans++;
20+
}
21+
}
22+
return ans;
23+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
use std::collections::HashSet;
2+
impl Solution {
3+
pub fn max_count(banned: Vec<i32>, n: i32, max_sum: i32) -> i32 {
4+
let mut set = banned.into_iter().collect::<HashSet<i32>>();
5+
let mut sum = 0;
6+
let mut ans = 0;
7+
for i in 1..=n {
8+
if sum + i > max_sum {
9+
break;
10+
}
11+
if set.contains(&i) {
12+
continue;
13+
}
14+
sum += i;
15+
ans += 1;
16+
}
17+
ans
18+
}
19+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function maxCount(banned: number[], n: number, maxSum: number): number {
2+
const set = new Set(banned);
3+
let sum = 0;
4+
let ans = 0;
5+
for (let i = 1; i <= n; i++) {
6+
if (i + sum > maxSum) {
7+
break;
8+
}
9+
if (set.has(i)) {
10+
continue;
11+
}
12+
sum += i;
13+
ans++;
14+
}
15+
return ans;
16+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /