Skip to content

Instantly share code, notes, and snippets.

@stephen-maina
stephen-maina / slow Passing cars lesson3
Created April 13, 2015 16:36
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);
@stephen-maina
stephen-maina / Passing cars codility fast
Last active August 29, 2015 14:19
Passing cars codility fast
class Solution {
public int solution(int[] A) {
// write your code in Java SE 8
int count=0;
int pair=0;
for(int index=0;index<A.length;index++){
if(A[index]==0){
count++;
}else{
pair+=count;
class Solution {
public int solution(int A, int B, int K) {
// write your code in Java SE 8
int first=0;
int last=0;
if(A%K==0){
first=A/K;
}else{
first=(A-A%K+K)/K;
}
import java.util.Arrays;
import java.util.HashMap;
class Solution {
public int[] solution(String S, int[] P, int[] Q) {
// write your code in Java SE 8
int []result=new int[P.length];
for(int index=0;index<P.length;index++){
String check= S.substring(P[index],Q[index]+1);
if(check.contains("A")){
result[index]=1;
@stephen-maina
stephen-maina / MaxProductOfThree lesson 4
Created April 14, 2015 08:25
MaxProductOfThree lesson 4
import java.util.stream.IntStream;
import java.lang.Math;
class Solution {
public int solution(int[] A) {
// write your code in Java SE 8
int [] sorted=IntStream.of(A).sorted().toArray();
return Math.max(sorted[0]*sorted[1]*sorted[sorted.length-1],sorted[sorted.length-1]*sorted[sorted.length-2]*sorted[sorted.length-3]);
}
}
import java.util.stream.IntStream;
import java.util.Set;
import java.util.stream.Collectors;
class Solution {
public int solution(int[] A) {
// write your code in Java SE 8
Set<Integer> result=IntStream.of(A).boxed().collect(Collectors.toSet());
return result.size();
}
}
import java.util.stream.IntStream;
class Solution {
public int solution(int[] A) {
// write your code in Java SE 8
int[]sorted=IntStream.of(A).sorted().toArray();
for(int index=2;index<sorted.length;index++){
long ind1=(long)sorted[index];
long ind2=(long)sorted[index-1];
long ind3=(long)sorted[index-2];
if(ind3+ind2>ind1&&ind1+ind2>ind3&&ind3+ind1>ind2){
@stephen-maina
stephen-maina / disc intersections
Created April 18, 2015 16:05
from research online
class Solution {
public int solution(int[] a) {
int result = 0;
int[] dps = new int[a.length];
int[] dpe = new int[a.length];
for (int i = 0; i < a.length; i++)
{
dps[Math.max(0, i - a[i])]++;
dpe[Math.min(a.length - 1, i + a[i])]++;
import java.util.Stack;
class Solution {
public int solution(String S) {
// write your code in Java SE 8
char [] test= S.toCharArray();
Stack<String> container=new Stack<String>();
for (int index=0;index<test.length;index++){
char popped=test[index];
if(container.empty()){
import java.util.Stack;
class Solution {
public int solution(String S) {
// write your code in Java SE 8
Stack<String>stack=new Stack<String>();
for(int index=0;index<S.length();index++){
if(S.charAt(index)=='('){
stack.push("(");
}else{