Skip to content

Commit 547196b

Browse files
authored
Added tasks 575, 576.
1 parent 17e9f94 commit 547196b

File tree

6 files changed

+168
-0
lines changed

6 files changed

+168
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package g0501_0600.s0575_distribute_candies;
2+
3+
// #Easy #Array #Hash_Table
4+
5+
import java.util.HashSet;
6+
import java.util.Set;
7+
8+
public class Solution {
9+
public int distributeCandies(int[] candyType) {
10+
Set<Integer> set = new HashSet<>();
11+
for (int i : candyType) {
12+
set.add(i);
13+
}
14+
return Math.min(set.size(), candyType.length / 2);
15+
}
16+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
575\. Distribute Candies
2+
3+
Easy
4+
5+
Alice has `n` candies, where the <code>i<sup>th</sup></code> candy is of type `candyType[i]`. Alice noticed that she started to gain weight, so she visited a doctor.
6+
7+
The doctor advised Alice to only eat `n / 2` of the candies she has (`n` is always even). Alice likes her candies very much, and she wants to eat the maximum number of different types of candies while still following the doctor's advice.
8+
9+
Given the integer array `candyType` of length `n`, return _the **maximum** number of different types of candies she can eat if she only eats_ `n / 2` _of them_.
10+
11+
**Example 1:**
12+
13+
**Input:** candyType = [1,1,2,2,3,3]
14+
15+
**Output:** 3
16+
17+
**Explanation:** Alice can only eat 6 / 2 = 3 candies. Since there are only 3 types, she can eat one of each type.
18+
19+
**Example 2:**
20+
21+
**Input:** candyType = [1,1,2,3]
22+
23+
**Output:** 2
24+
25+
**Explanation:** Alice can only eat 4 / 2 = 2 candies. Whether she eats types [1,2], [1,3], or [2,3], she still can only eat 2 different types.
26+
27+
**Example 3:**
28+
29+
**Input:** candyType = [6,6,6,6]
30+
31+
**Output:** 1
32+
33+
**Explanation:** Alice can only eat 4 / 2 = 2 candies. Even though she can eat 2 candies, she only has 1 type.
34+
35+
**Constraints:**
36+
37+
* `n == candyType.length`
38+
* <code>2 <= n <= 10<sup>4</sup></code>
39+
* `n` is even.
40+
* <code>-10<sup>5</sup> <= candyType[i] <= 10<sup>5</sup></code>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package g0501_0600.s0576_out_of_boundary_paths;
2+
3+
// #Medium #Dynamic_Programming
4+
5+
import java.util.Arrays;
6+
7+
public class Solution {
8+
private final int[][] dRowCol = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
9+
10+
private int dfs(int m, int n, int remainingMoves, int currRow, int currCol, int[][][] cache) {
11+
if (currRow < 0 || currRow == m || currCol < 0 || currCol == n) {
12+
return 1;
13+
}
14+
if (remainingMoves == 0) {
15+
return 0;
16+
}
17+
18+
if (cache[currRow][currCol][remainingMoves] == -1) {
19+
int paths = 0;
20+
for (int i = 0; i < 4; i++) {
21+
int newRow = currRow + dRowCol[i][0];
22+
int newCol = currCol + dRowCol[i][1];
23+
int m1 = 1000000007;
24+
paths = (paths + dfs(m, n, remainingMoves - 1, newRow, newCol, cache)) % m1;
25+
}
26+
cache[currRow][currCol][remainingMoves] = paths;
27+
}
28+
return cache[currRow][currCol][remainingMoves];
29+
}
30+
31+
public int findPaths(int m, int n, int maxMoves, int startRow, int startCol) {
32+
int[][][] cache = new int[m][n][maxMoves + 1];
33+
for (int[][] c1 : cache) {
34+
for (int[] c2 : c1) {
35+
Arrays.fill(c2, -1);
36+
}
37+
}
38+
39+
return dfs(m, n, maxMoves, startRow, startCol, cache);
40+
}
41+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
576\. Out of Boundary Paths
2+
3+
Medium
4+
5+
There is an `m x n` grid with a ball. The ball is initially at the position `[startRow, startColumn]`. You are allowed to move the ball to one of the four adjacent cells in the grid (possibly out of the grid crossing the grid boundary). You can apply **at most** `maxMove` moves to the ball.
6+
7+
Given the five integers `m`, `n`, `maxMove`, `startRow`, `startColumn`, return the number of paths to move the ball out of the grid boundary. Since the answer can be very large, return it **modulo** <code>10<sup>9</sup> + 7</code>.
8+
9+
**Example 1:**
10+
11+
![](https://assets.leetcode.com/uploads/2021/04/28/out_of_boundary_paths_1.png)
12+
13+
**Input:** m = 2, n = 2, maxMove = 2, startRow = 0, startColumn = 0
14+
15+
**Output:** 6
16+
17+
**Example 2:**
18+
19+
![](https://assets.leetcode.com/uploads/2021/04/28/out_of_boundary_paths_2.png)
20+
21+
**Input:** m = 1, n = 3, maxMove = 3, startRow = 0, startColumn = 1
22+
23+
**Output:** 12
24+
25+
**Constraints:**
26+
27+
* `1 <= m, n <= 50`
28+
* `0 <= maxMove <= 50`
29+
* `0 <= startRow < m`
30+
* `0 <= startColumn < n`
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g0501_0600.s0575_distribute_candies;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void distributeCandies() {
11+
assertThat(new Solution().distributeCandies(new int[] {1, 1, 2, 2, 3, 3}), equalTo(3));
12+
}
13+
14+
@Test
15+
void distributeCandies2() {
16+
assertThat(new Solution().distributeCandies(new int[] {1, 1, 2, 3}), equalTo(2));
17+
}
18+
19+
@Test
20+
void distributeCandies3() {
21+
assertThat(new Solution().distributeCandies(new int[] {6, 6, 6, 6}), equalTo(1));
22+
}
23+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g0501_0600.s0576_out_of_boundary_paths;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void findPaths() {
11+
assertThat(new Solution().findPaths(2, 2, 2, 0, 0), equalTo(6));
12+
}
13+
14+
@Test
15+
void findPaths2() {
16+
assertThat(new Solution().findPaths(1, 3, 3, 0, 1), equalTo(12));
17+
}
18+
}

0 commit comments

Comments
 (0)