【leetcode周赛记录】第76场双周赛+第289场周赛记录
赛后个人排名
赛题分析总结
第76场双周赛
2239.找到最接近0的数字
class Solution {
public:
// 暴力即可
int findClosestNumber(vector<int>& nums) {
int n = nums.size();
int maxValue{abs(nums[0])};
int result = nums[0];
for(int i = 1;i<n;++i){
if(abs(nums[i]) < maxValue){
maxValue = abs(nums[i]);
result = nums[i];
}else if(abs(nums[i]) == maxValue){
result = max(result,nums[i]);
}
}
return result;
}
};
2240.买钢笔和铅笔的方案数
class Solution {
public:
// 贪心求解
long long waysToBuyPensPencils(int total, int cost1, int cost2) {
long long result{};
for(int i{};i<=total/cost1;++i){
result = result + (total-(i*cost1))/cost2 + 1;
}
return result;
}
};
2241.设计一个ATM机器
class ATM {
public:
// 模拟求解
unordered_map<long,long> mp;
ATM() {
mp[20] = 0;
mp[50] = 0;
mp[100] = 0;
mp[200] = 0;
mp[500] = 0;
}
void deposit(vector<int> banknotesCount) {
mp[20] += banknotesCount[0];
mp[50] += banknotesCount[1];
mp[100] += banknotesCount[2];
mp[200] += banknotesCount[3];
mp[500] += banknotesCount[4];
}
vector<int> withdraw(int amount) {
vector<int> result(5);
unordered_map<long,long> temp = mp;
if(amount >= 500){
long a = amount/500;
amount -= min(a,temp[500])*500;
result[4] = min(a,temp[500]);
temp[500] -= min(a,temp[500]);
}
if(amount == 0) {
mp = temp;
return result;
}
if(amount >= 200){
long a = amount/200;
amount -= min(a,temp[200])*200;
result[3] = min(a,temp[200]);
temp[200] -= min(a,temp[200]);
}
if(amount == 0) {
mp = temp;
return result;
}
if(amount >= 100){
long a = amount/100;
amount -= min(a,temp[100])*100;
result[2] = min(a,temp[100]);
temp[100] -= min(a,temp[100]);
}
if(amount == 0) {
mp = temp;
return result;
}
if(amount >= 50){
long a = amount/50;
amount -= min(a,temp[50])*50;
result[1] = min(a,temp[50]);
temp[50] -= min(a,temp[50]);
}
if(amount == 0) {
mp = temp;
return result;
}
if(amount >= 20){
long a = amount/20;
amount -= min(a,temp[20])*20;
result[0] = min(a,temp[20]);
temp[20] -= min(a,temp[20]);
}
if(amount == 0) {
mp = temp;
return result;
}
if(amount > 0){
return {-1};
}
return {-1};
}
};
2242.节点序列的最大得分
直接忽略,嘿嘿
第289场周赛
2243.计算字符串的数字和
class Solution {
public:
string getNum(string &s, int k){
int n = s.size();
string result;
for(int i{};i<n;i+=k){
int temp{};
for(int j{};j<k && i+j < n;++j){
int num1 = s[i+j] - '0';
temp+=num1;
}
result += to_string(temp);
}
return result;
}
string digitSum(string s, int k) {
while(s.size() > k){
s = getNum(s,k);
}
return s;
}
};
2244.完成所有任务需要的最少轮数
class Solution {
public:
// 应该使用DP求解,但是没有想到;答题过程硬是逻辑分析搞定了
int minimumRounds(vector<int>& tasks) {
unordered_map<int,int> mp;
for(int &num : tasks) mp[num]++;
int count{};
for(auto &[key,value] : mp){
if(value == 1) return -1;
if(value%2 == 0){
if(value%3 == 0){
int a = value/3;
count+= value/3;
mp[key] -= a*3;
}else{
if((value-2)%3 == 0){
int value2 = value-2;
int b = value2/3;
count += value2/3;
mp[key] -= b*3;
count += 1;
mp[key] -= 2;
}else if((value -4)%3 == 0){
int value2 = value-4;
int b = value2/3;
count += value2/3;
mp[key] -= b*3;
count += 2;
mp[key] -= 4;
}else{
int b = value/2;
count += value/2;
mp[key] -= b*2;
}
}
}else{
if((value-2)%3 == 0){
int value2 = value-2;
int b = value2/3;
count += value2/3;
mp[key] -= b*3;
count += 1;
mp[key] -= 2;
}else if((value -4)%3 == 0){
int value2 = value-4;
int b = value2/3;
count += value2/3;
mp[key] -= b*3;
count += 2;
mp[key] -= 4;
}else{
int a = value/3;
count+= value/3;
mp[key] -= a*3;
int b = value/2;
count += value/2;
mp[key] -= b*2;
}
}
}
return count;
}
};
2245.转角路径的乘积中最多能有几个尾随零
// 这样的前缀和算法的应用超出自己的能力了,很难想到
// 后续需要加强前缀和算法的系统训练总结
2246.相邻字符不同的最长路径
第四题暂不考虑
反思总结
个人情况
第24、25次参加leetcode竞赛;
总计得到过6次12分,10次7分,9次3分;
后续改进
- 前缀和系统训练,总结
- DP总结