Skip to content

Commit e906334

Browse files
add 1090
1 parent a8e89e0 commit e906334

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ _If you like this project, please leave me a star._ ★
238238
|1103|[Distribute Candies to People](https://leetcode.com/problems/distribute-candies-to-people/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1103.java) | |Easy|Math|
239239
|1100|[Find K-Length Substrings With No Repeated Characters](https://leetcode.com/problems/find-k-length-substrings-with-no-repeated-characters/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1100.java) | |Medium|String, Sliding Window|
240240
|1099|[Two Sum Less Than K](https://leetcode.com/problems/two-sum-less-than-k/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1099.java) | [:tv:](https://www.youtube.com/watch?v=2Uq7p7HE0TI)|Easy||
241+
|1090|[Largest Values From Labels](https://leetcode.com/problems/largest-values-from-labels/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1090.java) | |Medium|HashTable, Greedy|
241242
|1089|[Duplicate Zeros](https://leetcode.com/problems/duplicate-zeros/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1089.java) | |Easy||
242243
|1087|[Brace Expansion](https://leetcode.com/problems/brace-expansion/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1087.java) | |Medium|Backtracking|
243244
|1086|[High Five](https://leetcode.com/problems/high-five/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1086.java) | [:tv:](https://www.youtube.com/watch?v=3iqC5J4l0Cc)|Easy||
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.Arrays;
4+
import java.util.HashMap;
5+
import java.util.Map;
6+
7+
public class _1090 {
8+
public static class Solution1 {
9+
public int largestValsFromLabels(int[] values, int[] labels, int numWanted, int useLimit) {
10+
int[][] tuple = new int[values.length][2];
11+
for (int i = 0; i < values.length; i++) {
12+
tuple[i][0] = values[i];
13+
tuple[i][1] = labels[i];
14+
}
15+
Arrays.sort(tuple, (a, b) -> b[0] - a[0]);
16+
Map<Integer, Integer> labelUsedCountMap = new HashMap<>();
17+
int sum = 0;
18+
int numbersUsed = 0;
19+
for (int i = 0; i < values.length; i++) {
20+
int val = tuple[i][0];
21+
int usedCount = labelUsedCountMap.getOrDefault(tuple[i][1], 0);
22+
if (usedCount >= useLimit) {
23+
continue;
24+
} else {
25+
sum += val;
26+
numbersUsed++;
27+
labelUsedCountMap.put(tuple[i][1], usedCount + 1);
28+
}
29+
if (numbersUsed >= numWanted) {
30+
break;
31+
}
32+
}
33+
return sum;
34+
}
35+
}
36+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1090;
4+
import com.fishercoder.solutions._28;
5+
import org.junit.Before;
6+
import org.junit.Test;
7+
8+
import static junit.framework.TestCase.assertEquals;
9+
10+
public class _1090Test {
11+
private static _1090.Solution1 solution1;
12+
13+
@Before
14+
public void setupForEachTest() {
15+
solution1 = new _1090.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
assertEquals(9, solution1.largestValsFromLabels(new int[]{5, 4, 3, 2, 1}, new int[]{1, 1, 2, 2, 3}, 3, 1));
21+
}
22+
23+
}

0 commit comments

Comments
 (0)