This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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){ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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++; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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){ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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]; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); |