-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
3 changed files
with
130 additions
and
0 deletions.
There are no files selected for viewing
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,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 | ||
|
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,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
23
LeetCode/Algorithms/Hard/MinimumNumberOfKConsecutiveBitFlips.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,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; | ||
} | ||
}; |