Skip to content

Instantly share code, notes, and snippets.

@cangoal
cangoal / PaintHouse.java
Last active April 21, 2016 04:59
LeetCode - Paint Hourse
// There are a row of n houses, each house can be painted with one of the three colors: red, blue or green. The cost of painting each house with a certain color is different. You have to paint all the houses such that no two adjacent houses have the same color.
// The cost of painting each house with a certain color is represented by a n x 3 cost matrix. For example, costs[0][0] is the cost of painting house 0 with color red; costs[1][2] is the cost of painting house 1 with color green, and so on... Find the minimum cost to paint all houses.
// Note:
// All costs are positive integers.
public int minCost(int[][] costs) {
// Write your code here
if(costs.length == 0 || costs[0].length == 0) return 0;
@cangoal
cangoal / CountofSmallerNumberbeforeitself.java
Last active April 12, 2016 01:47
LintCode - Count of Smaller Number before itself
public class Solution {
/**
* @param A: An integer array
* @return: Count the number of element before this element 'ai' is
* smaller than it and return count number array
*/
class SegmentTreeNode{
int start, end, count;
SegmentTreeNode left, right;
public SegmentTreeNode(int start, int end, int count){
@cangoal
cangoal / CountofSmallerNumber.java
Last active April 12, 2016 01:33
LintCode - Count of Smaller Number
// solution 1 --- brute force
public ArrayList<Integer> countOfSmallerNumber(int[] A, int[] queries) {
// write your code here
ArrayList<Integer> res = new ArrayList<Integer>();
if(A == null || queries == null || queries.length == 0)
return res;
for(int query : queries){
int count = 0;
for(int num : A){
if(num < query) count++;
@cangoal
cangoal / SuperUglyNumber.java
Created April 11, 2016 03:52
LeetCode - Super Ugly Number
public int nthSuperUglyNumber(int n, int[] primes) {
if(n <= 0 || primes.length == 0) return 0;
int[] uglyNumbers = new int[n];
int[] indexes = new int[primes.length];
int[] factors = new int[primes.length];
for(int i=0; i<primes.length; i++){
factors[i] = primes[i];
}
uglyNumbers[0] = 1;
@cangoal
cangoal / Heapify.java
Created April 10, 2016 05:09
LintCode - Heapify
public void heapify(int[] A) {
// write your code here
if(A == null || A.length == 0) return;
for(int i=A.length/2 - 1; i >= 0; i--){
int k = i;
while(true){
int child_1 = 2 * k + 1, child_2 = 2 * k + 2;
if(child_1 >= A.length) break;
else if(child_2 >= A.length){
@cangoal
cangoal / NumberofAirplanesintheSky.java
Created April 9, 2016 22:04
LintCode - Number of Airplanes in the Sky
// solution 1
public int countOfAirplanes(List<Interval> airplanes) {
// write your code here
int count = 0, max = 0;
if(airplanes == null || airplanes.size() == 0) return max;
Set<Integer> set = new TreeSet<Integer>();
List<Integer> start = new ArrayList<Integer>();
List<Integer> end = new ArrayList<Integer>();
for(Interval itv : airplanes){
set.add(itv.start);
@cangoal
cangoal / PaintFence.java
Last active April 14, 2016 15:00
LeetCode - Paint Fence
// There is a fence with n posts, each post can be painted with one of the k colors.
// You have to paint all the posts such that no more than two adjacent fence posts have the same color.
// Return the total number of ways you can paint the fence.
// Note:
// n and k are non-negative integers.
public int numWays(int n, int k) {
@cangoal
cangoal / GraphValidTree.java
Last active April 14, 2016 17:27
LeetCode - Graph Valid Tree
// Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), write a function to check whether these edges make up a valid tree.
// For example:
// Given n = 5 and edges = [[0, 1], [0, 2], [0, 3], [1, 4]], return true.
// Given n = 5 and edges = [[0, 1], [1, 2], [2, 3], [1, 3], [1, 4]], return false.
// Hint:
// 1. Given n = 5 and edges = [[0, 1], [1, 2], [3, 4]], what should your return? Is this case a valid tree?
// 2. According to the definition of tree on Wikipedia: “a tree is an undirected graph in which any two vertices are connected by exactly one path. In other words, any connected graph without simple cycles is a tree.”
// Note: you can assume that no duplicate edges will appear in edges. Since all edges are undirected, [0, 1] is the same as [1, 0] and thus will not appear together inedges.
@cangoal
cangoal / RangeSumQuery2D-Immutable.java
Created March 25, 2016 19:20
LeetCode - Range Sum Query 2D - Immutable
public class NumMatrix {
int[][] Sum;
public NumMatrix(int[][] matrix) {
if(matrix != null && matrix.length != 0 && matrix[0].length != 0){
Sum = new int[matrix.length+1][matrix[0].length+1];
for(int i=matrix.length-1; i>=0; i--){
for(int j=matrix[0].length -1; j >= 0; j--){
Sum[i][j] = Sum[i][j+1] + Sum[i+1][j] + matrix[i][j] - Sum[i+1][j+1];
}
}
@cangoal
cangoal / SerializeandDeserializeBinaryTree.java
Created March 21, 2016 19:42
LeetCode - Serialize and Deserialize Binary Tree
public class Codec {
// Encodes a tree to a single string.
public String serialize(TreeNode root) {
if(root == null) return null;
Stack<TreeNode> stack = new Stack<TreeNode>();
StringBuilder sb = new StringBuilder();
stack.push(root);
while(!stack.isEmpty()){
TreeNode cur = stack.pop();