|
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/) |
2 | 2 |
|
3 |
| -## **Date**: June 18, 2024 |
| 3 | +## **Date**: June 19, 2024 |
4 | 4 |
|
5 | 5 | **Difficulty**: 
|
6 |
| -**Related Topics**:    |
| 6 | +**Related Topics**:   |
7 | 7 |
|
8 | 8 | <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/"> |
10 | 10 | <img src="https://img.shields.io/badge/Link%20To%20The%20Question-blue" alt="Link To The Question">
|
11 | 11 | </a>
|
12 | 12 | </p>
|
13 | 13 |
|
14 | 14 | ## Editorial
|
15 | 15 |
|
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. |
17 | 17 |
|
18 | 18 | ### Approach
|
19 | 19 |
|
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. |
21 | 21 |
|
22 | 22 | #### 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. |
26 | 38 |
|
27 | 39 | ### Code
|
28 | 40 |
|
29 | 41 | ```cpp
|
30 | 42 | 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; |
36 | 45 | 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; |
40 | 71 | }
|
41 |
| - ans += curr; |
42 | 72 | }
|
43 |
| - return ans; |
| 73 | + return res; |
44 | 74 | }
|
45 | 75 | };
|
46 | 76 | ```
|
47 |
| -
|
48 | 77 | ### Explanation of Code
|
49 | 78 |
|
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`**: |
56 | 80 |
|
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`. |
62 | 84 |
|
63 |
| -#### Return Value: |
| 85 | +**Main Function `minDays`**: |
64 | 86 |
|
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. |
66 | 91 |
|
67 |
| -#### Efficiency: |
| 92 | +**Efficiency**: |
68 | 93 |
|
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. |
70 | 95 |
|
71 |
| -## Like and Upvote |
| 96 | +### Like and Upvote |
72 | 97 |
|
73 | 98 | If you found this solution helpful, please consider liking 👍 and upvoting ⬆️. Your support helps in continuing to provide high-quality solutions.
|
74 | 99 |
|
|
0 commit comments