总结:2/4 前两道还是比较简单的 5885是最基础的贪心算法(还没学过,但只是知道思想也可以做出来)
5886其实只要读懂题目,就知道统计最多操作次数就可以了,不用考虑动态变化(因为两人选的都是最优解,也就是尽量删除中间的字符),这只是一个静态问题。
5885. 使每位学生都有座位的最少移动次数
贪心
class Solution {
public:
int minMovesToSeat(vector<int> seats, vector<int>students) {
sort(seats.begin(), seats.end());
sort(students.begin(), students.end());
int ans = 0;
for (int i = 0; i < seats.size(); i++)
ans += abs(seats[i] - students[i]);
return ans;
}
};
5886. 如果相邻两个颜色均相同则删除当前颜色
思路就是其实只要判断Alice和Bob分别可以删多少次
删的次数多的那一方获胜
class Solution {
public:
bool winnerOfGame(string colors) {
//滑动窗口计数
if(colors.length()<3)
return false;
int Alice=0,Bob=0;
for(int i=1;i<colors.length();i++){
if(colors[i]=='A')
if(colors[i]==colors[i-1]&&colors[i]==colors[i+1])
Alice++;
if(colors[i]=='B')
if(colors[i]==colors[i-1]&&colors[i]==colors[i+1])
Bob++;
}
if(Alice>Bob)
return true;
else
return false;
}
};