Skip to content

Commit d43e44a

Browse files
committed
19-06-2024
1 parent e7b8a4e commit d43e44a

File tree

3 files changed

+204
-36
lines changed

3 files changed

+204
-36
lines changed

Daily Solution Leetcode/19-16-2024.md

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
### Editorial for the Problem: [Minimum Number of Days to Make m Bouquets](https://leetcode.com/problems/minimum-number-of-days-to-make-m-bouquets/)
2+
3+
## **Date**: June 19, 2024
4+
5+
**Difficulty**: ![Medium](https://img.shields.io/badge/Medium-yellow)
6+
**Related Topics**: ![Array](https://img.shields.io/badge/Array-blue) ![Binary Search](https://img.shields.io/badge/Binary%20Search-blue)
7+
8+
<p>
9+
<a href="https://leetcode.com/problems/minimum-number-of-days-to-make-m-bouquets/">
10+
<img src="https://img.shields.io/badge/Link%20To%20The%20Question-blue" alt="Link To The Question">
11+
</a>
12+
</p>
13+
14+
## Editorial
15+
16+
The problem is to determine the minimum number of days required to make \( m \) bouquets from an array of days, where each bouquet consists of \( k \) consecutive flowers. If it's not possible to make \( m \) bouquets, return -1.
17+
18+
### Approach
19+
20+
This problem can be efficiently solved using a binary search on the number of days. The idea is to find the minimum day such that we can make \( m \) bouquets using the given flowers blooming days.
21+
22+
#### Key Steps:
23+
24+
1. **Binary Search Initialization**:
25+
- Set `low` to the minimum day in the array.
26+
- Set `high` to the maximum day in the array.
27+
28+
2. **Binary Search Loop**:
29+
- Calculate `mid` as the average of `low` and `high`.
30+
- Use a helper function `sets` to count the number of bouquets that can be made if the flowers bloom by day `mid`.
31+
- If the number of bouquets is greater than or equal to \( m \), update the result and adjust the search to find a potentially smaller day by setting `high` to `mid - 1`.
32+
- If the number of bouquets is less than \( m \), adjust the search to find a larger day by setting `low` to `mid + 1`.
33+
34+
3. **Helper Function `sets`**:
35+
- This function counts the number of bouquets that can be made by day `mid`.
36+
- It iterates through the array and counts the number of consecutive flowers that bloom by `mid`.
37+
- When a non-blooming flower is encountered, it calculates the number of bouquets from the consecutive blooming flowers and resets the counter.
38+
39+
### Code
40+
41+
```cpp
42+
class Solution {
43+
int sets(vector<int> &day, int k, int mid) {
44+
int curr = 0, n = day.size(), cnt = 0;
45+
for (int i = 0; i < n; i++) {
46+
if (day[i] <= mid) curr++;
47+
else {cnt += curr / k; curr = 0;}
48+
}
49+
cnt += curr / k;
50+
return cnt;
51+
}
52+
53+
public:
54+
int minDays(vector<int>& day, int m, int k) {
55+
int n = day.size();
56+
if (n < 1LL * m * k) return -1;
57+
58+
int low = *min_element(day.begin(), day.end());
59+
int high = *max_element(day.begin(), day.end());
60+
int res = INT_MAX;
61+
62+
while (low <= high) {
63+
int mid = (low + high) / 2;
64+
int curr = sets(day, k, mid);
65+
66+
if (curr >= m) {
67+
res = min(res, mid);
68+
high = mid - 1;
69+
} else {
70+
low = mid + 1;
71+
}
72+
}
73+
return res;
74+
}
75+
};
76+
```
77+
### Explanation of Code
78+
79+
**Helper Function `sets`**:
80+
81+
- It initializes `curr` to count consecutive blooming flowers and `cnt` to count bouquets.
82+
- Iterates through the `day` array and increments `curr` if the flower blooms by `mid`.
83+
- If a flower does not bloom by `mid`, it calculates the number of bouquets from `curr` and resets `curr`.
84+
85+
**Main Function `minDays`**:
86+
87+
- Checks if the total number of flowers is less than ( m times k ). If true, returns -1.
88+
- Initializes `low` to the smallest day and `high` to the largest day.
89+
- Uses binary search to find the minimum day where \( m \) bouquets can be made.
90+
- Updates the result and adjusts the search range based on the number of bouquets that can be made by the current `mid` day.
91+
92+
**Efficiency**:
93+
94+
- The binary search approach ensures a time complexity of O(N log (Max-Min)), making it efficient for large inputs.
95+
96+
### Like and Upvote
97+
98+
If you found this solution helpful, please consider liking 👍 and upvoting ⬆️. Your support helps in continuing to provide high-quality solutions.
99+
100+
<p align="center">
101+
<img src="https://preview.redd.it/petition-to-change-the-upvote-and-downvote-button-to-like-v0-jbrdq402054c1.jpg?width=640&crop=smart&auto=webp&s=8225d21c98a245f44fd6c1f74a4c6c67f0061f25" width="280">
102+
</p>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
class Solution {
2+
3+
int sets(vector<int> &day , int k , int mid)
4+
{
5+
int curr = 0, n = day.size(), j = 0, cnt = 0;
6+
7+
for(int i = 0 ; i < n ; i++)
8+
{
9+
if(day[i] <= mid) curr++;
10+
11+
else {cnt += curr/k; curr = 0;}
12+
}
13+
14+
cnt += curr/k;
15+
return cnt;
16+
}
17+
18+
public:
19+
int minDays(vector<int>& day, int m, int k) {
20+
21+
int n = day.size();
22+
23+
if(n < 1LL*m*k) return -1;
24+
25+
int res = INT_MAX;
26+
int low = *min_element(day.begin() , day.end());
27+
int high = *max_element(day.begin() , day.end());
28+
29+
while(low <= high)
30+
{
31+
int mid = (low + high)/2;
32+
33+
int curr = sets(day,k,mid);
34+
35+
if(curr >= m) {res = min(res, mid); high = mid - 1; }
36+
else low = mid + 1;
37+
}
38+
39+
return res;
40+
}
41+
};

readme.md

Lines changed: 61 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,99 @@
1-
### Editorial for the Problem: [Most Profit Assigning Work](https://leetcode.com/problems/most-profit-assigning-work/)
1+
### Editorial for the Problem: [Minimum Number of Days to Make m Bouquets](https://leetcode.com/problems/minimum-number-of-days-to-make-m-bouquets/)
22

3-
## **Date**: June 18, 2024
3+
## **Date**: June 19, 2024
44

55
**Difficulty**: ![Medium](https://img.shields.io/badge/Medium-yellow)
6-
**Related Topics**: ![Array](https://img.shields.io/badge/Array-blue) ![Binary Search](https://img.shields.io/badge/Binary%20Search-blue) ![Sorting](https://img.shields.io/badge/Sorting-blue)
6+
**Related Topics**: ![Array](https://img.shields.io/badge/Array-blue) ![Binary Search](https://img.shields.io/badge/Binary%20Search-blue)
77

88
<p>
9-
<a href="https://leetcode.com/problems/most-profit-assigning-work/">
9+
<a href="https://leetcode.com/problems/minimum-number-of-days-to-make-m-bouquets/">
1010
<img src="https://img.shields.io/badge/Link%20To%20The%20Question-blue" alt="Link To The Question">
1111
</a>
1212
</p>
1313

1414
## Editorial
1515

16-
Given the difficulty of tasks and the corresponding profits, along with the abilities of workers, the task is to assign workers to tasks in a way that maximizes the total profit.
16+
The problem is to determine the minimum number of days required to make \( m \) bouquets from an array of days, where each bouquet consists of \( k \) consecutive flowers. If it's not possible to make \( m \) bouquets, return -1.
1717

1818
### Approach
1919

20-
We can solve this problem using a brute-force approach by iterating through each worker and finding the most profitable task they can perform.
20+
This problem can be efficiently solved using a binary search on the number of days. The idea is to find the minimum day such that we can make \( m \) bouquets using the given flowers blooming days.
2121

2222
#### Key Steps:
23-
1. **Initialization**: Calculate the number of workers (`n`) and the number of tasks (`m`).
24-
2. **Iterate Through Workers**: For each worker, check each task they can perform based on their ability.
25-
3. **Update Profit**: Keep track of the maximum profit a worker can achieve and add it to the total profit.
23+
24+
1. **Binary Search Initialization**:
25+
- Set `low` to the minimum day in the array.
26+
- Set `high` to the maximum day in the array.
27+
28+
2. **Binary Search Loop**:
29+
- Calculate `mid` as the average of `low` and `high`.
30+
- Use a helper function `sets` to count the number of bouquets that can be made if the flowers bloom by day `mid`.
31+
- If the number of bouquets is greater than or equal to \( m \), update the result and adjust the search to find a potentially smaller day by setting `high` to `mid - 1`.
32+
- If the number of bouquets is less than \( m \), adjust the search to find a larger day by setting `low` to `mid + 1`.
33+
34+
3. **Helper Function `sets`**:
35+
- This function counts the number of bouquets that can be made by day `mid`.
36+
- It iterates through the array and counts the number of consecutive flowers that bloom by `mid`.
37+
- When a non-blooming flower is encountered, it calculates the number of bouquets from the consecutive blooming flowers and resets the counter.
2638

2739
### Code
2840

2941
```cpp
3042
class Solution {
31-
public:
32-
int maxProfitAssignment(vector<int>& diff, vector<int>& profit, vector<int>& worker) {
33-
int n = worker.size();
34-
int m = profit.size();
35-
int ans = 0;
43+
int sets(vector<int> &day, int k, int mid) {
44+
int curr = 0, n = day.size(), cnt = 0;
3645
for (int i = 0; i < n; i++) {
37-
int curr = 0;
38-
for (int j = 0; j < m; j++) {
39-
if (worker[i] >= diff[j]) curr = max(curr, profit[j]);
46+
if (day[i] <= mid) curr++;
47+
else {cnt += curr / k; curr = 0;}
48+
}
49+
cnt += curr / k;
50+
return cnt;
51+
}
52+
53+
public:
54+
int minDays(vector<int>& day, int m, int k) {
55+
int n = day.size();
56+
if (n < 1LL * m * k) return -1;
57+
58+
int low = *min_element(day.begin(), day.end());
59+
int high = *max_element(day.begin(), day.end());
60+
int res = INT_MAX;
61+
62+
while (low <= high) {
63+
int mid = (low + high) / 2;
64+
int curr = sets(day, k, mid);
65+
66+
if (curr >= m) {
67+
res = min(res, mid);
68+
high = mid - 1;
69+
} else {
70+
low = mid + 1;
4071
}
41-
ans += curr;
4272
}
43-
return ans;
73+
return res;
4474
}
4575
};
4676
```
47-
4877
### Explanation of Code
4978

50-
#### Initialization:
51-
52-
- Calculate the number of workers `n` and the number of tasks `m`.
53-
- Initialize `ans` to store the total profit.
54-
55-
#### Iteration:
79+
**Helper Function `sets`**:
5680

57-
1. Loop through each worker.
58-
- For each worker, initialize `curr` to 0.
59-
- Loop through each task to check if the worker can perform the task (i.e., their ability is greater than or equal to the difficulty of the task).
60-
- If the worker can perform the task, update `curr` to be the maximum of `curr` and the profit of the task.
61-
- Add the maximum profit the worker can achieve (`curr`) to the total profit (`ans`).
81+
- It initializes `curr` to count consecutive blooming flowers and `cnt` to count bouquets.
82+
- Iterates through the `day` array and increments `curr` if the flower blooms by `mid`.
83+
- If a flower does not bloom by `mid`, it calculates the number of bouquets from `curr` and resets `curr`.
6284

63-
#### Return Value:
85+
**Main Function `minDays`**:
6486

65-
- After processing all workers, return the total profit (`ans`).
87+
- Checks if the total number of flowers is less than ( m times k ). If true, returns -1.
88+
- Initializes `low` to the smallest day and `high` to the largest day.
89+
- Uses binary search to find the minimum day where \( m \) bouquets can be made.
90+
- Updates the result and adjusts the search range based on the number of bouquets that can be made by the current `mid` day.
6691

67-
#### Efficiency:
92+
**Efficiency**:
6893

69-
- This approach has a time complexity of \( O(n \times m) \), which might not be optimal for large inputs but provides a clear and straightforward solution to the problem.
94+
- The binary search approach ensures a time complexity of O(N log (Max-Min)), making it efficient for large inputs.
7095

71-
## Like and Upvote
96+
### Like and Upvote
7297

7398
If you found this solution helpful, please consider liking 👍 and upvoting ⬆️. Your support helps in continuing to provide high-quality solutions.
7499

0 commit comments

Comments
 (0)