Skip to content

Commit 72ca041

Browse files
solves rings and rods
1 parent d31e2ce commit 72ca041

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -498,4 +498,4 @@
498498
| 2089 | [Find Target Indices After Sorting Array](https://leetcode.com/problems/find-target-indices-after-sorting-array) | [![Java](assets/java.png)](src/FindTargetIndicesAfterSortingArray.java) | |
499499
| 2094 | [Finding 3-Digit Even Numbers](https://leetcode.com/problems/finding-3-digit-even-numbers) | [![Java](assets/java.png)](src/Finding3DigitEvenNumbers.java) | |
500500
| 2099 | [Find Subsequence of Length K With the Largest Sum](https://leetcode.com/problems/find-subsequence-of-length-k-with-the-largest-sum) | [![Java](assets/java.png)](src/FindSubsequenceOfLengthKWithTheLargestSum.java) | |
501-
| 2103 | [Rings and Rods](https://leetcode.com/problems/rings-and-rods) | | |
501+
| 2103 | [Rings and Rods](https://leetcode.com/problems/rings-and-rods) | [![Java](assets/java.png)](src/RingsAndRods.java) | |

src/RingsAndRods.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// https://leetcode.com/problems/rings-and-rods
2+
// T: O(|rings|)
3+
// S: O(1)
4+
5+
public class RingsAndRods {
6+
public int countPoints(String rings) {
7+
final boolean[][] rods = new boolean[10][3];
8+
for (int i = 0 ; i < rings.length() ; i += 2) {
9+
rods[rings.charAt(i + 1) - '0'][toColorIndex(rings.charAt(i))] = true;
10+
}
11+
int points = 0;
12+
for (boolean[] rod : rods) {
13+
if (containsAllColors(rod)) {
14+
points++;
15+
}
16+
}
17+
return points;
18+
}
19+
20+
private int toColorIndex(char color) {
21+
return switch (color) {
22+
case 'R' -> 0;
23+
case 'G' -> 1;
24+
case 'B' -> 2;
25+
default -> -1;
26+
};
27+
}
28+
29+
private boolean containsAllColors(boolean[] rod) {
30+
return rod[0] && rod[1] && rod[2];
31+
}
32+
}

0 commit comments

Comments
 (0)