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 |
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 | |
| void getPrimes(vector<int>& nums,set<int>& primes){ | |
| for(int ele: nums){ | |
| for(int i=2;i*i<=ele;++i){ | |
| if(ele%i==0){ | |
| primes.insert(i); | |
| while(ele%i==0) | |
| ele/=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
| // 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; | |
| } |
NewerOlder