-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay_38.java
More file actions
55 lines (49 loc) · 1.31 KB
/
Copy pathDay_38.java
File metadata and controls
55 lines (49 loc) · 1.31 KB
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
50
51
52
53
54
55
// Binary Subarrays With Sum
class Solution {
public int atmost(int[] nums, int k) {
if(k<0) return 0;
int n = nums.length;
int l = 0, sum = 0, cnt = 0;
for(int r=0;r<n;r++){
sum+=nums[r];
while(sum>k){
sum-=nums[l];
l++;
}
cnt+=(r-l+1);
}
return cnt;
}
public int numSubarraysWithSum(int[] nums, int goal) {
return atmost(nums,goal) - atmost(nums,goal-1);
}
}
// Reverse Words in a String
class Solution {
public String reverseWords(String s) {
s = s.trim().replaceAll("\\s+"," ");
String[] str = s.split(" ");
int n = str.length;
StringBuilder sb = new StringBuilder();
for(int i=n-1;i>=0;i--){
sb.append(str[i]);
if(i != 0)sb.append(' ');
}
return sb.toString();
}
}
// Minimum Number of Arrows to Burst Balloons
class Solution {
public int findMinArrowShots(int[][] points) {
int n = points.length;
Arrays.sort(points,(a,b) -> Integer.compare(a[1],b[1]));
int cnt = 1, ed = points[0][1];
for(int i=1;i<n;i++){
if(ed<points[i][0]){
cnt++;
ed = points[i][1];
}
}
return cnt;
}
}