Rank
A. 速算机器人
简单模拟.
class Solution {
public:
int calculate(string s) {
int x=1,y=0;
for(char ch:s){
if(ch=='A'){
x=x*2+y;
}
else{
y=y*2+x;
}
}
return x+y;
}
};
B. 早餐组合
排序+二分.
class Solution {
public:
const static int mod=1e9+7;
int breakfastNumber(vector<int>& staple, vector<int>& drinks, int x) {
int ans=0;
sort(staple.begin(),staple.end());
sort(drinks.begin(),drinks.end());
int n=staple.size(),m=drinks.size();
for(int i=0;i<n;++i){
int pos=upper_bound(drinks.begin(),drinks.end(),x-staple[i])-drinks.begin();
ans+=pos;
ans%=mod;
}
return ans;
}
};
C. 秋叶收藏集
线性 DP.
dp[i][0]
表示把 leaves[0...i]<