获得最小卡牌和数
假设我们在数组 T 中有五个数字。有五张卡,每张卡上都写了一个数字。第 i 张卡上写着 T[i]。我们可以丢弃一些卡,我们的目标是使剩余数字中的数字之和小于最小值。他最多可以丢弃两张或三张带有相同数字的卡牌。如果无法选择两张或三张带有相同数字的卡牌,我们将不会丢弃卡牌。我们必须找到尽可能小的最小和。
因此,如果输入如 T = [7, 3, 7, 3, 20],则输出将为 26,因为删除了两张数字为 7 的卡。剩余之和将为 3 + 3 + 20 = 26。
步骤
为了解决这个问题,我们将按照以下步骤执行操作 −
n := 5 m := 0 Define an array k of size: 101 and fill with 0 for initialize i := 0, when i < n, update (increase i by 1), do: a := T[i] m := m + a (increase k[a] by 1) M := m for initialize i := 0, when i < 101, update (increase i by 1), do: if k[i] > 1, then: M := minimum of M and (m - i * (minimum of 3 and k[i])) return M
示例
让我们看看以下实现以获得更好的理解 −
#include <bits/stdc++.h> using namespace std; int solve(vector<int> T){ int n = 5, m = 0, a; int k[101] = { 0 }; for (int i = 0; i < n; i++){ int a = T[i]; m += a; k[a]++; } int M = m; for (int i = 0; i < 101; i++) if (k[i] > 1){ M = min(M, m - i * (min(3, k[i]))); } return M; } int main(){ vector<int> T = { 7, 3, 7, 3, 20 }; cout << solve(T) << endl; }
Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.
输入
{ 7, 3, 7, 3, 20 }
输出
26
开启你的事业
开始学习广告