Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Added tasks 517, 518, 519
  • Loading branch information
ThanhNIT committed Jan 3, 2022
commit 2d287ebed6d172309b0a2f046b456aa04c198230
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; // load-avg is "gain/lose"
max = Math.max(Math.max(max, Math.abs(cnt)), load - avg);
}
return max;
}
}
39 changes: 39 additions & 0 deletions src/main/java/g0501_0600/s0517_super_washing_machines/readme.md
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 src/main/java/g0501_0600/s0518_coin_change_2/Solution.java
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];
}
}
40 changes: 40 additions & 0 deletions src/main/java/g0501_0600/s0518_coin_change_2/readme.md
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 src/main/java/g0501_0600/s0519_random_flip_matrix/Solution.java
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 src/main/java/g0501_0600/s0519_random_flip_matrix/readme.md
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.
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.


**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`.
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 src/test/java/g0501_0600/s0518_coin_change_2/SolutionTest.java
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));
}
}
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));
}
}