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: | |
| int maximumSaleItems(vector<vector<int>>& items, int budget) { | |
| int n=items.size(); | |
| //Step-1: Find cheapest and max_factor O(N) | |
| int cheapest = INT_MAX; | |
| int max_factor = INT_MIN; | |
| for(int i=0;i<n;++i){ |
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 { | |
| bool checkIfIncreasing(vector<int>& nums,int& min_idx,int& n){ | |
| int i; | |
| for(i=min_idx;i<min_idx+n;++i){ | |
| if(nums[(i+1)%n]-1 != nums[i%n]) | |
| break; | |
| } | |
| if((i+1)%n == min_idx) | |
| return true; | |
| return false; |
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 countLocalMaximums(vector<vector<int>>& matrix) { | |
| int n = matrix.size(), m = matrix[0].size(); | |
| static int psum[201][205][205]; | |
| memset(psum, 0, sizeof(psum)); | |
| /* | |
| Step-1: Find PrefixSum for count of values > matrix[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
| //Solution-1: Space optimized solution | |
| //TC: O(NlogN + M (logN)^2) | |
| //SC: O(N) | |
| class Solution { | |
| #define ll long long | |
| public: | |
| long long minArraySum(vector<int>& nums) { | |
| int n = nums.size(); | |
| //Step-1: Build ordered_map using the nums array | |
| map<ll,ll> freq; |
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: | |
| vector<int> minCost(vector<int>& nums, vector<vector<int>>& queries) { | |
| int n = nums.size(); | |
| // Step-1: Calculate Prefix_Sum | |
| vector<int> prefix_sum(n+1); | |
| prefix_sum[1] = 1; | |
| int curr_cost; | |
| for(int i=1;i<n-1;++i){ |
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 minOperations(vector<int>& nums) { | |
| ll sum = 0; | |
| for(int i=1;i<nums.size();++i){ | |
| if(nums[i]<nums[i-1]) | |
| sum += nums[i-1]-nums[i]; | |
| } | |
| return sum; |
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 pii pair<int,int> | |
| static constexpr int dir[5] = {-1,0,1,0,-1}; | |
| public: | |
| vector<vector<int>> colorGrid(int n, int m, vector<vector<int>>& sources) { | |
| sort(sources.begin(),sources.end(),[](const vector<int>& a,const vector<int>& b){ | |
| return a[2]>b[2]; | |
| }); | |
| vector<vector<int>> grid(n,vector<int>(m)); | |
| queue<pii> q; |
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 { | |
| int findLongest(string& s){ | |
| int n=s.size(); | |
| int ones=0,zeroes=0; | |
| for(int i=0;i<n;++i){ | |
| if(s[i]=='1') ones++; | |
| else zeroes++; | |
| } | |
| int longest = 0; | |
| int balance = 0; |
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 { | |
| struct DSU{ | |
| vector<int> parent; | |
| DSU(int n) : parent(n,-1) {} | |
| int Find(int v) { | |
| if(parent[v] == -1) | |
| return v; | |
| return parent[v] = Find(parent[v]); | |
| } | |
| void Union(int u,int v){ |
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 { | |
| int rowSum(vector<vector<int>>& grid,int& row,int& col){ | |
| int rowSum = grid[row][col] + grid[row][col+1] + grid[row][col+2]; | |
| if(rowSum != grid[row+1][col] + grid[row+1][col+1] + grid[row+1][col+2] or | |
| rowSum != grid[row+2][col] + grid[row+2][col+1] + grid[row+2][col+2]) | |
| return -1; | |
| return rowSum; | |
| } | |
| int colSum(vector<vector<int>>& grid,int& row,int& col){ | |
| int colSum = grid[row][col] + grid[row+1][col] + grid[row+2][col]; |
NewerOlder