forked from 7oSkaaa/LeetCode_DailyChallenge_2023
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch '7oSkaaa:main' into main
- Loading branch information
Showing
3 changed files
with
231 additions
and
0 deletions.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
07- July/01- Fair Distribution of Cookies/01- Fair Distribution of Cookies (Mina Magdy).cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Author: Mina Magdy | ||
|
||
class Solution { | ||
public: | ||
// Function to distribute cookies among k children | ||
int distributeCookies(vector<int>& 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<int, vector<int>, greater<int>> 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; | ||
} | ||
}; |
38 changes: 38 additions & 0 deletions
38
...e Transfer Requests/02- Maximum Number of Achievable Transfer Requests (Ahmed Hossam).cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// Author: Ahmed Hossam | ||
|
||
class Solution { | ||
public: | ||
int maximumRequests(int n, vector<vector<int>>& 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; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
# LeetCode Daily Challenge Problems for July | ||
|
||
<br><br> | ||
|
||
## Workflow Checking | ||
|
||
<div align="center"> | ||
<img src="https://github.com/7oSkaaa/LeetCode_DailyChallenge_2023/actions/workflows/Author_Line.yml/badge.svg" alt="Checkers" width="150"> | ||
<a href="https://github.com/7oSkaaa/LeetCode_DailyChallenge_2023/actions/workflows/Author_Line.yml" taget="_blank"/> | ||
</img> | ||
| ||
<img src="https://github.com/7oSkaaa/LeetCode_DailyChallenge_2023/actions/workflows/File_Names.yml/badge.svg" alt="Checkers" width="150"> | ||
<a href="https://github.com/7oSkaaa/LeetCode_DailyChallenge_2023/actions/workflows/File_Names.yml" taget="_blank"/> | ||
</img> | ||
| ||
<img src="https://github.com/7oSkaaa/LeetCode_DailyChallenge_2023/actions/workflows/Daily_Problem.yml/badge.svg" alt="Checkers" width="150"> | ||
<a href="https://github.com/7oSkaaa/LeetCode_DailyChallenge_2023/actions/workflows/Daily_Problem.yml" taget="_blank"/> | ||
</img> | ||
</div> | ||
|
||
<br><br> | ||
|
||
## 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)** | ||
|
||
<hr> | ||
<br><br> | ||
|
||
## 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<int>& 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<int, vector<int>, greater<int>> 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; | ||
} | ||
}; | ||
``` | ||
|
||
|
||
<hr> | ||
<br><br> | ||
|
||
## 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<vector<int>>& 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; | ||
} | ||
}; | ||
``` | ||