├── .gitignore ├── Stack ├── dailyTemperatures.cpp ├── backspaceStringCompare.cpp └── stackUsingQueues.cpp ├── 1827-invalid-tweets └── 1827-invalid-tweets.sql ├── 0584-find-customer-referee └── 0584-find-customer-referee.sql ├── 0624-maximum-distance-in-arrays ├── README.md └── 0624-maximum-distance-in-arrays.cpp ├── 1258-article-views-i └── 1258-article-views-i.sql ├── 0595-big-countries └── 0595-big-countries.sql ├── 0258-add-digits ├── 0258-add-digits.cpp └── README.md ├── 0620-not-boring-movies └── 0620-not-boring-movies.sql ├── 1908-recyclable-and-low-fat-products └── 1908-recyclable-and-low-fat-products.sql ├── 0577-employee-bonus └── 0577-employee-bonus.sql ├── 0197-rising-temperature └── 0197-rising-temperature.sql ├── 1509-replace-employee-id-with-the-unique-identifier └── 1509-replace-employee-id-with-the-unique-identifier.sql ├── 0570-managers-with-at-least-5-direct-reports └── 0570-managers-with-at-least-5-direct-reports.sql ├── 2087-confirmation-rate └── 2087-confirmation-rate.sql ├── 0476-number-complement └── 0476-number-complement.cpp ├── LinkedList ├── tempCodeRunnerFile.cpp ├── reverseLL.cpp ├── MiddleOfLL.cpp ├── BinaryToInteger.cpp ├── LinkedListCycle.cpp ├── removeLLelements.cpp ├── PalindromeLL.cpp ├── RemoveDuplicatesFromSortedLL.cpp ├── sortList.cpp ├── reverseLL2.cpp ├── CopyListWithRandomPtrs.cpp ├── intersectionOf2LL.cpp └── addNumbers.cpp ├── 0338-counting-bits └── 0338-counting-bits.cpp ├── 2732-counter └── 2732-counter.js ├── 1724-customer-who-visited-but-did-not-make-any-transactions └── 1724-customer-who-visited-but-did-not-make-any-transactions.sql ├── 2809-create-hello-world-function └── 2809-create-hello-world-function.js ├── Arrays ├── MajorityElement.cpp ├── BuyAndSellStocks2.cpp ├── RemoveDuplicates.cpp ├── BuyAndSellStocks.cpp ├── containerWithMostWater.cpp ├── MoveZeros.cpp ├── findAllDuplicates.cpp ├── largestSubarrayWithZeroSum.cpp ├── maxPointFromCard.cpp ├── findPairGivenDifference.cpp ├── SlidingWindow │ ├── subArraySumDivsibleByK.cpp │ └── SubArraySumEqualsK.cpp ├── sortColors.cpp ├── removeKdigits.cpp ├── MergeSortedArray.cpp ├── checkIfReversingMakesSorted.cpp ├── ChocolateDistribution.cpp ├── TwoSum.cpp ├── 3Sum.cpp └── FindDuplicate.cpp ├── 0263-ugly-number └── 0263-ugly-number.cpp ├── 1801-average-time-of-process-per-machine └── 1801-average-time-of-process-per-machine.sql ├── Mathamatical ├── countingBits.cpp ├── permutationsInArray.cpp ├── maxProductOf3numbers.cpp ├── ExcelSheetColTitle.cpp └── addBinary.cpp ├── Greedy ├── minCostAquiringCoins.cpp └── minCostAcquiringCoins.cpp ├── DP ├── climbingStairs.cpp └── longestCommonSubstring.cpp ├── 1013-fibonacci-number └── 1013-fibonacci-number.cpp ├── Strings ├── duplicateCharacters.cpp ├── firstIndexOfFirstOccurence.cpp ├── longestCommonPrefix.cpp ├── groupAnagrams.cpp ├── IntegerToroman.cpp ├── simplifyPath.cpp ├── reverseWords.cpp ├── ValidParenthesis.cpp └── validPalindrome.cpp ├── 0215-kth-largest-element-in-an-array ├── 0215-kth-largest-element-in-an-array.cpp └── README.md ├── 2727-number-of-senior-citizens └── 2727-number-of-senior-citizens.cpp ├── 1415-students-and-examinations └── 1415-students-and-examinations.sql ├── 1960-check-if-the-sentence-is-pangram └── 1960-check-if-the-sentence-is-pangram.cpp ├── 3600-find-the-k-th-character-in-string-game-i └── 3600-find-the-k-th-character-in-string-game-i.cpp ├── 1666-make-the-string-great └── 1666-make-the-string-great.cpp ├── 0153-find-minimum-in-rotated-sorted-array └── 0153-find-minimum-in-rotated-sorted-array.cpp ├── 0214-shortest-palindrome ├── 0214-shortest-palindrome.cpp └── README.md ├── 0541-reverse-string-ii ├── 0541-reverse-string-ii.cpp └── README.md ├── 2323-minimum-bit-flips-to-convert-number └── 2323-minimum-bit-flips-to-convert-number.cpp ├── Trees ├── countNodesInRange.cpp ├── medianOfBST.cpp ├── InvertBT.cpp ├── SameTree.cpp └── diameterOfBT.cpp ├── 0026-remove-duplicates-from-sorted-array └── 0026-remove-duplicates-from-sorted-array.cpp ├── 0462-minimum-moves-to-equal-array-elements-ii └── 0462-minimum-moves-to-equal-array-elements-ii.cpp ├── 0080-remove-duplicates-from-sorted-array-ii └── 0080-remove-duplicates-from-sorted-array-ii.cpp ├── 0219-contains-duplicate-ii └── 0219-contains-duplicate-ii.cpp ├── 2006-find-the-student-that-will-replace-the-chalk └── 2006-find-the-student-that-will-replace-the-chalk.cpp ├── 0378-kth-smallest-element-in-a-sorted-matrix └── 0378-kth-smallest-element-in-a-sorted-matrix.cpp ├── 2813-to-be-or-not-to-be └── 2813-to-be-or-not-to-be.js ├── 1256-rank-transform-of-an-array └── 1256-rank-transform-of-an-array.cpp ├── 2076-sum-of-digits-of-string-after-convert └── 2076-sum-of-digits-of-string-after-convert.cpp ├── 2132-convert-1d-array-into-2d-array └── 2132-convert-1d-array-into-2d-array.cpp ├── 2163-kth-distinct-string-in-an-array └── 2163-kth-distinct-string-in-an-array.cpp ├── 0096-unique-binary-search-trees ├── 0096-unique-binary-search-trees.cpp └── README.md ├── 1556-make-two-arrays-equal-by-reversing-subarrays └── 1556-make-two-arrays-equal-by-reversing-subarrays.cpp ├── 3275-minimum-number-of-pushes-to-type-word-i └── 3275-minimum-number-of-pushes-to-type-word-i.cpp ├── 3276-minimum-number-of-pushes-to-type-word-ii └── 3276-minimum-number-of-pushes-to-type-word-ii.cpp ├── 0386-lexicographical-numbers ├── 0386-lexicographical-numbers.cpp └── README.md ├── 0494-target-sum └── 0494-target-sum.cpp ├── 2095-minimum-number-of-swaps-to-make-the-string-balanced └── 2095-minimum-number-of-swaps-to-make-the-string-balanced.cpp ├── 0078-subsets ├── 0078-subsets.cpp └── README.md ├── 1700-minimum-time-to-make-rope-colorful └── 1700-minimum-time-to-make-rope-colorful.cpp ├── 0152-maximum-product-subarray └── 0152-maximum-product-subarray.cpp ├── 0048-rotate-image └── 0048-rotate-image.cpp ├── 0134-gas-station └── 0134-gas-station.cpp ├── 0957-minimum-add-to-make-parentheses-valid └── 0957-minimum-add-to-make-parentheses-valid.cpp ├── 0966-binary-subarrays-with-sum └── 0966-binary-subarrays-with-sum.cpp ├── 1002-maximum-width-ramp └── 1002-maximum-width-ramp.cpp ├── 0937-online-stock-span └── 0937-online-stock-span.cpp ├── 1620-check-if-array-pairs-are-divisible-by-k └── 1620-check-if-array-pairs-are-divisible-by-k.cpp ├── 2155-find-missing-observations └── 2155-find-missing-observations.cpp ├── 0090-subsets-ii ├── 0090-subsets-ii.cpp └── README.md ├── 2503-longest-subarray-with-maximum-bitwise-and └── 2503-longest-subarray-with-maximum-bitwise-and.cpp ├── 2789-counter-ii └── 2789-counter-ii.js ├── 0322-coin-change └── 0322-coin-change.cpp ├── 0739-daily-temperatures └── 0739-daily-temperatures.cpp ├── backtracking └── Subsets.cpp ├── 0046-permutations ├── 0046-permutations.cpp └── README.md ├── 0264-ugly-number-ii ├── 0264-ugly-number-ii.cpp └── README.md ├── 0209-minimum-size-subarray-sum └── 0209-minimum-size-subarray-sum.cpp ├── 0948-sort-an-array └── 0948-sort-an-array.cpp ├── 1756-minimum-deletions-to-make-string-balanced └── 1756-minimum-deletions-to-make-string-balanced.cpp ├── 0091-decode-ways └── 0091-decode-ways.cpp ├── 0650-2-keys-keyboard └── 0650-2-keys-keyboard.cpp ├── Graphs ├── dfs.cpp ├── bfs.cpp ├── BipartiteGraph.cpp ├── numberOfIslands.cpp └── floodFill.cpp ├── 0077-combinations ├── 0077-combinations.cpp └── README.md ├── 1615-range-sum-of-sorted-subarray-sums └── 1615-range-sum-of-sorted-subarray-sums.cpp ├── 0303-range-sum-query-immutable └── 0303-range-sum-query-immutable.cpp ├── 0719-find-k-th-smallest-pair-distance └── 0719-find-k-th-smallest-pair-distance.cpp ├── 0239-sliding-window-maximum └── 0239-sliding-window-maximum.cpp ├── 2488-divide-intervals-into-minimum-number-of-groups └── 2488-divide-intervals-into-minimum-number-of-groups.cpp ├── 2800-minimum-string-length-after-removing-substrings └── 2800-minimum-string-length-after-removing-substrings.cpp ├── 0713-subarray-product-less-than-k └── 0713-subarray-product-less-than-k.cpp ├── 0718-maximum-length-of-repeated-subarray ├── 0718-maximum-length-of-repeated-subarray.cpp └── README.md ├── 1028-interval-list-intersections └── 1028-interval-list-intersections.cpp ├── 1781-check-if-two-string-arrays-are-equivalent └── 1781-check-if-two-string-arrays-are-equivalent.cpp ├── 0789-kth-largest-element-in-a-stream └── 0789-kth-largest-element-in-a-stream.cpp ├── 1622-max-value-of-equation └── 1622-max-value-of-equation.cpp ├── 2581-divide-players-into-teams-of-equal-skill └── 2581-divide-players-into-teams-of-equal-skill.cpp ├── 0729-my-calendar-i └── 0729-my-calendar-i.cpp ├── 1435-xor-queries-of-a-subarray └── 1435-xor-queries-of-a-subarray.cpp ├── 0041-first-missing-positive └── 0041-first-missing-positive.cpp ├── 0920-uncommon-words-from-two-sentences └── 0920-uncommon-words-from-two-sentences.cpp ├── 1786-count-the-number-of-consistent-strings └── 1786-count-the-number-of-consistent-strings.cpp ├── Sorting └── minSwapsToSort.cpp ├── 1813-maximum-erasure-value └── 1813-maximum-erasure-value.cpp ├── 0300-longest-increasing-subsequence └── 0300-longest-increasing-subsequence.cpp ├── 3519-find-the-number-of-winning-players └── 3519-find-the-number-of-winning-players.cpp ├── 0003-longest-substring-without-repeating-characters └── 0003-longest-substring-without-repeating-characters.cpp ├── 0104-maximum-depth-of-binary-tree ├── 0104-maximum-depth-of-binary-tree.cpp └── README.md ├── 0236-lowest-common-ancestor-of-a-binary-tree └── 0236-lowest-common-ancestor-of-a-binary-tree.cpp ├── 0151-reverse-words-in-a-string └── 0151-reverse-words-in-a-string.cpp ├── 1333-sort-the-jumbled-numbers └── 1333-sort-the-jumbled-numbers.cpp ├── 1792-find-the-most-competitive-subsequence └── 1792-find-the-most-competitive-subsequence.cpp ├── 2755-extra-characters-in-a-string └── 2755-extra-characters-in-a-string.cpp ├── 0235-lowest-common-ancestor-of-a-binary-search-tree └── 0235-lowest-common-ancestor-of-a-binary-search-tree.cpp ├── 0347-top-k-frequent-elements └── 0347-top-k-frequent-elements.cpp ├── 0617-merge-two-binary-trees └── 0617-merge-two-binary-trees.cpp ├── 0539-minimum-time-difference ├── 0539-minimum-time-difference.cpp └── README.md ├── 0416-partition-equal-subset-sum ├── 0416-partition-equal-subset-sum.cpp └── README.md ├── 0148-sort-list └── 0148-sort-list.cpp ├── 1823-determine-if-string-halves-are-alike └── 1823-determine-if-string-halves-are-alike.cpp ├── 0145-binary-tree-postorder-traversal └── 0145-binary-tree-postorder-traversal.cpp ├── 0776-n-ary-tree-postorder-traversal └── 0776-n-ary-tree-postorder-traversal.cpp ├── 1320-remove-all-adjacent-duplicates-in-string-ii └── 1320-remove-all-adjacent-duplicates-in-string-ii.cpp ├── 0039-combination-sum └── 0039-combination-sum.cpp ├── 1770-minimum-deletions-to-make-character-frequencies-unique └── 1770-minimum-deletions-to-make-character-frequencies-unique.cpp ├── 3510-maximize-the-total-height-of-unique-towers └── 3510-maximize-the-total-height-of-unique-towers.cpp ├── 0230-kth-smallest-element-in-a-bst └── 0230-kth-smallest-element-in-a-bst.cpp ├── 0440-k-th-smallest-in-lexicographical-order ├── 0440-k-th-smallest-in-lexicographical-order.cpp └── README.md ├── 0101-symmetric-tree ├── 0101-symmetric-tree.cpp └── README.md ├── 0975-range-sum-of-bst └── 0975-range-sum-of-bst.cpp ├── 1923-sentence-similarity-iii └── 1923-sentence-similarity-iii.cpp ├── 0921-spiral-matrix-iii └── 0921-spiral-matrix-iii.cpp ├── 0150-evaluate-reverse-polish-notation └── 0150-evaluate-reverse-polish-notation.cpp ├── 0984-most-stones-removed-with-same-row-or-column └── 0984-most-stones-removed-with-same-row-or-column.cpp ├── 0059-spiral-matrix-ii └── README.md ├── 0394-decode-string └── 0394-decode-string.cpp ├── 0424-longest-repeating-character-replacement └── 0424-longest-repeating-character-replacement.cpp ├── 0778-reorganize-string ├── README.md └── 0778-reorganize-string.cpp ├── 1511-count-number-of-teams └── 1511-count-number-of-teams.cpp ├── 1250-longest-common-subsequence └── 1250-longest-common-subsequence.cpp ├── 0526-beautiful-arrangement └── 0526-beautiful-arrangement.cpp ├── 0083-remove-duplicates-from-sorted-list └── 0083-remove-duplicates-from-sorted-list.cpp ├── 0098-validate-binary-search-tree └── 0098-validate-binary-search-tree.cpp ├── 0474-ones-and-zeroes └── 0474-ones-and-zeroes.cpp ├── 1196-filling-bookcase-shelves └── 1196-filling-bookcase-shelves.cpp ├── 1694-make-sum-divisible-by-p └── 1694-make-sum-divisible-by-p.cpp ├── 0567-permutation-in-string └── 0567-permutation-in-string.cpp ├── 0040-combination-sum-ii └── 0040-combination-sum-ii.cpp ├── 0064-minimum-path-sum └── 0064-minimum-path-sum.cpp ├── 0110-balanced-binary-tree └── 0110-balanced-binary-tree.cpp ├── 1402-count-square-submatrices-with-all-ones └── 1402-count-square-submatrices-with-all-ones.cpp ├── 0112-path-sum └── 0112-path-sum.cpp ├── 0131-palindrome-partitioning ├── README.md └── 0131-palindrome-partitioning.cpp ├── 0438-find-all-anagrams-in-a-string └── 0438-find-all-anagrams-in-a-string.cpp ├── queue └── queueUsingStacks.cpp ├── 0404-sum-of-left-leaves ├── 0404-sum-of-left-leaves.cpp └── README.md ├── 0664-strange-printer └── 0664-strange-printer.cpp ├── 2255-minimum-swaps-to-group-all-1s-together-ii └── 2255-minimum-swaps-to-group-all-1s-together-ii.cpp ├── 0108-convert-sorted-array-to-binary-search-tree └── 0108-convert-sorted-array-to-binary-search-tree.cpp ├── 0307-range-sum-query-mutable └── 0307-range-sum-query-mutable.cpp ├── 0890-lemonade-change └── 0890-lemonade-change.cpp ├── 0530-minimum-absolute-difference-in-bst └── 0530-minimum-absolute-difference-in-bst.cpp ├── 1497-design-a-stack-with-increment-operation └── 1497-design-a-stack-with-increment-operation.cpp ├── 0257-binary-tree-paths ├── 0257-binary-tree-paths.cpp └── README.md ├── 0273-integer-to-english-words └── README.md ├── 0042-trapping-rain-water └── 0042-trapping-rain-water.cpp ├── 0943-sum-of-subarray-minimums ├── 0943-sum-of-subarray-minimums.cpp └── README.md ├── 0034-find-first-and-last-position-of-element-in-sorted-array └── 0034-find-first-and-last-position-of-element-in-sorted-array.cpp ├── 0086-partition-list └── 0086-partition-list.cpp ├── 0435-non-overlapping-intervals └── 0435-non-overlapping-intervals.cpp ├── 0095-unique-binary-search-trees-ii └── README.md ├── 0199-binary-tree-right-side-view └── 0199-binary-tree-right-side-view.cpp ├── 3501-delete-nodes-from-linked-list-present-in-array └── 3501-delete-nodes-from-linked-list-present-in-array.cpp ├── Searching └── BooksAllocation.cpp ├── 0082-remove-duplicates-from-sorted-list-ii └── 0082-remove-duplicates-from-sorted-list-ii.cpp ├── 0410-split-array-largest-sum └── 0410-split-array-largest-sum.cpp ├── 1762-furthest-building-you-can-reach └── 1762-furthest-building-you-can-reach.cpp ├── 2067-maximum-number-of-points-with-cost └── 2067-maximum-number-of-points-with-cost.cpp ├── 0698-partition-to-k-equal-sum-subsets └── README.md ├── 0033-search-in-rotated-sorted-array └── 0033-search-in-rotated-sorted-array.cpp ├── 0731-my-calendar-ii └── 0731-my-calendar-ii.cpp └── 0076-minimum-window-substring └── 0076-minimum-window-substring.cpp /.gitignore: -------------------------------------------------------------------------------- 1 | .vscode 2 | .exe -------------------------------------------------------------------------------- /Stack/dailyTemperatures.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | 6 | return 0; 7 | } -------------------------------------------------------------------------------- /1827-invalid-tweets/1827-invalid-tweets.sql: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | SELECT tweet_id FROM Tweets WHERE length(content) > 15; -------------------------------------------------------------------------------- /0584-find-customer-referee/0584-find-customer-referee.sql: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | 3 | SELECT name FROM Customer WHERE referee_id != 2 or referee_id is null; -------------------------------------------------------------------------------- /0624-maximum-distance-in-arrays/README.md: -------------------------------------------------------------------------------- 1 |

624. Maximum Distance in Arrays

Medium


null -------------------------------------------------------------------------------- /1258-article-views-i/1258-article-views-i.sql: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | SELECT DISTINCT author_id as id FROM Views WHERE author_id=viewer_id ORDER BY author_id; -------------------------------------------------------------------------------- /0595-big-countries/0595-big-countries.sql: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | 3 | SELECT name, population, area FROM WORLD WHERE area >= 3000000 or population >= 25000000; -------------------------------------------------------------------------------- /0258-add-digits/0258-add-digits.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int addDigits(int num) { 4 | if(num == 0) return 0; 5 | return num%9 == 0 ? 9 : num%9; 6 | } 7 | }; -------------------------------------------------------------------------------- /0620-not-boring-movies/0620-not-boring-movies.sql: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | select * from Cinema 3 | where id%2 = 1 and description not in ('boring') 4 | order by rating desc; -------------------------------------------------------------------------------- /1908-recyclable-and-low-fat-products/1908-recyclable-and-low-fat-products.sql: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | 3 | SELECT product_id FROM Products Where low_fats='Y' and recyclable='Y'; -------------------------------------------------------------------------------- /0577-employee-bonus/0577-employee-bonus.sql: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | SELECT e.name, b.bonus FROM 3 | Employee as e 4 | LEFT JOIN Bonus as b 5 | ON e.empID = b.empID 6 | WHERE b.bonus < 1000 || bonus IS NULL; -------------------------------------------------------------------------------- /0197-rising-temperature/0197-rising-temperature.sql: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | SELECT w1.id 3 | FROM Weather as w1 JOIN Weather as w2 4 | ON datediff(w1.recordDate, w2.recordDate) = 1 5 | AND w1.temperature > w2.temperature; -------------------------------------------------------------------------------- /1509-replace-employee-id-with-the-unique-identifier/1509-replace-employee-id-with-the-unique-identifier.sql: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | SELECT u.unique_id, e.name FROM Employees as e 3 | LEFT JOIN EmployeeUNI as u ON e.id = u.id; -------------------------------------------------------------------------------- /0570-managers-with-at-least-5-direct-reports/0570-managers-with-at-least-5-direct-reports.sql: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | SELECT e1.name FROM Employee as e1 3 | JOIN Employee as e2 4 | ON e1.id = e2.managerId 5 | GROUP BY e2.managerId 6 | HAVING count(e2.managerId) >=5; -------------------------------------------------------------------------------- /2087-confirmation-rate/2087-confirmation-rate.sql: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | select s.user_id, round(avg(if(c.action = 'confirmed', 1, 0)),2) as confirmation_rate 3 | from signups as s 4 | left join confirmations as c 5 | on s.user_id = c.user_id 6 | group by s.user_id -------------------------------------------------------------------------------- /0476-number-complement/0476-number-complement.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findComplement(int num) { 4 | int bitLength = (int)(log2(num))+1; 5 | for(int i=0;inext; 6 | curr->next=prev; 7 | prev=curr; 8 | curr=next; 9 | } 10 | return prev; -------------------------------------------------------------------------------- /0338-counting-bits/0338-counting-bits.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector countBits(int n) { 4 | vector ans(n+1); 5 | for(int i=0;i<=n;i++) 6 | { 7 | if(i%2==0) ans[i]=ans[i/2]; 8 | else ans[i]=ans[i/2]+1; 9 | } 10 | return ans; 11 | } 12 | }; -------------------------------------------------------------------------------- /2732-counter/2732-counter.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number} n 3 | * @return {Function} counter 4 | */ 5 | var createCounter = function(n) { 6 | 7 | return function() { 8 | return n++; 9 | }; 10 | }; 11 | 12 | /** 13 | * const counter = createCounter(10) 14 | * counter() // 10 15 | * counter() // 11 16 | * counter() // 12 17 | */ -------------------------------------------------------------------------------- /1724-customer-who-visited-but-did-not-make-any-transactions/1724-customer-who-visited-but-did-not-make-any-transactions.sql: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | SELECT v.customer_id, count(v.visit_id) as count_no_trans 3 | FROM Visits as v 4 | LEFT JOIN Transactions as t 5 | ON v.visit_id = t.visit_id 6 | WHERE t.transaction_id IS NULL 7 | GROUP BY v.customer_id; -------------------------------------------------------------------------------- /2809-create-hello-world-function/2809-create-hello-world-function.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @return {Function} 3 | */ 4 | var createHelloWorld = function() { 5 | 6 | return function(...args) { 7 | const str = "Hello World"; 8 | return str; 9 | } 10 | }; 11 | 12 | /** 13 | * const f = createHelloWorld(); 14 | * f(); // "Hello World" 15 | */ -------------------------------------------------------------------------------- /Arrays/MajorityElement.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //1) using maps -> return max frequency element 5 | //2) using sorting 6 | class Solution { 7 | public: 8 | int majorityElement(vector& nums) { 9 | int n=nums.size(); 10 | sort(nums.begin(),nums.end()); 11 | return nums[n/2]; 12 | } 13 | }; -------------------------------------------------------------------------------- /0263-ugly-number/0263-ugly-number.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isUgly(int n) { 4 | while(n>1) 5 | { 6 | if(n%2 == 0) n= n/2; 7 | else if(n%3 == 0) n= n/3; 8 | else if(n%5 == 0) n= n/5; 9 | else return false; 10 | } 11 | return n==1 ? true: false; 12 | 13 | } 14 | }; -------------------------------------------------------------------------------- /1801-average-time-of-process-per-machine/1801-average-time-of-process-per-machine.sql: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | SELECT a1.machine_id , round(avg(a2.timestamp-a1.timestamp),3) as processing_time 3 | FROM activity a1 4 | JOIN activity a2 5 | ON a1.machine_id = a2.machine_id 6 | AND a1.process_id = a2.process_id 7 | AND a1.timestamp 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | vector countBits(int n) { 7 | vector ans(n+1); 8 | for(int i=0;i<=n;i++) 9 | { 10 | if(i%2==0) ans[i]=ans[i/2]; 11 | else ans[i]=ans[i/2]+1; 12 | } 13 | return ans; 14 | } 15 | }; -------------------------------------------------------------------------------- /Greedy/minCostAquiringCoins.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int minCost(int coin[], int n, int k) 5 | { 6 | sort(coin, coin + n); 7 | 8 | int coins_needed = ceil(1.0 * n / 9 | (k + 1)); 10 | 11 | int ans = 0; 12 | for (int i = 0; i <= coins_needed - 1; 13 | i++) 14 | ans += coin[i]; 15 | 16 | return ans; 17 | } 18 | -------------------------------------------------------------------------------- /DP/climbingStairs.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | int dp[46] = {0}; 7 | int climbStairs(int n) { 8 | if(n <=1) return 1; 9 | if(dp[n]) return dp[n]; 10 | int oneStep = climbStairs(n-1); 11 | int twoStep = climbStairs(n-2); 12 | 13 | return dp[n] = oneStep+twoStep; 14 | } 15 | }; -------------------------------------------------------------------------------- /1013-fibonacci-number/1013-fibonacci-number.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int fib(int n) { 4 | if(n == 0) return 0; 5 | if(n==1) return 1; 6 | vector dp(n+1); 7 | dp[0] = 0; 8 | dp[1] = 1; 9 | for(int i=2;i<=n;i++) 10 | { 11 | dp[i] = dp[i-1] + dp[i-2]; 12 | } 13 | return dp[n]; 14 | } 15 | }; -------------------------------------------------------------------------------- /Mathamatical/permutationsInArray.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | bool isPossible(long long a[], long long b[], int n, long long k) { 7 | sort(a, a+n); 8 | sort(b, b+n); 9 | for(int i=0;i 2 | using namespace std; 3 | 4 | void printDuplicate(string str) 5 | { 6 | unordered_map count; 7 | for (int i = 0; i < str.length(); i++) { 8 | count[str[i]]++; 9 | } 10 | for (auto it : count) { 11 | if (it.second > 1) 12 | cout << it.first << ", count = " << it.second 13 | << "\n"; 14 | } 15 | } -------------------------------------------------------------------------------- /0215-kth-largest-element-in-an-array/0215-kth-largest-element-in-an-array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findKthLargest(vector& nums, int k) { 4 | int n = nums.size(); 5 | priority_queue, greater> pq; 6 | for(auto x : nums) 7 | { 8 | pq.push(x); 9 | if(pq.size()>k) pq.pop(); 10 | } 11 | return pq.top(); 12 | } 13 | }; -------------------------------------------------------------------------------- /2727-number-of-senior-citizens/2727-number-of-senior-citizens.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int countSeniors(vector& details) { 4 | int n = details.size(); 5 | int count =0; 6 | for(int i=0;i60) count++; 11 | } 12 | return count; 13 | } 14 | }; -------------------------------------------------------------------------------- /Arrays/BuyAndSellStocks2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | int maxProfit(vector& prices) { 7 | int maxProfit = 0; 8 | for (int i = 1; i < prices.size(); i++) { 9 | if (prices[i] > prices[i - 1]) { 10 | maxProfit += prices[i] - prices[i - 1]; 11 | } 12 | } 13 | return maxProfit; 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /1415-students-and-examinations/1415-students-and-examinations.sql: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | SELECT s.student_id, s.student_name, sub.subject_name,count(e.subject_name) as attended_exams 3 | FROM Students s 4 | CROSS JOIN Subjects sub 5 | LEFT JOIN Examinations e 6 | ON s.student_id = e.student_id 7 | AND e.subject_name = sub.subject_name 8 | GROUP BY s.student_id, s.student_name, sub.subject_name 9 | ORDER BY s.student_id, s.student_name; -------------------------------------------------------------------------------- /1960-check-if-the-sentence-is-pangram/1960-check-if-the-sentence-is-pangram.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool checkIfPangram(string sentence) { 4 | int arr[26]; 5 | for(int i=0;i 2 | using namespace std; 3 | 4 | // sorting method 5 | // considering -ve numbers 6 | // negative + negative = positive 7 | class Solution { 8 | public: 9 | int maximumProduct(vector& nums) { 10 | int n = nums.size(); 11 | sort(nums.begin(), nums.end()); 12 | return max(nums[n - 1] * nums[n - 2] * nums[n - 3], nums[0] * nums[1] * nums[n - 1]); 13 | } 14 | 15 | }; -------------------------------------------------------------------------------- /3600-find-the-k-th-character-in-string-game-i/3600-find-the-k-th-character-in-string-game-i.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | char kthCharacter(int k) { 4 | string s = "a"; 5 | while(s.size() < k) 6 | { 7 | string temp = ""; 8 | for(char c : s) 9 | { 10 | temp += ++c; 11 | } 12 | s = s+ temp; 13 | } 14 | return s[k-1]; 15 | } 16 | }; -------------------------------------------------------------------------------- /1666-make-the-string-great/1666-make-the-string-great.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string makeGood(string s) { 4 | string res = ""; 5 | for(int i=0;i& nums) { 4 | int n = nums.size(); 5 | int s = 0, e=n-1; 6 | while(s nums[e]) 10 | { 11 | s = m+1; 12 | }else e = m; 13 | } 14 | return nums[e]; 15 | } 16 | }; -------------------------------------------------------------------------------- /0214-shortest-palindrome/0214-shortest-palindrome.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string shortestPalindrome(string s) { 4 | string rev = s; 5 | reverse(rev.begin(),rev.end()); 6 | 7 | for(int i=0;i0 || goal > 0) 6 | { 7 | if((start&1) != (goal&1)) 8 | { 9 | count++; 10 | } 11 | start = start>>1; 12 | goal = goal>>1; 13 | } 14 | return count; 15 | } 16 | }; -------------------------------------------------------------------------------- /Trees/countNodesInRange.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution{ 5 | public: 6 | int getCount(Node *root, int l, int h) 7 | { 8 | if(!root) return 0; 9 | 10 | int left = getCount(root->left,l,h); 11 | int right = getCount(root->right,l,h); 12 | if(root->data >= l && root->data <= h) 13 | { 14 | return 1 + left + right; 15 | } 16 | return left + right; 17 | } 18 | }; -------------------------------------------------------------------------------- /0026-remove-duplicates-from-sorted-array/0026-remove-duplicates-from-sorted-array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int removeDuplicates(vector& nums) { 4 | int n = nums.size(); 5 | int i=0; 6 | int j=1; 7 | while(j& nums) { 4 | sort(nums.begin(),nums.end()); 5 | int n = nums.size(); 6 | int mid = nums[n/2]; 7 | int count =0; 8 | for(int i=0;i 2 | using namespace std; 3 | 4 | // using two pointers 5 | class Solution { 6 | public: 7 | int removeDuplicates(vector& nums) { 8 | int i=0; 9 | int j=0; 10 | while(j& nums) { 4 | int n = nums.size(); 5 | if (n <= 2) return n; 6 | int i=2; 7 | for(int j=2;j& nums, int k) { 4 | int n = nums.size(); 5 | unordered_map mp; 6 | for(int i=0;i& chalk, int k) { 4 | int n = chalk.size(); 5 | long long sum = accumulate(chalk.begin(),chalk.end(),0LL); 6 | int rem = k%sum; 7 | int i=0; 8 | while(rem >= chalk[i]) 9 | { 10 | rem -= chalk[i]; 11 | i = (i+1) % n; 12 | } 13 | return i; 14 | } 15 | }; -------------------------------------------------------------------------------- /Arrays/BuyAndSellStocks.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | class Solution { 6 | public: 7 | int maxProfit(vector& prices) { 8 | int min_price =INT_MAX; 9 | int max_profit = 0; 10 | for(int i=0;i>& matrix, int k) { 4 | int n = matrix.size(); 5 | priority_queue pq; 6 | for(int i=0;ik) pq.pop(); 12 | } 13 | } 14 | return pq.top(); 15 | } 16 | }; -------------------------------------------------------------------------------- /2813-to-be-or-not-to-be/2813-to-be-or-not-to-be.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {string} val 3 | * @return {Object} 4 | */ 5 | var expect = function(val) { 6 | return { 7 | toBe : (v) => 8 | { 9 | if(val === v) return true; 10 | else throw new Error("Not Equal"); 11 | }, 12 | notToBe : (v) =>{ 13 | if(val !== v) return true; 14 | else throw new Error("Equal"); 15 | }}; 16 | }; 17 | 18 | /** 19 | * expect(5).toBe(5); // true 20 | * expect(5).notToBe(5); // throws "Equal" 21 | */ -------------------------------------------------------------------------------- /1256-rank-transform-of-an-array/1256-rank-transform-of-an-array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector arrayRankTransform(vector& arr) { 4 | unordered_map mp; 5 | set st(arr.begin(),arr.end()); 6 | int rank =1; 7 | for(int num : st) 8 | { 9 | mp[num] = rank; 10 | rank++; 11 | } 12 | for(int i=0;i 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | int maxArea(vector& height) { 7 | int n = height.size(); 8 | int i=0; 9 | int j=n-1; 10 | int maxi = 0; 11 | while(i> construct2DArray(vector& original, int m, int n) { 4 | if (m * n != original.size()) { 5 | return {}; 6 | } 7 | vector> v(m,vector(n)); 8 | for(int i=0;i& arr, int k) { 4 | int n = arr.size(); 5 | unordered_map mp; 6 | for(int i=0;i&dp){ 4 | if(n<=1){ 5 | return 1; 6 | } 7 | if(dp[n]!=-1){ 8 | return dp[n]; 9 | } 10 | int ans=0; 11 | for(int i=1;i<=n;i++){ 12 | ans+=solve(i-1,dp)*solve(n-i,dp); 13 | } 14 | return dp[n]=ans; 15 | } 16 | int numTrees(int n) { 17 | vectordp(n+1,-1); 18 | return solve(n,dp); 19 | } 20 | }; -------------------------------------------------------------------------------- /Mathamatical/ExcelSheetColTitle.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | //log(colNum) base 26 6 | class Solution { 7 | public: 8 | string convertToTitle(int columnNumber) { 9 | int colNum=columnNumber; 10 | string ans=""; 11 | while(colNum>0) 12 | { 13 | colNum--; // to handle remainder = 0 14 | int remainder = colNum%26; 15 | ans+= remainder+'A'; 16 | colNum = colNum/26; 17 | } 18 | reverse(ans.begin(),ans.end()); 19 | return ans; 20 | } 21 | }; -------------------------------------------------------------------------------- /1556-make-two-arrays-equal-by-reversing-subarrays/1556-make-two-arrays-equal-by-reversing-subarrays.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool canBeEqual(vector& target, vector& arr) { 4 | unordered_map freq; 5 | for(int i : arr) 6 | { 7 | freq[i]++; 8 | } 9 | for(int i : target) 10 | { 11 | freq[i]--; 12 | } 13 | for(auto p : freq) 14 | { 15 | if(p.second >= 1) return false; 16 | } 17 | return true; 18 | } 19 | }; -------------------------------------------------------------------------------- /3275-minimum-number-of-pushes-to-type-word-i/3275-minimum-number-of-pushes-to-type-word-i.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minimumPushes(string word) { 4 | vector mp(26,0); 5 | for(char ch : word) 6 | { 7 | mp[ch-'a']++; 8 | } 9 | sort(mp.begin(),mp.end(),greater()); 10 | int sum =0; 11 | for(int i=0;i<26;i++) 12 | { 13 | int freq = mp[i]; 14 | int press = i/8+1; 15 | sum+= press * freq; 16 | } 17 | return sum; 18 | } 19 | }; -------------------------------------------------------------------------------- /3276-minimum-number-of-pushes-to-type-word-ii/3276-minimum-number-of-pushes-to-type-word-ii.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minimumPushes(string word) { 4 | vector mp(26,0); 5 | for(char ch : word) 6 | { 7 | mp[ch-'a']++; 8 | } 9 | sort(mp.begin(),mp.end(),greater()); 10 | int sum =0; 11 | for(int i=0;i<26;i++) 12 | { 13 | int freq = mp[i]; 14 | int press = i/8+1; 15 | sum+= press * freq; 16 | } 17 | return sum; 18 | } 19 | }; -------------------------------------------------------------------------------- /Arrays/MoveZeros.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //1) using extra space 5 | 6 | //2) using 2 pointers 7 | class Solution { 8 | public: 9 | void moveZeroes(vector& nums) { 10 | int n = nums.size(); 11 | int k=0; 12 | for(int i=0;i &ans, int curr) 4 | { 5 | if(curr > n ) return; 6 | ans.push_back(curr); 7 | for(int i=0;i<=9;i++) 8 | { 9 | int currNum = curr*10 + i; 10 | solve(n,ans,currNum); 11 | } 12 | } 13 | vector lexicalOrder(int n) { 14 | vector ans; 15 | for(int i=1;i<=9;i++) 16 | { 17 | solve(n,ans,i); 18 | } 19 | return ans; 20 | } 21 | }; -------------------------------------------------------------------------------- /0494-target-sum/0494-target-sum.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | 4 | int solve(int idx, vector& nums,int sum, int target) 5 | { 6 | if(idx >= nums.size()) 7 | { 8 | if(sum == target) return 1; 9 | return 0; 10 | } 11 | 12 | int opt1 = solve(idx+1,nums,sum+nums[idx],target); 13 | int opt2 = solve(idx+1,nums,sum-nums[idx],target); 14 | 15 | return opt1+opt2; 16 | } 17 | int findTargetSumWays(vector& nums, int target) { 18 | 19 | return solve(0,nums,0,target);; 20 | } 21 | }; -------------------------------------------------------------------------------- /2095-minimum-number-of-swaps-to-make-the-string-balanced/2095-minimum-number-of-swaps-to-make-the-string-balanced.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minSwaps(string s) { 4 | if(s.size() == 0) return 0; 5 | 6 | stack st; 7 | for(int i=0;i> ans; 4 | void solve(vector& nums, vector &curr,int idx) 5 | { 6 | if(idx == nums.size()) 7 | { 8 | ans.push_back(curr); 9 | return; 10 | } 11 | 12 | curr.push_back(nums[idx]); 13 | solve(nums,curr,idx+1); 14 | curr.pop_back(); 15 | solve(nums,curr,idx+1); 16 | } 17 | vector> subsets(vector& nums) { 18 | vector curr; 19 | solve(nums,curr,0); 20 | return ans; 21 | } 22 | }; -------------------------------------------------------------------------------- /1700-minimum-time-to-make-rope-colorful/1700-minimum-time-to-make-rope-colorful.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minCost(string colors, vector& neededTime) { 4 | int n = colors.size(); 5 | int time =0; 6 | int prevMax = 0; 7 | 8 | for(int i=0;i0 && colors[i] != colors[i-1]){ 11 | prevMax=0; 12 | } 13 | 14 | int curr = neededTime[i]; 15 | time += min(prevMax, curr); 16 | prevMax = max(prevMax,curr); 17 | } 18 | return time; 19 | } 20 | }; -------------------------------------------------------------------------------- /Arrays/findAllDuplicates.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //1) sort the array and then check for duplicates 5 | //2) use maps to store the frequency 6 | 7 | class Solution { 8 | public: 9 | vector findDuplicates(vector& nums) { 10 | unordered_map mp; 11 | vector ans; 12 | for(int i=0;i& nums) { 4 | int n = nums.size(); 5 | int max_product = INT_MIN; 6 | int prefix =1; 7 | int suffix =1; 8 | for(int i=0;i 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | int strStr(string haystack, string needle) { 7 | int i,j; 8 | int len1=haystack.size(), len2=needle.size(); 9 | for (i = 0; i <= len1-len2; i++) { 10 | for (j = 0; j >& matrix) 4 | { 5 | int n = matrix.size(); 6 | for(int i=0;i>& matrix) { 15 | int n = matrix.size(); 16 | Transpose(matrix); 17 | for(int i=0;i& gas, vector& cost) { 4 | if (accumulate(gas.begin(), gas.end(), 0) < accumulate(cost.begin(), cost.end(), 0)) { 5 | return -1; 6 | } 7 | int n = gas.size(); 8 | int fuel =0; 9 | int ans =0; 10 | for(int i=0;i st; 6 | int count = 0; 7 | for(int i=0;i 0) return count+st.size(); 18 | return count; 19 | } 20 | }; -------------------------------------------------------------------------------- /0966-binary-subarrays-with-sum/0966-binary-subarrays-with-sum.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numSubarraysWithSum(vector& nums, int goal) { 4 | int n = nums.size(); 5 | unordered_map mp; 6 | mp[0] = 1; 7 | int sum = 0; 8 | int cnt =0; 9 | for(int i=0;i 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | int maxLen(vector& arr, int n) { 7 | unordered_map mp; // sum, index 8 | int sum =0; 9 | int ans =0; 10 | mp[0] = -1; 11 | for(int i=0;i& arr) { 4 | int n = arr.size(); 5 | int maxi = 0; 6 | int max_right[n]; 7 | max_right[n-1] = arr[n-1]; 8 | for(int i=n-2;i>=0;i--) 9 | { 10 | max_right[i] = max(max_right[i+1] ,arr[i]); 11 | } 12 | int i=0; 13 | int j=0; 14 | while(j> st; 4 | StockSpanner() { 5 | 6 | } 7 | 8 | int next(int price) { 9 | int span = 1; 10 | while (!st.empty() && st.top().first <= price) { 11 | span += st.top().second; 12 | st.pop(); 13 | } 14 | st.push({price, span}); 15 | return span; 16 | } 17 | }; 18 | 19 | /** 20 | * Your StockSpanner object will be instantiated and called as such: 21 | * StockSpanner* obj = new StockSpanner(); 22 | * int param_1 = obj->next(price); 23 | */ -------------------------------------------------------------------------------- /1620-check-if-array-pairs-are-divisible-by-k/1620-check-if-array-pairs-are-divisible-by-k.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool canArrange(vector& arr, int k) { 4 | int n = arr.size(); 5 | vector mp(k,0); 6 | for(int i=0;i missingRolls(vector& rolls, int mean, int n) { 4 | int m=rolls.size(),sumM=0; 5 | for(auto x:rolls) sumM+=x; 6 | int rhs=mean*(n+m); 7 | rhs-=sumM; 8 | int x=rhs/n,y=rhs%n; 9 | if(x<=0 || x>6) return {}; 10 | if(x==6 && y>0) return {}; 11 | vectorans(n); 12 | for(int i=0;i 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | int maxScore(vector& nums, int k) { 7 | int n = nums.size(); 8 | int lsum =0; 9 | int rsum =0; 10 | for(int i=0;i=0;i--) 17 | { 18 | lsum -= nums[i]; 19 | rsum += nums[r]; 20 | r--; 21 | maxSum = max(maxSum,lsum+rsum); 22 | } 23 | return maxSum; 24 | } 25 | }; -------------------------------------------------------------------------------- /0090-subsets-ii/0090-subsets-ii.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> ans; 4 | vector sub; 5 | void recur(vector& nums, int ind) 6 | { 7 | ans.push_back(sub); 8 | for(int i=ind;iind && nums[i] == nums[i-1]) continue; 11 | sub.push_back(nums[i]); 12 | recur(nums,i+1); 13 | sub.pop_back(); 14 | } 15 | } 16 | vector> subsetsWithDup(vector& nums) { 17 | sort(nums.begin(),nums.end()); 18 | recur(nums,0); 19 | return ans; 20 | } 21 | }; -------------------------------------------------------------------------------- /2503-longest-subarray-with-maximum-bitwise-and/2503-longest-subarray-with-maximum-bitwise-and.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int longestSubarray(vector& nums) { 4 | int n = nums.size(); 5 | int maxi = *max_element(nums.begin(), nums.end()); 6 | int max_ans = 0; 7 | int count = 0; 8 | 9 | for (int i = 0; i < n; i++) { 10 | if (nums[i] == maxi) { 11 | count++; 12 | max_ans = max(max_ans, count); 13 | } else { 14 | count = 0; 15 | } 16 | } 17 | 18 | return max_ans; 19 | } 20 | }; 21 | -------------------------------------------------------------------------------- /2789-counter-ii/2789-counter-ii.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {integer} init 3 | * @return { increment: Function, decrement: Function, reset: Function } 4 | */ 5 | var createCounter = function(init) { 6 | let curr = init; 7 | function increment(){ 8 | return ++curr; 9 | } 10 | function decrement(){ 11 | return --curr; 12 | } 13 | function reset(){ 14 | return curr = init; 15 | } 16 | 17 | return {increment, decrement, reset}; 18 | }; 19 | 20 | /** 21 | * const counter = createCounter(5) 22 | * counter.increment(); // 6 23 | * counter.reset(); // 5 24 | * counter.decrement(); // 4 25 | */ -------------------------------------------------------------------------------- /0322-coin-change/0322-coin-change.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int coinChange(vector& coins, int amount) { 4 | if(amount<1) return 0; 5 | vector dp(amount + 1, amount + 1); 6 | dp[0] = 0; 7 | 8 | for(int i=1;i<=amount;i++) 9 | { 10 | dp[i] = INT_MAX; 11 | 12 | for(int coin : coins) 13 | { 14 | if(coin <= i && dp[i-coin] != INT_MAX) 15 | { 16 | dp[i] = min(dp[i], 1 + dp[i-coin]); 17 | } 18 | } 19 | } 20 | if(dp[amount] == INT_MAX) return -1; 21 | return dp[amount]; 22 | } 23 | }; -------------------------------------------------------------------------------- /0739-daily-temperatures/0739-daily-temperatures.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector dailyTemperatures(vector& temperatures) { 4 | int n = temperatures.size(); 5 | vector answer(n); 6 | stack st; 7 | for(int i=n-1;i>=0;i--) 8 | { 9 | while(!st.empty() && temperatures[i] >= temperatures[st.top()] ) st.pop(); 10 | if(st.empty()) 11 | { 12 | answer[i]=0; 13 | }else{ 14 | answer[i] = st.top() - i; 15 | } 16 | st.push(i); 17 | } 18 | return answer; 19 | } 20 | }; -------------------------------------------------------------------------------- /backtracking/Subsets.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | vector> ans; 7 | void solve(vector& nums, int i, vector &temp) 8 | { 9 | if(i == nums.size()) 10 | { 11 | ans.push_back(temp); 12 | return; 13 | } 14 | temp.push_back(nums[i]); 15 | solve(nums,i+1,temp); 16 | temp.pop_back(); 17 | solve(nums,i+1,temp); 18 | } 19 | vector> subsets(vector& nums) { 20 | vector temp; 21 | solve(nums,0,temp); 22 | return ans; 23 | } 24 | }; -------------------------------------------------------------------------------- /0046-permutations/0046-permutations.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> ans; 4 | void solve(vector& nums,int idx) 5 | { 6 | if(idx == nums.size()) 7 | { 8 | ans.push_back(nums); 9 | return; 10 | } 11 | 12 | for(int i=idx;i> permute(vector& nums) { 21 | vector curr; 22 | solve(nums,0); 23 | return ans; 24 | } 25 | }; -------------------------------------------------------------------------------- /0264-ugly-number-ii/0264-ugly-number-ii.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int nthUglyNumber(int n) { 4 | int arr[n+1]; 5 | arr[1]=1; 6 | int i2 ,i3,i5; 7 | i2 = i3= i5 = 1; 8 | for(int i=2;i<=n;i++) 9 | { 10 | int i2Num = arr[i2]*2; 11 | int i3Num = arr[i3]*3; 12 | int i5Num = arr[i5]*5; 13 | 14 | int mini = min({i2Num, i3Num, i5Num}); 15 | arr[i] = mini; 16 | 17 | if(mini == i2Num) i2++; 18 | if(mini == i3Num) i3++; 19 | if(mini == i5Num) i5++; 20 | } 21 | return arr[n]; 22 | } 23 | }; -------------------------------------------------------------------------------- /Arrays/findPairGivenDifference.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | class Solution { 6 | public: 7 | int findPair(int n, int x, vector &arr) { 8 | sort(arr.begin(), arr.end()); 9 | 10 | int i = 0; 11 | int j = 1; 12 | 13 | while (i < n && j < n) { 14 | int diff = arr[j] - arr[i]; 15 | if (i != j && diff == x) { 16 | return 1; 17 | } else if (diff < x) { 18 | j++; 19 | } else { 20 | i++; 21 | } 22 | } 23 | 24 | return -1; 25 | } 26 | }; -------------------------------------------------------------------------------- /Mathamatical/addBinary.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | string addBinary(string a, string b) { 7 | string ans = ""; 8 | int i=0; 9 | char carry=0; 10 | while(i>& arrays) { 4 | int n = arrays.size(); 5 | int minVal = arrays[0][0]; 6 | int maxVal = arrays[0].back(); 7 | int maxDist = 0; 8 | 9 | for (int i = 1; i < n; ++i) { 10 | maxDist = max(maxDist, abs(arrays[i].back() - minVal)); 11 | maxDist = max(maxDist, abs(maxVal - arrays[i][0])); 12 | 13 | minVal = min(minVal, arrays[i][0]); 14 | maxVal = max(maxVal, arrays[i].back()); 15 | } 16 | 17 | return maxDist; 18 | } 19 | }; -------------------------------------------------------------------------------- /0209-minimum-size-subarray-sum/0209-minimum-size-subarray-sum.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minSubArrayLen(int target, vector& nums) { 4 | int n = nums.size(); 5 | int i=0; 6 | int j=0; 7 | int sum = 0; 8 | int ans = INT_MAX; 9 | while(j=target) 13 | { 14 | ans = min(ans,j-i+1); 15 | sum -= nums[i]; 16 | i++; 17 | } 18 | j++; 19 | } 20 | if(ans == INT_MAX) return 0; 21 | return ans; 22 | } 23 | }; -------------------------------------------------------------------------------- /0948-sort-an-array/0948-sort-an-array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector sortArray(vector& nums) { 4 | unordered_map mp; 5 | for( int n : nums) 6 | { 7 | mp[n]++; 8 | } 9 | int mini = *min_element(nums.begin(),nums.end()); 10 | int maxi = *max_element(nums.begin(),nums.end()); 11 | 12 | int i=0; 13 | for(int p = mini;p<=maxi;p++) 14 | { 15 | while(mp[p]>0) 16 | { 17 | nums[i] = p; 18 | i++; 19 | mp[p]--; 20 | } 21 | } 22 | return nums; 23 | } 24 | }; -------------------------------------------------------------------------------- /1756-minimum-deletions-to-make-string-balanced/1756-minimum-deletions-to-make-string-balanced.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minimumDeletions(string s) { 4 | int n = s.size(); 5 | int count=0; 6 | stack st; 7 | for(int i=0;i n) return 100000; 8 | if(dp[currA][clipBoardA] != -1) return dp[currA][clipBoardA]; 9 | int CopyPaste = 1+1+solve(n, currA + currA,currA); 10 | int Paste = 1+solve(n, currA + clipBoardA,clipBoardA); 11 | return dp[currA][clipBoardA] = min(CopyPaste ,Paste); 12 | } 13 | int minSteps(int n) { 14 | if(n==1) return 0; 15 | memset(dp,-1,sizeof(dp)); 16 | return 1+solve(n,1,1); 17 | } 18 | }; -------------------------------------------------------------------------------- /Arrays/SlidingWindow/subArraySumDivsibleByK.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | int subarraysDivByK(vector& nums, int k) { 7 | unordered_map mp; 8 | mp[0] = 1; 9 | int sum = 0, ans=0; 10 | for(int i=0;i 2 | using namespace std; 3 | 4 | 5 | class Solution { 6 | public: 7 | vector vis; 8 | void solve(int V, vector adj[], vector &ans, int u) 9 | { 10 | if(vis[u]) return; 11 | vis[u]=true; 12 | ans.push_back(u); 13 | for(auto v : adj[u]) 14 | { 15 | if(!vis[v]) 16 | { 17 | solve(V,adj,ans,v); 18 | } 19 | } 20 | } 21 | vector dfsOfGraph(int V, vector adj[]) { 22 | vector ans; 23 | vis.resize(V, false); 24 | solve(V, adj, ans,0); 25 | return ans; 26 | } 27 | }; -------------------------------------------------------------------------------- /0077-combinations/0077-combinations.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> ans; 4 | void solve(int n, int k, vector &curr,int i) 5 | { 6 | if(curr.size() == k) 7 | { 8 | sort(curr.begin(),curr.end()); 9 | ans.push_back(curr); 10 | return; 11 | } 12 | if(i > n) return; 13 | 14 | curr.push_back(i); 15 | solve(n,k,curr,i+1); 16 | curr.pop_back(); 17 | solve(n,k,curr,i+1); 18 | } 19 | vector> combine(int n, int k) { 20 | vector curr; 21 | solve(n,k,curr,1); 22 | 23 | return ans; 24 | } 25 | }; -------------------------------------------------------------------------------- /Strings/longestCommonPrefix.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // using Sorting Method 5 | class Solution { 6 | public: 7 | string longestCommonPrefix(vector& strs) { 8 | int n = strs.size(); 9 | sort(strs.begin(), strs.end()); 10 | string str1 = strs[0]; 11 | string str2 = strs[n-1]; 12 | string answer = ""; 13 | int i=0; 14 | while(i& nums, int n, int left, int right) { 5 | vector v; 6 | for(int i=0;i 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | vector> groupAnagrams(vector& strs) { 7 | unordered_map> anagramGroups; 8 | 9 | for (string& str : strs) { 10 | string sortedStr = str; 11 | sort(sortedStr.begin(), sortedStr.end()); 12 | anagramGroups[sortedStr].push_back(str); 13 | } 14 | 15 | vector> result; 16 | for (auto& group : anagramGroups) { 17 | result.push_back(group.second); 18 | } 19 | 20 | return result; 21 | } 22 | }; -------------------------------------------------------------------------------- /0303-range-sum-query-immutable/0303-range-sum-query-immutable.cpp: -------------------------------------------------------------------------------- 1 | class NumArray { 2 | public: 3 | vector arr; 4 | 5 | NumArray(vector& nums) { 6 | arr.resize(nums.size()); 7 | arr[0] = nums[0]; 8 | for(int i=1;i 0) return arr[right] - arr[left-1]; 16 | return arr[right]; 17 | } 18 | }; 19 | 20 | /** 21 | * Your NumArray object will be instantiated and called as such: 22 | * NumArray* obj = new NumArray(nums); 23 | * int param_1 = obj->sumRange(left,right); 24 | */ -------------------------------------------------------------------------------- /0719-find-k-th-smallest-pair-distance/0719-find-k-th-smallest-pair-distance.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int smallestDistancePair(vector& nums, int k) { 4 | int n = nums.size(); 5 | int maxEl = *max_element(nums.begin(),nums.end()); 6 | vector vec(maxEl+1,0); 7 | for(int i=0;i 2 | using namespace std; 3 | 4 | void dfs(struct Node *root,vector &inorder) 5 | { 6 | if(!root) return; 7 | 8 | dfs(root->left,inorder); 9 | inorder.push_back(root->data); 10 | dfs(root->right,inorder); 11 | } 12 | float findMedian(struct Node *root) 13 | { 14 | vector inorder; 15 | dfs(root,inorder); 16 | int n = inorder.size(); 17 | if (n == 0) return 0.0; 18 | float mid = 0.0; 19 | if (n % 2 == 0) 20 | { 21 | 22 | return (inorder[n / 2 - 1] + inorder[n / 2]) / 2.0; 23 | } 24 | else 25 | { 26 | return inorder[n / 2]; 27 | } 28 | return mid; 29 | } 30 | -------------------------------------------------------------------------------- /Arrays/sortColors.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //1) using extra space --> map 5 | //2) using sorting 6 | //3) using 3 pointers 7 | class Solution { 8 | public: 9 | void sortColors(vector& nums) { 10 | int n = nums.size(); 11 | int i=0; 12 | int k=0; 13 | int j=n-1; 14 | while(k<=j){ 15 | if(nums[k]==2) 16 | { 17 | swap(nums[k],nums[j]); 18 | j--; 19 | } 20 | else if(nums[k]==0) 21 | { 22 | swap(nums[k],nums[i]); 23 | k++; 24 | i++; 25 | }else k++; 26 | } 27 | } 28 | }; -------------------------------------------------------------------------------- /0239-sliding-window-maximum/0239-sliding-window-maximum.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector maxSlidingWindow(vector& arr, int k) { 4 | deque dq; 5 | vector ans; 6 | 7 | for (int i = 0; i < arr.size(); ++i) { 8 | if (!dq.empty() && dq.front() == i - k) { 9 | dq.pop_front(); 10 | } 11 | 12 | while (!dq.empty() && arr[dq.back()] < arr[i]) { 13 | dq.pop_back(); 14 | } 15 | 16 | dq.push_back(i); 17 | 18 | if (i >= k - 1) { 19 | ans.push_back(arr[dq.front()]); 20 | } 21 | } 22 | 23 | return ans; 24 | } 25 | }; -------------------------------------------------------------------------------- /2488-divide-intervals-into-minimum-number-of-groups/2488-divide-intervals-into-minimum-number-of-groups.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minGroups(vector>& intervals) { 4 | int n = intervals.size(); 5 | sort(intervals.begin(),intervals.end()); 6 | priority_queue, greater> pq; 7 | for(auto p : intervals) 8 | { 9 | int start = p[0]; 10 | int end = p[1]; 11 | if(!pq.empty() && start > pq.top()){ 12 | pq.pop(); 13 | pq.push(end); 14 | }else{ 15 | pq.push(end); 16 | } 17 | } 18 | return pq.size(); 19 | } 20 | }; -------------------------------------------------------------------------------- /2800-minimum-string-length-after-removing-substrings/2800-minimum-string-length-after-removing-substrings.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minLength(string s) { 4 | string s1 = "AB"; 5 | string s2 = "CD"; 6 | stack st; 7 | for(int i=0;i& nums, int k) { 4 | int n = nums.size(); 5 | int l=0,r=0; 6 | int count =0; 7 | int prod = 1; 8 | while(r= k) 12 | { 13 | if(prod == 1) break; 14 | prod = prod/nums[l]; 15 | l++; 16 | } 17 | if(prod < k) 18 | { 19 | count += r-l+1; 20 | } 21 | r++; 22 | } 23 | return count; 24 | } 25 | }; -------------------------------------------------------------------------------- /0718-maximum-length-of-repeated-subarray/0718-maximum-length-of-repeated-subarray.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findLength(vector& nums1, vector& nums2) { 4 | int n1 = nums1.size(); 5 | int n2 = nums2.size(); 6 | vector> dp(n1+1,vector(n2+1,0)); 7 | int maxi = 0; 8 | for(int i=1;i<=n1;i++) 9 | { 10 | for(int j=1;j<=n2;j++) 11 | { 12 | if(nums1[i-1] == nums2[j-1]) 13 | { 14 | dp[i][j] = dp[i-1][j-1]+1; 15 | maxi = max(dp[i][j],maxi); 16 | } 17 | } 18 | } 19 | return maxi; 20 | 21 | } 22 | }; -------------------------------------------------------------------------------- /1028-interval-list-intersections/1028-interval-list-intersections.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> intervalIntersection(vector>& f, vector>& s) { 4 | vector> ans; 5 | int i = 0, j = 0; 6 | while (i < f.size() && j < s.size()) { 7 | int start = max(f[i][0], s[j][0]); 8 | int end = min(f[i][1], s[j][1]); 9 | 10 | if (start <= end) { 11 | ans.push_back({start, end}); 12 | } 13 | 14 | if (f[i][1] < s[j][1]) { 15 | i++; 16 | } else { 17 | j++; 18 | } 19 | } 20 | return ans; 21 | } 22 | }; -------------------------------------------------------------------------------- /1781-check-if-two-string-arrays-are-equivalent/1781-check-if-two-string-arrays-are-equivalent.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool arrayStringsAreEqual(vector& word1, vector& word2) { 4 | string str1 = ""; 5 | string str2 = ""; 6 | for(int i=0;i, greater> pq; 4 | int K; 5 | KthLargest(int k, vector& nums) { 6 | K =k; 7 | for(auto x : nums) 8 | { 9 | pq.push(x); 10 | if(pq.size()>k) pq.pop(); 11 | } 12 | 13 | } 14 | 15 | int add(int val) { 16 | pq.push(val); 17 | if(pq.size()>K) pq.pop(); 18 | return pq.top(); 19 | } 20 | }; 21 | 22 | /** 23 | * Your KthLargest object will be instantiated and called as such: 24 | * KthLargest* obj = new KthLargest(k, nums); 25 | * int param_1 = obj->add(val); 26 | */ -------------------------------------------------------------------------------- /1622-max-value-of-equation/1622-max-value-of-equation.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findMaxValueOfEquation(vector>& points, int k) { 4 | int n = points.size(); 5 | priority_queue> pq; 6 | int maxValue = INT_MIN; 7 | for(auto &pt : points){ 8 | int x_j = pt[0]; 9 | int y_j = pt[1]; 10 | 11 | while(!pq.empty() && x_j - pq.top().second > k){ 12 | pq.pop(); 13 | } 14 | if (!pq.empty()) { 15 | maxValue = max(maxValue, y_j + x_j + pq.top().first); 16 | } 17 | pq.emplace(y_j - x_j, x_j); 18 | } 19 | return maxValue; 20 | } 21 | }; -------------------------------------------------------------------------------- /LinkedList/reverseLL.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct ListNode { 5 | int val; 6 | ListNode *next; 7 | ListNode() : val(0), next(nullptr) {} 8 | ListNode(int x) : val(x), next(nullptr) {} 9 | ListNode(int x, ListNode *next) : val(x), next(next) {} 10 | }; 11 | 12 | // using previous node; 13 | class Solution { 14 | public: 15 | ListNode* reverseList(ListNode* head) { 16 | ListNode* curr = head; 17 | ListNode* prev = NULL; 18 | while(curr!= NULL) 19 | { 20 | ListNode* next = curr->next; 21 | curr->next=prev; 22 | prev=curr; 23 | curr=next; 24 | } 25 | return prev; 26 | } 27 | }; -------------------------------------------------------------------------------- /Strings/IntegerToroman.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | string intToRoman(int num) { 7 | vector> mp = { 8 | {1000, "M"}, {900, "CM"}, {500, "D"}, {400, "CD"}, 9 | {100, "C"}, {90, "XC"}, {50, "L"}, {40, "XL"}, 10 | {10, "X"}, {9, "IX"}, {5, "V"}, {4, "IV"}, {1, "I"} 11 | }; 12 | 13 | string result = ""; 14 | for (auto& value : mp) { 15 | int count = num / value.first; 16 | num %= value.first; 17 | for (int i = 0; i < count; ++i) { 18 | result += value.second; 19 | } 20 | } 21 | return result; 22 | } 23 | }; -------------------------------------------------------------------------------- /2581-divide-players-into-teams-of-equal-skill/2581-divide-players-into-teams-of-equal-skill.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | long long dividePlayers(vector& skill) { 4 | int n = skill.size(); 5 | if(n == 2) return skill[0]*skill[1]; 6 | sort(skill.begin(),skill.end()); 7 | int i=1; 8 | int j=n-2; 9 | long long common = skill[0]+skill[n-1]; 10 | long long ans = skill[0]*skill[n-1]; 11 | while(i 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | string removeKdigits(string num, int k) { 7 | int n = num.length(); 8 | string result = ""; 9 | for(int i=0;i0 && result.back()>num[i] && k>0) 12 | { 13 | result.pop_back(); 14 | k--; 15 | } 16 | if(result.length()>0 || num[i] != '0') 17 | result.push_back(num[i]); 18 | } 19 | while(k>0 && !result.empty()){ 20 | result.pop_back(); 21 | k--; 22 | } 23 | return result == "" ? "0": result; 24 | } 25 | }; -------------------------------------------------------------------------------- /0729-my-calendar-i/0729-my-calendar-i.cpp: -------------------------------------------------------------------------------- 1 | class MyCalendar { 2 | public: 3 | set> st; 4 | MyCalendar() { 5 | 6 | } 7 | 8 | bool book(int start, int end) { 9 | auto it = st.lower_bound({start,end}); 10 | 11 | if(it != st.end() && it->firstsecond) return false; 16 | } 17 | 18 | st.insert({start,end}); 19 | 20 | return true; 21 | } 22 | }; 23 | 24 | /** 25 | * Your MyCalendar object will be instantiated and called as such: 26 | * MyCalendar* obj = new MyCalendar(); 27 | * bool param_1 = obj->book(start,end); 28 | */ -------------------------------------------------------------------------------- /1435-xor-queries-of-a-subarray/1435-xor-queries-of-a-subarray.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector xorQueries(vector& arr, vector>& queries) { 4 | int n = queries.size(); 5 | int m = arr.size(); 6 | vector ans; 7 | vector PreXor(m); 8 | PreXor[0] = arr[0]; 9 | for(int i=1;i 2 | using namespace std; 3 | 4 | struct ListNode { 5 | int val; 6 | ListNode *next; 7 | ListNode() : val(0), next(nullptr) {} 8 | ListNode(int x) : val(x), next(nullptr) {} 9 | ListNode(int x, ListNode *next) : val(x), next(next) {} 10 | }; 11 | 12 | //1) find the length of LL and then find the middle 13 | //2) using slow-fast pointers 14 | 15 | class Solution { 16 | public: 17 | ListNode* middleNode(ListNode* head) { 18 | ListNode *slow=head, *fast=head; 19 | while(fast != NULL && fast -> next != NULL) 20 | { 21 | slow = slow -> next; 22 | fast = fast -> next -> next; 23 | } 24 | return slow; 25 | } 26 | }; -------------------------------------------------------------------------------- /Trees/InvertBT.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct TreeNode { 5 | int val; 6 | TreeNode *left; 7 | TreeNode *right; 8 | TreeNode() : val(0), left(nullptr), right(nullptr) {} 9 | TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 10 | TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 11 | }; 12 | 13 | class Solution { 14 | public: 15 | TreeNode* invertTree(TreeNode* root) { 16 | if(!root) return root; 17 | 18 | invertTree(root->left); 19 | TreeNode* left = root->left; 20 | root->left = root->right; 21 | root->right = left; 22 | invertTree(root->left); 23 | return root; 24 | } 25 | }; -------------------------------------------------------------------------------- /0041-first-missing-positive/0041-first-missing-positive.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int firstMissingPositive(vector& nums) { 4 | int n = nums.size(); 5 | bool containsOne = false; 6 | for(int i=0;i n) nums[i] = 1; 10 | } 11 | if(!containsOne) return 1; 12 | for(int i=0;i 0) return i+1; 21 | } 22 | return n+1; 23 | } 24 | }; -------------------------------------------------------------------------------- /LinkedList/BinaryToInteger.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct ListNode { 5 | int val; 6 | ListNode *next; 7 | ListNode() : val(0), next(nullptr) {} 8 | ListNode(int x) : val(x), next(nullptr) {} 9 | ListNode(int x, ListNode *next) : val(x), next(next) {} 10 | }; 11 | // 1) store in array and then find decimal 12 | // 2) reverse the ll and then find decimal 13 | 14 | class Solution { 15 | public: 16 | int getDecimalValue(ListNode* head) { 17 | int result = 0; 18 | ListNode* temp = head; 19 | while(temp!= NULL) 20 | { 21 | result *= 2; 22 | result += temp -> val; 23 | temp = temp -> next; 24 | 25 | }return result; 26 | } 27 | }; -------------------------------------------------------------------------------- /0920-uncommon-words-from-two-sentences/0920-uncommon-words-from-two-sentences.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector uncommonFromSentences(string s1, string s2) { 4 | unordered_map mp; 5 | string token = ""; 6 | stringstream ss1(s1); 7 | stringstream ss2(s2); 8 | while(getline(ss1,token,' ')) 9 | { 10 | mp[token]++; 11 | } 12 | while(getline(ss2,token,' ')) 13 | { 14 | mp[token]++; 15 | } 16 | vector ans; 17 | for(auto p : mp) 18 | { 19 | if(p.second == 1) 20 | { 21 | ans.push_back(p.first); 22 | } 23 | } 24 | return ans; 25 | } 26 | }; -------------------------------------------------------------------------------- /1786-count-the-number-of-consistent-strings/1786-count-the-number-of-consistent-strings.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int countConsistentStrings(string allowed, vector& words) { 4 | int n = words.size(); 5 | unordered_set mp; 6 | int count =0; 7 | for(int i=0;i 2 | using namespace std; 3 | 4 | class Solution 5 | { 6 | public: 7 | //Function to find the minimum number of swaps required to sort the array. 8 | int minSwaps(vector&nums) 9 | { 10 | int n = nums.size(); 11 | vector> mp; 12 | for(int i=0;i 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | string simplifyPath(string path) { 7 | string result = ""; 8 | stack st; 9 | stringstream ss(path); 10 | string token; 11 | while(getline(ss,token,'/')) 12 | { 13 | if(token == "" || token == ".") continue; 14 | if(token != "..") 15 | { 16 | st.push(token); 17 | }else if(!st.empty()) st.pop(); 18 | } 19 | if(st.empty()) return "/"; 20 | 21 | while(!st.empty()) 22 | { 23 | result = '/'+st.top()+result; 24 | st.pop(); 25 | } 26 | return result; 27 | } 28 | }; -------------------------------------------------------------------------------- /1813-maximum-erasure-value/1813-maximum-erasure-value.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maximumUniqueSubarray(vector& nums) { 4 | int n = nums.size(); 5 | unordered_set st; 6 | int i=0, j=0; 7 | int currSum = 0; 8 | int maxi = 0; 9 | while(j& nums, int i,int prev) 5 | { 6 | if(i >= nums.size()) return 0; 7 | if(prev != -1 && dp[i][prev] != -1) return dp[i][prev]; 8 | int take=0, skip=0; 9 | if(prev == -1 || nums[i] > nums[prev]) 10 | { 11 | take = 1 + LIS(nums,i+1,i); 12 | } 13 | skip = LIS(nums,i+1,prev); 14 | 15 | if(prev != -1) 16 | { 17 | dp[i][prev] = max(take,skip); 18 | } 19 | return max(take,skip); 20 | } 21 | int lengthOfLIS(vector& nums) { 22 | memset(dp,-1,sizeof(dp)); 23 | return LIS(nums,0,-1); 24 | } 25 | }; -------------------------------------------------------------------------------- /3519-find-the-number-of-winning-players/3519-find-the-number-of-winning-players.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int winningPlayerCount(int n, vector>& pick) { 4 | vector> playerColorCount(n); 5 | 6 | for (auto p : pick) { 7 | int player = p[0]; 8 | int color = p[1]; 9 | playerColorCount[player][color]++; 10 | } 11 | 12 | int winningPlayers = 0; 13 | 14 | for (int i = 0; i < n; ++i) { 15 | for (auto entry : playerColorCount[i]) { 16 | if (entry.second > i) { 17 | winningPlayers++; 18 | break; 19 | } 20 | } 21 | } 22 | 23 | return winningPlayers; 24 | } 25 | }; -------------------------------------------------------------------------------- /0003-longest-substring-without-repeating-characters/0003-longest-substring-without-repeating-characters.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int lengthOfLongestSubstring(string s) { 4 | int ans=0; 5 | int n = s.size(); 6 | vector ex; 7 | int count =0; 8 | for(int i=0;i 2 | using namespace std; 3 | 4 | //1) using extra space (Array) 5 | 6 | //2) using two pointers 7 | class Solution { 8 | public: 9 | void merge(vector& nums1, int m, vector& nums2, int n) { 10 | int i = m - 1, j = n - 1, k = (m + n - 1); 11 | while (i>=0 && j>=0) 12 | { 13 | if (nums1[i] >= nums2[j]) 14 | { 15 | nums1[k--] = nums1[i--]; 16 | } 17 | else 18 | { 19 | nums1[k--] = nums2[j--]; 20 | } 21 | } 22 | while (i>=0) 23 | { 24 | nums1[k--] = nums1[i--]; 25 | } 26 | while (j>=0) 27 | { 28 | nums1[k--] = nums2[j--]; 29 | } 30 | } 31 | }; -------------------------------------------------------------------------------- /0104-maximum-depth-of-binary-tree/0104-maximum-depth-of-binary-tree.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | class Solution { 13 | public: 14 | int height(TreeNode* root) 15 | { 16 | if(!root) return 0; 17 | int left = height(root->left); 18 | int right = height(root->right); 19 | 20 | return max(left,right) + 1; 21 | } 22 | int maxDepth(TreeNode* root) { 23 | return height(root); 24 | } 25 | }; -------------------------------------------------------------------------------- /Arrays/checkIfReversingMakesSorted.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool sortArr(int a[], int n) 5 | { 6 | int x = -1; 7 | int y = -1; 8 | 9 | for (int i = 0; i < n - 1; i++) { 10 | if (a[i] > a[i + 1]) { 11 | if (x == -1) { 12 | x = i; 13 | } 14 | y = i + 1; 15 | } 16 | } 17 | 18 | if (x != -1) { 19 | reverse(a + x, a + y + 1); 20 | for (int i = 0; i < n - 1; i++) { 21 | if (a[i] > a[i + 1]) { 22 | return false; 23 | return 0; 24 | } 25 | } 26 | } 27 | 28 | return true; 29 | } 30 | 31 | int main() 32 | { 33 | int arr[] = { 1, 2, 5, 4, 3 }; 34 | int n = sizeof(arr) / sizeof(arr[0]); 35 | 36 | sortArr(arr, n) ? (cout << "Yes" << endl) 37 | : (cout << "No" << endl); 38 | return 0; 39 | } 40 | 41 | -------------------------------------------------------------------------------- /Graphs/bfs.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | // Function to return Breadth First Traversal of given graph. 7 | vector bfsOfGraph(int V, vector adj[]) { 8 | queue q; 9 | vector vis(V,false); 10 | vector ans; 11 | q.push(0); 12 | vis[0] = true; 13 | while(!q.empty()) 14 | { 15 | int node = q.front(); 16 | ans.push_back(node); 17 | q.pop(); 18 | for(int n : adj[node]) 19 | { 20 | if(!vis[n]) 21 | { 22 | q.push(n); 23 | vis[n] = true; 24 | } 25 | } 26 | } 27 | return ans; 28 | } 29 | }; 30 | -------------------------------------------------------------------------------- /0236-lowest-common-ancestor-of-a-binary-tree/0236-lowest-common-ancestor-of-a-binary-tree.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 | * }; 9 | */ 10 | class Solution { 11 | public: 12 | TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { 13 | if(!root) return NULL; 14 | 15 | if(root == p || root == q) return root; 16 | 17 | TreeNode* left= lowestCommonAncestor(root->left,p,q); 18 | TreeNode* right= lowestCommonAncestor(root->right,p,q); 19 | 20 | if(left != NULL && right != NULL) return root; 21 | if(left != NULL) return left; 22 | return right; 23 | } 24 | }; -------------------------------------------------------------------------------- /0151-reverse-words-in-a-string/0151-reverse-words-in-a-string.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string reverseWords(string s) { 4 | reverse(s.begin(),s.end()); 5 | int l=0, r=0; 6 | string ans= ""; 7 | while(l < s.size() && s[l] ==' '){ 8 | l++; 9 | r++; 10 | } 11 | while(r sortJumbled(vector& mapping, vector& nums) { 4 | vector> mp; 5 | int n = nums.size(); 6 | for(int i=0;i ans; 18 | for(auto p : mp) 19 | { 20 | int ind = p.second; 21 | ans.push_back(nums[ind]); 22 | } 23 | return ans; 24 | } 25 | }; -------------------------------------------------------------------------------- /Stack/backspaceStringCompare.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | bool backspaceCompare(string s, string t) { 7 | vector st1; 8 | vector st2; 9 | for(int i=0;i mostCompetitive(vector& nums, int k) { 4 | int n = nums.size(); 5 | stack st; 6 | 7 | for (int i = 0; i < n; ++i) { 8 | while (!st.empty() && nums[i] < nums[st.top()] && st.size() + (n - i) > k) { 9 | st.pop(); 10 | } 11 | 12 | if (st.size() < k) { 13 | st.push(i); 14 | } 15 | } 16 | 17 | vector ans; 18 | while (!st.empty()) { 19 | ans.push_back(nums[st.top()]); 20 | st.pop(); 21 | } 22 | 23 | reverse(ans.begin(), ans.end()); 24 | 25 | return ans; 26 | } 27 | }; -------------------------------------------------------------------------------- /2755-extra-characters-in-a-string/2755-extra-characters-in-a-string.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector dp; 4 | int solve(int i, unordered_set &st, string &s, int n) 5 | { 6 | if(i>=n) return 0; 7 | if(dp[i] != -1) return dp[i]; 8 | int res = 1+ solve(i+1,st,s,n); 9 | for(int j=i;j& dict) { 20 | int n = s.size(); 21 | dp.resize(n,-1); 22 | unordered_set st(dict.begin(),dict.end()); 23 | return solve(0,st,s,n); 24 | } 25 | }; -------------------------------------------------------------------------------- /0235-lowest-common-ancestor-of-a-binary-search-tree/0235-lowest-common-ancestor-of-a-binary-search-tree.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 | * }; 9 | */ 10 | 11 | class Solution { 12 | public: 13 | TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { 14 | if(!root) return NULL; 15 | 16 | if(root->val < p->val && root->val < q->val) 17 | { 18 | return lowestCommonAncestor(root->right,p,q); 19 | } 20 | if(root->val > p->val && root->val > q->val) 21 | { 22 | return lowestCommonAncestor(root->left,p,q); 23 | } 24 | return root; 25 | } 26 | }; 27 | -------------------------------------------------------------------------------- /0347-top-k-frequent-elements/0347-top-k-frequent-elements.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector topKFrequent(vector& nums, int k) { 4 | int n = nums.size(); 5 | vector> freq(n+1); 6 | unordered_map mp; 7 | for(int i=0;i ans; 16 | for(int i=n;i>=0;i--) 17 | { 18 | while(freq[i].size() > 0 && k >0) 19 | { 20 | ans.push_back(freq[i].back()); 21 | freq[i].pop_back(); 22 | k--; 23 | } 24 | if(k <=0) break; 25 | } 26 | return ans; 27 | } 28 | }; -------------------------------------------------------------------------------- /0617-merge-two-binary-trees/0617-merge-two-binary-trees.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | class Solution { 13 | public: 14 | TreeNode* mergeTrees(TreeNode* root1, TreeNode* root2) { 15 | if(!root1) return root2; 16 | if(!root2) return root1; 17 | root1->val += root2->val; 18 | root1->left = mergeTrees(root1->left, root2->left); 19 | root1->right = mergeTrees(root1->right, root2->right); 20 | return root1; 21 | } 22 | }; -------------------------------------------------------------------------------- /0539-minimum-time-difference/0539-minimum-time-difference.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findMinDifference(vector& timePoints) { 4 | int n = timePoints.size(); 5 | vector inMins(n); 6 | for(int i=0;i 2 | using namespace std; 3 | 4 | struct ListNode { 5 | int val; 6 | ListNode *next; 7 | ListNode() : val(0), next(nullptr) {} 8 | ListNode(int x) : val(x), next(nullptr) {} 9 | ListNode(int x, ListNode *next) : val(x), next(next) {} 10 | }; 11 | 12 | // using slow, fast pointers (hare and tortoise) 13 | class Solution { 14 | public: 15 | bool hasCycle(ListNode *head) { 16 | ListNode *fast = head; 17 | ListNode *slow = head; 18 | while(fast != NULL && fast->next != NULL) 19 | { 20 | slow = slow->next; 21 | fast = fast->next->next; 22 | if(fast == slow) 23 | { 24 | return true; // cycle is present 25 | } 26 | } 27 | return false; 28 | } 29 | }; -------------------------------------------------------------------------------- /LinkedList/removeLLelements.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct ListNode { 5 | int val; 6 | ListNode *next; 7 | ListNode() : val(0), next(nullptr) {} 8 | ListNode(int x) : val(x), next(nullptr) {} 9 | ListNode(int x, ListNode *next) : val(x), next(next) {} 10 | }; 11 | 12 | class Solution { 13 | public: 14 | ListNode* removeElements(ListNode* head, int val) { 15 | ListNode* dummy = new ListNode(-1); 16 | dummy->next = head; 17 | ListNode* curr = dummy; 18 | while(curr->next != NULL) 19 | { 20 | if(curr->next->val == val) 21 | { 22 | curr->next = curr->next->next; 23 | }else{ 24 | curr = curr->next; 25 | } 26 | } 27 | return dummy->next; 28 | } 29 | }; -------------------------------------------------------------------------------- /0416-partition-equal-subset-sum/0416-partition-equal-subset-sum.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> dp; 4 | bool solve(vector& nums, int idx, int target) 5 | { 6 | if (target == 0) return true; 7 | if (idx == nums.size() || target < 0) return false; 8 | 9 | if (dp[idx][target] != -1) return dp[idx][target]; 10 | int take = solve(nums,idx+1,target-nums[idx]); 11 | int not_take = solve(nums,idx+1,target); 12 | 13 | return dp[idx][target] = take || not_take; 14 | } 15 | bool canPartition(vector& nums) { 16 | int sum = accumulate(nums.begin(),nums.end(),0); 17 | if(sum%2 == 1) return false; 18 | int t = sum/2; 19 | dp.resize(nums.size(), vector(t + 1, -1)); 20 | return solve(nums,0,t); 21 | } 22 | }; -------------------------------------------------------------------------------- /Strings/reverseWords.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // 1) using stringStream 5 | // 1) using 2 pointer 6 | 7 | class Solution { 8 | public: 9 | string reverseWords(string s) { 10 | reverse(s.begin(),s.end()); 11 | int i=0; 12 | int n = s.length(); 13 | int l=0,r=0; 14 | while(i v; 15 | ListNode* curr = head; 16 | while(curr) 17 | { 18 | v.push_back(curr->val); 19 | curr = curr->next; 20 | } 21 | sort(v.begin(),v.end()); 22 | curr = head; 23 | for(int i=0;ival = v[i]; 26 | curr = curr->next; 27 | } 28 | return head; 29 | } 30 | }; -------------------------------------------------------------------------------- /1823-determine-if-string-halves-are-alike/1823-determine-if-string-halves-are-alike.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool halvesAreAlike(string s) { 4 | unordered_set st = {'A','E','I','O','U','a','e','i','o','u'}; 5 | string str1 = s.substr(0,s.length()/2); 6 | string str2 = s.substr((s.length()/2),s.length()/2); 7 | int n1 = 0; 8 | int n2 = 0; 9 | for(int i=0;i ans; 15 | void postOrder(TreeNode* root) 16 | { 17 | if(!root) return; 18 | postOrder(root->left); 19 | postOrder(root->right); 20 | ans.push_back(root->val); 21 | } 22 | vector postorderTraversal(TreeNode* root) { 23 | postOrder(root); 24 | return ans; 25 | } 26 | }; -------------------------------------------------------------------------------- /0776-n-ary-tree-postorder-traversal/0776-n-ary-tree-postorder-traversal.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | // Definition for a Node. 3 | class Node { 4 | public: 5 | int val; 6 | vector children; 7 | 8 | Node() {} 9 | 10 | Node(int _val) { 11 | val = _val; 12 | } 13 | 14 | Node(int _val, vector _children) { 15 | val = _val; 16 | children = _children; 17 | } 18 | }; 19 | */ 20 | 21 | class Solution { 22 | public: 23 | vector ans; 24 | void helper(Node* & root) 25 | { 26 | if(root == NULL) return; 27 | 28 | for(int i=0;ichildren.size();i++) 29 | { 30 | helper(root->children[i]); 31 | } 32 | ans.push_back(root->val); 33 | } 34 | vector postorder(Node* root) { 35 | helper(root); 36 | return ans; 37 | } 38 | }; -------------------------------------------------------------------------------- /1320-remove-all-adjacent-duplicates-in-string-ii/1320-remove-all-adjacent-duplicates-in-string-ii.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string removeDuplicates(string s, int k) { 4 | stack> st; 5 | for (char ch : s) { 6 | if (!st.empty() && st.top().first == ch) { 7 | st.top().second++; 8 | if (st.top().second == k) { 9 | st.pop(); 10 | } 11 | } else { 12 | st.push({ch, 1}); 13 | } 14 | } 15 | 16 | string result; 17 | while (!st.empty()) { 18 | result.append(st.top().second, st.top().first); 19 | st.pop(); 20 | } 21 | reverse(result.begin(), result.end()); // reverse the result to correct the order 22 | return result; 23 | } 24 | }; -------------------------------------------------------------------------------- /0039-combination-sum/0039-combination-sum.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> ans; 4 | void solve(vector& nums, int target,vector & curr, int idx) 5 | { 6 | if(idx == nums.size()) return; 7 | if(target == 0) 8 | { 9 | ans.push_back(curr); 10 | return; 11 | } 12 | 13 | if(nums[idx] <= target) 14 | { 15 | curr.push_back(nums[idx]); 16 | solve(nums,target-nums[idx],curr,idx); 17 | curr.pop_back(); 18 | solve(nums,target,curr,idx+1); 19 | } 20 | } 21 | vector> combinationSum(vector& candidates, int target) { 22 | vector curr; 23 | sort(candidates.begin(),candidates.end()); 24 | solve(candidates,target,curr,0); 25 | return ans; 26 | } 27 | }; -------------------------------------------------------------------------------- /Stack/stackUsingQueues.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class MyStack { 5 | public: 6 | queue q1; 7 | queue q2; 8 | MyStack() { 9 | 10 | } 11 | 12 | void push(int x) { 13 | while(!q1.empty()) 14 | { 15 | int top = q1.front(); 16 | q2.push(top); 17 | q1.pop(); 18 | } 19 | q1.push(x); 20 | while(!q2.empty()) 21 | { 22 | int top = q2.front(); 23 | q1.push(top); 24 | q2.pop(); 25 | } 26 | } 27 | 28 | int pop() { 29 | int top = q1.front(); 30 | q1.pop(); 31 | return top; 32 | } 33 | 34 | int top() { 35 | return q1.front(); 36 | } 37 | 38 | bool empty() { 39 | return q1.empty(); 40 | } 41 | }; 42 | -------------------------------------------------------------------------------- /1770-minimum-deletions-to-make-character-frequencies-unique/1770-minimum-deletions-to-make-character-frequencies-unique.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minDeletions(string s) { 4 | unordered_map freq; 5 | for (char c : s) { 6 | freq[c]++; 7 | } 8 | 9 | vector counts; 10 | for (auto& p : freq) { 11 | counts.push_back(p.second); 12 | } 13 | 14 | sort(counts.begin(), counts.end(), greater()); 15 | 16 | int deletions = 0; 17 | for (int i = 1; i < counts.size(); i++) { 18 | while (i < counts.size() && counts[i] >= counts[i - 1] && counts[i] > 0) { 19 | counts[i]--; 20 | deletions++; 21 | } 22 | } 23 | 24 | return deletions; 25 | } 26 | }; 27 | -------------------------------------------------------------------------------- /LinkedList/PalindromeLL.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct ListNode { 5 | int val; 6 | ListNode *next; 7 | ListNode() : val(0), next(nullptr) {} 8 | ListNode(int x) : val(x), next(nullptr) {} 9 | ListNode(int x, ListNode *next) : val(x), next(next) {} 10 | }; 11 | 12 | class Solution { 13 | public: 14 | bool isPalindrome(int i, int j, vector &arr) 15 | { 16 | if(i>j) return true; 17 | if(arr[i]==arr[j]) return isPalindrome(i+1,j-1,arr); 18 | return false; 19 | } 20 | bool isPalindrome(ListNode* head) { 21 | vector arr; 22 | ListNode* curr = head; 23 | while(curr != NULL) 24 | { 25 | arr.push_back(curr->val); 26 | curr = curr->next; 27 | } 28 | return isPalindrome(0,arr.size()-1,arr); 29 | } 30 | }; -------------------------------------------------------------------------------- /LinkedList/RemoveDuplicatesFromSortedLL.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct ListNode { 5 | int val; 6 | ListNode *next; 7 | ListNode() : val(0), next(nullptr) {} 8 | ListNode(int x) : val(x), next(nullptr) {} 9 | ListNode(int x, ListNode *next) : val(x), next(next) {} 10 | }; 11 | 12 | class Solution { 13 | public: 14 | ListNode* deleteDuplicates(ListNode* head) { 15 | if(head == NULL || head->next == NULL) return head; 16 | ListNode* prev = head; 17 | ListNode* curr = head->next; 18 | while(curr != NULL) 19 | { 20 | if (curr->val == prev->val) { 21 | prev->next = curr->next; 22 | } else { 23 | prev = prev->next; 24 | } 25 | curr = prev->next; 26 | } 27 | return head; 28 | } 29 | }; -------------------------------------------------------------------------------- /Trees/SameTree.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct TreeNode { 5 | int val; 6 | TreeNode *left; 7 | TreeNode *right; 8 | TreeNode() : val(0), left(nullptr), right(nullptr) {} 9 | TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 10 | TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 11 | }; 12 | 13 | class Solution { 14 | public: 15 | bool isSameTree(TreeNode* p, TreeNode* q) { 16 | if(p == NULL && q == NULL) return true; 17 | if(p == NULL && q != NULL) return false; 18 | if(p != NULL && q == NULL) return false; 19 | bool left = isSameTree(p->left,q->left); 20 | bool right = isSameTree(p->right,q->right); 21 | bool isValSame = p->val == q->val ? true : false; 22 | return left && right && isValSame; 23 | } 24 | }; -------------------------------------------------------------------------------- /Arrays/ChocolateDistribution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // sorting method 5 | class Solution{ 6 | public: 7 | long long findMinDiff(vector a, long long n, long long m){ 8 | sort(a.begin(),a.end()); 9 | long long mini = INT_MAX; 10 | for(int i=0;i+m-1>t; 22 | while(t--) 23 | { 24 | long long n; 25 | cin>>n; 26 | vector a; 27 | long long x; 28 | for(long long i=0;i>x; 31 | a.push_back(x); 32 | } 33 | 34 | long long m; 35 | cin>>m; 36 | Solution ob; 37 | cout<& maximumHeight) { 4 | int n = maximumHeight.size(); 5 | 6 | sort(maximumHeight.begin(), maximumHeight.end()); 7 | 8 | long long totalSum = 0; 9 | int currentHeight = maximumHeight[n - 1]; 10 | 11 | for (int i = n - 1; i >= 0; --i) { 12 | if (currentHeight > maximumHeight[i]) { 13 | currentHeight = maximumHeight[i]; 14 | } 15 | 16 | if (currentHeight <= 0) { 17 | return -1; 18 | } 19 | 20 | totalSum += currentHeight; 21 | currentHeight--; 22 | } 23 | 24 | return totalSum; 25 | } 26 | }; -------------------------------------------------------------------------------- /0230-kth-smallest-element-in-a-bst/0230-kth-smallest-element-in-a-bst.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | class Solution { 13 | public: 14 | vector inorderTraversal; 15 | void inorder(TreeNode* root) 16 | { 17 | if(!root) return; 18 | 19 | inorder(root->left); 20 | inorderTraversal.push_back(root->val); 21 | inorder(root->right); 22 | } 23 | int kthSmallest(TreeNode* root, int k) { 24 | inorder(root); 25 | return inorderTraversal[k-1]; 26 | } 27 | }; -------------------------------------------------------------------------------- /0440-k-th-smallest-in-lexicographical-order/0440-k-th-smallest-in-lexicographical-order.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int Count(long curr, long next, int n) { 4 | int countNum = 0; 5 | 6 | while(curr <= n) { 7 | countNum += (next - curr); 8 | 9 | curr *= 10; 10 | next *= 10; 11 | 12 | next = min(next, long(n+1)); 13 | } 14 | 15 | return countNum; 16 | } 17 | 18 | int findKthNumber(int n, int k) { 19 | int curr = 1; 20 | k -= 1; 21 | while(k > 0) { 22 | int count = Count(curr, curr+1, n); 23 | if(count <= k) { 24 | curr++; 25 | k -= count; 26 | } else { 27 | curr *= 10; 28 | k -= 1; 29 | } 30 | } 31 | 32 | return curr; 33 | 34 | } 35 | }; -------------------------------------------------------------------------------- /Trees/diameterOfBT.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct TreeNode { 5 | int val; 6 | TreeNode *left; 7 | TreeNode *right; 8 | TreeNode() : val(0), left(nullptr), right(nullptr) {} 9 | TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 10 | TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 11 | }; 12 | 13 | class Solution { 14 | public: 15 | 16 | int solve(TreeNode* root,int &result) 17 | { 18 | if(!root) return 0; 19 | 20 | int left = solve(root->left,result); 21 | int right = solve(root->right,result); 22 | result = max(result,left+right); 23 | return max(left,right)+1; 24 | } 25 | int diameterOfBinaryTree(TreeNode* root) { 26 | int diameter=0; 27 | solve(root,diameter); 28 | return diameter; 29 | } 30 | }; -------------------------------------------------------------------------------- /0101-symmetric-tree/0101-symmetric-tree.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | class Solution { 13 | public: 14 | bool traversal(TreeNode* left, TreeNode* right) 15 | { 16 | if(left == NULL || right == NULL) return left==right; 17 | 18 | if(left->val != right->val) return false; 19 | return traversal(left->left, right->right) && traversal(left->right, right->left); 20 | } 21 | bool isSymmetric(TreeNode* root) { 22 | return root== NULL || traversal(root->left, root->right); 23 | } 24 | }; -------------------------------------------------------------------------------- /0386-lexicographical-numbers/README.md: -------------------------------------------------------------------------------- 1 |

386. Lexicographical Numbers

Medium


Given an integer n, return all the numbers in the range [1, n] sorted in lexicographical order.

2 | 3 |

You must write an algorithm that runs in O(n) time and uses O(1) extra space. 

4 | 5 |

 

6 |

Example 1:

7 |
Input: n = 13
 8 | Output: [1,10,11,12,13,2,3,4,5,6,7,8,9]
 9 | 

Example 2:

10 |
Input: n = 2
11 | Output: [1,2]
12 | 
13 |

 

14 |

Constraints:

15 | 16 |
    17 |
  • 1 <= n <= 5 * 104
  • 18 |
19 | -------------------------------------------------------------------------------- /0975-range-sum-of-bst/0975-range-sum-of-bst.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | class Solution { 13 | public: 14 | int count=0; 15 | void sum(TreeNode* root, int low, int high) 16 | { 17 | if(root == NULL) return; 18 | 19 | sum(root->left,low,high); 20 | if(root->val >= low && root->val <= high) count += root->val; 21 | sum(root->right,low,high); 22 | } 23 | int rangeSumBST(TreeNode* root, int low, int high) { 24 | sum(root,low,high); 25 | return count; 26 | } 27 | }; -------------------------------------------------------------------------------- /LinkedList/sortList.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | /** 5 | * Definition for singly-linked list. 6 | * struct ListNode { 7 | * int val; 8 | * ListNode *next; 9 | * ListNode() : val(0), next(nullptr) {} 10 | * ListNode(int x) : val(x), next(nullptr) {} 11 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 12 | * }; 13 | */ 14 | class Solution { 15 | public: 16 | ListNode* sortList(ListNode* head) { 17 | vector v; 18 | ListNode* curr = head; 19 | while(curr) 20 | { 21 | v.push_back(curr->val); 22 | curr = curr->next; 23 | } 24 | sort(v.begin(),v.end()); 25 | curr = head; 26 | for(int i=0;ival = v[i]; 29 | curr = curr->next; 30 | } 31 | return head; 32 | } 33 | }; -------------------------------------------------------------------------------- /1923-sentence-similarity-iii/1923-sentence-similarity-iii.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool areSentencesSimilar(string s1, string s2) { 4 | if(s1.size() < s2.size()) swap(s1,s2); 5 | 6 | vector v1, v2; 7 | stringstream ss1(s1); 8 | string token; 9 | while(ss1>>token) 10 | { 11 | v1.push_back(token); 12 | } 13 | stringstream ss2(s2); 14 | while(ss2>>token) 15 | { 16 | v2.push_back(token); 17 | } 18 | 19 | int i=0, j= v1.size()-1; 20 | int k=0, l= v2.size()-1; 21 | while(i= i && l>=k && v1[j] == v2[l]) 27 | { 28 | j--; 29 | l--; 30 | } 31 | return l> spiralMatrixIII(int rows, int cols, int rStart, int cStart) { 4 | vector> mp = {{0,1},{1,0},{0,-1},{-1,0}}; // E,S,W,N 5 | vector> ans; 6 | ans.push_back({rStart, cStart}); 7 | int dir =0, steps=0; 8 | while(ans.size() = 0 && rStart < rows && cStart >= 0 && cStart < cols) 16 | { 17 | ans.push_back({rStart, cStart}); 18 | } 19 | } 20 | dir = (dir+1)%4; 21 | } 22 | return ans; 23 | } 24 | }; -------------------------------------------------------------------------------- /DP/longestCommonSubstring.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | int dp[1001][1001]; 7 | 8 | int solve(string& s1, string& s2, int i, int j) { 9 | if (i >= s1.size() || j >= s2.size()) return 0; 10 | if (dp[i][j] != -1) return dp[i][j]; 11 | if (s1[i] == s2[j]) { 12 | dp[i][j] = 1 + solve(s1, s2, i + 1, j + 1); 13 | } else { 14 | dp[i][j] = 0; 15 | } 16 | return dp[i][j]; 17 | } 18 | 19 | int longestCommonSubstr(string s1, string s2) { 20 | memset(dp, -1, sizeof(dp)); 21 | int maxLength = 0; 22 | for (int i = 0; i < s1.size(); ++i) { 23 | for (int j = 0; j < s2.size(); ++j) { 24 | maxLength = max(maxLength, solve(s1, s2, i, j)); 25 | } 26 | } 27 | return maxLength; 28 | } 29 | }; 30 | -------------------------------------------------------------------------------- /0150-evaluate-reverse-polish-notation/0150-evaluate-reverse-polish-notation.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int evalRPN(vector& tokens) { 4 | int n = tokens.size(); 5 | stackst; 6 | for(int i=0;i vis; 6 | void dfs(int idx, vector>& stones) 7 | { 8 | vis[idx] = true; 9 | 10 | for(int i=0;i>& stones) { 19 | n = stones.size(); 20 | int groups = 0; 21 | vis.resize(n,false); 22 | for(int i=0;i 2 | using namespace std; 3 | 4 | // Brute Force 5 | class Solution { 6 | public: 7 | int subarraySum(vector& nums, int k) { 8 | int n = nums.size(); 9 | int count=0; 10 | for(int i=0;i& nums, int k) { 26 | unordered_map mp; 27 | mp[0] = 1; 28 | int Sum = 0, count =0; 29 | for(int i=0; i< nums.size();i++) 30 | { 31 | Sum += nums[i]; 32 | int remove = Sum -k; 33 | count += mp[remove]; 34 | mp[Sum] +=1; 35 | } 36 | return count; 37 | } 38 | }; -------------------------------------------------------------------------------- /0059-spiral-matrix-ii/README.md: -------------------------------------------------------------------------------- 1 |

59. Spiral Matrix II

Medium


Given a positive integer n, generate an n x n matrix filled with elements from 1 to n2 in spiral order.

2 | 3 |

 

4 |

Example 1:

5 | 6 |
 7 | Input: n = 3
 8 | Output: [[1,2,3],[8,9,4],[7,6,5]]
 9 | 
10 | 11 |

Example 2:

12 | 13 |
14 | Input: n = 1
15 | Output: [[1]]
16 | 
17 | 18 |

 

19 |

Constraints:

20 | 21 |
    22 |
  • 1 <= n <= 20
  • 23 |
24 | -------------------------------------------------------------------------------- /Strings/ValidParenthesis.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // using stack 5 | class Solution { 6 | public: 7 | bool isValid(string s) { 8 | stack st; 9 | for(int i=0;i 2 | using namespace std; 3 | 4 | // Brute Force --> try removing every character and see if remaining string is palindrome 5 | 6 | class Solution { 7 | public: 8 | bool isPalindrome(const string& s, int left, int right) { 9 | while (left < right) { 10 | if (s[left] != s[right]) { 11 | return false; 12 | } 13 | left++; 14 | right--; 15 | } 16 | return true; 17 | } 18 | 19 | bool validPalindrome(string s) { 20 | int left = 0; 21 | int right = s.size() - 1; 22 | 23 | while (left < right) { 24 | if (s[left] != s[right]) { 25 | return isPalindrome(s, left + 1, right) || isPalindrome(s, left, right - 1); 26 | } 27 | left++; 28 | right--; 29 | } 30 | return true; 31 | } 32 | }; 33 | -------------------------------------------------------------------------------- /0394-decode-string/0394-decode-string.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string decodeString(string s) { 4 | stack st; 5 | stack st1; 6 | string sb; 7 | int n = 0; 8 | 9 | for (char c : s) { 10 | if (isdigit(c)) { 11 | n = n * 10 + (c - '0'); 12 | } else if (c == '[') { 13 | st.push(n); 14 | n = 0; 15 | st1.push(sb); 16 | sb = ""; 17 | } else if (c == ']') { 18 | int k = st.top(); 19 | st.pop(); 20 | string temp = sb; 21 | sb = st1.top(); 22 | st1.pop(); 23 | while (k-- > 0) { 24 | sb += temp; 25 | } 26 | } else { 27 | sb += c; 28 | } 29 | } 30 | 31 | return sb; 32 | } 33 | }; -------------------------------------------------------------------------------- /0424-longest-repeating-character-replacement/0424-longest-repeating-character-replacement.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int characterReplacement(string s, int k) { 4 | int n = s.size(); 5 | vector mp(26,0); 6 | int len =0, maxfreq=0; 7 | int l=0,r=0; 8 | while(r k) 13 | { 14 | mp[s[l]-'A']--; 15 | maxfreq =0; 16 | for(int f : mp) 17 | { 18 | maxfreq = max(maxfreq, f); 19 | } 20 | l++; 21 | } 22 | if((r-l+1) - maxfreq <= k) 23 | { 24 | len = max(len, r-l+1); 25 | } 26 | r++; 27 | } 28 | return len; 29 | 30 | } 31 | }; -------------------------------------------------------------------------------- /0778-reorganize-string/README.md: -------------------------------------------------------------------------------- 1 |

778. Reorganize String

Medium


Given a string s, rearrange the characters of s so that any two adjacent characters are not the same.

2 | 3 |

Return any possible rearrangement of s or return "" if not possible.

4 | 5 |

 

6 |

Example 1:

7 |
Input: s = "aab"
 8 | Output: "aba"
 9 | 

Example 2:

10 |
Input: s = "aaab"
11 | Output: ""
12 | 
13 |

 

14 |

Constraints:

15 | 16 |
    17 |
  • 1 <= s.length <= 500
  • 18 |
  • s consists of lowercase English letters.
  • 19 |
20 | -------------------------------------------------------------------------------- /1511-count-number-of-teams/1511-count-number-of-teams.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numTeams(vector& rating) { 4 | int n = rating.size(); 5 | int count =0; 6 | for(int j=1;jrating[j]) countLargerLeft++; 14 | } 15 | for(int i=j;irating[j]) countLargerRight++; 19 | } 20 | count+= countSmallerLeft*countLargerRight; 21 | count+= countLargerLeft*countSmallerRight; 22 | } 23 | return count; 24 | } 25 | }; -------------------------------------------------------------------------------- /0539-minimum-time-difference/README.md: -------------------------------------------------------------------------------- 1 |

539. Minimum Time Difference

Medium


Given a list of 24-hour clock time points in "HH:MM" format, return the minimum minutes difference between any two time-points in the list. 2 |

 

3 |

Example 1:

4 |
Input: timePoints = ["23:59","00:00"]
 5 | Output: 1
 6 | 

Example 2:

7 |
Input: timePoints = ["00:00","23:59","00:00"]
 8 | Output: 0
 9 | 
10 |

 

11 |

Constraints:

12 | 13 |
    14 |
  • 2 <= timePoints.length <= 2 * 104
  • 15 |
  • timePoints[i] is in the format "HH:MM".
  • 16 |
17 | -------------------------------------------------------------------------------- /1250-longest-common-subsequence/1250-longest-common-subsequence.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | 4 | int longestCommonSubsequence(string text1, string text2) { 5 | int n = text1.size(); 6 | int m = text2.size(); 7 | if(n <= 0 || m <= 0) return 0; 8 | vector> dp(n+1, vector(m+1)); 9 | for(int i=0;i &arr, int i,int j) 5 | { 6 | int temp = arr[i]; 7 | arr[i] = arr[j]; 8 | arr[j] = temp; 9 | } 10 | void check(vector &arr,int j) 11 | { 12 | if(j == arr.size()){ 13 | count++; 14 | return; 15 | } 16 | for(int i=j;i arr(n,0); 29 | for(int i=0;inext = curr; 17 | while(curr) 18 | { 19 | if(curr->val != prev->val) 20 | { 21 | prev = curr; 22 | curr = curr->next; 23 | }else{ 24 | prev->next = curr->next; 25 | curr = curr->next; 26 | } 27 | } 28 | return head; 29 | } 30 | }; -------------------------------------------------------------------------------- /0098-validate-binary-search-tree/0098-validate-binary-search-tree.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | class Solution { 13 | public: 14 | bool isValid(TreeNode* root, long long min,long long max) 15 | { 16 | if(root == NULL) return true; 17 | if(root->val >= max || root->val <= min) return false; 18 | return isValid(root->left,min,root->val) && isValid(root->right,root->val,max); 19 | } 20 | bool isValidBST(TreeNode* root) { 21 | if(!root->left && !root->right) return true; 22 | return isValid(root,LLONG_MIN, LLONG_MAX); 23 | } 24 | }; -------------------------------------------------------------------------------- /0474-ones-and-zeroes/0474-ones-and-zeroes.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector>> dp; 4 | int solve(vector& strs, int m, int n, int idx) 5 | { 6 | if(idx >= strs.size()) return 0; 7 | if(dp[m][n][idx] != -1) return dp[m][n][idx]; 8 | int oneCount = count(strs[idx].begin(),strs[idx].end(),'1'); 9 | int zeroCount = strs[idx].size()-oneCount; 10 | 11 | 12 | int not_take = solve(strs,m, n,idx+1); 13 | 14 | if(m-zeroCount >=0 && n-oneCount >= 0) 15 | { 16 | int take = 1 + solve(strs,m-zeroCount, n-oneCount,idx+1); 17 | return dp[m][n][idx] = max(take, not_take); 18 | } 19 | return dp[m][n][idx] = not_take; 20 | } 21 | int findMaxForm(vector& strs, int m, int n) { 22 | dp.resize(m+1,vector>(n+1,vector(strs.size(), -1))); 23 | return solve(strs,m,n,0); 24 | } 25 | }; -------------------------------------------------------------------------------- /1196-filling-bookcase-shelves/1196-filling-bookcase-shelves.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int WIDTH; 4 | int dp[1001][1001]; 5 | int solve(int i, vector>& books, int remainingWidth,int maxHeight) 6 | { 7 | if(i>= books.size()) return maxHeight; 8 | if(dp[i][remainingWidth] != -1) return dp[i][remainingWidth]; 9 | int bookW = books[i][0]; 10 | int bookH = books[i][1]; 11 | int keep=INT_MAX, skip=INT_MAX; 12 | if(bookW <= remainingWidth){ 13 | keep = solve(i+1,books,remainingWidth-bookW,max(maxHeight,bookH)); 14 | } 15 | skip = maxHeight + solve(i+1,books,WIDTH-bookW,bookH); 16 | return dp[i][remainingWidth] = min(keep,skip); 17 | } 18 | int minHeightShelves(vector>& books, int shelfWidth) { 19 | memset(dp,-1,sizeof(dp)); 20 | WIDTH = shelfWidth; 21 | return solve(0,books,shelfWidth,0); 22 | } 23 | }; -------------------------------------------------------------------------------- /1694-make-sum-divisible-by-p/1694-make-sum-divisible-by-p.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minSubarray(vector& nums, int p) { 4 | int n = nums.size(); 5 | long long sum = accumulate(nums.begin(),nums.end(),0LL); 6 | int rem = sum%p; 7 | if(rem == 0) return 0; 8 | unordered_map mp; 9 | mp[0] = -1; 10 | int ans = n; 11 | long long prefix_sum = 0; 12 | long long curr_mod; 13 | int target_mod; 14 | for(int i=0;i mp; 7 | for(int i=0;i= 1){ 23 | greater=true; 24 | break; 25 | } 26 | } 27 | if(!greater){ 28 | return true; 29 | } 30 | 31 | mp[s2[i]]++; 32 | i++; 33 | } 34 | j++; 35 | } 36 | 37 | return false; 38 | } 39 | }; -------------------------------------------------------------------------------- /0040-combination-sum-ii/0040-combination-sum-ii.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> ans; 4 | void solve(vector& nums, int target,vector & curr, int idx) 5 | { 6 | if(target == 0) 7 | { 8 | if(find(ans.begin(), ans.end(), curr) != ans.end()) return; 9 | ans.push_back(curr); 10 | return; 11 | } 12 | 13 | for(int i=idx;i idx && nums[i] == nums[i-1]) continue; 16 | if(nums[i]>target) break; 17 | curr.push_back(nums[i]); 18 | solve(nums,target-nums[i],curr,i+1); 19 | curr.pop_back(); 20 | } 21 | } 22 | vector> combinationSum2(vector& candidates, int target) { 23 | vector curr; 24 | sort(candidates.begin(),candidates.end()); 25 | solve(candidates,target,curr,0); 26 | return ans; 27 | } 28 | }; -------------------------------------------------------------------------------- /Graphs/BipartiteGraph.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // using 2 colors --> 0,1 5 | class Solution { 6 | public: 7 | vector visited; 8 | bool Bipartite = true; 9 | void dfs(int u,vectoradj[], int color) 10 | { 11 | if(visited[u] != -1) return; 12 | visited[u] = color; 13 | for(int v : adj[u]) 14 | { 15 | if(visited[v] == -1) dfs(v,adj,1-color); 16 | else if(visited[v] == color) 17 | { 18 | Bipartite = false; 19 | return; 20 | } 21 | } 22 | 23 | } 24 | bool isBipartite(int V, vectoradj[]){ 25 | int n = V; 26 | visited.resize(n, -1); 27 | 28 | for (int i = 0; i < n; i++) { 29 | if (visited[i] == -1) { 30 | dfs(i, adj, 1); 31 | } 32 | } 33 | 34 | return Bipartite; 35 | } 36 | 37 | }; 38 | -------------------------------------------------------------------------------- /0090-subsets-ii/README.md: -------------------------------------------------------------------------------- 1 |

90. Subsets II

Medium


Given an integer array nums that may contain duplicates, return all possible subsets (the power set).

2 | 3 |

The solution set must not contain duplicate subsets. Return the solution in any order.

4 | 5 |

 

6 |

Example 1:

7 |
Input: nums = [1,2,2]
 8 | Output: [[],[1],[1,2],[1,2,2],[2],[2,2]]
 9 | 

Example 2:

10 |
Input: nums = [0]
11 | Output: [[],[0]]
12 | 
13 |

 

14 |

Constraints:

15 | 16 |
    17 |
  • 1 <= nums.length <= 10
  • 18 |
  • -10 <= nums[i] <= 10
  • 19 |
20 | -------------------------------------------------------------------------------- /0214-shortest-palindrome/README.md: -------------------------------------------------------------------------------- 1 |

214. Shortest Palindrome

Hard


You are given a string s. You can convert s to a palindrome by adding characters in front of it.

2 | 3 |

Return the shortest palindrome you can find by performing this transformation.

4 | 5 |

 

6 |

Example 1:

7 |
Input: s = "aacecaaa"
 8 | Output: "aaacecaaa"
 9 | 

Example 2:

10 |
Input: s = "abcd"
11 | Output: "dcbabcd"
12 | 
13 |

 

14 |

Constraints:

15 | 16 |
    17 |
  • 0 <= s.length <= 5 * 104
  • 18 |
  • s consists of lowercase English letters only.
  • 19 |
20 | -------------------------------------------------------------------------------- /0064-minimum-path-sum/0064-minimum-path-sum.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int solve(int row,int col,vector>& grid, vector> &dp) 4 | { 5 | if(row == grid.size()-1 && col == grid[0].size()-1) return grid[row][col]; 6 | 7 | if(dp[row][col]) return dp[row][col]; 8 | 9 | if(col == grid[0].size()-1) 10 | { 11 | return dp[row][col] = grid[row][col] + solve(row+1,col,grid,dp); 12 | } 13 | else if(row == grid.size()-1) 14 | { 15 | return dp[row][col] = grid[row][col] + solve(row,col+1,grid,dp); 16 | } 17 | else return dp[row][col] = grid[row][col] + min(solve(row+1,col,grid,dp), solve(row,col+1,grid,dp)); 18 | } 19 | int minPathSum(vector>& grid) { 20 | int n = grid.size(); 21 | int m = grid[0].size(); 22 | vector> dp(n, vector(m,0)); 23 | return solve(0,0,grid,dp); 24 | } 25 | }; -------------------------------------------------------------------------------- /0110-balanced-binary-tree/0110-balanced-binary-tree.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | class Solution { 13 | public: 14 | int balanced(TreeNode* root,bool &flag) 15 | { 16 | if(root== NULL) 17 | { 18 | return 0; 19 | } 20 | 21 | int left = balanced(root->left,flag); 22 | int right = balanced(root->right,flag); 23 | if(abs(left-right) > 1)flag = false; 24 | 25 | return max(left,right)+1; 26 | } 27 | bool isBalanced(TreeNode* root) { 28 | bool flag = true; 29 | balanced(root,flag); 30 | return flag; 31 | } 32 | }; -------------------------------------------------------------------------------- /1402-count-square-submatrices-with-all-ones/1402-count-square-submatrices-with-all-ones.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int countSquares(vector>& matrix) { 4 | int n = matrix.size(); 5 | int m = matrix[0].size(); 6 | vector> dp(n,vector(m,0)); 7 | for(int j=0;jval; 19 | if(root->left == NULL && root->right == NULL && currSum == targetSum) return true; 20 | bool left = dfs(root->left,targetSum, currSum); 21 | bool right = dfs(root->right, targetSum, currSum); 22 | 23 | return left || right; 24 | } 25 | bool hasPathSum(TreeNode* root, int targetSum) { 26 | return dfs(root,targetSum,0); 27 | } 28 | }; -------------------------------------------------------------------------------- /0131-palindrome-partitioning/README.md: -------------------------------------------------------------------------------- 1 |

131. Palindrome Partitioning

Medium


Given a string s, partition s such that every substring of the partition is a palindrome. Return all possible palindrome partitioning of s.

2 | 3 |

 

4 |

Example 1:

5 |
Input: s = "aab"
 6 | Output: [["a","a","b"],["aa","b"]]
 7 | 

Example 2:

8 |
Input: s = "a"
 9 | Output: [["a"]]
10 | 
11 |

 

12 |

Constraints:

13 | 14 |
    15 |
  • 1 <= s.length <= 16
  • 16 |
  • s contains only lowercase English letters.
  • 17 |
18 | -------------------------------------------------------------------------------- /Arrays/TwoSum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // brute force 5 | class Solution { 6 | public: 7 | vector twoSum(vector& nums, int target) { 8 | int n = nums.size(); 9 | for(int i=0;i twoSum(vector& nums, int target) { 25 | int n = nums.size(); 26 | unordered_map mp; 27 | for(int i=0;i96. Unique Binary Search Trees

Medium


Given an integer n, return the number of structurally unique BST's (binary search trees) which has exactly n nodes of unique values from 1 to n.

2 | 3 |

 

4 |

Example 1:

5 | 6 |
 7 | Input: n = 3
 8 | Output: 5
 9 | 
10 | 11 |

Example 2:

12 | 13 |
14 | Input: n = 1
15 | Output: 1
16 | 
17 | 18 |

 

19 |

Constraints:

20 | 21 |
    22 |
  • 1 <= n <= 19
  • 23 |
24 | -------------------------------------------------------------------------------- /0438-find-all-anagrams-in-a-string/0438-find-all-anagrams-in-a-string.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector findAnagrams(string s, string p) { 4 | int n = s.size(); 5 | int m = p.size(); 6 | unordered_map mp; 7 | for(int i=0;i ans; 12 | int i=0, j=0; 13 | while(j= 1){ 24 | greater=true; 25 | break; 26 | } 27 | } 28 | if(!greater){ 29 | ans.push_back(i); 30 | } 31 | 32 | mp[s[i]]++; 33 | i++; 34 | } 35 | j++; 36 | } 37 | 38 | return ans; 39 | } 40 | }; -------------------------------------------------------------------------------- /Greedy/minCostAcquiringCoins.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // Converts coin[] to prefix sum array 5 | void preprocess(int coin[], int n) 6 | { 7 | // sort the coins value 8 | sort(coin, coin + n); 9 | 10 | // Maintain prefix sum array 11 | for (int i = 1; i <= n - 1; i++) 12 | coin[i] += coin[i - 1]; 13 | } 14 | 15 | // Function to calculate min 16 | // cost when we can get k extra 17 | // coins after paying cost of one. 18 | int minCost(int coin[], int n, int k) 19 | { 20 | // calculate no. of coins needed 21 | int coins_needed = ceil(1.0 * n / (k + 1)); 22 | 23 | // return sum of from prefix array 24 | return coin[coins_needed - 1]; 25 | } 26 | 27 | int main() 28 | { 29 | int coin[] = {8, 5, 3, 10, 30 | 2, 1, 15, 25}; 31 | int n = sizeof(coin) / sizeof(coin[0]); 32 | preprocess(coin, n); 33 | int k = 3; 34 | cout << minCost(coin, n, k) << endl; 35 | k = 7; 36 | cout << minCost(coin, n, k) << endl; 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /0258-add-digits/README.md: -------------------------------------------------------------------------------- 1 |

258. Add Digits

Easy


Given an integer num, repeatedly add all its digits until the result has only one digit, and return it.

2 | 3 |

 

4 |

Example 1:

5 | 6 |
 7 | Input: num = 38
 8 | Output: 2
 9 | Explanation: The process is
10 | 38 --> 3 + 8 --> 11
11 | 11 --> 1 + 1 --> 2 
12 | Since 2 has only one digit, return it.
13 | 
14 | 15 |

Example 2:

16 | 17 |
18 | Input: num = 0
19 | Output: 0
20 | 
21 | 22 |

 

23 |

Constraints:

24 | 25 |
    26 |
  • 0 <= num <= 231 - 1
  • 27 |
28 | 29 |

 

30 |

Follow up: Could you do it without any loop/recursion in O(1) runtime?

31 | -------------------------------------------------------------------------------- /queue/queueUsingStacks.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class MyQueue { 5 | public: 6 | stack s1; 7 | stack s2; 8 | MyQueue() { 9 | 10 | } 11 | 12 | void push(int x) { 13 | s1.push(x); 14 | } 15 | 16 | int pop() { 17 | while(!s1.empty()) 18 | { 19 | int elem = s1.top(); 20 | s1.pop(); 21 | s2.push(elem); 22 | }; 23 | int elem = s2.top(); 24 | s2.pop(); 25 | while(!s2.empty()) 26 | { 27 | int elem = s2.top(); 28 | s2.pop(); 29 | s1.push(elem); 30 | }; 31 | return elem; 32 | } 33 | 34 | int peek() { 35 | while(!s1.empty()) 36 | { 37 | int elem = s1.top(); 38 | s1.pop(); 39 | s2.push(elem); 40 | }; 41 | int elem = s2.top(); 42 | while(!s2.empty()) 43 | { 44 | int elem = s2.top(); 45 | s2.pop(); 46 | s1.push(elem); 47 | }; 48 | return elem; 49 | } 50 | 51 | bool empty() { 52 | return s1.empty(); 53 | } 54 | }; 55 | -------------------------------------------------------------------------------- /0404-sum-of-left-leaves/0404-sum-of-left-leaves.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | class Solution { 13 | public: 14 | int sum =0; 15 | void solve(TreeNode* root, TreeNode* parent) 16 | { 17 | if(!root) return; 18 | 19 | if(root->left == NULL && root->right == NULL) 20 | { 21 | if(parent != NULL && parent->left == root) 22 | { 23 | sum+= root->val; 24 | } 25 | } 26 | solve(root->left,root); 27 | solve(root->right,root); 28 | } 29 | int sumOfLeftLeaves(TreeNode* root) { 30 | solve(root,NULL); 31 | return sum; 32 | } 33 | }; -------------------------------------------------------------------------------- /0664-strange-printer/0664-strange-printer.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int n; 4 | vector> dp; 5 | int solve(int l, int r,string &s) 6 | { 7 | if(l==r) return 1; 8 | if(l>r) return 0; 9 | if(dp[l][r] != -1) return dp[l][r]; 10 | int i = l+1; 11 | while(i<=r && s[i] == s[l]) i++; 12 | if(i==r+1) return dp[l][r] = 1; 13 | int basic = 1+ solve(i,r,s); 14 | 15 | int greedy = INT_MAX; 16 | for(int j=i;j<=r;j++) 17 | { 18 | if(s[j] == s[l]) 19 | { 20 | int ans = solve(i,j-1,s) + solve(j,r,s); 21 | greedy = min(greedy, ans); 22 | } 23 | } 24 | return dp[l][r] = min(basic,greedy); 25 | } 26 | int strangePrinter(string s) { 27 | n = s.size(); 28 | dp.resize(n+1,vector(n+1,-1)); 29 | return solve(0,n-1,s); 30 | } 31 | }; -------------------------------------------------------------------------------- /2255-minimum-swaps-to-group-all-1s-together-ii/2255-minimum-swaps-to-group-all-1s-together-ii.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | class Solution { 6 | public: 7 | int minSwaps(vector& nums) { 8 | int n = nums.size(); 9 | int oneCount = 0; 10 | 11 | for(int i = 0; i < n; i++) { 12 | if(nums[i] == 1) oneCount++; 13 | } 14 | 15 | if (oneCount == 0 || oneCount == n) return 0; 16 | 17 | nums.insert(nums.end(), nums.begin(), nums.end()); 18 | 19 | int zeroCount = 0; 20 | for(int i = 0; i < oneCount; i++) { 21 | if(nums[i] == 0) zeroCount++; 22 | } 23 | 24 | int minSwaps = zeroCount; 25 | for(int i = oneCount; i < 2 * n; i++) { 26 | if(nums[i - oneCount] == 0) zeroCount--; 27 | if(nums[i] == 0) zeroCount++; 28 | minSwaps = min(minSwaps, zeroCount); 29 | } 30 | 31 | return minSwaps; 32 | } 33 | }; 34 | -------------------------------------------------------------------------------- /0108-convert-sorted-array-to-binary-search-tree/0108-convert-sorted-array-to-binary-search-tree.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | class Solution { 13 | public: 14 | TreeNode* recur(vector& nums, int start, int end) 15 | { 16 | if(start > end) return NULL; 17 | int mid = (start + end)/2; 18 | TreeNode* root = new TreeNode(nums[mid]); 19 | 20 | root->left = recur(nums, start, mid-1); 21 | root->right = recur(nums, mid+1, end); 22 | 23 | return root; 24 | } 25 | TreeNode* sortedArrayToBST(vector& nums) { 26 | int n = nums.size(); 27 | return recur(nums, 0, n-1); 28 | } 29 | }; -------------------------------------------------------------------------------- /0307-range-sum-query-mutable/0307-range-sum-query-mutable.cpp: -------------------------------------------------------------------------------- 1 | class NumArray { 2 | public: 3 | vector nums; 4 | vector PrefixSum; 5 | 6 | NumArray(vector& nums) : nums(nums) { 7 | PrefixSum.resize(nums.size() + 1); 8 | PrefixSum[0] = 0; 9 | for (int i = 0; i < nums.size(); i++) { 10 | PrefixSum[i + 1] = PrefixSum[i] + nums[i]; 11 | } 12 | } 13 | 14 | void update(int index, int val) { 15 | int diff = val - nums[index]; 16 | nums[index] = val; 17 | 18 | for (int i = index + 1; i < PrefixSum.size(); i++) { 19 | PrefixSum[i] += diff; 20 | } 21 | } 22 | 23 | int sumRange(int left, int right) { 24 | return PrefixSum[right + 1] - PrefixSum[left]; 25 | } 26 | }; 27 | 28 | /** 29 | * Your NumArray object will be instantiated and called as such: 30 | * NumArray* obj = new NumArray(nums); 31 | * obj->update(index,val); 32 | * int param_2 = obj->sumRange(left,right); 33 | */ -------------------------------------------------------------------------------- /0440-k-th-smallest-in-lexicographical-order/README.md: -------------------------------------------------------------------------------- 1 |

440. K-th Smallest in Lexicographical Order

Hard


Given two integers n and k, return the kth lexicographically smallest integer in the range [1, n].

2 | 3 |

 

4 |

Example 1:

5 | 6 |
 7 | Input: n = 13, k = 2
 8 | Output: 10
 9 | Explanation: The lexicographical order is [1, 10, 11, 12, 13, 2, 3, 4, 5, 6, 7, 8, 9], so the second smallest number is 10.
10 | 
11 | 12 |

Example 2:

13 | 14 |
15 | Input: n = 1, k = 1
16 | Output: 1
17 | 
18 | 19 |

 

20 |

Constraints:

21 | 22 |
    23 |
  • 1 <= k <= n <= 109
  • 24 |
25 | -------------------------------------------------------------------------------- /0890-lemonade-change/0890-lemonade-change.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool lemonadeChange(vector& bills) { 4 | int n = bills.size(); 5 | unordered_map mp; 6 | for(int i : bills) 7 | { 8 | if(i==5) 9 | { 10 | mp[i]++; 11 | } 12 | else if(i == 10) 13 | { 14 | if(mp[5] >0) 15 | { 16 | mp[5]--; 17 | mp[i]++; 18 | }else return false; 19 | }else{ 20 | if(mp[10] > 0 && mp[5] > 0) 21 | { 22 | mp[5]--; 23 | mp[10]--; 24 | } 25 | else if(mp[5]>=3) 26 | { 27 | mp[5]--; 28 | mp[5]--; 29 | mp[5]--; 30 | }else return false; 31 | } 32 | } 33 | return true; 34 | } 35 | }; -------------------------------------------------------------------------------- /Graphs/numberOfIslands.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | int n,m; 7 | vector> visited; 8 | void dfs(vector>& grid,int i,int j) 9 | { 10 | if(i<0 || j<0 || i>=n || j>=m || grid[i][j]=='0' || visited[i][j]) return; 11 | 12 | visited[i][j] = true; 13 | 14 | dfs(grid,i+1,j); 15 | dfs(grid,i-1,j); 16 | dfs(grid,i,j+1); 17 | dfs(grid,i,j-1); 18 | } 19 | int numIslands(vector>& grid) { 20 | n = grid.size(); 21 | m = grid[0].size(); 22 | visited.resize(n,vector(m,false)); 23 | int count =0; 24 | for(int i=0;i 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | void dfs(int sr,int sc, vector>& image, int color, int iniColor,int n,int m) { 7 | if (sr < 0 || sc < 0 || sr >= n || sc >= m || image[sr][sc] != iniColor) { 8 | return; 9 | } 10 | 11 | image[sr][sc] = color; 12 | 13 | dfs (sr+ 1, sc, image, color, iniColor, n, m); // Down 14 | dfs(sr - 1, sc, image, color, iniColor, n, m); // Up 15 | dfs(sr, sc + 1, image, color, iniColor, n, m); // Right 16 | dfs(sr, sc - 1, image, color, iniColor, n, m); // Left 17 | } 18 | vector> floodFill(vector>& image, int sr, int sc, int color) { 19 | vector> temp = image; 20 | int iniColor= image[sr][sc]; 21 | int n = image.size(); 22 | int m = image[0].size(); 23 | if(iniColor == color) return image; 24 | dfs(sr,sc,temp,color,iniColor,n,m); 25 | return temp; 26 | } 27 | }; -------------------------------------------------------------------------------- /0778-reorganize-string/0778-reorganize-string.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string reorganizeString(string s) { 4 | int n = s.length(); 5 | unordered_map mp; 6 | priority_queue> pq; 7 | for(auto ch : s) 8 | { 9 | mp[ch]++; 10 | if(mp[ch] > (n+1)/2) return ""; 11 | } 12 | for(auto p : mp) 13 | { 14 | pq.push({p.second,p.first}); 15 | } 16 | string result = ""; 17 | while(pq.size() >= 2) 18 | { 19 | auto p1 = pq.top(); 20 | pq.pop(); 21 | auto p2 = pq.top(); 22 | pq.pop(); 23 | 24 | result.push_back(p1.second); 25 | result.push_back(p2.second); 26 | p1.first -= 1; 27 | p2.first -= 1; 28 | if(p1.first >=1) pq.push(p1); 29 | if(p2.first >=1) pq.push(p2); 30 | } 31 | if(!pq.empty()) result.push_back(pq.top().second); 32 | return result; 33 | } 34 | }; -------------------------------------------------------------------------------- /0530-minimum-absolute-difference-in-bst/0530-minimum-absolute-difference-in-bst.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | class Solution { 13 | public: 14 | vector inorder; 15 | void inorderTraversal(TreeNode* root) 16 | { 17 | if(!root) return; 18 | inorderTraversal(root->left); 19 | inorder.push_back(root->val); 20 | inorderTraversal(root->right); 21 | } 22 | int getMinimumDifference(TreeNode* root) { 23 | inorderTraversal(root); 24 | int diff = INT_MAX; 25 | for(int i=1;i st; 4 | vector inc; 5 | int size; 6 | int top; 7 | 8 | CustomStack(int maxSize) { 9 | st = vector(maxSize); 10 | inc = vector(maxSize, 0); 11 | size = maxSize; 12 | top = -1; 13 | } 14 | 15 | void push(int x) { 16 | if(top + 1 < size) { 17 | top++; 18 | st[top] = x; 19 | } 20 | } 21 | 22 | int pop() { 23 | if(top == -1) return -1; 24 | int poppedValue = st[top] + inc[top]; 25 | if(top > 0) { 26 | inc[top-1] += inc[top]; 27 | } 28 | 29 | inc[top] = 0; 30 | top--; 31 | 32 | return poppedValue; 33 | } 34 | 35 | void increment(int k, int val) { 36 | int limit = min(k, top + 1); 37 | if(limit > 0) { 38 | inc[limit - 1] += val; 39 | } 40 | } 41 | }; 42 | -------------------------------------------------------------------------------- /0131-palindrome-partitioning/0131-palindrome-partitioning.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> ans; 4 | bool palindrome(string s, int start, int end) 5 | { 6 | while(start <= end) 7 | { 8 | if(s[start] != s[end]) return false; 9 | 10 | start++; 11 | end--; 12 | } 13 | return true; 14 | } 15 | void solve(string s, vector &curr, int idx) 16 | { 17 | if(idx == s.size()) 18 | { 19 | ans.push_back(curr); 20 | return; 21 | } 22 | 23 | for(int i=idx;i> partition(string s) { 34 | vector curr; 35 | solve(s,curr,0); 36 | return ans; 37 | } 38 | }; -------------------------------------------------------------------------------- /0257-binary-tree-paths/0257-binary-tree-paths.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | class Solution { 13 | public: 14 | vector ans; 15 | void dfs(TreeNode* root, string temp) 16 | { 17 | if (!root) return; 18 | 19 | if (!temp.empty()) { 20 | temp += "->"; 21 | } 22 | temp += to_string(root->val); 23 | 24 | if (!root->left && !root->right) { 25 | ans.push_back(temp); 26 | return; 27 | } 28 | 29 | dfs(root->left, temp); 30 | dfs(root->right, temp); 31 | } 32 | vector binaryTreePaths(TreeNode* root) { 33 | dfs(root,""); 34 | return ans; 35 | } 36 | }; -------------------------------------------------------------------------------- /0273-integer-to-english-words/README.md: -------------------------------------------------------------------------------- 1 |

273. Integer to English Words

Hard


Convert a non-negative integer num to its English words representation.

2 | 3 |

 

4 |

Example 1:

5 | 6 |
 7 | Input: num = 123
 8 | Output: "One Hundred Twenty Three"
 9 | 
10 | 11 |

Example 2:

12 | 13 |
14 | Input: num = 12345
15 | Output: "Twelve Thousand Three Hundred Forty Five"
16 | 
17 | 18 |

Example 3:

19 | 20 |
21 | Input: num = 1234567
22 | Output: "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"
23 | 
24 | 25 |

 

26 |

Constraints:

27 | 28 |
    29 |
  • 0 <= num <= 231 - 1
  • 30 |
31 | -------------------------------------------------------------------------------- /0042-trapping-rain-water/0042-trapping-rain-water.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector calcLeft(vector& height) 4 | { 5 | vector ans(height.size()); 6 | ans[0] = height[0]; 7 | for(int i=1;i calcRight(vector& height) 14 | { 15 | int n = height.size(); 16 | vector ans(n); 17 | ans[n-1] = height[n-1]; 18 | for(int i=n-2;i>=0;i--) 19 | { 20 | ans[i] = max(ans[i+1], height[i]); 21 | } 22 | return ans; 23 | } 24 | int trap(vector& height) { 25 | int count =0; 26 | int n = height.size(); 27 | vector leftMax = calcLeft(height); 28 | vector rightMax = calcRight(height); 29 | for(int i=0;i46. Permutations

Medium


Given an array nums of distinct integers, return all the possible permutations. You can return the answer in any order.

2 | 3 |

 

4 |

Example 1:

5 |
Input: nums = [1,2,3]
 6 | Output: [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
 7 | 

Example 2:

8 |
Input: nums = [0,1]
 9 | Output: [[0,1],[1,0]]
10 | 

Example 3:

11 |
Input: nums = [1]
12 | Output: [[1]]
13 | 
14 |

 

15 |

Constraints:

16 | 17 |
    18 |
  • 1 <= nums.length <= 6
  • 19 |
  • -10 <= nums[i] <= 10
  • 20 |
  • All the integers of nums are unique.
  • 21 |
22 | -------------------------------------------------------------------------------- /0257-binary-tree-paths/README.md: -------------------------------------------------------------------------------- 1 |

257. Binary Tree Paths

Easy


Given the root of a binary tree, return all root-to-leaf paths in any order.

2 | 3 |

A leaf is a node with no children.

4 | 5 |

 

6 |

Example 1:

7 | 8 |
 9 | Input: root = [1,2,3,null,5]
10 | Output: ["1->2->5","1->3"]
11 | 
12 | 13 |

Example 2:

14 | 15 |
16 | Input: root = [1]
17 | Output: ["1"]
18 | 
19 | 20 |

 

21 |

Constraints:

22 | 23 |
    24 |
  • The number of nodes in the tree is in the range [1, 100].
  • 25 |
  • -100 <= Node.val <= 100
  • 26 |
27 | -------------------------------------------------------------------------------- /0943-sum-of-subarray-minimums/0943-sum-of-subarray-minimums.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int sumSubarrayMins(vector& arr) { 4 | int n = arr.size(); 5 | vector left(n), right(n); 6 | stack> st; 7 | 8 | for (int i = 0; i < n; i++) { 9 | while (!st.empty() && st.top().first > arr[i]) 10 | st.pop(); 11 | left[i] = st.empty() ? -1 : st.top().second; 12 | st.push({arr[i], i}); 13 | } 14 | 15 | while (!st.empty()) st.pop(); 16 | 17 | for (int i = n - 1; i >= 0; i--) { 18 | while (!st.empty() && st.top().first >= arr[i]) 19 | st.pop(); 20 | right[i] = st.empty() ? n : st.top().second; 21 | st.push({arr[i], i}); 22 | } 23 | 24 | long long result = 0; 25 | const int MOD = 1e9 + 7; 26 | for (int i = 0; i < n; ++i) { 27 | result = (result + (long long)arr[i] * (i - left[i]) * (right[i] - i)) % MOD; 28 | } 29 | 30 | return result; 31 | } 32 | }; -------------------------------------------------------------------------------- /0034-find-first-and-last-position-of-element-in-sorted-array/0034-find-first-and-last-position-of-element-in-sorted-array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int BS(vector &nums, int s, int e, bool isLeft, int target) 4 | { 5 | int ans = -1; 6 | while(s<=e) 7 | { 8 | int m = s+(e-s)/2; 9 | if(nums[m] == target) 10 | { 11 | ans = m; 12 | if(isLeft) e = m-1; 13 | else s = m+1; 14 | } 15 | else if(nums[m] < target) 16 | { 17 | s = m+1; 18 | }else{ 19 | e = m-1; 20 | } 21 | } 22 | return ans; 23 | } 24 | vector searchRange(vector& nums, int target) { 25 | vector ans(2); 26 | int n = nums.size(); 27 | int s= 0, e = n-1; 28 | 29 | int start = BS(nums,0,n-1,true,target); 30 | int end = BS(nums,0,n-1,false,target); 31 | 32 | ans[0] = start; 33 | ans[1] = end; 34 | return ans; 35 | } 36 | }; -------------------------------------------------------------------------------- /0086-partition-list/0086-partition-list.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode() : val(0), next(nullptr) {} 7 | * ListNode(int x) : val(x), next(nullptr) {} 8 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 9 | * }; 10 | */ 11 | class Solution { 12 | public: 13 | ListNode* partition(ListNode* head, int x) { 14 | ListNode* curr = head; 15 | ListNode* leftHead = new ListNode(0); 16 | ListNode* rightHead= new ListNode(0); 17 | ListNode* l = leftHead; 18 | ListNode* r = rightHead; 19 | while(curr) 20 | { 21 | if(curr->val < x) 22 | { 23 | l->next = new ListNode(curr->val); 24 | l = l->next; 25 | }else{ 26 | rightHead->next = new ListNode(curr->val); 27 | rightHead = rightHead->next; 28 | } 29 | curr = curr->next; 30 | } 31 | l->next = r->next; 32 | return leftHead->next; 33 | } 34 | }; -------------------------------------------------------------------------------- /0264-ugly-number-ii/README.md: -------------------------------------------------------------------------------- 1 |

264. Ugly Number II

Medium


An ugly number is a positive integer whose prime factors are limited to 2, 3, and 5.

2 | 3 |

Given an integer n, return the nth ugly number.

4 | 5 |

 

6 |

Example 1:

7 | 8 |
 9 | Input: n = 10
10 | Output: 12
11 | Explanation: [1, 2, 3, 4, 5, 6, 8, 9, 10, 12] is the sequence of the first 10 ugly numbers.
12 | 
13 | 14 |

Example 2:

15 | 16 |
17 | Input: n = 1
18 | Output: 1
19 | Explanation: 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.
20 | 
21 | 22 |

 

23 |

Constraints:

24 | 25 |
    26 |
  • 1 <= n <= 1690
  • 27 |
28 | -------------------------------------------------------------------------------- /0435-non-overlapping-intervals/0435-non-overlapping-intervals.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int eraseOverlapIntervals(vector>& intervals) { 4 | int n = intervals.size(); 5 | sort(intervals.begin(),intervals.end()); 6 | int count =0; 7 | int i=0, j=1; 8 | while(j < n) 9 | { 10 | vector curr_interval = intervals[i]; 11 | vector next_interval = intervals[j]; 12 | int cs = curr_interval[0]; 13 | int ce = curr_interval[1]; 14 | int ns = next_interval[0]; 15 | int ne = next_interval[1]; 16 | if(ce <= ns) // no overlapping 17 | { 18 | i=j; 19 | j++; 20 | } 21 | else if(ce <= ne) // overlapping 22 | { 23 | j++; 24 | count++; 25 | } 26 | else if(ce > ne) 27 | { 28 | i=j; 29 | j++; 30 | count++; 31 | } 32 | } 33 | return count; 34 | } 35 | }; -------------------------------------------------------------------------------- /0078-subsets/README.md: -------------------------------------------------------------------------------- 1 |

78. Subsets

Medium


Given an integer array nums of unique elements, return all possible subsets (the power set).

2 | 3 |

The solution set must not contain duplicate subsets. Return the solution in any order.

4 | 5 |

 

6 |

Example 1:

7 | 8 |
 9 | Input: nums = [1,2,3]
10 | Output: [[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]
11 | 
12 | 13 |

Example 2:

14 | 15 |
16 | Input: nums = [0]
17 | Output: [[],[0]]
18 | 
19 | 20 |

 

21 |

Constraints:

22 | 23 |
    24 |
  • 1 <= nums.length <= 10
  • 25 |
  • -10 <= nums[i] <= 10
  • 26 |
  • All the numbers of nums are unique.
  • 27 |
28 | -------------------------------------------------------------------------------- /0095-unique-binary-search-trees-ii/README.md: -------------------------------------------------------------------------------- 1 |

95. Unique Binary Search Trees II

Medium


Given an integer n, return all the structurally unique BST's (binary search trees), which has exactly n nodes of unique values from 1 to n. Return the answer in any order.

2 | 3 |

 

4 |

Example 1:

5 | 6 |
 7 | Input: n = 3
 8 | Output: [[1,null,2,null,3],[1,null,3,2],[2,1,3],[3,1,null,null,2],[3,2,null,1]]
 9 | 
10 | 11 |

Example 2:

12 | 13 |
14 | Input: n = 1
15 | Output: [[1]]
16 | 
17 | 18 |

 

19 |

Constraints:

20 | 21 |
    22 |
  • 1 <= n <= 8
  • 23 |
24 | -------------------------------------------------------------------------------- /Arrays/3Sum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | vector> threeSum(vector& nums) { 7 | int n = nums.size(); 8 | vector> ans; 9 | sort(nums.begin(), nums.end()); 10 | for(int i = 0; i < n - 2; ++i) { 11 | if (i > 0 && nums[i] == nums[i - 1]) continue; 12 | int j = i + 1; 13 | int k = n - 1; 14 | while(j < k) { 15 | int sum = nums[i] + nums[j] + nums[k]; 16 | if (sum == 0) { 17 | ans.push_back({nums[i], nums[j], nums[k]}); 18 | while (j < k && nums[j] == nums[j + 1]) ++j; 19 | while (j < k && nums[k] == nums[k - 1]) --k; 20 | ++j; 21 | --k; 22 | } else if (sum < 0) { 23 | ++j; 24 | } else { 25 | --k; 26 | } 27 | } 28 | } 29 | return ans; 30 | } 31 | }; 32 | 33 | int main(){ 34 | 35 | return 0; 36 | } -------------------------------------------------------------------------------- /0416-partition-equal-subset-sum/README.md: -------------------------------------------------------------------------------- 1 |

416. Partition Equal Subset Sum

Medium


Given an integer array nums, return true if you can partition the array into two subsets such that the sum of the elements in both subsets is equal or false otherwise.

2 | 3 |

 

4 |

Example 1:

5 | 6 |
 7 | Input: nums = [1,5,11,5]
 8 | Output: true
 9 | Explanation: The array can be partitioned as [1, 5, 5] and [11].
10 | 
11 | 12 |

Example 2:

13 | 14 |
15 | Input: nums = [1,2,3,5]
16 | Output: false
17 | Explanation: The array cannot be partitioned into equal sum subsets.
18 | 
19 | 20 |

 

21 |

Constraints:

22 | 23 |
    24 |
  • 1 <= nums.length <= 200
  • 25 |
  • 1 <= nums[i] <= 100
  • 26 |
27 | -------------------------------------------------------------------------------- /0215-kth-largest-element-in-an-array/README.md: -------------------------------------------------------------------------------- 1 |

215. Kth Largest Element in an Array

Medium


Given an integer array nums and an integer k, return the kth largest element in the array.

2 | 3 |

Note that it is the kth largest element in the sorted order, not the kth distinct element.

4 | 5 |

Can you solve it without sorting?

6 | 7 |

 

8 |

Example 1:

9 |
Input: nums = [3,2,1,5,6,4], k = 2
10 | Output: 5
11 | 

Example 2:

12 |
Input: nums = [3,2,3,1,2,4,5,5,6], k = 4
13 | Output: 4
14 | 
15 |

 

16 |

Constraints:

17 | 18 |
    19 |
  • 1 <= k <= nums.length <= 105
  • 20 |
  • -104 <= nums[i] <= 104
  • 21 |
22 | -------------------------------------------------------------------------------- /LinkedList/reverseLL2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | /** 5 | * Definition for singly-linked list. 6 | * struct ListNode { 7 | * int val; 8 | * ListNode *next; 9 | * ListNode() : val(0), next(nullptr) {} 10 | * ListNode(int x) : val(x), next(nullptr) {} 11 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 12 | * }; 13 | */ 14 | class Solution { 15 | public: 16 | ListNode* reverseBetween(ListNode* head, int left, int right) { 17 | if(head == NULL || head->next == NULL || left>right) return head; 18 | ListNode* dummy = new ListNode(-1); 19 | dummy->next = head; 20 | ListNode* prev = dummy; 21 | for(int i=1;inext; 24 | } 25 | 26 | ListNode* curr = prev->next; 27 | for(int i=0;inext; 30 | prev->next = curr->next; 31 | curr->next = curr->next->next; 32 | prev->next->next = temp; 33 | } 34 | return dummy->next; 35 | } 36 | }; -------------------------------------------------------------------------------- /0104-maximum-depth-of-binary-tree/README.md: -------------------------------------------------------------------------------- 1 |

104. Maximum Depth of Binary Tree

Easy


Given the root of a binary tree, return its maximum depth.

2 | 3 |

A binary tree's maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.

4 | 5 |

 

6 |

Example 1:

7 | 8 |
 9 | Input: root = [3,9,20,null,null,15,7]
10 | Output: 3
11 | 
12 | 13 |

Example 2:

14 | 15 |
16 | Input: root = [1,null,2]
17 | Output: 2
18 | 
19 | 20 |

 

21 |

Constraints:

22 | 23 |
    24 |
  • The number of nodes in the tree is in the range [0, 104].
  • 25 |
  • -100 <= Node.val <= 100
  • 26 |
27 | -------------------------------------------------------------------------------- /0199-binary-tree-right-side-view/0199-binary-tree-right-side-view.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | class Solution { 13 | public: 14 | vector rightSideView(TreeNode* root) { 15 | if(!root) return{}; 16 | vector ans; 17 | queue q; 18 | q.push(root); 19 | while(!q.empty()) 20 | { 21 | int size = q.size(); 22 | TreeNode* curr; 23 | while(size--) 24 | { 25 | curr = q.front(); 26 | q.pop(); 27 | if(curr->left != NULL) q.push(curr->left); 28 | if(curr->right != NULL) q.push(curr->right); 29 | } 30 | ans.push_back(curr->val); 31 | } 32 | return ans; 33 | } 34 | }; -------------------------------------------------------------------------------- /LinkedList/CopyListWithRandomPtrs.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct Node 5 | { 6 | int val; 7 | Node* next; 8 | Node* random; 9 | 10 | Node(int x){ 11 | val = x; 12 | next = NULL; 13 | } 14 | }; 15 | 16 | // Approach 1 --> using map 17 | class Solution { 18 | public: 19 | Node* copyRandomList(Node* head) { 20 | unordered_map mp; 21 | Node* newHead = new Node(-1); 22 | Node* curr1 = head; 23 | Node* prev = newHead; 24 | // traverse for data and next ptr 25 | while(curr1!= NULL) 26 | { 27 | Node* newNode = new Node(curr1->val); 28 | prev->next = newNode; 29 | prev = prev->next; 30 | mp[curr1] = newNode; 31 | curr1 = curr1->next; 32 | } 33 | Node* curr = head; 34 | Node* Head = newHead->next; 35 | while(curr != NULL) 36 | { 37 | Head->random = mp[curr->random]; 38 | curr = curr->next; 39 | Head = Head->next; 40 | } 41 | return newHead->next; 42 | } 43 | }; 44 | -------------------------------------------------------------------------------- /LinkedList/intersectionOf2LL.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct ListNode { 5 | int val; 6 | ListNode *next; 7 | ListNode() : val(0), next(nullptr) {} 8 | ListNode(int x) : val(x), next(nullptr) {} 9 | ListNode(int x, ListNode *next) : val(x), next(next) {} 10 | }; 11 | class Solution { 12 | public: 13 | int Length(ListNode* head) 14 | { 15 | int count = 1; 16 | while(head != NULL) 17 | { 18 | count++; 19 | head = head->next; 20 | } 21 | return count; 22 | } 23 | ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) { 24 | int len1 = Length(headA); 25 | int len2 = Length(headB); 26 | 27 | while(len1>len2) 28 | { 29 | len1--; 30 | headA=headA->next; 31 | } 32 | while(len2>len1) 33 | { 34 | len2--; 35 | headB=headB->next; 36 | } 37 | 38 | while(headA != headB) 39 | { 40 | headA=headA->next; 41 | headB=headB->next; 42 | } 43 | return headA; 44 | } 45 | }; -------------------------------------------------------------------------------- /0718-maximum-length-of-repeated-subarray/README.md: -------------------------------------------------------------------------------- 1 |

718. Maximum Length of Repeated Subarray

Medium


Given two integer arrays nums1 and nums2, return the maximum length of a subarray that appears in both arrays.

2 | 3 |

 

4 |

Example 1:

5 | 6 |
 7 | Input: nums1 = [1,2,3,2,1], nums2 = [3,2,1,4,7]
 8 | Output: 3
 9 | Explanation: The repeated subarray with maximum length is [3,2,1].
10 | 
11 | 12 |

Example 2:

13 | 14 |
15 | Input: nums1 = [0,0,0,0,0], nums2 = [0,0,0,0,0]
16 | Output: 5
17 | Explanation: The repeated subarray with maximum length is [0,0,0,0,0].
18 | 
19 | 20 |

 

21 |

Constraints:

22 | 23 |
    24 |
  • 1 <= nums1.length, nums2.length <= 1000
  • 25 |
  • 0 <= nums1[i], nums2[i] <= 100
  • 26 |
27 | -------------------------------------------------------------------------------- /3501-delete-nodes-from-linked-list-present-in-array/3501-delete-nodes-from-linked-list-present-in-array.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode() : val(0), next(nullptr) {} 7 | * ListNode(int x) : val(x), next(nullptr) {} 8 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 9 | * }; 10 | */ 11 | class Solution { 12 | public: 13 | ListNode* modifiedList(vector& nums, ListNode* head) { 14 | unordered_set numSet(nums.begin(), nums.end()); 15 | ListNode* curr = head; 16 | ListNode* prev = new ListNode(-1); 17 | ListNode* newHead = prev; 18 | prev->next = curr; 19 | while(curr) 20 | { 21 | int value = prev->next->val; 22 | if(numSet.find(value) != numSet.end()) 23 | { 24 | prev->next = curr ->next; 25 | curr = curr->next; 26 | }else{ 27 | prev = prev->next; 28 | curr = curr ->next; 29 | } 30 | } 31 | return newHead->next; 32 | } 33 | }; 34 | -------------------------------------------------------------------------------- /Searching/BooksAllocation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | int studentsAllocated(int maxPages, int arr[], int n) 7 | { 8 | int students=1; 9 | int pagesAllocated = 0; 10 | for(int i=0;in) return -1; 22 | int lowest = 0; 23 | int highest = 0; 24 | for(int i=0;i m) lowest = mid+1; 34 | else highest = mid-1; 35 | } 36 | return lowest; 37 | } 38 | }; 39 | -------------------------------------------------------------------------------- /0082-remove-duplicates-from-sorted-list-ii/0082-remove-duplicates-from-sorted-list-ii.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode() : val(0), next(nullptr) {} 7 | * ListNode(int x) : val(x), next(nullptr) {} 8 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 9 | * }; 10 | */ 11 | class Solution { 12 | public: 13 | ListNode* deleteDuplicates(ListNode* head) { 14 | ListNode* curr = head; 15 | ListNode* prev = new ListNode(-102); 16 | ListNode* newHead = prev; 17 | prev->next = curr; 18 | while(curr && curr->next) 19 | { 20 | if(curr->next->val != curr->val) 21 | { 22 | prev = curr; 23 | curr = curr->next; 24 | }else{ 25 | while(curr->next && curr->next->val == curr->val) 26 | { 27 | curr = curr->next; 28 | } 29 | curr = curr->next; 30 | prev->next = curr; 31 | } 32 | } 33 | return newHead->next; 34 | } 35 | }; -------------------------------------------------------------------------------- /0410-split-array-largest-sum/0410-split-array-largest-sum.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool check(vector& nums, int k, int mid) 4 | { 5 | int arrCount = 1; 6 | int sum = nums[0]; 7 | for(int i=1;i mid) 10 | { 11 | sum = nums[i]; 12 | arrCount++; 13 | if(arrCount > k) return false; 14 | }else{ 15 | sum += nums[i]; 16 | } 17 | 18 | } 19 | return true; 20 | } 21 | int splitArray(vector& nums, int k) { 22 | int n = nums.size(); 23 | int low = *max_element(nums.begin(),nums.end()); 24 | int high = accumulate(nums.begin(),nums.end(),0); 25 | int ans = high; 26 | while(low<=high) 27 | { 28 | int mid = low+(high-low)/2; 29 | if(check(nums,k,mid)) 30 | { 31 | ans = mid; 32 | high = mid-1; 33 | }else{ 34 | low = mid+1; 35 | } 36 | } 37 | return ans; 38 | } 39 | }; -------------------------------------------------------------------------------- /1762-furthest-building-you-can-reach/1762-furthest-building-you-can-reach.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int furthestBuilding(vector& heights, int bricks, int ladders) { 4 | int n = heights.size(); 5 | int count = 0; 6 | priority_queue pq; 7 | int i; 8 | for (i = 0; i < n - 1; i++) { 9 | int diff = heights[i + 1] - heights[i]; 10 | 11 | if(heights[i+1] <= heights[i]) continue; 12 | if(diff<=bricks) 13 | { 14 | bricks-= diff; 15 | pq.push(diff); 16 | }else if(ladders >0) 17 | { 18 | if(!pq.empty()) 19 | { 20 | int maxPast = pq.top(); 21 | if(maxPast>diff) 22 | { 23 | bricks += maxPast; 24 | pq.pop(); 25 | bricks -= diff; 26 | pq.push(diff); 27 | } 28 | } 29 | ladders--; 30 | }else break; 31 | } 32 | 33 | return i; 34 | } 35 | }; -------------------------------------------------------------------------------- /2067-maximum-number-of-points-with-cost/2067-maximum-number-of-points-with-cost.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | long long maxPoints(vector>& points) { 4 | int m = points.size(); 5 | int n = points[0].size(); 6 | vector prev(n); 7 | for(int col=0;col left(n,0); 14 | vector right(n,0); 15 | left[0] = prev[0]; 16 | for(int j=1;j=0;j--) 22 | { 23 | right[j] = max(prev[j],right[j+1]-1); 24 | } 25 | vector curr(n); 26 | for(int j=0;j698. Partition to K Equal Sum Subsets

Medium


Given an integer array nums and an integer k, return true if it is possible to divide this array into k non-empty subsets whose sums are all equal.

2 | 3 |

 

4 |

Example 1:

5 | 6 |
 7 | Input: nums = [4,3,2,3,5,2,1], k = 4
 8 | Output: true
 9 | Explanation: It is possible to divide it into 4 subsets (5), (1, 4), (2,3), (2,3) with equal sums.
10 | 
11 | 12 |

Example 2:

13 | 14 |
15 | Input: nums = [1,2,3,4], k = 3
16 | Output: false
17 | 
18 | 19 |

 

20 |

Constraints:

21 | 22 |
    23 |
  • 1 <= k <= nums.length <= 16
  • 24 |
  • 1 <= nums[i] <= 104
  • 25 |
  • The frequency of each element is in the range [1, 4].
  • 26 |
27 | -------------------------------------------------------------------------------- /LinkedList/addNumbers.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | /** 5 | * Definition for singly-linked list. 6 | * struct ListNode { 7 | * int val; 8 | * ListNode *next; 9 | * ListNode() : val(0), next(nullptr) {} 10 | * ListNode(int x) : val(x), next(nullptr) {} 11 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 12 | * }; 13 | */ 14 | class Solution { 15 | public: 16 | ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { 17 | ListNode* dummy = new ListNode(0); 18 | ListNode* curr = dummy; 19 | int carry = 0; 20 | 21 | while (l1 != NULL || l2 != NULL || carry != 0) { 22 | int sum = carry; 23 | if (l1 != NULL) { 24 | sum += l1->val; 25 | l1 = l1->next; 26 | } 27 | if (l2 != NULL) { 28 | sum += l2->val; 29 | l2 = l2->next; 30 | } 31 | carry = sum / 10; 32 | sum = sum % 10; 33 | 34 | curr->next = new ListNode(sum); 35 | curr = curr->next; 36 | } 37 | return dummy->next; 38 | } 39 | }; -------------------------------------------------------------------------------- /0943-sum-of-subarray-minimums/README.md: -------------------------------------------------------------------------------- 1 |

943. Sum of Subarray Minimums

Medium


Given an array of integers arr, find the sum of min(b), where b ranges over every (contiguous) subarray of arr. Since the answer may be large, return the answer modulo 109 + 7.

2 | 3 |

 

4 |

Example 1:

5 | 6 |
 7 | Input: arr = [3,1,2,4]
 8 | Output: 17
 9 | Explanation: 
10 | Subarrays are [3], [1], [2], [4], [3,1], [1,2], [2,4], [3,1,2], [1,2,4], [3,1,2,4]. 
11 | Minimums are 3, 1, 2, 4, 1, 1, 2, 1, 1, 1.
12 | Sum is 17.
13 | 
14 | 15 |

Example 2:

16 | 17 |
18 | Input: arr = [11,81,94,43,3]
19 | Output: 444
20 | 
21 | 22 |

 

23 |

Constraints:

24 | 25 |
    26 |
  • 1 <= arr.length <= 3 * 104
  • 27 |
  • 1 <= arr[i] <= 3 * 104
  • 28 |
29 | -------------------------------------------------------------------------------- /Arrays/FindDuplicate.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // https://leetcode.com/problems/find-the-duplicate-number/ 5 | 6 | // using sort method 7 | // O(n) 8 | class Solution { 9 | public: 10 | int findDuplicate(vector& nums) { 11 | sort(nums.begin(),nums.end()); 12 | int n = nums.size(); 13 | for(int i=0;i there is a cycle 23 | class Solution { 24 | public: 25 | int findDuplicate(vector& nums) { 26 | int n = nums.size(); 27 | int slow = nums[0]; 28 | int fast = nums[0]; 29 | slow = nums[slow]; 30 | fast = nums[nums[fast]]; 31 | while(slow != fast) 32 | { 33 | slow = nums[slow]; 34 | fast = nums[nums[fast]]; 35 | } 36 | slow=nums[0]; 37 | while(slow != fast) 38 | { 39 | slow = nums[slow]; 40 | fast = nums[fast]; 41 | } 42 | return slow; 43 | } 44 | }; -------------------------------------------------------------------------------- /0033-search-in-rotated-sorted-array/0033-search-in-rotated-sorted-array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findPivot(vector& nums) { 4 | int n = nums.size(); 5 | int s = 0, e=n-1; 6 | while(s nums[e]) 10 | { 11 | s = m+1; 12 | }else e = m; 13 | } 14 | return e; 15 | } 16 | int BS(vector& nums, int target, int s, int e) 17 | { 18 | while(s<=e) 19 | { 20 | int m = s+(e-s)/2; 21 | if(nums[m] == target) return m; 22 | else if(nums[m] < target) 23 | { 24 | s = m+1; 25 | }else{ 26 | e = m-1; 27 | } 28 | } 29 | return -1; 30 | } 31 | int search(vector& nums, int target) { 32 | int n = nums.size(); 33 | int s =0, e = n-1; 34 | int pivot = findPivot(nums); 35 | int left = BS(nums,target,0,pivot-1); 36 | int right = BS(nums,target,pivot,n-1); 37 | 38 | if(left == -1) return right; 39 | return left; 40 | } 41 | }; -------------------------------------------------------------------------------- /0731-my-calendar-ii/0731-my-calendar-ii.cpp: -------------------------------------------------------------------------------- 1 | class MyCalendarTwo { 2 | public: 3 | vector> doubleOverlappedRegion; 4 | vector> overallBookings; 5 | 6 | bool checkOverlap(int start1, int end1, int start2, int end2) { 7 | return max(start1, start2) < min(end1, end2); 8 | } 9 | 10 | pair findOverlappedRegion(int start1, int end1, int start2, int end2) { 11 | return {max(start1, start2), min(end1, end2)}; 12 | } 13 | 14 | MyCalendarTwo() { 15 | 16 | } 17 | 18 | bool book(int start, int end) { 19 | for(pair region : doubleOverlappedRegion) { 20 | if(checkOverlap(region.first, region.second, start, end)) { 21 | return false; 22 | } 23 | } 24 | 25 | for(pair booking : overallBookings) { 26 | if(checkOverlap(booking.first, booking.second, start, end)) { 27 | doubleOverlappedRegion.push_back(findOverlappedRegion(booking.first, booking.second, start, end)); 28 | } 29 | } 30 | 31 | overallBookings.push_back({start, end}); 32 | return true; 33 | } 34 | }; -------------------------------------------------------------------------------- /0076-minimum-window-substring/0076-minimum-window-substring.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string minWindow(string s, string t) { 4 | int n = s.size(); 5 | 6 | if(t.size()>n) return ""; 7 | 8 | unordered_map mp; 9 | for(auto ch : t) 10 | { 11 | mp[ch]++; 12 | } 13 | int requiredCount = t.length(); 14 | int i=0,j=0; 15 | int minWinSize = INT_MAX; 16 | int start=0; 17 | while(j 0) requiredCount--; 21 | 22 | mp[ch]--; 23 | while(requiredCount==0) 24 | { 25 | int currWindSize = j-i+1; 26 | if(minWinSize > currWindSize){ 27 | minWinSize = currWindSize; 28 | start = i; 29 | } 30 | mp[s[i]]++; 31 | if(mp[s[i]] > 0){ 32 | requiredCount++; 33 | } 34 | i++; 35 | } 36 | j++; 37 | } 38 | 39 | return minWinSize==INT_MAX ? "" : s.substr(start,minWinSize); 40 | } 41 | }; -------------------------------------------------------------------------------- /0077-combinations/README.md: -------------------------------------------------------------------------------- 1 |

77. Combinations

Medium


Given two integers n and k, return all possible combinations of k numbers chosen from the range [1, n].

2 | 3 |

You may return the answer in any order.

4 | 5 |

 

6 |

Example 1:

7 | 8 |
 9 | Input: n = 4, k = 2
10 | Output: [[1,2],[1,3],[1,4],[2,3],[2,4],[3,4]]
11 | Explanation: There are 4 choose 2 = 6 total combinations.
12 | Note that combinations are unordered, i.e., [1,2] and [2,1] are considered to be the same combination.
13 | 
14 | 15 |

Example 2:

16 | 17 |
18 | Input: n = 1, k = 1
19 | Output: [[1]]
20 | Explanation: There is 1 choose 1 = 1 total combination.
21 | 
22 | 23 |

 

24 |

Constraints:

25 | 26 |
    27 |
  • 1 <= n <= 20
  • 28 |
  • 1 <= k <= n
  • 29 |
30 | -------------------------------------------------------------------------------- /0101-symmetric-tree/README.md: -------------------------------------------------------------------------------- 1 |

101. Symmetric Tree

Easy


Given the root of a binary tree, check whether it is a mirror of itself (i.e., symmetric around its center).

2 | 3 |

 

4 |

Example 1:

5 | 6 |
 7 | Input: root = [1,2,2,3,4,4,3]
 8 | Output: true
 9 | 
10 | 11 |

Example 2:

12 | 13 |
14 | Input: root = [1,2,2,null,3,null,3]
15 | Output: false
16 | 
17 | 18 |

 

19 |

Constraints:

20 | 21 |
    22 |
  • The number of nodes in the tree is in the range [1, 1000].
  • 23 |
  • -100 <= Node.val <= 100
  • 24 |
25 | 26 |

 

27 | Follow up: Could you solve it both recursively and iteratively? -------------------------------------------------------------------------------- /0541-reverse-string-ii/README.md: -------------------------------------------------------------------------------- 1 |

541. Reverse String II

Easy


Given a string s and an integer k, reverse the first k characters for every 2k characters counting from the start of the string.

2 | 3 |

If there are fewer than k characters left, reverse all of them. If there are less than 2k but greater than or equal to k characters, then reverse the first k characters and leave the other as original.

4 | 5 |

 

6 |

Example 1:

7 |
Input: s = "abcdefg", k = 2
 8 | Output: "bacdfeg"
 9 | 

Example 2:

10 |
Input: s = "abcd", k = 2
11 | Output: "bacd"
12 | 
13 |

 

14 |

Constraints:

15 | 16 |
    17 |
  • 1 <= s.length <= 104
  • 18 |
  • s consists of only lowercase English letters.
  • 19 |
  • 1 <= k <= 104
  • 20 |
21 | -------------------------------------------------------------------------------- /0404-sum-of-left-leaves/README.md: -------------------------------------------------------------------------------- 1 |

404. Sum of Left Leaves

Easy


Given the root of a binary tree, return the sum of all left leaves.

2 | 3 |

A leaf is a node with no children. A left leaf is a leaf that is the left child of another node.

4 | 5 |

 

6 |

Example 1:

7 | 8 |
 9 | Input: root = [3,9,20,null,null,15,7]
10 | Output: 24
11 | Explanation: There are two left leaves in the binary tree, with values 9 and 15 respectively.
12 | 
13 | 14 |

Example 2:

15 | 16 |
17 | Input: root = [1]
18 | Output: 0
19 | 
20 | 21 |

 

22 |

Constraints:

23 | 24 |
    25 |
  • The number of nodes in the tree is in the range [1, 1000].
  • 26 |
  • -1000 <= Node.val <= 1000
  • 27 |
28 | --------------------------------------------------------------------------------