-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathPassingCars.java
More file actions
29 lines (23 loc) · 810 Bytes
/
Copy pathPassingCars.java
File metadata and controls
29 lines (23 loc) · 810 Bytes
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
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int[] A) {
// 0: east -->
// 1: west <--
// p: -->
// q: <-- passing
// for each left 0, there are as many passing cars as 1 s on its right
// in other words,
// ... for each 1, there are as many passing cars as 0 s on its left
int eastCount = 0;
int passingCount = 0;
for(int val: A) {
if(val == 0) { eastCount++; }
else { passingCount += eastCount; }
if(passingCount > 1000000000) return -1;
}
return passingCount;
}
}