Skip to content

Instantly share code, notes, and snippets.

@stephen-maina
Created April 13, 2015 16:36
Show Gist options
  • Save stephen-maina/aa872f3f3eaf2b84bcf6 to your computer and use it in GitHub Desktop.
Save stephen-maina/aa872f3f3eaf2b84bcf6 to your computer and use it in GitHub Desktop.
Slow passing cars lesson 3
import java.util.BitSet;
import java.util.stream.IntStream;
class Solution {
public int solution(int[] A) {
// write your code in Java SE 8
BitSet bs=new BitSet(A.length+1);
//IntStream.of(A).filter(p->p==1).forEach(i->bs.set(i));
for(int set=0;set<A.length;set++){
if(A[set]==1){
bs.set(set,true);
}else{
bs.set(set,false);
}
}
int count=0;
for(int index=bs.nextClearBit(0);index<bs.length();){
bs.set(0,index,false);
count+=bs.cardinality();
index=bs.nextClearBit(index+1);
}
return count;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment