Level 1 (day 5)

第一题

题目链接:https://leetcode.cn/problems/best-time-to-buy-and-sell-stock/

个人题解:线性DP

代码:

class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int minn=INT_MAX,maxx=0;
        for(auto x:prices){
            maxx=max(maxx,x-minn);
            minn=min(minn,x);
        }
        return maxx;
    }
};

第二题

题目链接:https://leetcode.cn/problems/longest-palindrome/

个人题解:贪心,偶数加2,奇数加1

代码:

class Solution {
public:
    int longestPalindrome(string s) {
        unordered_map<char,int> mp;
        for(auto x:s) mp[x]++;
        int res=0;
        for(auto [k,v]:mp){
            res+=v/2*2;
            if(v%2==1 && res%2==0) res++;
        }
        return res;
    }
};
posted @ 2022-06-28 08:27  黑VS白-清墨  阅读(46)  评论(0)    收藏  举报