Skip to content

Commit

Permalink
Oct 2024 hacktoberfest (#100)
Browse files Browse the repository at this point in the history
* add changes

* add changes
  • Loading branch information
afrozchakure authored Oct 27, 2024
1 parent 8284765 commit 0d25188
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 0 deletions.
68 changes: 68 additions & 0 deletions LeetCode/Algorithms/Hard/MeetingRoomsIII.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
bool compare(vector<int> &a, vector<int> &b) {
return a[0] < b[0];
}
class Solution {
public:
int mostBooked(int n, vector<vector<int>>& meetings) {
sort(meetings.begin(), meetings.end(), compare);

// engaged meetings - {current_meeting_time, room}
priority_queue<pair<long long, int>, vector<pair<long long, int>>, greater<pair<long long, int>>> engaged;

// unused - {meetng_room}
priority_queue<int, vector<int>, greater<int>> unused;

unordered_map<int, int> freq;

for(int i=0; i<n; i++) {
unused.push(i);
}

for(auto &x: meetings) {
int start = x[0];
int end = x[1];

// remove the meetings that have already ended from engaged heap and add them in unused
while(engaged.size() > 0 && engaged.top().first <= start) {
int room = engaged.top().second;

engaged.pop();
unused.push(room);
}

// unused
if(unused.size() > 0) {
int room = unused.top();
unused.pop();

freq[room]++;
engaged.push({end, room});
}
// engaged
else {
auto temp = engaged.top();
engaged.pop();
long long newEnd = temp.first;
int room = temp.second;
freq[room]++;
newEnd += end - start;

engaged.push({newEnd, room});
}
}

int maxi = 0;
for(int i=1; i<n; i++) {
if(freq[i] > freq[maxi]) {
maxi = i;
}
}
return maxi;

}
};

// Time Complexity - MlogM + MlogN, where M is the number of meetings
// N is the number of rooms
// Space Complexity - O(n), since we using priority-queue

39 changes: 39 additions & 0 deletions LeetCode/Algorithms/Hard/MinimumFallingPathSumII.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
class Solution {
public:
int dp[205][205];
int minFallingPathSum(vector<vector<int>>& grid) {
memset(dp, -1, sizeof(dp));
int n = grid.size();
int ans = INT_MAX;
for(int i=0; i<n; i++) {
ans = min(ans, ok(grid, 0, i));
}

return ans;
}

int ok(vector<vector<int>> &grid, int row, int column) {
int n = grid.size();
if(row == grid.size() - 1) {
return grid[row][column];
}

if(row == grid.size()) {
return 0;
}

if(dp[row][column] != -1) {
return dp[row][column];
}

int ans = INT_MAX;
for(int i=0; i<n; i++) {
if(i != column) {
ans = min(ans, ok(grid, row + 1, i));
}
}

dp[row][column] = ans + grid[row][column];
return dp[row][column];
}
};
23 changes: 23 additions & 0 deletions LeetCode/Algorithms/Hard/MinimumNumberOfKConsecutiveBitFlips.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class Solution {
public:
int minKBitFlips(vector<int>& nums, int k) {
// 1. Start window at 0, has to be size k
// 2. flip, some might turn into 0's
// 3. start window there

deque<int> q;
int result = 0;

for(int i=0; i<nums.size(); i++) {
if((nums[i] + q.size()) % 2 == 0) {
if(i + k > nums.size()) {
return -1;
}
result += 1;
q.push_back(i);
}
}

return result;
}
};

0 comments on commit 0d25188

Please sign in to comment.