标签:周赛 ch return int 298 ans ord dp
暴力解决
1 class Solution: 2 def greatestLetter(self, s: str) -> str: 3 a=[0]*66 4 b=[0]*66 5 # str=input() 6 7 for ch in s: 8 if ch.isupper(): 9 a[ord(ch)-ord("A")]=1 10 if ch.islower(): 11 b[ord(ch)-ord("a")]=1 12 ans=-1 13 # print(a[:27]) 14 # print(b[:27]) 15 for i in range(26): 16 if (a[i]==1 and b[i]==1): 17 ans=i 18 # print(ans) 19 20 p="" 21 if ans==-1: 22 return p; 23 else: 24 p=chr(ord("A")+ans) 25 return p;
BFS暴力遍历
1 const int N = 2e5+10; 2 int vis[N]; 3 struct node{ 4 int a,b; 5 bool operator < (const node &t)const{ 6 return b>t.b; 7 } 8 }; 9 class Solution { 10 public: 11 int minimumNumbers(int num, int k) { 12 if(!num) return 0; 13 if(num<k) return -1; 14 if(k==2&&num%2) return -1; 15 vector<int> f; 16 f.clear(); 17 memset(vis,0,sizeof(vis)); 18 priority_queue<node> q; 19 for(int i=1;i<=num;i++){ 20 if(i%10==k){ 21 if(i==num) return 1; 22 f.push_back(i); 23 q.push({i,1}); 24 } 25 } 26 if(f.size()==0) return -1; 27 if(num==f[0]){ 28 return 1; 29 } 30 int ans=-1; 31 while(!q.empty()){ 32 node u=q.top(); 33 q.pop(); 34 if(vis[u.a]) continue; 35 vis[u.a]=1; 36 for(auto x:f){ 37 int tmp=u.a+x; 38 if(tmp>num) continue; 39 else if(tmp==num){ 40 ans=u.b+1; 41 break; 42 }else{ 43 q.push({tmp,u.b+1}); 44 } 45 } 46 } 47 return ans; 48 } 49 };
贪心的思想:字符为0全选,字符为1的个数通过二分进行选取。由于给定K的范围可以知道二分右端点不会超过31.
1 class Solution: 2 def longestSubsequence(self, s: str, k: int) -> int: 3 a=[] 4 tot=0 5 for i in range(len(s)): 6 if s[i]=='0': 7 tot=tot+1 8 else: 9 a.append(len(s)-1-i) 10 a.reverse() 11 p=0 12 l=0 13 r=min(len(a),31) 14 while l<=r: 15 mid=(l+r)//2 16 sum=0 17 for i in range(mid): 18 sum=sum+2**a[i] 19 if sum<=k: 20 l=mid+1 21 p=mid 22 else: 23 r=mid-1 24 return (p+tot)
记忆化深搜. 设dp[i][j]表示高为i、宽为j的木头可以获得的最大价值。 那么,我们就可以对每一种裁法进行暴搜,获得最大值。
1 typedef long long ll; 2 const int N = 209 ; 3 ll dp[N][N]; 4 map<pair<int,int>,int> mp; 5 class Solution { 6 public: 7 long long dfs(int h,int w){ 8 if(dp[h][w]!=-1) return dp[h][w]; 9 // if(!h||!w) return 0; 10 ll ans=0; 11 if(mp[{h,w}]) ans=mp[{h,w}]; 12 for(int i=1;i<h;i++) ans=max(ans*1ll,dfs(i,w)+dfs(h-i,w)); 13 for(int i=1;i<w;i++) ans=max(ans*1ll,dfs(h,i)+dfs(h,w-i)); 14 return dp[h][w]=ans; 15 } 16 long long sellingWood(int m, int n, vector<vector<int>>& prices) { 17 memset(dp,-1,sizeof(dp)); 18 mp.clear(); 19 for(int i=0;i<prices.size();i++){ 20 mp[{prices[i][0],prices[i][1]}]=prices[i][2]; 21 } 22 return dfs(m,n); 23 } 24 };
标签:周赛,ch,return,int,298,ans,ord,dp 来源: https://www.cnblogs.com/pengge666/p/16391814.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。