You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
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.
//C++: TLEclassSolution{public:intmaxCoins(vector<int>& nums){int n = nums.size();if(n==0)return0;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;}};classSolution{public:intmaxCoins(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];}};
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.
//C++:
classNumMatrix {
vector<vector<int>> mt;
public:
NumMatrix(vector<vector<int>>&matrix) {
int m = matrix.size();
if(m==0) return;
int n = matrix[0].size();
if(n==0) return;
mt.resize(m+1, vector<int>(n+1, 0));
for(int i=1; i<=m; ++i){
for(int j=1; j<=n; ++j){
mt[i][j] += matrix[i-1][j-1] + mt[i-1][j] + mt[i][j-1] - mt[i-1][j-1];
}
}
}
int sumRegion(int row1, int col1, int row2, int col2) {
return mt[row2+1][col2+1] - mt[row2+1][col1] - mt[row1][col2+1] + mt[row1][col1];
}
};
//Java
classNumMatrix {
int[][] extMatrix;
public NumMatrix(int[][] matrix) {
int m = matrix.length;
if(m==0) return;
int n = matrix[0].length;
if(n==0) return;
extMatrix = new int[m+1][n+1];
for(int i=1; i<=m; ++i){
for(int j=1; j<=n; ++j){
extMatrix[i][j] = extMatrix[i-1][j]+extMatrix[i][j-1]+matrix[i-1][j-1]-extMatrix[i-1][j-1];
}
}
}
public int sumRegion(int row1, int col1, int row2, int col2) {
int r = extMatrix[row2+1][col2+1]-extMatrix[row2+1][col1]-extMatrix[row1][col2+1]+extMatrix[row1][col1];
return r;
}
}
/*** Your NumMatrix object will be instantiated and called as such:
* NumMatrix obj = new NumMatrix(matrix);
*int param_1 = obj.sumRegion(row1,col1,row2,col2);
*/
//Java, BIT
classNumArray {
int[] arr;
int[] tree;
int m;
public NumArray(int[] nums) {
m = nums.length;
arr = new int[m];
tree = new int[m+1];
for(int i=0; i<m; ++i){
update(i, nums[i]);
}
}
//index for arr
public void update(int k, int val) {
int d = val-arr[k];
arr[k] = val;
for(int i=k+1; i<=m; i+=i&(-i)){
tree[i]+=d;
}
}
//index for tree
public intsum(int k){
int s =0;
for(int i=k; i>0; i-=i&(-i)){
s += tree[i];
}
return s;
}
public int sumRange(int i, int j) {
returnsum(j+1)-sum(i);
}
}
/*** 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);
*/
//C++classNumArray {
vector<int>sum;
public:
NumArray(vector<int>&nums) {
for(auto n:nums){
sum.push_back(n+(sum.empty()?0:sum.back()));
}
}
int sumRange(int i, int j) {
returnsum[j]-(i==0?0:sum[i-1]);
}
};
// Your NumArray object will be instantiated and called as such:
// NumArray numArray(nums);
// numArray.sumRange(0, 1);
// numArray.sumRange(1, 2);
//Java
classNumArray {
int[] nums;
public NumArray(int[] nums) {
int s =0;
this.nums = Arrays.copyOf(nums, nums.length);
for(int i=0; i<nums.length; ++i){
s += nums[i];
this.nums[i] = s;
}
}
public int sumRange(int i, int j) {
return this.nums[j] - (i-1>=0?this.nums[i-1]:0);
}
}
/*** Your NumArray object will be instantiated and called as such:
* NumArray obj = new NumArray(nums);
*int param_1 = obj.sumRange(i,j);
*/
classSolution {
public:int lengthOfLIS(vector<int>& nums) {
int n = nums.size();
if(n==0) return0;
vector<int> dp(n, 1);
int ret =1;
for(int i=0; i<n; ++i){
for(int j=0; j<i; ++j){
if(nums[i]>nums[j]) dp[i] = max(dp[i], dp[j]+1);
}
ret = max(ret, dp[i]);
}
return ret;
}
};
//https://www.geeksforgeeks.org/longest-monotonically-increasing-subsequence-size-n-log-n/classSolution1 {
public:int lengthOfLIS(vector<int>& nums) {
//al[L-1] is the last element of the min L-length sequence//for each number n in nums, find the first element al[L-1] in al greater (or equal) than n//replace al[L-1] by n (so we can keep the min property of the L-length subsequence)
vector<int> al;
for(auto n:nums){
if(al.size()==0|| n>al.back()) al.push_back(n);
else{
int l =0, r = al.size()-1;
while(l<=r){
int m = l+(r-l)/2;
if(n<=al[m] && (m==l || al[m-1]<n)){
al[m] = n;
break;
}elseif(n<al[m]){
r = m-1;
}else{
l = m+1;
}
}
}
}
return al.size();
}
};
classSolution2 {
public:int lengthOfLIS(vector<int>& nums) {
vector<int> al;
for(auto n:nums){
auto it = lower_bound(al.begin(), al.end(), n);
if(it==al.end()) al.push_back(n);
else*it = n;
}
return al.size();
}
};