Movatterモバイル変換


[0]ホーム

URL:


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

Commitc79ed76

Browse files
authored
Merge pull requestneetcode-gh#1342 from julienChemillier/patch-29
Add 881 in c language
2 parentse565c64 +41c8232 commitc79ed76

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

‎c/881-Boats-To-Save-People.c‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
3+
Space: O(n) (because of quicksort)
4+
Time: O(nlog(n)) (because of quicksort)
5+
*/
6+
7+
intcmp(constvoid*a,constvoid*b) {// Function of comparison for quicksort
8+
return*(int*)a-*(int*)b;
9+
}
10+
11+
intnumRescueBoats(int*people,intpeopleSize,intlimit){
12+
intcpt=0;
13+
qsort(people,peopleSize,sizeof(int),cmp);
14+
inti=0;
15+
intj=peopleSize-1;
16+
while (people[i]+people[j]>limit&&j>0)
17+
j--;
18+
cpt+=peopleSize-j-1;
19+
while (i<j) {
20+
if (people[i]+people[j]<=limit){// Boat for 2 people
21+
i++;
22+
j--;
23+
cpt++;
24+
}else {// Boat for 1 person
25+
cpt++;
26+
j--;
27+
}
28+
}
29+
if (i==j)// Boat for 1 person
30+
cpt++;
31+
returncpt;
32+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp