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: TC: O(N), SC: O(N) | |
| class Solution { | |
| #define ll long long | |
| ll transform(const int& ele,const int& k,const bool is_positive){ | |
| if(is_positive) | |
| return 1LL*ele*k; | |
| return ele>=0? floor(ele/k) : ceil(ele/k); | |
| } |
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 Node { | |
| int u; | |
| int count; | |
| int wt; | |
| // Min-heap comparison based on weight | |
| bool operator>(const Node& other) const { | |
| return wt > other.wt; | |
| } |
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 maximumSum(vector<int>& nums, int m, int l, int r) { | |
| int n = nums.size(); | |
| //Step-1: Build PrefixSum array | |
| vector<ll> psum(n+1); | |
| for(int i=1;i<=n;++i) | |
| psum[i] = psum[i-1] + nums[i-1]; |
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; |
NewerOlder