- Notifications
You must be signed in to change notification settings - Fork 97
Added tasks 546, 547, 561 #224
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits Select commit Hold shift + click to select a range
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
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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) | ||
| ||
**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 37 src/main/java/g0501_0600/s0547_number_of_provinces/Solution.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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 36 src/main/java/g0501_0600/s0547_number_of_provinces/readme.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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:** | ||
| ||
 | ||
| ||
**Input:** isConnected = [[1,1,0],[1,1,0],[0,0,1]] | ||
| ||
**Output:** 2 | ||
| ||
**Example 2:** | ||
| ||
 | ||
| ||
**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 16 src/main/java/g0501_0600/s0561_array_partition_i/Solution.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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 27 src/main/java/g0501_0600/s0561_array_partition_i/readme.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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 23 src/test/java/g0501_0600/s0546_remove_boxes/SolutionTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions 22 src/test/java/g0501_0600/s0547_number_of_provinces/SolutionTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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 18 src/test/java/g0501_0600/s0561_array_partition_i/SolutionTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.