package leetcode;

/**
 * https://leetcode.com/problems/largest-values-from-labels/
 */
public class Problem1090 {
    public int largestValsFromLabels(int[] values, int[] labels, int num_wanted, int use_limit) {
        // TODO
        return 0;
    }

    public static void main(String[] args) {
        Problem1090 prob = new Problem1090();
        System.out.println(prob.largestValsFromLabels(new int[]{5, 4, 3, 2, 1},
            new int[]{1, 1, 2, 2, 3}, 3, 1)); // 9
        System.out.println(prob.largestValsFromLabels(new int[]{5, 4, 3, 2, 1},
            new int[]{1, 3, 3, 3, 2}, 3, 2)); // 12
        System.out.println(prob.largestValsFromLabels(new int[]{5, 4, 3, 2, 1},
            new int[]{1, 3, 3, 3, 2}, 3, 1)); // 10
        System.out.println(prob.largestValsFromLabels(new int[]{9, 8, 8, 7, 6},
            new int[]{0, 0, 0, 1, 1}, 3, 1)); // 16
        System.out.println(prob.largestValsFromLabels(new int[]{9, 8, 8, 7, 6},
            new int[]{0, 0, 0, 1, 1}, 3, 2)); // 24
    }
}

@@ -12,16 +12,168 @@
 tcode;%0A%0A
+import java.util.ArrayList;%0Aimport java.util.Arrays;%0Aimport java.util.HashMap;%0Aimport java.util.Iterator;%0Aimport java.util.List;%0Aimport java.util.Map;%0A%0A
 /**%0A * h
@@ -370,790 +370,1280 @@
     
-// TODO%0A        return 0;%0A    %7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem1090 prob = new Problem1090();%0A        System.out.println(prob.largestValsFromLabels(new int%5B%5D%7B5, 4, 3, 2, 1%7D,%0A            new int%5B%5D%7B1, 1, 2, 2, 3%7D, 3, 1)); // 9%0A        System.out.println(prob.largestValsFromLabels(new int%5B%5D%7B5, 4, 3, 2, 1%7D,%0A            new int%5B%5D%7B1, 3, 3, 3, 2%7D, 3, 2)); // 12%0A        System.out.println(prob.largestValsFromLabels(new int%5B%5D%7B5, 4, 3, 2, 1%7D,%0A            new int%5B%5D%7B1, 3, 3, 3, 2%7D, 3, 1)); // 10%0A        System.out.println(prob.largestValsFromLabels(new int%5B%5D%7B9, 8, 8, 7, 6%7D,%0A            new int%5B%5D%7B0, 0, 0, 1, 1%7D, 3, 1)); // 16%0A        System.out.println(prob.largestValsFromLabels(new int%5B%5D%7B9, 8, 8, 7, 6%7D,%0A            new int%5B%5D%7B0, 0, 0, 1, 1%7D, 3, 2)); // 24
+Map%3C/*value*/ Integer, /*labels*/ List%3CInteger%3E%3E valueLabels = new HashMap%3C%3E();%0A        for (int i = 0; i %3C labels.length; i++) %7B%0A            if (!valueLabels.containsKey(values%5Bi%5D)) %7B%0A                valueLabels.put(values%5Bi%5D, new ArrayList%3C%3E());%0A            %7D%0A            valueLabels.get(values%5Bi%5D).add(labels%5Bi%5D);%0A        %7D%0A        int answer = 0;%0A        Arrays.sort(values);%0A        int size = 0;%0A        Map%3C/*label*/ Integer, /*count*/ Integer%3E labelCounts = new HashMap%3C%3E();%0A        outer:%0A        for (int i = values.length - 1; i %3E= 0; i--) %7B%0A            if (size == num_wanted) %7B%0A                break;%0A            %7D%0A            List%3CInteger%3E labelList = valueLabels.get(values%5Bi%5D);%0A            Iterator%3CInteger%3E iterator = labelList.iterator();%0A            while (iterator.hasNext()) %7B%0A                int label = iterator.next();%0A                if (size == num_wanted) %7B%0A                    break outer;%0A                %7D%0A                Integer count = labelCounts.getOrDefault(label, 0);%0A                if (count %3C use_limit) %7B%0A                    answer += values%5Bi%5D;%0A                    labelCounts.put(label, count + 1);%0A                    iterator.remove();%0A                    size++;%0A                %7D%0A            %7D%0A        %7D%0A        return answer;
 %0A   

Solve problem 1090