Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions src/main/java/g0501_0600/s0546_remove_boxes/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package g0501_0600.s0546_remove_boxes;

// #Hard #Array #Dynamic_Programming #Memoization

public class Solution {
private Integer[][][] dp;

public int removeBoxes(int[] boxes) {
int n = boxes.length;
dp = new Integer[n][n][n + 1];
return helper(boxes, 0, n - 1, 0);
}

private int helper(int[] boxes, int left, int right, int count) {
if (left > right) {
return 0;
}
if (dp[left][right][count] != null) {
return dp[left][right][count];
}
int leftDash = left;
int countDash = count;
while (leftDash < right && boxes[leftDash] == boxes[leftDash + 1]) {
leftDash++;
countDash++;
}
int ans = 0;
ans = helper(boxes, leftDash + 1, right, 0) + (countDash + 1) * (countDash + 1);
for (int i = leftDash + 1; i <= right; i++) {
if (boxes[i] == boxes[leftDash]) {
int temp =
helper(boxes, leftDash + 1, i - 1, 0)
+ helper(boxes, i, right, countDash + 1);
ans = Math.max(temp, ans);
}
}
dp[left][right][count] = ans;
return ans;
}
}
34 changes: 34 additions & 0 deletions src/main/java/g0501_0600/s0546_remove_boxes/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
546\. Remove Boxes

Hard

You are given several `boxes` with different colors represented by different positive numbers.

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.

Return _the maximum points you can get_.

**Example 1:**

**Input:** boxes = [1,3,2,2,2,3,4,3,1]

**Output:** 23

**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)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like a multi line block.


**Example 2:**

**Input:** boxes = [1,1,1]

**Output:** 9

**Example 3:**

**Input:** boxes = [1]

**Output:** 1

**Constraints:**

* `1 <= boxes.length <= 100`
* `1 <= boxes[i] <= 100`
37 changes: 37 additions & 0 deletions src/main/java/g0501_0600/s0547_number_of_provinces/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package g0501_0600.s0547_number_of_provinces;

import java.util.Arrays;

public class Solution {
public int findCircleNum(int[][] arr) {
int[] parent = new int[arr.length];
Arrays.fill(parent, -1);
int ans = 0;
for (int i = 0; i < arr.length - 1; i++) {
for (int j = i + 1; j < arr[i].length; j++) {
if (arr[i][j] == 1) {
ans += union(i, j, parent);
}
}
}
return arr.length - ans;
}

private int union(int a, int b, int[] arr) {
int ga = find(a, arr);
int gb = find(b, arr);
if (ga != gb) {
arr[gb] = ga;
return 1;
}
return 0;
}

private int find(int a, int[] arr) {
if (arr[a] == -1) {
return a;
}
arr[a] = find(arr[a], arr);
return arr[a];
}
}
36 changes: 36 additions & 0 deletions src/main/java/g0501_0600/s0547_number_of_provinces/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
547\. Number of Provinces

Medium

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`.

A **province** is a group of directly or indirectly connected cities and no other cities outside of the group.

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.

Return _the total number of **provinces**_.

**Example 1:**

![](https://assets.leetcode.com/uploads/2020/12/24/graph1.jpg)

**Input:** isConnected = [[1,1,0],[1,1,0],[0,0,1]]

**Output:** 2

**Example 2:**

![](https://assets.leetcode.com/uploads/2020/12/24/graph2.jpg)

**Input:** isConnected = [[1,0,0],[0,1,0],[0,0,1]]

**Output:** 3

**Constraints:**

* `1 <= n <= 200`
* `n == isConnected.length`
* `n == isConnected[i].length`
* `isConnected[i][j]` is `1` or `0`.
* `isConnected[i][i] == 1`
* `isConnected[i][j] == isConnected[j][i]`
16 changes: 16 additions & 0 deletions src/main/java/g0501_0600/s0561_array_partition_i/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package g0501_0600.s0561_array_partition_i;

// #Easy #Array #Sorting #Greedy #Counting_Sort

import java.util.Arrays;

public class Solution {
public int arrayPairSum(int[] nums) {
Arrays.sort(nums);
int sum = 0;
for (int i = 0; i < nums.length - 1; i = i + 2) {
sum += Math.min(nums[i], nums[i + 1]);
}
return sum;
}
}
27 changes: 27 additions & 0 deletions src/main/java/g0501_0600/s0561_array_partition_i/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
561\. Array Partition I

Easy

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_.

**Example 1:**

**Input:** nums = [1,4,3,2]

**Output:** 4

**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.

**Example 2:**

**Input:** nums = [6,2,6,5,1,2]

**Output:** 9

**Explanation:** The optimal pairing is (2, 1), (2, 5), (6, 6). min(2, 1) + min(2, 5) + min(6, 6) = 1 + 2 + 6 = 9.

**Constraints:**

* <code>1 <= n <= 10<sup>4</sup></code>
* `nums.length == 2 * n`
* <code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code>
23 changes: 23 additions & 0 deletions src/test/java/g0501_0600/s0546_remove_boxes/SolutionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package g0501_0600.s0546_remove_boxes;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;

import org.junit.jupiter.api.Test;

class SolutionTest {
@Test
void removeBoxes() {
assertThat(new Solution().removeBoxes(new int[] {1, 3, 2, 2, 2, 3, 4, 3, 1}), equalTo(23));
}

@Test
void removeBoxes2() {
assertThat(new Solution().removeBoxes(new int[] {1, 1, 1}), equalTo(9));
}

@Test
void removeBoxes3() {
assertThat(new Solution().removeBoxes(new int[] {1}), equalTo(1));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package g0501_0600.s0547_number_of_provinces;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;

import org.junit.jupiter.api.Test;

class SolutionTest {
@Test
void findCircleNum() {
assertThat(
new Solution().findCircleNum(new int[][] {{1, 1, 0}, {1, 1, 0}, {0, 0, 1}}),
equalTo(2));
}

@Test
void findCircleNum2() {
assertThat(
new Solution().findCircleNum(new int[][] {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}}),
equalTo(3));
}
}
18 changes: 18 additions & 0 deletions src/test/java/g0501_0600/s0561_array_partition_i/SolutionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package g0501_0600.s0561_array_partition_i;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;

import org.junit.jupiter.api.Test;

class SolutionTest {
@Test
void arrayPairSum() {
assertThat(new Solution().arrayPairSum(new int[] {1, 4, 3, 2}), equalTo(4));
}

@Test
void arrayPairSum2() {
assertThat(new Solution().arrayPairSum(new int[] {6, 2, 6, 5, 1, 2}), equalTo(9));
}
}