Skip to content

Commit

Permalink
Merge branch '7oSkaaa:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
AhmedGamal2212 committed Jul 2, 2023
2 parents a1ff272 + b9e0a06 commit b40e26b
Show file tree
Hide file tree
Showing 3 changed files with 231 additions and 0 deletions.
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;
}
};
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;
}
};
146 changes: 146 additions & 0 deletions 07- July/README.md
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>
&nbsp;
<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>
&nbsp;
<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;
}
};
```

0 comments on commit b40e26b

Please sign in to comment.