Tuesday, December 8, 2015
Sunday, December 6, 2015
LeetCode [312] Burst Balloons
Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented by array nums. You are asked to burst all the balloons. If the you burst balloon i you will get nums[left] * nums[i] * nums[right] coins. Here left and right are adjacent indices of i. After the burst, the left and right then becomes adjacent.
Find the maximum coins you can collect by bursting the balloons wisely.
Note:
- You may imagine
nums[-1] = nums[n] = 1. They are not real therefore you can not burst them. - 0 ≤
n≤ 500, 0 ≤nums[i]≤ 100
Example:
Input:[3,1,5,8]Output:167 Explanation:nums = [3,1,5,8] --> [3,5,8] --> [3,8] --> [8] --> [] coins = 3*1*5 + 3*5*8 + 1*3*8 + 1*8*1 = 167
=============
Eg.nums = 3 1 5 8
0 1 2 3 4 5
after extended nums = 1 3 1 5 8 1
bottom up
0 1 2 3 4 5
1 3 1 5 8 1
step 1: burst balloon 2 => 3*1*5 = 15 => dp[2][2] = dp[2][1]+dp[3][2]+15 = 15
0 1
1 3
step 2: burst balloon 3 => 3*5*8 = 120 => dp[2][3] = dp[2][2]+dp[4][3]+120 = 135
0 1
1 3
step 1: burst balloon 1 => 1*3*8= 24 => dp[1][3] = dp[1][0]+dp[2][3]+24 = 159
0
1
step 1: burst balloon 4 => 1*8*1 = 8 => dp[1][4] = dp[1][3]+dp[5][4]+8 = 167
Ref
[1] https://leetcode.com/problems/burst-balloons/
[2] https://leetcode.com/discuss/72186/c-dynamic-programming-o-n-3-32-ms-with-comments
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | //C++: TLE class Solution { public: int maxCoins(vector<int>& nums) { int n = nums.size(); if(n==0) return 0; int ret = 0; for(int i=0; i<n; ++i){ int tmp = (i==0?1:nums[i-1])*nums[i]*(i==n-1?1:nums[i+1]); vector<int> nums1 = nums; nums1.erase(nums1.begin()+i); tmp += maxCoins(nums1); ret = max(ret, tmp); } return ret; } }; class Solution { public: int maxCoins(vector<int>& nums) { int n = nums.size(); nums.insert(nums.begin(), 1); nums.insert(nums.end(), 1); //can be further optimized by removing all zeros //dp[s][e] is max coins by bursting all balloons from s to e vector<vector<int>> dp(n+2, vector<int>(n+2, 0)); for(int s = n; s>0; --s){ for(int e = s; e<=n; ++e){ int bestCoins = 0; for(int i = s; i<=e; ++i){ //coins is the max coins when balloon i is the last balloon int coins = dp[s][i-1] + dp[i+1][e] + nums[s-1]*nums[i]*nums[e+1]; bestCoins = max(bestCoins, coins); } dp[s][e] = bestCoins; } } return dp[1][n]; } }; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | class Solution { public int maxCoins(int[] nums) { int n = nums.length; int[] numsExt = new int[n+2]; Arrays.fill(numsExt, 1); for(int i=1; i<=n; ++i) numsExt[i] = nums[i-1]; int[][] dp = new int[n+2][n+2]; for(int s=n; s>0; --s){ for(int e = s; e<=n; ++e){ int bestCoins = 0; for(int i=s; i<=e; ++i){ int coins = dp[s][i-1] + dp[i+1][e] + numsExt[s-1]*numsExt[i]*numsExt[e+1]; bestCoins = Math.max(bestCoins, coins); } dp[s][e] = bestCoins; } } return dp[1][n]; } } |
Friday, December 4, 2015
LeetCode [311] Sparse Matrix Multiplication
Input: A = [ [ 1, 0, 0], [-1, 0, 3] ] B = [ [ 7, 0, 0 ], [ 0, 0, 0 ], [ 0, 0, 1 ] ] Output: | 1 0 0 | | 7 0 0 | | 7 0 0 | AB = | -1 0 3 | x | 0 0 0 | = | -7 0 3 | | 0 0 1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | class Solution { public: vector<vector<int>> multiply(vector<vector<int>>& A, vector<vector<int>>& B) { unordered_map<int, unordered_set<int>> A1, B1; int ma = A.size(), na = A[0].size(); int mb = B.size(), nb = B[0].size(); for(int i=0; i<ma; ++i){ for(int j=0; j<na; ++j){ if(A[i][j]){ A1[i].insert(j); } } } for(int j=0; j<nb; ++j){ for(int i=0; i<mb; ++i){ if(B[i][j]){ B1[j].insert(i); } } } vector<vector<int>> ret(ma, vector<int>(nb, 0)); for(int i=0; i<ma; ++i){ for(int j=0; j<nb; ++j){ int s = 0; if(A1.count(i) && B1.count(j)){ for(auto k:A1[i]){ if(B1[j].count(k)){ s += A[i][k]*B[k][j]; } } } ret[i][j] = s; } } return ret; } }; |
//Java class Solution { public int[][] multiply(int[][] A, int[][] B) { int am = A.length, an = A[0].length; int bm = B.length, bn = B[0].length; List<Map<Integer, Integer>> mapA = new ArrayList<Map<Integer, Integer>>(am); List<Map<Integer, Integer>> mapB = new ArrayList<Map<Integer, Integer>>(bn); for(int i=0; i<am; ++i){ mapA.add(new HashMap<>()); for(int j=0; j<an; ++j){ if(A[i][j]!=0){ mapA.get(i).put(j, A[i][j]); } } } for(int j=0; j<bn; ++j){ mapB.add(new HashMap<>()); for(int i=0; i<bm; ++i){ if(B[i][j]!=0){ mapB.get(j).put(i, B[i][j]); } } } int[][] ret = new int[am][bn]; for(int i=0; i<am; ++i){ for(int j=0; j<bn; ++j){ int s = 0; for(Map.Entry<Integer, Integer> e : mapA.get(i).entrySet()){ int k = e.getKey(), v = e.getValue(); if(mapB.get(j).containsKey(k)){ s += v*mapB.get(j).get(k); } } ret[i][j] = s; } } return ret; } }
Saturday, November 28, 2015
LeetCode [310] Minimum Height Trees
[1] https://leetcode.com/problems/minimum-height-trees/
OJ
[2] https://leetcode.com/discuss/71656/c-solution-o-n-time-o-n-space
Tuesday, November 24, 2015
LeetCode [309] Best Time to Buy and Sell Stock with Cooldown
Monday, November 23, 2015
LeetCode [308] Range Sum Query 2D - Mutable
308. Range Sum Query 2D - Mutable
Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper left corner (row1, col1) and lower right corner (row2, col2).

The above rectangle (with the red border) is defined by (row1, col1) = (2, 1) and (row2, col2) = (4, 3), which contains sum = 8.
Example:
Given matrix = [ [3, 0, 1, 4, 2], [5, 6, 3, 2, 1], [1, 2, 0, 1, 5], [4, 1, 0, 1, 7], [1, 0, 3, 0, 5] ] sumRegion(2, 1, 4, 3) -> 8 update(3, 2, 2) sumRegion(2, 1, 4, 3) -> 10
Note:
- The matrix is only modifiable by the update function.
- You may assume the number of calls to update and sumRegion function is distributed evenly.
- You may assume that row1 ≤ row2 and col1 ≤ col2.
//C++, Segment Tree //C++: 784ms class STNode{ public: int sum; int ss, se; STNode *left, *right; STNode(vector<int> &row, int s, int e){ sum = 0; ss = s; se = e; left = NULL; right = NULL; if(s==e){ sum = row[s]; }else{ int m = (s+e)/2; left = new STNode(row, s, m); right = new STNode(row, m+1, e); sum = left->sum+right->sum; } } }; class NumMatrix { int nRows; vector<STNode *> roots; void updateST(STNode* root, int j, int val){ if(!root || j<root->ss || j>root->se) return; else if(j==root->ss && j==root->se) root->sum = val; else{ updateST(root->left, j, val); updateST(root->right, j, val); root->sum = (root->left?root->left->sum:0) + (root->right?root->right->sum:0); } } int sumRange(STNode* root, int i, int j){ if(!root) return 0; else if(i>root->se || j<root->ss) return 0; else if(i<=root->ss && j>=root->se) return root->sum; else return sumRange(root->left, i, j)+sumRange(root->right, i, j); } public: NumMatrix(vector<vector<int>> &matrix) { nRows = matrix.size(); roots.resize(nRows); for(int i=0; i<nRows; ++i){ roots[i] = new STNode(matrix[i], 0, (int)matrix[i].size()-1); } } void update(int row, int col, int val) { updateST(roots[row], col, val); } int sumRegion(int row1, int col1, int row2, int col2) { int ret = 0; for(int i=row1; i<=row2; ++i){ ret += sumRange(roots[i], col1, col2); } return ret; } }; //Java, Segment Tree class SGTree{ int sum, start, end; SGTree left, right; SGTree(int[] nums, int l, int r){ start = l; end = r; if(start==end){ sum = nums[l]; }else{ int m = (l+r)/2; left = new SGTree(nums, l, m); right = new SGTree(nums, m+1, r); sum = left.sum + right.sum; } } }; class NumMatrix { int[] nums; SGTree root; int m, n; public NumMatrix(int[][] matrix) { m = matrix.length; if(m==0) return; n = matrix[0].length; if(n==0) return; nums = new int[m*n]; for(int i=0; i<m; ++i){ for(int j=0; j<n; ++j){ nums[i*n+j] = matrix[i][j]; } } root = new SGTree(nums, 0, nums.length-1); } void updateST(SGTree node, int i, int v){ if(node==null || i<node.start || i>node.end) return; if(node.start==i && node.end==i) node.sum = v; else{ updateST(node.left, i, v); updateST(node.right, i, v); node.sum = node.left.sum+node.right.sum; } } int sumST(SGTree node, int l, int r){ if(node==null) return 0; if(r<node.start || l>node.end) return 0; if(l<=node.start && node.end<=r) return node.sum; int s = sumST(node.left, l, r) + sumST(node.right, l, r); return s; } public void update(int row, int col, int val) { updateST(root, row*n+col,val); } public int sumRegion(int row1, int col1, int row2, int col2) { int s = 0; for(int i=row1; i<=row2; ++i){ s += sumST(root, i*n+col1, i*n+col2); } return s; } }; /** * Your NumMatrix object will be instantiated and called as such: * NumMatrix obj = new NumMatrix(matrix); * obj.update(row,col,val); * int param_2 = obj.sumRegion(row1,col1,row2,col2); */ //Java, Binary Indexed Tree class NumMatrix { int[][] nums; int[][] tree; int m, n; public NumMatrix(int[][] matrix) { m = matrix.length; if(m==0) return; n = matrix[0].length; tree = new int[m+1][n+1]; nums = new int[m][n]; for(int i=0; i<m; ++i){ for(int j=0; j<n; ++j){ update(i, j, matrix[i][j]); } } } public void update(int row, int col, int val) { int d = val-nums[row][col]; nums[row][col] = val; for(int i=row+1; i<=m; i+=i&(-i)){ for(int j=col+1; j<=n; j+=j&(-j)){ tree[i][j] += d; } } } //row and col are corrordinates for tree public int sum(int row, int col){ int s = 0; for(int i=row; i>0; i-=i&(-i)){ for(int j=col; j>0; j-=j&(-j)){ s += tree[i][j]; } } return s; } public int sumRegion(int row1, int col1, int row2, int col2) { return sum(row2+1, col2+1)-sum(row2+1,col1)-sum(row1, col2+1)+sum(row1, col1); } } /** * Your NumMatrix object will be instantiated and called as such: * NumMatrix obj = new NumMatrix(matrix); * obj.update(row,col,val); * int param_2 = obj.sumRegion(row1,col1,row2,col2); */
Wednesday, November 18, 2015
LeetCode [307] Range Sum Query - Mutable
307. Range Sum Query - Mutable
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive.
The update(i, val) function modifies nums by updating the element at index i to val.
Example:
Given nums = [1, 3, 5] sumRange(0, 2) -> 9 update(1, 2) sumRange(0, 2) -> 8
Constraints:
- The array is only modifiable by the update function.
- You may assume the number of calls to update and sumRange function is distributed evenly.
0 <= i <= j <= nums.length - 1
//C++ //C++: 1720ms class STNode{ public: int sum; int ss; int se; STNode *left; STNode *right; STNode(vector<int> &nums, int s, int e){ sum = 0; ss = s; se = e; left = NULL; right = NULL; if(s==e){ sum = nums[s]; }else if(s<e){ int m = (s+e)/2; left = new STNode(nums, s, m); right = new STNode(nums, m+1, e); sum = left->sum + right->sum; } } }; class NumArray { STNode * root; void updateST(STNode* root, int i, int val){ if(!root || i<root->ss || i>root->se){ return; }if(i==root->ss && i==root->se){ root->sum = val; }else{ updateST(root->left, i, val); updateST(root->right, i, val); root->sum = (root->left?root->left->sum:0) + (root->right?root->right->sum:0); } } int sumRange(STNode* root, int i, int j){ if(!root) return 0; else if(i>root->se || j<root->ss) return 0; else if(i<=root->ss && j>=root->se) return root->sum; else return sumRange(root->left, i, j)+sumRange(root->right, i, j); } public: NumArray(vector<int> &nums) { int n = nums.size(); root = new STNode(nums, 0, n-1); } void update(int i, int val) { updateST(root, i, val); } int sumRange(int i, int j) { return sumRange(root, i, j); } }; //Java class SGTree{ int sum; int start; int end; SGTree left; SGTree right; SGTree(int[] nums, int l, int r){ start = l; end = r; if(start==end){ sum = nums[l]; }else{ int m = (l+r)/2; left = new SGTree(nums, l, m); right = new SGTree(nums, m+1, r); sum = left.sum + right.sum; } } }; public class NumArray { SGTree sgTree; public NumArray(int[] nums) { if(nums.length>0) sgTree = new SGTree(nums, 0, nums.length-1); } void updateST(SGTree node, int i, int v){ if(node==null || i<node.start || i>node.end) return; if(i==node.start && i==node.end) node.sum = v; else{ updateST(node.left, i, v); updateST(node.right, i, v); node.sum = node.left.sum + node.right.sum; } } int sumST(SGTree node, int i, int j){ if(node==null || j<node.start || i>node.end) return 0; if(i<=node.start && node.end<=j) return node.sum; return sumST(node.left, i, j)+sumST(node.right, i, j); } public void update(int i, int val) { updateST(sgTree, i, val); } public int sumRange(int i, int j) { return sumST(sgTree, i, j); } } /** * Your NumArray object will be instantiated and called as such: * NumArray obj = new NumArray(nums); * obj.update(i,val); * int param_2 = obj.sumRange(i,j); */
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | class NumArray { int n; int[] bit; int[] arr; int getNext(int i){ return i+(i&(-i)); } int getParent(int i){ return i-(i&(-i)); } public NumArray(int[] nums) { n = nums.length; bit = new int[n+1]; arr = new int[n]; for(int i=0; i<n; ++i){ update(i, nums[i]); } } public void update(int index, int val) { int diff = val-arr[index]; arr[index] = val; int p = index+1; while(p<=n){ bit[p] += diff; p = getNext(p); } } int getPreSum(int index){ int sum = 0; int p = index+1; while(p>0){ sum += bit[p]; p = getParent(p); } return sum; } public int sumRange(int left, int right) { return getPreSum(right)-getPreSum(left-1); } } /** * Your NumArray object will be instantiated and called as such: * NumArray obj = new NumArray(nums); * obj.update(index,val); * int param_2 = obj.sumRange(left,right); */ |