lc3270
遍历数位 不用字符串
class Solution {
public:
int generateKey(int x, int y, int z) {
int ans = 0;
for (int pow10 = 1;x && y && z; pow10 *= 10) {
ans += min({x % 10, y % 10, z % 10}) * pow10;
x /= 10;
y /= 10;
z /= 10;
}
return ans;
}
};
lc3271
class Solution {
public:
string stringHash(string s, int k) {
int n = s.length();
string ans(n / k, 'a');
for (int i = 0; i < n; i += k) {
int sum = 0;
for (int j = i; j < i + k; j++) {
sum += s[j] - 'a';
}
ans[i / k] += sum % 26;
}
return ans;
}
};
lc3273
特殊到一般 分析出贪心
class Solution {
public:
long long minDamage(int power, vector<int>& damage, vector<int>& health) {
int n = health.size();
vector<pair<int, int>> a(n);
for (int i = 0; i < n; i++) {
a[i] = {(health[i] - 1) / power + 1, damage[i]};
}
ranges::sort(a, [](const auto& p, const auto& q) {
return p.first * q.second < q.first * p.second;
});
long long ans = 0, s = 0;
for (auto& [k, d] : a) {
s += k;
ans += s * d;
}
return ans;
}
};