Skip to content

Instantly share code, notes, and snippets.

View bhaveshmunot1's full-sized avatar

Bhavesh Munot bhaveshmunot1

View GitHub Profile
@bhaveshmunot1
bhaveshmunot1 / Leetcode_1465.cpp
Created June 24, 2020 03:38
Leetcode #1465: Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts (https://www.InterviewRecipes.com/leetcode-1465)
class Solution {
const int mod = (1e9)+7; // As the question expects.
int getMax(int length, vector<int> &cuts) {
sort(cuts.begin(), cuts.end()); // Sort so that we will easily get the thickness
// of each piece after cutting it.
int n = cuts.size();
int maxCut = max(length-cuts[n-1], // Thickness of the last piece.
cuts[0]); // Thickness of the first piece.
for (int i=1; i<n; i++) { // For each cut -
@bhaveshmunot1
bhaveshmunot1 / Leetcode_1419.cpp
Last active June 23, 2020 07:36
Leetcode #1419: Minimum Number of Frogs Croaking (https://www.InterviewRecipes.com/leetcode-1419)
class Solution {
unordered_map<char, int> frequency; // Stores how many times each sound has occured.
// Sounds are c, r, o, a, k.
/*
* At any time, for a sequence to be valid, number of 'c' must not be less than 'r'.
* Similarly, #'r' shouldn't less than #'o', and so on.
*/
bool isStateValid() {
return (frequency['c'] >= frequency['r']) &&
(frequency['r'] >= frequency['o']) &&
@bhaveshmunot1
bhaveshmunot1 / Leetcode_1423.cpp
Created June 21, 2020 20:45
Leetcode #1423: Maximum Points You Can Obtain from Cards (https://interviewrecipes.com/leetcode-1423)
class Solution {
public:
int maxScore(vector<int>& cardPoints, int k) {
int n = cardPoints.size();
vector<int> cumulativeSumFront(n+1, 0); // For easier indexing, add an extra element.
vector<int> cumulativeSumBack(n+1, 0); // For easier indexing, add an extra element.
for (int i=0; i<n; i++) {
cumulativeSumFront[i+1] = cumulativeSumFront[i] + cardPoints[i];
}
@bhaveshmunot1
bhaveshmunot1 / Leetcode_1443.cpp
Last active June 9, 2020 01:53
Leetcode #1443: Minimum Time to Collect All Apples in a Tree (https://interviewrecipes.com/leetcode-1443)
class Solution {
unordered_map<int, vector<int>> tree;
unordered_map<int, bool> visited;
vector<bool> hasApple;
/** Convert edge list into adjacency list */
void createTreeStructure(vector<vector<int>>& edges) {
// Adjacency list representation.
for (auto e: edges) {
tree[e[0]].push_back(e[1]); // a --> b
@bhaveshmunot1
bhaveshmunot1 / Leetcode_1472.cpp
Created June 7, 2020 21:28
Leetcode #1472: Design Browser History
class BrowserHistory {
stack<string> history;
stack<string> future;
string currentWebsite;
void clearForwardStack() {
future = stack<string>();
}
void goBackOneStep() {
@bhaveshmunot1
bhaveshmunot1 / Leetcode_0406.cpp
Created June 7, 2020 00:56
Leetcode #406: Queue Reconstruction by Height
bool comparator(const vector<int> &x, const vector<int> &y) {
return (x[0] > y[0]) || ((x[0] == y[0]) && (x[1] < y[1]));
}
class Solution {
public:
vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {
vector<vector<int>> answer;
sort(people.begin(), people.end(), comparator);
for (auto &x:people) {
@bhaveshmunot1
bhaveshmunot1 / Leetcode_1424.cpp
Created June 6, 2020 23:32
Leetcode #1424: Diagonal Traverse II
vector<int> findDiagonalOrder(vector<vector<int>>& nums) {
vector<int> answer;
unordered_map<int, vector<int>> m;
int maxKey = 0; // maximum key inserted into the map i.e. max value of i+j indices.
for (int i=0; i<nums.size(); i++) {
for (int j=0; j<nums[i].size(); j++) {
m[i+j].push_back(nums[i][j]); // insert nums[i][j] in bucket (i+j).
maxKey = max(maxKey, i+j); //
}
int start = 0; // 1
int end = 0; // 1
while (end <= n-1) { // n times
if ( ... ) { // 1
while (start <= end && nums[start] < nums[end]) { // ??
...
start++; // 1
}
}
end++; // 1
@bhaveshmunot1
bhaveshmunot1 / File1.cpp
Created June 4, 2020 19:43
Test 2 Files
int main() {
return 0;
}
@bhaveshmunot1
bhaveshmunot1 / RecursiveFactorialAnalysis.cpp
Created June 4, 2020 10:04
Find Time Complexity of Recursive Implementation of N!.
int factorial(int n) { // T(n)
if (n <= 1) { // 1
return 1; // 1
}
int temp = factorial(n-1); // T(n-1)
return n*temp; // 1
}