Skip to content

Solution to problem 390 of leetcode #464

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions Leetcode/Elimination-game.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
Time Complexity : O(log N)
Space Complexity : O(log N)
*/

//Author : Deepak Mehrotra (StealthX2k20)

class Solution {
public:

/*
if direction is 0 that means we are going from left to right
otherwise we are going from right to left
*/

int find_ans(int first, int last, int direction, int diff)
{
if(first == last) return first; // we have a single element remaining

if(first + diff == last) // we have 2 elements remaining
{
return (direction == 0 ? last : first); // return the element which will not get removed
}

int len = 1 + ((last - first) / diff); // find length of remaining segment

if(len & 1) // check if len is odd
{
return find_ans(first + diff, last - diff, direction ^ 1, diff * 2);
}

else // len is even
{
if(!direction) // if direction is 0 then we are going from left to right -> -> ->
{
return find_ans(first + diff, last, direction ^ 1, diff * 2);
}

else // if direction is 1 then we are going from right to left <- <- <-
{
return find_ans(first, last - diff, direction ^ 1, diff * 2);
}
}
}

int lastRemaining(int n)
{
return find_ans(1, n, 0, 1); /* initially we have segment from 1, 2, 3 .. n and we have to go from left to right and our initial difference is 1 */
}
};
35 changes: 35 additions & 0 deletions Leetcode/Leetcode Problem 1465.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//Problem link : https://leetcode.com/problems/maximum-area-of-a-piece-of-cake-after-horizontal-and-vertical-cuts/

class Solution {
public:
int maxArea(int h, int w, vector<int>& horizontalCuts, vector<int>& verticalCuts) {
sort(horizontalCuts.begin(), horizontalCuts.end());
sort(verticalCuts.begin(), verticalCuts.end());

int mxh = -1, mxv = -1;
int prevh = 0, prevv = 0;
for(int i = 0; i < horizontalCuts.size(); i++)
{
mxh = max(mxh, horizontalCuts[i] - prevh);
prevh = horizontalCuts[i];
}

mxh = max(mxh, h - prevh);

for(int i = 0; i < verticalCuts.size(); i++)
{
mxv = max(mxv, verticalCuts[i] - prevv);
prevv = verticalCuts[i];
}

mxv = max(mxv, w - prevv);

// mxv++, mxh++;

long long res, mod = 1000000007;

res = (((mxv * 1ll) % mod) * ((mxh * 1ll) % mod)) % mod;

return res;
}
};