Skip to content

Instantly share code, notes, and snippets.

View stphung's full-sized avatar

Steven Phung stphung

  • Autodesk, Inc.
  • San Francisco, CA
View GitHub Profile
@stphung
stphung / CandyShop.java
Created June 2, 2011 15:56
TopCoder SRM 508 Division 2 - 250 point problem
import java.util.HashSet;
import java.util.Set;
public class CandyShop {
public int countProbablePlaces(int[] X, int[] Y, int[] R) {
Set<Position> pos = new HashSet<Position>();
pos.add(new Position(X[0], Y[0]));
pos = locationsFor(R[0], pos);
for (int i = 1; i < X.length; i++) {
@stphung
stphung / CubeStickers.java
Created May 29, 2011 21:35
TopCoder SRM 507 Division 2 - 500 point problem
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
public class CubeStickers {
public String isPossible(String[] sticker) {
Set<String> uniqueStickers = new HashSet<String>();
for (String s : sticker) uniqueStickers.add(s);
if (uniqueStickers.size() >= 5) return "YES";
@stphung
stphung / CubeAnts.java
Created May 29, 2011 21:35
TopCoder SRM 507 Division 2 - 250 point problem
import java.util.ArrayList;
import java.util.List;
public class CubeAnts {
public int getMinimumSteps(int[] pos) {
List<Integer> l = new ArrayList<Integer>();
for (int i : pos) l.add(i);
if (l.contains(6)) return 3;
else if (l.contains(2) || l.contains(5) || l.contains(7)) return 2;
else if (l.contains(3) || l.contains(1) || l.contains(4)) return 1;
@stphung
stphung / TheNumbersWithLuckyLastDigit.java
Created May 17, 2011 16:18
TopCoder SRM 505 Division 2 - 500 point problem
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class TheNumbersWithLuckyLastDigit {
private static int INF = 9999;
public int find(int n) {
int[][] edges = new int[10][10];
for (int i = 0; i < edges.length; i++)
@stphung
stphung / Magicka.scala
Created May 10, 2011 05:22
Google Code Jam 2011 Qualifier Round - Problem B, Magicka
object Magicka {
def invokeElements(spells: String, combines: List[String], oppositions: List[String]) = {
val oppositionSets = oppositions.map(_.toSet)
val combineMap = combines.map(c => (Set(c.charAt(0),c.charAt(1)), c.charAt(2))).toMap
spells.map(List(_)).foldLeft(List[Char]())((acc,spell) => {
acc.size match {
case 0 => spell
case _ => {
// Lazily compute the set which represents spells in the accumulator that clash with the current spell.
lazy val clashes = oppositionSets.filter(_.contains(spell.head)).map(_.diff(Set(spell.head))).flatten
@stphung
stphung / PaintBucket.java
Created May 9, 2011 03:30
Implementation of paint bucket operation using bfs
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
public class PaintBucket {
public static void main(String[] args) {
int[][] colors = new int[][] {{2,2,0,1},
{1,0,0,0},
{0,0,0,1},
{1,1,1,1}};
@stphung
stphung / CandySplitting.java
Created May 8, 2011 19:17
Google Code Jam 2011 Qualifier Round - Problem C, Candy Splitting
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
public class CandySplitting {
public static void main(String[] args) throws FileNotFoundException {
Scanner sc = new Scanner(new File("C-small-attempt0.in"));
@stphung
stphung / Magicka.java
Created May 8, 2011 18:23
Google Code Jam 2011 Qualifier Round - Problem B, Magicka
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
@stphung
stphung / BotTrust.java
Created May 8, 2011 18:16
Google Code Jam 2011 Qualifier Round - Problem A, Bot Trust
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Queue;
import java.util.Scanner;
import java.util.concurrent.LinkedBlockingQueue;
public class BotTrust {
public static void main(String[] args) throws FileNotFoundException {
Scanner sc = new Scanner(new File("A-large.in"));
@stphung
stphung / PerfectSequences.java
Created May 3, 2011 12:42
TopCoder SRM 505 Division 2 - 500 point problem
public class PerfectSequences {
public String fixIt(int[] seq) {
if (seq.length == 1) return "Yes";
else {
for(int i=0; i<seq.length; i++) {
int sum = sum(seq,i);
int product = product(seq,i);
if (product == 0) continue;
else {