2020-7-13

948. 令牌放置

题解: 贪心算法:tokens从小到大排序, 如果现在手里面的能量还大于最小的token,我们就将其换成分数(这样做怎么都不会亏,因为我们可以用分数去换更大的);否则如果我们手里还有分数,我们就去换最大的token。

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. 删除与获得点数

题解:动态规划: 首先把每个点数出现的数量统计一下,第i个点出现的次数为count[i],接下来就是经典的选与不选(相邻的不能选,不相邻的必选)的问题。dp[i] = min(dp[i-1] , dp[i-2]+i*count[i])。

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. 打家劫舍(输出路径)

题解: dp[i] = max(dp[i-1], dp[i-2] + val[i])

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. 二叉树中的列表

题解: 对树做遍历,若当前root->val == head->val 则单独进行递归。

/**
 * Definition for singly-linked list.
 * 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:
    bool isok(ListNode* head, TreeNode* root){
        if(head==NULL) return true;
        if(root==NULL) return false;
        if(head->val == root->val){
            return isok(head->next, root->left) || isok(head->next, root->right);
        }
        return false;
    }
    bool isSubPath(ListNode* head, TreeNode* root) {
        if(root==NULL) return false;
        bool flag = false;
        if(root->val == head->val) flag = isok(head, root);
        bool ok1 = isSubPath(head, root->left);
        bool ok2 = isSubPath(head, root->right);
        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编辑  收藏