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
| //C++: Tabulation 1D | |
| class Solution { | |
| #define MOD 1000000007 | |
| int binaryExponentiation(int a,int b){ | |
| int res = 1; | |
| while(b){ | |
| if(b&1) | |
| res *= a; | |
| a *= a; |
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 | |
| vector<int> powers; | |
| int mem[300+2][300+2]; | |
| int binaryExponentiation(int a,int b){ | |
| int res = 1; | |
| while(b){ | |
| if(b&1) | |
| res *= a; |
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; | |
| static constexpr int INF = std::numeric_limits<int>::max(); | |
| // DFS + Memoization | |
| int dfs(const vector<vector<int>>& fruits, | |
| int row, int col, int moves, | |
| vector<vector<int>>& mem, | |
| const vector<pair<int,int>>& dirs) | |
| { |
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 { | |
| vector<int> segTree; | |
| void buildSegTreeRMQ(vector<int>& baskets,int low,int high,int st_idx){ | |
| if(low==high){ | |
| segTree[st_idx] = baskets[low]; | |
| return; | |
| } | |
| int mid = low + (high-low)/2; |
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 maxTotalFruits(vector<vector<int>>& fruits, int startPos, int k) { | |
| int n = fruits.size(); | |
| //Step-1: Divide the fruits array into 2 parts | |
| vector<vector<int>> left; | |
| int i = 0; | |
| for (; i < n && fruits[i][0] <= startPos; ++i) | |
| left.push_back({ startPos - fruits[i][0], fruits[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: | |
| long long minCost(vector<int>& basket1, vector<int>& basket2) { | |
| int n = basket1.size(); | |
| int min_val = INT_MAX; | |
| map<int,int> balance; | |
| for(int i=0;i<n;++i){ | |
| balance[basket1[i]]++; | |
| balance[basket2[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
| // C++ | |
| class Solution { | |
| public: | |
| int subarrayBitwiseORs(vector<int>& arr) { | |
| unordered_set<int> all_possible_or; | |
| unordered_set<int> or_ending_at_prev; | |
| unordered_set<int> or_ending_here; | |
| for(int i=0;i<arr.size();++i){ | |
| or_ending_here.clear(); |
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 longestSubarray(vector<int>& nums) { | |
| int n = nums.size(); | |
| int max_value = *max_element(nums.begin(),nums.end()); | |
| int max_len = 0; | |
| int curr_len = 0; | |
| for(int i=0;i<n;++i){ | |
| if(nums[i]==max_value){ |
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> smallestSubarrays(vector<int>& nums) { | |
| int n = nums.size(); | |
| int maxval = *max_element(nums.begin(),nums.end()); | |
| int bits = maxval==0 ? 0 : 1+log2(maxval); | |
| //Track next greater element (NGE) | |
| vector<int> nearest_setbit(bits,n); | |
| vector<int> ans(n); |
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 maxSubarrays(int n, vector<vector<int>>& conflictingPairs) { | |
| int m = conflictingPairs.size(); | |
| // Arrange each pair | |
| for(int i=0;i<m;++i) | |
| if(conflictingPairs[i][0]>conflictingPairs[i][1]) | |
| swap(conflictingPairs[i][0],conflictingPairs[i][1]); |