diff --git a/Leetcode/Elimination-game.cpp b/Leetcode/Elimination-game.cpp new file mode 100644 index 0000000..d763576 --- /dev/null +++ b/Leetcode/Elimination-game.cpp @@ -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 */ + } +}; diff --git a/Leetcode/Leetcode Problem 1465.cpp b/Leetcode/Leetcode Problem 1465.cpp new file mode 100644 index 0000000..1fa7a51 --- /dev/null +++ b/Leetcode/Leetcode Problem 1465.cpp @@ -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& horizontalCuts, vector& 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; + } +};