LeetCode - Max Points on a Line Solution
Category: Data Structure & Algorithm
Leetcode Lintcode answers/solutions
LeetCode – Matrix Zigzag Traversal
Java Solution public int[] printZMatrix(int[][] matrix) { if (matrix == null || matrix.length == 0 || matrix[0] == null || matrix[0].length == 0) { return new int[0]; } int row = matrix.length; int col = matrix[0].length; int[] res = new int[row * col]; int lvl = 0; int next = 0; while (next = 0 … Continue reading LeetCode – Matrix Zigzag Traversal
Binary Indexed Tree
Binary indexed tree is a kind of data that use mostly to handle questions like "get the sum from indexes x to y of array"; It pre-processes all data in another array for query purpose. One of the key functions is to get the lowest binary "1" bit. (lowBit())For example: given number "15", which in binary is "1111",lowBit … Continue reading Binary Indexed Tree
Storing a two-integer element (coordinate, result set)?
I did a little test today about how to store a two-integer element like coordinate or global/local result set. The most compact way, which I prefer, is to use a long to store the two results. public static void main (String[] args) { long point = 0L; long lowMask = 0x00000000FFFFFFFF; int x = -7; int … Continue reading Storing a two-integer element (coordinate, result set)?
LeetCode – Palindrome Partitioning II
The first thing that comes up is that we should be able to solve this problem using DFS. For each recursion we do: 1.From designated start point, we scan from len - 1 down to start point to see if s.substring(i, j) is palindrome. 2.If so, we do dfs(s, j + 1), record the minimum … Continue reading LeetCode – Palindrome Partitioning II
LeetCode – Best Time to Buy and Sell Stock IV Answers/Solution/Thoughts
This is the generic version of following problems Best Time to Buy and Sell Stock III (two transactions) Best Time to Buy and Sell Stock II (unlimited transactions) Best Time to Buy and Sell Stock (one transaction) So there are two dimensions to be considered: the number of transactions and the profit up to a given point of … Continue reading LeetCode – Best Time to Buy and Sell Stock IV Answers/Solution/Thoughts
