├── 0 - 1 Knapsack Problem - GFG ├── 0-1-knapsack-problem.java └── README.md ├── 0001-two-sum ├── 0001-two-sum.java └── README.md ├── 0003-longest-substring-without-repeating-characters ├── 0003-longest-substring-without-repeating-characters.java └── README.md ├── 0004-median-of-two-sorted-arrays ├── 0004-median-of-two-sorted-arrays.java ├── NOTES.md └── README.md ├── 0007-reverse-integer ├── 0007-reverse-integer.java ├── NOTES.md └── README.md ├── 0013-roman-to-integer ├── 0013-roman-to-integer.java └── README.md ├── 0017-letter-combinations-of-a-phone-number ├── 0017-letter-combinations-of-a-phone-number.java └── README.md ├── 0019-remove-nth-node-from-end-of-list ├── 0019-remove-nth-node-from-end-of-list.java ├── NOTES.md └── README.md ├── 0020-valid-parentheses ├── 0020-valid-parentheses.java ├── NOTES.md └── README.md ├── 0022-generate-parentheses ├── 0022-generate-parentheses.java └── README.md ├── 0026-remove-duplicates-from-sorted-array ├── 0026-remove-duplicates-from-sorted-array.java └── README.md ├── 0033-search-in-rotated-sorted-array ├── 0033-search-in-rotated-sorted-array.java └── README.md ├── 0034-find-first-and-last-position-of-element-in-sorted-array ├── 0034-find-first-and-last-position-of-element-in-sorted-array.java └── README.md ├── 0035-search-insert-position ├── 0035-search-insert-position.java └── README.md ├── 0036-valid-sudoku ├── 0036-valid-sudoku.java └── README.md ├── 0038-count-and-say ├── 0038-count-and-say.java └── README.md ├── 0040-combination-sum-ii ├── 0040-combination-sum-ii.java └── README.md ├── 0042-trapping-rain-water ├── 0042-trapping-rain-water.java └── README.md ├── 0046-permutations ├── 0046-permutations.java └── README.md ├── 0048-rotate-image ├── 0048-rotate-image.java └── README.md ├── 0049-group-anagrams ├── 0049-group-anagrams.java └── README.md ├── 0050-powx-n ├── 0050-powx-n.java └── README.md ├── 0053-maximum-subarray ├── 0053-maximum-subarray.java └── README.md ├── 0054-spiral-matrix ├── 0054-spiral-matrix.java └── README.md ├── 0055-jump-game ├── 0055-jump-game.java └── README.md ├── 0061-rotate-list ├── 0061-rotate-list.java └── README.md ├── 0062-unique-paths ├── 0062-unique-paths.java └── README.md ├── 0063-unique-paths-ii ├── 0063-unique-paths-ii.java └── README.md ├── 0064-minimum-path-sum ├── 0064-minimum-path-sum.java └── README.md ├── 0066-plus-one ├── 0066-plus-one.java └── README.md ├── 0068-text-justification └── README.md ├── 0070-climbing-stairs ├── 0070-climbing-stairs.java └── README.md ├── 0072-edit-distance ├── 0072-edit-distance.java └── README.md ├── 0073-set-matrix-zeroes ├── 0073-set-matrix-zeroes.java └── README.md ├── 0074-search-a-2d-matrix ├── 0074-search-a-2d-matrix.java └── README.md ├── 0075-sort-colors ├── 0075-sort-colors.java └── README.md ├── 0076-minimum-window-substring ├── 0076-minimum-window-substring.java └── README.md ├── 0077-combinations ├── 0077-combinations.java └── README.md ├── 0081-search-in-rotated-sorted-array-ii ├── 0081-search-in-rotated-sorted-array-ii.java └── README.md ├── 0086-partition-list └── README.md ├── 0088-merge-sorted-array ├── 0088-merge-sorted-array.java ├── NOTES.md └── README.md ├── 0090-subsets-ii ├── 0090-subsets-ii.java └── README.md ├── 0091-decode-ways ├── 0091-decode-ways.java └── README.md ├── 0094-binary-tree-inorder-traversal ├── 0094-binary-tree-inorder-traversal.java └── README.md ├── 0095-unique-binary-search-trees-ii └── README.md ├── 0097-interleaving-string ├── 0097-interleaving-string.java └── README.md ├── 0110-balanced-binary-tree ├── 0110-balanced-binary-tree.java └── README.md ├── 0118-pascals-triangle ├── 0118-pascals-triangle.java └── README.md ├── 0121-best-time-to-buy-and-sell-stock ├── 0121-best-time-to-buy-and-sell-stock.java └── README.md ├── 0122-best-time-to-buy-and-sell-stock-ii ├── 0122-best-time-to-buy-and-sell-stock-ii.java └── README.md ├── 0125-valid-palindrome ├── 0125-valid-palindrome.java └── README.md ├── 0128-longest-consecutive-sequence ├── 0128-longest-consecutive-sequence.java └── README.md ├── 0131-palindrome-partitioning ├── 0131-palindrome-partitioning.java └── README.md ├── 0135-candy ├── 0135-candy.java └── README.md ├── 0141-linked-list-cycle ├── 0141-linked-list-cycle.java └── README.md ├── 0142-linked-list-cycle-ii ├── 0142-linked-list-cycle-ii.java ├── NOTES.md └── README.md ├── 0145-binary-tree-postorder-traversal ├── 0145-binary-tree-postorder-traversal.java └── README.md ├── 0151-reverse-words-in-a-string ├── 0151-reverse-words-in-a-string.java └── README.md ├── 0153-find-minimum-in-rotated-sorted-array ├── 0153-find-minimum-in-rotated-sorted-array.java └── README.md ├── 0160-intersection-of-two-linked-lists ├── 0160-intersection-of-two-linked-lists.java └── README.md ├── 0162-find-peak-element ├── 0162-find-peak-element.java └── README.md ├── 0168-excel-sheet-column-title └── README.md ├── 0179-largest-number ├── 0179-largest-number.java └── README.md ├── 0198-house-robber ├── 0198-house-robber.java └── README.md ├── 0200-number-of-islands ├── 0200-number-of-islands.java └── README.md ├── 0206-reverse-linked-list ├── 0206-reverse-linked-list.java ├── NOTES.md └── README.md ├── 0213-house-robber-ii ├── 0213-house-robber-ii.java └── README.md ├── 0214-shortest-palindrome ├── 0214-shortest-palindrome.java └── README.md ├── 0215-kth-largest-element-in-an-array ├── 0215-kth-largest-element-in-an-array.java └── README.md ├── 0217-contains-duplicate ├── 0217-contains-duplicate.java └── README.md ├── 0228-summary-ranges ├── 0228-summary-ranges.java └── README.md ├── 0234-palindrome-linked-list ├── 0234-palindrome-linked-list.java ├── NOTES.md └── README.md ├── 0239-sliding-window-maximum ├── 0239-sliding-window-maximum.java └── README.md ├── 0240-search-a-2d-matrix-ii ├── 0240-search-a-2d-matrix-ii.java └── README.md ├── 0242-valid-anagram ├── 0242-valid-anagram.java └── README.md ├── 0264-ugly-number-ii ├── 0264-ugly-number-ii.java └── README.md ├── 0273-integer-to-english-words ├── 0273-integer-to-english-words.java └── README.md ├── 0283-move-zeroes ├── 0283-move-zeroes.java └── README.md ├── 0287-find-the-duplicate-number ├── 0287-find-the-duplicate-number.java └── README.md ├── 0300-longest-increasing-subsequence ├── 0300-longest-increasing-subsequence.java └── README.md ├── 0322-coin-change ├── 0322-coin-change.java └── README.md ├── 0330-patching-array ├── 0330-patching-array.java └── README.md ├── 0332-reconstruct-itinerary └── README.md ├── 0338-counting-bits ├── 0338-counting-bits.java └── README.md ├── 0344-reverse-string ├── 0344-reverse-string.java └── README.md ├── 0347-top-k-frequent-elements ├── 0347-top-k-frequent-elements.java └── README.md ├── 0350-intersection-of-two-arrays-ii ├── 0350-intersection-of-two-arrays-ii.java └── README.md ├── 0368-largest-divisible-subset ├── 0368-largest-divisible-subset.java └── README.md ├── 0383-ransom-note ├── 0383-ransom-note.java └── README.md ├── 0386-lexicographical-numbers ├── 0386-lexicographical-numbers.java └── README.md ├── 0392-is-subsequence ├── 0392-is-subsequence.java └── README.md ├── 0403-frog-jump └── README.md ├── 0407-trapping-rain-water-ii ├── 0407-trapping-rain-water-ii.java └── README.md ├── 0410-split-array-largest-sum ├── 0410-split-array-largest-sum.java └── README.md ├── 0416-partition-equal-subset-sum ├── 0416-partition-equal-subset-sum.java └── README.md ├── 0432-all-oone-data-structure └── README.md ├── 0440-k-th-smallest-in-lexicographical-order ├── 0440-k-th-smallest-in-lexicographical-order.java └── README.md ├── 0451-sort-characters-by-frequency ├── 0451-sort-characters-by-frequency.java └── README.md ├── 0455-assign-cookies ├── 0455-assign-cookies.java └── README.md ├── 0476-number-complement ├── 0476-number-complement.java └── README.md ├── 0485-max-consecutive-ones ├── 0485-max-consecutive-ones.java └── README.md ├── 0486-predict-the-winner ├── 0486-predict-the-winner.java └── README.md ├── 0494-target-sum ├── 0494-target-sum.java └── README.md ├── 0502-ipo ├── 0502-ipo.java └── README.md ├── 0515-find-largest-value-in-each-tree-row ├── 0515-find-largest-value-in-each-tree-row.java └── README.md ├── 0516-longest-palindromic-subsequence ├── 0516-longest-palindromic-subsequence.java └── README.md ├── 0518-coin-change-ii └── README.md ├── 0523-continuous-subarray-sum ├── 0523-continuous-subarray-sum.java └── README.md ├── 0539-minimum-time-difference ├── 0539-minimum-time-difference.java └── README.md ├── 0540-single-element-in-a-sorted-array ├── 0540-single-element-in-a-sorted-array.java └── README.md ├── 0542-01-matrix ├── 0542-01-matrix.java └── README.md ├── 0547-number-of-provinces ├── 0547-number-of-provinces.java └── README.md ├── 0567-permutation-in-string ├── 0567-permutation-in-string.java └── README.md ├── 0583-delete-operation-for-two-strings ├── 0583-delete-operation-for-two-strings.java └── README.md ├── 0592-fraction-addition-and-subtraction └── README.md ├── 0624-maximum-distance-in-arrays ├── 0624-maximum-distance-in-arrays.java └── README.md ├── 0633-sum-of-square-numbers ├── 0633-sum-of-square-numbers.java └── README.md ├── 0646-maximum-length-of-pair-chain ├── 0646-maximum-length-of-pair-chain.java └── README.md ├── 0647-palindromic-substrings ├── 0647-palindromic-substrings.java └── README.md ├── 0648-replace-words ├── 0648-replace-words.java └── README.md ├── 0650-2-keys-keyboard ├── 0650-2-keys-keyboard.java └── README.md ├── 0664-strange-printer ├── 0664-strange-printer.java └── README.md ├── 0670-maximum-swap └── README.md ├── 0684-redundant-connection ├── 0684-redundant-connection.java └── README.md ├── 0689-maximum-sum-of-3-non-overlapping-subarrays ├── 0689-maximum-sum-of-3-non-overlapping-subarrays.java └── README.md ├── 0695-max-area-of-island ├── 0695-max-area-of-island.java └── README.md ├── 0712-minimum-ascii-delete-sum-for-two-strings ├── 0712-minimum-ascii-delete-sum-for-two-strings.java └── README.md ├── 0714-best-time-to-buy-and-sell-stock-with-transaction-fee ├── 0714-best-time-to-buy-and-sell-stock-with-transaction-fee.java └── README.md ├── 0719-find-k-th-smallest-pair-distance ├── 0719-find-k-th-smallest-pair-distance.java └── README.md ├── 0724-find-pivot-index ├── 0724-find-pivot-index.java └── README.md ├── 0725-split-linked-list-in-parts ├── 0725-split-linked-list-in-parts.java └── README.md ├── 0726-number-of-atoms ├── 0726-number-of-atoms.java └── README.md ├── 0729-my-calendar-i ├── 0729-my-calendar-i.java └── README.md ├── 0731-my-calendar-ii ├── 0731-my-calendar-ii.java └── README.md ├── 0740-delete-and-earn ├── 0740-delete-and-earn.java └── README.md ├── 0745-find-smallest-letter-greater-than-target ├── 0745-find-smallest-letter-greater-than-target.java └── README.md ├── 0747-min-cost-climbing-stairs ├── 0747-min-cost-climbing-stairs.java └── README.md ├── 0768-partition-labels ├── 0768-partition-labels.java └── README.md ├── 0776-n-ary-tree-postorder-traversal ├── 0776-n-ary-tree-postorder-traversal.java └── README.md ├── 0778-reorganize-string ├── 0778-reorganize-string.java └── README.md ├── 0780-max-chunks-to-make-sorted ├── 0780-max-chunks-to-make-sorted.java └── README.md ├── 0787-sliding-puzzle ├── 0787-sliding-puzzle.java └── README.md ├── 0789-kth-largest-element-in-a-stream ├── 0789-kth-largest-element-in-a-stream.java └── README.md ├── 0797-rabbits-in-forest ├── 0797-rabbits-in-forest.java └── README.md ├── 0806-domino-and-tromino-tiling ├── 0806-domino-and-tromino-tiling.java └── README.md ├── 0812-rotate-string ├── 0812-rotate-string.java └── README.md ├── 0820-find-eventual-safe-states ├── 0820-find-eventual-safe-states.java └── README.md ├── 0826-soup-servings ├── 0826-soup-servings.java └── README.md ├── 0853-most-profit-assigning-work ├── 0853-most-profit-assigning-work.java └── README.md ├── 0854-making-a-large-island ├── 0854-making-a-large-island.java └── README.md ├── 0859-design-circular-deque ├── 0859-design-circular-deque.java └── README.md ├── 0868-push-dominoes ├── 0868-push-dominoes.java └── README.md ├── 0870-magic-squares-in-grid ├── 0870-magic-squares-in-grid.java └── README.md ├── 0876-middle-of-the-linked-list ├── 0876-middle-of-the-linked-list.java ├── NOTES.md └── README.md ├── 0882-peak-index-in-a-mountain-array ├── 0882-peak-index-in-a-mountain-array.java └── README.md ├── 0890-lemonade-change ├── 0890-lemonade-change.java └── README.md ├── 0892-shortest-subarray-with-sum-at-least-k └── README.md ├── 0905-length-of-longest-fibonacci-subsequence ├── 0905-length-of-longest-fibonacci-subsequence.java └── README.md ├── 0906-walking-robot-simulation ├── 0906-walking-robot-simulation.java └── README.md ├── 0907-koko-eating-bananas ├── 0907-koko-eating-bananas.java └── README.md ├── 0920-uncommon-words-from-two-sentences ├── 0920-uncommon-words-from-two-sentences.java └── README.md ├── 0921-spiral-matrix-iii ├── 0921-spiral-matrix-iii.java └── README.md ├── 0925-construct-binary-tree-from-preorder-and-postorder-traversal ├── 0925-construct-binary-tree-from-preorder-and-postorder-traversal.java └── README.md ├── 0945-snakes-and-ladders ├── 0945-snakes-and-ladders.java └── README.md ├── 0952-word-subsets ├── 0952-word-subsets.java └── README.md ├── 0956-number-of-music-playlists ├── 0956-number-of-music-playlists.java └── README.md ├── 0957-minimum-add-to-make-parentheses-valid ├── 0957-minimum-add-to-make-parentheses-valid.java └── README.md ├── 0971-shortest-bridge ├── 0971-shortest-bridge.java └── README.md ├── 0982-minimum-increment-to-make-array-unique ├── 0982-minimum-increment-to-make-array-unique.java └── README.md ├── 0984-most-stones-removed-with-same-row-or-column ├── 0984-most-stones-removed-with-same-row-or-column.java └── README.md ├── 0988-flip-equivalent-binary-trees ├── 0988-flip-equivalent-binary-trees.java └── README.md ├── 1-two-sum ├── 1-two-sum.java ├── NOTES.md └── README.md ├── 1002-maximum-width-ramp └── README.md ├── 1013-fibonacci-number ├── 1013-fibonacci-number.java └── README.md ├── 1016-subarray-sums-divisible-by-k ├── 1016-subarray-sums-divisible-by-k.java └── README.md ├── 1025-minimum-cost-for-tickets ├── 1025-minimum-cost-for-tickets.java └── README.md ├── 1044-find-common-characters ├── 1044-find-common-characters.java └── README.md ├── 1049-minimum-domino-rotations-for-equal-row ├── 1049-minimum-domino-rotations-for-equal-row.java └── README.md ├── 1056-capacity-to-ship-packages-within-d-days ├── 1056-capacity-to-ship-packages-within-d-days.java └── README.md ├── 1058-lexicographically-smallest-equivalent-string ├── 1058-lexicographically-smallest-equivalent-string.java └── README.md ├── 1063-best-sightseeing-pair ├── 1063-best-sightseeing-pair.java └── README.md ├── 1073-number-of-enclaves ├── 1073-number-of-enclaves.java └── README.md ├── 1087-longest-arithmetic-subsequence ├── 1087-longest-arithmetic-subsequence.java └── README.md ├── 1093-recover-a-tree-from-preorder-traversal ├── 1093-recover-a-tree-from-preorder-traversal.java └── README.md ├── 1114-binary-search-tree-to-greater-sum-tree ├── 1114-binary-search-tree-to-greater-sum-tree.java └── README.md ├── 1129-longest-string-chain ├── 1129-longest-string-chain.java └── README.md ├── 1138-grumpy-bookstore-owner ├── 1138-grumpy-bookstore-owner.java └── README.md ├── 1147-flip-columns-for-maximum-number-of-equal-rows └── README.md ├── 1160-letter-tile-possibilities ├── 1160-letter-tile-possibilities.java └── README.md ├── 1170-shortest-common-supersequence ├── 1170-shortest-common-supersequence.java └── README.md ├── 1196-filling-bookcase-shelves ├── 1196-filling-bookcase-shelves.java └── README.md ├── 1197-parsing-a-boolean-expression ├── 1197-parsing-a-boolean-expression.java └── README.md ├── 1218-lowest-common-ancestor-of-deepest-leaves ├── 1218-lowest-common-ancestor-of-deepest-leaves.java └── README.md ├── 1227-number-of-equivalent-domino-pairs ├── 1227-number-of-equivalent-domino-pairs.java └── README.md ├── 1236-n-th-tribonacci-number ├── 1236-n-th-tribonacci-number.java └── README.md ├── 1240-stone-game-ii ├── 1240-stone-game-ii.java └── README.md ├── 1249-snapshot-array ├── 1249-snapshot-array.java └── README.md ├── 1250-longest-common-subsequence ├── 1250-longest-common-subsequence.java └── README.md ├── 1256-rank-transform-of-an-array ├── 1256-rank-transform-of-an-array.java └── README.md ├── 1293-three-consecutive-odds ├── 1293-three-consecutive-odds.java └── README.md ├── 1298-reverse-substrings-between-each-pair-of-parentheses └── README.md ├── 13-roman-to-integer ├── 13-roman-to-integer.java ├── NOTES.md └── README.md ├── 1302-delete-characters-to-make-fancy-string ├── 1302-delete-characters-to-make-fancy-string.java └── README.md ├── 1304-longest-happy-string ├── 1304-longest-happy-string.java └── README.md ├── 1305-number-of-visible-people-in-a-queue ├── 1305-number-of-visible-people-in-a-queue.java └── README.md ├── 1325-path-with-maximum-probability ├── 1325-path-with-maximum-probability.java └── README.md ├── 1333-sort-the-jumbled-numbers ├── 1333-sort-the-jumbled-numbers.java └── README.md ├── 1335-maximum-candies-allocated-to-k-children ├── 1335-maximum-candies-allocated-to-k-children.java └── README.md ├── 1350-remove-sub-folders-from-the-filesystem ├── 1350-remove-sub-folders-from-the-filesystem.java └── README.md ├── 1364-tuple-with-same-product ├── 1364-tuple-with-same-product.java └── README.md ├── 1370-count-number-of-nice-subarrays ├── 1370-count-number-of-nice-subarrays.java └── README.md ├── 1380-number-of-closed-islands ├── 1380-number-of-closed-islands.java └── README.md ├── 1387-find-elements-in-a-contaminated-binary-tree ├── 1387-find-elements-in-a-contaminated-binary-tree.java └── README.md ├── 1392-find-the-difference-of-two-arrays ├── 1392-find-the-difference-of-two-arrays.java └── README.md ├── 1396-count-servers-that-communicate ├── 1396-count-servers-that-communicate.java └── README.md ├── 14-longest-common-prefix ├── 14-longest-common-prefix.java ├── NOTES.md └── README.md ├── 1402-count-square-submatrices-with-all-ones ├── 1402-count-square-submatrices-with-all-ones.java └── README.md ├── 1407-group-the-people-given-the-group-size-they-belong-to ├── 1407-group-the-people-given-the-group-size-they-belong-to.java └── README.md ├── 1408-find-the-smallest-divisor-given-a-threshold ├── 1408-find-the-smallest-divisor-given-a-threshold.java └── README.md ├── 1421-find-numbers-with-even-number-of-digits ├── 1421-find-numbers-with-even-number-of-digits.java └── README.md ├── 1424-maximum-candies-you-can-get-from-boxes ├── 1424-maximum-candies-you-can-get-from-boxes.java └── README.md ├── 1431-all-ancestors-of-a-node-in-a-directed-acyclic-graph ├── 1431-all-ancestors-of-a-node-in-a-directed-acyclic-graph.java └── README.md ├── 1435-xor-queries-of-a-subarray ├── 1435-xor-queries-of-a-subarray.java └── README.md ├── 1437-minimum-insertion-steps-to-make-a-string-palindrome ├── 1437-minimum-insertion-steps-to-make-a-string-palindrome.java └── README.md ├── 1451-minimum-number-of-taps-to-open-to-water-a-garden ├── 1451-minimum-number-of-taps-to-open-to-water-a-garden.java └── README.md ├── 1456-find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance ├── 1456-find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance.java └── README.md ├── 1460-number-of-substrings-containing-all-three-characters ├── 1460-number-of-substrings-containing-all-three-characters.java └── README.md ├── 1461-count-all-valid-pickup-and-delivery-options ├── 1461-count-all-valid-pickup-and-delivery-options.java └── README.md ├── 1473-find-the-longest-substring-containing-vowels-in-even-counts ├── 1473-find-the-longest-substring-containing-vowels-in-even-counts.java └── README.md ├── 1477-product-of-the-last-k-numbers ├── 1477-product-of-the-last-k-numbers.java └── README.md ├── 1484-linked-list-in-binary-tree ├── 1484-linked-list-in-binary-tree.java └── README.md ├── 1485-minimum-cost-to-make-at-least-one-valid-path-in-a-grid ├── 1485-minimum-cost-to-make-at-least-one-valid-path-in-a-grid.java └── README.md ├── 1496-lucky-numbers-in-a-matrix ├── 1496-lucky-numbers-in-a-matrix.java └── README.md ├── 1497-design-a-stack-with-increment-operation ├── 1497-design-a-stack-with-increment-operation.java └── README.md ├── 1500-count-largest-group ├── 1500-count-largest-group.java └── README.md ├── 1502-construct-k-palindrome-strings └── README.md ├── 1511-count-number-of-teams ├── 1511-count-number-of-teams.java └── README.md ├── 1516-the-k-th-lexicographical-string-of-all-happy-strings-of-length-n ├── 1516-the-k-th-lexicographical-string-of-all-happy-strings-of-length-n.java └── README.md ├── 1524-string-matching-in-an-array ├── 1524-string-matching-in-an-array.java └── README.md ├── 1537-maximum-score-after-splitting-a-string ├── 1537-maximum-score-after-splitting-a-string.java └── README.md ├── 1549-longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit ├── 1549-longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit.java └── README.md ├── 1556-make-two-arrays-equal-by-reversing-subarrays ├── 1556-make-two-arrays-equal-by-reversing-subarrays.java └── README.md ├── 1558-course-schedule-iv └── README.md ├── 1570-final-prices-with-a-special-discount-in-a-shop ├── 1570-final-prices-with-a-special-discount-in-a-shop.java └── README.md ├── 1605-minimum-number-of-days-to-make-m-bouquets ├── 1605-minimum-number-of-days-to-make-m-bouquets.java └── README.md ├── 1615-range-sum-of-sorted-subarray-sums ├── 1615-range-sum-of-sorted-subarray-sums.java └── README.md ├── 1620-check-if-array-pairs-are-divisible-by-k ├── 1620-check-if-array-pairs-are-divisible-by-k.java └── README.md ├── 1631-number-of-sub-arrays-with-odd-sum ├── 1631-number-of-sub-arrays-with-odd-sum.java └── README.md ├── 1642-water-bottles ├── 1642-water-bottles.java └── README.md ├── 1646-kth-missing-positive-number ├── 1646-kth-missing-positive-number.java └── README.md ├── 1653-number-of-good-leaf-nodes-pairs ├── 1653-number-of-good-leaf-nodes-pairs.java └── README.md ├── 1656-count-good-triplets ├── 1656-count-good-triplets.java └── README.md ├── 1667-find-kth-bit-in-nth-binary-string ├── 1667-find-kth-bit-in-nth-binary-string.java └── README.md ├── 1675-magnetic-force-between-two-balls ├── 1675-magnetic-force-between-two-balls.java └── README.md ├── 1679-shortest-subarray-to-be-removed-to-make-array-sorted └── README.md ├── 1691-minimum-number-of-days-to-disconnect-island └── README.md ├── 1694-make-sum-divisible-by-p ├── 1694-make-sum-divisible-by-p.java └── README.md ├── 1701-remove-max-number-of-edges-to-keep-graph-fully-traversable ├── 1701-remove-max-number-of-edges-to-keep-graph-fully-traversable.java └── README.md ├── 1706-min-cost-to-connect-all-points ├── 1706-min-cost-to-connect-all-points.java └── README.md ├── 1711-find-valid-matrix-given-row-and-column-sums ├── 1711-find-valid-matrix-given-row-and-column-sums.java └── README.md ├── 1715-split-a-string-into-the-max-number-of-unique-substrings └── README.md ├── 1720-crawler-log-folder ├── 1720-crawler-log-folder.java └── README.md ├── 1741-sort-array-by-increasing-frequency ├── 1741-sort-array-by-increasing-frequency.java └── README.md ├── 1744-number-of-ways-to-form-a-target-string-given-a-dictionary ├── 1744-number-of-ways-to-form-a-target-string-given-a-dictionary.java └── README.md ├── 1753-path-with-minimum-effort ├── 1753-path-with-minimum-effort.java └── README.md ├── 1755-defuse-the-bomb ├── 1755-defuse-the-bomb.java └── README.md ├── 1756-minimum-deletions-to-make-string-balanced ├── 1756-minimum-deletions-to-make-string-balanced.java └── README.md ├── 1766-minimum-number-of-removals-to-make-mountain-array ├── 1766-minimum-number-of-removals-to-make-mountain-array.java └── README.md ├── 1770-minimum-deletions-to-make-character-frequencies-unique └── README.md ├── 1775-design-an-ordered-stream ├── 1775-design-an-ordered-stream.java └── README.md ├── 1776-minimum-operations-to-reduce-x-to-zero ├── 1776-minimum-operations-to-reduce-x-to-zero.java └── README.md ├── 1786-count-the-number-of-consistent-strings ├── 1786-count-the-number-of-consistent-strings.java └── README.md ├── 1803-average-waiting-time ├── 1803-average-waiting-time.java └── README.md ├── 1818-maximum-score-from-removing-substrings ├── 1818-maximum-score-from-removing-substrings.java └── README.md ├── 1833-find-the-highest-altitude ├── 1833-find-the-highest-altitude.java └── README.md ├── 1849-maximum-absolute-sum-of-any-subarray ├── 1849-maximum-absolute-sum-of-any-subarray.java └── README.md ├── 1876-map-of-highest-peak ├── 1876-map-of-highest-peak.java └── README.md ├── 1878-check-if-array-is-sorted-and-rotated ├── 1878-check-if-array-is-sorted-and-rotated.java └── README.md ├── 1886-minimum-limit-of-balls-in-a-bag ├── 1886-minimum-limit-of-balls-in-a-bag.java └── README.md ├── 1889-check-if-number-is-a-sum-of-powers-of-three ├── 1889-check-if-number-is-a-sum-of-powers-of-three.java └── README.md ├── 1890-sum-of-beauty-of-all-substrings ├── 1890-sum-of-beauty-of-all-substrings.java └── README.md ├── 1895-minimum-number-of-operations-to-move-all-balls-to-each-box ├── 1895-minimum-number-of-operations-to-move-all-balls-to-each-box.java └── README.md ├── 1915-check-if-one-string-swap-can-make-strings-equal ├── 1915-check-if-one-string-swap-can-make-strings-equal.java └── README.md ├── 1917-maximum-average-pass-ratio └── README.md ├── 1923-sentence-similarity-iii ├── 1923-sentence-similarity-iii.java └── README.md ├── 1927-maximum-ascending-subarray-sum ├── 1927-maximum-ascending-subarray-sum.java └── README.md ├── 1929-maximum-value-at-a-given-index-in-a-bounded-array ├── 1929-maximum-value-at-a-given-index-in-a-bounded-array.java └── README.md ├── 1940-maximum-xor-for-each-query ├── 1940-maximum-xor-for-each-query.java └── README.md ├── 1951-find-the-winner-of-the-circular-game ├── 1951-find-the-winner-of-the-circular-game.java └── README.md ├── 1972-rotating-the-box ├── 1972-rotating-the-box.java └── README.md ├── 1975-minimum-distance-to-the-target-element └── README.md ├── 1986-largest-color-value-in-a-directed-graph ├── 1986-largest-color-value-in-a-directed-graph.java └── README.md ├── 1993-sum-of-all-subset-xor-totals ├── 1993-sum-of-all-subset-xor-totals.java └── README.md ├── 2-add-two-numbers ├── 2-add-two-numbers.java ├── NOTES.md └── README.md ├── 20-valid-parentheses ├── 20-valid-parentheses.java ├── NOTES.md └── README.md ├── 2000-minimum-speed-to-arrive-on-time ├── 2000-minimum-speed-to-arrive-on-time.java └── README.md ├── 2006-find-the-student-that-will-replace-the-chalk ├── 2006-find-the-student-that-will-replace-the-chalk.java └── README.md ├── 2021-remove-all-occurrences-of-a-substring ├── 2021-remove-all-occurrences-of-a-substring.java └── README.md ├── 2035-count-sub-islands ├── 2035-count-sub-islands.java └── README.md ├── 2038-nearest-exit-from-entrance-in-maze ├── 2038-nearest-exit-from-entrance-in-maze.java └── README.md ├── 2047-find-a-peak-element-ii ├── 2047-find-a-peak-element-ii.java └── README.md ├── 2048-build-array-from-permutation ├── 2048-build-array-from-permutation.java └── README.md ├── 2050-count-good-numbers ├── 2050-count-good-numbers.java └── README.md ├── 2054-the-number-of-the-smallest-unoccupied-chair ├── 2054-the-number-of-the-smallest-unoccupied-chair.java └── README.md ├── 2057-smallest-index-with-equal-value ├── 2057-smallest-index-with-equal-value.java ├── NOTES.md └── README.md ├── 2059-unique-length-3-palindromic-subsequences └── README.md ├── 2061-painting-a-grid-with-three-different-colors ├── 2061-painting-a-grid-with-three-different-colors.java └── README.md ├── 2067-maximum-number-of-points-with-cost ├── 2067-maximum-number-of-points-with-cost.java └── README.md ├── 2076-sum-of-digits-of-string-after-convert ├── 2076-sum-of-digits-of-string-after-convert.java └── README.md ├── 2089-maximum-matrix-sum ├── 2089-maximum-matrix-sum.java └── README.md ├── 2090-number-of-ways-to-arrive-at-destination ├── 2090-number-of-ways-to-arrive-at-destination.java └── README.md ├── 2095-minimum-number-of-swaps-to-make-the-string-balanced ├── 2095-minimum-number-of-swaps-to-make-the-string-balanced.java └── README.md ├── 21-merge-two-sorted-lists ├── 21-merge-two-sorted-lists.java ├── NOTES.md └── README.md ├── 2107-find-unique-binary-string └── README.md ├── 2132-convert-1d-array-into-2d-array ├── 2132-convert-1d-array-into-2d-array.java └── README.md ├── 2145-grid-game ├── 2145-grid-game.java └── README.md ├── 2148-minimum-number-of-moves-to-seat-everyone └── README.md ├── 2149-remove-colored-pieces-if-both-neighbors-are-the-same-color ├── 2149-remove-colored-pieces-if-both-neighbors-are-the-same-color.java └── README.md ├── 2155-find-missing-observations └── README.md ├── 2160-minimum-operations-to-make-a-uni-value-grid ├── 2160-minimum-operations-to-make-a-uni-value-grid.java └── README.md ├── 2163-kth-distinct-string-in-an-array ├── 2163-kth-distinct-string-in-an-array.java └── README.md ├── 2164-two-best-non-overlapping-events └── README.md ├── 2170-count-number-of-maximum-bitwise-or-subsets ├── 2170-count-number-of-maximum-bitwise-or-subsets.java └── README.md ├── 2171-second-minimum-time-to-reach-destination ├── 2171-second-minimum-time-to-reach-destination.java └── README.md ├── 2179-most-beautiful-item-for-each-query ├── 2179-most-beautiful-item-for-each-query.java └── README.md ├── 2180-maximum-number-of-tasks-you-can-assign ├── 2180-maximum-number-of-tasks-you-can-assign.java └── README.md ├── 2181-smallest-index-with-equal-value ├── 2181-smallest-index-with-equal-value.java └── README.md ├── 2182-find-the-minimum-and-maximum-number-of-nodes-between-critical-points ├── 2182-find-the-minimum-and-maximum-number-of-nodes-between-critical-points.java └── README.md ├── 2188-minimized-maximum-of-products-distributed-to-any-store ├── 2188-minimized-maximum-of-products-distributed-to-any-store.java └── README.md ├── 2201-valid-arrangement-of-pairs ├── 2201-valid-arrangement-of-pairs.java └── README.md ├── 2211-k-radius-subarray-averages ├── 2211-k-radius-subarray-averages.java └── README.md ├── 2215-finding-3-digit-even-numbers ├── 2215-finding-3-digit-even-numbers.java └── README.md ├── 2216-delete-the-middle-node-of-a-linked-list ├── 2216-delete-the-middle-node-of-a-linked-list.java └── README.md ├── 2220-find-all-possible-recipes-from-given-supplies ├── 2220-find-all-possible-recipes-from-given-supplies.java └── README.md ├── 2221-check-if-a-parentheses-string-can-be-valid ├── 2221-check-if-a-parentheses-string-can-be-valid.java └── README.md ├── 2237-longest-palindrome-by-concatenating-two-letter-words ├── 2237-longest-palindrome-by-concatenating-two-letter-words.java └── README.md ├── 2246-maximum-employees-to-be-invited-to-a-meeting ├── 2246-maximum-employees-to-be-invited-to-a-meeting.java └── README.md ├── 2248-minimum-cost-of-buying-candies-with-discount ├── 2248-minimum-cost-of-buying-candies-with-discount.java └── README.md ├── 2249-count-the-hidden-sequences ├── 2249-count-the-hidden-sequences.java └── README.md ├── 2255-minimum-swaps-to-group-all-1s-together-ii ├── 2255-minimum-swaps-to-group-all-1s-together-ii.java └── README.md ├── 2262-solving-questions-with-brainpower ├── 2262-solving-questions-with-brainpower.java └── README.md ├── 2263-maximum-running-time-of-n-computers ├── 2263-maximum-running-time-of-n-computers.java └── README.md ├── 2265-partition-array-according-to-given-pivot ├── 2265-partition-array-according-to-given-pivot.java └── README.md ├── 2277-count-equal-and-divisible-pairs-in-an-array ├── 2277-count-equal-and-divisible-pairs-in-an-array.java └── README.md ├── 2280-count-good-triplets-in-an-array ├── 2280-count-good-triplets-in-an-array.java └── README.md ├── 2292-counting-words-with-a-given-prefix ├── 2292-counting-words-with-a-given-prefix.java └── README.md ├── 2300-construct-string-with-repeat-limit ├── 2300-construct-string-with-repeat-limit.java └── README.md ├── 2306-create-binary-tree-from-descriptions ├── 2306-create-binary-tree-from-descriptions.java └── README.md ├── 2308-divide-array-into-equal-pairs ├── 2308-divide-array-into-equal-pairs.java └── README.md ├── 2323-minimum-bit-flips-to-convert-number └── README.md ├── 2343-count-unguarded-cells-in-the-grid ├── 2343-count-unguarded-cells-in-the-grid.java └── README.md ├── 2356-largest-combination-with-bitwise-and-greater-than-zero ├── 2356-largest-combination-with-bitwise-and-greater-than-zero.java └── README.md ├── 2358-number-of-ways-to-split-array ├── 2358-number-of-ways-to-split-array.java └── README.md ├── 2375-minimum-obstacle-removal-to-reach-corner ├── 2375-minimum-obstacle-removal-to-reach-corner.java └── README.md ├── 2394-count-subarrays-with-score-less-than-k ├── 2394-count-subarrays-with-score-less-than-k.java └── README.md ├── 2409-number-of-increasing-paths-in-a-grid ├── 2409-number-of-increasing-paths-in-a-grid.java └── README.md ├── 2411-spiral-matrix-iv ├── 2411-spiral-matrix-iv.java └── README.md ├── 2414-move-pieces-to-obtain-a-string └── README.md ├── 2415-count-the-number-of-ideal-arrays ├── 2415-count-the-number-of-ideal-arrays.java └── README.md ├── 2428-equal-row-and-column-pairs ├── 2428-equal-row-and-column-pairs.java └── README.md ├── 2434-design-a-number-container-system ├── 2434-design-a-number-container-system.java └── README.md ├── 2438-find-closest-node-to-given-two-nodes ├── 2438-find-closest-node-to-given-two-nodes.java └── README.md ├── 2442-number-of-arithmetic-triplets ├── 2442-number-of-arithmetic-triplets.java └── README.md ├── 2443-check-if-there-is-a-valid-partition-for-the-array ├── 2443-check-if-there-is-a-valid-partition-for-the-array.java └── README.md ├── 2448-count-number-of-bad-pairs ├── 2448-count-number-of-bad-pairs.java └── README.md ├── 2450-minimum-replacements-to-sort-the-array ├── 2450-minimum-replacements-to-sort-the-array.java └── README.md ├── 2456-construct-smallest-number-from-di-string ├── 2456-construct-smallest-number-from-di-string.java └── README.md ├── 2463-minimum-recolors-to-get-k-consecutive-black-blocks ├── 2463-minimum-recolors-to-get-k-consecutive-black-blocks.java └── README.md ├── 2465-shifting-letters-ii ├── 2465-shifting-letters-ii.java └── README.md ├── 2472-build-a-matrix-with-conditions ├── 2472-build-a-matrix-with-conditions.java └── README.md ├── 2473-max-sum-of-a-pair-with-equal-sum-of-digits ├── 2473-max-sum-of-a-pair-with-equal-sum-of-digits.java └── README.md ├── 2478-longest-nice-subarray ├── 2478-longest-nice-subarray.java └── README.md ├── 2488-divide-intervals-into-minimum-number-of-groups ├── 2488-divide-intervals-into-minimum-number-of-groups.java └── README.md ├── 2493-reverse-odd-levels-of-binary-tree ├── 2493-reverse-odd-levels-of-binary-tree.java └── README.md ├── 2494-sum-of-prefix-scores-of-strings └── README.md ├── 2502-sort-the-people ├── 2502-sort-the-people.java └── README.md ├── 2503-longest-subarray-with-maximum-bitwise-and ├── 2503-longest-subarray-with-maximum-bitwise-and.java └── README.md ├── 2509-minimize-xor ├── 2509-minimize-xor.java └── README.md ├── 2520-using-a-robot-to-print-the-lexicographically-smallest-string ├── 2520-using-a-robot-to-print-the-lexicographically-smallest-string.java └── README.md ├── 2527-count-subarrays-with-fixed-bounds ├── 2527-count-subarrays-with-fixed-bounds.java └── README.md ├── 2533-bitwise-xor-of-all-pairings ├── 2533-bitwise-xor-of-all-pairings.java └── README.md ├── 2538-minimum-cost-to-make-array-equal ├── 2538-minimum-cost-to-make-array-equal.java └── README.md ├── 2545-height-of-binary-tree-after-subtree-removal-queries ├── 2545-height-of-binary-tree-after-subtree-removal-queries.java └── README.md ├── 2551-apply-operations-to-an-array ├── 2551-apply-operations-to-an-array.java └── README.md ├── 2552-maximum-sum-of-distinct-subarrays-with-length-k ├── 2552-maximum-sum-of-distinct-subarrays-with-length-k.java └── README.md ├── 2554-minimum-total-distance-traveled ├── 2554-minimum-total-distance-traveled.java └── README.md ├── 2558-minimum-number-of-operations-to-sort-a-binary-tree-by-level ├── 2558-minimum-number-of-operations-to-sort-a-binary-tree-by-level.java └── README.md ├── 2562-count-ways-to-build-good-strings ├── 2562-count-ways-to-build-good-strings.java └── README.md ├── 2564-most-profitable-path-in-a-tree ├── 2564-most-profitable-path-in-a-tree.java └── README.md ├── 2580-circular-sentence └── README.md ├── 2581-divide-players-into-teams-of-equal-skill ├── 2581-divide-players-into-teams-of-equal-skill.java └── README.md ├── 2583-divide-nodes-into-the-maximum-number-of-groups ├── 2583-divide-nodes-into-the-maximum-number-of-groups.java └── README.md ├── 2586-longest-square-streak-in-an-array ├── 2586-longest-square-streak-in-an-array.java └── README.md ├── 2588-maximum-number-of-points-from-grid-queries ├── 2588-maximum-number-of-points-from-grid-queries.java └── README.md ├── 2599-take-k-of-each-character-from-left-and-right └── README.md ├── 26-remove-duplicates-from-sorted-array ├── 26-remove-duplicates-from-sorted-array.java ├── NOTES.md └── README.md ├── 2610-closest-prime-numbers-in-range ├── 2610-closest-prime-numbers-in-range.java └── README.md ├── 2616-maximal-score-after-applying-k-operations ├── 2616-maximal-score-after-applying-k-operations.java └── README.md ├── 2626-count-the-number-of-good-subarrays ├── 2626-count-the-number-of-good-subarrays.java └── README.md ├── 2640-maximum-number-of-integers-to-choose-from-a-range-i ├── 2640-maximum-number-of-integers-to-choose-from-a-range-i.java └── README.md ├── 2645-pass-the-pillow ├── 2645-pass-the-pillow.java └── README.md ├── 2646-kth-largest-sum-in-a-binary-tree ├── 2646-kth-largest-sum-in-a-binary-tree.java └── README.md ├── 2649-count-total-number-of-colored-cells ├── 2649-count-total-number-of-colored-cells.java └── README.md ├── 2654-count-the-number-of-vowel-strings-in-range ├── 2654-count-the-number-of-vowel-strings-in-range.java └── README.md ├── 2665-minimum-time-to-repair-cars ├── 2665-minimum-time-to-repair-cars.java └── README.md ├── 2673-maximize-win-from-two-segments ├── 2673-maximize-win-from-two-segments.java └── README.md ├── 2677-cousins-in-binary-tree-ii ├── 2677-cousins-in-binary-tree-ii.java └── README.md ├── 2681-put-marbles-in-bags ├── 2681-put-marbles-in-bags.java └── README.md ├── 2685-first-completely-painted-row-or-column ├── 2685-first-completely-painted-row-or-column.java └── README.md ├── 2690-house-robber-iv └── README.md ├── 2691-count-vowel-strings-in-ranges ├── 2691-count-vowel-strings-in-ranges.java └── README.md ├── 2692-take-gifts-from-the-richest-pile ├── 2692-take-gifts-from-the-richest-pile.java └── README.md ├── 2695-find-score-of-an-array-after-marking-all-elements ├── 2695-find-score-of-an-array-after-marking-all-elements.java └── README.md ├── 2699-count-the-number-of-fair-pairs ├── 2699-count-the-number-of-fair-pairs.java └── README.md ├── 2707-merge-two-2d-arrays-by-summing-values ├── 2707-merge-two-2d-arrays-by-summing-values.java └── README.md ├── 2711-minimum-time-to-visit-a-cell-in-a-grid ├── 2711-minimum-time-to-visit-a-cell-in-a-grid.java └── README.md ├── 2716-prime-subtraction-operation ├── 2716-prime-subtraction-operation.java └── README.md ├── 2720-minimize-the-maximum-difference-of-pairs ├── 2720-minimize-the-maximum-difference-of-pairs.java └── README.md ├── 2727-number-of-senior-citizens ├── 2727-number-of-senior-citizens.java └── README.md ├── 2728-sum-in-a-matrix ├── 2728-sum-in-a-matrix.java └── README.md ├── 2755-extra-characters-in-a-string ├── 2755-extra-characters-in-a-string.java └── README.md ├── 2764-maximum-number-of-fish-in-a-grid ├── 2764-maximum-number-of-fish-in-a-grid.java └── README.md ├── 2766-find-the-prefix-common-array-of-two-arrays ├── 2766-find-the-prefix-common-array-of-two-arrays.java └── README.md ├── 2785-semi-ordered-permutation ├── 2785-semi-ordered-permutation.java └── README.md ├── 2786-find-the-longest-semi-repetitive-substring └── README.md ├── 2792-neighboring-bitwise-xor ├── 2792-neighboring-bitwise-xor.java └── README.md ├── 2793-count-the-number-of-complete-components ├── 2793-count-the-number-of-complete-components.java └── README.md ├── 2794-maximum-number-of-moves-in-a-grid ├── 2794-maximum-number-of-moves-in-a-grid.java └── README.md ├── 2800-minimum-string-length-after-removing-substrings ├── 2800-minimum-string-length-after-removing-substrings.java └── README.md ├── 2803-modify-graph-edge-weights ├── 2803-modify-graph-edge-weights.java └── README.md ├── 2811-determine-the-minimum-sum-of-a-k-avoiding-array ├── 2811-determine-the-minimum-sum-of-a-k-avoiding-array.java └── README.md ├── 2812-find-the-maximum-achievable-number ├── 2812-find-the-maximum-achievable-number.java └── README.md ├── 2825-minimize-string-length ├── 2825-minimize-string-length.java └── README.md ├── 2831-number-of-beautiful-pairs ├── 2831-number-of-beautiful-pairs.java └── README.md ├── 2832-find-the-longest-equal-subarray ├── 2832-find-the-longest-equal-subarray.java └── README.md ├── 2836-neither-minimum-nor-maximum ├── 2836-neither-minimum-nor-maximum.java └── README.md ├── 2838-sum-of-matrix-after-queries ├── 2838-sum-of-matrix-after-queries.java └── README.md ├── 2845-find-the-value-of-the-partition ├── 2845-find-the-value-of-the-partition.java └── README.md ├── 2846-robot-collisions ├── 2846-robot-collisions.java └── README.md ├── 2847-find-maximum-number-of-string-pairs ├── 2847-find-maximum-number-of-string-pairs.java └── README.md ├── 2850-construct-the-longest-new-string ├── 2850-construct-the-longest-new-string.java └── README.md ├── 2856-count-complete-subarrays-in-an-array ├── 2856-count-complete-subarrays-in-an-array.java └── README.md ├── 2857-total-distance-traveled ├── 2857-total-distance-traveled.java └── README.md ├── 2867-ways-to-split-array-into-good-subarrays ├── 2867-ways-to-split-array-into-good-subarrays.java └── README.md ├── 2868-continuous-subarrays ├── 2868-continuous-subarrays.java └── README.md ├── 2871-double-a-number-represented-as-a-linked-list ├── 2871-double-a-number-represented-as-a-linked-list.java └── README.md ├── 2872-largest-element-in-an-array-after-merge-operations ├── 2872-largest-element-in-an-array-after-merge-operations.java └── README.md ├── 2876-number-of-employees-who-met-the-target ├── 2876-number-of-employees-who-met-the-target.java └── README.md ├── 2877-shortest-string-that-contains-three-strings ├── 2877-shortest-string-that-contains-three-strings.java └── README.md ├── 2881-split-strings-by-separator ├── 2881-split-strings-by-separator.java └── README.md ├── 2886-faulty-keyboard ├── 2886-faulty-keyboard.java └── README.md ├── 2888-minimum-index-of-a-valid-split ├── 2888-minimum-index-of-a-valid-split.java └── README.md ├── 2891-maximum-beauty-of-an-array-after-applying-operation ├── 2891-maximum-beauty-of-an-array-after-applying-operation.java └── README.md ├── 2902-max-pair-sum-in-an-array ├── 2902-max-pair-sum-in-an-array.java └── README.md ├── 2903-insert-greatest-common-divisors-in-linked-list ├── 2903-insert-greatest-common-divisors-in-linked-list.java └── README.md ├── 2904-sorting-three-groups ├── 2904-sorting-three-groups.java └── README.md ├── 2915-count-of-interesting-subarrays ├── 2915-count-of-interesting-subarrays.java └── README.md ├── 2916-check-if-it-is-possible-to-split-array ├── 2916-check-if-it-is-possible-to-split-array.java └── README.md ├── 2917-count-pairs-whose-sum-is-less-than-target ├── 2917-count-pairs-whose-sum-is-less-than-target.java └── README.md ├── 2920-minimum-seconds-to-equalize-a-circular-array ├── 2920-minimum-seconds-to-equalize-a-circular-array.java └── README.md ├── 2955-account-balance-after-rounded-purchase ├── 2955-account-balance-after-rounded-purchase.java └── README.md ├── 2998-count-symmetric-integers ├── 2998-count-symmetric-integers.java └── README.md ├── 3-longest-substring-without-repeating-characters ├── 3-longest-substring-without-repeating-characters.java ├── NOTES.md └── README.md ├── 3000-minimum-absolute-difference-between-elements-with-constraint ├── 3000-minimum-absolute-difference-between-elements-with-constraint.java └── README.md ├── 3001-apply-operations-to-maximize-score ├── 3001-apply-operations-to-maximize-score.java └── README.md ├── 3018-make-string-a-subsequence-using-cyclic-increments ├── 3018-make-string-a-subsequence-using-cyclic-increments.java └── README.md ├── 3019-furthest-point-from-origin ├── 3019-furthest-point-from-origin.java └── README.md ├── 3026-find-the-minimum-possible-sum-of-a-beautiful-array ├── 3026-find-the-minimum-possible-sum-of-a-beautiful-array.java └── README.md ├── 3046-minimum-operations-to-make-a-special-number ├── 3046-minimum-operations-to-make-a-special-number.java └── README.md ├── 3142-longest-unequal-adjacent-groups-subsequence-ii ├── 3142-longest-unequal-adjacent-groups-subsequence-ii.java └── README.md ├── 3143-longest-unequal-adjacent-groups-subsequence-i ├── 3143-longest-unequal-adjacent-groups-subsequence-i.java └── README.md ├── 3152-maximum-value-of-an-ordered-triplet-ii ├── 3152-maximum-value-of-an-ordered-triplet-ii.java └── README.md ├── 3154-maximum-value-of-an-ordered-triplet-i ├── 3154-maximum-value-of-an-ordered-triplet-i.java └── README.md ├── 3171-minimum-equal-sum-of-two-arrays-after-replacing-zeros ├── 3171-minimum-equal-sum-of-two-arrays-after-replacing-zeros.java └── README.md ├── 3172-divisible-and-non-divisible-sums-difference ├── 3172-divisible-and-non-divisible-sums-difference.java └── README.md ├── 3174-minimum-number-of-changes-to-make-binary-string-beautiful └── README.md ├── 3181-find-building-where-alice-and-bob-can-meet └── README.md ├── 3189-find-champion-ii ├── 3189-find-champion-ii.java └── README.md ├── 3194-find-words-containing-character ├── 3194-find-words-containing-character.java └── README.md ├── 3195-separate-black-and-white-balls ├── 3195-separate-black-and-white-balls.java └── README.md ├── 3213-count-subarrays-where-max-element-appears-at-least-k-times ├── 3213-count-subarrays-where-max-element-appears-at-least-k-times.java └── README.md ├── 3219-make-lexicographically-smallest-array-by-swapping-elements ├── 3219-make-lexicographically-smallest-array-by-swapping-elements.java └── README.md ├── 3227-find-missing-and-repeated-values ├── 3227-find-missing-and-repeated-values.java └── README.md ├── 3235-minimum-cost-to-convert-string-i ├── 3235-minimum-cost-to-convert-string-i.java └── README.md ├── 3243-count-the-number-of-powerful-integers ├── 3243-count-the-number-of-powerful-integers.java └── README.md ├── 3267-find-longest-special-substring-that-occurs-thrice-i ├── 3267-find-longest-special-substring-that-occurs-thrice-i.java └── README.md ├── 3276-minimum-number-of-pushes-to-type-word-ii ├── 3276-minimum-number-of-pushes-to-type-word-ii.java └── README.md ├── 3291-find-if-array-can-be-sorted ├── 3291-find-if-array-can-be-sorted.java └── README.md ├── 3307-find-the-maximum-sum-of-node-values ├── 3307-find-the-maximum-sum-of-node-values.java └── README.md ├── 3309-count-prefix-and-suffix-pairs-i ├── 3309-count-prefix-and-suffix-pairs-i.java └── README.md ├── 3321-type-of-triangle ├── 3321-type-of-triangle.java └── README.md ├── 3329-find-the-length-of-the-longest-common-prefix ├── 3329-find-the-length-of-the-longest-common-prefix.java └── README.md ├── 3332-minimum-operations-to-exceed-threshold-value-ii ├── 3332-minimum-operations-to-exceed-threshold-value-ii.java └── README.md ├── 3348-minimum-cost-walk-in-weighted-graph ├── 3348-minimum-cost-walk-in-weighted-graph.java └── README.md ├── 3372-longest-strictly-increasing-or-strictly-decreasing-subarray ├── 3372-longest-strictly-increasing-or-strictly-decreasing-subarray.java └── README.md ├── 3379-score-of-a-string ├── 3379-score-of-a-string.java └── README.md ├── 3380-shortest-subarray-with-or-at-least-k-ii ├── 3380-shortest-subarray-with-or-at-least-k-ii.java └── README.md ├── 3394-minimum-array-end ├── 3394-minimum-array-end.java └── README.md ├── 3427-special-array-ii ├── 3427-special-array-ii.java └── README.md ├── 3429-special-array-i ├── 3429-special-array-i.java └── README.md ├── 3430-count-days-without-meetings ├── 3430-count-days-without-meetings.java └── README.md ├── 3434-find-the-number-of-distinct-colors-among-the-balls ├── 3434-find-the-number-of-distinct-colors-among-the-balls.java └── README.md ├── 3439-find-minimum-diameter-after-merging-two-trees ├── 3439-find-minimum-diameter-after-merging-two-trees.java └── README.md ├── 3445-lexicographically-minimum-string-after-removing-stars ├── 3445-lexicographically-minimum-string-after-removing-stars.java └── README.md ├── 3447-clear-digits └── README.md ├── 3451-string-compression-iii ├── 3451-string-compression-iii.java └── README.md ├── 3455-minimum-length-of-string-after-operations ├── 3455-minimum-length-of-string-after-operations.java └── README.md ├── 3475-minimum-operations-to-make-binary-array-elements-equal-to-one-i ├── 3475-minimum-operations-to-make-binary-array-elements-equal-to-one-i.java └── README.md ├── 3483-alternating-groups-ii ├── 3483-alternating-groups-ii.java └── README.md ├── 3501-delete-nodes-from-linked-list-present-in-array ├── 3501-delete-nodes-from-linked-list-present-in-array.java └── README.md ├── 3517-shortest-distance-after-road-addition-queries-i ├── 3517-shortest-distance-after-road-addition-queries-i.java └── README.md ├── 3522-find-the-power-of-k-size-subarrays-i ├── 3522-find-the-power-of-k-size-subarrays-i.java └── README.md ├── 3548-find-the-count-of-good-integers ├── 3548-find-the-count-of-good-integers.java └── README.md ├── 3555-final-array-state-after-k-multiplication-operations-i ├── 3555-final-array-state-after-k-multiplication-operations-i.java └── README.md ├── 3569-count-of-substrings-containing-every-vowel-and-k-consonants-ii ├── 3569-count-of-substrings-containing-every-vowel-and-k-consonants-ii.java └── README.md ├── 3621-minimum-operations-to-make-array-values-equal-to-k ├── 3621-minimum-operations-to-make-array-values-equal-to-k.java └── README.md ├── 3627-find-minimum-time-to-reach-last-room-i ├── 3627-find-minimum-time-to-reach-last-room-i.java └── README.md ├── 3628-find-minimum-time-to-reach-last-room-ii ├── 3628-find-minimum-time-to-reach-last-room-ii.java └── README.md ├── 3629-total-characters-in-string-after-transformations-i ├── 3629-total-characters-in-string-after-transformations-i.java └── README.md ├── 3630-total-characters-in-string-after-transformations-ii ├── 3630-total-characters-in-string-after-transformations-ii.java └── README.md ├── 3633-maximize-the-number-of-target-nodes-after-connecting-trees-i ├── 3633-maximize-the-number-of-target-nodes-after-connecting-trees-i.java └── README.md ├── 3637-count-number-of-balanced-permutations ├── 3637-count-number-of-balanced-permutations.java └── README.md ├── 3639-zero-array-transformation-i ├── 3639-zero-array-transformation-i.java └── README.md ├── 3645-maximize-the-number-of-target-nodes-after-connecting-trees-ii ├── 3645-maximize-the-number-of-target-nodes-after-connecting-trees-ii.java └── README.md ├── 3647-zero-array-transformation-iii ├── 3647-zero-array-transformation-iii.java └── README.md ├── 3656-minimum-number-of-operations-to-make-elements-in-array-distinct ├── 3656-minimum-number-of-operations-to-make-elements-in-array-distinct.java └── README.md ├── 3657-check-if-grid-can-be-cut-into-sections ├── 3657-check-if-grid-can-be-cut-into-sections.java └── README.md ├── 3683-find-the-lexicographically-largest-string-from-the-box-i ├── 3683-find-the-lexicographically-largest-string-from-the-box-i.java └── README.md ├── 3685-count-subarrays-of-length-three-with-a-condition ├── 3685-count-subarrays-of-length-three-with-a-condition.java └── README.md ├── 4-median-of-two-sorted-arrays ├── 4-median-of-two-sorted-arrays.java ├── NOTES.md └── README.md ├── 9-palindrome-number ├── 9-palindrome-number.java └── NOTES.md ├── Bottom View of Binary Tree - GFG ├── README.md └── bottom-view-of-binary-tree.java ├── LICENSE ├── Largest subarray with 0 sum - GFG ├── README.md └── largest-subarray-with-0-sum.java ├── Left View of Binary Tree - GFG ├── README.md └── left-view-of-binary-tree.java ├── Minimum Platforms - GFG ├── README.md └── minimum-platforms.java ├── Power Set - GFG ├── README.md └── power-set.java ├── README.md └── stats.json /0001-two-sum/0001-two-sum.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] twoSum(int[] nums, int target) { 3 | int[] res = new int[2]; 4 | Map map = new HashMap<>(); 5 | for (int i=0; i map = new HashMap<>(); 4 | int result = 0; 5 | int left = 0; 6 | for(int right = 0; right < s.length(); right++) { 7 | if (map.containsKey(s.charAt(right))) { 8 | left = Math.max(left, map.get(s.charAt(right))); 9 | } 10 | result = Math.max(result, right - left + 1); 11 | map.put(s.charAt(right), right + 1); 12 | } 13 | return result; 14 | } 15 | } -------------------------------------------------------------------------------- /0004-median-of-two-sorted-arrays/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0007-reverse-integer/0007-reverse-integer.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int reverse(int x) { 3 | int result = 0; 4 | while (x != 0) { 5 | int tail = x %10; 6 | int newResult = result * 10 + tail; 7 | 8 | // If overflow exists, the new result will not equal previous one. 9 | if ((newResult - tail) / 10 != result) return 0; 10 | 11 | result = newResult; 12 | x = x/10; 13 | } 14 | return result; 15 | } 16 | } -------------------------------------------------------------------------------- /0007-reverse-integer/NOTES.md: -------------------------------------------------------------------------------- 1 | ``` 2 | public int reverse(int x) { 3 | int result = 0; 4 | while (x != 0) { 5 | int tail = x % 10; 6 | int newResult = result * 10 + tail; 7 | if ((newResult - tail) / 10 != result) 8 | return 0; 9 | } 10 | result = newResult; 11 | x = x / 10; 12 | } 13 | return result; 14 | } 15 | ``` -------------------------------------------------------------------------------- /0019-remove-nth-node-from-end-of-list/0019-remove-nth-node-from-end-of-list.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public ListNode removeNthFromEnd(ListNode head, int n) { 3 | ListNode dummy = new ListNode(0); 4 | dummy.next = head; 5 | ListNode fast = dummy; 6 | ListNode slow = dummy; 7 | for (int i=1; i <= n+1; i++) { 8 | fast = fast.next; 9 | } 10 | while (fast != null) { 11 | fast = fast.next; 12 | slow = slow.next; 13 | } 14 | slow.next = slow.next.next; 15 | return dummy.next; 16 | } 17 | } -------------------------------------------------------------------------------- /0019-remove-nth-node-from-end-of-list/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0020-valid-parentheses/0020-valid-parentheses.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean isValid(String s) { 3 | Stack stack = new Stack(); 4 | for (int i=0; i stack = new Stack(); 5 | for (int i=0; i res = new ArrayList<>(); 4 | 5 | public List generateParenthesis(int n) { 6 | generate(0, 0, n); 7 | return res; 8 | } 9 | 10 | public void generate(int open, int close, int n) { 11 | if (open == close && open == n) { 12 | res.add(sb.toString()); 13 | return; 14 | } 15 | 16 | if (close < open) { 17 | sb.append(')'); 18 | generate(open, close + 1, n); 19 | sb.deleteCharAt(sb.length() - 1); 20 | } 21 | 22 | if (open < n) { 23 | sb.append('('); 24 | generate(open + 1, close, n); 25 | sb.deleteCharAt(sb.length() - 1); 26 | } 27 | } 28 | } -------------------------------------------------------------------------------- /0022-generate-parentheses/README.md: -------------------------------------------------------------------------------- 1 |

22. Generate Parentheses

Medium


Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.

2 | 3 |

 

4 |

Example 1:

5 |
Input: n = 3
 6 | Output: ["((()))","(()())","(())()","()(())","()()()"]
 7 | 

Example 2:

8 |
Input: n = 1
 9 | Output: ["()"]
10 | 
11 |

 

12 |

Constraints:

13 | 14 |
    15 |
  • 1 <= n <= 8
  • 16 |
17 | -------------------------------------------------------------------------------- /0026-remove-duplicates-from-sorted-array/0026-remove-duplicates-from-sorted-array.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int removeDuplicates(int[] nums) { 3 | if (nums.length == 0) return 0; 4 | int flag = 0; 5 | for (int i=1; i1) { 5 | int mid = lo + (hi-lo)/2; 6 | if (nums[mid]>=target) hi = mid; 7 | else lo = mid; 8 | } 9 | 10 | return hi; 11 | } 12 | } -------------------------------------------------------------------------------- /0042-trapping-rain-water/0042-trapping-rain-water.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int trap(int[] A) { 3 | int n = A.length; 4 | if (n == 1) return 0; 5 | 6 | int[] pf = new int[n]; int[] sf = new int[n]; 7 | 8 | int max = pf[0]; 9 | for (int i=0; i=0; i--) {max = Math.max(A[i], max); sf[i] = Math.max(sf[i], max);} 13 | 14 | int res = 0; 15 | for (int i=0; i> groupAnagrams(String[] strs) { 3 | if (strs.length == 0) return new ArrayList(); 4 | Map map = new HashMap<>(); 5 | int[] count = new int[26]; 6 | 7 | for (String s : strs) { 8 | Arrays.fill(count, 0); 9 | for (char c : s.toCharArray()) count[c - 'a']++; 10 | 11 | StringBuilder sb = new StringBuilder(""); 12 | for (int i=0; i<26; i++) { 13 | sb.append('#'); 14 | sb.append(count[i]); 15 | } 16 | 17 | String key = sb.toString(); 18 | if (!map.containsKey(key)) map.put(key, new ArrayList()); 19 | map.get(key).add(s); 20 | } 21 | 22 | return new ArrayList(map.values()); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /0050-powx-n/0050-powx-n.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public double myPow(double x, int n) { 3 | double ans = 1.0; 4 | long nn = n; 5 | if (nn<0) nn = -1 * nn; 6 | while (nn > 0) { 7 | if (nn % 2 == 1) { 8 | ans = ans * x; 9 | nn = nn - 1; 10 | } else { 11 | x = x * x; 12 | nn = nn / 2; 13 | } 14 | } 15 | if(n<0) ans = (double)(1.0) / (double)(ans); 16 | return ans; 17 | } 18 | } -------------------------------------------------------------------------------- /0053-maximum-subarray/0053-maximum-subarray.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int maxSubArray(int[] nums) { 3 | int sum = 0, maxSum = nums[0]; 4 | for (int i : nums) { 5 | sum += i; 6 | if (sum > maxSum) maxSum = sum; 7 | if (sum < 0) sum = 0; 8 | } 9 | return maxSum; 10 | } 11 | } -------------------------------------------------------------------------------- /0055-jump-game/0055-jump-game.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean canJump(int[] a) { 3 | int n = a.length; 4 | int maxReach = 0; 5 | 6 | for (int i=0; i maxReach) return false; 8 | maxReach = Math.max(maxReach, i + a[i]); 9 | } 10 | 11 | return maxReach >= n-1; 12 | } 13 | } -------------------------------------------------------------------------------- /0062-unique-paths/0062-unique-paths.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int uniquePaths(int m, int n) { 3 | int[][] dp = new int[m][n]; 4 | for (int i=m-1; i>=0; i--) { 5 | for (int j=n-1; j>=0; j--) { 6 | if (i == m-1 || j == n-1) dp[i][j] = 1; 7 | else dp[i][j] = dp[i+1][j] + dp[i][j+1]; 8 | } 9 | } 10 | return dp[0][0]; 11 | } 12 | } -------------------------------------------------------------------------------- /0064-minimum-path-sum/0064-minimum-path-sum.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int minPathSum(int[][] A) { 3 | int n = A.length, m = A[0].length; 4 | int[][] dp = new int[n][m]; 5 | 6 | for (int i=0; i= 0; i--) { 4 | if (digits[i] < 9) { 5 | digits[i]++; 6 | return digits; 7 | } 8 | digits[i] = 0; 9 | } 10 | 11 | digits = new int[digits.length + 1]; 12 | digits[0] = 1; 13 | return digits; 14 | } 15 | } -------------------------------------------------------------------------------- /0070-climbing-stairs/0070-climbing-stairs.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int climbStairs(int n) { 3 | int[] dp = new int[n+1]; 4 | 5 | dp[0] = 1; dp[1] = 1; 6 | 7 | for (int i=2; i<=n; i++) { 8 | dp[i] = dp[i-1] + dp[i-2]; 9 | } 10 | 11 | return dp[n]; 12 | } 13 | } -------------------------------------------------------------------------------- /0072-edit-distance/0072-edit-distance.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int minDistance(String S, String T) { 3 | int m = S.length(), n = T.length(); 4 | 5 | int[][] dp = new int[m+1][n+1]; 6 | for (int i=0; i<=m; i++) dp[i][0] = i; 7 | for (int j=0; j<=n; j++) dp[0][j] = j; 8 | 9 | for(int i=1; i<=m; i++) { 10 | for(int j=1; j<=n; j++) { 11 | if (S.charAt(i-1) == T.charAt(j-1)) dp[i][j] = dp[i-1][j-1]; 12 | else dp[i][j] = 1 + Math.min(dp[i-1][j-1], Math.min(dp[i][j-1], dp[i-1][j])); 13 | } 14 | } 15 | return dp[m][n]; 16 | } 17 | } -------------------------------------------------------------------------------- /0073-set-matrix-zeroes/0073-set-matrix-zeroes.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public void setZeroes(int[][] matrix) { 3 | int col0 = 1, m = matrix.length, n = matrix[0].length; 4 | 5 | for (int i = 0; i < m; i++) { 6 | if (matrix[i][0] == 0) col0 = 0; 7 | for (int j = 1; j < n; j++) 8 | if (matrix[i][j] == 0) matrix[i][0] = matrix[0][j] = 0; 9 | } 10 | 11 | for (int i = m - 1; i >= 0; i--) { 12 | for (int j = n - 1; j >= 1; j--) 13 | if (matrix[i][0] == 0 || matrix[0][j] == 0) matrix[i][j] = 0; 14 | if (col0 == 0) matrix[i][0] = 0; 15 | } 16 | } 17 | } -------------------------------------------------------------------------------- /0074-search-a-2d-matrix/0074-search-a-2d-matrix.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean searchMatrix(int[][] a, int k) { 3 | int m = a.length, n = a[0].length; 4 | int lo = 0, hi = (m * n) - 1; 5 | 6 | while (lo <= hi) { 7 | int mid = lo + (hi-lo)/2; 8 | 9 | if (a[mid / n][mid % n] == k) return true; 10 | else if (a[mid / n][mid % n] < k) lo = mid + 1; 11 | else hi = mid - 1; 12 | } 13 | return false; 14 | } 15 | } 16 | 17 | // TC : log(m*n) -------------------------------------------------------------------------------- /0075-sort-colors/0075-sort-colors.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | private static void swap(int[] A, int i, int j) { int temp = A[i]; A[i] = A[j]; A[j] = temp; } 3 | 4 | public void sortColors(int[] nums) { 5 | int low = 0, mid = 0, high = nums.length - 1; 6 | 7 | while (mid <= high) { 8 | if (nums[mid] == 0) { 9 | swap(nums, mid, low); 10 | low++; mid++; 11 | } 12 | else if (nums[mid] == 1) { 13 | mid++; 14 | } 15 | else { 16 | swap(nums, mid, high); 17 | high--; 18 | } 19 | } 20 | } 21 | } -------------------------------------------------------------------------------- /0077-combinations/0077-combinations.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public List> combine(int n, int k) { 3 | List> combs = new ArrayList<>(); 4 | backTrack(combs, new ArrayList(), 1, n, k); 5 | return combs; 6 | } 7 | public void backTrack(List> combs, List comb, int start, int n, int k) { 8 | if (k == 0){ 9 | combs.add(new ArrayList(comb)); 10 | return; 11 | } 12 | for (int i = start; i <= n - k + 1; i++) { 13 | comb.add(i); 14 | backTrack(combs, comb, i + 1, n, k-1); 15 | comb.remove(comb.size() -1); 16 | } 17 | } 18 | } -------------------------------------------------------------------------------- /0088-merge-sorted-array/0088-merge-sorted-array.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public void merge(int[] nums1, int m, int[] nums2, int n) { 3 | int first = m - 1; 4 | int second = n - 1; 5 | for (int i = m + n - 1; i >= 0; i--) { 6 | if (second < 0) { 7 | break; 8 | } 9 | if (first >= 0 && nums1[first] > nums2[second]) { 10 | nums1[i] = nums1[first]; 11 | first--; 12 | } else { 13 | nums1[i] = nums2[second]; 14 | second--; 15 | } 16 | } 17 | } 18 | } -------------------------------------------------------------------------------- /0088-merge-sorted-array/NOTES.md: -------------------------------------------------------------------------------- 1 | ``` 2 | class Solution { 3 | public void merge(int[] nums1, int m, int[] nums2, int n) { 4 | int first = m - 1; 5 | int second = n - 1; 6 | ​ 7 | for (int i = m + n - 1; i >= 0; i--) { 8 | if (second < 0) { 9 | break; 10 | } 11 | if (first >= 0 && nums1[first] > nums2[second]) { 12 | nums1[i] = nums1[first]; 13 | first--; 14 | } else { 15 | nums1[i] = nums2[second]; 16 | second--; 17 | } 18 | } 19 | } 20 | } 21 | ``` -------------------------------------------------------------------------------- /0090-subsets-ii/0090-subsets-ii.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public List> subsetsWithDup(int[] nums) { 3 | Arrays.sort(nums); 4 | List> res = new ArrayList<>(); 5 | helper(0, nums, new ArrayList(), res); 6 | return res; 7 | } 8 | 9 | public void helper(int index, int[] nums, List temp, List> res) { 10 | res.add(new ArrayList(temp)); 11 | for (int i=index; iindex && nums[i] == nums[i-1]) continue; 13 | temp.add(nums[i]); 14 | helper(i+1, nums, temp, res); 15 | temp.remove(temp.size() - 1); 16 | } 17 | } 18 | } -------------------------------------------------------------------------------- /0091-decode-ways/0091-decode-ways.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int numDecodings(String s) { 3 | int n = s.length(); 4 | int[] dp = new int[n+1]; // dp[i] -> no. of strings form with digits from index 0 to i 5 | dp[0] = 1; 6 | dp[1] = s.charAt(0) == '0' ? 0 : 1; 7 | 8 | for (int i=2; i<=n; i++) { 9 | int d = s.charAt(i - 1) - '0'; 10 | int dd = 10 * (s.charAt(i - 2) - '0') + (s.charAt(i - 1) - '0'); 11 | 12 | if (d >= 1) dp[i] += dp[i - 1]; 13 | if (dd >= 10 && dd <= 26) dp[i] += dp[i - 2]; 14 | } 15 | return dp[n]; 16 | } 17 | } 18 | 19 | // https://youtu.be/np1tSnaYG10 -------------------------------------------------------------------------------- /0094-binary-tree-inorder-traversal/0094-binary-tree-inorder-traversal.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public List inorderTraversal(TreeNode root) { 3 | List res = new ArrayList(); 4 | helper(root, res); 5 | return res; 6 | } 7 | 8 | public void helper(TreeNode root, List res) { 9 | if (root != null) { 10 | helper(root.left, res); 11 | res.add(root.val); 12 | helper(root.right, res); 13 | } 14 | } 15 | } -------------------------------------------------------------------------------- /0110-balanced-binary-tree/0110-balanced-binary-tree.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean isBalanced(TreeNode root) { 3 | if (root == null) return true; 4 | int left = helper(root.left); 5 | int right = helper(root.right); 6 | 7 | return Math.abs(left - right) <= 1 && isBalanced(root.left) && isBalanced(root.right); 8 | } 9 | 10 | public int helper (TreeNode root) { 11 | if (root == null) return 0; 12 | return Math.max(helper(root.left), helper(root.right)) + 1; 13 | } 14 | } -------------------------------------------------------------------------------- /0118-pascals-triangle/0118-pascals-triangle.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public List> generate(int n) { 3 | List> pascal = new ArrayList<>(); 4 | List first = new ArrayList<>(); 5 | 6 | int[][] dp = new int[n][n + 1]; 7 | dp[0][1]=1; first.add(dp[0][1]); 8 | pascal.add(first); 9 | 10 | for(int i=1; i internal = new ArrayList<>(); 12 | for(int j=1; j<=i+1; j++) { 13 | dp[i][j] = dp[i-1][j-1] + dp[i-1][j]; 14 | internal.add(dp[i][j]); 15 | } 16 | pascal.add(internal); 17 | } 18 | 19 | return pascal; 20 | } 21 | } -------------------------------------------------------------------------------- /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 minPrice = Integer.MAX_VALUE; 4 | int maxProfit = 0; 5 | 6 | for (int i : prices) { 7 | if (i < minPrice) minPrice = i; 8 | if (i - minPrice > maxProfit) maxProfit = i - minPrice; 9 | } 10 | 11 | return maxProfit; 12 | } 13 | } -------------------------------------------------------------------------------- /0122-best-time-to-buy-and-sell-stock-ii/0122-best-time-to-buy-and-sell-stock-ii.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int maxProfit(int[] a) { 3 | int ans = 0; 4 | for (int i=1; ia[i-1]) ans+= (a[i]-a[i-1]); 6 | } 7 | return ans; 8 | } 9 | } 10 | 11 | // more = 1 12 | // less = 5 13 | 14 | // [7,1,5,3,6,4] 15 | 16 | // 4 + 3 = 7 -------------------------------------------------------------------------------- /0125-valid-palindrome/0125-valid-palindrome.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean isPalindrome(String s) { 3 | StringBuilder sb = new StringBuilder(""), rev = new StringBuilder(""); 4 | 5 | for (int i=0; i set = new HashSet(); 6 | for (int i=0; i postorderTraversal(TreeNode root) { 3 | List res = new ArrayList(); 4 | helper(root, res); 5 | return res; 6 | } 7 | 8 | public void helper(TreeNode root, List res) { 9 | if (root != null) { 10 | helper(root.left, res); 11 | helper(root.right, res); 12 | res.add(root.val); 13 | } 14 | } 15 | } -------------------------------------------------------------------------------- /0151-reverse-words-in-a-string/0151-reverse-words-in-a-string.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public String reverseWords(String s) { 3 | String[] words = s.split("\\s+"); 4 | String res = ""; 5 | 6 | for (int i = words.length - 1; i>=0; i--) { 7 | res += words[i] + " "; 8 | } 9 | return res.trim(); 10 | } 11 | } 12 | 13 | // class Solution { 14 | // public String reverseWords(String s) { 15 | // StringBuilder sb = new StringBuilder(); 16 | // String[] words = s.split(" "); 17 | 18 | // for (int i = words.length - 1; i>=0; i--) { 19 | // if(words[i].length()>0) { 20 | // sb.append(words[i]); 21 | // sb.append(" "); 22 | // } 23 | // } 24 | // return sb.toString().trim(); 25 | // } 26 | // } -------------------------------------------------------------------------------- /0153-find-minimum-in-rotated-sorted-array/0153-find-minimum-in-rotated-sorted-array.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int findMin(int[] a) { 3 | if (a.length == 1 || a[0] < a[a.length-1]) return a[0]; 4 | 5 | int lo = 0, hi = a.length-1; 6 | while (hi-lo>1) { 7 | int mid = lo + (hi-lo)/2; 8 | if (a[mid] > a[hi]) lo = mid; 9 | else hi = mid; 10 | } 11 | return a[hi]; 12 | } 13 | } -------------------------------------------------------------------------------- /0160-intersection-of-two-linked-lists/0160-intersection-of-two-linked-lists.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public ListNode getIntersectionNode(ListNode headA, ListNode headB) { 3 | if (headA == null || headB == null) return null; 4 | ListNode dummyA = headA, dummyB = headB; 5 | 6 | while (dummyA != dummyB) { 7 | dummyA = dummyA == null ? headB : dummyA.next; 8 | dummyB = dummyB == null ? headA : dummyB.next; 9 | } 10 | return dummyA; 11 | } 12 | } -------------------------------------------------------------------------------- /0162-find-peak-element/0162-find-peak-element.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int findPeakElement(int[] A) { 3 | int n = A.length; 4 | // Edge Cases 5 | if (n == 1) return 0; 6 | if (A[0] >= A[1]) return 0; 7 | if (A[n - 1] >= A[n - 2]) return n - 1; 8 | 9 | int lo = 1, hi = n - 2; 10 | while (lo <= hi) { 11 | int mid = lo + (hi-lo)/2; 12 | if (A[mid] >= A[mid - 1] && A[mid] >= A[mid + 1]) return mid; 13 | else if (A[mid] < A[mid - 1]) hi = mid - 1; 14 | else lo = mid + 1; 15 | } 16 | return -1; 17 | } 18 | } -------------------------------------------------------------------------------- /0198-house-robber/0198-house-robber.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int rob(int[] A) { 3 | int n = A.length; 4 | if (n == 1) return A[0]; 5 | int[] dp = new int[n]; 6 | 7 | dp[0] = A[0]; dp[1] = Math.max(A[0], A[1]); 8 | 9 | for (int i=2; i= 0; i--) { 5 | if (s.charAt(i) == s.charAt(j)) { j += 1; } 6 | } 7 | if (j == s.length()) { return s; } 8 | String suffix = s.substring(j); 9 | return new StringBuffer(suffix).reverse().toString() + shortestPalindrome(s.substring(0, j)) + suffix; 10 | } 11 | } -------------------------------------------------------------------------------- /0217-contains-duplicate/0217-contains-duplicate.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean containsDuplicate(int[] nums) { 3 | HashSet set = new HashSet(); 4 | for (int i=0; i summaryRanges(int[] nums) { 3 | ArrayList res = new ArrayList<>(); 4 | 5 | for(int i=0; i"+nums[i]); 10 | else res.add(""+start); 11 | } 12 | return res; 13 | } 14 | } -------------------------------------------------------------------------------- /0234-palindrome-linked-list/0234-palindrome-linked-list.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean isPalindrome(ListNode head) { 3 | Stack st = new Stack(); 4 | ListNode dummy = head; 5 | while (dummy != null) { 6 | st.push(dummy.val); 7 | dummy = dummy.next; 8 | } 9 | dummy = head; 10 | while (dummy != null) { 11 | if (dummy.val != st.pop()) return false; 12 | dummy = dummy.next; 13 | } 14 | return true; 15 | } 16 | } -------------------------------------------------------------------------------- /0234-palindrome-linked-list/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0239-sliding-window-maximum/0239-sliding-window-maximum.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] maxSlidingWindow(int[] nums, int k) { 3 | if (nums == null || nums.length == 0 || k <= 0) return new int[0]; 4 | int[] result = new int[nums.length - k + 1]; 5 | Deque deque = new ArrayDeque<>(); 6 | for (int i = 0; i < nums.length; i++) { 7 | while (!deque.isEmpty() && deque.peekFirst() < i - k + 1) deque.pollFirst(); 8 | while (!deque.isEmpty() && nums[deque.peekLast()] < nums[i]) deque.pollLast(); 9 | deque.offerLast(i); 10 | if (i >= k - 1) result[i - k + 1] = nums[deque.peekFirst()]; 11 | } 12 | return result; 13 | } 14 | } -------------------------------------------------------------------------------- /0240-search-a-2d-matrix-ii/0240-search-a-2d-matrix-ii.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public boolean searchMatrix(int[][] a, int k) { 3 | if(a == null || a.length < 1 || a[0].length <1) return false; 4 | 5 | int m = 0, n = a[0].length-1; 6 | 7 | while(n >= 0 && m <= a.length-1) { 8 | if(k == a[m][n]) return true; 9 | else if(k > a[m][n]) m++; 10 | else if(k < a[m][n]) n--; 11 | } 12 | return false; 13 | } 14 | } -------------------------------------------------------------------------------- /0242-valid-anagram/0242-valid-anagram.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean isAnagram(String s, String t) { 3 | if (s.length() != t.length()) return false; 4 | 5 | int[] counter = new int[26]; 6 | for (int i=0; i A[j]) dp[i] = Math.max(dp[i], 1+dp[j]); 10 | } 11 | } 12 | 13 | int ans = 0; 14 | for (int i : dp) ans = Math.max(ans, i); 15 | 16 | return ans; 17 | } 18 | } -------------------------------------------------------------------------------- /0330-patching-array/0330-patching-array.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int minPatches(int[] nums, int n) { 3 | long miss = 1, added = 0; 4 | int i = 0; 5 | while (miss <= n) { 6 | if (i < nums.length && nums[i] <= miss) miss += nums[i++]; 7 | else { 8 | miss += miss; 9 | added++; 10 | } 11 | } 12 | return (int)added; 13 | } 14 | } -------------------------------------------------------------------------------- /0338-counting-bits/0338-counting-bits.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] countBits(int n) { 3 | int[] dp = new int[n+1]; 4 | dp[0] = 0; 5 | 6 | for (int i=1; i<=n; i++) { 7 | dp[i] = dp[i/2] + i%2; 8 | // or, dp[i] = dp[i >> 1] + (i % 2); 9 | } 10 | return dp; 11 | } 12 | } -------------------------------------------------------------------------------- /0344-reverse-string/0344-reverse-string.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public void reverseString(char[] s) { 3 | int left = 0, right = s.length - 1; 4 | while (left < right) { 5 | char temp = s[left]; 6 | s[left++] = s[right]; 7 | s[right--] = temp; 8 | } 9 | } 10 | } -------------------------------------------------------------------------------- /0347-top-k-frequent-elements/0347-top-k-frequent-elements.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] topKFrequent(int[] nums, int k) { 3 | Map map = new HashMap<>(); 4 | for (int i: nums) map.put(i, map.getOrDefault(i, 0) + 1); 5 | 6 | PriorityQueue> minHeap = new PriorityQueue<>((a, b) -> a.getValue() - b.getValue()); 7 | for (Map.Entry e : map.entrySet()) { 8 | minHeap.offer(e); 9 | if (minHeap.size() > k) minHeap.poll(); 10 | } 11 | 12 | int[] ans = new int[k]; 13 | for (int i=0; i map = new HashMap<>(); 4 | ArrayList res = new ArrayList<>(); 5 | for (int i=0; i0) { 10 | map.put(nums2[i], map.get(nums2[i]) - 1); 11 | res.add(nums2[i]); 12 | } 13 | } 14 | int[] a = new int[res.size()]; 15 | for (int i=0; i lexicalOrder(int n) { 3 | List res = new ArrayList<>(); 4 | for (int i=1; i<10; ++i){ 5 | dfs(i, n, res); 6 | } 7 | return res; 8 | } 9 | 10 | public void dfs(int cur, int n, List res){ 11 | if (cur > n) return; 12 | else { 13 | res.add(cur); 14 | for (int i=0; i<10; ++i){ 15 | if(10*cur+i>n) return; 16 | dfs(10*cur+i, n, res); 17 | } 18 | } 19 | } 20 | } -------------------------------------------------------------------------------- /0440-k-th-smallest-in-lexicographical-order/0440-k-th-smallest-in-lexicographical-order.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | private int getReqNum(long a, long b, long n) { 3 | int gap = 0; 4 | while (a <= n) { 5 | gap += Math.min(n + 1, b) - a; 6 | a *= 10; 7 | b *= 10; 8 | } 9 | return gap; 10 | } 11 | 12 | public int findKthNumber(int n, int k) { 13 | long num = 1; 14 | for (int i = 1; i < k;) { 15 | int req = getReqNum(num, num + 1, n); 16 | if (i + req <= k) { 17 | i += req; 18 | num++; 19 | } else { 20 | i++; 21 | num *= 10; 22 | } 23 | } 24 | return (int) num; 25 | } 26 | } -------------------------------------------------------------------------------- /0451-sort-characters-by-frequency/0451-sort-characters-by-frequency.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public String frequencySort(String s) { 3 | HashMap map = new HashMap<>(); 4 | for (char ch : s.toCharArray()) { 5 | map.put(ch, map.getOrDefault(ch, 0) + 1); 6 | } 7 | 8 | ArrayList list = new ArrayList<>(map.keySet()); 9 | Collections.sort(list, (a, b) -> map.get(b) - map.get(a)); 10 | 11 | StringBuilder sb = new StringBuilder(); 12 | for (char ch : list) { 13 | for (int i=0; i= 0; 23 | } 24 | } -------------------------------------------------------------------------------- /0516-longest-palindromic-subsequence/0516-longest-palindromic-subsequence.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int longestPalindromeSubseq(String s) { 3 | int n = s.length(); 4 | int[][] dp = new int[n][n]; // dp[i][j] -> LCS in string from i to j; 5 | 6 | for (int i=0; i map = new HashMap<>(){{put(0,-1);}};; 4 | int runningSum = 0; 5 | for (int i=0;i 1) return true; 11 | } 12 | else map.put(runningSum, i); 13 | } 14 | return false; 15 | } 16 | } -------------------------------------------------------------------------------- /0540-single-element-in-a-sorted-array/0540-single-element-in-a-sorted-array.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int singleNonDuplicate(int[] nums) { 3 | int lo = 0, hi = nums.length-1; 4 | while(lo < hi){ 5 | int mid = (lo + hi)/2; 6 | if( (mid % 2 == 0 && nums[mid] == nums[mid +1]) || (mid %2 == 1 && nums[mid] == nums[mid - 1]) ) 7 | lo = mid + 1; 8 | else 9 | hi = mid; 10 | } 11 | return nums[lo]; 12 | } 13 | } -------------------------------------------------------------------------------- /0547-number-of-provinces/0547-number-of-provinces.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | int n; 3 | public int findCircleNum(int[][] adj) { 4 | n = adj.length; 5 | boolean[] vis = new boolean[n]; 6 | int cnt = 0; 7 | 8 | for (int i=0; i> arrays) { 3 | int smallest = arrays.get(0).get(0); 4 | int biggest = arrays.get(0).get(arrays.get(0).size() - 1); 5 | int maxDistance = 0; 6 | 7 | for (int i = 1; i < arrays.size(); i++) { 8 | maxDistance = Math.max(maxDistance, Math.abs(arrays.get(i).get(arrays.get(i).size() - 1) - smallest)); 9 | maxDistance = Math.max(maxDistance, Math.abs(biggest - arrays.get(i).get(0))); 10 | smallest = Math.min(smallest, arrays.get(i).get(0)); 11 | biggest = Math.max(biggest, arrays.get(i).get(arrays.get(i).size() - 1)); 12 | } 13 | 14 | return maxDistance; 15 | } 16 | } -------------------------------------------------------------------------------- /0633-sum-of-square-numbers/0633-sum-of-square-numbers.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean judgeSquareSum(int c) { 3 | long left = 0, right = (long) Math.sqrt(c); 4 | while (left <= right) { 5 | long cur = left * left + right * right; 6 | if (cur < c) left++; 7 | else if (cur > c) right--; 8 | else return true; 9 | } 10 | return false; 11 | } 12 | } -------------------------------------------------------------------------------- /0633-sum-of-square-numbers/README.md: -------------------------------------------------------------------------------- 1 |

633. Sum of Square Numbers

Medium


Given a non-negative integer c, decide whether there're two integers a and b such that a2 + b2 = c.

2 | 3 |

 

4 |

Example 1:

5 | 6 |
 7 | Input: c = 5
 8 | Output: true
 9 | Explanation: 1 * 1 + 2 * 2 = 5
10 | 
11 | 12 |

Example 2:

13 | 14 |
15 | Input: c = 3
16 | Output: false
17 | 
18 | 19 |

 

20 |

Constraints:

21 | 22 |
    23 |
  • 0 <= c <= 231 - 1
  • 24 |
25 | -------------------------------------------------------------------------------- /0646-maximum-length-of-pair-chain/0646-maximum-length-of-pair-chain.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int findLongestChain(int[][] pairs) { 3 | Arrays.sort(pairs, (a, b) -> Integer.compare(a[1], b[1])); 4 | int curr = Integer.MIN_VALUE, ans = 0; 5 | 6 | for (int[] pair : pairs) { 7 | if (curr < pair[0]) { 8 | curr = pair[1]; 9 | ans++; 10 | } 11 | } 12 | return ans; 13 | } 14 | } -------------------------------------------------------------------------------- /0647-palindromic-substrings/0647-palindromic-substrings.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | int count = 0; 3 | public int countSubstrings(String s) { 4 | for (int i=0; i= 0 && right < s.length() && s.charAt(left) ==s.charAt(right)) { 14 | count++; left--; right++; 15 | } 16 | } 17 | } -------------------------------------------------------------------------------- /0650-2-keys-keyboard/0650-2-keys-keyboard.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int minSteps(int n) { 3 | int[] dp = new int[n+1]; 4 | 5 | for (int i = 2; i <= n; i++) { 6 | dp[i] = i; 7 | for (int j = i-1; j > 1; j--) { 8 | if (i % j == 0) { 9 | dp[i] = dp[j] + (i/j); 10 | break; 11 | } 12 | 13 | } 14 | } 15 | return dp[n]; 16 | } 17 | } -------------------------------------------------------------------------------- /0695-max-area-of-island/0695-max-area-of-island.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | int[][] adj; 3 | boolean[][] vis; 4 | int n, m; 5 | 6 | public int maxAreaOfIsland(int[][] adj) { 7 | this.adj = adj; n = adj.length; m = adj[0].length; vis = new boolean[n][m]; 8 | int ans = 0; 9 | 10 | for (int i=0; i=n || j<0 || j>=m || vis[i][j] || adj[i][j] == 0) return 0; 21 | vis[i][j] = true; 22 | return (1 + dfs(i+1, j) + dfs(i-1, j) + dfs(i, j+1) + dfs(i, j-1)); 23 | } 24 | } -------------------------------------------------------------------------------- /0714-best-time-to-buy-and-sell-stock-with-transaction-fee/0714-best-time-to-buy-and-sell-stock-with-transaction-fee.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int maxProfit(int[] prices, int fee) { 3 | int m = prices.length; 4 | int[] dp = new int[2]; 5 | 6 | for(int i = m - 1; i >= 0; i--){ 7 | int[] curr = new int[2]; 8 | for(int k = 0; k <= 1; k++){ 9 | //doNothing 10 | int doNothing = dp[k]; 11 | 12 | //buy sell 13 | int buysell = 0; 14 | 15 | if(k == 0) buysell = -prices[i] + dp[1]; 16 | else buysell = prices[i] - fee + dp[0]; 17 | 18 | curr[k] = Math.max(doNothing, buysell); 19 | } 20 | 21 | dp = curr; 22 | } 23 | 24 | return dp[0]; 25 | } 26 | } -------------------------------------------------------------------------------- /0719-find-k-th-smallest-pair-distance/0719-find-k-th-smallest-pair-distance.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int smallestDistancePair(int[] nums, int k) { 3 | Arrays.sort(nums); 4 | 5 | int n = nums.length; 6 | int l = 0, r = nums[n - 1] - nums[0]; 7 | 8 | for (int cnt = 0; l < r; cnt = 0) { 9 | int m = l + (r - l) / 2; 10 | 11 | for (int i = 0, j = 0; i < n; i++) { 12 | while (j < n && nums[j] <= nums[i] + m) j++; 13 | cnt += j - i - 1; 14 | } 15 | 16 | if (cnt < k) l = m + 1; 17 | else r = m; 18 | } 19 | 20 | return l; 21 | } 22 | } -------------------------------------------------------------------------------- /0724-find-pivot-index/0724-find-pivot-index.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int pivotIndex(int[] nums) { 3 | int n = nums.length; 4 | if (n == 1) return 0; 5 | int lsum = 0, rsum = 0; 6 | for (int i : nums) rsum += i; 7 | for (int i=0; i map; 3 | public MyCalendar() { 4 | map = new TreeMap<>(); 5 | } 6 | 7 | public boolean book(int start, int end) { 8 | Integer low = map.lowerKey(end); 9 | 10 | if(low == null || map.get(low) <= start) { 11 | map.put(start, end); 12 | return true; 13 | } 14 | return false; 15 | } 16 | } -------------------------------------------------------------------------------- /0740-delete-and-earn/0740-delete-and-earn.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int deleteAndEarn(int[] nums) { 3 | if(nums.length == 1) return nums[0]; 4 | 5 | int RANGE = 10000; 6 | int[] dp = new int[RANGE + 1]; 7 | for(int n : nums) dp[n] += n; 8 | 9 | //index 0 house is not used, -> 0 10 | //index 1 is just the value in dp[1] 11 | int max = dp[1]; 12 | for(int i = 2; i < RANGE + 1; i ++) { 13 | dp[i] = Math.max(dp[i - 1], dp[i] + dp[i - 2]); 14 | if(dp[i] > max)max = dp[i]; 15 | } 16 | 17 | return max; 18 | } 19 | } -------------------------------------------------------------------------------- /0745-find-smallest-letter-greater-than-target/0745-find-smallest-letter-greater-than-target.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public char nextGreatestLetter(char[] letters, char target) { 3 | int l = 0, h = letters.length; 4 | while (l < h) { 5 | int mid = l + (h - l) / 2; 6 | if (letters[mid] <= target) l = mid + 1; 7 | else h = mid; 8 | } 9 | return letters[l % letters.length]; 10 | } 11 | } -------------------------------------------------------------------------------- /0747-min-cost-climbing-stairs/0747-min-cost-climbing-stairs.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int minCostClimbingStairs(int[] a) { 3 | int n = a.length; 4 | int[] dp = new int[n]; // dp[i] -> min cost from 0 to ith step 5 | dp[0] = a[0]; dp[1] = a[1]; 6 | 7 | for (int i=2; i partitionLabels(String s) { 3 | Map map = new HashMap<>(); 4 | for (int i=0; i ans = new ArrayList<>(); 9 | int prev = -1, max = 0; 10 | 11 | for (int i=0; i postorder(Node root) { 3 | ArrayList list = new ArrayList<>(); 4 | return helper(root, list); 5 | } 6 | 7 | public List helper(Node root, ArrayList list) { 8 | if (root == null) return list; 9 | 10 | for (Node child : root.children) { 11 | helper(child, list); 12 | } 13 | 14 | list.add(root.val); 15 | return list; 16 | } 17 | } -------------------------------------------------------------------------------- /0780-max-chunks-to-make-sorted/0780-max-chunks-to-make-sorted.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int maxChunksToSorted(int[] arr) { 3 | if (arr == null || arr.length == 0) return 0; 4 | 5 | int count = 0, max = 0; 6 | for (int i = 0; i < arr.length; i++) { 7 | max = Math.max(max, arr[i]); 8 | if (max == i) { 9 | count++; 10 | } 11 | } 12 | 13 | return count; 14 | } 15 | } -------------------------------------------------------------------------------- /0789-kth-largest-element-in-a-stream/0789-kth-largest-element-in-a-stream.java: -------------------------------------------------------------------------------- 1 | class KthLargest { 2 | final PriorityQueue pq; 3 | final int k; 4 | 5 | public KthLargest(int k, int[] a) { 6 | this.k = k; 7 | pq = new PriorityQueue<>(k); 8 | for (int n : a) add(n); 9 | } 10 | 11 | public int add(int n) { 12 | if (pq.size() < k) pq.offer(n); 13 | else if (pq.peek() < n) { 14 | pq.poll(); 15 | pq.offer(n); 16 | } 17 | return pq.peek(); 18 | } 19 | } -------------------------------------------------------------------------------- /0806-domino-and-tromino-tiling/0806-domino-and-tromino-tiling.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | private static final int MOD = 1000000007; 3 | 4 | public int numTilings(int n) { 5 | int[][] dp = new int[n + 2][2]; 6 | dp[1][0] = 1; dp[1][1] = 1; dp[2][0] = 2; dp[2][1] = 2; 7 | 8 | for (int i = 3; i <= n; i++) { 9 | dp[i][0] = (int)((dp[i - 1][0] + dp[i - 2][0] + 2L * dp[i - 2][1]) % MOD); 10 | dp[i][1] = (int)((dp[i - 1][0] + dp[i - 1][1]) % MOD); 11 | } 12 | 13 | return dp[n][0]; 14 | } 15 | } 16 | 17 | 18 | // https://leetcode.com/problems/domino-and-tromino-tiling/solutions/1620975/c-python-simple-solution-w-images-explanation-optimization-from-brute-force-to-dp -------------------------------------------------------------------------------- /0812-rotate-string/0812-rotate-string.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean rotateString(String A, String B) { 3 | return A.length() == B.length() && (A + A).contains(B); 4 | } 5 | } -------------------------------------------------------------------------------- /0826-soup-servings/0826-soup-servings.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | private Map mp = new HashMap<>(); 3 | 4 | private double recur(int a, int b) { 5 | if (a <= 0 && b <= 0) return 0.5; 6 | if (a <= 0 && b > 0) return 1; 7 | if (a > 0 && b <= 0) return 0; 8 | if (mp.containsKey(new Pair(a, b))) return mp.get(new Pair(a, b)); 9 | 10 | double op1 = recur(a - 100, b); 11 | double op2 = recur(a - 75, b - 25); 12 | double op3 = recur(a - 50, b - 50); 13 | double op4 = recur(a - 25, b - 75); 14 | 15 | double result = 0.25 * (op1 + op2 + op3 + op4); 16 | mp.put(new Pair(a, b), result); 17 | return result; 18 | } 19 | 20 | public double soupServings(int n) { 21 | if (n >= 4800) return 1; 22 | double ans = recur(n, n); 23 | return ans; 24 | } 25 | } -------------------------------------------------------------------------------- /0853-most-profit-assigning-work/0853-most-profit-assigning-work.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int maxProfitAssignment(int[] difficulty, int[] profit, int[] worker) { 3 | List> jobs = new ArrayList<>(); 4 | int N = profit.length, res = 0, i = 0, best = 0; 5 | for (int j = 0; j < N; ++j) 6 | jobs.add(new Pair(difficulty[j], profit[j])); 7 | Collections.sort(jobs, Comparator.comparing(Pair::getKey)); 8 | Arrays.sort(worker); 9 | for (int ability : worker) { 10 | while (i < N && ability >= jobs.get(i).getKey()) 11 | best = Math.max(jobs.get(i++).getValue(), best); 12 | res += best; 13 | } 14 | return res; 15 | } 16 | } -------------------------------------------------------------------------------- /0876-middle-of-the-linked-list/0876-middle-of-the-linked-list.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public ListNode middleNode(ListNode head) { 3 | ListNode slow = head, fast = head; 4 | while (fast != null && fast.next != null) { 5 | slow = slow.next; 6 | fast = fast.next.next; 7 | } 8 | return slow; 9 | } 10 | } -------------------------------------------------------------------------------- /0876-middle-of-the-linked-list/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0882-peak-index-in-a-mountain-array/0882-peak-index-in-a-mountain-array.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int peakIndexInMountainArray(int[] arr) { 3 | int lo = 0, hi = arr.length - 1; 4 | while (lo < hi) { 5 | int mid = lo + (hi - lo) / 2; 6 | if (arr[mid] < arr[mid+1]) 7 | lo = mid + 1; 8 | else 9 | hi = mid; 10 | } 11 | return lo; 12 | } 13 | } -------------------------------------------------------------------------------- /0890-lemonade-change/0890-lemonade-change.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean lemonadeChange(int[] bills) { 3 | int five = 0, ten = 0; 4 | for (int bill : bills) { 5 | if (bill == 5) five++; 6 | else if (bill == 10) { 7 | if (five == 0) return false; 8 | five--; 9 | ten++; 10 | } else { 11 | if (five > 0 && ten > 0) { 12 | five--; 13 | ten--; 14 | } 15 | else if (five >= 3) five -= 3; 16 | else return false; 17 | } 18 | } 19 | return true; 20 | } 21 | } -------------------------------------------------------------------------------- /0905-length-of-longest-fibonacci-subsequence/0905-length-of-longest-fibonacci-subsequence.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int lenLongestFibSubseq(int[] A) { 3 | Set s = new HashSet(); 4 | for (int x : A) s.add(x); 5 | int res = 2; 6 | for (int i = 0; i < A.length; ++i) 7 | for (int j = i + 1; j < A.length; ++j) { 8 | int a = A[i], b = A[j], l = 2; 9 | while (s.contains(a + b)) { 10 | b = a + b; 11 | a = b - a; 12 | l++; 13 | } 14 | res = Math.max(res, l); 15 | } 16 | return res > 2 ? res : 0; 17 | } 18 | } -------------------------------------------------------------------------------- /0907-koko-eating-bananas/0907-koko-eating-bananas.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int minEatingSpeed(int[] piles, int h) { 3 | int lo = 0, hi = maxEle(piles); 4 | 5 | while (hi-lo>1) { 6 | int mid = lo + (hi-lo)/2; 7 | if (helper(piles, h, mid)) hi = mid; 8 | else lo = mid; 9 | } 10 | 11 | return hi; 12 | } 13 | 14 | public boolean helper(int[] piles, int h, int k) { 15 | long hour = 0; 16 | for (int pile : piles) { 17 | hour += pile / k; 18 | if (pile % k != 0) hour++; 19 | } 20 | return hour <= h; 21 | } 22 | 23 | public int maxEle(int[] a) { 24 | int max = 0; 25 | for (int ele : a) max = Math.max(max, ele); 26 | return max; 27 | } 28 | } -------------------------------------------------------------------------------- /0921-spiral-matrix-iii/0921-spiral-matrix-iii.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[][] spiralMatrixIII(int R, int C, int x, int y) { 3 | int[][] res = new int[R * C][2]; 4 | int dx = 0, dy = 1, n = 0, tmp; 5 | for (int j = 0; j < R * C; ++n) { 6 | for (int i = 0; i < n / 2 + 1; ++i) { 7 | if (0 <= x && x < R && 0 <= y && y < C) 8 | res[j++] = new int[] {x, y}; 9 | x += dx; 10 | y += dy; 11 | } 12 | tmp = dx; 13 | dx = dy; 14 | dy = -tmp; 15 | } 16 | return res; 17 | } 18 | } -------------------------------------------------------------------------------- /0925-construct-binary-tree-from-preorder-and-postorder-traversal/0925-construct-binary-tree-from-preorder-and-postorder-traversal.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | int preIndex = 0, posIndex = 0; 3 | public TreeNode constructFromPrePost(int[]pre, int[]post) { 4 | TreeNode root = new TreeNode(pre[preIndex++]); 5 | if (root.val != post[posIndex]) 6 | root.left = constructFromPrePost(pre, post); 7 | if (root.val != post[posIndex]) 8 | root.right = constructFromPrePost(pre, post); 9 | posIndex++; 10 | return root; 11 | } 12 | } -------------------------------------------------------------------------------- /0952-word-subsets/0952-word-subsets.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public List wordSubsets(String[] A, String[] B) { 3 | int[] count = new int[26], tmp; 4 | int i; 5 | for (String b : B) { 6 | tmp = counter(b); 7 | for (i = 0; i < 26; ++i) 8 | count[i] = Math.max(count[i], tmp[i]); 9 | } 10 | List res = new ArrayList<>(); 11 | for (String a : A) { 12 | tmp = counter(a); 13 | for (i = 0; i < 26; ++i) 14 | if (tmp[i] < count[i]) 15 | break; 16 | if (i == 26) res.add(a); 17 | } 18 | return res; 19 | } 20 | 21 | public int[] counter(String word) { 22 | int[] count = new int[26]; 23 | for (char c : word.toCharArray()) count[c - 'a']++; 24 | return count; 25 | } 26 | } -------------------------------------------------------------------------------- /0956-number-of-music-playlists/0956-number-of-music-playlists.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int numMusicPlaylists(int n, int goal, int k) { 3 | final int MOD = (int)1e9 + 7; 4 | long[][] dp = new long[2][n+1]; 5 | dp[0][0] = 1; 6 | 7 | for (int i = 1; i <= goal; i++) { 8 | dp[i%2][0] = 0; 9 | for (int j = 1; j <= Math.min(i, n); j++) { 10 | dp[i%2][j] = dp[(i - 1)%2][j - 1] * (n - (j - 1)) % MOD; 11 | if (j > k) dp[i%2][j] = (dp[i%2][j] + dp[(i - 1)%2][j] * (j - k)) % MOD; 12 | } 13 | } 14 | 15 | return (int)dp[goal%2][n]; 16 | } 17 | } -------------------------------------------------------------------------------- /0957-minimum-add-to-make-parentheses-valid/0957-minimum-add-to-make-parentheses-valid.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int minAddToMakeValid(String s) { 3 | int left = 0, right = 0; 4 | for (int i = 0; i < s.length(); i++) { 5 | if (s.charAt(i) == '(') right++; 6 | else if (right > 0) right--; 7 | else left++; 8 | } 9 | return left + right; 10 | } 11 | } -------------------------------------------------------------------------------- /0982-minimum-increment-to-make-array-unique/0982-minimum-increment-to-make-array-unique.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int minIncrementForUnique(int[] A) { 3 | Arrays.sort(A); 4 | int res = 0, need = 0; 5 | for (int a : A) { 6 | res += Math.max(need - a, 0); 7 | need = Math.max(a, need) + 1; 8 | } 9 | return res; 10 | } 11 | } -------------------------------------------------------------------------------- /0984-most-stones-removed-with-same-row-or-column/0984-most-stones-removed-with-same-row-or-column.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | Map f = new HashMap<>(); 3 | int islands = 0; 4 | 5 | public int removeStones(int[][] stones) { 6 | for (int i = 0; i < stones.length; ++i) 7 | union(stones[i][0], ~stones[i][1]); 8 | return stones.length - islands; 9 | } 10 | 11 | public int find(int x) { 12 | if (f.putIfAbsent(x, x) == null) 13 | islands++; 14 | if (x != f.get(x)) 15 | f.put(x, find(f.get(x))); 16 | return f.get(x); 17 | } 18 | 19 | public void union(int x, int y) { 20 | x = find(x); 21 | y = find(y); 22 | if (x != y) { 23 | f.put(x, y); 24 | islands--; 25 | } 26 | } 27 | } -------------------------------------------------------------------------------- /0988-flip-equivalent-binary-trees/0988-flip-equivalent-binary-trees.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean flipEquiv(TreeNode r1, TreeNode r2) { 3 | if (r1 == null || r2 == null) return r1 == r2; 4 | return r1.val == r2.val && 5 | (flipEquiv(r1.left, r2.left) && flipEquiv(r1.right, r2.right) || 6 | flipEquiv(r1.left, r2.right) && flipEquiv(r1.right, r2.left)); 7 | } 8 | } -------------------------------------------------------------------------------- /1-two-sum/1-two-sum.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] twoSum(int[] nums, int target) { 3 | int[] result = new int[2]; 4 | Map map = new HashMap(); 5 | for(int i=0; i < nums.length; i++) { 6 | if (map.containsKey(target - nums[i])) { 7 | result[1] = i; 8 | result[0] = map.get(target - nums[i]); 9 | return result; 10 | } 11 | map.put(nums[i], i); 12 | } 13 | return result; 14 | } 15 | } -------------------------------------------------------------------------------- /1-two-sum/NOTES.md: -------------------------------------------------------------------------------- 1 | ``` 2 | public int[] twoSum(int[] numbers, int target) { 3 | int[] result = new int[2]; 4 | Map map = new HashMap(); 5 | for (int i = 0; i < numbers.length; i++) { 6 | if (map.containsKey(target - numbers[i])) { 7 | result[1] = i; 8 | result[0] = map.get(target - numbers[i]); 9 | return result; 10 | } 11 | map.put(numbers[i], i); 12 | } 13 | return result; 14 | } 15 | ``` -------------------------------------------------------------------------------- /1013-fibonacci-number/1013-fibonacci-number.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int fib(int n) { 3 | if (n==0 || n==1) return n; // if (n==0) return 0; if (n==1) return 1; 4 | int[] dp = new int[n+1]; //dp[i] -> ith fib 5 | dp[0] = 0; dp[1] = 1; 6 | for (int i=2; i<=n; i++) { 7 | dp[i] = dp[i-1] + dp[i-2]; 8 | } 9 | 10 | return dp[n]; 11 | } 12 | } -------------------------------------------------------------------------------- /1016-subarray-sums-divisible-by-k/1016-subarray-sums-divisible-by-k.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int subarraysDivByK(int[] nums, int k) { 3 | int n = nums.length; 4 | int prefixMod = 0, result = 0; 5 | 6 | // There are k mod groups 0...k-1. 7 | int[] modGroups = new int[k]; 8 | modGroups[0] = 1; 9 | 10 | for (int num: nums) { 11 | // Take modulo twice to avoid negative remainders. 12 | prefixMod = (prefixMod + num % k + k) % k; 13 | // Add the count of subarrays that have the same remainder as the current 14 | // one to cancel out the remainders. 15 | result += modGroups[prefixMod]; 16 | modGroups[prefixMod]++; 17 | } 18 | 19 | return result; 20 | } 21 | } -------------------------------------------------------------------------------- /1049-minimum-domino-rotations-for-equal-row/1049-minimum-domino-rotations-for-equal-row.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int minDominoRotations(int[] A, int[] B) { 3 | HashSet s = new HashSet<>(Arrays.asList(1, 2, 3, 4, 5, 6)); 4 | int[] countA = new int[7], countB = new int[7]; 5 | for (int i = 0; i < A.length; ++i) { 6 | s.retainAll(new HashSet<>(Arrays.asList(A[i], B[i]))); 7 | countA[A[i]]++; 8 | countB[B[i]]++; 9 | } 10 | for (int i : s) 11 | return A.length - Math.max(countA[i], countB[i]); 12 | return -1; 13 | } 14 | } -------------------------------------------------------------------------------- /1063-best-sightseeing-pair/1063-best-sightseeing-pair.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int maxScoreSightseeingPair(int[] A) { 3 | int res = 0, cur = 0; 4 | for (int a: A) { 5 | res = Math.max(res, cur + a); 6 | cur = Math.max(cur, a) - 1; 7 | } 8 | return res; 9 | } 10 | } -------------------------------------------------------------------------------- /1087-longest-arithmetic-subsequence/1087-longest-arithmetic-subsequence.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int longestArithSeqLength(int[] A) { 3 | int res = 2, n = A.length; 4 | HashMap[] dp = new HashMap[n]; 5 | for (int j = 0; j < A.length; j++) { 6 | dp[j] = new HashMap<>(); 7 | for (int i = 0; i < j; i++) { 8 | int d = A[j] - A[i]; 9 | dp[j].put(d, dp[i].getOrDefault(d, 1) + 1); 10 | res = Math.max(res, dp[j].get(d)); 11 | } 12 | } 13 | return res; 14 | } 15 | } -------------------------------------------------------------------------------- /1114-binary-search-tree-to-greater-sum-tree/1114-binary-search-tree-to-greater-sum-tree.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | int pre = 0; 3 | public TreeNode bstToGst(TreeNode root) { 4 | if (root.right != null) bstToGst(root.right); 5 | pre = root.val = pre + root.val; 6 | if (root.left != null) bstToGst(root.left); 7 | return root; 8 | } 9 | } -------------------------------------------------------------------------------- /1129-longest-string-chain/1129-longest-string-chain.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int longestStrChain(String[] words) { 3 | Arrays.sort(words, (s1, s2) -> Integer.compare(s1.length(), s2.length())); 4 | Map map = new HashMap<>(); 5 | int ans = 0; 6 | 7 | for (String word : words) { 8 | int longest = 0; 9 | for (int i = 0; i < word.length(); i++) { 10 | StringBuilder sub = new StringBuilder(word); 11 | sub.deleteCharAt(i); 12 | String subStr = sub.toString(); 13 | longest = Math.max(longest, map.getOrDefault(subStr, 0) + 1); 14 | } 15 | map.put(word, longest); 16 | ans = Math.max(ans, longest); 17 | } 18 | 19 | return ans; 20 | } 21 | } -------------------------------------------------------------------------------- /1138-grumpy-bookstore-owner/1138-grumpy-bookstore-owner.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int maxSatisfied(int[] customers, int[] grumpy, int X) { 3 | int satisfied = 0, maxMakeSatisfied = 0; 4 | for (int i = 0, winOfMakeSatisfied = 0; i < grumpy.length; ++i) { 5 | if (grumpy[i] == 0) satisfied += customers[i]; 6 | else winOfMakeSatisfied += customers[i]; 7 | if (i >= X) { 8 | winOfMakeSatisfied -= grumpy[i - X] * customers[i - X]; 9 | } 10 | maxMakeSatisfied = Math.max(winOfMakeSatisfied, maxMakeSatisfied); 11 | } 12 | return satisfied + maxMakeSatisfied; 13 | } 14 | } -------------------------------------------------------------------------------- /1160-letter-tile-possibilities/1160-letter-tile-possibilities.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int numTilePossibilities(String tiles) { 3 | int[] count = new int[26]; 4 | for (char c : tiles.toCharArray()) count[c - 'A']++; 5 | return dfs(count); 6 | } 7 | 8 | public int dfs(int[] arr) { 9 | int sum = 0; 10 | for (int i = 0; i < 26; i++) { 11 | if (arr[i] == 0) continue; 12 | sum++; 13 | arr[i]--; 14 | sum += dfs(arr); 15 | arr[i]++; 16 | } 17 | return sum; 18 | } 19 | } -------------------------------------------------------------------------------- /1196-filling-bookcase-shelves/1196-filling-bookcase-shelves.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int minHeightShelves(int[][] books, int shelf_width) { 3 | int[] dp = new int[books.length + 1]; 4 | dp[0] = 0; 5 | 6 | for (int i = 1; i <= books.length; ++i) { 7 | int width = books[i-1][0]; 8 | int height = books[i-1][1]; 9 | dp[i] = dp[i-1] + height; 10 | for (int j = i - 1; j > 0 && width + books[j-1][0] <= shelf_width; --j) { 11 | height = Math.max(height, books[j-1][1]); 12 | width += books[j-1][0]; 13 | dp[i] = Math.min(dp[i], dp[j-1] + height); 14 | } 15 | } 16 | return dp[books.length]; 17 | } 18 | } -------------------------------------------------------------------------------- /1218-lowest-common-ancestor-of-deepest-leaves/1218-lowest-common-ancestor-of-deepest-leaves.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | class Pair { 3 | TreeNode node; 4 | int d; 5 | Pair(TreeNode node, int d) { 6 | this.node = node; 7 | this.d = d; 8 | } 9 | } 10 | public TreeNode lcaDeepestLeaves(TreeNode root) { 11 | Pair p = getLca(root, 0); 12 | return p.node; 13 | } 14 | private Pair getLca(TreeNode root, int d) { 15 | if (root == null) return new Pair(null, d); 16 | Pair l = getLca(root.left, d + 1); 17 | Pair r = getLca(root.right, d + 1); 18 | if (l.d == r.d) { 19 | return new Pair(root, l.d); 20 | } else { 21 | return l.d > r.d ? l : r; 22 | } 23 | } 24 | } -------------------------------------------------------------------------------- /1227-number-of-equivalent-domino-pairs/1227-number-of-equivalent-domino-pairs.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int numEquivDominoPairs(int[][] dominoes) { 3 | int ans = 0; 4 | int[] bucket = new int[100]; 5 | for (int[] domino : dominoes) 6 | bucket[domino[0] * 10 + domino[1]]++; 7 | int ten = 0, one = 0; 8 | for (int i = 11; i < 100; i++) { 9 | int k = bucket[i]; // for 11,22,33...99, add k*(k-1)/2 10 | ten = i / 10; 11 | one = i % 10; 12 | if (one > ten) 13 | k += bucket[one * 10 + ten]; // for bucket[12], k= bucket[12]+bucket[21], then add k*(k-1)/2 14 | else if (one < ten) // for bucket[21], ignore since already counted in bucket[12] 15 | continue; 16 | ans += k * (k - 1) / 2; 17 | } 18 | return ans; 19 | } 20 | } -------------------------------------------------------------------------------- /1236-n-th-tribonacci-number/1236-n-th-tribonacci-number.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int tribonacci(int n) { 3 | if (n==0) return 0; if (n==1) return 1; if (n==2) return 1; 4 | 5 | int[] dp = new int[n+1]; // dp[i] -> ith Tribonacci Number 6 | dp[0] = 0; dp[1] = 1; dp[2] = 1; 7 | 8 | for (int i=3; i<=n; i++) { 9 | dp[i] = dp[i-1] + dp[i-2] + dp[i-3]; 10 | } 11 | return dp[n]; 12 | } 13 | } -------------------------------------------------------------------------------- /1249-snapshot-array/1249-snapshot-array.java: -------------------------------------------------------------------------------- 1 | class SnapshotArray { 2 | TreeMap[] A; 3 | int snap_id = 0; 4 | public SnapshotArray(int length) { 5 | A = new TreeMap[length]; 6 | for (int i = 0; i < length; i++) { 7 | A[i] = new TreeMap(); 8 | A[i].put(0, 0); 9 | } 10 | } 11 | 12 | public void set(int index, int val) { 13 | A[index].put(snap_id, val); 14 | } 15 | 16 | public int snap() { 17 | return snap_id++; 18 | } 19 | 20 | public int get(int index, int snap_id) { 21 | return A[index].floorEntry(snap_id).getValue(); 22 | } 23 | } -------------------------------------------------------------------------------- /1250-longest-common-subsequence/1250-longest-common-subsequence.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int longestCommonSubsequence(String text1, String text2) { 3 | int m = text1.length(); 4 | int n = text2.length(); 5 | 6 | int[][] dp = new int[m+1][n+1]; 7 | for (int i=0; i<=m; i++) { 8 | for (int j=0; j<=n; j++) { 9 | if (i == 0 || j == 0) dp[i][j] = 0; 10 | else { 11 | if (text1.charAt(i-1) == text2.charAt(j-1)) dp[i][j] = 1 + dp[i-1][j-1]; 12 | else dp[i][j] = Math.max(dp[i-1][j], dp[i][j-1]); 13 | } 14 | } 15 | } 16 | 17 | return dp[m][n]; 18 | } 19 | } -------------------------------------------------------------------------------- /1256-rank-transform-of-an-array/1256-rank-transform-of-an-array.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] arrayRankTransform(int[] arr) { 3 | int[] A = Arrays.copyOf(arr, arr.length); 4 | Arrays.sort(A); 5 | HashMap rank = new HashMap<>(); 6 | for (int x : A) 7 | rank.putIfAbsent(x, rank.size() + 1); 8 | for (int i = 0; i < arr.length; ++i) 9 | A[i] = rank.get(arr[i]); 10 | return A; 11 | } 12 | } -------------------------------------------------------------------------------- /1293-three-consecutive-odds/1293-three-consecutive-odds.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean threeConsecutiveOdds(int[] arr) { 3 | for (int i = 0; i < arr.length - 2; i++) { 4 | if (arr[i]%2 == 1 && arr[i+1]%2 == 1 && arr[i+2]%2 == 1) return true; 5 | } 6 | return false; 7 | } 8 | } -------------------------------------------------------------------------------- /13-roman-to-integer/NOTES.md: -------------------------------------------------------------------------------- 1 | ``` 2 | class Solution { 3 | public int romanToInt(String s) { 4 | Map map = new HashMap(); 5 | map.put('I', 1); 6 | map.put('V', 5); 7 | map.put('X', 10); 8 | map.put('L', 50); 9 | map.put('C', 100); 10 | map.put('D', 500); 11 | map.put('M', 1000); 12 | int result = 0; 13 | for (int i=0; i0 && map.get(s.charAt(i)) > map.get(s.charAt(i-1))){ 15 | result += map.get(s.charAt(i)) - 2 * map.get(s.charAt(i-1)); 16 | } else { 17 | result += map.get(s.charAt(i)); 18 | } 19 | } 20 | return result; 21 | } 22 | } 23 | ``` -------------------------------------------------------------------------------- /1302-delete-characters-to-make-fancy-string/1302-delete-characters-to-make-fancy-string.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public String makeFancyString(String s) { 3 | StringBuilder ans = new StringBuilder(); 4 | ans.append(s.charAt(0)); 5 | int n = s.length(), cnt = 1; 6 | for (int i = 1; i < n; i++) { 7 | if (s.charAt(i) == ans.charAt(ans.length() - 1)) { 8 | cnt++; 9 | if (cnt < 3) { 10 | ans.append(s.charAt(i)); 11 | } 12 | } else { 13 | cnt = 1; 14 | ans.append(s.charAt(i)); 15 | } 16 | } 17 | return ans.toString(); 18 | } 19 | } -------------------------------------------------------------------------------- /1304-longest-happy-string/1304-longest-happy-string.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public String generate(int a, int b, int c, String aa, String bb, String cc) { 3 | if (a < b) return generate(b, a, c, bb, aa, cc); 4 | if (b < c) return generate(a, c, b, aa, cc, bb); 5 | if (b == 0) return aa.repeat(Math.min(2, a)); 6 | int use_a = Math.min(2, a), use_b = a - use_a >= b ? 1 : 0; 7 | return aa.repeat(use_a) + bb.repeat(use_b) + 8 | generate(a - use_a, b - use_b, c, aa, bb, cc); 9 | } 10 | public String longestDiverseString(int a, int b, int c) { 11 | return generate(a, b, c, "a", "b", "c"); 12 | } 13 | } -------------------------------------------------------------------------------- /1305-number-of-visible-people-in-a-queue/1305-number-of-visible-people-in-a-queue.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] canSeePersonsCount(int[] a) { 3 | int n = a.length; 4 | int[] res = new int[n]; 5 | Stack st = new Stack<>(); 6 | 7 | for (int i=0; i a[st.peek()]) { 9 | res[st.pop()]++; 10 | } 11 | 12 | if (!st.isEmpty()) { 13 | res[st.peek()]++; 14 | } 15 | st.push(i);} 16 | return res; 17 | } 18 | } -------------------------------------------------------------------------------- /1335-maximum-candies-allocated-to-k-children/1335-maximum-candies-allocated-to-k-children.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int maximumCandies(int[] A, long k) { 3 | int left = 0, right = 10_000_000; 4 | while (left < right) { 5 | long sum = 0; 6 | int mid = (left + right + 1) / 2; 7 | for (int a : A) { 8 | sum += a / mid; 9 | } 10 | if (k > sum) 11 | right = mid - 1; 12 | else 13 | left = mid; 14 | } 15 | return left; 16 | } 17 | } -------------------------------------------------------------------------------- /1350-remove-sub-folders-from-the-filesystem/1350-remove-sub-folders-from-the-filesystem.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public List removeSubfolders(String[] folder) { 3 | Arrays.sort(folder, Comparator.comparing(s -> s.length())); 4 | Set seen = new HashSet<>(); 5 | outer: 6 | for (String f : folder) { 7 | for (int i = 2; i < f.length(); ++i) 8 | if (f.charAt(i) == '/' && seen.contains(f.substring(0, i))) 9 | continue outer; 10 | seen.add(f); 11 | } 12 | return new ArrayList<>(seen); 13 | } 14 | } -------------------------------------------------------------------------------- /1364-tuple-with-same-product/1364-tuple-with-same-product.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int tupleSameProduct(int[] nums) { 3 | HashMap productCount = new HashMap<>(); 4 | int result = 0; 5 | 6 | for (int i = 0; i < nums.length; i++) 7 | for (int j = i+1; j < nums.length; j++) { 8 | int product = nums[i] * nums[j]; 9 | result += 8 * productCount.getOrDefault(product, 0); 10 | productCount.put(product, productCount.getOrDefault(product, 0) + 1); 11 | } 12 | 13 | return result; 14 | } 15 | } -------------------------------------------------------------------------------- /1370-count-number-of-nice-subarrays/1370-count-number-of-nice-subarrays.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int numberOfSubarrays(int[] A, int k) { 3 | return atMost(A, k) - atMost(A, k - 1); 4 | } 5 | 6 | public int atMost(int[] A, int k) { 7 | int res = 0, i = 0, n = A.length; 8 | for (int j = 0; j < n; j++) { 9 | k -= A[j] % 2; 10 | while (k < 0) 11 | k += A[i++] % 2; 12 | res += j - i + 1; 13 | } 14 | return res; 15 | } 16 | } -------------------------------------------------------------------------------- /1380-number-of-closed-islands/1380-number-of-closed-islands.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | int n, m; 3 | public int closedIsland(int[][] adj) { 4 | n = adj.length; m = adj[0].length; 5 | int cnt = 0; 6 | 7 | for (int i=0; i=n || j>=m) return false; 18 | if (adj[i][j] == 1) return true; 19 | adj[i][j] = 1; 20 | 21 | boolean left = dfs(adj,i, j-1), right = dfs(adj,i, j+1); 22 | boolean up = dfs(adj,i-1, j), down = dfs(adj,i+1, j); 23 | return left && right && up && down; 24 | } 25 | } -------------------------------------------------------------------------------- /1392-find-the-difference-of-two-arrays/1392-find-the-difference-of-two-arrays.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public List> findDifference(int[] nums1, int[] nums2) { 3 | Set set1 = new HashSet(); 4 | Set set2 = new HashSet(); 5 | 6 | for (int i : nums1) {set1.add(i);} 7 | for (int i : nums2) {set2.add(i);} 8 | 9 | List> res = new ArrayList<>(); 10 | res.add(new ArrayList<>()); 11 | res.add(new ArrayList<>()); 12 | 13 | for (int i : set1) { 14 | if (!set2.contains(i)) {res.get(0).add(i);} 15 | } 16 | for (int i: set2) { 17 | if (!set1.contains(i)) {res.get(1).add(i);} 18 | } 19 | return res; 20 | } 21 | } -------------------------------------------------------------------------------- /14-longest-common-prefix/14-longest-common-prefix.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public String longestCommonPrefix(String[] strs) { 3 | if (strs.length == 0) return ""; 4 | String prefix = strs[0]; 5 | for(int i=0; i < strs.length; i++) 6 | while (strs[i].indexOf(prefix) != 0){ 7 | prefix = prefix.substring(0, prefix.length() - 1); 8 | if(prefix.isEmpty()) return ""; 9 | } 10 | return prefix; 11 | } 12 | } -------------------------------------------------------------------------------- /14-longest-common-prefix/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1402-count-square-submatrices-with-all-ones/1402-count-square-submatrices-with-all-ones.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int countSquares(int[][] A) { 3 | int res = 0; 4 | for (int i = 0; i < A.length; ++i) { 5 | for (int j = 0; j < A[0].length; ++j) { 6 | if (A[i][j] > 0 && i > 0 && j > 0) { 7 | A[i][j] = Math.min(A[i - 1][j - 1], Math.min(A[i - 1][j], A[i][j - 1])) + 1; 8 | } 9 | res += A[i][j]; 10 | } 11 | } 12 | return res; 13 | } 14 | } -------------------------------------------------------------------------------- /1407-group-the-people-given-the-group-size-they-belong-to/1407-group-the-people-given-the-group-size-they-belong-to.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public List> groupThePeople(int[] groupSizes) { 3 | HashMap> temp_group = new HashMap<>(); 4 | List> result = new ArrayList<>(); 5 | 6 | for(int i = 0; i < groupSizes.length; i++) { 7 | int size = groupSizes[i]; 8 | temp_group.putIfAbsent(size, new ArrayList<>()); 9 | temp_group.get(size).add(i); 10 | 11 | if(temp_group.get(size).size() == size) { 12 | result.add(new ArrayList<>(temp_group.get(size))); 13 | temp_group.get(size).clear(); 14 | } 15 | } 16 | return result; 17 | } 18 | } -------------------------------------------------------------------------------- /1408-find-the-smallest-divisor-given-a-threshold/1408-find-the-smallest-divisor-given-a-threshold.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int smallestDivisor(int[] nums, int threshold) { 3 | int lo = 0, hi = maxEle(nums); 4 | while (hi-lo>1) { 5 | int mid = lo + (hi-lo)/2; 6 | if (helper(nums, threshold, mid)) hi = mid; 7 | else lo = mid; 8 | } 9 | 10 | return hi; 11 | } 12 | 13 | public boolean helper(int[] a, int k, int mid) { 14 | int sum = 0; 15 | for (int i=0; i= 10 && i < 100) { 11 | ++counter; 12 | } 13 | 14 | if (i >= 1000 && i < 10000) { 15 | ++counter; 16 | } 17 | 18 | if (i >= 100000 && i < 1000000) { 19 | ++counter; 20 | } 21 | } 22 | 23 | return counter; 24 | } 25 | } -------------------------------------------------------------------------------- /1435-xor-queries-of-a-subarray/1435-xor-queries-of-a-subarray.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] xorQueries(int[] A, int[][] queries) { 3 | int[] res = new int[queries.length], q; 4 | for (int i = 1; i < A.length; ++i) 5 | A[i] ^= A[i - 1]; 6 | for (int i = 0; i < queries.length; ++i) { 7 | q = queries[i]; 8 | res[i] = q[0] > 0 ? A[q[0] - 1] ^ A[q[1]] : A[q[1]]; 9 | } 10 | return res; 11 | } 12 | } -------------------------------------------------------------------------------- /1437-minimum-insertion-steps-to-make-a-string-palindrome/1437-minimum-insertion-steps-to-make-a-string-palindrome.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int minInsertions(String s) { 3 | int n = s.length(); 4 | int[][] dp = new int[n][n]; // dp[i][j] -> LCS in string from i to j; 5 | 6 | for (int i=0; i 0 && count[1] > 0 && count[2] > 0) 7 | --count[s.charAt(i++) - 'a']; 8 | res += i; 9 | } 10 | return res; 11 | } 12 | } -------------------------------------------------------------------------------- /1461-count-all-valid-pickup-and-delivery-options/1461-count-all-valid-pickup-and-delivery-options.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | private static final int MOD = 1000000007; 3 | 4 | public int countOrders(int n) { 5 | long count = 1; 6 | for (int i = 2; i <= n; i++) { 7 | count = (count * (2 * i - 1) * i) % MOD; 8 | } 9 | return (int) count; 10 | } 11 | } -------------------------------------------------------------------------------- /1473-find-the-longest-substring-containing-vowels-in-even-counts/1473-find-the-longest-substring-containing-vowels-in-even-counts.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int findTheLongestSubstring(String s) { 3 | int res = 0 , cur = 0, n = s.length(); 4 | HashMap seen = new HashMap<>(); 5 | seen.put(0, -1); 6 | for (int i = 0; i < n; ++i) { 7 | cur ^= 1 << ("aeiou".indexOf(s.charAt(i)) + 1 ) >> 1; 8 | seen.putIfAbsent(cur, i); 9 | res = Math.max(res, i - seen.get(cur)); 10 | } 11 | return res; 12 | } 13 | } -------------------------------------------------------------------------------- /1477-product-of-the-last-k-numbers/1477-product-of-the-last-k-numbers.java: -------------------------------------------------------------------------------- 1 | class ProductOfNumbers { 2 | ArrayList A = new ArrayList(){{ 3 | add(1); 4 | }}; 5 | 6 | public void add(int a) { 7 | if (a > 0) 8 | A.add(A.get(A.size() - 1) * a); 9 | else { 10 | A = new ArrayList(); 11 | A.add(1); 12 | } 13 | } 14 | 15 | public int getProduct(int k) { 16 | int n = A.size(); 17 | return k < n ? A.get(n - 1) / A.get(n - k - 1) : 0; 18 | } 19 | } 20 | 21 | /** 22 | * Your ProductOfNumbers object will be instantiated and called as such: 23 | * ProductOfNumbers obj = new ProductOfNumbers(); 24 | * obj.add(num); 25 | * int param_2 = obj.getProduct(k); 26 | */ -------------------------------------------------------------------------------- /1500-count-largest-group/1500-count-largest-group.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int countLargestGroup(int n) { 3 | int ans = 0; 4 | int[] dp = new int[n + 1]; 5 | int[] counts = new int[37]; // max sum of digits of n <= 10000 is 36 (sum of digits of 9999) 6 | for (int i = 1; i <= n; i++) { 7 | dp[i] = i % 10 + dp[i / 10]; 8 | counts[dp[i]]++; 9 | } 10 | int max = 0; 11 | for (int count : counts) 12 | max = Math.max(max, count); 13 | for (int count : counts) { 14 | if (count == max) 15 | ans++; 16 | } 17 | return ans; 18 | } 19 | } -------------------------------------------------------------------------------- /1524-string-matching-in-an-array/1524-string-matching-in-an-array.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public List stringMatching(String[] words) { 3 | List answer = new ArrayList<>(); 4 | for(int i = 0; i < words.length; i++) { 5 | if(isSubstring(words[i], words, i)) { 6 | answer.add(words[i]); 7 | } 8 | } 9 | return answer; 10 | } 11 | 12 | private boolean isSubstring(String w, String[] words, int idx) { 13 | for(int i = 0; i < words.length; i++) { 14 | if(idx != i && w.length() <= words[i].length()) { 15 | if(words[i].contains(w)) { 16 | return true; 17 | } 18 | } 19 | } 20 | return false; 21 | } 22 | } -------------------------------------------------------------------------------- /1537-maximum-score-after-splitting-a-string/1537-maximum-score-after-splitting-a-string.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int maxScore(String s) { 3 | int countOne = 0, n = s.length(); 4 | char[] chars = s.toCharArray(); 5 | 6 | for(int i=0; i arr = new ArrayList<>(); 4 | for (int i = 0; i < n; i++) { 5 | int prefix = 0; 6 | for (int j = i; j < n; j++) { 7 | prefix += nums[j]; 8 | arr.add(prefix); 9 | } 10 | } 11 | Collections.sort(arr); 12 | int result = 0; 13 | int MOD = 1000000007; 14 | for (int i = left - 1; i < right; i++) { 15 | result = (result + arr.get(i)) % MOD; 16 | } 17 | return result; 18 | } 19 | } -------------------------------------------------------------------------------- /1620-check-if-array-pairs-are-divisible-by-k/1620-check-if-array-pairs-are-divisible-by-k.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean canArrange(int[] arr, int k) { 3 | int [] freq = new int[k]; 4 | for(int num: arr){ 5 | int rem = num % k; 6 | if (rem < 0) rem = rem + k; 7 | freq[rem]++; 8 | } 9 | 10 | if (freq[0] % 2 != 0) return false; 11 | for(int i=1; i<=k/2; i++) { 12 | if (freq[i] != freq[k-i]) return false; 13 | } 14 | return true; 15 | } 16 | } -------------------------------------------------------------------------------- /1631-number-of-sub-arrays-with-odd-sum/1631-number-of-sub-arrays-with-odd-sum.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int numOfSubarrays(int[] arr) { 3 | int sum = 0, odd = 0; 4 | for (int i = 0; i < arr.length; ++i) { 5 | odd = arr[i] % 2 == 1 ? i - odd + 1 : odd; 6 | sum = (sum + odd) % 1000000007; 7 | } 8 | return sum; 9 | } 10 | } -------------------------------------------------------------------------------- /1642-water-bottles/1642-water-bottles.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int numWaterBottles(int numBottles, int numExchange) { 3 | int res = numBottles; 4 | 5 | while(numBottles >= numExchange) { 6 | int remainder = numBottles % numExchange; 7 | numBottles /= numExchange; 8 | res += numBottles; 9 | numBottles += remainder; 10 | } 11 | 12 | return res; 13 | } 14 | } -------------------------------------------------------------------------------- /1646-kth-missing-positive-number/1646-kth-missing-positive-number.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int findKthPositive(int[] a, int k) { 3 | int lo = -1, hi = a.length; 4 | while (hi-lo>1) { 5 | int mid = lo + (hi-lo)/2; 6 | if (a[mid] - mid - 1 >= k) hi = mid; 7 | else lo = mid; 8 | } 9 | 10 | return hi + k; 11 | } 12 | } -------------------------------------------------------------------------------- /1656-count-good-triplets/1656-count-good-triplets.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int countGoodTriplets(int[] arr, int a, int b, int c) { 3 | int count = 0; 4 | for (int i=0; i 1) { 5 | if (k == l / 2 + 1) 6 | return count % 2 == 0 ? '1' : '0'; 7 | if (k > l / 2) { 8 | k = l + 1 - k; 9 | count++; 10 | } 11 | l /= 2; 12 | } 13 | return count % 2 == 0 ? '0' : '1'; 14 | } 15 | } -------------------------------------------------------------------------------- /1694-make-sum-divisible-by-p/1694-make-sum-divisible-by-p.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int minSubarray(int[] A, int p) { 3 | int n = A.length, res = n, need = 0, cur = 0; 4 | for (int a : A) 5 | need = (need + a) % p; 6 | Map last = new HashMap<>(); 7 | last.put(0, -1); 8 | for (int i = 0; i < n; ++i) { 9 | cur = (cur + A[i]) % p; 10 | last.put(cur, i); 11 | int want = (cur - need + p) % p; 12 | res = Math.min(res, i - last.getOrDefault(want, -n)); 13 | } 14 | return res < n ? res : -1; 15 | } 16 | } -------------------------------------------------------------------------------- /1711-find-valid-matrix-given-row-and-column-sums/1711-find-valid-matrix-given-row-and-column-sums.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[][] restoreMatrix(int[] row, int[] col) { 3 | int n = row.length, m = col.length; 4 | int[][] A = new int[n][m]; 5 | for (int i=0; i 0) 20 | ret[i] = pref[i+k+1]-pref[i+1]; 21 | if(k < 0) 22 | ret[i] = pref[i+n]-pref[i+k+n]; 23 | } 24 | return ret; 25 | } 26 | } -------------------------------------------------------------------------------- /1775-design-an-ordered-stream/1775-design-an-ordered-stream.java: -------------------------------------------------------------------------------- 1 | class OrderedStream { 2 | private String[] data; 3 | private int ptr; 4 | 5 | public OrderedStream(int n) { 6 | data = new String[n + 1]; 7 | ptr = 1; 8 | } 9 | 10 | public String[] insert(int idKey, String value) { 11 | data[idKey] = value; 12 | List res = new ArrayList<>(); 13 | for (int i = ptr; i < data.length; i++) { 14 | if (data[i] != null) { 15 | res.add(data[i]); 16 | } else { 17 | ptr = i; 18 | break; 19 | } 20 | } 21 | return res.toArray(new String[0]); 22 | } 23 | } -------------------------------------------------------------------------------- /1776-minimum-operations-to-reduce-x-to-zero/1776-minimum-operations-to-reduce-x-to-zero.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int minOperations(int[] nums, int x) { 3 | int target = -x, n = nums.length; 4 | for (int num : nums) target += num; 5 | 6 | if (target == 0) return n; 7 | 8 | int maxLen = 0, curSum = 0, left = 0; 9 | 10 | for (int right = 0; right < n; ++right) { 11 | curSum += nums[right]; 12 | while (left <= right && curSum > target) { 13 | curSum -= nums[left]; 14 | left++; 15 | } 16 | if (curSum == target) { 17 | maxLen = Math.max(maxLen, right - left + 1); 18 | } 19 | } 20 | 21 | return maxLen != 0 ? n - maxLen : -1; 22 | } 23 | } -------------------------------------------------------------------------------- /1786-count-the-number-of-consistent-strings/1786-count-the-number-of-consistent-strings.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int countConsistentStrings(String allowed, String[] words) { 3 | boolean[] map = new boolean[26]; 4 | for (char c : allowed.toCharArray()) { 5 | map[c - 'a'] = true; 6 | } 7 | int ans = 0; 8 | for (String word : words) { 9 | int i = 0; 10 | int len = word.length(); 11 | while (i < len) { 12 | if (!map[word.charAt(i) - 'a']) break; 13 | i += 1; 14 | } 15 | ans += (i == len ? 1 : 0); 16 | } 17 | return ans; 18 | } 19 | } -------------------------------------------------------------------------------- /1803-average-waiting-time/1803-average-waiting-time.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public double averageWaitingTime(int[][] A) { 3 | double wait = 0, cur = 0; 4 | for (int[] a: A) { 5 | cur = Math.max(cur, 1.0 * a[0]) + a[1]; 6 | wait += cur - a[0]; 7 | } 8 | return 1.0 * wait / A.length; 9 | } 10 | } -------------------------------------------------------------------------------- /1833-find-the-highest-altitude/1833-find-the-highest-altitude.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int largestAltitude(int[] gain) { 3 | int max = 0; 4 | int alt = 0; 5 | for (int i = 0; i < gain.length; i++) { 6 | alt += gain[i]; 7 | max = Math.max(alt, max); 8 | } 9 | return max; 10 | } 11 | } -------------------------------------------------------------------------------- /1849-maximum-absolute-sum-of-any-subarray/1849-maximum-absolute-sum-of-any-subarray.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int maxAbsoluteSum(int[] A) { 3 | int s = 0, min = 0, max = 0; 4 | for (int a: A) { 5 | s += a; 6 | min = Math.min(min, s); 7 | max = Math.max(max, s); 8 | } 9 | return max - min; 10 | } 11 | } -------------------------------------------------------------------------------- /1878-check-if-array-is-sorted-and-rotated/1878-check-if-array-is-sorted-and-rotated.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean check(int[] A) { 3 | int k = 0, n = A.length; 4 | for (int i = 0; i < n; ++i) { 5 | if (A[i] > A[(i + 1) % n]) { 6 | k++; 7 | } 8 | if (k > 1) { 9 | return false; 10 | } 11 | } 12 | return true; 13 | } 14 | } -------------------------------------------------------------------------------- /1889-check-if-number-is-a-sum-of-powers-of-three/1889-check-if-number-is-a-sum-of-powers-of-three.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean checkPowersOfThree(int n) { 3 | while (n > 0) { 4 | if (n % 3 == 2) { 5 | return false; 6 | } 7 | n /= 3; 8 | } 9 | return true; 10 | } 11 | } -------------------------------------------------------------------------------- /1890-sum-of-beauty-of-all-substrings/1890-sum-of-beauty-of-all-substrings.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int beautySum(String s) { 3 | int ans=0; 4 | for (int i=0; i 0) { 12 | min = Math.min(min,freq[k]); 13 | max = Math.max(max,freq[k]); 14 | } 15 | } 16 | ans += max-min; 17 | } 18 | } 19 | return ans; 20 | } 21 | } -------------------------------------------------------------------------------- /1895-minimum-number-of-operations-to-move-all-balls-to-each-box/1895-minimum-number-of-operations-to-move-all-balls-to-each-box.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] minOperations(String boxes) { 3 | int[] res = new int[boxes.length()]; 4 | for (int i = 0, ops = 0, cnt = 0; i < boxes.length(); ++i) { 5 | res[i] += ops; 6 | cnt += boxes.charAt(i) == '1' ? 1 : 0; 7 | ops += cnt; 8 | } 9 | for (int i = boxes.length() - 1, ops = 0, cnt = 0; i >= 0; --i) { 10 | res[i] += ops; 11 | cnt += boxes.charAt(i) == '1' ? 1 : 0; 12 | ops += cnt; 13 | } 14 | return res; 15 | } 16 | } -------------------------------------------------------------------------------- /1915-check-if-one-string-swap-can-make-strings-equal/1915-check-if-one-string-swap-can-make-strings-equal.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean areAlmostEqual(String s1, String s2) { 3 | List l = new ArrayList<>(); 4 | for (int i = 0; i < s1.length(); i++) { 5 | if (s1.charAt(i) != s2.charAt(i)) l.add(i); 6 | if (l.size() > 2) return false; // added this line to short circuit the loop 7 | } 8 | return l.size() == 0 || (l.size() == 2 9 | && s1.charAt(l.get(0)) == s2.charAt(l.get(1)) 10 | && s1.charAt(l.get(1)) == s2.charAt(l.get(0))); 11 | } 12 | } -------------------------------------------------------------------------------- /1923-sentence-similarity-iii/1923-sentence-similarity-iii.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean areSentencesSimilar(String s1, String s2) { 3 | String[] words1 = s1.split(" "), words2 = s2.split(" "); 4 | int i = 0, n1 = words1.length, n2 = words2.length; 5 | if (n1 > n2) { 6 | return areSentencesSimilar(s2, s1); 7 | } 8 | while (i < n1 && words1[i].equals(words2[i])) { 9 | ++i; 10 | } 11 | while (i < n1 && words1[i].equals(words2[n2 - n1 + i])) { 12 | ++i; 13 | } 14 | return i == n1; 15 | } 16 | } -------------------------------------------------------------------------------- /1927-maximum-ascending-subarray-sum/1927-maximum-ascending-subarray-sum.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int maxAscendingSum(int[] nums) { 3 | int res = nums[0],temp = nums[0]; 4 | for(int i = 1;i nums[i-1]) 6 | temp+=nums[i]; 7 | else 8 | temp = nums[i]; 9 | res = Math.max(res,temp); 10 | } 11 | return res; 12 | } 13 | } -------------------------------------------------------------------------------- /1929-maximum-value-at-a-given-index-in-a-bounded-array/1929-maximum-value-at-a-given-index-in-a-bounded-array.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int maxValue(int n, int index, int maxSum) { 3 | maxSum -= n; 4 | int left = 0, right = maxSum, mid; 5 | while (left < right) { 6 | mid = (left + right + 1) / 2; 7 | if (search(n, index, mid) <= maxSum) 8 | left = mid; 9 | else 10 | right = mid - 1; 11 | } 12 | return left + 1; 13 | } 14 | 15 | private long search(int n, int index, int a) { 16 | int b = Math.max(a - index, 0); 17 | long res = (long)(a + b) * (a - b + 1) / 2; 18 | b = Math.max(a - ((n - 1) - index), 0); 19 | res += (long)(a + b) * (a - b + 1) / 2; 20 | return res - a; 21 | } 22 | } -------------------------------------------------------------------------------- /1940-maximum-xor-for-each-query/1940-maximum-xor-for-each-query.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] getMaximumXor(int[] nums, int maximumBit) { 3 | int n = nums.length; 4 | int xorr = nums[0]; 5 | int maxXor = (1 << maximumBit) - 1; 6 | 7 | for (int i = 1; i < n; i++) { 8 | xorr ^= nums[i]; 9 | } 10 | 11 | int[] ans = new int[n]; 12 | for (int i = 0; i < n; i++) { 13 | ans[i] = xorr ^ maxXor; 14 | xorr ^= nums[n - 1 - i]; 15 | } 16 | 17 | return ans; 18 | } 19 | } -------------------------------------------------------------------------------- /1951-find-the-winner-of-the-circular-game/1951-find-the-winner-of-the-circular-game.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int findTheWinner(int n, int k) { 3 | int ans = 0; 4 | for (int i = 2; i <= n; i++) { 5 | ans = (ans + k) % i; 6 | } 7 | // add 1 to convert back to 1 indexing 8 | return ans + 1; 9 | } 10 | } -------------------------------------------------------------------------------- /1993-sum-of-all-subset-xor-totals/1993-sum-of-all-subset-xor-totals.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int subsetXORSum(int[] nums) { 3 | return helper(nums, 0, 0); 4 | } 5 | 6 | private int helper(int[] nums, int index, int currentXor) { 7 | // return currentXor when all elements in nums are already considered 8 | if (index == nums.length) return currentXor; 9 | 10 | // calculate sum of xor with current element 11 | int withElement = helper(nums, index + 1, currentXor ^ nums[index]); 12 | 13 | // calculate sum of xor without current element 14 | int withoutElement = helper(nums, index + 1, currentXor); 15 | 16 | // return sum of xors from recursion 17 | return withElement + withoutElement; 18 | } 19 | } -------------------------------------------------------------------------------- /2-add-two-numbers/2-add-two-numbers.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public ListNode addTwoNumbers(ListNode l1, ListNode l2) { 3 | int carry = 0; 4 | ListNode p, dummy = new ListNode(0); 5 | p = dummy; 6 | while (l1 != null || l2 != null || carry != 0) { 7 | if(l1 != null) { 8 | carry += l1.val; 9 | l1 = l1.next; 10 | } 11 | if (l2 != null) { 12 | carry += l2.val; 13 | l2 = l2.next; 14 | } 15 | p.next = new ListNode(carry%10); 16 | carry /= 10; 17 | p = p.next; 18 | } 19 | return dummy.next; 20 | } 21 | } -------------------------------------------------------------------------------- /2-add-two-numbers/NOTES.md: -------------------------------------------------------------------------------- 1 | ``` 2 | public ListNode addTwoNumbers(ListNode l1, ListNode l2) { 3 | int carry = 0; 4 | ListNode p, dummy = new ListNode(0); 5 | p = dummy; 6 | while (l1 != null || l2 != null || carry != 0) { 7 | if (l1 != null) { 8 | carry += l1.val; 9 | l1 = l1.next; 10 | } 11 | if (l2 != null) { 12 | carry += l2.val; 13 | l2 = l2.next; 14 | } 15 | p.next = new ListNode(carry%10); 16 | carry /= 10; 17 | p = p.next; 18 | } 19 | return dummy.next; 20 | } 21 | ``` -------------------------------------------------------------------------------- /20-valid-parentheses/20-valid-parentheses.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean isValid(String s) { 3 | Stack stack = new Stack(); 4 | for (int i=0; i stack = new Stack<>(); 5 | for(int i = 0; i < s.length(); i++) { 6 | char a = s.charAt(i); 7 | if(a == '(' || a == '[' || a == '{') stack.push(a); 8 | else if(stack.empty()) return false; 9 | else if(a == ')' && stack.pop() != '(') return false; 10 | else if(a == ']' && stack.pop() != '[') return false; 11 | else if(a == '}' && stack.pop() != '{') return false; 12 | } 13 | return stack.empty(); 14 | } 15 | } 16 | ``` -------------------------------------------------------------------------------- /2000-minimum-speed-to-arrive-on-time/2000-minimum-speed-to-arrive-on-time.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int minSpeedOnTime(int[] dist, double hour) { 3 | int lo = 1, hi = 10000000, ans = -1; 4 | 5 | while(lo <= hi){ 6 | int mid = lo + (hi - lo) / 2; 7 | if(isPossible(dist, mid, hour)){ 8 | ans = mid; 9 | hi = mid - 1; 10 | } 11 | else lo = mid + 1; 12 | } 13 | return ans; 14 | } 15 | 16 | 17 | private boolean isPossible(int[] dist, int speed, double hour) { 18 | double time = 0.0; 19 | for(int i = 0; i < dist.length; i++) { 20 | time += (i != dist.length - 1) ? Math.ceil((double)dist[i] / speed): ((double)dist[i] / speed); 21 | } 22 | return time <= hour; 23 | } 24 | } -------------------------------------------------------------------------------- /2006-find-the-student-that-will-replace-the-chalk/2006-find-the-student-that-will-replace-the-chalk.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int chalkReplacer(int[] chalk, int initialChalkPieces) { 3 | long totalChalkNeeded = 0; 4 | for (int studentChalkUse : chalk) { 5 | totalChalkNeeded += studentChalkUse; 6 | } 7 | 8 | int remainingChalk = (int)(initialChalkPieces % totalChalkNeeded); 9 | 10 | for (int studentIndex = 0; studentIndex < chalk.length; studentIndex++) { 11 | if (remainingChalk < chalk[studentIndex]) return studentIndex; 12 | remainingChalk -= chalk[studentIndex]; 13 | } 14 | 15 | return 0; 16 | } 17 | } -------------------------------------------------------------------------------- /2035-count-sub-islands/2035-count-sub-islands.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int countSubIslands(int[][] B, int[][] A) { 3 | int m = A.length, n = A[0].length, res = 0; 4 | for (int i = 0; i < m; i++) 5 | for (int j = 0; j < n; j++) 6 | if (A[i][j] == 1) res += dfs(B, A, i, j); 7 | return res; 8 | } 9 | 10 | 11 | private int dfs(int[][] B, int[][] A, int i, int j) { 12 | int m = A.length, n = A[0].length, res = 1; 13 | if (i < 0 || i == m || j < 0 || j == n || A[i][j] == 0) return 1; 14 | A[i][j] = 0; 15 | res &= dfs(B, A, i - 1, j); 16 | res &= dfs(B, A, i + 1, j); 17 | res &= dfs(B, A, i, j - 1); 18 | res &= dfs(B, A, i, j + 1); 19 | return res & B[i][j]; 20 | } 21 | } -------------------------------------------------------------------------------- /2047-find-a-peak-element-ii/2047-find-a-peak-element-ii.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] findPeakGrid(int[][] mat) { 3 | int m = mat.length, n = mat[0].length; 4 | 5 | int low = 0, high = m-1, maxIdx = 0; 6 | while(low < high) { 7 | int mid = (low + high) / 2; 8 | maxIdx = findMax(mat[mid]); 9 | if(mat[mid][maxIdx] < mat[mid+1][maxIdx]) low = mid + 1; 10 | else high = mid; 11 | } 12 | maxIdx = findMax(mat[low]); 13 | return new int[]{low, maxIdx}; 14 | } 15 | 16 | private int findMax(int[] arr) { 17 | int index = 0, max = 0; 18 | for(int i = 0; i < arr.length; i++) { 19 | if(max < arr[i]) { 20 | max = arr[i]; 21 | index = i; 22 | } 23 | } 24 | return index; 25 | } 26 | } -------------------------------------------------------------------------------- /2048-build-array-from-permutation/2048-build-array-from-permutation.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] buildArray(int[] nums) { 3 | int n = nums.length; 4 | 5 | for(int i=0; i=0;j--){ 14 | dp[j] = Math.max(dp[j], dp[j+1] - 1); 15 | } 16 | } 17 | for(int i=0;i stack = new Stack (); 4 | int mismatch = 0; 5 | for (int i = 0; i < s.length (); i++) { 6 | char ch = s.charAt (i); 7 | if (ch == '[') stack.push (ch); 8 | else { 9 | if (!stack.isEmpty ()) stack.pop (); 10 | else mismatch++; 11 | } 12 | } 13 | return (mismatch + 1) / 2; 14 | } 15 | } -------------------------------------------------------------------------------- /21-merge-two-sorted-lists/21-merge-two-sorted-lists.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public ListNode mergeTwoLists(ListNode l1, ListNode l2) { 3 | if (l1 == null) return l2; 4 | if (l2 == null) return l1; 5 | if (l1.val < l2.val) { 6 | l1.next = mergeTwoLists(l1.next, l2); 7 | return l1; 8 | } else { 9 | l2.next = mergeTwoLists(l1, l2.next); 10 | return l2; 11 | } 12 | } 13 | } -------------------------------------------------------------------------------- /21-merge-two-sorted-lists/NOTES.md: -------------------------------------------------------------------------------- 1 | ``` 2 | class Solution { 3 | public ListNode mergeTwoLists(ListNode l1, ListNode l2) { 4 | if (l1 == null) return l2; 5 | if (l2 == null) return l1; 6 | if (l1.val < l2.val) { 7 | l1.next = mergeTwoLists(l1.next, l2); 8 | return l1; 9 | } else { 10 | l2.next = mergeTwoLists(l1, l2.next); 11 | return l2; 12 | } 13 | } 14 | } 15 | ``` -------------------------------------------------------------------------------- /2132-convert-1d-array-into-2d-array/2132-convert-1d-array-into-2d-array.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[][] construct2DArray(int[] original, int m, int n) { 3 | if(original.length != m*n) return new int[0][0]; 4 | 5 | int[][] res = new int[m][n]; 6 | for(int i=0; i map = new HashMap(); 5 | map.put('A', 0); map.put('B', 0); 6 | 7 | for (int i=0; i map.get('B'); 21 | } 22 | } -------------------------------------------------------------------------------- /2160-minimum-operations-to-make-a-uni-value-grid/2160-minimum-operations-to-make-a-uni-value-grid.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int minOperations(int[][] grid, int x) { 3 | List values = new ArrayList<>(); 4 | 5 | for (int[] row : grid) { 6 | for (int val : row) { 7 | values.add(val); 8 | } 9 | } 10 | 11 | Collections.sort(values); 12 | 13 | for (int val : values) { 14 | if (Math.abs(val - values.get(0)) % x != 0) { 15 | return -1; 16 | } 17 | } 18 | 19 | int median = values.get(values.size() / 2); 20 | int operations = 0; 21 | 22 | for (int val : values) { 23 | operations += Math.abs(val - median) / x; 24 | } 25 | 26 | return operations; 27 | } 28 | } -------------------------------------------------------------------------------- /2163-kth-distinct-string-in-an-array/2163-kth-distinct-string-in-an-array.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public String kthDistinct(String[] arr, int k) { 3 | Map counter = new HashMap<>(); 4 | for (String v : arr) { 5 | counter.put(v, counter.getOrDefault(v, 0) + 1); 6 | } 7 | for (String v : arr) { 8 | if (counter.get(v) == 1) { 9 | k--; 10 | if (k == 0) return v; 11 | } 12 | } 13 | return ""; 14 | } 15 | } -------------------------------------------------------------------------------- /2170-count-number-of-maximum-bitwise-or-subsets/2170-count-number-of-maximum-bitwise-or-subsets.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int countMaxOrSubsets(int[] A) { 3 | int max = 0, dp[] = new int[1 << 17]; 4 | dp[0] = 1; 5 | for (int a : A) { 6 | for (int i = max; i >= 0; --i) 7 | dp[i | a] += dp[i]; 8 | max |= a; 9 | } 10 | return dp[max]; 11 | } 12 | } -------------------------------------------------------------------------------- /2181-smallest-index-with-equal-value/2181-smallest-index-with-equal-value.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int smallestEqual(int[] nums) { 3 | for (int i=0; i= k) { 10 | result[i - k] = (int)(current / (2 * k + 1)); 11 | current -= nums[i - (2 * k)]; 12 | } 13 | } 14 | return result; 15 | } 16 | } -------------------------------------------------------------------------------- /2215-finding-3-digit-even-numbers/2215-finding-3-digit-even-numbers.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] findEvenNumbers(int[] digits) { 3 | int[] mpp = new int[10]; 4 | for (int d : digits) mpp[d]++; 5 | List res = new ArrayList<>(); 6 | for (int i = 1; i <= 9; i++) { 7 | if (mpp[i] == 0) continue; 8 | mpp[i]--; 9 | for (int j = 0; j <= 9; j++) { 10 | if (mpp[j] == 0) continue; 11 | mpp[j]--; 12 | for (int k = 0; k <= 8; k += 2) { 13 | if (mpp[k] == 0) continue; 14 | res.add(i*100 + j*10 + k); 15 | } 16 | mpp[j]++; 17 | } 18 | mpp[i]++; 19 | } 20 | return res.stream().mapToInt(Integer::intValue).toArray(); 21 | } 22 | } -------------------------------------------------------------------------------- /2216-delete-the-middle-node-of-a-linked-list/2216-delete-the-middle-node-of-a-linked-list.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public ListNode deleteMiddle(ListNode head) { 3 | if (head.next == null) return null; 4 | ListNode slow = head; 5 | ListNode fast = head.next.next; 6 | 7 | while (fast != null && fast.next != null) { 8 | fast = fast.next.next; 9 | slow = slow.next; 10 | } 11 | slow.next = slow.next.next; 12 | return head; 13 | } 14 | } -------------------------------------------------------------------------------- /2237-longest-palindrome-by-concatenating-two-letter-words/2237-longest-palindrome-by-concatenating-two-letter-words.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int longestPalindrome(String[] words) { 3 | int counter[][] = new int[26][26]; 4 | int ans = 0; 5 | for (String w: words) { 6 | int a = w.charAt(0) - 'a', b = w.charAt(1) - 'a'; 7 | if (counter[b][a] > 0) { 8 | ans += 4; 9 | counter[b][a]--; 10 | } 11 | else counter[a][b]++; 12 | } 13 | for (int i = 0; i < 26; i++) { 14 | if (counter[i][i] > 0) { 15 | ans += 2; 16 | break; 17 | } 18 | } 19 | return ans; 20 | } 21 | } -------------------------------------------------------------------------------- /2248-minimum-cost-of-buying-candies-with-discount/2248-minimum-cost-of-buying-candies-with-discount.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int minimumCost(int[] A) { 3 | Arrays.sort(A); 4 | int res = 0, n = A.length; 5 | for (int i = 0; i < n; ++i) 6 | if (i % 3 != n % 3) 7 | res += A[i]; 8 | return res; 9 | } 10 | } -------------------------------------------------------------------------------- /2249-count-the-hidden-sequences/2249-count-the-hidden-sequences.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int numberOfArrays(int[] differences, int lower, int upper) { 3 | long sum = 0, maxi = 0, mini = 0; 4 | for (int x : differences) { 5 | sum += x; 6 | maxi = Math.max(maxi, sum); 7 | mini = Math.min(mini, sum); 8 | } 9 | return (int)Math.max(0, upper - lower - maxi + mini + 1); 10 | } 11 | } -------------------------------------------------------------------------------- /2255-minimum-swaps-to-group-all-1s-together-ii/2255-minimum-swaps-to-group-all-1s-together-ii.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int minSwaps(int[] nums) { 3 | int ones = 0, x = 0, onesInWindow = 0, i = 0, n = nums.length; 4 | for (int v: nums) 5 | if (v == 1) ones++; 6 | int nums2[] = new int[n * 2]; 7 | for (i = 0; i < n * 2; i++) 8 | nums2[i] = nums[i % n]; 9 | for (i = 0; i < n * 2; i++) { 10 | if (i >= ones && nums2[i - ones] == 1) x--; 11 | if (nums2[i] == 1) x++; 12 | onesInWindow = Math.max(x, onesInWindow); 13 | } 14 | return ones - onesInWindow; 15 | } 16 | } -------------------------------------------------------------------------------- /2262-solving-questions-with-brainpower/2262-solving-questions-with-brainpower.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public long mostPoints(int[][] questions) { 3 | int n = questions.length; 4 | long[] dp = new long[n + 1]; 5 | 6 | for(int i = n - 1; i >= 0; i--){ 7 | dp[i] = Math.max(questions[i][0] + dp[Math.min(n, i + questions[i][1] + 1)], dp[i + 1]); 8 | } 9 | 10 | return dp[0]; 11 | } 12 | } -------------------------------------------------------------------------------- /2263-maximum-running-time-of-n-computers/2263-maximum-running-time-of-n-computers.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public long maxRunTime(int n, int[] batteries) { 3 | long sumPower = 0; 4 | for (int power : batteries) sumPower += power; 5 | long left = 1, right = sumPower / n; 6 | 7 | while (left < right){ 8 | long time = (left+right+1) / 2; 9 | if (check(batteries, n, time)) left = time; 10 | else right = time - 1; 11 | } 12 | return left; 13 | } 14 | 15 | public boolean check(int [] B, int n, long time){ 16 | long sum = 0; 17 | for(int battery: B) { 18 | sum+=Math.min(battery, time); 19 | } 20 | return (sum>=(long)time*n); 21 | } 22 | } -------------------------------------------------------------------------------- /2265-partition-array-according-to-given-pivot/2265-partition-array-according-to-given-pivot.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] pivotArray(int[] nums, int pivot) { 3 | int[] result = new int[nums.length]; 4 | int left = 0, right = nums.length - 1; 5 | 6 | for (int i = 0, j = nums.length - 1; i < nums.length; i++, j--) { 7 | if (nums[i] < pivot) { 8 | result[left] = nums[i]; 9 | left++; 10 | } 11 | 12 | if (nums[j] > pivot) { 13 | result[right] = nums[j]; 14 | right--; 15 | } 16 | } 17 | 18 | while (left <= right) { 19 | result[left] = pivot; 20 | left++; 21 | } 22 | 23 | return result; 24 | } 25 | } -------------------------------------------------------------------------------- /2292-counting-words-with-a-given-prefix/2292-counting-words-with-a-given-prefix.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int prefixCount(String[] words, String pref) { 3 | int count = 0; 4 | for (String word : words) { 5 | if (word.startsWith(pref)) { 6 | count++; 7 | } 8 | } 9 | return count; 10 | } 11 | } -------------------------------------------------------------------------------- /2308-divide-array-into-equal-pairs/2308-divide-array-into-equal-pairs.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean divideArray(int[] nums) { 3 | Map counter = new HashMap<>(); 4 | for (int num : nums) { 5 | counter.put(num, counter.getOrDefault(num, 0) + 1); 6 | } 7 | 8 | for (int count : counter.values()) { 9 | if (count % 2 != 0) return false; 10 | } 11 | 12 | return true; 13 | } 14 | } -------------------------------------------------------------------------------- /2356-largest-combination-with-bitwise-and-greater-than-zero/2356-largest-combination-with-bitwise-and-greater-than-zero.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int largestCombination(int[] candidates) { 3 | int ans = 0; 4 | for (int i = 0; i < 32; i++) { 5 | int cnt = 0; 6 | for (int candidate : candidates) { 7 | if ((candidate & (1 << i)) != 0) cnt++; 8 | } 9 | ans = Math.max(ans, cnt); 10 | } 11 | return ans; 12 | } 13 | } -------------------------------------------------------------------------------- /2358-number-of-ways-to-split-array/2358-number-of-ways-to-split-array.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int waysToSplitArray(int[] nums) { 3 | long leftSum = 0, rightSum = 0; 4 | for (int num : nums) { 5 | rightSum += num; 6 | } 7 | 8 | int validSplits = 0; 9 | for (int i = 0; i < nums.length - 1; i++) { 10 | leftSum += nums[i]; 11 | rightSum -= nums[i]; 12 | if (leftSum >= rightSum) { 13 | validSplits++; 14 | } 15 | } 16 | 17 | return validSplits; 18 | } 19 | } -------------------------------------------------------------------------------- /2394-count-subarrays-with-score-less-than-k/2394-count-subarrays-with-score-less-than-k.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public long countSubarrays(int[] nums, long k) { 3 | long sum = 0, res = 0; 4 | for (int i = 0, j = 0; i < nums.length; ++i) { 5 | sum += nums[i]; 6 | while (sum * (i - j + 1) >= k) 7 | sum -= nums[j++]; 8 | res += i - j + 1; 9 | } 10 | return res; 11 | } 12 | } -------------------------------------------------------------------------------- /2428-equal-row-and-column-pairs/2428-equal-row-and-column-pairs.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int equalPairs(int[][] grid) { 3 | Map cnt = new HashMap<>(); 4 | for (int[] row : grid) { 5 | cnt.merge(Arrays.toString(row), 1, Integer::sum); 6 | } 7 | int pairs = 0; 8 | for (int c = 0, n = grid.length; c < n; ++c) { 9 | int[] col = new int[n]; 10 | for (int r = 0; r < n; ++r) { 11 | col[r] = grid[r][c]; 12 | } 13 | pairs += cnt.getOrDefault(Arrays.toString(col), 0); 14 | } 15 | return pairs; 16 | } 17 | } -------------------------------------------------------------------------------- /2442-number-of-arithmetic-triplets/2442-number-of-arithmetic-triplets.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int arithmeticTriplets(int[] nums, int diff) { 3 | int cnt = 0; 4 | Set seen = new HashSet<>(); 5 | for (int num : nums) { 6 | if (seen.contains(num - diff) && seen.contains(num - diff * 2)) { 7 | ++cnt; 8 | } 9 | seen.add(num); 10 | } 11 | return cnt; 12 | } 13 | } -------------------------------------------------------------------------------- /2448-count-number-of-bad-pairs/2448-count-number-of-bad-pairs.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public long countBadPairs(int[] nums) { 3 | Map bag = new HashMap<>(); 4 | long count = 0; 5 | for (int i = 0; i < nums.length; i++) { 6 | count += -1 + bag.merge(nums[i] - i, 1, Integer::sum); 7 | } 8 | int len = nums.length; 9 | return 1L * len * (len - 1) / 2 - count; 10 | } 11 | } -------------------------------------------------------------------------------- /2456-construct-smallest-number-from-di-string/2456-construct-smallest-number-from-di-string.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public String smallestNumber(String s) { 3 | StringBuilder res = new StringBuilder(), stack = new StringBuilder(); 4 | for (int i = 0; i <= s.length(); i++) { 5 | stack.append((char)('1' + i)); 6 | if (i == s.length() || s.charAt(i) == 'I') { 7 | res.append(stack.reverse()); 8 | stack = new StringBuilder(); 9 | } 10 | } 11 | return res.toString(); 12 | } 13 | } -------------------------------------------------------------------------------- /2463-minimum-recolors-to-get-k-consecutive-black-blocks/2463-minimum-recolors-to-get-k-consecutive-black-blocks.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int minimumRecolors(String blocks, int k) { 3 | int blackCount = 0, ans = Integer.MAX_VALUE; 4 | 5 | for (int i = 0; i < blocks.length(); i++) { 6 | if (i - k >= 0 && blocks.charAt(i - k) == 'B') blackCount--; 7 | if (blocks.charAt(i) == 'B') blackCount++; 8 | ans = Math.min(ans, k - blackCount); 9 | } 10 | 11 | return ans; 12 | } 13 | } -------------------------------------------------------------------------------- /2473-max-sum-of-a-pair-with-equal-sum-of-digits/2473-max-sum-of-a-pair-with-equal-sum-of-digits.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int maximumSum(int[] nums) { 3 | int[] max = new int[82]; 4 | int ans = -1; 5 | for (int x : nums) { 6 | int sum = 0; 7 | int temp = x; 8 | while (temp != 0) { 9 | sum += temp % 10; 10 | temp /= 10; 11 | } 12 | if (max[sum] != 0) { 13 | ans = Math.max(ans, x + max[sum]); 14 | } 15 | max[sum] = Math.max(max[sum], x); 16 | } 17 | return ans; 18 | } 19 | } -------------------------------------------------------------------------------- /2478-longest-nice-subarray/2478-longest-nice-subarray.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int longestNiceSubarray(int[] nums) { 3 | int used = 0, j = 0, res = 0; 4 | for (int i = 0; i < nums.length; ++i) { 5 | while ((used & nums[i]) != 0) 6 | used ^= nums[j++]; 7 | used |= nums[i]; 8 | res = Math.max(res, i - j + 1); 9 | } 10 | return res; 11 | } 12 | } -------------------------------------------------------------------------------- /2488-divide-intervals-into-minimum-number-of-groups/2488-divide-intervals-into-minimum-number-of-groups.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int minGroups(int[][] intervals) { 3 | int res = 0, cur = 0, n = intervals.length, A[][] = new int[n * 2][2]; 4 | for (int i = 0; i < n; ++i) { 5 | A[i * 2] = new int[]{intervals[i][0], 1}; 6 | A[i * 2 + 1] = new int[]{intervals[i][1] + 1, -1}; 7 | } 8 | Arrays.sort(A, Comparator.comparingInt(o -> o[0] * 3 + o[1])); 9 | for (int[] a: A) 10 | res = Math.max(res, cur += a[1]); 11 | return res; 12 | } 13 | } -------------------------------------------------------------------------------- /2493-reverse-odd-levels-of-binary-tree/2493-reverse-odd-levels-of-binary-tree.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public TreeNode reverseOddLevels(TreeNode root) { 3 | helper(root.left, root.right, 1); 4 | return root; 5 | } 6 | 7 | public void helper(TreeNode root, TreeNode node2, int lvl) { 8 | if (root == null || node2 == null) return; 9 | 10 | if (lvl % 2 == 1){ 11 | int temp = root.val; 12 | root.val = node2.val; 13 | node2.val = temp; 14 | } 15 | 16 | helper(root.left, node2.right, lvl + 1); 17 | helper(root.right, node2.left, lvl + 1); 18 | } 19 | } -------------------------------------------------------------------------------- /2502-sort-the-people/2502-sort-the-people.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public String[] sortPeople(String[] names, int[] heights) { 3 | for(int i=0; i b && ((1 << i) & num1) > 0) { 8 | res ^= 1 << i; 9 | a--; 10 | } 11 | if (a < b && ((1 << i) & num1) == 0) { 12 | res ^= 1 << i; 13 | a++; 14 | } 15 | } 16 | return res; 17 | } 18 | } -------------------------------------------------------------------------------- /2527-count-subarrays-with-fixed-bounds/2527-count-subarrays-with-fixed-bounds.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public long countSubarrays(int[] nums, int minK, int maxK) { 3 | long ans = 0; 4 | int leftBound = -1, jmin = -1, jmax = -1; 5 | for(int i = 0; i < nums.length;++i) { 6 | if(nums[i] < minK || nums[i] > maxK) leftBound = i; 7 | if(nums[i] == minK) jmin = i; 8 | if(nums[i] == maxK) jmax = i; 9 | ans += Math.max(0, Math.min(jmax , jmin) - leftBound); 10 | } 11 | return ans; 12 | } 13 | } -------------------------------------------------------------------------------- /2533-bitwise-xor-of-all-pairings/2533-bitwise-xor-of-all-pairings.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int xorAllNums(int[] A, int[] B) { 3 | int x = 0, y = 0; 4 | for (int a: A) 5 | x ^= a; 6 | for (int b: B) 7 | y ^= b; 8 | return (A.length % 2 * y) ^ (B.length % 2 * x); 9 | } 10 | } -------------------------------------------------------------------------------- /2551-apply-operations-to-an-array/2551-apply-operations-to-an-array.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] applyOperations(int[] nums) { 3 | for(int i = 0; i+1 < nums.length; ++i){ 4 | if(nums[i] == nums[i+1]){ //update values of ith and (i+1)th value 5 | nums[i] = 2*nums[i]; 6 | nums[i+1] = 0; 7 | } 8 | } 9 | int i = 0; 10 | for(int n: nums){ // collect all noon zero values at front 11 | if(n != 0) nums[i++] = n; 12 | } 13 | while(i < nums.length) nums[i++] = 0; //set values to zero for remainintg ith element 14 | return nums; 15 | } 16 | } -------------------------------------------------------------------------------- /2562-count-ways-to-build-good-strings/2562-count-ways-to-build-good-strings.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int countGoodStrings(int low, int high, int zero, int one) { 3 | int dp[] = new int[high+1]; 4 | dp[0] = 1; 5 | int ans = 0; int mod = 1000000007; 6 | for(int i=1;i<=high;i++){ 7 | dp[i] = ((i-zero>=0 ? dp[i-zero] : 0) + (i-one>=0 ? dp[i-one] : 0))%mod; 8 | if(i>=low) ans = (ans + dp[i])%mod; 9 | } 10 | return ans; 11 | } 12 | } -------------------------------------------------------------------------------- /2581-divide-players-into-teams-of-equal-skill/2581-divide-players-into-teams-of-equal-skill.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public long dividePlayers(int[] skill) { 3 | Arrays.sort(skill); 4 | 5 | int n = skill.length; 6 | int totalSkill = skill[0] + skill[n - 1]; 7 | long chemistrySum = 0; 8 | 9 | for (int i = 0; i < n / 2; i++) { 10 | if (skill[i] + skill[n - i - 1] != totalSkill) return -1; 11 | chemistrySum += (long) skill[i] * skill[n - i - 1]; 12 | } 13 | 14 | return chemistrySum; // Return total chemistry 15 | } 16 | } -------------------------------------------------------------------------------- /2586-longest-square-streak-in-an-array/2586-longest-square-streak-in-an-array.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int longestSquareStreak(int[] nums) { 3 | Map mp = new HashMap<>(); 4 | Arrays.sort(nums); 5 | int res = -1; 6 | 7 | for (int num : nums) { 8 | int sqrt = (int) Math.sqrt(num); 9 | 10 | if (sqrt * sqrt == num && mp.containsKey(sqrt)) { 11 | mp.put(num, mp.get(sqrt) + 1); 12 | res = Math.max(res, mp.get(num)); 13 | } else { 14 | mp.put(num, 1); 15 | } 16 | } 17 | return res; 18 | } 19 | } -------------------------------------------------------------------------------- /26-remove-duplicates-from-sorted-array/26-remove-duplicates-from-sorted-array.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int removeDuplicates(int[] nums) { 3 | if(nums.length == 0) return 0; 4 | int flag = 0; 5 | for (int i = 1; i count = new HashMap<>(); 5 | for(int i = 0, j = 0; j < A.length; ++j){ 6 | k -= count.getOrDefault(A[j],0); 7 | count.put(A[j],count.getOrDefault(A[j],0)+1); 8 | // Or 9 | /* 10 | count.putIfAbsent(A[j],0); 11 | k -= count.get(A[j]); 12 | count.put(A[j],count.get(A[j])+1); 13 | */ 14 | while(k <= 0){ 15 | count.put(A[i],count.get(A[i])-1); 16 | k += count.get(A[i++]); 17 | } 18 | res += i; 19 | } 20 | return res; 21 | } 22 | } -------------------------------------------------------------------------------- /2640-maximum-number-of-integers-to-choose-from-a-range-i/2640-maximum-number-of-integers-to-choose-from-a-range-i.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int maxCount(int[] banned, int n, int maxSum) { 3 | int[] arr=new int[10001]; 4 | for(int i=0; imaxSum){ 14 | break; 15 | } 16 | cnt++; 17 | } 18 | 19 | return cnt; 20 | } 21 | } -------------------------------------------------------------------------------- /2645-pass-the-pillow/2645-pass-the-pillow.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int passThePillow(int n, int time) { 3 | time = time % ((n - 1) * 2) + 1; 4 | return Math.min(time, n * 2 - time); 5 | } 6 | } -------------------------------------------------------------------------------- /2649-count-total-number-of-colored-cells/2649-count-total-number-of-colored-cells.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public long coloredCells(int n) { 3 | return 1 + 4L * n * (n - 1) / 2; 4 | } 5 | } -------------------------------------------------------------------------------- /2654-count-the-number-of-vowel-strings-in-range/2654-count-the-number-of-vowel-strings-in-range.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int vowelStrings(String[] words, int left, int right) { 3 | int count=0; 4 | while(left<=right) 5 | { 6 | if(vowel(words[left].charAt(0))&&vowel(words[left].charAt(words[left].length()-1))) 7 | count++; 8 | left++; 9 | } 10 | return count; 11 | } 12 | boolean vowel(char ch) 13 | { 14 | if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u') 15 | return true; 16 | else return false; 17 | } 18 | } -------------------------------------------------------------------------------- /2673-maximize-win-from-two-segments/2673-maximize-win-from-two-segments.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int maximizeWin(int[] A, int k) { 3 | int res = 0, n = A.length, j = 0, dp[] = new int[n + 1]; 4 | for (int i = 0; i < n; ++i) { 5 | while (A[i] - A[j] > k) 6 | ++j; 7 | dp[i + 1] = Math.max(dp[i], i - j + 1); 8 | res = Math.max(res, i - j + 1 + dp[j]); 9 | } 10 | return res; 11 | } 12 | } -------------------------------------------------------------------------------- /2681-put-marbles-in-bags/2681-put-marbles-in-bags.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public long putMarbles(int[] weights, int k) { 3 | int n = weights.length; 4 | int[] pairs = new int[n - 1]; 5 | for (int i = 1; i < n; i++) { 6 | pairs[i - 1] = weights[i] + weights[i - 1]; 7 | } 8 | Arrays.sort(pairs); 9 | long minScore = 0; 10 | long maxScore = 0; 11 | for (int i = 0; i < k - 1; i++) { 12 | minScore += pairs[i]; 13 | maxScore += pairs[n - i - 2]; 14 | } 15 | return maxScore - minScore; 16 | } 17 | } -------------------------------------------------------------------------------- /2691-count-vowel-strings-in-ranges/2691-count-vowel-strings-in-ranges.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] vowelStrings(String[] words, int[][] queries) { 3 | int n = words.length; 4 | int[] Prefix = new int[n + 1]; 5 | Set vowels = new HashSet<>(Arrays.asList('a', 'e', 'i', 'o', 'u')); 6 | 7 | for (int i = 0; i < n; i++) { 8 | Prefix[i + 1] = Prefix[i]; 9 | if (vowels.contains(words[i].charAt(0)) && vowels.contains(words[i].charAt(words[i].length() - 1))) { 10 | Prefix[i + 1]++; 11 | } 12 | } 13 | 14 | int[] ANS = new int[queries.length]; 15 | for (int i = 0; i < queries.length; i++) { 16 | ANS[i] = Prefix[queries[i][1] + 1] - Prefix[queries[i][0]]; 17 | } 18 | return ANS; 19 | } 20 | } -------------------------------------------------------------------------------- /2692-take-gifts-from-the-richest-pile/2692-take-gifts-from-the-richest-pile.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public long pickGifts(int[] nums, int k) { 3 | int n = nums.length; 4 | PriorityQueue pq = new PriorityQueue<>((a,b)->(b-a)); // MAX-HEAP 5 | 6 | for(int i = 0; i0){ 12 | pq.add((int)Math.sqrt(pq.remove())); 13 | } 14 | 15 | while(pq.size()>0){ 16 | sum+=pq.remove(); 17 | } 18 | 19 | return sum; 20 | } 21 | } -------------------------------------------------------------------------------- /2699-count-the-number-of-fair-pairs/2699-count-the-number-of-fair-pairs.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public long countFairPairs(int[] nums, int lower, int upper) { 3 | Arrays.sort(nums); 4 | return getPairsLessThan(nums, upper+1)-getPairsLessThan(nums, lower); 5 | } 6 | 7 | public long getPairsLessThan(int nums[], int val) { 8 | int left = 0, right = nums.length-1; 9 | long res = 0; 10 | 11 | while (left <= right) { 12 | if ((nums[left]+nums[right]) 60) ans++; 7 | } 8 | return ans; 9 | } 10 | } -------------------------------------------------------------------------------- /2728-sum-in-a-matrix/2728-sum-in-a-matrix.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int matrixSum(int[][] nums) { 3 | int m = nums.length; 4 | int n = nums[0].length; 5 | int[] A = new int[m]; 6 | for (int i=0; i max) max = nums[i][j]; 14 | } 15 | sum += max; 16 | } 17 | return sum; 18 | } 19 | } -------------------------------------------------------------------------------- /2755-extra-characters-in-a-string/2755-extra-characters-in-a-string.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int minExtraChar(String s, String[] dictionary) { 3 | Set dict = new HashSet<>(); 4 | 5 | for (String word : dictionary) dict.add(word); 6 | 7 | int n = s.length(); 8 | 9 | int[] dp = new int[n + 1]; 10 | for (int i = 1; i <= n; i++) { 11 | dp[i] = dp[i - 1] + 1; 12 | 13 | for (int j = i; j >= 1; j--) { 14 | String sub = s.substring(j - 1, i); 15 | 16 | if (dict.contains(sub)) { 17 | dp[i] = Math.min(dp[i], dp[j - 1]); 18 | } 19 | } 20 | } 21 | 22 | return dp[n]; 23 | } 24 | } -------------------------------------------------------------------------------- /2766-find-the-prefix-common-array-of-two-arrays/2766-find-the-prefix-common-array-of-two-arrays.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] findThePrefixCommonArray(int[] A, int[] B) { 3 | int n = A.length; 4 | int[] res = new int[n],cnt = new int[51]; 5 | for (int i = 0; i < n; ++i) { 6 | res[i] = (++cnt[A[i]] == 2 ? 1 : 0) + (++cnt[B[i]] == 2 ? 1 : 0); 7 | if(i > 0) res[i] += res[i - 1]; 8 | } 9 | return res; 10 | } 11 | } -------------------------------------------------------------------------------- /2785-semi-ordered-permutation/2785-semi-ordered-permutation.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int semiOrderedPermutation(int[] nums) { 3 | int minInd = 0, maxInd = nums.length-1, n = nums.length; 4 | for (int i=0; i st = new Stack<>(); 4 | int n = s.length(); 5 | st.push(s.charAt(0)); 6 | for(int i=1;i set = new HashSet<>(); 4 | int ans = 0; 5 | for (int i=1; set.size() max) max = Math.max(max, num); 7 | } 8 | 9 | for (int num : nums) { 10 | if (num != min && num != max) return num; 11 | } 12 | return -1; 13 | } 14 | } -------------------------------------------------------------------------------- /2845-find-the-value-of-the-partition/2845-find-the-value-of-the-partition.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int findValueOfPartition(int[] nums) { 3 | Arrays.sort(nums); 4 | int min = Integer.MAX_VALUE; 5 | for (int i=1; i set = new HashSet(words.length); 7 | 8 | for (String word : words) { 9 | String reversedWord = sb.append(word).reverse().toString(); 10 | if (set.contains(reversedWord)) count++; 11 | 12 | set.add(word); 13 | sb.setLength(0); 14 | } 15 | 16 | return count; 17 | } 18 | } -------------------------------------------------------------------------------- /2850-construct-the-longest-new-string/2850-construct-the-longest-new-string.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int longestString(int x, int y, int z) { 3 | if (x>y) return 2*(2*y+1+z); 4 | else if (x set = new HashSet(); 5 | for (int i: a) set.add(i); 6 | return set.size(); 7 | } 8 | public int countCompleteSubarrays(int[] nums) { 9 | int n = nums.length; 10 | int count = 0; 11 | int distinct = countDistinct(nums); 12 | for (int i=0; i set = new HashSet(); 14 | for (int j=i; j 4) 4 | head = new ListNode(0, head); 5 | for(ListNode node = head; node != null; node = node.next) { 6 | node.val = (node.val * 2) % 10; 7 | if (node.next != null && node.next.val > 4) 8 | node.val++; 9 | } 10 | return head; 11 | } 12 | } -------------------------------------------------------------------------------- /2872-largest-element-in-an-array-after-merge-operations/2872-largest-element-in-an-array-after-merge-operations.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public long maxArrayValue(int[] nums) { 3 | long sum = 0; 4 | for (int i=nums.length-1; i>=0; i--) { 5 | if (nums[i] <= sum) sum += nums[i]; 6 | else sum = nums[i]; 7 | } 8 | return sum; 9 | } 10 | } -------------------------------------------------------------------------------- /2876-number-of-employees-who-met-the-target/2876-number-of-employees-who-met-the-target.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int numberOfEmployeesWhoMetTarget(int[] hours, int target) { 3 | int count = 0; 4 | for (int i:hours) if (i >= target) count++; 5 | return count; 6 | } 7 | } -------------------------------------------------------------------------------- /2881-split-strings-by-separator/2881-split-strings-by-separator.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public List splitWordsBySeparator(List words, char separator) { 3 | ArrayList ans = new ArrayList(); 4 | for (String str: words) { 5 | String[] temp = str.split("["+separator+"]"); 6 | for(String s:temp){ 7 | if (s.length() >= 1) ans.add(s); 8 | } 9 | } 10 | return ans; 11 | } 12 | } -------------------------------------------------------------------------------- /2886-faulty-keyboard/2886-faulty-keyboard.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public String finalString(String s) { 3 | StringBuilder sb = new StringBuilder(); 4 | for (int i=0; i list) { 3 | int n = list.size(); 4 | int max = Integer.MIN_VALUE; 5 | int count = 0; 6 | for (int i = 0; i < n; i++) { 7 | if (count == 0) max = list.get(i); 8 | if (max == list.get(i)) count++; 9 | else count--; 10 | } 11 | count = 0; 12 | for (int num : list) { 13 | if (num == max) count++; 14 | } 15 | if (count <= (n - count) + 1) return -1; 16 | 17 | count = 0; 18 | for (int i = 0; i < n; i++) { 19 | if (list.get(i) == max) count++; 20 | else count--; 21 | if (count == 1) return i; 22 | } 23 | return -1; 24 | } 25 | } -------------------------------------------------------------------------------- /2891-maximum-beauty-of-an-array-after-applying-operation/2891-maximum-beauty-of-an-array-after-applying-operation.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int maximumBeauty(int[] nums, int k) { 3 | if (nums.length == 1) 4 | return 1; 5 | 6 | int maxBeauty = 0; 7 | int maxValue = 0; 8 | 9 | for (int num : nums) 10 | maxValue = Math.max(maxValue, num); 11 | 12 | int[] count = new int[maxValue + 1]; 13 | 14 | for (int num : nums) { 15 | count[Math.max(num - k, 0)]++; 16 | count[Math.min(num + k + 1, maxValue)]--; 17 | } 18 | 19 | int currentSum = 0; 20 | 21 | for (int val : count) { 22 | currentSum += val; 23 | maxBeauty = Math.max(maxBeauty, currentSum); 24 | } 25 | 26 | return maxBeauty; 27 | } 28 | } -------------------------------------------------------------------------------- /2902-max-pair-sum-in-an-array/2902-max-pair-sum-in-an-array.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int maxSum(int[] nums) { 3 | int max = 0; 4 | for(int i=0; i 0){ 15 | mdig = Math.max(mdig, num%10); 16 | num /= 10; 17 | } 18 | return mdig; 19 | } 20 | } -------------------------------------------------------------------------------- /2903-insert-greatest-common-divisors-in-linked-list/2903-insert-greatest-common-divisors-in-linked-list.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | private int gcd(int a, int b) {return b == 0 ? a : gcd(b, a%b);} 3 | public ListNode insertGreatestCommonDivisors(ListNode head) { 4 | if (head.next == null) return head; 5 | ListNode p1 = head; 6 | ListNode p2 = head.next; 7 | while (p2 != null) { 8 | ListNode temp = new ListNode(gcd(p1.val, p2.val)); 9 | p1.next = temp; 10 | temp.next = p2; 11 | p1 = p1.next.next; 12 | p2 = p2.next; 13 | } 14 | return head; 15 | } 16 | } -------------------------------------------------------------------------------- /2904-sorting-three-groups/2904-sorting-three-groups.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int minimumOperations(List nums) { 3 | int n = nums.size(); 4 | int[] lis = new int[n]; 5 | Arrays.fill(lis,1); 6 | 7 | int maxLis = 1; 8 | for(int i=1;i=nums.get(j)){ 11 | lis[i] = Math.max(lis[i], lis[j]+1); 12 | maxLis = Math.max(maxLis, lis[i]); 13 | } 14 | } 15 | } 16 | return n-maxLis; 17 | } 18 | } -------------------------------------------------------------------------------- /2915-count-of-interesting-subarrays/2915-count-of-interesting-subarrays.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public long countInterestingSubarrays(List nums, int m, int k) { 3 | Map map = new HashMap<>(); 4 | int n = nums.size(); 5 | long ans = 0; 6 | map.put(0,1); 7 | int prefixRemainder = 0; 8 | for(int i=0;i nums, int m) { 3 | if(nums.size() <= 2) return true; 4 | for(int i = 1; i < nums.size(); i++){ 5 | if(nums.get(i) + nums.get(i-1) >= m) return true; 6 | } 7 | return false; 8 | } 9 | } -------------------------------------------------------------------------------- /2917-count-pairs-whose-sum-is-less-than-target/2917-count-pairs-whose-sum-is-less-than-target.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int countPairs(List nums, int target) { 3 | int n = nums.size(), ans = 0; 4 | for (int i=0; i lookup = new HashMap<>(); 4 | int result = 0; 5 | int left = 0; 6 | for(int right = 0; right < s.length(); right++) { 7 | if (lookup.containsKey(s.charAt(right))) { 8 | left = Math.max(left, lookup.get(s.charAt(right))); 9 | } 10 | result = Math.max(result, right - left + 1); 11 | lookup.put(s.charAt(right), right + 1); 12 | } 13 | return result; 14 | } 15 | } -------------------------------------------------------------------------------- /3-longest-substring-without-repeating-characters/NOTES.md: -------------------------------------------------------------------------------- 1 | ``` 2 | class Solution { 3 | public int lengthOfLongestSubstring(String s) { 4 | Map lookup = new HashMap<>(); 5 | int result = 0; 6 | int left = 0; 7 | for(int right = 0; right < s.length(); right++) { 8 | if(lookup.containsKey(s.charAt(right))) { 9 | left = Math.max(left, lookup.get(s.charAt(right))); 10 | } 11 | result = Math.max(result, right - left + 1); 12 | lookup.put(s.charAt(right), right + 1); 13 | } 14 | return result; 15 | } 16 | } 17 | ``` -------------------------------------------------------------------------------- /3000-minimum-absolute-difference-between-elements-with-constraint/3000-minimum-absolute-difference-between-elements-with-constraint.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int minAbsoluteDifference(List nums, int x) { 3 | int ans=Integer.MAX_VALUE; 4 | TreeSet nm=new TreeSet<>(); 5 | int size=nums.size(); 6 | for(int i=x;i set = new HashSet<>(); 5 | 6 | int num = 1; 7 | while (count < n) { 8 | int val = target - num; 9 | if (!set.contains(val)) { 10 | set.add(num); 11 | sum += num; 12 | count++; 13 | } 14 | num++; 15 | } 16 | 17 | return sum; 18 | } 19 | } -------------------------------------------------------------------------------- /3143-longest-unequal-adjacent-groups-subsequence-i/3143-longest-unequal-adjacent-groups-subsequence-i.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public List getLongestSubsequence(String[] words, int[] groups) { 3 | List result = new ArrayList<>(); 4 | int last = -1; 5 | for (int i = 0; i < words.length; i++) { 6 | if (groups[i] != last) { 7 | result.add(words[i]); 8 | last = groups[i]; 9 | } 10 | } 11 | return result; 12 | } 13 | } -------------------------------------------------------------------------------- /3152-maximum-value-of-an-ordered-triplet-ii/3152-maximum-value-of-an-ordered-triplet-ii.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public long maximumTripletValue(int[] A) { 3 | long res = 0, maxa = 0, maxab = 0; 4 | for (int a : A) { 5 | res = Math.max(res, 1L * maxab * a); 6 | maxab = Math.max(maxab, maxa - a); 7 | maxa = Math.max(maxa, a); 8 | } 9 | return res; 10 | } 11 | } -------------------------------------------------------------------------------- /3154-maximum-value-of-an-ordered-triplet-i/3154-maximum-value-of-an-ordered-triplet-i.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public long maximumTripletValue(int[] A) { 3 | long res = 0, maxa = 0, maxab = 0; 4 | for (int a : A) { 5 | res = Math.max(res, 1L * maxab * a); 6 | maxab = Math.max(maxab, maxa - a); 7 | maxa = Math.max(maxa, a); 8 | } 9 | return res; 10 | } 11 | } -------------------------------------------------------------------------------- /3172-divisible-and-non-divisible-sums-difference/3172-divisible-and-non-divisible-sums-difference.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int differenceOfSums(int n, int m) { 3 | int totalSum = n * (n + 1) / 2; 4 | int divisibleSum = m * (n / m) * (n / m + 1); 5 | return totalSum - divisibleSum; 6 | } 7 | } -------------------------------------------------------------------------------- /3189-find-champion-ii/3189-find-champion-ii.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int findChampion(int n, int[][] edges) { 3 | boolean[] isUndefeated = new boolean[n]; 4 | Arrays.fill(isUndefeated, true); 5 | 6 | for (int[] edge : edges) { 7 | int winner = edge[0]; 8 | int loser = edge[1]; 9 | isUndefeated[loser] = false; 10 | } 11 | 12 | int champion = -1; 13 | int championCount = 0; 14 | 15 | for (int team = 0; team < n; team++) { 16 | if (isUndefeated[team]) { 17 | champion = team; 18 | championCount++; 19 | } 20 | } 21 | 22 | return championCount == 1 ? champion : -1; 23 | } 24 | } -------------------------------------------------------------------------------- /3194-find-words-containing-character/3194-find-words-containing-character.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public List findWordsContaining(String[] words, char x) { 3 | List res = new ArrayList<>(); 4 | for (int i = 0; i < words.length; i++) { 5 | if (words[i].chars().filter(ch -> ch == x).count() > 0) 6 | res.add(i); 7 | } 8 | return res; 9 | } 10 | } -------------------------------------------------------------------------------- /3195-separate-black-and-white-balls/3195-separate-black-and-white-balls.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public long minimumSteps(String s) { 3 | int n = s.length(); 4 | long zeroCnt = 0, ans = 0; 5 | for (int i=n-1; i>=0; i--) { 6 | if (s.charAt(i) == '0') zeroCnt++; 7 | else ans += zeroCnt; 8 | } 9 | 10 | return ans; 11 | } 12 | } -------------------------------------------------------------------------------- /3227-find-missing-and-repeated-values/3227-find-missing-and-repeated-values.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] findMissingAndRepeatedValues(int[][] grid) { 3 | int n = grid.length; 4 | int size = n * n; 5 | int[] count = new int[size + 1]; 6 | 7 | for (int i = 0; i < n; i++) { 8 | for (int j = 0; j < n; j++) { 9 | count[grid[i][j]]++; 10 | } 11 | } 12 | 13 | int a = -1, b = -1; 14 | 15 | for (int num = 1; num <= size; num++) { 16 | if (count[num] == 2) { 17 | a = num; 18 | } else if (count[num] == 0) { 19 | b = num; 20 | } 21 | } 22 | 23 | return new int[]{a, b}; 24 | } 25 | } -------------------------------------------------------------------------------- /3291-find-if-array-can-be-sorted/3291-find-if-array-can-be-sorted.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public static boolean canSortArray(final int[] nums) { 3 | short pmax = 0, cmin = 0, cmax = 0; 4 | byte pcnt = 0; 5 | for (final int v : nums) { 6 | final byte ccnt = (byte)Integer.bitCount(v); 7 | if (pcnt == ccnt) { 8 | cmin = cmin > v ? (short)v : cmin; 9 | cmax = cmax < v ? (short)v : cmax; 10 | } 11 | else if (cmin < pmax) return false; 12 | else { 13 | pmax = cmax; 14 | cmin = cmax = (short)v; 15 | pcnt = ccnt; 16 | } 17 | } 18 | return cmin >= pmax; 19 | } 20 | } -------------------------------------------------------------------------------- /3307-find-the-maximum-sum-of-node-values/3307-find-the-maximum-sum-of-node-values.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public long maximumValueSum(int[] nums, int k, int[][] edges) { 3 | long total = 0; 4 | for (int num : nums) { 5 | total += num; 6 | } 7 | 8 | long totalDiff = 0; 9 | long diff; 10 | int positiveCount = 0; 11 | long minAbsDiff = Long.MAX_VALUE; 12 | for (int num : nums) { 13 | diff = (num ^ k) - num; 14 | 15 | if (diff > 0) { 16 | totalDiff += diff; 17 | positiveCount++; 18 | } 19 | minAbsDiff = Math.min(minAbsDiff, Math.abs(diff)); 20 | } 21 | if (positiveCount % 2 == 1) totalDiff -= minAbsDiff; 22 | return total + totalDiff; 23 | } 24 | } -------------------------------------------------------------------------------- /3309-count-prefix-and-suffix-pairs-i/3309-count-prefix-and-suffix-pairs-i.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | private boolean isPrefixAndSuffix(String str1, String str2) { 3 | int n1 = str1.length(), n2 = str2.length(); 4 | if (n1 > n2) { 5 | return false; 6 | } 7 | return str2.substring(0, n1).equals(str1) && str2.substring(n2 - n1).equals(str1); 8 | } 9 | 10 | public int countPrefixSuffixPairs(String[] words) { 11 | int n = words.length, count = 0; 12 | for (int i = 0; i < n; ++i) { 13 | for (int j = i + 1; j < n; ++j) { 14 | if (isPrefixAndSuffix(words[i], words[j])) { 15 | count++; 16 | } 17 | } 18 | } 19 | return count; 20 | } 21 | } -------------------------------------------------------------------------------- /3321-type-of-triangle/3321-type-of-triangle.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Solution { 4 | public String triangleType(int[] nums) { 5 | Arrays.sort(nums); 6 | Set set = new HashSet<>(); 7 | for (int num : nums) set.add(num); 8 | 9 | if (nums[0] + nums[1] <= nums[2]) return "none"; 10 | if (set.size() == 1) return "equilateral"; 11 | if (set.size() == 2) return "isosceles"; 12 | return "scalene"; 13 | } 14 | } -------------------------------------------------------------------------------- /3332-minimum-operations-to-exceed-threshold-value-ii/3332-minimum-operations-to-exceed-threshold-value-ii.java: -------------------------------------------------------------------------------- 1 | 2 | class Solution { 3 | public int minOperations(int[] nums, int k) { 4 | PriorityQueue pq = new PriorityQueue<>(); 5 | for (int num : nums) { 6 | if (num < k) pq.add(num); 7 | } 8 | 9 | int operations = 0; 10 | 11 | while (!pq.isEmpty()) { 12 | int x = pq.poll(); 13 | operations++; 14 | 15 | if (pq.isEmpty()) break; 16 | 17 | int y = pq.poll(); 18 | long newValue = 2L * x + y; 19 | 20 | if (newValue < k) pq.add((int) newValue); 21 | } 22 | 23 | return operations; 24 | } 25 | } -------------------------------------------------------------------------------- /3372-longest-strictly-increasing-or-strictly-decreasing-subarray/3372-longest-strictly-increasing-or-strictly-decreasing-subarray.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int longestMonotonicSubarray(int[] nums) { 3 | int maxLength = 1; 4 | int incLength = 1; 5 | int decLength = 1; 6 | for(int i = 1; i < nums.length; i++) { 7 | if(nums[i] > nums[i - 1]) { 8 | incLength++; 9 | decLength = 1; 10 | } else if(nums[i] < nums[i - 1]) { 11 | decLength++; 12 | incLength = 1; 13 | } else { 14 | incLength = 1; 15 | decLength = 1; 16 | } 17 | maxLength = Math.max(maxLength, Math.max(incLength, decLength)); 18 | } 19 | return maxLength; 20 | } 21 | } -------------------------------------------------------------------------------- /3379-score-of-a-string/3379-score-of-a-string.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int scoreOfString(String s) { 3 | return java.util.stream.IntStream.range(0, s.length() - 1).map(i -> Math.abs(s.charAt(i) - s.charAt(i + 1))).sum(); 4 | } 5 | } -------------------------------------------------------------------------------- /3394-minimum-array-end/3394-minimum-array-end.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public long minEnd(int n, int x) { 3 | long a = x; 4 | while (--n > 0) 5 | a = (a + 1) | x; 6 | return a; 7 | } 8 | } -------------------------------------------------------------------------------- /3429-special-array-i/3429-special-array-i.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean isArraySpecial(int[] nums) { 3 | for(int i = 1; i < nums.length; ++i) { 4 | if(nums[i - 1] % 2 == nums[i] % 2) { 5 | return false; 6 | } 7 | } 8 | 9 | return true; 10 | } 11 | } -------------------------------------------------------------------------------- /3451-string-compression-iii/3451-string-compression-iii.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public String compressedString(String word) { 3 | StringBuilder comp = new StringBuilder(); 4 | int cnt = 1, n = word.length(); 5 | char ch = word.charAt(0); 6 | for (int i = 1; i < n; i++) { 7 | if (word.charAt(i) == ch && cnt < 9) cnt++; 8 | else { 9 | comp.append(cnt).append(ch); 10 | ch = word.charAt(i); 11 | cnt = 1; 12 | } 13 | } 14 | comp.append(cnt).append(ch); 15 | return comp.toString(); 16 | } 17 | } -------------------------------------------------------------------------------- /3455-minimum-length-of-string-after-operations/3455-minimum-length-of-string-after-operations.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int minimumLength(String s) { 3 | int[] charFrequency = new int[26]; 4 | int totalLength = 0; 5 | for (char c : s.toCharArray()) { 6 | charFrequency[c - 'a']++; 7 | } 8 | for (int frequency : charFrequency) { 9 | if (frequency == 0) continue; 10 | if (frequency % 2 == 0) { 11 | totalLength += 2; 12 | } else { 13 | totalLength += 1; 14 | } 15 | } 16 | return totalLength; 17 | } 18 | } -------------------------------------------------------------------------------- /3475-minimum-operations-to-make-binary-array-elements-equal-to-one-i/3475-minimum-operations-to-make-binary-array-elements-equal-to-one-i.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int minOperations(int[] nums) { 3 | int count = 0; 4 | int n = nums.length; 5 | 6 | for (int i = 0; i < n - 2; i++) { 7 | if (nums[i] == 0) { 8 | nums[i] ^= 1; 9 | nums[i + 1] ^= 1; 10 | nums[i + 2] ^= 1; 11 | count++; 12 | } 13 | } 14 | 15 | return (nums[n - 2] == 1 && nums[n - 1] == 1) ? count : -1; 16 | } 17 | } -------------------------------------------------------------------------------- /3483-alternating-groups-ii/3483-alternating-groups-ii.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int numberOfAlternatingGroups(int[] colors, int k) { 3 | int n = colors.length; 4 | int count = 0; 5 | int left = 0; 6 | 7 | for (int right = 0; right < n + k - 1; right++) { 8 | if (right > 0 && colors[right % n] == colors[(right - 1) % n]) { 9 | left = right; 10 | } 11 | 12 | if (right - left + 1 >= k) { 13 | count++; 14 | } 15 | } 16 | 17 | return count; 18 | } 19 | } -------------------------------------------------------------------------------- /3501-delete-nodes-from-linked-list-present-in-array/3501-delete-nodes-from-linked-list-present-in-array.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public ListNode modifiedList(int[] nums, ListNode head) { 3 | int max = -1; 4 | for(int num : nums ){ 5 | max = num > max ? num : max; 6 | } 7 | boolean[] freq = new boolean[max+1]; 8 | 9 | for(int num : nums) freq[num] = true; 10 | 11 | ListNode temp = new ListNode(); 12 | ListNode current = temp; 13 | 14 | while(head != null){ 15 | if(head.val >= freq.length || freq[head.val] == false){ 16 | current.next = head; 17 | current = current.next; 18 | } 19 | head = head.next; 20 | } 21 | 22 | current.next = null; 23 | return temp.next; 24 | } 25 | } -------------------------------------------------------------------------------- /3522-find-the-power-of-k-size-subarrays-i/3522-find-the-power-of-k-size-subarrays-i.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] resultsArray(int[] nums, int k) { 3 | if (nums == null || nums.length < k) { 4 | return new int[0]; 5 | } 6 | 7 | int arr[] = new int[nums.length - k + 1]; 8 | int i=0, j=k-1, a=0; 9 | while (j k) st.set(i); 9 | 10 | return st.cardinality(); 11 | } 12 | } -------------------------------------------------------------------------------- /3629-total-characters-in-string-after-transformations-i/3629-total-characters-in-string-after-transformations-i.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | private final int mod = 1_000_000_007; 3 | public int lengthAfterTransformations(String s, int t) { 4 | int[] dp = new int[t + 26]; 5 | for (int i = 0; i < 26; i++) 6 | dp[i] = 1; 7 | for (int i = 26; i < t + 26; i++) 8 | dp[i] = (dp[i - 26] + dp[i - 25]) % mod; 9 | 10 | int ans = 0; 11 | for (char ch : s.toCharArray()) 12 | ans = (ans + dp[ch - 'a' + t]) % mod; 13 | return ans; 14 | } 15 | } -------------------------------------------------------------------------------- /3639-zero-array-transformation-i/3639-zero-array-transformation-i.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean isZeroArray(int[] nums, int[][] queries) { 3 | int n = nums.length; 4 | int[] diff = new int[n + 1]; 5 | 6 | for (int[] q : queries) { 7 | int l = q[0]; 8 | int r = q[1]; 9 | diff[l]++; 10 | if (r + 1 < n) { 11 | diff[r + 1]--; 12 | } 13 | } 14 | 15 | int cnt = 0; 16 | for (int i = 0; i < n; i++) { 17 | cnt += diff[i]; 18 | if (nums[i] > cnt) { 19 | return false; 20 | } 21 | } 22 | 23 | return true; 24 | } 25 | } -------------------------------------------------------------------------------- /3656-minimum-number-of-operations-to-make-elements-in-array-distinct/3656-minimum-number-of-operations-to-make-elements-in-array-distinct.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int minimumOperations(int[] nums) { 3 | int[] mpp = new int[101]; 4 | for (int i = nums.length - 1; i >= 0; --i) { 5 | if (++mpp[nums[i]] > 1) return (i + 3) / 3; 6 | } 7 | return 0; 8 | } 9 | } -------------------------------------------------------------------------------- /3683-find-the-lexicographically-largest-string-from-the-box-i/3683-find-the-lexicographically-largest-string-from-the-box-i.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public String answerString(String word, int numFriends) { 3 | if (numFriends == 1) return word; 4 | String res = ""; 5 | int length = word.length() - numFriends + 1; 6 | for (int i = 0; i < word.length(); i++) { 7 | String temp; 8 | if (i + length <= word.length()) 9 | temp = word.substring(i, i + length); 10 | else 11 | temp = word.substring(i); 12 | if (temp.compareTo(res) > 0) 13 | res = temp; 14 | } 15 | return res; 16 | } 17 | } -------------------------------------------------------------------------------- /3685-count-subarrays-of-length-three-with-a-condition/3685-count-subarrays-of-length-three-with-a-condition.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int countSubarrays(int[] nums) { 3 | int count = 0; 4 | for (int i = 0; i + 2 < nums.length; i++) { 5 | if (2 * (nums[i] + nums[i + 2]) == nums[i + 1]) { 6 | count++; 7 | } 8 | } 9 | return count; 10 | } 11 | } -------------------------------------------------------------------------------- /4-median-of-two-sorted-arrays/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /9-palindrome-number/9-palindrome-number.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean isPalindrome(int x) { 3 | int rev = 0, temp, main; 4 | main = x; 5 | 6 | while(x>0){ 7 | temp = x % 10; 8 | x = x/10; 9 | rev = rev * 10 + temp; 10 | } 11 | 12 | if(main == rev) { 13 | return true; 14 | } else { 15 | return false; 16 | } 17 | } 18 | } -------------------------------------------------------------------------------- /9-palindrome-number/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ --------------------------------------------------------------------------------