Skip to content

Instantly share code, notes, and snippets.

View monkeylyf's full-sized avatar

Yifeng Liu monkeylyf

  • Google
  • Bay area
View GitHub Profile
public static void insertionSort(int[] ar) {
if (ar == null || ar.length == 0) {
return;
}
int tmp = ar[ar.length - 1];
int i = ar.length - 2;
while (i >= 0 && ar[i] > tmp) {
ar[i + 1] = ar[i];
i -= 1;
printArray(ar);
import java.math.BigInteger;
public class Lego_Blocks {
public static void main(String[] args) {
lego(new int[][] {{2, 2}, {3, 2}, {2, 3}, {4, 4}, {1, 10}, {10, 10}, {20, 20}});
}
public static void lego(int[][] arr) {
boolean debug = true;
int width, height;
BigInteger[] P, N;
@monkeylyf
monkeylyf / gist:5258196
Created March 27, 2013 21:30
Longest_Product_Subarray
public class Longest_Product_Subarray {
/**
* Given a double array, find the longest sub-array, whose product is the
* largest. E.g., given {-2.5, 4, 0, 3, 0.5, 8, -1}, the longest sub-array
* with max product is {3, 0.5, 8}
*
* @param args
*/
public static void main(String[] args) {
@monkeylyf
monkeylyf / gist:5281435
Created March 31, 2013 17:55
Racquetball simulation
import java.util.*;
public class Raquetball {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int totalTrial = 1000;
@monkeylyf
monkeylyf / gist:5379110
Created April 13, 2013 16:36
fair and square small
import java.util.*;
public class Fair_and_Square {
public static void main(String[] args) {
solve(1, 4, 1);
solve(10, 120, 1);
solve(100, 1000, 1);
}
@monkeylyf
monkeylyf / gist:5380396
Last active December 16, 2015 04:49
我是傻逼
import java.util.*;
class Treasure {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int T, t, K, k, N, n;
ArrayList<ArrayList<Integer>> chests;
int[] keys;
T = sc.nextInt();
for (t = 1; t <= T; ++t) {
@monkeylyf
monkeylyf / gist:5427059
Created April 20, 2013 19:20
poj_Banana.py
class Cell:
def __init__(self, x, y, index):
self.x = x
self.y = y
self.index = index
def __repr__(self):
return repr((self.x, self.y))
@monkeylyf
monkeylyf / gist:5447661
Created April 23, 2013 21:44
codegen.py
def parse(data):
'Parse a variable length record format'
i = 0
while i < len(data):
kind = data[i]
i += 1
if 0:
pass
elif kind == 'p':
lastname = str(data[i:i+8]).rstrip()
@monkeylyf
monkeylyf / gist:5473908
Created April 27, 2013 17:38
Mange the Energy
public static void solve(int[] arr, int max, int gain, int n) {
int[] dp = new int[max - gain + 1], next;
int i, j, k, localMax;
for (i = 0; i <= max - gain; ++i) {
dp[i] = (max - i) * arr[0];
}
//printArray(dp);
for (i = 1; i < n; ++i) {
next = new int[max - gain + 1];
for (j = 0; j <= max - gain; ++j) {
@monkeylyf
monkeylyf / gist:5503338
Created May 2, 2013 16:18
DominoChecker
from sets import Set
class DominoChecker:
def __init__(self):
self.container = Set()
def addBox(self, str):
arr = [char for char in str]