Skip to content

Commit 90ccae9

Browse files
solves #135: Candy in java
1 parent 2cc35cf commit 90ccae9

File tree

3 files changed

+41
-30
lines changed

3 files changed

+41
-30
lines changed

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@
125125
| 131 | [Palindrome Partitioning](https://leetcode.com/problems/palindrome-partitioning) | [![Java](assets/java.png)](src/PalindromePartitioning.java) | |
126126
| 133 | [Clone Graph](https://leetcode.com/problems/clone-graph) | [![Java](assets/java.png)](src/CloneGraph.java) [![Python](assets/python.png)](python/clone_graph.py) | |
127127
| 134 | [Gas Station](https://leetcode.com/problems/gas-station) | [![Java](assets/java.png)](src/GasStation.java) | |
128+
| 135 | [Candy](https://leetcode.com/problems/candy) | [![Java](assets/java.png)](src/Candy.java) | |
128129
| 136 | [Single Number](https://leetcode.com/problems/single-number) | [![Java](assets/java.png)](src/SingleNumber.java) [![Python](assets/python.png)](python/single_number.py) | |
129130
| 137 | [Single Number II](https://leetcode.com/problems/single-number-ii) | [![Java](assets/java.png)](src/SingleNumberII.java) | |
130131
| 138 | [Copy List with Random Pointer](https://leetcode.com/problems/copy-list-with-random-pointer) | [![Java](assets/java.png)](src/CopyListWithRandomPointer.java) | |

Diff for: src/Candy.java

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// https://leetcode.com/problems/candy
2+
// T: O(N)
3+
// S: O(N)
4+
5+
import java.util.Arrays;
6+
7+
public class Candy {
8+
public int candy(int[] ratings) {
9+
final int[] candies = new int[ratings.length];
10+
11+
// left pass
12+
for (int i = 1 ; i < ratings.length ; i++) {
13+
if (ratings[i] > ratings[i - 1]) {
14+
candies[i] = candies[i - 1] + 1;
15+
}
16+
}
17+
18+
// right pass
19+
for (int i = ratings.length - 2 ; i >= 0 ; i--) {
20+
if (ratings[i] > ratings[i + 1]) {
21+
candies[i] = Math.max(candies[i], candies[i + 1] + 1);
22+
}
23+
}
24+
25+
return Arrays.stream(candies).sum() + ratings.length;
26+
}
27+
}

Diff for: src/HelloWorld.java

+13-30
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,22 @@
1-
import java.util.*;
1+
import java.util.Arrays;
22

33
public class HelloWorld {
4-
static class RandomizedSet {
5-
private final Random random = new Random();
6-
private final List<Integer> list = new ArrayList<>();
7-
private final Map<Integer, Integer> indexMapping = new HashMap<>();
4+
public int canCompleteCircuit(int[] gas, int[] cost) {
5+
final int totalGas = Arrays.stream(gas).sum();
6+
final int totalCost = Arrays.stream(cost).sum();
87

9-
public boolean insert(int val) {
10-
if (indexMapping.containsKey(val)) {
11-
return false;
12-
}
13-
list.add(val);
14-
indexMapping.put(val, list.size() - 1);
15-
return true;
16-
}
17-
18-
private int last() {
19-
return this.list.getLast();
8+
if (totalCost > totalGas) {
9+
return -1;
2010
}
2111

22-
public boolean remove(int val) {
23-
if (!indexMapping.containsKey(val)) {
24-
return false;
12+
int startingIndex = 0;
13+
for (int i = 0, currentGas = 0 ; i < cost.length ; i++) {
14+
currentGas += gas[i] - cost[i];
15+
if (currentGas < 0) {
16+
currentGas = 0;
17+
startingIndex = i + 1;
2518
}
26-
final int index = indexMapping.get(val);
27-
indexMapping.put(last(), index);
28-
indexMapping.remove(val);
29-
list.set(index, last());
30-
list.removeLast();
31-
return true;
32-
}
33-
34-
public int getRandom() {
35-
final int randomIndex = random.nextInt(list.size());
36-
return list.get(randomIndex);
3719
}
20+
return startingIndex;
3821
}
3922
}

0 commit comments

Comments
 (0)