├── DAY 10 ├── DAY 12 ├── DAY 13 ├── DAY 14 ├── DAY 15 ├── DAY 16 ├── DAY 17 ├── DAY 18 ├── DAY 19 ├── DAY 20 ├── DAY 21 ├── DAY 22 ├── DAY 23 ├── DAY 24 ├── DAY 25 ├── DAY 26 ├── DAY 27 ├── DAY11 ├── DAY3 ├── DAY4 ├── DAY5 ├── DAY6 ├── DAY7 ├── DAY9 ├── day2 └── day8 /DAY 10: -------------------------------------------------------------------------------- 1 | https://www.linkedin.com/feed/update/urn:li:activity:7158441780877660161/ 2 | class Solution { 3 | public: 4 | vector dailyTemperatures( vector& temperatures) { 5 | deque deque; 6 | 7 | vector res(temperatures.size(), 0); 8 | 9 | for (int i = temperatures.size() - 1; i >= 0; --i) { 10 | if (deque.empty()) { 11 | deque.push_front(i); 12 | res[i] = 0; 13 | } else { 14 | while (!deque.empty() && temperatures[i] >= temperatures[deque.front()]) { 15 | deque.pop_front(); 16 | } 17 | 18 | if (deque.empty()) { 19 | res[i] = 0; 20 | } else { 21 | res[i] = deque.front() - i; 22 | } 23 | 24 | deque.push_front(i); 25 | } 26 | } 27 | 28 | return res; 29 | } 30 | }; 31 | 32 | 33 | -------------------------------------------------------------------------------- /DAY 12: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector sequentialDigits(int low, int high) { 4 | vector a; 5 | 6 | for (int i = 1; i <= 9; ++i) { 7 | int num = i; 8 | int nextDigit = i + 1; 9 | 10 | while (num <= high && nextDigit <= 9) { 11 | num = num * 10 + nextDigit; 12 | if (low <= num && num <= high) a.push_back(num); 13 | ++nextDigit; 14 | } 15 | } 16 | 17 | sort(a.begin(), a.end()); 18 | return a; 19 | } 20 | }; 21 | 22 | 23 | -------------------------------------------------------------------------------- /DAY 13: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxSumAfterPartitioning(vector& arr, int k) { 4 | int N = arr.size(); 5 | int K = k + 1; 6 | 7 | int dp[k + 1]; 8 | memset(dp, 0, sizeof(dp)); 9 | 10 | for (int start = N - 1; start >= 0; start--) { 11 | int currMax = 0; 12 | int end = min(N, start + k); 13 | 14 | for (int i = start; i < end; i++) { 15 | currMax = max(currMax, arr[i]); 16 | dp[start % K] = max(dp[start % K], dp[(i + 1) % K] + currMax * (i - start + 1)); 17 | } 18 | } 19 | return dp[0]; 20 | } 21 | }; 22 | 23 | 24 | -------------------------------------------------------------------------------- /DAY 14: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | std::string minWindow(std::string s, std::string t) { 4 | if (s.empty() || t.empty() || s.length() < t.length()) { 5 | return ""; 6 | } 7 | 8 | std::vector map(128, 0); 9 | int count = t.length(); 10 | int start = 0, end = 0, minLen = INT_MAX, startIndex = 0; 11 | /// UPVOTE ! 12 | for (char c : t) { 13 | map[c]++; 14 | } 15 | 16 | while (end < s.length()) { 17 | if (map[s[end++]]-- > 0) { 18 | count--; 19 | } 20 | 21 | while (count == 0) { 22 | if (end - start < minLen) { 23 | startIndex = start; 24 | minLen = end - start; 25 | } 26 | 27 | if (map[s[start++]]++ == 0) { 28 | count++; 29 | } 30 | } 31 | } 32 | 33 | return minLen == INT_MAX ? "" : s.substr(startIndex, minLen); 34 | } 35 | }; 36 | -------------------------------------------------------------------------------- /DAY 15: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int firstUniqChar(string s) { 4 | vector oc(26); 5 | //store frequency of each character of s 6 | for (auto i : s) oc[i - 'a']++; 7 | //first character with frequency = 1 is the answer 8 | for (int i = 0; i < s.size(); i++) { 9 | if (oc[s[i] - 'a'] == 1) return i; 10 | } 11 | //no character with frequency = 1 12 | return -1; 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /DAY 16: -------------------------------------------------------------------------------- 1 | 2 | class Solution { 3 | public: 4 | string getSignature(const string& s) { 5 | vector count(26, 0); 6 | for (char c : s) { 7 | count[c - 'a']++; 8 | } 9 | 10 | stringstream ss; 11 | for (int i = 0; i < 26; i++) { 12 | if (count[i] != 0) { 13 | ss << (char)('a' + i) << count[i]; 14 | } 15 | } 16 | return ss.str(); 17 | } 18 | 19 | vector> groupAnagrams(vector& strs) { 20 | vector> result; 21 | unordered_map> groups; 22 | 23 | for (const string& s : strs) { 24 | groups[getSignature(s)].push_back(s); 25 | } 26 | 27 | for (const auto& entry : groups) { 28 | result.push_back(entry.second); 29 | } 30 | 31 | return result; 32 | } 33 | }; 34 | -------------------------------------------------------------------------------- /DAY 17: -------------------------------------------------------------------------------- 1 | 2 | //SORT CHAR BY FREQUENCY 3 | class Solution { 4 | public: 5 | string frequencySort(string s) { 6 | auto cmp = [](const pair& a, const pair& b) { 7 | return a.second < b.second; 8 | }; 9 | 10 | priority_queue, vector>, decltype(cmp)> pq(cmp); 11 | 12 | unordered_map hm; 13 | 14 | for (char c : s) { 15 | hm[c]++; 16 | } 17 | 18 | for (const auto& entry : hm) { 19 | pq.push(make_pair(entry.first, entry.second)); 20 | } 21 | 22 | string result = ""; 23 | while (!pq.empty()) { 24 | pair p = pq.top(); 25 | pq.pop(); 26 | result.append(p.second, p.first); 27 | } 28 | 29 | return result; 30 | } 31 | }; 32 | 33 | 34 | -------------------------------------------------------------------------------- /DAY 18: -------------------------------------------------------------------------------- 1 | //PERFECT SQUARE 2 | class Solution { 3 | public: 4 | int numSquares(int n) { 5 | vector dp(n + 1, INT_MAX); 6 | dp[0] = 0; 7 | for (int i = 1; i <= n; ++i) { 8 | for (int j = 1; j * j <= i; ++j){ 9 | dp[i] = min(dp[i], dp[i - j * j] + 1); 10 | } 11 | } 12 | return dp[n]; 13 | } 14 | }; 15 | 16 | 17 | -------------------------------------------------------------------------------- /DAY 19: -------------------------------------------------------------------------------- 1 | 2 | class Solution { 3 | public: 4 | vector largestDivisibleSubset(vector& nums) { 5 | int n=nums.size(), maxi=1, num=-1; 6 | vectorv; 7 | sort(nums.begin(), nums.end()); 8 | vectordp(n, 1); 9 | for(int i=1; i=0; i--){ 20 | if(maxi==dp[i] && (num==-1 || !(num%nums[i]))){ 21 | v.push_back(nums[i]); 22 | maxi--; 23 | num=nums[i]; 24 | } 25 | } 26 | return v; 27 | } 28 | }; 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /DAY 20: -------------------------------------------------------------------------------- 1 | class Solution { 2 | 3 | public boolean isPalindrome(String s, int left, int right) { 4 | while(left < right) { 5 | if(s.charAt(left++) != s.charAt(right--)) return false; 6 | } 7 | return true; 8 | } 9 | 10 | public int countSubstrings(String s) { 11 | int ans = 0; 12 | int n = s.length(); 13 | for(int i=0;i>& grid) { 5 | int m = grid.size(), n = grid[0].size(); 6 | vector>> dp(m, vector>(n, vector(n, -1))); 7 | dp[0][0][n-1] = grid[0][0] + grid[0][n-1]; 8 | 9 | int ans = 0; 10 | for(int i = 1; i < m; ++i) { 11 | for(int j = 0; j < n; ++j) { // robotA 12 | for(int k = j+1; k < n; ++k) { // robotB 13 | for(int x = -1; x <= 1; ++x) { // x and y all possible combinations 14 | for(int y = -1; y <= 1; ++y) { 15 | int nj = j + x, nk = k + y; 16 | if(nj >= 0 && nj < n && nk >= 0 && nk < n) { 17 | int prev = dp[i-1][nj][nk]; 18 | if(prev != -1) { 19 | dp[i][j][k] = max(dp[i][j][k], prev + grid[i][j] + (j != k ? grid[i][k] : 0)); 20 | } 21 | } 22 | } 23 | } 24 | if(ans < dp[i][j][k]) ans = dp[i][j][k]; 25 | } 26 | } 27 | } 28 | 29 | return ans; 30 | } 31 | }; 32 | -------------------------------------------------------------------------------- /DAY 22: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode(int x) : val(x), next(NULL) {} 7 | * }; 8 | */ 9 | 10 | class Solution { 11 | public: 12 | bool hasCycle(ListNode *head) { 13 | if(head==NULL || head->next ==NULL)return false; 14 | 15 | ListNode *slow=head; 16 | ListNode *fast=head; 17 | 18 | while(fast->next){ 19 | fast=fast->next; 20 | if(fast->next){ 21 | fast=fast->next; 22 | slow=slow->next; 23 | } 24 | 25 | if(slow==fast){ 26 | return true; 27 | } 28 | } 29 | return false; 30 | } 31 | }; 32 | -------------------------------------------------------------------------------- /DAY 23: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findMinArrowShots(vector>& points) { 4 | std::sort(points.begin(), points.end(), [](const auto& a, const auto& b) { 5 | return a[0] < b[0]; 6 | }); 7 | 8 | int arrows = 1; 9 | int end = points[0][1]; 10 | 11 | for (size_t i = 1; i < points.size(); ++i) { 12 | if (points[i][0] > end) { 13 | arrows++; 14 | end = points[i][1]; 15 | } else { 16 | end = std::min(end, points[i][1]); 17 | } 18 | } 19 | 20 | return arrows; 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /DAY 24: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int leastInterval(vector& tasks, int n) { 4 | int freq[26] = {0}; 5 | for(char task : tasks){ 6 | freq[task - 'A']++; 7 | } 8 | sort(begin(freq) , end(freq)); 9 | int chunk = freq[25] - 1; 10 | int idel = chunk * n; 11 | 12 | for(int i=24; i>=0; i--){ 13 | idel -= min(chunk,freq[i]); 14 | } 15 | 16 | return idel < 0 ? tasks.size() : tasks.size() + idel; 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /DAY 25: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | ListNode* reverseList(ListNode* head) { 4 | ListNode* prev = nullptr; 5 | ListNode* curr = head; 6 | while (curr != nullptr) { 7 | ListNode* nextTemp = curr->next; 8 | curr->next = prev; 9 | prev = curr; 10 | curr = nextTemp; 11 | } 12 | return prev; 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /DAY 26: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isPalindrome(ListNode* head) { 4 | vector listVals; 5 | while (head) { 6 | listVals.push_back(head->val); 7 | head = head->next; 8 | } 9 | 10 | int left = 0, right = listVals.size() - 1; 11 | while (left < right && listVals[left] == listVals[right]) { 12 | left++; 13 | right--; 14 | } 15 | return left >= right; 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /DAY 27: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector findDuplicates(vector& nums) { 4 | vectorans; 5 | int n=size(nums); 6 | for(int i=0;i> divideArray(vector& nums, int k) { 4 | int size = nums.size(); 5 | 6 | 7 | sort(nums.begin(), nums.end()); 8 | 9 | vector> result(size / 3, vector(3)); 10 | int groupIndex = 0; 11 | for (int i = 0; i < size; i += 3) { 12 | if (i + 2 < size && nums[i + 2] - nums[i] <= k) { 13 | result[groupIndex] = { nums[i], nums[i + 1], nums[i + 2] }; 14 | groupIndex++; 15 | } 16 | else { 17 | return vector>(); 18 | } 19 | } 20 | return result; 21 | } 22 | }; 23 | 24 | 25 | -------------------------------------------------------------------------------- /DAY3: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | class Solution { 5 | public: 6 | int pseudoPalindromicPaths(TreeNode* root) { 7 | int count = 0, path = 0; 8 | 9 | stack> stk; 10 | stk.push({root, 0}); 11 | 12 | while (!stk.empty()) { 13 | auto [node, path] = stk.top(); 14 | stk.pop(); 15 | 16 | if (node != nullptr) { 17 | 18 | path = path ^ (1 << node->val); 19 | 20 | 21 | if (node->left == nullptr && node->right == nullptr) { 22 | 23 | if ((path & (path - 1)) == 0) { 24 | ++count; 25 | } 26 | } else { 27 | stk.push({node->right, path}); 28 | stk.push({node->left, path}); 29 | } 30 | } 31 | } 32 | 33 | return count; 34 | } 35 | }; 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /DAY4: -------------------------------------------------------------------------------- 1 | //LONGEST SUBSEQUENCE 2 | class Solution { 3 | public: 4 | // Function to find the length of the longest common subsequence in two strings. 5 | int longestCommonSubsequence(string text1, string text2) { 6 | int text1Length = text1.size(), text2Length = text2.size(); 7 | // Create a 2D array to store lengths of common subsequence at each index. 8 | int dp[text1Length + 1][text2Length + 1]; 9 | 10 | // Initialize the 2D array with zero. 11 | memset(dp, 0, sizeof dp); 12 | 13 | // Loop through both strings and fill the dp array. 14 | for (int i = 1; i <= text1Length; ++i) { 15 | for (int j = 1; j <= text2Length; ++j) { 16 | // If current characters match, add 1 to the length of the sequence 17 | // until the previous character from both strings. 18 | if (text1[i - 1] == text2[j - 1]) { 19 | dp[i][j] = dp[i - 1][j - 1] + 1; 20 | } else { 21 | // If current characters do not match, take the maximum length 22 | // achieved by either skipping the current character of text1 or text2. 23 | dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); 24 | } 25 | } 26 | } 27 | 28 | // Return the value in the bottom-right cell which contains the 29 | // length of the longest common subsequence for the entire strings. 30 | return dp[text1Length][text2Length]; 31 | } 32 | }; 33 | -------------------------------------------------------------------------------- /DAY5: -------------------------------------------------------------------------------- 1 | //out of boundary path 2 | class Solution { 3 | public: 4 | int findPaths(int m, int n, int N, int x, int y) { 5 | const int M = 1000000000 + 7; 6 | vector> dp(m, vector(n, 0)); 7 | dp[x][y] = 1; 8 | int count = 0; 9 | 10 | for (int moves = 1; moves <= N; moves++) { 11 | vector> temp(m, vector(n, 0)); 12 | 13 | for (int i = 0; i < m; i++) { 14 | for (int j = 0; j < n; j++) { 15 | if (i == m - 1) count = (count + dp[i][j]) % M; 16 | if (j == n - 1) count = (count + dp[i][j]) % M; 17 | if (i == 0) count = (count + dp[i][j]) % M; 18 | if (j == 0) count = (count + dp[i][j]) % M; 19 | temp[i][j] = ( 20 | ((i > 0 ? dp[i - 1][j] : 0) + (i < m - 1 ? dp[i + 1][j] : 0)) % M + 21 | ((j > 0 ? dp[i][j - 1] : 0) + (j < n - 1 ? dp[i][j + 1] : 0)) % M 22 | ) % M; 23 | } 24 | } 25 | dp = temp; 26 | } 27 | 28 | return count; 29 | } 30 | }; 31 | 32 | 33 | -------------------------------------------------------------------------------- /DAY6: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int kInversePairs(int n, int k) { 4 | int dp[1001][1001] = {1}; 5 | for (int i = 1; i <= n; i++) { 6 | for (int j = 0; j <= k; j++) { 7 | for (int x = 0; x <= min(j, i - 1); x++) { 8 | 9 | if (j - x >= 0) { 10 | dp[i][j] = (dp[i][j] + dp[i - 1][j - x]) % 1000000007; 11 | } 12 | } 13 | } 14 | } 15 | 16 | return dp[n][k]; 17 | } 18 | }; 19 | 20 | 21 | -------------------------------------------------------------------------------- /DAY7: -------------------------------------------------------------------------------- 1 | 2 | //number-of-submatrices-that-sum-to-target 3 | 4 | class Solution { 5 | public: 6 | int numSubmatrixSumTarget(std::vector>& matrix, int target) { 7 | int m = matrix.size(); 8 | int n = matrix[0].size(); 9 | 10 | for (int row = 0; row < m; row++) { 11 | for (int col = 1; col < n; col++) { 12 | matrix[row][col] += matrix[row][col - 1]; 13 | } 14 | } 15 | 16 | int count = 0; 17 | 18 | for (int c1 = 0; c1 < n; c1++) { 19 | for (int c2 = c1; c2 < n; c2++) { 20 | std::unordered_map map; 21 | map[0] = 1; 22 | int sum = 0; 23 | 24 | for (int row = 0; row < m; row++) { 25 | sum += matrix[row][c2] - (c1 > 0 ? matrix[row][c1 - 1] : 0); 26 | count += map[sum - target]; 27 | map[sum]++; 28 | } 29 | } 30 | } 31 | 32 | return count; 33 | } 34 | }; 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /DAY9: -------------------------------------------------------------------------------- 1 | 2 | class Solution { 3 | 4 | long resolves(int a, int b, char Operator){ 5 | if(Operator == '+') return a + b; 6 | else if(Operator == '-') return a - b; 7 | else if(Operator == '*') return (long)a*b; 8 | return a/b; 9 | } 10 | public: 11 | int evalRPN(vector& tokens) { 12 | stack Stack; 13 | int n = tokens.size(); 14 | for(int i = 0; i < n; i++){ 15 | 16 | if(tokens[i].size() == 1 and tokens[i][0] < 48){ 17 | long integer2 = Stack.top(); 18 | Stack.pop(); 19 | long integer1 = Stack.top(); 20 | Stack.pop(); 21 | 22 | string Operator = tokens[i]; 23 | long resolvedAns = resolves(integer1, integer2 , Operator[0]); 24 | Stack.push(resolvedAns); 25 | }else 26 | Stack.push(stol(tokens[i])); 27 | } 28 | return Stack.top(); 29 | } 30 | }; 31 | 32 | -------------------------------------------------------------------------------- /day2: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxLength(vector& arr) { 4 | vector dp = {0}; 5 | int res = 0; 6 | 7 | for (const string& s : arr) { 8 | int a = 0, dup = 0; 9 | for (char c : s) { 10 | dup |= a & (1 << (c - 'a')); 11 | a |= 1 << (c - 'a'); 12 | } 13 | 14 | if (dup > 0) 15 | continue; 16 | 17 | for (int i = dp.size() - 1; i >= 0; i--) { 18 | if ((dp[i] & a) > 0) 19 | continue; 20 | dp.push_back(dp[i] | a); 21 | res = max(res, __builtin_popcount(dp[i] | a)); 22 | } 23 | } 24 | 25 | return res; 26 | } 27 | }; 28 | -------------------------------------------------------------------------------- /day8: -------------------------------------------------------------------------------- 1 | // implement queue using stack 2 | class MyQueue { 3 | private: 4 | stack s1; 5 | stack s2; 6 | 7 | public: 8 | MyQueue() {} 9 | 10 | void push(int x) { 11 | while (!s1.empty()) { 12 | s2.push(s1.top()); 13 | s1.pop(); 14 | } 15 | s1.push(x); 16 | while (!s2.empty()) { 17 | s1.push(s2.top()); 18 | s2.pop(); 19 | } 20 | } 21 | 22 | int pop() { 23 | int temp = s1.top(); 24 | s1.pop(); 25 | return temp; 26 | } 27 | 28 | int peek() { 29 | return s1.top(); 30 | } 31 | 32 | bool empty() { 33 | return s1.empty(); 34 | } 35 | }; 36 | 37 | --------------------------------------------------------------------------------