Skip to content

Commit 81674a1

Browse files
authored
Added tasks 546, 547, 561.
1 parent 49b66f3 commit 81674a1

File tree

9 files changed

+253
-0
lines changed

9 files changed

+253
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package g0501_0600.s0546_remove_boxes;
2+
3+
// #Hard #Array #Dynamic_Programming #Memoization
4+
5+
public class Solution {
6+
private Integer[][][] dp;
7+
8+
public int removeBoxes(int[] boxes) {
9+
int n = boxes.length;
10+
dp = new Integer[n][n][n + 1];
11+
return helper(boxes, 0, n - 1, 0);
12+
}
13+
14+
private int helper(int[] boxes, int left, int right, int count) {
15+
if (left > right) {
16+
return 0;
17+
}
18+
if (dp[left][right][count] != null) {
19+
return dp[left][right][count];
20+
}
21+
int leftDash = left;
22+
int countDash = count;
23+
while (leftDash < right && boxes[leftDash] == boxes[leftDash + 1]) {
24+
leftDash++;
25+
countDash++;
26+
}
27+
int ans = 0;
28+
ans = helper(boxes, leftDash + 1, right, 0) + (countDash + 1) * (countDash + 1);
29+
for (int i = leftDash + 1; i <= right; i++) {
30+
if (boxes[i] == boxes[leftDash]) {
31+
int temp =
32+
helper(boxes, leftDash + 1, i - 1, 0)
33+
+ helper(boxes, i, right, countDash + 1);
34+
ans = Math.max(temp, ans);
35+
}
36+
}
37+
dp[left][right][count] = ans;
38+
return ans;
39+
}
40+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
546\. Remove Boxes
2+
3+
Hard
4+
5+
You are given several `boxes` with different colors represented by different positive numbers.
6+
7+
You may experience several rounds to remove boxes until there is no box left. Each time you can choose some continuous boxes with the same color (i.e., composed of `k` boxes, `k >= 1`), remove them and get `k * k` points.
8+
9+
Return _the maximum points you can get_.
10+
11+
**Example 1:**
12+
13+
**Input:** boxes = [1,3,2,2,2,3,4,3,1]
14+
15+
**Output:** 23
16+
17+
**Explanation:** [1, 3, 2, 2, 2, 3, 4, 3, 1] ----> [1, 3, 3, 4, 3, 1] (3\*3=9 points) ----> [1, 3, 3, 3, 1] (1\*1=1 points) ----> [1, 1] (3\*3=9 points) ----> [] (2\*2=4 points)
18+
19+
**Example 2:**
20+
21+
**Input:** boxes = [1,1,1]
22+
23+
**Output:** 9
24+
25+
**Example 3:**
26+
27+
**Input:** boxes = [1]
28+
29+
**Output:** 1
30+
31+
**Constraints:**
32+
33+
* `1 <= boxes.length <= 100`
34+
* `1 <= boxes[i] <= 100`
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package g0501_0600.s0547_number_of_provinces;
2+
3+
import java.util.Arrays;
4+
5+
public class Solution {
6+
public int findCircleNum(int[][] arr) {
7+
int[] parent = new int[arr.length];
8+
Arrays.fill(parent, -1);
9+
int ans = 0;
10+
for (int i = 0; i < arr.length - 1; i++) {
11+
for (int j = i + 1; j < arr[i].length; j++) {
12+
if (arr[i][j] == 1) {
13+
ans += union(i, j, parent);
14+
}
15+
}
16+
}
17+
return arr.length - ans;
18+
}
19+
20+
private int union(int a, int b, int[] arr) {
21+
int ga = find(a, arr);
22+
int gb = find(b, arr);
23+
if (ga != gb) {
24+
arr[gb] = ga;
25+
return 1;
26+
}
27+
return 0;
28+
}
29+
30+
private int find(int a, int[] arr) {
31+
if (arr[a] == -1) {
32+
return a;
33+
}
34+
arr[a] = find(arr[a], arr);
35+
return arr[a];
36+
}
37+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
547\. Number of Provinces
2+
3+
Medium
4+
5+
There are `n` cities. Some of them are connected, while some are not. If city `a` is connected directly with city `b`, and city `b` is connected directly with city `c`, then city `a` is connected indirectly with city `c`.
6+
7+
A **province** is a group of directly or indirectly connected cities and no other cities outside of the group.
8+
9+
You are given an `n x n` matrix `isConnected` where `isConnected[i][j] = 1` if the <code>i<sup>th</sup></code> city and the <code>j<sup>th</sup></code> city are directly connected, and `isConnected[i][j] = 0` otherwise.
10+
11+
Return _the total number of **provinces**_.
12+
13+
**Example 1:**
14+
15+
![](https://assets.leetcode.com/uploads/2020/12/24/graph1.jpg)
16+
17+
**Input:** isConnected = [[1,1,0],[1,1,0],[0,0,1]]
18+
19+
**Output:** 2
20+
21+
**Example 2:**
22+
23+
![](https://assets.leetcode.com/uploads/2020/12/24/graph2.jpg)
24+
25+
**Input:** isConnected = [[1,0,0],[0,1,0],[0,0,1]]
26+
27+
**Output:** 3
28+
29+
**Constraints:**
30+
31+
* `1 <= n <= 200`
32+
* `n == isConnected.length`
33+
* `n == isConnected[i].length`
34+
* `isConnected[i][j]` is `1` or `0`.
35+
* `isConnected[i][i] == 1`
36+
* `isConnected[i][j] == isConnected[j][i]`
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package g0501_0600.s0561_array_partition_i;
2+
3+
// #Easy #Array #Sorting #Greedy #Counting_Sort
4+
5+
import java.util.Arrays;
6+
7+
public class Solution {
8+
public int arrayPairSum(int[] nums) {
9+
Arrays.sort(nums);
10+
int sum = 0;
11+
for (int i = 0; i < nums.length - 1; i = i + 2) {
12+
sum += Math.min(nums[i], nums[i + 1]);
13+
}
14+
return sum;
15+
}
16+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
561\. Array Partition I
2+
3+
Easy
4+
5+
Given an integer array `nums` of `2n` integers, group these integers into `n` pairs <code>(a<sub>1</sub>, b<sub>1</sub>), (a<sub>2</sub>, b<sub>2</sub>), ..., (a<sub>n</sub>, b<sub>n</sub>)</code> such that the sum of <code>min(a<sub>i</sub>, b<sub>i</sub>)</code> for all `i` is **maximized**. Return _the maximized sum_.
6+
7+
**Example 1:**
8+
9+
**Input:** nums = [1,4,3,2]
10+
11+
**Output:** 4
12+
13+
**Explanation:** All possible pairings (ignoring the ordering of elements) are: 1. (1, 4), (2, 3) -> min(1, 4) + min(2, 3) = 1 + 2 = 3 2. (1, 3), (2, 4) -> min(1, 3) + min(2, 4) = 1 + 2 = 3 3. (1, 2), (3, 4) -> min(1, 2) + min(3, 4) = 1 + 3 = 4 So the maximum possible sum is 4.
14+
15+
**Example 2:**
16+
17+
**Input:** nums = [6,2,6,5,1,2]
18+
19+
**Output:** 9
20+
21+
**Explanation:** The optimal pairing is (2, 1), (2, 5), (6, 6). min(2, 1) + min(2, 5) + min(6, 6) = 1 + 2 + 6 = 9.
22+
23+
**Constraints:**
24+
25+
* <code>1 <= n <= 10<sup>4</sup></code>
26+
* `nums.length == 2 * n`
27+
* <code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g0501_0600.s0546_remove_boxes;
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 removeBoxes() {
11+
assertThat(new Solution().removeBoxes(new int[] {1, 3, 2, 2, 2, 3, 4, 3, 1}), equalTo(23));
12+
}
13+
14+
@Test
15+
void removeBoxes2() {
16+
assertThat(new Solution().removeBoxes(new int[] {1, 1, 1}), equalTo(9));
17+
}
18+
19+
@Test
20+
void removeBoxes3() {
21+
assertThat(new Solution().removeBoxes(new int[] {1}), equalTo(1));
22+
}
23+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package g0501_0600.s0547_number_of_provinces;
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 findCircleNum() {
11+
assertThat(
12+
new Solution().findCircleNum(new int[][] {{1, 1, 0}, {1, 1, 0}, {0, 0, 1}}),
13+
equalTo(2));
14+
}
15+
16+
@Test
17+
void findCircleNum2() {
18+
assertThat(
19+
new Solution().findCircleNum(new int[][] {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}}),
20+
equalTo(3));
21+
}
22+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g0501_0600.s0561_array_partition_i;
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 arrayPairSum() {
11+
assertThat(new Solution().arrayPairSum(new int[] {1, 4, 3, 2}), equalTo(4));
12+
}
13+
14+
@Test
15+
void arrayPairSum2() {
16+
assertThat(new Solution().arrayPairSum(new int[] {6, 2, 6, 5, 1, 2}), equalTo(9));
17+
}
18+
}

0 commit comments

Comments
 (0)