diff --git a/07- July/01- Fair Distribution of Cookies/01- Fair Distribution of Cookies (Mina Magdy).cpp b/07- July/01- Fair Distribution of Cookies/01- Fair Distribution of Cookies (Mina Magdy).cpp new file mode 100644 index 000000000..d0cbe3112 --- /dev/null +++ b/07- July/01- Fair Distribution of Cookies/01- Fair Distribution of Cookies (Mina Magdy).cpp @@ -0,0 +1,47 @@ +// Author: Mina Magdy + +class Solution { +public: + // Function to distribute cookies among k children + int distributeCookies(vector& cookies, int k) { + // Sort the cookies in ascending order + sort(cookies.begin(), cookies.end()); + // Initialize ans variable with a large value + int ans = 1e9; + + // Generate permutations of cookies + do { + // Create a min heap priority queue to store the sum of cookies for each child + priority_queue, greater> pq; + + // Initialize the priority queue with k zeros + for (int i = 0; i < k; i++) + pq.push(0); + + // Initialize the current answer variable to 0 + int cur_ans = 0; + + // Distribute cookies among children + for (int i = 0; i < cookies.size(); i++) { + // Calculate the new total for the child with the least sum of cookies + int new_tp = pq.top() + cookies[i]; + + // Update the current answer with the maximum sum encountered + cur_ans = max(cur_ans, new_tp); + + // Remove the child with the least sum of cookies from the priority queue + pq.pop(); + + // Add the child with the updated sum back to the priority queue + pq.push(new_tp); + } + + // Update the minimum answer encountered so far + ans = min(ans, cur_ans); + + } while (next_permutation(cookies.begin(), cookies.end())); + + // Return the minimum answer + return ans; + } +}; diff --git a/07- July/02- Maximum Number of Achievable Transfer Requests/02- Maximum Number of Achievable Transfer Requests (Ahmed Hossam).cpp b/07- July/02- Maximum Number of Achievable Transfer Requests/02- Maximum Number of Achievable Transfer Requests (Ahmed Hossam).cpp new file mode 100644 index 000000000..d16496bd4 --- /dev/null +++ b/07- July/02- Maximum Number of Achievable Transfer Requests/02- Maximum Number of Achievable Transfer Requests (Ahmed Hossam).cpp @@ -0,0 +1,38 @@ +// Author: Ahmed Hossam + +class Solution { +public: + int maximumRequests(int n, vector>& requests) { + // Initialize variables r and maxReq. + int r = requests.size(), maxReq = 0; + + // Iterate through all possible combinations of requests using a bitmask. + for(int mask = 0; mask < (1 << r); mask++){ + // Create a vector net to keep track of the degree of each node. + vector < int > net(n); + + // Iterate through each request in the requests vector. + for(int i = 0; i < r; i++){ + // Check if the i-th request is included in the bitmask. + if(mask & (1 << i)){ + // If the request is included, decrement the net of the source node + // and increment the net of the destination node. + net[requests[i][0]]--; + net[requests[i][1]]++; + } + } + + // Check if all nodes have a net of 0, indicating that requests can be made. + bool can_make_requests = true; + for(int i = 0; i < n; i++) + can_make_requests &= (net[i] == 0); + + // If all nodes have a net of 0, update maxReq with the maximum number of requests made. + if(can_make_requests) + maxReq = max(maxReq, __builtin_popcount(mask)); + } + + // Return the maximum number of requests that can be made. + return maxReq; + } +}; diff --git a/07- July/README.md b/07- July/README.md new file mode 100644 index 000000000..fec302522 --- /dev/null +++ b/07- July/README.md @@ -0,0 +1,146 @@ +# LeetCode Daily Challenge Problems for July + +

+ +## Workflow Checking + +
+Checkers + + +  +Checkers + + +  +Checkers + + +
+ +

+ +## Problems: +1. **[Fair Distribution of Cookies](#01--fair-distribution-of-cookies)** +1. **[Maximum Number of Achievable Transfer Requests](#02--maximum-number-of-achievable-transfer-requests)** + +
+

+ +## 01) [Fair Distribution of Cookies](https://leetcode.com/problems/fair-distribution-of-cookies/) + +### Difficulty + +![](https://img.shields.io/badge/Medium-orange?style=for-the-badge) + +### Related Topic + +`Array` `Dynamic Programming` `Backtracking` `Bit Manipulation` `Bitmask` + +### Code + + +```cpp +class Solution { +public: + // Function to distribute cookies among k children + int distributeCookies(vector& cookies, int k) { + // Sort the cookies in ascending order + sort(cookies.begin(), cookies.end()); + // Initialize ans variable with a large value + int ans = 1e9; + + // Generate permutations of cookies + do { + // Create a min heap priority queue to store the sum of cookies for each child + priority_queue, greater> pq; + + // Initialize the priority queue with k zeros + for (int i = 0; i < k; i++) + pq.push(0); + + // Initialize the current answer variable to 0 + int cur_ans = 0; + + // Distribute cookies among children + for (int i = 0; i < cookies.size(); i++) { + // Calculate the new total for the child with the least sum of cookies + int new_tp = pq.top() + cookies[i]; + + // Update the current answer with the maximum sum encountered + cur_ans = max(cur_ans, new_tp); + + // Remove the child with the least sum of cookies from the priority queue + pq.pop(); + + // Add the child with the updated sum back to the priority queue + pq.push(new_tp); + } + + // Update the minimum answer encountered so far + ans = min(ans, cur_ans); + + } while (next_permutation(cookies.begin(), cookies.end())); + + // Return the minimum answer + return ans; + } +}; +``` + + +
+

+ +## 02) [Maximum Number of Achievable Transfer Requests](https://leetcode.com/problems/maximum-number-of-achievable-transfer-requests/) + +### Difficulty + +![](https://img.shields.io/badge/Hard-red?style=for-the-badge) + +### Related Topic + +`Array` `Backtracking` `Bit Manipulation` `Enumeration` + +### Code + + +```cpp +class Solution { +public: + int maximumRequests(int n, vector>& requests) { + // Initialize variables r and maxReq. + int r = requests.size(), maxReq = 0; + + // Iterate through all possible combinations of requests using a bitmask. + for(int mask = 0; mask < (1 << r); mask++){ + // Create a vector net to keep track of the degree of each node. + vector < int > net(n); + + // Iterate through each request in the requests vector. + for(int i = 0; i < r; i++){ + // Check if the i-th request is included in the bitmask. + if(mask & (1 << i)){ + // If the request is included, decrement the net of the source node + // and increment the net of the destination node. + net[requests[i][0]]--; + net[requests[i][1]]++; + } + } + + // Check if all nodes have a net of 0, indicating that requests can be made. + bool can_make_requests = true; + for(int i = 0; i < n; i++) + can_make_requests &= (net[i] == 0); + + // If all nodes have a net of 0, update maxReq with the maximum number of requests made. + if(can_make_requests) + maxReq = max(maxReq, __builtin_popcount(mask)); + } + + // Return the maximum number of requests that can be made. + return maxReq; + } +}; +``` + \ No newline at end of file