├── August ├── AUG_21_Repeated_Substring_Pattern.cpp ├── AUG_22_Excel_Sheet_Column_Title.cpp ├── AUG_23_Reorganize_String.cpp ├── AUG_24_Text_justification.cpp ├── AUG_25_Interleaving_String.cpp ├── AUG_26_Maximum_Length_of_Pair_Chain.cpp ├── AUG_27_Frog_Jump.cpp ├── AUG_28_Implement_Stack_using_Queues.cpp ├── AUG_29_Minimum_Penalty_for_a_Shop.cpp ├── AUG_30_Minimum_Replacements_to_Sort_the_Array.cpp └── AUG_31_Minimum_Number_of_Taps_toOpen_to_Water_a_Garden.cpp ├── September ├── SEP_01_Counting_Bits.cpp ├── SEP_02_Extra_Characters_in_a_String.cpp ├── SEP_03_Unique_Paths.cpp ├── SEP_04_Linked_List_Cycle.cpp ├── SEP_05_Copy_List_with_Random_Pointer.cpp ├── SEP_06_Split_Linked_List_in_Parts.cpp ├── SEP_07_Reverse_Linked_List_II.cpp ├── SEP_08_Pascals_Triangle.cpp ├── SEP_09_Combination_Sum_IV.cpp ├── SEP_10_Count_All_Valid_Pickup_and_Delivery_Options.cpp ├── SEP_11_Group_the_People_Given_the_Group_Size_They_Belong_To.cpp ├── SEP_12_Minimum_Deletions_to_Make_Character_Frequencies_Unique.cpp ├── SEP_13_Candy.cpp ├── SEP_14_Reconstruct_Itinerary.cpp ├── SEP_15_Min_Cost_to_Connect_All_Points.cpp ├── SEP_16_Path_With_Minimum_Effort.cpp ├── SEP_17_Shortest_Path_Visiting_All_Nodes.cpp ├── SEP_18_The_K_Weakest_Rows_in_a_Matrix.cpp ├── SEP_19_Find_the_Duplicate_Number.cpp ├── SEP_20_Minimum_Operations_to_Reduce_X_to_Zero.cpp ├── SEP_21_Median_of_Two_Sorted_Arrays.cpp ├── SEP_22_Is_Subsequence.cpp ├── SEP_23_Longest_String_Chain.cpp ├── SEP_24_Champagne_Tower.cpp ├── SEP_25_Find_the_Difference.cpp ├── SEP_26_Remove_Duplicate_Letters.cpp ├── SEP_27_Decoded_String_at_Index.cpp ├── SEP_28_Sort_Array_By_Parity.cpp ├── SEP_29_Monotonic_Array.cpp └── SEP_30_132_Pattern.cpp ├── november ├── NOV_1_Find_Mode_in_Binary_Search_Tree.cpp ├── NOV_2_Count_Nodes_Equal_to_Average_of_Subtree.cpp └── NOV_3_Build_an_Array_With_Stack_Operations.cpp └── october ├── OCT_01_Reverse_Words_in_a_String_III.cpp ├── OCT_02_Remove_Colored_Pieces_if_Both_Neighbors_are_the_Same_Color.cpp ├── OCT_03_Number_of_Good_Pairs.cpp ├── OCT_04_Design_HashMap.cpp ├── OCT_05_Majority_Element_II.cpp ├── OCT_06_Integer_Break.cpp ├── OCT_07_Build_Array_Where_You_Can_Find_The_Maximum_Exactly_K_Comparisons.cpp ├── OCT_08_Max_Dot_Product_of_Two_Subsequences.cpp ├── OCT_09_Find_First_and_Last_Position_of_Element_in_Sorted_Array.cpp ├── OCT_10_Minimum_Number_of_Operations_to_Make_Array_Continuous.cpp ├── OCT_11_Number_of_Flowers_in_Full_Bloom.cpp ├── OCT_12_Find_in_Mountain_Array.cpp ├── OCT_13_Min_Cost_Climbing_Stairs.cpp ├── OCT_14_Painting_the_Walls.cpp ├── OCT_15_Number_of_Ways_to_Stay_in_the_Same_Place_After_Some_Steps.cpp ├── OCT_16.cpp └── OCT_28_Count_Vowels_Permutation.cpp /August/AUG_21_Repeated_Substring_Pattern.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | bool isRepeated(string substr, string s) 4 | { 5 | if (s.size() % substr.size() != 0 || substr.size() > s.size() / 2) 6 | return false; 7 | 8 | cout << substr << endl; 9 | int i, j; 10 | for (i = 0, j = 0; i < s.size(); i++, j %= substr.size()) 11 | { 12 | if (s[i] != substr[j++]) 13 | return false; 14 | } 15 | return true; 16 | } 17 | 18 | public: 19 | bool repeatedSubstringPattern(string s) 20 | { 21 | string substr; 22 | for (int i = 0; i < s.size(); i++) 23 | { 24 | substr += s[i]; 25 | if (isRepeated(substr, s)) 26 | return true; 27 | } 28 | return false; 29 | } 30 | }; -------------------------------------------------------------------------------- /August/AUG_22_Excel_Sheet_Column_Title.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | stack stack; 4 | void fun(int num) 5 | { 6 | if (num == 0) 7 | return; 8 | 9 | if (num % 26 == 0) 10 | { 11 | stack.push('Z'); 12 | num--; 13 | } 14 | else 15 | stack.push(64 + (num % 26)); 16 | 17 | fun(num / 26); 18 | } 19 | 20 | public: 21 | string convertToTitle(int columnNumber) 22 | { 23 | string ans; 24 | fun(columnNumber); 25 | while (!stack.empty()) 26 | { 27 | ans.push_back(stack.top()); 28 | stack.pop(); 29 | } 30 | return ans; 31 | } 32 | }; -------------------------------------------------------------------------------- /August/AUG_23_Reorganize_String.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | void display(auto map) 4 | { 5 | for (auto i : map) 6 | cout << i.first << " " << i.second << endl; 7 | } 8 | 9 | public: 10 | string reorganizeString(string s) 11 | { 12 | string ans(s.size(), ' '); 13 | 14 | unordered_map map; 15 | // step 1 count the frequency 16 | for (auto i : s) 17 | { 18 | map[i]++; 19 | } 20 | 21 | // step 2 find the element with max frequency 22 | pair maxFreq; //{char, freq} 23 | maxFreq.second = 0; 24 | for (auto i : map) 25 | { 26 | if (maxFreq.second < i.second) 27 | maxFreq = i; 28 | } 29 | 30 | // edge case 31 | if (maxFreq.second > (s.size() + 1) / 2) 32 | return ""; 33 | 34 | // step 3 evenly distrubute that element ex:- at (0,2,4,...) 35 | int i = 0; 36 | while (map[maxFreq.first]-- && i < ans.size()) 37 | { 38 | ans[i] = maxFreq.first; 39 | i += 2; 40 | } 41 | display(map); 42 | // step4 put rest element remain in the map 43 | for (auto it = map.begin(); it != map.end(); ++it) 44 | { 45 | while (it->second > 0) 46 | { 47 | if (i >= ans.size()) 48 | i = 1; 49 | ans[i] = it->first; 50 | it->second--; 51 | i += 2; 52 | } 53 | } 54 | // cout< fullJustify(vector &words, int maxWidth) 40 | { 41 | vector ans; 42 | vector container; 43 | int count = 0; 44 | 45 | for (auto word : words) 46 | { 47 | // checking word will fit or not 48 | // I used (container.size() - 1) to ensure that the space required for separating words 49 | if (count + word.size() + container.size() <= maxWidth) 50 | { 51 | container.push_back(word); 52 | count += word.size(); 53 | } 54 | else 55 | { 56 | ans.push_back(getLine(container, maxWidth)); 57 | container.clear(); 58 | container.push_back(word); 59 | count = word.size(); 60 | } 61 | } 62 | 63 | // creating last line with remaining words in container 64 | string lastLine = ""; 65 | for (auto word : container) 66 | { 67 | lastLine += word; 68 | if (lastLine.size() < maxWidth) 69 | { 70 | lastLine += ' '; 71 | } 72 | } 73 | // appending remaining space at the end of the line 74 | lastLine += string(maxWidth - lastLine.size(), ' '); 75 | ans.push_back(lastLine); 76 | 77 | return ans; 78 | } 79 | }; -------------------------------------------------------------------------------- /August/AUG_25_Interleaving_String.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | bool recursion(const string &s1, const string &s2, const string &s3, int i, int j, int k, auto &dp) 4 | { 5 | if (dp[i][j] != -1) 6 | return dp[i][j]; 7 | // base case 8 | if (i == s1.size() && j == s2.size() && k == s3.size()) 9 | return true; 10 | 11 | bool x = false, y = false; 12 | if (i < s1.size() && s1[i] == s3[k]) 13 | { 14 | x = recursion(s1, s2, s3, i + 1, j, k + 1, dp); 15 | } 16 | if (j < s2.size() && s2[j] == s3[k]) 17 | { 18 | y = recursion(s1, s2, s3, i, j + 1, k + 1, dp); 19 | } 20 | 21 | return dp[i][j] = x || y; 22 | } 23 | 24 | public: 25 | bool isInterleave(string s1, string s2, string s3) 26 | { 27 | if (s1.size() + s2.size() != s3.size()) 28 | return false; 29 | 30 | vector> dp(s1.size() + 1, vector(s2.size() + 1, -1)); 31 | return recursion(s1, s2, s3, 0, 0, 0, dp); 32 | } 33 | }; -------------------------------------------------------------------------------- /August/AUG_26_Maximum_Length_of_Pair_Chain.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | static bool cmp(const vector &v1, const vector &v2) 4 | { 5 | return v1[1] < v2[1]; 6 | } 7 | 8 | public: 9 | int findLongestChain(vector> &pairs) 10 | { 11 | if (pairs.size() == 0) 12 | return 0; 13 | 14 | sort(pairs.begin(), pairs.end(), cmp); 15 | 16 | vector curr = pairs[0]; 17 | int chain = 1; 18 | 19 | for (auto pair : pairs) 20 | { 21 | if (curr[1] < pair[0]) 22 | { 23 | chain++; 24 | curr = pair; 25 | } 26 | } 27 | 28 | return chain; 29 | } 30 | }; -------------------------------------------------------------------------------- /August/AUG_27_Frog_Jump.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | bool canCross(vector &stones) 5 | { 6 | map> hashTable; 7 | 8 | for (auto i : stones) 9 | { 10 | hashTable.insert({i, set()}); 11 | } 12 | // there is one way for starting 13 | hashTable[0].insert(1); 14 | for (int i = 0; i < stones.size(); i++) 15 | { 16 | int stone = stones[i]; 17 | 18 | for (auto k : hashTable[stone]) 19 | { 20 | // k jump laga ke next pe pahunche 21 | int next = stone + k; 22 | // if next stone exit 23 | if (hashTable.find(next) != hashTable.end()) 24 | { 25 | if (k - 1 > 0) 26 | { 27 | hashTable[next].insert(k - 1); 28 | } 29 | hashTable[next].insert(k + 1); 30 | hashTable[next].insert(k); 31 | } 32 | } 33 | } 34 | 35 | //{stones, set()} rbegin() last index 36 | // second -> set{k1,k2,k3} 37 | return !hashTable.rbegin()->second.empty(); 38 | } 39 | }; -------------------------------------------------------------------------------- /August/AUG_28_Implement_Stack_using_Queues.cpp: -------------------------------------------------------------------------------- 1 | class MyStack 2 | { 3 | deque q; 4 | // q back will be top(); 5 | public: 6 | MyStack() 7 | { 8 | } 9 | 10 | void push(int x) 11 | { 12 | q.push_back(x); 13 | } 14 | 15 | int pop() 16 | { 17 | if (!empty()) 18 | { 19 | int poped = top(); 20 | q.pop_back(); 21 | return poped; 22 | } 23 | return -1; 24 | } 25 | 26 | int top() 27 | { 28 | if (!empty()) 29 | { 30 | return q.back(); 31 | } 32 | return -1; 33 | } 34 | 35 | bool empty() 36 | { 37 | return q.empty(); 38 | } 39 | }; 40 | 41 | /** 42 | * Your MyStack object will be instantiated and called as such: 43 | * MyStack* obj = new MyStack(); 44 | * obj->push(x); 45 | * int param_2 = obj->pop(); 46 | * int param_3 = obj->top(); 47 | * bool param_4 = obj->empty(); 48 | */ -------------------------------------------------------------------------------- /August/AUG_29_Minimum_Penalty_for_a_Shop.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | int bestClosingTime(string customers) 5 | { 6 | // APPROCH 7 | // total penality is (pre N) + (post Y) 8 | // number of customer don't comes before close 9 | // number of customer comes at or after close 10 | 11 | int n = customers.size(); 12 | vector pre_N(n), post_Y(n); 13 | 14 | // how many y or n ith index 15 | pre_N[0] = (customers[0] == 'N'); 16 | post_Y[n - 1] = (customers[n - 1] == 'Y'); 17 | 18 | for (int i = 1; i < n; i++) 19 | { 20 | if (customers[i] == 'N') 21 | pre_N[i] = pre_N[i - 1] + 1; 22 | else 23 | pre_N[i] = pre_N[i - 1]; 24 | 25 | if (customers[n - 1 - i] == 'Y') 26 | post_Y[n - 1 - i] = post_Y[n - i] + 1; 27 | else 28 | post_Y[n - 1 - i] = post_Y[n - i]; 29 | } 30 | 31 | int minPenality = INT_MAX, best_time; 32 | for (int i = 0; i < n; i++) 33 | { 34 | // before close '<' 35 | int preN = (i - 1 < 0) ? 0 : pre_N[i - 1]; 36 | // close or after close '>=' 37 | int postY = post_Y[i]; 38 | int penality = preN + postY; 39 | 40 | if (penality < minPenality) 41 | { 42 | minPenality = penality; 43 | best_time = i; 44 | } 45 | } 46 | // closing the show at the end of the day 47 | if (pre_N[n - 1] < minPenality) 48 | best_time = n; 49 | 50 | return best_time; 51 | } 52 | }; -------------------------------------------------------------------------------- /August/AUG_30_Minimum_Replacements_to_Sort_the_Array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | long long minimumReplacement(vector& nums) { 4 | long long Total_Operation = 0; 5 | 6 | for(int i = nums.size()-2; i>=0; i--){ 7 | if(nums[i] > nums[i+1]){ 8 | int parts = nums[i] / nums[i+1]; 9 | if(nums[i] % nums[i+1] != 0) 10 | parts++; 11 | 12 | int operation = parts - 1; 13 | Total_Operation += operation; 14 | 15 | nums[i] = nums[i] / parts; 16 | } 17 | } 18 | 19 | return Total_Operation; 20 | } 21 | }; -------------------------------------------------------------------------------- /August/AUG_31_Minimum_Number_of_Taps_toOpen_to_Water_a_Garden.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | int minTaps(int n, vector &ranges) 5 | { 6 | vector range(n + 1); 7 | for (int i = 0; i <= n; i++) 8 | { 9 | int left = max(0, i - ranges[i]); 10 | int right = min(n, i + ranges[i]); 11 | range[left] = max(range[left], right); 12 | } 13 | 14 | int maxend = 0, currend = 0, taps = 0; 15 | for (int i = 0; i <= n; i++) 16 | { 17 | if (i > maxend) 18 | return -1; 19 | if (i > currend) 20 | { 21 | taps++; 22 | currend = maxend; 23 | } 24 | maxend = max(maxend, range[i]); 25 | } 26 | return taps; 27 | } 28 | }; -------------------------------------------------------------------------------- /September/SEP_01_Counting_Bits.cpp: -------------------------------------------------------------------------------- 1 | <<<<<<< HEAD 2 | class Solution 3 | { 4 | int count(int n) 5 | { 6 | int i = 0; 7 | while (n) 8 | { 9 | n &= (n - 1); 10 | i++; 11 | } 12 | return i; 13 | } 14 | 15 | public: 16 | vector countBits(int n) 17 | { 18 | vector ans(n + 1); 19 | for (int i = 0; i <= n; i++) 20 | { 21 | ans[i] = count(i); 22 | } 23 | return ans; 24 | } 25 | }; 26 | ======= 27 | class Solution 28 | { 29 | int count(int n) 30 | { 31 | int i = 0; 32 | while (n) 33 | { 34 | n &= (n - 1); 35 | i++; 36 | } 37 | return i; 38 | } 39 | 40 | public: 41 | vector countBits(int n) 42 | { 43 | vector ans(n + 1); 44 | for (int i = 0; i <= n; i++) 45 | { 46 | ans[i] = count(i); 47 | } 48 | return ans; 49 | } 50 | }; 51 | >>>>>>> 20b74905543e6f2c1e9f6645cdcc1758f7124c27 52 | -------------------------------------------------------------------------------- /September/SEP_02_Extra_Characters_in_a_String.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | static bool cmp(string &s1, string &s2) 4 | { 5 | return s1.size() > s2.size(); 6 | } 7 | int extraChar(string &s, int i, auto &hashTable, auto &dp) 8 | { 9 | if (i >= s.size()) 10 | return 0; 11 | 12 | if (dp[i] != -1) 13 | return dp[i]; 14 | 15 | int val = INT_MAX; 16 | 17 | for (auto word : hashTable[s[i]]) 18 | { 19 | 20 | // is word length fit 21 | if (i + word.size() <= s.size()) 22 | { 23 | 24 | // pick a word and start matching 25 | bool matched = true; 26 | int j = 0; 27 | while (j < word.size()) 28 | { 29 | if (word[j] != s[i + j]) 30 | { 31 | matched = false; 32 | break; 33 | } 34 | j++; 35 | } 36 | 37 | // INCLUDE THE WORD 38 | if (matched) 39 | { 40 | val = min(val, extraChar(s, i + word.size(), hashTable, dp)); 41 | } 42 | } 43 | } 44 | 45 | // EXCLUDE THE FIRST CHAR (assume as extra) 46 | val = min(val, extraChar(s, i + 1, hashTable, dp) + 1); 47 | return dp[i] = val; 48 | } 49 | 50 | public: 51 | int minExtraChar(string s, vector &dictionary) 52 | { 53 | // map the distionary for ease in accessing 54 | sort(dictionary.begin(), dictionary.end(), cmp); 55 | 56 | // longer word will come first. be greedy larger matching word is better than smaller one 57 | 58 | unordered_map> hashTable; 59 | for (auto str : dictionary) 60 | { 61 | hashTable[str[0]].push_back(str); 62 | } 63 | 64 | vector dp(s.size() + 1, -1); 65 | return extraChar(s, 0, hashTable, dp); 66 | } 67 | }; -------------------------------------------------------------------------------- /September/SEP_03_Unique_Paths.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | int totalWays(int row, int col, vector> &dp) 4 | { 5 | if (row < 0 || col < 0) 6 | return 0; 7 | if (row == 0 && col == 0) 8 | return 1; 9 | 10 | if (dp[row][col] != -1) 11 | return dp[row][col]; 12 | 13 | return dp[row][col] = totalWays(row - 1, col, dp) + totalWays(row, col - 1, dp); 14 | } 15 | 16 | public: 17 | int uniquePaths(int m, int n) 18 | { 19 | vector> dp(m, vector(n, -1)); 20 | return totalWays(m - 1, n - 1, dp); 21 | } 22 | }; -------------------------------------------------------------------------------- /September/SEP_04_Linked_List_Cycle.cpp: -------------------------------------------------------------------------------- 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 | class Solution 10 | { 11 | public: 12 | bool hasCycle(ListNode *head) 13 | { 14 | if (!head) 15 | return false; 16 | ListNode *fast = head, *slow = head; 17 | 18 | while (fast) 19 | { 20 | slow = slow->next; 21 | fast = fast->next; 22 | if (!fast) 23 | return false; 24 | 25 | fast = fast->next; 26 | if (slow == fast) 27 | return true; 28 | } 29 | return false; 30 | } 31 | }; -------------------------------------------------------------------------------- /September/SEP_05_Copy_List_with_Random_Pointer.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | // Definition for a Node. 3 | class Node { 4 | public: 5 | int val; 6 | Node* next; 7 | Node* random; 8 | 9 | Node(int _val) { 10 | val = _val; 11 | next = NULL; 12 | random = NULL; 13 | } 14 | }; 15 | */ 16 | 17 | class Solution 18 | { 19 | public: 20 | Node *copyRandomList(Node *head) 21 | { 22 | unordered_map map; 23 | Node *temp = head; 24 | int i = 0; 25 | while (temp) 26 | { 27 | map[temp] = i++; 28 | temp = temp->next; 29 | } 30 | // 7 0 31 | // 13 1 32 | // 11 2 33 | // 10 3 34 | // 1 4 35 | 36 | Node *newHead = NULL, *tail = NULL; 37 | temp = head; 38 | while (temp) 39 | { 40 | Node *newnode = new Node(temp->val); 41 | if (!newHead) 42 | { 43 | newHead = newnode; 44 | } 45 | else 46 | { 47 | tail->next = newnode; 48 | } 49 | tail = newnode; 50 | temp = temp->next; 51 | } 52 | 53 | vector aux; 54 | temp = newHead; 55 | while (temp) 56 | { 57 | aux.push_back(temp); 58 | temp = temp->next; 59 | } 60 | 61 | temp = head; 62 | Node *newTemp = newHead; 63 | 64 | while (temp) 65 | { 66 | Node *rand = NULL; 67 | if (temp->random) 68 | { 69 | int i = map[temp->random]; 70 | rand = aux[i]; 71 | } 72 | newTemp->random = rand; 73 | temp = temp->next; 74 | newTemp = newTemp->next; 75 | } 76 | return newHead; 77 | } 78 | }; -------------------------------------------------------------------------------- /September/SEP_06_Split_Linked_List_in_Parts.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode() : val(0), next(nullptr) {} 7 | * ListNode(int x) : val(x), next(nullptr) {} 8 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 9 | * }; 10 | */ 11 | class Solution 12 | { 13 | ListNode *tail = NULL; 14 | void insert(ListNode *&list, int val) 15 | { 16 | ListNode *newnode = new ListNode(val); 17 | if (!list) 18 | { 19 | list = newnode; 20 | } 21 | else 22 | { 23 | tail->next = newnode; 24 | } 25 | tail = newnode; 26 | } 27 | int getSize(ListNode *head) 28 | { 29 | int size = 0; 30 | while (head) 31 | { 32 | size++; 33 | head = head->next; 34 | } 35 | return size; 36 | } 37 | 38 | public: 39 | vector splitListToParts(ListNode *head, int k) 40 | { 41 | vector ans(k, NULL); 42 | int size = getSize(head), i = 0; 43 | if (k >= size) 44 | { 45 | while (head) 46 | { 47 | ListNode *newnode = new ListNode(head->val); 48 | ans[i++] = newnode; 49 | head = head->next; 50 | } 51 | return ans; 52 | } 53 | 54 | // in this case partition 55 | int partitionSize = size / k, extra = size % k; 56 | while (head) 57 | { 58 | ListNode *list = NULL; 59 | 60 | if (head && extra) 61 | { 62 | // cout<val<<" "; 63 | insert(list, head->val); 64 | head = head->next; 65 | extra--; 66 | } 67 | 68 | for (int i = 0; i < partitionSize && head; i++) 69 | { 70 | // cout<val<<" "; 71 | insert(list, head->val); 72 | head = head->next; 73 | } 74 | cout << endl; 75 | ans[i++] = list; 76 | } 77 | return ans; 78 | } 79 | }; -------------------------------------------------------------------------------- /September/SEP_07_Reverse_Linked_List_II.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode() : val(0), next(nullptr) {} 7 | * ListNode(int x) : val(x), next(nullptr) {} 8 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 9 | * }; 10 | */ 11 | class Solution 12 | { 13 | ListNode *reverse(ListNode *curr, ListNode *tail) 14 | { 15 | ListNode *prevNode, *nextNode, *temp = curr; 16 | 17 | prevNode = nextNode = NULL; 18 | while (curr != tail) 19 | { 20 | nextNode = curr->next; 21 | curr->next = prevNode; 22 | prevNode = curr; 23 | curr = nextNode; 24 | } 25 | temp->next = tail; 26 | return prevNode; 27 | } 28 | 29 | public: 30 | ListNode *reverseBetween(ListNode *head, int left, int right) 31 | { 32 | if (left == right) 33 | return head; 34 | 35 | ListNode *temp = head, *leftNode = head, *rightNode; 36 | int i = 1; 37 | while (temp) 38 | { 39 | if (i == left - 1) 40 | leftNode = temp; 41 | if (i == right) 42 | rightNode = temp; 43 | i++; 44 | temp = temp->next; 45 | } 46 | if (left == 1) 47 | head = reverse(leftNode, rightNode->next); 48 | else 49 | leftNode->next = reverse(leftNode->next, rightNode->next); 50 | 51 | return head; 52 | } 53 | }; -------------------------------------------------------------------------------- /September/SEP_08_Pascals_Triangle.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | vector> generate(int numRows) 5 | { 6 | vector> PascalTriangle; 7 | 8 | PascalTriangle.push_back({1}); 9 | if (numRows == 1) 10 | return PascalTriangle; 11 | 12 | PascalTriangle.push_back({1, 1}); 13 | if (numRows == 2) 14 | return PascalTriangle; 15 | 16 | for (int i = 1; i < numRows - 1; i++) 17 | { 18 | vector row = {1}; 19 | for (int j = 0; j < PascalTriangle[i].size() - 1; j++) 20 | { 21 | row.push_back(PascalTriangle[i][j] + PascalTriangle[i][j + 1]); 22 | } 23 | row.push_back({1}); 24 | PascalTriangle.push_back(row); 25 | } 26 | return PascalTriangle; 27 | } 28 | }; -------------------------------------------------------------------------------- /September/SEP_09_Combination_Sum_IV.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | vector dp; 4 | int solve(vector &nums, int target) 5 | { 6 | if (target == 0) 7 | { 8 | return dp[target] = 1; 9 | } 10 | if (target < 0) 11 | { 12 | return 0; 13 | } 14 | // applying dp 15 | if (dp[target] != -1) 16 | { 17 | return dp[target]; 18 | } 19 | 20 | int combination = 0; 21 | for (auto num : nums) 22 | { 23 | combination += solve(nums, target - num); 24 | } 25 | return dp[target] = combination; 26 | } 27 | 28 | public: 29 | int combinationSum4(vector &nums, int target) 30 | { 31 | dp.resize(target + 1, -1); 32 | return solve(nums, target); 33 | } 34 | }; -------------------------------------------------------------------------------- /September/SEP_10_Count_All_Valid_Pickup_and_Delivery_Options.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | int mod = 1e9 + 7; 4 | 5 | public: 6 | int countOrders(int n) 7 | { 8 | if (n == 1) 9 | return 1; 10 | long long result = 1; 11 | for (int i = 2; i <= n; i++) 12 | { 13 | int space = (i - 1) * 2 + 1; 14 | int currPossibilities = ((space * (space + 1)) % mod) / 2; 15 | result = (result % mod * currPossibilities % mod) % mod; 16 | } 17 | return result; 18 | } 19 | }; -------------------------------------------------------------------------------- /September/SEP_11_Group_the_People_Given_the_Group_Size_They_Belong_To.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | vector> groupThePeople(vector &groupSizes) 5 | { 6 | unordered_map> map; 7 | vector> ans; 8 | for (int i = 0; i < groupSizes.size(); i++) 9 | { 10 | // group became full 11 | int size = groupSizes[i]; 12 | map[size].push_back(i); 13 | 14 | if (map[size].size() == size) 15 | { 16 | ans.push_back(map[size]); 17 | map[size].clear(); 18 | } 19 | } 20 | return ans; 21 | } 22 | }; -------------------------------------------------------------------------------- /September/SEP_12_Minimum_Deletions_to_Make_Character_Frequencies_Unique.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | int minDeletions(string s) 5 | { 6 | unordered_map map; 7 | unordered_set set; 8 | for (auto c : s) 9 | { 10 | map[c]++; 11 | } 12 | int deletes = 0; 13 | for (auto &pair : map) 14 | { 15 | while (pair.second != 0 && set.find(pair.second) != set.end()) 16 | { 17 | pair.second--; 18 | deletes++; 19 | } 20 | if (pair.second != 0) 21 | { 22 | set.insert(pair.second); 23 | } 24 | } 25 | return deletes; 26 | } 27 | }; -------------------------------------------------------------------------------- /September/SEP_13_Candy.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | int candy(vector &ratings) 5 | { 6 | int n = ratings.size(); 7 | vector vec(n, 1); 8 | 9 | // compare with left neighbours 10 | for (int i = 0; i < n; i++) 11 | { 12 | if (i - 1 >= 0 && ratings[i] > ratings[i - 1]) 13 | { 14 | vec[i] = vec[i - 1] + 1; 15 | } 16 | } 17 | for (auto i : vec) 18 | cout << i << " "; 19 | cout << endl; 20 | // compare with right neighbours 21 | for (int i = n - 1; i >= 0; i--) 22 | { 23 | if (i + 1 < n && ratings[i] > ratings[i + 1]) 24 | { 25 | vec[i] = max(vec[i], vec[i + 1] + 1); 26 | } 27 | } 28 | for (auto i : vec) 29 | cout << i << " "; 30 | 31 | int sum = accumulate(vec.begin(), vec.end(), 0); 32 | return sum; 33 | } 34 | }; -------------------------------------------------------------------------------- /September/SEP_14_Reconstruct_Itinerary.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | unordered_map> adj; 4 | vector route; 5 | 6 | void dfs(string airport) 7 | { 8 | while (!adj[airport].empty()) 9 | { 10 | string next = *(adj[airport].begin()); 11 | adj[airport].erase(adj[airport].begin()); 12 | dfs(next); 13 | } 14 | route.push_back(airport); 15 | } 16 | 17 | public: 18 | vector findItinerary(vector> &tickets) 19 | { 20 | for (auto ticket : tickets) 21 | { 22 | adj[ticket[0]].insert(ticket[1]); 23 | } 24 | 25 | dfs("JFK"); 26 | 27 | return vector(route.rbegin(), route.rend()); 28 | } 29 | }; 30 | -------------------------------------------------------------------------------- /September/SEP_15_Min_Cost_to_Connect_All_Points.cpp: -------------------------------------------------------------------------------- 1 | class pair_hash 2 | { 3 | public: 4 | size_t operator()(const pair point) const 5 | { 6 | size_t h1 = hash{}(point.first); 7 | size_t h2 = hash{}(point.second); 8 | return h1 ^ h2; 9 | } 10 | }; 11 | class Solution 12 | { 13 | void init(auto &keys, auto &points) 14 | { 15 | for (auto point : points) 16 | { 17 | keys[{point[0], point[1]}] = INT_MAX; 18 | } 19 | } 20 | void initFalse(auto &set, auto &points) 21 | { 22 | for (auto point : points) 23 | { 24 | set[{point[0], point[1]}] = false; 25 | } 26 | } 27 | 28 | public: 29 | int minCostConnectPoints(vector> &points) 30 | { 31 | unordered_map, vector>, pair_hash> adj; 32 | for (int i = 0; i < points.size(); i++) 33 | { 34 | int Xi = points[i][0]; 35 | int Yi = points[i][1]; 36 | for (int j = 0; j < points.size(); j++) 37 | { 38 | if (i != j) 39 | { 40 | int Yj = points[j][1]; 41 | int Xj = points[j][0]; 42 | int distance = abs(Xi - Xj) + abs(Yi - Yj); 43 | adj[{Xi, Yi}].push_back({Xj, Yj, distance}); 44 | } 45 | } 46 | } 47 | 48 | // finding minimum spanning tree (Prim's Algo) 49 | int V = points.size(), cost = 0; 50 | unordered_map, int, pair_hash> keys; 51 | unordered_map, bool, pair_hash> set; 52 | initFalse(set, points); 53 | init(keys, points); 54 | keys.begin()->second = 0; 55 | for (int i = 0; i < V; i++) 56 | { 57 | pair u = {-1, -1}; 58 | // keys[{x,y}] = distance 59 | // .first .second 60 | for (auto it : keys) 61 | { 62 | if (!set[it.first]) 63 | { 64 | if ((u.first == -1 && u.second == -1) || it.second < keys[u]) 65 | { 66 | u = it.first; 67 | } 68 | } 69 | } 70 | 71 | set[u] = true; 72 | cost += keys[u]; 73 | 74 | for (auto neighbour : adj[u]) 75 | { 76 | pair v = {neighbour[0], neighbour[1]}; 77 | int distance = neighbour[2]; 78 | if (!set[v]) 79 | { 80 | keys[v] = min(keys[v], distance); 81 | } 82 | } 83 | } 84 | return cost; 85 | } 86 | }; -------------------------------------------------------------------------------- /September/SEP_16_Path_With_Minimum_Effort.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minimumEffortPath(vector>& heights) { 4 | int n = heights.size(); 5 | int m = heights[0].size(); 6 | vector> dp(n, vector(m, INT_MAX)); 7 | vector> directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; 8 | 9 | priority_queue, vector>, greater>> pq; 10 | 11 | pq.push({0, 0, 0}); 12 | dp[0][0] = 0; 13 | 14 | while (!pq.empty()) { 15 | auto [effort, i, j] = pq.top(); 16 | pq.pop(); 17 | 18 | if (i == n-1 && j == m-1) { 19 | return effort; 20 | } 21 | 22 | for (auto& direction : directions) { 23 | int ni = i + direction.first; 24 | int nj = j + direction.second; 25 | 26 | if (ni >= 0 && ni < n && nj >= 0 && nj < m) { 27 | int newEffort = max(effort, abs(heights[i][j] - heights[ni][nj])); 28 | 29 | if (newEffort < dp[ni][nj]) { 30 | dp[ni][nj] = newEffort; 31 | pq.push({newEffort, ni, nj}); 32 | } 33 | } 34 | } 35 | } 36 | 37 | return -1; // Return -1 if no path is found (this should not happen in this problem) 38 | } 39 | }; 40 | -------------------------------------------------------------------------------- /September/SEP_17_Shortest_Path_Visiting_All_Nodes.cpp: -------------------------------------------------------------------------------- 1 | // question no 847 2 | 3 | class Hash 4 | { 5 | public: 6 | size_t operator()(const pair &p) const 7 | { 8 | size_t h1 = std::hash{}(p.first); 9 | size_t h2 = std::hash{}(p.second); 10 | return (h1 ^ h2); 11 | } 12 | }; 13 | class Solution 14 | { 15 | public: 16 | int shortestPathLength(vector> &graph) 17 | { 18 | int n = graph.size(); 19 | // there is no edge 20 | if (n == 0 || n == 1) 21 | return 0; 22 | 23 | queue> q; 24 | unordered_set, Hash> visited; 25 | 26 | for (int i = 0; i < n; i++) 27 | { 28 | q.push({i, (1 << i)}); 29 | visited.insert({i, (1 << i)}); 30 | } 31 | 32 | int allVisited = (1 << n) - 1; 33 | int paths = 0; 34 | 35 | while (!q.empty()) 36 | { 37 | int size = q.size(); 38 | paths++; 39 | while (size--) 40 | { 41 | 42 | auto curr = q.front(); 43 | q.pop(); 44 | 45 | int currNode = curr.first; 46 | int currMask = curr.second; 47 | 48 | for (auto &adj : graph[currNode]) 49 | { 50 | // combine currmast with ongoing node 51 | int nextMask = currMask | (1 << adj); 52 | 53 | if (nextMask == allVisited) 54 | { 55 | return paths; 56 | } 57 | 58 | if (visited.find({adj, nextMask}) == visited.end()) 59 | { 60 | visited.insert({adj, nextMask}); 61 | q.push({adj, nextMask}); 62 | } 63 | } 64 | } 65 | } 66 | return -1; 67 | } 68 | }; -------------------------------------------------------------------------------- /September/SEP_18_The_K_Weakest_Rows_in_a_Matrix.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> arr; 4 | vector kWeakestRows(vector>& mat, int k) { 5 | for(int i = 0; i vec; 17 | for(int i = 0; i& nums) { 4 | //Intution use the two pointer approch as you used in Linked List while 5 | //detecting loop position 6 | 7 | //detect loop 8 | int slow = -1, fast = 0; 9 | while(slow != fast){ 10 | if(slow == -1) slow++; 11 | slow = nums[slow]; 12 | fast = nums[fast]; 13 | fast = nums[fast]; 14 | } 15 | 16 | 17 | //find loop size 18 | slow = nums[slow]; 19 | int loopSize = 1; 20 | while(slow != fast){ 21 | slow = nums[slow]; 22 | loopSize++; 23 | } 24 | 25 | //move one pointer by loopSize 26 | fast = nums[0]; 27 | slow = nums[0]; 28 | while(loopSize--){ 29 | slow = nums[slow]; 30 | } 31 | while(slow != fast){ 32 | slow = nums[slow]; 33 | fast = nums[fast]; 34 | } 35 | return slow; 36 | } 37 | }; -------------------------------------------------------------------------------- /September/SEP_20_Minimum_Operations_to_Reduce_X_to_Zero.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minOperations(vector& nums, int x) { 4 | int totalSum = accumulate(nums.begin(), nums.end(), 0); 5 | int target = totalSum - x; 6 | 7 | if(target == 0) 8 | return nums.size(); 9 | 10 | //find longest subArray with target sum 11 | int i = 0, j = 0, maxSize = 0, sum = 0; 12 | while(j < nums.size()){ 13 | sum += nums[j]; 14 | while(sum > target && i < j){ 15 | sum -= nums[i++]; 16 | } 17 | if(sum == target){ 18 | int subArrSize = (j - i) + 1; 19 | cout<& nums1, vector& nums2) { 4 | vector merged;\ 5 | double ans; 6 | int i, j; 7 | for(i = 0, j = 0; i < nums1.size() && j < nums2.size();){ 8 | if(nums1[i] < nums2[j]){ 9 | merged.push_back(nums1[i++]); 10 | } 11 | else{ 12 | merged.push_back(nums2[j++]); 13 | } 14 | } 15 | while(i < nums1.size()){ 16 | merged.push_back(nums1[i++]); 17 | } 18 | while(j < nums2.size()){ 19 | merged.push_back(nums2[j++]); 20 | } 21 | if(merged.size() % 2 == 0){ 22 | int mid = merged.size()/2; 23 | return (merged[mid] + merged[mid-1]) / 2.0; 24 | } 25 | else 26 | return merged[merged.size()/2]; 27 | } 28 | }; 29 | 30 | 31 | 32 | //constant space solution 33 | class Solution { 34 | public: 35 | double findMedianSortedArrays(vector& nums1, vector& nums2) { 36 | int n = nums1.size(), m = nums2.size(); 37 | int i = 0, j = 0, k = 0; 38 | double curr = 0, prev; 39 | 40 | while(i < n && j < m && k <= (m+n)/2){ 41 | if(nums1[i] < nums2[j]){ 42 | prev = curr; 43 | curr = nums1[i++]; 44 | } 45 | else{ 46 | prev = curr; 47 | curr = nums2[j++]; 48 | } 49 | k++; 50 | } 51 | 52 | while(i < n && k <= (m+n)/2){ 53 | prev = curr; 54 | curr = nums1[i++]; 55 | k++; 56 | } 57 | while(j < m && k <= (m+n)/2){ 58 | prev = curr; 59 | curr = nums2[j++]; 60 | k++; 61 | } 62 | 63 | if((m+n) % 2 == 0){ 64 | return (prev + curr) / 2.0; 65 | } 66 | else{ 67 | return curr; 68 | } 69 | } 70 | }; -------------------------------------------------------------------------------- /September/SEP_22_Is_Subsequence.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isSubsequence(string s, string t) { 4 | int i = 0, j = 0; 5 | while(j < t.size()){ 6 | if(s[i] == t[j]){ 7 | cout<> dp; 4 | bool isPredecessor(string &word1, string &word2){ 5 | int M = word1.size(); 6 | int N = word2.size(); 7 | 8 | if(N - M != 1) return false; 9 | 10 | int i = 0, j = 0; 11 | while(i < M && j < N){ 12 | if(word1[i] == word2[j]){ 13 | i++; 14 | } 15 | j++; 16 | } 17 | return i == M; 18 | } 19 | 20 | static bool cmp(string &word1, string &word2){ 21 | return word1.size() < word2.size(); 22 | } 23 | 24 | int lis(int prev, int curr, vector &words){ 25 | if(curr == n) return 0; 26 | if(prev != -1 && dp[prev][curr] != -1) 27 | return dp[prev][curr]; 28 | 29 | int take = 0; 30 | if(prev == -1 || isPredecessor(words[prev], words[curr])){ 31 | take = 1 + lis(curr, curr+1, words); 32 | } 33 | int notTake = lis(prev, curr+1, words); 34 | 35 | if(prev != -1){ 36 | dp[prev][curr] = max(take, notTake); 37 | } 38 | return max(take, notTake); 39 | } 40 | public: 41 | int longestStrChain(vector& words) { 42 | dp.resize(1001, vector(1001, -1)); 43 | n = words.size(); 44 | sort(words.begin(), words.end(), cmp); 45 | 46 | return lis(-1, 0, words); 47 | } 48 | }; -------------------------------------------------------------------------------- /September/SEP_24_Champagne_Tower.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | vector> dp; 3 | double solve(int poured, int i, int j){ 4 | if(i < 0 || j < 0 || i < j) return 0; 5 | if(i == 0) return poured; 6 | if(dp[i][j] != -1) return dp[i][j]; 7 | 8 | //-1 represent 1 cup full hone ke bad kitna mila 9 | //2.0 represent jo mila uko 2 hisso me bant liya 10 | double left = (solve(poured, i-1, j-1) - 1) / 2.0; 11 | double right = (solve(poured, i-1, j) - 1) / 2.0; 12 | 13 | if(left < 0) left = 0; 14 | if(right < 0) right = 0; 15 | 16 | return dp[i][j] = (left + right); 17 | } 18 | public: 19 | double champagneTower(int poured, int query_row, int query_glass) { 20 | dp.resize(query_row + 1, vector(query_glass + 1, -1)); 21 | //1 se jyada hua to floor pe gir jayega 22 | return min(1.0, solve(poured, query_row, query_glass)); 23 | } 24 | }; -------------------------------------------------------------------------------- /September/SEP_25_Find_the_Difference.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | char findTheDifference(string s, string t) { 4 | char x = s[0]; 5 | for(int i = 1; i stack; 5 | vector lastIndexOf(26, -1); 6 | for(int i = 0; i visited(26, false); 11 | for(int i = 0; i s[i] && lastIndexOf[stack.top()-'a'] > i){ 16 | visited[stack.top()-'a'] = false; 17 | stack.pop(); 18 | } 19 | stack.push(s[i]); 20 | visited[s[i] - 'a'] = true; 21 | } 22 | 23 | string ans; 24 | while(!stack.empty()){ 25 | ans.push_back(stack.top()); 26 | stack.pop(); 27 | } 28 | reverse(ans.begin(), ans.end()); 29 | return ans; 30 | } 31 | }; -------------------------------------------------------------------------------- /September/SEP_27_Decoded_String_at_Index.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string decodeAtIndex(string s, int k) { 4 | long long size = 0; 5 | for(auto c : s){ 6 | if(isdigit(c)){ 7 | size *= (c - '0'); 8 | } 9 | else{ 10 | size++; 11 | } 12 | } 13 | 14 | for(int i = s.size()-1; i>=0; i--){ 15 | k %= size; 16 | if(k == 0 && isalpha(s[i])) 17 | return string(1, s[i]); 18 | 19 | if(isalpha(s[i])) 20 | size--; 21 | else 22 | size /= (s[i] - '0'); 23 | } 24 | return ""; 25 | } 26 | }; -------------------------------------------------------------------------------- /September/SEP_28_Sort_Array_By_Parity.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector sortArrayByParity(vector& nums) { 4 | int j = 0; 5 | for(int i = 0; i< nums.size(); i++){ 6 | if(nums[i] % 2 == 0){ 7 | swap(nums[i], nums[j++]); 8 | } 9 | } 10 | return nums; 11 | } 12 | }; -------------------------------------------------------------------------------- /September/SEP_29_Monotonic_Array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isMonotonic(vector& nums) { 4 | stack stack; 5 | int n = nums.size(); 6 | 7 | //should be increasing 8 | if(nums[0] <= nums[n-1]){ 9 | for(int i = 0; i nums[i+1]) 11 | return false; 12 | } 13 | } 14 | //should be decreasing 15 | else{ 16 | for(int i = 0; i& nums) { 4 | int n = nums.size(); 5 | 6 | //time complexity N³ 7 | 8 | // for(int i = 0; i stack; 50 | 51 | for(int i = nums.size()-1 ; i>= 0; i--){ 52 | if(nums[i] < num3) 53 | return true; 54 | 55 | while(!stack.empty() && stack.top() < nums[i]){ 56 | num3 = stack.top(); 57 | stack.pop(); 58 | } 59 | stack.push(nums[i]); 60 | } 61 | return false; 62 | } 63 | }; -------------------------------------------------------------------------------- /november/NOV_1_Find_Mode_in_Binary_Search_Tree.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | class Solution { 13 | int mx = 0; 14 | int vector ans; 15 | 16 | void inorder(TreeNode *root){ 17 | if(!root) return; 18 | inorder(root->left); 19 | int curr = root->val; 20 | if(currFreq > maxFreq){ 21 | maxFreq = currFreq; 22 | ans.clear(); 23 | ans.push_back(curr); 24 | } 25 | else if(currFreq == maxFreq){ 26 | ans.push_back(curr); 27 | } 28 | inorder(root->right); 29 | } 30 | public: 31 | vector findMode(TreeNode* root) { 32 | inorder(root); 33 | } 34 | }; -------------------------------------------------------------------------------- /november/NOV_2_Count_Nodes_Equal_to_Average_of_Subtree.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | 13 | 14 | //just need a function that can return size of left and right subTree 15 | //and sum of left and right subTree 16 | // 17 | 18 | class Solution { 19 | int count = 0; 20 | //pair{sum, size} 21 | pair solve(auto root){ 22 | if(!root) return {0, 0}; 23 | 24 | pair left = solve(root->left); 25 | pair right = solve(root->right); 26 | 27 | int totalSum = left.first + right.first + root->val; 28 | int totalSize = left.second + right.second + 1; 29 | // printf("%d %d %d\n", root->val, totalSum, totalSize); 30 | if(totalSum / totalSize == root->val){ 31 | count++; 32 | } 33 | 34 | return {totalSum, totalSize}; 35 | } 36 | public: 37 | int averageOfSubtree(TreeNode* root) { 38 | auto p = solve(root); 39 | return count; 40 | } 41 | }; -------------------------------------------------------------------------------- /november/NOV_3_Build_an_Array_With_Stack_Operations.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector buildArray(vector& target, int n) { 4 | int j = 0; 5 | vector stack; 6 | // j 7 | // 1 2 3 8 | // i = 2 9 | for(int i = 1; i<=n && j < target.size(); i++){ 10 | int curr = target[j]; 11 | // cout< bob; 14 | } 15 | }; -------------------------------------------------------------------------------- /october/OCT_03_Number_of_Good_Pairs.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numIdenticalPairs(vector& nums) { 4 | //freq pairs 5 | //1. 1 0 6 | //2. 1,1 1 7 | //3. 1,1,1 2+1 8 | //4. 1,1,1,1 3+2+1 9 | //5. 1,1,1,1,1 4+3+2+1 10 | 11 | //(freq-1) * (freq-1+1)/2 12 | 13 | int sum = 0; 14 | unordered_map freq; 15 | for(auto num : nums){ 16 | freq[num]++; 17 | } 18 | for(auto p : freq){ 19 | sum += ((p.second-1) * (p.second))/2; 20 | } 21 | return sum; 22 | } 23 | }; -------------------------------------------------------------------------------- /october/OCT_04_Design_HashMap.cpp: -------------------------------------------------------------------------------- 1 | class MyHashMap { 2 | vector>> buckets; 3 | int size = 10000; 4 | public: 5 | MyHashMap() { 6 | buckets.resize(size); 7 | } 8 | 9 | void put(int key, int value) { 10 | int bucket_no = key % size; 11 | auto &chain = buckets[bucket_no]; 12 | 13 | for(auto &p : chain){ 14 | if(p.first == key){ 15 | p.second = value; 16 | return; 17 | } 18 | } 19 | chain.push_back({key, value}); 20 | } 21 | 22 | int get(int key) { 23 | int bucket_no = key % size; 24 | auto &chain = buckets[bucket_no]; 25 | 26 | for(auto &p : chain){ 27 | if(p.first == key){ 28 | return p.second; 29 | } 30 | } 31 | return -1; 32 | } 33 | 34 | void remove(int key) { 35 | int bucket_no = key % size; 36 | auto &chain = buckets[bucket_no]; 37 | 38 | for(auto i = chain.begin(); i != chain.end(); i++){ 39 | if(i->first == key){ 40 | chain.erase(i); 41 | return; 42 | } 43 | } 44 | } 45 | }; 46 | 47 | /** 48 | * Your MyHashMap object will be instantiated and called as such: 49 | * MyHashMap* obj = new MyHashMap(); 50 | * obj->put(key,value); 51 | * int param_2 = obj->get(key); 52 | * obj->remove(key); 53 | */ -------------------------------------------------------------------------------- /october/OCT_05_Majority_Element_II.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector majorityElement(vector& nums) { 4 | 5 | vector ans; 6 | int n = nums.size(); 7 | //complexity: 8 | //time : O(N) 9 | //space : O(N) 10 | 11 | // unordered_map freq; 12 | // for(auto num : nums){ 13 | // freq[num]++; 14 | // } 15 | 16 | // for(auto i : freq){ 17 | // if(i.second > nums.size()/3){ 18 | // ans.push_back(i.first); 19 | // } 20 | // } 21 | 22 | 23 | 24 | 25 | //complexity: 26 | //time : O(logN) 27 | //space : O(1) 28 | 29 | // sort(nums.begin(), nums.end()); 30 | // int count = 0; 31 | 32 | // for(int i = 0; i 0 && nums[i-1] != nums[i]){ 34 | // if(count > n/3) 35 | // ans.push_back(nums[i-1]); 36 | // count = 0; 37 | // } 38 | // count++; 39 | // } 40 | // if(count > n/3){ 41 | // ans.push_back(nums[n-1]); 42 | // } 43 | 44 | 45 | //there will be at most two majority element in the ans 46 | int element1 = INT_MIN, element2 = INT_MIN; 47 | int count1 = 0, count2 = 0; 48 | 49 | for(auto num : nums){ 50 | if(count1 == 0 && num != element2){ 51 | element1 = num; 52 | count1 = 1; 53 | } 54 | else if(count2 == 0 && num != element1){ 55 | element2 = num; 56 | count2 = 1; 57 | } 58 | else if(num == element1){ 59 | count1++; 60 | } 61 | else if(num == element2){ 62 | count2++; 63 | } 64 | else{ 65 | count1--; 66 | count2--; 67 | } 68 | } 69 | 70 | int majority = n/3; 71 | count1 = count2 = 0; 72 | 73 | for(auto num : nums){ 74 | if(element1 == num) 75 | count1++; 76 | } 77 | 78 | for(auto num : nums){ 79 | if(element2 == num) 80 | count2++; 81 | } 82 | 83 | if(count1 > majority) ans.push_back(element1); 84 | if(count2 > majority) ans.push_back(element2); 85 | 86 | return ans; 87 | } 88 | }; -------------------------------------------------------------------------------- /october/OCT_06_Integer_Break.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | vector dp; 3 | int solve(int n){ 4 | if(n == 1) return 1; 5 | if(dp[n] != -1) return dp[n]; 6 | 7 | int result = INT_MIN; 8 | for(int i = 1; i maximum){ 20 | result = (result + solve(currIndex + 1, i, cost+1)) % mod; 21 | } 22 | else{ 23 | result = (result + solve(currIndex + 1, maximum, cost)) % mod; 24 | } 25 | } 26 | return dp[currIndex][maximum][cost] = result; 27 | } 28 | public: 29 | int numOfArrays(int n, int m, int k) { 30 | this->N = n; 31 | this->M = m; 32 | this->K = k; 33 | memset(dp, -1, sizeof(dp)); 34 | return solve(0, 0, 0); 35 | } 36 | }; -------------------------------------------------------------------------------- /october/OCT_08_Max_Dot_Product_of_Two_Subsequences.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | int dp[501][501]; 3 | int solve(int i, int j, vector &nums1, vector &nums2){ 4 | if(i == nums1.size() || j == nums2.size()) return -1e9; 5 | if(dp[i][j] != -1) return dp[i][j]; 6 | 7 | int val = nums1[i]*nums2[j]; 8 | int taken_i_j = (nums1[i] * nums2[j]) + solve(i+1, j+1, nums1, nums2); 9 | int taken_i = solve(i+1, j, nums1, nums2); 10 | int taken_j = solve(i, j+1, nums1, nums2); 11 | 12 | return dp[i][j] = max({val, taken_i_j, taken_i, taken_j}); 13 | } 14 | public: 15 | int maxDotProduct(vector& nums1, vector& nums2) { 16 | memset(dp, -1, sizeof(dp)); 17 | return solve(0, 0, nums1, nums2); 18 | } 19 | }; -------------------------------------------------------------------------------- /october/OCT_09_Find_First_and_Last_Position_of_Element_in_Sorted_Array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | int lowerBound(vector &arr, int target){ 3 | int left = 0, right = arr.size()-1; 4 | int ans = arr.size(); 5 | while(left <= right){ 6 | int mid = (left + right)/2; 7 | 8 | if(arr[mid] >= target){ 9 | ans = mid; 10 | right = mid-1; 11 | } 12 | else if(arr[mid] < target){ 13 | left = mid+1; 14 | } 15 | } 16 | return ans; 17 | } 18 | int upperBound(vector &arr, int target){ 19 | int left = 0, right = arr.size()-1; 20 | int ans = arr.size(); 21 | 22 | while(left <= right){ 23 | int mid = (left + right)/2; 24 | if(arr[mid] > target){ 25 | ans = mid; 26 | right = mid-1; 27 | } 28 | else if(arr[mid] <= target){ 29 | left = mid+1; 30 | } 31 | } 32 | return ans; 33 | } 34 | 35 | public: 36 | vector searchRange(vector& nums, int target) { 37 | 38 | if(nums.empty()) return {-1, -1}; 39 | 40 | //Handling not found case 41 | int l = lowerBound(nums, target); 42 | if(l == nums.size() || nums[l] != target) return {-1, -1}; 43 | 44 | int u = upperBound(nums, target); 45 | return {l, u-1}; 46 | } 47 | }; -------------------------------------------------------------------------------- /october/OCT_10_Minimum_Number_of_Operations_to_Make_Array_Continuous.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minOperations(vector& nums) { 4 | int n = nums.size(); 5 | int ans = INT_MAX; 6 | 7 | // for(int i = 0; i set; 13 | // for(auto num : nums){ 14 | 15 | // if(num < mn || num > mx || set.find(num) != set.end()){ 16 | // operation++; 17 | // } 18 | // set.insert(num); 19 | // } 20 | // ans = min(ans, operation); 21 | // } 22 | 23 | 24 | 25 | //sort ans remove duplication 26 | set s(nums.begin(), nums.end()); 27 | vector arr(s.begin(), s.end()); 28 | 29 | for(int i = 0; i fullBloomFlowers(vector>& flowers, vector& people) { 4 | vector ans; 5 | 6 | //Time Complexity : people.size * flower 7 | // for(auto i : people){ 8 | // int count = 0; 9 | // for(auto flower : flowers){ 10 | // if(flower[0] <= i && i <= flower[1]) 11 | // count++; 12 | // } 13 | // ans.push_back(count); 14 | // } 15 | 16 | int n = flowers.size(); 17 | vector startTime(n), endTime(n); 18 | for(int i = 0; i target){ 51 | left = mid+1; 52 | } 53 | else{ 54 | right = mid-1; 55 | } 56 | } 57 | return -1; 58 | } 59 | 60 | public: 61 | int findInMountainArray(int target, MountainArray &mountainArr) { 62 | int peak = findPeak(mountainArr); 63 | int valueAt = binarySearch(mountainArr, 0, peak, target); 64 | if(valueAt != -1) 65 | return valueAt; 66 | 67 | return reverseBinarySearch(mountainArr, peak+1, mountainArr.length()-1, target); 68 | } 69 | }; -------------------------------------------------------------------------------- /october/OCT_13_Min_Cost_Climbing_Stairs.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | vector dp; 3 | int solve(vector &cost, int i){ 4 | if(i >= cost.size()) return 0; 5 | if(dp[i] != -1) return dp[i]; 6 | 7 | return dp[i] = cost[i] + min(solve(cost, i+1), solve(cost, i+2)); 8 | } 9 | public: 10 | int minCostClimbingStairs(vector& cost) { 11 | 12 | // ---------------Time O(N) space :O(N)--------------- 13 | // dp.resize(cost.size()+1, -1); 14 | // return min(solve(cost, 0), solve(cost, 1)); 15 | 16 | 17 | 18 | // ---------------Time O(N) space :O(1)--------------- 19 | if(cost.size() < 2) return 0; 20 | 21 | vector dp(3); 22 | dp[0] = cost[0]; 23 | dp[1] = cost[1]; 24 | 25 | for(int i = 2; i> dp; 3 | int solve(int i, int walls, vector &time, vector &cost){ 4 | if(walls <= 0) return 0;//no walls remains to paint 5 | if(i >= time.size()) return 1e9; 6 | 7 | 8 | if(dp[i][walls] != -1) return dp[i][walls]; 9 | 10 | //***"walls-1", one wall painted 11 | //****while taking time[i] to paint meanwhile 12 | // freePainter will paint time[i] no of walls 13 | int painted = cost[i] + solve(i+1, walls-1-time[i], time, cost); 14 | //skip 15 | int skip = solve(i+1, walls, time, cost); 16 | 17 | return dp[i][walls] = min(painted, skip); 18 | } 19 | public: 20 | int paintWalls(vector& cost, vector& time) { 21 | int n = cost.size(); 22 | dp.resize(n+1, vector(n+1, -1)); 23 | return solve(0, n, time, cost); 24 | } 25 | }; -------------------------------------------------------------------------------- /october/OCT_15_Number_of_Ways_to_Stay_in_the_Same_Place_After_Some_Steps.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | int n, mod = 1e9+7; 3 | int dp[501][501]; 4 | int solve(int i, int steps){ 5 | if(i < 0 || i >= n) return 0;//outbound handling 6 | 7 | if(steps == 0){ 8 | if(i == 0) return 1; 9 | return 0; 10 | } 11 | 12 | //memoization 13 | if(dp[i][steps] != -1) return dp[i][steps]; 14 | 15 | int ans = 0; 16 | ans = (ans + solve(i+1, steps-1)) % mod;//move left 17 | ans = (ans + solve(i-1, steps-1)) % mod;//move right 18 | ans = (ans + solve(i, steps-1)) % mod;//stay 19 | 20 | return dp[i][steps] = ans; 21 | } 22 | public: 23 | int numWays(int steps, int arrLen) { 24 | this->n = arrLen; 25 | memset(dp, -1, sizeof(dp)); 26 | return solve(0, steps); 27 | } 28 | }; -------------------------------------------------------------------------------- /october/OCT_16.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abhijeetSinghRajput/Daily-LeetCoding-Challenge/a7171f81ec8a64002b31a9244f94995b304ff671/october/OCT_16.cpp -------------------------------------------------------------------------------- /october/OCT_28_Count_Vowels_Permutation.cpp: -------------------------------------------------------------------------------- 1 | struct pair_hash { 2 | template 3 | std::size_t operator () (const std::pair& p) const { 4 | auto h1 = std::hash{}(p.first); 5 | auto h2 = std::hash{}(p.second); 6 | 7 | // Assuming sizeof(size_t) == 2 * sizeof(unsigned int) 8 | return (h1 << sizeof(std::size_t) / 2) ^ h2; 9 | } 10 | }; 11 | class Solution { 12 | int N, mod = 1e9+7; 13 | string vowels = "aeiou"; 14 | unordered_map, int, pair_hash> dp; 15 | 16 | long long solve(char endsWith, int n){ 17 | if(n == N) 18 | return 1; 19 | if(dp.find({endsWith, n}) != dp.end()) 20 | return dp[{endsWith, n}]; 21 | 22 | long long count = 0; 23 | 24 | if(endsWith == 'a'){ 25 | count = (count + solve('e', n+1))%mod; 26 | } 27 | else if(endsWith =='e'){ 28 | count = (count + solve('a', n+1))%mod; 29 | count = (count + solve('i', n+1))%mod; 30 | } 31 | else if(endsWith == 'i'){ 32 | for(auto vowel : vowels){ 33 | if(vowel != 'i') 34 | count = (count + solve(vowel, n+1))%mod; 35 | } 36 | } 37 | else if(endsWith == 'o'){ 38 | count = (count + solve('i', n+1))%mod; 39 | count = (count + solve('u', n+1))%mod; 40 | } 41 | else if(endsWith == 'u'){ 42 | count = (count + solve('a', n+1))%mod; 43 | } 44 | return dp[{endsWith,n}] = count; 45 | } 46 | 47 | public: 48 | 49 | int countVowelPermutation(int n) { 50 | this->N = n; 51 | long long count = 0; 52 | for(int i = 0; i