- Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathTT6.java
72 lines (50 loc) · 1.65 KB
/
TT6.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
importjava.util.*;
publicclassTT6 {
publicstaticList<int[]> compress(int[] a) {
List<int[]> result = newArrayList<>();
intcount = 1;
for (inti = 1; i < a.length; i++) {
while (i < a.length && a[i] == a[i - 1]) {
++count;
++i;
}
result.add(newint[] { a[i - 1], count });
count = 1;
}
returnresult;
}
publicstaticintdotProduct(List<int[]> v1, List<int[]> v2) {
intresult = 0;
inti = 0, j = 0;
while (i < v1.size() && j < v2.size()) {
int[] a = v1.get(i), b = v2.get(j);
intcounter = Math.min(a[1], b[1]);
a[1] -= counter;
b[1] -= counter;
result += a[0] * b[0] * counter;
if (a[1] == 0) {
++i;
}
if (b[1] == 0) {
++j;
}
}
returnresult;
}
publicstaticvoidmain(String[] args) {
int[] a = newint[] { 1, 1, 4, 4, 4, 4, 7, 7, 7, 7, 7, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 };
int[] b = newint[] { 2, 2, 5, 5, 5, 5, 5, 5, 5, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3 };
List<int[]> result1 = compress(a);
List<int[]> result2 = compress(b);
for (int[] pair : result1) {
System.out.println(pair[0] + ", " + pair[1]);
}
System.out.println("\n---\n");
for (int[] pair : result2) {
System.out.println(pair[0] + ", " + pair[1]);
}
System.out.println("\n---\n");
System.out.println(dotProduct(result1, result2) == 291);
return;
}
}