-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay_42.java
More file actions
37 lines (34 loc) · 893 Bytes
/
Copy pathDay_42.java
File metadata and controls
37 lines (34 loc) · 893 Bytes
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
// Find the Index of the First Occurrence in a String
class Solution {
public int strStr(String haystack, String needle) {
int hl = haystack.length();
int nl = needle.length();
if(nl > hl) return -1;
for(int i=0;i<=hl-nl;i++){
if(haystack.substring(i,i+nl).equals(needle)){
return i;
}
}
return -1;
}
}
// Boats to Save People
class Solution {
public int numRescueBoats(int[] people, int limit) {
Arrays.sort(people);
int n = people.length, cnt=0;
int l = 0,r=n-1;
while(l<=r){
if(people[r] + people[l] <= limit){
cnt++;
r--;
l++;
}
else if(people[r]<=limit){
cnt++;
r--;
}
}
return cnt;
}
}