Skip to content

Instantly share code, notes, and snippets.

View bemusementpark's full-sized avatar

Andrew bemusementpark

View GitHub Profile
@bemusementpark
bemusementpark / countMaxFloodFill.kt
Last active March 4, 2019 13:41
A solution to the flood-fill related question described here: https://youtu.be/IWvbPIYQPFM
fun countMaxFloodFill(colors: Array<IntArray>): Int {
var best = 0
val counts = arrayOfNulls<AtomicInteger?>(colors.size)
colors.forEachIndexed { y, rows ->
rows.forEachIndexed { x, color ->
// get the current count from the neighbour above or to the left of the current cell if either have the same color.
// If above and left have the same color, check if they are connected, if they are not, we can connect them and add them together.
val leftIsSameColor = colors.getOrNull(y)?.getOrNull(x - 1)
@bemusementpark
bemusementpark / Averages.java
Created May 21, 2016 13:47
Online implementations of Mean, Median and Mode
import java.util.*;
public class Averages {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
Mean mean = new Mean();
GeometricMean geometricMean = new GeometricMean();
Median<Double> median = new Median<>((a, b) -> (a + b) / 2);
@bemusementpark
bemusementpark / UniqueList.java
Last active May 27, 2016 17:55
A List that prevents duplicates with O(1) random access. Similar to a LinkedHashSet but more useful in an Adapter class thanks to O(1) random access.
import android.support.annotation.NonNull;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.RandomAccess;
import java.util.Set;
/**
* Created by andy on 14/08/15.