forked from Sunchit/Coding-Decoded
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoatsToSavePeople.java
49 lines (40 loc) · 1022 Bytes
/
BoatsToSavePeople.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// @saorav21994
// TC : O(nlogn)
// SC : O(1)
class Solution {
public int numRescueBoats(int[] people, int limit) {
Arrays.sort(people);
int l = people.length;
int s = 0, e = l-1;
int res = 0;
while (s <= e) {
int d = limit - people[e];
if (d >= people[s]) {
s += 1;
}
res += 1;
e -= 1;
}
return res;
}
}
// Author: @romitdutta10
// TC : O(nlogn)
// SC : O(1)
// Problem : https://leetcode.com/problems/boats-to-save-people/
class Solution {
public int numRescueBoats(int[] people, int limit) {
Arrays.sort(people);
int start = 0;
int end = people.length - 1;
int res = 0;
while(start <= end) {
if(people[start] + people[end] <= limit) {
start++;
}
end--;
res++;
}
return res;
}
}