-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution1124.java
More file actions
84 lines (78 loc) · 2.76 KB
/
Copy pathSolution1124.java
File metadata and controls
84 lines (78 loc) · 2.76 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package medium;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class Solution1124 {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader(new File("test.txt")));
String line;
List temp = new ArrayList();
while ((line = reader.readLine()) != null) {
String[] strings = line.split(",");
int[] array = new int[strings.length];
for (int i = 0; i < strings.length; i++)
array[i] = Integer.parseInt(strings[i]);
temp.add(array);
}
int[][] cases = new int[temp.size()][];
for (int i = 0; i < temp.size(); i++)
cases[i] = (int[]) temp.get(i);
Solution1124 solution = new Solution1124(), bad = new BadSolution(), test = new TestSolution();
int i = 0;
long start;
while (i < cases.length) {
start = System.currentTimeMillis();
System.out.println("good: " + solution.longestWPI(cases[i].clone()) + " " + (System.currentTimeMillis() - start) + "ms");
start = System.currentTimeMillis();
System.out.println("test: " + test.longestWPI(cases[i].clone()) + " " + (System.currentTimeMillis() - start) + "ms");
start = System.currentTimeMillis();
System.out.println("bad: " + bad.longestWPI(cases[i].clone()) + " " + (System.currentTimeMillis() - start) + "ms");
i++;
}
}
public int longestWPI(int[] hours) {
int max = 0;
for (int i = 0; i < hours.length; i++) {
int temp = 0;
hours[i] = hours[i] > 8 ? 1 : -1;
for (int j = i; j >= 0; j--) {
temp += hours[j];
if (temp > 0)
max = Math.max(max, i - j + 1);
}
}
return max;
}
}
class BadSolution extends Solution1124 {
public int longestWPI(int[] hours) {
int max = 0;
for (int i = 0; i < hours.length; i++) {
int temp = 0;
for (int j = i; j >= 0; j--) {
if (hours[j] > 8) temp++;
else temp--;
if (temp > 0)
max = Math.max(max, i - j + 1);
}
}
return max;
}
}
class TestSolution extends Solution1124 {
public int longestWPI(int[] hours) {
int max = 0;
for (int i = 0; i < hours.length; i++) {
int temp = 0;
for (int j = i; j >= 0; j--) {
temp += hours[j] > 8 ? 1 : -1;
if (temp > 0)
max = Math.max(max, i - j + 1);
}
}
return max;
}
}