OA Help : Interview Help
Открыть в Telegram
Codeforces Codechef Leetcode AtCoder GFG CodeStudio All Contests Solutions available.
Больше898
Подписчики
Нет данных24 часа
-47 дней
-1430 день
Архив постов
D solution
Step 1
Push all elements in a single Vector
step 2
Sort it
step 3
consider 1-indexing and multiply each element with its index and add them
class Solution {
public:
long long maxSpending(vector>& values) {
vector items;
for (const auto& it : values) {
for (int x : it) {
items.push_back(x);
}
}
sort(items.begin(), items.end());
long long ans = 0;
long long d = 1;
for (int x : items) {
ans += d++ * static_cast(x);
}
return ans;
}
};
// D
https://leetcode.ca/2023-11-09-2927-Distribute-Candies-Among-Children-III/
Posted 2 days ago
Questions pehle hi out hogye the 😂
class Solution {
public:
long long distributeCandies(int n, int limit) {
auto comb2 = [](int n) {
return 1LL * n * (n - 1) / 2;
};
if (n > 3 * limit) {
return 0;
}
long long ans = comb2(n + 2);
if (n > limit) {
ans -= 3 * comb2(n - limit + 1);
}
if (n - 2 >= 2 * limit) {
ans += 3 * comb2(n - 2 * limit);
}
return ans;
}
};
// A and B both
