Skip to content

Instantly share code, notes, and snippets.

@stephen-maina
stephen-maina / Fish codility lesson 5
Created April 18, 2015 20:20
had to research to get the full 100
import java.util.Stack;
class Solution {
public int solution(int[] A, int[] B) {
// write your code in Java SE 8
Stack<Integer> upStack=new Stack<Integer>();
Stack<Integer> downStack=new Stack<Integer>();
for(int index=0;index<A.length;index++){
if(B[index]==0){
while(!downStack.empty()&&downStack.peek()<A[index]){
import java.util.Stack;
class Solution {
public int solution(int[] H) {
// write your code in Java SE 8
Stack<Integer> wall=new Stack<Integer>();
int blocks=0;
for(int index=0;index<H.length;index++){
while(!wall.empty()&&H[index]<wall.peek()){
wall.pop();
if(!wall.empty()&&H[index]>wall.peek()){
@stephen-maina
stephen-maina / equileader
Last active August 29, 2015 14:19
researched the counting bit online
// you can also use imports, for example:
// import java.util.*;
// you can use System.out.println for debugging purposes, e.g.
// System.out.println("this is a debug message");
import java.util.Stack;
class Solution {
public int solution(int[] A) {
// write your code in Java SE 8
Stack<Integer> stack=new Stack<Integer>();
import java.util.Stack;
class Solution {
public int solution(int[] A) {
// write your code in Java SE 8
if(A.length==0){
return -1;
}
Stack<Integer> stack=new Stack<Integer>();
int size=0;
int dominatorIndex=0;
class Solution {
public int solution(int[] A) {
// write your code in Java SE 8
if(A.length==0){
return 0;
}
int ans=0;
int min=Integer.MAX_VALUE;
@stephen-maina
stephen-maina / maxslicesum
Created May 2, 2015 16:54
had to research
public class Solution {
public int solution(int[] A) {
int ans = A[0];
int sum = 0;
for (int i = 0; i < A.length; i++) {
if (sum > 0) {
sum += A[i];
} else {
sum = A[i];
}
import java.math.BigInteger;
import java.util.Random;
class Solution {
private static final Random rnd = new Random();
public static boolean miller_rabin(BigInteger n) {
for (int repeat = 0; repeat < 20; repeat++) {
BigInteger a;
do {
a = new BigInteger(n.bitLength(), rnd);
} while (a.equals(BigInteger.ZERO));
class Solution {
public int solution(int N) {
// write your code in Java SE 8
int fac=0;
int count=0;
for(int index=(int)Math.sqrt(N);index>0;index--){
fac=N/index;
if(fac*index==N){count=count+2;
if(fac==index){count-=1;}}
@stephen-maina
stephen-maina / peaks
Created May 4, 2015 17:16
not working only works for sample
class Solution {
public int solution(int[] A) {
// write your code in Java SE 8
if(A.length<=2){
return 0;
}
int []peak=getPeaks(A);
int blocks=0;
for(int blk=1;blk<A.length;blk++){
int index=1;
@stephen-maina
stephen-maina / flags
Created May 4, 2015 21:05
still not fully working 40%
import java.util.ArrayList;
class Solution {
public int solution(int[] A) {
// write your code in Java SE 8
if(A.length<=2){
return 0;
}
ArrayList<Integer>peak=getPeaksIndex(A);
if(peak.size()==0){
return 0;