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;
}
};
标签:maxx,minn,Level,int,res,mp,auto,day 来源: https://www.cnblogs.com/cytcnblogs/p/16418213.html