Skip to content

Commit 697e942

Browse files
author
Aarzoo
committed
add solutions, explanation and update README file of the day 2064
1 parent a56e824 commit 697e942

File tree

8 files changed

+315
-0
lines changed

8 files changed

+315
-0
lines changed

Algorithms/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
| 2022 | Convert 1D Array Into 2D Array | [C++](./Algorithms/src/2022.%20Convert%201D%20Array%20Into%202D%20Array/Code/solution.cpp), [Java](./Algorithms/src/2022.%20Convert%201D%20Array%20Into%202D%20Array/Code/solution.java), [JavaScript](./Algorithms/src/2022.%20Convert%201D%20Array%20Into%202D%20Array/Code/solution.js), [Python](./Algorithms/src/2022.%20Convert%201D%20Array%20Into%202D%20Array/Code/solution.py), [Go](./Algorithms/src/2022.%20Convert%201D%20Array%20Into%202D%20Array/Code/solution.go) | [Explanation](./Algorithms/src/2022.%20Convert%201D%20Array%20Into%202D%20Array/Explanation/explanation.md) | Easy |
5454
| 2044 | Count Number of Maximum Bitwise-OR Subsets | [C++](./Algorithms/src/2044.%20Count%20Number%20of%20Maximum%20Bitwise-OR%20Subsets/Code/solution.cpp), [Java](./Algorithms/src/2044.%20Count%20Number%20of%20Maximum%20Bitwise-OR%20Subsets/Code/solution.java), [JavaScript](./Algorithms/src/2044.%20Count%20Number%20of%20Maximum%20Bitwise-OR%20Subsets/Code/solution.js), [Python](./Algorithms/src/2044.%20Count%20Number%20of%20Maximum%20Bitwise-OR%20Subsets/Code/solution.py), [Go](./Algorithms/src/2044.%20Count%20Number%20of%20Maximum%20Bitwise-OR%20Subsets/Code/solution.go) | [Explanation](./Algorithms/src/2044.%20Count%20Number%20of%20Maximum%20Bitwise-OR%20Subsets/Explanation/explanation.md) | Medium |
5555
| 2053 | Kth Distinct String in an Array | [C++](./Algorithms/src/2053.%20Kth%20Distinct%20String%20in%20an%20Array/Code/solution.cpp), [Java](./Algorithms/src/2053.%20Kth%20Distinct%20String%20in%20an%20Array/Code/solution.java), [JavaScript](./Algorithms/src/2053.%20Kth%20Distinct%20String%20in%20an%20Array/Code/solution.js), [Python](./Algorithms/src/2053.%20Kth%20Distinct%20String%20in%20an%20Array/Code/solution.py), [Go](./Algorithms/src/2053.%20Kth%20Distinct%20String%20in%20an%20Array/Code/solution.go) | [Explanation](./Algorithms/src/2053.%20Kth%20Distinct%20String%20in%20an%20Array/Explanation/explanation.md) | Easy |
56+
| 2064 | Minimized Maximum of Products Distributed to Any Store | [C++](./Algorithms/src/2064.%20Minimized%20Maximum%20of%20Products%20Distributed%20to%20Any%20Store/Code/solution.cpp), [Java](./Algorithms/src/2064.%20Minimized%20Maximum%20of%20Products%20Distributed%20to%20Any%20Store/Code/solution.java), [JavaScript](./Algorithms/src/2064.%20Minimized%20Maximum%20of%20Products%20Distributed%20to%20Any%20Store/Code/solution.js), [Python](./Algorithms/src/2064.%20Minimized%20Maximum%20of%20Products%20Distributed%20to%20Any%20Store/Code/solution.py), [Go](./Algorithms/src/2064.%20Minimized%20Maximum%20of%20Products%20Distributed%20to%20Any%20Store/Code/solution.go) | [Explanation](./Algorithms/src/2064.%20Minimized%20Maximum%20of%20Products%20Distributed%20to%20Any%20Store/Explanation/explanation.md) | Medium |
5657
| 2070 | Most Beautiful Item for Each Query | [C++](./Algorithms/src/2070.%20Most%20Beautiful%20Item%20for%20Each%20Query/Code/solution.cpp), [Java](./Algorithms/src/2070.%20Most%20Beautiful%20Item%20for%20Each%20Query/Code/solution.java), [JavaScript](./Algorithms/src/2070.%20Most%20Beautiful%20Item%20for%20Each%20Query/Code/solution.js), [Python](./Algorithms/src/2070.%20Most%20Beautiful%20Item%20for%20Each%20Query/Code/solution.py), [Go](./Algorithms/src/2070.%20Most%20Beautiful%20Item%20for%20Each%20Query/Code/solution.go) | [Explanation](./Algorithms/src/2070.%20Most%20Beautiful%20Item%20for%20Each%20Query/Explanation/explanation.md) | Medium |
5758
| 2134 | Minimum Swaps to Group All 1's Together II | [C++](./Algorithms/src/2134.%20Minimum%20Swaps%20to%20Group%20All%201's%20Together%20II/Code/solution.cpp), [Java](./Algorithms/src/2134.%20Minimum%20Swaps%20to%20Group%20All%201's%20Together%20II/Code/solution.java), [JavaScript](./Algorithms/src/2134.%20Minimum%20Swaps%20to%20Group%20All%201's%20Together%20II/Code/solution.js), [Python](./Algorithms/src/2134.%20Minimum%20Swaps%20to%20Group%20All%201's%20Together%20II/Code/solution.py), [Go](./Algorithms/src/2134.%20Minimum%20Swaps%20to%20Group%20All%201's%20Together%20II/Code/solution.go) | [Explanation](./Algorithms/src/2134.%20Minimum%20Swaps%20to%20Group%20All%201's%20Together%20II/Explanation/explanation.md) | Medium |
5859
| 2275 | Largest Combination With Bitwise AND Greater Than Zero | [C++](./Algorithms/src/2275.%20Largest%20Combination%20With%20Bitwise%20AND%20Greater%20Than%20Zero/Code/solution.cpp), [Java](./Algorithms/src/2275.%20Largest%20Combination%20With%20Bitwise%20AND%20Greater%20Than%20Zero/Code/solution.java), [JavaScript](./Algorithms/src/2275.%20Largest%20Combination%20With%20Bitwise%20AND%20Greater%20Than%20Zero/Code/solution.js), [Python](./Algorithms/src/2275.%20Largest%20Combination%20With%20Bitwise%20AND%20Greater%20Than%20Zero/Code/solution.py), [Go](./Algorithms/src/2275.%20Largest%20Combination%20With%20Bitwise%20AND%20Greater%20Than%20Zero/Code/solution.go) | [Explanation](./Algorithms/src/2275.%20Largest%20Combination%20With%20Bitwise%20AND%20Greater%20Than%20Zero/Explanation/explanation.md) | Medium |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include <vector>
2+
#include <cmath>
3+
#include <algorithm>
4+
5+
class Solution
6+
{
7+
public:
8+
bool canDistribute(const std::vector<int> &quantities, int maxProducts, int n)
9+
{
10+
int storesNeeded = 0;
11+
for (int quantity : quantities)
12+
{
13+
storesNeeded += std::ceil((double)quantity / maxProducts);
14+
if (storesNeeded > n)
15+
return false;
16+
}
17+
return storesNeeded <= n;
18+
}
19+
20+
int minimizedMaximum(int n, std::vector<int> &quantities)
21+
{
22+
int low = 1;
23+
int high = *std::max_element(quantities.begin(), quantities.end());
24+
int answer = high;
25+
26+
while (low <= high)
27+
{
28+
int mid = low + (high - low) / 2;
29+
if (canDistribute(quantities, mid, n))
30+
{
31+
answer = mid;
32+
high = mid - 1;
33+
}
34+
else
35+
{
36+
low = mid + 1;
37+
}
38+
}
39+
40+
return answer;
41+
}
42+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import "math"
2+
3+
func minimizedMaximum(n int, quantities []int) int {
4+
canDistribute := func(maxProducts int) bool {
5+
storesNeeded := 0
6+
for _, quantity := range quantities {
7+
storesNeeded += int(math.Ceil(float64(quantity) / float64(maxProducts)))
8+
if storesNeeded > n {
9+
return false
10+
}
11+
}
12+
return storesNeeded <= n
13+
}
14+
15+
low, high := 1, 0
16+
for _, quantity := range quantities {
17+
if quantity > high {
18+
high = quantity
19+
}
20+
}
21+
answer := high
22+
23+
for low <= high {
24+
mid := low + (high - low) / 2
25+
if canDistribute(mid) {
26+
answer = mid
27+
high = mid - 1
28+
} else {
29+
low = mid + 1
30+
}
31+
}
32+
33+
return answer
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
private boolean canDistribute(int[] quantities, int maxProducts, int n) {
3+
int storesNeeded = 0;
4+
for (int quantity : quantities) {
5+
storesNeeded += (int) Math.ceil((double) quantity / maxProducts);
6+
if (storesNeeded > n)
7+
return false;
8+
}
9+
return storesNeeded <= n;
10+
}
11+
12+
public int minimizedMaximum(int n, int[] quantities) {
13+
int low = 1;
14+
int high = Arrays.stream(quantities).max().getAsInt();
15+
int answer = high;
16+
17+
while (low <= high) {
18+
int mid = low + (high - low) / 2;
19+
if (canDistribute(quantities, mid, n)) {
20+
answer = mid;
21+
high = mid - 1;
22+
} else {
23+
low = mid + 1;
24+
}
25+
}
26+
27+
return answer;
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* @param {number} n
3+
* @param {number[]} quantities
4+
* @return {number}
5+
*/
6+
var minimizedMaximum = function (n, quantities) {
7+
const canDistribute = (maxProducts) => {
8+
let storesNeeded = 0;
9+
for (const quantity of quantities) {
10+
storesNeeded += Math.ceil(quantity / maxProducts);
11+
if (storesNeeded > n) return false;
12+
}
13+
return storesNeeded <= n;
14+
};
15+
16+
let low = 1;
17+
let high = Math.max(...quantities);
18+
let answer = high;
19+
20+
while (low <= high) {
21+
const mid = Math.floor((low + high) / 2);
22+
if (canDistribute(mid)) {
23+
answer = mid;
24+
high = mid - 1;
25+
} else {
26+
low = mid + 1;
27+
}
28+
}
29+
30+
return answer;
31+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import math
2+
from typing import List
3+
4+
class Solution:
5+
def minimizedMaximum(self, n: int, quantities: List[int]) -> int:
6+
def canDistribute(maxProducts):
7+
storesNeeded = 0
8+
for quantity in quantities:
9+
storesNeeded += math.ceil(quantity / maxProducts)
10+
if storesNeeded > n:
11+
return False
12+
return storesNeeded <= n
13+
14+
low, high = 1, max(quantities)
15+
answer = high
16+
17+
while low <= high:
18+
mid = (low + high) // 2
19+
if canDistribute(mid):
20+
answer = mid
21+
high = mid - 1
22+
else:
23+
low = mid + 1
24+
25+
return answer
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
# Minimized Maximum Distribution - Step-by-Step Explanation
2+
3+
This README provides a step-by-step breakdown of the solution for the problem of minimizing the maximum number of products any store can receive when distributing products among multiple stores. The solution is implemented in multiple languages: C++, Java, JavaScript, Python, and Go.
4+
5+
The solution uses **binary search** on the potential maximum number of products per store and checks the feasibility of each mid-point using a helper function.
6+
7+
## Problem Summary
8+
9+
- **Goal**: Distribute products among `n` stores to minimize the maximum number of products any store receives.
10+
- **Approach**: Use binary search to find the smallest feasible "maximum products per store".
11+
12+
---
13+
14+
## Approach Overview
15+
16+
The following steps outline the common approach used in all languages:
17+
18+
1. **Initialize Binary Search Range**:
19+
- Set `low` to 1 (minimum possible max products per store).
20+
- Set `high` to the maximum value in the `quantities` array, representing the initial upper bound for products per store.
21+
22+
2. **Binary Search Loop**:
23+
- Calculate the midpoint `mid` between `low` and `high`.
24+
- Use a helper function to check if it’s feasible to distribute products such that no store has more than `mid` products.
25+
- For each product type, calculate the number of stores required to hold its quantity without exceeding `mid` per store.
26+
- If the distribution is feasible with `mid`, update `high` to `mid - 1` to try for a smaller maximum.
27+
- If not feasible, update `low` to `mid + 1` to allow more products per store.
28+
29+
3. **Return the Result**:
30+
- After the binary search concludes, `low` will hold the minimum possible maximum products per store for an optimal distribution.
31+
32+
---
33+
34+
## Language-Specific Explanation
35+
36+
---
37+
38+
### C++ Code
39+
40+
1. **Define Helper Function**:
41+
- Implement a helper function to check if it’s feasible to distribute products with `mid` as the maximum limit.
42+
- For each product type, compute the number of stores required if each store receives no more than `mid` products.
43+
44+
2. **Binary Search Setup**:
45+
- Set `low` to 1 and `high` to the maximum quantity in the array.
46+
- Initialize `answer` to store the optimal maximum products per store.
47+
48+
3. **Binary Search Execution**:
49+
- Calculate `mid`, the midpoint between `low` and `high`.
50+
- Use the helper function to check if distributing with `mid` is feasible.
51+
- If feasible, set `high = mid - 1` and update `answer`.
52+
- If not feasible, set `low = mid + 1`.
53+
54+
4. **Return the Result**:
55+
- Return `answer`, which now holds the minimized maximum products per store.
56+
57+
---
58+
59+
### Java Code
60+
61+
1. **Define Helper Method**:
62+
- Create a helper method that takes `quantities`, `maxProducts`, and `n` as inputs.
63+
- Loop over `quantities` and calculate the required number of stores for each product type under the `maxProducts` limit.
64+
65+
2. **Binary Search Initialization**:
66+
- Set `low` to 1 and `high` to the maximum value in `quantities`.
67+
- Initialize `answer` to store the smallest possible maximum products per store.
68+
69+
3. **Execute Binary Search**:
70+
- Find `mid` as the average of `low` and `high`.
71+
- Use the helper method to verify if distributing with `mid` is possible.
72+
- If feasible, set `high = mid - 1` and update `answer`.
73+
- If not feasible, set `low = mid + 1`.
74+
75+
4. **Return the Solution**:
76+
- Return `answer` after completing the search for the minimized maximum.
77+
78+
---
79+
80+
### JavaScript Code
81+
82+
1. **Define Helper Function**:
83+
- Write a helper function that calculates if `mid` products per store is feasible.
84+
- For each product in `quantities`, calculate the number of stores required to meet `mid` constraints.
85+
86+
2. **Initialize Binary Search Variables**:
87+
- Set `low` to 1 and `high` to the maximum element in `quantities`.
88+
- Initialize `answer` to store the minimal feasible "maximum products per store".
89+
90+
3. **Run Binary Search**:
91+
- Compute `mid` as the midpoint between `low` and `high`.
92+
- Check feasibility using the helper function.
93+
- If feasible, update `high` to `mid - 1` and set `answer` to `mid`.
94+
- If not, update `low` to `mid + 1`.
95+
96+
4. **Final Output**:
97+
- Return `answer`, which holds the minimized maximum value for product distribution.
98+
99+
---
100+
101+
### Python Code
102+
103+
1. **Define Feasibility Check Function**:
104+
- Implement a function to check if distributing products with a maximum of `mid` per store is possible.
105+
- Loop through `quantities`, calculating the required stores for each product type under the `mid` constraint.
106+
107+
2. **Binary Search Preparation**:
108+
- Set `low` to 1 and `high` to the maximum value in `quantities`.
109+
- Use `answer` to store the minimum possible maximum products per store.
110+
111+
3. **Binary Search Execution**:
112+
- Calculate `mid` as the midpoint between `low` and `high`.
113+
- If the helper function determines that `mid` is feasible, set `high = mid - 1` and update `answer`.
114+
- If not feasible, increase `low` to `mid + 1`.
115+
116+
4. **Return Result**:
117+
- Return `answer`, the minimized maximum products per store for an optimal distribution.
118+
119+
---
120+
121+
### Go Code
122+
123+
1. **Define Helper Function**:
124+
- Implement a helper function to check if distributing products with a maximum of `mid` per store is feasible.
125+
- For each quantity, calculate the required number of stores under the `mid` constraint.
126+
127+
2. **Binary Search Initialization**:
128+
- Set `low` to 1 and `high` to the maximum value in `quantities`.
129+
- Store the answer in a variable `answer` for the minimized maximum value.
130+
131+
3. **Binary Search Execution**:
132+
- Calculate `mid` as the average of `low` and `high`.
133+
- Check feasibility using the helper function.
134+
- If feasible, set `high = mid - 1` and update `answer`.
135+
- If not, set `low = mid + 1`.
136+
137+
4. **Return the Solution**:
138+
- After the search, return `answer`, which holds the minimized maximum number of products per store.
139+
140+
---
141+
142+
## Complexity Analysis
143+
144+
- **Time Complexity**: \(O(m \log(\text{max}(\text{quantities})))\)
145+
- The binary search range is bounded by the maximum value in `quantities`, leading to \(O(\log(\text{max}(\text{quantities})))\) iterations.
146+
- Each iteration involves a feasibility check that takes \(O(m)\) time, where \(m\) is the number of product types.
147+
148+
- **Space Complexity**: \(O(1)\), since we use a constant amount of extra space.
149+
150+
---
151+
152+
Each implementation follows the same logic with slight variations in syntax and function handling. This method ensures that the maximum number of products per store is minimized for optimal distribution.

0 commit comments

Comments
 (0)