- Notifications
You must be signed in to change notification settings - Fork 97
Added tasks 517, 518, 519 #213
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
2 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
24 changes: 24 additions & 0 deletions 24 src/main/java/g0501_0600/s0517_super_washing_machines/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,24 @@ | ||
package g0501_0600.s0517_super_washing_machines; | ||
| ||
// #Hard #Array #Greedy | ||
| ||
public class Solution { | ||
// Reference: https://discuss.leetcode.com/topic/79938/super-short-easy-java-o-n-solution | ||
public int findMinMoves(int[] machines) { | ||
int total = 0; | ||
for (int i : machines) { | ||
total += i; | ||
} | ||
if (total % machines.length != 0) { | ||
return -1; | ||
} | ||
int avg = total / machines.length; | ||
int cnt = 0; | ||
int max = 0; | ||
for (int load : machines) { | ||
cnt += load - avg; | ||
max = Math.max(Math.max(max, Math.abs(cnt)), load - avg); | ||
} | ||
return max; | ||
} | ||
} |
39 changes: 39 additions & 0 deletions 39 src/main/java/g0501_0600/s0517_super_washing_machines/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,39 @@ | ||
517\. Super Washing Machines | ||
| ||
Hard | ||
| ||
You have `n` super washing machines on a line. Initially, each washing machine has some dresses or is empty. | ||
| ||
For each move, you could choose any `m` (`1 <= m <= n`) washing machines, and pass one dress of each washing machine to one of its adjacent washing machines at the same time. | ||
| ||
Given an integer array `machines` representing the number of dresses in each washing machine from left to right on the line, return _the minimum number of moves to make all the washing machines have the same number of dresses_. If it is not possible to do it, return `-1`. | ||
| ||
**Example 1:** | ||
| ||
**Input:** machines = [1,0,5] | ||
| ||
**Output:** 3 | ||
| ||
**Explanation:** 1st move: 1 0 <-- 5 => 1 1 4 2nd move: 1 <-- 1 <-- 4 => 2 1 3 3rd move: 2 1 <-- 3 => 2 2 2 | ||
| ||
**Example 2:** | ||
| ||
**Input:** machines = [0,3,0] | ||
| ||
**Output:** 2 | ||
| ||
**Explanation:** 1st move: 0 <-- 3 0 => 1 2 0 2nd move: 1 2 --> 0 => 1 1 1 | ||
| ||
**Example 3:** | ||
| ||
**Input:** machines = [0,2,0] | ||
| ||
**Output:** -1 | ||
| ||
**Explanation:** It's impossible to make all three washing machines have the same number of dresses. | ||
| ||
**Constraints:** | ||
| ||
* `n == machines.length` | ||
* <code>1 <= n <= 10<sup>4</sup></code> | ||
* <code>0 <= machines[i] <= 10<sup>5</sup></code> |
16 changes: 16 additions & 0 deletions 16 src/main/java/g0501_0600/s0518_coin_change_2/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.s0518_coin_change_2; | ||
| ||
// #Medium #Array #Dynamic_Programming | ||
| ||
public class Solution { | ||
public int change(int amount, int[] coins) { | ||
int[] dp = new int[amount + 1]; | ||
dp[0] = 1; | ||
for (int coin : coins) { | ||
for (int i = coin; i <= amount; i++) { | ||
dp[i] += dp[i - coin]; | ||
} | ||
} | ||
return dp[amount]; | ||
} | ||
} |
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 @@ | ||
518\. Coin Change 2 | ||
| ||
Medium | ||
| ||
You are given an integer array `coins` representing coins of different denominations and an integer `amount` representing a total amount of money. | ||
| ||
Return _the number of combinations that make up that amount_. If that amount of money cannot be made up by any combination of the coins, return `0`. | ||
| ||
You may assume that you have an infinite number of each kind of coin. | ||
| ||
The answer is **guaranteed** to fit into a signed **32-bit** integer. | ||
| ||
**Example 1:** | ||
| ||
**Input:** amount = 5, coins = [1,2,5] | ||
| ||
**Output:** 4 | ||
| ||
**Explanation:** there are four ways to make up the amount: 5=5 5=2+2+1 5=2+1+1+1 5=1+1+1+1+1 | ||
| ||
**Example 2:** | ||
| ||
**Input:** amount = 3, coins = [2] | ||
| ||
**Output:** 0 | ||
| ||
**Explanation:** the amount of 3 cannot be made up just with coins of 2. | ||
| ||
**Example 3:** | ||
| ||
**Input:** amount = 10, coins = [10] | ||
| ||
**Output:** 1 | ||
| ||
**Constraints:** | ||
| ||
* `1 <= coins.length <= 300` | ||
* `1 <= coins[i] <= 5000` | ||
* All the values of `coins` are **unique**. | ||
* `0 <= amount <= 5000` |
36 changes: 36 additions & 0 deletions 36 src/main/java/g0501_0600/s0519_random_flip_matrix/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,36 @@ | ||
package g0501_0600.s0519_random_flip_matrix; | ||
| ||
// #Medium #Hash_Table #Math #Randomized #Reservoir_Sampling | ||
| ||
import java.security.SecureRandom; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
| ||
public class Solution { | ||
| ||
private final int cols; | ||
private final int total; | ||
private final SecureRandom rand; | ||
private final Set<Integer> available; | ||
| ||
public Solution(int nRows, int nCols) { | ||
this.cols = nCols; | ||
this.rand = new SecureRandom(); | ||
this.available = new HashSet<>(); | ||
this.total = nRows * this.cols; | ||
} | ||
| ||
public int[] flip() { | ||
int x = rand.nextInt(this.total); | ||
while (available.contains(x)) { | ||
x = rand.nextInt(this.total); | ||
} | ||
| ||
this.available.add(x); | ||
return new int[] {x / this.cols, x % this.cols}; | ||
} | ||
| ||
public void reset() { | ||
this.available.clear(); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions 27 src/main/java/g0501_0600/s0519_random_flip_matrix/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 @@ | ||
519\. Random Flip Matrix | ||
| ||
Medium | ||
| ||
There is an `m x n` binary grid `matrix` with all the values set `0` initially. Design an algorithm to randomly pick an index `(i, j)` where `matrix[i][j] == 0` and flips it to `1`. All the indices `(i, j)` where `matrix[i][j] == 0` should be equally likely to be returned. | ||
| ||
Optimize your algorithm to minimize the number of calls made to the **built-in** random function of your language and optimize the time and space complexity. | ||
| ||
Implement the `Solution` class: | ||
| ||
* `Solution(int m, int n)` Initializes the object with the size of the binary matrix `m` and `n`. | ||
* `int[] flip()` Returns a random index `[i, j]` of the matrix where `matrix[i][j] == 0` and flips it to `1`. | ||
* `void reset()` Resets all the values of the matrix to be `0`. | ||
| ||
**Example 1:** | ||
| ||
**Input** ["Solution", "flip", "flip", "flip", "reset", "flip"] [[3, 1], [], [], [], [], []] | ||
| ||
**Output:** [null, [1, 0], [2, 0], [0, 0], null, [2, 0]] | ||
| ||
**Explanation:** Solution solution = new Solution(3, 1); solution.flip(); // return [1, 0], [0,0], [1,0], and [2,0] should be equally likely to be returned. solution.flip(); // return [2, 0], Since [1,0] was returned, [2,0] and [0,0] solution.flip(); // return [0, 0], Based on the previously returned indices, only [0,0] can be returned. solution.reset(); // All the values are reset to 0 and can be returned. solution.flip(); // return [2, 0], [0,0], [1,0], and [2,0] should be equally likely to be returned. | ||
| ||
**Constraints:** | ||
| ||
* <code>1 <= m, n <= 10<sup>4</sup></code> | ||
* There will be at least one free cell for each call to `flip`. | ||
* At most `1000` calls will be made to `flip` and `reset`. |
18 changes: 18 additions & 0 deletions 18 src/test/java/g0501_0600/s0517_super_washing_machines/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.s0517_super_washing_machines; | ||
| ||
import static org.hamcrest.CoreMatchers.equalTo; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
| ||
import org.junit.jupiter.api.Test; | ||
| ||
class SolutionTest { | ||
@Test | ||
void findMinMoves() { | ||
assertThat(new Solution().findMinMoves(new int[] {1, 0, 5}), equalTo(3)); | ||
} | ||
| ||
@Test | ||
void findMinMoves2() { | ||
assertThat(new Solution().findMinMoves(new int[] {0, 3, 0}), equalTo(2)); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions 23 src/test/java/g0501_0600/s0518_coin_change_2/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.s0518_coin_change_2; | ||
| ||
import static org.hamcrest.CoreMatchers.equalTo; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
| ||
import org.junit.jupiter.api.Test; | ||
| ||
class SolutionTest { | ||
@Test | ||
void change() { | ||
assertThat(new Solution().change(5, new int[] {1, 2, 5}), equalTo(4)); | ||
} | ||
| ||
@Test | ||
void change2() { | ||
assertThat(new Solution().change(3, new int[] {2}), equalTo(0)); | ||
} | ||
| ||
@Test | ||
void change3() { | ||
assertThat(new Solution().change(10, new int[] {10}), equalTo(1)); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions 20 src/test/java/g0501_0600/s0519_random_flip_matrix/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,20 @@ | ||
package g0501_0600.s0519_random_flip_matrix; | ||
| ||
import static org.hamcrest.CoreMatchers.equalTo; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
| ||
import com_github_leetcode.CommonUtils; | ||
import org.junit.jupiter.api.Test; | ||
| ||
class SolutionTest { | ||
@Test | ||
void solutionTest() { | ||
Solution solution = new Solution(3, 1); | ||
CommonUtils.printArray(solution.flip()); | ||
CommonUtils.printArray(solution.flip()); | ||
CommonUtils.printArray(solution.flip()); | ||
solution.reset(); | ||
CommonUtils.printArray(solution.flip()); | ||
assertThat(true, equalTo(true)); | ||
} | ||
} |
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.