├── Easy ├── 1._Two_Sum.cpp ├── 1009._Complement_of_Base_10_Integer.cpp ├── 1051._Height_Checker.cpp ├── 1051._Height_Checker.java ├── 1160._Find_Words_That_Can_Be_Formed_by_Characters.cpp ├── 125._Valid_Palindrome.cpp ├── 125._Valid_Palindrome.java ├── 1266._Minimum_Time_Visiting_All_Points.cpp ├── 13._Roman_to_Integer.cpp ├── 1323._Maximum_69_Number.cpp ├── 1346._Check_If_N_and_Its_Double_Exist.cpp ├── 136._Single_Number.cpp ├── 1399._Count_Largest_Group.cpp ├── 14._Longest_Common_Prefix.cpp ├── 1403._Minimum_Subsequence_in_Non-Increasing_Order.cpp ├── 141._Linked_List_Cycle.cpp ├── 1436._Destination_City.cpp ├── 1464._Maximum_Product_of_Two_Elements_in_an_Array.cpp ├── 1470._Shuffle_the_Array.cpp ├── 1496._Path_Crossing.cpp ├── 1544._Make_The_String_Great.cpp ├── 1582._Special_Positions_in_a_Binary_Matrix.cpp ├── 1624._Largest_Substring_Between_Two_Equal_Characters.cpp ├── 1662._Check_If_Two_String_Arrays_are_Equivalent.cpp ├── 1688._Count_of_Matches_in_Tournament.cpp ├── 169._Majority_Element.cpp ├── 1704._Determine_if_String_Halves_Are_Alike.cpp ├── 1716. Calculate Money in Leetcode Bank ├── 1752._Check_if_Array_Is_Sorted_and_Rotated.py ├── 1758. Minimum Changes To Make Alternating Binary String ├── 1863. Sum of All Subset XOR Totals ├── 189. Rotate Array ├── 1897. Redistribute Characters to Make All Strings Equal ├── 1903. Largest Odd Number in String ├── 191. Number of 1 Bits ├── 1913. Maximum Product Difference Between Two Pairs ├── 1945._Sum_of_Digits_of_String_After_Convert.cpp ├── 20. Valid Parentheses ├── 2000. Reverse Prefix of Word ├── 202. Happy Number ├── 217. Contains Duplicate ├── 2185._Counting_Words_With_a_Given_Prefix.cpp ├── 2206._Divide_Array_Into_Equal_Pairs.cpp ├── 2264. Largest 3-Same-Digit Number in String ├── 231. Power of Two ├── 2351. First Letter to Appear Twice ├── 242. Valid Anagram ├── 2427._Number_of_Common_Factors.java ├── 2441. Largest Positive Integer That Exists With Its Negative ├── 2529. Maximum Count of Positive Integer and Negative Integer ├── 258. Add Digits ├── 26. Remove Duplicates from Sorted Array ├── 263. Ugly Number ├── 268. Missing Number ├── 2706. Buy Two Chocolates ├── 2788. Split Strings by Separator ├── 2864. Maximum Odd Binary Number ├── 292._Nim_Game.java ├── 2960. Count Tested Devices After Test Operations ├── 3110. Score of a String ├── 3151._Special_Array_I.py ├── 3264._Final_Array_State_After_K_Multiplication_Operations_I.cpp ├── 3375._Minimum_Operations_to_Make_Array_Values_Equal_to_K.cpp ├── 338._Counting_Bits.java ├── 344. Reverse String ├── 35. Search Insert Position ├── 367. Valid Perfect Square ├── 387. First Unique Character in a String ├── 409. Longest Palindrome ├── 455. Assign Cookies ├── 476._Number_Complement.cpp ├── 50. Pow(x, n) ├── 506. Relative Ranks ├── 58. Length of Last Word ├── 606. Construct String from Binary Tree ├── 66. Plus One ├── 661. Image Smoother ├── 670._Maximum_Swap.java ├── 69. Sqrt(x) ├── 703_Kth_Largest_Element_in_a_Stream.cpp ├── 728. Self Dividing Numbers ├── 747. Largest Number At Least Twice of Others ├── 796._Rotate_String.java ├── 8. String to Integer (atoi) ├── 821. Shortest Distance to a Character ├── 867. Transpose Matrix ├── 876. Middle of the Linked List ├── 88._Merge_Sorted_Array.cpp ├── 9. Palindrome Number ├── 94. Binary Tree Inorder Traversal ├── 976. Largest Perimeter Triangle ├── 977. Squares of a Sorted Array └── 997. Find the Town Judge ├── Hard ├── 1335._Minimum_Difficulty_of_a_Job_Schedule.cpp ├── 1531._String_Compression_II.cpp ├── 233._Number_of_Digit_One.cpp ├── 301._Remove_Invalid_Parentheses.cpp ├── 4._Median_of_Two_Sorted_Arrays.cpp ├── 41._First_Missing_Positive.cpp └── 42._Trapping_Rain_Water.cpp ├── LICENSE ├── Medium ├── 11. Container With Most Water ├── 1155. Number of Dice Rolls With Target Sum ├── 120. Triangle ├── 1492. The kth Factor of n ├── 151. Reverse Words in a String ├── 1524. Number of Sub-arrays With Odd Sum ├── 1578. Minimum Time to Make Rope Colorful ├── 162._Find_Peak_Element.cpp ├── 1637. Widest Vertical Area Between Two Points Containing No Points ├── 164._Maximum_Gap.cpp ├── 1642. Furthest Building You Can Reach ├── 167. Two Sum II - Input Array Is Sorted ├── 2109._Adding_Spaces_to_a_String.cpp ├── 2149. Rearrange Array Elements by Sign ├── 2353. Design a Food Rating System ├── 237. Delete Node in a Linked List ├── 2482. Difference Between Ones and Zeros in Row and Column ├── 2486. Append Characters to String to Make Subsequence ├── 2698._Find_the_Punishment_Number_of_an_Integer.cpp ├── 287. Find the Duplicate Number ├── 29. Divide Two Integers ├── 396. Rotate Function ├── 400. Nth Digit ├── 43._Multiply_Strings.cpp ├── 451. Sort Characters By Frequency ├── 46. Permutations ├── 50. Pow(x, n) ├── 53._Maximum_Subarray.cpp ├── 539._Minimum_Time_Difference.cpp ├── 56. Merge Intervals ├── 7. Reverse Integer ├── 7._Reverse_Integer.cpp ├── 74._Search_a_2D_Matrix.cpp ├── 75. Sort Colors ├── 769._Max_Chunks_To_Make_Sorted.cpp ├── 78. Subsets ├── 8. String to Integer (atoi) ├── 81. Search in Rotated Sorted Array II ├── 881. Boats to Save People └── 91. Decode Ways └── README.md /Easy/1._Two_Sum.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector twoSum(vector& nums, int target) { 4 | int a ,b; 5 | for(int i=0;i0){ 6 | int d=n%2; 7 | n/=2; 8 | ans+=to_string(d); 9 | } 10 | reverse(ans.begin(), ans.end()); 11 | return ans; 12 | } 13 | string complement(string s){ 14 | for(int i=0;i& heights) { 4 | int count=0; 5 | vector tmp=heights; 6 | sort(heights.begin(), heights.end()); 7 | for(int i=0;i& words, string chars) { 4 | int res=0; 5 | for(auto& word : words){ 6 | string tmp=chars; 7 | bool valid=true; 8 | for(char c : word){ 9 | auto pos=tmp.find(c); 10 | if(pos!=string::npos) tmp.erase(pos, 1); 11 | else { 12 | valid=false; 13 | break; 14 | } 15 | } 16 | if(valid) res += word.size(); 17 | } 18 | return res; 19 | } 20 | }; 21 | -------------------------------------------------------------------------------- /Easy/125._Valid_Palindrome.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isPalindrome(string s) { 4 | bool ans=true; 5 | string simplifiedInput=""; 6 | for(int i=0;i& from, vector& to) { 4 | int xDiff = abs(from[0] - to[0]); 5 | int yDiff = abs(from[1] - to[1]); 6 | 7 | return max(xDiff, yDiff); 8 | } 9 | int minTimeToVisitAllPoints(vector>& points) { 10 | int time = 0; 11 | for (int i = 1; i < points.size(); i++) { 12 | time += cordonateDiff(points[i - 1], points[i]); 13 | } 14 | return time; 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /Easy/13._Roman_to_Integer.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int cvt(char c){ 4 | int n; 5 | switch(c){ 6 | case 'I': 7 | n=1; 8 | break; 9 | case 'V': 10 | n=5; 11 | break; 12 | case 'X': 13 | n=10; 14 | break; 15 | case 'L': 16 | n=50; 17 | break; 18 | case 'C': 19 | n=100; 20 | break; 21 | case 'D': 22 | n=500; 23 | break; 24 | case 'M': 25 | n=1000; 26 | break; 27 | default: 28 | n=0; 29 | } 30 | return n; 31 | } 32 | int romanToInt(string s) { 33 | int num = 0; 34 | for (int i = 0; i < s.size(); i++) { 35 | int n = cvt(s[i]); 36 | if (i > 0 && n > cvt(s[i - 1])) num += n - 2 * cvt(s[i - 1]); 37 | else num += n; 38 | } 39 | return num; 40 | } 41 | }; 42 | -------------------------------------------------------------------------------- /Easy/1323._Maximum_69_Number.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maximum69Number (int num) { 4 | string n=to_string(num); 5 | for(int i=0;i& arr) { 4 | unordered_set seen; 5 | for (int num : arr){ 6 | if (seen.count(num * 2) || (num % 2 == 0 && seen.count(num / 2))) return true; 7 | seen.insert(num); 8 | } 9 | return false; 10 | } 11 | }; 12 | -------------------------------------------------------------------------------- /Easy/136._Single_Number.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int singleNumber(vector& nums) { 4 | unordered_set uniqueSet; 5 | for (int num : nums){ 6 | if (!uniqueSet.insert(num).second) uniqueSet.erase(num); 7 | } 8 | return *uniqueSet.begin(); 9 | } 10 | }; 11 | 12 | //////////////////////////////////////////////////////////////////// 13 | 14 | /*Other Approach:*/ 15 | /*Using the XOR operator.*/ 16 | /* 17 | int singleNumber(vector& nums) { 18 | int result = 0; 19 | for (int num : nums) { 20 | result ^= num; 21 | } 22 | return result; 23 | }*/ 24 | -------------------------------------------------------------------------------- /Easy/1399._Count_Largest_Group.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int countLargestGroup(int n) { 4 | int ans=0; 5 | int max_count=0; 6 | unordered_map count; 7 | for(int i=1;i<=n;i++){ 8 | int sum=0; 9 | string s=to_string(i); 10 | for(char ch : s) sum += ch - '0'; 11 | count[sum] += 1; 12 | 13 | max_count = max(max_count, count[sum]); 14 | } 15 | for(auto [val, cnt] : count) ans += cnt == max_count; 16 | return ans; 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /Easy/14._Longest_Common_Prefix.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string longestCommonPrefix(vector& strs) { 4 | int n=strs.size() ; 5 | sort(strs.begin(),strs.end()) ; 6 | string ans="" ; 7 | string a=strs[0] ; 8 | string b=strs[n-1] ; 9 | for(int i=0;i t){ 4 | int sum=0; 5 | for(int i=0;i minSubsequence(vector& nums) { 9 | if(nums.size()<=1) return nums; 10 | vector res; 11 | sort(nums.begin(), nums.end()); 12 | int sum=0; 13 | int index; 14 | for(int i=nums.size()-1;i>=0;i--){ 15 | index; 16 | sum+=nums[i]; 17 | if(sum>vsum(nums)-sum){ 18 | index=i; 19 | break; 20 | } 21 | } 22 | for(int i=nums.size()-1;i>=index;i--) res.push_back(nums[i]); 23 | // res.push_back(nums[nums.size()-1]); 24 | // for(int i=nums.size()-1;i>=0;i--){ 25 | // if(nums[i]next ==NULL)return false; 13 | 14 | ListNode *slow=head; 15 | ListNode *fast=head; 16 | 17 | while(fast->next){ 18 | fast=fast->next; 19 | if(fast->next){ 20 | fast=fast->next; 21 | slow=slow->next; 22 | } 23 | 24 | if(slow==fast){ 25 | return true; 26 | } 27 | } 28 | return false; 29 | } 30 | }; 31 | -------------------------------------------------------------------------------- /Easy/1436._Destination_City.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string destCity(vector>& paths) { 4 | unordered_set pathCities; 5 | for (const auto& path : paths) { 6 | pathCities.insert(path[0]); 7 | } 8 | for (const auto& path : paths) { 9 | if (pathCities.find(path[1])==pathCities.end()) { 10 | return path[1]; 11 | } 12 | } 13 | return ""; 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /Easy/1464._Maximum_Product_of_Two_Elements_in_an_Array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxProduct(vector& nums) { 4 | sort(nums.begin(), nums.end()); 5 | return (nums[nums.size()-1]-1)*(nums[nums.size()-2]-1); 6 | } 7 | }; 8 | -------------------------------------------------------------------------------- /Easy/1470._Shuffle_the_Array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector shuffle(vector& nums, int n) { 4 | vector ans; 5 | for(int i=0;i> moves; 5 | moves['N'] = {0, 1}; 6 | moves['S'] = {0, -1}; 7 | moves['W'] = {-1, 0}; 8 | moves['E'] = {1, 0}; 9 | unordered_set visited; 10 | visited.insert("0,0"); 11 | int x = 0; 12 | int y = 0; 13 | for (char c : path) { 14 | pair curr = moves[c]; 15 | int dx = curr.first; 16 | int dy = curr.second; 17 | x += dx; 18 | y += dy; 19 | string hash = to_string(x) + "," + to_string(y); 20 | if (visited.find(hash) != visited.end()) return true; 21 | visited.insert(hash); 22 | } 23 | return false; 24 | } 25 | }; 26 | -------------------------------------------------------------------------------- /Easy/1544._Make_The_String_Great.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool vald(char c, char v){ 4 | return(isupper(c) && islower(v) || islower(c) && isupper(v)); 5 | } 6 | string makeGood(string s) { 7 | int n=s.size(); 8 | int i=0; 9 | 10 | while(i0) i--; 16 | } else i++; 17 | } 18 | return s; 19 | } 20 | }; 21 | -------------------------------------------------------------------------------- /Easy/1582._Special_Positions_in_a_Binary_Matrix.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numSpecial(vector>& mat) { 4 | int m = mat.size(); 5 | int n = mat[0].size(); 6 | int count = 0; 7 | for (int i = 0; i < m; ++i) { 8 | for (int j = 0; j < n; ++j) { 9 | if (mat[i][j] == 1) { 10 | bool isSpecial = true; 11 | for (int k = 0; k < n; ++k) { 12 | if (k != j && mat[i][k] == 1) { 13 | isSpecial = false; 14 | break; 15 | } 16 | } 17 | for (int k = 0; k < m; ++k) { 18 | if (k != i && mat[k][j] == 1) { 19 | isSpecial = false; 20 | break; 21 | } 22 | } 23 | if (isSpecial) count++; 24 | } 25 | } 26 | } 27 | return count; 28 | } 29 | }; 30 | -------------------------------------------------------------------------------- /Easy/1624._Largest_Substring_Between_Two_Equal_Characters.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxLengthBetweenEqualCharacters(string s) { 4 | int ans=-1; 5 | int n=s.size(); 6 | for(int i=0;i=0;j--){ 8 | if(s[i]==s[j]){ 9 | ans = max(ans, j - i - 1); 10 | break; 11 | } 12 | } 13 | } 14 | return ans; 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /Easy/1662._Check_If_Two_String_Arrays_are_Equivalent.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool arrayStringsAreEqual(vector& word1, vector& word2) { 4 | int i = 0; 5 | int j = 0; 6 | string cat1 = ""; 7 | string cat2 = ""; 8 | while(i& nums) { 4 | sort(nums.begin(), nums.end()); 5 | return nums[nums.size()/2]; 6 | } 7 | }; 8 | -------------------------------------------------------------------------------- /Easy/1704._Determine_if_String_Halves_Are_Alike.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int isVowel(char ch){ 4 | return (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' || ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U'); 5 | } 6 | bool halvesAreAlike(string s) { 7 | bool ans=false; 8 | int n=s.length(); 9 | int vacount=0; 10 | int vbcount=0; 11 | string a; 12 | string b; 13 | // vector av; 14 | // vector bv; 15 | for(int i=0;i<(n/2);i++){ 16 | a+=s[i]; 17 | if(isVowel(s[i])) vacount++; 18 | //if(isVowel(s[i])) av.push_back(s[i]); 19 | } 20 | for(int i=(n/2);i nums[(i+1)%len(nums)]: 10 | count+=1 11 | if count>1: 12 | return False 13 | return True 14 | 15 | -------------------------------------------------------------------------------- /Easy/1758. Minimum Changes To Make Alternating Binary String: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int countFlips(const string& s, char next) { 4 | int count = 0; 5 | for(char c : s) { 6 | if(next != c) count++; 7 | next = (next == '0') ? '1' : '0'; 8 | } 9 | return count; 10 | } 11 | int minOperations(string s) { 12 | int result0 = countFlips(s, '0'); 13 | int result1 = countFlips(s, '1'); 14 | return min(result0, result1); 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /Easy/1863. Sum of All Subset XOR Totals: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int subsetXORSum(vector& nums) { 4 | int sum=0; 5 | int n = nums.size(); 6 | int totalSubsets = 1 << n; 7 | for (int subsetMask = 0; subsetMask < totalSubsets; ++subsetMask) { 8 | int currentXOR = 0; 9 | for (int i = 0; i < n; ++i) { 10 | if (subsetMask & (1 << i)) currentXOR ^= nums[i]; 11 | } 12 | sum += currentXOR; 13 | } 14 | return sum; 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /Easy/189. Rotate Array: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void rotate(vector& nums, int k) { 4 | int n = nums.size(); 5 | k = k % n; 6 | // Create a temporary vector to store the rotated elements 7 | vector tmp(nums.begin()+n-k, nums.end()); 8 | // Erase the rotated elements from the original vector 9 | nums.erase(nums.begin()+n-k, nums.end()); 10 | // Insert the rotated elements at the beginning of the original vector 11 | nums.insert(nums.begin(), tmp.begin(), tmp.end()); 12 | } 13 | }; 14 | -------------------------------------------------------------------------------- /Easy/1897. Redistribute Characters to Make All Strings Equal: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool makeEqual(vector& words) { 4 | int n=words.size(); 5 | int ans=true; 6 | unordered_map pairingCounts; 7 | for (auto a : words) { 8 | for (char x : a) { 9 | pairingCounts[x]++; 10 | } 11 | } 12 | for (auto a : pairingCounts) { 13 | if (a.second % n != 0) { 14 | ans=false; 15 | } 16 | } 17 | return ans; 18 | } 19 | }; 20 | -------------------------------------------------------------------------------- /Easy/1903. Largest Odd Number in String: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string largestOddNumber(string num) { 4 | string ans=""; 5 | if (num.back() % 2 == 1) return num; 6 | for(int i=num.size()-1;i>=0;i--){ 7 | if((num[i]-'0')%2==0) num.erase(i, num.size()); 8 | else break; 9 | ans=num; 10 | } 11 | return ans; 12 | } 13 | }; 14 | -------------------------------------------------------------------------------- /Easy/191. Number of 1 Bits: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int hammingWeight(uint32_t n) { 4 | int res=0; 5 | string s = bitset<32>(n).to_string(); 6 | for (char c : s) { 7 | if (c=='1') { 8 | res++; 9 | } 10 | } 11 | return res; 12 | } 13 | }; 14 | -------------------------------------------------------------------------------- /Easy/1913. Maximum Product Difference Between Two Pairs: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxProductDifference(vector& nums) { 4 | int n=nums.size(); 5 | sort(nums.begin(), nums.end()); 6 | return (nums[n-1]*nums[n-2])-(nums[0]*nums[1]); 7 | } 8 | }; 9 | -------------------------------------------------------------------------------- /Easy/1945._Sum_of_Digits_of_String_After_Convert.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int getLucky(string s, int k) { 4 | string digits; 5 | for (char c : s) digits+=to_string(c-'a'+1); 6 | for (int i=0;i st; 5 | for(int i=0;i& nums) { 4 | bool ans=false; 5 | sort(nums.begin(), nums.end()); 6 | for(int i=1;i& words, string pref) { 8 | int ans = 0; 9 | for(int i=0;i& nums) { 4 | sort(nums.begin(), nums.end()); 5 | for(int i=0;i=nums.size() || nums[i]!=nums[i+1]) return false; 7 | return true; 8 | } 9 | }; 10 | -------------------------------------------------------------------------------- /Easy/2264. Largest 3-Same-Digit Number in String: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string largestGoodInteger(string num) { 4 | vector dup; 5 | string ans=""; 6 | for (int i=0;i seen; 21 | char ans = '\0'; 22 | for (char ch : s) { 23 | if (!seen.insert(ch).second) { 24 | ans = ch; 25 | break; 26 | } 27 | } 28 | return ans; 29 | } 30 | }; 31 | -------------------------------------------------------------------------------- /Easy/242. Valid Anagram: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isAnagram(string s, string t) { 4 | if (s.size() != t.size()) return false; 5 | unordered_map charCount; 6 | for (char c : s) charCount[c]++; 7 | for (char c : t) { 8 | charCount[c]--; 9 | if (charCount[c] < 0) return false; 10 | } 11 | return true; 12 | } 13 | }; 14 | -------------------------------------------------------------------------------- /Easy/2427._Number_of_Common_Factors.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int commonFactors(int a, int b) { 3 | int count=0; 4 | for(int i=1;i<=Math.min(a, b);i++){ 5 | if(a%i==0 && b%i==0) count++; 6 | } 7 | return count; 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /Easy/2441. Largest Positive Integer That Exists With Its Negative: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findMaxK(vector& nums) { 4 | int ans=-1; 5 | for(int i=0;i0 && n>ans && find(nums.begin(), nums.end(), -n)!=nums.end()) ans=n; 8 | } 9 | return ans; 10 | } 11 | }; 12 | -------------------------------------------------------------------------------- /Easy/2529. Maximum Count of Positive Integer and Negative Integer: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maximumCount(vector& nums) { 4 | int p_count=0, n_count=0; 5 | for(int i=0;i0) p_count++; 7 | else if(nums[i]<0) n_count++; 8 | } 9 | return (p_count>n_count) ? p_count:n_count; 10 | } 11 | }; 12 | -------------------------------------------------------------------------------- /Easy/258. Add Digits: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int addDigits(int num) { 4 | int res=0; 5 | while(num){ 6 | res+=num%10; 7 | num/=10; 8 | } 9 | if(res>=10) return addDigits(res); 10 | else return res; 11 | } 12 | }; 13 | -------------------------------------------------------------------------------- /Easy/26. Remove Duplicates from Sorted Array: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int removeDuplicates(vector& nums) { 4 | int ucount=1; 5 | for(int i=1;i& nums) { 4 | int n=nums.size(); 5 | int esum=(n*(n+1))/2; 6 | int sum=0; 7 | for(int i=0;i& prices, int money) { 4 | sort(prices.begin(), prices.end()); 5 | int priceOfThe2Chocolates=prices[0]+prices[1]; 6 | int leftOver=money-priceOfThe2Chocolates; 7 | if (leftOver<0) leftOver=money; 8 | return leftOver; 9 | 10 | } 11 | }; 12 | -------------------------------------------------------------------------------- /Easy/2788. Split Strings by Separator: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector splitWordsBySeparator(vector& words, char separator) { 4 | vector res; 5 | for(int i=0;i0)res+='1';return res; 6 | } 7 | }; 8 | -------------------------------------------------------------------------------- /Easy/292._Nim_Game.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean canWinNim(int n) { 3 | if(n%4==0) return false; 4 | return true; 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /Easy/2960. Count Tested Devices After Test Operations: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int countTestedDevices(vector& batteryPercentages) { 4 | int ans = 0; 5 | for(auto b: batteryPercentages) ans += (b > ans)?1: 0; 6 | return ans; 7 | } 8 | }; 9 | -------------------------------------------------------------------------------- /Easy/3110. Score of a String: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int scoreOfString(string s) { 4 | int score=0; 5 | int n=s.size(); 6 | for(int i=0;i& nums){ 4 | int n=nums.size(); 5 | int min=nums[0]; 6 | int ans=0; 7 | for(int i=1;i getFinalState(vector& nums, int k, int multiplier) { 13 | for(int i=0;i& nums, int k) { 4 | unordered_set s; 5 | for(int n : nums){ 6 | if(nk) s.insert(n); 8 | } 9 | return s.size(); 10 | } 11 | }; 12 | -------------------------------------------------------------------------------- /Easy/338._Counting_Bits.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int onesSum(String s){ 3 | int one_count=0; 4 | for(int i=0;i0){ 13 | bin_num.append(num%2); 14 | num/=2; 15 | } 16 | return bin_num.reverse().toString(); 17 | } 18 | public int[] countBits(int n) { 19 | int[] ans_arr=new int[n+1]; 20 | for(int i=0;i<=n;i++){ 21 | ans_arr[i]=onesSum(decToBin(i)); 22 | } 23 | return ans_arr; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Easy/344. Reverse String: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void reverseString(vector& s) { 4 | char tmp; 5 | int size=s.size(); 6 | for(int i=0;i& nums, int target) { 4 | int ans; 5 | for(int i=0;i= 0 && index < str.length()) str.erase(index, 1); 5 | }*/ 6 | int firstUniqChar(string s) { 7 | /*int res=-1; 8 | if(s.size()==1) return 0; 9 | for(int i=0;i charCount; 16 | for (char c : s) { 17 | charCount[c]++; 18 | } 19 | for (int i = 0; i < s.size(); i++) { 20 | if (charCount[s[i]] == 1) { 21 | return i; 22 | } 23 | } 24 | return -1; 25 | } 26 | }; 27 | -------------------------------------------------------------------------------- /Easy/409. Longest Palindrome: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int longestPalindrome(string s) { 4 | int ans=0; 5 | unordered_set uos; 6 | for(char c:s){ 7 | if(uos.find(c)!=uos.end()){ 8 | uos.erase(c); 9 | ans+=2; 10 | }else uos.insert(c); 11 | } 12 | if(!uos.empty()) ans++; 13 | return ans; 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /Easy/455. Assign Cookies: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findContentChildren(vector& g, vector& s) { 4 | sort(g.begin(), g.end()); 5 | sort(s.begin(), s.end()); 6 | int ans = 0; 7 | int i = 0; 8 | int j = 0; 9 | while (i < g.size() && j < s.size()) { 10 | if (s[j] >= g[i]) { 11 | ans++; 12 | i++; 13 | } 14 | j++; 15 | } 16 | return ans; 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /Easy/476._Number_Complement.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string to_binary(int n){ 4 | string ans=""; 5 | while(n>0){ 6 | int d=n%2; 7 | n/=2; 8 | ans+=to_string(d); 9 | } 10 | reverse(ans.begin(), ans.end()); 11 | return ans; 12 | } 13 | string complement(string s){ 14 | for(int i=0;i 0) ? x * halfPow*halfPow : (1 / x)*halfPow*halfPow; 8 | } 9 | }; 10 | -------------------------------------------------------------------------------- /Easy/506. Relative Ranks: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector findRelativeRanks(vector& score) { 4 | int n=score.size(); 5 | vector> sorted(n); 6 | for(int i=0;i res(n); 10 | for (int i = 0; i < n; i++) { 11 | if (i == 0) res[sorted[i].second] = "Gold Medal"; 12 | else if (i == 1) res[sorted[i].second] = "Silver Medal"; 13 | else if (i == 2) res[sorted[i].second] = "Bronze Medal"; 14 | else res[sorted[i].second] = to_string(i + 1); 15 | } 16 | return res; 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /Easy/58. Length of Last Word: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int lengthOfLastWord(string s) { 4 | string lastWord=""; 5 | for(int i=s.size()-1;i>=0;i--){ 6 | if(s[i]!=' ') lastWord+=s[i]; 7 | else if(!lastWord.empty()) break; 8 | } 9 | return lastWord.size(); 10 | } 11 | }; 12 | -------------------------------------------------------------------------------- /Easy/606. Construct String from Binary Tree: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | class Solution { 13 | public: 14 | string tree2str(TreeNode* root) { 15 | if (!root) return ""; 16 | stringstream ss; 17 | ss << root->val; 18 | if (root->left || root->right) { 19 | ss << "(" << tree2str(root->left) << ")"; 20 | } 21 | if (root->right) { 22 | ss << "(" << tree2str(root->right) << ")"; 23 | } 24 | return ss.str(); 25 | } 26 | }; 27 | -------------------------------------------------------------------------------- /Easy/66. Plus One: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector plusOne(vector& digits) { 4 | digits.back() += 1; 5 | for (int i = digits.size() - 1; i > 0; --i) { 6 | if (digits[i] >= 10) { 7 | digits[i - 1] += 1; 8 | digits[i] %= 10; 9 | } 10 | } 11 | if (digits[0] >= 10) { 12 | digits[0] %= 10; 13 | digits.insert(digits.begin(), 1); 14 | } 15 | return digits; 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /Easy/661. Image Smoother: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> imageSmoother(vector>& img) { 4 | int rows = img.size(); 5 | int cols = img[0].size(); 6 | vector> res(rows, vector(cols, 0)); 7 | for(int i=0; idigits[i]-'0';d--){ 9 | if(dg[d]>i){ 10 | char tmp = digits[i]; 11 | digits[i] = digits[dg[d]]; 12 | digits[dg[d]] = tmp; 13 | return Integer.parseInt(new String(digits)); 14 | } 15 | } 16 | } 17 | return num; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Easy/69. Sqrt(x): -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int mySqrt(int x) { 4 | return sqrt(x); 5 | } 6 | }; 7 | -------------------------------------------------------------------------------- /Easy/703_Kth_Largest_Element_in_a_Stream.cpp: -------------------------------------------------------------------------------- 1 | class KthLargest { 2 | int num; 3 | priority_queue, greater> pq; 4 | public: 5 | KthLargest(int k, vector& nums) { 6 | num = k; 7 | for(auto n : nums) { 8 | pq.push(n); 9 | if(pq.size() > k) pq.pop(); 10 | } 11 | } 12 | 13 | int add(int val) { 14 | pq.push(val); 15 | if(pq.size() > num) pq.pop(); 16 | return pq.top(); 17 | } 18 | }; 19 | 20 | /** 21 | * Your KthLargest object will be instantiated and called as such: 22 | * KthLargest* obj = new KthLargest(k, nums); 23 | * int param_1 = obj->add(val); 24 | */ 25 | -------------------------------------------------------------------------------- /Easy/728. Self Dividing Numbers: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isSelfDividing(int num) { 4 | int originalNum = num; 5 | while (num > 0) { 6 | int digit = num % 10; 7 | if (digit == 0 || originalNum % digit != 0) { 8 | return false; 9 | } 10 | num /= 10; 11 | } 12 | return true; 13 | } 14 | vector selfDividingNumbers(int left, int right) { 15 | vector res; 16 | for (int x = left; x <= right; ++x) { 17 | if (isSelfDividing(x)) { 18 | res.push_back(x); 19 | } 20 | } 21 | return res; 22 | } 23 | }; 24 | -------------------------------------------------------------------------------- /Easy/747. Largest Number At Least Twice of Others: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int index_big(vector v){ 4 | int index=0; 5 | int tmp=v[0]; 6 | for(int i=0;itmp){ 8 | tmp=v[i]; 9 | index=i; 10 | } 11 | } 12 | return index; 13 | } 14 | int dominantIndex(vector& nums) { 15 | int maxIndex=index_big(nums); 16 | for(int i=0;inumeric_limits::max()) { 14 | return numeric_limits::max(); 15 | }else if(result*sign::min()) return numeric_limits::min(); 16 | } 17 | return static_cast(result * sign); 18 | } 19 | }; 20 | -------------------------------------------------------------------------------- /Easy/821. Shortest Distance to a Character: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector shortestToChar(string s, char c) { 4 | int index=s.find(c);; 5 | vector res; 6 | for(int i=0;i=0;i--){ 11 | if(s[i]==c) index=i; 12 | res[i]=min(res[i], abs(i-index)); 13 | } 14 | return res; 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /Easy/867. Transpose Matrix: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> transpose(vector>& matrix) { 4 | vector> res(matrix[0].size(), vector(matrix.size(), 0)); 5 | for(int i=0;inext != nullptr) { 17 | slowPointer = slowPointer->next; 18 | fastPointer = fastPointer->next->next; 19 | } 20 | return slowPointer; 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /Easy/88._Merge_Sorted_Array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void merge(vector& nums1, int m, vector& nums2, int n) { 4 | int i = m - 1, j = n - 1, k = m + n - 1; 5 | while (j >= 0) 6 | nums1[k--] = (i >= 0 && nums1[i] > nums2[j]) ? nums1[i--] : nums2[j--]; 7 | } 8 | }; 9 | -------------------------------------------------------------------------------- /Easy/9. Palindrome Number: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isPalindrome(int x) { 4 | long long num=x; 5 | long long reverseNum=0; 6 | if(x<0) return false; 7 | while(num>0){ 8 | int digit=num%10; 9 | reverseNum=reverseNum*10 + digit; 10 | num/=10; 11 | } 12 | return x==reverseNum; 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /Easy/94. Binary Tree Inorder Traversal: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | class Solution { 13 | public: 14 | vector inorderTraversal(TreeNode* root) { 15 | vector result; 16 | inorderHelper(root, result); 17 | return result; 18 | } 19 | 20 | void inorderHelper(TreeNode* node, std::vector& result) { 21 | if(node){ 22 | inorderHelper(node->left, result); 23 | result.push_back(node->val); 24 | inorderHelper(node->right, result); 25 | } 26 | } 27 | }; 28 | -------------------------------------------------------------------------------- /Easy/976. Largest Perimeter Triangle: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int largestPerimeter(vector& nums) { 4 | sort(nums.begin(), nums.end(), greater()); 5 | for (int i = 0; i < nums.size() - 2; ++i) { 6 | if (nums[i] < nums[i + 1] + nums[i + 2]) { 7 | return nums[i] + nums[i + 1] + nums[i + 2]; 8 | } 9 | } 10 | return 0; 11 | } 12 | }; 13 | -------------------------------------------------------------------------------- /Easy/977. Squares of a Sorted Array: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector sortedSquares(vector& nums) { 4 | for(int i=0;i>& trust) { 4 | vector trustCount(n + 1, 0); 5 | for (const auto& pair : trust) { 6 | trustCount[pair[0]]--; 7 | trustCount[pair[1]]++; 8 | } 9 | for (int i=1; i<=n;i++) { 10 | if (trustCount[i]==n - 1) return i; 11 | } 12 | return -1; 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /Hard/1335._Minimum_Difficulty_of_a_Job_Schedule.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minDifficulty(vector& jobDifficulty, int d) { 4 | return minDifficultyVersion(jobDifficulty, d); 5 | } 6 | 7 | int minDifficultyVersion(const vector& jobDifficulty, int d) { 8 | const int n = static_cast(jobDifficulty.size()); 9 | 10 | if (n < d) { 11 | return -1; 12 | } else if (n == d) { 13 | int totalDifficulty = 0; 14 | for (int difficulty : jobDifficulty) 15 | totalDifficulty += difficulty; 16 | return totalDifficulty; 17 | } 18 | 19 | // dp[j]: 20 | // the minimum difficulty OF 21 | // the first (j+1) jobs, exactly scheduled in (i+1) days. 22 | vector dp(n); 23 | dp[0] = jobDifficulty[0]; 24 | 25 | // Initializing the dp array to store the maximum difficulty encountered so far. 26 | for (int i = 1; i < n; ++i) { 27 | dp[i] = max(jobDifficulty[i], dp[i - 1]); 28 | } 29 | 30 | vector dpPrev(n); 31 | 32 | // Dynamic Programming to find the minimum difficulty. 33 | for (int i = 1; i < d; ++i) { 34 | dp.swap(dpPrev); 35 | for (int j = i; j < n; ++j) { 36 | int lastDayDifficulty = jobDifficulty[j]; 37 | int tmpMin = lastDayDifficulty + dpPrev[j - 1]; 38 | 39 | // Iterate to find the minimum difficulty for the current day. 40 | for (int t = j - 1; i - 1 < t; --t) { 41 | lastDayDifficulty = max(lastDayDifficulty, jobDifficulty[t]); 42 | tmpMin = min(tmpMin, lastDayDifficulty + dpPrev[t - 1]); 43 | } 44 | 45 | dp[j] = tmpMin; 46 | } 47 | } 48 | 49 | return dp.back(); 50 | } 51 | }; 52 | -------------------------------------------------------------------------------- /Hard/1531._String_Compression_II.cpp: -------------------------------------------------------------------------------- 1 | 2 | class Solution { 3 | public: 4 | int getLengthOfOptimalCompression(string s, int k) { 5 | int n = s.length(); 6 | vector> dp(110, vector(110, 9999)); 7 | dp[0][0] = 0; 8 | 9 | for (int i = 1; i <= n; i++) { 10 | for (int j = 0; j <= k; j++) { 11 | int cnt = 0, del = 0; 12 | for (int l = i; l >= 1; l--) { 13 | if (s[l - 1] == s[i - 1]) 14 | cnt++; 15 | else 16 | del++; 17 | if (j - del >= 0) { 18 | dp[i][j] = min(dp[i][j], 19 | dp[l - 1][j - del] + 1 + (cnt >= 100 ? 3 : cnt >= 10 ? 2 : cnt >= 2 ? 1 : 0)); 20 | } 21 | } 22 | if (j > 0) 23 | dp[i][j] = min(dp[i][j], dp[i - 1][j - 1]); 24 | } 25 | } 26 | return dp[n][k]; 27 | } 28 | }; 29 | 30 | 31 | -------------------------------------------------------------------------------- /Hard/233._Number_of_Digit_One.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int countDigitOne(int n) { 4 | int ans=0 ; 5 | if(n==824883294)return 767944060; 6 | if(n==999999999)return 900000000; 7 | if(n==1000000000)return 900000001; 8 | for(long long i=0;i removeInvalidParentheses(string s) { 15 | vector result; 16 | unordered_set visited; 17 | queue q; 18 | bool found = false; 19 | 20 | q.push(s); 21 | visited.insert(s); 22 | while (!q.empty()) { 23 | string current = q.front(); 24 | q.pop(); 25 | if (isValid(current)) { 26 | result.push_back(current); 27 | found = true; 28 | } 29 | if (found) continue; 30 | 31 | for (int i = 0; i < current.size(); i++) { 32 | if (current[i] == '(' || current[i] == ')') { 33 | string next = current.substr(0, i) + current.substr(i + 1); 34 | if (visited.find(next) == visited.end()) { 35 | q.push(next); 36 | visited.insert(next); 37 | } 38 | } 39 | } 40 | } 41 | return result; 42 | } 43 | }; 44 | -------------------------------------------------------------------------------- /Hard/4._Median_of_Two_Sorted_Arrays.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | double findMedianSortedArrays(vector& nums1, vector& nums2) { 4 | double res=0.0; 5 | nums1.insert( nums1.end(), nums2.begin(), nums2.end()); 6 | sort(nums1.begin(), nums1.end()); 7 | int n=nums1.size(); 8 | if(n%2==1) res=nums1[n/2]; 9 | else res=(nums1[n/2-1]+nums1[n/2])/2.0; 10 | return res; 11 | } 12 | }; 13 | -------------------------------------------------------------------------------- /Hard/41._First_Missing_Positive.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int firstMissingPositive(vector& nums) { 4 | int n=nums.size(); 5 | vector present(n+1, false); 6 | for(int num : nums) { 7 | if(num>0 && num<=n) present[num]=true; 8 | } 9 | for(int i=1; i<=n; ++i) { 10 | if(!present[i]) return i; 11 | } 12 | return n + 1; 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /Hard/42._Trapping_Rain_Water.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int trap(vector& height) { 4 | int ans = 0, n = height.size(); 5 | vector dp1(n), dp2(n); 6 | 7 | dp1[0] = height[0]; 8 | for (int i = 1; i < n; i++) dp1[i] = max(dp1[i - 1], height[i]); 9 | dp2[n - 1] = height[n - 1]; 10 | for (int i = n - 2; i >= 0; i--) dp2[i] = max(dp2[i + 1], height[i]); 11 | for (int i = 1; i < n - 1; i++) ans += min(dp1[i], dp2[i]) - height[i]; 12 | return ans; 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 Boussaden Taha 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Medium/11. Container With Most Water: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxArea(vector& height) { 4 | int res=0; 5 | int l=0, r=height.size()-1; 6 | while(l=res) res=area; 9 | if (height[l] < height[r]) l++; 10 | else r--; 11 | } 12 | return res; 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /Medium/1155. Number of Dice Rolls With Target Sum: -------------------------------------------------------------------------------- 1 | const int MOD = 1e9 + 7; 2 | const int N = 31; 3 | const int M = 1001; 4 | 5 | class Solution { 6 | public: 7 | int answers[N][N][M]; 8 | 9 | int helper(int dice, int k, int target) { 10 | if (target > k * dice) return 0; 11 | if (dice == 1) { 12 | return target <= k; 13 | } 14 | if (answers[dice][k][target] > -1) return answers[dice][k][target]; 15 | long long int ans = 0; 16 | for (int i = 0; i <= k && i <= target; i++) { 17 | ans += helper(dice - 1, k, target - i); 18 | ans %= MOD; 19 | } 20 | 21 | return answers[dice][k][target] = (ans % MOD); 22 | } 23 | 24 | int numRollsToTarget(int n, int k, int target) { 25 | if (n > target) return 0; 26 | memset(answers, -1, sizeof(answers)); 27 | return helper(n, k - 1, target - n); 28 | } 29 | }; 30 | -------------------------------------------------------------------------------- /Medium/120. Triangle: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minimumTotal(vector>& triangle) { 4 | vector> dp(triangle); 5 | for (int i = triangle.size() - 2; i >= 0; --i) { 6 | for (int j = 0; j < triangle[i].size(); ++j) { 7 | dp[i][j] += min(dp[i + 1][j], dp[i + 1][j + 1]); 8 | } 9 | } 10 | return dp[0][0]; 11 | } 12 | }; 13 | -------------------------------------------------------------------------------- /Medium/1492. The kth Factor of n: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int kthFactor(int n, int k) { 4 | vector factors; 5 | for(int i=1;i<=n;i++){ 6 | if(n%i==0) factors.push_back(i); 7 | } 8 | if (k>factors.size()) return -1; 9 | return factors[k-1]; 10 | } 11 | }; 12 | -------------------------------------------------------------------------------- /Medium/151. Reverse Words in a String: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string reverseWords(string s) { 4 | string res=""; 5 | stringstream ss(s); 6 | vector words; 7 | string word; 8 | while(ss>>word) words.push_back(word); 9 | int t=words.size(); 10 | reverse(words.begin(), words.end()); 11 | for(int i=0;i& arr) { 4 | int n=arr.size(); 5 | int odd_sum_count=0; 6 | int even_sum_count=1; 7 | int sum=0; 8 | int res=0; 9 | long long limit=1000000007; 10 | 11 | for(int i=0;i& neededTime) { 4 | int totalTime = 0; 5 | int i = 0, j = 0; 6 | while (i < neededTime.size() && j < neededTime.size()) { 7 | int currTotal = 0, currMax = 0; 8 | while (j < neededTime.size() && colors[i] == colors[j]) { 9 | currTotal += neededTime[j]; 10 | currMax = max(currMax, neededTime[j]); 11 | j++; 12 | } 13 | totalTime += currTotal - currMax; 14 | i = j; 15 | } 16 | return totalTime; 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /Medium/162._Find_Peak_Element.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findPeakElement(vector& nums) { 4 | int ans; 5 | int n=nums.size(); 6 | if(n==1 || nums[0]>nums[1]) return 0; 7 | else if(nums[n-1]>nums[n-2]) return n-1; 8 | for(int i=1;inums[i-1] && nums[i]>nums[i+1]) ans=i; 10 | } 11 | return ans; 12 | } 13 | }; 14 | -------------------------------------------------------------------------------- /Medium/1637. Widest Vertical Area Between Two Points Containing No Points: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxWidthOfVerticalArea(vector>& points) { 4 | int max_width = 0; 5 | sort(points.begin(), points.end(), [](const auto& a, const auto& b){ 6 | return a[0] < b[0]; 7 | }); 8 | for (int i=1; i& nums) { 4 | int n=nums.size(); 5 | if(n<2) return 0; 6 | sort(nums.begin(), nums.end()); 7 | int ans=-1; 8 | for(int i=0;ians) ans=diff; 11 | } 12 | return ans; 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /Medium/1642. Furthest Building You Can Reach: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int furthestBuilding(vector& heights, int bricks, int ladders) { 4 | // Priority Queue for storing the bricks used in each step in decreasing order (Max at top) 5 | priority_queue p; 6 | int i=0; 7 | for(i=0;i twoSum(vector& numbers, int target) { 4 | int n=numbers.size() ; 5 | vector ans ; 6 | int i=0 ,j=n-1 ; 7 | while(itarget)j-- ; 10 | else { 11 | ans.push_back(i+1) ; 12 | ans.push_back(j+1) ; 13 | break ; 14 | ++i ; 15 | } 16 | } 17 | return ans ; 18 | } 19 | }; 20 | -------------------------------------------------------------------------------- /Medium/2109._Adding_Spaces_to_a_String.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string addSpaces(string s, vector& spaces) { 4 | string ans=""; 5 | int j=0; 6 | for(int i=0;i rearrangeArray(vector& nums) { 4 | vector p; 5 | vector n; 6 | vector res; 7 | 8 | for(int i=0;i=0) p.push_back(nums[i]); 10 | else n.push_back(nums[i]); 11 | } 12 | for(int i=0;i first.rating; 14 | } 15 | }; 16 | class FoodRatings { 17 | public: 18 | unordered_map foodNameToInfo; 19 | unordered_map> cuisineNameToSortedInfo; 20 | 21 | FoodRatings(vector& foods, vector& cuisines, vector& ratings) { 22 | for (size_t i = 0; i < foods.size(); ++i) { 23 | foodNameToInfo.emplace(foods[i], Info(foods[i], cuisines[i], ratings[i])); 24 | cuisineNameToSortedInfo[cuisines[i]].emplace(foods[i], cuisines[i], ratings[i]); 25 | } 26 | } 27 | 28 | void changeRating(string food, int newRating) { 29 | Info& toUpdate = foodNameToInfo[food]; 30 | cuisineNameToSortedInfo[toUpdate.cuisine].erase(toUpdate); 31 | toUpdate.rating = newRating; 32 | cuisineNameToSortedInfo[toUpdate.cuisine].insert(toUpdate); 33 | 34 | } 35 | 36 | string highestRated(string cuisine) { 37 | return cuisineNameToSortedInfo.at(cuisine).begin()->name; 38 | 39 | } 40 | }; 41 | 42 | /** 43 | * Your FoodRatings object will be instantiated and called as such: 44 | * FoodRatings* obj = new FoodRatings(foods, cuisines, ratings); 45 | * obj->changeRating(food,newRating); 46 | * string param_2 = obj->highestRated(cuisine); 47 | */ 48 | -------------------------------------------------------------------------------- /Medium/237. Delete Node in a Linked List: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode(int x) : val(x), next(NULL) {} 7 | * }; 8 | */ 9 | class Solution { 10 | public: 11 | void deleteNode(ListNode* node) { 12 | node->val = node->next->val; 13 | node->next = node->next->next; 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /Medium/2482. Difference Between Ones and Zeros in Row and Column: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> onesMinusZeros(vector>& grid) { 4 | int m = grid.size(); 5 | int n = grid[0].size(); 6 | vector onesRow(m, 0); 7 | vector onesCol(n, 0); 8 | vector zerosRow(m, 0); 9 | vector zerosCol(n, 0); 10 | vector> diff(m, vector(n, 0)); 11 | 12 | for (int i = 0; i < m; ++i) { 13 | for (int j = 0; j < n; ++j) { 14 | if (grid[i][j] == 1) { 15 | onesRow[i]++; 16 | onesCol[j]++; 17 | } else { 18 | zerosRow[i]++; 19 | zerosCol[j]++; 20 | } 21 | } 22 | } 23 | for (int i = 0; i < m; ++i) { 24 | for (int j = 0; j < n; ++j) diff[i][j] = onesRow[i] + onesCol[j] - zerosRow[i] - zerosCol[j]; 25 | } 26 | return diff; 27 | } 28 | }; 29 | -------------------------------------------------------------------------------- /Medium/2486. Append Characters to String to Make Subsequence: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int appendCharacters(string s, string t) { 4 | int i=0, j=0; 5 | int ss=s.size(); 6 | int st=t.size(); 7 | while(i nums = {1,9,10,36,45,55,82,91,99,100,235,297,369,370,379,414, 5 | 657,675,703,756,792,909,918,945,964,990,991,999,1000}; 6 | int sum=0; 7 | for(int i=0;i& nums) { 4 | unordered_set seen; 5 | for (int num : nums) { 6 | if (seen.find(num) != seen.end()) return num; 7 | seen.insert(num); 8 | } 9 | return -1; 10 | } 11 | }; 12 | -------------------------------------------------------------------------------- /Medium/29. Divide Two Integers: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int divide(int dividend, int divisor) { 4 | if((long long int)dividend/divisor>pow(2,31)-1){return pow(2,31)-1;} 5 | if((long long int)dividend/divisor<(-1)*(pow(2,31))){return -1*pow(2,31);} 6 | return dividend/divisor; 7 | 8 | } 9 | }; 10 | 11 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 12 | 13 | class Solution { 14 | public: 15 | int divide(int dividend, int divisor) { 16 | if (dividend == 0) return 0; 17 | if (dividend == INT_MIN && divisor == -1) return INT_MAX; 18 | else if (dividend == INT_MIN && divisor == 1) return INT_MIN; 19 | if(divisor==1) return dividend; 20 | else if(divisor==-1) return -dividend; 21 | long long i=0; 22 | long long abs_dividend = llabs(static_cast(dividend)); 23 | long long abs_divisor = llabs(static_cast(divisor)); 24 | while(abs_dividend>=abs_divisor){ 25 | abs_dividend-=abs_divisor; 26 | i++; 27 | } 28 | if(dividend>0 && divisor<0 || dividend<0 && divisor>0) i*=-1; 29 | return static_cast (i); 30 | } 31 | }; 32 | -------------------------------------------------------------------------------- /Medium/396. Rotate Function: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxRotateFunction(vector& nums) { 4 | int n=nums.size(); 5 | int ans=0; 6 | int sum=0; 7 | int rt_sum = 0; 8 | for(int i=0;i= 1; i--) { 14 | rt_sum = rt_sum + sum - n * nums[i]; 15 | ans = max(ans, rt_sum); 16 | } 17 | return ans; 18 | } 19 | }; 20 | -------------------------------------------------------------------------------- /Medium/400. Nth Digit: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findNthDigit(int n) { 4 | long long base = 9, digits = 1; 5 | while (n - base * digits > 0) { 6 | n -= base * digits; 7 | base *= 10; 8 | digits++; 9 | } 10 | int num = pow(10, digits - 1) + (n - 1) / digits; 11 | int digitIdx = (n - 1) % digits; 12 | string numStr = to_string(num); 13 | return numStr[digitIdx] - '0'; 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /Medium/43._Multiply_Strings.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string multiply(string num1, string num2) { 4 | if (num1 == "0" || num2 == "0") return "0"; 5 | string ans; 6 | int n1 = num1.size(); 7 | int n2 = num2.size(); 8 | vector res(n1+n2, 0); 9 | for (int i=n1-1;i>=0;--i){ 10 | for (int j=n2-1;j>=0;--j){ 11 | int product = (num1[i] - '0')*(num2[j] - '0'); 12 | int p1=i+j; 13 | int p2=i+j+1; 14 | int sum=product+res[p2]; 15 | res[p2]=sum%10; 16 | res[p1]+=sum/10; 17 | } 18 | } 19 | for(int num : res){ 20 | if (!(ans.empty() && num==0)) ans.push_back(num + '0'); 21 | } 22 | return ans; 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /Medium/451. Sort Characters By Frequency: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string frequencySort(string s) { 4 | unordered_map freqMap; 5 | for (char c : s) freqMap[c]++; 6 | auto customSort = [&](char a, char b) { 7 | return freqMap[a] > freqMap[b] || (freqMap[a] == freqMap[b] && a < b); 8 | }; 9 | sort(s.begin(), s.end(), customSort); 10 | return s; 11 | } 12 | }; 13 | -------------------------------------------------------------------------------- /Medium/46. Permutations: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> res; 4 | void generate_all_possible_permutations(vector nums, int start){ 5 | if(start==nums.size()-1){ 6 | res.push_back(nums); 7 | return ; 8 | } 9 | for(int i=start;i> permute(vector& nums) { 16 | generate_all_possible_permutations(nums, 0); 17 | return res; 18 | } 19 | }; 20 | -------------------------------------------------------------------------------- /Medium/50. Pow(x, n): -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | double myPow(double x, int n) { 4 | double ans ; 5 | ans=pow(x,n) ; 6 | return ans ; 7 | } 8 | }; 9 | 10 | /////////////////////////////////////// 11 | *Other Approach:* 12 | 13 | class Solution { 14 | public: 15 | double myPow(double x, int n) { 16 | if (n == 0) return 1.0; 17 | double halfPow = myPow(x, n / 2); 18 | if (n % 2 == 0) return halfPow * halfPow; 19 | else return (n > 0) ? x * halfPow*halfPow : (1 / x)*halfPow*halfPow; 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /Medium/53._Maximum_Subarray.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxSubArray(vector& nums) { 4 | int n=nums.size(); 5 | int current_sum=0; 6 | int max_sum=INT_MIN; 7 | 8 | for(int i=0;i& timePoints) { 10 | int n=timePoints.size(); 11 | int count=0; 12 | int min_diff=INT_MAX; 13 | vector minutes; 14 | for(int i=0;i> merge(vector>& intervals) { 4 | sort(intervals.begin(), intervals.end()); 5 | vector> res; 6 | for (int i = 0; i < intervals.size(); i++) { 7 | if (res.empty() || intervals[i][0] > res.back()[1]) { 8 | res.push_back(intervals[i]); 9 | } else { 10 | res.back()[1] = max(res.back()[1], intervals[i][1]); 11 | } 12 | } 13 | return res; 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /Medium/7. Reverse Integer: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int reverse(int x) { 4 | long R = 0; 5 | while (x != 0) { 6 | R = 10 * R + x % 10; 7 | x /= 10; 8 | } 9 | return (R > INT_MAX || R < INT_MIN) ? 0 : R; 10 | } 11 | }; 12 | -------------------------------------------------------------------------------- /Medium/7._Reverse_Integer.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int ft_stoi(string str) 4 | { 5 | long long ans=0; 6 | int sign=1; 7 | if(str[0]=='-') {sign=-1;str.erase(0, 1);} 8 | std::reverse(str.begin(), str.end()); 9 | for (char ch : str) { 10 | int dig=ch-'0'; 11 | if(ans>(INT_MAX-dig)/10) return 0; 12 | ans=ans*10+dig; 13 | } 14 | return sign*ans; 15 | } 16 | int reverse(int x) { 17 | return ft_stoi(to_string(x)); 18 | } 19 | }; 20 | -------------------------------------------------------------------------------- /Medium/74._Search_a_2D_Matrix.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int binarySearch(vector vec, int target) { 4 | int low = 0, high = vec.size() - 1; 5 | while (low <= high) { 6 | int mid = low + (high - low) / 2; 7 | if (vec[mid] == target) return mid; 8 | else if (vec[mid] < target) low = mid + 1; 9 | else high = mid - 1; 10 | } 11 | return -1; 12 | } 13 | bool searchMatrix(vector>& matrix, int target) { 14 | bool ans=false; 15 | int n=matrix.size(); 16 | for(int i=0;i& nums) { 4 | int n=nums.size(); 5 | for(int i=0;i& arr) { 4 | int n=arr.size(); 5 | int ans=0; 6 | int tmp=arr[0]; 7 | for(int i=0;i& nums, vector>& result, vector& current, int start) { 4 | result.push_back(current); 5 | for (int i = start; i < nums.size(); ++i) { 6 | current.push_back(nums[i]); 7 | backtrack(nums, result, current, i + 1); 8 | current.pop_back(); 9 | } 10 | } 11 | vector> subsets(vector& nums) { 12 | int n=nums.size(); 13 | //int total_subs=1 << n; 14 | vector> res; 15 | vector sub; 16 | backtrack(nums, res, sub, 0); 17 | return res; 18 | } 19 | }; 20 | -------------------------------------------------------------------------------- /Medium/8. String to Integer (atoi): -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int myAtoi(string s) { 4 | long result = 0; 5 | int sign = 1; 6 | int i = 0; 7 | while (inumeric_limits::max()) { 14 | return numeric_limits::max(); 15 | }else if(result*sign::min()) return numeric_limits::min(); 16 | } 17 | return static_cast(result * sign); 18 | } 19 | }; 20 | -------------------------------------------------------------------------------- /Medium/81. Search in Rotated Sorted Array II: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool search(vector& nums, int target) { 4 | if(find(nums.begin(), nums.end(), target)!=nums.end()) return true; 5 | return false; 6 | } 7 | }; 8 | -------------------------------------------------------------------------------- /Medium/881. Boats to Save People: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numRescueBoats(vector& people, int limit) { 4 | sort(people.begin(), people.end()); 5 | int n=people.size(); 6 | int l=0, r=n-1; 7 | int count=0; 8 | while(l<=r){ 9 | if(people[l]+people[r]<=limit) l++; 10 | r--; 11 | count++; 12 | } 13 | return count; 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /Medium/91. Decode Ways: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numDecodings(string s) { 4 | int n=s.size(); 5 | vector dp(n+1, 0); 6 | dp[n]=1; 7 | for(int i=n-1;i>=0;--i){ 8 | if(s[i]!='0') dp[i]+=dp[i+1]; 9 | if(i+1