# 2020-7-13

948. 令牌放置

class Solution {
public:
bool vis[1005][2];
int bagOfTokensScore(vector<int>& tokens, int P) {
sort(tokens.begin(), tokens.end());
int i = 0,j= tokens.size()-1,score = 0,ans=0 ;
while(i<=j){
if(P>=tokens[i]){
score++;
P-=tokens[i++];
}else if(score>=1){
score--;
P+=tokens[j--];
}else break;
ans = max(ans, score);
}
return ans;
}
};
View Code

#### 740. 删除与获得点数

class Solution {
public:
int count[10005], dp[10005];
int deleteAndEarn(vector<int>& nums) {
for(int i=0;i<nums.size();i++) count[nums[i]]++;
dp[1] = count[1], dp[2] = max(count[2]*2, dp[1]);
for(int i=3;i<=10000;i++){
dp[i] = max(dp[i-2] + i*count[i], dp[i-1]);
// printf("%d %d\n",i, dp[i]);
}
return dp[10000];
}
};
View Code

#### 198. 打家劫舍（输出路径）

class Solution {
public:
int rob(vector<int>& nums) {
if(nums.size()==0) return 0;
if(nums.size()==1) return nums[0];
vector <int> dp(nums.size());
vector <pair<int,int> > pre(nums.size()); /// first代表该位置的上一位，second代表该位置是否被选
dp[0] = nums[0];
pre[0] = {-1, 1};
if(nums[1] > nums[0]) {dp[1] = nums[1] , pre[1] = {-1, 1};}
else dp[1] = nums[0], pre[1] = {0, 0};
for(int i=2;i<nums.size();i++){
if(dp[i-1] > dp[i-2] + nums[i]){
pre[i] = {i-1, 0};
dp[i] = dp[i-1];
}else{
pre[i] = {i-2, 1};
dp[i] = dp[i-2] + nums[i];
}
}
int now = nums.size()-1;
vector <int> ans;
while(now!=-1){
if(pre[now].second) ans.push_back(now);
now = pre[now].first;
}
reverse(ans.begin(), ans.end());
for(int i=0;i<ans.size();i++){
printf("%d ",ans[i]);
}
return dp[nums.size()-1];
}
};

class Solution(object):
def rob(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
if len(nums)==0: return 0
if len(nums)==1: return nums[0]
dp = [0]*len(nums)
pre = [0]*len(nums)

dp[0] = nums[0]
pre[0] = [-1, 1]
if nums[0] > nums[1]:
dp[1] = nums[0]
pre[1] = [0, 0]
else:
dp[1] = nums[1]
pre[1] = [-1,1]

for i in range(2, len(nums)):
if dp[i-1] > dp[i-2] + nums[i]:
dp[i] = dp[i-1]
pre[i] = [i-1, 0]
else:
dp[i] = dp[i-2] + nums[i]
pre[i] = [i-2, 1]

take = []
now = len(nums)-1
while now!=-1:
l = list(pre[now])
if pre[now][1] == 1:
take.append(now)
now = pre[now][0]

print(take[::-1])
return dp[len(nums)-1]


class Solution(object):
def rob(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
if len(nums)==0: return 0
if len(nums)==1: return nums[0]
dp = [0]*len(nums)
pre = [0]*len(nums)

dp[0] = nums[0]
pre[0] = [0]
if nums[0] > nums[1]:
dp[1] = nums[0]
pre[1] = [0]
else:
dp[1] = nums[1]
pre[1] = [1]

for i in range(2, len(nums)):
if dp[i-1] > dp[i-2] + nums[i]:
pre[i] = pre[i-1]
dp[i] = dp[i-1]
else:
pre[i] = pre[i-2] + [i]
dp[i] = dp[i-2] + nums[i]

print(pre[len(nums)-1])
return dp[len(nums)-1]

#### 1367. 二叉树中的列表

/**
* struct ListNode {
*     int val;
*     ListNode *next;
*     ListNode(int x) : val(x), next(NULL) {}
* };
*/
/**
* Definition for a binary tree node.
* struct TreeNode {
*     int val;
*     TreeNode *left;
*     TreeNode *right;
*     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
if(root==NULL) return false;
}
return false;
}
bool isSubPath(ListNode* head, TreeNode* root) {
if(root==NULL) return false;
bool flag = false;
return flag || ok1 || ok2;
}
};
View Code

#### 1343. 大小为 K 且平均值大于等于阈值的子数组数目

class Solution {
public:

int numOfSubarrays(vector<int>& arr, int k, int threshold) {
vector<int> sum(arr.size());
sum[0] = arr[0];
for(int i=1;i<arr.size();i++){
sum[i] = sum[i-1] + arr[i];
}
int ans = 0;
if(sum[k-1]>=threshold*k) ans++;
for(int i=k;i<arr.size();i++){
int l = i-k, r = i;
if(sum[r]-sum[l] >= threshold*k) ans++;
}
return ans;
}
};
View Code

#### 97. 交错字符串

class Solution {
public:
int dp[2005][2005];
bool isInterleave(string s1, string s2, string s3) {
int n = s1.size(), m = s2.size();// s1 负责列方向， s2 负责行方向
if(n+m!=s3.size()) return false;
dp[0][0] = 1;
for(int i=1;i<=n && s1[i-1] == s3[i-1];i++) dp[i][0] = true;
for(int i=1;i<=m && s2[i-1] == s3[i-1];i++) dp[0][i] = true;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
dp[i][j] = (dp[i-1][j] && s1[i-1] == s3[i+j-1]) ||
(dp[i][j-1] && s2[j-1] == s3[i+j-1]);
}
}
return dp[n][m];
}
};
View Code

posted @ 2020-07-18 20:54  樱花庄的龙之介大人  阅读(45)  评论(0编辑  收藏