Skip to content

Commit 329b808

Browse files
add MadParents solution
1 parent 606ca41 commit 329b808

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

medium300/MadParents.java

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package medium300;
2+
3+
public class MadParents {
4+
5+
int[] gen(int N, int mul, int add, int r, int l) {
6+
int[] arr = new int[N];
7+
int seed = 0;
8+
for (int i = 0; i < arr.length; i++) {
9+
seed = (seed * mul + add) % (r - l) + l + 1;
10+
arr[i] = seed;
11+
}
12+
return arr;
13+
}
14+
15+
public int catchDrones(int[] params) {
16+
17+
int[] a = gen(params[0], params[1], params[2], params[3], params[4]);
18+
19+
int T = params[5];
20+
int n = params[0];
21+
int i = 0, j = 0;
22+
int sum = 0, ret = 0, ans = 1 << 25;
23+
while (i < n && j < n) {
24+
25+
if (sum + a[j] < T) {
26+
sum += a[j];
27+
j++;
28+
ret++;
29+
} else if (sum < T && sum + a[j] >= T) {
30+
sum += a[j];
31+
j++;
32+
ret++;
33+
ans = Math.min(ret, ans);
34+
} else if (sum >= T) {
35+
sum -= a[i];
36+
i++;
37+
ret--;
38+
if (sum >= T)
39+
ans = Math.min(ret, ans);
40+
}
41+
}
42+
43+
while (sum - a[i] >= T) {
44+
sum -= a[i];
45+
i++;
46+
ret--;
47+
ans = Math.min(ret, ans);
48+
}
49+
50+
return ans;
51+
}
52+
53+
public static void main(String[] args) {
54+
MadParents m = new MadParents();
55+
System.out.println(m.catchDrones(new int[] { 20000, 4115, 20094, 630, 243, 5048148 }));
56+
System.out.println(m.catchDrones(new int[] { 10, 8930, 11272, 26, 4, 93 }));
57+
}
58+
}

0 commit comments

Comments
 (0)