├── 0050-powx-n └── 0050-powx-n.py ├── 0069-sqrtx └── 0069-sqrtx.py ├── 1827-invalid-tweets └── 1827-invalid-tweets.sql ├── 0342-power-of-four └── 0342-power-of-four.py ├── 0182-duplicate-emails └── 0182-duplicate-emails.sql ├── 0461-hamming-distance └── 0461-hamming-distance.py ├── 0948-sort-an-array └── 0948-sort-an-array.py ├── 0584-find-customer-referee └── 0584-find-customer-referee.sql ├── 2058-concatenation-of-array └── 2058-concatenation-of-array.py ├── 0595-big-countries └── 0595-big-countries.sql ├── 1258-article-views-i └── 1258-article-views-i.sql ├── 0169-majority-element └── 0169-majority-element.py ├── 2649-count-total-number-of-colored-cells └── 2649-count-total-number-of-colored-cells.py ├── 3676-smallest-number-with-all-set-bits └── 3676-smallest-number-with-all-set-bits.py ├── 0231-power-of-two ├── 0231-power-of-two.py └── 0231-power-of-two.cpp ├── 0292-nim-game └── 0292-nim-game.cpp ├── 3065-display-the-first-three-rows └── 3065-display-the-first-three-rows.py ├── 1908-recyclable-and-low-fat-products └── 1908-recyclable-and-low-fat-products.sql ├── 1523-count-odd-numbers-in-an-interval-range └── 1523-count-odd-numbers-in-an-interval-range.py ├── 2860-sort-by └── 2860-sort-by.js ├── 3076-get-the-size-of-a-dataframe └── 3076-get-the-size-of-a-dataframe.py ├── 0258-add-digits ├── 0258-add-digits.cpp └── README.md ├── 0476-number-complement └── 0476-number-complement.py ├── 0136-single-number ├── 0136-single-number.py └── 0136-single-number.cpp ├── 0009-palindrome-number ├── 0009-palindrome-number.py ├── 0009-palindrome-number.java └── 0009-palindrome-number.cpp ├── 0389-find-the-difference └── 0389-find-the-difference.py ├── 0191-number-of-1-bits ├── 0191-number-of-1-bits.py └── 0191-number-of-1-bits.cpp ├── 0089-gray-code └── 0089-gray-code.py ├── 2174-next-greater-numerically-balanced-number └── 2174-next-greater-numerically-balanced-number.py ├── 3062-create-a-dataframe-from-list └── 3062-create-a-dataframe-from-list.py ├── 0075-sort-colors ├── 0075-sort-colors.py ├── 0075-sort-colors.cpp └── 0075-sort-colors.java ├── 0041-first-missing-positive └── 0041-first-missing-positive.py ├── 2021-remove-all-occurrences-of-a-substring └── 2021-remove-all-occurrences-of-a-substring.py ├── 0812-rotate-string ├── 0812-rotate-string.java └── 0812-rotate-string.cpp ├── 1364-tuple-with-same-product └── 1364-tuple-with-same-product.py ├── 1817-calculate-money-in-leetcode-bank └── 1817-calculate-money-in-leetcode-bank.py ├── 0125-valid-palindrome └── 0125-valid-palindrome.py ├── 0175-combine-two-tables └── 0175-combine-two-tables.sql ├── 1013-fibonacci-number └── 1013-fibonacci-number.cpp ├── 2747-apply-transform-over-each-element-in-array └── 2747-apply-transform-over-each-element-in-array.js ├── 0172-factorial-trailing-zeroes └── 0172-factorial-trailing-zeroes.py ├── 3432-count-partitions-with-even-sum-difference └── 3432-count-partitions-with-even-sum-difference.py ├── 0190-reverse-bits └── 0190-reverse-bits.cpp ├── 1146-greatest-common-divisor-of-strings └── 1146-greatest-common-divisor-of-strings.py ├── 2308-divide-array-into-equal-pairs └── 2308-divide-array-into-equal-pairs.py ├── 2211-count-collisions-on-a-road ├── 2211-count-collisions-on-a-road.py └── 2211-count-collisions-on-a-road.cpp ├── 0540-single-element-in-a-sorted-array ├── 0540-single-element-in-a-sorted-array.py └── 0540-single-element-in-a-sorted-array.cpp ├── 3512-minimum-operations-to-make-array-sum-divisible-by-k └── 3512-minimum-operations-to-make-array-sum-divisible-by-k.cpp ├── 0067-add-binary ├── 0067-add-binary.py └── README.md ├── 0074-search-a-2d-matrix └── 0074-search-a-2d-matrix.py ├── 0217-contains-duplicate ├── 0217-contains-duplicate.py └── 0217-contains-duplicate.java ├── 0240-search-a-2d-matrix-ii └── 0240-search-a-2d-matrix-ii.py ├── 2448-count-number-of-bad-pairs └── 2448-count-number-of-bad-pairs.py ├── 0001-two-sum └── 0001-two-sum.py ├── 0717-1-bit-and-2-bit-characters └── 0717-1-bit-and-2-bit-characters.cpp ├── 0283-move-zeroes ├── 0283-move-zeroes.py └── 0283-move-zeroes.cpp ├── 1319-unique-number-of-occurrences └── 1319-unique-number-of-occurrences.py ├── 3429-special-array-i └── 3429-special-array-i.py ├── 0027-remove-element ├── 0027-remove-element.py ├── 0027-remove-element.cpp └── 0027-remove-element.java ├── 0151-reverse-words-in-a-string └── 0151-reverse-words-in-a-string.py ├── 0780-max-chunks-to-make-sorted └── 0780-max-chunks-to-make-sorted.py ├── 1610-xor-operation-in-an-array └── 1610-xor-operation-in-an-array.cpp ├── 2323-minimum-bit-flips-to-convert-number ├── 2323-minimum-bit-flips-to-convert-number.py ├── 2323-minimum-bit-flips-to-convert-number.java └── 2323-minimum-bit-flips-to-convert-number.cpp ├── 1468-check-if-n-and-its-double-exist └── 1468-check-if-n-and-its-double-exist.py ├── 1732-minimum-one-bit-operations-to-make-integers-zero └── 1732-minimum-one-bit-operations-to-make-integers-zero.cpp ├── 2533-bitwise-xor-of-all-pairings └── 2533-bitwise-xor-of-all-pairings.java ├── 2809-create-hello-world-function └── 2809-create-hello-world-function.js ├── 3555-final-array-state-after-k-multiplication-operations-i └── 3555-final-array-state-after-k-multiplication-operations-i.py ├── 0122-best-time-to-buy-and-sell-stock-ii └── 0122-best-time-to-buy-and-sell-stock-ii.py ├── 0088-merge-sorted-array └── 0088-merge-sorted-array.py ├── 0189-rotate-array ├── 0189-rotate-array.py └── 0189-rotate-array.cpp ├── 2732-counter └── 2732-counter.js ├── 2095-minimum-number-of-swaps-to-make-the-string-balanced └── 2095-minimum-number-of-swaps-to-make-the-string-balanced.py ├── 3242-count-elements-with-maximum-frequency └── 3242-count-elements-with-maximum-frequency.py ├── 0080-remove-duplicates-from-sorted-array-ii ├── 0080-remove-duplicates-from-sorted-array-ii.py ├── 0080-remove-duplicates-from-sorted-array-ii.java └── 0080-remove-duplicates-from-sorted-array-ii.cpp ├── 2210-find-target-indices-after-sorting-array ├── 2210-find-target-indices-after-sorting-array.py └── 2210-find-target-indices-after-sorting-array.cpp ├── 0371-sum-of-two-integers ├── 0371-sum-of-two-integers.py └── README.md ├── 1528-kids-with-the-greatest-number-of-candies └── 1528-kids-with-the-greatest-number-of-candies.py ├── 1894-merge-strings-alternately └── 1894-merge-strings-alternately.py ├── 2244-number-of-laser-beams-in-a-bank └── 2244-number-of-laser-beams-in-a-bank.py ├── 2746-filter-elements-from-array └── 2746-filter-elements-from-array.js ├── 3190-find-minimum-operations-to-make-all-elements-divisible-by-three └── 3190-find-minimum-operations-to-make-all-elements-divisible-by-three.py ├── 0383-ransom-note └── 0383-ransom-note.py ├── 2761-array-reduce-transformation └── 2761-array-reduce-transformation.js ├── 0162-find-peak-element └── 0162-find-peak-element.py ├── 0055-jump-game ├── 0055-jump-game.java ├── 0055-jump-game.cpp └── 0055-jump-game.py ├── 0643-maximum-average-subarray-i └── 0643-maximum-average-subarray-i.py ├── 0026-remove-duplicates-from-sorted-array ├── 0026-remove-duplicates-from-sorted-array.py ├── 0026-remove-duplicates-from-sorted-array.cpp └── 0026-remove-duplicates-from-sorted-array.java ├── 1018-binary-prefix-divisible-by-5 └── 1018-binary-prefix-divisible-by-5.cpp ├── 2614-maximum-count-of-positive-integer-and-negative-integer ├── 2614-maximum-count-of-positive-integer-and-negative-integer.cpp └── 2614-maximum-count-of-positive-integer-and-negative-integer.py ├── 2692-take-gifts-from-the-richest-pile └── 2692-take-gifts-from-the-richest-pile.py ├── 0185-department-top-three-salaries └── 0185-department-top-three-salaries.sql ├── 0154-find-minimum-in-rotated-sorted-array-ii └── 0154-find-minimum-in-rotated-sorted-array-ii.cpp ├── 1633-minimum-number-of-increments-on-subarrays-to-form-a-target-array └── 1633-minimum-number-of-increments-on-subarrays-to-form-a-target-array.py ├── 0011-container-with-most-water └── 0011-container-with-most-water.py ├── 0153-find-minimum-in-rotated-sorted-array └── 0153-find-minimum-in-rotated-sorted-array.cpp ├── 1458-sort-integers-by-the-number-of-1-bits └── 1458-sort-integers-by-the-number-of-1-bits.py ├── 1700-minimum-time-to-make-rope-colorful └── 1700-minimum-time-to-make-rope-colorful.py ├── 2292-counting-words-with-a-given-prefix └── 2292-counting-words-with-a-given-prefix.cpp ├── 2324-find-triangular-sum-of-an-array ├── 2324-find-triangular-sum-of-an-array.py └── 2324-find-triangular-sum-of-an-array.cpp ├── 0053-maximum-subarray └── 0053-maximum-subarray.cpp ├── 2137-final-value-of-variable-after-performing-operations └── 2137-final-value-of-variable-after-performing-operations.py ├── 0007-reverse-integer ├── 0007-reverse-integer.cpp ├── 0007-reverse-integer.py └── 0007-reverse-integer.java ├── 0121-best-time-to-buy-and-sell-stock └── 0121-best-time-to-buy-and-sell-stock.py ├── 2288-count-operations-to-obtain-zero └── 2288-count-operations-to-obtain-zero.py ├── 1460-number-of-substrings-containing-all-three-characters └── 1460-number-of-substrings-containing-all-three-characters.py ├── 3583-count-special-triplets ├── 3583-count-special-triplets.py └── 3583-count-special-triplets.cpp ├── 0485-max-consecutive-ones ├── 0485-max-consecutive-ones.py └── 0485-max-consecutive-ones.cpp ├── 1392-find-the-difference-of-two-arrays └── 1392-find-the-difference-of-two-arrays.py ├── 3447-clear-digits └── 3447-clear-digits.py ├── 0187-repeated-dna-sequences └── 0187-repeated-dna-sequences.py ├── 3174-minimum-number-of-changes-to-make-binary-string-beautiful └── 3174-minimum-number-of-changes-to-make-binary-string-beautiful.cpp ├── 3195-separate-black-and-white-balls ├── 3195-separate-black-and-white-balls.java └── 3195-separate-black-and-white-balls.cpp ├── 0957-minimum-add-to-make-parentheses-valid └── 0957-minimum-add-to-make-parentheses-valid.py ├── 2580-circular-sentence ├── 2580-circular-sentence.cpp └── 2580-circular-sentence.java ├── 2608-count-the-digits-that-divide-a-number └── 2608-count-the-digits-that-divide-a-number.cpp ├── 3383-taking-maximum-energy-from-the-mystic-dungeon └── 3383-taking-maximum-energy-from-the-mystic-dungeon.py ├── 0882-peak-index-in-a-mountain-array └── 0882-peak-index-in-a-mountain-array.py ├── 2144-maximum-difference-between-increasing-elements └── 2144-maximum-difference-between-increasing-elements.py ├── 0152-maximum-product-subarray ├── 0152-maximum-product-subarray.py └── 0152-maximum-product-subarray.cpp ├── 1642-water-bottles ├── 1642-water-bottles.java └── 1642-water-bottles.cpp ├── 2110-number-of-smooth-descent-periods-of-a-stock └── 2110-number-of-smooth-descent-periods-of-a-stock.py ├── 3018-make-string-a-subsequence-using-cyclic-increments └── 3018-make-string-a-subsequence-using-cyclic-increments.py ├── 0477-total-hamming-distance └── 0477-total-hamming-distance.py ├── 1889-check-if-number-is-a-sum-of-powers-of-three └── 1889-check-if-number-is-a-sum-of-powers-of-three.cpp ├── 1915-check-if-one-string-swap-can-make-strings-equal └── 1915-check-if-one-string-swap-can-make-strings-equal.py ├── 0260-single-number-iii ├── 0260-single-number-iii.py └── 0260-single-number-iii.cpp ├── 1537-maximum-score-after-splitting-a-string └── 1537-maximum-score-after-splitting-a-string.py ├── 2274-keep-multiplying-found-values-by-two └── 2274-keep-multiplying-found-values-by-two.cpp ├── 2870-longest-alternating-subarray └── 2870-longest-alternating-subarray.py ├── 0134-gas-station └── 0134-gas-station.py ├── 1231-replace-elements-with-greatest-element-on-right-side └── 1231-replace-elements-with-greatest-element-on-right-side.py ├── 1015-smallest-integer-divisible-by-k └── 1015-smallest-integer-divisible-by-k.cpp ├── 1353-find-resultant-array-after-removing-anagrams └── 1353-find-resultant-array-after-removing-anagrams.py ├── 1927-maximum-ascending-subarray-sum └── 1927-maximum-ascending-subarray-sum.py ├── 1940-maximum-xor-for-each-query ├── 1940-maximum-xor-for-each-query.cpp └── 1940-maximum-xor-for-each-query.java ├── 0042-trapping-rain-water ├── 0042-trapping-rain-water.c └── 0042-trapping-rain-water.py ├── 1570-final-prices-with-a-special-discount-in-a-shop └── 1570-final-prices-with-a-special-discount-in-a-shop.py ├── 3332-minimum-operations-to-exceed-threshold-value-ii └── 3332-minimum-operations-to-exceed-threshold-value-ii.py ├── 2616-maximal-score-after-applying-k-operations ├── 2616-maximal-score-after-applying-k-operations.cpp └── 2616-maximal-score-after-applying-k-operations.java ├── 3501-delete-nodes-from-linked-list-present-in-array └── 3501-delete-nodes-from-linked-list-present-in-array.py ├── 0204-count-primes ├── 0204-count-primes.cpp └── README.md ├── 0066-plus-one └── 0066-plus-one.cpp ├── 1925-count-square-sum-triples └── 1925-count-square-sum-triples.py ├── 2358-number-of-ways-to-split-array └── 2358-number-of-ways-to-split-array.py ├── 0137-single-number-ii └── 0137-single-number-ii.cpp ├── 0238-product-of-array-except-self └── 0238-product-of-array-except-self.py ├── 0988-flip-equivalent-binary-trees └── 0988-flip-equivalent-binary-trees.java ├── 2356-largest-combination-with-bitwise-and-greater-than-zero ├── 2356-largest-combination-with-bitwise-and-greater-than-zero.java └── 2356-largest-combination-with-bitwise-and-greater-than-zero.cpp ├── 3768-check-if-digits-are-equal-in-string-after-operations-i └── 3768-check-if-digits-are-equal-in-string-after-operations-i.py ├── 0005-longest-palindromic-substring └── 0005-longest-palindromic-substring.py ├── 0123-best-time-to-buy-and-sell-stock-iii └── 0123-best-time-to-buy-and-sell-stock-iii.py ├── 0206-reverse-linked-list └── 0206-reverse-linked-list.py ├── 2265-partition-array-according-to-given-pivot ├── 2265-partition-array-according-to-given-pivot.py └── 2265-partition-array-according-to-given-pivot.cpp ├── 1502-construct-k-palindrome-strings └── 1502-construct-k-palindrome-strings.java ├── 2640-maximum-number-of-integers-to-choose-from-a-range-i └── 2640-maximum-number-of-integers-to-choose-from-a-range-i.py ├── 1566-check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence ├── 1566-check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence.java ├── 1566-check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence.py └── 1566-check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence.cpp ├── 2891-maximum-beauty-of-an-array-after-applying-operation └── 2891-maximum-beauty-of-an-array-after-applying-operation.cpp ├── 2488-divide-intervals-into-minimum-number-of-groups ├── 2488-divide-intervals-into-minimum-number-of-groups.py ├── 2488-divide-intervals-into-minimum-number-of-groups.cpp └── 2488-divide-intervals-into-minimum-number-of-groups.java ├── 2699-count-the-number-of-fair-pairs └── 2699-count-the-number-of-fair-pairs.cpp ├── 3612-adjacent-increasing-subarrays-detection-i └── 3612-adjacent-increasing-subarrays-detection-i.py ├── 2232-adding-spaces-to-a-string └── 2232-adding-spaces-to-a-string.py ├── 3336-water-bottles-ii └── 3336-water-bottles-ii.py ├── 2169-simple-bank-system └── 2169-simple-bank-system.py ├── 2551-apply-operations-to-an-array ├── 2551-apply-operations-to-an-array.py └── 2551-apply-operations-to-an-array.cpp ├── 2147-number-of-ways-to-divide-a-long-corridor └── 2147-number-of-ways-to-divide-a-long-corridor.py ├── 2586-longest-square-streak-in-an-array ├── 2586-longest-square-streak-in-an-array.cpp └── 2586-longest-square-streak-in-an-array.java ├── 3794-find-the-minimum-amount-of-time-to-brew-potions └── 3794-find-the-minimum-amount-of-time-to-brew-potions.py ├── 0078-subsets ├── 0078-subsets.cpp └── 0078-subsets.java ├── 0367-valid-perfect-square └── 0367-valid-perfect-square.cpp ├── 2509-minimize-xor └── 2509-minimize-xor.cpp ├── 3619-adjacent-increasing-subarrays-detection-ii └── 3619-adjacent-increasing-subarrays-detection-ii.py ├── 2188-minimized-maximum-of-products-distributed-to-any-store └── 2188-minimized-maximum-of-products-distributed-to-any-store.cpp ├── 1302-delete-characters-to-make-fancy-string ├── 1302-delete-characters-to-make-fancy-string.cpp └── 1302-delete-characters-to-make-fancy-string.java ├── 2661-smallest-missing-non-negative-integer-after-operations └── 2661-smallest-missing-non-negative-integer-after-operations.py ├── 3577-count-the-number-of-computer-unlocking-permutations └── 3577-count-the-number-of-computer-unlocking-permutations.cpp ├── 2145-grid-game └── 2145-grid-game.cpp ├── 3455-minimum-length-of-string-after-operations └── 3455-minimum-length-of-string-after-operations.py ├── 0306-additive-number └── 0306-additive-number.py ├── 0083-remove-duplicates-from-sorted-list ├── 0083-remove-duplicates-from-sorted-list.py └── 0083-remove-duplicates-from-sorted-list.cpp ├── 0345-reverse-vowels-of-a-string └── 0345-reverse-vowels-of-a-string.py ├── 2141-maximum-running-time-of-n-computers └── 2141-maximum-running-time-of-n-computers.cpp ├── 3623-count-number-of-trapezoids-i └── 3623-count-number-of-trapezoids-i.cpp ├── 0944-delete-columns-to-make-sorted └── 0944-delete-columns-to-make-sorted.cpp ├── 3309-count-prefix-and-suffix-pairs-i └── 3309-count-prefix-and-suffix-pairs-i.py ├── 3381-maximum-subarray-sum-with-length-divisible-by-k └── 3381-maximum-subarray-sum-with-length-divisible-by-k.cpp ├── 2089-maximum-matrix-sum └── 2089-maximum-matrix-sum.cpp ├── 2392-successful-pairs-of-spells-and-potions └── 2392-successful-pairs-of-spells-and-potions.py ├── 0034-find-first-and-last-position-of-element-in-sorted-array └── 0034-find-first-and-last-position-of-element-in-sorted-array.cpp ├── 0954-maximum-sum-circular-subarray └── 0954-maximum-sum-circular-subarray.py ├── 3267-find-longest-special-substring-that-occurs-thrice-i └── 3267-find-longest-special-substring-that-occurs-thrice-i.py ├── 0038-count-and-say └── 0038-count-and-say.cpp ├── 3427-special-array-ii └── 3427-special-array-ii.py ├── 3834-minimum-operations-to-convert-all-elements-to-zero └── 3834-minimum-operations-to-convert-all-elements-to-zero.cpp ├── 3581-the-two-sneaky-numbers-of-digitville └── 3581-the-two-sneaky-numbers-of-digitville.py ├── 2690-house-robber-iv └── 2690-house-robber-iv.py ├── 3493-maximum-number-of-operations-to-move-ones-to-the-end └── 3493-maximum-number-of-operations-to-move-ones-to-the-end.cpp ├── 1476-count-negative-numbers-in-a-sorted-matrix └── 1476-count-negative-numbers-in-a-sorted-matrix.cpp ├── 1477-product-of-the-last-k-numbers └── 1477-product-of-the-last-k-numbers.py ├── 3434-find-the-number-of-distinct-colors-among-the-balls └── 3434-find-the-number-of-distinct-colors-among-the-balls.py ├── 1063-best-sightseeing-pair └── 1063-best-sightseeing-pair.cpp ├── 1895-minimum-number-of-operations-to-move-all-balls-to-each-box └── 1895-minimum-number-of-operations-to-move-all-balls-to-each-box.py ├── 2435-paths-in-matrix-whose-sum-is-divisible-by-k └── 2435-paths-in-matrix-whose-sum-is-divisible-by-k.py ├── 0021-merge-two-sorted-lists ├── 0021-merge-two-sorted-lists.cpp ├── 0021-merge-two-sorted-lists.java └── 0021-merge-two-sorted-lists.py ├── 0443-string-compression └── 0443-string-compression.py ├── 1002-maximum-width-ramp └── 1002-maximum-width-ramp.py ├── 1147-flip-columns-for-maximum-number-of-equal-rows └── 1147-flip-columns-for-maximum-number-of-equal-rows.java ├── 3291-find-if-array-can-be-sorted ├── 3291-find-if-array-can-be-sorted.cpp └── 3291-find-if-array-can-be-sorted.java ├── 0003-longest-substring-without-repeating-characters └── 0003-longest-substring-without-repeating-characters.py ├── 2473-max-sum-of-a-pair-with-equal-sum-of-digits └── 2473-max-sum-of-a-pair-with-equal-sum-of-digits.py ├── 2813-to-be-or-not-to-be └── 2813-to-be-or-not-to-be.js ├── 2625-increment-submatrices-by-one └── 2625-increment-submatrices-by-one.py ├── 0012-integer-to-roman └── 0012-integer-to-roman.cpp ├── 0140-word-break-ii ├── 0140-word-break-ii.java └── 0140-word-break-ii.cpp ├── 0374-guess-number-higher-or-lower └── 0374-guess-number-higher-or-lower.py ├── 0474-ones-and-zeroes └── 0474-ones-and-zeroes.py ├── 1636-number-of-substrings-with-only-1s └── 1636-number-of-substrings-with-only-1s.cpp ├── 0670-maximum-swap ├── 0670-maximum-swap.java ├── README.md └── 0670-maximum-swap.cpp ├── 1878-check-if-array-is-sorted-and-rotated └── 1878-check-if-array-is-sorted-and-rotated.py ├── 3652-best-time-to-buy-and-sell-stock-using-strategy └── 3652-best-time-to-buy-and-sell-stock-using-strategy.py ├── 2170-count-number-of-maximum-bitwise-or-subsets ├── 2170-count-number-of-maximum-bitwise-or-subsets.cpp └── 2170-count-number-of-maximum-bitwise-or-subsets.java ├── 2562-count-ways-to-build-good-strings └── 2562-count-ways-to-build-good-strings.java ├── 3189-find-champion-ii └── 3189-find-champion-ii.cpp ├── 1548-check-if-all-1s-are-at-least-length-k-places-away └── 1548-check-if-all-1s-are-at-least-length-k-places-away.cpp ├── 0029-divide-two-integers ├── 0029-divide-two-integers.py ├── 0029-divide-two-integers.cpp └── 0029-divide-two-integers.java ├── 3522-find-the-power-of-k-size-subarrays-i ├── 3522-find-the-power-of-k-size-subarrays-i.py └── 3522-find-the-power-of-k-size-subarrays-i.cpp ├── 3620-maximum-number-of-distinct-elements-after-operations └── 3620-maximum-number-of-distinct-elements-after-operations.py ├── 0040-combination-sum-ii └── 0040-combination-sum-ii.py ├── 0043-multiply-strings ├── 0043-multiply-strings.java └── 0043-multiply-strings.cpp ├── 0048-rotate-image └── 0048-rotate-image.cpp ├── 0016-3sum-closest └── 0016-3sum-closest.cpp ├── 0820-find-eventual-safe-states └── 0820-find-eventual-safe-states.cpp ├── 0033-search-in-rotated-sorted-array └── 0033-search-in-rotated-sorted-array.py ├── 2054-the-number-of-the-smallest-unoccupied-chair └── 2054-the-number-of-the-smallest-unoccupied-chair.py ├── 1886-minimum-limit-of-balls-in-a-bag └── 1886-minimum-limit-of-balls-in-a-bag.cpp ├── 2059-unique-length-3-palindromic-subsequences └── 2059-unique-length-3-palindromic-subsequences.py ├── 0006-zigzag-conversion └── 0006-zigzag-conversion.cpp ├── 1679-shortest-subarray-to-be-removed-to-make-array-sorted └── 1679-shortest-subarray-to-be-removed-to-make-array-sorted.cpp ├── 3622-maximum-frequency-of-an-element-after-performing-operations-i └── 3622-maximum-frequency-of-an-element-after-performing-operations-i.py ├── 0015-3sum └── 0015-3sum.py ├── 2753-minimum-number-of-operations-to-make-all-array-elements-equal-to-1 └── 2753-minimum-number-of-operations-to-make-all-array-elements-equal-to-1.cpp ├── 0044-wildcard-matching └── 0044-wildcard-matching.cpp ├── 1715-split-a-string-into-the-max-number-of-unique-substrings ├── 1715-split-a-string-into-the-max-number-of-unique-substrings.java └── 1715-split-a-string-into-the-max-number-of-unique-substrings.cpp ├── 1755-defuse-the-bomb └── 1755-defuse-the-bomb.cpp ├── 3394-minimum-array-end └── 3394-minimum-array-end.java ├── 2434-design-a-number-container-system └── 2434-design-a-number-container-system.py ├── 2792-neighboring-bitwise-xor └── 2792-neighboring-bitwise-xor.cpp ├── 1667-find-kth-bit-in-nth-binary-string ├── 1667-find-kth-bit-in-nth-binary-string.java └── 1667-find-kth-bit-in-nth-binary-string.cpp ├── 1524-string-matching-in-an-array └── 1524-string-matching-in-an-array.cpp ├── 3606-coupon-code-validator └── 3606-coupon-code-validator.py ├── 3616-make-array-elements-equal-to-zero └── 3616-make-array-elements-equal-to-zero.py ├── 0165-compare-version-numbers └── 0165-compare-version-numbers.cpp ├── 1972-rotating-the-box └── 1972-rotating-the-box.cpp ├── 0002-add-two-numbers └── 0002-add-two-numbers.py ├── 3372-longest-strictly-increasing-or-strictly-decreasing-subarray └── 3372-longest-strictly-increasing-or-strictly-decreasing-subarray.py ├── 2414-move-pieces-to-obtain-a-string └── 2414-move-pieces-to-obtain-a-string.py ├── 1747-lexicographically-smallest-string-after-applying-operations └── 1747-lexicographically-smallest-string-after-applying-operations.py ├── 3451-string-compression-iii └── 3451-string-compression-iii.cpp ├── 2707-merge-two-2d-arrays-by-summing-values └── 2707-merge-two-2d-arrays-by-summing-values.py ├── 3433-count-mentions-per-user └── 3433-count-mentions-per-user.py ├── 3531-count-covered-buildings └── 3531-count-covered-buildings.cpp ├── 2465-shifting-letters-ii └── 2465-shifting-letters-ii.cpp ├── 3610-find-x-sum-of-all-k-long-subarrays-i └── 3610-find-x-sum-of-all-k-long-subarrays-i.cpp ├── 0494-target-sum └── 0494-target-sum.py ├── 0892-shortest-subarray-with-sum-at-least-k ├── 0892-shortest-subarray-with-sum-at-least-k.cpp └── 0892-shortest-subarray-with-sum-at-least-k.java ├── 2868-continuous-subarrays └── 2868-continuous-subarrays.java ├── 2552-maximum-sum-of-distinct-subarrays-with-length-k └── 2552-maximum-sum-of-distinct-subarrays-with-length-k.cpp ├── 3219-make-lexicographically-smallest-array-by-swapping-elements └── 3219-make-lexicographically-smallest-array-by-swapping-elements.py ├── 3483-alternating-groups-ii └── 3483-alternating-groups-ii.py ├── 0628-maximum-product-of-three-numbers └── README.md ├── 0046-permutations └── 0046-permutations.cpp ├── 3573-best-time-to-buy-and-sell-stock-v └── 3573-best-time-to-buy-and-sell-stock-v.cpp └── 0952-word-subsets └── 0952-word-subsets.py /0050-powx-n/0050-powx-n.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def myPow(self, x: float, n: int) -> float: 3 | return x**n 4 | -------------------------------------------------------------------------------- /0069-sqrtx/0069-sqrtx.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def mySqrt(self, x: int) -> int: 3 | return trunc(x**0.5) 4 | 5 | -------------------------------------------------------------------------------- /1827-invalid-tweets/1827-invalid-tweets.sql: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | select tweet_id 3 | from Tweets 4 | where length(content) > 15 -------------------------------------------------------------------------------- /0342-power-of-four/0342-power-of-four.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isPowerOfFour(self, n: int) -> bool: 3 | return n&(n-1)==0 and n%3==1 4 | -------------------------------------------------------------------------------- /0182-duplicate-emails/0182-duplicate-emails.sql: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | select email from Person 3 | group by email 4 | having count(email)>1 -------------------------------------------------------------------------------- /0461-hamming-distance/0461-hamming-distance.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def hammingDistance(self, x: int, y: int) -> int: 3 | return bin(x ^ y).count('1') 4 | 5 | -------------------------------------------------------------------------------- /0948-sort-an-array/0948-sort-an-array.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def sortArray(self, nums: List[int]) -> List[int]: 3 | nums.sort() 4 | return nums 5 | -------------------------------------------------------------------------------- /0584-find-customer-referee/0584-find-customer-referee.sql: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | select name 3 | from Customer 4 | where referee_id is null or referee_id != '2' -------------------------------------------------------------------------------- /2058-concatenation-of-array/2058-concatenation-of-array.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def getConcatenation(self, nums: List[int]) -> List[int]: 3 | return nums+nums 4 | -------------------------------------------------------------------------------- /0595-big-countries/0595-big-countries.sql: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | select name , population, area 3 | from World 4 | where area >= 3000000 or population >= 25000000 -------------------------------------------------------------------------------- /1258-article-views-i/1258-article-views-i.sql: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | select distinct author_id as id 3 | from Views 4 | where author_id = viewer_id 5 | order by author_id -------------------------------------------------------------------------------- /0169-majority-element/0169-majority-element.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def majorityElement(self, nums: List[int]) -> int: 3 | nums.sort() 4 | return nums[len(nums)//2] 5 | -------------------------------------------------------------------------------- /2649-count-total-number-of-colored-cells/2649-count-total-number-of-colored-cells.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def coloredCells(self, n: int) -> int: 3 | return 1 + n * (n-1) * 2 4 | -------------------------------------------------------------------------------- /3676-smallest-number-with-all-set-bits/3676-smallest-number-with-all-set-bits.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def smallestNumber(self, n: int) -> int: 3 | return 2**n.bit_length()-1 4 | -------------------------------------------------------------------------------- /0231-power-of-two/0231-power-of-two.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isPowerOfTwo(self, n: int) -> bool: 3 | if n==0: 4 | return False 5 | return n&(n-1)==0 6 | 7 | -------------------------------------------------------------------------------- /0292-nim-game/0292-nim-game.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool canWinNim(int n) { 4 | if(n%4==0){ 5 | return false; 6 | } 7 | return true; 8 | } 9 | }; -------------------------------------------------------------------------------- /3065-display-the-first-three-rows/3065-display-the-first-three-rows.py: -------------------------------------------------------------------------------- 1 | import pandas as pd 2 | 3 | def selectFirstRows(employees: pd.DataFrame) -> pd.DataFrame: 4 | return employees.head(3) 5 | -------------------------------------------------------------------------------- /1908-recyclable-and-low-fat-products/1908-recyclable-and-low-fat-products.sql: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | select product_id 3 | from Products 4 | where low_fats = "Y" and recyclable = "Y" 5 | -------------------------------------------------------------------------------- /1523-count-odd-numbers-in-an-interval-range/1523-count-odd-numbers-in-an-interval-range.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def countOdds(self, low: int, high: int) -> int: 3 | return (high + 1) // 2 - (low // 2) -------------------------------------------------------------------------------- /2860-sort-by/2860-sort-by.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {Array} arr 3 | * @param {Function} fn 4 | * @return {Array} 5 | */ 6 | var sortBy = function(arr, fn) { 7 | return arr.sort((a,b) => fn(a) - fn(b)) 8 | }; 9 | -------------------------------------------------------------------------------- /3076-get-the-size-of-a-dataframe/3076-get-the-size-of-a-dataframe.py: -------------------------------------------------------------------------------- 1 | import pandas as pd 2 | 3 | def getDataframeSize(players: pd.DataFrame) -> List[int]: 4 | [r,c] = players.shape 5 | return [r,c] 6 | -------------------------------------------------------------------------------- /0258-add-digits/0258-add-digits.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int addDigits(int num) { 4 | if (num ==0){ 5 | return 0; 6 | } 7 | return (num%9==0) ? 9 : num % 9; 8 | } 9 | }; -------------------------------------------------------------------------------- /0476-number-complement/0476-number-complement.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findComplement(self, num: int) -> int: 3 | result = num.bit_length() 4 | mask = (1< int: 3 | ans = 0 4 | for i in range(len(nums)): 5 | ans ^= nums[i] 6 | return ans 7 | -------------------------------------------------------------------------------- /0009-palindrome-number/0009-palindrome-number.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isPalindrome(self, x: int) -> bool: 3 | n = str(x) 4 | if n == n[::-1]: 5 | return True 6 | else: 7 | return False 8 | -------------------------------------------------------------------------------- /0389-find-the-difference/0389-find-the-difference.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findTheDifference(self, s: str, t: str) -> str: 3 | for i in t: 4 | if s.count(i)!=t.count(i): 5 | return i 6 | 7 | -------------------------------------------------------------------------------- /0191-number-of-1-bits/0191-number-of-1-bits.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def hammingWeight(self, n: int) -> int: 3 | count = 0 4 | while n: 5 | n = n&(n-1) 6 | count += 1 7 | return count 8 | 9 | -------------------------------------------------------------------------------- /0089-gray-code/0089-gray-code.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def grayCode(self, n: int) -> List[int]: 3 | res = [] 4 | total = 1<>1)) 7 | return res 8 | -------------------------------------------------------------------------------- /2174-next-greater-numerically-balanced-number/2174-next-greater-numerically-balanced-number.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def nextBeautifulNumber(self, n: int) -> int: 3 | return next(v for v in count(n+1) if all(starmap(eq,Counter(map(int,str(v))).items()))) -------------------------------------------------------------------------------- /3062-create-a-dataframe-from-list/3062-create-a-dataframe-from-list.py: -------------------------------------------------------------------------------- 1 | import pandas as pd 2 | 3 | def createDataframe(student_data: List[List[int]]) -> pd.DataFrame: 4 | data = pd.DataFrame(student_data , columns = ['student_id','age']) 5 | return data -------------------------------------------------------------------------------- /0075-sort-colors/0075-sort-colors.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def sortColors(self, nums: List[int]) -> None: 3 | nums.sort() 4 | return nums 5 | """ 6 | Do not return anything, modify nums in-place instead. 7 | """ 8 | -------------------------------------------------------------------------------- /0041-first-missing-positive/0041-first-missing-positive.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def firstMissingPositive(self, nums: List[int]) -> int: 3 | seen = set(nums) 4 | i = 1 5 | while i in seen: 6 | i += 1 7 | return i 8 | -------------------------------------------------------------------------------- /2021-remove-all-occurrences-of-a-substring/2021-remove-all-occurrences-of-a-substring.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def removeOccurrences(self, s: str, part: str) -> str: 3 | while part in s: 4 | s = s.replace(part,'',1) 5 | return s 6 | -------------------------------------------------------------------------------- /0812-rotate-string/0812-rotate-string.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean rotateString(String s, String goal) { 3 | if(s.length() != goal.length()){ 4 | return false; 5 | } 6 | return (s+s).contains(goal); 7 | 8 | } 9 | } -------------------------------------------------------------------------------- /0812-rotate-string/0812-rotate-string.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool rotateString(string s, string goal) { 4 | if (s.length() != goal.length()) { 5 | return false; 6 | } 7 | return (s + s).find(goal) != string::npos; 8 | } 9 | }; -------------------------------------------------------------------------------- /0136-single-number/0136-single-number.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int singleNumber(vector& nums) { 4 | int res = 0; 5 | for(int i=0;i int: 3 | product_counts = Counter(a * b for a, b in combinations(nums, 2)) 4 | return 8 * sum(comb(C, 2) for C in product_counts.values()) -------------------------------------------------------------------------------- /1817-calculate-money-in-leetcode-bank/1817-calculate-money-in-leetcode-bank.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def totalMoney(self, n): 3 | weeks, days = divmod(n, 7) 4 | amount = (weeks*(weeks-1)//2)*7 + weeks*28 + (days*(days+1)//2) + weeks*days 5 | return amount -------------------------------------------------------------------------------- /0125-valid-palindrome/0125-valid-palindrome.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isPalindrome(self, s: str) -> bool: 3 | rev = ''.join(char.lower() for char in s if char.isalnum()) 4 | if rev == rev[::-1]: 5 | return True 6 | else: 7 | return False -------------------------------------------------------------------------------- /0175-combine-two-tables/0175-combine-two-tables.sql: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | SELECT 3 | p.firstName, 4 | p.lastName, 5 | a.city, 6 | a.state 7 | FROM 8 | Person p 9 | LEFT JOIN 10 | Address a ON p.personId = a.personId; -------------------------------------------------------------------------------- /1013-fibonacci-number/1013-fibonacci-number.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int fib(int n) { 4 | int a=0,b=1,c; 5 | for(int i=0;i int: 3 | count = 0 4 | div = 5 5 | while n>= div: 6 | count += n // div 7 | div *= 5 8 | return count 9 | -------------------------------------------------------------------------------- /3432-count-partitions-with-even-sum-difference/3432-count-partitions-with-even-sum-difference.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def countPartitions(self, nums: List[int]) -> int: 3 | total = sum(nums) 4 | if total%2!=0: 5 | return 0 6 | return len(nums)-1 7 | -------------------------------------------------------------------------------- /0190-reverse-bits/0190-reverse-bits.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | uint32_t reverseBits(uint32_t n) { 4 | int res = 0; 5 | for(int i=0;i<32;i++){ 6 | res = (res<<1) | (n& 1); 7 | n >>= 1; 8 | } 9 | return res; 10 | } 11 | }; -------------------------------------------------------------------------------- /1146-greatest-common-divisor-of-strings/1146-greatest-common-divisor-of-strings.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def gcdOfStrings(self, str1: str, str2: str) -> str: 3 | if str1+str2 != str2+str1: 4 | return "" 5 | return str1[:gcd(len(str1), len(str2))] 6 | 7 | -------------------------------------------------------------------------------- /2308-divide-array-into-equal-pairs/2308-divide-array-into-equal-pairs.py: -------------------------------------------------------------------------------- 1 | from collections import Counter 2 | class Solution: 3 | def divideArray(self, nums: List[int]) -> bool: 4 | count = Counter(nums) 5 | return all(count %2 == 0 for count in count.values()) 6 | 7 | 8 | -------------------------------------------------------------------------------- /2211-count-collisions-on-a-road/2211-count-collisions-on-a-road.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def countCollisions(self, directions: str) -> int: 3 | directions = directions.lstrip("L") 4 | directions = directions.rstrip("R") 5 | 6 | return directions.count("R") + directions.count("L") -------------------------------------------------------------------------------- /0540-single-element-in-a-sorted-array/0540-single-element-in-a-sorted-array.py: -------------------------------------------------------------------------------- 1 | from collections import Counter 2 | class Solution: 3 | def singleNonDuplicate(self, nums: List[int]) -> int: 4 | count = 0 5 | for num in nums: 6 | count ^= num 7 | return count 8 | -------------------------------------------------------------------------------- /3512-minimum-operations-to-make-array-sum-divisible-by-k/3512-minimum-operations-to-make-array-sum-divisible-by-k.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minOperations(vector& nums, int k) { 4 | int sum = 0; 5 | for(auto x : nums) sum += x; 6 | return sum % k; 7 | } 8 | }; -------------------------------------------------------------------------------- /0067-add-binary/0067-add-binary.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def addBinary(self, a: str, b: str) -> str: 3 | i = int(a,2) 4 | j = int(b,2) 5 | while j: 6 | sum = i^j 7 | carry = (i&j) <<1 8 | i,j = sum,carry 9 | return bin(i)[2:] 10 | -------------------------------------------------------------------------------- /0074-search-a-2d-matrix/0074-search-a-2d-matrix.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def searchMatrix(self, matrix: List[List[int]], target: int) -> bool: 3 | for num in matrix: 4 | for n in num: 5 | if n == target: 6 | return True 7 | return False 8 | -------------------------------------------------------------------------------- /0217-contains-duplicate/0217-contains-duplicate.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def containsDuplicate(self, nums: List[int]) -> bool: 3 | n = len(nums) 4 | arr = set(nums) 5 | if n != len(arr): 6 | return True 7 | else: 8 | return False 9 | 10 | 11 | -------------------------------------------------------------------------------- /0240-search-a-2d-matrix-ii/0240-search-a-2d-matrix-ii.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def searchMatrix(self, matrix: List[List[int]], target: int) -> bool: 3 | for num in matrix: 4 | for i in num: 5 | if i == target: 6 | return True 7 | return False 8 | -------------------------------------------------------------------------------- /2448-count-number-of-bad-pairs/2448-count-number-of-bad-pairs.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def countBadPairs(self, A: List[int]) -> int: 3 | m, ans = defaultdict(int), 0 4 | for i in range(len(A)): 5 | ans += i - m[A[i] - i] 6 | m[A[i] - i] += 1 7 | return ans 8 | 9 | -------------------------------------------------------------------------------- /0001-two-sum/0001-two-sum.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def twoSum(self, nums: List[int], target: int) -> List[int]: 3 | n = len(nums) 4 | for i in range(n): 5 | for j in range(i+1,n): 6 | if nums[i]+nums[j] == target: 7 | return i,j 8 | return {} 9 | -------------------------------------------------------------------------------- /0717-1-bit-and-2-bit-characters/0717-1-bit-and-2-bit-characters.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isOneBitCharacter(vector& bits) { 4 | int n = bits.size(); 5 | int i = 0; 6 | while(i < n - 1) 7 | i += bits[i] + 1; 8 | return i == n - 1; 9 | } 10 | }; -------------------------------------------------------------------------------- /0191-number-of-1-bits/0191-number-of-1-bits.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int hammingWeight(int n) { 4 | int count=0; 5 | while(n!=0){ 6 | if(n&1 !=0){ 7 | count++; 8 | } 9 | n=n/2; 10 | } 11 | return count; 12 | } 13 | }; -------------------------------------------------------------------------------- /0283-move-zeroes/0283-move-zeroes.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def moveZeroes(self, nums: List[int]) -> None: 3 | n = len(nums) 4 | count = 0 5 | for i in range(n): 6 | if nums[i]!=0: 7 | nums[i],nums[count] = nums[count], nums[i] 8 | count+=1 9 | return nums -------------------------------------------------------------------------------- /0540-single-element-in-a-sorted-array/0540-single-element-in-a-sorted-array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int singleNonDuplicate(vector& nums) { 4 | int count =0; 5 | for(int i=0;i bool: 4 | count = Counter(arr) 5 | occurence = list(count.values()) 6 | return len(occurence) == len(set(occurence)) 7 | 8 | -------------------------------------------------------------------------------- /3429-special-array-i/3429-special-array-i.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isArraySpecial(self, nums: List[int]) -> bool: 3 | n = len(nums) 4 | if n == 1: 5 | return True 6 | for i in range(n-1): 7 | if nums[i] & 1 == nums[i+1] & 1 : 8 | return False 9 | return True -------------------------------------------------------------------------------- /0027-remove-element/0027-remove-element.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def removeElement(self, nums: List[int], val: int) -> int: 3 | n = len(nums) 4 | index = 0 5 | for i in range(n): 6 | if nums[i] != val: 7 | nums[index] = nums[i] 8 | index = index + 1 9 | return index -------------------------------------------------------------------------------- /0151-reverse-words-in-a-string/0151-reverse-words-in-a-string.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def reverseWords(self, s: str) -> str: 3 | arr = s.split() 4 | n = len(arr) 5 | temp = [] 6 | for i in range(n-1,-1,-1): 7 | temp.append(arr[i]) 8 | return ' '.join(temp) 9 | 10 | -------------------------------------------------------------------------------- /0780-max-chunks-to-make-sorted/0780-max-chunks-to-make-sorted.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def maxChunksToSorted(self, arr: List[int]) -> int: 3 | count = 0 4 | curr = 0 5 | for i, x in enumerate(arr): 6 | curr = max(curr, x) 7 | if curr == i: 8 | count += 1 9 | return count -------------------------------------------------------------------------------- /1610-xor-operation-in-an-array/1610-xor-operation-in-an-array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int xorOperation(int n, int start) { 4 | int single = 0; 5 | for(int i=0;i int: 3 | res = start ^ goal 4 | count=0 5 | while res >0 : 6 | count+=1 7 | res &= res-1 8 | return count 9 | 10 | -------------------------------------------------------------------------------- /1468-check-if-n-and-its-double-exist/1468-check-if-n-and-its-double-exist.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def checkIfExist(self, arr: List[int]) -> bool: 3 | lis = set() 4 | for num in arr: 5 | if num *2 in lis or (num%2==0 and num//2 in lis): 6 | return True 7 | lis.add(num) 8 | return False -------------------------------------------------------------------------------- /1732-minimum-one-bit-operations-to-make-integers-zero/1732-minimum-one-bit-operations-to-make-integers-zero.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minimumOneBitOperations(int n) { 4 | int res = 0; 5 | while (n) { 6 | res ^= n; 7 | n >>= 1; 8 | } 9 | return res; 10 | } 11 | }; -------------------------------------------------------------------------------- /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 | } -------------------------------------------------------------------------------- /2809-create-hello-world-function/2809-create-hello-world-function.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @return {Function} 3 | */ 4 | var createHelloWorld = function() { 5 | 6 | return function(...args) { 7 | return "Hello World"; 8 | 9 | } 10 | }; 11 | 12 | /** 13 | * const f = createHelloWorld(); 14 | * f(); // "Hello World" 15 | */ -------------------------------------------------------------------------------- /3555-final-array-state-after-k-multiplication-operations-i/3555-final-array-state-after-k-multiplication-operations-i.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def getFinalState(self, nums: List[int], k: int, multiplier: int) -> List[int]: 3 | for _ in range(k): 4 | x=nums.index(min(nums)) 5 | nums[x]*=multiplier 6 | return nums -------------------------------------------------------------------------------- /0122-best-time-to-buy-and-sell-stock-ii/0122-best-time-to-buy-and-sell-stock-ii.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def maxProfit(self, prices: List[int]) -> int: 3 | profit = 0 4 | for i in range(1,len(prices)): 5 | if prices[i] > prices[i-1]: 6 | profit += prices[i]-prices[i-1] 7 | return profit 8 | 9 | -------------------------------------------------------------------------------- /0217-contains-duplicate/0217-contains-duplicate.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean containsDuplicate(int[] nums) { 3 | HashSet temp = new HashSet<>(); 4 | for(int num : nums){ 5 | if(!temp.add(num)){ 6 | return true; 7 | } 8 | } 9 | return false; 10 | } 11 | } -------------------------------------------------------------------------------- /0088-merge-sorted-array/0088-merge-sorted-array.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def merge(self, nums1: List[int], m: int, nums2: List[int], n: int) -> None: 3 | for i in range(len(nums2)): 4 | nums1[m+i] = nums2[i] 5 | nums1.sort() 6 | """ 7 | Do not return anything, modify nums1 in-place instead. 8 | """ 9 | -------------------------------------------------------------------------------- /0189-rotate-array/0189-rotate-array.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def rotate(self, nums: List[int], k: int) -> None: 3 | n = len(nums) 4 | k = k % n 5 | rotated = [0] * n 6 | 7 | for i in range(n): 8 | rotated[(i + k) % n] = nums[i] 9 | 10 | for i in range(n): 11 | nums[i] = rotated[i] -------------------------------------------------------------------------------- /0231-power-of-two/0231-power-of-two.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isPowerOfTwo(int n) { 4 | if(n==0){ 5 | return false; 6 | } 7 | while(n!=1){ 8 | if(n&1!=0){ 9 | return false; 10 | } 11 | n=n/2; 12 | } 13 | return true; 14 | } 15 | }; -------------------------------------------------------------------------------- /2732-counter/2732-counter.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number} n 3 | * @return {Function} counter 4 | */ 5 | var createCounter = function(n) { 6 | 7 | return function() { 8 | return n++; 9 | }; 10 | }; 11 | 12 | /** 13 | * const counter = createCounter(10) 14 | * counter() // 10 15 | * counter() // 11 16 | * counter() // 12 17 | */ -------------------------------------------------------------------------------- /0009-palindrome-number/0009-palindrome-number.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean isPalindrome(int x) { 3 | int val = x; 4 | int car = 0; 5 | while(x>0){ 6 | int temp = x%10; 7 | car = car * 10 + temp; 8 | x= x/10; 9 | } 10 | return (val == car); 11 | 12 | } 13 | } -------------------------------------------------------------------------------- /2095-minimum-number-of-swaps-to-make-the-string-balanced/2095-minimum-number-of-swaps-to-make-the-string-balanced.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def minSwaps(self, s: str) -> int: 3 | ans = 0 4 | for c in s: 5 | if c == '[': 6 | ans += 1 7 | elif ans > 0: 8 | ans -= 1 9 | return (ans + 1) // 2 -------------------------------------------------------------------------------- /2323-minimum-bit-flips-to-convert-number/2323-minimum-bit-flips-to-convert-number.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int minBitFlips(int start, int goal) { 3 | int res = start ^ goal; 4 | int count=0; 5 | while(res>0){ 6 | count++; 7 | res = res & (res-1); 8 | } 9 | return count; 10 | } 11 | } -------------------------------------------------------------------------------- /3242-count-elements-with-maximum-frequency/3242-count-elements-with-maximum-frequency.py: -------------------------------------------------------------------------------- 1 | from collections import Counter 2 | class Solution: 3 | def maxFrequencyElements(self, nums: List[int]) -> int: 4 | freq = Counter(nums) 5 | max_freq = max(freq.values()) 6 | return sum(count for count in freq.values() if count == max_freq) 7 | -------------------------------------------------------------------------------- /0080-remove-duplicates-from-sorted-array-ii/0080-remove-duplicates-from-sorted-array-ii.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def removeDuplicates(self, nums: List[int]) -> int: 3 | k = 2 4 | 5 | for i in range(2, len(nums)): 6 | if nums[i] != nums[k - 2]: 7 | nums[k] = nums[i] 8 | k += 1 9 | 10 | return k -------------------------------------------------------------------------------- /2210-find-target-indices-after-sorting-array/2210-find-target-indices-after-sorting-array.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def targetIndices(self, nums: List[int], target: int) -> List[int]: 3 | res = [] 4 | nums.sort() 5 | for i in range(len(nums)): 6 | if nums[i] == target: 7 | res.append(i) 8 | return res 9 | -------------------------------------------------------------------------------- /2323-minimum-bit-flips-to-convert-number/2323-minimum-bit-flips-to-convert-number.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minBitFlips(int start, int goal) { 4 | int res = start ^ goal; 5 | int count = 0; 6 | while(res>0){ 7 | count++; 8 | res = res&(res-1); 9 | } 10 | return count; 11 | } 12 | }; -------------------------------------------------------------------------------- /0371-sum-of-two-integers/0371-sum-of-two-integers.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def getSum(self, a: int, b: int) -> int: 3 | mask = 0xFFFFFFFF 4 | max_int = 0x7FFFFFF 5 | while b: 6 | carry = (a&b) & mask 7 | a=(a^b) & mask 8 | b = (carry<<1) & mask 9 | return a if a<= max_int else ~(a ^ mask) 10 | -------------------------------------------------------------------------------- /1528-kids-with-the-greatest-number-of-candies/1528-kids-with-the-greatest-number-of-candies.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def kidsWithCandies(self, candies: List[int], extraCandies: int) -> List[bool]: 3 | res = [] 4 | max_num = max(candies) 5 | for num in candies: 6 | res.append(num + extraCandies >= max_num) 7 | return res 8 | -------------------------------------------------------------------------------- /1894-merge-strings-alternately/1894-merge-strings-alternately.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def mergeAlternately(self, word1: str, word2: str) -> str: 3 | mini = min(len(word1), len(word2)) 4 | merge = [word1[i] + word2[i] for i in range (mini) ] 5 | merge.append(word1[mini:]) 6 | merge.append(word2[mini:]) 7 | return ''.join(merge) 8 | -------------------------------------------------------------------------------- /2244-number-of-laser-beams-in-a-bank/2244-number-of-laser-beams-in-a-bank.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def numberOfBeams(self, bank: List[str]) -> int: 3 | beams, prev = 0, 0 4 | for row in bank: 5 | devices = row.count("1") 6 | if devices: 7 | beams += prev * devices 8 | prev = devices 9 | return beams -------------------------------------------------------------------------------- /2746-filter-elements-from-array/2746-filter-elements-from-array.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number[]} arr 3 | * @param {Function} fn 4 | * @return {number[]} 5 | */ 6 | var filter = function(arr, fn) { 7 | let fil=[] 8 | for(let i=0;i int: 3 | count = 0 4 | for i in range(len(nums)): 5 | if nums[i]%3!=0: 6 | count += 1 7 | return count 8 | -------------------------------------------------------------------------------- /0027-remove-element/0027-remove-element.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int removeElement(vector& nums, int val) { 4 | int count = 0; 5 | for(int num:nums){ 6 | if(num != val){ 7 | nums[count] = num; 8 | count ++; 9 | } 10 | } 11 | return count; 12 | 13 | } 14 | }; -------------------------------------------------------------------------------- /0383-ransom-note/0383-ransom-note.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def canConstruct(self, ransomNote: str, magazine: str) -> bool: 3 | if len(ransomNote) > len(magazine): 4 | return False 5 | for c in ransomNote: 6 | if magazine.count(c) < ransomNote.count(c): 7 | return False 8 | return True 9 | 10 | 11 | -------------------------------------------------------------------------------- /2761-array-reduce-transformation/2761-array-reduce-transformation.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number[]} nums 3 | * @param {Function} fn 4 | * @param {number} init 5 | * @return {number} 6 | */ 7 | var reduce = function(nums, fn, init) { 8 | let val = init; 9 | for(const elements of nums){ 10 | val = fn(val,elements); 11 | } 12 | return val; 13 | 14 | }; -------------------------------------------------------------------------------- /0027-remove-element/0027-remove-element.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int removeElement(int[] nums, int val) { 3 | int index = 0; 4 | for (int i = 0; i < nums.length; i++) { 5 | if (nums[i] != val) { 6 | nums[index] = nums[i]; 7 | index++; 8 | } 9 | } 10 | return index; 11 | } 12 | } -------------------------------------------------------------------------------- /0162-find-peak-element/0162-find-peak-element.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findPeakElement(self, nums: List[int]) -> int: 3 | n = len(nums) 4 | l , r = 0,n-1 5 | while l= 0; i--) { 5 | if (targetNumIndex <= i + nums[i]) { 6 | targetNumIndex = i; 7 | } 8 | } 9 | return targetNumIndex == 0; 10 | } 11 | } -------------------------------------------------------------------------------- /0075-sort-colors/0075-sort-colors.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void sortColors(vector& nums) { 4 | int n = nums.size(); 5 | for(int i=0;i nums[j]) { 8 | swap(nums[i],nums[j]); 9 | } 10 | } 11 | } 12 | } 13 | }; -------------------------------------------------------------------------------- /0643-maximum-average-subarray-i/0643-maximum-average-subarray-i.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findMaxAverage(self, nums: List[int], k: int) -> float: 3 | curr_sum = sum(nums[:k]) 4 | max_sum = curr_sum 5 | for i in range(k,len(nums)): 6 | curr_sum += nums[i] - nums[i-k] 7 | max_sum = max(max_sum,curr_sum) 8 | return max_sum / k 9 | -------------------------------------------------------------------------------- /0026-remove-duplicates-from-sorted-array/0026-remove-duplicates-from-sorted-array.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def removeDuplicates(self, nums: List[int]) -> int: 3 | n = len(nums) 4 | index = 1 5 | for i in range(1,n): 6 | if nums[i-1] != nums[i]: 7 | nums[index] = nums[i] 8 | index += 1 9 | return index 10 | -------------------------------------------------------------------------------- /1018-binary-prefix-divisible-by-5/1018-binary-prefix-divisible-by-5.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector prefixesDivBy5(vector& nums) { 4 | int msb=0, n=nums.size(); 5 | vector ans(n); 6 | for(int i=0; i& nums) { 4 | int pos = 0,neg =0; 5 | for(int num:nums){ 6 | pos += num > 0; 7 | neg += num < 0; 8 | } 9 | return max(pos, neg); 10 | } 11 | }; -------------------------------------------------------------------------------- /2692-take-gifts-from-the-richest-pile/2692-take-gifts-from-the-richest-pile.py: -------------------------------------------------------------------------------- 1 | import math 2 | class Solution: 3 | def pickGifts(self, gifts: List[int], k: int) -> int: 4 | i = 0 5 | while i < k: 6 | gifts = sorted(gifts) 7 | sqrt = math.floor(math.sqrt(gifts[-1])) 8 | gifts[-1] = sqrt 9 | i +=1 10 | return sum(gifts) -------------------------------------------------------------------------------- /0055-jump-game/0055-jump-game.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool canJump(vector& nums) { 4 | int targetNumIndex = nums.size() - 1; 5 | for (int i = nums.size() - 2; i >= 0; i--) { 6 | if (targetNumIndex <= i + nums[i]) { 7 | targetNumIndex = i; 8 | } 9 | } 10 | return targetNumIndex == 0; 11 | } 12 | }; -------------------------------------------------------------------------------- /0185-department-top-three-salaries/0185-department-top-three-salaries.sql: -------------------------------------------------------------------------------- 1 | SELECT Department, name AS Employee, salary AS Salary 2 | FROM ( 3 | SELECT e.*, d.name AS Department, 4 | DENSE_RANK() OVER (PARTITION BY e.departmentId ORDER BY e.salary DESC) AS rank1 5 | FROM employee e 6 | INNER JOIN department d ON e.departmentId = d.id 7 | ) AS ranked_data 8 | WHERE rank1 <= 3; 9 | -------------------------------------------------------------------------------- /0154-find-minimum-in-rotated-sorted-array-ii/0154-find-minimum-in-rotated-sorted-array-ii.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findMin(vector& nums) { 4 | int n=nums.size(); 5 | int ans = nums[0]; 6 | for(int i=1;i int: 3 | res = prev = 0 4 | for num in target: 5 | if num > prev : 6 | res += num -prev 7 | prev = num 8 | return res 9 | -------------------------------------------------------------------------------- /0011-container-with-most-water/0011-container-with-most-water.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def maxArea(self, nums: List[int]) -> int: 3 | i,j = 0, len(nums)-1 4 | res = 0 5 | while i& nums) { 4 | int res=1; 5 | for(int i=1;i& nums) { 4 | int n = nums.size(); 5 | int ans = nums[0]; 6 | for(int i=1;i>=1 7 | return count 8 | 9 | def sortByBits(self, arr: List[int]) -> List[int]: 10 | return sorted(arr, key=lambda x: (self.fun(x), x)) 11 | 12 | 13 | -------------------------------------------------------------------------------- /1700-minimum-time-to-make-rope-colorful/1700-minimum-time-to-make-rope-colorful.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def minCost(self, s, cost): 3 | res = max_cost = 0 4 | for i in range(len(s)): 5 | if i > 0 and s[i] != s[i - 1]: 6 | max_cost = 0 7 | res += min(max_cost, cost[i]) 8 | max_cost = max(max_cost, cost[i]) 9 | return res 10 | -------------------------------------------------------------------------------- /2292-counting-words-with-a-given-prefix/2292-counting-words-with-a-given-prefix.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int prefixCount(vector& words, string pref) { 4 | int count = 0; 5 | for (const string& word : words){ 6 | if (word.substr(0, pref.size()) == pref){ 7 | count++; 8 | } 9 | } 10 | return count; 11 | } 12 | }; -------------------------------------------------------------------------------- /2324-find-triangular-sum-of-an-array/2324-find-triangular-sum-of-an-array.py: -------------------------------------------------------------------------------- 1 | from typing import List 2 | 3 | class Solution: 4 | def triangularSum(self, nums: List[int]) -> int: 5 | while len(nums) > 1: 6 | temp = [] 7 | for i in range(len(nums) - 1): 8 | temp.append((nums[i] + nums[i + 1]) % 10) 9 | nums = temp 10 | return nums[0] 11 | -------------------------------------------------------------------------------- /0053-maximum-subarray/0053-maximum-subarray.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxSubArray(vector& nums) { 4 | int res = nums[0]; 5 | int curr_sum = nums[0]; 6 | int n = nums.size(); 7 | for(int i=1;i int: 3 | count = 0 4 | for num in operations: 5 | if "--" in num: 6 | count -= 1 7 | else: 8 | count += 1 9 | return count 10 | -------------------------------------------------------------------------------- /0007-reverse-integer/0007-reverse-integer.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int reverse(int x) { 4 | long rev = 0 ; 5 | while(x!=0){ 6 | int dig = x %10; 7 | rev = rev * 10 + dig; 8 | x/=10; 9 | if(rev > INT_MAX || rev < INT_MIN){ 10 | return 0; 11 | } 12 | } 13 | return (int)rev; 14 | 15 | } 16 | }; -------------------------------------------------------------------------------- /0121-best-time-to-buy-and-sell-stock/0121-best-time-to-buy-and-sell-stock.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def maxProfit(self, prices: List[int]) -> int: 3 | buy_price = prices[0] 4 | profit = 0 5 | for num in prices[1:]: 6 | if buy_price > num : 7 | buy_price = num 8 | profit = max(profit, num-buy_price) 9 | return profit 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /2288-count-operations-to-obtain-zero/2288-count-operations-to-obtain-zero.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def countOperations(self, num1: int, num2: int) -> int: 3 | count = 0 4 | while num1!=0 and num2!=0: 5 | if num1 >= num2: 6 | num1 = num1-num2 7 | elif num2>=num1: 8 | num2= num2-num1 9 | count += 1 10 | return count 11 | -------------------------------------------------------------------------------- /1460-number-of-substrings-containing-all-three-characters/1460-number-of-substrings-containing-all-three-characters.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def numberOfSubstrings(self, s: str) -> int: 3 | n = len(s) 4 | position = [-1] * 3 5 | total = 0 6 | 7 | for i in range(n): 8 | position[ord(s[i]) - ord("a")] = i 9 | total += 1 + min(position) 10 | 11 | return total -------------------------------------------------------------------------------- /3583-count-special-triplets/3583-count-special-triplets.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def specialTriplets(self, nums: List[int]) -> int: 3 | MOD=10**9+7 4 | ans,n=0,len(nums) 5 | pref=Counter() 6 | suff=Counter(nums) 7 | for i in range(n): 8 | suff[nums[i]]-=1 9 | ans=(ans+pref[nums[i]*2]*suff[nums[i]*2])%MOD 10 | pref[nums[i]]+=1 11 | return ans -------------------------------------------------------------------------------- /0485-max-consecutive-ones/0485-max-consecutive-ones.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findMaxConsecutiveOnes(self, nums: List[int]) -> int: 3 | n = len(nums) 4 | count = 0 5 | res = 0 6 | for i in range(n): 7 | if nums[i]==0: 8 | count =0 9 | else: 10 | count += 1 11 | res = max(count , res) 12 | return res 13 | -------------------------------------------------------------------------------- /1392-find-the-difference-of-two-arrays/1392-find-the-difference-of-two-arrays.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findDifference(self, nums1: List[int], nums2: List[int]) -> List[List[int]]: 3 | h1 = set(nums1) 4 | h2 = set(nums2) 5 | for num in nums2: 6 | if num in h1: 7 | h1.remove(num) 8 | h2.discard(num) 9 | return [list(h1),list(h2)] 10 | 11 | -------------------------------------------------------------------------------- /3447-clear-digits/3447-clear-digits.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def clearDigits(self, s: str) -> str: 3 | res = 0 4 | s = list(s) 5 | for i in range(len(s)): 6 | if s[i].isdigit(): 7 | res = max(res - 1, 0) 8 | else: 9 | s[res] = s[i] 10 | res += 1 11 | s = s[:res] 12 | return "".join(s) 13 | 14 | -------------------------------------------------------------------------------- /0080-remove-duplicates-from-sorted-array-ii/0080-remove-duplicates-from-sorted-array-ii.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int removeDuplicates(int[] nums) { 3 | int k = 2; 4 | 5 | for (int i = 2; i < nums.length; i++) { 6 | if (nums[i] != nums[k - 2]) { 7 | nums[k] = nums[i]; 8 | k++; 9 | } 10 | } 11 | 12 | return k; 13 | } 14 | } -------------------------------------------------------------------------------- /0187-repeated-dna-sequences/0187-repeated-dna-sequences.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findRepeatedDnaSequences(self, s: str)-> List[str]: 3 | L=10 4 | seen,res = set(),set() 5 | for i in range(len(s)-L+1): 6 | substr = s[i:i+L] 7 | if substr in seen: 8 | res.add(substr) 9 | else: 10 | seen.add(substr) 11 | return list(res) 12 | -------------------------------------------------------------------------------- /3174-minimum-number-of-changes-to-make-binary-string-beautiful/3174-minimum-number-of-changes-to-make-binary-string-beautiful.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minChanges(const std::string& s) { 4 | int count = 0; 5 | for (int i = 0; i < s.size() - 1; i += 2) { 6 | if (s[i] != s[i + 1]) { 7 | count++; 8 | } 9 | } 10 | return count; 11 | } 12 | }; 13 | #include 14 | 15 | -------------------------------------------------------------------------------- /3195-separate-black-and-white-balls/3195-separate-black-and-white-balls.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public long minimumSteps(String s) { 3 | long swap = 0; 4 | int black = 0; 5 | for (int i = 0; i < s.length(); i++) { 6 | if (s.charAt(i) == '0') 7 | swap += (long) black; 8 | else 9 | black++; 10 | } 11 | return swap; 12 | } 13 | } -------------------------------------------------------------------------------- /0957-minimum-add-to-make-parentheses-valid/0957-minimum-add-to-make-parentheses-valid.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def minAddToMakeValid(self, s: str) -> int: 3 | open_c = close_c = 0 4 | for c in s: 5 | if c == '(': 6 | open_c += 1 7 | elif c == ')' and open_c > 0: 8 | open_c -= 1 9 | else: 10 | close_c += 1 11 | return open_c + close_c -------------------------------------------------------------------------------- /2324-find-triangular-sum-of-an-array/2324-find-triangular-sum-of-an-array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int triangularSum(vector& nums) { 4 | while(nums.size()>1){ 5 | vectorarr; 6 | for(int i=0;i 0){ 7 | int temp = num % 10; 8 | if(val % temp == 0){ 9 | count ++; 10 | } 11 | num /= 10; 12 | } 13 | return count; 14 | } 15 | }; -------------------------------------------------------------------------------- /3383-taking-maximum-energy-from-the-mystic-dungeon/3383-taking-maximum-energy-from-the-mystic-dungeon.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def maximumEnergy(self, energy: List[int], k: int) -> int: 3 | n = len(energy) 4 | dp = [0] * n 5 | result = float('-inf') 6 | for i in range(n - 1, -1, -1): 7 | dp[i] = energy[i] + (dp[i + k] if i + k < n else 0) 8 | result = max(result, dp[i]) 9 | return result -------------------------------------------------------------------------------- /2614-maximum-count-of-positive-integer-and-negative-integer/2614-maximum-count-of-positive-integer-and-negative-integer.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def maximumCount(self, nums: List[int]) -> int: 3 | n = len(nums) 4 | pos = 0 5 | neg = 0 6 | for num in nums: 7 | if num > 0: 8 | pos += 1 9 | if num < 0: 10 | neg += 1 11 | return max(pos,neg) 12 | -------------------------------------------------------------------------------- /3195-separate-black-and-white-balls/3195-separate-black-and-white-balls.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | long long minimumSteps(string s) { 4 | long long swap = 0; 5 | int black = 0; 6 | for (int i = 0; i < s.length(); i++) { 7 | if (s[i] == '0') 8 | swap += (long long) black; 9 | else 10 | black++; 11 | } 12 | return swap; 13 | } 14 | }; -------------------------------------------------------------------------------- /0882-peak-index-in-a-mountain-array/0882-peak-index-in-a-mountain-array.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def peakIndexInMountainArray(self, arr: List[int]) -> int: 3 | n = len(arr) 4 | low , high = 0 , n-1 5 | while low < high : 6 | mid = (low + high) // 2 7 | if arr[mid] < arr[mid+1]: 8 | low = mid+1 9 | else: 10 | high = mid 11 | 12 | return low 13 | -------------------------------------------------------------------------------- /2144-maximum-difference-between-increasing-elements/2144-maximum-difference-between-increasing-elements.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def maximumDifference(self, nums: List[int]) -> int: 3 | n = len(nums) 4 | max_diff = -1 5 | for i in range(n): 6 | for j in range(i+1,n): 7 | if nums[j] > nums[i]: 8 | max_diff = max(max_diff , nums[j]-nums[i]) 9 | return max_diff 10 | -------------------------------------------------------------------------------- /0152-maximum-product-subarray/0152-maximum-product-subarray.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def maxProduct(self, nums: List[int]) -> int: 3 | res = max(nums) 4 | curr_max , curr_min = 1,1 5 | for num in nums: 6 | temp = curr_max *num 7 | curr_max = max(num*curr_max, num*curr_min , num) 8 | curr_min = min(temp ,num*curr_min , num) 9 | res = max(res,curr_max) 10 | return res 11 | -------------------------------------------------------------------------------- /1642-water-bottles/1642-water-bottles.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int numWaterBottles(int numBottles, int numExchange) { 3 | int ans=numBottles; 4 | while(numBottles>=numExchange){ 5 | int newBottles=numBottles/numExchange; 6 | int remBottles= numBottles % numExchange; 7 | ans=ans+newBottles; 8 | numBottles=newBottles+remBottles; 9 | } 10 | return ans; 11 | } 12 | } -------------------------------------------------------------------------------- /2110-number-of-smooth-descent-periods-of-a-stock/2110-number-of-smooth-descent-periods-of-a-stock.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def getDescentPeriods(self, prices: List[int]) -> int: 3 | n = len(prices) 4 | res , prev = 1,1 5 | for i in range(1,n): 6 | if prices[i] == prices[i-1]-1: 7 | prev += 1 8 | else: 9 | prev = 1 10 | res += prev 11 | return res 12 | -------------------------------------------------------------------------------- /3018-make-string-a-subsequence-using-cyclic-increments/3018-make-string-a-subsequence-using-cyclic-increments.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def canMakeSubsequence(self, str1: str, str2: str) -> bool: 3 | i, j = 0, 0 4 | while i < len(str1) and j < len(str2): 5 | if str1[i] == str2[j] or chr((ord(str1[i]) - ord('a') + 1) % 26 + ord('a')) == str2[j]: 6 | j += 1 7 | i += 1 8 | return j == len(str2) -------------------------------------------------------------------------------- /0477-total-hamming-distance/0477-total-hamming-distance.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def totalHammingDistance(self, nums: List[int]) -> int: 3 | res = 0 4 | n = len(nums) 5 | for i in range(32): 6 | count = 0 7 | for num in nums: 8 | if(num>>i)&1: 9 | count += 1 10 | ans = n - count 11 | res += ans * count 12 | return res 13 | 14 | -------------------------------------------------------------------------------- /1642-water-bottles/1642-water-bottles.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numWaterBottles(int numBottles, int numExchange) { 4 | int ans=numBottles; 5 | while(numBottles>=numExchange){ 6 | int newBottles=numBottles/numExchange; 7 | int remBottles= numBottles % numExchange; 8 | ans=ans+newBottles; 9 | numBottles=newBottles+remBottles; 10 | } 11 | return ans; 12 | } 13 | }; -------------------------------------------------------------------------------- /1889-check-if-number-is-a-sum-of-powers-of-three/1889-check-if-number-is-a-sum-of-powers-of-three.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool checkPowersOfThree(int n) { 4 | for(int i=15 ; i >= 0; i--){ 5 | int x = pow(3,i); 6 | if(x <= n){ 7 | n -= x; 8 | } 9 | if(n == 0) { 10 | return 1; 11 | } 12 | } 13 | return 0; 14 | 15 | } 16 | }; -------------------------------------------------------------------------------- /1915-check-if-one-string-swap-can-make-strings-equal/1915-check-if-one-string-swap-can-make-strings-equal.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def areAlmostEqual(self, s1: str, s2: str) -> bool: 3 | temp = [] 4 | for a,b in zip(s1,s2): 5 | if a != b: 6 | temp.append(a) 7 | temp.append(b) 8 | a, b, c, d, e = (temp + [''] * 5)[:5] 9 | return not a or (not e and a == d and b == c) 10 | 11 | -------------------------------------------------------------------------------- /0260-single-number-iii/0260-single-number-iii.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def singleNumber(self, nums: List[int]) -> List[int]: 3 | x=nums[0] 4 | for i in range(1,len(nums)): 5 | x^=nums[i] 6 | k=(x&(~(x-1))) 7 | res,ans=0,0 8 | for i in range(len(nums)): 9 | if nums[i]&k!=0: 10 | res^=nums[i] 11 | else: 12 | ans^=nums[i] 13 | return [res,ans] 14 | -------------------------------------------------------------------------------- /1537-maximum-score-after-splitting-a-string/1537-maximum-score-after-splitting-a-string.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def maxScore(self, s: str) -> int: 3 | n = len(s) 4 | zero = 0 5 | one = s.count("1") 6 | res = 0 7 | for i in range(n-1): 8 | if s[i] == "0": 9 | zero += 1 10 | else: 11 | one -= 1 12 | res = max(res,zero + one) 13 | return res 14 | -------------------------------------------------------------------------------- /2210-find-target-indices-after-sorting-array/2210-find-target-indices-after-sorting-array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector targetIndices(vector& nums, int target) { 4 | sort(nums.begin(), nums.end()); 5 | vector ans; 6 | for (int i = 0; i < nums.size(); i++) { 7 | if (nums[i] == target) { 8 | ans.push_back(i); 9 | } 10 | } 11 | return ans; 12 | } 13 | }; 14 | -------------------------------------------------------------------------------- /2274-keep-multiplying-found-values-by-two/2274-keep-multiplying-found-values-by-two.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findFinalValue(vector& nums, int k) { 4 | int bits = 0; 5 | for (auto& n : nums) { 6 | if (n % k != 0) continue; 7 | n = n / k; 8 | if ((n & (n - 1)) == 0) 9 | bits |= n; 10 | } 11 | int d = bits + 1; 12 | return k * (d & -d); 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /2870-longest-alternating-subarray/2870-longest-alternating-subarray.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def alternatingSubarray(self, nums: List[int]) -> int: 3 | n = len(nums) 4 | res = ans = -1 5 | for i in range(1,n): 6 | if ans > 0 and nums[i]== nums[i-2]: 7 | ans += 1 8 | else: 9 | ans = 2 if nums[i] == nums[i-1] + 1 else -1 10 | res = max(res,ans) 11 | return res 12 | -------------------------------------------------------------------------------- /0134-gas-station/0134-gas-station.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def canCompleteCircuit(self, gas: List[int], cost: List[int]) -> int: 3 | n = len(gas) 4 | if sum(gas) - sum(cost) < 0: 5 | return -1 6 | curr_gas = 0 7 | start = 0 8 | for i in range(n): 9 | curr_gas += gas[i] - cost[i] 10 | if curr_gas < 0: 11 | curr_gas = 0 12 | start = i+1 13 | return start 14 | -------------------------------------------------------------------------------- /1231-replace-elements-with-greatest-element-on-right-side/1231-replace-elements-with-greatest-element-on-right-side.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def replaceElements(self, arr: List[int]) -> List[int]: 3 | n = len(arr) 4 | if n==0: 5 | return -1 6 | res = -1 7 | for i in range(n-1,-1,-1): 8 | curr = arr[i] 9 | arr[i] = res 10 | if curr> res: 11 | res = curr 12 | return arr 13 | -------------------------------------------------------------------------------- /1015-smallest-integer-divisible-by-k/1015-smallest-integer-divisible-by-k.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int smallestRepunitDivByK(int k) { 4 | if(k % 2 == 0 || k % 5 == 0) 5 | return -1; 6 | int rem = 1 % k; 7 | int len = 1; 8 | while(rem != 0){ 9 | rem = ((rem * 10) + 1) % k; 10 | len++; 11 | 12 | if(len > k) 13 | return -1; 14 | } 15 | return len; 16 | } 17 | }; -------------------------------------------------------------------------------- /1353-find-resultant-array-after-removing-anagrams/1353-find-resultant-array-after-removing-anagrams.py: -------------------------------------------------------------------------------- 1 | from collections import Counter 2 | class Solution: 3 | def removeAnagrams(self, words: List[str]) -> List[str]: 4 | res =[ words[0]] 5 | n = len(words) 6 | for i in range(1,n): 7 | ans , sol = Counter(words[i-1]) , Counter(words[i]) 8 | if ans != sol: 9 | res.append(words[i]) 10 | return res 11 | 12 | -------------------------------------------------------------------------------- /1927-maximum-ascending-subarray-sum/1927-maximum-ascending-subarray-sum.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def maxAscendingSum(self, nums: List[int]) -> int: 3 | n = len(nums) 4 | max_sum = 0 5 | curr_sum = nums[0] 6 | for i in range(1,n): 7 | if nums[i] <= nums[i-1]: 8 | max_sum = max(max_sum, curr_sum) 9 | curr_sum = 0 10 | curr_sum += nums[i] 11 | return max(max_sum , curr_sum) 12 | 13 | -------------------------------------------------------------------------------- /1940-maximum-xor-for-each-query/1940-maximum-xor-for-each-query.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector getMaximumXor(vector& nums, int maximumBit) { 4 | int n = nums.size(),xorr = nums[0],maxxorr = pow(2,maximumBit)-1; 5 | for(int i=1;ians(n); 7 | for(int i=0;i=r) return 0; 10 | int col=0; 11 | for( ; l<=r; l++){ 12 | col+=D[l]!='S'; 13 | } 14 | return col; 15 | } 16 | }; -------------------------------------------------------------------------------- /2580-circular-sentence/2580-circular-sentence.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean isCircularSentence(String sentence) { 3 | int n = sentence.length(); 4 | if (sentence.charAt(0) != sentence.charAt(n-1)) return false; 5 | for (int i = 1; i < n-1; i++) { 6 | if (sentence.charAt(i) == ' ') { 7 | if (sentence.charAt(i-1) != sentence.charAt(i+1)) return false; 8 | } 9 | } 10 | return true; 11 | } 12 | } -------------------------------------------------------------------------------- /0042-trapping-rain-water/0042-trapping-rain-water.c: -------------------------------------------------------------------------------- 1 | int trap(int* height, int n) { 2 | int res=0; 3 | int lmax[n],rmax[n]; 4 | lmax[0]=height[0]; 5 | for(int i=1;i=0;i--){ 10 | rmax[i]=fmax(height[i],rmax[i+1]); 11 | } 12 | for(int i=1;i& nums) { 4 | int n = nums.size(); 5 | int count = 0 ; 6 | int res= 0 ; 7 | for(int i=0;i List[int]: 3 | n = len(prices) 4 | ans = prices[:] 5 | res = [] 6 | for i in range(n): 7 | while res and prices[res[-1]] >= prices[i]: 8 | j = res.pop() 9 | ans[j] -= prices[i] 10 | res.append(i) 11 | return ans 12 | -------------------------------------------------------------------------------- /3332-minimum-operations-to-exceed-threshold-value-ii/3332-minimum-operations-to-exceed-threshold-value-ii.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def minOperations(self, nums: List[int], k: int) -> int: 3 | heapq.heapify(nums) 4 | count = 0 5 | while nums[0] < k: 6 | x = heapq.heappop(nums) 7 | y = heapq.heappop(nums) 8 | heapq.heappush(nums, min(x, y) * 2 + max(x, y)) 9 | count += 1 10 | return count 11 | 12 | 13 | -------------------------------------------------------------------------------- /2616-maximal-score-after-applying-k-operations/2616-maximal-score-after-applying-k-operations.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | long long maxKelements(vector& nums, int k) { 4 | priority_queue pq; 5 | for(auto &i: nums)pq.push(i); 6 | long long ans = 0,x; 7 | while(k--){ 8 | x = pq.top(); 9 | pq.pop(); 10 | ans += x; 11 | pq.push(ceil(x/3.0)); 12 | } 13 | return ans; 14 | } 15 | }; -------------------------------------------------------------------------------- /3501-delete-nodes-from-linked-list-present-in-array/3501-delete-nodes-from-linked-list-present-in-array.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def modifiedList(self, nums, head): 3 | mpp = set(nums) 4 | while head and head.val in mpp: 5 | head = head.next 6 | curr = head 7 | while curr and curr.next: 8 | while curr.next and curr.next.val in mpp: 9 | curr.next = curr.next.next 10 | curr = curr.next 11 | return head -------------------------------------------------------------------------------- /0204-count-primes/0204-count-primes.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int countPrimes(int n) { 4 | vectorv(n+1,1); 5 | int count = 0; 6 | for(long long i=2;i<=sqrt(n);i++){ 7 | if(v[i]){ 8 | for(long long j = i*i;j plusOne(vector& digits) { 4 | for(int i=digits.size()-1;i>=0;i--){ 5 | if(digits[i]+1 !=10){ 6 | digits[i]+=1; 7 | return digits; 8 | } 9 | digits[i]=0; 10 | if(i==0){ 11 | digits.insert(digits.begin(),1); 12 | return digits; 13 | } 14 | } 15 | return digits; 16 | } 17 | }; -------------------------------------------------------------------------------- /0009-palindrome-number/0009-palindrome-number.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isPal(const string &s, int start, int end) { 4 | if (start >= end){ 5 | return true; 6 | } 7 | return (s[start] == s[end]) && isPal(s, start + 1, end - 1); 8 | } 9 | 10 | bool isPalindrome(int x) { 11 | if (x < 0){ 12 | return false; 13 | } 14 | string s = to_string(x); 15 | return isPal(s, 0, s.size() - 1); 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /0080-remove-duplicates-from-sorted-array-ii/0080-remove-duplicates-from-sorted-array-ii.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int removeDuplicates(vector& nums) { 4 | int k = 2; 5 | 6 | if (nums.size() <= 2) return nums.size(); 7 | 8 | for (int i = 2; i < nums.size(); i++) { 9 | if (nums[i] != nums[k - 2]) { 10 | nums[k] = nums[i]; 11 | k++; 12 | } 13 | } 14 | 15 | return k; 16 | } 17 | }; -------------------------------------------------------------------------------- /0283-move-zeroes/0283-move-zeroes.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void moveZeroes(vector& nums) { 4 | int n = nums.size(); 5 | for (int i = 0; i < n; i++) { 6 | if (nums[i] == 0) { 7 | for (int j = i + 1; j < n; j++) { 8 | if (nums[j] != 0) { 9 | swap(nums[i], nums[j]); 10 | break; 11 | } 12 | } 13 | } 14 | } 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /1925-count-square-sum-triples/1925-count-square-sum-triples.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def countTriples(self, n: int) -> int: 3 | res = 0 4 | for u in range(2, int(sqrt(n)) + 1): 5 | for v in range(1, u): 6 | if (u - v) & 1 == 0 or gcd(u, v) != 1: 7 | continue 8 | c = u * u + v * v 9 | if c > n: 10 | continue 11 | res += 2 * (n // c) 12 | return res 13 | -------------------------------------------------------------------------------- /2358-number-of-ways-to-split-array/2358-number-of-ways-to-split-array.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def waysToSplitArray(self, nums: List[int]) -> int: 3 | leftSideSum = 0 4 | rightSideSum = sum(nums) 5 | validSplits = 0 6 | 7 | for i in range(len(nums) - 1): 8 | leftSideSum += nums[i] 9 | rightSideSum -= nums[i] 10 | if leftSideSum >= rightSideSum: 11 | validSplits += 1 12 | 13 | return validSplits -------------------------------------------------------------------------------- /0137-single-number-ii/0137-single-number-ii.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int singleNumber(vector& nums) { 4 | sort(nums.begin(), nums.end()); 5 | int n = nums.size(); 6 | for(int i = 0; i < n; i++){ 7 | bool Start = (i == 0); 8 | bool End = (i == n - 1); 9 | if ((Start || nums[i] != nums[i - 1]) && (End || nums[i] != nums[i + 1])) { 10 | return nums[i]; 11 | } 12 | } 13 | return -1; 14 | } 15 | }; -------------------------------------------------------------------------------- /0238-product-of-array-except-self/0238-product-of-array-except-self.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def productExceptSelf(self, nums: List[int]) -> List[int]: 3 | n = len(nums) 4 | output = [1] * n 5 | prefix = 1 6 | for i in range(n): 7 | output[i] = prefix 8 | prefix *= nums[i] 9 | suffix = 1 10 | for i in range(n - 1, -1, -1): 11 | output[i] *= suffix 12 | suffix *= nums[i] 13 | 14 | return output 15 | 16 | -------------------------------------------------------------------------------- /0988-flip-equivalent-binary-trees/0988-flip-equivalent-binary-trees.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean flipEquiv(TreeNode node1, TreeNode node2) { 3 | if (node1 == null && node2 == null) 4 | return true; 5 | 6 | if (node1 == null || node2 == null || node1.val != node2.val) 7 | return false; 8 | 9 | return (flipEquiv(node1.left, node2.left) && flipEquiv(node1.right, node2.right)) || 10 | (flipEquiv(node1.left, node2.right) && flipEquiv(node1.right, node2.left)); 11 | } 12 | } -------------------------------------------------------------------------------- /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 | } -------------------------------------------------------------------------------- /3768-check-if-digits-are-equal-in-string-after-operations-i/3768-check-if-digits-are-equal-in-string-after-operations-i.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def hasSameDigits(self, s): 3 | i = 0 4 | res = "" 5 | while len(s) > 2 and i < len(s) - 1: 6 | res += str((int(s[i]) + int(s[i + 1])) % 10) 7 | i += 1 8 | if i == len(s) - 1: 9 | s = res 10 | i = 0 11 | res = "" 12 | return len(s) == 2 and s[0] == s[1] -------------------------------------------------------------------------------- /0005-longest-palindromic-substring/0005-longest-palindromic-substring.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def longestPalindrome(self, s: str) -> str: 3 | if len(s) <= 1: 4 | return s 5 | 6 | Max_Len=1 7 | Max_Str=s[0] 8 | for i in range(len(s)-1): 9 | for j in range(i+1,len(s)): 10 | if j-i+1 > Max_Len and s[i:j+1] == s[i:j+1][::-1]: 11 | Max_Len = j-i+1 12 | Max_Str = s[i:j+1] 13 | 14 | return Max_Str -------------------------------------------------------------------------------- /0189-rotate-array/0189-rotate-array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void rotate(vector& nums, int k) { 4 | int n = nums.size(); 5 | k = k % n; 6 | vector temp; 7 | for(int i = n - k; i < n; i++) { 8 | temp.push_back(nums[i]); 9 | } 10 | for(int i = n - k - 1; i >= 0; i--) { 11 | nums[i + k] = nums[i]; 12 | } 13 | for(int i = 0; i < k; i++) { 14 | nums[i] = temp[i]; 15 | } 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /2356-largest-combination-with-bitwise-and-greater-than-zero/2356-largest-combination-with-bitwise-and-greater-than-zero.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int largestCombination(vector& candidates) { 4 | int n = candidates.size(),ans = 0; 5 | for(int i=0;i<32;i++){ 6 | int cnt = 0; 7 | for(auto candidate : candidates){ 8 | if(candidate & (1< int: 3 | if not prices: 4 | return 0 5 | buy1 = buy2 = float('-inf') 6 | sell1 = sell2 = 0 7 | for price in prices: 8 | buy1 = max(buy1, -price) 9 | sell1 = max(sell1, buy1 + price) 10 | buy2 = max(buy2, sell1 - price) 11 | sell2 = max(sell2, buy2 + price) 12 | 13 | return sell2 14 | -------------------------------------------------------------------------------- /0206-reverse-linked-list/0206-reverse-linked-list.py: -------------------------------------------------------------------------------- 1 | # Definition for singly-linked list. 2 | # class ListNode: 3 | # def __init__(self, val=0, next=None): 4 | # self.val = val 5 | # self.next = next 6 | class Solution: 7 | def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]: 8 | node = None 9 | while head: 10 | temp = head.next 11 | head.next = node 12 | node = head 13 | head = temp 14 | return node 15 | 16 | -------------------------------------------------------------------------------- /2265-partition-array-according-to-given-pivot/2265-partition-array-according-to-given-pivot.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def pivotArray(self, nums: List[int], pivot: int) -> List[int]: 3 | less = [] 4 | equal = [] 5 | greater = [] 6 | for num in nums: 7 | if num < pivot: 8 | less.append(num) 9 | elif num == pivot: 10 | equal.append(num) 11 | else: 12 | greater.append(num) 13 | return less + equal + greater -------------------------------------------------------------------------------- /1502-construct-k-palindrome-strings/1502-construct-k-palindrome-strings.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean canConstruct(String s, int k) { 3 | int odd = 0; 4 | int n = s.length(); 5 | int[] arr = new int[26]; 6 | 7 | //count number of odd chars 8 | for (char c : s.toCharArray()) { 9 | arr[c - 'a']++; 10 | } 11 | 12 | for (int i : arr) { 13 | if (i % 2 == 1) odd++; 14 | } 15 | 16 | return odd <= k && k <= n; 17 | } 18 | } -------------------------------------------------------------------------------- /2640-maximum-number-of-integers-to-choose-from-a-range-i/2640-maximum-number-of-integers-to-choose-from-a-range-i.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def maxCount(self, banned: List[int], n: int, maxSum: int) -> int: 3 | banned_set = set(banned) 4 | curr_sum = 0 5 | count = 0 6 | for num in range(1,n+1): 7 | if num in banned_set or curr_sum + num > maxSum: 8 | continue 9 | curr_sum += num 10 | count+= 1 11 | return count 12 | 13 | 14 | -------------------------------------------------------------------------------- /1566-check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence/1566-check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int isPrefixOfWord(String sentence, String searchWord) { 3 | String[] arr = sentence.split(" "); 4 | int n=arr.length; 5 | 6 | for(int i=0; i& nums, int k) { 4 | int n=nums.size(); 5 | int s=0; 6 | int maxx=0; 7 | sort(nums.begin(),nums.end()); 8 | for(int i=0;i int: 3 | start_times = sorted(i[0] for i in intervals) 4 | end_times = sorted(i[1] for i in intervals) 5 | end_ptr, group_count = 0, 0 6 | 7 | for start in start_times: 8 | if start > end_times[end_ptr]: 9 | end_ptr += 1 10 | else: 11 | group_count += 1 12 | 13 | return group_count -------------------------------------------------------------------------------- /2699-count-the-number-of-fair-pairs/2699-count-the-number-of-fair-pairs.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | long long countFairPairs(vector& v, int lower, int upper) { 4 | long long ans = 0; 5 | sort(v.begin(), v.end()); 6 | 7 | for (int i = 0; i < v.size() - 1; i++) { 8 | auto up = upper_bound(v.begin() + i + 1, v.end(), upper - v[i]); 9 | auto low = lower_bound(v.begin() + i + 1, v.end(), lower - v[i]); 10 | ans += (up - low); 11 | } 12 | return ans; 13 | } 14 | }; -------------------------------------------------------------------------------- /3612-adjacent-increasing-subarrays-detection-i/3612-adjacent-increasing-subarrays-detection-i.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def hasIncreasingSubarrays(self, nums: List[int], k: int) -> bool: 3 | knew = k - 1 4 | if knew == 0: 5 | return True 6 | for j in range(k + 1, len(nums)): 7 | if nums[j] > nums[j - 1] and nums[j - k] > nums[j - k - 1]: 8 | knew -= 1 9 | else: 10 | knew = k - 1 11 | if knew == 0: 12 | return True 13 | return False -------------------------------------------------------------------------------- /2232-adding-spaces-to-a-string/2232-adding-spaces-to-a-string.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def addSpaces(self, s, spaces): 3 | result = [] 4 | i, j = 0, 0 5 | n, m = len(s), len(spaces) 6 | 7 | while j < m: 8 | if i == spaces[j]: 9 | result.append(' ') 10 | j += 1 11 | result.append(s[i]) 12 | i += 1 13 | 14 | while i < n: 15 | result.append(s[i]) 16 | i += 1 17 | 18 | return ''.join(result) -------------------------------------------------------------------------------- /3336-water-bottles-ii/3336-water-bottles-ii.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def maxBottlesDrunk(self, numBottles: int, numExchange: int) -> int: 3 | drink = 0 4 | empty = 0 5 | while numBottles > 0: 6 | while numBottles > 0 and empty < numExchange: 7 | drink += 1 8 | empty += 1 9 | numBottles -= 1 10 | if empty == numExchange: 11 | numExchange += 1 12 | empty = 0 13 | numBottles += 1 14 | return drink 15 | -------------------------------------------------------------------------------- /3583-count-special-triplets/3583-count-special-triplets.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int specialTriplets(vector& nums) { 4 | const int mod = 1e9 + 7; 5 | unordered_map suffix; 6 | unordered_map prefix; 7 | for(auto x : nums) suffix[x] += 1; 8 | long long ans = 0; 9 | for(auto x : nums){ 10 | suffix[x] -= 1; 11 | ans += (long long)prefix[2 * x] * suffix[2 * x]; 12 | prefix[x] += 1; 13 | } 14 | return ans % mod; 15 | } 16 | }; -------------------------------------------------------------------------------- /2169-simple-bank-system/2169-simple-bank-system.py: -------------------------------------------------------------------------------- 1 | class Bank: 2 | def __init__(self, b: List[int]): 3 | self.b = Counter(dict(enumerate([0]+b))) 4 | 5 | def transfer(self, a1: int, a2: int, m: int) -> bool: 6 | return self.withdraw(a1,m) and (self.deposit(a2,m) or not self.deposit(a1,m)) 7 | 8 | def deposit(self, a: int, m: int) -> bool: 9 | return a bool: 12 | return a=m and not self.b.update({a:-m}) -------------------------------------------------------------------------------- /2551-apply-operations-to-an-array/2551-apply-operations-to-an-array.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def applyOperations(self, nums: List[int]) -> List[int]: 3 | n = len(nums) 4 | zero = 0 5 | for i in range(n-1): 6 | if (i < n-1 and nums[i] == nums[i+1]): 7 | nums[i] *= 2 8 | nums[i+1] = 0 9 | for i in range(n): 10 | if nums[i] != 0: 11 | nums[zero],nums[i] = nums[i],nums[zero] 12 | zero += 1 13 | 14 | return nums 15 | -------------------------------------------------------------------------------- /2147-number-of-ways-to-divide-a-long-corridor/2147-number-of-ways-to-divide-a-long-corridor.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def numberOfWays(self, corridor: str) -> int: 3 | seat , res, plant = 0,1,0 4 | for i in corridor: 5 | if i=='S': 6 | seat += 1 7 | else: 8 | if seat ==2: 9 | plant += 1 10 | if seat == 3: 11 | res = res*(plant+1)%(10**9+7) 12 | seat,plant = 1,0 13 | if seat !=2: 14 | return 0 15 | return res -------------------------------------------------------------------------------- /2551-apply-operations-to-an-array/2551-apply-operations-to-an-array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector applyOperations(vector& nums) { 4 | int n = nums.size(); 5 | int zero = 0; 6 | for(int i=0;i int: 3 | Max_int = 2 ** 31 - 1 4 | Min_int = -2 ** 31 5 | 6 | res = 0 7 | lamp = False 8 | if x < 0: 9 | x = x * (-1) 10 | lamp = True 11 | while x > 0: 12 | res = res * 10 + x % 10 13 | x //= 10 14 | 15 | if res > Max_int or res < Min_int: 16 | return 0 17 | else: 18 | if lamp: 19 | res = -res 20 | return res -------------------------------------------------------------------------------- /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 | } -------------------------------------------------------------------------------- /2586-longest-square-streak-in-an-array/2586-longest-square-streak-in-an-array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int longestSquareStreak(vector& nums) { 4 | mapmp; 5 | sort(nums.begin(), nums.end()); 6 | int res = -1; 7 | for(int num: nums) { 8 | int _sqrt = sqrt(num); 9 | if(_sqrt*_sqrt == num && mp.find(_sqrt)!=mp.end()) { 10 | mp[num] = mp[_sqrt]+1; 11 | res = max(res, mp[num]); 12 | } else mp[num] = 1; 13 | } 14 | return res; 15 | } 16 | }; -------------------------------------------------------------------------------- /3794-find-the-minimum-amount-of-time-to-brew-potions/3794-find-the-minimum-amount-of-time-to-brew-potions.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def minTime(self, skill: List[int], mana: List[int]) -> int: 3 | n, m = len(skill), len(mana) 4 | done = [0] * (n + 1) 5 | 6 | for j in range(m): 7 | for i in range(n): 8 | done[i + 1] = max(done[i + 1], done[i]) + mana[j] * skill[i] 9 | for i in range(n - 1, 0, -1): 10 | done[i] = done[i + 1] - mana[j] * skill[i] 11 | 12 | return done[n] -------------------------------------------------------------------------------- /0078-subsets/0078-subsets.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void fun(int st, vector&nums, vector&v1,vector>&v){ 4 | v.push_back(v1); 5 | for(int i=st;i> subsets(vector& nums) { 12 | sort(nums.begin(),nums.end()); 13 | vector>v; 14 | vectorv1; 15 | fun(0,nums,v1,v); 16 | return v; 17 | } 18 | }; -------------------------------------------------------------------------------- /0367-valid-perfect-square/0367-valid-perfect-square.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isPerfectSquare(int num) { 4 | int low= 0 ; 5 | int high = num; 6 | while(low <= high){ 7 | long long mid = (low+high)/2; 8 | long long res = mid*mid; 9 | if(res == num){ 10 | return true; 11 | } else if (res < num){ 12 | low = mid+1; 13 | } else{ 14 | high = mid-1; 15 | } 16 | 17 | } 18 | return false; 19 | } 20 | }; -------------------------------------------------------------------------------- /2509-minimize-xor/2509-minimize-xor.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minimizeXor(int num1, int num2) { 4 | int a = __builtin_popcount(num1); 5 | int b = __builtin_popcount(num2); 6 | int res = num1; 7 | for (int i = 0; i < 32; ++i) { 8 | if (a > b && (1 << i) & num1) { 9 | res ^= 1 << i; 10 | --a; 11 | } 12 | if (a < b && !((1 << i) & num1)) { 13 | res ^= 1 << i; 14 | ++a; 15 | } 16 | } 17 | return res; 18 | } 19 | }; -------------------------------------------------------------------------------- /2616-maximal-score-after-applying-k-operations/2616-maximal-score-after-applying-k-operations.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public long maxKelements(int[] nums, int k) { 3 | PriorityQueuepq = new PriorityQueue<>((a,b)->b-a); // Building Max Heap 4 | long ans = 0; 5 | for(int num:nums){ 6 | pq.add(num); 7 | } 8 | long sum =0; 9 | for(int i=0;i int: 3 | l = [] 4 | start = 0 5 | for i in range(1,len(nums)): 6 | if nums[i] <= nums[i-1]: 7 | l.append(i-start) 8 | start = i 9 | if sum(l) < len(nums): 10 | l.append(len(nums) - sum(l)) 11 | res = max(l) // 2 12 | for i in range(len(l)-1): 13 | res = max(res, min(l[i], l[i+1])) 14 | return res -------------------------------------------------------------------------------- /2188-minimized-maximum-of-products-distributed-to-any-store/2188-minimized-maximum-of-products-distributed-to-any-store.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minimizedMaximum(int n, vector& A) { 4 | int left = 1, right = 100000; 5 | while (left < right) { 6 | int mid = (left + right) / 2, sum = 0; 7 | for (int a : A) 8 | sum += (a + mid - 1) / mid; 9 | if (sum > n) 10 | left = mid + 1; 11 | else 12 | right = mid; 13 | } 14 | return left; 15 | } 16 | }; -------------------------------------------------------------------------------- /1302-delete-characters-to-make-fancy-string/1302-delete-characters-to-make-fancy-string.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string makeFancyString(string s) { 4 | string ans = ""; 5 | ans.push_back(s[0]); 6 | int n = s.size(),cnt = 1; 7 | for(int i=1;i= 0 or num % value == 0: 7 | rests[num % value] += 1 8 | else: 9 | rests[value - (abs(num) % value)] += 1 10 | 11 | mini = float('inf') 12 | for i in range(value): 13 | mini = min(mini, rests[i] * value + i) 14 | 15 | return mini 16 | 17 | 18 | -------------------------------------------------------------------------------- /3577-count-the-number-of-computer-unlocking-permutations/3577-count-the-number-of-computer-unlocking-permutations.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | static int countPermutations(vector& complexity) { 4 | const int x0=complexity[0], 5 | x1=*min_element(complexity.begin()+1, complexity.end()), 6 | mod=1e9+7, n=complexity.size(); 7 | 8 | if (x0>=x1) return 0; 9 | long long cnt=1; 10 | for(int i=1; i<=n-1; i++){ 11 | cnt*=i; 12 | if (cnt>=mod) cnt%=mod; 13 | } 14 | return cnt; 15 | 16 | } 17 | }; -------------------------------------------------------------------------------- /2145-grid-game/2145-grid-game.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | long long gridGame(vector>& grid) { 4 | long long firstRowSum = accumulate(begin(grid[0]), end(grid[0]), 0LL), 5 | secondRowSum = 0; 6 | long long minimumSum = LONG_LONG_MAX; 7 | for (int turnIndex = 0; turnIndex < grid[0].size(); ++turnIndex) { 8 | firstRowSum -= grid[0][turnIndex]; 9 | minimumSum = min(minimumSum, max(firstRowSum, secondRowSum)); 10 | secondRowSum += grid[1][turnIndex]; 11 | } 12 | return minimumSum; 13 | } 14 | }; -------------------------------------------------------------------------------- /3455-minimum-length-of-string-after-operations/3455-minimum-length-of-string-after-operations.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def minimumLength(self, s: str) -> int: 3 | freq_map = {} 4 | 5 | for c in s: 6 | freq_map[c] = freq_map.get(c, 0) + 1 7 | 8 | deletions = 0 9 | 10 | for count in freq_map.values(): 11 | if count > 2: 12 | if count % 2 == 0: 13 | deletions += (count - 2) 14 | else: 15 | deletions += (count - 1) 16 | 17 | return len(s) - deletions -------------------------------------------------------------------------------- /0306-additive-number/0306-additive-number.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isAdditiveNumber(self, num): 3 | n = len(num) 4 | for i, j in itertools.combinations(range(1, n), 2): 5 | a, b = num[:i], num[i:j] 6 | if a != str(int(a)) or b != str(int(b)): 7 | continue 8 | while j < n: 9 | c = str(int(a) + int(b)) 10 | if not num.startswith(c, j): 11 | break 12 | j += len(c) 13 | a, b = b, c 14 | if j == n: 15 | return True 16 | return False -------------------------------------------------------------------------------- /0083-remove-duplicates-from-sorted-list/0083-remove-duplicates-from-sorted-list.py: -------------------------------------------------------------------------------- 1 | # Definition for singly-linked list. 2 | # class ListNode: 3 | # def __init__(self, val=0, next=None): 4 | # self.val = val 5 | # self.next = next 6 | class Solution: 7 | def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]: 8 | curr = head 9 | while curr and curr.next: 10 | if curr.val == curr.next.val: 11 | curr.next = curr.next.next 12 | else: 13 | curr = curr.next 14 | return head 15 | 16 | -------------------------------------------------------------------------------- /0345-reverse-vowels-of-a-string/0345-reverse-vowels-of-a-string.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def reverseVowels(self, s: str) -> str: 3 | word = list(s) 4 | start = 0 5 | end = len(s) - 1 6 | vowel = "aeiouAEIOU" 7 | while start < end: 8 | while start < end and vowel.find(word[start]) == -1: 9 | start += 1 10 | while start < end and vowel.find(word[end]) == -1: 11 | end -= 1 12 | word[start],word[end] = word[end],word[start] 13 | start += 1 14 | end -= 1 15 | return "".join(word) 16 | -------------------------------------------------------------------------------- /2141-maximum-running-time-of-n-computers/2141-maximum-running-time-of-n-computers.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | long long maxRunTime(int n, vector& batteries) { 4 | long long totalEnergy = 0; 5 | for (int b : batteries) totalEnergy += b; 6 | 7 | sort(batteries.begin(), batteries.end()); 8 | 9 | for (int i = batteries.size() - 1; i >= 0; i--) { 10 | if (batteries[i] > totalEnergy / n) { 11 | totalEnergy -= batteries[i]; 12 | n--; 13 | } else break; 14 | } 15 | 16 | return totalEnergy / n; 17 | } 18 | }; -------------------------------------------------------------------------------- /3623-count-number-of-trapezoids-i/3623-count-number-of-trapezoids-i.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int countTrapezoids(vector>& points) { 4 | int MOD = 1000000007; 5 | unordered_map groups; 6 | for (auto& point : points) 7 | groups[point[1]]++; 8 | long long res = 0, total = 0; 9 | for (auto& group : groups){ 10 | long long lines = group.second * (group.second - 1) / 2; 11 | res = (res + total * lines) % MOD; 12 | total = (total + lines) % MOD; 13 | } 14 | return (int)res; 15 | } 16 | }; -------------------------------------------------------------------------------- /0944-delete-columns-to-make-sorted/0944-delete-columns-to-make-sorted.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minDeletionSize(vector& strs) { 4 | int delete_count=0; 5 | int row = strs.size(); 6 | int col = strs[0].size(); 7 | 8 | for(int j=0; jstrs[i+1][j]) 13 | { 14 | delete_count++; 15 | break; 16 | } 17 | } 18 | } 19 | return delete_count; 20 | } 21 | }; -------------------------------------------------------------------------------- /3309-count-prefix-and-suffix-pairs-i/3309-count-prefix-and-suffix-pairs-i.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isPrefixAndSuffix(self, str1: str, str2: str) -> bool: 3 | n1, n2 = len(str1), len(str2) 4 | if n1 > n2: 5 | return False 6 | return str2[:n1] == str1 and str2[-n1:] == str1 7 | 8 | def countPrefixSuffixPairs(self, words: List[str]) -> int: 9 | n = len(words) 10 | count = 0 11 | for i in range(n): 12 | for j in range(i + 1, n): 13 | if self.isPrefixAndSuffix(words[i], words[j]): 14 | count += 1 15 | return count -------------------------------------------------------------------------------- /3381-maximum-subarray-sum-with-length-divisible-by-k/3381-maximum-subarray-sum-with-length-divisible-by-k.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | long long maxSubarraySum(vector& nums, int k) { 4 | int n = nums.size(); 5 | long long sum = 0; 6 | long long ans = -1e18; 7 | long long dp[k]; 8 | for(int i = 0; i < k; i ++) dp[i] = 1e18; 9 | dp[0] = 0; 10 | for(int i = 1; i <= n; i ++){ 11 | sum += nums[i-1]; 12 | ans = max(ans, sum - dp[i % k]); 13 | dp[i % k] = min(dp[i % k], sum); 14 | } 15 | return ans; 16 | } 17 | }; -------------------------------------------------------------------------------- /0042-trapping-rain-water/0042-trapping-rain-water.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def trap(self, height: List[int]) -> int: 3 | n = len(height) 4 | if n == 0: 5 | return 0 6 | res = 0 7 | lmax = [0] * n 8 | rmax = [0] * n 9 | lmax[0] = height[0] 10 | for i in range(1, n): 11 | lmax[i] = max(height[i], lmax[i - 1]) 12 | rmax[-1] = height[-1] 13 | for i in range(n - 2, -1, -1): 14 | rmax[i] = max(height[i], rmax[i + 1]) 15 | for i in range(1, n - 1): 16 | res += min(lmax[i], rmax[i]) - height[i] 17 | return res 18 | -------------------------------------------------------------------------------- /0078-subsets/0078-subsets.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public List> subsets(int[] nums) { 3 | List> sets = new ArrayList<>(); 4 | int n = nums.length; 5 | int subsetsCount = 1 << n; // 2^n subsets 6 | 7 | for (int i = 0; i < subsetsCount; ++i) { 8 | List subset = new ArrayList<>(); 9 | for (int j = 0; j < n; ++j) { 10 | if ((i & (1 << j)) != 0) { 11 | subset.add(nums[j]); 12 | } 13 | } 14 | sets.add(subset); 15 | } 16 | return sets; 17 | } 18 | 19 | 20 | } -------------------------------------------------------------------------------- /2089-maximum-matrix-sum/2089-maximum-matrix-sum.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | long long maxMatrixSum(vector>& matrix) { 4 | long long sum = 0; 5 | int minus = INT_MAX, amt = 0; 6 | bool zero = false; 7 | for(int i = 0; i < matrix.size(); i++){ 8 | for(int j = 0; j < matrix[0].size(); j++){ 9 | int tmp = matrix[i][j]; 10 | sum += abs(tmp); 11 | minus = min(minus, abs(tmp)); 12 | if(tmp < 0) amt++; 13 | } 14 | } 15 | if(amt%2) return sum-minus*2; 16 | return sum; 17 | } 18 | }; -------------------------------------------------------------------------------- /2392-successful-pairs-of-spells-and-potions/2392-successful-pairs-of-spells-and-potions.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def successfulPairs(self, spells: List[int], potions: List[int], success: int) -> List[int]: 3 | freq=Counter(potions) 4 | pMax=max(freq) 5 | F=[0]*(1+pMax) 6 | for p, f in freq.items(): 7 | F[p]=f 8 | freq=list(accumulate(F)) 9 | n, m=len(spells), len(potions) 10 | res=[0]*n 11 | for i, x in enumerate(spells): 12 | k=(success+x-1)//x 13 | if k<=pMax: 14 | res[i]=m-freq[k-1] 15 | return res 16 | 17 | -------------------------------------------------------------------------------- /0034-find-first-and-last-position-of-element-in-sorted-array/0034-find-first-and-last-position-of-element-in-sorted-array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector searchRange(vector& nums, int target) { 4 | int n=nums.size(); 5 | int res =-1, ans = -1; 6 | for(int i=0;i=0;i--){ 13 | if(nums[i]==target){ 14 | res = i; 15 | break; 16 | } 17 | } 18 | return {ans,res}; 19 | } 20 | }; -------------------------------------------------------------------------------- /0954-maximum-sum-circular-subarray/0954-maximum-sum-circular-subarray.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def maxSubarraySumCircular(self, nums): 3 | curr_min = curr_max = min_sum = max_sum = total_sum = nums[0] 4 | 5 | for i in range(1, len(nums)): 6 | curr_max = max(nums[i], curr_max + nums[i]) 7 | max_sum = max(max_sum, curr_max) 8 | 9 | curr_min = min(nums[i], curr_min + nums[i]) 10 | min_sum = min(min_sum, curr_min) 11 | 12 | total_sum += nums[i] 13 | 14 | if min_sum == total_sum: 15 | return max_sum 16 | return max(max_sum, total_sum - min_sum) -------------------------------------------------------------------------------- /3267-find-longest-special-substring-that-occurs-thrice-i/3267-find-longest-special-substring-that-occurs-thrice-i.py: -------------------------------------------------------------------------------- 1 | from collections import Counter 2 | class Solution: 3 | def maximumLength(self, s: str) -> int: 4 | n = len(s) 5 | temp = Counter() 6 | for i in range(n): 7 | ch = s[i] 8 | for l in range(1,n-i+1): 9 | sub = s[i:i+l] 10 | if sub == ch * l: 11 | temp[sub] += 1 12 | lon = -1 13 | for sub , count in temp.items(): 14 | if count >= 3: 15 | lon = max(lon,len(sub)) 16 | return lon 17 | -------------------------------------------------------------------------------- /0038-count-and-say/0038-count-and-say.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string solve(string s , int n){ 4 | if(n == 1) return s; 5 | 6 | int i = 0; 7 | string ans = ""; 8 | while(i < s.size()){ 9 | int cnt = 1; 10 | while(i count = new HashMap<>(); 4 | count.put(0, 0); 5 | count.put(1, 0); 6 | count.put(2, 0); 7 | 8 | for (int num : nums) { 9 | count.put(num, count.get(num) + 1); 10 | } 11 | 12 | int idx = 0; 13 | for (int color = 0; color < 3; color++) { 14 | int freq = count.get(color); 15 | for (int j = 0; j < freq; j++) { 16 | nums[idx] = color; 17 | idx++; 18 | } 19 | } 20 | } 21 | } -------------------------------------------------------------------------------- /3427-special-array-ii/3427-special-array-ii.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isArraySpecial(self, nums: List[int], queries: List[List[int]]) -> List[bool]: 3 | n = len(nums) 4 | prefix = [0] * n 5 | for i in range(1, n): 6 | prefix[i] = prefix[i - 1] 7 | if (nums[i - 1] % 2 == 0 and nums[i] % 2 == 0) or (nums[i - 1] % 2 != 0 and nums[i] % 2 != 0): 8 | prefix[i] += 1 9 | 10 | result = [] 11 | for left, right in queries: 12 | special_count = prefix[right] - (prefix[left] if left > 0 else 0) 13 | result.append(special_count == 0) 14 | 15 | return result -------------------------------------------------------------------------------- /3834-minimum-operations-to-convert-all-elements-to-zero/3834-minimum-operations-to-convert-all-elements-to-zero.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minOperations(vector& nums) { 4 | vector stack; 5 | int res = 0; 6 | for (int num : nums) { 7 | while (!stack.empty() && stack.back() > num) 8 | stack.pop_back(); 9 | 10 | if (num == 0) 11 | continue; 12 | if (stack.empty() || stack.back() < num) { 13 | res++; 14 | stack.push_back(num); 15 | } 16 | } 17 | return res; 18 | } 19 | }; -------------------------------------------------------------------------------- /0055-jump-game/0055-jump-game.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def canJump(self, nums: List[int]) -> bool: 3 | count = 0 4 | for n in nums: 5 | if count < 0: 6 | return False 7 | elif count > 0: 8 | count = n 9 | count = count - 1 10 | return True 11 | 12 | 13 | 14 | 15 | class Solution: 16 | def canJump(self, nums: List[int]) -> bool: 17 | gas = 0 18 | for n in nums: 19 | if gas < 0: 20 | return False 21 | elif n > gas: 22 | gas = n 23 | gas -= 1 24 | 25 | return True -------------------------------------------------------------------------------- /1566-check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence/1566-check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isPrefixOfWord(self, sentence: str, searchWord: str) -> int: 3 | # Split the sentence into words 4 | words = sentence.split(" ") 5 | 6 | # Iterate over the words 7 | for i, word in enumerate(words): 8 | # Check if the word starts with the searchWord 9 | if word.startswith(searchWord): 10 | return i + 1 # Return 1-based index 11 | 12 | # Return -1 if no word starts with the searchWord 13 | return -1 -------------------------------------------------------------------------------- /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 | } -------------------------------------------------------------------------------- /3581-the-two-sneaky-numbers-of-digitville/3581-the-two-sneaky-numbers-of-digitville.py: -------------------------------------------------------------------------------- 1 | from collections import Counter 2 | class Solution: 3 | def getSneakyNumbers(self, nums: List[int]) -> List[int]: 4 | n = len(nums)-2 5 | res = [] 6 | for i in range(len(nums)): 7 | while 0<= nums[i] < len(nums) and nums[nums[i]]!= nums[i]: 8 | j = nums[i] 9 | nums[i],nums[j] = nums[j],nums[i] 10 | for i in range(len(nums)): 11 | if nums[i]!=i: 12 | res.append(nums[i]) 13 | if len(res)==2: 14 | break 15 | return res 16 | 17 | -------------------------------------------------------------------------------- /2690-house-robber-iv/2690-house-robber-iv.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def minCapability(self, nums, k): 3 | start, end = 1, max(nums) 4 | total = len(nums) 5 | while start< end: 6 | mid = (start + end) // 2 7 | count = 0 8 | 9 | index = 0 10 | while index < total: 11 | if nums[index] <= mid: 12 | count += 1 13 | index += 2 14 | else: 15 | index += 1 16 | 17 | if count >= k: 18 | end = mid 19 | else: 20 | start = mid + 1 21 | 22 | return start -------------------------------------------------------------------------------- /3493-maximum-number-of-operations-to-move-ones-to-the-end/3493-maximum-number-of-operations-to-move-ones-to-the-end.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxOperations(string s) { 4 | int count = 0; 5 | int countOne = 0, i = 0; 6 | while(i>& grid) { 4 | int count = 0; 5 | for(auto &row:grid){ 6 | int n =row.size(); 7 | int low =0 , high = n-1; 8 | while(low <= high){ 9 | int mid = (low + high)/2; 10 | if(row[mid]< 0){ 11 | high = mid-1; 12 | }else{ 13 | low = mid+1; 14 | } 15 | } 16 | count = count + n-low; 17 | } 18 | return count; 19 | } 20 | }; -------------------------------------------------------------------------------- /1477-product-of-the-last-k-numbers/1477-product-of-the-last-k-numbers.py: -------------------------------------------------------------------------------- 1 | class ProductOfNumbers: 2 | def __init__(self): 3 | self.prefix_product = [1] 4 | self.size = 0 5 | 6 | def add(self, num: int): 7 | if num == 0: 8 | self.prefix_product = [1] 9 | self.size = 0 10 | else: 11 | self.prefix_product.append(self.prefix_product[self.size] * num) 12 | self.size += 1 13 | 14 | def getProduct(self, k: int) -> int: 15 | if k > self.size: 16 | return 0 17 | return ( 18 | self.prefix_product[self.size] // self.prefix_product[self.size - k] 19 | ) -------------------------------------------------------------------------------- /3434-find-the-number-of-distinct-colors-among-the-balls/3434-find-the-number-of-distinct-colors-among-the-balls.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def queryResults(self, limit: int, queries: List[List[int]]) -> List[int]: 3 | clerd = {} 4 | freq = {} 5 | ans = [] 6 | for ball , clr in queries: 7 | if ball in clerd: 8 | old_color = clerd[ball] 9 | freq[old_color] -= 1 10 | if freq[old_color] == 0: 11 | del freq[old_color] 12 | 13 | clerd[ball] = clr 14 | freq[clr] = freq.get(clr , 0) + 1 15 | ans.append(len(freq)) 16 | 17 | return ans -------------------------------------------------------------------------------- /1063-best-sightseeing-pair/1063-best-sightseeing-pair.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxScoreSightseeingPair(vector& values) { 4 | int n = values.size(); 5 | if (n < 2) return values[0] + values[1] + 0 - 1; 6 | vector suffixMax(n); 7 | suffixMax[n - 1] = values[n - 1] - (n - 1); 8 | for (int i = n - 2; i >= 0; i--) { 9 | suffixMax[i] = max(suffixMax[i + 1], values[i] - i); 10 | } 11 | int maxScore = INT_MIN; 12 | for (int i = 0; i < n - 1; i++) { 13 | maxScore = max(maxScore, values[i] + i + suffixMax[i + 1]); 14 | } 15 | 16 | return maxScore; 17 | } 18 | }; -------------------------------------------------------------------------------- /1895-minimum-number-of-operations-to-move-all-balls-to-each-box/1895-minimum-number-of-operations-to-move-all-balls-to-each-box.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def minOperations(self, boxes: str) -> List[int]: 3 | n = len(boxes) 4 | left = [0] * n 5 | right = [0] * n 6 | cnt = 0 7 | for i in range(1, n): 8 | if boxes[i - 1] == '1': 9 | cnt += 1 10 | left[i] = left[i - 1] + cnt 11 | cnt = 0 12 | for i in range(n - 2, -1, -1): 13 | if boxes[i + 1] == '1': 14 | cnt += 1 15 | right[i] = right[i + 1] + cnt 16 | return [a + b for a, b in zip(left, right)] -------------------------------------------------------------------------------- /2435-paths-in-matrix-whose-sum-is-divisible-by-k/2435-paths-in-matrix-whose-sum-is-divisible-by-k.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def numberOfPaths(self, grid: List[List[int]], k: int) -> int: 3 | m, n, mod = len(grid), len(grid[0]), 10 ** 9 + 7 4 | dp = [[[0] * k for _ in range(n)] for _ in range(m)] 5 | dp[0][0][grid[0][0] % k] = 1 6 | for i in range(m): 7 | for j in range(n): 8 | for s in range(k): 9 | modded_sum = (s + grid[i][j]) % k 10 | if j > 0: dp[i][j][modded_sum] += dp[i][j - 1][s] 11 | if i > 0: dp[i][j][modded_sum] += dp[i - 1][j][s] 12 | dp[i][j][modded_sum] %= mod 13 | return dp[m - 1][n - 1][0] 14 | -------------------------------------------------------------------------------- /0021-merge-two-sorted-lists/0021-merge-two-sorted-lists.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) { 4 | ListNode* dummy = new ListNode(0); 5 | ListNode* cur = dummy; 6 | 7 | while (list1 && list2) { 8 | if (list1->val > list2->val) { 9 | cur->next = list2; 10 | list2 = list2->next; 11 | } else { 12 | cur->next = list1; 13 | list1 = list1->next; 14 | } 15 | cur = cur->next; 16 | } 17 | 18 | cur->next = list1 ? list1 : list2; 19 | 20 | return dummy->next; 21 | } 22 | }; -------------------------------------------------------------------------------- /0152-maximum-product-subarray/0152-maximum-product-subarray.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | class Solution { 6 | public: 7 | int maxProduct(vector& nums) { 8 | int n = nums.size(); 9 | int curr_max = nums[0]; 10 | int curr_min = nums[0]; 11 | int res = nums[0]; 12 | for (int i = 1; i < n; i++) { 13 | int temp = curr_max; 14 | curr_max = max({nums[i], nums[i] * curr_max, nums[i] * curr_min}); 15 | curr_min = min({nums[i], nums[i] * temp, nums[i] * curr_min}); 16 | res = max(res, curr_max); 17 | } 18 | return res; 19 | } 20 | }; 21 | -------------------------------------------------------------------------------- /0443-string-compression/0443-string-compression.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def compress(self, chars: List[str]) -> int: 3 | i = 0 4 | res = 0 5 | while i < len(chars): 6 | group_length = 1 7 | while (i + group_length < len(chars) 8 | and chars[i + group_length] == chars[i]): 9 | group_length += 1 10 | chars[res] = chars[i] 11 | res += 1 12 | if group_length > 1: 13 | str_repr = str(group_length) 14 | chars[res:res+len(str_repr)] = list(str_repr) 15 | res += len(str_repr) 16 | i += group_length 17 | return res -------------------------------------------------------------------------------- /0021-merge-two-sorted-lists/0021-merge-two-sorted-lists.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public ListNode mergeTwoLists(ListNode list1, ListNode list2) { 3 | ListNode dummy = new ListNode(); 4 | ListNode cur = dummy; 5 | 6 | while (list1 != null && list2 != null) { 7 | if (list1.val > list2.val) { 8 | cur.next = list2; 9 | list2 = list2.next; 10 | } else { 11 | cur.next = list1; 12 | list1 = list1.next; 13 | } 14 | cur = cur.next; 15 | } 16 | 17 | cur.next = (list1 != null) ? list1 : list2; 18 | 19 | return dummy.next; 20 | } 21 | } -------------------------------------------------------------------------------- /0260-single-number-iii/0260-single-number-iii.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector singleNumber(vector& nums) { 4 | int n = nums.size(); 5 | int x = nums[0]; 6 | for (int i = 1; i < n; i++) { 7 | x ^= nums[i]; 8 | } 9 | 10 | unsigned int ux = static_cast(x); 11 | unsigned int k = ux & -ux; 12 | 13 | int res = 0, ans = 0; 14 | for (int i = 0; i < n; i++) { 15 | if (nums[i] & k) { 16 | res ^= nums[i]; 17 | } else { 18 | ans ^= nums[i]; 19 | } 20 | } 21 | 22 | return {res, ans}; 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /1002-maximum-width-ramp/1002-maximum-width-ramp.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def maxWidthRamp(self, nums: List[int]) -> int: 3 | n = len(nums) 4 | stack = [] 5 | 6 | # Step 1: Build a decreasing stack of indices 7 | for i in range(n): 8 | if not stack or nums[stack[-1]] > nums[i]: 9 | stack.append(i) 10 | 11 | maxWidth = 0 12 | 13 | # Step 2: Traverse from the end and find maximum width ramp 14 | for j in range(n - 1, -1, -1): 15 | while stack and nums[stack[-1]] <= nums[j]: 16 | maxWidth = max(maxWidth, j - stack.pop()) 17 | 18 | return maxWidth -------------------------------------------------------------------------------- /1147-flip-columns-for-maximum-number-of-equal-rows/1147-flip-columns-for-maximum-number-of-equal-rows.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int maxEqualRowsAfterFlips(int[][] matrix) { 3 | int ans = 0; 4 | int m = matrix.length, n = matrix[0].length; 5 | for(int i = 0; i < m; i++) { 6 | int cnt = 0; 7 | int[] flip = new int[n]; 8 | for(int j = 0; j < n; j++) flip[j] = 1 - matrix[i][j]; 9 | for(int k = i; k < m; k++) { 10 | if(Arrays.equals(matrix[k], matrix[i]) || Arrays.equals(matrix[k], flip)) cnt++; 11 | } 12 | ans = Math.max(ans, cnt); 13 | } 14 | return ans; 15 | } 16 | } -------------------------------------------------------------------------------- /3291-find-if-array-can-be-sorted/3291-find-if-array-can-be-sorted.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | 4 | bool haveSameSetBits(int a, int b) { 5 | return __builtin_popcount(a) == __builtin_popcount(b); 6 | } 7 | 8 | bool canSortArray(vector& nums) { 9 | 10 | int N = nums.size(), times = nums.size(); 11 | while (times--) { 12 | for (int i = 0; i < N - 1; i++) { 13 | if (haveSameSetBits(nums[i], nums[i + 1]) && nums[i + 1] < nums[i]) { 14 | swap(nums[i], nums[i + 1]); 15 | } 16 | } 17 | } 18 | 19 | return is_sorted(nums.begin(), nums.end()); 20 | } 21 | }; -------------------------------------------------------------------------------- /0021-merge-two-sorted-lists/0021-merge-two-sorted-lists.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]: 3 | dummy = ListNode() 4 | cur = dummy 5 | 6 | while list1 and list2: 7 | if list1.val > list2.val: 8 | cur.next = list2 9 | list2 = list2.next 10 | else: 11 | cur.next = list1 12 | list1 = list1.next 13 | 14 | cur = cur.next 15 | 16 | if list1: 17 | cur.next = list1 18 | else: 19 | cur.next = list2 20 | 21 | return dummy.next -------------------------------------------------------------------------------- /0003-longest-substring-without-repeating-characters/0003-longest-substring-without-repeating-characters.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def lengthOfLongestSubstring(self, s: str) -> int: 3 | n = len(s) 4 | maxLength = 0 5 | charSet = set() 6 | left = 0 7 | 8 | for right in range(n): 9 | if s[right] not in charSet: 10 | charSet.add(s[right]) 11 | maxLength = max(maxLength, right - left + 1) 12 | else: 13 | while s[right] in charSet: 14 | charSet.remove(s[left]) 15 | left += 1 16 | charSet.add(s[right]) 17 | 18 | return maxLength -------------------------------------------------------------------------------- /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 | } -------------------------------------------------------------------------------- /2473-max-sum-of-a-pair-with-equal-sum-of-digits/2473-max-sum-of-a-pair-with-equal-sum-of-digits.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def maximumSum(self, nums: List[int]) -> int: 3 | dict_map = {} 4 | 5 | res = -1 6 | for num in nums: 7 | temp = num 8 | new_num = 0 9 | while temp: 10 | new_num += temp % 10 11 | temp = temp // 10 12 | if new_num in dict_map: 13 | new_res = num + dict_map[new_num] 14 | res = max(res, new_res) 15 | dict_map[new_num] = max(num, dict_map[new_num]) 16 | else: 17 | dict_map[new_num] = num 18 | return res -------------------------------------------------------------------------------- /2813-to-be-or-not-to-be/2813-to-be-or-not-to-be.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {string} val 3 | * @return {Object} 4 | */ 5 | var expect = function(val) { 6 | return { 7 | toBe: (val2) => { 8 | if (val !== val2){ 9 | throw new Error("Not Equal"); 10 | }else{ 11 | return true; 12 | } 13 | }, 14 | notToBe: (val2) => { 15 | if (val === val2){ 16 | throw new Error("Equal"); 17 | } 18 | else{ 19 | return true; 20 | } 21 | } 22 | } 23 | }; 24 | 25 | /** 26 | * expect(5).toBe(5); // true 27 | * expect(5).notToBe(5); // throws "Equal" 28 | */ -------------------------------------------------------------------------------- /2625-increment-submatrices-by-one/2625-increment-submatrices-by-one.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def rangeAddQueries(self, n: int, queries: List[List[int]]) -> List[List[int]]: 3 | ans = [[0] * n for _ in range(n)] 4 | for r1, c1, r2, c2 in queries: 5 | ans[r1][c1] += 1 6 | if r2 + 1 < n: ans[r2 + 1][c1] -= 1 7 | if c2 + 1 < n: ans[r1][c2 + 1] -= 1 8 | if r2 + 1 < n and c2 + 1 < n: ans[r2 + 1][c2 + 1] += 1 9 | for r in range(1, n): 10 | for c in range(n): 11 | ans[r][c] += ans[r - 1][c] 12 | for r in range(n): 13 | for c in range(1, n): 14 | ans[r][c] += ans[r][c - 1] 15 | return ans -------------------------------------------------------------------------------- /0012-integer-to-roman/0012-integer-to-roman.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string intToRoman(int num) { 4 | const vector> valueSymbols{ 5 | {1000, "M"}, {900, "CM"}, {500, "D"}, {400, "CD"}, {100, "C"}, 6 | {90, "XC"}, {50, "L"}, {40, "XL"}, {10, "X"}, {9, "IX"}, 7 | {5, "V"}, {4, "IV"}, {1, "I"}}; 8 | 9 | string res; 10 | 11 | for (const auto& [value, symbol] : valueSymbols) { 12 | if (num == 0) 13 | break; 14 | 15 | while (num >= value) { 16 | res += symbol; 17 | num -= value; 18 | } 19 | } 20 | 21 | return res; 22 | } 23 | }; -------------------------------------------------------------------------------- /0140-word-break-ii/0140-word-break-ii.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | int n; 3 | Set dict = new HashSet<>(); 4 | List res = new ArrayList<>(); 5 | 6 | private void backTrack(String s, int i, String l) { 7 | if (i == n) { 8 | res.add(l.substring(0, l.length() - 1)); 9 | return; 10 | } 11 | for (var j=i; j wordBreak(String s, List wordDict) { 20 | n = s.length(); 21 | for (var word : wordDict) dict.add(word); 22 | 23 | backTrack(s, 0, ""); 24 | 25 | return res; 26 | } 27 | } -------------------------------------------------------------------------------- /0374-guess-number-higher-or-lower/0374-guess-number-higher-or-lower.py: -------------------------------------------------------------------------------- 1 | # The guess API is already defined for you. 2 | # @param num, your guess 3 | # @return -1 if num is higher than the picked number 4 | # 1 if num is lower than the picked number 5 | # otherwise return 0 6 | # def guess(num: int) -> int: 7 | 8 | class Solution: 9 | def guessNumber(self, n: int) -> int: 10 | low = 1 11 | high = n 12 | while low <= high: 13 | mid = (low + high)//2 14 | res = guess(mid) 15 | if res > 0: 16 | low = mid + 1 17 | elif res < 0: 18 | high = mid - 1 19 | else: 20 | return mid 21 | 22 | -------------------------------------------------------------------------------- /0474-ones-and-zeroes/0474-ones-and-zeroes.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findMaxForm(self, strs: List[str], m: int, n: int) -> int: 3 | dp = {(0, 0): 0} 4 | 5 | for s in strs: 6 | ones = s.count('1') 7 | zeroes = s.count('0') 8 | newdp = {} 9 | 10 | for (prevZeroes, prevOnes), val in dp.items(): 11 | newZeroes, newOnes = prevZeroes + zeroes, prevOnes + ones 12 | if newZeroes <= m and newOnes <= n: 13 | if (newZeroes, newOnes) not in dp or dp[(newZeroes, newOnes)] < val + 1: 14 | newdp[(newZeroes, newOnes)] = val + 1 15 | 16 | dp.update(newdp) 17 | 18 | return max(dp.values()) -------------------------------------------------------------------------------- /1636-number-of-substrings-with-only-1s/1636-number-of-substrings-with-only-1s.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numSub(string s) { 4 | int ans=0; 5 | int cnt=0; 6 | char ch=s[0]; 7 | int mod=1e9+7; 8 | for(int i=0;i=0;k--){ 9 | if(a[i] bool: 3 | size = len(nums) 4 | 5 | # Create a sorted copy of the list 6 | sorted_nums = sorted(nums) 7 | 8 | # Compare the original list with the sorted list, considering all possible rotations 9 | for rotation_offset in range(size): 10 | is_match = True 11 | for index in range(size): 12 | if nums[(rotation_offset + index) % size] != sorted_nums[index]: 13 | is_match = False 14 | break 15 | if is_match: 16 | return True 17 | 18 | return False -------------------------------------------------------------------------------- /3652-best-time-to-buy-and-sell-stock-using-strategy/3652-best-time-to-buy-and-sell-stock-using-strategy.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def maxProfit(self, prices: List[int], strategy: List[int], k: int) -> int: 3 | sp = [s * p for s, p in zip(strategy, prices)] 4 | n = len(prices) 5 | 6 | baseline = sum(sp) 7 | h = k // 2 8 | old = sum(sp[:k]) 9 | new = sum(prices[h:k]) 10 | 11 | maxdiff = max(0, new - old) 12 | 13 | for r in range(k, n): 14 | l = r - k + 1 15 | old += sp[r] - sp[l - 1] 16 | new += prices[r] 17 | new -= prices[l - 1 + h] 18 | maxdiff = max(maxdiff, new - old) 19 | 20 | return baseline + maxdiff -------------------------------------------------------------------------------- /2170-count-number-of-maximum-bitwise-or-subsets/2170-count-number-of-maximum-bitwise-or-subsets.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int n; 4 | unsigned max_OR; 5 | int f(int i, unsigned acc_or, vector& nums){ 6 | if (i<0) return (acc_or==max_OR)?1:0; 7 | int skip=f(i-1, acc_or, nums); 8 | int take=f(i-1, acc_or| nums[i], nums); 9 | return skip+take; 10 | } 11 | int countMaxOrSubsets(vector& nums) { 12 | n=nums.size(); 13 | max_OR=accumulate(nums.begin(), nums.end(), 0, bit_or<>()); 14 | return f(n-1, 0, nums); 15 | } 16 | }; 17 | 18 | 19 | auto init = []() { 20 | ios::sync_with_stdio(0); 21 | cin.tie(0); 22 | cout.tie(0); 23 | return 'c'; 24 | }(); -------------------------------------------------------------------------------- /1566-check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence/1566-check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int isPrefixOfWord(string sentence, string searchWord) { 4 | // Split the sentence into words 5 | istringstream stream(sentence); 6 | string word; 7 | int index = 1; // 1-based index 8 | 9 | while (stream >> word) { 10 | // Check if the word starts with the searchWord 11 | if (word.find(searchWord) == 0) { 12 | return index; 13 | } 14 | index++; 15 | } 16 | 17 | // Return -1 if no word starts with the searchWord 18 | return -1; 19 | } 20 | }; -------------------------------------------------------------------------------- /2562-count-ways-to-build-good-strings/2562-count-ways-to-build-good-strings.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | private int mod = (int) 1e9 + 7; 3 | 4 | public int countGoodStrings(int low, int high, int zero, int one) { 5 | if (zero > one) 6 | return countGoodStrings(low, high, one, zero); 7 | 8 | int dp[] = new int[high + 1], res = 0; 9 | dp[0] = 1; 10 | 11 | for (int i = 1; i <= high; i++) { 12 | if (i >= zero) 13 | dp[i] = (dp[i] + dp[i - zero]) % mod; 14 | if (i >= one) 15 | dp[i] = (dp[i] + dp[i - one]) % mod; 16 | if (i >= low) 17 | res = (res + dp[i]) % mod; 18 | } 19 | 20 | return res; 21 | } 22 | } -------------------------------------------------------------------------------- /3189-find-champion-ii/3189-find-champion-ii.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findChampion(int n, vector>& edges) { 4 | vector isUndefeated(n, true); 5 | 6 | for (const auto& 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 | }; -------------------------------------------------------------------------------- /0083-remove-duplicates-from-sorted-list/0083-remove-duplicates-from-sorted-list.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode() : val(0), next(nullptr) {} 7 | * ListNode(int x) : val(x), next(nullptr) {} 8 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 9 | * }; 10 | */ 11 | class Solution { 12 | public: 13 | ListNode* deleteDuplicates(ListNode* head) { 14 | ListNode* curr = head; 15 | while(curr && curr->next){ 16 | if(curr->val == curr->next->val){ 17 | curr->next = curr->next->next; 18 | } else{ 19 | curr = curr->next; 20 | } 21 | } 22 | return head; 23 | } 24 | }; -------------------------------------------------------------------------------- /1548-check-if-all-1s-are-at-least-length-k-places-away/1548-check-if-all-1s-are-at-least-length-k-places-away.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool kLengthApart(vector& nums, int k) { 4 | 5 | int n = nums.size(); 6 | int pre_index; 7 | bool f = true; 8 | 9 | for (int i = 0; i < n; i++) { 10 | if (f && nums[i] == 1) { 11 | f = false; 12 | pre_index = i; 13 | continue; 14 | } 15 | if (nums[i] == 1) { 16 | if (i - pre_index - 1 >= k) 17 | pre_index = i; 18 | else 19 | return false; 20 | } 21 | } 22 | 23 | return true; 24 | } 25 | }; -------------------------------------------------------------------------------- /0029-divide-two-integers/0029-divide-two-integers.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def divide(self, dividend: int, divisor: int) -> int: 3 | if dividend == -2147483648 and divisor == -1: 4 | return 2147483647 5 | if abs(divisor) == 1: 6 | return dividend * divisor 7 | 8 | is_negative = (dividend < 0) ^ (divisor < 0) 9 | count = 0 10 | 11 | dividend, divisor = abs(dividend), abs(divisor) 12 | 13 | while dividend >= divisor: 14 | x = 1 15 | base = divisor 16 | while base <= (dividend >> 1): 17 | base <<= 1 18 | x <<= 1 19 | count += x 20 | dividend -= base 21 | 22 | return -count if is_negative else count -------------------------------------------------------------------------------- /0371-sum-of-two-integers/README.md: -------------------------------------------------------------------------------- 1 |

371. Sum of Two Integers

Medium


Given two integers a and b, return the sum of the two integers without using the operators + and -.

2 | 3 |

 

4 |

Example 1:

5 |
Input: a = 1, b = 2
 6 | Output: 3
 7 | 

Example 2:

8 |
Input: a = 2, b = 3
 9 | Output: 5
10 | 
11 |

 

12 |

Constraints:

13 | 14 |
    15 |
  • -1000 <= a, b <= 1000
  • 16 |
17 | -------------------------------------------------------------------------------- /3522-find-the-power-of-k-size-subarrays-i/3522-find-the-power-of-k-size-subarrays-i.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def resultsArray(self, nums: List[int], k: int) -> List[int]: 3 | n = len(nums) 4 | result = [] 5 | for i in range(n - k + 1): 6 | is_valid = True 7 | max_element = nums[i] 8 | for j in range(i, i + k - 1): 9 | if nums[j] + 1 != nums[j + 1]: 10 | is_valid = False 11 | break 12 | max_element = max(max_element, nums[j]) 13 | 14 | if is_valid: 15 | result.append(max(max_element, nums[i + k - 1])) 16 | else: 17 | result.append(-1) 18 | 19 | return result -------------------------------------------------------------------------------- /3620-maximum-number-of-distinct-elements-after-operations/3620-maximum-number-of-distinct-elements-after-operations.py: -------------------------------------------------------------------------------- 1 | from typing import List 2 | 3 | class Solution: 4 | def maxDistinctElements(self, nums: List[int], k: int) -> int: 5 | nums.sort() 6 | last_picked = -10**18 7 | distinct_count = 0 8 | 9 | for num in nums: 10 | lower_bound = num - k 11 | upper_bound = num + k 12 | if last_picked < lower_bound: 13 | last_picked = lower_bound 14 | else: 15 | last_picked += 1 16 | if last_picked <= upper_bound: 17 | distinct_count += 1 18 | else: 19 | last_picked -= 1 20 | 21 | return distinct_count -------------------------------------------------------------------------------- /0040-combination-sum-ii/0040-combination-sum-ii.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def combinationSum2(self, candidates: List[int], target: int) -> List[List[int]]: 3 | candidates.sort() 4 | res = [] 5 | 6 | def dfs(target, start, comb): 7 | if target < 0: 8 | return 9 | if target == 0: 10 | res.append(comb) 11 | return 12 | for i in range(start, len(candidates)): 13 | if i > start and candidates[i] == candidates[i-1]: 14 | continue 15 | if candidates[i] > target: 16 | break 17 | dfs(target-candidates[i], i+1, comb+[candidates[i]]) 18 | 19 | dfs(target, 0, []) 20 | return res -------------------------------------------------------------------------------- /0043-multiply-strings/0043-multiply-strings.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public String multiply(String num1, String num2) { 3 | int m = num1.length(), n = num2.length(); 4 | int[] pos = new int[m + n]; 5 | 6 | for(int i = m - 1; i >= 0; i--) { 7 | for(int j = n - 1; j >= 0; j--) { 8 | int mul = (num1.charAt(i) - '0') * (num2.charAt(j) - '0'); 9 | int p1 = i + j, p2 = i + j + 1; 10 | int sum = mul + pos[p2]; 11 | 12 | pos[p1] += sum / 10; 13 | pos[p2] = (sum) % 10; 14 | } 15 | } 16 | 17 | StringBuilder sb = new StringBuilder(); 18 | for(int p : pos) if(!(sb.length() == 0 && p == 0)) sb.append(p); 19 | return sb.length() == 0 ? "0" : sb.toString(); 20 | } 21 | } -------------------------------------------------------------------------------- /0048-rotate-image/0048-rotate-image.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void rotate(vector>& matrix) { 4 | int n = matrix.size(); 5 | 6 | for (int row = 0; row < n / 2; row++) { 7 | for (int col = row; col < n - row - 1; col++) { 8 | // Swap the top-left and top-right cells in the current group 9 | swap(matrix[row][col], matrix[col][n - 1 - row]); 10 | // Swap the top-left and bottom-right cells in the current group 11 | swap(matrix[row][col], matrix[n - 1 - row][n - 1 - col]); 12 | // Swap the top-left and bottom-left cells in the current group 13 | swap(matrix[row][col], matrix[n - 1 - col][row]); 14 | } 15 | } 16 | } 17 | }; -------------------------------------------------------------------------------- /0016-3sum-closest/0016-3sum-closest.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int threeSumClosest(vector& nums, int target) { 4 | sort(nums.begin(), nums.end()); 5 | int result = nums[0] + nums[1] + nums[2]; 6 | 7 | for (int i = 0; i < nums.size() - 2; i++) { 8 | int left = i + 1, right = nums.size() - 1; 9 | 10 | while (left < right) { 11 | int sum = nums[i] + nums[left] + nums[right]; 12 | 13 | if (abs(target - sum) < abs(target - result)) 14 | result = sum; 15 | 16 | if (sum == target) return target; 17 | else if (sum < target) left++; 18 | else right--; 19 | } 20 | } 21 | 22 | return result; 23 | } 24 | }; -------------------------------------------------------------------------------- /0140-word-break-ii/0140-word-break-ii.cpp: -------------------------------------------------------------------------------- 1 | 2 | class Solution { 3 | public: 4 | set dict; 5 | vector wordBreak(string s, vector& wordDict) { 6 | for(auto x : wordDict)dict.insert(x); 7 | vector res; 8 | rec(s, 0, "", res); 9 | return res; 10 | } 11 | 12 | void rec(string s, int beg, string curr, vector& res) { 13 | if(beg==s.size()) { 14 | if(curr[0]==' ')curr = curr.substr(1); 15 | res.push_back(curr); 16 | } 17 | string temp = ""; 18 | for(int i=beg;i res(num1.size()+num2.size(), 0); 7 | 8 | for (int i = num1.size()-1; i >= 0; i--) { 9 | for (int j = num2.size()-1; j >= 0; j--) { 10 | res[i + j + 1] += (num1[i]-'0') * (num2[j]-'0'); 11 | res[i + j] += res[i + j + 1] / 10; 12 | res[i + j + 1] %= 10; 13 | } 14 | } 15 | 16 | int i = 0; 17 | string ans = ""; 18 | while (res[i] == 0) i++; 19 | while (i < res.size()) ans += to_string(res[i++]); 20 | 21 | return ans; 22 | } 23 | }; -------------------------------------------------------------------------------- /0820-find-eventual-safe-states/0820-find-eventual-safe-states.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector eventualSafeNodes(vector>& graph) { 4 | int n = graph.size(); 5 | vector safe(n, false), vis(n, false); 6 | vector ans; 7 | for(int i=0; i>& graph, vector& vis, int node, vector& safe){ 14 | vis[node] = true; 15 | bool ret = true; 16 | for(auto nei : graph[node]){ 17 | ret &= vis[nei] ? safe[nei] : dfs(graph, vis, nei, safe); 18 | } 19 | return safe[node] = ret; 20 | } 21 | }; -------------------------------------------------------------------------------- /0033-search-in-rotated-sorted-array/0033-search-in-rotated-sorted-array.py: -------------------------------------------------------------------------------- 1 | from typing import List 2 | 3 | class Solution: 4 | def search(self, nums: List[int], target: int) -> int: 5 | left, right = 0, len(nums) - 1 6 | 7 | while left <= right: 8 | mid = (left + right) // 2 9 | if nums[mid] == target: 10 | return mid 11 | if nums[left] <= nums[mid]: 12 | if nums[left] <= target < nums[mid]: 13 | right = mid - 1 14 | else: 15 | left = mid + 1 16 | else: 17 | if nums[mid] < target <= nums[right]: 18 | left = mid + 1 19 | else: 20 | right = mid - 1 21 | 22 | return -1 23 | -------------------------------------------------------------------------------- /2054-the-number-of-the-smallest-unoccupied-chair/2054-the-number-of-the-smallest-unoccupied-chair.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def smallestChair(self, times: List[List[int]], targetFriend: int) -> int: 3 | 4 | order = sorted(range(len(times)), key = lambda x: times[x][0]) 5 | emptySeats, takenSeats = list(range(len(times))), [] 6 | 7 | for i in order: 8 | ar, lv = times[i] 9 | 10 | while takenSeats and takenSeats[0][0] <= ar: 11 | heappush(emptySeats, heappop(takenSeats)[1]) 12 | seat = heappop(emptySeats) 13 | 14 | if i == targetFriend: return seat 15 | 16 | heappush(takenSeats,(lv, seat)) -------------------------------------------------------------------------------- /1886-minimum-limit-of-balls-in-a-bag/1886-minimum-limit-of-balls-in-a-bag.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minimumSize(vector& nums, int k) { 4 | int mx = 0; 5 | for(auto &i: nums){ 6 | mx = max(mx,i); 7 | } 8 | int mn = 1,mid,x,ans; 9 | while(mn<=mx){ 10 | x = 0; 11 | mid = (mx-mn)/2+mn; 12 | for(auto &i: nums){ 13 | if(i%mid){ 14 | x += i/mid; 15 | }else{ 16 | x += i/mid-1; 17 | } 18 | } 19 | if(x>k){ 20 | mn = mid+1; 21 | }else{ 22 | ans= mid; 23 | mx = mid-1; 24 | } 25 | } 26 | return ans; 27 | } 28 | }; -------------------------------------------------------------------------------- /2059-unique-length-3-palindromic-subsequences/2059-unique-length-3-palindromic-subsequences.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def countPalindromicSubsequence(self, s): 3 | """ 4 | :type s: str 5 | :rtype: int 6 | """ 7 | first = [-1] * 26 8 | last = [-1] * 26 9 | for i in range(len(s)): 10 | curr = ord(s[i]) - ord('a') 11 | if first[curr] == -1: 12 | first[curr] = i 13 | last[curr] = i 14 | 15 | ans = 0 16 | for i in range(26): 17 | if first[i] == -1: 18 | continue 19 | 20 | between = set() 21 | for j in range(first[i] + 1, last[i]): 22 | between.add(s[j]) 23 | ans += len(between) 24 | 25 | return ans -------------------------------------------------------------------------------- /3291-find-if-array-can-be-sorted/3291-find-if-array-can-be-sorted.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean canSortArray(int[] nums) { 3 | int[]ans=new int[nums.length]; 4 | for(int i=0;inums[j+1]){ 12 | int t=nums[j]; 13 | nums[j]=nums[j+1]; 14 | nums[j+1]=t; 15 | } 16 | 17 | } 18 | } 19 | return Arrays.equals(nums,ans); 20 | 21 | } 22 | } -------------------------------------------------------------------------------- /0006-zigzag-conversion/0006-zigzag-conversion.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string convert(string s, int numRows) { 4 | if (numRows == 1 || numRows >= s.length()) { 5 | return s; 6 | } 7 | 8 | int idx = 0, d = 1; 9 | vector> rows(numRows); 10 | 11 | for (char c : s) { 12 | rows[idx].push_back(c); 13 | if (idx == 0) { 14 | d = 1; 15 | } else if (idx == numRows - 1) { 16 | d = -1; 17 | } 18 | idx += d; 19 | } 20 | 21 | string result; 22 | for (const auto& row : rows) { 23 | for (char c : row) { 24 | result += c; 25 | } 26 | } 27 | 28 | return result; 29 | } 30 | }; -------------------------------------------------------------------------------- /1679-shortest-subarray-to-be-removed-to-make-array-sorted/1679-shortest-subarray-to-be-removed-to-make-array-sorted.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findLengthOfShortestSubarray(vector& arr) { 4 | const int n = arr.size(); 5 | int left= 0; 6 | for (; left+1 < n && arr[left] <= arr[left+1]; left++); 7 | if (left== n-1) return 0; 8 | int right = n-1; 9 | for (; right>left && arr[right-1] <= arr[right]; right--); 10 | int remove = min(n-left-1, right); 11 | for (int i = 0, j = right; i <= left && j < n; ) { 12 | if (arr[i] <= arr[j]) { 13 | remove = min(remove, j-i-1); 14 | i++; 15 | } 16 | else j++; 17 | } 18 | 19 | return remove; 20 | } 21 | }; -------------------------------------------------------------------------------- /3622-maximum-frequency-of-an-element-after-performing-operations-i/3622-maximum-frequency-of-an-element-after-performing-operations-i.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def maxFrequency(self, nums: List[int], k: int, numOps: int) -> int: 3 | maxVal = max(nums) + k + 2 4 | count = [0] * maxVal 5 | 6 | for v in nums: 7 | count[v] += 1 8 | 9 | for i in range(1, maxVal): 10 | count[i] += count[i - 1] 11 | 12 | res = 0 13 | for i in range(maxVal): 14 | left = max(0, i - k) 15 | right = min(maxVal - 1, i + k) 16 | total = count[right] - (count[left - 1] if left else 0) 17 | freq = count[i] - (count[i - 1] if i else 0) 18 | res = max(res, freq + min(numOps, total - freq)) 19 | 20 | return res 21 | -------------------------------------------------------------------------------- /2265-partition-array-according-to-given-pivot/2265-partition-array-according-to-given-pivot.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector pivotArray(vector& nums, int pivot) { 4 | vector less_than; 5 | vector equal_to; 6 | vector greater_than; 7 | for(int num:nums){ 8 | if(num < pivot){ 9 | less_than.push_back(num); 10 | } else if(num == pivot){ 11 | equal_to.push_back(num); 12 | } else{ 13 | greater_than.push_back(num); 14 | } 15 | } 16 | less_than.insert(less_than.end(), equal_to.begin(), equal_to.end()); 17 | less_than.insert(less_than.end(), greater_than.begin(), greater_than.end()); 18 | 19 | return less_than; 20 | 21 | } 22 | }; -------------------------------------------------------------------------------- /0015-3sum/0015-3sum.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def threeSum(self, nums: List[int]) -> List[List[int]]: 3 | res = [] 4 | nums.sort() 5 | 6 | for i in range(len(nums)): 7 | if i > 0 and nums[i] == nums[i-1]: 8 | continue 9 | 10 | j = i + 1 11 | k = len(nums) - 1 12 | 13 | while j < k: 14 | total = nums[i] + nums[j] + nums[k] 15 | 16 | if total > 0: 17 | k -= 1 18 | elif total < 0: 19 | j += 1 20 | else: 21 | res.append([nums[i], nums[j], nums[k]]) 22 | j += 1 23 | 24 | while nums[j] == nums[j-1] and j < k: 25 | j += 1 26 | 27 | return res -------------------------------------------------------------------------------- /0007-reverse-integer/0007-reverse-integer.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int reverse(int x) { 3 | int num = Math.abs(x); // Original number ka absolute value nikala 4 | 5 | int rev = 0; // Reversed number 6 | 7 | while (num != 0) { 8 | int ld = num % 10; // Last digit nikala 9 | 10 | // Overflow check 11 | if (rev > (Integer.MAX_VALUE - ld) / 10) { 12 | return 0; // Agar overflow hua, toh 0 return kardo 13 | } 14 | 15 | rev = rev * 10 + ld; // Reverse mein digit ko add kiya 16 | num = num / 10; // Last digit hata diya, next iteration ke liye 17 | } 18 | 19 | return (x < 0) ? (-rev) : rev; // Original number ke sign ke hisaab se result diya 20 | } 21 | } -------------------------------------------------------------------------------- /2753-minimum-number-of-operations-to-make-all-array-elements-equal-to-1/2753-minimum-number-of-operations-to-make-all-array-elements-equal-to-1.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minOperations(vector& nums) { 4 | const int n = nums.size(); 5 | int res = INT_MAX, cnt1 = 0; 6 | for (int i = 0; i < n; ++i) 7 | cnt1 += (nums[i] == 1); 8 | if (cnt1) 9 | return n - cnt1; 10 | for (int i = 0; i < n; ++i) { 11 | int g = nums[i]; 12 | for (int j = i + 1; j < n; ++j) { 13 | g = __gcd(g, nums[j]); 14 | if (g == 1) { 15 | res = min(res, j - i + n - 1); 16 | break; 17 | } 18 | } 19 | } 20 | return res == INT_MAX ? -1 : res; 21 | } 22 | }; -------------------------------------------------------------------------------- /0044-wildcard-matching/0044-wildcard-matching.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isMatch(string s, string p) { 4 | int i = 0, j = 0, sTmpIdx = -1, starIdx = -1; 5 | int m = s.size(), n = p.size(); 6 | 7 | while (i < m) { 8 | if (j < n && (p[j] == '?' || p[j] == s[i])) { 9 | i++; 10 | j++; 11 | } else if (j < n && p[j] == '*') { 12 | starIdx = j; 13 | sTmpIdx = i; 14 | j++; 15 | } else if (starIdx != -1) { 16 | j = starIdx + 1; 17 | sTmpIdx++; 18 | i = sTmpIdx; 19 | } else { 20 | return false; 21 | } 22 | } 23 | 24 | while (j < n && p[j] == '*') j++; 25 | 26 | return j == n; 27 | } 28 | }; -------------------------------------------------------------------------------- /1715-split-a-string-into-the-max-number-of-unique-substrings/1715-split-a-string-into-the-max-number-of-unique-substrings.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int maxUniqueSplit(String s) { 3 | return backtrack(0, s, new HashSet<>()); 4 | } 5 | private int backtrack(int start, String s, HashSet seen) { 6 | if (start == s.length()) { 7 | return 0; 8 | } 9 | int maxSplits = 0; 10 | for (int end = start + 1; end <= s.length(); end++) { 11 | String substring = s.substring(start, end); 12 | if (!seen.contains(substring)) { 13 | seen.add(substring); 14 | maxSplits = Math.max(maxSplits, 1 + backtrack(end, s, seen)); 15 | seen.remove(substring); 16 | } 17 | } 18 | return maxSplits; 19 | } 20 | } -------------------------------------------------------------------------------- /1755-defuse-the-bomb/1755-defuse-the-bomb.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector decrypt(vector& code, int k) { 4 | int n=code.size(); 5 | vector ans(n, 0); 6 | if (k==0) return ans; 7 | if (k>0){ 8 | int wsum=accumulate(code.begin()+1, code.begin()+k+1, 0); 9 | ans[0]=wsum; 10 | for(int l=1, r=k+1; l= 0 && j>=0) { 10 | char ch1 = str.charAt(i--); 11 | char ch2 = strn.charAt(j); 12 | if(ch1 == '1') { 13 | res += curr; 14 | } else { 15 | if(ch2 == '1') { 16 | res += curr; 17 | } 18 | j--; 19 | } 20 | curr += curr; 21 | } 22 | 23 | while(i >= 0) { 24 | char ch = str.charAt(i--); 25 | if(ch == '1') { 26 | res += curr; 27 | } 28 | curr += curr; 29 | } 30 | 31 | while(j >= 0) { 32 | char ch = strn.charAt(j--); 33 | if(ch == '1') { 34 | res += curr; 35 | } 36 | curr += curr; 37 | } 38 | 39 | return res; 40 | } 41 | } -------------------------------------------------------------------------------- /2434-design-a-number-container-system/2434-design-a-number-container-system.py: -------------------------------------------------------------------------------- 1 | from sortedcontainers import SortedList 2 | 3 | class NumberContainers: 4 | 5 | def __init__(self): 6 | self.num_to_indices = defaultdict(SortedList) 7 | self.index_to_num = {} 8 | 9 | def change(self, index: int, number: int) -> None: 10 | if index in self.index_to_num: 11 | old = self.index_to_num[index] 12 | self.num_to_indices[old].discard(index) 13 | if not self.num_to_indices[old]: 14 | del self.num_to_indices[old] 15 | self.num_to_indices[number].add(index) 16 | self.index_to_num[index] = number 17 | 18 | 19 | def find(self, number: int) -> int: 20 | if number in self.num_to_indices: 21 | return self.num_to_indices[number][0] 22 | return -1 -------------------------------------------------------------------------------- /2792-neighboring-bitwise-xor/2792-neighboring-bitwise-xor.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool doesValidArrayExist(vector& derived) { 4 | // Create an original array initialized with 0. 5 | vector original = {0}; 6 | for (int i = 0; i < derived.size(); i++) { 7 | original.push_back((derived[i] ^ original[i])); 8 | } 9 | // Store the validation results in checkForZero and checkForOne 10 | // respectively. 11 | bool checkForZero = (original[0] == original[original.size() - 1]); 12 | original = {1}; 13 | for (int i = 0; i < derived.size(); i++) { 14 | original.push_back((derived[i] ^ original[i])); 15 | } 16 | bool checkForOne = (original[0] == original[original.size() - 1]); 17 | 18 | return checkForZero | checkForOne; 19 | } 20 | }; -------------------------------------------------------------------------------- /1667-find-kth-bit-in-nth-binary-string/1667-find-kth-bit-in-nth-binary-string.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public char findKthBit(int n, int k) { 3 | // Base case: When n = 1, the binary string is "0" 4 | if (n == 1) return '0'; 5 | 6 | // Find the length of the current string Sn, which is 2^n - 1 7 | int length = (1 << n) - 1; 8 | 9 | // Find the middle position 10 | int mid = length / 2 + 1; 11 | 12 | // If k is the middle position, return '1' 13 | if (k == mid) return '1'; 14 | 15 | // If k is in the first half, find the bit in Sn-1 16 | if (k < mid) return findKthBit(n - 1, k); 17 | 18 | // If k is in the second half, find the bit in Sn-1 and invert it 19 | return findKthBit(n - 1, length - k + 1) == '0' ? '1' : '0'; 20 | } 21 | } -------------------------------------------------------------------------------- /1524-string-matching-in-an-array/1524-string-matching-in-an-array.cpp: -------------------------------------------------------------------------------- 1 | 2 | class Solution { 3 | public: 4 | vector stringMatching(vector& words) { 5 | vector result; 6 | 7 | sort(words.begin(), words.end(), 8 | [&](const string &a, const string &b) -> bool { 9 | return a.size() > b.size(); 10 | }); 11 | 12 | for(int i = 0; i < words.size(); ++i) { 13 | for(int j = i + 1; j < words.size(); ++j) { 14 | if(words[i].find(words[j]) != string::npos) { 15 | result.push_back(words[j]); 16 | } 17 | } 18 | } 19 | 20 | sort(result.begin(), result.end()); 21 | result.erase(unique(result.begin(), result.end()), result.end()); 22 | 23 | return result; 24 | } 25 | }; -------------------------------------------------------------------------------- /1667-find-kth-bit-in-nth-binary-string/1667-find-kth-bit-in-nth-binary-string.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | char findKthBit(int n, int k) { 4 | // Base case: When n = 1, the binary string is "0" 5 | if (n == 1) return '0'; 6 | 7 | // Find the length of the current string Sn, which is 2^n - 1 8 | int length = (1 << n) - 1; 9 | 10 | // Find the middle position 11 | int mid = length / 2 + 1; 12 | 13 | // If k is the middle position, return '1' 14 | if (k == mid) return '1'; 15 | 16 | // If k is in the first half, find the bit in Sn-1 17 | if (k < mid) return findKthBit(n - 1, k); 18 | 19 | // If k is in the second half, find the bit in Sn-1 and invert it 20 | return findKthBit(n - 1, length - k + 1) == '0' ? '1' : '0'; 21 | } 22 | }; -------------------------------------------------------------------------------- /3606-coupon-code-validator/3606-coupon-code-validator.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def validateCoupons(self, code, businessLine, isActive): 3 | # Business line priority 4 | priority = { 5 | "electronics": 0, 6 | "grocery": 1, 7 | "pharmacy": 2, 8 | "restaurant": 3 9 | } 10 | 11 | # Check if coupon code is valid 12 | def is_valid_code(s): 13 | return len(s) > 0 and all(c.isalnum() or c == '_' for c in s) 14 | 15 | valid = [] 16 | 17 | for i in range(len(code)): 18 | if ( 19 | isActive[i] and 20 | businessLine[i] in priority and 21 | is_valid_code(code[i]) 22 | ): 23 | valid.append((priority[businessLine[i]], code[i])) 24 | 25 | valid.sort() 26 | return [c for _, c in valid] -------------------------------------------------------------------------------- /0029-divide-two-integers/0029-divide-two-integers.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int divide(int dividend, int divisor) { 4 | if (dividend == INT_MIN && divisor == -1) return INT_MAX; 5 | if (abs(divisor) == 1) return dividend * divisor; 6 | 7 | bool isNegative = (dividend < 0) ^ (divisor < 0); 8 | int count = 0; 9 | 10 | long long absDividend = abs((long long)dividend); 11 | long long absDivisor = abs((long long)divisor); 12 | 13 | while (absDividend >= absDivisor) { 14 | long long base = absDivisor; 15 | int x = 1; 16 | while (base <= (absDividend >> 1)) { 17 | base <<= 1; 18 | x <<= 1; 19 | } 20 | count += x; 21 | absDividend -= base; 22 | } 23 | 24 | return isNegative ? -count : count; 25 | } 26 | }; -------------------------------------------------------------------------------- /3616-make-array-elements-equal-to-zero/3616-make-array-elements-equal-to-zero.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def countValidSelections(self, nums: List[int]) -> int: 3 | n = len(nums) 4 | count = 0 5 | def simulate(start,direction): 6 | copy = nums[:] 7 | curr = start 8 | while 0 <= curr < n: 9 | if copy[curr] == 0: 10 | curr += direction 11 | else: 12 | copy[curr] -= 1 13 | direction *= -1 14 | curr += direction 15 | return all(x==0 for x in copy) 16 | for i in range(n): 17 | if nums[i] == 0: 18 | if simulate(i,-1): 19 | count+=1 20 | if simulate(i,1): 21 | count+=1 22 | return count 23 | 24 | -------------------------------------------------------------------------------- /0029-divide-two-integers/0029-divide-two-integers.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int divide(int dividend, int divisor) { 3 | if (dividend == Integer.MIN_VALUE && divisor == -1) return Integer.MAX_VALUE; 4 | if (Math.abs(divisor) == 1) return dividend * divisor; 5 | 6 | boolean isNegative = (dividend < 0) ^ (divisor < 0); 7 | int count = 0; 8 | 9 | long absDividend = Math.abs((long)dividend); 10 | long absDivisor = Math.abs((long)divisor); 11 | 12 | while (absDividend >= absDivisor) { 13 | long base = absDivisor; 14 | int x = 1; 15 | while (base <= (absDividend >> 1)) { 16 | base <<= 1; 17 | x <<= 1; 18 | } 19 | count += x; 20 | absDividend -= base; 21 | } 22 | 23 | return isNegative ? -count : count; 24 | } 25 | } -------------------------------------------------------------------------------- /0165-compare-version-numbers/0165-compare-version-numbers.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int compareVersion(string version1, string version2) { 4 | size_t i = 0, j = 0; 5 | while (i < version1.size() || j < version2.size()) { 6 | int x = 0; 7 | while (i < version1.size() && version1[i] != '.') { 8 | x = x * 10 + (version1[i] - '0'); 9 | i++; 10 | } 11 | int y = 0; 12 | while (j < version2.size() && version2[j] != '.') { 13 | y = y * 10 + (version2[j] - '0'); 14 | j++; 15 | } 16 | if (x < y) return -1; 17 | if (x > y) return 1; 18 | if (i < version1.size() && version1[i] == '.') i++; 19 | if (j < version2.size() && version2[j] == '.') j++; 20 | } 21 | return 0; 22 | } 23 | }; -------------------------------------------------------------------------------- /1972-rotating-the-box/1972-rotating-the-box.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> rotateTheBox(vector>& box) { 4 | int n = box.size(), m = box[0].size(); 5 | for (int i = 0; i < box.size(); ++i) { 6 | int next = m - 1; 7 | for (int j = m - 1; j >= 0; --j) { 8 | if (box[i][j] == '*') { 9 | next = j - 1; 10 | } else if (box[i][j] == '#') { 11 | swap(box[i][j], box[i][next]); 12 | next--; 13 | } 14 | } 15 | } 16 | vector> newMatrix(m, vector(n)); 17 | for (int i = 0; i < n; ++i) { 18 | for (int j = 0; j < m; ++j) { 19 | newMatrix[j][n - i - 1] = box[i][j]; 20 | } 21 | } 22 | return newMatrix; 23 | } 24 | }; -------------------------------------------------------------------------------- /0002-add-two-numbers/0002-add-two-numbers.py: -------------------------------------------------------------------------------- 1 | class ListNode: 2 | def __init__(self, val=0, next=None): 3 | self.val = val 4 | self.next = next 5 | class Solution: 6 | def addTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]: 7 | dummy = ListNode(0) 8 | curr = dummy 9 | carry = 0 10 | while l1 != None or l2 != None or carry != 0: 11 | l1val = l1.val if l1 != None else 0 12 | l2val = l2.val if l2 != None else 0 13 | whole_sum = l1val + l2val + carry 14 | carry = whole_sum // 10 15 | newNode = ListNode(whole_sum % 10) 16 | curr.next = newNode 17 | curr = curr.next 18 | l1 = l1.next if l1 != None else l1 19 | l2 = l2.next if l2 != None else l2 20 | 21 | return dummy.next -------------------------------------------------------------------------------- /3372-longest-strictly-increasing-or-strictly-decreasing-subarray/3372-longest-strictly-increasing-or-strictly-decreasing-subarray.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def longestMonotonicSubarray(self, nums: List[int]) -> int: 3 | max_len = 0 4 | n = len(nums) 5 | for i in range(n): 6 | count = 1 7 | for pos in range(i+1,n): 8 | if nums[pos] > nums[pos-1]: 9 | count += 1 10 | else: 11 | break 12 | max_len = max(max_len , count) 13 | for i in range(n): 14 | count = 1 15 | for pos in range(i+1,n): 16 | if nums[pos] < nums[pos-1]: 17 | count += 1 18 | else: 19 | break 20 | max_len = max(max_len , count) 21 | return max_len 22 | 23 | 24 | -------------------------------------------------------------------------------- /2414-move-pieces-to-obtain-a-string/2414-move-pieces-to-obtain-a-string.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def canChange(self, start: str, target: str) -> bool: 3 | if start == target: 4 | return True 5 | waitL = 0 6 | waitR = 0 7 | 8 | for curr, goal in zip(start, target): 9 | if curr == 'R': 10 | if waitL > 0: 11 | return False 12 | waitR += 1 13 | if goal == 'L': 14 | if waitR > 0: 15 | return False 16 | waitL += 1 17 | if goal == 'R': 18 | if waitR == 0: 19 | return False 20 | waitR -= 1 21 | if curr == 'L': 22 | if waitL == 0: 23 | return False 24 | waitL -= 1 25 | return waitL == 0 and waitR == 0 -------------------------------------------------------------------------------- /1747-lexicographically-smallest-string-after-applying-operations/1747-lexicographically-smallest-string-after-applying-operations.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findLexSmallestString(self, s: str, a: int, b: int) -> str: 3 | vis = set([s]) 4 | smallest = s 5 | q = [s] 6 | 7 | while q: 8 | cur = q.pop(0) 9 | if cur < smallest: 10 | smallest = cur 11 | 12 | chars = list(cur) 13 | for i in range(1, len(chars), 2): 14 | chars[i] = str((int(chars[i]) + a) % 10) 15 | added = ''.join(chars) 16 | if added not in vis: 17 | vis.add(added) 18 | q.append(added) 19 | 20 | rotated = cur[-b:] + cur[:-b] 21 | if rotated not in vis: 22 | vis.add(rotated) 23 | q.append(rotated) 24 | 25 | return smallest -------------------------------------------------------------------------------- /3451-string-compression-iii/3451-string-compression-iii.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string compressedString(string word) { 4 | int n = word.size(); 5 | int ans = 1; 6 | string rans = ""; 7 | for(int i=1;i seen; 5 | return backtrack(0, s, seen); 6 | } 7 | private: 8 | int backtrack(int start, const string& s, unordered_set& seen) { 9 | if (start == s.size()) { 10 | return 0; 11 | } 12 | int maxSplits = 0; 13 | for (int end = start + 1; end <= s.size(); ++end) { 14 | string substring = s.substr(start, end - start); 15 | if (seen.find(substring) == seen.end()) { 16 | seen.insert(substring); 17 | maxSplits = max(maxSplits, 1 + backtrack(end, s, seen)); 18 | seen.erase(substring); 19 | } 20 | } 21 | return maxSplits; 22 | } 23 | }; -------------------------------------------------------------------------------- /2707-merge-two-2d-arrays-by-summing-values/2707-merge-two-2d-arrays-by-summing-values.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def mergeArrays(self, nums1: List[List[int]], nums2: List[List[int]]) -> List[List[int]]: 3 | i , j = 0 , 0 4 | res = [] 5 | while i < len(nums1) and j < len(nums2): 6 | if nums1[i][0] < nums2[j][0]: 7 | res.append(nums1[i]) 8 | i += 1 9 | elif nums1[i][0] > nums2[j][0]: 10 | res.append(nums2[j]) 11 | j += 1 12 | else: 13 | res.append([nums1[i][0] , nums1[i][1] + nums2[j][1]]) 14 | i += 1 15 | j += 1 16 | while i < len(nums1): 17 | res.append(nums1[i]) 18 | i += 1 19 | while j < len(nums2): 20 | res.append(nums2[j]) 21 | j += 1 22 | return res 23 | 24 | 25 | -------------------------------------------------------------------------------- /3433-count-mentions-per-user/3433-count-mentions-per-user.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def countMentions(self, n: int, events: List[List[str]]) -> List[int]: 3 | mention = [0]*n 4 | back = [0]*n 5 | events.sort(key=lambda e: (int(e[1]),e[0]=="MESSAGE")) 6 | for typ , t , data in events: 7 | t = int(t) 8 | if typ == "OFFLINE": 9 | back[int(data)] = t+60 10 | continue 11 | 12 | for tok in data.split(): 13 | if tok == "ALL": 14 | for u in range(n): 15 | mention[u] += 1 16 | elif tok == "HERE": 17 | for u in range(n): 18 | if t >= back[u]: 19 | mention[u] += 1 20 | else: 21 | mention[int(tok[2:])]+=1 22 | return mention 23 | 24 | -------------------------------------------------------------------------------- /3522-find-the-power-of-k-size-subarrays-i/3522-find-the-power-of-k-size-subarrays-i.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector resultsArray(vector& nums, int k) { 4 | int n = nums.size(); 5 | vector result; 6 | 7 | for (int i = 0; i <= n - k; ++i) { 8 | bool is_valid = true; 9 | int max_element = nums[i]; 10 | 11 | for (int j = i; j < i + k - 1; ++j) { 12 | if (nums[j] + 1 != nums[j + 1]) { 13 | is_valid = false; 14 | break; 15 | } 16 | max_element = max(max_element, nums[j]); 17 | } 18 | 19 | if (is_valid) { 20 | result.push_back(max(max_element, nums[i + k - 1])); 21 | } else { 22 | result.push_back(-1); 23 | } 24 | } 25 | 26 | return result; 27 | } 28 | }; -------------------------------------------------------------------------------- /3531-count-covered-buildings/3531-count-covered-buildings.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int countCoveredBuildings(int n, vector>& buildings) { 4 | vector minx(n + 1, n + 1); 5 | vector maxx(n + 1, 0); 6 | vector miny(n + 1, n + 1); 7 | vector maxy(n + 1, 0); 8 | for(auto b : buildings){ 9 | int x = b[0]; 10 | int y = b[1]; 11 | minx[y] = min(minx[y], x); 12 | maxx[y] = max(maxx[y], x); 13 | miny[x] = min(miny[x], y); 14 | maxy[x] = max(maxy[x], y); 15 | } 16 | int ans = 0; 17 | for(auto b : buildings){ 18 | int x = b[0]; 19 | int y = b[1]; 20 | if(minx[y] < x && x < maxx[y] && miny[x] < y && y < maxy[x]){ 21 | ans += 1; 22 | } 23 | } 24 | return ans; 25 | } 26 | }; -------------------------------------------------------------------------------- /0670-maximum-swap/README.md: -------------------------------------------------------------------------------- 1 |

670. Maximum Swap

Medium


You are given an integer num. You can swap two digits at most once to get the maximum valued number.

2 | 3 |

Return the maximum valued number you can get.

4 | 5 |

 

6 |

Example 1:

7 | 8 |
 9 | Input: num = 2736
10 | Output: 7236
11 | Explanation: Swap the number 2 and the number 7.
12 | 
13 | 14 |

Example 2:

15 | 16 |
17 | Input: num = 9973
18 | Output: 9973
19 | Explanation: No swap.
20 | 
21 | 22 |

 

23 |

Constraints:

24 | 25 |
    26 |
  • 0 <= num <= 108
  • 27 |
28 | -------------------------------------------------------------------------------- /2465-shifting-letters-ii/2465-shifting-letters-ii.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string shiftingLetters(string s, vector>& shifts) { 4 | int n = s.size(); 5 | vector shift(n + 1, 0); 6 | 7 | // Process the shifts 8 | for (auto& shiftOp : shifts) { 9 | int start = shiftOp[0], end = shiftOp[1], direction = shiftOp[2]; 10 | shift[start] += (direction == 1 ? 1 : -1); 11 | shift[end + 1] -= (direction == 1 ? 1 : -1); 12 | } 13 | 14 | int currentShift = 0; 15 | for (int i = 0; i < n; ++i) { 16 | currentShift += shift[i]; 17 | shift[i] = currentShift; 18 | } 19 | 20 | for (int i = 0; i < n; ++i) { 21 | int netShift = (shift[i] % 26 + 26) % 26; 22 | s[i] = 'a' + (s[i] - 'a' + netShift) % 26; 23 | } 24 | 25 | return s; 26 | } 27 | }; -------------------------------------------------------------------------------- /3610-find-x-sum-of-all-k-long-subarrays-i/3610-find-x-sum-of-all-k-long-subarrays-i.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector findXSum(vector& nums, int k, int x) { 4 | vector result; 5 | 6 | auto do_sum = [&](int idx) -> int { 7 | unordered_map ctr; 8 | 9 | for (int i = idx; i < idx + k; ++i) { 10 | ctr[nums[i]]++;} 11 | 12 | priority_queue> pq; 13 | 14 | for (auto& p : ctr) { 15 | pq.push({p.second, p.first}); } 16 | 17 | int sum = 0; 18 | for (int i = 0; i < x && !pq.empty(); ++i) { 19 | sum += pq.top().second * pq.top().first; 20 | pq.pop();} 21 | 22 | return sum;}; 23 | 24 | for (int i = 0; i <= nums.size() - k; ++i) { 25 | result.push_back(do_sum(i));} 26 | 27 | return result;} 28 | }; -------------------------------------------------------------------------------- /0067-add-binary/README.md: -------------------------------------------------------------------------------- 1 |

67. Add Binary

Easy


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

2 | 3 |

 

4 |

Example 1:

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

Example 2:

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

 

12 |

Constraints:

13 | 14 |
    15 |
  • 1 <= a.length, b.length <= 104
  • 16 |
  • a and b consist only of '0' or '1' characters.
  • 17 |
  • Each string does not contain leading zeros except for the zero itself.
  • 18 |
19 | -------------------------------------------------------------------------------- /0494-target-sum/0494-target-sum.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findTargetSumWays(self, nums: List[int], target: int) -> int: 3 | # Calculate the total sum of nums 4 | total_sum = sum(nums) 5 | 6 | # Edge case: if target is out of bounds of achievable sums 7 | if target > total_sum or (total_sum - target) % 2 != 0: 8 | return 0 9 | 10 | # The actual sum we need to find in subset (derived from equation) 11 | s = (total_sum - target) // 2 12 | 13 | # Initialize the DP table where dp[j] is the number of ways to sum to j 14 | dp = [0] * (s + 1) 15 | dp[0] = 1 # There's one way to get sum 0, by choosing nothing 16 | 17 | # Update dp table for each number in nums 18 | for num in nums: 19 | for j in range(s, num - 1, -1): # Traverse backward to avoid overwriting 20 | dp[j] += dp[j - num] 21 | 22 | return dp[s] -------------------------------------------------------------------------------- /0892-shortest-subarray-with-sum-at-least-k/0892-shortest-subarray-with-sum-at-least-k.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int shortestSubarray(vector& nums, int k) { 4 | int n = nums.size(); 5 | vector sum(n + 1); 6 | 7 | for (int i = 0; i < n; i++) { 8 | sum[i + 1] = sum[i] + nums[i]; 9 | } 10 | 11 | vector q(n + 1); 12 | int l = 0, r = 0; 13 | int minLength = n + 1; 14 | 15 | for (int i = 0; i < sum.size(); i++) { 16 | while (r > l && sum[i] >= sum[q[l]] + k) { 17 | minLength = min(minLength, i - q[l++]); 18 | } 19 | 20 | while (r > l && sum[i] <= sum[q[r - 1]]) { 21 | r--; 22 | } 23 | 24 | q[r++] = i; 25 | } 26 | 27 | return minLength <= n ? minLength : -1; 28 | } 29 | }; -------------------------------------------------------------------------------- /0892-shortest-subarray-with-sum-at-least-k/0892-shortest-subarray-with-sum-at-least-k.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int shortestSubarray(int[] nums, int k) { 3 | int n = nums.length; 4 | long[] sum = new long[n + 1]; 5 | 6 | for (int i = 0; i < n; i++) { 7 | sum[i + 1] = sum[i] + nums[i]; 8 | } 9 | 10 | int[] q = new int[n + 1]; 11 | int l = 0, r = 0; 12 | int minLength = n + 1; 13 | 14 | for (int i = 0; i < sum.length; i++) { 15 | while (r > l && sum[i] >= sum[q[l]] + k) { 16 | minLength = Math.min(minLength, i - q[l++]); 17 | } 18 | 19 | while (r > l && sum[i] <= sum[q[r - 1]]) { 20 | r--; 21 | } 22 | 23 | q[r++] = i; 24 | } 25 | 26 | return minLength <= n ? minLength : -1; 27 | } 28 | } -------------------------------------------------------------------------------- /2868-continuous-subarrays/2868-continuous-subarrays.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public long continuousSubarrays(int[] nums) { 3 | int n = nums.length; 4 | int j = 0; 5 | long cnt = 0; 6 | 7 | Map map = new HashMap<>(); 8 | 9 | for (int i = 0; i < n; i++) { 10 | map.put(nums[i], map.getOrDefault(nums[i], 0) + 1); 11 | 12 | while ((i - j + 1) > getCount(nums[i], map)) { 13 | map.put(nums[j], map.get(nums[j]) - 1); 14 | 15 | j++; 16 | } 17 | 18 | cnt += (i - j + 1); 19 | } 20 | 21 | return cnt; 22 | } 23 | 24 | private int getCount(int num, Map map) { 25 | return map.getOrDefault(num, 0) + map.getOrDefault(num - 1, 0) + 26 | map.getOrDefault(num + 1, 0) + map.getOrDefault(num - 2, 0) + 27 | map.getOrDefault(num + 2, 0); 28 | } 29 | } -------------------------------------------------------------------------------- /2552-maximum-sum-of-distinct-subarrays-with-length-k/2552-maximum-sum-of-distinct-subarrays-with-length-k.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | long long maximumSubarraySum(vector& nums, int k) { 4 | int n=nums.size(); 5 | unordered_mapmp; 6 | long long start=0,cnt=0,ans=0; 7 | for(int i=0;i List[int]: 3 | n = len(nums) 4 | sorted_enum = sorted((num, i) for i, num in enumerate(nums)) 5 | 6 | new_positions = [] 7 | curr_positions = [] 8 | prev = float('-inf') 9 | 10 | for num, idx in sorted_enum: 11 | if num > prev + limit: 12 | new_positions.extend(sorted(curr_positions)) 13 | curr_positions = [idx] 14 | else: 15 | curr_positions.append(idx) 16 | prev = num 17 | new_positions.extend(sorted(curr_positions)) 18 | res = [0] * n 19 | for i, idx in enumerate(new_positions): 20 | res[idx] = sorted_enum[i][0] 21 | 22 | return res -------------------------------------------------------------------------------- /0204-count-primes/README.md: -------------------------------------------------------------------------------- 1 |

204. Count Primes

Medium


Given an integer n, return the number of prime numbers that are strictly less than n.

2 | 3 |

 

4 |

Example 1:

5 | 6 |
 7 | Input: n = 10
 8 | Output: 4
 9 | Explanation: There are 4 prime numbers less than 10, they are 2, 3, 5, 7.
10 | 
11 | 12 |

Example 2:

13 | 14 |
15 | Input: n = 0
16 | Output: 0
17 | 
18 | 19 |

Example 3:

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

 

27 |

Constraints:

28 | 29 |
    30 |
  • 0 <= n <= 5 * 106
  • 31 |
32 | -------------------------------------------------------------------------------- /3483-alternating-groups-ii/3483-alternating-groups-ii.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def numberOfAlternatingGroups(self, colors, k): 3 | n = len(colors) 4 | if n < k: 5 | return 0 6 | 7 | count = 0 8 | length = 1 9 | prev_color = colors[0] 10 | 11 | for i in range(1, n): 12 | if colors[i] != prev_color: 13 | length += 1 14 | if length >= k: 15 | count += 1 16 | else: 17 | length = 1 18 | prev_color = colors[i] 19 | 20 | if k > 1: 21 | for i in range(min(k - 1, n)): 22 | if colors[i] != prev_color: 23 | length += 1 24 | if length >= k: 25 | count += 1 26 | else: 27 | break 28 | prev_color = colors[i] 29 | 30 | return count -------------------------------------------------------------------------------- /0628-maximum-product-of-three-numbers/README.md: -------------------------------------------------------------------------------- 1 |

628. Maximum Product of Three Numbers

Easy


Given an integer array nums, find three numbers whose product is maximum and return the maximum product.

2 | 3 |

 

4 |

Example 1:

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

Example 2:

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

Example 3:

11 |
Input: nums = [-1,-2,-3]
12 | Output: -6
13 | 
14 |

 

15 |

Constraints:

16 | 17 |
    18 |
  • 3 <= nums.length <= 104
  • 19 |
  • -1000 <= nums[i] <= 1000
  • 20 |
21 | -------------------------------------------------------------------------------- /2488-divide-intervals-into-minimum-number-of-groups/2488-divide-intervals-into-minimum-number-of-groups.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minGroups(vector>& intervals) { 4 | vector start_times, end_times; 5 | 6 | // Extract start and end times 7 | for (const auto& interval : intervals) { 8 | start_times.push_back(interval[0]); 9 | end_times.push_back(interval[1]); 10 | } 11 | 12 | // Sort start and end times 13 | sort(start_times.begin(), start_times.end()); 14 | sort(end_times.begin(), end_times.end()); 15 | 16 | int end_ptr = 0, group_count = 0; 17 | 18 | // Traverse through the start times 19 | for (int start : start_times) { 20 | if (start > end_times[end_ptr]) { 21 | end_ptr++; 22 | } else { 23 | group_count++; 24 | } 25 | } 26 | 27 | return group_count; 28 | } 29 | }; -------------------------------------------------------------------------------- /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 n = intervals.length; 4 | int[] start_times = new int[n]; 5 | int[] end_times = new int[n]; 6 | 7 | // Extract start and end times 8 | for (int i = 0; i < n; i++) { 9 | start_times[i] = intervals[i][0]; 10 | end_times[i] = intervals[i][1]; 11 | } 12 | 13 | // Sort start and end times 14 | Arrays.sort(start_times); 15 | Arrays.sort(end_times); 16 | 17 | int end_ptr = 0, group_count = 0; 18 | 19 | // Traverse through the start times 20 | for (int start : start_times) { 21 | if (start > end_times[end_ptr]) { 22 | end_ptr++; 23 | } else { 24 | group_count++; 25 | } 26 | } 27 | 28 | return group_count; 29 | } 30 | } -------------------------------------------------------------------------------- /0046-permutations/0046-permutations.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> permute(vector& nums) { 4 | vector> res; 5 | if (nums.size() == 1) { 6 | vector singleList; 7 | singleList.push_back(nums[0]); 8 | res.push_back(singleList); 9 | return res; 10 | } 11 | 12 | for (int i = 0; i < nums.size(); i++) { 13 | int n = nums[i]; 14 | vector remainingNums; 15 | for (int j = 0; j < nums.size(); j++) { 16 | if (j != i) { 17 | remainingNums.push_back(nums[j]); 18 | } 19 | } 20 | 21 | vector> perms = permute(remainingNums); 22 | for (vector p : perms) { 23 | p.insert(p.begin(), n); 24 | res.push_back(p); 25 | } 26 | } 27 | 28 | return res; 29 | } 30 | }; -------------------------------------------------------------------------------- /0670-maximum-swap/0670-maximum-swap.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maximumSwap(int num) { 4 | vectordigits; 5 | while(num>0){ 6 | digits.push_back(num%10); 7 | num/=10; 8 | } 9 | int maxi=0; 10 | vectortemp(digits.size()); 11 | temp[0]=-1; 12 | for(int i=1;idigits[i]?maxi:-1; 14 | if(digits[i]>digits[maxi]){ 15 | maxi=i; 16 | } 17 | } 18 | 19 | for(int i=temp.size()-1;i>=0;i--){ 20 | if(temp[i]!=-1){ 21 | swap(digits[i],digits[temp[i]]); 22 | break; 23 | } 24 | } 25 | int mul=pow(10,temp.size()-1); 26 | int org=0; 27 | for(int i=digits.size()-1;i>=0;i--){ 28 | org+=(digits[i]*mul); 29 | mul/=10; 30 | } 31 | 32 | return org; 33 | 34 | 35 | } 36 | }; -------------------------------------------------------------------------------- /3573-best-time-to-buy-and-sell-stock-v/3573-best-time-to-buy-and-sell-stock-v.cpp: -------------------------------------------------------------------------------- 1 | 2 | class Solution { 3 | public: 4 | long long maximumProfit(vector& prices, int k) { 5 | int n = prices.size(); 6 | vector prev(n, 0), curr(n, 0); 7 | 8 | for (int t = 1; t <= k; t++) { 9 | long long bestLong = -prices[0]; 10 | long long bestShort = prices[0]; 11 | curr[0] = 0; 12 | 13 | for (int i = 1; i < n; i++) { 14 | long long res = curr[i - 1]; 15 | res = max(res, (long long)prices[i] + bestLong); 16 | res = max(res, - (long long)prices[i] + bestShort); 17 | curr[i] = res; 18 | 19 | bestLong = max(bestLong, prev[i - 1] - (long long)prices[i]); 20 | bestShort = max(bestShort, prev[i - 1] + (long long)prices[i]); 21 | } 22 | prev.swap(curr); 23 | } 24 | 25 | return prev[n - 1]; 26 | } 27 | }; -------------------------------------------------------------------------------- /0952-word-subsets/0952-word-subsets.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def wordSubsets(self, words1: List[str], words2: List[str]) -> List[str]: 3 | maxCharFreq = [0] * 26 4 | tempCharFreq = [0] * 26 5 | 6 | for word in words2: 7 | for ch in word: 8 | tempCharFreq[ord(ch) - ord('a')] += 1 9 | for i in range(26): 10 | maxCharFreq[i] = max(maxCharFreq[i], tempCharFreq[i]) 11 | tempCharFreq = [0] * 26 12 | 13 | universalWords = [] 14 | 15 | for word in words1: 16 | for ch in word: 17 | tempCharFreq[ord(ch) - ord('a')] += 1 18 | isUniversal = True 19 | for i in range(26): 20 | if maxCharFreq[i] > tempCharFreq[i]: 21 | isUniversal = False 22 | break 23 | if isUniversal: 24 | universalWords.append(word) 25 | tempCharFreq = [0] * 26 26 | 27 | return universalWords -------------------------------------------------------------------------------- /0258-add-digits/README.md: -------------------------------------------------------------------------------- 1 |

258. Add Digits

Easy


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

2 | 3 |

 

4 |

Example 1:

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

Example 2:

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

 

23 |

Constraints:

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

 

30 |

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

31 | --------------------------------------------------------------------------------