Created
April 13, 2015 16:36
-
-
Save stephen-maina/aa872f3f3eaf2b84bcf6 to your computer and use it in GitHub Desktop.
Slow passing cars lesson 3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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