├── .github └── ISSUE_TEMPLATE │ ├── bug_report.md │ ├── config.yml │ ├── custom.md │ └── feature_request.md ├── .gitignore ├── CODE_OF_CONDUCT.md ├── CONTRIBUTING.md ├── LICENSE ├── README.md ├── SECURITY.md └── algorithms ├── 000001-two-sum ├── README.md └── solution.php ├── 000002-add-two-numbers ├── README.md └── solution.php ├── 000003-longest-substring-without-repeating-characters ├── README.md └── solution.php ├── 000004-median-of-two-sorted-arrays ├── README.md └── solution.php ├── 000017-letter-combinations-of-a-phone-number ├── README.md └── solution.php ├── 000038-count-and-say ├── README.md └── solution.php ├── 000040-combination-sum-ii ├── README.md └── solution.php ├── 000042-trapping_rain_water ├── README.md └── solution.php ├── 000058-length-of-last-word ├── README.md └── solution.php ├── 000073-set-matrix-zeroes ├── README.md └── solution.php ├── 000075-sort-colors ├── README.md └── solution.php ├── 000078-subsets ├── README.md └── solution.php ├── 000079-word-search ├── README.md └── solution.php ├── 000085-maximal-rectangle ├── README.md └── solution.php ├── 000129-sum-root-to-leaf-numbers ├── README.md └── solution.php ├── 000131-palindrome-partitioning ├── README.md └── solution.php ├── 000135-candy ├── README.md └── solution.php ├── 000140-word-break-ii ├── README.md └── solution.php ├── 000145-binary-tree-postorder-traversal ├── README.md └── solution.php ├── 000165-compare-version-numbers ├── README.md └── solution.php ├── 000179-largest-number ├── README.md └── solution.php ├── 000200-number-of-islands ├── README.md └── solution.php ├── 000205-isomorphic-strings ├── README.md └── solution.php ├── 000214-shortest-palindrome ├── README.md └── solution.php ├── 000237-delete-node-in-a-linked-list ├── README.md └── solution.php ├── 000241-different-ways-to-add-parentheses ├── README.md └── solution.php ├── 000260-single-number-iii ├── README.md └── solution.php ├── 000264-ugly-number-ii ├── README.md └── solution.php ├── 000273-integer-to-english-words ├── README.md └── solution.php ├── 000310-minimum-height-trees ├── README.md └── solution.php ├── 000330-patching-array ├── README.md └── solution.php ├── 000344-reverse-string ├── README.md └── solution.php ├── 000350-intersection-of-two-arrays-ii ├── README.md └── solution.php ├── 000368-largest-divisible-subset ├── README.md └── solution.php ├── 000386-lexicographical-numbers ├── README.md └── solution.php ├── 000402-remove-k-digits ├── README.md └── solution.php ├── 000404-sum-of-left-leaves ├── README.md └── solution.php ├── 000407-trapping-rain-water-ii ├── README.md └── solution.php ├── 000409-longest-palindrome ├── README.md └── solution.php ├── 000416-partition-equal-subset-sum ├── README.md └── solution.php ├── 000432-all-oone-data-structure ├── README.md └── solution.php ├── 000440-k-th-smallest-in-lexicographical-order ├── README.md └── solution.php ├── 000445-add-two-numbers-ii ├── README.md └── solution.php ├── 000463-island-perimeter ├── README.md └── solution.php ├── 000476-number-complement ├── README.md └── solution.php ├── 000494-target-sum ├── README.md └── solution.php ├── 000502-ipo ├── README.md └── solution.php ├── 000506-relative-ranks ├── README.md └── solution.php ├── 000514-freedom-trail ├── README.md └── solution.php ├── 000515-find-largest-value-in-each-tree-row ├── README.md └── solution.php ├── 000523-continuous-subarray-sum ├── README.md └── solution.php ├── 000539-minimum-time-difference ├── README.md └── solution.php ├── 000542-01-matrix ├── README.md └── solution.php ├── 000552-student-attendance-record-ii ├── README.md └── solution.php ├── 000564-find-the-closest-palindrome ├── README.md └── solution.php ├── 000567-permutation-in-string ├── README.md └── solution.php ├── 000590-n-ary-tree-postorder-traversal ├── README.md └── solution.php ├── 000592-fraction-addition-and-subtraction ├── README.md └── solution.php ├── 000623-add-one-row-to-tree ├── README.md └── solution.php ├── 000624-maximum-distance-in-arrays ├── README.md └── solution.php ├── 000632-smallest-range-covering-elements-from-k-lists ├── README.md └── solution.php ├── 000633-sum-of-square-numbers ├── README.md └── solution.php ├── 000641-design-circular-deque ├── README.md └── solution.php ├── 000648-replace-words ├── README.md └── solution.php ├── 000650-2-keys-keyboard ├── README.md └── solution.php ├── 000664-strange-printer ├── README.md └── solution.php ├── 000670-maximum-swap ├── README.md └── solution.php ├── 000678-valid-parenthesis-string ├── README.md └── solution.php ├── 000684-redundant-connection ├── README.md └── solution.php ├── 000689-maximum-sum-of-3-non-overlapping-subarrays ├── README.md └── solution.php ├── 000703-kth-largest-element-in-a-stream ├── README.md └── solution.php ├── 000719-find-k-th-smallest-pair-distance ├── README.md └── solution.php ├── 000725-split-linked-list-in-parts ├── README.md └── solution.php ├── 000726-number-of-atoms ├── README.md └── solution.php ├── 000729-my-calendar-i ├── README.md └── solution.php ├── 000731-my-calendar-ii ├── README.md └── solution.php ├── 000752-open-the-lock ├── README.md └── solution.php ├── 000763-partition-labels ├── README.md └── solution.php ├── 000769-max-chunks-to-make-sorted ├── README.md └── solution.php ├── 000773-sliding-puzzle ├── README.md └── solution.php ├── 000781-rabbits-in-forest ├── README.md └── solution.php ├── 000786-k-th-smallest-prime-fraction ├── README.md └── solution.php ├── 000790-domino-and-tromino-tiling ├── README.md └── solution.php ├── 000796-rotate-string ├── README.md └── solution.php ├── 000802-find-eventual-safe-states ├── README.md └── solution.php ├── 000826-most-profit-assigning-work ├── README.md └── solution.php ├── 000827-making-a-large-island ├── README.md └── solution.php ├── 000834-sum-of-distances-in-tree ├── README.md └── solution.php ├── 000838-push-dominoes ├── README.md └── solution.php ├── 000840-magic-squares-in-grid ├── README.md └── solution.php ├── 000846-hand-of-straights ├── README.md └── solution.php ├── 000857-minimum-cost-to-hire-k-workers ├── README.md └── solution.php ├── 000860-lemonade-change ├── README.md └── solution.php ├── 000861-score-after-flipping-matrix ├── README.md └── solution.php ├── 000862-shortest-subarray-with-sum-at-least-k ├── README.md └── solution.php ├── 000873-length-of-longest-fibonacci-subsequence ├── README.md └── solution.php ├── 000874-walking-robot-simulation ├── README.md └── solution.php ├── 000881-boats-to-save-people ├── README.md └── solution.php ├── 000884-uncommon-words-from-two-sentences ├── README.md └── solution.php ├── 000885-spiral-matrix-iii ├── README.md └── solution.php ├── 000889-construct-binary-tree-from-preorder-and-postorder-traversal ├── README.md └── solution.php ├── 000909-snakes-and-ladders ├── README.md └── solution.php ├── 000912-sort-an-array ├── README.md └── solution.php ├── 000916-word-subsets ├── README.md └── solution.php ├── 000921-minimum-add-to-make-parentheses-valid ├── README.md └── solution.php ├── 000945-minimum-increment-to-make-array-unique ├── README.md └── solution.php ├── 000947-most-stones-removed-with-same-row-or-column ├── README.md └── solution.php ├── 000950-reveal-cards-in-increasing-order ├── README.md └── solution.php ├── 000951-flip-equivalent-binary-trees ├── README.md └── solution.php ├── 000959-regions-cut-by-slashes ├── README.md └── solution.php ├── 000962-maximum-width-ramp ├── README.md └── solution.php ├── 000974-subarray-sums-divisible-by-k ├── README.md └── solution.php ├── 000979-distribute-coins-in-binary-tree ├── README.md └── solution.php ├── 000983-minimum-cost-for-tickets ├── README.md └── solution.php ├── 000988-smallest-string-starting-from-leaf ├── README.md ├── solution.php └── solution.py ├── 000995-minimum-number-of-k-consecutive-bit-flips ├── README.md └── solution.php ├── 001002-find-common-characters ├── README.md └── solution.php ├── 001007-minimum-domino-rotations-for-equal-row ├── README.md └── solution.php ├── 001014-best-sightseeing-pair ├── README.md └── solution.php ├── 001028-recover-a-tree-from-preorder-traversal ├── README.md └── solution.php ├── 001038-binary-search-tree-to-greater-sum-tree ├── README.md └── solution.php ├── 001051-height-checker ├── README.md └── solution.php ├── 001052-grumpy-bookstore-owner ├── README.md └── solution.php ├── 001061-lexicographically-smallest-equivalent-string ├── README.md └── solution.php ├── 001072-flip-columns-for-maximum-number-of-equal-rows ├── README.md └── solution.php ├── 001079-letter-tile-possibilities ├── README.md └── solution.php ├── 001092-shortest-common-supersequence ├── README.md └── solution.php ├── 001105-filling-bookcase-shelves ├── README.md └── solution.php ├── 001106-parsing-a-boolean-expression ├── README.md └── solution.php ├── 001110-delete-nodes-and-return-forest ├── README.md └── solution.php ├── 001122-relative-sort-array ├── README.md └── solution.php ├── 001123-lowest-common-ancestor-of-deepest-leaves ├── README.md └── solution.php ├── 001128-number-of-equivalent-domino-pairs ├── README.md └── solution.php ├── 001137-n-th-tribonacci-number ├── README.md └── solution.php ├── 001140-stone-game-ii ├── README.md └── solution.php ├── 001190-reverse-substrings-between-each-pair-of-parentheses ├── README.md └── solution.php ├── 001208-get-equal-substrings-within-budget ├── README.md └── solution.php ├── 001219-path-with-maximum-gold ├── README.md └── solution.php ├── 001233-remove-sub-folders-from-the-filesystem ├── README.md └── solution.php ├── 001248-count-number-of-nice-subarrays ├── README.md └── solution.php ├── 001249-minimum-remove-to-make-valid-parentheses ├── README.md └── solution.php ├── 001255-maximum-score-words-formed-by-letters ├── README.md └── solution.php ├── 001261-find-elements-in-a-contaminated-binary-tree ├── README.md └── solution.php ├── 001267-count-servers-that-communicate ├── README.md └── solution.php ├── 001277-count-square-submatrices-with-all-ones ├── README.md └── solution.php ├── 001289-minimum-falling-path-sum-ii ├── README.md └── solution.php ├── 001295-find-numbers-with-even-number-of-digits ├── README.md └── solution.php ├── 001298-maximum-candies-you-can-get-from-boxes ├── README.md └── solution.php ├── 001310-xor-queries-of-a-subarray ├── README.md └── solution.php ├── 001325-delete-leaves-with-a-given-value ├── README.md └── solution.php ├── 001331-rank-transform-of-an-array ├── README.md └── solution.php ├── 001334-find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance ├── README.md └── solution.php ├── 001346-check-if-n-and-its-double-exist ├── README.md └── solution.php ├── 001352-product-of-the-last-k-numbers ├── README.md └── solution.php ├── 001358-number-of-substrings-containing-all-three-characters ├── README.md └── solution.php ├── 001367-linked-list-in-binary-tree ├── README.md └── solution.php ├── 001368-minimum-cost-to-make-at-least-one-valid-path-in-a-grid ├── README.md └── solution.php ├── 001371-find-the-longest-substring-containing-vowels-in-even-counts ├── README.md └── solution.php ├── 001380-lucky-numbers-in-a-matrix ├── README.md └── solution.php ├── 001381-design-a-stack-with-increment-operation ├── README.md └── solution.php ├── 001382-balance-a-binary-search-tree ├── README.md └── solution.php ├── 001395-count-number-of-teams ├── README.md └── solution.php ├── 001399-count-largest-group ├── README.md └── solution.php ├── 001400-construct-k-palindrome-strings ├── README.md └── solution.php ├── 001404-number-of-steps-to-reduce-a-number-in-binary-representation-to-one ├── README.md └── solution.php ├── 001405-longest-happy-string ├── README.md └── solution.php ├── 001408-string-matching-in-an-array ├── README.md └── solution.php ├── 001415-the-k-th-lexicographical-string-of-all-happy-strings-of-length-n ├── README.md └── solution.php ├── 001422-maximum-score-after-splitting-a-string ├── README.md └── solution.php ├── 001438-longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit ├── README.md └── solution.php ├── 001442-count-triplets-that-can-form-two-arrays-of-equal-xor ├── README.md └── solution.php ├── 001455-check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence ├── README.md └── solution.php ├── 001460-make-two-arrays-equal-by-reversing-subarrays ├── README.md └── solution.php ├── 001462-course-schedule-iv ├── README.md └── solution.php ├── 001475-final-prices-with-a-special-discount-in-a-shop ├── README.md └── solution.php ├── 001482-minimum-number-of-days-to-make-m-bouquets ├── README.md └── solution.php ├── 001497-check-if-array-pairs-are-divisible-by-k ├── README.md └── solution.php ├── 001508-range-sum-of-sorted-subarray-sums ├── README.md └── solution.php ├── 001509-minimum-difference-between-largest-and-smallest-value-in-three-moves ├── README.md └── solution.php ├── 001514-path-with-maximum-probability ├── README.md └── solution.php ├── 001518-water-bottles ├── README.md └── solution.php ├── 001524-number-of-sub-arrays-with-odd-sum ├── README.md └── solution.php ├── 001530-number-of-good-leaf-nodes-pairs ├── README.md └── solution.php ├── 001534-count-good-triplets ├── README.md └── solution.php ├── 001544-make-the-string-great ├── README.md └── solution.php ├── 001545-find-kth-bit-in-nth-binary-string ├── README.md └── solution.php ├── 001550-three-consecutive-odds ├── README.md └── solution.php ├── 001552-magnetic-force-between-two-balls ├── README.md └── solution.php ├── 001568-minimum-number-of-days-to-disconnect-island ├── README.md └── solution.php ├── 001574-shortest-subarray-to-be-removed-to-make-array-sorted ├── README.md └── solution.php ├── 001579-remove-max-number-of-edges-to-keep-graph-fully-traversable ├── README.md └── solution.php ├── 001590-make-sum-divisible-by-p ├── README.md └── solution.php ├── 001593-split-a-string-into-the-max-number-of-unique-substrings ├── README.md └── solution.php ├── 001595-minimum-cost-to-connect-two-groups-of-points ├── README.md └── solution.php ├── 001598-crawler-log-folder ├── README.md └── solution.php ├── 001605-find-valid-matrix-given-row-and-column-sums ├── README.md └── solution.php ├── 001608-special-array-with-x-elements-greater-than-or-equal-x ├── README.md └── solution.php ├── 001614-maximum-nesting-depth-of-the-parentheses ├── README.md └── solution.php ├── 001636-sort-array-by-increasing-frequency ├── README.md └── solution.php ├── 001639-number-of-ways-to-form-a-target-string-given-a-dictionary ├── README.md └── solution.php ├── 001652-defuse-the-bomb ├── README.md └── solution.php ├── 001653-minimum-deletions-to-make-string-balanced ├── README.md └── solution.php ├── 001671-minimum-number-of-removals-to-make-mountain-array ├── README.md └── solution.php ├── 001684-count-the-number-of-consistent-strings ├── README.md └── solution.php ├── 001700-number-of-students-unable-to-eat-lunch ├── README.md └── solution.php ├── 001701-average-waiting-time ├── README.md └── solution.php ├── 001717-maximum-score-from-removing-substrings ├── README.md └── solution.php ├── 001718-construct-the-lexicographically-largest-valid-sequence ├── README.md └── solution.php ├── 001726-tuple-with-same-product ├── README.md └── solution.php ├── 001749-maximum-absolute-sum-of-any-subarray ├── README.md └── solution.php ├── 001752-check-if-array-is-sorted-and-rotated ├── README.md └── solution.php ├── 001760-minimum-limit-of-balls-in-a-bag ├── README.md └── solution.php ├── 001765-map-of-highest-peak ├── README.md ├── example-03.md └── solution.php ├── 001769-minimum-number-of-operations-to-move-all-balls-to-each-box ├── README.md └── solution.php ├── 001780-check-if-number-is-a-sum-of-powers-of-three ├── README.md └── solution.php ├── 001790-check-if-one-string-swap-can-make-strings-equal ├── README.md └── solution.php ├── 001791-find-center-of-star-graph ├── README.md └── solution.php ├── 001792-maximum-average-pass-ratio ├── README.md └── solution.php ├── 001800-maximum-ascending-subarray-sum ├── README.md └── solution.php ├── 001813-sentence-similarity-iii ├── README.md └── solution.php ├── 001823-find-the-winner-of-the-circular-game ├── README.md └── solution.php ├── 001829-maximum-xor-for-each-query ├── README.md └── solution.php ├── 001857-largest-color-value-in-a-directed-graph ├── README.md └── solution.php ├── 001861-rotating-the-box ├── README.md └── solution.php ├── 001863-sum-of-all-subset-xor-totals ├── README.md └── solution.php ├── 001894-find-the-student-that-will-replace-the-chalk ├── README.md └── solution.php ├── 001905-count-sub-islands ├── README.md └── solution.php ├── 001910-remove-all-occurrences-of-a-substring ├── README.md └── solution.php ├── 001915-number-of-wonderful-substrings ├── README.md └── solution.php ├── 001920-build-array-from-permutation ├── README.md └── solution.php ├── 001922-count-good-numbers ├── README.md └── solution.php ├── 001930-unique-length-3-palindromic-subsequences ├── README.md └── solution.php ├── 001931-painting-a-grid-with-three-different-colors ├── README.md └── solution.php ├── 001937-maximum-number-of-points-with-cost ├── README.md └── solution.php ├── 001942-the-number-of-the-smallest-unoccupied-chair ├── README.md └── solution.php ├── 001945-sum-of-digits-of-string-after-convert ├── README.md └── solution.php ├── 001957-delete-characters-to-make-fancy-string ├── README.md └── solution.php ├── 001963-minimum-number-of-swaps-to-make-the-string-balanced ├── README.md └── solution.php ├── 001971-find-if-path-exists-in-graph ├── README.md └── solution.php ├── 001975-maximum-matrix-sum ├── README.md └── solution.php ├── 001976-number-of-ways-to-arrive-at-destination ├── README.md └── solution.php ├── 001980-find-unique-binary-string ├── README.md └── solution.php ├── 002000-reverse-prefix-of-word ├── README.md └── solution.php ├── 002017-grid-game ├── README.md └── solution.php ├── 002022-convert-1d-array-into-2d-array ├── README.md └── solution.php ├── 002028-find-missing-observations ├── README.md └── solution.php ├── 002033-minimum-operations-to-make-a-uni-value-grid ├── README.md └── solution.php ├── 002037-minimum-number-of-moves-to-seat-everyone ├── README.md └── solution.php ├── 002044-count-number-of-maximum-bitwise-or-subsets ├── README.md └── solution.php ├── 002045-second-minimum-time-to-reach-destination ├── README.md └── solution.php ├── 002053-kth-distinct-string-in-an-array ├── README.md └── solution.php ├── 002054-two-best-non-overlapping-events ├── README.md └── solution.php ├── 002058-find-the-minimum-and-maximum-number-of-nodes-between-critical-points ├── README.md └── solution.php ├── 002064-minimized-maximum-of-products-distributed-to-any-store ├── README.md └── solution.php ├── 002070-most-beautiful-item-for-each-query ├── README.md └── solution.php ├── 002071-maximum-number-of-tasks-you-can-assign ├── README.md └── solution.php ├── 002073-time-needed-to-buy-tickets ├── README.md └── solution.php ├── 002094-finding-3-digit-even-numbers ├── README.md └── solution.php ├── 002096-step-by-step-directions-from-a-binary-tree-node-to-another ├── README.md └── solution.php ├── 002097-valid-arrangement-of-pairs ├── README.md └── solution.php ├── 002109-adding-spaces-to-a-string ├── README.md └── solution.php ├── 002115-find-all-possible-recipes-from-given-supplies ├── README.md └── solution.php ├── 002116-check-if-a-parentheses-string-can-be-valid ├── README.md └── solution.php ├── 002127-maximum-employees-to-be-invited-to-a-meeting ├── README.md └── solution.php ├── 002131-longest-palindrome-by-concatenating-two-letter-words ├── README.md └── solution.php ├── 002134-minimum-swaps-to-group-all-1s-together-ii ├── README.md └── solution.php ├── 002140-solving-questions-with-brainpower ├── README.md └── solution.php ├── 002145-count-the-hidden-sequences ├── README.md └── solution.php ├── 002161-partition-array-according-to-given-pivot ├── README.md └── solution.php ├── 002176-count-equal-and-divisible-pairs-in-an-array ├── README.md └── solution.php ├── 002179-count-good-triplets-in-an-array ├── README.md └── solution.php ├── 002181-merge-nodes-in-between-zeros ├── README.md └── solution.php ├── 002182-construct-string-with-repeat-limit ├── README.md └── solution.php ├── 002185-counting-words-with-a-given-prefix ├── README.md └── solution.php ├── 002191-sort-the-jumbled-numbers ├── README.md └── solution.php ├── 002192-all-ancestors-of-a-node-in-a-directed-acyclic-graph ├── README.md └── solution.php ├── 002196-create-binary-tree-from-descriptions ├── README.md └── solution.php ├── 002206-divide-array-into-equal-pairs ├── README.md └── solution.php ├── 002220-minimum-bit-flips-to-convert-number ├── README.md └── solution.php ├── 002226-maximum-candies-allocated-to-k-children ├── README.md └── solution.php ├── 002257-count-unguarded-cells-in-the-grid ├── README.md └── solution.php ├── 002270-number-of-ways-to-split-array ├── README.md └── solution.php ├── 002275-largest-combination-with-bitwise-and-greater-than-zero ├── README.md └── solution.php ├── 002285-maximum-total-importance-of-roads ├── README.md └── solution.php ├── 002290-minimum-obstacle-removal-to-reach-corner ├── README.md └── solution.php ├── 002302-count-subarrays-with-score-less-than-k ├── README.md └── solution.php ├── 002326-spiral-matrix-iv ├── README.md └── solution.php ├── 002331-evaluate-boolean-binary-tree ├── README.md └── solution.php ├── 002337-move-pieces-to-obtain-a-string ├── README.md └── solution.php ├── 002338-count-the-number-of-ideal-arrays ├── README.md └── solution.php ├── 002342-max-sum-of-a-pair-with-equal-sum-of-digits ├── README.md └── solution.php ├── 002349-design-a-number-container-system ├── README.md └── solution.php ├── 002359-find-closest-node-to-given-two-nodes ├── README.md └── solution.php ├── 002364-count-number-of-bad-pairs ├── README.md └── solution.php ├── 002370-longest-ideal-subsequence ├── README.md └── solution.php ├── 002373-largest-local-values-in-a-matrix ├── README.md └── solution.php ├── 002375-construct-smallest-number-from-di-string ├── README.md └── solution.php ├── 002379-minimum-recolors-to-get-k-consecutive-black-blocks ├── README.md └── solution.php ├── 002381-shifting-letters-ii ├── README.md └── solution.php ├── 002392-build-a-matrix-with-conditions ├── README.md └── solution.php ├── 002401-longest-nice-subarray ├── README.md └── solution.php ├── 002406-divide-intervals-into-minimum-number-of-groups ├── README.md └── solution.php ├── 002415-reverse-odd-levels-of-binary-tree ├── README.md └── solution.php ├── 002416-sum-of-prefix-scores-of-strings ├── README.md └── solution.php ├── 002418-sort-the-people ├── README.md └── solution.php ├── 002419-longest-subarray-with-maximum-bitwise-and ├── README.md └── solution.php ├── 002425-bitwise-xor-of-all-pairings ├── README.md └── solution.php ├── 002429-minimize-xor ├── README.md └── solution.php ├── 002434-using-a-robot-to-print-the-lexicographically-smallest-string ├── README.md └── solution.php ├── 002441-largest-positive-integer-that-exists-with-its-negative ├── README.md └── solution.php ├── 002444-count-subarrays-with-fixed-bounds ├── README.md └── solution.php ├── 002458-height-of-binary-tree-after-subtree-removal-queries ├── README.md └── solution.php ├── 002460-apply-operations-to-an-array ├── README.md └── solution.php ├── 002461-maximum-sum-of-distinct-subarrays-with-length-k ├── README.md └── solution.php ├── 002463-minimum-total-distance-traveled ├── README.md └── solution.php ├── 002466-count-ways-to-build-good-strings ├── README.md └── solution.php ├── 002467-most-profitable-path-in-a-tree ├── README.md └── solution.php ├── 002471-minimum-number-of-operations-to-sort-a-binary-tree-by-level ├── README.md └── solution.php ├── 002486-append-characters-to-string-to-make-subsequence ├── README.md └── solution.php ├── 002487-remove-nodes-from-linked-list ├── README.md └── solution.php ├── 002490-circular-sentence ├── README.md └── solution.php ├── 002491-divide-players-into-teams-of-equal-skill ├── README.md └── solution.php ├── 002493-divide-nodes-into-the-maximum-number-of-groups ├── README.md └── solution.php ├── 002501-longest-square-streak-in-an-array ├── README.md └── solution.php ├── 002503-maximum-number-of-points-from-grid-queries ├── README.md └── solution.php ├── 002516-take-k-of-each-character-from-left-and-right ├── README.md └── solution.php ├── 002523-closest-prime-numbers-in-range ├── README.md └── solution.php ├── 002529-maximum-count-of-positive-integer-and-negative-integer ├── README.md └── solution.php ├── 002530-maximal-score-after-applying-k-operations ├── README.md ├── solution.php └── solution.py ├── 002537-count-the-number-of-good-subarrays ├── README.md └── solution.php ├── 002551-put-marbles-in-bags ├── README.md └── solution.php ├── 002554-maximum-number-of-integers-to-choose-from-a-range-i ├── README.md └── solution.php ├── 002558-take-gifts-from-the-richest-pile ├── README.md └── solution.php ├── 002559-count-vowel-strings-in-ranges ├── README.md └── solution.php ├── 002560-house-robber-iv ├── README.md └── solution.php ├── 002563-count-the-number-of-fair-pairs ├── README.md └── solution.php ├── 002570-merge-two-2d-arrays-by-summing-values-solution ├── README.md ├── solution.php └── solution.py ├── 002577-minimum-time-to-visit-a-cell-in-a-grid ├── README.md └── solution.php ├── 002579-count-total-number-of-colored-cells ├── README.md └── solution.php ├── 002582-pass-the-pillow ├── README.md └── solution.php ├── 002583-kth-largest-sum-in-a-binary-tree ├── README.md └── solution.php ├── 002593-find-score-of-an-array-after-marking-all-elements ├── README.md └── solution.php ├── 002594-minimum-time-to-repair-cars ├── README.md └── solution.php ├── 002597-the-number-of-beautiful-subsets ├── README.md └── solution.php ├── 002601-prime-subtraction-operation ├── README.md └── solution.php ├── 002641-cousins-in-binary-tree-ii ├── README.md └── solution.php ├── 002657-find-the-prefix-common-array-of-two-arrays ├── README.md └── solution.php ├── 002658-maximum-number-of-fish-in-a-grid ├── README.md └── solution.php ├── 002661-first-completely-painted-row-or-column ├── README.md └── solution.php ├── 002678-number-of-senior-citizens ├── README.md └── solution.php ├── 002683-neighboring-bitwise-xor ├── README.md └── solution.php ├── 002684-maximum-number-of-moves-in-a-grid ├── README.md └── solution.php ├── 002685-count-the-number-of-complete-components ├── README.md └── solution.php ├── 002696-minimum-string-length-after-removing-substrings ├── README.md └── solution.php ├── 002698-find-the-punishment-number-of-an-integer ├── README.md └── solution.php ├── 002699-modify-graph-edge-weights ├── README.md └── solution.php ├── 002707-extra-characters-in-a-string ├── README.md └── solution.php ├── 002751-robot-collisions ├── README.md └── solution.php ├── 002762-continuous-subarrays ├── README.md └── solution.php ├── 002779-maximum-beauty-of-an-array-after-applying-operation ├── README.md └── solution.php ├── 002780-minimum-index-of-a-valid-split ├── README.md └── solution.php ├── 002799-count-complete-subarrays-in-an-array ├── README.md └── solution.php ├── 002807-insert-greatest-common-divisors-in-linked-list ├── README.md └── solution.php ├── 002812-find-the-safest-path-in-a-grid ├── README.md └── solution.php ├── 002816-double-a-number-represented-as-a-linked-list ├── README.md └── solution.php ├── 002818-apply-operations-to-maximize-score ├── README.md └── solution.php ├── 002825-make-string-a-subsequence-using-cyclic-increments ├── README.md └── solution.php ├── 002843-count-symmetric-integers ├── README.md └── solution.php ├── 002845-count-of-interesting-subarrays ├── README.md └── solution.php ├── 002872-maximum-number-of-k-divisible-components ├── README.md └── solution.php ├── 002873-maximum-value-of-an-ordered-triplet-i ├── README.md └── solution.php ├── 002874-maximum-value-of-an-ordered-triplet-ii ├── README.md └── solution.php ├── 002894-divisible-and-non-divisible-sums-difference ├── README.md └── solution.php ├── 002900-longest-unequal-adjacent-groups-subsequence-i ├── README.md └── solution.php ├── 002901-longest-unequal-adjacent-groups-subsequence-ii ├── README.md └── solution.php ├── 002914-minimum-number-of-changes-to-make-binary-string-beautiful ├── README.md └── solution.php ├── 002918-minimum-equal-sum-of-two-arrays-after-replacing-zeros ├── README.md └── solution.php ├── 002924-find-champion-ii ├── README.md └── solution.php ├── 002929-distribute-candies-among-children-ii ├── README.md └── solution.php ├── 002938-separate-black-and-white-balls ├── README.md └── solution.php ├── 002940-find-building-where-alice-and-bob-can-meet ├── README.md └── solution.php ├── 002942-find-words-containing-character ├── README.md └── solution.php ├── 002948-make-lexicographically-smallest-array-by-swapping-elements ├── README.md └── solution.php ├── 002962-count-subarrays-where-max-element-appears-at-least-k-times ├── README.md └── solution.php ├── 002965-find-missing-and-repeated-values ├── README.md └── solution.php ├── 002976-minimum-cost-to-convert-string-i ├── README.md └── solution.php ├── 002981-find-longest-special-substring-that-occurs-thrice-i ├── README.md └── solution.php ├── 002997-minimum-number-of-operations-to-make-array-xor-equal-to-k ├── README.md └── solution.php ├── 002999-count-the-number-of-powerful-integers ├── README.md └── solution.php ├── 003011-find-if-array-can-be-sorted ├── README.md └── solution.php ├── 003016-minimum-number-of-pushes-to-type-word-ii ├── README.md └── solution.php ├── 003024-type-of-triangle ├── README.md └── solution.php ├── 003042-count-prefix-and-suffix-pairs-i ├── README.md └── solution.php ├── 003043-find-the-length-of-the-longest-common-prefix ├── README.md └── solution.php ├── 003066-minimum-operations-to-exceed-threshold-value-ii ├── README.md └── solution.php ├── 003068-find-the-maximum-sum-of-node-values ├── README.md └── solution.php ├── 003075-maximize-happiness-of-selected-children ├── README.md └── solution.php ├── 003097-shortest-subarray-with-or-at-least-k-ii ├── README.md └── solution.php ├── 003105-longest-strictly-increasing-or-strictly-decreasing-subarray ├── README.md └── solution.php ├── 003108-minimum-cost-walk-in-weighted-graph ├── README.md └── solution.php ├── 003110-score-of-a-string ├── README.md └── solution.php ├── 003113-find-the-number-of-subarrays-where-boundary-elements-are-maximum ├── README.md └── solution.php ├── 003121-count-the-number-of-special-characters-ii ├── README.md └── solution.php ├── 003122-minimum-number-of-operations-to-satisfy-conditions ├── README.md └── solution.php ├── 003123-ind-edges-in-shortest-paths ├── README.md └── solution.php ├── 003133-minimum-array-end ├── README.md └── solution.php ├── 003151-special-array-i ├── README.md └── solution.php ├── 003152-special-array-ii ├── README.md └── solution.php ├── 003160-find-the-number-of-distinct-colors-among-the-balls ├── README.md └── solution.php ├── 003163-string-compression-iii ├── README.md └── solution.php ├── 003169-count-days-without-meetings ├── README.md └── solution.php ├── 003170-lexicographically-minimum-string-after-removing-stars ├── README.md └── solution.php ├── 003174-clear-digits ├── README.md └── solution.php ├── 003191-minimum-operations-to-make-binary-array-elements-equal-to-one-i ├── README.md └── solution.php ├── 003203-find-minimum-diameter-after-merging-two-trees ├── README.md └── solution.php ├── 003208-alternating-groups-ii ├── README.md └── solution.php ├── 003217-delete-nodes-from-linked-list-present-in-array ├── README.md └── solution.php ├── 003223-minimum-length-of-string-after-operations ├── README.md └── solution.php ├── 003243-shortest-distance-after-road-addition-queries-i ├── README.md └── solution.php ├── 003254-find-the-power-of-k-size-subarrays-i ├── README.md └── solution.php ├── 003264-final-array-state-after-k-multiplication-operations-i ├── README.md └── solution.php ├── 003272-find-the-count-of-good-integers ├── README.md └── solution.php ├── 003306-count-of-substrings-containing-every-vowel-and-k-consonants-ii ├── README.md └── solution.php ├── 003335-total-characters-in-string-after-transformations-i ├── README.md └── solution.php ├── 003337-total-characters-in-string-after-transformations-ii ├── README.md └── solution.php ├── 003341-find-minimum-time-to-reach-last-room-i ├── README.md └── solution.php ├── 003342-find-minimum-time-to-reach-last-room-ii ├── README.md └── solution.php ├── 003343-count-number-of-balanced-permutations ├── README.md └── solution.php ├── 003355-zero-array-transformation-i ├── README.md └── solution.php ├── 003356-zero-array-transformation-ii ├── README.md └── solution.php ├── 003362-zero-array-transformation-iii ├── README.md ├── example5 └── solution.php ├── 003372-maximize-the-number-of-target-nodes-after-connecting-trees-i ├── README.md └── solution.php ├── 003373-maximize-the-number-of-target-nodes-after-connecting-trees-ii ├── README.md └── solution.php ├── 003375-minimum-operations-to-make-array-values-equal-to-k ├── README.md └── solution.php ├── 003392-count-subarrays-of-length-three-with-a-condition ├── README.md └── solution.php ├── 003394-check-if-grid-can-be-cut-into-sections ├── README.md └── solution.php ├── 003396-minimum-number-of-operations-to-make-elements-in-array-distinct ├── README.md └── solution.php └── 003403-find-the-lexicographically-largest-string-from-the-box-i ├── README.md └── solution.php /.github/ISSUE_TEMPLATE/bug_report.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Bug report 3 | about: Create a report to help us improve 4 | title: '' 5 | labels: '' 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Describe the bug** 11 | A clear and concise description of what the bug is. 12 | 13 | **To Reproduce** 14 | Steps to reproduce the behavior: 15 | 1. Go to '...' 16 | 2. Click on '....' 17 | 3. Scroll down to '....' 18 | 4. See error 19 | 20 | **Expected behavior** 21 | A clear and concise description of what you expected to happen. 22 | 23 | **Screenshots** 24 | If applicable, add screenshots to help explain your problem. 25 | 26 | **Desktop (please complete the following information):** 27 | - OS: [e.g. iOS] 28 | - Browser [e.g. chrome, safari] 29 | - Version [e.g. 22] 30 | 31 | **Smartphone (please complete the following information):** 32 | - Device: [e.g. iPhone6] 33 | - OS: [e.g. iOS8.1] 34 | - Browser [e.g. stock browser, safari] 35 | - Version [e.g. 22] 36 | 37 | **Additional context** 38 | Add any other context about the problem here. 39 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/config.yml: -------------------------------------------------------------------------------- 1 | blank_issues_enabled: false 2 | contact_links: 3 | - name: Ask a question 4 | url: https://github.com/mah-shamim/leet-code-in-php/discussions/new?category=q-a 5 | about: Ask the community for help 6 | - name: Request a feature 7 | url: https://github.com/mah-shamim/leet-code-in-php/discussions/new?category=ideas 8 | about: Share ideas for new features 9 | - name: Report a security issue 10 | url: https://github.com/mah-shamim/leet-code-in-php/security/policy 11 | about: Learn how to notify us for sensitive bugs 12 | - name: Report a bug 13 | url: https://github.com/mah-shamim/leet-code-in-php/issues/new 14 | about: Report a reproducable bug 15 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/custom.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Custom issue template 3 | about: Describe this issue template's purpose here. 4 | title: '' 5 | labels: '' 6 | assignees: '' 7 | 8 | --- 9 | 10 | 11 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/feature_request.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Feature request 3 | about: Suggest an idea for this project 4 | title: '' 5 | labels: question 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Is your feature request related to a problem? Please describe.** 11 | A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] 12 | 13 | **Describe the solution you'd like** 14 | A clear and concise description of what you want to happen. 15 | 16 | **Describe alternatives you've considered** 17 | A clear and concise description of any alternative solutions or features you've considered. 18 | 19 | **Additional context** 20 | Add any other context or screenshots about the feature request here. 21 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .idea/ 2 | .vscode/ 3 | automate_git.bat 4 | auto_commit.sh 5 | auto_commit.bat 6 | *.iml -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # leet-code-in-php 2 | Php-based LeetCode algorithm problem solutions, regularly updated. 3 | 4 | 5 | ![Repobeats analytics image](https://repobeats.axiom.co/api/embed/9f698d3cdb218feb946714e458ac140a276b39a5.svg "Repobeats analytics image") 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /SECURITY.md: -------------------------------------------------------------------------------- 1 | # Security Policy 2 | 3 | ## Supported Versions 4 | 5 | Use this section to tell people about which versions of your project are 6 | currently being supported with security updates. 7 | 8 | | Version | Supported | 9 | | ------- | ------------------ | 10 | | 5.1.x | :white_check_mark: | 11 | | 5.0.x | :x: | 12 | | 4.0.x | :white_check_mark: | 13 | | < 4.0 | :x: | 14 | 15 | ## Reporting a Vulnerability 16 | 17 | Use this section to tell people how to report a vulnerability. 18 | 19 | Tell them where to go, how often they can expect to get an update on a 20 | reported vulnerability, what to expect if the vulnerability is accepted or 21 | declined, etc. 22 | -------------------------------------------------------------------------------- /algorithms/000001-two-sum/solution.php: -------------------------------------------------------------------------------- 1 | $num) { 16 | // Calculate the complement of the current number 17 | $complement = $target - $num; 18 | 19 | // Check if the complement exists in the map 20 | if (isset($map[$complement])) { 21 | // If found, return the indices of the complement and the current number 22 | return [$map[$complement], $index]; 23 | } 24 | 25 | // Otherwise, add the current number and its index to the map 26 | $map[$num] = $index; 27 | } 28 | 29 | // If no solution is found (although the problem guarantees one), return an empty array 30 | return []; 31 | } 32 | } -------------------------------------------------------------------------------- /algorithms/000003-longest-substring-without-repeating-characters/solution.php: -------------------------------------------------------------------------------- 1 | = $start) { 19 | // Move the start right after the last occurrence of current character 20 | $start = $charIndexMap[$char] + 1; 21 | } 22 | 23 | // Update the latest index of the character 24 | $charIndexMap[$char] = $end; 25 | 26 | // Update the maximum length of substring found 27 | $maxLength = max($maxLength, $end - $start + 1); 28 | } 29 | 30 | return $maxLength; 31 | 32 | } 33 | } -------------------------------------------------------------------------------- /algorithms/000017-letter-combinations-of-a-phone-number/solution.php: -------------------------------------------------------------------------------- 1 | findCombinations(0, $digits, $letters, $curr, $ans); 18 | return $ans; 19 | } 20 | 21 | private function findCombinations($start, $nums, $letters, &$curr, &$ans): void 22 | { 23 | if (strlen($curr) == strlen($nums)) { 24 | $ans[] = $curr; 25 | return; 26 | } 27 | for ($i = $start; $i < strlen($nums); $i++) { 28 | $n = intval($nums[$i]); 29 | for ($j = 0; $j < strlen($letters[$n]); $j++) { 30 | $ch = $letters[$n][$j]; 31 | $curr .= $ch; 32 | $this->findCombinations($i + 1, $nums, $letters, $curr, $ans); 33 | $curr = substr($curr, 0, -1); 34 | } 35 | } 36 | } 37 | 38 | } -------------------------------------------------------------------------------- /algorithms/000038-count-and-say/solution.php: -------------------------------------------------------------------------------- 1 | = $left_max) { 22 | $left_max = $height[$left]; 23 | } else { 24 | $water += $left_max - $height[$left]; 25 | } 26 | $left++; 27 | } else { 28 | if ($height[$right] >= $right_max) { 29 | $right_max = $height[$right]; 30 | } else { 31 | $water += $right_max - $height[$right]; 32 | } 33 | $right--; 34 | } 35 | } 36 | 37 | return $water; 38 | 39 | } 40 | } -------------------------------------------------------------------------------- /algorithms/000058-length-of-last-word/solution.php: -------------------------------------------------------------------------------- 1 | val = $val; 15 | $this->left = $left; 16 | $this->right = $right; 17 | } 18 | } 19 | 20 | class Solution 21 | { 22 | 23 | /** 24 | * @param TreeNode $root 25 | * @return float|int 26 | */ 27 | function sumNumbers(TreeNode $root): float|int 28 | { 29 | return $this->sumNumbersRecu($root, 0); 30 | } 31 | 32 | function sumNumbersRecu($root, $num) 33 | { 34 | if ($root === null) { 35 | return 0; 36 | } 37 | 38 | if ($root->left === null && $root->right === null) { 39 | return $num * 10 + $root->val; 40 | } 41 | 42 | return $this->sumNumbersRecu($root->left, $num * 10 + $root->val) + $this->sumNumbersRecu($root->right, $num * 10 + $root->val); 43 | } 44 | } 45 | 46 | ?> -------------------------------------------------------------------------------- /algorithms/000135-candy/solution.php: -------------------------------------------------------------------------------- 1 | $ratings[$i - 1]) { 15 | $candies[$i] = $candies[$i - 1] + 1; 16 | } 17 | } 18 | 19 | for ($i = $n - 2; $i >= 0; $i--) { 20 | if ($ratings[$i] > $ratings[$i + 1]) { 21 | $candies[$i] = max($candies[$i], $candies[$i + 1] + 1); 22 | } 23 | } 24 | 25 | return array_sum($candies); 26 | } 27 | } -------------------------------------------------------------------------------- /algorithms/000140-word-break-ii/solution.php: -------------------------------------------------------------------------------- 1 | map)) { 14 | return $this->map[$s]; 15 | } 16 | 17 | $result = array(); 18 | 19 | if(strlen($s) == 0){ 20 | $result[] = ""; 21 | $this->map[""] = $result; 22 | return $result; 23 | } 24 | 25 | foreach($wordDict as $word) { 26 | if(strpos($s, $word) === 0){ 27 | $subWords = $this->wordBreak(substr($s, strlen($word)), $wordDict); 28 | foreach($subWords as $subWord) { 29 | $result[] = $word . (strlen($subWord) > 0 ? " " : "") . $subWord; 30 | } 31 | } 32 | } 33 | 34 | $this->map[$s] = $result; 35 | return $result; 36 | } 37 | } -------------------------------------------------------------------------------- /algorithms/000165-compare-version-numbers/solution.php: -------------------------------------------------------------------------------- 1 | $seg2) { 28 | return 1; 29 | } 30 | } 31 | 32 | // All segments are equal 33 | return 0; 34 | 35 | } 36 | } -------------------------------------------------------------------------------- /algorithms/000179-largest-number/solution.php: -------------------------------------------------------------------------------- 1 | val = $val; 15 | } 16 | } 17 | 18 | class Solution 19 | { 20 | /** 21 | * @param ListNode $node 22 | * @return void 23 | */ 24 | function deleteNode(ListNode $node): void 25 | { 26 | // Step 1: Copy the value from the next node into the current node 27 | $node->val = $node->next->val; 28 | 29 | // Step 2: Point the current node's next to the next node's next 30 | $node->next = $node->next->next; 31 | } 32 | } -------------------------------------------------------------------------------- /algorithms/000260-single-number-iii/solution.php: -------------------------------------------------------------------------------- 1 | $count) { 19 | // Check if the element exists in the second array 20 | if (isset($counts2[$num])) { 21 | // Find the minimum occurrence 22 | $minCount = min($count, $counts2[$num]); 23 | // Add the element to the result array, repeated $minCount times 24 | for ($i = 0; $i < $minCount; $i++) { 25 | $intersection[] = $num; 26 | } 27 | } 28 | } 29 | 30 | return $intersection; 31 | } 32 | } -------------------------------------------------------------------------------- /algorithms/000368-largest-divisible-subset/solution.php: -------------------------------------------------------------------------------- 1 | $dp[$i]) { 24 | $dp[$i] = $dp[$j] + 1; 25 | $prev[$i] = $j; 26 | } 27 | } 28 | } 29 | if ($dp[$i] > $max_len) { 30 | $max_len = $dp[$i]; 31 | $max_idx = $i; 32 | } 33 | } 34 | 35 | $result = []; 36 | $idx = $max_idx; 37 | while ($idx != -1) { 38 | array_unshift($result, $nums[$idx]); 39 | $idx = $prev[$idx]; 40 | } 41 | 42 | return $result; 43 | } 44 | } -------------------------------------------------------------------------------- /algorithms/000386-lexicographical-numbers/solution.php: -------------------------------------------------------------------------------- 1 | = $n) { 23 | $current /= 10; 24 | } 25 | $current++; 26 | while ($current % 10 == 0) { 27 | $current /= 10; 28 | } 29 | } 30 | } 31 | 32 | return $result; 33 | } 34 | } -------------------------------------------------------------------------------- /algorithms/000402-remove-k-digits/solution.php: -------------------------------------------------------------------------------- 1 | 0 && !empty($stack) && end($stack) > $digit) { 19 | array_pop($stack); 20 | $k--; 21 | } 22 | 23 | // Push the current digit onto the stack 24 | $stack[] = $digit; 25 | } 26 | 27 | // If we still have digits to remove, remove them from the end 28 | while ($k > 0) { 29 | array_pop($stack); 30 | $k--; 31 | } 32 | 33 | // Build the final result, removing leading zeros 34 | $result = ltrim(implode('', $stack), '0'); 35 | 36 | // Return "0" if the result is empty 37 | return $result === '' ? '0' : $result; 38 | 39 | } 40 | } -------------------------------------------------------------------------------- /algorithms/000404-sum-of-left-leaves/solution.php: -------------------------------------------------------------------------------- 1 | val = $val; 15 | $this->left = $left; 16 | $this->right = $right; 17 | } 18 | } 19 | 20 | class Solution 21 | { 22 | 23 | /** 24 | * @param TreeNode $root 25 | * @return Integer 26 | */ 27 | function sumOfLeftLeaves(TreeNode $root): int 28 | { 29 | if (!$root) { 30 | return 0; 31 | } 32 | 33 | $ans = 0; 34 | 35 | if ($root->left) { 36 | if (!$root->left->left && !$root->left->right) { 37 | $ans += $root->left->val; 38 | } else { 39 | $ans += $this->sumOfLeftLeaves($root->left); 40 | } 41 | } 42 | $ans += $this->sumOfLeftLeaves($root->right); 43 | 44 | return $ans; 45 | } 46 | } -------------------------------------------------------------------------------- /algorithms/000416-partition-equal-subset-sum/solution.php: -------------------------------------------------------------------------------- 1 | = $num; $j--) { 21 | $dp[$j] = $dp[$j] || $dp[$j - $num]; 22 | } 23 | } 24 | 25 | return $dp[$target]; 26 | } 27 | } -------------------------------------------------------------------------------- /algorithms/000463-island-perimeter/solution.php: -------------------------------------------------------------------------------- 1 | = $num; $j--) { 27 | $dp[$j] += $dp[$j - $num]; 28 | } 29 | } 30 | 31 | // Step 5: Return the number of ways to achieve the target sum 32 | return $dp[$S]; 33 | } 34 | } -------------------------------------------------------------------------------- /algorithms/000539-minimum-time-difference/solution.php: -------------------------------------------------------------------------------- 1 | val = $val; 10 | * $this->children = array(); 11 | * } 12 | * } 13 | */ 14 | 15 | class Solution { 16 | /** 17 | * @param Node $root 18 | * @return integer[] 19 | */ 20 | function postorder($root) { 21 | if ($root === null) { 22 | return []; 23 | } 24 | 25 | $result = []; 26 | $stack = [$root]; 27 | 28 | while (!empty($stack)) { 29 | $node = array_pop($stack); 30 | array_unshift($result, $node->val); 31 | foreach ($node->children as $child) { 32 | $stack[] = $child; 33 | } 34 | } 35 | 36 | return $result; 37 | } 38 | } -------------------------------------------------------------------------------- /algorithms/000624-maximum-distance-in-arrays/solution.php: -------------------------------------------------------------------------------- 1 | = 0; $i--) { 14 | $dp[$i][$i] = 1; // Single character needs only one turn 15 | for ($j = $i + 1; $j < $n; $j++) { 16 | $dp[$i][$j] = $dp[$i][$j - 1] + 1; 17 | for ($k = $i; $k < $j; $k++) { 18 | if ($s[$k] == $s[$j]) { 19 | $dp[$i][$j] = min($dp[$i][$j], $dp[$i][$k] + ($k + 1 <= $j - 1 ? $dp[$k + 1][$j - 1] : 0)); 20 | } 21 | } 22 | } 23 | } 24 | 25 | return $dp[0][$n - 1]; 26 | 27 | } 28 | } -------------------------------------------------------------------------------- /algorithms/000703-kth-largest-element-in-a-stream/solution.php: -------------------------------------------------------------------------------- 1 | k = $k; 13 | $this->minHeap = new SplMinHeap(); 14 | 15 | foreach ($nums as $num) { 16 | $this->add($num); 17 | } 18 | } 19 | 20 | /** 21 | * @param Integer $val 22 | * @return Integer 23 | */ 24 | function add($val) { 25 | if ($this->minHeap->count() < $this->k) { 26 | $this->minHeap->insert($val); 27 | } elseif ($val > $this->minHeap->top()) { 28 | $this->minHeap->extract(); 29 | $this->minHeap->insert($val); 30 | } 31 | return $this->minHeap->top(); 32 | } 33 | } 34 | 35 | /** 36 | * Your KthLargest object will be instantiated and called as such: 37 | * $obj = KthLargest($k, $nums); 38 | * $ret_1 = $obj->add($val); 39 | */ -------------------------------------------------------------------------------- /algorithms/000719-find-k-th-smallest-pair-distance/solution.php: -------------------------------------------------------------------------------- 1 | countPairsWithDistanceLessThanOrEqualTo($nums, $mid) < $k) { 19 | $low = $mid + 1; 20 | } else { 21 | $high = $mid; 22 | } 23 | } 24 | 25 | return $low; 26 | 27 | } 28 | 29 | /** 30 | * @param $nums 31 | * @param $mid 32 | * @return int 33 | */ 34 | function countPairsWithDistanceLessThanOrEqualTo($nums, $mid) { 35 | $count = 0; 36 | $left = 0; 37 | 38 | for ($right = 1; $right < count($nums); $right++) { 39 | while ($nums[$right] - $nums[$left] > $mid) { 40 | $left++; 41 | } 42 | $count += $right - $left; 43 | } 44 | 45 | return $count; 46 | } 47 | 48 | } -------------------------------------------------------------------------------- /algorithms/000763-partition-labels/solution.php: -------------------------------------------------------------------------------- 1 | $cnt) { 20 | if ($x == 0) { 21 | $total += $cnt; 22 | } else { 23 | $denominator = $x + 1; 24 | $groups = (int)(($cnt + $x) / $denominator); 25 | $total += $groups * $denominator; 26 | } 27 | } 28 | return $total; 29 | } 30 | } -------------------------------------------------------------------------------- /algorithms/000790-domino-and-tromino-tiling/solution.php: -------------------------------------------------------------------------------- 1 | 0 && $five > 0) { 24 | $ten--; 25 | $five--; 26 | } elseif ($five >= 3) { 27 | $five -= 3; 28 | } else { 29 | return false; 30 | } 31 | } 32 | } 33 | return true; 34 | } 35 | } -------------------------------------------------------------------------------- /algorithms/000873-length-of-longest-fibonacci-subsequence/solution.php: -------------------------------------------------------------------------------- 1 | $val) { 16 | $valueMap[$val] = $i; 17 | } 18 | 19 | $max = 0; 20 | $dp = array_fill(0, $n, array_fill(0, $n, 2)); 21 | 22 | // Dynamic programming approach 23 | for ($k = 0; $k < $n; $k++) { 24 | for ($j = 0; $j < $k; $j++) { 25 | $diff = $arr[$k] - $arr[$j]; // Previous term in Fibonacci sequence 26 | if (isset($valueMap[$diff])) { 27 | $i = $valueMap[$diff]; 28 | if ($i < $j) { 29 | $dp[$j][$k] = $dp[$i][$j] + 1; 30 | if ($dp[$j][$k] > $max) { 31 | $max = $dp[$j][$k]; 32 | } 33 | } 34 | } 35 | } 36 | } 37 | 38 | return $max >= 3 ? $max : 0; 39 | } 40 | } -------------------------------------------------------------------------------- /algorithms/000881-boats-to-save-people/solution.php: -------------------------------------------------------------------------------- 1 | dfs($i, $stones, $visited); 18 | $numComponents++; 19 | } 20 | } 21 | 22 | return $n - $numComponents; 23 | } 24 | 25 | /** 26 | * @param $stoneIndex 27 | * @param $stones 28 | * @param $visited 29 | * @return void 30 | */ 31 | function dfs($stoneIndex, &$stones, &$visited) { 32 | $visited[$stoneIndex] = true; 33 | for ($i = 0; $i < count($stones); $i++) { 34 | if (!isset($visited[$i]) && ($stones[$i][0] == $stones[$stoneIndex][0] || $stones[$i][1] == $stones[$stoneIndex][1])) { 35 | self::dfs($i, $stones, $visited); 36 | } 37 | } 38 | } 39 | 40 | } -------------------------------------------------------------------------------- /algorithms/000950-reveal-cards-in-increasing-order/solution.php: -------------------------------------------------------------------------------- 1 | isEmpty()) { 22 | $deque_cards->unshift($deque_cards->pop()); 23 | } 24 | 25 | // Insert the current card to the front of the deque 26 | $deque_cards->unshift($card); 27 | 28 | } 29 | 30 | // Convert the deque back to a list before returning it 31 | return iterator_to_array($deque_cards); 32 | } 33 | } -------------------------------------------------------------------------------- /algorithms/000962-maximum-width-ramp/solution.php: -------------------------------------------------------------------------------- 1 | $nums[$i]) { 18 | array_push($stack, $i); 19 | } 20 | } 21 | 22 | // Step 2: Traverse from the end of the array 23 | for ($j = $n - 1; $j >= 0; $j--) { 24 | // While the stack is not empty and nums[stackTop] <= nums[j] 25 | while (!empty($stack) && $nums[$stack[count($stack) - 1]] <= $nums[$j]) { 26 | // Calculate the ramp width and update maxWidth 27 | $i = array_pop($stack); 28 | $maxWidth = max($maxWidth, $j - $i); 29 | } 30 | } 31 | 32 | return $maxWidth; 33 | } 34 | } -------------------------------------------------------------------------------- /algorithms/000974-subarray-sums-divisible-by-k/solution.php: -------------------------------------------------------------------------------- 1 | str: 9 | def DFS(root,str): 10 | if root is not None: 11 | if root.left is None and root.right is None: 12 | str+=chr(root.val+ord('a')) 13 | RevStr = str[::-1] 14 | #print("RevStr {}".format(RevStr)) 15 | return RevStr 16 | else: 17 | str1=DFS(root.left,str+chr(root.val+ord('a'))) 18 | str2=DFS(root.right,str+chr(root.val+ord('a'))) 19 | #print("str1 {} , str2 {}".format(str1,str2)) 20 | if(str1 and str2): 21 | return min(str1,str2) 22 | elif (str1): 23 | return str1 24 | else: 25 | return str2 26 | else: 27 | return "" 28 | return DFS(root,"") -------------------------------------------------------------------------------- /algorithms/001014-best-sightseeing-pair/solution.php: -------------------------------------------------------------------------------- 1 | val = $val; 11 | * $this->left = $left; 12 | * $this->right = $right; 13 | * } 14 | * } 15 | */ 16 | class Solution { 17 | 18 | /** 19 | * @param TreeNode $root 20 | * @return TreeNode 21 | */ 22 | function bstToGst($root) { 23 | $prefix = 0; 24 | 25 | $reversedInorder = function ($root) use (&$reversedInorder, &$prefix) { 26 | if ($root == null) 27 | return; 28 | 29 | $reversedInorder($root->right); 30 | 31 | $root->val += $prefix; 32 | $prefix = $root->val; 33 | 34 | $reversedInorder($root->left); 35 | }; 36 | 37 | $reversedInorder($root); 38 | return $root; 39 | } 40 | } -------------------------------------------------------------------------------- /algorithms/001051-height-checker/solution.php: -------------------------------------------------------------------------------- 1 | = $minutes && $grumpy[$i - $minutes] == 1) 22 | $windowSatisfied -= $customers[$i - $minutes]; 23 | $madeSatisfied = max($madeSatisfied, $windowSatisfied); 24 | } 25 | 26 | return $satisfied + $madeSatisfied; 27 | } 28 | } -------------------------------------------------------------------------------- /algorithms/001079-letter-tile-possibilities/solution.php: -------------------------------------------------------------------------------- 1 | backtrack($counts, $k, 0, $currentResult); 15 | $total += $currentResult; 16 | } 17 | return $total; 18 | } 19 | 20 | /** 21 | * @param $counts 22 | * @param $k 23 | * @param $currentLength 24 | * @param $result 25 | * @return void 26 | */ 27 | function backtrack(&$counts, $k, $currentLength, &$result) { 28 | if ($currentLength == $k) { 29 | $result++; 30 | return; 31 | } 32 | foreach (array_keys($counts) as $char) { 33 | if ($counts[$char] == 0) { 34 | continue; 35 | } 36 | $counts[$char]--; 37 | $this->backtrack($counts, $k, $currentLength + 1, $result); 38 | $counts[$char]++; 39 | } 40 | } 41 | } -------------------------------------------------------------------------------- /algorithms/001105-filling-bookcase-shelves/solution.php: -------------------------------------------------------------------------------- 1 | 0; $j--) { 19 | $width += $books[$j - 1][0]; 20 | if ($width > $shelfWidth) { 21 | break; 22 | } 23 | $height = max($height, $books[$j - 1][1]); 24 | $dp[$i] = min($dp[$i], $dp[$j - 1] + $height); 25 | } 26 | } 27 | 28 | return $dp[$n]; 29 | 30 | } 31 | } -------------------------------------------------------------------------------- /algorithms/001128-number-of-equivalent-domino-pairs/solution.php: -------------------------------------------------------------------------------- 1 | = 2) { 24 | $total += $count * ($count - 1) / 2; 25 | } 26 | } 27 | return $total; 28 | } 29 | } -------------------------------------------------------------------------------- /algorithms/001137-n-th-tribonacci-number/solution.php: -------------------------------------------------------------------------------- 1 | = $m || $j < 0 || $j >= $n || !$grid[$i][$j]) { 15 | return 0; 16 | } 17 | $v = $grid[$i][$j]; 18 | $grid[$i][$j] = 0; 19 | $ans = $v + max([$dfs($i - 1, $j), $dfs($i + 1, $j), $dfs($i, $j - 1), $dfs($i, $j + 1)]); 20 | $grid[$i][$j] = $v; 21 | return $ans; 22 | }; 23 | 24 | $ans = 0; 25 | for ($i = 0; $i < $m; ++$i) { 26 | for ($j = 0; $j < $n; ++$j) { 27 | $ans = max($ans, $dfs($i, $j)); 28 | } 29 | } 30 | 31 | return $ans; 32 | } 33 | } -------------------------------------------------------------------------------- /algorithms/001233-remove-sub-folders-from-the-filesystem/solution.php: -------------------------------------------------------------------------------- 1 | val = $val; 11 | * $this->left = $left; 12 | * $this->right = $right; 13 | * } 14 | * } 15 | */ 16 | class Solution { 17 | 18 | /** 19 | * @param TreeNode $root 20 | * @param Integer $target 21 | * @return TreeNode 22 | */ 23 | function removeLeafNodes($root, $target) { 24 | if ($root == null) return null; 25 | 26 | if ($root->left == null && $root->right == null && $root->val == $target) 27 | return null; 28 | 29 | $root->left = $this->removeLeafNodes($root->left, $target); 30 | $root->right = $this->removeLeafNodes($root->right, $target); 31 | 32 | if ($root->left == null && $root->right == null && $root->val == $target) 33 | return null; 34 | 35 | return $root; 36 | } 37 | } -------------------------------------------------------------------------------- /algorithms/001331-rank-transform-of-an-array/solution.php: -------------------------------------------------------------------------------- 1 | prefixProducts = [1]; 13 | } 14 | 15 | /** 16 | * @param Integer $num 17 | * @return NULL 18 | */ 19 | function add($num) { 20 | if ($num == 0) { 21 | $this->prefixProducts = [1]; 22 | } else { 23 | $last = end($this->prefixProducts); 24 | $this->prefixProducts[] = $last * $num; 25 | } 26 | } 27 | 28 | /** 29 | * @param Integer $k 30 | * @return Integer 31 | */ 32 | function getProduct($k) { 33 | $n = count($this->prefixProducts) - 1; 34 | if ($k > $n) { 35 | return 0; 36 | } 37 | return $this->prefixProducts[$n] / $this->prefixProducts[$n - $k]; 38 | } 39 | } 40 | 41 | /** 42 | * Your ProductOfNumbers object will be instantiated and called as such: 43 | * $obj = ProductOfNumbers(); 44 | * $obj->add($num); 45 | * $ret_2 = $obj->getProduct($k); 46 | */ -------------------------------------------------------------------------------- /algorithms/001358-number-of-substrings-containing-all-three-characters/solution.php: -------------------------------------------------------------------------------- 1 | $rating[$j]) $leftMore++; 19 | } 20 | 21 | for ($k = $j + 1; $k < $n; $k++) { 22 | if ($rating[$k] < $rating[$j]) $rightLess++; 23 | if ($rating[$k] > $rating[$j]) $rightMore++; 24 | } 25 | 26 | $count += $leftLess * $rightMore + $leftMore * $rightLess; 27 | } 28 | 29 | return $count; 30 | } 31 | } -------------------------------------------------------------------------------- /algorithms/001399-count-largest-group/solution.php: -------------------------------------------------------------------------------- 1 | digitSum($i); 13 | if (isset($counts[$s])) { 14 | $counts[$s]++; 15 | } else { 16 | $counts[$s] = 1; 17 | } 18 | } 19 | $max = max($counts); 20 | $result = 0; 21 | foreach ($counts as $cnt) { 22 | if ($cnt == $max) { 23 | $result++; 24 | } 25 | } 26 | return $result; 27 | } 28 | 29 | /** 30 | * @param $num 31 | * @return int 32 | */ 33 | function digitSum($num) { 34 | $sum = 0; 35 | while ($num != 0) { 36 | $sum += $num % 10; 37 | $num = (int) ($num / 10); 38 | } 39 | return $sum; 40 | } 41 | } -------------------------------------------------------------------------------- /algorithms/001400-construct-k-palindrome-strings/solution.php: -------------------------------------------------------------------------------- 1 | $k) { 37 | return false; 38 | } 39 | 40 | // Step 5: Otherwise, return true 41 | return true; 42 | } 43 | } -------------------------------------------------------------------------------- /algorithms/001404-number-of-steps-to-reduce-a-number-in-binary-representation-to-one/solution.php: -------------------------------------------------------------------------------- 1 | generate("", $n, null, $result); 13 | return (count($result) >= $k) ? $result[$k - 1] : ""; 14 | } 15 | 16 | /** 17 | * @param $current 18 | * @param $remaining 19 | * @param $last_char 20 | * @param $result 21 | * @return void 22 | */ 23 | function generate($current, $remaining, $last_char, &$result) { 24 | if ($remaining == 0) { 25 | array_push($result, $current); 26 | return; 27 | } 28 | $chars = array('a', 'b', 'c'); 29 | foreach ($chars as $c) { 30 | if ($last_char === null || $c != $last_char) { 31 | $this->generate($current . $c, $remaining - 1, $c, $result); 32 | } 33 | } 34 | } 35 | } -------------------------------------------------------------------------------- /algorithms/001438-longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit/solution.php: -------------------------------------------------------------------------------- 1 | isEmpty() && $minQ->top() > $nums[$r]) 17 | $minQ->pop(); 18 | $minQ->push($nums[$r]); 19 | while (!$maxQ->isEmpty() && $maxQ->top() < $nums[$r]) 20 | $maxQ->pop(); 21 | $maxQ->push($nums[$r]); 22 | while ($maxQ->bottom() - $minQ->bottom() > $limit) { 23 | if ($minQ->bottom() == $nums[$l]) 24 | $minQ->shift(); 25 | if ($maxQ->bottom() == $nums[$l]) 26 | $maxQ->shift(); 27 | ++$l; 28 | } 29 | $ans = max($ans, $r - $l + 1); 30 | } 31 | 32 | return $ans; 33 | } 34 | } -------------------------------------------------------------------------------- /algorithms/001442-count-triplets-that-can-form-two-arrays-of-equal-xor/solution.php: -------------------------------------------------------------------------------- 1 | $a) { 15 | $prefix ^= $a; 16 | $xors[] = $prefix; 17 | } 18 | 19 | for ($j = 1; $j < count($arr); $j++) { 20 | for ($i = 0; $i < $j; $i++) { 21 | $xors_i = $xors[$j] ^ $xors[$i]; 22 | for ($k = $j; $k < count($arr); $k++) { 23 | $xors_k = $xors[$k + 1] ^ $xors[$j]; 24 | if ($xors_i == $xors_k) { 25 | $ans += 1; 26 | } 27 | } 28 | } 29 | } 30 | 31 | return $ans; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /algorithms/001455-check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence/solution.php: -------------------------------------------------------------------------------- 1 | $word) { 16 | // Check if searchWord is a prefix of the word 17 | if (substr($word, 0, strlen($searchWord)) === $searchWord) { 18 | // Return the 1-indexed position 19 | return $index + 1; 20 | } 21 | } 22 | 23 | // If no word contains searchWord as a prefix, return -1 24 | return -1; 25 | } 26 | } -------------------------------------------------------------------------------- /algorithms/001460-make-two-arrays-equal-by-reversing-subarrays/solution.php: -------------------------------------------------------------------------------- 1 | = $prices[$i]) { 17 | $index = array_pop($stack); // Pop the index from the stack 18 | $answer[$index] = $prices[$index] - $prices[$i]; // Apply the discount 19 | } 20 | array_push($stack, $i); // Push the current index onto the stack 21 | } 22 | 23 | return $answer; 24 | } 25 | } -------------------------------------------------------------------------------- /algorithms/001497-check-if-array-pairs-are-divisible-by-k/solution.php: -------------------------------------------------------------------------------- 1 | 0) { 15 | // Drink all the current full bottles 16 | $totalDrunk += $numBottles; 17 | 18 | // Collect the empty bottles 19 | $emptyBottles += $numBottles; 20 | 21 | // Exchange the empty bottles for new full ones 22 | $numBottles = floor($emptyBottles / $numExchange); 23 | $emptyBottles = $emptyBottles % $numExchange; 24 | } 25 | 26 | return $totalDrunk; 27 | } 28 | } -------------------------------------------------------------------------------- /algorithms/001524-number-of-sub-arrays-with-odd-sum/solution.php: -------------------------------------------------------------------------------- 1 | $a) { 19 | continue; 20 | } 21 | for ($k = $j + 1; $k < $n; $k++) { 22 | $diff_jk = abs($arr[$j] - $arr[$k]); 23 | if ($diff_jk > $b) { 24 | continue; 25 | } 26 | $diff_ik = abs($arr[$i] - $arr[$k]); 27 | if ($diff_ik <= $c) { 28 | $count++; 29 | } 30 | } 31 | } 32 | } 33 | return $count; 34 | } 35 | } -------------------------------------------------------------------------------- /algorithms/001544-make-the-string-great/solution.php: -------------------------------------------------------------------------------- 1 | isBadPair(end($ans), $c)) { 14 | array_pop($ans); 15 | } else { 16 | $ans[] = $c; 17 | } 18 | } 19 | return implode('', $ans); 20 | } 21 | 22 | /** 23 | * @param $a 24 | * @param $b 25 | * @return bool 26 | */ 27 | private function isBadPair($a, $b): bool 28 | { 29 | return $a != $b && strtolower($a) == strtolower($b); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /algorithms/001545-find-kth-bit-in-nth-binary-string/solution.php: -------------------------------------------------------------------------------- 1 | findKthBit($n - 1, $k); 21 | } else { 22 | $result = $this->findKthBit($n - 1, $mid * 2 - $k); 23 | return $result === "0" ? "1" : "0"; // Flip the bit 24 | } 25 | } 26 | } -------------------------------------------------------------------------------- /algorithms/001550-three-consecutive-odds/solution.php: -------------------------------------------------------------------------------- 1 | 0) { 15 | $depth--; 16 | } 17 | } elseif ($log != "./") { 18 | $depth++; 19 | } 20 | } 21 | 22 | return $depth; 23 | } 24 | } -------------------------------------------------------------------------------- /algorithms/001605-find-valid-matrix-given-row-and-column-sums/solution.php: -------------------------------------------------------------------------------- 1 | = count($nums)) 13 | return count($nums); 14 | 15 | for ($i = 1; $i < count($nums); ++$i) { 16 | $count = count($nums) - $i; 17 | if ($nums[$i - 1] < $count && $nums[$i] >= $count) 18 | return $count; 19 | } 20 | 21 | return -1; 22 | } 23 | } -------------------------------------------------------------------------------- /algorithms/001614-maximum-nesting-depth-of-the-parentheses/solution.php: -------------------------------------------------------------------------------- 1 | 0) { 23 | for ($j = 1; $j <= $k; $j++) { 24 | $sum += $code[($i + $j) % $n]; // Wrap around using modulo 25 | } 26 | } else { // k < 0 27 | for ($j = 1; $j <= abs($k); $j++) { 28 | $sum += $code[($i - $j + $n) % $n]; // Wrap around backward using modulo 29 | } 30 | } 31 | 32 | $result[$i] = $sum; 33 | } 34 | 35 | return $result; 36 | } 37 | } -------------------------------------------------------------------------------- /algorithms/001653-minimum-deletions-to-make-string-balanced/solution.php: -------------------------------------------------------------------------------- 1 | = 0; $i--) { 23 | $suffixA[$i] = $suffixA[$i + 1] + ($s[$i] == 'a' ? 1 : 0); 24 | } 25 | 26 | // Calculate the minimum deletions needed 27 | $minDeletions = PHP_INT_MAX; 28 | for ($i = 0; $i < $n; $i++) { 29 | $deletions = ($i > 0 ? $prefixB[$i - 1] : 0) + ($i < $n - 1 ? $suffixA[$i + 1] : 0); 30 | $minDeletions = min($minDeletions, $deletions); 31 | } 32 | 33 | return $minDeletions; 34 | } 35 | } -------------------------------------------------------------------------------- /algorithms/001700-number-of-students-unable-to-eat-lunch/solution.php: -------------------------------------------------------------------------------- 1 | = 2) { 25 | $total += 4 * $count * ($count - 1); 26 | } 27 | } 28 | return $total; 29 | } 30 | } -------------------------------------------------------------------------------- /algorithms/001749-maximum-absolute-sum-of-any-subarray/solution.php: -------------------------------------------------------------------------------- 1 | $nums[$i + 1]) { 16 | $pivot = $i + 1; 17 | break; 18 | } 19 | } 20 | 21 | // If no pivot found, the array is already sorted 22 | if ($pivot == -1) { 23 | return true; 24 | } 25 | 26 | // Check if the array is sorted after the pivot 27 | for ($i = $pivot; $i < $n - 1; $i++) { 28 | if ($nums[$i] > $nums[$i + 1]) { 29 | return false; 30 | } 31 | } 32 | 33 | // Check if the last element is less than or equal to the first element 34 | if ($nums[$n - 1] > $nums[0]) { 35 | return false; 36 | } 37 | 38 | return true; 39 | } 40 | } -------------------------------------------------------------------------------- /algorithms/001769-minimum-number-of-operations-to-move-all-balls-to-each-box/solution.php: -------------------------------------------------------------------------------- 1 | = 0; $i--) { 28 | $answer[$i] += $rightMoves; 29 | if ($boxes[$i] == '1') { 30 | $rightCount++; 31 | } 32 | $rightMoves += $rightCount; 33 | } 34 | 35 | return $answer; 36 | } 37 | } -------------------------------------------------------------------------------- /algorithms/001780-check-if-number-is-a-sum-of-powers-of-three/solution.php: -------------------------------------------------------------------------------- 1 | 0) { 11 | $remainder = $n % 3; 12 | if ($remainder == 2) { 13 | return false; 14 | } 15 | $n = (int) ($n / 3); 16 | } 17 | return true; 18 | } 19 | } -------------------------------------------------------------------------------- /algorithms/001790-check-if-one-string-swap-can-make-strings-equal/solution.php: -------------------------------------------------------------------------------- 1 | 2) { 21 | return false; 22 | } 23 | } 24 | 25 | if (count($diff) != 2) { 26 | return false; 27 | } 28 | 29 | $i = $diff[0]; 30 | $j = $diff[1]; 31 | return ($s1[$i] == $s2[$j] && $s1[$j] == $s2[$i]); 32 | } 33 | } -------------------------------------------------------------------------------- /algorithms/001800-maximum-ascending-subarray-sum/solution.php: -------------------------------------------------------------------------------- 1 | $nums[$i-1]) { 14 | $current_sum += $nums[$i]; 15 | } else { 16 | $current_sum = $nums[$i]; 17 | } 18 | if ($current_sum > $max_sum) { 19 | $max_sum = $current_sum; 20 | } 21 | } 22 | return $max_sum; 23 | } 24 | } -------------------------------------------------------------------------------- /algorithms/001813-sentence-similarity-iii/solution.php: -------------------------------------------------------------------------------- 1 | = min($n1, $n2); 33 | } 34 | } -------------------------------------------------------------------------------- /algorithms/001823-find-the-winner-of-the-circular-game/solution.php: -------------------------------------------------------------------------------- 1 | = 0; $i--) { 23 | // Find k that maximizes the XOR result 24 | $answer[] = $currentXOR ^ $maxNum; 25 | 26 | // Remove the last element from currentXOR by XORing it out 27 | $currentXOR ^= $nums[$i]; 28 | } 29 | 30 | return $answer; // We populated answer in reverse order 31 | } 32 | } -------------------------------------------------------------------------------- /algorithms/001863-sum-of-all-subset-xor-totals/solution.php: -------------------------------------------------------------------------------- 1 | pow_mod(5, $even_count, $mod); 15 | $odd_power = $this->pow_mod(4, $odd_count, $mod); 16 | 17 | return (int)(($even_power * $odd_power) % $mod); 18 | } 19 | 20 | /** 21 | * @param $base 22 | * @param $exponent 23 | * @param $mod 24 | * @return int 25 | */ 26 | function pow_mod($base, $exponent, $mod) { 27 | $result = 1; 28 | $base = $base % $mod; 29 | while ($exponent > 0) { 30 | if ($exponent % 2 == 1) { 31 | $result = ($result * $base) % $mod; 32 | } 33 | $base = ($base * $base) % $mod; 34 | $exponent = (int)($exponent / 2); 35 | } 36 | return $result; 37 | } 38 | } -------------------------------------------------------------------------------- /algorithms/001945-sum-of-digits-of-string-after-convert/solution.php: -------------------------------------------------------------------------------- 1 | 1 && $s[$i] == $result[strlen($result) - 1] && $s[$i] == $result[strlen($result) - 2]) { 16 | continue; // Skip adding the character if it makes three consecutive 17 | } 18 | 19 | $result .= $s[$i]; // Add character to result 20 | } 21 | 22 | return $result; 23 | } 24 | } -------------------------------------------------------------------------------- /algorithms/001963-minimum-number-of-swaps-to-make-the-string-balanced/solution.php: -------------------------------------------------------------------------------- 1 | graph = array_fill(0, $n, 0); 18 | 19 | for ($i = 1; $i < $n; $i++) { 20 | $this->graph[$i] = $i; 21 | } 22 | 23 | foreach ($edges as $edge) { 24 | $this->merge($this->find($edge[0]), $this->find($edge[1])); 25 | } 26 | 27 | return $this->find($this->graph[$source]) == $this->find($this->graph[$destination]); 28 | } 29 | 30 | public function find($a) { 31 | return $this->graph[$a] == $a ? $a : $this->graph[$a] = $this->find($this->graph[$a]); 32 | } 33 | 34 | public function merge($a, $b): void 35 | { 36 | $this->graph[$a] = $b; 37 | } 38 | } -------------------------------------------------------------------------------- /algorithms/001975-maximum-matrix-sum/solution.php: -------------------------------------------------------------------------------- 1 | = $k) { 31 | return $distinctStrings[$k - 1]; 32 | } else { 33 | return ""; 34 | } 35 | 36 | } 37 | } -------------------------------------------------------------------------------- /algorithms/002073-time-needed-to-buy-tickets/solution.php: -------------------------------------------------------------------------------- 1 | $ticket) { 14 | if ($i <= $k) { 15 | $ans += min($ticket, $tickets[$k]); 16 | } else { 17 | $ans += min($ticket, $tickets[$k] - 1); 18 | } 19 | } 20 | 21 | return $ans; 22 | } 23 | } -------------------------------------------------------------------------------- /algorithms/002109-adding-spaces-to-a-string/solution.php: -------------------------------------------------------------------------------- 1 | = 0; $i--) { 14 | $points = $questions[$i][0]; 15 | $bp = $questions[$i][1]; 16 | $next = $i + $bp + 1; 17 | $solve = $points + ($next <= $n ? $dp[$next] : 0); 18 | $skip = $dp[$i + 1]; 19 | $dp[$i] = max($solve, $skip); 20 | } 21 | 22 | return $dp[0]; 23 | } 24 | } -------------------------------------------------------------------------------- /algorithms/002145-count-the-hidden-sequences/solution.php: -------------------------------------------------------------------------------- 1 | $max_lower) { 20 | $max_lower = $lower_i; 21 | } 22 | $upper_i = $upper - $current_sum; 23 | if ($upper_i < $min_upper) { 24 | $min_upper = $upper_i; 25 | } 26 | } 27 | 28 | if ($min_upper >= $max_lower) { 29 | return $min_upper - $max_lower + 1; 30 | } else { 31 | return 0; 32 | } 33 | } 34 | } -------------------------------------------------------------------------------- /algorithms/002161-partition-array-according-to-given-pivot/solution.php: -------------------------------------------------------------------------------- 1 | val = $val; 10 | * $this->next = $next; 11 | * } 12 | * } 13 | */ 14 | class Solution { 15 | 16 | /** 17 | * @param ListNode $head 18 | * @return ListNode 19 | */ 20 | function mergeNodes($head) { 21 | $dummy = new ListNode(0); 22 | $current = $dummy; 23 | $sum = 0; 24 | 25 | // Skip the first zero node 26 | $head = $head->next; 27 | 28 | while ($head !== null) { 29 | if ($head->val == 0) { 30 | $current->next = new ListNode($sum); 31 | $current = $current->next; 32 | $sum = 0; 33 | } else { 34 | $sum += $head->val; 35 | } 36 | $head = $head->next; 37 | } 38 | 39 | return $dummy->next; 40 | } 41 | } -------------------------------------------------------------------------------- /algorithms/002185-counting-words-with-a-given-prefix/solution.php: -------------------------------------------------------------------------------- 1 | 0) { 17 | // Increment count if the last bit is 1 18 | $bitFlips += $xor & 1; 19 | // Shift xor to the right by 1 bit 20 | $xor >>= 1; 21 | } 22 | 23 | return $bitFlips; 24 | 25 | } 26 | } -------------------------------------------------------------------------------- /algorithms/002226-maximum-candies-allocated-to-k-children/solution.php: -------------------------------------------------------------------------------- 1 | = $k) { 26 | break; 27 | } 28 | } 29 | if ($total >= $k) { 30 | $ans = $mid; 31 | $left = $mid + 1; 32 | } else { 33 | $right = $mid - 1; 34 | } 35 | } 36 | 37 | return $ans; 38 | } 39 | } -------------------------------------------------------------------------------- /algorithms/002270-number-of-ways-to-split-array/solution.php: -------------------------------------------------------------------------------- 1 | = $remainingSum) { 22 | $validSplits++; 23 | } 24 | } 25 | 26 | return $validSplits; 27 | } 28 | } -------------------------------------------------------------------------------- /algorithms/002275-largest-combination-with-bitwise-and-greater-than-zero/solution.php: -------------------------------------------------------------------------------- 1 | val = $val; 11 | * $this->left = $left; 12 | * $this->right = $right; 13 | * } 14 | * } 15 | */ 16 | class Solution { 17 | 18 | /** 19 | * @param TreeNode $root 20 | * @return Boolean 21 | */ 22 | function evaluateTree(TreeNode $root): bool 23 | { 24 | if ($root->val < 2){ 25 | $return = $root->val; 26 | }elseif ($root->val == 2){ 27 | $return = $this->evaluateTree($root->left) || $this->evaluateTree($root->right); 28 | }else{ 29 | $return = $this->evaluateTree($root->left) && $this->evaluateTree($root->right); 30 | } 31 | return $return; 32 | } 33 | } -------------------------------------------------------------------------------- /algorithms/002364-count-number-of-bad-pairs/solution.php: -------------------------------------------------------------------------------- 1 | $num) { 14 | $key = $num - $i; 15 | if (!isset($freq[$key])) { 16 | $freq[$key] = 0; 17 | } 18 | $freq[$key]++; 19 | } 20 | $good = 0; 21 | foreach ($freq as $count) { 22 | if ($count >= 2) { 23 | $good += $count * ($count - 1) / 2; 24 | } 25 | } 26 | return $total - $good; 27 | } 28 | } -------------------------------------------------------------------------------- /algorithms/002370-longest-ideal-subsequence/solution.php: -------------------------------------------------------------------------------- 1 | getMaxReachable($dp, $charIndex, $k); 19 | } 20 | 21 | return max($dp); 22 | } 23 | 24 | /** 25 | * @param $dp 26 | * @param $i 27 | * @param $k 28 | * @return int|mixed 29 | */ 30 | private function getMaxReachable($dp, $i, $k) { 31 | $first = max(0, $i - $k); 32 | $last = min(25, $i + $k); 33 | $maxReachable = 0; 34 | for ($j = $first; $j <= $last; $j++) { 35 | $maxReachable = max($maxReachable, $dp[$j]); 36 | } 37 | return $maxReachable; 38 | } 39 | } -------------------------------------------------------------------------------- /algorithms/002373-largest-local-values-in-a-matrix/solution.php: -------------------------------------------------------------------------------- 1 | = $left) { 19 | $left = max($left, $last_occurrence[$i] + 1); 20 | } 21 | } 22 | } 23 | for ($i = 0; $i <= 30; $i++) { 24 | if (($current & (1 << $i)) != 0) { 25 | $last_occurrence[$i] = $right; 26 | } 27 | } 28 | $max_len = max($max_len, $right - $left + 1); 29 | } 30 | return $max_len; 31 | } 32 | } -------------------------------------------------------------------------------- /algorithms/002406-divide-intervals-into-minimum-number-of-groups/solution.php: -------------------------------------------------------------------------------- 1 | = 0; $i--) { 18 | $min_right[$i] = min($s[$i], $min_right[$i + 1]); 19 | } 20 | 21 | $stack = array(); 22 | $res = array(); 23 | 24 | for ($i = 0; $i < $n; $i++) { 25 | array_push($stack, $s[$i]); 26 | while (!empty($stack)) { 27 | $top = end($stack); 28 | if ($top <= $min_right[$i + 1]) { 29 | array_pop($stack); 30 | $res[] = $top; 31 | } else { 32 | break; 33 | } 34 | } 35 | } 36 | 37 | return implode('', $res); 38 | } 39 | } -------------------------------------------------------------------------------- /algorithms/002441-largest-positive-integer-that-exists-with-its-negative/README.md: -------------------------------------------------------------------------------- 1 | 2441\. Largest Positive Integer That Exists With Its Negative 2 | 3 | Easy 4 | 5 | Given an integer array `nums` that **does not contain** any zeros, find **the largest positive** integer `k` such that `-k` also exists in the array. 6 | 7 | Return _the positive integer `k`. If there is no such integer, return `-1`_. 8 | 9 | **Example 1:** 10 | 11 | - **Input:** nums = [-1,2,-3,3] 12 | - **Output:** 3 13 | - **Explanation:** 3 is the only valid k we can find in the array. 14 | 15 | **Example 2:** 16 | 17 | - **Input:** nums = [-1,10,6,7,-7,1] 18 | - **Output:** 7 19 | - **Explanation:** Both 1 and 7 have their corresponding negative values in the array. 7 has a larger value. 20 | 21 | **Example 3:** 22 | 23 | - **Input:** nums = [-10,8,6,7,-2,-3] 24 | - **Output:** -1 25 | - **Explanation:** There is no a single valid k, we return -1. 26 | 27 | **Constraints:** 28 | 29 | - 1 <= nums.length <= 1000 30 | - -1000 <= nums[i] <= 1000 31 | - nums[i] != 0 -------------------------------------------------------------------------------- /algorithms/002441-largest-positive-integer-that-exists-with-its-negative/solution.php: -------------------------------------------------------------------------------- 1 | $maxK) { 21 | $start = $i + 1; 22 | $lastMin = -1; 23 | $lastMax = -1; 24 | } else { 25 | if ($num == $minK) { 26 | $lastMin = $i; 27 | } 28 | if ($num == $maxK) { 29 | $lastMax = $i; 30 | } 31 | if ($lastMin != -1 && $lastMax != -1) { 32 | $count += max(0, min($lastMin, $lastMax) - $start + 1); 33 | } 34 | } 35 | } 36 | return $count; 37 | } 38 | } -------------------------------------------------------------------------------- /algorithms/002460-apply-operations-to-an-array/solution.php: -------------------------------------------------------------------------------- 1 | [1, 105]. 31 | - 1 <= Node.val <= 105 32 | -------------------------------------------------------------------------------- /algorithms/002487-remove-nodes-from-linked-list/solution.php: -------------------------------------------------------------------------------- 1 | val = $val; 14 | $this->next = $next; 15 | } 16 | } 17 | 18 | class Solution 19 | { 20 | 21 | /** 22 | * @param ListNode $head 23 | * @return ListNode|null 24 | */ 25 | function removeNodes(ListNode $head): ?ListNode 26 | { 27 | if ($head == null) { 28 | return null; 29 | } 30 | $head->next = $this->removeNodes($head->next); 31 | return $head->next != null && $head->val < $head->next->val ? $head->next : $head; 32 | } 33 | } -------------------------------------------------------------------------------- /algorithms/002490-circular-sentence/solution.php: -------------------------------------------------------------------------------- 1 | = 2) { 31 | $maxStreak = max($maxStreak, $currentStreak); 32 | } 33 | } 34 | 35 | // Step 4: Return result 36 | return $maxStreak >= 2 ? $maxStreak : -1; 37 | } 38 | } -------------------------------------------------------------------------------- /algorithms/002516-take-k-of-each-character-from-left-and-right/solution.php: -------------------------------------------------------------------------------- 1 | insert($num); 17 | } 18 | 19 | // Perform k operations 20 | for ($i = 0; $i < $k; $i++) { 21 | // Extract the largest element 22 | $t = $pq->extract(); 23 | 24 | // Add it to the score 25 | $sum += $t; 26 | 27 | // Calculate the new value using ceil(t / 3) 28 | $newVal = (int)(($t + 2) / 3); 29 | 30 | // Insert the updated value back into the heap 31 | $pq->insert($newVal); 32 | } 33 | 34 | return $sum; 35 | } 36 | } -------------------------------------------------------------------------------- /algorithms/002530-maximal-score-after-applying-k-operations/solution.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def maxKelements(self, nums: List[int], k: int) -> int: 3 | for i, v in enumerate(nums): 4 | nums[i] = -v 5 | heapify(nums) 6 | ans = 0 7 | for _ in range(k): 8 | ans -= heapreplace(nums, -ceil(-nums[0] / 3)) 9 | return ans -------------------------------------------------------------------------------- /algorithms/002551-put-marbles-in-bags/solution.php: -------------------------------------------------------------------------------- 1 | = $pair_count - $m; $i--) { 32 | $max_sum += $pairs[$i]; 33 | } 34 | 35 | return $max_sum - $min_sum; 36 | } 37 | } -------------------------------------------------------------------------------- /algorithms/002554-maximum-number-of-integers-to-choose-from-a-range-i/solution.php: -------------------------------------------------------------------------------- 1 | $maxSum) { 27 | break; 28 | } 29 | 30 | // Add this number to the sum and increment the count 31 | $sum += $i; 32 | $count++; 33 | } 34 | 35 | return $count; 36 | } 37 | } -------------------------------------------------------------------------------- /algorithms/002559-count-vowel-strings-in-ranges/solution.php: -------------------------------------------------------------------------------- 1 | isVowelString($words[$i]) ? 1 : 0); 16 | } 17 | 18 | // Step 2: Answer queries using the prefix sum 19 | $result = []; 20 | foreach ($queries as $query) { 21 | list($l, $r) = $query; 22 | $result[] = $prefixSum[$r + 1] - $prefixSum[$l]; 23 | } 24 | 25 | return $result; 26 | } 27 | 28 | /** 29 | * Helper function to check if a string starts and ends with a vowel 30 | * 31 | * @param $word 32 | * @return bool 33 | */ 34 | function isVowelString($word) { 35 | $vowels = ['a', 'e', 'i', 'o', 'u']; 36 | $n = strlen($word); 37 | return in_array($word[0], $vowels) && in_array($word[$n - 1], $vowels); 38 | } 39 | } -------------------------------------------------------------------------------- /algorithms/002570-merge-two-2d-arrays-by-summing-values-solution/solution.php: -------------------------------------------------------------------------------- 1 | List[List[int]]: 31 | -------------------------------------------------------------------------------- /algorithms/002579-count-total-number-of-colored-cells/solution.php: -------------------------------------------------------------------------------- 1 | = $cars) { 24 | $high = $mid; 25 | } else { 26 | $low = $mid + 1; 27 | } 28 | } 29 | return $low; 30 | } 31 | } -------------------------------------------------------------------------------- /algorithms/002657-find-the-prefix-common-array-of-two-arrays/solution.php: -------------------------------------------------------------------------------- 1 | 0 && $freqB[$j] > 0) { 26 | $commonCount++; 27 | } 28 | } 29 | 30 | // Store the common count in the result array 31 | $result[] = $commonCount; 32 | } 33 | 34 | return $result; 35 | } 36 | } -------------------------------------------------------------------------------- /algorithms/002678-number-of-senior-citizens/solution.php: -------------------------------------------------------------------------------- 1 | 60) { 18 | $seniorCount++; 19 | } 20 | } 21 | 22 | return $seniorCount; 23 | 24 | } 25 | } -------------------------------------------------------------------------------- /algorithms/002683-neighboring-bitwise-xor/solution.php: -------------------------------------------------------------------------------- 1 | 2 * $k) { 21 | $i++; // Shrink the window from the left 22 | } 23 | // Update the maximum beauty (window size) 24 | $maxBeauty = max($maxBeauty, $j - $i + 1); 25 | } 26 | 27 | return $maxBeauty; 28 | } 29 | } -------------------------------------------------------------------------------- /algorithms/002816-double-a-number-represented-as-a-linked-list/solution.php: -------------------------------------------------------------------------------- 1 | val = $val; 15 | $this->next = $next; 16 | } 17 | } 18 | 19 | class Solution 20 | { 21 | 22 | /** 23 | * @param ListNode $head 24 | * @return ListNode 25 | */ 26 | function doubleIt(ListNode $head): ListNode 27 | { 28 | if ($head->val >= 5) 29 | $head = new ListNode(0, $head); 30 | 31 | for ($curr = $head; $curr != null; $curr = $curr->next) { 32 | $curr->val *= 2; 33 | $curr->val %= 10; 34 | if ($curr->next && $curr->next->val >= 5) 35 | ++$curr->val; 36 | } 37 | 38 | return $head; 39 | } 40 | } -------------------------------------------------------------------------------- /algorithms/002825-make-string-a-subsequence-using-cyclic-increments/solution.php: -------------------------------------------------------------------------------- 1 | =3, but added for safety 12 | 13 | $max_right = array_fill(0, $len, 0); 14 | for ($j = $len - 2; $j >= 0; $j--) { 15 | $max_right[$j] = max($nums[$j + 1], $max_right[$j + 1]); 16 | } 17 | 18 | $max_val = PHP_INT_MIN; 19 | for ($i = 0; $i < $len - 1; $i++) { 20 | for ($j = $i + 1; $j < $len - 1; $j++) { 21 | $current = ($nums[$i] - $nums[$j]) * $max_right[$j]; 22 | if ($current > $max_val) { 23 | $max_val = $current; 24 | } 25 | } 26 | } 27 | 28 | return $max_val > 0 ? $max_val : 0; 29 | } 30 | } -------------------------------------------------------------------------------- /algorithms/002874-maximum-value-of-an-ordered-triplet-ii/solution.php: -------------------------------------------------------------------------------- 1 | $num) { 16 | // Calculate the complement of the current number 17 | $complement = $target - $num; 18 | 19 | // Check if the complement exists in the map 20 | if (isset($map[$complement])) { 21 | // If found, return the indices of the complement and the current number 22 | return [$map[$complement], $index]; 23 | } 24 | 25 | // Otherwise, add the current number and its index to the map 26 | $map[$num] = $index; 27 | } 28 | 29 | // If no solution is found (although the problem guarantees one), return an empty array 30 | return []; 31 | } 32 | } -------------------------------------------------------------------------------- /algorithms/002894-divisible-and-non-divisible-sums-difference/solution.php: -------------------------------------------------------------------------------- 1 | = 0; $i--) { 16 | if ($s[$i] == '0') { 17 | // Increment the count of zeros when encountering '0' 18 | $zeroCount++; 19 | } elseif ($s[$i] == '1') { 20 | // When encountering '1', add the count of '0's to swaps 21 | $swaps += $zeroCount; 22 | } 23 | } 24 | 25 | return $swaps; 26 | } 27 | } -------------------------------------------------------------------------------- /algorithms/002942-find-words-containing-character/solution.php: -------------------------------------------------------------------------------- 1 | $word) { 13 | if (strpos($word, $x) !== false) { 14 | array_push($result, $index); 15 | } 16 | } 17 | return $result; 18 | } 19 | } -------------------------------------------------------------------------------- /algorithms/002962-count-subarrays-where-max-element-appears-at-least-k-times/solution.php: -------------------------------------------------------------------------------- 1 | $num) { 14 | if ($num == $max) { 15 | $indices[] = $i; 16 | } 17 | } 18 | $m = count($indices); 19 | if ($m < $k) { 20 | return 0; 21 | } 22 | $n = count($nums); 23 | $total = 0; 24 | for ($j = $k - 1; $j < $m; $j++) { 25 | $i = $j - $k + 1; 26 | $prev = ($i > 0) ? $indices[$i - 1] : -1; 27 | $left = $indices[$i] - $prev; 28 | $right = $n - $indices[$j]; 29 | $total += $left * $right; 30 | } 31 | return $total; 32 | } 33 | } -------------------------------------------------------------------------------- /algorithms/002965-find-missing-and-repeated-values/solution.php: -------------------------------------------------------------------------------- 1 | $cnt) { 25 | if ($cnt == 2) { 26 | $a = $num; 27 | break; 28 | } 29 | } 30 | 31 | $n = count($grid); 32 | $max = $n * $n; 33 | $S = $max * ($max + 1) / 2; 34 | $b = $a + ($S - $sum); 35 | 36 | return array($a, $b); 37 | } 38 | } -------------------------------------------------------------------------------- /algorithms/002997-minimum-number-of-operations-to-make-array-xor-equal-to-k/solution.php: -------------------------------------------------------------------------------- 1 | $count) { 27 | // Determine the number of pushes for the current character 28 | $pushesForChar = (int)($position / 8) + 1; 29 | $pushes += $pushesForChar * $count; 30 | $position++; 31 | } 32 | 33 | return $pushes; 34 | 35 | } 36 | } -------------------------------------------------------------------------------- /algorithms/003024-type-of-triangle/solution.php: -------------------------------------------------------------------------------- 1 | isPrefixAndSuffix($words[$i], $words[$j])) { 18 | $count++; 19 | } 20 | } 21 | } 22 | 23 | return $count; 24 | } 25 | 26 | /** 27 | * Function to check if str1 is both a prefix and a suffix of str2 28 | * 29 | * @param $str1 30 | * @param $str2 31 | * @return bool 32 | */ 33 | function isPrefixAndSuffix($str1, $str2) { 34 | // Check if str1 is a prefix and a suffix of str2 35 | return (substr($str2, 0, strlen($str1)) === $str1 && substr($str2, -strlen($str1)) === $str1); 36 | } 37 | } -------------------------------------------------------------------------------- /algorithms/003068-find-the-maximum-sum-of-node-values/solution.php: -------------------------------------------------------------------------------- 1 | $num) ? 1 : 0; 19 | $minChangeDiff = min($minChangeDiff, abs($num - ($num ^ $k))); 20 | } 21 | 22 | if ($changedCount % 2 == 0) 23 | return $maxSum; 24 | return $maxSum - $minChangeDiff; 25 | } 26 | } -------------------------------------------------------------------------------- /algorithms/003075-maximize-happiness-of-selected-children/solution.php: -------------------------------------------------------------------------------- 1 | $nums[$i - 1]) { 19 | $incLen++; 20 | $decLen = 1; 21 | } elseif ($nums[$i] < $nums[$i - 1]) { 22 | $decLen++; 23 | $incLen = 1; 24 | } else { 25 | $incLen = 1; 26 | $decLen = 1; 27 | } 28 | 29 | if ($incLen > $maxLen) { 30 | $maxLen = $incLen; 31 | } 32 | if ($decLen > $maxLen) { 33 | $maxLen = $decLen; 34 | } 35 | } 36 | 37 | return $maxLen; 38 | } 39 | } -------------------------------------------------------------------------------- /algorithms/003110-score-of-a-string/README.md: -------------------------------------------------------------------------------- 1 | 3110\. Score of a String 2 | 3 | Easy 4 | 5 | You are given a string `s`. The **score** of a string is defined as the sum of the absolute difference between the **ASCII** values of adjacent characters. 6 | 7 | Return the **score** of `s`. 8 | 9 | **Example 1:** 10 | 11 | - **Input:** s = "hello" 12 | - **Output:** 13 13 | - **Explanation:** 14 | 15 | The ASCII values of the characters in `s` are: `'h' = 104`, `'e' = 101`, `'l' = 108`, `'o' = 111`. So, the score of `s` would be `|104 - 101| + |101 - 108| + |108 - 108| + |108 - 111| = 3 + 7 + 0 + 3 = 13`. 16 | 17 | **Example 2:** 18 | 19 | - **Input:** s = "zaz" 20 | - **Output:** 50 21 | - **Explanation:** 22 | 23 | The ASCII values of the characters in `s` are: `'z' = 122`, `'a' = 97`. So, the score of `s` would be `|122 - 97| + |97 - 122| = 25 + 25 = 50`. 24 | 25 | 26 | **Constraints:** 27 | 28 | - 2 <= s.length <= 100 29 | - `s` consists only of lowercase English letters. -------------------------------------------------------------------------------- /algorithms/003110-score-of-a-string/solution.php: -------------------------------------------------------------------------------- 1 | 1 <= word.length <= 2 * 105 30 | - `word` consists of only lowercase and uppercase English letters. 31 | -------------------------------------------------------------------------------- /algorithms/003121-count-the-number-of-special-characters-ii/solution.php: -------------------------------------------------------------------------------- 1 | > $i) & 1) == 0) { 21 | // Set the bit in $ans if the corresponding bit in $k is 1 22 | if (($k >> $kBinaryIndex) & 1) { 23 | $ans |= (1 << $i); 24 | } 25 | ++$kBinaryIndex; 26 | } 27 | } 28 | 29 | return $ans; 30 | } 31 | } -------------------------------------------------------------------------------- /algorithms/003151-special-array-i/solution.php: -------------------------------------------------------------------------------- 1 | $n - 3) { 25 | return -1; 26 | } 27 | array_push($queue, $i); 28 | $operations++; 29 | } 30 | } 31 | 32 | return $operations; 33 | } 34 | } -------------------------------------------------------------------------------- /algorithms/003223-minimum-length-of-string-after-operations/solution.php: -------------------------------------------------------------------------------- 1 | = 3 23 | foreach ($frequency as $char => $count) { 24 | if ($count >= 3) { 25 | // Remove pairs of characters 26 | $frequency[$char] = $count % 2 == 0 ? 2 : 1; 27 | } 28 | } 29 | 30 | // Step 3: Calculate the minimum length 31 | $minLength = 0; 32 | foreach ($frequency as $count) { 33 | $minLength += $count; 34 | } 35 | 36 | return $minLength; 37 | } 38 | } -------------------------------------------------------------------------------- /algorithms/003254-find-the-power-of-k-size-subarrays-i/solution.php: -------------------------------------------------------------------------------- 1 | $k) { 20 | $unique_gt[$num] = true; 21 | } 22 | } 23 | 24 | return count($unique_gt); 25 | } 26 | } -------------------------------------------------------------------------------- /algorithms/003392-count-subarrays-of-length-three-with-a-condition/solution.php: -------------------------------------------------------------------------------- 1 | = $n) { 14 | return $k; 15 | } 16 | $subarray = array_slice($nums, $start); 17 | $seen = array(); 18 | $valid = true; 19 | foreach ($subarray as $num) { 20 | if (isset($seen[$num])) { 21 | $valid = false; 22 | break; 23 | } 24 | $seen[$num] = true; 25 | } 26 | if ($valid) { 27 | return $k; 28 | } 29 | } 30 | } 31 | } --------------------------------------------------------------------------------