You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi @cutajarj,
I came up with a different solution for Passing Car problem and thoguht that you might be interested to check it out.
The only improvement is that the space complexity is O(1).
publicclassSolution10CarCollision {
publicintsolution(int[] A) {
intfactor = 0;
intsum = 0;
for (inti = 0; i < A.length; i++) {
if (A[i] == 0)
factor += 1;
if (A[i] == 1 && factor != 0)
sum += factor;
if (sum > 1_000_000_000)
return -1;
}
returnsum;
}
@Testpublicvoidtest_solution() throwsException {
Assertions.assertEquals(5, solution(newint[]{0, 1, 0, 1, 1}));
Assertions.assertEquals(6, solution(newint[]{0, 1, 0, 1, 0, 1}));
}
}
The text was updated successfully, but these errors were encountered:
Hi @cutajarj,
I came up with a different solution for Passing Car problem and thoguht that you might be interested to check it out.
The only improvement is that the space complexity is O(1).
The text was updated successfully, but these errors were encountered: