Skip to content

Commit aaebef4

Browse files
committed
一刷1090
1 parent bb85457 commit aaebef4

File tree

5 files changed

+77
-13
lines changed

5 files changed

+77
-13
lines changed

README.adoc

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7655,14 +7655,14 @@ TIP: **公众号的微信号是: `jikerizhi`**。__因为众所周知的原因
76557655
//|{doc_base_url}/1089-duplicate-zeros.adoc[题解]
76567656
//|Easy
76577657
//|
7658-
//
7659-
//|{counter:codes}
7660-
//|{leetcode_base_url}/largest-values-from-labels/[1090. Largest Values From Labels^]
7661-
//|{source_base_url}/_1090_LargestValuesFromLabels.java[Java]
7662-
//|{doc_base_url}/1090-largest-values-from-labels.adoc[题解]
7663-
//|Medium
7664-
//|
7665-
//
7658+
7659+
|{counter:codes}
7660+
|{leetcode_base_url}/largest-values-from-labels/[1090. Largest Values From Labels^]
7661+
|{source_base_url}/_1090_LargestValuesFromLabels.java[Java]
7662+
|{doc_base_url}/1090-largest-values-from-labels.adoc[题解]
7663+
|Medium
7664+
|排序规则跟 179、870 题类似:保存下标,使用其他数组的值对下标数组进行排序。
7665+
76667666
//|{counter:codes}
76677667
//|{leetcode_base_url}/shortest-path-in-binary-matrix/[1091. Shortest Path in Binary Matrix^]
76687668
//|{source_base_url}/_1091_ShortestPathInBinaryMatrix.java[Java]

docs/1090-largest-values-from-labels.adoc

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ We have a set of items: the `i`-th item has value `values[i]` and label `labels[
88
Then, we choose a subset `S` of these items, such that:
99

1010

11-
* `|S| <= num_wanted`
12-
* For every label `L`, the number of items in `S` with label `L` is `<= use_limit`.
11+
* `|S| \<= num_wanted`
12+
* For every label `L`, the number of items in `S` with label `L` is `\<= use_limit`.
1313
1414
1515
Return the largest possible sum of the subset `S`.
@@ -66,14 +66,34 @@ Return the largest possible sum of the subset `S`.
6666
* `1 <= num_wanted, use_limit <= values.length`
6767

6868

69+
== 思路分析
6970

71+
初看以为是回溯(回溯也可以,能通过大部分的测试),看答案之后,可以通过排序来解决。
7072

71-
72-
73+
再次想想,感觉回溯解决不了问题,它没办法保证首先选中的就是大数。
7374

7475
[[src-1090]]
76+
[tabs]
77+
====
78+
一刷::
79+
+
80+
--
7581
[{java_src_attr}]
7682
----
7783
include::{sourcedir}/_1090_LargestValuesFromLabels.java[tag=answer]
7884
----
85+
--
86+
87+
// 二刷::
88+
// +
89+
// --
90+
// [{java_src_attr}]
91+
// ----
92+
// include::{sourcedir}/_1090_LargestValuesFromLabels_2.java[tag=answer]
93+
// ----
94+
// --
95+
====
96+
97+
== 参考资料
7998

99+
. https://leetcode.cn/problems/largest-values-from-labels/solutions/2278874/shou-biao-qian-ying-xiang-de-zui-da-zhi-5h9ll/[1090. 受标签影响的最大值 - 官方题解^]

docs/index.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2254,7 +2254,7 @@ include::1011-capacity-to-ship-packages-within-d-days.adoc[leveloffset=+1]
22542254

22552255
// include::1089-duplicate-zeros.adoc[leveloffset=+1]
22562256

2257-
// include::1090-largest-values-from-labels.adoc[leveloffset=+1]
2257+
include::1090-largest-values-from-labels.adoc[leveloffset=+1]
22582258

22592259
// include::1091-shortest-path-in-binary-matrix.adoc[leveloffset=+1]
22602260

logbook/202406.adoc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -868,6 +868,13 @@
868868
|{doc_base_url}/2507-smallest-value-after-replacing-with-sum-of-prime-factors.adoc[题解]
869869
|❌ 数学计算,一脸懵逼
870870

871+
872+
|{counter:codes}
873+
|{leetcode_base_url}/largest-values-from-labels/[1090. Largest Values From Labels^]
874+
|{doc_base_url}/1090-largest-values-from-labels.adoc[题解]
875+
|⭕️ 贪心。排序规则跟 179、870 题类似:保存下标,使用其他数组的值对下标数组进行排序。
876+
877+
871878
|===
872879

873880
截止目前,本轮练习一共完成 {codes} 道题。
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.diguage.algo.leetcode;
2+
3+
import java.util.Arrays;
4+
import java.util.HashMap;
5+
import java.util.Map;
6+
7+
public class _1090_LargestValuesFromLabels {
8+
// tag::answer[]
9+
10+
/**
11+
* @author D瓜哥 · https://www.diguage.com
12+
* @since 2024-09-25 19:36:28
13+
*/
14+
public int largestValsFromLabels(int[] values, int[] labels, int numWanted, int useLimit) {
15+
int length = values.length;
16+
Integer[] id = new Integer[length];
17+
for (int i = 0; i < length; i++) {
18+
id[i] = i;
19+
}
20+
// 倒序排列,把大数放在前面
21+
Arrays.sort(id, (a, b) -> Integer.compare(values[b], values[a]));
22+
Map<Integer, Integer> cnt = new HashMap<>();
23+
int result = 0;
24+
int choose = 0;
25+
for (int i = 0; i < length && choose < numWanted; i++) {
26+
int label = labels[id[i]];
27+
if (cnt.getOrDefault(label, 0) == useLimit) {
28+
continue;
29+
}
30+
result += values[id[i]];
31+
choose++;
32+
cnt.put(label, cnt.getOrDefault(label, 0) + 1);
33+
}
34+
return result;
35+
}
36+
// end::answer[]
37+
}

0 commit comments

Comments
 (0)