#include
using namespace std;
const int modMin = 1e9 + 7;
int main()
{
int dp[55][1005] = { 0 };//加上这句比较好
dp[0][0] = 1;
int n, k;
while (cin >> n >> k)
{
for (int i = 0; ifor (int j = k; j >= 1; j--)
{
for (int s = 0; s1][(n + s - i) % n]) % modMin;
}
}
}
cout << dp[k][0] << endl;
}
return 0;
}
Find out how many ways to assign symbols to make sum of integers equal to target S.
Example 1:
Input: nums is [1, 1, 1, 1, 1], S is 3.
Output: 5
Explanation:
-1+1+1+1+1 = 3
+1-1+1+1+1 = 3
+1+1-1+1+1 = 3
+1+1+1-1+1 = 3
+1+1+1+1-1 = 3
There are 5 ways to assign symbols to make the sum of nums be target 3.
**思路:先转化成和为sum,有多少中可能的序列的问题,然后规划。
代码:**
class Solution {
public:
int findTargetSumWays(vector<int>& nums, int s) {
int sum = accumulate(nums.begin(), nums.end(), 0);
return sum < s || (s + sum) & 1 ? 0 : subsetSum(nums, (s + sum) >> 1);
}
int subsetSum(vector<int>& nums, int s) {
int dp[s + 1] = { 0 };
dp[0] = 1;//重要
for (int n : nums)
for (int i = s; i >= n; i--)
dp[i] += dp[i - n];
return dp[s];
}
};
Example 1:
Input:
“bbbab”
Output:
4
One possible longest palindromic subsequence is “bbbb”.
Example 2:
Input:
“cbbd”
Output:
2
这道题的意思:比如一个字符串”axbcdxa”,最长回文是”axbxa”,答案是5。
**思路:如代码
代码:**
class Solution {
public:
int longestPalindromeSubseq(string s) {
int n=s.length();
vector<vector<int>> dp(n,vector<int>(n));
for(int i=n-1;i>=0;i--)//这种遍历方式更好。
{
for(int j=i;jif(i==j)
dp[i][j]=1;
else if(i+1==j)
{
if(s[i]==s[j])
dp[i][j]=2;
else
dp[i][j]=1;
}
else
{
if(s[i]==s[j])
dp[i][j]=max(max(dp[i+1][j],dp[i][j-1]),dp[i+1][j-1]+2);
else
dp[i][j]=max(max(dp[i+1][j],dp[i][j-1]),dp[i+1][j-1]);
}
}
}
return dp[0].back();
}
};
4.Predict the Winner
Given an array of scores that are non-negative integers. Player 1 picks one of the numbers from either end of the array followed by the player 2 and then player 1 and so on. Each time a player picks a number, that number will not be available for the next player. This continues until all the scores have been chosen. The player with the maximum score wins.
Given an array of scores, predict whether player 1 is the winner. You can assume each player plays to maximize his score.
Example 1:
Input: [1, 5, 2]
Output: False
Explanation: Initially, player 1 can choose between 1 and 2.
If he chooses 2 (or 1), then player 2 can choose from 1 (or 2) and 5. If player 2 chooses 5, then player 1 will be left with 1 (or 2).
So, final score of player 1 is 1 + 2 = 3, and player 2 is 5.
Hence, player 1 will never be the winner and you need to return False.
Example 2:
Input: [1, 5, 233, 7]
Output: True
Explanation: Player 1 first chooses 1. Then player 2 have to choose between 5 and 7. No matter which number player 2 choose, player 1 can choose 233.
Finally, player 1 has more score (234) than player 2 (12), so you need to return True representing player1 can win.
**思路:dp[i][j]代表在i-j个数中,第一个人能取到的最大和是多少,则
若第一人取左边的数,能得到的最大值为left=sum(i-j)-dp[i+1][j];
若第一人取右边的数,能得到的最大值为right=sum(i-j)-dp[i][j-1];
所以dp[i][j]=max(dp[i+1][j],dp[i][j-1])。
最后判定一下第一个人取到的最大数,能够大于第二人即可。
代码:**
class Solution {
public:
bool PredictTheWinner(vector<int>& nums) {
int n=nums.size();
if(n==1)
return true;
vector<vector<int>> dp(nums.size(),vector<int> (nums.size()));
vector<int> ssum(n+1);
ssum[0]=0;
for(int i=0;i1]+=ssum[i]+nums[i];
}
for(int i=n-1;i>=0;i--)
{
for(int j=i;jif(i==j)
dp[i][j]=nums[i];
else if(j-i==1)
{
dp[i][j]=max(nums[i],nums[j]);
}
else
{
int left=nums[i]+ssum[j+1]-ssum[i+1]-dp[i+1][j];
int right=nums[j]+ssum[j]-ssum[i]-dp[i][j-1];
dp[i][j]=max(left,right);
}
}
}
return dp[0][n-1]>=(ssum[n]-dp[0][n-1]);
}
};
Note:
Each of the array element will not exceed 100.
The array size will not exceed 200.
Example 1:
Input: [1, 5, 11, 5]
Output: true
Explanation: The array can be partitioned as [1, 5, 5] and [11].
Example 2:
Input: [1, 2, 3, 5]
Output: false
Explanation: The array cannot be partitioned into equal sum subsets.
**思路:这题思路前所未闻!首先要#include,创建一个比特串。然后利用bits |=bits<
class Solution {
public:
bool canPartition(vector<int>& nums) {
bitset<200*100> bits(1);
int ans=0;
for(auto i : nums)
{
ans+=i;
bits |= bits<return !(ans%2) && bits[ans>>1];
}
};
class Solution {
public:
bool isSubsequence(string s, string t) {
string::iterator it=s.begin();
for(char c:t)
{
it+= (*it==c);
}
return it==s.end();
}
};
What is the maximum number of envelopes can you Russian doll? (put one inside other)
Example:
Given envelopes = [[5,4],[6,4],[6,7],[2,3]], the maximum number of envelopes you can Russian doll is 3 ([2,3] => [5,4] => [6,7]).
**思路:先排序,再O(n^2)的去遍历,规划。
代码:**
bool cmp(pair<int,int> i,pair<int, int> j)
{
if(i.first==j.first)
return i.secondreturn i.firstclass Solution {
public:
int maxEnvelopes(vectorint , int>>& envelopes) {
sort(envelopes.begin(),envelopes.end(),cmp);
int n=envelopes.size();
int mx=n==0?0:1;
vector<int> dp(n,1);
for(int i=0;ifor(int j=0;jif(envelopes[i].first>envelopes[j].first && envelopes[i].second>envelopes[j].second)
{
dp[i]=max(dp[j]+1,dp[i]);
}
mx=dp[i]>mx?dp[i]:mx;
}
}
return mx;
}
};
class Solution {
public:
int rob(vector<int>& nums) {
int n=nums.size();
if(n==0)
return 0;
vector<int> dp(n);
for(int i=0;iif(i==0)
dp[i]=nums[i];
else if(i==1)
dp[i]=max(nums[i-1],nums[i]);
else
{
dp[i]=max(dp[i-1],dp[i-2]+nums[i]);
}
}
return dp[n-1];
}
};
#include
#include
#include
#include
#include
#include
using namespace std;
int main(){
int n;cin>>n;
vector<int> A;
for(int i=0;iint tmp;cin>>tmp;
A.push_back(tmp);
}
int K,d;cin>>K>>d;
long long int maxV=-99999999;
long long int fmax[11][51],fmin[11][51];//二阶
for(int i=0;i<=K;i++){
for(int j=0;j<=n;j++)
{
fmax[i][j]=0,fmin[i][j]=0;
}
}
for(int i=0;i<=n;i++){
fmax[1][i]=A[i];fmin[1][i]=A[i];
for(int k=2;k<=min(K,i+1);k++){//几个数参与相乘?
for(int j=i-1;j>=0 && i-j<=d;j--){
fmax[k][i]=max(fmax[k][i],max(fmax[k-1][j]*A[i],fmin[k-1][j]*A[i]));
fmin[k][i]=min(fmin[k][i],min(fmin[k-1][j]*A[i],fmax[k-1][j]*A[i]));
}
}
maxV=max(maxV,fmax[K][i]);
}
cout<return 0;
}
//代码需要调整for循环的内外顺序即可,体会一下。这样每一次内循环都可以把所有的元素考虑进去,就会使得不同排列顺序的算是两种可能。
class Solution {
public:
int combinationSum4(vector<int>& nums, int target) {
if(nums.size()==0)return 0;
vector<int> dp(target+1);
dp[0]=1;
for(int i=1;i<=target;i++){
for(int j=0;jif(nums[j]<=i)dp[i]+=dp[i-nums[j]];
}
}
return dp[target];
}
};
//采用回溯,逐一放入,不行弹出。dfs
class Solution {
public:
vector<vector<int>> res;
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
vector<int> tmp;
set(candidates,target,tmp,0);
return res;
}
void set(vector<int> &A,int target,vector<int> &tmp,int begin){
if(!target){
res.push_back(tmp);
return;
}
for(int i=begin;iif(target>=A[i])set(A,target-A[i],tmp,i);
tmp.pop_back();
}
}
};