This file contains hidden or 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
| class Solution { | |
| public: | |
| int numberOfPairs(vector<vector<int>>& points) { | |
| int n = points.size(); | |
| sort(points.begin(),points.end(),[](const vector<int>& p1,const vector<int>& p2){ | |
| return p1[0]==p2[0] ? p1[1]>p2[1] : p1[0]<p2[0]; | |
| }); | |
| int count = 0; | |
| for(int A=0;A<n-1;++A){ |
This file contains hidden or 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
| class Solution { | |
| #define ll long long | |
| public: | |
| long long flowerGame(int n, int m) { | |
| ll count = 0; | |
| //Case-1: Odd in N and Even in M | |
| int odds_in_n = (n+1)/2; | |
| int even_in_m = m/2; | |
| count += (1LL * odds_in_n * even_in_m); |
This file contains hidden or 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
| // C++: O(M^2 + N^2 + MN) * (log M + log N) Solution | |
| class Solution { | |
| vector<vector<int>> prefix_sum; | |
| void computePrefixSum(vector<vector<int>>& grid){ | |
| for(int i=0;i<grid.size();++i){ | |
| for(int j=0;j<grid[0].size();++j){ | |
| prefix_sum[i+1][j+1] = grid[i][j] + prefix_sum[i+1][j] + prefix_sum[i][j+1] - prefix_sum[i][j]; | |
| } | |
| } |
This file contains hidden or 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
| class Solution { | |
| public: | |
| int minimumArea(vector<vector<int>>& grid) { | |
| int m = grid.size(); | |
| int n = grid[0].size(); | |
| int low_x = INT_MAX, high_x = -1; | |
| int low_y = INT_MAX, high_y = -1; | |
| for(int i=0;i<m;++i){ | |
| for(int j=0;j<n;++j){ |
This file contains hidden or 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
| class Solution { | |
| public: | |
| int numSubmat(vector<vector<int>>& mat) { | |
| int m = mat.size(); | |
| int n = mat[0].size(); | |
| int count = 0, curr_count; | |
| vector<int> histogram(n); | |
| for(int i=0;i<m;++i){ | |
| // Update the current row to histogram |
This file contains hidden or 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
| class Solution { | |
| public: | |
| int sortPermutation(vector<int>& nums) { | |
| int n = nums.size(); | |
| int max_k = pow(2,ceil(log2(n)))-1; | |
| int k = max_k; | |
| // Iterate and find max k value | |
| for(int i=0;i<n;++i){ | |
| // If current element is not at the correct position |
This file contains hidden or 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
| //C++: Tabulation 1D | |
| class Solution { | |
| #define MOD 1000000007 | |
| int binaryExponentiation(int a,int b){ | |
| int res = 1; | |
| while(b){ | |
| if(b&1) | |
| res *= a; | |
| a *= a; |
This file contains hidden or 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
| class Solution { | |
| #define MOD 1000000007 | |
| vector<int> powers; | |
| int mem[300+2][300+2]; | |
| int binaryExponentiation(int a,int b){ | |
| int res = 1; | |
| while(b){ | |
| if(b&1) | |
| res *= a; |
This file contains hidden or 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
| class Solution { | |
| using ll = long long; | |
| static constexpr int INF = std::numeric_limits<int>::max(); | |
| // DFS + Memoization | |
| int dfs(const vector<vector<int>>& fruits, | |
| int row, int col, int moves, | |
| vector<vector<int>>& mem, | |
| const vector<pair<int,int>>& dirs) | |
| { |
This file contains hidden or 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
| class Solution { | |
| vector<int> segTree; | |
| void buildSegTreeRMQ(vector<int>& baskets,int low,int high,int st_idx){ | |
| if(low==high){ | |
| segTree[st_idx] = baskets[low]; | |
| return; | |
| } | |
| int mid = low + (high-low)/2; |