├── 0945-snakes-and-ladders ├── 0945-snakes-and-ladders.cpp └── README.md ├── 1007-minimum-domino-rotations-for-equal-row ├── 1007-minimum-domino-rotations-for-equal-row.cpp ├── NOTES.md └── README.md ├── 1008-construct-binary-search-tree-from-preorder-traversal ├── 1008-construct-binary-search-tree-from-preorder-traversal.cpp ├── NOTES.md └── README.md ├── 1010-pairs-of-songs-with-total-durations-divisible-by-60 ├── NOTES.md └── README.md ├── 1013-partition-array-into-three-parts-with-equal-sum ├── 1013-partition-array-into-three-parts-with-equal-sum.cpp ├── NOTES.md └── README.md ├── 102-binary-tree-level-order-traversal ├── 102-binary-tree-level-order-traversal.cpp ├── NOTES.md └── README.md ├── 1029-two-city-scheduling ├── 1029-two-city-scheduling.cpp ├── NOTES.md └── README.md ├── 1046-last-stone-weight ├── 1046-last-stone-weight.cpp ├── NOTES.md └── README.md ├── 108-convert-sorted-array-to-binary-search-tree ├── 108-convert-sorted-array-to-binary-search-tree.cpp ├── NOTES.md └── README.md ├── 109-convert-sorted-list-to-binary-search-tree ├── 109-convert-sorted-list-to-binary-search-tree.cpp ├── NOTES.md └── README.md ├── 1091-shortest-path-in-binary-matrix ├── 1091-shortest-path-in-binary-matrix.cpp ├── NOTES.md └── README.md ├── 1094-car-pooling ├── 1094-car-pooling.cpp ├── NOTES.md └── README.md ├── 11-container-with-most-water ├── 11-container-with-most-water.cpp ├── NOTES.md └── README.md ├── 110-balanced-binary-tree ├── 110-balanced-binary-tree.cpp ├── NOTES.md └── README.md ├── 1131-maximum-of-absolute-value-expression ├── 1131-maximum-of-absolute-value-expression.cpp ├── NOTES.md └── README.md ├── 1143-longest-common-subsequence ├── 1143-longest-common-subsequence.cpp ├── NOTES.md └── README.md ├── 1146-snapshot-array ├── 1146-snapshot-array.cpp ├── NOTES.md └── README.md ├── 115-distinct-subsequences ├── 115-distinct-subsequences.cpp ├── NOTES.md └── README.md ├── 116-populating-next-right-pointers-in-each-node ├── NOTES.md └── README.md ├── 117-populating-next-right-pointers-in-each-node-ii ├── 117-populating-next-right-pointers-in-each-node-ii.cpp ├── NOTES.md └── README.md ├── 119-pascals-triangle-ii ├── 119-pascals-triangle-ii.cpp ├── NOTES.md └── README.md ├── 1192-critical-connections-in-a-network ├── 1192-critical-connections-in-a-network.cpp ├── NOTES.md └── README.md ├── 1202-smallest-string-with-swaps ├── 1202-smallest-string-with-swaps.cpp ├── NOTES.md └── README.md ├── 1207-unique-number-of-occurrences ├── 1207-unique-number-of-occurrences.cpp ├── NOTES.md └── README.md ├── 1209-remove-all-adjacent-duplicates-in-string-ii ├── 1209-remove-all-adjacent-duplicates-in-string-ii.cpp ├── NOTES.md └── README.md ├── 121-best-time-to-buy-and-sell-stock ├── 121-best-time-to-buy-and-sell-stock.cpp ├── NOTES.md └── README.md ├── 1249-minimum-remove-to-make-valid-parentheses ├── 1249-minimum-remove-to-make-valid-parentheses.cpp ├── NOTES.md └── README.md ├── 125-valid-palindrome ├── 125-valid-palindrome.cpp ├── NOTES.md └── README.md ├── 1260-shift-2d-grid ├── 1260-shift-2d-grid.cpp ├── NOTES.md └── README.md ├── 1266-minimum-time-visiting-all-points ├── 1266-minimum-time-visiting-all-points.cpp ├── NOTES.md └── README.md ├── 1275-find-winner-on-a-tic-tac-toe-game ├── NOTES.md └── README.md ├── 128-longest-consecutive-sequence ├── 128-longest-consecutive-sequence.cpp ├── NOTES.md └── README.md ├── 130-surrounded-regions ├── 130-surrounded-regions.cpp ├── NOTES.md └── README.md ├── 1326-minimum-number-of-taps-to-open-to-water-a-garden ├── 1326-minimum-number-of-taps-to-open-to-water-a-garden.cpp └── README.md ├── 133-clone-graph ├── NOTES.md └── README.md ├── 1331-rank-transform-of-an-array ├── 1331-rank-transform-of-an-array.cpp ├── NOTES.md └── README.md ├── 1337-the-k-weakest-rows-in-a-matrix ├── 1337-the-k-weakest-rows-in-a-matrix.cpp ├── NOTES.md └── README.md ├── 1342-number-of-steps-to-reduce-a-number-to-zero ├── 1342-number-of-steps-to-reduce-a-number-to-zero.cpp ├── NOTES.md └── README.md ├── 135-candy ├── 135-candy.cpp ├── NOTES.md └── README.md ├── 1379-find-a-corresponding-node-of-a-binary-tree-in-a-clone-of-that-tree ├── 1379-find-a-corresponding-node-of-a-binary-tree-in-a-clone-of-that-tree.cpp ├── NOTES.md └── README.md ├── 1396-design-underground-system ├── 1396-design-underground-system.cpp ├── NOTES.md └── README.md ├── 14-longest-common-prefix ├── 14-longest-common-prefix.cpp ├── NOTES.md └── README.md ├── 1423-maximum-points-you-can-obtain-from-cards ├── 1423-maximum-points-you-can-obtain-from-cards.cpp ├── NOTES.md └── README.md ├── 1424-diagonal-traverse-ii ├── 1424-diagonal-traverse-ii.cpp ├── NOTES.md └── README.md ├── 1461-check-if-a-string-contains-all-binary-codes-of-size-k ├── 1461-check-if-a-string-contains-all-binary-codes-of-size-k.cpp ├── NOTES.md └── README.md ├── 1465-maximum-area-of-a-piece-of-cake-after-horizontal-and-vertical-cuts ├── 1465-maximum-area-of-a-piece-of-cake-after-horizontal-and-vertical-cuts.cpp ├── NOTES.md └── README.md ├── 1473-paint-house-iii ├── 1473-paint-house-iii.cpp ├── NOTES.md └── README.md ├── 1477-find-two-non-overlapping-sub-arrays-each-with-target-sum ├── 1477-find-two-non-overlapping-sub-arrays-each-with-target-sum.cpp ├── NOTES.md └── README.md ├── 1480-running-sum-of-1d-array ├── 1480-running-sum-of-1d-array.cpp └── README.md ├── 1482-minimum-number-of-days-to-make-m-bouquets └── README.md ├── 15-3sum ├── 15-3sum.cpp ├── NOTES.md └── README.md ├── 151-reverse-words-in-a-string ├── 151-reverse-words-in-a-string.cpp ├── NOTES.md └── README.md ├── 152-maximum-product-subarray ├── 152-maximum-product-subarray.cpp ├── NOTES.md └── README.md ├── 1525-number-of-good-ways-to-split-a-string ├── 1525-number-of-good-ways-to-split-a-string.cpp ├── NOTES.md └── README.md ├── 153-find-minimum-in-rotated-sorted-array ├── 153-find-minimum-in-rotated-sorted-array.cpp ├── NOTES.md └── README.md ├── 1539-kth-missing-positive-number ├── 1539-kth-missing-positive-number.cpp ├── NOTES.md └── README.md ├── 1584-min-cost-to-connect-all-points ├── 1584-min-cost-to-connect-all-points.cpp ├── NOTES.md └── README.md ├── 1588-sum-of-all-odd-length-subarrays ├── 1588-sum-of-all-odd-length-subarrays.cpp ├── NOTES.md └── README.md ├── 16-3sum-closest ├── NOTES.md └── README.md ├── 1631-path-with-minimum-effort ├── 1631-path-with-minimum-effort.cpp ├── NOTES.md └── README.md ├── 164-maximum-gap ├── 164-maximum-gap.cpp ├── NOTES.md └── README.md ├── 1641-count-sorted-vowel-strings ├── 1641-count-sorted-vowel-strings.cpp ├── NOTES.md └── README.md ├── 1663-smallest-string-with-a-given-numeric-value ├── 1663-smallest-string-with-a-given-numeric-value.cpp ├── NOTES.md └── README.md ├── 1679-max-number-of-k-sum-pairs ├── 1679-max-number-of-k-sum-pairs.cpp ├── NOTES.md └── README.md ├── 168-excel-sheet-column-title ├── 168-excel-sheet-column-title.cpp ├── NOTES.md └── README.md ├── 17-letter-combinations-of-a-phone-number ├── 17-letter-combinations-of-a-phone-number.cpp ├── NOTES.md └── README.md ├── 171-excel-sheet-column-number ├── 171-excel-sheet-column-number.cpp ├── NOTES.md └── README.md ├── 172-factorial-trailing-zeroes ├── 172-factorial-trailing-zeroes.cpp ├── NOTES.md └── README.md ├── 1721-swapping-nodes-in-a-linked-list ├── 1721-swapping-nodes-in-a-linked-list.cpp ├── NOTES.md └── README.md ├── 173-binary-search-tree-iterator ├── 173-binary-search-tree-iterator.cpp ├── NOTES.md └── README.md ├── 179-largest-number ├── 179-largest-number.cpp ├── NOTES.md └── README.md ├── 1793-maximum-score-of-a-good-subarray ├── 1793-maximum-score-of-a-good-subarray.cpp ├── NOTES.md └── README.md ├── 18-4sum ├── 18-4sum.cpp ├── NOTES.md └── README.md ├── 189-rotate-array ├── 189-rotate-array.cpp ├── NOTES.md └── README.md ├── 191-number-of-1-bits ├── 191-number-of-1-bits.cpp ├── NOTES.md └── README.md ├── 2-add-two-numbers ├── 2-add-two-numbers.cpp ├── NOTES.md └── README.md ├── 2007-find-original-array-from-doubled-array ├── 2007-find-original-array-from-doubled-array.cpp ├── NOTES.md └── README.md ├── 2013-detect-squares ├── 2013-detect-squares.cpp ├── NOTES.md └── README.md ├── 207-course-schedule ├── 207-course-schedule.cpp ├── NOTES.md └── README.md ├── 210-course-schedule-ii ├── 210-course-schedule-ii.cpp ├── NOTES.md └── README.md ├── 2115-find-all-possible-recipes-from-given-supplies ├── 2115-find-all-possible-recipes-from-given-supplies.cpp ├── NOTES.md └── README.md ├── 212-word-search-ii ├── 212-word-search-ii.cpp ├── NOTES.md └── README.md ├── 2135-count-words-obtained-after-adding-a-letter ├── 2135-count-words-obtained-after-adding-a-letter.cpp ├── NOTES.md └── README.md ├── 215-kth-largest-element-in-an-array ├── 215-kth-largest-element-in-an-array.cpp ├── NOTES.md └── README.md ├── 216-combination-sum-iii ├── 216-combination-sum-iii.cpp ├── NOTES.md └── README.md ├── 225-implement-stack-using-queues ├── 225-implement-stack-using-queues.cpp ├── NOTES.md └── README.md ├── 226-invert-binary-tree ├── 226-invert-binary-tree.cpp ├── NOTES.md └── README.md ├── 229-majority-element-ii ├── NOTES.md └── README.md ├── 23-merge-k-sorted-lists ├── 23-merge-k-sorted-lists.cpp ├── NOTES.md └── README.md ├── 230-kth-smallest-element-in-a-bst ├── 230-kth-smallest-element-in-a-bst.cpp ├── NOTES.md └── README.md ├── 231-power-of-two ├── 231-power-of-two.cpp ├── NOTES.md └── README.md ├── 235-lowest-common-ancestor-of-a-binary-search-tree ├── NOTES.md └── README.md ├── 238-product-of-array-except-self ├── NOTES.md └── README.md ├── 239-sliding-window-maximum ├── 239-sliding-window-maximum.cpp ├── NOTES.md └── README.md ├── 25-reverse-nodes-in-k-group ├── 25-reverse-nodes-in-k-group.cpp ├── NOTES.md └── README.md ├── 26-remove-duplicates-from-sorted-array ├── NOTES.md └── README.md ├── 268-missing-number ├── 268-missing-number.cpp ├── NOTES.md └── README.md ├── 28-implement-strstr ├── 28-implement-strstr.cpp ├── NOTES.md └── README.md ├── 283-move-zeroes ├── NOTES.md └── README.md ├── 284-peeking-iterator ├── 284-peeking-iterator.cpp ├── NOTES.md └── README.md ├── 287-find-the-duplicate-number ├── 287-find-the-duplicate-number.cpp ├── NOTES.md └── README.md ├── 289-game-of-life ├── 289-game-of-life.cpp ├── NOTES.md └── README.md ├── 29-divide-two-integers ├── 29-divide-two-integers.cpp ├── NOTES.md └── README.md ├── 3-longest-substring-without-repeating-characters ├── 3-longest-substring-without-repeating-characters.cpp ├── NOTES.md └── README.md ├── 30-substring-with-concatenation-of-all-words ├── 30-substring-with-concatenation-of-all-words.cpp ├── NOTES.md └── README.md ├── 304-range-sum-query-2d-immutable ├── 304-range-sum-query-2d-immutable.cpp ├── NOTES.md └── README.md ├── 31-next-permutation ├── 31-next-permutation.cpp ├── NOTES.md └── README.md ├── 316-remove-duplicate-letters ├── 316-remove-duplicate-letters.cpp ├── NOTES.md └── README.md ├── 318-maximum-product-of-word-lengths ├── 318-maximum-product-of-word-lengths.cpp ├── NOTES.md └── README.md ├── 32-longest-valid-parentheses ├── 32-longest-valid-parentheses.cpp ├── NOTES.md └── README.md ├── 322-coin-change ├── 322-coin-change.cpp ├── NOTES.md └── README.md ├── 329-longest-increasing-path-in-a-matrix ├── 329-longest-increasing-path-in-a-matrix.cpp ├── NOTES.md └── README.md ├── 33-search-in-rotated-sorted-array ├── 33-search-in-rotated-sorted-array.cpp ├── NOTES.md └── README.md ├── 341-flatten-nested-list-iterator ├── 341-flatten-nested-list-iterator.cpp ├── NOTES.md └── README.md ├── 344-reverse-string ├── 344-reverse-string.cpp ├── NOTES.md └── README.md ├── 347-top-k-frequent-elements ├── 347-top-k-frequent-elements.cpp ├── NOTES.md └── README.md ├── 354-russian-doll-envelopes ├── 354-russian-doll-envelopes.cpp └── README.md ├── 37-sudoku-solver ├── 37-sudoku-solver.cpp ├── NOTES.md └── README.md ├── 38-count-and-say ├── 38-count-and-say.cpp ├── NOTES.md └── README.md ├── 381-insert-delete-getrandom-o1-duplicates-allowed ├── 381-insert-delete-getrandom-o1-duplicates-allowed.cpp ├── NOTES.md └── README.md ├── 394-decode-string ├── 394-decode-string.cpp ├── NOTES.md └── README.md ├── 399-evaluate-division ├── 399-evaluate-division.cpp ├── NOTES.md └── README.md ├── 4-median-of-two-sorted-arrays ├── NOTES.md └── README.md ├── 404-sum-of-left-leaves ├── 404-sum-of-left-leaves.cpp ├── NOTES.md └── README.md ├── 41-first-missing-positive ├── 41-first-missing-positive.cpp ├── NOTES.md └── README.md ├── 410-split-array-largest-sum ├── 410-split-array-largest-sum.cpp ├── NOTES.md └── README.md ├── 412-fizz-buzz ├── 412-fizz-buzz.cpp ├── NOTES.md └── README.md ├── 42-trapping-rain-water ├── 42-trapping-rain-water.cpp ├── NOTES.md └── README.md ├── 44-wildcard-matching ├── 44-wildcard-matching.cpp ├── NOTES.md └── README.md ├── 456-132-pattern ├── 456-132-pattern.cpp ├── NOTES.md └── README.md ├── 47-permutations-ii ├── 47-permutations-ii.cpp └── README.md ├── 474-ones-and-zeroes ├── 474-ones-and-zeroes.cpp ├── NOTES.md └── README.md ├── 477-total-hamming-distance ├── 477-total-hamming-distance.cpp ├── NOTES.md └── README.md ├── 48-rotate-image ├── 48-rotate-image.cpp ├── NOTES.md └── README.md ├── 5-longest-palindromic-substring ├── 5-longest-palindromic-substring.cpp ├── NOTES.md └── README.md ├── 509-fibonacci-number ├── 509-fibonacci-number.cpp ├── NOTES.md └── README.md ├── 51-n-queens ├── 51-n-queens.cpp ├── NOTES.md └── README.md ├── 53-maximum-subarray ├── 53-maximum-subarray.cpp ├── NOTES.md └── README.md ├── 532-k-diff-pairs-in-an-array ├── 532-k-diff-pairs-in-an-array.cpp ├── NOTES.md └── README.md ├── 535-encode-and-decode-tinyurl ├── 535-encode-and-decode-tinyurl.cpp ├── NOTES.md └── README.md ├── 538-convert-bst-to-greater-tree ├── 538-convert-bst-to-greater-tree.cpp ├── NOTES.md └── README.md ├── 547-number-of-provinces ├── 547-number-of-provinces.cpp ├── NOTES.md └── README.md ├── 55-jump-game ├── 55-jump-game.cpp ├── NOTES.md └── README.md ├── 56-merge-intervals ├── 56-merge-intervals.cpp ├── NOTES.md └── README.md ├── 560-subarray-sum-equals-k ├── NOTES.md └── README.md ├── 57-insert-interval ├── 57-insert-interval.cpp ├── NOTES.md └── README.md ├── 572-subtree-of-another-tree ├── NOTES.md └── README.md ├── 58-length-of-last-word ├── 58-length-of-last-word.cpp ├── NOTES.md └── README.md ├── 581-shortest-unsorted-continuous-subarray ├── 581-shortest-unsorted-continuous-subarray.cpp ├── NOTES.md └── README.md ├── 59-spiral-matrix-ii ├── 59-spiral-matrix-ii.cpp ├── NOTES.md └── README.md ├── 617-merge-two-binary-trees ├── NOTES.md └── README.md ├── 62-unique-paths ├── 62-unique-paths.cpp ├── NOTES.md └── README.md ├── 63-unique-paths-ii ├── 63-unique-paths-ii.cpp ├── NOTES.md └── README.md ├── 632-smallest-range-covering-elements-from-k-lists ├── 632-smallest-range-covering-elements-from-k-lists.cpp ├── NOTES.md └── README.md ├── 66-plus-one ├── 66-plus-one.cpp ├── NOTES.md └── README.md ├── 662-maximum-width-of-binary-tree ├── 662-maximum-width-of-binary-tree.cpp ├── NOTES.md └── README.md ├── 669-trim-a-binary-search-tree ├── 669-trim-a-binary-search-tree.cpp ├── NOTES.md └── README.md ├── 680-valid-palindrome-ii ├── 680-valid-palindrome-ii.cpp ├── NOTES.md └── README.md ├── 682-baseball-game ├── 682-baseball-game.cpp ├── NOTES.md └── README.md ├── 7-reverse-integer ├── 7-reverse-integer.cpp ├── NOTES.md └── README.md ├── 700-search-in-a-binary-search-tree ├── 700-search-in-a-binary-search-tree.cpp ├── NOTES.md └── README.md ├── 703-kth-largest-element-in-a-stream ├── 703-kth-largest-element-in-a-stream.cpp └── README.md ├── 704-binary-search ├── 704-binary-search.cpp ├── NOTES.md └── README.md ├── 705-design-hashset ├── 705-design-hashset.cpp ├── NOTES.md └── README.md ├── 706-design-hashmap ├── 706-design-hashmap.cpp ├── NOTES.md └── README.md ├── 71-simplify-path ├── 71-simplify-path.cpp ├── NOTES.md └── README.md ├── 72-edit-distance ├── 72-edit-distance.cpp ├── NOTES.md └── README.md ├── 724-find-pivot-index ├── NOTES.md └── README.md ├── 73-set-matrix-zeroes ├── 73-set-matrix-zeroes.cpp ├── NOTES.md └── README.md ├── 74-search-a-2d-matrix ├── 74-search-a-2d-matrix.cpp ├── NOTES.md └── README.md ├── 743-network-delay-time ├── 743-network-delay-time.cpp ├── NOTES.md └── README.md ├── 754-reach-a-number ├── 754-reach-a-number.cpp ├── NOTES.md └── README.md ├── 76-minimum-window-substring ├── 76-minimum-window-substring.cpp ├── NOTES.md └── README.md ├── 763-partition-labels ├── 763-partition-labels.cpp ├── NOTES.md └── README.md ├── 78-subsets ├── NOTES.md └── README.md ├── 785-is-graph-bipartite ├── 785-is-graph-bipartite.cpp └── README.md ├── 79-word-search ├── 79-word-search.cpp ├── NOTES.md └── README.md ├── 81-search-in-rotated-sorted-array-ii ├── 81-search-in-rotated-sorted-array-ii.cpp ├── NOTES.md └── README.md ├── 844-backspace-string-compare ├── 844-backspace-string-compare.cpp ├── NOTES.md └── README.md ├── 852-peak-index-in-a-mountain-array ├── 852-peak-index-in-a-mountain-array.cpp ├── NOTES.md └── README.md ├── 856-score-of-parentheses ├── 856-score-of-parentheses.cpp ├── NOTES.md └── README.md ├── 867-transpose-matrix ├── 867-transpose-matrix.cpp ├── NOTES.md └── README.md ├── 881-boats-to-save-people ├── 881-boats-to-save-people.cpp ├── NOTES.md └── README.md ├── 889-construct-binary-tree-from-preorder-and-postorder-traversal ├── 889-construct-binary-tree-from-preorder-and-postorder-traversal.cpp ├── NOTES.md └── README.md ├── 895-maximum-frequency-stack ├── 895-maximum-frequency-stack.cpp ├── NOTES.md └── README.md ├── 897-increasing-order-search-tree ├── 897-increasing-order-search-tree.cpp ├── NOTES.md └── README.md ├── 9-palindrome-number ├── 9-palindrome-number.cpp ├── NOTES.md └── README.md ├── 905-sort-array-by-parity ├── 905-sort-array-by-parity.cpp ├── NOTES.md └── README.md ├── 909-snakes-and-ladders ├── 909-snakes-and-ladders.cpp ├── NOTES.md └── README.md ├── 923-3sum-with-multiplicity ├── 923-3sum-with-multiplicity.cpp ├── NOTES.md └── README.md ├── 934-shortest-bridge ├── 934-shortest-bridge.cpp ├── NOTES.md └── README.md ├── 938-range-sum-of-bst ├── 938-range-sum-of-bst.cpp ├── NOTES.md └── README.md ├── 946-validate-stack-sequences ├── 946-validate-stack-sequences.cpp ├── NOTES.md └── README.md ├── 95-unique-binary-search-trees-ii ├── 95-unique-binary-search-trees-ii.cpp ├── NOTES.md └── README.md ├── 96-unique-binary-search-trees ├── 96-unique-binary-search-trees.cpp ├── NOTES.md └── README.md ├── 97-interleaving-string ├── 97-interleaving-string.cpp ├── NOTES.md └── README.md ├── 977-squares-of-a-sorted-array ├── 977-squares-of-a-sorted-array.cpp ├── NOTES.md └── README.md ├── 98-validate-binary-search-tree ├── 98-validate-binary-search-tree.cpp ├── NOTES.md └── README.md ├── 99-recover-binary-search-tree ├── 99-recover-binary-search-tree.cpp ├── NOTES.md └── README.md ├── 991-broken-calculator ├── 991-broken-calculator.cpp ├── NOTES.md └── README.md └── README.md /0945-snakes-and-ladders/0945-snakes-and-ladders.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | void getCoordinate(int n,int s, int &row, int &col){ 3 | row = n-1-(s-1)/n; 4 | col = (s-1)%n; 5 | if((n%2==1 && row%2==1)||(n%2==0 && row%2==0)) 6 | col = n-1-col; 7 | } 8 | public: 9 | int snakesAndLadders(vector>& board) { 10 | int n = board.size(); 11 | vector seen(n*n+1,false); 12 | seen[1] = true; 13 | queue> q; 14 | q.push({1,0}); 15 | while(!q.empty()){ 16 | pair p = q.front();q.pop(); 17 | int row,col,s = p.first, dist = p.second; 18 | if(s == n*n) 19 | return dist; 20 | for(int i=1;s+i<=n*n && i<=6;i++){ 21 | getCoordinate(n,s+i,row,col); 22 | int sfinal = board[row][col]==-1?s+i:board[row][col]; // check for snake or ladder 23 | if(seen[sfinal]==false){ 24 | seen[sfinal] = true; 25 | q.push({sfinal,dist+1}); 26 | } 27 | } 28 | } 29 | return -1; 30 | } 31 | }; -------------------------------------------------------------------------------- /1007-minimum-domino-rotations-for-equal-row/1007-minimum-domino-rotations-for-equal-row.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minDominoRotations(vector& tops, vector& bottoms) { 4 | 5 | int n=tops.size(); 6 | 7 | int ans=INT_MAX; 8 | for(int i=1;i<=6;i++){ 9 | int temp=0; 10 | for(int k=0;k&p, int s, int e){ 15 | TreeNode *root = new TreeNode(p[s]); 16 | int m; 17 | for(m=s+1; m<=e; ++m) 18 | if(p[m]>p[s]) break; 19 | if(m<=e) 20 | root->right = solve(p,m,e); 21 | if(sleft = solve(p,s+1,m-1); 23 | return root; 24 | } 25 | 26 | TreeNode* bstFromPreorder(vector& preorder) { 27 | return solve(preorder,0,preorder.size()-1); 28 | } 29 | }; -------------------------------------------------------------------------------- /1008-construct-binary-search-tree-from-preorder-traversal/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1010-pairs-of-songs-with-total-durations-divisible-by-60/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1013-partition-array-into-three-parts-with-equal-sum/1013-partition-array-into-three-parts-with-equal-sum.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool canThreePartsEqualSum(vector& arr) { 4 | int sum2=0, n=arr.size(); 5 | for(int i=0;i=3){ 21 | return true; 22 | } 23 | return false; 24 | 25 | } 26 | }; -------------------------------------------------------------------------------- /1013-partition-array-into-three-parts-with-equal-sum/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /102-binary-tree-level-order-traversal/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /102-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 |
Input: root = [3,9,20,null,null,15,7]
 7 | Output: [[3],[9,20],[15,7]]
 8 | 
9 | 10 |

Example 2:

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

Example 3:

17 | 18 |
Input: root = []
19 | Output: []
20 | 
21 | 22 |

 

23 |

Constraints:

24 | 25 |
    26 |
  • The number of nodes in the tree is in the range [0, 2000].
  • 27 |
  • -1000 <= Node.val <= 1000
  • 28 |
29 |
-------------------------------------------------------------------------------- /1029-two-city-scheduling/1029-two-city-scheduling.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | 4 | // static bool sortbysec(const vector &a, 5 | // const vector &b) 6 | // { 7 | // if(a[0]==b[0]){ 8 | // return a[1]>b[1]; 9 | // } 10 | // return (a[0] < b[0]); 11 | // } 12 | 13 | static bool sortbysec(const vector &a, 14 | const vector &b) 15 | { 16 | return (a[0]-a[1]< b[0]-b[1]); 17 | } 18 | int twoCitySchedCost(vector>& costs) { 19 | int seats=costs.size(); 20 | int n=seats/2; 21 | 22 | sort(costs.begin(), costs.end(), sortbysec); 23 | 24 | int i; 25 | int ans=0; 26 | for(i=0;i& stones) { 4 | int n=stones.size(); 5 | priority_queue pq; 6 | for(int i=0;i0){ 10 | if(pq.size()==1){ 11 | return pq.top(); 12 | } 13 | int y=pq.top(); 14 | pq.pop(); 15 | int x=pq.top(); 16 | pq.pop(); 17 | if(x==y){ 18 | continue; 19 | } else { 20 | pq.push(y-x); 21 | } 22 | } 23 | return 0; 24 | } 25 | }; -------------------------------------------------------------------------------- /1046-last-stone-weight/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /108-convert-sorted-array-to-binary-search-tree/108-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* helper(vector& nums, int l, int r){ 15 | if(l>r){ 16 | return NULL; 17 | } 18 | int mid=(l+r)/2; 19 | int val=nums[mid]; 20 | TreeNode* root=new TreeNode(val); 21 | root->right=helper(nums, mid+1, r); 22 | root->left=helper(nums, l, mid-1); 23 | return root; 24 | } 25 | TreeNode* sortedArrayToBST(vector& nums) { 26 | int n=nums.size(); 27 | return helper(nums, 0, nums.size()-1); 28 | } 29 | }; -------------------------------------------------------------------------------- /108-convert-sorted-array-to-binary-search-tree/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /109-convert-sorted-list-to-binary-search-tree/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1091-shortest-path-in-binary-matrix/1091-shortest-path-in-binary-matrix.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int dx[8] = {0,0,1,1,1,-1,-1,-1}; 4 | int dy[8] = {-1,1,-1,0,1,-1,0,1}; 5 | 6 | typedef pair> pp; 7 | 8 | int shortestPathBinaryMatrix(vector>& grid) { 9 | if(grid[0][0])return -1; // beginning node is blocked 10 | 11 | int n = grid.size(); 12 | vector> vis(n,vector(n,0)); 13 | priority_queue,greater> q; 14 | q.push({1,{0,0}}); 15 | vis[0][0] = 1; 16 | 17 | while(q.size()){ 18 | auto cur = q.top(); q.pop(); 19 | int d = cur.first, i = cur.second.first, j = cur.second.second; 20 | 21 | if(i == n-1 && j == n-1)return d; // target node reached 22 | 23 | for(int k = 0; k < 8; k++){ 24 | int ni = i+dx[k], nj = j+dy[k]; 25 | 26 | // check if the new position is valid 27 | if(ni >= 0 && nj >= 0 && ni < n && nj < n && !vis[ni][nj] && !grid[ni][nj]){ 28 | vis[ni][nj] = 1; 29 | q.push({1+d,{ni,nj}}); 30 | } 31 | } 32 | } 33 | return -1; // target can't be reached 34 | } 35 | }; -------------------------------------------------------------------------------- /1091-shortest-path-in-binary-matrix/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1094-car-pooling/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /11-container-with-most-water/11-container-with-most-water.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | class Solution 4 | { 5 | public: 6 | int maxArea(vector &height) 7 | { 8 | 9 | int water = 0; 10 | int i = 0, j = height.size() - 1; 11 | while(ileft); 19 | int rh=height(root->right); 20 | return 1+max(lh,rh); 21 | } 22 | bool isBalanced(TreeNode* root) { 23 | if(!root){ 24 | return true; 25 | } 26 | 27 | if(abs(height(root->left)-height(root->right))<=1 && isBalanced(root->left) && isBalanced(root->right)){ 28 | return true; 29 | } 30 | 31 | 32 | 33 | return false; 34 | } 35 | }; -------------------------------------------------------------------------------- /110-balanced-binary-tree/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1131-maximum-of-absolute-value-expression/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1131-maximum-of-absolute-value-expression/README.md: -------------------------------------------------------------------------------- 1 |

1131. Maximum of Absolute Value Expression

Medium


Given two arrays of integers with equal lengths, return the maximum value of:

2 | 3 |

|arr1[i] - arr1[j]| + |arr2[i] - arr2[j]| + |i - j|

4 | 5 |

where the maximum is taken over all 0 <= i, j < arr1.length.

6 | 7 |

 

8 |

Example 1:

9 | 10 |
Input: arr1 = [1,2,3,4], arr2 = [-1,4,5,6]
11 | Output: 13
12 | 
13 | 14 |

Example 2:

15 | 16 |
Input: arr1 = [1,-2,-5,0,10], arr2 = [0,-2,-1,-7,-4]
17 | Output: 20
18 | 
19 | 20 |

 

21 |

Constraints:

22 | 23 |
    24 |
  • 2 <= arr1.length == arr2.length <= 40000
  • 25 |
  • -10^6 <= arr1[i], arr2[i] <= 10^6
  • 26 |
27 |
-------------------------------------------------------------------------------- /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(), m=text2.length(); 5 | vector> dp(n, vector(m,0)); 6 | dp[0][0]=(text1[0]==text2[0])?1:0; 7 | for(int i=1;i> hmap; 3 | vector latestsnap; 4 | int snapid=0; 5 | public: 6 | SnapshotArray(int length) { 7 | vector temp(length+1,0); 8 | latestsnap=temp; 9 | } 10 | 11 | void set(int index, int val) { 12 | hmap[snapid][index]=val; 13 | 14 | 15 | } 16 | 17 | int snap() { 18 | snapid++; 19 | return snapid-1; 20 | } 21 | 22 | int get(int index, int snap_id) { 23 | 24 | for(int i=snap_id;i>=0;i--) 25 | { 26 | if(hmap[i].count(index)==1) 27 | return hmap[i][index]; 28 | } 29 | return 0; 30 | } 31 | }; 32 | /** 33 | *Your SnapshotArray object will be instantiated and called as such: 34 | *SnapshotArray* obj = new SnapshotArray(length); 35 | *obj->set(index,val); 36 | *int param_2 = obj->snap(); 37 | *int param_3 = obj->get(index,snap_id); 38 | */ -------------------------------------------------------------------------------- /1146-snapshot-array/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /115-distinct-subsequences/115-distinct-subsequences.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int helper(string s, string t, int i, int j, vector> dp ){ 4 | if(j<0){ 5 | return 1; 6 | } 7 | if(i<0){ 8 | return 0; 9 | } 10 | if(dp[i][j]!=-1){ 11 | return dp[i][j]; 12 | } 13 | 14 | if(s[i]==t[j]){ 15 | return dp[i][j]= helper(s,t,i-1,j-1,dp)+helper(s,t,i-1,j,dp); 16 | } else { 17 | return dp[i][j]= helper(s,t,i-1,j,dp); 18 | } 19 | } 20 | int numDistinct(string s, string t) { 21 | static int MOD = (int)1e9 + 7; 22 | long long int n=s.length(), m=t.length(); 23 | vector> dp(n+1, vector(m+1,0)); 24 | for(long long int j=0;j<=n;j++){ 25 | dp[j][0]=1; 26 | } 27 | for(long long int i=1;i<=n;i++){ 28 | for(long long int j=1;j<=m;j++){ 29 | if(s[i-1]==t[j-1]){ 30 | dp[i][j]=(dp[i-1][j]+dp[i-1][j-1])%MOD; 31 | } else { 32 | dp[i][j]=dp[i-1][j]; 33 | } 34 | } 35 | } 36 | return (int)dp[n][m]; 37 | } 38 | }; -------------------------------------------------------------------------------- /115-distinct-subsequences/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /116-populating-next-right-pointers-in-each-node/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /117-populating-next-right-pointers-in-each-node-ii/117-populating-next-right-pointers-in-each-node-ii.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 | { 23 | if(!root) return nullptr; 24 | queue queue; 25 | queue.push(root); 26 | 27 | while(!queue.empty()){ 28 | int size = queue.size(); 29 | Node *node = NULL; 30 | while(size--){ 31 | auto curr = queue.front(); 32 | if(node != NULL){ 33 | node->next = curr; 34 | } 35 | queue.pop(); 36 | node = curr; 37 | if(curr->left) queue.push(curr->left); 38 | if(curr->right) queue.push(curr->right); 39 | } 40 | node->next = NULL; 41 | } 42 | return root; 43 | } 44 | }; -------------------------------------------------------------------------------- /117-populating-next-right-pointers-in-each-node-ii/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /119-pascals-triangle-ii/119-pascals-triangle-ii.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector getRow(int rowIndex) { 4 | vector ans={1,1}; 5 | if(rowIndex<=0){ 6 | return {1}; 7 | } else if(rowIndex<=1){ 8 | return {1,1}; 9 | } 10 | for(int i=2;i<=rowIndex;i++){ 11 | vector temp; 12 | temp.push_back(1); 13 | for(int j=0;j119. Pascal's Triangle II

Easy


Given an integer rowIndex, return the rowIndexth (0-indexed) row of the 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: rowIndex = 3
 8 | Output: [1,3,3,1]
 9 | 

Example 2:

10 |
Input: rowIndex = 0
11 | Output: [1]
12 | 

Example 3:

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

 

17 |

Constraints:

18 | 19 |
    20 |
  • 0 <= rowIndex <= 33
  • 21 |
22 | 23 |

 

24 |

Follow up: Could you optimize your algorithm to use only O(rowIndex) extra space?

25 |
-------------------------------------------------------------------------------- /1192-critical-connections-in-a-network/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1202-smallest-string-with-swaps/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1207-unique-number-of-occurrences/1207-unique-number-of-occurrences.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool uniqueOccurrences(vector& arr) { 4 | int n=arr.size(); 5 | unordered_map m; 6 | for(int i=0;i::iterator itr; 10 | unordered_map> m2; 11 | for(itr=m.begin();itr!=m.end();itr++){ 12 | m2[itr->second].push_back(itr->first); 13 | } 14 | unordered_map>::iterator itr2; 15 | for(itr2=m2.begin();itr2!=m2.end();itr2++){ 16 | if(itr2->second.size()>1){ 17 | return false; 18 | } 19 | } 20 | return true; 21 | } 22 | }; -------------------------------------------------------------------------------- /1207-unique-number-of-occurrences/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1207-unique-number-of-occurrences/README.md: -------------------------------------------------------------------------------- 1 |

1207. Unique Number of Occurrences

Easy


Given an array of integers arr, return true if the number of occurrences of each value in the array is unique, or false otherwise.

2 | 3 |

 

4 |

Example 1:

5 | 6 |
Input: arr = [1,2,2,1,1,3]
 7 | Output: true
 8 | Explanation: The value 1 has 3 occurrences, 2 has 2 and 3 has 1. No two values have the same number of occurrences.
9 | 10 |

Example 2:

11 | 12 |
Input: arr = [1,2]
13 | Output: false
14 | 
15 | 16 |

Example 3:

17 | 18 |
Input: arr = [-3,0,1,-3,1,1,1,-3,10,0]
19 | Output: true
20 | 
21 | 22 |

 

23 |

Constraints:

24 | 25 |
    26 |
  • 1 <= arr.length <= 1000
  • 27 |
  • -1000 <= arr[i] <= 1000
  • 28 |
29 |
-------------------------------------------------------------------------------- /1209-remove-all-adjacent-duplicates-in-string-ii/1209-remove-all-adjacent-duplicates-in-string-ii.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | string removeDuplicates(string s, int k) 5 | { 6 | stack> st; 7 | string ans = ""; 8 | int n = s.size(); 9 | 10 | for (int i = 0; i < n; i++) 11 | { 12 | if (st.empty() || st.top().first != s[i]) 13 | { 14 | st.push({ s[i], 15 | 1 }); 16 | } 17 | else 18 | { 19 | auto prev = st.top(); 20 | st.pop(); 21 | st.push({ s[i], 22 | prev.second + 1 }); 23 | } 24 | 25 | if (st.top().second == k) st.pop(); 26 | } 27 | 28 | while (st.size() != 0) 29 | { 30 | auto prev = st.top(); 31 | st.pop(); 32 | 33 | while (prev.second--) 34 | { 35 | ans.push_back(prev.first); 36 | } 37 | } 38 | 39 | reverse(ans.begin(), ans.end()); 40 | 41 | return ans; 42 | } 43 | }; -------------------------------------------------------------------------------- /1209-remove-all-adjacent-duplicates-in-string-ii/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /121-best-time-to-buy-and-sell-stock/121-best-time-to-buy-and-sell-stock.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxProfit(vector& prices) { 4 | int profit=0, n=prices.size(); 5 | 6 | int minim=prices[0]; 7 | for(int i=1;i=65 && s[i]<=90) || (s[i]>=97 && s[i]<=122) || (s[i]>=48 && s[i]<=57))){ 8 | continue; 9 | } else if(s[i]>=65 && s[i]<=90){ 10 | str+=s[i]+32; 11 | } else { 12 | str+=s[i]; 13 | } 14 | } 15 | string ans=str; 16 | reverse(str.begin(), str.end()); 17 | return ans==str; 18 | } 19 | }; -------------------------------------------------------------------------------- /125-valid-palindrome/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1260-shift-2d-grid/1260-shift-2d-grid.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | 5 | vector> shiftGrid(vector> &grid, int k) 6 | { 7 | int n = grid.size(); 8 | int m = grid[0].size(); 9 | vector dp(n*m, -1); 10 | int count=0; 11 | k=k%(m*n); 12 | k=(n*m)-k; 13 | for(int i=0;i>& points) { 4 | int n=points.size(); 5 | int ans=0; 6 | for(int i=1;i& nums) { 4 | unordered_map m; 5 | int n=nums.size(); 6 | for(int i=0;i128. 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 |
Input: nums = [100,4,200,1,3,2]
 9 | Output: 4
10 | Explanation: The longest consecutive elements sequence is [1, 2, 3, 4]. Therefore its length is 4.
11 | 
12 | 13 |

Example 2:

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

 

20 |

Constraints:

21 | 22 |
    23 |
  • 0 <= nums.length <= 105
  • 24 |
  • -109 <= nums[i] <= 109
  • 25 |
26 |
-------------------------------------------------------------------------------- /130-surrounded-regions/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1326-minimum-number-of-taps-to-open-to-water-a-garden/1326-minimum-number-of-taps-to-open-to-water-a-garden.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | class Solution { 4 | public: 5 | int minTaps(int n, vector& ranges) { 6 | 7 | int min=0, max=0, open=0; 8 | while(maxmax){ 11 | max=i+ranges[i]; 12 | } 13 | } 14 | if(min==max){ 15 | return -1; 16 | } 17 | open++; 18 | min=max; 19 | } 20 | return open; 21 | 22 | } 23 | }; -------------------------------------------------------------------------------- /133-clone-graph/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1331-rank-transform-of-an-array/1331-rank-transform-of-an-array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector arrayRankTransform(vector& arr) 4 | { 5 | //map 6 | int rank=1; //to assign the rank to the elements 7 | map>mp; //why map->to ensure the sorted nature of the elements 8 | 9 | for(int i=0;iv; 14 | for(auto it=mp.begin();it!=mp.end();it++) 15 | { 16 | v=it->second; //this vector will have basically all the index of the it->first 17 | int idx; 18 | for(int i=0;ileft, cloned->left, target, ans); 25 | dfs(original->right, cloned->right, target, ans); 26 | return; 27 | } 28 | TreeNode* getTargetCopy(TreeNode *original, TreeNode *cloned, TreeNode *target) 29 | { 30 | TreeNode *ans = nullptr; 31 | dfs(original, cloned, target, ans); 32 | return ans; 33 | } 34 | }; -------------------------------------------------------------------------------- /1379-find-a-corresponding-node-of-a-binary-tree-in-a-clone-of-that-tree/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1396-design-underground-system/1396-design-underground-system.cpp: -------------------------------------------------------------------------------- 1 | class UndergroundSystem { 2 | public: 3 | UndergroundSystem() { 4 | 5 | } 6 | 7 | unordered_map> m; 8 | unordered_map> stats; 9 | void checkIn(int id, string startStation, int timeIn) { 10 | m[id] = {startStation, timeIn}; 11 | } 12 | void checkOut(int id, string endStation, int timeOut) { 13 | const auto &[startStation, timeIn] = m[id]; 14 | auto &[totalDuration, tripsCnt] = stats[startStation + ">" + endStation]; 15 | totalDuration += timeOut - timeIn; 16 | ++tripsCnt; 17 | } 18 | double getAverageTime(string startStation, string endStation) { 19 | auto [totalDuration, tripsCnt] = stats[startStation + ">" + endStation]; 20 | return (double)totalDuration / tripsCnt; 21 | } 22 | }; 23 | 24 | /** 25 | * Your UndergroundSystem object will be instantiated and called as such: 26 | * UndergroundSystem* obj = new UndergroundSystem(); 27 | * obj->checkIn(id,stationName,t); 28 | * obj->checkOut(id,stationName,t); 29 | * double param_3 = obj->getAverageTime(startStation,endStation); 30 | */ -------------------------------------------------------------------------------- /1396-design-underground-system/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /14-longest-common-prefix/14-longest-common-prefix.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string longestCommonPrefix(vector& strs) { 4 | int minlen=INT_MAX; 5 | string minstr; 6 | string ans; 7 | int n=strs.size(); 8 | if(n==1){ 9 | return strs[0]; 10 | } 11 | for(int i=0;istrs[i].length()){ 13 | minlen=strs[i].length(); 14 | minstr=strs[i]; 15 | } 16 | } 17 | if(minlen==0){ 18 | return ""; 19 | } 20 | cout<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 |
Input: strs = ["flower","flow","flight"]
 9 | Output: "fl"
10 | 
11 | 12 |

Example 2:

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

 

20 |

Constraints:

21 | 22 |
    23 |
  • 1 <= strs.length <= 200
  • 24 |
  • 0 <= strs[i].length <= 200
  • 25 |
  • strs[i] consists of only lower-case English letters.
  • 26 |
27 |
-------------------------------------------------------------------------------- /1423-maximum-points-you-can-obtain-from-cards/1423-maximum-points-you-can-obtain-from-cards.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | 5 | int maxScore(vector &C, int K) 6 | { 7 | int ans = 0; 8 | int n = C.size(); 9 | for(int i=0;i=0){ 16 | ans-=C[K]; 17 | ans+=C[n]; 18 | ansmax=max(ans, ansmax); 19 | n--; 20 | K--; 21 | 22 | } 23 | return ansmax; 24 | } 25 | }; -------------------------------------------------------------------------------- /1423-maximum-points-you-can-obtain-from-cards/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1424-diagonal-traverse-ii/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1424-diagonal-traverse-ii/README.md: -------------------------------------------------------------------------------- 1 |

1424. Diagonal Traverse II

Medium


Given a 2D integer array nums, return all elements of nums in diagonal order as shown in the below images.

2 | 3 |

 

4 |

Example 1:

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

Example 2:

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

 

17 |

Constraints:

18 | 19 |
    20 |
  • 1 <= nums.length <= 105
  • 21 |
  • 1 <= nums[i].length <= 105
  • 22 |
  • 1 <= sum(nums[i].length) <= 105
  • 23 |
  • 1 <= nums[i][j] <= 105
  • 24 |
25 |
-------------------------------------------------------------------------------- /1461-check-if-a-string-contains-all-binary-codes-of-size-k/1461-check-if-a-string-contains-all-binary-codes-of-size-k.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | class Solution { 4 | public: 5 | bool hasAllCodes(string s, int k) { 6 | unordered_set gg; 7 | if(s.length()& hCuts, vector& vCuts) { 4 | sort(begin(hCuts), end(hCuts)); 5 | sort(begin(vCuts), end(vCuts)); 6 | auto max_h = max(hCuts[0], h - hCuts.back()); 7 | auto max_v = max(vCuts[0], w - vCuts.back()); 8 | for (auto i = 0; i < hCuts.size() - 1; ++i) 9 | max_h = max(max_h, hCuts[i + 1] - hCuts[i]); 10 | for (auto i = 0; i < vCuts.size() - 1; ++i) 11 | max_v = max(max_v, vCuts[i + 1] - vCuts[i]); 12 | return (long)max_h * max_v % 1000000007; 13 | } 14 | }; -------------------------------------------------------------------------------- /1465-maximum-area-of-a-piece-of-cake-after-horizontal-and-vertical-cuts/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1473-paint-house-iii/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1477-find-two-non-overlapping-sub-arrays-each-with-target-sum/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1480-running-sum-of-1d-array/1480-running-sum-of-1d-array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector runningSum(vector& nums) { 4 | int n=nums.size(); 5 | vector prefix(n,0); 6 | prefix[0]=nums[0]; 7 | for(int i=1;i1480. Running Sum of 1d Array

Easy


Given an array nums. We define a running sum of an array as runningSum[i] = sum(nums[0]…nums[i]).

2 | 3 |

Return the running sum of nums.

4 | 5 |

 

6 |

Example 1:

7 | 8 |
Input: nums = [1,2,3,4]
 9 | Output: [1,3,6,10]
10 | Explanation: Running sum is obtained as follows: [1, 1+2, 1+2+3, 1+2+3+4].
11 | 12 |

Example 2:

13 | 14 |
Input: nums = [1,1,1,1,1]
15 | Output: [1,2,3,4,5]
16 | Explanation: Running sum is obtained as follows: [1, 1+1, 1+1+1, 1+1+1+1, 1+1+1+1+1].
17 | 18 |

Example 3:

19 | 20 |
Input: nums = [3,1,2,10,1]
21 | Output: [3,4,6,16,17]
22 | 
23 | 24 |

 

25 |

Constraints:

26 | 27 |
    28 |
  • 1 <= nums.length <= 1000
  • 29 |
  • -10^6 <= nums[i] <= 10^6
  • 30 |
-------------------------------------------------------------------------------- /15-3sum/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /15-3sum/README.md: -------------------------------------------------------------------------------- 1 |

15. 3Sum

Medium


Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0.

2 | 3 |

Notice that the solution set must not contain duplicate triplets.

4 | 5 |

 

6 |

Example 1:

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

Example 2:

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

Example 3:

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

 

17 |

Constraints:

18 | 19 |
    20 |
  • 0 <= nums.length <= 3000
  • 21 |
  • -105 <= nums[i] <= 105
  • 22 |
23 |
-------------------------------------------------------------------------------- /151-reverse-words-in-a-string/151-reverse-words-in-a-string.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string reverseWords(string s) { 4 | string ans; 5 | string temp=""; 6 | int n=s.length(); 7 | reverse(s.begin(), s.end()); 8 | for(int i=0;i152. Maximum Product Subarray

Medium


Given an integer array nums, find a contiguous non-empty subarray within the array that has the largest product, and return the product.

2 | 3 |

The test cases are generated so that the answer will fit in a 32-bit integer.

4 | 5 |

A subarray is a contiguous subsequence of the array.

6 | 7 |

 

8 |

Example 1:

9 | 10 |
Input: nums = [2,3,-2,4]
11 | Output: 6
12 | Explanation: [2,3] has the largest product 6.
13 | 
14 | 15 |

Example 2:

16 | 17 |
Input: nums = [-2,0,-1]
18 | Output: 0
19 | Explanation: The result cannot be 2, because [-2,-1] is not a subarray.
20 | 
21 | 22 |

 

23 |

Constraints:

24 | 25 |
    26 |
  • 1 <= nums.length <= 2 * 104
  • 27 |
  • -10 <= nums[i] <= 10
  • 28 |
  • The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer.
  • 29 |
30 |
-------------------------------------------------------------------------------- /1525-number-of-good-ways-to-split-a-string/1525-number-of-good-ways-to-split-a-string.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numSplits(string s) { 4 | unordered_map freq; 5 | for(char ch:s){ 6 | freq[ch]++; 7 | } 8 | 9 | int result = 0; 10 | unordered_map currFreq; 11 | 12 | for(int i=0; i& nums) { 6 | int ans=INT_MAX; 7 | if(nums.size()==1){ 8 | return nums[0]; 9 | } else if(nums[0]<=nums[nums.size()-1]){ 10 | return nums[0]; 11 | } 12 | int left=0, right=nums.size()-1; 13 | 14 | while(left<=right){ 15 | int mid=(left+right)/2; 16 | 17 | if(nums[0]<=nums[mid]){ 18 | left=mid+1; 19 | } else { 20 | ans=min(ans, nums[mid]); 21 | right=mid-1; 22 | } 23 | 24 | } 25 | 26 | return ans; 27 | 28 | } 29 | }; -------------------------------------------------------------------------------- /153-find-minimum-in-rotated-sorted-array/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1539-kth-missing-positive-number/1539-kth-missing-positive-number.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | int findKthPositive(vector &arr, int k) 5 | { 6 | int size = arr.size(), ll = 0, ul = size - 1, ans = 0; 7 | if (k < arr[0]) 8 | return k; 9 | while (ll <= ul) 10 | { 11 | int mid = ll + (ul - ll) / 2, diff = arr[mid] - (mid + 1); 12 | if (diff == k) 13 | { 14 | ans = arr[mid] - 1; 15 | ul = mid - 1; 16 | } 17 | else if (diff < k) 18 | ll = mid + 1; 19 | else 20 | ul = mid - 1; 21 | } 22 | if (ans) 23 | return ans; 24 | int diff = arr[ul] - (ul + 1); 25 | return arr[ul] + (k - diff); 26 | } 27 | }; -------------------------------------------------------------------------------- /1539-kth-missing-positive-number/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1584-min-cost-to-connect-all-points/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1588-sum-of-all-odd-length-subarrays/1588-sum-of-all-odd-length-subarrays.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | int sumOddLengthSubarrays(vector &arr) 5 | { 6 | int n = arr.size(); 7 | long long int sum = 0; 8 | vector ans(n, 0); 9 | ans[0] = arr[0]; 10 | for (int i = 1; i < n; i++) 11 | { 12 | ans[i] = ans[i - 1] + arr[i]; 13 | } 14 | for (int i = 0; i < n; i++) 15 | { 16 | sum += arr[i]; 17 | } 18 | 19 | for (int j = 2; j < n; j = j + 2) 20 | { 21 | for (int i = 0; i + j < n; i++) 22 | { 23 | 24 | if (i == 0) 25 | { 26 | sum += ans[j]; 27 | } 28 | else 29 | { 30 | sum += ans[i + j] - ans[i - 1]; 31 | } 32 | } 33 | } 34 | 35 | return (int) sum; 36 | } 37 | }; -------------------------------------------------------------------------------- /1588-sum-of-all-odd-length-subarrays/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /16-3sum-closest/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /16-3sum-closest/README.md: -------------------------------------------------------------------------------- 1 |

16. 3Sum Closest

Medium


Given an integer array nums of length n and an integer target, find three integers in nums such that the sum is closest to target.

2 | 3 |

Return the sum of the three integers.

4 | 5 |

You may assume that each input would have exactly one solution.

6 | 7 |

 

8 |

Example 1:

9 | 10 |
Input: nums = [-1,2,1,-4], target = 1
11 | Output: 2
12 | Explanation: The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
13 | 
14 | 15 |

Example 2:

16 | 17 |
Input: nums = [0,0,0], target = 1
18 | Output: 0
19 | 
20 | 21 |

 

22 |

Constraints:

23 | 24 |
    25 |
  • 3 <= nums.length <= 1000
  • 26 |
  • -1000 <= nums[i] <= 1000
  • 27 |
  • -104 <= target <= 104
  • 28 |
29 |
-------------------------------------------------------------------------------- /1631-path-with-minimum-effort/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /164-maximum-gap/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /164-maximum-gap/README.md: -------------------------------------------------------------------------------- 1 |

164. Maximum Gap

Hard


Given an integer array nums, return the maximum difference between two successive elements in its sorted form. If the array contains less than two elements, return 0.

2 | 3 |

You must write an algorithm that runs in linear time and uses linear extra space.

4 | 5 |

 

6 |

Example 1:

7 | 8 |
Input: nums = [3,6,9,1]
 9 | Output: 3
10 | Explanation: The sorted form of the array is [1,3,6,9], either (3,6) or (6,9) has the maximum difference 3.
11 | 
12 | 13 |

Example 2:

14 | 15 |
Input: nums = [10]
16 | Output: 0
17 | Explanation: The array contains less than 2 elements, therefore return 0.
18 | 
19 | 20 |

 

21 |

Constraints:

22 | 23 |
    24 |
  • 1 <= nums.length <= 105
  • 25 |
  • 0 <= nums[i] <= 109
  • 26 |
27 |
-------------------------------------------------------------------------------- /1641-count-sorted-vowel-strings/1641-count-sorted-vowel-strings.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void helper(int n, vector &temp,int &count, int idx, vector &vowels) 4 | { 5 | if(temp.size()==n) 6 | { 7 | count++; 8 | return; 9 | } 10 | 11 | for(int i=idx; i vowels{'a','e','i','o','u'}; 23 | int count=0; 24 | vector temp; 25 | helper(n, temp, count, 0, vowels); 26 | return count; 27 | } 28 | }; -------------------------------------------------------------------------------- /1641-count-sorted-vowel-strings/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1663-smallest-string-with-a-given-numeric-value/1663-smallest-string-with-a-given-numeric-value.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string getSmallestString(int n, int k) { 4 | string ans; 5 | for(int i=0;i0){ 15 | if(k>25){ 16 | ans[gg]=ans[gg]+25; 17 | gg--; 18 | k-=25; 19 | } else { 20 | ans[gg]=ans[gg]+k; 21 | // updating remaining score to 0 22 | k=0; 23 | } 24 | } 25 | return ans; 26 | } 27 | }; -------------------------------------------------------------------------------- /1663-smallest-string-with-a-given-numeric-value/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1679-max-number-of-k-sum-pairs/1679-max-number-of-k-sum-pairs.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | int maxOperations(vector &num, int k) 5 | { 6 | sort(num.begin(), num.end()); 7 | int st = 0, end = num.size() - 1; 8 | int ans = 0; 9 | while (st < end) 10 | { 11 | if (num[st] + num[end] == k) 12 | { 13 | ans++; 14 | st++; 15 | end--; 16 | } 17 | else if (num[st] + num[end] > k) 18 | { 19 | end--; 20 | } 21 | else 22 | { 23 | st++; 24 | } 25 | } 26 | 27 | return ans; 28 | } 29 | }; -------------------------------------------------------------------------------- /1679-max-number-of-k-sum-pairs/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /168-excel-sheet-column-title/168-excel-sheet-column-title.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string convertToTitle(int x) { 4 | string ans=""; 5 | while(x>0){ 6 | int mod=x%26; 7 | if(mod==0){ 8 | ans+='Z'; 9 | x=x/26; 10 | x--; 11 | continue; 12 | } else { 13 | ans+='A'+mod-1; 14 | } 15 | x=x/26; 16 | } 17 | reverse(ans.begin(), ans.end()); 18 | return ans; 19 | } 20 | }; -------------------------------------------------------------------------------- /168-excel-sheet-column-title/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /168-excel-sheet-column-title/README.md: -------------------------------------------------------------------------------- 1 |

168. Excel Sheet Column Title

Easy


Given an integer columnNumber, return its corresponding column title as it appears in an Excel sheet.

2 | 3 |

For example:

4 | 5 |
A -> 1
 6 | B -> 2
 7 | C -> 3
 8 | ...
 9 | Z -> 26
10 | AA -> 27
11 | AB -> 28 
12 | ...
13 | 
14 | 15 |

 

16 |

Example 1:

17 | 18 |
Input: columnNumber = 1
19 | Output: "A"
20 | 
21 | 22 |

Example 2:

23 | 24 |
Input: columnNumber = 28
25 | Output: "AB"
26 | 
27 | 28 |

Example 3:

29 | 30 |
Input: columnNumber = 701
31 | Output: "ZY"
32 | 
33 | 34 |

 

35 |

Constraints:

36 | 37 |
    38 |
  • 1 <= columnNumber <= 231 - 1
  • 39 |
40 |
-------------------------------------------------------------------------------- /17-letter-combinations-of-a-phone-number/17-letter-combinations-of-a-phone-number.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector ans; 4 | map m; 5 | 6 | 7 | void util(string A, int start, string gg){ 8 | if(start==A.length()){ 9 | ans.push_back(gg); 10 | return ; 11 | } 12 | int x=A[start]-'0'; 13 | string temp=m[x]; 14 | 15 | for(int j=0;j letterCombinations(string digits) { 23 | m[0]="0"; 24 | m[1]="1"; 25 | m[2]="abc"; 26 | m[3]="def"; 27 | m[4]="ghi"; 28 | m[5]="jkl";m[6]="mno"; m[7]="pqrs"; m[8]="tuv";m[9]="wxyz"; 29 | ans.clear(); 30 | if(digits.length()==0){ 31 | return ans; 32 | } 33 | util(digits, 0, ""); 34 | //sort(ans.begin(), ans.end()); 35 | return ans; 36 | } 37 | }; -------------------------------------------------------------------------------- /17-letter-combinations-of-a-phone-number/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /171-excel-sheet-column-number/171-excel-sheet-column-number.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int titleToNumber(string str) { 4 | int n=str.length(); 5 | long long int ans=str[0]-'A'+1; 6 | for(int i=1;i171. Excel Sheet Column Number

Easy


Given a string columnTitle that represents the column title as appear in an Excel sheet, return its corresponding column number.

2 | 3 |

For example:

4 | 5 |
A -> 1
 6 | B -> 2
 7 | C -> 3
 8 | ...
 9 | Z -> 26
10 | AA -> 27
11 | AB -> 28 
12 | ...
13 | 
14 | 15 |

 

16 |

Example 1:

17 | 18 |
Input: columnTitle = "A"
19 | Output: 1
20 | 
21 | 22 |

Example 2:

23 | 24 |
Input: columnTitle = "AB"
25 | Output: 28
26 | 
27 | 28 |

Example 3:

29 | 30 |
Input: columnTitle = "ZY"
31 | Output: 701
32 | 
33 | 34 |

 

35 |

Constraints:

36 | 37 |
    38 |
  • 1 <= columnTitle.length <= 7
  • 39 |
  • columnTitle consists only of uppercase English letters.
  • 40 |
  • columnTitle is in the range ["A", "FXSHRXW"].
  • 41 |
42 |
-------------------------------------------------------------------------------- /172-factorial-trailing-zeroes/172-factorial-trailing-zeroes.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int trailingZeroes(int n) { 4 | int count=0,i=1; 5 | while(n/pow(5,i)>0) count+=n/pow(5,i++); 6 | 7 | return count; 8 | } 9 | }; -------------------------------------------------------------------------------- /172-factorial-trailing-zeroes/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /172-factorial-trailing-zeroes/README.md: -------------------------------------------------------------------------------- 1 |

172. Factorial Trailing Zeroes

Medium


Given an integer n, return the number of trailing zeroes in n!.

2 | 3 |

Note that n! = n * (n - 1) * (n - 2) * ... * 3 * 2 * 1.

4 | 5 |

 

6 |

Example 1:

7 | 8 |
Input: n = 3
 9 | Output: 0
10 | Explanation: 3! = 6, no trailing zero.
11 | 
12 | 13 |

Example 2:

14 | 15 |
Input: n = 5
16 | Output: 1
17 | Explanation: 5! = 120, one trailing zero.
18 | 
19 | 20 |

Example 3:

21 | 22 |
Input: n = 0
23 | Output: 0
24 | 
25 | 26 |

 

27 |

Constraints:

28 | 29 |
    30 |
  • 0 <= n <= 104
  • 31 |
32 | 33 |

 

34 |

Follow up: Could you write a solution that works in logarithmic time complexity?

35 |
-------------------------------------------------------------------------------- /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 | { 13 | public: 14 | ListNode* swapNodes(ListNode *head, int k) 15 | { 16 | ListNode *kfromstart = head, *kfromend = head, *temp = head; 17 | int x = k - 1; 18 | while (x) 19 | { 20 | kfromstart = kfromstart->next; 21 | temp = temp->next; 22 | x--; 23 | } 24 | 25 | while (temp->next) 26 | { 27 | temp = temp->next; 28 | kfromend = kfromend->next; 29 | } 30 | 31 | swap(kfromend->val, kfromstart->val); 32 | return head; 33 | } 34 | }; -------------------------------------------------------------------------------- /1721-swapping-nodes-in-a-linked-list/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1721-swapping-nodes-in-a-linked-list/README.md: -------------------------------------------------------------------------------- 1 |

1721. Swapping Nodes in a Linked List

Medium


You are given the head of a linked list, and an integer k.

2 | 3 |

Return the head of the linked list after swapping the values of the kth node from the beginning and the kth node from the end (the list is 1-indexed).

4 | 5 |

 

6 |

Example 1:

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

Example 2:

13 | 14 |
Input: head = [7,9,6,6,7,8,3,0,9,5], k = 5
15 | Output: [7,9,6,6,8,7,3,0,9,5]
16 | 
17 | 18 |

 

19 |

Constraints:

20 | 21 |
    22 |
  • The number of nodes in the list is n.
  • 23 |
  • 1 <= k <= n <= 105
  • 24 |
  • 0 <= Node.val <= 100
  • 25 |
26 |
-------------------------------------------------------------------------------- /173-binary-search-tree-iterator/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /179-largest-number/179-largest-number.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | static bool oper(int x, int y){ 4 | string X=to_string(x); 5 | string Y=to_string(y); 6 | return X+Y>Y+X; 7 | } 8 | string largestNumber(vector& nums) { 9 | sort(nums.begin(), nums.end(), oper); 10 | string ans; 11 | for(int i=0;i1 && ans[0]=='0'){ 15 | ans=ans.substr(1); 16 | } 17 | return ans; 18 | } 19 | }; -------------------------------------------------------------------------------- /179-largest-number/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /179-largest-number/README.md: -------------------------------------------------------------------------------- 1 |

179. Largest Number

Medium


Given a list of non-negative integers nums, arrange them such that they form the largest number and return it.

2 | 3 |

Since the result may be very large, so you need to return a string instead of an integer.

4 | 5 |

 

6 |

Example 1:

7 | 8 |
Input: nums = [10,2]
 9 | Output: "210"
10 | 
11 | 12 |

Example 2:

13 | 14 |
Input: nums = [3,30,34,5,9]
15 | Output: "9534330"
16 | 
17 | 18 |

 

19 |

Constraints:

20 | 21 |
    22 |
  • 1 <= nums.length <= 100
  • 23 |
  • 0 <= nums[i] <= 109
  • 24 |
25 |
-------------------------------------------------------------------------------- /1793-maximum-score-of-a-good-subarray/1793-maximum-score-of-a-good-subarray.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maximumScore(vector& nums, int k) { 4 | int n=nums.size(); 5 | vectorr(n,n); 6 | vectorl(n,-1); 7 | 8 | for(int i=1;i=0 and nums[j]>=nums[i]) 12 | { 13 | j=l[j]; 14 | } 15 | 16 | l[i]=j; 17 | } 18 | 19 | for(int i=n-2;i>=0;i--) 20 | { 21 | int j=i+1; 22 | while(j=nums[i]) 23 | { 24 | j=r[j]; 25 | } 26 | r[i]=j; 27 | } 28 | int ans=0; 29 | for(int i=0;i=l[i]+1) 33 | { 34 | ans=max(ans,val*(nums[i])); 35 | } 36 | } 37 | return ans; 38 | } 39 | }; -------------------------------------------------------------------------------- /1793-maximum-score-of-a-good-subarray/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /18-4sum/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /18-4sum/README.md: -------------------------------------------------------------------------------- 1 |

18. 4Sum

Medium


Given an array nums of n integers, return an array of all the unique quadruplets [nums[a], nums[b], nums[c], nums[d]] such that:

2 | 3 |
    4 |
  • 0 <= a, b, c, d < n
  • 5 |
  • a, b, c, and d are distinct.
  • 6 |
  • nums[a] + nums[b] + nums[c] + nums[d] == target
  • 7 |
8 | 9 |

You may return the answer in any order.

10 | 11 |

 

12 |

Example 1:

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

Example 2:

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

 

25 |

Constraints:

26 | 27 |
    28 |
  • 1 <= nums.length <= 200
  • 29 |
  • -109 <= nums[i] <= 109
  • 30 |
  • -109 <= target <= 109
  • 31 |
32 |
-------------------------------------------------------------------------------- /189-rotate-array/189-rotate-array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void rotate(vector& nums, int k) { 4 | int n=nums.size(); 5 | if(n==k){ 6 | return ; 7 | } 8 | 9 | if(k>n){ 10 | k=k%n; 11 | } 12 | 13 | 14 | int st=0, end=n-k-1; 15 | while(st>1; 12 | } 13 | 14 | return cnt; 15 | } 16 | }; -------------------------------------------------------------------------------- /191-number-of-1-bits/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /2-add-two-numbers/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /2007-find-original-array-from-doubled-array/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /2013-detect-squares/2013-detect-squares.cpp: -------------------------------------------------------------------------------- 1 | class DetectSquares { 2 | public: 3 | map,int> mp; 4 | 5 | DetectSquares() { 6 | return ; 7 | } 8 | 9 | void add(vector point) { 10 | mp[{point[0],point[1]}]++; 11 | return ; 12 | } 13 | 14 | int count(vector point) { 15 | int count = 0; 16 | int x1= point[0], y1 = point[1]; 17 | for(auto pt : mp){ 18 | pair it = pt.first; 19 | if(it.first!=x1 && it.second!=y1 && (abs(it.first-x1)==abs(it.second-y1))){ 20 | count += mp[{it.first,it.second}]*mp[{it.first,y1}]*mp[{x1,it.second}]; 21 | } 22 | } 23 | return count; 24 | } 25 | }; 26 | 27 | /** 28 | * Your DetectSquares object will be instantiated and called as such: 29 | * DetectSquares* obj = new DetectSquares(); 30 | * obj->add(point); 31 | * int param_2 = obj->count(point); 32 | */ -------------------------------------------------------------------------------- /2013-detect-squares/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /207-course-schedule/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /210-course-schedule-ii/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /2115-find-all-possible-recipes-from-given-supplies/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /212-word-search-ii/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /2135-count-words-obtained-after-adding-a-letter/2135-count-words-obtained-after-adding-a-letter.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | class Solution { 3 | public: 4 | int wordCount(vector& startWords, vector& targetWords) { 5 | map m; 6 | int ans = 0; 7 | for(int i=0;i& nums, int k) { 5 | int left = 0; 6 | int right = nums.size()-1; 7 | int pivotIdx = nums.size(); 8 | 9 | while (pivotIdx != nums.size()-k) { 10 | pivotIdx = partition(nums, left, right); 11 | if (pivotIdx < nums.size()-k) { 12 | left = pivotIdx+1; 13 | } else { 14 | right = pivotIdx-1; 15 | } 16 | } 17 | return nums[pivotIdx]; 18 | } 19 | private: 20 | int partition(vector& nums, int left, int right) { 21 | // 1. we take the middle element 22 | int pivot_idx = right; 23 | int pivot_val = nums[pivot_idx]; 24 | // 3. we swap all elements smaller than pivot to the left 25 | int store_idx = left; 26 | for (int i = left; i < right; ++i) { 27 | if (nums[i] < pivot_val) { 28 | swap(nums[i], nums[store_idx]); 29 | store_idx++; 30 | } 31 | } 32 | // 4. move pivot back to its place 33 | swap(nums[store_idx], nums[pivot_idx]); 34 | return store_idx; 35 | } 36 | }; -------------------------------------------------------------------------------- /215-kth-largest-element-in-an-array/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /215-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 |

 

6 |

Example 1:

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

Example 2:

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

 

14 |

Constraints:

15 | 16 |
    17 |
  • 1 <= k <= nums.length <= 104
  • 18 |
  • -104 <= nums[i] <= 104
  • 19 |
20 |
-------------------------------------------------------------------------------- /216-combination-sum-iii/216-combination-sum-iii.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | void helper(int idx, int sum, vector> &ans, vector< int > &v, int k, int n) 5 | { 6 | 7 | //base case 8 | if (sum == n && k == 0) 9 | { 10 | ans.push_back(v); 11 | return; 12 | } 13 | 14 | if (sum > n) 15 | { 16 | //when sum exceedes n -> return 17 | return; 18 | } 19 | 20 | for (int i = idx; i <= 9; i++) 21 | { 22 | //nos that can be used are from 1 to 9 23 | 24 | v.push_back(i); //if chosen i 25 | helper(i + 1, sum + i, ans, v, k - 1, n); //recursive call -> from i+1 since we do not want to repeat nos 26 | v.pop_back(); //backtrack 27 | } 28 | } 29 | 30 | vector> combinationSum3(int k, int n) 31 | { 32 | vector> ans; 33 | 34 | vector v; 35 | 36 | helper(1, 0, ans, v, k, n); 37 | return ans; 38 | } 39 | }; -------------------------------------------------------------------------------- /216-combination-sum-iii/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /225-implement-stack-using-queues/225-implement-stack-using-queues.cpp: -------------------------------------------------------------------------------- 1 | class MyStack { 2 | public: 3 | /** Initialize your data structure here. */ 4 | queue q1; 5 | queue q2; 6 | 7 | void gg(queue &q){ 8 | if(q.empty()){ 9 | return ; 10 | } 11 | int x=q.front(); 12 | q.pop(); 13 | gg(q); 14 | q.push(x); 15 | 16 | } 17 | MyStack() { 18 | 19 | } 20 | 21 | /** Push element x onto stack. */ 22 | void push(int x) { 23 | q1.push(x); 24 | } 25 | 26 | /** Removes the element on top of the stack and returns that element. */ 27 | int pop() { 28 | 29 | gg(q1); 30 | int x=q1.front(); 31 | q1.pop(); 32 | gg(q1); 33 | return x; 34 | } 35 | 36 | /** Get the top element. */ 37 | int top() { 38 | gg(q1); 39 | int x=q1.front(); 40 | gg(q1); 41 | return x; 42 | } 43 | 44 | /** Returns whether the stack is empty. */ 45 | bool empty() { 46 | return q1.empty(); 47 | } 48 | }; 49 | 50 | /** 51 | * Your MyStack object will be instantiated and called as such: 52 | * MyStack* obj = new MyStack(); 53 | * obj->push(x); 54 | * int param_2 = obj->pop(); 55 | * int param_3 = obj->top(); 56 | * bool param_4 = obj->empty(); 57 | */ -------------------------------------------------------------------------------- /225-implement-stack-using-queues/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /226-invert-binary-tree/226-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 root; 17 | } 18 | queue q; 19 | TreeNode* temp=root; 20 | q.push(temp); 21 | while(!q.empty()){ 22 | int n=q.size(); 23 | while(n--){ 24 | TreeNode* use=q.front(); 25 | q.pop(); 26 | temp=use->left; 27 | use->left=use->right; 28 | use->right=temp; 29 | 30 | if(use->left){ 31 | q.push(use->left); 32 | } 33 | if(use->right){ 34 | q.push(use->right); 35 | } 36 | 37 | } 38 | } 39 | return root; 40 | } 41 | }; -------------------------------------------------------------------------------- /226-invert-binary-tree/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /226-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 |
Input: root = [4,2,7,1,3,6,9]
 7 | Output: [4,7,2,9,6,3,1]
 8 | 
9 | 10 |

Example 2:

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

Example 3:

17 | 18 |
Input: root = []
19 | Output: []
20 | 
21 | 22 |

 

23 |

Constraints:

24 | 25 |
    26 |
  • The number of nodes in the tree is in the range [0, 100].
  • 27 |
  • -100 <= Node.val <= 100
  • 28 |
29 |
-------------------------------------------------------------------------------- /229-majority-element-ii/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /229-majority-element-ii/README.md: -------------------------------------------------------------------------------- 1 |

229. 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 |
Input: nums = [3,2,3]
 7 | Output: [3]
 8 | 
9 | 10 |

Example 2:

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

Example 3:

17 | 18 |
Input: nums = [1,2]
19 | Output: [1,2]
20 | 
21 | 22 |

 

23 |

Constraints:

24 | 25 |
    26 |
  • 1 <= nums.length <= 5 * 104
  • 27 |
  • -109 <= nums[i] <= 109
  • 28 |
29 | 30 |

 

31 |

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

32 |
-------------------------------------------------------------------------------- /23-merge-k-sorted-lists/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /230-kth-smallest-element-in-a-bst/230-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 | int i=1, gg=-1; 15 | void helper(TreeNode* root, int k) { 16 | if(!root || i>k){ 17 | return; 18 | } 19 | helper(root->left, k); 20 | if(i==k){ 21 | gg=root->val; 22 | } 23 | i++; 24 | helper(root->right, k); 25 | 26 | } 27 | int kthSmallest(TreeNode* root, int k) { 28 | helper(root,k); 29 | return gg; 30 | 31 | } 32 | }; -------------------------------------------------------------------------------- /230-kth-smallest-element-in-a-bst/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /231-power-of-two/231-power-of-two.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isPowerOfTwo(int n) { 4 | return n>0 && !(n&(n-1)); 5 | } 6 | }; -------------------------------------------------------------------------------- /231-power-of-two/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /231-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 |
Input: n = 1
 9 | Output: true
10 | Explanation: 20 = 1
11 | 
12 | 13 |

Example 2:

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

Example 3:

21 | 22 |
Input: n = 3
23 | Output: false
24 | 
25 | 26 |

 

27 |

Constraints:

28 | 29 |
    30 |
  • -231 <= n <= 231 - 1
  • 31 |
32 | 33 |

 

34 | Follow up: Could you solve it without loops/recursion?
-------------------------------------------------------------------------------- /235-lowest-common-ancestor-of-a-binary-search-tree/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /238-product-of-array-except-self/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /239-sliding-window-maximum/239-sliding-window-maximum.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector maxSlidingWindow(vector& nums, int k) { 4 | int w=k; 5 | vector A=nums; 6 | int n = A.size(); 7 | vector B; 8 | if (n < w) return B; 9 | B.resize(n - w + 1); 10 | deque Q; 11 | for (int i = 0; i < w; i++) { 12 | while (!Q.empty() && A[i] >= A[Q.back()]) 13 | Q.pop_back(); 14 | Q.push_back(i); 15 | } 16 | for (int i = w; i < n; i++) { 17 | B[i - w] = A[Q.front()]; 18 | while (!Q.empty() && A[i] >= A[Q.back()]) 19 | Q.pop_back(); 20 | while (!Q.empty() && Q.front() <= i - w) 21 | Q.pop_front(); 22 | Q.push_back(i); 23 | } 24 | B[n - w] = A[Q.front()]; 25 | return B; 26 | } 27 | }; -------------------------------------------------------------------------------- /239-sliding-window-maximum/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /25-reverse-nodes-in-k-group/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /26-remove-duplicates-from-sorted-array/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /268-missing-number/268-missing-number.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int missingNumber(vector& nums) { 4 | int n=nums.size(); 5 | long long int sum=(n*(n+1))/2; 6 | long long int temp=0; 7 | for(int i=0;i283. 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?
-------------------------------------------------------------------------------- /284-peeking-iterator/284-peeking-iterator.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | *Below is the interface for Iterator, which is already defined for you. 3 | ***DO NOT**modify the interface for Iterator. 4 | * 5 | *class Iterator { 6 | * struct Data; 7 | * Data* data; 8 | *public: 9 | * Iterator(const vector& nums); 10 | * Iterator(const Iterator& iter); 11 | * 12 | * // Returns the next element in the iteration. 13 | * int next(); 14 | * 15 | * // Returns true if the iteration has more elements. 16 | * bool hasNext() const; 17 | * }; 18 | */ 19 | 20 | class PeekingIterator: public Iterator 21 | { 22 | public: int i = 0, 23 | n; 24 | vector ans; 25 | PeekingIterator(const vector &nums): Iterator(nums) 26 | { 27 | n = nums.size(); 28 | ans = nums; 29 | } 30 | 31 | // Returns the next element in the iteration without advancing the iterator. 32 | int peek() { 33 | 34 | if(i& nums) { 4 | int fast=nums[0]; 5 | int slow=nums[0]; 6 | do{ 7 | fast=nums[nums[fast]]; 8 | slow=nums[slow]; 9 | } while(slow!=fast); 10 | slow=nums[0]; 11 | while(slow!=fast){ 12 | slow=nums[slow]; 13 | fast=nums[fast]; 14 | } 15 | return slow; 16 | } 17 | }; -------------------------------------------------------------------------------- /287-find-the-duplicate-number/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /289-game-of-life/289-game-of-life.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void gameOfLife(vector>& board) { 4 | int n=board.size(); 5 | int m=board[0].size(); 6 | vector> gg(n, vector(m)); 7 | for(int i=0;i=0 && xind=0 && yind3)){ 28 | gg[i][j]=0; 29 | } else if(board[i][j]==1 && (nei==2 || nei==3)) { 30 | gg[i][j]=1; 31 | } else if(board[i][j]==0 && nei==3){ 32 | gg[i][j]=1; 33 | } 34 | } 35 | } 36 | board=gg; 37 | } 38 | }; -------------------------------------------------------------------------------- /289-game-of-life/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /29-divide-two-integers/29-divide-two-integers.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int divide(int dividend, int divisor) { 4 | long long n=dividend, m=divisor; 5 | if(n <= INT_MIN && m == -1) return INT_MAX; 6 | 7 | int sign = (n < 0) ^ (m < 0) ? -1: 1; 8 | 9 | n=abs(n); 10 | m=abs(m); 11 | 12 | long long q=0, temp=0; 13 | 14 | for(int i=31; i>=0; i--){ 15 | if(temp + (m << i) <= n){ 16 | temp += m << i; 17 | q += 1 << i; // q |= 1 << i; 18 | } 19 | } 20 | return sign * q; 21 | } 22 | }; -------------------------------------------------------------------------------- /29-divide-two-integers/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /3-longest-substring-without-repeating-characters/3-longest-substring-without-repeating-characters.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int lengthOfLongestSubstring(string s) { 4 | map ans; 5 | int maxlen=0; 6 | int currlen=0; 7 | int n=s.length(); 8 | if(n<=1){ 9 | return n; 10 | } 11 | int st=0; 12 | int i=0; 13 | while(i1){ 16 | while(ans[s[i]]>1){ 17 | ans[s[st]]--; 18 | st++; 19 | } 20 | } 21 | currlen=i-st+1; 22 | maxlen=max(maxlen, currlen); 23 | i++; 24 | } 25 | return maxlen; 26 | } 27 | }; -------------------------------------------------------------------------------- /3-longest-substring-without-repeating-characters/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /3-longest-substring-without-repeating-characters/README.md: -------------------------------------------------------------------------------- 1 |

3. Longest Substring Without Repeating Characters

Medium


Given a string s, find the length of the longest substring without repeating characters.

2 | 3 |

 

4 |

Example 1:

5 | 6 |
Input: s = "abcabcbb"
 7 | Output: 3
 8 | Explanation: The answer is "abc", with the length of 3.
 9 | 
10 | 11 |

Example 2:

12 | 13 |
Input: s = "bbbbb"
14 | Output: 1
15 | Explanation: The answer is "b", with the length of 1.
16 | 
17 | 18 |

Example 3:

19 | 20 |
Input: s = "pwwkew"
21 | Output: 3
22 | Explanation: The answer is "wke", with the length of 3.
23 | Notice that the answer must be a substring, "pwke" is a subsequence and not a substring.
24 | 
25 | 26 |

 

27 |

Constraints:

28 | 29 |
    30 |
  • 0 <= s.length <= 5 * 104
  • 31 |
  • s consists of English letters, digits, symbols and spaces.
  • 32 |
33 |
-------------------------------------------------------------------------------- /30-substring-with-concatenation-of-all-words/30-substring-with-concatenation-of-all-words.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | vector findSubstring(string s, vector &words) 5 | { 6 | vector ans; 7 | int n = words.size(); 8 | int m = words[0].size(); 9 | if (s.size() < m *n) 10 | { 11 | return ans; 12 | } 13 | for (int i = 0; i <= s.size() - (m *n); i++) 14 | { 15 | unordered_map mp; 16 | for (int j = 0; j < n; j++) 17 | { 18 | mp[words[j]]++; 19 | } 20 | int t; 21 | for (t = 0; t < n; t++) 22 | { 23 | string str = s.substr(i + t *m, m); 24 | if (mp[str] > 0) 25 | { 26 | mp[str]--; 27 | } 28 | else 29 | { 30 | break; 31 | } 32 | } 33 | if (t == n) 34 | { 35 | ans.push_back(i); 36 | } 37 | } 38 | return ans; 39 | } 40 | }; -------------------------------------------------------------------------------- /30-substring-with-concatenation-of-all-words/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /304-range-sum-query-2d-immutable/304-range-sum-query-2d-immutable.cpp: -------------------------------------------------------------------------------- 1 | class NumMatrix { 2 | public: 3 | vector>pre; 4 | NumMatrix(vector>& matrix) { 5 | int r=matrix.size(); 6 | int c=matrix[0].size(); 7 | pre.resize(r+1,vector(c+1,0)); 8 | for (int i=1; i<=r; i++){ 9 | for (int j=1; j<=c; j++){ 10 | pre[i][j]=matrix[i-1][j-1]+pre[i][j-1]+pre[i-1][j]-pre[i-1][j-1]; 11 | } 12 | } 13 | } 14 | 15 | int sumRegion(int row1, int col1, int row2, int col2) { 16 | return pre[row2+1][col2+1]-pre[row1-1+1][col2+1]-pre[row2+1][col1-1+1]+pre[row1-1+1][col1-1+1]; 17 | } 18 | }; 19 | 20 | /** 21 | * Your NumMatrix object will be instantiated and called as such: 22 | * NumMatrix* obj = new NumMatrix(matrix); 23 | * int param_1 = obj->sumRegion(row1,col1,row2,col2); 24 | */ -------------------------------------------------------------------------------- /304-range-sum-query-2d-immutable/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /31-next-permutation/31-next-permutation.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | void nextPermutation(vector &nums) 5 | { 6 | int i = nums.size() - 1; 7 | for (; i > 0; i--) 8 | { 9 | if (nums[i - 1] < nums[i]) 10 | { 11 | break; 12 | } 13 | } 14 | if (i == 0) 15 | { 16 | sort(nums.begin(), nums.end()); 17 | return; 18 | } 19 | int num = nums[i - 1]; 20 | int index = nums.size()-1; 21 | while(i-1=nums[index]){ 22 | index--; 23 | } 24 | swap(nums[i - 1], nums[index]); 25 | reverse(nums.begin() + i, nums.end()); 26 | } 27 | }; -------------------------------------------------------------------------------- /31-next-permutation/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /316-remove-duplicate-letters/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /316-remove-duplicate-letters/README.md: -------------------------------------------------------------------------------- 1 |

316. Remove Duplicate Letters

Medium


Given a string s, remove duplicate letters so that every letter appears once and only once. You must make sure your result is the smallest in lexicographical order among all possible results.

2 | 3 |

 

4 |

Example 1:

5 | 6 |
Input: s = "bcabc"
 7 | Output: "abc"
 8 | 
9 | 10 |

Example 2:

11 | 12 |
Input: s = "cbacdcbc"
13 | Output: "acdb"
14 | 
15 | 16 |

 

17 |

Constraints:

18 | 19 |
    20 |
  • 1 <= s.length <= 104
  • 21 |
  • s consists of lowercase English letters.
  • 22 |
23 | 24 |

 

25 |

Note: This question is the same as 1081: https://leetcode.com/problems/smallest-subsequence-of-distinct-characters/

26 |
-------------------------------------------------------------------------------- /318-maximum-product-of-word-lengths/318-maximum-product-of-word-lengths.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | class Solution { 3 | public: 4 | int maxProduct(vector& words) { 5 | int n=words.size(); 6 | int ans=0; 7 | vector state(n,0); 8 | for(int i=0;i ans){ 21 | ans=words[i].length()*words[j].length(); 22 | } 23 | 24 | } 25 | } 26 | } 27 | 28 | return ans; 29 | } 30 | }; -------------------------------------------------------------------------------- /318-maximum-product-of-word-lengths/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /32-longest-valid-parentheses/32-longest-valid-parentheses.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int longestValidParentheses(string s) { 4 | 5 | stackst; 6 | st.push(-1); 7 | int ans=0; 8 | 9 | for(int i=0; i32. Longest Valid Parentheses

Hard


Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring.

2 | 3 |

 

4 |

Example 1:

5 | 6 |
Input: s = "(()"
 7 | Output: 2
 8 | Explanation: The longest valid parentheses substring is "()".
 9 | 
10 | 11 |

Example 2:

12 | 13 |
Input: s = ")()())"
14 | Output: 4
15 | Explanation: The longest valid parentheses substring is "()()".
16 | 
17 | 18 |

Example 3:

19 | 20 |
Input: s = ""
21 | Output: 0
22 | 
23 | 24 |

 

25 |

Constraints:

26 | 27 |
    28 |
  • 0 <= s.length <= 3 * 104
  • 29 |
  • s[i] is '(', or ')'.
  • 30 |
31 |
-------------------------------------------------------------------------------- /322-coin-change/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /329-longest-increasing-path-in-a-matrix/329-longest-increasing-path-in-a-matrix.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int dp[200][200]; // constraints are small enough that we can just set them to MAX 4 | int cnt, n, m; 5 | int longestIncreasingPath(vector>& matrix) { 6 | cnt = 0, n = size(matrix), m = size(matrix[0]); 7 | for(int i = 0; i < n; i++) 8 | for(int j = 0; j < m; j++) 9 | cnt = max(cnt, solve(matrix, i, j, -1)); 10 | return cnt; 11 | } 12 | int solve(vector>& mat, int i, int j, int prev){ 13 | if(i < 0 || j < 0 || i >= n || j >= m || mat[i][j] <= prev) return 0; 14 | if(dp[i][j]) return dp[i][j]; 15 | return dp[i][j] = 1 + max({ solve(mat, i + 1, j, mat[i][j]), 16 | solve(mat, i - 1, j, mat[i][j]), 17 | solve(mat, i, j + 1, mat[i][j]), 18 | solve(mat, i, j - 1, mat[i][j]) }); 19 | } 20 | }; -------------------------------------------------------------------------------- /329-longest-increasing-path-in-a-matrix/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /33-search-in-rotated-sorted-array/33-search-in-rotated-sorted-array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | 4 | int search(vector& nums, int target) { 5 | int low=0,high=nums.size()-1; 6 | 7 | while(low<=high) 8 | { 9 | int mid=(low+high)/2; 10 | if(nums[mid]==target) 11 | return mid; 12 | 13 | if(nums[mid]>=nums[low])//left side is sorted 14 | { 15 | if(target=nums[low])//if left side contains the target (range check) 16 | high=mid-1; 17 | else//if doesn't contains target, goto right half of the array 18 | low=mid+1; 19 | } 20 | else//right side is sorted 21 | { 22 | if(target<=nums[high] && target>nums[mid])//if right side contains target 23 | low=mid+1; 24 | else//else goto left part 25 | high=mid-1; 26 | } 27 | } 28 | return -1; 29 | } 30 | }; -------------------------------------------------------------------------------- /33-search-in-rotated-sorted-array/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /341-flatten-nested-list-iterator/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /344-reverse-string/344-reverse-string.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void reverseString(vector& s) { 4 | int n=s.size()-1, st=0; 5 | while(st<=n) swap(s[n--], s[st++]); 6 | } 7 | }; -------------------------------------------------------------------------------- /344-reverse-string/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /344-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 |
-------------------------------------------------------------------------------- /347-top-k-frequent-elements/347-top-k-frequent-elements.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector topKFrequent(vector& nums, int k) { 4 | int n = nums.size(); 5 | unordered_map mp; 6 | for(auto it: nums){ 7 | mp[it]++; 8 | } 9 | priority_queue> pq; 10 | for(auto i: mp){ 11 | int num = i.first; 12 | int freq = i.second; 13 | pq.push({freq, num}); 14 | } 15 | vector vv; 16 | while(k--){ 17 | vv.push_back(pq.top().second); 18 | pq.pop(); 19 | } 20 | return vv; 21 | } 22 | }; -------------------------------------------------------------------------------- /347-top-k-frequent-elements/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /347-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 |
  • k is in the range [1, the number of unique elements in the array].
  • 17 |
  • It is guaranteed that the answer is unique.
  • 18 |
19 | 20 |

 

21 |

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

22 |
-------------------------------------------------------------------------------- /37-sudoku-solver/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /38-count-and-say/38-count-and-say.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string countAndSay(int n) { 4 | string ans="1"; 5 | if(n==1){ 6 | return ans; 7 | } 8 | for(int i=1;i4. Median of Two Sorted Arrays

Hard


Given two sorted arrays nums1 and nums2 of size m and n respectively, return the median of the two sorted arrays.

2 | 3 |

The overall run time complexity should be O(log (m+n)).

4 | 5 |

 

6 |

Example 1:

7 | 8 |
Input: nums1 = [1,3], nums2 = [2]
 9 | Output: 2.00000
10 | Explanation: merged array = [1,2,3] and median is 2.
11 | 
12 | 13 |

Example 2:

14 | 15 |
Input: nums1 = [1,2], nums2 = [3,4]
16 | Output: 2.50000
17 | Explanation: merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5.
18 | 
19 | 20 |

 

21 |

Constraints:

22 | 23 |
    24 |
  • nums1.length == m
  • 25 |
  • nums2.length == n
  • 26 |
  • 0 <= m <= 1000
  • 27 |
  • 0 <= n <= 1000
  • 28 |
  • 1 <= m + n <= 2000
  • 29 |
  • -106 <= nums1[i], nums2[i] <= 106
  • 30 |
31 |
-------------------------------------------------------------------------------- /404-sum-of-left-leaves/404-sum-of-left-leaves.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | *Definition for a binary tree node. 3 | *struct TreeNode { 4 | * int val; 5 | * TreeNode * left; 6 | * TreeNode * right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | *}; 11 | */ 12 | class Solution 13 | { 14 | public: 15 | int sumOfLeftLeaves(TreeNode *root) 16 | { 17 | if (!root) 18 | { 19 | return 0; 20 | } 21 | 22 | if (root->left) 23 | { 24 | if (!root->left->left && !root->left-> right) 25 | { 26 | return root->left->val + sumOfLeftLeaves(root->left) + sumOfLeftLeaves(root->right); 27 | } 28 | } 29 | return sumOfLeftLeaves(root->left) + sumOfLeftLeaves(root->right); 30 | } 31 | }; -------------------------------------------------------------------------------- /404-sum-of-left-leaves/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /404-sum-of-left-leaves/README.md: -------------------------------------------------------------------------------- 1 |

404. Sum of Left Leaves

Easy


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

2 | 3 |

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

4 | 5 |

 

6 |

Example 1:

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

Example 2:

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

 

20 |

Constraints:

21 | 22 |
    23 |
  • The number of nodes in the tree is in the range [1, 1000].
  • 24 |
  • -1000 <= Node.val <= 1000
  • 25 |
26 |
-------------------------------------------------------------------------------- /41-first-missing-positive/41-first-missing-positive.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | int firstMissingPositive(vector &nums) 5 | { 6 | 7 | auto i = 0; 8 | int n = nums.size(); 9 | for (int i = 0; i < n; i++) 10 | { 11 | if (nums[i] <= 0 || nums[i] > n) 12 | { 13 | continue; 14 | } 15 | else 16 | { 17 | int chair = nums[i] - 1; 18 | int element = nums[i]; 19 | if (nums[chair] != element) 20 | { 21 | swap(nums[chair], nums[i--]); 22 | } 23 | } 24 | } 25 | for (int i = 0; i < n; i++) 26 | { 27 | if (nums[i]!=i+1) 28 | { 29 | return i+1; 30 | } 31 | 32 | } 33 | return n+1; 34 | } 35 | }; -------------------------------------------------------------------------------- /41-first-missing-positive/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /41-first-missing-positive/README.md: -------------------------------------------------------------------------------- 1 |

41. First Missing Positive

Hard


Given an unsorted integer array nums, return the smallest missing positive integer.

2 | 3 |

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

4 | 5 |

 

6 |

Example 1:

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

Example 2:

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

Example 3:

13 |
Input: nums = [7,8,9,11,12]
14 | Output: 1
15 | 
16 |

 

17 |

Constraints:

18 | 19 |
    20 |
  • 1 <= nums.length <= 5 * 105
  • 21 |
  • -231 <= nums[i] <= 231 - 1
  • 22 |
23 |
-------------------------------------------------------------------------------- /410-split-array-largest-sum/410-split-array-largest-sum.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int splitArray(vector& nums, int m) { 4 | int low = 0; 5 | int high = 0; 6 | int sum = 0; 7 | 8 | for(int i = 0; i < nums.size(); i++) { 9 | high += nums[i]; 10 | low = max(low, nums[i]); 11 | } 12 | 13 | while(low <= high) { 14 | int mid = low + (high - low) / 2; 15 | if(canSplit(nums, mid, m)) { 16 | high = mid - 1; 17 | } 18 | else { 19 | low = mid + 1; 20 | } 21 | } 22 | return low; 23 | } 24 | 25 | bool canSplit(vector& nums, int maxSum, int m) { 26 | int totalPart = 0; 27 | int currSum = 0; 28 | 29 | for(int i = 0; i < nums.size(); i++) { 30 | if(currSum + nums[i] <= maxSum) { 31 | currSum += nums[i]; 32 | } 33 | else { 34 | currSum = nums[i]; 35 | totalPart++; 36 | } 37 | } 38 | return (totalPart + 1) <= m; 39 | } 40 | }; -------------------------------------------------------------------------------- /410-split-array-largest-sum/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /412-fizz-buzz/412-fizz-buzz.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector fizzBuzz(int n) { 4 | vector ans; 5 | for(int i=1;i<=n;i++){ 6 | if(i%15==0){ 7 | ans.push_back("FizzBuzz"); 8 | } else if(i%5==0){ 9 | ans.push_back("Buzz"); 10 | } else if(i%3==0){ 11 | ans.push_back("Fizz"); 12 | } else { 13 | ans.push_back(to_string(i)); 14 | } 15 | } 16 | return ans; 17 | } 18 | }; -------------------------------------------------------------------------------- /412-fizz-buzz/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /412-fizz-buzz/README.md: -------------------------------------------------------------------------------- 1 |

412. Fizz Buzz

Easy


Given an integer n, return a string array answer (1-indexed) where:

2 | 3 |
    4 |
  • answer[i] == "FizzBuzz" if i is divisible by 3 and 5.
  • 5 |
  • answer[i] == "Fizz" if i is divisible by 3.
  • 6 |
  • answer[i] == "Buzz" if i is divisible by 5.
  • 7 |
  • answer[i] == i (as a string) if none of the above conditions are true.
  • 8 |
9 | 10 |

 

11 |

Example 1:

12 |
Input: n = 3
13 | Output: ["1","2","Fizz"]
14 | 

Example 2:

15 |
Input: n = 5
16 | Output: ["1","2","Fizz","4","Buzz"]
17 | 

Example 3:

18 |
Input: n = 15
19 | Output: ["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz"]
20 | 
21 |

 

22 |

Constraints:

23 | 24 |
    25 |
  • 1 <= n <= 104
  • 26 |
27 |
-------------------------------------------------------------------------------- /42-trapping-rain-water/42-trapping-rain-water.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int trap(vector& height) { 4 | int n=height.size(); 5 | vector left(n,INT_MAX); 6 | vector right(n,INT_MAX); 7 | 8 | int maxans=0; 9 | for(int i=0;i=0;i--){ 17 | if(i==n-1){ 18 | right[i]=height[i]; 19 | } else { 20 | right[i]=max(height[i], right[i+1]); 21 | } 22 | } 23 | for(int i=0;i42. 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 |
Input: height = [0,1,0,2,1,0,1,3,2,1,2,1]
 7 | Output: 6
 8 | 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.
 9 | 
10 | 11 |

Example 2:

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

 

18 |

Constraints:

19 | 20 |
    21 |
  • n == height.length
  • 22 |
  • 1 <= n <= 2 * 104
  • 23 |
  • 0 <= height[i] <= 105
  • 24 |
25 |
-------------------------------------------------------------------------------- /44-wildcard-matching/44-wildcard-matching.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | bool isMatch(string &s, string &p) 5 | { 6 | if (p.size() == 0) return s.size() == 0; 7 | 8 | int x = s.size(), y = p.size(); 9 | vector> t(x + 1, vector (y + 1, 0)); 10 | 11 | t[0][0] = 1; 12 | 13 | for (int i = 1; i <= p.size(); i++) 14 | { 15 | if (p[i - 1] == '*') t[0][i] = 1; 16 | else break; 17 | } 18 | 19 | for (int i = 1; i <= s.size(); i++) 20 | { 21 | for (int j = 1; j <= p.size(); j++) 22 | { 23 | if (p[j - 1] == '*') t[i][j] = t[i - 1][j] || t[i][j - 1]; 24 | else if (p[j - 1] == '?' || p[j - 1] == s[i - 1]) 25 | { 26 | t[i][j] = t[i - 1][j - 1]; 27 | } 28 | } 29 | } 30 | return t[x][y]; 31 | } 32 | }; -------------------------------------------------------------------------------- /44-wildcard-matching/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /456-132-pattern/456-132-pattern.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool find132pattern(vector& nums) { 4 | stack stk; 5 | int min_el = INT_MIN; 6 | for(int i = nums.size() - 1 ; i >= 0 ; i--) 7 | { 8 | if(nums[i] < min_el) return true; 9 | 10 | while(!stk.empty() && nums[i] > stk.top()) 11 | { 12 | min_el = stk.top(); 13 | stk.pop(); 14 | } 15 | stk.push(nums[i]); 16 | } 17 | return false; 18 | } 19 | }; -------------------------------------------------------------------------------- /456-132-pattern/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /47-permutations-ii/47-permutations-ii.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | set> ans; 4 | void helper(vector& nums, vector &temp, int start, int n){ 5 | if(start==n-1){ 6 | ans.insert(nums); 7 | return ; 8 | } 9 | 10 | for(int i=start;i> permuteUnique(vector& nums) { 21 | int n=nums.size(); 22 | vector temp; 23 | helper(nums, temp, 0, n); 24 | vector> res; 25 | for(auto x: ans){ 26 | res.push_back(x); 27 | } 28 | 29 | return res; 30 | } 31 | }; -------------------------------------------------------------------------------- /47-permutations-ii/README.md: -------------------------------------------------------------------------------- 1 |

47. Permutations II

Medium


Given a collection of numbers, nums, that might contain duplicates, return all possible unique permutations in any order.

2 | 3 |

 

4 |

Example 1:

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

Example 2:

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

 

20 |

Constraints:

21 | 22 |
    23 |
  • 1 <= nums.length <= 8
  • 24 |
  • -10 <= nums[i] <= 10
  • 25 |
26 |
-------------------------------------------------------------------------------- /474-ones-and-zeroes/474-ones-and-zeroes.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findMaxForm(vector& strs, int m, int n) { 4 | vector>dp(m+1,vector(n+1,0)); 5 | for(int i=0;i=z;j--) 17 | { 18 | for(int k=n;k>=o;k--) 19 | { 20 | dp[j][k]=max(dp[j][k],dp[j-z][k-o]+1); 21 | } 22 | } 23 | } 24 | return dp[m][n]; 25 | } 26 | }; -------------------------------------------------------------------------------- /474-ones-and-zeroes/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /477-total-hamming-distance/477-total-hamming-distance.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | 5 | int totalHammingDistance(vector &nums) 6 | { 7 | int n = nums.size(); 8 | long long int count = 0; 9 | for (int i = 0; i < 32; i++) 10 | { 11 | long long int count0 = 0; 12 | long long int count1 = 0; 13 | for (int j = 0; j < n; j++) 14 | { 15 | int x = nums[j]; 16 | if (x &(1 << i)) 17 | { 18 | count1++; 19 | } 20 | else 21 | { 22 | count0++; 23 | } 24 | } 25 | count += count1 * count0; 26 | } 27 | return (int) count; 28 | } 29 | }; -------------------------------------------------------------------------------- /477-total-hamming-distance/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /48-rotate-image/48-rotate-image.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void rotate(vector>& matrix) { 4 | int n=matrix.size(); 5 | 6 | for(int i=0;i5. Longest Palindromic Substring

Medium


Given a string s, return the longest palindromic substring in s.

2 | 3 |

 

4 |

Example 1:

5 | 6 |
Input: s = "babad"
 7 | Output: "bab"
 8 | Explanation: "aba" is also a valid answer.
 9 | 
10 | 11 |

Example 2:

12 | 13 |
Input: s = "cbbd"
14 | Output: "bb"
15 | 
16 | 17 |

 

18 |

Constraints:

19 | 20 |
    21 |
  • 1 <= s.length <= 1000
  • 22 |
  • s consist of only digits and English letters.
  • 23 |
24 |
-------------------------------------------------------------------------------- /509-fibonacci-number/509-fibonacci-number.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int fib(int n) { 4 | if(n<=1){ 5 | return n; 6 | } 7 | int a=0, b=1, c=1; 8 | while(n>1){ 9 | c=a+b; 10 | a=b; 11 | b=c; 12 | n--; 13 | } 14 | return c; 15 | } 16 | }; -------------------------------------------------------------------------------- /509-fibonacci-number/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /509-fibonacci-number/README.md: -------------------------------------------------------------------------------- 1 |

509. Fibonacci Number

Easy


The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1. That is,

2 | 3 |
F(0) = 0, F(1) = 1
 4 | F(n) = F(n - 1) + F(n - 2), for n > 1.
 5 | 
6 | 7 |

Given n, calculate F(n).

8 | 9 |

 

10 |

Example 1:

11 | 12 |
Input: n = 2
13 | Output: 1
14 | Explanation: F(2) = F(1) + F(0) = 1 + 0 = 1.
15 | 
16 | 17 |

Example 2:

18 | 19 |
Input: n = 3
20 | Output: 2
21 | Explanation: F(3) = F(2) + F(1) = 1 + 1 = 2.
22 | 
23 | 24 |

Example 3:

25 | 26 |
Input: n = 4
27 | Output: 3
28 | Explanation: F(4) = F(3) + F(2) = 2 + 1 = 3.
29 | 
30 | 31 |

 

32 |

Constraints:

33 | 34 |
    35 |
  • 0 <= n <= 30
  • 36 |
37 |
-------------------------------------------------------------------------------- /51-n-queens/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /51-n-queens/README.md: -------------------------------------------------------------------------------- 1 |

51. N-Queens

Hard


The n-queens puzzle is the problem of placing n queens on an n x n chessboard such that no two queens attack each other.

2 | 3 |

Given an integer n, return all distinct solutions to the n-queens puzzle. You may return the answer in any order.

4 | 5 |

Each solution contains a distinct board configuration of the n-queens' placement, where 'Q' and '.' both indicate a queen and an empty space, respectively.

6 | 7 |

 

8 |

Example 1:

9 | 10 |
Input: n = 4
11 | Output: [[".Q..","...Q","Q...","..Q."],["..Q.","Q...","...Q",".Q.."]]
12 | Explanation: There exist two distinct solutions to the 4-queens puzzle as shown above
13 | 
14 | 15 |

Example 2:

16 | 17 |
Input: n = 1
18 | Output: [["Q"]]
19 | 
20 | 21 |

 

22 |

Constraints:

23 | 24 |
    25 |
  • 1 <= n <= 9
  • 26 |
27 |
-------------------------------------------------------------------------------- /53-maximum-subarray/53-maximum-subarray.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxSubArray(vector& nums) { 4 | int maxtillnow=nums[0], maxans=nums[0], n=nums.size(); 5 | 6 | for(int i=1;imaxtillnow){ 9 | maxtillnow=nums[i]; 10 | } 11 | maxans=max(maxans, maxtillnow); 12 | } 13 | 14 | return maxans; 15 | } 16 | }; -------------------------------------------------------------------------------- /53-maximum-subarray/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /532-k-diff-pairs-in-an-array/532-k-diff-pairs-in-an-array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findPairs(vector& nums, int k) { 4 | unordered_map mp; 5 | for(auto it:nums){ 6 | mp[it]++; 7 | } 8 | int ans = 0; 9 | for(auto it:mp){ 10 | int findd = it.first + k; 11 | if(mp.find(findd) != mp.end()){ 12 | if(findd == it.first && mp[findd] > 1){ 13 | ans++; 14 | }else if(findd != it.first){ 15 | ans++; 16 | } 17 | } 18 | } 19 | return ans; 20 | 21 | } 22 | }; -------------------------------------------------------------------------------- /532-k-diff-pairs-in-an-array/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /535-encode-and-decode-tinyurl/535-encode-and-decode-tinyurl.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | 4 | 5 | mapmpp; 6 | string key = "1"; 7 | string encode(string longUrl) { 8 | mpp[key] = longUrl; 9 | string encode = key; 10 | key +='x'; 11 | return encode; 12 | } 13 | 14 | // Decodes a shortened URL to its original URL. 15 | string decode(string shortUrl) { 16 | return mpp[shortUrl]; 17 | } 18 | }; 19 | 20 | // Your Solution object will be instantiated and called as such: 21 | // Solution solution; 22 | // solution.decode(solution.encode(url)); -------------------------------------------------------------------------------- /535-encode-and-decode-tinyurl/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /538-convert-bst-to-greater-tree/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /547-number-of-provinces/547-number-of-provinces.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | 4 | void helper(int i, vector> isConnected, vector &vis){ 5 | 6 | vis[i]=true; 7 | for (int j = 0; j < vis.size(); j++) { 8 | if (i != j && isConnected[i][j] && !vis[j]) { 9 | helper(j, isConnected, vis); 10 | } 11 | } 12 | } 13 | 14 | 15 | int findCircleNum(vector>& isConnected) { 16 | int n=isConnected.size(); 17 | vector vis(n,false); 18 | int count=0; 19 | for(int i=0;i& A) { 4 | int i = 0; 5 | int n=A.size(); 6 | for (int reach = 0; i < n && i <= reach; ++i) 7 | reach = max(i + A[i], reach); 8 | return i == n; 9 | } 10 | }; -------------------------------------------------------------------------------- /55-jump-game/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /55-jump-game/README.md: -------------------------------------------------------------------------------- 1 |

55. Jump Game

Medium


You are given an integer array nums. You are initially positioned at the array's first index, and each element in the array represents your maximum jump length at that position.

2 | 3 |

Return true if you can reach the last index, or false otherwise.

4 | 5 |

 

6 |

Example 1:

7 | 8 |
Input: nums = [2,3,1,1,4]
 9 | Output: true
10 | Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index.
11 | 
12 | 13 |

Example 2:

14 | 15 |
Input: nums = [3,2,1,0,4]
16 | Output: false
17 | Explanation: You will always arrive at index 3 no matter what. Its maximum jump length is 0, which makes it impossible to reach the last index.
18 | 
19 | 20 |

 

21 |

Constraints:

22 | 23 |
    24 |
  • 1 <= nums.length <= 104
  • 25 |
  • 0 <= nums[i] <= 105
  • 26 |
27 |
-------------------------------------------------------------------------------- /56-merge-intervals/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /56-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 |
Input: intervals = [[1,3],[2,6],[8,10],[15,18]]
 7 | Output: [[1,6],[8,10],[15,18]]
 8 | Explanation: Since intervals [1,3] and [2,6] overlaps, merge them into [1,6].
 9 | 
10 | 11 |

Example 2:

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

 

19 |

Constraints:

20 | 21 |
    22 |
  • 1 <= intervals.length <= 104
  • 23 |
  • intervals[i].length == 2
  • 24 |
  • 0 <= starti <= endi <= 104
  • 25 |
26 |
-------------------------------------------------------------------------------- /560-subarray-sum-equals-k/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /560-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 |
-------------------------------------------------------------------------------- /57-insert-interval/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /572-subtree-of-another-tree/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /58-length-of-last-word/58-length-of-last-word.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int lengthOfLastWord(string s) { 4 | string ans; 5 | int n=s.length()-1; 6 | while(n>=0 && s[n]==' '){ 7 | n--; 8 | } 9 | while(n>=0 && s[n]!=' '){ 10 | ans+=s[n]; 11 | n--; 12 | } 13 | return ans.length(); 14 | 15 | } 16 | }; -------------------------------------------------------------------------------- /58-length-of-last-word/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /581-shortest-unsorted-continuous-subarray/581-shortest-unsorted-continuous-subarray.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findUnsortedSubarray(vector& arr) { 4 | int maxim=INT_MIN; 5 | int minim=INT_MAX; 6 | int n=arr.size(); 7 | if(n==1){ 8 | return 0; 9 | } 10 | for(int i=0;iarr[i+1]){ 13 | maxim=max(maxim, arr[i]); 14 | minim=min(minim, arr[i]); 15 | } 16 | } else if(i==n-1){ 17 | if(arr[i-1]>arr[i]){ 18 | maxim=max(maxim, arr[i]); 19 | minim=min(minim, arr[i]); 20 | } 21 | } else { 22 | if(arr[i]>arr[i+1] || arr[i]=0 && arr[j]>=maxim;j--); 34 | // cout<> generateMatrix(int n) { 4 | int count=1; 5 | vector> ans(n, vector(n,0)); 6 | int rowStart=0,rowEnd=n-1, colStart=0, colEnd=n-1; 7 | while(count<=n*n){ 8 | for(int i=colStart;i<=colEnd;i++){ 9 | ans[rowStart][i]=count; 10 | count++; 11 | } 12 | rowStart++; 13 | 14 | for(int i=rowStart;i<=rowEnd;i++){ 15 | ans[i][colEnd]=count; 16 | count++; 17 | } 18 | colEnd--; 19 | 20 | for(int i=colEnd;i>=colStart;i--){ 21 | ans[rowEnd][i]=count; 22 | count++; 23 | } 24 | rowEnd--; 25 | 26 | for(int i=rowEnd;i>=rowStart;i--){ 27 | ans[i][colStart]=count; 28 | count++; 29 | } 30 | colStart++; 31 | 32 | } 33 | 34 | return ans; 35 | } 36 | }; -------------------------------------------------------------------------------- /59-spiral-matrix-ii/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /59-spiral-matrix-ii/README.md: -------------------------------------------------------------------------------- 1 |

59. Spiral Matrix II

Medium


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

2 | 3 |

 

4 |

Example 1:

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

Example 2:

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

 

17 |

Constraints:

18 | 19 |
    20 |
  • 1 <= n <= 20
  • 21 |
22 |
-------------------------------------------------------------------------------- /617-merge-two-binary-trees/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /62-unique-paths/62-unique-paths.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | long long int ans=0; 4 | int uniquePaths(int m, int n) { 5 | vector> dp(m, vector(n,0)); 6 | for(int i=0;i>& grid, int i, int j, vector>& dp){ 5 | 6 | //base cases 7 | if(i < 0 || j < 0 8 | || i >= row 9 | || j >= col) 10 | return 0; 11 | 12 | if(grid[i][j]){ 13 | dp[i][j] = 0; 14 | return dp[i][j]; 15 | } 16 | 17 | if(i == row - 1 && j == col - 1) 18 | return 1; 19 | 20 | if(dp[i][j]) 21 | return dp[i][j]; 22 | 23 | dp[i][j] = dfs(grid, i + 1, j, dp) + dfs(grid, i, j + 1, dp); 24 | 25 | return dp[i][j]; 26 | } 27 | 28 | int uniquePathsWithObstacles(vector>& obstacleGrid) { 29 | 30 | row = obstacleGrid.size(); 31 | col = obstacleGrid[0].size(); 32 | 33 | vector> dp(row, vector(col, 0)); 34 | 35 | return dfs(obstacleGrid, 0, 0, dp); 36 | } 37 | }; -------------------------------------------------------------------------------- /63-unique-paths-ii/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /632-smallest-range-covering-elements-from-k-lists/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /66-plus-one/66-plus-one.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector plusOne(vector& digits) { 4 | reverse(digits.begin(), digits.end()); 5 | int carry=0, n=digits.size(), i=0; 6 | while(i> que; 17 | que.push({root, 0}); 18 | int width = 0; 19 | while (que.size() != 0) { 20 | unsigned long long int left = que.front().second, right = 0; 21 | int size = que.size(); 22 | while (size-- > 0) { 23 | pair rNode = que.front(); 24 | que.pop(); 25 | right = rNode.second; 26 | if (rNode.first->left) { 27 | que.push({rNode.first->left, 2 * rNode.second + 1}); 28 | } 29 | if (rNode.first->right) { 30 | que.push({rNode.first->right, 2 * rNode.second + 2}); 31 | } 32 | } 33 | width = max(width, int(right - left + 1)); 34 | } 35 | return width; 36 | } 37 | }; -------------------------------------------------------------------------------- /662-maximum-width-of-binary-tree/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /669-trim-a-binary-search-tree/669-trim-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* trimBST(TreeNode* root, int low, int high) { 15 | if(!root){ 16 | return NULL; 17 | } 18 | if(root->val val > high){ 19 | if(root->val right, low, high); 21 | } else { 22 | return trimBST(root->left, low, high); 23 | } 24 | } 25 | root->left=trimBST(root->left, low, high); 26 | root->right=trimBST(root->right, low, high); 27 | 28 | return root; 29 | 30 | } 31 | }; -------------------------------------------------------------------------------- /669-trim-a-binary-search-tree/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /680-valid-palindrome-ii/680-valid-palindrome-ii.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool checkpalindrome(string s,int left,int right) 4 | { 5 | while(left680. Valid Palindrome II

Easy


Given a string s, return true if the s can be palindrome after deleting at most one character from it.

2 | 3 |

 

4 |

Example 1:

5 | 6 |
Input: s = "aba"
 7 | Output: true
 8 | 
9 | 10 |

Example 2:

11 | 12 |
Input: s = "abca"
13 | Output: true
14 | Explanation: You could delete the character 'c'.
15 | 
16 | 17 |

Example 3:

18 | 19 |
Input: s = "abc"
20 | Output: false
21 | 
22 | 23 |

 

24 |

Constraints:

25 | 26 |
    27 |
  • 1 <= s.length <= 105
  • 28 |
  • s consists of lowercase English letters.
  • 29 |
30 |
-------------------------------------------------------------------------------- /682-baseball-game/682-baseball-game.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | class Solution { 3 | public: 4 | int calPoints(vector& ops) { 5 | stack s; 6 | int n=ops.size(); 7 | for(int i=0;i 2 | class Solution { 3 | public: 4 | int reverse(int x) { 5 | long long int ans=0; 6 | bool neg=(x<0); 7 | if(x<=INT_MIN){ 8 | return 0; 9 | } 10 | if(neg){ 11 | x=-x; 12 | } 13 | string gg=to_string(x); 14 | std::reverse(gg.begin(), gg.end()); 15 | int n=gg.length(); 16 | int i=0; 17 | while(i=INT_MAX){ 22 | return 0; 23 | } 24 | } 25 | 26 | return (neg==1)? -1*(int)ans : (int)ans; 27 | } 28 | }; -------------------------------------------------------------------------------- /7-reverse-integer/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /7-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 |
Input: x = 123
 9 | Output: 321
10 | 
11 | 12 |

Example 2:

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

Example 3:

19 | 20 |
Input: x = 120
21 | Output: 21
22 | 
23 | 24 |

 

25 |

Constraints:

26 | 27 |
    28 |
  • -231 <= x <= 231 - 1
  • 29 |
30 |
-------------------------------------------------------------------------------- /700-search-in-a-binary-search-tree/700-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){ 16 | return NULL; 17 | } 18 | if(root->val==val){ 19 | return root; 20 | } else if(root->val>val){ 21 | return searchBST(root->left, val); 22 | } else { 23 | return searchBST(root->right, val); 24 | } 25 | } 26 | }; -------------------------------------------------------------------------------- /700-search-in-a-binary-search-tree/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /703-kth-largest-element-in-a-stream/703-kth-largest-element-in-a-stream.cpp: -------------------------------------------------------------------------------- 1 | class KthLargest 2 | { 3 | public: 4 | priority_queue, greater < int>> pq; 5 | int gg = 0; 6 | KthLargest(int k, vector &nums) 7 | { 8 | 9 | gg = k; 10 | for (int i = 0; i < nums.size(); i++) 11 | { 12 | pq.push(nums[i]); 13 | } 14 | } 15 | 16 | int add(int val) 17 | { 18 | while (pq.size() && pq.size() > gg) 19 | { 20 | pq.pop(); 21 | } 22 | if (pq.size() < gg) 23 | { 24 | pq.push(val); 25 | return pq.top(); 26 | } 27 | //cout << pq.top() << endl; 28 | 29 | int x = pq.top(); 30 | if (x > val) 31 | { 32 | return x; 33 | } 34 | else 35 | { 36 | 37 | pq.pop(); 38 | pq.push(val); 39 | return pq.top(); 40 | } 41 | } 42 | }; 43 | 44 | /** 45 | *Your KthLargest object will be instantiated and called as such: 46 | *KthLargest* obj = new KthLargest(k, nums); 47 | *int param_1 = obj->add(val); 48 | */ -------------------------------------------------------------------------------- /704-binary-search/704-binary-search.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int search(vector& nums, int target) { 4 | int st=0, end=nums.size()-1; 5 | while(st<=end){ 6 | int mid=(st+end)/2; 7 | if(nums[mid]==target){ 8 | return mid; 9 | } 10 | if(nums[mid]>target){ 11 | end=mid-1; 12 | } else { 13 | st=mid+1; 14 | } 15 | } 16 | return -1; 17 | } 18 | }; -------------------------------------------------------------------------------- /704-binary-search/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /704-binary-search/README.md: -------------------------------------------------------------------------------- 1 |

704. Binary Search

Easy


Given an array of integers nums which is sorted in ascending order, and an integer target, write a function to search target in nums. If target exists, then return its index. Otherwise, return -1.

2 | 3 |

You must write an algorithm with O(log n) runtime complexity.

4 | 5 |

 

6 |

Example 1:

7 | 8 |
Input: nums = [-1,0,3,5,9,12], target = 9
 9 | Output: 4
10 | Explanation: 9 exists in nums and its index is 4
11 | 
12 | 13 |

Example 2:

14 | 15 |
Input: nums = [-1,0,3,5,9,12], target = 2
16 | Output: -1
17 | Explanation: 2 does not exist in nums so return -1
18 | 
19 | 20 |

 

21 |

Constraints:

22 | 23 |
    24 |
  • 1 <= nums.length <= 104
  • 25 |
  • -104 < nums[i], target < 104
  • 26 |
  • All the integers in nums are unique.
  • 27 |
  • nums is sorted in ascending order.
  • 28 |
29 |
-------------------------------------------------------------------------------- /705-design-hashset/705-design-hashset.cpp: -------------------------------------------------------------------------------- 1 | class MyHashSet { 2 | public: 3 | unordered_map m; 4 | MyHashSet() { 5 | m.clear(); 6 | } 7 | 8 | void add(int key) { 9 | m[key]++; 10 | } 11 | 12 | void remove(int key) { 13 | if(m.count(key)){ 14 | m.erase(m.find(key)); 15 | } 16 | } 17 | 18 | bool contains(int key) { 19 | return m.count(key)>0; 20 | } 21 | }; 22 | 23 | /** 24 | * Your MyHashSet object will be instantiated and called as such: 25 | * MyHashSet* obj = new MyHashSet(); 26 | * obj->add(key); 27 | * obj->remove(key); 28 | * bool param_3 = obj->contains(key); 29 | */ -------------------------------------------------------------------------------- /705-design-hashset/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /706-design-hashmap/706-design-hashmap.cpp: -------------------------------------------------------------------------------- 1 | class MyHashMap { 2 | public: 3 | std::vector vec = std::vector(1000005,-1); 4 | MyHashMap() { 5 | 6 | } 7 | 8 | void put(int key, int value) { 9 | vec[key]=value; 10 | } 11 | 12 | int get(int key) { 13 | if(vec[key]==-1){ 14 | return -1; 15 | } else { 16 | return vec[key]; 17 | } 18 | } 19 | 20 | void remove(int key) { 21 | vec[key]=-1; 22 | } 23 | }; 24 | 25 | /** 26 | * Your MyHashMap object will be instantiated and called as such: 27 | * MyHashMap* obj = new MyHashMap(); 28 | * obj->put(key,value); 29 | * int param_2 = obj->get(key); 30 | * obj->remove(key); 31 | */ -------------------------------------------------------------------------------- /706-design-hashmap/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /71-simplify-path/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /72-edit-distance/72-edit-distance.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minDistance(string word1, string word2) { 4 | int n=word1.length(), m=word2.length(); 5 | if(n==0){ 6 | return m; 7 | } else if(m==0){ 8 | return n; 9 | } 10 | vector> dp(n+1, vector(m+1,0)); 11 | for(int i=1;i<=m;i++){ 12 | dp[0][i]=i; 13 | } 14 | for(int j=1;j<=n;j++){ 15 | dp[j][0]=j; 16 | } 17 | for(int i=1;i<=n;i++){ 18 | for(int j=1;j<=m;j++){ 19 | if(word1[i-1]==word2[j-1]){ 20 | dp[i][j]=dp[i-1][j-1]; 21 | } else { 22 | dp[i][j]=1+min({dp[i-1][j], dp[i-1][j-1], dp[i][j-1]}); 23 | } 24 | } 25 | } 26 | return dp[n][m]; 27 | } 28 | }; -------------------------------------------------------------------------------- /72-edit-distance/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /724-find-pivot-index/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /73-set-matrix-zeroes/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /74-search-a-2d-matrix/74-search-a-2d-matrix.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | bool searchMatrix(vector> &arr, int target) 5 | { 6 | int m = arr.size(), n = arr[0].size(); 7 | int i = 0, j = n - 1; 8 | while (i < m and j >= 0 and min(i, j) >= 0) 9 | { 10 | if (arr[i][j] == target) return true; 11 | else if (arr[i][j] > target) --j; 12 | else ++i; 13 | } 14 | return false; 15 | } 16 | }; -------------------------------------------------------------------------------- /74-search-a-2d-matrix/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /743-network-delay-time/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /754-reach-a-number/754-reach-a-number.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | 5 | int reachNumber(int target) 6 | { 7 | if (target < 0) 8 | { 9 | return reachNumber(-1 *target); 10 | } 11 | long long int counter = 0, step = 1; 12 | while (counter < target) 13 | { 14 | counter += step; 15 | step++; 16 | } 17 | while ((counter - target) % 2 == 1) 18 | { 19 | counter += step; 20 | step++; 21 | } 22 | return --step; 23 | } 24 | }; -------------------------------------------------------------------------------- /754-reach-a-number/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /76-minimum-window-substring/76-minimum-window-substring.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | string minWindow(string s, string t) 5 | { 6 | vector freq(128, 0); 7 | for (int i = 0; i < t.size(); i++) 8 | { 9 | freq[t[i]]++; 10 | } 11 | int start = 0, end = 0, counter = t.size(), minlen = INT_MAX, minstart = 0; 12 | while (end < s.size()) 13 | { 14 | char temp = s[end]; 15 | if (freq[temp] > 0) counter--; 16 | freq[temp]--; 17 | end++; 18 | while (counter == 0) 19 | { 20 | if (minlen > end - start) 21 | { 22 | minlen = end - start; 23 | minstart = start; 24 | } 25 | char chars = s[start]; 26 | freq[chars]++; 27 | if (freq[chars] > 0) counter++; 28 | start++; 29 | } 30 | } 31 | return minlen == INT_MAX ? "" : s.substr(minstart, minlen); 32 | } 33 | }; -------------------------------------------------------------------------------- /76-minimum-window-substring/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /763-partition-labels/763-partition-labels.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector partitionLabels(string s) { 4 | 5 | vector temp(26, -1); 6 | int n=s.length(); 7 | for(int i=0;i ans; 11 | int maxcur=temp[s[0]-'a'], st=0; 12 | for(int i=0;i763. Partition Labels

Medium


You are given a string s. We want to partition the string into as many parts as possible so that each letter appears in at most one part.

2 | 3 |

Note that the partition is done so that after concatenating all the parts in order, the resultant string should be s.

4 | 5 |

Return a list of integers representing the size of these parts.

6 | 7 |

 

8 |

Example 1:

9 | 10 |
Input: s = "ababcbacadefegdehijhklij"
11 | Output: [9,7,8]
12 | Explanation:
13 | The partition is "ababcbaca", "defegde", "hijhklij".
14 | This is a partition so that each letter appears in at most one part.
15 | A partition like "ababcbacadefegde", "hijhklij" is incorrect, because it splits s into less parts.
16 | 
17 | 18 |

Example 2:

19 | 20 |
Input: s = "eccbbbbdec"
21 | Output: [10]
22 | 
23 | 24 |

 

25 |

Constraints:

26 | 27 |
    28 |
  • 1 <= s.length <= 500
  • 29 |
  • s consists of lowercase English letters.
  • 30 |
31 |
-------------------------------------------------------------------------------- /78-subsets/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /78-subsets/README.md: -------------------------------------------------------------------------------- 1 |

78. Subsets

Medium


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

2 | 3 |

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

4 | 5 |

 

6 |

Example 1:

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

Example 2:

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

 

19 |

Constraints:

20 | 21 |
    22 |
  • 1 <= nums.length <= 10
  • 23 |
  • -10 <= nums[i] <= 10
  • 24 |
  • All the numbers of nums are unique.
  • 25 |
26 |
-------------------------------------------------------------------------------- /785-is-graph-bipartite/785-is-graph-bipartite.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | 4 | int dfs(int node, int c, int col[], vector> &adj){ 5 | col[node]=c; 6 | for(int child:adj[node]){ 7 | // if colour of child is same as that of node return 0 8 | if(col[child]==c) return 0; 9 | // col[child]==-1 means child is not visited 10 | if(col[child]==-1 && dfs(child, (c^1), col, adj)==0) return 0; 11 | } 12 | return 1; 13 | } 14 | bool isBipartite(vector>& graph) { 15 | int A=graph.size(); 16 | int col[A]; 17 | memset(col, -1, sizeof(col)); 18 | for(int i=0; i 0) 13 | { 14 | ans.pop_back(); 15 | } 16 | } 17 | else 18 | { 19 | ans += s[i]; 20 | } 21 | } 22 | string ans2; 23 | n = t.length(); 24 | for (int i = 0; i < n; i++) 25 | { 26 | if (t[i] == '#') 27 | { 28 | if (ans2.length() > 0) 29 | { 30 | ans2.pop_back(); 31 | } 32 | } 33 | else 34 | { 35 | ans2 += t[i]; 36 | } 37 | } 38 | return ans == ans2; 39 | } 40 | }; -------------------------------------------------------------------------------- /844-backspace-string-compare/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /852-peak-index-in-a-mountain-array/852-peak-index-in-a-mountain-array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int peakIndexInMountainArray(vector& arr) { 4 | int n=arr.size(); 5 | for(int i=1;iarr[i-1] && arr[i]>arr[i+1]){ 7 | return i; 8 | } 9 | } 10 | return 0; 11 | } 12 | }; -------------------------------------------------------------------------------- /852-peak-index-in-a-mountain-array/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /856-score-of-parentheses/856-score-of-parentheses.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int scoreOfParentheses(string s) { 4 | stack stk; 5 | int n=s.length(); 6 | for(int i=0;i> transpose(vector>& A) { 4 | vector> vRes(A[0].size(), vector(A.size(), 0)); 5 | for (int i = 0; i < A.size(); i++) { 6 | for (int j = 0; j < A[0].size(); j++) { 7 | vRes[j][i] = A[i][j]; 8 | } 9 | } 10 | return vRes; 11 | } 12 | }; -------------------------------------------------------------------------------- /867-transpose-matrix/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /867-transpose-matrix/README.md: -------------------------------------------------------------------------------- 1 |

867. Transpose Matrix

Easy


Given a 2D integer array matrix, return the transpose of matrix.

2 | 3 |

The transpose of a matrix is the matrix flipped over its main diagonal, switching the matrix's row and column indices.

4 | 5 |

6 | 7 |

 

8 |

Example 1:

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

Example 2:

15 | 16 |
Input: matrix = [[1,2,3],[4,5,6]]
17 | Output: [[1,4],[2,5],[3,6]]
18 | 
19 | 20 |

 

21 |

Constraints:

22 | 23 |
    24 |
  • m == matrix.length
  • 25 |
  • n == matrix[i].length
  • 26 |
  • 1 <= m, n <= 1000
  • 27 |
  • 1 <= m * n <= 105
  • 28 |
  • -109 <= matrix[i][j] <= 109
  • 29 |
30 |
-------------------------------------------------------------------------------- /881-boats-to-save-people/881-boats-to-save-people.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numRescueBoats(vector& people, int limit) { 4 | sort(people.begin(), people.end()); 5 | auto light = people.begin(), heavy = people.end() - 1; 6 | int boats = 0; 7 | while(light <= heavy) { 8 | if(*light + *heavy <= limit) 9 | light++; 10 | boats++; 11 | heavy--; 12 | } 13 | return boats; 14 | } 15 | }; -------------------------------------------------------------------------------- /881-boats-to-save-people/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /889-construct-binary-tree-from-preorder-and-postorder-traversal/889-construct-binary-tree-from-preorder-and-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 | public: 15 | TreeNode* constructFromPrePost(vector& pre, vector& post) { 16 | TreeNode* root = new TreeNode(pre[preIndex++]); 17 | if(root->val != post[posIndex]) root->left = constructFromPrePost(pre,post); 18 | if(root->val != post[posIndex]) root->right = constructFromPrePost(pre,post); 19 | posIndex++; 20 | return root; 21 | } 22 | private: 23 | int preIndex = 0, posIndex = 0; 24 | }; -------------------------------------------------------------------------------- /889-construct-binary-tree-from-preorder-and-postorder-traversal/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /895-maximum-frequency-stack/895-maximum-frequency-stack.cpp: -------------------------------------------------------------------------------- 1 | class FreqStack 2 | { 3 | public: 4 | 5 | unordered_map> freqStack; 6 | unordered_map freqMap; 7 | int maxcount = 0; 8 | FreqStack() {} 9 | 10 | void push(int val) 11 | { 12 | freqMap[val]++; 13 | maxcount = max(maxcount, freqMap[val]); 14 | freqStack[freqMap[val]].push(val); 15 | } 16 | 17 | int pop() 18 | { 19 | int ret = freqStack[maxcount].top(); 20 | freqStack[maxcount].pop(); 21 | freqMap[ret]--; 22 | if (freqStack[maxcount].empty()) 23 | maxcount--; 24 | return ret; 25 | } 26 | }; 27 | 28 | /** 29 | *Your FreqStack object will be instantiated and called as such: 30 | *FreqStack* obj = new FreqStack(); 31 | *obj->push(val); 32 | *int param_2 = obj->pop(); 33 | */ -------------------------------------------------------------------------------- /895-maximum-frequency-stack/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /897-increasing-order-search-tree/897-increasing-order-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 | vector ans; 15 | void inorder(TreeNode* root){ 16 | if(!root){ 17 | return ; 18 | } 19 | inorder(root->left); 20 | ans.push_back(root); 21 | inorder(root->right); 22 | } 23 | TreeNode* increasingBST(TreeNode* root) { 24 | inorder(root); 25 | int n=ans.size(); 26 | if(n==0){ 27 | return root; 28 | } 29 | 30 | for(int i=0;ileft=NULL; 33 | ans[i]->right=ans[i+1]; 34 | } else { 35 | ans[i]->left=NULL; 36 | ans[i]->right=NULL; 37 | } 38 | cout<val<897. Increasing Order Search Tree

Easy


Given the root of a binary search tree, rearrange the tree in in-order so that the leftmost node in the tree is now the root of the tree, and every node has no left child and only one right child.

2 | 3 |

 

4 |

Example 1:

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

Example 2:

11 | 12 |
Input: root = [5,1,7]
13 | Output: [1,null,5,null,7]
14 | 
15 | 16 |

 

17 |

Constraints:

18 | 19 |
    20 |
  • The number of nodes in the given tree will be in the range [1, 100].
  • 21 |
  • 0 <= Node.val <= 1000
  • 22 |
-------------------------------------------------------------------------------- /9-palindrome-number/9-palindrome-number.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isPalindrome(int x) { 4 | if(x<0){ 5 | return false; 6 | } 7 | long long int ans=0; 8 | int temp=x; 9 | while(x){ 10 | ans=ans*10+ x%10; 11 | if(ans>INT_MAX){ 12 | return false; 13 | } 14 | x=x/10; 15 | } 16 | cout< sortArrayByParity(vector& nums) { 4 | // vector eve; 5 | // vector odd; 6 | // int n=nums.size(); 7 | int oddindex=nums.size()-1, evenindex=0; 8 | 9 | while(oddindex>evenindex){ 10 | while(oddindex>=0 && nums[oddindex]%2==1){ 11 | oddindex--; 12 | } 13 | while(evenindexevenindex){ 17 | int temp=nums[oddindex]; 18 | nums[oddindex]=nums[evenindex]; 19 | nums[evenindex]=temp; 20 | 21 | oddindex--; 22 | evenindex++; 23 | } 24 | 25 | } 26 | return nums; 27 | 28 | } 29 | }; -------------------------------------------------------------------------------- /905-sort-array-by-parity/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /905-sort-array-by-parity/README.md: -------------------------------------------------------------------------------- 1 |

905. Sort Array By Parity

Easy


Given an integer array nums, move all the even integers at the beginning of the array followed by all the odd integers.

2 | 3 |

Return any array that satisfies this condition.

4 | 5 |

 

6 |

Example 1:

7 | 8 |
Input: nums = [3,1,2,4]
 9 | Output: [2,4,3,1]
10 | Explanation: The outputs [4,2,3,1], [2,4,1,3], and [4,2,1,3] would also be accepted.
11 | 
12 | 13 |

Example 2:

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

 

20 |

Constraints:

21 | 22 |
    23 |
  • 1 <= nums.length <= 5000
  • 24 |
  • 0 <= nums[i] <= 5000
  • 25 |
26 |
-------------------------------------------------------------------------------- /909-snakes-and-ladders/909-snakes-and-ladders.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | void getCoordinate(int n,int s, int &row, int &col){ 3 | row = n-1-(s-1)/n; 4 | col = (s-1)%n; 5 | if((n%2==1 && row%2==1)||(n%2==0 && row%2==0)) 6 | col = n-1-col; 7 | } 8 | public: 9 | int snakesAndLadders(vector>& board) { 10 | int n = board.size(); 11 | vector seen(n*n+1,false); 12 | seen[1] = true; 13 | queue> q; 14 | q.push({1,0}); 15 | while(!q.empty()){ 16 | pair p = q.front();q.pop(); 17 | int row,col,s = p.first, dist = p.second; 18 | if(s == n*n) 19 | return dist; 20 | for(int i=1;s+i<=n*n && i<=6;i++){ 21 | getCoordinate(n,s+i,row,col); 22 | int sfinal = board[row][col]==-1?s+i:board[row][col]; // check for snake or ladder 23 | if(seen[sfinal]==false){ 24 | seen[sfinal] = true; 25 | q.push({sfinal,dist+1}); 26 | } 27 | } 28 | } 29 | return -1; 30 | } 31 | }; -------------------------------------------------------------------------------- /909-snakes-and-ladders/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /923-3sum-with-multiplicity/923-3sum-with-multiplicity.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | int threeSumMulti(vector &arr, int target) 5 | { 6 | long long int ans = 0; 7 | long long int mod = pow(10, 9) + 7; 8 | 9 | map m; 10 | int n = arr.size(); 11 | for (int i = 0; i < n; i++) 12 | { 13 | m[arr[i]]++; 14 | } 15 | 16 | for (auto it: m) 17 | { 18 | for (auto itr: m) 19 | { 20 | int i = it.first, j = itr.first, k = target - i - j; 21 | if (!m[k]) 22 | { 23 | continue; 24 | } 25 | if (i == j && j == k) 26 | { 27 | ans += ((m[i] *(m[i] - 1) *(m[i] - 2)) / 6) % mod; 28 | } 29 | else if (i == j && j != k) 30 | { 31 | ans += ((m[i] *(m[i] - 1) *(m[k])) / 2) % mod; 32 | } 33 | else if(i build(int n,int val){ 15 | //base case 16 | if(n==0){return {nullptr};} 17 | 18 | vectorans; 19 | TreeNode* root; 20 | 21 | for(int i=0;il=build(i,val); 24 | vectorr=build(n-i-1,val+i+1); 25 | 26 | for(int j=0;jleft=l[j]; 30 | root->right=r[k]; 31 | ans.push_back(root); 32 | } 33 | } 34 | } 35 | return ans; 36 | } 37 | 38 | vector generateTrees(int n) { 39 | return build(n,1); 40 | } 41 | }; -------------------------------------------------------------------------------- /95-unique-binary-search-trees-ii/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /95-unique-binary-search-trees-ii/README.md: -------------------------------------------------------------------------------- 1 |

95. Unique Binary Search Trees II

Medium


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

2 | 3 |

 

4 |

Example 1:

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

Example 2:

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

 

17 |

Constraints:

18 | 19 |
    20 |
  • 1 <= n <= 8
  • 21 |
22 |
-------------------------------------------------------------------------------- /96-unique-binary-search-trees/96-unique-binary-search-trees.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numTrees(int n) { 4 | int dp[n+1]; 5 | dp[0] = dp[1] = 1; 6 | for (int i=2; i<=n; i++) { 7 | dp[i] = 0; 8 | for (int j=1; j<=i; j++) { 9 | dp[i] += dp[j-1] * dp[i-j]; 10 | } 11 | } 12 | return dp[n]; 13 | } 14 | }; -------------------------------------------------------------------------------- /96-unique-binary-search-trees/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /96-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 |
Input: n = 3
 7 | Output: 5
 8 | 
9 | 10 |

Example 2:

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

 

17 |

Constraints:

18 | 19 |
    20 |
  • 1 <= n <= 19
  • 21 |
22 |
-------------------------------------------------------------------------------- /97-interleaving-string/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /977-squares-of-a-sorted-array/977-squares-of-a-sorted-array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector sortedSquares(vector& nums) { 4 | int n=nums.size(); 5 | vector ans(n, 0); 6 | int i=0,j=n-1, k=n-1; 7 | while(i<=j){ 8 | if(abs(nums[i])>abs(nums[j])){ 9 | ans[k--]=nums[i]*nums[i]; 10 | i++; 11 | } else { 12 | ans[k--]=nums[j]*nums[j]; 13 | j--; 14 | } 15 | } 16 | return ans; 17 | } 18 | }; -------------------------------------------------------------------------------- /977-squares-of-a-sorted-array/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /977-squares-of-a-sorted-array/README.md: -------------------------------------------------------------------------------- 1 |

977. Squares of a Sorted Array

Easy


Given an integer array nums sorted in non-decreasing order, return an array of the squares of each number sorted in non-decreasing order.

2 | 3 |

 

4 |

Example 1:

5 | 6 |
Input: nums = [-4,-1,0,3,10]
 7 | Output: [0,1,9,16,100]
 8 | Explanation: After squaring, the array becomes [16,1,0,9,100].
 9 | After sorting, it becomes [0,1,9,16,100].
10 | 
11 | 12 |

Example 2:

13 | 14 |
Input: nums = [-7,-3,2,3,11]
15 | Output: [4,9,9,49,121]
16 | 
17 | 18 |

 

19 |

Constraints:

20 | 21 |
    22 |
  • 1 <= nums.length <= 104
  • 23 |
  • -104 <= nums[i] <= 104
  • 24 |
  • nums is sorted in non-decreasing order.
  • 25 |
26 | 27 |

 

28 | Follow up: Squaring each element and sorting the new array is very trivial, could you find an O(n) solution using a different approach?
-------------------------------------------------------------------------------- /98-validate-binary-search-tree/98-validate-binary-search-tree.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | class Solution { 13 | public: 14 | bool isBST(TreeNode* root,int64_t min,int64_t max){ 15 | if(root==NULL){ 16 | return true; 17 | } 18 | 19 | return ((root->val>min && root->valleft,min,root->val) && isBST(root->right,root->val,max)); 20 | 21 | } 22 | 23 | bool isValidBST(TreeNode* root) { 24 | return isBST(root,LLONG_MIN,LLONG_MAX); 25 | } 26 | }; -------------------------------------------------------------------------------- /98-validate-binary-search-tree/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /99-recover-binary-search-tree/99-recover-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* firstMistake, *secondMistake, *pre; 15 | void recoverTree(TreeNode* root) { 16 | pre = new TreeNode(INT_MIN); 17 | inorder(root); 18 | swap(firstMistake->val, secondMistake->val); 19 | } 20 | 21 | void inorder(TreeNode* root) { 22 | if(root == nullptr) 23 | return; 24 | 25 | inorder(root->left); 26 | 27 | if(firstMistake == nullptr && root->val < pre->val) 28 | firstMistake = pre; 29 | if(firstMistake != nullptr && root->val < pre->val) 30 | secondMistake = root; 31 | pre = root; 32 | 33 | inorder(root->right); 34 | } 35 | }; -------------------------------------------------------------------------------- /99-recover-binary-search-tree/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /991-broken-calculator/991-broken-calculator.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | int brokenCalc(int x, int y) 5 | { 6 | if(x>=y){ 7 | return x-y; 8 | } 9 | if(y%2==0){ 10 | return 1+brokenCalc(x, y/2); 11 | } 12 | return 1+brokenCalc(x, y+1); 13 | } 14 | }; -------------------------------------------------------------------------------- /991-broken-calculator/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # LeetCode 2 | Collection of LeetCode questions to ace the coding interview! 3 | --------------------------------------------------------------------------------