├── 2023 ├── 11 - November │ ├── 06-11-2023.cpp │ ├── 07-11-2023.cpp │ ├── 08-11-2023.cpp │ ├── 09-11-2023.cpp │ ├── 10-11-2023.cpp │ ├── 11-11-2023.cpp │ ├── 13-11-2023.cpp │ ├── 14-11-2023.cpp │ ├── 15-11-2023.cpp │ ├── 16-11-2023.cpp │ ├── 17-11-2023.cpp │ ├── 19-11-2023.cpp │ ├── 20-11-2023.cpp │ ├── 21-11-2023.cpp │ ├── 22-11-2023.cpp │ ├── 23-11-2023.cpp │ ├── 24-11-2023.cpp │ ├── 25-11-2023.cpp │ ├── 26-11-2023.cpp │ ├── 27-11-2023.cpp │ ├── 28-11-2023.cpp │ ├── 29-11-2023.cpp │ └── 30-11-2023.cpp └── 12 - December │ ├── 01-12-2023.cpp │ ├── 02-12-2023.cpp │ ├── 03-12-2023.cpp │ ├── 04-12-2023.cpp │ ├── 05-12-2023.cpp │ ├── 06-12-2023.cpp │ ├── 07-12-2023.cpp │ ├── 08-12-2023.cpp │ ├── 09-12-2023.cpp │ ├── 10-12-2023.cpp │ ├── 11-12-2023.cpp │ ├── 12-12-2023.cpp │ ├── 13-12-2023.cpp │ ├── 14-12-2023.cpp │ ├── 15-12-2023.cpp │ ├── 16-12-2023.cpp │ ├── 17-12-2023.cpp │ ├── 18-12-2023.cpp │ ├── 19-12-2023.cpp │ ├── 20-12-2023.cpp │ ├── 21-12-2023.cpp │ ├── 22-12-2023.cpp │ ├── 23-12-2023.cpp │ ├── 24-12-2023.cpp │ ├── 25-12-2023.cpp │ ├── 26-12-2023.cpp │ ├── 27-12-2023.cpp │ ├── 28-12-2023.cpp │ ├── 29-12-2023.cpp │ ├── 30-12-2023.cpp │ └── 31-12-2023.cpp ├── 2024 ├── 01 - January │ ├── 01-01-2024.cpp │ ├── 02-01-2024.cpp │ ├── 03-01-2024.cpp │ ├── 04-01-2024.cpp │ ├── 05-01-2024.cpp │ ├── 06-01-2024.cpp │ ├── 07-01-2024.cpp │ ├── 08-01-2024.cpp │ ├── 09-01-2024.cpp │ ├── 10-01-2024.cpp │ ├── 11-01-2024.cpp │ ├── 12-01-2024.cpp │ ├── 13-01-2024.cpp │ ├── 14-01-2024.cpp │ ├── 15-01-2024.cpp │ ├── 16-01-2024.cpp │ ├── 17-01-2024.cpp │ ├── 18-01-2024.cpp │ ├── 19-01-2024.cpp │ ├── 20-01-2024.cpp │ ├── 21-01-2024.cpp │ ├── 22-01-2024.cpp │ ├── 23-01-2024.cpp │ ├── 24-01-2024.cpp │ ├── 25-01-2024.cpp │ ├── 26-01-2024.cpp │ ├── 27-01-2024.cpp │ ├── 28-01-2024.cpp │ ├── 29-01-2024.cpp │ ├── 30-01-2024.cpp │ └── 31-01-2024.cpp ├── 02 - February │ ├── 01-02-2024.cpp │ ├── 02-02-2024.cpp │ ├── 03-02-2024.cpp │ ├── 04-02-2024.cpp │ ├── 05-02-2024.cpp │ ├── 06-02-2024.cpp │ ├── 07-02-2024.cpp │ ├── 08-02-2024.cpp │ ├── 09-02-2024.cpp │ ├── 10-02-2024.cpp │ ├── 11-02-2024.cpp │ ├── 12-02-2024.cpp │ ├── 13-02-2024.cpp │ ├── 14-02-2024.cpp │ ├── 15-02-2024.cpp │ ├── 16-02-2024.cpp │ ├── 17-02-2024.cpp │ ├── 18-02-2024.cpp │ ├── 19-02-2024.cpp │ ├── 20-02-2024.cpp │ ├── 21-02-2024.cpp │ ├── 22-02-2024.cpp │ ├── 23-02-2024.cpp │ ├── 24-02-2024.cpp │ ├── 25-02-2024.cpp │ ├── 26-02-2024.cpp │ ├── 27-02-2024.cpp │ ├── 28-02-2024.cpp │ └── 29-02-2024.cpp └── 03 - March │ ├── 01-03-2024.cpp │ ├── 02-03-2024.cpp │ ├── 03-03-2024.cpp │ ├── 04-03-2024.cpp │ ├── 05-03-2024.cpp │ ├── 06-03-2024.cpp │ ├── 07-03-2024.cpp │ ├── 08-03-2024.cpp │ ├── 09-03-2024.cpp │ ├── 10-03-2024.cpp │ ├── 11-03-2024.cpp │ ├── 12-03-2024.cpp │ ├── 13-03-2024.cpp │ ├── 14-03-2024.cpp │ ├── 15-03-2024.cpp │ ├── 16-03-2024.cpp │ ├── 17-03-2024.cpp │ ├── 18-03-2024.cpp │ ├── 19-03-2024.cpp │ ├── 20-03-2024.cpp │ ├── 21-03-2024.cpp │ ├── 22-03-2024.cpp │ ├── 23-03-2024.cpp │ ├── 24-03-2024.cpp │ ├── 25-03-2024.cpp │ ├── 26-03-2024.cpp │ ├── 27-03-2024.cpp │ ├── 28-03-2024.cpp │ ├── 29-03-2024.cpp │ ├── 30-03-2024.cpp │ └── 31-03-2024.cpp └── README.md /2023/11 - November/06-11-2023.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 06/11/2023 5 | Problem : Seat Reservation Manager 6 | Difficulty : Medium 7 | Problem Link : https://leetcode.com/problems/seat-reservation-manager/ 8 | Video Solution : NA 9 | 10 | */ 11 | 12 | 13 | class SeatManager { 14 | public: 15 | set seats; 16 | SeatManager(int n) { 17 | for(int i = 1; i < n + 1; i++){ 18 | seats.insert(i); 19 | } 20 | } 21 | 22 | int reserve() { 23 | int take = *seats.begin(); 24 | seats.erase(seats.begin()); 25 | 26 | return take; 27 | } 28 | 29 | void unreserve(int seatNumber) { 30 | seats.insert(seatNumber); 31 | } 32 | }; -------------------------------------------------------------------------------- /2023/11 - November/07-11-2023.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 07/11/2023 5 | Problem : Eliminate Maximum Number of Monsters 6 | Difficulty : Medium 7 | Problem Link : https://leetcode.com/problems/eliminate-maximum-number-of-monsters/ 8 | Video Solution : https://youtu.be/9btWEMlzMoE 9 | 10 | */ 11 | 12 | class Solution { 13 | public: 14 | int eliminateMaximum(vector& dist, vector& speed) { 15 | int n = dist.size(); 16 | 17 | vector time(n); 18 | 19 | for(int i = 0; i < n; i++){ 20 | time[i] = (dist[i] + speed[i] - 1) / speed[i]; 21 | } 22 | 23 | sort(time.begin(), time.end()); 24 | 25 | int cur = 0; 26 | int ans = 0; 27 | 28 | for(auto i : time){ 29 | if(cur < i){ 30 | ++ans; 31 | ++cur; 32 | } 33 | else{ 34 | break; 35 | } 36 | } 37 | 38 | return ans; 39 | } 40 | }; -------------------------------------------------------------------------------- /2023/11 - November/08-11-2023.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 08/11/2023 5 | Problem : Determine if a Cell Is Reachable at a Given Time 6 | Difficulty : Medium 7 | Problem Link : https://leetcode.com/problems/determine-if-a-cell-is-reachable-at-a-given-time/ 8 | Video Solution : https://youtu.be/PWJuLPhyyqM 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | bool isReachableAtTime(int sx, int sy, int fx, int fy, int t) { 16 | int dx = abs(sx - fx); 17 | int dy = abs(sy - fy); 18 | 19 | int MIN = max(dx, dy); 20 | 21 | if(t < MIN) 22 | return 0; 23 | 24 | if(!dx and !dy and t == 1) 25 | return 0; 26 | 27 | return 1; 28 | } 29 | }; -------------------------------------------------------------------------------- /2023/11 - November/09-11-2023.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 09/11/2023 5 | Problem : Count Number of Homogenous Substrings 6 | Difficulty : Medium 7 | Problem Link : https://leetcode.com/problems/count-number-of-homogenous-substrings/ 8 | Video Solution : https://youtu.be/DHEhpRU3sC0 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | int countHomogenous(string s) { 16 | const long long mod = 1e9 + 7; 17 | 18 | long long ans, cur; 19 | ans = 0; 20 | cur = 1; 21 | 22 | int n = s.size(); 23 | 24 | for(int i = 1; i < n; i++){ 25 | if(s[i] != s[i - 1]){ 26 | long long newval = (cur * (cur + 1)) / 2; 27 | newval = newval % mod; 28 | 29 | ans = (ans + newval) % mod; 30 | 31 | cur = 1; 32 | } 33 | else{ 34 | ++cur; 35 | } 36 | } 37 | 38 | long long newval = (cur * (cur + 1)) / 2; 39 | newval = newval % mod; 40 | 41 | ans = (ans + newval) % mod; 42 | 43 | return ans; 44 | } 45 | }; -------------------------------------------------------------------------------- /2023/11 - November/10-11-2023.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 10/11/2023 5 | Problem : Restore the Array From Adjacent Pairs 6 | Difficulty : Medium 7 | Problem Link : https://leetcode.com/problems/restore-the-array-from-adjacent-pairs/ 8 | Video Solution : https://youtu.be/l5Ds8Vqwcls 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | vector restoreArray(vector>& adjacentPairs) { 16 | map> g; 17 | map in; 18 | 19 | for(auto i : adjacentPairs){ 20 | g[i[0]].insert(i[1]); 21 | g[i[1]].insert(i[0]); 22 | ++in[i[0]]; 23 | ++in[i[1]]; 24 | } 25 | 26 | int start = -1; 27 | 28 | for(auto i : in){ 29 | if(i.second == 1){ 30 | start = i.first; 31 | break; 32 | } 33 | } 34 | 35 | vector ans; 36 | queue q; 37 | q.push(start); 38 | 39 | while(!q.empty()){ 40 | int node = q.front(); 41 | q.pop(); 42 | 43 | ans.push_back(node); 44 | 45 | for(auto child : g[node]){ 46 | q.push(child); 47 | g[child].erase(node); 48 | } 49 | } 50 | 51 | return ans; 52 | } 53 | }; -------------------------------------------------------------------------------- /2023/11 - November/11-11-2023.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 11/11/2023 5 | Problem : Design Graph With Shortest Path Calculator 6 | Difficulty : Hard 7 | Problem Link : https://leetcode.com/problems/design-graph-with-shortest-path-calculator/ 8 | Video Solution : https://youtu.be/gi7nyQUaa1s 9 | 10 | */ 11 | 12 | 13 | vector>> g(101); 14 | 15 | class Graph { 16 | public: 17 | Graph(int n, vector>& edges) { 18 | for(int i = 0; i < n; i++) 19 | g[i].clear(); 20 | 21 | for(auto i : edges){ 22 | int x = i[0]; 23 | int y = i[1]; 24 | int w = i[2]; 25 | 26 | g[x].emplace_back(y, w); 27 | } 28 | } 29 | 30 | void addEdge(vector edge) { 31 | int x = edge[0]; 32 | int y = edge[1]; 33 | int w = edge[2]; 34 | 35 | g[x].emplace_back(y, w); 36 | } 37 | 38 | int shortestPath(int node1, int node2) { 39 | vector d(101, 1e9); 40 | priority_queue, vector>, greater>> pq; 41 | 42 | pq.push({0, node1}); 43 | d[node1] = 0; 44 | 45 | while(!pq.empty()){ 46 | int cd, node; 47 | tie(cd, node) = pq.top(); 48 | pq.pop(); 49 | 50 | for(auto next : g[node]){ 51 | int w, child; 52 | tie(child, w) = next; 53 | 54 | if(cd + w < d[child]){ 55 | d[child] = cd + w; 56 | pq.push({d[child], child}); 57 | } 58 | } 59 | } 60 | 61 | return d[node2] == 1e9 ? -1 : d[node2]; 62 | } 63 | }; 64 | 65 | /** 66 | * Your Graph object will be instantiated and called as such: 67 | * Graph* obj = new Graph(n, edges); 68 | * obj->addEdge(edge); 69 | * int param_2 = obj->shortestPath(node1,node2); 70 | */ -------------------------------------------------------------------------------- /2023/11 - November/13-11-2023.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 13/11/2023 5 | Problem : Sort Vowels in a String 6 | Difficulty : Medium 7 | Problem Link : https://leetcode.com/problems/sort-vowels-in-a-string/ 8 | Video Solution : https://youtu.be/-eTSYwxO2p0 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | string sortVowels(string s) { 16 | vector vowels = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'}; 17 | sort(vowels.begin(), vowels.end()); 18 | 19 | int n = s.size(); 20 | vector take; 21 | 22 | for(int i = 0; i < s.size(); i++){ 23 | if(binary_search(vowels.begin(), vowels.end(), s[i])) 24 | take.push_back(s[i]); 25 | } 26 | 27 | sort(take.begin(), take.end(), [](char a, char b){ 28 | return (int)a > (int)b; 29 | }); 30 | 31 | for(int i = 0; i < s.size(); i++){ 32 | if(binary_search(vowels.begin(), vowels.end(), s[i])){ 33 | s[i] = take.back(); 34 | take.pop_back(); 35 | } 36 | } 37 | 38 | return s; 39 | } 40 | }; -------------------------------------------------------------------------------- /2023/11 - November/14-11-2023.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 14/11/2023 5 | Problem : Unique Length-3 Palindromic Subsequences 6 | Difficulty : Medium 7 | Problem Link : https://leetcode.com/problems/unique-length-3-palindromic-subsequences/ 8 | Video Solution : https://youtu.be/jbxyHCJJEfk 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | int countPalindromicSubsequence(string s) { 16 | int n = s.size(); 17 | vector first(26, -1), last(26, -1); 18 | vector> pre(n, vector (26, 0)); 19 | 20 | for(int i = 0; i < n; i++){ 21 | last[s[i] - 'a'] = i; 22 | first[s[n - i - 1] - 'a'] = n - i - 1; 23 | 24 | if(i > 0){ 25 | for(int j = 0; j < 26; j++) 26 | pre[i][j] = pre[i - 1][j]; 27 | } 28 | 29 | pre[i][s[i] - 'a']++; 30 | } 31 | 32 | int ans = 0; 33 | 34 | for(int i = 0; i < 26; i++){ 35 | if(first[i] == last[i]) 36 | continue; 37 | 38 | for(int j = 0; j < 26; j++) 39 | ans += (pre[last[i] - 1][j] - pre[first[i]][j]) > 0; 40 | } 41 | 42 | return ans; 43 | } 44 | }; -------------------------------------------------------------------------------- /2023/11 - November/15-11-2023.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 15/11/2023 5 | Problem : Maximum Element After Decreasing and Rearranging 6 | Difficulty : Medium 7 | Problem Link : https://leetcode.com/problems/maximum-element-after-decreasing-and-rearranging/ 8 | Video Solution : https://youtu.be/v1fQIKkW_tE 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | int maximumElementAfterDecrementingAndRearranging(vector& arr) { 16 | sort(arr.begin(), arr.end()); 17 | arr[0] = 1; 18 | 19 | for(int i = 1; i < arr.size(); i++){ 20 | arr[i] = min(arr[i - 1] + 1, arr[i]); 21 | } 22 | 23 | return arr.back(); 24 | } 25 | }; -------------------------------------------------------------------------------- /2023/11 - November/16-11-2023.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 16/11/2023 5 | Problem : Find Unique Binary String 6 | Difficulty : Medium 7 | Problem Link : https://leetcode.com/problems/find-unique-binary-string/ 8 | Video Solution : https://youtu.be/YEAJRiDYYkQ 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | string findDifferentBinaryString(vector& nums) { 16 | vector values; 17 | 18 | for(auto i : nums){ 19 | string cur = i; 20 | reverse(cur.begin(), cur.end()); 21 | 22 | int val = 0; 23 | for(int j = 0; j < cur.size(); j++) 24 | if(cur[j] == '1') 25 | val += (1 << j); 26 | 27 | values.push_back(val); 28 | } 29 | 30 | sort(values.begin(), values.end()); 31 | 32 | int ans = 0; 33 | 34 | for(auto i : values){ 35 | if(i == ans) 36 | ++ans; 37 | else 38 | break; 39 | } 40 | 41 | string res = ""; 42 | while(ans){ 43 | if(ans & 1) 44 | res += '1'; 45 | else 46 | res += '0'; 47 | 48 | ans >>= 1; 49 | } 50 | 51 | while(res.size() < nums[0].size()) 52 | res += '0'; 53 | 54 | reverse(res.begin(), res.end()); 55 | 56 | return res; 57 | } 58 | }; -------------------------------------------------------------------------------- /2023/11 - November/17-11-2023.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 17/11/2023 5 | Problem : Minimize Maximum Pair Sum in Array 6 | Difficulty : Medium 7 | Problem Link : https://leetcode.com/problems/minimize-maximum-pair-sum-in-array/ 8 | Video Solution : https://youtu.be/0H5B0VoarzI 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | int minPairSum(vector& nums) { 16 | sort(nums.begin(), nums.end()); 17 | int n = nums.size(); 18 | 19 | int ans = 0; 20 | for(int i = 0; i < n / 2; i++){ 21 | ans = max(ans, nums[i] + nums[n - i - 1]); 22 | } 23 | 24 | return ans; 25 | } 26 | }; -------------------------------------------------------------------------------- /2023/11 - November/19-11-2023.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 19/11/2023 5 | Problem : Reduction Operations to Make the Array Elements Equal 6 | Difficulty : Medium 7 | Problem Link : https://leetcode.com/problems/reduction-operations-to-make-the-array-elements-equal/ 8 | Video Solution : https://youtu.be/XDVIRjRVKog 9 | 10 | */ 11 | 12 | class Solution { 13 | public: 14 | int reductionOperations(vector& nums) { 15 | sort(nums.begin(), nums.end(), [](int a, int b){ 16 | return a > b; 17 | }); 18 | 19 | int ans = 0; 20 | 21 | for(int i = 1; i < nums.size(); i++){ 22 | if(nums[i] != nums[i - 1]) 23 | ans += i; 24 | } 25 | 26 | return ans; 27 | } 28 | }; -------------------------------------------------------------------------------- /2023/11 - November/20-11-2023.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 20/11/2023 5 | Problem : Minimum Amount of Time to Collect Garbage 6 | Difficulty : Medium 7 | Problem Link : https://leetcode.com/problems/minimum-amount-of-time-to-collect-garbage/ 8 | Video Solution : https://youtu.be/AdIUsCZKIQ0 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | int garbageCollection(vector& garbage, vector& travel) { 16 | int n = garbage.size(); 17 | 18 | int m, p, g; 19 | m = p = g = 0; 20 | 21 | int lastm, lastp, lastg; 22 | lastm = lastp = lastg = 0; 23 | 24 | for(int i = 1; i < n - 1; i++) 25 | travel[i] += travel[i - 1]; 26 | 27 | for(int i = 0; i < n; i++){ 28 | int dm, dp, dg; 29 | dm = dp = dg = 0; 30 | 31 | for(auto j : garbage[i]){ 32 | dm += j == 'M'; 33 | dp += j == 'P'; 34 | dg += j == 'G'; 35 | } 36 | 37 | m += dm; 38 | p += dp; 39 | g += dg; 40 | 41 | lastm = (dm > 0) ? i : lastm; 42 | lastp = (dp > 0) ? i : lastp; 43 | lastg = (dg > 0) ? i : lastg; 44 | } 45 | 46 | int ans = m + p + g; 47 | 48 | if(lastm) 49 | ans += travel[lastm - 1]; 50 | if(lastp) 51 | ans += travel[lastp - 1]; 52 | if(lastg) 53 | ans += travel[lastg - 1]; 54 | 55 | return ans; 56 | } 57 | }; -------------------------------------------------------------------------------- /2023/11 - November/21-11-2023.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 21/11/2023 5 | Problem : Count Nice Pairs in an Array 6 | Difficulty : Medium 7 | Problem Link : https://leetcode.com/problems/count-nice-pairs-in-an-array/ 8 | Video Solution : https://youtu.be/P7ddJJxsbdI 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | int countNicePairs(vector& nums) { 16 | map f; 17 | long long ans = 0; 18 | const long long mod = 1e9 + 7; 19 | 20 | for(auto i : nums){ 21 | string rev = to_string(i); 22 | reverse(rev.begin(), rev.end()); 23 | int j = stoi(rev); 24 | 25 | ans = (ans + f[i - j]) % mod; 26 | ++f[i - j]; 27 | } 28 | 29 | return ans; 30 | } 31 | }; -------------------------------------------------------------------------------- /2023/11 - November/22-11-2023.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 22/11/2023 5 | Problem : Diagonal Traverse II 6 | Difficulty : Medium 7 | Problem Link : https://leetcode.com/problems/diagonal-traverse-ii/ 8 | Video Solution : https://youtu.be/DvsqLBgkbgU 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | vector findDiagonalOrder(vector>& nums) { 16 | int n = nums.size(); 17 | // pos vector tells me the elements at the ith diagonal 18 | vector> pos(1e5 + 10); 19 | vector ans; 20 | 21 | for(int i = 0; i < n; i++){ 22 | for(int j = 0; j < nums[i].size(); j++){ 23 | pos[i + j].push_back(nums[i][j]); 24 | } 25 | } 26 | 27 | for(int i = 0; i < 1e5 + 10; i++){ 28 | while(pos[i].size()){ 29 | ans.push_back(pos[i].back()); 30 | pos[i].pop_back(); 31 | } 32 | } 33 | 34 | return ans; 35 | } 36 | }; -------------------------------------------------------------------------------- /2023/11 - November/23-11-2023.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 23/11/2023 5 | Problem : Arithmetic Subarrays 6 | Difficulty : Medium 7 | Problem Link : https://leetcode.com/problems/arithmetic-subarrays/ 8 | Video Solution : https://youtu.be/ky4fJPAupP4 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | vector checkArithmeticSubarrays(vector& nums, vector& l, vector& r) { 16 | vector ans(l.size(), 0); 17 | 18 | for(int i = 0; i < l.size(); i++){ 19 | if(r[i] - l[i] + 1 > 1){ 20 | ans[i] = 1; 21 | vector copy = nums; 22 | sort(copy.begin() + l[i], copy.begin() + r[i] + 1); 23 | 24 | int dx = copy[l[i] + 1] - copy[l[i]]; 25 | 26 | for(int j = l[i] + 2; j < r[i] + 1; j++){ 27 | if(copy[j] - copy[j - 1] != dx){ 28 | ans[i] = 0; 29 | break; 30 | } 31 | } 32 | } 33 | } 34 | 35 | return ans; 36 | } 37 | }; -------------------------------------------------------------------------------- /2023/11 - November/24-11-2023.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 24/11/2023 5 | Problem : Maximum Number of Coins You Can Get 6 | Difficulty : Medium 7 | Problem Link : https://leetcode.com/problems/maximum-number-of-coins-you-can-get/ 8 | Video Solution : https://youtu.be/AAhEjr6OVho 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | int maxCoins(vector& piles) { 16 | sort(piles.begin(), piles.end(), [](int a, int b){ 17 | return a > b; 18 | }); 19 | 20 | int ans = 0; 21 | 22 | for(int i = 1; i < piles.size(); i += 2){ 23 | ans += piles[i]; 24 | piles.pop_back(); 25 | } 26 | 27 | return ans; 28 | } 29 | }; -------------------------------------------------------------------------------- /2023/11 - November/25-11-2023.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 25/11/2023 5 | Problem : Sum of Absolute Differences in a Sorted Array 6 | Difficulty : Medium 7 | Problem Link : https://leetcode.com/problems/sum-of-absolute-differences-in-a-sorted-array/ 8 | Video Solution : https://youtu.be/dnbR2VnrSp8 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | vector getSumAbsoluteDifferences(vector& nums) { 16 | int n = nums.size(); 17 | 18 | vector prefix(n, 0), suffix(n, 0); 19 | prefix[0] = nums[0]; 20 | suffix[n - 1] = nums[n - 1]; 21 | 22 | for(int i = 1; i < n; i++){ 23 | prefix[i] = nums[i] + prefix[i - 1]; 24 | suffix[n - i - 1] = nums[n - i - 1] + suffix[n - i]; 25 | } 26 | 27 | vector ans(n); 28 | for(int i = 0; i < n; i++){ 29 | int left = i * nums[i] - (i ? prefix[i - 1] : 0); 30 | int right = ((i < n - 1) ? suffix[i + 1] : 0) - nums[i] * (n - i - 1); 31 | 32 | ans[i] = left + right; 33 | } 34 | 35 | return ans; 36 | } 37 | }; -------------------------------------------------------------------------------- /2023/11 - November/26-11-2023.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 26/11/2023 5 | Problem : Largest Submatrix With Rearrangements 6 | Difficulty : Medium 7 | Problem Link : https://leetcode.com/problems/largest-submatrix-with-rearrangements/ 8 | Video Solution : https://youtu.be/ipb6pj9kkcA 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | int largestSubmatrix(vector>& matrix) { 16 | int ans = 0; 17 | int n = matrix.size(); 18 | int m = matrix[0].size(); 19 | 20 | int dp[n][m]; 21 | for(int i = 0; i < m; i++) 22 | dp[0][i] = matrix[0][i]; 23 | 24 | for(int i = 1; i < n; i++){ 25 | for(int j = 0; j < m; j++){ 26 | if(matrix[i][j]){ 27 | dp[i][j] = dp[i - 1][j] + 1; 28 | } 29 | else{ 30 | dp[i][j] = 0; 31 | } 32 | } 33 | } 34 | 35 | for(int i = 0; i < n; i++){ 36 | vector values(m); 37 | for(int j = 0; j < m; j++) 38 | values[j] = dp[i][j]; 39 | 40 | sort(values.begin(), values.end(), [](int a, int b){ 41 | return a > b; 42 | }); 43 | 44 | for(int i = 0; i < m; i++){ 45 | ans = max(ans, values[i] * (i + 1)); 46 | } 47 | } 48 | 49 | return ans; 50 | } 51 | }; -------------------------------------------------------------------------------- /2023/11 - November/27-11-2023.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 27/11/2023 5 | Problem : Knight Dialer 6 | Difficulty : Medium 7 | Problem Link : https://leetcode.com/problems/knight-dialer/ 8 | Video Solution : https://youtu.be/1nUmbFuRHmI 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | int knightDialer(int n) { 16 | vector> dp(n, vector (10, 0)); 17 | 18 | for(int i = 0; i < 10; i++) 19 | dp[n - 1][i] = 1; 20 | 21 | const long long mod = 1e9 + 7; 22 | 23 | vector> go = {{4, 6}, {6, 8}, {7, 9}, {4, 8}, {0, 3, 9}, {}, {0, 1, 7}, {2, 6}, {1, 3}, {2, 4}}; 24 | 25 | for(int i = n - 2; i > -1; i--){ 26 | for(int j = 0; j < 10; j++){ 27 | for(auto next : go[j]){ 28 | dp[i][j] = (dp[i][j] + dp[i + 1][next]) % mod; 29 | } 30 | } 31 | } 32 | 33 | long long ans = 0; 34 | 35 | for(int i = 0; i < 10; i++){ 36 | ans = (ans + dp[0][i]) % mod; 37 | } 38 | 39 | return ans; 40 | } 41 | }; -------------------------------------------------------------------------------- /2023/11 - November/28-11-2023.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 28/11/2023 5 | Problem : Number of Ways to Divide a Long Corridor 6 | Difficulty : Hard 7 | Problem Link : https://leetcode.com/problems/number-of-ways-to-divide-a-long-corridor/ 8 | Video Solution : https://youtu.be/pLlwTvzZOK0 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | int numberOfWays(string corridor) { 16 | vector points; 17 | const long long mod = 1e9 + 7; 18 | 19 | int n = corridor.size(); 20 | int total = 0; 21 | int index = 0; 22 | 23 | while(index < n){ 24 | if(corridor[index] == 'S') 25 | ++total; 26 | 27 | if(!(total & 1) and total){ 28 | ++index; 29 | int count = 1; 30 | while(index < n and corridor[index] != 'S'){ 31 | ++count; 32 | ++index; 33 | } 34 | 35 | if(index < n) 36 | points.push_back(count); 37 | } 38 | else{ 39 | ++index; 40 | } 41 | } 42 | 43 | if((total & 1) or (total == 0)) 44 | return 0; 45 | else{ 46 | long long ans = 1; 47 | 48 | for(auto i : points) 49 | ans = (ans * (long long)i) % mod; 50 | 51 | return ans; 52 | } 53 | 54 | return 0; 55 | } 56 | }; -------------------------------------------------------------------------------- /2023/11 - November/29-11-2023.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 29/11/2023 5 | Problem : Number of 1 Bits 6 | Difficulty : Easy 7 | Problem Link : https://leetcode.com/problems/number-of-1-bits/ 8 | Video Solution : NA 9 | 10 | */ 11 | 12 | class Solution { 13 | public: 14 | int hammingWeight(uint32_t n) { 15 | return __builtin_popcountll(n); 16 | } 17 | }; -------------------------------------------------------------------------------- /2023/11 - November/30-11-2023.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 30/11/2023 5 | Problem : Minimum One Bit Operations to Make Integers Zero 6 | Difficulty : Hard 7 | Problem Link : https://leetcode.com/problems/minimum-one-bit-operations-to-make-integers-zero/ 8 | Video Solution : https://youtu.be/9o1f7AvzXLA 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | int minimumOneBitOperations(int n) { 16 | int ans = 0; 17 | bool one = 0; 18 | 19 | for(int i = 31; i > -1; i--){ 20 | if((n >> i) & 1){ 21 | if(!one){ 22 | ans += 1 << i; 23 | one = 1; 24 | } 25 | else{ 26 | one = 0; 27 | } 28 | } 29 | else{ 30 | if(one){ 31 | ans += 1 << i; 32 | } 33 | } 34 | } 35 | 36 | return ans; 37 | } 38 | }; -------------------------------------------------------------------------------- /2023/12 - December/01-12-2023.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 01/12/2023 5 | Problem : Check If Two String Arrays are Equivalent 6 | Difficulty : Easy 7 | Problem Link : https://leetcode.com/problems/check-if-two-string-arrays-are-equivalent/ 8 | Video Solution : https://youtu.be/FR1m1oNwpPA 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | bool arrayStringsAreEqual(vector& word1, vector& word2) { 16 | string a, b; 17 | a = ""; 18 | b = ""; 19 | 20 | for(auto i : word1) 21 | a += i; 22 | 23 | for(auto i : word2) 24 | b += i; 25 | 26 | return a == b; 27 | } 28 | }; -------------------------------------------------------------------------------- /2023/12 - December/02-12-2023.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 02/12/2023 5 | Problem : Find Words That Can Be Formed by Characters 6 | Difficulty : Easy 7 | Problem Link : https://leetcode.com/problems/find-words-that-can-be-formed-by-characters/ 8 | Video Solution : https://youtu.be/2OCo6MOMb74 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | int countCharacters(vector& words, string chars) { 16 | vector f(26, 0); 17 | 18 | for(auto i : chars) 19 | ++f[i - 'a']; 20 | 21 | int ans = 0; 22 | 23 | for(auto i : words){ 24 | bool ok = 1; 25 | vector left = f; 26 | 27 | for(auto j : i){ 28 | if(!left[j - 'a']){ 29 | ok = 0; 30 | break; 31 | } 32 | 33 | --left[j - 'a']; 34 | } 35 | 36 | ans += ok ? i.size() : 0; 37 | } 38 | 39 | return ans; 40 | } 41 | }; -------------------------------------------------------------------------------- /2023/12 - December/03-12-2023.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 03/12/2023 5 | Problem : Minimum Time Visiting All Points 6 | Difficulty : Easy 7 | Problem Link : https://leetcode.com/problems/minimum-time-visiting-all-points/ 8 | Video Solution : https://youtu.be/pIcsoQ5MoRQ 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | int minTimeToVisitAllPoints(vector>& points) { 16 | int ans = 0; 17 | 18 | for(int i = 1; i < points.size(); i++){ 19 | int dx = abs(points[i][0] - points[i - 1][0]); 20 | int dy = abs(points[i][1] - points[i - 1][1]); 21 | 22 | ans += max(dx, dy); 23 | } 24 | 25 | return ans; 26 | } 27 | }; -------------------------------------------------------------------------------- /2023/12 - December/04-12-2023.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 04/12/2023 5 | Problem : Largest 3-Same-Digit Number in String 6 | Difficulty : Easy 7 | Problem Link : https://leetcode.com/problems/largest-3-same-digit-number-in-string/ 8 | Video Solution : https://youtu.be/bdVX9ovqQvs 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | string largestGoodInteger(string num) { 16 | string ans = ""; 17 | 18 | for(int i = 0; i < num.size() - 2; i++){ 19 | if(num[i] == num[i + 1] and num[i + 1] == num[i + 2]) 20 | ans = max(ans, num.substr(i, 3)); 21 | } 22 | 23 | return ans; 24 | } 25 | }; -------------------------------------------------------------------------------- /2023/12 - December/05-12-2023.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 05/12/2023 5 | Problem : Count of Matches in Tournament 6 | Difficulty : Easy 7 | Problem Link : https://leetcode.com/problems/count-of-matches-in-tournament/ 8 | Video Solution : https://youtu.be/JiW3FskET9o 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | int numberOfMatches(int n) { 16 | int ans = 0; 17 | 18 | while(n > 1){ 19 | ans += n / 2; 20 | n = (n / 2) + (n & 1); 21 | } 22 | 23 | return ans; 24 | } 25 | }; -------------------------------------------------------------------------------- /2023/12 - December/06-12-2023.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 06/12/2023 5 | Problem : Calculate Money in Leetcode Bank 6 | Difficulty : Easy 7 | Problem Link : https://leetcode.com/problems/calculate-money-in-leetcode-bank/ 8 | Video Solution : https://youtu.be/p-jpSVIIHo4 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | int totalMoney(int n) { 16 | int day = 1; 17 | int week = 1; 18 | int ans = 0; 19 | 20 | while(day < n + 1){ 21 | int i = 0; // the current day of the week 22 | 23 | while(i < 7 and day < n + 1){ 24 | ans += week + i; 25 | ++day; 26 | ++i; 27 | } 28 | 29 | ++week; 30 | } 31 | 32 | return ans; 33 | } 34 | }; -------------------------------------------------------------------------------- /2023/12 - December/07-12-2023.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 07/12/2023 5 | Problem : Largest Odd Number in String 6 | Difficulty : Easy 7 | Problem Link : https://leetcode.com/problems/largest-odd-number-in-string/ 8 | Video Solution : https://youtu.be/8jsq5Hey_r0 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | string largestOddNumber(string num) { 16 | string ans = ""; 17 | vector odd = {'1', '3', '5', '7', '9'}; 18 | 19 | string cur = ""; 20 | 21 | for(auto i : num){ 22 | cur += i; 23 | 24 | if(binary_search(odd.begin(), odd.end(), i)) 25 | ans = cur; 26 | } 27 | 28 | return ans; 29 | } 30 | }; -------------------------------------------------------------------------------- /2023/12 - December/08-12-2023.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 08/12/2023 5 | Problem : Construct String from Binary Tree 6 | Difficulty : Easy 7 | Problem Link : https://leetcode.com/problems/construct-string-from-binary-tree/ 8 | Video Solution : https://youtu.be/UqIbvD4ktPw 9 | 10 | */ 11 | 12 | 13 | /** 14 | * Definition for a binary tree node. 15 | * struct TreeNode { 16 | * int val; 17 | * TreeNode *left; 18 | * TreeNode *right; 19 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 20 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 21 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 22 | * }; 23 | */ 24 | class Solution { 25 | public: 26 | string tree2str(TreeNode* root) { 27 | if(!root) 28 | return ""; 29 | 30 | string ans = to_string(root -> val); 31 | 32 | if(root -> left and root -> right){ 33 | ans += '('; 34 | ans += tree2str(root -> left); 35 | ans += ')'; 36 | ans += '('; 37 | ans += tree2str(root -> right); 38 | ans += ')'; 39 | } 40 | else if(root -> left){ 41 | ans += '('; 42 | ans += tree2str(root -> left); 43 | ans += ')'; 44 | } 45 | else if(root -> right){ 46 | ans += "()("; 47 | ans += tree2str(root -> right); 48 | ans += ')'; 49 | } 50 | 51 | return ans; 52 | } 53 | }; -------------------------------------------------------------------------------- /2023/12 - December/09-12-2023.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 09/12/2023 5 | Problem : Binary Tree Inorder Traversal 6 | Difficulty : Easy 7 | Problem Link : https://leetcode.com/problems/binary-tree-inorder-traversal/ 8 | Video Solution : https://youtu.be/z8JxFCli6aQ 9 | 10 | */ 11 | 12 | 13 | /** 14 | * Definition for a binary tree node. 15 | * struct TreeNode { 16 | * int val; 17 | * TreeNode *left; 18 | * TreeNode *right; 19 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 20 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 21 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 22 | * }; 23 | */ 24 | class Solution { 25 | public: 26 | vector inorderTraversal(TreeNode* root) { 27 | vector inorder; 28 | 29 | function traverse = [&](TreeNode * node) { 30 | if(!node) 31 | return; 32 | 33 | traverse(node -> left); 34 | inorder.push_back(node -> val); 35 | traverse(node -> right); 36 | }; 37 | 38 | traverse(root); 39 | 40 | return inorder; 41 | } 42 | }; -------------------------------------------------------------------------------- /2023/12 - December/10-12-2023.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 10/12/2023 5 | Problem : Transpose Matrix 6 | Difficulty : Easy 7 | Problem Link : https://leetcode.com/problems/transpose-matrix/ 8 | Video Solution : https://youtu.be/TCM8vxTiKmE 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | vector> transpose(vector>& matrix) { 16 | int n, m; 17 | n = matrix.size(); 18 | m = matrix[0].size(); 19 | 20 | vector> ans(m, vector (n)); 21 | 22 | for(int i = 0; i < m; i++){ 23 | for(int j = 0; j < n; j++) 24 | ans[i][j] = matrix[j][i]; 25 | } 26 | 27 | return ans; 28 | } 29 | }; -------------------------------------------------------------------------------- /2023/12 - December/11-12-2023.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 11/12/2023 5 | Problem : Element Appearing More Than 25% In Sorted Array 6 | Difficulty : Easy 7 | Problem Link : https://leetcode.com/problems/element-appearing-more-than-25-in-sorted-array/description/ 8 | Video Solution : https://youtu.be/h-AbzZY-nKo 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | int findSpecialInteger(vector& arr) { 16 | int n = arr.size(); 17 | int need = n / 4; 18 | int count = 1; 19 | 20 | for(int i = 1; i < n; i++){ 21 | if(arr[i] != arr[i - 1]){ 22 | if(count > need) 23 | return arr[i - 1]; 24 | 25 | count = 1; 26 | } 27 | else{ 28 | ++count; 29 | } 30 | } 31 | 32 | return arr[n - 1]; 33 | } 34 | }; -------------------------------------------------------------------------------- /2023/12 - December/12-12-2023.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 12/12/2023 5 | Problem : Maximum Product of Two Elements in an Array 6 | Difficulty : Easy 7 | Problem Link : https://leetcode.com/problems/maximum-product-of-two-elements-in-an-array/ 8 | Video Solution : https://youtu.be/frNimr1LdXs 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | int maxProduct(vector& nums) { 16 | int first, second; 17 | first = second = 0; 18 | 19 | for(auto i : nums){ 20 | if(i > first){ 21 | second = first; 22 | first = i; 23 | } 24 | else if(i > second){ 25 | second = i; 26 | } 27 | } 28 | 29 | return (first - 1) * (second - 1); 30 | } 31 | }; -------------------------------------------------------------------------------- /2023/12 - December/13-12-2023.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 13/12/2023 5 | Problem : Special Positions in a Binary Matrix 6 | Difficulty : Easy 7 | Problem Link : https://leetcode.com/problems/special-positions-in-a-binary-matrix/ 8 | Video Solution : https://youtu.be/Chc7JW0z7ww 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | int numSpecial(vector>& mat) { 16 | int n = mat.size(); 17 | int m = mat[0].size(); 18 | 19 | vector row(n, 0), col(m, 0); 20 | 21 | for(int i = 0; i < n; i++){ 22 | for(int j = 0; j < m; j++){ 23 | if(mat[i][j]){ 24 | ++row[i]; 25 | ++col[j]; 26 | } 27 | } 28 | } 29 | 30 | int ans = 0; 31 | 32 | for(int i = 0; i < n; i++){ 33 | for(int j = 0; j < m; j++){ 34 | ans += (mat[i][j] and row[i] == 1 and col[j] == 1); 35 | } 36 | } 37 | 38 | return ans; 39 | } 40 | }; -------------------------------------------------------------------------------- /2023/12 - December/14-12-2023.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 14/12/2023 5 | Problem : Difference Between Ones and Zeros in Row and Column 6 | Difficulty : Medium 7 | Problem Link : https://leetcode.com/problems/difference-between-ones-and-zeros-in-row-and-column/ 8 | Video Solution : https://youtu.be/EkT2krlC-FY 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | vector> onesMinusZeros(vector>& grid) { 16 | int n = grid.size(); 17 | int m = grid[0].size(); 18 | 19 | vector rows(n, 0), cols(m, 0); 20 | 21 | for(int i = 0; i < n; i++){ 22 | for(int j = 0; j < m; j++){ 23 | rows[i] += grid[i][j]; 24 | cols[j] += grid[i][j]; 25 | } 26 | } 27 | 28 | for(int i = 0; i < n; i++){ 29 | for(int j = 0; j < m; j++){ 30 | grid[i][j] = 2 * (rows[i] + cols[j]) - (n + m); 31 | } 32 | } 33 | 34 | return grid; 35 | } 36 | }; -------------------------------------------------------------------------------- /2023/12 - December/15-12-2023.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 15/12/2023 5 | Problem : Destination City 6 | Difficulty : Easy 7 | Problem Link : https://leetcode.com/problems/destination-city/ 8 | Video Solution : https://youtu.be/hjUj0hJWGH0 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | string destCity(vector>& paths) { 16 | map ans; 17 | 18 | for(auto i : paths) 19 | ans[i[1]] = 1; 20 | 21 | for(auto i : paths) 22 | ans.erase(i[0]); 23 | 24 | return (*ans.begin()).first; 25 | } 26 | }; -------------------------------------------------------------------------------- /2023/12 - December/16-12-2023.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 16/12/2023 5 | Problem : Valid Anagram 6 | Difficulty : Easy 7 | Problem Link : https://leetcode.com/problems/valid-anagram/ 8 | Video Solution : https://youtu.be/yyU3zU8eAkg 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | bool isAnagram(string s, string t) { 16 | vector f(26, 0); 17 | 18 | for(auto i : s) 19 | ++f[i - 'a']; 20 | for(auto i : t) 21 | --f[i - 'a']; 22 | 23 | bool ok = 1; 24 | for(int i = 0; i < 26; i++) 25 | ok = ok and (f[i] == 0); 26 | 27 | return ok; 28 | } 29 | }; -------------------------------------------------------------------------------- /2023/12 - December/17-12-2023.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 17/12/2023 5 | Problem : Design a Food Rating System 6 | Difficulty : Medium 7 | Problem Link : https://leetcode.com/problems/design-a-food-rating-system/ 8 | Video Solution : https://youtu.be/z9HOFXzdoXE 9 | 10 | */ 11 | 12 | 13 | class FoodRatings { 14 | public: 15 | map>> cuisine_rating; 16 | map food_type; 17 | map food_rating; 18 | 19 | FoodRatings(vector& foods, vector& cuisines, vector& ratings) { 20 | int n = foods.size(); 21 | 22 | for(int i = 0; i < n; i++){ 23 | food_type[foods[i]] = cuisines[i]; 24 | cuisine_rating[cuisines[i]][ratings[i]].insert(foods[i]); 25 | food_rating[foods[i]] = ratings[i]; 26 | } 27 | } 28 | 29 | void changeRating(string food, int newRating) { 30 | string cuisine = food_type[food]; 31 | int oldRating = food_rating[food]; 32 | 33 | cuisine_rating[cuisine][oldRating].erase(food); 34 | 35 | if(cuisine_rating[cuisine][oldRating].size() == 0) 36 | cuisine_rating[cuisine].erase(oldRating); 37 | 38 | food_rating[food] = newRating; 39 | 40 | cuisine_rating[cuisine][newRating].insert(food); 41 | } 42 | 43 | string highestRated(string cuisine) { 44 | set food_list = (*cuisine_rating[cuisine].rbegin()).second; 45 | 46 | return *food_list.begin(); 47 | } 48 | }; 49 | 50 | /** 51 | * Your FoodRatings object will be instantiated and called as such: 52 | * FoodRatings* obj = new FoodRatings(foods, cuisines, ratings); 53 | * obj->changeRating(food,newRating); 54 | * string param_2 = obj->highestRated(cuisine); 55 | */ -------------------------------------------------------------------------------- /2023/12 - December/18-12-2023.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 18/12/2023 5 | Problem : Maximum Product Difference Between Two Pairs 6 | Difficulty : Easy 7 | Problem Link : https://leetcode.com/problems/maximum-product-difference-between-two-pairs/ 8 | Video Solution : https://youtu.be/EbNkxg9vJhs 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | int maxProductDifference(vector& nums) { 16 | sort(nums.begin(), nums.end()); 17 | int n = nums.size(); 18 | 19 | return nums[n - 1] * nums[n - 2] - nums[0] * nums[1]; 20 | } 21 | }; -------------------------------------------------------------------------------- /2023/12 - December/19-12-2023.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 19/12/2023 5 | Problem : Image Smoother 6 | Difficulty : Easy 7 | Problem Link : https://leetcode.com/problems/image-smoother/ 8 | Video Solution : https://youtu.be/j-sqJdeV3TY 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | vector> imageSmoother(vector>& img) { 16 | int n = img.size(); 17 | int m = img[0].size(); 18 | 19 | int dx[] = { -1 , 1 , 0 , 0 , -1 , -1 , 1 , 1 }; 20 | int dy[] = { 0 , 0 , -1 , 1 , -1 , 1 , -1 , 1 }; 21 | 22 | // { U , D , L , R , UL , UR , DL , DR } 23 | 24 | auto valid = [&](int x, int y) -> bool { 25 | if(x < 0 or x >= n or y < 0 or y >= m) 26 | return 0; 27 | 28 | return 1; 29 | }; 30 | 31 | vector> ans(n, vector (m)); 32 | 33 | for(int i = 0; i < n; i++){ 34 | for(int j = 0; j < m; j++){ 35 | int sum = img[i][j]; 36 | int count = 1; 37 | 38 | for(int p = 0; p < 8; p++){ 39 | int nx = i + dx[p]; 40 | int ny = j + dy[p]; 41 | 42 | if(valid(nx, ny)){ 43 | sum += img[nx][ny]; 44 | ++count; 45 | } 46 | } 47 | 48 | ans[i][j] = sum / count; 49 | } 50 | } 51 | 52 | return ans; 53 | } 54 | }; -------------------------------------------------------------------------------- /2023/12 - December/20-12-2023.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 20/12/2023 5 | Problem : Buy Two Chocolates 6 | Difficulty : Easy 7 | Problem Link : https://leetcode.com/problems/buy-two-chocolates/ 8 | Video Solution : https://youtu.be/NLYvsBtoloM 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | int buyChoco(vector& prices, int money) { 16 | int mina, minb; 17 | mina = minb = 1e9; 18 | 19 | for(auto i : prices){ 20 | if(i < mina){ 21 | minb = mina; 22 | mina = i; 23 | } 24 | else if(i < minb){ 25 | minb = i; 26 | } 27 | } 28 | 29 | return (money - (mina + minb) > -1) ? money - (mina + minb) : money; 30 | } 31 | }; -------------------------------------------------------------------------------- /2023/12 - December/21-12-2023.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 21/12/2023 5 | Problem : Widest Vertical Area Between Two Points Containing No Points 6 | Difficulty : Medium 7 | Problem Link : https://leetcode.com/problems/widest-vertical-area-between-two-points-containing-no-points/ 8 | Video Solution : https://youtu.be/hlH0rwkWs3c 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | int maxWidthOfVerticalArea(vector>& points) { 16 | sort(points.begin(), points.end(), [](vector &a, vector &b){ 17 | return a[0] < b[0]; 18 | }); 19 | 20 | int ans = 0; 21 | 22 | for(int i = 1; i < points.size(); i++) 23 | ans = max(ans, points[i][0] - points[i - 1][0]); 24 | 25 | return ans; 26 | } 27 | }; -------------------------------------------------------------------------------- /2023/12 - December/22-12-2023.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 22/12/2023 5 | Problem : Maximum Score After Splitting a String 6 | Difficulty : Easy 7 | Problem Link : https://leetcode.com/problems/maximum-score-after-splitting-a-string/ 8 | Video Solution : https://youtu.be/6C9LA8kJ5ms 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | int maxScore(string s) { 16 | int n = s.size(); 17 | vector suf(n, 0); 18 | suf[n - 1] = s[n - 1] == '1'; 19 | 20 | for(int i = n - 2; i > -1; i--){ 21 | suf[i] = suf[i + 1] + (s[i] == '1'); 22 | } 23 | 24 | int zero = 0; 25 | int ans = 0; 26 | 27 | for(int i = 0; i < n - 1; i++){ 28 | zero += s[i] == '0'; 29 | 30 | ans = max(ans, zero + suf[i + 1]); 31 | } 32 | 33 | return ans; 34 | } 35 | }; -------------------------------------------------------------------------------- /2023/12 - December/23-12-2023.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 23/12/2023 5 | Problem : Path Crossing 6 | Difficulty : Easy 7 | Problem Link : https://leetcode.com/problems/path-crossing/ 8 | Video Solution : https://youtu.be/DCyTR8uAcV8 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | bool isPathCrossing(string path) { 16 | map, bool> vis; 17 | 18 | int cx, cy; 19 | cx = cy = 0; 20 | 21 | for(auto i : path){ 22 | vis[{cx, cy}] = 1; 23 | 24 | if(i == 'N') 25 | --cx; 26 | else if(i == 'S') 27 | ++cx; 28 | else if(i == 'E') 29 | ++cy; 30 | else 31 | --cy; 32 | 33 | if(vis.count({cx, cy})) 34 | return 1; 35 | } 36 | 37 | return 0; 38 | } 39 | }; -------------------------------------------------------------------------------- /2023/12 - December/24-12-2023.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 24/12/2023 5 | Problem : Minimum Changes To Make Alternating Binary String 6 | Difficulty : Easy 7 | Problem Link : https://leetcode.com/problems/minimum-changes-to-make-alternating-binary-string/ 8 | Video Solution : https://youtu.be/tHcQEvAPE68 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | int minOperations(string s) { 16 | // start with 0 17 | 18 | int expect = 0; 19 | int ans1 = 0; 20 | 21 | for(auto i : s){ 22 | ans1 += expect != (i - '0'); 23 | 24 | expect ^= 1; 25 | } 26 | 27 | // start with 1 28 | 29 | expect = 1; 30 | int ans2 = 0; 31 | 32 | for(auto i : s){ 33 | ans2 += expect != (i - '0'); 34 | 35 | expect ^= 1; 36 | } 37 | 38 | return min(ans1, ans2); 39 | } 40 | }; -------------------------------------------------------------------------------- /2023/12 - December/25-12-2023.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 25/12/2023 5 | Problem : Decode Ways 6 | Difficulty : Medium 7 | Problem Link : https://leetcode.com/problems/decode-ways/ 8 | Video Solution : NA 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | int numDecodings(string s) { 16 | int n = s.size(); 17 | 18 | vector dp(n + 1); 19 | dp[n] = 1; 20 | 21 | for(int i = n - 1; i > -1; i--){ 22 | if(s[i] == '0'){ 23 | dp[i] = 0; 24 | } 25 | else{ 26 | dp[i] = dp[i + 1]; 27 | 28 | if(i < n - 1 and stoi(s.substr(i, 2)) < 27) 29 | dp[i] += dp[i + 2]; 30 | } 31 | } 32 | 33 | return dp[0]; 34 | } 35 | }; -------------------------------------------------------------------------------- /2023/12 - December/26-12-2023.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 26/12/2023 5 | Problem : Number of Dice Rolls With Target Sum 6 | Difficulty : Medium 7 | Problem Link : https://leetcode.com/problems/number-of-dice-rolls-with-target-sum/ 8 | Video Solution : https://youtu.be/NIayybqPOpg 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | int numRollsToTarget(int n, int k, int target) { 16 | vector> dp(n + 1, vector (k * n + 1, 0)); 17 | 18 | if(target > k * n) 19 | return 0; 20 | 21 | dp[n][target] = 1; 22 | 23 | const int mod = 1e9 + 7; 24 | 25 | for(int i = n - 1; i > -1; i--){ 26 | for(int j = 0; j < k * i + 1; j++){ 27 | 28 | for(int z = 1; z < k + 1; z++){ 29 | dp[i][j] = (dp[i][j] + dp[i + 1][j + z]) % mod; 30 | } 31 | } 32 | } 33 | 34 | return dp[0][0]; 35 | } 36 | }; -------------------------------------------------------------------------------- /2023/12 - December/27-12-2023.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 27/12/2023 5 | Problem : Minimum Time to Make Rope Colorful 6 | Difficulty : Medium 7 | Problem Link : https://leetcode.com/problems/minimum-time-to-make-rope-colorful/ 8 | Video Solution : https://youtu.be/uhBSJYY1Mw4 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | int minCost(string colors, vector& neededTime) { 16 | int n = colors.size(); 17 | vector> dp(n + 1, vector (27, 0)); 18 | 19 | const int inf = 1e9; 20 | 21 | for(int i = n - 1; i > -1; i--){ 22 | for(int j = 0; j < 27; j++){ 23 | int remove, noremove; 24 | 25 | remove = neededTime[i] + dp[i + 1][j]; 26 | noremove = dp[i + 1][colors[i] - 'a' + 1]; 27 | 28 | if(colors[i] - 'a' + 1 == j) 29 | dp[i][j] = remove; 30 | else 31 | dp[i][j] = min(remove, noremove); 32 | } 33 | } 34 | 35 | return dp[0][0]; 36 | } 37 | }; -------------------------------------------------------------------------------- /2023/12 - December/28-12-2023.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 28/12/2023 5 | Problem : String Compression II 6 | Difficulty : Hard 7 | Problem Link : https://leetcode.com/problems/string-compression-ii/ 8 | Video Solution : https://youtu.be/u7b9avoQtbY 9 | 10 | */ 11 | 12 | 13 | int dp[110][110][27][110]; 14 | 15 | class Solution { 16 | public: 17 | int getLengthOfOptimalCompression(string s, int k) { 18 | int size = s.size() + 1; 19 | 20 | for(int p = 0; p < size + 1; p++){ 21 | for(int q = 0; q < k + 1; q++){ 22 | for(int r = 0; r < 27; r++){ 23 | for(int s = 0; s < size + 1; s++) 24 | dp[p][q][r][s] = -1; 25 | } 26 | } 27 | } 28 | 29 | function helper = [&](int cur_pos, int left_k, int last_char, int last_count) -> int { 30 | if(cur_pos == s.size()) 31 | return 0; 32 | 33 | if(dp[cur_pos][left_k][last_char][last_count] != -1) 34 | return dp[cur_pos][left_k][last_char][last_count]; 35 | 36 | int take, notake; 37 | take = notake = 1e9; 38 | 39 | int inc = last_count == 1 or last_count == 9 or last_count == 99; 40 | 41 | if(s[cur_pos] - 'a' == last_char){ 42 | take = helper(cur_pos + 1, left_k, s[cur_pos] - 'a', last_count + 1) + inc; 43 | } 44 | else{ 45 | take = helper(cur_pos + 1, left_k, s[cur_pos] - 'a', 1) + 1; 46 | } 47 | 48 | if(left_k > 0){ 49 | notake = helper(cur_pos + 1, left_k - 1, last_char, last_count); 50 | } 51 | 52 | 53 | return dp[cur_pos][left_k][last_char][last_count] = min(take, notake); 54 | }; 55 | 56 | return helper(0, k, 26, 0); 57 | } 58 | }; -------------------------------------------------------------------------------- /2023/12 - December/29-12-2023.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 29/12/2023 5 | Problem : Minimum Difficulty of a Job Schedule 6 | Difficulty : Hard 7 | Problem Link : https://leetcode.com/problems/minimum-difficulty-of-a-job-schedule/ 8 | Video Solution : https://youtu.be/D-BJwxRfwCU 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | int minDifficulty(vector& jobDifficulty, int d) { 16 | int n = jobDifficulty.size(); 17 | 18 | const int inf = 1e5; 19 | 20 | vector> dp(n + 1, vector (d + 1, inf)); 21 | dp[n][d] = 0; 22 | 23 | for(int i = n - 1; i > -1; i--){ 24 | for(int j = 0; j < d; j++){ 25 | int MAX = 0; 26 | 27 | for(int k = i; k < n; k++){ 28 | MAX = max(MAX, jobDifficulty[k]); 29 | 30 | dp[i][j] = min(dp[i][j], MAX + dp[k + 1][j + 1]); 31 | } 32 | } 33 | } 34 | 35 | return dp[0][0] >= inf ? -1 : dp[0][0]; 36 | } 37 | }; -------------------------------------------------------------------------------- /2023/12 - December/30-12-2023.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 30/12/2023 5 | Problem : Redistribute Characters to Make All Strings Equal 6 | Difficulty : Easy 7 | Problem Link : https://leetcode.com/problems/redistribute-characters-to-make-all-strings-equal/ 8 | Video Solution : https://youtu.be/t7nO6EGvvfY 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | bool makeEqual(vector& words) { 16 | vector f(26, 0); 17 | 18 | for(auto i : words){ 19 | for(auto j : i) 20 | ++f[j - 'a']; 21 | } 22 | 23 | bool ok = 1; 24 | 25 | for(int i = 0; i < 26; i++) 26 | ok = ok and !(f[i] % words.size()); 27 | 28 | return ok; 29 | } 30 | }; -------------------------------------------------------------------------------- /2023/12 - December/31-12-2023.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 31/12/2023 5 | Problem : Largest Substring Between Two Equal Characters 6 | Difficulty : Easy 7 | Problem Link : https://leetcode.com/problems/largest-substring-between-two-equal-characters/ 8 | Video Solution : https://youtu.be/AU0ZQ3vUVg0 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | int maxLengthBetweenEqualCharacters(string s) { 16 | vector> f(26, {-1, -1}); 17 | int n = s.size(); 18 | 19 | for(int i = 0; i < n; i++){ 20 | f[s[i] - 'a'][1] = i; 21 | f[s[n - i - 1] - 'a'][0] = n - i - 1; 22 | } 23 | 24 | int ans = -1; 25 | 26 | for(int i = 0; i < 26; i++) 27 | if(f[i][0] != -1) 28 | ans = max(ans, f[i][1] - f[i][0] - 1); 29 | 30 | return ans; 31 | } 32 | }; -------------------------------------------------------------------------------- /2024/01 - January/01-01-2024.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 01/01/2024 5 | Problem : Assign Cookies 6 | Difficulty : Easy 7 | Problem Link : https://leetcode.com/problems/assign-cookies/ 8 | Video Solution : https://youtu.be/QKWNyBV7GJ8 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | int findContentChildren(vector& g, vector& s) { 16 | sort(g.begin(), g.end(), greater()); 17 | sort(s.begin(), s.end(), greater()); 18 | 19 | int ans = 0; 20 | 21 | int child = 0, cookie = 0; 22 | 23 | while(cookie < s.size() and child < g.size()){ 24 | if(g[child] <= s[cookie]){ 25 | ++ans; 26 | ++child; 27 | ++cookie; 28 | } 29 | else{ 30 | ++child; 31 | } 32 | } 33 | 34 | return ans; 35 | } 36 | }; -------------------------------------------------------------------------------- /2024/01 - January/02-01-2024.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 02/01/2024 5 | Problem : Convert an Array Into a 2D Array With Conditions 6 | Difficulty : Medium 7 | Problem Link : https://leetcode.com/problems/convert-an-array-into-a-2d-array-with-conditions/ 8 | Video Solution : https://youtu.be/Zywrn_9zLKU 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | vector> findMatrix(vector& nums) { 16 | unordered_map f; 17 | 18 | for(auto i : nums) 19 | ++f[i]; 20 | 21 | int size = 0; 22 | 23 | for(auto i : f) 24 | size = max(size, i.second); 25 | 26 | vector> ans(size); 27 | 28 | for(auto i : f){ 29 | for(int j = 0; j < i.second; j++) 30 | ans[j].push_back(i.first); 31 | } 32 | 33 | return ans; 34 | } 35 | }; -------------------------------------------------------------------------------- /2024/01 - January/03-01-2024.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 03/01/2024 5 | Problem : Number of Laser Beams in a Bank 6 | Difficulty : Medium 7 | Problem Link : https://leetcode.com/problems/number-of-laser-beams-in-a-bank/ 8 | Video Solution : https://youtu.be/7bMnPC3mVCI 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | int numberOfBeams(vector& bank) { 16 | int ans = 0; 17 | int next = 0; 18 | 19 | for(int i = bank.size() - 1; i > -1; i--){ 20 | int to_be_next = 0; 21 | 22 | for(auto j : bank[i]){ 23 | if(j == '1'){ 24 | ++to_be_next; 25 | ans += next; 26 | } 27 | } 28 | 29 | next = to_be_next > 0 ? to_be_next : next; 30 | } 31 | 32 | return ans; 33 | } 34 | }; -------------------------------------------------------------------------------- /2024/01 - January/04-01-2024.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 04/01/2024 5 | Problem : Minimum Number of Operations to Make Array Empty 6 | Difficulty : Medium 7 | Problem Link : https://leetcode.com/problems/minimum-number-of-operations-to-make-array-empty/ 8 | Video Solution : https://youtu.be/HOB5HwnkIrE 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | int minOperations(vector& nums) { 16 | int ans = 0; 17 | 18 | unordered_map f; 19 | 20 | for(auto i : nums) 21 | ++f[i]; 22 | 23 | for(auto i : f){ 24 | if(i.second % 3 == 0){ 25 | ans += i.second / 3; 26 | } 27 | else if(i.second % 3 == 2){ 28 | ++ans; 29 | ans += (i.second - 2) / 3; 30 | } 31 | else{ 32 | if(i.second == 1) 33 | return -1; 34 | 35 | ans += 2; 36 | ans += (i.second - 4) / 3; 37 | } 38 | } 39 | 40 | return ans; 41 | } 42 | }; -------------------------------------------------------------------------------- /2024/01 - January/05-01-2024.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 05/01/2024 5 | Problem : Longest Increasing Subsequence 6 | Difficulty : Medium 7 | Problem Link : https://leetcode.com/problems/longest-increasing-subsequence/ 8 | Video Solution : https://youtu.be/cvm3rvd8Pfw 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | int lengthOfLIS(vector& nums) { 16 | int n = nums.size(); 17 | 18 | vector dp(n, 1e5); 19 | 20 | for(int i = 0; i < n; i++){ 21 | int p = lower_bound(dp.begin(), dp.end(), nums[i]) - dp.begin(); 22 | 23 | if(p < n) 24 | dp[p] = min(dp[p], nums[i]); 25 | } 26 | 27 | for(int i = n - 1; i > -1; i--){ 28 | if(dp[i] < 1e5) 29 | return i + 1; 30 | } 31 | 32 | return 0; 33 | } 34 | }; -------------------------------------------------------------------------------- /2024/01 - January/06-01-2024.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 06/01/2024 5 | Problem : Maximum Profit in Job Scheduling 6 | Difficulty : Hard 7 | Problem Link : https://leetcode.com/problems/maximum-profit-in-job-scheduling/ 8 | Video Solution : https://youtu.be/IBSv87Sk-rw 9 | 10 | */ 11 | 12 | 13 | class job{ 14 | public: 15 | int start, end, profit; 16 | 17 | job(int a, int b, int c){ 18 | start = a; 19 | end = b; 20 | profit = c; 21 | } 22 | }; 23 | 24 | class Solution { 25 | public: 26 | int jobScheduling(vector& startTime, vector& endTime, vector& profit) { 27 | int n = profit.size(); 28 | 29 | vector jobs; 30 | 31 | for(int i = 0; i < n; i++){ 32 | job newJob(startTime[i], endTime[i], profit[i]); 33 | jobs.emplace_back(newJob); 34 | } 35 | 36 | sort(jobs.begin(), jobs.end(), [](job &a, job &b){ 37 | return a.start < b.start; 38 | }); 39 | 40 | vector dp(n + 1, 0); 41 | 42 | for(int i = n - 1; i > -1; i--){ 43 | int take, notake; 44 | 45 | notake = dp[i + 1]; 46 | 47 | int low = i; // low is always false 48 | int high = n; // high is always true 49 | 50 | while(low < high - 1){ 51 | int mid = low + (high - low) / 2; 52 | 53 | if(jobs[mid].start >= jobs[i].end) 54 | high = mid; 55 | else 56 | low = mid; 57 | } 58 | 59 | take = jobs[i].profit + dp[high]; 60 | 61 | dp[i] = max(take, notake); 62 | } 63 | 64 | return dp[0]; 65 | } 66 | }; -------------------------------------------------------------------------------- /2024/01 - January/07-01-2024.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 07/01/2024 5 | Problem : Arithmetic Slices II - Subsequence 6 | Difficulty : Hard 7 | Problem Link : https://leetcode.com/problems/arithmetic-slices-ii-subsequence/ 8 | Video Solution : https://youtu.be/mcoTTCwPmTk 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | int numberOfArithmeticSlices(vector& nums) { 16 | int n = nums.size(); 17 | 18 | vector> dp(n); 19 | unordered_map> pos; 20 | 21 | for(int i = 0; i < n; i++) 22 | pos[nums[i]].push_back(i); 23 | 24 | int ans = 0; 25 | 26 | function helper = [&](int p, long long dx) -> int { 27 | if(dp[p].count(dx)) 28 | return dp[p][dx]; 29 | 30 | dp[p][dx] = 1; 31 | 32 | long long need = nums[p] + dx; 33 | 34 | int next = lower_bound(pos[need].begin(), pos[need].end(), p + 1) - pos[need].begin(); 35 | 36 | for(int i = next; i < pos[need].size(); i++){ 37 | dp[p][dx] += helper(pos[need][i], dx); 38 | } 39 | 40 | return dp[p][dx]; 41 | }; 42 | 43 | for(int i = 0; i < n; i++){ 44 | for(int j = i + 1; j < n; j++) 45 | ans += helper(j, (long long)nums[j] - (long long)nums[i]) - 1; 46 | } 47 | 48 | return ans; 49 | } 50 | }; -------------------------------------------------------------------------------- /2024/01 - January/08-01-2024.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 08/01/2024 5 | Problem : Range Sum of BST 6 | Difficulty : Easy 7 | Problem Link : https://leetcode.com/problems/range-sum-of-bst/ 8 | Video Solution : https://youtu.be/EBSxsMnbGHs 9 | 10 | */ 11 | 12 | 13 | /** 14 | * Definition for a binary tree node. 15 | * struct TreeNode { 16 | * int val; 17 | * TreeNode *left; 18 | * TreeNode *right; 19 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 20 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 21 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 22 | * }; 23 | */ 24 | class Solution { 25 | public: 26 | int rangeSumBST(TreeNode* root, int low, int high) { 27 | int ans = 0; 28 | 29 | function traverse = [&](TreeNode * node) { 30 | if(!node) 31 | return; 32 | 33 | if(node -> val >= low and node -> val <= high){ 34 | ans += node -> val; 35 | traverse(node -> left); 36 | traverse(node -> right); 37 | } 38 | else if(node -> val < low){ 39 | traverse(node -> right); 40 | } 41 | else if(node -> val > high){ 42 | traverse(node -> left); 43 | } 44 | }; 45 | 46 | traverse(root); 47 | 48 | return ans; 49 | } 50 | }; -------------------------------------------------------------------------------- /2024/01 - January/09-01-2024.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 09/01/2024 5 | Problem : Leaf-Similar Trees 6 | Difficulty : Easy 7 | Problem Link : https://leetcode.com/problems/leaf-similar-trees/ 8 | Video Solution : https://youtu.be/P7gBx_vktgY 9 | 10 | */ 11 | 12 | 13 | /** 14 | * Definition for a binary tree node. 15 | * struct TreeNode { 16 | * int val; 17 | * TreeNode *left; 18 | * TreeNode *right; 19 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 20 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 21 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 22 | * }; 23 | */ 24 | class Solution { 25 | public: 26 | bool leafSimilar(TreeNode* root1, TreeNode* root2) { 27 | deque leaves; 28 | bool ans = 1; 29 | 30 | function traverse = [&](TreeNode * node, bool push) { 31 | if(!node) 32 | return; 33 | 34 | if(!(node -> left) and !(node -> right)){ 35 | if(push){ 36 | leaves.push_back(node -> val); 37 | } 38 | else{ 39 | if(leaves.empty() or node -> val != leaves.front()) 40 | ans = 0; 41 | 42 | if(!leaves.empty()) 43 | leaves.pop_front(); 44 | } 45 | } 46 | 47 | traverse(node -> left, push); 48 | traverse(node -> right, push); 49 | }; 50 | 51 | traverse(root1, 1); 52 | traverse(root2, 0); 53 | 54 | return (ans and leaves.empty()); 55 | } 56 | }; -------------------------------------------------------------------------------- /2024/01 - January/10-01-2024.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 10/01/2024 5 | Problem : Amount of Time for Binary Tree to Be Infected 6 | Difficulty : Medium 7 | Problem Link : https://leetcode.com/problems/amount-of-time-for-binary-tree-to-be-infected/ 8 | Video Solution : https://youtu.be/Bqx8AbNzGE4 9 | 10 | */ 11 | 12 | 13 | /** 14 | * Definition for a binary tree node. 15 | * struct TreeNode { 16 | * int val; 17 | * TreeNode *left; 18 | * TreeNode *right; 19 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 20 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 21 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 22 | * }; 23 | */ 24 | class Solution { 25 | public: 26 | int amountOfTime(TreeNode* root, int start) { 27 | int ans = 0; 28 | map height; 29 | height[nullptr] = 0; 30 | 31 | function dfs1 = [&](TreeNode * node) -> int { 32 | if(!node) 33 | return -1; 34 | 35 | int left = dfs1(node -> left); 36 | int right = dfs1(node -> right); 37 | 38 | return height[node] = max(left, right) + 1; 39 | }; 40 | 41 | function dfs2 = [&](TreeNode * node, int prev) { 42 | if(!node) 43 | return; 44 | 45 | if(node -> val == start){ 46 | ans = max(prev, height[node]); 47 | return; 48 | } 49 | 50 | int left = (node -> left) ? height[node -> left] + 1 : 0; 51 | int right = (node -> right) ? height[node -> right] + 1 : 0; 52 | 53 | dfs2(node -> left, 1 + max(prev, right)); 54 | dfs2(node -> right, 1 + max(prev, left)); 55 | }; 56 | 57 | dfs1(root); 58 | dfs2(root, 0); 59 | 60 | return ans; 61 | } 62 | }; -------------------------------------------------------------------------------- /2024/01 - January/11-01-2024.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 11/01/2024 5 | Problem : Maximum Difference Between Node and Ancestor 6 | Difficulty : Medium 7 | Problem Link : https://leetcode.com/problems/maximum-difference-between-node-and-ancestor/ 8 | Video Solution : https://youtu.be/LZxShI1FwAw 9 | 10 | */ 11 | 12 | 13 | /** 14 | * Definition for a binary tree node. 15 | * struct TreeNode { 16 | * int val; 17 | * TreeNode *left; 18 | * TreeNode *right; 19 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 20 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 21 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 22 | * }; 23 | */ 24 | class Solution { 25 | public: 26 | int maxAncestorDiff(TreeNode* root) { 27 | int ans = 0; 28 | const int inf = 1e6; 29 | 30 | function(TreeNode *)> dfs = [&](TreeNode * node) -> pair { 31 | if(!node) 32 | return {inf, -inf}; 33 | 34 | pair left = dfs(node -> left); 35 | pair right = dfs(node -> right); 36 | 37 | int MIN = min(left.first, right.first); 38 | int MAX = max(left.second, right.second); 39 | 40 | if(node -> left or node -> right) 41 | ans = max({ans, abs(MIN - node -> val), abs(MAX - node -> val)}); 42 | 43 | return {min(MIN, node -> val), max(MAX, node -> val)}; 44 | }; 45 | 46 | dfs(root); 47 | 48 | return ans; 49 | } 50 | }; -------------------------------------------------------------------------------- /2024/01 - January/12-01-2024.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 12/01/2024 5 | Problem : Determine if String Halves Are Alike 6 | Difficulty : Easy 7 | Problem Link : https://leetcode.com/problems/determine-if-string-halves-are-alike/ 8 | Video Solution : NA 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | bool halvesAreAlike(string s) { 16 | auto gud = [&](char c) -> bool { 17 | return c == 'a' or c == 'e' or c == 'i' or c == 'o' or c == 'u'; 18 | }; 19 | 20 | int n = s.size(); 21 | 22 | int cur = 0; 23 | 24 | for(int i = 0; i < n / 2; i++){ 25 | cur += gud(tolower(s[i])); 26 | } 27 | 28 | for(int i = n / 2; i < n; i++){ 29 | cur -= gud(tolower(s[i])); 30 | } 31 | 32 | return cur == 0; 33 | } 34 | }; -------------------------------------------------------------------------------- /2024/01 - January/13-01-2024.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 13/01/2024 5 | Problem : Minimum Number of Steps to Make Two Strings Anagram 6 | Difficulty : Medium 7 | Problem Link : https://leetcode.com/problems/minimum-number-of-steps-to-make-two-strings-anagram/ 8 | Video Solution : https://youtu.be/wOsf_MWR2to 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | int minSteps(string s, string t) { 16 | vector f(26, 0); 17 | 18 | for(auto i : s) 19 | ++f[i - 'a']; 20 | for(auto i : t) 21 | --f[i - 'a']; 22 | 23 | int ans = 0; 24 | 25 | for(int i = 0; i < 26; i++){ 26 | ans += f[i] < 0 ? -f[i] : 0; 27 | } 28 | 29 | return ans; 30 | } 31 | }; -------------------------------------------------------------------------------- /2024/01 - January/14-01-2024.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 14/01/2024 5 | Problem : Determine if Two Strings Are Close 6 | Difficulty : Medium 7 | Problem Link : https://leetcode.com/problems/determine-if-two-strings-are-close/description/ 8 | Video Solution : https://youtu.be/v70hGCpmMz8 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | bool closeStrings(string word1, string word2) { 16 | vector need(26, 0), have(26, 0); 17 | 18 | for(auto i : word1) 19 | ++need[i - 'a']; 20 | for(auto i : word2) 21 | ++have[i - 'a']; 22 | 23 | for(int i = 0; i < 26; i++){ 24 | if(need[i] == have[i]) 25 | continue; 26 | 27 | for(int j = i + 1; j < 26; j++){ 28 | if(need[i] == have[j] and have[i] and have[j]){ 29 | swap(have[i], have[j]); 30 | break; 31 | } 32 | } 33 | } 34 | 35 | return need == have; 36 | } 37 | }; -------------------------------------------------------------------------------- /2024/01 - January/15-01-2024.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 15/01/2024 5 | Problem : Find Players With Zero or One Losses 6 | Difficulty : Medium 7 | Problem Link : https://www.geeksforgeeks.org/problems/grinding-geek/1 8 | Video Solution : https://youtu.be/Wn5JxNlf1yo 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | vector> findWinners(vector>& matches) { 16 | vector part(1e5 + 10, 0), lose(1e5 + 10, 0); 17 | 18 | for(auto i : matches){ 19 | part[i[0]] = part[i[1]] = 1; 20 | 21 | ++lose[i[1]]; 22 | } 23 | 24 | vector> ans(2); 25 | 26 | for(int i = 1; i < 1e5 + 10; i++){ 27 | if(part[i]){ 28 | if(lose[i] == 1) 29 | ans[1].push_back(i); 30 | else if(lose[i] == 0) 31 | ans[0].push_back(i); 32 | } 33 | } 34 | 35 | return ans; 36 | } 37 | }; -------------------------------------------------------------------------------- /2024/01 - January/16-01-2024.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 16/01/2024 5 | Problem : Insert Delete GetRandom O(1) 6 | Difficulty : Medium 7 | Problem Link : https://leetcode.com/problems/insert-delete-getrandom-o1/description/ 8 | Video Solution : https://youtu.be/teTh40HoM60 9 | 10 | */ 11 | 12 | 13 | class RandomizedSet { 14 | public: 15 | unordered_map pos; 16 | vector values; 17 | 18 | RandomizedSet() { 19 | 20 | } 21 | 22 | bool insert(int val) { 23 | if(pos.find(val) != pos.end()) 24 | return 0; 25 | 26 | pos[val] = values.size(); 27 | values.push_back(val); 28 | 29 | return 1; 30 | } 31 | 32 | bool remove(int val) { 33 | if(pos.find(val) == pos.end()) 34 | return 0; 35 | 36 | int p1 = pos[val]; 37 | int p2 = values.size() - 1; 38 | 39 | swap(values[p1], values[p2]); 40 | values.pop_back(); 41 | 42 | pos[values[p1]] = p1; 43 | 44 | pos.erase(val); 45 | 46 | return 1; 47 | } 48 | 49 | int getRandom() { 50 | return values[rand() % values.size()]; 51 | } 52 | }; 53 | 54 | /** 55 | * Your RandomizedSet object will be instantiated and called as such: 56 | * RandomizedSet* obj = new RandomizedSet(); 57 | * bool param_1 = obj->insert(val); 58 | * bool param_2 = obj->remove(val); 59 | * int param_3 = obj->getRandom(); 60 | */ -------------------------------------------------------------------------------- /2024/01 - January/17-01-2024.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 17/01/2024 5 | Problem : Unique Number of Occurrences 6 | Difficulty : Easy 7 | Problem Link : https://leetcode.com/problems/unique-number-of-occurrences/description/ 8 | Video Solution : https://youtu.be/neDNGPm_QDw 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | bool uniqueOccurrences(vector& arr) { 16 | sort(arr.begin(), arr.end()); 17 | 18 | vector f(1001, 0); 19 | int count = 1; 20 | 21 | for(int i = 1; i < arr.size(); i++){ 22 | if(arr[i] != arr[i - 1]){ 23 | if(f[count]) 24 | return 0; 25 | 26 | f[count] = 1; 27 | count = 1; 28 | } 29 | else{ 30 | ++count; 31 | } 32 | } 33 | 34 | return !f[count]; 35 | } 36 | }; -------------------------------------------------------------------------------- /2024/01 - January/18-01-2024.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 18/01/2024 5 | Problem : Climbing Stairs 6 | Difficulty : Easy 7 | Problem Link : https://leetcode.com/problems/climbing-stairs/description/ 8 | Video Solution : https://youtu.be/Q_XYBvXg0fw 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | int climbStairs(int n) { 16 | vector fibo(n + 1); 17 | fibo[0] = fibo[1] = 1; 18 | 19 | for(int i = 2; i < n + 1; i++) 20 | fibo[i] = fibo[i - 1] + fibo[i - 2]; 21 | 22 | return fibo[n]; 23 | } 24 | }; -------------------------------------------------------------------------------- /2024/01 - January/19-01-2024.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 19/01/2024 5 | Problem : Minimum Falling Path Sum 6 | Difficulty : Medium 7 | Problem Link : https://leetcode.com/problems/minimum-falling-path-sum/description/ 8 | Video Solution : https://youtu.be/j1IRI_GgnbI 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | int minFallingPathSum(vector>& matrix) { 16 | int n = matrix.size(); 17 | int m = matrix[0].size(); 18 | 19 | int dp[n][m]; 20 | for(int i = 0; i < m; i++) 21 | dp[n - 1][i] = matrix[n - 1][i]; 22 | 23 | for(int i = n - 2; i > -1; i--){ 24 | for(int j = 0; j < m; j++){ 25 | int best = dp[i + 1][j]; 26 | 27 | if(j > 0) 28 | best = min(dp[i + 1][j - 1], best); 29 | 30 | if(j < n - 1) 31 | best = min(dp[i + 1][j + 1], best); 32 | 33 | dp[i][j] = best + matrix[i][j]; 34 | } 35 | } 36 | 37 | return *min_element(dp[0], dp[0] + m); 38 | } 39 | }; -------------------------------------------------------------------------------- /2024/01 - January/20-01-2024.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 20/01/2024 5 | Problem : Sum of Subarray Minimums 6 | Difficulty : Medium 7 | Problem Link : https://leetcode.com/problems/sum-of-subarray-minimums/description/ 8 | Video Solution : https://youtu.be/05wcxHDjupg 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | int sumSubarrayMins(vector& arr) { 16 | int n = arr.size(); 17 | vector> order(n); 18 | long long ans = 0; 19 | const long long mod = 1e9 + 7; 20 | 21 | for(int i = 0; i < n; i++) 22 | order[i] = {arr[i], i}; 23 | 24 | sort(order.begin(), order.end()); 25 | 26 | function helper = [&](int low, int high, int pos) { 27 | int nextp = order[pos].second; 28 | 29 | while(nextp < low or nextp > high){ 30 | nextp = order[++pos].second; 31 | } 32 | 33 | long long left = nextp - low + 1; 34 | long long right = high - nextp + 1; 35 | 36 | ans = (ans + (left * right * arr[nextp]) % mod) % mod; 37 | 38 | if(nextp > low) 39 | helper(low, nextp - 1, pos + 1); 40 | if(nextp < high) 41 | helper(nextp + 1, high, pos + 1); 42 | }; 43 | 44 | helper(0, n - 1, 0); 45 | 46 | return ans; 47 | } 48 | }; -------------------------------------------------------------------------------- /2024/01 - January/21-01-2024.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 21/01/2024 5 | Problem : House Robber 6 | Difficulty : Medium 7 | Problem Link : https://leetcode.com/problems/house-robber/description/ 8 | Video Solution : https://youtu.be/1STDPYJwjX0 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | int rob(vector& nums) { 16 | int n = nums.size(); 17 | 18 | int dp[n + 1][2]; 19 | dp[n][0] = dp[n][1] = 0; 20 | 21 | for(int i = n - 1; i > -1; i--){ 22 | for(int j = 0; j < 2; j++){ 23 | int take = 0, notake = 0; 24 | 25 | if(j == 0) 26 | take = nums[i] + dp[i + 1][1]; 27 | 28 | notake = dp[i + 1][0]; 29 | 30 | dp[i][j] = max(take, notake); 31 | } 32 | } 33 | 34 | return dp[0][0]; 35 | } 36 | }; -------------------------------------------------------------------------------- /2024/01 - January/22-01-2024.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 22/01/2024 5 | Problem : Set Mismatch 6 | Difficulty : Easy 7 | Problem Link : https://leetcode.com/problems/set-mismatch/description/ 8 | Video Solution : NA 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | vector findErrorNums(vector& nums) { 16 | vector taken(1e4 + 10, 0); 17 | 18 | int n = nums.size(); 19 | int sum = 0; 20 | vector ans(2); 21 | 22 | for(int i = 0; i < n; i++){ 23 | if(!taken[nums[i]]){ 24 | sum += nums[i]; 25 | taken[nums[i]] = 1; 26 | } 27 | else{ 28 | ans[0] = nums[i]; 29 | } 30 | } 31 | 32 | ans[1] = (n * (n + 1)) / 2 - sum; 33 | 34 | return ans; 35 | } 36 | }; -------------------------------------------------------------------------------- /2024/01 - January/23-01-2024.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 23/01/2024 5 | Problem : Maximum Length of a Concatenated String with Unique Characters 6 | Difficulty : Medium 7 | Problem Link : https://leetcode.com/problems/maximum-length-of-a-concatenated-string-with-unique-characters/description/ 8 | Video Solution : https://youtu.be/-Y4hPWZZGAY 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | int maxLength(vector& arr) { 16 | int ans = 0; 17 | int n = arr.size(); 18 | 19 | for(int i = 0; i < (1 << n); i++){ 20 | int cur_ans = 0; 21 | vector f(26, 0); 22 | 23 | for(int j = 0; j < n; j++){ 24 | if((i >> j) & 1){ 25 | cur_ans += arr[j].size(); 26 | 27 | for(auto x : arr[j]) 28 | ++f[x - 'a']; 29 | } 30 | } 31 | 32 | bool ok = 1; 33 | for(auto x : f){ 34 | if(x > 1){ 35 | ok = 0; 36 | break; 37 | } 38 | } 39 | 40 | if(ok) 41 | ans = max(ans, cur_ans); 42 | } 43 | 44 | return ans; 45 | } 46 | }; -------------------------------------------------------------------------------- /2024/01 - January/24-01-2024.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 24/01/2024 5 | Problem : Pseudo-Palindromic Paths in a Binary Tree 6 | Difficulty : Medium 7 | Problem Link : https://leetcode.com/problems/pseudo-palindromic-paths-in-a-binary-tree/description/ 8 | Video Solution : https://youtu.be/r5MWg0FYgCc 9 | 10 | */ 11 | 12 | 13 | /** 14 | * Definition for a binary tree node. 15 | * struct TreeNode { 16 | * int val; 17 | * TreeNode *left; 18 | * TreeNode *right; 19 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 20 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 21 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 22 | * }; 23 | */ 24 | class Solution { 25 | public: 26 | int pseudoPalindromicPaths (TreeNode* root) { 27 | int ans = 0; 28 | vector f(10, 0); 29 | int count = 0; 30 | 31 | 32 | function dfs = [&](TreeNode * node, bool need) { 33 | if(!node) 34 | return; 35 | 36 | int val = node -> val; 37 | 38 | f[val] ^= 1; 39 | if(f[val]) 40 | ++count; 41 | else 42 | --count; 43 | 44 | if(!(node -> left) and !(node -> right)){ 45 | ans += count == need; 46 | } 47 | 48 | dfs(node -> left, need ^ 1); 49 | dfs(node -> right, need ^ 1); 50 | 51 | f[val] ^= 1; 52 | if(f[val]) 53 | ++count; 54 | else 55 | --count; 56 | }; 57 | 58 | dfs(root, 1); 59 | 60 | return ans; 61 | } 62 | }; -------------------------------------------------------------------------------- /2024/01 - January/25-01-2024.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 25/01/2024 5 | Problem : Longest Common Subsequence 6 | Difficulty : Medium 7 | Problem Link : https://leetcode.com/problems/longest-common-subsequence/description/ 8 | Video Solution : https://youtu.be/CIHmh14hkMM 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | int longestCommonSubsequence(string text1, string text2) { 16 | int n = text1.size(); 17 | int m = text2.size(); 18 | 19 | int dp[n + 1][m + 1]; 20 | 21 | for(int i = 0; i < m + 1; i++) 22 | dp[n][i] = 0; 23 | for(int i = 0; i < n + 1; i++) 24 | dp[i][m] = 0; 25 | 26 | for(int i = n - 1; i > -1; i--){ 27 | for(int j = m - 1; j > -1; j--){ 28 | if(text1[i] == text2[j]){ 29 | dp[i][j] = 1 + dp[i + 1][j + 1]; 30 | } 31 | else{ 32 | dp[i][j] = max(dp[i + 1][j], dp[i][j + 1]); 33 | } 34 | } 35 | } 36 | 37 | return dp[0][0]; 38 | } 39 | }; -------------------------------------------------------------------------------- /2024/01 - January/26-01-2024.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 26/01/2024 5 | Problem : Out of Boundary Paths 6 | Difficulty : Medium 7 | Problem Link : https://leetcode.com/problems/out-of-boundary-paths/description/ 8 | Video Solution : https://youtu.be/ogD8C8FL0RA 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | int findPaths(int n, int m, int maxMove, int startRow, int startColumn) { 16 | long long dp[n][m][maxMove + 1]; 17 | int dx[] = {0, 0, -1, 1}; 18 | int dy[] = {-1, 1, 0, 0}; 19 | 20 | const int mod = 1e9 + 7; 21 | 22 | for(int i = 0; i < n; i++){ 23 | for(int j = 0; j < m; j++) 24 | dp[i][j][0] = 0; 25 | } 26 | 27 | auto inside = [&](int x, int y) -> bool { 28 | if(x < 0 or x >= n or y < 0 or y >= m) 29 | return 0; 30 | 31 | return 1; 32 | }; 33 | 34 | for(int k = 1; k < maxMove + 1; k++){ 35 | for(int i = 0; i < n; i++){ 36 | for(int j = 0; j < m; j++){ 37 | dp[i][j][k] = 0; 38 | 39 | for(int d = 0; d < 4; d++){ 40 | int nx = i + dx[d]; 41 | int ny = j + dy[d]; 42 | 43 | if(inside(nx, ny)){ 44 | dp[i][j][k] = (dp[i][j][k] + dp[nx][ny][k - 1]) % mod; 45 | } 46 | else{ 47 | dp[i][j][k] = (dp[i][j][k] + 1) % mod; 48 | } 49 | } 50 | } 51 | } 52 | } 53 | 54 | return dp[startRow][startColumn][maxMove]; 55 | } 56 | }; -------------------------------------------------------------------------------- /2024/01 - January/27-01-2024.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 27/01/2024 5 | Problem : K Inverse Pairs Array 6 | Difficulty : Hard 7 | Problem Link : https://leetcode.com/problems/k-inverse-pairs-array/description/ 8 | Video Solution : https://youtu.be/-O-YUrcrvH4 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | int kInversePairs(int n, int k) { 16 | const long long mod = 1e9 + 7; 17 | 18 | vector> dp(n + 1, vector (k + 1, 1)); 19 | 20 | for(int i = 1; i < n + 1; i++){ 21 | for(int j = 1; j < k + 1; j++){ 22 | int MAX = j; 23 | int MIN = j - (i - 1); 24 | 25 | dp[i][j] = (dp[i][j - 1] + (dp[i - 1][MAX] - (MIN > 0 ? dp[i - 1][MIN - 1] : 0) + mod)) % mod; 26 | } 27 | } 28 | 29 | return (dp[n][k] - (k > 0 ? dp[n][k - 1] : 0) + mod) % mod; 30 | } 31 | }; -------------------------------------------------------------------------------- /2024/01 - January/28-01-2024.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 28/01/2024 5 | Problem : Number of Submatrices That Sum to Target 6 | Difficulty : Hard 7 | Problem Link : https://leetcode.com/problems/number-of-submatrices-that-sum-to-target/description/ 8 | Video Solution : https://youtu.be/djZOhN4_ABw 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | int numSubmatrixSumTarget(vector>& matrix, int target) { 16 | int n = matrix.size(); 17 | int m = matrix[0].size(); 18 | 19 | vector> pre(n, vector (m, 0)); 20 | 21 | for(int i = 0; i < n; i++){ 22 | for(int j = 0; j < m; j++){ 23 | pre[i][j] = matrix[i][j]; 24 | 25 | if(i > 0) 26 | pre[i][j] += pre[i - 1][j]; 27 | if(j > 0) 28 | pre[i][j] += pre[i][j - 1]; 29 | if(i > 0 and j > 0) 30 | pre[i][j] -= pre[i - 1][j - 1]; 31 | } 32 | } 33 | 34 | int ans = 0; 35 | 36 | for(int sx = 0; sx < n; sx++){ 37 | for(int ex = sx; ex < n; ex++){ 38 | for(int sy = 0; sy < m; sy++){ 39 | for(int ey = sy; ey < m; ey++){ 40 | int cur = pre[ex][ey]; 41 | 42 | if(sy > 0) 43 | cur -= pre[ex][sy - 1]; 44 | if(sx > 0) 45 | cur -= pre[sx - 1][ey]; 46 | if(sx > 0 and sy > 0) 47 | cur += pre[sx - 1][sy - 1]; 48 | 49 | ans += cur == target; 50 | } 51 | } 52 | } 53 | } 54 | 55 | return ans; 56 | } 57 | }; -------------------------------------------------------------------------------- /2024/01 - January/29-01-2024.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 29/01/2024 5 | Problem : Implement Queue using Stacks 6 | Difficulty : Easy 7 | Problem Link : https://leetcode.com/problems/implement-queue-using-stacks/description/ 8 | Video Solution : NA 9 | 10 | */ 11 | 12 | 13 | class MyQueue { 14 | public: 15 | stack s1, s2; 16 | MyQueue() { 17 | 18 | } 19 | 20 | void push(int x) { 21 | while(s1.size()){ 22 | s2.push(s1.top()); 23 | s1.pop(); 24 | } 25 | 26 | s1.push(x); 27 | 28 | while(s2.size()){ 29 | s1.push(s2.top()); 30 | s2.pop(); 31 | } 32 | } 33 | 34 | int pop() { 35 | int to_return = s1.top(); 36 | s1.pop(); 37 | 38 | return to_return; 39 | } 40 | 41 | int peek() { 42 | return s1.top(); 43 | } 44 | 45 | bool empty() { 46 | return s1.empty(); 47 | } 48 | }; 49 | 50 | /** 51 | * Your MyQueue object will be instantiated and called as such: 52 | * MyQueue* obj = new MyQueue(); 53 | * obj->push(x); 54 | * int param_2 = obj->pop(); 55 | * int param_3 = obj->peek(); 56 | * bool param_4 = obj->empty(); 57 | */ -------------------------------------------------------------------------------- /2024/01 - January/30-01-2024.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 30/01/2024 5 | Problem : Evaluate Reverse Polish Notation 6 | Difficulty : Medium 7 | Problem Link : https://leetcode.com/problems/evaluate-reverse-polish-notation/description/ 8 | Video Solution : NA 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | int evalRPN(vector& tokens) { 16 | stack val; 17 | 18 | for(auto i : tokens){ 19 | if(i == "+" or i == "-" or i == "*" or i == "/"){ 20 | int y = val.top(); 21 | val.pop(); 22 | int x = val.top(); 23 | val.pop(); 24 | 25 | if(i == "+") 26 | val.push(x + y); 27 | else if(i == "-") 28 | val.push(x - y); 29 | else if(i == "*") 30 | val.push(x * y); 31 | else 32 | val.push(x / y); 33 | } 34 | else{ 35 | val.push(stoi(i)); 36 | } 37 | } 38 | 39 | return val.top(); 40 | } 41 | }; -------------------------------------------------------------------------------- /2024/01 - January/31-01-2024.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 31/01/2024 5 | Problem : Daily Temperatures 6 | Difficulty : Medium 7 | Problem Link : https://leetcode.com/problems/daily-temperatures/ 8 | Video Solution : https://youtu.be/g4VZNMAussM 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | vector dailyTemperatures(vector& temperatures) { 16 | stack> st; 17 | 18 | int n = temperatures.size(); 19 | 20 | vector ans(n, 0); 21 | 22 | for(int i = n - 1; i > -1; i--){ 23 | while(st.size() and st.top().first <= temperatures[i]) 24 | st.pop(); 25 | 26 | if(st.size() and st.top().first > temperatures[i]) 27 | ans[i] = st.top().second - i; 28 | 29 | st.push({temperatures[i], i}); 30 | } 31 | 32 | return ans; 33 | } 34 | }; -------------------------------------------------------------------------------- /2024/02 - February/01-02-2024.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 01/02/2024 5 | Problem : Divide Array Into Arrays With Max Difference 6 | Difficulty : Medium 7 | Problem Link : https://leetcode.com/problems/divide-array-into-arrays-with-max-difference/description/ 8 | Video Solution : https://youtu.be/_a_TUZ8UNd8 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | vector> divideArray(vector& nums, int k) { 16 | sort(nums.begin(), nums.end()); 17 | 18 | vector> ans; 19 | 20 | for(int i = 0; i < nums.size(); i += 3){ 21 | if(nums[i + 2] - nums[i] > k){ 22 | ans.clear(); 23 | return ans; 24 | } 25 | 26 | ans.push_back({nums[i], nums[i + 1], nums[i + 2]}); 27 | } 28 | 29 | return ans; 30 | } 31 | }; -------------------------------------------------------------------------------- /2024/02 - February/02-02-2024.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 02/02/2024 5 | Problem : Sequential Digits 6 | Difficulty : Medium 7 | Problem Link : https://leetcode.com/problems/sequential-digits/description/ 8 | Video Solution : https://youtu.be/RYLA6hDQ7lA 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | vector sequentialDigits(int low, int high) { 16 | vector ans; 17 | 18 | for(int start = 1; start < 10; start++){ 19 | int cur = start + 1; 20 | int val = start; 21 | 22 | if(val >= low and val <= high) 23 | ans.push_back(val); 24 | 25 | for(; cur < 10; cur++){ 26 | val *= 10; 27 | val += cur; 28 | 29 | 30 | if(val >= low and val <= high) 31 | ans.push_back(val); 32 | } 33 | } 34 | 35 | sort(ans.begin(), ans.end()); 36 | 37 | return ans; 38 | } 39 | }; -------------------------------------------------------------------------------- /2024/02 - February/03-02-2024.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 03/02/2024 5 | Problem : Partition Array for Maximum Sum 6 | Difficulty : Medium 7 | Problem Link : https://leetcode.com/problems/partition-array-for-maximum-sum/description/ 8 | Video Solution : https://youtu.be/Ndrs061tocs 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | int maxSumAfterPartitioning(vector& arr, int k) { 16 | int n = arr.size(); 17 | 18 | vector dp(n + 1, 0); 19 | 20 | for(int i = n - 1; i > -1; i--){ 21 | long long MAX = 0; 22 | 23 | for(int j = i; j < min(i + k, n); j++){ 24 | MAX = max(MAX, (long long)arr[j]); 25 | 26 | dp[i] = max(dp[i], (j - i + 1) * MAX + dp[j + 1]); 27 | } 28 | } 29 | 30 | return dp[0]; 31 | } 32 | }; -------------------------------------------------------------------------------- /2024/02 - February/04-02-2024.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 04/02/2024 5 | Problem : Minimum Window Substring 6 | Difficulty : Hard 7 | Problem Link : https://leetcode.com/problems/minimum-window-substring/description/ 8 | Video Solution : https://youtu.be/QayR_0AdzT4 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | string minWindow(string s, string t) { 16 | int best = 1e9, start = 0; 17 | int l = 0, r = 0; 18 | 19 | unordered_map fs, ft; 20 | 21 | for(auto i : t) 22 | ++ft[i]; 23 | 24 | while(r < s.size()){ 25 | ++fs[s[r++]]; 26 | 27 | while(1){ 28 | bool ok = 1; 29 | 30 | for(auto x : ft){ 31 | if(fs[x.first] < x.second){ 32 | ok = 0; 33 | break; 34 | } 35 | } 36 | 37 | if(!ok) 38 | break; 39 | 40 | if(r - l < best){ 41 | best = r - l; 42 | start = l; 43 | } 44 | 45 | --fs[s[l++]]; 46 | } 47 | } 48 | 49 | if(best == 1e9) 50 | return ""; 51 | else 52 | return s.substr(start, best); 53 | } 54 | }; -------------------------------------------------------------------------------- /2024/02 - February/05-02-2024.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 05/02/2024 5 | Problem : First Unique Character in a String 6 | Difficulty : Easy 7 | Problem Link : https://leetcode.com/problems/first-unique-character-in-a-string/ 8 | Video Solution : https://youtu.be/tXCJ9kF8-hw 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | int firstUniqChar(string s) { 16 | vector f(26, 0); 17 | 18 | for(auto i : s) 19 | ++f[i - 'a']; 20 | 21 | for(int i = 0; i < s.size(); i++){ 22 | if(f[s[i] - 'a'] == 1) 23 | return i; 24 | } 25 | 26 | return -1; 27 | } 28 | }; -------------------------------------------------------------------------------- /2024/02 - February/06-02-2024.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 06/02/2024 5 | Problem : Group Anagrams 6 | Difficulty : Medium 7 | Problem Link : https://leetcode.com/problems/group-anagrams/description/ 8 | Video Solution : https://youtu.be/8RxBQxBQyho 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | vector> groupAnagrams(vector& strs) { 16 | map, vector> grp; 17 | 18 | for(int i = 0; i < strs.size(); i++){ 19 | vector f(26, 0); 20 | 21 | for(auto j : strs[i]) 22 | ++f[j - 'a']; 23 | 24 | grp[f].push_back(strs[i]); 25 | } 26 | 27 | vector> ans; 28 | 29 | for(auto i : grp) 30 | ans.emplace_back(i.second); 31 | 32 | return ans; 33 | } 34 | }; -------------------------------------------------------------------------------- /2024/02 - February/07-02-2024.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 07/02/2024 5 | Problem : Sort Characters By Frequency 6 | Difficulty : Medium 7 | Problem Link : https://leetcode.com/problems/sort-characters-by-frequency/description/ 8 | Video Solution : https://youtu.be/M-XCd3QzmuM 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | string frequencySort(string s) { 16 | map f; 17 | for(auto i : s) 18 | ++f[i]; 19 | 20 | priority_queue> pq; 21 | 22 | for(auto i : f){ 23 | pq.push({i.second, i.first}); 24 | } 25 | 26 | string ans = ""; 27 | while(!pq.empty()){ 28 | int count; 29 | char c; 30 | tie(count, c) = pq.top(); 31 | pq.pop(); 32 | 33 | while(count--) 34 | ans += c; 35 | } 36 | 37 | return ans; 38 | } 39 | }; -------------------------------------------------------------------------------- /2024/02 - February/08-02-2024.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 08/02/2024 5 | Problem : Perfect Squares 6 | Difficulty : Medium 7 | Problem Link : https://leetcode.com/problems/perfect-squares/description/ 8 | Video Solution : https://youtu.be/8zPDyIUG90Y 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | int numSquares(int n) { 16 | const int inf = 1e9; 17 | vector> dp(102, vector (n + 1, inf)); 18 | 19 | for(int i = 0; i < 102; i++) 20 | dp[i][0] = 0; 21 | 22 | for(int i = 100; i > -1; i--){ 23 | for(int j = 0; j < n + 1; j++){ 24 | int take = inf, notake = inf; 25 | 26 | notake = dp[i + 1][j]; 27 | 28 | if(i * i <= j) 29 | take = 1 + dp[i][j - i * i]; 30 | 31 | dp[i][j] = min(take, notake); 32 | } 33 | } 34 | 35 | return dp[0][n]; 36 | } 37 | }; -------------------------------------------------------------------------------- /2024/02 - February/09-02-2024.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 09/02/2024 5 | Problem : Largest Divisible Subset 6 | Difficulty : Medium 7 | Problem Link : https://leetcode.com/problems/largest-divisible-subset/description/ 8 | Video Solution : https://youtu.be/nFJ5p7Gz1hM 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | vector largestDivisibleSubset(vector& nums) { 16 | int n = nums.size(); 17 | sort(nums.begin(), nums.end()); 18 | 19 | vector> dp(n + 1, vector (2, 0)); 20 | 21 | for(int i = n - 1; i > -1; i--){ 22 | dp[i][0] = 1; 23 | dp[i][1] = i; 24 | 25 | for(int j = i + 1; j < n; j++){ 26 | if(nums[j] % nums[i] == 0 and 1 + dp[j][0] > dp[i][0]){ 27 | dp[i][0] = 1 + dp[j][0]; 28 | dp[i][1] = j; 29 | } 30 | } 31 | } 32 | 33 | int best = 0; 34 | int start = 0; 35 | 36 | for(int i = 0; i < n; i++){ 37 | if(dp[i][0] > best){ 38 | best = dp[i][0]; 39 | start = i; 40 | } 41 | } 42 | 43 | vector ans; 44 | while(best--){ 45 | ans.push_back(nums[start]); 46 | start = dp[start][1]; 47 | } 48 | 49 | return ans; 50 | } 51 | }; -------------------------------------------------------------------------------- /2024/02 - February/10-02-2024.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 10/02/2024 5 | Problem : Palindromic Substrings 6 | Difficulty : Medium 7 | Problem Link : https://leetcode.com/problems/palindromic-substrings/description/ 8 | Video Solution : https://youtu.be/bDz5_ap-fl8 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | int countSubstrings(string s) { 16 | int n = s.size(); 17 | 18 | vector> isPalindrome(n, vector (n, 0)); 19 | int ans = 0; 20 | 21 | for(int i = n - 1; i > -1; i--){ 22 | for(int j = i; j < n; j++){ 23 | if(i == j){ 24 | isPalindrome[i][j] = 1; 25 | ++ans; 26 | } 27 | else{ 28 | if(s[i] == s[j] and (i == j - 1 or isPalindrome[i + 1][j - 1])){ 29 | isPalindrome[i][j] = 1; 30 | ++ans; 31 | } 32 | } 33 | } 34 | } 35 | 36 | return ans; 37 | } 38 | }; -------------------------------------------------------------------------------- /2024/02 - February/11-02-2024.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 11/02/2024 5 | Problem : Cherry Pickup II 6 | Difficulty : Hard 7 | Problem Link : https://leetcode.com/problems/cherry-pickup-ii/description/ 8 | Video Solution : https://youtu.be/8D1mZ4lX8PI 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | int cherryPickup(vector>& grid) { 16 | int n = grid.size(); 17 | int m = grid[0].size(); 18 | 19 | int dp[n][m][m]; 20 | 21 | for(int i = n - 1; i > -1; i--){ 22 | for(int x = 0; x < m; x++){ 23 | for(int y = 0; y < m; y++){ 24 | dp[i][x][y] = 0; 25 | 26 | for(int p1 = -1; p1 < 2; p1++){ 27 | for(int p2 = -1; p2 < 2; p2++){ 28 | if(x + p1 > -1 and x + p1 < m and y + p2 > -1 and y + p2 < m){ 29 | int cur = grid[i][x]; 30 | 31 | if(x != y) 32 | cur += grid[i][y]; 33 | 34 | if(i + 1 < n){ 35 | cur += dp[i + 1][x + p1][y + p2]; 36 | } 37 | 38 | dp[i][x][y] = max(dp[i][x][y], cur); 39 | } 40 | } 41 | } 42 | } 43 | } 44 | } 45 | 46 | return dp[0][0][m - 1]; 47 | } 48 | }; 49 | Difficulty : Hard -------------------------------------------------------------------------------- /2024/02 - February/12-02-2024.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 12/02/2024 5 | Problem : Majority Element 6 | Difficulty : Easy 7 | Problem Link : https://leetcode.com/problems/majority-element/description/ 8 | Video Solution : https://youtu.be/CDBFHIhjHBU 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | int majorityElement(vector& nums) { 16 | unordered_map f; 17 | 18 | for(auto i : nums){ 19 | ++f[i]; 20 | } 21 | 22 | for(auto i : f){ 23 | if(i.second > nums.size() / 2){ 24 | return i.first; 25 | } 26 | } 27 | 28 | return 0; 29 | } 30 | };f -------------------------------------------------------------------------------- /2024/02 - February/13-02-2024.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 13/02/2024 5 | Problem : Find First Palindromic String in the Array 6 | Difficulty : Easy 7 | Problem Link : https://leetcode.com/problems/find-first-palindromic-string-in-the-array/description/ 8 | Video Solution : https://youtu.be/klls5_9MYYs 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | string firstPalindrome(vector& words) { 16 | for(auto s : words){ 17 | bool ok = 1; 18 | int n = s.size(); 19 | for(int i = 0; i < n / 2; i++) 20 | if(s[i] != s[n - i - 1]){ 21 | ok = 0; 22 | break; 23 | } 24 | 25 | if(ok) 26 | return s; 27 | } 28 | 29 | return ""; 30 | } 31 | }; -------------------------------------------------------------------------------- /2024/02 - February/14-02-2024.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 14/02/2024 5 | Problem : Rearrange Array Elements by Sign 6 | Difficulty : Medium 7 | Problem Link : https://leetcode.com/problems/rearrange-array-elements-by-sign/description/ 8 | Video Solution : https://youtu.be/t3-6EpHhFrY 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | vector rearrangeArray(vector& nums) { 16 | int i = 0, j = 1; 17 | int n = nums.size(); 18 | 19 | vector ans(n); 20 | 21 | for(int p = 0; p < n; p++){ 22 | if(nums[p] < 0){ 23 | ans[j] = nums[p]; 24 | j += 2; 25 | } 26 | else{ 27 | ans[i] = nums[p]; 28 | i += 2; 29 | } 30 | } 31 | 32 | return ans; 33 | } 34 | }; -------------------------------------------------------------------------------- /2024/02 - February/15-02-2024.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 15/02/2024 5 | Problem : Find Polygon With the Largest Perimeter 6 | Difficulty : Medium 7 | Problem Link : https://leetcode.com/problems/find-polygon-with-the-largest-perimeter/description/ 8 | Video Solution : https://youtu.be/a_Ibszsz2vU 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | long long largestPerimeter(vector& nums) { 16 | sort(nums.begin(), nums.end()); 17 | 18 | int n = nums.size(); 19 | 20 | long long ans = -1; 21 | long long pre = nums[0] + nums[1]; 22 | 23 | for(int i = 2; i < n; i++){ 24 | if(pre > nums[i]){ 25 | ans = max(ans, pre + nums[i]); 26 | } 27 | 28 | pre += nums[i]; 29 | } 30 | 31 | return ans; 32 | } 33 | }; -------------------------------------------------------------------------------- /2024/02 - February/16-02-2024.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 16/02/2024 5 | Problem : Least Number of Unique Integers after K Removals 6 | Difficulty : Medium 7 | Problem Link : https://leetcode.com/problems/least-number-of-unique-integers-after-k-removals/description/ 8 | Video Solution : https://youtu.be/cXQtnEQfvC8 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | int findLeastNumOfUniqueInts(vector& arr, int k) { 16 | map f; 17 | 18 | for(auto i : arr) 19 | ++f[i]; 20 | 21 | vector values; 22 | for(auto i : f) 23 | values.push_back(i.second); 24 | 25 | sort(values.begin(), values.end()); 26 | 27 | int ans = values.size(); 28 | 29 | for(auto i : values){ 30 | if(i <= k){ 31 | k -= i; 32 | --ans; 33 | } 34 | else 35 | break; 36 | } 37 | 38 | return ans; 39 | } 40 | }; -------------------------------------------------------------------------------- /2024/02 - February/17-02-2024.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 17/02/2024 5 | Problem : Furthest Building You Can Reach 6 | Difficulty : Medium 7 | Problem Link : https://leetcode.com/problems/furthest-building-you-can-reach/description/ 8 | Video Solution : https://youtu.be/9sKP5QBCAa4 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | int furthestBuilding(vector& heights, int bricks, int ladders) { 16 | int n = heights.size(); 17 | 18 | int low = 0; 19 | int high = n; 20 | 21 | auto check = [&](int p) -> bool { 22 | int rem_brick = bricks; 23 | vector need; 24 | 25 | for(int i = 0; i < p; i++){ 26 | if(heights[i + 1] > heights[i]) 27 | need.push_back(heights[i + 1] - heights[i]); 28 | } 29 | 30 | sort(need.begin(), need.end(), [](int a, int b){ 31 | return a > b; 32 | }); 33 | 34 | while(need.size() and rem_brick >= need.back()){ 35 | rem_brick -= need.back(); 36 | need.pop_back(); 37 | } 38 | 39 | return ladders >= need.size(); 40 | }; 41 | 42 | while(low < high - 1){ 43 | int mid = low + (high - low) / 2; 44 | 45 | if(check(mid)) 46 | low = mid; 47 | else 48 | high = mid; 49 | } 50 | 51 | return low; 52 | } 53 | }; -------------------------------------------------------------------------------- /2024/02 - February/18-02-2024.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 18/02/2024 5 | Problem : Meeting Rooms III 6 | Difficulty : Hard 7 | Problem Link : https://leetcode.com/problems/meeting-rooms-iii/description/ 8 | Video Solution : https://youtu.be/7h047ZRAWhs 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | int mostBooked(int n, vector>& meetings) { 16 | sort(meetings.begin(), meetings.end()); 17 | vector ans(n, 0); 18 | 19 | priority_queue, vector>, greater>> pq; 20 | priority_queue, greater> pq2; 21 | for(int i = 0; i < n; i++) 22 | pq.push({0, i}); 23 | 24 | long long time = 0; 25 | for(int i = 0; i < meetings.size(); i++){ 26 | time = max(time, (long long)meetings[i][0]); 27 | 28 | while(pq.size() and time >= pq.top().first){ 29 | pq2.push(pq.top().second); 30 | pq.pop(); 31 | } 32 | 33 | if(pq2.empty()){ 34 | time = pq.top().first; 35 | while(pq.size() and time >= pq.top().first){ 36 | pq2.push(pq.top().second); 37 | pq.pop(); 38 | } 39 | } 40 | 41 | int meet = pq2.top(); 42 | pq2.pop(); 43 | 44 | ++ans[meet]; 45 | pq.push({time + meetings[i][1] - meetings[i][0], meet}); 46 | } 47 | 48 | int room = 0; 49 | int MAX = 0; 50 | 51 | for(int i = 0; i < n; i++){ 52 | if(ans[i] > MAX){ 53 | MAX = ans[i]; 54 | room = i; 55 | } 56 | } 57 | 58 | return room; 59 | } 60 | }; -------------------------------------------------------------------------------- /2024/02 - February/19-02-2024.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 19/02/2024 5 | Problem : Power of Two 6 | Difficulty : Easy 7 | Problem Link : https://leetcode.com/problems/power-of-two/description/ 8 | Video Solution : NA 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | bool isPowerOfTwo(int n) { 16 | return n and !(n & (n - 1LL)); 17 | } 18 | }; -------------------------------------------------------------------------------- /2024/02 - February/20-02-2024.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 20/02/2024 5 | Problem : Missing Number 6 | Difficulty : Easy 7 | Problem Link : https://leetcode.com/problems/missing-number/description/?envType=daily-question&envId=2024-02-20 8 | Video Solution : NA 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | int missingNumber(vector& nums) { 16 | int sum = nums.size(); 17 | sum = (sum * (sum + 1)) / 2; 18 | 19 | for(auto i : nums) 20 | sum -= i; 21 | 22 | return sum; 23 | } 24 | }; -------------------------------------------------------------------------------- /2024/02 - February/21-02-2024.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 21/02/2024 5 | Problem : Bitwise AND of Numbers Range 6 | Difficulty : Medium 7 | Problem Link : https://leetcode.com/problems/bitwise-and-of-numbers-range/description/ 8 | Video Solution : NA 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | int rangeBitwiseAnd(int left, int right) { 16 | long long cur = 0; 17 | long long ans = 0; 18 | 19 | for(int i = 0; i < 31; i++){ 20 | if((left >> i) & 1){ 21 | int dx = right - left; 22 | int poss = (1 << i) - cur - 1; 23 | 24 | if(dx <= poss) 25 | ans |= (1 << i); 26 | 27 | cur |= (1 << i); 28 | } 29 | } 30 | 31 | return ans; 32 | } 33 | }; -------------------------------------------------------------------------------- /2024/02 - February/22-02-2024.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 22/02/2024 5 | Problem : Find the Town Judge 6 | Difficulty : Easy 7 | Problem Link : https://leetcode.com/problems/find-the-town-judge/description/ 8 | Video Solution : NA 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | int findJudge(int n, vector>& trust) { 16 | vector ans(n, 0); 17 | 18 | for(auto i : trust) { 19 | ans[i[0] - 1] = -1; 20 | 21 | if(ans[i[1] - 1] > -1) 22 | ++ans[i[1] - 1]; 23 | } 24 | 25 | for(int i = 0; i < n; i++){ 26 | if(ans[i] == n - 1) 27 | return i + 1; 28 | } 29 | 30 | return -1; 31 | } 32 | }; 33 | -------------------------------------------------------------------------------- /2024/02 - February/23-02-2024.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 23/02/2024 5 | Problem : Cheapest Flights Within K Stops 6 | Difficulty : Medium 7 | Problem Link : https://leetcode.com/problems/cheapest-flights-within-k-stops/description/ 8 | Video Solution : NA 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | int findCheapestPrice(int n, vector>& flights, int src, int dst, int k) { 16 | const int inf = 1e9; 17 | vector> d(n, vector (k + 2, inf)); 18 | 19 | vector>> g(n); 20 | for(auto i : flights) { 21 | g[i[0]].emplace_back(i[1], i[2]); 22 | } 23 | 24 | d[src][0] = 0; 25 | priority_queue>, vector>>, greater>>> pq; 26 | pq.push({0, {src, 0}}); 27 | 28 | while(!pq.empty()) { 29 | int cd = pq.top().first; 30 | int node, ck; 31 | tie(node, ck) = pq.top().second; 32 | 33 | pq.pop(); 34 | 35 | if(ck == k + 1) 36 | continue; 37 | 38 | for(auto next : g[node]) { 39 | int child, w; 40 | tie(child, w) = next; 41 | 42 | if(cd + w < d[child][ck + 1]) { 43 | d[child][ck + 1] = cd + w; 44 | pq.push({d[child][ck + 1], {child, ck + 1}}); 45 | } 46 | } 47 | } 48 | 49 | int ans = *min_element(d[dst].begin(), d[dst].end()); 50 | 51 | return ans == 1e9 ? -1 : ans; 52 | } 53 | }; 54 | -------------------------------------------------------------------------------- /2024/02 - February/24-02-2024.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 24/02/2024 5 | Problem : Find All People With Secret 6 | Difficulty : Hard 7 | Problem Link : https://leetcode.com/problems/find-all-people-with-secret/description/ 8 | Video Solution : NA 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | vector findAllPeople(int n, vector>& meetings, int firstPerson) { 16 | vector ans(n + 1, 1e9); 17 | ans[0] = 0; 18 | ans[firstPerson] = 0; 19 | 20 | vector>> g(n + 1); 21 | 22 | for(auto i : meetings) { 23 | g[i[0]].emplace_back(i[1], i[2]); 24 | g[i[1]].emplace_back(i[0], i[2]); 25 | } 26 | 27 | priority_queue, vector>, greater>> pq; 28 | pq.push({0, firstPerson}); 29 | pq.push({0, 0}); 30 | 31 | while(!pq.empty()) { 32 | int node, time; 33 | tie(time, node) = pq.top(); 34 | pq.pop(); 35 | 36 | for(auto next : g[node]) { 37 | int child, ntime; 38 | tie(child, ntime) = next; 39 | 40 | if(ntime >= time and ans[child] > ntime) { 41 | ans[child] = ntime; 42 | pq.push({ntime, child}); 43 | } 44 | } 45 | } 46 | 47 | vector res; 48 | 49 | for(int i = 0; i < n + 1; i++) 50 | if(ans[i] < 1e9) 51 | res.push_back(i); 52 | 53 | return res; 54 | } 55 | }; 56 | -------------------------------------------------------------------------------- /2024/02 - February/25-02-2024.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 25/02/2024 5 | Problem : Greatest Common Divisor Traversal 6 | Difficulty : Hard 7 | Problem Link : https://leetcode.com/problems/greatest-common-divisor-traversal/description/ 8 | Video Solution : NA 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | bool canTraverseAllPairs(vector& nums) { 16 | int n = nums.size(); 17 | vector> g(n); 18 | 19 | int MAX = *max_element(nums.begin(), nums.end()) + 1; 20 | vector> pos(MAX); 21 | 22 | for(int i = 0; i < nums.size(); i++) 23 | pos[nums[i]].push_back(i); 24 | 25 | for(int i = 2; i < MAX; i++) { 26 | vector cur; 27 | 28 | for(int j = i; j < MAX; j += i) 29 | for(auto x : pos[j]) 30 | cur.push_back(x); 31 | 32 | for(int j = 1; j < cur.size(); j++){ 33 | g[cur[j]].push_back(cur[j - 1]); 34 | g[cur[j - 1]].push_back(cur[j]); 35 | } 36 | } 37 | 38 | vector vis(n, 0); 39 | int cc = 0; 40 | 41 | function dfs = [&](int node) { 42 | vis[node] = 1; 43 | 44 | for(auto child : g[node]){ 45 | if(!vis[child]) 46 | dfs(child); 47 | } 48 | }; 49 | 50 | for(int i = 0; i < n; i++) { 51 | if(!vis[i]){ 52 | ++cc; 53 | dfs(i); 54 | } 55 | } 56 | 57 | return cc == 1; 58 | } 59 | }; 60 | -------------------------------------------------------------------------------- /2024/02 - February/26-02-2024.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 26/02/2024 5 | Problem : Same Tree 6 | Difficulty : Easy 7 | Problem Link : https://leetcode.com/problems/same-tree/description/ 8 | Video Solution : NA 9 | 10 | */ 11 | 12 | 13 | /** 14 | * Definition for a binary tree node. 15 | * struct TreeNode { 16 | * int val; 17 | * TreeNode *left; 18 | * TreeNode *right; 19 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 20 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 21 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 22 | * }; 23 | */ 24 | class Solution { 25 | public: 26 | bool isSameTree(TreeNode* p, TreeNode* q) { 27 | function dfs = [&](TreeNode * x, TreeNode * y) -> bool { 28 | if((x == nullptr) ^ (y == nullptr)) 29 | return 0; 30 | 31 | if(!x) 32 | return 1; 33 | 34 | if(x -> val != y -> val) 35 | return 0; 36 | 37 | return dfs(x -> left, y -> left) and dfs(x -> right, y -> right); 38 | }; 39 | 40 | return dfs(p, q); 41 | } 42 | }; 43 | -------------------------------------------------------------------------------- /2024/02 - February/27-02-2024.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 27/02/2024 5 | Problem : Diameter of Binary Tree 6 | Difficulty : Easy 7 | Problem Link : https://leetcode.com/problems/diameter-of-binary-tree/ 8 | Video Solution : NA 9 | 10 | */ 11 | 12 | 13 | /** 14 | * Definition for a binary tree node. 15 | * struct TreeNode { 16 | * int val; 17 | * TreeNode *left; 18 | * TreeNode *right; 19 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 20 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 21 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 22 | * }; 23 | */ 24 | class Solution { 25 | public: 26 | int diameterOfBinaryTree(TreeNode* root) { 27 | int ans = 0; 28 | 29 | function dfs = [&](TreeNode * node) -> int { 30 | if(!node) 31 | return 0; 32 | 33 | int left = dfs(node -> left); 34 | int right = dfs(node -> right); 35 | 36 | ans = max(ans, left + right); 37 | 38 | return max(left, right) + 1; 39 | }; 40 | 41 | dfs(root); 42 | 43 | return ans; 44 | } 45 | }; 46 | -------------------------------------------------------------------------------- /2024/02 - February/28-02-2024.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 28/02/2024 5 | Problem : Find Bottom Left Tree Value 6 | Difficulty : Medium 7 | Problem Link : https://leetcode.com/problems/find-bottom-left-tree-value/description/ 8 | Video Solution : NA 9 | 10 | */ 11 | 12 | 13 | /** 14 | * Definition for a binary tree node. 15 | * struct TreeNode { 16 | * int val; 17 | * TreeNode *left; 18 | * TreeNode *right; 19 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 20 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 21 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 22 | * }; 23 | */ 24 | class Solution { 25 | public: 26 | int findBottomLeftValue(TreeNode* root) { 27 | int d = 0; 28 | int ans = root -> val; 29 | 30 | function dfs = [&](TreeNode * node, int cd) { 31 | if(!node) 32 | return; 33 | 34 | if(cd > d) { 35 | d = cd; 36 | ans = node -> val; 37 | } 38 | 39 | dfs(node -> left, cd + 1); 40 | dfs(node -> right, cd + 1); 41 | }; 42 | 43 | dfs(root, 0); 44 | 45 | return ans; 46 | } 47 | }; 48 | -------------------------------------------------------------------------------- /2024/02 - February/29-02-2024.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 29/02/2024 5 | Problem : Even Odd Tree 6 | Difficulty : Medium 7 | Problem Link : https://leetcode.com/problems/even-odd-tree/description/ 8 | Video Solution : NA 9 | 10 | */ 11 | 12 | 13 | /** 14 | * Definition for a binary tree node. 15 | * struct TreeNode { 16 | * int val; 17 | * TreeNode *left; 18 | * TreeNode *right; 19 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 20 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 21 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 22 | * }; 23 | */ 24 | class Solution { 25 | public: 26 | bool isEvenOddTree(TreeNode* root) { 27 | if (root == nullptr) 28 | return false; 29 | 30 | queue q; 31 | q.push(root); 32 | int level = -1; 33 | while (!q.empty()) { 34 | level++; 35 | int n = q.size(); 36 | int prev ; 37 | if(level % 2 == 0) { 38 | prev=INT_MIN; 39 | } 40 | else { 41 | prev=INT_MAX; 42 | } 43 | for (int i = 0; i < n; i++) { 44 | TreeNode* ptr = q.front(); 45 | q.pop(); 46 | int val = ptr->val; 47 | if ((level % 2 == 0 && (val % 2 == 0 || val <= prev)) || 48 | (level % 2 != 0 && (val % 2 != 0 || val >= prev))) { 49 | return false; 50 | } 51 | prev = val; 52 | if (ptr->left) { 53 | q.push(ptr->left); 54 | } 55 | if (ptr->right) { 56 | q.push(ptr->right); 57 | } 58 | } 59 | } 60 | 61 | return true; 62 | } 63 | }; 64 | -------------------------------------------------------------------------------- /2024/03 - March/01-03-2024.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 01/03/2024 5 | Problem : Maximum Odd Binary Number 6 | Difficulty : Easy 7 | Problem Link : https://leetcode.com/problems/maximum-odd-binary-number/description/ 8 | Video Solution : NA 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | string maximumOddBinaryNumber(string s) { 16 | int count = 0; 17 | for(auto i : s) 18 | count += i == '1'; 19 | 20 | --count; 21 | for(int i = 0; i < s.size() - 1; i++){ 22 | if(count){ 23 | s[i] = '1'; 24 | --count; 25 | } 26 | else{ 27 | s[i] = '0'; 28 | } 29 | } 30 | 31 | s.back() = '1'; 32 | 33 | return s; 34 | } 35 | }; 36 | -------------------------------------------------------------------------------- /2024/03 - March/02-03-2024.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 02/03/2024 5 | Problem : Squares of a Sorted Array 6 | Difficulty : Easy 7 | Problem Link : https://leetcode.com/problems/squares-of-a-sorted-array/description/ 8 | Video Solution : NA 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | vector sortedSquares(vector& nums) { 16 | int n = nums.size(); 17 | vector ans(n); 18 | 19 | int i = 0; 20 | int j = n - 1; 21 | int p = n - 1; 22 | 23 | while(i <= j) { 24 | if(abs(nums[i]) > nums[j]){ 25 | ans[p--] = nums[i] * nums[i]; 26 | ++i; 27 | } 28 | else{ 29 | ans[p--] = nums[j] * nums[j]; 30 | --j; 31 | } 32 | } 33 | 34 | return ans; 35 | } 36 | }; 37 | -------------------------------------------------------------------------------- /2024/03 - March/03-03-2024.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 03/03/2024 5 | Problem : Remove Nth Node From End of List 6 | Difficulty : Medium 7 | Problem Link : https://leetcode.com/problems/remove-nth-node-from-end-of-list/description/ 8 | Video Solution : NA 9 | 10 | */ 11 | 12 | 13 | /** 14 | * Definition for singly-linked list. 15 | * struct ListNode { 16 | * int val; 17 | * ListNode *next; 18 | * ListNode() : val(0), next(nullptr) {} 19 | * ListNode(int x) : val(x), next(nullptr) {} 20 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 21 | * }; 22 | */ 23 | class Solution { 24 | public: 25 | ListNode* removeNthFromEnd(ListNode* head, int n) { 26 | int pos = -1; 27 | 28 | function traverse = [&](ListNode * node) { 29 | if(!node) 30 | return; 31 | 32 | traverse(node -> next); 33 | ++pos; 34 | 35 | if(pos == n) { 36 | node -> next = node -> next -> next; 37 | } 38 | }; 39 | 40 | traverse(head); 41 | ++pos; 42 | 43 | return pos == n ? head -> next : head; 44 | } 45 | }; 46 | -------------------------------------------------------------------------------- /2024/03 - March/04-03-2024.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 04/03/2024 5 | Problem : Bag of Tokens 6 | Difficulty : Medium 7 | Problem Link : https://leetcode.com/problems/bag-of-tokens/description/ 8 | Video Solution : NA 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | int bagOfTokensScore(vector& tokens, int power) { 16 | int n = tokens.size(); 17 | int i = 0; 18 | int j = n - 1; 19 | 20 | int sc = 0; 21 | sort(tokens.begin(), tokens.end()); 22 | 23 | while(i <= j) { 24 | if(tokens[i] <= power) { 25 | power -= tokens[i++]; 26 | ++sc; 27 | } 28 | else if(sc > 0 and i < j) { 29 | power += tokens[j--]; 30 | --sc; 31 | } 32 | else{ 33 | break; 34 | } 35 | } 36 | 37 | return sc; 38 | } 39 | }; 40 | -------------------------------------------------------------------------------- /2024/03 - March/05-03-2024.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 05/03/2024 5 | Problem : Minimum Length of String After Deleting Similar Ends 6 | Difficulty : Medium 7 | Problem Link : https://leetcode.com/problems/minimum-length-of-string-after-deleting-similar-ends/description/ 8 | Video Solution : NA 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | int minimumLength(string s) { 16 | int i = 0; 17 | int j = (int)s.size() - 1; 18 | 19 | while(i < j and s[i] == s[j]){ 20 | char x = s[i]; 21 | 22 | while(i < s.size() and s[i] == x) ++i; 23 | while(j > -1 and s[j] == x) --j; 24 | } 25 | 26 | return max(0, j - i + 1); 27 | } 28 | }; 29 | -------------------------------------------------------------------------------- /2024/03 - March/06-03-2024.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 06/03/2024 5 | Problem : Linked List Cycle 6 | Difficulty : Easy 7 | Problem Link : https://leetcode.com/problems/linked-list-cycle/description/ 8 | Video Solution : NA 9 | 10 | */ 11 | 12 | 13 | /** 14 | * Definition for singly-linked list. 15 | * struct ListNode { 16 | * int val; 17 | * ListNode *next; 18 | * ListNode(int x) : val(x), next(NULL) {} 19 | * }; 20 | */ 21 | class Solution { 22 | public: 23 | bool hasCycle(ListNode *head) { 24 | if(!head) 25 | return 0; 26 | 27 | ListNode * slow = head; 28 | ListNode * fast = head -> next; 29 | 30 | while(fast) { 31 | if(fast == slow) 32 | return 1; 33 | 34 | slow = slow -> next; 35 | fast = fast -> next ? (fast -> next) -> next : fast -> next; 36 | } 37 | 38 | return 0; 39 | } 40 | }; 41 | -------------------------------------------------------------------------------- /2024/03 - March/07-03-2024.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Saransh Bangar 4 | Date : 07/03/2024 5 | Problem : Middle of the Linked List 6 | Difficulty : Easy 7 | Problem Link : https://leetcode.com/problems/middle-of-the-linked-list/description/ 8 | Video Solution : NA 9 | 10 | */ 11 | 12 | 13 | /** 14 | * Definition for singly-linked list. 15 | * struct ListNode { 16 | * int val; 17 | * ListNode *next; 18 | * ListNode() : val(0), next(nullptr) {} 19 | * ListNode(int x) : val(x), next(nullptr) {} 20 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 21 | * }; 22 | */ 23 | class Solution { 24 | public: 25 | ListNode* middleNode(ListNode* head) 26 | { 27 | ListNode* temp=head; 28 | int count=0; 29 | while (temp!=nullptr) 30 | { 31 | count++; 32 | temp=temp->next; 33 | } 34 | temp=head; 35 | int ans=count/2; 36 | while (ans) 37 | { 38 | temp=temp->next; 39 | ans--; 40 | } 41 | return temp; 42 | } 43 | }; 44 | -------------------------------------------------------------------------------- /2024/03 - March/08-03-2024.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Saransh Bangar 3 | Date : 08/03/2024 4 | Problem : Count Elements With Maximum Frequency 5 | Difficulty : Easy 6 | Problem Link : https://leetcode.com/problems/count-elements-with-maximum-frequency/description/ 7 | Video Solution : NA 8 | */ 9 | 10 | 11 | class Solution { 12 | public: 13 | int maxFrequencyElements(vector& nums) 14 | { 15 | vector a(101, 0); 16 | 17 | int maxFreq = INT_MIN; 18 | for(int i=0; i& vec, int k) 14 | { 15 | int low=0, high=vec.size()-1; 16 | while (low<=high) 17 | { 18 | int mid=low+((high-low)/2); 19 | if (vec[mid]==k) 20 | return true; 21 | else if (vec[mid]>k) 22 | high=mid-1; 23 | else low=mid+1; 24 | } 25 | return false; 26 | } 27 | int getCommon(vector& nums1, vector& nums2) 28 | { 29 | int ans; 30 | for (int i=0, j=0;inums2[j]) 33 | { 34 | if (func(nums1, nums2[j])) 35 | return nums2[j]; 36 | else j++; 37 | } 38 | else 39 | { 40 | if (func(nums2, nums1[i])) 41 | return nums1[i]; 42 | else i++; 43 | } 44 | } 45 | return -1; 46 | } 47 | }; 48 | -------------------------------------------------------------------------------- /2024/03 - March/10-03-2024.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 10/03/2024 5 | Problem : Intersection of Two Arrays 6 | Difficulty : Easy 7 | Problem Link : https://leetcode.com/problems/intersection-of-two-arrays/description/ 8 | Video Solution : NA 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | vector intersection(vector& nums1, vector& nums2) { 16 | vector x(1001), y(1001); 17 | 18 | for(auto i : nums1) 19 | ++x[i]; 20 | for(auto i : nums2) 21 | ++y[i]; 22 | 23 | vector ans; 24 | 25 | for(int i = 0; i < 1001; i++){ 26 | if(x[i] and y[i]) 27 | ans.push_back(i); 28 | } 29 | 30 | return ans; 31 | } 32 | }; 33 | -------------------------------------------------------------------------------- /2024/03 - March/11-03-2024.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Saransh Bangar 3 | Date : 11/03/2024 4 | Problem : Custom Sort String 5 | Difficulty : Medium 6 | Problem Link : https://leetcode.com/problems/custom-sort-string/description/ 7 | Video Solution : NA 8 | */ 9 | 10 | 11 | class Solution { 12 | public: 13 | string customSortString(string order, string s) 14 | { 15 | unordered_mapmap; 16 | for (int i=0;i prefixSumToNode; 29 | prefixSumToNode[0] = front; 30 | 31 | while (current != nullptr) 32 | { 33 | prefixSum += current->val; 34 | prefixSumToNode[prefixSum] = current; 35 | current = current->next; 36 | } 37 | 38 | prefixSum = 0; 39 | current = front; 40 | 41 | while (current != nullptr) 42 | { 43 | prefixSum += current->val; 44 | current->next = prefixSumToNode[prefixSum]->next; 45 | current = current->next; 46 | } 47 | return front->next; 48 | } 49 | }; 50 | -------------------------------------------------------------------------------- /2024/03 - March/13-03-2024.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Saransh Bangar 3 | Date : 13/03/2024 4 | Problem : Find the Pivot Integer 5 | Difficulty : Easy 6 | Problem Link : https://leetcode.com/problems/find-the-pivot-integer/description/ 7 | Video Solution : NA 8 | */ 9 | 10 | 11 | class Solution { 12 | public: 13 | int pivotInteger(int n) 14 | { 15 | float ans1=sqrt((n*(n+1))/2); 16 | int ans2=int(ans1); 17 | if (ans1-ans2==0) 18 | return ans1; 19 | return -1; 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /2024/03 - March/14-03-2024.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Saransh Bangar 3 | Date : 14/03/2024 4 | Problem : Binary Subarrays With Sum 5 | Difficulty : Medium 6 | Problem Link : https://leetcode.com/problems/binary-subarrays-with-sum/description/ 7 | Video Solution : NA 8 | */ 9 | 10 | 11 | class Solution { 12 | public: 13 | int numSubarraysWithSum(vector& nums, int goal) 14 | { 15 | unordered_mapmap; 16 | int sum=0, count=0; 17 | for (int i : nums) 18 | { 19 | sum+=i; 20 | if (sum==goal) 21 | count++; 22 | if (map.find(sum-goal)!=map.end()) 23 | count+=map[sum-goal]; 24 | map[sum]++; 25 | } 26 | return count; 27 | } 28 | }; 29 | -------------------------------------------------------------------------------- /2024/03 - March/15-03-2024.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 15/03/2024 5 | Problem : Product of Array Except Self 6 | Difficulty : Medium 7 | Problem Link : https://leetcode.com/problems/product-of-array-except-self/description/ 8 | Video Solution : NA 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | vector productExceptSelf(vector& nums) { 16 | unordered_map f; 17 | for(auto i : nums) 18 | ++f[i]; 19 | 20 | int n = nums.size(); 21 | vector ans(n); 22 | 23 | for(int i = 0; i < n; i++){ 24 | ans[i] = 1; 25 | 26 | for(auto x : f) { 27 | ans[i] *= pow(x.first, x.second - (nums[i] == x.first)); 28 | } 29 | } 30 | 31 | return ans; 32 | } 33 | }; 34 | -------------------------------------------------------------------------------- /2024/03 - March/16-03-2024.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 16/03/2024 5 | Problem : Contiguous Array 6 | Difficulty : Medium 7 | Problem Link : https://leetcode.com/problems/contiguous-array/description/ 8 | Video Solution : NA 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | int findMaxLength(vector& nums) { 16 | int n = nums.size(); 17 | vector pre(n); 18 | 19 | pre[0] = nums[0] == 1 ? 1 : -1; 20 | 21 | for(int i = 1; i < n; i++){ 22 | pre[i] = pre[i - 1]; 23 | pre[i] += nums[i] == 1 ? 1 : -1; 24 | } 25 | 26 | unordered_map first; 27 | first[0] = -1; 28 | int ans = 0; 29 | for(int i = 0; i < n; i++){ 30 | if(first.find(pre[i]) != first.end()) 31 | ans = max(ans, i - first[pre[i]]); 32 | else 33 | first[pre[i]] = i; 34 | } 35 | 36 | return ans; 37 | } 38 | }; 39 | -------------------------------------------------------------------------------- /2024/03 - March/17-03-2024.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Saransh Bangar 4 | Date : 17/03/2024 5 | Problem : Insert Interval 6 | Difficulty : Medium 7 | Problem Link : https://leetcode.com/problems/insert-interval/description/ 8 | Video Solution : NA 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | vector> insert(vector>& intervals, vector& newInterval) 16 | { 17 | vector>ans; 18 | int i=0; 19 | while (i>& points) { 16 | sort(points.begin(), points.end(), [](vector &a, vector &b) { 17 | if(a[1] == b[1]) 18 | return a[0] < b[0]; 19 | return a[1] < b[1]; 20 | }); 21 | 22 | int ans = 0; 23 | long long last = -1e18; 24 | 25 | for(auto i : points) { 26 | if(i[0] <= last) continue; 27 | 28 | ++ans; 29 | last = i[1]; 30 | } 31 | 32 | return ans; 33 | } 34 | }; 35 | -------------------------------------------------------------------------------- /2024/03 - March/19-03-2024.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Saransh Bangar 4 | Date : 19/03/2024 5 | Problem : Task Scheduler 6 | Difficulty : Medium 7 | Problem Link : https://leetcode.com/problems/task-scheduler/description/ 8 | Video Solution : NA 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | int leastInterval(vector& tasks, int n) 16 | { 17 | int freq[26]={0}; 18 | for (char &ch : tasks) 19 | freq[ch-'A']++; 20 | 21 | priority_queue pq; 22 | for (int i=0; i < 26; i++) 23 | if (freq[i] > 0) 24 | pq.push(freq[i]); 25 | 26 | int time=0; 27 | while (!pq.empty()) 28 | { 29 | int cycle=n + 1; 30 | vector store; 31 | int taskCount=0; 32 | 33 | while (cycle-- && !pq.empty()) 34 | { 35 | if (pq.top() > 1) 36 | store.push_back(pq.top() - 1); 37 | 38 | pq.pop(); 39 | taskCount++; 40 | } 41 | for (int &x : store) 42 | pq.push(x); 43 | time += (pq.empty() ? taskCount : n + 1); 44 | } 45 | return time; 46 | } 47 | }; 48 | -------------------------------------------------------------------------------- /2024/03 - March/20-03-2024.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 20/03/2024 5 | Problem : Merge In Between Linked Lists 6 | Difficulty : Medium 7 | Problem Link : https://leetcode.com/problems/merge-in-between-linked-lists/description 8 | Video Solution : NA 9 | 10 | */ 11 | 12 | 13 | /** 14 | * Definition for singly-linked list. 15 | * struct ListNode { 16 | * int val; 17 | * ListNode *next; 18 | * ListNode() : val(0), next(nullptr) {} 19 | * ListNode(int x) : val(x), next(nullptr) {} 20 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 21 | * }; 22 | */ 23 | class Solution { 24 | public: 25 | ListNode* mergeInBetween(ListNode* list1, int a, int b, ListNode* list2) { 26 | ListNode * b_head = list2; 27 | ListNode * b_tail = list2; 28 | 29 | while(b_tail -> next != nullptr) 30 | b_tail = b_tail -> next; 31 | 32 | ListNode * cur = list1; 33 | 34 | int index = 0; 35 | 36 | while(index != a - 1) 37 | cur = cur -> next,++index; 38 | 39 | ListNode * last = cur; 40 | cur = cur -> next; 41 | ++index; 42 | last -> next = b_head; 43 | 44 | while(index != b) 45 | cur = cur -> next, ++index; 46 | 47 | b_tail -> next = cur -> next; 48 | 49 | return list1; 50 | } 51 | }; 52 | -------------------------------------------------------------------------------- /2024/03 - March/21-03-2024.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Saransh Bangar 4 | Date : 21/03/2024 5 | Problem : Reverse Linked List 6 | Difficulty : Easy 7 | Problem Link : https://leetcode.com/problems/reverse-linked-list/description/ 8 | Video Solution : NA 9 | 10 | */ 11 | 12 | 13 | /** 14 | * Definition for singly-linked list. 15 | * struct ListNode { 16 | * int val; 17 | * ListNode *next; 18 | * ListNode() : val(0), next(nullptr) {} 19 | * ListNode(int x) : val(x), next(nullptr) {} 20 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 21 | * }; 22 | */ 23 | class Solution { 24 | public: 25 | ListNode* reverseList(ListNode* head) 26 | { 27 | vectorhelp; 28 | ListNode* temp=head; 29 | while (temp!=nullptr) 30 | { 31 | help.push_back(temp->val); 32 | temp=temp->next; 33 | } 34 | temp=head; 35 | for (int i=help.size()-1;i>=0;i--) 36 | { 37 | temp->val=help[i]; 38 | temp=temp->next; 39 | } 40 | return head; 41 | } 42 | }; 43 | -------------------------------------------------------------------------------- /2024/03 - March/22-03-2024.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 22/03/2024 5 | Problem : Palindrome Linked List 6 | Difficulty : Easy 7 | Problem Link : https://leetcode.com/problems/palindrome-linked-list/description/ 8 | Video Solution : NA 9 | 10 | */ 11 | 12 | 13 | /** 14 | * Definition for singly-linked list. 15 | * struct ListNode { 16 | * int val; 17 | * ListNode *next; 18 | * ListNode() : val(0), next(nullptr) {} 19 | * ListNode(int x) : val(x), next(nullptr) {} 20 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 21 | * }; 22 | */ 23 | class Solution { 24 | public: 25 | bool isPalindrome(ListNode* head) { 26 | ListNode * left = head; 27 | 28 | function traverse = [&](ListNode * right) -> bool { 29 | if(!right) 30 | return 1; 31 | 32 | bool ans = traverse(right -> next); 33 | if(!ans or left -> val != right -> val) 34 | return 0; 35 | 36 | left = left -> next; 37 | return 1; 38 | }; 39 | 40 | return traverse(head); 41 | } 42 | }; 43 | -------------------------------------------------------------------------------- /2024/03 - March/23-03-2024.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 23/03/2024 5 | Problem : Reorder List 6 | Difficulty : Medium 7 | Problem Link : https://leetcode.com/problems/reorder-list/description 8 | Video Solution : NA 9 | 10 | */ 11 | 12 | 13 | /** 14 | * Definition for singly-linked list. 15 | * struct ListNode { 16 | * int val; 17 | * ListNode *next; 18 | * ListNode() : val(0), next(nullptr) {} 19 | * ListNode(int x) : val(x), next(nullptr) {} 20 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 21 | * }; 22 | */ 23 | class Solution { 24 | public: 25 | void reorderList(ListNode* head) { 26 | stack s; 27 | ListNode* curr = head; 28 | while(curr){ 29 | s.push(curr); 30 | curr = curr->next; 31 | } 32 | curr = head; 33 | unordered_map vis; 34 | while(true){ 35 | ListNode* last = s.top(); 36 | s.pop(); 37 | ListNode* next = curr->next; 38 | vis[curr] = true; 39 | if(vis[last]){ 40 | curr->next = NULL; 41 | break; 42 | } 43 | curr->next = last; 44 | vis[last] = true; 45 | curr = curr->next; 46 | if(vis[next]){ 47 | curr->next = NULL; 48 | break; 49 | } 50 | curr->next = next; 51 | curr = curr->next; 52 | } 53 | } 54 | }; 55 | -------------------------------------------------------------------------------- /2024/03 - March/24-03-2024.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 24/03/2024 5 | Problem : Find the Duplicate Number 6 | Difficulty : Medium 7 | Problem Link : https://leetcode.com/problems/find-the-duplicate-number/description 8 | Video Solution : NA 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | int findDuplicate(vector& nums) { 16 | int n = nums.size(); 17 | vector f(n, 0); 18 | 19 | for(auto i : nums) { 20 | if(f[i]) 21 | return i; 22 | 23 | ++f[i]; 24 | } 25 | 26 | return 0; 27 | } 28 | }; 29 | -------------------------------------------------------------------------------- /2024/03 - March/25-03-2024.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 25/03/2024 5 | Problem : Find All Duplicates in an Array 6 | Difficulty : Medium 7 | Problem Link : https://leetcode.com/problems/find-all-duplicates-in-an-array/description/ 8 | Video Solution : NA 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | vector findDuplicates(vector& nums) { 16 | vector ans; 17 | int n = nums.size() + 1; 18 | vector f(n, 0); 19 | 20 | for(auto i : nums) 21 | ++f[i]; 22 | 23 | for(int i = 1; i < n; i++) 24 | if(f[i] == 2) 25 | ans.push_back(i); 26 | 27 | return ans; 28 | } 29 | }; 30 | -------------------------------------------------------------------------------- /2024/03 - March/26-03-2024.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 26/03/2024 5 | Problem : First Missing Positive 6 | Difficulty : Hard 7 | Problem Link : https://leetcode.com/problems/first-missing-positive/description/ 8 | Video Solution : NA 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | int firstMissingPositive(vector& nums) { 16 | int n = nums.size(); 17 | 18 | for (auto& num : nums) { 19 | if (num <= 0) num = n + 1; 20 | } 21 | 22 | for (auto& num : nums) { 23 | int index = abs(num) - 1; 24 | if (index < n) nums[index] = -abs(nums[index]); 25 | } 26 | 27 | for (int i = 0; i < n; i++) { 28 | if (nums[i] > 0) return (i + 1); 29 | } 30 | 31 | return (n + 1); 32 | } 33 | }; 34 | -------------------------------------------------------------------------------- /2024/03 - March/27-03-2024.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 27/03/2024 5 | Problem : Subarray Product Less Than K 6 | Difficulty : Medium 7 | Problem Link : https://leetcode.com/problems/subarray-product-less-than-k/description/?envType=daily-question&envId=2024-03-27 8 | Video Solution : NA 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | int numSubarrayProductLessThanK(vector& nums, int k) { 16 | if (k == 0) return 0; 17 | double logK = log(k); 18 | int m = nums.size() + 1; 19 | vector logsPrefixSum(m); 20 | 21 | for (int i = 0; i < nums.size(); i++) { 22 | logsPrefixSum[i + 1] = logsPrefixSum[i] + log(nums[i]); 23 | } 24 | 25 | int totalCount = 0; 26 | 27 | for (int currIdx = 0; currIdx < m; currIdx++) { 28 | int low = currIdx + 1, high = m; 29 | while (low < high) { 30 | int mid = low + (high - low) / 2; 31 | if (logsPrefixSum[mid] < logsPrefixSum[currIdx] + logK - 1e-9) { 32 | low = mid + 1; 33 | } else { 34 | high = mid; 35 | } 36 | } 37 | totalCount += low - currIdx - 1; 38 | } 39 | 40 | return totalCount; 41 | } 42 | }; 43 | 44 | -------------------------------------------------------------------------------- /2024/03 - March/28-03-2024.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 28/03/2024 5 | Problem : Length of Longest Subarray With at Most K Frequency 6 | Difficulty : Medium 7 | Problem Link : https://leetcode.com/problems/length-of-longest-subarray-with-at-most-k-frequency/description/ 8 | Video Solution : NA 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | int maxSubarrayLength(vector& nums, int k) { 16 | int ans = 0; 17 | int l = 0, r = 0; 18 | map f; 19 | 20 | while(r < nums.size()) { 21 | ++f[nums[r]]; 22 | 23 | while(f[nums[r]] > k){ 24 | --f[nums[l]]; 25 | if(f[nums[l]] == 0) 26 | f.erase(nums[l]); 27 | 28 | ++l; 29 | } 30 | 31 | ans = max(r - l + 1, ans); 32 | ++r; 33 | } 34 | 35 | return ans; 36 | } 37 | }; 38 | -------------------------------------------------------------------------------- /2024/03 - March/29-03-2024.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 29/03/2024 5 | Problem : Count Subarrays Where Max Element Appears at Least K Times 6 | Difficulty : Medium 7 | Problem Link : https://leetcode.com/problems/count-subarrays-where-max-element-appears-at-least-k-times/description 8 | Video Solution : NA 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | long long countSubarrays(vector& nums, int k) { 16 | int maxElement = *max_element(nums.begin(), nums.end()); 17 | vector indexesOfMaxElements; 18 | long long ans = 0; 19 | 20 | for (int index = 0; index < nums.size(); index++) { 21 | if (nums[index] == maxElement) { 22 | indexesOfMaxElements.push_back(index); 23 | } 24 | 25 | int freq = indexesOfMaxElements.size(); 26 | if (freq >= k) { 27 | ans += indexesOfMaxElements[freq - k] + 1; 28 | } 29 | } 30 | 31 | return ans; 32 | } 33 | }; 34 | -------------------------------------------------------------------------------- /2024/03 - March/30-03-2024.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 30/03/2024 5 | Problem : Subarrays with K Different Integers 6 | Difficulty : Hard 7 | Problem Link : https://leetcode.com/problems/subarrays-with-k-different-integers/description/ 8 | Video Solution : NA 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | int subarraysWithKDistinct(vector& nums, int k) { 16 | vector f(nums.size() + 1, 0); 17 | 18 | int ans = 0; 19 | int l = 0; 20 | int r = 0; 21 | int cur = 0; 22 | 23 | while (r < nums.size()) { 24 | if (++f[nums[r++]] == 1) { 25 | k--; 26 | } 27 | 28 | if (k < 0) { 29 | --f[nums[l++]]; 30 | k++; 31 | cur = 0; 32 | } 33 | 34 | if (k == 0) { 35 | while (f[nums[l]] > 1) { 36 | --f[nums[l++]]; 37 | cur++; 38 | } 39 | ans += (cur + 1); 40 | } 41 | } 42 | return ans; 43 | } 44 | }; 45 | -------------------------------------------------------------------------------- /2024/03 - March/31-03-2024.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Author : Manas Rawat 4 | Date : 31/03/2024 5 | Problem : Count Subarrays With Fixed Bounds 6 | Difficulty : Hard 7 | Problem Link : https://leetcode.com/problems/count-subarrays-with-fixed-bounds/description/ 8 | Video Solution : NA 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | long long countSubarrays(vector& nums, int minK, int maxK) { 16 | long long ans=0; 17 | int maxi=-1, mini=-1; 18 | int s=nums.size(); 19 | for(int r=0, l=0; rmaxK){ 22 | l=r+1; 23 | continue; 24 | } 25 | if (x==maxK) maxi=r; 26 | if (x==minK) mini=r; 27 | ans+=max((min(maxi, mini)-l+1),0); 28 | } 29 | return ans; 30 | } 31 | }; 32 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Leetcode Problem of the Day Repository 2 | This repository is a collection of C++ codes for Leetcode problem of the Day. 3 | 4 | ## Code of Conduct 5 | The solutions to the problems given here are just for reference only. Please avoid copying the code and trying doing them on your own by taking help from this repository. 6 | 7 | ## Video Solutions to all problems 8 | You can find the video solutions to all the problems given here on my youtube channel [cpwithcpp](https://www.youtube.com/@cpwithcpp). 9 | 10 | ### Happy Coding --------------------------------------------------------------------------------