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 { | |
| unordered_map<int,int> mymap; | |
| bool isValid(vector<int>& nums,int r){ | |
| for(auto [ele,freq]: mymap){ | |
| // Case 1: ele + nums[r] = some existing element | |
| if(mymap.count(nums[r] + ele)){ | |
| 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 { | |
| using ll = long long; | |
| public: | |
| long long maxValue(vector<int>& nums) { | |
| int n=nums.size(); | |
| if(n==1) return nums[0]; | |
| if(n==2) return max(nums[0]-nums[1],nums[1]-nums[0]); | |
| ll total = 0; | |
| 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 { | |
| using ll = long long; | |
| using pii = pair<int,int>; | |
| public: | |
| long long shadowPairs(vector<int>& nums) { | |
| int n = nums.size(); | |
| stack<pii> st; | |
| int total = 0; | |
| ll count = 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 { | |
| public: | |
| int countGroups(vector<int>& position, vector<int>& speed, int distance) { | |
| int n = position.size(); | |
| int groups = 1; //atleast 1 group will be made | |
| int group_speed = speed[n-1]; | |
| for(int i=n-2;i>=0;--i){ | |
| if(position[i+1]-position[i] <= distance){ | |
| continue; |
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 { | |
| void primeFactorize(unordered_map<int,vector<int>>& prime_factors,int num){ | |
| int val = num; | |
| vector<int> fact; | |
| for(int i=2;i*i<=num;++i){ | |
| if(num%i==0) | |
| fact.push_back(i); | |
| while(num%i==0) | |
| num/=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 { | |
| int minCount(vector<int>& nums,int idx,int sum,vector<vector<int>>& min_ops){ | |
| if(sum==0) | |
| return 0; | |
| if(idx>=nums.size() or sum<0) | |
| return INT_MAX; | |
| if(min_ops[idx][sum]!=-1) | |
| return min_ops[idx][sum]; | |
| // Don't Include curr element |
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 maximumGap(string skill, string station) { | |
| int n = skill.size(); | |
| //Step-1: Find the earliest valid occurence of skills | |
| vector<int> earliest(n); | |
| int pos = 0; | |
| for(int i=0;i<n;++i){ | |
| while(skill[i]!=station[pos]) |
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; | |
| int MOD = 1e9+7; | |
| vector<int> fact; | |
| vector<int> inv_fact; | |
| void calculateFactorial(int& n){ | |
| fact[0] = 1; | |
| for(int i=1;i<=n;++i) | |
| fact[i] = (1ll * i * fact[i-1]) % MOD; |
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 count(string str,char value){ | |
| int count = 0; | |
| for(char c: str) | |
| if(c==value) | |
| count++; | |
| return count; | |
| } | |
| public: |
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<string> createGrid(int m, int n, int k) { | |
| vector<string> grid; | |
| //Step-1: Fill grid with obstacles except 1st row and last col. This is for 1st path | |
| // On a 1*N or M*1 grid, only 1 path is possible | |
| for(int i=0;i<m;++i){ | |
| if(i==0) | |
| grid.push_back(string(n,'.'));//all free cells | |
| else |
NewerOlder