返回介绍

solution / 1000-1099 / 1090.Largest Values From Labels / README_EN

发布于 2024-06-17 01:03:23 字数 5549 浏览 0 评论 0 收藏 0

1090. Largest Values From Labels

中文文档

Description

There is a set of n items. You are given two integer arrays values and labels where the value and the label of the ith element are values[i] and labels[i] respectively. You are also given two integers numWanted and useLimit.

Choose a subset s of the n elements such that:

  • The size of the subset s is less than or equal to numWanted.
  • There are at most useLimit items with the same label in s.

The score of a subset is the sum of the values in the subset.

Return _the maximum score of a subset _s.

 

Example 1:

Input: values = [5,4,3,2,1], labels = [1,1,2,2,3], numWanted = 3, useLimit = 1
Output: 9
Explanation: The subset chosen is the first, third, and fifth items.

Example 2:

Input: values = [5,4,3,2,1], labels = [1,3,3,3,2], numWanted = 3, useLimit = 2
Output: 12
Explanation: The subset chosen is the first, second, and third items.

Example 3:

Input: values = [9,8,8,7,6], labels = [0,0,0,1,1], numWanted = 3, useLimit = 1
Output: 16
Explanation: The subset chosen is the first and fourth items.

 

Constraints:

  • n == values.length == labels.length
  • 1 <= n <= 2 * 104
  • 0 <= values[i], labels[i] <= 2 * 104
  • 1 <= numWanted, useLimit <= n

Solutions

Solution 1

class Solution:
  def largestValsFromLabels(
    self, values: List[int], labels: List[int], numWanted: int, useLimit: int
  ) -> int:
    ans = num = 0
    cnt = Counter()
    for v, l in sorted(zip(values, labels), reverse=True):
      if cnt[l] < useLimit:
        cnt[l] += 1
        num += 1
        ans += v
        if num == numWanted:
          break
    return ans
class Solution {
  public int largestValsFromLabels(int[] values, int[] labels, int numWanted, int useLimit) {
    int n = values.length;
    int[][] pairs = new int[n][2];
    for (int i = 0; i < n; ++i) {
      pairs[i] = new int[] {values[i], labels[i]};
    }
    Arrays.sort(pairs, (a, b) -> b[0] - a[0]);
    Map<Integer, Integer> cnt = new HashMap<>();
    int ans = 0, num = 0;
    for (int i = 0; i < n && num < numWanted; ++i) {
      int v = pairs[i][0], l = pairs[i][1];
      if (cnt.getOrDefault(l, 0) < useLimit) {
        cnt.merge(l, 1, Integer::sum);
        num += 1;
        ans += v;
      }
    }
    return ans;
  }
}
class Solution {
public:
  int largestValsFromLabels(vector<int>& values, vector<int>& labels, int numWanted, int useLimit) {
    int n = values.size();
    vector<pair<int, int>> pairs(n);
    for (int i = 0; i < n; ++i) {
      pairs[i] = {-values[i], labels[i]};
    }
    sort(pairs.begin(), pairs.end());
    unordered_map<int, int> cnt;
    int ans = 0, num = 0;
    for (int i = 0; i < n && num < numWanted; ++i) {
      int v = -pairs[i].first, l = pairs[i].second;
      if (cnt[l] < useLimit) {
        ++cnt[l];
        ++num;
        ans += v;
      }
    }
    return ans;
  }
};
func largestValsFromLabels(values []int, labels []int, numWanted int, useLimit int) (ans int) {
  n := len(values)
  pairs := make([][2]int, n)
  for i := 0; i < n; i++ {
    pairs[i] = [2]int{values[i], labels[i]}
  }
  sort.Slice(pairs, func(i, j int) bool { return pairs[i][0] > pairs[j][0] })
  cnt := map[int]int{}
  for i, num := 0, 0; i < n && num < numWanted; i++ {
    v, l := pairs[i][0], pairs[i][1]
    if cnt[l] < useLimit {
      cnt[l]++
      num++
      ans += v
    }
  }
  return
}
function largestValsFromLabels(
  values: number[],
  labels: number[],
  numWanted: number,
  useLimit: number,
): number {
  const n = values.length;
  const pairs = new Array(n);
  for (let i = 0; i < n; ++i) {
    pairs[i] = [values[i], labels[i]];
  }
  pairs.sort((a, b) => b[0] - a[0]);
  const cnt: Map<number, number> = new Map();
  let ans = 0;
  for (let i = 0, num = 0; i < n && num < numWanted; ++i) {
    const [v, l] = pairs[i];
    if ((cnt.get(l) || 0) < useLimit) {
      cnt.set(l, (cnt.get(l) || 0) + 1);
      ++num;
      ans += v;
    }
  }
  return ans;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文