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; | |
| } |
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] |
NewerOlder