Skip to content

Commit

Permalink
Add passing cars (Go/Java)
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuamegnauth54 committed May 21, 2024
1 parent 4be9650 commit 71fbf0a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
17 changes: 17 additions & 0 deletions Go/jobspls/algorithms/passing_cars.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package algorithms

func PassingCars(cars []int) int {
west := 0
total := 0

for _, car := range cars {
switch car {
case 0:
west += 1
case 1:
total += west
}
}

return total
}
19 changes: 19 additions & 0 deletions Java/jobspls/algorithms/PassingCars.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package jobspls.algorithms;

public class PassingCars {
public static int passing_cars(int[] cars) {
var west = 0;
var total = 0;

for (var car : cars) {
if (car == 0) {
west++;
} else {
// One eastbound car will pass `west` westbound cars
total += west;
}
}

return total;
}
}

0 comments on commit 71fbf0a

Please sign in to comment.