├── 0 - 1 Knapsack Problem - GFG ├── 0-1-knapsack-problem.java └── README.md ├── 0001-two-sum ├── 0001-two-sum.java ├── NOTES.md └── README.md ├── 0006-zigzag-conversion ├── 0006-zigzag-conversion.java ├── NOTES.md └── README.md ├── 0017-letter-combinations-of-a-phone-number ├── 0017-letter-combinations-of-a-phone-number.java ├── NOTES.md └── README.md ├── 0018-4sum ├── 0018-4sum.java ├── NOTES.md └── README.md ├── 0038-count-and-say ├── 0038-count-and-say.cpp ├── 0038-count-and-say.java ├── NOTES.md └── README.md ├── 0041-first-missing-positive ├── 0041-first-missing-positive.java ├── NOTES.md └── README.md ├── 0042-trapping-rain-water ├── 0042-trapping-rain-water.java ├── NOTES.md └── README.md ├── 0048-rotate-image ├── 0048-rotate-image.java ├── NOTES.md └── README.md ├── 0049-group-anagrams ├── 0049-group-anagrams.cpp ├── NOTES.md └── README.md ├── 0053-maximum-subarray ├── 0053-maximum-subarray.cpp ├── 0053-maximum-subarray.java ├── 0053-maximum-subarray.py ├── NOTES.md └── README.md ├── 0054-spiral-matrix ├── 0054-spiral-matrix.java ├── NOTES.md └── README.md ├── 0056-merge-intervals ├── 0056-merge-intervals.java ├── NOTES.md └── README.md ├── 0059-spiral-matrix-ii ├── 0059-spiral-matrix-ii.java ├── NOTES.md └── README.md ├── 0064-minimum-path-sum ├── 0064-minimum-path-sum.java ├── NOTES.md └── README.md ├── 0073-set-matrix-zeroes ├── 0073-set-matrix-zeroes.java ├── NOTES.md └── README.md ├── 0093-restore-ip-addresses ├── 0093-restore-ip-addresses.java ├── NOTES.md └── README.md ├── 0094-binary-tree-inorder-traversal ├── 0094-binary-tree-inorder-traversal.java ├── NOTES.md └── README.md ├── 0102-binary-tree-level-order-traversal ├── 0102-binary-tree-level-order-traversal.java ├── NOTES.md └── README.md ├── 0103-binary-tree-zigzag-level-order-traversal ├── 0103-binary-tree-zigzag-level-order-traversal.java ├── NOTES.md └── README.md ├── 0105-construct-binary-tree-from-preorder-and-inorder-traversal ├── 0105-construct-binary-tree-from-preorder-and-inorder-traversal.java ├── NOTES.md └── README.md ├── 0110-balanced-binary-tree ├── 0110-balanced-binary-tree.java ├── NOTES.md └── README.md ├── 0114-flatten-binary-tree-to-linked-list ├── 0114-flatten-binary-tree-to-linked-list.java ├── NOTES.md └── README.md ├── 0121-best-time-to-buy-and-sell-stock ├── 0121-best-time-to-buy-and-sell-stock.java ├── NOTES.md └── README.md ├── 0124-binary-tree-maximum-path-sum ├── 0124-binary-tree-maximum-path-sum.java ├── NOTES.md └── README.md ├── 0125-valid-palindrome ├── 0125-valid-palindrome.java ├── NOTES.md └── README.md ├── 0134-gas-station ├── 0134-gas-station.java ├── NOTES.md └── README.md ├── 0144-binary-tree-preorder-traversal ├── 0144-binary-tree-preorder-traversal.java ├── NOTES.md └── README.md ├── 0145-binary-tree-postorder-traversal ├── 0145-binary-tree-postorder-traversal.java ├── NOTES.md └── README.md ├── 0146-lru-cache ├── 0146-lru-cache.java ├── NOTES.md └── README.md ├── 0151-reverse-words-in-a-string ├── 0151-reverse-words-in-a-string.java ├── NOTES.md └── README.md ├── 0154-find-minimum-in-rotated-sorted-array-ii ├── 0154-find-minimum-in-rotated-sorted-array-ii.java ├── NOTES.md └── README.md ├── 0155-min-stack ├── NOTES.md └── README.md ├── 0183-customers-who-never-order ├── 0183-customers-who-never-order.sql ├── NOTES.md └── README.md ├── 0189-rotate-array ├── 0189-rotate-array.java ├── NOTES.md └── README.md ├── 0199-binary-tree-right-side-view ├── 0199-binary-tree-right-side-view.java ├── NOTES.md └── README.md ├── 0226-invert-binary-tree ├── 0226-invert-binary-tree.java ├── NOTES.md └── README.md ├── 0236-lowest-common-ancestor-of-a-binary-tree ├── 0236-lowest-common-ancestor-of-a-binary-tree.java ├── NOTES.md └── README.md ├── 0239-sliding-window-maximum ├── 0239-sliding-window-maximum.java ├── NOTES.md └── README.md ├── 0240-search-a-2d-matrix-ii ├── 0240-search-a-2d-matrix-ii.java ├── NOTES.md └── README.md ├── 0268-missing-number ├── 0268-missing-number.java ├── NOTES.md └── README.md ├── 0287-find-the-duplicate-number ├── 0287-find-the-duplicate-number.java ├── NOTES.md └── README.md ├── 0295-find-median-from-data-stream ├── 0295-find-median-from-data-stream.cpp ├── 0295-find-median-from-data-stream.java ├── 0295-find-median-from-data-stream.py ├── NOTES.md └── README.md ├── 0297-serialize-and-deserialize-binary-tree ├── 0297-serialize-and-deserialize-binary-tree.java ├── NOTES.md └── README.md ├── 0300-longest-increasing-subsequence ├── 0300-longest-increasing-subsequence.cpp ├── 0300-longest-increasing-subsequence.java ├── 0300-longest-increasing-subsequence.py ├── NOTES.md └── README.md ├── 0319-bulb-switcher ├── 0319-bulb-switcher.java ├── NOTES.md └── README.md ├── 0344-reverse-string ├── 0344-reverse-string.java ├── NOTES.md └── README.md ├── 0352-data-stream-as-disjoint-intervals ├── 0352-data-stream-as-disjoint-intervals.java ├── NOTES.md └── README.md ├── 0354-russian-doll-envelopes ├── 0354-russian-doll-envelopes.cpp ├── 0354-russian-doll-envelopes.java ├── NOTES.md └── README.md ├── 0373-find-k-pairs-with-smallest-sums ├── 0373-find-k-pairs-with-smallest-sums.cpp ├── NOTES.md └── README.md ├── 0442-find-all-duplicates-in-an-array ├── 0442-find-all-duplicates-in-an-array.java ├── NOTES.md └── README.md ├── 0448-find-all-numbers-disappeared-in-an-array ├── 0448-find-all-numbers-disappeared-in-an-array.java ├── NOTES.md └── README.md ├── 0456-132-pattern ├── 0456-132-pattern.java ├── NOTES.md └── README.md ├── 0460-lfu-cache ├── 0460-lfu-cache.java ├── NOTES.md └── README.md ├── 0472-concatenated-words ├── 0472-concatenated-words.cpp ├── NOTES.md └── README.md ├── 0491-non-decreasing-subsequences ├── 0491-non-decreasing-subsequences.java ├── NOTES.md └── README.md ├── 0503-next-greater-element-ii ├── NOTES.md └── README.md ├── 0540-single-element-in-a-sorted-array ├── 0540-single-element-in-a-sorted-array.java ├── NOTES.md └── README.md ├── 0543-diameter-of-binary-tree ├── 0543-diameter-of-binary-tree.java ├── NOTES.md └── README.md ├── 0567-permutation-in-string ├── 0567-permutation-in-string.cpp ├── 0567-permutation-in-string.java ├── NOTES.md └── README.md ├── 0584-find-customer-referee ├── 0584-find-customer-referee.sql ├── NOTES.md └── README.md ├── 0595-big-countries ├── NOTES.md └── README.md ├── 0627-swap-salary ├── NOTES.md └── README.md ├── 0645-set-mismatch ├── 0645-set-mismatch.cpp ├── 0645-set-mismatch.java ├── NOTES.md └── README.md ├── 0678-valid-parenthesis-string ├── 0678-valid-parenthesis-string.java ├── NOTES.md └── README.md ├── 0735-asteroid-collision ├── 0735-asteroid-collision.java ├── NOTES.md └── README.md ├── 0766-toeplitz-matrix ├── 0766-toeplitz-matrix.cpp ├── NOTES.md └── README.md ├── 0787-cheapest-flights-within-k-stops ├── NOTES.md └── README.md ├── 0857-minimum-cost-to-hire-k-workers ├── 0857-minimum-cost-to-hire-k-workers.cpp ├── NOTES.md └── README.md ├── 0862-shortest-subarray-with-sum-at-least-k ├── 0862-shortest-subarray-with-sum-at-least-k.java ├── NOTES.md └── README.md ├── 0863-all-nodes-distance-k-in-binary-tree ├── 0863-all-nodes-distance-k-in-binary-tree.java ├── NOTES.md └── README.md ├── 0901-online-stock-span ├── 0901-online-stock-span.java ├── NOTES.md └── README.md ├── 0904-fruit-into-baskets ├── 0904-fruit-into-baskets.java ├── NOTES.md └── README.md ├── 0921-minimum-add-to-make-parentheses-valid ├── 0921-minimum-add-to-make-parentheses-valid.java ├── NOTES.md └── README.md ├── 0941-valid-mountain-array ├── 0941-valid-mountain-array.cpp ├── 0941-valid-mountain-array.java ├── NOTES.md └── README.md ├── 0973-k-closest-points-to-origin ├── 0973-k-closest-points-to-origin.cpp ├── NOTES.md └── README.md ├── 0976-largest-perimeter-triangle ├── 0976-largest-perimeter-triangle.go ├── NOTES.md └── README.md ├── 0983-minimum-cost-for-tickets ├── 0983-minimum-cost-for-tickets.cpp ├── NOTES.md └── README.md ├── 0987-vertical-order-traversal-of-a-binary-tree ├── 0987-vertical-order-traversal-of-a-binary-tree.java ├── NOTES.md └── README.md ├── 0994-rotting-oranges ├── 0994-rotting-oranges.java ├── NOTES.md └── README.md ├── 0997-find-the-town-judge ├── 0997-find-the-town-judge.java └── README.md ├── 0two-sum ├── 0two-sum.java └── README.md ├── 100-same-tree ├── 100-same-tree.java ├── NOTES.md └── README.md ├── 1005-maximize-sum-of-array-after-k-negations ├── 1005-maximize-sum-of-array-after-k-negations.cpp ├── NOTES.md └── README.md ├── 1007-minimum-domino-rotations-for-equal-row ├── 1007-minimum-domino-rotations-for-equal-row.cpp ├── NOTES.md └── README.md ├── 101-symmetric-tree ├── 101-symmetric-tree.java ├── NOTES.md └── README.md ├── 1011-capacity-to-ship-packages-within-d-days ├── 1011-capacity-to-ship-packages-within-d-days.java ├── NOTES.md └── README.md ├── 1018-binary-prefix-divisible-by-5 ├── 1018-binary-prefix-divisible-by-5.cpp ├── NOTES.md └── README.md ├── 102-binary-tree-level-order-traversal ├── 102-binary-tree-level-order-traversal.cpp ├── 102-binary-tree-level-order-traversal.java ├── NOTES.md └── README.md ├── 1020-number-of-enclaves ├── 1020-number-of-enclaves.cpp ├── NOTES.md └── README.md ├── 1029-two-city-scheduling ├── 1029-two-city-scheduling.cpp ├── NOTES.md └── README.md ├── 103-binary-tree-zigzag-level-order-traversal ├── 103-binary-tree-zigzag-level-order-traversal.java ├── NOTES.md └── README.md ├── 1032-stream-of-characters ├── 1032-stream-of-characters.cpp ├── NOTES.md └── README.md ├── 1034-coloring-a-border ├── 1034-coloring-a-border.cpp ├── NOTES.md └── README.md ├── 1035-uncrossed-lines ├── 1035-uncrossed-lines.cpp ├── NOTES.md └── README.md ├── 1046-last-stone-weight ├── 1046-last-stone-weight.cpp ├── NOTES.md └── README.md ├── 1049-last-stone-weight-ii ├── 1049-last-stone-weight-ii.cpp ├── NOTES.md └── README.md ├── 107-binary-tree-level-order-traversal-ii ├── 107-binary-tree-level-order-traversal-ii.java ├── NOTES.md └── README.md ├── 108-convert-sorted-array-to-binary-search-tree ├── 108-convert-sorted-array-to-binary-search-tree.java ├── NOTES.md └── README.md ├── 1081-smallest-subsequence-of-distinct-characters ├── 1081-smallest-subsequence-of-distinct-characters.cpp ├── NOTES.md └── README.md ├── 1091-shortest-path-in-binary-matrix ├── 1091-shortest-path-in-binary-matrix.cpp ├── 1091-shortest-path-in-binary-matrix.java ├── NOTES.md └── README.md ├── 11-container-with-most-water ├── 11-container-with-most-water.cpp ├── NOTES.md └── README.md ├── 1114-print-in-order ├── 1114-print-in-order.java ├── NOTES.md └── README.md ├── 1115-print-foobar-alternately ├── 1115-print-foobar-alternately.java ├── NOTES.md └── README.md ├── 1117-building-h2o ├── 1117-building-h2o.java ├── NOTES.md └── README.md ├── 1123-lowest-common-ancestor-of-deepest-leaves ├── 1123-lowest-common-ancestor-of-deepest-leaves.java ├── NOTES.md └── README.md ├── 1137-n-th-tribonacci-number ├── 1137-n-th-tribonacci-number.java ├── NOTES.md └── README.md ├── 114-flatten-binary-tree-to-linked-list ├── 114-flatten-binary-tree-to-linked-list.cpp ├── 114-flatten-binary-tree-to-linked-list.java ├── NOTES.md └── README.md ├── 1143-longest-common-subsequence ├── 1143-longest-common-subsequence.cpp ├── NOTES.md └── README.md ├── 1145-binary-tree-coloring-game ├── 1145-binary-tree-coloring-game.cpp ├── NOTES.md └── README.md ├── 1179-reformat-department-table ├── NOTES.md └── README.md ├── 118-pascals-triangle ├── 118-pascals-triangle.cpp ├── NOTES.md └── README.md ├── 1188-design-bounded-blocking-queue ├── 1188-design-bounded-blocking-queue.java ├── NOTES.md └── README.md ├── 119-pascals-triangle-ii ├── 119-pascals-triangle-ii.cpp ├── NOTES.md └── README.md ├── 1195-fizz-buzz-multithreaded ├── 1195-fizz-buzz-multithreaded.java ├── NOTES.md └── README.md ├── 120-triangle ├── 120-triangle.java ├── NOTES.md └── README.md ├── 1202-smallest-string-with-swaps ├── 1202-smallest-string-with-swaps.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 ├── 122-best-time-to-buy-and-sell-stock-ii ├── 122-best-time-to-buy-and-sell-stock-ii.java ├── NOTES.md └── README.md ├── 1220-count-vowels-permutation ├── 1220-count-vowels-permutation.cpp ├── NOTES.md └── README.md ├── 1221-split-a-string-in-balanced-strings ├── 1221-split-a-string-in-balanced-strings.java ├── NOTES.md └── README.md ├── 1222-queens-that-can-attack-the-king ├── 1222-queens-that-can-attack-the-king.cpp ├── NOTES.md └── README.md ├── 1226-the-dining-philosophers ├── 1226-the-dining-philosophers.java ├── NOTES.md └── README.md ├── 123-best-time-to-buy-and-sell-stock-iii ├── 123-best-time-to-buy-and-sell-stock-iii.java ├── NOTES.md └── README.md ├── 1236-web-crawler ├── 1236-web-crawler.java ├── NOTES.md └── README.md ├── 124-binary-tree-maximum-path-sum ├── 124-binary-tree-maximum-path-sum.java ├── NOTES.md └── README.md ├── 1242-web-crawler-multithreaded ├── 1242-web-crawler-multithreaded.cpp ├── 1242-web-crawler-multithreaded.java ├── NOTES.md └── README.md ├── 1249-minimum-remove-to-make-valid-parentheses ├── 1249-minimum-remove-to-make-valid-parentheses.cpp ├── 1249-minimum-remove-to-make-valid-parentheses.java ├── NOTES.md └── README.md ├── 126-word-ladder-ii ├── 126-word-ladder-ii.cpp ├── NOTES.md └── README.md ├── 1260-shift-2d-grid ├── 1260-shift-2d-grid.cpp ├── NOTES.md └── README.md ├── 1267-count-servers-that-communicate ├── 1267-count-servers-that-communicate.cpp ├── NOTES.md └── README.md ├── 1268-search-suggestions-system ├── 1268-search-suggestions-system.cpp ├── NOTES.md └── README.md ├── 127-word-ladder ├── 127-word-ladder.cpp ├── NOTES.md └── README.md ├── 1289-minimum-falling-path-sum-ii ├── 1289-minimum-falling-path-sum-ii.cpp ├── NOTES.md └── README.md ├── 1293-shortest-path-in-a-grid-with-obstacles-elimination ├── 1293-shortest-path-in-a-grid-with-obstacles-elimination.cpp ├── NOTES.md └── README.md ├── 130-surrounded-regions ├── 130-surrounded-regions.cpp ├── 130-surrounded-regions.java ├── NOTES.md └── README.md ├── 1306-jump-game-iii ├── 1306-jump-game-iii.java ├── NOTES.md └── README.md ├── 1312-minimum-insertion-steps-to-make-a-string-palindrome ├── 1312-minimum-insertion-steps-to-make-a-string-palindrome.java ├── NOTES.md └── README.md ├── 1319-number-of-operations-to-make-network-connected ├── 1319-number-of-operations-to-make-network-connected.cpp ├── NOTES.md └── README.md ├── 1325-delete-leaves-with-a-given-value ├── 1325-delete-leaves-with-a-given-value.java ├── NOTES.md └── README.md ├── 1328-break-a-palindrome ├── 1328-break-a-palindrome.go ├── NOTES.md └── README.md ├── 1329-sort-the-matrix-diagonally ├── 1329-sort-the-matrix-diagonally.cpp ├── NOTES.md └── README.md ├── 133-clone-graph ├── 133-clone-graph.cpp ├── NOTES.md └── README.md ├── 1332-remove-palindromic-subsequences ├── 1332-remove-palindromic-subsequences.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 ├── 1338-reduce-array-size-to-the-half ├── 1338-reduce-array-size-to-the-half.cpp └── NOTES.md ├── 135-candy ├── 135-candy.java ├── NOTES.md └── README.md ├── 1356-sort-integers-by-the-number-of-1-bits ├── 1356-sort-integers-by-the-number-of-1-bits.java ├── NOTES.md └── README.md ├── 1365-how-many-numbers-are-smaller-than-the-current-number ├── 1365-how-many-numbers-are-smaller-than-the-current-number.java ├── NOTES.md └── README.md ├── 1367-linked-list-in-binary-tree ├── 1367-linked-list-in-binary-tree.java ├── NOTES.md └── README.md ├── 1372-longest-zigzag-path-in-a-binary-tree ├── 1372-longest-zigzag-path-in-a-binary-tree.java ├── NOTES.md └── README.md ├── 138-copy-list-with-random-pointer ├── 138-copy-list-with-random-pointer.cpp ├── NOTES.md └── README.md ├── 139-word-break ├── 139-word-break.cpp ├── 139-word-break.java ├── NOTES.md └── README.md ├── 1396-design-underground-system ├── 1396-design-underground-system.cpp ├── NOTES.md └── README.md ├── 140-word-break-ii ├── 140-word-break-ii.java ├── NOTES.md └── README.md ├── 1402-reducing-dishes ├── 1402-reducing-dishes.cpp ├── 1402-reducing-dishes.java ├── NOTES.md └── README.md ├── 1411-number-of-ways-to-paint-n-3-grid ├── 1411-number-of-ways-to-paint-n-3-grid.cpp ├── NOTES.md └── README.md ├── 1448-count-good-nodes-in-binary-tree ├── 1448-count-good-nodes-in-binary-tree.java ├── NOTES.md └── README.md ├── 1463-cherry-pickup-ii ├── 1463-cherry-pickup-ii.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 ├── 1470-shuffle-the-array ├── 1470-shuffle-the-array.java ├── NOTES.md └── README.md ├── 1473-paint-house-iii ├── 1473-paint-house-iii.cpp ├── NOTES.md └── README.md ├── 1482-minimum-number-of-days-to-make-m-bouquets ├── 1482-minimum-number-of-days-to-make-m-bouquets.ts ├── NOTES.md └── README.md ├── 1483-kth-ancestor-of-a-tree-node ├── 1483-kth-ancestor-of-a-tree-node.java ├── NOTES.md └── README.md ├── 1488-avoid-flood-in-the-city ├── 1488-avoid-flood-in-the-city.java ├── NOTES.md └── README.md ├── 1490-clone-n-ary-tree ├── 1490-clone-n-ary-tree.cpp ├── NOTES.md └── README.md ├── 1502-can-make-arithmetic-progression-from-sequence ├── 1502-can-make-arithmetic-progression-from-sequence.java ├── NOTES.md └── README.md ├── 1512-number-of-good-pairs ├── 1512-number-of-good-pairs.java ├── NOTES.md └── README.md ├── 1528-shuffle-string ├── 1528-shuffle-string.java ├── NOTES.md └── README.md ├── 1537-get-the-maximum-score ├── 1537-get-the-maximum-score.cpp ├── NOTES.md └── README.md ├── 1552-magnetic-force-between-two-balls ├── 1552-magnetic-force-between-two-balls.java ├── NOTES.md └── README.md ├── 1581-customer-who-visited-but-did-not-make-any-transactions ├── NOTES.md └── README.md ├── 1584-min-cost-to-connect-all-points ├── 1584-min-cost-to-connect-all-points.cpp ├── NOTES.md └── README.md ├── 1587-bank-account-summary-ii ├── NOTES.md └── README.md ├── 16-3sum-closest ├── 16-3sum-closest.go ├── NOTES.md └── README.md ├── 1609-even-odd-tree ├── 1609-even-odd-tree.cpp ├── 1609-even-odd-tree.java ├── NOTES.md └── README.md ├── 1631-path-with-minimum-effort ├── 1631-path-with-minimum-effort.cpp ├── NOTES.md └── README.md ├── 1640-check-array-formation-through-concatenation ├── 1640-check-array-formation-through-concatenation.java ├── NOTES.md └── README.md ├── 1671-minimum-number-of-removals-to-make-mountain-array ├── 1671-minimum-number-of-removals-to-make-mountain-array.cpp ├── 1671-minimum-number-of-removals-to-make-mountain-array.java ├── NOTES.md └── README.md ├── 1691-maximum-height-by-stacking-cuboids ├── 1691-maximum-height-by-stacking-cuboids.cpp ├── 1691-maximum-height-by-stacking-cuboids.java ├── NOTES.md └── README.md ├── 1693-daily-leads-and-partners ├── 1693-daily-leads-and-partners.sql ├── NOTES.md └── README.md ├── 1706-where-will-the-ball-fall ├── 1706-where-will-the-ball-fall.cpp ├── NOTES.md └── README.md ├── 1721-swapping-nodes-in-a-linked-list ├── 1721-swapping-nodes-in-a-linked-list.java ├── NOTES.md └── README.md ├── 173-binary-search-tree-iterator ├── 173-binary-search-tree-iterator.cpp ├── NOTES.md └── README.md ├── 174-dungeon-game ├── 174-dungeon-game.cpp ├── NOTES.md └── README.md ├── 1741-find-total-time-spent-by-each-employee └── README.md ├── 175-combine-two-tables ├── 175-combine-two-tables.sql ├── NOTES.md └── README.md ├── 1757-recyclable-and-low-fat-products ├── NOTES.md └── README.md ├── 1760-minimum-limit-of-balls-in-a-bag ├── 1760-minimum-limit-of-balls-in-a-bag.java ├── 1760-minimum-limit-of-balls-in-a-bag.ts ├── NOTES.md └── README.md ├── 1795-rearrange-products-table ├── NOTES.md └── README.md ├── 1832-check-if-the-sentence-is-pangram ├── 1832-check-if-the-sentence-is-pangram.java ├── NOTES.md └── README.md ├── 1873-calculate-special-bonus ├── 1873-calculate-special-bonus.sql ├── NOTES.md └── README.md ├── 188-best-time-to-buy-and-sell-stock-iv ├── 188-best-time-to-buy-and-sell-stock-iv.java ├── NOTES.md └── README.md ├── 1886-determine-whether-matrix-can-be-obtained-by-rotation ├── 1886-determine-whether-matrix-can-be-obtained-by-rotation.cpp ├── 1886-determine-whether-matrix-can-be-obtained-by-rotation.java ├── NOTES.md └── README.md ├── 1905-count-sub-islands ├── 1905-count-sub-islands.cpp ├── NOTES.md └── README.md ├── 1964-find-the-longest-valid-obstacle-course-at-each-position ├── 1964-find-the-longest-valid-obstacle-course-at-each-position.cpp ├── 1964-find-the-longest-valid-obstacle-course-at-each-position.java ├── NOTES.md └── README.md ├── 198-house-robber ├── 198-house-robber.cpp ├── NOTES.md └── README.md ├── 199-binary-tree-right-side-view ├── 199-binary-tree-right-side-view.cpp ├── 199-binary-tree-right-side-view.java ├── NOTES.md └── README.md ├── 2-add-two-numbers ├── 2-add-two-numbers.cpp ├── NOTES.md └── README.md ├── 20-valid-parentheses ├── 20-valid-parentheses.cpp ├── NOTES.md └── README.md ├── 200-number-of-islands ├── 200-number-of-islands.cpp ├── 200-number-of-islands.java ├── NOTES.md └── README.md ├── 2006-count-number-of-pairs-with-absolute-difference-k ├── 2006-count-number-of-pairs-with-absolute-difference-k.java └── NOTES.md ├── 207-course-schedule ├── 207-course-schedule.cpp ├── NOTES.md └── README.md ├── 208-implement-trie-prefix-tree ├── 208-implement-trie-prefix-tree.cpp ├── NOTES.md └── README.md ├── 2096-step-by-step-directions-from-a-binary-tree-node-to-another ├── 2096-step-by-step-directions-from-a-binary-tree-node-to-another.java ├── NOTES.md └── README.md ├── 210-course-schedule-ii ├── 210-course-schedule-ii.cpp └── NOTES.md ├── 2102-sequentially-ordinal-rank-tracker ├── 2102-sequentially-ordinal-rank-tracker.cpp ├── NOTES.md └── README.md ├── 212-word-search-ii ├── 212-word-search-ii.cpp ├── NOTES.md └── README.md ├── 215-kth-largest-element-in-an-array ├── 215-kth-largest-element-in-an-array.java ├── NOTES.md └── README.md ├── 221-maximal-square ├── 221-maximal-square.cpp ├── NOTES.md └── README.md ├── 2218-maximum-value-of-k-coins-from-piles ├── 2218-maximum-value-of-k-coins-from-piles.java ├── NOTES.md └── README.md ├── 2265-count-nodes-equal-to-average-of-subtree ├── 2265-count-nodes-equal-to-average-of-subtree.java ├── NOTES.md └── README.md ├── 2279-maximum-bags-with-full-capacity-of-rocks ├── 2279-maximum-bags-with-full-capacity-of-rocks.java ├── NOTES.md └── README.md ├── 2296-design-a-text-editor ├── 2296-design-a-text-editor.java ├── NOTES.md └── README.md ├── 23-merge-k-sorted-lists ├── 23-merge-k-sorted-lists.java ├── NOTES.md └── README.md ├── 230-kth-smallest-element-in-a-bst ├── 230-kth-smallest-element-in-a-bst.cpp ├── NOTES.md └── README.md ├── 2325-decode-the-message ├── 2325-decode-the-message.cpp ├── NOTES.md └── README.md ├── 2328-number-of-increasing-paths-in-a-grid ├── 2328-number-of-increasing-paths-in-a-grid.java └── README.md ├── 235-lowest-common-ancestor-of-a-binary-search-tree ├── 235-lowest-common-ancestor-of-a-binary-search-tree.java ├── NOTES.md └── README.md ├── 236-lowest-common-ancestor-of-a-binary-tree ├── 236-lowest-common-ancestor-of-a-binary-tree.java ├── NOTES.md └── README.md ├── 2360-longest-cycle-in-a-graph ├── 2360-longest-cycle-in-a-graph.java ├── NOTES.md └── README.md ├── 2385-amount-of-time-for-binary-tree-to-be-infected ├── 2385-amount-of-time-for-binary-tree-to-be-infected.cpp ├── 2385-amount-of-time-for-binary-tree-to-be-infected.java ├── NOTES.md └── README.md ├── 240-search-a-2d-matrix-ii ├── 240-search-a-2d-matrix-ii.cpp ├── NOTES.md └── README.md ├── 2415-reverse-odd-levels-of-binary-tree ├── 2415-reverse-odd-levels-of-binary-tree.java ├── NOTES.md └── README.md ├── 242-valid-anagram ├── NOTES.md └── README.md ├── 2469-convert-the-temperature ├── 2469-convert-the-temperature.java ├── NOTES.md └── README.md ├── 253-meeting-rooms-ii ├── 253-meeting-rooms-ii.cpp ├── 253-meeting-rooms-ii.java ├── NOTES.md └── README.md ├── 264-ugly-number-ii ├── 264-ugly-number-ii.cpp ├── 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 └── README.md ├── 289-game-of-life ├── 289-game-of-life.cpp ├── NOTES.md └── README.md ├── 300-longest-increasing-subsequence ├── 300-longest-increasing-subsequence.cpp ├── NOTES.md └── README.md ├── 301-remove-invalid-parentheses ├── 301-remove-invalid-parentheses.cpp ├── 301-remove-invalid-parentheses.java ├── 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 ├── 322-coin-change ├── 322-coin-change.cpp ├── NOTES.md └── README.md ├── 326-power-of-three ├── 326-power-of-three.java ├── NOTES.md └── README.md ├── 33-search-in-rotated-sorted-array ├── 33-search-in-rotated-sorted-array.java ├── NOTES.md └── README.md ├── 337-house-robber-iii ├── 337-house-robber-iii.java ├── NOTES.md └── README.md ├── 342-power-of-four ├── 342-power-of-four.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 ├── 363-max-sum-of-rectangle-no-larger-than-k ├── 363-max-sum-of-rectangle-no-larger-than-k.cpp ├── NOTES.md └── README.md ├── 377-combination-sum-iv ├── 377-combination-sum-iv.java ├── NOTES.md └── README.md ├── 38-count-and-say ├── 38-count-and-say.java ├── NOTES.md └── README.md ├── 383-ransom-note ├── 383-ransom-note.java ├── NOTES.md └── README.md ├── 387-first-unique-character-in-a-string ├── 387-first-unique-character-in-a-string.java ├── NOTES.md └── README.md ├── 399-evaluate-division ├── 399-evaluate-division.cpp ├── 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 └── README.md ├── 417-pacific-atlantic-water-flow ├── 417-pacific-atlantic-water-flow.cpp ├── NOTES.md └── README.md ├── 42-trapping-rain-water ├── 42-trapping-rain-water.cpp ├── 42-trapping-rain-water.java ├── NOTES.md └── README.md ├── 429-n-ary-tree-level-order-traversal ├── 429-n-ary-tree-level-order-traversal.java ├── NOTES.md └── README.md ├── 438-find-all-anagrams-in-a-string ├── 438-find-all-anagrams-in-a-string.java ├── NOTES.md └── README.md ├── 45-jump-game-ii ├── 45-jump-game-ii.java ├── NOTES.md └── README.md ├── 450-delete-node-in-a-bst ├── 450-delete-node-in-a-bst.java ├── NOTES.md └── README.md ├── 456-132-pattern ├── 456-132-pattern.cpp ├── NOTES.md └── README.md ├── 462-minimum-moves-to-equal-array-elements-ii ├── 462-minimum-moves-to-equal-array-elements-ii.cpp ├── NOTES.md └── README.md ├── 472-concatenated-words ├── 472-concatenated-words.cpp ├── NOTES.md └── README.md ├── 473-matchsticks-to-square ├── 473-matchsticks-to-square.cpp ├── NOTES.md └── README.md ├── 48-rotate-image ├── 48-rotate-image.java ├── NOTES.md └── README.md ├── 494-target-sum ├── 494-target-sum.java ├── NOTES.md └── README.md ├── 509-fibonacci-number ├── 509-fibonacci-number.cpp ├── 509-fibonacci-number.java ├── NOTES.md └── README.md ├── 529-minesweeper ├── 529-minesweeper.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.java ├── NOTES.md └── README.md ├── 542-01-matrix ├── 542-01-matrix.cpp ├── NOTES.md └── README.md ├── 543-diameter-of-binary-tree ├── 543-diameter-of-binary-tree.java ├── NOTES.md └── README.md ├── 547-number-of-provinces ├── 547-number-of-provinces.cpp ├── 547-number-of-provinces.java ├── NOTES.md └── README.md ├── 55-jump-game ├── 55-jump-game.java ├── NOTES.md └── README.md ├── 559-maximum-depth-of-n-ary-tree ├── 559-maximum-depth-of-n-ary-tree.cpp └── README.md ├── 563-binary-tree-tilt ├── 563-binary-tree-tilt.java ├── NOTES.md └── README.md ├── 576-out-of-boundary-paths ├── 576-out-of-boundary-paths.cpp ├── NOTES.md └── README.md ├── 58-length-of-last-word ├── 58-length-of-last-word.java ├── NOTES.md └── README.md ├── 583-delete-operation-for-two-strings ├── 583-delete-operation-for-two-strings.cpp ├── NOTES.md └── README.md ├── 59-spiral-matrix-ii ├── 59-spiral-matrix-ii.cpp ├── NOTES.md └── README.md ├── 606-construct-string-from-binary-tree ├── 606-construct-string-from-binary-tree.cpp ├── 606-construct-string-from-binary-tree.java ├── NOTES.md └── README.md ├── 61-rotate-list ├── 61-rotate-list.cpp ├── NOTES.md └── README.md ├── 62-unique-paths ├── 62-unique-paths.cpp ├── NOTES.md └── README.md ├── 623-add-one-row-to-tree ├── 623-add-one-row-to-tree.go ├── 623-add-one-row-to-tree.java ├── NOTES.md └── README.md ├── 637-average-of-levels-in-binary-tree ├── 637-average-of-levels-in-binary-tree.java ├── NOTES.md └── README.md ├── 64-minimum-path-sum ├── 64-minimum-path-sum.cpp ├── NOTES.md └── README.md ├── 647-palindromic-substrings ├── 647-palindromic-substrings.cpp ├── NOTES.md └── README.md ├── 648-replace-words ├── 648-replace-words.cpp ├── NOTES.md └── README.md ├── 65-valid-number ├── 65-valid-number.cpp ├── 65-valid-number.java ├── NOTES.md └── README.md ├── 653-two-sum-iv-input-is-a-bst ├── 653-two-sum-iv-input-is-a-bst.go ├── NOTES.md └── README.md ├── 669-trim-a-binary-search-tree ├── 669-trim-a-binary-search-tree.cpp ├── 669-trim-a-binary-search-tree.java ├── NOTES.md └── README.md ├── 677-map-sum-pairs ├── 677-map-sum-pairs.cpp ├── NOTES.md └── README.md ├── 680-valid-palindrome-ii ├── 680-valid-palindrome-ii.java └── README.md ├── 682-baseball-game ├── 682-baseball-game.cpp ├── NOTES.md └── README.md ├── 684-redundant-connection ├── NOTES.md └── README.md ├── 695-max-area-of-island ├── 695-max-area-of-island.cpp ├── NOTES.md └── README.md ├── 70-climbing-stairs ├── 70-climbing-stairs.java ├── NOTES.md └── README.md ├── 700-search-in-a-binary-search-tree ├── 700-search-in-a-binary-search-tree.cpp ├── NOTES.md └── README.md ├── 701-insert-into-a-binary-search-tree ├── 701-insert-into-a-binary-search-tree.java ├── NOTES.md └── README.md ├── 703-kth-largest-element-in-a-stream ├── 703-kth-largest-element-in-a-stream.cpp ├── NOTES.md └── 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 ├── NOTES.md └── README.md ├── 71-simplify-path ├── 71-simplify-path.java ├── NOTES.md └── README.md ├── 732-my-calendar-iii ├── 732-my-calendar-iii.cpp ├── NOTES.md └── README.md ├── 733-flood-fill ├── 733-flood-fill.cpp ├── 733-flood-fill.java ├── 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 ├── 743-network-delay-time.java ├── NOTES.md └── README.md ├── 745-prefix-and-suffix-search ├── 745-prefix-and-suffix-search.java ├── NOTES.md └── README.md ├── 746-min-cost-climbing-stairs ├── 746-min-cost-climbing-stairs.java ├── NOTES.md └── README.md ├── 763-partition-labels ├── NOTES.md └── README.md ├── 771-jewels-and-stones ├── 771-jewels-and-stones.cpp ├── NOTES.md └── README.md ├── 773-sliding-puzzle ├── 773-sliding-puzzle.cpp ├── NOTES.md └── README.md ├── 775-global-and-local-inversions ├── 775-global-and-local-inversions.java ├── NOTES.md └── README.md ├── 785-is-graph-bipartite ├── 785-is-graph-bipartite.cpp ├── NOTES.md └── README.md ├── 790-domino-and-tromino-tiling ├── 790-domino-and-tromino-tiling.cpp ├── NOTES.md └── README.md ├── 796-rotate-string ├── NOTES.md └── README.md ├── 801-minimum-swaps-to-make-sequences-increasing ├── 801-minimum-swaps-to-make-sequences-increasing.cpp ├── NOTES.md └── README.md ├── 804-unique-morse-code-words ├── 804-unique-morse-code-words.cpp ├── NOTES.md └── README.md ├── 81-search-in-rotated-sorted-array-ii ├── 81-search-in-rotated-sorted-array-ii.cpp ├── NOTES.md └── README.md ├── 814-binary-tree-pruning ├── 814-binary-tree-pruning.cpp ├── NOTES.md └── README.md ├── 82-remove-duplicates-from-sorted-list-ii ├── 82-remove-duplicates-from-sorted-list-ii.java ├── NOTES.md └── README.md ├── 844-backspace-string-compare ├── 844-backspace-string-compare.cpp ├── 844-backspace-string-compare.java ├── NOTES.md └── README.md ├── 856-score-of-parentheses ├── 856-score-of-parentheses.cpp ├── NOTES.md └── README.md ├── 881-boats-to-save-people ├── 881-boats-to-save-people.cpp ├── 881-boats-to-save-people.java ├── NOTES.md └── README.md ├── 890-find-and-replace-pattern ├── 890-find-and-replace-pattern.cpp ├── 890-find-and-replace-pattern.java ├── 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.java ├── NOTES.md └── README.md ├── 905-sort-array-by-parity ├── 905-sort-array-by-parity.cpp ├── NOTES.md └── README.md ├── 91-decode-ways ├── 91-decode-ways.cpp ├── NOTES.md └── README.md ├── 912-sort-an-array ├── 912-sort-an-array.java ├── NOTES.md └── README.md ├── 92-reverse-linked-list-ii ├── 92-reverse-linked-list-ii.cpp ├── NOTES.md └── README.md ├── 923-3sum-with-multiplicity ├── 923-3sum-with-multiplicity.cpp └── NOTES.md ├── 931-minimum-falling-path-sum ├── 931-minimum-falling-path-sum.cpp ├── NOTES.md └── README.md ├── 935-knight-dialer ├── 935-knight-dialer.java ├── NOTES.md └── README.md ├── 94-binary-tree-inorder-traversal ├── 94-binary-tree-inorder-traversal.java ├── NOTES.md └── README.md ├── 940-distinct-subsequences-ii ├── 940-distinct-subsequences-ii.cpp └── README.md ├── 946-validate-stack-sequences ├── 946-validate-stack-sequences.cpp ├── NOTES.md └── README.md ├── 947-most-stones-removed-with-same-row-or-column ├── 947-most-stones-removed-with-same-row-or-column.cpp ├── NOTES.md └── README.md ├── 967-numbers-with-same-consecutive-differences ├── 967-numbers-with-same-consecutive-differences.java ├── NOTES.md └── README.md ├── 97-interleaving-string ├── 97-interleaving-string.cpp ├── NOTES.md └── README.md ├── 98-validate-binary-search-tree ├── 98-validate-binary-search-tree.java ├── NOTES.md └── README.md ├── 983-minimum-cost-for-tickets ├── 983-minimum-cost-for-tickets.cpp ├── NOTES.md └── README.md ├── 987-vertical-order-traversal-of-a-binary-tree ├── 987-vertical-order-traversal-of-a-binary-tree.java ├── 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 ├── 993-cousins-in-binary-tree ├── 993-cousins-in-binary-tree.java ├── NOTES.md └── README.md ├── 994-rotting-oranges ├── 994-rotting-oranges.java ├── NOTES.md └── README.md ├── Activity Selection - GFG ├── README.md ├── activity-selection.cpp └── activity-selection.java ├── Aggressive Cows - GFG ├── README.md └── aggressive-cows.java ├── Allocate minimum number of pages - GFG ├── README.md └── allocate-minimum-number-of-pages.java ├── Armstrong Numbers - GFG ├── README.md └── armstrong-numbers.cpp ├── Array to BST - GFG ├── README.md └── array-to-bst.java ├── Assign Mice Holes - GFG ├── README.md └── assign-mice-holes.java ├── BFS of graph - GFG ├── README.md └── bfs-of-graph.cpp ├── Binary Tree to DLL - GFG ├── README.md └── binary-tree-to-dll.java ├── Binary Tree to DLL - GFG ├── README.md └── binary-tree-to-dll.cpp ├── Boundary Traversal of binary tree - GFG ├── README.md └── boundary-traversal-of-binary-tree.java ├── Bubble Sort - GFG └── bubble-sort.java ├── Burning Tree - GFG ├── README.md └── burning-tree.java ├── Children Sum Parent - GFG ├── README.md └── children-sum-parent.java ├── Circular tour - GFG ├── README.md └── circular-tour.java ├── Consecutive 1's not allowed - GFG ├── README.md └── consecutive-1s-not-allowed.cpp ├── Count Alphabets - GFG ├── README.md └── count-alphabets.java ├── Count Digits - GFG ├── README.md └── count-digits.java ├── Count Inversions - GFG ├── README.md └── count-inversions.java ├── Count possible ways to construct buildings - GFG ├── README.md └── count-possible-ways-to-construct-buildings.cpp ├── Count subsequences of type a^i, b^j, c^k - GFG ├── README.md └── count-subsequences-of-type-ai-bj-ck.cpp ├── Count the number of ways to tile the floor of size n x m using 1 x m size tiles - GFG ├── README.md └── count-the-number-of-ways-to-tile-the-floor-of-size-n-x-m-using-1-x-m-size-tiles.cpp ├── Count ways to reach the n'th stair - GFG ├── README.md └── count-ways-to-reach-the-nth-stair.cpp ├── DFS of Graph - GFG ├── README.md └── dfs-of-graph.cpp ├── Detect cycle in an undirected graph - GFG ├── README.md └── detect-cycle-in-an-undirected-graph.cpp ├── Equilibrium Point - GFG ├── README.md └── equilibrium-point.java ├── Expression contains redundant bracket or not - GFG ├── README.md └── expression-contains-redundant-bracket-or-not.java ├── Friends Pairing Problem - GFG ├── README.md └── friends-pairing-problem.cpp ├── Image Multiplication - GFG ├── README.md └── image-multiplication.java ├── Job Sequencing Problem - GFG ├── README.md └── job-sequencing-problem.cpp ├── Largest subarray with 0 sum - GFG ├── README.md └── largest-subarray-with-0-sum.java ├── Leaves to DLL - GFG └── README.md ├── Left View of Binary Tree - GFG ├── README.md └── left-view-of-binary-tree.java ├── Level order traversal - GFG ├── README.md └── level-order-traversal.cpp ├── Longest K unique characters substring - GFG ├── README.md └── longest-k-unique-characters-substring.java ├── Longest Repeating Subsequence - GFG ├── README.md └── longest-repeating-subsequence.cpp ├── Max Sum without Adjacents - GFG ├── README.md └── max-sum-without-adjacents.java ├── Maximum value of difference of a pair of elements and their Index - GFG ├── README.md ├── maximum-value-of-difference-of-a-pair-of-elements-and-their-index.cpp └── maximum-value-of-difference-of-a-pair-of-elements-and-their-index.java ├── Min distance between two given nodes of a Binary Tree - GFG ├── README.md └── min-distance-between-two-given-nodes-of-a-binary-tree.java ├── Minimum Cost of ropes - GFG ├── README.md ├── minimum-cost-of-ropes.cpp └── minimum-cost-of-ropes.java ├── Minimum Platforms - GFG ├── README.md └── minimum-platforms.cpp ├── Nuts and Bolts Problem - GFG ├── README.md └── nuts-and-bolts-problem.cpp ├── Palindrome - GFG ├── README.md └── palindrome.java ├── Pascal Triangle - GFG ├── README.md └── pascal-triangle.cpp ├── Preorder traversal (Iterative) - GFG ├── README.md └── preorder-traversal-iterative.cpp ├── Print Diagonally - GFG ├── README.md └── print-diagonally.java ├── Rod Cutting - GFG ├── README.md └── rod-cutting.java ├── Search in a row-column sorted Matrix - GFG ├── README.md └── search-in-a-rowcolumn-sorted-matrix.java ├── Smallest distinct window - GFG ├── README.md └── smallest-distinct-window.java ├── Top View of Binary Tree - GFG ├── README.md └── top-view-of-binary-tree.java ├── Tower Of Hanoi - GFG ├── README.md └── tower-of-hanoi.java ├── Ways To Tile A Floor - GFG └── README.md ├── design-hashmap ├── README.md └── design-hashmap.cpp ├── isomorphic-strings ├── README.md └── isomorphic-strings.java ├── minimum-index-sum-of-two-lists ├── README.md └── minimum-index-sum-of-two-lists.java ├── nCr - GFG ├── README.md └── n-cr.cpp ├── nPr - GFG ├── README.md └── n-pr.cpp └── valid-sudoku └── README.md /0001-two-sum/0001-two-sum.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] twoSum(int[] nums, int target) { 3 | 4 | for (int i = 0; i < nums.length; i++) { 5 | for (int j = i + 1; j < nums.length; j++) { 6 | if (nums[i] + nums[j] == target) { 7 | return new int[] {i, j}; 8 | } 9 | } 10 | } 11 | 12 | return new int[] {-1, -1}; 13 | } 14 | } -------------------------------------------------------------------------------- /0001-two-sum/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0006-zigzag-conversion/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0017-letter-combinations-of-a-phone-number/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0018-4sum/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0038-count-and-say/0038-count-and-say.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string countAndSay(int n) { 4 | if (n == 1) return "1"; 5 | 6 | string str = "1"; 7 | for (int i = 2; i <= n; i++) { 8 | string nextString = ""; 9 | int cnt = 1; 10 | char ch = str[0]; 11 | 12 | for (int j = 1; j < str.size(); j++) { 13 | if (ch == str[j]) { 14 | cnt++; 15 | } else { 16 | // cnt = 1, ch = '2' -> cnt + ch = '3' or '12' 17 | // nextString += cnt + '' + ch; 18 | nextString += to_string(cnt) + ch; 19 | ch = str[j]; 20 | cnt = 1; 21 | } 22 | } 23 | nextString += to_string(cnt) + ch; 24 | str = nextString; 25 | } 26 | 27 | return str; 28 | } 29 | }; -------------------------------------------------------------------------------- /0038-count-and-say/0038-count-and-say.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | 3 | public String countAndSay(int n) { 4 | if (n == 1) return "1"; 5 | 6 | String str = "1"; 7 | for (int i = 2; i <= n; i++) { 8 | String nextString = ""; 9 | int cnt = 1; 10 | char ch = str.charAt(0); 11 | 12 | for (int j = 1; j < str.length(); j++) { 13 | if (ch == str.charAt(j)) { 14 | cnt++; 15 | } else { 16 | // cnt = 1, ch = '2' -> cnt + ch = '3' or '12' 17 | // nextString += cnt + '' + ch; 18 | nextString += cnt + "" + ch; 19 | ch = str.charAt(j); 20 | cnt = 1; 21 | } 22 | } 23 | nextString += cnt + "" + ch; 24 | str = nextString; 25 | } 26 | 27 | return str; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /0038-count-and-say/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0041-first-missing-positive/0041-first-missing-positive.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int firstMissingPositive(int[] nums) { 3 | int i = 0; 4 | while (i < nums.length) { 5 | if (nums[i] >= 1 && nums[i] <= nums.length) { 6 | int correctPos = nums[i] - 1; 7 | if (correctPos != i && nums[correctPos] != correctPos + 1) { 8 | int temp = nums[correctPos]; 9 | nums[correctPos] = nums[i]; 10 | nums[i] = temp; 11 | } else { 12 | i++; 13 | } 14 | } else { 15 | i++; 16 | } 17 | } 18 | 19 | for (int j = 0; j < nums.length; j++) { 20 | if (j + 1 != nums[j]) { 21 | return j + 1; 22 | } 23 | } 24 | 25 | return nums.length + 1; 26 | } 27 | } -------------------------------------------------------------------------------- /0041-first-missing-positive/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0042-trapping-rain-water/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0048-rotate-image/0048-rotate-image.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | void swap(int[][] matrix, int x1, int y1, int x2, int y2) { 3 | int temp = matrix[x1][y1]; 4 | matrix[x1][y1] = matrix[x2][y2]; 5 | matrix[x2][y2] = temp; 6 | } 7 | 8 | void reverse(int[][] matrix, int r) { 9 | int si = 0; 10 | int ei = matrix[r].length - 1; 11 | while (si <= ei) { 12 | swap(matrix, r, si, r, ei); 13 | si++; 14 | ei--; 15 | } 16 | } 17 | 18 | void transpose(int[][] matrix) { 19 | for (int i = 0; i < matrix.length; i++) { 20 | for (int j = 0; j < i; j++) { 21 | swap(matrix, i, j, j, i); 22 | } 23 | } 24 | } 25 | 26 | public void rotate(int[][] matrix) { 27 | transpose(matrix); 28 | for (int r = 0; r < matrix.length; r++) { 29 | reverse(matrix, r); 30 | } 31 | } 32 | } -------------------------------------------------------------------------------- /0048-rotate-image/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0049-group-anagrams/0049-group-anagrams.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> groupAnagrams(vector& strs) { 4 | vector> ans; 5 | unordered_map> umap; 6 | for(string& str : strs) { 7 | vectorfreq (26, 0); 8 | for(char ch : str) 9 | freq[ch - 'a']++; 10 | 11 | string code = ""; 12 | for(int i = 0; i < 26; i++) 13 | if(freq[i] != 0) 14 | code += string(1, (char)(i + 'a')) + "" + to_string(freq[i]); 15 | 16 | umap[code].push_back(str); 17 | } 18 | 19 | for(auto& a : umap) 20 | ans.push_back(a.second); 21 | 22 | return ans; 23 | } 24 | }; -------------------------------------------------------------------------------- /0049-group-anagrams/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0053-maximum-subarray/0053-maximum-subarray.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxSubArray(vector& nums) { 4 | int n = nums.size(); 5 | 6 | int maxSum = nums[0]; 7 | int currSum = nums[0]; 8 | for (int i = 1; i < n; i++) { 9 | int sum = currSum + nums[i]; 10 | if (sum < nums[i]) { 11 | currSum = nums[i]; 12 | } else { 13 | currSum = sum; 14 | } 15 | maxSum = max(maxSum, currSum); 16 | } 17 | 18 | return maxSum; 19 | } 20 | }; -------------------------------------------------------------------------------- /0053-maximum-subarray/0053-maximum-subarray.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int maxSubArray(int[] nums) { 3 | int n = nums.length; 4 | 5 | int maxSum = nums[0]; 6 | int currSum = nums[0]; 7 | for (int i = 1; i < n; i++) { 8 | int sum = currSum + nums[i]; 9 | if (sum < nums[i]) { 10 | currSum = nums[i]; 11 | } else { 12 | currSum = sum; 13 | } 14 | 15 | maxSum = Math.max(maxSum, currSum); 16 | } 17 | 18 | return maxSum; 19 | } 20 | } -------------------------------------------------------------------------------- /0053-maximum-subarray/0053-maximum-subarray.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def maxSubArray(self, nums: List[int]) -> int: 3 | n = len(nums) 4 | 5 | maxSum = nums[0]; 6 | currSum = nums[0]; 7 | 8 | for i in range(1, n): 9 | sum = currSum + nums[i]; 10 | if sum < nums[i]: 11 | currSum = nums[i]; 12 | else: 13 | currSum = sum; 14 | 15 | maxSum = max(maxSum, currSum); 16 | 17 | return maxSum; -------------------------------------------------------------------------------- /0053-maximum-subarray/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0054-spiral-matrix/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0056-merge-intervals/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0059-spiral-matrix-ii/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0059-spiral-matrix-ii/README.md: -------------------------------------------------------------------------------- 1 |

59. Spiral Matrix II

Medium


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

2 | 3 |

 

4 |

Example 1:

5 | 6 |
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 |
-------------------------------------------------------------------------------- /0064-minimum-path-sum/0064-minimum-path-sum.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int minPathSum(int[][] grid) { 3 | int n = grid.length; 4 | int m = grid[0].length; 5 | 6 | int[][] dp = new int[n][m]; 7 | 8 | dp[n - 1][m - 1] = grid[n - 1][m - 1]; 9 | 10 | for (int i = m - 2; i >= 0; i--) { 11 | dp[n - 1][i] = dp[n - 1][i + 1] + grid[n - 1][i]; 12 | } 13 | 14 | for (int i = n - 2; i >= 0; i--) { 15 | dp[i][m - 1] = dp[i + 1][m - 1] + grid[i][m - 1]; 16 | } 17 | 18 | for (int i = n - 2; i >= 0; i--) { 19 | for (int j = m - 2; j >= 0; j--) { 20 | dp[i][j] = Math.min(dp[i + 1][j], dp[i][j + 1]) + grid[i][j]; 21 | } 22 | } 23 | 24 | return dp[0][0]; 25 | } 26 | } -------------------------------------------------------------------------------- /0064-minimum-path-sum/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0073-set-matrix-zeroes/0073-set-matrix-zeroes.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public void setZeroes(int[][] mat) { 3 | int n = mat.length; 4 | int m = mat[0].length; 5 | 6 | HashSet row = new HashSet<>(); 7 | HashSet col = new HashSet<>(); 8 | 9 | for (int i = 0; i < n; i++) { 10 | for (int j = 0; j < m; j++) { 11 | if (mat[i][j] == 0) { 12 | row.add(i); 13 | col.add(j); 14 | } 15 | } 16 | } 17 | 18 | for (int r: row) { 19 | for (int j = 0; j < m; j++) { 20 | mat[r][j] = 0; 21 | } 22 | } 23 | 24 | for (int c: col) { 25 | for (int i = 0; i < n; i++) { 26 | mat[i][c] = 0; 27 | } 28 | } 29 | } 30 | } -------------------------------------------------------------------------------- /0073-set-matrix-zeroes/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0093-restore-ip-addresses/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0094-binary-tree-inorder-traversal/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0102-binary-tree-level-order-traversal/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0103-binary-tree-zigzag-level-order-traversal/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0105-construct-binary-tree-from-preorder-and-inorder-traversal/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0110-balanced-binary-tree/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0114-flatten-binary-tree-to-linked-list/0114-flatten-binary-tree-to-linked-list.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * public class TreeNode { 4 | * int val; 5 | * TreeNode left; 6 | * TreeNode right; 7 | * TreeNode() {} 8 | * TreeNode(int val) { this.val = val; } 9 | * TreeNode(int val, TreeNode left, TreeNode right) { 10 | * this.val = val; 11 | * this.left = left; 12 | * this.right = right; 13 | * } 14 | * } 15 | */ 16 | class Solution { 17 | // TC: O(N^2) 18 | public void flatten(TreeNode root) { 19 | if (root == null) { 20 | return; 21 | } 22 | 23 | flatten(root.left); 24 | flatten(root.right); 25 | 26 | TreeNode temp = root.right; 27 | root.right = root.left; 28 | root.left = null; 29 | TreeNode temp2 = root; 30 | while (temp2.right != null) { 31 | temp2 = temp2.right; 32 | } 33 | temp2.right = temp; 34 | } 35 | } -------------------------------------------------------------------------------- /0114-flatten-binary-tree-to-linked-list/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0121-best-time-to-buy-and-sell-stock/0121-best-time-to-buy-and-sell-stock.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int maxProfit(int[] prices) { 3 | int maxProfit = 0; 4 | int minPrice = prices[0]; 5 | for (int i = 1; i < prices.length; i++) { 6 | if (prices[i] > minPrice) { 7 | int profit = prices[i] - minPrice; 8 | if (profit > maxProfit) { 9 | maxProfit = profit; 10 | } 11 | } else { 12 | minPrice = prices[i]; 13 | } 14 | } 15 | 16 | return maxProfit; 17 | } 18 | } -------------------------------------------------------------------------------- /0121-best-time-to-buy-and-sell-stock/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0124-binary-tree-maximum-path-sum/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0125-valid-palindrome/0125-valid-palindrome.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | // alphanumeric -> 'a' - 'z', '0' - '9', 'A' - 'Z' 3 | // non-alphanumeric -> rest of char 4 | public boolean isPalindrome(String s) { 5 | s = s.toLowerCase(); 6 | int i = 0; 7 | int j = s.length() - 1; 8 | 9 | while (i < j) { 10 | while ((s.charAt(i) < 'a' || s.charAt(i) > 'z') && (s.charAt(i) < '0' || s.charAt(i) > '9') && i < j) { 11 | i++; 12 | } 13 | while ((s.charAt(j) < 'a' || s.charAt(j) > 'z') && (s.charAt(j) < '0' || s.charAt(j) > '9') && i < j) { 14 | j--; 15 | } 16 | if (s.charAt(i) != s.charAt(j)) { 17 | return false; 18 | } 19 | i++; 20 | j--; 21 | } 22 | 23 | return true; 24 | } 25 | } -------------------------------------------------------------------------------- /0125-valid-palindrome/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0134-gas-station/0134-gas-station.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int canCompleteCircuit(int[] gas, int[] cost) { 3 | int totol_gas = 0; 4 | for (int g : gas) { 5 | totol_gas += g; 6 | } 7 | 8 | int total_cost = 0; 9 | for (int c : cost) { 10 | total_cost += c; 11 | } 12 | 13 | if (totol_gas < total_cost) { 14 | return -1; 15 | } 16 | 17 | int remaining = 0; 18 | int idx = 0; 19 | for (int i = 0; i < gas.length; i++) { 20 | remaining += gas[i] - cost[i]; 21 | if (remaining < 0) { 22 | idx = i + 1; 23 | remaining = 0; 24 | } 25 | } 26 | 27 | return idx; 28 | } 29 | } -------------------------------------------------------------------------------- /0134-gas-station/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0144-binary-tree-preorder-traversal/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0145-binary-tree-postorder-traversal/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0146-lru-cache/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0151-reverse-words-in-a-string/0151-reverse-words-in-a-string.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public String reverseWords(String s) { 3 | s = s.trim(); 4 | String[] strArray = s.split("\\s+"); 5 | int i = 0; 6 | int j = strArray.length - 1; 7 | while (i < j) { 8 | String temp = strArray[i]; 9 | strArray[i] = strArray[j]; 10 | strArray[j] = temp; 11 | i++; 12 | j--; 13 | } 14 | 15 | StringBuilder ans = new StringBuilder(""); 16 | for (int k = 0; k < strArray.length; k++) { 17 | ans.append(strArray[k]); 18 | if (k < strArray.length - 1) { 19 | ans.append(" "); 20 | } 21 | } 22 | 23 | return ans.toString(); 24 | } 25 | } -------------------------------------------------------------------------------- /0151-reverse-words-in-a-string/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0154-find-minimum-in-rotated-sorted-array-ii/0154-find-minimum-in-rotated-sorted-array-ii.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int findMin(int[] nums) { 3 | int lo = 0; 4 | int hi = nums.length - 1; 5 | 6 | while (lo < hi) { 7 | int mid = (lo + hi) / 2; 8 | if (nums[mid] < nums[hi]) { 9 | hi = mid; 10 | } else if (nums[mid] > nums[hi]){ 11 | lo = mid + 1; 12 | } else { 13 | hi--; 14 | } 15 | } 16 | 17 | return nums[lo]; 18 | } 19 | } -------------------------------------------------------------------------------- /0154-find-minimum-in-rotated-sorted-array-ii/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0155-min-stack/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0183-customers-who-never-order/0183-customers-who-never-order.sql: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | SELECT Customers.name as Customers FROM Customers WHERE Customers.id NOT IN (SELECT Orders.customerId FROM Orders); -------------------------------------------------------------------------------- /0183-customers-who-never-order/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0189-rotate-array/0189-rotate-array.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public void swap(int[] nums, int si, int ei) { 3 | int temp = nums[si]; 4 | nums[si] = nums[ei]; 5 | nums[ei] = temp; 6 | } 7 | 8 | public void reverse(int[] nums, int si, int ei) { 9 | while (si <= ei) { 10 | swap(nums, si, ei); 11 | si++; 12 | ei--; 13 | } 14 | } 15 | 16 | // TC: O(N), SC: O(1) 17 | public void rotate(int[] nums, int k) { 18 | int n = nums.length; 19 | 20 | k = k % n; 21 | 22 | reverse(nums, n - k, n - 1); 23 | reverse(nums, 0, n - k - 1); 24 | reverse(nums, 0, n - 1); 25 | } 26 | } -------------------------------------------------------------------------------- /0189-rotate-array/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0199-binary-tree-right-side-view/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0226-invert-binary-tree/0226-invert-binary-tree.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * public class TreeNode { 4 | * int val; 5 | * TreeNode left; 6 | * TreeNode right; 7 | * TreeNode() {} 8 | * TreeNode(int val) { this.val = val; } 9 | * TreeNode(int val, TreeNode left, TreeNode right) { 10 | * this.val = val; 11 | * this.left = left; 12 | * this.right = right; 13 | * } 14 | * } 15 | */ 16 | class Solution { 17 | public TreeNode invertTree(TreeNode root) { 18 | if (root == null) return null; 19 | 20 | TreeNode leftSubTree = invertTree (root.left); 21 | 22 | TreeNode rightSubTree = invertTree (root.right); 23 | 24 | root.left = rightSubTree; 25 | root.right = leftSubTree; 26 | 27 | return root; 28 | } 29 | } -------------------------------------------------------------------------------- /0226-invert-binary-tree/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0236-lowest-common-ancestor-of-a-binary-tree/0236-lowest-common-ancestor-of-a-binary-tree.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * public class TreeNode { 4 | * int val; 5 | * TreeNode left; 6 | * TreeNode right; 7 | * TreeNode(int x) { val = x; } 8 | * } 9 | */ 10 | class Solution { 11 | public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { 12 | if (root == null) return null; 13 | 14 | if (root == p ) return p; 15 | if (root == q) return q; 16 | 17 | TreeNode filc = lowestCommonAncestor (root.left, p, q); 18 | TreeNode firc = lowestCommonAncestor (root.right, p, q); 19 | 20 | if (filc != null && firc != null) return root; 21 | if (filc != null) return filc; 22 | if (firc != null) return firc; 23 | 24 | return null; 25 | } 26 | } -------------------------------------------------------------------------------- /0236-lowest-common-ancestor-of-a-binary-tree/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0239-sliding-window-maximum/0239-sliding-window-maximum.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] maxSlidingWindow(int[] nums, int k) { 3 | int n = nums.length; 4 | int[] ans = new int[n - k + 1]; 5 | int itr = 0; 6 | 7 | Deque dq = new ArrayDeque<>(); 8 | for (int i = 0; i < n; i++) { 9 | if (dq.size() > 0 && dq.peek() == i - k) { 10 | dq.removeFirst(); 11 | } 12 | 13 | while (dq.size() > 0 && nums[dq.getLast()] <= nums[i]) { 14 | dq.removeLast(); 15 | } 16 | 17 | dq.addLast(i); 18 | 19 | if (i >= k - 1) { 20 | ans[itr] = nums[dq.peek()]; 21 | itr++; 22 | } 23 | } 24 | 25 | return ans; 26 | } 27 | } -------------------------------------------------------------------------------- /0239-sliding-window-maximum/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0240-search-a-2d-matrix-ii/0240-search-a-2d-matrix-ii.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | // TC: O(N + M), SC: O(1) 3 | public boolean searchMatrix(int[][] matrix, int target) { 4 | int n = matrix.length; 5 | int m = matrix[0].length; 6 | 7 | int r = 0; 8 | int c = m - 1; 9 | 10 | while (r < n && c >= 0) { 11 | if (matrix[r][c] == target) { 12 | return true; 13 | } else if (matrix[r][c] < target) { 14 | // move towards greater side 15 | r += 1; 16 | } else { 17 | // move towards smaller side 18 | c -= 1; 19 | } 20 | } 21 | 22 | return false; 23 | } 24 | } -------------------------------------------------------------------------------- /0240-search-a-2d-matrix-ii/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0268-missing-number/0268-missing-number.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int missingNumber(int[] nums) { 3 | int i = 0; 4 | while (i < nums.length) { 5 | int correctPos = nums[i]; 6 | if (correctPos != nums.length && correctPos != i) { 7 | int temp = nums[i]; 8 | nums[i] = nums[correctPos]; 9 | nums[correctPos] = temp; 10 | } else { 11 | i++; 12 | } 13 | } 14 | 15 | for (int j = 0; j < nums.length; j++) { 16 | if (nums[j] != j) { 17 | return j; 18 | } 19 | } 20 | 21 | return nums.length; 22 | } 23 | } -------------------------------------------------------------------------------- /0268-missing-number/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0287-find-the-duplicate-number/0287-find-the-duplicate-number.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int findDuplicate(int[] nums) { 3 | int i = 0; 4 | while (i < nums.length) { 5 | int correctPos = nums[i] - 1; 6 | if (correctPos != i && nums[correctPos] != correctPos + 1) { 7 | int temp = nums[i]; 8 | nums[i] = nums[correctPos]; 9 | nums[correctPos] = temp; 10 | } else { 11 | i++; 12 | } 13 | } 14 | 15 | for (int j = 0; j < nums.length; j++) { 16 | if (nums[j] != j + 1) { 17 | return nums[j]; 18 | } 19 | } 20 | 21 | return -1; 22 | } 23 | } -------------------------------------------------------------------------------- /0287-find-the-duplicate-number/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0295-find-median-from-data-stream/0295-find-median-from-data-stream.java: -------------------------------------------------------------------------------- 1 | class MedianFinder { 2 | PriorityQueue minHeap = new PriorityQueue<>(); 3 | PriorityQueue maxHeap = new PriorityQueue<>(Collections.reverseOrder()); 4 | 5 | public MedianFinder() {} 6 | 7 | public void addNum(int num) { 8 | maxHeap.offer(num); 9 | minHeap.offer(maxHeap.poll()); 10 | if (minHeap.size() > maxHeap.size()) maxHeap.offer(minHeap.poll()); 11 | } 12 | 13 | public double findMedian() { 14 | if (maxHeap.size() > minHeap.size()) return maxHeap.peek(); 15 | return (minHeap.peek() + maxHeap.peek()) / 2.0d; 16 | } 17 | } 18 | /** 19 | * Your MedianFinder object will be instantiated and called as such: 20 | * MedianFinder obj = new MedianFinder(); 21 | * obj.addNum(num); 22 | * double param_2 = obj.findMedian(); 23 | */ 24 | -------------------------------------------------------------------------------- /0295-find-median-from-data-stream/0295-find-median-from-data-stream.py: -------------------------------------------------------------------------------- 1 | class MedianFinder: 2 | def __init__(self): 3 | self.minHeap = [] 4 | self.maxHeap = [] 5 | 6 | def addNum(self, num: int) -> None: 7 | heappush(self.maxHeap, -num) 8 | heappush(self.minHeap, -heappop(self.maxHeap)) 9 | if len(self.minHeap) > len(self.maxHeap): 10 | heappush(self.maxHeap, -heappop(self.minHeap)) 11 | 12 | def findMedian(self) -> float: 13 | if len(self.maxHeap) > len(self.minHeap): 14 | return -self.maxHeap[0] 15 | return (-self.maxHeap[0] + self.minHeap[0]) / 2 16 | 17 | 18 | 19 | # Your MedianFinder object will be instantiated and called as such: 20 | # obj = MedianFinder() 21 | # obj.addNum(num) 22 | # param_2 = obj.findMedian() -------------------------------------------------------------------------------- /0295-find-median-from-data-stream/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0297-serialize-and-deserialize-binary-tree/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0300-longest-increasing-subsequence/0300-longest-increasing-subsequence.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int lengthOfLIS(vector& nums) { 4 | int n = nums.size(); 5 | 6 | vector temp; 7 | for (int i = 0; i < n; i++) { 8 | int idx = lower_bound(temp.begin(), temp.end(), nums[i]) - temp.begin(); 9 | if (idx == temp.size()) { 10 | temp.push_back(nums[i]); 11 | } else { 12 | temp[idx] = nums[i]; 13 | } 14 | } 15 | 16 | return temp.size(); 17 | } 18 | }; -------------------------------------------------------------------------------- /0300-longest-increasing-subsequence/0300-longest-increasing-subsequence.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int lengthOfLIS(int[] nums) { 3 | int n = nums.length; 4 | 5 | int[] dp = new int[n]; 6 | Arrays.fill(dp, 1); 7 | int maxLen = 1; 8 | for (int i = 0; i < n; i++) { 9 | for (int j = 0; j < i; j++) { 10 | if (nums[j] < nums[i]) { 11 | dp[i] = Math.max(dp[i], dp[j] + 1); 12 | } 13 | } 14 | maxLen = Math.max(maxLen, dp[i]); 15 | } 16 | 17 | return maxLen; 18 | } 19 | } -------------------------------------------------------------------------------- /0300-longest-increasing-subsequence/0300-longest-increasing-subsequence.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def lengthOfLIS(self, nums: List[int]) -> int: 3 | n = len(nums) 4 | 5 | dp = [1]*n 6 | maxLen = 1 7 | for i in range(n): 8 | for j in range(i): 9 | if nums[j] < nums[i]: 10 | dp[i] = max(dp[i], dp[j] + 1) 11 | maxLen = max(maxLen, dp[i]) 12 | 13 | return maxLen -------------------------------------------------------------------------------- /0300-longest-increasing-subsequence/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0319-bulb-switcher/0319-bulb-switcher.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int bulbSwitch(int n) { 3 | int ans = 0; 4 | for (int i = 1; i * i <= n; i++) { 5 | ans++; 6 | } 7 | return ans; 8 | } 9 | } -------------------------------------------------------------------------------- /0319-bulb-switcher/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0344-reverse-string/0344-reverse-string.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public void reverseString(char[] s) { 3 | int i = 0; 4 | int j = s.length - 1; 5 | while (i < j) { 6 | char temp = s[i]; 7 | s[i] = s[j]; 8 | s[j] = temp; 9 | 10 | i++; 11 | j--; 12 | } 13 | } 14 | } -------------------------------------------------------------------------------- /0344-reverse-string/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0352-data-stream-as-disjoint-intervals/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0354-russian-doll-envelopes/0354-russian-doll-envelopes.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxEnvelopes(vector>& envelopes) { 4 | int n = envelopes.size(); 5 | 6 | sort(envelopes.begin(), envelopes.end(), [](auto& a, auto& b) { 7 | if (a[0] == b[0]) { 8 | return a[1] > b[1]; 9 | } 10 | return a[0] < b[0]; 11 | }); 12 | 13 | vector temp; 14 | for (int i = 0; i < n; i++) { 15 | int idx = lower_bound(temp.begin(), temp.end(), envelopes[i][1]) - temp.begin(); 16 | if (idx == temp.size()) { 17 | temp.push_back(envelopes[i][1]); 18 | } else { 19 | temp[idx] = envelopes[i][1]; 20 | } 21 | } 22 | 23 | return temp.size(); 24 | } 25 | }; -------------------------------------------------------------------------------- /0354-russian-doll-envelopes/0354-russian-doll-envelopes.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxEnvelopes(vector>& envelopes) { 4 | int n = envelopes.size(); 5 | 6 | sort(envelopes.begin(), envelopes.end(), [](auto& a, auto& b) { 7 | if(a[0] == b[0]) { 8 | return a[1] > b[1]; 9 | } 10 | return a[0] < b[0]; 11 | }); 12 | 13 | vector dp; 14 | int maxRussianDoll = 0; 15 | 16 | for (int i = 0; i < n; i++) { 17 | int idx = lower_bound(dp.begin(), dp.end(), envelopes[i][1]) - dp.begin(); 18 | if (idx == dp.size()) { 19 | dp.push_back(envelopes[i][1]); 20 | maxRussianDoll++; 21 | } else { 22 | dp[idx] = envelopes[i][1]; 23 | } 24 | } 25 | 26 | return maxRussianDoll; 27 | } 28 | }; -------------------------------------------------------------------------------- /0354-russian-doll-envelopes/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0373-find-k-pairs-with-smallest-sums/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0442-find-all-duplicates-in-an-array/0442-find-all-duplicates-in-an-array.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public List findDuplicates(int[] nums) { 3 | int i = 0; 4 | while (i < nums.length) { 5 | int correctPos = nums[i] - 1; 6 | if (correctPos != i && nums[correctPos] != correctPos + 1) { 7 | int temp = nums[correctPos]; 8 | nums[correctPos] = nums[i]; 9 | nums[i] = temp; 10 | } else { 11 | i++; 12 | } 13 | } 14 | 15 | List ans = new ArrayList<>(); 16 | for (int j = 0; j < nums.length; j++) { 17 | if (j + 1 != nums[j]) { 18 | ans.add(nums[j]); 19 | } 20 | } 21 | 22 | return ans; 23 | } 24 | } -------------------------------------------------------------------------------- /0442-find-all-duplicates-in-an-array/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0448-find-all-numbers-disappeared-in-an-array/0448-find-all-numbers-disappeared-in-an-array.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public List findDisappearedNumbers(int[] nums) { 3 | int i = 0; 4 | while (i < nums.length) { 5 | int correctPos = nums[i] - 1; 6 | if (correctPos != i && nums[correctPos] != correctPos + 1) { 7 | int temp = nums[correctPos]; 8 | nums[correctPos] = nums[i]; 9 | nums[i] = temp; 10 | } else { 11 | i++; 12 | } 13 | } 14 | 15 | List ans = new ArrayList<>(); 16 | for (int j = 0; j < nums.length; j++) { 17 | if (j + 1 != nums[j]) { 18 | ans.add(j + 1); 19 | } 20 | } 21 | 22 | return ans; 23 | } 24 | } -------------------------------------------------------------------------------- /0448-find-all-numbers-disappeared-in-an-array/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0456-132-pattern/0456-132-pattern.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | class Pair { 3 | int val; 4 | int prevMin; 5 | 6 | Pair (int val, int prevMin) { 7 | this.val = val; 8 | this.prevMin = prevMin; 9 | } 10 | } 11 | 12 | public boolean find132pattern(int[] nums) { 13 | Stack st = new Stack<>(); 14 | st.push(new Pair(nums[0], Integer.MAX_VALUE)); 15 | 16 | int minVal = nums[0]; 17 | 18 | for (int i = 1; i < nums.length; i++) { 19 | while (st.size() > 0 && st.peek().val <= nums[i]) { 20 | st.pop(); 21 | } 22 | 23 | if (st.size() > 0 && nums[i] > st.peek().prevMin) { 24 | return true; 25 | } 26 | 27 | st.push(new Pair(nums[i], minVal)); 28 | minVal = Math.min(minVal, nums[i]); 29 | } 30 | 31 | return false; 32 | } 33 | } -------------------------------------------------------------------------------- /0456-132-pattern/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0460-lfu-cache/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0472-concatenated-words/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0491-non-decreasing-subsequences/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0491-non-decreasing-subsequences/README.md: -------------------------------------------------------------------------------- 1 |

491. Non-decreasing Subsequences

Medium


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

2 | 3 |

 

4 |

Example 1:

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

Example 2:

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

 

17 |

Constraints:

18 | 19 |
    20 |
  • 1 <= nums.length <= 15
  • 21 |
  • -100 <= nums[i] <= 100
  • 22 |
23 |
-------------------------------------------------------------------------------- /0503-next-greater-element-ii/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0540-single-element-in-a-sorted-array/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0543-diameter-of-binary-tree/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0567-permutation-in-string/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0584-find-customer-referee/0584-find-customer-referee.sql: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | SELECT name FROM Customer WHERE referee_id is NULL OR referee_id != 2; -------------------------------------------------------------------------------- /0584-find-customer-referee/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0595-big-countries/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0627-swap-salary/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0645-set-mismatch/0645-set-mismatch.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector findErrorNums(vector& nums) { 4 | int n = nums.size(); 5 | int xor1 = 0; 6 | for (int i = 1; i <= n; i++) { 7 | xor1 = xor1 xor i; 8 | } 9 | int occTwice = 1; 10 | for (int i = 0; i < n; i++) { 11 | int num = abs(nums[i]); 12 | xor1 = xor1 xor num; 13 | if (nums[num - 1] > 0) { 14 | nums[num - 1] = -nums[num - 1]; 15 | } else { 16 | occTwice = num; 17 | } 18 | } 19 | return {occTwice, xor1 xor occTwice}; 20 | } 21 | }; -------------------------------------------------------------------------------- /0645-set-mismatch/0645-set-mismatch.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] findErrorNums(int[] nums) { 3 | int i = 0; 4 | while (i < nums.length) { 5 | int correctPos = nums[i] - 1; 6 | if (correctPos != i && nums[correctPos] != correctPos + 1) { 7 | int temp = nums[correctPos]; 8 | nums[correctPos] = nums[i]; 9 | nums[i] = temp; 10 | } else { 11 | i++; 12 | } 13 | } 14 | 15 | int[] ans = new int[2]; 16 | for (int j = 0; j < nums.length; j++) { 17 | if (j + 1 != nums[j]) { 18 | ans[0] = nums[j]; 19 | ans[1] = j + 1; 20 | } 21 | } 22 | 23 | return ans; 24 | } 25 | } -------------------------------------------------------------------------------- /0645-set-mismatch/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0678-valid-parenthesis-string/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0735-asteroid-collision/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0766-toeplitz-matrix/0766-toeplitz-matrix.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isToeplitzMatrix(vector>& matrix) { 4 | int n = matrix.size(); 5 | int m = matrix[0].size(); 6 | 7 | for (int i = 0; i < n - 1; i++) { 8 | for (int j = 0; j < m - 1; j++) { 9 | if (matrix[i][j] != matrix[i + 1][j + 1]) { 10 | return false; 11 | } 12 | } 13 | } 14 | 15 | return true; 16 | } 17 | }; -------------------------------------------------------------------------------- /0766-toeplitz-matrix/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0787-cheapest-flights-within-k-stops/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0857-minimum-cost-to-hire-k-workers/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0862-shortest-subarray-with-sum-at-least-k/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0863-all-nodes-distance-k-in-binary-tree/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0901-online-stock-span/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0904-fruit-into-baskets/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0921-minimum-add-to-make-parentheses-valid/0921-minimum-add-to-make-parentheses-valid.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int minAddToMakeValid(String s) { 3 | if (s.length() == 0) { 4 | return 0; 5 | } 6 | 7 | int open = 0; 8 | int extra = 0; 9 | for (char ch: s.toCharArray()) { 10 | if (ch == '(') { 11 | open++; 12 | } else { 13 | if (open > 0) { 14 | open--; 15 | } else { 16 | extra++; 17 | } 18 | } 19 | } 20 | 21 | return Math.abs(open) + extra; 22 | } 23 | } -------------------------------------------------------------------------------- /0921-minimum-add-to-make-parentheses-valid/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0941-valid-mountain-array/0941-valid-mountain-array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool validMountainArray(vector& arr) { 4 | int i = 0; 5 | while (i + 1 < arr.size() && arr[i] < arr[i + 1]) { 6 | i++; 7 | } 8 | int j = arr.size() - 1; 9 | while (j - 1 >= 0 && arr[j] < arr[j - 1]) { 10 | j--; 11 | } 12 | 13 | if(i == 0) return false; 14 | 15 | if(j == arr.size() - 1) return false; 16 | 17 | return i == j; 18 | } 19 | }; -------------------------------------------------------------------------------- /0941-valid-mountain-array/0941-valid-mountain-array.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean validMountainArray(int[] arr) { 3 | int i = 0; 4 | while (i + 1 < arr.length && arr[i] < arr[i + 1]) { 5 | i++; 6 | } 7 | int j = arr.length - 1; 8 | while (j - 1 >= 0 && arr[j] < arr[j - 1]) { 9 | j--; 10 | } 11 | 12 | if(i == 0) return false; 13 | 14 | if(j == arr.length - 1) return false; 15 | 16 | return i == j; 17 | } 18 | } -------------------------------------------------------------------------------- /0941-valid-mountain-array/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0973-k-closest-points-to-origin/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0976-largest-perimeter-triangle/0976-largest-perimeter-triangle.go: -------------------------------------------------------------------------------- 1 | func largestPerimeter(nums []int) int { 2 | sort.Ints(nums) 3 | for i := len(nums) - 1; i >= 2; i-- { 4 | if nums[i] < (nums[i - 1] + nums[i - 2]) { 5 | return nums[i] + nums[i - 1] + nums[i - 2] 6 | } 7 | } 8 | return 0 9 | } -------------------------------------------------------------------------------- /0976-largest-perimeter-triangle/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0976-largest-perimeter-triangle/README.md: -------------------------------------------------------------------------------- 1 |

976. Largest Perimeter Triangle

Easy


Given an integer array nums, return the largest perimeter of a triangle with a non-zero area, formed from three of these lengths. If it is impossible to form any triangle of a non-zero area, return 0.

2 | 3 |

 

4 |

Example 1:

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

Example 2:

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

 

17 |

Constraints:

18 | 19 |
    20 |
  • 3 <= nums.length <= 104
  • 21 |
  • 1 <= nums[i] <= 106
  • 22 |
23 |
-------------------------------------------------------------------------------- /0983-minimum-cost-for-tickets/0983-minimum-cost-for-tickets.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int memo(int day, unordered_set& set, vector& costs, vector& dp) { 4 | if(day >= 366) { 5 | return 0; 6 | } 7 | 8 | if(dp[day] != 0) { 9 | return dp[day]; 10 | } 11 | 12 | if(set.find(day) != set.end()) { 13 | return dp[day] = min({memo(day + 1, set, costs, dp) + costs[0], memo(day + 7, set, costs, dp) + costs[1], memo(day + 30, set, costs, dp) + costs[2]}); 14 | } else { 15 | return dp[day] = memo(day + 1, set, costs, dp); 16 | } 17 | } 18 | 19 | int mincostTickets(vector& days, vector& costs) { 20 | unordered_set set (days.begin(), days.end()); 21 | vector dp(366, 0); 22 | return memo(1, set, costs, dp); 23 | } 24 | }; -------------------------------------------------------------------------------- /0983-minimum-cost-for-tickets/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0987-vertical-order-traversal-of-a-binary-tree/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0994-rotting-oranges/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0997-find-the-town-judge/0997-find-the-town-judge.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int findJudge(int n, int[][] trust) { 3 | int[] trustee = new int[n + 1]; 4 | int[] trusts = new int[n + 1]; 5 | 6 | for(var t : trust) { 7 | trusts[t[1]]++; 8 | trustee[t[0]]++; 9 | } 10 | 11 | for(int i = 1; i < n + 1; i++) { 12 | if(trusts[i] == n - 1 && trustee[i] == 0) { 13 | return i; 14 | } 15 | } 16 | 17 | return -1; 18 | } 19 | } -------------------------------------------------------------------------------- /0two-sum/0two-sum.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] twoSum(int[] nums, int target) { 3 | int sum = 0; 4 | for(int i = 0; i < nums.length; i++) { 5 | for(int j = i + 1; j < nums.length; j++) { 6 | sum = nums[i] + nums[j]; 7 | if(sum == target) 8 | return new int[] {i, j}; 9 | } 10 | } 11 | 12 | return new int[] {0}; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /100-same-tree/100-same-tree.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * public class TreeNode { 4 | * int val; 5 | * TreeNode left; 6 | * TreeNode right; 7 | * TreeNode() {} 8 | * TreeNode(int val) { this.val = val; } 9 | * TreeNode(int val, TreeNode left, TreeNode right) { 10 | * this.val = val; 11 | * this.left = left; 12 | * this.right = right; 13 | * } 14 | * } 15 | */ 16 | class Solution { 17 | public boolean isSameTree(TreeNode p, TreeNode q) { 18 | if(p == null && q == null) { 19 | return true; 20 | } 21 | 22 | if(p == null) { 23 | return false; 24 | } 25 | 26 | if(q == null) { 27 | return false; 28 | } 29 | 30 | return isSameTree(p.left, q.left) && isSameTree(p.right, q.right) && (p.val == q.val); 31 | } 32 | } -------------------------------------------------------------------------------- /100-same-tree/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1005-maximize-sum-of-array-after-k-negations/1005-maximize-sum-of-array-after-k-negations.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int largestSumAfterKNegations(vector& nums, int k) { 4 | int sum = 0; 5 | for (int n : nums) { 6 | sum += n; 7 | } 8 | 9 | priority_queue, greater> minHeap (nums.begin(), nums.end()); 10 | while (k-- > 0) { 11 | int topEle = minHeap.top(); 12 | minHeap.pop(); 13 | sum -= topEle; 14 | minHeap.push(-topEle); 15 | sum += (-topEle); 16 | } 17 | 18 | return sum; 19 | } 20 | }; -------------------------------------------------------------------------------- /1005-maximize-sum-of-array-after-k-negations/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1007-minimum-domino-rotations-for-equal-row/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /101-symmetric-tree/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1011-capacity-to-ship-packages-within-d-days/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1018-binary-prefix-divisible-by-5/1018-binary-prefix-divisible-by-5.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector prefixesDivBy5(vector& nums) { 4 | int n = 0; 5 | vector ans; 6 | for(int i = 0; i < nums.size(); i++) { 7 | if(nums[i] == 1) 8 | n = n * 2 + 1; 9 | else 10 | n = n * 2; 11 | 12 | if(n % 5 == 0) 13 | ans.push_back(true); 14 | else 15 | ans.push_back(false); 16 | 17 | n %= 10; 18 | } 19 | return ans; 20 | } 21 | }; -------------------------------------------------------------------------------- /1018-binary-prefix-divisible-by-5/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /102-binary-tree-level-order-traversal/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1020-number-of-enclaves/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1029-two-city-scheduling/1029-two-city-scheduling.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int twoCitySchedCost(vector>& costs) { 4 | sort(costs.begin(),costs.end(), [](vector&a,vector&b){ 5 | return a[0]-a[1]& A, vector& B) { 4 | vector> dp(A.size() + 1, vector (B.size() + 1, 0)); 5 | int ans = 0; 6 | for(int i = A.size(); i >= 0; i--) 7 | for(int j = B.size(); j>= 0; j--) { 8 | if(i == A.size() || j == B.size()) { 9 | dp[i][j] = 0; 10 | continue; 11 | } 12 | ans = (A[i] == B[j]) ? dp[i + 1][j + 1] + 1 : max(dp[i + 1][j], dp[i][j + 1]); 13 | dp[i][j] = ans; 14 | } 15 | 16 | return ans; 17 | } 18 | }; 19 | 20 | 21 | auto SpeedUp = []() { 22 | std::ios::sync_with_stdio(false); 23 | std::cin.tie(nullptr); 24 | std::cout.tie(nullptr); 25 | return nullptr; 26 | }(); -------------------------------------------------------------------------------- /1035-uncrossed-lines/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1046-last-stone-weight/1046-last-stone-weight.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int lastStoneWeight(vector& stones) { 4 | priority_queue maxHeap (stones.begin(), stones.end()); 5 | while (maxHeap.size() >= 2) { 6 | int stone1 = maxHeap.top(); 7 | maxHeap.pop(); 8 | int stone2 = maxHeap.top(); 9 | maxHeap.pop(); 10 | 11 | int remain = stone1 - stone2; 12 | if (remain != 0) { 13 | maxHeap.push(remain); 14 | } 15 | } 16 | return maxHeap.size() == 0 ? 0 : maxHeap.top(); 17 | } 18 | }; -------------------------------------------------------------------------------- /1046-last-stone-weight/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1049-last-stone-weight-ii/1049-last-stone-weight-ii.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int memo(vector& stones, int sumLeft, int sumRight, int idx, int n) { 4 | if(idx == n) { 5 | return dp[idx][sumLeft] = abs(sumLeft - sumRight); 6 | } 7 | 8 | if(dp[idx][sumLeft] != -1) { 9 | return dp[idx][sumLeft] ; 10 | } 11 | 12 | int addToLeft = memo(stones, sumLeft + stones[idx], sumRight, idx + 1, n); 13 | int addToRight = memo(stones, sumLeft, sumRight + stones[idx], idx + 1, n); 14 | return dp[idx][sumLeft] = min(addToLeft, addToRight); 15 | } 16 | 17 | int dp[31][3001]; 18 | int lastStoneWeightII(vector& stones) { 19 | int n = stones.size(); 20 | memset(dp, -1, sizeof(dp)); 21 | int ans = memo(stones, 0, 0, 0, n); 22 | return ans; 23 | } 24 | }; -------------------------------------------------------------------------------- /1049-last-stone-weight-ii/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /107-binary-tree-level-order-traversal-ii/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /108-convert-sorted-array-to-binary-search-tree/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1081-smallest-subsequence-of-distinct-characters/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1091-shortest-path-in-binary-matrix/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /11-container-with-most-water/11-container-with-most-water.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxArea(vector& height) { 4 | int i=0,j=height.size()-1; 5 | int area=0; 6 | while(iheight[j]) 10 | j--; 11 | else 12 | i++; 13 | } 14 | return area; 15 | } 16 | }; 17 | auto SpeedUp = []() { 18 | std::ios::sync_with_stdio(false); 19 | std::cin.tie(nullptr); 20 | std::cout.tie(nullptr); 21 | return nullptr; 22 | }(); -------------------------------------------------------------------------------- /11-container-with-most-water/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1114-print-in-order/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1115-print-foobar-alternately/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1117-building-h2o/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1123-lowest-common-ancestor-of-deepest-leaves/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1137-n-th-tribonacci-number/1137-n-th-tribonacci-number.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int tribonacci(int n) { 3 | if (n == 0 || n == 1) return n; 4 | if (n == 2) return 1; 5 | 6 | int a = 0, b = 1, c = 1; 7 | for (int i = 3; i <= n; i++) { 8 | int d = a + b + c; 9 | a = b; 10 | b = c; 11 | c = d; 12 | } 13 | 14 | return c; 15 | } 16 | } -------------------------------------------------------------------------------- /1137-n-th-tribonacci-number/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /114-flatten-binary-tree-to-linked-list/114-flatten-binary-tree-to-linked-list.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | class Solution { 13 | public: 14 | void flatten(TreeNode* root) { 15 | if(!root) return; 16 | flatten(root->left); 17 | flatten(root->right); 18 | while(root->left) { 19 | TreeNode* rootKaRight = root->right; 20 | root->right = root->left; 21 | root->left = nullptr; 22 | while(root->right) root = root->right; 23 | root->right = rootKaRight; 24 | } 25 | } 26 | }; -------------------------------------------------------------------------------- /114-flatten-binary-tree-to-linked-list/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /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.size(); 5 | int m = text2.size(); 6 | 7 | vector> dp(n + 1, vector (m + 1, 0)); 8 | for (int i = n - 1; i >= 0; i--) { 9 | for (int j = m - 1; j >= 0; j--) { 10 | if (text1[i] == text2[j]) { 11 | dp[i][j] = dp[i + 1][j + 1] + 1; 12 | } else { 13 | dp[i][j] = max(dp[i + 1][j], dp[i][j + 1]); 14 | } 15 | } 16 | } 17 | 18 | return dp[0][0]; 19 | } 20 | }; -------------------------------------------------------------------------------- /1143-longest-common-subsequence/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1145-binary-tree-coloring-game/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1179-reformat-department-table/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /118-pascals-triangle/118-pascals-triangle.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> generate(int numRows) { 4 | vector> ans(numRows); 5 | ans[0].push_back(1); 6 | for(int i = 1; i < numRows; i++) { 7 | ans[i].push_back(1); 8 | for(int j = 1; j < i; j++) { 9 | ans[i].push_back(ans[i - 1][j] + ans[i - 1][j - 1]); 10 | } 11 | ans[i].push_back(1); 12 | } 13 | return ans; 14 | } 15 | }; -------------------------------------------------------------------------------- /118-pascals-triangle/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /118-pascals-triangle/README.md: -------------------------------------------------------------------------------- 1 |

118. Pascal's Triangle

Easy


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

2 | 3 |

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

4 | 5 |

 

6 |

Example 1:

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

Example 2:

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

 

14 |

Constraints:

15 | 16 |
    17 |
  • 1 <= numRows <= 30
  • 18 |
19 |
-------------------------------------------------------------------------------- /1188-design-bounded-blocking-queue/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(rowIndex + 1, 1); 5 | int temp = 0; 6 | for(int i = 0; i <= rowIndex; i++) { 7 | for(int j = 0; j <= i; j++) { 8 | if(j == 0 || j == i) { 9 | temp = 1; 10 | ans[j] = 1; 11 | } else { 12 | int t = ans[j]; 13 | ans[j] = ans[j] + temp; 14 | temp = t; 15 | } 16 | } 17 | } 18 | 19 | return ans; 20 | } 21 | }; -------------------------------------------------------------------------------- /119-pascals-triangle-ii/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1195-fizz-buzz-multithreaded/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /120-triangle/120-triangle.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int minimumTotal(List> triangle) { 3 | int n = triangle.size(); 4 | int[] dp = new int[n]; 5 | for(int i = 0; i < n; i++) { 6 | dp[i] = triangle.get(n - 1).get(i); 7 | } 8 | 9 | for(int i = n - 2; i >= 0; i--) { 10 | for(int j = 0; j <= i; j++) { 11 | dp[j] = Math.min(dp[j], dp[j + 1]) + triangle.get(i).get(j); 12 | } 13 | } 14 | 15 | return dp[0]; 16 | } 17 | } -------------------------------------------------------------------------------- /120-triangle/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1202-smallest-string-with-swaps/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1209-remove-all-adjacent-duplicates-in-string-ii/1209-remove-all-adjacent-duplicates-in-string-ii.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string removeDuplicates(string s, int k) { 4 | stack> st; 5 | 6 | for(char &ch : s){ 7 | if(st.empty() || st.top().first != ch) st.push({ch, 1}); 8 | else st.push({ch, st.top().second + 1}); 9 | if(st.top().second == k){ 10 | int count = 0; 11 | while(count++ < k) st.pop(); 12 | } 13 | } 14 | string res = ""; 15 | while(!st.empty()) { 16 | res += st.top().first; 17 | st.pop(); 18 | } 19 | reverse(res.begin(), res.end()); 20 | return res; 21 | } 22 | }; -------------------------------------------------------------------------------- /1209-remove-all-adjacent-duplicates-in-string-ii/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /122-best-time-to-buy-and-sell-stock-ii/122-best-time-to-buy-and-sell-stock-ii.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int maxProfit(int[] prices) { 3 | int bp = 0; 4 | int sp = 0; 5 | int profit = 0; 6 | for(int i = 0; i < prices.length; i++) { 7 | if(prices[i] > prices[sp]) { 8 | sp++; 9 | } else { 10 | profit += prices[sp] - prices[bp]; 11 | sp = bp = i; 12 | } 13 | } 14 | profit += prices[sp] - prices[bp]; 15 | 16 | return profit; 17 | } 18 | } -------------------------------------------------------------------------------- /122-best-time-to-buy-and-sell-stock-ii/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1220-count-vowels-permutation/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1221-split-a-string-in-balanced-strings/1221-split-a-string-in-balanced-strings.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int balancedStringSplit(String s) { 3 | int cntR = 0; 4 | int cntL = 0; 5 | int numOfStrings = 0; 6 | for (int i = 0; i < s.length(); i++) { 7 | char ch = s.charAt(i); 8 | if (ch == 'R') { 9 | cntR++; 10 | } else if (ch == 'L') { 11 | cntL++; 12 | } 13 | 14 | if (cntR > 0 && cntL > 0 && cntL == cntR) { 15 | numOfStrings++; 16 | cntR = 0; 17 | cntL = 0; 18 | } 19 | } 20 | 21 | return numOfStrings; 22 | } 23 | } -------------------------------------------------------------------------------- /1221-split-a-string-in-balanced-strings/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1222-queens-that-can-attack-the-king/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1226-the-dining-philosophers/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /123-best-time-to-buy-and-sell-stock-iii/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1236-web-crawler/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /124-binary-tree-maximum-path-sum/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1242-web-crawler-multithreaded/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1249-minimum-remove-to-make-valid-parentheses/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /126-word-ladder-ii/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1260-shift-2d-grid/1260-shift-2d-grid.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> shiftGrid(vector>& grid, int k) { 4 | int r = grid.size(), c = grid[0].size(); 5 | vector> ans(r, vector(c)); 6 | 7 | for(int i = 0; i < r; i++) { 8 | for(int j = 0; j < c; j++) { 9 | int newj = (j + k) % c; 10 | int newi = (i + (j + k) / c) % r; 11 | ans[newi][newj] = grid[i][j]; 12 | } 13 | } 14 | 15 | return ans; 16 | } 17 | }; -------------------------------------------------------------------------------- /1260-shift-2d-grid/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1267-count-servers-that-communicate/1267-count-servers-that-communicate.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int countServers(vector>& grid) { 4 | int n = grid.size(); 5 | int m = grid[0].size(); 6 | vector row(n, 0); 7 | vector col(m, 0); 8 | for(int i = 0; i < n; i++) { 9 | for(int j = 0; j < m; j++) { 10 | if(grid[i][j] == 1) { 11 | row[i]++; 12 | col[j]++; 13 | } 14 | } 15 | } 16 | int ans = 0; 17 | for(int i = 0; i < n; i++) { 18 | for(int j = 0; j < m; j++) { 19 | if(grid[i][j] == 1 && (row[i] > 1 || col[j] > 1)) { 20 | ans++; 21 | } 22 | } 23 | } 24 | return ans; 25 | } 26 | }; -------------------------------------------------------------------------------- /1267-count-servers-that-communicate/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1268-search-suggestions-system/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /127-word-ladder/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1289-minimum-falling-path-sum-ii/1289-minimum-falling-path-sum-ii.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minFallingPathSum(vector>& grid) { 4 | int n = grid.size(); 5 | int ans = INT_MAX; 6 | vector> dp(n, vector(n, INT_MAX)); 7 | for(int j = 0; j < n; j++) { 8 | ans = min(ans, memo(grid, 0, j, n, dp)); 9 | } 10 | return ans; 11 | } 12 | 13 | int memo(vector>& grid, int r, int c, int n, vector>& dp) { 14 | if(r == n - 1) { 15 | return dp[r][c] = grid[r][c]; 16 | } 17 | 18 | if(dp[r][c] != INT_MAX) { 19 | return dp[r][c]; 20 | } 21 | 22 | int ans = INT_MAX; 23 | for(int j = 0; j < n; j++) { 24 | if(j != c) { 25 | ans = min(ans, memo(grid, r + 1, j, n, dp) + grid[r][c]); 26 | } 27 | } 28 | 29 | return dp[r][c] = ans; 30 | } 31 | }; -------------------------------------------------------------------------------- /1289-minimum-falling-path-sum-ii/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1293-shortest-path-in-a-grid-with-obstacles-elimination/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /130-surrounded-regions/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1306-jump-game-iii/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1312-minimum-insertion-steps-to-make-a-string-palindrome/1312-minimum-insertion-steps-to-make-a-string-palindrome.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int memo (String s, int si, int ei, int[][] dp) { 3 | if (si > ei) { 4 | return 0; 5 | } 6 | 7 | if (dp[si][ei] != 0) { 8 | return dp[si][ei]; 9 | } 10 | 11 | if (s.charAt(si) == s.charAt(ei)) { 12 | return dp[si][ei] = memo(s, si + 1, ei - 1, dp); 13 | } else { 14 | return dp[si][ei] = Math.min(memo(s, si + 1, ei, dp), memo(s, si, ei - 1, dp)) + 1; 15 | } 16 | } 17 | 18 | public int minInsertions(String s) { 19 | int[][] dp = new int[s.length()][s.length()]; 20 | return memo(s, 0, s.length() - 1, dp); 21 | } 22 | } -------------------------------------------------------------------------------- /1312-minimum-insertion-steps-to-make-a-string-palindrome/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1319-number-of-operations-to-make-network-connected/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1325-delete-leaves-with-a-given-value/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1328-break-a-palindrome/1328-break-a-palindrome.go: -------------------------------------------------------------------------------- 1 | func breakPalindrome(palindrome string) string { 2 | str := []rune(palindrome) 3 | 4 | if len(str) == 1 { 5 | return "" 6 | } 7 | 8 | firstIndexOfNonA := -1 9 | for index, ch := range(str) { 10 | if ch != 'a' { 11 | firstIndexOfNonA = index 12 | break; 13 | } 14 | } 15 | 16 | if (len(str) % 2 == 1 && firstIndexOfNonA == len(str) / 2) || firstIndexOfNonA == -1 { 17 | str[len(str) - 1] = 'b' 18 | } else { 19 | str[firstIndexOfNonA] = 'a' 20 | } 21 | 22 | return string(str) 23 | } -------------------------------------------------------------------------------- /1328-break-a-palindrome/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1329-sort-the-matrix-diagonally/1329-sort-the-matrix-diagonally.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> diagonalSort(vector>& mat) { 4 | int n = mat.size(); 5 | int m = mat[0].size(); 6 | 7 | unordered_map> umap; 8 | for(int i = 0; i < n; i++) { 9 | for(int j = 0; j < m; j++) { 10 | umap[i - j].push(mat[i][j]); 11 | } 12 | } 13 | 14 | for(int i = n - 1; i >= 0; i--) { 15 | for(int j = m - 1; j >= 0; j--) { 16 | mat[i][j] = umap[i - j].top(); 17 | umap[i - j].pop(); 18 | } 19 | } 20 | 21 | return mat; 22 | } 23 | }; -------------------------------------------------------------------------------- /1329-sort-the-matrix-diagonally/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /133-clone-graph/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1332-remove-palindromic-subsequences/1332-remove-palindromic-subsequences.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isPalindrome(string s) { 4 | int i = 0, j = s.length() - 1; 5 | while(i < j) { 6 | if(s[i] != s[j]) { 7 | return false; 8 | } 9 | i++; 10 | j--; 11 | } 12 | return true; 13 | } 14 | 15 | int removePalindromeSub(string s) { 16 | if(isPalindrome(s) == true) { 17 | return 1; 18 | } else { 19 | return 2; 20 | } 21 | } 22 | }; -------------------------------------------------------------------------------- /1332-remove-palindromic-subsequences/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1337-the-k-weakest-rows-in-a-matrix/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1338-reduce-array-size-to-the-half/1338-reduce-array-size-to-the-half.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minSetSize(vector& arr) { 4 | unordered_map freq; 5 | for(int a : arr) freq[a]++; 6 | priority_queue pq; 7 | for(auto& [_, cnt] : freq) pq.push(cnt); 8 | int ans = 0, n = arr.size() / 2, removed = 0; 9 | while(removed < n) { 10 | removed += pq.top(); 11 | pq.pop(); 12 | ans++; 13 | } 14 | return ans; 15 | } 16 | }; -------------------------------------------------------------------------------- /1338-reduce-array-size-to-the-half/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /135-candy/135-candy.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int candy(int[] ratings) { 3 | int n = ratings.length; 4 | 5 | int[] candies = new int[n]; 6 | candies[0] = 1; 7 | for(int i = 1; i < n; i++) { 8 | if(ratings[i] > ratings[i - 1]) { 9 | candies[i] = candies[i - 1] + 1; 10 | } else { 11 | candies[i] = 1; 12 | } 13 | } 14 | 15 | for(int i = n - 2; i >= 0; i--) { 16 | if(ratings[i] > ratings[i + 1]) { 17 | candies[i] = Math.max(candies[i], candies[i + 1] + 1); 18 | } 19 | } 20 | 21 | int totalCandies = 0; 22 | for(int candy_ : candies) { 23 | totalCandies += candy_; 24 | } 25 | 26 | return totalCandies; 27 | } 28 | } -------------------------------------------------------------------------------- /135-candy/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1356-sort-integers-by-the-number-of-1-bits/1356-sort-integers-by-the-number-of-1-bits.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int count(int n) { 3 | int count = 0; 4 | while(n > 0) { 5 | count += (n & 1); 6 | n >>= 1; 7 | } 8 | return count; 9 | } 10 | 11 | class Comp implements Comparator { 12 | public int compare(Integer a, Integer b) { 13 | int c1 = count(a); 14 | int c2 = count(b); 15 | if(c1 == c2) { 16 | return a - b; 17 | } 18 | return c1 - c2; 19 | } 20 | } 21 | 22 | public int[] sortByBits(int[] arr) { 23 | PriorityQueue pq = new PriorityQueue(new Comp()); 24 | for(int i : arr) { 25 | pq.add(i); 26 | } 27 | int[] ans = new int[arr.length]; 28 | for(int i = 0; i < arr.length; i++) { 29 | ans[i] = pq.peek(); 30 | pq.remove(); 31 | } 32 | return ans; 33 | } 34 | } -------------------------------------------------------------------------------- /1356-sort-integers-by-the-number-of-1-bits/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1365-how-many-numbers-are-smaller-than-the-current-number/1365-how-many-numbers-are-smaller-than-the-current-number.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] smallerNumbersThanCurrent(int[] nums) { 3 | int[] ans = new int[nums.length]; 4 | for (int i = 0; i < nums.length; i++) { 5 | int smaller = 0; 6 | for (int j = 0; j < nums.length; j++) { 7 | if (nums[i] > nums[j]) { 8 | smaller++; 9 | } 10 | } 11 | ans[i] = smaller; 12 | } 13 | return ans; 14 | } 15 | } -------------------------------------------------------------------------------- /1365-how-many-numbers-are-smaller-than-the-current-number/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1367-linked-list-in-binary-tree/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1372-longest-zigzag-path-in-a-binary-tree/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /138-copy-list-with-random-pointer/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /139-word-break/139-word-break.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int rec(String s, List wordDict, int idx, int[] cache) { 3 | if(idx == s.length()) return cache[idx] = 1; 4 | 5 | if(cache[idx] != 0) return cache[idx]; 6 | 7 | int ans = -1; 8 | for(int i = idx + 1; i <= s.length(); i++) { 9 | String substr = s.substring(idx, i); 10 | if(wordDict.contains(substr) == true) { 11 | int myAns = rec(s, wordDict, i, cache); 12 | if(myAns == 1) ans = 1; 13 | } 14 | } 15 | 16 | return cache[idx] = ans; 17 | } 18 | 19 | public boolean wordBreak(String s, List wordDict) { 20 | int n = s.length(); 21 | int[] cache = new int[n + 1]; 22 | Arrays.fill(cache, 0); 23 | int ans = rec(s, wordDict, 0, cache); 24 | return (ans == 1); 25 | } 26 | } -------------------------------------------------------------------------------- /139-word-break/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1396-design-underground-system/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /140-word-break-ii/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1402-reducing-dishes/1402-reducing-dishes.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int helper(vector& arr, int idx, int time, vector>& dp) { 4 | if(idx == arr.size()) { 5 | return dp[idx][time] = 0; 6 | } 7 | 8 | if(dp[idx][time] != -1) { 9 | return dp[idx][time]; 10 | } 11 | 12 | int ans = max(helper(arr, idx + 1, time, dp), helper(arr, idx + 1, time + 1, dp) + arr[idx] * time); 13 | return dp[idx][time] = ans; 14 | } 15 | 16 | int maxSatisfaction(vector& arr) { 17 | sort(arr.begin(), arr.end()); 18 | vector> dp(arr.size() + 1, vector(arr.size() + 2, -1)); 19 | int ans = helper(arr, 0, 1, dp); 20 | return ans; 21 | } 22 | }; -------------------------------------------------------------------------------- /1402-reducing-dishes/1402-reducing-dishes.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int maxSatisfaction(int[] satisfaction) { 3 | int n = satisfaction.length; 4 | 5 | Arrays.sort(satisfaction); 6 | 7 | int ans = 0; 8 | int pSum = 0; 9 | int timeCoff = 0; 10 | 11 | for (int i = n - 1; i >= 0; i--) { 12 | pSum += satisfaction[i]; 13 | timeCoff += pSum; 14 | ans = Math.max(timeCoff, ans); 15 | } 16 | 17 | return ans; 18 | } 19 | } -------------------------------------------------------------------------------- /1402-reducing-dishes/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1411-number-of-ways-to-paint-n-3-grid/1411-number-of-ways-to-paint-n-3-grid.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | #define MOD 1000000007 4 | int numOfWays(int n) { 5 | long long color2 = 6, color3 = 6; 6 | for (int i = 1; i < n; ++i) { 7 | long long t1 = color2 * 3 + color3 * 2; 8 | long long t2 = color2 * 2 + color3 * 2; 9 | color2 = t1 % MOD; 10 | color3 = t2 % MOD; 11 | } 12 | return (color2 + color3) % MOD; 13 | } 14 | }; -------------------------------------------------------------------------------- /1411-number-of-ways-to-paint-n-3-grid/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1448-count-good-nodes-in-binary-tree/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1463-cherry-pickup-ii/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1465-maximum-area-of-a-piece-of-cake-after-horizontal-and-vertical-cuts/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1470-shuffle-the-array/1470-shuffle-the-array.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] shuffle(int[] nums, int n) { 3 | int[] ans = new int[2*n]; 4 | 5 | int i = 0; 6 | int j = n; 7 | 8 | int itr = 0; 9 | 10 | while (i < n && j < 2*n) { 11 | ans[itr] = nums[i]; 12 | i++; 13 | itr++; 14 | 15 | ans[itr] = nums[j]; 16 | j++; 17 | itr++; 18 | } 19 | 20 | return ans; 21 | } 22 | } -------------------------------------------------------------------------------- /1470-shuffle-the-array/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1473-paint-house-iii/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1482-minimum-number-of-days-to-make-m-bouquets/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1483-kth-ancestor-of-a-tree-node/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1488-avoid-flood-in-the-city/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1490-clone-n-ary-tree/1490-clone-n-ary-tree.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | // Definition for a Node. 3 | class Node { 4 | public: 5 | int val; 6 | vector children; 7 | 8 | Node() {} 9 | 10 | Node(int _val) { 11 | val = _val; 12 | } 13 | 14 | Node(int _val, vector _children) { 15 | val = _val; 16 | children = _children; 17 | } 18 | }; 19 | */ 20 | 21 | class Solution { 22 | public: 23 | Node* cloneTree(Node* root) { 24 | if(!root) 25 | return nullptr; 26 | 27 | Node* clone = new Node(root->val); 28 | for(Node* child : root->children) { 29 | clone->children.push_back(cloneTree(child)); 30 | } 31 | 32 | return clone; 33 | } 34 | }; -------------------------------------------------------------------------------- /1490-clone-n-ary-tree/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1502-can-make-arithmetic-progression-from-sequence/1502-can-make-arithmetic-progression-from-sequence.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean canMakeArithmeticProgression(int[] arr) { 3 | Arrays.sort(arr); 4 | int diff = arr[1] - arr[0]; 5 | for(int i = 0; i < arr.length - 1; i++) { 6 | int currDiff = arr[i + 1] - arr[i]; 7 | if(currDiff != diff) { 8 | return false; 9 | } 10 | } 11 | return true; 12 | } 13 | } -------------------------------------------------------------------------------- /1502-can-make-arithmetic-progression-from-sequence/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1512-number-of-good-pairs/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1528-shuffle-string/1528-shuffle-string.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public String restoreString(String s, int[] indices) { 3 | char[] ans = new char[s.length()]; 4 | for (int i = 0; i < indices.length; i++) { 5 | char ch = s.charAt(i); 6 | int idx = indices[i]; 7 | ans[idx] = ch; 8 | } 9 | String str = new String(ans); 10 | return str; 11 | } 12 | } -------------------------------------------------------------------------------- /1528-shuffle-string/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1537-get-the-maximum-score/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1552-magnetic-force-between-two-balls/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1581-customer-who-visited-but-did-not-make-any-transactions/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1584-min-cost-to-connect-all-points/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1587-bank-account-summary-ii/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /16-3sum-closest/16-3sum-closest.go: -------------------------------------------------------------------------------- 1 | func abs(a int) int { 2 | if a < 0 { 3 | return -a 4 | } else { 5 | return a 6 | } 7 | } 8 | 9 | func threeSumClosest(nums []int, target int) int { 10 | n := len(nums) 11 | sort.Ints(nums) 12 | 13 | si := 0 14 | ans := (1<<32) - 1 15 | for si < n - 2 { 16 | ei := n - 1 17 | i := si + 1 18 | for i < ei { 19 | sum := nums[si] + nums[ei] + nums[i] 20 | if abs(target - sum) < abs(target - ans) { 21 | ans = sum 22 | } 23 | if sum > target { 24 | ei--; 25 | } else if sum < target { 26 | i++; 27 | } else { 28 | return target 29 | } 30 | } 31 | si++ 32 | } 33 | 34 | return ans 35 | } -------------------------------------------------------------------------------- /16-3sum-closest/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1609-even-odd-tree/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1631-path-with-minimum-effort/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1640-check-array-formation-through-concatenation/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1671-minimum-number-of-removals-to-make-mountain-array/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1691-maximum-height-by-stacking-cuboids/1691-maximum-height-by-stacking-cuboids.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxHeight(vector>& cuboids) { 4 | int n = cuboids.size(); 5 | 6 | for (int i = 0; i < n; i++) { 7 | sort(cuboids[i].begin(), cuboids[i].end()); 8 | } 9 | sort(cuboids.begin(), cuboids.end()); 10 | 11 | vector dp(n); 12 | for (int i = 0; i < n; i++) { 13 | dp[i] = cuboids[i][2]; 14 | } 15 | int maxHeightOfStack = 0; 16 | for (int i = 0; i < n; i++) { 17 | for (int j = 0; j < i; j++) { 18 | if (cuboids[i][0] >= cuboids[j][0] and cuboids[i][1] >= cuboids[j][1] and cuboids[i][2] >= cuboids[j][2]) { 19 | dp[i] = max(dp[i], dp[j] + cuboids[i][2]); 20 | } 21 | } 22 | maxHeightOfStack = max(maxHeightOfStack, dp[i]); 23 | } 24 | 25 | return maxHeightOfStack; 26 | } 27 | }; -------------------------------------------------------------------------------- /1691-maximum-height-by-stacking-cuboids/1691-maximum-height-by-stacking-cuboids.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxHeight(vector>& cuboids) { 4 | int n = cuboids.size(); 5 | 6 | for (int i = 0; i < n; i++) { 7 | sort(cuboids[i].begin(), cuboids[i].end()); 8 | } 9 | sort(cuboids.begin(), cuboids.end()); 10 | 11 | vector dp(n); 12 | for (int i = 0; i < n; i++) { 13 | dp[i] = cuboids[i][2]; 14 | } 15 | int maxStackHeight = dp[0]; 16 | for (int i = 1; i < n; i++) { 17 | for (int j = 0; j < i; j++) { 18 | if (cuboids[j][0] <= cuboids[i][0] && cuboids[j][2] <= cuboids[i][2] && cuboids[j][1] <= cuboids[i][1]) { 19 | dp[i] = max(dp[i], dp[j] + cuboids[i][2]); 20 | } 21 | } 22 | maxStackHeight = max(maxStackHeight, dp[i]); 23 | } 24 | 25 | return maxStackHeight; 26 | } 27 | }; -------------------------------------------------------------------------------- /1691-maximum-height-by-stacking-cuboids/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1693-daily-leads-and-partners/1693-daily-leads-and-partners.sql: -------------------------------------------------------------------------------- 1 | SELECT date_id, 2 | make_name, 3 | Count(DISTINCT lead_id) AS unique_leads, 4 | Count(DISTINCT partner_id) AS unique_partners 5 | FROM DailySales 6 | GROUP BY date_id, 7 | make_name; -------------------------------------------------------------------------------- /1693-daily-leads-and-partners/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1706-where-will-the-ball-fall/1706-where-will-the-ball-fall.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector findBall(vector>& grid) { 4 | int m = grid.size(); 5 | int n = grid[0].size(); 6 | 7 | vector res; 8 | for (int i = 0; i < n; ++i) { 9 | int i1 = i; 10 | int i2; 11 | for (int j = 0; j < m; ++j) { 12 | i2 = i1 + grid[j][i1]; 13 | if (i2 < 0 || i2 >= n || grid[j][i2] != grid[j][i1]) { 14 | i1 = -1; 15 | break; 16 | } 17 | i1 = i2; 18 | } 19 | res.push_back(i1); 20 | } 21 | 22 | return res; 23 | } 24 | }; -------------------------------------------------------------------------------- /1706-where-will-the-ball-fall/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1721-swapping-nodes-in-a-linked-list/1721-swapping-nodes-in-a-linked-list.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * public class ListNode { 4 | * int val; 5 | * ListNode next; 6 | * ListNode() {} 7 | * ListNode(int val) { this.val = val; } 8 | * ListNode(int val, ListNode next) { this.val = val; this.next = next; } 9 | * } 10 | */ 11 | class Solution { 12 | public ListNode swapNodes(ListNode head, int k) { 13 | 14 | ListNode temp = head, left = null, right = null; 15 | 16 | while(temp != null) { 17 | right = (right == null) ? null : right.next; 18 | if(--k == 0) { 19 | left = temp; 20 | right = head; 21 | } 22 | temp = temp.next; 23 | } 24 | 25 | int t = left.val; 26 | left.val = right.val; 27 | right.val = t; 28 | 29 | return head; 30 | 31 | } 32 | } -------------------------------------------------------------------------------- /1721-swapping-nodes-in-a-linked-list/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /173-binary-search-tree-iterator/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /174-dungeon-game/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /175-combine-two-tables/175-combine-two-tables.sql: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | SELECT FirstName, LastName, City, State FROM Person LEFT JOIN Address ON Person.PersonId = Address.PersonId; -------------------------------------------------------------------------------- /175-combine-two-tables/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1757-recyclable-and-low-fat-products/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1760-minimum-limit-of-balls-in-a-bag/1760-minimum-limit-of-balls-in-a-bag.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean isPossibleToDivide(int[] nums, int pen, int maxOpt) { 3 | int currOpt = 0; 4 | for (int i = 0; i < nums.length; i++) { 5 | int divide = (nums[i] - 1) / pen; 6 | currOpt += divide; 7 | } 8 | return currOpt <= maxOpt; 9 | } 10 | 11 | public int minimumSize(int[] nums, int maxOperations) { 12 | int lo = 1; 13 | int hi = 0; 14 | for (int i = 0; i < nums.length; i++) { 15 | hi = Math.max(hi, nums[i]); 16 | } 17 | 18 | int res = -1; 19 | while (lo <= hi) { 20 | int mid = (lo + hi) / 2; 21 | if (isPossibleToDivide(nums, mid, maxOperations) == true) { 22 | res = mid; 23 | hi = mid - 1; 24 | } else { 25 | lo = mid + 1; 26 | } 27 | } 28 | 29 | return res; 30 | } 31 | } -------------------------------------------------------------------------------- /1760-minimum-limit-of-balls-in-a-bag/1760-minimum-limit-of-balls-in-a-bag.ts: -------------------------------------------------------------------------------- 1 | function minimumSize(nums: number[], maxOperations: number): number { 2 | let lo: number = 0; 3 | let hi: number = Math.max(...nums); 4 | let pans: number = -1; 5 | 6 | let isPossible = (pen: number): boolean => { 7 | let currOpt: number = 0; 8 | nums.forEach((num) => { 9 | let divide: number = Math.floor((num - 1) / pen); 10 | currOpt += divide; 11 | }); 12 | 13 | return currOpt <= maxOperations; 14 | }; 15 | 16 | while (lo <= hi) { 17 | let mid: number = Math.floor((lo + hi) / 2); 18 | if (isPossible(mid) == true) { 19 | pans = mid; 20 | hi = mid - 1; 21 | } else { 22 | lo = mid + 1; 23 | } 24 | } 25 | 26 | return pans; 27 | }; -------------------------------------------------------------------------------- /1760-minimum-limit-of-balls-in-a-bag/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1795-rearrange-products-table/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1832-check-if-the-sentence-is-pangram/1832-check-if-the-sentence-is-pangram.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | 3 | public boolean checkIfPangram(String str) { 4 | str = str.toLowerCase(); 5 | for (int i = 0; i < 26; i++) { 6 | char ch = (char) ('a' + i); 7 | if (str.indexOf(ch) == -1) { 8 | return false; 9 | } 10 | } 11 | return true; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /1832-check-if-the-sentence-is-pangram/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1873-calculate-special-bonus/1873-calculate-special-bonus.sql: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | SELECT employee_id, if(employee_id % 2 = 1 AND name NOT LIKE "M%", salary, 0) AS bonus FROM Employees ORDER BY employee_id; -------------------------------------------------------------------------------- /1873-calculate-special-bonus/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /188-best-time-to-buy-and-sell-stock-iv/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1886-determine-whether-matrix-can-be-obtained-by-rotation/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1905-count-sub-islands/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1964-find-the-longest-valid-obstacle-course-at-each-position/1964-find-the-longest-valid-obstacle-course-at-each-position.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector longestObstacleCourseAtEachPosition(vector& obstacles) { 4 | int n = obstacles.size(); 5 | vector ans (n); 6 | vector lis; 7 | for (int i = 0; i < n; i++) { 8 | int idx = upper_bound(lis.begin(), lis.end(), obstacles[i]) - lis.begin(); 9 | if (idx == lis.size()) { 10 | lis.push_back(obstacles[i]); 11 | ans[i] = lis.size(); 12 | } else { 13 | lis[idx] = obstacles[i]; 14 | ans[i] = idx + 1; 15 | } 16 | } 17 | 18 | return ans; 19 | } 20 | }; -------------------------------------------------------------------------------- /1964-find-the-longest-valid-obstacle-course-at-each-position/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /198-house-robber/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /199-binary-tree-right-side-view/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /2-add-two-numbers/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /20-valid-parentheses/20-valid-parentheses.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isValid(string s) { 4 | stack st; 5 | st.push(s[0]); 6 | for(int i = 1; i < s.size(); i++) { 7 | char ch = s[i]; 8 | if(ch == '(' or ch == '[' or ch == '{') { 9 | st.push(ch); 10 | } else if(ch == ')') { 11 | if(st.size() and st.top() == '(') 12 | st.pop(); 13 | else 14 | return false; 15 | } else if(ch == '}') { 16 | if(st.size() and st.top() == '{') 17 | st.pop(); 18 | else 19 | return false; 20 | } else if(ch == ']') { 21 | if(st.size() and st.top() == '[') 22 | st.pop(); 23 | else 24 | return false; 25 | } 26 | } 27 | 28 | return st.size() == 0; 29 | } 30 | }; -------------------------------------------------------------------------------- /20-valid-parentheses/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /200-number-of-islands/200-number-of-islands.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int dir[4][2] = {{-1, 0}, {0, 1}, {0, -1}, {1, 0}}; 4 | int n, m; 5 | 6 | void dfs(vector>& arr, int r, int c) { 7 | arr[r][c] = '2'; 8 | for(int d = 0; d < 4; d++) { 9 | int nr = r + dir[d][0]; 10 | int nc = c + dir[d][1]; 11 | if(nr >= 0 && nr < n && nc >= 0 && nc < m && arr[nr][nc] == '1') { 12 | dfs(arr, nr, nc); 13 | } 14 | } 15 | } 16 | 17 | int numIslands(vector>& grid) { 18 | n = grid.size(); 19 | m = grid[0].size(); 20 | int ans = 0; 21 | for(int i = 0; i < n; i++) { 22 | for(int j = 0; j < m; j++) { 23 | if(grid[i][j] == '1') { 24 | dfs(grid, i, j); 25 | ans++; 26 | } 27 | } 28 | } 29 | return ans; 30 | } 31 | }; -------------------------------------------------------------------------------- /200-number-of-islands/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /2006-count-number-of-pairs-with-absolute-difference-k/2006-count-number-of-pairs-with-absolute-difference-k.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int countKDifference(int[] nums, int k) { 3 | HashMap map = new HashMap<>(); 4 | int count = 0; 5 | for (int num : nums) { 6 | int y1 = num + k; 7 | int y2 = num - k; 8 | 9 | if (map.containsKey(y1) == true) { 10 | count += map.get(y1); 11 | } 12 | if (map.containsKey(y2) == true) { 13 | count += map.get(y2); 14 | } 15 | 16 | map.put(num, map.getOrDefault(num, 0) + 1); 17 | } 18 | 19 | return count; 20 | } 21 | } -------------------------------------------------------------------------------- /2006-count-number-of-pairs-with-absolute-difference-k/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /207-course-schedule/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /208-implement-trie-prefix-tree/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /2096-step-by-step-directions-from-a-binary-tree-node-to-another/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /210-course-schedule-ii/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /2102-sequentially-ordinal-rank-tracker/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /212-word-search-ii/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /215-kth-largest-element-in-an-array/215-kth-largest-element-in-an-array.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int findKthLargest(int[] nums, int k) { 3 | Arrays.sort (nums); 4 | int n = nums.length; 5 | return nums[n - k]; 6 | } 7 | } -------------------------------------------------------------------------------- /215-kth-largest-element-in-an-array/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /221-maximal-square/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /2218-maximum-value-of-k-coins-from-piles/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /2265-count-nodes-equal-to-average-of-subtree/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /2279-maximum-bags-with-full-capacity-of-rocks/2279-maximum-bags-with-full-capacity-of-rocks.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int maximumBags(int[] capacity, int[] rocks, int additionalRocks) { 3 | int[] vacancy = new int[capacity.length]; 4 | for (int i = 0; i < capacity.length; i++) { 5 | vacancy[i] = capacity[i] - rocks[i]; 6 | } 7 | Arrays.sort (vacancy); 8 | int fullBagsCnt = 0; 9 | for (int i = 0; i < capacity.length; i++) { 10 | if (vacancy[i] == 0) { 11 | fullBagsCnt++; 12 | } else { 13 | if (vacancy[i] <= additionalRocks) { 14 | additionalRocks -= vacancy[i]; 15 | fullBagsCnt++; 16 | } else { 17 | break; 18 | } 19 | } 20 | } 21 | 22 | return fullBagsCnt; 23 | } 24 | } -------------------------------------------------------------------------------- /2279-maximum-bags-with-full-capacity-of-rocks/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /2296-design-a-text-editor/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /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 kthSmallest(TreeNode* root, int k) { 15 | int ans = 0; 16 | inrec(root, k, ans); 17 | return ans; 18 | } 19 | 20 | void inrec(TreeNode* root, int& k, int& ans) { 21 | if(!root) 22 | return; 23 | 24 | inrec(root->left, k, ans); 25 | k--; 26 | if(k == 0) { 27 | ans = root->val; 28 | return; 29 | } 30 | inrec(root->right, k, ans); 31 | return; 32 | } 33 | }; -------------------------------------------------------------------------------- /230-kth-smallest-element-in-a-bst/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /2325-decode-the-message/2325-decode-the-message.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string decodeMessage(string key, string message) { 4 | vector code(26, '$'); 5 | char ch = 'a'; 6 | for (int i = 0; i < key.size(); i++) { 7 | char tempChar = key[i]; 8 | 9 | if (tempChar == ' ') { 10 | continue; 11 | } 12 | 13 | if (code[tempChar - 'a'] == '$') { 14 | code[tempChar - 'a'] = ch; 15 | ch += 1; 16 | } else { 17 | continue; 18 | } 19 | } 20 | 21 | string ans = ""; 22 | for (int i = 0; i < message.size(); i++) { 23 | char tempChar = message[i]; 24 | if (tempChar == ' ') { 25 | ans += tempChar; 26 | } 27 | else { 28 | ans += code[tempChar - 'a']; 29 | } 30 | } 31 | 32 | return ans; 33 | } 34 | }; -------------------------------------------------------------------------------- /2325-decode-the-message/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /235-lowest-common-ancestor-of-a-binary-search-tree/235-lowest-common-ancestor-of-a-binary-search-tree.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * public class TreeNode { 4 | * int val; 5 | * TreeNode left; 6 | * TreeNode right; 7 | * TreeNode(int x) { val = x; } 8 | * } 9 | */ 10 | 11 | class Solution { 12 | public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { 13 | if(root == null) 14 | return null; 15 | 16 | if(root.val > p.val && root.val > q.val) { 17 | return lowestCommonAncestor(root.left, p, q); 18 | } else if(root.val < p.val && root.val < q.val) { 19 | return lowestCommonAncestor(root.right, p, q); 20 | } else { 21 | return root; 22 | } 23 | } 24 | } -------------------------------------------------------------------------------- /235-lowest-common-ancestor-of-a-binary-search-tree/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /236-lowest-common-ancestor-of-a-binary-tree/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /2360-longest-cycle-in-a-graph/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /2385-amount-of-time-for-binary-tree-to-be-infected/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /240-search-a-2d-matrix-ii/240-search-a-2d-matrix-ii.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool searchMatrix(vector>& matrix, int target) { 4 | int n = matrix.size(); 5 | int m = matrix[0].size(); 6 | int i = 0, j = m - 1; 7 | while(i < n && j >= 0) { 8 | int ele = matrix[i][j]; 9 | if(target > ele) { 10 | i++; 11 | } else if (ele == target) { 12 | return true; 13 | } else { 14 | j--; 15 | } 16 | } 17 | 18 | return false; 19 | } 20 | }; -------------------------------------------------------------------------------- /240-search-a-2d-matrix-ii/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /2415-reverse-odd-levels-of-binary-tree/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /242-valid-anagram/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /2469-convert-the-temperature/2469-convert-the-temperature.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public double[] convertTemperature(double celsius) { 3 | return new double[] {celsius + 273.15, celsius * 1.80 + 32.00}; 4 | } 5 | } -------------------------------------------------------------------------------- /2469-convert-the-temperature/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /253-meeting-rooms-ii/253-meeting-rooms-ii.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minMeetingRooms(vector>& intervals) { 4 | sort(intervals.begin(), intervals.end()); 5 | vector rooms(1, -1); 6 | for(int i = 0; i < intervals.size(); i++) { 7 | int idx = -1; 8 | for(int j = 0; j < rooms.size(); j++) { 9 | if(rooms[j] <= intervals[i][0]) { 10 | idx = j; 11 | } 12 | } 13 | if(idx == -1) 14 | rooms.push_back(intervals[i][1]); 15 | else 16 | rooms[idx] = intervals[i][1]; 17 | } 18 | 19 | return rooms.size(); 20 | } 21 | }; -------------------------------------------------------------------------------- /253-meeting-rooms-ii/253-meeting-rooms-ii.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int minMeetingRooms(int[][] intervals) { 3 | Arrays.sort(intervals, (a, b) -> a[0] - b[0]); 4 | PriorityQueue pq = new PriorityQueue(); 5 | int maxCnt = 0; 6 | for(int[] interval : intervals) { 7 | while (!pq.isEmpty() && interval[0] >= pq.peek()) { 8 | pq.remove(); 9 | } 10 | pq.add(interval[1]); 11 | maxCnt = Math.max(maxCnt, (int)pq.size()); 12 | } 13 | return maxCnt; 14 | } 15 | } -------------------------------------------------------------------------------- /253-meeting-rooms-ii/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /253-meeting-rooms-ii/README.md: -------------------------------------------------------------------------------- 1 |

253. Meeting Rooms II

Medium


Given an array of meeting time intervals intervals where intervals[i] = [starti, endi], return the minimum number of conference rooms required.

2 | 3 |

 

4 |

Example 1:

5 |
Input: intervals = [[0,30],[5,10],[15,20]]
 6 | Output: 2
 7 | 

Example 2:

8 |
Input: intervals = [[7,10],[2,4]]
 9 | Output: 1
10 | 
11 |

 

12 |

Constraints:

13 | 14 |
    15 |
  • 1 <= intervals.length <= 104
  • 16 |
  • 0 <= starti < endi <= 106
  • 17 |
18 |
-------------------------------------------------------------------------------- /264-ugly-number-ii/264-ugly-number-ii.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int nthUglyNumber(int n) { 4 | vector dp(n, 0); 5 | dp[0] = 1; 6 | int twoIdx = 0, threeIdx = 0, fiveIdx = 0; 7 | for(int i = 1; i < n; i++) { 8 | int next = min({2 * dp[twoIdx], 3 * dp[threeIdx], 5 * dp[fiveIdx]}); 9 | dp[i] = next; 10 | if(next == 2 * dp[twoIdx]) { 11 | twoIdx++; 12 | } 13 | if(next == 3 * dp[threeIdx]) { 14 | threeIdx++; 15 | } 16 | if(next == 5 * dp[fiveIdx]) { 17 | fiveIdx++; 18 | } 19 | } 20 | 21 | return dp[n - 1]; 22 | } 23 | }; -------------------------------------------------------------------------------- /264-ugly-number-ii/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /284-peeking-iterator/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /287-find-the-duplicate-number/287-find-the-duplicate-number.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findDuplicate(vector& nums) { 4 | int slow=nums[0]; 5 | int fast=nums[0]; 6 | while(true) 7 | { 8 | slow=nums[slow]; 9 | fast=nums[nums[fast]]; 10 | if(slow==fast) 11 | break; 12 | } 13 | 14 | slow=nums[0]; 15 | while(fast!=slow) 16 | { 17 | slow=nums[slow]; 18 | fast=nums[fast]; 19 | } 20 | return fast; 21 | } 22 | }; -------------------------------------------------------------------------------- /289-game-of-life/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /300-longest-increasing-subsequence/300-longest-increasing-subsequence.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int lengthOfLIS(vector& nums) { 4 | int n = nums.size(); 5 | vector lis(n + 1, 1); 6 | 7 | for(int i = 0; i < n; i++) { 8 | for(int j = 0; j < i; ++j) { 9 | if(nums[j] < nums[i]) 10 | lis[i] = max(lis[i], lis[j] + 1); 11 | } 12 | } 13 | 14 | int ans = 0; 15 | for(int e : lis) 16 | ans = max(ans, e); 17 | 18 | return ans; 19 | } 20 | }; -------------------------------------------------------------------------------- /300-longest-increasing-subsequence/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /301-remove-invalid-parentheses/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /31-next-permutation/31-next-permutation.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void nextPermutation(vector& nums) { 4 | int n = nums.size(), k, l; 5 | for (k = n - 2; k >= 0; k--) { 6 | if (nums[k] < nums[k + 1]) { 7 | break; 8 | } 9 | } 10 | if (k < 0) { 11 | reverse(nums.begin(), nums.end()); 12 | } else { 13 | for (l = n - 1; l > k; l--) { 14 | if (nums[l] > nums[k]) { 15 | break; 16 | } 17 | } 18 | swap(nums[k], nums[l]); 19 | reverse(nums.begin() + k + 1, nums.end()); 20 | } 21 | } 22 | }; -------------------------------------------------------------------------------- /31-next-permutation/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /316-remove-duplicate-letters/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /322-coin-change/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /326-power-of-three/326-power-of-three.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean isPowerOfThree(int n) { 3 | return (Math.log10(n) / Math.log10(3)) % 1 == 0; 4 | } 5 | } -------------------------------------------------------------------------------- /326-power-of-three/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /33-search-in-rotated-sorted-array/33-search-in-rotated-sorted-array.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int search(int[] nums, int target) { 3 | int n = nums.length; 4 | int si = 0; 5 | int ei = n - 1; 6 | while(si < ei) { 7 | int mid = si + (ei - si) / 2; 8 | if(nums[mid] == target) return mid; 9 | if(nums[si] <= nums[mid]) { 10 | if(target >= nums[si] && target < nums[mid]) { 11 | ei = mid - 1; 12 | } else { 13 | si = mid + 1; 14 | } 15 | } else { 16 | if(target <= nums[ei] && target > nums[mid]) { 17 | si = mid + 1; 18 | } else { 19 | ei = mid - 1; 20 | } 21 | } 22 | } 23 | 24 | return nums[si] == target ? si : -1; 25 | } 26 | } -------------------------------------------------------------------------------- /33-search-in-rotated-sorted-array/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /337-house-robber-iii/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /342-power-of-four/342-power-of-four.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isPowerOfFour(int n) { 4 | return n > 0 and ((n - 1) % 3) == 0 and (n & (n - 1)) == 0; 5 | } 6 | }; -------------------------------------------------------------------------------- /342-power-of-four/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /344-reverse-string/344-reverse-string.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void reverseString(vector& s) { 4 | int i = 0, j = s.size() - 1; 5 | while(i < j) { 6 | char temp = s[i]; 7 | s[i] = s[j]; 8 | s[j] = temp; 9 | i++; 10 | j--; 11 | } 12 | } 13 | }; -------------------------------------------------------------------------------- /344-reverse-string/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /347-top-k-frequent-elements/347-top-k-frequent-elements.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector topKFrequent(vector& nums, int k) { 4 | unordered_map umap; 5 | for(int e : nums) 6 | umap[e]++; 7 | 8 | priority_queue,vector>,greater>>pq; 9 | for(auto& a : umap) { 10 | pq.push({a.second,a.first}); 11 | if(pq.size()>k) 12 | pq.pop(); 13 | } 14 | 15 | vectorans; 16 | while(pq.size()!=0) { 17 | ans.push_back(pq.top().second); 18 | pq.pop(); 19 | } 20 | return ans; 21 | } 22 | }; -------------------------------------------------------------------------------- /347-top-k-frequent-elements/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /363-max-sum-of-rectangle-no-larger-than-k/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /377-combination-sum-iv/377-combination-sum-iv.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int combinationSum4(int[] nums, int target) { 3 | int[] dp = new int[target + 1]; 4 | dp[target] = 1; 5 | for(int tar = target - 1; tar >= 0; tar--) { 6 | for(int n : nums) { 7 | if(tar + n <= target) 8 | dp[tar] += dp[tar + n]; 9 | } 10 | } 11 | return dp[0]; 12 | } 13 | } -------------------------------------------------------------------------------- /377-combination-sum-iv/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /38-count-and-say/38-count-and-say.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | 3 | public String countAndSay(int n) { 4 | if (n == 1) { 5 | System.out.println("1"); 6 | } 7 | 8 | String ans = "1"; 9 | for (int i = 2; i <= n; i++) { 10 | String nextAns = ""; 11 | int j = 1; 12 | char ch = ans.charAt(0); 13 | int cnt = 1; 14 | while (j < ans.length()) { 15 | if (ch == ans.charAt(j)) { 16 | cnt++; 17 | } else { 18 | nextAns += cnt + "" + ch; 19 | ch = ans.charAt(j); 20 | cnt = 1; 21 | } 22 | j++; 23 | } 24 | nextAns += cnt + "" + ch; 25 | ans = nextAns; 26 | } 27 | return ans; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /38-count-and-say/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /383-ransom-note/383-ransom-note.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | 3 | public boolean canConstruct(String ransomNote, String magazine) { 4 | int n = magazine.length(); 5 | int[] freq = new int[26]; 6 | for (int i = 0; i < n; i++) freq[magazine.charAt(i) - 'a']++; 7 | 8 | int m = ransomNote.length(); 9 | for (int i = 0; i < m; i++) if (freq[ransomNote.charAt(i) - 'a'] > 0) freq[ransomNote.charAt(i) - 'a']--; else return false; 10 | 11 | return true; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /383-ransom-note/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /387-first-unique-character-in-a-string/387-first-unique-character-in-a-string.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int firstUniqChar(String s) { 3 | int n = s.length(); 4 | HashMap countOfChar = new HashMap<>(); 5 | 6 | for(int i = 0; i < n; i++) { 7 | char ch = s.charAt(i); 8 | if(countOfChar.containsKey(ch) == true) { 9 | int currCount = countOfChar.get(ch); 10 | countOfChar.put(ch, currCount + 1); 11 | } else { 12 | countOfChar.put(ch, 1); 13 | } 14 | } 15 | 16 | for(int i = 0; i < n; i++) { 17 | char ch = s.charAt(i); 18 | if(countOfChar.get(ch) == 1) { 19 | return i; 20 | } 21 | } 22 | 23 | return -1; 24 | } 25 | } -------------------------------------------------------------------------------- /387-first-unique-character-in-a-string/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /387-first-unique-character-in-a-string/README.md: -------------------------------------------------------------------------------- 1 |

387. First Unique Character in a String

Easy


Given a string s, find the first non-repeating character in it and return its index. If it does not exist, return -1.

2 | 3 |

 

4 |

Example 1:

5 |
Input: s = "leetcode"
 6 | Output: 0
 7 | 

Example 2:

8 |
Input: s = "loveleetcode"
 9 | Output: 2
10 | 

Example 3:

11 |
Input: s = "aabb"
12 | Output: -1
13 | 
14 |

 

15 |

Constraints:

16 | 17 |
    18 |
  • 1 <= s.length <= 105
  • 19 |
  • s consists of only lowercase English letters.
  • 20 |
21 |
-------------------------------------------------------------------------------- /399-evaluate-division/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /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 | public: 14 | int sum = 0; 15 | int sumOfLeftLeaves(TreeNode* root) { 16 | if(!root) 17 | return 0; 18 | if(root->left && !root->left->left && !root->left->right) 19 | sum += root->left->val; 20 | sumOfLeftLeaves(root->left); 21 | sumOfLeftLeaves(root->right); 22 | return sum; 23 | } 24 | }; -------------------------------------------------------------------------------- /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 |

 

4 |

Example 1:

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

Example 2:

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

 

18 |

Constraints:

19 | 20 |
    21 |
  • The number of nodes in the tree is in the range [1, 1000].
  • 22 |
  • -1000 <= Node.val <= 1000
  • 23 |
24 |
-------------------------------------------------------------------------------- /41-first-missing-positive/41-first-missing-positive.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int firstMissingPositive(vector& nums) { 4 | int n = nums.size(); 5 | for(int i = 0; i < n; ++i) { 6 | while(nums[i] > 0 && nums[i] <= n && nums[i] != nums[nums[i] - 1]) { 7 | swap(nums[i], nums[nums[i] - 1]); 8 | } 9 | } 10 | for(int i = 0; i < n; ++i) { 11 | if(nums[i] != i + 1) 12 | return i + 1; 13 | } 14 | return n + 1; 15 | } 16 | }; -------------------------------------------------------------------------------- /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 |
-------------------------------------------------------------------------------- /417-pacific-atlantic-water-flow/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /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 | 6 | int left = 0; 7 | int right = n - 1; 8 | int leftMax = 0; 9 | int rightMax = 0; 10 | 11 | int ans = 0; 12 | 13 | while(left <= right) { 14 | if(height[left] <= height[right]) { 15 | if(height[left] >= leftMax) { 16 | leftMax = height[left]; 17 | } else { 18 | ans += leftMax - height[left]; 19 | } 20 | left++; 21 | } else { 22 | if(height[right] >= rightMax) { 23 | rightMax = height[right]; 24 | } else { 25 | ans += rightMax - height[right]; 26 | } 27 | right--; 28 | } 29 | } 30 | 31 | return ans; 32 | } 33 | }; -------------------------------------------------------------------------------- /42-trapping-rain-water/42-trapping-rain-water.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int trap(int[] height) { 3 | int n = height.length; 4 | 5 | int left = 0; 6 | int right = n - 1; 7 | int leftMax = 0; 8 | int rightMax = 0; 9 | 10 | int ans = 0; 11 | 12 | while(left <= right) { 13 | if(height[left] <= height[right]) { 14 | if(height[left] >= leftMax) { 15 | leftMax = height[left]; 16 | } else { 17 | ans += leftMax - height[left]; 18 | } 19 | left++; 20 | } else { 21 | if(height[right] >= rightMax) { 22 | rightMax = height[right]; 23 | } else { 24 | ans += rightMax - height[right]; 25 | } 26 | right--; 27 | } 28 | } 29 | 30 | return ans; 31 | } 32 | } -------------------------------------------------------------------------------- /42-trapping-rain-water/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /429-n-ary-tree-level-order-traversal/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /438-find-all-anagrams-in-a-string/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /45-jump-game-ii/45-jump-game-ii.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int jump(int[] nums) { 3 | int farthest = 0, end = 0, jumps = 0; 4 | for(int i = 0; i < nums.length - 1; i++) { 5 | farthest = Math.max(farthest, nums[i] + i); 6 | if(i == end) { 7 | jumps++; 8 | end = farthest; 9 | } 10 | } 11 | return jumps; 12 | } 13 | } -------------------------------------------------------------------------------- /45-jump-game-ii/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /450-delete-node-in-a-bst/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /456-132-pattern/456-132-pattern.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | /* 4 | n1 n3 n2 -> 1 3 2 5 | */ 6 | bool find132pattern(vector& nums) { 7 | stack st; 8 | int n3 = INT_MIN; 9 | for(int i = nums.size() - 1; i >= 0; i--) { 10 | if(n3 > nums[i]) { 11 | return true; 12 | }else { 13 | while(st.size() != 0 && st.top() < nums[i]) { 14 | n3 = max(n3, st.top()); 15 | st.pop(); 16 | } 17 | } 18 | st.push(nums[i]); 19 | } 20 | return false; 21 | } 22 | }; -------------------------------------------------------------------------------- /456-132-pattern/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /462-minimum-moves-to-equal-array-elements-ii/462-minimum-moves-to-equal-array-elements-ii.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minMoves2(vector& nums) { 4 | sort(nums.begin(), nums.end()); 5 | int mid = nums[nums.size() / 2]; 6 | int ans = 0; 7 | for(int i = 0; i < nums.size(); ++i) { 8 | if(nums.size() / 2 == i) continue; 9 | else ans += abs(nums[i] - mid); 10 | } 11 | return ans; 12 | } 13 | }; -------------------------------------------------------------------------------- /462-minimum-moves-to-equal-array-elements-ii/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /472-concatenated-words/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /473-matchsticks-to-square/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /48-rotate-image/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /494-target-sum/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /509-fibonacci-number/509-fibonacci-number.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int fib(int n) { 4 | if(n < 2) 5 | return n; 6 | 7 | int a = 0, b = 1, c; 8 | while(n-- > 1) { 9 | c = a + b; 10 | a = b; 11 | b = c; 12 | } 13 | 14 | return c; 15 | } 16 | }; -------------------------------------------------------------------------------- /509-fibonacci-number/509-fibonacci-number.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int fib(int n) { 3 | if(n == 0 || n == 1) { 4 | return n; 5 | } 6 | 7 | int prev1 = 1, prev2 = 0; 8 | for(int i = 2; i <= n; i++) { 9 | int curr = prev1 + prev2; 10 | prev2 = prev1; 11 | prev1 = curr; 12 | } 13 | 14 | return prev1; 15 | } 16 | } -------------------------------------------------------------------------------- /509-fibonacci-number/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /529-minesweeper/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /535-encode-and-decode-tinyurl/535-encode-and-decode-tinyurl.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | 4 | // Encodes a URL to a shortened URL. 5 | string encode(string longUrl) { 6 | return longUrl; 7 | } 8 | 9 | // Decodes a shortened URL to its original URL. 10 | string decode(string shortUrl) { 11 | return shortUrl; 12 | } 13 | }; 14 | 15 | // Your Solution object will be instantiated and called as such: 16 | // Solution solution; 17 | // solution.decode(solution.encode(url)); -------------------------------------------------------------------------------- /535-encode-and-decode-tinyurl/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /538-convert-bst-to-greater-tree/538-convert-bst-to-greater-tree.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * public class TreeNode { 4 | * int val; 5 | * TreeNode left; 6 | * TreeNode right; 7 | * TreeNode() {} 8 | * TreeNode(int val) { this.val = val; } 9 | * TreeNode(int val, TreeNode left, TreeNode right) { 10 | * this.val = val; 11 | * this.left = left; 12 | * this.right = right; 13 | * } 14 | * } 15 | */ 16 | class Solution { 17 | int sum = 0; 18 | public void reverseIN(TreeNode root) { 19 | if(root == null) return; 20 | 21 | reverseIN(root.right); 22 | sum += root.val; 23 | root.val = sum; 24 | reverseIN(root.left); 25 | } 26 | 27 | public TreeNode convertBST(TreeNode root) { 28 | reverseIN(root); 29 | return root; 30 | } 31 | } -------------------------------------------------------------------------------- /538-convert-bst-to-greater-tree/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /542-01-matrix/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /543-diameter-of-binary-tree/543-diameter-of-binary-tree.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * public class TreeNode { 4 | * int val; 5 | * TreeNode left; 6 | * TreeNode right; 7 | * TreeNode() {} 8 | * TreeNode(int val) { this.val = val; } 9 | * TreeNode(int val, TreeNode left, TreeNode right) { 10 | * this.val = val; 11 | * this.left = left; 12 | * this.right = right; 13 | * } 14 | * } 15 | */ 16 | class Solution { 17 | int dia = 0; 18 | public int height(TreeNode root) { 19 | if(root == null) { 20 | return -1; 21 | } 22 | 23 | int lh = height(root.left); 24 | int rh = height(root.right); 25 | 26 | int myDia = lh + rh + 2; 27 | dia = Math.max(dia, myDia); 28 | 29 | return Math.max(lh, rh) + 1; 30 | } 31 | public int diameterOfBinaryTree(TreeNode root) { 32 | height(root); 33 | return dia; 34 | } 35 | } -------------------------------------------------------------------------------- /543-diameter-of-binary-tree/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /547-number-of-provinces/547-number-of-provinces.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | private void dfs(int x, int[][] isConnected, boolean[] vis) { 3 | vis[x] = true; 4 | for(int col = 0; col < isConnected[x].length; col++) { 5 | if(isConnected[x][col] == 1 && vis[col] == false) { 6 | dfs(col, isConnected, vis); 7 | } 8 | } 9 | } 10 | 11 | public int findCircleNum(int[][] isConnected) { 12 | int n = isConnected.length; 13 | 14 | int numOfProvinces = 0; 15 | 16 | boolean[] vis = new boolean[n]; 17 | for(int i = 0; i < n; i++) { 18 | if(vis[i] == false) { 19 | numOfProvinces += 1; 20 | dfs(i, isConnected, vis); 21 | } 22 | } 23 | 24 | return numOfProvinces; 25 | } 26 | } -------------------------------------------------------------------------------- /547-number-of-provinces/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /55-jump-game/55-jump-game.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean canJump(int[] nums) { 3 | int n = nums.length; 4 | 5 | int desti = n - 1; 6 | for(int i = n - 1; i >= 0; i--) { 7 | if(i + nums[i] >= desti) { 8 | desti = i; 9 | } 10 | } 11 | 12 | return desti == 0; 13 | } 14 | } -------------------------------------------------------------------------------- /55-jump-game/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /559-maximum-depth-of-n-ary-tree/559-maximum-depth-of-n-ary-tree.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | // Definition for a Node. 3 | class Node { 4 | public: 5 | int val; 6 | vector children; 7 | 8 | Node() {} 9 | 10 | Node(int _val) { 11 | val = _val; 12 | } 13 | 14 | Node(int _val, vector _children) { 15 | val = _val; 16 | children = _children; 17 | } 18 | }; 19 | */ 20 | 21 | class Solution { 22 | public: 23 | int maxDepth(Node* root) { 24 | if(root == nullptr) 25 | return 0; 26 | 27 | int depth = 0; 28 | for(Node* child : root->children) { 29 | depth = max(depth, maxDepth(child)); 30 | } 31 | 32 | return depth + 1; 33 | } 34 | }; -------------------------------------------------------------------------------- /563-binary-tree-tilt/563-binary-tree-tilt.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * public class TreeNode { 4 | * int val; 5 | * TreeNode left; 6 | * TreeNode right; 7 | * TreeNode() {} 8 | * TreeNode(int val) { this.val = val; } 9 | * TreeNode(int val, TreeNode left, TreeNode right) { 10 | * this.val = val; 11 | * this.left = left; 12 | * this.right = right; 13 | * } 14 | * } 15 | */ 16 | class Solution { 17 | public int sum(TreeNode root, int[] tilt) { 18 | if(root == null) return 0; 19 | int leftKaSum = sum(root.left, tilt); 20 | int rightKaSum = sum(root.right, tilt); 21 | int tiltOfNode = Math.abs(leftKaSum - rightKaSum); 22 | tilt[0] += tiltOfNode; 23 | return leftKaSum + root.val + rightKaSum; 24 | } 25 | 26 | public int findTilt(TreeNode root) { 27 | int[] tilt = new int[1]; 28 | sum(root, tilt); 29 | return tilt[0]; 30 | } 31 | } -------------------------------------------------------------------------------- /563-binary-tree-tilt/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /576-out-of-boundary-paths/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /58-length-of-last-word/58-length-of-last-word.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int lengthOfLastWord(String s) { 3 | int len = 0; 4 | for(int i = s.length() - 1; i >= 0; i--) { 5 | if(s.charAt(i) != ' ') len++; 6 | else if(len > 0) return len; 7 | } 8 | return len; 9 | } 10 | } -------------------------------------------------------------------------------- /58-length-of-last-word/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /583-delete-operation-for-two-strings/583-delete-operation-for-two-strings.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int LIS(string word1, string word2, int i, int j) { 4 | if(i == word1.size() || j == word2.size()) { 5 | return dp[i][j] = 0; 6 | } 7 | 8 | if(dp[i][j] != -1) { 9 | return dp[i][j]; 10 | } 11 | 12 | if(word1[i] == word2[j]) { 13 | return dp[i][j] = LIS(word1, word2, i + 1, j + 1) + 1; 14 | } else { 15 | return dp[i][j] = max({LIS(word1, word2, i + 1, j), LIS(word1, word2, i, j + 1), LIS(word1, word2, i + 1, j + 1)}); 16 | } 17 | } 18 | 19 | int dp[501][501]; 20 | int minDistance(string word1, string word2) { 21 | memset(dp, -1, sizeof(dp)); 22 | int lis = LIS(word1, word2, 0, 0); 23 | return word1.size() + word2.size() - 2*lis; 24 | } 25 | }; -------------------------------------------------------------------------------- /583-delete-operation-for-two-strings/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /59-spiral-matrix-ii/59-spiral-matrix-ii.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> generateMatrix(int n) { 4 | vector> ans (n, vector (n)); 5 | int sr = 0, er = n - 1, sc = 0, ec = n - 1; 6 | int num = 1; 7 | while(sr <= er and ec <= ec) { 8 | for(int i = sc; i <= ec; i++) { 9 | ans[sr][i] = num; 10 | num++; 11 | } 12 | sr++; 13 | 14 | for(int i = sr; i <= er; i++) { 15 | ans[i][ec] = num; 16 | num++; 17 | } 18 | ec--; 19 | 20 | for(int i = ec; i >= sc; i--) { 21 | ans[er][i] = num; 22 | num++; 23 | } 24 | er--; 25 | 26 | for(int i = er; i >= sr; i--) { 27 | ans[i][sc] = num; 28 | num++; 29 | } 30 | sc++; 31 | } 32 | 33 | return ans; 34 | } 35 | }; -------------------------------------------------------------------------------- /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 |
-------------------------------------------------------------------------------- /606-construct-string-from-binary-tree/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /61-rotate-list/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /62-unique-paths/62-unique-paths.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int dp[101][101]; 4 | 5 | int memo(int sr, int sc, int er, int ec) { 6 | if(sr >= er or sc >= ec) return dp[sr][sc] = 0; 7 | if(sr == er - 1 and sc == ec - 1) return dp[sr][sc] = 1; 8 | if(dp[sr][sc] != -1) return dp[sr][sc]; 9 | return dp[sr][sc] = memo(sr + 1, sc, er, ec) + memo(sr, sc + 1, er, ec); 10 | } 11 | 12 | int uniquePaths(int m, int n) { 13 | memset(dp, -1, sizeof(dp)); 14 | int ans = memo(0, 0, m, n); 15 | return ans; 16 | } 17 | }; -------------------------------------------------------------------------------- /62-unique-paths/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /623-add-one-row-to-tree/623-add-one-row-to-tree.go: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * type TreeNode struct { 4 | * Val int 5 | * Left *TreeNode 6 | * Right *TreeNode 7 | * } 8 | */ 9 | 10 | func addOneRow(root *TreeNode, val int, depth int) *TreeNode { 11 | if depth == 1 { 12 | return &TreeNode{val, root, nil} 13 | } 14 | 15 | if depth == 2 { 16 | root.Left = &TreeNode{val, root.Left, nil} 17 | root.Right = &TreeNode{val, nil, root.Right} 18 | } 19 | 20 | if root.Left != nil { 21 | addOneRow(root.Left, val, depth - 1) 22 | } 23 | 24 | if root.Right != nil { 25 | addOneRow(root.Right, val, depth - 1) 26 | } 27 | 28 | return root 29 | } -------------------------------------------------------------------------------- /623-add-one-row-to-tree/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /637-average-of-levels-in-binary-tree/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /64-minimum-path-sum/64-minimum-path-sum.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minPathSum(vector>& grid) { 4 | int n = grid.size(), m = grid[0].size(); 5 | int dp[n][m]; 6 | for(int i = n - 1; i >= 0; i--) { 7 | for(int j = m - 1; j >= 0; j--) { 8 | if(i == n - 1 && j == m - 1) { 9 | dp[i][j] = grid[i][j]; 10 | continue; 11 | } 12 | 13 | int right = INT_MAX, down = INT_MAX; 14 | if(i + 1 < n) { 15 | right = dp[i + 1][j]; 16 | } 17 | if(j + 1 < m) { 18 | down = dp[i][j + 1]; 19 | } 20 | 21 | dp[i][j] = min(right, down) + grid[i][j]; 22 | } 23 | } 24 | 25 | return dp[0][0]; 26 | } 27 | }; -------------------------------------------------------------------------------- /64-minimum-path-sum/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /647-palindromic-substrings/647-palindromic-substrings.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int dp[1001][1001]; 4 | int countSubstrings(string s) { 5 | memset(dp, -1, sizeof(dp)); 6 | int n = s.size(); 7 | int ans = 0; 8 | for(int i = 0; i < n; i++) { 9 | for(int j = n - 1; j >= i; j--) { 10 | ans += memo(s, i, j); 11 | } 12 | } 13 | return ans; 14 | } 15 | 16 | int memo(string& s, int si, int ei) { 17 | if(si > ei) { 18 | return 1; 19 | } 20 | 21 | if(s[si] != s[ei]) { 22 | return dp[si][ei] = 0; 23 | } 24 | 25 | if(dp[si][ei] != -1) { 26 | return dp[si][ei]; 27 | } 28 | 29 | if(memo(s, si + 1, ei - 1) == 1) { 30 | return dp[si][ei] = 1; 31 | } 32 | 33 | return dp[si][ei] = 0; 34 | } 35 | }; -------------------------------------------------------------------------------- /647-palindromic-substrings/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /648-replace-words/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /65-valid-number/65-valid-number.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isNumber(string s) { 4 | bool num = false, exp = false, sign = false, dec = false; 5 | for (int i = 0; i < s.size(); i++) { 6 | char c = s[i]; 7 | if (c >= '0' && c <= '9') num = true ; 8 | else if (c == 'e' || c == 'E') 9 | if (exp || !num) return false; 10 | else { 11 | exp = true; 12 | sign = false; 13 | num = false; 14 | dec = false; 15 | } 16 | else if (c == '+' || c == '-') 17 | if (sign || num || dec) return false; 18 | else sign = true; 19 | else if (c == '.') 20 | if (dec || exp) return false; 21 | else dec = true; 22 | else return false; 23 | } 24 | return num; 25 | } 26 | }; -------------------------------------------------------------------------------- /65-valid-number/65-valid-number.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean isNumber(String s) { 3 | try { 4 | s = s.trim(); 5 | int n = s.length(); 6 | if ( n == 0 || (s.charAt(n-1) != '.' && (s.charAt(n-1) - '0' < 0 || s.charAt(n-1) - '0' > 9 )) ) { 7 | return false; 8 | } 9 | double i = Double.parseDouble(s); 10 | return true; 11 | } 12 | catch (NumberFormatException e) { 13 | return false; 14 | } 15 | } 16 | } -------------------------------------------------------------------------------- /65-valid-number/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /653-two-sum-iv-input-is-a-bst/653-two-sum-iv-input-is-a-bst.go: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * type TreeNode struct { 4 | * Val int 5 | * Left *TreeNode 6 | * Right *TreeNode 7 | * } 8 | */ 9 | 10 | func dfs(root *TreeNode, arr *[]int) { 11 | if root == nil { 12 | return 13 | } 14 | 15 | dfs(root.Left, arr) 16 | *arr = append(*arr, root.Val) 17 | dfs(root.Right, arr) 18 | } 19 | 20 | func findTarget(root *TreeNode, k int) bool { 21 | arr := []int{} 22 | dfs(root, &arr) 23 | 24 | si, ei := 0, len(arr) - 1 25 | for si < ei { 26 | sum := arr[si] + arr[ei] 27 | switch { 28 | case sum == k: return true 29 | case sum < k: si++ 30 | case sum > k: ei-- 31 | } 32 | } 33 | 34 | return false 35 | } -------------------------------------------------------------------------------- /653-two-sum-iv-input-is-a-bst/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 L, int R) { 15 | if(!root) 16 | return nullptr; 17 | if(root->val< L) 18 | return trimBST(root->right, L, R); 19 | if(root->val > R) 20 | return trimBST(root->left, L, R); 21 | root->left = trimBST(root->left, L, R); 22 | root->right = trimBST(root->right, L, R); 23 | return root; 24 | } 25 | }; -------------------------------------------------------------------------------- /669-trim-a-binary-search-tree/669-trim-a-binary-search-tree.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * public class TreeNode { 4 | * int val; 5 | * TreeNode left; 6 | * TreeNode right; 7 | * TreeNode() {} 8 | * TreeNode(int val) { this.val = val; } 9 | * TreeNode(int val, TreeNode left, TreeNode right) { 10 | * this.val = val; 11 | * this.left = left; 12 | * this.right = right; 13 | * } 14 | * } 15 | */ 16 | class Solution { 17 | public TreeNode trimBST(TreeNode root, int low, int high) { 18 | if(root == null) return null; 19 | 20 | if(root.val < low) { 21 | return trimBST(root.right, low, high); 22 | } 23 | 24 | if(root.val > high) { 25 | return trimBST(root.left, low, high); 26 | } 27 | 28 | root.left = trimBST(root.left, low, high); 29 | root.right = trimBST(root.right, low, high); 30 | 31 | return root; 32 | } 33 | } -------------------------------------------------------------------------------- /669-trim-a-binary-search-tree/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /677-map-sum-pairs/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /680-valid-palindrome-ii/680-valid-palindrome-ii.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean validPalindrome(String s) { 3 | int i = 0, j = s.length() - 1; 4 | while(i < j && s.charAt(i) == s.charAt(j)) { 5 | i++; 6 | j--; 7 | } 8 | 9 | if(i >= j) return true; 10 | 11 | if(isPalindrome(s, i + 1, j) || isPalindrome(s, i, j - 1)) { 12 | return true; 13 | } 14 | 15 | return false; 16 | } 17 | 18 | public boolean isPalindrome(String s, int i, int j) { 19 | while(i < j && s.charAt(i) == s.charAt(j)) { 20 | i++; 21 | j--; 22 | } 23 | 24 | return i >= j; 25 | } 26 | } -------------------------------------------------------------------------------- /682-baseball-game/682-baseball-game.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int calPoints(vector& ops) { 4 | stack st; 5 | int sum = 0; 6 | for(int i = 0; i < ops.size(); i++) { 7 | string str = ops[i]; 8 | if(str == "+") { 9 | int p1 = st.top(); st.pop(); 10 | int p2 = st.top(); st.pop(); 11 | st.push(p2); 12 | st.push(p1); 13 | st.push(p1 + p2); 14 | } else if(str == "D") { 15 | int p = st.top(); 16 | st.push(p * 2); 17 | } else if(str == "C") { 18 | sum -= st.top(); 19 | st.pop(); 20 | continue; 21 | } else { 22 | st.push(stoi(str)); 23 | } 24 | sum += st.top(); 25 | } 26 | return sum; 27 | } 28 | }; -------------------------------------------------------------------------------- /682-baseball-game/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /684-redundant-connection/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /695-max-area-of-island/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /70-climbing-stairs/70-climbing-stairs.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int climbStairs(int n) { 3 | if(n == 1 || n == 2) { 4 | return n; 5 | } 6 | 7 | int prev1 = 2, prev2 = 1; 8 | for(int i = 3; i <= n; i++) { 9 | int curr = prev1 + prev2; 10 | prev2 = prev1; 11 | prev1 = curr; 12 | } 13 | 14 | return prev1; 15 | } 16 | } -------------------------------------------------------------------------------- /70-climbing-stairs/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /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* curr, int val) { 15 | while(curr != nullptr) { 16 | if(curr->val > val) 17 | curr = curr->left; 18 | else if(curr->val < val) 19 | curr = curr->right; 20 | else 21 | return curr; 22 | } 23 | return nullptr; 24 | } 25 | }; 26 | auto SpeedUp = []() { 27 | std::ios::sync_with_stdio(false); 28 | std::cin.tie(nullptr); 29 | std::cout.tie(nullptr); 30 | return nullptr; 31 | }(); -------------------------------------------------------------------------------- /700-search-in-a-binary-search-tree/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /701-insert-into-a-binary-search-tree/701-insert-into-a-binary-search-tree.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * public class TreeNode { 4 | * int val; 5 | * TreeNode left; 6 | * TreeNode right; 7 | * TreeNode() {} 8 | * TreeNode(int val) { this.val = val; } 9 | * TreeNode(int val, TreeNode left, TreeNode right) { 10 | * this.val = val; 11 | * this.left = left; 12 | * this.right = right; 13 | * } 14 | * } 15 | */ 16 | class Solution { 17 | public TreeNode insertIntoBST(TreeNode root, int val) { 18 | if(root == null) return new TreeNode(val); 19 | 20 | if(root.val > val) { 21 | root.left = insertIntoBST(root.left, val); 22 | return root; 23 | } else { 24 | root.right = insertIntoBST(root.right, val); 25 | return root; 26 | } 27 | } 28 | } -------------------------------------------------------------------------------- /701-insert-into-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 | private: 3 | int m; 4 | priority_queue,greater>pq; 5 | 6 | public: 7 | KthLargest(int k, vector& nums) { 8 | for(int e : nums) { 9 | pq.push(e); 10 | if(pq.size()>k) 11 | pq.pop(); 12 | } 13 | m=k; 14 | } 15 | 16 | int add(int val) { 17 | pq.push(val); 18 | while(madd(val); 28 | */ -------------------------------------------------------------------------------- /703-kth-largest-element-in-a-stream/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /704-binary-search/704-binary-search.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int search(vector& nums, int target) { 4 | int n = nums.size(); 5 | int l = 0, r = n - 1; 6 | while(l <= r) { 7 | int mid = l + (r - l) / 2; 8 | if(nums[mid] == target) return mid; 9 | else if(nums[mid] > target) r = mid - 1; 10 | else l = mid + 1; 11 | } 12 | return -1; 13 | } 14 | }; -------------------------------------------------------------------------------- /704-binary-search/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /705-design-hashset/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /706-design-hashmap/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /71-simplify-path/71-simplify-path.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public String simplifyPath(String path) { 3 | Stack st = new Stack(); 4 | for(String str : path.split("/")) { 5 | if(str.equals(".") || str.equals("")) 6 | continue; 7 | else if(str.equals("..")) { 8 | if(st.size() > 0) 9 | st.pop(); 10 | } else 11 | st.add(str); 12 | } 13 | 14 | String ans = ""; 15 | for(String str : st) { 16 | ans += "/" + str; 17 | } 18 | return ans == "" ? "/" : ans; 19 | } 20 | } -------------------------------------------------------------------------------- /71-simplify-path/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /732-my-calendar-iii/732-my-calendar-iii.cpp: -------------------------------------------------------------------------------- 1 | class MyCalendarThree { 2 | map schedule; 3 | 4 | public: 5 | MyCalendarThree() { 6 | 7 | } 8 | 9 | int book(int start, int end) { 10 | schedule[start]++; 11 | schedule[end]--; 12 | int maxBookings = 0; 13 | int booking = 0; 14 | for(auto& s : schedule) { 15 | booking += s.second; 16 | maxBookings = max(maxBookings, booking); 17 | } 18 | return maxBookings; 19 | } 20 | }; 21 | 22 | /** 23 | * Your MyCalendarThree object will be instantiated and called as such: 24 | * MyCalendarThree* obj = new MyCalendarThree(); 25 | * int param_1 = obj->book(start,end); 26 | */ -------------------------------------------------------------------------------- /732-my-calendar-iii/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /733-flood-fill/733-flood-fill.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | private: 3 | int n; 4 | int m; 5 | 6 | int dir[4][2] = {{-1, 0}, {0, 1}, {0, -1}, {1, 0}}; 7 | 8 | void dfs(vector>& image, int sr, int sc, int prevColor, int newColor) { 9 | image[sr][sc] = newColor; 10 | 11 | for(int d = 0; d < 4; d++) { 12 | int nr = sr + dir[d][0]; 13 | int nc = sc + dir[d][1]; 14 | 15 | if(nr >= 0 and nc >= 0 and nr < n and nc < m and image[nr][nc] == prevColor) { 16 | dfs(image, nr, nc, prevColor, newColor); 17 | } 18 | } 19 | } 20 | 21 | public: 22 | vector> floodFill(vector>& image, int sr, int sc, int color) { 23 | if(image[sr][sc] == color) return image; 24 | 25 | n = image.size(); 26 | m = image[0].size(); 27 | 28 | dfs(image, sr, sc, image[sr][sc], color); 29 | 30 | return image; 31 | } 32 | }; -------------------------------------------------------------------------------- /733-flood-fill/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /74-search-a-2d-matrix/74-search-a-2d-matrix.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool searchMatrix(vector>& matrix, int target) { 4 | int r = matrix.size(), c = matrix[0].size(); 5 | int low = 0, high = r * c - 1; 6 | 7 | while(low <= high) { 8 | int mid = (low+high)/2; 9 | 10 | if(matrix[mid / c][mid % c] == target) 11 | return true; 12 | else if(matrix[mid / c][mid % c] < target) 13 | low = mid + 1; 14 | else 15 | high = mid - 1; 16 | } 17 | 18 | return false; 19 | } 20 | }; -------------------------------------------------------------------------------- /74-search-a-2d-matrix/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /743-network-delay-time/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /745-prefix-and-suffix-search/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /746-min-cost-climbing-stairs/746-min-cost-climbing-stairs.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int minCostClimbingStairs(int[] cost) { 3 | int n = cost.length; 4 | int prev2 = cost[0], prev1 = cost[1]; 5 | for(int i = 2; i < n; i++) { 6 | int curr = Math.min(prev1, prev2) + cost[i]; 7 | prev2 = prev1; 8 | prev1 = curr; 9 | } 10 | 11 | return Math.min(prev1, prev2); 12 | } 13 | } -------------------------------------------------------------------------------- /746-min-cost-climbing-stairs/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /763-partition-labels/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /771-jewels-and-stones/771-jewels-and-stones.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numJewelsInStones(string jewels, string stones) { 4 | unordered_set set; 5 | for(char ch : jewels) 6 | set.insert(ch); 7 | 8 | int ans = 0; 9 | for(char ch : stones) { 10 | if(set.find(ch) != set.end()) 11 | ans++; 12 | } 13 | 14 | return ans; 15 | } 16 | }; -------------------------------------------------------------------------------- /771-jewels-and-stones/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /773-sliding-puzzle/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /775-global-and-local-inversions/775-global-and-local-inversions.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean isIdealPermutation(int[] nums) { 3 | int cmax = 0; 4 | 5 | for (int i = 0; i < nums.length - 2; ++i) { 6 | cmax = Math.max(cmax, nums[i]); 7 | if (cmax > nums[i + 2]) 8 | return false; 9 | } 10 | 11 | return true; 12 | } 13 | } -------------------------------------------------------------------------------- /775-global-and-local-inversions/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /785-is-graph-bipartite/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /790-domino-and-tromino-tiling/790-domino-and-tromino-tiling.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | #define MOD 1000000007 4 | int numTilings(int n) { 5 | long dp[n + 3]; 6 | dp[0] = 1; 7 | dp[1] = 2; 8 | dp[2] = 5; 9 | for (int i = 3; i < n; i ++) { 10 | dp[i] = (2 * dp[i - 1] + dp[i - 3]) % MOD; 11 | } 12 | return dp[n - 1]; 13 | } 14 | }; -------------------------------------------------------------------------------- /790-domino-and-tromino-tiling/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /796-rotate-string/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /801-minimum-swaps-to-make-sequences-increasing/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /804-unique-morse-code-words/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /81-search-in-rotated-sorted-array-ii/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /814-binary-tree-pruning/814-binary-tree-pruning.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 | private: 14 | bool HasOne(TreeNode* root) { 15 | if(!root) 16 | return false; 17 | bool lst = HasOne(root->left); //leftSubTree 18 | bool rst = HasOne(root->right); //rightSubTree 19 | if(!lst) root->left = nullptr; 20 | if(!rst) root->right = nullptr; 21 | return root->val == 1 || lst || rst; 22 | } 23 | public: 24 | TreeNode* pruneTree(TreeNode* root) { 25 | return HasOne(root) ? root : nullptr; 26 | } 27 | }; -------------------------------------------------------------------------------- /814-binary-tree-pruning/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /82-remove-duplicates-from-sorted-list-ii/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /844-backspace-string-compare/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /856-score-of-parentheses/856-score-of-parentheses.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int scoreOfParentheses(string S) { 4 | stack st; 5 | st.push(0); 6 | for(char ch : S) { 7 | if(ch == '(') 8 | st.push(0); 9 | else { 10 | int a = st.top(); st.pop(); 11 | int b = st.top(); st.pop(); 12 | st.push(max(1, 2 * a) + b); 13 | } 14 | } 15 | return st.top(); 16 | } 17 | }; -------------------------------------------------------------------------------- /856-score-of-parentheses/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /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 | int i = 0, j = people.size() - 1; 6 | int ans = 0; 7 | while(i <= j) { 8 | if (people[i] + people[j] <= limit) 9 | i++; 10 | j--; 11 | ans++; 12 | } 13 | 14 | return ans; 15 | } 16 | }; -------------------------------------------------------------------------------- /881-boats-to-save-people/881-boats-to-save-people.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int numRescueBoats(int[] people, int limit) { 3 | Arrays.sort(people); 4 | int i = 0, j = people.length - 1; 5 | int ans = 0; 6 | while(i <= j) { 7 | if (people[i] + people[j] <= limit) 8 | i++; 9 | j--; 10 | ans++; 11 | } 12 | 13 | return ans; 14 | } 15 | } -------------------------------------------------------------------------------- /881-boats-to-save-people/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /890-find-and-replace-pattern/890-find-and-replace-pattern.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string decodeStringToNumString(string& code) { 4 | string numStr = ""; 5 | unordered_map umap; 6 | int i = 1; 7 | for(char ch : code) { 8 | if(umap[ch - 'a'] == 0) { 9 | umap[ch - 'a'] = i; 10 | } 11 | numStr += to_string(umap[ch - 'a']) + " "; 12 | i++; 13 | } 14 | return numStr; 15 | } 16 | 17 | vector findAndReplacePattern(vector& words, string pattern) { 18 | string decodedPatternString = decodeStringToNumString(pattern); 19 | vector ans; 20 | for(string& word : words) { 21 | string decoded_word = decodeStringToNumString(word); 22 | if(decoded_word == decodedPatternString) { 23 | ans.push_back(word); 24 | } 25 | } 26 | return ans; 27 | } 28 | }; -------------------------------------------------------------------------------- /890-find-and-replace-pattern/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /895-maximum-frequency-stack/895-maximum-frequency-stack.cpp: -------------------------------------------------------------------------------- 1 | class FreqStack { 2 | private: 3 | unordered_map m; /* {ele, freq} */ 4 | unordered_map> st; 5 | int maxFreq=0; 6 | 7 | public: 8 | FreqStack() { 9 | 10 | } 11 | 12 | void push(int x) { 13 | m[x]++; 14 | st[m[x]].push(x); 15 | if(m[x]>maxFreq) 16 | maxFreq=m[x]; 17 | } 18 | 19 | int pop() { 20 | int x=st[maxFreq].top(); 21 | st[maxFreq].pop(); 22 | m[x]--; 23 | if(st[maxFreq].size()==0) 24 | maxFreq--; 25 | return x; 26 | } 27 | }; 28 | 29 | /** 30 | * Your FreqStack object will be instantiated and called as such: 31 | * FreqStack* obj = new FreqStack(); 32 | * obj->push(x); 33 | * int param_2 = obj->pop(); 34 | */ -------------------------------------------------------------------------------- /895-maximum-frequency-stack/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /897-increasing-order-search-tree/897-increasing-order-search-tree.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * public class TreeNode { 4 | * int val; 5 | * TreeNode left; 6 | * TreeNode right; 7 | * TreeNode() {} 8 | * TreeNode(int val) { this.val = val; } 9 | * TreeNode(int val, TreeNode left, TreeNode right) { 10 | * this.val = val; 11 | * this.left = left; 12 | * this.right = right; 13 | * } 14 | * } 15 | */ 16 | class Solution { 17 | public TreeNode ans; 18 | public void in(TreeNode root) { 19 | if(root == null) return; 20 | in(root.left); 21 | ans.right = root; 22 | ans.right.left = null; 23 | ans = ans.right; 24 | in(root.right); 25 | } 26 | public TreeNode increasingBST(TreeNode root) { 27 | ans = new TreeNode(-1); 28 | TreeNode nroot = ans; 29 | in(root); 30 | return nroot.right; 31 | } 32 | } -------------------------------------------------------------------------------- /897-increasing-order-search-tree/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /905-sort-array-by-parity/905-sort-array-by-parity.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector sortArrayByParity(vector& A) { 4 | int i = 0, j = A.size() - 1; 5 | while(i < j) { 6 | if(A[j] % 2 != 0) { 7 | j--; 8 | } else if(A[i] % 2 == 0) { 9 | i++; 10 | } else { 11 | int temp = A[i]; 12 | A[i] = A[j]; 13 | A[j] = temp; 14 | i++; 15 | j--; 16 | } 17 | } 18 | return A; 19 | } 20 | }; 21 | auto SpeedUp = []() { 22 | std::ios::sync_with_stdio(false); 23 | std::cin.tie(nullptr); 24 | std::cout.tie(nullptr); 25 | return nullptr; 26 | }(); -------------------------------------------------------------------------------- /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 |
-------------------------------------------------------------------------------- /91-decode-ways/91-decode-ways.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numDecodings(string s) { 4 | // prev1 -> i + 1 dp, prev2 -> i + 2 dp 5 | int prev1 = 1, prev2 = 0; 6 | for(int i = s.length() - 1; i >= 0; --i) { 7 | int curr = 0; 8 | if(s[i] != '0') { 9 | curr += prev1; 10 | } 11 | if(i + 1 < s.length() && (s[i] == '1' || s[i] == '2' && s[i+1] <= '6')) { 12 | curr += prev2; 13 | } 14 | prev2 = prev1; 15 | prev1 = curr; 16 | } 17 | 18 | return prev1; 19 | } 20 | }; -------------------------------------------------------------------------------- /91-decode-ways/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /912-sort-an-array/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /912-sort-an-array/README.md: -------------------------------------------------------------------------------- 1 |

912. Sort an Array

Medium


Given an array of integers nums, sort the array in ascending order.

2 | 3 |

 

4 |

Example 1:

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

Example 2:

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

 

12 |

Constraints:

13 | 14 |
    15 |
  • 1 <= nums.length <= 5 * 104
  • 16 |
  • -5 * 104 <= nums[i] <= 5 * 104
  • 17 |
18 |
-------------------------------------------------------------------------------- /92-reverse-linked-list-ii/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /923-3sum-with-multiplicity/923-3sum-with-multiplicity.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int threeSumMulti(vector& arr, int target) { 4 | int n = arr.size(); 5 | int mod = 1e9+7; 6 | int ans = 0; 7 | unordered_map m; 8 | 9 | for(int i = 0; i < n; i++) { 10 | ans = (ans + m[target - arr[i]]) % mod; 11 | 12 | for(int j = 0; j < i; j++) { 13 | m[arr[i] + arr[j]]++; 14 | } 15 | } 16 | return ans; 17 | } 18 | }; -------------------------------------------------------------------------------- /923-3sum-with-multiplicity/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /931-minimum-falling-path-sum/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /935-knight-dialer/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /94-binary-tree-inorder-traversal/94-binary-tree-inorder-traversal.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * public class TreeNode { 4 | * int val; 5 | * TreeNode left; 6 | * TreeNode right; 7 | * TreeNode() {} 8 | * TreeNode(int val) { this.val = val; } 9 | * TreeNode(int val, TreeNode left, TreeNode right) { 10 | * this.val = val; 11 | * this.left = left; 12 | * this.right = right; 13 | * } 14 | * } 15 | */ 16 | class Solution { 17 | List ans = new ArrayList(); 18 | 19 | public void inorder(TreeNode root) { 20 | if(root == null) 21 | return; 22 | 23 | inorder(root.left); 24 | ans.add(root.val); 25 | inorder(root.right); 26 | } 27 | 28 | public List inorderTraversal(TreeNode root) { 29 | inorder(root); 30 | return ans; 31 | } 32 | } -------------------------------------------------------------------------------- /94-binary-tree-inorder-traversal/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /940-distinct-subsequences-ii/940-distinct-subsequences-ii.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | #define MOD 1000000007 4 | #define ll long long 5 | int distinctSubseqII(string s) { 6 | int n = s.size(); 7 | vector lastOccurrenceOf(26, -1); 8 | vector dp(n + 1, 0); 9 | dp[0] = 1; 10 | for(int i = 0; i < n; i++) { 11 | dp[i + 1] = dp[i] * 2 % MOD; 12 | if(lastOccurrenceOf[s[i] - 'a'] != -1) { 13 | dp[i + 1] -= dp[lastOccurrenceOf[s[i] - 'a'] - 1]; 14 | } 15 | dp[i + 1] %= MOD; 16 | lastOccurrenceOf[s[i] - 'a'] = i + 1; 17 | } 18 | 19 | return dp[n] - 1 < 0 ? dp[n] += MOD - 1 : dp[n] - 1; 20 | } 21 | }; -------------------------------------------------------------------------------- /946-validate-stack-sequences/946-validate-stack-sequences.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool validateStackSequences(vector& pushed, vector& popped) { 4 | stackst; 5 | int j=0; 6 | for(int e : pushed) { 7 | st.push(e); 8 | while(!st.empty() && j startValue) { 6 | ans++; 7 | if (target % 2 == 1) 8 | target++; 9 | else 10 | target /= 2; 11 | } 12 | 13 | return ans + startValue - target; 14 | } 15 | }; -------------------------------------------------------------------------------- /991-broken-calculator/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /993-cousins-in-binary-tree/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /994-rotting-oranges/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /Armstrong Numbers - GFG/armstrong-numbers.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | // Initial Template for C++ 3 | #include 4 | using namespace std; 5 | 6 | // } Driver Code Ends 7 | // User function Template for C++ 8 | class Solution { 9 | public: 10 | string armstrongNumber(int n) { 11 | // code here 12 | int orignalNumber = n; 13 | int armstrongSum = 0; 14 | while (n != 0) { 15 | int digit = n % 10; 16 | n = n / 10; 17 | armstrongSum += pow(digit, 3); 18 | } 19 | return armstrongSum == orignalNumber ? "Yes" : "No"; 20 | } 21 | }; 22 | 23 | //{ Driver Code Starts. 24 | int main() { 25 | int t; 26 | cin >> t; 27 | while (t--) { 28 | int n; 29 | cin >> n; 30 | Solution ob; 31 | cout << ob.armstrongNumber(n) << endl; 32 | } 33 | return 0; 34 | } 35 | 36 | // } Driver Code Ends -------------------------------------------------------------------------------- /Count possible ways to construct buildings - GFG/count-possible-ways-to-construct-buildings.cpp: -------------------------------------------------------------------------------- 1 | // { Driver Code Starts 2 | #include 3 | using namespace std; 4 | 5 | // } Driver Code Ends 6 | class Solution{ 7 | public: 8 | #define MOD 1000000007 9 | #define ll long long 10 | int TotalWays(int N) 11 | { 12 | // Code here 13 | if(N == 1) { 14 | return 2*2; 15 | } 16 | 17 | if(N == 2) { 18 | return 3*3; 19 | } 20 | 21 | ll prev1 = 3; 22 | ll prev2 = 2; 23 | 24 | for(int i = 3; i <= N; i++) { 25 | ll curr = (prev1 + prev2) % MOD; 26 | prev2 = prev1; 27 | prev1 = curr; 28 | } 29 | 30 | return (prev1 * prev1) % MOD; 31 | } 32 | }; 33 | 34 | // { Driver Code Starts. 35 | int main(){ 36 | int tc; 37 | cin >> tc; 38 | while(tc--){ 39 | int N; 40 | cin >> N; 41 | Solution ob; 42 | int ans = ob.TotalWays(N); 43 | cout << ans <<"\n"; 44 | } 45 | return 0; 46 | } // } Driver Code Ends -------------------------------------------------------------------------------- /Friends Pairing Problem - GFG/friends-pairing-problem.cpp: -------------------------------------------------------------------------------- 1 | // { Driver Code Starts 2 | #include 3 | using namespace std; 4 | 5 | // } Driver Code Ends 6 | class Solution 7 | { 8 | public: 9 | #define ll long long 10 | #define MOD 1000000007 11 | int countFriendsPairings(int n) 12 | { 13 | // code here 14 | if(n == 1 || n == 2) { 15 | return n; 16 | } 17 | 18 | ll prev1 = 2, prev2 = 1; 19 | for(int i = 3; i <= n; i++) { 20 | ll curr = (prev1 + (i - 1) * prev2) % MOD; 21 | prev2 = prev1; 22 | prev1 = curr; 23 | } 24 | 25 | return prev1; 26 | } 27 | }; 28 | 29 | 30 | // { Driver Code Starts. 31 | int main() 32 | { 33 | int t; 34 | cin>>t; 35 | while(t--) 36 | { 37 | int n; 38 | cin>>n; 39 | Solution ob; 40 | cout < map = new HashMap<>(); 4 | for (int i = 0; i < s.length(); i++) { 5 | if (map.containsKey(s.charAt(i)) == true) { 6 | if (map.get(s.charAt(i)) != t.charAt(i)) { 7 | return false; 8 | } 9 | } else { 10 | map.put(s.charAt(i), t.charAt(i)); 11 | } 12 | } 13 | return true; 14 | } 15 | 16 | public boolean isIsomorphic(String s, String t) { 17 | boolean check1 = helper(s, t); 18 | boolean check2 = helper(t, s); 19 | 20 | if (check1 == false || check2 == false) { 21 | return false; 22 | } 23 | 24 | return true; 25 | } 26 | } -------------------------------------------------------------------------------- /nCr - GFG/n-cr.cpp: -------------------------------------------------------------------------------- 1 | // { Driver Code Starts 2 | // Initial Template for C++ 3 | 4 | #include 5 | using namespace std; 6 | 7 | // } Driver Code Ends 8 | // User function Template for C++ 9 | 10 | class Solution{ 11 | public: 12 | #define ll long long 13 | #define MOD 1000000007 14 | int nCr(int n, int r){ 15 | // code here 16 | if(r > n) 17 | return 0; 18 | vector ans(n + 1, 0); 19 | ans[0] = 1; 20 | for(int i = 1; i <= n; i++) { 21 | for(int j = i; j > 0; j--) { 22 | ans[j] = (ans[j] + ans[j - 1]) % MOD; 23 | } 24 | } 25 | return ans[r]; 26 | } 27 | }; 28 | 29 | // { Driver Code Starts. 30 | 31 | int main(){ 32 | int t; 33 | cin>>t; 34 | while(t--){ 35 | int n, r; 36 | cin>>n>>r; 37 | 38 | Solution ob; 39 | cout< 5 | using namespace std; 6 | 7 | // } Driver Code Ends 8 | // User function Template for C++ 9 | 10 | class Solution{ 11 | public: 12 | long long fact(int n) { 13 | long long a = 1; 14 | for(int i = 1; i <= n; i++) { 15 | a *= i; 16 | } 17 | return a; 18 | } 19 | 20 | long long nPr(int n, int r){ 21 | // code here 22 | if(r > n) 23 | return 0; 24 | return fact(n) / fact(n - r); 25 | } 26 | }; 27 | 28 | // { Driver Code Starts. 29 | 30 | int main(){ 31 | int t; 32 | cin>>t; 33 | while(t--){ 34 | int n, r; 35 | cin>>n>>r; 36 | 37 | Solution ob; 38 | cout<