-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountDistinctSlices.js
More file actions
86 lines (81 loc) · 1.33 KB
/
Copy pathCountDistinctSlices.js
File metadata and controls
86 lines (81 loc) · 1.33 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
85
86
function solution(M, A) {
console.log(A);
const len = A.length;
let result = len;
let back = 0;
let front = 0;
let B = new Array();
while( back < len) {
while (front < len && ! B[A[front]]) {
result += front - back;
if (result >= 1000000000) {
return 1000000000;
}
B[A[front]] = true;
front++;
}
while (A[back] != A[front]) {
B[A[back]] = false;
back++;
}
B[A[back]] = false;
back++;
}
return result;
}
const tests = [
{
data:{
M: 6,
A: [3, 4, 5, 5, 2]
},
expect: 9
},
{
data:{
M: 6,
A: [ 1, 3, 4, 1, 2, 1, 3, 2, 1 ]
},
expect: 24
},
{
data:{
M: 6,
A: [1, 2, 3, 4, 5, 9, 10, 4, 5]
},
expect: 36
},
{
data:{
M: 6,
A: [ 2, 3, 3, 3, 2, 4, 1, 2, 5, 1, 4, 5, 1, 4 ]
},
expect: 37
},
{
data:{
M: 6,
A: [1, 1]
},
expect: 2
},
{
data:{
M: 6,
A: [1, 1, 5, 5, 2, 9, 10, 4, 5]
},
expect: 24
},
{
data:{
M: 6,
A: [3, 4, 1, 2, 1, 3 ]
},
expect: 15
}
];
for (let i = 0; i < tests.length; i++) {
let index = i + 1;
console.log('Answer ' + index + ':', solution(tests[i].data.M, tests[i].data.A), '| Expect', tests[i].expect);
console.log('********************************');
}