Skip to content

Commit 47b1823

Browse files
committed
new problem
1 parent 0d35982 commit 47b1823

File tree

3 files changed

+67
-33
lines changed

3 files changed

+67
-33
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@
143143
|131|[Palindrome Partitioning](https://leetcode.com/problems/palindrome-partitioning/)| [Java](leetcode/solution/src/PalindromePartitioning.java)||
144144
|132|[Palindrome Partitioning II](https://leetcode.com/problems/palindrome-partitioning-ii/)| [Java](leetcode/solution/src/PalindromePartitioningII.java)||
145145
|133|[Clone Graph](https://leetcode.com/problems/clone-graph/)| [Java](leetcode/solution/src/CloneGraph.java)|70|这题不难,多看两遍,BFS方法再做两遍|
146+
|135|[Candy](https://leetcode.com/problems/candy/)|[Java](leetcode/solution/src/Candy.java)|80||
146147
|138|[Copy List with Random Pointer](https://leetcode.com/problems/copy-list-with-random-pointer/)| [Java](leetcode/solution/src/CopyListWithRandomPointer.java)|95|有一个易错点|
147148
|139|[Word Break](https://leetcode.com/problems/word-break/)| [Java](leetcode/solution/src/WordBreak.java)|80|多看两遍|
148149
|140|[Word Break II](https://leetcode.com/problems/word-break-ii/)| [Java](leetcode/solution/src/WordBreakII.java)|75|这题多做两遍,很典型|

leetcode/solution/src/Candy.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import java.util.Arrays;
2+
3+
public class Candy {
4+
5+
public int candy(int[] ratings) {
6+
int[] left = new int[ratings.length];
7+
int[] right = new int[ratings.length];
8+
9+
Arrays.fill(left, 1);
10+
Arrays.fill(right, 1);
11+
12+
for (int i = 1; i < ratings.length; i++) {
13+
if (ratings[i] > ratings[i - 1]) {
14+
left[i] = left[i - 1] + 1;
15+
}
16+
}
17+
for (int i = ratings.length - 2; i >= 0; i--) {
18+
if (ratings[i] > ratings[i + 1]) {
19+
right[i] = right[i + 1] + 1;
20+
}
21+
}
22+
int sum = 0;
23+
for (int i = 0; i < ratings.length; i++) {
24+
sum += Math.max(left[i], right[i]);
25+
}
26+
return sum;
27+
}
28+
29+
/**
30+
* 在上面的基础上优化了一下,去掉了一个数组
31+
*/
32+
public int candy2(int[] ratings) {
33+
int[] candys = new int[ratings.length];
34+
35+
Arrays.fill(candys, 1);
36+
37+
for (int i = 1; i < ratings.length; i++) {
38+
if (ratings[i] > ratings[i - 1]) {
39+
candys[i] = candys[i - 1] + 1;
40+
}
41+
}
42+
int sum = candys[ratings.length - 1], prev = 1;
43+
for (int i = ratings.length - 2; i >= 0; i--) {
44+
int cur = ratings[i] > ratings[i + 1] ? prev + 1 : 1;
45+
sum += Math.max(cur, candys[i]);
46+
prev = cur;
47+
}
48+
return sum;
49+
}
50+
}

leetcode/src/Main.java

Lines changed: 16 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,48 +6,31 @@ public class Main {
66

77
public static class Solution {
88

9-
public String getHint(String secret, String guess) {
10-
HashMap<Character, Set<Integer>> map1 = new HashMap<>();
11-
HashMap<Character, Set<Integer>> map2 = new HashMap<>();
12-
for (int i = 0; i < secret.length(); i++) {
13-
Set<Integer> set = map1.getOrDefault(secret.charAt(i), new HashSet<>());
14-
set.add(i);
15-
map1.put(secret.charAt(i), set);
16-
}
17-
for (int i = 0; i < guess.length(); i++) {
18-
Set<Integer> set = map2.getOrDefault(guess.charAt(i), new HashSet<>());
19-
set.add(i);
20-
map2.put(guess.charAt(i), set);
21-
}
22-
int bulls = 0, cows = 0;
23-
for (Character c : map2.keySet()) {
24-
Set<Integer> set1 = map1.get(c);
25-
26-
if (set1 == null) {
27-
continue;
28-
}
299

30-
Set<Integer> set2 = map2.get(c);
10+
public int candy(int[] ratings) {
11+
int[] candys = new int[ratings.length];
3112

32-
int count = 0;
13+
Arrays.fill(candys, 1);
3314

34-
for (Integer index : set2) {
35-
if (set1.contains(index)) {
36-
count++;
37-
}
15+
for (int i = 1; i < ratings.length; i++) {
16+
if (ratings[i] > ratings[i - 1]) {
17+
candys[i] = candys[i - 1] + 1;
3818
}
39-
40-
bulls += count;
41-
cows += Math.min(set1.size(), set2.size()) - count;
4219
}
43-
44-
return String.format("%dA%dB", bulls, cows);
20+
int sum = candys[ratings.length - 1];
21+
for (int i = ratings.length - 2; i >= 0; i--) {
22+
if (ratings[i] > ratings[i + 1]) {
23+
candys[i] = Math.max(candys[i], candys[i + 1] + 1);
24+
}
25+
sum += candys[i];
26+
}
27+
return sum;
4528
}
4629
}
4730

4831
public static void main(String[] args) {
4932
Solution solution = new Solution();
50-
String s = solution.getHint("1123", "0111");
51-
System.out.println(s);
33+
System.out.println(solution.candy(new int[] {1, 3, 2, 2, 1}));
34+
// System.out.println(solution.candy(new int[] {1, 2, 2}));
5235
}
5336
}

0 commit comments

Comments
 (0)