├── 1683-invalid-tweets └── 1683-invalid-tweets.sql ├── 0231-power-of-two ├── 0231-power-of-two.cpp └── README.md ├── 0584-find-customer-referee └── 0584-find-customer-referee.sql ├── 0595-big-countries └── 0595-big-countries.sql ├── 1148-article-views-i └── 1148-article-views-i.sql ├── 3676-smallest-number-with-all-set-bits └── 3676-smallest-number-with-all-set-bits.go ├── 0620-not-boring-movies └── 0620-not-boring-movies.sql ├── 1757-recyclable-and-low-fat-products └── 1757-recyclable-and-low-fat-products.sql ├── 0169-majority-element ├── 0169-majority-element.java ├── 0169-majority-element.cpp └── README.md ├── 1068-product-sales-analysis-i └── 1068-product-sales-analysis-i.sql ├── 0175-combine-two-tables └── 0175-combine-two-tables.sql ├── 1667-fix-names-in-a-table └── 1667-fix-names-in-a-table.sql ├── 0176-second-highest-salary └── 0176-second-highest-salary.sql ├── 0197-rising-temperature └── 0197-rising-temperature.sql ├── 1378-replace-employee-id-with-the-unique-identifier └── 1378-replace-employee-id-with-the-unique-identifier.sql ├── 2356-number-of-unique-subjects-taught-by-each-teacher └── 2356-number-of-unique-subjects-taught-by-each-teacher.sql ├── 0796-rotate-string ├── 0796-rotate-string.java ├── 0796-rotate-string.cpp └── README.md ├── 0434-number-of-segments-in-a-string └── 0434-number-of-segments-in-a-string.java ├── 0088-merge-sorted-array └── 0088-merge-sorted-array.cpp ├── 0136-single-number ├── 0136-single-number.cpp └── README.md ├── 0540-single-element-in-a-sorted-array ├── 0540-single-element-in-a-sorted-array.java ├── 0540-single-element-in-a-sorted-array.cpp └── README.md ├── 1025-divisor-game └── 1025-divisor-game.cpp ├── 1978-employees-whose-manager-left-the-company └── 1978-employees-whose-manager-left-the-company.sql ├── 1581-customer-who-visited-but-did-not-make-any-transactions └── 1581-customer-who-visited-but-did-not-make-any-transactions.sql ├── 0217-contains-duplicate └── 0217-contains-duplicate.cpp ├── 0069-sqrtx ├── 0069-sqrtx.cpp └── README.md ├── 0070-climbing-stairs ├── 0070-climbing-stairs.cpp └── README.md ├── 2974-minimum-number-game └── 2974-minimum-number-game.py ├── 0201-bitwise-and-of-numbers-range ├── 0201-bitwise-and-of-numbers-range.cpp └── README.md ├── 0344-reverse-string ├── 0344-reverse-string.java ├── 0344-reverse-string.cpp └── README.md ├── 0009-palindrome-number └── 0009-palindrome-number.cpp ├── 0338-counting-bits └── 0338-counting-bits.cpp ├── 0392-is-subsequence └── 0392-is-subsequence.cpp ├── 0977-squares-of-a-sorted-array └── 0977-squares-of-a-sorted-array.cpp ├── 1903-largest-odd-number-in-string └── 1903-largest-odd-number-in-string.cpp ├── 0326-power-of-three └── 0326-power-of-three.cpp ├── 0485-max-consecutive-ones ├── 0485-max-consecutive-ones.cpp └── README.md ├── 0125-valid-palindrome ├── 0125-valid-palindrome.java └── 0125-valid-palindrome.cpp ├── 0027-remove-element └── 0027-remove-element.java ├── 0215-kth-largest-element-in-an-array ├── 0215-kth-largest-element-in-an-array.cpp └── README.md ├── 0268-missing-number └── 0268-missing-number.cpp ├── 0041-first-missing-positive └── 0041-first-missing-positive.cpp ├── 0053-maximum-subarray └── 0053-maximum-subarray.cpp ├── 0237-delete-node-in-a-linked-list └── 0237-delete-node-in-a-linked-list.java ├── 0442-find-all-duplicates-in-an-array └── 0442-find-all-duplicates-in-an-array.cpp ├── 1217-minimum-cost-to-move-chips-to-the-same-position └── 1217-minimum-cost-to-move-chips-to-the-same-position.cpp ├── 1752-check-if-array-is-sorted-and-rotated └── 1752-check-if-array-is-sorted-and-rotated.java ├── 0258-add-digits ├── 0258-add-digits.cpp └── README.md ├── 0026-remove-duplicates-from-sorted-array └── 0026-remove-duplicates-from-sorted-array.cpp ├── 0121-best-time-to-buy-and-sell-stock ├── 0121-best-time-to-buy-and-sell-stock.cpp └── 0121-best-time-to-buy-and-sell-stock.java ├── 0198-house-robber └── 0198-house-robber.cpp ├── 0387-first-unique-character-in-a-string └── 0387-first-unique-character-in-a-string.cpp ├── 0509-fibonacci-number └── 0509-fibonacci-number.cpp ├── 0746-min-cost-climbing-stairs └── 0746-min-cost-climbing-stairs.cpp ├── 0151-reverse-words-in-a-string └── 0151-reverse-words-in-a-string.java ├── 0287-find-the-duplicate-number └── 0287-find-the-duplicate-number.cpp ├── 0560-subarray-sum-equals-k ├── 0560-subarray-sum-equals-k.cpp └── README.md ├── 2914-minimum-number-of-changes-to-make-binary-string-beautiful └── 2914-minimum-number-of-changes-to-make-binary-string-beautiful.cpp ├── 0122-best-time-to-buy-and-sell-stock-ii └── 0122-best-time-to-buy-and-sell-stock-ii.cpp ├── 0202-happy-number └── 0202-happy-number.cpp ├── 0575-distribute-candies └── 0575-distribute-candies.cpp ├── 0704-binary-search └── 0704-binary-search.cpp ├── 0048-rotate-image └── 0048-rotate-image.cpp ├── 0645-set-mismatch ├── 0645-set-mismatch.cpp └── README.md ├── 3232-find-if-digit-game-can-be-won └── 3232-find-if-digit-game-can-be-won.py ├── 0074-search-a-2d-matrix ├── 0074-search-a-2d-matrix.cpp └── 0074-search-a-2d-matrix.java ├── 0001-two-sum ├── 0001-two-sum.cpp └── 0001-two-sum.java ├── 0229-majority-element-ii ├── 0229-majority-element-ii.cpp └── README.md ├── 0242-valid-anagram └── 0242-valid-anagram.cpp ├── 0283-move-zeroes ├── 0283-move-zeroes.cpp └── README.md ├── 2520-count-the-digits-that-divide-a-number └── 2520-count-the-digits-that-divide-a-number.py ├── 2022-convert-1d-array-into-2d-array └── 2022-convert-1d-array-into-2d-array.cpp ├── 3304-find-the-k-th-character-in-string-game-i └── 3304-find-the-k-th-character-in-string-game-i.java ├── 0058-length-of-last-word └── 0058-length-of-last-word.java ├── 0004-median-of-two-sorted-arrays └── 0004-median-of-two-sorted-arrays.cpp ├── 1539-kth-missing-positive-number └── 1539-kth-missing-positive-number.cpp ├── 3581-the-two-sneaky-numbers-of-digitville └── 3581-the-two-sneaky-numbers-of-digitville.cpp ├── 0011-container-with-most-water └── 0011-container-with-most-water.cpp ├── 0020-valid-parentheses ├── 0020-valid-parentheses.java └── 0020-valid-parentheses.cpp ├── 0349-intersection-of-two-arrays ├── 0349-intersection-of-two-arrays.cpp └── README.md ├── 1021-remove-outermost-parentheses └── 1021-remove-outermost-parentheses.cpp ├── 1614-maximum-nesting-depth-of-the-parentheses └── 1614-maximum-nesting-depth-of-the-parentheses.cpp ├── 1731-the-number-of-employees-which-report-to-each-employee └── 1731-the-number-of-employees-which-report-to-each-employee.sql ├── 0189-rotate-array └── 0189-rotate-array.cpp ├── 0448-find-all-numbers-disappeared-in-an-array ├── 0448-find-all-numbers-disappeared-in-an-array.cpp ├── 0448-find-all-numbers-disappeared-in-an-array.cs └── README.md ├── 0035-search-insert-position └── 0035-search-insert-position.cpp ├── 0205-isomorphic-strings └── 0205-isomorphic-strings.cpp ├── 3477-fruits-into-baskets-ii └── 3477-fruits-into-baskets-ii.cpp ├── 0744-find-smallest-letter-greater-than-target └── 0744-find-smallest-letter-greater-than-target.cpp ├── 0007-reverse-integer ├── 0007-reverse-integer.java └── README.md ├── 0476-number-complement └── 0476-number-complement.java ├── 0062-unique-paths └── 0062-unique-paths.cpp ├── 0594-longest-harmonious-subsequence └── 0594-longest-harmonious-subsequence.java ├── 0141-linked-list-cycle ├── 0141-linked-list-cycle.cpp └── 0141-linked-list-cycle.java ├── 2149-rearrange-array-elements-by-sign └── 2149-rearrange-array-elements-by-sign.cpp ├── 0153-find-minimum-in-rotated-sorted-array └── 0153-find-minimum-in-rotated-sorted-array.cpp ├── 0451-sort-characters-by-frequency └── 0451-sort-characters-by-frequency.cpp ├── 0056-merge-intervals ├── 0056-merge-intervals.cpp └── README.md ├── 0135-candy └── 0135-candy.cpp ├── 0014-longest-common-prefix ├── 0014-longest-common-prefix.java └── README.md ├── 0046-permutations ├── 0046-permutations.cpp └── README.md ├── 0049-group-anagrams └── 0049-group-anagrams.cpp ├── 0228-summary-ranges └── 0228-summary-ranges.java ├── 0238-product-of-array-except-self └── 0238-product-of-array-except-self.cpp ├── 0096-unique-binary-search-trees ├── 0096-unique-binary-search-trees.cpp └── README.md ├── 0028-find-the-index-of-the-first-occurrence-in-a-string └── 0028-find-the-index-of-the-first-occurrence-in-a-string.java ├── 1684-count-the-number-of-consistent-strings └── 1684-count-the-number-of-consistent-strings.cpp ├── 0067-add-binary ├── 0067-add-binary.cpp └── README.md ├── 0075-sort-colors └── 0075-sort-colors.cpp ├── 0303-range-sum-query-immutable └── 0303-range-sum-query-immutable.cpp ├── 0042-trapping-rain-water ├── 0042-trapping-rain-water.cpp └── README.md ├── 0118-pascals-triangle ├── 0118-pascals-triangle.cpp └── README.md ├── 0050-powx-n ├── 0050-powx-n.cpp └── 0050-powx-n.java ├── 1143-longest-common-subsequence └── 1143-longest-common-subsequence.cpp ├── 0128-longest-consecutive-sequence ├── 0128-longest-consecutive-sequence.cpp ├── 0128-longest-consecutive-sequence.java └── README.md ├── 0160-intersection-of-two-linked-lists ├── 0160-intersection-of-two-linked-lists.java └── 0160-intersection-of-two-linked-lists.cpp ├── 0222-count-complete-tree-nodes └── 0222-count-complete-tree-nodes.cpp ├── 0876-middle-of-the-linked-list └── 0876-middle-of-the-linked-list.cpp ├── 0583-delete-operation-for-two-strings └── 0583-delete-operation-for-two-strings.cpp ├── 0100-same-tree └── 0100-same-tree.cpp ├── 0234-palindrome-linked-list └── 0234-palindrome-linked-list.cpp ├── 0236-lowest-common-ancestor-of-a-binary-tree └── 0236-lowest-common-ancestor-of-a-binary-tree.cpp ├── 0073-set-matrix-zeroes └── 0073-set-matrix-zeroes.cpp ├── 0090-subsets-ii ├── 0090-subsets-ii.cpp └── README.md ├── 0036-valid-sudoku └── 0036-valid-sudoku.cpp ├── 0104-maximum-depth-of-binary-tree ├── 0104-maximum-depth-of-binary-tree.java ├── 0104-maximum-depth-of-binary-tree.cpp └── README.md ├── 0235-lowest-common-ancestor-of-a-binary-search-tree └── 0235-lowest-common-ancestor-of-a-binary-search-tree.cpp ├── 3201-find-the-maximum-length-of-valid-subsequence-i └── 3201-find-the-maximum-length-of-valid-subsequence-i.cpp ├── 0225-implement-stack-using-queues └── 0225-implement-stack-using-queues.cpp ├── 0700-search-in-a-binary-search-tree └── 0700-search-in-a-binary-search-tree.cpp ├── 0783-search-in-a-binary-search-tree └── 0783-search-in-a-binary-search-tree.cpp ├── 0226-invert-binary-tree ├── 0226-invert-binary-tree.cpp ├── 0226-invert-binary-tree.java └── README.md ├── 0543-diameter-of-binary-tree └── 0543-diameter-of-binary-tree.cpp ├── 1498-number-of-subsequences-that-satisfy-the-given-sum-condition └── 1498-number-of-subsequences-that-satisfy-the-given-sum-condition.java ├── 0033-search-in-rotated-sorted-array ├── 0033-search-in-rotated-sorted-array.cpp └── 0033-search-in-rotated-sorted-array.java ├── 0015-3sum └── 0015-3sum.java ├── 0206-reverse-linked-list └── 0206-reverse-linked-list.cpp ├── 0111-minimum-depth-of-binary-tree ├── 0111-minimum-depth-of-binary-tree.java └── README.md ├── 0083-remove-duplicates-from-sorted-list ├── 0083-remove-duplicates-from-sorted-list.cpp └── README.md ├── 0144-binary-tree-preorder-traversal └── 0144-binary-tree-preorder-traversal.cpp ├── 0148-sort-list └── 0148-sort-list.cpp ├── 0094-binary-tree-inorder-traversal └── 0094-binary-tree-inorder-traversal.cpp ├── 0145-binary-tree-postorder-traversal ├── 0145-binary-tree-postorder-traversal.cpp └── 0145-binary-tree-postorder-traversal.java ├── 1710-maximum-units-on-a-truck └── 1710-maximum-units-on-a-truck.cpp ├── 0101-symmetric-tree ├── 0101-symmetric-tree.cpp └── README.md ├── 2305-fair-distribution-of-cookies └── 2305-fair-distribution-of-cookies.cpp ├── 0061-rotate-list ├── 0061-rotate-list.cpp └── README.md ├── 0784-insert-into-a-binary-search-tree └── 0784-insert-into-a-binary-search-tree.cpp ├── 0003-longest-substring-without-repeating-characters └── 0003-longest-substring-without-repeating-characters.java ├── 1283-find-the-smallest-divisor-given-a-threshold └── 1283-find-the-smallest-divisor-given-a-threshold.cpp ├── 0142-linked-list-cycle-ii ├── 0142-linked-list-cycle-ii.java └── 0142-linked-list-cycle-ii.cpp ├── 0213-house-robber-ii └── 0213-house-robber-ii.cpp ├── 0131-palindrome-partitioning ├── 0131-palindrome-partitioning.cpp └── README.md ├── 2216-delete-the-middle-node-of-a-linked-list └── 2216-delete-the-middle-node-of-a-linked-list.cpp ├── 0110-balanced-binary-tree ├── 0110-balanced-binary-tree.cpp └── README.md ├── 0230-kth-smallest-element-in-a-bst └── 0230-kth-smallest-element-in-a-bst.cpp ├── 2461-maximum-sum-of-distinct-subarrays-with-length-k └── 2461-maximum-sum-of-distinct-subarrays-with-length-k.cpp ├── 0124-binary-tree-maximum-path-sum └── 0124-binary-tree-maximum-path-sum.cpp ├── 2095-delete-the-middle-node-of-a-linked-list └── 2095-delete-the-middle-node-of-a-linked-list.cpp ├── 0025-reverse-nodes-in-k-group └── 0025-reverse-nodes-in-k-group.cpp ├── 0063-unique-paths-ii └── 0063-unique-paths-ii.cpp ├── 0491-non-decreasing-subsequences ├── 0491-non-decreasing-subsequences.java └── README.md ├── 0017-letter-combinations-of-a-phone-number └── 0017-letter-combinations-of-a-phone-number.cpp ├── 0098-validate-binary-search-tree └── 0098-validate-binary-search-tree.cpp ├── 0024-swap-nodes-in-pairs └── 0024-swap-nodes-in-pairs.cpp ├── 0108-convert-sorted-array-to-binary-search-tree └── 0108-convert-sorted-array-to-binary-search-tree.cpp ├── 0019-remove-nth-node-from-end-of-list └── 0019-remove-nth-node-from-end-of-list.cpp ├── 1721-swapping-nodes-in-a-linked-list └── 1721-swapping-nodes-in-a-linked-list.cpp ├── 0532-k-diff-pairs-in-an-array └── 0532-k-diff-pairs-in-an-array.cpp ├── 0203-remove-linked-list-elements └── 0203-remove-linked-list-elements.cpp ├── 0572-subtree-of-another-tree └── 0572-subtree-of-another-tree.cpp ├── 0661-image-smoother └── 0661-image-smoother.java ├── 0232-implement-queue-using-stacks └── 0232-implement-queue-using-stacks.cpp ├── 0031-next-permutation └── 0031-next-permutation.cpp ├── 0617-merge-two-binary-trees └── 0617-merge-two-binary-trees.cpp ├── 0112-path-sum └── 0112-path-sum.cpp ├── 0081-search-in-rotated-sorted-array-ii └── 0081-search-in-rotated-sorted-array-ii.cpp ├── 0980-unique-paths-iii └── 0980-unique-paths-iii.java ├── 0199-binary-tree-right-side-view └── 0199-binary-tree-right-side-view.cpp ├── 0257-binary-tree-paths ├── README.md └── 0257-binary-tree-paths.cpp ├── 0875-koko-eating-bananas └── 0875-koko-eating-bananas.cpp ├── 0034-find-first-and-last-position-of-element-in-sorted-array └── 0034-find-first-and-last-position-of-element-in-sorted-array.cpp ├── 1008-construct-binary-search-tree-from-preorder-traversal └── 1008-construct-binary-search-tree-from-preorder-traversal.cpp ├── 0079-word-search ├── 0079-word-search.cpp └── 0079-word-search.java ├── 0102-binary-tree-level-order-traversal ├── 0102-binary-tree-level-order-traversal.cpp └── README.md ├── 0328-odd-even-linked-list └── 0328-odd-even-linked-list.cpp ├── 0637-average-of-levels-in-binary-tree └── 0637-average-of-levels-in-binary-tree.cpp ├── 0200-number-of-islands └── 0200-number-of-islands.cpp ├── 0450-delete-node-in-a-bst └── 0450-delete-node-in-a-bst.cpp ├── LICENSE ├── 0116-populating-next-right-pointers-in-each-node └── 0116-populating-next-right-pointers-in-each-node.cpp ├── 0002-add-two-numbers └── 0002-add-two-numbers.cpp ├── 0021-merge-two-sorted-lists └── 0021-merge-two-sorted-lists.cpp ├── 1011-capacity-to-ship-packages-within-d-days └── 1011-capacity-to-ship-packages-within-d-days.cpp ├── 0013-roman-to-integer └── 0013-roman-to-integer.cpp ├── 0054-spiral-matrix ├── 0054-spiral-matrix.cpp └── README.md ├── 0107-binary-tree-level-order-traversal-ii ├── 0107-binary-tree-level-order-traversal-ii.cpp └── README.md ├── 0662-maximum-width-of-binary-tree └── 0662-maximum-width-of-binary-tree.cpp ├── 0347-top-k-frequent-elements ├── README.md └── 0347-top-k-frequent-elements.cpp ├── 0103-binary-tree-zigzag-level-order-traversal └── README.md └── 0138-copy-list-with-random-pointer └── 0138-copy-list-with-random-pointer.java /1683-invalid-tweets/1683-invalid-tweets.sql: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | SELECT tweet_id FROM Tweets 3 | WHERE LENGTH(content) > 15; -------------------------------------------------------------------------------- /0231-power-of-two/0231-power-of-two.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isPowerOfTwo(int n) { 4 | return n > 0 && (n & (n - 1)) == 0; 5 | } 6 | }; -------------------------------------------------------------------------------- /0584-find-customer-referee/0584-find-customer-referee.sql: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | SELECT name FROM Customer 3 | WHERE referee_id!=2 OR referee_id IS NULL; -------------------------------------------------------------------------------- /0595-big-countries/0595-big-countries.sql: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | SELECT name,population,area FROM World 3 | WHERE area >= 3000000 OR population >= 25000000; -------------------------------------------------------------------------------- /1148-article-views-i/1148-article-views-i.sql: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | SELECT DISTINCT author_id AS id FROM Views 3 | WHERE author_id = viewer_id ORDER BY id; -------------------------------------------------------------------------------- /3676-smallest-number-with-all-set-bits/3676-smallest-number-with-all-set-bits.go: -------------------------------------------------------------------------------- 1 | func smallestNumber(n int) int { 2 | for n & (n + 1) != 0 { 3 | n |= n + 1 4 | } 5 | return n 6 | } -------------------------------------------------------------------------------- /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 !="boring") 4 | ORDER BY rating DESC; -------------------------------------------------------------------------------- /1757-recyclable-and-low-fat-products/1757-recyclable-and-low-fat-products.sql: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | SELECT product_id FROM Products 3 | WHERE low_fats ="Y" AND recyclable ="Y"; -------------------------------------------------------------------------------- /0169-majority-element/0169-majority-element.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int majorityElement(int[] nums) { 3 | Arrays.sort(nums); 4 | return nums[nums.length / 2]; 5 | } 6 | } -------------------------------------------------------------------------------- /1068-product-sales-analysis-i/1068-product-sales-analysis-i.sql: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | SELECT b.product_name , a.year , a.price 3 | FROM Sales a JOIN Product b ON a.product_id = b.product_id; -------------------------------------------------------------------------------- /0175-combine-two-tables/0175-combine-two-tables.sql: -------------------------------------------------------------------------------- 1 | /* Write your PL/SQL query statement below */ 2 | SELECT p.firstName,p.lastName,a.city,a.state 3 | FROM person p LEFT JOIN address a ON p.personId = a.personId 4 | -------------------------------------------------------------------------------- /1667-fix-names-in-a-table/1667-fix-names-in-a-table.sql: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | SELECT user_id, CONCAT(UPPER(LEFT(name,1)),LOWER(RIGHT(name,LENGTH(name)-1))) 3 | AS name FROM Users ORDER BY user_id ASC; -------------------------------------------------------------------------------- /0176-second-highest-salary/0176-second-highest-salary.sql: -------------------------------------------------------------------------------- 1 | -- Write your PostgreSQL query statement below 2 | SELECT MAX(salary) AS SecondHighestSalary 3 | FROM Employee 4 | WHERE salary < (SELECT MAX(salary) FROM Employee); 5 | -------------------------------------------------------------------------------- /0197-rising-temperature/0197-rising-temperature.sql: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | SELECT w1.id FROM Weather w1 JOIN Weather w2 3 | ON DATEDIFF(w1.recordDate,w2.recordDate) = 1 4 | WHERE w1.temperature > w2.temperature; -------------------------------------------------------------------------------- /1378-replace-employee-id-with-the-unique-identifier/1378-replace-employee-id-with-the-unique-identifier.sql: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | SELECT b.unique_id , a.name FROM Employees a 3 | LEFT JOIN EmployeeUNI b ON a.id = b.id; -------------------------------------------------------------------------------- /2356-number-of-unique-subjects-taught-by-each-teacher/2356-number-of-unique-subjects-taught-by-each-teacher.sql: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | SELECT teacher_id, COUNT(DISTINCT subject_id) AS cnt 3 | FROM Teacher GROUP BY teacher_id; 4 | -------------------------------------------------------------------------------- /0796-rotate-string/0796-rotate-string.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean rotateString(String s, String goal) { 3 | if(s.length() != goal.length()){ 4 | return false; 5 | } 6 | return (s+s).contains(goal); 7 | } 8 | } -------------------------------------------------------------------------------- /0434-number-of-segments-in-a-string/0434-number-of-segments-in-a-string.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int countSegments(String s) { 3 | s = s.trim(); 4 | if(s.length() == 0) return 0; 5 | String []words = s.split("\\s++"); 6 | return words.length; 7 | } 8 | } -------------------------------------------------------------------------------- /0088-merge-sorted-array/0088-merge-sorted-array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void merge(vector& nums1, int m, vector& nums2, int n) { 4 | for(int i=0;i& nums) { 4 | int n = nums.size(); 5 | int ans = 0; 6 | for(int i=0;i& nums) { 4 | sort(nums.begin(),nums.end()); 5 | for(int i=0;ix/m) r=m-1; 9 | else if(m dp(n+1,0); 7 | dp[1] = 1; 8 | dp[2] = 2; 9 | for(int i=3;i<=n;i++){ 10 | dp[i] = dp[i-1]+dp[i-2]; 11 | } 12 | return dp[n]; 13 | } 14 | }; -------------------------------------------------------------------------------- /2974-minimum-number-game/2974-minimum-number-game.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def numberGame(self, nums): 3 | """ 4 | :type nums: List[int] 5 | :rtype: List[int] 6 | """ 7 | nums.sort() 8 | for i in range(0, len(nums), 2): 9 | nums[i], nums[i+1] = nums[i+1], nums[i] 10 | return nums 11 | -------------------------------------------------------------------------------- /0201-bitwise-and-of-numbers-range/0201-bitwise-and-of-numbers-range.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int rangeBitwiseAnd(int left, int right) { 4 | int cnt = 0; 5 | while (left != right) { 6 | left >>= 1; 7 | right >>= 1; 8 | cnt++; 9 | } 10 | return (left << cnt); 11 | 12 | } 13 | }; -------------------------------------------------------------------------------- /0344-reverse-string/0344-reverse-string.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public void reverseString(char[] s) { 3 | int l = 0; 4 | int r = s.length - 1; 5 | 6 | while(l 0){ 8 | int rem = x%10; 9 | rev = rev*10+rem; 10 | x /= 10; 11 | } 12 | return (num==rev); 13 | } 14 | }; -------------------------------------------------------------------------------- /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=1;i<=n;i++){ 6 | if(i%2 != 0){ 7 | ans[i] = ans[i/2]+1; 8 | }else{ 9 | ans[i] = ans[i/2]; 10 | } 11 | } 12 | return ans; 13 | } 14 | }; -------------------------------------------------------------------------------- /0392-is-subsequence/0392-is-subsequence.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isSubsequence(string s, string t) { 4 | int i=0,j=0; 5 | while(i sortedSquares(vector& nums) { 4 | int n = nums.size(); 5 | vector ans(n); 6 | for(int i=0;i=0 ; i--){ 6 | if((num[i]-'0')%2 != 0){ 7 | return num.substr(0,i+1); 8 | } 9 | } 10 | return ""; 11 | } 12 | }; -------------------------------------------------------------------------------- /0326-power-of-three/0326-power-of-three.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isPowerOfThree(int n) { 4 | if(n <= 0) return false; 5 | int mx = 20; 6 | ; 7 | for(int i=0;i& nums) { 4 | int mx = 0; 5 | int c = 0; 6 | for(int i=0;i& arr, int k) { 4 | int ans = 0; 5 | sort(arr.begin(), arr.end(), greater()); 6 | int i = 0; 7 | while (k--) { 8 | ans = arr[i]; 9 | i++; 10 | } 11 | return ans; 12 | } 13 | }; -------------------------------------------------------------------------------- /0268-missing-number/0268-missing-number.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int missingNumber(vector& nums) { 4 | int n = nums.size(); 5 | int arrsum=0 , elesum=0; 6 | for(int i=0;i& nums) { 4 | int num = 1; 5 | sort(nums.begin(),nums.end()); 6 | 7 | for(int i=0;i0){ 9 | num++; 10 | } 11 | } 12 | return num; 13 | } 14 | }; -------------------------------------------------------------------------------- /0053-maximum-subarray/0053-maximum-subarray.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxSubArray(vector& nums) { 4 | int n = nums.size(); 5 | int ans = INT_MIN; 6 | int sum = 0; 7 | for(int i=0;i ans) ans = sum; 10 | if(sum < 0) sum = 0; 11 | } 12 | return ans; 13 | } 14 | }; -------------------------------------------------------------------------------- /0237-delete-node-in-a-linked-list/0237-delete-node-in-a-linked-list.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * public class ListNode { 4 | * int val; 5 | * ListNode next; 6 | * ListNode(int x) { val = x; } 7 | * } 8 | */ 9 | class Solution { 10 | public void deleteNode(ListNode node) { 11 | node.val = node.next.val; 12 | node.next = node.next.next; 13 | } 14 | } -------------------------------------------------------------------------------- /0442-find-all-duplicates-in-an-array/0442-find-all-duplicates-in-an-array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector findDuplicates(vector& nums) { 4 | unordered_map mp; 5 | vector ans; 6 | for(int i=0;i 1) ans.push_back(nums[i]); 9 | } 10 | return ans; 11 | } 12 | }; -------------------------------------------------------------------------------- /1217-minimum-cost-to-move-chips-to-the-same-position/1217-minimum-cost-to-move-chips-to-the-same-position.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minCostToMoveChips(vector& position) { 4 | int o=0,e=0; 5 | for(int i=0;i nums[0]) c++; 6 | for(int i=1; i nums[i]) c++; 8 | } 9 | if(c <= 1) return true; 10 | else return false; 11 | } 12 | } -------------------------------------------------------------------------------- /0258-add-digits/0258-add-digits.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int fun(int num){ 4 | while(num >= 10){ 5 | int sum = 0; 6 | while(num > 0){ 7 | sum += num%10; 8 | num /= 10; 9 | } 10 | num = sum; 11 | } 12 | return num; 13 | } 14 | 15 | int addDigits(int num) { 16 | return fun(num); 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 | set s; 5 | for(int i=0;i& p) { 4 | int minpri = p[0]; 5 | int mxpro = 0; 6 | 7 | for(int i=1;i& nums) { 4 | int n = nums.size(); 5 | if(n==1) return nums[0]; 6 | vector dp(n,0); 7 | dp[0] = nums[0]; 8 | dp[1] = max(nums[0],nums[1]); 9 | 10 | for(int i=2;i mp; 5 | 6 | for(auto ele : s){ 7 | mp[ele]++; 8 | } 9 | 10 | for(int i=0;i& cost) { 4 | int n = cost.size(); 5 | vector dp(n); 6 | dp[0] = cost[0]; 7 | dp[1] = cost[1]; 8 | 9 | for(int i=2;i=0 ;i--){ 7 | ans.append(arr[i]); 8 | ans.append(" "); 9 | } 10 | return ans.toString().trim(); 11 | } 12 | } -------------------------------------------------------------------------------- /0287-find-the-duplicate-number/0287-find-the-duplicate-number.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findDuplicate(vector& nums) { 4 | unordered_map mp; 5 | for(int i=0;i 1) ans = it.first; 12 | } 13 | return ans; 14 | } 15 | }; -------------------------------------------------------------------------------- /0560-subarray-sum-equals-k/0560-subarray-sum-equals-k.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int subarraySum(vector& nums, int k) { 4 | int c = 0; 5 | int n = nums.size(); 6 | for(int i=0;i& prices) { 4 | int ans = 0; 5 | int st = prices[0]; 6 | for(int i=1;i0){ 9 | int temp=n%10; 10 | sum+= temp*temp; 11 | n=n/10; 12 | } 13 | return isHappy(sum); 14 | } 15 | } 16 | }; -------------------------------------------------------------------------------- /0575-distribute-candies/0575-distribute-candies.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int distributeCandies(vector& candyType) { 4 | int n = candyType.size(); 5 | sort(candyType.begin(),candyType.end()); 6 | int t=1; 7 | for(int i=0;i= n/2) return n/2; 12 | return t; 13 | } 14 | }; -------------------------------------------------------------------------------- /0704-binary-search/0704-binary-search.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int bs(vector n,int t,int l,int h){ 4 | if(l > h) return -1; 5 | int m = l+(h-l)/2; 6 | if(n[m] == t) return m; 7 | else if(n[m] < t) return bs(n,t,m+1,h); 8 | else return bs(n,t,l,m-1); 9 | 10 | } 11 | int search(vector& nums, int target) { 12 | return bs(nums,target,0,nums.size()-1); 13 | } 14 | }; -------------------------------------------------------------------------------- /0048-rotate-image/0048-rotate-image.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void rotate(vector>& matrix) { 4 | int n = matrix.size(); 5 | for(int i=0;i findErrorNums(vector& nums) { 4 | vector ans(2,0); 5 | map mp; 6 | for(int ele : nums){ 7 | mp[ele]++; 8 | } 9 | 10 | for(int i=1;i<=nums.size();i++){ 11 | if(mp[i]==2) ans[0] = i; 12 | if(mp[i]==0) ans[1] = i; 13 | } 14 | return ans; 15 | } 16 | }; -------------------------------------------------------------------------------- /3232-find-if-digit-game-can-be-won/3232-find-if-digit-game-can-be-won.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def canAliceWin(self, nums): 3 | """ 4 | :type nums: List[int] 5 | :rtype: bool 6 | """ 7 | sum1,sum2 = 0,0 8 | for ele in nums : 9 | if ele < 10 : sum1 += ele 10 | else : sum2 += ele 11 | 12 | if sum1>sum2 or sum2>sum1 : return True 13 | else : return False -------------------------------------------------------------------------------- /0074-search-a-2d-matrix/0074-search-a-2d-matrix.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool searchMatrix(vector>& matrix, int target) { 4 | int r = matrix.size(); 5 | int c = matrix[0].size(); 6 | 7 | for(int i=0;i twoSum(vector& nums, int target) { 4 | map mp; 5 | for(int i=0;i majorityElement(vector& nums) { 4 | int n = nums.size(); 5 | map mp; 6 | for(int i=0;i ans; 11 | for(auto ele : mp){ 12 | if(ele.second > n/3) ans.push_back(ele.first); 13 | } 14 | return ans; 15 | } 16 | }; -------------------------------------------------------------------------------- /0242-valid-anagram/0242-valid-anagram.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isAnagram(string s, string t) { 4 | if(s.length() != t.length()){ 5 | return false; 6 | } 7 | sort(s.begin(),s.end()); 8 | sort(t.begin(),t.end()); 9 | 10 | for(int i=0;i& nums) { 4 | int zc = 0; 5 | for(int i=0;i 0 : 10 | lw = num%10 11 | if real%lw==0 and lw!=0 : 12 | count = count+1 13 | num = num/10 14 | return count 15 | -------------------------------------------------------------------------------- /0001-two-sum/0001-two-sum.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] twoSum(int[] nums, int target) { 3 | int[] ans = new int[2]; 4 | int len = nums.length; 5 | for(int i=0;i> construct2DArray(vector& original, int m, int n) { 4 | vector> ans(m, vector(n)); 5 | if(original.size()!=m*n) return {}; 6 | for(int i=0;i& s , vector& rev ,int n){ 4 | if(n==0) return; 5 | rev.push_back(s[n-1]); 6 | solve(s,rev,n-1); 7 | } 8 | 9 | void reverseString(vector& s) { 10 | vector rev; 11 | int n = s.size(); 12 | solve(s,rev,n); 13 | 14 | for(int i=0;i=0 ; i--){ 9 | if(str.charAt(i) != ' '){ 10 | count++; 11 | } 12 | else{ 13 | break; 14 | } 15 | } 16 | return count; 17 | } 18 | } -------------------------------------------------------------------------------- /0004-median-of-two-sorted-arrays/0004-median-of-two-sorted-arrays.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | double findMedianSortedArrays(vector& nums1, vector& nums2) { 4 | 5 | nums1.insert(nums1.end(), nums2.begin(), nums2.end()); 6 | 7 | std::sort(nums1.begin(), nums1.end()); 8 | 9 | int size = nums1.size(); 10 | 11 | if(size % 2 == 0) return (nums1[size / 2 - 1] + nums1[size / 2]) / 2.0; 12 | 13 | else return (nums1[size / 2]); 14 | } 15 | }; -------------------------------------------------------------------------------- /1539-kth-missing-positive-number/1539-kth-missing-positive-number.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findKthPositive(vector& arr, int k) { 4 | int l = 0; 5 | int r = arr.size()-1; 6 | while(l <= r){ 7 | int m = l+(r-l)/2; 8 | int missingele = arr[m] - (m+1); 9 | if(missingele < k){ 10 | l = m+1; 11 | }else{ 12 | r = m-1; 13 | } 14 | } 15 | return l+k; 16 | } 17 | }; -------------------------------------------------------------------------------- /3581-the-two-sneaky-numbers-of-digitville/3581-the-two-sneaky-numbers-of-digitville.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector getSneakyNumbers(vector& nums) { 4 | vector ans; 5 | map mp; 6 | for(int i=0;i& height) { 4 | int l = 0; 5 | int r = height.size()-1; 6 | int ans = 0; 7 | 8 | while(l& nums) { 4 | map mp; 5 | for(int i=0;i mxfreq){ 12 | mxfreq = ele.second; 13 | mxele = ele.first; 14 | } 15 | } 16 | return mxele; 17 | } 18 | }; -------------------------------------------------------------------------------- /0349-intersection-of-two-arrays/0349-intersection-of-two-arrays.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector intersection(vector& nums1, vector& nums2) { 4 | set s1,s2; 5 | 6 | for(auto ele : nums1){ 7 | s1.insert(ele); 8 | } 9 | 10 | for(auto ele : nums2){ 11 | if(s1.find(ele) != s1.end()){ 12 | s2.insert(ele); 13 | } 14 | } 15 | vector ans(s2.begin(),s2.end()); 16 | return ans; 17 | } 18 | }; -------------------------------------------------------------------------------- /1021-remove-outermost-parentheses/1021-remove-outermost-parentheses.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string removeOuterParentheses(string s) { 4 | int bc = 0; 5 | int n = s.length(); 6 | string ans=""; 7 | for(int i=0;i 0) ans += "("; 10 | bc++; 11 | }else{ 12 | bc--; 13 | if(bc > 0) ans += ")"; 14 | } 15 | } 16 | return ans; 17 | } 18 | }; -------------------------------------------------------------------------------- /1614-maximum-nesting-depth-of-the-parentheses/1614-maximum-nesting-depth-of-the-parentheses.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxDepth(string s) { 4 | int c = 0; 5 | int ans = 0; 6 | for(int i=0;i& nums, int k) { 4 | int n = nums.size(); 5 | if(n<=1) return; 6 | k = k%n; 7 | vector ans; 8 | for(int i=n-k; i<=n-1 ; i++){ 9 | ans.push_back(nums[i]); 10 | } 11 | for(int i=0;i findDisappearedNumbers(vector& nums) { 4 | vector ans; 5 | int n = nums.size(); 6 | vector freq(n+1,0); 7 | 8 | for(int n : nums){ 9 | freq[n]++; 10 | } 11 | 12 | for(int i=1;i<=n;i++){ 13 | if(!freq[i]){ 14 | ans.push_back(i); 15 | } 16 | } 17 | return ans; 18 | } 19 | }; -------------------------------------------------------------------------------- /0035-search-insert-position/0035-search-insert-position.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int searchInsert(vector& nums, int target) { 4 | int l = 0; 5 | int r = nums.size()-1; 6 | if(nums[r] < target) return r+1; 7 | while(l <= r){ 8 | int m = l+(r-l)/2; 9 | if(nums[m] == target) return m; 10 | else if(nums[m] < target){ 11 | l = m+1; 12 | }else{ 13 | r = m-1; 14 | } 15 | } 16 | return l; 17 | } 18 | }; -------------------------------------------------------------------------------- /0205-isomorphic-strings/0205-isomorphic-strings.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isIsomorphic(string s, string t){ 4 | vector idxs(128,0); 5 | vector idxt(128,0); 6 | if(s.length() != t.length()){ 7 | return false; 8 | } 9 | 10 | for(int i=0;i& fruits, vector& baskets) { 4 | int n = fruits.size(), alloted = 0; 5 | for (int i = 0; i < n; ++i) { 6 | for (int j = 0; j < n; ++j) { 7 | if (fruits[i] <= baskets[j]) { 8 | baskets[j] = -1; 9 | alloted++; 10 | break; 11 | } 12 | } 13 | } 14 | return n - alloted; 15 | } 16 | }; -------------------------------------------------------------------------------- /0744-find-smallest-letter-greater-than-target/0744-find-smallest-letter-greater-than-target.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | char nextGreatestLetter(vector& letters, char target) { 4 | int l = 0; 5 | int r = letters.size() - 1; 6 | while (l <= r) { 7 | int mid = l + (r - l) / 2; 8 | if (letters[mid] <= target) { 9 | l = mid + 1; 10 | } else { 11 | r = mid - 1; 12 | } 13 | } 14 | return letters[l % letters.size()]; 15 | } 16 | }; -------------------------------------------------------------------------------- /0007-reverse-integer/0007-reverse-integer.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int reverse(int x) { 3 | String n = ""+x; 4 | String rev = ""; 5 | if(x>0){ 6 | rev = new StringBuilder(n).reverse().toString(); 7 | }else if(x<0){ 8 | rev = "-"+new StringBuilder(n.substring(1)).reverse().toString(); 9 | }else{ 10 | return 0; 11 | } 12 | try { 13 | return Integer.parseInt(rev); 14 | }catch (Exception e) { 15 | return 0; 16 | } 17 | } 18 | } -------------------------------------------------------------------------------- /0476-number-complement/0476-number-complement.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int findComplement(int num) { 3 | String bS = Integer.toBinaryString(num); 4 | StringBuilder ansSt = new StringBuilder(); 5 | int len = bS.length(); 6 | 7 | for(int i=0;i> dp(m,vector(n)); 5 | for(int col=0;col map = new HashMap<>(); 4 | for (int num : nums) { 5 | map.put(num, map.getOrDefault(num, 0) + 1); 6 | } 7 | int maxLen = 0; 8 | for (int key : map.keySet()) { 9 | if (map.containsKey(key + 1)) { 10 | maxLen = Math.max(maxLen, map.get(key) + map.get(key + 1)); 11 | } 12 | } 13 | return maxLen; 14 | } 15 | } -------------------------------------------------------------------------------- /0141-linked-list-cycle/0141-linked-list-cycle.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode(int x) : val(x), next(NULL) {} 7 | * }; 8 | */ 9 | class Solution { 10 | public: 11 | bool hasCycle(ListNode *head) { 12 | unordered_set visited; 13 | 14 | while(head){ 15 | if(visited.find(head)!=visited.end()) return true; 16 | 17 | visited.insert(head); 18 | head = head->next; 19 | } 20 | return false; 21 | } 22 | }; -------------------------------------------------------------------------------- /2149-rearrange-array-elements-by-sign/2149-rearrange-array-elements-by-sign.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector rearrangeArray(vector& nums) { 4 | int n = nums.size(); 5 | vector ans(n); 6 | int pidx = 0 , nidx = 1; 7 | for(int ele : nums){ 8 | if(ele >= 0){ 9 | ans[pidx] = ele; 10 | pidx += 2; 11 | } 12 | if(ele < 0){ 13 | ans[nidx] = ele; 14 | nidx += 2; 15 | } 16 | } 17 | return ans; 18 | } 19 | }; -------------------------------------------------------------------------------- /0153-find-minimum-in-rotated-sorted-array/0153-find-minimum-in-rotated-sorted-array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findMin(vector& arr) { 4 | int low = 0, high = arr.size() - 1; 5 | int ans = INT_MAX; 6 | while (low <= high) { 7 | int mid = (low + high) / 2; 8 | if (arr[low] <= arr[mid]) { 9 | ans = min(ans, arr[low]); 10 | low = mid + 1; 11 | } 12 | else { 13 | ans = min(ans, arr[mid]); 14 | high = mid - 1; 15 | } 16 | } 17 | return ans; 18 | } 19 | }; -------------------------------------------------------------------------------- /0451-sort-characters-by-frequency/0451-sort-characters-by-frequency.cpp: -------------------------------------------------------------------------------- 1 | class Solution{ 2 | public: 3 | string frequencySort(string s) { 4 | unordered_map freq; 5 | for(char c : s){ 6 | freq[c]++; 7 | } 8 | priority_queue> maxheap; 9 | for(auto ele : freq){ 10 | maxheap.push({ele.second , ele.first}); 11 | } 12 | string ans = ""; 13 | while(!maxheap.empty()){ 14 | auto [val,key] = maxheap.top(); 15 | maxheap.pop(); 16 | ans.append(val,key); 17 | } 18 | return ans; 19 | } 20 | }; -------------------------------------------------------------------------------- /0056-merge-intervals/0056-merge-intervals.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> merge(vector>& intervals) { 4 | vector> ans; 5 | sort(intervals.begin(),intervals.end()); 6 | int n = intervals.size(); 7 | 8 | for(int i=0;i ans.back()[1]){ 10 | ans.push_back(intervals[i]); 11 | }else{ 12 | ans.back()[1] = max(ans.back()[1],intervals[i][1]); 13 | } 14 | } 15 | return ans; 16 | } 17 | }; -------------------------------------------------------------------------------- /0448-find-all-numbers-disappeared-in-an-array/0448-find-all-numbers-disappeared-in-an-array.cs: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public IList FindDisappearedNumbers(int[] nums) { 3 | IList ans = new List(); 4 | 5 | foreach(int num in nums){ 6 | int indx = Math.Abs(num)-1; 7 | if(nums[indx] > 0){ 8 | nums[indx] *= -1; 9 | } 10 | } 11 | for(int i=0;i 0){ 13 | ans.Add(i+1); 14 | } 15 | } 16 | return ans; 17 | } 18 | } -------------------------------------------------------------------------------- /0135-candy/0135-candy.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int candy(vector &ratings) 4 | { 5 | int size=ratings.size(); 6 | if(size<=1) 7 | return size; 8 | vector num(size,1); 9 | for (int i = 1; i < size; i++) 10 | { 11 | if(ratings[i]>ratings[i-1]) 12 | num[i]=num[i-1]+1; 13 | } 14 | for (int i= size-1; i>0 ; i--) 15 | { 16 | if(ratings[i-1]>ratings[i]) 17 | num[i-1]=max(num[i]+1,num[i-1]); 18 | } 19 | int result=0; 20 | for (int i = 0; i < size; i++) 21 | { 22 | result+=num[i]; 23 | // cout< n , vector> &a , int idx){ 4 | if(idx >= n.size()){ 5 | a.push_back(n); 6 | return; 7 | } 8 | 9 | for(int i = idx;i> permute(vector& nums) { 17 | vector> ans; 18 | int i = 0; 19 | solve(nums,ans,i); 20 | return ans; 21 | } 22 | }; -------------------------------------------------------------------------------- /0049-group-anagrams/0049-group-anagrams.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> groupAnagrams(vector& strs) { 4 | unordered_map> anagramMap; 5 | 6 | for (string s : strs) { 7 | string sortedStr = s; 8 | sort(sortedStr.begin(), sortedStr.end()); 9 | anagramMap[sortedStr].push_back(s); 10 | } 11 | 12 | vector> result; 13 | for (auto& pair : anagramMap) { 14 | result.push_back(pair.second); 15 | } 16 | 17 | return result; 18 | 19 | } 20 | }; -------------------------------------------------------------------------------- /0074-search-a-2d-matrix/0074-search-a-2d-matrix.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean searchMatrix(int[][] matrix, int target) { 3 | int n = matrix.length; 4 | int m = matrix[0].length; 5 | int l = 0; 6 | int h = (m*n-1); 7 | while(l <= h){ 8 | int mid = (l+h)/2; 9 | int row = mid/m; 10 | int col = mid%m; 11 | if(matrix[row][col]==target) return true; 12 | if(matrix[row][col] < target) l = mid+1; 13 | if(matrix[row][col] > target) h = mid-1; 14 | } 15 | return false; 16 | 17 | } 18 | } -------------------------------------------------------------------------------- /0228-summary-ranges/0228-summary-ranges.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public List summaryRanges(int[] nums) { 3 | ArrayList ans = new ArrayList<>(); 4 | 5 | for(int i=0;i"+nums[i]); 17 | } 18 | 19 | 20 | } 21 | return ans; 22 | } 23 | } -------------------------------------------------------------------------------- /0238-product-of-array-except-self/0238-product-of-array-except-self.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector productExceptSelf(vector& nums) { 4 | int n = nums.size(); 5 | vector l(n); 6 | vector r(n); 7 | vector ans(n); 8 | 9 | l[0] = 1; 10 | for(int i=1;i=0;i--){ 16 | r[i] = r[i+1]*nums[i+1]; 17 | } 18 | 19 | for(int i=0;i st; 5 | for(int i=0;i& dp){ 4 | if(n==0 || n== 1){ 5 | return 1; 6 | } 7 | if(dp[n]!=-1) return dp[n]; 8 | int sum = 0; 9 | for(int i=1;i<=n;i++){ 10 | int ls = solve(i-1 ,dp); 11 | int rs = solve(n-i ,dp); 12 | sum += ls*rs; 13 | } 14 | dp[n] = sum; 15 | return dp[n]; 16 | } 17 | int numTrees(int n) { 18 | vector dp(n+1,-1); 19 | int ans = solve(n,dp); 20 | return ans; 21 | } 22 | }; -------------------------------------------------------------------------------- /0028-find-the-index-of-the-first-occurrence-in-a-string/0028-find-the-index-of-the-first-occurrence-in-a-string.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int strStr(String haystack, String needle) { 3 | int h = haystack.length(); 4 | int n = needle.length(); 5 | if (n > h) { 6 | return -1; 7 | } 8 | for (int i = 0; i <= h - n; i++) { 9 | if (haystack.charAt(i) == needle.charAt(0)) { 10 | if (haystack.substring(i, i + n).equals(needle)) { 11 | return i; 12 | } 13 | } 14 | } 15 | return -1; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /1684-count-the-number-of-consistent-strings/1684-count-the-number-of-consistent-strings.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int countConsistentStrings(string allowed, vector& words) { 4 | unordered_set us(allowed.begin() , allowed.end()); 5 | int c = 0; 6 | for(const string word : words){ 7 | bool isExist = true; 8 | for(char c : word){ 9 | if(us.find(c)==us.end()){ 10 | isExist = false; 11 | break; 12 | } 13 | } 14 | if(isExist) c++; 15 | } 16 | return c; 17 | } 18 | }; -------------------------------------------------------------------------------- /0067-add-binary/0067-add-binary.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string addBinary(string a, string b) { 4 | int i = a.length()-1; 5 | int j = b.length()-1; 6 | int c = 0; 7 | string ans; 8 | 9 | while(i>=0 || j>=0 || c){ 10 | if(i >= 0){ 11 | c += a[i]-'0'; 12 | i--; 13 | } 14 | if(j >= 0){ 15 | c += b[j]-'0'; 16 | j--; 17 | } 18 | ans += (c%2+'0'); 19 | c = c/2; 20 | } 21 | reverse(ans.begin(),ans.end()); 22 | return ans; 23 | } 24 | }; -------------------------------------------------------------------------------- /0075-sort-colors/0075-sort-colors.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void sortColors(vector& nums) { 4 | // Dutch National Flag Algo 5 | 6 | int low = 0 , mid = 0 , high = nums.size()-1; 7 | 8 | while(mid <= high){ 9 | if(nums[mid]==0){ 10 | swap(nums[low],nums[mid]); 11 | low++; 12 | mid++; 13 | } 14 | else if(nums[mid]==1){ 15 | mid++; 16 | } 17 | else if(nums[mid]==2){ 18 | swap(nums[mid],nums[high]); 19 | high--; 20 | } 21 | } 22 | } 23 | }; -------------------------------------------------------------------------------- /0303-range-sum-query-immutable/0303-range-sum-query-immutable.cpp: -------------------------------------------------------------------------------- 1 | class NumArray { 2 | public: 3 | vector arr; 4 | NumArray(vector& nums) { 5 | int sum = 0; 6 | for(int i=0;isumRange(left,right); 22 | */ -------------------------------------------------------------------------------- /0042-trapping-rain-water/0042-trapping-rain-water.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int trap(vector& height) { 4 | int n = height.size(); 5 | 6 | vector l(n); 7 | vector r(n); 8 | 9 | l[0] = height[0]; 10 | for(int i=1;i=0;i--){ 16 | r[i] = max(r[i+1],height[i]); 17 | } 18 | 19 | int ans = 0; 20 | for(int i=0;i> generate(int numRows) { 13 | vector> ans; 14 | for(int r=1;r<=numRows;r++){ 15 | vector store; 16 | for(int c=1;c<=r;c++){ 17 | store.push_back(nCr(r-1,c-1)); 18 | } 19 | ans.push_back(store); 20 | } 21 | return ans; 22 | } 23 | }; -------------------------------------------------------------------------------- /0050-powx-n/0050-powx-n.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | double myPow(double x, int n) { 4 | double ans = 1.0; 5 | long power = n; // Convert n to long to avoid overflow 6 | 7 | if (power < 0) { 8 | power = -power; // Make it positive 9 | x = 1 / x; // Take reciprocal 10 | } 11 | 12 | while (power > 0) { 13 | if (power % 2 == 1) { 14 | ans = ans * x; 15 | power = power - 1; 16 | } else { 17 | x = x * x; 18 | power = power / 2; 19 | } 20 | } 21 | return ans; 22 | } 23 | }; -------------------------------------------------------------------------------- /0050-powx-n/0050-powx-n.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public double myPow(double x, int n) { 3 | double ans = 1.0; 4 | long power = n; // Convert n to long to avoid overflow 5 | 6 | if (power < 0) { 7 | power = -power; // Make it positive 8 | x = 1 / x; // Take reciprocal 9 | } 10 | 11 | while (power > 0) { 12 | if (power % 2 == 1) { 13 | ans = ans * x; 14 | power = power - 1; 15 | } else { 16 | x = x * x; 17 | power = power / 2; 18 | } 19 | } 20 | return ans; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /1143-longest-common-subsequence/1143-longest-common-subsequence.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int longestCommonSubsequence(string text1, string text2) { 4 | int n = text1.length(); 5 | int m = text2.length(); 6 | 7 | vector>dp(n+1,vector(m+1,0)); 8 | 9 | for(int i=1;i<=n;i++){ 10 | for(int j=1;j<=m;j++){ 11 | if(text1[i-1] == text2[j-1]){ 12 | dp[i][j] = 1+dp[i-1][j-1]; 13 | }else{ 14 | dp[i][j] = max(dp[i-1][j],dp[i][j-1]); 15 | } 16 | } 17 | } 18 | return dp[n][m]; 19 | } 20 | }; -------------------------------------------------------------------------------- /0128-longest-consecutive-sequence/0128-longest-consecutive-sequence.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int longestConsecutive(vector& nums) { 4 | if(nums.size()==0) return 0; 5 | int longest = 1 , count = 0 , lastsmall = INT_MIN; 6 | sort(nums.begin(),nums.end()); 7 | for(int i=0;ileft); 17 | int r = countNodes(root->right); 18 | return 1+l+r; 19 | } 20 | }; -------------------------------------------------------------------------------- /0876-middle-of-the-linked-list/0876-middle-of-the-linked-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* middleNode(ListNode* head) { 14 | ListNode* slow = head; 15 | ListNode* fast = head; 16 | while(fast!=NULL && fast->next!=NULL){ 17 | fast = fast->next->next; 18 | slow = slow->next; 19 | } 20 | return slow; 21 | } 22 | }; -------------------------------------------------------------------------------- /0141-linked-list-cycle/0141-linked-list-cycle.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * class ListNode { 4 | * int val; 5 | * ListNode next; 6 | * ListNode(int x) { 7 | * val = x; 8 | * next = null; 9 | * } 10 | * } 11 | */ 12 | public class Solution { 13 | public boolean hasCycle(ListNode head) { 14 | if(head==null || head.next==null) return false; 15 | 16 | ListNode s = head; 17 | ListNode f = head; 18 | 19 | while(f.next!=null && f.next.next!=null){ 20 | s = s.next; 21 | f = f.next.next; 22 | 23 | if(s==f) return true; 24 | } 25 | return false; 26 | 27 | } 28 | } -------------------------------------------------------------------------------- /0583-delete-operation-for-two-strings/0583-delete-operation-for-two-strings.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minDistance(string word1, string word2) { 4 | int n = word1.length(); 5 | int m = word2.length(); 6 | 7 | vector>dp(n+1,vector(m+1,0)); 8 | 9 | for(int i=1;i<=n;i++){ 10 | for(int j=1;j<=m;j++){ 11 | if(word1[i-1] == word2[j-1]){ 12 | dp[i][j] = 1+dp[i-1][j-1]; 13 | }else{ 14 | dp[i][j] = max(dp[i-1][j],dp[i][j-1]); 15 | } 16 | } 17 | } 18 | int lcs = dp[n][m]; 19 | int ans = m-lcs+n-lcs; 20 | return ans; 21 | } 22 | }; -------------------------------------------------------------------------------- /0100-same-tree/0100-same-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 isSameTree(TreeNode* p, TreeNode* q) { 15 | if(!p && !q) return true; 16 | if(!p || !q) return false; 17 | if(p->val != q->val) return false; 18 | return isSameTree(p->left,q->left) && isSameTree(p->right,q->right); 19 | } 20 | }; -------------------------------------------------------------------------------- /0234-palindrome-linked-list/0234-palindrome-linked-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 | bool isPalindrome(ListNode* head) { 14 | ListNode* t = head; 15 | vector arr; 16 | while(t!=NULL){ 17 | arr.push_back(t->val); 18 | t = t->next; 19 | } 20 | vector rev = arr; 21 | reverse(rev.begin(),rev.end()); 22 | 23 | return arr==rev; 24 | } 25 | }; -------------------------------------------------------------------------------- /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==NULL || root==p || root==q) return root; 14 | 15 | TreeNode* l = lowestCommonAncestor(root->left,p,q); 16 | TreeNode* r = lowestCommonAncestor(root->right,p,q); 17 | 18 | if(l==NULL) return r; 19 | if(r==NULL) return l; 20 | return root; 21 | } 22 | }; -------------------------------------------------------------------------------- /0073-set-matrix-zeroes/0073-set-matrix-zeroes.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void setZeroes(vector>& mtx) { 4 | int n = mtx.size(); // no of row 5 | int m = mtx[0].size(); // no of col 6 | 7 | vector r(n,0); 8 | vector c(m,0); 9 | 10 | for(int i=0;i& nums, vector>& ans, vector& ds , int idx){ 4 | if(idx==nums.size()){ 5 | if(find(ans.begin(),ans.end(), ds) == ans.end()){ 6 | ans.push_back(ds); 7 | } 8 | return; 9 | } 10 | subsets(nums,ans,ds,idx+1); 11 | ds.push_back(nums[idx]); 12 | subsets(nums,ans,ds,idx+1); 13 | ds.pop_back(); 14 | } 15 | 16 | vector> subsetsWithDup(vector& nums) { 17 | sort(nums.begin(),nums.end()); 18 | vector> ans; 19 | vector ds; 20 | subsets(nums , ans , ds, 0); 21 | return ans; 22 | } 23 | }; -------------------------------------------------------------------------------- /0036-valid-sudoku/0036-valid-sudoku.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isValidSudoku(vector>& board) { 4 | bool rows[9][9] = {false}; 5 | bool cols[9][9] = {false}; 6 | bool boxes[9][9] = {false}; 7 | 8 | for (int i = 0; i < 9; i++) { 9 | for (int j = 0; j < 9; j++) { 10 | if (board[i][j] != '.') { 11 | int num = board[i][j] - '1'; 12 | int boxIndex = (i / 3) * 3 + (j / 3); 13 | if (rows[i][num] || cols[j][num] || boxes[boxIndex][num]) return false; 14 | rows[i][num] = cols[j][num] = boxes[boxIndex][num] = true; 15 | } 16 | } 17 | } 18 | return true; 19 | } 20 | }; -------------------------------------------------------------------------------- /0104-maximum-depth-of-binary-tree/0104-maximum-depth-of-binary-tree.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * public class TreeNode { 4 | * int val; 5 | * TreeNode left; 6 | * TreeNode right; 7 | * TreeNode() {} 8 | * TreeNode(int val) { this.val = val; } 9 | * TreeNode(int val, TreeNode left, TreeNode right) { 10 | * this.val = val; 11 | * this.left = left; 12 | * this.right = right; 13 | * } 14 | * } 15 | */ 16 | class Solution { 17 | public int maxDepth(TreeNode root) { 18 | if(root == null) return 0; 19 | 20 | int leftSub = maxDepth(root.left); 21 | int rightSub = maxDepth(root.right); 22 | 23 | return 1+ Math.max(leftSub,rightSub); 24 | 25 | } 26 | } -------------------------------------------------------------------------------- /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==NULL || root==p || root==q) return root; 15 | TreeNode* left = lowestCommonAncestor(root->left,p,q); 16 | TreeNode* right = lowestCommonAncestor(root->right,p,q); 17 | 18 | if(left==NULL) return right; 19 | if(right==NULL) return left; 20 | return root; 21 | } 22 | }; -------------------------------------------------------------------------------- /3201-find-the-maximum-length-of-valid-subsequence-i/3201-find-the-maximum-length-of-valid-subsequence-i.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maximumLength(vector& nums) { 4 | int oddCnt = 0, evenCnt = 0, cnt = 0; 5 | bool flag = nums[0] % 2 == 1; 6 | 7 | for (int num : nums) { 8 | if (num % 2 == 1) { 9 | if (flag) { 10 | flag = !flag; 11 | cnt++; 12 | } 13 | oddCnt++; 14 | } else { 15 | if (!flag) { 16 | flag = !flag; 17 | cnt++; 18 | } 19 | evenCnt++; 20 | } 21 | } 22 | return max({cnt, oddCnt, evenCnt}); 23 | } 24 | }; -------------------------------------------------------------------------------- /0540-single-element-in-a-sorted-array/0540-single-element-in-a-sorted-array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int singleNonDuplicate(vector& nums) { 4 | int n = nums.size(); 5 | if(n==1) return nums[0]; 6 | if(nums[0]!=nums[1]) return nums[0]; 7 | if(nums[n-1]!=nums[n-2]) return nums[n-1]; 8 | int l = 1; 9 | int r = n-2; 10 | while(l <= r){ 11 | int m = (l+r)/2; 12 | if(nums[m]!=nums[m+1] && nums[m]!=nums[m-1]){ 13 | return nums[m]; 14 | } 15 | if( (m%2==1 && nums[m]==nums[m-1]) 16 | || (m%2==0 && nums[m]==nums[m+1])){ 17 | l = m+1; 18 | }else{ 19 | r = m-1; 20 | } 21 | } 22 | return -1; 23 | } 24 | }; -------------------------------------------------------------------------------- /0225-implement-stack-using-queues/0225-implement-stack-using-queues.cpp: -------------------------------------------------------------------------------- 1 | class MyStack { 2 | private: 3 | deque ans; 4 | public: 5 | MyStack() { 6 | ans = deque(); 7 | } 8 | 9 | void push(int x) { 10 | ans.push_back(x); 11 | } 12 | 13 | int pop() { 14 | int topele = ans.back(); 15 | ans.pop_back(); 16 | return topele; 17 | } 18 | 19 | int top() { 20 | return ans.back(); 21 | } 22 | 23 | bool empty() { 24 | return ans.empty(); 25 | } 26 | }; 27 | 28 | /** 29 | * Your MyStack object will be instantiated and called as such: 30 | * MyStack* obj = new MyStack(); 31 | * obj->push(x); 32 | * int param_2 = obj->pop(); 33 | * int param_3 = obj->top(); 34 | * bool param_4 = obj->empty(); 35 | */ -------------------------------------------------------------------------------- /0128-longest-consecutive-sequence/0128-longest-consecutive-sequence.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int longestConsecutive(int[] nums) { 3 | if (nums.length == 0) return 0; 4 | SortedSet numSet = new TreeSet<>(); 5 | for (int num : nums) { 6 | numSet.add(num); // TreeSet automatically sorts elements and removes duplicates 7 | } 8 | 9 | int longest = 1, count = 0, lastSmall = Integer.MIN_VALUE; 10 | for (int num : numSet) { 11 | if (num - 1 == lastSmall) { 12 | count++; 13 | lastSmall = num; 14 | } else { 15 | count = 1; 16 | lastSmall = num; 17 | } 18 | longest = Math.max(longest, count); 19 | } 20 | return longest; 21 | } 22 | } -------------------------------------------------------------------------------- /0700-search-in-a-binary-search-tree/0700-search-in-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() : 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* searchBST(TreeNode* root, int val) { 15 | if(!root) return NULL; 16 | if(root->val == val) return root; 17 | 18 | if(root->val > val){ 19 | return searchBST(root->left,val); 20 | }else{ 21 | return searchBST(root->right,val); 22 | } 23 | } 24 | }; -------------------------------------------------------------------------------- /0783-search-in-a-binary-search-tree/0783-search-in-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() : 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* searchBST(TreeNode* root, int val) { 15 | if(!root) return NULL; 16 | if(root->val == val) return root; 17 | 18 | if(root->val > val){ 19 | return searchBST(root->left,val); 20 | }else{ 21 | return searchBST(root->right,val); 22 | } 23 | } 24 | }; -------------------------------------------------------------------------------- /0226-invert-binary-tree/0226-invert-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 | TreeNode* invertTree(TreeNode* root) { 15 | if(!root){ 16 | return NULL; 17 | } 18 | TreeNode* temp = root->left; 19 | root->left = root->right; 20 | root->right = temp; 21 | 22 | invertTree(root->left); 23 | invertTree(root->right); 24 | 25 | return root; 26 | } 27 | }; -------------------------------------------------------------------------------- /0226-invert-binary-tree/0226-invert-binary-tree.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * public class TreeNode { 4 | * int val; 5 | * TreeNode left; 6 | * TreeNode right; 7 | * TreeNode() {} 8 | * TreeNode(int val) { this.val = val; } 9 | * TreeNode(int val, TreeNode left, TreeNode right) { 10 | * this.val = val; 11 | * this.left = left; 12 | * this.right = right; 13 | * } 14 | * } 15 | */ 16 | class Solution { 17 | public TreeNode invertTree(TreeNode root) { 18 | if(root==null){ 19 | return null; 20 | } 21 | TreeNode temp = root.left; 22 | root.left = root.right; 23 | root.right = temp; 24 | 25 | invertTree(root.left); 26 | invertTree(root.right); 27 | 28 | return root; 29 | } 30 | } -------------------------------------------------------------------------------- /0543-diameter-of-binary-tree/0543-diameter-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 mx = 0; 15 | int height(TreeNode* root){ 16 | if(!root) return 0; 17 | int l = height(root->left); 18 | int r = height(root->right); 19 | mx = max(mx,l+r); 20 | return 1+max(l,r); 21 | } 22 | 23 | int diameterOfBinaryTree(TreeNode* root) { 24 | height(root); 25 | return mx; 26 | } 27 | }; -------------------------------------------------------------------------------- /1498-number-of-subsequences-that-satisfy-the-given-sum-condition/1498-number-of-subsequences-that-satisfy-the-given-sum-condition.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int numSubseq(int[] nums, int target) { 3 | int mod = 1000000007 , n = nums.length; 4 | Arrays.sort(nums); 5 | 6 | int[] power = new int[n]; 7 | power[0] = 1; 8 | for (int i = 1; i < n; i++) { 9 | power[i] = (power[i - 1] * 2) % mod; 10 | } 11 | int left = 0 , right = n - 1 , result = 0; 12 | 13 | while (left <= right) { 14 | if (nums[left] + nums[right] <= target) { 15 | result = (result + power[right - left]) % mod; 16 | left++; 17 | } else { 18 | right--; 19 | } 20 | } 21 | return result; 22 | } 23 | } -------------------------------------------------------------------------------- /0033-search-in-rotated-sorted-array/0033-search-in-rotated-sorted-array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int search(vector& nums, int target) { 4 | int l = 0; 5 | int r = nums.size()-1; 6 | while(l<=r){ 7 | int m = l+(r-l)/2; 8 | if(nums[m] == target){ 9 | return m; 10 | } 11 | if(nums[m] >= nums[l]){ // left 12 | if(nums[l] <= target && nums[m] >= target){ 13 | r = m-1; 14 | }else{ 15 | l = m+1; 16 | } 17 | }else{ // right 18 | if(nums[m] <= target && nums[r] >= target){ 19 | l = m+1; 20 | }else{ 21 | r = m-1; 22 | } 23 | } 24 | } 25 | return -1; 26 | } 27 | }; -------------------------------------------------------------------------------- /0033-search-in-rotated-sorted-array/0033-search-in-rotated-sorted-array.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int search(int[] nums, int target) { 3 | int l = 0; 4 | int r = nums.length-1; 5 | while(l<=r){ 6 | int m = l+(r-l)/2; 7 | if(nums[m] == target){ 8 | return m; 9 | } 10 | if(nums[l] <= nums[m]){ 11 | if(nums[l] <= target && target < nums[m]){ 12 | r = m-1; 13 | }else{ 14 | l = m+1; 15 | } 16 | } 17 | else{ 18 | if(nums[m] < target && target<= nums[r]){ 19 | l = m+1; 20 | }else{ 21 | r = m-1; 22 | } 23 | } 24 | } 25 | return -1; 26 | } 27 | } -------------------------------------------------------------------------------- /0015-3sum/0015-3sum.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public List> threeSum(int[] arr) { 3 | if(arr == null || arr.length < 3) return new ArrayList<>(); 4 | Arrays.sort(arr); 5 | Set> res = new HashSet<>(); 6 | 7 | for(int i=0;i(res); 25 | } 26 | } -------------------------------------------------------------------------------- /0206-reverse-linked-list/0206-reverse-linked-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* reverseList(ListNode* head) { 14 | ListNode* temp = head; 15 | stack st; 16 | while(temp!=NULL){ 17 | st.push(temp->val); 18 | temp = temp->next; 19 | } 20 | temp = head; 21 | while(!st.empty()){ 22 | int x = st.top(); 23 | temp->val = x; 24 | temp = temp->next; 25 | st.pop(); 26 | } 27 | return head; 28 | } 29 | }; -------------------------------------------------------------------------------- /0111-minimum-depth-of-binary-tree/0111-minimum-depth-of-binary-tree.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * public class TreeNode { 4 | * int val; 5 | * TreeNode left; 6 | * TreeNode right; 7 | * TreeNode() {} 8 | * TreeNode(int val) { this.val = val; } 9 | * TreeNode(int val, TreeNode left, TreeNode right) { 10 | * this.val = val; 11 | * this.left = left; 12 | * this.right = right; 13 | * } 14 | * } 15 | */ 16 | class Solution { 17 | public int minDepth(TreeNode root) { 18 | if(root == null) return 0; 19 | 20 | int leftSub = minDepth(root.left); 21 | int rightSub = minDepth(root.right); 22 | 23 | if(leftSub == 0) return 1+rightSub; 24 | if(rightSub == 0) return 1+leftSub; 25 | 26 | return 1+Math.min(leftSub,rightSub); 27 | } 28 | } -------------------------------------------------------------------------------- /0083-remove-duplicates-from-sorted-list/0083-remove-duplicates-from-sorted-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* deleteDuplicates(ListNode* head) { 14 | ListNode* p = head; 15 | while(p!=NULL && p->next!=NULL){ 16 | if(p->val == p->next->val){ 17 | ListNode* duplicate = p->next; 18 | p->next = p->next->next; 19 | delete duplicate; 20 | }else{ 21 | p = p->next; 22 | } 23 | } 24 | 25 | return head; 26 | } 27 | }; -------------------------------------------------------------------------------- /0144-binary-tree-preorder-traversal/0144-binary-tree-preorder-traversal.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 | void preorder(TreeNode* r,vector &a){ 15 | if(!r) return; 16 | a.push_back(r->val); 17 | if(r->left) preorder(r->left,a); 18 | if(r->right) preorder(r->right,a); 19 | } 20 | vector preorderTraversal(TreeNode* root) { 21 | vector ans; 22 | preorder(root,ans); 23 | return ans; 24 | } 25 | }; -------------------------------------------------------------------------------- /0148-sort-list/0148-sort-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* sortList(ListNode* head) { 14 | if(!head) return NULL; 15 | ListNode* temp = head; 16 | vector arr; 17 | while(temp!=NULL){ 18 | arr.push_back(temp->val); 19 | temp = temp->next; 20 | } 21 | temp = head; 22 | sort(arr.begin(),arr.end()); 23 | for(int i=0;ival = arr[i]; 25 | temp = temp->next; 26 | } 27 | return head; 28 | } 29 | }; -------------------------------------------------------------------------------- /0094-binary-tree-inorder-traversal/0094-binary-tree-inorder-traversal.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 | void inorder(vector& a,TreeNode* r){ 15 | if(r==NULL) return; 16 | inorder(a,r->left); 17 | a.push_back(r->val); 18 | inorder(a,r->right); 19 | } 20 | vector inorderTraversal(TreeNode* root) { 21 | vector ans; 22 | if(!root) return ans; 23 | inorder(ans,root); 24 | return ans; 25 | } 26 | }; -------------------------------------------------------------------------------- /0145-binary-tree-postorder-traversal/0145-binary-tree-postorder-traversal.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 | void postorder(TreeNode* r,vector &a){ 15 | if(!r) return; 16 | if(r->left) postorder(r->left,a); 17 | if(r->right) postorder(r->right,a); 18 | a.push_back(r->val); 19 | } 20 | vector postorderTraversal(TreeNode* root) { 21 | vector ans; 22 | postorder(root,ans); 23 | return ans; 24 | } 25 | }; -------------------------------------------------------------------------------- /1710-maximum-units-on-a-truck/1710-maximum-units-on-a-truck.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maximumUnits(vector>& boxTypes, int truckSize) { 4 | priority_queue> pq; 5 | int c = 0; 6 | int u = 0; 7 | for(vector x : boxTypes){ 8 | pq.push({x[1],x[0]}); 9 | } 10 | while(!pq.empty() && cval != r->val) return false; 18 | return isMirror(l->left,r->right) && isMirror(l->right , r->left); 19 | } 20 | 21 | bool isSymmetric(TreeNode* root) { 22 | if(!root) return true; 23 | return isMirror(root->left , root->right); 24 | } 25 | }; -------------------------------------------------------------------------------- /0160-intersection-of-two-linked-lists/0160-intersection-of-two-linked-lists.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode(int x) : val(x), next(NULL) {} 7 | * }; 8 | */ 9 | class Solution { 10 | public: 11 | ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) { 12 | if(!headA && !headB){ 13 | return NULL; 14 | } 15 | ListNode* t1 = headA; 16 | ListNode* t2 = headB; 17 | 18 | while(t1!=t2){ 19 | if(t1==NULL){ 20 | t1 = headB; 21 | }else{ 22 | t1 = t1->next; 23 | } 24 | 25 | if(t2==NULL){ 26 | t2 = headA; 27 | }else{ 28 | t2 = t2->next; 29 | } 30 | } 31 | return t1; 32 | } 33 | }; -------------------------------------------------------------------------------- /2305-fair-distribution-of-cookies/2305-fair-distribution-of-cookies.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int dCookies(vector& cookies, vector& ch, int k, int c) { 4 | if (c == cookies.size()) { 5 | int unfairness = 0; 6 | for (int i : ch) { 7 | unfairness = max(unfairness, i); 8 | } 9 | return unfairness; 10 | } 11 | 12 | int minUnfairness = INT_MAX; 13 | for (int j = 0; j < k; j++) { 14 | ch[j] += cookies[c]; 15 | minUnfairness = min(minUnfairness, dCookies(cookies, ch, k, c + 1)); 16 | ch[j] -= cookies[c]; 17 | } 18 | return minUnfairness; 19 | } 20 | 21 | int distributeCookies(vector& cookies, int k) { 22 | vector children(k); 23 | return dCookies(cookies, children, k, 0); 24 | } 25 | }; 26 | -------------------------------------------------------------------------------- /0061-rotate-list/0061-rotate-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* rotateRight(ListNode* head, int k) { 14 | if(head==NULL || head->next==NULL || k==0) return head; 15 | 16 | ListNode* t = head; 17 | int len = 1; 18 | while(t->next){ 19 | len++; 20 | t = t->next; 21 | } 22 | 23 | t->next = head; 24 | k = k%len; 25 | k = len-k; 26 | 27 | while(k--) t = t->next; 28 | 29 | head = t->next; 30 | t->next = NULL; 31 | 32 | return head; 33 | } 34 | }; -------------------------------------------------------------------------------- /0784-insert-into-a-binary-search-tree/0784-insert-into-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() : 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* insertIntoBST(TreeNode* root, int val) { 15 | if(root==NULL) return new TreeNode(val); 16 | if(root->val < val){ 17 | root->right = insertIntoBST(root->right,val); 18 | } 19 | else if(root->val > val){ 20 | root->left = insertIntoBST(root->left,val); 21 | } 22 | return root; 23 | } 24 | }; -------------------------------------------------------------------------------- /0003-longest-substring-without-repeating-characters/0003-longest-substring-without-repeating-characters.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int lengthOfLongestSubstring(String str) { 3 | if(str.length()==0) 4 | return 0; 5 | int maxans = Integer.MIN_VALUE; 6 | Set < Character > set = new HashSet < > (); 7 | int l = 0; 8 | for (int r = 0; r < str.length(); r++) // outer loop for traversing the string 9 | { 10 | if (set.contains(str.charAt(r))) //if duplicate element is found 11 | { 12 | while (l < r && set.contains(str.charAt(r))) { 13 | set.remove(str.charAt(l)); 14 | l++; 15 | } 16 | } 17 | set.add(str.charAt(r)); 18 | maxans = Math.max(maxans, r - l + 1); 19 | } 20 | return maxans; 21 | } 22 | } -------------------------------------------------------------------------------- /1283-find-the-smallest-divisor-given-a-threshold/1283-find-the-smallest-divisor-given-a-threshold.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int divisor(vector n,int m){ 4 | int sum = 0; 5 | for(int i=0;i& nums, int threshold) { 15 | int l = 1; 16 | int r = *max_element(nums.begin(),nums.end()); 17 | int ans = -1; 18 | while(l <= r){ 19 | int mid = l+(r-l)/2; 20 | int div = divisor(nums,mid); 21 | if(div <= threshold){ 22 | ans = mid; 23 | r = mid-1; 24 | }else{ 25 | l = mid+1; 26 | } 27 | } 28 | return ans; 29 | } 30 | }; -------------------------------------------------------------------------------- /0142-linked-list-cycle-ii/0142-linked-list-cycle-ii.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * class ListNode { 4 | * int val; 5 | * ListNode next; 6 | * ListNode(int x) { 7 | * val = x; 8 | * next = null; 9 | * } 10 | * } 11 | */ 12 | public class Solution { 13 | public ListNode detectCycle(ListNode head) { 14 | if(head==null || head.next==null) return null; 15 | 16 | ListNode s = head; 17 | ListNode f = head; 18 | 19 | while(f.next!=null && f.next.next!=null){ 20 | s = s.next; 21 | f = f.next.next; 22 | 23 | if(s==f){ 24 | ListNode d = head; 25 | while(s!=d){ 26 | s = s.next; 27 | d = d.next; 28 | } 29 | return d; 30 | } 31 | } 32 | return null; 33 | } 34 | } -------------------------------------------------------------------------------- /0145-binary-tree-postorder-traversal/0145-binary-tree-postorder-traversal.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * public class TreeNode { 4 | * int val; 5 | * TreeNode left; 6 | * TreeNode right; 7 | * TreeNode() {} 8 | * TreeNode(int val) { this.val = val; } 9 | * TreeNode(int val, TreeNode left, TreeNode right) { 10 | * this.val = val; 11 | * this.left = left; 12 | * this.right = right; 13 | * } 14 | * } 15 | */ 16 | class Solution { 17 | public void postorder(TreeNode root,List ans){ 18 | if(root==null) return; 19 | postorder(root.left,ans); 20 | postorder(root.right,ans); 21 | ans.add(root.val); 22 | } 23 | public List postorderTraversal(TreeNode root) { 24 | List ans = new ArrayList<>(); 25 | postorder(root,ans); 26 | return ans; 27 | } 28 | } -------------------------------------------------------------------------------- /0213-house-robber-ii/0213-house-robber-ii.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int solve(vector &nums, int n, int i, vector &dp){ 4 | if(i>=n) return 0; 5 | if(dp[i]!=-1) return dp[i]; 6 | 7 | int inc = solve(nums,n,i+2,dp)+nums[i]; 8 | int dec = solve(nums,n,i+1,dp); 9 | dp[i] = max(inc,dec); 10 | return dp[i]; 11 | } 12 | 13 | int rob(vector& nums) { 14 | int n = nums.size(); 15 | if(n==1) return nums[0]; 16 | vector first,second; 17 | for(int i=0;i dp1(n,-1); 22 | vector dp2(n,-1); 23 | int ans1 = solve(first,first.size(),0,dp1); 24 | int ans2 = solve(second,second.size(),0,dp2); 25 | return max(ans1,ans2); 26 | } 27 | }; -------------------------------------------------------------------------------- /0131-palindrome-partitioning/0131-palindrome-partitioning.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isPalindrome(string s, int st , int end){ 4 | while(st <= end){ 5 | if(s[st++]!=s[end--]) return false; 6 | } 7 | return true; 8 | } 9 | 10 | void fun(string s , int idx , vector& path, vector>& ans){ 11 | if(s.length()==idx){ 12 | ans.push_back(path); 13 | return; 14 | } 15 | for(int i=idx;i> partition(string s) { 25 | vector> ans; 26 | vector path; 27 | fun(s,0,path,ans); 28 | return ans; 29 | } 30 | }; -------------------------------------------------------------------------------- /2216-delete-the-middle-node-of-a-linked-list/2216-delete-the-middle-node-of-a-linked-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* deleteMiddle(ListNode* head) { 14 | if(!head || !head->next) return nullptr; 15 | ListNode*slow = head; 16 | ListNode*fast = head->next->next; 17 | while(fast !=nullptr && fast->next != nullptr){ 18 | fast = fast->next->next; 19 | slow = slow->next; 20 | } 21 | ListNode*delnode = slow->next; 22 | slow->next =slow->next->next; 23 | delete delnode; 24 | return head; 25 | } 26 | }; -------------------------------------------------------------------------------- /0067-add-binary/README.md: -------------------------------------------------------------------------------- 1 |

67. Add Binary

Easy


Given two binary strings a and b, return their sum as a binary string.

2 | 3 |

 

4 |

Example 1:

5 |
Input: a = "11", b = "1"
 6 | Output: "100"
 7 | 

Example 2:

8 |
Input: a = "1010", b = "1011"
 9 | Output: "10101"
10 | 
11 |

 

12 |

Constraints:

13 | 14 |
    15 |
  • 1 <= a.length, b.length <= 104
  • 16 |
  • a and b consist only of '0' or '1' characters.
  • 17 |
  • Each string does not contain leading zeros except for the zero itself.
  • 18 |
19 | -------------------------------------------------------------------------------- /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 height(TreeNode* root){ 15 | if(!root) return 0; 16 | int l = height(root->left); 17 | if(l==-1) return -1; 18 | int r = height(root->right); 19 | if(r==-1) return -1; 20 | if(abs(l-r) > 1) return -1; 21 | return 1+max(l,r); 22 | } 23 | bool isBalanced(TreeNode* root) { 24 | int ans = height(root); 25 | if(ans==-1) return false; 26 | return true; 27 | } 28 | }; -------------------------------------------------------------------------------- /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 | void inorder(TreeNode* root , vector& arr){ 15 | if(root==NULL){ 16 | return; 17 | } 18 | if(root->left) inorder(root->left,arr); 19 | arr.push_back(root->val); 20 | if(root->right) inorder(root->right,arr); 21 | } 22 | int kthSmallest(TreeNode* root, int k) { 23 | vector arr; 24 | inorder(root,arr); 25 | return arr[k-1]; 26 | } 27 | }; -------------------------------------------------------------------------------- /2461-maximum-sum-of-distinct-subarrays-with-length-k/2461-maximum-sum-of-distinct-subarrays-with-length-k.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | long long maximumSubarraySum(vector& nums, int k) { 4 | int n = nums.size(); 5 | long long maxsum = 0; 6 | unordered_map freq; 7 | 8 | 9 | long long windowsum=0; 10 | for(int i=0;i=k){ 17 | windowsum -= nums[i-k]; 18 | freq[nums[i-k]]--; 19 | if(freq[nums[i-k]]==0){ 20 | freq.erase(nums[i-k]); 21 | } 22 | } 23 | 24 | // Maximum maxsum 25 | if(i >=k-1 && freq.size()==k){ 26 | maxsum = max(maxsum,windowsum); 27 | } 28 | } 29 | return maxsum; 30 | } 31 | }; -------------------------------------------------------------------------------- /0142-linked-list-cycle-ii/0142-linked-list-cycle-ii.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode(int x) : val(x), next(NULL) {} 7 | * }; 8 | */ 9 | class Solution { 10 | public: 11 | ListNode *detectCycle(ListNode *head) { 12 | if(!head){ 13 | return NULL; 14 | } 15 | 16 | ListNode* slow = head; 17 | ListNode* fast = head; 18 | ListNode* point = head; 19 | 20 | while(fast->next && fast->next->next){ 21 | slow = slow->next; 22 | fast = fast->next->next;; 23 | 24 | if(slow == fast){ 25 | while(point!=slow){ 26 | point=point->next; 27 | slow=slow->next; 28 | } 29 | return point; 30 | } 31 | 32 | } 33 | return NULL; 34 | } 35 | }; -------------------------------------------------------------------------------- /0124-binary-tree-maximum-path-sum/0124-binary-tree-maximum-path-sum.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 helper(TreeNode* root, int &a){ 15 | if(root==NULL) return 0; 16 | int l = max(0,helper(root->left,a)); 17 | int r = max(0,helper(root->right,a)); 18 | a = max(a, l+r+root->val); 19 | return max(l,r)+root->val; 20 | } 21 | int maxPathSum(TreeNode* root) { 22 | if(!root) return 0; 23 | int ans = INT_MIN; 24 | helper(root,ans); 25 | return ans; 26 | } 27 | }; -------------------------------------------------------------------------------- /2095-delete-the-middle-node-of-a-linked-list/2095-delete-the-middle-node-of-a-linked-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* deleteMiddle(ListNode* head) { 14 | if(head==NULL || head->next==NULL) return NULL; 15 | ListNode* prev = NULL; 16 | ListNode* slow = head; 17 | ListNode* fast = head; 18 | while(fast!=NULL && fast->next!=NULL){ 19 | prev = slow; 20 | slow = slow->next; 21 | fast = fast->next->next; 22 | } 23 | prev->next = slow->next; 24 | delete slow; 25 | return head; 26 | } 27 | }; -------------------------------------------------------------------------------- /0025-reverse-nodes-in-k-group/0025-reverse-nodes-in-k-group.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* reverseKGroup(ListNode* head, int k) { 14 | ListNode* t = head; 15 | vector arr; 16 | while(t!=NULL){ 17 | arr.push_back(t->val); 18 | t = t->next; 19 | } 20 | 21 | for(int i=0;i+k<=arr.size();i=i+k){ 22 | reverse(arr.begin()+i,arr.begin()+i+k); 23 | } 24 | 25 | t = head; 26 | for(int i=0;ival = arr[i]; 28 | t = t->next; 29 | } 30 | return head; 31 | } 32 | }; -------------------------------------------------------------------------------- /0063-unique-paths-ii/0063-unique-paths-ii.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int solve(vector> &og , int m , int n , int i, int j ,vector> &dp){ 4 | if(i==m-1 && j==n-1) return 1; 5 | 6 | if(i<0 || j<0 || i>=m || j>=n || og[i][j]==1) return 0; 7 | 8 | // int t = og[i][j]; 9 | //og[i][j] = 1; 10 | if(dp[i][j]!=-1) return dp[i][j]; 11 | dp[i][j] = solve(og,m,n,i+1,j,dp)+solve(og,m,n,i,j+1,dp); 12 | // og[i][j] = t; 13 | return dp[i][j]; 14 | } 15 | 16 | int uniquePathsWithObstacles(vector>& obstacleGrid) { 17 | int m = obstacleGrid.size(); 18 | int n = obstacleGrid[0].size(); 19 | if(obstacleGrid[m-1][n-1]==1 || obstacleGrid[0][0]==1) return 0; 20 | if(n==1 && m==1) return 1; 21 | 22 | vector> dp(m,vector(n,-1)); 23 | return solve(obstacleGrid ,m,n,0,0,dp); 24 | } 25 | }; -------------------------------------------------------------------------------- /0491-non-decreasing-subsequences/0491-non-decreasing-subsequences.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public void backtrack(int[] nums, int index, List>finalAnswer,Listans){ 3 | if(ans.size() >= 2) finalAnswer.add(new ArrayList<>(ans)); 4 | HashSet set = new HashSet<>(); 5 | for(int i=index;i> findSubsequences(int[] nums) { 15 | List>finalAnswer = new ArrayList<>(); 16 | Listans = new ArrayList<>(); 17 | backtrack(nums,0,finalAnswer,ans); 18 | return finalAnswer; 19 | } 20 | } -------------------------------------------------------------------------------- /0017-letter-combinations-of-a-phone-number/0017-letter-combinations-of-a-phone-number.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void solve(vector& ans, string op, int i, string mp[], string d){ 4 | // base case 5 | if(i >= d.length()){ 6 | ans.push_back(op); 7 | return; 8 | } 9 | 10 | int num = d[i]-'0'; 11 | string val = mp[num]; 12 | 13 | for(int j=0;j letterCombinations(string digits) { 21 | if(digits.length()==0) return {}; 22 | vector ans; 23 | string op; 24 | int i = 0; 25 | string mapping[10] = {"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"}; 26 | solve(ans,op,i,mapping,digits); 27 | return ans; 28 | } 29 | }; -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | void inorder(TreeNode* root , vector& arr){ 15 | if(root==NULL) return; 16 | inorder(root->left,arr); 17 | arr.push_back(root->val); 18 | inorder(root->right,arr); 19 | } 20 | 21 | bool isValidBST(TreeNode* root) { 22 | vector arr; 23 | inorder(root,arr); 24 | for(int i=0;i=arr[i+1]) return false; 26 | } 27 | return true; 28 | } 29 | }; -------------------------------------------------------------------------------- /0024-swap-nodes-in-pairs/0024-swap-nodes-in-pairs.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* swapPairs(ListNode* head) { 14 | if(head==NULL || head->next==NULL){ 15 | return head; 16 | } 17 | ListNode* temp = head; 18 | vector arr; 19 | while(temp!=NULL){ 20 | arr.push_back(temp->val); 21 | temp = temp->next; 22 | } 23 | 24 | for(int i=0;ival = arr[i]; 30 | temp = temp->next; 31 | } 32 | return head; 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* bst(vector& n , int l , int r){ 15 | if(l>r) return NULL; 16 | int m = (l+r)/2; 17 | TreeNode* newNode = new TreeNode(n[m]); 18 | newNode->left = bst(n,l,m-1); 19 | newNode->right = bst(n,m+1,r); 20 | return newNode; 21 | } 22 | TreeNode* sortedArrayToBST(vector& nums) { 23 | if(nums.size()==0) return NULL; 24 | return bst(nums,0,nums.size()-1); 25 | } 26 | }; -------------------------------------------------------------------------------- /0019-remove-nth-node-from-end-of-list/0019-remove-nth-node-from-end-of-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* removeNthFromEnd(ListNode* head, int n) { 14 | ListNode* dummy = new ListNode(); 15 | dummy->next = head; 16 | ListNode* s = dummy; 17 | ListNode* f = dummy; 18 | 19 | for(int i=0;inext; 21 | } 22 | 23 | while(f->next!=NULL){ 24 | f = f->next; 25 | s = s->next; 26 | } 27 | 28 | ListNode* delNode = s->next; 29 | s->next = s->next->next; 30 | delete delNode; 31 | return dummy->next; 32 | } 33 | }; -------------------------------------------------------------------------------- /1721-swapping-nodes-in-a-linked-list/1721-swapping-nodes-in-a-linked-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* swapNodes(ListNode* head, int k) { 14 | if(head==NULL || head->next==NULL){ 15 | return head; 16 | } 17 | ListNode* temp = head; 18 | vector arr; 19 | while(temp!=NULL){ 20 | arr.push_back(temp->val); 21 | temp = temp->next; 22 | } 23 | swap(arr[k-1],arr[arr.size()-k]); 24 | temp = head; 25 | for(int i=0;ival = arr[i]; 27 | temp = temp->next; 28 | } 29 | return head; 30 | } 31 | }; -------------------------------------------------------------------------------- /0118-pascals-triangle/README.md: -------------------------------------------------------------------------------- 1 |

118. Pascal's Triangle

Easy


Given an integer numRows, return the first numRows of Pascal's triangle.

2 | 3 |

In Pascal's triangle, each number is the sum of the two numbers directly above it as shown:

4 | 5 |

 

6 |

Example 1:

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

Example 2:

10 |
Input: numRows = 1
11 | Output: [[1]]
12 | 
13 |

 

14 |

Constraints:

15 | 16 |
    17 |
  • 1 <= numRows <= 30
  • 18 |
19 | -------------------------------------------------------------------------------- /0121-best-time-to-buy-and-sell-stock/0121-best-time-to-buy-and-sell-stock.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int maxProfit(int[] prices){ 3 | int len = prices.length; 4 | int maxProfit = 0; 5 | int minPrice = prices[0]; 6 | 7 | 8 | // for (int i = 0; i < len; i++) { 9 | // for (int j = i + 1; j < len; j++) { 10 | // int profit = prices[j] - prices[i]; 11 | // if (profit > maxProfit) { 12 | // maxProfit = profit; 13 | // } 14 | // } 15 | // } 16 | // return maxProfit; 17 | 18 | 19 | for(int i=0;i prices[i]){ 22 | minPrice = prices[i]; 23 | } 24 | 25 | int profit = prices[i] - minPrice; 26 | 27 | if(profit > maxProfit){ 28 | maxProfit = profit; 29 | } 30 | 31 | } 32 | return maxProfit; 33 | } 34 | } -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /0096-unique-binary-search-trees/README.md: -------------------------------------------------------------------------------- 1 |

96. 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 | -------------------------------------------------------------------------------- /0532-k-diff-pairs-in-an-array/0532-k-diff-pairs-in-an-array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findPairs(vector& nums, int k) { 4 | // if k is less than 0 5 | if(k < 0 ) return 0; 6 | 7 | unordered_map mp; 8 | // add all elements on map for record -> ele count 9 | for(auto ele : nums){ 10 | mp[ele]++; 11 | } 12 | 13 | int count = 0; 14 | 15 | // if k is 0 , then find those numbres whose frequency are greater than 1. Ex - 1-1 = 0 , and count of 1 is 2 times 16 | if(k==0){ 17 | for(auto it : mp){ 18 | if(it.second>1){ 19 | count ++; 20 | } 21 | } 22 | return count; 23 | } 24 | 25 | // arr[i] - arr[j] = k ==>>> arr[i] - k = arr[j] . 26 | for(auto it : mp){ 27 | if(mp.count(it.first-k)){ 28 | count ++; 29 | } 30 | } 31 | return count; 32 | } 33 | }; -------------------------------------------------------------------------------- /0203-remove-linked-list-elements/0203-remove-linked-list-elements.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* removeElements(ListNode* head, int val) { 14 | ListNode* dummy = new ListNode(0); 15 | dummy->next = head; 16 | 17 | ListNode* prev = dummy; 18 | ListNode* crnt = head; 19 | 20 | while(crnt!=NULL){ 21 | if(crnt->val == val){ 22 | prev->next = crnt->next; 23 | delete crnt; 24 | crnt = prev->next; 25 | }else{ 26 | crnt = crnt->next; 27 | prev = prev->next; 28 | } 29 | } 30 | 31 | return dummy->next; 32 | } 33 | }; -------------------------------------------------------------------------------- /0491-non-decreasing-subsequences/README.md: -------------------------------------------------------------------------------- 1 |

491. Non-decreasing Subsequences

Medium


Given an integer array nums, return all the different possible non-decreasing subsequences of the given array with at least two elements. You may return the answer in any order.

2 | 3 |

 

4 |

Example 1:

5 | 6 |
 7 | Input: nums = [4,6,7,7]
 8 | Output: [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
 9 | 
10 | 11 |

Example 2:

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

 

19 |

Constraints:

20 | 21 |
    22 |
  • 1 <= nums.length <= 15
  • 23 |
  • -100 <= nums[i] <= 100
  • 24 |
25 | -------------------------------------------------------------------------------- /0572-subtree-of-another-tree/0572-subtree-of-another-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 subtree(TreeNode* r, TreeNode* sr){ 15 | if(!r && !sr) return true; 16 | if(!r || !sr) return false; 17 | if(r->val != sr->val) return false; 18 | return subtree(r->left,sr->left) && subtree(r->right,sr->right); 19 | } 20 | 21 | bool isSubtree(TreeNode* root, TreeNode* subRoot) { 22 | if(!root) return false; 23 | if(subtree(root,subRoot)) return true; 24 | return isSubtree(root->left,subRoot) || isSubtree(root->right,subRoot); 25 | } 26 | }; -------------------------------------------------------------------------------- /0661-image-smoother/0661-image-smoother.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[][] imageSmoother(int[][] img) { 3 | int r = img.length; 4 | int c = img[0].length; 5 | 6 | int ans[][] = new int[r][c]; 7 | 8 | int neighbour[][] = { {-1,-1} , {-1,0},{-1,1}, 9 | {0,-1} , {0,0} , {0,1}, 10 | {1,-1} , {1,0} , {1,1} }; 11 | 12 | for(int i=0;i=0 && b>=0 && a s1,s2; 4 | public: 5 | MyQueue() { 6 | 7 | } 8 | 9 | void push(int x) { 10 | while(!s1.empty()){ 11 | s2.push(s1.top()); 12 | s1.pop(); 13 | } 14 | s1.push(x); 15 | while(!s2.empty()){ 16 | s1.push(s2.top()); 17 | s2.pop(); 18 | } 19 | 20 | } 21 | 22 | int pop() { 23 | int topele = s1.top(); 24 | s1.pop(); 25 | return topele; 26 | } 27 | 28 | int peek() { 29 | return s1.top(); 30 | } 31 | 32 | bool empty() { 33 | return s1.empty(); 34 | } 35 | }; 36 | 37 | /** 38 | * Your MyQueue object will be instantiated and called as such: 39 | * MyQueue* obj = new MyQueue(); 40 | * obj->push(x); 41 | * int param_2 = obj->pop(); 42 | * int param_3 = obj->peek(); 43 | * bool param_4 = obj->empty(); 44 | */ -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /0031-next-permutation/0031-next-permutation.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void nextPermutation(vector& A) { 4 | int n = A.size(); // size of the array. 5 | 6 | // Step 1: Find the break point: 7 | int ind = -1; // break point 8 | for (int i = n - 2; i >= 0; i--) { 9 | if (A[i] < A[i + 1]) { 10 | // index i is the break point 11 | ind = i; 12 | break; 13 | } 14 | } 15 | 16 | // If break point does not exist: 17 | if (ind == -1) { 18 | // reverse the whole array: 19 | reverse(A.begin(), A.end()); 20 | return; 21 | } 22 | 23 | // Step 2: Find the next greater element 24 | // and swap it with arr[ind]: 25 | 26 | for (int i = n - 1; i > ind; i--) { 27 | if (A[i] > A[ind]) { 28 | swap(A[i], A[ind]); 29 | break; 30 | } 31 | } 32 | 33 | // Step 3: reverse the right half: 34 | reverse(A.begin() + ind + 1, A.end()); 35 | } 36 | }; -------------------------------------------------------------------------------- /0485-max-consecutive-ones/README.md: -------------------------------------------------------------------------------- 1 |

485. Max Consecutive Ones

Easy


Given a binary array nums, return the maximum number of consecutive 1's in the array.

2 | 3 |

 

4 |

Example 1:

5 | 6 |
 7 | Input: nums = [1,1,0,1,1,1]
 8 | Output: 3
 9 | Explanation: The first two digits or the last three digits are consecutive 1s. The maximum number of consecutive 1s is 3.
10 | 
11 | 12 |

Example 2:

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

 

20 |

Constraints:

21 | 22 |
    23 |
  • 1 <= nums.length <= 105
  • 24 |
  • nums[i] is either 0 or 1.
  • 25 |
26 | -------------------------------------------------------------------------------- /0560-subarray-sum-equals-k/README.md: -------------------------------------------------------------------------------- 1 |

560. Subarray Sum Equals K

Medium


Given an array of integers nums and an integer k, return the total number of subarrays whose sum equals to k.

2 | 3 |

A subarray is a contiguous non-empty sequence of elements within an array.

4 | 5 |

 

6 |

Example 1:

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

Example 2:

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

 

14 |

Constraints:

15 | 16 |
    17 |
  • 1 <= nums.length <= 2 * 104
  • 18 |
  • -1000 <= nums[i] <= 1000
  • 19 |
  • -107 <= k <= 107
  • 20 |
21 | -------------------------------------------------------------------------------- /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* marge(TreeNode* r1, TreeNode* r2){ 15 | if(!r1 && !r2) return NULL; 16 | 17 | if(r1!=NULL && r2==NULL) return r1; 18 | 19 | if(r2!=NULL && r1==NULL) return r2; 20 | 21 | TreeNode* sum = new TreeNode(r1->val+r2->val); 22 | sum->left = marge(r1->left,r2->left); 23 | sum->right = marge(r1->right,r2->right); 24 | 25 | return sum; 26 | } 27 | 28 | TreeNode* mergeTrees(TreeNode* root1, TreeNode* root2) { 29 | return marge(root1,root2); 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 maxDepth(TreeNode* root) { 15 | int c = 0; 16 | if(!root) return c; 17 | queue q; 18 | q.push(root); 19 | while(!q.empty()){ 20 | c++; 21 | int n = q.size(); 22 | for(int i=0;ileft) q.push(curr->left); 27 | if(curr->right) q.push(curr->right); 28 | } 29 | } 30 | return c; 31 | } 32 | }; -------------------------------------------------------------------------------- /0112-path-sum/0112-path-sum.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 findSum(TreeNode* root , int sum , int targetSum){ 15 | if(!root) return false; 16 | sum += root->val; 17 | 18 | if(!root->left && !root->right){ 19 | return sum == targetSum; 20 | } 21 | 22 | int l = findSum(root->left,sum,targetSum); 23 | int r = findSum(root->right,sum,targetSum); 24 | return l || r; 25 | 26 | } 27 | bool hasPathSum(TreeNode* root, int targetSum) { 28 | int sum = 0; 29 | bool ans = findSum(root , sum , targetSum); 30 | return ans; 31 | } 32 | }; -------------------------------------------------------------------------------- /0081-search-in-rotated-sorted-array-ii/0081-search-in-rotated-sorted-array-ii.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool search(vector& nums, int target) { 4 | int l = 0; 5 | int r = nums.size()-1; 6 | while(l <= r){ 7 | int m = l+(r-l)/2; 8 | if(nums[m]==target){ 9 | return true; 10 | } 11 | if(nums[l] == nums[m] && nums[m]==nums[r]){ 12 | l++; 13 | r--; 14 | continue; 15 | } 16 | 17 | if(nums[l] <= nums[m]){ // left 18 | if(nums[l] <= target && target < nums[m]){ 19 | r = m-1; 20 | }else{ 21 | l = m+1; 22 | } 23 | }else{ // right 24 | if(nums[m] < target && target <= nums[r]){ 25 | l = m+1; 26 | }else{ 27 | r = m-1; 28 | } 29 | } 30 | } 31 | return false; 32 | } 33 | }; -------------------------------------------------------------------------------- /0283-move-zeroes/README.md: -------------------------------------------------------------------------------- 1 |

283. Move Zeroes

Easy


Given an integer array nums, move all 0's to the end of it while maintaining the relative order of the non-zero elements.

2 | 3 |

Note that you must do this in-place without making a copy of the array.

4 | 5 |

 

6 |

Example 1:

7 |
Input: nums = [0,1,0,3,12]
 8 | Output: [1,3,12,0,0]
 9 | 

Example 2:

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

 

14 |

Constraints:

15 | 16 |
    17 |
  • 1 <= nums.length <= 104
  • 18 |
  • -231 <= nums[i] <= 231 - 1
  • 19 |
20 | 21 |

 

22 | Follow up: Could you minimize the total number of operations done? -------------------------------------------------------------------------------- /0980-unique-paths-iii/0980-unique-paths-iii.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int path(int i,int j, int[][] g,int zc){ 3 | if(i<0 || j<0 || i>=g.length || j>=g[0].length || g[i][j]==-1) return 0; 4 | 5 | if(g[i][j] == 2 && zc==0) return 1; 6 | 7 | int temp = g[i][j]; 8 | g[i][j] = -1; 9 | int nzc = (temp==0) ? zc-1 : zc; 10 | int ans = path(i+1,j,g,nzc)+ path(i-1,j,g,nzc)+ path(i,j+1,g,nzc)+ path(i,j-1,g,nzc); 11 | g[i][j] = temp; 12 | return ans; 13 | } 14 | 15 | public int uniquePathsIII(int[][] grid) { 16 | int zc = 0; 17 | for(int i=0;i201. Bitwise AND of Numbers Range

Medium


Given two integers left and right that represent the range [left, right], return the bitwise AND of all numbers in this range, inclusive.

2 | 3 |

 

4 |

Example 1:

5 | 6 |
 7 | Input: left = 5, right = 7
 8 | Output: 4
 9 | 
10 | 11 |

Example 2:

12 | 13 |
14 | Input: left = 0, right = 0
15 | Output: 0
16 | 
17 | 18 |

Example 3:

19 | 20 |
21 | Input: left = 1, right = 2147483647
22 | Output: 0
23 | 
24 | 25 |

 

26 |

Constraints:

27 | 28 |
    29 |
  • 0 <= left <= right <= 231 - 1
  • 30 |
31 | -------------------------------------------------------------------------------- /0540-single-element-in-a-sorted-array/README.md: -------------------------------------------------------------------------------- 1 |

540. Single Element in a Sorted Array

Medium


You are given a sorted array consisting of only integers where every element appears exactly twice, except for one element which appears exactly once.

2 | 3 |

Return the single element that appears only once.

4 | 5 |

Your solution must run in O(log n) time and O(1) space.

6 | 7 |

 

8 |

Example 1:

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

Example 2:

12 |
Input: nums = [3,3,7,7,10,11,11]
13 | Output: 10
14 | 
15 |

 

16 |

Constraints:

17 | 18 |
    19 |
  • 1 <= nums.length <= 105
  • 20 |
  • 0 <= nums[i] <= 105
  • 21 |
22 | -------------------------------------------------------------------------------- /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 | vector ans; 16 | if(!root) return ans; 17 | queue q; 18 | q.push(root); 19 | while(!q.empty()){ 20 | int n = q.size(); 21 | for(int i=0;ival); 25 | if(t->left) q.push(t->left); 26 | if(t->right) q.push(t->right); 27 | } 28 | } 29 | return ans; 30 | } 31 | }; -------------------------------------------------------------------------------- /0070-climbing-stairs/README.md: -------------------------------------------------------------------------------- 1 |

70. Climbing Stairs

Easy


You are climbing a staircase. It takes n steps to reach the top.

2 | 3 |

Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?

4 | 5 |

 

6 |

Example 1:

7 | 8 |
 9 | Input: n = 2
10 | Output: 2
11 | Explanation: There are two ways to climb to the top.
12 | 1. 1 step + 1 step
13 | 2. 2 steps
14 | 
15 | 16 |

Example 2:

17 | 18 |
19 | Input: n = 3
20 | Output: 3
21 | Explanation: There are three ways to climb to the top.
22 | 1. 1 step + 1 step + 1 step
23 | 2. 1 step + 2 steps
24 | 3. 2 steps + 1 step
25 | 
26 | 27 |

 

28 |

Constraints:

29 | 30 |
    31 |
  • 1 <= n <= 45
  • 32 |
33 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /0046-permutations/README.md: -------------------------------------------------------------------------------- 1 |

46. 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 | -------------------------------------------------------------------------------- /0344-reverse-string/README.md: -------------------------------------------------------------------------------- 1 |

344. Reverse String

Easy


Write a function that reverses a string. The input string is given as an array of characters s.

2 | 3 |

You must do this by modifying the input array in-place with O(1) extra memory.

4 | 5 |

 

6 |

Example 1:

7 |
Input: s = ["h","e","l","l","o"]
 8 | Output: ["o","l","l","e","h"]
 9 | 

Example 2:

10 |
Input: s = ["H","a","n","n","a","h"]
11 | Output: ["h","a","n","n","a","H"]
12 | 
13 |

 

14 |

Constraints:

15 | 16 | 20 | -------------------------------------------------------------------------------- /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 | void dfs(TreeNode* root , vector &arr, string path){ 15 | path += to_string(root->val); 16 | 17 | if(root->left==NULL && root->right==NULL){ 18 | arr.push_back(path); 19 | } 20 | path += "->"; 21 | if(root->left) dfs(root->left,arr,path); 22 | if(root->right) dfs(root->right,arr,path); 23 | path.pop_back(); 24 | } 25 | 26 | vector binaryTreePaths(TreeNode* root) { 27 | vector ans; 28 | if(!root) return ans; 29 | string path=""; 30 | dfs(root,ans,path); 31 | return ans; 32 | } 33 | }; -------------------------------------------------------------------------------- /0875-koko-eating-bananas/0875-koko-eating-bananas.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | long long totalHour(vector p, int k){ 4 | int n = p.size(); 5 | long long sum = 0; 6 | for(int i=0;i p){ 16 | int max = p[0]; 17 | int n = p.size(); 18 | for(int i=0;i max){ 20 | max = p[i]; 21 | } 22 | } 23 | return max; 24 | } 25 | 26 | int minEatingSpeed(vector& piles, int h) { 27 | int l = 1; 28 | int r = maxele(piles); 29 | int k = 0; 30 | while(l <= r){ 31 | int m = l+(r-l)/2; 32 | long long thr = totalHour(piles,m); 33 | if(thr <= h){ 34 | k = m; 35 | r = m-1; 36 | }else{ 37 | l = m+1; 38 | } 39 | } 40 | return k; 41 | } 42 | }; -------------------------------------------------------------------------------- /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 | vector searchRange(vector& nums, int target) { 4 | int l = 0; 5 | int r = nums.size()-1; 6 | int st = -1; 7 | int end = -1; 8 | // for search first occ - 9 | while(l <= r){ 10 | int m = l+(r-l)/2; 11 | if(nums[m]==target){ 12 | st = m; 13 | r = m-1; 14 | }else if(nums[m] < target){ 15 | l = m+1; 16 | }else{ 17 | r = m-1; 18 | } 19 | } 20 | // reset value - 21 | l = 0; 22 | r = nums.size() - 1; 23 | // for search last occ - 24 | while(l <= r){ 25 | int m = l+(r-l)/2; 26 | if(nums[m]==target){ 27 | end = m; 28 | l = m+1; 29 | }else if(nums[m] < target){ 30 | l = m+1; 31 | }else{ 32 | r = m-1; 33 | } 34 | } 35 | return {st,end}; 36 | } 37 | }; -------------------------------------------------------------------------------- /0349-intersection-of-two-arrays/README.md: -------------------------------------------------------------------------------- 1 |

349. Intersection of Two Arrays

Easy


Given two integer arrays nums1 and nums2, return an array of their intersection. Each element in the result must be unique and you may return the result in any order.

2 | 3 |

 

4 |

Example 1:

5 | 6 |
 7 | Input: nums1 = [1,2,2,1], nums2 = [2,2]
 8 | Output: [2]
 9 | 
10 | 11 |

Example 2:

12 | 13 |
14 | Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
15 | Output: [9,4]
16 | Explanation: [4,9] is also accepted.
17 | 
18 | 19 |

 

20 |

Constraints:

21 | 22 |
    23 |
  • 1 <= nums1.length, nums2.length <= 1000
  • 24 |
  • 0 <= nums1[i], nums2[i] <= 1000
  • 25 |
26 | -------------------------------------------------------------------------------- /1008-construct-binary-search-tree-from-preorder-traversal/1008-construct-binary-search-tree-from-preorder-traversal.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* buildbst(TreeNode* &r , int ele){ 15 | if(!r) return r = new TreeNode(ele); 16 | if(r->val > ele){ 17 | r->left = buildbst(r->left,ele); 18 | } 19 | if(r->val < ele){ 20 | r->right = buildbst(r->right,ele); 21 | } 22 | return r; 23 | } 24 | TreeNode* bstFromPreorder(vector& preorder) { 25 | 26 | TreeNode* root = NULL; 27 | for(int i=0;i> &board,string word,int indx , int i,int j){ 4 | if(indx == word.size()){ 5 | return true; 6 | } 7 | 8 | if(i<0 || j<0 || i>=board.size() || j>=board[0].size() || board[i][j]!=word[indx]){ 9 | return false; 10 | } 11 | 12 | char temp = board[i][j]; 13 | board[i][j] = '#'; 14 | 15 | bool ans = isPresent(board,word,indx+1,i+1,j) || isPresent(board,word,indx+1,i-1,j) 16 | || isPresent(board,word,indx+1,i,j+1) || isPresent(board,word,indx+1,i,j-1); 17 | board[i][j] = temp; 18 | return ans; 19 | } 20 | 21 | bool exist(vector>& board, string word) { 22 | for(int i=0;i169. Majority Element

Easy


Given an array nums of size n, return the majority element.

2 | 3 |

The majority element is the element that appears more than ⌊n / 2⌋ times. You may assume that the majority element always exists in the array.

4 | 5 |

 

6 |

Example 1:

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

Example 2:

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

 

14 |

Constraints:

15 | 16 |
    17 |
  • n == nums.length
  • 18 |
  • 1 <= n <= 5 * 104
  • 19 |
  • -109 <= nums[i] <= 109
  • 20 |
21 | 22 |

 

23 | Follow-up: Could you solve the problem in linear time and in O(1) space? -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /0061-rotate-list/README.md: -------------------------------------------------------------------------------- 1 |

61. Rotate List

Medium


Given the head of a linked list, rotate the list to the right by k places.

2 | 3 |

 

4 |

Example 1:

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

Example 2:

12 | 13 |
14 | Input: head = [0,1,2], k = 4
15 | Output: [2,0,1]
16 | 
17 | 18 |

 

19 |

Constraints:

20 | 21 |
    22 |
  • The number of nodes in the list is in the range [0, 500].
  • 23 |
  • -100 <= Node.val <= 100
  • 24 |
  • 0 <= k <= 2 * 109
  • 25 |
26 | -------------------------------------------------------------------------------- /0102-binary-tree-level-order-traversal/0102-binary-tree-level-order-traversal.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> levelOrder(TreeNode* root) { 15 | vector> ans; 16 | if(!root) return ans; 17 | queue q; 18 | q.push(root); 19 | while(!q.empty()){ 20 | int n = q.size(); 21 | vector a; 22 | for(int i=0;ival); 26 | if(t->left) q.push(t->left); 27 | if(t->right) q.push(t->right); 28 | } 29 | ans.push_back(a); 30 | } 31 | return ans; 32 | } 33 | }; -------------------------------------------------------------------------------- /0328-odd-even-linked-list/0328-odd-even-linked-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* oddEvenList(ListNode* head) { 14 | if(head==NULL || head->next==NULL) return head; 15 | ListNode* temp = head; 16 | vector arr; 17 | while(temp!=NULL){ 18 | arr.push_back(temp->val); 19 | temp = temp->next; 20 | } 21 | vector ans; 22 | for(int i=0;ival = ans[i]; 31 | temp = temp->next; 32 | } 33 | return head; 34 | } 35 | }; -------------------------------------------------------------------------------- /0637-average-of-levels-in-binary-tree/0637-average-of-levels-in-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 | vector averageOfLevels(TreeNode* root) { 15 | vector ans; 16 | queue q; 17 | q.push(root); 18 | 19 | while(!q.empty()){ 20 | int n = q.size(); 21 | double sum = 0; 22 | for(int i=0;ileft != NULL) q.push(temp->left); 27 | if(temp->right != NULL) q.push(temp->right); 28 | 29 | sum+=temp->val; 30 | } 31 | ans.push_back(sum/n); 32 | } 33 | return ans; 34 | } 35 | }; -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /0200-number-of-islands/0200-number-of-islands.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void findIsland(int i,int j,vector>& grid){ 4 | if(i<0 || j<0 || i>=grid.size() || j>=grid[0].size() || grid[i][j]=='0'){ 5 | return; 6 | } 7 | 8 | grid[i][j] = '0'; 9 | 10 | findIsland(i+1,j,grid); 11 | findIsland(i-1,j,grid); 12 | findIsland(i,j+1,grid); 13 | findIsland(i,j-1,grid); 14 | 15 | return; 16 | } 17 | void func(vector>& grid){ 18 | for(int i=0;i>& grid) { 28 | int c = 0; 29 | for(int i=0;ival) root->left = deleteNode(root->left, key); 17 | else if (key > root->val) root->right = deleteNode(root->right, key); 18 | else { 19 | if (!root->right) return root->left; 20 | if (!root->left) return root->right; 21 | auto tmp = findMinNode(root->right); 22 | root->val = tmp->val; 23 | root->right = deleteNode(root->right, tmp->val); 24 | } 25 | return root; 26 | } 27 | 28 | private: 29 | TreeNode* findMinNode(TreeNode* root) { 30 | if (!root || !root->left) return root; 31 | return findMinNode(root->left); 32 | } 33 | }; -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2025 Ankan Ghorai 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /0116-populating-next-right-pointers-in-each-node/0116-populating-next-right-pointers-in-each-node.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | // Definition for a Node. 3 | class Node { 4 | public: 5 | int val; 6 | Node* left; 7 | Node* right; 8 | Node* next; 9 | 10 | Node() : val(0), left(NULL), right(NULL), next(NULL) {} 11 | 12 | Node(int _val) : val(_val), left(NULL), right(NULL), next(NULL) {} 13 | 14 | Node(int _val, Node* _left, Node* _right, Node* _next) 15 | : val(_val), left(_left), right(_right), next(_next) {} 16 | }; 17 | */ 18 | 19 | class Solution { 20 | public: 21 | Node* connect(Node* root) { 22 | if(!root) return NULL; 23 | queue q; 24 | q.push(root); 25 | while(!q.empty()){ 26 | int n = q.size(); 27 | Node* prev = NULL; 28 | for(int i=0;inext = t; 32 | prev=t; 33 | if(t->left) q.push(t->left); 34 | if(t->right) q.push(t->right); 35 | } 36 | } 37 | return root; 38 | } 39 | }; -------------------------------------------------------------------------------- /0079-word-search/0079-word-search.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean findWord(int i, int j, int index , char[][] b , String w){ 3 | // Goal - 4 | if(index == w.length()){ 5 | return true; 6 | } 7 | // Invalid Conditions - 8 | if(i<0 || j<0 || i>= b.length || j>=b[0].length || b[i][j]!= w.charAt(index) || b[i][j] =='$'){ 9 | return false; 10 | } 11 | 12 | char temp = b[i][j]; 13 | b[i][j] = '$'; 14 | // Explore all paths - 15 | boolean ans = findWord(i+1,j,index+1,b,w) || 16 | findWord(i-1,j,index+1,b,w) || 17 | findWord(i,j+1,index+1,b,w) || 18 | findWord(i,j-1,index+1,b,w); 19 | b[i][j] = temp; 20 | return ans; 21 | 22 | } 23 | 24 | public boolean exist(char[][] board, String word) { 25 | for(int i=0 ; i< board.length ; i++){ 26 | for(int j=0; j229. Majority Element II

Medium


Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times.

2 | 3 |

 

4 |

Example 1:

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

Example 2:

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

Example 3:

19 | 20 |
21 | Input: nums = [1,2]
22 | Output: [1,2]
23 | 
24 | 25 |

 

26 |

Constraints:

27 | 28 |
    29 |
  • 1 <= nums.length <= 5 * 104
  • 30 |
  • -109 <= nums[i] <= 109
  • 31 |
32 | 33 |

 

34 |

Follow up: Could you solve the problem in linear time and in O(1) space?

35 | -------------------------------------------------------------------------------- /0002-add-two-numbers/0002-add-two-numbers.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* addTwoNumbers(ListNode* l1, ListNode* l2) { 14 | // create a dummy node 15 | ListNode* dummy = new ListNode(); 16 | ListNode* temp = dummy; 17 | 18 | int carry = 0; 19 | while(l1!=NULL || l2!=NULL || carry){ 20 | int sum = 0; 21 | if(l1!=NULL){ 22 | sum+=l1->val; 23 | l1 = l1->next; 24 | } 25 | if(l2!=NULL){ 26 | sum+=l2->val; 27 | l2 = l2->next; 28 | } 29 | 30 | sum += carry; 31 | carry = sum/10; 32 | ListNode* newNode = new ListNode(sum%10); 33 | temp->next = newNode; 34 | temp = temp->next; 35 | } 36 | return dummy->next; 37 | } 38 | }; -------------------------------------------------------------------------------- /0021-merge-two-sorted-lists/0021-merge-two-sorted-lists.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* mergeTwoLists(ListNode* list1, ListNode* list2) { 14 | vector arr; 15 | ListNode* t1 = list1; 16 | ListNode* t2 = list2; 17 | while(t1!=NULL){ 18 | arr.push_back(t1->val); 19 | t1 = t1->next; 20 | } 21 | while(t2!=NULL){ 22 | arr.push_back(t2->val); 23 | t2 = t2->next; 24 | } 25 | sort(arr.begin(),arr.end()); 26 | 27 | if (arr.empty()) return nullptr; 28 | 29 | ListNode* newNode = new ListNode(arr[0]); 30 | ListNode* temp = newNode; 31 | for(int i=1;inext = new ListNode(arr[i]); 33 | temp = temp->next; 34 | } 35 | return newNode; 36 | } 37 | }; -------------------------------------------------------------------------------- /0111-minimum-depth-of-binary-tree/README.md: -------------------------------------------------------------------------------- 1 |

111. Minimum Depth of Binary Tree

Easy


Given a binary tree, find its minimum depth.

2 | 3 |

The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.

4 | 5 |

Note: A leaf is a node with no children.

6 | 7 |

 

8 |

Example 1:

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

Example 2:

16 | 17 |
18 | Input: root = [2,null,3,null,4,null,5,null,6]
19 | Output: 5
20 | 
21 | 22 |

 

23 |

Constraints:

24 | 25 |
    26 |
  • The number of nodes in the tree is in the range [0, 105].
  • 27 |
  • -1000 <= Node.val <= 1000
  • 28 |
29 | -------------------------------------------------------------------------------- /0014-longest-common-prefix/README.md: -------------------------------------------------------------------------------- 1 |

14. Longest Common Prefix

Easy


Write a function to find the longest common prefix string amongst an array of strings.

2 | 3 |

If there is no common prefix, return an empty string "".

4 | 5 |

 

6 |

Example 1:

7 | 8 |
 9 | Input: strs = ["flower","flow","flight"]
10 | Output: "fl"
11 | 
12 | 13 |

Example 2:

14 | 15 |
16 | Input: strs = ["dog","racecar","car"]
17 | Output: ""
18 | Explanation: There is no common prefix among the input strings.
19 | 
20 | 21 |

 

22 |

Constraints:

23 | 24 |
    25 |
  • 1 <= strs.length <= 200
  • 26 |
  • 0 <= strs[i].length <= 200
  • 27 |
  • strs[i] consists of only lowercase English letters if it is non-empty.
  • 28 |
29 | -------------------------------------------------------------------------------- /1011-capacity-to-ship-packages-within-d-days/1011-capacity-to-ship-packages-within-d-days.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findday(vector &w,int cap){ 4 | int day = 1; 5 | int load = 0; 6 | for(int i=0;i cap){ 8 | day++; 9 | load = w[i]; 10 | }else{ 11 | load +=w[i]; 12 | } 13 | } 14 | return day; 15 | } 16 | int sumallele(vector w){ 17 | int n = w.size(); 18 | int sum = 0; 19 | for(int i=0;i& w, int days) { 25 | int l = *max_element(w.begin(),w.end()); 26 | int r = sumallele(w); 27 | int ans = -1; 28 | while(l <= r){ 29 | int m = l+(r-l)/2; 30 | int noofdays = findday(w,m); 31 | if(noofdays <= days){ 32 | ans = m; 33 | r = m-1; 34 | }else{ 35 | l = m+1; 36 | } 37 | } 38 | return ans; 39 | } 40 | }; -------------------------------------------------------------------------------- /0007-reverse-integer/README.md: -------------------------------------------------------------------------------- 1 |

7. Reverse Integer

Medium


Given a signed 32-bit integer x, return x with its digits reversed. If reversing x causes the value to go outside the signed 32-bit integer range [-231, 231 - 1], then return 0.

2 | 3 |

Assume the environment does not allow you to store 64-bit integers (signed or unsigned).

4 | 5 |

 

6 |

Example 1:

7 | 8 |
 9 | Input: x = 123
10 | Output: 321
11 | 
12 | 13 |

Example 2:

14 | 15 |
16 | Input: x = -123
17 | Output: -321
18 | 
19 | 20 |

Example 3:

21 | 22 |
23 | Input: x = 120
24 | Output: 21
25 | 
26 | 27 |

 

28 |

Constraints:

29 | 30 |
    31 |
  • -231 <= x <= 231 - 1
  • 32 |
33 | -------------------------------------------------------------------------------- /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? -------------------------------------------------------------------------------- /0136-single-number/README.md: -------------------------------------------------------------------------------- 1 |

136. Single Number

Easy


Given a non-empty array of integers nums, every element appears twice except for one. Find that single one.

2 | 3 |

You must implement a solution with a linear runtime complexity and use only constant extra space.

4 | 5 |

 

6 |

Example 1:

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

Example 2:

10 |
Input: nums = [4,1,2,1,2]
11 | Output: 4
12 | 

Example 3:

13 |
Input: nums = [1]
14 | Output: 1
15 | 
16 |

 

17 |

Constraints:

18 | 19 |
    20 |
  • 1 <= nums.length <= 3 * 104
  • 21 |
  • -3 * 104 <= nums[i] <= 3 * 104
  • 22 |
  • Each element in the array appears twice except for one element which appears only once.
  • 23 |
24 | -------------------------------------------------------------------------------- /0013-roman-to-integer/0013-roman-to-integer.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int romanToInt(string s) { 4 | int ans = 0; 5 | int n = s.length(); 6 | for(int i=0;i< n;i++){ 7 | if(s[i]=='I'){ // 1 8 | if(i+1 < n && s[i+1] == 'V' || s[i+1]=='X'){ 9 | ans -= 1; 10 | }else{ 11 | ans += 1; 12 | } 13 | }if(s[i]=='V'){ // 5 14 | ans += 5; 15 | }if(s[i]=='X'){ // 10 16 | if(i+1 < n && s[i+1]=='L' || s[i+1]=='C'){ 17 | ans -= 10; 18 | }else{ 19 | ans += 10; 20 | } 21 | }if(s[i]=='L'){ // 50 22 | ans += 50; 23 | }if(s[i]=='C'){ //100 24 | if(i+1 < n && s[i+1]=='D' || s[i+1]=='M'){ 25 | ans -= 100; 26 | }else{ 27 | ans += 100; 28 | } 29 | }if(s[i]=='D'){ 30 | ans += 500; 31 | }if(s[i]=='M'){ 32 | ans += 1000; 33 | } 34 | } 35 | return ans; 36 | } 37 | }; -------------------------------------------------------------------------------- /0054-spiral-matrix/0054-spiral-matrix.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector spiralOrder(vector>& matrix) { 4 | int rowStart = 0 , colStart = 0; 5 | int rowEnd = matrix.size()-1; 6 | int colEnd = matrix[0].size()-1; 7 | 8 | vector ans; 9 | 10 | while(rowStart <= rowEnd && colStart <= colEnd){ 11 | 12 | for(int i=colStart ; i<=colEnd ; i++){ 13 | ans.push_back(matrix[rowStart][i]); 14 | } 15 | rowStart++; 16 | for(int i = rowStart ; i <= rowEnd ; i++){ 17 | ans.push_back(matrix[i][colEnd]); 18 | } 19 | colEnd--; 20 | if(rowStart <= rowEnd){ 21 | for(int i=colEnd ; i >= colStart ; i--){ 22 | ans.push_back(matrix[rowEnd][i]); 23 | } 24 | } 25 | rowEnd--; 26 | if(colStart <= colEnd){ 27 | for(int i=rowEnd ; i>= rowStart ; i-- ){ 28 | ans.push_back(matrix[i][colStart]); 29 | } 30 | } 31 | colStart++; 32 | } 33 | 34 | return ans; 35 | } 36 | }; -------------------------------------------------------------------------------- /0107-binary-tree-level-order-traversal-ii/0107-binary-tree-level-order-traversal-ii.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> levelOrderBottom(TreeNode* root) { 15 | vector> res; 16 | if(!root) return res; 17 | 18 | queue q; 19 | q.push(root); 20 | while(!q.empty()){ 21 | int lvs = q.size(); 22 | vector lv; 23 | 24 | for(int i=0;ival); 28 | if(node->left) q.push(node->left); 29 | if(node->right) q.push(node->right); 30 | } 31 | res.insert(res.begin(),lv); 32 | } 33 | return res; 34 | } 35 | }; -------------------------------------------------------------------------------- /0448-find-all-numbers-disappeared-in-an-array/README.md: -------------------------------------------------------------------------------- 1 |

448. Find All Numbers Disappeared in an Array

Easy


Given an array nums of n integers where nums[i] is in the range [1, n], return an array of all the integers in the range [1, n] that do not appear in nums.

2 | 3 |

 

4 |

Example 1:

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

Example 2:

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

 

12 |

Constraints:

13 | 14 |
    15 |
  • n == nums.length
  • 16 |
  • 1 <= n <= 105
  • 17 |
  • 1 <= nums[i] <= n
  • 18 |
19 | 20 |

 

21 |

Follow up: Could you do it without extra space and in O(n) runtime? You may assume the returned list does not count as extra space.

22 | -------------------------------------------------------------------------------- /0054-spiral-matrix/README.md: -------------------------------------------------------------------------------- 1 |

54. Spiral Matrix

Medium


Given an m x n matrix, return all elements of the matrix in spiral order.

2 | 3 |

 

4 |

Example 1:

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

Example 2:

12 | 13 |
14 | Input: matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]
15 | Output: [1,2,3,4,8,12,11,10,9,5,6,7]
16 | 
17 | 18 |

 

19 |

Constraints:

20 | 21 |
    22 |
  • m == matrix.length
  • 23 |
  • n == matrix[i].length
  • 24 |
  • 1 <= m, n <= 10
  • 25 |
  • -100 <= matrix[i][j] <= 100
  • 26 |
27 | -------------------------------------------------------------------------------- /0069-sqrtx/README.md: -------------------------------------------------------------------------------- 1 |

69. Sqrt(x)

Easy


Given a non-negative integer x, return the square root of x rounded down to the nearest integer. The returned integer should be non-negative as well.

2 | 3 |

You must not use any built-in exponent function or operator.

4 | 5 |
    6 |
  • For example, do not use pow(x, 0.5) in c++ or x ** 0.5 in python.
  • 7 |
8 | 9 |

 

10 |

Example 1:

11 | 12 |
13 | Input: x = 4
14 | Output: 2
15 | Explanation: The square root of 4 is 2, so we return 2.
16 | 
17 | 18 |

Example 2:

19 | 20 |
21 | Input: x = 8
22 | Output: 2
23 | Explanation: The square root of 8 is 2.82842..., and since we round it down to the nearest integer, 2 is returned.
24 | 
25 | 26 |

 

27 |

Constraints:

28 | 29 |
    30 |
  • 0 <= x <= 231 - 1
  • 31 |
32 | -------------------------------------------------------------------------------- /0102-binary-tree-level-order-traversal/README.md: -------------------------------------------------------------------------------- 1 |

102. Binary Tree Level Order Traversal

Medium


Given the root of a binary tree, return the level order traversal of its nodes' values. (i.e., from left to right, level by level).

2 | 3 |

 

4 |

Example 1:

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

Example 2:

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

Example 3:

19 | 20 |
21 | Input: root = []
22 | Output: []
23 | 
24 | 25 |

 

26 |

Constraints:

27 | 28 |
    29 |
  • The number of nodes in the tree is in the range [0, 2000].
  • 30 |
  • -1000 <= Node.val <= 1000
  • 31 |
32 | -------------------------------------------------------------------------------- /0226-invert-binary-tree/README.md: -------------------------------------------------------------------------------- 1 |

226. Invert Binary Tree

Easy


Given the root of a binary tree, invert the tree, and return its root.

2 | 3 |

 

4 |

Example 1:

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

Example 2:

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

Example 3:

19 | 20 |
21 | Input: root = []
22 | Output: []
23 | 
24 | 25 |

 

26 |

Constraints:

27 | 28 |
    29 |
  • The number of nodes in the tree is in the range [0, 100].
  • 30 |
  • -100 <= Node.val <= 100
  • 31 |
32 | -------------------------------------------------------------------------------- /0796-rotate-string/README.md: -------------------------------------------------------------------------------- 1 |

796. Rotate String

Easy


Given two strings s and goal, return true if and only if s can become goal after some number of shifts on s.

2 | 3 |

A shift on s consists of moving the leftmost character of s to the rightmost position.

4 | 5 |
    6 |
  • For example, if s = "abcde", then it will be "bcdea" after one shift.
  • 7 |
8 | 9 |

 

10 |

Example 1:

11 |
Input: s = "abcde", goal = "cdeab"
12 | Output: true
13 | 

Example 2:

14 |
Input: s = "abcde", goal = "abced"
15 | Output: false
16 | 
17 |

 

18 |

Constraints:

19 | 20 |
    21 |
  • 1 <= s.length, goal.length <= 100
  • 22 |
  • s and goal consist of lowercase English letters.
  • 23 |
24 | -------------------------------------------------------------------------------- /0042-trapping-rain-water/README.md: -------------------------------------------------------------------------------- 1 |

42. Trapping Rain Water

Hard


Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it can trap after raining.

2 | 3 |

 

4 |

Example 1:

5 | 6 |
 7 | Input: height = [0,1,0,2,1,0,1,3,2,1,2,1]
 8 | Output: 6
 9 | Explanation: The above elevation map (black section) is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped.
10 | 
11 | 12 |

Example 2:

13 | 14 |
15 | Input: height = [4,2,0,3,2,5]
16 | Output: 9
17 | 
18 | 19 |

 

20 |

Constraints:

21 | 22 |
    23 |
  • n == height.length
  • 24 |
  • 1 <= n <= 2 * 104
  • 25 |
  • 0 <= height[i] <= 105
  • 26 |
27 | -------------------------------------------------------------------------------- /0231-power-of-two/README.md: -------------------------------------------------------------------------------- 1 |

231. Power of Two

Easy


Given an integer n, return true if it is a power of two. Otherwise, return false.

2 | 3 |

An integer n is a power of two, if there exists an integer x such that n == 2x.

4 | 5 |

 

6 |

Example 1:

7 | 8 |
 9 | Input: n = 1
10 | Output: true
11 | Explanation: 20 = 1
12 | 
13 | 14 |

Example 2:

15 | 16 |
17 | Input: n = 16
18 | Output: true
19 | Explanation: 24 = 16
20 | 
21 | 22 |

Example 3:

23 | 24 |
25 | Input: n = 3
26 | Output: false
27 | 
28 | 29 |

 

30 |

Constraints:

31 | 32 |
    33 |
  • -231 <= n <= 231 - 1
  • 34 |
35 | 36 |

 

37 | Follow up: Could you solve it without loops/recursion? -------------------------------------------------------------------------------- /0645-set-mismatch/README.md: -------------------------------------------------------------------------------- 1 |

645. Set Mismatch

Easy


You have a set of integers s, which originally contains all the numbers from 1 to n. Unfortunately, due to some error, one of the numbers in s got duplicated to another number in the set, which results in repetition of one number and loss of another number.

2 | 3 |

You are given an integer array nums representing the data status of this set after the error.

4 | 5 |

Find the number that occurs twice and the number that is missing and return them in the form of an array.

6 | 7 |

 

8 |

Example 1:

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

Example 2:

12 |
Input: nums = [1,1]
13 | Output: [1,2]
14 | 
15 |

 

16 |

Constraints:

17 | 18 |
    19 |
  • 2 <= nums.length <= 104
  • 20 |
  • 1 <= nums[i] <= 104
  • 21 |
22 | -------------------------------------------------------------------------------- /0107-binary-tree-level-order-traversal-ii/README.md: -------------------------------------------------------------------------------- 1 |

107. Binary Tree Level Order Traversal II

Medium


Given the root of a binary tree, return the bottom-up level order traversal of its nodes' values. (i.e., from left to right, level by level from leaf to root).

2 | 3 |

 

4 |

Example 1:

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

Example 2:

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

Example 3:

19 | 20 |
21 | Input: root = []
22 | Output: []
23 | 
24 | 25 |

 

26 |

Constraints:

27 | 28 |
    29 |
  • The number of nodes in the tree is in the range [0, 2000].
  • 30 |
  • -1000 <= Node.val <= 1000
  • 31 |
32 | -------------------------------------------------------------------------------- /0083-remove-duplicates-from-sorted-list/README.md: -------------------------------------------------------------------------------- 1 |

83. Remove Duplicates from Sorted List

Easy


Given the head of a sorted linked list, delete all duplicates such that each element appears only once. Return the linked list sorted as well.

2 | 3 |

 

4 |

Example 1:

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

Example 2:

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

 

19 |

Constraints:

20 | 21 |
    22 |
  • The number of nodes in the list is in the range [0, 300].
  • 23 |
  • -100 <= Node.val <= 100
  • 24 |
  • The list is guaranteed to be sorted in ascending order.
  • 25 |
26 | -------------------------------------------------------------------------------- /0662-maximum-width-of-binary-tree/0662-maximum-width-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 widthOfBinaryTree(TreeNode* root) { 15 | long long ans = 0; 16 | if(!root) return 0; 17 | queue> q; 18 | q.push({root,0}); 19 | while(!q.empty()){ 20 | int n = q.size(); 21 | long long l = q.front().second; 22 | long long r = q.back().second; 23 | ans = max(ans,r-l+1); 24 | for(int i=0;ileft) q.push({t->left, 2*idx+1}); 30 | if(t->right) q.push({t->right, 2*idx+2}); 31 | } 32 | } 33 | return ans; 34 | } 35 | }; -------------------------------------------------------------------------------- /0347-top-k-frequent-elements/README.md: -------------------------------------------------------------------------------- 1 |

347. Top K Frequent Elements

Medium


Given an integer array nums and an integer k, return the k most frequent elements. You may return the answer in any order.

2 | 3 |

 

4 |

Example 1:

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

Example 2:

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

 

12 |

Constraints:

13 | 14 |
    15 |
  • 1 <= nums.length <= 105
  • 16 |
  • -104 <= nums[i] <= 104
  • 17 |
  • k is in the range [1, the number of unique elements in the array].
  • 18 |
  • It is guaranteed that the answer is unique.
  • 19 |
20 | 21 |

 

22 |

Follow up: Your algorithm's time complexity must be better than O(n log n), where n is the array's size.

23 | -------------------------------------------------------------------------------- /0056-merge-intervals/README.md: -------------------------------------------------------------------------------- 1 |

56. Merge Intervals

Medium


Given an array of intervals where intervals[i] = [starti, endi], merge all overlapping intervals, and return an array of the non-overlapping intervals that cover all the intervals in the input.

2 | 3 |

 

4 |

Example 1:

5 | 6 |
 7 | Input: intervals = [[1,3],[2,6],[8,10],[15,18]]
 8 | Output: [[1,6],[8,10],[15,18]]
 9 | Explanation: Since intervals [1,3] and [2,6] overlap, merge them into [1,6].
10 | 
11 | 12 |

Example 2:

13 | 14 |
15 | Input: intervals = [[1,4],[4,5]]
16 | Output: [[1,5]]
17 | Explanation: Intervals [1,4] and [4,5] are considered overlapping.
18 | 
19 | 20 |

 

21 |

Constraints:

22 | 23 |
    24 |
  • 1 <= intervals.length <= 104
  • 25 |
  • intervals[i].length == 2
  • 26 |
  • 0 <= starti <= endi <= 104
  • 27 |
28 | -------------------------------------------------------------------------------- /0128-longest-consecutive-sequence/README.md: -------------------------------------------------------------------------------- 1 |

128. Longest Consecutive Sequence

Medium


Given an unsorted array of integers nums, return the length of the longest consecutive elements sequence.

2 | 3 |

You must write an algorithm that runs in O(n) time.

4 | 5 |

 

6 |

Example 1:

7 | 8 |
 9 | Input: nums = [100,4,200,1,3,2]
10 | Output: 4
11 | Explanation: The longest consecutive elements sequence is [1, 2, 3, 4]. Therefore its length is 4.
12 | 
13 | 14 |

Example 2:

15 | 16 |
17 | Input: nums = [0,3,7,2,5,8,4,6,0,1]
18 | Output: 9
19 | 
20 | 21 |

Example 3:

22 | 23 |
24 | Input: nums = [1,0,1,2]
25 | Output: 3
26 | 
27 | 28 |

 

29 |

Constraints:

30 | 31 |
    32 |
  • 0 <= nums.length <= 105
  • 33 |
  • -109 <= nums[i] <= 109
  • 34 |
35 | -------------------------------------------------------------------------------- /0103-binary-tree-zigzag-level-order-traversal/README.md: -------------------------------------------------------------------------------- 1 |

103. Binary Tree Zigzag Level Order Traversal

Medium


Given the root of a binary tree, return the zigzag level order traversal of its nodes' values. (i.e., from left to right, then right to left for the next level and alternate between).

2 | 3 |

 

4 |

Example 1:

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

Example 2:

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

Example 3:

19 | 20 |
21 | Input: root = []
22 | Output: []
23 | 
24 | 25 |

 

26 |

Constraints:

27 | 28 |
    29 |
  • The number of nodes in the tree is in the range [0, 2000].
  • 30 |
  • -100 <= Node.val <= 100
  • 31 |
32 | -------------------------------------------------------------------------------- /0110-balanced-binary-tree/README.md: -------------------------------------------------------------------------------- 1 |

110. Balanced Binary Tree

Easy


Given a binary tree, determine if it is height-balanced.

2 | 3 |

 

4 |

Example 1:

5 | 6 |
 7 | Input: root = [3,9,20,null,null,15,7]
 8 | Output: true
 9 | 
10 | 11 |

Example 2:

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

Example 3:

19 | 20 |
21 | Input: root = []
22 | Output: true
23 | 
24 | 25 |

 

26 |

Constraints:

27 | 28 |
    29 |
  • The number of nodes in the tree is in the range [0, 5000].
  • 30 |
  • -104 <= Node.val <= 104
  • 31 |
32 | -------------------------------------------------------------------------------- /0138-copy-list-with-random-pointer/0138-copy-list-with-random-pointer.java: -------------------------------------------------------------------------------- 1 | /* 2 | // Definition for a Node. 3 | class Node { 4 | int val; 5 | Node next; 6 | Node random; 7 | 8 | public Node(int val) { 9 | this.val = val; 10 | this.next = null; 11 | this.random = null; 12 | } 13 | } 14 | */ 15 | 16 | class Solution { 17 | public Node copyRandomList(Node head) { 18 | if (head == null) return null; 19 | 20 | Node curr = head; // Create new list w/ same values 21 | while (curr != null) { 22 | Node newNode = new Node(curr.val); 23 | newNode.next = curr.next; 24 | curr.next = newNode; 25 | curr = newNode.next; 26 | } 27 | 28 | curr = head; // Copy the random pointers 29 | while (curr != null) { 30 | if (curr.random != null) 31 | curr.next.random = curr.random.next; 32 | curr = curr.next.next; 33 | } 34 | 35 | curr = head; // Separate the two lists 36 | Node newHead = head.next; 37 | Node newCurr = newHead; 38 | while (curr != null) { 39 | curr.next = newCurr.next; 40 | curr = curr.next; 41 | if (curr != null) { 42 | newCurr.next = curr.next; 43 | newCurr = newCurr.next; 44 | } 45 | } 46 | 47 | return newHead; 48 | 49 | } 50 | } -------------------------------------------------------------------------------- /0347-top-k-frequent-elements/0347-top-k-frequent-elements.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector topKFrequent(vector& nums, int k) { 4 | vector ans; 5 | 6 | // Step 1: Count the frequency of each element in the array 7 | unordered_map freqMap; 8 | for (int num : nums) { 9 | freqMap[num]++; 10 | } 11 | 12 | // Step 2: Use a min-heap (priority queue) to store the k most frequent elements 13 | priority_queue, vector>, greater>> minHeap; 14 | 15 | // Step 3: Insert elements into the heap 16 | for (auto& entry : freqMap) { 17 | minHeap.push({entry.second, entry.first}); // Push {frequency, element} 18 | 19 | // If the heap size exceeds k, pop the element with the smallest frequency 20 | if (minHeap.size() > k) { 21 | minHeap.pop(); 22 | } 23 | } 24 | 25 | // Step 4: Extract the top k elements from the heap 26 | while (!minHeap.empty()) { 27 | ans.push_back(minHeap.top().second); // Push the element (not frequency) 28 | minHeap.pop(); 29 | } 30 | 31 | return ans; 32 | } 33 | }; --------------------------------------------------------------------------------