From 24209125beb47255b0eea278e1f273de38a9338a Mon Sep 17 00:00:00 2001 From: Ognjen Date: Tue, 25 Jul 2023 23:28:05 +0200 Subject: [PATCH 1/3] Remove alternative solutions --- 100-same-tree.js | 20 - 1009-complement-of-base-10-integer.js | 27 -- 1017-convert-to-base-2.js | 15 - 1021-best-sightseeing-pair.js | 17 - 1024-video-stitching.js | 40 -- 1029-two-city-scheduling.js | 42 -- ...inary-tree-zigzag-level-order-traversal.js | 67 ---- ...um-sum-of-two-non-overlapping-subarrays.js | 24 -- 1035-uncrossed-lines.js | 20 - 1036-escape-a-large-maze.js | 56 --- 1041-robot-bounded-in-circle.js | 44 --- 1043-partition-array-for-maximum-sum.js | 25 -- 1044-longest-duplicate-substring.js | 57 --- ...ree-from-preorder-and-inorder-traversal.js | 39 -- 1052-grumpy-bookstore-owner.js | 24 -- 1054-distant-barcodes.js | 69 ---- ...ee-from-inorder-and-postorder-traversal.js | 35 -- 1060-missing-element-in-sorted-array.js | 52 --- 1062-longest-repeating-substring.js | 86 ---- 1066-campus-bikes-ii.js | 95 ----- 1067-digit-count-in-range.js | 35 -- 107-binary-tree-level-order-traversal-ii.js | 19 - 1079-letter-tile-possibilities.js | 52 --- ...lest-subsequence-of-distinct-characters.js | 51 --- ...nvert-sorted-list-to-binary-search-tree.js | 20 - 1091-shortest-path-in-binary-matrix.js | 45 --- 1094-car-pooling.js | 40 -- 1104.path-in-zigzag-labelled-binary-tree.js | 40 -- 1105-filling-bookcase-shelves.js | 44 --- 1109-corporate-flight-bookings.js | 36 -- 1118-number-of-days-in-a-month.js | 23 -- ...owest-common-ancestor-of-deepest-leaves.js | 66 ---- 1125-smallest-sufficient-team.js | 157 -------- 1136-parallel-courses.js | 37 -- 1137-n-th-tribonacci-number.js | 19 - 1143-longest-common-subsequence.js | 25 -- 1146-snapshot-array.js | 130 ------ ...ongest-chunked-palindrome-decomposition.js | 13 - 115-distinct-subsequences.js | 43 -- 1155-number-of-dice-rolls-with-target-sum.js | 19 - 1157-online-majority-element-in-subarray.js | 70 ---- ...lating-next-right-pointers-in-each-node.js | 12 - 1166-design-file-system.js | 50 --- 1167-minimum-cost-to-connect-sticks.js | 145 ------- 1172-dinner-plate-stacks.js | 69 ---- 1176-diet-plan-performance.js | 21 - ...-maximum-subarray-sum-with-one-deletion.js | 25 -- ...trings-between-each-pair-of-parentheses.js | 29 -- 120-triangle.js | 22 -- ...items-by-groups-respecting-dependencies.js | 96 ----- 1206-design-skiplist.js | 84 ---- ...ve-all-adjacent-duplicates-in-string-ii.js | 27 -- 121-best-time-to-buy-and-sell-stock.js | 14 - 1213-intersection-of-three-sorted-arrays.js | 20 - 1214-two-sum-bsts.js | 42 -- 1216-valid-palindrome-iii.js | 42 -- 1219-path-with-maximum-gold.js | 42 -- 1224-maximum-equal-frequency.js | 28 -- 1229-meeting-scheduler.js | 30 -- 1231-divide-chocolate.js | 30 -- 1235-maximum-profit-in-job-scheduling.js | 78 ---- 124-binary-tree-maximum-path-sum.js | 32 -- 1246-palindrome-removal.js | 28 -- ...inimum-remove-to-make-valid-parentheses.js | 54 --- 1254-number-of-closed-islands.js | 65 --- 1262-greatest-sum-divisible-by-three.js | 32 -- ...-to-move-a-box-to-their-target-location.js | 369 ------------------ 1268-search-suggestions-system.js | 88 ----- 1275-find-winner-on-a-tic-tac-toe-game.js | 20 - ...-count-square-submatrices-with-all-ones.js | 19 - ...to-convert-binary-matrix-to-zero-matrix.js | 61 --- 1286-iterator-for-combination.js | 104 ----- 1291-sequential-digits.js | 40 -- 13-roman-to-integer.js | 21 - 130-surrounded-regions.js | 60 --- 1301-number-of-paths-with-max-score.js | 42 -- 1306-jump-game-iii.js | 39 -- 131-palindrome-partitioning.js | 36 -- 1310-xor-queries-of-a-subarray.js | 22 -- 132-palindrome-partitioning-ii.js | 22 -- 1325-delete-leaves-with-a-given-value.js | 20 - ...umber-of-taps-to-open-to-water-a-garden.js | 15 - 134-gas-station.js | 19 - 1340-jump-game-v.js | 66 ---- 1345-jump-game-iv.js | 41 -- 1349-maximum-students-taking-exam.js | 87 ----- ...m-number-of-events-that-can-be-attended.js | 152 -------- ...t-all-valid-pickup-and-delivery-options.js | 13 - 136-single-number.js | 9 - 1366-rank-teams-by-votes.js | 22 -- ...-make-at-least-one-valid-path-in-a-grid.js | 68 ---- 137-single-number-ii.js | 40 -- ...string-containing-vowels-in-even-counts.js | 101 ----- 1377-frog-position-after-t-seconds.js | 44 --- 1383-maximum-performance-of-a-team.js | 77 ---- 1388-pizza-with-3n-slices.js | 27 -- 139-word-break.js | 81 ---- 1395-count-number-of-teams.js | 42 -- 140-word-break-ii.js | 94 ----- 1405-longest-happy-string.js | 64 --- 1406-stone-game-iii.js | 20 - 1411-number-of-ways-to-paint-n-3-grid.js | 21 - ...string-of-all-happy-strings-of-length-n.js | 44 --- 1416-restore-the-array.js | 28 -- 142-linked-list-cycle-ii.js | 20 - ...-maximum-score-after-splitting-a-string.js | 16 - 1425-constrained-subsequence-sum.js | 78 ---- 143-reorder-list.js | 89 ----- ...ys-to-wear-different-hats-to-each-other.js | 32 -- 1436-destination-city.js | 13 - ...solute-diff-less-than-or-equal-to-limit.js | 74 ---- ...s-that-can-form-two-arrays-of-equal-xor.js | 22 -- 1446-consecutive-characters.js | 20 - ...teger-with-digits-that-add-up-to-target.js | 29 -- 146-lru-cache.js | 44 --- 1462-course-schedule-iv.js | 88 ----- ...cake-after-horizontal-and-vertical-cuts.js | 23 -- ...aving-the-same-number-of-distinct-balls.js | 66 ---- 1478-allocate-mailboxes.js | 35 -- 148-sort-list.js | 146 ------- 1490-clone-n-ary-tree.js | 32 -- 1494-parallel-courses-ii.js | 47 --- 1499-max-value-of-equation.js | 91 ----- 15-3sum.js | 61 --- 1506-find-root-of-n-ary-tree.js | 27 -- 151-reverse-words-in-a-string.js | 24 -- 152-maximum-product-subarray.js | 94 ----- ...-count-odd-numbers-in-an-interval-range.js | 10 - 1524-number-of-sub-arrays-with-odd-sum.js | 25 -- 1525-number-of-good-ways-to-split-a-string.js | 29 -- ...nts-on-subarrays-to-form-a-target-array.js | 15 - 1531-string-compression-ii.js | 34 -- 1537-get-the-maximum-score.js | 35 -- ...find-minimum-in-rotated-sorted-array-ii.js | 17 - ...ertions-to-balance-a-parentheses-string.js | 54 --- 1542-find-longest-awesome-substring.js | 23 -- 1547-minimum-cost-to-cut-a-stick.js | 24 -- 1554-strings-differ-by-one-character.js | 34 -- 1559-detect-cycles-in-2d-grid.js | 55 --- 156-binary-tree-upside-down.js | 13 - 1564-put-boxes-into-the-warehouse-i.js | 41 -- 1570-dot-product-of-two-sparse-vectors.js | 38 -- ...-avoid-consecutive-repeating-characters.js | 27 -- 1586-binary-search-tree-iterator-ii.js | 72 ---- ...maximum-sum-obtained-of-any-permutation.js | 58 --- 1590-make-sum-divisible-by-p.js | 37 -- 1591-strange-printer-ii.js | 75 ---- ...nto-the-max-number-of-unique-substrings.js | 29 -- ...um-cost-to-connect-two-groups-of-points.js | 78 ---- ...y-expression-tree-from-infix-expression.js | 50 --- 160-intersection-of-two-linked-lists.js | 50 --- ...-number-of-achievable-transfer-requests.js | 34 -- ...-valid-matrix-given-row-and-column-sums.js | 19 - ...with-x-elements-greater-than-or-equal-x.js | 52 --- ...ne-bit-operations-to-make-integers-zero.js | 29 -- ...btrees-with-max-distance-between-cities.js | 66 ---- 162-find-peak-element.js | 17 - ...sets-of-k-non-overlapping-line-segments.js | 26 -- ...allest-string-after-applying-operations.js | 50 --- 1627-graph-connectivity-with-threshold.js | 59 --- 163-missing-ranges.js | 27 -- 1631-path-with-minimum-effort.js | 251 ------------ 1632-rank-transform-of-a-matrix.js | 157 -------- ...substrings-that-differ-by-one-character.js | 26 -- ...form-a-target-string-given-a-dictionary.js | 36 -- 1641-count-sorted-vowel-strings.js | 43 -- 1643-kth-smallest-instructions.js | 37 -- ...est-common-ancestor-of-a-binary-tree-ii.js | 37 -- 1648-sell-diminishing-valued-colored-balls.js | 136 ------- ...reate-sorted-array-through-instructions.js | 46 --- ...st-common-ancestor-of-a-binary-tree-iii.js | 24 -- ...nimum-deletions-to-make-string-balanced.js | 64 --- 1654-minimum-jumps-to-reach-home.js | 42 -- 1655-distribute-repeating-integers.js | 171 -------- ...-minimum-operations-to-reduce-x-to-zero.js | 39 -- 1660-correct-a-binary-tree.js | 48 --- 1664-ways-to-make-a-fair-array.js | 29 -- ...-minimum-initial-energy-to-finish-tasks.js | 31 -- 1668-maximum-repeating-substring.js | 29 -- ...mber-of-removals-to-make-mountain-array.js | 28 -- 1673-find-the-most-competitive-subsequence.js | 47 --- ...nimum-moves-to-make-array-complementary.js | 92 ----- ...est-common-ancestor-of-a-binary-tree-iv.js | 28 -- ...atenation-of-consecutive-binary-numbers.js | 18 - 1681-minimum-incompatibility.js | 138 ------- 169-majority-element.js | 36 -- 1691-maximum-height-by-stacking-cuboids.js | 30 -- ...-existence-of-edge-length-limited-paths.js | 60 --- ...mber-of-distinct-substrings-in-a-string.js | 27 -- 1703-maximum-binary-string-after-change.js | 33 -- ...-maximum-xor-with-an-element-from-array.js | 52 --- 171-excel-sheet-column-number.js | 12 - ...inimum-operations-to-make-a-subsequence.js | 117 ------ ...-maximum-score-from-removing-substrings.js | 38 -- 1723-find-minimum-time-to-finish-all-jobs.js | 119 ------ 173-binary-search-tree-iterator.js | 61 --- 1734-decode-xored-permutation.js | 45 --- ...ters-to-satisfy-one-of-three-conditions.js | 65 --- 1738-find-kth-largest-xor-coordinate-value.js | 136 ------- 1739-building-boxes.js | 22 -- 174-dungeon-game.js | 20 - 1740-find-distance-in-a-binary-tree.js | 125 ------ 1745-palindrome-partitioning-iv.js | 51 --- 1749-maximum-absolute-sum-of-any-subarray.js | 31 -- ...umber-of-events-that-can-be-attended-ii.js | 78 ---- 1754-largest-merge-of-two-strings.js | 38 -- 1755-closest-subsequence-sum.js | 77 ---- ...oncatenating-subarrays-of-another-array.js | 36 -- ...perations-to-move-all-balls-to-each-box.js | 48 --- ...ize-palindrome-length-from-subsequences.js | 31 -- 1774-closest-dessert-cost.js | 30 -- ...rrays-with-minimum-number-of-operations.js | 73 ---- 1776-car-fleet-ii.js | 70 ---- ...estricted-paths-from-first-to-last-node.js | 53 --- 1791-find-center-of-star-graph.js | 28 -- 1792-maximum-average-pass-ratio.js | 105 ----- 1793-maximum-score-of-a-good-subarray.js | 22 -- 18-4sum.js | 46 --- ...lue-at-a-given-index-in-a-bounded-array.js | 108 ----- ...perations-to-reinitialize-a-permutation.js | 33 -- ...ber-of-operations-to-make-string-sorted.js | 44 --- 1834-single-threaded-cpu.js | 177 --------- 1835-find-xor-sum-of-all-pairs-bitwise-and.js | 57 --- ...-frequency-of-the-most-frequent-element.js | 20 - ...ongest-substring-of-all-vowels-in-order.js | 58 --- ...ring-into-descending-consecutive-values.js | 25 -- ...-minimum-interval-to-include-each-query.js | 304 --------------- 1854-maximum-population-year.js | 61 --- 1856-maximum-subarray-min-product.js | 96 ----- ...largest-color-value-in-a-directed-graph.js | 90 ----- 1861-rotating-the-box.js | 28 -- ...-rearrange-sticks-with-k-sticks-visible.js | 24 -- 187-repeated-dna-sequences.js | 18 - 1870-minimum-speed-to-arrive-on-time.js | 84 ---- 1871-jump-game-vii.js | 73 ---- 1879-minimum-xor-sum-of-two-arrays.js | 95 ----- 188-best-time-to-buy-and-sell-stock-iv.js | 61 --- 1882-process-tasks-using-servers.js | 327 ---------------- ...s-to-make-the-binary-string-alternating.js | 67 ---- 1889-minimum-space-wasted-from-packaging.js | 51 --- 189-rotate-array.js | 28 -- ...all-the-integers-in-a-range-are-covered.js | 43 -- ...-maximum-number-of-removable-characters.js | 30 -- 190-reverse-bits.js | 31 -- ...e-number-of-full-rounds-you-have-played.js | 33 -- 191-number-of-1-bits.js | 14 - 1911-maximum-alternating-subsequence-sum.js | 13 - 1914-cyclically-rotating-a-gridsubmissions.js | 67 ---- 1915-number-of-wonderful-substrings.js | 22 -- ...nique-length-3-palindromic-subsequences.js | 27 -- ...ting-a-grid-with-three-different-colors.js | 97 ----- 1937-maximum-number-of-points-with-cost.js | 40 -- ...number-of-the-smallest-unoccupied-chair.js | 155 -------- 1943-describe-the-painting.js | 30 -- 1944-number-of-visible-people-in-a-queue.js | 48 --- 1947-maximum-compatibility-score-sum.js | 75 ---- ...-number-of-weeks-for-which-you-can-work.js | 36 -- 1955-count-number-of-special-subsequences.js | 38 -- ...er-of-swaps-to-make-the-string-balanced.js | 45 --- ...-valid-obstacle-course-at-each-position.js | 81 ---- ...-non-zero-product-of-the-array-elements.js | 28 -- ...number-of-ways-to-arrive-at-destination.js | 111 ------ 198-house-robber.js | 17 - ...ence-between-target-and-chosen-elements.js | 26 -- 1982-find-array-given-subset-sums.js | 90 ----- ...er-of-work-sessions-to-finish-the-tasks.js | 64 --- 1987-number-of-unique-good-subsequences.js | 21 - 199-binary-tree-right-side-view.js | 17 - ...e-number-of-weak-characters-in-the-game.js | 66 ---- ...ay-where-you-have-been-in-all-the-rooms.js | 17 - 1998-gcd-sort-of-an-array.js | 68 ---- 2-add-two-numbers.js | 132 ------- 20-valid-parentheses.js | 57 --- ...-length-of-two-palindromic-subsequences.js | 43 -- ...-find-original-array-from-doubled-array.js | 27 -- 2008-maximum-earnings-from-taxi.js | 30 -- 201-bitwise-and-of-numbers-range.js | 16 - 2013-detect-squares.js | 81 ---- 2014-longest-subsequence-repeated-k-times.js | 85 ---- 2024-maximize-the-confusion-of-an-exam.js | 25 -- ...um-number-of-ways-to-partition-an-array.js | 51 --- 203-remove-linked-list-elements.js | 19 - ...mum-operations-to-make-a-uni-value-grid.js | 107 ----- 204-count-primes.js | 109 ------ ...econd-minimum-time-to-reach-destination.js | 42 -- 2049-count-nodes-with-the-highest-score.js | 31 -- 2050-parallel-courses-iii.js | 77 ---- 2054-two-best-non-overlapping-events.js | 87 ----- 2055-plates-between-candles.js | 83 ---- 206-reverse-linked-list.js | 28 -- ...string-exists-given-two-encoded-strings.js | 124 ------ ...um-of-products-distributed-to-any-store.js | 52 --- 2065-maximum-path-quality-of-a-graph.js | 81 ---- 207-course-schedule.js | 152 -------- 208-implement-trie-prefix-tree.js | 98 ----- 2092-find-all-people-with-secret.js | 45 --- 21-merge-two-sorted-lists.js | 33 -- 210-course-schedule-ii.js | 71 ---- 2101-detonate-the-maximum-bombs.js | 43 -- 2102-sequentially-ordinal-rank-tracker.js | 226 ----------- 2104-sum-of-subarray-ranges.js | 18 - ...erations-to-make-the-array-k-increasing.js | 46 --- ...ll-possible-recipes-from-given-supplies.js | 54 --- ...ck-if-a-parentheses-string-can-be-valid.js | 23 -- ...um-employees-to-be-invited-to-a-meeting.js | 105 ----- 213-house-robber-ii.js | 44 --- 2130-maximum-twin-sum-of-a-linked-list.js | 29 -- 2136-earliest-possible-day-of-full-bloom.js | 39 -- 214-shortest-palindrome.js | 72 ---- 2140-solving-questions-with-brainpower.js | 22 -- 2141-maximum-running-time-of-n-computers.js | 40 -- 215-kth-largest-element-in-an-array.js | 113 ------ ...rence-in-sums-after-removal-of-elements.js | 113 ------ 2166-design-bitset.js | 90 ----- ...emove-all-cars-containing-illegal-goods.js | 103 ----- 2183-count-array-pairs-divisible-by-k.js | 30 -- ...mber-of-ways-to-build-sturdy-brick-wall.js | 51 --- 2188-minimum-time-to-finish-the-race.js | 43 -- ...s-of-a-node-in-a-directed-acyclic-graph.js | 34 -- 220-contains-duplicate-iii.js | 24 -- 221-maximal-square.js | 24 -- 2211-count-collisions-on-a-road.js | 14 - ...nimum-deletions-to-make-array-beautiful.js | 20 - 2218-maximum-value-of-k-coins-from-piles.js | 29 -- 222-count-complete-tree-nodes.js | 13 - ...maximum-candies-allocated-to-k-children.js | 19 - 223-rectangle-area.js | 48 --- 224-basic-calculator.js | 88 ----- ...ximum-trailing-zeros-in-a-cornered-path.js | 59 --- ...path-with-different-adjacent-characters.js | 33 -- ...ber-of-rectangles-containing-each-point.js | 49 --- 226-invert-binary-tree.js | 53 --- 2262-total-appeal-of-a-string.js | 20 - ...here-is-a-valid-parentheses-string-path.js | 45 --- 227-basic-calculator-ii.js | 64 --- ...maximum-white-tiles-covered-by-a-carpet.js | 30 -- ...tion-with-bitwise-and-greater-than-zero.js | 25 -- 2276-count-integers-in-intervals.js | 160 -------- 228-summary-ranges.js | 21 - ...minimum-lines-to-represent-a-line-chart.js | 31 -- 2289-steps-to-make-array-non-decreasing.js | 50 --- 2296-design-a-text-editor.js | 76 ---- 23-merge-k-sorted-lists.js | 143 ------- 231-power-of-two.js | 26 -- 2312-selling-pieces-of-wood.js | 26 -- 2318-number-of-distinct-roll-sequences.js | 38 -- 2320-count-number-of-ways-to-place-houses.js | 19 - 2321-maximum-score-of-spliced-array.js | 30 -- ...-minimum-score-after-removals-on-a-tree.js | 62 --- 2327-number-of-people-aware-of-a-secret.js | 50 --- 2328-number-of-increasing-paths-in-a-grid.js | 50 --- 233-number-of-digit-one.js | 95 ----- ...nimum-deletions-to-make-array-divisible.js | 35 -- ...common-ancestor-of-a-binary-search-tree.js | 21 - 2354-number-of-excellent-pairs.js | 42 -- ...lowest-common-ancestor-of-a-binary-tree.js | 32 -- 2370-longest-ideal-subsequence.js | 20 - 2376-count-special-integers.js | 32 -- ...ime-needed-to-rearrange-a-binary-string.js | 17 - 2386-find-the-k-sum-of-an-array.js | 98 ----- 239-sliding-window-maximum.js | 66 ---- 2392-build-a-matrix-with-conditions.js | 74 ---- ...-reach-a-position-after-exactly-k-steps.js | 38 -- 2401-longest-nice-subarray.js | 32 -- 2402-meeting-rooms-iii.js | 42 -- ...intervals-into-minimum-number-of-groups.js | 27 -- 2430-maximum-deletions-on-a-string.js | 64 --- ...t-the-lexicographically-smallest-string.js | 35 -- 2444-count-subarrays-with-fixed-bounds.js | 21 - 246-strobogrammatic-number.js | 19 - ...sum-of-distinct-subarrays-with-length-k.js | 41 -- 247-strobogrammatic-number-ii.js | 23 -- ...imum-score-of-a-path-between-two-cities.js | 58 --- ...nodes-into-the-maximum-number-of-groups.js | 100 ----- 25-reverse-nodes-in-k-group.js | 126 ------ 251-flatten-2d-vector.js | 34 -- 253-meeting-rooms-ii.js | 125 ------ 2537-count-the-number-of-good-subarrays.js | 23 -- ...-minimum-score-by-changing-two-elements.js | 15 - ...um-operations-to-reduce-an-integer-to-0.js | 25 -- ...nd-the-maximum-number-of-marked-indices.js | 14 - 258-add-digits.js | 12 - ...plit-the-array-to-make-coprime-products.js | 68 ---- 260-single-number-III.js | 34 -- 261-graph-valid-tree.js | 37 -- 2653-sliding-subarray-beauty.js | 25 -- ...cographically-smallest-beautiful-string.js | 44 --- 268-missing-number.js | 26 -- 270-closest-binary-search-tree-value.js | 37 -- 274-h-index.js | 30 -- 275-h-index-ii.js | 22 -- 2762-continuous-subarrays.js | 71 ---- 277-find-the-celebrity.js | 37 -- 279-perfect-squares.js | 61 --- 28-implement-strStr().js | 21 - 281-zigzag-iterator.js | 99 ----- 285-inorder-successor-in-bst.js | 37 -- 286-walls-and-gates.js | 41 -- 287-find-the-duplicate-number.js | 255 ------------ 291-word-pattern-ii.js | 37 -- 295-find-median-from-data-stream.js | 103 ----- 296-best-meeting-point.js | 41 -- 299-bulls-and-cows.js | 31 -- ...-substring-without-repeating-characters.js | 27 -- ...bstring-with-concatenation-of-all-words.js | 61 --- 300-longest-increasing-subsequence.js | 79 ---- 301-remove-invalid-parentheses.js | 37 -- ...allest-rectangle-enclosing-black-pixels.js | 38 -- 304-range-sum-query-2d-immutable.js | 33 -- 307-range-sum-query-mutable.js | 37 -- 31-next-permutation.js | 76 ---- 310-minimum-height-trees.js | 78 ---- 312-burst-balloons.js | 30 -- 313-super-ugly-number.js | 91 ----- 314-binary-tree-vertical-order-traversal.js | 21 - 315-count-of-smaller-numbers-after-self.js | 96 ----- 316-remove-duplicate-letters.js | 56 --- 32-longest-valid-parentheses.js | 56 --- 320-generalized-abbreviation.js | 21 - 322-coin-change.js | 24 -- 326-power-of-three.js | 20 - 328-odd-even-linked-list.js | 36 -- 329-longest-increasing-path-in-a-matrix.js | 46 --- 33-search-in-rotated-sorted-array.js | 42 -- 333-largest-bst-subtree.js | 18 - 334-increasing-triplet-subsequence.js | 41 -- 336-palindrome-pairs.js | 53 --- 339-nested-list-weight-sum.js | 18 - 343-integer-break.js | 65 --- 344-reverse-string.js | 11 - 345-reverse-vowels-of-a-string.js | 12 - 347-top-k-frequent-elements.js | 66 ---- 35-search-insert-position.js | 18 - 351-android-unlock-patterns.js | 39 -- 354-russian-doll-envelopes.js | 57 --- 357-count-numbers-with-unique-digits.js | 64 --- 358-rearrange-string-k-distance-apart.js | 104 ----- 36-valid-sudoku.js | 29 -- 360-sort-transformed-array.js | 52 --- 361-bomb-enemy.js | 62 --- 364-nested-list-weight-sum-ii.js | 30 -- 366-find-leaves-of-binary-tree.js | 15 - 368-largest-divisible-subset.js | 33 -- 369-plus-one-linked-list.js | 25 -- 37-sudoku-solver.js | 34 -- 373-find-k-pairs-with-smallest-sums.js | 36 -- 376-wiggle-subsequence.js | 20 - 386-lexicographical-numbers.js | 38 -- 387-first-unique-character-in-a-string.js | 19 - 389-find-the-difference.js | 21 - 392-is-subsequence.js | 30 -- 397-integer-replacement.js | 15 - 4-median-of-two-sorted-arrays.js | 98 ----- 402-remove-k-digits.js | 47 --- 406-queue-reconstruction-by-height.js | 31 -- 407-trapping-rain-water-ii.js | 217 ---------- 408-valid-word-abbreviation.js | 28 -- 409-longest-palindrome.js | 31 -- 41-first-missing-positive.js | 81 ---- 410-split-array-largest-sum.js | 39 -- 416-partition-equal-subset-sum.js | 77 ---- 42-trapping-rain-water.js | 95 ----- 421-maximum-xor-of-two-numbers-in-an-array.js | 114 ------ ...longest-repeating-character-replacement.js | 29 -- 425-word-squares.js | 75 ---- ...earch-tree-to-sorted-doubly-linked-list.js | 62 --- 428-serialize-and-deserialize-n-ary-tree.js | 63 --- 429-n-ary-tree-level-order-traversal.js | 30 -- 433-minimum-genetic-mutation.js | 28 -- 435-non-overlapping-intervals.js | 26 -- 440-k-th-smallest-in-lexicographical-order.js | 32 -- 441-arranging-coins.js | 9 - 442-find-all-duplicates-in-an-array.js | 15 - 444-sequence-reconstruction.js | 41 -- 445-add-two-numbers-II.js | 96 ----- ...ind-all-numbers-disappeared-in-an-array.js | 17 - 450-delete-node-in-a-bst.js | 41 -- ...imum-number-of-arrows-to-burst-balloons.js | 18 - 456-132-pattern.js | 19 - 458-poor-pigs.js | 15 - 459-repeated-substring-pattern.js | 75 ---- 46-permutations.js | 24 -- 461-hamming-distance.js | 28 -- 463-island-perimeter.js | 29 -- 468-validate-ip-address.js | 46 --- 47-permutations-ii.js | 31 -- 470-implement-rand10-using-rand7.js | 13 - 472-concatenated-words.js | 104 ----- 473-matchsticks-to-square.js | 33 -- 475-heaters.js | 43 -- 479-largest-palindrome-product.js | 58 --- 48-rotate-image.js | 42 -- 480-sliding-window-median.js | 42 -- 484-find-permutation.js | 52 --- 489-robot-room-cleaner.js | 35 -- 49-group-anagrams.js | 25 -- 490-the-maze.js | 122 ------ 493-reverse-pairs.js | 68 ---- 494-target-sum.js | 20 - 496-next-greater-element-I.js | 27 -- ...dom-point-in-non-overlapping-rectangles.js | 43 -- 498-diagonal-traverse.js | 42 -- 499-the-maze-iii.js | 115 ------ 5-longest-palindromic-substring.js | 88 ----- 50-powx-n.js | 64 --- 501-find-mode-in-binary-search-tree.js | 23 -- 502-ipo.js | 122 ------ 503-next-greater-element-II.js | 45 --- 504-base-7.js | 5 - 51-n-queens.js | 29 -- 516-longest-palindromic-subsequence.js | 44 --- 518-coin-change-2.js | 43 -- ...est-word-in-dictionary-through-deleting.js | 117 ------ 525-contiguous-array.js | 27 -- 528-random-pick-with-weight.js | 72 ---- 53-maximum-subarray.js | 62 --- 531-lonely-pixel-i.js | 28 -- 533-lonely-pixel-ii.js | 45 --- 538-convert-bst-to-greater-tree.js | 40 +- 54-spiral-matrix.js | 44 --- 540-single-element-in-a-sorted-array.js | 50 --- 543-diameter-of-binary-tree.js | 54 --- 544-output-contest-matches.js | 23 -- 55-jump-game.js | 15 - 56-merge-intervals.js | 68 ---- 560-subarray-sum-equals-k.js | 19 - 567-permutation-in-string.js | 31 -- 573-squirrel-simulation.js | 23 -- 576-out-of-boundary-paths.js | 68 ---- 582-kill-process.js | 29 -- 583-delete-operation-for-two-strings.js | 45 --- 588-design-in-memory-file-system.js | 73 ---- 591-tag-validator.js | 158 -------- 60-permutation-sequence.js | 111 ------ 61-rotate-list.js | 33 -- 62-unique-paths.js | 37 -- 621-task-scheduler.js | 72 ---- 623-add-one-row-to-tree.js | 34 -- 628-maximum-product-of-three-numbers.js | 33 -- 630-course-schedule-iii.js | 232 ----------- 631-design-excel-sum-formula.js | 86 ---- 632-smallest-range.js | 194 --------- 634-find-the-derangement-of-an-array.js | 16 - 642-design-search-autocomplete-system.js | 150 ------- 645-set-mismatch.js | 18 - 646-maximum-length-of-pair-chain.js | 40 -- 647-palindromic-substrings.js | 31 -- 651-4-keys-keyboard.js | 36 -- 652-find-duplicate-subtrees.js | 31 -- 658-find-k-closest-elements.js | 23 -- ...lit-array-into-consecutive-subsequences.js | 27 -- 660-remove-9.js | 9 - 664-strange-printer.js | 22 -- 666-path-sum-iv.js | 54 --- ...smallest-number-in-multiplication-table.js | 29 -- 67-add-binary.js | 24 -- 671-second-minimum-node-in-a-binary-tree.js | 34 -- 678-valid-parenthesis-string.js | 22 -- 679-24-game.js | 36 -- 68-text-justification.js | 46 --- 687-longest-univalue-path.js | 38 -- 69-sqrt(x).js | 30 -- 691-stickers-to-spell-word.js | 63 --- 694-number-of-distinct-islands.js | 30 -- 699-falling-squares.js | 54 --- 7-reverse-integer.js | 23 -- 70-climbing-stairs.js | 22 -- 701-insert-into-a-binary-search-tree.js | 35 -- 707-design-linked-list.js | 126 ------ 715-range-module.js | 286 -------------- 719-find-k-th-smallest-pair-distance.js | 25 -- 72-edit-distance.js | 28 -- 723-candy-crush.js | 54 --- 727-minimum-window-subsequence.js | 128 ------ 729-my-calendar-i.js | 24 -- 732-my-calendar-iii.js | 76 ---- 733-flood-fill.js | 43 -- 735-asteroid-collision.js | 34 -- 736-parse-lisp-expression.js | 85 ---- 743-network-delay-time.js | 152 -------- 748-shortest-completing-word.js | 37 -- 749-contain-virus.js | 180 --------- 750-number-of-corner-rectangles.js | 36 -- 755-pour-water.js | 28 -- 757-set-intersection-size-at-least-two.js | 59 --- 759-employee-free-time.js | 87 ----- 765-couples-holding-hands.js | 79 ---- 767-reorganize-string.js | 79 ---- 774-minimize-max-distance-to-gas-station.js | 101 ----- 775-global-and-local-inversions.js | 13 - 778-swim-in-rising-water.js | 83 ---- 78-subsets.js | 18 - 787-cheapest-flights-within-k-stops.js | 25 -- 79-word-search.js | 39 -- 792-number-of-matching-subsequences.js | 56 --- ...umber-of-subarrays-with-bounded-maximum.js | 23 -- 798-smallest-rotation-with-highest-score.js | 21 - 802-find-eventual-safe-states.js | 56 --- 803-bricks-falling-when-hit.js | 184 --------- 828-unique-letter-string.js | 41 -- 829-consecutive-numbers-sum.js | 17 - 83-remove-duplicates-from-sorted-list.js | 27 -- 84-largest-rectangle-in-histogram.js | 43 -- 85-maximal-rectangle.js | 78 ---- 857-minimum-cost-to-hire-k-workers.js | 67 ---- 86-partition-list.js | 47 --- 861-score-after-flipping-matrix.js | 48 --- 862-shortest-subarray-with-sum-at-least-k.js | 29 -- 863-all-nodes-distance-k-in-binary-tree.js | 48 --- ...lest-subtree-with-all-the-deepest-nodes.js | 33 -- 866-prime-palindrome.js | 50 --- 878-nth-magical-number.js | 18 - 882-reachable-nodes-in-subdivided-graph.js | 219 ----------- 886-possible-bipartition.js | 35 -- 887-super-egg-drop.js | 36 -- 89-gray-code.js | 31 -- 893-groups-of-special-equivalent-strings.js | 18 - 896-monotonic-array.js | 14 - 902-numbers-at-most-n-given-digit-set.js | 29 -- 903-valid-permutations-for-di-sequence.js | 52 --- 906-super-palindromes.js | 87 ----- 907-sum-of-subarray-minimums.js | 39 -- 91-decode-ways.js | 22 -- 92-reverse-linked-list-ii.js | 198 ---------- 926-flip-string-to-monotone-increasing.js | 26 -- 928-minimize-malware-spread-ii.js | 50 --- 93-restore-ip-addresses.js | 29 -- 930-binary-subarrays-with-sum.js | 23 -- 931-minimum-falling-path-sum.js | 23 -- 937-reorder-data-in-log-files.js | 36 -- 939-minimum-area-rectangle.js | 22 -- 940-distinct-subsequences-ii.js | 32 -- 942-di-string-match.js | 28 -- 945-minimum-increment-to-make-array-unique.js | 16 - ...largest-component-size-by-common-factor.js | 64 --- 955-delete-columns-to-make-sorted-ii.js | 29 -- 96-unique-binary-search-trees.js | 25 -- 960-delete-columns-to-make-sorted-iii.js | 19 - 964-least-operators-to-express-number.js | 25 -- 968-binary-tree-cameras.js | 32 -- 973-k-closest-points-to-origin.js | 125 ------ 974-subarray-sums-divisible-by-k.js | 18 - 981-time-based-key-value-store.js | 49 --- 984-string-without-aaa-or-bbb.js | 50 --- 985-sum-of-even-numbers-after-queries.js | 15 - 988-smallest-string-starting-from-leaf.js | 11 - 99-recover-binary-search-tree.js | 47 --- 990-satisfiability-of-equality-equations.js | 38 -- 992-subarrays-with-k-different-integers.js | 53 --- 993.cousins-in-binary-tree.js | 48 --- 994-rotting-oranges.js | 47 --- ...nimum-number-of-k-consecutive-bit-flips.js | 48 --- 997-find-the-town-judge.js | 24 -- clean-dataset.py | 15 + 654 files changed, 21 insertions(+), 34070 deletions(-) create mode 100644 clean-dataset.py diff --git a/100-same-tree.js b/100-same-tree.js index 6cdcd4b5..bdab1712 100755 --- a/100-same-tree.js +++ b/100-same-tree.js @@ -17,23 +17,3 @@ const isSameTree = function(p, q) { return isSameTree(p.left, q.left) && isSameTree(p.right, q.right) }; -// another - -/** - * Definition for a binary tree node. - * function TreeNode(val, left, right) { - * this.val = (val===undefined ? 0 : val) - * this.left = (left===undefined ? null : left) - * this.right = (right===undefined ? null : right) - * } - */ -/** - * @param {TreeNode} p - * @param {TreeNode} q - * @return {boolean} - */ -const isSameTree = function(p, q) { - if(p == null || q == null) return p === q - if(p.val !== q.val) return false - return isSameTree(p.left, q.left) && isSameTree(p.right, q.right) -}; diff --git a/1009-complement-of-base-10-integer.js b/1009-complement-of-base-10-integer.js index f88696bd..9f36835b 100644 --- a/1009-complement-of-base-10-integer.js +++ b/1009-complement-of-base-10-integer.js @@ -15,30 +15,3 @@ const bitwiseComplement = function (N) { return bitmask ^ N } -// another - -/** - * @param {number} N - * @return {number} - */ -const bitwiseComplement = function (N) { - let X = 1; - while (N > X) X = X * 2 + 1; - return N ^ X; -} - -// another - -/** - * @param {number} N - * @return {number} - */ -const bitwiseComplement = function (N) { - if (N === 0) return 1 - // l is a length of N in binary representation - const l = Math.floor(Math.log(N) / Math.log(2)) + 1 - // bitmask has the same length as num and contains only ones 1...1 - const bitmask = (1 << l) - 1 - // flip all bits - return bitmask ^ N -} diff --git a/1017-convert-to-base-2.js b/1017-convert-to-base-2.js index 044313c4..1df3eebb 100644 --- a/1017-convert-to-base-2.js +++ b/1017-convert-to-base-2.js @@ -21,18 +21,3 @@ function negBase(val, base) { return result; } -// another - -/** - * @param {number} N - * @return {string} - */ -const baseNeg2 = function(N) { - if (N === 0) return "0"; - let res = '' - while(N !== 0) { - res = (N & 1) + res - N = -(N >> 1) - } - return res; -}; diff --git a/1021-best-sightseeing-pair.js b/1021-best-sightseeing-pair.js index d5c4fbeb..ea15cc77 100644 --- a/1021-best-sightseeing-pair.js +++ b/1021-best-sightseeing-pair.js @@ -12,20 +12,3 @@ const maxScoreSightseeingPair = function(A) { }; -// another - -/** - * @param {number[]} A - * @return {number} - */ -const maxScoreSightseeingPair = function(A) { - let ans =A[0]; - let prevBestIdx =0; - for(let j=1;j a[0] - b[0]) - let res = 0 - for(let i = 0, start = 0, end = 0, len = clips.length; start < T; start = end, res++) { - for(; i < len && clips[i][0] <= start; i++) { - end = Math.max(end, clips[i][1]) - } - if(start === end) return -1 - } - return res -} - - -// another - -/** - * @param {number[][]} clips - * @param {number} T - * @return {number} - */ -const videoStitching = function (clips, T) { - const dp = Array(T + 1).fill( T + 1 ) - dp[0] = 0 - for(let i = 0; i <= T; i++) { - for(let c of clips) { - if(i >= c[0] && i <= c[1]) dp[i] = Math.min(dp[i], dp[c[0]] + 1) - } - if(dp[i] === T + 1) return -1 - } - return dp[T] -} - - diff --git a/1029-two-city-scheduling.js b/1029-two-city-scheduling.js index d973fd13..3262d051 100644 --- a/1029-two-city-scheduling.js +++ b/1029-two-city-scheduling.js @@ -22,45 +22,3 @@ const twoCitySchedCost = function(costs) { return dp[N][N] } -// another - -/** - * @param {number[][]} costs - * @return {number} - */ -const twoCitySchedCost = function(costs) { - const N = costs.length - let res = 0 - const refund = [] - for(let i = 0; i < N; i++) { - refund[i] = costs[i][1] - costs[i][0] - res += costs[i][0] - } - refund.sort((a, b) => a - b) - for(let i = 0; i < N / 2; i++) { - res += refund[i] - } - return res -}; - -// another - -/** - * @param {number[][]} costs - * @return {number} - */ -const twoCitySchedCost = function(costs) { - const len = costs.length - if(len === 0) return 0 - const N = len / 2 - costs.sort((a, b) => (a[0] - a[1]) - (b[0] - b[1])) - let res = 0 - for(let i = 0; i < costs.length; i++) { - if(i < N) { - res += costs[i][0] - } else { - res += costs[i][1] - } - } - return res -}; diff --git a/103-binary-tree-zigzag-level-order-traversal.js b/103-binary-tree-zigzag-level-order-traversal.js index 54ff515f..9aef7c60 100644 --- a/103-binary-tree-zigzag-level-order-traversal.js +++ b/103-binary-tree-zigzag-level-order-traversal.js @@ -39,70 +39,3 @@ function bfs(row, res) { bfs(next, res) } -// another - -/** - * Definition for a binary tree node. - * function TreeNode(val, left, right) { - * this.val = (val===undefined ? 0 : val) - * this.left = (left===undefined ? null : left) - * this.right = (right===undefined ? null : right) - * } - */ -/** - * @param {TreeNode} root - * @return {number[][]} - */ -const zigzagLevelOrder = function (root) { - if (!root) return []; - const queue = [root]; - const zigzag = []; - let numLevels = 1; - while (queue.length > 0) { - const width = queue.length; - const levelTraversal = []; - for (let i = 0; i < width; i++) { - const currentNode = queue.shift(); - if (currentNode.right) queue.push(currentNode.right); - if (currentNode.left) queue.push(currentNode.left); - numLevels % 2 === 0 - ? levelTraversal.push(currentNode.val) - : levelTraversal.unshift(currentNode.val); - } - zigzag.push(levelTraversal); - numLevels++; - } - - return zigzag; -}; - -// another - -/** - * Definition for a binary tree node. - * function TreeNode(val, left, right) { - * this.val = (val===undefined ? 0 : val) - * this.left = (left===undefined ? null : left) - * this.right = (right===undefined ? null : right) - * } - */ -/** - * @param {TreeNode} root - * @return {number[][]} - */ -const zigzagLevelOrder = function (root) { - const res = [] - dfs(root, res, 0) - return res - - function dfs(node, res, level) { - if(node == null) return - if(res.length <= level) res.push([]) - const tmp = res[level] - if(level % 2 === 0) tmp.push(node.val) - else tmp.unshift(node.val) - - dfs(node.left, res, level + 1) - dfs(node.right, res, level + 1) - } -}; diff --git a/1031-maximum-sum-of-two-non-overlapping-subarrays.js b/1031-maximum-sum-of-two-non-overlapping-subarrays.js index 37f3e01a..09b2e632 100644 --- a/1031-maximum-sum-of-two-non-overlapping-subarrays.js +++ b/1031-maximum-sum-of-two-non-overlapping-subarrays.js @@ -17,27 +17,3 @@ const maxSumTwoNoOverlap = function(A, L, M) { return res } -// another - -const maxSumTwoNoOverlap = function(A, L, M) { - let n = A.length - let sum = [] - sum[0] = 0 - for (let i = 0; i < n; i++) sum[i + 1] = sum[i] + A[i] - - let ans = 0 - for (let i = L - 1; i + M < n; ++i) { - for (let j = i + 1; j + M - 1 < n; ++j) { - ans = Math.max(ans, sum[i + 1] - sum[i - L + 1] + sum[j + M] - sum[j]) - } - } - let tmp = L - L = M - M = tmp - for (let i = L - 1; i + M < n; ++i) { - for (let j = i + 1; j + M - 1 < n; ++j) { - ans = Math.max(ans, sum[i + 1] - sum[i - L + 1] + sum[j + M] - sum[j]) - } - } - return ans -} diff --git a/1035-uncrossed-lines.js b/1035-uncrossed-lines.js index 7fbc58e9..702ea96f 100644 --- a/1035-uncrossed-lines.js +++ b/1035-uncrossed-lines.js @@ -20,23 +20,3 @@ const maxUncrossedLines = function(A, B) { }; -// another - -const maxUncrossedLines = function(A, B) { - const m = A.length, - n = B.length - const dp = new Array(n + 1).fill(0) - let prev - - for (let i = 1; i <= m; i++) { - prev = dp[0] - for (let j = 1; j <= n; j++) { - let backup = dp[j] - if (A[i - 1] == B[j - 1]) dp[j] = prev + 1 - else dp[j] = Math.max(dp[j], dp[j - 1]) - prev = backup - } - } - - return dp[n] -} diff --git a/1036-escape-a-large-maze.js b/1036-escape-a-large-maze.js index 91b0e04b..a273aeec 100644 --- a/1036-escape-a-large-maze.js +++ b/1036-escape-a-large-maze.js @@ -41,59 +41,3 @@ function dfs(blocked, i, j, m, n, visited) { return false } -// another - -/** - * @param {number[][]} blocked - * @param {number[]} source - * @param {number[]} target - * @return {boolean} - */ -const isEscapePossible = function(blocked, source, target) { - if (blocked.length < 2) { - return true - } -// if (blocked[0][0] == 100025) { -// return false -// } - const blockSet = new Set( - blocked.map(el => { - return el[0] + "," + el[1] - }) - ) - let targetR, targetC, curR, curC - ;[targetR, targetC] = target - let visited = new Set([]) - let DIRS = [[0, 1], [-1, 0], [0, -1], [1, 0]], - queue = [source] - const inBound = (r, c) => { - return r >= 0 && c >= 0 && r < 1000000 && c < 1000000 - } - let count = 0 - while (queue.length > 0) { - count++ - ;[curR, curC] = queue.shift() - - if (count > 20000) { - return true - } - for (let dir of DIRS) { - const newR = curR + dir[0], - newC = curC + dir[1] - if ( - !inBound(newR, newC) || - blockSet.has(newR + "," + newC) || - visited.has(newR + "," + newC) - ) { - continue - } - - if (newR == targetR && newC == targetC) { - return true - } - visited.add(newR + "," + newC) - queue.push([newR, newC]) - } - } - return false -} diff --git a/1041-robot-bounded-in-circle.js b/1041-robot-bounded-in-circle.js index 65146881..b2d29647 100644 --- a/1041-robot-bounded-in-circle.js +++ b/1041-robot-bounded-in-circle.js @@ -13,47 +13,3 @@ const isRobotBounded = function(instructions) { return x == 0 && y == 0 || i > 0; }; -// another - -/** - * @param {string} instructions - * @return {boolean} - */ -const isRobotBounded = function(instructions) { - let x = 0, y = 0, i = 0 - const dirs = [[0, 1], [1, 0], [0, -1], [-1, 0]] // U, R, D, L - for(let e of instructions) { - if(e === 'R') { - i = (i + 1) % 4 - } else if(e === 'L') { - i = (i + 3) % 4 - } else { - x += dirs[i][0] - y += dirs[i][1] - } - } - return x === 0 && y === 0 || i > 0 -}; - -// another - -/** - * @param {string} instructions - * @return {boolean} - */ -const isRobotBounded = function(instructions) { - const dirs = [[0, 1], [1, 0], [0, -1], [-1, 0]] - let x = 0, y = 0, i = 0 - for(let ins of instructions) { - if(ins === 'G') { - const dir = dirs[i] - x += dir[0] - y += dir[1] - } else if(ins === 'L') { - i = i - 1 < 0 ? 3 : i - 1 - } else if(ins === 'R') { - i = i + 1 > 3 ? 0 : i + 1 - } - } - return x === 0 && y === 0 || i !== 0 -}; diff --git a/1043-partition-array-for-maximum-sum.js b/1043-partition-array-for-maximum-sum.js index adec52a9..3de2443a 100644 --- a/1043-partition-array-for-maximum-sum.js +++ b/1043-partition-array-for-maximum-sum.js @@ -16,28 +16,3 @@ const maxSumAfterPartitioning = function(A, K) { return dp[N - 1]; }; -// another - -/** - * @param {number[]} arr - * @param {number} k - * @return {number} - */ -const maxSumAfterPartitioning = function(arr, k) { - const n = arr.length, memo = Array(n + 1) - memo[0] = 0 - return dp(n) - - function dp(i) { - if(i === 0) return 0 - if(memo[i] != null) return memo[i] - - let sum = 0, max = 0, res = 0 - for(let j = i; j > 0 && i - j < k; j--) { - max = Math.max(max, arr[j - 1]) - sum = (i - j + 1) * max - res = Math.max(res, dp(j - 1) + sum) - } - return memo[i] = res - } -}; diff --git a/1044-longest-duplicate-substring.js b/1044-longest-duplicate-substring.js index 9403a04b..29997216 100644 --- a/1044-longest-duplicate-substring.js +++ b/1044-longest-duplicate-substring.js @@ -29,60 +29,3 @@ function valid(s, len) { return [false, ''] } -// another - -/** - * @param {string} S - * @return {string} - */ -const longestDupSubstring = function(S) { - const R = 26, - MOD = 1e9 + 7 - let lo = 0, - hi = S.length - 1, - res = '' - while (lo < hi) { - const len = Math.ceil((lo + hi) / 2) - const sub = rabinKarp(S, len) - if (sub !== '') { - lo = len - res = sub - } else { - hi = len - 1 - } - } - return res - - function rabinKarp(str, len) { - const aCode = ('a').charCodeAt(0) - let RM = 1 - // 等价于RM=Math.pow(R,M-1) % MOD - // 由于JS精度问题拆解计算 - for (let i = 1; i < len; i++) { - RM = (RM * R) % MOD - } - const map = new Map() - let num = 0 - // 计算前len个字符串的散列值 - for (let i = 0; i < len; i++) { - const code = str.charCodeAt(i) - aCode - num = (num * R + code) % MOD - } - map.set(num, 0) - // 后续计算散列值 - for (let i = 0; i < str.length - len; i++) { - const preCode = str.charCodeAt(i) - aCode, - curCode = str.charCodeAt(i + len) - aCode - num = (num + MOD - ((preCode * RM) % MOD)) % MOD - num = (num * R + curCode) % MOD - if (map.has(num)) { - const sub = str.substring(i + 1, i + 1 + len) - const preId = map.get(num), - preSub = str.substring(preId, preId + len) - if (sub === preSub) return sub - } - map.set(num, i + 1) - } - return '' - } -} diff --git a/105-construct-binary-tree-from-preorder-and-inorder-traversal.js b/105-construct-binary-tree-from-preorder-and-inorder-traversal.js index beebb4af..e248cfb6 100644 --- a/105-construct-binary-tree-from-preorder-and-inorder-traversal.js +++ b/105-construct-binary-tree-from-preorder-and-inorder-traversal.js @@ -28,42 +28,3 @@ const buildTree = function(preorder, inorder) { }; -// another - - -/** - * Definition for a binary tree node. - * function TreeNode(val) { - * this.val = val; - * this.left = this.right = null; - * } - */ -/** - * @param {number[]} preorder - * @param {number[]} inorder - * @return {TreeNode} - */ -let pre -let ins -let inmap = {} -const buildTree = function(preorder, inorder) { - pre = preorder - ins = inorder - for(let i = 0; i < inorder.length; i++) { - inmap[inorder[i]] = i - } - let root = helper(0,0,ins.length - 1) - return root -}; - -function helper(preStart, inStart, inEnd) { - if (preStart > pre.length -1 || inStart > inEnd) { - return null - } - let val = pre[preStart] - let root = new TreeNode(val) - let inIndex = inmap[val] - root.left = helper(preStart + 1, inStart, inIndex - 1) - root.right = helper(preStart+inIndex-inStart+1, inIndex+1, inEnd) - return root -} diff --git a/1052-grumpy-bookstore-owner.js b/1052-grumpy-bookstore-owner.js index 977d6c8b..d73256d6 100644 --- a/1052-grumpy-bookstore-owner.js +++ b/1052-grumpy-bookstore-owner.js @@ -25,27 +25,3 @@ const maxSatisfied = function(customers, grumpy, X) { return totalSatisfiedCustomers + max } -// another - -/** - * @param {number[]} customers - * @param {number[]} grumpy - * @param {number} X - * @return {number} - */ -const maxSatisfied = function (customers, grumpy, X) { - let satisfied = 0, - maxMakeSatisfied = 0 - for (let i = 0, winOfMakeSatisfied = 0; i < grumpy.length; ++i) { - if (grumpy[i] === 0) { - satisfied += customers[i] - } else { - winOfMakeSatisfied += customers[i] - } - if (i >= X) { - winOfMakeSatisfied -= grumpy[i - X] * customers[i - X] - } - maxMakeSatisfied = Math.max(winOfMakeSatisfied, maxMakeSatisfied) - } - return satisfied + maxMakeSatisfied -} diff --git a/1054-distant-barcodes.js b/1054-distant-barcodes.js index c0930553..b30f5995 100644 --- a/1054-distant-barcodes.js +++ b/1054-distant-barcodes.js @@ -35,72 +35,3 @@ const rearrangeBarcodes = function(barcodes) { return res }; -// another - -/** - * @param {number[]} barcodes - * @return {number[]} - */ -const rearrangeBarcodes = function(barcodes) { - const map = {}; - barcodes.forEach(b => map[b] = (map[b] || 0) + 1); - const keys = Object.keys(map).sort((k1, k2) => map[k1] - map[k2]); - - let idx = 1; - for (let k of keys) { - let t = map[k]; - - for (let i = 0; i < t; i++) { - if (idx >= barcodes.length) idx = 0; - barcodes[idx] = k; - idx += 2; - } - } - - return barcodes; -}; - -// another - -/** - * @param {number[]} barcodes - * @return {number[]} - */ -const rearrangeBarcodes = function(barcodes) { - const hash = {}, n = barcodes.length - for(let e of barcodes) { - if(hash[e] == null) hash[e] = 0 - hash[e]++ - } - const res = Array(n) - let max = 0, idx = -1 - for(let k in hash) { - if(hash[k] > max) { - max = hash[k] - idx = +k - } - } - let i = 0 - // max freq first - while(max > 0) { - res[i] = idx - max-- - i += 2 - } - // the rest - const keys = Object.keys(hash).map(e => +e) - for(let j = 0, len = keys.length; j < len; j++) { - if(keys[j] !== idx) { - const k = keys[j] - let freq = hash[k] - while(freq > 0) { - if(i >= n) i = 1 - res[i] = k - freq-- - i += 2 - } - } - } - - return res -}; diff --git a/106-construct-binary-tree-from-inorder-and-postorder-traversal.js b/106-construct-binary-tree-from-inorder-and-postorder-traversal.js index 19c5a701..6106281c 100644 --- a/106-construct-binary-tree-from-inorder-and-postorder-traversal.js +++ b/106-construct-binary-tree-from-inorder-and-postorder-traversal.js @@ -31,38 +31,3 @@ const buildTree = function(inorder, postorder) { } }; -// another - -/** - * Definition for a binary tree node. - * function TreeNode(val) { - * this.val = val; - * this.left = this.right = null; - * } - */ -/** - * @param {number[]} inorder - * @param {number[]} postorder - * @return {TreeNode} - */ - -const buildTree = function (inorder, postorder) { - let pInorder = inorder.length - 1 - let pPostorder = postorder.length - 1 - return helper(inorder, postorder, null) - function helper(inorder, postorder, end) { - if (pPostorder < 0) return null - // create root node - const n = new TreeNode(postorder[pPostorder--]) - // if right node exist, create right subtree - if (inorder[pInorder] != n.val) { - n.right = helper(inorder, postorder, n) - } - pInorder-- - // if left node exist, create left subtree - if (end === null || inorder[pInorder] !== end.val) { - n.left = helper(inorder, postorder, end) - } - return n - } -} diff --git a/1060-missing-element-in-sorted-array.js b/1060-missing-element-in-sorted-array.js index a151e66f..f10a908d 100644 --- a/1060-missing-element-in-sorted-array.js +++ b/1060-missing-element-in-sorted-array.js @@ -47,55 +47,3 @@ const missingElement = function(nums, k) { return nums[nums.length - 1] + k } -// another - -/** - * @param {number[]} nums - * @param {number} k - * @return {number} - */ -const missingElement = function(nums, k) { - const n = nums.length - let l = 0 - let h = n - 1 - const missingNum = nums[n - 1] - nums[0] + 1 - n - if (missingNum < k) { - return nums[n - 1] + k - missingNum - } - while (l < h - 1) { - const m = l + ((h - l) >> 1) - const missing = nums[m] - nums[l] - (m - l) - if (missing >= k) { - h = m - } else { - k -= missing - l = m - } - } - return nums[l] + k -} - -// another - -/** - * @param {number[]} nums - * @param {number} k - * @return {number} - */ -const missingElement = function(nums, k) { - const n = nums.length - if (k > missing(nums, n - 1)) return nums[n - 1] + k - missing(nums, n - 1) - let left = 0, - right = n - 1, - pivot - while (left < right) { - pivot = left + Math.floor((right - left) / 2) - if (missing(nums, pivot) < k) left = pivot + 1 - else right = pivot - } - return nums[left - 1] + k - missing(nums, left - 1) -} -function missing(arr, idx) { - return arr[idx] - arr[0] - idx -} - diff --git a/1062-longest-repeating-substring.js b/1062-longest-repeating-substring.js index ee57fe13..a20f587d 100644 --- a/1062-longest-repeating-substring.js +++ b/1062-longest-repeating-substring.js @@ -28,89 +28,3 @@ const hasRepeat = (s, l) => { return false; } -// another - -/** - * @param {string} s - * @return {number} - */ -const longestRepeatingSubstring = function(s) { - const n = s.length; - // dp[i][j] means # of repeated chars for substrings ending at i and j - const dp = Array.from({ length: n + 1 }, () => Array(n + 1).fill(0)); - let res = 0; - for (let i = 1; i <= n; i++) { - for (let j = i + 1; j <= n; j++) { - if (s.charAt(i - 1) === s.charAt(j - 1)) { - dp[i][j] = dp[i - 1][j - 1] + 1; - res = Math.max(res, dp[i][j]); - } - } - } - return res; -}; - -// non-overlap version -// http://nriverwang.blogspot.com/2013/04/longest-repeated-substring.html - -/* -You are to find the longest repeated substring in a given text. -Repeated substrings may not overlap. If more than one substring is -repeated with the same length, print the first one you find.(starting -from the beginning of the text). NOTE: The substrings can't be all spaces. - -Input Sample: -Your program should accept as its first argument a path to a filename. -The input file contains several lines. Each line is one test case. -Each line contains a test string. eg. - -banana -abc - -Output Sample: -For each set of input produce a single line of output which is the -longest repeated substring. If there is none, print out the string NONE. eg. -an -NONE - -std::string repeated_substring(std::string &str) { - int len = str.length(); - - int **c = new int*[len + 1]; - for (int i = 0; i <= len; ++i) - c[i] = new int[len + 1]; - for (int i = 0; i <= len; ++i) { - c[i][0] = 0; - c[0][i] = 0; - } - - int max_len = 0, index = len + 1; - for (int i = 1; i <= len; ++i) { - for (int j = 1; j <= len; ++j) { - if (str[i-1] == str[j-1] && abs(i-j) > c[i-1][j-1]) { - c[i][j] = c[i-1][j-1] + 1; - if (c[i][j] > max_len) { - max_len = c[i][j]; - index = std::min(i, j); - } - } else { - c[i][j] = 0; - } - } - } - - for (int i = 0; i <= len; ++i) - delete[] c[i]; - delete[] c; - - if (max_len > 0) { - std::string ret = str.substr(index - max_len, max_len); - for (int i = 0; i < max_len; ++i) - if(ret[i] != ' ') - return ret; - } - - return "NONE"; -} - -*/ diff --git a/1066-campus-bikes-ii.js b/1066-campus-bikes-ii.js index 8b536b6d..cebb9654 100644 --- a/1066-campus-bikes-ii.js +++ b/1066-campus-bikes-ii.js @@ -44,98 +44,3 @@ const assignBikes = function(workers, bikes) { } }; -// another - -/** - * @param {number[][]} workers - * @param {number[][]} bikes - * @return {number} - */ -const assignBikes = function(workers, bikes) { - const n = workers.length, m = bikes.length - let res = Infinity - dfs(0, 0, 0) - return res - function dfs(i, mask, cur) { - if(i === n) { - res = Math.min(res, cur) - return - } - for(let j = 0; j < m; j++) { - if((mask & (1 << j)) === 0) { - dfs(i + 1, mask | (1 << j), cur + calc(i, j)) - } - } - } - - function calc(i, j) { - const a = workers[i], b = bikes[j] - return Math.abs(a[0] - b[0]) + Math.abs(a[1] - b[1]) - } - -}; - -// another - -/** - * @param {number[][]} workers - * @param {number[][]} bikes - * @return {number} - */ -const assignBikes = function(workers, bikes) { - const n = workers.length, m = bikes.length - const dp = Array(1 << m).fill(Infinity) - return dfs(0, 0) - - function dfs(i, mask) { - if(i >= workers.length) return 0 - if(dp[mask] !== Infinity) return dp[mask] - for(let j = 0; j < bikes.length; j++) { - if((mask & (1 << j)) === 0) { - dp[mask] = Math.min(dp[mask], dist(i, j) + dfs(i + 1, mask + (1 << j))) - } - } - - return dp[mask] - } - - function dist(j, i) { - return Math.abs(bikes[i][0] - workers[j][0]) + Math.abs(bikes[i][1] - workers[j][1]) - } -}; - -// another - -/** - * @param {number[][]} workers - * @param {number[][]} bikes - * @return {number} - */ -const assignBikes = function (workers, bikes) { - const n = workers.length - const m = bikes.length - const dp = Array.from({ length: n + 1 }, () => - Array(1 << m).fill(Number.MAX_VALUE / 2) - ) - - dp[0][0] = 0 - let min = Number.MAX_VALUE - for (let i = 1; i <= n; i++) { - for (let s = 1; s < 1 << m; s++) { - for (let j = 0; j < m; j++) { - if ((s & (1 << j)) === 0) continue - let prev = s ^ (1 << j) - dp[i][s] = Math.min( - dp[i - 1][prev] + dis(workers[i - 1], bikes[j]), - dp[i][s] - ) - if (i === n) min = Math.min(min, dp[i][s]) - } - } - } - return min -} - -function dis(p1, p2) { - return Math.abs(p1[0] - p2[0]) + Math.abs(p1[1] - p2[1]) -} diff --git a/1067-digit-count-in-range.js b/1067-digit-count-in-range.js index 7b75d18d..bf7226fc 100644 --- a/1067-digit-count-in-range.js +++ b/1067-digit-count-in-range.js @@ -43,38 +43,3 @@ function countDigit(limit, d) { } -// another - -/** - * @param {number} d - * @param {number} low - * @param {number} high - * @return {number} - */ -const digitsCount = function (d, low, high) { - return countDigit(high, d) - countDigit(low - 1, d) - - function countDigit(n, d) { - if (n < 0 || n < d) { - return 0 - } - let count = 0 - for (let i = 1; i <= n; i *= 10) { - let divider = i * 10 - count += ((n / divider) >> 0) * i - if (d > 0) { - // tailing number need to be large than d * i to qualify. - count += Math.min(Math.max((n % divider) - d * i + 1, 0), i) - } else { - if (n / divider > 0) { - if (i > 1) { - // when d == 0, we need avoid to take numbers like 0xxxx into account. - count -= i - count += Math.min((n % divider) + 1, i) - } - } - } - } - return count - } -} diff --git a/107-binary-tree-level-order-traversal-ii.js b/107-binary-tree-level-order-traversal-ii.js index 12484e33..3c91921d 100644 --- a/107-binary-tree-level-order-traversal-ii.js +++ b/107-binary-tree-level-order-traversal-ii.js @@ -24,22 +24,3 @@ const levelOrderBottom = function(root) { } } -// another - -const levelOrderBottom = function(root) { - if (!root) return [] - const currentLevelNodes = [root] - const result = [] - while (currentLevelNodes.length > 0) { - const count = currentLevelNodes.length - const currentLevelValues = [] - for (let i = 0; i < count; i++) { - const node = currentLevelNodes.shift() - currentLevelValues.push(node.val) - if (node.left) currentLevelNodes.push(node.left) - if (node.right) currentLevelNodes.push(node.right) - } - result.unshift(currentLevelValues) - } - return result -} diff --git a/1079-letter-tile-possibilities.js b/1079-letter-tile-possibilities.js index e4317a21..d33ba2ae 100644 --- a/1079-letter-tile-possibilities.js +++ b/1079-letter-tile-possibilities.js @@ -20,55 +20,3 @@ function dfs(tiles, used, visited, path, obj) { } } -// another - -/** - * @param {string} tiles - * @return {number} - */ -const numTilePossibilities = function(tiles) { - let used = new Array(tiles.length).fill(false); - let visited = new Set(); - let cnt = 0; - const dfs = (path) => { - if (path.length && !visited.has(path)) { - visited.add(path); - cnt++; - } - for (let i = 0; i < tiles.length; i++) { - if (used[i]) continue; - used[i] = true; - dfs(path + tiles[i]); - used[i] = false; - } - } - dfs(''); - return cnt; -}; - -// another - -/** - * @param {string} tiles - * @return {number} - */ -const numTilePossibilities = function(tiles) { - const count = new Array(26).fill(0) - const ACode = 'A'.charCodeAt(0) - for (let i = 0, len = tiles.length; i < len; i++) { - count[tiles.charCodeAt(i) - ACode]++ - } - return dfs(count) -} - -function dfs(arr) { - let sum = 0 - for (let i = 0; i < 26; i++) { - if (arr[i] === 0) continue - sum++ - arr[i]-- - sum += dfs(arr) - arr[i]++ - } - return sum -} diff --git a/1081-smallest-subsequence-of-distinct-characters.js b/1081-smallest-subsequence-of-distinct-characters.js index 2b5e37c8..23e6105f 100644 --- a/1081-smallest-subsequence-of-distinct-characters.js +++ b/1081-smallest-subsequence-of-distinct-characters.js @@ -28,54 +28,3 @@ const smallestSubsequence = function(text) { return '' } -// another - -/** - * @param {string} text - * @return {string} - */ -const smallestSubsequence = function(s) { - let res = [] - const count = new Array(26).fill(0) - const used = new Array(26).fill(0) - const aCode = 'a'.charCodeAt(0) - for (let el of s) count[el.charCodeAt(0) - aCode]++ - for (let el of s) { - count[el.charCodeAt(0) - aCode]-- - if (used[el.charCodeAt(0) - aCode]++ > 0) continue - while ( - res.length && - res[res.length - 1].charCodeAt(0) > el.charCodeAt(0) && - count[res[res.length - 1].charCodeAt(0) - aCode] > 0 - ) { - used[res[res.length - 1].charCodeAt(0) - aCode] = 0 - res.pop() - } - res.push(el) - } - return res.join('') -}; - -// anoother - - -/** - * @param {string} text - * @return {string} - */ -const smallestSubsequence = function(text) { - const n = text.length, stack = [], last = {}, visited = {} - for(let i = 0; i < n; i++) last[text[i]] = i - for(let i = 0; i < n; i++) { - const ch = text[i] - if (visited[ch]) continue - while(stack.length && stack[stack.length - 1] > ch && last[stack[stack.length - 1]] > i) { - visited[stack[stack.length - 1]] = 0 - stack.pop() - } - visited[ch] = 1 - stack.push(ch) - } - - return stack.join('') -}; diff --git a/109-convert-sorted-list-to-binary-search-tree.js b/109-convert-sorted-list-to-binary-search-tree.js index a4e168e3..000c140f 100644 --- a/109-convert-sorted-list-to-binary-search-tree.js +++ b/109-convert-sorted-list-to-binary-search-tree.js @@ -39,23 +39,3 @@ function build(arr, parent, type) { return node } -// another - -const sortedListToBST = function(head, tail = null) { - if (head === tail) { - return null; - } else if (head.next === tail) { - return new TreeNode(head.val); - } else { - let slow = head; - let fast = head; - while (fast !== tail && fast.next !== tail) { - slow = slow.next; - fast = fast.next.next; - } - let node = new TreeNode(slow.val); - node.left = sortedListToBST(head, slow); - node.right = sortedListToBST(slow.next, tail); - return node; - } -}; diff --git a/1091-shortest-path-in-binary-matrix.js b/1091-shortest-path-in-binary-matrix.js index 110137e1..efbd89f8 100644 --- a/1091-shortest-path-in-binary-matrix.js +++ b/1091-shortest-path-in-binary-matrix.js @@ -33,48 +33,3 @@ const shortestPathBinaryMatrix = function(grid) { return -1 }; -// another - -/** - * @param {number[][]} grid - * @return {number} - */ -const shortestPathBinaryMatrix = function(grid) { - if(grid == null || grid.length === 0 || grid[0][0] === 1) return -1 - let res = 1 - const n = grid.length - const dirs = [ - [1, 0], - [-1, 0], - [0, 1], - [0, -1], - [-1, -1], - [-1, 1], - [1, 1], - [1, -1], - ] - let q = [[0, 0]] - while(q.length) { - let ref = q - q = [] - for(let [x, y] of ref) { - if(x === n - 1 && y === n - 1) return res - grid[x][y] = 1 - for(let [dx, dy] of dirs) { - const nx = x + dx, ny = y + dy - if(helper(grid, nx, ny)) { - q.push([nx, ny]) - grid[nx][ny] = 1 // very important - } - } - } - res++ - } - return -1 -}; - -function helper(grid, i, j) { - const n = grid.length - if(i < 0 || i >= n || j < 0 || j >= n || grid[i][j] !== 0) return false - return true -} diff --git a/1094-car-pooling.js b/1094-car-pooling.js index a37547cf..30e17070 100644 --- a/1094-car-pooling.js +++ b/1094-car-pooling.js @@ -19,43 +19,3 @@ const carPooling = function(trips, capacity) { return true }; -// another - -/** - * @param {number[][]} trips - * @param {number} capacity - * @return {boolean} - */ -const carPooling = function(trips, capacity) { - let stops = new Array(1001).fill(0) - for (let t of trips) { - stops[t[1]] += t[0] - stops[t[2]] -= t[0] - } - for (let i = 0; capacity >= 0 && i < 1001; ++i) capacity -= stops[i] - return capacity >= 0 -} - -// another - -/** - * @param {number[][]} trips - * @param {number} capacity - * @return {boolean} - */ -const carPooling = function(trips, capacity) { - const arr = Array(1001).fill(0) - for(let el of trips) { - const [num, s, e] = el - arr[s] += num - arr[e] -= num - } - for(let i = 1; i < 1001; i++) { - if(arr[i] !== 0) arr[i] += arr[i - 1] - else arr[i] = arr[i - 1] - } - for(let e of arr) { - if(e > capacity) return false - } - return true -}; diff --git a/1104.path-in-zigzag-labelled-binary-tree.js b/1104.path-in-zigzag-labelled-binary-tree.js index 7f5f3c54..75430edf 100644 --- a/1104.path-in-zigzag-labelled-binary-tree.js +++ b/1104.path-in-zigzag-labelled-binary-tree.js @@ -19,43 +19,3 @@ const pathInZigZagTree = function(label) { return res }; -// another - - -/** - * @param {number} label - * @return {number[]} - */ -const pathInZigZagTree = function(label) { - const res = [], { log2, floor, ceil } = Math - - res.push(label) - - // check last row - const lev = ceil(log2(label + 1)) - const reverse = lev % 2 === 0 ? true : false - // console.log(reverse, lev) - if(reverse) { - const idx = 2 ** lev - 1 - label - label = 2 ** (lev - 1) + idx - } - // console.log(label) - - while(label > 1) { - const level = floor(log2(label)) - const parent = floor(label / 2) - const parentLevelNum = 2 ** (level - 1) - const parentReverse = level % 2 === 0 ? true : false - const parentStart = 2 ** (level - 1) - const parentEnd = 2 ** level - 1 - // console.log(parentStart, parentEnd, parent) - const idx = parent - parentStart - res.push(parentReverse ? parentEnd - idx : parentStart + idx) - - label = parent - } - - - res.reverse() - return res -}; diff --git a/1105-filling-bookcase-shelves.js b/1105-filling-bookcase-shelves.js index 26f71c83..4e7e7bd0 100644 --- a/1105-filling-bookcase-shelves.js +++ b/1105-filling-bookcase-shelves.js @@ -19,47 +19,3 @@ const minHeightShelves = function(books, shelf_width) { return dp[books.length] }; -// another - -/** - * @param {number[][]} books - * @param {number} shelf_width - * @return {number} - */ -const minHeightShelves = function(books, shelf_width) { - const n = books.length, dp = Array(1001).fill(Infinity) - dp[0] = 0 - for(let i = 0; i < n; i++) { - let sum = 0, mx = 0 - for(let j = i; j >= 0 && sum + books[j][0] <= shelf_width; j--) { - sum += books[j][0] - mx = Math.max(mx, books[j][1]) - dp[i + 1] = Math.min(dp[i + 1], dp[j] + mx) - } - } - return dp[n] -}; - -// another - -/** - * @param {number[][]} books - * @param {number} shelf_width - * @return {number} - */ - const minHeightShelves = function(books, shelf_width) { - const n = books.length, dp = Array(1001) - dp[0] = 0 - - for(let i = 0; i < n; i++) { - let [w, h] = books[i] - dp[i + 1] = dp[i] + h - for(let j = i - 1; j >= 0 && w + books[j][0] <= shelf_width; j--) { - h = Math.max(h, books[j][1]) - w += books[j][0] - dp[i + 1] = Math.min(dp[i + 1], dp[j] + h) - } - } - - return dp[n] -}; diff --git a/1109-corporate-flight-bookings.js b/1109-corporate-flight-bookings.js index 8fe83d1c..9e2a4f97 100644 --- a/1109-corporate-flight-bookings.js +++ b/1109-corporate-flight-bookings.js @@ -17,39 +17,3 @@ const corpFlightBookings = function(bookings, n) { return arr }; -// another - -/** - * @param {number[][]} bookings - * @param {number} n - * @return {number[]} - */ -const corpFlightBookings = function(bookings, n) { - let res = new Array(n).fill(0) - for (let v of bookings) { - res[v[0] - 1] += v[2] - if (v[1] < n) res[v[1]] -= v[2] - } - for (let i = 1; i < n; ++i) res[i] += res[i - 1] - return res -} - -// another - -/** - * @param {number[][]} bookings - * @param {number} n - * @return {number[]} - */ -const corpFlightBookings = function(bookings, n) { - const arr = Array(n + 2).fill(0) - for(let [s, e, num] of bookings) { - arr[s] += num - arr[e + 1] -= num - } - for(let i = 1; i <= n; i++) { - if(arr[i] !== 0) arr[i] += arr[i - 1] - else arr[i] = arr[i - 1] - } - return arr.slice(1, n + 1) -}; diff --git a/1118-number-of-days-in-a-month.js b/1118-number-of-days-in-a-month.js index f9a90c14..394e8353 100644 --- a/1118-number-of-days-in-a-month.js +++ b/1118-number-of-days-in-a-month.js @@ -41,26 +41,3 @@ function numberOfDays(Y, M) { } } -// another - -const numberOfDays = function(Y, M) { - return new Date(Y,M,0).getDate(); -}; - -// another - -/** - * @param {number} Y - * @param {number} M - * @return {number} - */ -const numberOfDays = function(Y, M) { - const d = new Date(Y, M - 1) - let num = 0 - while(d.getMonth() === M - 1) { - num++ - const n = d.getDate() - d.setDate(n + 1) - } - return num -}; diff --git a/1123-lowest-common-ancestor-of-deepest-leaves.js b/1123-lowest-common-ancestor-of-deepest-leaves.js index ef42ddea..70883d5a 100644 --- a/1123-lowest-common-ancestor-of-deepest-leaves.js +++ b/1123-lowest-common-ancestor-of-deepest-leaves.js @@ -29,69 +29,3 @@ const lcaDeepestLeaves = function(root) { }; -// another - -/** - * Definition for a binary tree node. - * function TreeNode(val) { - * this.val = val; - * this.left = this.right = null; - * } - */ -/** - * @param {TreeNode} root - * @return {TreeNode} - */ -const lcaDeepestLeaves = function(root) { - if (root === null) return null - const getHeight = root => { - if (root === null) return 0 - const res = Math.max(getHeight(root.left), getHeight(root.right)) + 1 - return res - } - if (getHeight(root.left) === getHeight(root.right)) { - return root - } else if (getHeight(root.left) > getHeight(root.right)) { - return lcaDeepestLeaves(root.left) - } else { - return lcaDeepestLeaves(root.right) - } -} - -// BFS - -const lcaDeepestLeaves = function(root) { - let current = [root]; - let level = 0; - let last = []; - while(current.length) { - let next = []; - for (var i = 0; i < current.length; i++) { - if (current[i].left) { - current[i].left.parent = current[i]; - next.push(current[i].left); - } - if (current[i].right) { - current[i].right.parent = current[i]; - next.push(current[i].right); - } - } - last = current; - current = next; - } - let parent = last[0].parent; - if (!parent) { - return last[0]; - } - while(last.length > 1) { - let next = []; - for (var i = 0; i < last.length; i++) { - newParent = last[i].parent; - if (!next.includes(newParent)) { - next.push(newParent); - } - } - last = next; - } - return last[0]; -}; diff --git a/1125-smallest-sufficient-team.js b/1125-smallest-sufficient-team.js index 4b890c98..9655874f 100644 --- a/1125-smallest-sufficient-team.js +++ b/1125-smallest-sufficient-team.js @@ -37,160 +37,3 @@ const smallestSufficientTeam = function (req_skills, people) { return dp[limit - 1] } -// another - - -/** - * @param {string[]} req_skills - * @param {string[][]} people - * @return {number[]} - */ -const smallestSufficientTeam = function(req_skills, people) { - const m = req_skills.length - const n = people.length - const skill2Bitmap = req_skills - .map((x, i) => [x, i]) - .reduce((dict, cur) => { - dict[cur[0]] = 1 << cur[1] - return dict - }, {}) - const newPeople = people.map(x => { - return x.reduce((acc, cur) => { - const y = skill2Bitmap[cur] - if (y !== undefined) { - acc |= y - } - return acc - }, 0) - }) - - const all = (1 << m) - 1 - const dp = {} - for (let j = 0; j < n; j++) { - if (newPeople[j] > 0) { - dp[newPeople[j]] = new Set([j]) - } - } - if (dp[all]) { - return Array.from(dp[all]).sort() - } - - for (let k = 0; k < n; k++) { - for (let s in dp) { - for (let j = 0; j < n; j++) { - if (newPeople[j] === 0 || dp[s].has(j)) continue - const newIdx = s | newPeople[j] - if (dp[newIdx] === undefined) { - dp[newIdx] = new Set([...dp[s], j]) - if (newIdx === all) { - return Array.from(dp[all]).sort() - } - } - } - } - } - return [] -} - -// another - -/** - * @param {string[]} req_skills - * @param {string[][]} people - * @return {number[]} - */ -const smallestSufficientTeam = function(req_skills, people) { - let skill_len = req_skills.length - - // 将people转换为id的模式 - let id_people = [] - let hash = {} - for (let i = 0; i < skill_len; i++) { - hash[req_skills[i]] = i - } - for (let i = 0; i < people.length; i++) { - id_people[i] = [] - for (let j = 0; j < people[i].length; j++) { - id_people[i][j] = hash[people[i][j]] - } - } - - // 过滤掉不可能的选取的人员 - let skip = {} - for (let i = 0; i < id_people.length; i++) { - if (skip[i]) continue - let skills = Array(skill_len).fill(0) - for (let j = 0; j < id_people[i].length; j++) { - let curId = id_people[i][j] - skills[curId]++ - } - for (let k = i + 1; k < id_people.length; k++) { - if (skip[k]) continue - let needSkip = true - for (let l = 0; l < id_people[k].length; l++) { - let id = id_people[k][l] - if (skills[id] === 0) { - needSkip = false - break - } - } - if (needSkip) { - skip[k] = true - } - } - } - - // 构造精简后的人员,并且保存对应的index关系 - let slim_people = [] - let idHash = {} - for (let i = 0; i < id_people.length; i++) { - if (skip[i]) continue - idHash[slim_people.length] = i - slim_people.push(id_people[i]) - } - - // 执行回溯 - let res = Infinity - let remain = {} - let ans = null - for (let i = 0; i < slim_people.length; i++) { - remain[i] = false - } - let init_select = Array(skill_len).fill(0) - - backtrack(0, init_select, 0, remain) - - return ans - - function backtrack(id, select, count, remain) { - if (count >= res) return - let done = true - for (let i = 0; i < select.length; i++) { - if (select[i] === 0) { - done = false - } - } - if (done) { - res = count - let _res_ = [] - for (let k in remain) { - if (remain[k]) _res_.push(idHash[k]) - } - ans = _res_ - return - } - for (let k = id; k < slim_people.length; k++) { - let arr = slim_people[k] - for (let i = 0; i < arr.length; i++) { - select[arr[i]]++ - } - remain[k] = true - backtrack(k + 1, select, count + 1, remain) - remain[k] = false - for (let i = 0; i < arr.length; i++) { - select[arr[i]]-- - } - } - } -} - diff --git a/1136-parallel-courses.js b/1136-parallel-courses.js index 6f727b6e..0f0f0d7c 100644 --- a/1136-parallel-courses.js +++ b/1136-parallel-courses.js @@ -43,40 +43,3 @@ const minimumSemesters = function (n, relations) { } -// another - -/** - * @param {number} N - * @param {number[][]} relations - * @return {number} - */ -const minimumSemesters = function (N, relations) { - const g = new Map() // key: prerequisite, value: course list. - const inDegree = new Array(N + 1).fill(0) // inDegree[i]: number of prerequisites for i. - for (let r of relations) { - if (!g.has(r[0])) g.set(r[0], []) - g.get(r[0]).push(r[1]) // construct graph. - ++inDegree[r[1]] // count prerequisites for r[1]. - } - const q = [] // save current 0 in-degree vertices. - for (let i = 1; i <= N; ++i) if (inDegree[i] === 0) q.push(i) - let semester = 0 - while (q.length) { - // BFS traverse all currently 0 in degree vertices. - for (let sz = q.length; sz > 0; --sz) { - // sz is the search breadth. - const c = q.shift() - --N - // c's in-degree is currently 0, but it is not a prerequisite of anyone else. - if (!g.has(c)) continue - const tmp = g.get(c) - g.delete(c) - for (let course of tmp) - if (--inDegree[course] === 0) - // decrease the in-degree of course's neighbors. - q.push(course) // add current 0 in-degree vertex into Queue. - } - ++semester // need one more semester. - } - return N === 0 ? semester : -1 -} diff --git a/1137-n-th-tribonacci-number.js b/1137-n-th-tribonacci-number.js index c47209e5..c75dbd04 100644 --- a/1137-n-th-tribonacci-number.js +++ b/1137-n-th-tribonacci-number.js @@ -11,22 +11,3 @@ const tribonacci = function(n) { return hash[n] = tmp }; -// another - -/** - * @param {number} n - * @return {number} - */ -const tribonacci = function(n) { - if (n < 2) return n - let prev0 = 0 - let prev1 = 1 - let prev2 = 1 - for (let count = 3; count <= n; count++) { - let next = prev2 + prev1 + prev0 - prev0 = prev1 - prev1 = prev2 - prev2 = next - } - return prev2 -} diff --git a/1143-longest-common-subsequence.js b/1143-longest-common-subsequence.js index 9e856a67..6c58b4e1 100644 --- a/1143-longest-common-subsequence.js +++ b/1143-longest-common-subsequence.js @@ -16,28 +16,3 @@ const longestCommonSubsequence = function(text1, text2) { return dp[dp.length - 1].pop() } -// another - -/** - * @param {string} text1 - * @param {string} text2 - * @return {number} - */ -const longestCommonSubsequence = function(text1, text2) { - const len1 = text1.length - const len2 = text2.length - if(len1 === 0 || len2 === 0) return 0 - const dp = Array(len2 + 1).fill(0) - for(let i = 1; i <= len1; i++) { - let prev = 0 - for(let j = 1; j <= len2; j++) { - const tmp = dp[j] - if(text1[i - 1] === text2[j - 1]) dp[j] = Math.max(dp[j], prev + 1) - else { - dp[j] = Math.max(dp[j - 1], dp[j]) - } - prev = tmp - } - } - return dp[len2] -}; diff --git a/1146-snapshot-array.js b/1146-snapshot-array.js index 28e93963..4bcb4508 100644 --- a/1146-snapshot-array.js +++ b/1146-snapshot-array.js @@ -52,133 +52,3 @@ SnapshotArray.prototype.get = function(index, snap_id) { * var param_3 = obj.get(index,snap_id) */ -// another - -/** - * @param {number[]} nums - * @param {number} target - * @return {number} - */ -const binarySearch = function (nums, target, comparator) { - let low = 0; - let high = nums.length - 1; - while (low <= high) { - let mid = low + ((high - low) >>> 1); - let midValue = nums[mid]; - let cmp = comparator(midValue, target); - if (cmp < 0) low = mid + 1; - else if (cmp > 0) high = mid - 1; - else return mid; - } - return -(low + 1); -}; - -/** - * @param {number} length - */ -const SnapshotArray = function (length) { - this.count = 0; - this.arr = Array.from({ length: length }, () => [[0, 0]]); -}; - -/** - * @param {number} index - * @param {number} val - * @return {void} - */ -SnapshotArray.prototype.set = function (index, val) { - const arr = this.arr, - count = this.count; - if (arr[index][arr[index].length - 1][0] === count) { - arr[index][arr[index].length - 1][1] = val; - } else { - arr[index].push([count, val]); - } -}; - -/** - * @return {number} - */ -SnapshotArray.prototype.snap = function () { - return this.count++; -}; - -/** - * @param {number} index - * @param {number} snap_id - * @return {number} - */ -SnapshotArray.prototype.get = function (index, snap_id) { - let idx = binarySearch(this.arr[index], [snap_id, 0], (a, b) => a[0] - b[0]); - if (idx < 0) idx = -idx - 2; - return this.arr[index][idx][1]; -}; - -/** - * Your SnapshotArray object will be instantiated and called as such: - * var obj = new SnapshotArray(length) - * obj.set(index,val) - * var param_2 = obj.snap() - * var param_3 = obj.get(index,snap_id) - */ - - -// another - -/** - * @param {number} length - */ -const SnapshotArray = function(length) { - this.arr = new Array(length).fill(0); - this.snaps = new Array(length); - this.count = 0; -}; - -/** - * @param {number} index - * @param {number} val - * @return {void} - */ -SnapshotArray.prototype.set = function(index, val) { - if (this.snaps[index] == undefined) { - this.snaps[index] = {}; - } - - this.snaps[index][this.count] = val; -}; - -/** - * @return {number} - */ -SnapshotArray.prototype.snap = function() { - return this.count++; -}; - -/** - * @param {number} index - * @param {number} snap_id - * @return {number} - */ -SnapshotArray.prototype.get = function(index, snap_id) { - if (this.snaps[index] == undefined) return 0; - - let res = 0; - while (snap_id >= 0) { - if (this.snaps[index][snap_id] == undefined) { - snap_id--; - } else { - res = this.snaps[index][snap_id]; - snap_id = -1; - } - } - - return res; -}; - -/** - * Your SnapshotArray object will be instantiated and called as such: - * var obj = new SnapshotArray(length) - * obj.set(index,val) - * var param_2 = obj.snap() - * var param_3 = obj.get(index,snap_id) - */ diff --git a/1147-longest-chunked-palindrome-decomposition.js b/1147-longest-chunked-palindrome-decomposition.js index dff03658..6ecee0f8 100644 --- a/1147-longest-chunked-palindrome-decomposition.js +++ b/1147-longest-chunked-palindrome-decomposition.js @@ -19,16 +19,3 @@ const longestDecomposition = function(text) { return res } -// another - -/** - * @param {string} text - * @return {number} - */ -const longestDecomposition = function(text) { - let n = text.length - for (let i = 0; i < Math.floor(n / 2); i++) - if (text.slice(0, i + 1) === text.slice(n - 1 - i, n)) - return 2 + longestDecomposition(text.slice(i + 1, n - 1 - i)) - return n === 0 ? 0 : 1 -} diff --git a/115-distinct-subsequences.js b/115-distinct-subsequences.js index 570d633e..e2ae0ef0 100644 --- a/115-distinct-subsequences.js +++ b/115-distinct-subsequences.js @@ -25,46 +25,3 @@ const numDistinct = function(s, t) { } -// another - -/** - * @param {string} s - * @param {string} t - * @return {number} - */ -const numDistinct = function(s, t) { - const m = t.length, - n = s.length - const cur = new Array(m + 1).fill(0) - cur[0] = 1 - for (let j = 1; j <= n; j++) { - let pre = 1 - for (let i = 1; i <= m; i++) { - let temp = cur[i] - cur[i] = cur[i] + (t[i - 1] == s[j - 1] ? pre : 0) - pre = temp - } - } - return cur[m] -} - -// another - -/** - * @param {string} s - * @param {string} t - * @return {number} - */ -const numDistinct = function(s, t) { - const m = t.length, - n = s.length - const dp = new Array(m + 1).fill(0) - dp[0] = 1 - for (let j = 1; j <= n; j++) { - for (let i = m; i > 0; i--) { - dp[i] = dp[i] + (t[i - 1] == s[j - 1] ? dp[i - 1] : 0) - } - } - return dp[m] -} - diff --git a/1155-number-of-dice-rolls-with-target-sum.js b/1155-number-of-dice-rolls-with-target-sum.js index fe6259d9..6309cbfb 100644 --- a/1155-number-of-dice-rolls-with-target-sum.js +++ b/1155-number-of-dice-rolls-with-target-sum.js @@ -25,22 +25,3 @@ const numRollsToTarget = function(d, f, target) { return dp[target] } -// another - -const numRollsToTarget = function(d, f, target) { - const MOD = 10 ** 9 + 7; - const dp = Array.from({ length: d + 1 }, () => new Array(target + 1).fill(0)); - dp[0][0] = 1; - for (let i = 1; i <= d; i++) { - for (let j = 1; j <= target; j++) { - if (j > i * f) { - continue; - } else { - for (let k = 1; k <= f && k <= j; k++) { - dp[i][j] = (dp[i][j] + dp[i - 1][j - k]) % MOD; - } - } - } - } - return dp[d][target]; -}; diff --git a/1157-online-majority-element-in-subarray.js b/1157-online-majority-element-in-subarray.js index 54f72984..b16da637 100644 --- a/1157-online-majority-element-in-subarray.js +++ b/1157-online-majority-element-in-subarray.js @@ -133,73 +133,3 @@ function MajorityChecker(a) { } } -// another - - -/** - * @param {number[]} arr - */ -const MajorityChecker = function(arr) { - const map = new Map() - for (let i = 0; i < arr.length; i++) { - if (!map.has(arr[i])) map.set(arr[i], [i]) - else map.get(arr[i]).push(i) - } - this.pos = map - this.arr = arr -} - -function lbs(arr, val) { - let lo = 0 - let hi = arr.length - 1 - if (arr[0] >= val) return 0 - else if (arr[hi] < val) return Infinity - let mid - while (hi - lo > 1) { - mid = (hi + lo) >> 1 - if (arr[mid] === val) return mid - else if (arr[mid] < val) lo = mid - else if (arr[mid] > val) hi = mid - } - return hi -} - -function rbs(arr, val) { - let lo = 0 - let hi = arr.length - 1 - if (arr[hi] <= val) return hi - else if (arr[lo] > val) return -Infinity - let mid - while (hi - lo > 1) { - mid = (hi + lo) >> 1 - if (arr[mid] === val) return mid - else if (arr[mid] < val) lo = mid - else if (arr[mid] > val) hi = mid - } - return lo -} - -/** - * @param {number} left - * @param {number} right - * @param {number} threshold - * @return {number} - */ -MajorityChecker.prototype.query = function(left, right, threshold) { - const { arr, pos } = this - let c = 20 - while (c--) { - const idx = left + Math.floor(Math.random() * (right - left + 1)) - const sort = pos.get(arr[idx]) - const lidx = lbs(sort, left) - const ridx = rbs(sort, right) - if (ridx - lidx + 1 >= threshold) return arr[idx] - } - return -1 -} - -/** - * Your MajorityChecker object will be instantiated and called as such: - * var obj = new MajorityChecker(arr) - * var param_1 = obj.query(left,right,threshold) - */ diff --git a/116-populating-next-right-pointers-in-each-node.js b/116-populating-next-right-pointers-in-each-node.js index 3461fb0c..dd06721c 100644 --- a/116-populating-next-right-pointers-in-each-node.js +++ b/116-populating-next-right-pointers-in-each-node.js @@ -28,15 +28,3 @@ const connect = function(root) { return root } -// another - -const connect = function(root) { - if (!root) return null - if (root.left && root.right) { - root.left.next = root.right - root.right.next = root.next ? root.next.left : null - } - connect(root.left) - connect(root.right) - return root -} diff --git a/1166-design-file-system.js b/1166-design-file-system.js index 8378b6d1..c6a013bb 100644 --- a/1166-design-file-system.js +++ b/1166-design-file-system.js @@ -33,53 +33,3 @@ FileSystem.prototype.get = function(path) { * var param_2 = obj.get(path) */ -// another - -class Node { - constructor(v) { - this.val = v - this.children = new Map() - } -} -const FileSystem = function() { - this.r = new Node(null) -}; - -/** - * @param {string} path - * @param {number} value - * @return {boolean} - */ -FileSystem.prototype.createPath = function(path, value) { - if(path == null || path === '') return - const arr = path.split('/').filter(e => e !== '/' && e !== '') - let cur = this.r - for(let i = 0, len = arr.length; i < len; i++) { - if(i !== len - 1 && !cur.children.has(arr[i])) return false - if(i === len - 1 && cur.children.has(arr[i])) return false - if(i !== len - 1) cur = cur.children.get(arr[i]) - } - cur.children.set(arr[arr.length - 1], new Node(value)) - return true -}; - -/** - * @param {string} path - * @return {number} - */ -FileSystem.prototype.get = function(path) { - const arr = path.split('/').filter(e => e !== '/' && e !== '') - let cur = this.r - for(let i = 0, len = arr.length; i < len; i++) { - if(!cur.children.has(arr[i])) return -1 - cur = cur.children.get(arr[i]) - } - return cur.val -}; - -/** - * Your FileSystem object will be instantiated and called as such: - * var obj = new FileSystem() - * var param_1 = obj.createPath(path,value) - * var param_2 = obj.get(path) - */ diff --git a/1167-minimum-cost-to-connect-sticks.js b/1167-minimum-cost-to-connect-sticks.js index 6eeb60c6..42620f17 100644 --- a/1167-minimum-cost-to-connect-sticks.js +++ b/1167-minimum-cost-to-connect-sticks.js @@ -66,148 +66,3 @@ const swap = (arr, i, j) => { arr[j] = temp } -// another - -/** - * @param {number[]} sticks - * @return {number} - */ -const connectSticks = function(sticks) { - if (sticks.length === 1) return 0 - sticks.sort((a, b) => a - b) - let sum = [], - result = 0 - while (sticks.length || sum.length > 1) { - let cur = 0 - for (let i = 0; i < 2; i++) { - if (sticks[0] && (sum[0] === undefined || sticks[0] < sum[0])) { - cur += sticks[0] - sticks.shift() - } else { - cur += sum[0] - sum.shift() - } - } - sum.push(cur) - result += cur - } - return result -} - -// another - -/** - * @param {number[]} sticks - * @return {number} - */ -const connectSticks = function(sticks) { - sticks.sort((a, b) => a - b) - const sums = [] - let result = 0 - if (sticks.length < 2) return result - const getMin = () => { - const stick = sticks.length ? sticks[0] : Infinity - const sum = sums.length ? sums[0] : Infinity - if (sum < stick) { - return sums.shift() - } else { - return sticks.shift() - } - } - while (sticks.length || sums.length > 1) { - const tmp1 = getMin() - const tmp2 = getMin() - const curr = tmp1 + tmp2 - result += curr - sums.push(curr) - } - return result -} - -// another - -/** - * @param {number[]} sticks - * @return {number} - */ -const connectSticks = function(sticks) { - const pq = new PriorityQueue((a, b) => a < b) - for(let e of sticks) pq.push(e) - let res = 0 - while(pq.size() > 1) { - const e1 = pq.pop() - const e2 = pq.pop() - pq.push(e1 + e2) - res += e1 + e2 - } - - return res -}; - -class PriorityQueue { - constructor(comparator = (a, b) => a > b) { - this.heap = [] - this.top = 0 - this.comparator = comparator - } - size() { - return this.heap.length - } - isEmpty() { - return this.size() === 0 - } - peek() { - return this.heap[this.top] - } - push(...values) { - values.forEach((value) => { - this.heap.push(value) - this.siftUp() - }) - return this.size() - } - pop() { - const poppedValue = this.peek() - const bottom = this.size() - 1 - if (bottom > this.top) { - this.swap(this.top, bottom) - } - this.heap.pop() - this.siftDown() - return poppedValue - } - replace(value) { - const replacedValue = this.peek() - this.heap[this.top] = value - this.siftDown() - return replacedValue - } - - parent = (i) => ((i + 1) >>> 1) - 1 - left = (i) => (i << 1) + 1 - right = (i) => (i + 1) << 1 - greater = (i, j) => this.comparator(this.heap[i], this.heap[j]) - swap = (i, j) => ([this.heap[i], this.heap[j]] = [this.heap[j], this.heap[i]]) - siftUp = () => { - let node = this.size() - 1 - while (node > this.top && this.greater(node, this.parent(node))) { - this.swap(node, this.parent(node)) - node = this.parent(node) - } - } - siftDown = () => { - let node = this.top - while ( - (this.left(node) < this.size() && this.greater(this.left(node), node)) || - (this.right(node) < this.size() && this.greater(this.right(node), node)) - ) { - let maxChild = - this.right(node) < this.size() && - this.greater(this.right(node), this.left(node)) - ? this.right(node) - : this.left(node) - this.swap(node, maxChild) - node = maxChild - } - } -} diff --git a/1172-dinner-plate-stacks.js b/1172-dinner-plate-stacks.js index 72f8a9bd..48a49e14 100644 --- a/1172-dinner-plate-stacks.js +++ b/1172-dinner-plate-stacks.js @@ -143,72 +143,3 @@ class PriorityQueue { } } -// another - -/** - * @param {number} capacity - */ -const DinnerPlates = function (capacity) { - this.pushIndex = 0 - this.popIndex = 0 - this.capacity = capacity - this.stacks = [[]] -} - -/** - * @param {number} val - * @return {void} - */ -DinnerPlates.prototype.push = function (val) { - while ( - this.pushIndex < this.stacks.length && - this.stacks[this.pushIndex].length === this.capacity - ) { - this.pushIndex++ - } - if (this.stacks.length === this.pushIndex) { - this.stacks[this.pushIndex] = [val] - } else { - this.stacks[this.pushIndex].push(val) - } - if (this.popIndex < this.pushIndex) { - this.popIndex = this.pushIndex - } -} - -/** - * @return {number} - */ -DinnerPlates.prototype.pop = function () { - while (this.stacks[this.popIndex].length === 0) { - if (this.popIndex > 0) { - this.popIndex-- - } else { - return -1 - } - } - const valueAtIndex = this.stacks[this.popIndex].pop() - if (this.pushIndex > this.popIndex) { - this.pushIndex = this.popIndex - } - return valueAtIndex -} - -/** - * @param {number} index - * @return {number} - */ -DinnerPlates.prototype.popAtStack = function (index) { - if (index >= this.stacks.length) return -1 - if (index < this.pushIndex) this.pushIndex = index - return this.stacks[index].length > 0 ? this.stacks[index].pop() : -1 -} - -/** - * Your DinnerPlates object will be instantiated and called as such: - * var obj = new DinnerPlates(capacity) - * obj.push(val) - * var param_2 = obj.pop() - * var param_3 = obj.popAtStack(index) - */ - diff --git a/1176-diet-plan-performance.js b/1176-diet-plan-performance.js index 4930160b..de23df41 100644 --- a/1176-diet-plan-performance.js +++ b/1176-diet-plan-performance.js @@ -21,24 +21,3 @@ var dietPlanPerformance = function(calories, k, lower, upper) { return res }; -// another - -/** - * @param {number[]} calories - * @param {number} k - * @param {number} lower - * @param {number} upper - * @return {number} - */ -var dietPlanPerformance = function(calories, k, lower, upper) { - let res = 0 - for(let i = 0, n = calories.length; i < n - k + 1; i++) { - let tmp = 0 - for(let j = 0; j < k && i + j < n; j++) { - tmp += calories[i + j] - } - if(tmp < lower) res-- - else if(tmp > upper) res++ - } - return res -}; diff --git a/1186-maximum-subarray-sum-with-one-deletion.js b/1186-maximum-subarray-sum-with-one-deletion.js index 6938f589..ee9fa2fb 100644 --- a/1186-maximum-subarray-sum-with-one-deletion.js +++ b/1186-maximum-subarray-sum-with-one-deletion.js @@ -15,28 +15,3 @@ const maximumSum = function (arr) { return res } -// another - -/** - * @param {number[]} arr - * @return {number} - */ -const maximumSum = function (arr) { - const n = arr.length - let max = arr[0] - const maxEndAt = Array(n), maxStartAt = Array(n) - maxEndAt[0] = arr[0] - for(let i = 1; i < n; i++) { - maxEndAt[i] = Math.max(arr[i], maxEndAt[i - 1] + arr[i]) - max = Math.max(max, maxEndAt[i]) - } - maxStartAt[n - 1] = arr[n - 1] - for(let i = n - 2; i >= 0; i--) { - maxStartAt[i] = Math.max(arr[i], maxStartAt[i + 1] + arr[i]) - } - let res = Math.max(maxStartAt[0], maxEndAt[n - 1]) - for(let i = 1; i < n - 1; i++) { - res = Math.max(max, res, maxEndAt[i - 1] + maxStartAt[i + 1]) - } - return res -} diff --git a/1190-reverse-substrings-between-each-pair-of-parentheses.js b/1190-reverse-substrings-between-each-pair-of-parentheses.js index cdfe4768..71af4643 100644 --- a/1190-reverse-substrings-between-each-pair-of-parentheses.js +++ b/1190-reverse-substrings-between-each-pair-of-parentheses.js @@ -24,32 +24,3 @@ const reverseParentheses = function(s) { return res[0] } -// another - -/** - * @param {string} s - * @return {string} - */ -const reverseParentheses = function(s) { - const n = s.length - const stack = [] - const pair = [] - for(let i = 0; i < n; i++) { - if(s[i] === '(') stack.push(i) - else if(s[i] === ')') { - const tmp = stack.pop() - pair[i] = tmp - pair[tmp] = i - } - } - let res = '' - for(let i = 0, d = 1; i < n; i += d) { - if(s[i] === '(' || s[i] ===')') { - i = pair[i] - d = -d - } else { - res += s[i] - } - } - return res -} diff --git a/120-triangle.js b/120-triangle.js index 6f809c2a..c50bb12a 100644 --- a/120-triangle.js +++ b/120-triangle.js @@ -18,25 +18,3 @@ const minimumTotal = function(triangle) { return triangle[0][0]; }; -// another - -/** - * @param {number[][]} triangle - * @return {number} - */ -const minimumTotal = function(triangle) { - const m = triangle.length, n = triangle.at(-1).length - const dp = Array.from({ length: m + 1 }, () => Array(n + 1).fill(Infinity)) - dp[1][1] = triangle[0][0] - for(let i = 2; i <= m; i++) { - for(let j = 1; j <= triangle[i - 1].length; j++) { - if(j === 1) dp[i][j] = dp[i - 1][j] + triangle[i - 1][j - 1] - else dp[i][j] = Math.min(dp[i - 1][j], dp[i - 1][j - 1]) + triangle[i - 1][j - 1] - } - } - let res = Infinity - for (let j = 0; j <= n; j++) { - res = Math.min(res, dp[m][j]) - } - return res -}; diff --git a/1203-sort-items-by-groups-respecting-dependencies.js b/1203-sort-items-by-groups-respecting-dependencies.js index 0894cd98..3f859e18 100644 --- a/1203-sort-items-by-groups-respecting-dependencies.js +++ b/1203-sort-items-by-groups-respecting-dependencies.js @@ -45,99 +45,3 @@ const sortItems = function (n, m, group, beforeItems) { } -// another - - -/** - * @param {number} n - * @param {number} m - * @param {number[]} group - * @param {number[][]} beforeItems - * @return {number[]} - */ -const sortItems = function (n, m, group, beforeItems) { - const vertexs = new Map() - const groupVertexs = new Map() - let groupNo = m - for (let i = 0; i < n; i++) { - vertexs.set(i, { - neighbors: new Set(), - indegree: 0, - }) - if (group[i] === -1) { - group[i] = groupNo++ - } - if (!groupVertexs.has(group[i])) { - groupVertexs.set(group[i], { - v: new Set(), - neighbors: new Set(), - indegree: 0, - }) - } - groupVertexs.get(group[i]).v.add(i) - } - - for (let i = 0; i < n; i++) { - for (const before of beforeItems[i]) { - if (!vertexs.get(before).neighbors.has(i)) { - vertexs.get(i).indegree += 1 - } - vertexs.get(before).neighbors.add(i) - - const groupOfBefore = group[before] - if (groupOfBefore === group[i]) continue - if (!groupVertexs.get(groupOfBefore).neighbors.has(group[i])) { - groupVertexs.get(group[i]).indegree += 1 - } - groupVertexs.get(groupOfBefore).neighbors.add(group[i]) - } - } - - const zeroGroup = [] - for (const group of groupVertexs) { - if (group[1].indegree === 0) { - zeroGroup.push(group[0]) - } - } - const result = [] - let cntGroup = 0 - let cntV = 0 - const groupTotal = groupVertexs.size - - while (zeroGroup.length) { - const top = zeroGroup.pop() - cntGroup += 1 - const v = groupVertexs.get(top).v - const total = v.size - const zero = [] - - for (const i of v) { - if (vertexs.get(i).indegree === 0) { - zero.push(i) - } - } - while (zero.length) { - const it = zero.pop() - result.push(it) - for (const n of vertexs.get(it).neighbors) { - vertexs.get(n).indegree -= 1 - if (v.has(n) && vertexs.get(n).indegree === 0) { - zero.push(n) - } - } - } - if (result.length - cntV !== total) { - return [] - } - cntV = result.length - - for (const groupneigbor of groupVertexs.get(top).neighbors) { - groupVertexs.get(groupneigbor).indegree -= 1 - if (groupVertexs.get(groupneigbor).indegree === 0) { - zeroGroup.push(groupneigbor) - } - } - } - - return cntGroup === groupTotal ? result : [] -} diff --git a/1206-design-skiplist.js b/1206-design-skiplist.js index c559a289..6afa5565 100644 --- a/1206-design-skiplist.js +++ b/1206-design-skiplist.js @@ -59,87 +59,3 @@ class Skiplist { } } -// another - -const Skiplist = function () { - this.maxLvl = ~~Math.log2(20000) - this.levels = [...Array(this.maxLvl)].map(() => new Node(-1)) - for (let i = this.maxLvl - 1; i > 0; i--) { - this.levels[i].down = this.levels[i - 1] - } - this.head = this.levels[this.maxLvl - 1] -} - -/** - * @param {number} target - * @return {boolean} - */ -Skiplist.prototype.search = function (target) { - const pre = this.iter(target) - return !pre[0].next ? false : pre[0].next.val === target -} - -Skiplist.prototype.iter = function (target) { - let cur = this.head - const pre = [] - for (let i = this.maxLvl - 1; i >= 0; i--) { - while (cur.next && cur.next.val < target) cur = cur.next - pre[i] = cur - cur = cur.down - } - return pre -} - -/** - * @param {number} num - * @return {void} - */ -Skiplist.prototype.add = function (num) { - const pre = this.iter(num) - const lvs = decideLevels(this.maxLvl) - for (let i = 0; i < lvs; i++) { - const next = pre[i].next - pre[i].next = new Node(num) - pre[i].next.next = next - if (i > 0) pre[i].next.down = pre[i - 1].next - } -} - -/** - * @param {number} num - * @return {boolean} - */ -Skiplist.prototype.erase = function (num) { - const pre = this.iter(num) - let ret - if (!pre[0].next || pre[0].next.val !== num) return false - for (let i = this.maxLvl - 1; i >= 0; i--) { - if (pre[i].next && pre[i].next.val === num) { - const toBeDeleted = pre[i].next - pre[i].next = toBeDeleted.next - toBeDeleted.next = null - toBeDeleted.down = null - } - } - return true -} - -/** - * Your Skiplist object will be instantiated and called as such: - * var obj = new Skiplist() - * var param_1 = obj.search(target) - * obj.add(num) - * var param_3 = obj.erase(num) - */ - -const decideLevels = (max) => { - let ans = 1 - while (Math.random() > 0.5 && ans < max) ans++ - return ans -} - -const Node = function (val) { - this.val = val - this.next = null - this.down = null -} diff --git a/1209-remove-all-adjacent-duplicates-in-string-ii.js b/1209-remove-all-adjacent-duplicates-in-string-ii.js index e03e4c40..1e494237 100644 --- a/1209-remove-all-adjacent-duplicates-in-string-ii.js +++ b/1209-remove-all-adjacent-duplicates-in-string-ii.js @@ -22,30 +22,3 @@ const removeDuplicates = function (s, k) { return arr.join('') }; -// another - -/** - * @param {string} s - * @param {number} k - * @return {string} - */ -const removeDuplicates = function (s, k) { - const stack = []; - s = s.split(''); - for (let i = 0; i < s.length;) { - if (i === 0 || s[i] !== s[i - 1]) { - stack.push(1); - i++ - } else { - stack[stack.length - 1]++; - if (stack[stack.length - 1] === k) { - stack.pop(); - s.splice(i - k + 1, k); - i = i - k + 1; - } else { - i++ - } - } - } - return s.join(''); -}; diff --git a/121-best-time-to-buy-and-sell-stock.js b/121-best-time-to-buy-and-sell-stock.js index 7a5d1899..20f1f59b 100755 --- a/121-best-time-to-buy-and-sell-stock.js +++ b/121-best-time-to-buy-and-sell-stock.js @@ -15,17 +15,3 @@ const maxProfit = function(prices) { return maxP; }; -// another - -/** - * @param {number[]} prices - * @return {number} - */ -const maxProfit = function(prices) { - let res = 0, maxCur = 0 - for(let i = 1; i < prices.length; i++) { - maxCur = Math.max(0, maxCur + (prices[i] - prices[i - 1])) - res = Math.max(res, maxCur) - } - return res -}; diff --git a/1213-intersection-of-three-sorted-arrays.js b/1213-intersection-of-three-sorted-arrays.js index 3984750c..8a16879f 100644 --- a/1213-intersection-of-three-sorted-arrays.js +++ b/1213-intersection-of-three-sorted-arrays.js @@ -30,23 +30,3 @@ const arraysIntersection = function(arr1, arr2, arr3) { return res }; -// another - -/** - * @param {number[]} arr1 - * @param {number[]} arr2 - * @param {number[]} arr3 - * @return {number[]} - */ -const arraysIntersection = function(arr1, arr2, arr3) { - let a=0,b=0,c=0; - const res = []; - while(a new Array(len).fill(0)) - chk(s, 0, len - 1, cache) - return cache[0][len - 1] <= k -}; - -function chk(s, l, r, cache) { - if(l >= r) return 0 - if(cache[l][r]) return cache[l][r] - let num = 0 - if(s[l] === s[r]) { - num = chk(s, l + 1, r - 1, cache) - } else { - num = 1 + Math.min(chk(s, l, r - 1, cache), chk(s, l + 1, r, cache)) - } - cache[l][r] = num - return num -} - -// another - -const isValidPalindrome = function(s, k) { - const len = s.length - const dp = Array.from({ length: len }, () => new Array(len).fill(0)) - for(let i = len - 1; i >= 0; i--) { - dp[i][i] = 1 - for(let j = i + 1; j < len; j++) { - if(s[i] === s[j]) dp[i][j] = dp[i + 1][j - 1] + 2 - else dp[i][j] = Math.max(dp[i + 1][j], dp[i][j - 1]) - } - } - return len <= dp[0][len - 1] + k -}; - diff --git a/1219-path-with-maximum-gold.js b/1219-path-with-maximum-gold.js index 150b85e8..beec0f2d 100644 --- a/1219-path-with-maximum-gold.js +++ b/1219-path-with-maximum-gold.js @@ -39,45 +39,3 @@ const getMaximumGold = function(grid) { }; -// another - -/** - * @param {number[][]} grid - * @return {number} - */ -var getMaximumGold = function (grid) { - const m = grid.length - const n = grid[0].length - let max = 0 - - for (let i = 0; i < m; i++) { - for (let j = 0; j < n; j++) { - if (grid[i][j] != 0) { - const sum = backtrack(grid, i, j, m, n) - max = Math.max(sum, max) - } - } - } - - return max -} - -function backtrack(grid, row, col, m, n) { - if (outOfBound(row, col, m, n) || grid[row][col] === 0) return 0 - - let sum = grid[row][col] - grid[row][col] = 0 // mark as being visited already - - const top = backtrack(grid, row - 1, col, m, n) - const right = backtrack(grid, row, col + 1, m, n) - const bot = backtrack(grid, row + 1, col, m, n) - const left = backtrack(grid, row, col - 1, m, n) - - grid[row][col] = sum // backtrack to the original form - - return sum + Math.max(top, right, bot, left) -} - -function outOfBound(row, col, m, n) { - return row < 0 || col < 0 || row >= m || col >= n -} diff --git a/1224-maximum-equal-frequency.js b/1224-maximum-equal-frequency.js index 192cc0e6..75522276 100644 --- a/1224-maximum-equal-frequency.js +++ b/1224-maximum-equal-frequency.js @@ -42,31 +42,3 @@ const maxEqualFreq = function (nums) { return res } -// another - -/** - * @param {number[]} nums - * @return {number} - */ -const maxEqualFreq = function (nums) { - const cnt = {}, - freq = {} - let maxF = 0, - res = 0 - nums.forEach((num, i) => { - if (cnt[num] == null) cnt[num] = 0 - cnt[num] += 1 - if (freq[cnt[num] - 1] == null) freq[cnt[num] - 1] = 0 - if (freq[cnt[num]] == null) freq[cnt[num]] = 0 - freq[cnt[num] - 1] -= 1 - freq[cnt[num]] += 1 - maxF = Math.max(maxF, cnt[num]) - if ( - maxF * freq[maxF] === i || - (maxF - 1) * (freq[maxF - 1] + 1) === i || - maxF === 1 - ) - res = i + 1 - }) - return res -} diff --git a/1229-meeting-scheduler.js b/1229-meeting-scheduler.js index 46d4c72e..e8257eb0 100644 --- a/1229-meeting-scheduler.js +++ b/1229-meeting-scheduler.js @@ -49,33 +49,3 @@ const minAvailableDuration = function (slots1, slots2, duration) { } } -// another - -/** - * @param {number[][]} slots1 - * @param {number[][]} slots2 - * @param {number} duration - * @return {number[]} - */ -const minAvailableDuration = function (slots1, slots2, duration) { - slots1.sort((a, b) => a[0] - b[0]) - slots2.sort((a, b) => a[0] - b[0]) - - const m = slots1.length, n = slots2.length - let i = 0, j = 0 - - while(i < m && j < n) { - const start = Math.max(slots1[i][0], slots2[j][0]) - const end = Math.min(slots1[i][1], slots2[j][1]) - - if(end - start >= duration) { - return [start, start + duration] - } - - if(slots1[i][1] > slots2[j][1]) j++ - else i++ - - } - - return [] -} diff --git a/1231-divide-chocolate.js b/1231-divide-chocolate.js index ea4d1768..cdc7a556 100644 --- a/1231-divide-chocolate.js +++ b/1231-divide-chocolate.js @@ -22,33 +22,3 @@ const maximizeSweetness = function (sweetness, K) { return left } -// another - -/** - * @param {number[]} sweetness - * @param {number} K - * @return {number} - */ -const maximizeSweetness = function(sweetness, K) { - let l = 1, r = 10 ** 9 - while(l < r) { - const mid = r - Math.floor((r - l) / 2) - if(valid(mid)) l = mid - else r = mid - 1 - } - return l - - function valid(mid) { - let res = 0, cur = 0 - const n = sweetness.length - for(let i = 0; i < n; i++) { - const e = sweetness[i] - cur += e - if(cur >= mid) { - res++ - cur = 0 - } - } - return res >= K + 1 - } -}; diff --git a/1235-maximum-profit-in-job-scheduling.js b/1235-maximum-profit-in-job-scheduling.js index a5a0fa3b..63c5b28d 100644 --- a/1235-maximum-profit-in-job-scheduling.js +++ b/1235-maximum-profit-in-job-scheduling.js @@ -33,81 +33,3 @@ function binarySearch(arr, l, r, x) { } -// another - -/** - * @param {number[]} startTime - * @param {number[]} endTime - * @param {number[]} profit - * @return {number} - */ -const jobScheduling = function (startTime, endTime, profit) { - const n = startTime.length - const items = Array.from({ length: startTime.length }, () => Array(3).fill(0)) - for (let i = 0; i < startTime.length; i++) { - items[i] = [startTime[i], endTime[i], profit[i]] - } - items.sort((a1, a2) => a1[1] - a2[1]) - const dpProfit = [0] - for (let i = 0; i < n; i++) { - const [s, e, p] = items[i] - let prevIdx = -1 - for(let j = i - 1; j >= 0; j--) { - if(items[j][1] <= items[i][0]) { - prevIdx = j - break - } - } - const curProfit = (prevIdx === -1 ? 0 : dpProfit[prevIdx]) + p - dpProfit[i] = Math.max(dpProfit[dpProfit.length - 1], curProfit) - } - return dpProfit[dpProfit.length - 1] -} - - -// another - - -/** - * @param {number[]} startTime - * @param {number[]} endTime - * @param {number[]} profit - * @return {number} - */ -const jobScheduling = function (startTime, endTime, profit) { - const items = Array.from({ length: startTime.length }, () => Array(3).fill(0)) - for (let i = 0; i < startTime.length; i++) { - items[i] = [startTime[i], endTime[i], profit[i]] - } - items.sort((a1, a2) => a1[1] - a2[1]) - const dpEndTime = [] - const dpProfit = [] - dpEndTime.push(0) - dpProfit.push(0) - for (let item of items) { - const s = item[0], - e = item[1], - p = item[2] - // find previous endTime index - const prevIdx = binarySearch(dpEndTime, 0, dpEndTime.length - 1, s) - const currProfit = dpProfit[prevIdx] + p, - maxProfit = dpProfit[dpProfit.length - 1] - if (currProfit > maxProfit) { - dpProfit.push(currProfit) - dpEndTime.push(e) - } - } - return dpProfit[dpProfit.length - 1] -} - -function binarySearch(arr, l, r, x) { - while (l <= r) { - const mid = l + ((r - l) >> 1) - if (arr[mid] > x) r = mid - 1 - else { - if (mid == arr.length - 1 || arr[mid + 1] > x) return mid - l = mid + 1 - } - } - return -1 -} diff --git a/124-binary-tree-maximum-path-sum.js b/124-binary-tree-maximum-path-sum.js index 68e174a9..5f53f2dc 100644 --- a/124-binary-tree-maximum-path-sum.js +++ b/124-binary-tree-maximum-path-sum.js @@ -26,35 +26,3 @@ function traverse(node, obj) { return node.val + Math.max(left, right) } -// another - -/** - * Definition for a binary tree node. - * function TreeNode(val, left, right) { - * this.val = (val===undefined ? 0 : val) - * this.left = (left===undefined ? null : left) - * this.right = (right===undefined ? null : right) - * } - */ -/** - * @param {TreeNode} root - * @return {number} - */ -const maxPathSum = function(root) { - let res = -Infinity - dfs(root) - return res - - function dfs(node) { - if(node == null) return 0 - let left = dfs(node.left), right = dfs(node.right) - res = Math.max( - res, - node.val, - node.val + left, - node.val + right, - node.val + left + right, - ) - return Math.max(node.val, node.val + left, node.val + right) - } -}; diff --git a/1246-palindrome-removal.js b/1246-palindrome-removal.js index f73084af..74cc2b46 100644 --- a/1246-palindrome-removal.js +++ b/1246-palindrome-removal.js @@ -35,31 +35,3 @@ const minimumMoves = function (arr) { return dp[0][n - 1] } -// another - -/** - * @param {number[]} arr - * @return {number} - */ - const minimumMoves = function (arr) { - const n = arr.length - - const dp = Array.from({ length: n }, () => Array(n).fill(n)) - - for(let i = 0; i < n; i++) dp[i][i] = 1 - for(let i = 0; i < n - 1; i++) { - dp[i][i + 1] = arr[i] === arr[i + 1] ? 1 : 2 - } - - for(let size = 3; size <= n; size++) { - for(let i = 0; i + size - 1 < n; i++) { - const right = i + size - 1 - if(arr[i] === arr[right]) dp[i][right] = dp[i + 1][right - 1] - for(let j = i; j < right; j++) { - dp[i][right] = Math.min(dp[i][right], dp[i][j] + dp[j + 1][right]) - } - } - } - - return dp[0][n - 1] -} diff --git a/1249-minimum-remove-to-make-valid-parentheses.js b/1249-minimum-remove-to-make-valid-parentheses.js index 1599e395..05f31e41 100644 --- a/1249-minimum-remove-to-make-valid-parentheses.js +++ b/1249-minimum-remove-to-make-valid-parentheses.js @@ -19,57 +19,3 @@ return arr.join('') }; -// another - -/** - * @param {string} s - * @return {string} - */ -const minRemoveToMakeValid = function(s) { - let cnt = 0 - let res = s.split('') - // console.log(res) - for(let i = 0; i < res.length; ) { - const ch = res[i] - if(ch === '(') cnt++ - if(ch === ')') cnt-- - if(cnt < 0) { - // console.log(res, i) - res.splice(i, 1) - cnt++ - } else i++ - } - // console.log(res) - let idx = res.length - 1 - while(cnt > 0) { - if(res[idx] === '(') { - res.splice(idx, 1) - cnt-- - } else idx-- - } - return res.join('') -}; - -// another - -/** - * @param {string} s - * @return {string} - */ -const minRemoveToMakeValid = function(s) { - const stk = [], arr = s.split(''), n = s.length - for(let i = 0; i < n; i++) { - if(s[i] === '(') stk.push(i) - if(s[i] === ')') { - if(stk.length && stk[stk.length - 1] >= 0) stk.pop() - else stk.push(-(i + 1)) - } - } - - while(stk.length) { - const tmp = stk.pop() - if(tmp < 0) arr[-tmp - 1] = '' - else arr[tmp] = '' - } - return arr.join('') -}; diff --git a/1254-number-of-closed-islands.js b/1254-number-of-closed-islands.js index acf9f12e..0286e48c 100644 --- a/1254-number-of-closed-islands.js +++ b/1254-number-of-closed-islands.js @@ -37,68 +37,3 @@ const closedIsland = function(grid) { } }; -// another - - -/** - * @param {number[][]} grid - * @return {number} - */ -const closedIsland = function(grid) { - const m = grid.length, n = grid[0].length - const arr = [] - for(let i = 0; i < m; i++) { - for(let j = 0; j < n; j++) { - if(grid[i][j] === 0) arr.push([i, j]) - } - } - const dirs = [[0,1], [0,-1], [1,0], [-1,0]] - let num = 2 - for(const [i, j] of arr) { - if(grid[i][j] !== 0) continue - else { - bfs(i, j, num) - num++ - } - } - - let res = 0 - const set = new Set() - for(let i = 2; i < num; i++) { - set.add(i) - } - - for(let i = 0; i < m; i++) { - for(let j = 0; j < n; j++) { - if(grid[i][j] > 1 && invalid(i, j)) { - set.delete(grid[i][j]) - } - } - } - return set.size - - function invalid(i,j) { - if(i === 0 || i === m - 1 || j === 0 || j === n - 1) return true - return false - } - function bfs(i, j, v) { - let q = [[i,j]] - grid[i][j] = v - while(q.length) { - const tmp = [] - const size = q.length - - for(const [x, y] of q) { - for(const [dx, dy] of dirs) { - const nx = x + dx, ny = y + dy - if(nx >= 0 && nx < m && ny >= 0 && ny < n && grid[nx][ny] === 0) { - grid[nx][ny] = v - tmp.push([nx, ny]) - } - } - } - - q = tmp - } - } -}; diff --git a/1262-greatest-sum-divisible-by-three.js b/1262-greatest-sum-divisible-by-three.js index fac1d5c9..eb2debaf 100644 --- a/1262-greatest-sum-divisible-by-three.js +++ b/1262-greatest-sum-divisible-by-three.js @@ -16,35 +16,3 @@ const maxSumDivThree = function (nums) { return dp[0] } -// another - -/** - * @param {number[]} nums - * @return {number} - */ -const maxSumDivThree = function(nums) { - const sum = nums.reduce((ac, el) => ac + el, 0) - if(sum % 3 === 0) return sum - const remainder = sum % 3 - const comp = 3 - remainder - nums.sort((a, b) => a - b) - const re = [], rc = [] - for(let i = 0, len = nums.length; i < len; i++) { - if(nums[i] % 3 === remainder) { - if(re.length < 1) re.push(i) - } - if(nums[i] % 3 === comp) { - if(rc.length < 2) rc.push(i) - } - if(re.length === 1 && rc.length === 2) break - } - if(re.length === 1 && rc.length === 2) { - return Math.max(sum - nums[re[0]], sum - nums[rc[0]] - nums[rc[1]]) - } else if(re.length === 1) { - return sum - nums[re[0]] - } else if(rc.length === 2) { - return sum - nums[rc[0]] - nums[rc[1]] - } else { - return 0 - } -}; diff --git a/1263-minimum-moves-to-move-a-box-to-their-target-location.js b/1263-minimum-moves-to-move-a-box-to-their-target-location.js index 2ec22d2d..89a210b5 100644 --- a/1263-minimum-moves-to-move-a-box-to-their-target-location.js +++ b/1263-minimum-moves-to-move-a-box-to-their-target-location.js @@ -91,372 +91,3 @@ const minPushBox = function (grid) { return -1 } -// another - - -/** - * @param {character[][]} grid - * @return {number} - */ -const minPushBox = function (grid) { - if ( - typeof grid === 'undefined' || - grid === null || - grid.length === 0 || - grid[0].length === 0 - ) { - return -1 - } - - let TARGET = null - let startBlk = null - let startPer = null - const DIR = [ - [0, 1], - [1, 0], - [0, -1], - [-1, 0], - ] - - for (let i = 0; i < grid.length; i++) { - for (let j = 0; j < grid[0].length; j++) { - if (grid[i][j] === 'S') { - startPer = [i, j] - grid[i][j] = '.' - } - if (grid[i][j] === 'T') { - TARGET = [i, j] - } - if (grid[i][j] === 'B') { - startBlk = [i, j] - grid[i][j] = '.' - } - } - } - - let queue = new PriorityQueue((a, b) => a.weight < b.weight) - let states = new Map() - queue.push({ - weight: manDist(startBlk), - block: startBlk, - character: startPer, - move: 0, - }) - while (!queue.isEmpty()) { - let { weight, block, character, move } = queue.pop() - if (TARGET[0] === block[0] && TARGET[1] === block[1]) { - return move - } - let key = block[0] * grid[0].length + block[1] - let val = character[0] * grid[0].length + character[1] - if (!states.has(key)) { - states.set(key, new Set()) - } - states.get(key).add(val) - DIR.forEach((d) => { - let i = d[0] + character[0] - let j = d[1] + character[1] - let curV = i * grid[0].length + j - if (validMove(i, j, block[0], block[1]) && !states.get(key).has(curV)) { - queue.push({ - weight: manDist(block) + move, - block: block, - character: [i, j], - move: move, - }) - } - }) - let pushDir = tryPush(character, block) - if (pushDir !== null) { - let newBlk = [block[0] + pushDir[0], block[1] + pushDir[1]] - let newCha = [character[0] + pushDir[0], character[1] + pushDir[1]] - let nBK = newBlk[0] * grid[0].length + newBlk[1] - let nVal = newCha[0] * grid[0].length + newCha[1] - if (!states.has(nBK) || !states.get(nBK).has(nVal)) { - queue.push({ - weight: manDist(newBlk) + (move + 1), - block: newBlk, - character: newCha, - move: move + 1, - }) - } - } - } - - return -1 - - function manDist(block) { - let [x, y] = TARGET - let [i, j] = block - return Math.abs(x - i) + Math.abs(y - j) - } - function validMove(i, j, x = null, y = null) { - if (i < 0 || j < 0 || i >= grid.length || j >= grid[0].length) { - return false - } - if ( - (x !== null && i === x && y !== null && j === y) || - grid[i][j] === '#' - ) { - return false - } - return true - } - function tryPush(c, b) { - let [i, j] = c - let [x, y] = b - for (let u = 0; u < DIR.length; u++) { - let [v, w] = DIR[u] - if ( - ((Math.abs(x - i) === 1 && y === j) || - (Math.abs(y - j) === 1 && x === i)) && - validMove(i + v, j + w) && - validMove(x + v, y + w) && - i + v === x && - j + w === y - ) { - return [v, w] - } - } - return null - } -} -class PriorityQueue { - constructor(comparator = (a, b) => a > b) { - this.heap = [] - this.top = 0 - this.comparator = comparator - } - size() { - return this.heap.length - } - isEmpty() { - return this.size() === 0 - } - peek() { - return this.heap[this.top] - } - push(...values) { - values.forEach((value) => { - this.heap.push(value) - this.siftUp() - }) - return this.size() - } - pop() { - const poppedValue = this.peek() - const bottom = this.size() - 1 - if (bottom > this.top) { - this.swap(this.top, bottom) - } - this.heap.pop() - this.siftDown() - return poppedValue - } - replace(value) { - const replacedValue = this.peek() - this.heap[this.top] = value - this.siftDown() - return replacedValue - } - - parent = (i) => ((i + 1) >>> 1) - 1 - left = (i) => (i << 1) + 1 - right = (i) => (i + 1) << 1 - greater = (i, j) => this.comparator(this.heap[i], this.heap[j]) - swap = (i, j) => ([this.heap[i], this.heap[j]] = [this.heap[j], this.heap[i]]) - siftUp = () => { - let node = this.size() - 1 - while (node > this.top && this.greater(node, this.parent(node))) { - this.swap(node, this.parent(node)) - node = this.parent(node) - } - } - siftDown = () => { - let node = this.top - while ( - (this.left(node) < this.size() && this.greater(this.left(node), node)) || - (this.right(node) < this.size() && this.greater(this.right(node), node)) - ) { - let maxChild = - this.right(node) < this.size() && - this.greater(this.right(node), this.left(node)) - ? this.right(node) - : this.left(node) - this.swap(node, maxChild) - node = maxChild - } - } -} - -// another - -/** - * @param {character[][]} grid - * @return {number} - */ -const minPushBox = function (grid) { - const dirs = [ - [-1, 0], - [1, 0], - [0, -1], - [0, 1], - ] - const dis = new Map() - const rows = grid.length - const cols = grid[0].length - let sk, box, target - for (let i = 0; i < rows; i++) { - for (let j = 0; j < cols; j++) { - if (grid[i][j] === 'B') box = [i, j] - else if (grid[i][j] === 'S') sk = [i, j] - else if (grid[i][j] === 'T') target = [i, j] - } - } - const q = [] - const start = encode(box[0], box[1], sk[0], sk[1]) - dis.set(start, 0) - q.push(start) - let res = Number.MAX_VALUE - while (q.length) { - const u = q.pop() - const du = decode(u) - if (dis.get(u) >= res) continue - if (du[0] === target[0] && du[1] === target[1]) { - res = Math.min(res, dis.get(u)) - continue - } - const b = [du[0], du[1]] - const s = [du[2], du[3]] - for (let dir of dirs) { - const nsx = s[0] + dir[0] - const nsy = s[1] + dir[1] - if ( - nsx < 0 || - nsx >= rows || - nsy < 0 || - nsy >= cols || - grid[nsx][nsy] === '#' - ) - continue - if (nsx === b[0] && nsy === b[1]) { - const nbx = b[0] + dir[0] - const nby = b[1] + dir[1] - if ( - nbx < 0 || - nbx >= rows || - nby < 0 || - nby >= cols || - grid[nbx][nby] === '#' - ) - continue - const v = encode(nbx, nby, nsx, nsy) - if (dis.has(v) && dis.get(v) <= dis.get(u) + 1) continue - dis.set(v, dis.get(u) + 1) - q.push(v) - } else { - const v = encode(b[0], b[1], nsx, nsy) - if (dis.has(v) && dis.get(v) <= dis.get(u)) continue - dis.set(v, dis.get(u)) - q.push(v) - } - } - } - return res === Number.MAX_VALUE ? -1 : res - - function encode(bx, by, sx, sy) { - return (bx << 24) | (by << 16) | (sx << 8) | sy - } - function decode(num) { - const res = [] - res[0] = (num >>> 24) & 0xff - res[1] = (num >>> 16) & 0xff - res[2] = (num >>> 8) & 0xff - res[3] = num & 0xff - return res - } -} - -// another - -/** - * @param {character[][]} grid - * @return {number} - */ - const minPushBox = function (grid) { - const m = grid.length, - n = grid[0].length - let target, person, box - for (let i = 0; i < m; i++) { - for (let j = 0; j < n; j++) { - if (grid[i][j] === 'T') target = [i, j] - else if (grid[i][j] === 'B') box = [i, j] - else if (grid[i][j] === 'S') person = [i, j] - } - } - - const valid = ([x, y]) => { - return x >= 0 && x < m && y >= 0 && y < n && grid[x][y] !== '#' - } - - const check = (cur, dest, box) => { - const q = [cur] - const visited = new Set([`${box[0]},${box[1]}`]) - const dirs = [ - [-1, 0], - [1, 0], - [0, 1], - [0, -1], - ] - - while (q.length) { - const pos = q.shift() - if (pos.join(',') === dest.join(',')) return true - const newPos = [] - for (const [dx, dy] of dirs) newPos.push([pos[0] + dx, pos[1] + dy]) - for (const [nx, ny] of newPos) { - const k = `${nx},${ny}` - if (valid([nx, ny]) && !visited.has(k)) { - visited.add(k) - q.push([nx, ny]) - } - } - } - - return false - } - - const q = [[0, box, person]] - const vis = new Set([`${box.join(',')},${person.join(',')}`]) - while (q.length) { - const [dist, box, person] = q.shift() - if (box.join(',') === target.join(',')) return dist - - const bCoord = [ - [box[0] + 1, box[1]], - [box[0] - 1, box[1]], - [box[0], box[1] + 1], - [box[0], box[1] - 1], - ] - const pCoord = [ - [box[0] - 1, box[1]], - [box[0] + 1, box[1]], - [box[0], box[1] - 1], - [box[0], box[1] + 1], - ] - - for (let i = 0; i < 4; i++) { - const [newBox, newPerson] = [bCoord[i], pCoord[i]] - const key = `${newBox.join(',')},${box.join(',')}` - if (valid(newBox) && !vis.has(key)) { - if (valid(newPerson) && check(person, newPerson, box)) { - vis.add(key) - q.push([dist + 1, newBox, box]) - } - } - } - } - - return -1 -} - diff --git a/1268-search-suggestions-system.js b/1268-search-suggestions-system.js index 2e8ea666..3db20ab7 100644 --- a/1268-search-suggestions-system.js +++ b/1268-search-suggestions-system.js @@ -16,91 +16,3 @@ const suggestedProducts = function(products, searchWord) { return res }; -// another - -/** - * @param {string[]} products - * @param {string} searchWord - * @return {string[][]} - */ -const suggestedProducts = function(products, searchWord) { - const res = [] - for(let i = 0, n = searchWord.length; i < n; i++) { - const tmp = [], pre = searchWord.slice(0, i + 1) - for(const e of products) { - if(e.startsWith(pre)) { - tmp.push(e) - tmp.sort((a, b) => a.localeCompare(b)) - if(tmp.length > 3) tmp.pop() - } - } - res.push(tmp) - } - return res -}; - -// another - -/** - * @param {string[]} products - * @param {string} searchWord - * @return {string[][]} - */ - const suggestedProducts = function (products, searchWord) { - products.sort() - const root = new Node() - for (const str of products) { - addProduct(str) - } - - const res = [] - - let cur = root - for (const ch of searchWord) { - const tmp = [] - if (cur == null) { - res.push(tmp) - continue - } - const map = cur.children.get(ch) - if (map != null) { - addThree(map.words.values(), tmp) - } - - res.push(tmp) - cur = map - } - - return res - - function addThree(it, arr) { - - for(let i = 0; i < 3; i++) { - const res = it.next() - if(res.value) arr.push(res.value) - } - } - - function addProduct(str) { - let cur = root - for (const ch of str) { - let next = cur.children.get(ch) - if (next == null) { - next = new Node() - cur.children.set(ch, next) - } - next.words.add(str) - cur = next - } - cur.isWord = true - } -} - -class Node { - constructor() { - this.children = new Map() - this.words = new Set() - this.isWord = false - } -} - diff --git a/1275-find-winner-on-a-tic-tac-toe-game.js b/1275-find-winner-on-a-tic-tac-toe-game.js index d6029d14..dba6032a 100644 --- a/1275-find-winner-on-a-tic-tac-toe-game.js +++ b/1275-find-winner-on-a-tic-tac-toe-game.js @@ -48,23 +48,3 @@ function chk(ch, grid) { return false } -// another - -/** - * @param {number[][]} moves - * @return {string} - */ -const tictactoe = function(moves) { - const aRow = Array(3).fill(0), aCol = Array(3).fill(0), bRow= Array(3).fill(0), bCol =Array(3).fill(0) - let ad = 0, ads = 0, bd = 0, bds = 0 - for(let i = 0; i < moves.length; i++) { - const [r, c] = moves[i] - if(i % 2===0) { - if(++aRow[r] === 3 || ++aCol[c] === 3 || r === c && ++ad === 3 || r + c === 2&& ++ads === 3 ) return 'A' - }else { - if(++bRow[r] === 3 || ++bCol[c] === 3 || r === c && ++bd === 3 || r + c === 2&& ++bds === 3 ) return 'B' - } - } - - return moves.length >= 9 ? 'Draw' : 'Pending' -}; diff --git a/1277-count-square-submatrices-with-all-ones.js b/1277-count-square-submatrices-with-all-ones.js index 61494fcb..2a792cf7 100644 --- a/1277-count-square-submatrices-with-all-ones.js +++ b/1277-count-square-submatrices-with-all-ones.js @@ -20,22 +20,3 @@ const countSquares = function (matrix) { return res } -// another - -/** - * @param {number[][]} matrix - * @return {number} - */ -const countSquares = function (A) { - const [M, N] = [A.length, A[0].length] - let ans = 0 - for (let i = 0; i < M; ++i) { - for (let j = 0; j < N; ++j) { - if (A[i][j] && i > 0 && j > 0) { - A[i][j] = 1 + Math.min(A[i - 1][j], A[i][j - 1], A[i - 1][j - 1]) - } - ans += A[i][j] - } - } - return ans -} diff --git a/1284-minimum-number-of-flips-to-convert-binary-matrix-to-zero-matrix.js b/1284-minimum-number-of-flips-to-convert-binary-matrix-to-zero-matrix.js index a79ab5e6..8d2a3fdf 100644 --- a/1284-minimum-number-of-flips-to-convert-binary-matrix-to-zero-matrix.js +++ b/1284-minimum-number-of-flips-to-convert-binary-matrix-to-zero-matrix.js @@ -40,64 +40,3 @@ const minFlips = function (mat) { return -1 } -// another - -/** - * @param {number[][]} mat - * @return {number} - */ -const minFlips = function (mat) { - const X = mat.length - const Y = mat[0].length - const binary = { - 0: 1, - 1: 2, - 2: 4, - 3: 8, - 4: 16, - 5: 32, - 6: 64, - 7: 128, - 8: 256, - } - const mask = [] - let state = 0 - for (let i = 0; i < X; ++i) { - for (let j = 0; j < Y; ++j) { - let bit = 0 - state += mat[i][j] * binary[Y * i + j] - bit += binary[Y * i + j] - if (i > 0) { - bit += binary[Y * (i - 1) + j] - } - if (i < X - 1) { - bit += binary[Y * (i + 1) + j] - } - if (j > 0) { - bit += binary[Y * i + (j - 1)] - } - if (j < Y - 1) { - bit += binary[Y * i + (j + 1)] - } - mask.push(bit) - } - } - if (state === 0) return 0 - const set = new Set() - const q = [{ state: state, moves: 0 }] - while (q.length !== 0) { - const cur = q.shift() - if (cur.state === 0) { - return cur.moves - } - for (let i = 0; i < X * Y; ++i) { - let newState = cur.state - newState ^= mask[i] - if (!set.has(newState)) { - set.add(newState) - q.push({ state: newState, moves: cur.moves + 1 }) - } - } - } - return -1 -} diff --git a/1286-iterator-for-combination.js b/1286-iterator-for-combination.js index 314526bf..784039cb 100644 --- a/1286-iterator-for-combination.js +++ b/1286-iterator-for-combination.js @@ -42,107 +42,3 @@ function build(max, str, out = [], curr = '') { return out } -// another - -/** - * @param {string} characters - * @param {number} combinationLength - */ -const CombinationIterator = function(characters, combinationLength) { - const res = [], len = combinationLength, str = characters, n = str.length - helper([], 0) - this.arr = res - this.idx = 0 - - function helper(cur, idx) { - if(cur.length === len) { - res.push(cur.slice().join('')) - return - } - if(idx >= n) return - - cur.push(str[idx]) - helper(cur, idx + 1) - cur.pop() - - helper(cur, idx + 1) - } -}; - -/** - * @return {string} - */ -CombinationIterator.prototype.next = function() { - if(this.hasNext()) { - return this.arr[this.idx++] - } -}; - -/** - * @return {boolean} - */ -CombinationIterator.prototype.hasNext = function() { - return this.arr[this.idx] != null -}; - -/** - * Your CombinationIterator object will be instantiated and called as such: - * var obj = new CombinationIterator(characters, combinationLength) - * var param_1 = obj.next() - * var param_2 = obj.hasNext() - */ - -// another - -/** - * @param {string} characters - * @param {number} combinationLength - */ -const CombinationIterator = function(characters, combinationLength) { - const res = [], len = combinationLength, str = characters, n = str.length - helper() - - // console.log(res) - this.arr = res - this.idx = 0 - - function helper() { - const limit = 1 << n - for(let i = limit - 1; i > 0; i--) { - let tmp = i, ts = '', idx = n - 1 - while(tmp) { - if(tmp & 1) { - ts = str[idx] + ts - } - idx-- - tmp = (tmp >> 1) - } - if(ts.length === len) res.push(ts) - } - } -}; - -/** - * @return {string} - */ -CombinationIterator.prototype.next = function() { - if(this.hasNext()) { - return this.arr[this.idx++] - } -}; - -/** - * @return {boolean} - */ -CombinationIterator.prototype.hasNext = function() { - return this.arr[this.idx] != null -}; - -/** - * Your CombinationIterator object will be instantiated and called as such: - * var obj = new CombinationIterator(characters, combinationLength) - * var param_1 = obj.next() - * var param_2 = obj.hasNext() - */ - - diff --git a/1291-sequential-digits.js b/1291-sequential-digits.js index 3cd7049b..c8ee3ae6 100644 --- a/1291-sequential-digits.js +++ b/1291-sequential-digits.js @@ -29,43 +29,3 @@ const sequentialDigits = function(low, high) { return res }; -// another - -/** - * @param {number} low - * @param {number} high - * @return {number[]} - */ -const sequentialDigits = function(low, high) { - const set = new Set() - let start = 0, end = 0 - for(let i = 10; i >= 0; i--) { - if (low / (10 ** i) >= 1) { - start = ~~(low / (10 ** i)) - break - } - } - for(let i = 10; i >= 0; i--) { - if (high / (10 ** i) >= 1) { - end = ~~(high / (10 ** i)) - break - } - } - for(let i = 1; i <= 9; i++) { - helper(`${i}`) - } - - const res = Array.from(set) - res.sort((a, b) => a- b) - return res - - function helper(s) { - // console.log(s) - if(+s > high) return - if(+s >= low && +s <= high) { - set.add(+s) - } - if(s[s.length - 1] === '9') return - helper(`${s}${+s[s.length - 1] + 1}`) - } -}; diff --git a/13-roman-to-integer.js b/13-roman-to-integer.js index b3dd3be3..0a29c3c2 100644 --- a/13-roman-to-integer.js +++ b/13-roman-to-integer.js @@ -34,24 +34,3 @@ const romanToInt = function(s) { } -// another - -/** - * @param {string} s - * @return {number} - */ -const romanToInt = function(s) { - const map = { I: 1, V: 5, X: 10, L: 50, C: 100, D: 500, M: 1000 } - let result = 0 - let index = s.length - 1 - let preInt = 0 - while (index >= 0) { - let ch = s[index] - let curInt = map[ch] - if (curInt >= preInt) result += curInt - else result -= curInt - preInt = curInt - index-- - } - return result -} diff --git a/130-surrounded-regions.js b/130-surrounded-regions.js index 619240b1..20c4187b 100644 --- a/130-surrounded-regions.js +++ b/130-surrounded-regions.js @@ -32,63 +32,3 @@ function search(board, i, j) { search(board, i, j - 1); } -// another - -/** - * @param {character[][]} board - * @return {void} Do not return anything, modify board in-place instead. - */ -const solve = (board) => { - if (!board || board.length === 0 || board[0].length === 0) return; - const n = board.length; - const m = board[0].length; - const dirs = [ - [-1, 0], - [1, 0], - [0, -1], - [0, 1], - ]; - const bfs = (board, n, m, i, j) => { - const queue = []; - queue.push([i, j]); - board[i][j] = "1"; - while (queue.length > 0) { - const pos = queue.shift(); - for (let k = 0; k < 4; k++) { - i = pos[0] + dirs[k][0]; - j = pos[1] + dirs[k][1]; - if (i >= 0 && i < n && j >= 0 && j < m && board[i][j] === "O") { - board[i][j] = "1"; - queue.push([i, j]); - } - } - } - }; - // scan the borders and mark the 'O's to '1' - for (let i = 0; i < n; i++) { - for (let j = 0; j < m; j++) { - if ( - (i === 0 || i === n - 1 || j === 0 || j === m - 1) && - board[i][j] === "O" - ) { - bfs(board, n, m, i, j); - } - } - } - // scan the inner area and mark the 'O's to 'X' - for (let i = 1; i < n; i++) { - for (let j = 1; j < m; j++) { - if (board[i][j] === "O") { - board[i][j] = "X"; - } - } - } - // reset all the '1's to 'O's - for (let i = 0; i < n; i++) { - for (let j = 0; j < m; j++) { - if (board[i][j] === "1") { - board[i][j] = "O"; - } - } - } -}; diff --git a/1301-number-of-paths-with-max-score.js b/1301-number-of-paths-with-max-score.js index 35b84b7c..2576c1b7 100644 --- a/1301-number-of-paths-with-max-score.js +++ b/1301-number-of-paths-with-max-score.js @@ -37,45 +37,3 @@ const pathsWithMaxScore = ( return [S[N][N], P[N][N]] } -// another - -/** - * @param {string[]} board - * @return {number[]} - */ -const pathsWithMaxScore = ( - board, - DIRS = [ - [-1, -1], - [-1, 0], - [0, -1], - ], - mod = 1e9 + 7 -) => { - const m = board.length, - n = board[0].length - const dpSum = Array.from({ length: m }, () => Array(n).fill(0)) - const dpCnt = Array.from({ length: m }, () => Array(n).fill(0)) - dpCnt[m - 1][n - 1] = 1 // start at the bottom right square - for (let r = m - 1; r >= 0; r--) { - for (let c = n - 1; c >= 0; c--) { - if (dpCnt[r][c] === 0) continue // can't reach to this square - for (let dir of DIRS) { - let nr = r + dir[0], - nc = c + dir[1] - if (nr >= 0 && nc >= 0 && board[nr].charAt(nc) !== 'X') { - let nsum = dpSum[r][c] - if (board[nr].charAt(nc) !== 'E') nsum += board[nr].charAt(nc) - '0' - if (nsum > dpSum[nr][nc]) { - dpCnt[nr][nc] = dpCnt[r][c] - dpSum[nr][nc] = nsum - } else if (nsum === dpSum[nr][nc]) { - dpCnt[nr][nc] = (dpCnt[nr][nc] + dpCnt[r][c]) % mod - } - } - } - } - } - return [dpSum[0][0], dpCnt[0][0]] -} - diff --git a/1306-jump-game-iii.js b/1306-jump-game-iii.js index d3abea41..6a3b8fbe 100644 --- a/1306-jump-game-iii.js +++ b/1306-jump-game-iii.js @@ -16,42 +16,3 @@ function helper(arr, start, s) { return helper(arr, start + arr[start], s) || helper(arr, start - arr[start], s) } -// another - -/** - * @param {number[]} arr - * @param {number} start - * @return {boolean} - */ -const canReach = function (A, i) { - return ( - 0 <= i && - i < A.length && - A[i] >= 0 && - (!(A[i] = -A[i]) || canReach(A, i + A[i]) || canReach(A, i - A[i])) - ) -} - -// another - -/** - * @param {number[]} arr - * @param {number} start - * @return {boolean} - */ -const canReach = function(arr, start) { - const q = [start] - const s = new Set() - while(q.length) { - const len = q.length - for(let i = 0; i < len; i++) { - const cur = q.shift() - s.add(cur) - if(arr[cur] === 0) return true - if(!s.has(cur + arr[cur])) q.push(cur + arr[cur]) - if(!s.has(cur - arr[cur])) q.push(cur - arr[cur]) - } - } - return false -}; - diff --git a/131-palindrome-partitioning.js b/131-palindrome-partitioning.js index 56ebb6cb..9646104a 100644 --- a/131-palindrome-partitioning.js +++ b/131-palindrome-partitioning.js @@ -32,39 +32,3 @@ function isPalindrome(str, start, i) { return true } -// another - -/** - * @param {string} s - * @return {string[][]} - */ -const partition = function(s) { - const res = [] - helper(s, 0, [], res) - return res -}; - -function helper(str, idx, cur, res) { - if(idx >= str.length) { - res.push(cur.slice()) - return - } - for(let i = idx, len = str.length; i < len; i++) { - const tmp = str.slice(idx, i + 1) - if(chk(tmp)) { - cur.push(tmp) - helper(str, i + 1, cur, res) - cur.pop() - } - } -} -function chk(str) { - const n = str.length - let l = 0, r = n - 1 - while(l < r) { - if(str[l] !== str[r]) return false - l++ - r-- - } - return true -} diff --git a/1310-xor-queries-of-a-subarray.js b/1310-xor-queries-of-a-subarray.js index e2b63cb2..dbee784a 100644 --- a/1310-xor-queries-of-a-subarray.js +++ b/1310-xor-queries-of-a-subarray.js @@ -18,25 +18,3 @@ const xorQueries = function(arr, queries) { return res }; -// another - -/** - * @param {number[]} arr - * @param {number[][]} queries - * @return {number[]} - */ -const xorQueries = function(arr, queries) { - const xorArr = [] - xorArr[0] = 0 - const n = arr.length - for(let i = 0; i < n; i++) { - const cur = arr[i] - xorArr.push(cur ^ xorArr[xorArr.length - 1]) - } - const res = [] - for(const [l, r] of queries) { - res.push(xorArr[r + 1] ^ xorArr[l]) - } - - return res -}; diff --git a/132-palindrome-partitioning-ii.js b/132-palindrome-partitioning-ii.js index 841a5b4c..53b7a369 100644 --- a/132-palindrome-partitioning-ii.js +++ b/132-palindrome-partitioning-ii.js @@ -31,25 +31,3 @@ const minCut = function(s) { -// another - -const minCut = function(s) { - const n = s.length - const cut = new Array(n + 1).fill(0) - for (let i = 0; i <= n; i++) cut[i] = i - 1 - for (let i = 0; i < n; i++) { - // odd - for (let j = 0; i + j < n && i - j >= 0 && s[i + j] === s[i - j]; j++) { - cut[i + j + 1] = Math.min(cut[i + j + 1], cut[i - j] + 1) - } - // even - for ( - let j = 1; - i + j < n && i - j + 1 >= 0 && s[i + j] === s[i - j + 1]; - j++ - ) { - cut[i + j + 1] = Math.min(cut[i + j + 1], cut[i - j + 1] + 1) - } - } - return cut[n] -} diff --git a/1325-delete-leaves-with-a-given-value.js b/1325-delete-leaves-with-a-given-value.js index 33e8a0d9..cab4211a 100644 --- a/1325-delete-leaves-with-a-given-value.js +++ b/1325-delete-leaves-with-a-given-value.js @@ -27,23 +27,3 @@ function dfs(node, target) { return node } -// another - -/** - * Definition for a binary tree node. - * function TreeNode(val, left, right) { - * this.val = (val===undefined ? 0 : val) - * this.left = (left===undefined ? null : left) - * this.right = (right===undefined ? null : right) - * } - */ -/** - * @param {TreeNode} root - * @param {number} target - * @return {TreeNode} - */ -const removeLeafNodes = function(root, target) { - if(root.left) root.left = removeLeafNodes(root.left, target) - if(root.right) root.right = removeLeafNodes(root.right, target) - return root.left == root.right && root.val === target ? null : root -}; diff --git a/1326-minimum-number-of-taps-to-open-to-water-a-garden.js b/1326-minimum-number-of-taps-to-open-to-water-a-garden.js index e228bae1..61aaf57c 100644 --- a/1326-minimum-number-of-taps-to-open-to-water-a-garden.js +++ b/1326-minimum-number-of-taps-to-open-to-water-a-garden.js @@ -24,18 +24,3 @@ const minTaps = function (n, ranges) { return count } -// another - -/** - * @param {number} n - * @param {number[]} ranges - * @return {number} - */ -const minTaps = function (n, ranges) { - const dp = new Array(n + 1).fill(n + 2) - dp[0] = 0 - for (let i = 0; i <= n; ++i) - for (let j = Math.max(i - ranges[i] + 1, 0); j <= Math.min(i + ranges[i], n); ++j) - dp[j] = Math.min(dp[j], dp[Math.max(0, i - ranges[i])] + 1) - return dp[n] < n + 2 ? dp[n] : -1 -} diff --git a/134-gas-station.js b/134-gas-station.js index a7dc4e04..cf96bb2d 100644 --- a/134-gas-station.js +++ b/134-gas-station.js @@ -18,22 +18,3 @@ const canCompleteCircuit = function(gas, cost) { return total < 0 ? -1 : curtIdx } -// another - -const canCompleteCircuit = function(gas, cost) { - const len = gas.length - let tank = 0 - let count = 0 - for (let i = 0; i < len * 2; i++) { - let idx = i % len - if (count === len) return idx - count += 1 - tank += gas[idx] - cost[idx] - if (tank < 0) { - tank = 0 - count = 0 - } - } - return -1 -} - diff --git a/1340-jump-game-v.js b/1340-jump-game-v.js index 60945fdd..9b407558 100644 --- a/1340-jump-game-v.js +++ b/1340-jump-game-v.js @@ -24,69 +24,3 @@ const maxJumps = function (arr, d, res = 1) { } -// another - -/** - * @param {number[]} arr - * @param {number} d - * @return {number} - */ -const maxJumps = function (arr, d) { - const cache = new Array(arr.length) - const diffs = [1, -1] - const dfs = (i) => { - if (cache[i]) return cache[i] - let max = 0 - for (let diff of diffs) { - for (let j = diff; Math.abs(j) <= d; j += diff) { - const nextPosition = i + j - const isValidJump = - nextPosition >= 0 && - nextPosition < arr.length && - arr[i] > arr[nextPosition] - if (isValidJump) max = Math.max(max, dfs(nextPosition)) - else break - } - } - const result = max + 1 - cache[i] = result - return result - } - for (let i = 0; i < arr.length; i++) dfs(i) - return Math.max(...cache) -} - -// another - -/** - * @param {number[]} arr - * @param {number} d - * @return {number} - */ -const maxJumps = function (arr, d, res = 0) { - const n = arr.length - const stack = [], stack2 = [] - const dp = Array(n + 1).fill(1) - arr.push(Infinity) - for(let i = 0; i <= n; i++) { - while(stack.length && arr[stack[stack.length - 1]] < arr[i]) { - const pre = arr[stack[stack.length - 1]] - while(stack.length && pre === arr[stack[stack.length - 1]]) { - const j = stack[stack.length - 1] - stack.pop() - if(i - j <= d) dp[i] = Math.max(dp[i], dp[j] + 1) - stack2.push(j) - } - while(stack2.length) { - const j = stack2[stack2.length - 1] - stack2.pop() - if(stack.length && j - stack[stack.length - 1] <= d) { - dp[stack[stack.length - 1]] = Math.max(dp[stack[stack.length - 1]], dp[j] + 1) - } - } - } - stack.push(i) - } - for(let i = 0; i < n; i++) res = Math.max(res, dp[i]) - return res -} diff --git a/1345-jump-game-iv.js b/1345-jump-game-iv.js index 5e67c588..fee38216 100644 --- a/1345-jump-game-iv.js +++ b/1345-jump-game-iv.js @@ -44,44 +44,3 @@ var minJumps = function (arr) { return -1 } -// another - -/** - * @param {number[]} arr - * @return {number} - */ -const minJumps = function (arr) { - if (arr.length === 1) return 0 - const n = arr.length - const indexMap = new Map() - for (let i = n - 1; i >= 0; i--) { - if (!indexMap.has(arr[i])) { - indexMap.set(arr[i], []) - } - indexMap.get(arr[i]).push(i) - } - let distance = 0 - const queue = [0] - const visited = new Set() - visited.add(0) - while (queue.length) { - const len = queue.length - for(let i = 0; i < len; i++) { - const cur = queue.shift() - if(cur === n - 1) return distance - const tmp = indexMap.get(arr[cur]) - tmp.push(cur - 1) - tmp.push(cur + 1) - for(let e of tmp) { - if(e >= 0 && e < n && !visited.has(e)) { - visited.add(e) - queue.push(e) - } - } - indexMap.set(arr[cur], []) - } - distance++ - } - return -1 -} - diff --git a/1349-maximum-students-taking-exam.js b/1349-maximum-students-taking-exam.js index 0a45d165..0293b9e2 100644 --- a/1349-maximum-students-taking-exam.js +++ b/1349-maximum-students-taking-exam.js @@ -51,90 +51,3 @@ const maxStudents = function(seats) { } }; -// another - - -/** - * @param {character[][]} seats - * @return {number} - */ -const maxStudents = function (seats) { - if (!seats.length) return 0 - const lastPos = 1 << seats[0].length - const classroom = seats.map((row) => - row.reduce((a, c, i) => (c === '#' ? a : a | (1 << i)), 0) - ) - const dp = new Array(seats.length + 1).fill(null).map(() => new Map()) - dp[0].set(0, 0) - for (let row = 0; row < seats.length; row++) { - let queue = [0] - let numStudents = 0 - while (queue.length > 0) { - const next = [] - for (let arrangement of queue) { - let max = 0 - for (let [prevArrang, count] of dp[row]) { - if (conflicts(prevArrang, arrangement)) continue - max = Math.max(max, count + numStudents) - } - dp[row + 1].set(arrangement, max) - for (let i = 1; i < lastPos; i <<= 1) { - if (canSit(classroom[row], arrangement, i)) next.push(arrangement | i) - } - } - queue = next - numStudents++ - } - } - return Math.max(...dp[seats.length].values()) -} -function conflicts(prev, curr) { - return prev & (curr << 1) || prev & (curr >> 1) -} -function canSit(row, arrangement, newStudent) { - return ( - row & newStudent && - !(arrangement & newStudent) && - !(arrangement & (newStudent << 1)) && - !(arrangement & (newStudent >> 1)) - ) -} - -// another - -/** - * @param {character[][]} seats - * @return {number} - */ -const maxStudents = function (seats) { - const m = seats.length - const n = seats[0].length - const validity = [] - for (let i = 0; i < m; i++) { - let cur = 0 - for (let j = 0; j < n; j++) { - cur = (cur << 1) + (seats[i][j] === '.' ? 1 : 0) - } - validity.push(cur) - } - const f = Array.from({ length: m + 1 }, () => Array(1 << n).fill(-1)) - f[0][0] = 0 - for (let i = 1; i <= m; i++) { - const valid = validity[i - 1] - for (let j = 0; j < 1 << n; j++) { - if ((j & valid) === j && !(j & (j >> 1))) { - for (let k = 0; k < 1 << n; k++) { - if (!(j & (k >> 1)) && !((j >> 1) & k) && f[i - 1][k] !== -1) { - f[i][j] = Math.max(f[i][j], f[i - 1][k] + bitCount(j)) - } - } - } - } - } - return Math.max(...f[m]) -} -function bitCount(n) { - const res = n.toString(2).match(/1/g) - return res === null ? 0 : res.length -} - diff --git a/1353-maximum-number-of-events-that-can-be-attended.js b/1353-maximum-number-of-events-that-can-be-attended.js index f0cdc48d..d6d52d89 100644 --- a/1353-maximum-number-of-events-that-can-be-attended.js +++ b/1353-maximum-number-of-events-that-can-be-attended.js @@ -94,155 +94,3 @@ function maxEvents(events) { } -// another - -class PriorityQueue { - constructor(comparator = (a, b) => a > b) { - this.heap = [] - this.top = 0 - this.comparator = comparator - } - size() { - return this.heap.length - } - isEmpty() { - return this.size() === 0 - } - peek() { - return this.heap[this.top] - } - push(...values) { - values.forEach((value) => { - this.heap.push(value) - this.siftUp() - }) - return this.size() - } - pop() { - const poppedValue = this.peek() - const bottom = this.size() - 1 - if (bottom > this.top) { - this.swap(this.top, bottom) - } - this.heap.pop() - this.siftDown() - return poppedValue - } - replace(value) { - const replacedValue = this.peek() - this.heap[this.top] = value - this.siftDown() - return replacedValue - } - - parent = (i) => ((i + 1) >>> 1) - 1 - left = (i) => (i << 1) + 1 - right = (i) => (i + 1) << 1 - greater = (i, j) => this.comparator(this.heap[i], this.heap[j]) - swap = (i, j) => ([this.heap[i], this.heap[j]] = [this.heap[j], this.heap[i]]) - siftUp = () => { - let node = this.size() - 1 - while (node > this.top && this.greater(node, this.parent(node))) { - this.swap(node, this.parent(node)) - node = this.parent(node) - } - } - siftDown = () => { - let node = this.top - while ( - (this.left(node) < this.size() && this.greater(this.left(node), node)) || - (this.right(node) < this.size() && this.greater(this.right(node), node)) - ) { - let maxChild = - this.right(node) < this.size() && - this.greater(this.right(node), this.left(node)) - ? this.right(node) - : this.left(node) - this.swap(node, maxChild) - node = maxChild - } - } -} -/** - * @param {number[][]} events - * @return {number} - */ -function maxEvents(events) { - const pq = new PriorityQueue((a, b) => a < b) - events.sort((a, b) => a[0] - b[0]) - let res = 0, i = 0, n = events.length - for(let d = 1; d <= 100000; d++) { - while(i < n && events[i][0] === d) { - pq.push(events[i++][1]) - } - while(!pq.isEmpty() && pq.peek() < d) { - pq.pop() - } - if(!pq.isEmpty()) { - res++ - pq.pop() - } - } - return res -} - - -// another - - - -/** - * @param {number[][]} events - * @return {number} - */ -function maxEvents(events) { - events.sort(([, aEnd], [, bEnd]) => aEnd - bEnd); - const lastDay = events[events.length - 1][1]; - const segmentTree = new SegmentTree(Array.from({ length: lastDay }, (_, i) => i), Infinity, (a, b) => Math.min(a, b)); - let daysAttended = 0; - - for (const [start, end] of events) { - // earliest attendable day - const ead = segmentTree.queryIn(start - 1, end); - if (ead <= end) { - daysAttended += 1; - segmentTree.setAt(ead, Infinity); - } - } - - return daysAttended; -} - -// https://github.com/axross/complex-data-structures -// new SegmentTree(values, identity, associate) -// segmentTree.getAt(i) -// segmentTree.queryIn(from, to) -// segmentTree.setAt(i, value) -// segmentTree.length -class SegmentTree{constructor(t,e,s){if(this.valueLength=t.length,this.identity=e,this.associate=s,0===t.length)this.tree=[];else{const h=2**Math.ceil(Math.log2(t.length))*2-1,i=[];for(let s=0;s<=h>>1;++s)i[(h>>1)+s]=s>1)-1;t>=0;--t)i[t]=s(i[2*t+1],i[2*t+2]);this.tree=i}}get length(){return this.valueLength}getAt(t){return this.tree[t+(this.tree.length>>1)]}queryIn(t,e){let s=this.identity;const h=[[0,0,1+(this.tree.length>>1)]];for(;h.length>0;){const[i,r,n]=h.pop();r>=t&&n<=e?s=this.associate(s,this.tree[i]):r>=e||nthis.tree.length>>1||h.push([2*i+1,r,r+n>>1],[2*i+2,r+n>>1,n])}return s}setAt(t,e){const s=t+(this.tree.length>>1);this.tree[s]=e;let h=s-1>>1;for(;h>=0;)this.tree[h]=this.associate(this.tree[2*h+1],this.tree[2*h+2]),h=h-1>>1}} - - -// another - -/** - * @param {number[][]} events - * @return {number} - */ -function maxEvents(events) { - const pq = new MinPriorityQueue () - events.sort((a, b) => a[0] - b[0]) - let res = 0, i = 0, n = events.length - for(let d = 1; d <= 1e5; d++) { - while(i < n && events[i][0] === d) { - pq.enqueue(events[i++][1]) - } - while(!pq.isEmpty() && pq.front().element < d) { - pq.dequeue() - } - if(!pq.isEmpty()) { - res++ - pq.dequeue() - } - } - return res -} diff --git a/1359-count-all-valid-pickup-and-delivery-options.js b/1359-count-all-valid-pickup-and-delivery-options.js index ae1de659..cb58fb1b 100644 --- a/1359-count-all-valid-pickup-and-delivery-options.js +++ b/1359-count-all-valid-pickup-and-delivery-options.js @@ -11,16 +11,3 @@ const countOrders = function(n) { return res }; -// another - -/** - * @param {number} n - * @return {number} - */ -const countOrders = function(n) { - let res = 1 - const MOD = 10 ** 9 + 7 - for(let i = 1; i <= n; i++) res = res * i % MOD - for(let i = 1; i < 2 * n; i += 2) res = res * i % MOD - return res -}; diff --git a/136-single-number.js b/136-single-number.js index 662dfc28..706d17ec 100644 --- a/136-single-number.js +++ b/136-single-number.js @@ -9,12 +9,3 @@ const singleNumber = function(nums) { }; -// another - -/** - * @param {number[]} nums - * @return {number} - */ -const singleNumber = function(nums) { - return nums.reduce((ac, e) => ac ^ e, 0) -}; diff --git a/1366-rank-teams-by-votes.js b/1366-rank-teams-by-votes.js index ef253262..560e70b1 100644 --- a/1366-rank-teams-by-votes.js +++ b/1366-rank-teams-by-votes.js @@ -25,25 +25,3 @@ const rankTeams = function(votes) { return keys.join('') }; -// another - -/** - * @param {string[]} votes - * @return {string} - */ -const rankTeams = function(votes) { - if (votes.length === 1) return votes[0]; - const score = new Map(votes[0].split('').map(c => [c, new Array(votes[0].length).fill(0)])); - for (s of votes) { - for (let i = 0; i < s.length; i++) { - score.get(s[i])[i]++; - } - } - return votes[0].split('').sort((a,b) => { - for (let i = 0; i < votes[0].length; i++) { - if (score.get(a)[i] > score.get(b)[i]) return -1; - if (score.get(a)[i] < score.get(b)[i]) return 1; - } - return a < b ? -1 : 1; - }).join(''); -}; diff --git a/1368-minimum-cost-to-make-at-least-one-valid-path-in-a-grid.js b/1368-minimum-cost-to-make-at-least-one-valid-path-in-a-grid.js index def7dab1..7bf13c9d 100644 --- a/1368-minimum-cost-to-make-at-least-one-valid-path-in-a-grid.js +++ b/1368-minimum-cost-to-make-at-least-one-valid-path-in-a-grid.js @@ -28,71 +28,3 @@ function minCost(grid) { return dp[m - 1][n - 1] } -// another - -/** - * @param {number[][]} grid - * @return {number} - */ -const minCost = function (grid) { - const n = grid.length - const m = grid[0].length - const moves = [ - [0, 1], - [0, -1], - [1, 0], - [-1, 0], - ] - const dp = [...new Array(n)].map((e) => [...new Array(m)].fill(Infinity)) - dp[0][0] = 0 - let queue = [[0, 0]] - while (queue.length > 0) { - const temp = [] - for (let i = 0; i < queue.length; i++) { - const [x, y] = queue[i] - for (let j = 0; j < moves.length; j++) { - const nextX = x + moves[j][0] - const nextY = y + moves[j][1] - if (nextX >= 0 && nextY >= 0 && nextX < n && nextY < m) { - if (dp[nextX][nextY] > dp[x][y] + (grid[x][y] - 1 === j ? 0 : 1)) { - dp[nextX][nextY] = dp[x][y] + (grid[x][y] - 1 === j ? 0 : 1) - queue.push([nextX, nextY]) - } - } - } - } - queue = temp - } - return dp[n - 1][m - 1] -} - -// another - -function minCost(grid) { - const INF = 1e9, m = grid.length, n = grid[0].length - const dirs = [[0, 1], [0, -1], [1, 0], [-1, 0]] // right, left, down, up - let cost = 0 - const dp = Array.from({ length: m }, () => Array(n).fill(INF)) - const q = [] - dfs(0, 0, 0) - while(q.length) { - cost++ - for (let size = q.length; size > 0; size--) { - const [r, c] = q.shift() - for(let [dx, dy] of dirs) { - dfs(r + dx, c + dy, cost) - } - } - } - - return dp[m - 1][n - 1] - function dfs(r, c, cost) { - if(r < 0 || r >= m || c < 0 || c >= n || dp[r][c] !== INF) return - dp[r][c] = cost - q.push([r, c]) - const nextDir = grid[r][c] - 1 - const [dx, dy] = dirs[nextDir] - dfs(r + dx, c + dy, cost) - } -} - diff --git a/137-single-number-ii.js b/137-single-number-ii.js index 64249683..746731bd 100644 --- a/137-single-number-ii.js +++ b/137-single-number-ii.js @@ -14,43 +14,3 @@ const singleNumber = function(nums) { } }; -// another - -/** - * @param {number[]} nums - * @return {number} - */ -const singleNumber = (nums)=> { - let one=0, two=0; - for (let i=0; i { - // Initialize result - let result = 0; - let x, sum; - const n = nums.length - // Iterate through every bit - for (let i = 0; i < 32; i++) { - // Find sum of set bits at ith position in all - // array elements - sum = 0; - x = (1 << i); - for (let j = 0; j < n; j++ ) { - if (nums[j] & x) sum++; - } - // The bits with sum not multiple of 3, are the - // bits of element with single occurrence. - if (sum % 3) result |= x; - } - return result; -} diff --git a/1371-find-the-longest-substring-containing-vowels-in-even-counts.js b/1371-find-the-longest-substring-containing-vowels-in-even-counts.js index 9b9130d8..b4f77385 100644 --- a/1371-find-the-longest-substring-containing-vowels-in-even-counts.js +++ b/1371-find-the-longest-substring-containing-vowels-in-even-counts.js @@ -23,104 +23,3 @@ const findTheLongestSubstring = function(s) { return res }; -// another - -/** - * @param {string} s - * @return {number} - */ -const findTheLongestSubstring = function(s) { - const n = s.length - let res = 0, mask = 0 - const map = new Map([[0, -1]]) - - for(let i = 0; i < n; i++) { - const ch = s[i] - const idx = 'aeiou'.indexOf(ch) - if(idx !== -1) { - mask ^= (1 << idx) - if(map.has(mask)) { - res = Math.max(res, i - map.get(mask)) - } else { - map.set(mask, i) - } - } else { - res = Math.max(res, i - map.get(mask)) - } - } - - return res -}; - -// another - -/** - * @param {string} s - * @return {number} - */ -const findTheLongestSubstring = function(s) { - const n = s.length - const ch2num = ch => { - const idx = 'aeiou'.indexOf(ch) - return idx === -1 ? 0 : (1 << idx) - } - let res = 0 - let mask = 0 - const hash = new Map([[0, -1]]) - for (let i = 0; i < n; i++) { - mask ^= ch2num(s[i]) - const first = hash.has(mask) ? hash.get(mask) : i - if (!hash.has(mask)) hash.set(mask, i) - res = Math.max(res, i - first) - } - - return res -}; - -// another - -/** - * @param {string} s - * @return {number} - */ -var findTheLongestSubstring = function (s, V = 'aeiou', max = 0) { - let encode = (c) => { - let i = V.indexOf(c) - return i == -1 ? 0 : 1 << i - } - let N = s.length - let A = Array(N + 1).fill(0) - let seen = new Map([[0, 0]]) - for (let i = 1; i <= N; ++i) { - A[i] = A[i - 1] ^ encode(s[i - 1]) - let first = seen.has(A[i]) ? seen.get(A[i]) : i - if (first == i) seen.set(A[i], i) // first seen A[i] index - max = Math.max(max, i - first) // max of i-th index minus first seen A[i] index - } - return max -} - -// another - -/** - * @param {string} s - * @return {number} - */ -const findTheLongestSubstring = function(s) { - const n = s.length - const ch2num = ch => { - const idx = 'aeiou'.indexOf(ch) - return idx === -1 ? 0 : (1 << idx) - } - let res = 0 - let mask = 0 - const hash = new Map([[0, 0]]) - for (let i = 1; i <= n; i++) { - mask ^= ch2num(s[i - 1]) - const first = hash.has(mask) ? hash.get(mask) : i - if (!hash.has(mask)) hash.set(mask, i) - res = Math.max(res, i - first) - } - - return res -}; diff --git a/1377-frog-position-after-t-seconds.js b/1377-frog-position-after-t-seconds.js index dd3dabc6..cf0f8829 100644 --- a/1377-frog-position-after-t-seconds.js +++ b/1377-frog-position-after-t-seconds.js @@ -37,47 +37,3 @@ const frogPosition = function (n, edges, t, target) { return res[target - 1] || 0 } -// another - -/** - * @param {number} n - * @param {number[][]} edges - * @param {number} t - * @param {number} target - * @return {number} - */ -const frogPosition = function (n, edges, t, target) { - const graph = { 1: new Set() } - for (let [from, to] of edges) { - if (graph[from]) graph[from].add(to) - else graph[from] = new Set([to]) - if (graph[to]) graph[to].add(from) - else graph[to] = new Set([from]) - } - - // dfs through the graph storing the vetices you've visited, number of jumps, and current vertice - const dfs = (from, numJumps, visited) => { - // if the count equals t then return 1 if the vertice is the target - if (numJumps === t) return from === target ? 1 : 0 - - // average out all the next results - let numEdgesCanJump = 0 - let total = 0 - for (let to of graph[from]) { - if (visited.has(to)) continue - visited.add(to) - total += dfs(to, numJumps + 1, visited) - visited.delete(to) - numEdgesCanJump++ - } - - // if we can jump, average all the next results - // otherwise we can't jump anywhere and return 1 if we are at the target - // if we are not at the target return 0 - if (numEdgesCanJump > 0) { - return total / numEdgesCanJump - } - return from === target ? 1 : 0 - } - return dfs(1, 0, new Set([1])) -} diff --git a/1383-maximum-performance-of-a-team.js b/1383-maximum-performance-of-a-team.js index e741a417..a2e43d08 100644 --- a/1383-maximum-performance-of-a-team.js +++ b/1383-maximum-performance-of-a-team.js @@ -81,80 +81,3 @@ function moveDown(arr, i, comparator) { } } -// another - -const MinHeap = () => { - const list = [] - const parent = (index) => Math.floor((index - 1) / 2) - const left = (index) => 2 * index + 1 - const right = (index) => 2 * index + 2 - - const swap = (a, b) => { - const temp = list[a] - list[a] = list[b] - list[b] = temp - } - const insert = (x) => { - list.push(x) - let currentIndex = list.length - 1 - let parentIndex = parent(currentIndex) - while (list[parentIndex] > list[currentIndex]) { - swap(parentIndex, currentIndex) - currentIndex = parentIndex - parentIndex = parent(parentIndex) - } - } - const sink = (index) => { - let minIndex = index - const leftIndex = left(index) - const rightIndex = right(index) - if (list[leftIndex] < list[minIndex]) { - minIndex = leftIndex - } - if (list[rightIndex] < list[minIndex]) { - minIndex = rightIndex - } - if (minIndex !== index) { - swap(minIndex, index) - sink(minIndex) - } - } - const size = () => list.length - const extract = () => { - swap(0, size() - 1) - const min = list.pop() - sink(0) - return min - } - return { - insert, - size, - extract, - } -} - -/** Heap Greedy - * @param {number} n - * @param {number[]} speed - * @param {number[]} efficiency - * @param {number} k - * @return {number} - */ -const maxPerformance = function (n, speed, efficiency, k) { - const works = speed.map((s, index) => [s, efficiency[index]]) - works.sort((a, b) => b[1] - a[1]) - let totalSpeed = 0 - let max = 0 - const minHeap = MinHeap() - for (const work of works) { - if (minHeap.size() >= k) { - const minSpeed = minHeap.extract() - totalSpeed -= minSpeed - } - minHeap.insert(work[0]) - totalSpeed += work[0] - max = Math.max(max, totalSpeed * work[1]) - } - const result = max % (10 ** 9 + 7) - return result === 301574163 ? result + 1 : result -} diff --git a/1388-pizza-with-3n-slices.js b/1388-pizza-with-3n-slices.js index 5b214871..1bc445e7 100644 --- a/1388-pizza-with-3n-slices.js +++ b/1388-pizza-with-3n-slices.js @@ -38,30 +38,3 @@ function maxSum(arr, n) { } -// another - -/** - * @param {number[]} slices - * @return {number} - */ -const maxSizeSlices = function (slices) { - const n = slices.length, m = ~~(n / 3) - const arr1 = slices.slice(1), arr2 = slices.slice(0, n - 1) - return Math.max(helper(arr1, m), helper(arr2, m)) - function helper(arr, k) { - const len = arr.length - const dp = Array.from({ length: len + 1 }, () => Array(k + 1).fill(0)) - for(let i = 1; i <= len; i++) { - for(let j = 1; j <= k; j++) { - if(i === 1) dp[i][j] = arr[i - 1] - else { - dp[i][j] = Math.max( - dp[i - 1][j], - dp[i - 2][j - 1] + arr[i - 1] - ) - } - } - } - return dp[len][k] - } -} diff --git a/139-word-break.js b/139-word-break.js index ad37fdf0..7dfc1501 100644 --- a/139-word-break.js +++ b/139-word-break.js @@ -26,84 +26,3 @@ function helper(str, idx, set, map) { return res } -// another - - -/** - * @param {string} s - * @param {string[]} wordDict - * @return {boolean} - */ -const wordBreak = function(s, wordDict) { - const len = s.length; - const dp = new Array(len).fill(false); - - for (let i = 0; i < len; i++) { - for (let word of wordDict) { - if (word.length <= i + 1 - && s.substring(i - word.length + 1, i + 1) === word) { - let index = i - word.length; - if (index < 0) { - dp[i] = true; - } else { - dp[i] = dp[index]; - } - if(dp[i]) break; - } - } - } - - return dp[len - 1]; -}; - -// another - -/** - * @param {string} s - * @param {string[]} wordDict - * @return {boolean} - */ -const wordBreak = function(s, wordDict) { - const len = s.length; - const f = new Array(len + 1).fill(false); - - f[0] = true; - - for(let i = 1; i <= len; i++){ - for(let str of wordDict){ - if(str.length <= i - && f[i - str.length] - && s.slice(i - str.length, i) === str){ - f[i] = true; - break; - } - } - } - - return f[len]; -}; - - -// another - -/** - * @param {string} s - * @param {string[]} wordDict - * @return {boolean} - */ -const wordBreak = function(s, wordDict) { - const set = new Set(wordDict) - const dp = Array(s.length + 1).fill(false) - dp[0] = true - for(let i = 1; i <= s.length; i++) { - for(let j = 0; j < i; j++) { - if(dp[j] && set.has(s.slice(j, i))) { - dp[i] = true - break - } - } - } - - return dp[s.length] -}; - diff --git a/1395-count-number-of-teams.js b/1395-count-number-of-teams.js index 076cd0ff..6bb67762 100644 --- a/1395-count-number-of-teams.js +++ b/1395-count-number-of-teams.js @@ -20,45 +20,3 @@ const numTeams = function(rating) { }; -// another - -/** - * @param {number[]} rating - * @return {number} - */ -const numTeams = function(rating) { - if(rating.length < 3) return 0 - const n = rating.length - const leftTree = Array(1e5 + 1).fill(0) - const rightTree = Array(1e5 + 1).fill(0) - for(let r of rating) update(rightTree, r, 1) - let res = 0 - for(let r of rating) { - update(rightTree, r, -1) - res += getPrefixSum(leftTree, r - 1) * getSuffixSum(rightTree, r + 1) - res += getSuffixSum(leftTree, r + 1) * getPrefixSum(rightTree, r - 1) - update(leftTree, r, 1) - } - - return res -}; - -function update(bit, index, val) { - while(index < bit.length) { - bit[index] += val - index += index & (-index) - } -} - -function getPrefixSum(bit, index) { - let res = 0 - while(index > 0) { - res += bit[index] - index -= index & (-index) - } - return res -} - -function getSuffixSum(bit, index) { - return getPrefixSum(bit, 1e5) - getPrefixSum(bit, index - 1) -} diff --git a/140-word-break-ii.js b/140-word-break-ii.js index 36070c6a..9ad89448 100644 --- a/140-word-break-ii.js +++ b/140-word-break-ii.js @@ -26,97 +26,3 @@ function helper(str, idx, set, map) { map.set(idx, res) return res } -// another - - -/** - * @param {string} s - * @param {string[]} wordDict - * @return {string[]} - */ -const wordBreak = function(s, wordDict) { - return backTrack(s, wordDict, {}) -}; - -function backTrack(s, wordDict, mem) { - if(mem.hasOwnProperty(s)) return mem[s] - const result = [] - for(let word of wordDict) { - if(s.startsWith(word)) { - let next = s.slice(word.length) - if(next.length === 0) result.push(word) - else { - for(let sub of backTrack(next, wordDict, mem)) { - result.push(word+ ' '+sub) - } - } - } - } - mem[s] = result - return result -} - -// another - -/** - * @param {string} s - * @param {string[]} wordDict - * @return {string[]} - */ -const wordBreak = function (s, wordDict) { - const dictSet = new Set(wordDict) - const memo = {} - function dfs(start) { - if (start > s.length - 1) { - return [[]] - } - if (memo[start] !== undefined) { - return memo[start] - } - const out = [] - for (let i = start; i < s.length; i++) { - const substr = s.substring(start, i + 1) - if (dictSet.has(substr)) { - let next = dfs(i + 1) - for (let n of next) { - out.push([substr, ...n]) - } - } - } - return (memo[start] = out) - } - const res = dfs(0) - return res.filter((a) => a.join('') === s).map((a) => a.join(' ')) -} - -// another - -/** - * @param {string} s - * @param {string[]} wordDict - * @return {string[]} - */ - -const wordBreak = (s, wordDict) => { - const set = new Set(wordDict) - return helper(s, 0, set) -} - -function helper(s, idx, dict) { - if(idx === s.length) return [] - const res = [] - for(let i = idx; i < s.length; i++) { - const tmp = s.slice(idx, i + 1) - if(dict.has(tmp)) { - const arr = helper(s, i + 1, dict) - if(i + 1 >= s.length) { - res.push(tmp) - } else if(arr.length) { - for(let e of arr) { - res.push(tmp + ' ' + e) - } - } - } - } - return res -} diff --git a/1405-longest-happy-string.js b/1405-longest-happy-string.js index d86f669f..dde37d90 100644 --- a/1405-longest-happy-string.js +++ b/1405-longest-happy-string.js @@ -27,67 +27,3 @@ const longestDiverseString = function (a, b, c) { }; -// another - - -/** - * @param {number} a - * @param {number} b - * @param {number} c - * @return {string} - */ -const longestDiverseString = function (a, b, c) { - return generate(a, b, c, "a", "b", "c"); -}; - -function generate(a, b, c, ac, bc, cc) { - if (a < b) return generate(b, a, c, bc, ac, cc); - if (b < c) return generate(a, c, b, ac, cc, bc); - if (b === 0) return ac.repeat(Math.min(2, a)); - let use_a = Math.min(2, a), - use_b = a - use_a >= b ? 1 : 0; - return ( - ac.repeat(use_a) + - bc.repeat(use_b) + - generate(a - use_a, b - use_b, c, ac, bc, cc) - ); -} - -// another - -/** - * @param {number} a - * @param {number} b - * @param {number} c - * @return {string} - */ -const longestDiverseString = function (a, b, c) { - const n = a + b + c - let res = '' - let A = 0, B = 0, C = 0 - for(let i = 0; i < n; i++) { - if((a >= c && a >= b && A !== 2) || (B === 2 && a > 0) || (C === 2 && a > 0)) { - A++ - res += 'a' - a-- - B = 0 - C = 0 - } else if((b >= c && b >= a && B !== 2) || (A === 2 && b > 0) || (C === 2 && b)) { - B++ - res += 'b' - b-- - A = 0 - C = 0 - } else if((c >= a && c >= b && C !== 2) || (A === 2 && c) || (B === 2 && c)) { - C++ - res += 'c' - c-- - A = 0 - B = 0 - } - } - - return res -}; - - diff --git a/1406-stone-game-iii.js b/1406-stone-game-iii.js index d837f6f5..d06be7de 100644 --- a/1406-stone-game-iii.js +++ b/1406-stone-game-iii.js @@ -21,23 +21,3 @@ const stoneGameIII = function (stoneValue) { else return 'Bob' } -// another - -/** - * @param {number[]} stoneValue - * @return {string} - */ -const stoneGameIII = function (stoneValue) { - const n = stoneValue.length, - dp = new Array(4).fill(0) - for (let i = n - 1; i >= 0; --i) { - dp[i % 4] = -Infinity - for (let k = 0, take = 0; k < 3 && i + k < n; ++k) { - take += stoneValue[i + k] - dp[i % 4] = Math.max(dp[i % 4], take - dp[(i + k + 1) % 4]) - } - } - if (dp[0] > 0) return 'Alice' - if (dp[0] < 0) return 'Bob' - return 'Tie' -} diff --git a/1411-number-of-ways-to-paint-n-3-grid.js b/1411-number-of-ways-to-paint-n-3-grid.js index 199a4a5b..116340bb 100644 --- a/1411-number-of-ways-to-paint-n-3-grid.js +++ b/1411-number-of-ways-to-paint-n-3-grid.js @@ -16,24 +16,3 @@ const numOfWays = function(n) { return (colors2 + colors3) % mod }; -// another - - -/** - * @param {number} n - * @return {number} - */ -const numOfWays = function (n) { - let a121 = 6, - a123 = 6, - b121, - b123, - mod = 1e9 + 7 - for (let i = 1; i < n; ++i) { - b121 = a121 * 3 + a123 * 2 - b123 = a121 * 2 + a123 * 2 - a121 = b121 % mod - a123 = b123 % mod - } - return (a121 + a123) % mod -} diff --git a/1415-the-k-th-lexicographical-string-of-all-happy-strings-of-length-n.js b/1415-the-k-th-lexicographical-string-of-all-happy-strings-of-length-n.js index e5568823..aef031fe 100644 --- a/1415-the-k-th-lexicographical-string-of-all-happy-strings-of-length-n.js +++ b/1415-the-k-th-lexicographical-string-of-all-happy-strings-of-length-n.js @@ -20,47 +20,3 @@ const getHappyString = function(n, k) { return '' }; -// another - -/** - * @param {number} n - * @param {number} k - * @return {string} - */ -const getHappyString = function(n, k) { - const hash = {a: 'bc', b: 'ac', c: 'ab'} - const q = ['a', 'b', 'c'] - while(q[0].length !== n) { - const pre = q.shift() - for(const ch of hash[pre[pre.length - 1]]) { - q.push(pre + ch) - } - } - - return q.length >= k ? q[k - 1] : '' -}; - -// another - -/** - * @param {number} n - * @param {number} k - * @return {string} - */ -var getHappyString = function(n, k) { - const res = [] - dfs() - return res.length === k ? res[res.length - 1] : '' - function dfs(path = '') { - if(res.length === k) return - if(path.length === n) { - res.push(path) - return - } - for(const e of 'abc') { - if(path === '' || e !== path[path.length - 1]) { - dfs(path + e) - } - } - } -}; diff --git a/1416-restore-the-array.js b/1416-restore-the-array.js index de517075..ce0c845c 100644 --- a/1416-restore-the-array.js +++ b/1416-restore-the-array.js @@ -31,31 +31,3 @@ function dfs(s, k, i, dp) { return (dp[i] = ans) } -// another - -/** - * @param {string} s - * @param {number} k - * @return {number} - */ -const numberOfArrays = function (s, k) { - const mod = 10 ** 9 + 7 - const n = s.length - const dp = new Array(n + 1).fill(0) - dp[n] = 1 - for (let i = n - 1; i >= 0; i--) { - if (s[i] === '0') continue - else { - let temp = s[i] - for (let j = i + 1; j <= n; j++) { - if (temp > k) break - dp[i] = (dp[i] + dp[j]) % mod - if (j < n) { - temp = temp * 10 + parseInt(s[j]) - } - } - } - } - return parseInt(dp[0]) -} - diff --git a/142-linked-list-cycle-ii.js b/142-linked-list-cycle-ii.js index ec8083fb..78130812 100644 --- a/142-linked-list-cycle-ii.js +++ b/142-linked-list-cycle-ii.js @@ -29,23 +29,3 @@ const detectCycle = function(head) { return null }; -// another method - -/** - * @param {ListNode} head - * @return {ListNode} - */ -const detectCycle = (head) => { - if (!head) return head; - let currentNode = head; - let previousNode = true; - while (currentNode) { - if (currentNode.previous) return currentNode - if (!currentNode.previous) { - currentNode.previous = previousNode; - previousNode = currentNode; - currentNode = currentNode.next; - } - } - return null; -}; diff --git a/1422-maximum-score-after-splitting-a-string.js b/1422-maximum-score-after-splitting-a-string.js index 916bae85..42a56b61 100644 --- a/1422-maximum-score-after-splitting-a-string.js +++ b/1422-maximum-score-after-splitting-a-string.js @@ -16,19 +16,3 @@ const maxScore = function(s) { return res }; -// another - -/** - * @param {string} s - * @return {number} - */ -const maxScore = function(s) { - const n = s.length - let res = -Infinity, one = 0, zero = 0 - for(let i = 0; i < n; i++) { - s[i] === '0' ? zero++ : one++ - if(i !== n - 1) res = Math.max(res, zero - one) - } - - return res + one -}; diff --git a/1425-constrained-subsequence-sum.js b/1425-constrained-subsequence-sum.js index 81ac967e..482d68c4 100644 --- a/1425-constrained-subsequence-sum.js +++ b/1425-constrained-subsequence-sum.js @@ -21,81 +21,3 @@ const constrainedSubsetSum = function(nums, k) { return max; }; -// another - -/** - * @param {number[]} nums - * @param {number} k - * @return {number} - */ -const constrainedSubsetSum = function (nums, k) { - const dll = new DLL() - dll.push([0, nums[0]]) - let max = nums[0] - for (let i = 1; i < nums.length; i++) { - if (!dll.isEmpty() && i - dll.peek().val[0] > k) { - dll.shift() - } - const sum = Math.max(dll.peek().val[1], 0) + nums[i] - max = Math.max(max, sum) - while (!dll.isEmpty() && dll.peekLast().val[1] < sum) { - dll.pop() - } - dll.push([i, sum]) - } - return max -} - -class Node { - constructor(val) { - this.val = val - this.prev = null - this.next = null - } -} -class DLL { - constructor() { - this.head = new Node() - this.tail = null - this.size = 0 - } - peek() { - return this.head.next - } - peekLast() { - return this.tail - } - isEmpty() { - return this.head.next == null - } - shift() { - const h = this.head.next - if (h) { - this.head.next = h.next - if (h.next) { - h.next.prev = this.head - } else { - this.tail = null - } - this.size-- - } - } - pop() { - if (this.tail == null) return - const newTail = this.tail.prev - if (newTail) { - newTail.next = null - this.tail.prev = null - this.tail = newTail - this.size-- - } - - } - push(val) { - const node = new Node(val) - node.prev = this.tail ?? this.head - node.prev.next = node - this.tail = node - this.size++ - } -} diff --git a/143-reorder-list.js b/143-reorder-list.js index 5d49c176..cd3d4e61 100644 --- a/143-reorder-list.js +++ b/143-reorder-list.js @@ -41,92 +41,3 @@ const reorderList = function(head) { -// another - -/** - * Definition for singly-linked list. - * function ListNode(val) { - * this.val = val; - * this.next = null; - * } - */ -/** - * @param {ListNode} head - * @return {void} Do not return anything, modify head in-place instead. - */ -const reorderList = function(head) { - if (!head || !head.next) return head; - - const reverse = head => { - if (!head || !head.next) return head; - const newHead = reverse(head.next); - head.next.next = head; - head.next = null; - return newHead; - }; - - const merge = (l1, l2) => { - if (!l1) return l2; - if (!l2) return l1; - while (l1 && l2) { - const next1 = l1.next; - const next2 = l2.next; - l1.next = l2; - if (next1 == null) break; - l2.next = next1; - l1 = next1; - l2 = next2; - } - }; - - let fast = head; - let slow = head; - - while (fast && fast.next) { - fast = fast.next.next; - slow = slow.next; - } - - fast = slow.next; - slow.next = null; - - fast = reverse(fast); - merge(head, fast); -}; - - -// another - -/** - * Definition for singly-linked list. - * function ListNode(val) { - * this.val = val; - * this.next = null; - * } - */ -/** - * @param {ListNode} head - * @return {void} Do not return anything, modify head in-place instead. - */ -const reorderList = function(head) { - if (head == null || head.next == null) { - return head; - } - const arr = []; - let tmp = head; - while (tmp.next) { - arr.push(tmp); - tmp = tmp.next; - } - arr.push(tmp); - for (let i = 1; i < arr.length; i = i + 2) { - if (arr.length - 1 > i) { - let el = arr.pop(); - arr.splice(i, 0, el); - } - } - for (let i = 1; i < arr.length; i++) { - arr[i - 1].next = arr[i]; - if (i === arr.length - 1) arr[i].next = null; - } -}; diff --git a/1434-number-of-ways-to-wear-different-hats-to-each-other.js b/1434-number-of-ways-to-wear-different-hats-to-each-other.js index 4656736b..da67e74c 100644 --- a/1434-number-of-ways-to-wear-different-hats-to-each-other.js +++ b/1434-number-of-ways-to-wear-different-hats-to-each-other.js @@ -38,35 +38,3 @@ const numberWays = function(hats) { }; -// another - - -/** - * @param {number[][]} hats - * @return {number} - */ -const numberWays = function (hats) { - const pplThatCanWearHats = new Array(40 + 1).fill(null).map(() => []) - for (let i = 0; i < hats.length; i++) { - const personMask = 1 << i - for (let hat of hats[i]) { - pplThatCanWearHats[hat].push(personMask) - } - } - - const cache = {} - const dfs = (hat, pplWithoutHatsMask) => { - if (!pplWithoutHatsMask) return 1 - if (hat === 41) return 0 - const key = `${hat}-${pplWithoutHatsMask}` - if (cache.hasOwnProperty(key)) return cache[key] - const nextHat = hat + 1 - let total = dfs(nextHat, pplWithoutHatsMask) - for (let personMask of pplThatCanWearHats[hat]) { - if (!(pplWithoutHatsMask & personMask)) continue - total += dfs(nextHat, pplWithoutHatsMask ^ personMask) - } - return (cache[key] = total % 1000000007) - } - return dfs(1, (1 << hats.length) - 1) -} diff --git a/1436-destination-city.js b/1436-destination-city.js index 18999109..5e976104 100644 --- a/1436-destination-city.js +++ b/1436-destination-city.js @@ -15,16 +15,3 @@ const destCity = function(paths) { } }; -// another - -/** - * @param {string[][]} paths - * @return {string} - */ -const destCity = function(paths) { - const set = new Set() - for(let [s, e] of paths) set.add(e) - for(let [s, e] of paths) set.delete(s) - - return set[Symbol.iterator]().next().value -}; diff --git a/1438-longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit.js b/1438-longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit.js index ffc4ed5c..39383ea3 100644 --- a/1438-longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit.js +++ b/1438-longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit.js @@ -24,77 +24,3 @@ const longestSubarray = function(nums, limit) { return j - i; }; -// another - -/** - * @param {number[]} nums - * @param {number} limit - * @return {number} - */ -const longestSubarray = function (nums, limit) { - const maxDq = new Deque(), minDq = new Deque(), n = nums.length - let l = 0, r = 0 - let res = 0 - for(r = 0; r < n; r++) { - const cur = nums[r] - while(!maxDq.isEmpty() && maxDq.last() < cur) { - maxDq.pop() - } - maxDq.enqueue(cur) - while(!minDq.isEmpty() && minDq.last() > cur) { - minDq.pop() - } - minDq.enqueue(cur) - - while(maxDq.first() - minDq.first() > limit) { - if(nums[l] === maxDq.first()) maxDq.dequeue() - if(nums[l] === minDq.first()) minDq.dequeue() - l++ - } - res = Math.max(res, r - l + 1) - } - return res -} - -class Deque { - constructor() { - this.head = new Node() - this.tail = this.head - } - - isEmpty() { - return this.head.next === null - } - - first() { - return this.head.next.value - } - - last() { - return this.tail.value - } - - dequeue() { - this.head = this.head.next - this.head.prev = null - } - - enqueue(value) { - this.tail.next = new Node(value) - this.tail.next.prev = this.tail - this.tail = this.tail.next - } - - pop() { - this.tail = this.tail.prev - this.tail.next = null - } -} - -class Node { - constructor(value) { - this.value = value - this.next = null - this.prev = null - } -} diff --git a/1442-count-triplets-that-can-form-two-arrays-of-equal-xor.js b/1442-count-triplets-that-can-form-two-arrays-of-equal-xor.js index 02a65836..e462e122 100644 --- a/1442-count-triplets-that-can-form-two-arrays-of-equal-xor.js +++ b/1442-count-triplets-that-can-form-two-arrays-of-equal-xor.js @@ -17,25 +17,3 @@ const countTriplets = function(arr) { return res }; -// another - -/** - * @param {number[]} arr - * @return {number} - */ -const countTriplets = function(arr) { - arr.unshift(0) - const n = arr.length - let res = 0 - for(let i = 1; i < n; i++) { - arr[i] ^= arr[i - 1] - } - const count = {}, total = {} - for(let i = 0; i < n; i++) { - if(count[arr[i]] == null) count[arr[i]] = 0 - if(total[arr[i]] == null) total[arr[i]] = 0 - res += count[arr[i]]++ * (i - 1) - total[arr[i]] - total[arr[i]] += i - } - return res -}; diff --git a/1446-consecutive-characters.js b/1446-consecutive-characters.js index a8e49a47..8b286d0d 100644 --- a/1446-consecutive-characters.js +++ b/1446-consecutive-characters.js @@ -14,23 +14,3 @@ const maxPower = function(s) { return res }; -// another - -/** - * @param {string} s - * @return {number} - */ -const maxPower = function(s) { - let prev = '', prevIdx = -1, res = -Infinity - for(let i = 0; i < s.length; i++) { - const cur = s[i] - if(cur !== prev) { - res = Math.max(res, i - prevIdx) - prev = cur - prevIdx = i - } else { - if(i === s.length - 1) res = Math.max(res, i - prevIdx + 1) - } - } - return res -}; diff --git a/1449-form-largest-integer-with-digits-that-add-up-to-target.js b/1449-form-largest-integer-with-digits-that-add-up-to-target.js index 8ceae82d..7df37d9c 100644 --- a/1449-form-largest-integer-with-digits-that-add-up-to-target.js +++ b/1449-form-largest-integer-with-digits-that-add-up-to-target.js @@ -25,32 +25,3 @@ const largestNumber = function (cost, target) { return paint } -// another - -/** - * @param {number[]} cost - * @param {number} target - * @return {string} - */ -const largestNumber = function(cost, target) { - const m = new Map() - const res = dfs(cost, 1, target, m) - return res.indexOf('0') !== -1 ? '0' : res -}; -function dfs(cost, index, remain, m) { - if(remain === 0) return '' - if(remain < 0 || index === cost.length + 1) return '0' - if(m.has(remain)) return m.get(remain) - const take = '' + index + dfs(cost, 1, remain - cost[index - 1], m) - const skip = dfs(cost, index + 1, remain, m) - const res = getBigger(take, skip) - m.set(remain, res) - return res -} -function getBigger(num1, num2) { - if(num1.indexOf('0') !== -1) return num2 - if(num2.indexOf('0') !== -1) return num1 - if(num1.length > num2.length) return num1 - else if(num1.length < num2.length) return num2 - else return num1 > num2 ? num1 : num2 -} diff --git a/146-lru-cache.js b/146-lru-cache.js index d9ef0dc7..96a6bb83 100755 --- a/146-lru-cache.js +++ b/146-lru-cache.js @@ -81,47 +81,3 @@ LRUCache.prototype.put = function(key, value) { * obj.put(key,value) */ -// another - -/** - * @param {number} capacity - */ -const LRUCache = function(capacity) { - this.m = new Map() - this.limit = capacity -}; - -/** - * @param {number} key - * @return {number} - */ -LRUCache.prototype.get = function(key) { - if(!this.m.has(key)) return -1 - const v = this.m.get(key) - this.m.delete(key) - this.m.set(key, v) - return v -}; - -/** - * @param {number} key - * @param {number} value - * @return {void} - */ -LRUCache.prototype.put = function(key, value) { - if(this.m.has(key)) { - this.m.delete(key) - } else { - if(this.m.size >= this.limit) { - const first = this.m.keys().next().value - this.m.delete(first) - } - } - this.m.set(key, value) -}; -/** - * Your LRUCache object will be instantiated and called as such: - * var obj = new LRUCache(capacity) - * var param_1 = obj.get(key) - * obj.put(key,value) - */ diff --git a/1462-course-schedule-iv.js b/1462-course-schedule-iv.js index abbcaf55..5b639e6b 100644 --- a/1462-course-schedule-iv.js +++ b/1462-course-schedule-iv.js @@ -53,91 +53,3 @@ const checkIfPrerequisite = function (numCourses, prerequisites, queries) { return res } -// another - - -/** - * @param {number} numCourses - * @param {number[][]} prerequisites - * @param {number[][]} queries - * @return {boolean[]} - */ -const checkIfPrerequisite = function(numCourses, prerequisites, queries) { - // https://en.wikipedia.org/wiki/Floyd%E2%80%93Warshall_algorithm - const n = numCourses - const connected = Array.from({ length: n }, () => Array(n).fill(false)) - for(let p of prerequisites) connected[p[0]][p[1]] = true - for(let k = 0; k < n; k++) { - for(let i = 0; i < n; i++) { - for(let j = 0; j < n; j++) { - connected[i][j] = connected[i][j] || (connected[i][k] && connected[k][j]); - } - } - } - const res = [] - for(let q of queries) res.push(connected[q[0]][q[1]]) - return res -}; - -// another - -/** - * @param {number} numCourses - * @param {number[][]} prerequisites - * @param {number[][]} queries - * @return {boolean[]} - */ -const checkIfPrerequisite = function (numCourses, prerequisites, queries) { - const n = numCourses - const connected = Array.from({ length: n }, () => Array(n).fill(false)) - for (let p of prerequisites) connected[p[0]][p[1]] = true - for (let k = 0; k < n; k++) { - for (let i = 0; i < n; i++) { - for (let j = 0; j < n; j++) { - connected[i][j] = - connected[i][j] || (connected[i][k] && connected[k][j]) - } - } - } - const res = [] - for (let q of queries) res.push(connected[q[0]][q[1]]) - return res -} - -// another - -/** - * @param {number} numCourses - * @param {number[][]} prerequisites - * @param {number[][]} queries - * @return {boolean[]} - */ -const checkIfPrerequisite = function (numCourses, prerequisites, queries) { - const graph = {}, - connected = Array.from({ length: numCourses }, () => - Array(numCourses).fill(-1) - ) - for (const [u, v] of prerequisites) { - if (graph[u] == null) graph[u] = [] - graph[u].push(v) - connected[u][v] = 1 - } - - const res = [] - for (const [u, v] of queries) res.push(dfs(u, v)) - - return res - - function dfs(u, v) { - if (connected[u][v] !== -1) return connected[u][v] - let res = false - for (const next of graph[u] || []) { - if (!res) { - res ||= dfs(next, v) - } else break - } - connected[u][v] = res - return res - } -} - diff --git a/1465-maximum-area-of-a-piece-of-cake-after-horizontal-and-vertical-cuts.js b/1465-maximum-area-of-a-piece-of-cake-after-horizontal-and-vertical-cuts.js index 12701fe7..1f467311 100644 --- a/1465-maximum-area-of-a-piece-of-cake-after-horizontal-and-vertical-cuts.js +++ b/1465-maximum-area-of-a-piece-of-cake-after-horizontal-and-vertical-cuts.js @@ -19,26 +19,3 @@ function getMax(limit, cuts) { return max } -// another - -/** - * @param {number} h - * @param {number} w - * @param {number[]} horizontalCuts - * @param {number[]} verticalCuts - * @return {number} - */ -const maxArea = function(h, w, horizontalCuts, verticalCuts) { - return (BigInt(maxGap(h, horizontalCuts)) * BigInt(maxGap(w, verticalCuts))) % BigInt(1e9 + 7) - function maxGap(limit, arr) { - let res = 0 - arr.sort((a, b) => a - b) - for(let i = 0, n = arr.length; i < n; i++) { - let tmp = i === 0 ? arr[0] : arr[i] - arr[i - 1] - res = Math.max(res, tmp) - } - res = Math.max(res, limit - arr[arr.length - 1]) - - return res - } -}; diff --git a/1467-probability-of-a-two-boxes-having-the-same-number-of-distinct-balls.js b/1467-probability-of-a-two-boxes-having-the-same-number-of-distinct-balls.js index 465ec0b8..f9d2d284 100644 --- a/1467-probability-of-a-two-boxes-having-the-same-number-of-distinct-balls.js +++ b/1467-probability-of-a-two-boxes-having-the-same-number-of-distinct-balls.js @@ -63,69 +63,3 @@ const getProbability = function(balls) { }; -// another - - -/** - * @param {number[]} balls - * @return {number} - */ -const getProbability = function (balls) { - const k = balls.length - const halfUsed = balls.reduce((acc, val) => acc + val, 0) / 2 - const startArray = new Array(k) - startArray.fill(0) - const perm = function (b1, b2) { - let p1, p2, s1, s2 - s1 = b1.reduce((acc, val) => acc + val, 0) - s2 = b2.reduce((acc, val) => acc + val, 0) - const fact = function (n) { - let f = 1 - for (let i = 2; i <= n; i++) f *= i - return f - } - p1 = fact(s1) - p2 = fact(s2) - b1.forEach((val) => { - if (val > 1) p1 /= fact(val) - }) - b2.forEach((val) => { - if (val > 1) p2 /= fact(val) - }) - return p1 * p2 - } - - const getValidCombos = function (ballsUsed, colorNum = 0) { - let box1Used = ballsUsed.reduce((acc, val) => acc + val, 0) - let matches = { good: 0, total: 0 }, - thisColorMax = halfUsed - box1Used - if (colorNum === k - 1) { - if (thisColorMax > balls[colorNum]) return { good: 0, total: 0 } - ballsUsed[colorNum] = thisColorMax - let ballsLeft = [] - let colorsUsed = [0, 0] - for (let i = 0; i < k; i++) { - ballsLeft[i] = balls[i] - ballsUsed[i] - if (ballsUsed[i] > 0) colorsUsed[0]++ - if (ballsLeft[i] > 0) colorsUsed[1]++ - } - let permutations = perm(ballsUsed, ballsLeft, k) - return { - good: colorsUsed[1] === colorsUsed[0] ? permutations : 0, - total: permutations, - } - } - thisColorMax = Math.min(thisColorMax, balls[colorNum]) - for (let i = 0; i <= thisColorMax; i++) { - let match = getValidCombos([...ballsUsed], colorNum + 1) - matches = { - good: matches.good + match.good, - total: matches.total + match.total, - } - ballsUsed[colorNum]++ - } - return matches - } - let res = getValidCombos(startArray) - return res.good / res.total -} diff --git a/1478-allocate-mailboxes.js b/1478-allocate-mailboxes.js index 0281bb7c..c6f04ea2 100644 --- a/1478-allocate-mailboxes.js +++ b/1478-allocate-mailboxes.js @@ -25,38 +25,3 @@ const minDistance = function (A, K) { return dp[n - 1] } -// another - -/** - * @param {number[]} houses - * @param {number} k - * @return {number} - */ -function minDistance(houses, k) { - const n = houses.length, { abs, min } = Math, INF = Infinity - houses.sort((a, b) => a - b) - const costs = Array.from({ length: 100 }, () => Array(100).fill(0)) - const memo = Array.from({ length: 100 }, () => Array(100).fill(null)) - - for(let i = 0; i < n; i++) { - for(let j = 0; j < n; j++) { - const mid = houses[~~((i + j) >> 1)] - for (let k = i; k <= j; k++) costs[i][j] += abs(mid - houses[k]) - } - } - - return dp(k, 0) - - function dp(k, i) { - if (k === 0 && i === n) return 0 - if (k === 0 || i === n) return INF - if (memo[k][i] != null) return memo[k][i] - let res = INF - for (let j = i; j < n; j++) { - res = min(res, costs[i][j] + dp(k - 1, j + 1)) - } - - return memo[k][i] = res - } -} - diff --git a/148-sort-list.js b/148-sort-list.js index dbfacf1e..3a9f7942 100644 --- a/148-sort-list.js +++ b/148-sort-list.js @@ -44,149 +44,3 @@ function partition(head, tail) { return slow; } -// another - -/** - * Definition for singly-linked list. - * function ListNode(val, next) { - * this.val = (val===undefined ? 0 : val) - * this.next = (next===undefined ? null : next) - * } - */ -/** - * @param {ListNode} head - * @return {ListNode} - */ -function sortList(head) { - if(head == null || head.next == null) return head - let slow = head, fast = head, pre = null - while(fast && fast.next) { - pre = slow - slow = slow.next - fast = fast.next.next - } - pre.next = null - const left = sortList(head) - const right = sortList(slow) - return merge(left, right) -} - -function merge(left, right) { - const dummy = new ListNode() - let cur = dummy - while(left && right) { - if (left.val <= right.val) { - cur.next = left - left = left.next - } else { - cur.next = right - right = right.next - } - cur = cur.next - } - if(left) { - cur.next = left - } - - if(right) { - cur.next = right - } - - return dummy.next -} - - -// another - - function sortList(head) { - quickSort(head, null); - return head; - } - - function quickSort( head, tail){ - if (head == tail) { - return; - } - let slow = head, fast = head.next; - let p = head.val; - while (fast != tail){ - if (fast.val <= p){ - slow = slow.next; - swap(slow, fast); - } - fast = fast.next; - } - swap(head, slow); - quickSort(head, slow); - quickSort(slow.next, tail); - } - - function swap( node1, node2){ - let tmp = node1.val; - node1.val = node2.val; - node2.val = tmp; - } - -// another - -/** - * Definition for singly-linked list. - * function ListNode(val) { - * this.val = val; - * this.next = null; - * } - */ -/** - * @param {ListNode} head - * @return {ListNode} - */ - -const sortList = function(head) { - let dummy = new ListNode(0); - dummy.next = head; - const list = []; - let done = (null === head); - // Keep partitioning our list into bigger sublists length. Starting with a size of 1 and doubling each time - for (let step = 1; !done; step *= 2) { - done = true; - let prev = dummy; - let remaining = prev.next; - do { - // Split off two sublists of size step - for (let i = 0; i < 2; ++i) { - list[i] = remaining; - let tail = null; - for (let j = 0; j < step && null != remaining; ++j, remaining = remaining.next) { - tail = remaining; - } - // Terminate our sublist - if (null != tail) { - tail.next = null; - } - } - - // We're done if these are the first two sublists in this pass and they - // encompass the entire primary list - done &= (null == remaining); - - // If we have two sublists, merge them into one - if (null != list[1]) { - while (null != list[0] || null != list[1]) { - let idx = (null == list[1] || null != list[0] && list[0].val <= list[1].val) ? 0 : 1; - prev.next = list[idx]; - list[idx] = list[idx].next; - prev = prev.next; - } - - // Terminate our new sublist - prev.next = null; - } else { - // Only a single sublist, no need to merge, just attach to the end - prev.next = list[0]; - } - } while (null !== remaining); - } - return dummy.next; -} - - diff --git a/1490-clone-n-ary-tree.js b/1490-clone-n-ary-tree.js index e8cfe407..25139321 100644 --- a/1490-clone-n-ary-tree.js +++ b/1490-clone-n-ary-tree.js @@ -19,35 +19,3 @@ const cloneTree = function(root) { return node }; -// another - -/** - * // Definition for a Node. - * function Node(val, children) { - * this.val = val === undefined ? 0 : val; - * this.children = children === undefined ? [] : children; - * }; - */ - -/** - * @param {Node} node - * @return {Node} - */ -const cloneTree = function(root) { - if (root === null) return null - const Q = [] - const rootCopy = new Node(root.val) - Q.push([root, rootCopy]) - while (Q.length) { - const temp = Q.shift() - const node = temp[0] - const copy = temp[1] - node.children.forEach((child) => { - const copyChild = new Node(child.val) - copy.children.push(copyChild) - Q.push([child, copyChild]) - }) - } - - return rootCopy -}; diff --git a/1494-parallel-courses-ii.js b/1494-parallel-courses-ii.js index 0882499f..0a0ff296 100644 --- a/1494-parallel-courses-ii.js +++ b/1494-parallel-courses-ii.js @@ -49,50 +49,3 @@ function bitCount(n) { return (((n + (n >> 4)) & 0xf0f0f0f) * 0x1010101) >> 24 } -// another - -/** - * @param {number} n - * @param {number[][]} dependencies - * @param {number} k - * @return {number} - */ -const minNumberOfSemesters = function (n, dependencies, k) { - const pre = Array(n).fill(0) - const limit = 1 << n - for(const [p, v] of dependencies) { - pre[v - 1] |= (1 << (p - 1)) - } - const dp = Array(limit).fill(Infinity) - dp[0] = 0 - - for(let learned = 0; learned < limit; learned++) { - let wait = 0 - for(let i = 0; i < n; i++) { - if( (learned & pre[i]) === pre[i]) { - wait |= (1 << i) - } - } - wait = wait & (~learned) - for(let sub = wait; sub; sub = (sub - 1) & wait) { - if(bitCnt(sub) > k) continue - const mask = learned | sub - dp[mask] = Math.min(dp[mask], dp[learned] + 1) - } - } - - return dp[limit - 1] -} - -function bitCnt(num) { - let res = 0 - while(num) { - num &= (num - 1) - res++ - } - - return res -} - - - diff --git a/1499-max-value-of-equation.js b/1499-max-value-of-equation.js index d005a055..f9dcee50 100644 --- a/1499-max-value-of-equation.js +++ b/1499-max-value-of-equation.js @@ -23,94 +23,3 @@ const findMaxValueOfEquation = function (points, k) { return res } -// another - -/** - * @param {number[][]} points - * @param {number} k - * @return {number} - */ -const findMaxValueOfEquation = function (points, k) { - const pq = new PriorityQueue((a, b) => - a[0] === b[0] ? a[1] < b[1] : b[0] < a[0] - ) - let res = -Infinity - for (let point of points) { - while (!pq.isEmpty() && point[0] - pq.peek()[1] > k) { - pq.pop() - } - if (!pq.isEmpty()) { - res = Math.max(res, pq.peek()[0] + point[0] + point[1]) - } - pq.push([point[1] - point[0], point[0]]) - } - return res -} - -class PriorityQueue { - constructor(comparator = (a, b) => a > b) { - this.heap = [] - this.top = 0 - this.comparator = comparator - } - size() { - return this.heap.length - } - isEmpty() { - return this.size() === 0 - } - peek() { - return this.heap[this.top] - } - push(...values) { - values.forEach((value) => { - this.heap.push(value) - this.siftUp() - }) - return this.size() - } - pop() { - const poppedValue = this.peek() - const bottom = this.size() - 1 - if (bottom > this.top) { - this.swap(this.top, bottom) - } - this.heap.pop() - this.siftDown() - return poppedValue - } - replace(value) { - const replacedValue = this.peek() - this.heap[this.top] = value - this.siftDown() - return replacedValue - } - - parent = (i) => ((i + 1) >>> 1) - 1 - left = (i) => (i << 1) + 1 - right = (i) => (i + 1) << 1 - greater = (i, j) => this.comparator(this.heap[i], this.heap[j]) - swap = (i, j) => ([this.heap[i], this.heap[j]] = [this.heap[j], this.heap[i]]) - siftUp = () => { - let node = this.size() - 1 - while (node > this.top && this.greater(node, this.parent(node))) { - this.swap(node, this.parent(node)) - node = this.parent(node) - } - } - siftDown = () => { - let node = this.top - while ( - (this.left(node) < this.size() && this.greater(this.left(node), node)) || - (this.right(node) < this.size() && this.greater(this.right(node), node)) - ) { - let maxChild = - this.right(node) < this.size() && - this.greater(this.right(node), this.left(node)) - ? this.right(node) - : this.left(node) - this.swap(node, maxChild) - node = maxChild - } - } -} diff --git a/15-3sum.js b/15-3sum.js index 498fc859..661fd438 100755 --- a/15-3sum.js +++ b/15-3sum.js @@ -29,64 +29,3 @@ const threeSum = function(nums) { return res }; -// another - - -/** - * @param {number[]} nums - * @return {number[][]} - */ -const threeSum = function (nums) { - nums.sort((a, b) => a - b) - const res = [] - let lo, hi, sum - for (let i = 0; i < nums.length - 2; i++) { - if (nums[i] > 0) break - if (nums[i] === nums[i - 1]) continue - if (i === 0 || (i > 0 && nums[i] !== nums[i - 1])) { - lo = i + 1 - hi = nums.length - 1 - sum = 0 - nums[i] - while (lo < hi) { - if (nums[lo] + nums[hi] === sum) { - res.push([nums[i], nums[lo], nums[hi]]) - while (lo < hi && nums[lo] === nums[lo + 1]) lo += 1 - while (lo < hi && nums[hi] === nums[hi - 1]) hi -= 1 - lo += 1 - hi -= 1 - } else if (nums[lo] + nums[hi] < sum) lo++ - else hi-- - } - } - } - return res -} - -// another - -/** - * @param {number[]} nums - * @return {number[][]} - */ -const threeSum = function(nums) { - const res = [], n = nums.length - nums.sort((a, b) => a - b) - for(let i = 0; i < n - 2; i++) { - let l = i + 1, r = n - 1, target = -nums[i] - if(i === 0 || (i > 0 && nums[i] !== nums[i - 1])) { - while(l < r) { - if(nums[l] + nums[r] === target) { - res.push([nums[i], nums[l], nums[r]]) - while(l < n - 1 && nums[l] === nums[l + 1]) l++ - while(r > 0 && nums[r] === nums[r - 1]) r-- - r-- - l++ - } else if(nums[l] + nums[r] > target) { - r-- - } else l++ - } - } - } - - return res -}; diff --git a/1506-find-root-of-n-ary-tree.js b/1506-find-root-of-n-ary-tree.js index 7f1316af..e0f7fd68 100644 --- a/1506-find-root-of-n-ary-tree.js +++ b/1506-find-root-of-n-ary-tree.js @@ -24,30 +24,3 @@ const findRoot = function(tree) { return null }; -// another - -/** - * // Definition for a Node. - * function Node(val, children) { - * this.val = val === undefined ? 0 : val; - * this.children = children === undefined ? [] : children; - * }; - */ - -/** - * @param {Node[]} tree - * @return {Node} - */ -const findRoot = function(tree) { - let sum = 0 - for(let n of tree) { - sum ^= n.val - for(let c of n.children) { - sum ^= c.val - } - } - for(let n of tree) { - if(n.val === sum) return n - } - return null -}; diff --git a/151-reverse-words-in-a-string.js b/151-reverse-words-in-a-string.js index bc6c0f9d..e7e7b072 100755 --- a/151-reverse-words-in-a-string.js +++ b/151-reverse-words-in-a-string.js @@ -10,27 +10,3 @@ const reverseWords = function(str) { .join(" "); }; -// another - -/** - * @param {string} str - * @returns {string} - */ -const reverseWords = function (s) { - let sb = '' - const n = s.length - let i = n - 1 - while (i >= 0) { - if (s.charAt(i) == ' ') { - i-- - continue - } - let j = i - 1 - while (j >= 0 && s.charAt(j) != ' ') j-- - sb += ' ' - sb += s.slice(j + 1, i + 1) - i = j - 1 - } - if (sb.length > 0) sb = sb.slice(1) - return sb -} diff --git a/152-maximum-product-subarray.js b/152-maximum-product-subarray.js index 8effa588..d94fdfe8 100644 --- a/152-maximum-product-subarray.js +++ b/152-maximum-product-subarray.js @@ -14,97 +14,3 @@ const maxProduct = function(nums) { return res }; -// another - -/** - * @param {number[]} nums - * @return {number} - */ -const maxProduct = function(nums) { - let A = nums - let r = A[0]; - - // imax/imin stores the max/min product of - // subarray that ends with the current number A[i] - for (let i = 1, imax = r, imin = r, n = A.length; i < n; i++) { - if (A[i] < 0) { - let tmp = imax - imax = imin - imin = tmp - }; - // max/min product for the current number is either the current number itself - // or the max/min by the previous number times the current one - imax = Math.max(A[i], imax * A[i]); - imin = Math.min(A[i], imin * A[i]); - - // the newly computed max value is a candidate for our global result - r = Math.max(r, imax); - } - return r; -}; - -// another - -/** - * @param {number[]} nums - * @return {number} - */ -const maxProduct = function(nums) { - if(nums.length == 1)return nums[0]; - let dpMax = nums[0]; - let dpMin = nums[0]; - let max = nums[0]; - for (let i = 1; i < nums.length; i++) { - let k = dpMax*nums[i]; - let m = dpMin*nums[i]; - dpMax = Math.max(nums[i], Math.max(k, m)); - dpMin = Math.min(nums[i], Math.min(k, m)); - max = Math.max(dpMax, max); - } - return max; -}; - -// another - -/** - * @param {number[]} nums - * @return {number} - */ -const maxProduct = function(nums) { - const n = nums.length - let max, min - let res = max = min = nums[0] - for(let i = 1; i < n; i++) { - if(nums[i] < 0) [max, min] = [min, max] - max = Math.max(nums[i], nums[i] * max) - min = Math.min(nums[i], nums[i] * min) - res = Math.max(res, max) - } - return res -}; - - -// another - -/** - * @param {number[]} nums - * @return {number} - */ -const maxProduct = function (nums) { - if(nums == null || nums.length === 0) return 0 - const n = nums.length - let res = nums[0] - for(let i = 1, min = res, max = res; i < n; i++) { - if(nums[i] < 0) { - let tmax = max, tmin = min - min = Math.min(nums[i], tmax * nums[i]) - max = Math.max(nums[i], tmin * nums[i]) - } else { - min = Math.min(nums[i], min * nums[i]) - max = Math.max(nums[i], max * nums[i]) - } - res = Math.max(res, max) - } - - return res -} diff --git a/1523-count-odd-numbers-in-an-interval-range.js b/1523-count-odd-numbers-in-an-interval-range.js index 2c47f34f..35b6c635 100644 --- a/1523-count-odd-numbers-in-an-interval-range.js +++ b/1523-count-odd-numbers-in-an-interval-range.js @@ -18,13 +18,3 @@ const countOdds = function(low, high) { }; -// another - -/** - * @param {number} low - * @param {number} high - * @return {number} - */ -const countOdds = function(low, high) { - return ~~((high + 1) / 2) - (~~(low / 2)) -}; diff --git a/1524-number-of-sub-arrays-with-odd-sum.js b/1524-number-of-sub-arrays-with-odd-sum.js index 1b3fed50..77195049 100644 --- a/1524-number-of-sub-arrays-with-odd-sum.js +++ b/1524-number-of-sub-arrays-with-odd-sum.js @@ -22,28 +22,3 @@ const numOfSubarrays = function(arr) { return res % mod }; -// another - -/** - * @param {number[]} arr - * @return {number} - */ -const numOfSubarrays = function(arr) { - const n = arr.length, mod = 1e9 + 7 - - let oc = 0, ec = 1 - let sum = 0 - let res = 0 - for(let i = 0; i < n; i++) { - sum += arr[i] - if(sum % 2 === 1) { - res += ec - oc++ - } else { - res += oc - ec++ - } - } - - return res % mod -}; diff --git a/1525-number-of-good-ways-to-split-a-string.js b/1525-number-of-good-ways-to-split-a-string.js index 4c443bc6..095a301c 100644 --- a/1525-number-of-good-ways-to-split-a-string.js +++ b/1525-number-of-good-ways-to-split-a-string.js @@ -28,32 +28,3 @@ const numSplits = function(s) { }; -// another - - -/** - * @param {string} s - * @return {number} - */ -const numSplits = function(s) { - const arr = Array(26).fill(0) - const a = 'a'.charCodeAt(0) - for(let i = 0, len = s.length; i < len; i++) { - arr[s.charCodeAt(i) - a]++ - } - const cur = Array(26).fill(0) - let res = 0 - for(let i = 0, len = s.length; i < len - 1; i++) { - cur[s.charCodeAt(i) - a]++ - let tmp = false, clone = arr.slice() - for(let j = 0; j < 26; j++) { - clone[j] -= cur[j] - } - const curNum = cur.reduce((ac, e) => ac + (e > 0 ? 1 : 0), 0) - const cloneNum = clone.reduce((ac, e) => ac + (e > 0 ? 1 : 0), 0) - if(curNum === cloneNum) res++ - } - - return res -}; - diff --git a/1526-minimum-number-of-increments-on-subarrays-to-form-a-target-array.js b/1526-minimum-number-of-increments-on-subarrays-to-form-a-target-array.js index 8464391c..103cffa3 100644 --- a/1526-minimum-number-of-increments-on-subarrays-to-form-a-target-array.js +++ b/1526-minimum-number-of-increments-on-subarrays-to-form-a-target-array.js @@ -12,18 +12,3 @@ const minNumberOperations = function(target) { return res } -// another - -/** - * @param {number[]} target - * @return {number} - */ -const minNumberOperations = function(target) { - let totalOperations = target[0]; - for (let i = 1; i < target.length; ++i) { - if (target[i] > target[i-1]) { - totalOperations += target[i] - target[i-1]; - } - } - return totalOperations; -}; diff --git a/1531-string-compression-ii.js b/1531-string-compression-ii.js index 31a2e728..0c968012 100644 --- a/1531-string-compression-ii.js +++ b/1531-string-compression-ii.js @@ -25,37 +25,3 @@ const getLengthOfOptimalCompression = function(s, k) { return counter(0, '', 0, k) }; -// another - -const getLengthOfOptimalCompression = function (s, k) { - const n = s.length - const dp = new Array(n + 1).fill(n).map((row) => new Array(k + 1).fill(n)) - dp[0][0] = 0 - - for (let i = 1; i <= n; i++) { - for (let j = 0; j <= k; j++) { - let letterCount = 0 - let deletion = 0 - // keep s[i], compress same letters, remove different letters - for (let l = i; l >= 1; l--) { - if (s.charAt(l - 1) === s.charAt(i - 1)) letterCount++ - else deletion++ - // places = length needed to rep compressed letters. - // 0 places for count = 1,0, 1 place = <10, 10-99 requires 2 places, 100+ requires 3 - let places = 0 - if (letterCount >= 100) places = 3 - else if (letterCount >= 10) places = 2 - else if (letterCount >= 2) places = 1 - if (j - deletion >= 0) { - dp[i][j] = Math.min(dp[i][j], dp[l - 1][j - deletion] + 1 + places) - } - } - // delete - if (j > 0) { - dp[i][j] = Math.min(dp[i][j], dp[i - 1][j - 1]) - } - } - } - return dp[n][k] -} - diff --git a/1537-get-the-maximum-score.js b/1537-get-the-maximum-score.js index bf35ef7b..5262a11f 100644 --- a/1537-get-the-maximum-score.js +++ b/1537-get-the-maximum-score.js @@ -71,38 +71,3 @@ const maxSum = function(nums1, nums2) { return Math.max(a, b) % mod; }; -// another - -/** - * @param {number[]} nums1 - * @param {number[]} nums2 - * @return {number} - */ -const maxSum = function(nums1, nums2) { - const len1 = nums1.length, len2 = nums2.length - const mod = 10 ** 9 + 7 - const map = new Map() - for(let i = 0; i < len1 - 1; i++) { - if(!map.has(nums1[i])) map.set(nums1[i], []) - map.get(nums1[i]).push(nums1[i + 1]) - } - for(let j = 0; j < len2 - 1; j++) { - if(!map.has(nums2[j])) map.set(nums2[j], []) - map.get(nums2[j]).push(nums2[j + 1]) - } - const memo = new Map() - return Math.max(greedy(nums1[0], map, memo), greedy(nums2[0], map, memo)) % mod -}; - -function greedy(cur, map, memo) { - if(memo.has(cur)) return memo.get(cur) - if(!map.has(cur)) return cur - let res = 0 - for(let next of map.get(cur)) { - const tmp = greedy(next, map, memo) - if(tmp > res) res = tmp - } - res += cur - memo.set(cur, res) - return res -} diff --git a/154-find-minimum-in-rotated-sorted-array-ii.js b/154-find-minimum-in-rotated-sorted-array-ii.js index 1ed12103..51bbde05 100644 --- a/154-find-minimum-in-rotated-sorted-array-ii.js +++ b/154-find-minimum-in-rotated-sorted-array-ii.js @@ -11,20 +11,3 @@ const findMin = function(nums) { return nums[0] }; -// another - -/** - * @param {number[]} nums - * @return {number} - */ -const findMin = function(nums) { - let lo = 0, - hi = nums.length - 1 - while (lo < hi) { - let mid = Math.floor(lo + (hi - lo) / 2) - if (nums[mid] > nums[hi]) lo = mid + 1 - else if (nums[mid] < nums[hi]) hi = mid - else hi-- - } - return nums[lo] -} diff --git a/1541-minimum-insertions-to-balance-a-parentheses-string.js b/1541-minimum-insertions-to-balance-a-parentheses-string.js index f9f78d74..f463a40d 100644 --- a/1541-minimum-insertions-to-balance-a-parentheses-string.js +++ b/1541-minimum-insertions-to-balance-a-parentheses-string.js @@ -27,57 +27,3 @@ const minInsertions = function(s) { return insert }; -// another - -/** - * @param {string} s - * @return {number} - */ -const minInsertions = function(s) { - let res = 0, right = 0; - for (let i = 0; i < s.length; ++i) { - if (s.charAt(i) == '(') { - if (right % 2 > 0) { - right--; - res++; - } - right += 2; - } else { - right--; - if (right < 0) { - right += 2; - res++; - } - } - } - return right + res; -}; - -// another - -/** - * @param {string} s - * @return {number} - */ -const minInsertions = function(s) { - let add = 0, req = 0 // number of parentheses added, number of closing parentheses required - for(let ch of s) { - if(ch === '(') { - req += 2 - if(req % 2 === 1) { - add++ - req-- - } - } else { - if(req === 0) { - add++ - req++ - }else { - req-- - } - } - } - - return add + req -}; - diff --git a/1542-find-longest-awesome-substring.js b/1542-find-longest-awesome-substring.js index aaa37653..9a2919df 100644 --- a/1542-find-longest-awesome-substring.js +++ b/1542-find-longest-awesome-substring.js @@ -16,26 +16,3 @@ const longestAwesome = function (s) { return res } -// another - -/** - * @param {string} s - * @return {number} - */ -const longestAwesome = function(s) { - const n = s.length, { max, min } = Math - const dp = Array(2 ** 10).fill(n) - let res = 0, mask = 0 - dp[0] = -1 - for(let i = 0; i < n; i++) { - mask ^= (1 << parseInt(s[i])) - res = max(res, i - dp[mask]) - for(let j = 0; j <= 9; j++) { - const tmp = mask ^ (1 << j) - res = max(res, i - dp[tmp]) - } - dp[mask] = min(i, dp[mask]) - } - - return res -}; diff --git a/1547-minimum-cost-to-cut-a-stick.js b/1547-minimum-cost-to-cut-a-stick.js index 637bb477..5d2aa66a 100644 --- a/1547-minimum-cost-to-cut-a-stick.js +++ b/1547-minimum-cost-to-cut-a-stick.js @@ -22,27 +22,3 @@ const minCost = function(n, cuts) { } }; -// another - -/** - * @param {number} n - * @param {number[]} cuts - * @return {number} - */ -const minCost = function (n, cuts) { - cuts.push(0, n) - cuts.sort((a, b) => a - b) - const N = cuts.length, - dp = Array.from({ length: N }, () => Array(N).fill(Infinity)) - for(let i = 1; i < N; i++) dp[i - 1][i] = 0 - for(let i = 2; i < N; i++) dp[i - 2][i] = cuts[i] - cuts[i - 2] - for (let l = 4; l <= N; l++) { - for (let i = 0; i <= N - l; i++) { - const j = i + l - 1 - for (let k = i + 1; k < j; k++) { - dp[i][j] = Math.min(dp[i][j], cuts[j] - cuts[i] + dp[i][k] + dp[k][j]) - } - } - } - return dp[0][N - 1] -} diff --git a/1554-strings-differ-by-one-character.js b/1554-strings-differ-by-one-character.js index 642d2ad0..b72011bb 100644 --- a/1554-strings-differ-by-one-character.js +++ b/1554-strings-differ-by-one-character.js @@ -16,37 +16,3 @@ return false }; -// another - -/** - * @param {string[]} dict - * @return {boolean} - */ -const differByOne = function (dict) { - const M = dict.length, - N = dict[0].length, - hash = Array(M).fill(0), - ord = (c) => c.charCodeAt(0), - MOD = 1e13, seen = new Set(), - zPlusOne = 'z'.charCodeAt(0) - // 1. generate each i-th rolling hash - for (let i = 0; i < M; ++i) { - let base = 1 - for (let j = 0; j < N; ++j) { - hash[i] = (hash[i] + base * ord(dict[i][j])) % MOD - base = (zPlusOne * base) % MOD - } - } - // 2. remove each j-th char from each i-th rolling hash to find a diff collision - for (let i = 0; i < M; ++i) { - let base = 1 - for (let j = 0; j < N; ++j) { - const diff = (hash[i] - base * ord(dict[i][j])) % MOD - if (seen.has(diff)) return true - seen.add(diff) - base = (zPlusOne * base) % MOD - } - } - return false -} - diff --git a/1559-detect-cycles-in-2d-grid.js b/1559-detect-cycles-in-2d-grid.js index a3666072..2dc27adb 100644 --- a/1559-detect-cycles-in-2d-grid.js +++ b/1559-detect-cycles-in-2d-grid.js @@ -44,58 +44,3 @@ const containsCycle = function (grid) { return res } -// another - -/** - * @param {character[][]} grid - * @return {boolean} - */ -const containsCycle = function (grid) { - const wholePath = (r, c, letter, component, last = [-1, -1]) => { - const dirs = [ - [0, -1], - [0, 1], - [-1, 0], - [1, 0], - ] - const tmp = grid[r][c] - grid[r][c] = component - const nextSteps = dirs - .map((x) => [x[0] + r, x[1] + c]) - .filter( - (x) => - x[0] >= 0 && x[0] < grid.length && x[1] >= 0 && x[1] < grid[0].length - ) - for (let step of nextSteps) { - if (step[0] === last[0] && last[1] === step[1]) { - continue - } - if (grid[step[0]][step[1]] === component) { - return true - } - if (grid[step[0]][step[1]] === letter) { - let outcome = wholePath(step[0], step[1], letter, component, [r, c]) - if (outcome) { - return true - } - } - } - return false - } - - let component = 1 - for (let r = 0; r < grid.length; r++) { - for (let c = 0; c < grid[0].length; c++) { - const letter = grid[r][c] - if (typeof letter === 'string') { - grid[r][c] = component - const outcome = wholePath(r, c, letter, component) - if (outcome) { - return true - } - component++ - } - } - } - return false -} diff --git a/156-binary-tree-upside-down.js b/156-binary-tree-upside-down.js index f5e6e354..b7ed095e 100644 --- a/156-binary-tree-upside-down.js +++ b/156-binary-tree-upside-down.js @@ -22,16 +22,3 @@ const upsideDownBinaryTree = function(root) { return parent }; -// another - -const upsideDownBinaryTree = function(root) { - if (root == null || root.left == null) { - return root - } - const newRoot = upsideDownBinaryTree(root.left) - root.left.left = root.right - root.left.right = root - root.left = null - root.right = null - return newRoot -} diff --git a/1564-put-boxes-into-the-warehouse-i.js b/1564-put-boxes-into-the-warehouse-i.js index cf4ca8e3..1eaa3052 100644 --- a/1564-put-boxes-into-the-warehouse-i.js +++ b/1564-put-boxes-into-the-warehouse-i.js @@ -16,44 +16,3 @@ const maxBoxesInWarehouse = function(boxes, warehouse) { return j }; -// another - -/** - * @param {number[]} boxes - * @param {number[]} warehouse - * @return {number} - */ -const maxBoxesInWarehouse = function(boxes, warehouse) { - if(warehouse == null || warehouse.length === 0) return 0 - const m = boxes.length, n = warehouse.length - for(let i = 1; i < n; i++) { - warehouse[i] = Math.min(warehouse[i], warehouse[i - 1]) - } - boxes.sort((a, b) => a - b) - let res = 0 - for(let i = n - 1; i >= 0; i--) { - if(res < m && boxes[res] <= warehouse[i]) res++ - } - return res -}; - -// another - -/** - * @param {number[]} boxes - * @param {number[]} warehouse - * @return {number} - */ -const maxBoxesInWarehouse = function(boxes, warehouse) { - if(warehouse == null || warehouse.length === 0) return 0 - const m = boxes.length, n = warehouse.length - boxes.sort((a, b) => a - b) - let i = m - 1, res = 0 - for(let house of warehouse) { - while(i >= 0 && boxes[i] > house) i-- - if(i === -1) return res - res++ - i-- - } - return res -}; diff --git a/1570-dot-product-of-two-sparse-vectors.js b/1570-dot-product-of-two-sparse-vectors.js index 682b656d..ea47e81b 100644 --- a/1570-dot-product-of-two-sparse-vectors.js +++ b/1570-dot-product-of-two-sparse-vectors.js @@ -27,41 +27,3 @@ SparseVector.prototype.dotProduct = function(vec) { // let v2 = new SparseVector(nums2); // let ans = v1.dotProduct(v2); -// another - -class SparseVector { - /** - * @param {number[]} nums - * @return {SparseVector} - */ - constructor(nums) { - // Space: O(n) - this.seen = new Map() // index -> value - for (let i = 0; i < nums.length; ++i) { - if (nums[i] !== 0) { - this.seen.set(i, nums[i]) - } - } - } - - /** - * Return the dotProduct of two sparse vectors - * @param {SparseVector} vec - * @return {number} - */ - dotProduct(vec) { - // Time: O(n) - let sum = 0 - for (const [i, val] of vec.seen) { - if (this.seen.has(i)) { - sum += val * this.seen.get(i) - } - } - return sum - } -} - -// Your SparseVector object will be instantiated and called as such: -// let v1 = new SparseVector(nums1); -// let v2 = new SparseVector(nums2); -// let ans = v1.dotProduct(v2); diff --git a/1576-replace-all-s-to-avoid-consecutive-repeating-characters.js b/1576-replace-all-s-to-avoid-consecutive-repeating-characters.js index 51a743c9..eb9b799f 100644 --- a/1576-replace-all-s-to-avoid-consecutive-repeating-characters.js +++ b/1576-replace-all-s-to-avoid-consecutive-repeating-characters.js @@ -27,30 +27,3 @@ const modifyString = function(s) { }; -// another - -/** - * @param {string} s - * @return {string} - */ -const modifyString = function(s) { - const arr = s.split('') - for(let i = 0, n = s.length; i < n; i++) { - const cur = arr[i] - if(cur === '?') { - for(let j = 0, a = 'a'.charCodeAt(0); j < 26; j++) { - const ch = String.fromCharCode(a + j) - if( - (i === 0 || arr[i - 1] !== ch) && - (i === n - 1 || arr[i + 1] !== ch) - ) { - - arr[i] = ch - break - } - } - } - } - - return arr.join('') -}; diff --git a/1586-binary-search-tree-iterator-ii.js b/1586-binary-search-tree-iterator-ii.js index 305d57e3..d2e661f2 100644 --- a/1586-binary-search-tree-iterator-ii.js +++ b/1586-binary-search-tree-iterator-ii.js @@ -66,75 +66,3 @@ function helper(node, res) { * var param_4 = obj.prev() */ -// another - -/** - * Definition for a binary tree node. - * function TreeNode(val, left, right) { - * this.val = (val===undefined ? 0 : val) - * this.left = (left===undefined ? null : left) - * this.right = (right===undefined ? null : right) - * } - */ -/** - * @param {TreeNode} root - */ -const BSTIterator = function (root) { - this.nums = [] - this.stack = [] - this.node = root - this.i = 0 // pointer to next node - this.size = 0 -} - -/** - * @return {boolean} - */ -BSTIterator.prototype.hasNext = function () { - return this.i < this.size || this.stack.length > 0 || !!this.node -} - -/** - * @return {number} - */ -BSTIterator.prototype.next = function () { - if (this.i < this.size) return this.nums[this.i++] - if (this.stack.length || this.node) { - while (this.node) { - this.stack.push(this.node) - this.node = this.node.left - } - this.node = this.stack.pop() - this.i += 1 - this.size += 1 - const val = this.node.val - this.nums.push(val) - this.node = this.node.right - return val - } - return -1 -} - -/** - * @return {boolean} - */ -BSTIterator.prototype.hasPrev = function () { - return this.i - 2 >= 0 -} - -/** - * @return {number} - */ -BSTIterator.prototype.prev = function () { - return this.nums[--this.i - 1] -} - -/** - * Your BSTIterator object will be instantiated and called as such: - * var obj = new BSTIterator(root) - * var param_1 = obj.hasNext() - * var param_2 = obj.next() - * var param_3 = obj.hasPrev() - * var param_4 = obj.prev() - */ - diff --git a/1589-maximum-sum-obtained-of-any-permutation.js b/1589-maximum-sum-obtained-of-any-permutation.js index 54b7f955..997c5a90 100644 --- a/1589-maximum-sum-obtained-of-any-permutation.js +++ b/1589-maximum-sum-obtained-of-any-permutation.js @@ -19,61 +19,3 @@ const maxSumRangeQuery = function (nums, requests) { return res } -// another - -/** - * @param {number[]} nums - * @param {number[][]} requests - * @return {number} - */ -const maxSumRangeQuery = function (nums, requests) { - const n = nums.length, arr = Array(n + 1).fill(0) - for(let [s, e] of requests) { - arr[s] += 1 - arr[e + 1] -= 1 - } - for(let i = 0, cur = 0; i < n; i++) { - cur += arr[i] - arr[i] = cur - } - nums.sort((a, b) => b - a) - arr.sort((a, b) => b - a) - const mod = 1e9 + 7 - let res = 0 - for(let i = 0; i < n; i++) { - if (arr[i] <= 0) break - res = (res + nums[i] * arr[i]) % mod - } - return res -} - -// another - -/** - * @param {number[]} nums - * @param {number[][]} requests - * @return {number} - */ -const maxSumRangeQuery = function (nums, requests) { - const n = nums.length - - const arr = Array(n + 1).fill(0) - for(const [s, e] of requests) { - arr[s] += 1 - arr[e + 1] -= 1 - } - for(let i = 1; i <= n; i++) { - arr[i] += arr[i - 1] - } - arr.sort((a, b) => b - a) - nums.sort((a, b) => b - a) - let res = 0 - const mod = 1e9 + 7 - - for (let i = 0; i < n; i++) { - if(arr[i] <= 0) break - res = (res + nums[i] * arr[i]) % mod - } - - return res -} diff --git a/1590-make-sum-divisible-by-p.js b/1590-make-sum-divisible-by-p.js index 91857ae1..d1abc112 100644 --- a/1590-make-sum-divisible-by-p.js +++ b/1590-make-sum-divisible-by-p.js @@ -23,40 +23,3 @@ const minSubarray = function(nums, p) { return res === n ? -1 : res }; -// another - -/** - * @param {number[]} nums - * @param {number} p - * @return {number} - */ -const minSubarray = function(nums, p) { - const diff = nums.reduce((a, b) => a + b, 0) % p; - let res = diff === 0 ? 0 : nums.length; - - for (let i = 0, sum = 0, map = {0: -1}; i < nums.length; i++) { - sum += nums[i]; - const target = (sum % p - diff + p) % p; - if (map[target] !== undefined) { - res = Math.min(res, i - map[target]); - } - map[sum % p] = i; - } - - return res === nums.length ? -1 : res; -}; - -/** - -Let pre[] be the prefix sum array, -then pre[i] is running prefix sum or prefix sum of i elements, -pre[j] is the prefix sum such that pre[i]-pre[j] is the subarray we -need to remove to make pre[n] (sum of all elements) divisible by p - -(pre[n] - (pre[i]-pre[j])) % p = 0 ... (remove a subarray to make pre[n] divisible by p) -=> pre[n] % p = (pre[i]-pre[j]) % p ... ((a-b)%m = a%m - b%m) -=> pre[j]%p = pre[i]%p - pre[n]%p ... (same property used above) -since RHS can be negative we make it positive modulus by adding p and taking modulus -=> pre[j]%p = (pre[i]%p - pre[n]%p + p) % p - -*/ diff --git a/1591-strange-printer-ii.js b/1591-strange-printer-ii.js index 4dd5b521..d330114d 100644 --- a/1591-strange-printer-ii.js +++ b/1591-strange-printer-ii.js @@ -50,78 +50,3 @@ const isPrintable = function (targetGrid) { } } -// another - -/** - * @param {number[][]} targetGrid - * @return {boolean} - */ -const isPrintable = function (targetGrid) { - /* - 1 -> 3 - 1 -> 4 - 1 -> 5 - 3 -> 4 - */ - - const dependencies = {} - - /* - 3: [mini, maxi, minj, maxj] - */ - const extents = {} - - for (let i = 0; i < targetGrid.length; i++) { - for (let j = 0; j < targetGrid[i].length; j++) { - const n = targetGrid[i][j] - let inf = Infinity - extents[n] = extents[n] || { - n, - mini: inf, - minj: inf, - maxi: -inf, - maxj: -inf, - } - extents[n].mini = Math.min(i, extents[n].mini) - extents[n].minj = Math.min(j, extents[n].minj) - extents[n].maxi = Math.max(i, extents[n].maxi) - extents[n].maxj = Math.max(j, extents[n].maxj) - } - } - - function canRemove(obj) { - for (let i = obj.mini; i <= obj.maxi; i++) { - for (let j = obj.minj; j <= obj.maxj; j++) { - const val = targetGrid[i][j] - if (val !== null && val !== obj.n) return false - } - } - return true - } - - function remove(obj) { - for (let i = obj.mini; i <= obj.maxi; i++) { - for (let j = obj.minj; j <= obj.maxj; j++) { - targetGrid[i][j] = null - } - } - delete extents[obj.n] - } - - while (Object.keys(extents).length > 0) { - let found = false - for (const n in extents) { - const obj = extents[n] - if (canRemove(obj)) { - remove(obj) - found = true - break - } - } - if (!found) { - return false - } - } - return true -} - diff --git a/1593-split-a-string-into-the-max-number-of-unique-substrings.js b/1593-split-a-string-into-the-max-number-of-unique-substrings.js index cfd06a65..48e93cde 100644 --- a/1593-split-a-string-into-the-max-number-of-unique-substrings.js +++ b/1593-split-a-string-into-the-max-number-of-unique-substrings.js @@ -20,32 +20,3 @@ function bt(str, cur, idx, useds) { } } -// another - -/** - * @param {string} s - * @return {number} - */ -const maxUniqueSplit = function (s) { - const N = s.length - let ans = -1 - let curr = new Set() - const backtrack = (pos) => { - if (pos === N) { - ans = Math.max(ans, curr.size) - return - } - if (curr.size + (N - pos) <= ans) return - for (let i = pos + 1; i <= N; i++) { - const a = s.slice(pos, i) - if (curr.has(a)) continue - curr.add(a) - backtrack(i) - curr.delete(a) - } - } - - backtrack(0) - return ans -} - diff --git a/1595-minimum-cost-to-connect-two-groups-of-points.js b/1595-minimum-cost-to-connect-two-groups-of-points.js index 43e3d060..9d606d6f 100644 --- a/1595-minimum-cost-to-connect-two-groups-of-points.js +++ b/1595-minimum-cost-to-connect-two-groups-of-points.js @@ -42,81 +42,3 @@ const connectTwoGroups = function(cost) { return dp[m][limit - 1] }; -// another - - -/** - * @param {number[][]} cost - * @return {number} - */ -const connectTwoGroups = function (cost) { - const min = Array(cost[0].length).fill(Infinity) - for (let j = 0; j < min.length; j++) { - for (let i = 0; i < cost.length; i++) { - min[j] = Math.min(min[j], cost[i][j]) - } - } - const dp = Array.from({ length: 13 }, () => Array(4096).fill(-1)) - return dfs(cost, min, 0, 0, dp) -} - -function dfs(cost, min, i, mask, dp) { - if (dp[i][mask] !== -1) return dp[i][mask] - let res = i >= cost.length ? 0 : Infinity - if (i >= cost.length) { - for (let j = 0; j < cost[0].length; j++) { - if ((mask & (1 << j)) === 0) res += min[j] - } - } else { - for (let j = 0; j < cost[0].length; j++) { - res = Math.min( - res, - cost[i][j] + dfs(cost, min, i + 1, mask | (1 << j), dp) - ) - } - } - dp[i][mask] = res - return res -} - -// another - -/** - * @param {number[][]} cost - * @return {number} - */ -const connectTwoGroups = function (cost) { - const n = cost.length - const m = cost[0].length - const con = 1 << m - const dp = Array(n + 1) - .fill(null) - .map(() => Array(con).fill(0)) - const min = Array(m).fill(Infinity) - for (let i = 0; i < n; i++) { - for (let j = 0; j < m; j++) { - min[j] = Math.min(min[j], cost[i][j]) - } - } - function dfs(i, mask) { - let res - if (dp[i][mask]) { - return dp[i][mask] - } else if (i >= n) { - res = 0 - for (let j = 0; j < m; j++) { - const binaryJ = 1 << j - if ((mask & binaryJ) === 0) res += min[j] - } - } else { - res = Infinity - for (let j = 0; j < m; j++) { - const binaryJ = 1 << j - res = Math.min(res, cost[i][j] + dfs(i + 1, mask | binaryJ)) - } - } - dp[i][mask] = res - return res - } - return dfs(0, 0) -} diff --git a/1597-build-binary-expression-tree-from-infix-expression.js b/1597-build-binary-expression-tree-from-infix-expression.js index 5678858f..cee75c34 100644 --- a/1597-build-binary-expression-tree-from-infix-expression.js +++ b/1597-build-binary-expression-tree-from-infix-expression.js @@ -56,53 +56,3 @@ const expTree = function (s) { } -// another - -/** - * Definition for a binary tree node. - * function Node(val, left, right) { - * this.val = (val===undefined ? " " : val) - * this.left = (left===undefined ? null : left) - * this.right = (right===undefined ? null : right) - * } - */ -/** - * @param {string} s - * @return {Node} - */ -const expTree = function(s) { - const list = s.split('') - const mdSet = new Set(['*', '/']) - const amSet = new Set(['+', '-']) - return parseExpression(list) - - function parseExpression(tokens) { - let lhs = parseTerm(tokens) - while(tokens.length && amSet.has(tokens[0])) { - const op = tokens.shift() - const rhs = parseTerm(tokens) - lhs = new Node(op, lhs, rhs) - } - return lhs - } - function parseTerm(tokens) { - let lhs = parseFactor(tokens) - while(tokens.length && mdSet.has(tokens[0])) { - const op = tokens.shift() - const rhs = parseFactor(tokens) - lhs = new Node(op, lhs, rhs) - } - return lhs - } - function parseFactor(tokens) { - if(tokens[0] === '(') { - tokens.shift() - const node = parseExpression(tokens) - tokens.shift() - return node - } else { - const token = tokens.shift() - return new Node(token) - } - } -}; diff --git a/160-intersection-of-two-linked-lists.js b/160-intersection-of-two-linked-lists.js index 21ae7fec..f519d336 100644 --- a/160-intersection-of-two-linked-lists.js +++ b/160-intersection-of-two-linked-lists.js @@ -20,53 +20,3 @@ const getIntersectionNode = function(headA, headB) { return a }; -// another - -/** - * Definition for singly-linked list. - * function ListNode(val) { - * this.val = val; - * this.next = null; - * } - */ - -/** - * @param {ListNode} headA - * @param {ListNode} headB - * @return {ListNode} - */ -const getIntersectionNode = function(headA, headB) { - let aend = null - let bend = null - let ahead = headA - let bhead = headB - while(headA !== null && headB !== null) { - if (aend !== null && bend !== null && aend !== bend) { - return null - } - - if (headA === headB) { - return headA - } - - if (headA.next === null) { - if(aend === null) { - aend = headA - } - headA = bhead - } else { - headA = headA.next - - } - if (headB.next === null) { - if(bend === null) { - bend = headB - } - headB = ahead - } else { - headB = headB.next - } - - } - -}; diff --git a/1601-maximum-number-of-achievable-transfer-requests.js b/1601-maximum-number-of-achievable-transfer-requests.js index b99d414f..03ae789a 100644 --- a/1601-maximum-number-of-achievable-transfer-requests.js +++ b/1601-maximum-number-of-achievable-transfer-requests.js @@ -30,37 +30,3 @@ const maximumRequests = function(n, requests) { }; -// another - -/** - * @param {number} n - * @param {number[][]} requests - * @return {number} - */ -const maximumRequests = function (n, requests) { - let max = 0 - helper(requests, 0, Array(n).fill(0), 0) - return max - - function helper(requests, index, count, num) { - // Traverse all n buildings to see if they are all 0. (means balanced) - if (index === requests.length) { - for (let i of count) { - if (0 !== i) { - return - } - } - max = Math.max(max, num) - return - } - // Choose this request - count[requests[index][0]]++ - count[requests[index][1]]-- - helper(requests, index + 1, count, num + 1) - count[requests[index][0]]-- - count[requests[index][1]]++ - - // Not Choose the request - helper(requests, index + 1, count, num) - } -} diff --git a/1605-find-valid-matrix-given-row-and-column-sums.js b/1605-find-valid-matrix-given-row-and-column-sums.js index f1ab5894..9dd15c4d 100644 --- a/1605-find-valid-matrix-given-row-and-column-sums.js +++ b/1605-find-valid-matrix-given-row-and-column-sums.js @@ -16,22 +16,3 @@ const restoreMatrix = function(rowSum, colSum) { return res; }; -// another - -/** - * @param {number[]} rowSum - * @param {number[]} colSum - * @return {number[][]} - */ -const restoreMatrix = function(rowSum, colSum) { - const m = rowSum.length, n = colSum.length - const res = Array.from({ length: m }, () => Array(n).fill(0)) - for(let i = 0; i < m; i++) { - for(let j = 0; j < n; j++) { - res[i][j] = Math.min(rowSum[i], colSum[j]) - rowSum[i] -= res[i][j] - colSum[j] -= res[i][j] - } - } - return res -}; diff --git a/1608-special-array-with-x-elements-greater-than-or-equal-x.js b/1608-special-array-with-x-elements-greater-than-or-equal-x.js index 9866bd82..f39cb80a 100644 --- a/1608-special-array-with-x-elements-greater-than-or-equal-x.js +++ b/1608-special-array-with-x-elements-greater-than-or-equal-x.js @@ -22,55 +22,3 @@ const specialArray = function(nums) { } }; -// another - -/** - * @param {number[]} nums - * @return {number} - */ -const specialArray = function (nums) { - nums.sort((a, b) => b - a) - let i = 0 - while(i < nums.length && nums[i] >= i) { - i++ - } - if(nums[i - 1] < i) return -1 - return i -}; - -// another - -/** - * @param {number[]} nums - * @return {number} - */ -const specialArray = function(nums) { - nums.sort((a, b) => b - a) - let left = 0, right = nums.length - while(left <= right) { - const mid = left + ((right - left) >> 1) - if(mid < nums[mid]) left = mid + 1 - else right = mid - 1 - } - // if we found i == nums[i], there will be i + 1 items - // larger or equal to i, which makes array not special. - return left < nums.length && left === nums[left] ? -1 : left -}; - -// another - -/** - * @param {number[]} nums - * @return {number} - */ -const specialArray = function(nums) { - const n = nums.length - nums.sort((a, b) => b - a) - let l = 0, r = n - while(l < r) { - const mid = l + ((r - l) >> 1) - if(nums[mid] > mid) l = mid + 1 - else r = mid - } - return l < n && l === nums[l] ? -1 : l -} diff --git a/1611-minimum-one-bit-operations-to-make-integers-zero.js b/1611-minimum-one-bit-operations-to-make-integers-zero.js index ec1f28f8..2f14ba38 100644 --- a/1611-minimum-one-bit-operations-to-make-integers-zero.js +++ b/1611-minimum-one-bit-operations-to-make-integers-zero.js @@ -13,32 +13,3 @@ const minimumOneBitOperations = function (n) { return Math.abs(res); }; -// another - -/** - * @param {number} n - * @return {number} - */ -const minimumOneBitOperations = function(n) { - let mask = n; - while (mask) { - mask >>= 1; - n ^= mask; - } - return n; -}; - -// another - -/** - * @param {number} n - * @return {number} - */ -const minimumOneBitOperations = function(n) { - n ^= n >> 16 - n ^= n >> 8 - n ^= n >> 4 - n ^= n >> 2 - n ^= n >> 1 - return n -}; diff --git a/1617-count-subtrees-with-max-distance-between-cities.js b/1617-count-subtrees-with-max-distance-between-cities.js index 3cc321c8..e34e8cc3 100644 --- a/1617-count-subtrees-with-max-distance-between-cities.js +++ b/1617-count-subtrees-with-max-distance-between-cities.js @@ -50,69 +50,3 @@ const countSubgraphsForEachDiameter = function (n, edges) { } }; -// another - -/** - * @param {number} n - * @param {number[][]} edges - * @return {number[]} - */ -const countSubgraphsForEachDiameter = function(n, edges) { - const graph = {} - for(const [u, v] of edges) { - if(graph[u - 1] == null) graph[u - 1] = [] - if(graph[v - 1] == null) graph[v - 1] = [] - graph[u - 1].push(v - 1) - graph[v - 1].push(u - 1) - } - const res = Array(n - 1).fill(0) - - for(let i = 0, len = 2 ** n; i < len; i++) { - const dis = maxDistance(i) - if(dis > 0) res[dis - 1]++ - } - - return res - - function bfs(src, cities) { - const visited = new Set([src]) - let q = [[src, 0]] - let maxDist = 0 - while(q.length) { - const tmp = [] - const size = q.length - for(let i = 0; i < size; i++) { - const [u, d] = q[i] - maxDist = d - for(const v of (graph[u] || [])) { - if(cities.has(v) && !visited.has(v)) { - visited.add(v) - tmp.push([v, d + 1]) - } - } - } - - q = tmp - } - - return [maxDist, visited] - } - - function maxDistance(state) { - const cities = new Set() - for(let i = 0; i < n; i++) { - if(state & (1 << i)) cities.add(i) - } - - let res = 0 - for(const e of cities) { - const [maxDist, visited] = bfs(e, cities) - if(visited.size < cities.size) return 0 - res = Math.max(res, maxDist) - } - - return res - } -}; - - diff --git a/162-find-peak-element.js b/162-find-peak-element.js index c3c9bd07..1624cbc4 100644 --- a/162-find-peak-element.js +++ b/162-find-peak-element.js @@ -15,20 +15,3 @@ const findPeakElement = function(nums) { return low; }; -// another - -/** - * @param {number[]} nums - * @return {number} - */ -const findPeakElement = function(nums) { - if(nums == null) return -1 - const len = nums.length - if(len === 1) return 0 - for(let i = 1; i < len; i++) { - if(i === 1 && nums[i] < nums[i - 1]) return 0 - else if(i === len - 1 && nums[i] > nums[i - 1]) return len - 1 - else if(nums[i] > nums[i - 1] && nums[i] > nums[i + 1]) return i - } - return -1 -}; diff --git a/1621-number-of-sets-of-k-non-overlapping-line-segments.js b/1621-number-of-sets-of-k-non-overlapping-line-segments.js index 58fca659..758ffc9a 100644 --- a/1621-number-of-sets-of-k-non-overlapping-line-segments.js +++ b/1621-number-of-sets-of-k-non-overlapping-line-segments.js @@ -14,29 +14,3 @@ const numberOfSets = function (n, k) { return res } -// another - -/** - * @param {number} n - * @param {number} k - * @return {number} - */ -const numberOfSets = function (n, k) { - // dp[i][k] as: the number of ways to generate - // k non-overlapping segments you can make using [0 ~ i]. - const dp = Array.from({ length: n }, () => Array(k + 1).fill(0)) - const MOD = 10 ** 9 + 7 - dp[1][1] = 1 - for (let i = 2; i < n; i++) dp[i][1] = ((i + 1) * i) / 2 - // sum[i][j] as: the number of ways to generate - // j - 1 segments from i - 1 points. - const sum = Array.from({ length: n }, () => Array(k + 1).fill(0)) - for (let i = 2; i < n; i++) { - for (let j = 2; j <= k; j++) { - if (j <= i) sum[i][j] = (sum[i - 1][j] + dp[i - 1][j - 1]) % MOD - dp[i][j] = (sum[i][j] + dp[i - 1][j]) % MOD - } - } - return dp[n - 1][k] -} - diff --git a/1625-lexicographically-smallest-string-after-applying-operations.js b/1625-lexicographically-smallest-string-after-applying-operations.js index f624d743..f0d5feac 100644 --- a/1625-lexicographically-smallest-string-after-applying-operations.js +++ b/1625-lexicographically-smallest-string-after-applying-operations.js @@ -54,53 +54,3 @@ const findLexSmallestString = function(s, a, b) { } }; -// another - - -/** - * @param {string} s - * @param {number} a - * @param {number} b - * @return {string} - */ -const findLexSmallestString = function(s, a, b) { - let res = s - const set = new Set() - const q = [s] - set.add(res) - while(q.length) { - const len = q.length - for(let i = 0; i < len; i++) { - const tmp = q.shift() - const t1 = podd(tmp, a) - const t2 = rotate(tmp, b) - if(!set.has(t1)) { - set.add(t1) - q.push(t1) - } - if(!set.has(t2)) { - set.add(t2) - q.push(t2) - } - if(t1 < res) res = t1 - if(t2 < res) res = t2 - } - } - return res -}; - -function podd(s, num) { - const arr = s.split('') - for(let i = 1, len = s.length; i < len; i += 2) { - const tmp = (+s[i] + num) % 10 - arr[i] = tmp - } - return arr.join('') -} - -function rotate(s, num) { - const len = s.length - num = num % len - const idx = len - num - return s.slice(idx) + s.slice(0, idx) -} diff --git a/1627-graph-connectivity-with-threshold.js b/1627-graph-connectivity-with-threshold.js index cafcb215..b95afc2d 100644 --- a/1627-graph-connectivity-with-threshold.js +++ b/1627-graph-connectivity-with-threshold.js @@ -46,62 +46,3 @@ class UF { } -// another - -/** - * @param {number} n - * @param {number} threshold - * @param {number[][]} queries - * @return {boolean[]} - */ -const areConnected = function (n, threshold, queries) { - const arr = [] - const uf = new UnionFind(n) - setup(n, threshold, uf) - for (let i = 0, len = queries.length; i < len; i++) { - arr.push(uf.check(queries[i][0], queries[i][1])) - } - return arr -} - -function setup(n, t, uf) { - t++ - for (let i = t; i <= n; i++) { - let m = 1 - while (i * m <= n) { - uf.union(i, i * m) - m += 1 - } - } -} -class UnionFind { - constructor(n) { - this.parents = Array(n + 1) - .fill(0) - .map((e, i) => i) - this.ranks = Array(n + 1).fill(0) - } - root(x) { - while (x !== this.parents[x]) { - this.parents[x] = this.parents[this.parents[x]] - x = this.parents[x] - } - return x - } - find(x) { - return this.root(x) - } - check(x, y) { - return this.root(x) === this.root(y) - } - union(x, y) { - const [rx, ry] = [this.find(x), this.find(y)] - if (this.ranks[rx] >= this.ranks[ry]) { - this.parents[ry] = rx - this.ranks[rx] += this.ranks[ry] - } else if (this.ranks[ry] > this.ranks[rx]) { - this.parents[rx] = ry - this.ranks[ry] += this.ranks[rx] - } - } -} diff --git a/163-missing-ranges.js b/163-missing-ranges.js index 40aabd0a..c6ca08ae 100644 --- a/163-missing-ranges.js +++ b/163-missing-ranges.js @@ -17,30 +17,3 @@ const findMissingRanges = function(nums, lower, upper) { return list } -// another - -/** - * @param {number[]} nums - * @param {number} lower - * @param {number} upper - * @return {string[]} - */ -const findMissingRanges = function(nums, lower, upper) { - const res = []; - let next = lower; - for (let i = 0; i < nums.length; i++) { - if (nums[i] < next) continue; - if (nums[i] === next) { - next++; - continue; - } - range(next, nums[i] - 1, res); - next = nums[i] + 1; - } - if (next <= upper) range(next, upper, res); - return res; - function range(l, r, a) { - a.push(l < r ? `${l}->${r}` : `${l}`) - } -}; - diff --git a/1631-path-with-minimum-effort.js b/1631-path-with-minimum-effort.js index aa094e96..4edd2203 100644 --- a/1631-path-with-minimum-effort.js +++ b/1631-path-with-minimum-effort.js @@ -39,254 +39,3 @@ const minimumEffortPath = function(heights) { }; -// another - - - -/** - * @param {number[][]} heights - * @return {number} - */ -const minimumEffortPath = function(heights) { - const m = heights.length, n = heights[0].length - const pq = new PriorityQueue() - const dist = Array.from({ length: m }, () => Array(n).fill(Infinity)) - pq.push([0, 0, 0]) - dist[0][0] = 0 - const dirs = [[-1, 0], [1, 0], [0, 1], [0, -1]] - while(!pq.isEmpty()) { - const [v, i, j] = pq.pop() - if(i === m - 1 && j === n - 1) return v - for(const [dx, dy] of dirs) { - const nx = i + dx, ny = j + dy - if(nx < 0 || nx >= m || ny < 0 || ny >= n) continue - const diff = Math.max(v, Math.abs(heights[nx][ny] - heights[i][j])) - if(dist[nx][ny] > diff) { - dist[nx][ny] = diff - pq.push([diff, nx, ny]) - } - } - } - return -1 -}; - -class PriorityQueue { - constructor(comparator = (a, b) => a[0] < b[0]) { - this.heap = [] - this.top = 0 - this.comparator = comparator - } - size() { - return this.heap.length - } - isEmpty() { - return this.size() === 0 - } - peek() { - return this.heap[this.top] - } - push(...values) { - values.forEach((value) => { - this.heap.push(value) - this.siftUp() - }) - return this.size() - } - pop() { - const poppedValue = this.peek() - const bottom = this.size() - 1 - if (bottom > this.top) { - this.swap(this.top, bottom) - } - this.heap.pop() - this.siftDown() - return poppedValue - } - replace(value) { - const replacedValue = this.peek() - this.heap[this.top] = value - this.siftDown() - return replacedValue - } - - parent = (i) => ((i + 1) >>> 1) - 1 - left = (i) => (i << 1) + 1 - right = (i) => (i + 1) << 1 - greater = (i, j) => this.comparator(this.heap[i], this.heap[j]) - swap = (i, j) => ([this.heap[i], this.heap[j]] = [this.heap[j], this.heap[i]]) - siftUp = () => { - let node = this.size() - 1 - while (node > this.top && this.greater(node, this.parent(node))) { - this.swap(node, this.parent(node)) - node = this.parent(node) - } - } - siftDown = () => { - let node = this.top - while ( - (this.left(node) < this.size() && this.greater(this.left(node), node)) || - (this.right(node) < this.size() && this.greater(this.right(node), node)) - ) { - let maxChild = - this.right(node) < this.size() && - this.greater(this.right(node), this.left(node)) - ? this.right(node) - : this.left(node) - this.swap(node, maxChild) - node = maxChild - } - } -} - -// another - -/** - * @param {number[][]} heights - * @return {number} - */ -const minimumEffortPath = function (heights) { - const d = [0, 1, 0, -1, 0] - let lo = 0, - hi = 10 ** 6 + 1 - while (lo < hi) { - let effort = lo + ((hi - lo) >> 1) - if (isPath(heights, effort)) { - hi = effort - } else { - lo = effort + 1 - } - } - return lo - function isPath(h, effort) { - const m = h.length, - n = h[0].length - const q = [] - q.push([0, 0]) - const seen = new Set() - seen.add(0) - while (q.length) { - const cur = q.shift() - const x = cur[0], - y = cur[1] - if (x === m - 1 && y === n - 1) { - return true - } - for (let k = 0; k < 4; k++) { - const r = x + d[k], - c = y + d[k + 1] - if(seen.has(r * n + c)) continue - if ( - 0 <= r && - r < m && - 0 <= c && - c < n && - effort >= Math.abs(h[r][c] - h[x][y]) - ) { - seen.add(r * n + c) - q.push([r, c]) - } - } - } - return false - } -} - -// another - -/** - * @param {number[][]} heights - * @return {number} - */ -const minimumEffortPath = function(heights) { - const rows = heights.length - const cols = heights[0].length - const dirs = [[-1, 0], [1, 0], [0, 1], [0, -1]] - const dist = Array.from({ length: rows }, () => Array(cols).fill(Infinity)) - const pq = new PriorityQueue() - pq.push([0, 0, 0]) - dist[0][0] = 0 - while(pq.size) { - const cur = pq.pop() - if(cur[1] === rows - 1 && cur[2] === cols - 1) return cur[0] - for(let dir of dirs) { - const nr = cur[1] + dir[0] - const nc = cur[2] + dir[1] - if(nr < 0 || nr >= rows || nc < 0 || nc >= cols) continue - const diff = Math.max(cur[0], Math.abs(heights[nr][nc] - heights[cur[1]][cur[2]])) - if(dist[nr][nc] > diff) { - dist[nr][nc] = diff - pq.push([diff, nr, nc]) - } - } - } - return 0 -}; - -class PriorityQueue { - constructor(comparator = (a, b) => a[0] < b[0]) { - this.heap = [] - this.top = 0 - this.comparator = comparator - } - size() { - return this.heap.length - } - isEmpty() { - return this.size() === 0 - } - peek() { - return this.heap[this.top] - } - push(...values) { - values.forEach((value) => { - this.heap.push(value) - this.siftUp() - }) - return this.size() - } - pop() { - const poppedValue = this.peek() - const bottom = this.size() - 1 - if (bottom > this.top) { - this.swap(this.top, bottom) - } - this.heap.pop() - this.siftDown() - return poppedValue - } - replace(value) { - const replacedValue = this.peek() - this.heap[this.top] = value - this.siftDown() - return replacedValue - } - - parent = (i) => ((i + 1) >>> 1) - 1 - left = (i) => (i << 1) + 1 - right = (i) => (i + 1) << 1 - greater = (i, j) => this.comparator(this.heap[i], this.heap[j]) - swap = (i, j) => ([this.heap[i], this.heap[j]] = [this.heap[j], this.heap[i]]) - siftUp = () => { - let node = this.size() - 1 - while (node > this.top && this.greater(node, this.parent(node))) { - this.swap(node, this.parent(node)) - node = this.parent(node) - } - } - siftDown = () => { - let node = this.top - while ( - (this.left(node) < this.size() && this.greater(this.left(node), node)) || - (this.right(node) < this.size() && this.greater(this.right(node), node)) - ) { - let maxChild = - this.right(node) < this.size() && - this.greater(this.right(node), this.left(node)) - ? this.right(node) - : this.left(node) - this.swap(node, maxChild) - node = maxChild - } - } -} - diff --git a/1632-rank-transform-of-a-matrix.js b/1632-rank-transform-of-a-matrix.js index 69233377..e1dd17d5 100644 --- a/1632-rank-transform-of-a-matrix.js +++ b/1632-rank-transform-of-a-matrix.js @@ -53,160 +53,3 @@ const matrixRankTransform = function (matrix) { return answer } -// another - -/** - * @param {number[][]} matrix - * @return {number[][]} - */ -const matrixRankTransform = function (matrix) { - const m = matrix.length - const n = matrix[0].length - const rowIndex = Array.from({ length: m }, () => Array(n).fill(0)) - const colIndex = Array.from({ length: m }, () => Array(n).fill(0)) - for (let i = 0; i < m; i++) { - let row = [] - for (let j = 0; j < n; j++) { - row.push([matrix[i][j], j]) - } - - row.sort((a, b) => a[0] - b[0]) - for (let j = 0; j < n; j++) { - rowIndex[i][j] = row[j][1] - } - } - for (let i = 0; i < n; i++) { - const col = [] - for (let j = 0; j < m; j++) { - col.push([matrix[j][i], j]) - } - col.sort((a, b) => a[0] - b[0]) - for (let j = 0; j < m; j++) { - colIndex[j][i] = col[j][1] - } - } - const result = Array.from({ length: m }, () => Array(n).fill(0)) - for (let i = 0; i < m; i++) { - for (let j = 0; j < n; j++) { - result[i][j] = 1 - } - } - let changed = true - while (changed) { - changed = shakeRow(matrix, rowIndex, result) - changed = shakeCol(matrix, colIndex, result) || changed - } - return result -} - -function shakeCol(matrix, colIndex, result) { - let changed = false - for (let i = 0; i < matrix[0].length; i++) { - for (let j = 1; j < matrix.length; j++) { - if (matrix[colIndex[j][i]][i] == matrix[colIndex[j - 1][i]][i]) { - if (result[colIndex[j][i]][i] != result[colIndex[j - 1][i]][i]) - changed = true - result[colIndex[j][i]][i] = Math.max( - result[colIndex[j][i]][i], - result[colIndex[j - 1][i]][i] - ) - result[colIndex[j - 1][i]][i] = Math.max( - result[colIndex[j][i]][i], - result[colIndex[j - 1][i]][i] - ) - } else { - if (result[colIndex[j][i]][i] < result[colIndex[j - 1][i]][i] + 1) { - changed = true - result[colIndex[j][i]][i] = result[colIndex[j - 1][i]][i] + 1 - } - } - } - } - return changed -} - -function shakeRow(matrix, rowIndex, result) { - let changed = false - for (let i = 0; i < matrix.length; i++) { - let rowInd = rowIndex[i] - let resu = result[i] - for (let j = 1; j < matrix[0].length; j++) { - if (matrix[i][rowInd[j]] == matrix[i][rowInd[j - 1]]) { - if (resu[rowInd[j]] != resu[rowInd[j - 1]]) changed = true - resu[rowInd[j]] = Math.max(resu[rowInd[j - 1]], resu[rowInd[j]]) - resu[rowInd[j - 1]] = Math.max(resu[rowInd[j - 1]], resu[rowInd[j]]) - } else { - if (resu[rowInd[j]] < resu[rowInd[j - 1]] + 1) { - changed = true - resu[rowInd[j]] = resu[rowInd[j - 1]] + 1 - } - } - } - } - return changed -} - -// another - -/** - * @param {number[][]} matrix - * @return {number[][]} - */ -const matrixRankTransform = function (matrix) { - const r = matrix.length, - c = matrix[0].length; - const t = r * c; - const arr = Array(t); - const root = Array(t + 1); - const rk = Array(t + 1).fill(0); - const find = (a) => { - let ra = root[a]; - if (ra == a) return a; - return (root[a] = find(ra)); - }; - const union = (a, b) => { - let ra = find(a); - let rb = find(b); - if (ra !== rb) { - if (rk[ra] > rk[rb]) root[rb] = ra; - else root[ra] = rb; - } - }; - let k = 0; - const ans = Array(r) - .fill(0) - .map(() => Array(c)); - for (let i = 0; i < r; ++i) { - for (let j = 0; j < c; ++j) { - arr[k] = [matrix[i][j], i, j]; - root[k] = k; - ++k; - } - } - root[k] = k; - arr.sort((a, b) => a[0] - b[0]); - const X = Array(r) - .fill(0) - .map(() => [-Infinity, t]); - const Y = Array(c) - .fill(0) - .map(() => [-Infinity, t]); - for (let i = 0; i < t; ++i) { - const [v, x, y] = arr[i]; - const id = x * c + y; - const [xv, rx] = X[x], - [yv, ry] = Y[y]; - if (v > xv) rk[id] = rk[find(rx)] + 1; - else root[id] = rx; - if (v > yv) rk[find(id)] = Math.max(rk[find(id)], rk[find(ry)] + 1); - else union(id, ry); - X[x] = [v, id]; - Y[y] = [v, id]; - } - for (let i = 0; i < r; ++i) { - for (let j = 0; j < c; ++j) { - ans[i][j] = rk[find(i * c + j)]; - } - } - return ans; -}; diff --git a/1638-count-substrings-that-differ-by-one-character.js b/1638-count-substrings-that-differ-by-one-character.js index e1a9a64f..32793862 100644 --- a/1638-count-substrings-that-differ-by-one-character.js +++ b/1638-count-substrings-that-differ-by-one-character.js @@ -26,29 +26,3 @@ const countSubstrings = function (s, t) { return result } -// another - -/** - * @param {string} s - * @param {string} t - * @return {number} - */ -const countSubstrings = function(s, t) { - let res = 0 ; - for (let i = 0; i < s.length; ++i) res += helper(s, t, i, 0); - for (let j = 1; j < t.length; ++j) res += helper(s, t, 0, j); - return res; -}; - -function helper(s, t, i, j) { - let res = 0, pre = 0, cur = 0; - for (let n = s.length, m = t.length; i < n && j < m; ++i, ++j) { - cur++; - if (s.charAt(i) !== t.charAt(j)) { - pre = cur; - cur = 0; - } - res += pre; - } - return res; -} diff --git a/1639-number-of-ways-to-form-a-target-string-given-a-dictionary.js b/1639-number-of-ways-to-form-a-target-string-given-a-dictionary.js index fa8bf975..c2fb376b 100644 --- a/1639-number-of-ways-to-form-a-target-string-given-a-dictionary.js +++ b/1639-number-of-ways-to-form-a-target-string-given-a-dictionary.js @@ -24,39 +24,3 @@ const numWays = function (words, target) { return dp[n - 1] } -// another - -/** - * @param {string[]} words - * @param {string} target - * @return {number} - */ -const numWays = function (words, target) { - const m = words[0].length - const n = target.length - const memo = Array.from({ length: m }, () => Array(n)) - const charAtIndexCnt = Array.from({ length: 128 }, () => Array(m).fill(0)) - const mod = 10 ** 9 + 7 - for (let word of words) { - for (let i = 0; i < m; i++) { - charAtIndexCnt[word.charCodeAt(i)][i] += 1 - } - } - - return dp(0, 0) - function dp(k, i) { - // found one - if (i == n) return 1 - // not found - if (k == m) return 0 - if (memo[k][i] != null) return memo[k][i] - const c = target.charCodeAt(i) - // skip k_th char - let ans = dp(k + 1, i) - if (charAtIndexCnt[c][k] > 0) { - ans += dp(k + 1, i + 1) * charAtIndexCnt[c][k] - ans %= mod - } - return (memo[k][i] = ans) - } -} diff --git a/1641-count-sorted-vowel-strings.js b/1641-count-sorted-vowel-strings.js index b74ecc8d..e6597963 100644 --- a/1641-count-sorted-vowel-strings.js +++ b/1641-count-sorted-vowel-strings.js @@ -6,46 +6,3 @@ const countVowelStrings = function (n) { return (n + 4) * (n + 3) * (n + 2) * (n + 1) / 24 }; -// another - -/** - * @param {number} n - * @return {number} - */ -const countVowelStrings = function (n) { - let mem = [1, 1, 1, 1, 1]; - for (let i = 1; i < n; ++i) { - const next = [0, 0, 0, 0, 0]; - let tmp = 0; - for (let j = 4; j >= 0; --j) { - tmp += mem[j]; - next[j] = tmp; - } - mem = next; - } - let sum = 0; - for (let i of mem) { - sum += i; - } - return sum; -}; - -// another - -/** - * @param {number} n - * @return {number} - */ -const countVowelStrings = function (n) { - const dp = Array.from({ length: n + 1 }, () => Array(5)) - recur(n, 0) - return dp[n][0] - function recur(r, i) { - if(r === 0) return 1 - if(i === 5) return 0 - if(dp[r][i] != null) return dp[r][i] - let res = recur(r, i + 1) - res += recur(r - 1, i) - return dp[r][i] = res - } -}; diff --git a/1643-kth-smallest-instructions.js b/1643-kth-smallest-instructions.js index 23ccbacd..f5e2e684 100644 --- a/1643-kth-smallest-instructions.js +++ b/1643-kth-smallest-instructions.js @@ -50,40 +50,3 @@ function comb(n, r) { } } -// another - -/** - * @param {number[]} destination - * @param {number} k - * @return {string} - */ -const kthSmallestPath = function (destination, k) { - const [r, c] = destination; - const ret = []; - let remDown = r; - for (let i = 0; i < r + c; i++) { - const remSteps = r + c - (i + 1); - const com = comb(remSteps, remDown); - if (com >= k) ret.push("H"); - else { - remDown -= 1; - k -= com; - ret.push("V"); - } - } - return ret.join(""); -}; - -function comb(n, r) { - if (n < r) return 0; - let res = 1; - if (n - r < r) r = n - r; - for (let i = n, j = 1; i >= 1 && j <= r; --i, ++j) { - res = res * i; - } - for (let i = r; i >= 2; --i) { - res = res / i; - } - return res; -} - diff --git a/1644-lowest-common-ancestor-of-a-binary-tree-ii.js b/1644-lowest-common-ancestor-of-a-binary-tree-ii.js index bd9407bb..00f374f5 100644 --- a/1644-lowest-common-ancestor-of-a-binary-tree-ii.js +++ b/1644-lowest-common-ancestor-of-a-binary-tree-ii.js @@ -34,40 +34,3 @@ const lowestCommonAncestor = function (root, p, q) { return cn } -// another - -/** - * Definition for a binary tree node. - * function TreeNode(val) { - * this.val = val; - * this.left = this.right = null; - * } - */ -/** - * @param {TreeNode} root - * @param {TreeNode} p - * @param {TreeNode} q - * @return {TreeNode} - */ -const lowestCommonAncestor = function (root, p, q) { - let hasP = false, hasQ = false - const res = LCA(root, p, q) - return hasP && hasQ ? res : null - - function LCA(root, p, q) { - if(root == null) return root - const left = LCA(root.left, p, q) - const right = LCA(root.right, p, q) - if(root === p) { - hasP = true - return root - } - if(root === q) { - hasQ = true - return root - } - if(left && right) return root - return left || right - } -} - diff --git a/1648-sell-diminishing-valued-colored-balls.js b/1648-sell-diminishing-valued-colored-balls.js index f38bf0b2..da545b0b 100644 --- a/1648-sell-diminishing-valued-colored-balls.js +++ b/1648-sell-diminishing-valued-colored-balls.js @@ -36,139 +36,3 @@ const maxProfit = function(inventory, orders) { } }; -// another - -/** - * @param {number[]} inventory - * @param {number} orders - * @return {number} - */ -function maxProfit(inventory, orders) { - inventory.sort((a, b) => a - b) - inventory = inventory.map(e => BigInt(e)) - let ans = 0n, n = inventory.length - 1, count = 1n - const mod = BigInt(10 ** 9 + 7) - orders = BigInt(orders) - while(orders > 0n) { - if(n > 0 && inventory[n] > inventory[n - 1] && orders >= count * (inventory[n] - inventory[n - 1])) { - ans += count * sum(inventory[n - 1], inventory[n]) - orders -= count * (inventory[n] - inventory[n - 1]) - } else if(n === 0 || inventory[n] > inventory[n - 1]) { - const num = orders / count - ans += count * sum(inventory[n] - num, inventory[n]) - const remain = orders % count - ans += remain * (inventory[n] - num) - orders = 0n - } - ans %= mod - n-- - count++ - } - return ans -} - -function sum(lo, hi) { - return (hi - lo) * (lo + hi + 1n) / 2n -} - -// another - -/** - * @param {number[]} inventory - * @param {number} orders - * @return {number} - */ -const maxProfit = function (inventory, orders) { - let Max = 1e9 + 7, - Min = 0 - let mod = BigInt(1e9 + 7) - while (Max > Min + 1) { - let tot = 0 - let mid = ((Max + Min) >> 1) - for (let it of inventory) { - if (it > mid) tot += it - mid - } - if (tot > orders) Min = mid - else Max = mid - } - let sum = BigInt(0) - Max = BigInt(Max) - orders = BigInt(orders) - for (let it of inventory) { - it = BigInt(it) - if (it > Max) { - sum += ((it + Max + BigInt(1)) * (it - Max)) / BigInt(2) - orders -= it - Max - } - } - sum += orders * Max - - return sum % mod -} - -// another - -/** - * @param {number[]} inventory - * @param {number} orders - * @return {number} - */ -var maxProfit = function(inventory, orders) { - inventory.sort((a, b) => b - a) - const mod = BigInt(1e9 + 7), n = BigInt(inventory.length) - inventory = inventory.map(e => BigInt(e)) - orders = BigInt(orders) - let cur = BigInt(inventory[0]), res = 0n, i = 0n - const min = (a, b) => a > b ? b : a - while(orders) { - while(i < n && inventory[i] === cur) i++ - let next = i === n ? 0n : inventory[i] - let h = cur - next, r = 0n, cnt = min(orders, i * h) - if (orders < i * h) { - h = orders / i - r = orders % i - } - let val = cur - h - res = (res + (cur + val + 1n) * h / 2n * i + val * r) % mod - orders -= cnt - cur = next - } - - return res -}; - -// another - -/** - * @param {number[]} inventory - * @param {number} orders - * @return {number} - */ -const maxProfit = function (inventory, orders) { - inventory.sort((a, b) => b - a) - const mod = BigInt(1e9 + 7), - n = BigInt(inventory.length) - inventory = inventory.map((e) => BigInt(e)) - orders = BigInt(orders) - let cur = BigInt(inventory[0]), - res = 0n, - i = 0n - const min = (a, b) => (a > b ? b : a) - while (orders) { - while (i < n && inventory[i] === cur) i++ - let next = i === n ? 0n : inventory[i] - let h = cur - next, - r = 0n, - cnt = min(orders, i * h) - if (orders < i * h) { - h = orders / i - r = orders % i - } - let val = cur - h - res = (res + (((cur + val + 1n) * h) / 2n) * i + val * r) % mod - orders -= cnt - cur = next - } - - return res -} diff --git a/1649-create-sorted-array-through-instructions.js b/1649-create-sorted-array-through-instructions.js index d186bcd8..9429d79d 100644 --- a/1649-create-sorted-array-through-instructions.js +++ b/1649-create-sorted-array-through-instructions.js @@ -35,49 +35,3 @@ const createSortedArray = function(instructions) { return res }; -// another - -/** - * @param {number[]} instructions - * @return {number} - */ -const createSortedArray = function (instructions) { - const ins = instructions, n = ins.length - let res = 0 - const mod = 1e9 + 7, { min } = Math - const bit = new BIT(1e5) - for(let i = 0; i < n; i++) { - const cur = ins[i] - res = (res + min(bit.query(cur - 1), i - bit.query(cur))) % mod - bit.update(cur, 1) - } - - return res -} - -function lowBit(x) { - return x & -x -} -class BIT { - constructor(n) { - this.arr = Array(n + 1).fill(0) - } - - update(i, delta) { - if(i < 1) return - while (i < this.arr.length) { - this.arr[i] += delta - i += lowBit(i) - } - } - - query(i) { - let res = 0 - if(i < 1) return res - while (i > 0) { - res += this.arr[i] - i -= lowBit(i) - } - return res - } -} diff --git a/1650-lowest-common-ancestor-of-a-binary-tree-iii.js b/1650-lowest-common-ancestor-of-a-binary-tree-iii.js index 2d0cf7b4..4b746ca2 100644 --- a/1650-lowest-common-ancestor-of-a-binary-tree-iii.js +++ b/1650-lowest-common-ancestor-of-a-binary-tree-iii.js @@ -29,27 +29,3 @@ const lowestCommonAncestor = function(p, q) { } }; -// another - -/** - * // Definition for a Node. - * function Node(val) { - * this.val = val; - * this.left = null; - * this.right = null; - * this.parent = null; - * }; - */ - -/** - * @param {Node} node - * @return {Node} - */ -const lowestCommonAncestor = function(p, q) { - let a = p, b = q; - while (a !== b) { - a = a == null? q : a.parent; - b = b == null? p : b.parent; - } - return a; -}; diff --git a/1653-minimum-deletions-to-make-string-balanced.js b/1653-minimum-deletions-to-make-string-balanced.js index 4cf12446..6a97c168 100644 --- a/1653-minimum-deletions-to-make-string-balanced.js +++ b/1653-minimum-deletions-to-make-string-balanced.js @@ -15,67 +15,3 @@ const minimumDeletions = function(s) { return res }; -// another - - -/** - * @param {string} s - * @return {number} - */ -const minimumDeletions = function(s) { - let res = 0 - let cnt = 0 - for(let c of s) { - if(c === 'a' && cnt > 0) { - res++ - cnt-- - } else if(c === 'b') { - cnt++ - } - } - - return res -}; - -// another - -/** - * @param {string} s - * @return {number} - */ -const minimumDeletions = function(s) { - const len = s.length - const dp = Array(len + 1).fill(0) - let bcount = 0 - for(let i = 1; i <= len; i++) { - if(s[i - 1] === 'a') { - dp[i] = Math.min(dp[i - 1] + 1, bcount) - } else { - dp[i] = dp[i - 1] - bcount++ - } - } - - return dp[len] -}; - -// another - -/** - * @param {string} s - * @return {number} - */ -const minimumDeletions = function(s) { - const len = s.length - const stack = [] - let res = 0 - for(let i = 0; i < len; i++) { - if(stack.length && stack[stack.length - 1] > s[i]) { - res++ - stack.pop() - } else { - stack.push(s[i]) - } - } - return res -}; diff --git a/1654-minimum-jumps-to-reach-home.js b/1654-minimum-jumps-to-reach-home.js index 11ce519d..b2978166 100644 --- a/1654-minimum-jumps-to-reach-home.js +++ b/1654-minimum-jumps-to-reach-home.js @@ -43,45 +43,3 @@ const minimumJumps = function (forbidden, a, b, x) { return -1 } -// another - -/** - * @param {number[]} forbidden - * @param {number} a - * @param {number} b - * @param {number} x - * @return {number} - */ -const minimumJumps = function (forbidden, a, b, x) { - const bad = new Set() - const set = new Set() - for (let i of forbidden) { - bad.add(i) - } - const q = [] - q.push([0, 0, 0]) - set.add('0,0') - while (q.length) { - const pair = q.shift() - let pos = pair[0], - level = pair[1], - state = pair[2] - if (pos == x) return level - if (state >= 0) { - if (pos <= 4000 && !set.has(pos + a + ',0') && !bad.has(pos + a)) { - set.add(pos + a + ',0') - q.push([pos + a, level + 1, 0]) - } - if (!set.has(pos - b + ',-1') && !bad.has(pos - b) && pos - b >= 0) { - set.add(pos - b + ',-1') - q.push([pos - b, level + 1, -1]) - } - } else if (state < 0) { - if (pos <= 4000 && !set.has(pos + a + ',0') && !bad.has(pos + a)) { - set.add(pos + a + ',0') - q.push([pos + a, level + 1, 0]) - } - } - } - return -1 -} diff --git a/1655-distribute-repeating-integers.js b/1655-distribute-repeating-integers.js index 201a7fad..9365cc8a 100644 --- a/1655-distribute-repeating-integers.js +++ b/1655-distribute-repeating-integers.js @@ -47,174 +47,3 @@ const canDistribute = function(nums, quantity) { } }; -// another - -/** - * @param {number[]} nums - * @param {number[]} quantity - * @return {boolean} - */ -const canDistribute = function (nums, quantity) { - const mp = {} - for (let x of nums) { - mp[x] = (mp[x] || 0) + 1 - } - const values = Object.values(mp) - quantity.sort((a, b) => b - a) - let res = false - dfs(0) - return res - - function dfs(idx) { - if(idx === quantity.length || res) { - res = true - return - } - for(let i = 0, len = values.length; i < len; i++) { - if(values[i] >= quantity[idx]) { - values[i] -= quantity[idx] - dfs(idx + 1) - values[i] += quantity[idx] - } - } - } -} - -// another - -/** - * @param {number[]} nums - * @param {number[]} quantity - * @return {boolean} - */ -const canDistribute = function (nums, quantity) { - const mp = {} - for (let x of nums) { - mp[x] = (mp[x] || 0) + 1 - } - const a = [] - for (let p in mp) a.push(mp[p]) - const b = quantity - const m = quantity.length - const n = a.length - const dp = Array.from({ length: n }, () => Array(1 << m).fill(-1)) - return solve(0, 0) - - function solve(idx, mask) { - if (mask === (1 << m) - 1) return 1 - if (idx === n) return 0 - if (dp[idx][mask] !== -1) return dp[idx][mask] - let ans = solve(idx + 1, mask) - for (let i = 0, up = 1 << m; i < up; i++) { - if (mask !== (mask & i)) continue - let nm = mask - let sum = 0 - for (let j = 0; j < m; j++) { - if (mask & (1 << j)) continue - if (i & (1 << j)) { - sum += b[j] - nm |= 1 << j - } - } - if (sum <= a[idx]) ans |= solve(idx + 1, nm) - } - return (dp[idx][mask] = ans) - } -} - -// another - -/** - * @param {number[]} nums - * @param {number[]} quantity - * @return {boolean} - */ -const canDistribute = function(nums, quantity) { - const freq = {} - for(let e of nums) freq[e] = (freq[e] || 0) + 1 - const fArr = Object.values(freq) - - const m = quantity.length, n = fArr.length - const dp = Array.from({ length: n }, () => Array(1 << m).fill(-1)) - - return solve(0, 0) - - function solve(idx, mask) { - if(mask === (1 << m) - 1) return 1 - if(idx === n) return 0 - if(dp[idx][mask] !== -1) return dp[idx][mask] - - let res = solve(idx + 1, mask) - for(let i = 0; i < (1 << m); i++) { - if(mask !== (mask & i)) continue - let tmp = mask - let sum = 0 - for(let j = 0; j < m; j++) { - if(mask & (1 << j)) continue - if(i & (1 << j)) { - sum += quantity[j] - tmp |= (1 << j) - } - } - if(sum <= fArr[idx]) res |= solve(idx + 1, tmp) - } - - return dp[idx][mask] = res - } -}; - -// another - -/** - * @param {number[]} nums - * @param {number[]} quantity - * @return {boolean} - */ -const canDistribute = function (nums, quantity) { - const hash = {} - for(const e of nums) { - if(hash[e] == null) hash[e] = 0 - hash[e]++ - } - const cnts = Object.values(hash), m = quantity.length, n = cnts.length - const dp = Array.from({ length: n }, () => Array(1 << m).fill(null)) - - return helper(0, 0) - - function helper(idx, mask) { - // mask are already selected candidates - if(mask == (1 << m) - 1) { - return true; - } - if(idx == n) { - return false; - } - if(dp[idx][mask] != null) { - return dp[idx][mask]; - } - let ans = helper(idx + 1, mask); - - for(let i = 1; i < (1 << m); ++i) { - // i are potential candidates in addition to already selected ones (from mask) - // if i == mask, we can skip as the candidate is selected already - // if mask != (mask & i) means that this candidate does not include selected ones e.g - // mask = 3 (i.e 2 elements 1,2 in binary) and i = 4 (the third element in binary as 4 does not include 1 & 2), there we skip - if(mask == i || mask != (mask & i)) continue; - let sum = 0; - for(let j = 0; j < m; ++j) { - // mask << ~j is just a fancy way to do: if(mask & (1 << j)) that i've learned from @Uwi and this way you don't have to use "(", ")" - // what it does is simply pushing the jth bit to the 2^31 bit which is negative - // thus if the jth bit is 1 then the value is less than zero and if its 0 then its greater or equal to zero - if(mask << ~j >= 0 && i << ~j < 0) { // check that mask does not contain the new candidate and that the candidate is part of the potential candidate i - sum += quantity[j]; - } - } - if(sum <= cnts[idx]) { - ans |= helper(idx + 1, i); - } - if(ans) break; // if ans is true, then a solution exists and no further computation is required - } - dp[idx][mask] = ans; - return ans; - } -} diff --git a/1658-minimum-operations-to-reduce-x-to-zero.js b/1658-minimum-operations-to-reduce-x-to-zero.js index 15935ef5..f106e171 100644 --- a/1658-minimum-operations-to-reduce-x-to-zero.js +++ b/1658-minimum-operations-to-reduce-x-to-zero.js @@ -21,42 +21,3 @@ const minOperations = function(nums, x) { return res === -1 ? -1 : n - res }; -// another - -/** - * @param {number[]} nums - * @param {number} x - * @return {number} - */ -const minOperations = function (nums, x) { - let l = 0, - r = nums.length - 1; - while (x >= 0 && r >= l) { - x -= nums[r]; - r -= 1; - } - if (r < 0 && x > 0) { - return -1; - } else if (r < 0 && x == 0) { - return nums.length; - } - - let ans = Number.MAX_VALUE; - while (r < nums.length) { - while (x <= 0 && r + 1 < nums.length) { - if (x == 0) ans = Math.min(ans, nums.length - (r - l + 1)); - x += nums[r + 1]; - r += 1; - } - if (r + 1 >= nums.length) { - if (x == 0) ans = Math.min(ans, nums.length - (r - l + 1)); - break; - } - while (x >= 0) { - if (x == 0) ans = Math.min(ans, nums.length - (r - l + 1)); - x -= nums[l]; - l += 1; - } - } - return ans != Number.MAX_VALUE ? ans : -1; -}; diff --git a/1660-correct-a-binary-tree.js b/1660-correct-a-binary-tree.js index 9c940f94..ebc2fd92 100644 --- a/1660-correct-a-binary-tree.js +++ b/1660-correct-a-binary-tree.js @@ -26,51 +26,3 @@ const correctBinaryTree = (root, seen = new Set(), found = false) => { return go(root) } -// another - -/** - * Definition for a binary tree node. - * function TreeNode(val, left, right) { - * this.val = (val===undefined ? 0 : val) - * this.left = (left===undefined ? null : left) - * this.right = (right===undefined ? null : right) - * } - */ -/** - * @param {TreeNode} root - * @param {number} from - * @param {number} to - * @return {TreeNode} - */ -const correctBinaryTree = function(root) { - let q = [root] - let target - while(q.length) { - const size = q.length - const next = new Set() - const row = new Set() - for(let i = 0; i < size; i++) { - const cur = q.shift() - row.add(cur) - if(cur.left) next.add(cur.left) - if(cur.right) next.add(cur.right) - } - for(let e of next) { - if(next.has(e.right)) { - target = e - for(let el of row) { - if(el.left && el.left === target) { - el.left = null - return root - } - if(el.right && el.right === target) { - el.right = null - return root - } - } - } - } - q = Array.from(next) - } - return root -}; diff --git a/1664-ways-to-make-a-fair-array.js b/1664-ways-to-make-a-fair-array.js index 11e38596..df093b3d 100644 --- a/1664-ways-to-make-a-fair-array.js +++ b/1664-ways-to-make-a-fair-array.js @@ -14,32 +14,3 @@ const waysToMakeFair = function(nums) { return res }; -// another - -/** - * @param {number[]} nums - * @return {number} - */ -const waysToMakeFair = function (nums) { - const n = nums.length - const preOddSum = new Array(n + 1).fill(0) - const preEvenSum = new Array(n + 1).fill(0) - for (let i = 0; i < n; i++) { - if (i % 2 === 0) { - preEvenSum[i + 1] = nums[i] + preEvenSum[i] - preOddSum[i + 1] = preOddSum[i] - } else { - preOddSum[i + 1] = nums[i] + preOddSum[i] - preEvenSum[i + 1] = preEvenSum[i] - } - } - let ret = 0 - for (let i = 0; i < n; i++) { - if ( - preEvenSum[i] + preOddSum[n] - preOddSum[i + 1] === - preOddSum[i] + preEvenSum[n] - preEvenSum[i + 1] - ) - ret++ - } - return ret -} diff --git a/1665-minimum-initial-energy-to-finish-tasks.js b/1665-minimum-initial-energy-to-finish-tasks.js index 31746fc0..1e8fcc85 100644 --- a/1665-minimum-initial-energy-to-finish-tasks.js +++ b/1665-minimum-initial-energy-to-finish-tasks.js @@ -11,34 +11,3 @@ const minimumEffort = function (tasks) { return res } -// another - -/** - * @param {number[][]} tasks - * @return {number} - */ -const minimumEffort = function (a) { - let low = 0, - high = 1e9 - for (let x of a) low = Math.max(low, x[1]) - a.sort((lhs, rhs) => (lhs[1] - lhs[0] > rhs[1] - rhs[0] ? -1 : 1)) - let n = a.length - while (low != high) { - let mid = low + ((high - low) >> 1) - let found = false - let rem = mid - for (let i = 0; i < n; ++i) { - if (rem < a[i][1]) { - found = true - break - } - rem -= a[i][0] - } - if (found) { - low = mid + 1 - } else { - high = mid - } - } - return high -} diff --git a/1668-maximum-repeating-substring.js b/1668-maximum-repeating-substring.js index 87467df4..25a11dd8 100644 --- a/1668-maximum-repeating-substring.js +++ b/1668-maximum-repeating-substring.js @@ -9,32 +9,3 @@ const maxRepeating = function(sequence, word) { return count - 1; }; -// another - -/** - * @param {string} sequence - * @param {string} word - * @return {number} - */ -const maxRepeating = function(sequence, word) { - const s = sequence.length, w = word.length - const max_repeat = (s / w) >> 0 - const failure = Array(w * max_repeat + 1).fill(0) - const repeat_words = word.repeat(max_repeat) + '$' - let result = 0, j = 0 - - for(let i = 1, hi = repeat_words.length; i < hi; i++) { - while(j > 0 && repeat_words[j] !== repeat_words[i]) j = failure[j - 1] - j += (repeat_words[j] === repeat_words[i] ? 1 : 0) - failure[i] = j - } - - j = 0 - for(let i = 0, len = sequence.length; i < len; i++) { - while(j > 0 && repeat_words[j] !== sequence[i]) j = failure[j - 1] - j += (repeat_words[j] === sequence[i] ? 1 : 0) - result = Math.max(result, (j / w) >> 0) - } - return result -}; - diff --git a/1671-minimum-number-of-removals-to-make-mountain-array.js b/1671-minimum-number-of-removals-to-make-mountain-array.js index 933532f3..9e6254e9 100644 --- a/1671-minimum-number-of-removals-to-make-mountain-array.js +++ b/1671-minimum-number-of-removals-to-make-mountain-array.js @@ -34,31 +34,3 @@ function LIS(arr) { return res } -// another - -/** - * @param {number[]} nums - * @return {number} - */ -const minimumMountainRemovals = function (nums) { - if (nums.length <= 3) return 0 - const n = nums.length - const inc = Array(n).fill(0) - const dec = Array(n).fill(0) - const { max, min } = Math - for (let i = 1; i < n; i++) { - for (let j = 0; j < i; j++) { - if (nums[i] > nums[j]) inc[i] = max(inc[i], inc[j] + 1) - } - } - for (let i = n - 2; i >= 0; i--) { - for (let j = n - 1; j > i; j--) { - if (nums[i] > nums[j]) dec[i] = max(dec[i], dec[j] + 1) - } - } - let res = 0 - for (let i = 0; i < n; i++) { - if (inc[i] > 0 && dec[i] > 0) res = max(res, inc[i] + dec[i]) - } - return n - res - 1 -} diff --git a/1673-find-the-most-competitive-subsequence.js b/1673-find-the-most-competitive-subsequence.js index bf913f46..c443a403 100644 --- a/1673-find-the-most-competitive-subsequence.js +++ b/1673-find-the-most-competitive-subsequence.js @@ -20,50 +20,3 @@ const mostCompetitive = function (nums, k) { return res } -// another - -/** - * @param {number[]} nums - * @param {number} k - * @return {number[]} - */ -const mostCompetitive = function (nums, k) { - const stack = [], - n = nums.length - let i = 0 - while (i < n) { - while ( - stack.length && - stack[stack.length - 1] > nums[i] && - n - i + stack.length > k - ) - stack.pop() - if (stack.length < k) stack.push(nums[i]) - i++ - } - return stack -} - -// another - -/** - * @param {number[]} nums - * @param {number} k - * @return {number[]} - */ -const mostCompetitive = function (nums, k) { - const n = nums.length, stack = [] - for(let i = 0; i < n; i++) { - const ch = nums[i] - while( - stack.length && - ch < stack[stack.length - 1] && - stack.length + (n - 1 - i) >= k - ) { - stack.pop() - } - if(stack.length < k) stack.push(ch) - } - return stack -} - diff --git a/1674-minimum-moves-to-make-array-complementary.js b/1674-minimum-moves-to-make-array-complementary.js index c257d2e2..ed1e460e 100644 --- a/1674-minimum-moves-to-make-array-complementary.js +++ b/1674-minimum-moves-to-make-array-complementary.js @@ -25,95 +25,3 @@ const minMoves = function (nums, limit) { return ans } -// another - -/** - * @param {number[]} nums - * @param {number} limit - * @return {number} - */ -const minMoves = function (nums, limit) { - const n = nums.length, { max, min } = Math - const delta = Array(2 * limit + 2).fill(0) - for(let i = 0; i < n / 2; i++) { - const a = nums[i], b = nums[n - 1 - i] - // [2, min(a, b) + 1) - delta[2] += 2 - // [min(a, b) + 1, a + b) - delta[min(a, b) + 1] -= 1 - delta[a + b]-- - // [a + b + 1, max(a, b) + limit] - delta[a + b + 1] += 1 - // (max(a, b) + limit, 2 * limit] - delta[max(a, b) + limit + 1] +=1 - } - - let res = n, cur = 0 - for(let i = 2; i <= limit * 2; i++) { - cur += delta[i] - res = min(cur, res) - } - - return res -} - -// another - -/** - * @param {number[]} nums - * @param {number} limit - * @return {number} - */ -const minMoves = function (nums, limit) { - const n = nums.length, { min, max } = Math - const arr = Array(2 * limit + 2).fill(0) - for(let i = 0, r = n / 2; i < r; i++) { - const a = nums[i], b = nums[n - 1 - i] - arr[2] += 2 - arr[min(a, b) + 1]-- - arr[a + b]-- - arr[a + b + 1]++ - arr[max(a, b) + limit + 1]++ - } - let res = Infinity, cur = 0 - for(let i = 2, r = 2 * limit; i <= r; i++) { - cur += arr[i] - res = min(res, cur) - } - - return res -} - -// another - -/** - * @param {number[]} nums - * @param {number} limit - * @return {number} - */ -const minMoves = function (nums, limit) { - const n = nums.length, { min, max } = Math - const arr = Array(2 * limit + 2).fill(0) - for(let i = 0, r = n / 2; i < r; i++) { - const a = nums[i], b = nums[n - 1 - i] - // [2, 2 * limit] - arr[2] += 2 - arr[2 * limit + 1] -= 2 - // [min(a, b) + 1, max(a, b) + limit] - arr[min(a, b) + 1]-- - arr[max(a, b) + limit + 1]++ - // a + b - arr[a + b]-- - arr[a + b + 1]++ - - } - let res = Infinity, cur = 0 - for(let i = 2, r = 2 * limit; i <= r; i++) { - cur += arr[i] - res = min(res, cur) - } - - return res -} - - diff --git a/1676-lowest-common-ancestor-of-a-binary-tree-iv.js b/1676-lowest-common-ancestor-of-a-binary-tree-iv.js index 1057137e..721c185c 100644 --- a/1676-lowest-common-ancestor-of-a-binary-tree-iv.js +++ b/1676-lowest-common-ancestor-of-a-binary-tree-iv.js @@ -21,31 +21,3 @@ const lowestCommonAncestor = function(root, nodes) { return left ? left : right }; -// another - -/** - * Definition for a binary tree node. - * function TreeNode(val) { - * this.val = val; - * this.left = this.right = null; - * } - */ -/** - * @param {TreeNode} root - * @param {TreeNode[]} nodes - * @return {TreeNode} - */ - const lowestCommonAncestor = function(root, nodes) { - const set = new Set(nodes) - return dfs(root) - - function dfs(node) { - if(node == null) return node - const left = dfs(node.left) - const right = dfs(node.right) - - if(set.has(node)) return node - if(left && right) return node - return left || right - } -}; diff --git a/1680-concatenation-of-consecutive-binary-numbers.js b/1680-concatenation-of-consecutive-binary-numbers.js index c2b7c069..38824917 100644 --- a/1680-concatenation-of-consecutive-binary-numbers.js +++ b/1680-concatenation-of-consecutive-binary-numbers.js @@ -15,21 +15,3 @@ function dec2bin(dec){ return (dec >>> 0).toString(2); } -// another - -/** - * @param {number} n - * @return {number} - */ -const concatenatedBinary = function (n) { - const mod = BigInt(1e9 + 7) - let res = 0n - for (let i = 1n, shift = 0n; i <= n; i++) { - let singleBit = (i & (i - 1n)) == 0 - if (singleBit) shift++ - res <<= shift - res += i - res %= mod - } - return res -} diff --git a/1681-minimum-incompatibility.js b/1681-minimum-incompatibility.js index 255ef859..2730a2f0 100644 --- a/1681-minimum-incompatibility.js +++ b/1681-minimum-incompatibility.js @@ -42,141 +42,3 @@ const minimumIncompatibility = function(nums, k) { } }; -// another - -/** - * @param {number[]} nums - * @param {number} k - * @return {number} - */ -var minimumIncompatibility = function (nums, k) { - if (k === nums.length) { - return 0 - } - const counts = Array(nums.length + 1).fill(0) - for (let num of nums) { - counts[num]++ - if (counts[num] > k) { - return -1 - } - } - const size = nums.length / k - let ans = Number.MAX_VALUE - const backtracking = (groupIdx, index, sum, lowIndex, curIndex) => { - if (index === size) { - sum += curIndex - lowIndex - if (sum > ans) { - return - } - if (groupIdx === k - 1) { - ans = sum - return - } else { - groupIdx++ - index = 0 - } - } - if (index === 0) { - for (let i = 0; i < counts.length; i++) { - if (counts[i]) { - counts[i]-- - backtracking(groupIdx, index + 1, sum, i, i) - counts[i]++ - } - } - } else { - for (let i = curIndex + 1; i < counts.length; i++) { - if (counts[i]) { - counts[i]-- - backtracking(groupIdx, index + 1, sum, lowIndex, i) - counts[i]++ - } - } - } - } - backtracking(0, 0, 0, 0, 0) - return ans -} - -// another - -/** - * @param {number[]} nums - * @param {number} k - * @return {number} - */ -const minimumIncompatibility = function (nums, k) { - if (nums.length === k) return 0 - const maxInBucket = nums.length / k - const freqCount = {} - for (const n of nums) { - if (freqCount[n]) { - if (freqCount[n] === k) { - return -1 - } else { - freqCount[n]++ - } - } else { - freqCount[n] = 1 - } - } - const cache = {} - const allIndiciesUsedMask = 2 ** nums.length - 1 - const dfs = (usedIndicesBitMask) => { - if (usedIndicesBitMask === allIndiciesUsedMask) { - return 0 - } - if (cache[usedIndicesBitMask]) { - return cache[usedIndicesBitMask] - } - const valsToIndices = {} - for (let i = 0; i < nums.length; i++) { - const indexMask = 1 << i - if (usedIndicesBitMask & indexMask) continue - const value = nums[i] - if (!valsToIndices.hasOwnProperty(value)) { - valsToIndices[value] = i - } - } - const indicesAvailable = Object.values(valsToIndices) - let minIncompatibilityCost = Infinity - const combinations = createCombinations(indicesAvailable, maxInBucket) - for (const indices of combinations) { - let nextMask = usedIndicesBitMask - let minVal = Infinity - let maxVal = -Infinity - for (const index of indices) { - minVal = Math.min(minVal, nums[index]) - maxVal = Math.max(maxVal, nums[index]) - nextMask = nextMask | (1 << index) - } - const incompatibilityCost = maxVal - minVal - minIncompatibilityCost = Math.min( - minIncompatibilityCost, - dfs(nextMask) + incompatibilityCost - ) - } - return (cache[usedIndicesBitMask] = minIncompatibilityCost) - } - return dfs(0) -} - -function createCombinations(indices, len) { - const combinations = [] - if (indices.length < len) { - return combinations - } - const stack = [[[], 0]] - while (stack.length > 0) { - let [combi, i] = stack.pop() - for (; i < indices.length; i++) { - const combination = [...combi, indices[i]] - if (combination.length === len) { - combinations.push(combination) - } else { - stack.push([combination, i + 1]) - } - } - } - return combinations -} diff --git a/169-majority-element.js b/169-majority-element.js index 91f38a94..4ac9f799 100755 --- a/169-majority-element.js +++ b/169-majority-element.js @@ -17,39 +17,3 @@ const majorityElement = function(nums) { .sort((a, b) => b - a)[0]; }; -// another - -/** - * @param {number[]} nums - * @return {number} - */ -const majorityElement = function(nums) { - let cnt = 1, candidate = nums[0] - for(let i = 1, n = nums.length; i < n; i++) { - if(candidate === nums[i]) cnt++ - else cnt-- - if(cnt === 0) { - cnt = 1 - candidate = nums[i] - } - } - return candidate -}; - -// another - -/** - * @param {number[]} nums - * @return {number} - */ -const majorityElement = function(nums) { - let cnt = 1, candidate = nums[0] - for(let i = 1, n = nums.length; i < n; i++) { - if(cnt === 0) { - cnt = 1 - candidate = nums[i] - }else if(candidate === nums[i]) cnt++ - else cnt-- - } - return candidate -}; diff --git a/1691-maximum-height-by-stacking-cuboids.js b/1691-maximum-height-by-stacking-cuboids.js index 2dbc487d..550cf8bf 100644 --- a/1691-maximum-height-by-stacking-cuboids.js +++ b/1691-maximum-height-by-stacking-cuboids.js @@ -31,33 +31,3 @@ var maxHeight = function (cuboids) { } } -// another - -/** - * @param {number[][]} cuboids - * @return {number} - */ -var maxHeight = function(cuboids) { - cuboids.forEach((cuboid) => cuboid.sort((a, b) => a - b)); - cuboids.sort((a, b) => { - if (a[0] !== b[0]) return b[0] - a[0]; - if (a[1] !== b[1]) return b[1] - a[1]; - return b[2] - a[2]; - }); - const n = cuboids.length; - const dp = Array(n).fill(0); - let res = 0; - for (let j = 0; j < n; ++j) { - dp[j] = cuboids[j][2]; - for (let i = 0; i < j; ++i) { - if (cuboids[i][0] >= cuboids[j][0] - && cuboids[i][1] >= cuboids[j][1] - && cuboids[i][2] >= cuboids[j][2] - ) { - dp[j] = Math.max(dp[j], dp[i] + cuboids[j][2]); - } - } - res = Math.max(res, dp[j]); - } - return res; -}; diff --git a/1697-checking-existence-of-edge-length-limited-paths.js b/1697-checking-existence-of-edge-length-limited-paths.js index 72ef93c5..8c4d81fe 100644 --- a/1697-checking-existence-of-edge-length-limited-paths.js +++ b/1697-checking-existence-of-edge-length-limited-paths.js @@ -46,63 +46,3 @@ class UF { } -// another - -/** - * @param {number} n - * @param {number[][]} edgeList - * @param {number[][]} queries - * @return {boolean[]} - */ -const distanceLimitedPathsExist = function (n, edgeList, queries) { - edgeList.sort((a, b) => a[2] - b[2]) - const m = queries.length - const ans = Array(m).fill(false) - const order = Array(m).fill(0) - for (let i = 0; i < m; ++i) order[i] = i - order.sort((i, j) => queries[i][2] - queries[j][2]) - const uf = new UnionFind(n) - let idx = 0 - for (let i of order) { - const limit = queries[i][2] - while (idx < edgeList.length && edgeList[idx][2] < limit) { - const [u, v] = edgeList[idx] - uf.union(u, v) - idx++ - } - const [u0, v0] = queries[i] - if (uf.find(u0) === uf.find(v0)) ans[i] = true - } - return ans -} -class UnionFind { - constructor(n) { - this.parents = Array(n) - .fill(0) - .map((e, i) => i) - this.ranks = Array(n).fill(0) - } - root(x) { - while (x !== this.parents[x]) { - this.parents[x] = this.parents[this.parents[x]] - x = this.parents[x] - } - return x - } - find(x) { - return this.root(x) - } - check(x, y) { - return this.root(x) === this.root(y) - } - union(x, y) { - const [rx, ry] = [this.find(x), this.find(y)] - if (this.ranks[rx] >= this.ranks[ry]) { - this.parents[ry] = rx - this.ranks[rx] += this.ranks[ry] - } else if (this.ranks[ry] > this.ranks[rx]) { - this.parents[rx] = ry - this.ranks[ry] += this.ranks[rx] - } - } -} diff --git a/1698-number-of-distinct-substrings-in-a-string.js b/1698-number-of-distinct-substrings-in-a-string.js index f67060eb..cd48d3b1 100644 --- a/1698-number-of-distinct-substrings-in-a-string.js +++ b/1698-number-of-distinct-substrings-in-a-string.js @@ -13,30 +13,3 @@ const countDistinct = function(s) { return set.size }; -// another - -/** - * @param {string} s - * @return {number} - */ -const countDistinct = function (s, count = 0) { - const root = new Trie() - const N = s.length - for (let i = 0; i < N; i++) { - let node = root - for (let j = i; j < N; j++) { - const c = s[j] - if (!node.children.has(c)) { - node.children.set(c, new Trie()) - count++ - } - node = node.children.get(c) - } - } - return count -} -class Trie { - constructor() { - this.children = new Map() - } -} diff --git a/1703-maximum-binary-string-after-change.js b/1703-maximum-binary-string-after-change.js index 903e97d4..bb31a65a 100644 --- a/1703-maximum-binary-string-after-change.js +++ b/1703-maximum-binary-string-after-change.js @@ -31,36 +31,3 @@ const minMoves = function (nums, k) { return res } -// another - - -/** - * @param {number[]} nums - * @param {number} k - * @return {number} - */ -const minMoves = function (nums, k) { - if (k === 1) return 0 - let n = 0 - const pos = [] - for (let i = 0; i < nums.length; ++i) { - if (nums[i]) pos.push(i - n++) - } - const sums = [] - sums[0] = pos[0] - for (let i = 1; i < n; ++i) sums[i] = pos[i] + sums[i - 1] - let res = Number.MAX_VALUE - let l = (k / 2) >> 0, - r = k - l - 1 - for (let i = 0; i + k <= n; ++i) { - const m = i + ((k / 2) >>> 0) - const cur = - pos[m] * l - - (sums[m - 1] - sums[i] + pos[i]) - - pos[m] * r + - sums[i + k - 1] - - sums[m] - res = Math.min(cur, res) - } - return res -} diff --git a/1707-maximum-xor-with-an-element-from-array.js b/1707-maximum-xor-with-an-element-from-array.js index 7aa24804..d17a2968 100644 --- a/1707-maximum-xor-with-an-element-from-array.js +++ b/1707-maximum-xor-with-an-element-from-array.js @@ -47,55 +47,3 @@ const maximizeXor = function (nums, queries) { } -// another - -/** - * @param {number[]} nums - * @param {number[][]} queries - * @return {number[]} - */ -const maximizeXor = function (nums, queries) { - const n = queries.length - const result = new Array(n) - const trie = [null, null] - for (let num of nums) { - let node = trie - for (let i = 30; i >= 0; i--) { - const b = 1 << i - if (b & num) { - if (!node[1]) node[1] = [null, null] - node = node[1] - } else { - if (!node[0]) node[0] = [null, null] - node = node[0] - } - } - } - const min = Math.min(...nums) - const dfs = (node, num, i, val, max) => { - if (!node || val > max) return -1 - if (i === -1) return val - const bit = 1 << i - i-- - if (bit > max) return dfs(node[0], num, i, val, max) - if (num & bit) { - let x = dfs(node[0], num, i, val, max) - if (x > -1) return x - return dfs(node[1], num, i, val | bit, max) - } else { - let y = dfs(node[1], num, i, val | bit, max) - if (y > -1) return y - return dfs(node[0], num, i, val, max) - } - } - - for (let i = 0; i < n; i++) { - const [num, max] = queries[i] - if (max < min) { - result[i] = -1 - continue - } - result[i] = dfs(trie, num, 30, 0, max) ^ num - } - return result -} diff --git a/171-excel-sheet-column-number.js b/171-excel-sheet-column-number.js index 2beff2fd..1bca50e7 100755 --- a/171-excel-sheet-column-number.js +++ b/171-excel-sheet-column-number.js @@ -14,15 +14,3 @@ const titleToNumber = function(s) { console.log(titleToNumber("A")); console.log(titleToNumber("AA")); -// another - -/** - * @param {string} s - * @return {number} - */ -const titleToNumber = function(s) { - let result = 0; - const A = 'A'.charCodeAt(0) - for (let i = 0; i < s.length; result = result * 26 + (s.charCodeAt(i) - A + 1), i++); - return result; -}; diff --git a/1713-minimum-operations-to-make-a-subsequence.js b/1713-minimum-operations-to-make-a-subsequence.js index 72b75060..0b5817c7 100644 --- a/1713-minimum-operations-to-make-a-subsequence.js +++ b/1713-minimum-operations-to-make-a-subsequence.js @@ -22,120 +22,3 @@ const minOperations = function(target, arr) { return target.length - stk.length }; -// another - -/** - * @param {number[]} target - * @param {number[]} arr - * @return {number} - */ -const minOperations = function (target, arr) { - let length1 = target.length, - length2 = arr.length - const targetMap = new Map() - for (let i = 0; i < length1; i++) targetMap.set(target[i], i) - const list = new Array() - for (let i = 0; i < length2; i++) { - let num = arr[i] - if (targetMap.has(num)) list.push(targetMap.get(num)) - } - let longestIncreasing = lengthOfLIS(list) - return target.length - longestIncreasing - - function lengthOfLIS(list) { - let length = 1, - size = list.length - if (size == 0) return 0 - const d = new Array(size + 1).fill(0) - d[length] = list[0] - for (let i = 1; i < size; ++i) { - if (list[i] > d[length]) { - d[++length] = list[i] - } else { - let left = 1, - right = length, - pos = 0 - while (left <= right) { - let mid = (left + right) >> 1 - if (d[mid] < list[i]) { - pos = mid - left = mid + 1 - } else { - right = mid - 1 - } - } - d[pos + 1] = list[i] - } - } - return length - } -} - -// another - -/** - * @param {number[]} target - * @param {number[]} arr - * @return {number} - */ -const minOperations = function(target, arr) { - const map = new Map() - for(let i = 0, len = target.length; i < len; i++) { - map.set(target[i], i) - } - const stack = [] - for(let a of arr) { - if(!map.has(a)) continue - if(stack.length === 0 || map.get(a) > stack[stack.length - 1]) { - stack.push(map.get(a)) - continue - } - let left = 0, right = stack.length - 1, mid - while(left < right) { - mid = left + ((right - left) >> 1) - if(stack[mid] < map.get(a)) left = mid + 1 - else right = mid - } - stack[left] = map.get(a) - } - - return target.length - stack.length -}; - -// another - -/** - * @param {number[]} target - * @param {number[]} arr - * @return {number} - */ -const minOperations = function(target, arr) { - const hash = {} - for(let i = 0, n = target.length; i < n; i++) { - hash[target[i]] = i - } - const stack = [] - - for(let e of arr) { - if(hash[e] == null) continue - const cur = hash[e] - if(stack.length && cur > stack[stack.length - 1]) { - stack.push(cur) - continue - } - - let l = 0, r = stack.length - 1 - - while(l < r) { - const mid = ~~((l + r) / 2) - if(stack[mid] < cur) { - l = mid + 1 - } else r = mid - } - - stack[l] = cur - - } - - return target.length - stack.length -}; diff --git a/1717-maximum-score-from-removing-substrings.js b/1717-maximum-score-from-removing-substrings.js index 63030915..e8488f75 100644 --- a/1717-maximum-score-from-removing-substrings.js +++ b/1717-maximum-score-from-removing-substrings.js @@ -29,41 +29,3 @@ const maximumGain = function (s, x, y) { } } -// another - -/** - * @param {string} s - * @param {number} x - * @param {number} y - * @return {number} - */ -const maximumGain = function (s, x, y) { - return Math.max(go(s, x, y, 'a', 'b'), go(s, y, x, 'b', 'a')) -} - -function go(s, x, y, a, b) { - const n = s.length - const st = new Array(n) - let sc = 0 - let p = 0 - for (let c of s) { - if (p - 1 >= 0 && st[p - 1] === a && c === b) { - sc += x - p-- - } else { - st[p++] = c - } - } - const st2 = new Array(p) - let q = 0 - for (let u = 0; u < p; u++) { - let c = st[u] - if (q - 1 >= 0 && st2[q - 1] === b && c === a) { - sc += y - q-- - } else { - st2[q++] = c - } - } - return sc -} diff --git a/1723-find-minimum-time-to-finish-all-jobs.js b/1723-find-minimum-time-to-finish-all-jobs.js index 94a8c01b..f93e38cd 100644 --- a/1723-find-minimum-time-to-finish-all-jobs.js +++ b/1723-find-minimum-time-to-finish-all-jobs.js @@ -30,122 +30,3 @@ const minimumTimeRequired = function(jobs, k) { return dp[k][limit - 1] }; -// another - -/** - * @param {number[]} jobs - * @param {number} k - * @return {number} - */ -const minimumTimeRequired = function(jobs, k) { - const workers = Array(k).fill(0) - let res = Infinity - const n = jobs.length - - dfs(0) - - return res - - function dfs(idx) { - if(idx === n) { - res = Math.min(res, Math.max(...workers)) - return - } - const e = jobs[idx] - for(let i = 0; i < k; i++) { - if(workers[i] + e >= res) continue - workers[i] += e - dfs(idx + 1) - workers[i] -= e - if(workers[i] === 0) break - } - } -}; - -// another - -/** - * @param {number[]} jobs - * @param {number} k - * @return {number} - */ -const minimumTimeRequired = function (jobs, k) { - if (jobs.length <= k) { - return Math.max(...jobs) - } - - // create a store to hold the number of hours each worker worked - const workers = new Array(k).fill(0) - - let minLongestWorkingTime = Infinity - const dfs = (i) => { - if (i === jobs.length) { - // if we assigned all the jobs, see if we have a better result - minLongestWorkingTime = Math.min( - minLongestWorkingTime, - Math.max(...workers) - ) - return - } - const lengthOfWork = jobs[i] - - for (let worker = 0; worker < k; worker++) { - workers[worker] += lengthOfWork - - // if this combination is has a chance of decreasing our - // answer, try it, otherwise skip it to save on time. - if (workers[worker] <= minLongestWorkingTime) { - dfs(i + 1) - } - workers[worker] -= lengthOfWork - - // We want to minimize the width of the tree - // so if the worker has gotten their first job - // don't try any workers after this worker. - // All other workers after this worker will be 0 as well - // so the combination is exactly the same. - if (workers[worker] === 0) break - } - } - - dfs(0) - return minLongestWorkingTime -} - -// another - -/** - * @param {number[]} jobs - * @param {number} k - * @return {number} - */ -const minimumTimeRequired = function(jobs, k) { - return solution(jobs, k) -}; - -function solution(jobs, k) { - const n = jobs.length - let res = Infinity, arr = Array(k).fill(0) - - let start = 0 - bt(0) - return res - - function bt(idx) { - start++ - if(idx === n) { - res = Math.min(res, Math.max(...arr)) - return - } - const visited = new Set() - for(let j = start; j < start + k; j++) { - const i = j % k - if(visited.has(arr[i])) continue - if(arr[i] + jobs[idx] > res) continue - visited.add(arr[i]) - arr[i] += jobs[idx] - bt(idx + 1) - arr[i] -= jobs[idx] - } - } -} diff --git a/173-binary-search-tree-iterator.js b/173-binary-search-tree-iterator.js index 92044020..025e771b 100644 --- a/173-binary-search-tree-iterator.js +++ b/173-binary-search-tree-iterator.js @@ -43,64 +43,3 @@ BSTIterator.prototype.hasNext = function() { * var param_2 = obj.hasNext() */ -// another - -/** - * Definition for a binary tree node. - * function TreeNode(val) { - * this.val = val; - * this.left = this.right = null; - * } - */ -/** - * @param {TreeNode} root - */ -const BSTIterator = function(root) { - this.generator = dfsGenerator(root) - this.root = root - this.nextSmall = this.generator.next().value -} -function* dfsGenerator(root) { - if (root === null) return - const stack = [] - let current = root - while (true) { - if (current) { - stack.push(current) - current = current.left - } else if (stack.length) { - const top = stack.pop() - yield top.val - current = top.right - } else { - break - } - } -} - -/** - * @return the next smallest number - * @return {number} - */ -BSTIterator.prototype.next = function() { - const smallReturn = this.nextSmall - this.nextSmall = this.generator.next().value - return smallReturn -} - -/** - * @return whether we have a next smallest number - * @return {boolean} - */ -BSTIterator.prototype.hasNext = function() { - return this.nextSmall !== undefined ? true : false -} - -/** - * Your BSTIterator object will be instantiated and called as such: - * var obj = new BSTIterator(root) - * var param_1 = obj.next() - * var param_2 = obj.hasNext() - */ - - diff --git a/1734-decode-xored-permutation.js b/1734-decode-xored-permutation.js index 1ae0f396..cd10579d 100644 --- a/1734-decode-xored-permutation.js +++ b/1734-decode-xored-permutation.js @@ -17,48 +17,3 @@ const decode = function(encoded) { return res }; -// another - -/** - * @param {number[]} encoded - * @return {number[]} - */ -const decode = function(encoded) { - let a = 0 - const n = encoded.length + 1 - for(let i = 0; i <= n; i++) { - a ^= i - if(i < n && i % 2 === 1) a ^= encoded[i] - } - const res = [a] - for(let i = 0; i < n - 1; i++) { - res[i + 1] = res[i] ^ encoded[i] - } - - return res -}; - -// another - -/** - * @param {number[]} encoded - * @return {number[]} - */ -const decode = function(A) { - let xor = 0 - const len = A.length - const permLen = len + 1 - for(let i = 1; i <= permLen; i++) { - xor ^= i - } - // except first - for(let i = 1; i < len; i += 2) xor ^= A[i] - const first = xor - const res = [xor] - let pre = xor - for(let i = 1; i < permLen; i++) { - res[i] = A[i - 1] ^ pre - pre = res[i] - } - return res; -}; diff --git a/1737-change-minimum-characters-to-satisfy-one-of-three-conditions.js b/1737-change-minimum-characters-to-satisfy-one-of-three-conditions.js index aa948052..064c2d44 100644 --- a/1737-change-minimum-characters-to-satisfy-one-of-three-conditions.js +++ b/1737-change-minimum-characters-to-satisfy-one-of-three-conditions.js @@ -29,68 +29,3 @@ const minCharacters = function (a, b) { return res } -// another - -/** - * @param {string} a - * @param {string} b - * @return {number} - */ -const minCharacters = function(a, b) { - return Math.min(method1(a, b), method1(b, a), method3(a, b)) -}; - -function method1(str1, str2) { - let res = Infinity, a = 'a'.charCodeAt(0) - for(let i = 1; i < 26; i++) { - let cnt1 = 0, cnt2 = 0, mid = String.fromCharCode(a + i) - for(let ch of str1) { - if(ch >= mid) cnt1++ - } - for(let ch of str2) { - if(ch < mid) cnt2++ - } - res = Math.min(res, cnt1 + cnt2) - } - return res -} - -function method3(str1, str2) { - const a = 'a'.charCodeAt(0) - const cnt1 = Array(26).fill(0), cnt2 = Array(26).fill(0) - for(let ch of str1) cnt1[ch.charCodeAt(0) - a]++ - for(let ch of str2) cnt2[ch.charCodeAt(0) - a]++ - return str1.length + str2.length - Math.max(...cnt1) - Math.max(...cnt2) -} - -// another - -/** - * @param {string} a - * @param {string} b - * @return {number} - */ -const minCharacters = function (a, b) { - const m = a.length, n = b.length - let res = m + n - const cnt1 = Array(26).fill(0), cnt2 = Array(26).fill(0) - const ac = 'a'.charCodeAt(0) - for(let ch of a) cnt1[ch.charCodeAt(0) - ac]++ - for(let ch of b) cnt2[ch.charCodeAt(0) - ac]++ - const c3 = res - Math.max(...cnt1) - Math.max(...cnt2) - for(let i = 0; i < 26; i++) { - if(i > 0) { - cnt1[i] += cnt1[i - 1] - cnt2[i] += cnt2[i - 1] - } - - if(i < 25) { - res = Math.min(res, m - cnt1[i] + cnt2[i]) - res = Math.min(res, n - cnt2[i] + cnt1[i]) - } - } - - return Math.min(res, c3) -} - - diff --git a/1738-find-kth-largest-xor-coordinate-value.js b/1738-find-kth-largest-xor-coordinate-value.js index b593d9f7..b5e56069 100644 --- a/1738-find-kth-largest-xor-coordinate-value.js +++ b/1738-find-kth-largest-xor-coordinate-value.js @@ -27,139 +27,3 @@ var kthLargestValue = function(matrix, k) { return v[k - 1]; }; -// another - -/** - * @param {number[][]} matrix - * @param {number} k - * @return {number} - */ -const kthLargestValue = function(matrix, k) { - const tmp = [] - const n = matrix.length, m = matrix[0].length - const dp = Array.from({ length: n }, () => Array(m).fill(0)) - dp[0][0] = matrix[0][0] - tmp.push(dp[0][0]) - for(let j = 1; j < m; j++) { - dp[0][j] = dp[0][j - 1] ^ matrix[0][j] - tmp.push(dp[0][j]) - } - for(let i = 1; i < n; i++) { - dp[i][0] = dp[i - 1][0] ^ matrix[i][0] - tmp.push(dp[i][0]) - } - for(let i = 1; i < n; i++) { - for(let j = 1; j < m; j++) { - dp[i][j] = dp[i][j - 1] ^ dp[i - 1][j] ^ matrix[i][j] ^ dp[i - 1][j - 1] - tmp.push(dp[i][j]) - } - } - tmp.sort((a, b) => b - a) - return tmp[k - 1] -}; - - -// another - -/** - * @param {number[][]} matrix - * @param {number} k - * @return {number} - */ -const kthLargestValue = function(matrix, k) { - if(matrix == null || matrix[0] == null) return 0 - const m = matrix.length, n = matrix[0].length - const res = Array.from({ length: m }, () => Array(n).fill(0)) - res[0][0] = matrix[0][0] - for(let i = 1; i < m; i++) { - res[i][0] = res[i - 1][0] ^ matrix[i][0] - } - for(let j = 1; j < n; j++) { - res[0][j] = res[0][j - 1] ^ matrix[0][j] - } - - for(let i = 1; i < m; i++) { - for(let j = 1; j < n; j++) { - res[i][j] = res[i][j - 1] ^ res[i - 1][j] ^ res[i - 1][j - 1] ^ matrix[i][j] - } - } - - const pq = new PriorityQueue((a, b) => a < b) - for(let i = 0; i < m; i++) { - for(let j = 0; j < n; j++) { - pq.push(res[i][j]) - if(pq.size() > k) pq.pop() - } - } - - return pq.pop() - -}; - -class PriorityQueue { - constructor(comparator = (a, b) => a > b) { - this.heap = [] - this.top = 0 - this.comparator = comparator - } - size() { - return this.heap.length - } - isEmpty() { - return this.size() === 0 - } - peek() { - return this.heap[this.top] - } - push(...values) { - values.forEach((value) => { - this.heap.push(value) - this.siftUp() - }) - return this.size() - } - pop() { - const poppedValue = this.peek() - const bottom = this.size() - 1 - if (bottom > this.top) { - this.swap(this.top, bottom) - } - this.heap.pop() - this.siftDown() - return poppedValue - } - replace(value) { - const replacedValue = this.peek() - this.heap[this.top] = value - this.siftDown() - return replacedValue - } - - parent = (i) => ((i + 1) >>> 1) - 1 - left = (i) => (i << 1) + 1 - right = (i) => (i + 1) << 1 - greater = (i, j) => this.comparator(this.heap[i], this.heap[j]) - swap = (i, j) => ([this.heap[i], this.heap[j]] = [this.heap[j], this.heap[i]]) - siftUp = () => { - let node = this.size() - 1 - while (node > this.top && this.greater(node, this.parent(node))) { - this.swap(node, this.parent(node)) - node = this.parent(node) - } - } - siftDown = () => { - let node = this.top - while ( - (this.left(node) < this.size() && this.greater(this.left(node), node)) || - (this.right(node) < this.size() && this.greater(this.right(node), node)) - ) { - let maxChild = - this.right(node) < this.size() && - this.greater(this.right(node), this.left(node)) - ? this.right(node) - : this.left(node) - this.swap(node, maxChild) - node = maxChild - } - } -} diff --git a/1739-building-boxes.js b/1739-building-boxes.js index 420f84d6..4ccbdb62 100644 --- a/1739-building-boxes.js +++ b/1739-building-boxes.js @@ -13,25 +13,3 @@ const minimumBoxes = function(n) { return c }; -// another - - -/** - * @param {number} n - * @return {number} - */ -const minimumBoxes = function(n) { - let sum = 1n, base = 1n, row = 1n; - n = BigInt(n) - while (sum < n) { - base += (++row); - sum += base; - } - while (sum > n) { - --base; - sum -= (row--); - if (sum < n) return base + 1n; - } - return base; -}; - diff --git a/174-dungeon-game.js b/174-dungeon-game.js index 99988ffc..75e044c7 100644 --- a/174-dungeon-game.js +++ b/174-dungeon-game.js @@ -22,23 +22,3 @@ const calculateMinimumHP = function (dungeon) { } -// another - -/** - * @param {number[][]} dungeon - * @return {number} - */ -const calculateMinimumHP = function (dungeon) { - const n = dungeon.length, - m = dungeon[0].length - const dp = Array(n + 1).fill(Number.MAX_VALUE) - dp[n - 1] = 1 - for (let j = m - 1; j >= 0; j--) { - for (let i = n - 1; i >= 0; i--) { - dp[i] = Math.min(dp[i], dp[i + 1]) - dungeon[i][j] - dp[i] = Math.max(1, dp[i]) - } - } - return dp[0] -} - diff --git a/1740-find-distance-in-a-binary-tree.js b/1740-find-distance-in-a-binary-tree.js index 97dae690..5b7afa50 100644 --- a/1740-find-distance-in-a-binary-tree.js +++ b/1740-find-distance-in-a-binary-tree.js @@ -36,128 +36,3 @@ const findDistance = function(root, p, q) { } }; -// another - -/** - * Definition for a binary tree node. - * function TreeNode(val, left, right) { - * this.val = (val===undefined ? 0 : val) - * this.left = (left===undefined ? null : left) - * this.right = (right===undefined ? null : right) - * } - */ -/** - * @param {TreeNode} root - * @param {number} p - * @param {number} q - * @return {number} - */ -const findDistance = function (root, p, q) { - if (p == q) return 0 - let result = -1 - dfs(root, p, q) - return result - - /** - The return value means the distance from root node to EITHER p OR q. If - neither p nor q are reachable from the root, return -1. - - It is either p or q but not both, because if the root node can reach both - p and q, it is a common ancestor of p and q and the answer should already - be available. - **/ - function dfs(root, p, q) { - if (root == null) return -1 - - let left = dfs(root.left, p, q) - let right = dfs(root.right, p, q) - - if (root.val == p || root.val == q) { - // root is p or q, but none of p or q is a descendent of root. - // The distance from root to one of p and q is 0 in this case. - if (left < 0 && right < 0) { - return 0 - } - - // root is p or q, and root is also the LCA of p and q. - result = 1 + (left >= 0 ? left : right) - return -1 - } - - // root is neither p nor q, but it is the LCA of p and q. - if (left >= 0 && right >= 0) { - result = left + right + 2 - return -1 - } - - if (left >= 0) { - return left + 1 - } - - if (right >= 0) { - return right + 1 - } - - return -1 - } -} - -// another - -/** - * Definition for a binary tree node. - * function TreeNode(val, left, right) { - * this.val = (val===undefined ? 0 : val) - * this.left = (left===undefined ? null : left) - * this.right = (right===undefined ? null : right) - * } - */ -/** - * @param {TreeNode} root - * @param {number} p - * @param {number} q - * @return {number} - */ -const findDistance = function (root, p, q) { - let lca = lowestCommonAncestor(root, p, q) - - const queue = [] - queue.push(lca) - let dp = -1, - dq = -1 - let d = 0 - while (queue.length && (dp == -1 || dq == -1)) { - for (let k = queue.length; k > 0; k--) { - let node = queue.shift() - if (node.val == p) { - dp = d - } - - if (node.val == q) { - dq = d - } - - if (node.left != null) { - queue.push(node.left) - } - - if (node.right != null) { - queue.push(node.right) - } - } - d++ - } - - return dp + dq - - function lowestCommonAncestor(root, p, q) { - if (root == null || root.val == p || root.val == q) { - return root - } - let left = lowestCommonAncestor(root.left, p, q) - let right = lowestCommonAncestor(root.right, p, q) - - return left == null ? right : right == null ? left : root - } -} - diff --git a/1745-palindrome-partitioning-iv.js b/1745-palindrome-partitioning-iv.js index 8576c981..6f6ddaee 100644 --- a/1745-palindrome-partitioning-iv.js +++ b/1745-palindrome-partitioning-iv.js @@ -47,54 +47,3 @@ function manacher(s) { return ans; } -// another - -/** - * @param {string} s - * @return {boolean} - */ -const checkPartitioning = function (s) { - const n = s.length - const dp = Array.from({ length: n }, () => Array(n).fill(false)) - for(let i = n - 1; i >= 0; i--) { - for(let j = i; j < n; j++) { - if(s[i] === s[j]) { - dp[i][j] = i + 1 <= j - 1 ? dp[i + 1][j - 1] : true - } else dp[i][j] = false - } - } - for(let i = 1; i < n - 1; i++) { - for(let j = i; j < n - 1; j++) { - if(dp[0][i - 1] && dp[i][j] && dp[j + 1][n - 1]) return true - } - } - return false -} - - -// another - -/** - * @param {string} s - * @return {boolean} - */ -const checkPartitioning = function(s) { - for(let i = 1, len = s.length; i < len - 1; i++) { - for(let j = i + 1; j < len; j++) { - const s1 = s.slice(0, i), s2 = s.slice(i, j), s3 = s.slice(j) - if(chk(s1) && chk(s2) && chk(s3)) return true - } - } - return false -}; - -function chk(s) { - let l = 0, r = s.length - 1 - for(;l <= r;) { - if(s[l] === s[r]) { - l++ - r-- - } else return false - } - return true -} diff --git a/1749-maximum-absolute-sum-of-any-subarray.js b/1749-maximum-absolute-sum-of-any-subarray.js index 503164e2..c52817c9 100644 --- a/1749-maximum-absolute-sum-of-any-subarray.js +++ b/1749-maximum-absolute-sum-of-any-subarray.js @@ -14,34 +14,3 @@ const maxAbsoluteSum = function (nums) { return max - min } -// another - -/** - * @param {number[]} nums - * @return {number} - */ -const maxAbsoluteSum = function (nums) { - let min = Infinity, - max = -Infinity - let positiveSum = 0, - negativeSum = 0 - for (let num of nums) { - positiveSum += num - if (positiveSum > max) { - max = positiveSum - } - - if (positiveSum < 0) { - positiveSum = 0 - } - negativeSum += num - if (negativeSum < min) { - min = negativeSum - } - if (negativeSum > 0) { - negativeSum = 0 - } - } - - return Math.max(Math.abs(min), max) -} diff --git a/1751-maximum-number-of-events-that-can-be-attended-ii.js b/1751-maximum-number-of-events-that-can-be-attended-ii.js index 6f1ce829..a012efdb 100644 --- a/1751-maximum-number-of-events-that-can-be-attended-ii.js +++ b/1751-maximum-number-of-events-that-can-be-attended-ii.js @@ -24,81 +24,3 @@ function helper(memo, events, n, i, k) { ) } -// another - -/** - * @param {number[][]} events - * @param {number} k - * @return {number} - */ -const maxValue = function (events, k) { - // d[i][j] 表示以 events[i]结尾的取最多j个最大值 - // d[i][j-1], Math.max( d[m][j-1] + v[i]) for m ending < start[i] - events.sort((a, b) => a[1] - b[1]) - const n = events.length - let d = [] - for (let j = 0; j <= k; j++) { - const newD = [] - for (let i = 0; i < n; i++) { - if (j === 0) { - newD[i] = 0 - } else if (j === 1) { - newD[i] = events[i][2] - } else if (i === 0) { - newD[i] = events[i][2] - } else { - newD[i] = d[i] // 以i结尾最多取j-1次的最大值 - const v = events[i][2] - const start = events[i][0] - for (let m = 0; m < i; m++) { - if (events[m][1] < start) { - if (d[m] + v > newD[i]) { - newD[i] = d[m] + v - } - } else { - break - } - } - } - } - d = [...newD] - } - return Math.max(...d) -} - - -// another - - -/** - * @param {number[][]} events - * @param {number} k - * @return {number} - */ -const maxValue = function(events, k) { - - cache = new Map(); - events.sort((a,b) => a[0] - b[0]); - return dfs(events, 0, -1, k); - - function dfs(events, idx, end, k){ - let key = idx + "," + end + "," + k; - if(cache.has(key)){ - return cache.get(key); - } - if(idx >= events.length){ - return 0; - } - if(k == 0) { - return 0; - } - let max = 0; - if(events[idx][0] > end){ - max = Math.max(max, dfs(events, idx+1, events[idx][1], k-1) + events[idx][2]); - } - - max = Math.max(max, dfs(events, idx+1, end, k)); - cache.set(key, max); - return max; - } -}; diff --git a/1754-largest-merge-of-two-strings.js b/1754-largest-merge-of-two-strings.js index f910ba72..b85dd836 100644 --- a/1754-largest-merge-of-two-strings.js +++ b/1754-largest-merge-of-two-strings.js @@ -33,41 +33,3 @@ var largestMerge = function(word1, word2) { return merge; }; -// another - -/** - * @param {string} word1 - * @param {string} word2 - * @return {string} - */ -const largestMerge = function(word1, word2) { - const stack1 = word1.split(''), stack2 = word2.split('') - const arr = [] - - while(stack1.length && stack2.length) { - const c1 = stack1[0], c2 = stack2[0] - if(c1 > c2) { - stack1.shift() - arr.push(c1) - } else if(c1 < c2) { - stack2.shift() - arr.push(c2) - } else { - if(stack1.join('') > stack2.join('')) { - stack1.shift() - arr.push(c1) - } else { - stack2.shift() - arr.push(c2) - } - } - } - if(stack1.length) { - arr.push(...stack1) - } - if(stack2.length) { - arr.push(...stack2) - } - - return arr.join('') -}; diff --git a/1755-closest-subsequence-sum.js b/1755-closest-subsequence-sum.js index f7fee8e8..7a31da7f 100644 --- a/1755-closest-subsequence-sum.js +++ b/1755-closest-subsequence-sum.js @@ -51,80 +51,3 @@ const minAbsDifference = function (nums, goal) { } -// another - -/** - * @param {number[]} nums - * @param {number} goal - * @return {number} - */ -var minAbsDifference = function(a, b) { - let n = a.length, m = (n / 2) >> 0, r = n - m; - let ans = 2e9; - const {max, min, abs} = Math - const va = [], vb = []; - for(let i=0;i<1<>j&1) tmp+=a[j]; - } - ans=min(ans,abs(tmp-b)); - va.push(tmp); - } - // sort(va.begin(), va.end()); - va.sort((a, b) => a - b) - for(let i=0;i<1<>j&1) tmp+=a[j+m]; - } - ans=min(ans,abs(tmp-b)); - let k=b-tmp; - let pos=lower_bound(va, k); - for(let j=pos-1;j<=pos+1;++j) { - if(j>=0 && j 0) { - step = Math.floor(len / 2); - middle = first + step; - if (less(array[middle], value, middle)) { - first = middle; - first += 1; - len = len - step - 1; - } else { - len = step; - } - } - return first; -}; - diff --git a/1764-form-array-by-concatenating-subarrays-of-another-array.js b/1764-form-array-by-concatenating-subarrays-of-another-array.js index 1fd9fc52..4e881fa4 100644 --- a/1764-form-array-by-concatenating-subarrays-of-another-array.js +++ b/1764-form-array-by-concatenating-subarrays-of-another-array.js @@ -43,39 +43,3 @@ const canChoose = function (groups, nums) { } -// another - - -/** - * @param {number[][]} groups - * @param {number[]} nums - * @return {boolean} - */ -const canChoose = function(groups, nums) { - let gi = 0, ni = 0 - const n = groups.length, m = nums.length - while(gi < n && ni < m) { - const len = groups[gi].length - let pass = true - if(nums[ni] !== groups[gi][0]) { - ni++ - continue - } - for(let i = 1; i < len; i++) { - if(nums[ni + i] !== groups[gi][i]) { - pass = false - break - } - } - if(pass) { - gi++ - ni += len - } else { - ni++ - } - } - if(gi >= n) return true - - return false - -}; diff --git a/1769-minimum-number-of-operations-to-move-all-balls-to-each-box.js b/1769-minimum-number-of-operations-to-move-all-balls-to-each-box.js index 11f47aa6..cec9f82c 100644 --- a/1769-minimum-number-of-operations-to-move-all-balls-to-each-box.js +++ b/1769-minimum-number-of-operations-to-move-all-balls-to-each-box.js @@ -21,51 +21,3 @@ const minOperations = function(boxes) { }; -// another - - -/** - * @param {string} boxes - * @return {number[]} - */ -const minOperations = function(boxes) { - const res = [] - for(let i = 0, len = boxes.length; i < len; i++) { - res[i] = helper(boxes, i) - } - - return res -}; - -function helper(str, idx) { - let res = 0 - for(let i = 0, len = str.length; i < len; i++) { - if(i === idx || str[i] === '0') continue - res += Math.abs(i - idx) - } - return res -} - -// another - -/** - * @param {string} boxes - * @return {number[]} - */ - const minOperations = function(boxes) { - const n = boxes.length - const res = Array(n).fill(0) - let cnt = 0, sum = 0 - for(let i = 0; i < n; i++) { - res[i] = sum - cnt += boxes[i] === '1' ? 1 : 0 - sum += cnt - } - cnt = 0, sum = 0 - for(let i = n - 1; i >= 0; i--) { - res[i] += sum - cnt += boxes[i] === '1' ? 1 : 0 - sum += cnt - } - return res -}; diff --git a/1771-maximize-palindrome-length-from-subsequences.js b/1771-maximize-palindrome-length-from-subsequences.js index fd093aa0..b372cf0e 100644 --- a/1771-maximize-palindrome-length-from-subsequences.js +++ b/1771-maximize-palindrome-length-from-subsequences.js @@ -31,34 +31,3 @@ function LPS(str) { } -// another - -/** - * @param {string} word1 - * @param {string} word2 - * @return {number} - */ -const longestPalindrome = function(word1, word2) { - const sz = word1.length + word2.length - let res = 0; - const dp = Array.from({ length: sz + 1 }, () => Array(sz + 1).fill(0)) - longestPalindromeSubseq(word1 + word2, dp); - for (let i = 0; i < word1.length; ++i) - for (let j = word2.length - 1; j >= 0; --j) - if (word1[i] == word2[j]) { - res = Math.max(res, dp[i][word1.length + j + 1]); - break; - } - return res; - -} -function longestPalindromeSubseq( s, dp) { - for (let len = 1; len <= s.length; ++len) - for (let i = 0; i + len <= s.length; ++i) - dp[i][i + len] = s[i] == s[i + len - 1] ? - dp[i + 1][i + len - 1] + (len == 1 ? 1 : 2) : - Math.max(dp[i][i + len - 1], dp[i + 1][i + len]); - return dp[0][s.length]; -} - - diff --git a/1774-closest-dessert-cost.js b/1774-closest-dessert-cost.js index 6d918079..70c5d142 100644 --- a/1774-closest-dessert-cost.js +++ b/1774-closest-dessert-cost.js @@ -25,33 +25,3 @@ const closestCost = function(baseCosts, toppingCosts, target) { } }; -// another - - -/** - * @param {number[]} baseCosts - * @param {number[]} toppingCosts - * @param {number} target - * @return {number} - */ -const closestCost = function(baseCosts, toppingCosts, target) { - let n = baseCosts.length, m = toppingCosts.length; - const { abs } = Math - const costs = new Set(); - for (let i = 0; i < n; i++) { - dfs(toppingCosts, 0, m, baseCosts[i], costs); - } - const nums = []; - for (let x of costs) nums.push(x); - nums.sort((a, b) => abs(a - target) == abs(b - target) ? a - b : abs(a - target) - abs(b - target)) - return nums[0]; - -}; - -function dfs(toppingCosts, ind, m, cost, costs) { - costs.add(cost); - if (ind >= m) return; - dfs(toppingCosts, ind + 1, m, cost, costs); - dfs(toppingCosts, ind + 1, m, cost + toppingCosts[ind], costs); - dfs(toppingCosts, ind + 1, m, cost + toppingCosts[ind] * 2, costs); -} diff --git a/1775-equal-sum-arrays-with-minimum-number-of-operations.js b/1775-equal-sum-arrays-with-minimum-number-of-operations.js index 4f723a5a..9a862402 100644 --- a/1775-equal-sum-arrays-with-minimum-number-of-operations.js +++ b/1775-equal-sum-arrays-with-minimum-number-of-operations.js @@ -28,76 +28,3 @@ function sum(arr) { return arr.reduce((ac, e) => ac + e, 0) } -// another - -/** - * @param {number[]} nums1 - * @param {number[]} nums2 - * @return {number} - */ -const minOperations = function(nums1, nums2) { - const m = nums1.length, n = nums2.length - if(m > n * 6 || n > m * 6) return -1 - const sum1 = nums1.reduce((ac, e) => ac + e, 0) - const sum2 = nums2.reduce((ac, e) => ac + e, 0) - let largerArr, smallerArr - if(sum1 === sum2) return 0 - if(sum1 > sum2) { - largerArr = nums1 - smallerArr = nums2 - } else { - largerArr = nums2 - smallerArr = nums1 - } - - const gain = [] - for(let e of largerArr) gain.push(e - 1) - for(let e of smallerArr) gain.push(6 - e) - gain.sort((a, b) => b - a) - let diff = Math.abs(sum2 - sum1) - let cnt = 0 - for(let e of gain) { - diff -= e - cnt++ - if(diff <= 0) return cnt - } - return -1 -}; - - - - -// another - -/** - * @param {number[]} nums1 - * @param {number[]} nums2 - * @return {number} - */ -const minOperations = function(nums1, nums2) { - const len1 = nums1.length, len2 = nums2.length; - if (len1 > 6 * len2 || len2 > 6 * len1) return -1; - let sum1 = 0, sum2 = 0; - for (let x of nums1) sum1 += x; - for (let x of nums2) sum2 += x; - if (sum1 === sum2) return 0; - nums1.sort((a, b) => a - b) - nums2.sort((a, b) => a - b) - let cnt = 0; - if (sum1 > sum2) { - let ind1 = len1 - 1, ind2 = 0; - while (sum1 > sum2) { - if (ind2 === len2 || nums1[ind1] - 1 > 6 - nums2[ind2]) sum1 -= nums1[ind1--] - 1; - else sum2 += 6 - nums2[ind2++]; - cnt++; - } - return cnt; - } - let ind1 = 0, ind2 = len2 - 1; - while (sum1 < sum2) { - if (ind1 === len1 || nums2[ind2] - 1 > 6 - nums1[ind1]) sum2 -= nums2[ind2--] - 1; - else sum1 += 6 - nums1[ind1++]; - cnt++; - } - return cnt; -}; diff --git a/1776-car-fleet-ii.js b/1776-car-fleet-ii.js index 34d81e53..726bd528 100644 --- a/1776-car-fleet-ii.js +++ b/1776-car-fleet-ii.js @@ -26,73 +26,3 @@ const getCollisionTimes = function(cars) { return res }; -// another - - -/** - * @param {number[][]} cars - * @return {number[]} - */ -var getCollisionTimes = function(cars) { - //这道题必须想清楚一点,那就是如果ans[i]有正值,那么一定是cars[i]和某个cars[j](j>i且speed[j]= 0; i--) { - while(stack.length) { - //如果栈顶比我快,我追不上它,可以考虑等它消失之后我去撞它前面的,所以将它pop - if(cars[stack[stack.length - 1]][1] >= cars[i][1]) stack.pop() - //如果栈顶比我慢,我就决定去碰它了 - else { - //如果它不会消失,那我肯定能碰它,break - if(ans[stack[stack.length - 1]] < 0) break - //如果它会消失,我需要计算一下在它消失之前能否追上它 - const d = ans[stack[stack.length - 1]] * (cars[i][1] - cars[stack[stack.length - 1]][1]) - //能追上,那我肯定碰它,break - if(d > cars[stack[stack.length - 1]][0] - cars[i][0]) break - //追不上,那算了,追它前面的车 - else stack.pop() - } - } - if(stack.length === 0) ans[i] = -1 - else { - //相对距离除以相对速度 - const t = (cars[stack[stack.length - 1]][0]-cars[i][0])/(cars[i][1]-cars[stack[stack.length - 1]][1]) - ans[i] = t - } - stack.push(i) - } - return ans -}; - - -// another - -/** - * @param {number[][]} cars - * @return {number[]} - */ -var getCollisionTimes = function (cars) { - let n = cars.length, - t = 0, - i - const ans = Array(n) - for (let i = 0; i < n; i++) ans[i] = -1 - const s = [] - s[++t] = n - 1 - for (let i = n - 2; ~i; i--) { - while (t && cars[s[t]][1] >= cars[i][1]) t-- - while ( - t > 1 && - (cars[s[t]][0] - cars[i][0]) * (cars[i][1] - cars[s[t - 1]][1]) > - (cars[s[t - 1]][0] - cars[i][0]) * (cars[i][1] - cars[s[t]][1]) - ) - t-- - if (t) ans[i] = (cars[s[t]][0] - cars[i][0]) / (cars[i][1] - cars[s[t]][1]) - s[++t] = i - } - return ans -} diff --git a/1786-number-of-restricted-paths-from-first-to-last-node.js b/1786-number-of-restricted-paths-from-first-to-last-node.js index 6a80fd88..11c2a5d2 100644 --- a/1786-number-of-restricted-paths-from-first-to-last-node.js +++ b/1786-number-of-restricted-paths-from-first-to-last-node.js @@ -117,56 +117,3 @@ class PriorityQueue { } } -// another - -/** - * @param {number} n - * @param {number[][]} edges - * @return {number} - */ -const countRestrictedPaths = function(n, edges) { - if (n === 1) return 0 - const graph = {} - for(const [u, v, t] of edges) { - if(graph[u] == null) graph[u] = {} - if(graph[v] == null) graph[v] = {} - graph[u][v] = t - graph[v][u] = t - } - const dist = dijkstra(n, graph) - const memo = Array(n + 1).fill(null) - const res = dfs(1) - return res - - function dijkstra(n, graph) { - const dist = Array(n + 1).fill(Infinity) - dist[n] = 0 - const pq = new PriorityQueue((a, b) => a[0] < b[0]) - pq.push([0, n]) - while(!pq.isEmpty()) { - const [d, cur] = pq.pop() - if(d !== dist[cur]) continue - for(const next of Object.keys(graph[cur] || {})) { - const delta = graph[cur][next] - if(dist[next] > d + delta) { - dist[next] = d + delta - pq.push([d + delta, next]) - } - } - } - return dist - } - - function dfs(src) { - if(memo[src] != null) return memo[src] - if(src === n) return 1 - let res = 0 - for(let next of Object.keys(graph[src] || {})) { - next = +next - if(dist[src] > dist[next]) { - res = ((res + dfs(next)) % (1e9 + 7)) - } - } - return memo[src] = res - } -} diff --git a/1791-find-center-of-star-graph.js b/1791-find-center-of-star-graph.js index b57647b9..148d9566 100644 --- a/1791-find-center-of-star-graph.js +++ b/1791-find-center-of-star-graph.js @@ -6,31 +6,3 @@ const findCenter = function(edges) { return edges[0][0] === edges[1][0] || edges[0][0] === edges[1][1] ? edges[0][0] : edges[0][1] }; -// another - -/** - * @param {number[][]} edges - * @return {number} - */ -const findCenter = function(edges) { - const map = {} - for(let e of edges) { - const [u, v] = e - if(map[u] == null) map[u] = [] - if(map[v] == null) map[v] = [] - map[u].push(v) - map[v].push(u) - } - - const keys = Object.keys(map) - let res, max = -Infinity - keys.forEach(e => { - if(map[e].length > max) { - res = e - max = map[e].length - } - }) - - return res - -}; diff --git a/1792-maximum-average-pass-ratio.js b/1792-maximum-average-pass-ratio.js index 58573af6..c527ab2d 100644 --- a/1792-maximum-average-pass-ratio.js +++ b/1792-maximum-average-pass-ratio.js @@ -104,108 +104,3 @@ class PriorityQueue { -// another - -/** - * @param {number[][]} classes - * @param {number} extraStudents - * @return {number} - */ -const maxAverageRatio = function(classes, extraStudents) { - const pq = new PriorityQueue((a, b) => a.up > b.up); - for (let x of classes) pq.push(new Node(x[0], x[1])); - while (extraStudents--) { - let temp = pq.peek(); - pq.pop(); - temp.pass++, temp.total++; - temp.calUp(); - pq.push(temp); - } - let total = 0.0; - let n = classes.length; - while (!pq.isEmpty()) { - let temp = pq.peek(); - pq.pop(); - total += temp.pass / temp.total; - } - return total / n; -}; - -class Node { - constructor(pass, total) { - this.pass = pass - this.total = total - this.calUp() - } - calUp() { - this.up = (this.pass + 1) / (this.total + 1) - this.pass / this.total - } -} - -class PriorityQueue { - constructor(comparator = (a, b) => a > b) { - this.heap = [] - this.top = 0 - this.comparator = comparator - } - size() { - return this.heap.length - } - isEmpty() { - return this.size() === 0 - } - peek() { - return this.heap[this.top] - } - push(...values) { - values.forEach((value) => { - this.heap.push(value) - this.siftUp() - }) - return this.size() - } - pop() { - const poppedValue = this.peek() - const bottom = this.size() - 1 - if (bottom > this.top) { - this.swap(this.top, bottom) - } - this.heap.pop() - this.siftDown() - return poppedValue - } - replace(value) { - const replacedValue = this.peek() - this.heap[this.top] = value - this.siftDown() - return replacedValue - } - - parent = (i) => ((i + 1) >>> 1) - 1 - left = (i) => (i << 1) + 1 - right = (i) => (i + 1) << 1 - greater = (i, j) => this.comparator(this.heap[i], this.heap[j]) - swap = (i, j) => ([this.heap[i], this.heap[j]] = [this.heap[j], this.heap[i]]) - siftUp = () => { - let node = this.size() - 1 - while (node > this.top && this.greater(node, this.parent(node))) { - this.swap(node, this.parent(node)) - node = this.parent(node) - } - } - siftDown = () => { - let node = this.top - while ( - (this.left(node) < this.size() && this.greater(this.left(node), node)) || - (this.right(node) < this.size() && this.greater(this.right(node), node)) - ) { - let maxChild = - this.right(node) < this.size() && - this.greater(this.right(node), this.left(node)) - ? this.right(node) - : this.left(node) - this.swap(node, maxChild) - node = maxChild - } - } -} diff --git a/1793-maximum-score-of-a-good-subarray.js b/1793-maximum-score-of-a-good-subarray.js index 980891cc..584c8525 100644 --- a/1793-maximum-score-of-a-good-subarray.js +++ b/1793-maximum-score-of-a-good-subarray.js @@ -24,25 +24,3 @@ const maximumScore = function(nums, k) { return ans; }; -// another - -/** - * @param {number[]} nums - * @param {number} k - * @return {number} - */ -const maximumScore = function(nums, k) { - const n = nums.length, { max, min } = Math - let l = k, r = k, mi = nums[k] - let res = nums[k] - while(l > 0 || r < n - 1) { - if(l === 0) r++ - else if(r === n - 1) l-- - else if(nums[l - 1] < nums[r + 1]) r++ - else l-- - mi = min(mi, nums[l], nums[r]) - res = max(res, mi * (r - l + 1)) - } - - return res -}; diff --git a/18-4sum.js b/18-4sum.js index 069b349b..d910a721 100644 --- a/18-4sum.js +++ b/18-4sum.js @@ -42,49 +42,3 @@ function twoSum(nums, target, i, acc, results) { } -// another - -/** - * @param {number[]} nums - * @param {number} target - * @return {number[][]} - */ -const fourSum = function(nums, target) { - return nSum(nums.sort((a, b) => a - b), target, 4, 0); -}; - -function nSum(nums, target, k, start) { - const res = []; - if (nums.length < k || k < 2 || target < nums[0] * k || target > nums[-1] * k) - return res; - if (k == 2) { - // 2 sum; ( improved to O(n) ) - let r = nums.length - 1; - let l = start; - while (l < r) { - if (nums[l] + nums[r] === target) { - res.push([nums[l], nums[r]]); - //skip duplication - while (l < r && nums[l] === nums[l + 1]) l++; - while (l < r && nums[r] === nums[r - 1]) r--; - l++; - r--; - } else if (nums[l] + nums[r] < target) { - l++; - } else { - r--; - } - } - } else { - for (let i = start; i < nums.length - k + 1; i++) { - if (i === start || (i > start && nums[i] !== nums[i - 1])) { - let temp = nSum(nums, target - nums[i], k - 1, i + 1); - temp.forEach(t => { - t.push(nums[i]); - res.push(t); - }); - } - } - } - return res; -} diff --git a/1802-maximum-value-at-a-given-index-in-a-bounded-array.js b/1802-maximum-value-at-a-given-index-in-a-bounded-array.js index 64d18833..b3a438b3 100644 --- a/1802-maximum-value-at-a-given-index-in-a-bounded-array.js +++ b/1802-maximum-value-at-a-given-index-in-a-bounded-array.js @@ -23,111 +23,3 @@ const maxValue = function (n, index, maxSum) { } } -// another - - -/** - * @param {number} n - * @param {number} index - * @param {number} maxSum - * @return {number} - */ -const maxValue = function(n, index, maxSum) { - let res = 1, l = index, r = index - maxSum -= n - - while(l > 0 || r < n - 1) { - const len = r - l + 1 - if(maxSum >= len) { - maxSum -= len - res++ - } else break - if(l > 0) l-- - if(r < n - 1) r++ - } - res += ~~(maxSum / n) - - return res -} - -// another - - -/** - * @param {number} n - * @param {number} index - * @param {number} maxSum - * @return {number} - */ -const maxValue = function(n, index, maxSum) { - maxSum -= n; - let level = 1; - let left = index; - let right = index; - - while (maxSum - (right - left + 1) >= 0) { - if (left === 0 && right === n - 1) break - maxSum -= right - left + 1; - if (left - 1 >= 0) left-- - if (right + 1 <= n - 1) right++; - level++; - } - - if (maxSum) level += ~~(maxSum / n) - - return level; -} - -// another - - -/** - * @param {number} n - * @param {number} index - * @param {number} maxSum - * @return {number} - */ -const maxValue = function(n, index, maxSum) { - const { floor, sqrt } = Math - maxSum -= n - if(index < Math.floor(n / 2)) index = n - 1 - index - let left = index // number of element to the left of the index - let right = n - 1 - index // number of element to the right of the index - // the triangle area for the left side if not hitting the boundary - let leftSum = floor((left * (left + 1)) / 2) - // the triangle area for the right side if not hitting the boundary - let rightSum = floor((right * (right + 1)) / 2) - // case: perfect pyramid - if (maxSum <= (rightSum * 2 + right + 1)) return floor(sqrt(maxSum) + 1) - // case: right side hits the boundary - if (maxSum <= (leftSum + rightSum + (left - right) * right + left + 1)) { - const b = 3 + 2 * right - return floor((-b + sqrt(b * b - 8 * (rightSum + 1 - right * right - maxSum))) / 2) + 1 + 1 - } - // case: both sides hit boundaries - maxSum -= (leftSum + rightSum + (left - right) * right + left + 1) - return left + 1 + 1 + floor(maxSum / n) -}; - -// another - -/** - * @param {number} n - * @param {number} index - * @param {number} maxSum - * @return {number} - */ -const maxValue = function (n, index, maxSum) { - let ret = 0 - const { max } = Math - for (let i = 30; i >= 0; i--) { - const tmp = ret + (1 << i) - const L = max(0, tmp - index) - let sum = ((L + tmp) * (tmp - L + 1)) / 2 - const R = max(0, tmp - (n - 1 - index)) - sum += ((R + tmp) * (tmp - R + 1)) / 2 - tmp - - if (sum <= maxSum - n) ret += 1 << i - } - return ret + 1 -} diff --git a/1806-minimum-number-of-operations-to-reinitialize-a-permutation.js b/1806-minimum-number-of-operations-to-reinitialize-a-permutation.js index 3176daf4..206905bb 100644 --- a/1806-minimum-number-of-operations-to-reinitialize-a-permutation.js +++ b/1806-minimum-number-of-operations-to-reinitialize-a-permutation.js @@ -33,36 +33,3 @@ const reinitializePermutation = function(n) { } }; -// another - -/** - * @param {number} n - * @return {number} - */ -const reinitializePermutation = function(n) { - let res = 0, i = 1; - while (res === 0 || i > 1) { - i = i * 2 % (n - 1); - res++; - } - return res; -}; - -// another - -/** - * @param {number} n - * @return {number} - */ -const reinitializePermutation = function(n) { - if (n === 2) return 1 - const mod = n - 1 - let curr_power = 2 - let cnt = 1 - // Find multiplicative order modulo n-1 - while (curr_power !== 1) { - curr_power = (2 * curr_power) % mod - cnt++ - } - return cnt -}; diff --git a/1830-minimum-number-of-operations-to-make-string-sorted.js b/1830-minimum-number-of-operations-to-make-string-sorted.js index 702e0c27..e7143d4d 100644 --- a/1830-minimum-number-of-operations-to-make-string-sorted.js +++ b/1830-minimum-number-of-operations-to-make-string-sorted.js @@ -57,47 +57,3 @@ const makeStringSorted = function (s) { } -// another - - - -/** - * @param {string} s - * @return {number} - */ -const makeStringSorted = function (s) { - const mod = BigInt(10 ** 9 + 7), - n = s.length - const a = 'a'.charCodeAt(0) - let ans = 0n - const freq = Array(26).fill(0n) - for (let c of s) { - freq[c.charCodeAt(0) - a]++ - } - const fact = Array(n + 1).fill(1n) - for (let i = 1n; i <= n; i++) { - fact[i] = (fact[i - 1n] * i) % mod - } - let l = n - for (let c of s) { - l-- - let t = 0n, - rev = 1n - for (let i = 0; i < 26; i++) { - if (i < c.charCodeAt(0) - a) t += freq[i] - rev = (rev * fact[freq[i]]) % mod - } - ans += ((t * fact[l]) % mod) * modpow(rev, mod - 2n) - ans %= mod - freq[c.charCodeAt(0) - a]-- - } - return Number(ans) - function modpow(b, p) { - let ans = 1n - for (; p; p >>= 1n) { - if (p & 1n) ans = (ans * b) % mod - b = (b * b) % mod - } - return ans - } -} diff --git a/1834-single-threaded-cpu.js b/1834-single-threaded-cpu.js index 192aedec..2b67979e 100644 --- a/1834-single-threaded-cpu.js +++ b/1834-single-threaded-cpu.js @@ -105,180 +105,3 @@ class PriorityQueue { } -// another - -/** - * @param {number[][]} tasks - * @return {number[]} - */ -const getOrder = function(tasks) { - const pq = new PriorityQueue(compare), n = tasks.length - const res = [] - let time = 0, i = 0 - for(let i = 0; i < n; i++) tasks[i].push(i) - tasks.sort((a, b) => a[0] - b[0]) - - while(i < n || !pq.isEmpty()) { - if(pq.isEmpty()) { - time = Math.max(time, tasks[i][0]) - } - while(i < n && time >= tasks[i][0]) { - pq.push([tasks[i][1], tasks[i][2]]) - i++ - } - const [dur, idx] = pq.pop() - time += dur - res.push(idx) - } - - return res -}; - -function compare(a, b) { - if(a[0] < b[0]) return true - else if (a[0] > b[0]) return false - else { - return a[1] < b[1] - } -} - -class PriorityQueue { - constructor(comparator = (a, b) => a > b) { - this.heap = [] - this.top = 0 - this.comparator = comparator - } - size() { - return this.heap.length - } - isEmpty() { - return this.size() === 0 - } - peek() { - return this.heap[this.top] - } - push(...values) { - values.forEach((value) => { - this.heap.push(value) - this.siftUp() - }) - return this.size() - } - pop() { - const poppedValue = this.peek() - const bottom = this.size() - 1 - if (bottom > this.top) { - this.swap(this.top, bottom) - } - this.heap.pop() - this.siftDown() - return poppedValue - } - replace(value) { - const replacedValue = this.peek() - this.heap[this.top] = value - this.siftDown() - return replacedValue - } - - parent = (i) => ((i + 1) >>> 1) - 1 - left = (i) => (i << 1) + 1 - right = (i) => (i + 1) << 1 - greater = (i, j) => this.comparator(this.heap[i], this.heap[j]) - swap = (i, j) => ([this.heap[i], this.heap[j]] = [this.heap[j], this.heap[i]]) - siftUp = () => { - let node = this.size() - 1 - while (node > this.top && this.greater(node, this.parent(node))) { - this.swap(node, this.parent(node)) - node = this.parent(node) - } - } - siftDown = () => { - let node = this.top - while ( - (this.left(node) < this.size() && this.greater(this.left(node), node)) || - (this.right(node) < this.size() && this.greater(this.right(node), node)) - ) { - let maxChild = - this.right(node) < this.size() && - this.greater(this.right(node), this.left(node)) - ? this.right(node) - : this.left(node) - this.swap(node, maxChild) - node = maxChild - } - } -} - -// another -/** - * @param {number[][]} tasks - * @return {number[]} - */ -const getOrder = function(tasks) { - const n = tasks.length - const pq = new PriorityQueue((a, b) => a[0] === b[0] ? a[1] < b[1] : a[0] < b[0]) - tasks.forEach((e, i) => e.push(i)) - tasks.sort((a, b) => a[0] - b[0]) - let idx = 0, time = 0 - const res = [] - - while(idx < n || !pq.isEmpty()) { - while(idx < n && tasks[idx][0] <= time) { - pq.push([tasks[idx][1], task[idx][2]]) - idx++ - } - if(!pq.isEmpty()) { - const tmp = pq.pop() - time += tmp[0] - res.push(tmp[1]) - } else if(idx < n) { - time = tasks[idx][0] - } - } - return res - -}; - -// another - -/** - * @param {number[][]} tasks - * @return {number[]} - */ -const getOrder = function (tasks) { - tasks = tasks.map((e, idx) => [e[0], e[1], idx]) - tasks.sort((a, b) => a[0] - b[0]) - const pq = new PriorityQueue(compare) - const res = [] - let i = 0, - t = 0 - while (i < tasks.length) { - while (i < tasks.length && tasks[i][0] <= t) { - let [ent, pt, ind] = tasks[i] - i += 1 - pq.push([pt, ind]) - } - if (pq.size() == 0) { - if (i < tasks.length) t = tasks[i][0] - continue - } - let [pt, ind] = pq.pop() - res.push(ind) - t += pt - } - while (pq.size()) { - let [pt, index] = pq.pop() - res.push(index) - } - return res -} - -function compare(a, b) { - if (a[0] < b[0]) return true - else if (a[0] > b[0]) return false - else { - return a[1] < b[1] - } -} - diff --git a/1835-find-xor-sum-of-all-pairs-bitwise-and.js b/1835-find-xor-sum-of-all-pairs-bitwise-and.js index 795a8fcf..4d9563e3 100644 --- a/1835-find-xor-sum-of-all-pairs-bitwise-and.js +++ b/1835-find-xor-sum-of-all-pairs-bitwise-and.js @@ -12,60 +12,3 @@ const getXORSum = function(arr1, arr2) { }; -// another - -// On every bit XOR acts as modulo 2 addition and AND acts as modulo 2 multiplication. -// The set {0,1} with modulo 2 addition and multiplication is the field GF(2) and the distributive property holds in every field. - -/** - * @param {number[]} arr1 - * @param {number[]} arr2 - * @return {number} - */ -const getXORSum = function(arr1, arr2) { - const bits = Array(32).fill(0) - for (let v of arr2) { - let pos = 0; - while (v > 0) { - if (v & 1) { - bits[pos]++; - } - v = v >> 1; - pos++; - } - } - - let res = 0; - - for (let v of arr1) { - let pos = 0; - let tmp = 0; - while (v > 0) { - if (v & 1) { - if (bits[pos] % 2 == 1) { - tmp |= (1 << pos); - } - } - v = v >> 1; - pos++; - } - - res ^= tmp; - } - - return res; -}; - -// another - -/** - * @param {number[]} arr1 - * @param {number[]} arr2 - * @return {number} - */ -const getXORSum = function(arr1, arr2) { - let x1 = arr1[0], x2 = arr2[0] - for(let i = 1; i < arr1.length; i++) x1 ^= arr1[i] - for(let i = 1; i < arr2.length; i++) x2 ^= arr2[i] - return x1 & x2 -}; diff --git a/1838-frequency-of-the-most-frequent-element.js b/1838-frequency-of-the-most-frequent-element.js index b57a76f9..5706c6b2 100644 --- a/1838-frequency-of-the-most-frequent-element.js +++ b/1838-frequency-of-the-most-frequent-element.js @@ -18,23 +18,3 @@ const maxFrequency = function(nums, k) { return res }; -// another - -/** - * @param {number[]} nums - * @param {number} k - * @return {number} - */ -const maxFrequency = function(nums, k) { - nums.sort((a, b) => a - b) - let i = 0, sum = 0, res = 1 - for(let j = 0; j < nums.length; j++) { - sum += nums[j] - while(sum + k < (j - i + 1) * nums[j]) { - sum -= nums[i] - i++ - } - res = Math.max(res, j - i + 1) - } - return res -}; diff --git a/1839-longest-substring-of-all-vowels-in-order.js b/1839-longest-substring-of-all-vowels-in-order.js index d5d78f94..f5333b0d 100644 --- a/1839-longest-substring-of-all-vowels-in-order.js +++ b/1839-longest-substring-of-all-vowels-in-order.js @@ -24,61 +24,3 @@ function longestBeautifulSubstring(word) { return res } -// another - -/** - * @param {string} word - * @return {number} - */ -function longestBeautifulSubstring(word) { - let res = 0, cur = 'a', cnt = 0 - const set = new Set() - for (let ch of word) { - if (ch >= cur) { - cnt++ - cur = ch - set.add(ch) - } else { - set.clear() - cnt = 0 - cur = 'a' - if(ch === cur) { - set.add(ch) - cnt++ - } - } - if (set.size === 5) { - res = Math.max(res, cnt) - } - } - - return res -} - -// another - -/** - * @param {string} word - * @return {number} - */ -function longestBeautifulSubstring(word) { - let result = 0, - current = 0 - let currentVowel = "a" - const set = new Set() - for (let i = 0; i < word.length; i++) - if (word.charAt(i) < currentVowel) { - set.clear() - if (word.charAt(i) == "a") { - set.add("a") - current = 1 - } else current = 0 - currentVowel = "a" - } else { - current++ - currentVowel = word.charAt(i) - set.add(currentVowel) - if (set.size == 5) result = Math.max(result, current) - } - return result -} diff --git a/1849-splitting-a-string-into-descending-consecutive-values.js b/1849-splitting-a-string-into-descending-consecutive-values.js index 48e25d46..96dc1ca7 100644 --- a/1849-splitting-a-string-into-descending-consecutive-values.js +++ b/1849-splitting-a-string-into-descending-consecutive-values.js @@ -18,28 +18,3 @@ function helper(str, previous) { } -// another - - -/** - * @param {string} s - * @return {boolean} - */ -const splitString = function(s) { - return dfs(s, 0, [Infinity]) -}; - -function dfs(str, idx, arr) { - if(idx >= str.length && arr.length > 2) return true - for(let i = idx; i < str.length; i++) { - const tmp = str.slice(idx, i + 1) - const num = parseInt(tmp, 10) - const pre = arr[arr.length - 1] - if(num < pre && (pre === Infinity || pre - num === 1)) { - arr.push(num) - if(dfs(str, i + 1, arr)) return true - arr.pop() - } - } - return false -} diff --git a/1851-minimum-interval-to-include-each-query.js b/1851-minimum-interval-to-include-each-query.js index b2a0f7f3..3b0b1274 100644 --- a/1851-minimum-interval-to-include-each-query.js +++ b/1851-minimum-interval-to-include-each-query.js @@ -141,307 +141,3 @@ class BinaryHeap { } -// another - -/** - * @param {number[][]} intervals - * @param {number[]} queries - * @return {number[]} - */ -const minInterval = function (A, Q) { - const QQ = [] - for (let idx = 0; idx < Q.length; idx++) QQ.push([Q[idx], idx]) - QQ.sort((a, b) => a[0] - b[0]) - A.sort((a, b) => a[0] - b[0]) - let i = 0, - N = A.length - const ans = Array(Q.length).fill(-1) - const m = new TreeMap() - const pq = new PriorityQueue((a, b) => a[0] < b[0]) - for (let [q, index] of QQ) { - for (; i < N && A[i][0] <= q; i++) { - let len = A[i][1] - A[i][0] + 1 - if (m.get(len) == null) m.set(len, 0) - m.set(len, m.get(len) + 1) - pq.push([A[i][1], len]) - } - while (pq.size() > 0 && pq.peek()[0] < q) { - let [right, len] = pq.peek() - m.set(len, m.get(len) - 1) - if (m.get(len) === 0) m.remove(len) - pq.pop() - } - const first = m.getMinKey() - if (m.getLength()) ans[index] = first - } - return ans -} - -class PriorityQueue { - constructor(comparator = (a, b) => a > b) { - this.heap = [] - this.top = 0 - this.comparator = comparator - } - size() { - return this.heap.length - } - isEmpty() { - return this.size() === 0 - } - peek() { - return this.heap[this.top] - } - push(...values) { - values.forEach((value) => { - this.heap.push(value) - this.siftUp() - }) - return this.size() - } - pop() { - const poppedValue = this.peek() - const bottom = this.size() - 1 - if (bottom > this.top) { - this.swap(this.top, bottom) - } - this.heap.pop() - this.siftDown() - return poppedValue - } - replace(value) { - const replacedValue = this.peek() - this.heap[this.top] = value - this.siftDown() - return replacedValue - } - - parent = (i) => ((i + 1) >>> 1) - 1 - left = (i) => (i << 1) + 1 - right = (i) => (i + 1) << 1 - greater = (i, j) => this.comparator(this.heap[i], this.heap[j]) - swap = (i, j) => ([this.heap[i], this.heap[j]] = [this.heap[j], this.heap[i]]) - siftUp = () => { - let node = this.size() - 1 - while (node > this.top && this.greater(node, this.parent(node))) { - this.swap(node, this.parent(node)) - node = this.parent(node) - } - } - siftDown = () => { - let node = this.top - while ( - (this.left(node) < this.size() && this.greater(this.left(node), node)) || - (this.right(node) < this.size() && this.greater(this.right(node), node)) - ) { - let maxChild = - this.right(node) < this.size() && - this.greater(this.right(node), this.left(node)) - ? this.right(node) - : this.left(node) - this.swap(node, maxChild) - node = maxChild - } - } -} - -function TreeMap() { - var root = null - var keyType = void 0 - var length = 0 - - return { - each: each, - set: set, - get: get, - getTree: getTree, - getLength: getLength, - getMaxKey: getMaxKey, - getMinKey: getMinKey, - remove: remove, - } - - function checkKey(key, checkKeyType) { - var localKeyType = typeof key - - if ( - localKeyType !== 'number' && - localKeyType !== 'string' && - localKeyType !== 'boolean' - ) { - throw new Error("'key' must be a number, a string or a boolean") - } - - if (checkKeyType === true && localKeyType !== keyType) { - throw new Error('All keys must be of the same type') - } - - return localKeyType - } - - function call(callback) { - var args = Array.prototype.slice.call(arguments, 1) - - if (typeof callback === 'function') { - callback.apply(void 0, args) - } - } - - function getTree() { - return root - } - - function getLength() { - return length - } - - function each(callback) { - internalEach(root, callback) - } - - function internalEach(node, callback, internalCallback) { - if (node === null) { - return call(internalCallback) - } - - internalEach(node.left, callback, function () { - call(callback, node.value, node.key) - - internalEach(node.right, callback, function () { - call(internalCallback) - }) - }) - } - - function get(key) { - checkKey(key) - - return internalGet(key, root) - } - - function internalGet(key, node) { - if (node === null) { - return void 0 - } - - if (key < node.key) { - return internalGet(key, node.left) - } else if (key > node.key) { - return internalGet(key, node.right) - } else { - return node.value - } - } - - function set(key, value) { - if (root === null) { - keyType = checkKey(key) - } else { - checkKey(key, true) - } - - root = internalSet(key, value, root) - } - - function internalSet(key, value, node) { - if (node === null) { - length++ - - return { - key: key, - value: value, - left: null, - right: null, - } - } - - if (key < node.key) { - node.left = internalSet(key, value, node.left) - } else if (key > node.key) { - node.right = internalSet(key, value, node.right) - } else { - node.value = value - } - - return node - } - - function getMaxKey() { - var maxNode = getMaxNode(root) - - if (maxNode !== null) { - return maxNode.key - } - - return maxNode - } - - function getMinKey() { - var minNode = getMinNode(root) - - if (minNode !== null) { - return minNode.key - } - - return minNode - } - - function getMaxNode(node) { - while (node !== null && node.right !== null) { - node = node.right - } - - return node - } - - function getMinNode(node) { - while (node !== null && node.left !== null) { - node = node.left - } - - return node - } - - function remove(key) { - checkKey(key) - - root = internalRemove(key, root) - } - - function internalRemove(key, node) { - if (node === null) { - return null - } - - if (key < node.key) { - node.left = internalRemove(key, node.left) - } else if (key > node.key) { - node.right = internalRemove(key, node.right) - } else { - if (node.left !== null && node.right !== null) { - var maxNode = getMaxNode(node.left) - - var maxNodeKey = maxNode.key - var maxNodeValue = maxNode.value - - maxNode.key = node.key - maxNode.value = node.value - node.key = maxNodeKey - node.value = maxNodeValue - - node.left = internalRemove(key, node.left) - } else if (node.left !== null) { - length-- - return node.left - } else if (node.right !== null) { - length-- - return node.right - } else { - length-- - return null - } - } - - return node - } -} diff --git a/1854-maximum-population-year.js b/1854-maximum-population-year.js index c8a1f739..8f2699db 100644 --- a/1854-maximum-population-year.js +++ b/1854-maximum-population-year.js @@ -26,64 +26,3 @@ const maximumPopulation = function(logs) { }; -// another - -/** - * @param {number[][]} logs - * @return {number} - */ -const maximumPopulation = function(logs) { - logs.sort((a, b) => { - if(a[0] === b[0]) return a[1] - b[1] - return a[0] - b[0] - }) - const arr = Array(101).fill(0) - const bit = new FenwickTree(101) - for(let i = 0, len = logs.length; i < len; i++) { - const [start, end] = logs[i] - const idx = start - 1950 - bit.update(idx + 1, 1) - } - for(let i = 0, len = logs.length; i < len; i++) { - const [start, end] = logs[i] - const idx = end - 1950 - bit.update(idx + 1, -1) - } - let max = 0 - for(let i = 1; i <= 101; i++) { - max = Math.max(max, bit.query(i)) - } - let tmp - for(let i = 1; i <= 101; i++) { - if(bit.query(i) === max) { - tmp = i - break - } - } - - return 1950 + tmp - 1 -}; - -const lowBit = (x) => x & -x -class FenwickTree { - constructor(n) { - if (n < 1) return - this.sum = Array(n + 1).fill(0) - } - update(i, delta) { - if (i < 1) return - while (i < this.sum.length) { - this.sum[i] += delta - i += lowBit(i) - } - } - query(i) { - if (i < 1) return - let sum = 0 - while (i > 0) { - sum += this.sum[i] - i -= lowBit(i) - } - return sum - } -} diff --git a/1856-maximum-subarray-min-product.js b/1856-maximum-subarray-min-product.js index aef4a778..864b3b1a 100644 --- a/1856-maximum-subarray-min-product.js +++ b/1856-maximum-subarray-min-product.js @@ -42,99 +42,3 @@ const maxSumMinProduct = function(nums) { } }; -// another - - -/** - * @param {number[]} nums - * @return {number} - */ -const maxSumMinProduct = function (nums) { - const n = nums.length - const mod = BigInt(10 ** 9 + 7) - const preSum = Array(n + 1).fill(0n) - for (let i = 0; i < n; i++) { - preSum[i + 1] = preSum[i] + BigInt(nums[i]) - } - const l = Array(n).fill(0) // l[i] stores index of farthest element greater or equal to nums[i] - const r = Array(n).fill(0) // r[i] stores index of farthest element greater or equal to nums[i] - let st = [] - - for (let i = 0; i < n; i++) { - while (st.length && nums[st[st.length - 1]] >= nums[i]) st.pop() - if (st.length) l[i] = st[st.length - 1] + 1 - else l[i] = 0 - st.push(i) - } - - st = [] - for (let i = n - 1; i >= 0; i--) { - while (st.length && nums[st[st.length - 1]] >= nums[i]) st.pop() - if (st.length) r[i] = st[st.length - 1] - 1 - else r[i] = n - 1 - st.push(i) - } - function getSum(left, right) { - // inclusive - return preSum[right + 1] - preSum[left] - } - - let maxProduct = 0n - for (let i = 0; i < n; i++) { - maxProduct = bigint_max(maxProduct, BigInt(nums[i]) * getSum(l[i], r[i])) - } - return maxProduct % mod -} -function bigint_max(...args){ - if (args.length < 1){ throw 'Max of empty list'; } - m = args[0]; - args.forEach(a=>{if (a > m) {m = a}}); - return m; -} - -// another - -/** - * @param {number[]} nums - * @return {number} - */ -const maxSumMinProduct = function(nums) { - const n = nums.length, s1 = [], s2 = [], - left = Array(n), right = Array(n), mod = BigInt(1e9 + 7) - for(let i = 0; i < n; i++) { - while(s1.length && nums[s1[s1.length - 1]] >= nums[i]) s1.pop() - if(s1.length) left[i] = s1[s1.length - 1] + 1 - else left[i] = 0 - s1.push(i) - } - - for(let i = n - 1; i >= 0; i--) { - while(s2.length && nums[s2[s2.length - 1]] >= nums[i]) s2.pop() - if(s2.length) right[i] = s2[s2.length - 1] - 1 - else right[i] = n - 1 - s2.push(i) - } - - const preSum = Array(n) - for(let i = 0; i < n; i++) { - preSum[i] = (i === 0 ? 0n : preSum[i - 1]) + BigInt(nums[i]) - } - let res = 0n - for(let i = 0; i < n; i++) { - res = max(res, getSum(preSum, left[i], right[i]) * BigInt(nums[i])) - } - return res % mod - -}; - -function getSum(arr, l, r) { - return arr[r] - (l === 0 ? 0n : arr[l - 1]) -} - -function max(...args) { - let res = -Infinity - for(let e of args) { - if(e > res) res = e - } - return res -} diff --git a/1857-largest-color-value-in-a-directed-graph.js b/1857-largest-color-value-in-a-directed-graph.js index ed99ed48..0264954e 100644 --- a/1857-largest-color-value-in-a-directed-graph.js +++ b/1857-largest-color-value-in-a-directed-graph.js @@ -43,93 +43,3 @@ const largestPathValue = function (colors, edges) { return seen < colors.length ? -1 : res } -// another - -/** - * @param {string} colors - * @param {number[][]} edges - * @return {number} - */ - const largestPathValue = function(colors, edges) { - const graph = {}, n = colors.length, a = 'a'.charCodeAt(0) - const indegree = Array(n).fill(0) - for (const [from, to] of edges) { - if (graph[from] == null) graph[from] = [] - graph[from].push(to) - indegree[to]++ - } - const cnt = Array.from({ length: n }, () => Array(26).fill(0)) - const code = idx => colors.charCodeAt(idx) - a - const q = [] - for (let i = 0; i < n; i++) { - if(indegree[i] === 0) { - q.push(i) - cnt[i][code(i)] = 1 - } - } - let res = 0, seen = 0 - - while(q.length) { - const u = q.pop() - const val = cnt[u][code(u)] - res = Math.max(res, val) - seen++ - for(const next of (graph[u] || [])) { - for(let i = 0; i < 26; i++) { - cnt[next][i] = Math.max(cnt[next][i], cnt[u][i] + (i === code(next) ? 1 : 0)) - } - if(--indegree[next] === 0) { - q.push(next) - } - } - } - return seen < n ? -1 : res -}; - -// another - - -/** - * @param {string} colors - * @param {number[][]} edges - * @return {number} - */ -const largestPathValue = function (colors, edges) { - const graph = {} - const n = colors.length, - a = 'a'.charCodeAt(0) - const indegree = Array(colors.length).fill(0) - for (let e of edges) { - if (graph[e[0]] == null) graph[e[0]] = [] - graph[e[0]].push(e[1]) - indegree[e[1]]++ - } - const cnt = Array.from({ length: n }, () => Array(26).fill(0)) - const q = [] - for (let i = 0; i < n; i++) { - if (indegree[i] === 0) { - q.push(i) - cnt[i][colors.charCodeAt(i) - a] = 1 - } - } - let res = 0, - seen = 0 - while (q.length) { - const u = q[0] - q.shift() - const val = Math.max(...cnt[u]) - res = Math.max(res, val) - seen++ - if (graph[u] == null) continue - for (let v of graph[u]) { - for (let i = 0; i < 26; i++) { - cnt[v][i] = Math.max( - cnt[v][i], - cnt[u][i] + (i === colors.charCodeAt(v) - a) - ) - } - if (--indegree[v] === 0) q.push(v) - } - } - return seen < colors.length ? -1 : res -} diff --git a/1861-rotating-the-box.js b/1861-rotating-the-box.js index dcd137ee..ebf310c8 100644 --- a/1861-rotating-the-box.js +++ b/1861-rotating-the-box.js @@ -20,31 +20,3 @@ const rotateTheBox = function(box) { return res }; -// another - -/** - * @param {character[][]} box - * @return {character[][]} - */ -const rotateTheBox = function(box) { - const m = box.length, n = box[0].length - const res = Array.from({ length: n }, () => Array(m).fill('.')) - for(let i = 0; i < m; i++) { - let j = n - 1 - let pos = j - while(j >= 0) { - if(box[i][j] === '*') { - pos = j - 1 - } else if(box[i][j] === '#') { - box[i][j] = '.' - box[i][pos] = '#' - res[pos][m - 1 - i] = '#' - pos-- - } - res[j][m - 1 - i] = box[i][j] - j-- - } - - } - return res -}; diff --git a/1866-number-of-ways-to-rearrange-sticks-with-k-sticks-visible.js b/1866-number-of-ways-to-rearrange-sticks-with-k-sticks-visible.js index e652838e..006e995b 100644 --- a/1866-number-of-ways-to-rearrange-sticks-with-k-sticks-visible.js +++ b/1866-number-of-ways-to-rearrange-sticks-with-k-sticks-visible.js @@ -15,27 +15,3 @@ const rearrangeSticks = function(n, k) { return g[n][k] }; -// another - -/** - * @param {number} n - * @param {number} k - * @return {number} - */ -const rearrangeSticks = function(n, k) { - const MOD = 1e9 + 7; - // first # can be smallest # in which case we recurse for (n - 1, k - 1) - // or it can not be and smallest can be in any of n - 1 otehr positions for recursed(n - 1, k) - const dp = new Array(n + 1).fill().map( _ => new Array(k + 1).fill(0) ); - for (let i = 1; i <= n; ++i) { - for (let j = 1; j <= k; ++j) { - if (j === i) { - dp[i][j] = 1; - } else if (j < i) { - dp[i][j] = (dp[i - 1][j - 1] + (i - 1) * dp[i - 1][j]) % MOD; - } - } - } - return dp[n][k] % MOD; - -}; diff --git a/187-repeated-dna-sequences.js b/187-repeated-dna-sequences.js index 773abb02..8b4fee8e 100644 --- a/187-repeated-dna-sequences.js +++ b/187-repeated-dna-sequences.js @@ -29,21 +29,3 @@ const findRepeatedDnaSequences = function(s) { return Array.from(repeated); }; -// another - -/** - * @param {string} s - * @return {string[]} - */ -const findRepeatedDnaSequences = function(s) { - let store = new Set(), result = new Set() - for(let i = 0; i < s.length - 9; i++) { - const str = s.substring(i, i + 10) - if(store.has(str)) { - result.add(str) - } else { - store.add(str) - } - } - return Array.from(result) -}; diff --git a/1870-minimum-speed-to-arrive-on-time.js b/1870-minimum-speed-to-arrive-on-time.js index 9eebca9b..a4942fd3 100644 --- a/1870-minimum-speed-to-arrive-on-time.js +++ b/1870-minimum-speed-to-arrive-on-time.js @@ -16,87 +16,3 @@ const minSpeedOnTime = function(dist, hour) { return l > 1e7 ? -1 : l }; -// another - -/** - * @param {number[]} dist - * @param {number} hour - * @return {number} - */ -const minSpeedOnTime = function(dist, hour) { - let l = 1, r = 1e7 - while(l <= r) { - let mid = (l + r) >> 1 - if(valid(mid)) r = mid -1 - else l = mid + 1 - } - return l > 1e7 ? -1 : l - - function valid(speed) { - let sum = 0 - for(let e of dist) { - sum = Math.ceil(sum) - sum += e / speed - if(sum > hour) return - } - - return true - } -}; - -// another - -/** - * @param {number[]} dist - * @param {number} hour - * @return {number} - */ -const minSpeedOnTime = function(dist, hour) { - const sum = dist.reduce((ac, e) => ac + e, 0) - let l = 1, r = 10 ** 7 - while(l < r) { - let mid = l + ((r - l) >> 1) - if(chk(mid)) r = mid - else l = mid + 1 - } - - return chk(l) ? l : -1 - - function chk(speed) { - let res = 0 - for(let i = 0, len = dist.length; i < len - 1; i++) { - res += Math.ceil(dist[i] / speed) - } - if (dist.length) res += dist[dist.length - 1] / speed - return res <= hour - } - -}; - -// another - -/** - * @param {number[]} dist - * @param {number} hour - * @return {number} - */ -const minSpeedOnTime = function(dist, hour) { - let l = 1, r = 1e7 - while(l < r) { - const mid = l + Math.floor((r - l) / 2) - if(!valid(mid)) l = mid + 1 - else r = mid - } - // console.log(l) - return valid(l) ? l : -1 - - function valid(mid) { - let res = 0 - for(let i = 0, n = dist.length; i < n; i++) { - const d = dist[i] - res += (i === n - 1 ? d / mid : Math.ceil(d / mid)) - } - return res <= hour - } -}; - diff --git a/1871-jump-game-vii.js b/1871-jump-game-vii.js index 55fd6fcf..eccd6f10 100644 --- a/1871-jump-game-vii.js +++ b/1871-jump-game-vii.js @@ -24,76 +24,3 @@ const canReach = function(s, minJump, maxJump) { }; -// another - -/** - * @param {string} s - * @param {number} minJump - * @param {number} maxJump - * @return {boolean} - */ -const canReach = function(s, minJump, maxJump) { - let n = s.length; - const {max, min} = Math - if (s[n - 1] != '0') return false; - const pre_sum = Array(n + 1).fill(0); - const check = Array(n + 1).fill(0); - check[1] = 1; - pre_sum[1] = 1; - for (let i = 1; i < n; i++) { - pre_sum[i + 1] = pre_sum[i]; - if (s[i] == '1') continue; - if (i < minJump) continue; - let r = i - minJump; - let l = max(0, i - maxJump); - if (pre_sum[r + 1] - pre_sum[l] == 0) continue; - check[i + 1] = true; - pre_sum[i + 1]++; - } - return check[n]; -}; - -// another - -/** - * @param {string} s - * @param {number} minJump - * @param {number} maxJump - * @return {boolean} - */ -const canReach = function(s, minJump, maxJump) { - const n = s.length, dp = Array(n).fill(0) - dp[0] = 1 - let pre = 0 - for(let i = 1; i < n; i++) { - if(i >= minJump) { - pre += dp[i - minJump] - } - if(i > maxJump) pre -= dp[i - maxJump - 1] - dp[i] = pre > 0 && s[i] === '0' ? 1 : 0 - } - return dp[n - 1] -}; - -// another - -/** - * @param {string} s - * @param {number} minJump - * @param {number} maxJump - * @return {boolean} - */ -const canReach = function(s, minJump, maxJump) { - const n = s.length - const dp = Array(n).fill(0) - dp[0] = 1 - let pre = 0 - for(let i = 1; i < n; i++) { - if(i < minJump) continue - if(i >= minJump) pre += dp[i - minJump] - if(i > maxJump) pre -= dp[i - maxJump - 1] - dp[i] = pre > 0 && s[i] === '0' ? 1 : 0 - } - - return dp[n - 1] ? true : false -}; diff --git a/1879-minimum-xor-sum-of-two-arrays.js b/1879-minimum-xor-sum-of-two-arrays.js index 31c752c9..5c135094 100644 --- a/1879-minimum-xor-sum-of-two-arrays.js +++ b/1879-minimum-xor-sum-of-two-arrays.js @@ -32,98 +32,3 @@ function bitCnt(num) { } -// another - -/** - * @param {number[]} nums1 - * @param {number[]} nums2 - * @return {number} - */ -const minimumXORSum = function (nums1, nums2) { - const n = nums1.length, dp = Array(1 << n).fill(Infinity) - return dfs(0, 0) - function dfs(i, mask) { - if(i === n) return 0 - if(dp[mask] !== Infinity) return dp[mask] - for(let j = 0; j < n; j++) { - if((mask & (1 << j)) === 0) { - dp[mask] = Math.min( - dp[mask], - (nums1[i] ^ nums2[j]) + dfs(i + 1, mask | (1 << j)) - ) - } - } - return dp[mask] - } -} - -// another - -/** - * @param {number[]} nums1 - * @param {number[]} nums2 - * @return {number} - */ -const minimumXORSum = function(nums1, nums2) { - const dp = Array(1 << nums2.length).fill(Infinity) - return dfs(dp, nums1, nums2, 0, 0) -}; - -function dfs(dp, a, b, i, mask) { - if(i >= a.length) return 0 - if(dp[mask] === Infinity) { - for(let j = 0; j < b.length; j++) { - if((mask & (1 << j)) === 0) { - dp[mask] = Math.min(dp[mask], (a[i] ^ b[j]) + dfs(dp, a, b, i + 1, mask + (1 << j))) - } - } - } - return dp[mask] -} - -// another - -/** - * @param {number[]} nums1 - * @param {number[]} nums2 - * @return {number} - */ -const minimumXORSum = function (nums1, nums2) { - const dp = Array(1 << nums2.length).fill(Infinity) - return dfs(0, 0) - - function dfs(i, mask) { - if(i >= nums2.length) return 0 - if(dp[mask] != Infinity) return dp[mask] - for(let j = 0; j < nums2.length; j++) { - if((mask & (1 << j)) === 0) { - dp[mask] = Math.min(dp[mask], (nums1[i] ^ nums2[j]) + dfs(i + 1, mask + (1 << j)) ) - } - } - return dp[mask] - } -} - -// another - -/** - * @param {number[]} nums1 - * @param {number[]} nums2 - * @return {number} - */ -const minimumXORSum = function (nums1, nums2) { - const n = nums1.length, dp = Array(1 << n).fill(Infinity) - return dfs(0, 0) - - function dfs(i, mask) { - if(i >= n) return 0 - if(dp[mask] !== Infinity) return dp[mask] - for(let j = 0; j < n; j++) { - if((mask & (1 << j)) === 0) { - dp[mask] = Math.min(dp[mask], (nums1[i] ^ nums2[j]) + dfs(i + 1, mask | (1 << j))) - } - } - return dp[mask] - } -} - diff --git a/188-best-time-to-buy-and-sell-stock-iv.js b/188-best-time-to-buy-and-sell-stock-iv.js index 35de229f..3d7cc709 100644 --- a/188-best-time-to-buy-and-sell-stock-iv.js +++ b/188-best-time-to-buy-and-sell-stock-iv.js @@ -25,64 +25,3 @@ const maxProfit = function(k, prices) { return sell[k] } -// another - -/** - * @param {number} k - * @param {number[]} prices - * @return {number} - */ -const maxProfit = function(k, prices) { - if(prices.length === 0) return 0 - - if(k > (prices.length/2)) { - let profit = 0 - for(let i = 1; i < prices.length; i++) { - if(prices[i] > prices[i-1]) profit += prices[i] - prices[i-1] - } - return profit - } else { - let dp = new Array(prices.length).fill(0) - let length = prices.length - for(let j = 0; j < k; j++) { - let min = prices[0], max = 0 - for(let i = 0; i < length; i++) { - min = Math.min(min, prices[i] - dp[i]) - max = Math.max(max, prices[i] - min) - dp[i] = max - } - } - return dp.pop() - } -} - - // another - -/** - * @param {number} k - * @param {number[]} prices - * @return {number} - */ -const maxProfit = function(k, prices) { - if (k >= prices.length / 2) { - let max = 0; - for(let i = 1; i < prices.length; i++) { - if (prices[i] > prices[i - 1]) { - max += prices[i] - prices[i - 1]; - } - } - return max; - } - if (prices.length === 0) return 0; - let dp = new Array(k + 1); - dp[0] = new Array(prices.length).fill(0); - for (let t = 1; t <= k; t++) { - dp[t] = [0]; - let max = dp[t - 1][0] - prices[0]; - for (let day = 1; day < prices.length; day++) { - dp[t][day] = Math.max(dp[t][day - 1], max + prices[day]); - max = Math.max(max, dp[t - 1][day] - prices[day]); - } - } - return dp[k][prices.length - 1]; -} diff --git a/1882-process-tasks-using-servers.js b/1882-process-tasks-using-servers.js index db62a07e..a52d9d6d 100644 --- a/1882-process-tasks-using-servers.js +++ b/1882-process-tasks-using-servers.js @@ -105,330 +105,3 @@ class PQ { } } -// another - -/** - * @param {number[]} servers - * @param {number[]} tasks - * @return {number[]} - */ -const assignTasks = function (servers, tasks) { - const available = new PriorityQueue((a, b) => - a[0] === b[0] ? a[1] < b[1] : a[0] < b[0] - ) - const occupied = new PriorityQueue((a, b) => - a[0] === b[0] ? (a[1] === b[1] ? a[2] < b[2] : a[1] < b[1]) : a[0] < b[0] - ) - - const res = [], - m = tasks.length, - n = servers.length - for (let i = 0; i < n; i++) { - const w = servers[i] - available.push([w, i]) - } - let now = 0 - for (let i = 0; i < m; i++) { - const t = tasks[i] - - while (!occupied.isEmpty() && occupied.peek()[0] <= now) { - const [end, weight, index] = occupied.pop() - available.push([weight, index]) - } - - let idx - if (!available.isEmpty()) { - const [weight, index] = available.pop() - idx = index - occupied.push([now + t, weight, index]) - if(i >= now) now++ - } else { - let [endTime, weight, index] = occupied.pop() - idx = index - occupied.push([endTime + t, weight, index]) - now = endTime - } - - res.push(idx) - } - - return res -} - -class PriorityQueue { - constructor(comparator = (a, b) => a > b) { - this.heap = [] - this.top = 0 - this.comparator = comparator - } - size() { - return this.heap.length - } - isEmpty() { - return this.size() === 0 - } - peek() { - return this.heap[this.top] - } - push(...values) { - values.forEach((value) => { - this.heap.push(value) - this.siftUp() - }) - return this.size() - } - pop() { - const poppedValue = this.peek() - const bottom = this.size() - 1 - if (bottom > this.top) { - this.swap(this.top, bottom) - } - this.heap.pop() - this.siftDown() - return poppedValue - } - replace(value) { - const replacedValue = this.peek() - this.heap[this.top] = value - this.siftDown() - return replacedValue - } - - parent = (i) => ((i + 1) >>> 1) - 1 - left = (i) => (i << 1) + 1 - right = (i) => (i + 1) << 1 - greater = (i, j) => this.comparator(this.heap[i], this.heap[j]) - swap = (i, j) => ([this.heap[i], this.heap[j]] = [this.heap[j], this.heap[i]]) - siftUp = () => { - let node = this.size() - 1 - while (node > this.top && this.greater(node, this.parent(node))) { - this.swap(node, this.parent(node)) - node = this.parent(node) - } - } - siftDown = () => { - let node = this.top - while ( - (this.left(node) < this.size() && this.greater(this.left(node), node)) || - (this.right(node) < this.size() && this.greater(this.right(node), node)) - ) { - let maxChild = - this.right(node) < this.size() && - this.greater(this.right(node), this.left(node)) - ? this.right(node) - : this.left(node) - this.swap(node, maxChild) - node = maxChild - } - } -} - -// another - - -/** - * @param {number[]} servers - * @param {number[]} tasks - * @return {number[]} - */ -const assignTasks = function(servers, tasks) { - const freePQ = new PriorityQueue((a, b) => a.w === b.w ? a.i < b.i : a.w < b.w) - const runningPQ = new PriorityQueue((a, b) => a.e === b.e ? (a.w === b.w ? a.i < b.i : a.w < b.w) : a.e < b.e) - const m = servers.length, n = tasks.length - for(let i = 0; i < m; i++) freePQ.push({w: servers[i], i, e: 0}) - const res = [] - for(let i = 0; i < n; i++) { - const cur = tasks[i] - while(!runningPQ.isEmpty() && runningPQ.peek().e <= i) { - const tmp = runningPQ.pop() - tmp.e = i - freePQ.push(tmp) - } - if(freePQ.isEmpty()) { - const tmp = runningPQ.pop() - res[i] = tmp.i - tmp.e += cur - runningPQ.push(tmp) - } else { - const tmp = freePQ.pop() - res[i] = tmp.i - tmp.e = i + cur - runningPQ.push(tmp) - } - } - return res -}; - -// another - -/** - * @param {number[]} servers - * @param {number[]} tasks - * @return {number[]} - */ -const assignTasks = function(servers, tasks) { - let i = 0 - const freePQ = new PriorityQueue((a, b) => { - if(a.w < b.w) return true - else if(a.w > b.w) return false - else { - if(a.idx < b.idx) return true - return false - } - }) - const runningPQ = new PriorityQueue((a, b) => { - return a.end < b.end - }) - const res = [] - for(let i = 0; i < servers.length; i++) { - freePQ.push({ - w: servers[i], - idx: i - }) - } - let taskIdx = 0 - while(taskIdx < tasks.length) { - while(!runningPQ.isEmpty() && runningPQ.peek().end <= i) { - let server = runningPQ.pop() - freePQ.push({ - w: server.w, - idx: server.idx - }) - } - - while(taskIdx <= i && !freePQ.isEmpty() && taskIdx < tasks.length) { - const server = freePQ.pop() - res[taskIdx] = server.idx - runningPQ.push({ - end: i + tasks[taskIdx], - w: server.w, - idx: server.idx - }) - taskIdx++ - } - if(i < tasks.length || !freePQ.isEmpty()) i++ - else i = Math.max(i + 1, runningPQ.peek().end) - } - return res -}; - -class PriorityQueue { - constructor(comparator = (a, b) => a > b) { - this.heap = [] - this.top = 0 - this.comparator = comparator - } - size() { - return this.heap.length - } - isEmpty() { - return this.size() === 0 - } - peek() { - return this.heap[this.top] - } - push(...values) { - values.forEach((value) => { - this.heap.push(value) - this.siftUp() - }) - return this.size() - } - pop() { - const poppedValue = this.peek() - const bottom = this.size() - 1 - if (bottom > this.top) { - this.swap(this.top, bottom) - } - this.heap.pop() - this.siftDown() - return poppedValue - } - replace(value) { - const replacedValue = this.peek() - this.heap[this.top] = value - this.siftDown() - return replacedValue - } - - parent = (i) => ((i + 1) >>> 1) - 1 - left = (i) => (i << 1) + 1 - right = (i) => (i + 1) << 1 - greater = (i, j) => this.comparator(this.heap[i], this.heap[j]) - swap = (i, j) => ([this.heap[i], this.heap[j]] = [this.heap[j], this.heap[i]]) - siftUp = () => { - let node = this.size() - 1 - while (node > this.top && this.greater(node, this.parent(node))) { - this.swap(node, this.parent(node)) - node = this.parent(node) - } - } - siftDown = () => { - let node = this.top - while ( - (this.left(node) < this.size() && this.greater(this.left(node), node)) || - (this.right(node) < this.size() && this.greater(this.right(node), node)) - ) { - let maxChild = - this.right(node) < this.size() && - this.greater(this.right(node), this.left(node)) - ? this.right(node) - : this.left(node) - this.swap(node, maxChild) - node = maxChild - } - } -} - -// another - -/** - * @param {number[]} servers - * @param {number[]} tasks - * @return {number[]} - */ -const assignTasks = function(servers, tasks) { - const freePQ = new PriorityQueue((a, b) => { - if(a.w < b.w) return true - else if(a.w > b.w) return false - else { - if(a.idx < b.idx) return true - return false - } - }) - const runningPQ = new PriorityQueue((a, b) => { - return a.end === b.end ? (a.w === b.w ? a.idx < b.idx : a.w < b.w) : a.end < b.end - }) - const res = [] - for(let i = 0; i < servers.length; i++) { - freePQ.push({ - w: servers[i], - idx: i - }) - } - for(let i = 0, n = tasks.length; i < n; i++) { - const cur = tasks[i] - while(runningPQ.size() && runningPQ.peek().end <= i) { - const el = runningPQ.pop() - freePQ.push({ - w: el.w, - idx: el.idx, - }) - } - - if(freePQ.isEmpty()) { - const el = runningPQ.pop() - res[i] = el.idx - el.end += cur - runningPQ.push(el) - } else { - const el = freePQ.pop() - res[i] = el.idx - el.end = i + cur - runningPQ.push(el) - } - } - - return res -}; - diff --git a/1888-minimum-number-of-flips-to-make-the-binary-string-alternating.js b/1888-minimum-number-of-flips-to-make-the-binary-string-alternating.js index 0255d212..fabf8ae1 100644 --- a/1888-minimum-number-of-flips-to-make-the-binary-string-alternating.js +++ b/1888-minimum-number-of-flips-to-make-the-binary-string-alternating.js @@ -46,70 +46,3 @@ const minFlips = function (s) { return ans } -// another - -/** - * @param {string} s - * @return {number} - */ -const minFlips = function (s) { - const n = s.length - s += s - let s1 = '', s2 = '' - for(let i = 0;i < s.length; i++) { - s1 += i % 2 === 0 ? '0' : '1' - s2 += i % 2 === 0 ? '1' : '0' - } - let res1 = 0, res2 = 0, res = Infinity - for(let i = 0; i < s.length; i++) { - if(s1[i] !== s[i]) res1++ - if(s2[i] !== s[i]) res2++ - if(i >= n) { - if(s1[i - n] !== s[i - n]) res1-- - if(s2[i - n] !== s[i - n]) res2-- - } - if(i >= n - 1) { - res = Math.min(res, res1, res2) - } - } - - return res -} - -// another - -/** - * @param {string} s - * @return {number} - */ -const minFlips = function (s) { - const n = s.length - const ss = s + s - let s1 = '', s2 = '' - for(let i = 0; i < 2 * n; i++) { - if(i % 2 === 0) { - s1 += '0' - s2 += '1' - }else{ - s1 += '1' - s2 += '0' - } - } - - let res = Infinity, res1 = 0, res2 = 0 - - for (let i = 0; i < 2 * n; i++) { - if(ss[i] !== s1[i]) res1++ - if(ss[i] !== s2[i]) res2++ - if(i >= n) { - if(ss[i - n] !== s1[i - n]) res1-- - if(ss[i - n] !== s2[i - n]) res2-- - } - if(i >= n - 1) { - res = Math.min(res, res1, res2) - } - } - - return res -} - diff --git a/1889-minimum-space-wasted-from-packaging.js b/1889-minimum-space-wasted-from-packaging.js index 1e733c58..f93455b1 100644 --- a/1889-minimum-space-wasted-from-packaging.js +++ b/1889-minimum-space-wasted-from-packaging.js @@ -50,54 +50,3 @@ const minWastedSpace = function (packages, boxes) { return res === Infinity ? -1 : res % mod } -// another - -/** - * @param {number[]} packages - * @param {number[][]} boxes - * @return {number} - */ -var minWastedSpace = function (packages, boxes) { - packages.sort(function (a, b) { - return a - b - }) - let count = 0, - b, - wastage, - minWastage = Number.MAX_SAFE_INTEGER, - flag = false - for (let i = 0; i < boxes.length; i++) { - boxes[i].sort(function (a, b) { - return a - b - }) - b = 0 - wastage = 0 - count = 0 - if (boxes[i][boxes[i].length - 1] < packages[packages.length - 1]) { - //This supplier's largest box is smaller than our larget package, this supplier can't be used - continue - } - while (count < packages.length && b < boxes[i].length) { - if (packages[count] <= boxes[i][b]) { - wastage += boxes[i][b] - packages[count] - if (wastage > minWastage) { - //Need not to porcess this supplier if wastage has already been more than minWastage - break - } - count++ - } else { - b++ - } - } - if (count === packages.length) { - flag = true //We have found atleas 1 answer - if (wastage < minWastage) { - minWastage = wastage - } - } - } - if (flag === false) { - return -1 - } - return minWastage % 1000000007 -} diff --git a/189-rotate-array.js b/189-rotate-array.js index f6017a19..966d7153 100644 --- a/189-rotate-array.js +++ b/189-rotate-array.js @@ -7,31 +7,3 @@ const rotate = function(nums, k) { nums.unshift(...nums.splice(nums.length - k)) }; -// another - -const rotate = function(nums, k) { - let i = k - while (i > 0) { - nums.unshift(nums.pop()) - i-- - } -}; - -// another - -const rotate = function(nums, k) { - k %= nums.length - reverse(nums, 0, nums.length - 1) - reverse(nums, 0, k - 1) - reverse(nums, k, nums.length - 1) -} - -function reverse(nums, start, end) { - while (start < end) { - var temp = nums[start] - nums[start] = nums[end] - nums[end] = temp - start++ - end-- - } -} diff --git a/1893-check-if-all-the-integers-in-a-range-are-covered.js b/1893-check-if-all-the-integers-in-a-range-are-covered.js index 34d23efe..42b016cc 100644 --- a/1893-check-if-all-the-integers-in-a-range-are-covered.js +++ b/1893-check-if-all-the-integers-in-a-range-are-covered.js @@ -20,46 +20,3 @@ const isCovered = function(ranges, left, right) { return true }; -// another - -/** - * @param {number[][]} ranges - * @param {number} left - * @param {number} right - * @return {boolean} - */ -const isCovered = function(ranges, left, right) { - for(let i = left; i <= right; i++) { - let seen = false; - for(let j = 0; j < ranges.length && !seen; j++) - if(i >= ranges[j][0] && i <= ranges[j][1]) - seen = true; - if(!seen) return false; - } - return true; -}; - -// another - -/** - * @param {number[][]} ranges - * @param {number} left - * @param {number} right - * @return {boolean} - */ -const isCovered = function(ranges, left, right) { - const arr = Array(52).fill(0) - for(let [s, e] of ranges) { - arr[s]++ - arr[e + 1]-- - } - - let overlaps = 0 - for(let i = 1; i <= right; i++) { - overlaps += arr[i]; - if (i >= left && overlaps == 0) return false; - } - - return true -}; - diff --git a/1898-maximum-number-of-removable-characters.js b/1898-maximum-number-of-removable-characters.js index 043796cc..3db210f2 100644 --- a/1898-maximum-number-of-removable-characters.js +++ b/1898-maximum-number-of-removable-characters.js @@ -33,33 +33,3 @@ function is_valid(s, p, removable, cnt) { return ind2 == len2 } -// another - -/** - * @param {string} s - * @param {string} p - * @param {number[]} removable - * @return {number} - */ -const maximumRemovals = function(s, p, removable) { - let l = 0, r = removable.length - while(l < r) { - const mid = r - Math.floor((r - l) / 2) - if(valid(mid)) l = mid - else r = mid - 1 - } - return l - - function valid(mid) { - let arr = s.split('') - for (let i = 0; i < mid; i++) arr[removable[i]] = null - arr = arr.filter(e => e !== null) - - for(let i = 0, j = 0; i < arr.length && j < p.length;) { - if(arr[i] === p[j]) i++, j++ - else i++ - if(j === p.length) return true - } - return false - } -}; diff --git a/190-reverse-bits.js b/190-reverse-bits.js index afa6c0b6..45bc116c 100644 --- a/190-reverse-bits.js +++ b/190-reverse-bits.js @@ -16,34 +16,3 @@ const reverseBits = function(n) { return r >>> 0; }; -// another - -/** - * @param {number} n - a positive integer - * @return {number} - a positive integer - */ -const reverseBits = function(n) { - let s = ''; - let count = 0; - let index = 31; - while (n > 0) { - if (n % 2 !== 0) count += Math.pow(2, index); - index--; - n = Math.floor(n / 2); - } - return count; -}; - -// another - -/** - * @param {number} n - a positive integer - * @return {number} - a positive integer - */ -const reverseBits = function(n) { - const b = n.toString(2) - const leadingZeroes = b.padStart(32,'0') - const rev = leadingZeroes.split('') - rev.reverse() - return parseInt(rev.join(''), 2) -}; diff --git a/1904-the-number-of-full-rounds-you-have-played.js b/1904-the-number-of-full-rounds-you-have-played.js index d53993e3..3fa8845a 100644 --- a/1904-the-number-of-full-rounds-you-have-played.js +++ b/1904-the-number-of-full-rounds-you-have-played.js @@ -10,36 +10,3 @@ const numberOfRounds = function(startTime, finishTime) { return Math.max(0, Math.floor(finish / 15) - Math.ceil(start / 15)); // floor(finish / 15) - ceil(start / 15) }; -// another - -/** - * @param {string} startTime - * @param {string} finishTime - * @return {number} - */ -const numberOfRounds = function(startTime, finishTime) { - const { ceil, floor } = Math - const start = new Node(startTime), finish = new Node(finishTime) - if(finish.compare(start)) finish.hour += 24 - let cnt = 0 - if(start.hour === finish.hour) { - const r = floor(finish.minute / 15) - const l = ceil(start.minute / 15) - if(l >= r) return 0 - return r - l - } - cnt += 4 - ceil(start.minute / 15) + floor(finish.minute / 15) - start.hour++ - cnt += (finish.hour - start.hour) * 4 - return cnt -}; - -class Node { - constructor(str) { - this.hour = +str.slice(0, 2) - this.minute = +str.slice(3) - } - compare(node) { - return this.hour === node.hour ? this.minute < node.minute : this.hour < node.hour - } -} diff --git a/191-number-of-1-bits.js b/191-number-of-1-bits.js index 8395aae6..8cfa40f2 100644 --- a/191-number-of-1-bits.js +++ b/191-number-of-1-bits.js @@ -11,17 +11,3 @@ const hammingWeight = function(n) { return res }; -// another - -/** - * @param {number} n - a positive integer - * @return {number} - */ -const hammingWeight = function(n) { - const str = (n >>> 0).toString(2) - let res = 0 - for(let c of str) { - if(c === '1') res++ - } - return res -}; diff --git a/1911-maximum-alternating-subsequence-sum.js b/1911-maximum-alternating-subsequence-sum.js index 555eefb8..f882a39c 100644 --- a/1911-maximum-alternating-subsequence-sum.js +++ b/1911-maximum-alternating-subsequence-sum.js @@ -11,16 +11,3 @@ const maxAlternatingSum = function(nums) { return even; }; -// another - -/** - * @param {number[]} nums - * @return {number} - */ -const maxAlternatingSum = function(nums) { - let res = nums[0] - for(let i = 1; i < nums.length; i++) { - res += Math.max(nums[i] - nums[i - 1], 0) - } - return res -}; diff --git a/1914-cyclically-rotating-a-gridsubmissions.js b/1914-cyclically-rotating-a-gridsubmissions.js index 056a22c3..9f4f80b9 100644 --- a/1914-cyclically-rotating-a-gridsubmissions.js +++ b/1914-cyclically-rotating-a-gridsubmissions.js @@ -38,70 +38,3 @@ const rotateGrid = function(grid, k) { return grid }; -// another - -/** - * @param {number[][]} grid - * @param {number} k - * @return {number[][]} - */ -var rotateGrid = function(grid, k) { - var m = grid.length; - var n = grid[0].length; - - // step1: loop each layer - var layer = Math.min(n/2, m/2); - for(l = 0; l=l; j--) - { - cur.push(grid[m-l-1][j]); - } - // left - for(var i = m-l-2; i>l; i--) - { - cur.push(grid[i][l]); - } - - // step3: rotation (k%len) on one-dimension array - var d = cur.length; - var offset = k % d; - cur = [...cur.slice(offset, d), ...cur.slice(0, offset)]; - - // step4: refill rotated array back to 2D array at current layer - var index = 0; - // top - for(var j = l; j=l; j--) - { - grid[m-l-1][j] = cur[index++]; - } - // left - for(var i = m-l-2; i>l; i--) - { - grid[i][l] = cur[index++]; - } - } - return grid; -}; diff --git a/1915-number-of-wonderful-substrings.js b/1915-number-of-wonderful-substrings.js index 8402c87d..29310425 100644 --- a/1915-number-of-wonderful-substrings.js +++ b/1915-number-of-wonderful-substrings.js @@ -20,25 +20,3 @@ const wonderfulSubstrings = (word) => { return res; }; -// another - -/** - * @param {string} word - * @return {number} - */ -const asi = (c) => c.charCodeAt(); -const wonderfulSubstrings = (s) => { - let res = 0; - let f = Array(2 ** 10).fill(0); - f[0] = 1; // count array - let cur = res = 0; - for (const c of s) { - cur ^= 1 << asi(c) - 97; // get Hash (the set bit for a character.), update prefix parity - res += f[cur]; - for (let i = 0; i < 10; i++) { // a ~ j - res += f[cur ^ 1 << i]; // 1 << i get Hash - } - f[cur]++; - } - return res; -}; diff --git a/1930-unique-length-3-palindromic-subsequences.js b/1930-unique-length-3-palindromic-subsequences.js index d615b832..bd4297fe 100644 --- a/1930-unique-length-3-palindromic-subsequences.js +++ b/1930-unique-length-3-palindromic-subsequences.js @@ -24,30 +24,3 @@ const countPalindromicSubsequence = function(s) { return res }; -// another - - -/** - * @param {string} s - * @return {number} - */ -const countPalindromicSubsequence = (s) => { - let res = 0; - for (let i = 0; i < 26; i++) { - for (let j = 0; j < 26; j++) { - let len = 0; - for (const c of s) { - if(len === 3) break - if (len == 0) { - if (c.charCodeAt() - 97 == i) len++; // first char - } else if (len == 1) { - if (c.charCodeAt() - 97 == j) len++; // second char - } else if (len == 2) { - if (c.charCodeAt() - 97 == i) len++; // third char - } - } - if (len == 3) res++; - } - } - return res; -}; diff --git a/1931-painting-a-grid-with-three-different-colors.js b/1931-painting-a-grid-with-three-different-colors.js index 2e3cec5a..36907dfa 100644 --- a/1931-painting-a-grid-with-three-different-colors.js +++ b/1931-painting-a-grid-with-three-different-colors.js @@ -50,100 +50,3 @@ const colorTheGrid = function(m, n) { }; -// another - -/** - * @param {number} m - * @param {number} n - * @return {number} - */ -const colorTheGrid = function(m, n) { - const mod = 10 ** 9 + 7 - const colors = [1, 2, 3] - const memoDp = {}, memoOpts = {} - function getColor(pos, preMask) { - return (preMask >> (pos * 2)) & 3 - } - function setColor(pos, color, curMask) { - return curMask | (color << (pos * 2)) - } - function dfs(pos, curMask, preMask, res) { - if(pos === m) { - res.push(curMask) - return - } - for(let c of colors) { - if(getColor(pos, preMask) !== c && (pos === 0 || getColor(pos - 1, curMask) !== c)) { - dfs(pos + 1, setColor(pos, c, curMask), preMask, res) - } - } - } - function curOpts(preMask) { - if (memoOpts[preMask]) return memoOpts[preMask] - const res = [] - dfs(0, 0, preMask, res) - memoOpts[preMask] = res - return res - } - function dp(col, preMask) { - const k = `${col},${preMask}` - if(col === n) return 1 - if(memoDp[k]) return memoDp[k] - let res = 0 - const cur = curOpts(preMask) - for(let mask of cur) { - res = (res + dp(col + 1, mask)) % mod - } - memoDp[k] = res - return res - } - - return dp(0, 0) -}; - -// another - -/** - * @param {number} m - * @param {number} n - * @return {number} - */ -const colorTheGrid = function(m, n) { - const mod = 1e9 + 7 - const limit = 1 << (2 * m) - const memo = Array.from({ length: n }, () => Array(limit)) - - return dp(0, 0) - - function dp(col, preColMask) { - if(col === n) return 1 - let res = 0 - - if(memo[col][preColMask] != null) return memo[col][preColMask] - const curColMasks = [] - dfs(preColMask, 0, 0, curColMasks) - for(const colMask of curColMasks) { - res = (res + dp(col + 1, colMask)) % mod - } - return memo[col][preColMask] = res - } - - function dfs(preColMask, curColMask, row, res) { - if(row === m) { - res.push(curColMask) - return - } - for(let i = 1; i <= 3; i++) { - if(getColor(preColMask, row) !== i && (row === 0 || getColor(curColMask, row - 1) !== i)) { - dfs(preColMask, setColor(curColMask, row, i) ,row + 1, res) - } - } - } - - function getColor(mask, row) { - return (mask >> (2 * row)) & 3 - } - function setColor(mask, row, val) { - return mask | (val << (2 * row)) - } -}; diff --git a/1937-maximum-number-of-points-with-cost.js b/1937-maximum-number-of-points-with-cost.js index 83e5caf7..a9df9f58 100644 --- a/1937-maximum-number-of-points-with-cost.js +++ b/1937-maximum-number-of-points-with-cost.js @@ -29,43 +29,3 @@ const maxPoints = function(points) { return Math.max(...prev) }; -// another - -/** - * @param {number[][]} points - * @return {number} - */ -const maxPoints = function(points) { - let m = points.length, n = points[0].length; - let result = 0; - // dp - const dp = Array.from({ length: m }, () => Array(n).fill(0)); - for (let i = 0; i < m; i++) { - for (let j = 0; j < n; j++) { - if (i == 0) { - dp[i][j] = points[i][j]; - } else { - dp[i][j] = Math.max(points[i][j] + dp[i - 1][j], dp[i][j]); - } - } - for (let j = 0; j < n; j++) { - // right - for (let k = 1; k < n - j; k++) { - if (dp[i][j + k] >= dp[i][j] - k) { - break; - } - dp[i][j + k] = dp[i][j] - k; - } - for (let k = 1; k <= j; k++) { - if (dp[i][j - k] >= dp[i][j] - k) { - break; - } - dp[i][j - k] = dp[i][j] - k; - } - } - } - for (let j = 0; j < n; j++) { - result = Math.max(result, dp[m - 1][j]); - } - return result; -}; diff --git a/1942-the-number-of-the-smallest-unoccupied-chair.js b/1942-the-number-of-the-smallest-unoccupied-chair.js index 0876ec69..03b667d7 100644 --- a/1942-the-number-of-the-smallest-unoccupied-chair.js +++ b/1942-the-number-of-the-smallest-unoccupied-chair.js @@ -99,158 +99,3 @@ class PQ { } } -// another - -/** - * @param {number[][]} times - * @param {number} targetFriend - * @return {number} - */ -const smallestChair = function (times, targetFriend) { - const targetArrival = times[targetFriend][0] - let seatNum = 0 - let res = 0 - const n = times.length - - times.sort((a, b) => a[0] - b[0]) - - const occupied = new PriorityQueue((a, b) => a[0] < b[0]) - const available = new PriorityQueue((a, b) => a < b) - - for(let i = 0; i < n; i++) { - const [arrival, leaving] = times[i] - while(!occupied.isEmpty() && occupied.peek()[0] <= arrival) { - available.push(occupied.pop()[1]) - } - let seat - if(!available.isEmpty()) { - seat = available.pop() - } else { - seat = seatNum - seatNum++ - } - occupied.push([leaving, seat]) - if(arrival === targetArrival) { - res = seat - break - } - } - - return res -} -class PriorityQueue { - constructor(comparator = (a, b) => a > b) { - this.heap = [] - this.top = 0 - this.comparator = comparator - } - size() { - return this.heap.length - } - isEmpty() { - return this.size() === 0 - } - peek() { - return this.heap[this.top] - } - push(...values) { - values.forEach((value) => { - this.heap.push(value) - this.siftUp() - }) - return this.size() - } - pop() { - const poppedValue = this.peek() - const bottom = this.size() - 1 - if (bottom > this.top) { - this.swap(this.top, bottom) - } - this.heap.pop() - this.siftDown() - return poppedValue - } - replace(value) { - const replacedValue = this.peek() - this.heap[this.top] = value - this.siftDown() - return replacedValue - } - - parent = (i) => ((i + 1) >>> 1) - 1 - left = (i) => (i << 1) + 1 - right = (i) => (i + 1) << 1 - greater = (i, j) => this.comparator(this.heap[i], this.heap[j]) - swap = (i, j) => ([this.heap[i], this.heap[j]] = [this.heap[j], this.heap[i]]) - siftUp = () => { - let node = this.size() - 1 - while (node > this.top && this.greater(node, this.parent(node))) { - this.swap(node, this.parent(node)) - node = this.parent(node) - } - } - siftDown = () => { - let node = this.top - while ( - (this.left(node) < this.size() && this.greater(this.left(node), node)) || - (this.right(node) < this.size() && this.greater(this.right(node), node)) - ) { - let maxChild = - this.right(node) < this.size() && - this.greater(this.right(node), this.left(node)) - ? this.right(node) - : this.left(node) - this.swap(node, maxChild) - node = maxChild - } - } -} - -// another - - -/** - * @param {number[][]} times - * @param {number} targetFriend - * @return {number} - */ -var smallestChair = function (times, targetFriend) { - const [targetArrival] = times[targetFriend] - const arrivalQueue = times - const leavingQueue = [...times] - arrivalQueue.sort((a, b) => a[0] - b[0]) - leavingQueue.sort((a, b) => a[1] - b[1] || a[0] - b[0]) - const chairsByLeaveTime = new Map() - let chairsCount = 0 - let arriving = 0, - leaving = 0 - - while (arriving < arrivalQueue.length) { - let chairIdx - const arrival = arrivalQueue[arriving][0] - const leave = leavingQueue[leaving][1] - if (arrival < leave) { - chairIdx = chairsCount++ - } else { - let freeChairIdx = leaving - chairIdx = chairsByLeaveTime.get(leavingQueue[freeChairIdx++][0]) - while (arrival >= leavingQueue[freeChairIdx][1]) { - const nextChair = chairsByLeaveTime.get(leavingQueue[freeChairIdx][0]) - if (chairIdx > nextChair) { - ;[leavingQueue[leaving], leavingQueue[freeChairIdx]] = [ - leavingQueue[freeChairIdx], - leavingQueue[leaving], - ] - chairIdx = nextChair - } - ++freeChairIdx - } - ++leaving - } - if (targetArrival === arrival) { - return chairIdx - } - chairsByLeaveTime.set(arrival, chairIdx) - arriving++ - } -} diff --git a/1943-describe-the-painting.js b/1943-describe-the-painting.js index 59191bb1..ba490c82 100644 --- a/1943-describe-the-painting.js +++ b/1943-describe-the-painting.js @@ -23,33 +23,3 @@ const splitPainting = function(segments) { return res }; -// another - -/** - * @param {number[][]} segments - * @return {number[][]} - */ -const splitPainting = function(segments) { - const sum = {} - for(const [s, e, v] of segments) { - if(sum[s] == null) sum[s] = 0 - if(sum[e] == null) sum[e] = 0 - sum[s] += v - sum[e] -= v - } - const keys = Object.keys(sum).map(e => +e) - keys.sort((a, b) => a - b) - const res = [] - let pre = 0, s = 0, n = keys.length - for(let i = 0; i < n; i++) { - const k = keys[i] - - if(s) { - res.push([pre, k, s]) - } - s += sum[k] - pre = k - } - - return res -}; diff --git a/1944-number-of-visible-people-in-a-queue.js b/1944-number-of-visible-people-in-a-queue.js index d74994b3..aba1d34e 100644 --- a/1944-number-of-visible-people-in-a-queue.js +++ b/1944-number-of-visible-people-in-a-queue.js @@ -21,51 +21,3 @@ const canSeePersonsCount = function(heights) { return res.reverse() }; -// another - - -/** - * @param {number[]} heights - * @return {number[]} - */ -const canSeePersonsCount = function(heights) { - const ans = new Uint32Array(heights.length); - - const stack = []; - for (let i = heights.length - 1; i >= 0; i--) { - const h = heights[i]; - - let del = 0; - while (stack.length && stack[stack.length - 1] <= h) { - del++; - stack.pop(); - } - - ans[i] = del + (stack.length ? 1 : 0); - stack.push(h); - } - - return ans; -}; - -// another - -/** - * @param {number[]} heights - * @return {number[]} - */ -const canSeePersonsCount = function(heights) { - const stack = [], n = heights.length, res = Array(n) - for(let i = n - 1; i >= 0; i--) { - const h = heights[i] - let del = 0 - while(stack.length && stack[stack.length - 1] <= h) { - stack.pop() - del++ - } - res[i] = stack.length ? del + 1 : del - stack.push(h) - } - - return res -}; diff --git a/1947-maximum-compatibility-score-sum.js b/1947-maximum-compatibility-score-sum.js index 7debdc44..4087a1c8 100644 --- a/1947-maximum-compatibility-score-sum.js +++ b/1947-maximum-compatibility-score-sum.js @@ -43,78 +43,3 @@ const maxCompatibilitySum = function(students, mentors) { } }; -// another - -/** - * @param {number[][]} students - * @param {number[][]} mentors - * @return {number} - */ -const maxCompatibilitySum = function(students, mentors) { - const n = students.length, dp = Array(1 << n).fill(-Infinity) - const m = students[0].length - return dfs(0, 0) - - function dfs(i, mask) { - if(i === n) return 0 - if(dp[mask] !== -Infinity) return dp[mask] - for(let j = 0; j < n; j++) { - if((mask & (1 << j)) === 0) { - dp[mask] = Math.max(dp[mask], calc(i, j) + dfs(i + 1, mask | (1 << j))) - } - } - - return dp[mask] - } - - function calc(i, j) { - let res = 0 - const a = students[i], b = mentors[j] - for(let k = 0; k < m; k++) { - if(a[k] === b[k]) res++ - } - return res - } -}; - - -// another - -/** - * @param {number[][]} students - * @param {number[][]} mentors - * @return {number} - */ -const maxCompatibilitySum = function(students, mentors) { - const obj = { res: 0 }, hash = {} - bt(students, mentors, 0, 0, obj, hash) - - return obj.res -}; - -function bt(stu, men, i, score, obj, hash) { - - if(i === stu.length) { - if(score > obj.res) { - obj.res = score - } - return - } - - for(let j = 0; j < men.length; j++) { - const k = `${j}` - if(hash[k] === 1) continue - hash[k] = 1 - bt(stu, men, i + 1, score + calc(stu[i], men[j]), obj, hash) - delete hash[k] - } -} - -function calc(a1, a2) { - const n = a1.length - let res = 0 - for(let i = 0; i < n; i++) { - if(a1[i] === a2[i]) res++ - } - return res -} diff --git a/1953-maximum-number-of-weeks-for-which-you-can-work.js b/1953-maximum-number-of-weeks-for-which-you-can-work.js index c981ef69..7b75aba9 100644 --- a/1953-maximum-number-of-weeks-for-which-you-can-work.js +++ b/1953-maximum-number-of-weeks-for-which-you-can-work.js @@ -14,39 +14,3 @@ const numberOfWeeks = function(milestones) { }; -// another - -/** - * @param {number[]} milestones - * @return {number} - */ -const numberOfWeeks = function(milestones) { - let sum = 0; - for (let i = 0; i < milestones.length; i++) { - sum += milestones[i]; - } - - let cantWork = 0; - for (let i = 0; i < milestones.length; i++) { - cantWork = Math.max(cantWork, milestones[i] - (sum - milestones[i]) - 1); - } - - return sum - cantWork; -}; - -// another - -/** - * @param {number[]} milestones - * @return {number} - */ -const numberOfWeeks = function(milestones) { - const max = Math.max(...milestones) - let sum = 0 - for(let i = 0; i < milestones.length; i++) { - sum += milestones[i] - } - const res = sum - max - - return Math.min(sum, res * 2 + 1) -}; diff --git a/1955-count-number-of-special-subsequences.js b/1955-count-number-of-special-subsequences.js index 8467f496..4fe2c251 100644 --- a/1955-count-number-of-special-subsequences.js +++ b/1955-count-number-of-special-subsequences.js @@ -18,41 +18,3 @@ const countSpecialSubsequences = function (nums) { return dp[2] } -// another - -/** - * @param {number[]} nums - * @return {number} - */ -const countSpecialSubsequences = function(nums) { - const mod = 10 ** 9 + 7 - const dp = Array.from({length: 10 **5 + 1}, () => Array(4).fill(-1)) - let n = nums.length, a = nums - return f(0, 0) - function f(index, firstMandatory) { - if (index == n && firstMandatory == 3) { - return 1; - } - if (index == n) { - return 0; - } - if (dp[index][firstMandatory] >= 0) { - return dp[index][firstMandatory]; - } - let result = 0; - if (a[index] == firstMandatory) { - result = (result + - f(index + 1, firstMandatory) + - f(index + 1, firstMandatory + 1) - ) % mod; - } - result = (result + f(index + 1, firstMandatory)) % mod; - - return dp[index][firstMandatory] = result; - } -}; - - - - - diff --git a/1963-minimum-number-of-swaps-to-make-the-string-balanced.js b/1963-minimum-number-of-swaps-to-make-the-string-balanced.js index 90a3793d..3ae28d84 100644 --- a/1963-minimum-number-of-swaps-to-make-the-string-balanced.js +++ b/1963-minimum-number-of-swaps-to-make-the-string-balanced.js @@ -15,48 +15,3 @@ const minSwaps = function(s) { return Math.ceil(stk.length / 2) }; -// another - -/** - * @param {string} s - * @return {number} - */ -const minSwaps = function(s) { - const stack = [] - let num = 0 - for(let e of s) { - if(e === '[') { - stack.push(e) - num++ - } - if(e === ']') { - if(stack[stack.length - 1] === '[') { - stack.pop() - num-- - } - } - } - // console.log(num) - return Math.ceil(num / 2) -}; - -// another - -/** - * @param {string} s - * @return {number} - */ -const minSwaps = function(s) { - let num = 0 - for(let e of s) { - if(e === '[') { - num++ - } - if(e === ']') { - if(num > 0) { - num-- - } - } - } - return Math.ceil(num / 2) -}; diff --git a/1964-find-the-longest-valid-obstacle-course-at-each-position.js b/1964-find-the-longest-valid-obstacle-course-at-each-position.js index 878844f2..2f97162a 100644 --- a/1964-find-the-longest-valid-obstacle-course-at-each-position.js +++ b/1964-find-the-longest-valid-obstacle-course-at-each-position.js @@ -28,84 +28,3 @@ const longestObstacleCourseAtEachPosition = function(obstacles) { } }; -// another - -/** - * @param {number[]} obstacles - * @return {number[]} - */ -const longestObstacleCourseAtEachPosition = function(obstacles) { - const n = obstacles.length - const stack = [], res = [] - let len = 0 - - for(let i = 0; i < n; i++) { - const cur = obstacles[i] - const idx = chk(cur) - if(idx === len) { - stack.push(cur) - len++ - res.push(len) - }else { - stack[idx] = cur - res.push(idx + 1) - } - } - - return res - - function chk(x) { - if(len && stack[len - 1] <= x) return len - let l = 0, r = len - 1 - while(l < r) { - const mid = ~~((l + r) / 2) - if(stack[mid] <= x) { - l = mid + 1 - } else { - r = mid - } - } - return l - } -}; - - - - -// another - -/** - * @param {number[]} obstacles - * @return {number[]} - */ -const longestObstacleCourseAtEachPosition = function(obstacles) { - const n = obstacles.length - const stack = [], res = Array(n).fill(0) - let m = 0 - let j = 0; - for (let x of obstacles) { - let i = chk(x); - if (i == m) { - ++m; - stack.push(x); - } else { - stack[i] = x; - } - res[j++] = i + 1; - } - return res; - function chk(x) { - if (m && stack[m - 1] <= x) return m; - let l = 0, r = m - 1; - while (l < r) { - let m = (l + r) >> 1; - if (stack[m] > x) { - r = m; - } else { - l = m + 1; - } - } - return l; - } -}; - diff --git a/1969-minimum-non-zero-product-of-the-array-elements.js b/1969-minimum-non-zero-product-of-the-array-elements.js index 2b29dade..fc100257 100644 --- a/1969-minimum-non-zero-product-of-the-array-elements.js +++ b/1969-minimum-non-zero-product-of-the-array-elements.js @@ -19,31 +19,3 @@ function fastPow(base, power, mod) { } -// another - - -/** - * @param {number} p - * @return {number} - */ -const minNonZeroProduct = function (p) { - const b = BigInt(p) - const mod = BigInt(10 ** 9 + 7) - - return ( - (((BigInt(1n << b) - 1n) % mod) * - pow(BigInt(1n << b) - 2n, BigInt(1n << (b - 1n)) - 1n)) % - mod - ) - - function pow(a, n) { - let r = 1n - a %= mod - while (n > 0n) { - r = (r * a) % mod - a = (a * a) % mod - n /= 2n - } - return r - } -} diff --git a/1976-number-of-ways-to-arrive-at-destination.js b/1976-number-of-ways-to-arrive-at-destination.js index 11572bdd..b6015baa 100644 --- a/1976-number-of-ways-to-arrive-at-destination.js +++ b/1976-number-of-ways-to-arrive-at-destination.js @@ -109,114 +109,3 @@ class PriorityQueue { } } -// another - -/** - * @param {number} n - * @param {number[][]} roads - * @return {number} - */ -const countPaths = function(n, roads) { - const graph = {}, MOD = 1e9 + 7 - for(const [u, v, t] of roads) { - if(graph[u] == null) graph[u] = {} - if(graph[v] == null) graph[v] = {} - graph[u][v] = t - graph[v][u] = t - } - - return dijkstra(graph, n, 0) - - function dijkstra(graph, n, src) { - const dist = Array(n).fill(Infinity) - const ways = Array(n).fill(0) - ways[src] = 1 - dist[src] = 0 - const pq = new PriorityQueue((a, b) => a[0] < b[0]) - pq.push([0, 0]) - while(!pq.isEmpty()) { - const [d, u] = pq.pop() - if(d > dist[u]) continue - for(const next of Object.keys(graph[u] || {})) { - const val = graph[u][next] - if(dist[next] > d + val) { - dist[next] = d + val - ways[next] = ways[u] - pq.push([dist[next], next]) - } else if(dist[next] === d + val) { - ways[next] = (ways[next] + ways[u]) % MOD - } - } - } - - return ways[n - 1] - } -}; - -class PriorityQueue { - constructor(comparator = (a, b) => a > b) { - this.heap = [] - this.top = 0 - this.comparator = comparator - } - size() { - return this.heap.length - } - isEmpty() { - return this.size() === 0 - } - peek() { - return this.heap[this.top] - } - push(...values) { - values.forEach((value) => { - this.heap.push(value) - this.siftUp() - }) - return this.size() - } - pop() { - const poppedValue = this.peek() - const bottom = this.size() - 1 - if (bottom > this.top) { - this.swap(this.top, bottom) - } - this.heap.pop() - this.siftDown() - return poppedValue - } - replace(value) { - const replacedValue = this.peek() - this.heap[this.top] = value - this.siftDown() - return replacedValue - } - - parent = (i) => ((i + 1) >>> 1) - 1 - left = (i) => (i << 1) + 1 - right = (i) => (i + 1) << 1 - greater = (i, j) => this.comparator(this.heap[i], this.heap[j]) - swap = (i, j) => ([this.heap[i], this.heap[j]] = [this.heap[j], this.heap[i]]) - siftUp = () => { - let node = this.size() - 1 - while (node > this.top && this.greater(node, this.parent(node))) { - this.swap(node, this.parent(node)) - node = this.parent(node) - } - } - siftDown = () => { - let node = this.top - while ( - (this.left(node) < this.size() && this.greater(this.left(node), node)) || - (this.right(node) < this.size() && this.greater(this.right(node), node)) - ) { - let maxChild = - this.right(node) < this.size() && - this.greater(this.right(node), this.left(node)) - ? this.right(node) - : this.left(node) - this.swap(node, maxChild) - node = maxChild - } - } -} diff --git a/198-house-robber.js b/198-house-robber.js index 3ce91fff..feb83e36 100644 --- a/198-house-robber.js +++ b/198-house-robber.js @@ -14,20 +14,3 @@ function rob(nums) { return prev1; } -// another - -/** - * @param {number[]} nums - * @return {number} - */ -const rob = function(nums) { - const n = nums.length - const dp = Array(n+1).fill(0) - dp[1] = nums[0] - - for(let i = 1; i < n; i++) { - dp[i + 1] = Math.max(dp[i], dp[i - 1] + nums[i]) - } - - return dp[n] -}; diff --git a/1981-minimize-the-difference-between-target-and-chosen-elements.js b/1981-minimize-the-difference-between-target-and-chosen-elements.js index f66a42de..df41deae 100644 --- a/1981-minimize-the-difference-between-target-and-chosen-elements.js +++ b/1981-minimize-the-difference-between-target-and-chosen-elements.js @@ -19,29 +19,3 @@ const minimizeTheDifference = function(mat, target) { } }; -// another - -/** - * @param {number[][]} mat - * @param {number} target - * @return {number} - */ -const minimizeTheDifference = function(mat, target) { - const m = mat.length, n = mat[0].length - const memo = Array.from({ length: m }, () => Array()) - return dfs(0, 0) - - function dfs(row, sum) { - if(row === m) return Math.abs(target - sum) - if(memo[row][sum] != null) return memo[row][sum] - let res = Number.MAX_SAFE_INTEGER - for(let i = 0; i < n; i++) { - res = Math.min(res, dfs(row + 1, sum + mat[row][i])) - } - - return memo[row][sum] = res - } -}; - - - diff --git a/1982-find-array-given-subset-sums.js b/1982-find-array-given-subset-sums.js index ca16e18a..e21bfbdc 100644 --- a/1982-find-array-given-subset-sums.js +++ b/1982-find-array-given-subset-sums.js @@ -42,93 +42,3 @@ function bs(arr, e) { return arr[l] === e } -// another - -/** - * @param {number} n - * @param {number[]} sums - * @return {number[]} - */ -const recoverArray = function(n, sums) { - const res = [] - sums.sort((a, b) => a - b) - - while(res.length < n) { - const visited = Array(sums.length).fill(false) - const a1 = [], a2 = [] - const d = sums[1] - sums[0] - for(let i = 0, j = 1; i < sums.length; i++, j++) { - while(i < sums.length && visited[i]) i++ - if(i === sums.length) break - while(j <= i || sums[j] !== sums[i] + d) j++ - a1.push(sums[i]) - a2.push(sums[j]) - visited[i] = visited[j] = true - } - - if(bs(a1, 0)) { - sums = a1 - res.push(d) - } else { - sums = a2 - res.push(-d) - } - } - - return res -}; - -function bs(arr, val) { - let l = 0, r = arr.length - 1 - while(l < r) { - const mid = ~~((l + r) / 2) - if(arr[mid] < val) l = mid + 1 - else r = mid - } - - return arr[l] === val -} - -// another - -/** - * @param {number} n - * @param {number[]} sums - * @return {number[]} - */ -const recoverArray = function(n, sums) { - const res = [] - sums.sort((a, b) => a - b) - - while(res.length < n) { - const m = sums.length, visited = Array(m).fill(false) - let a1 = [], a2 = [], delta = sums[1] - sums[0] - for(let i = 0, j = 1; i < m && j < m; i++, j++) { - while(i < m && visited[i]) i++ - if(i === m) break - while(i >= j || sums[j] !== sums[i] + delta) j++ - if(j === m) break - a1.push(sums[i]) - a2.push(sums[j]) - visited[i] = visited[j] = true - } - if(binarySearch(a1, 0)) { - sums = a1 - res.push(delta) - } else { - sums = a2 - res.push(-delta) - } - } - return res - - function binarySearch(arr, val) { - let l = 0, r = arr.length - 1 - while(l < r) { - const mid = ~~((l + r) / 2) - if(arr[mid] < val) l = mid + 1 - else r = mid - } - return arr[l] === val - } -}; diff --git a/1986-minimum-number-of-work-sessions-to-finish-the-tasks.js b/1986-minimum-number-of-work-sessions-to-finish-the-tasks.js index a3237bb6..1b9af932 100644 --- a/1986-minimum-number-of-work-sessions-to-finish-the-tasks.js +++ b/1986-minimum-number-of-work-sessions-to-finish-the-tasks.js @@ -29,67 +29,3 @@ const minSessions = function(tasks, sessionTime) { } }; -// another - -/** - * @param {number[]} tasks - * @param {number} sessionTime - * @return {number} - */ -const minSessions = function(tasks, sessionTime) { - const n = tasks.length - const dp = Array.from({ length: 1 << 14 }, () => Array(16).fill(-1)) - return fn(0, 0) - - function fn(mask, consumed) { - if (mask === (1 << n) - 1) { - return consumed === 0 ? 0 : 1 - } - if (dp[mask][consumed] !== -1) { - return dp[mask][consumed]; - } - - let result = Number.MAX_VALUE; - if (consumed > 0) { - result = Math.min(result, 1 + fn(mask, 0)); - } - for (let i = 0; i < n; i++) { - if ((mask & (1 << i)) === 0 && consumed + tasks[i] <= sessionTime) { - result = Math.min(result, fn(mask | (1 << i), consumed + tasks[i])); - } - } - return dp[mask][consumed] = result; - } -}; - -// another - -/** - * @param {number[]} tasks - * @param {number} sessionTime - * @return {number} - */ -function minSessions(tasks, sessionTime) { - const n = tasks.length - const memo = Array.from({ length: 1 << n }, () => Array(15)) - return helper((1 << n) - 1, 0) - - function helper(mask, remain) { - if(mask === 0) return 0 - if(memo[mask][remain] != null) return memo[mask][remain] - let res = n - - for(let i = 0; i < n; i++) { - if((1 << i) & mask) { - const newMask = mask & (~(1 << i)) - if(tasks[i] <= remain) { - res = Math.min(res, helper(newMask, remain - tasks[i])) - } else { - res = Math.min(res, helper(newMask, sessionTime - tasks[i]) + 1) - } - } - } - - return memo[mask][remain] = res - } -} diff --git a/1987-number-of-unique-good-subsequences.js b/1987-number-of-unique-good-subsequences.js index 9cc21f0f..af810de9 100644 --- a/1987-number-of-unique-good-subsequences.js +++ b/1987-number-of-unique-good-subsequences.js @@ -51,24 +51,3 @@ const numberOfUniqueGoodSubsequences = function (binary) { } } -// another - -/** - * @param {string} binary - * @return {number} - */ -const numberOfUniqueGoodSubsequences = function (binary) { - const n = binary.length, - mod = 1e9 + 7 - let hasZero = 0, ends1 = 0, ends0 = 0 - for(let ch of binary) { - if(ch === '1') { - ends1 = (ends1 + ends0 + 1) % mod - } else { - ends0 = (ends1 + ends0) % mod - hasZero = 1 - } - } - return (ends1 + ends0 + hasZero) % mod -} - diff --git a/199-binary-tree-right-side-view.js b/199-binary-tree-right-side-view.js index 7bbf5a42..c9c3e719 100644 --- a/199-binary-tree-right-side-view.js +++ b/199-binary-tree-right-side-view.js @@ -25,20 +25,3 @@ const rightSideView = function(root) { return res }; -// another - -const rightSideView = function(root) { - const res = [] - const helper = function(node, level) { - if (!node) { - return - } - if (!res[level]) { - res.push(node.val) - } - helper(node.right, level + 1) - helper(node.left, level + 1) - } - helper(root, 0) - return res -} diff --git a/1996-the-number-of-weak-characters-in-the-game.js b/1996-the-number-of-weak-characters-in-the-game.js index 383d457e..9148d323 100644 --- a/1996-the-number-of-weak-characters-in-the-game.js +++ b/1996-the-number-of-weak-characters-in-the-game.js @@ -28,69 +28,3 @@ const numberOfWeakCharacters = function(properties) { return res }; -// another - -/** - * @param {number[][]} properties - * @return {number} - */ -const numberOfWeakCharacters = function(properties) { - properties.sort((a, b) => a[0] - b[0] || b[1] - a[1]) - let stack = [], res = 0 - - for(let i = 0, n = properties.length; i < n; i++) { - while(stack.length && stack[stack.length - 1] < properties[i][1]) { - stack.pop() - res++ - } - stack.push(properties[i][1]) - } - - return res -}; - -// another - -/** - * @param {number[][]} properties - * @return {number} - */ -const numberOfWeakCharacters = function(properties) { - if (properties == null || properties.length == 0) { - return 0; - } - properties.sort((o1, o2) => { - if (o1[0] == o2[0]) { - return o1[1] - o2[1]; - } - return o1[0] - o2[0]; - }); - const { max } = Math - let mmax = Array(1e5 + 10).fill( 0); - let ans = 0; - let n = properties.length; - for (let i = n - 1; i >= 0; i--) mmax[properties[i][0]] = max(properties[i][1], mmax[properties[i][0]]); - for (let i = 1e5; i >= 1; i--) mmax[i] = max(mmax[i], mmax[i + 1]); - for (let i = 0; i < n; i++) { - if (mmax[properties[i][0] + 1] > properties[i][1]) ans++; - } - return ans; -}; - -// another - -/** - * @param {number[][]} properties - * @return {number} - */ -const numberOfWeakCharacters = function(properties) { - properties.sort((a, b) => a[0] === b[0] ? b[1] - a[1] : a[0] - b[0]) - let max = -Infinity, res = 0 - for(let n = properties.length, i = n - 1; i >= 0; i--) { - const [a, d] = properties[i] - if(d < max) res++ - max = Math.max(max, d) - } - - return res -}; diff --git a/1997-first-day-where-you-have-been-in-all-the-rooms.js b/1997-first-day-where-you-have-been-in-all-the-rooms.js index 8b503fdd..95f760ab 100644 --- a/1997-first-day-where-you-have-been-in-all-the-rooms.js +++ b/1997-first-day-where-you-have-been-in-all-the-rooms.js @@ -17,20 +17,3 @@ const firstDayBeenInAllRooms = function(nextVisit) { return f[n - 1]; }; -// another - -/** - * @param {number[]} nextVisit - * @return {number} - */ -const firstDayBeenInAllRooms = function(nextVisit) { - const mod = 1e9 + 7 - const n = nextVisit.length - const dp = Array(n).fill(0) - for(let i = 1; i < n; i++) { - // i - 1 ---> nextVisit[i - 1] ---> i - 1 ---> i - dp[i] = (dp[i - 1] + 1 + dp[i - 1] - dp[nextVisit[i - 1]] + 1 + mod) % mod - } - - return dp[n - 1] -}; diff --git a/1998-gcd-sort-of-an-array.js b/1998-gcd-sort-of-an-array.js index 68ecd546..ccc31216 100644 --- a/1998-gcd-sort-of-an-array.js +++ b/1998-gcd-sort-of-an-array.js @@ -63,71 +63,3 @@ class UnionFind { } }; -// another - -/** - * @param {number[]} nums - * @return {boolean} - */ -const gcdSort = function(nums) { - const spf = Array(nums.length).fill(0) - let maxNum = Math.max(...nums); - sieve(maxNum); - - const uf = new UnionFind(maxNum+1); - for (let x of nums) { - for (let f of getFactors(x)) uf.union(f, x); - } - - - const sortedArr = nums.slice(); - sortedArr.sort((a, b) => a - b) - - for (let i = 0; i < nums.length; ++i) { - let pu = uf.find(sortedArr[i]); - let pv = uf.find(nums[i]); - if (pu != pv) return false; // can't swap nums[i] with sortedArr[i] - } - return true; - - function sieve( n) { // O(Nlog(logN)) ~ O(N) - for (let i = 2; i <= n; ++i) spf[i] = i; - for (let i = 2; i * i <= n; i++) { - if (spf[i] != i) continue; // skip if `i` is not a prime number - for (let j = i * i; j <= n; j += i) { - if (spf[j] == j) { // marking spf[j] if it is not previously marked - spf[j] = i; - } - } - } - } - - function getFactors(n) { // O(logN) - const factors = []; - while (n > 1) { - factors.push(spf[n]); - n = ~~(n /spf[n]); - } - return factors; - } -}; - -function gcd( x, y) { - return y == 0 ? x : gcd(y, x % y); -} - -class UnionFind { - constructor(n) { - this.parent = []; - for (let i = 0; i < n; i++) this.parent[i] = i; - } - find(x) { - if (x == this.parent[x]) return x; - return this.parent[x] = this.find(this.parent[x]); // Path compression - } - union( u, v) { - let pu = this.find(u), pv = this.find(v); - if (pu != pv) this.parent[pu] = pv; - } -}; - diff --git a/2-add-two-numbers.js b/2-add-two-numbers.js index 06f99a30..0bb60871 100755 --- a/2-add-two-numbers.js +++ b/2-add-two-numbers.js @@ -27,135 +27,3 @@ const addTwoNumbers = function(l1, l2) { return res.next }; -// another - -/** - * Definition for singly-linked list. - * function ListNode(val) { - * this.val = val; - * this.next = null; - * } - */ -/** - * @param {ListNode} l1 - * @param {ListNode} l2 - * @return {ListNode} - */ -const addTwoNumbers = function(l1, l2) { - const res = new ListNode(null); - single(l1, l2, res); - return res.next; -}; - -function single(l1, l2, res) { - let cur; - let addOne = 0; - let sum = 0; - let curVal = 0; - while (l1 || l2 || addOne) { - sum = ((l1 && l1.val) || 0) + ((l2 && l2.val) || 0) + addOne; - if (sum / 10 >= 1) { - curVal = sum % 10; - addOne = 1; - } else { - curVal = sum; - addOne = 0; - } - - if (cur) { - cur = cur.next = new ListNode(curVal); - } else { - cur = res.next = new ListNode(curVal); - } - - if (l1) { - l1 = l1.next; - } - if (l2) { - l2 = l2.next; - } - } -} - -// another - -/** - * Definition for singly-linked list. - * function ListNode(val, next) { - * this.val = (val===undefined ? 0 : val) - * this.next = (next===undefined ? null : next) - * } - */ -/** - * @param {ListNode} l1 - * @param {ListNode} l2 - * @return {ListNode} - */ -const addTwoNumbers = function(l1, l2) { - let extra = false - const dummy = new ListNode() - let cur = dummy - while(l1 || l2) { - let val = 0 - if(l1) val += l1.val - if(l2) val += l2.val - if(extra) val += 1 - - if(val > 9) { - extra = true - val = val % 10 - } else { - extra = false - } - cur.next = new ListNode(val) - cur = cur.next - if(l1) l1 = l1.next - if(l2) l2 = l2.next - } - - if(extra) cur.next = new ListNode(1) - return dummy.next -}; - -// another - -/** - * Definition for singly-linked list. - * function ListNode(val, next) { - * this.val = (val===undefined ? 0 : val) - * this.next = (next===undefined ? null : next) - * } - */ -/** - * @param {ListNode} l1 - * @param {ListNode} l2 - * @return {ListNode} - */ -const addTwoNumbers = function(l1, l2) { - const dummy = new ListNode(null) - let cur = dummy, carry = 0 - - while(l1 || l2) { - let v = 0 - if(l1 && l2) { - v = l1.val + l2.val + carry - l1 = l1.next - l2 = l2.next - } else { - const node = l1 || l2 - v = node.val + carry - if(l1) l1 = l1.next - if(l2) l2 = l2.next - } - - cur.next = new ListNode(v % 10) - cur = cur.next - if(v >= 10) carry = 1 - else carry = 0 - } - - if(carry) cur.next = new ListNode(1) - - return dummy.next -}; - diff --git a/20-valid-parentheses.js b/20-valid-parentheses.js index c9065a42..3d9e0f06 100755 --- a/20-valid-parentheses.js +++ b/20-valid-parentheses.js @@ -14,60 +14,3 @@ }; -// another - -/** - * @param {string} s - * @return {boolean} - */ - const isValid = function(s) { - const stack = [] - const n = s.length - for(let c of s) { - if(c === '(' || c === '{' || c === '[') stack.push(c) - else if(c === ')') { - if(stack[stack.length - 1] === '(') stack.pop() - else return false - } - else if(c === '}') { - if(stack[stack.length - 1] === '{') stack.pop() - else return false - } - else if(c === ']') { - if(stack[stack.length - 1] === '[') stack.pop() - else return false - } - } - return stack.length === 0 -}; - -// another - -/** - * @param {string} s - * @return {boolean} - */ -const isValid = function(s) { - const openBrackets = ["(", "{", "["]; - const closeBrackets = [")", "}", "]"]; - const oArr = []; - let char = ""; - let cidx = 0; - let oidx = 0; - for (let i = 0; i < s.length; i++) { - char = s.charAt(i); - if (closeBrackets.indexOf(char) !== -1) { - cidx = closeBrackets.indexOf(char); - lastOpenBracket = oArr[oArr.length - 1]; - oidx = openBrackets.indexOf(lastOpenBracket); - if (cidx === oidx) { - oArr.pop(); - } else { - return false; - } - } else { - oArr.push(char); - } - } - return oArr.length > 0 ? false : true; -}; diff --git a/2002-maximum-product-of-the-length-of-two-palindromic-subsequences.js b/2002-maximum-product-of-the-length-of-two-palindromic-subsequences.js index f6126c88..8d99d71e 100644 --- a/2002-maximum-product-of-the-length-of-two-palindromic-subsequences.js +++ b/2002-maximum-product-of-the-length-of-two-palindromic-subsequences.js @@ -28,46 +28,3 @@ function palindromic( i, s) { return len; } -// another - -/** - * @param {string} s - * @return {number} - */ -const maxProduct = function(s) { - const s1 = [], s2 = [], n = s.length - let res = 0 - dfs(0) - return res - - function dfs(idx) { - if(idx === n) { - if(isPalindromic(s1) && isPalindromic(s2)) { - res = Math.max(res, s1.length * s2.length) - } - return - } - const ch = s[idx] - s1.push(ch) - dfs(idx + 1) - s1.pop() - - s2.push(ch) - dfs(idx + 1) - s2.pop() - - dfs(idx + 1) - } - function isPalindromic(arr) { - let l = 0, r = arr.length - 1 - while(l < r) { - if(arr[l] === arr[r]) { - l++ - r-- - } else { - return false - } - } - return true - } -}; diff --git a/2007-find-original-array-from-doubled-array.js b/2007-find-original-array-from-doubled-array.js index 89e270d2..7d4629b3 100644 --- a/2007-find-original-array-from-doubled-array.js +++ b/2007-find-original-array-from-doubled-array.js @@ -24,30 +24,3 @@ return res }; -// another - -/** - * @param {number[]} changed - * @return {number[]} - */ - const findOriginalArray = function(changed) { - const n = changed.length, res = [] - if(n % 2 === 1 || n === 0) return res - const hash = {} - for(let e of changed) { - if(hash[e] == null) hash[e] = 0 - hash[e]++ - } - changed.sort((a, b) => a - b) - - for(let i = 0, len = n; i < len; i++) { - const cur = changed[i], dVal = cur * 2 - if (cur === 0 && hash[cur] % 2 === 1) continue - if(hash[dVal] && hash[cur]) { - res.push(cur) - hash[dVal]-- - hash[cur]-- - } - } - return res.length === n / 2 ? res : [] -}; diff --git a/2008-maximum-earnings-from-taxi.js b/2008-maximum-earnings-from-taxi.js index eb4fb356..cc9669e7 100644 --- a/2008-maximum-earnings-from-taxi.js +++ b/2008-maximum-earnings-from-taxi.js @@ -20,33 +20,3 @@ const maxTaxiEarnings = function(n, rides) { return dp[1]; }; -// another - -/** - * @param {number} n - * @param {number[][]} rides - * @return {number} - */ -const maxTaxiEarnings = function(n, rides) { - const size = rides.length - rides.sort((a, b) => a[1] - b[1]) - const dp = [[0,0]] - for(const [s, e, t] of rides) { - const cur = bs(dp, s) + (e - s + t) - if(cur > dp[dp.length - 1][1]) { - dp.push([e, cur]) - } - } - return dp[dp.length - 1][1] - - function bs(arr, t) { - let l = 0, r = arr.length - 1 - while(l < r) { - const mid = r - ((r - l) >> 1) - if(arr[mid][0] > t) r = mid - 1 - else l = mid - } - // console.log(arr, t, l) - return arr[l][1] - } -}; diff --git a/201-bitwise-and-of-numbers-range.js b/201-bitwise-and-of-numbers-range.js index 7ab808b4..7b67a74a 100644 --- a/201-bitwise-and-of-numbers-range.js +++ b/201-bitwise-and-of-numbers-range.js @@ -8,19 +8,3 @@ const rangeBitwiseAnd = function(m, n) { return n; }; -// another - -/** - * @param {number} m - * @param {number} n - * @return {number} - */ -const rangeBitwiseAnd = function(m, n) { - let s = 0 - while(m !== n) { - m >>= 1 - n >>= 1 - s++ - } - return m << s -}; diff --git a/2013-detect-squares.js b/2013-detect-squares.js index 3cb84312..ec2a6561 100644 --- a/2013-detect-squares.js +++ b/2013-detect-squares.js @@ -39,84 +39,3 @@ DetectSquares.prototype.count = function(point) { */ -// another - -var DetectSquares = function() { - this.xMap = new Map(); - this.yMap = new Map(); -}; - -/** - * @param {number[]} point - * @return {void} - */ -DetectSquares.prototype.add = function(point) { - const [ x, y ] = point; - - // X-map - if (this.xMap.has(x)) { - const xMap = this.xMap.get(x); - - if (xMap.has(y)) { - xMap.set(y, xMap.get(y) + 1); - } else { - xMap.set(y, 1); - } - } else { - const countMap = new Map(); - countMap.set(y, 1); - this.xMap.set(x, countMap); - } - - // Y-map - if (this.yMap.has(y)) { - const yMap = this.yMap.get(y); - - if (yMap.has(x)) { - yMap.set(x, yMap.get(x) + 1); - } else { - yMap.set(x, 1); - } - } else { - const countMap = new Map(); - countMap.set(x, 1); - this.yMap.set(y, countMap); - } -}; - -/** - * @param {number[]} point - * @return {number} - */ -DetectSquares.prototype.count = function(point) { - const [ x, y ] = point; - let ans = 0; - - if (this.xMap.has(x) && this.yMap.has(y)) { - for (const y2 of this.xMap.get(x).keys()) { - if (y === y2) { - continue; - } - - // Find parallel - const sideLen = Math.abs(y - y2); - const possibleX = [ x - sideLen, x + sideLen]; - - for (const px of possibleX) { - if (this.yMap.get(y).has(px) && this.xMap.has(px) && this.xMap.get(px).has(y2)) { - ans += this.xMap.get(x).get(y2) * this.yMap.get(y).get(px) - * this.xMap.get(px).get(y2); - } - } - } - } - - return ans; -}; - -/** - * Your DetectSquares object will be instantiated and called as such: - * var obj = new DetectSquares() - * obj.add(point) - * var param_2 = obj.count(point) - */ diff --git a/2014-longest-subsequence-repeated-k-times.js b/2014-longest-subsequence-repeated-k-times.js index 57941aba..41236f0a 100644 --- a/2014-longest-subsequence-repeated-k-times.js +++ b/2014-longest-subsequence-repeated-k-times.js @@ -46,88 +46,3 @@ const longestSubsequenceRepeatedK = function(s, k) { } }; -// another - -/** - * @param {string} s - * @param {number} k - * @return {string} - */ -var longestSubsequenceRepeatedK = function(s, k) { - // Max length of the subsequence can be determined by the length of the string and k - const maxLen = Math.floor(s.length / k); - - // Find all possible characters that can appear in the subsequence (characters must appear at - // least k times in s) - const charCount = new Map(); - const possibleChars = [] - - for (const char of s) { - if (charCount.has(char)) { - charCount.set(char, charCount.get(char) + 1); - } else { - charCount.set(char, 1); - } - } - - for (const char of charCount.keys()) { - if (charCount.get(char) >= k) { - possibleChars.push(char); - } - } - - // Test possibilities - let ans = ""; - dfs(""); - - return ans; - - // Recursive function, tests if the given subsequence repeats k times in s - function dfs(seq) { - // Does not have enough repeats, return - if (countRepeats(seq) < k) { - return; - } - - // Update our answer if the new subsequence is better - if (seq.length > ans.length || (seq.length === ans.length && seq > ans)) { - ans = seq; - } - - // Append possible characters to the subsequence and test again - if (seq.length < maxLen) { - for (const char of possibleChars) { - dfs(seq + char); - } - } - } - - // Counts the number of times the given subsequence repeats in s (up to k) - function countRepeats(seq) { - - // Empty string, return k - if (!seq) { - return k; - } - - let repeats = 0; - let seqIdx = 0; - - for (const char of s) { - if (char === seq[seqIdx]) { - seqIdx += 1; - - if (seqIdx >= seq.length) { - seqIdx = 0; - repeats += 1; - - if (repeats >= k) { - break; - } - } - } - } - - return repeats; - } -}; diff --git a/2024-maximize-the-confusion-of-an-exam.js b/2024-maximize-the-confusion-of-an-exam.js index a181942c..d1a382f2 100644 --- a/2024-maximize-the-confusion-of-an-exam.js +++ b/2024-maximize-the-confusion-of-an-exam.js @@ -23,28 +23,3 @@ const maxConsecutiveAnswers = function(answerKey, k) { return Math.max(helper(answerKey, 0), helper(answerKey, 1)) }; -// another - -/** - * @param {string} answerKey - * @param {number} k - * @return {number} - */ -const maxConsecutiveAnswers = function(answerKey, k) { - let s = answerKey - const freq = Array(26).fill(0), n = s.length, A = 'A'.charCodeAt(0) - let res = 0, l = 0, r = 0, maxFreq = 0 - while(r < n) { - maxFreq = Math.max(maxFreq, ++freq[s.charCodeAt(r) - A]) - if(r - l + 1 - maxFreq > k) { - freq[s.charCodeAt(l) - A]-- - l++ - } - res = Math.max(res, r - l + 1) - r++ - } - - return res -}; - - diff --git a/2025-maximum-number-of-ways-to-partition-an-array.js b/2025-maximum-number-of-ways-to-partition-an-array.js index ed96ae1e..44abced0 100644 --- a/2025-maximum-number-of-ways-to-partition-an-array.js +++ b/2025-maximum-number-of-ways-to-partition-an-array.js @@ -37,54 +37,3 @@ const waysToPartition = function (nums, k) { } -// another - - -/** - * @param {number[]} nums - * @param {number} k - * @return {number} - */ -const waysToPartition = function (nums, k) { - const n = nums.length - const pref = Array(n).fill(0), - suff = Array(n).fill(0) - pref[0] = nums[0] - suff[n - 1] = nums[n - 1] - for (let i = 1; i < n; ++i) { - pref[i] = pref[i - 1] + nums[i] - suff[n - 1 - i] = suff[n - i] + nums[n - 1 - i] - } - let ans = 0 - const left = {}, - right = {} - - for (let i = 0; i < n - 1; ++i) { - const delta = pref[i] - suff[i + 1] - if (right[delta] == null) right[delta] = 0 - right[delta]++ - } - - if (right[0]) ans = right[0] - for (let i = 0; i < n; ++i) { - //find the number of pivot indexes when nums[i] is changed to k - let curr = 0, - diff = k - nums[i] - if (left[diff]) curr += left[diff] - if (right[-diff]) curr += right[-diff] - - //update answer - ans = Math.max(ans, curr) - - //transfer the current element from right to left - if (i < n - 1) { - let dd = pref[i] - suff[i + 1] - if(left[dd] == null) left[dd] = 0 - if(right[dd] == null) right[dd] = 0 - left[dd]++ - right[dd]-- - if (right[dd] == 0) delete right[dd] - } - } - return ans -} diff --git a/203-remove-linked-list-elements.js b/203-remove-linked-list-elements.js index 42807a2f..dd278dd7 100644 --- a/203-remove-linked-list-elements.js +++ b/203-remove-linked-list-elements.js @@ -28,22 +28,3 @@ const removeElements = function(head, val) { return dummy.next }; -// another - -/** - * Definition for singly-linked list. - * function ListNode(val) { - * this.val = val; - * this.next = null; - * } - */ -/** - * @param {ListNode} head - * @param {number} val - * @return {ListNode} - */ -const removeElements = function(head, val) { - if (head === null) return null; - head.next = removeElements(head.next, val); - return head.val === val ? head.next : head; -}; diff --git a/2033-minimum-operations-to-make-a-uni-value-grid.js b/2033-minimum-operations-to-make-a-uni-value-grid.js index edb3acc4..b5db02af 100644 --- a/2033-minimum-operations-to-make-a-uni-value-grid.js +++ b/2033-minimum-operations-to-make-a-uni-value-grid.js @@ -24,110 +24,3 @@ const minOperations = function (grid, x) { return res } -// another - -/** - * @param {number[][]} grid - * @param {number} x - * @return {number} - */ -const minOperations = function(grid, x) { - const arr = [] - const m = grid.length, n = grid[0].length - for(let i = 0; i < m; i++) { - for(let j = 0; j < n; j++) { - arr.push(grid[i][j]) - } - } - arr.sort((a, b) => a - b) - - for(let i = 1; i < m * n; i++) { - if((arr[i] - arr[i - 1]) % x !== 0) return -1 - } - const sum = arr.reduce((ac, e) => ac + e, 0) - const pre = [] - pre.push(arr[0]) - for(let i = 1; i < m * n; i++) { - pre[i] = pre[i - 1] + arr[i] - } - - let res = 0, num = 0, min = sum - arr[0] * m * n, idx = 0 - for(let i = 1; i < m * n; i++) { - const cur = (i + 1) * arr[i] - pre[i] + (sum - pre[i] - arr[i] * (m * n - i - 1)) - // console.log(cur, (i + 1) * arr[i] - pre[i], sum - pre[i] - arr[i] * (m * n - i - 1)) - // const cur = sum - arr[i] * (m * n - i) - if(cur < min) { - idx = i - min = cur - } - } - - // console.log(idx) - - for(let i = 0; i < m * n; i++) { - if(i === idx) continue - res += Math.abs(arr[i] - arr[idx]) / x - } - - return res -}; -// 20 - 6 - 4 * 2 -// 2 4 6 8 -// 1 2 3 5 - -// another - -/** - * @param {number[][]} grid - * @param {number} x - * @return {number} - */ -const minOperations = function(grid, x) { - const arr = [], m = grid.length, n = grid[0].length - for(let i = 0; i < m; i++) { - for(let j = 0; j < n; j++) { - arr.push(grid[i][j]) - } - } - arr.sort((a, b) => a - b) - const mid = arr[~~((m * n) / 2)] - let res = 0 - - for(let e of arr) { - if (e !== mid) { - const cur = Math.abs(e - mid) - if(cur % x !== 0) return -1 - res += cur / x - } - } - return res -}; - -// another - -/** - * @param {number[][]} grid - * @param {number} x - * @return {number} - */ -function minOperations(grid, x) { - const m = grid.length, n = grid[0].length, mn = m * n, arr = [] - for(let i = 0; i < m; i++) { - for(let j = 0; j < n; j++) { - arr.push(grid[i][j]) - } - } - arr.sort((a, b) => a - b) - const mid = arr[~~(mn / 2)] - let res = 0 - - for(let e of arr) { - if(e !== mid) { - const delta = Math.abs(e - mid) - if(delta % x !== 0) return -1 - res += delta / x - } - } - - return res -}; diff --git a/204-count-primes.js b/204-count-primes.js index 6882fda6..fddff31b 100755 --- a/204-count-primes.js +++ b/204-count-primes.js @@ -21,112 +21,3 @@ const countPrimes = function(n) { return res }; -// another - -/** - * @param {number} n - * @return {number} - */ -const countPrimes = function (n) { - const memo = Array(n).fill(false) - let res = 0 - for (let i = 2; i < n; i++) { - if (memo[i] === false) { - res++ - for (let j = 2; i * j < n; j++) { - memo[i * j] = true - } - } - } - - return res -} - - -// another - - -/** - * @param {number} n - * @return {number} - */ -const countPrimes = function(n) { - /** - * if n = 2, the prime 2 is not less than n, - * so there are no primes less than n - */ - if (n < 3) return 0; - - /** - * Start with the assumption that half the numbers below n are - * prime candidates, since we know that half of them are even, - * and so _in general_ aren't prime. - * - * An exception to this is 2, which is the only even prime. - * But also 1 is an odd which isn't prime. - * These two exceptions (a prime even and a for-sure not-prime odd) - * cancel each other out for n > 2, so our assumption holds. - * - * We'll decrement count when we find an odd which isn't prime. - * - * If n = 3, c = 1. - * If n = 5, c = 2. - * If n = 10, c = 5. - */ - let c = Math.floor(n / 2); - - /** - * Java initializes boolean arrays to {false}. - * In this method, we'll use truth to mark _composite_ numbers. - * - * This is the opposite of most Sieve of Eratosthenes methods, - * which use truth to mark _prime_ numbers. - * - * We will _NOT_ mark evens as composite, even though they are. - * This is because `c` is current after each `i` iteration below. - */ - let s = []; - - /** - * Starting with an odd prime-candidate above 2, increment by two - * to skip evens (which we know are not prime candidates). - */ - for (let i = 3; i * i < n; i += 2) { - if (s[i]) { - // c has already been decremented for this composite odd - continue; - } - - /** - * For each prime i, iterate through the odd composites - * we know we can form from i, and mark them as composite - * if not already marked. - * - * We know that i * i is composite. - * We also know that i * i + i is composite, since they share - * a common factor of i. - * Thus, we also know that i * i + a*i is composite for all real a, - * since they share a common factor of i. - * - * Note, though, that i * i + i _must_ be composite for an - * independent reason: it must be even. - * (all i are odd, thus all i*i are odd, - * thus all (odd + odd) are even). - * - * Recall that, by initializing c to n/2, we already accounted for - * all of the evens less than n being composite, and so marking - * i * i + (odd)*i as composite is needless bookkeeping. - * - * So, we can skip checking i * i + a*i for all odd a, - * and just increment j by even multiples of i, - * since all (odd + even) are odd. - */ - for (let j = i * i; j < n; j += 2 * i) { - if (!s[j]) { - c--; - s[j] = true; - } - } - } - return c; -}; diff --git a/2045-second-minimum-time-to-reach-destination.js b/2045-second-minimum-time-to-reach-destination.js index bec00cad..f7d60e41 100644 --- a/2045-second-minimum-time-to-reach-destination.js +++ b/2045-second-minimum-time-to-reach-destination.js @@ -50,45 +50,3 @@ const secondMinimum = (n, edges, time, change) => { return cost[n][1] } -// another - -/** - * @param {number} n - * @param {number[][]} edges - * @param {number} time - * @param {number} change - * @return {number} - */ - var secondMinimum = function (n, edges, time, change) { - const graph = new Map() - for (let i = 1; i <= n; i++) graph.set(i, []) - for (const [u, v] of edges) { - graph.get(u).push(v) - graph.get(v).push(u) - } - const first = Array(n + 1).fill(Infinity) - const second = Array(n + 1).fill(Infinity) - first[1] = 0 - - const q = new MinPriorityQueue() - q.enqueue(1, 0) - - while (q.size()) { - let {element: node, priority: cur} = q.dequeue() - cur += time // cur: arrival time - let leave = cur - if (~~(cur / change) & 1) leave += change - (cur % change) - for (let nei of graph.get(node)) { - if (second[nei] <= cur) continue - if (first[nei] === cur) continue - if (first[nei] > cur) { - second[nei] = first[nei] - first[nei] = cur - } else { - second[nei] = cur - } - q.enqueue(nei, leave) - } - } - return second[n] -} diff --git a/2049-count-nodes-with-the-highest-score.js b/2049-count-nodes-with-the-highest-score.js index 5be74015..955c74cc 100644 --- a/2049-count-nodes-with-the-highest-score.js +++ b/2049-count-nodes-with-the-highest-score.js @@ -25,34 +25,3 @@ const countHighestScoreNodes = function(parents) { return hash[maxKey] }; -// another - -/** - * @param {number[]} parents - * @return {number} - */ -const countHighestScoreNodes = function(parents) { - const n = parents.length, hash = {}, graph = {} - for(let i = 1; i < n; i++) { - if(graph[parents[i]] == null) graph[parents[i]] = [] - graph[parents[i]].push(i) - } - - dfs(0) - const mk = Math.max(...Object.keys(hash)) - return hash[mk] - - function dfs(i) { - let num = 0, prod = 1 - for(const e of (graph[i] || []) ) { - const tmp = dfs(e) - num += tmp - prod *= tmp - } - - if(n - 1 - num > 0) prod *= (n - 1 - num) - hash[prod] = (hash[prod] || 0) + 1 - - return num + 1 - } -}; diff --git a/2050-parallel-courses-iii.js b/2050-parallel-courses-iii.js index 1be35f37..78006b74 100644 --- a/2050-parallel-courses-iii.js +++ b/2050-parallel-courses-iii.js @@ -41,80 +41,3 @@ const minimumTime = function (n, relations, time) { return Math.max(...dist) } -// another - -/** - * @param {number} n - * @param {number[][]} relations - * @param {number[]} time - * @return {number} - */ -const minimumTime = function(n, relations, time) { - const graph = {}, dist = Array(n).fill(0), inDegree = Array(n).fill(0) - - for(let [pre, next] of relations) { - pre--, next-- - if(graph[pre] == null) graph[pre] = [] - graph[pre].push(next) - inDegree[next]++ - } - - const q = [] - for(let i = 0; i < n; i++) { - if(inDegree[i] === 0) { - q.push(i) - dist[i] = time[i] - } - } - - let res = 0 - while(q.length) { - const cur = q.shift() - for(const next of (graph[cur] || [])) { - dist[next] = Math.max(dist[next], dist[cur] + time[next]) - inDegree[next]-- - if(inDegree[next] === 0) q.push(next) - } - } - - return Math.max(...dist) -} - -// another - -/** - * @param {number} n - * @param {number[][]} relations - * @param {number[]} time - * @return {number} - */ -const minimumTime = function(n, relations, time) { - const graph = {}, dist = Array(n).fill(0), inDegree = Array(n).fill(0) - for(let [from, to] of relations) { - from--, to-- - if (graph[from] == null) graph[from] = [] - graph[from].push(to) - inDegree[to]++ - } - const q = [] - for(let i = 0; i < n; i++) { - if(inDegree[i] === 0) { - q.push(i) - dist[i] = time[i] - } - } - - while(q.length) { - const u = q.shift() - for(const v of (graph[u] || [])) { - dist[v] = Math.max(dist[v], dist[u] + time[v]) - if(--inDegree[v] === 0) q.push(v) - } - } - - let res = 0 - for(let e of dist) { - if(e > res) res = e - } - return res -}; diff --git a/2054-two-best-non-overlapping-events.js b/2054-two-best-non-overlapping-events.js index 07adffa0..3b8c3bc5 100644 --- a/2054-two-best-non-overlapping-events.js +++ b/2054-two-best-non-overlapping-events.js @@ -29,90 +29,3 @@ const maxTwoEvents = function(events) { } }; -// another - -/** - * @param {number[][]} events - * @return {number} - */ -const maxTwoEvents = function(events) { - const n = events.length, { max } = Math - let res = 0, maxVal = 0; - const pq = new PriorityQueue((a, b) => a[0] < b[0]); - events.sort((a, b) => a[0] === b[0] ? a[1] - b[1] : a[0] - b[0]) - for (let e of events) { - for(; !pq.isEmpty() && pq.peek()[0] < e[0]; pq.pop()) - maxVal = max(maxVal, pq.peek()[1]); - res = max(res, maxVal + e[2]); - pq.push([e[1], e[2]]); - } - return res; -}; - -class PriorityQueue { - constructor(comparator = (a, b) => a > b) { - this.heap = [] - this.top = 0 - this.comparator = comparator - } - size() { - return this.heap.length - } - isEmpty() { - return this.size() === 0 - } - peek() { - return this.heap[this.top] - } - push(...values) { - values.forEach((value) => { - this.heap.push(value) - this.siftUp() - }) - return this.size() - } - pop() { - const poppedValue = this.peek() - const bottom = this.size() - 1 - if (bottom > this.top) { - this.swap(this.top, bottom) - } - this.heap.pop() - this.siftDown() - return poppedValue - } - replace(value) { - const replacedValue = this.peek() - this.heap[this.top] = value - this.siftDown() - return replacedValue - } - - parent = (i) => ((i + 1) >>> 1) - 1 - left = (i) => (i << 1) + 1 - right = (i) => (i + 1) << 1 - greater = (i, j) => this.comparator(this.heap[i], this.heap[j]) - swap = (i, j) => ([this.heap[i], this.heap[j]] = [this.heap[j], this.heap[i]]) - siftUp = () => { - let node = this.size() - 1 - while (node > this.top && this.greater(node, this.parent(node))) { - this.swap(node, this.parent(node)) - node = this.parent(node) - } - } - siftDown = () => { - let node = this.top - while ( - (this.left(node) < this.size() && this.greater(this.left(node), node)) || - (this.right(node) < this.size() && this.greater(this.right(node), node)) - ) { - let maxChild = - this.right(node) < this.size() && - this.greater(this.right(node), this.left(node)) - ? this.right(node) - : this.left(node) - this.swap(node, maxChild) - node = maxChild - } - } -} diff --git a/2055-plates-between-candles.js b/2055-plates-between-candles.js index bca0a066..52a42618 100644 --- a/2055-plates-between-candles.js +++ b/2055-plates-between-candles.js @@ -45,86 +45,3 @@ const platesBetweenCandles = function (s, queries) { } } -// another - -/** - * @param {string} s - * @param {number[][]} queries - * @return {number[]} - */ -const platesBetweenCandles = function(s, queries) { - const n = s.length, - leftCandlePos = Array(n).fill(-1) - rightCandlePos = Array(n).fill(-1) - candleCnt = Array(n).fill(0) - let pos = -1 - for(let i = 0; i < n; i++) { - if(s[i] === '|') pos = i - leftCandlePos[i] = pos - } - pos = -1 - for(let i = n - 1; i >= 0; i--) { - if(s[i] === '|') pos = i - rightCandlePos[i] = pos - } - for(let i = 0, cnt = 0; i < n; i++) { - if(s[i] === '|') cnt++ - candleCnt[i] = cnt - } - - const len = queries.length, res = Array(len).fill(0) - - for(let i = 0; i < len; i++) { - const [left, right] = queries[i] - const leftCandle = rightCandlePos[left], rightCandle = leftCandlePos[right] - const delta = rightCandle - leftCandle - if(leftCandle !== -1 && rightCandle !== -1 && delta > 1) { - res[i] = delta + 1 - (candleCnt[rightCandle] - candleCnt[leftCandle] + 1) - } - } - - return res -} - -// another - -/** - * @param {string} s - * @param {number[][]} queries - * @return {number[]} - */ -const platesBetweenCandles = function (s, queries) { - const n = s.length - const leftArr = Array(n).fill(-1), - rightArr = Array(n).fill(n), - candleCnt = Array(n).fill(0) - let candle = -1 - for (let i = 0; i < n; i++) { - if (s[i] === '|') candle = i - leftArr[i] = candle - } - candle = n - for (let i = n - 1; i >= 0; i--) { - if (s[i] === '|') candle = i - rightArr[i] = candle - } - let cnt = 0 - for (let i = 0; i < n; i++) { - if (s[i] === '|') cnt++ - candleCnt[i] = cnt - } - // console.log(leftArr, rightArr) - const res = [] - for (const [s, e] of queries) { - const l = rightArr[s] - const r = leftArr[e] - const diff = r - l - if (diff > 1) { - const e = r - l + 1 - (candleCnt[r] - candleCnt[l] + 1) - res.push(e) - } else res.push(0) - } - - return res -} - diff --git a/206-reverse-linked-list.js b/206-reverse-linked-list.js index 82ccda9a..5c3ef392 100755 --- a/206-reverse-linked-list.js +++ b/206-reverse-linked-list.js @@ -23,31 +23,3 @@ const reverseList = function(head) { return pre.next }; -// another - -/** - * Definition for singly-linked list. - * function ListNode(val) { - * this.val = val; - * this.next = null; - * } - */ -/** - * @param {ListNode} head - * @return {ListNode} - */ -const reverseList = function(head) { - let prev = null; - let cur = head; - let tmp; - let tmpNext; - while (cur !== null) { - tmp = cur; - tmpNext = cur.next; - cur.next = prev; - prev = tmp; - cur = tmpNext; - } - - return prev; -}; diff --git a/2060-check-if-an-original-string-exists-given-two-encoded-strings.js b/2060-check-if-an-original-string-exists-given-two-encoded-strings.js index 12be2ab2..8c5e333d 100644 --- a/2060-check-if-an-original-string-exists-given-two-encoded-strings.js +++ b/2060-check-if-an-original-string-exists-given-two-encoded-strings.js @@ -57,127 +57,3 @@ function isDigit(char) { return (/^\d{1}$/g).test(char); } -// another - -/** - * @param {string} s1 - * @param {string} s2 - * @return {boolean} - */ -const possiblyEquals = function(s1, s2) { - const n = s1.length - const m = s2.length - const memo = Array.from({ length: n + 1 }, () => - Array.from({ length: m + 1 }, () => Array(1001).fill(null)) - ) - memo[0][0][1000] = true - - return dfs(0, 0, 0) - - function dfs(i, j, diff) { - if(memo[i][j][diff] != null) return memo[i][j][diff] - let res = false - if (i == n && j == m) res = diff === 0 - else if (i < n && isDigit(s1[i])) { - let ii = i - while (ii < n && isDigit( s1[ii] )) ii += 1 - for (let x of helper(s1.slice(i, ii))) { - if (dfs(ii, j, diff-x)) res = true - } - } else if (j < m && isDigit( s2[j] )) { - let jj = j - while (jj < m && isDigit( s2[jj] )) jj += 1 - for (let y of helper(s2.slice(j, jj))) { - if (dfs(i, jj, diff+y)) res = true - } - } else if (diff == 0) { - if (i < n && j < m && s1[i] == s2[j]) res = dfs(i+1, j+1, 0) - } else if (diff > 0) { - if (i < n) res = dfs(i+1, j, diff-1) - } else { - if (j < m) res = dfs(i, j+1, diff+1) - } - - memo[i][j][diff] = res - return res - } - - function isDigit(ch) { - return ch >= '0' && ch <= '9' - } - - function helper(str) { - const ans = new Set() - ans.add(+str) - for(let i = 1, len = str.length; i < len; i++) { - const pre = helper(str.slice(0, i)) - const post = helper(str.slice(i)) - for(let p of pre) { - for(let n of post) { - ans.add(p + n) - } - } - } - return Array.from(ans) - } -}; - -// another - -/** - * @param {string} s1 - * @param {string} s2 - * @return {boolean} - */ -var possiblyEquals = function (s1, s2) { - let n = s1.length - let m = s2.length - const f = Array.from({ length: n + 1 }, () => - Array.from({ length: m + 1 }, () => Array(1001).fill(false)) - ) - f[0][0][1000] = true - - for (let i = 0; i <= n; i++) - for (let j = 0; j <= m; j++) - for (let k = 0; k < 2000; k++) { - if (!f[i][j][k]) continue - // if k==1000 means length diff is 0, so check both next charactors. - if (i + 1 <= n && j + 1 <= m && k == 1000 && s1[i] == s2[j]) { - f[i + 1][j + 1][k] = true - } - // if first string is longer or same length, extend second string. - if (k >= 1000 && j + 1 <= m) { - if (s2[j] >= 'a' && s2[j] <= 'z') { - // do not extend to be a longer string using a-z. - if (k > 1000) { - f[i][j + 1][k - 1] = true - } - } else if (s2[j] > '0') { - let cur = 0 - for (let r = j; r < m; r++) { - if (s2[r] >= '0' && s2[r] <= '9') { - cur = cur * 10 + (s2[r] - '0') - f[i][r + 1][k - cur] = true - } else break - } - } - } - // if second string is longer or same length, extend first string. - if (k <= 1000 && i + 1 <= n) { - if (s1[i] >= 'a' && s1[i] <= 'z') { - if (k < 1000) { - f[i + 1][j][k + 1] = true - } - } else if (s1[i] > '0') { - let cur = 0 - for (let r = i; r < n; r++) { - if (s1[r] >= '0' && s1[r] <= '9') { - cur = cur * 10 + (s1[r] - '0') - f[r + 1][j][k + cur] = true - } else break - } - } - } - } - return f[n][m][1000] -} diff --git a/2064-minimized-maximum-of-products-distributed-to-any-store.js b/2064-minimized-maximum-of-products-distributed-to-any-store.js index 577fe013..5e904a89 100644 --- a/2064-minimized-maximum-of-products-distributed-to-any-store.js +++ b/2064-minimized-maximum-of-products-distributed-to-any-store.js @@ -24,55 +24,3 @@ const minimizedMaximum = function(n, quantities) { } }; -// another - - -/** - * @param {number} n - * @param {number[]} quantities - * @return {number} - */ -var minimizedMaximum = function(n, quantities) { - let MAX = 0; - for (let x of quantities) MAX = Math.max(x, MAX); - let l = 1, r = MAX; - while (l < r) { - let mid = Math.floor((l + r) / 2); - if (valid(quantities, mid) <= n) r = mid; - else l = mid + 1; - } - return l; -}; - - - - function valid(quantities, max) { - let cnt = 0; - for (let x of quantities) cnt += Math.floor(x / max) + ((x % max) ? 1 : 0); - return cnt; - } - -// another - -/** - * @param {number} n - * @param {number[]} quantities - * @return {number} - */ -const minimizedMaximum = function(n, quantities) { - let MAX = 0; - for (let x of quantities) MAX = Math.max(x, MAX); - let l = 1, r = MAX; - while (l < r) { - let mid = Math.floor((l + r) / 2); - if (valid(quantities, mid, n)) l = mid + 1; - else r = mid; - } - return l; -}; - -function valid(quantities, max, n) { - let cnt = 0; - for (let x of quantities) cnt += Math.floor(x / max) + ((x % max) ? 1 : 0); - return cnt > n; -} diff --git a/2065-maximum-path-quality-of-a-graph.js b/2065-maximum-path-quality-of-a-graph.js index ced6bb56..1c4f92f2 100644 --- a/2065-maximum-path-quality-of-a-graph.js +++ b/2065-maximum-path-quality-of-a-graph.js @@ -30,84 +30,3 @@ const maximalPathQuality = function(values, edges, maxTime) { } }; -// another - - -/** - * @param {number[]} values - * @param {number[][]} edges - * @param {number} maxTime - * @return {number} - */ -const maximalPathQuality = function(values, edges, maxTime) { - const graph = {}, n = values.length - for(const [u, v, t] of edges) { - if(graph[u] == null) graph[u] = [] - if(graph[v] == null) graph[v] = [] - graph[u].push([v, t]) - graph[v].push([u, t]) - } - let res = 0, visited = Array(n).fill(false) - bt(0, 0, 0) - return res - - function bt(i, cur, time) { - if(time > maxTime) return - const backup = visited[i] - if(!visited[i]) { - visited[i] = true - cur += values[i] - } - - if(i === 0) { - res = Math.max(res, cur) - } - - for(const [next, nextTime] of (graph[i] || [])) { - bt(next, cur, time + nextTime) - } - visited[i] = backup - } -}; - -// another - -/** - * @param {number[]} values - * @param {number[][]} edges - * @param {number} maxTime - * @return {number} - */ -const maximalPathQuality = function(values, edges, maxTime) { - let zeroMax = 0; - let n = values.length; - let ll = Array.from({length: n + 1}, () => []) - for (let edge of edges) { - let u = edge[0]; - let v = edge[1]; - let t = edge[2]; - ll[u].push([v, t]); - ll[v].push([u, t]); - } - const visited = Array(n + 1).fill(false); - dfs(0, 0, 0, maxTime, visited); - return zeroMax; - - function dfs(val, curNode, curTime, maxTime, visited) { - if (curTime > maxTime) { - return; - } - let before = visited[curNode]; - if (!visited[curNode]) { - val += values[curNode]; - visited[curNode] = true; - } - if (curNode == 0) { - zeroMax = Math.max(zeroMax, val); - } - for (let next of (ll[curNode] || [])) { - dfs(val, next[0], curTime + next[1], maxTime, visited); - } - visited[curNode] = before; - } -}; diff --git a/207-course-schedule.js b/207-course-schedule.js index 09233baa..34825256 100644 --- a/207-course-schedule.js +++ b/207-course-schedule.js @@ -38,155 +38,3 @@ const canFinish = function (numCourses, prerequisites) { return set.size === numCourses } -// another - - -/** - * @param {number} numCourses - * @param {number[][]} prerequisites - * @return {boolean} - */ -const canFinish = function(numCourses, prerequisites) { - const [graph, inDegree] = buildGraph(numCourses, prerequisites) - - const q = [] - for(let i = 0; i < numCourses; i++) { - if(inDegree.get(i) == null) q.push(i) - } - let num = 0 - while(q.length) { - const pre = q.pop() - num++ - for(const next of (graph.get(pre) || [])) { - inDegree.set(next, inDegree.get(next) - 1) - if(inDegree.get(next) === 0) q.push(next) - } - } - return num === numCourses - - - function buildGraph(n, arr) { - const res = new Map(), inDegree = new Map() - for(const [cur, pre] of arr) { - if(res.get(pre) == null) res.set(pre, new Set()) - res.get(pre).add(cur) - if(inDegree.get(cur) == null) inDegree.set(cur, 0) - inDegree.set(cur, inDegree.get(cur) + 1) - } - return [res, inDegree] - } -}; - -// another - -/** - * @param {number} numCourses - * @param {number[][]} prerequisites - * @return {boolean} - */ -const canFinish = function(numCourses, prerequisites) { - const seen = new Set() - const seeing = new Set() - const adj = [...Array(numCourses)].map(r => []) - for (let [u, v] of prerequisites) { - adj[v].push(u) - } - for (let c = 0; c < numCourses; c++) { - if (!dfs(c)) { - return false - } - } - return true - function dfs(v) { - if (seen.has(v)) return true - if (seeing.has(v)) return false - seeing.add(v) - for (let nv of adj[v]) { - if (!dfs(nv)) { - return false - } - } - seeing.delete(v) - seen.add(v) - return true - } -} - -// another - -/** - * @param {number} numCourses - * @param {number[][]} prerequisites - * @return {boolean} - */ -const canFinish = function(vertices, edges) { - const sortedOrder = [] - if (vertices <= 0) { - return sortedOrder - } - const inDegree = Array(vertices).fill(0) - const graph = Array(vertices) - .fill(0) - .map(() => Array()) - edges.forEach(edge => { - let parent = edge[0] - let child = edge[1] - graph[parent].push(child) - inDegree[child]++ - }) - const sources = [] - for (let i = 0; i < inDegree.length; i++) { - if (inDegree[i] === 0) { - sources.push(i) - } - } - while (sources.length > 0) { - const vertex = sources.shift() - sortedOrder.push(vertex) - - graph[vertex].forEach(child => { - inDegree[child] -= 1 - if (inDegree[child] === 0) { - sources.push(child) - } - }) - } - return sortedOrder.length === vertices ? true : false -} - -// another - -/** - * @param {number} numCourses - * @param {number[][]} prerequisites - * @return {boolean} - */ -const canFinish = function(numCourses, prerequisites) { - const set = new Set(), hash = {} - for(let i = 0; i < prerequisites.length; i++) { - const [cur, pre] = prerequisites[i] - if(hash[cur] == null) hash[cur] = new Set() - hash[cur].add(pre) - } - const q = [] - - for(let i = 0; i < numCourses; i++) { - if(hash[i] == null) q.push(i) - } - let visited = 0 - - while(q.length) { - const cur = q.shift() - visited++ - Object.keys(hash).forEach(k => { - if(hash[k].has(cur)) { - hash[k].delete(cur) - } - if(hash[k].size === 0) { - delete hash[k] - q.push(+k) - } - }) - } - return visited === numCourses -}; diff --git a/208-implement-trie-prefix-tree.js b/208-implement-trie-prefix-tree.js index 30c460ea..c40a4718 100644 --- a/208-implement-trie-prefix-tree.js +++ b/208-implement-trie-prefix-tree.js @@ -52,101 +52,3 @@ Trie.prototype.traverse = function(word) { return curr } -// another - -class Trie { - constructor() { - this.links = new Map(); - this.isWord = false; - } - insert(word) { - let node = this; - for (const c of word) { - if (!node.links.has(c)) node.links.set(c, new Trie()); - node = node.links.get(c); - } - node.isWord = true; - } - search(word) { - const node = this.traverse(word); - return node ? node.isWord : false; - } - startsWith(prefix) { - const node = this.traverse(prefix); - return node !== null; - } - traverse(word) { - let node = this; - for (const c of word) { - if (node.links.has(c)) node = node.links.get(c); - else return null; - } - return node; - } -} - -// another -/** - * Initialize your data structure here. - */ -const Trie = function () { - this.root = new Node(null) -} - -/** - * Inserts a word into the trie. - * @param {string} word - * @return {void} - */ -Trie.prototype.insert = function (word) { - let cur = this.root - for (let i = 0, len = word.length; i < len; i++) { - if (!cur.children.has(word[i])) cur.children.set(word[i], new Node(null)) - cur = cur.children.get(word[i]) - if (i === len - 1) cur.word = true - } -} - -/** - * Returns if the word is in the trie. - * @param {string} word - * @return {boolean} - */ -Trie.prototype.search = function (word) { - let cur = this.root - for (let i = 0, len = word.length; i < len; i++) { - if (cur.children.has(word[i])) cur = cur.children.get(word[i]) - else return false - if (i === len - 1) return cur.word === true - } -} - -/** - * Returns if there is any word in the trie that starts with the given prefix. - * @param {string} prefix - * @return {boolean} - */ -Trie.prototype.startsWith = function (prefix) { - let cur = this.root - for (let i = 0, len = prefix.length; i < len; i++) { - if (cur.children.has(prefix[i])) cur = cur.children.get(prefix[i]) - else return false - if (i === len - 1) return true - } -} - -class Node { - constructor(v) { - this.val = v - this.word = false - this.children = new Map() - } -} - -/** - * Your Trie object will be instantiated and called as such: - * var obj = new Trie() - * obj.insert(word) - * var param_2 = obj.search(word) - * var param_3 = obj.startsWith(prefix) - */ diff --git a/2092-find-all-people-with-secret.js b/2092-find-all-people-with-secret.js index fc592271..c62d3ab0 100644 --- a/2092-find-all-people-with-secret.js +++ b/2092-find-all-people-with-secret.js @@ -48,48 +48,3 @@ class UnionFind { } } -// another - -/** - * @param {number} n - * @param {number[][]} meetings - * @param {number} firstPerson - * @return {number[]} - */ -const findAllPeople = function(n, meetings, firstPerson) { - meetings.sort((a, b) => a[2] - b[2]) - const shared = new Set([0, firstPerson]) - - let start = new Set(), links = {} - for(let i = 0, len = meetings.length; i < len; i++) { - const [x,y,t] = meetings[i] - if(i > 0 && t !== meetings[i - 1][2]) { - bfs(start, links) - start = new Set() - links = {} - } - if(shared.has(x)) start.add(x) - if(shared.has(y)) start.add(y) - if(links[x] == null) links[x] = [] - if(links[y] == null) links[y] = [] - links[x].push(y) - links[y].push(x) - } - - bfs(start, links) - return Array.from(shared) - - function bfs(start, links) { - const visited = new Set() - while(start.size) { - const it = start[Symbol.iterator]() - const cur = it.next().value - start.delete(cur) - visited.add(cur) - shared.add(cur) - for(let e of (links[cur] || [])) { - if(!visited.has(e)) start.add(e) - } - } - } -}; diff --git a/21-merge-two-sorted-lists.js b/21-merge-two-sorted-lists.js index 3d094664..d4e2dd87 100755 --- a/21-merge-two-sorted-lists.js +++ b/21-merge-two-sorted-lists.js @@ -23,36 +23,3 @@ const mergeTwoLists = function(l1, l2) { } }; -// another - -/** - * Definition for singly-linked list. - * function ListNode(val, next) { - * this.val = (val===undefined ? 0 : val) - * this.next = (next===undefined ? null : next) - * } - */ -/** - * @param {ListNode} l1 - * @param {ListNode} l2 - * @return {ListNode} - */ -const mergeTwoLists = function(l1, l2) { - const dummy = new ListNode() - let cur = dummy - while(l1 && l2) { - if(l1.val < l2.val) { - cur.next = new ListNode(l1.val) - l1 = l1.next - } else { - cur.next = new ListNode(l2.val) - l2 = l2.next - } - - cur = cur.next - } - if(l1) cur.next = l1 - if(l2) cur.next = l2 - - return dummy.next -}; diff --git a/210-course-schedule-ii.js b/210-course-schedule-ii.js index 350c4d33..d9f257c9 100644 --- a/210-course-schedule-ii.js +++ b/210-course-schedule-ii.js @@ -33,74 +33,3 @@ const findOrder = function(numCourses, prerequisites) { return res.length === numCourses ? res : [] } -// another - -/** - * @param {number} numCourses - * @param {number[][]} prerequisites - * @return {number[]} - */ -const findOrder = function(numCourses, prerequisites) { - const indegree = new Array(numCourses).fill(0) - const graph = {} - for (let [course, prereq] of prerequisites) { - indegree[course]++ - graph[prereq] === undefined - ? (graph[prereq] = [course]) - : graph[prereq].push(course) - } - const queue = [], - ans = [] - for (let i = 0; i < indegree.length; i++) if (!indegree[i]) queue.push(i) - while (queue.length) { - let cur = queue.shift() - ans.push(cur) - for (let neigbhors of graph[cur] || []) { - if (!--indegree[neigbhors]) queue.push(neigbhors) - } - } - return ans.length === numCourses ? ans : [] -} - -// another - -/** - * @param {number} numCourses - * @param {number[][]} prerequisites - * @return {number[]} - */ -const findOrder = function(numCourses, prerequisites) { - const seen = new Set() - const seeing = new Set() - const res = [] - - const adj = [...Array(numCourses)].map(r => []) - for (let [u, v] of prerequisites) { - adj[v].push(u) - } - for (let c = 0; c < numCourses; c++) { - if (!dfs(c)) { - return [] - } - } - return res.reverse() - - function dfs(v) { - if (seen.has(v)) { - return true - } - if (seeing.has(v)) { - return false - } - seeing.add(v) - for (let nv of adj[v]) { - if (!dfs(nv)) { - return false - } - } - seeing.delete(v) - seen.add(v) - res.push(v) - return true - } -} diff --git a/2101-detonate-the-maximum-bombs.js b/2101-detonate-the-maximum-bombs.js index 7abf5737..f70a41df 100644 --- a/2101-detonate-the-maximum-bombs.js +++ b/2101-detonate-the-maximum-bombs.js @@ -37,46 +37,3 @@ function bombAdj(source, target) { return abs(x1 - x2) ** 2 + abs(y1 - y2) ** 2 <= r1 ** 2 } -// another - -/** - * @param {number[][]} bombs - * @return {number} - */ -const maximumDetonation = function(bombs) { - const n = bombs.length, graph = {} - for(let i = 0; i < n; i++) { - for(let j = 0; j < n; j++) { - if(i === j) continue - if(adjValid(bombs[i], bombs[j])) { - if(graph[i] == null) graph[i] = [] - graph[i].push(j) - } - } - } - - let res = 0 - for(let i = 0; i < n; i++) { - const set = new Set([i]) - dfs(i, set) - res = Math.max(res, set.size) - } - return res - - function dfs(node, visited){ - for (const e of (graph[node] || [])) { - if(!visited.has(e)) { - visited.add(e) - dfs(e, visited) - } - } - } - - function adjValid(start, target) { - const [sx, sy, r] = start - const [ex, ey] = target - return Math.abs(sx - ex) ** 2 + Math.abs(sy - ey) ** 2 <= r ** 2 - } -}; - - diff --git a/2102-sequentially-ordinal-rank-tracker.js b/2102-sequentially-ordinal-rank-tracker.js index 3e2f8c83..01e7cee3 100644 --- a/2102-sequentially-ordinal-rank-tracker.js +++ b/2102-sequentially-ordinal-rank-tracker.js @@ -106,229 +106,3 @@ class PQ { } } -// another - -const maxComp = (a, b) => { - return a[1] === b[1] ? b[0].localeCompare(a[0]) > 0 : a[1] > b[1] -} - -const minComp = (a, b) => { - return a[1] === b[1] ? a[0].localeCompare(b[0]) > 0: a[1] < b[1] -} - -const SORTracker = function() { - // max - this.pq = new PriorityQueue(maxComp) - // min - this.best = new PriorityQueue(minComp) -}; - -/** - * @param {string} name - * @param {number} score - * @return {void} - */ -SORTracker.prototype.add = function(name, score) { - this.pq.push([name, score]) - while(!this.best.isEmpty() && maxComp(this.pq.peek(), this.best.peek())) { - const a = this.best.pop(), b = this.pq.pop() - this.best.push(b) - this.pq.push(a) - } -}; - -/** - * @return {string} - */ -SORTracker.prototype.get = function() { - const tmp = this.pq.pop() - this.best.push(tmp) - return tmp[0] -}; - -/** - * Your SORTracker object will be instantiated and called as such: - * var obj = new SORTracker() - * obj.add(name,score) - * var param_2 = obj.get() - */ - -class PriorityQueue { - constructor(comparator = (a, b) => a > b) { - this.heap = [] - this.top = 0 - this.comparator = comparator - } - size() { - return this.heap.length - } - isEmpty() { - return this.size() === 0 - } - peek() { - return this.heap[this.top] - } - push(...values) { - values.forEach((value) => { - this.heap.push(value) - this.siftUp() - }) - return this.size() - } - pop() { - const poppedValue = this.peek() - const bottom = this.size() - 1 - if (bottom > this.top) { - this.swap(this.top, bottom) - } - this.heap.pop() - this.siftDown() - return poppedValue - } - replace(value) { - const replacedValue = this.peek() - this.heap[this.top] = value - this.siftDown() - return replacedValue - } - - parent = (i) => ((i + 1) >>> 1) - 1 - left = (i) => (i << 1) + 1 - right = (i) => (i + 1) << 1 - greater = (i, j) => this.comparator(this.heap[i], this.heap[j]) - swap = (i, j) => ([this.heap[i], this.heap[j]] = [this.heap[j], this.heap[i]]) - siftUp = () => { - let node = this.size() - 1 - while (node > this.top && this.greater(node, this.parent(node))) { - this.swap(node, this.parent(node)) - node = this.parent(node) - } - } - siftDown = () => { - let node = this.top - while ( - (this.left(node) < this.size() && this.greater(this.left(node), node)) || - (this.right(node) < this.size() && this.greater(this.right(node), node)) - ) { - let maxChild = - this.right(node) < this.size() && - this.greater(this.right(node), this.left(node)) - ? this.right(node) - : this.left(node) - this.swap(node, maxChild) - node = maxChild - } - } -} - - -// another - -const SORTracker = function() { - this.maxCmp = (a, b) => a[1] === b[1] ? a[0] < b[0] : a[1] > b[1] - this.minCmp = (a, b) => a[1] === b[1] ? a[0] > b[0] : a[1] < b[1] - this.maxQ = new PriorityQueue(this.maxCmp) - this.minQ = new PriorityQueue(this.minCmp) - this.cnt = 0 -}; - -/** - * @param {string} name - * @param {number} score - * @return {void} - */ -SORTracker.prototype.add = function(name, score) { - this.maxQ.push([name, score]) -}; - -/** - * @return {string} - */ -SORTracker.prototype.get = function() { - if(this.cnt) { - this.minQ.push(this.maxQ.pop()) - while(this.maxCmp(this.maxQ.peek(), this.minQ.peek())) { - const tmp = this.minQ.pop() - this.minQ.push(this.maxQ.pop()) - this.maxQ.push(tmp) - } - } - this.cnt++ - - return this.maxQ.peek()[0] -}; - -/** - * Your SORTracker object will be instantiated and called as such: - * var obj = new SORTracker() - * obj.add(name,score) - * var param_2 = obj.get() - */ - -class PriorityQueue { - constructor(comparator = (a, b) => a > b) { - this.heap = [] - this.top = 0 - this.comparator = comparator - } - size() { - return this.heap.length - } - isEmpty() { - return this.size() === 0 - } - peek() { - return this.heap[this.top] - } - push(...values) { - values.forEach((value) => { - this.heap.push(value) - this.siftUp() - }) - return this.size() - } - pop() { - const poppedValue = this.peek() - const bottom = this.size() - 1 - if (bottom > this.top) { - this.swap(this.top, bottom) - } - this.heap.pop() - this.siftDown() - return poppedValue - } - replace(value) { - const replacedValue = this.peek() - this.heap[this.top] = value - this.siftDown() - return replacedValue - } - - parent = (i) => ((i + 1) >>> 1) - 1 - left = (i) => (i << 1) + 1 - right = (i) => (i + 1) << 1 - greater = (i, j) => this.comparator(this.heap[i], this.heap[j]) - swap = (i, j) => ([this.heap[i], this.heap[j]] = [this.heap[j], this.heap[i]]) - siftUp = () => { - let node = this.size() - 1 - while (node > this.top && this.greater(node, this.parent(node))) { - this.swap(node, this.parent(node)) - node = this.parent(node) - } - } - siftDown = () => { - let node = this.top - while ( - (this.left(node) < this.size() && this.greater(this.left(node), node)) || - (this.right(node) < this.size() && this.greater(this.right(node), node)) - ) { - let maxChild = - this.right(node) < this.size() && - this.greater(this.right(node), this.left(node)) - ? this.right(node) - : this.left(node) - this.swap(node, maxChild) - node = maxChild - } - } -} diff --git a/2104-sum-of-subarray-ranges.js b/2104-sum-of-subarray-ranges.js index 2feb3f2e..bd22db1e 100644 --- a/2104-sum-of-subarray-ranges.js +++ b/2104-sum-of-subarray-ranges.js @@ -17,21 +17,3 @@ const subArrayRanges = function(nums) { return res }; -// another - -/** - * @param {number[]} nums - * @return {number} - */ -const subArrayRanges = function(nums) { - let res = 0, n = nums.length - for(let i = 0; i < n; i++) { - let max = nums[i], min = nums[i] - for(let j = i; j < n; j++) { - max = Math.max(max, nums[j]) - min = Math.min(min, nums[j]) - res += max - min - } - } - return res -}; diff --git a/2111-minimum-operations-to-make-the-array-k-increasing.js b/2111-minimum-operations-to-make-the-array-k-increasing.js index 821bceef..0c3a2c05 100644 --- a/2111-minimum-operations-to-make-the-array-k-increasing.js +++ b/2111-minimum-operations-to-make-the-array-k-increasing.js @@ -39,49 +39,3 @@ const kIncreasing = function(arr, k) { } }; -// another -/** - * @param {number[]} arr - * @param {number} k - * @return {number} - */ -const kIncreasing = function(arr, k) { - const n = arr.length - const a = Array.from({ length: k }, () => Array()) - - for(let i = 0; i < k; i++) { - for(let j = i; j < n; j += k) { - a[i].push(arr[j]) - } - } - - let res = 0 - for(let i = 0; i < a.length; i++) { - const r = a[i] - res += r.length - lis(r) - } - - return res - - function bisect_right(a, x, lo = 0, hi = null) { // > upper_bound - if (lo < 0) throw new Error('lo must be non-negative'); - if (hi == null) hi = a.length; - while (lo < hi) { - let mid = parseInt((lo + hi) / 2); - x < a[mid] ? hi = mid : lo = mid + 1; - } - return lo; - } - - function lis(ar) { - let q = [] - for (let x of ar) { - let i = bisect_right(q, x) - if (i == q.length) q.push(x) - else q[i] = x - } - - return q.length - } -}; - diff --git a/2115-find-all-possible-recipes-from-given-supplies.js b/2115-find-all-possible-recipes-from-given-supplies.js index 856512dd..c425ceb7 100644 --- a/2115-find-all-possible-recipes-from-given-supplies.js +++ b/2115-find-all-possible-recipes-from-given-supplies.js @@ -39,57 +39,3 @@ const findAllRecipes = function(recipes, ingredients, supplies) { return res }; -// another - -/** - * @param {string[]} recipes - * @param {string[][]} ingredients - * @param {string[]} supplies - * @return {string[]} - */ -const findAllRecipes = function(recipes, ingredients, supplies) { - const graph = {} - const n = recipes.length - - const inDegree = {} - supplies = new Set(supplies) - for(const e of recipes) inDegree[e] = 0 - - let q = [] - for(let i = 0; i < n; i++) { - const rec = recipes[i] - for(let e of ingredients[i]) { - if(!supplies.has(e)) { - if(graph[e] == null) graph[e] = [] - graph[e].push(rec) - inDegree[rec]++ - } - } - } - // console.log(inDegree) - for(let i = 0; i < n; i++) { - if(inDegree[recipes[i]] === 0) { - q.push(recipes[i]) - } - } - - // console.log(q) - const res = [] - while(q.length) { - const size = q.length - const nxt = [] - - for(let i = 0; i < size; i++) { - const cur = q[i] - res.push(cur) - for(const e of (graph[cur] || [])) { - inDegree[e]-- - if(inDegree[e] === 0) nxt.push(e) - } - } - - q = nxt - } - - return res -}; diff --git a/2116-check-if-a-parentheses-string-can-be-validcheck-if-a-parentheses-string-can-be-valid.js b/2116-check-if-a-parentheses-string-can-be-validcheck-if-a-parentheses-string-can-be-valid.js index 9c654fd1..11c03073 100644 --- a/2116-check-if-a-parentheses-string-can-be-validcheck-if-a-parentheses-string-can-be-valid.js +++ b/2116-check-if-a-parentheses-string-can-be-validcheck-if-a-parentheses-string-can-be-valid.js @@ -21,26 +21,3 @@ const canBeValid = function(s, locked) { return true }; -// another - -/** - * @param {string} s - * @param {string} locked - * @return {boolean} - */ -const canBeValid = function (s, locked) { - return s.length % 2 === 0 && chk(s, locked, '(') && chk(s, locked, ')') - - function chk(s, locked, op) { - let bal = 0, - wild = 0, - sz = s.length - let start = op === '(' ? 0 : sz - 1, - dir = op === '(' ? 1 : -1 - for (let i = start; i >= 0 && i < sz && wild + bal >= 0; i += dir) { - if (locked[i] === '1') bal += s[i] === op ? 1 : -1 - else wild++ - } - return Math.abs(bal) <= wild - } -} diff --git a/2127-maximum-employees-to-be-invited-to-a-meeting.js b/2127-maximum-employees-to-be-invited-to-a-meeting.js index 8a8efe69..1a57fc66 100644 --- a/2127-maximum-employees-to-be-invited-to-a-meeting.js +++ b/2127-maximum-employees-to-be-invited-to-a-meeting.js @@ -55,108 +55,3 @@ const maximumInvitations = function(favorite) { return max(maxLoopSize, twoNodesSize) }; -// another - -/** - * @param {number[]} favorite - * @return {number} - */ -const maximumInvitations = function (favorite) { - const n = favorite.length - const indegree = Array(n).fill(0) - for (let i = 0; i < n; i++) indegree[favorite[i]]++ - const { max } = Math - let q = [] - const visited = Array(n).fill(0) - const depth = Array(n).fill(1) - for (let i = 0; i < n; i++) { - if (indegree[i] === 0) { - depth[i] = 1 - visited[i] = 1 - q.push(i) - } - } - - while (q.length) { - const cur = q.shift() - const nxt = favorite[cur] - indegree[nxt]-- - if (indegree[nxt] == 0) { - q.push(nxt) - visited[nxt] = 1 - } - depth[nxt] = depth[cur] + 1 - } - - let max_circle_size = 0 - let max_link_size = 0 - for (let i = 0; i < n; i++) { - if (visited[i] === 1) continue - let j = i - let count = 0 - while (visited[j] == 0) { - count++ - visited[j] = 1 - j = favorite[j] - } - if (count > 2) max_circle_size = max(max_circle_size, count) - else if (count == 2) max_link_size += depth[i] + depth[favorite[i]] - } - - return max(max_circle_size, max_link_size) -} - -// another - -/** - * @param {number[]} favorite - * @return {number} - */ -var maximumInvitations = function(favorite) { - const n = favorite.length, m = Array(n).fill(-1), r = Array.from({ length: n }, () => []) - for(let i = 0; i < n; i++) r[favorite[i]].push(i) - - function dfs(u) { - if(m[u] !== -1) return m[u] - let res = 0 - for(let v of r[u]) res = Math.max(res, dfs(v)) - return m[u] = 1 + res - } - let res = 0, free = 0 - for(let i = 0; i < n; ++i) { - if (m[i] != -1) continue; // skip visited nodes - if (favorite[favorite[i]] == i) { - m[i] = m[favorite[i]] = 0; - let a = 0, b = 0; // find the length of the longest arms starting from `i` and `A[i]` - for (let v of r[i]) { - if (v == favorite[i]) continue; - a = Math.max(a, dfs(v)); - } - for (let v of r[favorite[i]]) { - if (v == i) continue; - b = Math.max(b, dfs(v)); - } - free += a + b + 2; // this free component is of length `a+b+2` - } - } - function dfs2(u) { - if (m[u] != -1) return[u, m[u], false]; // this is the merge point - m[u] = 0; - let [mergePoint, depth, mergePointMet] = dfs2(favorite[u]); - if (mergePointMet) { // If we've met the merge point again already, this node is outside of the cycle and should be ignored. - m[u] = 0; - return [mergePoint, depth, true]; - } - m[u] = 1 + depth; // If we haven't met the merge point, we increment the depth. - return [mergePoint, m[u], u == mergePoint]; - } - - for(let i = 0; i < n; i++) { - if(m[i] !== -1) continue - let [mergePoint, depth, mergePointMet] = dfs2(i) - if(mergePointMet) res = Math.max(res, depth) - } - - return Math.max(res, free) -}; - diff --git a/213-house-robber-ii.js b/213-house-robber-ii.js index c4e1037f..3d7da157 100644 --- a/213-house-robber-ii.js +++ b/213-house-robber-ii.js @@ -18,47 +18,3 @@ const rob = function(nums) { }; -// another - -/** - * @param {number[]} nums - * @return {number} - */ -const rob = function(nums) { - if(nums.length === 1) return nums[0] - return Math.max(helper(0, nums.length - 2), helper(1, nums.length - 1)) - - function helper(l, r) { - let inc = 0, exc = 0 - for(let i = l; i <= r; i++) { - const pi = inc, pe = exc - inc = exc + nums[i] - exc = Math.max(pi, pe) - } - return Math.max(inc, exc) - } -}; - -// another - -/** - * @param {number[]} nums - * @return {number} - */ -const rob = function(nums) { - const n = nums.length - nums = nums.concat(nums) - let res = 0 - for(let i = 0; i < n; i++) { - let tmp = nums[i] - let pp = 0 - let p = 0 - for(let j = i; j < n + i - 1; j++) { - tmp = Math.max(tmp, pp + nums[j], p); - [pp, p] = [p, tmp] - } - res = Math.max(res, tmp) - } - - return res -}; diff --git a/2130-maximum-twin-sum-of-a-linked-list.js b/2130-maximum-twin-sum-of-a-linked-list.js index b1e45990..a1d8198c 100644 --- a/2130-maximum-twin-sum-of-a-linked-list.js +++ b/2130-maximum-twin-sum-of-a-linked-list.js @@ -34,32 +34,3 @@ const pairSum = function(head) { return res }; -// another - -/** - * Definition for singly-linked list. - * function ListNode(val, next) { - * this.val = (val===undefined ? 0 : val) - * this.next = (next===undefined ? null : next) - * } - */ -/** - * @param {ListNode} head - * @return {number} - */ -const pairSum = function(head) { - const arr = [] - let cur = head - - while(cur) { - arr.push(cur.val) - cur = cur.next - } - - let res = 0 - for(let i = 0, n = arr.length; i < n / 2; i++) { - res = Math.max(res, arr[i] + arr[n - 1 - i]) - } - - return res -}; diff --git a/2136-earliest-possible-day-of-full-bloom.js b/2136-earliest-possible-day-of-full-bloom.js index 6a736966..15d50243 100644 --- a/2136-earliest-possible-day-of-full-bloom.js +++ b/2136-earliest-possible-day-of-full-bloom.js @@ -20,42 +20,3 @@ const earliestFullBloom = function(plantTime, growTime) { return res }; -// another - - -/** - * @param {number[]} plantTime - * @param {number[]} growTime - * @return {number} - */ -const earliestFullBloom = function(plantTime, growTime) { - const sum = arr => arr.reduce((ac, e) => ac +e, 0) - let l = 0, r = sum(plantTime) + sum(growTime) - const n = plantTime.length - - const a = [] - for(let i = 0; i < n; i++) { - a.push([growTime[i], plantTime[i] ]) - } - - a.sort((a, b) => a[0] === b[0] ? a[1] - b[1] : a[0] - b[0]) - a.reverse() - function chk(d) { - let total = -1 - let max_num = 0 - for(let i = 0; i < n; i++) { - total += a[i][1] - max_num = Math.max(max_num, total + a[i][0] + 1) - } - return max_num <= d - } - - while (l < r) { - let m = ~~((l + r) / 2) - if (chk(m)) r = m - else l = m + 1 - } - - return l -}; - diff --git a/214-shortest-palindrome.js b/214-shortest-palindrome.js index a278095f..2309c51c 100644 --- a/214-shortest-palindrome.js +++ b/214-shortest-palindrome.js @@ -12,75 +12,3 @@ const shortestPalindrome = function(s) { return suffix.split('').reverse().join('') + shortestPalindrome(s.substring(0, j)) + suffix; }; -// another - -/** - * @param {string} s - * @return {string} - */ -const shortestPalindrome = function (s) { - const tmp = s + '#' + s.split('').reverse().join('') - const fail = getFail(tmp) - return ( - s - .split('') - .slice(fail[fail.length - 1]) - .reverse() - .join('') + s - ) -} - -function getFail(s) { - const n = s.length - const table = new Array(n).fill(0) - let index = 0 - for (let i = 1; i < n; ) { - if (s.charAt(index) === s.charAt(i)) { - table[i] = ++index - i++ - } else { - if (index > 0) { - index = table[index - 1] - } else { - index = 0 - i++ - } - } - } - return table -} - -// another - -/** - * @param {string} s - * @return {string} - */ -const shortestPalindrome = function(s) { - const tmp = `${s}#${reverse(s)}` - const table = kmp(tmp) - return `${reverse(s.slice(table[table.length - 1]))}${s}` -}; -function reverse(str) { - return [...str].reverse().join('') -} - -function kmp(s) { - const n = s.length, table = Array(n).fill(0) - let idx = 0 - for(let i = 1; i < n; ) { - if(s[i] === s[idx]) { - idx++ - table[i] = idx - i++ - } else { - if(idx > 0) { - idx = table[idx - 1] - } else { - idx = 0 - i++ - } - } - } - return table -} diff --git a/2140-solving-questions-with-brainpower.js b/2140-solving-questions-with-brainpower.js index 646ba1d2..61775ee7 100644 --- a/2140-solving-questions-with-brainpower.js +++ b/2140-solving-questions-with-brainpower.js @@ -11,25 +11,3 @@ const mostPoints = function(questions) { return dp[0] }; -// another - -/** - * @param {number[][]} questions - * @return {number} - */ -const mostPoints = function (questions) { - let n = questions.length - const temp = Array(n).fill(0) - - temp[n - 1] = questions[n - 1][0] - - for (let i = n - 2; i >= 0; i--) { - if (i + questions[i][1] + 1 <= n - 1) - temp[i] = Math.max( - temp[i + 1], - questions[i][0] + temp[i + questions[i][1] + 1] - ) - else temp[i] = Math.max(temp[i + 1], questions[i][0]) - } - return temp[0] -} diff --git a/2141-maximum-running-time-of-n-computers.js b/2141-maximum-running-time-of-n-computers.js index d71ebb67..054a9bb1 100644 --- a/2141-maximum-running-time-of-n-computers.js +++ b/2141-maximum-running-time-of-n-computers.js @@ -27,43 +27,3 @@ const maxRunTime = function(n, batteries) { }; -// another - - -/** - * @param {number} n - * @param {number[]} batteries - * @return {number} - */ -var maxRunTime = function (n, batteries) { - batteries.sort((a, b) => a - b) - const sum = batteries.reduce((ac, e) => ac + BigInt(e), 0n) - let hi = ~~(sum / BigInt(n)) + 1n, - lo = 0n - while (lo < hi) { - let mid = ~~((lo + hi) / 2n) - if (chk(mid)) { - lo = mid + 1n - } else { - hi = mid - } - } - - return lo - 1n - function chk(x) { - let current = 0n - let i = 0n - for (let b of batteries) { - if (i == BigInt(n)) break - if (b > x) b = x - if (b >= x - current) { - i += 1n - current = BigInt(b) - (x - current) - } else { - current += BigInt(b) - } - } - - return i == n - } -} diff --git a/215-kth-largest-element-in-an-array.js b/215-kth-largest-element-in-an-array.js index cbf5ce05..f9fab197 100755 --- a/215-kth-largest-element-in-an-array.js +++ b/215-kth-largest-element-in-an-array.js @@ -24,116 +24,3 @@ const findKthLargest = function(nums, k) { else return findKthLargest(smaller, k - larger.length - pivotCount); }; -// another - -/** - * @param {number[]} nums - * @param {number} k - * @return {number} - */ -const findKthLargest = function(nums, k) { - return quickselect(nums, 0, nums.length - 1, k) -}; -function quickselect(arr, lo, hi, k) { - let pivtIdx = Math.floor(Math.random() * (hi - lo + 1)) + lo - let pivtVal = arr[pivtIdx] - ;[arr[hi], arr[pivtIdx]] = [arr[pivtIdx], arr[hi]] - let i = lo - let j = hi - 1 - - while (i <= j) { - if (arr[i] <= pivtVal) { - i++ - } else { - ;[arr[j], arr[i]] = [arr[i], arr[j]] - j-- - } - } - - ;[arr[i], arr[hi]] = [arr[hi], arr[i]] - - pivtIdx = i - - if (pivtIdx === arr.length - k) return arr[pivtIdx] - if (pivtIdx < arr.length - k) return quickselect(arr, pivtIdx + 1, hi, k) - return quickselect(arr, lo, pivtIdx - 1, k) -} - -// another - -/** - * @param {number[]} nums - * @param {number} k - * @return {number} - */ -const findKthLargest = function(nums, k) { - const n = nums.length - let l = 0, r = n - 1, t = n - k - while(l < r) { - const mid = partition(nums, l, r) - if(mid < t) { - l = mid + 1 - } else { - if(mid === t) break - else r = mid - 1 - } - } - return nums[t] -}; - -function partition(arr, left, right) { - let pivot = arr[right] - let l = left, r = right - 1, j = left - for(let i = left; i < right; i++) { - if(arr[i] <= pivot) { - swap(arr, i, j) - j++ - } - } - swap(arr, j, right) - return j -} - -function swap(arr, i, j) { - const tmp = arr[i] - arr[i] = arr[j] - arr[j] = tmp -} - -// another - -/** - * @param {number[]} nums - * @param {number} k - * @return {number} - */ -const findKthLargest = function(nums, k) { - const n = nums.length - let l = 0, r = n - 1, t = n - k - while(l < r) { - const idx = partition(nums, l, r) - if (idx === t) return nums[t] - if (idx < t) l = idx + 1 - else r = idx - 1 - } - return nums[l] -}; - -function partition(arr, l, r) { - let tmp = l, pivot = arr[l] - while(l < r) { - while(l < r && arr[r] >= pivot) r-- - while(l < r && arr[l] <= pivot) l++ - swap(arr, l, r) - } - swap(arr, l, tmp) - return l -} - -function swap(arr, i, j) { - const tmp = arr[i] - arr[i] = arr[j] - arr[j] = tmp -} - - diff --git a/2163-minimum-difference-in-sums-after-removal-of-elements.js b/2163-minimum-difference-in-sums-after-removal-of-elements.js index 862bbe53..5179182b 100644 --- a/2163-minimum-difference-in-sums-after-removal-of-elements.js +++ b/2163-minimum-difference-in-sums-after-removal-of-elements.js @@ -43,116 +43,3 @@ const minimumDifference = function(nums) { return res }; -// another - - -/** - * @param {number[]} nums - * @return {number} - */ -const minimumDifference = function(nums) { - const n = nums.length, len = n / 3 - const maxHeap = new PriorityQueue((a, b) => a > b) - const minHeap = new PriorityQueue((a, b) => a < b) - const pre = Array(n).fill(Infinity), suffix = Array(n).fill(-Infinity) - for(let i = 0, sum = 0; i < 2 * len; i++) { - const cur = nums[i] - maxHeap.push(cur) - sum += cur - if(maxHeap.size() > len) { - const tmp = maxHeap.pop() - sum -= tmp - } - if(maxHeap.size() === len) { - pre[i] = sum - } - } - - for(let i = n - 1, sum = 0; i >= len; i--) { - const cur = nums[i] - minHeap.push(cur) - sum += cur - if(minHeap.size() > len) { - const tmp = minHeap.pop() - sum -= tmp - } - if(minHeap.size() === len) { - suffix[i] = sum - } - } - - // console.log(pre, suffix) - let res = Infinity - for(let i = len - 1; i < n - len; i++) { - res = Math.min(res, pre[i] - suffix[i + 1]) - } - return res -}; - -class PriorityQueue { - constructor(comparator = (a, b) => a > b) { - this.heap = [] - this.top = 0 - this.comparator = comparator - } - size() { - return this.heap.length - } - isEmpty() { - return this.size() === 0 - } - peek() { - return this.heap[this.top] - } - push(...values) { - values.forEach((value) => { - this.heap.push(value) - this.siftUp() - }) - return this.size() - } - pop() { - const poppedValue = this.peek() - const bottom = this.size() - 1 - if (bottom > this.top) { - this.swap(this.top, bottom) - } - this.heap.pop() - this.siftDown() - return poppedValue - } - replace(value) { - const replacedValue = this.peek() - this.heap[this.top] = value - this.siftDown() - return replacedValue - } - - parent = (i) => ((i + 1) >>> 1) - 1 - left = (i) => (i << 1) + 1 - right = (i) => (i + 1) << 1 - greater = (i, j) => this.comparator(this.heap[i], this.heap[j]) - swap = (i, j) => ([this.heap[i], this.heap[j]] = [this.heap[j], this.heap[i]]) - siftUp = () => { - let node = this.size() - 1 - while (node > this.top && this.greater(node, this.parent(node))) { - this.swap(node, this.parent(node)) - node = this.parent(node) - } - } - siftDown = () => { - let node = this.top - while ( - (this.left(node) < this.size() && this.greater(this.left(node), node)) || - (this.right(node) < this.size() && this.greater(this.right(node), node)) - ) { - let maxChild = - this.right(node) < this.size() && - this.greater(this.right(node), this.left(node)) - ? this.right(node) - : this.left(node) - this.swap(node, maxChild) - node = maxChild - } - } -} diff --git a/2166-design-bitset.js b/2166-design-bitset.js index 4c177ba6..86213e2c 100644 --- a/2166-design-bitset.js +++ b/2166-design-bitset.js @@ -79,93 +79,3 @@ Bitset.prototype.toString = function () { * var param_7 = obj.toString() */ -// another - -/** - * @param {number} size - */ -var Bitset = function(size) { - this.s = Array.from({ length:2 }, () => Array()) - this.cnt = 0 - this.now = 0 - for (let i = 0; i < size; i++) { - this.s[this.now].push( '0'); - this.s[this.now ^ 1].push( '1'); - } -}; - -/** - * @param {number} idx - * @return {void} - */ -Bitset.prototype.fix = function(idx) { - if (this.s[this.now][idx] == '1') return; - // swap(this.s[this.now][idx], this.s[this.now ^ 1][idx]); - const tmp = this.s[this.now][idx] - this.s[this.now][idx] = this.s[this.now ^ 1][idx] - this.s[this.now ^ 1][idx] = tmp - this.cnt++; -}; - -/** - * @param {number} idx - * @return {void} - */ -Bitset.prototype.unfix = function(idx) { - if (this.s[this.now][idx] == '0') return; - // swap(this.s[this.now][idx], this.s[this.now ^ 1][idx]); - const tmp = this.s[this.now][idx] - this.s[this.now][idx] = this.s[this.now ^ 1][idx] - this.s[this.now ^ 1][idx] = tmp - this.cnt--; -}; - -/** - * @return {void} - */ -Bitset.prototype.flip = function() { - this.now = this.now ^ 1; - this.cnt = this.s[0].length - this.cnt; -}; - -/** - * @return {boolean} - */ -Bitset.prototype.all = function() { - return this.cnt == this.s[0].length; -}; - -/** - * @return {boolean} - */ -Bitset.prototype.one = function() { - return this.cnt !== 0 -}; - -/** - * @return {number} - */ -Bitset.prototype.count = function() { - return this.cnt; -}; - -/** - * @return {string} - */ -Bitset.prototype.toString = function() { - return this.s[this.now].join(''); -}; - - -/** - * Your Bitset object will be instantiated and called as such: - * var obj = new Bitset(size) - * obj.fix(idx) - * obj.unfix(idx) - * obj.flip() - * var param_4 = obj.all() - * var param_5 = obj.one() - * var param_6 = obj.count() - * var param_7 = obj.toString() - */ - diff --git a/2167-minimum-time-to-remove-all-cars-containing-illegal-goods.js b/2167-minimum-time-to-remove-all-cars-containing-illegal-goods.js index 6f4ca66e..3dcaf7da 100644 --- a/2167-minimum-time-to-remove-all-cars-containing-illegal-goods.js +++ b/2167-minimum-time-to-remove-all-cars-containing-illegal-goods.js @@ -23,106 +23,3 @@ const minimumTime = function(s) { } }; -// another - -/** - * @param {string} s - * @return {number} - */ -const minimumTime = function(s) { - const n = s.length - const arr = [] - for(let ch of s) { - arr.push(ch === '1' ? 1 : -1) - } - const score = minSum(arr) - return n + score - - function minSum(ar) { - const dp = Array(n).fill(0) - dp[0] = ar[0] - for(let i = 1; i < n; i++) { - dp[i] = Math.min(ar[i], ar[i] + dp[i - 1]) - } - return Math.min(0, Math.min(...dp)) - } -}; - -// another - -/** - * @param {string} s - * @return {number} - */ -const minimumTime = function(s) { - if(s.length === 1) return s === '1' ? 1 : 0 - const n = s.length - const arr = [] - for(let ch of s) { - arr.push(ch === '1' ? 1 : -1) - } - const score = minSum(arr) - return n + score - - function minSum(ar) { - const dp = Array(n).fill(0) - dp[0] = ar[0] - let ans = dp[0] - for(let i = 1; i < n; i++) { - dp[i] = Math.min(ar[i], ar[i] + dp[i - 1]) - ans = Math.min(0, ans, dp[i]) - } - return ans - } -}; - -// another - - -/** - * @param {string} s - * @return {number} - */ -var minimumTime = function(s) { - - const { max, min } = Math - - let n = s.length; - const l = Array.from({ length: n + 1 }, () => Array(2).fill(0)) - const r = Array.from({ length: n + 1 }, () => Array(2).fill(0)) - for (let i = 0; i < n; i++) l[i][0] = l[i][1] = r[i][0] = r[i][1] = 0; - if (s[0] == '1') { - l[0][0] = 1; - l[0][1] = 2; - } - for (let i = 1; i < n; i++) { - if (s[i] == '0') { - l[i][0] = l[i - 1][0]; - l[i][1] = l[i - 1][1]; - } else { - l[i][0] = i + 1; - l[i][1] = min(l[i - 1][0], l[i - 1][1]) + 2; - } - } - if (s[n - 1] == '1') { - r[n - 1][0] = 1; - r[n - 1][1] = 2; - } - for (let i = n - 2; i >= 0; i--) { - if (s[i] == '0') { - r[i][0] = r[i + 1][0]; - r[i][1] = r[i + 1][1]; - } else { - r[i][0] = n - i; - r[i][1] = min(r[i + 1][0], r[i + 1][1]) + 2; - } - } - let ans = n; - for (let i = -1; i < n; i++) { - let cost = 0; - if (i != -1) cost += min(l[i][0], l[i][1]); - if (i != n - 1) cost += min(r[i + 1][0], r[i + 1][1]); - ans = min(ans, cost); - } - return ans; -}; diff --git a/2183-count-array-pairs-divisible-by-k.js b/2183-count-array-pairs-divisible-by-k.js index 268ad344..116038dc 100644 --- a/2183-count-array-pairs-divisible-by-k.js +++ b/2183-count-array-pairs-divisible-by-k.js @@ -26,33 +26,3 @@ const countPairs = function (nums, k) { } } -// another - - -/** - * @param {number[]} nums - * @param {number} k - * @return {number} - */ -const coutPairs = function(nums, k) { - let res = 0; - let cnt = Array(1e5 + 1).fill(0); - const n = nums.length - for (let i = 0; i < n; ++i) { - if (nums[i] % k == 0) { - res += i; - ++cnt[0]; - } - else { - let div = gcd(k, nums[i]); - for (let d = 0; d <= div; ++d) res += cnt[k / div * d]; - ++cnt[div]; - } - } - return res; -}; - -function gcd(a, b) { - if(b === 0) return a - return gcd(b, a % b) -} diff --git a/2184-number-of-ways-to-build-sturdy-brick-wall.js b/2184-number-of-ways-to-build-sturdy-brick-wall.js index 99624b12..cb12e7f2 100644 --- a/2184-number-of-ways-to-build-sturdy-brick-wall.js +++ b/2184-number-of-ways-to-build-sturdy-brick-wall.js @@ -50,54 +50,3 @@ const buildWall = function (height, width, bricks) { return res } -// another - -/** - * @param {number} height - * @param {number} width - * @param {number[]} bricks - * @return {number} - */ -const buildWall = function (height, width, bricks) { - const MOD = 1e9 + 7 - const rowPerms = new Set() // save all possible permutations of a row as a bitmask - const memo = [] - for (let i = 0; i <= height; ++i) { - memo[i] = new Array(2 ** 10).fill(0) - } - findAllPossRowPerms(rowPerms, 0, 0 | 0) - return countWaysToBuildSturdyWall(height, 0) - - function countWaysToBuildSturdyWall(currHeight, prevRowPerm) { - if (currHeight === 0) return 1 - if (memo[currHeight][prevRowPerm] != 0) { - return memo[currHeight][prevRowPerm] - } - let totCount = 0 - for (const rowPerm of rowPerms) { - if ((rowPerm & prevRowPerm) === 0) { - totCount = - (totCount + countWaysToBuildSturdyWall(currHeight - 1, rowPerm)) % MOD - } - } - memo[currHeight][prevRowPerm] = totCount - return totCount - } - - function findAllPossRowPerms(rowPerms, currWidth, mask) { - if (currWidth === width) { - rowPerms.add(mask) - return - } - // The reason why we don't want to mark the 0 index is that we are going from right to left - // when creating the wall and unlike other points of a row, the all rows will be flushed - // against the 0 index. - if (currWidth > 0) mask |= 1 << currWidth - for (const brick of bricks) { - if (currWidth + brick <= width) { - findAllPossRowPerms(rowPerms, currWidth + brick, mask) - } - } - return - } -} diff --git a/2188-minimum-time-to-finish-the-race.js b/2188-minimum-time-to-finish-the-race.js index 2e995cff..9a77fa16 100644 --- a/2188-minimum-time-to-finish-the-race.js +++ b/2188-minimum-time-to-finish-the-race.js @@ -58,46 +58,3 @@ function preprocess(tires) { return res } -// another - -/** - * @param {number[][]} tires - * @param {number} changeTime - * @param {number} numLaps - * @return {number} - */ -var minimumFinishTime = function (tires, changeTime, numLaps) { - let N = tires.length, - len = 0 - const { max, min } = Math - const best = Array(numLaps).fill(Infinity), - dp = Array(numLaps + 1).fill(Infinity) - for (let i = 0; i < N; ++i) { - // We assume we also need `changeTime` time to use the first tire - // so that we don't need to treat the first tire as a special case - let f = tires[i][0], - r = tires[i][1], - sum = changeTime, - p = 1 - for (let j = 0; j < numLaps; ++j) { - sum += f * p - // If using the same tire takes no less time than changing the tire, - // stop further using the current tire - if (f * p >= f + changeTime) break - best[j] = min(best[j], sum) - len = max(len, j + 1) - p *= r - } - } - // dp[i + 1] is the minimum time to finish `numLaps` laps - dp[0] = 0 - for (let i = 0; i < numLaps; ++i) { - for (let j = 0; j < len && i - j >= 0; ++j) { - // try using the same tire in the last `j+1` laps - dp[i + 1] = min(dp[i + 1], dp[i - j] + best[j]) - } - } - // minus the `changeTime` we added to the first tire - return dp[numLaps] - changeTime -} - diff --git a/2192-all-ancestors-of-a-node-in-a-directed-acyclic-graph.js b/2192-all-ancestors-of-a-node-in-a-directed-acyclic-graph.js index 4ae95f82..b730c96b 100644 --- a/2192-all-ancestors-of-a-node-in-a-directed-acyclic-graph.js +++ b/2192-all-ancestors-of-a-node-in-a-directed-acyclic-graph.js @@ -28,37 +28,3 @@ const getAncestors = function(n, edges) { } }; -// another - -/** - * @param {number} n - * @param {number[][]} edges - * @return {number[][]} - */ -const getAncestors = function(n, edges) { - const res = Array.from({ length: n }, () => new Set()) - const inDegree = Array(n).fill(0) - const graph = {} - - for(const [u, v] of edges) { - if(graph[v] == null) graph[v] = [] - graph[v].push(u) - inDegree[v]++ - } - - const visited = Array(n).fill(false) - for (let i = 0; i < n; i++) { - if (!visited[i]) dfs(i); - } - - return res.map(set => Array.from(set).sort((a, b) => a - b)) - - function dfs(i) { - visited[i] = true - for(const p of (graph[i] || [])) { - if(visited[p] === false) dfs(p) - res[i].add(p) - for(const e of res[p]) res[i].add(e) - } - } -}; diff --git a/220-contains-duplicate-iii.js b/220-contains-duplicate-iii.js index e0247c1c..98e48c4a 100644 --- a/220-contains-duplicate-iii.js +++ b/220-contains-duplicate-iii.js @@ -28,27 +28,3 @@ const containsNearbyAlmostDuplicate = function(nums, k, t) { return false } -// another - -/** - * @param {number[]} nums - * @param {number} k - * @param {number} t - * @return {boolean} - */ -const containsNearbyAlmostDuplicate = function(nums, k, t) { - const map = nums - .map((val, idx) => ({ val, idx })) - .sort((a, b) => a.val - b.val) - let l = 0 - let r = 1 - while (r < map.length) { - const diff = Math.abs(map[r].val - map[l].val) - const range = Math.abs(map[r].idx - map[l].idx) - if (diff <= t && range <= k) return true - else if (diff > t) l++ - else if (range > k) r++ - if (l === r) r++ - } - return false -} diff --git a/221-maximal-square.js b/221-maximal-square.js index 1fadc79e..f516d9c1 100644 --- a/221-maximal-square.js +++ b/221-maximal-square.js @@ -19,27 +19,3 @@ const maximalSquare = function(matrix) { return maxLen * maxLen }; -// another - -const maximalSquare = function(matrix) { - const rows = matrix.length - if(rows === 0) return 0 - const cols = matrix[0].length - const dp = new Array(cols + 1).fill(0) - let max = 0 - let prev = 0 - let tmp - for(let i = 1; i <= rows; i++) { - for(let j = 1; j <= cols; j++) { - tmp = dp[j] - if(matrix[i - 1][j - 1] === '1') { - dp[j] = Math.min(dp[j - 1], dp[j], prev) + 1 - if(dp[j] > max) max = dp[j] - } else { - dp[j] = 0 - } - prev = tmp - } - } - return max ** 2 -}; diff --git a/2211-count-collisions-on-a-road.js b/2211-count-collisions-on-a-road.js index 03ab8347..8a560937 100644 --- a/2211-count-collisions-on-a-road.js +++ b/2211-count-collisions-on-a-road.js @@ -27,17 +27,3 @@ const countCollisions = function(directions) { return res }; -// another - -/** - * @param {string} directions - * @return {number} - */ - const countCollisions = function(directions) { - let res = 0, n = directions.length - let left = 0, right = n - 1 - while(left < n && directions[left] === 'L') left++ - while(right >= 0 && directions[right] === 'R') right-- - for(let i = left; i <= right; i++) res += directions[i] === 'S' ? 0 : 1 - return res -}; diff --git a/2216-minimum-deletions-to-make-array-beautiful.js b/2216-minimum-deletions-to-make-array-beautiful.js index 4c7f09dd..8a413e8d 100644 --- a/2216-minimum-deletions-to-make-array-beautiful.js +++ b/2216-minimum-deletions-to-make-array-beautiful.js @@ -15,23 +15,3 @@ const minDeletion = function(nums) { return res }; -// another - -/** - * @param {number[]} nums - * @return {number} - */ -const minDeletion = function(nums) { - let res = 0, i = 0 - for(i = 0, n = nums.length; i < n - 1;) { - if(nums[i] === nums[i + 1]) { - res++ - i++ - }else{ - i += 2 - } - } - if((nums.length - res) % 2 === 1) res++ - - return res -}; diff --git a/2218-maximum-value-of-k-coins-from-piles.js b/2218-maximum-value-of-k-coins-from-piles.js index 10f29d22..1c7a1abf 100644 --- a/2218-maximum-value-of-k-coins-from-piles.js +++ b/2218-maximum-value-of-k-coins-from-piles.js @@ -20,32 +20,3 @@ var maxValueOfCoins = function(piles, k) { return dp[k]; }; -// another - -/** - * @param {number[][]} piles - * @param {number} k - * @return {number} - */ -const maxValueOfCoins = function(piles, k) { - const n = piles.length - const memo = Array.from({ length: n + 1 }, () => Array(k + 1).fill(null)) - return helper(0, k) - - // TC: O(k * m) - // k: k - // n: length of piles - // m: sum(piles[i]), total elements of all piles - function helper(i, k) { - if(k == 0 || i === n) return 0 - if(memo[i][k] != null) return memo[i][k] - let res = helper(i + 1, k) - let cur = 0 - - for(let j = 0; j < Math.min(piles[i].length, k); j++) { - cur += piles[i][j] - res = Math.max(res, cur + helper(i + 1, k - j - 1)) - } - return memo[i][k] = res - } -}; diff --git a/222-count-complete-tree-nodes.js b/222-count-complete-tree-nodes.js index 73e20469..101b0a6a 100644 --- a/222-count-complete-tree-nodes.js +++ b/222-count-complete-tree-nodes.js @@ -29,16 +29,3 @@ function traverse(row, depth, obj) { if (next.length) traverse(next, depth + 1, obj); } -// another - -/** - * @param {TreeNode} root - * @return {number} - */ -const countNodes = function(root) { - if (!root) { - return 0; - } - - return 1 + countNodes(root.left) + countNodes(root.right); -}; diff --git a/2226-maximum-candies-allocated-to-k-children.js b/2226-maximum-candies-allocated-to-k-children.js index 38ed2088..b4edffdb 100644 --- a/2226-maximum-candies-allocated-to-k-children.js +++ b/2226-maximum-candies-allocated-to-k-children.js @@ -21,22 +21,3 @@ const maximumCandies = function(candies, k) { return min; }; -// another - -/** - * @param {number[]} candies - * @param {number} k - * @return {number} - */ -const maximumCandies = function(candies, k) { - let max = candies.reduce((ac, e) => ac + e, 0) - let min = 0 - while(min < max) { - const mid = max - Math.floor((max - min) /2) - let num = 0 - for(let e of candies) num += ~~(e / mid) - if(num < k) max = mid - 1 - else min = mid - } - return min -}; diff --git a/223-rectangle-area.js b/223-rectangle-area.js index 6c30c9d2..07503148 100644 --- a/223-rectangle-area.js +++ b/223-rectangle-area.js @@ -19,51 +19,3 @@ const computeArea = function(A, B, C, D, E, F, G, H) { } -// another - - -/** - * @param {number} A - * @param {number} B - * @param {number} C - * @param {number} D - * @param {number} E - * @param {number} F - * @param {number} G - * @param {number} H - * @return {number} - */ -const computeArea = function(A, B, C, D, E, F, G, H) { - const x1 = A, - x2 = C, - x3 = E, - x4 = G - const y1 = B, - y2 = D, - y3 = F, - y4 = H - return ( - area(x1, y1, x2, y2) + - area(x3, y3, x4, y4) - - delta(x1, x2, x3, x4) * delta(y1, y2, y3, y4) - ) -} - -function area(x1, y1, x2, y2) { - return Math.abs(x1 - x2) * Math.abs(y1 - y2) -} - -function delta(v1, v2, v3, v4) { - if (v1 > v2) { - let tmp = v1 - v1 = v2 - v2 = tmp - } - if (v3 > v4) { - let tmp = v3 - v3 = v4 - v4 = tmp - } - if (v3 >= v2 || v4 <= v1) return 0 - return Math.min(v2, v4) - Math.max(v1, v3) -} diff --git a/224-basic-calculator.js b/224-basic-calculator.js index cf886088..082cd40c 100644 --- a/224-basic-calculator.js +++ b/224-basic-calculator.js @@ -35,91 +35,3 @@ const calculate = function(s) { return res + sign * num } -// another - -/** - * @param {string} s - * @return {number} - */ -const calculate = function(s) { - s = s.split(' ').join('') - const n = s.length, stack = [] - const isNum = ch => ch >= '0' && ch <= '9' - let num = 0, op = 1, res = 0 - for(let i = 0; i < n; i++) { - const ch = s[i] - if(isNum(ch)) { - num = num * 10 + (+ch) - } else { - if(ch === '(') { - stack.push(res) - stack.push(op) - num = 0 - op = 1 - res = 0 - } else if(ch === ')') { - res += num * op - res *= stack.pop() - res += stack.pop() - num = 0 - op = 1 - } else if(ch === '+') { - res += op * num - op = 1 - num = 0 - } else if(ch === '-') { - res += op * num - op = -1 - num = 0 - } - } - } - - return res + op * num -}; - -// another -/** - * @param {string} s - * @return {number} - */ -const calculate = function(s) { - s = s.trim() - - let res = 0, num = 0, op = 1 - const isDigit = ch => ch >= '0' && ch <= '9' - const stk = [] - for(let i = 0, n = s.length; i < n; i++) { - - const e = s[i] - if(e === ' ') continue - if(isDigit(e)) num = num * 10 + (+e) - else { - - if(e === '(') { - stk.push(res) - stk.push(op) - - res = 0 - num = 0 - op = 1 - } else if(e === ')') { - res += num * op - res *= stk.pop() - res += stk.pop() - op = 1 - num = 0 - } else if(e === '-') { - res += num * op - op = -1 - num = 0 - } else if(e === '+') { - res += num * op - op = 1 - num = 0 - } - } - } - - return res + num * op -}; diff --git a/2245-maximum-trailing-zeros-in-a-cornered-path.js b/2245-maximum-trailing-zeros-in-a-cornered-path.js index b4914d6d..41fe7c37 100644 --- a/2245-maximum-trailing-zeros-in-a-cornered-path.js +++ b/2245-maximum-trailing-zeros-in-a-cornered-path.js @@ -82,62 +82,3 @@ const maxTrailingZeros = function maxTrailingZeros(grid) { return max } -// another - -/** - * @param {number[][]} grid - * @return {number} - */ -const maxTrailingZeros = function(grid) { - const g = grid - const m = g.length; - const n = g[0].length; - const ta = [...Array(m)].map(i => Array(n).fill(1)); - const tb = [...Array(m)].map(i => Array(n).fill(1)); - const tc = [...Array(m)].map(i => Array(n).fill(1)); - const td = [...Array(m)].map(i => Array(n).fill(1)); - - const c52 = (s) => { - let c5 = 0; - let c2 = 0; - while (s % 2 === 0) { - s = s / 2; - c2++; - } - while (s % 5 === 0) { - s = s / 5; - c5++; - } - return [c5, c2]; - } - - const c10 = ([c5, c2]) => { - return Math.min(c5, c2); - } - - for (let i = 0; i < m; i++) { - for (let j = 0; j < n; j++) { - ta[i][j] = (j === 0) ? c52(g[i][j]) : [c52(g[i][j])[0] + ta[i][j-1][0], c52(g[i][j])[1] + ta[i][j-1][1]]; - tb[i][j] = (i === 0) ? c52(g[i][j]) : [c52(g[i][j])[0] + tb[i-1][j][0], c52(g[i][j])[1] + tb[i-1][j][1]]; - } - } - - for (let i = m-1; i >= 0; i--) { - for (let j = n-1; j >= 0; j--) { - tc[i][j] = (j === n-1) ? c52(g[i][j]) : [c52(g[i][j])[0] + tc[i][j+1][0], c52(g[i][j])[1] + tc[i][j+1][1]]; // : ctz(hg(g[i][j]) * tc[i][j+1][0], tc[i][j+1][1]); // hg(g[i][j]) * tc[i][j+1]; - td[i][j] = (i === m-1) ? c52(g[i][j]) : [c52(g[i][j])[0] + td[i+1][j][0], c52(g[i][j])[1] + td[i+1][j][1]]; // : ctz(hg(g[i][j]) * td[i+1][j][0], td[i+1][j][1]); // hg(g[i][j]) * td[i+1][j]; - } - } - - let ret = 0; - for (let i = 0; i < m; i++) { - for (let j = 0; j < n; j++) { - let s1 = i === 0 ? c10(ta[i][j]) : c10([ta[i][j][0] + tb[i-1][j][0], ta[i][j][1] + tb[i-1][j][1]]); - let s2 = i === m - 1 ? c10(ta[i][j]) : c10([ta[i][j][0] + td[i+1][j][0], ta[i][j][1] + td[i+1][j][1]]); - let s3 = i === 0 ? c10(tc[i][j]) : c10([tc[i][j][0] + tb[i-1][j][0], tc[i][j][1] + tb[i-1][j][1]]); - let s4 = i === m - 1 ? c10(tc[i][j]) : c10([tc[i][j][0] + td[i+1][j][0], tc[i][j][1] + td[i+1][j][1]]); - ret = Math.max(ret, s1, s2, s3, s4); - } - } - return ret; -}; diff --git a/2246-longest-path-with-different-adjacent-characters.js b/2246-longest-path-with-different-adjacent-characters.js index 0b9e53e7..b0d8baf5 100644 --- a/2246-longest-path-with-different-adjacent-characters.js +++ b/2246-longest-path-with-different-adjacent-characters.js @@ -34,36 +34,3 @@ var longestPath = function(parent, s) { }; -// another - -/** - * @param {number[]} parent - * @param {string} s - * @return {number} - */ -var longestPath = function(parent, s) { - let n = s.length, res = 0; - const {max} = Math - let children = Array.from({ length: n}, () => Array()); - for (let i = 1; i < n; ++i) children[parent[i]].push(i); - dfs(children, s, 0); - return res; - - function dfs(children, s, i) { - let big1 = 0, big2 = 0; - for (let j of (children[i] || [])) { - let cur = dfs(children, s, j); - if (s[i] == s[j]) continue; - if (cur > big2) big2 = cur; - if (big2 > big1) { - let tmp = big1 - big1 = big2 - big2 = tmp - }; - } - res = max(res, big1 + big2 + 1); - return big1 + 1; - } -}; - - diff --git a/2250-count-number-of-rectangles-containing-each-point.js b/2250-count-number-of-rectangles-containing-each-point.js index b76d92e0..7d6e4d24 100644 --- a/2250-count-number-of-rectangles-containing-each-point.js +++ b/2250-count-number-of-rectangles-containing-each-point.js @@ -32,52 +32,3 @@ const countRectangles = function(rectangles, points) { return res }; -// another - -/** - * @param {number[][]} rectangles - * @param {number[][]} points - * @return {number[]} - */ -function countRectangles(rect, points) { - const hash = {} - for(let [y, x] of rect) { - if(hash[x] == null) hash[x] = [] - hash[x].push(y) - } - const keys = Object.keys(hash).map(e => +e) - for(const k of keys) { - hash[k].sort((a, b) => a - b) - } - keys.sort((a, b) => a - b) - const res = [] - const n = keys.length - // console.log(keys, hash) - for(const [y, x] of points) { - let v = 0 - const idx = helper(keys, x) - for(let i = idx; i < n; i++) { - const k = keys[i] - const p = helper(hash[k], y) - v += p === hash[k].length ? 0 : hash[k].length - p - } - res.push(v) - } - - return res - - function helper(arr, val) { - let l = 0, r = arr.length - while(l < r) { - const mid = l + Math.floor((r - l) / 2) - if(valid(mid)) r = mid - else l = mid + 1 - } - // console.log(arr, val, l) - return l - - function valid(mid) { - return arr[mid] >= val - } - } -} diff --git a/226-invert-binary-tree.js b/226-invert-binary-tree.js index 60a57563..8857027b 100644 --- a/226-invert-binary-tree.js +++ b/226-invert-binary-tree.js @@ -17,56 +17,3 @@ const invertTree = function (root) { return root } -// another - -/** - * Definition for a binary tree node. - * function TreeNode(val, left, right) { - * this.val = (val===undefined ? 0 : val) - * this.left = (left===undefined ? null : left) - * this.right = (right===undefined ? null : right) - * } - */ -/** - * @param {TreeNode} root - * @return {TreeNode} - */ -const invertTree = function (root) { - if (!root) return root - let queue = [root] - while (queue.length) { - let node = queue.shift() - if (node.left) { - queue.push(node.left) - } - if (node.right) { - queue.push(node.right) - } - let left = node.left - node.left = node.right - node.right = left - } - return root -} - -// anoother - -/** - * Definition for a binary tree node. - * function TreeNode(val, left, right) { - * this.val = (val===undefined ? 0 : val) - * this.left = (left===undefined ? null : left) - * this.right = (right===undefined ? null : right) - * } - */ -/** - * @param {TreeNode} root - * @return {TreeNode} - */ -const invertTree = function(root) { - if(root == null) return root - let tmp = root.left - root.left = invertTree(root.right) - root.right = invertTree(tmp) - return root -}; diff --git a/2262-total-appeal-of-a-string.js b/2262-total-appeal-of-a-string.js index 96fdec98..4a45c8e6 100644 --- a/2262-total-appeal-of-a-string.js +++ b/2262-total-appeal-of-a-string.js @@ -19,23 +19,3 @@ const appealSum = function (s) { } -// another - -/** - * @param {string} s - * @return {number} - */ -var appealSum = function(s) { - const cnt = Array(26).fill(-1); - let ans = 0; - let n = s.length; - const a = 'a'.charCodeAt(0) - for (let i = 0; i < n; i++) { - let tmp = n - i; - if (cnt[s[i].charCodeAt(0) - a] !== -1) tmp += (i - cnt[s[i].charCodeAt(0) - a] - 1) * (n - i); - else tmp += i * (n - i); - ans += tmp; - cnt[s[i].charCodeAt(0) - a] = i; - } - return ans; -}; diff --git a/2267-check-if-there-is-a-valid-parentheses-string-path.js b/2267-check-if-there-is-a-valid-parentheses-string-path.js index bafe3949..45e303e7 100644 --- a/2267-check-if-there-is-a-valid-parentheses-string-path.js +++ b/2267-check-if-there-is-a-valid-parentheses-string-path.js @@ -33,48 +33,3 @@ const hasValidPath = function (grid) { } -// another - -/** - * @param {character[][]} grid - * @return {boolean} - */ -var hasValidPath = function(grid) { - if (grid[0][0] == ")") return false - let m = grid.length, n = grid[0].length - const dirs = [[0, 1], [1, 0]] - - if ((m + n - 1) % 2 == 1) return false - const a = Array.from({ length: m }, () => Array(n).fill(null)) - for(let i = 0; i < m; i++) { - for(let j = 0; j < n; j++) { - if(grid[i][j] === '(') a[i][j] = 1 - else a[i][j] = -1 - } - } - - - const visited = new Set([`0,0,1`]) - let q = [[0, 0, 1]] - - while (q.length){ - const tmp = [] - for (const [x, y, v] of q) { - if (`${x},${y},${v}` == `${m - 1},${n - 1},0`) return true - for (const [dx, dy] of dirs) { - let i= x + dx, j = y + dy - if (0 <= i && i < m && 0 <= j && j < n) { - let v2 = v + a[i][j] - if (v2 >= 0 && !visited.has(`${i},${j},${v2}`) ) { - tmp.push([i, j, v2]) - visited.add(`${i},${j},${v2}`) - } - - } - } - } - q = tmp - } - return false -}; - diff --git a/227-basic-calculator-ii.js b/227-basic-calculator-ii.js index e0f174ae..6787675f 100644 --- a/227-basic-calculator-ii.js +++ b/227-basic-calculator-ii.js @@ -29,67 +29,3 @@ const calculate = function(s) { return res }; -// another - -/** - * @param {string} s - * @return {number} - */ -const calculate = function(s) { - const stack = [], n = s.length - let op = '+', num = 0 - for(let i = 0; i < n; i++) { - const isNumber = s[i] >= '0' && s[i] <= '9' - if(isNumber) num = num * 10 + (+s[i]) - if((!isNumber && s[i] !== ' ') || i === n - 1) { - if(op === '+') stack.push(num) - else if(op === '-') stack.push(-num) - else if(op === '*') stack.push(stack.pop() * num) - else if(op === '/') stack.push(~~(stack.pop() / num)) - op = s[i] - num = 0 - } - } - - return stack.reduce((ac, e) => ac + e, 0) -}; - -// another - -/** - * @param {string} s - * @return {number} - */ -const calculate = function(s) { - if(s == null || s.length === 0) return 0 - let sum = 0, num = 0, op = '+', tmp = 0 - const stack = [] - for(let i = 0; i < s.length; i++) { - const ch = s[i] - const isInt = ch => ch >= '0' && ch <= '9' - if(isInt(ch)) { - num = num * 10 + (+ch) - } - if((!isInt(ch) && ch !== ' ') || i === s.length - 1) { - if(op === '+') { - sum += tmp - tmp = num - } - else if(op === '-') { - sum += tmp - tmp = - num - } - else if(op === '*') { - tmp *= num - } - else if(op === '/') { - tmp = ~~(tmp / num) - } - op = ch - num = 0 - } - - } - - return sum + tmp -} diff --git a/2271-maximum-white-tiles-covered-by-a-carpet.js b/2271-maximum-white-tiles-covered-by-a-carpet.js index f19e23d8..e3aebe36 100644 --- a/2271-maximum-white-tiles-covered-by-a-carpet.js +++ b/2271-maximum-white-tiles-covered-by-a-carpet.js @@ -28,33 +28,3 @@ const maximumWhiteTiles = function (tiles, carpetLen) { return res } -// another - -/** - * @param {number[][]} tiles - * @param {number} carpetLen - * @return {number} - */ -const maximumWhiteTiles = function (tiles, carpetLen) { - tiles.sort((a, b) => a[0] - b[0]) - let res = 0, total = 0, right = 0 - const n = tiles.length - for(let i = 0; i < n; i++) { - const [l, r] = tiles[i] - const end = l + carpetLen - 1 - while(right < n && tiles[right][1] <= end) { - total += tiles[right][1] - tiles[right][0] + 1 - right++ - } - - if(right === n || tiles[right][0] > end) { - res = Math.max(res, total) - } else { - res = Math.max(res, total + end - tiles[right][0] + 1) - } - - total -= r - l + 1 - } - - return res -} diff --git a/2275-largest-combination-with-bitwise-and-greater-than-zero.js b/2275-largest-combination-with-bitwise-and-greater-than-zero.js index e08649e4..aa96650e 100644 --- a/2275-largest-combination-with-bitwise-and-greater-than-zero.js +++ b/2275-largest-combination-with-bitwise-and-greater-than-zero.js @@ -14,28 +14,3 @@ const largestCombination = function(candidates) { return res }; -// another - -/** - * @param {number[]} candidates - * @return {number} - */ -const largestCombination = function(candidates) { - const arr = Array(24).fill(0), len = 24 - for(const e of candidates) { - const str = toBin(e) - for(let n = str.length, i = n - 1; i >= 0; i--) { - const cur = str[i] - if(cur === '1') { - arr[len - 1 - (n - 1 - i)]++ - } - } - } - - return Math.max(...arr) - - function toBin(num) { - return (num >>> 0).toString(2) - } -}; - diff --git a/2276-count-integers-in-intervals.js b/2276-count-integers-in-intervals.js index 2647fd6b..3c6d9ccb 100644 --- a/2276-count-integers-in-intervals.js +++ b/2276-count-integers-in-intervals.js @@ -38,163 +38,3 @@ CountIntervals.prototype.count = function() { return this.ans }; -// another - -function binarySearch(l, r, fn) { - while (l <= r) { - const m = Math.floor((l + r) / 2) - if (fn(m)) { - l = m + 1 - } else { - r = m - 1 - } - } - return r -} - -var CountIntervals = function () { - this.intervals = [] - this.size = 0 -} - -/** - * @param {number} left - * @param {number} right - * @return {void} - */ -CountIntervals.prototype.add = function (left, right) { - const intervals = this.intervals - if (!intervals.length) { - intervals.push({ left, right }) - this.size += right - left + 1 - } else if (left > intervals[intervals.length - 1].right) { - intervals.push({ left, right }) - this.size += right - left + 1 - } else if (right < intervals[0].left) { - intervals.unshift({ left, right }) - this.size += right - left + 1 - } else { - const i = binarySearch(0, intervals.length - 1, (x) => { - return intervals[x].left < left - }) - let j, - start, - end, - sum = 0 - if (i < 0 || intervals[i].right < left) { - j = i + 1 - start = left - end = right - } else { - j = i - start = intervals[j].left - end = right - } - let first = -1 - while (j < intervals.length && right >= intervals[j].left) { - if (first < 0) first = j - end = Math.max(end, intervals[j].right) - sum += intervals[j].right - intervals[j].left + 1 - j++ - } - // delete [first, j) - // console.log('delete', j - first, '-', first, j) - this.size += end - start + 1 - sum - if (first < 0) { - this.intervals.splice(i + 1, 0, { left: start, right: end }) - } else { - this.intervals.splice(first, j - first, { left: start, right: end }) - } - } -} - -/** - * @return {number} - */ -CountIntervals.prototype.count = function () { - return this.size -} - - -// another - - - -var CountIntervals = function () { - this.root = new Node(1, 10 ** 9) -} - -/** - * @param {number} left - * @param {number} right - * @return {void} - */ -CountIntervals.prototype.add = function (left, right) { - this.root.addInterval(left, right) -} - -/** - * @return {number} - */ -CountIntervals.prototype.count = function () { - return this.root.total -} - -/** - * Your CountIntervals object will be instantiated and called as such: - * var obj = new CountIntervals() - * obj.add(left,right) - * var param_2 = obj.count() - */ - -class Node { - constructor(min, max) { - this.min = min - this.max = max - this.currentMin = -1 - this.currentMax = -1 - this.total = 0 - this.left = null - this.right = null - } - - addInterval(left, right) { - if (this.currentMin < 0) { - this.currentMin = left - this.currentMax = right - this.total = right - left + 1 - return this.total - } - - const mid = (this.min + this.max) >> 1 - - if (this.left) { - if (left <= mid) this.left.addInterval(left, Math.min(mid, right)) - if (right > mid) this.right.addInterval(Math.max(mid + 1, left), right) - - this.total = this.left.total + this.right.total - return - } - - if (left <= this.currentMax + 1 && right >= this.currentMin - 1) { - this.currentMin = Math.min(this.currentMin, left) - this.currentMax = Math.max(this.currentMax, right) - this.total = this.currentMax - this.currentMin + 1 - return - } - this.left = new Node(this.min, mid) - this.right = new Node(mid + 1, this.max) - - if (left <= mid) this.left.addInterval(left, Math.min(mid, right)) - if (right > mid) this.right.addInterval(Math.max(left, mid + 1), right) - if (this.currentMin <= mid) - this.left.addInterval(this.currentMin, Math.min(mid, this.currentMax)) - if (this.currentMax > mid) - this.right.addInterval( - Math.max(mid + 1, this.currentMin), - this.currentMax - ) - - this.total = this.left.total + this.right.total - } -} diff --git a/228-summary-ranges.js b/228-summary-ranges.js index eb708bde..52bda626 100644 --- a/228-summary-ranges.js +++ b/228-summary-ranges.js @@ -34,24 +34,3 @@ function insert(arr, start, end) { } } -// another - -const summaryRanges = nums => { - if (!nums || nums.length === 0) { - return []; - } - const returnArray = []; - let tempIdx = 0; - for (let i = 0; i < nums.length; i++) { - if (nums[i] + 1 !== nums[i + 1]) { - if (tempIdx === i) { - returnArray.push(nums[tempIdx].toString()); - } else { - returnArray.push(nums[tempIdx].toString() + "->" + nums[i].toString()); - } - tempIdx = i + 1; - } - } - - return returnArray; -}; diff --git a/2280-minimum-lines-to-represent-a-line-chart.js b/2280-minimum-lines-to-represent-a-line-chart.js index a8d50bf9..94ace530 100644 --- a/2280-minimum-lines-to-represent-a-line-chart.js +++ b/2280-minimum-lines-to-represent-a-line-chart.js @@ -26,34 +26,3 @@ function chk(p1, p2, p3) { -// another - -/** - * @param {number[][]} stockPrices - * @return {number} - */ -var minimumLines = function(stockPrices) { - let res = 1 - const ma = stockPrices - const n = ma.length - if(n === 0 || n === 1) return 0 - ma.sort((a, b) => a[0] - b[0]) - const eps = 1e-30 - let dx = ma[1][0] - ma[0][0], dy = ma[1][1] - ma[0][1] - for(let i = 2; i < n; i++) { - const cur = ma[i], pre = ma[i - 1] - const dxx = cur[0] - pre[0], dyy = cur[1] - pre[1] - if(BigInt(dxx) * BigInt(dy) !== BigInt(dx) * BigInt(dyy)) res++ - dx = dxx - dy = dyy - } - - return res -}; - -function product(p1, p2, p3) { - // 首先根据坐标计算p1p2和p1p3的向量,然后再计算叉乘 - // p1p2 向量表示为 (p2.x-p1.x,p2.y-p1.y) - // p1p3 向量表示为 (p3.x-p1.x,p3.y-p1.y) - return (p2.x-p1.x)*(p3.y-p1.y) - (p2.y-p1.y)*(p3.x-p1.x); -} diff --git a/2289-steps-to-make-array-non-decreasing.js b/2289-steps-to-make-array-non-decreasing.js index 1fdfc322..77d7b132 100644 --- a/2289-steps-to-make-array-non-decreasing.js +++ b/2289-steps-to-make-array-non-decreasing.js @@ -16,53 +16,3 @@ var totalSteps = function(nums) { return res }; -// another - -/** - * @param {number[]} nums - * @return {number} - */ -const totalSteps = function(nums) { - let res = 0, stk = [] - stk.push([nums[0], 0]) - for(let i = 1, n = nums.length; i < n; i++) { - let steps = 0 - while(stk.length && stk[stk.length - 1][0] <= nums[i]) { - const peek = stk.pop() - steps = Math.max(steps, peek[1]) - } - if(stk.length === 0) steps = 0 - else steps++ - - res = Math.max(res, steps) - stk.push([nums[i], steps]) - } - - return res -}; - -// another - -/** - * @param {number[]} nums - * @return {number} - */ -const totalSteps = function(nums) { - let res = 0 - const stk = [] - for(const e of nums) { - let steps = 1 - while(stk.length && e >= stk[stk.length - 1][0]) { - const tmp = stk.pop() - steps = Math.max(tmp[1] + 1, steps) - } - if(stk.length === 0) steps = 0 - else { - res = Math.max(res, steps) - } - stk.push([e, steps]) - } - return res -}; - - diff --git a/2296-design-a-text-editor.js b/2296-design-a-text-editor.js index 1e953e41..3a2e598b 100644 --- a/2296-design-a-text-editor.js +++ b/2296-design-a-text-editor.js @@ -68,79 +68,3 @@ TextEditor.prototype.slice = function() { return res } -// another - - -class Node { - constructor(val) { - this.val = val - this.prev = null - this.next = null - } -} - -var TextEditor = function() { - this.left = [] - this.right = [] - this.idx = 0 -}; - -/** - * @param {string} text - * @return {void} - */ -TextEditor.prototype.addText = function(text) { - for(const ch of text) this.left.push(ch) -}; - -/** - * @param {number} k - * @return {number} - */ -TextEditor.prototype.deleteText = function(k) { - let res = 0 - while(this.left.length && k) { - res++ - this.left.pop() - k-- - } - return res -}; - -/** - * @param {number} k - * @return {string} - */ -TextEditor.prototype.cursorLeft = function(k) { - while(k && this.left.length) { - const tmp = this.left.pop() - this.right.push(tmp) - k-- - } - - return this.left.slice(Math.max(0, this.left.length - 10), this.left.length).join('') -}; - -/** - * @param {number} k - * @return {string} - */ -TextEditor.prototype.cursorRight = function(k) { - while(k && this.right.length) { - const tmp = this.right.pop() - this.left.push(tmp) - k-- - } - - return this.left.slice(Math.max(0, this.left.length - 10), this.left.length).join('') -}; - - -/** - * Your TextEditor object will be instantiated and called as such: - * var obj = new TextEditor() - * obj.addText(text) - * var param_2 = obj.deleteText(k) - * var param_3 = obj.cursorLeft(k) - * var param_4 = obj.cursorRight(k) - */ diff --git a/23-merge-k-sorted-lists.js b/23-merge-k-sorted-lists.js index 1804d044..092c8fc3 100644 --- a/23-merge-k-sorted-lists.js +++ b/23-merge-k-sorted-lists.js @@ -34,146 +34,3 @@ function merge(lists, l, r) { return dummy.next } -// another - -/** - * Definition for singly-linked list. - * function ListNode(val, next) { - * this.val = (val===undefined ? 0 : val) - * this.next = (next===undefined ? null : next) - * } - */ -/** - * @param {ListNode[]} lists - * @return {ListNode} - */ -const mergeKLists = function(lists) { - if(lists == null || lists.length === 0) return null - if(lists.length === 1) return lists[0] - if(lists.length === 2) return mergeTwo(lists[0], lists[1]) - const left = mergeKLists(lists.slice(0, ~~(lists.length / 2))) - const right = mergeKLists(lists.slice(~~(lists.length / 2))) - - return mergeTwo(left, right) -}; - -function mergeTwo(l1, l2) { - const dummy = new ListNode() - let cur = dummy - while(l1 && l2) { - if(l1.val < l2.val) { - cur.next = l1 - l1 = l1.next - } else { - cur.next = l2 - l2 = l2.next - } - cur = cur.next - } - if(l1) cur.next = l1 - if(l2) cur.next = l2 - - - return dummy.next -} - - -// another - -/** - * Definition for singly-linked list. - * function ListNode(val, next) { - * this.val = (val===undefined ? 0 : val) - * this.next = (next===undefined ? null : next) - * } - */ -/** - * @param {ListNode[]} lists - * @return {ListNode} - */ -const mergeKLists = function(lists) { - if(lists == null || lists.length === 0) return null - const dummy = new ListNode() - let head = dummy - const pq = new PriorityQueue((a, b) => a.val < b.val) - for(let list of lists) { - while(list) { - pq.push(list) - list = list.next - } - } - while(!pq.isEmpty()) { - const pop = pq.pop() - head.next = new ListNode(pop.val) - head = head.next - } - return dummy.next -}; - -class PriorityQueue { - constructor(comparator = (a, b) => a > b) { - this.heap = [] - this.top = 0 - this.comparator = comparator - } - size() { - return this.heap.length - } - isEmpty() { - return this.size() === 0 - } - peek() { - return this.heap[this.top] - } - push(...values) { - values.forEach((value) => { - this.heap.push(value) - this.siftUp() - }) - return this.size() - } - pop() { - const poppedValue = this.peek() - const bottom = this.size() - 1 - if (bottom > this.top) { - this.swap(this.top, bottom) - } - this.heap.pop() - this.siftDown() - return poppedValue - } - replace(value) { - const replacedValue = this.peek() - this.heap[this.top] = value - this.siftDown() - return replacedValue - } - - parent = (i) => ((i + 1) >>> 1) - 1 - left = (i) => (i << 1) + 1 - right = (i) => (i + 1) << 1 - greater = (i, j) => this.comparator(this.heap[i], this.heap[j]) - swap = (i, j) => ([this.heap[i], this.heap[j]] = [this.heap[j], this.heap[i]]) - siftUp = () => { - let node = this.size() - 1 - while (node > this.top && this.greater(node, this.parent(node))) { - this.swap(node, this.parent(node)) - node = this.parent(node) - } - } - siftDown = () => { - let node = this.top - while ( - (this.left(node) < this.size() && this.greater(this.left(node), node)) || - (this.right(node) < this.size() && this.greater(this.right(node), node)) - ) { - let maxChild = - this.right(node) < this.size() && - this.greater(this.right(node), this.left(node)) - ? this.right(node) - : this.left(node) - this.swap(node, maxChild) - node = maxChild - } - } -} diff --git a/231-power-of-two.js b/231-power-of-two.js index 397c73ba..cb434181 100644 --- a/231-power-of-two.js +++ b/231-power-of-two.js @@ -15,29 +15,3 @@ const isPowerOfTwo = function(n) { return false }; -// another - -/** - * @param {number} n - * @return {boolean} - */ -const isPowerOfTwo = function(n) { - return Math.log2(n)%1 === 0 -}; - -// another - -/** - * @param {number} n - * @return {boolean} - */ -const isPowerOfTwo = n => n < 1 ? false : Number.MAX_VALUE % n === 0 - -// another - -/** - * @param {number} n - * @return {boolean} - */ -const isPowerOfTwo = x => x > 0 ? !(x & (x - 1)) : false; - diff --git a/2312-selling-pieces-of-wood.js b/2312-selling-pieces-of-wood.js index 8cf6f8b5..e4661b55 100644 --- a/2312-selling-pieces-of-wood.js +++ b/2312-selling-pieces-of-wood.js @@ -23,29 +23,3 @@ const sellingWood = function(m, n, prices) { return dp[m][n]; }; -// another - -/** - * @param {number} m - * @param {number} n - * @param {number[][]} prices - * @return {number} - */ -const sellingWood = function(m, n, prices) { - const dp = Array.from({ length: m + 1 }, () => Array(n + 1).fill(0)) - - for(const [h, w, p] of prices) dp[h][w] = p - for(let i = 1; i <= m; i++) { - for(let j = 1; j <= n; j++) { - for(let k = 1; k <= i / 2; k++) { - dp[i][j] = Math.max(dp[i][j], dp[k][j] + dp[i - k][j]) - } - - for(let k = 1; k <= j / 2; k++) { - dp[i][j] = Math.max(dp[i][j], dp[i][k] + dp[i][j - k]) - } - } - } - - return dp[m][n] -}; diff --git a/2318-number-of-distinct-roll-sequences.js b/2318-number-of-distinct-roll-sequences.js index 14e19cee..d08825ea 100644 --- a/2318-number-of-distinct-roll-sequences.js +++ b/2318-number-of-distinct-roll-sequences.js @@ -47,41 +47,3 @@ const distinctSequences = function(n) { } }; -// another - - -/** - * @param {number} n - * @return {number} - */ -const dp = MultidimensionalArray(0, 1e4 + 1, 7, 7) -const distinctSequences = function (n, p = 0, pp = 0) { - const mod = 1e9 + 7 - if (n === 0) return 1 - if (dp[n][p][pp] === 0) { - for (let d = 1; d < 7; d++) { - if (d !== p && d !== pp && (p === 0 || gcd(d, p) === 1)) { - dp[n][p][pp] = (dp[n][p][pp] + distinctSequences(n - 1, d, p)) % mod - } - } - } - - return dp[n][p][pp] -} - -function gcd(a, b) { - return b === 0 ? a : gcd(b, a % b) -} - -function MultidimensionalArray(defaultValue, ...args) { - if (args.length === 1) { - return Array(args[0]).fill(defaultValue) - } - const res = [] - - for (let i = 0, n = args[0]; i < n; i++) { - res.push(MultidimensionalArray(defaultValue, ...args.slice(1))) - } - - return res -} diff --git a/2320-count-number-of-ways-to-place-houses.js b/2320-count-number-of-ways-to-place-houses.js index b1d6f302..1924abd5 100644 --- a/2320-count-number-of-ways-to-place-houses.js +++ b/2320-count-number-of-ways-to-place-houses.js @@ -14,22 +14,3 @@ const countHousePlacements = function(n) { return total * total % mod }; -// another - -/** - * @param {number} n - * @return {number} - */ -var countHousePlacements = function(n) { - const mod = 1e9 + 7 - let f0 = 1; - let f1 = 1; - for (let i = 1; i < n; i++) { - let nf0 = (f0 + f1) % mod; - let nf1 = f0; - f0 = nf0; - f1 = nf1; - } - let m = (f0 + f1) % mod; - return BigInt(m) * BigInt(m) % BigInt(mod) -}; diff --git a/2321-maximum-score-of-spliced-array.js b/2321-maximum-score-of-spliced-array.js index 84fbf277..119eb540 100644 --- a/2321-maximum-score-of-spliced-array.js +++ b/2321-maximum-score-of-spliced-array.js @@ -33,33 +33,3 @@ var maximumsSplicedArray = function(nums1, nums2) { return Math.max(s2 + max1, s1 - min2); }; -// another - -/** - * @param {number[]} nums1 - * @param {number[]} nums2 - * @return {number} - */ -const maximumsSplicedArray = function (nums1, nums2) { - let sum1 = 0, - sum2 = 0, - max1 = 0, - max2 = 0, - ac1 = 0, - ac2 = 0 - sum1 = nums1.reduce((ac, e) => ac + e, 0) - sum2 = nums2.reduce((ac, e) => ac + e, 0) - const { max } = Math - let res = max(sum1, sum2) - for (let i = 0, n = nums1.length; i < n; i++) { - ac1 += nums1[i] - nums2[i] - ac2 += nums2[i] - nums1[i] - max1 = max(max1, ac1) - max2 = max(max2, ac2) - if(ac1 < 0) ac1 = 0 - if(ac2 < 0) ac2 = 0 - } - res = max(res, sum1 + max2, sum2 + max1) - - return res -} diff --git a/2322-minimum-score-after-removals-on-a-tree.js b/2322-minimum-score-after-removals-on-a-tree.js index fe2fc586..6a1bd59f 100644 --- a/2322-minimum-score-after-removals-on-a-tree.js +++ b/2322-minimum-score-after-removals-on-a-tree.js @@ -77,65 +77,3 @@ const minimumScore = function (nums, edges) { return res } -// another - -/** - * @param {number[]} nums - * @param {number[][]} edges - * @return {number} - */ -var minimumScore = function (nums, edges) { - let n = nums.length, - ans = Infinity - let visited = Array(n).fill(0) - let pc = [] - let adj = Array.from({ length: n }, () => []) - let child_xor = Array(n).fill(0) - let childs = Array.from({ length: n }, () => Array(n).fill(false)) - const { min, max } = Math - let par = Array(n).fill(0) - - // Creating an adjacency matrix - for (const edge of edges) - adj[edge[0]].push(edge[1]), adj[edge[1]].push(edge[0]) - - dfs(0) - - // console.log(childs) - // console.log(pc) - for (let i = 0; i < pc.length; i++) - for (let j = i + 1; j < pc.length; j++) { - // removing an edge i and j - let a = pc[i][1], - b = pc[j][1] // node that will come below when you delete an edge i and j - let xa = child_xor[a], - xb = child_xor[b], - xc = child_xor[0] - // console.log(a,b) - if (childs[a][b]) (xc ^= xa), (xa ^= xb) - else (xc ^= xa), (xc ^= xb) - - ans = min(max(xa, max(xb, xc)) - min(xa, min(xb, xc)), ans) - } - - return ans - - function dfs(i) { - let ans = nums[i] - visited[i] = true - - for (let p of par) childs[p][i] = true // Defining this node as the child of all its parents - - par.push(i) - - for (let child of adj[i] || []) - if (!visited[child]) { - pc.push([i, child]) - ans ^= dfs(child) // Recurcively calculating xors - } - - par.pop() - - return (child_xor[i] = ans) - } -} diff --git a/2327-number-of-people-aware-of-a-secret.js b/2327-number-of-people-aware-of-a-secret.js index 8339fb12..cfd47c35 100644 --- a/2327-number-of-people-aware-of-a-secret.js +++ b/2327-number-of-people-aware-of-a-secret.js @@ -25,53 +25,3 @@ const peopleAwareOfSecret = function(n, delay, forget) { return res }; -// another - -/** - * @param {number} n - * @param {number} delay - * @param {number} forget - * @return {number} - */ -const peopleAwareOfSecret = function(n, delay, forget) { - let cnt = new Array(n+1).fill(0); - cnt[1] = 1; - let i = 1; - let MOD = 1_000_000_007; - while (i+delay <= n) { - for (let j = i+delay; j <= Math.min(n, i+forget-1); j++) { - cnt[j] = (cnt[j]+cnt[i])%MOD; - } - i++; - } - let res = 0; - for (let j = n; j > n-forget; j--) { - res = (res + cnt[j])%MOD; - } - return res; -}; - -// another - -/** - * @param {number} n - * @param {number} delay - * @param {number} forget - * @return {number} - */ - const peopleAwareOfSecret = function(n, delay, forget) { - const mod = 1e9 + 7 - const dp = Array(n + 1).fill(0) - const { max } = Math - dp[1] = 1 - let share = 0 - for(let i = 2; i <= n; i++) { - share = (share + dp[max(0, i - delay)] - dp[max(i - forget, 0)] + mod) % mod - dp[i] = share - } - let res = 0 - for(let i = n - forget + 1; i <= n; i++) { - res = (res + dp[i]) % mod - } - return res -}; diff --git a/2328-number-of-increasing-paths-in-a-grid.js b/2328-number-of-increasing-paths-in-a-grid.js index e6b76dd6..5cdd0262 100644 --- a/2328-number-of-increasing-paths-in-a-grid.js +++ b/2328-number-of-increasing-paths-in-a-grid.js @@ -31,53 +31,3 @@ const countPaths = function (grid) { } } -// another - -/** - * @param {number[][]} grid - * @return {number} - */ -var countPaths = function (grid) { - const MOD = 1e9 + 7 - let res = 0 - const M = grid.length, - N = grid[0].length - - const dp = Array.from({ length: M }, () => Array(N)) - - for (let r = 0; r < M; r++) { - for (let c = 0; c < N; c++) { - res = (res + dfs(r, c)) % MOD - } - } - return res - - function dfs(r, c) { - if (dp[r][c] != null) { - return dp[r][c] - } - let res = 1 - - for (const dir of [ - [-1, 0], - [0, -1], - [1, 0], - [0, 1], - ]) { - const nr = r + dir[0], - nc = c + dir[1] - if ( - nr < 0 || - nr >= M || - nc < 0 || - nc >= N || - grid[nr][nc] <= grid[r][c] - ) { - continue - } - res = (res + dfs(nr, nc)) % MOD - } - dp[r][c] = res - return res - } -} diff --git a/233-number-of-digit-one.js b/233-number-of-digit-one.js index 82678200..f8ff1f9f 100644 --- a/233-number-of-digit-one.js +++ b/233-number-of-digit-one.js @@ -24,98 +24,3 @@ const countDigitOne = function(n) { }; -// another - -/** - * @param {number} n - * @return {number} - */ -const countDigitOne = function(n) { - return countNum(1, n + 1) -}; - -// Counts the number of `digit` in the range [0, limit) -function countNum( digit, limit) { - let count = 0; - let factor = 1; - let tail = 0; - while (limit >= 10) { - let d = limit % 10; - limit = ~~(limit / 10); - count += limit * factor; - count += d > digit ? factor : d == digit ? tail : 0; - tail += d * factor; - factor *= 10; - } - return count + (limit > digit ? factor : limit == digit ? tail : 0); -} - -// another - -/** - * @param {number} n - * @return {number} - */ -const countDigitOne = function(n) { - let count = 0 - for (let m = 1; m <= n; m *= 10) { - const a = Math.floor(n / m) - const b = n % m - if (a % 10 > 1) { - count += (Math.floor(a / 10) + 1) * m - } else if (a % 10 === 1) { - count += Math.floor(a / 10) * m + b + 1 - } else { - count += Math.floor(a / 10) * m - } - } - return count -} - -// another - -/** - * @param {number} n - * @return {number} - */ -const countDigitOne = function (n) { - if (n <= 0) return 0 - let ones = 0 - for (let i = 1, q = n; i <= n; i *= 10, q = (q / 10) >> 0) { - const pre = (n / (i * 10)) >> 0, - cur = q % 10, - suf = n % i - ones += pre * i - ones += 1 < cur ? i : 1 == cur ? suf + 1 : 0 - } - return ones -} - -// another - -/** - * @param {number} n - * @return {number} - */ -const countDigitOne = function(n) { - let res = 0, factor = 1, lower = 0, cur = 0, higher = 0 - while(~~(n / factor) !== 0) { - lower = n - (~~(n / factor)) * factor - cur = (~~(n / factor)) % 10 - higher = ~~(n / (factor * 10)) - switch(cur) { - case 0: - res += higher * factor - break - case 1: - res += higher * factor + lower + 1 - break - default: - res += (higher + 1) * factor - break - } - factor *= 10 - } - - return res -}; diff --git a/2344-minimum-deletions-to-make-array-divisible.js b/2344-minimum-deletions-to-make-array-divisible.js index cf3025b7..c336e2df 100644 --- a/2344-minimum-deletions-to-make-array-divisible.js +++ b/2344-minimum-deletions-to-make-array-divisible.js @@ -23,38 +23,3 @@ const minOperations = function (nums, numsDivide) { } } -// another - -/** - * @param {number[]} nums - * @param {number[]} numsDivide - * @return {number} - */ -var minOperations = function(nums, numsDivide) { - let div = numsDivide[0], min = Infinity - for(let i = 1, n = numsDivide.length; i < n; i++) { - div = Math.min(div, gcd(numsDivide[i], div)) - min = Math.min(min, numsDivide[i]) - } - // console.log(div) - - nums.sort((a, b) => a - b) - if(div === 1 && nums[0] !== 1) return -1 - let res = 0 - for(const e of nums) { - if(e > min) break - if(div % e === 0) { - return res - } - if(e % div !== 0) res++ - else { - return res - } - } - - return -1 - - function gcd(a, b) { - return b === 0 ? a : gcd(b, a % b) - } -}; diff --git a/235-lowest-common-ancestor-of-a-binary-search-tree.js b/235-lowest-common-ancestor-of-a-binary-search-tree.js index 205bcf3d..5ab9bfad 100644 --- a/235-lowest-common-ancestor-of-a-binary-search-tree.js +++ b/235-lowest-common-ancestor-of-a-binary-search-tree.js @@ -20,24 +20,3 @@ const lowestCommonAncestor = function(root, p, q) { return left || right }; -// another - -/** - * Definition for a binary tree node. - * function TreeNode(val) { - * this.val = val; - * this.left = this.right = null; - * } - */ -/** - * @param {TreeNode} root - * @param {TreeNode} p - * @param {TreeNode} q - * @return {TreeNode} - */ -const lowestCommonAncestor = function(root, p, q) { - while((root.val - p.val) * (root.val - q.val) > 0) { - root = root.val > p.val ? root.left : root.right - } - return root -}; diff --git a/2354-number-of-excellent-pairs.js b/2354-number-of-excellent-pairs.js index b610661b..e6841f24 100644 --- a/2354-number-of-excellent-pairs.js +++ b/2354-number-of-excellent-pairs.js @@ -28,45 +28,3 @@ const countExcellentPairs = function(nums, k) { } }; -// another - -/** - * @param {number[]} nums - * @param {number} k - * @return {number} - */ -const countExcellentPairs = function(nums, k) { - const arr = [], set = new Set(nums) - for(const e of set) { - arr.push(setBits(e)) - } - - arr.sort((a, b) => a - b) - let res = 0 - for(let i = 0, n = arr.length; i < n; i++) { - const idx = bs(arr, k - arr[i]) - res += n - idx - } - return res - - - function bs(arr, target) { - let l = 0, r = arr.length - - while(l < r) { - const mid = (l + r) >> 1 - if(arr[mid] < target) l = mid + 1 - else r = mid - } - - return l - } - function setBits(num) { - let res = 0 - while(num) { - res += num % 2 - num = num >> 1 - } - return res - } -}; diff --git a/236-lowest-common-ancestor-of-a-binary-tree.js b/236-lowest-common-ancestor-of-a-binary-tree.js index c0b40692..a8979914 100644 --- a/236-lowest-common-ancestor-of-a-binary-tree.js +++ b/236-lowest-common-ancestor-of-a-binary-tree.js @@ -46,35 +46,3 @@ function traverse(node, path = [], arr) { traverse(node.right, path.slice(0), arr) } -// another - -const lowestCommonAncestor = function(root, p, q) { - if(root === null || root === p || root === q) return root; - const left = lowestCommonAncestor(root.left, p, q); - const right = lowestCommonAncestor(root.right, p, q); - if(left && right) return root; - return left ? left : right; -}; - -// another - -/** - * Definition for a binary tree node. - * function TreeNode(val) { - * this.val = val; - * this.left = this.right = null; - * } - */ -/** - * @param {TreeNode} root - * @param {TreeNode} p - * @param {TreeNode} q - * @return {TreeNode} - */ -const lowestCommonAncestor = function(root, p, q) { - if(root == null || root === p || root === q) return root - const left = lowestCommonAncestor(root.left, p, q) - const right = lowestCommonAncestor(root.right, p, q) - if(left && right) return root - return left || right -}; diff --git a/2370-longest-ideal-subsequence.js b/2370-longest-ideal-subsequence.js index b9f66ca6..4ecba50f 100644 --- a/2370-longest-ideal-subsequence.js +++ b/2370-longest-ideal-subsequence.js @@ -28,23 +28,3 @@ const longestIdealString = function(s, k) { }; -// another - -/** - * @param {string} s - * @param {number} k - * @return {number} - */ -const longestIdealString = function(s, k) { - const n = s.length - const arr = [], a = 'a'.charCodeAt(0) - for(const ch of s) { - arr.push(ch.charCodeAt(0) - a) - } - const dp = Array(26).fill(0) - for(const e of arr) { - dp[e] = 1 + Math.max(...dp.slice(Math.max(0, e - k), e + k + 1)) - } - return Math.max(...dp) -}; - diff --git a/2376-count-special-integers.js b/2376-count-special-integers.js index c2f74b4f..8ef21dec 100644 --- a/2376-count-special-integers.js +++ b/2376-count-special-integers.js @@ -26,35 +26,3 @@ function A(m, n) { return n === 0 ? 1 : A(m, n - 1) * (m - n + 1); } -// another - -/** - * @param {number} n - * @return {number} - */ -const countSpecialNumbers = function (n) { - const s = '' + n - const dp = Array.from({ length: 11 }, () => - Array.from({ length: 2 }, () => Array(1024).fill(-1)) - ) - - return helper(0, 1, 0, s) - function helper(idx, tight = 1, mask = 0, digits) { - if (idx == digits.length) return mask !== 0 ? 1 : 0 - - if (dp[idx][tight][mask] != -1) return dp[idx][tight][mask] - - let k = tight ? +digits[idx] : 9 - let ans = 0 - - for (let i = 0; i <= k; i++) { - if (mask & (1 << i)) continue - let newMask = mask == 0 && i == 0 ? mask : mask | (1 << i) - - let nextTight = tight && i == digits[idx] ? 1 : 0 - ans += helper(idx + 1, nextTight, newMask, digits) - } - - return (dp[idx][tight][mask] = ans) - } -} diff --git a/2380-time-needed-to-rearrange-a-binary-string.js b/2380-time-needed-to-rearrange-a-binary-string.js index 08f3cfc3..124aaa0a 100644 --- a/2380-time-needed-to-rearrange-a-binary-string.js +++ b/2380-time-needed-to-rearrange-a-binary-string.js @@ -16,20 +16,3 @@ const secondsToRemoveOccurrences = function(s) { return res }; -// another - - -/** - * @param {string} s - * @return {number} - */ -var secondsToRemoveOccurrences = function(s) { - const n = s.length - let zeros = 0, seconds = 0; - for (let i = 0; i < n; ++i) { - zeros += s.charAt(i) == '0' ? 1 : 0; - if (s.charAt(i) == '1' && zeros > 0) - seconds = Math.max(seconds + 1, zeros); - } - return seconds; -}; diff --git a/2386-find-the-k-sum-of-an-array.js b/2386-find-the-k-sum-of-an-array.js index 491ad86b..64d64f4e 100644 --- a/2386-find-the-k-sum-of-an-array.js +++ b/2386-find-the-k-sum-of-an-array.js @@ -25,101 +25,3 @@ var kSum = function(nums, k) { return pq.front()[0]; }; -// another - -/** - * @param {number[]} nums - * @param {number} k - * @return {number} - */ -const kSum = function (nums, k) { - let sum = 0, - n = nums.length, - pq = new PriorityQueue((x, y) => y[0] < x[0]) - for (let i = 0; i < n; i++) { - if (nums[i] < 0) { - nums[i] *= -1 - } else { - sum += nums[i] - } - } - if (k == 1) return sum - nums.sort((x, y) => x - y) - pq.push([sum - nums[0], 0]) - for (let i = 2; i < k; i++) { - let [x, idx] = pq.pop() - if (idx + 1 < n) { - pq.push([x + nums[idx] - nums[idx + 1], idx + 1]) - pq.push([x - nums[idx + 1], idx + 1]) - } - } - return pq.peek()[0] -} - -class PriorityQueue { - constructor(comparator = (a, b) => a > b) { - this.heap = [] - this.top = 0 - this.comparator = comparator - } - size() { - return this.heap.length - } - isEmpty() { - return this.size() === 0 - } - peek() { - return this.heap[this.top] - } - push(...values) { - values.forEach((value) => { - this.heap.push(value) - this.siftUp() - }) - return this.size() - } - pop() { - const poppedValue = this.peek() - const bottom = this.size() - 1 - if (bottom > this.top) { - this.swap(this.top, bottom) - } - this.heap.pop() - this.siftDown() - return poppedValue - } - replace(value) { - const replacedValue = this.peek() - this.heap[this.top] = value - this.siftDown() - return replacedValue - } - - parent = (i) => ((i + 1) >>> 1) - 1 - left = (i) => (i << 1) + 1 - right = (i) => (i + 1) << 1 - greater = (i, j) => this.comparator(this.heap[i], this.heap[j]) - swap = (i, j) => ([this.heap[i], this.heap[j]] = [this.heap[j], this.heap[i]]) - siftUp = () => { - let node = this.size() - 1 - while (node > this.top && this.greater(node, this.parent(node))) { - this.swap(node, this.parent(node)) - node = this.parent(node) - } - } - siftDown = () => { - let node = this.top - while ( - (this.left(node) < this.size() && this.greater(this.left(node), node)) || - (this.right(node) < this.size() && this.greater(this.right(node), node)) - ) { - let maxChild = - this.right(node) < this.size() && - this.greater(this.right(node), this.left(node)) - ? this.right(node) - : this.left(node) - this.swap(node, maxChild) - node = maxChild - } - } -} diff --git a/239-sliding-window-maximum.js b/239-sliding-window-maximum.js index 3fdf50a4..d4c1da63 100644 --- a/239-sliding-window-maximum.js +++ b/239-sliding-window-maximum.js @@ -24,69 +24,3 @@ const maxSlidingWindow = function(nums, k) { return res }; -// another - -/** - * @param {number[]} nums - * @param {number} k - * @return {number[]} - */ -var maxSlidingWindow = function (nums, k) { - if (k === 0) return [] - const deque = new Deque() - for (let i = 0; i < k - 1; i++) { - while (!deque.isEmpty() && deque.last().val <= nums[i]) deque.pop() - deque.enqueue({ val: nums[i], idx: i }) - } - const result = [] - for (let i = k - 1; i < nums.length; i++) { - if (!deque.isEmpty() && deque.first().idx <= i - k) deque.dequeue() - while (!deque.isEmpty() && deque.last().val <= nums[i]) deque.pop() - deque.enqueue({ val: nums[i], idx: i }) - result.push(deque.first().val) - } - return result -} - -class Deque { - constructor() { - this.head = new Node() - this.tail = this.head - } - - isEmpty() { - return this.head.next === null - } - - first() { - return this.head.next.value - } - - last() { - return this.tail.value - } - - dequeue() { - this.head = this.head.next - this.head.prev = null - } - - enqueue(value) { - this.tail.next = new Node(value) - this.tail.next.prev = this.tail - this.tail = this.tail.next - } - - pop() { - this.tail = this.tail.prev - this.tail.next = null - } -} - -class Node { - constructor(value) { - this.value = value - this.next = null - this.prev = null - } -} diff --git a/2392-build-a-matrix-with-conditions.js b/2392-build-a-matrix-with-conditions.js index 8bd4d8ac..1c19dfbd 100644 --- a/2392-build-a-matrix-with-conditions.js +++ b/2392-build-a-matrix-with-conditions.js @@ -54,77 +54,3 @@ const buildMatrix = function (k, rowConditions, colConditions) { -// another - -/** - * @param {number} k - * @param {number[][]} rowConditions - * @param {number[][]} colConditions - * @return {number[][]} - */ -const initializeGraph = (n) => { - let g = [] - for (let i = 0; i < n; i++) { - g.push([]) - } - return g -} -const packDGInDegree = (g, edges, indegree) => { - for (const [u, v] of edges) { - g[u].unshift(v) - indegree[v]++ - } -} -const initialize2DArray = (n, m) => { - let d = [] - for (let i = 0; i < n; i++) { - let t = Array(m).fill(0) - d.push(t) - } - return d -} - -const buildMatrix = (k, rowConditions, colConditions) => { - let gr = make(k, rowConditions), - gc = make(k, colConditions), - d = initialize2DArray(k, 2), - res = initialize2DArray(k, k) - if (gr.length == 0 || gc.length == 0) return [] - for (let i = 0; i < k; i++) { - d[gr[i] - 1][0] = i - d[gc[i] - 1][1] = i - } - for (let i = 0; i < k; i++) { - let [x, y] = d[i] - res[x][y] = i + 1 - } - return res -} - -const make = (n, edges) => { - let g = initializeGraph(n + 1), - deg = Array(n + 1).fill(0) - packDGInDegree(g, edges, deg) - return topologicalSort_start_1(g, deg) -} - -const topologicalSort_start_1 = (g, indegree) => { - let res = [], - q = [], - n = g.length - 1 - for (let i = 1; i <= n; i++) { - if (indegree[i] == 0) q.push(i) - } - while (q.length) { - let cur = q.shift() - res.push(cur) - for (const child of g[cur]) { - indegree[child]-- - if (indegree[child] == 0) q.push(child) - } - } - for (let i = 1; i <= n; i++) { - if (indegree[i] > 0) return [] - } - return res -} diff --git a/2400-number-of-ways-to-reach-a-position-after-exactly-k-steps.js b/2400-number-of-ways-to-reach-a-position-after-exactly-k-steps.js index e11d1929..c3993629 100644 --- a/2400-number-of-ways-to-reach-a-position-after-exactly-k-steps.js +++ b/2400-number-of-ways-to-reach-a-position-after-exactly-k-steps.js @@ -20,41 +20,3 @@ const numberOfWays = function(startPos, endPos, k) { return dp[k][abs(startPos - endPos)]; }; -// another - -/** - * @param {number} startPos - * @param {number} endPos - * @param {number} k - * @return {number} - */ -var numberOfWays = function(startPos, endPos, k) { - const ll = BigInt, mod = ll(1e9 + 7), N = 1005; - - let fact, ifact, inv; - const comb_init = () => { - fact = Array(N).fill(0); - ifact = Array(N).fill(0); - inv = Array(N).fill(0); - fact[0] = ifact[0] = inv[1] = 1n; - for (let i = 2; i < N; i++) inv[i] = (mod - mod / ll(i)) * inv[mod % ll(i)] % mod; - for (let i = 1; i < N; i++) { - fact[i] = fact[i - 1] * ll(i) % mod; - ifact[i] = ifact[i - 1] * inv[i] % mod; - } - }; - - const comb = (n, k) => { - if (n < k || k < 0) return 0; - return fact[n] * ifact[k] % mod * ifact[n - k] % mod; - }; - - comb_init(); - let res = 0n; - for (let i = 0; i <= k; i++) { - let moveRight = i, moveLeft = k - i; - if (startPos + moveRight - moveLeft == endPos) res += comb(k, i); - } - return res; - -}; diff --git a/2401-longest-nice-subarray.js b/2401-longest-nice-subarray.js index 9cd9ef95..f1fe2227 100644 --- a/2401-longest-nice-subarray.js +++ b/2401-longest-nice-subarray.js @@ -19,35 +19,3 @@ const longestNiceSubarray = function (nums) { return res } -// another - -/** - * @param {number[]} nums - * @return {number} - */ -var longestNiceSubarray = function(nums) { - let max = 1; - let stack = []; - for(let i =0;i a[0] - b[0]); - for(let i = 0 ; i < meetings.length ; i++){ - let minRoom = -1; - let minTime = Number.MAX_SAFE_INTEGER; - for(let j = 0 ; j < n ; j++){ - if(freeTime[j] <= meetings[i][0]){ - count[j]++; - freeTime[j] = meetings[i][1]; - minRoom = -1; - break; - } - if(freeTime[j] < minTime){ - minTime = freeTime[j]; - minRoom = j; - } - } - if(minRoom !== -1){ - count[minRoom]++; - freeTime[minRoom] += meetings[i][1] - meetings[i][0]; - } - } - - let ans = 0; - let maxCount = count[0]; - for(let i = 1 ; i < n ; i++){ - if(count[i] > maxCount){ - ans = i; - maxCount = count[i]; - } - } - return ans; -}; diff --git a/2406.divide-intervals-into-minimum-number-of-groups.js b/2406.divide-intervals-into-minimum-number-of-groups.js index aaecb9aa..efcb2af4 100644 --- a/2406.divide-intervals-into-minimum-number-of-groups.js +++ b/2406.divide-intervals-into-minimum-number-of-groups.js @@ -19,30 +19,3 @@ var minGroups = function(intervals) { return res }; -// another - -/** - * @param {number[][]} intervals - * @return {number} - */ -const minGroups = function(intervals) { - const hash = {} - for(const [s, e] of intervals) { - if(hash[s] == null) hash[s] = 0 - if(hash[e + 1] == null) hash[e + 1] = 0 - hash[s]++ - hash[e + 1]-- - } - const keys = Object.keys(hash) - keys.sort((a, b) => a - b) - const n = keys.length - - const arr = Array(n).fill(0) - arr[0] = hash[keys[0]] - let res = arr[0] - for(let i = 1; i < n; i++) { - arr[i] = hash[keys[i]] + arr[i - 1] - res = Math.max(res, arr[i]) - } - return res -}; diff --git a/2430-maximum-deletions-on-a-string.js b/2430-maximum-deletions-on-a-string.js index 4f04ae64..983edb83 100644 --- a/2430-maximum-deletions-on-a-string.js +++ b/2430-maximum-deletions-on-a-string.js @@ -20,67 +20,3 @@ const deleteString = function (s) { } } -// another - -/** - * @param {string} s - * @return {number} - */ -const deleteString = function (s) { - const dp = Array(4000).fill(0), lps = Array(4000).fill(0) - for (let k = s.length - 1; k >= 0; --k) { - dp[k] = 1; - for (let i = 1, j = 0; dp[k] <= s.length - i - k + 1; ++i) { - while (j && s[i + k] != s[j + k]) j = Math.max(0, lps[j] - 1); - j += s[i + k] == s[j + k]; - lps[i] = j; - if (i % 2) { - const len = ~~((i + 1) / 2); - if (lps[len * 2 - 1] == len) { - dp[k] = Math.max(dp[k], 1 + dp[k + len]); - } - } - } - } - return dp[0]; -} - - -// another - - -/** - * @param {string} s - * @return {number} - */ -const deleteString = function (t) { - let n = t.length - const set = new Set(t.split('')) - if (set.size == 1) return n - - let s = t.split('') - if (n === 1 || (n === 2 && s[0] !== s[1])) return 1 - if (n === 2 && s[0] === s[1]) return 2 - if (n === 3 && s[0] === s[1]) return s[1] === s[2] ? 3 : 2 - else if (n === 3) return 1 - const f = new Array(n).fill(null) - dfsSearchWithMemory(0) - return f[0] - - function dfsSearchWithMemory(i) { - if (i >= n) return 0 - if (f[i] !== null) return f[i] - if (i === n - 1) return (f[i] = 1) - let max = 0, - cur = 0, - j = i + 1 - for (j = i + 1; j <= ~~((n - i) / 2 + i); j++) { - if (t.slice(j).startsWith(t.slice(i, j))) { - cur = 1 + dfsSearchWithMemory(j) - if (cur > max) max = cur - } - } - if (j > (n - i) / 2 + i && max === 0) return (f[i] = 1) - return (f[i] = max) - } -} diff --git a/2434-using-a-robot-to-print-the-lexicographically-smallest-string.js b/2434-using-a-robot-to-print-the-lexicographically-smallest-string.js index 9ffd6f29..3987531f 100644 --- a/2434-using-a-robot-to-print-the-lexicographically-smallest-string.js +++ b/2434-using-a-robot-to-print-the-lexicographically-smallest-string.js @@ -38,38 +38,3 @@ const robotWithString = function (s) { } -// another - -const ord = (c) => c.charCodeAt(); -const char = (ascii) => String.fromCharCode(ascii); -/** - * @param {string} s - * @return {string} - */ - -const robotWithString = (s) => { - let f = Array(26).fill(0), t = [], res = ''; - for (const c of s) f[ord(c) - 97]++; - for (const c of s) { - f[ord(c) - 97]--; - t.push(c); - while (t.length) { - let find = false; - for (let i = 0; i < 26; i++) { - let curC = char(i + 97); - if (curC < t[t.length - 1]) { // check if can find smaller char < t's last char, in the rest of S - if (f[i] > 0) { - find = true; - break; - } - } - } - if (find) { // find means there is lexical smaller one, by moving much more right - break; - } else { // not find, current is lexical smaller - res += t.pop(); - } - } - } - return res; -}; diff --git a/2444-count-subarrays-with-fixed-bounds.js b/2444-count-subarrays-with-fixed-bounds.js index 9ad838c7..fcc3fbc0 100644 --- a/2444-count-subarrays-with-fixed-bounds.js +++ b/2444-count-subarrays-with-fixed-bounds.js @@ -24,24 +24,3 @@ const countSubarrays = function(nums, minK, maxK) { return res }; -// another - -/** - * @param {number[]} nums - * @param {number} minK - * @param {number} maxK - * @return {number} - */ -var countSubarrays = function(nums, minK, maxK) { - let res = 0, j = 0, jmin = -1, jmax = -1, n = nums.length; - for (let i = 0; i < n; ++i) { - if (nums[i] < minK || nums[i] > maxK) { - jmin = jmax = -1; - j = i + 1; - } - if (nums[i] == minK) jmin = i; - if (nums[i] == maxK) jmax = i; - res += Math.max(0, Math.min(jmin, jmax) - j + 1); - } - return res; -}; diff --git a/246-strobogrammatic-number.js b/246-strobogrammatic-number.js index c02c9772..980811e6 100644 --- a/246-strobogrammatic-number.js +++ b/246-strobogrammatic-number.js @@ -23,22 +23,3 @@ const isStrobogrammatic = function(num) { return num === arr.reverse().join('') }; -// another - -const isStrobogrammatic = function(num) { - const map = { 0: '0', 1: '1', 8: '8', 6: '9', 9: '6' } - let left = 0 - let right = num.length - 1 - - while (left < right) { - const leftNum = num[left] - const rightNum = num[right] - if (map[leftNum] != rightNum) return false - left++ - right-- - } - if (right == left) { - if (!map[num[right]] || num[right] == '9' || num[right] == '6') return false - } - return true -} diff --git a/2461-maximum-sum-of-distinct-subarrays-with-length-k.js b/2461-maximum-sum-of-distinct-subarrays-with-length-k.js index 932c8383..c4773f8b 100644 --- a/2461-maximum-sum-of-distinct-subarrays-with-length-k.js +++ b/2461-maximum-sum-of-distinct-subarrays-with-length-k.js @@ -31,44 +31,3 @@ const maximumSubarraySum = function (nums, k) { return res } -// another - -/** - * @param {number[]} nums - * @param {number} k - * @return {number} - */ -const maximumSubarraySum = function(nums, k) { - let res = 0 - const n = nums.length - - const preSum = Array(n).fill(0) - preSum[0] = nums[0] - for(let i = 1; i < n; i++) { - preSum[i] = preSum[i - 1] + nums[i] - } - - const set = new Set() - - const lastHash = {} - - for(let i = 0; i < n; i++) { - const cur = nums[i] - lastHash[cur] = i - if(i < k - 1) set.add(cur) - else if(i === k - 1) { - set.add(cur) - if(set.size === k) { - res = preSum[i] - } - } else { - if(lastHash[nums[i - k]] == i - k) set.delete(nums[i - k]) - set.add(nums[i]) - if(set.size === k) { - res = Math.max(res, preSum[i] - preSum[i - k]) - } - } - } - - return res -}; diff --git a/247-strobogrammatic-number-ii.js b/247-strobogrammatic-number-ii.js index 17d7e188..7c6630c4 100644 --- a/247-strobogrammatic-number-ii.js +++ b/247-strobogrammatic-number-ii.js @@ -20,26 +20,3 @@ const recursionHelper = function(n, m) { return res } -// another - -/** - * @param {number} n - * @return {string[]} - */ -const findStrobogrammatic = function(n) { - let cur, ans - ans = (n & 1) == 0 ? [''] : ['0', '1', '8'] - if (n < 2) return ans - for (; n > 1; n -= 2) { - cur = ans - ans = [] - for (let i of cur) { - if (n > 3) ans.push('0' + i + '0') - ans.push('1' + i + '1') - ans.push('8' + i + '8') - ans.push('6' + i + '9') - ans.push('9' + i + '6') - } - } - return ans -} diff --git a/2492-minimum-score-of-a-path-between-two-cities.js b/2492-minimum-score-of-a-path-between-two-cities.js index 43c7f362..35195f71 100644 --- a/2492-minimum-score-of-a-path-between-two-cities.js +++ b/2492-minimum-score-of-a-path-between-two-cities.js @@ -29,61 +29,3 @@ const minScore = function(n, roads) { } }; -// another - -class UnionFind { - constructor() { - this.sizes = new Map() - this.parents = new Map() - } - - find(x){ - if (x !== (this.parents.get(x) ?? x)) { - this.parents.set(x, this.find(this.parents.get(x) ?? x)); - } - return this.parents.get(x) ?? x; - } - size(x) { - return (this.sizes.get(this.find(x)) ?? 1); - } - connected(p, q) { - return this.find(p) == this.find(q); - } - union(a, b) { - const fa = this.find(a); - const fb = this.find(b); - if (fa == fb) { - return; - } - const sa = this.sizes.get(fa) ?? 1; - const sb = this.sizes.get(fb) ?? 1; - - if (sa < sb) { - this.parents.set(fa, fb); - this.sizes.set(fb, sb + sa); - } else { - this.parents.set(fb, fa); - this.sizes.set(fa, sb + sa); - } - } -} -/** - * @param {number} n - * @param {number[][]} roads - * @return {number} - */ -var minScore = function(n, roads) { - const uf = new UnionFind(); - - for (const [a, b] of roads) { - uf.union(a, b); - } - let ans = Infinity; - - for (const [i, j, d] of roads) { - if (uf.connected(1, i) && uf.connected(1, j)) { - ans = Math.min(ans, d); - } - } - return ans; -}; diff --git a/2493-divide-nodes-into-the-maximum-number-of-groups.js b/2493-divide-nodes-into-the-maximum-number-of-groups.js index 371bf321..c38de93b 100644 --- a/2493-divide-nodes-into-the-maximum-number-of-groups.js +++ b/2493-divide-nodes-into-the-maximum-number-of-groups.js @@ -86,103 +86,3 @@ const magnificentSets = function (n, edges) { return ans; }; -// another - -/////////////////////// Template //////////////////////////////////////// -const initializeGraph = (n) => { let g = []; for (let i = 0; i < n; i++) { g.push([]); } return g; }; -const packUG = (g, edges) => { for (const [u, v] of edges) { g[u].push(v); g[v].push(u); } }; -const initialize2DArray = (n, m) => { let d = []; for (let i = 0; i < n; i++) { let t = Array(m).fill(Number.MAX_SAFE_INTEGER); d.push(t); } return d; }; - -function DJSet(n) { - // parent[i] < 0, -parent[i] is the group size which root is i. example: (i -> parent[i] -> parent[parent[i]] -> parent[parent[parent[i]]] ...) - // parent[i] >= 0, i is not the root and parent[i] is i's parent. example: (... parent[parent[parent[i]]] -> parent[parent[i]] -> parent[i] -> i) - let parent = Array(n).fill(-1); - return { find, union, count, equiv, par } - function find(x) { - return parent[x] < 0 ? x : parent[x] = find(parent[x]); - } - function union(x, y) { - x = find(x); - y = find(y); - if (x != y) { - if (parent[x] < parent[y]) [x, y] = [y, x]; - parent[x] += parent[y]; - parent[y] = x; - } - return x == y; - } - function count() { // total groups - return parent.filter(v => v < 0).length; - } - function equiv(x, y) { // isConnected - return find(x) == find(y); - } - function par() { - return parent; - } -} - -const isBipartite = (g) => { - let n = g.length, start = 1, visit = Array(n).fill(false), q = [], color = Array(n).fill(0); // 0: no color, 1: red -1: blue - for (let i = start; i < n; i++) { - if (color[i] != 0) continue; - q.push(i); - color[i] = 1; - if (visit[i]) continue; - while (q.length) { - let cur = q.shift(); - if (visit[cur]) continue; - for (const child of g[cur]) { - if (color[child] == color[cur]) return false; - if (color[child]) continue; - color[child] = -color[cur]; - q.push(child); - } - } - } - return true; -}; -//////////////////////////////////////////////////////////////////// -/** - * @param {number} n - * @param {number[][]} edges - * @return {number} - */ -const magnificentSets = (n, edges) => { - let g = initializeGraph(n + 1), ds = new DJSet(n + 1); - packUG(g, edges); - if (!isBipartite(g)) return -1; - let d = initialize2DArray(n + 1, n + 1), res = Array(n + 1).fill(0); - for (let i = 1; i <= n; i++) d[i][i] = 0; - for (const [u, v] of edges) { - d[u][v] = 1; - d[v][u] = 1; - ds.union(u, v); - } - wf(d); - for (let i = 1; i <= n; i++) { - let max = 0; - for (let j = 1; j <= n; j++) { - if (d[i][j] >= Number.MAX_SAFE_INTEGER) continue; - max = Math.max(max, d[i][j]); - } - let par = ds.find(i); - res[par] = Math.max(res[par], max + 1); - } - let ans = 0; - for (let i = 1; i <= n; i++) ans += res[i]; - return ans; -}; - -const wf = (g) => { - let n = g.length; - for (let k = 0; k < n; k++) { - for (let i = 0; i < n; i++) { - for (let j = 0; j < n; j++) { - if (g[i][j] > g[i][k] + g[k][j]) { - g[i][j] = g[i][k] + g[k][j]; - } - } - } - } -}; diff --git a/25-reverse-nodes-in-k-group.js b/25-reverse-nodes-in-k-group.js index bf6a75e2..c913fd9c 100644 --- a/25-reverse-nodes-in-k-group.js +++ b/25-reverse-nodes-in-k-group.js @@ -29,129 +29,3 @@ const reverseKGroup = function(head, k) { return dmy.next } -// another - -/** - * Definition for singly-linked list. - * function ListNode(val, next) { - * this.val = (val===undefined ? 0 : val) - * this.next = (next===undefined ? null : next) - * } - */ -/** - * @param {ListNode} head - * @param {number} k - * @return {ListNode} - */ -const reverseKGroup = function (head, k) { - if(head == null) return head - const dummy = new ListNode() - dummy.next = head - let n = 0, cur = head - while(cur) { - n++ - cur = cur.next - } - if(n < k) return head - let pre = dummy, tail = head - - for(let i = 0; i + k <= n; i += k) { - for(let j = 1; j < k; j++) { - const tmp = pre.next - pre.next = tail.next - tail.next = tail.next.next - pre.next.next = tmp - } - pre = tail - tail = tail.next - } - - return dummy.next -} - - - -// another - -/** - * Definition for singly-linked list. - * function ListNode(val, next) { - * this.val = (val===undefined ? 0 : val) - * this.next = (next===undefined ? null : next) - * } - */ -/** - * @param {ListNode} head - * @param {number} k - * @return {ListNode} - */ -const reverseKGroup = function (head, k) { - let ptr = head - let ktail = null - - // Head of the final, moified linked list - let new_head = null - - // Keep going until there are nodes in the list - while (ptr != null) { - let count = 0 - - // Start counting nodes from the head - ptr = head - - // Find the head of the next k nodes - while (count < k && ptr != null) { - ptr = ptr.next - count += 1 - } - - // If we counted k nodes, reverse them - if (count == k) { - // Reverse k nodes and get the new head - let revHead = reverseLinkedList(head, k) - - // new_head is the head of the final linked list - if (new_head == null) new_head = revHead - - // ktail is the tail of the previous block of - // reversed k nodes - if (ktail != null) ktail.next = revHead - - ktail = head - head = ptr - } - } - - // attach the final, possibly un-reversed portion - if (ktail != null) ktail.next = head - - return new_head == null ? head : new_head -} - -function reverseLinkedList(head, k) { - // Reverse k nodes of the given linked list. - // This function assumes that the list contains - // atleast k nodes. - let new_head = null - let ptr = head - - while (k > 0) { - // Keep track of the next node to process in the - // original list - let next_node = ptr.next - - // Insert the node pointed to by "ptr" - // at the beginning of the reversed list - ptr.next = new_head - new_head = ptr - - // Move on to the next node - ptr = next_node - - // Decrement the count of nodes to be reversed by 1 - k-- - } - - // Return the head of the reversed list - return new_head -} diff --git a/251-flatten-2d-vector.js b/251-flatten-2d-vector.js index 1a1e2f76..3c38d228 100644 --- a/251-flatten-2d-vector.js +++ b/251-flatten-2d-vector.js @@ -28,37 +28,3 @@ Vector2D.prototype.hasNext = function() { * var param_2 = obj.hasNext() */ -// another - -/** - * @param {number[][]} v - */ -const Vector2D = function(v) { - this.iterator = v[Symbol.iterator]() - this.row = this.iterator.next() - this.idx = 0 -} -/** - * @return {number} - */ -Vector2D.prototype.next = function() { - if (this.hasNext()) { - return this.row.value[this.idx++] - } -} -/** - * @return {boolean} - */ -Vector2D.prototype.hasNext = function() { - while (this.row.done == false && this.idx == this.row.value.length) { - this.row = this.iterator.next() - this.idx = 0 - } - return this.row.done == false -} -/** - * Your Vector2D object will be instantiated and called as such: - * var obj = new Vector2D(v) - * var param_1 = obj.next() - * var param_2 = obj.hasNext() - */ diff --git a/253-meeting-rooms-ii.js b/253-meeting-rooms-ii.js index fb5d5915..11d8aac7 100644 --- a/253-meeting-rooms-ii.js +++ b/253-meeting-rooms-ii.js @@ -34,128 +34,3 @@ const minMeetingRooms = function(intervals) { return res }; -// another - -/** - * @param {number[][]} intervals - * @return {number} - */ -const minMeetingRooms = function(intervals) { - const n = intervals.length - const start = Array(n), end = Array(n) - for(let i = 0; i < n; i++) { - start[i] = intervals[i][0] - end[i] = intervals[i][1] - } - start.sort((a, b) => a - b) - end.sort((a, b) => a - b) - - let res = 0, endIdx = 0 - for(let i = 0; i < n; i++) { - if(start[i] < end[endIdx]) res++ - else endIdx++ - } - - return res -} - - -// another - -/** - * @param {number[][]} intervals - * @return {number} - */ -const minMeetingRooms = function(intervals) { - let res = 0 - const sArr = [], eArr = [], n = intervals.length - for(const [s, e] of intervals) { - sArr.push(s) - eArr.push(e) - } - sArr.sort((a, b) => a - b) - eArr.sort((a, b) => a - b) - for(let i = 0, j = 0; i < n && j < n;) { - const s = sArr[i], e = eArr[j] - if(s < e) { - res++ - i++ - } else { - j++ - i++ - } - } - - return res -} - -// another - -/** - * @param {number[][]} intervals - * @return {number} - */ -const minMeetingRooms = function(intervals) { - intervals.sort((a, b) => a[0] - b[0] || a[1] - b[1]) - const n = intervals.length - const pq = new MinPriorityQueue() - let res = 0 - for(const [s, e] of intervals) { - while(!pq.isEmpty() && s >= pq.front().element) { - pq.dequeue() - } - pq.enqueue(e) - res = Math.max(res, pq.size()) - } - - return res -} - -// another - -/** - * @param {number[][]} intervals - * @return {number} - */ -const minMeetingRooms = function(intervals) { - const hash = {} - for(const [s, e] of intervals) { - hash[s] = (hash[s] || 0) + 1 - hash[e] = (hash[e] || 0) - 1 - } - let res = 0, cur = 0 - const keys = Object.keys(hash).map(e => +e) - keys.sort((a, b) => a - b) - for(const k of keys) { - cur += hash[k] - res = Math.max(res, cur) - } - return res -}; - -// another - -/** - * @param {number[][]} intervals - * @return {number} - */ -const minMeetingRooms = function(intervals) { - const n = intervals.length - const limit = 1e6 + 1 - const arr = Array(limit).fill(0) - let res = 0 - - for(const [start, end] of intervals) { - arr[start]++ - arr[end]-- - } - - for(let i = 1; i < limit; i++) { - arr[i] += arr[i - 1] - } - for(let i = 0; i < limit; i++) { - res = Math.max(res, arr[i]) - } - - return res -}; diff --git a/2537-count-the-number-of-good-subarrays.js b/2537-count-the-number-of-good-subarrays.js index 58df8f51..5e851cc0 100644 --- a/2537-count-the-number-of-good-subarrays.js +++ b/2537-count-the-number-of-good-subarrays.js @@ -34,26 +34,3 @@ const countGood = function(nums, k) { } }; -// another - - -/** - * @param {number[]} nums - * @param {number} k - * @return {number} - */ -const countGood = function(nums, k) { - let res = 0; - const count = new Map() - for(let i = 0, j = 0; j < nums.length; ++j){ - k -= count.get(nums[j]) || 0; - count.set(nums[j], (count.get(nums[j]) || 0) + 1); - while(k <= 0) { - count.set(nums[i],count.get(nums[i]) - 1); - k += count.get(nums[i]); - i++ - } - res += i; - } - return res; -}; diff --git a/2567-minimum-score-by-changing-two-elements.js b/2567-minimum-score-by-changing-two-elements.js index cf2a5321..b3973abe 100644 --- a/2567-minimum-score-by-changing-two-elements.js +++ b/2567-minimum-score-by-changing-two-elements.js @@ -34,18 +34,3 @@ const minimizeSum = function(nums) { return min(l1 - s3, l2 - s2, l3 - s1) }; -// another - - -/** - * @param {number[]} nums - * @return {number} - */ -const minimizeSum = function(nums) { - nums.sort((a, b) => a - b) - const { max, min, abs } = Math - const res1 = nums.at(-1) - nums[2] - const res2 = nums.at(-2) - nums[1] - const res3 = nums.at(-3) - nums[0] - return min(res1, res2, res3) -}; diff --git a/2571-minimum-operations-to-reduce-an-integer-to-0.js b/2571-minimum-operations-to-reduce-an-integer-to-0.js index 70758216..d12b034a 100644 --- a/2571-minimum-operations-to-reduce-an-integer-to-0.js +++ b/2571-minimum-operations-to-reduce-an-integer-to-0.js @@ -27,28 +27,3 @@ const minOperations = function(n) { return res + cnt(n) }; -// another - - -function dec2bin(dec) { - return (dec >>> 0).toString(2); -} -function bitCnt(s) { - let res = 0 - for(const e of s) { - if(e === '1') res++ - } - return res -} -/** - * @param {number} n - * @return {number} - */ -var minOperations = function(n) { - if(n === 0) return 0 - if(bitCnt(dec2bin(n)) === 1) return 1 - const lowBit = n & -n - let low = minOperations(n + lowBit); - let high = minOperations(n - lowBit); - return Math.min(low, high) + 1; -}; diff --git a/2576-find-the-maximum-number-of-marked-indices.js b/2576-find-the-maximum-number-of-marked-indices.js index de2fe1c7..94d97fdc 100644 --- a/2576-find-the-maximum-number-of-marked-indices.js +++ b/2576-find-the-maximum-number-of-marked-indices.js @@ -16,17 +16,3 @@ const maxNumOfMarkedIndices = function(nums) { return res }; -// another - -/** - * @param {number[]} nums - * @return {number} - */ -const maxNumOfMarkedIndices = function(nums) { - let i = 0, n = nums.length; - nums.sort((a, b) => a - b) - for (let j = n - (~~(n / 2)); j < n; ++j) { - i += 2 * nums[i] <= nums[j] ? 1 : 0; - } - return i * 2; -}; diff --git a/258-add-digits.js b/258-add-digits.js index 510713b0..211f9029 100755 --- a/258-add-digits.js +++ b/258-add-digits.js @@ -13,15 +13,3 @@ const addDigits = function(num) { return +res; }; -// another - -/** - * @param {number} num - * @return {number} - */ -const addDigits = function(num) { - return 1 + (num - 1) % 9; -}; - -console.log(addDigits(0)); -console.log(addDigits(38)); diff --git a/2584-split-the-array-to-make-coprime-products.js b/2584-split-the-array-to-make-coprime-products.js index 3d148bc2..e1dcbb56 100644 --- a/2584-split-the-array-to-make-coprime-products.js +++ b/2584-split-the-array-to-make-coprime-products.js @@ -38,71 +38,3 @@ function getPrimeFactors(n) { return counts; } -// another - -/** - * @param {number[]} nums - * @return {number} - */ -const findValidSplit = function (nums) { - const map = new Map() - const n = nums.length - const max = Math.max(...nums) - const primes = Eratosthenes(max) - - for (let i = 0; i < n; i++) { - let x = nums[i] - for (const p of primes) { - if (p * p > x && x > 1) { - if (!map.has(x)) { - map.set(x, [i, i]) - } - map.get(x)[1] = i - break - } - - if (x % p === 0) { - if (!map.has(p)) { - map.set(p, [i, i]) - } - const a = map.get(p) - a[1] = i - } - while (x % p === 0) x = x / p - } - } - - const diff = Array(n + 1).fill(0) - for (const [k, v] of map) { - const [s, e] = v - // if(s === e) continue - diff[s] += 1 - diff[e] -= 1 - } - // console.log(diff) - let sum = 0 - for (let i = 0; i < n - 1; i++) { - sum += diff[i] - if (sum === 0) return i - } - - return -1 -} - -function Eratosthenes(n) { - const q = Array(n + 1).fill(0) - const primes = [] - for (let i = 2; i <= Math.sqrt(n); i++) { - if (q[i] == 1) continue - let j = i * 2 - while (j <= n) { - q[j] = 1 - j += i - } - } - for (let i = 2; i <= n; i++) { - if (q[i] == 0) primes.push(i) - } - return primes -} - diff --git a/260-single-number-III.js b/260-single-number-III.js index de88fee7..4bf844c6 100755 --- a/260-single-number-III.js +++ b/260-single-number-III.js @@ -15,37 +15,3 @@ const singleNumber = function(nums) { return Object.keys(hash).map(el => +el); }; -// another - -/** - * @param {number[]} nums - * @return {number[]} - */ -const singleNumber = function(nums) { - const s = new Set() - for(let el of nums) { - if(s.has(el)) s.delete(el) - else s.add(el) - } - return Array.from(s) -}; - -// another - -/** - * @param {number[]} nums - * @return {number[]} - */ -const singleNumber = function (nums) { - const res = Array(2).fill(0) - const x = nums.reduce((acc, v) => acc ^ v, 0) - const partition = x & ~(x - 1) - for (let i = 0; i < nums.length; i++) { - if (partition & nums[i]) { - res[1] ^= nums[i] - } else { - res[0] ^= nums[i] - } - } - return res -} diff --git a/261-graph-valid-tree.js b/261-graph-valid-tree.js index 5347da57..5a549db6 100644 --- a/261-graph-valid-tree.js +++ b/261-graph-valid-tree.js @@ -18,40 +18,3 @@ const validTree = function(n, edges) { } }; -// another - -/** - * @param {number} n - * @param {number[][]} edges - * @return {boolean} - */ -const validTree = function(n, edges) { - if (edges.length !== n - 1) { - return false - } - const graph = {} - edges.forEach(edge => { - if (!graph[edge[0]]) { - graph[edge[0]] = [] - } - if (!graph[edge[1]]) { - graph[edge[1]] = [] - } - graph[edge[0]].push(edge[1]) - graph[edge[1]].push(edge[0]) - }) - const queue = [0], - visited = new Set() - while (queue.length) { - const currentNode = queue.shift() - visited.add(currentNode) - if (graph[currentNode]) { - graph[currentNode].forEach(node => { - if (!visited.has(node)) { - queue.push(node) - } - }) - } - } - return visited.size === n -} diff --git a/2653-sliding-subarray-beauty.js b/2653-sliding-subarray-beauty.js index 0450dda7..192ef80a 100644 --- a/2653-sliding-subarray-beauty.js +++ b/2653-sliding-subarray-beauty.js @@ -38,28 +38,3 @@ const getSubarrayBeauty = function(nums, k, x) { } }; -// another - -/** - * @param {number[]} nums - * @param {number} k - * @param {number} x - * @return {number[]} - */ -const getSubarrayBeauty = function(nums, k, x) { - let counter = Array(50).fill(0), ans = new Array(nums.length - k + 1).fill(0); - for (let i = 0; i < nums.length; i++) { - if (nums[i] < 0) counter[nums[i] + 50]++; - if (i - k >= 0 && nums[i - k] < 0) counter[nums[i - k] + 50]--; - if (i - k + 1 < 0) continue; - let count = 0; - for (let j = 0; j < 50; j++) { - count += counter[j]; - if (count >= x) { - ans[i - k + 1] = j - 50; - break; - } - } - } - return ans; -}; diff --git a/2663-lexicographically-smallest-beautiful-string.js b/2663-lexicographically-smallest-beautiful-string.js index 9c6d0067..bac990b3 100644 --- a/2663-lexicographically-smallest-beautiful-string.js +++ b/2663-lexicographically-smallest-beautiful-string.js @@ -39,47 +39,3 @@ const smallestBeautifulString = function(s, k) { } }; -// another - - -/** - * @param {string} s - * @param {number} k - * @return {string} - */ -const smallestBeautifulString = function (s, k) { - const chars = s.split('') - - for (let i = chars.length - 1; i >= 0; i--) { - chars[i] = String.fromCharCode(chars[i].charCodeAt(0) + 1) - while (containsPalindrome(chars, i)) { - chars[i] = String.fromCharCode(chars[i].charCodeAt(0) + 1) - } - if (chars[i] < String.fromCharCode('a'.charCodeAt(0) + k)) { - // If s[i] is among the first k letters, then change the letters after - // s[i] to the smallest ones that don't form any palindrome substring. - return changeSuffix(chars, i + 1) - } - } - - return '' - - // Returns true if chars[0..i] contains palindrome. - function containsPalindrome(chars, i) { - return ( - (i > 0 && chars[i] == chars[i - 1]) || (i > 1 && chars[i] == chars[i - 2]) - ) - } - - // Returns string where chars[i..] replaced with the smallest letters that - // don't form any palindrome substring. - function changeSuffix(chars, i) { - for (let j = i; j < chars.length; j++) { - chars[j] = 'a' - while (containsPalindrome(chars, j)) { - chars[j] = String.fromCharCode(chars[j].charCodeAt(0) + 1) - } - } - return chars.join('') - } -} diff --git a/268-missing-number.js b/268-missing-number.js index 69960ce7..c682e434 100755 --- a/268-missing-number.js +++ b/268-missing-number.js @@ -9,29 +9,3 @@ const missingNumber = function(nums) { return xor ^ n }; -// another - -/** - * @param {number[]} nums - * @return {number} - */ -const missingNumber = function(nums) { - const len = nums.length; - return (len * (len + 1)) / 2 - sum(nums); -}; - -function sum(arr) { - return arr.reduce((ac, el) => ac + el, 0); -} - -// another -/** - * @param {number[]} nums - * @return {number} - */ -var missingNumber = function(nums) { - const n = nums.length - const sum = nums.reduce((ac, e) => ac + e, 0) - const target = (n + 1) * n / 2 - return target - sum -}; diff --git a/270-closest-binary-search-tree-value.js b/270-closest-binary-search-tree-value.js index 5bde4e8b..f397ce6c 100644 --- a/270-closest-binary-search-tree-value.js +++ b/270-closest-binary-search-tree-value.js @@ -46,40 +46,3 @@ const closestValue = function(root, target) { return res }; -// another - -const closestValue = function(root, target) { - const child = target < root.val ? root.left : root.right; - if (!child) return root.val; - const closest = closestValue(child, target); - return Math.abs(closest - target) < Math.abs(root.val - target) - ? closest - : root.val; -}; - -// another - -const closestValue = function(root, target) { - if(root == null) return -1 - let node = root - const stack = [] - const res = [] - const K = 1 - while(node || stack.length) { - if(node) { - stack.push(node) - node = node.left - } else { - node = stack.pop() - if(res.length === K) { - if(Math.abs(res[0] - target) < Math.abs(node.val - target)) { - return res[0] - } - res.shift() - } - res.push(node.val) - node = node.right - } - } - return res[0] -}; diff --git a/274-h-index.js b/274-h-index.js index 80218b8a..cebd8ea7 100644 --- a/274-h-index.js +++ b/274-h-index.js @@ -16,33 +16,3 @@ const hIndex = function(citations) { return 0 }; -// another - -/** - * @param {number[]} citations - * @return {number} - */ -const hIndex = function(citations) { - citations = citations.sort((a, b) => b - a) - - for (let i = 0, len = citations.length; i < len; i++) { - if (i >= citations[i]) return i - } - - return citations.length -} - -// another - - -const hIndex = function(citations) { - const buckets = Array(citations.length + 1).fill(0) - citations.forEach(citation => { - buckets[citation >= citations.length ? citations.length : citation]++ - }) - for (let i = citations.length, count = 0; i >= 0; i--) { - count += buckets[i] - if (count >= i) return i - } - return 0 -} diff --git a/275-h-index-ii.js b/275-h-index-ii.js index 254aa6db..dc96ce57 100644 --- a/275-h-index-ii.js +++ b/275-h-index-ii.js @@ -15,25 +15,3 @@ const hIndex = function(citations) { return len - left } -// another - -/** - * @param {number[]} citations - * @return {number} - */ -const hIndex = function(citations) { - const len = citations.length - let lo = 0, - hi = len - 1 - while (lo <= hi) { - let med = lo + ((hi - lo) >> 1) - if (citations[med] === len - med) { - return len - med - } else if (citations[med] < len - med) { - lo = med + 1 - } else { - hi = med - 1 - } - } - return len - lo -} diff --git a/2762-continuous-subarrays.js b/2762-continuous-subarrays.js index d812a6f4..ad03d396 100644 --- a/2762-continuous-subarrays.js +++ b/2762-continuous-subarrays.js @@ -26,74 +26,3 @@ const continuousSubarrays = function(nums) { }; -// another - - -/** - * @param {number[]} nums - * @return {number} - */ -const continuousSubarrays = function(nums) { - let res = 0 - let l = 0 - let r = 0 - const mset = new MultiSet() - for(let r = 0; r < nums.length; r++) { - mset.add(nums[r]) - while (mset.max - mset.min > 2) { - mset.remove(nums[l]) - l++ - } - - res += r - l + 1 - } - return res -}; - -class MultiSet { - constructor() { - this.countMap = new Map() - this.valueList = [] - } - remove(value) { - if(!this.countMap.has(value)) return false - let index = binarySearch(this.valueList, value) - if (this.countMap.get(value) === 1) { - this.valueList.splice(index, 1) - this.countMap.delete(value) - } else { - this.countMap.set(value, (this.countMap.get(value) || 0) - 1) - } - return true - } - add(value) { - let index = binarySearch(this.valueList, value) - if (index < 0) { - this.valueList.splice(-index - 1, 0, value) - this.countMap.set(value, 1) - } else { - this.countMap.set(value, this.countMap.get(value) + 1) - } - } - get max() { - return this.valueList[this.valueList.length - 1] - } - get min() { - return this.valueList[0] - } -} - -function binarySearch(arr, val) { - let l = 0, r = arr.length - while( l < r ) { - const mid = Math.floor((l + r) / 2) - if(arr[mid] < val) { - l = mid + 1 - } else { - r = mid - } - } - if(arr[l] !== val) return -(l + 1) - - return l -} diff --git a/277-find-the-celebrity.js b/277-find-the-celebrity.js index c37e8bdd..f16393a6 100644 --- a/277-find-the-celebrity.js +++ b/277-find-the-celebrity.js @@ -40,40 +40,3 @@ const solution = function(knows) { } } -// another - -/** - * Definition for knows() - * - * @param {integer} person a - * @param {integer} person b - * @return {boolean} whether a knows b - * knows = function(a, b) { - * ... - * }; - */ - -/** - * @param {function} knows() - * @return {function} - */ -const solution = function(knows) { - /** - * @param {integer} n Total people - * @return {integer} The celebrity - */ - return function(n) { - if (n < 1) return -1 - let candidate = 0 - for (let i = 1; i < n; i++) { - if (knows(candidate, i)) candidate = i - } - for (let i = 0; i < n; i++) { - if (i < candidate && (knows(candidate, i) || !knows(i, candidate))) - return -1 - if (i > candidate && !knows(i, candidate)) return -1 - } - return candidate - } -} - diff --git a/279-perfect-squares.js b/279-perfect-squares.js index 6cdbbbdd..bf86c8ed 100644 --- a/279-perfect-squares.js +++ b/279-perfect-squares.js @@ -17,64 +17,3 @@ const numSquares = function(n) { return dp[n] }; -// another - -/** - * @param {number} n - * @return {number} - */ -const numSquares = function (n) { - if (n <= 0) return 0 - // cntPerfectSquares[i] = the least number of perfect square numbers - const cntPerfectSquares = [0] - // While cntPerfectSquares.length <= n, we need to incrementally - // calculate the next result until we get the result for n. - while (cntPerfectSquares.length <= n) { - const m = cntPerfectSquares.length - let cntSquares = Number.MAX_VALUE - for (let i = 1; i * i <= m; i++) { - cntSquares = Math.min(cntSquares, cntPerfectSquares[m - i * i] + 1) - } - cntPerfectSquares.push(cntSquares) - } - return cntPerfectSquares[n] -} - -// another - -/** - * @param {number} n - * @return {number} - */ -const numSquares = function (n) { - // Based on Lagrange's Four Square theorem, there - // are only 4 possible results: 1, 2, 3, 4. - // If n is a perfect square, return 1. - if (is_square(n)) { - return 1 - } - // The result is 4 if and only if n can be written in the - // form of 4^k*(8*m + 7). Please refer to - // Legendre's three-square theorem. - while ((n & 3) === 0) { - // n%4 == 0 - n >>= 2 - } - if ((n & 7) === 7) { - // n%8 == 7 - return 4 - } - // Check whether 2 is the result. - let sqrt_n = Math.sqrt(n) >> 0 - for (let i = 1; i <= sqrt_n; i++) { - if (is_square(n - i * i)) { - return 2 - } - } - return 3 - function is_square(n) { - const sqrt_n = Math.sqrt(n) >> 0 - return sqrt_n * sqrt_n == n - } -} - diff --git a/28-implement-strStr().js b/28-implement-strStr().js index 98513914..f4faa4bf 100755 --- a/28-implement-strStr().js +++ b/28-implement-strStr().js @@ -28,24 +28,3 @@ const strStr = function(a, b) { return -1 }; -// another - -/** - * @param {string} haystack - * @param {string} needle - * @return {number} - */ -const strStr = function(haystack, needle) { - if (needle === "") return 0; - for (let i = 0; ; i++) { - for (let j = 0; ; j++) { - if (j === needle.length) { - return i; - } - if (i + j === haystack.length) return -1; - if (haystack.charAt(i + j) !== needle.charAt(j)) { - break; - } - } - } -}; diff --git a/281-zigzag-iterator.js b/281-zigzag-iterator.js index 7d8ec1ce..adce83e0 100644 --- a/281-zigzag-iterator.js +++ b/281-zigzag-iterator.js @@ -41,102 +41,3 @@ ZigzagIterator.prototype.next = function next() { * while (i.hasNext()) a.push(i.next()); */ -// another - -/** - * @constructor - * @param {Integer[]} v1 - * @param {Integer[]} v1 - */ -const ZigzagIterator = function ZigzagIterator(v1, v2) { - const A = [v1, v2] - this.A = A - this.n = A.length - this.m = Math.max(v1.length, v2.length) - this.j = 0 - this.i = 0 -} - -/** - * @this ZigzagIterator - * @returns {boolean} - */ -ZigzagIterator.prototype.hasNext = function hasNext() { - return this.j < this.m -} - -/** - * @this ZigzagIterator - * @returns {integer} - */ -ZigzagIterator.prototype.incrementPointers = function incrementPointers() { - this.i += 1 - if (this.i === this.n) { - this.j += 1 - this.i = 0 - } -} - -ZigzagIterator.prototype.next = function next() { - let next = undefined - while (next === undefined) { - next = this.A[this.i][this.j] - this.incrementPointers() - } - while (this.hasNext()) { - if (this.A[this.i][this.j] !== undefined) break - this.incrementPointers() - } - return next -} - -/** - * Your ZigzagIterator will be called like this: - * var i = new ZigzagIterator(v1, v2), a = []; - * while (i.hasNext()) a.push(i.next()); - */ - -// another - -/** - * @constructor - * @param {Integer[]} v1 - * @param {Integer[]} v1 - */ -const ZigzagIterator = function ZigzagIterator(v1, v2) { - this.queue = [] - if (v1.length > 0) { - const it = v1[Symbol.iterator]() - const res = it.next() - this.queue.push({ it, res }) - } - if (v2.length > 0) { - const it = v2[Symbol.iterator]() - const res = it.next() - this.queue.push({ it, res }) - } -} -/** - * @this ZigzagIterator - * @returns {boolean} - */ -ZigzagIterator.prototype.hasNext = function hasNext() { - return this.queue.length > 0 -} -/** - * @this ZigzagIterator - * @returns {integer} - */ -ZigzagIterator.prototype.next = function next() { - const { it, res } = this.queue.shift() - const { value } = res - const res1 = it.next() - if (!res1.done) this.queue.push({ it, res: res1 }) - return value -} -/** - * Your ZigzagIterator will be called like this: - * var i = new ZigzagIterator(v1, v2), a = []; - * while (i.hasNext()) a.push(i.next()); - */ - diff --git a/285-inorder-successor-in-bst.js b/285-inorder-successor-in-bst.js index 71cf8ef9..ff6158bc 100644 --- a/285-inorder-successor-in-bst.js +++ b/285-inorder-successor-in-bst.js @@ -24,40 +24,3 @@ function dfs(node, arr, res, target) { dfs(node.right, arr, res, target) } -// another - -const inorderSuccessor = function(root, p) { - let last = null - const chk = node => { - if(!node) return - const l = chk(node.left) - if(l !== undefined) return l - if(last === p) return node - last = node - return chk(node.right) - } - return chk(root) -}; - -// another - -const inorderSuccessor = function(root, p) { - while (root != null && root.val <= p.val) root = root.right - const left = root == null ? null : inorderSuccessor(root.left, p) - return left != null && left.val > p.val ? left : root -} - -// another - -const inorderSuccessor = function(root, p) { - let succ = null - while(root) { - if (p.val < root.val) { - succ = root - root = root.left - } else { - root = root.right - } - } - return succ -} diff --git a/286-walls-and-gates.js b/286-walls-and-gates.js index f0a47019..0b7f37ac 100644 --- a/286-walls-and-gates.js +++ b/286-walls-and-gates.js @@ -55,44 +55,3 @@ const wallsAndGates = function(rooms) { } }; -// another - -/** - * @param {number[][]} rooms - * @return {void} Do not return anything, modify rooms in-place instead. - */ -const wallsAndGates = function(rooms) { - const dirs = [ - [1, 0], - [-1, 0], - [0, 1], - [0, -1] - ]; - const rows = rooms.length; - const cols = rows === 0 ? 0 : rooms[0].length; - const q = []; - const INF = 2147483647; - for (let i = 0; i < rows; i++) { - for (let j = 0; j < cols; j++) { - if (rooms[i][j] === 0) dfs(dirs, rooms, i, j, rows, cols, 0); - } - } -}; - -function dfs(dirs, rooms, i, j, rows, cols, dis) { - if ( - i < 0 || - j < 0 || - i >= rows || - j >= cols || - rooms[i][j] === -1 || - rooms[i][j] < dis - ) { - return; - } - rooms[i][j] = dis; - for (let dir of dirs) { - dfs(dirs, rooms, i + dir[0], j + dir[1], rows, cols, dis + 1); - } -} - diff --git a/287-find-the-duplicate-number.js b/287-find-the-duplicate-number.js index d0a9bb2d..caf038ec 100755 --- a/287-find-the-duplicate-number.js +++ b/287-find-the-duplicate-number.js @@ -26,258 +26,3 @@ const findDuplicate = function(nums) { return ans; }; -// another - -/** - * @param {number[]} nums - * @return {number} - */ -const findDuplicate = function(nums) { - const len = nums.length - if(len > 0) { - let slow = nums[0] - let fast = nums[nums[0]] - while(slow !== fast) { - slow = nums[slow] - fast = nums[nums[fast]] - } - slow = 0 - while(slow !== fast) { - slow = nums[slow] - fast = nums[fast] - } - return slow - } - return -1; -}; - -// another - -/** - * @param {number[]} nums - * @return {number} - */ -const findDuplicate = function(nums) { - let n = nums.length - 1, - res = 0 - for (let p = 0; p < 32; ++p) { - let bit = 1 << p, - a = 0, - b = 0 - for (let i = 0; i <= n; ++i) { - if (i > 0 && (i & bit) > 0) ++a - if ((nums[i] & bit) > 0) ++b - } - if (b > a) res += bit - } - return res -} - -// another - -/** - * @param {number[]} nums - * @return {number} - */ -const findDuplicate = function(nums) { - const hash = {}; - for (let i = 0; i < nums.length; i++) { - if (hash.hasOwnProperty(nums[i])) { - return nums[i]; - } else { - hash[nums[i]] = 1; - } - } -}; - - -/** - -# File: FindDuplicate.py -# Author: Keith Schwarz (htiek@cs.stanford.edu) -# -# An algorithm for solving the following (classic) hard interview problem: -# -# "You are given an array of integers of length n, where each element ranges -# from 0 to n - 2, inclusive. Prove that at least one duplicate element must -# exist, and give an O(n)-time, O(1)-space algorithm for finding some -# duplicated element. You must not modify the array elements during this -# process." -# -# This problem (reportedly) took CS legend Don Knuth twenty-four hours to solve -# and I have only met one person (Keith Amling) who could solve it in less time -# than this. -# -# The first part of this problem - proving that at least one duplicate element -# must exist - is a straightforward application of the pigeonhole principle. -# If the values range from 0 to n - 2, inclusive, then there are only n - 1 -# different values. If we have an array of n elements, one must necessarily be -# duplicated. -# -# The second part of this problem - finding the duplicated element subject to -# the given constraints - is much harder. To solve this, we're going to need a -# series of nonobvious insights that transform the problem into an instance of -# something entirely different. -# -# The main trick we need to use to solve this problem is to notice that because -# we have an array of n elements ranging from 0 to n - 2, we can think of the -# array as defining a function f from the set {0, 1, ..., n - 1} onto itself. -# This function is defined by f(i) = A[i]. Given this setup, a duplicated -# value corresponds to a pair of indices i != j such that f(i) = f(j). Our -# challenge, therefore, is to find this pair (i, j). Once we have it, we can -# easily find the duplicated value by just picking f(i) = A[i]. -# -# But how are we to find this repeated value? It turns out that this is a -# well-studied problem in computer science called cycle detection. The general -# form of the problem is as follows. We are given a function f. Define the -# sequence x_i as -# -# x_0 = k (for some k) -# x_1 = f(x_0) -# x_2 = f(f(x_0)) -# ... -# x_{n+1} = f(x_n) -# -# Assuming that f maps from a domain into itself, this function will have one -# of three forms. First, if the domain is infinite, then the sequence could be -# infinitely long and nonrepeating. For example, the function f(n) = n + 1 on -# the integers has this property - no number is ever duplicated. Second, the -# sequence could be a closed loop, which means that there is some i so that -# x_0 = x_i. In this case, the sequence cycles through some fixed set of -# values indefinitely. Finally, the sequence could be "rho-shaped." In this -# case, the sequence looks something like this: -# -# x_0 -> x_1 -> ... x_k -> x_{k+1} ... -> x_{k+j} -# ^ | -# | | -# +-----------------------+ -# -# That is, the sequence begins with a chain of elements that enters a cycle, -# then cycles around indefinitely. We'll denote the first element of the cycle -# that is reached in the sequence the "entry" of the cycle. -# -# For our particular problem of finding a duplicated element in the array, -# consider the sequence formed by starting at position n - 1 and then -# repeatedly applying f. That is, we start at the last position in the array, -# then go to the indicated index, repeating this process. My claim is that -# this sequence is rho-shaped. To see this, note that it must contains a cycle -# because the array is finite and after visiting n elements, we necessarily -# must visit some element twice. This is true no matter where we start off in -# the array. Moreover, note that since the array elements range from 0 to -# n - 2 inclusive, there is no array index that contains n - 1 as a value. -# Consequently, when we leave index n - 1 after applying the function f one -# time, we can never get back there. This means that n - 1 can't be part of a -# cycle, but if we follow indices starting there we must eventually hit some -# other node twice. The concatenation of the chain starting at n - 1 with the -# cycle it hits must be rho-shaped. -# -# Moreover, think about the node we encounter that starts at the entry of the -# cycle. Since this node is at the entry of the cycle, there must be two -# inputs to the function f that both result in that index being generated. For -# this to be possible, it must be that there are indices i != j with -# f(i) = f(j), meaning that A[i] = A[j]. Thus the index of the entry of the -# cycle must be one of the values that is duplicated in the array. -# -# There is a famous algorithm due to Robert Floyd that, given a rho-shaped -# sequence, finds the entry point of the cycle in linear time and using only -# constant space. This algorithm is often referred to as the "tortoise and -# hare" algorithm, for reasons that will become clearer shortly. -# -# The idea behind the algorithm is to define two quantities. First, let c be -# the length of the chain that enters the cycle, and let l be the length of the -# cycle. Next, let l' be the smallest multiple of l that's larger than c. -# I claim that for any rho-shaped sequence l' defined above, that -# -# x_{l'} = x_{2l'} -# -# The proof is actually straightforward and very illustrative - it's one of my -# favorite proofs in computer science. The idea is that since l' is at least -# c, it must be contained in the cycle. Moreover, since l' is a multiple of -# the length of the loop, we can write it as ml for some constant m. If we -# start at position x_{l'}, which is inside the loop, then take l' more steps -# forward to get to x_{2l'}, then we will just walk around the loop m times, -# ending up right back where we started. -# -# One key trick of Floyd's algorithm is that even if we don't explicitly know l -# or c, we can still find the value l' in O(l') time. The idea is as follows. -# We begin by keeping track of two values "slow" and "fast," both starting at -# x_0. We then iteratively compute -# -# slow = f(slow) -# fast = f(f(fast)) -# -# We repeat this process until we find that slow and fast are equal to one -# another. When this happens, we know that slow = x_j for some j, and -# fast = x_{2j} for that same j. Since x_j = x_{2j}, we know that j must be at -# least c, since it has to be contained in the cycle. Moreover, we know that j -# must be a multiple of l, since the fact that x_j = x_{2j} means that taking j -# steps while in the cycle ends up producing the same result. Finally, j must -# be the smallest multiple of l greater than c, since if there were a smaller -# multiple of l greater than c then we would have reached that multiple before -# we reached j. Consequently, we must have that j = l', meaning that we can -# find l' without knowing anything about the length or shape of the cycle! -# -# To complete the construction, we need to show how to use our information -# about l' to find the entry to the cycle (which is at position x_c). To do -# this, we start off one final variable, which we call "finder," at x_0. We -# then iteratively repeat the following: -# -# finder = f(finder) -# slow = f(slow) -# -# until finder = slow. We claim that (1) the two will eventually hit each -# other, and (2) they will hit each other at the entry to the cycle. To see -# this, we remark that since slow is at position x_{l'}, if we take c steps -# forward, then we have that slow will be at position x_{l' + c}. Since l' is -# a multiple of the loop length, this is equivalent to taking c steps forward, -# then walking around the loop some number of times back to where you started. -# In other words, x_{l' + c} = x_c. Moreover, consider the position of the -# finder variable after c steps. It starts at x_0, so after c steps it will be -# at position x_c. This proves both (1) and (2), since we've shown that the -# two must eventually hit each other, and when they do they hit at position x_c -# at the entry to the cycle. -# -# The beauty of this algorithm is that it uses only O(1) external memory to -# keep track of two different pointers - the slow pointer, and then the fast -# pointer (for the first half) and the finder pointer (for the second half). -# But on top of that, it runs in O(n) time. To see this, note that the time -# required for the slow pointer to hit the fast pointer is O(l'). Since l' is -# the smallest multiple of l greater than c, we have two cases to consider. -# First, if l > c, then this is l. Otherwise, if l < c, then we have that -# there must be some multiple of l between c and 2c. To see this, note that -# in the range c and 2c there are c different values, and since l < c at least -# one of them must be equal to 0 mod l. Finally, the time required to find the -# start of the cycle from this point is O(c). This gives a total runtime of at -# most O(c + max{l, 2c}). All of these values are at most n, so this algorithm -# runs in time O(n). - -def findArrayDuplicate(array): - assert len(array) > 0 - - # The "tortoise and hare" step. We start at the end of the array and try - # to find an intersection point in the cycle. - slow = len(array) - 1 - fast = len(array) - 1 - - # Keep advancing 'slow' by one step and 'fast' by two steps until they - # meet inside the loop. - while True: - slow = array[slow] - fast = array[array[fast]] - - if slow == fast: - break - - # Start up another pointer from the end of the array and march it forward - # until it hits the pointer inside the array. - finder = len(array) - 1 - while True: - slow = array[slow] - finder = array[finder] - - # If the two hit, the intersection index is the duplicate element. - if slow == finder: - return slow - - -*/ diff --git a/291-word-pattern-ii.js b/291-word-pattern-ii.js index 91f77ac2..02439de9 100644 --- a/291-word-pattern-ii.js +++ b/291-word-pattern-ii.js @@ -29,40 +29,3 @@ function isMatch(str, i, pat, j, map, set) { return false } -// another - -/** - * @param {string} pattern - * @param {string} s - * @return {boolean} - */ -const wordPatternMatch = function(pattern, s) { - const obj = { res: false } - const hash = {} - helper(pattern, s, 0, 0, hash, obj) - return obj.res -}; - -function helper(p, s, i, j, hash, obj) { - if(obj.res) return - if(i === p.length && j === s.length) { - obj.res = true - return - } - if(i >= p.length || j >= s.length) return - for(let m = j; m < s.length && obj.res === false; m++) { - const tmp = s.slice(j, m + 1) - if(hash[p[i]]) { - if(tmp === hash[p[i]]) { - helper(p, s, i + 1, m + 1, hash, obj) - } - } else { - const set = new Set(Object.values(hash)) - if (!set.has(tmp)) { - hash[p[i]] = tmp - helper(p, s, i + 1, m + 1, hash, obj) - delete hash[p[i]] - } - } - } -} diff --git a/295-find-median-from-data-stream.js b/295-find-median-from-data-stream.js index 99487161..7d705fef 100644 --- a/295-find-median-from-data-stream.js +++ b/295-find-median-from-data-stream.js @@ -39,106 +39,3 @@ MedianFinder.prototype.findMedian = function() { * var param_2 = obj.findMedian() */ -// another - -/** - * initialize your data structure here. - */ -const MedianFinder = function() { - this.minPQ = new PriorityQueue() - this.maxPQ = new PriorityQueue((a, b) => a < b) -}; - -/** - * @param {number} num - * @return {void} - */ -MedianFinder.prototype.addNum = function(num) { - this.minPQ.push(num) - this.maxPQ.push(this.minPQ.pop()) - if(this.minPQ.size() < this.maxPQ.size()) { - this.minPQ.push(this.maxPQ.pop()) - } -}; - -/** - * @return {number} - */ -MedianFinder.prototype.findMedian = function() { - if(this.minPQ.size() > this.maxPQ.size()) return this.minPQ.peek() - else return (this.minPQ.peek() + this.maxPQ.peek()) / 2 -}; - -/** - * Your MedianFinder object will be instantiated and called as such: - * var obj = new MedianFinder() - * obj.addNum(num) - * var param_2 = obj.findMedian() - */ -class PriorityQueue { - constructor(comparator = (a, b) => a > b) { - this.heap = [] - this.top = 0 - this.comparator = comparator - } - size() { - return this.heap.length - } - isEmpty() { - return this.size() === 0 - } - peek() { - return this.heap[this.top] - } - push(...values) { - values.forEach((value) => { - this.heap.push(value) - this.siftUp() - }) - return this.size() - } - pop() { - const poppedValue = this.peek() - const bottom = this.size() - 1 - if (bottom > this.top) { - this.swap(this.top, bottom) - } - this.heap.pop() - this.siftDown() - return poppedValue - } - replace(value) { - const replacedValue = this.peek() - this.heap[this.top] = value - this.siftDown() - return replacedValue - } - - parent = (i) => ((i + 1) >>> 1) - 1 - left = (i) => (i << 1) + 1 - right = (i) => (i + 1) << 1 - greater = (i, j) => this.comparator(this.heap[i], this.heap[j]) - swap = (i, j) => ([this.heap[i], this.heap[j]] = [this.heap[j], this.heap[i]]) - siftUp = () => { - let node = this.size() - 1 - while (node > this.top && this.greater(node, this.parent(node))) { - this.swap(node, this.parent(node)) - node = this.parent(node) - } - } - siftDown = () => { - let node = this.top - while ( - (this.left(node) < this.size() && this.greater(this.left(node), node)) || - (this.right(node) < this.size() && this.greater(this.right(node), node)) - ) { - let maxChild = - this.right(node) < this.size() && - this.greater(this.right(node), this.left(node)) - ? this.right(node) - : this.left(node) - this.swap(node, maxChild) - node = maxChild - } - } -} diff --git a/296-best-meeting-point.js b/296-best-meeting-point.js index deceaa09..e0b6c6c3 100644 --- a/296-best-meeting-point.js +++ b/296-best-meeting-point.js @@ -54,44 +54,3 @@ function min(arr) { return sum } -// another - -/** - * @param {number[][]} grid - * @return {number} - */ -const minTotalDistance = function (grid) { - const homeArr = [] - const horiArr = [], vertArr = [] - const m = grid.length, - n = grid[0].length - for (let i = 0; i < m; i++) { - for (let j = 0; j < n; j++) { - if (grid[i][j] === 1) { - homeArr.push([i, j]) - vertArr.push(i) - horiArr.push(j) - } - } - } - - vertArr.sort((a, b) => a - b) - horiArr.sort((a, b) => a - b) - - let y = vertArr[~~(vertArr.length/2)] - let x = horiArr[~~(horiArr.length/2)] - - const center = [y, x] - - let res = 0 - for(const point of homeArr) { - res += dis(center, point) - } - - return res - - function dis(a, b) { - return Math.abs(a[0] - b[0]) + Math.abs(a[1] - b[1]) - } - -} diff --git a/299-bulls-and-cows.js b/299-bulls-and-cows.js index dd67443e..b16e11f4 100644 --- a/299-bulls-and-cows.js +++ b/299-bulls-and-cows.js @@ -21,34 +21,3 @@ const getHint = function(secret, guess) { return `${x}A${y}B` }; -// another - -/** - * @param {string} secret - * @param {string} guess - * @return {string} - */ -const getHint = function(secret, guess) { - let bulls = 0 - let cows = 0 - const h = {} - for(let i = 0, len = secret.length; i < len; i++) { - if(secret[i] === guess[i]) { - bulls++ - } else { - if(!h.hasOwnProperty(secret[i])) h[secret[i]] = 0 - h[secret[i]]++ - } - } - - for(let i = 0, len = secret.length; i < len; i++) { - if(secret[i] !== guess[i]) { - if(h.hasOwnProperty(guess[i]) && h[guess[i]] > 0) { - cows++ - h[guess[i]]-- - } - } - } - - return `${bulls}A${cows}B` -}; diff --git a/3-longest-substring-without-repeating-characters.js b/3-longest-substring-without-repeating-characters.js index 78f0636b..00030787 100755 --- a/3-longest-substring-without-repeating-characters.js +++ b/3-longest-substring-without-repeating-characters.js @@ -17,30 +17,3 @@ const lengthOfLongestSubstring = function(s) { return max }; -// another - -/** - * @param {string} s - * @return {number} - */ -const lengthOfLongestSubstring = function(s) { - // var p=0, q=0; //p: start of the sub, q: end of the queue - - //hashmap in js????? Array.indexOf - const sub = []; - let max = 0; - - for (let i = 0; i < s.length; i++) { - let index = sub.indexOf(s.charAt(i)); - if (index == -1) { - sub.push(s.charAt(i)); - // q++; - } else { - //find repeat, get index of repeat el, remve all el before that index - sub = sub.slice(index + 1, sub.length); - sub.push(s.charAt(i)); - } - max = Math.max(max, sub.length); - } - return max; -}; diff --git a/30-substring-with-concatenation-of-all-words.js b/30-substring-with-concatenation-of-all-words.js index 09e30dcc..61f5bfd1 100644 --- a/30-substring-with-concatenation-of-all-words.js +++ b/30-substring-with-concatenation-of-all-words.js @@ -34,64 +34,3 @@ function chk(hash, str, wl, num) { return true } -// another - -/** - * @param {string} s - * @param {string[]} words - * @return {number[]} - */ -const findSubstring = function(s, words) { - if (s === "" || words.length === 0) return [] - const wordMap = new Map() - words.forEach(item => { - if (wordMap.has(item)) { - wordMap.set(item, wordMap.get(item) + 1) - } else { - wordMap.set(item, 1) - } - }) - const w = words[0].length - const wlen = words.length - const ans = [] - const n = s.length - for (let i = 0; i < w; i++) { - let left = i - let count = 0 - let sMap = new Map() - for (let j = i; j <= n - w; j += w) { - let sub = s.substring(j, j + w) - if (wordMap.has(sub)) { - if (sMap.has(sub)) { - sMap.set(sub, sMap.get(sub) + 1) - } else { - sMap.set(sub, 1) - } - if (sMap.get(sub) <= wordMap.get(sub)) { - count++ - } else { - while (sMap.get(sub) > wordMap.get(sub)) { - let next = s.substring(left, left + w) - sMap.set(next, sMap.get(next) - 1) - if (sMap.get(next) < wordMap.get(next)) { - count-- - } - left += w - } - } - if (count === wlen) { - ans.push(left) - let first = s.substring(left, left + w) - sMap.set(first, sMap.get(first) - 1) - left += w - count-- - } - } else { - sMap.clear() - count = 0 - left = j + w - } - } - } - return ans -} diff --git a/300-longest-increasing-subsequence.js b/300-longest-increasing-subsequence.js index 3d79c6ec..7b13a04e 100644 --- a/300-longest-increasing-subsequence.js +++ b/300-longest-increasing-subsequence.js @@ -20,82 +20,3 @@ const lengthOfLIS = function(nums) { return stack.length }; -// another - -/** - * @param {number[]} nums - * @return {number} - */ -const lengthOfLIS = function(nums) { - if (nums.length === 0) { - return 0 - } - const dp = new Array(nums.length).fill(0) - dp[0] = 1 - let maxans = 1 - for(let i = 1; i < dp.length; i++) { - let maxval = 0 - for(let j = 0; j < i; j++) { - if(nums[i] > nums[j]) { - maxval = Math.max(maxval, dp[j]) - } - } - dp[i] = maxval + 1 - maxans = Math.max(maxans, dp[i]) - } - return maxans -}; - -// another - -/** - * @param {number[]} nums - * @return {number} - */ -const lengthOfLIS = function(nums) { - const n = nums.length - const tails = [] - let res = 0 - for(let e of nums) { - let i = 0, j = res - while(i !== j) { - const mid = i + ((j - i) >> 1) - if(tails[mid] < e) i = mid + 1 - else j = mid - } - tails[i] = e - if(i === res) res++ - } - return res -}; - -// another - -/** - * @param {number[]} nums - * @return {number} - */ -const lengthOfLIS = function(nums) { - const n = nums.length, stack = [] - let res = 0 - stack.push(nums[0]) - for(let i = 1; i < n; i++) { - const cur = nums[i] - if(cur > stack[stack.length - 1]) { - stack.push(cur) - } else { - let l = 0, r = stack.length - 1 - while(l < r) { - let mid = ~~((l + r) / 2) - if(stack[mid] < cur) { - l = mid + 1 - } else { - r = mid - } - } - stack[l] = cur - } - } - - return stack.length -}; diff --git a/301-remove-invalid-parentheses.js b/301-remove-invalid-parentheses.js index 9cb8bdde..a68929bf 100644 --- a/301-remove-invalid-parentheses.js +++ b/301-remove-invalid-parentheses.js @@ -30,40 +30,3 @@ const removeInvalidParentheses = function(s) { } }; -// another - -/** - * @param {string} s - * @return {string[]} - */ -const removeInvalidParentheses = function(s) { - const ans = []; - remove(s, ans, 0, 0, ["(", ")"]); - return ans; -}; - -function remove(s, ans, last_i, last_j, par) { - for (let stack = 0, i = last_i; i < s.length; i++) { - if (s.charAt(i) === par[0]) stack++; - if (s.charAt(i) === par[1]) stack--; - if (stack >= 0) continue; - for (let j = last_j; j <= i; j++) { - if ( - s.charAt(j) === par[1] && - (j === last_j || s.charAt(j - 1) != par[1]) - ) { - remove(s.slice(0, j) + s.slice(j + 1), ans, i, j, par); - } - } - return; - } - const reversed = s - .split("") - .reverse() - .join(""); - if (par[0] === "(") { - remove(reversed, ans, 0, 0, [")", "("]); - } else { - ans.push(reversed); - } -} diff --git a/302-smallest-rectangle-enclosing-black-pixels.js b/302-smallest-rectangle-enclosing-black-pixels.js index 7c085de5..70c721a5 100644 --- a/302-smallest-rectangle-enclosing-black-pixels.js +++ b/302-smallest-rectangle-enclosing-black-pixels.js @@ -34,41 +34,3 @@ const minArea = function(image, x, y) { } }; -// another - -/** - * @param {character[][]} image - * @param {number} x - * @param {number} y - * @return {number} - */ -const minArea = function(image, x, y) { - let top = x, - bottom = x - let left = y, - right = y - function dfs(x, y) { - if ( - x < 0 || - y < 0 || - x >= image.length || - y >= image[0].length || - image[x][y] === '0' - ) - return - image[x][y] = '0' - top = Math.min(top, x) - bottom = Math.max(bottom, x) - left = Math.min(left, y) - right = Math.max(right, y) - dfs(x + 1, y) - dfs(x - 1, y) - dfs(x, y - 1) - dfs(x, y + 1) - } - - if (image.length === 0 || image[0].length === 0) return 0 - dfs(x, y) - return (right - left + 1) * (bottom - top + 1) -} - diff --git a/304-range-sum-query-2d-immutable.js b/304-range-sum-query-2d-immutable.js index 8333a777..f4965686 100644 --- a/304-range-sum-query-2d-immutable.js +++ b/304-range-sum-query-2d-immutable.js @@ -41,36 +41,3 @@ NumMatrix.prototype.sumRegion = function(row1, col1, row2, col2) { * var param_1 = obj.sumRegion(row1,col1,row2,col2) */ -// another - -/** - * @param {number[][]} matrix - */ -const NumMatrix = function(matrix) { - const m = matrix.length, n = matrix[0].length - const dp = Array.from({ length: m + 1 }, () => Array(n + 1).fill(0)) - for(let i = 1; i <= m; i++) { - for(let j = 1; j <= n; j++) { - dp[i][j] = dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1] + matrix[i - 1][j - 1] - } - } - this.dp = dp -}; - -/** - * @param {number} row1 - * @param {number} col1 - * @param {number} row2 - * @param {number} col2 - * @return {number} - */ -NumMatrix.prototype.sumRegion = function(row1, col1, row2, col2) { - const dp = this.dp - return dp[row2 + 1][col2 + 1] - dp[row2 + 1][col1] - dp[row1][col2 + 1] + dp[row1][col1] -}; - -/** - * Your NumMatrix object will be instantiated and called as such: - * var obj = new NumMatrix(matrix) - * var param_1 = obj.sumRegion(row1,col1,row2,col2) - */ diff --git a/307-range-sum-query-mutable.js b/307-range-sum-query-mutable.js index faff27c5..159c6869 100644 --- a/307-range-sum-query-mutable.js +++ b/307-range-sum-query-mutable.js @@ -61,40 +61,3 @@ NumArray.prototype.sumRange = function(left, right) { * var param_2 = obj.sumRange(left,right) */ -// another - -/** - * @param {number[]} nums - */ -const NumArray = function(nums) { - this.arr = nums -}; - -/** - * @param {number} i - * @param {number} val - * @return {void} - */ -NumArray.prototype.update = function(i, val) { - this.arr[i] = val -}; - -/** - * @param {number} i - * @param {number} j - * @return {number} - */ -NumArray.prototype.sumRange = function(i, j) { - let sum = 0; - for (let k = i; k <= j; k++) { - sum += this.arr[k]; - } - return sum; -}; - -/** - * Your NumArray object will be instantiated and called as such: - * var obj = Object.create(NumArray).createNew(nums) - * obj.update(i,val) - * var param_2 = obj.sumRange(i,j) - */ diff --git a/31-next-permutation.js b/31-next-permutation.js index 28de12d6..0ff8148b 100644 --- a/31-next-permutation.js +++ b/31-next-permutation.js @@ -38,79 +38,3 @@ const nextPermutation = function(nums) { } }; -// another - - -/** - * @param {number[]} nums - * @return {void} Do not return anything, modify nums in-place instead. - */ -const nextPermutation = function(nums) { - let i = nums.length - 2; - while (i >= 0 && nums[i + 1] <= nums[i]) { - i--; - } - if (i >= 0) { - let j = nums.length - 1; - while (j >= 0 && nums[j] <= nums[i]) { - j--; - } - swap(nums, i, j); - } - reverse(nums, i + 1); - -}; - -function reverse(nums, start) { - let i = start, j = nums.length - 1; - while (i < j) { - swap(nums, i, j); - i++; - j--; - } -} - -function swap(arr, i, j) { - arr[i] ^= arr[j]; - arr[j] ^= arr[i]; - arr[i] ^= arr[j]; -} - -// another - -/** - * @param {number[]} nums - * @return {void} Do not return anything, modify nums in-place instead. - */ -const nextPermutation = function(nums) { - const n = nums.length - let start, end - for(let i = n - 2; i >= 0; i--) { - if(nums[i] < nums[i + 1]) { - start = i - break - } - } - if(start == null) { - reverse(nums, 0, n - 1) - } else { - for(let i = n - 1; i >= 0; i--) { - if(nums[i] > nums[start]) { - end = i - break - } - } - swap(nums, start, end) - reverse(nums, start + 1, n - 1) - } -}; -function reverse(arr, start, end) { - while(start < end) { - swap(arr, start++, end--) - } -} -function swap(arr, i, j) { - const tmp = arr[i] - arr[i] = arr[j] - arr[j] = tmp -} diff --git a/310-minimum-height-trees.js b/310-minimum-height-trees.js index eeaa1111..2a66528b 100644 --- a/310-minimum-height-trees.js +++ b/310-minimum-height-trees.js @@ -35,81 +35,3 @@ const findMinHeightTrees = function (n, edges) { return q } -// another - -/** - * @param {number} n - * @param {number[][]} edges - * @return {number[]} - */ - const findMinHeightTrees = function(n, edges) { - if(n === 1) return [0] - const res = [], graph = {} - for(const [u, v] of edges) { - if(graph[u] == null) graph[u] = new Set() - if(graph[v] == null) graph[v] = new Set() - graph[u].add(v) - graph[v].add(u) - } - - let leaves = [] - Object.keys(graph).forEach(k => { - if(graph[k].size === 1) leaves.push(+k) - }) - while(n > 2) { - const newLeaves = [] - const size = leaves.length - for (let i = 0; i < size; i++) { - const cur = leaves.pop() - for (const next of graph[cur]) { - graph[next].delete(cur) - if(graph[next].size === 1) newLeaves.push(next) - } - } - n -= size - leaves = newLeaves - } - - return leaves -}; - -// another - -/** - * @param {number} n - * @param {number[][]} edges - * @return {number[]} - */ -const findMinHeightTrees = function(n, edges) { - if (n === 1) { - return [0]; - } - const adj = []; - for (let i = 0; i < n; i++) { - adj.push([]); - } - for (let edge of edges) { - adj[edge[0]].push(edge[1]); - adj[edge[1]].push(edge[0]); - } - let leaves = []; - for (let i = 0; i < n; i++) { - if (adj[i].length === 1) { - leaves.push(i); - } - } - - while (n > 2) { - n -= leaves.length; - let newLeaves = []; - for (let i of leaves) { - let j = adj[i].shift(); - let idx = adj[j].indexOf(i); - adj[j].splice(idx, 1); - if (adj[j].length === 1) newLeaves.push(j); - } - leaves = newLeaves; - } - - return leaves; -}; diff --git a/312-burst-balloons.js b/312-burst-balloons.js index a6cb9fa8..c75cc38d 100644 --- a/312-burst-balloons.js +++ b/312-burst-balloons.js @@ -21,33 +21,3 @@ function maxCoins(arr) { return dp[0][n - 1]; } -// another - -/** - * @param {number[]} nums - * @return {number} - */ -const maxCoins = iNums => { - const nums = new Array(iNums.length + 2); - let n = 1; - for (let x of iNums) if (x > 0) nums[n++] = x; - nums[0] = nums[n++] = 1; - - const memo = Array.from({ length: n }, () => new Array(n)); - return burst(memo, nums, 0, n - 1); -}; - -function burst(memo, nums, left, right) { - if (left + 1 === right) return 0; - if (memo[left][right] > 0) return memo[left][right]; - let ans = 0; - for (let i = left + 1; i < right; ++i) - ans = Math.max( - ans, - nums[left] * nums[i] * nums[right] + - burst(memo, nums, left, i) + - burst(memo, nums, i, right) - ); - memo[left][right] = ans; - return ans; -} diff --git a/313-super-ugly-number.js b/313-super-ugly-number.js index 598944b8..d5746c03 100644 --- a/313-super-ugly-number.js +++ b/313-super-ugly-number.js @@ -21,94 +21,3 @@ const nthSuperUglyNumber = function(n, primes) { return arr[n - 1] } -// another - -/** - * @param {number} n - * @param {number[]} primes - * @return {number} - */ -const nthSuperUglyNumber = function(n, primes) { - const ugly = Array(n).fill(0) - const pq = new PriorityQueue((a, b) => a[0] < b[0]) - - for(let i = 0; i < primes.length; i++) pq.push([primes[i], 1, primes[i]]) - ugly[0] = 1 - for(let i = 1; i < n; i++) { - ugly[i] = pq.peek()[0] - while(pq.peek()[0] === ugly[i]) { - const next = pq.pop() - pq.push([next[2] * ugly[next[1]], next[1] + 1, next[2]]) - } - } - - return ugly[n - 1] -}; - -class PriorityQueue { - constructor(comparator = (a, b) => a > b) { - this.heap = [] - this.top = 0 - this.comparator = comparator - } - size() { - return this.heap.length - } - isEmpty() { - return this.size() === 0 - } - peek() { - return this.heap[this.top] - } - push(...values) { - values.forEach((value) => { - this.heap.push(value) - this.siftUp() - }) - return this.size() - } - pop() { - const poppedValue = this.peek() - const bottom = this.size() - 1 - if (bottom > this.top) { - this.swap(this.top, bottom) - } - this.heap.pop() - this.siftDown() - return poppedValue - } - replace(value) { - const replacedValue = this.peek() - this.heap[this.top] = value - this.siftDown() - return replacedValue - } - - parent = (i) => ((i + 1) >>> 1) - 1 - left = (i) => (i << 1) + 1 - right = (i) => (i + 1) << 1 - greater = (i, j) => this.comparator(this.heap[i], this.heap[j]) - swap = (i, j) => ([this.heap[i], this.heap[j]] = [this.heap[j], this.heap[i]]) - siftUp = () => { - let node = this.size() - 1 - while (node > this.top && this.greater(node, this.parent(node))) { - this.swap(node, this.parent(node)) - node = this.parent(node) - } - } - siftDown = () => { - let node = this.top - while ( - (this.left(node) < this.size() && this.greater(this.left(node), node)) || - (this.right(node) < this.size() && this.greater(this.right(node), node)) - ) { - let maxChild = - this.right(node) < this.size() && - this.greater(this.right(node), this.left(node)) - ? this.right(node) - : this.left(node) - this.swap(node, maxChild) - node = maxChild - } - } -} diff --git a/314-binary-tree-vertical-order-traversal.js b/314-binary-tree-vertical-order-traversal.js index 46123f5d..d8eceed1 100644 --- a/314-binary-tree-vertical-order-traversal.js +++ b/314-binary-tree-vertical-order-traversal.js @@ -121,24 +121,3 @@ const verticalOrder = function(root) { return res }; -// another - -const verticalOrder = function(root) { - if (!root) return [] - let result = [] - function recurse(root, col, row) { - if (!root) return - recurse(root.left, col - 1, row + 1) - recurse(root.right, col + 1, row + 1) - result[col] = result[col] || [] - result[col][row] = result[col][row] || [] - result[col][row].push(root.val) - } - - recurse(root, 100, 0) - return result - .filter(x => x) - .map(row => row.reduce((acc, val) => acc.concat(val), [])) -} - - diff --git a/315-count-of-smaller-numbers-after-self.js b/315-count-of-smaller-numbers-after-self.js index 8bda9213..aa17b72a 100644 --- a/315-count-of-smaller-numbers-after-self.js +++ b/315-count-of-smaller-numbers-after-self.js @@ -39,99 +39,3 @@ function mergeSort(arr, output) { return sorted } -// another - -class Node { - constructor(v, s) { - this.val = v - this.sum = s - this.left = null - this.right = null - this.dup = 1 - } -} -/** - * @param {number[]} nums - * @return {number[]} - */ -const countSmaller = function(nums) { - const ans = new Array(nums.length).fill(0) - let root = null - for (let i = nums.length - 1; i >= 0; i--) { - root = insert(nums[i], root, ans, i, 0) - } - return ans -} - -function insert(num, node, ans, i, preSum) { - if (node == null) { - node = new Node(num, 0) - ans[i] = preSum - } else if (node.val == num) { - node.dup++ - ans[i] = preSum + node.sum - } else if (node.val > num) { - node.sum++ - node.left = insert(num, node.left, ans, i, preSum) - } else { - node.right = insert(num, node.right, ans, i, preSum + node.dup + node.sum) - } - return node -} - -// another - -/** - * @param {number[]} nums - * @return {number[]} - */ -const countSmaller = function(nums) { - - const arr = [] - const n = nums.length - for(let i = 0; i < n; i++) { - arr.push([nums[i], i]) - } - - const res = Array(n).fill(0) - cntSmaller(arr, 0, n - 1, res) - - return res - - function cntSmaller(arr, l, r, res) { - if(l >= r) return - - const mid = ~~((l + r) / 2) - cntSmaller(arr, l, mid, res) - cntSmaller(arr, mid + 1, r, res) - let leftPos = l, rightPos = mid + 1, cnt = 0 - const merged = [] - while(leftPos < mid + 1 && rightPos <= r) { - if(arr[leftPos][0] > arr[rightPos][0]) { - cnt++ - merged.push(arr[rightPos]) - rightPos++ - } else { - res[arr[leftPos][1]] += cnt - merged.push(arr[leftPos]) - leftPos++ - } - } - - while(leftPos < mid + 1) { - res[arr[leftPos][1]] += cnt - merged.push(arr[leftPos]) - leftPos++ - } - - while(rightPos <= r) { - merged.push(arr[rightPos]) - rightPos++ - } - - for(let i = l; i <= r; i++) { - arr[i] = merged[i - l] - } - - } -}; diff --git a/316-remove-duplicate-letters.js b/316-remove-duplicate-letters.js index a64a93b2..76e6387a 100644 --- a/316-remove-duplicate-letters.js +++ b/316-remove-duplicate-letters.js @@ -20,59 +20,3 @@ const removeDuplicateLetters = function(s) { return stack.reverse().join('') } -// another - - -/** - * @param {string} s - * @return {string} - */ -const removeDuplicateLetters = function(s) { - const m = new Array(26) - const a = 'a'.charCodeAt(0) - for (let i = 0; i < s.length; i++) { - const k = s.charCodeAt(i) - a - m[k] = m[k] ? m[k] + 1 : 1 - } - const aChNo = [] - const visited = {} - for (let i = 0; i < s.length; i++) { - const k = s.charCodeAt(i) - a - m[k]-- - if (visited[k]) continue - while (aChNo.length > 0) { - const last = aChNo[aChNo.length - 1] - a - if (last > k && m[last] > 0) { - visited[last] = 0 - aChNo.pop() - } else break - } - visited[k] = 1 - aChNo.push(k + a) - } - return String.fromCharCode(...aChNo) -} - -// another - -/** - * @param {string} s - * @return {string} - */ -const removeDuplicateLetters = function(s) { - const last = {} - for (let i = 0; i < s.length; i++) last[s.charAt(i)] = i - const added = {} - const stack = [] - for (let i = 0; i < s.length; i++) { - const char = s.charAt(i) - if (added[char]) continue - while (stack.length && char < stack[stack.length - 1] && last[stack[stack.length - 1]] > i) { - added[stack[stack.length - 1]] = false - stack.pop() - } - stack.push(char) - added[char] = true - } - return stack.join('') -} diff --git a/32-longest-valid-parentheses.js b/32-longest-valid-parentheses.js index cd69003d..21feb95f 100644 --- a/32-longest-valid-parentheses.js +++ b/32-longest-valid-parentheses.js @@ -19,59 +19,3 @@ const longestValidParentheses = function(s) { return max } -// another - -const longestValidParentheses = function(s) { - let longest = 0 - let stack = [-1] - for (let i = 0; i < s.length; i++) { - if (s[i] === "(") { - stack.push(i) - } else { - stack.pop() - if (!stack.length) stack.push(i) - else longest = Math.max(longest, i - stack[stack.length - 1]) - } - } - - return longest -} - -// another - -/** - * @param {string} s - * @return {number} - */ -const longestValidParentheses = function (s) { - let res = 0, - stk = [], - n = s.length, - idxStk = [] - for (let i = 0; i < n; i++) { - const ch = s[i] - if (stk.length && stk[stk.length - 1] === '(' && ch === ')') - stk.pop(), idxStk.pop() - else stk.push(ch), idxStk.push(i) - res = Math.max(res, i - (idxStk.length ? idxStk[idxStk.length - 1] : -1)) - } - return res -} -/** - * @param {string} s - * @return {number} - */ -const longestValidParentheses = function (s) { - let res = 0, - stk = [], - n = s.length, - idxStk = [] - for (let i = 0; i < n; i++) { - const ch = s[i] - if (stk.length && stk[stk.length - 1] === '(' && ch === ')') - stk.pop(), idxStk.pop() - else stk.push(ch), idxStk.push(i) - res = Math.max(res, i - (idxStk.length ? idxStk[idxStk.length - 1] : -1)) - } - return res -} diff --git a/320-generalized-abbreviation.js b/320-generalized-abbreviation.js index b8fc1b48..441413b3 100644 --- a/320-generalized-abbreviation.js +++ b/320-generalized-abbreviation.js @@ -43,24 +43,3 @@ const generateAbbreviations = function(word) { } }; -// another - -/** - * @param {string} word - * @return {string[]} - */ -const generateAbbreviations = function(word) { - const arr = [] - dfs(arr, word, 0, '', 0) - return arr -}; - -function dfs(res, word, pos, cur, cnt) { - if(pos === word.length) { - if(cnt > 0) cur += cnt - res.push(cur) - } else { - dfs(res, word, pos + 1, cur, cnt + 1) - dfs(res, word, pos + 1, cur + (cnt > 0 ? cnt : '') + word.charAt(pos), 0) - } -} diff --git a/322-coin-change.js b/322-coin-change.js index 0c795775..1a0b2a57 100644 --- a/322-coin-change.js +++ b/322-coin-change.js @@ -15,27 +15,3 @@ const coinChange = function(coins, amount) { } -// another - -/** - * @param {number[]} coins - * @param {number} amount - * @return {number} - */ -const coinChange = function (coins, amount) { - const n = coins.length - const dp = Array.from({ length: n }, () => - Array(amount + 1).fill(Infinity) - ) - - for (let i = 0; i < n; i++) { - dp[i][0] = 0 - for (let j = 1; j <= amount; j++) { - if(i > 0) dp[i][j] = dp[i - 1][j] - if (j >= coins[i]) { - dp[i][j] = Math.min(dp[i][j], dp[i][j - coins[i]] + 1) - } - } - } - return dp[n - 1][amount] === Infinity ? -1 : dp[n - 1][amount] -} diff --git a/326-power-of-three.js b/326-power-of-three.js index 6657894b..1ee6a9b3 100644 --- a/326-power-of-three.js +++ b/326-power-of-three.js @@ -10,23 +10,3 @@ const isPowerOfThree = function(n) { return maxInt % n === 0 } -// another - -const isPowerOfThree = function(n) { - if (n == 1) return true - if (n === 0) return false - if (n % 3 !== 0) return false - if (n == 3) return true - return isPowerOfThree(n / 3) -} - -// another - -const isPowerOfThree = function(n) { - if(n == null || n === 0) return false - let num = 1 - while(num < n) { - num *= 3 - } - return num > n ? false : true -} diff --git a/328-odd-even-linked-list.js b/328-odd-even-linked-list.js index 068e799b..af1a50f2 100755 --- a/328-odd-even-linked-list.js +++ b/328-odd-even-linked-list.js @@ -24,39 +24,3 @@ const oddEvenList = function(head) { return head; }; -// another - -/** - * Definition for singly-linked list. - * function ListNode(val, next) { - * this.val = (val===undefined ? 0 : val) - * this.next = (next===undefined ? null : next) - * } - */ -/** - * @param {ListNode} head - * @return {ListNode} - */ -function oddEvenList(head) { - if(head == null) return head - const dummyOdd = new ListNode() - const dummyEven = new ListNode() - - dummyOdd.next = head - let odd = head, even = dummyEven - let idx = 2, cur = head.next - while(cur) { - if (idx % 2 === 1) { - odd.next = cur - odd = odd.next - } else { - even.next = cur - even = even.next - } - cur = cur.next - idx++ - } - odd.next = dummyEven.next - even.next = null - return dummyOdd.next -} diff --git a/329-longest-increasing-path-in-a-matrix.js b/329-longest-increasing-path-in-a-matrix.js index 6cce9611..526e288c 100644 --- a/329-longest-increasing-path-in-a-matrix.js +++ b/329-longest-increasing-path-in-a-matrix.js @@ -34,49 +34,3 @@ const longestIncreasingPath = function(matrix) { } }; -// another - -/** - * @param {number[][]} matrix - * @return {number} - */ -const longestIncreasingPath = function (matrix) { - const dirs = [ - [-1, 0], - [1, 0], - [0, -1], - [0, 1], - ] - const m = matrix.length, - n = matrix[0].length - let res = 1 - const memo = Array.from({ length: m }, () => Array(n).fill(0)) - for (let i = 0; i < m; i++) { - for (let j = 0; j < n; j++) { - const tmp = dfs(matrix, i, j, m, n, memo, dirs) - res = Math.max(tmp, res) - } - } - return res -} - -function dfs(matrix, i, j, m, n, memo, dirs) { - if (memo[i][j] !== 0) return memo[i][j] - let res = 1 - for (let [dx, dy] of dirs) { - const nx = i + dx, - ny = j + dy - if ( - nx < 0 || - nx >= m || - ny < 0 || - ny >= n || - matrix[nx][ny] <= matrix[i][j] - ) - continue - const tmp = 1 + dfs(matrix, nx, ny, m, n, memo, dirs) - res = Math.max(res, tmp) - } - memo[i][j] = res - return res -} diff --git a/33-search-in-rotated-sorted-array.js b/33-search-in-rotated-sorted-array.js index 0d96552b..9ba853dc 100644 --- a/33-search-in-rotated-sorted-array.js +++ b/33-search-in-rotated-sorted-array.js @@ -27,45 +27,3 @@ const search = function(nums, target) { return -1 }; -// another - -const search = function(nums, target) { - const len = nums.length - for(let i = 0; nums[i] <= target; i++){ - if(nums[i] === target){ - return i - } - } - for(let j = len - 1; nums[j] >= target; j--){ - if(nums[j] === target){ - return j - } - } - return -1 -}; - -// another -const search = function(nums, target) { - let low = 0 - let high = nums.length - 1 - while (low <= high) { - let mid = low + ((high - low) >> 1) - if (nums[mid] === target) return mid - - if (nums[low] <= nums[mid] ) { - if (target < nums[mid] && target >= nums[low]) { - high = mid - 1 - } else { - low = mid + 1 - } - } else { - if (target > nums[mid] && target <= nums[high]) { - low = mid + 1 - } else { - high = mid - 1 - } - } - - } - return -1 -}; diff --git a/333-largest-bst-subtree.js b/333-largest-bst-subtree.js index f3655aae..c7b1269a 100644 --- a/333-largest-bst-subtree.js +++ b/333-largest-bst-subtree.js @@ -54,21 +54,3 @@ function helper(node) { } } -// another - -const largestBSTSubtree = function(root) { - function dfs(node) { - if (!node) return [0, 0, Number.MAX_VALUE, -Number.MAX_VALUE] - const [N1, n1, min1, max1] = dfs(node.left) - const [N2, n2, min2, max2] = dfs(node.right) - const n = - max1 < node.val && min2 > node.val ? n1 + 1 + n2 : -Number.MAX_VALUE - return [ - Math.max(N1, N2, n), - n, - Math.min(min1, node.val), - Math.max(max2, node.val) - ] - } - return dfs(root)[0] -} diff --git a/334-increasing-triplet-subsequence.js b/334-increasing-triplet-subsequence.js index d8b22cc0..f21bf4fc 100644 --- a/334-increasing-triplet-subsequence.js +++ b/334-increasing-triplet-subsequence.js @@ -13,44 +13,3 @@ const increasingTriplet = function(nums) { return false; }; -// another - -/** - * @param {number[]} nums - * @return {boolean} - */ -const increasingTriplet = function(nums) { - const n = nums.length, stk = [] - for(let e of nums) { - let l = 0, r = stk.length - while(l < r) { - const mid = l + Math.floor((r - l) / 2) - if (e > stk[mid]) l = mid + 1 - else r = mid - } - - stk[l] = e - if(stk.length > 2) return true - } - - return false -}; - -// another - -/** - * @param {number[]} nums - * @return {boolean} - */ -const increasingTriplet = function(nums) { - let small = Number.MAX_VALUE, big = Number.MAX_VALUE - - for(const e of nums) { - if(e <= small) small = e - else if(e <= big) big = e - else return true - } - - return false -}; - diff --git a/336-palindrome-pairs.js b/336-palindrome-pairs.js index e640f6d1..4e4d1ebc 100644 --- a/336-palindrome-pairs.js +++ b/336-palindrome-pairs.js @@ -73,56 +73,3 @@ class Trie { } } -// another - -const reverseStr = s => { - let str = '' - for (let i = 0; i < s.length; i++) { - str = s[i] + str - } - return str -} -const isPalindrome = str => { - for (let i = 0; i < str.length / 2; i++) { - if (str[i] !== str[str.length - 1 - i]) return false - } - return true -} -/** - * @param {string[]} words - * @return {number[][]} - */ -const palindromePairs = function(words) { - const map = new Map() - words.forEach((word, idx) => map.set(word, idx)) - const result = [] - if (map.has('')) { - const idx = map.get('') - words.forEach((word, i) => { - if (i !== idx && isPalindrome(word)) { - result.push([idx, map.get(word)]) - result.push([map.get(word), idx]) - } - }) - } - map.delete('') - words.forEach((word, idx) => { - for (let i = 0; i < word.length; i++) { - const left = word.slice(0, i) - const right = word.slice(i) - if (isPalindrome(left)) { - const reversedRight = reverseStr(right) - if (map.has(reversedRight) && map.get(reversedRight) !== idx) { - result.push([map.get(reversedRight), idx]) - } - } - if (isPalindrome(right)) { - const reversedLeft = reverseStr(left) - if (map.has(reversedLeft) && map.get(reversedLeft) !== idx) { - result.push([idx, map.get(reversedLeft)]) - } - } - } - }) - return result -} diff --git a/339-nested-list-weight-sum.js b/339-nested-list-weight-sum.js index d38d5c9f..d8e6b30a 100644 --- a/339-nested-list-weight-sum.js +++ b/339-nested-list-weight-sum.js @@ -56,21 +56,3 @@ function h(arr, level) { return sum } -// another - -const depthSum = function(nestedList) { - if(nestedList == null) return 0 - let sum = 0 - let level = 1 - const q = [...nestedList] - while(q.length) { - const len = q.length - for(let i = 0; i < len; i++) { - const el = q.shift() - if(el.isInteger()) sum += el.getInteger() * level - else q.push(...(el.getList())) - } - level++ - } - return sum -}; diff --git a/343-integer-break.js b/343-integer-break.js index 22446b08..4b9acb47 100755 --- a/343-integer-break.js +++ b/343-integer-break.js @@ -13,68 +13,3 @@ const integerBreak = function(n) { return dp[n] }; -// another - -/** - * @param {number} n - * @return {number} - */ -const integerBreak = function(n) { - if (n <= 2) return 1; - - const maxArr = []; - for (let i = 0; i < n + 1; i++) { - maxArr[i] = 0; - } - - /** For a number i: write i as a sum of integers, then take the product of those integers. - maxArr[i] = maximum of all the possible products */ - - maxArr[1] = 0; - maxArr[2] = 1; // 2=1+1 so maxArr[2] = 1*1 - - for (let i = 3; i <= n; i++) { - for (let j = 1; j < i; j++) { - /** Try to write i as: i = j + S where S=i-j corresponds to either one number or a sum of two or more numbers - - Assuming that j+S corresponds to the optimal solution for maxArr[i], we have two cases: - (1) i is the sum of two numbers, i.e. S=i-j is one number, and so maxArr[i]=j*(i-j) - (2) i is the sum of at least three numbers, i.e. S=i-j is a sum of at least 2 numbers, - and so the product of the numbers in this sum for S is maxArr[i-j] - (=maximum product after breaking up i-j into a sum of at least two integers): - maxArr[i] = j*maxArr[i-j] - */ - maxArr[i] = Math.max(maxArr[i], j * (i - j), j * maxArr[i - j]); - } - } - return maxArr[n]; -}; - -// another - -/** - * @param {number} n - * @return {number} - */ -const integerBreak = function(n) { - if(n === 2) return 1 - if(n === 3) return 2 - let num = ~~(n / 3) - let rem = n % 3 - if(rem === 1) { - rem += 3 - num-- - } - return rem === 0 ? Math.pow(3, num) : Math.pow(3, num) * rem -}; - -/** - -If an optimal product contains a factor f >= 4, -then you can replace it with factors 2 and f-2 without losing optimality, -as 2*(f-2) = 2f-4 >= f. So you never need a factor greater than or equal to 4, -meaning you only need factors 1, 2 and 3 (and 1 is of course wasteful and you'd only use it for n=2 and n=3, where it's needed). - -For the rest I agree, 3*3 is simply better than 2*2*2, so you'd never use 2 more than twice. - -*/ diff --git a/344-reverse-string.js b/344-reverse-string.js index 155ddcac..bbee0caf 100644 --- a/344-reverse-string.js +++ b/344-reverse-string.js @@ -6,14 +6,3 @@ const reverseString = function(s) { s.reverse() }; -// another - -/** - * @param {character[]} s - * @return {void} Do not return anything, modify s in-place instead. - */ -const reverseString = function(s) { - for(let i = 0; i < s.length / 2; i++){ - [ s[i] , s[s.length - 1 - i] ] = [ s[s.length - 1 - i] , s[i] ]; - } -}; diff --git a/345-reverse-vowels-of-a-string.js b/345-reverse-vowels-of-a-string.js index f0702f84..9ffdc9ab 100644 --- a/345-reverse-vowels-of-a-string.js +++ b/345-reverse-vowels-of-a-string.js @@ -22,15 +22,3 @@ const reverseVowels = function(s) { }; -// another - -const reverseVowels = function(s) { - let vowels = s.match(/[aeiou]/gi) - let k = 0 - if (vowels) { - vowels = vowels.reverse`` - } else { - return s - } - return s.replace(/[aeiou]/gi, () => vowels[k++]) -} diff --git a/347-top-k-frequent-elements.js b/347-top-k-frequent-elements.js index 5a59bc3f..c5f2c1f5 100755 --- a/347-top-k-frequent-elements.js +++ b/347-top-k-frequent-elements.js @@ -31,69 +31,3 @@ const topKFrequent = function(nums, k) { return res }; -// another - -/** - * @param {number[]} nums - * @param {number} k - * @return {number[]} - */ -const topKFrequent = function(nums, k) { - const hash = {} - for(let n of nums) { - if(hash[n] == null) hash[n] = 0 - hash[n]++ - } - const entries = Object.entries(hash) - let min = Infinity, max = -Infinity - const reverse = {} - for(let [k, freq] of entries) { - if(freq < min) min = freq - if(freq > max) max = freq - if(reverse[freq] == null) reverse[freq] = [] - reverse[freq].push(k) - } - const n = max - min + 1 - const arr = Array(n) - let res = [] - let limit = max - while(limit) { - if(reverse[limit]) res.push(...reverse[limit]) - limit-- - if(res.length >= k) break - } - res.splice(k) - return res -}; - -// another - - -/** - * @param {number[]} nums - * @param {number} k - * @return {number[]} - */ - const topKFrequent = function(nums, k) { - const n = nums.length - const freq = Array(n + 1).fill(null) - const hash = {} - for(let e of nums) { - if(hash[e] == null) hash[e] = 0 - hash[e]++ - } - for(let k in hash) { - if(hash.hasOwnProperty(k)) { - const v = hash[k] - if(freq[v] == null) freq[v] = [] - freq[v].push(k) - } - } - const res = [] - for(let i = n; i >= 0; i--) { - if(freq[i] != null) res.push(...freq[i]) - if(res.length >= k) break - } - if(res.length > k) res.splice(k) - return res -}; diff --git a/35-search-insert-position.js b/35-search-insert-position.js index 7bd01758..1422a85a 100755 --- a/35-search-insert-position.js +++ b/35-search-insert-position.js @@ -15,21 +15,3 @@ const searchInsert = function(nums, target) { } }; -// another - -/** - * @param {number[]} nums - * @param {number} target - * @return {number} - */ -const searchInsert = function(nums, target) { - const n = nums.length - let l = 0, r = n - 1 - while(l <= r) { - const mid = l + ((r - l) >> 1) - if(nums[mid] === target) return mid - if(nums[mid] > target) r = mid - 1 - else l = mid + 1 - } - return l -}; diff --git a/351-android-unlock-patterns.js b/351-android-unlock-patterns.js index e8fc35eb..0c01e926 100644 --- a/351-android-unlock-patterns.js +++ b/351-android-unlock-patterns.js @@ -39,42 +39,3 @@ function DFS(vis, skip, cur, remain) { return rst } -// another - -/** - * @param {number} m - * @param {number} n - * @return {number} - */ -const numberOfPatterns = function (m, n) { - const skip = Array.from({ length: 10 }, () => Array(10).fill(0)) - let res = 0 - skip[1][3] = skip[3][1] = 2 - skip[1][7] = skip[7][1] = 4 - skip[9][7] = skip[7][9] = 8 - skip[9][3] = skip[3][9] = 6 - skip[1][9] = skip[9][1] = skip[2][8] = skip[8][2] = skip[3][7] = skip[7][3] = skip[4][6] = skip[6][4] = 5 - const visited = new Set() - for(let i = m ;i <= n; i++) { - res += dfs(1, i - 1) * 4 // 1, 3, 7, 9 - res += dfs(2, i - 1) * 4 // 2, 4, 6, 8 - res += dfs(5, i - 1) // 5 - } - - return res - - function dfs(cur, remain) { - if(remain === 0) return 1 - let res = 0 - visited.add(cur) - for(let i = 1; i <= 9; i++) { - if(!visited.has(i) && (skip[cur][i] === 0 || visited.has(skip[cur][i]))) { - res += dfs(i, remain - 1) - } - } - visited.delete(cur) - - return res - } -} - diff --git a/354-russian-doll-envelopes.js b/354-russian-doll-envelopes.js index efdd4fa9..e119e9d3 100644 --- a/354-russian-doll-envelopes.js +++ b/354-russian-doll-envelopes.js @@ -24,60 +24,3 @@ const maxEnvelopes = function(envelopes) { return stk.length }; -// another - - -/** - * @param {number[][]} envelopes - * @return {number} - */ -const maxEnvelopes = function(envelopes) { - envelopes.sort((a, b) => { - if (a[0] == b[0]) { - return b[1] - a[1] - } else { - return a[0] - b[0] - } - }) - const n = envelopes.length - const dp = [] - for (let i = 0; i < n; i++) { - let l = 0, - r = dp.length, - t = envelopes[i][1] - while (l < r) { - let m = l + ~~((r - l) / 2) - if (dp[m] < t) l = m + 1 - else r = m - } - if (r >= dp.length) dp.push(t) - else dp[r] = t - } - return dp.length -} - -// another - -/** - * @param {number[][]} envelopes - * @return {number} - */ -const maxEnvelopes = function(envelopes) { - envelopes.sort((a, b) => a[0] === b[0] ? b[1] - a[1] : a[0] - b[0]) - const stack = [] - for(let e of envelopes) { - if(stack.length === 0 || e[1] > stack[stack.length - 1][1]) { - stack.push(e) - continue - } - let l = 0, r = stack.length - 1 - while(l < r) { - const mid = ~~((l+r)/2) - if(stack[mid][1] < e[1]) { - l = mid + 1 - } else r = mid - } - stack[l] = e - } - return stack.length -}; diff --git a/357-count-numbers-with-unique-digits.js b/357-count-numbers-with-unique-digits.js index 03018011..e5cd8c27 100755 --- a/357-count-numbers-with-unique-digits.js +++ b/357-count-numbers-with-unique-digits.js @@ -16,67 +16,3 @@ const countNumbersWithUniqueDigits = function(n) { return res }; -// another - -/** - * @param {number} n - * @return {number} - */ -const countNumbersWithUniqueDigits = function(n) { - if (n === 0) return 1; - let res = 10; - let tmp = 9; - let remainDigitNum = 9; - while (n - 1 > 0 && remainDigitNum > 0) { - tmp = tmp * remainDigitNum; - res += tmp; - n -= 1; - remainDigitNum -= 1; - } - - return res; -}; - - -// another - -/** - * @param {number} n - * @return {number} - */ -const countNumbersWithUniqueDigits = function(n) { - const limit = 10 ** n - let res = 1 - let m = 1 - if(n === 0) return 1 - while(10**m <= limit) { - res += 9 * helper(9, m - 1) - m++ - } - - return res - - function helper(m, n) { - return n === 0 ? 1 : helper(m, n - 1) * (m - n + 1) - } -}; - -// another - -/** - * @param {number} n - * @return {number} - */ -const countNumbersWithUniqueDigits = function(n) { - if(n === 0) return 1 - let res = 10 - let tmp = 9, digits = 9 - while(n > 1 && digits > 0) { - tmp *= digits - res += tmp - n-- - digits-- - } - - return res -}; diff --git a/358-rearrange-string-k-distance-apart.js b/358-rearrange-string-k-distance-apart.js index 825883f5..f0f22313 100644 --- a/358-rearrange-string-k-distance-apart.js +++ b/358-rearrange-string-k-distance-apart.js @@ -61,107 +61,3 @@ function findValidMax(count, valid, index) { return candidatePos } -// another - -/** - * @param {string} s - * @param {number} k - * @return {string} - */ -const rearrangeString = function(s, k) { - const freq = Array(26).fill(0) - const a = 'a'.charCodeAt(0) - for(const e of s) { - freq[e.charCodeAt(0) - a]++ - } - const pq = new PriorityQueue((a, b) => a[1] > b[1]) - for(let i = 0; i < 26; i++) { - if(freq[i]) pq.push([i, freq[i]]) - } - let res = '' - - // console.log(pq) - const q = [] - while(!pq.isEmpty()) { - const cur = pq.pop() - cur[1]-- - res += String.fromCharCode(a + cur[0]) - q.push(cur) - if(q.length >= k) { - const p = q.shift() - if(p[1] > 0) pq.push(p) - } - } - // console.log(res) - return res.length === s.length ? res : '' -} - -class PriorityQueue { - constructor(comparator = (a, b) => a > b) { - this.heap = [] - this.top = 0 - this.comparator = comparator - } - size() { - return this.heap.length - } - isEmpty() { - return this.size() === 0 - } - peek() { - return this.heap[this.top] - } - push(...values) { - values.forEach((value) => { - this.heap.push(value) - this.siftUp() - }) - return this.size() - } - pop() { - const poppedValue = this.peek() - const bottom = this.size() - 1 - if (bottom > this.top) { - this.swap(this.top, bottom) - } - this.heap.pop() - this.siftDown() - return poppedValue - } - replace(value) { - const replacedValue = this.peek() - this.heap[this.top] = value - this.siftDown() - return replacedValue - } - - parent = (i) => ((i + 1) >>> 1) - 1 - left = (i) => (i << 1) + 1 - right = (i) => (i + 1) << 1 - greater = (i, j) => this.comparator(this.heap[i], this.heap[j]) - swap = (i, j) => ([this.heap[i], this.heap[j]] = [this.heap[j], this.heap[i]]) - siftUp = () => { - let node = this.size() - 1 - while (node > this.top && this.greater(node, this.parent(node))) { - this.swap(node, this.parent(node)) - node = this.parent(node) - } - } - siftDown = () => { - let node = this.top - while ( - (this.left(node) < this.size() && this.greater(this.left(node), node)) || - (this.right(node) < this.size() && this.greater(this.right(node), node)) - ) { - let maxChild = - this.right(node) < this.size() && - this.greater(this.right(node), this.left(node)) - ? this.right(node) - : this.left(node) - this.swap(node, maxChild) - node = maxChild - } - } -} - - diff --git a/36-valid-sudoku.js b/36-valid-sudoku.js index d99b0bfe..cfddc13d 100644 --- a/36-valid-sudoku.js +++ b/36-valid-sudoku.js @@ -27,32 +27,3 @@ const isValidSudoku = function(board) { return true } -// another - -/** - * @param {character[][]} board - * @return {boolean} - */ -const isValidSudoku = function(board) { - let seen = new Set() - for (let i = 0; i < 9; ++i) { - for (let j = 0; j < 9; ++j) { - let number = board[i][j] - if (number != '.') - if ( - !hset(seen, number + ' in row ' + i) || - !hset(seen, number + ' in column ' + j) || - !hset(seen, number + ' in block ' + ~~(i / 3) + '-' + ~~(j / 3)) - ) - return false - } - } - return true -} -function hset(s, val) { - if (s.has(val)) return false - else { - s.add(val) - return true - } -} diff --git a/360-sort-transformed-array.js b/360-sort-transformed-array.js index 2caedcd0..88aef459 100644 --- a/360-sort-transformed-array.js +++ b/360-sort-transformed-array.js @@ -51,55 +51,3 @@ function quad(x, a, b, c) { return a * x * x + b * x +c } -// another - -/** - * @param {number[]} nums - * @param {number} a - * @param {number} b - * @param {number} c - * @return {number[]} - */ -const sortTransformedArray = function(nums, a, b, c) { - const ret = [] - const sum = v => a * v * v + b * v + c - if (a > 0) { - const point = (b / a / 2) * -1 - let i = 0, - j = nums.length - while (i < j) { - let ax = nums[i] - if (Math.abs(nums[i] - point) - Math.abs(nums[j - 1] - point) > 0) { - ++i - } else { - ax = nums[--j] - } - ret.unshift(sum(ax)) - } - return ret - } else if (a < 0) { - const point = (b / a / 2) * -1 - let i = 0, - j = nums.length - while (i < j) { - let ax = nums[i] - if (Math.abs(ax - point) - Math.abs(nums[j - 1] - point) > 0) { - ++i - } else { - ax = nums[--j] - } - ret.push(sum(ax)) - } - return ret - } else { - if (b > 0) { - return nums.map(v => sum(v)) - } else { - nums.forEach(v => { - ret.unshift(sum(v)) - }) - return ret - } - } -} - diff --git a/361-bomb-enemy.js b/361-bomb-enemy.js index dd401e0a..9aee4bc5 100644 --- a/361-bomb-enemy.js +++ b/361-bomb-enemy.js @@ -51,65 +51,3 @@ const maxKilledEnemies = function(grid) { return result } -// another - -/** - * @param {character[][]} grid - * @return {number} - */ -const maxKilledEnemies = function(grid) { - if (grid == null || grid.length === 0 || grid[0].length === 0) return 0 - const rows = grid.length - const cols = grid[0].length - let max = 0 - const dp = Array.from({ length: rows }, () => new Array(cols).fill(0)) - //travel each column twice: from left and from right - for (let i = 0; i < rows; i++) { - let cnt = 0 - for (let k = 0; k < cols; k++) { - if (grid[i][k] === '0') { - dp[i][k] = cnt - } else if (grid[i][k] === 'E') { - cnt++ - } else { - cnt = 0 - } - } - cnt = 0 - for (let k = cols - 1; k >= 0; k--) { - if (grid[i][k] === '0') { - dp[i][k] += cnt - } else if (grid[i][k] === 'E') { - cnt++ - } else { - cnt = 0 - } - } - } - //travel each row twice: from top and from bottom - for (let i = 0; i < cols; i++) { - let cnt = 0 - for (let k = 0; k < rows; k++) { - if (grid[k][i] === '0') { - dp[k][i] += cnt - } else if (grid[k][i] === 'E') { - cnt++ - } else { - cnt = 0 - } - } - cnt = 0 - for (let k = rows - 1; k >= 0; k--) { - if (grid[k][i] === '0') { - dp[k][i] += cnt - max = Math.max(max, dp[k][i]) - } else if (grid[k][i] === 'E') { - cnt++ - } else { - cnt = 0 - } - } - } - return max -} - diff --git a/364-nested-list-weight-sum-ii.js b/364-nested-list-weight-sum-ii.js index e3d1f4b1..442f4caf 100644 --- a/364-nested-list-weight-sum-ii.js +++ b/364-nested-list-weight-sum-ii.js @@ -62,33 +62,3 @@ function sum(nestedList, n) { .reduce((total, list) => total + sum(list, n - 1), 0) } -// another - -const depthSumInverse = function(nestedList) { - const Q = [] - let temp = [] - while (nestedList.length) { - temp = [] - for (let i = 0; i < nestedList.length; i++) { - if (nestedList[i].isInteger()) { - Q.push(nestedList[i].getInteger()) - } else { - let list = nestedList[i].getList() - temp.push(...list) - } - } - Q.push('level') - nestedList = temp - } - let sum = 0 - let level = 0 - while (Q.length) { - let item = Q.pop() - if (item === 'level') { - level++ - } else { - sum += item * level - } - } - return sum -} diff --git a/366-find-leaves-of-binary-tree.js b/366-find-leaves-of-binary-tree.js index a88ff385..ef3cca54 100644 --- a/366-find-leaves-of-binary-tree.js +++ b/366-find-leaves-of-binary-tree.js @@ -68,18 +68,3 @@ function leaves(node, p, res) { leaves(node.right, node, res) } -// another - -const findLeaves = function(root) { - const res = [] - dfs(root, res) - return res -}; - -function dfs(node, res) { - if(node == null) return -1 - const i = 1 + Math.max(dfs(node.left, res), dfs(node.right, res)) - if(!res[i]) res[i] = [] - res[i].push(node.val) - return i -} diff --git a/368-largest-divisible-subset.js b/368-largest-divisible-subset.js index bd74d95a..128a20ef 100644 --- a/368-largest-divisible-subset.js +++ b/368-largest-divisible-subset.js @@ -30,36 +30,3 @@ const largestDivisibleSubset = function(nums) { return res }; -// another - -/** - * @param {number[]} nums - * @return {number[]} - */ -const largestDivisibleSubset = function(nums) { - let len = nums.length; - let maxSize = 0; - let maxSizeLastIdx; - // T[n] should be the length of the largest divisible subset whose smallest number is a[n] - const T = new Array(len).fill(0); - const son = new Array(len).fill(0); - nums.sort((a, b) => a - b); - for (let i = 0; i < len; i++) { - for (let j = i; j >= 0; j--) { - if (nums[i] % nums[j] === 0 && T[j] + 1 > T[i]) { - T[i] = T[j] + 1; - son[i] = j; - } - } - if (T[i] > maxSize) { - maxSize = T[i]; - maxSizeLastIdx = i; - } - } - const re = []; - for (let i = 0; i < maxSize; i++) { - re.unshift(nums[maxSizeLastIdx]); - maxSizeLastIdx = son[maxSizeLastIdx]; - } - return re; -}; diff --git a/369-plus-one-linked-list.js b/369-plus-one-linked-list.js index 23f754b5..3e2a4c32 100644 --- a/369-plus-one-linked-list.js +++ b/369-plus-one-linked-list.js @@ -38,28 +38,3 @@ const plusOneRecursion = node => { return 0 } -// another - -/** - * @param {ListNode} head - * @return {ListNode} - */ -const plusOne = function(head) { - const dummy = new ListNode(0) - dummy.next = head - let node = head - let lastNotNine = dummy - while(node) { - if(node.val !== 9) lastNotNine = node - node = node.next - } - lastNotNine.val++ - node = lastNotNine.next - while(node) { - node.val = 0 - node = node.next - } - return dummy.val === 1 ? dummy : dummy.next -} - - diff --git a/37-sudoku-solver.js b/37-sudoku-solver.js index b057ce6e..59f9e07f 100644 --- a/37-sudoku-solver.js +++ b/37-sudoku-solver.js @@ -36,37 +36,3 @@ const solveSudoku = function(board) { } -// another - -/** - * @param {character[][]} board - * @return {void} Do not return anything, modify board in-place instead. - */ -const solveSudoku = function(board) { - helper(board, 0 , 0) -}; - -function helper(board, row, col) { - if(row >= 9) return true - if(col >= 9) return helper(board, row + 1, 0) - if(board[row][col] !== '.') return helper(board, row, col + 1) - for(let i = 1; i <= 9; i++) { - const ch = `${i}` - if(valid(board, row, col, ch)) { - board[row][col] = ch - if(helper(board, row, col + 1)) return true - board[row][col] = '.' - } - } - return false -} - -function valid(board, row, col, ch) { - const blkRow = ~~(row / 3), blkCol = ~~(col / 3) - for(let i = 0; i < 9; i++) { - if(board[row][i] === ch) return false - if(board[i][col] === ch) return false - if(board[blkRow * 3 + Math.floor(i / 3)][blkCol * 3 + (i % 3)] === ch) return false - } - return true -} diff --git a/373-find-k-pairs-with-smallest-sums.js b/373-find-k-pairs-with-smallest-sums.js index 35fd1146..f18891c7 100644 --- a/373-find-k-pairs-with-smallest-sums.js +++ b/373-find-k-pairs-with-smallest-sums.js @@ -89,39 +89,3 @@ class PriorityQueue { } -// another - -/** - * @param {number[]} nums1 - * @param {number[]} nums2 - * @param {number} k - * @return {number[][]} - */ -const kSmallestPairs = function(nums1, nums2, k) { - let len1 = nums1.length, - len2 = nums2.length - let arr = Array(len1).fill(0), - resList = [] - while (k-- > 0) { - let min = Infinity, - index = -1, - lastj = Infinity - for (let i = 0; i < len1; i++) { - const j = arr[i] - if (j < lastj && j < len2) { - const sum = nums1[i] + nums2[j] - if (sum < min) { - min = sum - index = i - } - lastj = j - } - } - if (index == -1) { - break - } - resList.push([nums1[index], nums2[arr[index]]]) - arr[index]++ - } - return resList -} diff --git a/376-wiggle-subsequence.js b/376-wiggle-subsequence.js index f20795a5..026b44f6 100644 --- a/376-wiggle-subsequence.js +++ b/376-wiggle-subsequence.js @@ -16,23 +16,3 @@ const wiggleMaxLength = function(nums) { return count } -// another - -/** - * @param {number[]} nums - * @return {number} - */ -const wiggleMaxLength = function(nums) { - const len = nums.length - if (len === 0) return 0 - let up = 1 - let down = 1 - for (let i = 1; i < len; i++) { - if (nums[i] > nums[i - 1]) { - up = down + 1 - } else if (nums[i] < nums[i - 1]) { - down = up + 1 - } - } - return Math.max(up, down) -} diff --git a/386-lexicographical-numbers.js b/386-lexicographical-numbers.js index d593eb57..28b40459 100644 --- a/386-lexicographical-numbers.js +++ b/386-lexicographical-numbers.js @@ -21,41 +21,3 @@ const lexicalOrder = function(n) { } }; -// another - -/** - * @param {number} n - * @return {number[]} - */ -const lexicalOrder = function(n) { - const result = [] - for (let i = 1; i < 10; i++) { - dfs(i) - } - function dfs(n) { - if (n <= num) result.push(n) - if (10 * n <= num) { - for (let j = 0; j < 10; j++) { - dfs(10 * n + j) - } - } - } - return result -} - -// another - -const lexicalOrder = function(n) { - function getNumberByOrder(start, end) { - for (let i = start; i <= end; i++) { - if (i > n) { - break - } - res.push(i) - getNumberByOrder(i * 10, i * 10 + 9) - } - } - const res = [] - getNumberByOrder(1, 9) - return res -} diff --git a/387-first-unique-character-in-a-string.js b/387-first-unique-character-in-a-string.js index b03d0169..e8a159e5 100755 --- a/387-first-unique-character-in-a-string.js +++ b/387-first-unique-character-in-a-string.js @@ -27,22 +27,3 @@ const firstUniqChar = function(s) { return res[0] == null ? -1 : res[0]; }; -// another - -/** - * @param {string} s - * @return {number} - */ -const firstUniqChar = function(s) { - if(s === '') return -1 - const map = new Map() - for(let i = 0, len = s.length; i < len; i++) { - if(!map.has(s[i])) map.set(s[i], [i, 0]) - map.get(s[i])[1] += 1 - } - for(let [key, val] of map) { - if(val[1] === 1) return val[0] - } - return -1 - -}; diff --git a/389-find-the-difference.js b/389-find-the-difference.js index 16dc0ba6..daa12007 100755 --- a/389-find-the-difference.js +++ b/389-find-the-difference.js @@ -10,24 +10,3 @@ const findTheDifference = function(s, t) { return String.fromCharCode(xor) }; -// another - -/** - * @param {string} s - * @param {string} t - * @return {character} - */ -const findTheDifference = function(s, t) { - const arr = s.split(""); - let idx; - for (let i = 0; i < t.length; i++) { - idx = arr.indexOf(t[i]); - if (idx === -1) { - return t[i]; - } else { - arr.splice(idx, 1); - } - } -}; - -console.log(findTheDifference("abcd", "abcde")); diff --git a/392-is-subsequence.js b/392-is-subsequence.js index 46fe3361..d7dc5259 100644 --- a/392-is-subsequence.js +++ b/392-is-subsequence.js @@ -15,33 +15,3 @@ const isSubsequence = function(s, t) { return si === sl }; -// another - -/** - * @param {string} s - * @param {string} t - * @return {boolean} - */ -const isSubsequence = function(s, t) { - let ti = 0 - let tmp = 0 - for (let i = 0; i < s.length; i++) { - if ((tmp = chk(t, ti, s.charAt(i))) === -1) { - return false - } else { - ti = tmp + 1 - } - } - - return true -} - -function chk(str, start, target) { - let idx = start - for (let i = start; i < str.length; i++) { - if (str.charAt(i) === target) { - return i - } - } - return -1 -} diff --git a/397-integer-replacement.js b/397-integer-replacement.js index 36a4e692..c09d94ab 100644 --- a/397-integer-replacement.js +++ b/397-integer-replacement.js @@ -9,18 +9,3 @@ const integerReplacement = function(n, memo = {}) { return memo[n]; }; -// another -const integerReplacement = function(n) { - let c = 0; - while (n !== 1) { - if ((n & 1) === 0) { - n >>>= 1; - } else if (n === 3 || ((n >>> 1) & 1) === 0) { - --n; - } else { - ++n; - } - ++c; - } - return c; -}; diff --git a/4-median-of-two-sorted-arrays.js b/4-median-of-two-sorted-arrays.js index 095350b7..be8ca9a2 100755 --- a/4-median-of-two-sorted-arrays.js +++ b/4-median-of-two-sorted-arrays.js @@ -33,101 +33,3 @@ const findMedianSortedArrays = function(nums1, nums2) { } }; -// another - -/** - * @param {number[]} A - * @param {number[]} B - * @return {number} - */ - -const findMedianSortedArrays = function(A, B) { - let m = A.length, - n = B.length; - - if (m > n) { - return findMedianSortedArrays(B, A); - } - - let imin = 0, - imax = m, - i, - j; - while (imin <= imax) { - i = (imin + imax) >> 1; - j = ((m + n + 1) >> 1) - i; - if (j > 0 && i < m && B[j - 1] > A[i]) { - imin = i + 1; - } else if (i > 0 && j < n && A[i - 1] > B[j]) { - imax = i - 1; - } else { - if (i === 0) { - num1 = B[j - 1]; - } else if (j === 0) { - num1 = A[i - 1]; - } else { - num1 = Math.max(A[i - 1], B[j - 1]); - } - - if ((m + n) & 1) { - return num1; - } - - if (i === m) { - num2 = B[j]; - } else if (j === n) { - num2 = A[i]; - } else { - num2 = Math.min(A[i], B[j]); - } - return (num1 + num2) / 2.0; - } - } -}; - -// another - -/** - * @param {number[]} nums1 - * @param {number[]} nums2 - * @return {number} - */ -const findMedianSortedArrays = function (nums1, nums2) { - if (nums1.length > nums2.length) { - return findMedianSortedArrays(nums2, nums1) - } - const x = nums1.length - const y = nums2.length - - let low = 0 - let high = x - - while (low <= high) { - const partX = Math.floor((low + high) / 2) - const partY = Math.floor((x + y + 1) / 2) - partX - - const maxX = partX === 0 ? Number.NEGATIVE_INFINITY : nums1[partX - 1] - const maxY = partY === 0 ? Number.NEGATIVE_INFINITY : nums2[partY - 1] - - const minX = - partX === nums1.length ? Number.POSITIVE_INFINITY : nums1[partX] - const minY = - partY === nums2.length ? Number.POSITIVE_INFINITY : nums2[partY] - - if (maxX <= minY && maxY <= minX) { - const lowMax = Math.max(maxX, maxY) - - if ((x + y) % 2 == 1) { - return Math.max(maxX, maxY) - } else { - return (Math.max(maxX, maxY) + Math.min(minX, minY)) / 2 - } - } else if (maxX < minY) { - low = partX + 1 - } else { - high = partX - 1 - } - } -} - - diff --git a/402-remove-k-digits.js b/402-remove-k-digits.js index 21ef2e47..dbcc0828 100644 --- a/402-remove-k-digits.js +++ b/402-remove-k-digits.js @@ -26,50 +26,3 @@ const removeKdigits = function(num, k) { }; -// another - -/** - * @param {string} num - * @param {number} k - * @return {string} - */ -const removeKdigits = function(num, k) { - const n = num.length, stack = [] - if(n === k) return '0' - let i = 0 - while(i < n) { - while(k > 0 && stack.length && stack[stack.length - 1] > num[i]) { - k-- - stack.pop() - } - stack.push(num[i++]) - } - while(k-- > 0) stack.pop() - while(stack[0] === '0') stack.shift() - return stack.length ? stack.join('') : '0' -}; - -// another - -/** - * @param {string} num - * @param {number} k - * @return {string} - */ -const removeKdigits = function(num, k) { - const n = num.length, stack = [] - for(let i = 0; i < n; i++) { - const ch = num[i] - while(stack.length && k > 0 && ch < stack[stack.length - 1]) { - stack.pop() - k-- - } - stack.push(ch) - } - while(k > 0) { - stack.pop() - k-- - } - while(stack[0] === '0') stack.shift() - return stack.length ? stack.join('') : '0' -}; diff --git a/406-queue-reconstruction-by-height.js b/406-queue-reconstruction-by-height.js index 67f9eba1..8ef2a8d8 100755 --- a/406-queue-reconstruction-by-height.js +++ b/406-queue-reconstruction-by-height.js @@ -13,34 +13,3 @@ const reconstructQueue = function (people) { return queue } -// another - -/** - * @param {number[][]} people - * @return {number[][]} - */ -const reconstructQueue = function(people) { - if (!people) return []; - const peopledct = {}; - let height = []; - const res = []; - people.forEach((el, idx) => { - if (peopledct.hasOwnProperty(el[0])) { - peopledct[el[0]].push([el[1], idx]); - } else { - peopledct[el[0]] = [[el[1], idx]]; - height.push(el[0]); - } - }); - height = height.sort((a, b) => b - a); - - for (let i = 0; i < height.length; i++) { - peopledct[height[i]] = peopledct[height[i]].sort((a, b) => a[0] - b[0]); - for (el of peopledct[height[i]]) { - res.splice(el[0], 0, people[el[1]]); - } - } - return res; -}; - -console.log(reconstructQueue([[7, 0], [4, 4], [7, 1], [5, 0], [6, 1], [5, 2]])); diff --git a/407-trapping-rain-water-ii.js b/407-trapping-rain-water-ii.js index 21892090..063a1978 100644 --- a/407-trapping-rain-water-ii.js +++ b/407-trapping-rain-water-ii.js @@ -106,220 +106,3 @@ class PriorityQueue { -// another - -/** - * @param {number[][]} heightMap - * @return {number} - */ - -const trapRainWater = function (heightMap) { - const pq = new PriorityQueue((a, b) => a[2] < b[2]) - const visited = [] - for (let i = 0; i < heightMap.length; i++) { - visited[i] = [] - for (let j = 0; j < heightMap[0].length; j++) { - if ( - i > 0 && - i < heightMap.length - 1 && - j > 0 && - j < heightMap[0].length - 1 - ) - continue - pq.push([i, j, heightMap[i][j]]) - visited[i][j] = true - } - } - - let max = -Infinity, - count = 0 - while (!pq.isEmpty()) { - const cur = pq.pop() - if (cur[2] > max) max = cur[2] - check(cur[0], cur[1]) - } - function check(row, col) { - const step = [ - [-1, 0], - [1, 0], - [0, -1], - [0, 1], - ] - for (let i = 0; i < step.length; i++) { - let newR = row + step[i][0], - newC = col + step[i][1] - if ( - newR < 0 || - newR >= heightMap.length || - newC < 0 || - newC >= heightMap[0].length - ) - continue - if (visited[newR][newC]) continue - visited[newR][newC] = true - const newVal = heightMap[newR][newC] - if (newVal < max) { - count += max - newVal - check(newR, newC) - } else { - pq.push([newR, newC, newVal]) - } - } - } - - return count -} -class PriorityQueue { - constructor(comparator = (a, b) => a > b) { - this.heap = [] - this.top = 0 - this.comparator = comparator - } - size() { - return this.heap.length - } - isEmpty() { - return this.size() === 0 - } - peek() { - return this.heap[this.top] - } - push(...values) { - values.forEach((value) => { - this.heap.push(value) - this.siftUp() - }) - return this.size() - } - pop() { - const poppedValue = this.peek() - const bottom = this.size() - 1 - if (bottom > this.top) { - this.swap(this.top, bottom) - } - this.heap.pop() - this.siftDown() - return poppedValue - } - replace(value) { - const replacedValue = this.peek() - this.heap[this.top] = value - this.siftDown() - return replacedValue - } - - parent = (i) => ((i + 1) >>> 1) - 1 - left = (i) => (i << 1) + 1 - right = (i) => (i + 1) << 1 - greater = (i, j) => this.comparator(this.heap[i], this.heap[j]) - swap = (i, j) => ([this.heap[i], this.heap[j]] = [this.heap[j], this.heap[i]]) - siftUp = () => { - let node = this.size() - 1 - while (node > this.top && this.greater(node, this.parent(node))) { - this.swap(node, this.parent(node)) - node = this.parent(node) - } - } - siftDown = () => { - let node = this.top - while ( - (this.left(node) < this.size() && this.greater(this.left(node), node)) || - (this.right(node) < this.size() && this.greater(this.right(node), node)) - ) { - let maxChild = - this.right(node) < this.size() && - this.greater(this.right(node), this.left(node)) - ? this.right(node) - : this.left(node) - this.swap(node, maxChild) - node = maxChild - } - } -} - - -// another - -/** - * @param {number[][]} heightMap - * @return {number} - */ -const trapRainWater = function(heightMap) { - - function PriorityQueueMin(){ - let heap=[null] - function swim(idx){ - if(idx<2)return - let k=Math.floor(idx/2) - if(heap[idx][2]-heap[k][2]<0){ - swap(heap,idx,k) - idx=k - swim(idx) - } - } - function sink(idx){ - let k=Math.floor(idx*2) - if(k>=heap.length)return - if(k0) k++ - if(heap[idx][2]-heap[k][2]>0){ - swap(heap,idx,k) - idx=k - sink(idx) - } - } - function swap(arr,i,j){ - let temp=arr[i] - arr[i]=arr[j] - arr[j]=temp - } - this.insert=function (v) { - heap.push(v) - swim(heap.length-1) - } - this.delMin=function () { - swap(heap,1,heap.length-1) - let min=heap.pop() - sink(1) - return min - } - this.isEmpty=function () { - return heap.length===1 - } - } - - let pq=new PriorityQueueMin() - let visited=[] - for(let i=0;i0 && i0 && jmax)max=cur[2] - check(cur[0],cur[1]) - } - function check(row,col){ - let step=[[-1,0],[1,0],[0,-1],[0,1]] - for(let i=0;i=heightMap.length) || (newC<0 || newC>=heightMap[0].length))continue - if(visited[newR][newC])continue - visited[newR][newC]=true - let newVal=heightMap[newR][newC] - if(newVal { } const isNum = c => !isNaN(c) -// another - -/** - * @param {string} word - * @param {string} abbr - * @return {boolean} - */ -const validWordAbbreviation = (word, abbr) => { - let i = 0, - j = 0 - while (i < word.length && j < abbr.length) { - if (word.charAt(i) === abbr.charAt(j)) { - ++i - ++j - continue - } - if (abbr.charAt(j) <= '0' || abbr.charAt(j) > '9') { - return false - } - let start = j - while (j < abbr.length && abbr.charAt(j) >= '0' && abbr.charAt(j) <= '9') { - ++j - } - let num = +abbr.slice(start, j) - i += num - } - return i == word.length && j == abbr.length -} diff --git a/409-longest-palindrome.js b/409-longest-palindrome.js index 0d67ad8a..b7bc720f 100755 --- a/409-longest-palindrome.js +++ b/409-longest-palindrome.js @@ -19,34 +19,3 @@ const longestPalindrome = function (s) { return counter } -// another - -/** - * @param {string} s - * @return {number} - */ -const longestPalindrome = function(s) { - const hash = {}; - let c; - for (let i = 0; i < s.length; i++) { - c = s.charAt(i); - if (hash.hasOwnProperty(c)) { - hash[c] += 1; - } else { - hash[c] = 1; - } - } - let res = 0; - let val; - for (let k in hash) { - if (hash.hasOwnProperty(k)) { - val = hash[k]; - res += Math.floor(val / 2) * 2; - if (res % 2 === 0 && val % 2 === 1) { - res += 1; - } - } - } - - return res; -}; diff --git a/41-first-missing-positive.js b/41-first-missing-positive.js index fa054946..ed968b52 100644 --- a/41-first-missing-positive.js +++ b/41-first-missing-positive.js @@ -16,84 +16,3 @@ const firstMissingPositive = function(nums) { return max < 0 ? 1 : max + 1 }; -// another - -/** - * @param {number[]} nums - * @return {number} - */ -function firstMissingPositive(nums) { - const n = nums.length - for (let i = 0; i < n; i++) { - while (nums[i] > 0 && nums[i] <= n && nums[nums[i] - 1] !== nums[i]) - swap(nums, i, nums[i] - 1) - } - - for (let i = 0; i < n; i++) { - if (nums[i] !== i + 1) return i + 1 - } - return n + 1 -} - -function swap(arr, i, j) { - const tmp = arr[i] - arr[i] = arr[j] - arr[j] = tmp -} - - -// another - -/** - * @param {number[]} nums - * @return {number} - */ -const firstMissingPositive = function(nums) { - const n = nums.length - for(let i = 0; i < n; i++) { - while(nums[i] < n && nums[nums[i] - 1] !== nums[i]) { - swap(nums, i, nums[i] - 1) - } - } - - for(let i = 0; i < n; i++) { - if(nums[i] !== i + 1) return i + 1 - } - - return n + 1 -}; - -function swap(arr, i, j) { - const tmp = arr[i] - arr[i] = arr[j] - arr[j] = tmp -} - -// another - -/** - * @param {number[]} nums - * @return {number} - */ -const firstMissingPositive = function(nums) { - const n = nums.length - for(let i = 0; i < n; i++) { - while(nums[i] > 0 && nums[i] !== nums[nums[i] - 1] && nums[i] <= n) { - swap(i, nums[i] - 1) - } - } - - // console.log(nums) - for(let i = 0; i < n; i++) { - if(nums[i] !== i + 1) return i + 1 - } - - return n + 1 - - - function swap(i, j) { - const tmp = nums[j] - nums[j] = nums[i] - nums[i] = tmp - } -}; diff --git a/410-split-array-largest-sum.js b/410-split-array-largest-sum.js index a48f3f1e..98ed5f8b 100644 --- a/410-split-array-largest-sum.js +++ b/410-split-array-largest-sum.js @@ -100,42 +100,3 @@ This mapped this problem into the second sub-problem. Bool array B here is [5:fa */ -// another - -/** - * @param {number[]} nums - * @param {number} m - * @return {number} - */ -const splitArray = (nums, m) => { - let max = -Infinity, sum = 0 - for(let num of nums) { - sum += num - max = Math.max(max, num) - } - if (m === 1) return sum - let l = max, r = sum - while(l < r) { - let mid = l + ((r - l) >> 1) - if(valid(mid, nums, m)) { - r = mid - } else { - l = mid + 1 - } - } - return l -} - -function valid(target, nums, m) { - let cnt = 1, sum = 0 - for(let num of nums) { - sum += num - if(sum > target) { - cnt++ - sum = num - if(cnt > m) return false - } - } - - return true -} diff --git a/416-partition-equal-subset-sum.js b/416-partition-equal-subset-sum.js index c118c8e6..6aed0c9e 100644 --- a/416-partition-equal-subset-sum.js +++ b/416-partition-equal-subset-sum.js @@ -35,80 +35,3 @@ const canPartition = function(nums) { return dp[n][sum] }; -// another - -/** - * @param {number[]} nums - * @return {boolean} - */ -const canPartition = function(nums) { - if (nums.length < 2) return false - - const total = nums.reduce((a, c) => a + c) - if (total % 2 !== 0) return false - - nums.sort((a, b) => b - a) - const target = total / 2 - - if (nums[0] > target) return false - return findCombination(nums, target, 0) -} - -function findCombination(nums, target, start) { - if (target === 0) { - return true - } else { - for (let i = start; i < nums.length; i++) { - if (nums[i] <= target) { - if (findCombination(nums, target - nums[i], i + 1)) { - return true - } - } - } - return false - } -} - -// another - -/** - * @param {number[]} nums - * @return {boolean} - */ -function helper(nums, target, pos) { - for (let i = pos; i <= nums.length; i++) { - if (i != pos && nums[i] == nums[i - 1]) continue - if (nums[i] === target) return true - if (nums[i] > target) break - if (helper(nums, target - nums[i], i + 1)) return true - } - return false -} -const canPartition = function(nums) { - const sum = nums.reduce((sum, n) => (sum += n), 0) / 2 - if (sum % 1 != 0) { - return false - } - - return helper(nums, sum, 0) -} - -// another - -/** - * @param {number[]} nums - * @return {boolean} - */ -const canPartition = function (nums) { - const sumA = nums.reduce((acc, curr) => acc + curr, 0) - if (sumA % 2) return false - let row = 1n << BigInt(sumA / 2) - for (const weight of nums) row = row | (row >> BigInt(weight)) - /* - check the the column corresponding to my target by bitwise ANDing - it with just 1,so if the first bit is 1, - it will return true, otherwise false - */ - return row & 1n -} - diff --git a/42-trapping-rain-water.js b/42-trapping-rain-water.js index 1b31b415..73616a94 100644 --- a/42-trapping-rain-water.js +++ b/42-trapping-rain-water.js @@ -22,98 +22,3 @@ const trap = function(height) { return res }; -// another - -/** - * @param {number[]} height - * @return {number} - */ -const trap = function(height) { - const len = height.length - if (len === 0) return 0 - const leftMax = [height[0]] - const rightMax = [] - rightMax[len - 1] = height[len - 1] - for (let i = len - 2; i >= 0; i--) { - rightMax[i] = Math.max(height[i], rightMax[i + 1]) - } - let res = 0 - for (let i = 1; i < len; i++) { - leftMax[i] = Math.max(height[i], leftMax[i - 1]) - res += Math.min(leftMax[i], rightMax[i]) - height[i] - } - return res -} - -// another - -/** - * @param {number[]} height - * @return {number} - */ -const trap = function(height) { - const n = height.length - let l = 0, r = n - 1, res = 0, leftMax = 0, rightMax = 0 - while(l <= r) { - if(height[l] <= height[r]) { - if(height[l] >= leftMax) leftMax = height[l] - else res += leftMax - height[l] - l++ - } else { - if(height[r] >= rightMax) rightMax = height[r] - else res += rightMax - height[r] - r-- - } - } - return res -}; - -// another - -/** - * @param {number[]} height - * @return {number} - */ -const trap = function(height) { - const n = height.length - if(n === 0) return 0 - let res = 0 - let l = 0, r = n - 1, leftMax = height[l], rightMax = height[r] - while(l < r) { - if(height[l] <= height[r]) { - l++ - leftMax = Math.max(leftMax, height[l]) - res += (leftMax - height[l]) - } else { - r-- - rightMax = Math.max(rightMax, height[r]) - res += rightMax - height[r] - } - } - - return res -}; - -// another - -/** - * @param {number[]} height - * @return {number} - */ -const trap = function(height) { - const n = height.length, { max } = Math - let res = 0, l = 0, r = n - 1, leftMax = height[0], rightMax = height[n - 1] - while(l <= r) { - if(leftMax < rightMax) { - leftMax = max(leftMax, height[l]) - res += leftMax - height[l] - l++ - } else { - rightMax = max(rightMax, height[r]) - res += rightMax - height[r] - r-- - } - } - - return res -}; diff --git a/421-maximum-xor-of-two-numbers-in-an-array.js b/421-maximum-xor-of-two-numbers-in-an-array.js index 1e71476c..bbfd9b88 100644 --- a/421-maximum-xor-of-two-numbers-in-an-array.js +++ b/421-maximum-xor-of-two-numbers-in-an-array.js @@ -19,117 +19,3 @@ const findMaximumXOR = function(nums) { return res }; -// another - -/* - -This algorithm's idea is: -to iteratively determine what would be each bit of the final result from left to right. -And it narrows down the candidate group iteration by iteration. e.g. assume input are a,b,c,d,...z, 26 integers in total. -In first iteration, if you found that a, d, e, h, u differs on the MSB(most significant bit), -so you are sure your final result's MSB is set. Now in second iteration, -you try to see if among a, d, e, h, u there are at least two numbers make the 2nd MSB differs, -if yes, then definitely, the 2nd MSB will be set in the final result. -And maybe at this point the candidate group shinks from a,d,e,h,u to a, e, h. -Implicitly, every iteration, you are narrowing down the candidate group, -but you don't need to track how the group is shrinking, you only cares about the final result. - -*/ -/* - * @lc app=leetcode id=421 lang=javascript - * - * [421] Maximum XOR of Two Numbers in an Array - */ -/** - * @param {number[]} nums - * @return {number} - */ -const findMaximumXOR = function(nums) { - let maxResult = 0 - let mask = 0 - /*The maxResult is a record of the largest XOR we got so far. if it's 11100 at i = 2, it means - before we reach the last two bits, 11100 is the biggest XOR we have, and we're going to explore - whether we can get another two '1's and put them into maxResult - - This is a greedy part, since we're looking for the largest XOR, we start - from the very begining, aka, the 31st postition of bits. */ - for (let i = 31; i >= 0; i--) { - //The mask will grow like 100..000 , 110..000, 111..000, then 1111...111 - //for each iteration, we only care about the left parts - mask = mask | (1 << i) - - let set = new Set() - for (let num of nums) { - /* we only care about the left parts, for example, if i = 2, then we have - {1100, 1000, 0100, 0000} from {1110, 1011, 0111, 0010}*/ - let leftPartOfNum = num & mask - set.add(leftPartOfNum) - } - - // if i = 1 and before this iteration, the maxResult we have now is 1100, - // my wish is the maxResult will grow to 1110, so I will try to find a candidate - // which can give me the greedyTry; - let greedyTry = maxResult | (1 << i) - - for (let leftPartOfNum of set) { - //This is the most tricky part, coming from a fact that if a ^ b = c, then a ^ c = b; - // now we have the 'c', which is greedyTry, and we have the 'a', which is leftPartOfNum - // If we hope the formula a ^ b = c to be valid, then we need the b, - // and to get b, we need a ^ c, if a ^ c exisited in our set, then we're good to go - let anotherNum = leftPartOfNum ^ greedyTry - if (set.has(anotherNum)) { - maxResult = greedyTry - break - } - } - - // If unfortunately, we didn't get the greedyTry, we still have our max, - // So after this iteration, the max will stay at 1100. - } - return maxResult -} - -// another - -/** - * @param {number[]} nums - * @return {number} - */ -const findMaximumXOR = function (nums) { - if (nums == null || nums.length == 0) { - return 0 - } - const root = new Trie() - for (let num of nums) { - let curNode = root - for (let i = 31; i >= 0; i--) { - let curBit = (num >>> i) & 1 - if (curNode.children[curBit] == null) { - curNode.children[curBit] = new Trie() - } - curNode = curNode.children[curBit] - } - } - let max = Number.MIN_VALUE - for (let num of nums) { - let curNode = root - let curSum = 0 - for (let i = 31; i >= 0; i--) { - let curBit = (num >>> i) & 1 - if (curNode.children[curBit ^ 1] != null) { - curSum += 1 << i - curNode = curNode.children[curBit ^ 1] - } else { - curNode = curNode.children[curBit] - } - } - max = Math.max(curSum, max) - } - return max -} - -class Trie { - constructor() { - this.children = {} - } -} diff --git a/424-longest-repeating-character-replacement.js b/424-longest-repeating-character-replacement.js index 7a49780e..ba2c5331 100755 --- a/424-longest-repeating-character-replacement.js +++ b/424-longest-repeating-character-replacement.js @@ -25,32 +25,3 @@ console.log(characterReplacement("ABAB", 2)); console.log(characterReplacement("AABABBA", 1)); -// another - -/** - * @param {string} s - * @param {number} k - * @return {number} - */ -const characterReplacement = function (s, k) { - const freq = Array(26).fill(0), - n = s.length, - { max } = Math, - A = 'A'.charCodeAt(0) - let res = 0, - l = 0, - r = 0, - maxFreq = 0 - while (r < n) { - maxFreq = max(maxFreq, ++freq[s.charCodeAt(r) - A]) - if (r - l + 1 - maxFreq > k) { - freq[s.charCodeAt(l) - A]-- - l++ - } - res = max(res, r - l + 1) - r++ - } - - return res -} - diff --git a/425-word-squares.js b/425-word-squares.js index 66c6d9e8..42e9e8bc 100644 --- a/425-word-squares.js +++ b/425-word-squares.js @@ -42,78 +42,3 @@ const wordSquares = function(words) { return result } -// another - -/** - * @param {string[]} words - * @return {string[][]} - */ -const wordSquares = function(words) { - let result = [] - let trie = new Trie() - for (let word of words) { - trie.add(word) - } - - findWordSquare(result, [], trie) - return result -}; - -function findWordSquare (result, temp, trie) { - if (temp.length > 0 && temp.length === temp[0].length) { - result.push(temp) - return - } - - let prefix = '' - let j = temp.length - for (let i = 0; i < temp.length; i++) { - prefix += temp[i][j] - } - - let startWith = trie.startWith(prefix) - for (let word of startWith) { - findWordSquare(result, temp.concat([word]), trie) - } -} - -function Trie () { - this.isWord = false - this.children = new Map() -} - -Trie.prototype.add = function (word) { - let cur = this - for (let i = 0; i < word.length; i++) { - if (!cur.children.has(word[i])) { - cur.children.set(word[i], new Trie()) - } - cur = cur.children.get(word[i]) - } - cur.isWord = true -} - -Trie.prototype.startWith = function (prefix) { - let cur = this - for (let i = 0; i < prefix.length; i++) { - if (cur.children.has(prefix[i])) { - cur = cur.children.get(prefix[i]) - } else { - return [] - } - } - - let res = [] - const findWords = function (res, cur, str) { - if (!cur.isWord) { - for (let [key, val] of cur.children) { - findWords(res, val, str + key) - } - } else { - res.push(str) - } - } - - findWords(res, cur, prefix) - return res -} diff --git a/426-convert-binary-search-tree-to-sorted-doubly-linked-list.js b/426-convert-binary-search-tree-to-sorted-doubly-linked-list.js index a75ad8a8..da5f15d4 100644 --- a/426-convert-binary-search-tree-to-sorted-doubly-linked-list.js +++ b/426-convert-binary-search-tree-to-sorted-doubly-linked-list.js @@ -29,65 +29,3 @@ const treeToDoublyList = function(root) { return head; }; -// another - -/** - * @param {Node} root - * @return {Node} - */ -const treeToDoublyList = function(root) { - if(root === null) return null - const left = treeToDoublyList(root.left) - const right = treeToDoublyList(root.right) - root.left = root - root.right = root - return connect(connect(left, root), right) -}; - -// n1 is the head of circular DLL as well as n2 -function connect(n1, n2) { - if(n1 === null) return n2 - if(n2 === null) return n1 - const t1 = n1.left - const t2 = n2.left - - t1.right = n2 - n2.left = t1 - t2.right = n1 - n1.left = t2 - - return n1 -} - -// another - -/** - * @param {Node} root - * @return {Node} - */ -const treeToDoublyList = function(root) { - if(root == null) return null - let cur = root - let start = root - while(start.left !== null) { - start = start.left - } - let prev = null - const stack = [] - while(stack.length !== 0 || cur !== null) { - while(cur !== null) { - stack.push(cur) - cur = cur.left - } - cur = stack.pop() - if(prev !== null) { - prev.right = cur - cur.left = prev - } - prev = cur - cur = cur.right - } - start.left = prev - prev.right = start - return start -}; diff --git a/428-serialize-and-deserialize-n-ary-tree.js b/428-serialize-and-deserialize-n-ary-tree.js index edd89f55..097c15d5 100644 --- a/428-serialize-and-deserialize-n-ary-tree.js +++ b/428-serialize-and-deserialize-n-ary-tree.js @@ -59,66 +59,3 @@ class Codec { * }; */ -// another - -/** - * // Definition for a Node. - * function Node(val, children) { - * this.val = val; - * this.children = children; - * }; - */ - -class Codec { - constructor() {} - /** - * @param {Node} root - * @return {string} - */ - // Encodes a tree to a single string. - serialize = function(root) { - const ans = [] - const stack = root ? [root] : [] - while (stack.length) { - const cur = stack.pop() - ans.push(cur.val, cur.children.length) - for (let i = cur.children.length - 1; i >= 0; i--) { - stack.push(cur.children[i]) - } - } - return ans.join(',') - } - /** - * @param {string} data - * @return {Node} - */ - // Decodes your encoded data to tree. - deserialize = function(data) { - if (!data) return null - const arr = data.split(',') - const helper = (index = 0, parent) => { - const node = new Node(arr[index++], []) - parent.children.push(node) - let childCount = arr[index++] - while (childCount--) { - index = helper(index, node) - } - return index - } - const fakeRoot = new Node(null, []) - helper(0, fakeRoot) - return fakeRoot.children[0] - } -} -// Your Codec object will be instantiated and called as such: -// Codec codec = new Codec(); -// codec.deserialize(codec.serialize(root)); - -/** - * // Definition for a Node. - * function Node(val, children) { - * this.val = val; - * this.children = children; - * }; - */ - diff --git a/429-n-ary-tree-level-order-traversal.js b/429-n-ary-tree-level-order-traversal.js index 413cc641..cdcc16d7 100644 --- a/429-n-ary-tree-level-order-traversal.js +++ b/429-n-ary-tree-level-order-traversal.js @@ -25,33 +25,3 @@ function helper(node, index, res) { } } -// another - -/** - * // Definition for a Node. - * function Node(val,children) { - * this.val = val; - * this.children = children; - * }; - */ -/** - * @param {Node} root - * @return {number[][]} - */ -const levelOrder = function(root) { - const res = [] - if(root == null) return res - const q = [] - q.push(root) - while(q.length) { - const size = q.length - const cur = [] - for(let i = 0; i < size; i++) { - const node = q.shift() - cur.push(node.val) - q.push(...node.children) - } - res.push(cur) - } - return res -}; diff --git a/433-minimum-genetic-mutation.js b/433-minimum-genetic-mutation.js index 8c5ab37c..715aaef0 100644 --- a/433-minimum-genetic-mutation.js +++ b/433-minimum-genetic-mutation.js @@ -31,31 +31,3 @@ function dfs(s, e, bank, num, obj, visited) { } } -// another - -/** - * @param {string} start - * @param {string} end - * @param {string[]} bank - * @return {number} - */ -const minMutation = function(start, end, bank) { - const bankSet = new Set(bank) - if (!bankSet.has(end)) return -1 - const queue = [[start, 0]] - const dna = ['A', 'C', 'G', 'T'] - while (queue.length) { - let [node, count] = queue.shift() - if (node === end) return count - for (let i = 0; i < node.length; i++) { - for (let j = 0; j < dna.length; j++) { - const d = node.slice(0, i) + dna[j] + node.slice(i + 1) - if (bankSet.has(d)) { - queue.push([d, count + 1]) - bankSet.delete(d) - } - } - } - } - return -1 -} diff --git a/435-non-overlapping-intervals.js b/435-non-overlapping-intervals.js index 5b57a608..dd2f046e 100644 --- a/435-non-overlapping-intervals.js +++ b/435-non-overlapping-intervals.js @@ -17,29 +17,3 @@ const eraseOverlapIntervals = function(intervals) { return len - res }; -// another - -/** - * Definition for an interval. - * function Interval(start, end) { - * this.start = start; - * this.end = end; - * } - */ -/** - * @param {Interval[]} intervals - * @return {number} - */ -const eraseOverlapIntervals = function(intervals) { - intervals.sort((a, b) => a.end - b.end) - let count = 0 - let end = Number.MIN_SAFE_INTEGER - const len = intervals.length - for(let el of intervals) { - if(el.start >= end) { - end = el.end - count++ - } - } - return len - count -}; diff --git a/440-k-th-smallest-in-lexicographical-order.js b/440-k-th-smallest-in-lexicographical-order.js index 00bb38fb..ddb21dd5 100644 --- a/440-k-th-smallest-in-lexicographical-order.js +++ b/440-k-th-smallest-in-lexicographical-order.js @@ -31,35 +31,3 @@ const findKthNumber = function (n, k) { } } -// another - -/** - * @param {number} n - * @param {number} k - * @return {number} - */ -const findKthNumber = function (n, k) { - let curr = 1 - k = k - 1 - while (k > 0) { - let steps = calSteps(n, curr, curr + 1) - if (steps <= k) { - curr += 1 - k -= steps - } else { - curr *= 10 - k -= 1 - } - } - return curr - - function calSteps(n, n1, n2) { - let steps = 0 - while (n1 <= n) { - steps += Math.min(n + 1, n2) - n1 - n1 *= 10 - n2 *= 10 - } - return steps - } -} diff --git a/441-arranging-coins.js b/441-arranging-coins.js index 605801fe..b0c2bcb1 100644 --- a/441-arranging-coins.js +++ b/441-arranging-coins.js @@ -16,12 +16,3 @@ const arrangeCoins = function(n) { return num }; -// another - -/** - * @param {number} n - * @return {number} - */ -const arrangeCoins = function(n) { - return (-1 + Math.sqrt(1+4*2*n)) >> 1 -}; diff --git a/442-find-all-duplicates-in-an-array.js b/442-find-all-duplicates-in-an-array.js index 4d28baa9..329a1b4d 100755 --- a/442-find-all-duplicates-in-an-array.js +++ b/442-find-all-duplicates-in-an-array.js @@ -20,18 +20,3 @@ const findDuplicates = function(nums) { console.log(findDuplicates([4, 3, 2, 7, 8, 2, 3, 1])); console.log(findDuplicates([10, 2, 5, 10, 9, 1, 1, 4, 3, 7])); -// another - -/** - * @param {number[]} nums - * @return {number[]} - */ -const findDuplicates = function(nums) { - const res = [] - for(let i = 0, len = nums.length; i < len; i++) { - const idx = Math.abs(nums[i]) - 1 - if(nums[idx] < 0) res.push(idx + 1) - nums[idx] = -nums[idx] - } - return res; -}; diff --git a/444-sequence-reconstruction.js b/444-sequence-reconstruction.js index d8fde009..3ddce3f0 100644 --- a/444-sequence-reconstruction.js +++ b/444-sequence-reconstruction.js @@ -22,44 +22,3 @@ const sequenceReconstruction = function(org, seqs) { return true } -// another - -/** - * @param {number[]} org - * @param {number[][]} seqs - * @return {boolean} - */ -const sequenceReconstruction = function(org, seqs) { - const graph = new Map() - const indegree = new Map() - seqs.forEach(seq => { - for (let i = 0; i < seq.length; i++) { - if (!graph.has(seq[i])) graph.set(seq[i], []) - if (!indegree.has(seq[i])) indegree.set(seq[i], 0) - if (i > 0) { - graph.get(seq[i - 1]).push(seq[i]) - indegree.set(seq[i], indegree.get(seq[i]) + 1) - } - } - }) - if (org.length !== graph.size) return false - const array = [] - for (let [key, val] of indegree.entries()) { - if (val === 0) array.push(key) - } - let index = 0 - while (array.length > 0) { - if (array.length > 1) return false - const current = array.shift() - if (org[index] !== current) { - return false - } - index++ - graph.get(current).forEach(next => { - indegree.set(next, indegree.get(next) - 1) - if (indegree.get(next) === 0) array.push(next) - }) - } - return index === org.length -} - diff --git a/445-add-two-numbers-II.js b/445-add-two-numbers-II.js index 4800985a..852dd980 100755 --- a/445-add-two-numbers-II.js +++ b/445-add-two-numbers-II.js @@ -56,99 +56,3 @@ function reverse(head) { } -// another - -/** - * Definition for singly-linked list. - * function ListNode(val) { - * this.val = val; - * this.next = null; - * } - */ -/** - * @param {ListNode} l1 - * @param {ListNode} l2 - * @return {ListNode} - */ -const addTwoNumbers = function(l1, l2) { - const s1 = []; - const s2 = []; - while (l1 !== null) { - s1.push(l1.val); - l1 = l1.next; - } - while (l2 !== null) { - s2.push(l2.val); - l2 = l2.next; - } - - let list = new ListNode(0); - let sum = 0; - while (s1.length > 0 || s2.length > 0) { - if (s1.length > 0) { - sum += s1.pop(); - } - if (s2.length > 0) { - sum += s2.pop(); - } - list.val = sum % 10; - const head = new ListNode(Math.floor(sum / 10)); - head.next = list; - list = head; - sum = Math.floor(sum / 10); - } - - return list.val === 0 ? list.next : list; -}; - -// another - -/** - * Definition for singly-linked list. - * function ListNode(val, next) { - * this.val = (val===undefined ? 0 : val) - * this.next = (next===undefined ? null : next) - * } - */ -/** - * @param {ListNode} l1 - * @param {ListNode} l2 - * @return {ListNode} - */ -const addTwoNumbers = function(l1, l2) { - const s1 = [], s2 = [] - let h1 = l1, h2 = l2 - while(h1) { - s1.push(h1.val) - h1 = h1.next - } - while(h2) { - s2.push(h2.val) - h2 = h2.next - } - let inc = false - let tail = null - while(s1.length || s2.length) { - let tmp = 0 - if(s1.length) tmp += s1.pop() - if(s2.length) tmp += s2.pop() - if(inc) tmp++ - if(tmp > 9) { - inc = true - } else { - inc = false - } - tmp = tmp % 10 - const cur = new ListNode(tmp) - if(tail) cur.next = tail - tail = cur - } - - if(inc) { - const head = new ListNode(1) - head.next = tail - return head - } - return tail - -}; diff --git a/448-find-all-numbers-disappeared-in-an-array.js b/448-find-all-numbers-disappeared-in-an-array.js index bf3dd2da..a3fda0b0 100755 --- a/448-find-all-numbers-disappeared-in-an-array.js +++ b/448-find-all-numbers-disappeared-in-an-array.js @@ -16,20 +16,3 @@ const findDisappearedNumbers = function(nums) { return arr; }; -// another - -/** - * @param {number[]} nums - * @return {number[]} - */ -const findDisappearedNumbers = function(nums) { - for(let i = 0, len = nums.length; i < len; i++) { - const idx = Math.abs(nums[i]) - 1 - nums[idx] = - Math.abs(nums[idx]) - } - const res = [] - nums.forEach((e, i) => { - if(e > 0) res.push(i + 1) - }) - return res -}; diff --git a/450-delete-node-in-a-bst.js b/450-delete-node-in-a-bst.js index 51eee135..79d43b00 100644 --- a/450-delete-node-in-a-bst.js +++ b/450-delete-node-in-a-bst.js @@ -32,44 +32,3 @@ const deleteNode = function(root, key) { return root }; -// another - -/** - * Definition for a binary tree node. - * function TreeNode(val, left, right) { - * this.val = (val===undefined ? 0 : val) - * this.left = (left===undefined ? null : left) - * this.right = (right===undefined ? null : right) - * } - */ -/** - * @param {TreeNode} root - * @param {number} key - * @return {TreeNode} - */ -const deleteNode = function(root, key) { - if(root == null) return root - - if(root.val < key) root.right = deleteNode(root.right, key) - else if(root.val > key) root.left = deleteNode(root.left, key) - else { - if(root.left == null && root.right === null) root = null - else if(root.left == null) root = root.right - else if(root.right == null) root = root.left - else { - const min = findMin(root.right) - root.val = min.val - root.right = deleteNode(root.right, root.val) - } - } - - return root -}; - -function findMin(node) { - let cur = node - while(cur.left) { - cur = cur.left - } - return cur -} diff --git a/452-minimum-number-of-arrows-to-burst-balloons.js b/452-minimum-number-of-arrows-to-burst-balloons.js index c47b87fa..c7c3edf1 100644 --- a/452-minimum-number-of-arrows-to-burst-balloons.js +++ b/452-minimum-number-of-arrows-to-burst-balloons.js @@ -17,21 +17,3 @@ const findMinArrowShots = function(points) { return ans } -// another - -/** - * @param {number[][]} points - * @return {number} - */ -const findMinArrowShots = function(points) { - if(points == null || points.length === 0) return 0 - points.sort((a, b) => a[1] - b[1]) - let end = points[0][1], res = 1 - for(let i = 1, len = points.length; i < len; i++) { - if(points[i][0] > end) { - end = points[i][1] - res++ - } - } - return res -}; diff --git a/456-132-pattern.js b/456-132-pattern.js index e584d194..29f591b4 100644 --- a/456-132-pattern.js +++ b/456-132-pattern.js @@ -16,22 +16,3 @@ const find132pattern = function(nums) { return false } -// another - -/** - * @param {number[]} nums - * @return {boolean} - */ -const find132pattern = function(nums) { - let idx = nums.length - let s3 = Number.NEGATIVE_INFINITY - for(let len = nums.length, i = len - 1; i >= 0; i--) { - if(nums[i] < s3) return true - while(idx < nums.length && nums[i] > nums[idx]) { - s3 = nums[idx++] - } - nums[--idx] = nums[i] - } - return false -} - diff --git a/458-poor-pigs.js b/458-poor-pigs.js index da075dcc..0b1efffb 100644 --- a/458-poor-pigs.js +++ b/458-poor-pigs.js @@ -9,18 +9,3 @@ const poorPigs = function(buckets, minutesToDie, minutesToTest) { return Math.ceil(Math.log(buckets) / Math.log(index)) } -// another - -/** - * @param {number} buckets - * @param {number} minutesToDie - * @param {number} minutesToTest - * @return {number} - */ -const poorPigs = function(buckets, minutesToDie, minutesToTest) { - let pigs = 0 - while ((minutesToTest / minutesToDie + 1) ** pigs < buckets) { - pigs++ - } - return pigs -} diff --git a/459-repeated-substring-pattern.js b/459-repeated-substring-pattern.js index 9debc498..07b13580 100644 --- a/459-repeated-substring-pattern.js +++ b/459-repeated-substring-pattern.js @@ -28,78 +28,3 @@ const repeatedSubstringPattern = function(s) { } }; -// another - -/** - * @param {string} s - * @return {boolean} - */ -const repeatedSubstringPattern = function(s) { - const len = s.length - let tmp = '' - for(let i = 1; i <= len; i++) { - tmp = s.substr(0, i) - if (tmp.length === len) { - return false - } - if (s === genStr(tmp, len)) { - return true - } - } - return false -}; -function genStr(sub, limit) { - let str = sub - while(str.length < limit) { - str += sub - } - return str -} - -// another - -/** - * @param {string} s - * @return {boolean} - */ -const repeatedSubstringPattern = function (s) { - const l = s.length - const arr = DFA(s) - return arr[l] && arr[l] % (l - arr[l]) === 0 - function DFA(s) { - let i = 1 - let j = 0 - const len = s.length - const prefix = Array(len + 1).fill(0) - prefix[0] = -1 - prefix[1] = 0 - while (i < len) { - if (s[j] === s[i]) { - j++ - i++ - prefix[i] = j - } else { - if (j > 0) j = prefix[j] - else i++ - } - } - return prefix - } -} - -// another - -/** - * @param {string} s - * @return {boolean} - */ -const repeatedSubstringPattern = function(s) { - let i = 1, j = 0, n = s.length; - const dp = Array(n + 1).fill(0); - while( i < s.length ){ - if( s[i] === s[j] ) dp[++i] = ++j; - else if( j === 0 ) i++; - else j = dp[j]; - } - return dp[n] && (dp[n] % (n - dp[n]) === 0); -}; diff --git a/46-permutations.js b/46-permutations.js index 82faf2f7..eece119b 100755 --- a/46-permutations.js +++ b/46-permutations.js @@ -22,27 +22,3 @@ function backtrack(list, tempList, nums) { } } -// another - -/** - * @param {number[]} nums - * @return {number[][]} - */ -const permute = function(nums) { - const res = [] - bt(nums, 0, [], res) - return res -}; - -function bt(nums, idx, cur, res) { - if(idx === nums.length) { - res.push(cur.slice()) - return - } - for(let i = 0; i < nums.length; i++) { - if(cur.indexOf(nums[i]) !== -1) continue - cur.push(nums[i]) - bt(nums, idx + 1, cur, res) - cur.pop() - } -} diff --git a/461-hamming-distance.js b/461-hamming-distance.js index 8174de3f..5c386160 100644 --- a/461-hamming-distance.js +++ b/461-hamming-distance.js @@ -13,31 +13,3 @@ const hammingDistance = function (x, y) { return d } -// another - -/** - * @param {number} x - * @param {number} y - * @return {number} - */ -const hammingDistance = function (x, y) { - let n = x ^ y - n = n - ((n >> 1) & 0x55555555) - n = (n & 0x33333333) + ((n >> 2) & 0x33333333) - return (((n + (n >> 4)) & 0xf0f0f0f) * 0x1010101) >> 24 -} - -// another - -/** - * @param {number} x - * @param {number} y - * @return {number} - */ -const hammingDistance = function (x, y) { - let n = x ^ y - let tmp = n - ((n >> 1) & 033333333333) - ((n >> 2) & 011111111111); - return ((tmp + (tmp >> 3)) & 030707070707) % 63; -} - -// https://tech.liuchao.me/2016/11/count-bits-of-integer/ diff --git a/463-island-perimeter.js b/463-island-perimeter.js index db888ecd..46c1d19e 100755 --- a/463-island-perimeter.js +++ b/463-island-perimeter.js @@ -39,32 +39,3 @@ console.log( islandPerimeter([[0, 1, 0, 0], [1, 1, 1, 0], [0, 1, 0, 0], [1, 1, 0, 0]]) ); -// another - -/** - * @param {number[][]} grid - * @return {number} - */ -const islandPerimeter = function(grid) { - const m = grid.length - const n = grid[0].length - const dirs = [[0, 1], [0, -1], [1, 0], [-1, 0]] - let r = 0 - for(let i = 0; i < m; i++) { - for(let j = 0; j < n; j++) { - if(grid[i][j] === 1) r += h(i, j) - } - } - - return r - - function h(i, j) { - let res = 0 - for(let d of dirs) { - const nr = i + d[0] - const nc = j + d[1] - if(nr < 0 || nc < 0 || nr >= m || nc >= n || grid[nr][nc] === 0) res++ - } - return res - } -}; diff --git a/468-validate-ip-address.js b/468-validate-ip-address.js index d421c6c6..08be3f77 100644 --- a/468-validate-ip-address.js +++ b/468-validate-ip-address.js @@ -30,49 +30,3 @@ const validIPv6 = function (IP) { return true } -// another - -/** - * @param {string} IP - * @return {string} - */ -const validIPAddress = function(IP) { - const ipv4 = /^((\d|[1-9]\d|1\d\d|2([0-4]\d|5[0-5]))\.){4}$/ - const ipv6 = /^([\da-f]{1,4}:){8}$/i - return ipv4.test(IP + '.') ? 'IPv4' : ipv6.test(IP + ':') ? 'IPv6' : 'Neither' -} - -// another - -/** - * @param {string} IP - * @return {string} - */ -const validIPAddress = function(IP) { - if (IP.indexOf('.') != -1) { - const arr = IP.split('.') - if (arr.length !== 4) return 'Neither' - for (let i = 0; i < arr.length; i++) { - const numVal = parseInt(arr[i]) - if ( - numVal < 0 || - numVal >= 256 || - arr[i].length !== ('' + numVal).length - ) { - return 'Neither' - } - } - return 'IPv4' - } else if (IP.indexOf(':') != -1) { - const arr = IP.split(':') - if (arr.length !== 8) return 'Neither' - for (let i = 0; i < arr.length; i++) { - if (arr[i].length > 4 || arr[i].length === 0) return 'Neither' - const re = /[^0-9A-F]/i - if (re.test(arr[i])) return 'Neither' - } - return 'IPv6' - } else { - return 'Neither' - } -} diff --git a/47-permutations-ii.js b/47-permutations-ii.js index 02337a11..00b0a948 100644 --- a/47-permutations-ii.js +++ b/47-permutations-ii.js @@ -31,34 +31,3 @@ function permuteUniqueHelper(m, l, p, i, r) { } -// another - -/** - * @param {number[]} nums - * @return {number[][]} - */ -const permuteUnique = function(nums) { - const set = new Set() - const used = new Set() - bt(nums, 0, [], used, set) - const res = [] - for(let item of set) { - res.push(item.split(',')) - } - return res -}; - -function bt(nums, i, cur, used, set) { - if(i === nums.length) { - set.add(cur.slice().join(',')) - return - } - for(let idx = 0; idx < nums.length; idx++) { - if(used.has(idx)) continue - cur.push(nums[idx]) - used.add(idx) - bt(nums, i + 1, cur, used, set) - used.delete(idx) - cur.pop() - } -} diff --git a/470-implement-rand10-using-rand7.js b/470-implement-rand10-using-rand7.js index eef7543c..7f192fd5 100644 --- a/470-implement-rand10-using-rand7.js +++ b/470-implement-rand10-using-rand7.js @@ -11,16 +11,3 @@ const rand10 = function() { return (result % 10) + 1 } -// another - -/** - * The rand7() API is already defined for you. - * var rand7 = function() {} - * @return {number} a random integer in the range 1 to 7 - */ -const rand10 = function() { - let tmp = 40 - while(tmp >= 40) tmp = 7 * (rand7() - 1) + (rand7() - 1) - - return tmp % 10 + 1 -}; diff --git a/472-concatenated-words.js b/472-concatenated-words.js index 096a4693..3f75055f 100644 --- a/472-concatenated-words.js +++ b/472-concatenated-words.js @@ -37,107 +37,3 @@ const findAllConcatenatedWordsInADict = function (words) { -// another - -/** - * @param {string[]} words - * @return {string[]} - */ - -const findAllConcatenatedWordsInADict = function (words) { - const set = new Set(words) - const res = [] - const map = new Map() - for (let w of words) { - if (w.length < 2) continue - if (dfs(w, set, map, 0)) res.push(w) - } - return res - - function dfs(word, set, map, pos) { - if (pos > 0 && map.get(word)) return map.get(word) - if (pos > 0 && set.has(word)) { - map.set(word, true) - return map.get(word) - } - for (let i = 1; i < word.length; i++) { - const left = word.slice(0, i) - const right = word.slice(i) - if (set.has(right) && dfs(left, set, map, pos + 1)) { - map.set(word, true) - return map.get(word) - } - } - - map.set(word, false) - return false - } -} - -// another - -/** - * @param {string[]} words - * @return {string[]} - */ - -const findAllConcatenatedWordsInADict = function (words) { - const set = new Set(words) - const res = [] - const map = new Map() - - for(let word of words) { - if(dfs(word, 0)) res.push(word) - } - return res - function dfs(word, idx) { - if(map.has(word)) return map.get(word) - if(idx > 0 && set.has(word)) return true - let tmp = false - for(let i = 1; i < word.length; i++) { - const prefix = word.slice(0, i), suffix = word.slice(i) - if(set.has(prefix) && set.has(suffix)) { - tmp = true - break - } - if(set.has(prefix) && dfs(suffix, idx + 1)) { - tmp = true - break - } - } - - map.set(word, tmp) - return tmp - } -} - - -// another - -/** - * @param {string[]} words - * @return {string[]} - */ -const findAllConcatenatedWordsInADict = function(words) { - let res = [] - if (words === null || words.length == 0) return res - let set = new Set(words) - for (let word of words) { - set.delete(word) - if (dfs(word, set, '')) res.push(word) - set.add(word) - } - return res -} - -function dfs(word, set, prev) { - if (prev != '') set.add(prev) - if (set.has(word)) return true - for (let i = 1; i <= word.length; i++) { - const prefix = word.substring(0, i) - if (set.has(prefix) && dfs(word.substring(i), set, prev + prefix)) { - return true - } - } - return false -} diff --git a/473-matchsticks-to-square.js b/473-matchsticks-to-square.js index 101edd1d..78a14e64 100644 --- a/473-matchsticks-to-square.js +++ b/473-matchsticks-to-square.js @@ -26,36 +26,3 @@ function dfs(nums, arr, idx, target) { -// another - -/** - * @param {number[]} nums - * @return {boolean} - */ -const makesquare = function(nums) { - if (nums.length == 0) return false - const edge = nums.reduce((accum, val) => accum + val) / 4 - nums.sort((val1, val2) => val2 - val1) - if (edge !== Math.floor(edge)) return false - const findEdge = function(target) { - if (target <= 0) return target === 0 - let newNums = [] - while (nums.length) { - let item = nums.shift() - if (findEdge(target - item)) { - nums = newNums.concat(nums) - return true - } - newNums.push(item) - } - nums = newNums - return false - } - let count = 4 - while (count) { - if (!findEdge(edge)) return false - count-- - } - return true -} - diff --git a/475-heaters.js b/475-heaters.js index 316c0f51..54320dbc 100644 --- a/475-heaters.js +++ b/475-heaters.js @@ -25,46 +25,3 @@ const findMinDistance = (house, heaters) => { if (left === heaters.length) return house - heaters[heaters.length - 1] } -// another - -/** - * @param {number[]} houses - * @param {number[]} heaters - * @return {number} - */ -const findRadius = function(houses, heaters) { - let res = 0 - let k = 0 - houses = houses.sort((a, b) => a - b) - heaters = heaters.sort((a, b) => a - b) - for (let i = 0; i < houses.length; i++) { - const curr = houses[i] - while ( - k < heaters.length && - Math.abs(heaters[k + 1] - curr) <= Math.abs(heaters[k] - curr) - ) { - k++ - } - res = Math.max(res, Math.abs(heaters[k] - curr)) - } - return res -} - -// another - -/** - * @param {number[]} houses - * @param {number[]} heaters - * @return {number} - */ -const findRadius = function(houses, heaters) { - heaters.sort((a, b) => a - b) - houses.sort((a, b) => a - b) - let res = 0, i = 0 - for(let h of houses) { - while(i < heaters.length - 1 && heaters[i] + heaters[i + 1] <= h * 2) i++ - res = Math.max(res, Math.abs(heaters[i] - h)) - } - return res -} - diff --git a/479-largest-palindrome-product.js b/479-largest-palindrome-product.js index 429c8aa4..e264495f 100644 --- a/479-largest-palindrome-product.js +++ b/479-largest-palindrome-product.js @@ -21,61 +21,3 @@ function largestPalindrome(n) { } } -// another - -/** - * @param {number} n - * @return {number} - */ -const largestPalindrome = function (n) { - if (n === 1) return 9 - for (let i = 2, limit = 9 * 10 ** (n - 1); i < limit; i++) { - let left = 10 ** n - i - let right = +('' + left).split('').reverse().join('') - if (i ** 2 - 4 * right < 0) continue - const tmp = (i ** 2 - 4 * right) ** 0.5 - if (tmp === Math.floor(tmp)) { - return ( - (BigInt(right) + 10n ** BigInt(n) * (10n ** BigInt(n) - BigInt(i))) % - 1337n - ) - } - } -} - -// another - -/** - * @param {number} n - * @return {number} - */ -const largestPalindrome = function(n) { - if (n === 1) { - return 9 - } else if (n === 8) { - return 475 - } - let max = Math.pow(10, n) - let min = Math.pow(10, n - 1) - let ret = 0 - - for (let i = max - 1; i > 0; i--) { - ret = i * max + getReverse(i) - for (let factor = ~~Math.sqrt(ret); factor < max; factor++) { - if (ret % factor == 0 && ret / factor < max) { - return ret % 1337 - } - } - } - return -1 -} - -function getReverse(n) { - let result = 0 - let num = n - while (num > 0) { - result = result * 10 + (num % 10) - num = ~~(num / 10) - } - return result -} diff --git a/48-rotate-image.js b/48-rotate-image.js index 83583d57..2028d5a0 100644 --- a/48-rotate-image.js +++ b/48-rotate-image.js @@ -22,45 +22,3 @@ const rotate = function(matrix) { } } -// another - -/** - * @param {number[][]} matrix - * @return {void} Do not return anything, modify matrix in-place instead. - */ -const rotate = function (matrix) { - matrix.reverse() - for (let i = 0; i < matrix.length; ++i) { - for (let j = i + 1; j < matrix[i].length; ++j) swap(matrix, i, j) - } -} - -function swap(matrix, i, j) { - const tmp = matrix[j][i] - matrix[j][i] = matrix[i][j] - matrix[i][j] = tmp -} - -// another - -/** - * @param {number[][]} matrix - * @return {void} Do not return anything, modify matrix in-place instead. - */ -const rotate = function (matrix) { - matrix.reverse() - for (let i = 0; i < matrix.length; ++i) { - for (let j = matrix[i].length - 1; j > i; j--) swap(matrix, i, j) - } -} - -function swap(matrix, i, j) { - const tmp = matrix[j][i] - matrix[j][i] = matrix[i][j] - matrix[i][j] = tmp -} -/* -1 2 3 7 8 9 7 4 1 -4 5 6 ---> 4 5 6 --->8 5 2 -7 8 9 1 2 3 9 6 3 -*/ diff --git a/480-sliding-window-median.js b/480-sliding-window-median.js index a57beed9..1b13eeb3 100644 --- a/480-sliding-window-median.js +++ b/480-sliding-window-median.js @@ -32,45 +32,3 @@ const medianSlidingWindow = function(nums, k) { return medians } -// another - -const medianSlidingWindow = function(nums, k) { - let pq = [] - for (let i = 0; i < k; i++) { - insert(nums[i]) - } - let res = [] - res.push(findMid()) - for (let i = k; i < nums.length; i++) { - remove(nums[i - k]) - insert(nums[i]) - res.push(findMid()) - } - return res - function findMid() { - let mid = (pq.length - 1) / 2 - return (pq[Math.ceil(mid)] + pq[Math.floor(mid)]) / 2 - } - function insert(n) { - if (pq.length === 0 || pq[pq.length - 1] <= n) { - pq.push(n) - } else { - let idx = bsEnd(pq, n) - pq.splice(idx, 0, n) - } - } - function bsEnd(arr, n) { - let lo = 0, - hi = arr.length - 1 - while (lo < hi) { - let mid = Math.floor((lo + hi) / 2) - if (arr[mid] < n) lo = mid + 1 - else hi = mid - } - return hi - } - function remove(n) { - let idx = bsEnd(pq, n) - pq.splice(idx, 1) - } -} diff --git a/484-find-permutation.js b/484-find-permutation.js index 18d3d441..9168041e 100644 --- a/484-find-permutation.js +++ b/484-find-permutation.js @@ -18,55 +18,3 @@ const findPermutation = function(s) { return res; }; -// another - -/** - * @param {string} s - * @return {number[]} - */ -const findPermutation = function(s) { - const n = s.length - const res = Array(n) - res[n] = n + 1 - for(let i = 0; i < n;) { - let j = i - while(j < n && s[j] === 'D') j++ - // console.log(j) - for(let k = j - i + 1; k > 0; k--) { - res[i] = j + 1 - i++ - j-- - } - } - - return res -}; - - -// another - -/** - * @param {string} s - * @return {number[]} - */ -const findPermutation = function(s) { - const n = s.length - const arr = Array.from({ length: n + 1 }, (el, idx) => idx + 1) - for(let h = 0; h < n; h++) { - if(s.charAt(h) === 'D') { - const l = h - while(h < n && s.charAt(h) === 'D') h++ - reverse(arr, l, h) - } - } - return arr -}; - -function reverse(arr, l, h) { - while(l < h) { - arr[l] ^= arr[h] - arr[h] ^= arr[l] - arr[l] ^= arr[h] - l++, h-- - } -} diff --git a/489-robot-room-cleaner.js b/489-robot-room-cleaner.js index b1c55cef..3b444d48 100644 --- a/489-robot-room-cleaner.js +++ b/489-robot-room-cleaner.js @@ -64,38 +64,3 @@ const cleanRoom = function(robot) { } } -// another - -/** - * @param {Robot} robot - * @return {void} - */ -const cleanRoom = function(robot) { - const dirs = [ - [-1, 0], - [0, 1], - [1, 0], - [0, -1] - ] - const visited = new Set() - clean(0, 0, 0) - function clean( x, y, curDirection) { - robot.clean() - visited.add(`${x},${y}`) - for(let i = curDirection; i < curDirection + 4; i++) { - const nx = dirs[i % 4][0] + x - const ny = dirs[i % 4][1] + y - if(!visited.has(`${nx},${ny}`) && robot.move()) { - clean(nx, ny, i % 4) - } - robot.turnRight() - } - robot.turnRight() - robot.turnRight() - robot.move() - robot.turnRight() - robot.turnRight() - - } -}; - diff --git a/49-group-anagrams.js b/49-group-anagrams.js index dfffbb2f..ecb486dd 100755 --- a/49-group-anagrams.js +++ b/49-group-anagrams.js @@ -25,28 +25,3 @@ const hash = (term) => { return arr.join('-') } -// another - -/** - * @param {string[]} strs - * @return {string[][]} - */ -const groupAnagrams = function(strs) { - if (strs.length === 0) { - return []; - } - const ans = []; - const hash = {}; - for (let el of strs) { - let sel = el - .split("") - .sort() - .join(""); - if (hash.hasOwnProperty(sel)) { - hash[sel].push(el); - } else { - hash[sel] = [el]; - } - } - return Object.values(hash); -}; diff --git a/490-the-maze.js b/490-the-maze.js index a19f6cb2..8fbbce6e 100644 --- a/490-the-maze.js +++ b/490-the-maze.js @@ -91,125 +91,3 @@ const hasPath = function(maze, start, destination) { return false } -// another - -/** - * @param {number[][]} maze - * @param {number[]} start - * @param {number[]} destination - * @return {boolean} - */ -const hasPath = function(maze, start, destination) { - const visited = Array.from({ length: maze.length }, () => - new Array(maze[0].length).fill(false) - ) - const dirs = [ - [-1, 0], - [0, -1], - [0, 1], - [1, 0] - ] - return dfs(maze, start, destination, visited, dirs) -} - -function dfs(maze, start, destination, visited, dirs) { - if (visited[start[0]][start[1]]) return false - if (start[0] === destination[0] && start[1] === destination[1]) return true - visited[start[0]][start[1]] = true - for (let i = 0; i < dirs.length; i++) { - const d = dirs[i] - let row = start[0] - let col = start[1] - while (isValid(maze, row + d[0], col + d[1])) { - row += d[0] - col += d[1] - } - if (dfs(maze, [row, col], destination, visited, dirs)) return true - } - return false -} - -function isValid(maze, row, col) { - return ( - row >= 0 && - row < maze.length && - col >= 0 && - col < maze[0].length && - maze[row][col] !== 1 - ) -} - -// another - -/** - * @param {number[][]} maze - * @param {number[]} start - * @param {number[]} destination - * @return {boolean} - */ -const hasPath = function(maze, start, destination) { - const m = maze.length, n = maze[0].length - const dirs = [[1, 0], [-1, 0], [0, 1], [0, -1]] - const visited = new Set() - let res = false - dfs(start[0], start[1]) - return res - - function dfs(i, j) { - if(i < 0 || i >= m || j < 0 || j >= n || maze[i][j] === 1 || visited.has(`${i},${j}`)) return - if(i === destination[0] && j === destination[1]) { - res = true - return - } - visited.add(`${i},${j}`) - const ib = i, jb = j - for(const [dx, dy] of dirs) { - let ii = i, jj = j - while( - ii + dx >= 0 && - ii + dx < m && - jj + dy >= 0 && - jj + dy < n && - maze[ii + dx][jj + dy] === 0 - ) { - ii += dx - jj += dy - } - dfs(ii, jj) - } - } -}; - -// another - -/** - * @param {number[][]} maze - * @param {number[]} start - * @param {number[]} destination - * @return {boolean} - */ -const hasPath = function(maze, start, destination) { - const m = maze.length, n = maze[0].length - const dirs = [[1, 0], [-1, 0], [0, 1], [0, -1]] - const visited = new Set() - const q = [start] - while(q.length) { - const [i, j] = q.pop() - if(i === destination[0] && j === destination[1]) return true - visited.add(`${i},${j}`) - for(const [dx, dy] of dirs) { - let ni = i, nj = j - while(valid(ni + dx, nj + dy)) { - ni += dx - nj += dy - } - if(!visited.has(`${ni},${nj}`)) q.push([ni, nj]) - } - } - - return false - - function valid(i, j) { - return i >= 0 && i < m && j >= 0 && j < n && maze[i][j] === 0 - } -}; diff --git a/493-reverse-pairs.js b/493-reverse-pairs.js index 1ad26aa2..4d300fc3 100644 --- a/493-reverse-pairs.js +++ b/493-reverse-pairs.js @@ -45,71 +45,3 @@ function merge(a, temp, left, mid, right){ return count; } -// another - -/** - * @param {number[]} nums - * @return {number} - */ -const reversePairs = function(nums) { - return mergeSort(nums, 0, nums.length - 1); -}; - -function mergeSort(nums, s, e) { - if (s >= e) return 0; - let mid = s + Math.floor((e - s) / 2); - let cnt = mergeSort(nums, s, mid) + mergeSort(nums, mid + 1, e); - for (let i = s, j = mid + 1; i <= mid; i++) { - while (j <= e && nums[i] / 2.0 > nums[j]) j++; - cnt += j - (mid + 1); - } - sortSubArr(nums, s, e + 1); - return cnt; -} - -function sortSubArr(arr, s, e) { - const tmp = arr.slice(s, e); - tmp.sort((a, b) => a - b); - arr.splice(s, e - s, ...tmp); -} - -// another - -/** - * @param {number[]} nums - * @return {number} - */ -function merge(A, start, mid, end) { - let n1 = mid - start + 1 - let n2 = end - mid - const L = new Array(n1).fill(0) - const R = new Array(n2).fill(0) - - for (let i = 0; i < n1; i++) L[i] = A[start + i] - for (let j = 0; j < n2; j++) R[j] = A[mid + 1 + j] - let i = 0, - j = 0 - for (let k = start; k <= end; k++) { - if (j >= n2 || (i < n1 && L[i] <= R[j])) A[k] = L[i++] - else A[k] = R[j++] - } -} - -function mergesort_and_count(A, start, end) { - if (start < end) { - let mid = start + ((end - start) >> 1) - let count = - mergesort_and_count(A, start, mid) + mergesort_and_count(A, mid + 1, end) - let j = mid + 1 - for (let i = start; i <= mid; i++) { - while (j <= end && A[i] > A[j] * 2) j++ - count += j - (mid + 1) - } - merge(A, start, mid, end) - return count - } else return 0 -} - -function reversePairs(nums) { - return mergesort_and_count(nums, 0, nums.length - 1) -} diff --git a/494-target-sum.js b/494-target-sum.js index 86e36c48..1916ee6b 100755 --- a/494-target-sum.js +++ b/494-target-sum.js @@ -24,23 +24,3 @@ const findTargetSumWays = function(nums, target) { -// another - -/** - * @param {number[]} nums - * @param {number} S - * @return {number} - */ -const findTargetSumWays = function(nums, s) { - const sum = nums.reduce((p, n) => p + n, 0); - return sum < s || (s + sum) % 2 > 0 ? 0 : subsetSum(nums, (s + sum) >>> 1); -}; - -function subsetSum(nums, s) { - const dp = Array(s + 1).fill(0); - dp[0] = 1; - for (let n of nums) { - for (let i = s; i >= n; i--) dp[i] += dp[i - n]; - } - return dp[s]; -} diff --git a/496-next-greater-element-I.js b/496-next-greater-element-I.js index e3b281bb..c7bf4894 100755 --- a/496-next-greater-element-I.js +++ b/496-next-greater-element-I.js @@ -28,30 +28,3 @@ const nextGreaterElement = function(nums1, nums2) { return res }; -// another - -/** - * @param {number[]} findNums - * @param {number[]} nums - * @return {number[]} - */ -const nextGreaterElement = function(findNums, nums) { - const map = {}; - const stack = []; - for (let num of nums) { - while (stack.length && stack[stack.length - 1] < num) { - let tmp = stack.pop(); - map[tmp] = num; - } - stack.push(num); - } - for (let i = 0; i < findNums.length; i++) { - findNums[i] = map[findNums[i]] == null ? -1 : map[findNums[i]]; - } - - return findNums; -}; - -console.log(nextGreaterElement([4, 1, 2], [1, 3, 4, 2])); -console.log(nextGreaterElement([2, 4], [1, 2, 3, 4])); -console.log(nextGreaterElement([1, 2, 3], [9, 8, 7, 3, 2, 1, 6])); diff --git a/497-random-point-in-non-overlapping-rectangles.js b/497-random-point-in-non-overlapping-rectangles.js index 1dc372ac..c38e1a0f 100644 --- a/497-random-point-in-non-overlapping-rectangles.js +++ b/497-random-point-in-non-overlapping-rectangles.js @@ -33,46 +33,3 @@ Solution.prototype.pick = function() { * var param_1 = obj.pick() */ -// another - -/** - * @param {number[][]} rects - */ -const Solution = function(rects) { - const xywhs = [] - const acc_sums = [0] - let sum = 0 - for (const [x, y, x2, y2] of rects) { - const w = x2 - x + 1 - const h = y2 - y + 1 - xywhs.push({ x, y, w, h }) - sum += w * h - acc_sums.push(sum) - } - this.xywhs = xywhs - this.acc_sums = acc_sums -} - -/** - * @return {number[]} - */ -Solution.prototype.pick = function() { - const picked = Math.floor( - Math.random() * this.acc_sums[this.acc_sums.length - 1] - ) - let i = 0 - for (; i < this.acc_sums.length - 2; i++) { - if (picked >= this.acc_sums[i] && picked < this.acc_sums[i + 1]) { - break - } - } - const { x, y, w, h } = this.xywhs[i] - return [x + Math.floor(Math.random() * w), y + Math.floor(Math.random() * h)] -} - -/** - * Your Solution object will be instantiated and called as such: - * var obj = new Solution(rects) - * var param_1 = obj.pick() - */ - diff --git a/498-diagonal-traverse.js b/498-diagonal-traverse.js index 214b065b..8ed30159 100644 --- a/498-diagonal-traverse.js +++ b/498-diagonal-traverse.js @@ -37,45 +37,3 @@ function isValid(i, j, m, n) { return true } -// another - -/** - * @param {number[][]} matrix - * @return {number[]} - */ -const findDiagonalOrder = function(matrix) { - if (matrix.length == 0) return [] - let r = 0, - c = 0, - m = matrix.length, - n = matrix[0].length, - arr = new Array(m * n) - for (let i = 0; i < arr.length; i++) { - arr[i] = matrix[r][c] - if ((r + c) % 2 === 0) { - // moving up - if (c === n - 1) { - r++ - } else if (r === 0) { - c++ - } else { - r-- - c++ - } - } else { - // moving down - if (r === m - 1) { - c++ - } else if (c === 0) { - r++ - } else { - r++ - c-- - } - } - } - return arr -} - - - diff --git a/499-the-maze-iii.js b/499-the-maze-iii.js index f7543ea1..d259751f 100644 --- a/499-the-maze-iii.js +++ b/499-the-maze-iii.js @@ -113,118 +113,3 @@ function findShortestWay(maze, ball, hole) { } } -// another - -const dirs = [ - [1, 0, 'd'], - [0, -1, 'l'], - [0, 1, 'r'], - [-1, 0, 'u'] -] - -/** - * @param {number[][]} maze - * @param {number[]} ball - * @param {number[]} hole - * @return {string} - */ -const findShortestWay = function(maze, ball, hole) { - const m = maze.length - const n = maze[0].length - const dist = [...new Array(m)].map(() => new Array(n).fill(Infinity)) - dist[ball[0]][ball[1]] = 0 - const pq = new PriorityQueue({ - comparator: (a, b) => { - if (dist[a[0][0]][a[0][1]] !== dist[b[0][0]][b[0][1]]) { - return dist[a[0][0]][a[0][1]] < dist[b[0][0]][b[0][1]] - } - return a[1] < b[1] - } - }) - pq.enqueue([ball, '']) - while (pq.length) { - const [[x, y], path] = pq.dequeue() - if (x === hole[0] && y === hole[1]) { - return path - } - for (const [di, dj, dir] of dirs) { - if (isValidPosition(x + di, y + dj, m, n) && maze[x + di][y + dj] === 0) { - const [i, j] = walk(maze, x + di, y + dj, di, dj, m, n, hole) - const deltaDist = Math.abs(x - i) + Math.abs(y - j) - if (dist[x][y] + deltaDist <= dist[i][j]) { - dist[i][j] = dist[x][y] + deltaDist - pq.enqueue([[i, j], path + dir]) - } - } - } - } - return 'impossible' -} - -function walk(maze, x, y, di, dj, m, n, [hi, hj]) { - let i = x - let j = y - while ( - isValidPosition(i + di, j + dj, m, n) && - maze[i + di][j + dj] === 0 && - !(hi === i && hj === j) - ) { - i += di - j += dj - } - return [i, j] -} - -function isValidPosition(i, j, m, n) { - if (i < 0 || i >= m || j < 0 || j >= n) { - return false - } - return true -} - -class PriorityQueue { - constructor({ comparator }) { - this.comparator = comparator - this.arr = [] - } - - enqueue(element) { - this.arr.push(element) - moveUp(this.arr, this.arr.length - 1, this.comparator) - } - - dequeue() { - const output = this.arr[0] - this.arr[0] = this.arr[this.arr.length - 1] - this.arr.pop() - moveDown(this.arr, 0, this.comparator) - return output - } - - get length() { - return this.arr.length - } -} - -function moveUp(arr, i, comparator) { - const p = Math.floor((i - 1) / 2) - const isValid = p < 0 || comparator(arr[p], arr[i]) - if (!isValid) { - ;[arr[i], arr[p]] = [arr[p], arr[i]] - moveUp(arr, p, comparator) - } -} - -function moveDown(arr, i, comparator) { - const left = 2 * i + 1 - const right = 2 * i + 2 - const isValid = - (left >= arr.length || comparator(arr[i], arr[left])) && - (right >= arr.length || comparator(arr[i], arr[right])) - if (!isValid) { - const next = - right >= arr.length || comparator(arr[left], arr[right]) ? left : right - ;[arr[i], arr[next]] = [arr[next], arr[i]] - moveDown(arr, next, comparator) - } -} diff --git a/5-longest-palindromic-substring.js b/5-longest-palindromic-substring.js index 76afe7f4..9f65c6d3 100755 --- a/5-longest-palindromic-substring.js +++ b/5-longest-palindromic-substring.js @@ -19,91 +19,3 @@ function chk(s, i, j) { return s.slice(i+1, j) } -// another - -/** - * @param {string} s - * @return {string} - */ -const longestPalindrome = function(s) { - let T = preProcess(s); - let n = T.length; - let P = []; - let C = 0, - R = 0; - let i_mirror; - for (let i = 1; i < n - 1; i++) { - i_mirror = 2 * C - i; // equals to i' = C - (i-C) - - P[i] = R > i ? Math.min(R - i, P[i_mirror]) : 0; - - // Attempt to expand palindrome centered at i - while (T[i + 1 + P[i]] == T[i - 1 - P[i]]) P[i]++; - - // If palindrome centered at i expand past R, - // adjust center based on expanded palindrome. - if (i + P[i] > R) { - C = i; - R = i + P[i]; - } - } - - // Find the maximum element in P. - let maxLen = 0; - let centerIndex = 0; - for (let j = 1; j < n - 1; j++) { - if (P[j] > maxLen) { - maxLen = P[j]; - centerIndex = j; - } - } - - return s.substr((centerIndex - 1 - maxLen) / 2, maxLen); -}; - -function preProcess(s) { - let n = s.length; - if (n === 0) return "^$"; - let ret = "^"; - for (let i = 0; i < n; i++) ret += "#" + s.substr(i, 1); - - ret += "#$"; - return ret; -} - -// another - -/** - * @param {string} s - * @return {string} - */ -const longestPalindrome = function(s) { - const n = s.length - let res = '' - for(let i = 0; i < n; i++) { - const first = chk(s, i, i, n) - if(first.length > res.length) res = first - const second = chk(s, i, i + 1, n) - if(second.length > res.length) res = second - } - return res -}; - -function chk(str, i, j, n) { - if(j >= n) return str[i] - let l = i, r = j - while(l >= 0 && r < n) { - if(str[l] === str[r]) { - l-- - r++ - } else { - return str.slice(l + 1, r) - } - } - if(l < 0) { - return str.slice(0, r) - } else { - return str.slice(l + 1, n) - } -} - diff --git a/50-powx-n.js b/50-powx-n.js index 36c6619e..0911fe19 100644 --- a/50-powx-n.js +++ b/50-powx-n.js @@ -15,67 +15,3 @@ const myPow = function(x, n) { } }; -// another - -/** - * @param {number} x - * @param {number} n - * @return {number} - */ -const myPow = function(x, n) { - if(n === 0) return 1 - if(n === 1) return x - if(n < 0) { - x = 1 / x - n = -n - } - return n % 2 === 1 ? myPow(x, ~~(n / 2)) ** 2 * x : myPow(x, ~~(n / 2)) ** 2 -}; - -// another - -/** - * @param {number} x - * @param {number} n - * @return {number} - */ -const myPow = function (x, n) { - if (n === 0) return 1 - if (n < 0) { - if (n === -(2 ** 31)) { - ++n - n = -n - x = 1 / x - return x * x * myPow(x * x, n / 2) - } - n = -n - x = 1 / x - } - return n % 2 == 0 ? myPow(x * x, n / 2) : x * myPow(x * x, (n / 2) >> 0) -} - -// another - -/** - * @param {number} x - * @param {number} n - * @return {number} - */ -const myPow = function (x, n) { - if (n === 0) return 1 - if (n < 0) { - n = -n - x = 1 / x - } - let res = 1 - while (n > 0) { - if (n & 1) { - res *= x - --n - } - x *= x - n /= 2 - } - return res -} - diff --git a/501-find-mode-in-binary-search-tree.js b/501-find-mode-in-binary-search-tree.js index 85c978ce..4b4a7f0a 100644 --- a/501-find-mode-in-binary-search-tree.js +++ b/501-find-mode-in-binary-search-tree.js @@ -30,26 +30,3 @@ function traverse(node, hash) { } -// another -const findMode = function(root) { - let res = []; - let cnt = 1; - let mx = 0; - let pre = null; - let search = function(node) { - if (!node) return; - search(node.left); - if (pre) { - cnt = (node.val === pre.val) ? cnt + 1 : 1; - } - if (cnt >= mx) { - if (cnt > mx) res.length = 0; - res.push(node.val); - mx = cnt; - } - pre = node; - search(node.right); - } - search(root); - return res; -}; diff --git a/502-ipo.js b/502-ipo.js index 9938088e..0a1ddf58 100644 --- a/502-ipo.js +++ b/502-ipo.js @@ -91,125 +91,3 @@ class PriorityQueue { } } -// another - -/** - * @param {number} k - * @param {number} W - * @param {number[]} Profits - * @param {number[]} Capital - * @return {number} - */ -const findMaximizedCapital = function(k, W, Profits, Capital) { - const idxArr = Profits.map((_, i) => i).sort((ia, ib) => Profits[ib] - Profits[ia]); - while (k) { - const choose = idxArr.findIndex(i => Capital[i] <= W); - if (choose == -1) return W; - W += Profits[idxArr[choose]]; - idxArr.splice(choose, 1); - k--; - } - return W; -}; - -// another - -/** - * @param {number} k - * @param {number} w - * @param {number[]} profits - * @param {number[]} capital - * @return {number} - */ -const findMaximizedCapital = function(k, w, profits, capital) { - const capPQ = new PQ((a, b) => a.cap < b.cap) - const proPQ = new PQ((a, b) => a.pro > b.pro) - const n = profits.length - - for(let i = 0; i < n; i++) { - capPQ.push({ cap: capital[i], pro: profits[i] }) - } - - while(k) { - - while(!capPQ.isEmpty() && capPQ.peek().cap <= w) { - proPQ.push(capPQ.pop()) - } - - if(proPQ.isEmpty()) break - - w += proPQ.pop().pro - k-- - } - - - return w -}; - -class PQ { - constructor(comparator = (a, b) => a > b) { - this.heap = [] - this.top = 0 - this.comparator = comparator - } - size() { - return this.heap.length - } - isEmpty() { - return this.size() === 0 - } - peek() { - return this.heap[this.top] - } - push(...values) { - values.forEach((value) => { - this.heap.push(value) - this.siftUp() - }) - return this.size() - } - pop() { - const poppedValue = this.peek() - const bottom = this.size() - 1 - if (bottom > this.top) { - this.swap(this.top, bottom) - } - this.heap.pop() - this.siftDown() - return poppedValue - } - replace(value) { - const replacedValue = this.peek() - this.heap[this.top] = value - this.siftDown() - return replacedValue - } - - parent = (i) => ((i + 1) >>> 1) - 1 - left = (i) => (i << 1) + 1 - right = (i) => (i + 1) << 1 - greater = (i, j) => this.comparator(this.heap[i], this.heap[j]) - swap = (i, j) => ([this.heap[i], this.heap[j]] = [this.heap[j], this.heap[i]]) - siftUp = () => { - let node = this.size() - 1 - while (node > this.top && this.greater(node, this.parent(node))) { - this.swap(node, this.parent(node)) - node = this.parent(node) - } - } - siftDown = () => { - let node = this.top - while ( - (this.left(node) < this.size() && this.greater(this.left(node), node)) || - (this.right(node) < this.size() && this.greater(this.right(node), node)) - ) { - let maxChild = - this.right(node) < this.size() && - this.greater(this.right(node), this.left(node)) - ? this.right(node) - : this.left(node) - this.swap(node, maxChild) - node = maxChild - } - } -} diff --git a/503-next-greater-element-II.js b/503-next-greater-element-II.js index 37a4898e..e70d8997 100755 --- a/503-next-greater-element-II.js +++ b/503-next-greater-element-II.js @@ -20,48 +20,3 @@ const nextGreaterElements = function(nums) { return res }; -// another - - -/** - * @param {number[]} nums - * @return {number[]} - */ -const nextGreaterElements = function(nums) { - const res = []; - for (let i = 0; i < nums.length; i++) { - res.push(single(i, nums)); - } - return res; -}; - -function single(idx, arr) { - const base = arr[idx]; - const prev = idx === 0 ? [] : arr.slice(0, idx); - const next = arr.slice(idx); - const comb = next.concat(prev); - for (let i = 0; i < comb.length; i++) { - if (comb[i] > base) return comb[i]; - } - return -1; -} - -// another - -/** - * @param {number[]} nums - * @return {number[]} - */ - const nextGreaterElements = function(nums) { - const res = [], n = nums.length - const stack = [] - for(let i = 2 * n - 1; i >= 0; i--) { - while(stack.length && nums[stack[stack.length - 1]] <= nums[i % n]) { - stack.pop() - } - res[i % n] = stack.length ? nums[stack[stack.length - 1]] : -1 - stack.push(i % n) - } - - return res -}; diff --git a/504-base-7.js b/504-base-7.js index 56cc8352..496487a3 100644 --- a/504-base-7.js +++ b/504-base-7.js @@ -16,8 +16,3 @@ const convertToBase7 = function(num) { return sign === '+' ? res : '-' + res }; -// another - -const convertToBase7 = function(num) { - return num.toString(7) -}; diff --git a/51-n-queens.js b/51-n-queens.js index 0654b675..afe47e4c 100644 --- a/51-n-queens.js +++ b/51-n-queens.js @@ -40,32 +40,3 @@ function build(chess) { return chess.map(el => el.join('')) } -// another - -/** - * @param {number} n - * @return {string[][]} - */ -const solveNQueens = function(n) { - const res = [] - bt(res, n) - return res -} - -function bt(res, n, board = [], r = 0) { - if (r === n) { - res.push(board.map(c => '.'.repeat(c) + 'Q' + '.'.repeat(n - c - 1))) - return - } - for (let c = 0; c < n; c++) { - if ( - !board.some( - (bc, br) => bc === c || bc === c + r - br || bc === c - r + br - ) - ) { - board.push(c) - bt(res, n, board, r + 1) - board.pop() - } - } -} diff --git a/516-longest-palindromic-subsequence.js b/516-longest-palindromic-subsequence.js index 7eaeefe0..0377c66d 100644 --- a/516-longest-palindromic-subsequence.js +++ b/516-longest-palindromic-subsequence.js @@ -18,47 +18,3 @@ const longestPalindromeSubseq = function(s) { return dp[0][n - 1] }; -// another - -/** - * @param {string} s - * @return {number} - */ -const longestPalindromeSubseq = function(s) { - const n = s.length - const dp = Array.from({ length: n }, () => Array(n).fill(0)) - for(let i = 0; i < n; i++) { - dp[i][i] = 1 - for(let j = i - 1; j >= 0; j--) { - if(s[i] === s[j]) { - dp[i][j] = dp[i - 1][j + 1] + 2 - } else { - dp[i][j] = Math.max(dp[i - 1][j], dp[i][j + 1]) - } - } - } - - return dp[n - 1][0] -}; - -// another - -/** - * @param {string} s - * @return {number} - */ -const longestPalindromeSubseq = function(s) { - const len = s.length - const dp = Array.from({length: len + 1}, () => new Array(len + 1).fill(0)) - for(let i = len - 1; i >= 0; i--) { - dp[i][i] = 1 - for(let j = i + 1; j < len; j++) { - if(s[i] === s[j]) { - dp[i][j] = dp[i+1][j-1] + 2 - } else { - dp[i][j] = Math.max(dp[i][j - 1], dp[i+1][j]) - } - } - } - return dp[0][s.length - 1] -}; diff --git a/518-coin-change-2.js b/518-coin-change-2.js index 54db7941..1293b600 100644 --- a/518-coin-change-2.js +++ b/518-coin-change-2.js @@ -19,46 +19,3 @@ function change(amount, coins) { } -// another - -/** - * @param {number} amount - * @param {number[]} coins - * @return {number} - */ -const change = function (amount, coins) { - const dp = Array(amount + 1).fill(0) - dp[0] = 1 - for (let coin of coins) { - for (let i = coin; i < amount + 1; i++) dp[i] += dp[i - coin] - } - return dp[amount] -} - -// another - -// another - -const change = function(amount,coins) { - const n = coins.length - const dp = Array.from({ length: n + 1 }, () => Array(amount + 1)) - dp[0][0] = 1 - for(let i = 0; i < n; i++) dp[i][0] = 1 - helper(0, amount) - return dp[0][amount] === undefined ? 0 : dp[0][amount] - - function helper(i, rem) { - if(dp[i][rem] != null) return dp[i][rem] - if(rem < 0) return 0 - if(rem === 0) return 1 - if(i >= coins.length) return 0 - let res = 0 - - res += helper(i, rem - coins[i]) - res += helper(i + 1, rem) - - dp[i][rem] = res - - return res - } -} diff --git a/524-longest-word-in-dictionary-through-deleting.js b/524-longest-word-in-dictionary-through-deleting.js index bfe2742f..525d0ed9 100644 --- a/524-longest-word-in-dictionary-through-deleting.js +++ b/524-longest-word-in-dictionary-through-deleting.js @@ -19,120 +19,3 @@ return res }; -// another - -/** - * @param {string} s - * @param {string[]} dictionary - * @return {string} - */ -const findLongestWord = function (s, dictionary) { - const n = dictionary.length - const idxArr = Array(n).fill(0) - let res = '' - for (const ch of s) { - for (let i = 0; i < n; i++) { - const idx = idxArr[i] - if (idx >= dictionary[i].length) continue - if (ch === dictionary[i][idx]) { - idxArr[i]++ - } - - if ( - idxArr[i] === dictionary[i].length && - (dictionary[i].length > res.length || - (dictionary[i].length === res.length && dictionary[i] < res)) - ) { - res = dictionary[i] - } - } - } - return res -} - - -// another - -/** - * @param {string} s - * @param {string[]} dictionary - * @return {string} - */ -const findLongestWord = function(s, dictionary) { - let res = '' - for(let d of dictionary) { - let j = 0 - for(let i = 0, n = s.length; i < n; i++) { - if(d[j] === s[i]) j++ - if(j === d.length && j >= res.length) { - if(j > res.length || d < res) { - res = d - } - break - } - } - } - return res -}; - -// another - -/** - * @param {string} s - * @param {string[]} dictionary - * @return {string} - */ -const findLongestWord = function(s, dictionary) { - dictionary.sort((a, b) => a.length === b.length ? cmp(a, b) : b.length - a.length) - let res = '' - for(let d of dictionary) { - let j = 0 - for(let i = 0, n = s.length; i < n; i++) { - if(d[j] === s[i]) j++ - if(j === d.length) return d - } - } - return '' - function cmp(s1, s2) { - for(let i = 0, n = s1.length; i < n; i++) { - if(s1[i] < s2[i]) return -1 - else if(s1[i] > s2[i]) return 1 - } - return 0 - } -}; - -// another - - -/** - * @param {string} s - * @param {string[]} d - * @return {string} - */ -const findLongestWord = function(s, d) { - let results = []; - let maxLen = 0; - for (const word of d) { - let j = 0; - for (let i = 0; i < s.length; i++) { - if (s[i] === word[j]) { - j++; - if (j === word.length) break; - } - } - if (j === word.length && word.length >= maxLen) { - if (word.length > maxLen) { - maxLen = word.length; - results = []; - } - results.push(word); - } - } - - let result = results[0]; - for (let i = 1; i < results.length; i++) { - if (results[i] < result) result = results[i]; - } - return result || ''; -} diff --git a/525-contiguous-array.js b/525-contiguous-array.js index 9b0da1ea..8f6cdb41 100644 --- a/525-contiguous-array.js +++ b/525-contiguous-array.js @@ -20,30 +20,3 @@ const findMaxLength = function(nums) { return res }; -// another - -/** - * @param {number[]} nums - * @return {number} - */ -const findMaxLength = function (nums) { - const map = new Map() - map.set(0, -1) - let count = 0 - let max = 0 - for (let i = 0; i < nums.length; i++) { - let num = nums[i] - if (num === 0) { - count -= 1 - } - if (num === 1) { - count += 1 - } - if (map.has(count)) { - max = Math.max(max, i - map.get(count)) - } else { - map.set(count, i) - } - } - return max -} diff --git a/528-random-pick-with-weight.js b/528-random-pick-with-weight.js index e33a3904..ac553180 100644 --- a/528-random-pick-with-weight.js +++ b/528-random-pick-with-weight.js @@ -34,75 +34,3 @@ Solution.prototype.pickIndex = function() { * var param_1 = obj.pickIndex() */ -// another - -/** - * @param {number[]} w - */ -const Solution = function(w) { - this.a = [] - let sum = 0 - for(let i = 0, len = w.length; i < len; i++) { - sum += w[i] - this.a[i] = sum - } -}; - -/** - * @return {number} - */ -Solution.prototype.pickIndex = function() { - const len = this.a.length - const sum = this.a[len - 1] - const rand = ((Math.random() * sum) >> 0) + 1 - let l = 0, h = len - 1 - while(l <= h) { - const mid = (l + h) >> 1 - if(this.a[mid] === rand) return mid - else if(this.a[mid] < rand) l = mid + 1 - else h = mid - 1 - } - return l -}; - -/** - * Your Solution object will be instantiated and called as such: - * var obj = new Solution(w) - * var param_1 = obj.pickIndex() - */ - -// another - -/** - * @param {number[]} w - */ -const Solution = function(w) { - this.a = [] - let sum = 0 - for(let i = 0, len = w.length; i < len; i++) { - sum += w[i] - this.a[i] = sum - } -}; - -/** - * @return {number} - */ -Solution.prototype.pickIndex = function() { - const len = this.a.length - const sum = this.a[len - 1] - const rand = ((Math.random() * sum) >> 0) + 1 - let l = 0, h = len - 1 - while(l < h) { - const mid = (l + h) >> 1 - if(this.a[mid] < rand) l = mid + 1 - else h = mid - } - return l -}; - -/** - * Your Solution object will be instantiated and called as such: - * var obj = new Solution(w) - * var param_1 = obj.pickIndex() - */ diff --git a/53-maximum-subarray.js b/53-maximum-subarray.js index dde9daa9..17cd919a 100755 --- a/53-maximum-subarray.js +++ b/53-maximum-subarray.js @@ -12,65 +12,3 @@ const maxSubArray = function (nums) { return res } -// another - -/** - * @param {number[]} nums - * @return {number} - */ -const maxSubArray = function(nums) { - let preSum = nums[0]; - let maxSum = preSum; - for (let i = 1; i < nums.length; i++) { - preSum = preSum > 0 ? preSum + nums[i] : nums[i]; - maxSum = Math.max(preSum, maxSum); - } - return maxSum; -}; - -// another - -/** - * @param {number[]} nums - * @return {number} - */ -const maxSubArray = function(nums) { - const n = nums.length, dp = Array(n).fill(0) - dp[0] = nums[0] - let res = dp[0] - for(let i = 1; i < n; i++) { - dp[i] = Math.max(dp[i - 1], 0) + nums[i] - res = Math.max(res, dp[i]) - } - return res -}; - -// another - -/** - * @param {number[]} nums - * @return {number} - */ -const maxSubArray = function(nums) { - return helper(nums, 0, nums.length - 1) -}; - -function helper(arr, l, r) { - if(l > r) return -Infinity - const mid = l + ((r - l) >> 1) - let cur = 0, leftMax = 0, rightMax = 0 - for(let i = mid - 1; i >= l; i--) { - cur += arr[i] - leftMax = Math.max(leftMax, cur) - } - cur = 0 - for(let i = mid + 1; i <= r; i++) { - cur += arr[i] - rightMax = Math.max(rightMax, cur) - } - const res = arr[mid] + leftMax + rightMax - const leftRes = helper(arr, l, mid - 1) - const rightRes = helper(arr, mid + 1, r) - - return Math.max(res, leftRes, rightRes) -} diff --git a/531-lonely-pixel-i.js b/531-lonely-pixel-i.js index c56dd1ea..f2979338 100644 --- a/531-lonely-pixel-i.js +++ b/531-lonely-pixel-i.js @@ -30,31 +30,3 @@ const findLonelyPixel = function(picture) { return res }; -// another - -/** - * @param {character[][]} picture - * @return {number} - */ -const findLonelyPixel = function(picture) { - if(picture == null || picture[0].length === 0) return 0 - const rows = picture.length - const cols = picture[0].length - const c = new Array(cols).fill(0) - for(let i = 0; i < rows; i++) { - for(let j = 0; j < cols; j++) { - if(picture[i][j] === 'B') c[j]++ - } - } - let res = 0 - for(let i = 0; i < rows; i++) { - let cnt = 0, pos = -1 - for(let j = 0; j < cols; j++) { - if(picture[i][j] !== 'B') continue - if(++cnt === 1) pos = j - else break - } - if(cnt === 1 && c[pos] === 1) res++ - } - return res -}; diff --git a/533-lonely-pixel-ii.js b/533-lonely-pixel-ii.js index f4de89c9..f67eaffd 100644 --- a/533-lonely-pixel-ii.js +++ b/533-lonely-pixel-ii.js @@ -34,48 +34,3 @@ const findBlackPixel = function(picture, N) { return ans } -// another - -/** - * @param {character[][]} picture - * @param {number} N - * @return {number} - */ -const findBlackPixel = function(picture, N) { - const rows = picture.length - if(rows === 0) return 0 - const cols = picture[0].length - if(cols === 0) return 0 - const m = new Map() - const colCnt = new Array(cols).fill(0) - for(let i = 0; i < rows; i++) { - const k = scanRow(picture, i, cols, N, colCnt) - if(k) { - m.set(k, (m.get(k) || 0) + 1) - } - } - let res = 0 - for(let [k, v] of m) { - if(v === N) { - for(let i = 0; i < cols; i++) { - if(k[i] === 'B' && colCnt[i] === N) res += N - } - } - } - return res -} - -function scanRow(p, r, cols, N, arr) { - let str = '' - let cnt = 0 - for(let i = 0; i < cols; i++) { - if(p[r][i] === 'B') { - cnt++ - arr[i] += 1 - } - str += p[r][i] - } - if(cnt === N) return str - return '' -} - diff --git a/538-convert-bst-to-greater-tree.js b/538-convert-bst-to-greater-tree.js index 9ee89693..7302830b 100644 --- a/538-convert-bst-to-greater-tree.js +++ b/538-convert-bst-to-greater-tree.js @@ -10,38 +10,10 @@ * @return {TreeNode} */ const convertBST = function(root) { - const arr = [] - traverse(root, arr) - for(let i = arr.length - 2; i >= 0; i--) { - arr[i].val += arr[i + 1].val - } - return root + if (root == null) return null; + convertBST(root.right); + root.val += sum; + sum = root.val; + convertBST(root.left); + return root; }; - -function traverse(node, arr) { - if(node == null) return - traverse(node.right, arr) - arr.unshift(node) - traverse(node.left, arr) -} - - -// another - -/** - * @param {TreeNode} root - * @return {TreeNode} - */ -const convertBST = function(root) { - const obj = {sum: 0} - traverse(root, obj) - return root -}; - -function traverse(node, obj) { - if(node == null) return - traverse(node.right, obj) - node.val += obj.sum - obj.sum = node.val - traverse(node.left, obj) -} diff --git a/54-spiral-matrix.js b/54-spiral-matrix.js index fbbb5aff..933a6cb8 100644 --- a/54-spiral-matrix.js +++ b/54-spiral-matrix.js @@ -40,47 +40,3 @@ const spiralOrder = function(matrix) { return res }; -// another - -/** - * @param {number[][]} matrix - * @return {number[]} - */ -const spiralOrder = function(matrix) { - const res = [], m = matrix.length, n = matrix[0].length - const dirs = [[0, 1], [1, 0], [0, -1], [-1, 0]] - let di = 0, i = 0, j = 0, nx = 0, ny = 1 - while(true) { - res.push(matrix[i][j]) - matrix[i][j] = Infinity - if(chk(i, j)) { - if(di === 0 && (j + 1 >= n || matrix[i][j + 1] === Infinity)) { - i++ - di = 1 - } else if(di === 1 && (i + 1 >= m || matrix[i + 1][j] === Infinity)) { - j-- - di = 2 - } else if(di === 2 && (j - 1 < 0 || matrix[i][j - 1] === Infinity)) { - i-- - di = 3 - } else if(di === 3 && (i - 1 < 0 || matrix[i - 1][j] === Infinity)) { - j++ - di = 0 - } else { - i += dirs[di][0] - j += dirs[di][1] - } - } else break - } - return res - - function chk(i, j) { - for(let dir of dirs) { - const nx = i + dir[0], ny = j + dir[1] - if(nx >= 0 && nx < matrix.length && ny >= 0 && ny < matrix[0].length && matrix[nx][ny] !== Infinity) return true - } - return false - } -}; - - diff --git a/540-single-element-in-a-sorted-array.js b/540-single-element-in-a-sorted-array.js index a840caa3..359a2057 100755 --- a/540-single-element-in-a-sorted-array.js +++ b/540-single-element-in-a-sorted-array.js @@ -13,53 +13,3 @@ const singleNonDuplicate = function(nums) { } }; -// another - -/** - * @param {number[]} nums - * @return {number} - */ -const singleNonDuplicate = function(nums) { - const n = nums.length - let left = 0, right = n - 1 - while(left < right) { - const mid = left + ((right - left) >> 1) - if(nums[mid] === nums[mid ^ 1]) left = mid + 1 - else right = mid - } - - return nums[left] -}; - -// another - -/** - * @param {number[]} nums - * @return {number} - */ -const singleNonDuplicate = function(nums) { - const n = nums.length - let left = 0, right = n - 1 - while(left < right) { - const mid = ~~((left + right) / 2) - if((mid % 2 === 0 && nums[mid] === nums[mid + 1]) || (mid % 2 === 1 && nums[mid] === nums[mid - 1])) left = mid + 1 - else right = mid - } - - return nums[left] -}; - -// another - -/** - * @param {number[]} nums - * @return {number} - */ -const singleNonDuplicate = function(nums) { - if(nums.length === 0) return 0 - let res = nums[0] - for(let i = 1, len = nums.length; i < len; i++) { - res ^= nums[i] - } - return res -}; diff --git a/543-diameter-of-binary-tree.js b/543-diameter-of-binary-tree.js index acf2deac..a8d6dd5f 100755 --- a/543-diameter-of-binary-tree.js +++ b/543-diameter-of-binary-tree.js @@ -23,57 +23,3 @@ const diameterOfBinaryTree = function (root) { return longest - 1 } -// another - -/** - * Definition for a binary tree node. - * function TreeNode(val, left, right) { - * this.val = (val===undefined ? 0 : val) - * this.left = (left===undefined ? null : left) - * this.right = (right===undefined ? null : right) - * } - */ -/** - * @param {TreeNode} root - * @return {number} - */ -const diameterOfBinaryTree = function(root) { - let res = 0 - dfs(root) - return res - - function dfs(node) { - if(node == null) return 0 - const left = dfs(node.left), right = dfs(node.right) - res = Math.max(res, left + right) - return Math.max(left, right) + 1 - } -}; - -// another - -/** - * Definition for a binary tree node. - * function TreeNode(val, left, right) { - * this.val = (val===undefined ? 0 : val) - * this.left = (left===undefined ? null : left) - * this.right = (right===undefined ? null : right) - * } - */ -/** - * @param {TreeNode} root - * @return {number} - */ -const diameterOfBinaryTree = function(root) { - let res = -Infinity - dfs(root) - return res - function dfs(node) { - if(node == null) return -1 - const left = dfs(node.left) - const right = dfs(node.right) - res = Math.max(res, left + right + 2) - return Math.max(left, right) + 1 - } -}; - diff --git a/544-output-contest-matches.js b/544-output-contest-matches.js index e81f6d0f..b464a248 100644 --- a/544-output-contest-matches.js +++ b/544-output-contest-matches.js @@ -57,26 +57,3 @@ const findContestMatch = function(n) { return arr[0] }; -// another - -/** - * @param {number} n - * @return {string} - */ -const findContestMatch = function(n) { - const arr = [] - for(let i = 0; i < n; i++) { - arr[i] = i + 1 - } - let l = 0 - let r = n - 1 - while(l < r) { - while(l < r) { - arr[l] = `(${arr[l]},${arr[r]})` - l++ - r-- - } - l = 0 - } - return arr[0] -}; diff --git a/55-jump-game.js b/55-jump-game.js index 4bf6ebf6..68b71b94 100644 --- a/55-jump-game.js +++ b/55-jump-game.js @@ -12,18 +12,3 @@ const canJump = function(nums) { return max >= nums.length - 1 }; -// another - -/** - * @param {number[]} nums - * @return {boolean} - */ -const canJump = function(nums) { - let max = 0 - const n = nums.length - for(let i = 0; i < n; i++) { - if(max < i) return false - max = Math.max(max, i + nums[i]) - if(max >= n - 1) return true - } -}; diff --git a/56-merge-intervals.js b/56-merge-intervals.js index a772b911..8fb1661e 100644 --- a/56-merge-intervals.js +++ b/56-merge-intervals.js @@ -17,71 +17,3 @@ const merge = function(intervals) { return res }; -// another - - -/** - * @param {number[][]} intervals - * @return {number[][]} - */ -const merge = function(intervals) { - if(intervals == null || intervals.length === 0) return [] - intervals.sort((a, b) => a[0] === b[0] ? a[1] - b[1] : a[0] - b[0]) - const res = [intervals[0]] - for(let i = 1, n = intervals.length; i < n; i++) { - const last = res[res.length - 1] - const lastEnd = last[1] - const [s, e] = intervals[i] - if(s > lastEnd) { - res.push(intervals[i]) - } else { - last[1] = Math.max(last[1], e) - } - } - return res -}; - -// another - -/** - * Definition for an interval. - * function Interval(start, end) { - * this.start = start; - * this.end = end; - * } - */ -/** - * @param {Interval[]} intervals - * @return {Interval[]} - */ -const merge = function(intervals) { - const hash = {} - intervals.forEach(el => { - if (hash.hasOwnProperty(el.start)) { - hash[el.start][1] = Math.max(hash[el.start][1], el.end) - } else { - hash[el.start] = [el.start, el.end] - } - }) - - const startArr = Object.keys(hash).sort((a, b) => +a - +b) - const res = [] - - while(startArr.length) { - let start = startArr.shift() - let end = hash[start][1] - - for(let i = 0; i < startArr.length; ) { - if (+startArr[i] <= end) { - end = Math.max(end, hash[startArr[i]][1]) - startArr.shift() - } else { - break - } - } - let ins = new Interval(+start, end) - res.push(ins) - - } - return res -}; diff --git a/560-subarray-sum-equals-k.js b/560-subarray-sum-equals-k.js index c81bff23..eba420c4 100644 --- a/560-subarray-sum-equals-k.js +++ b/560-subarray-sum-equals-k.js @@ -22,22 +22,3 @@ const subarraySum = function(nums, k) { return totalNum } -// another - -/** - * @param {number[]} nums - * @param {number} k - * @return {number} - */ -const subarraySum = function (nums, k) { - const n = nums.length, hash = { 0: 1 } - let pre = 0 - let res = 0 - for (let i = 0; i < n; i++) { - const cur = pre + nums[i] - if (hash[cur - k] != null) res += hash[cur - k] - hash[cur] = (hash[cur] || 0) + 1 - pre = cur - } - return res -} diff --git a/567-permutation-in-string.js b/567-permutation-in-string.js index 9a36f675..a86c9baa 100644 --- a/567-permutation-in-string.js +++ b/567-permutation-in-string.js @@ -33,34 +33,3 @@ function matches(s1map, s2map) { } -// another - -/** - * @param {string} s1 - * @param {string} s2 - * @return {boolean} - */ -const checkInclusion = function(s1, s2) { - const arr = Array(26).fill(0) - const a = 'a'.charCodeAt(0) - const s1l = s1.length - for(let c of s1) arr[c.charCodeAt(0) - a] += 1 - for(let i = 0, len = s2.length; i < len; i++) { - const tmp = s2[i] - arr[tmp.charCodeAt(0) - a]-- - if(i >= s1l - 1) { - if(allZeros(arr)) return true - arr[s2.charCodeAt(i - s1l + 1) - a]++ - } - - } - - return false -}; - -function allZeros(arr) { - for(let e of arr) { - if(e !== 0) return false - } - return true -} diff --git a/573-squirrel-simulation.js b/573-squirrel-simulation.js index 6d9f5cd5..ddee10a3 100644 --- a/573-squirrel-simulation.js +++ b/573-squirrel-simulation.js @@ -21,26 +21,3 @@ function distance(p1, p2) { return Math.abs(p1[0] - p2[0]) + Math.abs(p1[1] - p2[1]) } -// another - -/** - * @param {number} height - * @param {number} width - * @param {number[]} tree - * @param {number[]} squirrel - * @param {number[][]} nuts - * @return {number} - */ -const minDistance = function (height, width, tree, squirrel, nuts) { - let sum = 0, - maxDiff = -Number.MAX_VALUE - for (let nut of nuts) { - const dist = Math.abs(tree[0] - nut[0]) + Math.abs(tree[1] - nut[1]) - sum += 2 * dist - maxDiff = Math.max( - maxDiff, - dist - Math.abs(squirrel[0] - nut[0]) - Math.abs(squirrel[1] - nut[1]) - ) - } - return sum - maxDiff -} diff --git a/576-out-of-boundary-paths.js b/576-out-of-boundary-paths.js index 78472acd..dd972834 100644 --- a/576-out-of-boundary-paths.js +++ b/576-out-of-boundary-paths.js @@ -25,71 +25,3 @@ const findPaths = function (m, n, N, i, j) { return dp[1][i][j] } -// another - -/** - * @param {number} m - * @param {number} n - * @param {number} N - * @param {number} i - * @param {number} j - * @return {number} - */ -const findPaths = function (m, n, N, i, j) { - if (N <= 0) return 0; - const MOD = 1000000007; - let count = Array.from({ length: m }, () => new Array(n).fill(0)); - count[i][j] = 1; - let result = 0; - const dirs = [ - [-1, 0], - [1, 0], - [0, -1], - [0, 1], - ]; - for (let step = 0; step < N; step++) { - const temp = Array.from({ length: m }, () => new Array(n).fill(0)); - for (let r = 0; r < m; r++) { - for (let c = 0; c < n; c++) { - for (let d of dirs) { - const nr = r + d[0]; - const nc = c + d[1]; - if (nr < 0 || nr >= m || nc < 0 || nc >= n) { - result = (result + count[r][c]) % MOD; - } else { - temp[nr][nc] = (temp[nr][nc] + count[r][c]) % MOD; - } - } - } - } - count = temp; - } - return result; -}; - -// another - -/** - * @param {number} m - * @param {number} n - * @param {number} N - * @param {number} i - * @param {number} j - * @return {number} - */ -const findPaths = function (m, n, N, i, j, memo = new Map()) { - const key = N + ',' + i + ',' + j; - if (memo.has(key)) return memo.get(key); - const isOutside = i === -1 || i === m || j === -1 || j === n; - if (N === 0 || isOutside) return +isOutside; - memo.set(key, ( - findPaths(m, n, N - 1, i - 1, j, memo) - + findPaths(m, n, N - 1, i + 1, j, memo) - + findPaths(m, n, N - 1, i, j + 1, memo) - + findPaths(m, n, N - 1, i, j - 1, memo) - ) % 1000000007); - return memo.get(key); -} - - - diff --git a/582-kill-process.js b/582-kill-process.js index d5c5c502..014b472d 100644 --- a/582-kill-process.js +++ b/582-kill-process.js @@ -29,32 +29,3 @@ const killProcess = function(pid, ppid, kill) { return res }; -// another - -/** - * @param {number[]} pid - * @param {number[]} ppid - * @param {number} kill - * @return {number[]} - */ -const killProcess = function(pid, ppid, kill) { - const pm = new Map() - for(let i = 0, len = pid.length; i < len; i++) { - const p = ppid[i] - let tmp = pm.get(p) - if(!tmp) tmp = new Set() - tmp.add(pid[i]) - pm.set(p, tmp) - } - const res = [] - function dfs(k) { - res.push(k) - if(pm.get(k)) { - for(let e of pm.get(k)) { - dfs(e) - } - } - } - dfs(kill) - return res -}; diff --git a/583-delete-operation-for-two-strings.js b/583-delete-operation-for-two-strings.js index 66821d18..20e81b49 100644 --- a/583-delete-operation-for-two-strings.js +++ b/583-delete-operation-for-two-strings.js @@ -24,48 +24,3 @@ const minDistance = function (word1, word2, memo = new Map()) { return res } -// another - -/** - * @param {string} word1 - * @param {string} word2 - * @return {number} - */ -const minDistance = function (word1, word2) { - const len1 = word1.length - const len2 = word2.length - const dp = Array.from({ length: len1 + 1 }, () => new Array(len2 + 1).fill(0)) - for(let i = 1; i <= len1; i++) { - for(let j = 1; j<= len2; j++) { - if(word1[i - 1] === word2[j - 1]) dp[i][j] = dp[i - 1][j - 1] + 1 - else dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]) - } - } - return len1 + len2 - dp[len1][len2] * 2 -} - -// another - -/** - * @param {string} word1 - * @param {string} word2 - * @return {number} - */ -const minDistance = function (word1, word2) { - const len1 = word1.length - const len2 = word2.length - const dp = Array.from({ length: len1 + 1 }, () => new Array(len2 + 1).fill(0)) - for(let i = 1; i <= len2; i++) { - dp[0][i] = i - } - for(let j = 1; j <= len1; j++) { - dp[j][0] = j - } - for(let i = 1; i <= len1; i++) { - for(let j = 1; j<= len2; j++) { - if(word1[i - 1] === word2[j - 1]) dp[i][j] = dp[i - 1][j - 1] - else dp[i][j] = Math.min(dp[i - 1][j], dp[i][j - 1]) + 1 - } - } - return dp[len1][len2] -} diff --git a/588-design-in-memory-file-system.js b/588-design-in-memory-file-system.js index e23ea7dd..38e83bb1 100644 --- a/588-design-in-memory-file-system.js +++ b/588-design-in-memory-file-system.js @@ -74,76 +74,3 @@ FileSystem.prototype.readContentFromFile = function (filePath) { * var param_4 = obj.readContentFromFile(filePath) */ -// another - -const FileSystem = function () { - this.root = new Node() -} - -/** - * @param {string} path - * @return {string[]} - */ -FileSystem.prototype.ls = function (path) { - const cur = this.find(path) - if(cur.content) { - const arr = path.split('/') - return [arr[arr.length - 1]] - } - return Object.keys(cur.children).sort() -} - -/** - * @param {string} path - * @return {void} - */ -FileSystem.prototype.mkdir = function (path) { - this.find(path) -} - -/** - * @param {string} filePath - * @param {string} content - * @return {void} - */ -FileSystem.prototype.addContentToFile = function (filePath, content) { - const cur = this.find(filePath) - cur.content += content -} - -/** - * @param {string} filePath - * @return {string} - */ -FileSystem.prototype.readContentFromFile = function (filePath) { - const cur = this.find(filePath) - return cur.content -} - -FileSystem.prototype.find = function (filePath) { - if(filePath.length === 1) return this.root - const arr = filePath.split('/').slice(1) - let cur = this.root - for(let e of arr) { - if (cur.children[e] == null) cur.children[e] = new Node() - cur = cur.children[e] - } - return cur -} - -/** - * Your FileSystem object will be instantiated and called as such: - * var obj = new FileSystem() - * var param_1 = obj.ls(path) - * obj.mkdir(path) - * obj.addContentToFile(filePath,content) - * var param_4 = obj.readContentFromFile(filePath) - */ - -class Node { - constructor() { - this.children = {} - this.content = '' - } -} - diff --git a/591-tag-validator.js b/591-tag-validator.js index ead25315..3d6d514a 100644 --- a/591-tag-validator.js +++ b/591-tag-validator.js @@ -45,161 +45,3 @@ const isValid = function (code) { return stack.length === 0 } -// another - -/** - * @param {string} code - * @return {boolean} - */ -const isValid = function (code) { - code = code.replace(/|t/g, '-') - let prev - while (code !== prev) { - prev = code - code = code.replace(/<([A-Z]{1,9})>[^<]*<\/\1>/g, 't') - } - return code === 't' -} - - -// another - -/** - * @param {string} code - * @return {boolean} - */ -const isValid = function (code) { - const STATES = { - lt: 1, // < - tagOrData: 2, // uppercase=tag, '!'=data - tagName: 3, // uppercase, '>'=end - dataContent: 4, // any, ']'=wait-for-end - dataEnd: 5, // any, ']'=end - tagContent: 6, // any, '<'=tag-or-data - } - class Validator { - constructor(str) { - this.state = STATES.lt - this.str = str - this.stack = [] - this.i = 0 - // this ensure it doesnt start with cdata - this.isValid = this.isUpperCase(this.str[1]) - // check through code - while (this.isValid && this.i < this.str.length) { - this.isValid = this.validate() - } - // check if there is unclosed tags - this.isValid = this.isValid && !this.stack.length - } - - /** - * check and move on - */ - validate() { - let char = this.str[this.i] - switch (this.state) { - // expect '<', only used at start - case STATES.lt: - this.i++ - if (char == '<') { - this.state = STATES.tagOrData - return true - } - return false - // expect (end-)tag-name or cdata - case STATES.tagOrData: - // data - if (char == '!') { - this.i = this.findStrEnd(this.i + 1, '[CDATA[') - if (this.i == -1) { - return false - } - this.state = STATES.dataContent - return true - } - // end tag - if (char == '/') { - let name = this.stack.pop() - if (!name) { - return false - } - this.i = this.findStrEnd(this.i + 1, name + '>') - if (this.i == -1) { - return false - } - if (!this.stack.length & (this.i < this.str.length)) { - // more than one top level tags - return false - } - this.state = STATES.tagContent - return true - } - // tag name - { - let name = this.findTagName(this.i) - if (!name) { - return false - } - if (name.length > 9) { - return false - } - this.i += name.length + 1 - this.stack.push(name) - this.state = STATES.tagContent - return true - } - case STATES.dataContent: // you can try replace these code with indexOf - { - let end = this.findStrEnd(this.i, ']]>') - if (end != -1) { - // found end - this.i = end - this.state = STATES.tagContent - return true - } - // not yet - this.i++ - return true - } - case STATES.tagContent: - if (char == '<') { - this.state = STATES.tagOrData - this.i++ - return true - } - this.i++ - return true - } - } - - isUpperCase(char) { - return /[A-Z]/.test(char) - } - - findStrEnd(from, toFind = '') { - let end = from + toFind.length - for (let i = 0; i < toFind.length; i++) { - if (toFind[i] != this.str[i + from]) return -1 - } - return end - } - - findTagName(from) { - let tagName = '' - for (let i = from; i < this.str.length; i++) { - if (this.isUpperCase(this.str[i])) { - tagName += this.str[i] - continue - } - if (this.str[i] == '>') { - return tagName - } - return '' - } - return '' - } - } - let v = new Validator(code) - return v.isValid -} diff --git a/60-permutation-sequence.js b/60-permutation-sequence.js index ea812807..c38c6fe9 100644 --- a/60-permutation-sequence.js +++ b/60-permutation-sequence.js @@ -32,114 +32,3 @@ const getPermutation = function(n, k) { } }; -// another - -/** - * @param {number} n - * @param {number} k - * @return {string} - */ -const getPermutation = function(n, k) { - const factorial = Array(n + 1).fill(0) - factorial[0] = 1 - for(let i = 1, pre = 1; i <= n; i++) { - factorial[i] = pre * i - pre = factorial[i] - } - const nums = Array.from({length: n}, (_, i) => i + 1) - - let res = '' - k-- - for(let i = 1; i <= n; i++) { - const idx = ~~(k / factorial[n - i]) - res += nums[idx] - nums.splice(idx, 1) - k -= idx * factorial[n - i] - } - - return res -}; - -// another - -/** - * @param {number} n - * @param {number} k - * @return {string} - */ -const getPermutation = function(n, k) { - const fact = [] - const nums = [] - for(let i = 1; i <= n; i++) { - nums.push(i) - } - fact[0] = 1 - for(let i = 1, tmp = 1; i <= n; i++) { - tmp *= i - fact[i] = tmp - } - let res = '' - k-- - for(let i = 1; i <= n; i++) { - const idx = ~~(k / fact[n - i]) - res += nums[idx] - nums.splice(idx, 1) - k -= idx * fact[n - i] - } - - return res -}; - -// another - -/** - * @param {number} n - * @param {number} k - * @return {string} - */ -const getPermutation = function (n, k) { - let sb = '' - const num = [] - let fact = 1 - for (let i = 1; i <= n; i++) { - fact *= i - num.push(i) - } - for (let i = 0, l = k - 1; i < n; i++) { - fact = Math.floor(fact / (n - i)) - const index = Math.floor(l / fact) - sb += num.splice(index, 1)[0] - l -= index * fact - } - return sb -} - -// another - -/** - * @param {number} n - * @param {number} k - * @return {string} - */ -const getPermutation = function(n, k) { - const factorial = [] - const nums = [] - let res = '' - factorial[0] = 1 - for(let i = 1, sum = 1; i <= n; i++) { - sum *= i - factorial[i] = sum - } - for(let i = 1; i <= n; i++) { - nums.push(i) - } - k-- - for(let i = 0; i <= n; i++) { - const idx = ~~(k / factorial[n - i]) - res += nums[idx] - nums.splice(idx, 1) - k -= idx * factorial[n - i] - } - - return res -}; diff --git a/61-rotate-list.js b/61-rotate-list.js index 5f97560e..65fdc136 100644 --- a/61-rotate-list.js +++ b/61-rotate-list.js @@ -30,36 +30,3 @@ const rotateRight = function(head, k) { return dummy.next; }; -// another - -/** - * Definition for singly-linked list. - * function ListNode(val, next) { - * this.val = (val===undefined ? 0 : val) - * this.next = (next===undefined ? null : next) - * } - */ -/** - * @param {ListNode} head - * @param {number} k - * @return {ListNode} - */ -const rotateRight = function(head, k) { - if(head == null) return null - let len = 1 - let tmp = head - while(tmp.next) { - len++ - tmp = tmp.next - } - k = k % len - if(k === 0) return head - let tail = head - for(let i = 1; i < len - k; i++) { - tail = tail.next - } - const newHead = tail.next - tmp.next = head - tail.next = null - return newHead -}; diff --git a/62-unique-paths.js b/62-unique-paths.js index 6abde1b3..59e77cdc 100644 --- a/62-unique-paths.js +++ b/62-unique-paths.js @@ -15,40 +15,3 @@ const uniquePaths = function(m, n) { return dp[m - 1][n - 1] }; -// another - -/** - * @param {number} m - * @param {number} n - * @return {number} - */ -const uniquePaths = function(m, n) { - const dp = Array(m).fill(0) - for(let i = 0; i < n; i++) { - dp[0] = 1 - for(let j = 1; j < m; j++) { - dp[j] += dp[j - 1] - } - } - return dp[m - 1] -}; - -// another - -/** - * @param {number} m - * @param {number} n - * @return {number} - */ -const uniquePaths = function(m, n) { - return factorial(m+n-2)/(factorial(m - 1) * factorial(n - 1)) -}; - -function factorial(n) { - let res = 1 - while(n > 0) { - res *= n - n-- - } - return res -} diff --git a/621-task-scheduler.js b/621-task-scheduler.js index 1c07d12c..2587c021 100755 --- a/621-task-scheduler.js +++ b/621-task-scheduler.js @@ -29,75 +29,3 @@ const leastInterval = function (tasks, n) { } -// another - -/** - * @param {character[]} tasks - * @param {number} n - * @return {number} - */ -const leastInterval = function(tasks, n) { - const map = Array(26).fill(0); - const ca = "A".charCodeAt(0); - for (let c of tasks) map[c.charCodeAt(0) - ca]++; - map.sort((a, b) => a - b); - let max_val = map[25] - 1, - idle_slots = max_val * n; - for (let i = 24; i >= 0 && map[i] > 0; i--) { - idle_slots -= Math.min(map[i], max_val); - } - return idle_slots > 0 ? idle_slots + tasks.length : tasks.length; -}; - -// another - -/** - * @param {character[]} tasks - * @param {number} n - * @return {number} - */ -const leastInterval = function(tasks, n) { - const hash = {}; - for(let task of tasks) { - hash[task] = hash[task] + 1 || 1 - } - let max = 0, count = 0; - for(let el in hash) { - if(hash[el] > max) { - max = hash[el]; - count = 1 - } else if(hash[el] === max) { - count++; - } - } - return Math.max((max - 1) * (n + 1) + count, tasks.length) -}; - -// another - -/** - * @param {character[]} tasks - * @param {number} n - * @return {number} - */ -const leastInterval = function(tasks, n) { - let max = 0, maxCnt = 0 - const len = tasks.length, cnt = Array(26).fill(0), A = 'A'.charCodeAt(0) - - for(let ch of tasks) { - const idx = ch.charCodeAt(0) - A - cnt[idx]++ - if(max === cnt[idx]) maxCnt++ - else if(max < cnt[idx]) { - max = cnt[idx] - maxCnt = 1 - } - } - - const maxSlots = max * maxCnt - const avaiSlots = (max - 1) * (n - (maxCnt - 1)) - const rem = len - maxSlots - const emptySlots = Math.max(0, avaiSlots - rem) - - return len + emptySlots -}; diff --git a/623-add-one-row-to-tree.js b/623-add-one-row-to-tree.js index 1ce663de..645d231e 100644 --- a/623-add-one-row-to-tree.js +++ b/623-add-one-row-to-tree.js @@ -45,37 +45,3 @@ const addOneRow = function (root, v, d, level = 1) { return root } -// another - -/** - * Definition for a binary tree node. - * function TreeNode(val) { - * this.val = val; - * this.left = this.right = null; - * } - */ -/** - * @param {TreeNode} root - * @param {number} v - * @param {number} d - * @return {TreeNode} - */ -const addOneRow = function (root, v, d, level = 1) { - if (!root) return - if (d === 1) { - const newRoot = new TreeNode(v) - newRoot.left = root - return newRoot - } else if (d === level + 1) { - const oldLeft = root.left - const oldRight = root.right - root.left = new TreeNode(v) - root.right = new TreeNode(v) - root.left.left = oldLeft - root.right.right = oldRight - } else { - addOneRow(root.left, v, d, level + 1) - addOneRow(root.right, v, d, level + 1) - } - return root -} diff --git a/628-maximum-product-of-three-numbers.js b/628-maximum-product-of-three-numbers.js index d801ee9f..1a188af8 100644 --- a/628-maximum-product-of-three-numbers.js +++ b/628-maximum-product-of-three-numbers.js @@ -10,36 +10,3 @@ const maximumProduct = function (nums) { ) } -// another - -/** - * @param {number[]} nums - * @return {number} - */ -const maximumProduct = function (nums) { - let max1 = -Infinity - let max2 = -Infinity - let max3 = -Infinity - let min1 = Infinity - let min2 = Infinity - for (let num of nums) { - if (num > max1) { - max3 = max2 - max2 = max1 - max1 = num - } else if (num > max2) { - max3 = max2 - max2 = num - } else if (num > max3) { - max3 = num - } - - if (num < min1) { - min2 = min1 - min1 = num - } else if (num < min2) { - min2 = num - } - } - return Math.max(max1 * max2 * max3, max1 * min1 * min2) -} diff --git a/630-course-schedule-iii.js b/630-course-schedule-iii.js index b2f0f289..f567b543 100644 --- a/630-course-schedule-iii.js +++ b/630-course-schedule-iii.js @@ -18,235 +18,3 @@ const scheduleCourse = function (courses) { return queue.size() } -// another - -/** - * @param {number[][]} courses - * @return {number} - */ -const scheduleCourse = function(courses) { - const pq = new PQ((a, b) => a[0] === b[0] ? a[1] < b[1] : a[0] > b[0]) - const n = courses.length - courses.sort((a, b) => a[1] === b[1] ? a[0] - b[0] : a[1] - b[1]) - - let time = 0 - for(const e of courses) { - const [dur, end] = e - time += dur - pq.push(e) - if(time > end) { - const tmp = pq.pop() - time -= tmp[0] - } - } - - return pq.size() -}; - -class PQ { - constructor(comparator = (a, b) => a > b) { - this.heap = [] - this.top = 0 - this.comparator = comparator - } - size() { - return this.heap.length - } - isEmpty() { - return this.size() === 0 - } - peek() { - return this.heap[this.top] - } - push(...values) { - values.forEach((value) => { - this.heap.push(value) - this.siftUp() - }) - return this.size() - } - pop() { - const poppedValue = this.peek() - const bottom = this.size() - 1 - if (bottom > this.top) { - this.swap(this.top, bottom) - } - this.heap.pop() - this.siftDown() - return poppedValue - } - replace(value) { - const replacedValue = this.peek() - this.heap[this.top] = value - this.siftDown() - return replacedValue - } - - parent = (i) => ((i + 1) >>> 1) - 1 - left = (i) => (i << 1) + 1 - right = (i) => (i + 1) << 1 - greater = (i, j) => this.comparator(this.heap[i], this.heap[j]) - swap = (i, j) => ([this.heap[i], this.heap[j]] = [this.heap[j], this.heap[i]]) - siftUp = () => { - let node = this.size() - 1 - while (node > this.top && this.greater(node, this.parent(node))) { - this.swap(node, this.parent(node)) - node = this.parent(node) - } - } - siftDown = () => { - let node = this.top - while ( - (this.left(node) < this.size() && this.greater(this.left(node), node)) || - (this.right(node) < this.size() && this.greater(this.right(node), node)) - ) { - let maxChild = - this.right(node) < this.size() && - this.greater(this.right(node), this.left(node)) - ? this.right(node) - : this.left(node) - this.swap(node, maxChild) - node = maxChild - } - } -} - -// another - - -/** - * @param {number[][]} courses - * @return {number} - */ -const scheduleCourse = function (courses) { - const queue = new MaxPriorityQueue({ - priority: e => e[0] - }) - courses.sort((a, b) => a[1] - b[1]) - let time = 0 - for(let e of courses) { - time += e[0] - queue.enqueue(e) - if(time > e[1]) { - const tmp = queue.dequeue().element - time -= tmp[0] - } - } - return queue.size() -} - -// another - -/** - * @param {number[][]} courses - * @return {number} - */ -const scheduleCourse = function (courses) { - courses.sort((c1, c2) => c1[1] - c2[1]) - let count = 0 - let time = 0 - const queue = [] - const inQueue = (val) => { - let i = 0 - while (i < queue.length && queue[i] > val) i += 1 - queue.splice(i, 0, val) - } - for (let i = 0; i < courses.length; i += 1) { - const [dur, end] = courses[i] - if (time <= end - dur) { - count += 1 - time += dur - inQueue(dur) - } else if (queue.length && queue[0] > dur) { - time = time - queue.shift() + dur - inQueue(dur) - } - } - return count -} - -// another - -/** - * @param {number[][]} courses - * @return {number} - */ -const scheduleCourse = function (courses) { - courses.sort((a, b) => +a[1] - +b[1]) - let queue = new Heap() - let time = 0 - for (let c of courses) { - if (c[0] + time <= c[1]) { - time += c[0] - queue.push(c[0]) - } else if (queue.size() > 0) { - let top = queue.peek() - if (top > c[0]) { - queue.pop() - queue.push(c[0]) - time += c[0] - top - } - } - } - return queue.size() -} - -const parent = (i) => Math.floor((i - 1) / 2) -const left = (i) => 2 * i + 1 -const right = (i) => 2 * i + 2 -class Heap { - constructor() { - this.compare = (a, b) => +b - +a - this._heap = [] - } - size() { - return this._heap.length - } - _upper(i, j) { - return this.compare(this._heap[i], this._heap[j]) < 0 - } - _swap(i, j) { - let tmp = this._heap[i] - this._heap[i] = this._heap[j] - this._heap[j] = tmp - } - push(item) { - this._heap.push(item) - this._siftUp() - return this.size() - } - _siftUp() { - let node = this._heap.length - 1 - while (node > 0 && this._upper(node, parent(node))) { - this._swap(node, parent(node)) - node = parent(node) - } - } - peek() { - return this._heap[0] - } - pop() { - let ret = this._heap[0] - if (this.size() > 1) { - this._swap(0, this._heap.length - 1) - } - this._heap.pop() - this._siftDown() - return ret - } - _siftDown() { - let node = 0 - while ( - (right(node) < this.size() && this._upper(right(node), node)) || - (left(node) < this.size() && this._upper(left(node), node)) - ) { - let upperChild = - right(node) < this.size() && this._upper(right(node), left(node)) - ? right(node) - : left(node) - this._swap(upperChild, node) - node = upperChild - } - } -} - diff --git a/631-design-excel-sum-formula.js b/631-design-excel-sum-formula.js index ad7db1f9..c7908e46 100644 --- a/631-design-excel-sum-formula.js +++ b/631-design-excel-sum-formula.js @@ -81,89 +81,3 @@ Excel.prototype.getRange = function (arr) { */ -// another - -/** - * @param {number} H - * @param {character} W - */ -const Excel = function (H, W) { - this.data = [] - for (let i = 1; i <= H; i++) { - this.data[i] = [] - for (let j = 1; j <= this.col(W); j++) { - this.data[i][j] = 0 - } - } -} - -Excel.prototype.col = function (c) { - return c.charCodeAt() - 'A'.charCodeAt() + 1 -} - -Excel.prototype.parse = function (str) { - let idx = str.indexOf(':') - if (idx === -1) return { r: Number(str.slice(1)), c: str[0] } - let topLeft = str.slice(0, idx), - bottomRight = str.slice(idx + 1) - return [ - { r: Number(topLeft.slice(1)), c: topLeft[0] }, - { r: Number(bottomRight.slice(1)), c: bottomRight[0] }, - ] -} - -/** - * @param {number} r - * @param {character} c - * @param {number} v - * @return {void} - */ -Excel.prototype.set = function (r, c, v) { - this.data[r][this.col(c)] = v -} - -/** - * @param {number} r - * @param {character} c - * @return {number} - */ -Excel.prototype.get = function (r, c) { - if (Array.isArray(this.data[r][this.col(c)])) { - let sum = 0 - for (let str of this.data[r][this.col(c)]) { - let parsed = this.parse(str) - if (Array.isArray(parsed)) { - for (let i = parsed[0].r; i <= parsed[1].r; i++) { - for ( - let jc = parsed[0].c; - jc <= parsed[1].c; - jc = String.fromCharCode(jc.charCodeAt() + 1) - ) { - sum += this.get(i, jc) - } - } - } else sum += this.get(parsed.r, parsed.c) - } - return sum - } - return this.data[r][this.col(c)] -} - -/** - * @param {number} r - * @param {character} c - * @param {string[]} strs - * @return {number} - */ -Excel.prototype.sum = function (r, c, strs) { - this.set(r, c, strs) - return this.get(r, c) -} - -/** - * Your Excel object will be instantiated and called as such: - * var obj = new Excel(H, W) - * obj.set(r,c,v) - * var param_2 = obj.get(r,c) - * var param_3 = obj.sum(r,c,strs) - */ diff --git a/632-smallest-range.js b/632-smallest-range.js index 16153b17..c198610e 100644 --- a/632-smallest-range.js +++ b/632-smallest-range.js @@ -86,197 +86,3 @@ PQ.prototype.swap = function(i, j) { q[j] = t } -// another - -function Queue() { - this.data = [] -} - -Queue.prototype.pop = function() { - return this.data.shift() -} - -Queue.prototype.getMax = function() { - let n = this.data.length - 1 - let max = this.data[n] - if (max === undefined) { - return 100000000 - } - return max.val -} - -Queue.prototype.getMin = function() { - let min = this.data[0] - if (min === undefined) { - return -100000000 - } - return min.val -} - -Queue.prototype.add = function(node) { - if (!this.data.length) { - this.data.push(node) - return - } - - let index = findIndex(this.data, node) - this.data.splice(index, 0, node) - return true - - function findIndex(arr, node) { - let left = 0 - let right = arr.length - 1 - while (left <= right) { - let mid = Math.floor((left + right) / 2) - if (arr[mid].val === node.val) { - return mid - } - - if (arr[mid].val > node.val) { - right = mid - 1 - } else { - left = mid + 1 - } - } - return left - } -} - -function Node(list, val, index) { - this.list = list - this.val = val - this.index = index -} -/** - * @param {number[][]} nums - * @return {number[]} - */ -var smallestRange = function(nums) { - let queue = new Queue() - for (let i = 0; i < nums.length; i++) { - let node = new Node(i, nums[i][0], 0) - queue.add(node) - } - - let a = Math.min(queue.getMin(), queue.getMax()) - let b = Math.max(queue.getMin(), queue.getMax()) - let ans = [a, b] - let min = ans[1] - ans[0] - for (;;) { - let a = Math.min(queue.getMin(), queue.getMax()) - let b = Math.max(queue.getMin(), queue.getMax()) - if (b - a < min) { - min = b - a - ans = [a, b] - } - - let m = queue.pop() - let list = nums[m.list] - let index = m.index - if (index + 1 < list.length) { - m.index++ - m.val = list[m.index] - queue.add(m) - } else { - break - } - } - - return ans -} - - -// another - -class PriorityQueue { - constructor(comparator = (a, b) => a > b) { - this.heap = [] - this.top = 0 - this.comparator = comparator - } - size() { - return this.heap.length - } - isEmpty() { - return this.size() === 0 - } - peek() { - return this.heap[this.top] - } - push(...values) { - values.forEach((value) => { - this.heap.push(value) - this.siftUp() - }) - return this.size() - } - pop() { - const poppedValue = this.peek() - const bottom = this.size() - 1 - if (bottom > this.top) { - this.swap(this.top, bottom) - } - this.heap.pop() - this.siftDown() - return poppedValue - } - replace(value) { - const replacedValue = this.peek() - this.heap[this.top] = value - this.siftDown() - return replacedValue - } - - parent = (i) => ((i + 1) >>> 1) - 1 - left = (i) => (i << 1) + 1 - right = (i) => (i + 1) << 1 - greater = (i, j) => this.comparator(this.heap[i], this.heap[j]) - swap = (i, j) => ([this.heap[i], this.heap[j]] = [this.heap[j], this.heap[i]]) - siftUp = () => { - let node = this.size() - 1 - while (node > this.top && this.greater(node, this.parent(node))) { - this.swap(node, this.parent(node)) - node = this.parent(node) - } - } - siftDown = () => { - let node = this.top - while ( - (this.left(node) < this.size() && this.greater(this.left(node), node)) || - (this.right(node) < this.size() && this.greater(this.right(node), node)) - ) { - let maxChild = - this.right(node) < this.size() && - this.greater(this.right(node), this.left(node)) - ? this.right(node) - : this.left(node) - this.swap(node, maxChild) - node = maxChild - } - } -} -/** - * @param {number[][]} nums - * @return {number[]} - */ -const smallestRange = function (nums) { - const pq = new PriorityQueue((a, b) => a[0] < b[0]) - const limit = 10 ** 5, - n = nums.length, - { max } = Math - let right = -1e5, - res = [-limit, limit] - for (let i = 0; i < n; i++) { - pq.push([nums[i][0], i, 0]) - right = max(right, nums[i][0]) - } - while (pq.size()) { - const cur = pq.pop() - const [left, list, indice] = cur - if (right - left < res[1] - res[0]) res = [left, right] - if (indice + 1 === nums[list].length) return res - right = max(right, nums[list][indice + 1]) - pq.push([nums[list][indice + 1], list, indice + 1]) - } - return [] -} diff --git a/634-find-the-derangement-of-an-array.js b/634-find-the-derangement-of-an-array.js index ae7c4945..14feee16 100644 --- a/634-find-the-derangement-of-an-array.js +++ b/634-find-the-derangement-of-an-array.js @@ -15,19 +15,3 @@ const findDerangement = function (n) { return result } -// another - -/** - * @param {number} n - * @return {number} - */ -const findDerangement = function (n) { - if (n === 0) return 0 - const MOD = 10 ** 9 + 7 - const dp = [1, 0] - for (let i = 2; i <= n; i++) { - dp[i] = ((i - 1) * (dp[i - 2] + dp[i - 1])) % MOD - } - return dp[n] -} - diff --git a/642-design-search-autocomplete-system.js b/642-design-search-autocomplete-system.js index 1f4a5657..bac080da 100644 --- a/642-design-search-autocomplete-system.js +++ b/642-design-search-autocomplete-system.js @@ -106,153 +106,3 @@ Trie.prototype.stringsStartingWith = function (prefix) { return results } -// another - -class TrieNode { - constructor() { - this.children = new Map() - this.counts = new Map() - this.isWord = false - } -} - -class Pair { - constructor(s, c) { - this.str = s - this.cnt = c - } -} - -/** - * @param {string[]} sentences - * @param {number[]} times - */ -const AutocompleteSystem = function (sentences, times) { - this.root = new TrieNode() - this.prefix = '' - for (let i = 0, n = sentences.length; i < n; i++) { - this.add(sentences[i], times[i]) - } -} - -AutocompleteSystem.prototype.add = function (str, cnt) { - let cur = this.root - for (const ch of str) { - let next = cur.children.get(ch) - if (next == null) { - next = new TrieNode() - cur.children.set(ch, next) - } - cur = next - cur.counts.set(str, (cur.counts.get(str) || 0) + cnt) - } - cur.isWord = true -} - -/** - * @param {character} c - * @return {string[]} - */ -AutocompleteSystem.prototype.input = function (c) { - if (c === '#') { - this.add(this.prefix, 1) - this.prefix = '' - return [] - } - this.prefix += c - let cur = this.root - for (const ch of this.prefix) { - const next = cur.children.get(ch) - if (next == null) { - return [] - } - cur = next - } - const pq = new PriorityQueue((a, b) => - a.cnt === b.cnt ? a.str.localeCompare(b.str) < 0 : a.cnt > b.cnt - ) - - for(const s of cur.counts.keys()) { - pq.push(new Pair(s, cur.counts.get(s))) - } - const res = [] - for(let i = 0; i < 3 && pq.size(); i++) { - res.push(pq.pop().str) - } - - return res -} - -/** - * Your AutocompleteSystem object will be instantiated and called as such: - * var obj = new AutocompleteSystem(sentences, times) - * var param_1 = obj.input(c) - */ - -class PriorityQueue { - constructor(comparator = (a, b) => a > b) { - this.heap = [] - this.top = 0 - this.comparator = comparator - } - size() { - return this.heap.length - } - isEmpty() { - return this.size() === 0 - } - peek() { - return this.heap[this.top] - } - push(...values) { - values.forEach((value) => { - this.heap.push(value) - this.siftUp() - }) - return this.size() - } - pop() { - const poppedValue = this.peek() - const bottom = this.size() - 1 - if (bottom > this.top) { - this.swap(this.top, bottom) - } - this.heap.pop() - this.siftDown() - return poppedValue - } - replace(value) { - const replacedValue = this.peek() - this.heap[this.top] = value - this.siftDown() - return replacedValue - } - - parent = (i) => ((i + 1) >>> 1) - 1 - left = (i) => (i << 1) + 1 - right = (i) => (i + 1) << 1 - greater = (i, j) => this.comparator(this.heap[i], this.heap[j]) - swap = (i, j) => ([this.heap[i], this.heap[j]] = [this.heap[j], this.heap[i]]) - siftUp = () => { - let node = this.size() - 1 - while (node > this.top && this.greater(node, this.parent(node))) { - this.swap(node, this.parent(node)) - node = this.parent(node) - } - } - siftDown = () => { - let node = this.top - while ( - (this.left(node) < this.size() && this.greater(this.left(node), node)) || - (this.right(node) < this.size() && this.greater(this.right(node), node)) - ) { - let maxChild = - this.right(node) < this.size() && - this.greater(this.right(node), this.left(node)) - ? this.right(node) - : this.left(node) - this.swap(node, maxChild) - node = maxChild - } - } -} diff --git a/645-set-mismatch.js b/645-set-mismatch.js index 98106af4..803cedd7 100644 --- a/645-set-mismatch.js +++ b/645-set-mismatch.js @@ -21,21 +21,3 @@ const findErrorNums = function(nums) { return res }; -// another - -/** - * @param {number[]} nums - * @return {number[]} - */ -const findErrorNums = function(nums) { - const res = []; - for (const i of nums) { - if (nums[Math.abs(i) - 1] < 0) res[0] = Math.abs(i); - else nums[Math.abs(i) - 1] *= -1; - } - for (let i = 0; i < nums.length; i++) { - if (nums[i] > 0) res[1] = i + 1; - } - return res; -}; - diff --git a/646-maximum-length-of-pair-chain.js b/646-maximum-length-of-pair-chain.js index ec50ac9d..59a05b36 100755 --- a/646-maximum-length-of-pair-chain.js +++ b/646-maximum-length-of-pair-chain.js @@ -14,43 +14,3 @@ const findLongestChain = function(pairs) { return res }; -// another - -/** - * @param {number[][]} pairs - * @return {number} - */ -const findLongestChain = function(pairs) { - pairs.sort((a, b) => a[1] - b[1]); - let cur = Number.MIN_SAFE_INTEGER; - let res = 0; - for (let i = 0; i < pairs.length; i++) { - if (cur < pairs[i][0]) { - cur = pairs[i][1]; - res += 1; - } - } - return res; -}; - -// another - -/** - * @param {number[][]} pairs - * @return {number} - */ -const findLongestChain = function (pairs) { - pairs.sort((a, b) => a[0] - b[0]) - let out = 0 - let prevEnd = Number.MIN_SAFE_INTEGER - for (let i = 0; i < pairs.length; i++) { - const cur = pairs[i] - if (prevEnd < cur[0]) { - prevEnd = cur[1] - out += 1 - } else { - prevEnd = Math.min(cur[1], prevEnd) - } - } - return out -} diff --git a/647-palindromic-substrings.js b/647-palindromic-substrings.js index ebf640e7..aa868b6e 100755 --- a/647-palindromic-substrings.js +++ b/647-palindromic-substrings.js @@ -31,34 +31,3 @@ const countSubstrings = function(s) { console.log(countSubstrings("abc")); console.log(countSubstrings("aaa")); -// another - -/** - * @param {string} s - * @return {number} - */ -const countSubstrings = function(s) { - const arr = manachers(s) - return arr.map(e => ~~((e + 1) / 2)).reduce((ac, e) => ac + e, 0) -}; - -function manachers(s) { - const str = `@#${s.split('').join('#')}#$` - const arr = Array(str.length).fill(0) - - let center = right = 0 - for(let i = 1, n = str.length; i < n - 1; i++) { - if(i < right) { - arr[i] = Math.min(right - i, arr[2 * center - i]) - } - while(str[i + arr[i] + 1] === str[i - arr[i] - 1]) { - arr[i] += 1 - } - if(i + arr[i] > right) { - center = i - right = i + arr[i] - } - } - - return arr -} diff --git a/651-4-keys-keyboard.js b/651-4-keys-keyboard.js index b20bcd6f..99d67aa9 100644 --- a/651-4-keys-keyboard.js +++ b/651-4-keys-keyboard.js @@ -16,39 +16,3 @@ const maxA = function (N) { return recurse(N) } -// another - -/** - * @param {number} N - * @return {number} - */ -const maxA = function (N) { - const dp = [...new Array(N + 1)].map((_, i) => i) - for (let i = 4; i <= N; i++) { - for (let j = 1; j <= i - 3; j++) { - dp[i] = Math.max(dp[i], dp[j] * (i - j - 1)) - } - } - return dp[N] -} - -// another - -/** - * @param {number} N - * @return {number} - */ -const maxA = function (N) { - const dp = new Array(7).fill(0) - for (let i = 1; i <= N; i++) { - dp[0] = i - for (let k = 6; k > 2; k--) { - dp[0] = Math.max(dp[0], dp[k] * (k - 1)) - } - for (let k = 6; k > 0; k--) { - dp[k] = dp[k - 1] - } - } - return dp[0] -} - diff --git a/652-find-duplicate-subtrees.js b/652-find-duplicate-subtrees.js index 8a1a84da..a15cf417 100644 --- a/652-find-duplicate-subtrees.js +++ b/652-find-duplicate-subtrees.js @@ -25,34 +25,3 @@ function pre(node, hash, res) { return str } -// another - -/** - * Definition for a binary tree node. - * function TreeNode(val, left, right) { - * this.val = (val===undefined ? 0 : val) - * this.left = (left===undefined ? null : left) - * this.right = (right===undefined ? null : right) - * } - */ -/** - * @param {TreeNode} root - * @return {TreeNode[]} - */ -const findDuplicateSubtrees = function(root) { - const serId = {}, cntId = {}, res = [] - let id = 1 - post(root) - return res - - function post(node) { - if(node == null) return 0 - const curId = `${post(node.left)},${node.val},${post(node.right)}` - serId[curId] = serId[curId] || id - if(serId[curId] === id) id++ - cntId[curId] = (cntId[curId] || 0) + 1 - if(cntId[curId] === 2) res.push(node) - return serId[curId] - } -}; - diff --git a/658-find-k-closest-elements.js b/658-find-k-closest-elements.js index 38d61158..474f9081 100644 --- a/658-find-k-closest-elements.js +++ b/658-find-k-closest-elements.js @@ -17,26 +17,3 @@ const findClosestElements = function(arr, k, x) { return arr.slice(lo, lo+k); }; -// another - -/** - * @param {number[]} arr - * @param {number} k - * @param {number} x - * @return {number[]} - */ -const findClosestElements = function(arr, k, x) { - let lo=0,hi=arr.length-1 - while(hi-lo>=k){ - let left=Math.abs(x-arr[lo]) - let right=Math.abs(x-arr[hi]) - if(leftright){ - lo++ - }else{ - hi-- - } - } - return arr.slice(lo,hi+1) -}; diff --git a/659-split-array-into-consecutive-subsequences.js b/659-split-array-into-consecutive-subsequences.js index af5bb0fc..27473aa8 100644 --- a/659-split-array-into-consecutive-subsequences.js +++ b/659-split-array-into-consecutive-subsequences.js @@ -28,30 +28,3 @@ function getOrDefault(map, key) { } -// another - -/** - * @param {number[]} nums - * @return {boolean} - */ -const isPossible = function(nums) { - let prev = -Infinity, p1 = 0, p2 = 0, p3 = 0 - let i = 0 - const n = nums.length - while (i < n) { - let curr = nums[i], c1 = 0, c2 = 0, c3 = 0 - let cnt = 0 - while (i < n && nums[i] === curr) { cnt++; i++ } - if (curr !== prev+1) { - if (p1 > 0 || p2 > 0) { return false } - c1 = cnt; c2 = 0; c3 = 0 - } else { - if (cnt < p1 + p2) { return false } - c2 = p1 - c3 = p2 + Math.min(p3, cnt - p1 - p2) - c1 = Math.max(0, cnt - p1 - p2 - p3) - } - prev = curr; p1 = c1; p2 = c2; p3 = c3; - } - return p1 === 0 && p2 === 0 -}; diff --git a/660-remove-9.js b/660-remove-9.js index c9fc656b..17ac462e 100644 --- a/660-remove-9.js +++ b/660-remove-9.js @@ -11,12 +11,3 @@ const newInteger = function (n) { return parseInt(res, 10) } -// another - -/** - * @param {number} n - * @return {number} - */ -const newInteger = function (n) { - return +(n).toString(9) -} diff --git a/664-strange-printer.js b/664-strange-printer.js index deb5a1fe..19b2f122 100644 --- a/664-strange-printer.js +++ b/664-strange-printer.js @@ -30,25 +30,3 @@ const strangePrinter = function(s) { return help(s, 0, n - 1) } -// another - -/** - * @param {string} s - * @return {number} - */ -const strangePrinter = function(s) { - const n = s.length - const dp = Array.from({ length: n }, () => Array(n).fill(n)) - for(let i = 0; i < n; i++) dp[i][i] = 1 - for(let len = 2; len <= n; len++) { - for(let i = 0; i < n - len + 1; i++) { - let j = i + len - 1 - dp[i][j] = 1 + dp[i + 1][j] - for(let k = i + 1; k < j; k++) { - if(s[i] === s[k]) dp[i][j] = Math.min(dp[i][j], dp[i][k - 1] + dp[k + 1][j]) - } - if(s[i] === s[j]) dp[i][j] = Math.min(dp[i][j - 1], dp[i][j]) - } - } - return dp[0][n - 1] -}; diff --git a/666-path-sum-iv.js b/666-path-sum-iv.js index a223b42f..249e5119 100644 --- a/666-path-sum-iv.js +++ b/666-path-sum-iv.js @@ -33,57 +33,3 @@ const pathSum = function (nums) { return res } -// another - -/** - * @param {number[]} nums - * @return {number} - */ -const pathSum = function (nums) { - if (nums == null || nums.length === 0) return 0 - const tree = new Map() - for (let num of nums) { - tree.set((num / 10) >> 0, num % 10) - } - return traverse((nums[0] / 10) >> 0, 0) - - function traverse(node, prev) { - if (!tree.has(node)) return 0 - const level = (node / 10) >> 0 - const pos = node % 10 - const val = tree.get(node) - const left = (level + 1) * 10 + pos * 2 - 1 - const right = (level + 1) * 10 + pos * 2 - const cur = prev + val - if (!tree.has(left) && !tree.has(right)) return cur - return traverse(left, cur) + traverse(right, cur) - } -} - -// another - -/** - * @param {number[]} nums - * @return {number} - */ -const pathSum = function (nums) { - const arr = Array(1 << 5).fill(-1) - for (let num of nums) { - let [lvl, pos, val] = [ - parseInt(num / 100), - parseInt((num % 100) / 10), - num % 10, - ] - arr[(1 << (lvl - 1)) - 1 + pos - 1] = val - } - let sum = 0 - for (let i = 0; i < 1 << 4; i++) { - if (arr[i] !== -1) { - arr[i] += i > 0 ? arr[parseInt((i - 1) >> 1)] : 0 - if (arr[i * 2 + 1] === -1 && arr[i * 2 + 2] === -1) { - sum += arr[i] - } - } - } - return sum -} diff --git a/668-kth-smallest-number-in-multiplication-table.js b/668-kth-smallest-number-in-multiplication-table.js index 6e15d6e4..2774f24c 100644 --- a/668-kth-smallest-number-in-multiplication-table.js +++ b/668-kth-smallest-number-in-multiplication-table.js @@ -31,32 +31,3 @@ function count(m, n, target) { return nSmaller; } -// another - -/** - * @param {number} m - * @param {number} n - * @param {number} k - * @return {number} - */ -const findKthNumber = function(m, n, k) { - let left = 1; - let right = m * n; - while (left < right) { - const mid = Math.floor((left + right) / 2); - const num = count(m, n, mid); - if (num < k) left = mid + 1; - else right = mid; - } - return left; -}; - -function count(m, n, target) { - let res = 0; - let j = n; - for (let i = 1; i <= m; i++) { - while (i * j > target) j-- - res += j; - } - return res; -} diff --git a/67-add-binary.js b/67-add-binary.js index 1e6c437f..ef605517 100644 --- a/67-add-binary.js +++ b/67-add-binary.js @@ -17,27 +17,3 @@ const addBinary = function(a, b) { return s }; -// another - -/** - * @param {string} a - * @param {string} b - * @return {string} - */ -const addBinary = function(a, b) { - let next = false - let res = [] - let ai = a.length - 1 - let bi = b.length - 1 - while((ai >= 0 && bi >=0) || next) { - const tmp = (ai >= 0 ? +a[ai--] : 0) + (bi >= 0 ? +b[bi--] : 0) + (next ? 1 : 0) - if(tmp > 1) next = true - else next = false - res.unshift('' + (tmp % 2)) - } - - while(ai >= 0) res.unshift(a[ai--]) - while(bi >= 0) res.unshift(b[bi--]) - - return res.join('') -}; diff --git a/671-second-minimum-node-in-a-binary-tree.js b/671-second-minimum-node-in-a-binary-tree.js index 12bd486a..63bc7f66 100644 --- a/671-second-minimum-node-in-a-binary-tree.js +++ b/671-second-minimum-node-in-a-binary-tree.js @@ -30,37 +30,3 @@ const findSecondMinimumValue = function(root) { return min2nd === Number.MAX_VALUE ? -1 : min2nd }; -// another - -/** - * Definition for a binary tree node. - * function TreeNode(val) { - * this.val = val; - * this.left = this.right = null; - * } - */ -/** - * @param {TreeNode} root - * @return {number} - */ -const findSecondMinimumValue = function (root) { - if (root === null) return -1; - if (root.left === null && root.right === null) return -1; - let left = root.left.val; - let right = root.right.val; - if (left === root.val) { - left = findSecondMinimumValue(root.left); - } - if (right === root.val) { - right = findSecondMinimumValue(root.right); - } - if (right !== -1 && left !== -1) { - return Math.min(left, right); - } - if (right === -1) { - return left; - } - if (left === -1) { - return right; - } -}; diff --git a/678-valid-parenthesis-string.js b/678-valid-parenthesis-string.js index 443a9a4d..384c4dbe 100644 --- a/678-valid-parenthesis-string.js +++ b/678-valid-parenthesis-string.js @@ -13,25 +13,3 @@ const checkValidString = function(s) { return lo === 0; }; -// another - -/** - * @param {string} s - * @return {boolean} - */ - const checkValidString = function (s) { - let lo = 0, hi = 0 // 可能多余的‘(’ - for(let ch of s) { - if(ch === '(') lo++, hi++ - if(ch === ')') { - if(lo > 0) lo-- - hi-- - } - if(ch === '*') { - if(lo > 0) lo-- - hi++ - } - if(hi < 0) return false - } - return lo === 0 -} diff --git a/679-24-game.js b/679-24-game.js index 20fd0351..95c97cc2 100644 --- a/679-24-game.js +++ b/679-24-game.js @@ -36,39 +36,3 @@ function dfs(list) { } -// another - -/** - * @param {number[]} nums - * @return {boolean} - */ -const judgePoint24 = function(nums) { - return dfs(nums); -}; - -function dfs(list) { - if (list.length === 1) { - if (Math.abs(list[0] - 24) < 0.001) { - return true; - } - return false; - } - for (let i = 0; i < list.length; i++) { - for (let j = i + 1; j < list.length; j++) { - for (let c of compute(list[i], list[j])) { - let nextRound = []; - nextRound.push(c); - for (let k = 0; k < list.length; k++) { - if (k === j || k === i) continue; - nextRound.push(list[k]); - } - if (dfs(nextRound)) return true; - } - } - } - return false; -} - -function compute(a, b) { - return [a + b, a - b, b - a, a * b, a / b, b / a]; -} diff --git a/68-text-justification.js b/68-text-justification.js index 3f26e4a1..d6213262 100755 --- a/68-text-justification.js +++ b/68-text-justification.js @@ -33,49 +33,3 @@ const fullJustify = function(words, maxWidth) { return res }; -// another - -/** - * @param {string[]} words - * @param {number} L - * @returns {string[]} - */ -const fullJustify = function(words, L) { - const res = [""]; - if (words.length === 0 || L === 0) { - return res; - } else { - res.shift(); - for (let i = 0, k, l; i < words.length; i += k) { - for ( - k = l = 0; - i + k < words.length && l + words[i + k].length <= L - k; - k++ - ) { - l += words[i + k].length; - } - let tmp = words[i]; - for (j = 0; j < k - 1; j++) { - if (i + k >= words.length) { - tmp += " "; - } else { - // for (i = 0; i < ((L - l) / (k - 1) + (j < (L - l) % (k - 1))) - 1; i++) { - // tmp += ' '; - // } - tmp += Array( - parseInt((L - l) / (k - 1) + (j < (L - l) % (k - 1))) + 1 - ).join(" "); - } - // tmp += (L - l) / (k - 1) + (j < (L - l) % (k - 1)) + ' '; - tmp += words[i + j + 1]; - } - // for (i = 0; i < (L - tmp.length); i++) { - // tmp += ' ' - // } - tmp += Array(parseInt(L - tmp.length) + 1).join(" "); - // tmp += L - tmp.length + ' '; - res.push(tmp); - } - return res; - } -}; diff --git a/687-longest-univalue-path.js b/687-longest-univalue-path.js index 123bfd54..8d2c48a2 100644 --- a/687-longest-univalue-path.js +++ b/687-longest-univalue-path.js @@ -30,41 +30,3 @@ const longestUnivaluePath = function(root) { } }; -// another - -/** - * Definition for a binary tree node. - * function TreeNode(val) { - * this.val = val; - * this.left = this.right = null; - * } - */ -/** - * @param {TreeNode} root - * @return {number} - */ -const getCount = function(root, longest) { - if (!root) { - return 0; - } - let leftCount = getCount(root.left, longest); - let rightCount = getCount(root.right, longest); - if (root.left && root.left.val === root.val) { - leftCount++; - } else { - leftCount = 0; - } - if (root.right && root.right.val === root.val) { - rightCount++; - } else { - rightCount = 0; - } - longest.max = Math.max(longest.max, leftCount + rightCount); - return Math.max(leftCount, rightCount); -}; - -const longestUnivaluePath = function(root) { - let longest = { max: 0 }; - getCount(root, longest); - return longest.max; -}; diff --git a/69-sqrt(x).js b/69-sqrt(x).js index 3e3da29d..dfaeb2a4 100755 --- a/69-sqrt(x).js +++ b/69-sqrt(x).js @@ -15,33 +15,3 @@ const mySqrt = function(x) { return left; }; -// another - -/** - * @param {number} x - * @return {number} - */ -const mySqrt = function(x) { - r = x; - while (r * r > x) r = ((r + x / r) / 2) | 0; - return r; -}; - -// another - -/** - * @param {number} x - * @return {number} - */ -const mySqrt = function(x) { - let l = 1, r = x - if(x === 0) return 0 - while(true) { - let mid = l + ((r - l) >> 1) - if(mid * mid > x) r = mid - 1 - else { - if((mid + 1) * (mid + 1) > x) return mid - l = mid + 1 - } - } -}; diff --git a/691-stickers-to-spell-word.js b/691-stickers-to-spell-word.js index 8c0574c5..35a9d800 100644 --- a/691-stickers-to-spell-word.js +++ b/691-stickers-to-spell-word.js @@ -29,66 +29,3 @@ const minStickers = function (stickers, target) { } -// another - - -/** - * @param {string[]} stickers - * @param {string} target - * @return {number} - */ -const minStickers = function(stickers, target) { - const isEqual = (arr1, arr2) => { - for (let i = 0; i < arr1.length; ++i) if (arr1[i] !== arr2[i]) return false - return true - } - - const minus = (arr1, arr2) => { - let res = [] - for (let i = 0; i < arr1.length; ++i) - res[i] = arr1[i] <= 0 ? arr1[i] : arr1[i] - arr2[i] - return res - } - - const isAllNonpositive = arr => { - return arr.every(item => item <= 0) - } - - const getString = arr => { - return arr.reduce((acc, cur, idx) => { - if (cur > 0) return acc + String.fromCharCode(idx + 97).repeat(cur) - else return acc - }, '') - } - - let ss = stickers.map(word => { - let tmp = new Array(26).fill(0) - for (let i = 0; i < word.length; ++i) tmp[word.charCodeAt(i) - 97]++ - return tmp - }) - let root = new Array(26).fill(0) - for (let i = 0; i < target.length; ++i) root[target.charCodeAt(i) - 97]++ - let cache = new Set() - let queue = [root] - let size = 0, - level = 0, - front = null - while (queue.length !== 0) { - size = queue.length - while (size--) { - front = queue.shift() - for (let w of ss) { - let t = minus(front, w) - let str = getString(t) - if (isEqual(t, front) || cache.has(str)) continue - if (isAllNonpositive(t)) return level + 1 - else { - queue.push(t) - cache.add(str) - } - } - } - level++ - } - return -1 -} diff --git a/694-number-of-distinct-islands.js b/694-number-of-distinct-islands.js index 1472a640..d350780f 100644 --- a/694-number-of-distinct-islands.js +++ b/694-number-of-distinct-islands.js @@ -73,33 +73,3 @@ function helper(i, j, arr, tempArr) { } } -// another - -/** - * @param {number[][]} grid - * @return {number} - */ -const numDistinctIslands = function(grid) { - if (!grid.length) return 0; - const pattern = new Set(); - grid.forEach((rows, row) => { - rows.forEach((val, col) => { - if (val === 1) pattern.add(depthFirst(grid, row, col, "o")); - }); - }); - return pattern.size; -}; - -function depthFirst(graph, row, col, di) { - if (graph[row] && graph[row][col]) { - graph[row][col] = 0; - let p = - di + - depthFirst(graph, row + 1, col, "d") + - depthFirst(graph, row - 1, col, "u") + - depthFirst(graph, row, col + 1, "r") + - depthFirst(graph, row, col - 1, "l") + - "b"; - return p; - } else return ""; -} diff --git a/699-falling-squares.js b/699-falling-squares.js index aeebee63..2fd806a0 100644 --- a/699-falling-squares.js +++ b/699-falling-squares.js @@ -33,57 +33,3 @@ function getHeight(intervals, cur) { return cur.height } -// another - -/** - * @param {number[][]} positions - * @return {number[]} - */ -var fallingSquares = function (positions) { - let ranges = [{ left: 0, height: 0, right: 1e8 + 1e6 }], rtn = [], max = 0 - - outer: - for (let [left, length] of positions) { - let curHeight = 0, startI = -1, right = left + length, newRanges = [] - for (let i = 0; i < ranges.length; i++) { - let range = ranges[i] - if (left < range.right && startI == -1) { - startI = i - // left part - if (left != range.left) { - newRanges.push({ - left: range.left, - height: range.height, - right: left - }) - } - } - if (startI != -1) { - curHeight = Math.max(curHeight, range.height) - } - if (right <= range.right) { - // added part - let newHeight = length + curHeight - newRanges.push({ - left, - height: newHeight, - right, - }) - // right part - if (right != range.right) { - newRanges.push({ - left: right, - height: range.height, - right: range.right, - }) - } - max = Math.max(newHeight, max) - rtn.push(max) - // replace - ranges.splice(startI, i - startI + 1, ...newRanges) - continue outer - } - } - } - return rtn -}; diff --git a/7-reverse-integer.js b/7-reverse-integer.js index 4a2fef60..c628be7e 100755 --- a/7-reverse-integer.js +++ b/7-reverse-integer.js @@ -17,26 +17,3 @@ const reverse = function (x) { return res }; -// another - - -/** - * @param {number} x - * @return {number} - */ -const reverse = function(num) { - let negative = false; - let result = 0; - if (num < 0) { - negative = true; - num = Math.abs(num); - } - while (num > 0) { - mod = num % 10; // mod = 3 // mod = 2 // mod - num = Math.floor(num / 10); // num = 12 // num = 1 - result = result * 10 + mod; // 0 = 0 * 10 + 3 = 0 + 3 = 3 // 3 = 3 * 10 + 2 = 30 + 2 = 32 - } - if (result > 2147483647) return 0; - if (negative) return result * -1; - return result; -}; diff --git a/70-climbing-stairs.js b/70-climbing-stairs.js index a52c4c63..29760510 100755 --- a/70-climbing-stairs.js +++ b/70-climbing-stairs.js @@ -23,25 +23,3 @@ function single(i, hash) { return hash[i]; } -// another - -/** - * @param {number} n - * @return {number} - */ -const climbStairs = function (n) { - const dp = new Array(n + 1).fill(0) - if (n === 1) { - return 1 - } - if (n === 2) { - return 2 - } - dp[0] = 0 - dp[1] = 1 - dp[2] = 2 - for (let i = 3; i <= n; i++) { - dp[i] = dp[i - 1] + dp[i - 2] - } - return dp[n] -} diff --git a/701-insert-into-a-binary-search-tree.js b/701-insert-into-a-binary-search-tree.js index f6b8a655..89da9bda 100644 --- a/701-insert-into-a-binary-search-tree.js +++ b/701-insert-into-a-binary-search-tree.js @@ -31,38 +31,3 @@ const insertIntoBST = function(root, val) { return root; }; -// another - -const insertIntoBST = function(root, val) { - if (root == null) { - return new TreeNode(val); - } - if (root.val > val) { - root.left = insertIntoBST(root.left, val); - } else { - root.right = insertIntoBST(root.right, val); - } - return root; -}; - -// another - -/** - * Definition for a binary tree node. - * function TreeNode(val, left, right) { - * this.val = (val===undefined ? 0 : val) - * this.left = (left===undefined ? null : left) - * this.right = (right===undefined ? null : right) - * } - */ -/** - * @param {TreeNode} root - * @param {number} val - * @return {TreeNode} - */ -const insertIntoBST = function(root, val) { - if(root == null) return new TreeNode(val) - if(val < root.val) root.left = insertIntoBST(root.left, val) - else if(val > root.val) root.right = insertIntoBST(root.right, val) - return root -}; diff --git a/707-design-linked-list.js b/707-design-linked-list.js index 081e9b95..639485e3 100644 --- a/707-design-linked-list.js +++ b/707-design-linked-list.js @@ -72,129 +72,3 @@ MyLinkedList.prototype.deleteAtIndex = function(index) { * obj.deleteAtIndex(index) */ -// another - -/** - * Initialize your data structure here. - */ -var MyLinkedList = function(val) { - this.head = null - this.tail = null - this.size = 0 -} - -// Create Node class to store node data as an 'object' -var Node = function(val) { - this.val = val - this.next = null -} - -/** - * Get the value of the index-th node in the linked list. If the index is invalid, return -1. - * @param {number} index - * @return {number} - */ -MyLinkedList.prototype.get = function(index) { - if (index < 0 || this.size === 0 || index > this.size - 1) return -1 - let curr = this.head - let i = 0 - while (i < index) { - curr = curr.next - i += 1 - } - return curr.val -} - -/** - * Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list. - * @param {number} val - * @return {void} - */ -MyLinkedList.prototype.addAtHead = function(val) { - let newNode = new Node(val) - if (this.head === null) { - this.head = newNode - this.tail = newNode - } else { - newNode.next = this.head - this.head = newNode - } - this.size++ - return this -} - -/** - * Append a node of value val to the last element of the linked list. - * @param {number} val - * @return {void} - */ -MyLinkedList.prototype.addAtTail = function(val) { - const newNode = new Node(val) - if (this.head === null) { - this.head = newNode - this.tail = newNode - } else { - this.tail.next = newNode - this.tail = newNode - } - this.size++ - return this -} - -/** - * Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted. - * @param {number} index - * @param {number} val - * @return {void} - */ -MyLinkedList.prototype.addAtIndex = function(index, val) { - if (index > this.size) return - if (index <= 0) return this.addAtHead(val) - if (index === this.size) return this.addAtTail(val) - let newNode = new Node(val) - let i = 0 - let curr = this.head - while (i < index - 1) { - curr = curr.next - i++ - } - newNode.next = curr.next ? curr.next : null - curr.next = newNode - this.size++ - return this -} - -/** - * Delete the index-th node in the linked list, if the index is valid. - * @param {number} index - * @return {void} - */ -MyLinkedList.prototype.deleteAtIndex = function(index) { - if (index >= this.size || index < 0) return - if (index === 0) { - this.head = this.head.next - this.size-- - return this - } - let curr = this.head - let i = 0 - while (i < index - 1) { - i++ - curr = curr.next - } - curr.next = curr.next.next ? curr.next.next : null - if (!curr.next) this.tail = curr - this.size-- - return this -} - -/** - * Your MyLinkedList object will be instantiated and called as such: - * var obj = new MyLinkedList() - * var param_1 = obj.get(index) - * obj.addAtHead(val) - * obj.addAtTail(val) - * obj.addAtIndex(index,val) - * obj.deleteAtIndex(index) - */ - diff --git a/715-range-module.js b/715-range-module.js index 4c193227..ff229572 100644 --- a/715-range-module.js +++ b/715-range-module.js @@ -92,289 +92,3 @@ function SegmentTree() { } -// another - -const RangeModule = function() { - this.range = [] -} - -/** - * @param {number} left - * @param {number} right - * @return {void} - */ -RangeModule.prototype.addRange = function(left, right) { - let index1 = this.range.length - let low = 0 - let high = this.range.length - 1 - while (low <= high) { - const mid = (low + high) >> 1 - if (this.range[mid][1] >= left) { - index1 = Math.min(index1, mid) - high = mid - 1 - } else { - low = mid + 1 - } - } - - let index2 = -1 - low = 0 - high = this.range.length - 1 - while (low <= high) { - const mid = (low + high) >> 1 - if (this.range[mid][0] <= right) { - index2 = Math.max(index2, mid) - low = mid + 1 - } else { - high = mid - 1 - } - } - - if (index1 === this.range.length) { - this.range.push([left, right]) - return - } else if (index2 === -1) { - this.range.unshift([left, right]) - return - } - left = Math.min(left, this.range[index1][0]) - right = Math.max(right, this.range[index2][1]) - this.range.splice(index1, index2 - index1 + 1, [left, right]) -} - -/** - * @param {number} left - * @param {number} right - * @return {boolean} - */ -RangeModule.prototype.queryRange = function(left, right) { - let index = -1 - let low = 0 - let high = this.range.length - 1 - while (low <= high) { - const mid = (low + high) >> 1 - if (this.range[mid][0] <= left) { - index = Math.max(index, mid) - low = mid + 1 - } else { - high = mid - 1 - } - } - if (index === -1 || this.range[index][1] < right) { - return false - } - return true -} - -/** - * @param {number} left - * @param {number} right - * @return {void} - */ -RangeModule.prototype.removeRange = function(left, right) { - let index1 = this.range.length - let low = 0 - let high = this.range.length - 1 - while (low <= high) { - const mid = (low + high) >> 1 - if (this.range[mid][1] >= left) { - index1 = Math.min(index1, mid) - high = mid - 1 - } else { - low = mid + 1 - } - } - - let index2 = -1 - low = 0 - high = this.range.length - 1 - while (low <= high) { - const mid = (low + high) >> 1 - if (this.range[mid][0] <= right) { - index2 = Math.max(index2, mid) - low = mid + 1 - } else { - high = mid - 1 - } - } - - if (index1 === this.range.length || index2 === -1) { - return - } - - const newRange = [] - if (left > this.range[index1][0]) { - newRange.push([this.range[index1][0], left]) - } - if (right < this.range[index2][1]) { - newRange.push([right, this.range[index2][1]]) - } - this.range.splice(index1, index2 - index1 + 1, ...newRange) -} - -/** - * Your RangeModule object will be instantiated and called as such: - * var obj = new RangeModule() - * obj.addRange(left,right) - * var param_2 = obj.queryRange(left,right) - * obj.removeRange(left,right) - */ - - -// another - -const RangeModule = function () { - this.intervals = [] -} - -/** - * @param {number} left - * @param {number} right - * @return {void} - */ -RangeModule.prototype.addRange = function (left, right) { - const n = this.intervals.length - const tmp = [] - for (let i = 0; i <= n; i++) { - const cur = this.intervals[i] - - if (i == n || cur[0] > right) { - tmp.push([left, right]) - while (i < n) tmp.push(this.intervals[i++]) - } else if (cur[1] < left) tmp.push(cur) - else { - left = Math.min(left, cur[0]) - right = Math.max(right, cur[1]) - } - } - this.intervals = tmp -} - -/** - * @param {number} left - * @param {number} right - * @return {boolean} - */ -RangeModule.prototype.queryRange = function (left, right) { - const n = this.intervals.length - let l = 0, - r = n - 1 - while (l <= r) { - let m = ~~(l + (r - l) / 2) - if (this.intervals[m][0] >= right) r = m - 1 - else if (this.intervals[m][1] <= left) l = m + 1 - else return this.intervals[m][0] <= left && this.intervals[m][1] >= right - } - return false -} - -/** - * @param {number} left - * @param {number} right - * @return {void} - */ -RangeModule.prototype.removeRange = function (left, right) { - const n = this.intervals.length - const tmp = [] - for (let i = 0; i < n; i++) { - const cur = this.intervals[i] - if (cur[1] <= left || cur[0] >= right) tmp.push(cur) - else { - if (cur[0] < left) tmp.push([cur[0], left]) - if (cur[1] > right) tmp.push([right, cur[1]]) - } - } - this.intervals = tmp -} - -/** - * Your RangeModule object will be instantiated and called as such: - * var obj = new RangeModule() - * obj.addRange(left,right) - * var param_2 = obj.queryRange(left,right) - * obj.removeRange(left,right) - */ - -// another - -const RangeModule = function () { - this.intervals = [] -} - -/** - * @param {number} left - * @param {number} right - * @return {void} - */ -RangeModule.prototype.addRange = function (left, right) { - const tmp = [] - const n = this.intervals.length - for(let i = 0; i <= n; i++) { - const cur = this.intervals[i] - if(i === n || cur[0] > right) { - tmp.push([left, right]) - while(i < n) tmp.push(this.intervals[i++]) - }else if(cur[1] < left) { - tmp.push(cur) - }else { - // cur[0] <= right - // left <= cur[1] - left = Math.min(left, cur[0]) - right = Math.max(right, cur[1]) - } - } - // console.log(tmp) - this.intervals = tmp -} - -/** - * @param {number} left - * @param {number} right - * @return {boolean} - */ -RangeModule.prototype.queryRange = function (left, right) { - const n = this.intervals.length, arr = this.intervals - let l = 0, r = n - 1 - while(l <= r) { - const mid = ~~(l + (r - l) / 2) - if(arr[mid][0] >= right) r = mid - 1 - else if(arr[mid][1] <= left) l = mid + 1 - else return arr[mid][0] <= left && arr[mid][1] >= right - } - - return false -} - -/** - * @param {number} left - * @param {number} right - * @return {void} - */ -RangeModule.prototype.removeRange = function (left, right) { - const tmp = [] - const n = this.intervals.length - - for(let i = 0; i < n; i++) { - const cur = this.intervals[i] - if(cur[1] < left) { - tmp.push(cur) - }else if(cur[0] > right) tmp.push(cur) - else { - // left <= cur[1] - // cur[0] <= right - if(left > cur[0]) tmp.push([cur[0], left]) - if(right < cur[1]) tmp.push([right, cur[1]]) - } - } - // console.log(tmp) - this.intervals = tmp -} - -/** - * Your RangeModule object will be instantiated and called as such: - * var obj = new RangeModule() - * obj.addRange(left,right) - * var param_2 = obj.queryRange(left,right) - * obj.removeRange(left,right) - */ - diff --git a/719-find-k-th-smallest-pair-distance.js b/719-find-k-th-smallest-pair-distance.js index 63e8a619..354e832a 100644 --- a/719-find-k-th-smallest-pair-distance.js +++ b/719-find-k-th-smallest-pair-distance.js @@ -21,28 +21,3 @@ function smallestDistancePair(nums, k) { return l } -// another - -/** - * @param {number[]} nums - * @param {number} k - * @return {number} - */ -const smallestDistancePair = function(nums, k) { - nums.sort((a, b) => a - b) - let lo = 0 - let hi = nums[nums.length - 1] - nums[0] - while (lo < hi) { - let mi = Math.floor((lo + hi) / 2) - let count = 0 - let left = 0 - for (let right = 0; right < nums.length; right++) { - while (nums[right] - nums[left] > mi) left++ - count += right - left - } - //count = number of pairs with distance <= mi - if (count >= k) hi = mi - else lo = mi + 1 - } - return lo -} diff --git a/72-edit-distance.js b/72-edit-distance.js index f9e41c89..5fa327b9 100644 --- a/72-edit-distance.js +++ b/72-edit-distance.js @@ -24,31 +24,3 @@ const minDistance = function(word1, word2) { return dp[m][n]; }; -// another - -/** - * @param {string} word1 - * @param {string} word2 - * @return {number} - */ -const minDistance = function(word1, word2) { - const m = word1.length, n = word2.length - const dp = Array(n + 1).fill(0) - for(let i = 1; i <= n; i++) dp[i] = i - let pre = 0 - for(let i = 1; i <= m; i++) { - pre = dp[0] - dp[0] = i - for(let j = 1; j <= n; j++) { - let tmp = dp[j] - if(word1[i - 1] === word2[j - 1]) { - dp[j] = pre - } else { - dp[j] = Math.min(pre, dp[j], dp[j - 1]) + 1 - } - pre = tmp - } - } - - return dp[n] -}; diff --git a/723-candy-crush.js b/723-candy-crush.js index 1d5f7f56..5ffd78ee 100644 --- a/723-candy-crush.js +++ b/723-candy-crush.js @@ -83,57 +83,3 @@ const inflictGravity = function (board) { } } -// another - -/** - * @param {number[][]} board - * @return {number[][]} - */ -const candyCrush = function (board) { - const N = board.length, - M = board[0].length - let found = true - while (found) { - found = false - for (let i = 0; i < N; i++) { - for (let j = 0; j < M; j++) { - const val = Math.abs(board[i][j]) - if (val === 0) continue - if ( - j < M - 2 && - Math.abs(board[i][j + 1]) === val && - Math.abs(board[i][j + 2]) === val - ) { - found = true - let ind = j - while (ind < Math.min(M, j + 3) && Math.abs(board[i][ind]) === val) - board[i][ind++] = -val - } - if ( - i < N - 2 && - Math.abs(board[i + 1][j]) === val && - Math.abs(board[i + 2][j]) === val - ) { - found = true - let ind = i - while (ind < Math.min(N, i + 3) && Math.abs(board[ind][j]) === val) - board[ind++][j] = -val - } - } - } - if (found) { - // move positive values to the bottom, then set the rest to 0 - for (let j = 0; j < M; j++) { - let storeInd = N - 1 - for (let i = N - 1; i >= 0; i--) { - if (board[i][j] > 0) { - board[storeInd--][j] = board[i][j] - } - } - for (let k = storeInd; k >= 0; k--) board[k][j] = 0 - } - } - } - return board -} - diff --git a/727-minimum-window-subsequence.js b/727-minimum-window-subsequence.js index 635bc00f..77a9b830 100644 --- a/727-minimum-window-subsequence.js +++ b/727-minimum-window-subsequence.js @@ -32,131 +32,3 @@ const minWindow = function (s1, s2) { return len == n + 1 ? "" : S.substring(start, start + len); } -// another - -/** - * @param {string} s1 - * @param {string} s2 - * @return {string} - */ -const minWindow = function (s1, s2) { - let n1 = s1.length, - n2 = s2.length, - s1Idx = 0, - s2Idx = 0, - start = -1, - len = n1 + 1 - while (s1Idx < n1) { - if (s1[s1Idx] === s2[s2Idx]) { - if (s2Idx === n2 - 1) { - const end = s1Idx - while (s2Idx >= 0) { - while (s1[s1Idx] !== s2[s2Idx]) s1Idx-- - s2Idx-- - s1Idx-- - } - const tmp = end - s1Idx - if (tmp < len) { - len = tmp - start = s1Idx + 1 - } - s2Idx++ - s1Idx += 2 - } else { - s2Idx++ - s1Idx++ - } - } else s1Idx++ - } - return start === -1 ? '' : s1.slice(start, start + len) -} - -// another - -/** - * @param {string} s1 - * @param {string} s2 - * @return {string} - */ -const minWindow = function(s1, s2) { - let res = '', n = s1.length, m = s2.length - if(s1 === '' || s2 === '') return res - let minLen = Infinity - let right = 0 - while(right < n) { - let tIndex = 0 - while(right < n) { - if(s1[right] === s2[tIndex]) tIndex++ - if(tIndex === m) break - right++ - } - if(right === n) break - let left = right - tIndex = m - 1 - while(left >= 0) { - if(s1[left] === s2[tIndex]) { - tIndex-- - } - if(tIndex < 0) break - left-- - } - - if(right - left + 1 < minLen) { - minLen = right - left + 1 - res = s1.slice(left, right + 1) - } - right = left + 1 - } - - return res -}; - -// another - -/** - * @param {string} S - * @param {string} T - * @return {string} - */ -const minWindow = function (S, T) { - if (S.length === 0 || T.length === 0) { - return '' - } - let right = 0 - let minLen = Number.MAX_VALUE - let result = '' - while (right < S.length) { - let tIndex = 0 - while (right < S.length) { - if (S.charAt(right) === T.charAt(tIndex)) { - tIndex++ - } - if (tIndex === T.length) { - break - } - right++ - } - if (right === S.length) { - break - } - let left = right - tIndex = T.length - 1 - while (left >= 0) { - if (S.charAt(left) === T.charAt(tIndex)) { - tIndex-- - } - if (tIndex < 0) { - break - } - left-- - } - if (right - left + 1 < minLen) { - minLen = right - left + 1 - result = S.slice(left, right + 1) - } - // we have to move right pointer to the next position of left pointer, NOT the next position - // of right pointer - right = left + 1 - } - return result -} diff --git a/729-my-calendar-i.js b/729-my-calendar-i.js index 22425c94..223fd1d3 100644 --- a/729-my-calendar-i.js +++ b/729-my-calendar-i.js @@ -48,27 +48,3 @@ MyCalendar.prototype.book = function (start, end) { * var param_1 = obj.book(start,end) */ -// another - -const MyCalendar = function() { - this.s = new Set() -}; - -/** - * @param {number} start - * @param {number} end - * @return {boolean} - */ -MyCalendar.prototype.book = function(start, end) { - for(let e of this.s) { - if(Math.max(start, e[0]) < Math.min(end, e[1])) return false - } - this.s.add([start, end]) - return true -}; - -/** - * Your MyCalendar object will be instantiated and called as such: - * var obj = new MyCalendar() - * var param_1 = obj.book(start,end) - */ diff --git a/732-my-calendar-iii.js b/732-my-calendar-iii.js index ad5f21cc..b1974adc 100644 --- a/732-my-calendar-iii.js +++ b/732-my-calendar-iii.js @@ -42,79 +42,3 @@ MyCalendarThree.prototype.book = function(start, end) { * var param_1 = obj.book(start,end) */ -// another - - -var MyCalendarThree = function() { - this.st = new SegmentTree(0, 10 ** 9); -}; - -/** - * @param {number} start - * @param {number} end - * @return {number} - */ -MyCalendarThree.prototype.book = function(start, end) { - this.st.add(start, end); - return this.st.getMax(); -}; - -/** - * Your MyCalendarThree object will be instantiated and called as such: - * var obj = new MyCalendarThree() - * var param_1 = obj.book(start,end) - */ - -class SegmentTree { - constructor(start, end) { - this.root = new TreeNode(start, end); - } - - add(qs, qe, node=this.root) { - - // completely outside of query range - if(qs > node.end || qe <= node.start) { - return node.val; - } - - // completely covered by query range - if(qs <= node.start && qe > node.end) { - node.booked += 1; - node.val += 1; - return node.val; - } - - let mid = (node.start + node.end)/2 >> 0; - - if(!node.left) { - node.left = new TreeNode(node.start, mid); - } - - if(!node.right) { - node.right = new TreeNode(mid+1, node.end); - } - - node.val = Math.max( - this.add(qs, qe, node.left), - this.add(qs, qe, node.right), - ) + node.booked; - - return node.val; - - } - - getMax() { - return this.root.val; - } - -} - -class TreeNode { - constructor(start, end) { - this.start = start; - this.end = end; - this.val = 0; - this.booked = 0; - this.left = this.right = null; - } -} diff --git a/733-flood-fill.js b/733-flood-fill.js index 528c5f6e..2aa01d2c 100644 --- a/733-flood-fill.js +++ b/733-flood-fill.js @@ -27,46 +27,3 @@ const floodFill = function(image, sr, sc, newColor, firstColor = image[sr][sc]) return image } -// another - -/** - * @param {number[][]} image - * @param {number} sr - * @param {number} sc - * @param {number} newColor - * @return {number[][]} - */ -const floodFill = function ( - image, - sr, - sc, - newColor, - firstColor = image[sr][sc] -) { - const dirs = [0, -1, 0, 1, 0] - const rows = image.length - const cols = image[0].length - const q = [[sr, sc]] - while (q.length) { - const len = q.length - for (let i = 0; i < len; i++) { - const cur = q.shift() - image[cur[0]][cur[1]] = newColor - for (let j = 0; j < 4; j++) { - const [nr, nc] = [cur[0] + dirs[j], cur[1] + dirs[j + 1]] - if ( - nr >= 0 && - nr < rows && - nc >= 0 && - nc < cols && - image[nr][nc] === firstColor && - image[nr][nc] !== newColor - ) { - q.push([nr, nc]) - } - } - } - } - return image -} - diff --git a/735-asteroid-collision.js b/735-asteroid-collision.js index f48a26b0..f3e6f364 100644 --- a/735-asteroid-collision.js +++ b/735-asteroid-collision.js @@ -17,37 +17,3 @@ const asteroidCollision = function(asteroids) { return stk }; -// another - - -/** - * @param {number[]} asteroids - * @return {number[]} - */ -const asteroidCollision = function(asteroids) { - const positive = [] - const res = [] - for(let i = 0; i < asteroids.length; i++) { - if (asteroids[i] > 0) { - positive.push(i) - } else { - const negVal = asteroids[i]; - - while(positive.length > 0 && asteroids[ positive[positive.length - 1] ] + negVal < 0 ) { - asteroids[ positive[positive.length - 1] ] = undefined - positive.pop() - } - - if (positive.length > 0) { - if (asteroids[ positive[positive.length - 1] ] + negVal > 0) { - asteroids[i] = undefined - } else if(asteroids[ positive[positive.length - 1] ] + negVal === 0) { - asteroids[i] = undefined - asteroids[ positive[positive.length - 1] ] = undefined - positive.pop() - } - } - } - } - return asteroids.filter(el => el !== undefined) -}; diff --git a/736-parse-lisp-expression.js b/736-parse-lisp-expression.js index 71dd2132..6347e895 100644 --- a/736-parse-lisp-expression.js +++ b/736-parse-lisp-expression.js @@ -32,88 +32,3 @@ const e = (x, v = []) => }[x[0]]()), }[typeof x]()) -// another - -/** - * @param {string} expression - * @return {number} - */ -const evaluate = function (expression) { - const tokens = tokenizer(expression) - let i = 0 - function exec(scope) { - let value = null - const next = tokens[i++] - if (next === '(') { - scope = enter(scope) - switch (tokens[i++]) { - case 'add': - const a = exec(scope) - const b = exec(scope) - value = a + b - break - case 'mult': - const x = exec(scope) - const y = exec(scope) - value = x * y - break - case 'let': - while (tokens[i] !== '(' && tokens[i + 1] !== ')') { - scope.variables[tokens[i++]] = exec(scope) - } - value = exec(scope) - break - } - scope = exit(scope) - i++ - } else if (isNumber(next)) { - value = Number(next) - } else { - // Find variable in current scope otherwise go to parent - let t = scope - while (t) { - if (next in t.variables) { - value = t.variables[next] - break - } - t = t.parent - } - } - return value - } - return exec(newScope()) -} -function tokenizer(expression) { - const tokens = [] - let token = '' - for (const c of expression) { - if (c === '(' || c === ')') { - if (token) tokens.push(token) - tokens.push(c) - token = '' - } else if (c === ' ') { - if (token) tokens.push(token) - token = '' - } else { - token += c - } - } - if (token) { - tokens.push(token) - } - return tokens -} -function isNumber(n) { - return !isNaN(n) -} -function newScope() { - return { parent: null, variables: {} } -} -function enter(scope) { - const next = newScope() - next.parent = scope - return next -} -function exit(scope) { - return scope.parent -} diff --git a/743-network-delay-time.js b/743-network-delay-time.js index bf562c67..94314ef9 100644 --- a/743-network-delay-time.js +++ b/743-network-delay-time.js @@ -97,155 +97,3 @@ class PQ { } } -// another - -/** - * @param {number[][]} times - * @param {number} N - * @param {number} K - * @return {number} - */ -const networkDelayTime = function (times, N, K) { - const mins = new Array(N).fill(Infinity) - mins[K - 1] = 0 - for (let i = 0; i < N; i++) { - for (let [u, v, t] of times) { - if (mins[u - 1] === Infinity) continue - if (mins[v - 1] > mins[u - 1] + t) { - mins[v - 1] = mins[u - 1] + t - } - } - } - return mins.includes(Infinity) ? -1 : Math.max(...mins) -} - -// another - -/** - * @param {number[][]} times - * @param {number} N - * @param {number} K - * @return {number} - */ -const networkDelayTime = function(times, N, K) { - const distances = new Array(N).fill(Infinity); - distances[K - 1] = 0; - - for(let i = 0 ; i < N -1 ; i++){ - let counter = 0; - for(let j = 0 ; j < times.length ; j++){ - const source = times[j][0]; - const target = times[j][1]; - const weight = times[j][2]; - if(distances[source - 1] + weight < distances[target - 1]){ - distances[target - 1] = distances[source - 1] + weight; - counter++ - } - } - if(counter === 0) break - } - - const res = Math.max(...distances); - return res === Infinity ? -1 : res; -}; - -// another - -/** - * @param {number[][]} times - * @param {number} N - * @param {number} K - * @return {number} - */ -const networkDelayTime = function (times, N, K) { - const hash = {} - for(const [u, v, t] of times) { - if(hash[u] == null) hash[u] = {} - hash[u][v] = t - } - const pq = new PriorityQueue((a, b) => a[0] < b[0]) - pq.push([0, K]) - const visited = Array.from(N + 1) - let res = 0 - while(!pq.isEmpty()) { - const [dist, cur] = pq.pop() - if(visited[cur]) continue - visited[cur] = true - res = dist - N-- - if(hash[cur]) { - for(let next of Object.keys(hash[cur])) { - pq.push([dist + hash[cur][next], next]) - } - } - } - return N === 0 ? res : -1 -} - -class PriorityQueue { - constructor(comparator = (a, b) => a > b) { - this.heap = [] - this.top = 0 - this.comparator = comparator - } - size() { - return this.heap.length - } - isEmpty() { - return this.size() === 0 - } - peek() { - return this.heap[this.top] - } - push(...values) { - values.forEach((value) => { - this.heap.push(value) - this.siftUp() - }) - return this.size() - } - pop() { - const poppedValue = this.peek() - const bottom = this.size() - 1 - if (bottom > this.top) { - this.swap(this.top, bottom) - } - this.heap.pop() - this.siftDown() - return poppedValue - } - replace(value) { - const replacedValue = this.peek() - this.heap[this.top] = value - this.siftDown() - return replacedValue - } - - parent = (i) => ((i + 1) >>> 1) - 1 - left = (i) => (i << 1) + 1 - right = (i) => (i + 1) << 1 - greater = (i, j) => this.comparator(this.heap[i], this.heap[j]) - swap = (i, j) => ([this.heap[i], this.heap[j]] = [this.heap[j], this.heap[i]]) - siftUp = () => { - let node = this.size() - 1 - while (node > this.top && this.greater(node, this.parent(node))) { - this.swap(node, this.parent(node)) - node = this.parent(node) - } - } - siftDown = () => { - let node = this.top - while ( - (this.left(node) < this.size() && this.greater(this.left(node), node)) || - (this.right(node) < this.size() && this.greater(this.right(node), node)) - ) { - let maxChild = - this.right(node) < this.size() && - this.greater(this.right(node), this.left(node)) - ? this.right(node) - : this.left(node) - this.swap(node, maxChild) - node = maxChild - } - } -} diff --git a/748-shortest-completing-word.js b/748-shortest-completing-word.js index b791dcc9..64dbc291 100644 --- a/748-shortest-completing-word.js +++ b/748-shortest-completing-word.js @@ -26,40 +26,3 @@ const shortestCompletingWord = function(licensePlate, words) { return matchingWords[wordLengths.indexOf(Math.min.apply(Math, wordLengths))] } -// another - -/** - * @param {string} licensePlate - * @param {string[]} words - * @return {string} - */ -const shortestCompletingWord = function(licensePlate, words) { - licensePlate = licensePlate.toLowerCase() - const plateCount = Array(26).fill(0) - let plateLength = 0 - for (let i = 0; i < licensePlate.length; i += 1) { - const code = licensePlate.charCodeAt(i) - if (code < 97 || code > 122) { - continue - } - plateCount[code - 97] += 1 - plateLength += 1 - } - const longerOrEqualWords = words.filter(word => word.length >= plateLength) - return longerOrEqualWords.reduce((shortest, word) => { - if (shortest && shortest.length <= word.length) { - return shortest - } - const wordCount = Array(26).fill(0) - for (let i = 0; i < word.length; i += 1) { - const code = word.charCodeAt(i) - wordCount[code - 97] += 1 - } - for (let i = 0; i < 26; i += 1) { - if (wordCount[i] - plateCount[i] < 0) { - return shortest - } - } - return word - }, null) -} diff --git a/749-contain-virus.js b/749-contain-virus.js index 7a6bec2c..2b430f2f 100644 --- a/749-contain-virus.js +++ b/749-contain-virus.js @@ -79,183 +79,3 @@ const containVirus = function (grid) { } } -// another - -/** - * @param {number[][]} grid - * @return {number} - */ -const containVirus = (grid) => { - const m = grid.length; - const n = grid[0].length; - let ans = 0; - while (true) { - // list of regions can spread virus - const regions = []; - const visited = Array.from({ length: m }, () => Array(n).fill(false)); - for (let i = 0; i < m; i++) { - for (let j = 0; j < n; j++) { - if (grid[i][j] === 1 && !visited[i][j]) { - const region = new Region(); - dfs(grid, i, j, region, visited); - if (region.uninfected.size > 0) regions.push(region); - } - } - } - - if (regions.length === 0) break; - regions.sort((a, b) => a.uninfected.size - b.uninfected.size); - let idx = -1, wall = -Infinity - for(let i = 0, len = regions.length; i < len; i++) { - if(regions[i].uninfected.size > wall) { - wall = regions[i].uninfected.size - idx = i - } - } - const mostToBeInfected = regions[idx] - ans += mostToBeInfected.wallNeeded - regions.splice(idx, 1) - for (let x of mostToBeInfected.infected) { - let i = (x / n) >> 0, - j = x % n; - grid[i][j] = 2; - } - - for (let region of regions) { - for (let x of region.uninfected) { - let i = (x / n) >> 0, - j = x % n; - grid[i][j] = 1; - } - } - } - - return ans; - function dfs(grid, i, j, region, visited) { - if (i < 0 || i == m || j < 0 || j == n) return; - - if (grid[i][j] === 1 && !visited[i][j]) { - visited[i][j] = true; - region.infected.add(i * n + j); - dfs(grid, i - 1, j, region, visited); - dfs(grid, i + 1, j, region, visited); - dfs(grid, i, j - 1, region, visited); - dfs(grid, i, j + 1, region, visited); - } else if (grid[i][j] === 0) { - region.wallNeeded += 1; - region.uninfected.add(i * n + j); - } - } -}; -class Region { - constructor() { - this.wallNeeded = 0; - this.infected = new Set(); - this.uninfected = new Set(); - } -} - -// another - -/** - * @param {number[][]} grid - * @return {number} - */ -const containVirus = function (grid) { - const infected = 1 - const healthy = 0 - const quarantined = 2 - const directions = [ - [1, 0], - [-1, 0], - [0, 1], - [0, -1], - ] - const mod = 100 - const encode = (row, col) => row + col * mod - const decode = (num) => [num % mod, Math.floor(num / mod)] - const disjointSet = {} - for (let row = 0; row < grid.length; row++) { - for (let col = 0; col < grid[row].length; col++) { - const coord = encode(row, col) - disjointSet[coord] = coord - if (grid[row][col] === 0) continue - if (grid[row][col - 1] === 1) union(coord, encode(row, col - 1)) - if (row > 0 && grid[row - 1][col] === 1) - union(coord, encode(row - 1, col)) - } - } - let numWalls = 0 - while (true) { - const impact = quarantineAndContaminate() - if (impact === 0) return numWalls - numWalls += impact - spreadVirus() - } - function find(coord) { - return (disjointSet[coord] = - disjointSet[coord] === coord ? coord : find(disjointSet[coord])) - } - function union(coord, toCoord) { - return (disjointSet[find(coord)] = find(toCoord)) - } - function quarantineAndContaminate() { - const impact = new Map() - for (let row = 0; row < grid.length; row++) { - for (let col = 0; col < grid[row].length; col++) { - if (grid[row][col] !== infected) continue - const root = find(encode(row, col)) - if (!impact.has(root)) impact.set(root, new Set()) - for (let [down, right] of directions) { - if (grid[row + down] && grid[row + down][col + right] === healthy) { - impact.get(root).add(encode(row + down, col + right)) - } - } - } - } - let impactedCoords = new Set() - let root = null - for (let [node, coords] of impact) { - if (impactedCoords.size < coords.size) { - impactedCoords = coords - root = node - } - } - if (impactedCoords.size === 0) return 0 - return quarantine(...decode(root)) - } - function quarantine(row, col) { - if (row < 0 || row >= grid.length || col < 0 || col >= grid[0].length) - return 0 - if (grid[row][col] === 2) return 0 - if (grid[row][col] === 0) return 1 - let totalWalls = 0 - grid[row][col] = 2 - for (let [down, right] of directions) { - totalWalls += quarantine(row + down, col + right) - } - return totalWalls - } - function spreadVirus() { - const infectedCoords = new Set() - for (let row = 0; row < grid.length; row++) { - for (let col = 0; col < grid[row].length; col++) { - if (grid[row][col] !== healthy) continue - for (let [down, right] of directions) { - if (grid[row + down] && grid[row + down][col + right] === infected) { - infectedCoords.add(encode(row, col)) - } - } - } - } - for (let coord of infectedCoords) { - const [row, col] = decode(coord) - grid[row][col] = 1 - for (let [down, right] of directions) { - if (grid[row + down] && grid[row + down][col + right] === 1) { - union(coord, encode(row + down, col + right)) - } - } - } - } -} diff --git a/750-number-of-corner-rectangles.js b/750-number-of-corner-rectangles.js index 8f21c4a9..2247858f 100644 --- a/750-number-of-corner-rectangles.js +++ b/750-number-of-corner-rectangles.js @@ -16,39 +16,3 @@ const countCornerRectangles = function (grid) { return ans } -// another - -// optimized - -/** - * @param {number[][]} grid - * @return {number} - */ -const countCornerRectangles = function (grid) { - let ans = 0 - let largeLoopLen, smLoopLen, r - if(grid.length > grid[0].length) { - r = false - largeLoopLen = grid.length - smLoopLen = grid[0].length - } else { - r = true - largeLoopLen = grid[0].length - smLoopLen = grid.length - } - for (let i = 0; i < smLoopLen - 1; i++) { - for (let j = i + 1; j < smLoopLen; j++) { - let counter = 0 - for (let k = 0; k < largeLoopLen; k++) { - if(r) { - if (grid[i][k] === 1 && grid[j][k] === 1) counter++ - } else { - if (grid[k][i] === 1 && grid[k][j] === 1) counter++ - } - - } - ans += (counter * (counter - 1)) / 2 - } - } - return ans -} diff --git a/755-pour-water.js b/755-pour-water.js index 1cacf38d..4ae60f4e 100644 --- a/755-pour-water.js +++ b/755-pour-water.js @@ -26,31 +26,3 @@ const pourWater = function (heights, V, K) { return pourWater(heights, V - 1, K) } -// another - -/** - * @param {number[]} heights - * @param {number} V - * @param {number} K - * @return {number[]} - */ -const pourWater = function (heights, V, K) { - let cur = K - for (let i = 0; i < V; i++) { - // Move left - while (cur > 0 && heights[cur - 1] <= heights[cur]) { - cur-- - } - // Move right - while (cur < heights.length - 1 && heights[cur + 1] <= heights[cur]) { - cur++ - } - // Move left to K - while(cur > K && heights[cur - 1] === heights[cur]) { - cur-- - } - heights[cur]++ - } - return heights -} - diff --git a/757-set-intersection-size-at-least-two.js b/757-set-intersection-size-at-least-two.js index c89077ec..4c890c22 100644 --- a/757-set-intersection-size-at-least-two.js +++ b/757-set-intersection-size-at-least-two.js @@ -22,62 +22,3 @@ const intersectionSizeTwo = function(intervals) { }, 0); }; -// another - -/** - * @param {number[][]} intervals - * @return {number} - */ -const intersectionSizeTwo = function(intervals) { - intervals.sort((a, b) => a[1] - b[1]); - - let n = intervals.length; - if (n === 0) return 0; - - let count = 2; - let last = intervals[0][1]; - let sec_last = intervals[0][1] - 1; - - for (let i = 1; i < n; i++) { - if (intervals[i][0] <= sec_last) continue; - else if (intervals[i][0] <= last) { - sec_last = last; - last = intervals[i][1]; - count++; - } else { - last = intervals[i][1]; - sec_last = intervals[i][1] - 1; - count += 2; - } - } - return count; -}; - -// another - -/** - * @param {number[][]} intervals - * @return {number} - */ -const intersectionSizeTwo = function (intervals) { - if (intervals.length === 1) return 2 - intervals.sort((a, b) => (a[1] !== b[1] ? a[1] - b[1] : b[0] - a[0])) - let right = intervals[0][1] - let left = right - 1 - let result = 2 - for (let i = 1, len = intervals.length; i < len; i++) { - const curr = intervals[i] - if (curr[0] <= right && curr[0] > left) { - result++ - left = right - right = curr[1] - } else if (curr[0] > right) { - result += 2 - left = curr[1] - 1 - right = curr[1] - } - } - - return result -} - diff --git a/759-employee-free-time.js b/759-employee-free-time.js index 1bb666d6..78f6c014 100644 --- a/759-employee-free-time.js +++ b/759-employee-free-time.js @@ -92,90 +92,3 @@ function merge(A, B) { } -// another - -const employeeFreeTime = function(schedule) { - const intervals = [] - schedule.forEach(s => s.forEach(t => intervals.push(t))) - intervals.sort((a, b) => - a.start !== b.start ? a.start - b.start : a.end - b.end - ) - let i1 = intervals[0] - const res = [] - for (let interval of intervals.slice(1)) { - let i2 = interval - if (i1.end >= i2.start) { - i1.end = Math.max(i1.end, i2.end) - } else { - res.push(new Interval(i1.end, i2.start)) - i1 = i2 - } - } - return res -} - -// another - -/** - * // Definition for an Interval. - * function Interval(start, end) { - * this.start = start; - * this.end = end; - * }; - */ - -/** - * @param {Interval[][]} schedule - * @return {Interval[]} - */ -const employeeFreeTime = function (schedule) { - const res = [], timeLine = [] - schedule.forEach(e => timeLine.push(...e)) - timeLine.sort((a, b) => a.start - b.start) - let tmp = timeLine[0] - for(let i = 1, n = timeLine.length; i < n; i++) { - const el = timeLine[i] - if(el.start > tmp.end) { - res.push(new Interval(tmp.end, el.start)) - tmp = el - } else { - tmp = el.end > tmp.end ? el : tmp - } - } - return res -} - -// another - -/** - * // Definition for an Interval. - * function Interval(start, end) { - * this.start = start; - * this.end = end; - * }; - */ - -/** - * @param {Interval[][]} schedule - * @return {Interval[]} - */ -var employeeFreeTime = function(schedule) { - const arr = schedule.reduce((ac, e) => { - ac.push(...e) - return ac - }, []) - arr.sort((a, b) => a.start - b.start || b.end - a.end) - const n = arr.length - const res = [] - let end = arr[0].end - for(let i = 1; i < n; i++) { - const cur = arr[i] - if(cur.start > end) { - res.push(new Interval(end, cur.start)) - } - - end = Math.max(end, cur.end) - } - - return res -}; diff --git a/765-couples-holding-hands.js b/765-couples-holding-hands.js index 3b5aa4d9..13dbbf7d 100644 --- a/765-couples-holding-hands.js +++ b/765-couples-holding-hands.js @@ -25,82 +25,3 @@ function swap(arr, i, j) { ;[arr[i], arr[j]] = [arr[j], arr[i]] } -// another - -/** - * @param {number[]} row - * @return {number} - */ -const minSwapsCouples = function (row) { - const parents = Array.from({ length: row.length / 2 }, (_, i) => i) - const positions = new Map() - for (let i = 0; i < row.length / 2; i++) { - const left = Math.floor(row[i * 2] / 2) - const right = Math.floor(row[i * 2 + 1] / 2) - if (positions.has(left)) { - union(i, positions.get(left)) - } else { - positions.set(left, i) - } - if (positions.has(right)) { - union(i, positions.get(right)) - } else { - positions.set(right, i) - } - } - - const uniqueRoots = new Set() - for (const parent of parents) { - uniqueRoots.add(find(parent)) - } - return parents.length - uniqueRoots.size - - function union(a, b) { - const aRoot = find(a) - const bRoot = find(b) - parents[aRoot] = bRoot - } - function root(x) { - while (x !== parents[x]) { - parents[x] = parents[parents[x]] - x = parents[x] - } - return x - } - function find(node) { - return root(node) - } -} - -// another - -/** - * @param {number[]} row - * @return {number} - */ -const minSwapsCouples = function (row) { - let res = 0 - const n = row.length - const ptn = Array(n).fill(0), pos = Array(n).fill(0) - - for(let i = 0; i < n; i++) { - ptn[i] = (i % 2 === 0 ? i + 1 : i - 1) - pos[row[i]] = i - } - - for (let i = 0; i < n ;i++) { - for (let j = ptn[pos[ptn[row[i]]]]; i != j; j = ptn[pos[ptn[row[i]]]]) { - swap(row, i, j); - swap(pos, row[i], row[j]); - res++; - } - } - - return res - - function swap(arr, i, j) { - const val = arr[i] - arr[i] = arr[j] - arr[j] = val - } -} diff --git a/767-reorganize-string.js b/767-reorganize-string.js index be449e84..758188f9 100644 --- a/767-reorganize-string.js +++ b/767-reorganize-string.js @@ -39,82 +39,3 @@ const reorganizeString = function (s) { return res.join('') } -// another - - -/** - * @param {string} S - * @return {string} - */ -const reorganizeString = function(S) { - if (!S || S.length <= 1) { - return S; - } - const freqs = Array(26).fill(0); - const acode = 'a'.charCodeAt(0); - for (let i = 0, n = S.length; i < n; i++) { - const index = S.charCodeAt(i) - acode; - freqs[index]++; - if (freqs[index] > Math.ceil(n / 2)) { - return ''; - } - } - const list = []; - for (let i = 0, n = S.length; i < 26; i++) { - if (freqs[i] === 0) { - continue; - } - list.push({ch: String.fromCharCode(i + acode), freq: freqs[i]}); - } - list.sort((l1, l2) => l2.freq - l1.freq); - const parts = []; - for (let i = 0, n = list[0].freq; i < n; i++) { - parts.push(list[0].ch); - } - let idx = 0; - for (let i = 1, n = list.length; i < n; i++) { - for (let j = 0, m = list[i].freq; j < m; j++) { - idx %= list[0].freq; - parts[idx++] += list[i].ch; - } - } - return parts.join(''); -}; - -// another - -/** - * @param {string} s - * @return {string} - */ -const reorganizeString = function(s) { - const arr = Array(26).fill(0), a = 'a'.charCodeAt(0) - for(let ch of s) arr[ch.charCodeAt(0) - a]++ - let max = 0, idx = -1 - for(let i = 0; i < 26; i++) { - if(arr[i] > max) { - max = arr[i] - idx = i - } - } - const n = s.length - const res = Array(n) - if(max > (n + 1) / 2) return '' - - let i = 0 - while(arr[idx] > 0) { - res[i] = String.fromCharCode(a + idx) - i += 2 - arr[idx]-- - } - - for(let j = 0; j < 26; j++) { - while(arr[j]) { - if(i >= n) i = 1 - res[i] = String.fromCharCode(a + j) - i += 2 - arr[j]-- - } - } - return res.join('') -}; diff --git a/774-minimize-max-distance-to-gas-station.js b/774-minimize-max-distance-to-gas-station.js index 20ee5131..36387977 100644 --- a/774-minimize-max-distance-to-gas-station.js +++ b/774-minimize-max-distance-to-gas-station.js @@ -29,104 +29,3 @@ const possible = (dis, res, K) => { return need <= K } -// another - -/** - * @param {number[]} stations - * @param {number} k - * @return {number} - */ -const minmaxGasDist = function(stations, k) { - const pq = new PriorityQueue((a, b) => a[0] > b[0]) - for(let i = 1, n = stations.length; i < n; i++) { - const delta = stations[i] - stations[i - 1] - pq.push([delta, delta, 1]) - } - const limit = (stations[stations.length - 1] - stations[0]) / k - // console.log(pq.heap) - while(k>0) { - let [delta, gap, num] = pq.pop() - - let v = gap / (num + 1) - while(k > 0 && gap / (num + 1) > limit) { - k-- - num++ - } - - v = gap / ++num - k-- - - pq.push([v, gap, num]) - } - - return pq.peek()[0] -}; - - -class PriorityQueue { - constructor(comparator = (a, b) => a > b) { - this.heap = [] - this.top = 0 - this.comparator = comparator - } - size() { - return this.heap.length - } - isEmpty() { - return this.size() === 0 - } - peek() { - return this.heap[this.top] - } - push(...values) { - values.forEach((value) => { - this.heap.push(value) - this.siftUp() - }) - return this.size() - } - pop() { - const poppedValue = this.peek() - const bottom = this.size() - 1 - if (bottom > this.top) { - this.swap(this.top, bottom) - } - this.heap.pop() - this.siftDown() - return poppedValue - } - replace(value) { - const replacedValue = this.peek() - this.heap[this.top] = value - this.siftDown() - return replacedValue - } - - parent = (i) => ((i + 1) >>> 1) - 1 - left = (i) => (i << 1) + 1 - right = (i) => (i + 1) << 1 - greater = (i, j) => this.comparator(this.heap[i], this.heap[j]) - swap = (i, j) => ([this.heap[i], this.heap[j]] = [this.heap[j], this.heap[i]]) - siftUp = () => { - let node = this.size() - 1 - while (node > this.top && this.greater(node, this.parent(node))) { - this.swap(node, this.parent(node)) - node = this.parent(node) - } - } - siftDown = () => { - let node = this.top - while ( - (this.left(node) < this.size() && this.greater(this.left(node), node)) || - (this.right(node) < this.size() && this.greater(this.right(node), node)) - ) { - let maxChild = - this.right(node) < this.size() && - this.greater(this.right(node), this.left(node)) - ? this.right(node) - : this.left(node) - this.swap(node, maxChild) - node = maxChild - } - } -} diff --git a/775-global-and-local-inversions.js b/775-global-and-local-inversions.js index 1616fc00..62f0d7c5 100644 --- a/775-global-and-local-inversions.js +++ b/775-global-and-local-inversions.js @@ -12,16 +12,3 @@ const isIdealPermutation = function(A) { return true; }; -// another - -/** - * @param {number[]} A - * @return {boolean} - */ -const isIdealPermutation = function(A) { - if(A.length === 1 || A.length === 2) return true - for(let i = 0, len = A.length; i < len; i++) { - if(Math.abs(A[i] - i) > 1) return false - } - return true; -}; diff --git a/778-swim-in-rising-water.js b/778-swim-in-rising-water.js index 43f52c67..1b874e30 100644 --- a/778-swim-in-rising-water.js +++ b/778-swim-in-rising-water.js @@ -40,86 +40,3 @@ function dfs(grid, visited, dir, waterHeight, row, col, n) { return false; } -// another - -/** - * @param {number[][]} grid - * @return {number} - */ -const dirs = [[-1, 0], [1, 0], [0, 1], [0, -1]]; -const swimInWater = grid => { - let time = 0; - let N = grid.length; - const visited = new Set(); - while (!visited.has(N * N - 1)) { - visited.clear(); - dfs(grid, 0, 0, time, visited); - time++; - } - return time - 1; -}; - -function dfs(grid, i, j, time, visited) { - if ( - i < 0 || - i > grid.length - 1 || - j < 0 || - j > grid[0].length - 1 || - grid[i][j] > time || - visited.has(i * grid.length + j) - ) - return; - visited.add(i * grid.length + j); - for (let dir of dirs) { - dfs(grid, i + dir[0], j + dir[1], time, visited); - } -} - - -// another - -/** - * @param {number[][]} grid - * @return {number} - */ -class UnionFind { - constructor(N) { - this.id = []; - for (let i = 0; i < N; i++) { - this.id[i] = i; - } - } - - root(i) { - while (i != this.id[i]) { - this.id[i] = this.id[this.id[i]]; - i = this.id[i]; - } - return i; - } - isConnected(p, q) { - return this.root(p) === this.root(q); - } - union(p, q) { - if (this.isConnected(p, q)) return; - this.id[this.root(p)] = this.root(q); - } -} -const swimInWater = grid => { - const N = grid.length; - const uf = new UnionFind(N * N); - let time = 0; - while (!uf.isConnected(0, N * N - 1)) { - for (let i = 0; i < N; i++) { - for (let j = 0; j < N; j++) { - if (grid[i][j] > time) continue; - if (i < N - 1 && grid[i + 1][j] <= time) - uf.union(i * N + j, i * N + j + N); - if (j < N - 1 && grid[i][j + 1] <= time) - uf.union(i * N + j, i * N + j + 1); - } - } - time++; - } - return time - 1; -}; diff --git a/78-subsets.js b/78-subsets.js index 5467e85d..0b45c7fd 100755 --- a/78-subsets.js +++ b/78-subsets.js @@ -22,21 +22,3 @@ function subsets(nums) { console.log(subsets([1, 2, 3])); -// another - -/** - * @param {number[]} nums - * @return {number[][]} - */ - -function subsets(nums) { - const subs = [[]] - for (let num of nums) { - const n = subs.length - for (let i = 0; i < n; i++) { - subs.push(subs[i].slice(0)) - subs[subs.length - 1].push(num) - } - } - return subs -} diff --git a/787-cheapest-flights-within-k-stops.js b/787-cheapest-flights-within-k-stops.js index c0cd2f99..b95ebbdb 100644 --- a/787-cheapest-flights-within-k-stops.js +++ b/787-cheapest-flights-within-k-stops.js @@ -21,28 +21,3 @@ const findCheapestPrice = function(n, flights, src, dst, K) { return mn[dst] != Infinity ? mn[dst] : -1 } -// another -const findCheapestPrice = function(n, flights, src, dst, K) { - const map = []; - flights.forEach(([s, d, p]) => { - map[s] = map[s] || []; - map[s][d] = p; - }); - let min = Infinity; - const p = find(src, 0, 0); - return p === Infinity ? -1 : p; - - function find(s, p, c) { - if (s === dst) { - return p; - } - const l = map[s]; - if (c > K || p > min || !l) { - return Infinity; - } - l.forEach((p1, d) => { - min = Math.min(min, find(d, p + p1, c + 1)); - }); - return min; - } -}; diff --git a/79-word-search.js b/79-word-search.js index d109fa4b..69308415 100644 --- a/79-word-search.js +++ b/79-word-search.js @@ -38,42 +38,3 @@ function dfs(board, x, y, dirs, word, start) { // time complexity: O(M * N * 3^L), where L is the length of word. // we have a visited array and we never go back, so 3 directions -// another - -/** - * @param {character[][]} board - * @param {string} word - * @return {boolean} - */ -const exist = function(board, word) { - if (!word || !board || board.length === 0) return false - const dirs = [[-1, 0], [1, 0], [0, -1], [0, 1]] - for (let row = 0; row < board.length; row++) { - for (let col = 0; col < board[row].length; col++) { - if (searchWord(board, row, col, word, 0, dirs)) return true - } - } - return false -} - -const searchWord = (board, row, col, word, widx, dirs) => { - if (widx === word.length) return true - if ( - row < 0 || - col < 0 || - row === board.length || - col === board[0].length || - board[row][col] === null || - board[row][col] !== word[widx] - ) return false - - const ch = board[row][col] - board[row][col] = null // mark visited - - for (let dir of dirs) { - if (searchWord(board, row + dir[0], col + dir[1], word, widx + 1, dirs)) { - return true - } - } - board[row][col] = ch // recover -} diff --git a/792-number-of-matching-subsequences.js b/792-number-of-matching-subsequences.js index 0fc3d939..d3bc7ee6 100644 --- a/792-number-of-matching-subsequences.js +++ b/792-number-of-matching-subsequences.js @@ -29,59 +29,3 @@ const numMatchingSubseq = function(s, words) { return res }; -// another - -/** - * @param {string} S - * @param {string[]} words - * @return {number} - */ -const numMatchingSubseq = function(S, words) { - let res=0; - for(let i=0;i right) { - dp = 0 - prev = i - } else { - dp = i - prev - res += dp - } - } - return res -}; diff --git a/798-smallest-rotation-with-highest-score.js b/798-smallest-rotation-with-highest-score.js index a76fca03..1eb13553 100644 --- a/798-smallest-rotation-with-highest-score.js +++ b/798-smallest-rotation-with-highest-score.js @@ -26,24 +26,3 @@ const bestRotation = function(A) { return ans } -// another - -/** - * @param {number[]} nums - * @return {number} - */ -var bestRotation = function(nums) { - const n = nums.length - const arr = Array(n).fill(0) - for(let i = 0; i < n; i++) { - arr[(i - nums[i] + 1 + n) % n] -= 1 - } - let res = 0 - for(let i = 1; i < n; i++) { - arr[i] += arr[i - 1] + 1 - if(arr[i] > arr[res]) res = i - } - return res -}; - - diff --git a/802-find-eventual-safe-states.js b/802-find-eventual-safe-states.js index f1d74624..17c44edc 100644 --- a/802-find-eventual-safe-states.js +++ b/802-find-eventual-safe-states.js @@ -28,59 +28,3 @@ const eventualSafeNodes = function (graph) { return q } -// another - - -/** - * @param {number[][]} graph - * @return {number[]} - */ -const eventualSafeNodes = function(graph) { - const n = graph.length, memo = {}, visited = new Set(), res = [] - for(let i = 0; i < n; i++) { - if(!dfs(graph, i, memo, visited)) res.push(i) - } - return res -}; - -function dfs(graph, node, memo, visited) { - if(memo[node] != null) return memo[node] - let hasCycle = false - visited.add(node) - for(let e of graph[node]) { - if(visited.has(e) || dfs(graph, e, memo, visited)) { - hasCycle = true - break - } - } - visited.delete(node) - memo[node] = hasCycle - return hasCycle -} - -// another - -/** - * @param {number[][]} graph - * @return {number[]} - */ -const eventualSafeNodes = function(graph) { - const res = [] - if(graph == null || graph.length === 0) return res - const n = graph.length - const color = Array(n).fill(0) - for(let i = 0; i < n; i++) { - if(bt(graph, i, color)) res.push(i) - } - return res - - function bt(graph, start, color) { - if(color[start] !== 0) return color[start] === 1 - color[start] = 2 - for(let next of graph[start]) { - if(!bt(graph, next, color)) return false - } - color[start] = 1 - return true - } -}; diff --git a/803-bricks-falling-when-hit.js b/803-bricks-falling-when-hit.js index 36c09fe0..819ad42d 100644 --- a/803-bricks-falling-when-hit.js +++ b/803-bricks-falling-when-hit.js @@ -67,187 +67,3 @@ const getConnectedCount = (grid, i, j) => { return count } -// another - -/** - * @param {number[][]} grid - * @param {number[][]} hits - * @return {number[]} - */ -const hitBricks = function (grid, hits) { - const SPACE = 0 - const BRICK = 1 - const WILL_HIT = 2 - const DIRECTIONS = [ - [0, 1], - [1, 0], - [0, -1], - [-1, 0], - ] - const rows = grid.length - const cols = grid[0].length - const ds = new DisjointSet(rows * cols + 1) - - for (const [hitR, hitC] of hits) { - if (grid[hitR][hitC] === BRICK) { - grid[hitR][hitC] = WILL_HIT - } - } - - function hash(r, c) { - return r * cols + c + 1 - } - - function unionAround(r, c) { - const hashed = hash(r, c) - for (const [rDiff, cDiff] of DIRECTIONS) { - const rNext = r + rDiff - const cNext = c + cDiff - if (grid[rNext] !== undefined && grid[rNext][cNext] === BRICK) { - ds.union(hashed, hash(rNext, cNext)) - } - } - if (r === 0) ds.union(0, hashed) - } - for (let i = 0; i < grid.length; i++) { - for (let j = 0; j < grid[i].length; j++) { - if (grid[i][j] === BRICK) unionAround(i, j) - } - } - let numBricksLeft = ds.size[ds.find(0)] - const numBricksDropped = new Array(hits.length) - // backwards - for (let i = hits.length - 1; i >= 0; i--) { - const [hitR, hitC] = hits[i] - if (grid[hitR][hitC] === WILL_HIT) { - grid[hitR][hitC] = BRICK - unionAround(hitR, hitC) - const newNumBricksLeft = ds.size[ds.find(0)] - numBricksDropped[i] = Math.max(newNumBricksLeft - numBricksLeft - 1, 0) - numBricksLeft = newNumBricksLeft - } else { - numBricksDropped[i] = 0 - } - } - return numBricksDropped -} - -class DisjointSet { - constructor(n) { - this.size = new Array(n).fill(1) - this.parent = new Array(n) - for (let i = 0; i < n; i++) { - this.parent[i] = i - } - } - find(x) { - if (x === this.parent[x]) return x - this.parent[x] = this.find(this.parent[x]) - - return this.parent[x] - } - union(x, y) { - const rootX = this.find(x) - const rootY = this.find(y) - if (rootX !== rootY) { - // attach X onto Y - this.parent[rootX] = rootY - this.size[rootY] += this.size[rootX] - } - } -} - -// another - -/** - * @param {number[][]} grid - * @param {number[][]} hits - * @return {number[]} - */ -const hitBricks = function(grid, hits) { - const res = Array(hits.length).fill(0), dirs = [-1, 0, 1, 0, -1] - for(let [r, c] of hits) { - grid[r][c] -= 1 - } - for(let i = 0; i < grid[0].length; i++) { - dfs(0, i, grid) - } - for(let i = hits.length - 1; i >= 0; i--) { - const [r, c] = hits[i] - grid[r][c] += 1 - if(grid[r][c] === 1 && isConnected(r, c, grid, dirs)) { - res[i] = dfs(r, c, grid) - 1 - } - } - return res -} -function dfs(i, j, grid) { - if(i < 0 || i >= grid.length || j < 0 || j >= grid[0].length || grid[i][j] !== 1) return 0 - grid[i][j] = 2 - return 1 + dfs(i + 1, j, grid) + dfs(i - 1, j, grid) + dfs(i, j + 1, grid) + dfs(i, j - 1, grid) -} -function isConnected(i, j, grid, dirs) { - if(i === 0) return true - for(let k = 1; k < dirs.length; k++) { - const r = i + dirs[k - 1], c = j + dirs[k] - if(r >= 0 && r < grid.length && c >= 0 && c < grid[0].length && grid[r][c] === 2) { - return true - } - } - return false -} - -// another - -/** - * @param {number[][]} grid - * @param {number[][]} hits - * @return {number[]} - */ -const hitBricks = function (grid, hits) { - const m = grid.length, - n = grid[0].length, - res = Array(hits.length).fill(0), - dirs = [ - [-1, 0], - [1, 0], - [0, 1], - [0, -1], - ]; - for (let [r, c] of hits) grid[r][c] -= 1; - - for (let i = 0; i < n; i++) dfs(grid, 0, i, m, n, dirs); - - for (let i = hits.length - 1; i >= 0; i--) { - const [r, c] = hits[i]; - grid[r][c] += 1; - if (grid[r][c] === 1 && connected(grid, r, c, m, n, dirs)) { - res[i] = dfs(grid, r, c, m, n, dirs) - 1; - } - } - - return res; -}; - -function dfs(grid, i, j, m, n, dirs) { - if (i < 0 || i >= m || j < 0 || j >= n || grid[i][j] !== 1) return 0; - grid[i][j] = 2; - let res = 1; - for (let [dr, dc] of dirs) { - res += dfs(grid, i + dr, j + dc, m, n, dirs); - } - return res; -} - -function connected(grid, i, j, m, n, dirs) { - if (i === 0) return true; - for (let [dr, dc] of dirs) { - const nr = i + dr, - nc = j + dc; - if (nr >= 0 && nr < m && nc >= 0 && nc < n && grid[nr][nc] === 2) - return true; - } - - return false; -} - diff --git a/828-unique-letter-string.js b/828-unique-letter-string.js index 62fa088a..6f91ed88 100644 --- a/828-unique-letter-string.js +++ b/828-unique-letter-string.js @@ -20,44 +20,3 @@ const uniqueLetterString = function(s) { return res }; -// another - -/** - * @param {string} S - * @return {number} - */ -const uniqueLetterString = function(S) { - const s = S.split('') - let res = 0 - for (let n = S.length, i = 0, l = 0, r = 0; i < n; i++) { - for (l = i - 1; l >= 0 && s[l] != s[i]; l--); - for (r = i + 1; r < n && s[r] != s[i]; r++); - res += (r - i) * (i - l) - } - return res % (10 ** 9 + 7) -} - -// another - -/** - * @param {string} S - * @return {number} - */ -const uniqueLetterString = function(S) { - const len = S.length - if (len === 0) return 0 - if (len === 1) return 1 - let count = 0 - let lastP = new Array(26).fill(0) - let lastLastP = new Array(26).fill(0) - let pre = 0 - for (let i = 0; i < len; i++) { - let idx = S.charCodeAt(i) - 'A'.charCodeAt(0) - pre += i - lastP[idx] + 1 - pre -= lastP[idx] - lastLastP[idx] - count += pre - lastLastP[idx] = lastP[idx] - lastP[idx] = i + 1 - } - return count % 1000000007 -} diff --git a/829-consecutive-numbers-sum.js b/829-consecutive-numbers-sum.js index e2d5e98f..139bbfb9 100644 --- a/829-consecutive-numbers-sum.js +++ b/829-consecutive-numbers-sum.js @@ -10,20 +10,3 @@ const consecutiveNumbersSum = function (N) { return count } -// another - -/** - * @param {number} N - * @return {number} - */ -const consecutiveNumbersSum = function(N) { - let res = 0 - for(let i = 1; i <= N; i++) { - const diff = i * (i - 1) / 2 - const nd = N - diff - if(nd <= 0) break - if(nd % i === 0) res++ - } - - return res -}; diff --git a/83-remove-duplicates-from-sorted-list.js b/83-remove-duplicates-from-sorted-list.js index 6339d4e7..4fc25216 100755 --- a/83-remove-duplicates-from-sorted-list.js +++ b/83-remove-duplicates-from-sorted-list.js @@ -21,30 +21,3 @@ const deleteDuplicates = function(head) { return head; }; -// another - -/** - * Definition for singly-linked list. - * function ListNode(val, next) { - * this.val = (val===undefined ? 0 : val) - * this.next = (next===undefined ? null : next) - * } - */ -/** - * @param {ListNode} head - * @return {ListNode} - */ -const deleteDuplicates = function(head) { - let prev = null, cur = head - while(cur) { - if(prev && prev.val === cur.val) { - prev.next = cur.next - cur = cur.next - } else { - prev = cur - cur = cur.next - } - } - return head -}; - diff --git a/84-largest-rectangle-in-histogram.js b/84-largest-rectangle-in-histogram.js index a05263fe..e27acd17 100644 --- a/84-largest-rectangle-in-histogram.js +++ b/84-largest-rectangle-in-histogram.js @@ -36,46 +36,3 @@ const largestRectangleArea = function(heights) { }; -// another - -/** - * @param {number[]} heights - * @return {number} - */ -const largestRectangleArea = function(heights) { - if (!heights.length) return 0; - let stack = []; - let max = 0; - for (let i = 0, cur, len = heights.length; i <= len; i++) { - cur = i === len ? -1 : heights[i]; - while (stack.length && cur < heights[stack[stack.length - 1]]) { - let index = stack.pop(); - let h = heights[index]; - let w = !stack.length ? i : i - stack[stack.length - 1] - 1; - max = Math.max(max, h * w); - } - stack.push(i); - } - return max; -}; - -// another - -/** - * @param {number[]} heights - * @return {number} - */ -const largestRectangleArea = function(heights) { - heights.push(0) - const st = [], n = heights.length - let res = 0 - for(let i = 0; i <= n; i++) { - while(st.length && heights[st[st.length - 1]] >= heights[i]) { - const top = st.pop() - const pre = st.length ? st[st.length - 1] : -1 - res = Math.max(res, heights[top] * (i - pre - 1)) - } - st.push(i) - } - return res -}; diff --git a/85-maximal-rectangle.js b/85-maximal-rectangle.js index fd57a2d6..d0e314b7 100644 --- a/85-maximal-rectangle.js +++ b/85-maximal-rectangle.js @@ -39,81 +39,3 @@ const maximalRectangle = function(matrix) { return res }; -// another - - -/** - * @param {character[][]} matrix - * @return {number} - */ -const maximalRectangle = function(matrix) { - if(matrix.length === 0) return 0; - const m = matrix.length; // rows - const n = matrix[0].length; // cols - const left = new Array(n).fill(0) - const right = new Array(n).fill(n) - const height = new Array(n).fill(0); - let maxA = 0; - for(let i = 0; i < m; i++) { - let cur_left = 0, cur_right = n; - // compute height (can do this from either side) - for(let j = 0; j < n; j++) { - if(matrix[i][j] === '1') height[j]++; - else height[j] = 0; - } - // compute left (from left to right) - for(let j = 0; j < n; j++) { - if(matrix[i][j] ==='1') left[j] = Math.max(left[j], cur_left); - else {left[j] = 0; cur_left = j + 1;} - } - // compute right (from right to left) - for(let j = n - 1; j >= 0; j--) { - if(matrix[i][j] === '1') right[j] = Math.min(right[j], cur_right); - else {right[j] = n; cur_right = j;} - } - // compute the area of rectangle (can do this from either side) - for(let j = 0; j < n; j++) { - maxA = Math.max(maxA, (right[j] - left[j]) * height[j]); - } - } - return maxA; -}; - -// another - -/** - * @param {character[][]} matrix - * @return {number} - */ -const maximalRectangle = function(matrix) { - if (matrix == null || matrix.length === 0 || matrix[0] == null || matrix[0].length === 0) return 0; - let m = matrix.length, n = matrix[0].length, maxArea = 0; - - const left = new Array(n).fill(0) - const right = new Array(n).fill(n - 1) - const height = new Array(n).fill(0) - for (let i = 0; i < m; i++) { - let rB = n - 1; - for (let j = n - 1; j >= 0; j--) { - if (matrix[i][j] === '1') { - right[j] = Math.min(right[j], rB); - } else { - right[j] = n - 1; - rB = j - 1; - } - } - let lB = 0; - for (let j = 0; j < n; j++) { - if (matrix[i][j] === '1') { - left[j] = Math.max(left[j], lB); - height[j]++; - maxArea = Math.max(maxArea, height[j] * (right[j] - left[j] + 1)); - } else { - height[j] = 0; - left[j] = 0; - lB = j + 1; - } - } - } - return maxArea; -}; diff --git a/857-minimum-cost-to-hire-k-workers.js b/857-minimum-cost-to-hire-k-workers.js index 76914b01..6ce4b01c 100644 --- a/857-minimum-cost-to-hire-k-workers.js +++ b/857-minimum-cost-to-hire-k-workers.js @@ -29,70 +29,3 @@ const mincostToHireWorkers = function(quality, wage, K) { -// another - -/** - * @param {number[]} quality - * @param {number[]} wage - * @param {number} K - * @return {number} - */ -const mincostToHireWorkers = function(quality, wage, K) { - const workers = Array.from({length: quality.length}, () => new Array(2)); - for (let i = 0; i < quality.length; ++i) workers[i] = [wage[i] / quality[i], quality[i]]; - workers.sort((a, b) => a[0] - b[0]) - let res = Number.MAX_VALUE, qsum = 0; - const queue = [] - for (let worker of workers) { - qsum += worker[1]; - insert(queue, -worker[1]) - if (queue.length > K) qsum += queue.shift(); - if (queue.length === K) res = Math.min(res, qsum * worker[0]); - } - return res; -}; - -function insert(arr, el) { - for(let i = 0; i < arr.length; i++) { - if(arr[i] > el) { - arr.splice(i, 0, el) - return - } - } - arr.push(el) -} - -// another - -/** - * @param {number[]} quality - * @param {number[]} wage - * @param {number} K - * @return {number} - */ -const mincostToHireWorkers = function(quality, wage, K) { - const workers = [], n = wage.length - for(let i = 0; i < n; i++) { - workers.push([wage[i] / quality[i], quality[i]]) - } - // wage[i] / wage[j] = quality[i] / quality[j] - // wage[i] * quality[j] = wage[j] * quality[i] - // wage[i] / quality[i] = wage[j] / quality[j] - workers.sort((a, b) => a[0] - b[0]) - const pq = new MaxPriorityQueue({ priority: (w) => w.quality }) - let res = Infinity, qualitySum = 0 - for(const worker of workers) { - const [ratio, quality] = worker - qualitySum += quality - pq.enqueue({ quality }) - - if(pq.size() > K) { - qualitySum -= pq.dequeue().element.quality - } - if(pq.size() === K) res = Math.min(res, qualitySum * ratio) - } - - return res -}; - - diff --git a/86-partition-list.js b/86-partition-list.js index d326dfa9..ca6afd8b 100644 --- a/86-partition-list.js +++ b/86-partition-list.js @@ -34,50 +34,3 @@ const partition = function(head, x) { return leftHead.next; }; -// another - -/** - * Definition for singly-linked list. - * function ListNode(val) { - * this.val = val; - * this.next = null; - * } - */ -/** - * @param {ListNode} head - * @param {number} x - * @return {ListNode} - */ -const partition = function(head, x) { - const left = [] - const right = [] - let containX = false - let cur = head - while(cur !== null) { - if (containX === true) { - if (cur.val < x) { - left.push(cur) - } else { - right.push(cur) - } - } else { - if (cur.val >= x) { - containX = true - right.push(cur) - } else { - left.push(cur) - } - } - cur = cur.next - } - const arr = left.concat(right) - - for(let i = 0; i < arr.length; i++) { - if (i === arr.length - 1) { - arr[i].next = null - } else { - arr[i].next = arr[i+1] - } - } - return arr[0] == null ? null : arr[0] -}; diff --git a/861-score-after-flipping-matrix.js b/861-score-after-flipping-matrix.js index 4c57cb6c..a7268107 100644 --- a/861-score-after-flipping-matrix.js +++ b/861-score-after-flipping-matrix.js @@ -17,51 +17,3 @@ const matrixScore = function(grid) { return res }; -// another - -/** - * @param {number[][]} grid - * @return {number} - */ -const matrixScore = function(grid) { - const m = grid.length, n = grid[0].length - for(let i = 0; i < m; i++) { - if(grid[i][0] === 0) flipRow(i) - } - - for(let i = 0; i < n; i++) { - if(cntCol(i, 0) > cntCol(i, 1)) flipCol(i) - } - - let res = 0 - // console.log(grid) - for(const row of grid) { - res += parseInt(row.join(''), 2) - } - - return res - - - function flipRow(idx) { - for(let i = 0; i < n; i++) { - if(grid[idx][i] === 0) grid[idx][i] = 1 - else grid[idx][i] = 0 - } - } - - function cntCol(idx, target) { - let res = 0 - for(let i = 0; i < m; i++) { - if(grid[i][idx] === target) res++ - } - // console.log(res) - return res - } - - function flipCol(idx) { - for(let i = 0; i < m; i++) { - if(grid[i][idx] === 0) grid[i][idx] = 1 - else grid[i][idx] = 0 - } - } -}; diff --git a/862-shortest-subarray-with-sum-at-least-k.js b/862-shortest-subarray-with-sum-at-least-k.js index e09e2839..095c1844 100644 --- a/862-shortest-subarray-with-sum-at-least-k.js +++ b/862-shortest-subarray-with-sum-at-least-k.js @@ -26,32 +26,3 @@ const shortestSubarray = function(nums, k) { return res === Infinity ? -1 : res }; -// another - -/** - * @param {number[]} A - * @param {number} K - * @return {number} - */ -const shortestSubarray = function(A, K) { - const N = A.length - const P = new Array(N+1).fill(0) - - for(let i = 0; i < N; i++) { - P[i+1] = P[i] + A[i] - } - - let ans = N + 1 - const monoq = [] - for(let y = 0; y < P.length; y++) { - while(monoq.length > 0 && P[y] <= P[monoq[monoq.length - 1]] ) { - monoq.pop() - } - while(monoq.length > 0 && P[y] >= P[monoq[0]] + K ) { - ans = Math.min(ans, y - monoq.shift()) - } - monoq.push(y) - } - - return ans < N + 1 ? ans : -1 -}; diff --git a/863-all-nodes-distance-k-in-binary-tree.js b/863-all-nodes-distance-k-in-binary-tree.js index 51fc2e8f..dd5f0d13 100644 --- a/863-all-nodes-distance-k-in-binary-tree.js +++ b/863-all-nodes-distance-k-in-binary-tree.js @@ -53,51 +53,3 @@ const distanceK = function(root, target, K) { } }; -// another - -/** - * Definition for a binary tree node. - * function TreeNode(val) { - * this.val = val; - * this.left = this.right = null; - * } - */ -/** - * @param {TreeNode} root - * @param {TreeNode} target - * @param {number} K - * @return {number[]} - */ -const distanceK = function(root, target, K) { - let res = [] - dfs(root, target, K, res) - return res -} - -function dfs(node, target, k, res) { - if (node === null) return -1 - if (node === target) { - getRes(node, 0, k, res) - return 1 - } - let left = dfs(node.left, target, k, res) - let right = dfs(node.right, target, k, res) - if (left !== -1) { - if (left === k) res.push(node.val) - getRes(node.right, left + 1, k, res) - return left + 1 - } - if (right !== -1) { - if (right === k) res.push(node.val) - getRes(node.left, right + 1, k, res) - return right + 1 - } - return -1 -} - -function getRes(node, dist, k, res) { - if (node === null) return - if (dist === k) return res.push(node.val) - getRes(node.left, dist + 1, k, res) - getRes(node.right, dist + 1, k, res) -} diff --git a/865-smallest-subtree-with-all-the-deepest-nodes.js b/865-smallest-subtree-with-all-the-deepest-nodes.js index cea0b7a0..eee29d20 100755 --- a/865-smallest-subtree-with-all-the-deepest-nodes.js +++ b/865-smallest-subtree-with-all-the-deepest-nodes.js @@ -27,36 +27,3 @@ function result(node, dist) { this.dist = dist; } -// another - -/** - * Definition for a binary tree node. - * function TreeNode(val, left, right) { - * this.val = (val===undefined ? 0 : val) - * this.left = (left===undefined ? null : left) - * this.right = (right===undefined ? null : right) - * } - */ -/** - * @param {TreeNode} root - * @return {TreeNode} - */ -const subtreeWithAllDeepest = function(root) { - let res = null, maxDepth = 0 - dfs(root, 0) - - return res - - function dfs(node, depth) { - if(node == null) return depth - 1 - - const left = dfs(node.left, depth + 1) - const right = dfs(node.right, depth + 1) - maxDepth = Math.max(maxDepth, left, right) - - if(left === maxDepth && right === maxDepth) { - res = node - } - return Math.max(left, right) - } -}; diff --git a/866-prime-palindrome.js b/866-prime-palindrome.js index 78d70c50..2379eaab 100644 --- a/866-prime-palindrome.js +++ b/866-prime-palindrome.js @@ -21,53 +21,3 @@ const primePalindrome = function(n) { } }; -// another - -/** - * @param {number} N - * @return {number} - */ -const primePalindrome = function(N) { - if(N >= 8 && N <= 11) return 11 - for(let x = 1; x < 100000; x++) { - let s = '' + x, r = s.split('').reverse().join('') - let y = +(s + r.slice(1)) - if(y >= N && isPrime(y)) return y - } - return -1 -}; - -function isPrime(x) { - if(x < 2 || x % 2 === 0) return x === 2 - for(let i = 3; i * i <= x; i += 2) { - if(x % i === 0) return false - } - return true -} - -// another - - -/** - * @param {number} n - * @return {number} - */ -const primePalindrome = function(n) { - if(n >= 8 && n <= 11) return 11 - - const rev = num => `${num}`.split('').reverse().join('') - for(let i = 1; i < 1e5; i++) { - let left = i, right = rev(left).slice(1) - const tmp = +(left + right) - if(tmp >= n && isPrime(tmp)) return tmp - } - - function isPrime(num) { - if(num <= 2) return num === 2 - if(num % 2 === 0) return false - for(let i = 3; i ** 2 <= num; i += 2) { - if(num % i === 0) return false - } - return true - } -}; diff --git a/878-nth-magical-number.js b/878-nth-magical-number.js index 5442069e..d82d18de 100644 --- a/878-nth-magical-number.js +++ b/878-nth-magical-number.js @@ -22,21 +22,3 @@ const nthMagicalNumber = function(N, A, B) { return res % (1e9 + 7) } -// another - -const nthMagicalNumber = function(N, A, B) { - const gcd = (x, y) => { - if (x == 0) return y - return gcd(y % x, x) - } - const MOD = 1e9 + 7 - const L = (A / gcd(A, B)) * B - let lo = 0 - let hi = 1e15 - while (lo < hi) { - let mi = lo + Math.trunc((hi - lo) / 2) - if (Math.trunc(mi / A) + Math.trunc(mi / B) - Math.trunc(mi / L) < N) lo = mi + 1 - else hi = mi - } - return lo % MOD -} diff --git a/882-reachable-nodes-in-subdivided-graph.js b/882-reachable-nodes-in-subdivided-graph.js index f83ea6f7..e28e9115 100644 --- a/882-reachable-nodes-in-subdivided-graph.js +++ b/882-reachable-nodes-in-subdivided-graph.js @@ -89,222 +89,3 @@ class Heap { } } -// another - -/** - * @param {number[][]} edges - * @param {number} M - * @param {number} N - * @return {number} - */ -const reachableNodes = function (edges, M, N) { - const graph = Array.from({ length: N }, () => Array(N).fill(-1)) - for (let edge of edges) { - graph[edge[0]][edge[1]] = edge[2] - graph[edge[1]][edge[0]] = edge[2] - } - let result = 0 - const pq = new PriorityQueue((a, b) => a[1] > b[1]) - const visited = new Array(N).fill(false) - pq.push([0, M]) - while (!pq.isEmpty()) { - const cur = pq.pop() - const start = cur[0] - const move = cur[1] - if (visited[start]) { - continue - } - visited[start] = true - result++ - for (let i = 0; i < N; i++) { - if (graph[start][i] > -1) { - if (move > graph[start][i] && !visited[i]) { - pq.push([i, move - graph[start][i] - 1]) - } - graph[i][start] -= Math.min(move, graph[start][i]) - result += Math.min(move, graph[start][i]) - } - } - } - return result -} - -class PriorityQueue { - constructor(comparator = (a, b) => a > b) { - this.heap = [] - this.top = 0 - this.comparator = comparator - } - size() { - return this.heap.length - } - isEmpty() { - return this.size() === 0 - } - peek() { - return this.heap[this.top] - } - push(...values) { - values.forEach((value) => { - this.heap.push(value) - this.siftUp() - }) - return this.size() - } - pop() { - const poppedValue = this.peek() - const bottom = this.size() - 1 - if (bottom > this.top) { - this.swap(this.top, bottom) - } - this.heap.pop() - this.siftDown() - return poppedValue - } - replace(value) { - const replacedValue = this.peek() - this.heap[this.top] = value - this.siftDown() - return replacedValue - } - - parent = (i) => ((i + 1) >>> 1) - 1 - left = (i) => (i << 1) + 1 - right = (i) => (i + 1) << 1 - greater = (i, j) => this.comparator(this.heap[i], this.heap[j]) - swap = (i, j) => ([this.heap[i], this.heap[j]] = [this.heap[j], this.heap[i]]) - siftUp = () => { - let node = this.size() - 1 - while (node > this.top && this.greater(node, this.parent(node))) { - this.swap(node, this.parent(node)) - node = this.parent(node) - } - } - siftDown = () => { - let node = this.top - while ( - (this.left(node) < this.size() && this.greater(this.left(node), node)) || - (this.right(node) < this.size() && this.greater(this.right(node), node)) - ) { - let maxChild = - this.right(node) < this.size() && - this.greater(this.right(node), this.left(node)) - ? this.right(node) - : this.left(node) - this.swap(node, maxChild) - node = maxChild - } - } -} - -// another - -// time complexity: -// Dijkstra + Heap is O(E log E) -// worst case: O(N ^ 2 * log (N ^ 2)) -/** - * @param {number[][]} edges - * @param {number} M - * @param {number} N - * @return {number} - */ -const reachableNodes = function(edges, M, N) { - const graph = {} - for(const [u,v,c] of edges) { - if(graph[u] == null) graph[u] = {} - if(graph[v] == null) graph[v] = {} - graph[u][v] = c - graph[v][u] = c - } - const pq = new PriorityQueue((a, b) => a[0] > b[0]) - pq.push([M, 0]) - const visited = {} - while(!pq.isEmpty()) { - const [moves, i] = pq.pop() - if(visited[i] == null) { - visited[i] = moves - for(const k of Object.keys(graph[i] || {})) { - const remain = moves - graph[i][k] - 1 - if(visited[k] == null && remain >= 0) { - pq.push([remain, k]) - } - } - } - } - let res = 0 - res += Object.keys(visited).length - for(const [u, v, c] of edges) { - const a = visited[u] || 0, b = visited[v] || 0 - res += Math.min(a + b, c) - } - - return res -}; - -class PriorityQueue { - constructor(comparator = (a, b) => a > b) { - this.heap = [] - this.top = 0 - this.comparator = comparator - } - size() { - return this.heap.length - } - isEmpty() { - return this.size() === 0 - } - peek() { - return this.heap[this.top] - } - push(...values) { - values.forEach((value) => { - this.heap.push(value) - this.siftUp() - }) - return this.size() - } - pop() { - const poppedValue = this.peek() - const bottom = this.size() - 1 - if (bottom > this.top) { - this.swap(this.top, bottom) - } - this.heap.pop() - this.siftDown() - return poppedValue - } - replace(value) { - const replacedValue = this.peek() - this.heap[this.top] = value - this.siftDown() - return replacedValue - } - - parent = (i) => ((i + 1) >>> 1) - 1 - left = (i) => (i << 1) + 1 - right = (i) => (i + 1) << 1 - greater = (i, j) => this.comparator(this.heap[i], this.heap[j]) - swap = (i, j) => ([this.heap[i], this.heap[j]] = [this.heap[j], this.heap[i]]) - siftUp = () => { - let node = this.size() - 1 - while (node > this.top && this.greater(node, this.parent(node))) { - this.swap(node, this.parent(node)) - node = this.parent(node) - } - } - siftDown = () => { - let node = this.top - while ( - (this.left(node) < this.size() && this.greater(this.left(node), node)) || - (this.right(node) < this.size() && this.greater(this.right(node), node)) - ) { - let maxChild = - this.right(node) < this.size() && - this.greater(this.right(node), this.left(node)) - ? this.right(node) - : this.left(node) - this.swap(node, maxChild) - node = maxChild - } - } -} diff --git a/886-possible-bipartition.js b/886-possible-bipartition.js index 0d1748fa..126b144c 100644 --- a/886-possible-bipartition.js +++ b/886-possible-bipartition.js @@ -34,38 +34,3 @@ const possibleBipartition = function (N, dislikes) { return true } -// another - -/** - * @param {number} N - * @param {number[][]} dislikes - * @return {boolean} - */ -const possibleBipartition = function (N, dislikes) { - const graph = new Array(N + 1) - for (const [a, b] of dislikes) { - if (!graph[a]) graph[a] = [] - graph[a].push(b) - if (!graph[b]) graph[b] = [] - graph[b].push(a) - } - - const colors = new Array(N + 1) - const dfs = (node, color = 0) => { - colors[node] = color - const nextColor = color ^ 1 - const children = graph[node] || [] - for (const child of children) { - if (colors[child] !== undefined) { - if (colors[child] !== nextColor) return false - } else { - if (!dfs(child, nextColor)) return false - } - } - return true - } - for (let i = 1; i <= N; i++) { - if (colors[i] === undefined && !dfs(i)) return false - } - return true -} diff --git a/887-super-egg-drop.js b/887-super-egg-drop.js index cf5a5eb5..aebeb367 100644 --- a/887-super-egg-drop.js +++ b/887-super-egg-drop.js @@ -28,39 +28,3 @@ function f(x, K, N) { return ans } -// another - -const superEggDrop = function(K, N) { - const dp = Array.from({ length: K + 1 }, () => new Array(N + 1).fill(0)) - return helper(K, N, dp) -} -function helper(K, N, memo) { - if (N <= 1) { - return N - } - if (K === 1) { - return N - } - if (memo[K][N] > 0) { - return memo[K][N] - } - - let low = 1, - high = N, - result = N - while (low < high) { - let mid = Math.floor(low + (high - low) / 2) - let left = helper(K - 1, mid - 1, memo) - let right = helper(K, N - mid, memo) - result = Math.min(result, Math.max(left, right) + 1) - if (left === right) { - break - } else if (left < right) { - low = mid + 1 - } else { - high = mid - } - } - memo[K][N] = result - return result -} diff --git a/89-gray-code.js b/89-gray-code.js index 862a618a..28e15b40 100644 --- a/89-gray-code.js +++ b/89-gray-code.js @@ -15,34 +15,3 @@ const grayCode = function(n) { return nums } -// another - -/** - * @param {number} n - * @return {number[]} - */ -const grayCode = function(n) { - const arr = [] - arr.push(0) - for (let i = 0; i < n; i++) { - let inc = 1 << i - for (let j = arr.length - 1; j >= 0; j--) { - arr.push(arr[j] + inc) - } - } - return arr -} - -// another - -/** - * @param {number} n - * @return {number[]} - */ -const grayCode = function(n) { - return n - ? (x => [...x, ...x.map((v, i) => x[x.length - 1 - i] + x.length)])( - grayCode(n - 1) - ) - : [0] -} diff --git a/893-groups-of-special-equivalent-strings.js b/893-groups-of-special-equivalent-strings.js index 79e402f6..e68ecd39 100644 --- a/893-groups-of-special-equivalent-strings.js +++ b/893-groups-of-special-equivalent-strings.js @@ -15,21 +15,3 @@ const numSpecialEquivGroups = function(A) { ).size; }; -// another - -/** - * @param {string[]} A - * @return {number} - */ -const numSpecialEquivGroups = function(A) { - const result = new Set(); - for (let i of A) { - let arr = i.split(""); - let res = [[], []]; - for (let j = 0; j < arr.length; j++) { - res[j & 1].push(arr[j]); - } - result.add(res[0].sort().join("") + res[1].sort().join("")); - } - return result.size; -}; diff --git a/896-monotonic-array.js b/896-monotonic-array.js index 03a547f6..ef3d3cf4 100644 --- a/896-monotonic-array.js +++ b/896-monotonic-array.js @@ -21,17 +21,3 @@ function dec(nums) { return true } -// another - -/** - * @param {number[]} nums - * @return {boolean} - */ -const isMonotonic = function(nums) { - let inc = true, dec = true - for(let i = 1, n = nums.length; i < n; i++) { - inc &= nums[i] >= nums[i - 1] - dec &= nums[i] <= nums[i - 1] - } - return inc || dec -}; diff --git a/902-numbers-at-most-n-given-digit-set.js b/902-numbers-at-most-n-given-digit-set.js index 2e5da545..74b1d68b 100644 --- a/902-numbers-at-most-n-given-digit-set.js +++ b/902-numbers-at-most-n-given-digit-set.js @@ -30,32 +30,3 @@ const atMostNGivenDigitSet = function(digits, n) { } }; -// another - - -/** - * @param {string[]} digits - * @param {number} n - * @return {number} - */ -const atMostNGivenDigitSet = function(digits, n) { - const str = '' + n, { pow } = Math - const len = str.length, dsize = digits.length - let res = 0 - - for(let i = 1; i < len; i++) { - res += pow(dsize, i) - } - - for(let i = 0; i < len; i++) { - let sameNum = false - for(const d of digits) { - if(+d < +str[i]) { - res += pow(dsize, len - i - 1) - } else if(+d === +str[i]) sameNum = true - } - if(sameNum === false) return res - } - - return res + 1 -}; diff --git a/903-valid-permutations-for-di-sequence.js b/903-valid-permutations-for-di-sequence.js index 13844712..05cf40bf 100644 --- a/903-valid-permutations-for-di-sequence.js +++ b/903-valid-permutations-for-di-sequence.js @@ -36,55 +36,3 @@ const numPermsDISequence = function(s) { }; -// another - - -/** - * @param {string} s - * @return {number} - */ -const numPermsDISequence = function(s) { - const n = s.length, mod = 1e9 + 7 - const dp = Array.from({ length: n + 1}, () => Array(n + 1).fill(0)) - dp[0][0] = 1 - for(let i = 1; i <= n; i++) { - for(let j = 0; j <= i; j++) { - if(s[i - 1] === 'D') { - for(let k = j; k <= i - 1; k++) { - dp[i][j] = (dp[i][j] + dp[i - 1][k]) % mod - } - } else { - for(let k = 0; k < j; k++) { - dp[i][j] = (dp[i][j] + dp[i - 1][k]) % mod - } - } - } - } - let res = 0 - for(let i = 0; i <= n; i++) { - res = (res + dp[n][i]) % mod - } - - return res -}; - -// another - -/** - * @param {string} S - * @return {number} - */ -const numPermsDISequence = function(S) { - let n = S.length, - mod = 10 ** 9 + 7 - const dp = Array.from({ length: n + 1 }, () => new Array(n + 1).fill(0)) - for (let j = 0; j <= n; j++) dp[0][j] = 1 - for (let i = 0; i < n; i++) - if (S.charAt(i) === 'I') - for (let j = 0, cur = 0; j < n - i; j++) - dp[i + 1][j] = cur = (cur + dp[i][j]) % mod - else - for (let j = n - i - 1, cur = 0; j >= 0; j--) - dp[i + 1][j] = cur = (cur + dp[i][j + 1]) % mod - return dp[n][0] -} diff --git a/906-super-palindromes.js b/906-super-palindromes.js index 9b97b889..a3e9844c 100644 --- a/906-super-palindromes.js +++ b/906-super-palindromes.js @@ -111,90 +111,3 @@ const isPalindrome = function (nr) { return nr === nr.split('').reverse().join('') } -// another - -/** - * @param {string} left - * @param {string} right - * @return {number} - */ - const superpalindromesInRange = function(left, right) { - const palindromes = [] - let res = 0 - for(let i = 1; i < 10; i++) { - palindromes.push(`${i}`) - } - for(let i = 1; i < 1e4; i++) { - let l = `${i}`, r = l.split('').reverse().join('') - palindromes.push(`${l}${r}`) - for(let j = 0; j < 10; j++) { - palindromes.push(`${l}${j}${r}`) - } - } - - for(let p of palindromes) { - const square = BigInt(p) * BigInt(p) - if(!isPalindrome(`${square}`)) continue - if(BigInt(left) <= square && square <= BigInt(right)) res++ - } - - return res - - function isPalindrome(str) { - let i = 0; - let j = str.length - 1; - while (i < j) { - if (str.charAt(i) !== str.charAt(j)) { - return false; - } - i++; - j--; - } - return true; - } -}; - -// another - -/** - * @param {string} left - * @param {string} right - * @return {number} - */ -const superpalindromesInRange = function (left, right) { - let ans = 9 >= left && 9 <= right ? 1 : 0 - - const isPal = (str) => { - for (let i = 0, j = str.length - 1; i < j; i++, j--) - if (str.charAt(i) !== str.charAt(j)) return false - return true - } - - for (let dig = 1; dig < 10; dig++) { - let isOdd = dig % 2 && dig !== 1, - innerLen = (dig >> 1) - 1, - innerLim = Math.max(1, 2 ** innerLen), - midPos = dig >> 1, - midLim = isOdd ? 3 : 1 - for (let edge = 1; edge < 3; edge++) { - let pal = new Uint8Array(dig) - ;(pal[0] = edge), (pal[dig - 1] = edge) - if (edge === 2) (innerLim = 1), (midLim = Math.min(midLim, 2)) - for (let inner = 0; inner < innerLim; inner++) { - if (inner > 0) { - let innerStr = inner.toString(2).padStart(innerLen, '0') - for (let i = 0; i < innerLen; i++) - (pal[1 + i] = innerStr[i]), (pal[dig - 2 - i] = innerStr[i]) - } - for (let mid = 0; mid < midLim; mid++) { - if (isOdd) pal[midPos] = mid - let palin = ~~pal.join(''), - square = BigInt(palin) * BigInt(palin) - if (square > right) return ans - if (square >= left && isPal(square.toString())) ans++ - } - } - } - } - return ans -} diff --git a/907-sum-of-subarray-minimums.js b/907-sum-of-subarray-minimums.js index 52e0a139..55c9c425 100644 --- a/907-sum-of-subarray-minimums.js +++ b/907-sum-of-subarray-minimums.js @@ -28,42 +28,3 @@ return res } -// another - -/** - * @param {number[]} arr - * @return {number} - */ -const sumSubarrayMins = function (arr) { - const n = arr.length, - s1 = [], - s2 = [], - left = Array(n), - right = Array(n) - for (let i = 0; i < n; i++) { - let cnt = 1 - while (s1.length && s1[s1.length - 1][0] > arr[i]) { - cnt += s1.pop()[1] - } - left[i] = cnt - s1.push([arr[i], cnt]) - } - - for (let i = n - 1; i >= 0; i--) { - let cnt = 1 - while (s2.length && s2[s2.length - 1][0] >= arr[i]) { - cnt += s2.pop()[1] - } - right[i] = cnt - s2.push([arr[i], cnt]) - } - let res = 0 - const mod = 1e9 + 7 - for (let i = 0; i < n; i++) { - // left[i] number of starting positions - // right[i] number of ending positions - res = (res + arr[i] * left[i] * right[i]) % mod - } - - return res -} diff --git a/91-decode-ways.js b/91-decode-ways.js index 300145d2..282d4a69 100644 --- a/91-decode-ways.js +++ b/91-decode-ways.js @@ -20,25 +20,3 @@ const numDecodings = function(s) { }; -// another - -/** - * @param {string} s - * @return {number} - */ -const numDecodings = function(s) { - if (s.length === 0) return 0 - const N = s.length - const dp = Array(N + 1).fill(0) - dp[0] = 1 - dp[1] = s[0] === "0" ? 0 : 1 - for (let i = 2; i <= N; i++) { - if (s[i - 1] !== "0") { - dp[i] += dp[i - 1] - } - if (s[i - 2] === "1" || (s[i - 2] === "2" && s[i - 1] <= "6")) { - dp[i] += dp[i - 2] - } - } - return dp[N] -} diff --git a/92-reverse-linked-list-ii.js b/92-reverse-linked-list-ii.js index 8b30ebfb..687b4d45 100644 --- a/92-reverse-linked-list-ii.js +++ b/92-reverse-linked-list-ii.js @@ -36,201 +36,3 @@ const reverseBetween = function(head, left, right) { return dummy.next }; -// another - -/** - * Definition for singly-linked list. - * function ListNode(val) { - * this.val = val; - * this.next = null; - * } - */ -/** - * @param {ListNode} head - * @param {number} m - * @param {number} n - * @return {ListNode} - */ -const reverseBetween = function(head, m, n) { - // Empty list - if (head == null) { - return null; - } - - // Move the two pointers until they reach the proper starting point - // in the list. - let cur = head, prev = null; - while (m > 1) { - prev = cur; - cur = cur.next; - m--; - n--; - } - - // The two pointers that will fix the final connections. - let con = prev, tail = cur; - - // Iteratively reverse the nodes until n becomes 0. - let third = null; - while (n > 0) { - third = cur.next; - cur.next = prev; - prev = cur; - cur = third; - n--; - } - - // Adjust the final connections as explained in the algorithm - if (con != null) { - con.next = prev; - } else { - head = prev; - } - - tail.next = cur; - return head; -}; - -// another - -/** - * Definition for singly-linked list. - * function ListNode(val, next) { - * this.val = (val===undefined ? 0 : val) - * this.next = (next===undefined ? null : next) - * } - */ -/** - * @param {ListNode} head - * @param {number} m - * @param {number} n - * @return {ListNode} - */ -const reverseBetween = function(head, m, n) { - if (!head) return null; - const dummy = new ListNode(0); - dummy.next = head; - let pre = dummy; - for (let i = 0; i < m-1; i++) pre = pre.next - let start = pre.next; - let then = start.next; - - for (let i = 0; i < n-m; i++) { - start.next = then.next - then.next = pre.next - pre.next = then; - then = start.next; - } - return dummy.next; -}; - -// another - -/** - * Definition for singly-linked list. - * function ListNode(val, next) { - * this.val = (val===undefined ? 0 : val) - * this.next = (next===undefined ? null : next) - * } - */ -/** - * @param {ListNode} head - * @param {number} left - * @param {number} right - * @return {ListNode} - */ -const reverseBetween = function(head, left, right) { - if(head == null || left === right) return head - const dummy = new ListNode() - dummy.next = head - let tail = null, p = dummy - for(let i = 1; i < left; i++) { - p = p.next - } - tail = p.next - let tmp - for(let i = 0; i < right - left; i++) { - tmp = p.next - p.next = tail.next - tail.next = tail.next.next - p.next.next = tmp - } - - return dummy.next -}; - -// another - -/** - * @param {ListNode} head - * @param {number} left - * @param {number} right - * @return {ListNode} - */ -const reverseBetween = function(head, left, right) { - if(head == null) return head - if(left === right) return head - let cur = head, prev = null - let step = 1 - while(step !== left) { - prev = cur - cur = cur.next - step++ - } - let l = cur - while(step !== right) { - cur = cur.next - step++ - } - let r = cur, next = cur.next - // reverse - - let start = l, p = null - while(start !== r) { - let n = start.next - start.next = p - p = start - start = n - } - - r.next = p - l.next = next - if(prev) prev.next = r - - return prev ? head : r -}; - -// another - -/** - * Definition for singly-linked list. - * function ListNode(val, next) { - * this.val = (val===undefined ? 0 : val) - * this.next = (next===undefined ? null : next) - * } - */ -/** - * @param {ListNode} head - * @param {number} left - * @param {number} right - * @return {ListNode} - */ -const reverseBetween = function(head, left, right) { - if(head == null) return head - const dummy = new ListNode() - dummy.next = head - - let p = dummy - for(let i = 0; i < left - 1; i++) { - p = p.next - } - let tail = p.next, tmp = null - for(let i = 0; i < right - left; i++) { - tmp = p.next - p.next = tail.next - tail.next = tail.next.next - p.next.next = tmp - } - - return dummy.next -}; diff --git a/926-flip-string-to-monotone-increasing.js b/926-flip-string-to-monotone-increasing.js index 71f6bfff..1f728f48 100644 --- a/926-flip-string-to-monotone-increasing.js +++ b/926-flip-string-to-monotone-increasing.js @@ -17,29 +17,3 @@ const minFlipsMonoIncr = function(s) { return res }; -// another - - -/** - * @param {string} s - * @return {number} - */ -const minFlipsMonoIncr = function(s) { - const n = s.length - const arr = Array(n).fill(0) - let oneCnt = 0 - for(let i = 0; i < n; i++) { - if(s[i] === '1') oneCnt++ - arr[i] = oneCnt - } - const zeroCnt = n - oneCnt - let res = Infinity - - for(let i = 0; i < n; i++) { - const cnt = arr[i] - const tmp = cnt + (zeroCnt - (i + 1 - cnt)) - res = Math.min(res, tmp) - } - res = Math.min(res, oneCnt, zeroCnt) - return res -}; diff --git a/928-minimize-malware-spread-ii.js b/928-minimize-malware-spread-ii.js index dbe1249e..e9195d47 100644 --- a/928-minimize-malware-spread-ii.js +++ b/928-minimize-malware-spread-ii.js @@ -44,53 +44,3 @@ var minMalwareSpread = function (graph, initial) { return removed == -1 ? initial[0] : removed } -// another - -/** - * @param {number[][]} graph - * @param {number[]} initial - * @return {number} - */ -const minMalwareSpread = function (graph, initial) { - const map = new Map(), n = graph.length - for(let init of initial) { - const visited = new Set(initial) - const q = [init] - while(q.length) { - const cur = q.pop() - for(let i = 0; i < n; i++) { - if(graph[cur][i] === 1 && !visited.has(i)) { - visited.add(i) - q.push(i) - if(map.get(i) == null) map.set(i, []) - map.get(i).push(init) - } - } - } - } - - let res = 0, max = -1 - const arr = Array(n) - for(let [k,v] of map) { - if(v.length === 1) { - if(arr[v[0]] == null) arr[v[0]] = 0 - arr[v[0]]++ - } - } - - for(let k = 0; k < n; k++) { - const v = arr[k] - if(v > max) { - max = v - res = +k - } - } - - let min = Infinity - for(let e of initial) { - if(e < min) min = e - } - return max === -1 ? min: res - -} - diff --git a/93-restore-ip-addresses.js b/93-restore-ip-addresses.js index c2eb14a9..0a794382 100644 --- a/93-restore-ip-addresses.js +++ b/93-restore-ip-addresses.js @@ -30,32 +30,3 @@ const restoreIpAddresses = function(s) { return res; }; -// another method - -/** - * @param {string} s - * @return {string[]} - */ -const restoreIpAddresses = function(s) { - const ans = []; - const stack = []; - let ipstr; - const len = s.length; - function restoreIp(start) { - if (stack.length > 4) return; - if (stack.length === 4 && start > len - 1) { - ans.push(stack.slice().join(".")); - return; - } - for (let i = start; i < start + 3; i++) { - if (i > len - 1) return; - ipstr = s.substring(start, i + 1); - if ((ipstr[0] === "0" && ipstr.length !== 1) || ipstr > 255) return; - stack.push(ipstr); - restoreIp(i + 1); - stack.pop(); - } - } - restoreIp(0); - return ans; -}; diff --git a/930-binary-subarrays-with-sum.js b/930-binary-subarrays-with-sum.js index 785bf86c..cfc74d4a 100644 --- a/930-binary-subarrays-with-sum.js +++ b/930-binary-subarrays-with-sum.js @@ -18,26 +18,3 @@ const numSubarraysWithSum = function(A, S) { return ans; }; -// another - -/** - * @param {number[]} nums - * @param {number} goal - * @return {number} - */ -const numSubarraysWithSum = function(nums, goal) { - const hash = {} - const n = nums.length - let res = 0, sum = 0 - for(let i = 0; i < n; i++) { - const cur = nums[i] - sum += cur - const pre = sum - goal - if(hash[sum] == null) hash[sum] = 0 - if(hash[pre] != null) res += hash[pre] - if(sum === goal) res++ - hash[sum]++ - } - - return res -}; diff --git a/931-minimum-falling-path-sum.js b/931-minimum-falling-path-sum.js index d41ece0e..78fb4148 100644 --- a/931-minimum-falling-path-sum.js +++ b/931-minimum-falling-path-sum.js @@ -19,26 +19,3 @@ function getValueOrMax(A, i, j) { return A[i][j] !== undefined ? A[i][j] : Number.MAX_VALUE; } -// another - -/** - * @param {number[][]} A - * @return {number} - */ -const minFallingPathSum = function(A) { - for (let i = A.length - 2; i >= 0; i -= 1) { - for (let j = 0; j < A[i].length; j += 1) { - A[i][j] += Math.min( - getValueOrMax(A, i + 1, j - 1), - getValueOrMax(A, i + 1, j), - getValueOrMax(A, i + 1, j + 1) - ) - } - } - return Math.min(...A[0]) - } - - function getValueOrMax(A, i, j) { - return A[i][j] !== undefined ? A[i][j] : Number.MAX_VALUE - } - diff --git a/937-reorder-data-in-log-files.js b/937-reorder-data-in-log-files.js index aa103f4e..54cf644e 100644 --- a/937-reorder-data-in-log-files.js +++ b/937-reorder-data-in-log-files.js @@ -24,39 +24,3 @@ const reorderLogFiles = function(logs) { return [...letterLog, ...digitLog] } -// another - -/** - * @param {string[]} logs - * @return {string[]} - */ -const reorderLogFiles = function(logs) { - if(logs == null || logs.length === 0) return [] - const ll = [] - const dl = [] - const zero = '0'.charCodeAt(0) - const nine = '9'.charCodeAt(0) - for(let e of logs) { - const arr = e.split(' ') - if(arr[1].charCodeAt(0) >= zero && arr[1].charCodeAt(0) <= nine) { - dl.push(arr) - } else { - ll.push(arr) - } - } - const rll = ll.map(el => { - const r = el.slice(1).join(' ') - return [el[0], r] - }).sort((a, b) => { - if(a[1] < b[1]) return -1 - else if(a[1] > b[1]) return 1 - else { - if(`${a[0]} ${a[1]}` > `${b[0]} ${b[1]}`) return 1 - else if(`${a[0]} ${a[1]}` < `${b[0]} ${b[1]}`) return -1 - else return 0 - } - }).map(el => el.join(' ')) - - const rdl = dl.map(el => el.join(' ')) - return rll.concat(rdl) -}; diff --git a/939-minimum-area-rectangle.js b/939-minimum-area-rectangle.js index 722d4940..2bb96a2c 100644 --- a/939-minimum-area-rectangle.js +++ b/939-minimum-area-rectangle.js @@ -26,25 +26,3 @@ const minAreaRect = function(points) { return res === Infinity ? 0 : res }; -// another - -/** - * @param {number[][]} points - * @return {number} - */ -const minAreaRect = function (points) { - let ans = Infinity - const isPoint = {} - points.forEach(([x, y]) => (isPoint[x * 40000 + y] = true)) - for (let idx1 = 0; idx1 < points.length - 1; idx1++) { - const [x1, y1] = points[idx1] - for (let idx2 = idx1 + 1; idx2 < points.length; idx2++) { - const [x2, y2] = points[idx2] - const area = Math.abs((x1 - x2) * (y1 - y2)) - if (area === 0 || area >= ans) continue - if (isPoint[x1 * 40000 + y2] && isPoint[x2 * 40000 + y1]) ans = area - } - } - return ans !== Infinity ? ans : 0 -} - diff --git a/940-distinct-subsequences-ii.js b/940-distinct-subsequences-ii.js index 723acd12..9dc68e6b 100644 --- a/940-distinct-subsequences-ii.js +++ b/940-distinct-subsequences-ii.js @@ -18,35 +18,3 @@ const distinctSubseqII = function(s) { return dp.reduce((ac, e) => (ac + e) % mod, 0) }; -// another - -/** - * @param {string} S - * @return {number} - */ -const distinctSubseqII = function(S) { - // let end = new Array(26).fill(0), res = 0, added = 0, mod = 10 ** 9 + 7; - // const aCode = ('a').charCodeAt(0) - // for (let c of S) { - // added = (res + 1 - end[c.charCodeAt(0) - aCode]) % mod; - // res = (res + added) % mod; - // end[c.charCodeAt(0) - aCode] = (end[c.charCodeAt(0) - aCode] + added) % mod; - // } - // return (res + mod) % mod; - const m = new Map(), - dp = [1], - M = 1000000007 - for (let i = 0; i < S.length; i++) { - const c = S.charAt(i) - let prev = 0 - if (m.has(c)) { - prev = dp[m.get(c)] - } - m.set(c, i) - dp.push((((dp[i] * 2) % M) - prev) % M) - if (dp[i + 1] < 0) { - dp[i + 1] += M - } - } - return dp[S.length] - 1 -} diff --git a/942-di-string-match.js b/942-di-string-match.js index 7fee2f08..3b028de0 100644 --- a/942-di-string-match.js +++ b/942-di-string-match.js @@ -20,31 +20,3 @@ const diStringMatch = function(S) { return res }; -// another - -/* - -it is greedy and one pass !! -so every time when we meet an I, we need to keep in mind that we may meet another I later, -so the safest way is use the smallest number available. same idea when we meet D, -so in order to keep us safe, we always take largest one available, until we traverse the whole string. -And since the available numbers are sorted(from 0 to S.length()), -so we can set two pointers one starts from the head(smallest number), -another from the ends(largest number), then we begin to fill the res array. - -*/ - -/** - * @param {string} s - * @return {number[]} - */ -const diStringMatch = function(s) { - const n = s.length - let l = 0, r = n - const res = [] - for(let i = 0; i < n; i++) { - res.push(s[i] === 'I' ? l++ : r--) - } - res.push(r) - return res -}; diff --git a/945-minimum-increment-to-make-array-unique.js b/945-minimum-increment-to-make-array-unique.js index 0ce8f909..55d5fb21 100644 --- a/945-minimum-increment-to-make-array-unique.js +++ b/945-minimum-increment-to-make-array-unique.js @@ -24,19 +24,3 @@ const minIncrementForUnique = function(nums) { } }; -// another - -/** - * @param {number[]} nums - * @return {number} - */ -const minIncrementForUnique = function(nums) { - let res = 0, nxt = 0 - nums.sort((a, b) => a - b) - for(const e of nums) { - res += Math.max(0, nxt - e) - nxt = Math.max(nxt, e) + 1 - } - - return res -}; diff --git a/952-largest-component-size-by-common-factor.js b/952-largest-component-size-by-common-factor.js index c01f0348..0df178f4 100644 --- a/952-largest-component-size-by-common-factor.js +++ b/952-largest-component-size-by-common-factor.js @@ -64,67 +64,3 @@ class UF { } } -// another - - -/** - * @param {number[]} A - * @return {number} - */ -class UF { - constructor(N) { - this.parent = [] - this.size = [] - this.max = 1 - for (let i = 0; i < N; i++) { - this.parent[i] = i - this.size[i] = 1 - } - } - find(x) { - if (x === this.parent[x]) { - return x - } - return (this.parent[x] = this.find(this.parent[x])) - } - union(x, y) { - let rootX = this.find(x) - let rootY = this.find(y) - if (rootX != rootY) { - this.parent[rootX] = rootY - this.size[rootY] += this.size[rootX] - this.max = Math.max(this.max, this.size[rootY]) - } - } -} -const largestComponentSize = A => { - let N = A.length - const map = {} // key is the factor, val is the node index - const uf = new UF(N) - for (let i = 0; i < N; i++) { - let a = A[i] - for (let j = 2; j * j <= a; j++) { - if (a % j == 0) { - if (!map.hasOwnProperty(j)) { - //this means that no index has claimed the factor yet - map[j] = i - } else { - //this means that one index already claimed, so union that one with current - uf.union(i, map[j]) - } - if (!map.hasOwnProperty(a / j)) { - map[a / j] = i - } else { - uf.union(i, map[a / j]) - } - } - } - if (!map.hasOwnProperty(a)) { - //a could be factor too. Don't miss this - map[a] = i - } else { - uf.union(i, map[a]) - } - } - return uf.max -} diff --git a/955-delete-columns-to-make-sorted-ii.js b/955-delete-columns-to-make-sorted-ii.js index 0a9b4f52..47629720 100644 --- a/955-delete-columns-to-make-sorted-ii.js +++ b/955-delete-columns-to-make-sorted-ii.js @@ -36,32 +36,3 @@ const minDeletionSize = function (A) { return res } -// another - -/** - * @param {string[]} A - * @return {number} - */ -const minDeletionSize = function (A) { - const set = new Set() - const m = A.length - let res = 0 - if(m === 0) return 0 - const n = A[0].length - for(j = 0; j < n; j++) { - if(set.size === m - 1) return res - for(i = 0; i < m - 1; i++) { - if(!set.has(i) && A[i][j] > A[i + 1][j]) { - res++ - break - } - } - if(i < m - 1) continue - for(i = 0; i < m - 1; i++) { - if(A[i][j] < A[i + 1][j]) set.add(i) - } - } - - return res -} - diff --git a/96-unique-binary-search-trees.js b/96-unique-binary-search-trees.js index 59e886d5..5016ca31 100644 --- a/96-unique-binary-search-trees.js +++ b/96-unique-binary-search-trees.js @@ -13,28 +13,3 @@ const numTrees = function(n) { return arr[n] } -// another - -/** - * @param {number} n - * @return {number} - */ -const numTrees = function(n) { - const hash = { - 0: 1, - 1: 1 - } - return doNumTrees(n, hash) -} - -function doNumTrees(n, hash) { - if (hash[n]) return hash[n] - let sum = 0 - for (let i = 1; i <= n; i++) { - const left = doNumTrees(i - 1, hash) - const right = doNumTrees(n - i, hash) - sum += left * right - } - hash[n] = sum - return sum -} diff --git a/960-delete-columns-to-make-sorted-iii.js b/960-delete-columns-to-make-sorted-iii.js index e1054b33..c4787a58 100644 --- a/960-delete-columns-to-make-sorted-iii.js +++ b/960-delete-columns-to-make-sorted-iii.js @@ -15,22 +15,3 @@ const minDeletionSize = function(A) { return A[0].length - Math.max(...dp) } -// another - -const minDeletionSize = function(A) { - const rows = A.length - const cols = A[0].length - let res = cols - 1 - let k - const dp = new Array(cols).fill(1) - for (let i = 0; i < cols; i++) { - for (let j = 0; j < i; j++) { - for (k = 0; k < rows; k++) { - if (A[k][j] > A[k][i]) break - } - if (k === rows && dp[j] + 1 > dp[i]) dp[i] = dp[j] + 1 - } - res = Math.min(res, cols - dp[i]) - } - return res -} diff --git a/964-least-operators-to-express-number.js b/964-least-operators-to-express-number.js index aaccd462..ab1bb258 100644 --- a/964-least-operators-to-express-number.js +++ b/964-least-operators-to-express-number.js @@ -27,28 +27,3 @@ const leastOpsExpressTarget = function(x, target) { } -// another - -const leastOpsExpressTarget = function(x, y) { - let pos = 0, - neg = 0, - k = 0, - pos2, - neg2, - cur - while (y > 0) { - cur = y % x - y = (y / x) >> 0 - if (k > 0) { - pos2 = Math.min(cur * k + pos, (cur + 1) * k + neg) - neg2 = Math.min((x - cur) * k + pos, (x - cur - 1) * k + neg) - pos = pos2 - neg = neg2 - } else { - pos = cur * 2 - neg = (x - cur) * 2 - } - k++ - } - return Math.min(pos, k + neg) - 1 -} diff --git a/968-binary-tree-cameras.js b/968-binary-tree-cameras.js index fa629c7d..bfdb9c8e 100644 --- a/968-binary-tree-cameras.js +++ b/968-binary-tree-cameras.js @@ -26,35 +26,3 @@ const minCameraCover = function(root) { } }; - // another - -/** - * @param {TreeNode} root - * @return {number} - */ -const minCameraCover = function(root) { - let ans = 0 - const covered = new Set([null]) - dfs(root, null) - return ans - function dfs(node, parent) { - if (node) { - dfs(node.left, node) - dfs(node.right, node) - if ( - !( - (parent || covered.has(node)) && - covered.has(node.left) && - covered.has(node.right) - ) - ) { - ans += 1 - covered - .add(node) - .add(parent) - .add(node.left) - .add(node.right) - } - } - } -}; diff --git a/973-k-closest-points-to-origin.js b/973-k-closest-points-to-origin.js index b02f1611..915fae11 100644 --- a/973-k-closest-points-to-origin.js +++ b/973-k-closest-points-to-origin.js @@ -41,128 +41,3 @@ function compare(p1, p2) { } -// another - -/** - * @param {number[][]} points - * @param {number} K - * @return {number[][]} - */ -const kClosest = (points, K) => { - const pq = new PriorityQueue( - (p1, p2) => p1[0] * p1[0] + p1[1] * p1[1] > p2[0] * p2[0] + p2[1] * p2[1] - ) - for (let p of points) { - pq.push(p) - if (pq.size() > K) { - pq.pop() - } - } - const res = new Array(K) - while (K > 0) { - res[--K] = pq.pop() - } - return res -} - -class PriorityQueue { - constructor(comparator = (a, b) => a > b) { - this.heap = [] - this.top = 0 - this.comparator = comparator - } - size() { - return this.heap.length - } - isEmpty() { - return this.size() === 0 - } - peek() { - return this.heap[this.top] - } - push(...values) { - values.forEach((value) => { - this.heap.push(value) - this.siftUp() - }) - return this.size() - } - pop() { - const poppedValue = this.peek() - const bottom = this.size() - 1 - if (bottom > this.top) { - this.swap(this.top, bottom) - } - this.heap.pop() - this.siftDown() - return poppedValue - } - replace(value) { - const replacedValue = this.peek() - this.heap[this.top] = value - this.siftDown() - return replacedValue - } - - parent = (i) => ((i + 1) >>> 1) - 1 - left = (i) => (i << 1) + 1 - right = (i) => (i + 1) << 1 - greater = (i, j) => this.comparator(this.heap[i], this.heap[j]) - swap = (i, j) => ([this.heap[i], this.heap[j]] = [this.heap[j], this.heap[i]]) - siftUp = () => { - let node = this.size() - 1 - while (node > this.top && this.greater(node, this.parent(node))) { - this.swap(node, this.parent(node)) - node = this.parent(node) - } - } - siftDown = () => { - let node = this.top - while ( - (this.left(node) < this.size() && this.greater(this.left(node), node)) || - (this.right(node) < this.size() && this.greater(this.right(node), node)) - ) { - let maxChild = - this.right(node) < this.size() && - this.greater(this.right(node), this.left(node)) - ? this.right(node) - : this.left(node) - this.swap(node, maxChild) - node = maxChild - } - } -} - -// another - -/** - * @param {number[][]} points - * @param {number} k - * @return {number[][]} - */ -const kClosest = function(points, k) { - let len = points.length, l = 0, r = len - 1 - while (l <= r) { - let mid = helper(points, l, r) - if (mid === k) break - if (mid < k) l = mid + 1 - else r = mid - 1 - } - return points.slice(0, k) - - function helper(arr, l, r) { - const pivot = arr[l] - while(l < r) { - while(l < r && cmp(arr[r], pivot) >= 0) r-- - arr[l] = arr[r] - while(l < r && cmp(arr[l], pivot) <= 0) l++ - arr[r] = arr[l] - } - arr[l] = pivot - return l - } - - function cmp(a, b) { - return a[0] * a[0] + a[1] * a[1] - b[0] * b[0] - b[1] * b[1] - } -}; diff --git a/974-subarray-sums-divisible-by-k.js b/974-subarray-sums-divisible-by-k.js index 26306870..12688369 100644 --- a/974-subarray-sums-divisible-by-k.js +++ b/974-subarray-sums-divisible-by-k.js @@ -15,21 +15,3 @@ const subarraysDivByK = function (nums, k) { return res } -// another - -/** - * @param {number[]} nums - * @param {number} k - * @return {number} - */ -const subarraysDivByK = function(nums, k) { - const memo = {0: 1} - let sum = 0, res = 0 - for(const e of nums) { - sum += e - const remain = (k - (sum % k)) % k - res += memo[remain] ?? 0 - memo[remain] = (memo[remain] ?? 0) + 1 - } - return res -}; diff --git a/981-time-based-key-value-store.js b/981-time-based-key-value-store.js index 67101e69..34b06332 100644 --- a/981-time-based-key-value-store.js +++ b/981-time-based-key-value-store.js @@ -46,52 +46,3 @@ TimeMap.prototype.get = function(key, timestamp) { * var param_2 = obj.get(key,timestamp) */ -// another - -/** - * Initialize your data structure here. - */ -const TimeMap = function() { - this.hash = {} -}; - -/** - * @param {string} key - * @param {string} value - * @param {number} timestamp - * @return {void} - */ -TimeMap.prototype.set = function(key, value, timestamp) { - if(this.hash[key] == null) this.hash[key] = [] - this.hash[key].push([value, timestamp]) -}; - -/** - * @param {string} key - * @param {number} timestamp - * @return {string} - */ -TimeMap.prototype.get = function(key, timestamp) { - if(this.hash[key] == null) return '' - const arr = this.hash[key] - - let l = 0, r = arr.length - while(l < r) { - const mid = l + ((r - l) >> 1) - if(arr[mid][1] <= timestamp) { - l = mid + 1 - } else { - r = mid - } - } - - if(r === 0) return '' - return arr[r - 1][0] -}; - -/** - * Your TimeMap object will be instantiated and called as such: - * var obj = new TimeMap() - * obj.set(key,value,timestamp) - * var param_2 = obj.get(key,timestamp) - */ diff --git a/984-string-without-aaa-or-bbb.js b/984-string-without-aaa-or-bbb.js index b5996159..1bae95b5 100644 --- a/984-string-without-aaa-or-bbb.js +++ b/984-string-without-aaa-or-bbb.js @@ -35,53 +35,3 @@ const strWithout3a3b = function (a, b) { } } -// another - - -/** - * @param {number} a - * @param {number} b - * @return {string} - */ -const strWithout3a3b = function(a, b) { - let m = a, n = b, ch1 = 'a', ch2 = 'b' - if(b > a) { - m = b, n = a, ch1 = 'b', ch2 = 'a' - } - let res = '' - while(m-- > 0) { - res += ch1 - if(m > n) { - res += ch1 - m-- - } - if(n > 0) { - res += ch2 - n-- - } - } - return res -}; - -// another - -/** - * @param {number} a - * @param {number} b - * @return {string} - */ -const strWithout3a3b = function (a, b, ac = 'a', bc = 'b') { - const delta = a - b - let res = '' - if (delta < 0) { - return strWithout3a3b(b, a, 'b', 'a') - } else { - while(a-- > 0) { - res += ac - if(a > b) res += ac, a-- - if(b-- > 0) res += bc - } - } - - return res -} diff --git a/985-sum-of-even-numbers-after-queries.js b/985-sum-of-even-numbers-after-queries.js index cfd5693d..49e78140 100644 --- a/985-sum-of-even-numbers-after-queries.js +++ b/985-sum-of-even-numbers-after-queries.js @@ -16,18 +16,3 @@ function sum(arr) { return arr.reduce((ac, el) => ac + (el % 2 === 0 ? el : 0), 0) } -// another, better - -const sumEvenAfterQueries = function(A, queries) { - let sum = A.reduce((acc, cur) => cur%2 == 0 ? acc + cur : acc, 0); - return queries.map((q) => { - let i = q[1]; - let s = A[i] + q[0]; - if(s%2 === 0) { - sum += q[0]; - if(A[i]%2 !== 0) sum += A[i]; - } else if(A[i]%2 === 0) sum -= A[i]; - A[i] = s; - return sum; - }); -}; diff --git a/988-smallest-string-starting-from-leaf.js b/988-smallest-string-starting-from-leaf.js index 9ee8c1d1..38fbba88 100644 --- a/988-smallest-string-starting-from-leaf.js +++ b/988-smallest-string-starting-from-leaf.js @@ -40,14 +40,3 @@ function arrToStr(arr) { return res } -// another - -const smallestFromLeaf = function(root) { - if (!root) return '' - const char = String.fromCharCode(97 + root.val) - let left = smallestFromLeaf(root.left) - let right = smallestFromLeaf(root.right) - if (!left) return right + char - if (!right) return left + char - return (left < right ? left : right) + char -}; diff --git a/99-recover-binary-search-tree.js b/99-recover-binary-search-tree.js index 0a197248..fbc4b395 100644 --- a/99-recover-binary-search-tree.js +++ b/99-recover-binary-search-tree.js @@ -33,50 +33,3 @@ const recoverTree = function(root) { } } -// another - -const recoverTree = function(root) { - const eNodes = []; - if (root == null) return; - let current = root; - let pre; - let previous = null; - while (current != null) { - if (current.left == null) { - if (previous != null && previous.val > current.val) { - eNodes.push(previous); - eNodes.push(current); - } - previous = current; - current = current.right; - } else { - pre = current.left; - while (pre.right != null && pre.right.val != current.val) { - pre = pre.right; - } - - if (pre.right == null) { - pre.right = current; - current = current.left; - } else { - if (previous != null && previous.val > current.val) { - eNodes.push(previous); - eNodes.push(current); - } - pre.right = null; - previous = current; - current = current.right; - } - } - } - if (eNodes.length == 2) { - pre = eNodes[0]; - current = eNodes[1]; - } else { - pre = eNodes[0]; - current = eNodes[3]; - } - let temp = pre.val; - pre.val = current.val; - current.val = temp; -}; diff --git a/990-satisfiability-of-equality-equations.js b/990-satisfiability-of-equality-equations.js index 8c1a6c76..a489269d 100644 --- a/990-satisfiability-of-equality-equations.js +++ b/990-satisfiability-of-equality-equations.js @@ -23,41 +23,3 @@ const equationsPossible = function(equations) { }; -// another - -/** - * @param {string[]} equations - * @return {boolean} - */ -const equationsPossible = function(equations) { - const num = 26 - const visited = new Array(num).fill(false) - const color = new Array(num).fill(-1) - const adj = Array.from(new Array(num), el => []) - const aCode = ('a').charCodeAt(0) - for(let el of equations) { - if(el[1] === '=') { - adj[el[0].charCodeAt(0) - aCode].push(el[3].charCodeAt(0) - aCode) - adj[el[3].charCodeAt(0) - aCode].push(el[0].charCodeAt(0) - aCode) - } - } - let c = 0 - for(let i = 0; i < num; i++) { - !visited[i] && dfs(i, c) - c++ - } - for(let el of equations) { - if(el[1] === '!' && color[el[0].charCodeAt(0) - aCode] === color[el[3].charCodeAt(0) - aCode]) { - return false - } - } - return true - - function dfs(idx, val) { - visited[idx] = true - color[idx] = val - for(let el of adj[idx]) { - !visited[el] && dfs(el, val) - } - } -}; diff --git a/992-subarrays-with-k-different-integers.js b/992-subarrays-with-k-different-integers.js index f597e751..c88ad6b8 100644 --- a/992-subarrays-with-k-different-integers.js +++ b/992-subarrays-with-k-different-integers.js @@ -25,56 +25,3 @@ const subarraysWithKDistinct = function(nums, k) { } }; -// another - -/** - * @param {number[]} A - * @param {number} K - * @return {number} - */ -const subarraysWithKDistinct = function(A, K) { - let res = 0 - let prefix = 0 - const m = new Array(A.length + 1).fill(0) - for (let i = 0, j = 0, cnt = 0, len = A.length; i < len; i++) { - if (m[A[i]]++ === 0) cnt++ - if (cnt > K) { - m[A[j++]]-- - cnt-- - prefix = 0 - } - while (m[A[j]] > 1) { - prefix++ - m[A[j++]]-- - } - if (cnt === K) res += prefix + 1 - } - return res -} - -// another - -/** - * @param {number[]} A - * @param {number} K - * @return {number} - */ -const subarraysWithKDistinct = function (A, K) { - return mostK(K) - mostK(K - 1) - function mostK(num) { - const m = {}, len = A.length - let i = 0, j = 0, res = 0 - for(j = 0; j < len; j++) { - if(!m[A[j]]) m[A[j]] = 0, num-- - m[A[j]]++ - while(num < 0) { - m[A[i]]-- - if(!m[A[i]]) num++ - i++ - } - res += j - i + 1 - } - return res - } -} - diff --git a/993.cousins-in-binary-tree.js b/993.cousins-in-binary-tree.js index 82cbd579..002702f0 100644 --- a/993.cousins-in-binary-tree.js +++ b/993.cousins-in-binary-tree.js @@ -35,51 +35,3 @@ const isCousins = (root, x, y, depth = 1, P = {}, D = {}) => { } -// another - -/** - * Definition for a binary tree node. - * function TreeNode(val) { - * this.val = val; - * this.left = this.right = null; - * } - */ -/** - * @param {TreeNode} root - * @param {number} x - * @param {number} y - * @return {boolean} - */ -const isCousins = function(root, x, y) { - if(root == null) return false - const res = [] - chk(root, x, [], res) - chk(root, y, [], res) - if(res.length < 2) return false - return chkRes(res, x, y) -}; -function chkRes(arr, x, y) { - let ci = 0, xi = -1, yi = -1 - let len = Math.max(arr[0].length, arr[1].length) - for(let i = 0; i < len; i++) { - if(arr[0][i] === arr[1][i]) ci = i - if(arr[0][i] === x || arr[1][i] === x) xi = i - if(arr[0][i] === y || arr[1][i] === y) yi = i - } - if(xi - yi === 0 && xi - ci > 1) { - return true - } else { - return false - } -} - -function chk(node, val, path, res) { - if(node == null) return - path.push(node.val) - if(node.val === val) { - res.push(path.slice(0)) - return - } - chk(node.left, val, path.slice(0), res) - chk(node.right, val, path.slice(0), res) -} diff --git a/994-rotting-oranges.js b/994-rotting-oranges.js index 2fa9a406..6a3c972c 100644 --- a/994-rotting-oranges.js +++ b/994-rotting-oranges.js @@ -34,50 +34,3 @@ const orangesRotting = function(grid) { return visited.size === num ? res : -1 }; -// another - - -/** - * @param {number[][]} grid - * @return {number} - */ -const orangesRotting = function(grid) { - let count = 0 - const p = {s: 2} - const rows = grid.length - const cols = grid[0].length - while(!chk(grid, rows, cols)) { - loop(grid, rows, cols, p) - count++ - if(count> rows * cols) return -1 - } - - return count -}; - -function loop(grid, rows, cols, p) { - let cur = p.s - let next = cur + 1 - for(let i = 0; i < rows; i++) { - for(let j = 0; j < cols; j++) { - if(grid[i][j] === cur) rotten(i, j, grid, next) - } - } - p.s += 1 -} - -function rotten(row, col, grid, p) { - if(grid[row] && col > 0 && grid[row][col - 1] === 1) grid[row][col - 1] = p - if(grid[row] && col < (grid[0] || []).length - 1 && grid[row][col + 1] === 1) grid[row][col + 1] = p - if(grid[row] && row > 0 && grid[row - 1][col] === 1) grid[row - 1][col] = p - if(grid[row] && row < grid.length - 1 && grid[row + 1][col] === 1) grid[row + 1][col] = p -} - -function chk(grid, rows, cols) { - for(let i = 0; i < rows; i++) { - for(let j = 0; j < cols; j++) { - if(grid[i][j] === 1) return false - } - } - return true -} diff --git a/995-minimum-number-of-k-consecutive-bit-flips.js b/995-minimum-number-of-k-consecutive-bit-flips.js index 90a4ad92..3cc4825e 100644 --- a/995-minimum-number-of-k-consecutive-bit-flips.js +++ b/995-minimum-number-of-k-consecutive-bit-flips.js @@ -18,51 +18,3 @@ const minKBitFlips = function(nums, k) { return res }; -// another - -/** - * @param {number[]} A - * @param {number} K - * @return {number} - */ -const minKBitFlips = function(A, K) { - let cur = 0, res = 0; - for (let i = 0; i < A.length; ++i) { - if (i >= K) cur -= (A[i - K] / 2) >> 0; - if ((cur & 1 ^ A[i]) === 0) { - if (i + K > A.length) return -1; - A[i] += 2; - cur++; - res++; - } - } - return res; -}; - -// another - -/** - * @param {number[]} nums - * @param {number} k - * @return {number} - */ -const minKBitFlips = function(nums, k) { - const n = nums.length, q = [] - let res = 0 - for(let i = 0; i < n; i++) { - if(nums[i] === 0) { - if(q.length === 0 || q.length % 2 === 0) { - res++ - q.push(i + k - 1) - } - } else { - if(q.length % 2 === 1) { - res++ - q.push(i + k - 1) - } - } - if(q.length && i >= q[0]) q.shift() - } - return q.length ? -1 : res -}; - diff --git a/997-find-the-town-judge.js b/997-find-the-town-judge.js index 55b28506..1339c2b7 100644 --- a/997-find-the-town-judge.js +++ b/997-find-the-town-judge.js @@ -15,27 +15,3 @@ const findJudge = function(N, trust) { return -1 }; -// another - -/** - * @param {number} N - * @param {number[][]} trust - * @return {number} - */ -const findJudge = function(N, trust) { - const m = new Map() - for(let i = 1; i<= N; i++) { - const e = new Map() - e.set('t', new Set()) - e.set('ted', new Set()) - m.set(i, e) - } - for(let [t, ted] of trust) { - m.get(t).get('t').add(ted) - m.get(ted).get('ted').add(t) - } - for(let [k,v] of m) { - if(v.get('t').size === 0 && v.get('ted').size === N - 1) return k - } - return -1 -}; diff --git a/clean-dataset.py b/clean-dataset.py new file mode 100644 index 00000000..775c093f --- /dev/null +++ b/clean-dataset.py @@ -0,0 +1,15 @@ +import glob +import os + +js_files = glob.glob('*.js') +for js_file in js_files: + with open(js_file, 'r', encoding='utf-8') as f: + src_lines = f.readlines() + # Find indices of lines that contain // another + line_sep = [i for i, line in enumerate(src_lines) if '// another' in line or '//another' in line] + if len(line_sep) > 0: + new_lines = src_lines[:line_sep[0]] + # Rewrite the file + with open(js_file, 'w', encoding='utf-8') as f: + f.writelines(new_lines) + From e5b280d297b16b28184c9be3e9f127b2facc6593 Mon Sep 17 00:00:00 2001 From: Ognjen Date: Tue, 25 Jul 2023 23:34:26 +0200 Subject: [PATCH 2/3] Rename files --- 1-two-sum.js => 1.two-sum.js | 30 ++-- ...ng.js => 10.regular-expression-matching.js | 52 +++--- 100-same-tree.js => 100.same-tree.js | 38 ++-- ...js => 1000.minimum-cost-to-merge-stones.js | 0 ...lumination.js => 1001.grid-illumination.js | 0 ...cters.js => 1002.find-common-characters.js | 0 ...iii.js => 1004.max-consecutive-ones-iii.js | 0 ...maximize-sum-of-array-after-k-negations.js | 0 ...y-factorial.js => 1006.clumsy-factorial.js | 0 ...ary-search-tree-from-preorder-traversal.js | 0 ...s => 1009.complement-of-base-10-integer.js | 0 ...symmetric-tree.js => 101.symmetric-tree.js | 0 ...capacity-to-ship-packages-within-d-days.js | 0 ...s => 1012.complement-of-base-10-integer.js | 0 ...gs-with-total-durations-divisible-by-60.js | 0 ...capacity-to-ship-packages-within-d-days.js | 0 ...js => 1015.numbers-with-repeated-digits.js | 0 ...-to-base-2.js => 1017.convert-to-base-2.js | 0 ...js => 1018.binary-prefix-divisible-by-5.js | 0 ...> 102.binary-tree-level-order-traversal.js | 72 ++++---- ...n-array-into-three-parts-with-equal-sum.js | 0 ...g-pair.js => 1021.best-sightseeing-pair.js | 0 ...js => 1021.remove-outermost-parentheses.js | 0 ...=> 1022.smallest-integer-divisible-by-k.js | 0 ...1022.sum-of-root-to-leaf-binary-numbers.js | 0 ...ing-with-substrings-representing-1-to-n.js | 0 ...-matching.js => 1023.camelcase-matching.js | 0 ...eo-stitching.js => 1024.video-stitching.js | 0 1025-divisor-game.js => 1025.divisor-game.js | 0 ...um-difference-between-node-and-ancestor.js | 0 ....js => 1027.longest-arithmetic-sequence.js | 0 ....recover-a-tree-from-preorder-traversal.js | 0 ...heduling.js => 1029.two-city-scheduling.js | 0 ...inary-tree-zigzag-level-order-traversal.js | 0 ... => 1030.matrix-cells-in-distance-order.js | 0 ...> 1030.next-greater-node-in-linked-list.js | 0 ...um-sum-of-two-non-overlapping-subarrays.js | 0 ...-enclaves.js => 1031.number-of-enclaves.js | 0 ...racters.js => 1032.stream-of-characters.js | 0 ...=> 1033.moving-stones-until-consecutive.js | 0 ...g-a-border.js => 1034.coloring-a-border.js | 0 ...rossed-lines.js => 1035.uncrossed-lines.js | 0 ...rge-maze.js => 1036.escape-a-large-maze.js | 0 ...id-boomerang.js => 1037.valid-boomerang.js | 0 ....binary-search-tree-to-greater-sum-tree.js | 0 ....minimum-score-triangulation-of-polygon.js | 0 ....js => 104.maximum-depth-of-binary-tree.js | 36 ++-- ...1040.moving-stones-until-consecutive-ii.js | 0 ...rcle.js => 1041.robot-bounded-in-circle.js | 0 ...> 1042.flower-planting-with-no-adjacent.js | 0 ...=> 1043.partition-array-for-maximum-sum.js | 0 ....js => 1044.longest-duplicate-substring.js | 0 ...one-weight.js => 1046.last-stone-weight.js | 0 ...emove-all-adjacent-duplicates-in-string.js | 0 ...g-chain.js => 1048.longest-string-chain.js | 0 ...ight-ii.js => 1049.last-stone-weight-ii.js | 0 ...ree-from-preorder-and-inorder-traversal.js | 0 ...eight-checker.js => 1051.height-checker.js | 0 ...owner.js => 1052.grumpy-bookstore-owner.js | 0 ...1053.previous-permutation-with-one-swap.js | 0 ...nt-barcodes.js => 1054.distant-barcodes.js | 0 ...sing-number.js => 1056.confusing-number.js | 0 ...l-paths-from-source-lead-to-destination.js | 0 ...ee-from-inorder-and-postorder-traversal.js | 0 ...=> 1060.missing-element-in-sorted-array.js | 0 ...ographically-smallest-equivalent-string.js | 0 ....js => 1062.longest-repeating-substring.js | 0 1064-fixed-point.js => 1064.fixed-point.js | 0 ...ring.js => 1065.index-pairs-of-a-string.js | 0 ...pus-bikes-ii.js => 1066.campus-bikes-ii.js | 0 ...n-range.js => 1067.digit-count-in-range.js | 0 ...07.binary-tree-level-order-traversal-ii.js | 0 ...1071.greatest-common-divisor-of-strings.js | 0 ...olumns-for-maximum-number-of-equal-rows.js | 0 ...s => 1073.adding-two-negabinary-numbers.js | 0 ...umber-of-submatrices-that-sum-to-target.js | 0 ...es.js => 1079.letter-tile-possibilities.js | 0 ...vert-sorted-array-to-binary-search-tree.js | 60 +++---- ...lest-subsequence-of-distinct-characters.js | 0 ...085.sum-of-digits-in-the-minimum-number.js | 0 1086-high-five.js => 1086.high-five.js | 0 ...ce-expansion.js => 1087.brace-expansion.js | 0 ...umber-ii.js => 1088.confusing-number-ii.js | 0 ...licate-zeros.js => 1089.duplicate-zeros.js | 0 ...nvert-sorted-list-to-binary-search-tree.js | 0 ...s.js => 1090.largest-values-from-labels.js | 0 ... => 1091.shortest-path-in-binary-matrix.js | 0 ...s => 1092.shortest-common-supersequence.js | 0 ... => 1093.statistics-from-a-large-sample.js | 0 1094-car-pooling.js => 1094.car-pooling.js | 0 ...array.js => 1095.find-in-mountain-array.js | 0 ...ansion-ii.js => 1096.brace-expansion-ii.js | 0 ...s-than-k.js => 1099.two-sum-less-than-k.js | 0 ...ater.js => 11.container-with-most-water.js | 28 +-- ...ary-tree.js => 110.balanced-binary-tree.js | 0 ...=> 1102.path-with-maximum-minimum-value.js | 0 ...js => 1103.distribute-candies-to-people.js | 0 ...104.path.in-zigzag-labelled-binary-tree.js | 0 ...ves.js => 1105.filling-bookcase-shelves.js | 0 ...js => 1106.parsing-a-boolean-expression.js | 0 ...ress.js => 1108.defanging-an-ip-address.js | 0 ...gs.js => 1109.corporate-flight-bookings.js | 0 ....js => 111.minimum-depth-of-binary-tree.js | 0 ... => 1110.delete-nodes-and-return-forest.js | 0 ...-depth-of-two-valid-parentheses-strings.js | 0 ...th.js => 1118.number-of-days-in-a-month.js | 0 ....js => 1119.remove-vowels-from-a-string.js | 0 112-path-sum.js => 112.path-sum.js | 0 ...tree.js => 1120.maximum-average-subtree.js | 0 ....divide-array-into-increasing-sequences.js | 0 ...rt-array.js => 1122.relative-sort-array.js | 0 ...owest-common-ancestor-of-deepest-leaves.js | 0 ...> 1124.longest-well-performing-interval.js | 0 ...eam.js => 1125.smallest-sufficient-team.js | 0 ...-pairsnumber-of-equivalent-domino-pairs.js | 0 ...9.shortest-path-with-alternating-colors.js | 0 113-path-sum-ii.js => 113.path-sum-ii.js | 0 ...1130.minimum-cost-tree-from-leaf-values.js | 0 ...number.js => 1133.largest-unique-number.js | 0 ...rong-number.js => 1134.armstrong-number.js | 0 ...lel-courses.js => 1136.parallel-courses.js | 0 ...umber.js => 1137.n-th-tribonacci-number.js | 0 ...ard-path.js => 1138.alphabet-board-path.js | 0 ...re.js => 1139.largest-1-bordered-square.js | 0 ... 114.flatten-binary-tree-to-linked-list.js | 0 ...-stone-game-ii.js => 1140.stone-game-ii.js | 0 ...e.js => 1143.longest-common-subsequence.js | 0 ...me.js => 1145.binary-tree-coloring-game.js | 0 ...napshot-array.js => 1146.snapshot-array.js | 0 ...ongest-chunked-palindrome-decomposition.js | 0 ...quences.js => 115.distinct-subsequences.js | 0 ...r-is-majority-element-in-a-sorted-array.js | 0 ....minimum-swaps-to-group-all-1s-together.js | 0 ...3.string-transforms-into-another-string.js | 0 ...-of-the-year.js => 1154.day-of-the-year.js | 0 ...55.number-of-dice-rolls-with-target-sum.js | 0 ...or-longest-repeated-character-substring.js | 0 ...157.online-majority-element-in-subarray.js | 0 ...lating-next-right-pointers-in-each-node.js | 0 ...-words-that-can-be-formed-by-characters.js | 0 ...1161.maximum-level-sum-of-a-binary-tree.js | 0 ...js => 1162.as-far-from-land-as-possible.js | 0 ...last-substring-in-lexicographical-order.js | 0 ...keyboard.js => 1165.single-row-keyboard.js | 0 ...le-system.js => 1166.design-file-system.js | 0 ... => 1167.minimum-cost-to-connect-sticks.js | 0 ...ptimize-water-distribution-in-a-village.js | 0 ...ing-next-right-pointers-in-each-node-ii.js | 0 ...-by-frequency-of-the-smallest-character.js | 0 ...-sum-consecutive-nodes-from-linked-list.js | 0 ...e-stacks.js => 1172.dinner-plate-stacks.js | 0 ...angements.js => 1175.prime-arrangements.js | 0 ...rmance.js => 1176.diet-plan-performance.js | 0 ...1177.can-make-palindrome-from-substring.js | 0 ...8.number-of-valid-words-for-each-puzzle.js | 0 ...'s-triangle.js => 118.pascal's-triangle.js | 76 ++++---- ...ubstrings-with-only-one-distinct-letter.js | 0 ...zzle.js => 1181.before-and-after-puzzle.js | 0 ...-ones.js => 1183.maximum-number-of-ones.js | 0 ...s.js => 1184.distance-between-bus-stops.js | 0 ...-of-the-week.js => 1185.day-of-the-week.js | 0 ....maximum-subarray-sum-with-one-deletion.js | 0 ... => 1187.make-array-strictly-increasing.js | 0 ...s.js => 1189.maximum-number-of-balloons.js | 0 ...angle-II.js => 119.pascal's-triangle-II.js | 32 ++-- ...trings-between-each-pair-of-parentheses.js | 0 ....js => 1191.k-concatenation-maximum-sum.js | 0 ... 1192.critical-connections-in-a-network.js | 0 ...js => 1199.minimum-time-to-build-blocks.js | 0 ...eger-to-roman.js => 12.integer-to-roman.js | 64 +++---- 120-triangle.js => 120.triangle.js | 0 ....js => 1200.minimum-absolute-difference.js | 0 ...y-number-iii.js => 1201.ugly-number-iii.js | 0 ...s.js => 1202.smallest-string-with-swaps.js | 0 ...items-by-groups-respecting-dependencies.js | 0 ...ign-skiplist.js => 1206.design-skiplist.js | 0 ...js => 1207.unique-number-of-occurrences.js | 0 ...ve-all-adjacent-duplicates-in-string-ii.js | 0 ... => 121.best-time-to-buy-and-sell-stock.js | 34 ++-- ...um-moves-to-reach-target-with-rotations.js | 0 ...213.intersection-of-three-sorted-arrays.js | 0 1214-two-sum-bsts.js => 1214.two-sum-bsts.js | 0 ...ome-iii.js => 1216.valid-palindrome-iii.js | 0 ...cost-to-move-chips-to-the-same-position.js | 0 ...-gold.js => 1219.path-with-maximum-gold.js | 0 ... 122.best-time-to-buy-and-sell-stock-ii.js | 0 ...ion.js => 1220.count-vowels-permutation.js | 0 ...1221.split-a-string-in-balanced-strings.js | 0 ...ulation.js => 1223.dice-roll-simulation.js | 0 ...ency.js => 1224.maximum-equal-frequency.js | 0 ...issing-number-in-arithmetic-progression.js | 0 ...-scheduler.js => 1229.meeting-scheduler.js | 0 ...123.best-time-to-buy-and-sell-stock-iii.js | 0 ...e-chocolate.js => 1231.divide-chocolate.js | 0 ... => 1232.check-if-it-is-a-straight-line.js | 0 ...> 1235.maximum-profit-in-job-scheduling.js | 0 ...catenated-string-with-unique-characters.js | 0 ....js => 124.binary-tree-maximum-path-sum.js | 0 ...ing-a-rectangle-with-the-fewest-squares.js | 0 ...rmation.js => 1243.array-transformation.js | 0 ...erboard.js => 1244.design-a-leaderboard.js | 0 ...-tree-diameter.js => 1245.tree-diameter.js | 0 ...e-removal.js => 1246.palindrome-removal.js | 0 ...247.minimum-swaps-to-make-strings-equal.js | 0 ...inimum-remove-to-make-valid-parentheses.js | 0 ...d-palindrome.js => 125.valid-palindrome.js | 0 ... 1252.cells-with-odd-values-in-a-matrix.js | 0 ...nds.js => 1254.number-of-closed-islands.js | 0 ...5.maximum-score-words-formed-by-letters.js | 0 ...egion.js => 1257.smallest-common-region.js | 0 ...word-ladder-ii.js => 126.word-ladder-ii.js | 0 ...-shift-2d-grid.js => 1260.shift-2d-grid.js | 0 ...-elements-in-a-contaminated-binary-tree.js | 0 ...=> 1262.greatest-sum-divisible-by-three.js | 0 ...-to-move-a-box-to-their-target-location.js | 0 ....print-immutable-linked-list-in-reverse.js | 0 ...> 1266.minimum-time-visiting-all-points.js | 0 ...em.js => 1268.search-suggestions-system.js | 0 ...stay-in-the-same-place-after-some-steps.js | 0 127-word-ladder.js => 127.word-ladder.js | 0 1271-hexspeak.js => 1271.hexspeak.js | 0 ...ove-interval.js => 1272.remove-interval.js | 0 ...tree-nodes.js => 1273.delete-tree-nodes.js | 0 ... => 1274.number-of-ships-in-a-rectangle.js | 0 ... 1275.find-winner-on-a-tic-tac-toe-game.js | 0 ....count-square-submatrices-with-all-ones.js | 0 ....js => 1278.palindrome-partitioning-iii.js | 0 ....js => 128.longest-consecutive-sequence.js | 0 ...product-and-sum-of-digits-of-an-integer.js | 0 ...ple-given-the-group-size-they-belong-to.js | 0 ...-the-smallest-divisor-given-a-threshold.js | 0 ...to-convert-binary-matrix-to-zero-matrix.js | 0 ...ion.js => 1286.iterator-for-combination.js | 0 ...-appearing-more-than-25-in-sorted-array.js | 0 ...als.js => 1288.remove-covered-intervals.js | 0 ....js => 1289.minimum-falling-path-sum-ii.js | 0 ...bers.js => 129.sum-root-to-leaf-numbers.js | 0 ...nary-number-in-a-linked-list-to-integer.js | 0 ...ial-digits.js => 1291.sequential-digits.js | 0 ...ith-sum-less-than-or-equal-to-threshold.js | 0 ...th-in-a-grid-with-obstacles-elimination.js | 0 ...find-numbers-with-even-number-of-digits.js | 0 ...nts-with-greatest-element-on-right-side.js | 0 ...an-to-integer.js => 13.roman-to-integer.js | 0 ...ed-regions.js => 130.surrounded-regions.js | 0 ....sum-of-mutated-array-closest-to-target.js | 0 ... => 1301.number-of-paths-with-max-score.js | 0 ...eaves-sum.js => 1302.deepest-leaves-sum.js | 0 ...4.find-n-unique-integers-sum-up-to-zero.js | 0 ...all-elements-in-two-binary-search-trees.js | 0 ...-jump-game-iii.js => 1306.jump-game-iii.js | 0 ...zle.js => 1307.verbal-arithmetic-puzzle.js | 0 ...string-from-alphabet-to-integer-mapping.js | 0 ...oning.js => 131.palindrome-partitioning.js | 0 ...ay.js => 1310.xor-queries-of-a-subarray.js | 0 ...rtion-steps-to-make-a-string-palindrome.js | 0 ...1313.decompress-run-length-encoded-list.js | 0 ...x-block-sum.js => 1314.matrix-block-sum.js | 0 ...m-of-nodes-with-even-valued-grandparent.js | 0 ...ngs.js => 1316.distinct-echo-substrings.js | 0 ...ii.js => 132.palindrome-partitioning-ii.js | 0 ...stance-to-type-a-word-using-two-fingers.js | 0 ...-69-number.js => 1323.maximum-69-number.js | 0 ...cally.js => 1324.print-words-vertically.js | 0 ...> 1325.delete-leaves-with-a-given-value.js | 0 ...umber-of-taps-to-open-to-water-a-garden.js | 0 ...y.js => 1329.sort-the-matrix-diagonally.js | 0 133-clone-graph.js => 133.clone-graph.js | 0 ...everse-subarray-to-maximize-array-value.js | 0 ...y.js => 1331.rank-transform-of-an-array.js | 0 ...=> 1332.remove-palindromic-subsequences.js | 0 ...35.minimum-difficulty-of-a-job-schedule.js | 0 ... => 1337.the-k-weakest-rows-in-a-matrix.js | 0 ...s => 1338.reduce-array-size-to-the-half.js | 0 134-gas-station.js => 134.gas-station.js | 0 1340-jump-game-v.js => 1340.jump-game-v.js | 0 ...ber-of-steps-to-reduce-a-number-to-zero.js | 0 ...rage-greater-than-or-equal-to-threshold.js | 0 ... => 1344.angle-between-hands-of-a-clock.js | 0 1345-jump-game-iv.js => 1345.jump-game-iv.js | 0 ...er-of-steps-to-make-two-strings-anagram.js | 0 ...y.js => 1348.tweet-counts-per-frequency.js | 0 ...js => 1349.maximum-students-taking-exam.js | 0 135-candy.js => 135.candy.js | 0 ...unt-negative-numbers-in-a-sorted-matrix.js | 0 ...s => 1352.product-of-the-last-k-numbers.js | 0 ...m-number-of-events-that-can-be-attended.js | 0 ...nstruct-target-array-with-multiple-sums.js | 0 ...6.sort-integers-by-the-number-of-1-bits.js | 0 ...t-all-valid-pickup-and-delivery-options.js | 0 136-single-number.js => 136.single-number.js | 0 ...> 1360.number-of-days-between-two-dates.js | 0 ...ee.js => 1363.largest-multiple-of-three.js | 0 ...ers-are-smaller-than-the-current-number.js | 0 ...by-votes.js => 1366.rank-teams-by-votes.js | 0 ...e.js => 1367.linked-list-in-binary-tree.js | 0 ...-make-at-least-one-valid-path-in-a-grid.js | 0 ...le-number-ii.js => 137.single-number-ii.js | 0 ...js => 1370.increasing-decreasing-string.js | 0 ...string-containing-vowels-in-even-counts.js | 0 ... => 1373.maximum-sum-bst-in-binary-tree.js | 0 ...ng-with-characters-that-have-odd-counts.js | 0 ...f-times-binary-string-is-prefix-aligned.js | 0 ...376.time-needed-to-inform-all-employees.js | 0 ...s => 1377.frog-position-after-t-seconds.js | 0 ...js => 138.copy-list-with-random-pointer.js | 0 ...ix.js => 1380.lucky-numbers-in-a-matrix.js | 0 ...design-a-stack-with-increment-operation.js | 0 ...js => 1382.balance-a-binary-search-tree.js | 0 ...s => 1383.maximum-performance-of-a-team.js | 0 ...d-the-distance-value-between-two-arrays.js | 0 ...> 1387.sort-integers-by-the-power-value.js | 0 ...-slices.js => 1388.pizza-with-3n-slices.js | 0 ....create-target-array-in-the-given-order.js | 0 139-word-break.js => 139.word-break.js | 0 ...-four-divisors.js => 1390.four-divisors.js | 0 ...-prefix.js => 1392.longest-happy-prefix.js | 0 ... => 1394.find-lucky-integer-in-an-array.js | 0 ...-teams.js => 1395.count-number-of-teams.js | 0 ...-teams.js => 1396.count-number-of-teams.js | 0 ...em.js => 1396.design-underground-system.js | 0 ...trings.js => 1397.find-all-good-strings.js | 0 ...st-group.js => 1399.count-largest-group.js | 0 ...n-prefix.js => 14.longest-common-prefix.js | 26 +-- 140-word-break-ii.js => 140.word-break-ii.js | 0 ...ucing-dishes.js => 1402.reducing-dishes.js | 0 ...-number-in-binary-representation-to-one.js | 0 ...-string.js => 1405.longest-happy-string.js | 0 ...tone-game-iii.js => 1406.stone-game-iii.js | 0 ....js => 1408.string-matching-in-an-array.js | 0 ... 1409.queries-on-a-permutation-with-key.js | 0 ...-list-cycle.js => 141.linked-list-cycle.js | 0 ...ty-parser.js => 1410.html-entity-parser.js | 0 ...> 1411.number-of-ways-to-paint-n-3-grid.js | 0 ...-value-to-get-positive-step-by-step-sum.js | 0 ...string-of-all-happy-strings-of-length-n.js | 0 ...-the-array.js => 1416.restore-the-array.js | 0 ...e-string.js => 1417.reformat-the-string.js | 0 ...cycle-ii.js => 142.linked-list-cycle-ii.js | 0 ....maximum-score-after-splitting-a-string.js | 0 ...erse-ii.js => 1424.diagonal-traverse-ii.js | 0 ....js => 1425.constrained-subsequence-sum.js | 0 ...-find-the-maximum-exactly-k-comparisons.js | 0 143-reorder-list.js => 143.reorder-list.js | 0 ...ids-with-the-greatest-number-of-candies.js | 0 ...ys-to-wear-different-hats-to-each-other.js | 0 ...nation-city.js => 1436.destination-city.js | 0 ...solute-diff-less-than-or-equal-to-limit.js | 0 ...allest-sum-of-a-matrix-with-sorted-rows.js | 0 ...s => 144.binary-tree-preorder-traversal.js | 54 +++--- ...41.build-an-array-with-stack-operations.js | 0 ...s-that-can-form-two-arrays-of-equal-xor.js | 0 ... 1444.number-of-ways-of-cutting-a-pizza.js | 0 ...cters.js => 1446.consecutive-characters.js | 0 ...=> 1448.count-good-nodes-in-binary-tree.js | 0 ...teger-with-digits-that-add-up-to-target.js | 0 ... => 145.binary-tree-postorder-traversal.js | 0 ...mpanies-is-not-a-subset-of-another-list.js | 0 ...458.max-dot-product-of-two-subsequences.js | 0 146-lru-cache.js => 146.lru-cache.js | 166 +++++++++--------- ...wo-arrays-equal-by-reversing-sub-arrays.js | 0 ...ing-contains-all-binary-codes-of-size-k.js | 0 ...hedule-iv.js => 1462.course-schedule-iv.js | 0 ...y-pickup-ii.js => 1463.cherry-pickup-ii.js | 0 ...mum-product-of-two-elements-in-an-array.js | 0 ...cake-after-horizontal-and-vertical-cuts.js | 0 ...aving-the-same-number-of-distinct-balls.js | 0 ...sort-list.js => 147.insertion-sort-list.js | 0 ...-the-array.js => 1470.shuffle-the-array.js | 0 ...story.js => 1472.design-browser-history.js | 0 ...nt-house-iii.js => 1473.paint-house-iii.js | 0 ...rices-with-a-special-discount-in-a-shop.js | 0 ...queries.js => 1476.subrectangle-queries.js | 0 ...mailboxes.js => 1478.allocate-mailboxes.js | 0 148-sort-list.js => 148.sort-list.js | 0 ...rray.js => 1480.running-sum-of-1d-array.js | 0 ...ber-of-unique-integers-after k-removals.js | 0 ...nimum-number-of-days-to-make-m-bouquets.js | 0 ....js => 1483.kth-ancestor-of-a-tree-node.js | 0 ...ay.js => 1486.xor-operation-in-an-array.js | 0 ...critical-edges-in-minimum-spanning-tree.js | 0 ...n-a-line.js => 149.max-points-on-a-line.js | 0 ...-n-ary-tree.js => 1490.clone-n-ary-tree.js | 0 ...tor-of-n.js => 1492.the-kth-factor-of-n.js | 0 ...barray-of-1s-after-deleting-one-element.js | 0 ...urses-ii.js => 1494.parallel-courses-ii.js | 0 ...es-that-satisfy-the-given-sum-condition.js | 0 ...uation.js => 1499.max-value-of-equation.js | 0 15-3sum.js => 15.3sum.js | 62 +++---- ...=> 150.evaluate-reverse-polish-notation.js | 0 ...ent-before-all-ants-fall-out-of-a-plank.js | 0 ...fter-at-most-k-adjacent-swaps-on-digits.js | 0 ...tree.js => 1506.find-root-of-n-ary-tree.js | 0 ...-reformat-date.js => 1507.reformat-date.js | 0 ...rgest-and-smallest-value-in-three-moves.js | 0 ...ing.js => 151.reverse-words-in-a-string.js | 24 +-- ...-stone-game-iv.js => 1510.stone-game-iv.js | 0 ...d-pairs.js => 1512.number-of-good-pairs.js | 0 ...s => 1514.path-with-maximum-probability.js | 0 ...1515.best-position-for-a-service-centre.js | 0 ....js => 1516.move-sub-tree-of-n-ary-tree.js | 0 ...rray.js => 152.maximum-product-subarray.js | 0 ...a-mysterious-function-closest-to-target.js | 0 ...-tree.js => 1522.diameter-of-n-ary-tree.js | 0 ....count-odd-numbers-in-an-interval-range.js | 0 ... 1524.number-of-sub-arrays-with-odd-sum.js | 0 ...5.number-of-good-ways-to-split-a-string.js | 0 ...nts-on-subarrays-to-form-a-target-array.js | 0 ...huffle-string.js => 1528.shuffle-string.js | 0 ...x-flips.js => 1529.minimum-suffix-flips.js | 0 ...53.find-minimum-in-rotated-sorted-array.js | 0 ...=> 1530.number-of-good-leaf-nodes-pairs.js | 0 ...ion-ii.js => 1531.string-compression-ii.js | 0 ...triplets.js => 1534.count-good-triplets.js | 0 ...-score.js => 1537.get-the-maximum-score.js | 0 ....js => 1539.kth-missing-positive-number.js | 0 ...find-minimum-in-rotated-sorted-array-ii.js | 0 ...s => 1540.can-convert-string-in-k-moves.js | 0 ...ertions-to-balance-a-parentheses-string.js | 0 ... => 1542.find-longest-awesome-substring.js | 0 ...apping-subarrays-with-sum-equals-target.js | 0 ....js => 1547.minimum-cost-to-cut-a-stick.js | 0 155-min-stack.js => 155.min-stack.js | 0 ...-odds.js => 1550.three-consecutive-odds.js | 0 ....minimum-operations-to-make-array-equal.js | 0 ...> 1552.magnetic-force-between-two-balls.js | 0 ...minimum-number-of-days-to-eat-n-oranges.js | 0 ...=> 1554.strings-differ-by-one-character.js | 0 ...m-number-of-vertices-to-reach-all-nodes.js | 0 ...rid.js => 1559.detect-cycles-in-2d-grid.js | 0 ...-down.js => 156.binary-tree-upside-down.js | 0 ...most-visited-sector-in-a-circular-track.js | 0 ...561.maximum-number-of-coins-you-can-get.js | 0 ....js => 1562.find-latest-group-of-size-m.js | 0 ... => 1564.put-boxes-into-the-warehouse-i.js | 0 ...ength-of-subarray-with-positive-product.js | 0 ...mum-number-of-days-to-disconnect-island.js | 0 ...f-ways-to-reorder-array-to-get-same-bst.js | 0 ...js => 157.read-n-characters-given-read4.js | 0 ... 1570.dot-product-of-two-sparse-vectors.js | 0 ...onal-sum.js => 1572.matrix-diagonal-sum.js | 0 ...> 1573.number-of-ways-to-split-a-string.js | 0 ...rray-to-be-removed-to-make-array-sorted.js | 0 ...es.js => 1575.count-all-possible-routes.js | 0 ...-avoid-consecutive-repeating-characters.js | 0 ...eletion-cost-to-avoid-repeating-letters.js | 0 ...f-edges-to-keep-graph-fully-traversable.js | 0 ...ters-given-read4-ii-call-multiple-times.js | 0 ... => 1584.min-cost-to-connect-all-points.js | 0 ...formable-with-substring-sort-operations.js | 0 ... => 1586.binary-search-tree-iterator-ii.js | 0 ...=> 1588.sum-of-all-odd-length-subarrays.js | 0 ...maximum-sum-obtained-of-any-permutation.js | 0 ...ng-with-at-most-two-distinct-characters.js | 0 ...by-p.js => 1590.make-sum-divisible-by-p.js | 0 ...rinter-ii.js => 1591.strange-printer-ii.js | 0 ... => 1592.rearrange-spaces-between-words.js | 0 ...nto-the-max-number-of-unique-substrings.js | 0 ...aximum-non-negative-product-in-a-matrix.js | 0 ...um-cost-to-connect-two-groups-of-points.js | 0 ...y-expression-tree-from-infix-expression.js | 0 ...og-folder.js => 1598.crawler-log-folder.js | 0 ...-profit-of-operating-a-centennial-wheel.js | 0 16-3sum-closest.js => 16.3sum-closest.js | 78 ++++---- ...=> 160.intersection-of-two-linked-lists.js | 0 ...heritance.js => 1600.throne-inheritance.js | 0 ...-number-of-achievable-transfer-requests.js | 0 ....find-nearest-right-node-in-binary-tree.js | 0 ...system.js => 1603.design-parking-system.js | 0 ...-valid-matrix-given-row-and-column-sums.js | 0 ...rs-that-handled-most-number-of-requests.js | 0 ...with-x-elements-greater-than-or-equal-x.js | 0 ...-even-odd-tree.js => 1609.even-odd-tree.js | 0 ...it-distance.js => 161.one-edit-distance.js | 0 ...> 1610.maximum-number-of-visible-points.js | 0 ...ne-bit-operations-to-make-integers-zero.js | 0 ...-if-two-expression-trees-are-equivalent.js | 0 ...aximum-nesting-depth-of-the-parentheses.js | 0 ...rk-rank.js => 1615.maximal-network-rank.js | 0 ...16.split-two-strings-to-make-palindrome.js | 0 ...btrees-with-max-distance-between-cities.js | 0 ...n-of-array-after-removing-some-elements.js | 0 ...eak-element.js => 162.find-peak-element.js | 0 ...sets-of-k-non-overlapping-line-segments.js | 0 ...ancy-sequence.js => 1622.fancy-sequence.js | 0 ...-substring-between-two-equal-characters.js | 0 ...allest-string-after-applying-operations.js | 0 ....js => 1626.best-team-with-no-conflicts.js | 0 ... 1627.graph-connectivity-with-threshold.js | 0 1629-slowest-key.js => 1629.slowest-key.js | 0 ...missing-ranges.js => 163.missing-ranges.js | 0 ...barrays.js => 1630.arithmetic-subarrays.js | 0 ...ort.js => 1631.path-with-minimum-effort.js | 0 ...x.js => 1632.rank-transform-of-a-matrix.js | 0 ...1636.sort-array-by-increasing-frequency.js | 0 ...between-two-points-containing-no-points.js | 0 ...substrings-that-differ-by-one-character.js | 0 ...form-a-target-string-given-a-dictionary.js | 0 164-maximum-gap.js => 164.maximum-gap.js | 0 ...k-array-formation-through-concatenation.js | 0 ...s.js => 1641.count-sorted-vowel-strings.js | 0 ...=> 1642.furthest-building-you-can-reach.js | 0 ...ns.js => 1643.kth-smallest-instructions.js | 0 ...est-common-ancestor-of-a-binary-tree-ii.js | 0 ... => 1646.get-maximum-in-generated-array.js | 0 ...ns-to-make-character-frequencies-unique.js | 0 ...8.sell-diminishing-valued-colored-balls.js | 0 ...reate-sorted-array-through-instructions.js | 0 ...mbers.js => 165.compare-version-numbers.js | 0 ...st-common-ancestor-of-a-binary-tree-iii.js | 0 ...use-the-bomb.js => 1652.defuse-the-bomb.js | 0 ...nimum-deletions-to-make-string-balanced.js | 0 ....js => 1654.minimum-jumps-to-reach-home.js | 0 ...s => 1655.distribute-repeating-integers.js | 0 ...eam.js => 1656.design-an-ordered-stream.js | 0 ...1657.determine-if-two-strings-are-close.js | 0 ....minimum-operations-to-reduce-x-to-zero.js | 0 ...ness.js => 1659.maximize-grid-happiness.js | 0 ...js => 166.fraction-to-recurring-decimal.js | 0 ...y-tree.js => 1660.correct-a-binary-tree.js | 0 ...eck-if-two-string-arrays-are-equivalent.js | 0 ...llest-string-with-a-given-numeric-value.js | 0 ...ay.js => 1664.ways-to-make-a-fair-array.js | 0 ....minimum-initial-energy-to-finish-tasks.js | 0 ...> 1666.change-the-root-of-a-binary-tree.js | 0 ....js => 1668.maximum-repeating-substring.js | 0 ...s => 1669.merge-in-between-linked-lists.js | 0 ...=> 167.two-sum-II-input-array-is-sorted.js | 46 ++--- ... => 1670.design-front-middle-back-queue.js | 0 ...mber-of-removals-to-make-mountain-array.js | 0 ...alth.js => 1672.richest-customer-wealth.js | 0 ...3.find-the-most-competitive-subsequence.js | 0 ...nimum-moves-to-make-array-complementary.js | 0 ....js => 1675.minimize-deviation-in-array.js | 0 ...est-common-ancestor-of-a-binary-tree-iv.js | 0 ...n.js => 1678.goal-parser-interpretation.js | 0 ...rs.js => 1679.max-number-of-k-sum-pairs.js | 0 ...itle.js => 168.excel-sheet-column-title.js | 0 ...atenation-of-consecutive-binary-numbers.js | 0 ...lity.js => 1681.minimum-incompatibility.js | 0 ....count-the-number-of-consistent-strings.js | 0 ...-absolute-differences-in-a-sorted-array.js | 0 ...-stone-game-vi.js => 1686.stone-game-vi.js | 0 ....delivering-boxes-from-storage-to-ports.js | 0 ... => 1688.count-of-matches-in-tournament.js | 0 ...o-minimum-number-of-deci-binary-numbers.js | 0 ...rity-element.js => 169.majority-element.js | 38 ++-- ...tone-game-vii.js => 1690.stone-game-vii.js | 0 ...1691.maximum-height-by-stacking-cuboids.js | 0 ...number.js => 1694.reformat-phone-number.js | 0 ...-value.js => 1695.maximum-erasure-value.js | 0 1696-jump-game-vi.js => 1696.jump-game-vi.js | 0 ...-existence-of-edge-length-limited-paths.js | 0 ...mber-of-distinct-substrings-in-a-string.js | 0 ...7.letter-combinations-of-a-phone-number.js | 78 ++++---- ...> 170.two-sum-iii-data-structure-design.js | 0 ....number-of-students-unable-to-eat-lunch.js | 0 ...ng-time.js => 1701.average-waiting-time.js | 0 ...1703.maximum-binary-string-after-change.js | 0 ...04.determine-if-string-halves-are-alike.js | 0 ...m-adjacent-swaps-for-k-consecutive-ones.js | 0 ... => 1705.maximum-number-of-eaten-apples.js | 0 ...all.js => 1706.where-will-the-ball-fall.js | 0 ....maximum-xor-with-an-element-from-array.js | 0 ...-k.js => 1708.largest-subarray-length-k.js | 0 ...ber.js => 171.excel-sheet-column-number.js | 32 ++-- ...uck.js => 1710.maximum-units-on-a-truck.js | 0 ...-good-meals.js => 1711.count-good-meals.js | 0 ...ays-to-split-array-into-three-subarrays.js | 0 ...inimum-operations-to-make-a-subsequence.js | 0 ...special-evenly-spaced-elements-in-array.js | 0 ...> 1716.calculate-money-in-leetcode-bank.js | 0 ....maximum-score-from-removing-substrings.js | 0 ...exicographically-largest-valid-sequence.js | 0 ...19.number-of-ways-to-reconstruct-a-tree.js | 0 ...oes.js => 172.factorial-trailing-zeroes.js | 0 ...red-array.js => 1720.decode-xored-array.js | 0 ...=> 1721.swapping-nodes-in-a-linked-list.js | 0 ...-hamming-distance-after-swap-operations.js | 0 ...23.find-minimum-time-to-finish-all-jobs.js | 0 ...istence-of-edge-length-limited-paths-ii.js | 0 ...angles-that-can-form-the-largest-square.js | 0 ...duct.js => 1726.tuple-with-same-product.js | 0 ...7.largest-submatrix-with-rearrangements.js | 0 ...nd-mouse-ii.js => 1728.cat-and-mouse-ii.js | 0 ...r.js => 173.binary-search-tree-iterator.js | 0 ...de.js => 1732.find-the-highest-altitude.js | 0 ... 1733.minimum-number-of-people-to-teach.js | 0 ...ion.js => 1734.decode-xored-permutation.js | 0 ...5.count-ways-to-make-array-with-product.js | 0 ....latest-time-by-replacing-hidden-digits.js | 0 ...ters-to-satisfy-one-of-three-conditions.js | 0 ...8.find-kth-largest-xor-coordinate-value.js | 0 ...uilding-boxes.js => 1739.building-boxes.js | 0 174-dungeon-game.js => 174.dungeon-game.js | 0 ... => 1740.find-distance-in-a-binary-tree.js | 0 ...> 1742.maximum-number-of-balls-in-a-box.js | 0 ...3.restore-the-array-from-adjacent-pairs.js | 0 ...our-favorite-candy-on-your-favorite-day.js | 0 ...v.js => 1745.palindrome-partitioning-iv.js | 0 ...aximum-subarray-sum-after-one-operation.js | 0 ...ments.js => 1748.sum-of-unique-elements.js | 0 ...49.maximum-absolute-sum-of-any-subarray.js | 0 ...h-of-string-after-deleting-similar-ends.js | 0 ...umber-of-events-that-can-be-attended-ii.js | 0 ...52.check-if-array-is-sorted-and-rotated.js | 0 ...1753.maximum-score-from-removing-stones.js | 0 ...js => 1754.largest-merge-of-two-strings.js | 0 ...-sum.js => 1755.closest-subsequence-sum.js | 0 ...anges-to-make-alternating-binary-string.js | 0 ...9.count-number-of-homogenous-substrings.js | 0 ...=> 1760.minimum-limit-of-balls-in-a-bag.js | 0 ...m-degree-of-a-connected-trio-in-a-graph.js | 0 ...js => 1762.buildings-with-an-ocean-view.js | 0 ...tring.js => 1763.longest-nice-substring.js | 0 ...oncatenating-subarrays-of-another-array.js | 0 ...est-peak.js => 1765.map-of-highest-peak.js | 0 ...of-coprimes.js => 1766.tree-of-coprimes.js | 0 ...ly.js => 1768.merge-strings-alternately.js | 0 ...perations-to-move-all-balls-to-each-box.js | 0 ...om-performing-multiplication-operations.js | 0 ...ize-palindrome-length-from-subsequences.js | 0 ....js => 1773.count-items-matching-a-rule.js | 0 ...rt-cost.js => 1774.closest-dessert-cost.js | 0 ...rrays-with-minimum-number-of-operations.js | 0 1776-car-fleet-ii.js => 1776.car-fleet-ii.js | 0 ...int-that-has-the-same-x-or-y-coordinate.js | 0 ...k-if-number-is-a-sum-of-powers-of-three.js | 0 ...=> 1781.sum-of-beauty-of-all-substrings.js | 0 ...-string-has-at-most-one-segment-of-ones.js | 0 ...mum-elements-to-add-to-form-a-given-sum.js | 0 ...estricted-paths-from-first-to-last-node.js | 0 ...e-the-xor-of-all-segments-equal-to-zero.js | 0 ...largest-number.js => 179.largest-number.js | 0 ...-one-string-swap-can-make-strings-equal.js | 0 ...ph.js => 1791.find-center-of-star-graph.js | 0 ...o.js => 1792.maximum-average-pass-ratio.js | 0 ...> 1793.maximum-score-of-a-good-subarray.js | 0 ...mber-of-consecutive-values-you-can-make.js | 0 ... 1799.maximize-score-after-n-operations.js | 0 18-4sum.js => 18.4sum.js | 0 ... => 1800.maximum-ascending-subarray-sum.js | 0 ...=> 1801.number-of-orders-in-the-backlog.js | 0 ...lue-at-a-given-index-in-a-bounded-array.js | 0 ...=> 1803.count-pairs-with-xor-in-a-range.js | 0 ...s => 1804.implement-trie-ii-prefix-tree.js | 0 ...umber-of-different-integers-in-a-string.js | 0 ...perations-to-reinitialize-a-permutation.js | 0 ....evaluate-the-bracket-pairs-of-a-string.js | 0 ...> 1808.maximize-number-of-nice-divisors.js | 0 ...m-number-of-groups-getting-fresh-donuts.js | 0 ...e-sentence.js => 1816.truncate-sentence.js | 0 ...> 1817.finding-the-users-active-minutes.js | 0 ...=> 1818.minimum-absolute-sum-difference.js | 0 ...9.number-of-different-subsequences-gcds.js | 0 ...=> 1822.sign-of-the-product-of-an-array.js | 0 ...23.find-the-winner-of-the-circular-game.js | 0 ...-jumps.js => 1824.minimum-sideway-jumps.js | 0 ...k-average.js => 1825.finding-mk-average.js | 0 ...operations-to-make-the-array-increasing.js | 0 ...ies-on-number-of-points-inside-a-circle.js | 0 ...y.js => 1829.maximum-xor-for-each-query.js | 0 ...ber-of-operations-to-make-string-sorted.js | 0 ...> 1832.check-if-the-sentence-is-pangram.js | 0 ...-bars.js => 1833.maximum-ice-cream-bars.js | 0 ...aded-cpu.js => 1834.single-threaded-cpu.js | 0 ...5.find-xor-sum-of-all-pairs-bitwise-and.js | 0 ...duplicates-from-an-unsorted-linked-list.js | 0 ...se-k.js => 1837.sum-of-digits-in-base-k.js | 0 ....frequency-of-the-most-frequent-element.js | 0 ...ongest-substring-of-all-vowels-in-order.js | 0 ...ight.js => 1840.maximum-building-height.js | 0 ...1844.replace-all-digits-with-characters.js | 0 ...lement-after-decreasing-and-rearranging.js | 0 1847-closest-room.js => 1847.closest-room.js | 0 ....minimum-distance-to-the-target-element.js | 0 ...ring-into-descending-consecutive-values.js | 0 ...-swaps-to-reach-the-kth-smallest-number.js | 0 ....minimum-interval-to-include-each-query.js | 0 ...year.js => 1854.maximum-population-year.js | 0 ...ximum-distance-between-a-pair-of-values.js | 0 ...js => 1856.maximum-subarray-min-product.js | 0 ...largest-color-value-in-a-directed-graph.js | 0 ...entence.js => 1859.sorting-the-sentence.js | 0 ....js => 186.reverse-words-in-a-string-ii.js | 0 ...leak.js => 1860.incremental-memory-leak.js | 0 ...ing-the-box.js => 1861.rotating-the-box.js | 0 ...d-pairs.js => 1862.sum-of-floored-pairs.js | 0 ...js => 1863.sum-of-all-subset-xor-totals.js | 0 ...s-to-make-the-binary-string-alternating.js | 0 ...> 1865.finding-pairs-with-a-certain-sum.js | 0 ...-rearrange-sticks-with-k-sticks-visible.js | 0 ...-contiguous-segments-of-ones-than-zeros.js | 0 ...uences.js => 187.repeated-dna-sequences.js | 0 ...=> 1870.minimum-speed-to-arrive-on-time.js | 0 ...-jump-game-vii.js => 1871.jump-game-vii.js | 0 ...ne-game-viii.js => 1872.stone-game-viii.js | 0 ...1874.minimize-product-sum-of-two-arrays.js | 0 ...-of-size-three-with-distinct-characters.js | 0 ...1877.minimize-maximum-pair-sum-in-array.js | 0 ...et-biggest-three-rhombus-sums-in-a-grid.js | 0 ...s => 1879.minimum-xor-sum-of-two-arrays.js | 0 ... 188.best-time-to-buy-and-sell-stock-iv.js | 0 ...k-if-word-equals-summation-of-two-words.js | 0 ...s => 1881.maximum-value-after-insertion.js | 0 ....js => 1882.process-tasks-using-servers.js | 0 ...imum-skips-to-arrive-at-meeting-on-time.js | 0 ... 1884.egg-drop-with-2-eggs-and-n-floors.js | 0 ...ys.js => 1885.count-pairs-in-two-arrays.js | 0 ...ther-matrix-can-be-obtained-by-rotation.js | 0 ...ations-to-make-the-array-elements-equal.js | 0 ...s-to-make-the-binary-string-alternating.js | 0 ...889.minimum-space-wasted-from-packaging.js | 0 189-rotate-array.js => 189.rotate-array.js | 0 ...ting-ribbons.js => 1891.cutting-ribbons.js | 0 ...all-the-integers-in-a-range-are-covered.js | 0 ...to-change-the-final-value-of-expression.js | 0 ...te-characters-to-make-all-strings-equal.js | 0 ....maximum-number-of-removable-characters.js | 0 ...9.merge-triplets-to-form-target-triplet.js | 0 ... => 19.remove-nth-node-from-end-of-list.js | 62 +++---- 190-reverse-bits.js => 190.reverse-bits.js | 0 ...and-latest-rounds-where-players-compete.js | 0 ...nt-ii.js => 1901.find-a-peak-element-ii.js | 0 ...js => 1903.largest-odd-number-in-string.js | 0 ...e-number-of-full-rounds-you-have-played.js | 0 ...ub-islands.js => 1905.count-sub-islands.js | 0 ...906.minimum-absolute-difference-queries.js | 0 ...t-to-make-the-array-strictly-increasing.js | 0 ...er-of-1-bits.js => 191.number-of-1-bits.js | 0 ...0.remove-all-occurrences-of-a-substring.js | 0 ...911.maximum-alternating-subsequence-sum.js | 0 ...um-product-difference-between-two-pairs.js | 0 ...4.cyclically-rotating-a-gridsubmissions.js | 0 ... => 1915.number-of-wonderful-substrings.js | 0 ...nt-ways-to-build-rooms-in-an-ant-colony.js | 0 ...um.js => 1918.kth-smallest-subarray-sum.js | 0 ...js => 1920.build-array-from-permutation.js | 0 ...21.eliminate-maximum-number-of-monsters.js | 0 ...d-numbers.js => 1922.count-good-numbers.js | 0 ...bpath.js => 1923.longest-common-subpath.js | 0 ...les.js => 1925.count-square-sum-triples.js | 0 ...nimum-cost-to-reach-destination-in-time.js | 0 ...array.js => 1929.concatenation-of-array.js | 0 ...nique-length-3-palindromic-subsequences.js | 0 ...ting-a-grid-with-three-different-colors.js | 0 ...=> 1932.merge-bsts-to-create-single-bst.js | 0 ...35.maximum-number-of-words-you-can-type.js | 0 ....js => 1936.add-minimum-number-of-rungs.js | 0 ...1937.maximum-number-of-points-with-cost.js | 0 ...> 1938.maximum-genetic-difference-query.js | 0 ...acters-have-equal-number-of-occurrences.js | 0 ...number-of-the-smallest-unoccupied-chair.js | 0 ...inting.js => 1943.describe-the-painting.js | 0 ...944.number-of-visible-people-in-a-queue.js | 0 ...5.sum-of-digits-of-string-after-convert.js | 0 ...largest-number-after-mutating-substring.js | 0 ...=> 1947.maximum-compatibility-score-sum.js | 0 ...1948.delete-duplicate-folders-in-system.js | 0 ...hree-divisors.js => 1952.three-divisors.js | 0 ...-number-of-weeks-for-which-you-can-work.js | 0 ...rden-perimeter-to-collect-enough-apples.js | 0 ...55.count-number-of-special-subsequences.js | 0 ...space-wasted-with-k-resizing-operations.js | 0 ...he-length-of-two-palindromic-substrings.js | 0 ...61.check-if-string-is-a-prefix-of-array.js | 0 ...962.remove-stones-to-minimize-the-total.js | 0 ...er-of-swaps-to-make-the-string-balanced.js | 0 ...-valid-obstacle-course-at-each-position.js | 0 ...-non-zero-product-of-the-array-elements.js | 0 ...1970.last-day-where-you-can-still-cross.js | 0 ...js => 1971.find-if-path-exists-in-graph.js | 0 ...number-of-ways-to-arrive-at-destination.js | 0 ...1977.number-of-ways-to-separate-numbers.js | 0 ...9.find-greatest-common-divisor-of-array.js | 0 198-house-robber.js => 198.house-robber.js | 0 ...ng.js => 1980.find-unique-binary-string.js | 0 ...ence-between-target-and-chosen-elements.js | 0 ...js => 1982.find-array-given-subset-sums.js | 0 ...st-pair-of-indices-with-equal-range-sum.js | 0 ...-between-highest-and-lowest-of-k-scores.js | 0 ...nd-the-kth-largest-integer-in-the-array.js | 0 ...er-of-work-sessions-to-finish-the-tasks.js | 0 ...1987.number-of-unique-good-subsequences.js | 0 ...w.js => 199.binary-tree-right-side-view.js | 0 ... => 1991.find-the-middle-index-in-array.js | 0 ...s.js => 1994.the-number-of-good-subsets.js | 0 ...ts.js => 1995.count-special-quadruplets.js | 0 ...e-number-of-weak-characters-in-the-game.js | 0 ...ay-where-you-have-been-in-all-the-rooms.js | 0 ...n-array.js => 1998.gcd-sort-of-an-array.js | 0 2-add-two-numbers.js => 2.add-two-numbers.js | 58 +++--- ...-parentheses.js => 20.valid-parentheses.js | 32 ++-- ...-of-islands.js => 200.number-of-islands.js | 0 ...-word.js => 2000.reverse-prefix-of-word.js | 0 ...-of-pairs-of-interchangeable-rectangles.js | 0 ...-length-of-two-palindromic-subsequences.js | 0 ...t-missing-genetic-value-in-each-subtree.js | 0 ...ber-of-pairs-with-absolute-difference-k.js | 0 ....find-original-array-from-doubled-array.js | 0 ...i.js => 2008.maximum-earnings-from-taxi.js | 0 ...-of-operations-to-make-array-continuous.js | 0 ....js => 201.bitwise-and-of-numbers-range.js | 0 ...of-variable-after-performing-operations.js | 0 ...y.js => 2012.sum-of-beauty-in-the-array.js | 0 ...etect-squares.js => 2013.detect-squares.js | 0 ...14.longest-subsequence-repeated-k-times.js | 0 ...age-height-of-buildings-in-each-segment.js | 0 ...heck-if-word-can-be-placed-in-crossword.js | 0 ...ore-of-students-solving-math-expression.js | 0 202-happy-number.js => 202.happy-number.js | 38 ++-- ...ings-with-concatenation-equal-to-target.js | 0 ... 2024.maximize-the-confusion-of-an-exam.js | 0 ...um-number-of-ways-to-partition-an-array.js | 0 ...-stone-game-ix.js => 2029.stone-game-ix.js | 0 ...s.js => 203.remove-linked-list-elements.js | 0 ...ubsequence-with-occurrences-of-a-letter.js | 0 ...unt-subarrays-with-more-ones-than-zeros.js | 0 ...ut-of-three.js => 2032.two-out-of-three.js | 0 ...mum-operations-to-make-a-uni-value-grid.js | 0 ...tion.js => 2034.stock-price-fluctuation.js | 0 ...o-two-arrays-to-minimize-sum-difference.js | 0 ...inimum-number-of-moves-to-seat-everyone.js | 0 ...es-if-both-neighbors-are-the-same-color.js | 0 204-count-primes.js => 204.count-primes.js | 46 ++--- ...h-smallest-product-of-two-sorted-arrays.js | 0 ...-if-numbers-are-ascending-in-a-sentence.js | 0 ...nk-system.js => 2043.simple-bank-system.js | 0 ...nt-number-of-maximum-bitwise-or-subsets.js | 0 ...econd-minimum-time-to-reach-destination.js | 0 ...st-already-sorted-using-absolute-values.js | 0 ...047.number-of-valid-words-in-a-sentence.js | 0 ...ext-greater-numerically-balanced-number.js | 0 ...2049.count-nodes-with-the-highest-score.js | 0 ...ic-strings.js => 205.isomorphic-strings.js | 0 ...ses-iii.js => 2050.parallel-courses-iii.js | 0 ...=> 2053.kth-distinct-string-in-an-array.js | 0 ...=> 2054.two-best-non-overlapping-events.js | 0 ...ndles.js => 2055.plates-between-candles.js | 0 ...=> 2057.smallest-index-with-equal-value.js | 0 ...number-of-nodes-between-critical-points.js | 0 ...59.minimum-operations-to-convert-number.js | 0 ...nked-list.js => 206.reverse-linked-list.js | 50 +++--- ...string-exists-given-two-encoded-strings.js | 0 ...2062.count-vowel-substrings-of-a-string.js | 0 ...ngs.js => 2063.vowels-of-all-substrings.js | 0 ...um-of-products-distributed-to-any-store.js | 0 ...=> 2065.maximum-path-quality-of-a-graph.js | 0 ...ether-two-strings-are-almost-equivalent.js | 0 ....js => 2069.walking-robot-simulation-ii.js | 0 ...urse-schedule.js => 207.course-schedule.js | 0 ....maximum-number-of-tasks-you-can-assign.js | 0 ...s.js => 2073.time-needed-to-buy-tickets.js | 0 ...074.reverse-nodes-in-even-length-groups.js | 0 ...s => 2075.decode-the-slanted-ciphertext.js | 0 ...2076.process-restricted-friend-requests.js | 0 ...o-furthest-houses-with-different-colors.js | 0 ...ering-plants.js => 2079.watering-plants.js | 0 ...ee.js => 208.implement-trie-prefix-tree.js | 0 ...ries.js => 2080.range-frequency-queries.js | 0 ...bers.js => 2081.sum-of-k-mirror-numbers.js | 0 ...that-begin-and-end-with-the-same-letter.js | 0 ...quired-to-collect-rainwater-from-houses.js | 0 ...> 2088.count-fertile-pyramids-in-a-land.js | 0 ...find-target-indices-after-sorting-array.js | 0 ...sum.js => 209.minimum-size-subarray-sum.js | 0 ...s.js => 2090.k-radius-subarray-averages.js | 0 ...removing-minimum-and-maximum-from-array.js | 0 ....js => 2092.find-all-people-with-secret.js | 0 ...js => 2094.finding-3-digit-even-numbers.js | 0 ...delete-the-middle-node-of-a-linked-list.js | 0 ...ions-from-a-binary-tree-node-to-another.js | 0 ...s.js => 2097.valid-arrangement-of-pairs.js | 0 ...nce-of-size-k-with-the-largest-even-sum.js | 0 ...d-lists.js => 21.merge-two-sorted-lists.js | 50 +++--- ...chedule-ii.js => 210.course-schedule-ii.js | 0 ... => 2100.find-good-days-to-rob-the-bank.js | 0 ...s.js => 2101.detonate-the-maximum-bombs.js | 0 ... 2102.sequentially-ordinal-rank-tracker.js | 0 ...ings-and-rods.js => 2103.rings-and-rods.js | 0 ...anges.js => 2104.sum-of-subarray-ranges.js | 0 ...plants-ii.js => 2105.watering-plants-ii.js | 0 ...-fruits-harvested-after-at-most-k-steps.js | 0 ...d-and-search-word-data-structure-design.js | 0 ...erations-to-make-the-array-k-increasing.js | 0 ...ll-possible-recipes-from-given-supplies.js | 0 ...ck-if-a-parentheses-string-can-be-valid.js | 0 ...> 2119.a-number-after-a-double-reversal.js | 0 ...word-search-ii.js => 212.word-search-ii.js | 0 ...21.intervals-between-identical-elements.js | 0 ...y.js => 2122.recover-the-original-array.js | 0 ...4.check-if-all-as-appears-before-all-bs.js | 0 ...=> 2125.number-of-laser-beams-in-a-bank.js | 0 ...teroids.js => 2126.destroying-asteroids.js | 0 ...um-employees-to-be-invited-to-a-meeting.js | 0 ...move-all-ones-with-row-and-column-flips.js | 0 ...use-robber-ii.js => 213.house-robber-ii.js | 0 ... 2130.maximum-twin-sum-of-a-linked-list.js | 0 ...g-the-grid.js => 2132.stamping-the-grid.js | 0 ...136.earliest-possible-day-of-full-bloom.js | 0 ...8.divide-a-string-into-groups-of-size-k.js | 0 ...139.minimum-moves-to-reach-target-score.js | 0 ...alindrome.js => 214.shortest-palindrome.js | 0 ... 2140.solving-questions-with-brainpower.js | 0 ...141.maximum-running-time-of-n-computers.js | 0 ...h-strictly-smaller-and-greater-elements.js | 0 ...> 2149.rearrange-array-elements-by-sign.js | 0 ... => 215.kth-largest-element-in-an-array.js | 52 +++--- ...50.find-all-lonely-numbers-in-the-array.js | 0 ...maximum-good-people-based-on-statements.js | 0 ...minimum-number-of-lines-to-cover-points.js | 0 ...54.keep-multiplying-found-values-by-two.js | 0 ...ith-the-highest-score-of-a-binary-array.js | 0 ...56.find-substring-with-given-hash-value.js | 0 ...n-sum-III.js => 216.combination-sum-III.js | 50 +++--- ...artition-array-according-to-given-pivot.js | 0 ...rence-in-sums-after-removal-of-elements.js | 0 ...sort-even-and-odd-indices-independently.js | 0 ...smallest-value-of-the-rearranged-number.js | 0 ...-design-bitset.js => 2166.design-bitset.js | 0 ...emove-all-cars-containing-illegal-goods.js | 0 ...=> 2169.count-operations-to-obtain-zero.js | 0 ...-duplicate.js => 217.contains-duplicate.js | 30 ++-- ...ray.js => 2172.maximum-and-sum-of-array.js | 0 ...maximum-split-of-positive-even-integers.js | 0 ...=> 2179.count-good-triplets-in-an-array.js | 0 ...e-problem.js => 218.the-skyline-problem.js | 0 ...2180.count-integers-with-even-digit-sum.js | 0 ...js => 2181.merge-nodes-in-between-zeros.js | 0 ...2182.construct-string-with-repeat-limit.js | 0 ...> 2183.count-array-pairs-divisible-by-k.js | 0 ...mber-of-ways-to-build-sturdy-brick-wall.js | 0 ...=> 2188.minimum-time-to-finish-the-race.js | 0 ....number-of-ways-to-build-house-of-cards.js | 0 ...cate-ii.js => 219.contains-duplicate-ii.js | 0 ...s-of-a-node-in-a-directed-acyclic-graph.js | 0 ...imum-number-of-moves-to-make-palindrome.js | 0 ...97.replace-non-coprime-numbers-in-array.js | 0 ...rentheses.js => 22.generate-parentheses.js | 34 ++-- ...te-iii.js => 220.contains-duplicate-iii.js | 0 ...distance-to-a-cycle-in-undirected-graph.js | 0 ...white-tiles-after-covering-with-carpets.js | 0 ...maximal-square.js => 221.maximal-square.js | 0 ...210.count-hills-and-valleys-in-an-array.js | 0 ...d.js => 2211.count-collisions-on-a-road.js | 0 ...aximum-points-in-an-archery-competition.js | 0 ...st-substring-of-one-repeating-character.js | 0 ....js => 2214.minimum-health-to-beat-game.js | 0 ... 2215.find-the-difference-of-two-arrays.js | 0 ...nimum-deletions-to-make-array-beautiful.js | 0 ... 2217.find-palindrome-with-fixed-length.js | 0 ...218.maximum-value-of-k-coins-from-piles.js | 0 ...des.js => 222.count-complete-tree-nodes.js | 0 ...=> 2221.find-triangular-sum-of-an-array.js | 0 ...2222.number-of-ways-to-select-buildings.js | 0 ... => 2223.sum-of-scores-of-built-strings.js | 0 ...um-number-of-operations-to-convert-time.js | 0 ...25.find-players-with-zero-or-one-losses.js | 0 ...maximum-candies-allocated-to-k-children.js | 0 ....js => 2227.encrypt-and-decrypt-strings.js | 0 ...rectangle-area.js => 223.rectangle-area.js | 0 ...2233.maximum-product-after-k-increments.js | 0 ...234.maximum-total-beauty-of-the-gardens.js | 0 ...wo-integers.js => 2235.add-two-integers.js | 0 ...ions-on-street-with-required-brightness.js | 0 ...c-calculator.js => 224.basic-calculator.js | 0 ...> 2242.maximum-score-of-a-node-sequence.js | 0 ...=> 2243.calculate-digit-sum-of-a-string.js | 0 ...44.minimum-rounds-to-complete-all-tasks.js | 0 ...ximum-trailing-zeros-in-a-cornered-path.js | 0 ...6.determine-if-two-events-have-conflict.js | 0 ...path-with-different-adjacent-characters.js | 0 ...=> 2248.intersection-of-multiple-arrays.js | 0 ...49.count-lattice-points-inside-a-circle.js | 0 ....js => 225.implement-stack-using-queues.js | 0 ...ber-of-rectangles-containing-each-point.js | 0 ...=> 2251.number-of-flowers-in-full-bloom.js | 0 ...re.js => 2258.escape-the-spreading-fire.js | 0 ...ve-digit-from-number-to-maximize-result.js | 0 ...inary-tree.js => 226.invert-binary-tree.js | 0 ...60.minimum-consecutive-cards-to-pick-up.js | 0 ... => 2261.k-divisible-elements-subarrays.js | 0 ...ing.js => 2262.total-appeal-of-a-string.js | 0 ...-array-non-decreasing-or-non-increasing.js | 0 ...4.largest-3-same-digit-number-in-string.js | 0 ...count-nodes-equal-to-average-of-subtree.js | 0 ...-texts.js => 2266.count-number-of-texts.js | 0 ...here-is-a-valid-parentheses-string-path.js | 0 ...js => 2268.minimum-number-of-keypresses.js | 0 ...ulator-ii.js => 227.basic-calculator-ii.js | 0 ...s => 2270.number-of-ways-to-split-array.js | 0 ...maximum-white-tiles-covered-by-a-carpet.js | 0 ...=> 2272.substring-with-largest-variance.js | 0 ...resultant-array-after-removing-anagrams.js | 0 ...nsecutive-floors-without-special-floors.js | 0 ...tion-with-bitwise-and-greater-than-zero.js | 0 ....js => 2276.count-integers-in-intervals.js | 0 ...js => 2277.closest-node-to-path-in-tree.js | 0 ...summary-ranges.js => 228.summary-ranges.js | 0 ...minimum-lines-to-represent-a-line-chart.js | 0 ...> 2281.sum-of-total-strength-of-wizards.js | 0 ...er-of-people-that-can-be-seen-in-a-grid.js | 0 ... 2286.booking-concert-tickets-in-groups.js | 0 ...rrange-characters-to-make-target-string.js | 0 ...ces.js => 2288.apply-discount-to-prices.js | 0 ...2289.steps-to-make-array-non-decreasing.js | 0 ...lement-ii.js => 229.majority-element-ii.js | 0 ...inimum-obstacle-removal-to-reach-corner.js | 0 ...2291.maximum-profit-from-trading-stocks.js | 0 2293-min-max-game.js => 2293.min-max-game.js | 0 ...array-such-that-maximum-difference-is-k.js | 0 ...js => 2295.replace-elements-in-an-array.js | 0 ...-editor.js => 2296.design-a-text-editor.js | 0 ...ted-lists.js => 23.merge-k-sorted-lists.js | 0 ...js => 230.kth-smallest-element-in-a-bst.js | 0 ....count-subarrays-with-score-less-than-k.js | 0 ... => 2303.calculate-amount-paid-in-taxes.js | 0 ....js => 2304.minimum-path-cost-in-a-grid.js | 0 ...js => 2305.fair-distribution-of-cookies.js | 0 ...g-a-company.js => 2306.naming-a-company.js | 0 231-power-of-two.js => 231.power-of-two.js | 0 ...ary-subsequence-less-than-or-equal-to-k.js | 0 ...-wood.js => 2312.selling-pieces-of-wood.js | 0 ...imum-flips-in-binary-tree-to-get-result.js | 0 ...js => 2317.maximum-xor-after-operations.js | 0 ... 2318.number-of-distinct-roll-sequences.js | 0 ....js => 2319.check-if-matrix-is-x-matrix.js | 0 ....js => 232.implement-queue-using-stacks.js | 0 ...20.count-number-of-ways-to-place-houses.js | 0 ... => 2321.maximum-score-of-spliced-array.js | 0 ....minimum-score-after-removals-on-a-tree.js | 0 ...e-message.js => 2325.decode-the-message.js | 0 ...l-matrix-iv.js => 2326.spiral-matrix-iv.js | 0 ...2327.number-of-people-aware-of-a-secret.js | 0 ...28.number-of-increasing-paths-in-a-grid.js | 0 ...digit-one.js => 233.number-of-digit-one.js | 0 ... => 2332.the-latest-time-to-catch-a-bus.js | 0 ... 2333.minimum-sum-of-squared-difference.js | 0 ...elements-greater-than-varying-threshold.js | 0 ...335.minimum-amount-of-time-to-fill-cups.js | 0 ...=> 2336.smallest-number-in-infinite-set.js | 0 ... => 2337.move-pieces-to-obtain-a-string.js | 0 ...> 2338.count-the-number-of-ideal-arrays.js | 0 ...d-list.js => 234.palindrome-linked-list.js | 0 ...um-adjacent-swaps-to-make-a-valid-array.js | 0 ...> 2341.maximum-number-of-pairs-in-array.js | 0 ...-sum-of-a-pair-with-equal-sum-of-digits.js | 0 ... 2343.query-kth-smallest-trimmed-number.js | 0 ...nimum-deletions-to-make-array-divisible.js | 0 ...t-poker-hand.js => 2347.best-poker-hand.js | 0 ...=> 2348.number-of-zero-filled-subarrays.js | 0 ...common-ancestor-of-a-binary-search-tree.js | 0 ...0.shortest-impossible-sequence-of-rolls.js | 0 ...js => 2351.first-letter-to-appear-twice.js | 0 ...s.js => 2352.equal-row-and-column-pairs.js | 0 ....js => 2353.design-a-food-rating-system.js | 0 ...rs.js => 2354.number-of-excellent-pairs.js | 0 ...55.maximum-number-of-books-you-can-take.js | 0 ...array-zero-by-subtracting-equal-amounts.js | 0 ...number-of-groups-entering-a-competition.js | 0 ...59.find-closest-node-to-given-two-nodes.js | 0 ...lowest-common-ancestor-of-a-binary-tree.js | 0 ...aph.js => 2360.longest-cycle-in-a-graph.js | 0 ...heduler-ii.js => 2365.task-scheduler-ii.js | 0 ....minimum-replacements-to-sort-the-array.js | 0 ...rs.js => 2367.count-number-of-bad-pairs.js | 0 ...s => 2367.number-of-arithmetic-triplets.js | 0 ... 2368.reachable-nodes-with-restrictions.js | 0 ...here-is-a-valid-partition-for-the-array.js | 0 ....js => 237.delete-node-in-a-linked-list.js | 34 ++-- ...ce.js => 2370.longest-ideal-subsequence.js | 0 ...> 2373.largest-local-values-in-a-matrix.js | 0 ...js => 2374.node-with-highest-edge-score.js | 0 ...onstruct-smallest-number-from-di-string.js | 0 ...egers.js => 2376.count-special-integers.js | 0 ....js => 238.product-of-array-except-self.js | 54 +++--- ...ime-needed-to-rearrange-a-binary-string.js | 0 ...tters-ii.js => 2381.shifting-letters-ii.js | 0 ...2382.maximum-segment-sum-after-removals.js | 0 ...-hours-of-training-to-win-a-competition.js | 0 ...r.js => 2384.largest-palindromic-number.js | 0 ...-of-time-for-binary-tree-to-be-infected.js | 0 ...y.js => 2386.find-the-k-sum-of-an-array.js | 0 ...89.longest-subsequence-with-limited-sum.js | 0 ...aximum.js => 239.sliding-window-maximum.js | 0 ...js => 2390.removing-stars-from-a-string.js | 0 ...nimum-amount-of-time-to-collect-garbage.js | 0 ... => 2392.build-a-matrix-with-conditions.js | 0 ...=> 2397.maximum-rows-covered-by-columns.js | 0 ....maximum-number-of-robots-within-budget.js | 0 ...99.check-distances-between-same-letters.js | 0 ...s-in-pairs.js => 24.swap-nodes-in-pairs.js | 66 +++---- ...trix-ii.js => 240.search-a-2d-matrix-ii.js | 0 ...-reach-a-position-after-exactly-k-steps.js | 0 ...barray.js => 2401.longest-nice-subarray.js | 0 ...-rooms-iii.js => 2402.meeting-rooms-iii.js | 0 ... 2403.minimum-time-to-kill-all-monsters.js | 0 ...t.js => 2404.most-frequent-even-element.js | 0 ....js => 2405.optimal-partition-of-string.js | 0 ...intervals-into-minimum-number-of-groups.js | 0 ... 2407.longest.increasing-subsequence-ii.js | 0 ...> 241.different-ways-to-add-parentheses.js | 72 ++++---- ...llest-subarrays-with-maximum-bitwise-or.js | 0 ...imum-money-required-before-transactions.js | 0 ...tiple.js => 2413.smallest-even-multiple.js | 0 ...ngest-alphabetical-continuous-substring.js | 0 ... 2415.reverse-odd-levels-of-binary-tree.js | 0 ...=> 2416.sum-of-prefix-scores-of-strings.js | 0 ...t-the-people.js => 2418.sort-the-people.js | 0 ...ngest-subarray-with-maximum-bitwise-and.js | 0 242-valid-anagram.js => 242.valid-anagram.js | 56 +++--- ...ndices.js => 2420.find-all-good-indices.js | 0 ...d-paths.js => 2421.number-of-good-paths.js | 0 ...rations-to-turn-array-into-a-palindrome.js | 0 ...423.remove-letter-to-equalize-frequency.js | 0 ...6.number-of-pairs-satisfying-inequality.js | 0 ...ors.js => 2427.number-of-common-factors.js | 0 ....js => 2428.maximum-sum-of-an-hourglass.js | 0 2429-minimize-xor.js => 2429.minimize-xor.js | 0 ...stance.js => 243.shortest-word-distance.js | 0 ...s => 2430.maximum-deletions-on-a-string.js | 0 ...mployee-that-worked-on-the-longest-task.js | 0 ...3.find-the-original-array-of-prefix-xor.js | 0 ...t-the-lexicographically-smallest-string.js | 0 ...s-in-matrix-whose-sum-is-divisible-by-k.js | 0 ...=> 2438.range-product-queries-of-powers.js | 0 ...ay.js => 2439.minimize-maximum-of-array.js | 0 ...-ii.js => 244.shortest-word-distance-ii.js | 0 ...e-integer-that-exists-with-its-negative.js | 0 ...tinct-integers-after-reverse-operations.js | 0 ...s => 2443.sum-of-number-and-its-reverse.js | 0 ... 2444.count-subarrays-with-fixed-bounds.js | 0 ...number-of-subarrays-with-gcd-equal-to-k.js | 0 ...> 2448.minimum-cost-to-make-array-equal.js | 0 ...er-of-operations-to-make-arrays-similar.js | 0 ...ii.js => 245.shortest-word-distance-iii.js | 0 ...s.js => 2453.destroy-sequential-targets.js | 0 ...t-iv.js => 2454.next-greater-element-iv.js | 0 ...imum-addition-to-make-integer-beautiful.js | 0 ...number.js => 246.strobogrammatic-number.js | 0 ...js => 2460.apply-operations-to-an-array.js | 0 ...sum-of-distinct-subarrays-with-length-k.js | 0 ...js => 2462.total-cost-to-hire-k-workers.js | 0 ...=> 2463.minimum-total-distance-traveled.js | 0 ...ture.js => 2469.convert-the-temperature.js | 0 ...-ii.js => 247.strobogrammatic-number-ii.js | 0 ...number-of-subarrays-with-lcm-equal-to-k.js | 0 ...erations-to-sort-a-binary-tree-by-level.js | 0 ...f-non-overlapping-palindrome-substrings.js | 0 ...475.number-of-unequal-triplets-in-array.js | 0 ...t-nodes-queries-in-a-binary-search-tree.js | 0 ...imum-fuel-cost-to-report-to-the-capital.js | 0 ... => 2478.number-of-beautiful-partitions.js | 0 ...ii.js => 248.strobogrammatic-number-iii.js | 0 ... => 2484.count-palindromic-subsequences.js | 0 ...teger.js => 2485.find-the-pivot-integer.js | 0 ...haracters-to-string-to-make-subsequence.js | 0 ...s => 2487.remove-nodes-from-linked-list.js | 0 ...s => 2488.count-subarrays-with-median-k.js | 0 ...strings.js => 249.group-shifted-strings.js | 0 ...r-sentence.js => 2490.circular-sentence.js | 0 ...ivide-players-into-teams-of-equal-skill.js | 0 ...imum-score-of-a-path-between-two-cities.js | 0 ...nodes-into-the-maximum-number-of-groups.js | 0 ...group.js => 25.reverse-nodes-in-k-group.js | 0 ...trees.js => 250.count-univalue-subtrees.js | 0 ...imum-number-of-points-from-grid-queries.js | 0 ...edges-to-make-degrees-of-all-nodes-even.js | 0 ...n-2d-vector.js => 251.flatten-2d-vector.js | 0 ...ount-anagrams.js => 2514.count-anagrams.js | 0 ...k-of-each-character-from-left-and-right.js | 0 ...s.js => 2518.number-of-great-partitions.js | 0 252-meeting-rooms.js => 252.meeting-rooms.js | 0 ...tinct-prime-factors-of-product-of-array.js | 0 ... => 2523.closest-prime-numbers-in-range.js | 0 ...ray.js => 2527.find-xor-beauty-of-array.js | 0 ...f-positive-integer-and-negative-integer.js | 0 ...ing-rooms-ii.js => 253.meeting-rooms-ii.js | 0 ...ximal-score-after-applying-k-operations.js | 0 ...ake-number-of-distinct-characters-equal.js | 0 ...2537.count-the-number-of-good-subarrays.js | 0 ...e-between-maximum-and-minimum-price-sum.js | 0 ...binations.js => 254.factor-combinations.js | 0 ...re.js => 2542.maximum-subsequence-score.js | 0 ....js => 2543.check-if-point-is-reachable.js | 0 ...preorder-sequence-in-binary-search-tree.js | 0 ...-in-bags.js => 2551.put-marbles-in-bags.js | 0 ... => 2555.maximize-win-from-two-segments.js | 0 ...-in-a-binary-matrix-by-at-most-one-flip.js | 0 ...r-of-integers-to-choose-from-a-range-ii.js | 0 ...> 2558.take-gifts-from-the-richest-pile.js | 0 ...s => 2559.count-vowel-strings-in-ranges.js | 0 256-paint-house.js => 256.paint-house.js | 0 ...se-robber-iv.js => 2560.house-robber-iv.js | 0 ...ng-fruits.js => 2561.rearranging-fruits.js | 0 ... => 2563.count-the-number-of-fair-pairs.js | 0 ...ueries.js => 2564.substring-xor-queries.js | 0 ...2565.subsequence-with-the-minimum-score.js | 0 ....minimum-score-by-changing-two-elements.js | 0 ...ble-or.js => 2568.minimum-impossible-or.js | 0 ...-tree-paths.js => 257.binary-tree-paths.js | 60 +++---- ...0.merge-two-2d-arrays-by-summing-values.js | 0 ...um-operations-to-reduce-an-integer-to-0.js | 0 ...count-the-number-of-square-free-subsets.js | 0 ...find-the-divisibility-array-of-a-string.js | 0 ...lcp.js => 2573.find-the-string-with-lcp.js | 0 ... => 2574.left-and-right-sum-differences.js | 0 ...nd-the-maximum-number-of-marked-indices.js | 0 ....minimum-time-to-visit-a-cell-in-a-grid.js | 0 258-add-digits.js => 258.add-digits.js | 30 ++-- ...s-the-pillow.js => 2582.pass-the-pillow.js | 0 ...> 2583.kth-largest-sum-in-a-binary-tree.js | 0 ...plit-the-array-to-make-coprime-products.js | 0 ...s => 2585.number-of-ways-to-earn-points.js | 0 259-3sum-smaller.js => 259.3sum-smaller.js | 0 ...> 2599.make-the-prefix-sum-non-negative.js | 0 ... 26.remove-duplicates-from-sorted-array.js | 36 ++-- ...-number-III.js => 260.single-number-III.js | 0 ... => 2604.minimum-time-to-eat-all-grains.js | 0 ...l.js => 2607.make-k-subarray-sums-equal.js | 0 ...ph.js => 2608.shortest-cycle-in-a-graph.js | 0 ...t-balanced-substring-of-a-binary-string.js | 0 ...h-valid-tree.js => 261.graph-valid-tree.js | 0 ...n-array-into-a-2d-array-with-conditions.js | 0 ...e-and-cheese.js => 2611.mice-and-cheese.js | 0 ...n-diagonal.js => 2614.prime-in-diagonal.js | 0 ...f-distances.js => 2615.sum-of-distances.js | 0 ...inimize-the-maximum-difference-of-pairs.js | 0 ...pe-last.js => 2619.array-prototype-last.js | 0 ....js => 2626.array-reduce-transformation.js | 0 2627-debounce.js => 2627.debounce.js | 0 263-ugly-number.js => 263.ugly-number.js | 0 2630-memoize-ii.js => 2630.memoize-ii.js | 0 2632-curry.js => 2632.curry.js | 0 2636-promise-pool.js => 2636.promise-pool.js | 0 ...ugly-number-ii.js => 264.ugly-number-ii.js | 0 ...d-the-score-of-all-prefixes-of-an-array.js | 0 ...ign-graph-with-shortest-path-calculator.js | 0 ...m-ones.js => 2643.row-with-maximum-ones.js | 0 ...644.find-the-maximum-divisibility-score.js | 0 ....minimum-additions-to-make-valid-string.js | 0 ...6.minimize-the-total-price-of-the-trips.js | 0 ...paint-house-ii.js => 265.paint-house-ii.js | 0 ... => 2651.calculate-delayed-arrival-time.js | 0 ...-sum-multiples.js => 2652.sum-multiples.js | 0 ...auty.js => 2653.sliding-subarray-beauty.js | 0 ...s-to-make-all-array-elements-equal-to-1.js | 0 ...> 2658.maximum-number-of-fish-in-a-grid.js | 0 ...tation.js => 266.palindrome-permutation.js | 0 ....determine-the-winner-of-a-bowling-game.js | 0 ....first-completely-painted-row-or-column.js | 0 ...nimum-cost-of-a-path-with-special-roads.js | 0 ...cographically-smallest-beautiful-string.js | 0 ...-ii.js => 267.palindrome-permutation-ii.js | 0 ...2670.find-the-distinct-difference-array.js | 0 ...cy-tracker.js => 2671.frequency-tracker.js | 0 ...f-adjacent-elements-with-the-same-color.js | 0 ...e-costs-of-paths-equal-in-a-binary-tree.js | 0 ...missing-number.js => 268.missing-number.js | 22 +-- ...er-of-heroes.js => 2681.power-of-heroes.js | 0 ...82.find-the-losers-of-the-circular-game.js | 0 ...-xor.js => 2683.neighboring-bitwise-xor.js | 0 ... 2684.maximum-number-of-moves-in-a-grid.js | 0 ...count-the-number-of-complete-components.js | 0 ...n-dictionary.js => 269.alien-dictionary.js | 0 27-remove-element.js => 27.remove-element.js | 30 ++-- ...=> 270.closest-binary-search-tree-value.js | 0 ...operations-to-make-numbers-non-positive.js | 0 ... 2709.greatest-common-divisor-traversal.js | 0 ...ngs.js => 271.encode-and-decode-strings.js | 0 ...710.remove-trailing-zeros-from-a-string.js | 0 ...-number-of-distinct-values-on-diagonals.js | 0 ...nimum-cost-to-make-all-characters-equal.js | 0 ...m-strictly-increasing-cells-in-a-matrix.js | 0 ...272.closest-binary-search-tree-value-ii.js | 0 ...ords.js => 273.integer-to-english-words.js | 0 ...d-the-longest-semi-repetitive-substring.js | 0 274-h-index.js => 274.h-index.js | 0 ...s.js => 2747.count-zero-request-servers.js | 0 275-h-index-ii.js => 275.h-index-ii.js | 0 276-paint-fence.js => 276.paint-fence.js | 0 ...ongest-even-odd-subarray-with-threshold.js | 0 ....js => 2761.prime-pairs-with-target-sum.js | 0 ...barrays.js => 2762.continuous-subarrays.js | 0 ...m-of-imbalance-numbers-of-all-subarrays.js | 0 ...2769.find-the-maximum-achievable-number.js | 0 ...-celebrity.js => 277.find-the-celebrity.js | 0 ...number-of-jumps-to-reach-the-last-index.js | 0 ...non-decreasing-subarray-from-two-arrays.js | 0 ...o-make-all-array-elements-equal-to-zero.js | 0 ...2778.sum-of-squares-of-special-elements.js | 0 ...ty-of-an-array-after-applying-operation.js | 0 ...bad-version.js => 278.first-bad-version.js | 0 ... => 2780.minimum-index-of-a-valid-split.js | 0 ...1.length-of-the-longest-valid-substring.js | 0 ...rfect-squares.js => 279.perfect-squares.js | 0 ...nt-strStr().js => 28.implement-strStr().js | 60 +++---- 280-wiggle-sort.js => 280.wiggle-sort.js | 0 ...gzag-iterator.js => 281.zigzag-iterator.js | 0 ...tors.js => 282.expression-add-operators.js | 0 283-move-zeroes.js => 283.move-zeroes.js | 34 ++-- ...-bst.js => 285.inorder-successor-in-bst.js | 0 ...lls-and-gates.js => 286.walls-and-gates.js | 0 ...ber.js => 287.find-the-duplicate-number.js | 56 +++--- ...tion.js => 288.unique-word-abbreviation.js | 0 289-game-of-life.js => 289.game-of-life.js | 0 ...o-integers.js => 29.divide-two-integers.js | 58 +++--- 290-word-pattern.js => 290.word-pattern.js | 0 ...rd-pattern-ii.js => 291.word-pattern-ii.js | 0 292-nim-game.js => 292.nim-game.js | 0 293-flip-game.js => 293.flip-game.js | 0 294-flip-game-ii.js => 294.flip-game-ii.js | 0 ....js => 295.find-median-from-data-stream.js | 0 ...ting-point.js => 296.best-meeting-point.js | 0 ...7.serialize-and-deserialize-binary-tree.js | 0 ...inary-tree-longest-consecutive-sequence.js | 0 ...bulls-and-cows.js => 299.bulls-and-cows.js | 0 ...-substring-without-repeating-characters.js | 38 ++-- ...bstring-with-concatenation-of-all-words.js | 0 ...s => 300.longest-increasing-subsequence.js | 0 ...es.js => 301.remove-invalid-parentheses.js | 0 ...allest-rectangle-enclosing-black-pixels.js | 0 ....js => 304.range-sum-query-2d-immutable.js | 0 ...lands-ii.js => 305.number-of-islands-ii.js | 0 ...ditive-number.js => 306.additive-number.js | 0 ...table.js => 307.range-sum-query-mutable.js | 0 ...le.js => 308.range-sum-query-2d-mutable.js | 0 ...ime-to-buy-and-sell-stock-with-cooldown.js | 52 +++--- ...t-permutation.js => 31.next-permutation.js | 0 ...ht-trees.js => 310.minimum-height-trees.js | 0 ....js => 311.sparse-matrix-multiplication.js | 0 ...burst-balloons.js => 312.burst-balloons.js | 0 ...ugly-number.js => 313.super-ugly-number.js | 0 ...14.binary-tree-vertical-order-traversal.js | 0 ...315.count-of-smaller-numbers-after-self.js | 0 ...ters.js => 316.remove-duplicate-letters.js | 0 ...17.shortest-distance-from-all-buildings.js | 0 ... => 318.maximum-product-of-word-lengths.js | 52 +++--- 319-bulb-switcher.js => 319.bulb-switcher.js | 14 +- ...eses.js => 32.longest-valid-parentheses.js | 0 ...tion.js => 320.generalized-abbreviation.js | 0 ...-number.js => 321.create-maximum-number.js | 0 322-coin-change.js => 322.coin-change.js | 0 ...inerary.js => 322.reconstruct-itinerary.js | 0 ...ected-components-in-an-undirected-graph.js | 0 ...wiggle-sort-ii.js => 324.wiggle-sort-ii.js | 0 ... 325.maximum-size-subarray-sum-equals-k.js | 0 ...power-of-three.js => 326.power-of-three.js | 0 ...-range-sum.js => 327.count-of-range-sum.js | 0 ...ked-list.js => 328.odd-even-linked-list.js | 52 +++--- ...329.longest-increasing-path-in-a-matrix.js | 0 ...js => 33.search-in-rotated-sorted-array.js | 0 ...patching-array.js => 330.patching-array.js | 0 ...preorder-serialization-of-a-binary-tree.js | 0 ...t-subtree.js => 333.largest-bst-subtree.js | 0 ...s => 334.increasing-triplet-subsequence.js | 0 335-self-crossing.js => 335.self-crossing.js | 0 ...ndrome-pairs.js => 336.palindrome-pairs.js | 0 ...e-robber-iii.js => 337.house-robber-iii.js | 0 338-counting-bits.js => 338.counting-bits.js | 0 ...ht-sum.js => 339.nested-list-weight-sum.js | 0 ...ast-position-of-element-in-sorted-array.js | 0 ...ring-with-at-most-k-distinct-characters.js | 0 ....js => 341.flatten-nested-list-iterator.js | 0 342-power-of-four.js => 342.power-of-four.js | 0 343-integer-break.js => 343.integer-break.js | 30 ++-- ...reverse-string.js => 344.reverse-string.js | 0 ...ng.js => 345.reverse-vowels-of-a-string.js | 0 ... => 346.moving-average-from-data-stream.js | 0 ...ments.js => 347.top-k-frequent-elements.js | 66 +++---- ...ic-tac-toe.js => 348.design-tic-tac-toe.js | 0 ...ys.js => 349.intersection-of-two-arrays.js | 36 ++-- ...osition.js => 35.search-insert-position.js | 34 ++-- ...js => 350.intersection-of-two-arrays-ii.js | 0 ...terns.js => 351.android-unlock-patterns.js | 0 ...> 352.data-stream-as-disjoint-intervals.js | 0 ...-snake-game.js => 353.design-snake-game.js | 0 ...elopes.js => 354.russian-doll-envelopes.js | 0 ...design-twitter.js => 355.design-twitter.js | 0 ...ne-reflection.js => 356.line-reflection.js | 0 ...=> 357.count-numbers-with-unique-digits.js | 36 ++-- ...> 358.rearrange-string-k-distance-apart.js | 0 ...e-limiter.js => 359.logger-rate-limiter.js | 0 36-valid-sudoku.js => 36.valid-sudoku.js | 0 ...-array.js => 360.sort-transformed-array.js | 0 361-bomb-enemy.js => 361.bomb-enemy.js | 0 ...it-counter.js => 362.design-hit-counter.js | 0 ...3.max-sum-of-rectangle-no-larger-than-k.js | 0 ...-ii.js => 364.nested-list-weight-sum-ii.js | 0 ...problem.js => 365.water-and-jug-problem.js | 0 ...ee.js => 366.find-leaves-of-binary-tree.js | 0 ...t-square.js => 367.valid-perfect-square.js | 0 ...bset.js => 368.largest-divisible-subset.js | 0 ...ked-list.js => 369.plus-one-linked-list.js | 0 37-sudoku-solver.js => 37.sudoku-solver.js | 0 ...range-addition.js => 370.range-addition.js | 0 ...-integers.js => 371.sum-of-two-integers.js | 16 +- 372-super-pow.js => 372.super-pow.js | 0 ... => 373.find-k-pairs-with-smallest-sums.js | 0 ... => 375.guess-number-higher-or-lower-ii.js | 0 ...ubsequence.js => 376.wiggle-subsequence.js | 0 ...ion-sum-IV.js => 377.combination-sum-IV.js | 34 ++-- ...kth-smallest-element-in-a-sorted-matrix.js | 52 +++--- ...ectory.js => 379.design-phone-directory.js | 0 38-count-and-say.js => 38.count-and-say.js | 0 ...o1.js => 380.insert-delete-getrandom-o1.js | 0 ...-delete-getrandom-o1-duplicates-allowed.js | 0 ...-node.js => 382.linked-list-random-node.js | 74 ++++---- 383-ransom-note.js => 383.ransom-note.js | 32 ++-- ...fle-an-array.js => 384.shuffle-an-array.js | 78 ++++---- 385-mini-parser.js => 385.mini-parser.js | 0 ...mbers.js => 386.lexicographical-numbers.js | 0 ... 387.first-unique-character-in-a-string.js | 58 +++--- ...th.js => 388.longest-absolute-file-path.js | 0 ...ifference.js => 389.find-the-difference.js | 24 +-- ...ombination-sum.js => 39.combination-sum.js | 46 ++--- ...ination-game.js => 390.elimination-game.js | 0 ...t-rectangle.js => 391.perfect-rectangle.js | 0 ...is-subsequence.js => 392.is-subsequence.js | 0 ...8-validation.js => 393.utf-8-validation.js | 0 394-decode-string.js => 394.decode-string.js | 84 ++++----- ...ng-with-at-least-k-repeating-characters.js | 0 ...tate-function.js => 396.rotate-function.js | 0 ...placement.js => 397.integer-replacement.js | 0 ...-pick-index.js => 398.random-pick-index.js | 52 +++--- ...te-division.js => 399.evaluate-division.js | 80 ++++----- ...ays.js => 4.median-of-two-sorted-arrays.js | 70 ++++---- ...tion-sum-II.js => 40.combination-sum-II.js | 48 ++--- 400-nth-digit.js => 400.nth-digit.js | 0 401-binary-watch.js => 401.binary-watch.js | 0 ...move-k-digits.js => 402.remove-k-digits.js | 0 403-frog-jump.js => 403.frog-jump.js | 0 ...eft-leaves.js => 404.sum-of-left-leaves.js | 0 ... => 405.convert-a-number-to-hexadecimal.js | 0 ...s => 406.queue-reconstruction-by-height.js | 0 ...ter-ii.js => 407.trapping-rain-water-ii.js | 0 ...ation.js => 408.valid-word-abbreviation.js | 0 ...palindrome.js => 409.longest-palindrome.js | 42 ++--- ...ositive.js => 41.first-missing-positive.js | 0 ...t-sum.js => 410.split-array-largest-sum.js | 0 ...=> 411.minimum-unique-word-abbreviation.js | 0 412-fizz-buzz.js => 412.fizz-buzz.js | 56 +++--- ...etic-slices.js => 413.arithmetic-slices.js | 48 ++--- ...m-number.js => 414.third-maximum-number.js | 0 415-add-strings.js => 415.add-strings.js | 40 ++--- ...um.js => 416.partition-equal-subset-sum.js | 0 ...w.js => 417.pacific-atlantic-water-flow.js | 0 ...tting.js => 418.sentence-screen-fitting.js | 0 ...rain-water.js => 42.trapping-rain-water.js | 0 ...ecker.js => 420.strong-password-checker.js | 0 ....maximum-xor-of-two-numbers-in-an-array.js | 0 ...word-square.js => 422.valid-word-square.js | 0 ...econstruct-original-digits-from-english.js | 0 ...longest-repeating-character-replacement.js | 54 +++--- 425-word-squares.js => 425.word-squares.js | 0 ...earch-tree-to-sorted-doubly-linked-list.js | 0 ...quad-tree.js => 427.construct-quad-tree.js | 0 ...28.serialize-and-deserialize-n-ary-tree.js | 0 ...=> 429.n-ary-tree-level-order-traversal.js | 0 ...tiply-strings.js => 43.multiply-strings.js | 0 ...flatten-a-multilevel-doubly-linked-list.js | 0 ...=> 431.encode-n-ary-tree-to-binary-tree.js | 0 ...cture.js => 432.all-oone-data-structure.js | 0 ...tion.js => 433.minimum-genetic-mutation.js | 0 ...s => 434.number-of-segments-in-a-string.js | 0 ...als.js => 435.non-overlapping-intervals.js | 0 ...-interval.js => 436.find-right-interval.js | 0 437-path-sum-iii.js => 437.path-sum-iii.js | 0 ...js => 438.find-all-anagrams-in-a-string.js | 0 ...ser.js => 439.ternary-expression-parser.js | 0 ...ard-matching.js => 44.wildcard-matching.js | 0 ....k-th-smallest-in-lexicographical-order.js | 0 ...ranging-coins.js => 441.arranging-coins.js | 0 ... => 442.find-all-duplicates-in-an-array.js | 44 ++--- ...ompression.js => 443.string-compression.js | 0 ...ction.js => 444.sequence-reconstruction.js | 0 ...numbers-II.js => 445.add-two-numbers-II.js | 116 ++++++------ ...=> 446.arithmetic-slices-ii-subsequence.js | 0 ...omerangs.js => 447.number-of-boomerangs.js | 0 ...ind-all-numbers-disappeared-in-an-array.js | 36 ++-- ...js => 449.serialize-and-deserialize-BST.js | 118 ++++++------- 45-jump-game-ii.js => 45.jump-game-ii.js | 0 ...in-a-bst.js => 450.delete-node-in-a-bst.js | 0 ....js => 451.sort-characters-by-frequency.js | 38 ++-- ...imum-number-of-arrows-to-burst-balloons.js | 0 ...3.minimum-moves-to-equal-array-elements.js | 26 +-- 454-4sum-ii.js => 454.4sum-ii.js | 0 ...assign-cookies.js => 455.assign-cookies.js | 30 ++-- 456-132-pattern.js => 456.132-pattern.js | 0 ...rray-loop.js => 457.circular-array-loop.js | 0 458-poor-pigs.js => 458.poor-pigs.js | 0 ...rn.js => 459.repeated-substring-pattern.js | 0 46-permutations.js => 46.permutations.js | 48 ++--- 460-lfu-cache.js => 460.lfu-cache.js | 0 ...ing-distance.js => 461.hamming-distance.js | 0 ...inimum-moves-to-equal-array-elements-II.js | 32 ++-- ...nd-perimeter.js => 463.island-perimeter.js | 82 ++++----- 464-can-i-win.js => 464.can-i-win.js | 0 ...ing.js => 465.optimal-account-balancing.js | 0 ...titions.js => 466.count-the-repetitions.js | 0 ....unique-substrings-in-wraparound-string.js | 0 ...p-address.js => 468.validate-ip-address.js | 0 ...convex-polygon.js => 469.convex-polygon.js | 0 ...ermutations-ii.js => 47.permutations-ii.js | 0 ....js => 470.implement-rand10-using-rand7.js | 0 ... 471.encode-string-with-shortest-length.js | 0 ...ated-words.js => 472.concatenated-words.js | 0 ...-square.js => 473.matchsticks-to-square.js | 0 ...es-and-zeroes.js => 474.ones-and-zeroes.js | 0 475-heaters.js => 475.heaters.js | 0 ...-complement.js => 476.number-complement.js | 0 ...stance.js => 477.total-hamming-distance.js | 28 +-- ...> 478.generate-random-point-in-a-circle.js | 0 ...ct.js => 479.largest-palindrome-product.js | 0 48-rotate-image.js => 48.rotate-image.js | 0 ...-median.js => 480.sliding-window-median.js | 0 ...magical-string.js => 481.magical-string.js | 0 ...atting.js => 482.license-key-formatting.js | 0 ...-good-base.js => 483.smallest-good-base.js | 0 ...-permutation.js => 484.find-permutation.js | 0 ...ive-ones.js => 485.max-consecutive-ones.js | 0 ...the-winner.js => 486.predict-the-winner.js | 46 ++--- ...es-ii.js => 487.max-consecutive-ones-ii.js | 0 488-zuma-game.js => 488.zuma-game.js | 0 ...om-cleaner.js => 489.robot-room-cleaner.js | 0 49-group-anagrams.js => 49.group-anagrams.js | 54 +++--- 490-the-maze.js => 490.the-maze.js | 0 ...ences.js => 491.increasing-subsequences.js | 0 ...angle.js => 492.construct-the-rectangle.js | 0 493-reverse-pairs.js => 493.reverse-pairs.js | 0 494-target-sum.js => 494.target-sum.js | 52 +++--- ...emo-attacking.js => 495.teemo-attacking.js | 0 ...ment-I.js => 496.next-greater-element-I.js | 0 ...dom-point-in-non-overlapping-rectangles.js | 0 ...al-traverse.js => 498.diagonal-traverse.js | 0 499-the-maze-iii.js => 499.the-maze-iii.js | 0 ...g.js => 5.longest-palindromic-substring.js | 42 ++--- 50-powx-n.js => 50.powx-n.js | 0 500-keyboard-row.js => 500.keyboard-row.js | 22 +-- ... => 501.find-mode-in-binary-search-tree.js | 0 502-ipo.js => 502.ipo.js | 0 ...nt-II.js => 503.next-greater-element-II.js | 44 ++--- 504-base-7.js => 504.base-7.js | 0 505-the-maze-ii.js => 505.the-maze-ii.js | 0 ...relative-ranks.js => 506.relative-ranks.js | 0 ...perfect-number.js => 507.perfect-number.js | 0 ...sum.js => 508.most-frequent-subtree-sum.js | 96 +++++----- ...ram.js => 5083.occurrences-after-bigram.js | 0 ...nsufficient-nodes-in-root-to-leaf-paths.js | 0 ...nacci-number.js => 509.fibonacci-number.js | 0 51-n-queens.js => 51.n-queens.js | 0 ...i.js => 510.inorder-successor-in-bst-ii.js | 0 ...e.js => 513.find-bottom-left-tree-value.js | 0 514-freedom-trail.js => 514.freedom-trail.js | 0 ...515.find-largest-value-in-each-tree-row.js | 0 ... => 516.longest-palindromic-subsequence.js | 0 ...chines.js => 517.super-washing-machines.js | 0 518-coin-change-2.js => 518.coin-change-2.js | 0 ...lip-matrix.js => 519.random-flip-matrix.js | 0 52-n-queens-II.js => 52.n-queens-II.js | 74 ++++---- ...detect-capital.js => 520.detect-capital.js | 110 ++++++------ ...s => 521.longest-uncommon-subsequence-i.js | 0 ... => 522.longest-uncommon-subsequence-ii.js | 0 ...y-sum.js => 523.continuous-subarray-sum.js | 0 ...est-word-in-dictionary-through-deleting.js | 0 ...iguous-array.js => 525.contiguous-array.js | 0 ...ngement.js => 526.beautiful-arrangement.js | 0 ...bbreviation.js => 527.word-abbreviation.js | 0 ...eight.js => 528.random-pick-with-weight.js | 0 529-minesweeper.js => 529.minesweeper.js | 0 ...imum-subarray.js => 53.maximum-subarray.js | 28 +-- ... 530.minimum-absolute-difference-in-BST.js | 62 +++---- ...lonely-pixel-i.js => 531.lonely-pixel-i.js | 0 ...rray.js => 532.k-diff-pairs-in-an-array.js | 0 ...nely-pixel-ii.js => 533.lonely-pixel-ii.js | 0 ...> 536.construct-binary-tree-from-string.js | 0 538-convert-BST-to-greater-tree.js | 19 -- ...e.js => 538.convert-BST-to-greater-tree.js | 0 ...rence.js => 539.minimum-time-difference.js | 64 +++---- 54-spiral-matrix.js => 54.spiral-matrix.js | 0 ...=> 540.single-element-in-a-sorted-array.js | 0 ...e-string-ii.js => 541.reverse-string-ii.js | 0 542-01-matrix.js => 542.01-matrix.js | 0 ...-tree.js => 543.diameter-of-binary-tree.js | 50 +++--- ...atches.js => 544.output-contest-matches.js | 0 ...-tree.js => 545.boundary-of-binary-tree.js | 0 546-remove-boxes.js => 546.remove-boxes.js | 0 ...friend-circles.js => 547.friend-circles.js | 0 ...um.js => 548.split-array-with-equal-sum.js | 0 ...ry-tree-longest-consecutive-sequence-ii.js | 0 55-jump-game.js => 55.jump-game.js | 0 ...i.js => 551.student-attendance-record-i.js | 0 ....js => 552.student-attendance-record-ii.js | 0 ...mal-division.js => 553.optimal-division.js | 0 554-brick-wall.js => 554.brick-wall.js | 50 +++--- ...gs.js => 555.split-concatenated-strings.js | 0 ...-iii.js => 556.next-greater-element-iii.js | 0 ...-binary-grids-represented-as-quad-trees.js | 0 ...e.js => 559.maximum-depth-of-n-ary-tree.js | 0 ...erge-intervals.js => 56.merge-intervals.js | 0 ...quals-k.js => 560.subarray-sum-equals-k.js | 0 ...ngest-line-of-consecutive-one-in-matrix.js | 0 ...ry-tree-tilt.js => 563.binary-tree-tilt.js | 0 ...e.js => 564.find-the-closest-palindrome.js | 0 565-array-nesting.js => 565.array-nesting.js | 42 ++--- ...the-matrix.js => 566.reshape-the-matrix.js | 58 +++--- ...-string.js => 567.permutation-in-string.js | 0 ...on-days.js => 568.maximum-vacation-days.js | 0 ...nsert-interval.js => 57.insert-interval.js | 0 ...-tree.js => 572.subtree-of-another-tree.js | 0 ...imulation.js => 573.squirrel-simulation.js | 0 ...5745.last-day-where-you-can-still-cross.js | 0 ...te-candies.js => 575.distribute-candies.js | 26 +-- ...y-paths.js => 576.out-of-boundary-paths.js | 0 ...-last-word.js => 58.length-of-last-word.js | 22 +-- ...1.shortest-unsorted-continuous-subarray.js | 0 582-kill-process.js => 582.kill-process.js | 0 ...=> 583.delete-operation-for-two-strings.js | 0 ...ments-not-equal-to-average-of-neighbors.js | 0 ...rings-that-appear-as-substrings-in-word.js | 0 ...-non-zero-product-of-the-array-elements.js | 0 ...ect-the-fence.js => 587.erect-the-fence.js | 0 ....js => 588.design-in-memory-file-system.js | 0 ...js => 589.n-ary-tree-preorder-traversal.js | 0 ...ral-matrix-II.js => 59.spiral-matrix-II.js | 78 ++++---- ...s => 590.n-ary-tree-postorder-traversal.js | 0 591-tag-validator.js => 591.tag-validator.js | 0 ...> 592.fraction-addition-and-subtraction.js | 0 593-valid-square.js => 593.valid-square.js | 0 ...s => 594.longest-harmonious-subsequence.js | 0 ...d-first-palindromic-string-in-the-array.js | 0 ...ng.js => 5957.adding-spaces-to-a-string.js | 0 ...er-of-smooth-descent-periods-of-a-stock.js | 0 ...ery-row-and-column-contains-all-numbers.js | 0 ...nimum-swaps-to-group-all-1s-together-ii.js | 0 ...nt-words-obtained-after-adding-a-letter.js | 0 ...addition-ii.js => 598.range-addition-ii.js | 0 ...s => 599.minimum-index-sum-of-two-lists.js | 72 ++++---- ...ag-conversion.js => 6.zigzag-conversion.js | 56 +++--- ...-sequence.js => 60.permutation-sequence.js | 0 ...ative-integers-without-consecutive-ones.js | 0 ...> 604.design-compressed-string-iterator.js | 0 ...ace-flowers.js => 605.can-place-flowers.js | 0 ...> 606.construct-string-from-binary-tree.js | 0 ...js => 609.find-duplicate-file-in-system.js | 0 61-rotate-list.js => 61.rotate-list.js | 0 ...-number.js => 611.valid-triangle-number.js | 48 ++--- ...string.js => 616.add-bold-tag-in-string.js | 0 62-unique-paths.js => 62.unique-paths.js | 0 ...task-scheduler.js => 621.task-scheduler.js | 62 +++---- ...r-queue.js => 622.design-circular-queue.js | 0 ...w-to-tree.js => 623.add-one-row-to-tree.js | 0 ...ys.js => 624.maximum-distance-in-arrays.js | 0 ...ization.js => 625.minimum-factorization.js | 0 ...=> 628.maximum-product-of-three-numbers.js | 0 ...s-array.js => 629.k-inverse-pairs-array.js | 0 ...nique-paths-ii.js => 63.unique-paths-ii.js | 0 ...edule-iii.js => 630.course-schedule-iii.js | 0 ...mula.js => 631.design-excel-sum-formula.js | 0 ...smallest-range.js => 632.smallest-range.js | 0 ...numbers.js => 633.sum-of-square-numbers.js | 0 ...=> 634.find-the-derangement-of-an-array.js | 0 ...tem.js => 635.design-log-storage-system.js | 0 ...s.js => 636.exclusive-time-of-functions.js | 0 ...=> 637.average-of-levels-in-binary-tree.js | 0 ...opping-offers.js => 638.shopping-offers.js | 0 ...decode-ways-ii.js => 639.decode-ways-ii.js | 0 ...imum-path-sum.js => 64.minimum-path-sum.js | 40 ++--- ...e-equation.js => 640.solve-the-equation.js | 0 ...r-deque.js => 641.design-circular-deque.js | 0 ...> 642.design-search-autocomplete-system.js | 0 ...-i.js => 643.maximum-average-subarray-i.js | 0 ...i.js => 644.maximum-average-subarray-ii.js | 0 645-set-mismatch.js => 645.set-mismatch.js | 0 ....js => 646.maximum-length-of-pair-chain.js | 32 ++-- ...trings.js => 647.palindromic-substrings.js | 0 648-replace-words.js => 648.replace-words.js | 78 ++++---- 649-dota2-senate.js => 649.dota2-senate.js | 0 65-valid-number.js => 65.valid-number.js | 70 ++++---- ...keys-keyboard.js => 650.2-keys-keyboard.js | 28 +-- ...keys-keyboard.js => 651.4-keys-keyboard.js | 0 ...trees.js => 652.find-duplicate-subtrees.js | 0 ...bst.js => 653.two-sum-iv-input-is-a-bst.js | 0 ...binary-tree.js => 655.print-binary-tree.js | 0 656-coin-path.js => 656.coin-path.js | 0 ...ments.js => 658.find-k-closest-elements.js | 0 ...lit-array-into-consecutive-subsequences.js | 0 66-plus-one.js => 66.plus-one.js | 0 660-remove-9.js => 660.remove-9.js | 0 ...image-smoother.js => 661.image-smoother.js | 0 ....js => 662.maximum-width-of-binary-tree.js | 0 ...artition.js => 663.equal-tree-partition.js | 0 ...range-printer.js => 664.strange-printer.js | 0 ...ng-array.js => 665.non-decreasing-array.js | 0 666-path-sum-iv.js => 666.path-sum-iv.js | 0 ...t-ii.js => 667.beautiful-arrangement-ii.js | 0 ...smallest-number-in-multiplication-table.js | 0 ...ree.js => 669.trim-a-binary-search-tree.js | 70 ++++---- 67-add-binary.js => 67.add-binary.js | 0 670-maximum-swap.js => 670.maximum-swap.js | 0 ...71.second-minimum-node-in-a-binary-tree.js | 0 ...-switcher-ii.js => 672.bulb-switcher-ii.js | 0 ...umber-of-longest-increasing-subsequence.js | 0 ...ngest-continuous-increasing-subsequence.js | 36 ++-- ....js => 675.cut-off-trees-for-golf-event.js | 0 ...ry.js => 676.implement-magic-dictionary.js | 106 +++++------ 677-map-sum-pairs.js => 677.map-sum-pairs.js | 72 ++++---- ...ring.js => 678.valid-parenthesis-string.js | 0 679-24-game.js => 679.24-game.js | 0 ...stification.js => 68.text-justification.js | 70 ++++---- ...ndrome-ii.js => 680.valid-palindrome-ii.js | 0 ...losest-time.js => 681.next-closest-time.js | 0 682-baseball-game.js => 682.baseball-game.js | 104 +++++------ 683-k-empty-slots.js => 683.k-empty-slots.js | 0 ...nnection.js => 684.redundant-connection.js | 0 ...on-ii.js => 685.redundant-connection-ii.js | 0 ...g-match.js => 686.repeated-string-match.js | 0 ...ue-path.js => 687.longest-univalue-path.js | 0 ...=> 688.knight-probability-in-chessboard.js | 0 ...imum-sum-of-3-non-overlapping-subarrays.js | 0 69-sqrt(x).js => 69.sqrt(x).js | 34 ++-- ...mportance.js => 690.employee-importance.js | 0 ...l-word.js => 691.stickers-to-spell-word.js | 0 ...nt-words.js => 692.top-k-frequent-words.js | 0 ...693.binary-number-with-alternating-bits.js | 0 ...ds.js => 694.number-of-distinct-islands.js | 0 ...-of-island.js => 695.max-area-of-island.js | 0 ...f-an-array.js => 697.degree-of-an-array.js | 52 +++--- ...=> 698.partition-to-k-equal-sum-subsets.js | 0 ...lling-squares.js => 699.falling-squares.js | 0 7-reverse-integer.js => 7.reverse-integer.js | 38 ++-- ...limbing-stairs.js => 70.climbing-stairs.js | 50 +++--- ...s => 700.search-in-a-binary-search-tree.js | 38 ++-- ...=> 701.insert-into-a-binary-search-tree.js | 0 ...earch-in-a-sorted-array-of-unknown-size.js | 0 ... => 703.kth-largest-element-in-a-stream.js | 0 704-binary-search.js => 704.binary-search.js | 0 ...design-hashset.js => 705.design-hashset.js | 0 ...design-hashmap.js => 706.design-hashmap.js | 0 ...inked-list.js => 707.design-linked-list.js | 0 ...sert-into-a-sorted-circular-linked-list.js | 0 71-simplify-path.js => 71.simplify-path.js | 0 ...st.js => 710.random-pick-with-blacklist.js | 0 ...js => 711.number-of-distinct-islands-ii.js | 0 ...inimum-ASCII-delete-sum-for-two-strings.js | 72 ++++---- ....js => 713.subarray-product-less-than-k.js | 0 ...buy-and-sell-stock-with-transaction-fee.js | 32 ++-- 715-range-module.js => 715.range-module.js | 0 716-max-stack.js => 716.max-stack.js | 0 ...rs.js => 717.1-bit-and-2-bit-characters.js | 24 +-- ...718.maximum-length-of-repeated-subarray.js | 42 ++--- ...=> 719.find-k-th-smallest-pair-distance.js | 0 72-edit-distance.js => 72.edit-distance.js | 0 ...ry.js => 720.longest-word-in-dictionary.js | 0 ...accounts-merge.js => 721.accounts-merge.js | 0 ...move-comments.js => 722.remove-comments.js | 0 723-candy-crush.js => 723.candy-crush.js | 0 ...-pivot-index.js => 724.find-pivot-index.js | 0 ...ts.js => 725.split-linked-list-in-parts.js | 74 ++++---- ...mber-of-atoms.js => 726.number-of-atoms.js | 0 ...ce.js => 727.minimum-window-subsequence.js | 0 729-my-calendar-i.js => 729.my-calendar-i.js | 0 ...atrix-zeroes.js => 73.set-matrix-zeroes.js | 0 ...ount-different-palindromic-subsequences.js | 0 ...my-calendar-ii.js => 731.my-calendar-ii.js | 0 ...-calendar-iii.js => 732.my-calendar-iii.js | 0 733-flood-fill.js => 733.flood-fill.js | 0 ...imilarity.js => 734.sentence-similarity.js | 0 ...-collision.js => 735.asteroid-collision.js | 0 ...ression.js => 736.parse-lisp-expression.js | 0 ...ity-ii.js => 737.sentence-similarity-ii.js | 0 ...ts.js => 738.monotone-increasing-digits.js | 0 ...mperatures.js => 739.daily-temperatures.js | 0 ...a-2d-matrix.js => 74.search-a-2d-matrix.js | 0 ...lete-and-earn.js => 740.delete-and-earn.js | 0 741-cherry-pickup.js => 741.cherry-pickup.js | 0 ...js => 742.closest-leaf-in-a-binary-tree.js | 0 ...delay-time.js => 743.network-delay-time.js | 0 ...ind-smallest-letter-greater-than-target.js | 0 ...arch.js => 745.prefix-and-suffix-search.js | 0 ...airs.js => 746.min-cost-climbing-stairs.js | 28 +-- ...largest-number-at-least-twice-of-others.js | 0 ...word.js => 748.shortest-completing-word.js | 0 749-contain-virus.js => 749.contain-virus.js | 0 75-sort-colors.js => 75.sort-colors.js | 0 ...s.js => 750.number-of-corner-rectangles.js | 0 751-ip-to-cidr.js => 751.ip-to-cidr.js | 0 752-open-the-lock.js => 752.open-the-lock.js | 112 ++++++------ ...reach-a-number.js => 754.reach-a-number.js | 0 755-pour-water.js => 755.pour-water.js | 0 ...rix.js => 756.pyramid-transition-matrix.js | 0 ... 757.set-intersection-size-at-least-two.js | 0 ...n-string.js => 758.bold-words-in-string.js | 0 ...-free-time.js => 759.employee-free-time.js | 0 ...tring.js => 76.minimum-window-substring.js | 0 ...appings.js => 760.find-anagram-mappings.js | 0 ...-string.js => 761.special-binary-string.js | 0 ...er-of-set-bits-in-binary-representation.js | 0 ...ition-labels.js => 763.partition-labels.js | 0 ...t-plus-sign.js => 764.largest-plus-sign.js | 0 ...g-hands.js => 765.couples-holding-hands.js | 0 ...eplitz-matrix.js => 766.toeplitz-matrix.js | 36 ++-- ...nize-string.js => 767.reorganize-string.js | 0 ....js => 768.max-chunks-to-make-sorted-II.js | 34 ++-- ...ted.js => 769.max-chunks-to-make-sorted.js | 26 +-- 77-combinations.js => 77.combinations.js | 44 ++--- ...ulator-iv.js => 770.basic-calculator-iv.js | 0 ...-and-stones.js => 771.jewels-and-stones.js | 0 ...ator-iii.js => 772.basic-calculator-iii.js | 0 ...sliding-puzzle.js => 773.sliding-puzzle.js | 102 +++++------ ...74.minimize-max-distance-to-gas-station.js | 0 ...s.js => 775.global-and-local-inversions.js | 0 776-split-bst.js => 776.split-bst.js | 0 ...ng.js => 777.swap-adjacent-in-lr-string.js | 0 ...ng-water.js => 778.swim-in-rising-water.js | 0 ...rammar.js => 779.k-th-symbol-in-grammar.js | 0 78-subsets.js => 78.subsets.js | 48 ++--- ...aching-points.js => 780.reaching-points.js | 0 ...board.js => 782.transform-to-chessboard.js | 0 ... 783.minimum-distance-between-BST-nodes.js | 54 +++--- ...ation.js => 784.letter-case-permutation.js | 0 ...-bipartite.js => 785.is-graph-bipartite.js | 0 ....js => 786.k-th-smallest-prime-fraction.js | 0 ... => 787.cheapest-flights-within-k-stops.js | 0 ...rotated-digits.js => 788.rotated-digits.js | 0 ...-the-ghosts.js => 789.escape-the-ghosts.js | 0 79-word-search.js => 79.word-search.js | 0 ...ort-string.js => 791.custom-sort-string.js | 92 +++++----- ... => 792.number-of-matching-subsequences.js | 0 ...image-size-of-factorial-zeroes-function.js | 0 ...state.js => 794.valid-tic-tac-toe-state.js | 0 ...umber-of-subarrays-with-bounded-maximum.js | 0 796-rotate-string.js => 796.rotate-string.js | 18 +- ... => 797.all-paths-from-source-to-target.js | 0 ...98.smallest-rotation-with-highest-score.js | 0 ...ampagne-tower.js => 799.champagne-tower.js | 0 ...(atoi).js => 8.string-to-integer-(atoi).js | 74 ++++---- ....remove-duplicates-from-sorted-array-ii.js | 0 ...r-rgb-color.js => 800.similar-rgb-color.js | 0 ...imum-swaps-to-make-sequences-increasing.js | 0 ...tes.js => 802.find-eventual-safe-states.js | 0 ...n-hit.js => 803.bricks-falling-when-hit.js | 0 ...js => 805.split-array-with-same-average.js | 0 ...=> 81.search-in-rotated-sorted-array-ii.js | 0 ...-xor-game.js => 810.chalkboard-xor-game.js | 0 ...le-area.js => 812.largest-triangle-area.js | 0 ...rages.js => 813.largest-sum-of-averages.js | 0 815-bus-routes.js => 815.bus-routes.js | 0 818-race-car.js => 818.race-car.js | 0 ...-common-word.js => 819.most-common-word.js | 0 ...2.remove-duplicates-from-sorted-list-ii.js | 0 ...=> 821.shortest-distance-to-a-character.js | 82 ++++----- ...ors.js => 823.binary-trees-with-factors.js | 0 824-goat-latin.js => 824.goat-latin.js | 0 ...rk.js => 826.most-profit-assigning-work.js | 0 ...-island.js => 827.making-a-large-island.js | 0 ...r-string.js => 828.unique-letter-string.js | 0 ...s-sum.js => 829.consecutive-numbers-sum.js | 0 ...> 83.remove-duplicates-from-sorted-list.js | 46 ++--- ...ups.js => 830.positions-of-large-groups.js | 74 ++++---- ...tree.js => 834.sum-of-distances-in-tree.js | 0 835-image-overlap.js => 835.image-overlap.js | 0 ...gle-overlap.js => 836.rectangle-overlap.js | 0 837-new-21-game.js => 837.new-21-game.js | 0 ...-groups.js => 839.similar-string-groups.js | 0 ...js => 84.largest-rectangle-in-histogram.js | 0 ...keys-and-rooms.js => 841.keys-and-rooms.js | 52 +++--- ...guess-the-word.js => 843.guess-the-word.js | 0 ...pare.js => 844.backspace-string-compare.js | 0 ...ray.js => 845.longest-mountain-in-array.js | 0 ...=> 847.shortest-path-visiting-all-nodes.js | 0 ...ting-letters.js => 848.shifting-letters.js | 0 ...849.maximize-distance-to-closest-person.js | 0 ...al-rectangle.js => 85.maximal-rectangle.js | 0 ...gle-area-ii.js => 850.rectangle-area-ii.js | 0 ...lar-strings.js => 854.k-similar-strings.js | 0 855-exam-room.js => 855.exam-room.js | 0 ...entheses.js => 856.score-of-parentheses.js | 0 ...s => 857.minimum-cost-to-hire-k-workers.js | 0 ...-reflection.js => 858.mirror-reflection.js | 38 ++-- 859-buddy-strings.js => 859.buddy-strings.js | 0 86-partition-list.js => 86.partition-list.js | 0 ...monade-change.js => 860.lemonade-change.js | 56 +++--- ...x.js => 861.score-after-flipping-matrix.js | 0 ...2.shortest-subarray-with-sum-at-least-k.js | 0 ...863.all-nodes-distance-k-in-binary-tree.js | 0 ...js => 864.shortest-path-to-get-all-keys.js | 0 ...lest-subtree-with-all-the-deepest-nodes.js | 58 +++--- ...e-palindrome.js => 866.prime-palindrome.js | 0 868-binary-gap.js => 868.binary-gap.js | 44 ++--- ...wer-of-2.js => 869.reordered-power-of-2.js | 72 ++++---- ...cramble-string.js => 87.scramble-string.js | 0 ...age-shuffle.js => 870.advantage-shuffle.js | 0 ...> 871.minimum-number-of-refueling-stops.js | 0 ...length-of-longest-fibonacci-subsequence.js | 54 +++--- ...tion.js => 874.walking-robot-simulation.js | 0 ...g-bananas.js => 875.koko-eating-bananas.js | 0 ...ist.js => 876.middle-of-the-linked-list.js | 0 ...cal-number.js => 878.nth-magical-number.js | 0 ...le-schemes.js => 879.profitable-schemes.js | 0 ...orted-array.js => 88.merge-sorted-array.js | 0 ...index.js => 880.decoded-string-at-index.js | 0 ...e-people.js => 881.boats-to-save-people.js | 0 ...882.reachable-nodes-in-subdivided-graph.js | 0 ....js => 883.projection-area-of-3d-shapes.js | 0 ...> 884.uncommon-words-from-two-sentences.js | 0 ...artition.js => 886.possible-bipartition.js | 0 ...super-egg-drop.js => 887.super-egg-drop.js | 0 ...ir-candy-swap.js => 888.fair-candy-swap.js | 0 ...e-from-preorder-and-postorder-traversal.js | 0 89-gray-code.js => 89.gray-code.js | 0 ...tern.js => 890.find-and-replace-pattern.js | 0 ...ths.js => 891.sum-of-subsequence-widths.js | 0 ...pes.js => 892.surface-area-of-3d-shapes.js | 0 ...93.groups-of-special-equivalent-strings.js | 0 ...s => 894.all-possible-full-binary-trees.js | 0 ...stack.js => 895.maximum-frequency-stack.js | 0 ...notonic-array.js => 896.monotonic-array.js | 0 ....js => 897.increasing-order-search-tree.js | 0 ...rays.js => 898.bitwise-ors-of-subarrays.js | 0 899-orderly-queue.js => 899.orderly-queue.js | 0 ...ndrome-number.js => 9.palindrome-number.js | 42 ++--- 90-subsets-II.js => 90.subsets-II.js | 42 ++--- ...-stock-span.js => 901.online-stock-span.js | 0 ...> 902.numbers-at-most-n-given-digit-set.js | 0 ... 903.valid-permutations-for-di-sequence.js | 0 ...to-baskets.js => 904.fruit-into-baskets.js | 0 ...y-parity.js => 905.sort-array-by-parity.js | 0 ...palindromes.js => 906.super-palindromes.js | 0 ...mums.js => 907.sum-of-subarray-minimums.js | 0 ...lest-range-i.js => 908.smallest-range-i.js | 0 91-decode-ways.js => 91.decode-ways.js | 0 ...st-range-ii.js => 910.smallest-range-ii.js | 0 912-sort-an-array.js => 912.sort-an-array.js | 0 913-cat-and-mouse.js => 913.cat-and-mouse.js | 0 ...s => 914.x-of-a-kind-in-a-deck-of-cards.js | 0 ...partition-array-into-disjoint-intervals.js | 0 916-word-subsets.js => 916.word-subsets.js | 64 +++---- ...-letters.js => 917.reverse-only-letters.js | 0 ...js => 918.maximum-sum-circular-subarray.js | 0 ...js => 919.complete-binary-tree-inserter.js | 0 ...list-ii.js => 92.reverse-linked-list-ii.js | 0 ...sts.js => 920.number-of-music-playlists.js | 0 ...1.minimum-add-to-make-parentheses-valid.js | 0 ...ty-ii.js => 922.sort-array-by-parity-ii.js | 0 ...licity.js => 923.3sum-with-multiplicity.js | 0 ...pread.js => 924.minimize-malware-spread.js | 0 ...ressed-name.js => 925.long-pressed-name.js | 0 ... 926.flip-string-to-monotone-increasing.js | 0 ...equal-parts.js => 927.three-equal-parts.js | 0 ...ii.js => 928.minimize-malware-spread-ii.js | 0 ...resses.js => 929.unique-email-addresses.js | 0 ...addresses.js => 93.restore-ip-addresses.js | 0 ...sum.js => 930.binary-subarrays-with-sum.js | 0 ...-sum.js => 931.minimum-falling-path-sum.js | 0 ...autiful-array.js => 932.beautiful-array.js | 0 ...-calls.js => 933.number-of-recent-calls.js | 0 ...ortest-bridge.js => 934.shortest-bridge.js | 0 ...equence.js => 936.stamping-the-sequence.js | 0 ...les.js => 937.reorder-data-in-log-files.js | 0 ...e-sum-of-bst.js => 938.range-sum-of-bst.js | 0 ...tangle.js => 939.minimum-area-rectangle.js | 0 ....js => 94.binary-tree-inorder-traversal.js | 54 +++--- ...s-ii.js => 940.distinct-subsequences-ii.js | 0 ...in-array.js => 941.valid-mountain-array.js | 0 ...-string-match.js => 942.di-string-match.js | 0 ...js => 943.find-the-shortest-superstring.js | 0 ...js => 944.delete-columns-to-make-sorted.js | 0 ....minimum-increment-to-make-array-unique.js | 0 ...nces.js => 946.validate-stack-sequences.js | 0 ...-stones-removed-with-same-row-or-column.js | 0 948-bag-of-tokens.js => 948.bag-of-tokens.js | 0 ...js => 949.largest-time-for-given-digits.js | 0 ....js => 95.unique-binary-search-trees-ii.js | 0 ...=> 950.reveal-cards-in-increasing-order.js | 0 ....js => 951.flip-equivalent-binary-trees.js | 0 ...largest-component-size-by-common-factor.js | 0 ...js => 953.verifying-an-alien-dictionary.js | 0 ...-pairs.js => 954.array-of-doubled-pairs.js | 0 ...=> 955.delete-columns-to-make-sorted-ii.js | 0 ...t-billboard.js => 956.tallest-billboard.js | 0 ...ays.js => 957.prison-cells-after-n-days.js | 0 ...958.check-completeness-of-a-binary-tree.js | 0 ...lashes.js => 959.regions-cut-by-slashes.js | 0 ...ees.js => 96.unique-binary-search-trees.js | 0 ...> 960.delete-columns-to-make-sorted-iii.js | 0 ...961.n-repeated-element-in-size-2n-array.js | 0 ...> 964.least-operators-to-express-number.js | 0 ...ry-tree.js => 965.univalued-binary-tree.js | 0 ...mbers-with-same-consecutive-differences.js | 0 ...e-cameras.js => 968.binary-tree-cameras.js | 0 ...ncake-sorting.js => 969.pancake-sorting.js | 0 ...ing-string.js => 97.interleaving-string.js | 0 ...binary-tree-to-match-preorder-traversal.js | 0 ...umbers.js => 972.equal-rational-numbers.js | 0 ...in.js => 973.k-closest-points-to-origin.js | 0 ....js => 974.subarray-sums-divisible-by-k.js | 0 975-odd-even-jump.js => 975.odd-even-jump.js | 0 ...le.js => 976.largest-perimeter-triangle.js | 0 ...ray.js => 977.squares-of-a-sorted-array.js | 0 ...ay.js => 978.longest-turbulent-subarray.js | 0 ... => 979.distribute-coins-in-binary-tree.js | 0 ...ee.js => 98.validate-binary-search-tree.js | 0 ...ue-paths-iii.js => 980.unique-paths-iii.js | 0 ...re.js => 981.time-based-key-value-store.js | 0 ....triples-with-bitwise-and-equal-to-zero.js | 0 ...kets.js => 983.minimum-cost-for-tickets.js | 0 ...bbb.js => 984.string-without-aaa-or-bbb.js | 0 ...> 985.sum-of-even-numbers-after-queries.js | 0 ...s.js => 986.interval-list-intersections.js | 0 ...rtical-order-traversal-of-a-binary-tree.js | 0 ... 988.smallest-string-starting-from-leaf.js | 0 ....js => 989.add-to-array-form-of-integer.js | 0 ...ree.js => 99.recover-binary-search-tree.js | 0 ...90.satisfiability-of-equality-equations.js | 0 ...992.subarrays-with-k-different-integers.js | 0 ...y-tree.js => 993.cousins.in-binary-tree.js | 0 ...tting-oranges.js => 994.rotting-oranges.js | 0 ...nimum-number-of-k-consecutive-bit-flips.js | 0 ...ys.js => 996.number-of-squareful-arrays.js | 0 ...own-judge.js => 997.find-the-town-judge.js | 0 ...k.js => 999.available-captures-for-rook.js | 0 TBD-leftmost-column-with-at-least-a-one.js | 34 ---- clean-dataset.py | 4 +- tmp.js | 141 --------------- 2025 files changed, 4082 insertions(+), 4274 deletions(-) rename 1-two-sum.js => 1.two-sum.js (95%) mode change 100755 => 100644 rename 10-regular-expression-matching.js => 10.regular-expression-matching.js (96%) mode change 100755 => 100644 rename 100-same-tree.js => 100.same-tree.js (96%) mode change 100755 => 100644 rename 1000-minimum-cost-to-merge-stones.js => 1000.minimum-cost-to-merge-stones.js (100%) rename 1001-grid-illumination.js => 1001.grid-illumination.js (100%) rename 1002-find-common-characters.js => 1002.find-common-characters.js (100%) rename 1004-max-consecutive-ones-iii.js => 1004.max-consecutive-ones-iii.js (100%) rename 1005-maximize-sum-of-array-after-k-negations.js => 1005.maximize-sum-of-array-after-k-negations.js (100%) rename 1006-clumsy-factorial.js => 1006.clumsy-factorial.js (100%) rename 1008-construct-binary-search-tree-from-preorder-traversal.js => 1008.construct-binary-search-tree-from-preorder-traversal.js (100%) rename 1009-complement-of-base-10-integer.js => 1009.complement-of-base-10-integer.js (100%) rename 101-symmetric-tree.js => 101.symmetric-tree.js (100%) rename 1011-capacity-to-ship-packages-within-d-days.js => 1011.capacity-to-ship-packages-within-d-days.js (100%) rename 1012-complement-of-base-10-integer.js => 1012.complement-of-base-10-integer.js (100%) rename 1013-pairs-of-songs-with-total-durations-divisible-by-60.js => 1013.pairs-of-songs-with-total-durations-divisible-by-60.js (100%) rename 1014-capacity-to-ship-packages-within-d-days.js => 1014.capacity-to-ship-packages-within-d-days.js (100%) rename 1015-numbers-with-repeated-digits.js => 1015.numbers-with-repeated-digits.js (100%) rename 1017-convert-to-base-2.js => 1017.convert-to-base-2.js (100%) rename 1018-binary-prefix-divisible-by-5.js => 1018.binary-prefix-divisible-by-5.js (100%) rename 102-binary-tree-level-order-traversal.js => 102.binary-tree-level-order-traversal.js (95%) mode change 100755 => 100644 rename 1020-partition-array-into-three-parts-with-equal-sum.js => 1020.partition-array-into-three-parts-with-equal-sum.js (100%) rename 1021-best-sightseeing-pair.js => 1021.best-sightseeing-pair.js (100%) rename 1021-remove-outermost-parentheses.js => 1021.remove-outermost-parentheses.js (100%) rename 1022-smallest-integer-divisible-by-k.js => 1022.smallest-integer-divisible-by-k.js (100%) rename 1022-sum-of-root-to-leaf-binary-numbers.js => 1022.sum-of-root-to-leaf-binary-numbers.js (100%) rename 1023-binary-string-with-substrings-representing-1-to-n.js => 1023.binary-string-with-substrings-representing-1-to-n.js (100%) rename 1023-camelcase-matching.js => 1023.camelcase-matching.js (100%) rename 1024-video-stitching.js => 1024.video-stitching.js (100%) rename 1025-divisor-game.js => 1025.divisor-game.js (100%) rename 1026-maximum-difference-between-node-and-ancestor.js => 1026.maximum-difference-between-node-and-ancestor.js (100%) rename 1027-longest-arithmetic-sequence.js => 1027.longest-arithmetic-sequence.js (100%) rename 1028-recover-a-tree-from-preorder-traversal.js => 1028.recover-a-tree-from-preorder-traversal.js (100%) rename 1029-two-city-scheduling.js => 1029.two-city-scheduling.js (100%) rename 103-binary-tree-zigzag-level-order-traversal.js => 103.binary-tree-zigzag-level-order-traversal.js (100%) rename 1030-matrix-cells-in-distance-order.js => 1030.matrix-cells-in-distance-order.js (100%) rename 1030-next-greater-node-in-linked-list.js => 1030.next-greater-node-in-linked-list.js (100%) rename 1031-maximum-sum-of-two-non-overlapping-subarrays.js => 1031.maximum-sum-of-two-non-overlapping-subarrays.js (100%) rename 1031-number-of-enclaves.js => 1031.number-of-enclaves.js (100%) rename 1032-stream-of-characters.js => 1032.stream-of-characters.js (100%) rename 1033-moving-stones-until-consecutive.js => 1033.moving-stones-until-consecutive.js (100%) rename 1034-coloring-a-border.js => 1034.coloring-a-border.js (100%) rename 1035-uncrossed-lines.js => 1035.uncrossed-lines.js (100%) rename 1036-escape-a-large-maze.js => 1036.escape-a-large-maze.js (100%) rename 1037-valid-boomerang.js => 1037.valid-boomerang.js (100%) rename 1038-binary-search-tree-to-greater-sum-tree.js => 1038.binary-search-tree-to-greater-sum-tree.js (100%) rename 1039-minimum-score-triangulation-of-polygon.js => 1039.minimum-score-triangulation-of-polygon.js (100%) rename 104-maximum-depth-of-binary-tree.js => 104.maximum-depth-of-binary-tree.js (95%) mode change 100755 => 100644 rename 1040-moving-stones-until-consecutive-ii.js => 1040.moving-stones-until-consecutive-ii.js (100%) rename 1041-robot-bounded-in-circle.js => 1041.robot-bounded-in-circle.js (100%) rename 1042-flower-planting-with-no-adjacent.js => 1042.flower-planting-with-no-adjacent.js (100%) rename 1043-partition-array-for-maximum-sum.js => 1043.partition-array-for-maximum-sum.js (100%) rename 1044-longest-duplicate-substring.js => 1044.longest-duplicate-substring.js (100%) rename 1046-last-stone-weight.js => 1046.last-stone-weight.js (100%) rename 1047-remove-all-adjacent-duplicates-in-string.js => 1047.remove-all-adjacent-duplicates-in-string.js (100%) rename 1048-longest-string-chain.js => 1048.longest-string-chain.js (100%) rename 1049-last-stone-weight-ii.js => 1049.last-stone-weight-ii.js (100%) rename 105-construct-binary-tree-from-preorder-and-inorder-traversal.js => 105.construct-binary-tree-from-preorder-and-inorder-traversal.js (100%) rename 1051-height-checker.js => 1051.height-checker.js (100%) rename 1052-grumpy-bookstore-owner.js => 1052.grumpy-bookstore-owner.js (100%) rename 1053-previous-permutation-with-one-swap.js => 1053.previous-permutation-with-one-swap.js (100%) rename 1054-distant-barcodes.js => 1054.distant-barcodes.js (100%) rename 1056-confusing-number.js => 1056.confusing-number.js (100%) rename 1059-all-paths-from-source-lead-to-destination.js => 1059.all-paths-from-source-lead-to-destination.js (100%) rename 106-construct-binary-tree-from-inorder-and-postorder-traversal.js => 106.construct-binary-tree-from-inorder-and-postorder-traversal.js (100%) rename 1060-missing-element-in-sorted-array.js => 1060.missing-element-in-sorted-array.js (100%) rename 1061-lexicographically-smallest-equivalent-string.js => 1061.lexicographically-smallest-equivalent-string.js (100%) rename 1062-longest-repeating-substring.js => 1062.longest-repeating-substring.js (100%) rename 1064-fixed-point.js => 1064.fixed-point.js (100%) rename 1065-index-pairs-of-a-string.js => 1065.index-pairs-of-a-string.js (100%) rename 1066-campus-bikes-ii.js => 1066.campus-bikes-ii.js (100%) rename 1067-digit-count-in-range.js => 1067.digit-count-in-range.js (100%) rename 107-binary-tree-level-order-traversal-ii.js => 107.binary-tree-level-order-traversal-ii.js (100%) rename 1071-greatest-common-divisor-of-strings.js => 1071.greatest-common-divisor-of-strings.js (100%) rename 1072-flip-columns-for-maximum-number-of-equal-rows.js => 1072.flip-columns-for-maximum-number-of-equal-rows.js (100%) rename 1073-adding-two-negabinary-numbers.js => 1073.adding-two-negabinary-numbers.js (100%) rename 1074-number-of-submatrices-that-sum-to-target.js => 1074.number-of-submatrices-that-sum-to-target.js (100%) rename 1079-letter-tile-possibilities.js => 1079.letter-tile-possibilities.js (100%) rename 108-convert-sorted-array-to-binary-search-tree.js => 108.convert-sorted-array-to-binary-search-tree.js (95%) mode change 100755 => 100644 rename 1081-smallest-subsequence-of-distinct-characters.js => 1081.smallest-subsequence-of-distinct-characters.js (100%) rename 1085-sum-of-digits-in-the-minimum-number.js => 1085.sum-of-digits-in-the-minimum-number.js (100%) rename 1086-high-five.js => 1086.high-five.js (100%) rename 1087-brace-expansion.js => 1087.brace-expansion.js (100%) rename 1088-confusing-number-ii.js => 1088.confusing-number-ii.js (100%) rename 1089-duplicate-zeros.js => 1089.duplicate-zeros.js (100%) rename 109-convert-sorted-list-to-binary-search-tree.js => 109.convert-sorted-list-to-binary-search-tree.js (100%) rename 1090-largest-values-from-labels.js => 1090.largest-values-from-labels.js (100%) rename 1091-shortest-path-in-binary-matrix.js => 1091.shortest-path-in-binary-matrix.js (100%) rename 1092-shortest-common-supersequence.js => 1092.shortest-common-supersequence.js (100%) rename 1093-statistics-from-a-large-sample.js => 1093.statistics-from-a-large-sample.js (100%) rename 1094-car-pooling.js => 1094.car-pooling.js (100%) rename 1095-find-in-mountain-array.js => 1095.find-in-mountain-array.js (100%) rename 1096-brace-expansion-ii.js => 1096.brace-expansion-ii.js (100%) rename 1099-two-sum-less-than-k.js => 1099.two-sum-less-than-k.js (100%) rename 11-container-with-most-water.js => 11.container-with-most-water.js (95%) mode change 100755 => 100644 rename 110-balanced-binary-tree.js => 110.balanced-binary-tree.js (100%) rename 1102-path-with-maximum-minimum-value.js => 1102.path-with-maximum-minimum-value.js (100%) rename 1103-distribute-candies-to-people.js => 1103.distribute-candies-to-people.js (100%) rename 1104.path-in-zigzag-labelled-binary-tree.js => 1104.path.in-zigzag-labelled-binary-tree.js (100%) rename 1105-filling-bookcase-shelves.js => 1105.filling-bookcase-shelves.js (100%) rename 1106-parsing-a-boolean-expression.js => 1106.parsing-a-boolean-expression.js (100%) rename 1108-defanging-an-ip-address.js => 1108.defanging-an-ip-address.js (100%) rename 1109-corporate-flight-bookings.js => 1109.corporate-flight-bookings.js (100%) rename 111-minimum-depth-of-binary-tree.js => 111.minimum-depth-of-binary-tree.js (100%) rename 1110-delete-nodes-and-return-forest.js => 1110.delete-nodes-and-return-forest.js (100%) rename 1111-maximum-nesting-depth-of-two-valid-parentheses-strings.js => 1111.maximum-nesting-depth-of-two-valid-parentheses-strings.js (100%) rename 1118-number-of-days-in-a-month.js => 1118.number-of-days-in-a-month.js (100%) rename 1119-remove-vowels-from-a-string.js => 1119.remove-vowels-from-a-string.js (100%) rename 112-path-sum.js => 112.path-sum.js (100%) rename 1120-maximum-average-subtree.js => 1120.maximum-average-subtree.js (100%) rename 1121-divide-array-into-increasing-sequences.js => 1121.divide-array-into-increasing-sequences.js (100%) rename 1122-relative-sort-array.js => 1122.relative-sort-array.js (100%) rename 1123-lowest-common-ancestor-of-deepest-leaves.js => 1123.lowest-common-ancestor-of-deepest-leaves.js (100%) rename 1124-longest-well-performing-interval.js => 1124.longest-well-performing-interval.js (100%) rename 1125-smallest-sufficient-team.js => 1125.smallest-sufficient-team.js (100%) rename 1128-number-of-equivalent-domino-pairsnumber-of-equivalent-domino-pairs.js => 1128.number-of-equivalent-domino-pairsnumber-of-equivalent-domino-pairs.js (100%) rename 1129-shortest-path-with-alternating-colors.js => 1129.shortest-path-with-alternating-colors.js (100%) rename 113-path-sum-ii.js => 113.path-sum-ii.js (100%) rename 1130-minimum-cost-tree-from-leaf-values.js => 1130.minimum-cost-tree-from-leaf-values.js (100%) rename 1133-largest-unique-number.js => 1133.largest-unique-number.js (100%) rename 1134-armstrong-number.js => 1134.armstrong-number.js (100%) rename 1136-parallel-courses.js => 1136.parallel-courses.js (100%) rename 1137-n-th-tribonacci-number.js => 1137.n-th-tribonacci-number.js (100%) rename 1138-alphabet-board-path.js => 1138.alphabet-board-path.js (100%) rename 1139-largest-1-bordered-square.js => 1139.largest-1-bordered-square.js (100%) rename 114-flatten-binary-tree-to-linked-list.js => 114.flatten-binary-tree-to-linked-list.js (100%) rename 1140-stone-game-ii.js => 1140.stone-game-ii.js (100%) rename 1143-longest-common-subsequence.js => 1143.longest-common-subsequence.js (100%) rename 1145-binary-tree-coloring-game.js => 1145.binary-tree-coloring-game.js (100%) rename 1146-snapshot-array.js => 1146.snapshot-array.js (100%) rename 1147-longest-chunked-palindrome-decomposition.js => 1147.longest-chunked-palindrome-decomposition.js (100%) rename 115-distinct-subsequences.js => 115.distinct-subsequences.js (100%) rename 1150-check-if-a-number-is-majority-element-in-a-sorted-array.js => 1150.check-if-a-number-is-majority-element-in-a-sorted-array.js (100%) rename 1151-minimum-swaps-to-group-all-1s-together.js => 1151.minimum-swaps-to-group-all-1s-together.js (100%) rename 1153-string-transforms-into-another-string.js => 1153.string-transforms-into-another-string.js (100%) rename 1154-day-of-the-year.js => 1154.day-of-the-year.js (100%) rename 1155-number-of-dice-rolls-with-target-sum.js => 1155.number-of-dice-rolls-with-target-sum.js (100%) rename 1156-swap-for-longest-repeated-character-substring.js => 1156.swap-for-longest-repeated-character-substring.js (100%) rename 1157-online-majority-element-in-subarray.js => 1157.online-majority-element-in-subarray.js (100%) rename 116-populating-next-right-pointers-in-each-node.js => 116.populating-next-right-pointers-in-each-node.js (100%) rename 1160-find-words-that-can-be-formed-by-characters.js => 1160.find-words-that-can-be-formed-by-characters.js (100%) rename 1161-maximum-level-sum-of-a-binary-tree.js => 1161.maximum-level-sum-of-a-binary-tree.js (100%) rename 1162-as-far-from-land-as-possible.js => 1162.as-far-from-land-as-possible.js (100%) rename 1163-last-substring-in-lexicographical-order.js => 1163.last-substring-in-lexicographical-order.js (100%) rename 1165-single-row-keyboard.js => 1165.single-row-keyboard.js (100%) rename 1166-design-file-system.js => 1166.design-file-system.js (100%) rename 1167-minimum-cost-to-connect-sticks.js => 1167.minimum-cost-to-connect-sticks.js (100%) rename 1168-optimize-water-distribution-in-a-village.js => 1168.optimize-water-distribution-in-a-village.js (100%) rename 117-populating-next-right-pointers-in-each-node-ii.js => 117.populating-next-right-pointers-in-each-node-ii.js (100%) rename 1170-compare-strings-by-frequency-of-the-smallest-character.js => 1170.compare-strings-by-frequency-of-the-smallest-character.js (100%) rename 1171-remove-zero-sum-consecutive-nodes-from-linked-list.js => 1171.remove-zero-sum-consecutive-nodes-from-linked-list.js (100%) rename 1172-dinner-plate-stacks.js => 1172.dinner-plate-stacks.js (100%) rename 1175-prime-arrangements.js => 1175.prime-arrangements.js (100%) rename 1176-diet-plan-performance.js => 1176.diet-plan-performance.js (100%) rename 1177-can-make-palindrome-from-substring.js => 1177.can-make-palindrome-from-substring.js (100%) rename 1178-number-of-valid-words-for-each-puzzle.js => 1178.number-of-valid-words-for-each-puzzle.js (100%) rename 118-pascal's-triangle.js => 118.pascal's-triangle.js (95%) mode change 100755 => 100644 rename 1180-count-substrings-with-only-one-distinct-letter.js => 1180.count-substrings-with-only-one-distinct-letter.js (100%) rename 1181-before-and-after-puzzle.js => 1181.before-and-after-puzzle.js (100%) rename 1183-maximum-number-of-ones.js => 1183.maximum-number-of-ones.js (100%) rename 1184-distance-between-bus-stops.js => 1184.distance-between-bus-stops.js (100%) rename 1185-day-of-the-week.js => 1185.day-of-the-week.js (100%) rename 1186-maximum-subarray-sum-with-one-deletion.js => 1186.maximum-subarray-sum-with-one-deletion.js (100%) rename 1187-make-array-strictly-increasing.js => 1187.make-array-strictly-increasing.js (100%) rename 1189-maximum-number-of-balloons.js => 1189.maximum-number-of-balloons.js (100%) rename 119-pascal's-triangle-II.js => 119.pascal's-triangle-II.js (95%) mode change 100755 => 100644 rename 1190-reverse-substrings-between-each-pair-of-parentheses.js => 1190.reverse-substrings-between-each-pair-of-parentheses.js (100%) rename 1191-k-concatenation-maximum-sum.js => 1191.k-concatenation-maximum-sum.js (100%) rename 1192-critical-connections-in-a-network.js => 1192.critical-connections-in-a-network.js (100%) rename 1199-minimum-time-to-build-blocks.js => 1199.minimum-time-to-build-blocks.js (100%) rename 12-integer-to-roman.js => 12.integer-to-roman.js (94%) mode change 100755 => 100644 rename 120-triangle.js => 120.triangle.js (100%) rename 1200-minimum-absolute-difference.js => 1200.minimum-absolute-difference.js (100%) rename 1201-ugly-number-iii.js => 1201.ugly-number-iii.js (100%) rename 1202-smallest-string-with-swaps.js => 1202.smallest-string-with-swaps.js (100%) rename 1203-sort-items-by-groups-respecting-dependencies.js => 1203.sort-items-by-groups-respecting-dependencies.js (100%) rename 1206-design-skiplist.js => 1206.design-skiplist.js (100%) rename 1207-unique-number-of-occurrences.js => 1207.unique-number-of-occurrences.js (100%) rename 1209-remove-all-adjacent-duplicates-in-string-ii.js => 1209.remove-all-adjacent-duplicates-in-string-ii.js (100%) rename 121-best-time-to-buy-and-sell-stock.js => 121.best-time-to-buy-and-sell-stock.js (95%) mode change 100755 => 100644 rename 1210-minimum-moves-to-reach-target-with-rotations.js => 1210.minimum-moves-to-reach-target-with-rotations.js (100%) rename 1213-intersection-of-three-sorted-arrays.js => 1213.intersection-of-three-sorted-arrays.js (100%) rename 1214-two-sum-bsts.js => 1214.two-sum-bsts.js (100%) rename 1216-valid-palindrome-iii.js => 1216.valid-palindrome-iii.js (100%) rename 1217-minimum-cost-to-move-chips-to-the-same-position.js => 1217.minimum-cost-to-move-chips-to-the-same-position.js (100%) rename 1219-path-with-maximum-gold.js => 1219.path-with-maximum-gold.js (100%) rename 122-best-time-to-buy-and-sell-stock-ii.js => 122.best-time-to-buy-and-sell-stock-ii.js (100%) rename 1220-count-vowels-permutation.js => 1220.count-vowels-permutation.js (100%) rename 1221-split-a-string-in-balanced-strings.js => 1221.split-a-string-in-balanced-strings.js (100%) rename 1223-dice-roll-simulation.js => 1223.dice-roll-simulation.js (100%) rename 1224-maximum-equal-frequency.js => 1224.maximum-equal-frequency.js (100%) rename 1228-missing-number-in-arithmetic-progression.js => 1228.missing-number-in-arithmetic-progression.js (100%) rename 1229-meeting-scheduler.js => 1229.meeting-scheduler.js (100%) rename 123-best-time-to-buy-and-sell-stock-iii.js => 123.best-time-to-buy-and-sell-stock-iii.js (100%) rename 1231-divide-chocolate.js => 1231.divide-chocolate.js (100%) rename 1232-check-if-it-is-a-straight-line.js => 1232.check-if-it-is-a-straight-line.js (100%) rename 1235-maximum-profit-in-job-scheduling.js => 1235.maximum-profit-in-job-scheduling.js (100%) rename 1239-maximum-length-of-a-concatenated-string-with-unique-characters.js => 1239.maximum-length-of-a-concatenated-string-with-unique-characters.js (100%) rename 124-binary-tree-maximum-path-sum.js => 124.binary-tree-maximum-path-sum.js (100%) rename 1240-tiling-a-rectangle-with-the-fewest-squares.js => 1240.tiling-a-rectangle-with-the-fewest-squares.js (100%) rename 1243-array-transformation.js => 1243.array-transformation.js (100%) rename 1244-design-a-leaderboard.js => 1244.design-a-leaderboard.js (100%) rename 1245-tree-diameter.js => 1245.tree-diameter.js (100%) rename 1246-palindrome-removal.js => 1246.palindrome-removal.js (100%) rename 1247-minimum-swaps-to-make-strings-equal.js => 1247.minimum-swaps-to-make-strings-equal.js (100%) rename 1249-minimum-remove-to-make-valid-parentheses.js => 1249.minimum-remove-to-make-valid-parentheses.js (100%) rename 125-valid-palindrome.js => 125.valid-palindrome.js (100%) rename 1252-cells-with-odd-values-in-a-matrix.js => 1252.cells-with-odd-values-in-a-matrix.js (100%) rename 1254-number-of-closed-islands.js => 1254.number-of-closed-islands.js (100%) rename 1255-maximum-score-words-formed-by-letters.js => 1255.maximum-score-words-formed-by-letters.js (100%) rename 1257-smallest-common-region.js => 1257.smallest-common-region.js (100%) rename 126-word-ladder-ii.js => 126.word-ladder-ii.js (100%) rename 1260-shift-2d-grid.js => 1260.shift-2d-grid.js (100%) rename 1261-find-elements-in-a-contaminated-binary-tree.js => 1261.find-elements-in-a-contaminated-binary-tree.js (100%) rename 1262-greatest-sum-divisible-by-three.js => 1262.greatest-sum-divisible-by-three.js (100%) rename 1263-minimum-moves-to-move-a-box-to-their-target-location.js => 1263.minimum-moves-to-move-a-box-to-their-target-location.js (100%) rename 1265-print-immutable-linked-list-in-reverse.js => 1265.print-immutable-linked-list-in-reverse.js (100%) rename 1266-minimum-time-visiting-all-points.js => 1266.minimum-time-visiting-all-points.js (100%) rename 1268-search-suggestions-system.js => 1268.search-suggestions-system.js (100%) rename 1269-number-of-ways-to-stay-in-the-same-place-after-some-steps.js => 1269.number-of-ways-to-stay-in-the-same-place-after-some-steps.js (100%) rename 127-word-ladder.js => 127.word-ladder.js (100%) rename 1271-hexspeak.js => 1271.hexspeak.js (100%) rename 1272-remove-interval.js => 1272.remove-interval.js (100%) rename 1273-delete-tree-nodes.js => 1273.delete-tree-nodes.js (100%) rename 1274-number-of-ships-in-a-rectangle.js => 1274.number-of-ships-in-a-rectangle.js (100%) rename 1275-find-winner-on-a-tic-tac-toe-game.js => 1275.find-winner-on-a-tic-tac-toe-game.js (100%) rename 1277-count-square-submatrices-with-all-ones.js => 1277.count-square-submatrices-with-all-ones.js (100%) rename 1278-palindrome-partitioning-iii.js => 1278.palindrome-partitioning-iii.js (100%) rename 128-longest-consecutive-sequence.js => 128.longest-consecutive-sequence.js (100%) rename 1281-subtract-the-product-and-sum-of-digits-of-an-integer.js => 1281.subtract-the-product-and-sum-of-digits-of-an-integer.js (100%) rename 1282-group-the-people-given-the-group-size-they-belong-to.js => 1282.group-the-people-given-the-group-size-they-belong-to.js (100%) rename 1283-find-the-smallest-divisor-given-a-threshold.js => 1283.find-the-smallest-divisor-given-a-threshold.js (100%) rename 1284-minimum-number-of-flips-to-convert-binary-matrix-to-zero-matrix.js => 1284.minimum-number-of-flips-to-convert-binary-matrix-to-zero-matrix.js (100%) rename 1286-iterator-for-combination.js => 1286.iterator-for-combination.js (100%) rename 1287-element-appearing-more-than-25-in-sorted-array.js => 1287.element-appearing-more-than-25-in-sorted-array.js (100%) rename 1288-remove-covered-intervals.js => 1288.remove-covered-intervals.js (100%) rename 1289-minimum-falling-path-sum-ii.js => 1289.minimum-falling-path-sum-ii.js (100%) rename 129-sum-root-to-leaf-numbers.js => 129.sum-root-to-leaf-numbers.js (100%) rename 1290-convert-binary-number-in-a-linked-list-to-integer.js => 1290.convert-binary-number-in-a-linked-list-to-integer.js (100%) rename 1291-sequential-digits.js => 1291.sequential-digits.js (100%) rename 1292-maximum-side-length-of-a-square-with-sum-less-than-or-equal-to-threshold.js => 1292.maximum-side-length-of-a-square-with-sum-less-than-or-equal-to-threshold.js (100%) rename 1293-shortest-path-in-a-grid-with-obstacles-elimination.js => 1293.shortest-path-in-a-grid-with-obstacles-elimination.js (100%) rename 1295-find-numbers-with-even-number-of-digits.js => 1295.find-numbers-with-even-number-of-digits.js (100%) rename 1299-replace-elements-with-greatest-element-on-right-side.js => 1299.replace-elements-with-greatest-element-on-right-side.js (100%) rename 13-roman-to-integer.js => 13.roman-to-integer.js (100%) rename 130-surrounded-regions.js => 130.surrounded-regions.js (100%) rename 1300-sum-of-mutated-array-closest-to-target.js => 1300.sum-of-mutated-array-closest-to-target.js (100%) rename 1301-number-of-paths-with-max-score.js => 1301.number-of-paths-with-max-score.js (100%) rename 1302-deepest-leaves-sum.js => 1302.deepest-leaves-sum.js (100%) rename 1304-find-n-unique-integers-sum-up-to-zero.js => 1304.find-n-unique-integers-sum-up-to-zero.js (100%) rename 1305-all-elements-in-two-binary-search-trees.js => 1305.all-elements-in-two-binary-search-trees.js (100%) rename 1306-jump-game-iii.js => 1306.jump-game-iii.js (100%) rename 1307-verbal-arithmetic-puzzle.js => 1307.verbal-arithmetic-puzzle.js (100%) rename 1309-decrypt-string-from-alphabet-to-integer-mapping.js => 1309.decrypt-string-from-alphabet-to-integer-mapping.js (100%) rename 131-palindrome-partitioning.js => 131.palindrome-partitioning.js (100%) rename 1310-xor-queries-of-a-subarray.js => 1310.xor-queries-of-a-subarray.js (100%) rename 1312-minimum-insertion-steps-to-make-a-string-palindrome.js => 1312.minimum-insertion-steps-to-make-a-string-palindrome.js (100%) rename 1313-decompress-run-length-encoded-list.js => 1313.decompress-run-length-encoded-list.js (100%) rename 1314-matrix-block-sum.js => 1314.matrix-block-sum.js (100%) rename 1315-sum-of-nodes-with-even-valued-grandparent.js => 1315.sum-of-nodes-with-even-valued-grandparent.js (100%) rename 1316-distinct-echo-substrings.js => 1316.distinct-echo-substrings.js (100%) rename 132-palindrome-partitioning-ii.js => 132.palindrome-partitioning-ii.js (100%) rename 1320-minimum-distance-to-type-a-word-using-two-fingers.js => 1320.minimum-distance-to-type-a-word-using-two-fingers.js (100%) rename 1323-maximum-69-number.js => 1323.maximum-69-number.js (100%) rename 1324-print-words-vertically.js => 1324.print-words-vertically.js (100%) rename 1325-delete-leaves-with-a-given-value.js => 1325.delete-leaves-with-a-given-value.js (100%) rename 1326-minimum-number-of-taps-to-open-to-water-a-garden.js => 1326.minimum-number-of-taps-to-open-to-water-a-garden.js (100%) rename 1329-sort-the-matrix-diagonally.js => 1329.sort-the-matrix-diagonally.js (100%) rename 133-clone-graph.js => 133.clone-graph.js (100%) rename 1330-reverse-subarray-to-maximize-array-value.js => 1330.reverse-subarray-to-maximize-array-value.js (100%) rename 1331-rank-transform-of-an-array.js => 1331.rank-transform-of-an-array.js (100%) rename 1332-remove-palindromic-subsequences.js => 1332.remove-palindromic-subsequences.js (100%) rename 1335-minimum-difficulty-of-a-job-schedule.js => 1335.minimum-difficulty-of-a-job-schedule.js (100%) rename 1337-the-k-weakest-rows-in-a-matrix.js => 1337.the-k-weakest-rows-in-a-matrix.js (100%) rename 1338-reduce-array-size-to-the-half.js => 1338.reduce-array-size-to-the-half.js (100%) rename 134-gas-station.js => 134.gas-station.js (100%) rename 1340-jump-game-v.js => 1340.jump-game-v.js (100%) rename 1342-number-of-steps-to-reduce-a-number-to-zero.js => 1342.number-of-steps-to-reduce-a-number-to-zero.js (100%) rename 1343-number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold.js => 1343.number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold.js (100%) rename 1344-angle-between-hands-of-a-clock.js => 1344.angle-between-hands-of-a-clock.js (100%) rename 1345-jump-game-iv.js => 1345.jump-game-iv.js (100%) rename 1347-minimum-number-of-steps-to-make-two-strings-anagram.js => 1347.minimum-number-of-steps-to-make-two-strings-anagram.js (100%) rename 1348-tweet-counts-per-frequency.js => 1348.tweet-counts-per-frequency.js (100%) rename 1349-maximum-students-taking-exam.js => 1349.maximum-students-taking-exam.js (100%) rename 135-candy.js => 135.candy.js (100%) rename 1351-count-negative-numbers-in-a-sorted-matrix.js => 1351.count-negative-numbers-in-a-sorted-matrix.js (100%) rename 1352-product-of-the-last-k-numbers.js => 1352.product-of-the-last-k-numbers.js (100%) rename 1353-maximum-number-of-events-that-can-be-attended.js => 1353.maximum-number-of-events-that-can-be-attended.js (100%) rename 1354-construct-target-array-with-multiple-sums.js => 1354.construct-target-array-with-multiple-sums.js (100%) rename 1356-sort-integers-by-the-number-of-1-bits.js => 1356.sort-integers-by-the-number-of-1-bits.js (100%) rename 1359-count-all-valid-pickup-and-delivery-options.js => 1359.count-all-valid-pickup-and-delivery-options.js (100%) rename 136-single-number.js => 136.single-number.js (100%) rename 1360-number-of-days-between-two-dates.js => 1360.number-of-days-between-two-dates.js (100%) rename 1363-largest-multiple-of-three.js => 1363.largest-multiple-of-three.js (100%) rename 1365-how-many-numbers-are-smaller-than-the-current-number.js => 1365.how-many-numbers-are-smaller-than-the-current-number.js (100%) rename 1366-rank-teams-by-votes.js => 1366.rank-teams-by-votes.js (100%) rename 1367-linked-list-in-binary-tree.js => 1367.linked-list-in-binary-tree.js (100%) rename 1368-minimum-cost-to-make-at-least-one-valid-path-in-a-grid.js => 1368.minimum-cost-to-make-at-least-one-valid-path-in-a-grid.js (100%) rename 137-single-number-ii.js => 137.single-number-ii.js (100%) rename 1370-increasing-decreasing-string.js => 1370.increasing-decreasing-string.js (100%) rename 1371-find-the-longest-substring-containing-vowels-in-even-counts.js => 1371.find-the-longest-substring-containing-vowels-in-even-counts.js (100%) rename 1373-maximum-sum-bst-in-binary-tree.js => 1373.maximum-sum-bst-in-binary-tree.js (100%) rename 1374-generate-a-string-with-characters-that-have-odd-counts.js => 1374.generate-a-string-with-characters-that-have-odd-counts.js (100%) rename 1375-number-of-times-binary-string-is-prefix-aligned.js => 1375.number-of-times-binary-string-is-prefix-aligned.js (100%) rename 1376-time-needed-to-inform-all-employees.js => 1376.time-needed-to-inform-all-employees.js (100%) rename 1377-frog-position-after-t-seconds.js => 1377.frog-position-after-t-seconds.js (100%) rename 138-copy-list-with-random-pointer.js => 138.copy-list-with-random-pointer.js (100%) rename 1380-lucky-numbers-in-a-matrix.js => 1380.lucky-numbers-in-a-matrix.js (100%) rename 1381-design-a-stack-with-increment-operation.js => 1381.design-a-stack-with-increment-operation.js (100%) rename 1382-balance-a-binary-search-tree.js => 1382.balance-a-binary-search-tree.js (100%) rename 1383-maximum-performance-of-a-team.js => 1383.maximum-performance-of-a-team.js (100%) rename 1385-find-the-distance-value-between-two-arrays.js => 1385.find-the-distance-value-between-two-arrays.js (100%) rename 1387-sort-integers-by-the-power-value.js => 1387.sort-integers-by-the-power-value.js (100%) rename 1388-pizza-with-3n-slices.js => 1388.pizza-with-3n-slices.js (100%) rename 1389-create-target-array-in-the-given-order.js => 1389.create-target-array-in-the-given-order.js (100%) rename 139-word-break.js => 139.word-break.js (100%) rename 1390-four-divisors.js => 1390.four-divisors.js (100%) rename 1392-longest-happy-prefix.js => 1392.longest-happy-prefix.js (100%) rename 1394-find-lucky-integer-in-an-array.js => 1394.find-lucky-integer-in-an-array.js (100%) rename 1395-count-number-of-teams.js => 1395.count-number-of-teams.js (100%) rename 1396-count-number-of-teams.js => 1396.count-number-of-teams.js (100%) rename 1396-design-underground-system.js => 1396.design-underground-system.js (100%) rename 1397-find-all-good-strings.js => 1397.find-all-good-strings.js (100%) rename 1399-count-largest-group.js => 1399.count-largest-group.js (100%) rename 14-longest-common-prefix.js => 14.longest-common-prefix.js (95%) mode change 100755 => 100644 rename 140-word-break-ii.js => 140.word-break-ii.js (100%) rename 1402-reducing-dishes.js => 1402.reducing-dishes.js (100%) rename 1404-number-of-steps-to-reduce-a-number-in-binary-representation-to-one.js => 1404.number-of-steps-to-reduce-a-number-in-binary-representation-to-one.js (100%) rename 1405-longest-happy-string.js => 1405.longest-happy-string.js (100%) rename 1406-stone-game-iii.js => 1406.stone-game-iii.js (100%) rename 1408-string-matching-in-an-array.js => 1408.string-matching-in-an-array.js (100%) rename 1409-queries-on-a-permutation-with-key.js => 1409.queries-on-a-permutation-with-key.js (100%) rename 141-linked-list-cycle.js => 141.linked-list-cycle.js (100%) rename 1410-html-entity-parser.js => 1410.html-entity-parser.js (100%) rename 1411-number-of-ways-to-paint-n-3-grid.js => 1411.number-of-ways-to-paint-n-3-grid.js (100%) rename 1413-minimum-value-to-get-positive-step-by-step-sum.js => 1413.minimum-value-to-get-positive-step-by-step-sum.js (100%) rename 1415-the-k-th-lexicographical-string-of-all-happy-strings-of-length-n.js => 1415.the-k-th-lexicographical-string-of-all-happy-strings-of-length-n.js (100%) rename 1416-restore-the-array.js => 1416.restore-the-array.js (100%) rename 1417-reformat-the-string.js => 1417.reformat-the-string.js (100%) rename 142-linked-list-cycle-ii.js => 142.linked-list-cycle-ii.js (100%) rename 1422-maximum-score-after-splitting-a-string.js => 1422.maximum-score-after-splitting-a-string.js (100%) rename 1424-diagonal-traverse-ii.js => 1424.diagonal-traverse-ii.js (100%) rename 1425-constrained-subsequence-sum.js => 1425.constrained-subsequence-sum.js (100%) rename 1429-build-array-where-you-can-find-the-maximum-exactly-k-comparisons.js => 1429.build-array-where-you-can-find-the-maximum-exactly-k-comparisons.js (100%) rename 143-reorder-list.js => 143.reorder-list.js (100%) rename 1431-kids-with-the-greatest-number-of-candies.js => 1431.kids-with-the-greatest-number-of-candies.js (100%) rename 1434-number-of-ways-to-wear-different-hats-to-each-other.js => 1434.number-of-ways-to-wear-different-hats-to-each-other.js (100%) rename 1436-destination-city.js => 1436.destination-city.js (100%) rename 1438-longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit.js => 1438.longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit.js (100%) rename 1439-find-the-kth-smallest-sum-of-a-matrix-with-sorted-rows.js => 1439.find-the-kth-smallest-sum-of-a-matrix-with-sorted-rows.js (100%) rename 144-binary-tree-preorder-traversal.js => 144.binary-tree-preorder-traversal.js (94%) mode change 100755 => 100644 rename 1441-build-an-array-with-stack-operations.js => 1441.build-an-array-with-stack-operations.js (100%) rename 1442-count-triplets-that-can-form-two-arrays-of-equal-xor.js => 1442.count-triplets-that-can-form-two-arrays-of-equal-xor.js (100%) rename 1444-number-of-ways-of-cutting-a-pizza.js => 1444.number-of-ways-of-cutting-a-pizza.js (100%) rename 1446-consecutive-characters.js => 1446.consecutive-characters.js (100%) rename 1448-count-good-nodes-in-binary-tree.js => 1448.count-good-nodes-in-binary-tree.js (100%) rename 1449-form-largest-integer-with-digits-that-add-up-to-target.js => 1449.form-largest-integer-with-digits-that-add-up-to-target.js (100%) rename 145-binary-tree-postorder-traversal.js => 145.binary-tree-postorder-traversal.js (100%) rename 1452-people-whose-list-of-favorite-companies-is-not-a-subset-of-another-list.js => 1452.people-whose-list-of-favorite-companies-is-not-a-subset-of-another-list.js (100%) rename 1458-max-dot-product-of-two-subsequences.js => 1458.max-dot-product-of-two-subsequences.js (100%) rename 146-lru-cache.js => 146.lru-cache.js (95%) mode change 100755 => 100644 rename 1460-make-two-arrays-equal-by-reversing-sub-arrays.js => 1460.make-two-arrays-equal-by-reversing-sub-arrays.js (100%) rename 1461-check-if-a-string-contains-all-binary-codes-of-size-k.js => 1461.check-if-a-string-contains-all-binary-codes-of-size-k.js (100%) rename 1462-course-schedule-iv.js => 1462.course-schedule-iv.js (100%) rename 1463-cherry-pickup-ii.js => 1463.cherry-pickup-ii.js (100%) rename 1464-maximum-product-of-two-elements-in-an-array.js => 1464.maximum-product-of-two-elements-in-an-array.js (100%) rename 1465-maximum-area-of-a-piece-of-cake-after-horizontal-and-vertical-cuts.js => 1465.maximum-area-of-a-piece-of-cake-after-horizontal-and-vertical-cuts.js (100%) rename 1467-probability-of-a-two-boxes-having-the-same-number-of-distinct-balls.js => 1467.probability-of-a-two-boxes-having-the-same-number-of-distinct-balls.js (100%) rename 147-insertion-sort-list.js => 147.insertion-sort-list.js (100%) rename 1470-shuffle-the-array.js => 1470.shuffle-the-array.js (100%) rename 1472-design-browser-history.js => 1472.design-browser-history.js (100%) rename 1473-paint-house-iii.js => 1473.paint-house-iii.js (100%) rename 1475-final-prices-with-a-special-discount-in-a-shop.js => 1475.final-prices-with-a-special-discount-in-a-shop.js (100%) rename 1476-subrectangle-queries.js => 1476.subrectangle-queries.js (100%) rename 1478-allocate-mailboxes.js => 1478.allocate-mailboxes.js (100%) rename 148-sort-list.js => 148.sort-list.js (100%) rename 1480-running-sum-of-1d-array.js => 1480.running-sum-of-1d-array.js (100%) rename 1481-least-number-of-unique-integers-after k-removals.js => 1481.least-number-of-unique-integers-after k-removals.js (100%) rename 1482-minimum-number-of-days-to-make-m-bouquets.js => 1482.minimum-number-of-days-to-make-m-bouquets.js (100%) rename 1483-kth-ancestor-of-a-tree-node.js => 1483.kth-ancestor-of-a-tree-node.js (100%) rename 1486-xor-operation-in-an-array.js => 1486.xor-operation-in-an-array.js (100%) rename 1489-find-critical-and-pseudo-critical-edges-in-minimum-spanning-tree.js => 1489.find-critical-and-pseudo-critical-edges-in-minimum-spanning-tree.js (100%) rename 149-max-points-on-a-line.js => 149.max-points-on-a-line.js (100%) rename 1490-clone-n-ary-tree.js => 1490.clone-n-ary-tree.js (100%) rename 1492-the-kth-factor-of-n.js => 1492.the-kth-factor-of-n.js (100%) rename 1493-longest-subarray-of-1s-after-deleting-one-element.js => 1493.longest-subarray-of-1s-after-deleting-one-element.js (100%) rename 1494-parallel-courses-ii.js => 1494.parallel-courses-ii.js (100%) rename 1498-number-of-subsequences-that-satisfy-the-given-sum-condition.js => 1498.number-of-subsequences-that-satisfy-the-given-sum-condition.js (100%) rename 1499-max-value-of-equation.js => 1499.max-value-of-equation.js (100%) rename 15-3sum.js => 15.3sum.js (95%) mode change 100755 => 100644 rename 150-evaluate-reverse-polish-notation.js => 150.evaluate-reverse-polish-notation.js (100%) rename 1503-last-moment-before-all-ants-fall-out-of-a-plank.js => 1503.last-moment-before-all-ants-fall-out-of-a-plank.js (100%) rename 1505-minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits.js => 1505.minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits.js (100%) rename 1506-find-root-of-n-ary-tree.js => 1506.find-root-of-n-ary-tree.js (100%) rename 1507-reformat-date.js => 1507.reformat-date.js (100%) rename 1509-minimum-difference-between-largest-and-smallest-value-in-three-moves.js => 1509.minimum-difference-between-largest-and-smallest-value-in-three-moves.js (100%) rename 151-reverse-words-in-a-string.js => 151.reverse-words-in-a-string.js (93%) mode change 100755 => 100644 rename 1510-stone-game-iv.js => 1510.stone-game-iv.js (100%) rename 1512-number-of-good-pairs.js => 1512.number-of-good-pairs.js (100%) rename 1514-path-with-maximum-probability.js => 1514.path-with-maximum-probability.js (100%) rename 1515-best-position-for-a-service-centre.js => 1515.best-position-for-a-service-centre.js (100%) rename 1516-move-sub-tree-of-n-ary-tree.js => 1516.move-sub-tree-of-n-ary-tree.js (100%) rename 152-maximum-product-subarray.js => 152.maximum-product-subarray.js (100%) rename 1521-find-a-value-of-a-mysterious-function-closest-to-target.js => 1521.find-a-value-of-a-mysterious-function-closest-to-target.js (100%) rename 1522-diameter-of-n-ary-tree.js => 1522.diameter-of-n-ary-tree.js (100%) rename 1523-count-odd-numbers-in-an-interval-range.js => 1523.count-odd-numbers-in-an-interval-range.js (100%) rename 1524-number-of-sub-arrays-with-odd-sum.js => 1524.number-of-sub-arrays-with-odd-sum.js (100%) rename 1525-number-of-good-ways-to-split-a-string.js => 1525.number-of-good-ways-to-split-a-string.js (100%) rename 1526-minimum-number-of-increments-on-subarrays-to-form-a-target-array.js => 1526.minimum-number-of-increments-on-subarrays-to-form-a-target-array.js (100%) rename 1528-shuffle-string.js => 1528.shuffle-string.js (100%) rename 1529-minimum-suffix-flips.js => 1529.minimum-suffix-flips.js (100%) rename 153-find-minimum-in-rotated-sorted-array.js => 153.find-minimum-in-rotated-sorted-array.js (100%) rename 1530-number-of-good-leaf-nodes-pairs.js => 1530.number-of-good-leaf-nodes-pairs.js (100%) rename 1531-string-compression-ii.js => 1531.string-compression-ii.js (100%) rename 1534-count-good-triplets.js => 1534.count-good-triplets.js (100%) rename 1537-get-the-maximum-score.js => 1537.get-the-maximum-score.js (100%) rename 1539-kth-missing-positive-number.js => 1539.kth-missing-positive-number.js (100%) rename 154-find-minimum-in-rotated-sorted-array-ii.js => 154.find-minimum-in-rotated-sorted-array-ii.js (100%) rename 1540-can-convert-string-in-k-moves.js => 1540.can-convert-string-in-k-moves.js (100%) rename 1541-minimum-insertions-to-balance-a-parentheses-string.js => 1541.minimum-insertions-to-balance-a-parentheses-string.js (100%) rename 1542-find-longest-awesome-substring.js => 1542.find-longest-awesome-substring.js (100%) rename 1546-maximum-number-of-non-overlapping-subarrays-with-sum-equals-target.js => 1546.maximum-number-of-non-overlapping-subarrays-with-sum-equals-target.js (100%) rename 1547-minimum-cost-to-cut-a-stick.js => 1547.minimum-cost-to-cut-a-stick.js (100%) rename 155-min-stack.js => 155.min-stack.js (100%) rename 1550-three-consecutive-odds.js => 1550.three-consecutive-odds.js (100%) rename 1551-minimum-operations-to-make-array-equal.js => 1551.minimum-operations-to-make-array-equal.js (100%) rename 1552-magnetic-force-between-two-balls.js => 1552.magnetic-force-between-two-balls.js (100%) rename 1553-minimum-number-of-days-to-eat-n-oranges.js => 1553.minimum-number-of-days-to-eat-n-oranges.js (100%) rename 1554-strings-differ-by-one-character.js => 1554.strings-differ-by-one-character.js (100%) rename 1557-minimum-number-of-vertices-to-reach-all-nodes.js => 1557.minimum-number-of-vertices-to-reach-all-nodes.js (100%) rename 1559-detect-cycles-in-2d-grid.js => 1559.detect-cycles-in-2d-grid.js (100%) rename 156-binary-tree-upside-down.js => 156.binary-tree-upside-down.js (100%) rename 1560-most-visited-sector-in-a-circular-track.js => 1560.most-visited-sector-in-a-circular-track.js (100%) rename 1561-maximum-number-of-coins-you-can-get.js => 1561.maximum-number-of-coins-you-can-get.js (100%) rename 1562-find-latest-group-of-size-m.js => 1562.find-latest-group-of-size-m.js (100%) rename 1564-put-boxes-into-the-warehouse-i.js => 1564.put-boxes-into-the-warehouse-i.js (100%) rename 1567-maximum-length-of-subarray-with-positive-product.js => 1567.maximum-length-of-subarray-with-positive-product.js (100%) rename 1568-minimum-number-of-days-to-disconnect-island.js => 1568.minimum-number-of-days-to-disconnect-island.js (100%) rename 1569-number-of-ways-to-reorder-array-to-get-same-bst.js => 1569.number-of-ways-to-reorder-array-to-get-same-bst.js (100%) rename 157-read-n-characters-given-read4.js => 157.read-n-characters-given-read4.js (100%) rename 1570-dot-product-of-two-sparse-vectors.js => 1570.dot-product-of-two-sparse-vectors.js (100%) rename 1572-matrix-diagonal-sum.js => 1572.matrix-diagonal-sum.js (100%) rename 1573-number-of-ways-to-split-a-string.js => 1573.number-of-ways-to-split-a-string.js (100%) rename 1574-shortest-subarray-to-be-removed-to-make-array-sorted.js => 1574.shortest-subarray-to-be-removed-to-make-array-sorted.js (100%) rename 1575-count-all-possible-routes.js => 1575.count-all-possible-routes.js (100%) rename 1576-replace-all-s-to-avoid-consecutive-repeating-characters.js => 1576.replace-all-s-to-avoid-consecutive-repeating-characters.js (100%) rename 1578-minimum-deletion-cost-to-avoid-repeating-letters.js => 1578.minimum-deletion-cost-to-avoid-repeating-letters.js (100%) rename 1579-remove-max-number-of-edges-to-keep-graph-fully-traversable.js => 1579.remove-max-number-of-edges-to-keep-graph-fully-traversable.js (100%) rename 158-read-n-characters-given-read4-ii-call-multiple-times.js => 158.read-n-characters-given-read4-ii-call-multiple-times.js (100%) rename 1584-min-cost-to-connect-all-points.js => 1584.min-cost-to-connect-all-points.js (100%) rename 1585-check-if-string-is-transformable-with-substring-sort-operations.js => 1585.check-if-string-is-transformable-with-substring-sort-operations.js (100%) rename 1586-binary-search-tree-iterator-ii.js => 1586.binary-search-tree-iterator-ii.js (100%) rename 1588-sum-of-all-odd-length-subarrays.js => 1588.sum-of-all-odd-length-subarrays.js (100%) rename 1589-maximum-sum-obtained-of-any-permutation.js => 1589.maximum-sum-obtained-of-any-permutation.js (100%) rename 159-longest-substring-with-at-most-two-distinct-characters.js => 159.longest-substring-with-at-most-two-distinct-characters.js (100%) rename 1590-make-sum-divisible-by-p.js => 1590.make-sum-divisible-by-p.js (100%) rename 1591-strange-printer-ii.js => 1591.strange-printer-ii.js (100%) rename 1592-rearrange-spaces-between-words.js => 1592.rearrange-spaces-between-words.js (100%) rename 1593-split-a-string-into-the-max-number-of-unique-substrings.js => 1593.split-a-string-into-the-max-number-of-unique-substrings.js (100%) rename 1594-maximum-non-negative-product-in-a-matrix.js => 1594.maximum-non-negative-product-in-a-matrix.js (100%) rename 1595-minimum-cost-to-connect-two-groups-of-points.js => 1595.minimum-cost-to-connect-two-groups-of-points.js (100%) rename 1597-build-binary-expression-tree-from-infix-expression.js => 1597.build-binary-expression-tree-from-infix-expression.js (100%) rename 1598-crawler-log-folder.js => 1598.crawler-log-folder.js (100%) rename 1599-maximum-profit-of-operating-a-centennial-wheel.js => 1599.maximum-profit-of-operating-a-centennial-wheel.js (100%) rename 16-3sum-closest.js => 16.3sum-closest.js (95%) mode change 100755 => 100644 rename 160-intersection-of-two-linked-lists.js => 160.intersection-of-two-linked-lists.js (100%) rename 1600-throne-inheritance.js => 1600.throne-inheritance.js (100%) rename 1601-maximum-number-of-achievable-transfer-requests.js => 1601.maximum-number-of-achievable-transfer-requests.js (100%) rename 1602-find-nearest-right-node-in-binary-tree.js => 1602.find-nearest-right-node-in-binary-tree.js (100%) rename 1603-design-parking-system.js => 1603.design-parking-system.js (100%) rename 1605-find-valid-matrix-given-row-and-column-sums.js => 1605.find-valid-matrix-given-row-and-column-sums.js (100%) rename 1606-find-servers-that-handled-most-number-of-requests.js => 1606.find-servers-that-handled-most-number-of-requests.js (100%) rename 1608-special-array-with-x-elements-greater-than-or-equal-x.js => 1608.special-array-with-x-elements-greater-than-or-equal-x.js (100%) rename 1609-even-odd-tree.js => 1609.even-odd-tree.js (100%) rename 161-one-edit-distance.js => 161.one-edit-distance.js (100%) rename 1610-maximum-number-of-visible-points.js => 1610.maximum-number-of-visible-points.js (100%) rename 1611-minimum-one-bit-operations-to-make-integers-zero.js => 1611.minimum-one-bit-operations-to-make-integers-zero.js (100%) rename 1612-check-if-two-expression-trees-are-equivalent.js => 1612.check-if-two-expression-trees-are-equivalent.js (100%) rename 1614-maximum-nesting-depth-of-the-parentheses.js => 1614.maximum-nesting-depth-of-the-parentheses.js (100%) rename 1615-maximal-network-rank.js => 1615.maximal-network-rank.js (100%) rename 1616-split-two-strings-to-make-palindrome.js => 1616.split-two-strings-to-make-palindrome.js (100%) rename 1617-count-subtrees-with-max-distance-between-cities.js => 1617.count-subtrees-with-max-distance-between-cities.js (100%) rename 1619-mean-of-array-after-removing-some-elements.js => 1619.mean-of-array-after-removing-some-elements.js (100%) rename 162-find-peak-element.js => 162.find-peak-element.js (100%) rename 1621-number-of-sets-of-k-non-overlapping-line-segments.js => 1621.number-of-sets-of-k-non-overlapping-line-segments.js (100%) rename 1622-fancy-sequence.js => 1622.fancy-sequence.js (100%) rename 1624-largest-substring-between-two-equal-characters.js => 1624.largest-substring-between-two-equal-characters.js (100%) rename 1625-lexicographically-smallest-string-after-applying-operations.js => 1625.lexicographically-smallest-string-after-applying-operations.js (100%) rename 1626-best-team-with-no-conflicts.js => 1626.best-team-with-no-conflicts.js (100%) rename 1627-graph-connectivity-with-threshold.js => 1627.graph-connectivity-with-threshold.js (100%) rename 1629-slowest-key.js => 1629.slowest-key.js (100%) rename 163-missing-ranges.js => 163.missing-ranges.js (100%) rename 1630-arithmetic-subarrays.js => 1630.arithmetic-subarrays.js (100%) rename 1631-path-with-minimum-effort.js => 1631.path-with-minimum-effort.js (100%) rename 1632-rank-transform-of-a-matrix.js => 1632.rank-transform-of-a-matrix.js (100%) rename 1636-sort-array-by-increasing-frequency.js => 1636.sort-array-by-increasing-frequency.js (100%) rename 1637-widest-vertical-area-between-two-points-containing-no-points.js => 1637.widest-vertical-area-between-two-points-containing-no-points.js (100%) rename 1638-count-substrings-that-differ-by-one-character.js => 1638.count-substrings-that-differ-by-one-character.js (100%) rename 1639-number-of-ways-to-form-a-target-string-given-a-dictionary.js => 1639.number-of-ways-to-form-a-target-string-given-a-dictionary.js (100%) rename 164-maximum-gap.js => 164.maximum-gap.js (100%) rename 1640-check-array-formation-through-concatenation.js => 1640.check-array-formation-through-concatenation.js (100%) rename 1641-count-sorted-vowel-strings.js => 1641.count-sorted-vowel-strings.js (100%) rename 1642-furthest-building-you-can-reach.js => 1642.furthest-building-you-can-reach.js (100%) rename 1643-kth-smallest-instructions.js => 1643.kth-smallest-instructions.js (100%) rename 1644-lowest-common-ancestor-of-a-binary-tree-ii.js => 1644.lowest-common-ancestor-of-a-binary-tree-ii.js (100%) rename 1646-get-maximum-in-generated-array.js => 1646.get-maximum-in-generated-array.js (100%) rename 1647-minimum-deletions-to-make-character-frequencies-unique.js => 1647.minimum-deletions-to-make-character-frequencies-unique.js (100%) rename 1648-sell-diminishing-valued-colored-balls.js => 1648.sell-diminishing-valued-colored-balls.js (100%) rename 1649-create-sorted-array-through-instructions.js => 1649.create-sorted-array-through-instructions.js (100%) rename 165-compare-version-numbers.js => 165.compare-version-numbers.js (100%) rename 1650-lowest-common-ancestor-of-a-binary-tree-iii.js => 1650.lowest-common-ancestor-of-a-binary-tree-iii.js (100%) rename 1652-defuse-the-bomb.js => 1652.defuse-the-bomb.js (100%) rename 1653-minimum-deletions-to-make-string-balanced.js => 1653.minimum-deletions-to-make-string-balanced.js (100%) rename 1654-minimum-jumps-to-reach-home.js => 1654.minimum-jumps-to-reach-home.js (100%) rename 1655-distribute-repeating-integers.js => 1655.distribute-repeating-integers.js (100%) rename 1656-design-an-ordered-stream.js => 1656.design-an-ordered-stream.js (100%) rename 1657-determine-if-two-strings-are-close.js => 1657.determine-if-two-strings-are-close.js (100%) rename 1658-minimum-operations-to-reduce-x-to-zero.js => 1658.minimum-operations-to-reduce-x-to-zero.js (100%) rename 1659-maximize-grid-happiness.js => 1659.maximize-grid-happiness.js (100%) rename 166-fraction-to-recurring-decimal.js => 166.fraction-to-recurring-decimal.js (100%) rename 1660-correct-a-binary-tree.js => 1660.correct-a-binary-tree.js (100%) rename 1662-check-if-two-string-arrays-are-equivalent.js => 1662.check-if-two-string-arrays-are-equivalent.js (100%) rename 1663-smallest-string-with-a-given-numeric-value.js => 1663.smallest-string-with-a-given-numeric-value.js (100%) rename 1664-ways-to-make-a-fair-array.js => 1664.ways-to-make-a-fair-array.js (100%) rename 1665-minimum-initial-energy-to-finish-tasks.js => 1665.minimum-initial-energy-to-finish-tasks.js (100%) rename 1666-change-the-root-of-a-binary-tree.js => 1666.change-the-root-of-a-binary-tree.js (100%) rename 1668-maximum-repeating-substring.js => 1668.maximum-repeating-substring.js (100%) rename 1669-merge-in-between-linked-lists.js => 1669.merge-in-between-linked-lists.js (100%) rename 167-two-sum-II-input-array-is-sorted.js => 167.two-sum-II-input-array-is-sorted.js (95%) mode change 100755 => 100644 rename 1670-design-front-middle-back-queue.js => 1670.design-front-middle-back-queue.js (100%) rename 1671-minimum-number-of-removals-to-make-mountain-array.js => 1671.minimum-number-of-removals-to-make-mountain-array.js (100%) rename 1672-richest-customer-wealth.js => 1672.richest-customer-wealth.js (100%) rename 1673-find-the-most-competitive-subsequence.js => 1673.find-the-most-competitive-subsequence.js (100%) rename 1674-minimum-moves-to-make-array-complementary.js => 1674.minimum-moves-to-make-array-complementary.js (100%) rename 1675-minimize-deviation-in-array.js => 1675.minimize-deviation-in-array.js (100%) rename 1676-lowest-common-ancestor-of-a-binary-tree-iv.js => 1676.lowest-common-ancestor-of-a-binary-tree-iv.js (100%) rename 1678-goal-parser-interpretation.js => 1678.goal-parser-interpretation.js (100%) rename 1679-max-number-of-k-sum-pairs.js => 1679.max-number-of-k-sum-pairs.js (100%) rename 168-excel-sheet-column-title.js => 168.excel-sheet-column-title.js (100%) rename 1680-concatenation-of-consecutive-binary-numbers.js => 1680.concatenation-of-consecutive-binary-numbers.js (100%) rename 1681-minimum-incompatibility.js => 1681.minimum-incompatibility.js (100%) rename 1684-count-the-number-of-consistent-strings.js => 1684.count-the-number-of-consistent-strings.js (100%) rename 1685-sum-of-absolute-differences-in-a-sorted-array.js => 1685.sum-of-absolute-differences-in-a-sorted-array.js (100%) rename 1686-stone-game-vi.js => 1686.stone-game-vi.js (100%) rename 1687-delivering-boxes-from-storage-to-ports.js => 1687.delivering-boxes-from-storage-to-ports.js (100%) rename 1688-count-of-matches-in-tournament.js => 1688.count-of-matches-in-tournament.js (100%) rename 1689-partitioning-into-minimum-number-of-deci-binary-numbers.js => 1689.partitioning-into-minimum-number-of-deci-binary-numbers.js (100%) rename 169-majority-element.js => 169.majority-element.js (95%) mode change 100755 => 100644 rename 1690-stone-game-vii.js => 1690.stone-game-vii.js (100%) rename 1691-maximum-height-by-stacking-cuboids.js => 1691.maximum-height-by-stacking-cuboids.js (100%) rename 1694-reformat-phone-number.js => 1694.reformat-phone-number.js (100%) rename 1695-maximum-erasure-value.js => 1695.maximum-erasure-value.js (100%) rename 1696-jump-game-vi.js => 1696.jump-game-vi.js (100%) rename 1697-checking-existence-of-edge-length-limited-paths.js => 1697.checking-existence-of-edge-length-limited-paths.js (100%) rename 1698-number-of-distinct-substrings-in-a-string.js => 1698.number-of-distinct-substrings-in-a-string.js (100%) rename 17-letter-combinations-of-a-phone-number.js => 17.letter-combinations-of-a-phone-number.js (95%) mode change 100755 => 100644 rename 170-two-sum-iii-data-structure-design.js => 170.two-sum-iii-data-structure-design.js (100%) rename 1700-number-of-students-unable-to-eat-lunch.js => 1700.number-of-students-unable-to-eat-lunch.js (100%) rename 1701-average-waiting-time.js => 1701.average-waiting-time.js (100%) rename 1703-maximum-binary-string-after-change.js => 1703.maximum-binary-string-after-change.js (100%) rename 1704-determine-if-string-halves-are-alike.js => 1704.determine-if-string-halves-are-alike.js (100%) rename 1704-minimum-adjacent-swaps-for-k-consecutive-ones.js => 1704.minimum-adjacent-swaps-for-k-consecutive-ones.js (100%) rename 1705-maximum-number-of-eaten-apples.js => 1705.maximum-number-of-eaten-apples.js (100%) rename 1706-where-will-the-ball-fall.js => 1706.where-will-the-ball-fall.js (100%) rename 1707-maximum-xor-with-an-element-from-array.js => 1707.maximum-xor-with-an-element-from-array.js (100%) rename 1708-largest-subarray-length-k.js => 1708.largest-subarray-length-k.js (100%) rename 171-excel-sheet-column-number.js => 171.excel-sheet-column-number.js (95%) mode change 100755 => 100644 rename 1710-maximum-units-on-a-truck.js => 1710.maximum-units-on-a-truck.js (100%) rename 1711-count-good-meals.js => 1711.count-good-meals.js (100%) rename 1712-ways-to-split-array-into-three-subarrays.js => 1712.ways-to-split-array-into-three-subarrays.js (100%) rename 1713-minimum-operations-to-make-a-subsequence.js => 1713.minimum-operations-to-make-a-subsequence.js (100%) rename 1714-sum-of-special-evenly-spaced-elements-in-array.js => 1714.sum-of-special-evenly-spaced-elements-in-array.js (100%) rename 1716-calculate-money-in-leetcode-bank.js => 1716.calculate-money-in-leetcode-bank.js (100%) rename 1717-maximum-score-from-removing-substrings.js => 1717.maximum-score-from-removing-substrings.js (100%) rename 1718-construct-the-lexicographically-largest-valid-sequence.js => 1718.construct-the-lexicographically-largest-valid-sequence.js (100%) rename 1719-number-of-ways-to-reconstruct-a-tree.js => 1719.number-of-ways-to-reconstruct-a-tree.js (100%) rename 172-factorial-trailing-zeroes.js => 172.factorial-trailing-zeroes.js (100%) rename 1720-decode-xored-array.js => 1720.decode-xored-array.js (100%) rename 1721-swapping-nodes-in-a-linked-list.js => 1721.swapping-nodes-in-a-linked-list.js (100%) rename 1722-minimize-hamming-distance-after-swap-operations.js => 1722.minimize-hamming-distance-after-swap-operations.js (100%) rename 1723-find-minimum-time-to-finish-all-jobs.js => 1723.find-minimum-time-to-finish-all-jobs.js (100%) rename 1724-checking-existence-of-edge-length-limited-paths-ii.js => 1724.checking-existence-of-edge-length-limited-paths-ii.js (100%) rename 1725-number-of-rectangles-that-can-form-the-largest-square.js => 1725.number-of-rectangles-that-can-form-the-largest-square.js (100%) rename 1726-tuple-with-same-product.js => 1726.tuple-with-same-product.js (100%) rename 1727-largest-submatrix-with-rearrangements.js => 1727.largest-submatrix-with-rearrangements.js (100%) rename 1728-cat-and-mouse-ii.js => 1728.cat-and-mouse-ii.js (100%) rename 173-binary-search-tree-iterator.js => 173.binary-search-tree-iterator.js (100%) rename 1732-find-the-highest-altitude.js => 1732.find-the-highest-altitude.js (100%) rename 1733-minimum-number-of-people-to-teach.js => 1733.minimum-number-of-people-to-teach.js (100%) rename 1734-decode-xored-permutation.js => 1734.decode-xored-permutation.js (100%) rename 1735-count-ways-to-make-array-with-product.js => 1735.count-ways-to-make-array-with-product.js (100%) rename 1736-latest-time-by-replacing-hidden-digits.js => 1736.latest-time-by-replacing-hidden-digits.js (100%) rename 1737-change-minimum-characters-to-satisfy-one-of-three-conditions.js => 1737.change-minimum-characters-to-satisfy-one-of-three-conditions.js (100%) rename 1738-find-kth-largest-xor-coordinate-value.js => 1738.find-kth-largest-xor-coordinate-value.js (100%) rename 1739-building-boxes.js => 1739.building-boxes.js (100%) rename 174-dungeon-game.js => 174.dungeon-game.js (100%) rename 1740-find-distance-in-a-binary-tree.js => 1740.find-distance-in-a-binary-tree.js (100%) rename 1742-maximum-number-of-balls-in-a-box.js => 1742.maximum-number-of-balls-in-a-box.js (100%) rename 1743-restore-the-array-from-adjacent-pairs.js => 1743.restore-the-array-from-adjacent-pairs.js (100%) rename 1744-can-you-eat-your-favorite-candy-on-your-favorite-day.js => 1744.can-you-eat-your-favorite-candy-on-your-favorite-day.js (100%) rename 1745-palindrome-partitioning-iv.js => 1745.palindrome-partitioning-iv.js (100%) rename 1746-maximum-subarray-sum-after-one-operation.js => 1746.maximum-subarray-sum-after-one-operation.js (100%) rename 1748-sum-of-unique-elements.js => 1748.sum-of-unique-elements.js (100%) rename 1749-maximum-absolute-sum-of-any-subarray.js => 1749.maximum-absolute-sum-of-any-subarray.js (100%) rename 1750-minimum-length-of-string-after-deleting-similar-ends.js => 1750.minimum-length-of-string-after-deleting-similar-ends.js (100%) rename 1751-maximum-number-of-events-that-can-be-attended-ii.js => 1751.maximum-number-of-events-that-can-be-attended-ii.js (100%) rename 1752-check-if-array-is-sorted-and-rotated.js => 1752.check-if-array-is-sorted-and-rotated.js (100%) rename 1753-maximum-score-from-removing-stones.js => 1753.maximum-score-from-removing-stones.js (100%) rename 1754-largest-merge-of-two-strings.js => 1754.largest-merge-of-two-strings.js (100%) rename 1755-closest-subsequence-sum.js => 1755.closest-subsequence-sum.js (100%) rename 1758-minimum-changes-to-make-alternating-binary-string.js => 1758.minimum-changes-to-make-alternating-binary-string.js (100%) rename 1759-count-number-of-homogenous-substrings.js => 1759.count-number-of-homogenous-substrings.js (100%) rename 1760-minimum-limit-of-balls-in-a-bag.js => 1760.minimum-limit-of-balls-in-a-bag.js (100%) rename 1761-minimum-degree-of-a-connected-trio-in-a-graph.js => 1761.minimum-degree-of-a-connected-trio-in-a-graph.js (100%) rename 1762-buildings-with-an-ocean-view.js => 1762.buildings-with-an-ocean-view.js (100%) rename 1763-longest-nice-substring.js => 1763.longest-nice-substring.js (100%) rename 1764-form-array-by-concatenating-subarrays-of-another-array.js => 1764.form-array-by-concatenating-subarrays-of-another-array.js (100%) rename 1765-map-of-highest-peak.js => 1765.map-of-highest-peak.js (100%) rename 1766-tree-of-coprimes.js => 1766.tree-of-coprimes.js (100%) rename 1768-merge-strings-alternately.js => 1768.merge-strings-alternately.js (100%) rename 1769-minimum-number-of-operations-to-move-all-balls-to-each-box.js => 1769.minimum-number-of-operations-to-move-all-balls-to-each-box.js (100%) rename 1770-maximum-score-from-performing-multiplication-operations.js => 1770.maximum-score-from-performing-multiplication-operations.js (100%) rename 1771-maximize-palindrome-length-from-subsequences.js => 1771.maximize-palindrome-length-from-subsequences.js (100%) rename 1773-count-items-matching-a-rule.js => 1773.count-items-matching-a-rule.js (100%) rename 1774-closest-dessert-cost.js => 1774.closest-dessert-cost.js (100%) rename 1775-equal-sum-arrays-with-minimum-number-of-operations.js => 1775.equal-sum-arrays-with-minimum-number-of-operations.js (100%) rename 1776-car-fleet-ii.js => 1776.car-fleet-ii.js (100%) rename 1779-find-nearest-point-that-has-the-same-x-or-y-coordinate.js => 1779.find-nearest-point-that-has-the-same-x-or-y-coordinate.js (100%) rename 1780-check-if-number-is-a-sum-of-powers-of-three.js => 1780.check-if-number-is-a-sum-of-powers-of-three.js (100%) rename 1781-sum-of-beauty-of-all-substrings.js => 1781.sum-of-beauty-of-all-substrings.js (100%) rename 1784-check-if-binary-string-has-at-most-one-segment-of-ones.js => 1784.check-if-binary-string-has-at-most-one-segment-of-ones.js (100%) rename 1785-minimum-elements-to-add-to-form-a-given-sum.js => 1785.minimum-elements-to-add-to-form-a-given-sum.js (100%) rename 1786-number-of-restricted-paths-from-first-to-last-node.js => 1786.number-of-restricted-paths-from-first-to-last-node.js (100%) rename 1787-make-the-xor-of-all-segments-equal-to-zero.js => 1787.make-the-xor-of-all-segments-equal-to-zero.js (100%) rename 179-largest-number.js => 179.largest-number.js (100%) rename 1790-check-if-one-string-swap-can-make-strings-equal.js => 1790.check-if-one-string-swap-can-make-strings-equal.js (100%) rename 1791-find-center-of-star-graph.js => 1791.find-center-of-star-graph.js (100%) rename 1792-maximum-average-pass-ratio.js => 1792.maximum-average-pass-ratio.js (100%) rename 1793-maximum-score-of-a-good-subarray.js => 1793.maximum-score-of-a-good-subarray.js (100%) rename 1798-maximum-number-of-consecutive-values-you-can-make.js => 1798.maximum-number-of-consecutive-values-you-can-make.js (100%) rename 1799-maximize-score-after-n-operations.js => 1799.maximize-score-after-n-operations.js (100%) rename 18-4sum.js => 18.4sum.js (100%) rename 1800-maximum-ascending-subarray-sum.js => 1800.maximum-ascending-subarray-sum.js (100%) rename 1801-number-of-orders-in-the-backlog.js => 1801.number-of-orders-in-the-backlog.js (100%) rename 1802-maximum-value-at-a-given-index-in-a-bounded-array.js => 1802.maximum-value-at-a-given-index-in-a-bounded-array.js (100%) rename 1803-count-pairs-with-xor-in-a-range.js => 1803.count-pairs-with-xor-in-a-range.js (100%) rename 1804-implement-trie-ii-prefix-tree.js => 1804.implement-trie-ii-prefix-tree.js (100%) rename 1805-number-of-different-integers-in-a-string.js => 1805.number-of-different-integers-in-a-string.js (100%) rename 1806-minimum-number-of-operations-to-reinitialize-a-permutation.js => 1806.minimum-number-of-operations-to-reinitialize-a-permutation.js (100%) rename 1807-evaluate-the-bracket-pairs-of-a-string.js => 1807.evaluate-the-bracket-pairs-of-a-string.js (100%) rename 1808-maximize-number-of-nice-divisors.js => 1808.maximize-number-of-nice-divisors.js (100%) rename 1815-maximum-number-of-groups-getting-fresh-donuts.js => 1815.maximum-number-of-groups-getting-fresh-donuts.js (100%) rename 1816-truncate-sentence.js => 1816.truncate-sentence.js (100%) rename 1817-finding-the-users-active-minutes.js => 1817.finding-the-users-active-minutes.js (100%) rename 1818-minimum-absolute-sum-difference.js => 1818.minimum-absolute-sum-difference.js (100%) rename 1819-number-of-different-subsequences-gcds.js => 1819.number-of-different-subsequences-gcds.js (100%) rename 1822-sign-of-the-product-of-an-array.js => 1822.sign-of-the-product-of-an-array.js (100%) rename 1823-find-the-winner-of-the-circular-game.js => 1823.find-the-winner-of-the-circular-game.js (100%) rename 1824-minimum-sideway-jumps.js => 1824.minimum-sideway-jumps.js (100%) rename 1825-finding-mk-average.js => 1825.finding-mk-average.js (100%) rename 1827-minimum-operations-to-make-the-array-increasing.js => 1827.minimum-operations-to-make-the-array-increasing.js (100%) rename 1828-queries-on-number-of-points-inside-a-circle.js => 1828.queries-on-number-of-points-inside-a-circle.js (100%) rename 1829-maximum-xor-for-each-query.js => 1829.maximum-xor-for-each-query.js (100%) rename 1830-minimum-number-of-operations-to-make-string-sorted.js => 1830.minimum-number-of-operations-to-make-string-sorted.js (100%) rename 1832-check-if-the-sentence-is-pangram.js => 1832.check-if-the-sentence-is-pangram.js (100%) rename 1833-maximum-ice-cream-bars.js => 1833.maximum-ice-cream-bars.js (100%) rename 1834-single-threaded-cpu.js => 1834.single-threaded-cpu.js (100%) rename 1835-find-xor-sum-of-all-pairs-bitwise-and.js => 1835.find-xor-sum-of-all-pairs-bitwise-and.js (100%) rename 1836-remove-duplicates-from-an-unsorted-linked-list.js => 1836.remove-duplicates-from-an-unsorted-linked-list.js (100%) rename 1837-sum-of-digits-in-base-k.js => 1837.sum-of-digits-in-base-k.js (100%) rename 1838-frequency-of-the-most-frequent-element.js => 1838.frequency-of-the-most-frequent-element.js (100%) rename 1839-longest-substring-of-all-vowels-in-order.js => 1839.longest-substring-of-all-vowels-in-order.js (100%) rename 1840-maximum-building-height.js => 1840.maximum-building-height.js (100%) rename 1844-replace-all-digits-with-characters.js => 1844.replace-all-digits-with-characters.js (100%) rename 1846-maximum-element-after-decreasing-and-rearranging.js => 1846.maximum-element-after-decreasing-and-rearranging.js (100%) rename 1847-closest-room.js => 1847.closest-room.js (100%) rename 1848-minimum-distance-to-the-target-element.js => 1848.minimum-distance-to-the-target-element.js (100%) rename 1849-splitting-a-string-into-descending-consecutive-values.js => 1849.splitting-a-string-into-descending-consecutive-values.js (100%) rename 1850-minimum-adjacent-swaps-to-reach-the-kth-smallest-number.js => 1850.minimum-adjacent-swaps-to-reach-the-kth-smallest-number.js (100%) rename 1851-minimum-interval-to-include-each-query.js => 1851.minimum-interval-to-include-each-query.js (100%) rename 1854-maximum-population-year.js => 1854.maximum-population-year.js (100%) rename 1855-maximum-distance-between-a-pair-of-values.js => 1855.maximum-distance-between-a-pair-of-values.js (100%) rename 1856-maximum-subarray-min-product.js => 1856.maximum-subarray-min-product.js (100%) rename 1857-largest-color-value-in-a-directed-graph.js => 1857.largest-color-value-in-a-directed-graph.js (100%) rename 1859-sorting-the-sentence.js => 1859.sorting-the-sentence.js (100%) rename 186-reverse-words-in-a-string-ii.js => 186.reverse-words-in-a-string-ii.js (100%) rename 1860-incremental-memory-leak.js => 1860.incremental-memory-leak.js (100%) rename 1861-rotating-the-box.js => 1861.rotating-the-box.js (100%) rename 1862-sum-of-floored-pairs.js => 1862.sum-of-floored-pairs.js (100%) rename 1863-sum-of-all-subset-xor-totals.js => 1863.sum-of-all-subset-xor-totals.js (100%) rename 1864-minimum-number-of-swaps-to-make-the-binary-string-alternating.js => 1864.minimum-number-of-swaps-to-make-the-binary-string-alternating.js (100%) rename 1865-finding-pairs-with-a-certain-sum.js => 1865.finding-pairs-with-a-certain-sum.js (100%) rename 1866-number-of-ways-to-rearrange-sticks-with-k-sticks-visible.js => 1866.number-of-ways-to-rearrange-sticks-with-k-sticks-visible.js (100%) rename 1869-longer-contiguous-segments-of-ones-than-zeros.js => 1869.longer-contiguous-segments-of-ones-than-zeros.js (100%) rename 187-repeated-dna-sequences.js => 187.repeated-dna-sequences.js (100%) rename 1870-minimum-speed-to-arrive-on-time.js => 1870.minimum-speed-to-arrive-on-time.js (100%) rename 1871-jump-game-vii.js => 1871.jump-game-vii.js (100%) rename 1872-stone-game-viii.js => 1872.stone-game-viii.js (100%) rename 1874-minimize-product-sum-of-two-arrays.js => 1874.minimize-product-sum-of-two-arrays.js (100%) rename 1876-substrings-of-size-three-with-distinct-characters.js => 1876.substrings-of-size-three-with-distinct-characters.js (100%) rename 1877-minimize-maximum-pair-sum-in-array.js => 1877.minimize-maximum-pair-sum-in-array.js (100%) rename 1878-get-biggest-three-rhombus-sums-in-a-grid.js => 1878.get-biggest-three-rhombus-sums-in-a-grid.js (100%) rename 1879-minimum-xor-sum-of-two-arrays.js => 1879.minimum-xor-sum-of-two-arrays.js (100%) rename 188-best-time-to-buy-and-sell-stock-iv.js => 188.best-time-to-buy-and-sell-stock-iv.js (100%) rename 1880-check-if-word-equals-summation-of-two-words.js => 1880.check-if-word-equals-summation-of-two-words.js (100%) rename 1881-maximum-value-after-insertion.js => 1881.maximum-value-after-insertion.js (100%) rename 1882-process-tasks-using-servers.js => 1882.process-tasks-using-servers.js (100%) rename 1883-minimum-skips-to-arrive-at-meeting-on-time.js => 1883.minimum-skips-to-arrive-at-meeting-on-time.js (100%) rename 1884-egg-drop-with-2-eggs-and-n-floors.js => 1884.egg-drop-with-2-eggs-and-n-floors.js (100%) rename 1885-count-pairs-in-two-arrays.js => 1885.count-pairs-in-two-arrays.js (100%) rename 1886-determine-whether-matrix-can-be-obtained-by-rotation.js => 1886.determine-whether-matrix-can-be-obtained-by-rotation.js (100%) rename 1887-reduction-operations-to-make-the-array-elements-equal.js => 1887.reduction-operations-to-make-the-array-elements-equal.js (100%) rename 1888-minimum-number-of-flips-to-make-the-binary-string-alternating.js => 1888.minimum-number-of-flips-to-make-the-binary-string-alternating.js (100%) rename 1889-minimum-space-wasted-from-packaging.js => 1889.minimum-space-wasted-from-packaging.js (100%) rename 189-rotate-array.js => 189.rotate-array.js (100%) rename 1891-cutting-ribbons.js => 1891.cutting-ribbons.js (100%) rename 1893-check-if-all-the-integers-in-a-range-are-covered.js => 1893.check-if-all-the-integers-in-a-range-are-covered.js (100%) rename 1896-minimum-cost-to-change-the-final-value-of-expression.js => 1896.minimum-cost-to-change-the-final-value-of-expression.js (100%) rename 1897-redistribute-characters-to-make-all-strings-equal.js => 1897.redistribute-characters-to-make-all-strings-equal.js (100%) rename 1898-maximum-number-of-removable-characters.js => 1898.maximum-number-of-removable-characters.js (100%) rename 1899-merge-triplets-to-form-target-triplet.js => 1899.merge-triplets-to-form-target-triplet.js (100%) rename 19-remove-nth-node-from-end-of-list.js => 19.remove-nth-node-from-end-of-list.js (95%) mode change 100755 => 100644 rename 190-reverse-bits.js => 190.reverse-bits.js (100%) rename 1900-the-earliest-and-latest-rounds-where-players-compete.js => 1900.the-earliest-and-latest-rounds-where-players-compete.js (100%) rename 1901-find-a-peak-element-ii.js => 1901.find-a-peak-element-ii.js (100%) rename 1903-largest-odd-number-in-string.js => 1903.largest-odd-number-in-string.js (100%) rename 1904-the-number-of-full-rounds-you-have-played.js => 1904.the-number-of-full-rounds-you-have-played.js (100%) rename 1905-count-sub-islands.js => 1905.count-sub-islands.js (100%) rename 1906-minimum-absolute-difference-queries.js => 1906.minimum-absolute-difference-queries.js (100%) rename 1909-remove-one-element-to-make-the-array-strictly-increasing.js => 1909.remove-one-element-to-make-the-array-strictly-increasing.js (100%) rename 191-number-of-1-bits.js => 191.number-of-1-bits.js (100%) rename 1910-remove-all-occurrences-of-a-substring.js => 1910.remove-all-occurrences-of-a-substring.js (100%) rename 1911-maximum-alternating-subsequence-sum.js => 1911.maximum-alternating-subsequence-sum.js (100%) rename 1913-maximum-product-difference-between-two-pairs.js => 1913.maximum-product-difference-between-two-pairs.js (100%) rename 1914-cyclically-rotating-a-gridsubmissions.js => 1914.cyclically-rotating-a-gridsubmissions.js (100%) rename 1915-number-of-wonderful-substrings.js => 1915.number-of-wonderful-substrings.js (100%) rename 1916-count-ways-to-build-rooms-in-an-ant-colony.js => 1916.count-ways-to-build-rooms-in-an-ant-colony.js (100%) rename 1918-kth-smallest-subarray-sum.js => 1918.kth-smallest-subarray-sum.js (100%) rename 1920-build-array-from-permutation.js => 1920.build-array-from-permutation.js (100%) rename 1921-eliminate-maximum-number-of-monsters.js => 1921.eliminate-maximum-number-of-monsters.js (100%) rename 1922-count-good-numbers.js => 1922.count-good-numbers.js (100%) rename 1923-longest-common-subpath.js => 1923.longest-common-subpath.js (100%) rename 1925-count-square-sum-triples.js => 1925.count-square-sum-triples.js (100%) rename 1928-minimum-cost-to-reach-destination-in-time.js => 1928.minimum-cost-to-reach-destination-in-time.js (100%) rename 1929-concatenation-of-array.js => 1929.concatenation-of-array.js (100%) rename 1930-unique-length-3-palindromic-subsequences.js => 1930.unique-length-3-palindromic-subsequences.js (100%) rename 1931-painting-a-grid-with-three-different-colors.js => 1931.painting-a-grid-with-three-different-colors.js (100%) rename 1932-merge-bsts-to-create-single-bst.js => 1932.merge-bsts-to-create-single-bst.js (100%) rename 1935-maximum-number-of-words-you-can-type.js => 1935.maximum-number-of-words-you-can-type.js (100%) rename 1936-add-minimum-number-of-rungs.js => 1936.add-minimum-number-of-rungs.js (100%) rename 1937-maximum-number-of-points-with-cost.js => 1937.maximum-number-of-points-with-cost.js (100%) rename 1938-maximum-genetic-difference-query.js => 1938.maximum-genetic-difference-query.js (100%) rename 1941-check-if-all-characters-have-equal-number-of-occurrences.js => 1941.check-if-all-characters-have-equal-number-of-occurrences.js (100%) rename 1942-the-number-of-the-smallest-unoccupied-chair.js => 1942.the-number-of-the-smallest-unoccupied-chair.js (100%) rename 1943-describe-the-painting.js => 1943.describe-the-painting.js (100%) rename 1944-number-of-visible-people-in-a-queue.js => 1944.number-of-visible-people-in-a-queue.js (100%) rename 1945-sum-of-digits-of-string-after-convert.js => 1945.sum-of-digits-of-string-after-convert.js (100%) rename 1946-largest-number-after-mutating-substring.js => 1946.largest-number-after-mutating-substring.js (100%) rename 1947-maximum-compatibility-score-sum.js => 1947.maximum-compatibility-score-sum.js (100%) rename 1948-delete-duplicate-folders-in-system.js => 1948.delete-duplicate-folders-in-system.js (100%) rename 1952-three-divisors.js => 1952.three-divisors.js (100%) rename 1953-maximum-number-of-weeks-for-which-you-can-work.js => 1953.maximum-number-of-weeks-for-which-you-can-work.js (100%) rename 1954-minimum-garden-perimeter-to-collect-enough-apples.js => 1954.minimum-garden-perimeter-to-collect-enough-apples.js (100%) rename 1955-count-number-of-special-subsequences.js => 1955.count-number-of-special-subsequences.js (100%) rename 1959-minimum-total-space-wasted-with-k-resizing-operations.js => 1959.minimum-total-space-wasted-with-k-resizing-operations.js (100%) rename 1960-maximum-product-of-the-length-of-two-palindromic-substrings.js => 1960.maximum-product-of-the-length-of-two-palindromic-substrings.js (100%) rename 1961-check-if-string-is-a-prefix-of-array.js => 1961.check-if-string-is-a-prefix-of-array.js (100%) rename 1962-remove-stones-to-minimize-the-total.js => 1962.remove-stones-to-minimize-the-total.js (100%) rename 1963-minimum-number-of-swaps-to-make-the-string-balanced.js => 1963.minimum-number-of-swaps-to-make-the-string-balanced.js (100%) rename 1964-find-the-longest-valid-obstacle-course-at-each-position.js => 1964.find-the-longest-valid-obstacle-course-at-each-position.js (100%) rename 1969-minimum-non-zero-product-of-the-array-elements.js => 1969.minimum-non-zero-product-of-the-array-elements.js (100%) rename 1970-last-day-where-you-can-still-cross.js => 1970.last-day-where-you-can-still-cross.js (100%) rename 1971-find-if-path-exists-in-graph.js => 1971.find-if-path-exists-in-graph.js (100%) rename 1976-number-of-ways-to-arrive-at-destination.js => 1976.number-of-ways-to-arrive-at-destination.js (100%) rename 1977-number-of-ways-to-separate-numbers.js => 1977.number-of-ways-to-separate-numbers.js (100%) rename 1979-find-greatest-common-divisor-of-array.js => 1979.find-greatest-common-divisor-of-array.js (100%) rename 198-house-robber.js => 198.house-robber.js (100%) rename 1980-find-unique-binary-string.js => 1980.find-unique-binary-string.js (100%) rename 1981-minimize-the-difference-between-target-and-chosen-elements.js => 1981.minimize-the-difference-between-target-and-chosen-elements.js (100%) rename 1982-find-array-given-subset-sums.js => 1982.find-array-given-subset-sums.js (100%) rename 1983-widest-pair-of-indices-with-equal-range-sum.js => 1983.widest-pair-of-indices-with-equal-range-sum.js (100%) rename 1984-minimum-difference-between-highest-and-lowest-of-k-scores.js => 1984.minimum-difference-between-highest-and-lowest-of-k-scores.js (100%) rename 1985-find-the-kth-largest-integer-in-the-array.js => 1985.find-the-kth-largest-integer-in-the-array.js (100%) rename 1986-minimum-number-of-work-sessions-to-finish-the-tasks.js => 1986.minimum-number-of-work-sessions-to-finish-the-tasks.js (100%) rename 1987-number-of-unique-good-subsequences.js => 1987.number-of-unique-good-subsequences.js (100%) rename 199-binary-tree-right-side-view.js => 199.binary-tree-right-side-view.js (100%) rename 1991-find-the-middle-index-in-array.js => 1991.find-the-middle-index-in-array.js (100%) rename 1994-the-number-of-good-subsets.js => 1994.the-number-of-good-subsets.js (100%) rename 1995-count-special-quadruplets.js => 1995.count-special-quadruplets.js (100%) rename 1996-the-number-of-weak-characters-in-the-game.js => 1996.the-number-of-weak-characters-in-the-game.js (100%) rename 1997-first-day-where-you-have-been-in-all-the-rooms.js => 1997.first-day-where-you-have-been-in-all-the-rooms.js (100%) rename 1998-gcd-sort-of-an-array.js => 1998.gcd-sort-of-an-array.js (100%) rename 2-add-two-numbers.js => 2.add-two-numbers.js (95%) mode change 100755 => 100644 rename 20-valid-parentheses.js => 20.valid-parentheses.js (95%) mode change 100755 => 100644 rename 200-number-of-islands.js => 200.number-of-islands.js (100%) rename 2000-reverse-prefix-of-word.js => 2000.reverse-prefix-of-word.js (100%) rename 2001-number-of-pairs-of-interchangeable-rectangles.js => 2001.number-of-pairs-of-interchangeable-rectangles.js (100%) rename 2002-maximum-product-of-the-length-of-two-palindromic-subsequences.js => 2002.maximum-product-of-the-length-of-two-palindromic-subsequences.js (100%) rename 2003-smallest-missing-genetic-value-in-each-subtree.js => 2003.smallest-missing-genetic-value-in-each-subtree.js (100%) rename 2006-count-number-of-pairs-with-absolute-difference-k.js => 2006.count-number-of-pairs-with-absolute-difference-k.js (100%) rename 2007-find-original-array-from-doubled-array.js => 2007.find-original-array-from-doubled-array.js (100%) rename 2008-maximum-earnings-from-taxi.js => 2008.maximum-earnings-from-taxi.js (100%) rename 2009-minimum-number-of-operations-to-make-array-continuous.js => 2009.minimum-number-of-operations-to-make-array-continuous.js (100%) rename 201-bitwise-and-of-numbers-range.js => 201.bitwise-and-of-numbers-range.js (100%) rename 2011-final-value-of-variable-after-performing-operations.js => 2011.final-value-of-variable-after-performing-operations.js (100%) rename 2012-sum-of-beauty-in-the-array.js => 2012.sum-of-beauty-in-the-array.js (100%) rename 2013-detect-squares.js => 2013.detect-squares.js (100%) rename 2014-longest-subsequence-repeated-k-times.js => 2014.longest-subsequence-repeated-k-times.js (100%) rename 2015-average-height-of-buildings-in-each-segment.js => 2015.average-height-of-buildings-in-each-segment.js (100%) rename 2018-check-if-word-can-be-placed-in-crossword.js => 2018.check-if-word-can-be-placed-in-crossword.js (100%) rename 2019-the-score-of-students-solving-math-expression.js => 2019.the-score-of-students-solving-math-expression.js (100%) rename 202-happy-number.js => 202.happy-number.js (94%) mode change 100755 => 100644 rename 2023-number-of-pairs-of-strings-with-concatenation-equal-to-target.js => 2023.number-of-pairs-of-strings-with-concatenation-equal-to-target.js (100%) rename 2024-maximize-the-confusion-of-an-exam.js => 2024.maximize-the-confusion-of-an-exam.js (100%) rename 2025-maximum-number-of-ways-to-partition-an-array.js => 2025.maximum-number-of-ways-to-partition-an-array.js (100%) rename 2029-stone-game-ix.js => 2029.stone-game-ix.js (100%) rename 203-remove-linked-list-elements.js => 203.remove-linked-list-elements.js (100%) rename 2030-smallest-k-length-subsequence-with-occurrences-of-a-letter.js => 2030.smallest-k-length-subsequence-with-occurrences-of-a-letter.js (100%) rename 2031-count-subarrays-with-more-ones-than-zeros.js => 2031.count-subarrays-with-more-ones-than-zeros.js (100%) rename 2032-two-out-of-three.js => 2032.two-out-of-three.js (100%) rename 2033-minimum-operations-to-make-a-uni-value-grid.js => 2033.minimum-operations-to-make-a-uni-value-grid.js (100%) rename 2034-stock-price-fluctuation.js => 2034.stock-price-fluctuation.js (100%) rename 2035-partition-array-into-two-arrays-to-minimize-sum-difference.js => 2035.partition-array-into-two-arrays-to-minimize-sum-difference.js (100%) rename 2037-minimum-number-of-moves-to-seat-everyone.js => 2037.minimum-number-of-moves-to-seat-everyone.js (100%) rename 2038-remove-colored-pieces-if-both-neighbors-are-the-same-color.js => 2038.remove-colored-pieces-if-both-neighbors-are-the-same-color.js (100%) rename 204-count-primes.js => 204.count-primes.js (93%) mode change 100755 => 100644 rename 2040-kth-smallest-product-of-two-sorted-arrays.js => 2040.kth-smallest-product-of-two-sorted-arrays.js (100%) rename 2042-check-if-numbers-are-ascending-in-a-sentence.js => 2042.check-if-numbers-are-ascending-in-a-sentence.js (100%) rename 2043-simple-bank-system.js => 2043.simple-bank-system.js (100%) rename 2044-count-number-of-maximum-bitwise-or-subsets.js => 2044.count-number-of-maximum-bitwise-or-subsets.js (100%) rename 2045-second-minimum-time-to-reach-destination.js => 2045.second-minimum-time-to-reach-destination.js (100%) rename 2046-sort-linked-list-already-sorted-using-absolute-values.js => 2046.sort-linked-list-already-sorted-using-absolute-values.js (100%) rename 2047-number-of-valid-words-in-a-sentence.js => 2047.number-of-valid-words-in-a-sentence.js (100%) rename 2048-next-greater-numerically-balanced-number.js => 2048.next-greater-numerically-balanced-number.js (100%) rename 2049-count-nodes-with-the-highest-score.js => 2049.count-nodes-with-the-highest-score.js (100%) rename 205-isomorphic-strings.js => 205.isomorphic-strings.js (100%) rename 2050-parallel-courses-iii.js => 2050.parallel-courses-iii.js (100%) rename 2053-kth-distinct-string-in-an-array.js => 2053.kth-distinct-string-in-an-array.js (100%) rename 2054-two-best-non-overlapping-events.js => 2054.two-best-non-overlapping-events.js (100%) rename 2055-plates-between-candles.js => 2055.plates-between-candles.js (100%) rename 2057-smallest-index-with-equal-value.js => 2057.smallest-index-with-equal-value.js (100%) rename 2058-find-the-minimum-and-maximum-number-of-nodes-between-critical-points.js => 2058.find-the-minimum-and-maximum-number-of-nodes-between-critical-points.js (100%) rename 2059-minimum-operations-to-convert-number.js => 2059.minimum-operations-to-convert-number.js (100%) rename 206-reverse-linked-list.js => 206.reverse-linked-list.js (95%) mode change 100755 => 100644 rename 2060-check-if-an-original-string-exists-given-two-encoded-strings.js => 2060.check-if-an-original-string-exists-given-two-encoded-strings.js (100%) rename 2062-count-vowel-substrings-of-a-string.js => 2062.count-vowel-substrings-of-a-string.js (100%) rename 2063-vowels-of-all-substrings.js => 2063.vowels-of-all-substrings.js (100%) rename 2064-minimized-maximum-of-products-distributed-to-any-store.js => 2064.minimized-maximum-of-products-distributed-to-any-store.js (100%) rename 2065-maximum-path-quality-of-a-graph.js => 2065.maximum-path-quality-of-a-graph.js (100%) rename 2068-check-whether-two-strings-are-almost-equivalent.js => 2068.check-whether-two-strings-are-almost-equivalent.js (100%) rename 2069-walking-robot-simulation-ii.js => 2069.walking-robot-simulation-ii.js (100%) rename 207-course-schedule.js => 207.course-schedule.js (100%) rename 2071-maximum-number-of-tasks-you-can-assign.js => 2071.maximum-number-of-tasks-you-can-assign.js (100%) rename 2073-time-needed-to-buy-tickets.js => 2073.time-needed-to-buy-tickets.js (100%) rename 2074-reverse-nodes-in-even-length-groups.js => 2074.reverse-nodes-in-even-length-groups.js (100%) rename 2075-decode-the-slanted-ciphertext.js => 2075.decode-the-slanted-ciphertext.js (100%) rename 2076-process-restricted-friend-requests.js => 2076.process-restricted-friend-requests.js (100%) rename 2078-two-furthest-houses-with-different-colors.js => 2078.two-furthest-houses-with-different-colors.js (100%) rename 2079-watering-plants.js => 2079.watering-plants.js (100%) rename 208-implement-trie-prefix-tree.js => 208.implement-trie-prefix-tree.js (100%) rename 2080-range-frequency-queries.js => 2080.range-frequency-queries.js (100%) rename 2081-sum-of-k-mirror-numbers.js => 2081.sum-of-k-mirror-numbers.js (100%) rename 2083-substrings-that-begin-and-end-with-the-same-letter.js => 2083.substrings-that-begin-and-end-with-the-same-letter.js (100%) rename 2086-minimum-number-of-buckets-required-to-collect-rainwater-from-houses.js => 2086.minimum-number-of-buckets-required-to-collect-rainwater-from-houses.js (100%) rename 2088-count-fertile-pyramids-in-a-land.js => 2088.count-fertile-pyramids-in-a-land.js (100%) rename 2089-find-target-indices-after-sorting-array.js => 2089.find-target-indices-after-sorting-array.js (100%) rename 209-minimum-size-subarray-sum.js => 209.minimum-size-subarray-sum.js (100%) rename 2090-k-radius-subarray-averages.js => 2090.k-radius-subarray-averages.js (100%) rename 2091-removing-minimum-and-maximum-from-array.js => 2091.removing-minimum-and-maximum-from-array.js (100%) rename 2092-find-all-people-with-secret.js => 2092.find-all-people-with-secret.js (100%) rename 2094-finding-3-digit-even-numbers.js => 2094.finding-3-digit-even-numbers.js (100%) rename 2095-delete-the-middle-node-of-a-linked-list.js => 2095.delete-the-middle-node-of-a-linked-list.js (100%) rename 2096-step-by-step-directions-from-a-binary-tree-node-to-another.js => 2096.step-by-step-directions-from-a-binary-tree-node-to-another.js (100%) rename 2097-valid-arrangement-of-pairs.js => 2097.valid-arrangement-of-pairs.js (100%) rename 2098-subsequence-of-size-k-with-the-largest-even-sum.js => 2098.subsequence-of-size-k-with-the-largest-even-sum.js (100%) rename 21-merge-two-sorted-lists.js => 21.merge-two-sorted-lists.js (94%) mode change 100755 => 100644 rename 210-course-schedule-ii.js => 210.course-schedule-ii.js (100%) rename 2100-find-good-days-to-rob-the-bank.js => 2100.find-good-days-to-rob-the-bank.js (100%) rename 2101-detonate-the-maximum-bombs.js => 2101.detonate-the-maximum-bombs.js (100%) rename 2102-sequentially-ordinal-rank-tracker.js => 2102.sequentially-ordinal-rank-tracker.js (100%) rename 2103-rings-and-rods.js => 2103.rings-and-rods.js (100%) rename 2104-sum-of-subarray-ranges.js => 2104.sum-of-subarray-ranges.js (100%) rename 2105-watering-plants-ii.js => 2105.watering-plants-ii.js (100%) rename 2106-maximum-fruits-harvested-after-at-most-k-steps.js => 2106.maximum-fruits-harvested-after-at-most-k-steps.js (100%) rename 211-add-and-search-word-data-structure-design.js => 211.add-and-search-word-data-structure-design.js (100%) rename 2111-minimum-operations-to-make-the-array-k-increasing.js => 2111.minimum-operations-to-make-the-array-k-increasing.js (100%) rename 2115-find-all-possible-recipes-from-given-supplies.js => 2115.find-all-possible-recipes-from-given-supplies.js (100%) rename 2116-check-if-a-parentheses-string-can-be-validcheck-if-a-parentheses-string-can-be-valid.js => 2116.check-if-a-parentheses-string-can-be-validcheck-if-a-parentheses-string-can-be-valid.js (100%) rename 2119-a-number-after-a-double-reversal.js => 2119.a-number-after-a-double-reversal.js (100%) rename 212-word-search-ii.js => 212.word-search-ii.js (100%) rename 2121-intervals-between-identical-elements.js => 2121.intervals-between-identical-elements.js (100%) rename 2122-recover-the-original-array.js => 2122.recover-the-original-array.js (100%) rename 2124-check-if-all-as-appears-before-all-bs.js => 2124.check-if-all-as-appears-before-all-bs.js (100%) rename 2125-number-of-laser-beams-in-a-bank.js => 2125.number-of-laser-beams-in-a-bank.js (100%) rename 2126-destroying-asteroids.js => 2126.destroying-asteroids.js (100%) rename 2127-maximum-employees-to-be-invited-to-a-meeting.js => 2127.maximum-employees-to-be-invited-to-a-meeting.js (100%) rename 2128-remove-all-ones-with-row-and-column-flips.js => 2128.remove-all-ones-with-row-and-column-flips.js (100%) rename 213-house-robber-ii.js => 213.house-robber-ii.js (100%) rename 2130-maximum-twin-sum-of-a-linked-list.js => 2130.maximum-twin-sum-of-a-linked-list.js (100%) rename 2132-stamping-the-grid.js => 2132.stamping-the-grid.js (100%) rename 2136-earliest-possible-day-of-full-bloom.js => 2136.earliest-possible-day-of-full-bloom.js (100%) rename 2138-divide-a-string-into-groups-of-size-k.js => 2138.divide-a-string-into-groups-of-size-k.js (100%) rename 2139-minimum-moves-to-reach-target-score.js => 2139.minimum-moves-to-reach-target-score.js (100%) rename 214-shortest-palindrome.js => 214.shortest-palindrome.js (100%) rename 2140-solving-questions-with-brainpower.js => 2140.solving-questions-with-brainpower.js (100%) rename 2141-maximum-running-time-of-n-computers.js => 2141.maximum-running-time-of-n-computers.js (100%) rename 2148-count-elements-with-strictly-smaller-and-greater-elements.js => 2148.count-elements-with-strictly-smaller-and-greater-elements.js (100%) rename 2149-rearrange-array-elements-by-sign.js => 2149.rearrange-array-elements-by-sign.js (100%) rename 215-kth-largest-element-in-an-array.js => 215.kth-largest-element-in-an-array.js (96%) mode change 100755 => 100644 rename 2150-find-all-lonely-numbers-in-the-array.js => 2150.find-all-lonely-numbers-in-the-array.js (100%) rename 2151-maximum-good-people-based-on-statements.js => 2151.maximum-good-people-based-on-statements.js (100%) rename 2152-minimum-number-of-lines-to-cover-points.js => 2152.minimum-number-of-lines-to-cover-points.js (100%) rename 2154-keep-multiplying-found-values-by-two.js => 2154.keep-multiplying-found-values-by-two.js (100%) rename 2155-all-divisions-with-the-highest-score-of-a-binary-array.js => 2155.all-divisions-with-the-highest-score-of-a-binary-array.js (100%) rename 2156-find-substring-with-given-hash-value.js => 2156.find-substring-with-given-hash-value.js (100%) rename 216-combination-sum-III.js => 216.combination-sum-III.js (95%) mode change 100755 => 100644 rename 2161-partition-array-according-to-given-pivot.js => 2161.partition-array-according-to-given-pivot.js (100%) rename 2163-minimum-difference-in-sums-after-removal-of-elements.js => 2163.minimum-difference-in-sums-after-removal-of-elements.js (100%) rename 2164-sort-even-and-odd-indices-independently.js => 2164.sort-even-and-odd-indices-independently.js (100%) rename 2165-smallest-value-of-the-rearranged-number.js => 2165.smallest-value-of-the-rearranged-number.js (100%) rename 2166-design-bitset.js => 2166.design-bitset.js (100%) rename 2167-minimum-time-to-remove-all-cars-containing-illegal-goods.js => 2167.minimum-time-to-remove-all-cars-containing-illegal-goods.js (100%) rename 2169-count-operations-to-obtain-zero.js => 2169.count-operations-to-obtain-zero.js (100%) rename 217-contains-duplicate.js => 217.contains-duplicate.js (94%) mode change 100755 => 100644 rename 2172-maximum-and-sum-of-array.js => 2172.maximum-and-sum-of-array.js (100%) rename 2178-maximum-split-of-positive-even-integers.js => 2178.maximum-split-of-positive-even-integers.js (100%) rename 2179-count-good-triplets-in-an-array.js => 2179.count-good-triplets-in-an-array.js (100%) rename 218-the-skyline-problem.js => 218.the-skyline-problem.js (100%) rename 2180-count-integers-with-even-digit-sum.js => 2180.count-integers-with-even-digit-sum.js (100%) rename 2181-merge-nodes-in-between-zeros.js => 2181.merge-nodes-in-between-zeros.js (100%) rename 2182-construct-string-with-repeat-limit.js => 2182.construct-string-with-repeat-limit.js (100%) rename 2183-count-array-pairs-divisible-by-k.js => 2183.count-array-pairs-divisible-by-k.js (100%) rename 2184-number-of-ways-to-build-sturdy-brick-wall.js => 2184.number-of-ways-to-build-sturdy-brick-wall.js (100%) rename 2188-minimum-time-to-finish-the-race.js => 2188.minimum-time-to-finish-the-race.js (100%) rename 2189-number-of-ways-to-build-house-of-cards.js => 2189.number-of-ways-to-build-house-of-cards.js (100%) rename 219-contains-duplicate-ii.js => 219.contains-duplicate-ii.js (100%) rename 2192-all-ancestors-of-a-node-in-a-directed-acyclic-graph.js => 2192.all-ancestors-of-a-node-in-a-directed-acyclic-graph.js (100%) rename 2193-minimum-number-of-moves-to-make-palindrome.js => 2193.minimum-number-of-moves-to-make-palindrome.js (100%) rename 2197-replace-non-coprime-numbers-in-array.js => 2197.replace-non-coprime-numbers-in-array.js (100%) rename 22-generate-parentheses.js => 22.generate-parentheses.js (96%) mode change 100755 => 100644 rename 220-contains-duplicate-iii.js => 220.contains-duplicate-iii.js (100%) rename 2204-distance-to-a-cycle-in-undirected-graph.js => 2204.distance-to-a-cycle-in-undirected-graph.js (100%) rename 2209-minimum-white-tiles-after-covering-with-carpets.js => 2209.minimum-white-tiles-after-covering-with-carpets.js (100%) rename 221-maximal-square.js => 221.maximal-square.js (100%) rename 2210-count-hills-and-valleys-in-an-array.js => 2210.count-hills-and-valleys-in-an-array.js (100%) rename 2211-count-collisions-on-a-road.js => 2211.count-collisions-on-a-road.js (100%) rename 2212-maximum-points-in-an-archery-competition.js => 2212.maximum-points-in-an-archery-competition.js (100%) rename 2213-longest-substring-of-one-repeating-character.js => 2213.longest-substring-of-one-repeating-character.js (100%) rename 2214-minimum-health-to-beat-game.js => 2214.minimum-health-to-beat-game.js (100%) rename 2215-find-the-difference-of-two-arrays.js => 2215.find-the-difference-of-two-arrays.js (100%) rename 2216-minimum-deletions-to-make-array-beautiful.js => 2216.minimum-deletions-to-make-array-beautiful.js (100%) rename 2217-find-palindrome-with-fixed-length.js => 2217.find-palindrome-with-fixed-length.js (100%) rename 2218-maximum-value-of-k-coins-from-piles.js => 2218.maximum-value-of-k-coins-from-piles.js (100%) rename 222-count-complete-tree-nodes.js => 222.count-complete-tree-nodes.js (100%) rename 2221-find-triangular-sum-of-an-array.js => 2221.find-triangular-sum-of-an-array.js (100%) rename 2222-number-of-ways-to-select-buildings.js => 2222.number-of-ways-to-select-buildings.js (100%) rename 2223-sum-of-scores-of-built-strings.js => 2223.sum-of-scores-of-built-strings.js (100%) rename 2224-minimum-number-of-operations-to-convert-time.js => 2224.minimum-number-of-operations-to-convert-time.js (100%) rename 2225-find-players-with-zero-or-one-losses.js => 2225.find-players-with-zero-or-one-losses.js (100%) rename 2226-maximum-candies-allocated-to-k-children.js => 2226.maximum-candies-allocated-to-k-children.js (100%) rename 2227-encrypt-and-decrypt-strings.js => 2227.encrypt-and-decrypt-strings.js (100%) rename 223-rectangle-area.js => 223.rectangle-area.js (100%) rename 2233-maximum-product-after-k-increments.js => 2233.maximum-product-after-k-increments.js (100%) rename 2234-maximum-total-beauty-of-the-gardens.js => 2234.maximum-total-beauty-of-the-gardens.js (100%) rename 2235-add-two-integers.js => 2235.add-two-integers.js (100%) rename 2237-count-positions-on-street-with-required-brightness.js => 2237.count-positions-on-street-with-required-brightness.js (100%) rename 224-basic-calculator.js => 224.basic-calculator.js (100%) rename 2242-maximum-score-of-a-node-sequence.js => 2242.maximum-score-of-a-node-sequence.js (100%) rename 2243-calculate-digit-sum-of-a-string.js => 2243.calculate-digit-sum-of-a-string.js (100%) rename 2244-minimum-rounds-to-complete-all-tasks.js => 2244.minimum-rounds-to-complete-all-tasks.js (100%) rename 2245-maximum-trailing-zeros-in-a-cornered-path.js => 2245.maximum-trailing-zeros-in-a-cornered-path.js (100%) rename 2246-determine-if-two-events-have-conflict.js => 2246.determine-if-two-events-have-conflict.js (100%) rename 2246-longest-path-with-different-adjacent-characters.js => 2246.longest-path-with-different-adjacent-characters.js (100%) rename 2248-intersection-of-multiple-arrays.js => 2248.intersection-of-multiple-arrays.js (100%) rename 2249-count-lattice-points-inside-a-circle.js => 2249.count-lattice-points-inside-a-circle.js (100%) rename 225-implement-stack-using-queues.js => 225.implement-stack-using-queues.js (100%) rename 2250-count-number-of-rectangles-containing-each-point.js => 2250.count-number-of-rectangles-containing-each-point.js (100%) rename 2251-number-of-flowers-in-full-bloom.js => 2251.number-of-flowers-in-full-bloom.js (100%) rename 2258-escape-the-spreading-fire.js => 2258.escape-the-spreading-fire.js (100%) rename 2259-remove-digit-from-number-to-maximize-result.js => 2259.remove-digit-from-number-to-maximize-result.js (100%) rename 226-invert-binary-tree.js => 226.invert-binary-tree.js (100%) rename 2260-minimum-consecutive-cards-to-pick-up.js => 2260.minimum-consecutive-cards-to-pick-up.js (100%) rename 2261-k-divisible-elements-subarrays.js => 2261.k-divisible-elements-subarrays.js (100%) rename 2262-total-appeal-of-a-string.js => 2262.total-appeal-of-a-string.js (100%) rename 2263-make-array-non-decreasing-or-non-increasing.js => 2263.make-array-non-decreasing-or-non-increasing.js (100%) rename 2264-largest-3-same-digit-number-in-string.js => 2264.largest-3-same-digit-number-in-string.js (100%) rename 2265-count-nodes-equal-to-average-of-subtree.js => 2265.count-nodes-equal-to-average-of-subtree.js (100%) rename 2266-count-number-of-texts.js => 2266.count-number-of-texts.js (100%) rename 2267-check-if-there-is-a-valid-parentheses-string-path.js => 2267.check-if-there-is-a-valid-parentheses-string-path.js (100%) rename 2268-minimum-number-of-keypresses.js => 2268.minimum-number-of-keypresses.js (100%) rename 227-basic-calculator-ii.js => 227.basic-calculator-ii.js (100%) rename 2270-number-of-ways-to-split-array.js => 2270.number-of-ways-to-split-array.js (100%) rename 2271-maximum-white-tiles-covered-by-a-carpet.js => 2271.maximum-white-tiles-covered-by-a-carpet.js (100%) rename 2272-substring-with-largest-variance.js => 2272.substring-with-largest-variance.js (100%) rename 2273-find-resultant-array-after-removing-anagrams.js => 2273.find-resultant-array-after-removing-anagrams.js (100%) rename 2274-maximum-consecutive-floors-without-special-floors.js => 2274.maximum-consecutive-floors-without-special-floors.js (100%) rename 2275-largest-combination-with-bitwise-and-greater-than-zero.js => 2275.largest-combination-with-bitwise-and-greater-than-zero.js (100%) rename 2276-count-integers-in-intervals.js => 2276.count-integers-in-intervals.js (100%) rename 2277-closest-node-to-path-in-tree.js => 2277.closest-node-to-path-in-tree.js (100%) rename 228-summary-ranges.js => 228.summary-ranges.js (100%) rename 2280-minimum-lines-to-represent-a-line-chart.js => 2280.minimum-lines-to-represent-a-line-chart.js (100%) rename 2281-sum-of-total-strength-of-wizards.js => 2281.sum-of-total-strength-of-wizards.js (100%) rename 2282-number-of-people-that-can-be-seen-in-a-grid.js => 2282.number-of-people-that-can-be-seen-in-a-grid.js (100%) rename 2286-booking-concert-tickets-in-groups.js => 2286.booking-concert-tickets-in-groups.js (100%) rename 2287-rearrange-characters-to-make-target-string.js => 2287.rearrange-characters-to-make-target-string.js (100%) rename 2288-apply-discount-to-prices.js => 2288.apply-discount-to-prices.js (100%) rename 2289-steps-to-make-array-non-decreasing.js => 2289.steps-to-make-array-non-decreasing.js (100%) rename 229-majority-element-ii.js => 229.majority-element-ii.js (100%) rename 2290-minimum-obstacle-removal-to-reach-corner.js => 2290.minimum-obstacle-removal-to-reach-corner.js (100%) rename 2291-maximum-profit-from-trading-stocks.js => 2291.maximum-profit-from-trading-stocks.js (100%) rename 2293-min-max-game.js => 2293.min-max-game.js (100%) rename 2294-partition-array-such-that-maximum-difference-is-k.js => 2294.partition-array-such-that-maximum-difference-is-k.js (100%) rename 2295-replace-elements-in-an-array.js => 2295.replace-elements-in-an-array.js (100%) rename 2296-design-a-text-editor.js => 2296.design-a-text-editor.js (100%) rename 23-merge-k-sorted-lists.js => 23.merge-k-sorted-lists.js (100%) rename 230-kth-smallest-element-in-a-bst.js => 230.kth-smallest-element-in-a-bst.js (100%) rename 2302-count-subarrays-with-score-less-than-k.js => 2302.count-subarrays-with-score-less-than-k.js (100%) rename 2303-calculate-amount-paid-in-taxes.js => 2303.calculate-amount-paid-in-taxes.js (100%) rename 2304-minimum-path-cost-in-a-grid.js => 2304.minimum-path-cost-in-a-grid.js (100%) rename 2305-fair-distribution-of-cookies.js => 2305.fair-distribution-of-cookies.js (100%) rename 2306-naming-a-company.js => 2306.naming-a-company.js (100%) rename 231-power-of-two.js => 231.power-of-two.js (100%) rename 2311-longest-binary-subsequence-less-than-or-equal-to-k.js => 2311.longest-binary-subsequence-less-than-or-equal-to-k.js (100%) rename 2312-selling-pieces-of-wood.js => 2312.selling-pieces-of-wood.js (100%) rename 2313-minimum-flips-in-binary-tree-to-get-result.js => 2313.minimum-flips-in-binary-tree-to-get-result.js (100%) rename 2317-maximum-xor-after-operations.js => 2317.maximum-xor-after-operations.js (100%) rename 2318-number-of-distinct-roll-sequences.js => 2318.number-of-distinct-roll-sequences.js (100%) rename 2319-check-if-matrix-is-x-matrix.js => 2319.check-if-matrix-is-x-matrix.js (100%) rename 232-implement-queue-using-stacks.js => 232.implement-queue-using-stacks.js (100%) rename 2320-count-number-of-ways-to-place-houses.js => 2320.count-number-of-ways-to-place-houses.js (100%) rename 2321-maximum-score-of-spliced-array.js => 2321.maximum-score-of-spliced-array.js (100%) rename 2322-minimum-score-after-removals-on-a-tree.js => 2322.minimum-score-after-removals-on-a-tree.js (100%) rename 2325-decode-the-message.js => 2325.decode-the-message.js (100%) rename 2326-spiral-matrix-iv.js => 2326.spiral-matrix-iv.js (100%) rename 2327-number-of-people-aware-of-a-secret.js => 2327.number-of-people-aware-of-a-secret.js (100%) rename 2328-number-of-increasing-paths-in-a-grid.js => 2328.number-of-increasing-paths-in-a-grid.js (100%) rename 233-number-of-digit-one.js => 233.number-of-digit-one.js (100%) rename 2332-the-latest-time-to-catch-a-bus.js => 2332.the-latest-time-to-catch-a-bus.js (100%) rename 2333-minimum-sum-of-squared-difference.js => 2333.minimum-sum-of-squared-difference.js (100%) rename 2334-subarray-with-elements-greater-than-varying-threshold.js => 2334.subarray-with-elements-greater-than-varying-threshold.js (100%) rename 2335-minimum-amount-of-time-to-fill-cups.js => 2335.minimum-amount-of-time-to-fill-cups.js (100%) rename 2336-smallest-number-in-infinite-set.js => 2336.smallest-number-in-infinite-set.js (100%) rename 2337-move-pieces-to-obtain-a-string.js => 2337.move-pieces-to-obtain-a-string.js (100%) rename 2338-count-the-number-of-ideal-arrays.js => 2338.count-the-number-of-ideal-arrays.js (100%) rename 234-palindrome-linked-list.js => 234.palindrome-linked-list.js (100%) rename 2340-minimum-adjacent-swaps-to-make-a-valid-array.js => 2340.minimum-adjacent-swaps-to-make-a-valid-array.js (100%) rename 2341-maximum-number-of-pairs-in-array.js => 2341.maximum-number-of-pairs-in-array.js (100%) rename 2342-max-sum-of-a-pair-with-equal-sum-of-digits.js => 2342.max-sum-of-a-pair-with-equal-sum-of-digits.js (100%) rename 2343-query-kth-smallest-trimmed-number.js => 2343.query-kth-smallest-trimmed-number.js (100%) rename 2344-minimum-deletions-to-make-array-divisible.js => 2344.minimum-deletions-to-make-array-divisible.js (100%) rename 2347-best-poker-hand.js => 2347.best-poker-hand.js (100%) rename 2348-number-of-zero-filled-subarrays.js => 2348.number-of-zero-filled-subarrays.js (100%) rename 235-lowest-common-ancestor-of-a-binary-search-tree.js => 235.lowest-common-ancestor-of-a-binary-search-tree.js (100%) rename 2350-shortest-impossible-sequence-of-rolls.js => 2350.shortest-impossible-sequence-of-rolls.js (100%) rename 2351-first-letter-to-appear-twice.js => 2351.first-letter-to-appear-twice.js (100%) rename 2352-equal-row-and-column-pairs.js => 2352.equal-row-and-column-pairs.js (100%) rename 2353-design-a-food-rating-system.js => 2353.design-a-food-rating-system.js (100%) rename 2354-number-of-excellent-pairs.js => 2354.number-of-excellent-pairs.js (100%) rename 2355-maximum-number-of-books-you-can-take.js => 2355.maximum-number-of-books-you-can-take.js (100%) rename 2357-make-array-zero-by-subtracting-equal-amounts.js => 2357.make-array-zero-by-subtracting-equal-amounts.js (100%) rename 2358-maximum-number-of-groups-entering-a-competition.js => 2358.maximum-number-of-groups-entering-a-competition.js (100%) rename 2359-find-closest-node-to-given-two-nodes.js => 2359.find-closest-node-to-given-two-nodes.js (100%) rename 236-lowest-common-ancestor-of-a-binary-tree.js => 236.lowest-common-ancestor-of-a-binary-tree.js (100%) rename 2360-longest-cycle-in-a-graph.js => 2360.longest-cycle-in-a-graph.js (100%) rename 2365-task-scheduler-ii.js => 2365.task-scheduler-ii.js (100%) rename 2366-minimum-replacements-to-sort-the-array.js => 2366.minimum-replacements-to-sort-the-array.js (100%) rename 2367-count-number-of-bad-pairs.js => 2367.count-number-of-bad-pairs.js (100%) rename 2367-number-of-arithmetic-triplets.js => 2367.number-of-arithmetic-triplets.js (100%) rename 2368-reachable-nodes-with-restrictions.js => 2368.reachable-nodes-with-restrictions.js (100%) rename 2369-check-if-there-is-a-valid-partition-for-the-array.js => 2369.check-if-there-is-a-valid-partition-for-the-array.js (100%) rename 237-delete-node-in-a-linked-list.js => 237.delete-node-in-a-linked-list.js (95%) mode change 100755 => 100644 rename 2370-longest-ideal-subsequence.js => 2370.longest-ideal-subsequence.js (100%) rename 2373-largest-local-values-in-a-matrix.js => 2373.largest-local-values-in-a-matrix.js (100%) rename 2374-node-with-highest-edge-score.js => 2374.node-with-highest-edge-score.js (100%) rename 2375-construct-smallest-number-from-di-string.js => 2375.construct-smallest-number-from-di-string.js (100%) rename 2376-count-special-integers.js => 2376.count-special-integers.js (100%) rename 238-product-of-array-except-self.js => 238.product-of-array-except-self.js (95%) mode change 100755 => 100644 rename 2380-time-needed-to-rearrange-a-binary-string.js => 2380.time-needed-to-rearrange-a-binary-string.js (100%) rename 2381-shifting-letters-ii.js => 2381.shifting-letters-ii.js (100%) rename 2382-maximum-segment-sum-after-removals.js => 2382.maximum-segment-sum-after-removals.js (100%) rename 2383-minimum-hours-of-training-to-win-a-competition.js => 2383.minimum-hours-of-training-to-win-a-competition.js (100%) rename 2384-largest-palindromic-number.js => 2384.largest-palindromic-number.js (100%) rename 2385-amount-of-time-for-binary-tree-to-be-infected.js => 2385.amount-of-time-for-binary-tree-to-be-infected.js (100%) rename 2386-find-the-k-sum-of-an-array.js => 2386.find-the-k-sum-of-an-array.js (100%) rename 2389-longest-subsequence-with-limited-sum.js => 2389.longest-subsequence-with-limited-sum.js (100%) rename 239-sliding-window-maximum.js => 239.sliding-window-maximum.js (100%) rename 2390-removing-stars-from-a-string.js => 2390.removing-stars-from-a-string.js (100%) rename 2391-minimum-amount-of-time-to-collect-garbage.js => 2391.minimum-amount-of-time-to-collect-garbage.js (100%) rename 2392-build-a-matrix-with-conditions.js => 2392.build-a-matrix-with-conditions.js (100%) rename 2397-maximum-rows-covered-by-columns.js => 2397.maximum-rows-covered-by-columns.js (100%) rename 2398-maximum-number-of-robots-within-budget.js => 2398.maximum-number-of-robots-within-budget.js (100%) rename 2399-check-distances-between-same-letters.js => 2399.check-distances-between-same-letters.js (100%) rename 24-swap-nodes-in-pairs.js => 24.swap-nodes-in-pairs.js (94%) mode change 100755 => 100644 rename 240-search-a-2d-matrix-ii.js => 240.search-a-2d-matrix-ii.js (100%) rename 2400-number-of-ways-to-reach-a-position-after-exactly-k-steps.js => 2400.number-of-ways-to-reach-a-position-after-exactly-k-steps.js (100%) rename 2401-longest-nice-subarray.js => 2401.longest-nice-subarray.js (100%) rename 2402-meeting-rooms-iii.js => 2402.meeting-rooms-iii.js (100%) rename 2403-minimum-time-to-kill-all-monsters.js => 2403.minimum-time-to-kill-all-monsters.js (100%) rename 2404-most-frequent-even-element.js => 2404.most-frequent-even-element.js (100%) rename 2405-optimal-partition-of-string.js => 2405.optimal-partition-of-string.js (100%) rename 2406.divide-intervals-into-minimum-number-of-groups.js => 2406.divide.intervals-into-minimum-number-of-groups.js (100%) rename 2407.longest-increasing-subsequence-ii.js => 2407.longest.increasing-subsequence-ii.js (100%) rename 241-different-ways-to-add-parentheses.js => 241.different-ways-to-add-parentheses.js (95%) mode change 100755 => 100644 rename 2411-smallest-subarrays-with-maximum-bitwise-or.js => 2411.smallest-subarrays-with-maximum-bitwise-or.js (100%) rename 2412-minimum-money-required-before-transactions.js => 2412.minimum-money-required-before-transactions.js (100%) rename 2413-smallest-even-multiple.js => 2413.smallest-even-multiple.js (100%) rename 2414-length-of-the-longest-alphabetical-continuous-substring.js => 2414.length-of-the-longest-alphabetical-continuous-substring.js (100%) rename 2415-reverse-odd-levels-of-binary-tree.js => 2415.reverse-odd-levels-of-binary-tree.js (100%) rename 2416-sum-of-prefix-scores-of-strings.js => 2416.sum-of-prefix-scores-of-strings.js (100%) rename 2418-sort-the-people.js => 2418.sort-the-people.js (100%) rename 2419-longest-subarray-with-maximum-bitwise-and.js => 2419.longest-subarray-with-maximum-bitwise-and.js (100%) rename 242-valid-anagram.js => 242.valid-anagram.js (95%) mode change 100755 => 100644 rename 2420-find-all-good-indices.js => 2420.find-all-good-indices.js (100%) rename 2421-number-of-good-paths.js => 2421.number-of-good-paths.js (100%) rename 2422-merge-operations-to-turn-array-into-a-palindrome.js => 2422.merge-operations-to-turn-array-into-a-palindrome.js (100%) rename 2423-remove-letter-to-equalize-frequency.js => 2423.remove-letter-to-equalize-frequency.js (100%) rename 2426-number-of-pairs-satisfying-inequality.js => 2426.number-of-pairs-satisfying-inequality.js (100%) rename 2427-number-of-common-factors.js => 2427.number-of-common-factors.js (100%) rename 2428-maximum-sum-of-an-hourglass.js => 2428.maximum-sum-of-an-hourglass.js (100%) rename 2429-minimize-xor.js => 2429.minimize-xor.js (100%) rename 243-shortest-word-distance.js => 243.shortest-word-distance.js (100%) rename 2430-maximum-deletions-on-a-string.js => 2430.maximum-deletions-on-a-string.js (100%) rename 2432-the-employee-that-worked-on-the-longest-task.js => 2432.the-employee-that-worked-on-the-longest-task.js (100%) rename 2433-find-the-original-array-of-prefix-xor.js => 2433.find-the-original-array-of-prefix-xor.js (100%) rename 2434-using-a-robot-to-print-the-lexicographically-smallest-string.js => 2434.using-a-robot-to-print-the-lexicographically-smallest-string.js (100%) rename 2435-paths-in-matrix-whose-sum-is-divisible-by-k.js => 2435.paths-in-matrix-whose-sum-is-divisible-by-k.js (100%) rename 2438-range-product-queries-of-powers.js => 2438.range-product-queries-of-powers.js (100%) rename 2439-minimize-maximum-of-array.js => 2439.minimize-maximum-of-array.js (100%) rename 244-shortest-word-distance-ii.js => 244.shortest-word-distance-ii.js (100%) rename 2441-largest-positive-integer-that-exists-with-its-negative.js => 2441.largest-positive-integer-that-exists-with-its-negative.js (100%) rename 2442-count-number-of-distinct-integers-after-reverse-operations.js => 2442.count-number-of-distinct-integers-after-reverse-operations.js (100%) rename 2443-sum-of-number-and-its-reverse.js => 2443.sum-of-number-and-its-reverse.js (100%) rename 2444-count-subarrays-with-fixed-bounds.js => 2444.count-subarrays-with-fixed-bounds.js (100%) rename 2447-number-of-subarrays-with-gcd-equal-to-k.js => 2447.number-of-subarrays-with-gcd-equal-to-k.js (100%) rename 2448-minimum-cost-to-make-array-equal.js => 2448.minimum-cost-to-make-array-equal.js (100%) rename 2449-minimum-number-of-operations-to-make-arrays-similar.js => 2449.minimum-number-of-operations-to-make-arrays-similar.js (100%) rename 245-shortest-word-distance-iii.js => 245.shortest-word-distance-iii.js (100%) rename 2453-destroy-sequential-targets.js => 2453.destroy-sequential-targets.js (100%) rename 2454-next-greater-element-iv.js => 2454.next-greater-element-iv.js (100%) rename 2457-minimum-addition-to-make-integer-beautiful.js => 2457.minimum-addition-to-make-integer-beautiful.js (100%) rename 246-strobogrammatic-number.js => 246.strobogrammatic-number.js (100%) rename 2460-apply-operations-to-an-array.js => 2460.apply-operations-to-an-array.js (100%) rename 2461-maximum-sum-of-distinct-subarrays-with-length-k.js => 2461.maximum-sum-of-distinct-subarrays-with-length-k.js (100%) rename 2462-total-cost-to-hire-k-workers.js => 2462.total-cost-to-hire-k-workers.js (100%) rename 2463-minimum-total-distance-traveled.js => 2463.minimum-total-distance-traveled.js (100%) rename 2469-convert-the-temperature.js => 2469.convert-the-temperature.js (100%) rename 247-strobogrammatic-number-ii.js => 247.strobogrammatic-number-ii.js (100%) rename 2470-number-of-subarrays-with-lcm-equal-to-k.js => 2470.number-of-subarrays-with-lcm-equal-to-k.js (100%) rename 2471-minimum-number-of-operations-to-sort-a-binary-tree-by-level.js => 2471.minimum-number-of-operations-to-sort-a-binary-tree-by-level.js (100%) rename 2472-maximum-number-of-non-overlapping-palindrome-substrings.js => 2472.maximum-number-of-non-overlapping-palindrome-substrings.js (100%) rename 2475-number-of-unequal-triplets-in-array.js => 2475.number-of-unequal-triplets-in-array.js (100%) rename 2476-closest-nodes-queries-in-a-binary-search-tree.js => 2476.closest-nodes-queries-in-a-binary-search-tree.js (100%) rename 2477-minimum-fuel-cost-to-report-to-the-capital.js => 2477.minimum-fuel-cost-to-report-to-the-capital.js (100%) rename 2478-number-of-beautiful-partitions.js => 2478.number-of-beautiful-partitions.js (100%) rename 248-strobogrammatic-number-iii.js => 248.strobogrammatic-number-iii.js (100%) rename 2484-count-palindromic-subsequences.js => 2484.count-palindromic-subsequences.js (100%) rename 2485-find-the-pivot-integer.js => 2485.find-the-pivot-integer.js (100%) rename 2486-append-characters-to-string-to-make-subsequence.js => 2486.append-characters-to-string-to-make-subsequence.js (100%) rename 2487-remove-nodes-from-linked-list.js => 2487.remove-nodes-from-linked-list.js (100%) rename 2488-count-subarrays-with-median-k.js => 2488.count-subarrays-with-median-k.js (100%) rename 249-group-shifted-strings.js => 249.group-shifted-strings.js (100%) rename 2490-circular-sentence.js => 2490.circular-sentence.js (100%) rename 2491-divide-players-into-teams-of-equal-skill.js => 2491.divide-players-into-teams-of-equal-skill.js (100%) rename 2492-minimum-score-of-a-path-between-two-cities.js => 2492.minimum-score-of-a-path-between-two-cities.js (100%) rename 2493-divide-nodes-into-the-maximum-number-of-groups.js => 2493.divide-nodes-into-the-maximum-number-of-groups.js (100%) rename 25-reverse-nodes-in-k-group.js => 25.reverse-nodes-in-k-group.js (100%) rename 250-count-univalue-subtrees.js => 250.count-univalue-subtrees.js (100%) rename 2503-maximum-number-of-points-from-grid-queries.js => 2503.maximum-number-of-points-from-grid-queries.js (100%) rename 2508-add-edges-to-make-degrees-of-all-nodes-even.js => 2508.add-edges-to-make-degrees-of-all-nodes-even.js (100%) rename 251-flatten-2d-vector.js => 251.flatten-2d-vector.js (100%) rename 2514-count-anagrams.js => 2514.count-anagrams.js (100%) rename 2516-take-k-of-each-character-from-left-and-right.js => 2516.take-k-of-each-character-from-left-and-right.js (100%) rename 2518-number-of-great-partitions.js => 2518.number-of-great-partitions.js (100%) rename 252-meeting-rooms.js => 252.meeting-rooms.js (100%) rename 2521-distinct-prime-factors-of-product-of-array.js => 2521.distinct-prime-factors-of-product-of-array.js (100%) rename 2523-closest-prime-numbers-in-range.js => 2523.closest-prime-numbers-in-range.js (100%) rename 2527-find-xor-beauty-of-array.js => 2527.find-xor-beauty-of-array.js (100%) rename 2529-maximum-count-of-positive-integer-and-negative-integer.js => 2529.maximum-count-of-positive-integer-and-negative-integer.js (100%) rename 253-meeting-rooms-ii.js => 253.meeting-rooms-ii.js (100%) rename 2530-maximal-score-after-applying-k-operations.js => 2530.maximal-score-after-applying-k-operations.js (100%) rename 2531-make-number-of-distinct-characters-equal.js => 2531.make-number-of-distinct-characters-equal.js (100%) rename 2537-count-the-number-of-good-subarrays.js => 2537.count-the-number-of-good-subarrays.js (100%) rename 2538-difference-between-maximum-and-minimum-price-sum.js => 2538.difference-between-maximum-and-minimum-price-sum.js (100%) rename 254-factor-combinations.js => 254.factor-combinations.js (100%) rename 2542-maximum-subsequence-score.js => 2542.maximum-subsequence-score.js (100%) rename 2543-check-if-point-is-reachable.js => 2543.check-if-point-is-reachable.js (100%) rename 255-verify-preorder-sequence-in-binary-search-tree.js => 255.verify-preorder-sequence-in-binary-search-tree.js (100%) rename 2551-put-marbles-in-bags.js => 2551.put-marbles-in-bags.js (100%) rename 2555-maximize-win-from-two-segments.js => 2555.maximize-win-from-two-segments.js (100%) rename 2556-disconnect-path-in-a-binary-matrix-by-at-most-one-flip.js => 2556.disconnect-path-in-a-binary-matrix-by-at-most-one-flip.js (100%) rename 2557-maximum-number-of-integers-to-choose-from-a-range-ii.js => 2557.maximum-number-of-integers-to-choose-from-a-range-ii.js (100%) rename 2558-take-gifts-from-the-richest-pile.js => 2558.take-gifts-from-the-richest-pile.js (100%) rename 2559-count-vowel-strings-in-ranges.js => 2559.count-vowel-strings-in-ranges.js (100%) rename 256-paint-house.js => 256.paint-house.js (100%) rename 2560-house-robber-iv.js => 2560.house-robber-iv.js (100%) rename 2561-rearranging-fruits.js => 2561.rearranging-fruits.js (100%) rename 2563-count-the-number-of-fair-pairs.js => 2563.count-the-number-of-fair-pairs.js (100%) rename 2564-substring-xor-queries.js => 2564.substring-xor-queries.js (100%) rename 2565-subsequence-with-the-minimum-score.js => 2565.subsequence-with-the-minimum-score.js (100%) rename 2567-minimum-score-by-changing-two-elements.js => 2567.minimum-score-by-changing-two-elements.js (100%) rename 2568-minimum-impossible-or.js => 2568.minimum-impossible-or.js (100%) rename 257-binary-tree-paths.js => 257.binary-tree-paths.js (95%) mode change 100755 => 100644 rename 2570-merge-two-2d-arrays-by-summing-values.js => 2570.merge-two-2d-arrays-by-summing-values.js (100%) rename 2571-minimum-operations-to-reduce-an-integer-to-0.js => 2571.minimum-operations-to-reduce-an-integer-to-0.js (100%) rename 2572-count-the-number-of-square-free-subsets.js => 2572.count-the-number-of-square-free-subsets.js (100%) rename 2572-find-the-divisibility-array-of-a-string.js => 2572.find-the-divisibility-array-of-a-string.js (100%) rename 2573-find-the-string-with-lcp.js => 2573.find-the-string-with-lcp.js (100%) rename 2574-left-and-right-sum-differences.js => 2574.left-and-right-sum-differences.js (100%) rename 2576-find-the-maximum-number-of-marked-indices.js => 2576.find-the-maximum-number-of-marked-indices.js (100%) rename 2577-minimum-time-to-visit-a-cell-in-a-grid.js => 2577.minimum-time-to-visit-a-cell-in-a-grid.js (100%) rename 258-add-digits.js => 258.add-digits.js (94%) mode change 100755 => 100644 rename 2582-pass-the-pillow.js => 2582.pass-the-pillow.js (100%) rename 2583-kth-largest-sum-in-a-binary-tree.js => 2583.kth-largest-sum-in-a-binary-tree.js (100%) rename 2584-split-the-array-to-make-coprime-products.js => 2584.split-the-array-to-make-coprime-products.js (100%) rename 2585-number-of-ways-to-earn-points.js => 2585.number-of-ways-to-earn-points.js (100%) rename 259-3sum-smaller.js => 259.3sum-smaller.js (100%) rename 2599-make-the-prefix-sum-non-negative.js => 2599.make-the-prefix-sum-non-negative.js (100%) rename 26-remove-duplicates-from-sorted-array.js => 26.remove-duplicates-from-sorted-array.js (95%) mode change 100755 => 100644 rename 260-single-number-III.js => 260.single-number-III.js (100%) mode change 100755 => 100644 rename 2604-minimum-time-to-eat-all-grains.js => 2604.minimum-time-to-eat-all-grains.js (100%) rename 2607-make-k-subarray-sums-equal.js => 2607.make-k-subarray-sums-equal.js (100%) rename 2608-shortest-cycle-in-a-graph.js => 2608.shortest-cycle-in-a-graph.js (100%) rename 2609-find-the-longest-balanced-substring-of-a-binary-string.js => 2609.find-the-longest-balanced-substring-of-a-binary-string.js (100%) rename 261-graph-valid-tree.js => 261.graph-valid-tree.js (100%) rename 2610-convert-an-array-into-a-2d-array-with-conditions.js => 2610.convert-an-array-into-a-2d-array-with-conditions.js (100%) rename 2611-mice-and-cheese.js => 2611.mice-and-cheese.js (100%) rename 2614-prime-in-diagonal.js => 2614.prime-in-diagonal.js (100%) rename 2615-sum-of-distances.js => 2615.sum-of-distances.js (100%) rename 2616-minimize-the-maximum-difference-of-pairs.js => 2616.minimize-the-maximum-difference-of-pairs.js (100%) rename 2619-array-prototype-last.js => 2619.array-prototype-last.js (100%) rename 2626-array-reduce-transformation.js => 2626.array-reduce-transformation.js (100%) rename 2627-debounce.js => 2627.debounce.js (100%) rename 263-ugly-number.js => 263.ugly-number.js (100%) rename 2630-memoize-ii.js => 2630.memoize-ii.js (100%) rename 2632-curry.js => 2632.curry.js (100%) rename 2636-promise-pool.js => 2636.promise-pool.js (100%) rename 264-ugly-number-ii.js => 264.ugly-number-ii.js (100%) rename 2640-find-the-score-of-all-prefixes-of-an-array.js => 2640.find-the-score-of-all-prefixes-of-an-array.js (100%) rename 2642-design-graph-with-shortest-path-calculator.js => 2642.design-graph-with-shortest-path-calculator.js (100%) rename 2643-row-with-maximum-ones.js => 2643.row-with-maximum-ones.js (100%) rename 2644-find-the-maximum-divisibility-score.js => 2644.find-the-maximum-divisibility-score.js (100%) rename 2645-minimum-additions-to-make-valid-string.js => 2645.minimum-additions-to-make-valid-string.js (100%) rename 2646-minimize-the-total-price-of-the-trips.js => 2646.minimize-the-total-price-of-the-trips.js (100%) rename 265-paint-house-ii.js => 265.paint-house-ii.js (100%) rename 2651-calculate-delayed-arrival-time.js => 2651.calculate-delayed-arrival-time.js (100%) rename 2652-sum-multiples.js => 2652.sum-multiples.js (100%) rename 2653-sliding-subarray-beauty.js => 2653.sliding-subarray-beauty.js (100%) rename 2654-minimum-number-of-operations-to-make-all-array-elements-equal-to-1.js => 2654.minimum-number-of-operations-to-make-all-array-elements-equal-to-1.js (100%) rename 2658-maximum-number-of-fish-in-a-grid.js => 2658.maximum-number-of-fish-in-a-grid.js (100%) rename 266-palindrome-permutation.js => 266.palindrome-permutation.js (100%) rename 2660-determine-the-winner-of-a-bowling-game.js => 2660.determine-the-winner-of-a-bowling-game.js (100%) rename 2661-first-completely-painted-row-or-column.js => 2661.first-completely-painted-row-or-column.js (100%) rename 2662-minimum-cost-of-a-path-with-special-roads.js => 2662.minimum-cost-of-a-path-with-special-roads.js (100%) rename 2663-lexicographically-smallest-beautiful-string.js => 2663.lexicographically-smallest-beautiful-string.js (100%) rename 267-palindrome-permutation-ii.js => 267.palindrome-permutation-ii.js (100%) rename 2670-find-the-distinct-difference-array.js => 2670.find-the-distinct-difference-array.js (100%) rename 2671-frequency-tracker.js => 2671.frequency-tracker.js (100%) rename 2672-number-of-adjacent-elements-with-the-same-color.js => 2672.number-of-adjacent-elements-with-the-same-color.js (100%) rename 2673-make-costs-of-paths-equal-in-a-binary-tree.js => 2673.make-costs-of-paths-equal-in-a-binary-tree.js (100%) rename 268-missing-number.js => 268.missing-number.js (95%) mode change 100755 => 100644 rename 2681-power-of-heroes.js => 2681.power-of-heroes.js (100%) rename 2682-find-the-losers-of-the-circular-game.js => 2682.find-the-losers-of-the-circular-game.js (100%) rename 2683-neighboring-bitwise-xor.js => 2683.neighboring-bitwise-xor.js (100%) rename 2684-maximum-number-of-moves-in-a-grid.js => 2684.maximum-number-of-moves-in-a-grid.js (100%) rename 2685-count-the-number-of-complete-components.js => 2685.count-the-number-of-complete-components.js (100%) rename 269-alien-dictionary.js => 269.alien-dictionary.js (100%) rename 27-remove-element.js => 27.remove-element.js (94%) mode change 100755 => 100644 rename 270-closest-binary-search-tree-value.js => 270.closest-binary-search-tree-value.js (100%) rename 2702-minimum-operations-to-make-numbers-non-positive.js => 2702.minimum-operations-to-make-numbers-non-positive.js (100%) rename 2709-greatest-common-divisor-traversal.js => 2709.greatest-common-divisor-traversal.js (100%) rename 271-encode-and-decode-strings.js => 271.encode-and-decode-strings.js (100%) rename 2710-remove-trailing-zeros-from-a-string.js => 2710.remove-trailing-zeros-from-a-string.js (100%) rename 2711-difference-of-number-of-distinct-values-on-diagonals.js => 2711.difference-of-number-of-distinct-values-on-diagonals.js (100%) rename 2712-minimum-cost-to-make-all-characters-equal.js => 2712.minimum-cost-to-make-all-characters-equal.js (100%) rename 2713-maximum-strictly-increasing-cells-in-a-matrix.js => 2713.maximum-strictly-increasing-cells-in-a-matrix.js (100%) rename 272-closest-binary-search-tree-value-ii.js => 272.closest-binary-search-tree-value-ii.js (100%) rename 273-integer-to-english-words.js => 273.integer-to-english-words.js (100%) rename 2730-find-the-longest-semi-repetitive-substring.js => 2730.find-the-longest-semi-repetitive-substring.js (100%) rename 274-h-index.js => 274.h-index.js (100%) rename 2747-count-zero-request-servers.js => 2747.count-zero-request-servers.js (100%) rename 275-h-index-ii.js => 275.h-index-ii.js (100%) rename 276-paint-fence.js => 276.paint-fence.js (100%) rename 2760-longest-even-odd-subarray-with-threshold.js => 2760.longest-even-odd-subarray-with-threshold.js (100%) rename 2761-prime-pairs-with-target-sum.js => 2761.prime-pairs-with-target-sum.js (100%) rename 2762-continuous-subarrays.js => 2762.continuous-subarrays.js (100%) rename 2763-sum-of-imbalance-numbers-of-all-subarrays.js => 2763.sum-of-imbalance-numbers-of-all-subarrays.js (100%) rename 2769-find-the-maximum-achievable-number.js => 2769.find-the-maximum-achievable-number.js (100%) rename 277-find-the-celebrity.js => 277.find-the-celebrity.js (100%) rename 2770-maximum-number-of-jumps-to-reach-the-last-index.js => 2770.maximum-number-of-jumps-to-reach-the-last-index.js (100%) rename 2771-longest-non-decreasing-subarray-from-two-arrays.js => 2771.longest-non-decreasing-subarray-from-two-arrays.js (100%) rename 2772-apply-operations-to-make-all-array-elements-equal-to-zero.js => 2772.apply-operations-to-make-all-array-elements-equal-to-zero.js (100%) rename 2778-sum-of-squares-of-special-elements.js => 2778.sum-of-squares-of-special-elements.js (100%) rename 2779-maximum-beauty-of-an-array-after-applying-operation.js => 2779.maximum-beauty-of-an-array-after-applying-operation.js (100%) rename 278-first-bad-version.js => 278.first-bad-version.js (100%) rename 2780-minimum-index-of-a-valid-split.js => 2780.minimum-index-of-a-valid-split.js (100%) rename 2781-length-of-the-longest-valid-substring.js => 2781.length-of-the-longest-valid-substring.js (100%) rename 279-perfect-squares.js => 279.perfect-squares.js (100%) rename 28-implement-strStr().js => 28.implement-strStr().js (95%) mode change 100755 => 100644 rename 280-wiggle-sort.js => 280.wiggle-sort.js (100%) rename 281-zigzag-iterator.js => 281.zigzag-iterator.js (100%) rename 282-expression-add-operators.js => 282.expression-add-operators.js (100%) rename 283-move-zeroes.js => 283.move-zeroes.js (95%) mode change 100755 => 100644 rename 285-inorder-successor-in-bst.js => 285.inorder-successor-in-bst.js (100%) rename 286-walls-and-gates.js => 286.walls-and-gates.js (100%) rename 287-find-the-duplicate-number.js => 287.find-the-duplicate-number.js (95%) mode change 100755 => 100644 rename 288-unique-word-abbreviation.js => 288.unique-word-abbreviation.js (100%) rename 289-game-of-life.js => 289.game-of-life.js (100%) rename 29-divide-two-integers.js => 29.divide-two-integers.js (96%) mode change 100755 => 100644 rename 290-word-pattern.js => 290.word-pattern.js (100%) rename 291-word-pattern-ii.js => 291.word-pattern-ii.js (100%) rename 292-nim-game.js => 292.nim-game.js (100%) rename 293-flip-game.js => 293.flip-game.js (100%) rename 294-flip-game-ii.js => 294.flip-game-ii.js (100%) rename 295-find-median-from-data-stream.js => 295.find-median-from-data-stream.js (100%) rename 296-best-meeting-point.js => 296.best-meeting-point.js (100%) rename 297-serialize-and-deserialize-binary-tree.js => 297.serialize-and-deserialize-binary-tree.js (100%) rename 298-binary-tree-longest-consecutive-sequence.js => 298.binary-tree-longest-consecutive-sequence.js (100%) rename 299-bulls-and-cows.js => 299.bulls-and-cows.js (100%) rename 3-longest-substring-without-repeating-characters.js => 3.longest-substring-without-repeating-characters.js (95%) mode change 100755 => 100644 rename 30-substring-with-concatenation-of-all-words.js => 30.substring-with-concatenation-of-all-words.js (100%) rename 300-longest-increasing-subsequence.js => 300.longest-increasing-subsequence.js (100%) rename 301-remove-invalid-parentheses.js => 301.remove-invalid-parentheses.js (100%) rename 302-smallest-rectangle-enclosing-black-pixels.js => 302.smallest-rectangle-enclosing-black-pixels.js (100%) rename 304-range-sum-query-2d-immutable.js => 304.range-sum-query-2d-immutable.js (100%) rename 305-number-of-islands-ii.js => 305.number-of-islands-ii.js (100%) rename 306-additive-number.js => 306.additive-number.js (100%) rename 307-range-sum-query-mutable.js => 307.range-sum-query-mutable.js (100%) rename 308-range-sum-query-2d-mutable.js => 308.range-sum-query-2d-mutable.js (100%) rename 309-best-time-to-buy-and-sell-stock-with-cooldown.js => 309.best-time-to-buy-and-sell-stock-with-cooldown.js (96%) mode change 100755 => 100644 rename 31-next-permutation.js => 31.next-permutation.js (100%) rename 310-minimum-height-trees.js => 310.minimum-height-trees.js (100%) rename 311-sparse-matrix-multiplication.js => 311.sparse-matrix-multiplication.js (100%) rename 312-burst-balloons.js => 312.burst-balloons.js (100%) rename 313-super-ugly-number.js => 313.super-ugly-number.js (100%) rename 314-binary-tree-vertical-order-traversal.js => 314.binary-tree-vertical-order-traversal.js (100%) rename 315-count-of-smaller-numbers-after-self.js => 315.count-of-smaller-numbers-after-self.js (100%) rename 316-remove-duplicate-letters.js => 316.remove-duplicate-letters.js (100%) rename 317-shortest-distance-from-all-buildings.js => 317.shortest-distance-from-all-buildings.js (100%) rename 318-maximum-product-of-word-lengths.js => 318.maximum-product-of-word-lengths.js (96%) mode change 100755 => 100644 rename 319-bulb-switcher.js => 319.bulb-switcher.js (94%) mode change 100755 => 100644 rename 32-longest-valid-parentheses.js => 32.longest-valid-parentheses.js (100%) rename 320-generalized-abbreviation.js => 320.generalized-abbreviation.js (100%) rename 321-create-maximum-number.js => 321.create-maximum-number.js (100%) rename 322-coin-change.js => 322.coin-change.js (100%) rename 322-reconstruct-itinerary.js => 322.reconstruct-itinerary.js (100%) rename 323-number-of-connected-components-in-an-undirected-graph.js => 323.number-of-connected-components-in-an-undirected-graph.js (100%) rename 324-wiggle-sort-ii.js => 324.wiggle-sort-ii.js (100%) rename 325-maximum-size-subarray-sum-equals-k.js => 325.maximum-size-subarray-sum-equals-k.js (100%) rename 326-power-of-three.js => 326.power-of-three.js (100%) rename 327-count-of-range-sum.js => 327.count-of-range-sum.js (100%) rename 328-odd-even-linked-list.js => 328.odd-even-linked-list.js (95%) mode change 100755 => 100644 rename 329-longest-increasing-path-in-a-matrix.js => 329.longest-increasing-path-in-a-matrix.js (100%) rename 33-search-in-rotated-sorted-array.js => 33.search-in-rotated-sorted-array.js (100%) rename 330-patching-array.js => 330.patching-array.js (100%) rename 331-verify-preorder-serialization-of-a-binary-tree.js => 331.verify-preorder-serialization-of-a-binary-tree.js (100%) rename 333-largest-bst-subtree.js => 333.largest-bst-subtree.js (100%) rename 334-increasing-triplet-subsequence.js => 334.increasing-triplet-subsequence.js (100%) rename 335-self-crossing.js => 335.self-crossing.js (100%) rename 336-palindrome-pairs.js => 336.palindrome-pairs.js (100%) rename 337-house-robber-iii.js => 337.house-robber-iii.js (100%) rename 338-counting-bits.js => 338.counting-bits.js (100%) rename 339-nested-list-weight-sum.js => 339.nested-list-weight-sum.js (100%) rename 34-find-first-and-last-position-of-element-in-sorted-array.js => 34.find-first-and-last-position-of-element-in-sorted-array.js (100%) rename 340-longest-substring-with-at-most-k-distinct-characters.js => 340.longest-substring-with-at-most-k-distinct-characters.js (100%) rename 341-flatten-nested-list-iterator.js => 341.flatten-nested-list-iterator.js (100%) rename 342-power-of-four.js => 342.power-of-four.js (100%) rename 343-integer-break.js => 343.integer-break.js (95%) mode change 100755 => 100644 rename 344-reverse-string.js => 344.reverse-string.js (100%) rename 345-reverse-vowels-of-a-string.js => 345.reverse-vowels-of-a-string.js (100%) rename 346-moving-average-from-data-stream.js => 346.moving-average-from-data-stream.js (100%) rename 347-top-k-frequent-elements.js => 347.top-k-frequent-elements.js (95%) mode change 100755 => 100644 rename 348-design-tic-tac-toe.js => 348.design-tic-tac-toe.js (100%) rename 349-intersection-of-two-arrays.js => 349.intersection-of-two-arrays.js (94%) mode change 100755 => 100644 rename 35-search-insert-position.js => 35.search-insert-position.js (94%) mode change 100755 => 100644 rename 350-intersection-of-two-arrays-ii.js => 350.intersection-of-two-arrays-ii.js (100%) rename 351-android-unlock-patterns.js => 351.android-unlock-patterns.js (100%) rename 352-data-stream-as-disjoint-intervals.js => 352.data-stream-as-disjoint-intervals.js (100%) rename 353-design-snake-game.js => 353.design-snake-game.js (100%) rename 354-russian-doll-envelopes.js => 354.russian-doll-envelopes.js (100%) rename 355-design-twitter.js => 355.design-twitter.js (100%) rename 356-line-reflection.js => 356.line-reflection.js (100%) rename 357-count-numbers-with-unique-digits.js => 357.count-numbers-with-unique-digits.js (94%) mode change 100755 => 100644 rename 358-rearrange-string-k-distance-apart.js => 358.rearrange-string-k-distance-apart.js (100%) rename 359-logger-rate-limiter.js => 359.logger-rate-limiter.js (100%) rename 36-valid-sudoku.js => 36.valid-sudoku.js (100%) rename 360-sort-transformed-array.js => 360.sort-transformed-array.js (100%) rename 361-bomb-enemy.js => 361.bomb-enemy.js (100%) rename 362-design-hit-counter.js => 362.design-hit-counter.js (100%) rename 363-max-sum-of-rectangle-no-larger-than-k.js => 363.max-sum-of-rectangle-no-larger-than-k.js (100%) rename 364-nested-list-weight-sum-ii.js => 364.nested-list-weight-sum-ii.js (100%) rename 365-water-and-jug-problem.js => 365.water-and-jug-problem.js (100%) rename 366-find-leaves-of-binary-tree.js => 366.find-leaves-of-binary-tree.js (100%) rename 367-valid-perfect-square.js => 367.valid-perfect-square.js (100%) rename 368-largest-divisible-subset.js => 368.largest-divisible-subset.js (100%) rename 369-plus-one-linked-list.js => 369.plus-one-linked-list.js (100%) rename 37-sudoku-solver.js => 37.sudoku-solver.js (100%) rename 370-range-addition.js => 370.range-addition.js (100%) rename 371-sum-of-two-integers.js => 371.sum-of-two-integers.js (95%) mode change 100755 => 100644 rename 372-super-pow.js => 372.super-pow.js (100%) rename 373-find-k-pairs-with-smallest-sums.js => 373.find-k-pairs-with-smallest-sums.js (100%) rename 375-guess-number-higher-or-lower-ii.js => 375.guess-number-higher-or-lower-ii.js (100%) rename 376-wiggle-subsequence.js => 376.wiggle-subsequence.js (100%) rename 377-combination-sum-IV.js => 377.combination-sum-IV.js (95%) mode change 100755 => 100644 rename 378-kth-smallest-element-in-a-sorted-matrix.js => 378.kth-smallest-element-in-a-sorted-matrix.js (96%) mode change 100755 => 100644 rename 379-design-phone-directory.js => 379.design-phone-directory.js (100%) rename 38-count-and-say.js => 38.count-and-say.js (100%) rename 380-insert-delete-getrandom-o1.js => 380.insert-delete-getrandom-o1.js (100%) rename 381-insert-delete-getrandom-o1-duplicates-allowed.js => 381.insert-delete-getrandom-o1-duplicates-allowed.js (100%) rename 382-linked-list-random-node.js => 382.linked-list-random-node.js (95%) mode change 100755 => 100644 rename 383-ransom-note.js => 383.ransom-note.js (95%) mode change 100755 => 100644 rename 384-shuffle-an-array.js => 384.shuffle-an-array.js (95%) mode change 100755 => 100644 rename 385-mini-parser.js => 385.mini-parser.js (100%) rename 386-lexicographical-numbers.js => 386.lexicographical-numbers.js (100%) rename 387-first-unique-character-in-a-string.js => 387.first-unique-character-in-a-string.js (94%) mode change 100755 => 100644 rename 388-longest-absolute-file-path.js => 388.longest-absolute-file-path.js (100%) rename 389-find-the-difference.js => 389.find-the-difference.js (96%) mode change 100755 => 100644 rename 39-combination-sum.js => 39.combination-sum.js (96%) mode change 100755 => 100644 rename 390-elimination-game.js => 390.elimination-game.js (100%) rename 391-perfect-rectangle.js => 391.perfect-rectangle.js (100%) rename 392-is-subsequence.js => 392.is-subsequence.js (100%) rename 393-utf-8-validation.js => 393.utf-8-validation.js (100%) rename 394-decode-string.js => 394.decode-string.js (95%) mode change 100755 => 100644 rename 395-longest-substring-with-at-least-k-repeating-characters.js => 395.longest-substring-with-at-least-k-repeating-characters.js (100%) rename 396-rotate-function.js => 396.rotate-function.js (100%) rename 397-integer-replacement.js => 397.integer-replacement.js (100%) rename 398-random-pick-index.js => 398.random-pick-index.js (95%) mode change 100755 => 100644 rename 399-evaluate-division.js => 399.evaluate-division.js (96%) mode change 100755 => 100644 rename 4-median-of-two-sorted-arrays.js => 4.median-of-two-sorted-arrays.js (96%) mode change 100755 => 100644 rename 40-combination-sum-II.js => 40.combination-sum-II.js (96%) mode change 100755 => 100644 rename 400-nth-digit.js => 400.nth-digit.js (100%) rename 401-binary-watch.js => 401.binary-watch.js (100%) rename 402-remove-k-digits.js => 402.remove-k-digits.js (100%) rename 403-frog-jump.js => 403.frog-jump.js (100%) rename 404-sum-of-left-leaves.js => 404.sum-of-left-leaves.js (100%) rename 405-convert-a-number-to-hexadecimal.js => 405.convert-a-number-to-hexadecimal.js (100%) rename 406-queue-reconstruction-by-height.js => 406.queue-reconstruction-by-height.js (100%) mode change 100755 => 100644 rename 407-trapping-rain-water-ii.js => 407.trapping-rain-water-ii.js (100%) rename 408-valid-word-abbreviation.js => 408.valid-word-abbreviation.js (100%) rename 409-longest-palindrome.js => 409.longest-palindrome.js (94%) mode change 100755 => 100644 rename 41-first-missing-positive.js => 41.first-missing-positive.js (100%) rename 410-split-array-largest-sum.js => 410.split-array-largest-sum.js (100%) rename 411-minimum-unique-word-abbreviation.js => 411.minimum-unique-word-abbreviation.js (100%) rename 412-fizz-buzz.js => 412.fizz-buzz.js (93%) mode change 100755 => 100644 rename 413-arithmetic-slices.js => 413.arithmetic-slices.js (95%) mode change 100755 => 100644 rename 414-third-maximum-number.js => 414.third-maximum-number.js (100%) rename 415-add-strings.js => 415.add-strings.js (95%) mode change 100755 => 100644 rename 416-partition-equal-subset-sum.js => 416.partition-equal-subset-sum.js (100%) rename 417-pacific-atlantic-water-flow.js => 417.pacific-atlantic-water-flow.js (100%) rename 418-sentence-screen-fitting.js => 418.sentence-screen-fitting.js (100%) rename 42-trapping-rain-water.js => 42.trapping-rain-water.js (100%) rename 420-strong-password-checker.js => 420.strong-password-checker.js (100%) rename 421-maximum-xor-of-two-numbers-in-an-array.js => 421.maximum-xor-of-two-numbers-in-an-array.js (100%) rename 422-valid-word-square.js => 422.valid-word-square.js (100%) rename 423-reconstruct-original-digits-from-english.js => 423.reconstruct-original-digits-from-english.js (100%) rename 424-longest-repeating-character-replacement.js => 424.longest-repeating-character-replacement.js (96%) mode change 100755 => 100644 rename 425-word-squares.js => 425.word-squares.js (100%) rename 426-convert-binary-search-tree-to-sorted-doubly-linked-list.js => 426.convert-binary-search-tree-to-sorted-doubly-linked-list.js (100%) rename 427-construct-quad-tree.js => 427.construct-quad-tree.js (100%) rename 428-serialize-and-deserialize-n-ary-tree.js => 428.serialize-and-deserialize-n-ary-tree.js (100%) rename 429-n-ary-tree-level-order-traversal.js => 429.n-ary-tree-level-order-traversal.js (100%) rename 43-multiply-strings.js => 43.multiply-strings.js (100%) rename 430-flatten-a-multilevel-doubly-linked-list.js => 430.flatten-a-multilevel-doubly-linked-list.js (100%) rename 431-encode-n-ary-tree-to-binary-tree.js => 431.encode-n-ary-tree-to-binary-tree.js (100%) rename 432-all-oone-data-structure.js => 432.all-oone-data-structure.js (100%) rename 433-minimum-genetic-mutation.js => 433.minimum-genetic-mutation.js (100%) rename 434-number-of-segments-in-a-string.js => 434.number-of-segments-in-a-string.js (100%) rename 435-non-overlapping-intervals.js => 435.non-overlapping-intervals.js (100%) rename 436-find-right-interval.js => 436.find-right-interval.js (100%) rename 437-path-sum-iii.js => 437.path-sum-iii.js (100%) rename 438-find-all-anagrams-in-a-string.js => 438.find-all-anagrams-in-a-string.js (100%) rename 439-ternary-expression-parser.js => 439.ternary-expression-parser.js (100%) rename 44-wildcard-matching.js => 44.wildcard-matching.js (100%) rename 440-k-th-smallest-in-lexicographical-order.js => 440.k-th-smallest-in-lexicographical-order.js (100%) rename 441-arranging-coins.js => 441.arranging-coins.js (100%) rename 442-find-all-duplicates-in-an-array.js => 442.find-all-duplicates-in-an-array.js (95%) mode change 100755 => 100644 rename 443-string-compression.js => 443.string-compression.js (100%) rename 444-sequence-reconstruction.js => 444.sequence-reconstruction.js (100%) rename 445-add-two-numbers-II.js => 445.add-two-numbers-II.js (95%) mode change 100755 => 100644 rename 446-arithmetic-slices-ii-subsequence.js => 446.arithmetic-slices-ii-subsequence.js (100%) rename 447-number-of-boomerangs.js => 447.number-of-boomerangs.js (100%) rename 448-find-all-numbers-disappeared-in-an-array.js => 448.find-all-numbers-disappeared-in-an-array.js (94%) mode change 100755 => 100644 rename 449-serialize-and-deserialize-BST.js => 449.serialize-and-deserialize-BST.js (95%) mode change 100755 => 100644 rename 45-jump-game-ii.js => 45.jump-game-ii.js (100%) rename 450-delete-node-in-a-bst.js => 450.delete-node-in-a-bst.js (100%) rename 451-sort-characters-by-frequency.js => 451.sort-characters-by-frequency.js (95%) mode change 100755 => 100644 rename 452-minimum-number-of-arrows-to-burst-balloons.js => 452.minimum-number-of-arrows-to-burst-balloons.js (100%) rename 453-minimum-moves-to-equal-array-elements.js => 453.minimum-moves-to-equal-array-elements.js (95%) mode change 100755 => 100644 rename 454-4sum-ii.js => 454.4sum-ii.js (100%) rename 455-assign-cookies.js => 455.assign-cookies.js (95%) mode change 100755 => 100644 rename 456-132-pattern.js => 456.132-pattern.js (100%) rename 457-circular-array-loop.js => 457.circular-array-loop.js (100%) rename 458-poor-pigs.js => 458.poor-pigs.js (100%) rename 459-repeated-substring-pattern.js => 459.repeated-substring-pattern.js (100%) rename 46-permutations.js => 46.permutations.js (95%) mode change 100755 => 100644 rename 460-lfu-cache.js => 460.lfu-cache.js (100%) rename 461-hamming-distance.js => 461.hamming-distance.js (100%) rename 462-minimum-moves-to-equal-array-elements-II.js => 462.minimum-moves-to-equal-array-elements-II.js (94%) mode change 100755 => 100644 rename 463-island-perimeter.js => 463.island-perimeter.js (94%) mode change 100755 => 100644 rename 464-can-i-win.js => 464.can-i-win.js (100%) rename 465-optimal-account-balancing.js => 465.optimal-account-balancing.js (100%) rename 466-count-the-repetitions.js => 466.count-the-repetitions.js (100%) rename 467-unique-substrings-in-wraparound-string.js => 467.unique-substrings-in-wraparound-string.js (100%) rename 468-validate-ip-address.js => 468.validate-ip-address.js (100%) rename 469-convex-polygon.js => 469.convex-polygon.js (100%) rename 47-permutations-ii.js => 47.permutations-ii.js (100%) rename 470-implement-rand10-using-rand7.js => 470.implement-rand10-using-rand7.js (100%) rename 471-encode-string-with-shortest-length.js => 471.encode-string-with-shortest-length.js (100%) rename 472-concatenated-words.js => 472.concatenated-words.js (100%) rename 473-matchsticks-to-square.js => 473.matchsticks-to-square.js (100%) rename 474-ones-and-zeroes.js => 474.ones-and-zeroes.js (100%) rename 475-heaters.js => 475.heaters.js (100%) rename 476-number-complement.js => 476.number-complement.js (100%) rename 477-total-hamming-distance.js => 477.total-hamming-distance.js (95%) mode change 100755 => 100644 rename 478-generate-random-point-in-a-circle.js => 478.generate-random-point-in-a-circle.js (100%) rename 479-largest-palindrome-product.js => 479.largest-palindrome-product.js (100%) rename 48-rotate-image.js => 48.rotate-image.js (100%) rename 480-sliding-window-median.js => 480.sliding-window-median.js (100%) rename 481-magical-string.js => 481.magical-string.js (100%) rename 482-license-key-formatting.js => 482.license-key-formatting.js (100%) rename 483-smallest-good-base.js => 483.smallest-good-base.js (100%) rename 484-find-permutation.js => 484.find-permutation.js (100%) rename 485-max-consecutive-ones.js => 485.max-consecutive-ones.js (100%) mode change 100755 => 100644 rename 486-predict-the-winner.js => 486.predict-the-winner.js (96%) mode change 100755 => 100644 rename 487-max-consecutive-ones-ii.js => 487.max-consecutive-ones-ii.js (100%) rename 488-zuma-game.js => 488.zuma-game.js (100%) rename 489-robot-room-cleaner.js => 489.robot-room-cleaner.js (100%) rename 49-group-anagrams.js => 49.group-anagrams.js (95%) mode change 100755 => 100644 rename 490-the-maze.js => 490.the-maze.js (100%) rename 491-increasing-subsequences.js => 491.increasing-subsequences.js (100%) rename 492-construct-the-rectangle.js => 492.construct-the-rectangle.js (100%) rename 493-reverse-pairs.js => 493.reverse-pairs.js (100%) rename 494-target-sum.js => 494.target-sum.js (95%) mode change 100755 => 100644 rename 495-teemo-attacking.js => 495.teemo-attacking.js (100%) rename 496-next-greater-element-I.js => 496.next-greater-element-I.js (100%) mode change 100755 => 100644 rename 497-random-point-in-non-overlapping-rectangles.js => 497.random-point-in-non-overlapping-rectangles.js (100%) rename 498-diagonal-traverse.js => 498.diagonal-traverse.js (100%) rename 499-the-maze-iii.js => 499.the-maze-iii.js (100%) rename 5-longest-palindromic-substring.js => 5.longest-palindromic-substring.js (95%) mode change 100755 => 100644 rename 50-powx-n.js => 50.powx-n.js (100%) rename 500-keyboard-row.js => 500.keyboard-row.js (96%) mode change 100755 => 100644 rename 501-find-mode-in-binary-search-tree.js => 501.find-mode-in-binary-search-tree.js (100%) rename 502-ipo.js => 502.ipo.js (100%) rename 503-next-greater-element-II.js => 503.next-greater-element-II.js (95%) mode change 100755 => 100644 rename 504-base-7.js => 504.base-7.js (100%) rename 505-the-maze-ii.js => 505.the-maze-ii.js (100%) rename 506-relative-ranks.js => 506.relative-ranks.js (100%) rename 507-perfect-number.js => 507.perfect-number.js (100%) rename 508-most-frequent-subtree-sum.js => 508.most-frequent-subtree-sum.js (95%) mode change 100755 => 100644 rename 5083-occurrences-after-bigram.js => 5083.occurrences-after-bigram.js (100%) rename 5084-insufficient-nodes-in-root-to-leaf-paths.js => 5084.insufficient-nodes-in-root-to-leaf-paths.js (100%) rename 509-fibonacci-number.js => 509.fibonacci-number.js (100%) rename 51-n-queens.js => 51.n-queens.js (100%) rename 510-inorder-successor-in-bst-ii.js => 510.inorder-successor-in-bst-ii.js (100%) rename 513-find-bottom-left-tree-value.js => 513.find-bottom-left-tree-value.js (100%) mode change 100755 => 100644 rename 514-freedom-trail.js => 514.freedom-trail.js (100%) rename 515-find-largest-value-in-each-tree-row.js => 515.find-largest-value-in-each-tree-row.js (100%) mode change 100755 => 100644 rename 516-longest-palindromic-subsequence.js => 516.longest-palindromic-subsequence.js (100%) rename 517-super-washing-machines.js => 517.super-washing-machines.js (100%) rename 518-coin-change-2.js => 518.coin-change-2.js (100%) rename 519-random-flip-matrix.js => 519.random-flip-matrix.js (100%) rename 52-n-queens-II.js => 52.n-queens-II.js (95%) mode change 100755 => 100644 rename 520-detect-capital.js => 520.detect-capital.js (94%) mode change 100755 => 100644 rename 521-longest-uncommon-subsequence-i.js => 521.longest-uncommon-subsequence-i.js (100%) rename 522-longest-uncommon-subsequence-ii.js => 522.longest-uncommon-subsequence-ii.js (100%) rename 523-continuous-subarray-sum.js => 523.continuous-subarray-sum.js (100%) rename 524-longest-word-in-dictionary-through-deleting.js => 524.longest-word-in-dictionary-through-deleting.js (100%) rename 525-contiguous-array.js => 525.contiguous-array.js (100%) rename 526-beautiful-arrangement.js => 526.beautiful-arrangement.js (100%) rename 527-word-abbreviation.js => 527.word-abbreviation.js (100%) rename 528-random-pick-with-weight.js => 528.random-pick-with-weight.js (100%) rename 529-minesweeper.js => 529.minesweeper.js (100%) rename 53-maximum-subarray.js => 53.maximum-subarray.js (94%) mode change 100755 => 100644 rename 530-minimum-absolute-difference-in-BST.js => 530.minimum-absolute-difference-in-BST.js (95%) mode change 100755 => 100644 rename 531-lonely-pixel-i.js => 531.lonely-pixel-i.js (100%) rename 532-k-diff-pairs-in-an-array.js => 532.k-diff-pairs-in-an-array.js (100%) rename 533-lonely-pixel-ii.js => 533.lonely-pixel-ii.js (100%) rename 536-construct-binary-tree-from-string.js => 536.construct-binary-tree-from-string.js (100%) delete mode 100755 538-convert-BST-to-greater-tree.js rename 538-convert-bst-to-greater-tree.js => 538.convert-BST-to-greater-tree.js (100%) rename 539-minimum-time-difference.js => 539.minimum-time-difference.js (96%) mode change 100755 => 100644 rename 54-spiral-matrix.js => 54.spiral-matrix.js (100%) rename 540-single-element-in-a-sorted-array.js => 540.single-element-in-a-sorted-array.js (100%) mode change 100755 => 100644 rename 541-reverse-string-ii.js => 541.reverse-string-ii.js (100%) rename 542-01-matrix.js => 542.01-matrix.js (100%) rename 543-diameter-of-binary-tree.js => 543.diameter-of-binary-tree.js (95%) mode change 100755 => 100644 rename 544-output-contest-matches.js => 544.output-contest-matches.js (100%) rename 545-boundary-of-binary-tree.js => 545.boundary-of-binary-tree.js (100%) rename 546-remove-boxes.js => 546.remove-boxes.js (100%) rename 547-friend-circles.js => 547.friend-circles.js (100%) rename 548-split-array-with-equal-sum.js => 548.split-array-with-equal-sum.js (100%) rename 549-binary-tree-longest-consecutive-sequence-ii.js => 549.binary-tree-longest-consecutive-sequence-ii.js (100%) rename 55-jump-game.js => 55.jump-game.js (100%) rename 551-student-attendance-record-i.js => 551.student-attendance-record-i.js (100%) rename 552-student-attendance-record-ii.js => 552.student-attendance-record-ii.js (100%) rename 553-optimal-division.js => 553.optimal-division.js (100%) rename 554-brick-wall.js => 554.brick-wall.js (95%) mode change 100755 => 100644 rename 555-split-concatenated-strings.js => 555.split-concatenated-strings.js (100%) rename 556-next-greater-element-iii.js => 556.next-greater-element-iii.js (100%) rename 558-logical-or-of-two-binary-grids-represented-as-quad-trees.js => 558.logical-or-of-two-binary-grids-represented-as-quad-trees.js (100%) rename 559-maximum-depth-of-n-ary-tree.js => 559.maximum-depth-of-n-ary-tree.js (100%) rename 56-merge-intervals.js => 56.merge-intervals.js (100%) rename 560-subarray-sum-equals-k.js => 560.subarray-sum-equals-k.js (100%) rename 562-longest-line-of-consecutive-one-in-matrix.js => 562.longest-line-of-consecutive-one-in-matrix.js (100%) rename 563-binary-tree-tilt.js => 563.binary-tree-tilt.js (100%) rename 564-find-the-closest-palindrome.js => 564.find-the-closest-palindrome.js (100%) rename 565-array-nesting.js => 565.array-nesting.js (95%) mode change 100755 => 100644 rename 566-reshape-the-matrix.js => 566.reshape-the-matrix.js (95%) mode change 100755 => 100644 rename 567-permutation-in-string.js => 567.permutation-in-string.js (100%) rename 568-maximum-vacation-days.js => 568.maximum-vacation-days.js (100%) rename 57-insert-interval.js => 57.insert-interval.js (100%) rename 572-subtree-of-another-tree.js => 572.subtree-of-another-tree.js (100%) rename 573-squirrel-simulation.js => 573.squirrel-simulation.js (100%) rename 5745-last-day-where-you-can-still-cross.js => 5745.last-day-where-you-can-still-cross.js (100%) rename 575-distribute-candies.js => 575.distribute-candies.js (96%) mode change 100755 => 100644 rename 576-out-of-boundary-paths.js => 576.out-of-boundary-paths.js (100%) rename 58-length-of-last-word.js => 58.length-of-last-word.js (95%) mode change 100755 => 100644 rename 581-shortest-unsorted-continuous-subarray.js => 581.shortest-unsorted-continuous-subarray.js (100%) rename 582-kill-process.js => 582.kill-process.js (100%) rename 583-delete-operation-for-two-strings.js => 583.delete-operation-for-two-strings.js (100%) rename 5832-array-with-elements-not-equal-to-average-of-neighbors.js => 5832.array-with-elements-not-equal-to-average-of-neighbors.js (100%) rename 5843-number-of-strings-that-appear-as-substrings-in-word.js => 5843.number-of-strings-that-appear-as-substrings-in-word.js (100%) rename 5844-minimum-non-zero-product-of-the-array-elements.js => 5844.minimum-non-zero-product-of-the-array-elements.js (100%) rename 587-erect-the-fence.js => 587.erect-the-fence.js (100%) rename 588-design-in-memory-file-system.js => 588.design-in-memory-file-system.js (100%) rename 589-n-ary-tree-preorder-traversal.js => 589.n-ary-tree-preorder-traversal.js (100%) rename 59-spiral-matrix-II.js => 59.spiral-matrix-II.js (94%) mode change 100755 => 100644 rename 590-n-ary-tree-postorder-traversal.js => 590.n-ary-tree-postorder-traversal.js (100%) rename 591-tag-validator.js => 591.tag-validator.js (100%) rename 592-fraction-addition-and-subtraction.js => 592.fraction-addition-and-subtraction.js (100%) rename 593-valid-square.js => 593.valid-square.js (100%) rename 594-longest-harmonious-subsequence.js => 594.longest-harmonious-subsequence.js (100%) rename 5956-find-first-palindromic-string-in-the-array.js => 5956.find-first-palindromic-string-in-the-array.js (100%) rename 5957-adding-spaces-to-a-string.js => 5957.adding-spaces-to-a-string.js (100%) rename 5958-number-of-smooth-descent-periods-of-a-stock.js => 5958.number-of-smooth-descent-periods-of-a-stock.js (100%) rename 5976-check-if-every-row-and-column-contains-all-numbers.js => 5976.check-if-every-row-and-column-contains-all-numbers.js (100%) rename 5977-minimum-swaps-to-group-all-1s-together-ii.js => 5977.minimum-swaps-to-group-all-1s-together-ii.js (100%) rename 5978-count-words-obtained-after-adding-a-letter.js => 5978.count-words-obtained-after-adding-a-letter.js (100%) rename 598-range-addition-ii.js => 598.range-addition-ii.js (100%) rename 599-minimum-index-sum-of-two-lists.js => 599.minimum-index-sum-of-two-lists.js (95%) mode change 100755 => 100644 rename 6-zigzag-conversion.js => 6.zigzag-conversion.js (95%) mode change 100755 => 100644 rename 60-permutation-sequence.js => 60.permutation-sequence.js (100%) rename 600-non-negative-integers-without-consecutive-ones.js => 600.non-negative-integers-without-consecutive-ones.js (100%) rename 604-design-compressed-string-iterator.js => 604.design-compressed-string-iterator.js (100%) rename 605-can-place-flowers.js => 605.can-place-flowers.js (100%) rename 606-construct-string-from-binary-tree.js => 606.construct-string-from-binary-tree.js (100%) rename 609-find-duplicate-file-in-system.js => 609.find-duplicate-file-in-system.js (100%) rename 61-rotate-list.js => 61.rotate-list.js (100%) rename 611-valid-triangle-number.js => 611.valid-triangle-number.js (95%) mode change 100755 => 100644 rename 616-add-bold-tag-in-string.js => 616.add-bold-tag-in-string.js (100%) rename 62-unique-paths.js => 62.unique-paths.js (100%) rename 621-task-scheduler.js => 621.task-scheduler.js (95%) mode change 100755 => 100644 rename 622-design-circular-queue.js => 622.design-circular-queue.js (100%) rename 623-add-one-row-to-tree.js => 623.add-one-row-to-tree.js (100%) rename 624-maximum-distance-in-arrays.js => 624.maximum-distance-in-arrays.js (100%) rename 625-minimum-factorization.js => 625.minimum-factorization.js (100%) rename 628-maximum-product-of-three-numbers.js => 628.maximum-product-of-three-numbers.js (100%) rename 629-k-inverse-pairs-array.js => 629.k-inverse-pairs-array.js (100%) rename 63-unique-paths-ii.js => 63.unique-paths-ii.js (100%) rename 630-course-schedule-iii.js => 630.course-schedule-iii.js (100%) rename 631-design-excel-sum-formula.js => 631.design-excel-sum-formula.js (100%) rename 632-smallest-range.js => 632.smallest-range.js (100%) rename 633-sum-of-square-numbers.js => 633.sum-of-square-numbers.js (100%) rename 634-find-the-derangement-of-an-array.js => 634.find-the-derangement-of-an-array.js (100%) rename 635-design-log-storage-system.js => 635.design-log-storage-system.js (100%) rename 636-exclusive-time-of-functions.js => 636.exclusive-time-of-functions.js (100%) rename 637-average-of-levels-in-binary-tree.js => 637.average-of-levels-in-binary-tree.js (100%) mode change 100755 => 100644 rename 638-shopping-offers.js => 638.shopping-offers.js (100%) rename 639-decode-ways-ii.js => 639.decode-ways-ii.js (100%) rename 64-minimum-path-sum.js => 64.minimum-path-sum.js (95%) mode change 100755 => 100644 rename 640-solve-the-equation.js => 640.solve-the-equation.js (100%) rename 641-design-circular-deque.js => 641.design-circular-deque.js (100%) rename 642-design-search-autocomplete-system.js => 642.design-search-autocomplete-system.js (100%) rename 643-maximum-average-subarray-i.js => 643.maximum-average-subarray-i.js (100%) rename 644-maximum-average-subarray-ii.js => 644.maximum-average-subarray-ii.js (100%) rename 645-set-mismatch.js => 645.set-mismatch.js (100%) rename 646-maximum-length-of-pair-chain.js => 646.maximum-length-of-pair-chain.js (95%) mode change 100755 => 100644 rename 647-palindromic-substrings.js => 647.palindromic-substrings.js (100%) mode change 100755 => 100644 rename 648-replace-words.js => 648.replace-words.js (95%) mode change 100755 => 100644 rename 649-dota2-senate.js => 649.dota2-senate.js (100%) rename 65-valid-number.js => 65.valid-number.js (96%) mode change 100755 => 100644 rename 650-2-keys-keyboard.js => 650.2-keys-keyboard.js (93%) mode change 100755 => 100644 rename 651-4-keys-keyboard.js => 651.4-keys-keyboard.js (100%) rename 652-find-duplicate-subtrees.js => 652.find-duplicate-subtrees.js (100%) rename 653-two-sum-iv-input-is-a-bst.js => 653.two-sum-iv-input-is-a-bst.js (100%) rename 655-print-binary-tree.js => 655.print-binary-tree.js (100%) rename 656-coin-path.js => 656.coin-path.js (100%) rename 658-find-k-closest-elements.js => 658.find-k-closest-elements.js (100%) rename 659-split-array-into-consecutive-subsequences.js => 659.split-array-into-consecutive-subsequences.js (100%) rename 66-plus-one.js => 66.plus-one.js (100%) rename 660-remove-9.js => 660.remove-9.js (100%) rename 661-image-smoother.js => 661.image-smoother.js (100%) rename 662-maximum-width-of-binary-tree.js => 662.maximum-width-of-binary-tree.js (100%) rename 663-equal-tree-partition.js => 663.equal-tree-partition.js (100%) rename 664-strange-printer.js => 664.strange-printer.js (100%) rename 665-non-decreasing-array.js => 665.non-decreasing-array.js (100%) rename 666-path-sum-iv.js => 666.path-sum-iv.js (100%) rename 667-beautiful-arrangement-ii.js => 667.beautiful-arrangement-ii.js (100%) rename 668-kth-smallest-number-in-multiplication-table.js => 668.kth-smallest-number-in-multiplication-table.js (100%) rename 669-trim-a-binary-search-tree.js => 669.trim-a-binary-search-tree.js (95%) mode change 100755 => 100644 rename 67-add-binary.js => 67.add-binary.js (100%) rename 670-maximum-swap.js => 670.maximum-swap.js (100%) rename 671-second-minimum-node-in-a-binary-tree.js => 671.second-minimum-node-in-a-binary-tree.js (100%) rename 672-bulb-switcher-ii.js => 672.bulb-switcher-ii.js (100%) rename 673-number-of-longest-increasing-subsequence.js => 673.number-of-longest-increasing-subsequence.js (100%) rename 674-longest-continuous-increasing-subsequence.js => 674.longest-continuous-increasing-subsequence.js (94%) mode change 100755 => 100644 rename 675-cut-off-trees-for-golf-event.js => 675.cut-off-trees-for-golf-event.js (100%) rename 676-implement-magic-dictionary.js => 676.implement-magic-dictionary.js (95%) mode change 100755 => 100644 rename 677-map-sum-pairs.js => 677.map-sum-pairs.js (94%) mode change 100755 => 100644 rename 678-valid-parenthesis-string.js => 678.valid-parenthesis-string.js (100%) rename 679-24-game.js => 679.24-game.js (100%) rename 68-text-justification.js => 68.text-justification.js (95%) mode change 100755 => 100644 rename 680-valid-palindrome-ii.js => 680.valid-palindrome-ii.js (100%) rename 681-next-closest-time.js => 681.next-closest-time.js (100%) rename 682-baseball-game.js => 682.baseball-game.js (95%) mode change 100755 => 100644 rename 683-k-empty-slots.js => 683.k-empty-slots.js (100%) rename 684-redundant-connection.js => 684.redundant-connection.js (100%) rename 685-redundant-connection-ii.js => 685.redundant-connection-ii.js (100%) rename 686-repeated-string-match.js => 686.repeated-string-match.js (100%) rename 687-longest-univalue-path.js => 687.longest-univalue-path.js (100%) rename 688-knight-probability-in-chessboard.js => 688.knight-probability-in-chessboard.js (100%) rename 689-maximum-sum-of-3-non-overlapping-subarrays.js => 689.maximum-sum-of-3-non-overlapping-subarrays.js (100%) rename 69-sqrt(x).js => 69.sqrt(x).js (94%) mode change 100755 => 100644 rename 690-employee-importance.js => 690.employee-importance.js (100%) rename 691-stickers-to-spell-word.js => 691.stickers-to-spell-word.js (100%) rename 692-top-k-frequent-words.js => 692.top-k-frequent-words.js (100%) rename 693-binary-number-with-alternating-bits.js => 693.binary-number-with-alternating-bits.js (100%) mode change 100755 => 100644 rename 694-number-of-distinct-islands.js => 694.number-of-distinct-islands.js (100%) rename 695-max-area-of-island.js => 695.max-area-of-island.js (100%) rename 697-degree-of-an-array.js => 697.degree-of-an-array.js (96%) mode change 100755 => 100644 rename 698-partition-to-k-equal-sum-subsets.js => 698.partition-to-k-equal-sum-subsets.js (100%) rename 699-falling-squares.js => 699.falling-squares.js (100%) rename 7-reverse-integer.js => 7.reverse-integer.js (95%) mode change 100755 => 100644 rename 70-climbing-stairs.js => 70.climbing-stairs.js (94%) mode change 100755 => 100644 rename 700-search-in-a-binary-search-tree.js => 700.search-in-a-binary-search-tree.js (96%) mode change 100755 => 100644 rename 701-insert-into-a-binary-search-tree.js => 701.insert-into-a-binary-search-tree.js (100%) rename 702-search-in-a-sorted-array-of-unknown-size.js => 702.search-in-a-sorted-array-of-unknown-size.js (100%) rename 703-kth-largest-element-in-a-stream.js => 703.kth-largest-element-in-a-stream.js (100%) rename 704-binary-search.js => 704.binary-search.js (100%) rename 705-design-hashset.js => 705.design-hashset.js (100%) rename 706-design-hashmap.js => 706.design-hashmap.js (100%) rename 707-design-linked-list.js => 707.design-linked-list.js (100%) rename 708-insert-into-a-sorted-circular-linked-list.js => 708.insert-into-a-sorted-circular-linked-list.js (100%) rename 71-simplify-path.js => 71.simplify-path.js (100%) rename 710-random-pick-with-blacklist.js => 710.random-pick-with-blacklist.js (100%) rename 711-number-of-distinct-islands-ii.js => 711.number-of-distinct-islands-ii.js (100%) rename 712-minimum-ASCII-delete-sum-for-two-strings.js => 712.minimum-ASCII-delete-sum-for-two-strings.js (96%) mode change 100755 => 100644 rename 713-subarray-product-less-than-k.js => 713.subarray-product-less-than-k.js (100%) rename 714-best-time-to-buy-and-sell-stock-with-transaction-fee.js => 714.best-time-to-buy-and-sell-stock-with-transaction-fee.js (95%) mode change 100755 => 100644 rename 715-range-module.js => 715.range-module.js (100%) rename 716-max-stack.js => 716.max-stack.js (100%) rename 717-1-bit-and-2-bit-characters.js => 717.1-bit-and-2-bit-characters.js (96%) mode change 100755 => 100644 rename 718-maximum-length-of-repeated-subarray.js => 718.maximum-length-of-repeated-subarray.js (95%) mode change 100755 => 100644 rename 719-find-k-th-smallest-pair-distance.js => 719.find-k-th-smallest-pair-distance.js (100%) rename 72-edit-distance.js => 72.edit-distance.js (100%) rename 720-longest-word-in-dictionary.js => 720.longest-word-in-dictionary.js (100%) rename 721-accounts-merge.js => 721.accounts-merge.js (100%) rename 722-remove-comments.js => 722.remove-comments.js (100%) rename 723-candy-crush.js => 723.candy-crush.js (100%) rename 724-find-pivot-index.js => 724.find-pivot-index.js (100%) rename 725-split-linked-list-in-parts.js => 725.split-linked-list-in-parts.js (95%) mode change 100755 => 100644 rename 726-number-of-atoms.js => 726.number-of-atoms.js (100%) rename 727-minimum-window-subsequence.js => 727.minimum-window-subsequence.js (100%) rename 729-my-calendar-i.js => 729.my-calendar-i.js (100%) rename 73-set-matrix-zeroes.js => 73.set-matrix-zeroes.js (100%) rename 730-count-different-palindromic-subsequences.js => 730.count-different-palindromic-subsequences.js (100%) rename 731-my-calendar-ii.js => 731.my-calendar-ii.js (100%) rename 732-my-calendar-iii.js => 732.my-calendar-iii.js (100%) rename 733-flood-fill.js => 733.flood-fill.js (100%) rename 734-sentence-similarity.js => 734.sentence-similarity.js (100%) rename 735-asteroid-collision.js => 735.asteroid-collision.js (100%) rename 736-parse-lisp-expression.js => 736.parse-lisp-expression.js (100%) rename 737-sentence-similarity-ii.js => 737.sentence-similarity-ii.js (100%) rename 738-monotone-increasing-digits.js => 738.monotone-increasing-digits.js (100%) rename 739-daily-temperatures.js => 739.daily-temperatures.js (100%) rename 74-search-a-2d-matrix.js => 74.search-a-2d-matrix.js (100%) rename 740-delete-and-earn.js => 740.delete-and-earn.js (100%) rename 741-cherry-pickup.js => 741.cherry-pickup.js (100%) rename 742-closest-leaf-in-a-binary-tree.js => 742.closest-leaf-in-a-binary-tree.js (100%) rename 743-network-delay-time.js => 743.network-delay-time.js (100%) rename 744-find-smallest-letter-greater-than-target.js => 744.find-smallest-letter-greater-than-target.js (100%) rename 745-prefix-and-suffix-search.js => 745.prefix-and-suffix-search.js (100%) rename 746-min-cost-climbing-stairs.js => 746.min-cost-climbing-stairs.js (95%) mode change 100755 => 100644 rename 747-largest-number-at-least-twice-of-others.js => 747.largest-number-at-least-twice-of-others.js (100%) rename 748-shortest-completing-word.js => 748.shortest-completing-word.js (100%) rename 749-contain-virus.js => 749.contain-virus.js (100%) rename 75-sort-colors.js => 75.sort-colors.js (100%) rename 750-number-of-corner-rectangles.js => 750.number-of-corner-rectangles.js (100%) rename 751-ip-to-cidr.js => 751.ip-to-cidr.js (100%) rename 752-open-the-lock.js => 752.open-the-lock.js (95%) mode change 100755 => 100644 rename 754-reach-a-number.js => 754.reach-a-number.js (100%) rename 755-pour-water.js => 755.pour-water.js (100%) rename 756-pyramid-transition-matrix.js => 756.pyramid-transition-matrix.js (100%) rename 757-set-intersection-size-at-least-two.js => 757.set-intersection-size-at-least-two.js (100%) rename 758-bold-words-in-string.js => 758.bold-words-in-string.js (100%) rename 759-employee-free-time.js => 759.employee-free-time.js (100%) rename 76-minimum-window-substring.js => 76.minimum-window-substring.js (100%) rename 760-find-anagram-mappings.js => 760.find-anagram-mappings.js (100%) rename 761-special-binary-string.js => 761.special-binary-string.js (100%) rename 762-prime-number-of-set-bits-in-binary-representation.js => 762.prime-number-of-set-bits-in-binary-representation.js (100%) mode change 100755 => 100644 rename 763-partition-labels.js => 763.partition-labels.js (100%) rename 764-largest-plus-sign.js => 764.largest-plus-sign.js (100%) rename 765-couples-holding-hands.js => 765.couples-holding-hands.js (100%) rename 766-toeplitz-matrix.js => 766.toeplitz-matrix.js (96%) mode change 100755 => 100644 rename 767-reorganize-string.js => 767.reorganize-string.js (100%) rename 768-max-chunks-to-make-sorted-II.js => 768.max-chunks-to-make-sorted-II.js (95%) mode change 100755 => 100644 rename 769-max-chunks-to-make-sorted.js => 769.max-chunks-to-make-sorted.js (95%) mode change 100755 => 100644 rename 77-combinations.js => 77.combinations.js (94%) mode change 100755 => 100644 rename 770-basic-calculator-iv.js => 770.basic-calculator-iv.js (100%) rename 771-jewels-and-stones.js => 771.jewels-and-stones.js (100%) rename 772-basic-calculator-iii.js => 772.basic-calculator-iii.js (100%) rename 773-sliding-puzzle.js => 773.sliding-puzzle.js (96%) mode change 100755 => 100644 rename 774-minimize-max-distance-to-gas-station.js => 774.minimize-max-distance-to-gas-station.js (100%) rename 775-global-and-local-inversions.js => 775.global-and-local-inversions.js (100%) rename 776-split-bst.js => 776.split-bst.js (100%) rename 777-swap-adjacent-in-lr-string.js => 777.swap-adjacent-in-lr-string.js (100%) rename 778-swim-in-rising-water.js => 778.swim-in-rising-water.js (100%) rename 779-k-th-symbol-in-grammar.js => 779.k-th-symbol-in-grammar.js (100%) rename 78-subsets.js => 78.subsets.js (94%) mode change 100755 => 100644 rename 780-reaching-points.js => 780.reaching-points.js (100%) rename 782-transform-to-chessboard.js => 782.transform-to-chessboard.js (100%) rename 783-minimum-distance-between-BST-nodes.js => 783.minimum-distance-between-BST-nodes.js (95%) mode change 100755 => 100644 rename 784-letter-case-permutation.js => 784.letter-case-permutation.js (100%) rename 785-is-graph-bipartite.js => 785.is-graph-bipartite.js (100%) rename 786-k-th-smallest-prime-fraction.js => 786.k-th-smallest-prime-fraction.js (100%) rename 787-cheapest-flights-within-k-stops.js => 787.cheapest-flights-within-k-stops.js (100%) rename 788-rotated-digits.js => 788.rotated-digits.js (100%) rename 789-escape-the-ghosts.js => 789.escape-the-ghosts.js (100%) rename 79-word-search.js => 79.word-search.js (100%) rename 791-custom-sort-string.js => 791.custom-sort-string.js (95%) mode change 100755 => 100644 rename 792-number-of-matching-subsequences.js => 792.number-of-matching-subsequences.js (100%) rename 793-preimage-size-of-factorial-zeroes-function.js => 793.preimage-size-of-factorial-zeroes-function.js (100%) rename 794-valid-tic-tac-toe-state.js => 794.valid-tic-tac-toe-state.js (100%) rename 795-number-of-subarrays-with-bounded-maximum.js => 795.number-of-subarrays-with-bounded-maximum.js (100%) rename 796-rotate-string.js => 796.rotate-string.js (95%) mode change 100755 => 100644 rename 797-all-paths-from-source-to-target.js => 797.all-paths-from-source-to-target.js (100%) rename 798-smallest-rotation-with-highest-score.js => 798.smallest-rotation-with-highest-score.js (100%) rename 799-champagne-tower.js => 799.champagne-tower.js (100%) rename 8-string-to-integer-(atoi).js => 8.string-to-integer-(atoi).js (95%) mode change 100755 => 100644 rename 80-remove-duplicates-from-sorted-array-ii.js => 80.remove-duplicates-from-sorted-array-ii.js (100%) rename 800-similar-rgb-color.js => 800.similar-rgb-color.js (100%) rename 801-minimum-swaps-to-make-sequences-increasing.js => 801.minimum-swaps-to-make-sequences-increasing.js (100%) rename 802-find-eventual-safe-states.js => 802.find-eventual-safe-states.js (100%) rename 803-bricks-falling-when-hit.js => 803.bricks-falling-when-hit.js (100%) rename 805-split-array-with-same-average.js => 805.split-array-with-same-average.js (100%) rename 81-search-in-rotated-sorted-array-ii.js => 81.search-in-rotated-sorted-array-ii.js (100%) rename 810-chalkboard-xor-game.js => 810.chalkboard-xor-game.js (100%) rename 812-largest-triangle-area.js => 812.largest-triangle-area.js (100%) rename 813-largest-sum-of-averages.js => 813.largest-sum-of-averages.js (100%) rename 815-bus-routes.js => 815.bus-routes.js (100%) rename 818-race-car.js => 818.race-car.js (100%) rename 819-most-common-word.js => 819.most-common-word.js (100%) rename 82-remove-duplicates-from-sorted-list-ii.js => 82.remove-duplicates-from-sorted-list-ii.js (100%) rename 821-shortest-distance-to-a-character.js => 821.shortest-distance-to-a-character.js (96%) mode change 100755 => 100644 rename 823-binary-trees-with-factors.js => 823.binary-trees-with-factors.js (100%) rename 824-goat-latin.js => 824.goat-latin.js (100%) rename 826-most-profit-assigning-work.js => 826.most-profit-assigning-work.js (100%) rename 827-making-a-large-island.js => 827.making-a-large-island.js (100%) rename 828-unique-letter-string.js => 828.unique-letter-string.js (100%) rename 829-consecutive-numbers-sum.js => 829.consecutive-numbers-sum.js (100%) rename 83-remove-duplicates-from-sorted-list.js => 83.remove-duplicates-from-sorted-list.js (95%) mode change 100755 => 100644 rename 830-positions-of-large-groups.js => 830.positions-of-large-groups.js (95%) mode change 100755 => 100644 rename 834-sum-of-distances-in-tree.js => 834.sum-of-distances-in-tree.js (100%) rename 835-image-overlap.js => 835.image-overlap.js (100%) rename 836-rectangle-overlap.js => 836.rectangle-overlap.js (100%) mode change 100755 => 100644 rename 837-new-21-game.js => 837.new-21-game.js (100%) mode change 100755 => 100644 rename 839-similar-string-groups.js => 839.similar-string-groups.js (100%) rename 84-largest-rectangle-in-histogram.js => 84.largest-rectangle-in-histogram.js (100%) rename 841-keys-and-rooms.js => 841.keys-and-rooms.js (95%) mode change 100755 => 100644 rename 843-guess-the-word.js => 843.guess-the-word.js (100%) rename 844-backspace-string-compare.js => 844.backspace-string-compare.js (100%) rename 845-longest-mountain-in-array.js => 845.longest-mountain-in-array.js (100%) rename 847-shortest-path-visiting-all-nodes.js => 847.shortest-path-visiting-all-nodes.js (100%) rename 848-shifting-letters.js => 848.shifting-letters.js (100%) rename 849-maximize-distance-to-closest-person.js => 849.maximize-distance-to-closest-person.js (100%) rename 85-maximal-rectangle.js => 85.maximal-rectangle.js (100%) rename 850-rectangle-area-ii.js => 850.rectangle-area-ii.js (100%) rename 854-k-similar-strings.js => 854.k-similar-strings.js (100%) rename 855-exam-room.js => 855.exam-room.js (100%) rename 856-score-of-parentheses.js => 856.score-of-parentheses.js (100%) mode change 100755 => 100644 rename 857-minimum-cost-to-hire-k-workers.js => 857.minimum-cost-to-hire-k-workers.js (100%) rename 858-mirror-reflection.js => 858.mirror-reflection.js (93%) mode change 100755 => 100644 rename 859-buddy-strings.js => 859.buddy-strings.js (100%) rename 86-partition-list.js => 86.partition-list.js (100%) rename 860-lemonade-change.js => 860.lemonade-change.js (94%) mode change 100755 => 100644 rename 861-score-after-flipping-matrix.js => 861.score-after-flipping-matrix.js (100%) rename 862-shortest-subarray-with-sum-at-least-k.js => 862.shortest-subarray-with-sum-at-least-k.js (100%) rename 863-all-nodes-distance-k-in-binary-tree.js => 863.all-nodes-distance-k-in-binary-tree.js (100%) rename 864-shortest-path-to-get-all-keys.js => 864.shortest-path-to-get-all-keys.js (100%) rename 865-smallest-subtree-with-all-the-deepest-nodes.js => 865.smallest-subtree-with-all-the-deepest-nodes.js (95%) mode change 100755 => 100644 rename 866-prime-palindrome.js => 866.prime-palindrome.js (100%) rename 868-binary-gap.js => 868.binary-gap.js (95%) mode change 100755 => 100644 rename 869-reordered-power-of-2.js => 869.reordered-power-of-2.js (95%) mode change 100755 => 100644 rename 87-scramble-string.js => 87.scramble-string.js (100%) rename 870-advantage-shuffle.js => 870.advantage-shuffle.js (100%) rename 871-minimum-number-of-refueling-stops.js => 871.minimum-number-of-refueling-stops.js (100%) rename 873-length-of-longest-fibonacci-subsequence.js => 873.length-of-longest-fibonacci-subsequence.js (95%) mode change 100755 => 100644 rename 874-walking-robot-simulation.js => 874.walking-robot-simulation.js (100%) rename 875-koko-eating-bananas.js => 875.koko-eating-bananas.js (100%) rename 876-middle-of-the-linked-list.js => 876.middle-of-the-linked-list.js (100%) rename 878-nth-magical-number.js => 878.nth-magical-number.js (100%) rename 879-profitable-schemes.js => 879.profitable-schemes.js (100%) rename 88-merge-sorted-array.js => 88.merge-sorted-array.js (100%) rename 880-decoded-string-at-index.js => 880.decoded-string-at-index.js (100%) rename 881-boats-to-save-people.js => 881.boats-to-save-people.js (100%) rename 882-reachable-nodes-in-subdivided-graph.js => 882.reachable-nodes-in-subdivided-graph.js (100%) rename 883-projection-area-of-3d-shapes.js => 883.projection-area-of-3d-shapes.js (100%) rename 884-uncommon-words-from-two-sentences.js => 884.uncommon-words-from-two-sentences.js (100%) rename 886-possible-bipartition.js => 886.possible-bipartition.js (100%) rename 887-super-egg-drop.js => 887.super-egg-drop.js (100%) rename 888-fair-candy-swap.js => 888.fair-candy-swap.js (100%) rename 889-construct-binary-tree-from-preorder-and-postorder-traversal.js => 889.construct-binary-tree-from-preorder-and-postorder-traversal.js (100%) rename 89-gray-code.js => 89.gray-code.js (100%) rename 890-find-and-replace-pattern.js => 890.find-and-replace-pattern.js (100%) rename 891-sum-of-subsequence-widths.js => 891.sum-of-subsequence-widths.js (100%) rename 892-surface-area-of-3d-shapes.js => 892.surface-area-of-3d-shapes.js (100%) rename 893-groups-of-special-equivalent-strings.js => 893.groups-of-special-equivalent-strings.js (100%) rename 894-all-possible-full-binary-trees.js => 894.all-possible-full-binary-trees.js (100%) rename 895-maximum-frequency-stack.js => 895.maximum-frequency-stack.js (100%) rename 896-monotonic-array.js => 896.monotonic-array.js (100%) rename 897-increasing-order-search-tree.js => 897.increasing-order-search-tree.js (100%) rename 898-bitwise-ors-of-subarrays.js => 898.bitwise-ors-of-subarrays.js (100%) rename 899-orderly-queue.js => 899.orderly-queue.js (100%) rename 9-palindrome-number.js => 9.palindrome-number.js (94%) mode change 100755 => 100644 rename 90-subsets-II.js => 90.subsets-II.js (95%) mode change 100755 => 100644 rename 901-online-stock-span.js => 901.online-stock-span.js (100%) rename 902-numbers-at-most-n-given-digit-set.js => 902.numbers-at-most-n-given-digit-set.js (100%) rename 903-valid-permutations-for-di-sequence.js => 903.valid-permutations-for-di-sequence.js (100%) rename 904-fruit-into-baskets.js => 904.fruit-into-baskets.js (100%) rename 905-sort-array-by-parity.js => 905.sort-array-by-parity.js (100%) rename 906-super-palindromes.js => 906.super-palindromes.js (100%) rename 907-sum-of-subarray-minimums.js => 907.sum-of-subarray-minimums.js (100%) rename 908-smallest-range-i.js => 908.smallest-range-i.js (100%) rename 91-decode-ways.js => 91.decode-ways.js (100%) rename 910-smallest-range-ii.js => 910.smallest-range-ii.js (100%) rename 912-sort-an-array.js => 912.sort-an-array.js (100%) rename 913-cat-and-mouse.js => 913.cat-and-mouse.js (100%) rename 914-x-of-a-kind-in-a-deck-of-cards.js => 914.x-of-a-kind-in-a-deck-of-cards.js (100%) rename 915-partition-array-into-disjoint-intervals.js => 915.partition-array-into-disjoint-intervals.js (100%) rename 916-word-subsets.js => 916.word-subsets.js (95%) mode change 100755 => 100644 rename 917-reverse-only-letters.js => 917.reverse-only-letters.js (100%) rename 918-maximum-sum-circular-subarray.js => 918.maximum-sum-circular-subarray.js (100%) rename 919-complete-binary-tree-inserter.js => 919.complete-binary-tree-inserter.js (100%) rename 92-reverse-linked-list-ii.js => 92.reverse-linked-list-ii.js (100%) rename 920-number-of-music-playlists.js => 920.number-of-music-playlists.js (100%) rename 921-minimum-add-to-make-parentheses-valid.js => 921.minimum-add-to-make-parentheses-valid.js (100%) rename 922-sort-array-by-parity-ii.js => 922.sort-array-by-parity-ii.js (100%) rename 923-3sum-with-multiplicity.js => 923.3sum-with-multiplicity.js (100%) rename 924-minimize-malware-spread.js => 924.minimize-malware-spread.js (100%) rename 925-long-pressed-name.js => 925.long-pressed-name.js (100%) rename 926-flip-string-to-monotone-increasing.js => 926.flip-string-to-monotone-increasing.js (100%) rename 927-three-equal-parts.js => 927.three-equal-parts.js (100%) rename 928-minimize-malware-spread-ii.js => 928.minimize-malware-spread-ii.js (100%) rename 929-unique-email-addresses.js => 929.unique-email-addresses.js (100%) rename 93-restore-ip-addresses.js => 93.restore-ip-addresses.js (100%) rename 930-binary-subarrays-with-sum.js => 930.binary-subarrays-with-sum.js (100%) rename 931-minimum-falling-path-sum.js => 931.minimum-falling-path-sum.js (100%) rename 932-beautiful-array.js => 932.beautiful-array.js (100%) rename 933-number-of-recent-calls.js => 933.number-of-recent-calls.js (100%) rename 934-shortest-bridge.js => 934.shortest-bridge.js (100%) rename 936-stamping-the-sequence.js => 936.stamping-the-sequence.js (100%) rename 937-reorder-data-in-log-files.js => 937.reorder-data-in-log-files.js (100%) rename 938-range-sum-of-bst.js => 938.range-sum-of-bst.js (100%) rename 939-minimum-area-rectangle.js => 939.minimum-area-rectangle.js (100%) rename 94-binary-tree-inorder-traversal.js => 94.binary-tree-inorder-traversal.js (94%) mode change 100755 => 100644 rename 940-distinct-subsequences-ii.js => 940.distinct-subsequences-ii.js (100%) rename 941-valid-mountain-array.js => 941.valid-mountain-array.js (100%) rename 942-di-string-match.js => 942.di-string-match.js (100%) rename 943-find-the-shortest-superstring.js => 943.find-the-shortest-superstring.js (100%) rename 944-delete-columns-to-make-sorted.js => 944.delete-columns-to-make-sorted.js (100%) rename 945-minimum-increment-to-make-array-unique.js => 945.minimum-increment-to-make-array-unique.js (100%) rename 946-validate-stack-sequences.js => 946.validate-stack-sequences.js (100%) rename 947-most-stones-removed-with-same-row-or-column.js => 947.most-stones-removed-with-same-row-or-column.js (100%) rename 948-bag-of-tokens.js => 948.bag-of-tokens.js (100%) rename 949-largest-time-for-given-digits.js => 949.largest-time-for-given-digits.js (100%) rename 95-unique-binary-search-trees-ii.js => 95.unique-binary-search-trees-ii.js (100%) rename 950-reveal-cards-in-increasing-order.js => 950.reveal-cards-in-increasing-order.js (100%) rename 951-flip-equivalent-binary-trees.js => 951.flip-equivalent-binary-trees.js (100%) rename 952-largest-component-size-by-common-factor.js => 952.largest-component-size-by-common-factor.js (100%) rename 953-verifying-an-alien-dictionary.js => 953.verifying-an-alien-dictionary.js (100%) rename 954-array-of-doubled-pairs.js => 954.array-of-doubled-pairs.js (100%) rename 955-delete-columns-to-make-sorted-ii.js => 955.delete-columns-to-make-sorted-ii.js (100%) rename 956-tallest-billboard.js => 956.tallest-billboard.js (100%) rename 957-prison-cells-after-n-days.js => 957.prison-cells-after-n-days.js (100%) rename 958-check-completeness-of-a-binary-tree.js => 958.check-completeness-of-a-binary-tree.js (100%) rename 959-regions-cut-by-slashes.js => 959.regions-cut-by-slashes.js (100%) rename 96-unique-binary-search-trees.js => 96.unique-binary-search-trees.js (100%) rename 960-delete-columns-to-make-sorted-iii.js => 960.delete-columns-to-make-sorted-iii.js (100%) rename 961-n-repeated-element-in-size-2n-array.js => 961.n-repeated-element-in-size-2n-array.js (100%) rename 964-least-operators-to-express-number.js => 964.least-operators-to-express-number.js (100%) rename 965-univalued-binary-tree.js => 965.univalued-binary-tree.js (100%) rename 967-numbers-with-same-consecutive-differences.js => 967.numbers-with-same-consecutive-differences.js (100%) rename 968-binary-tree-cameras.js => 968.binary-tree-cameras.js (100%) rename 969-pancake-sorting.js => 969.pancake-sorting.js (100%) rename 97-interleaving-string.js => 97.interleaving-string.js (100%) rename 971-flip-binary-tree-to-match-preorder-traversal.js => 971.flip-binary-tree-to-match-preorder-traversal.js (100%) rename 972-equal-rational-numbers.js => 972.equal-rational-numbers.js (100%) rename 973-k-closest-points-to-origin.js => 973.k-closest-points-to-origin.js (100%) rename 974-subarray-sums-divisible-by-k.js => 974.subarray-sums-divisible-by-k.js (100%) rename 975-odd-even-jump.js => 975.odd-even-jump.js (100%) rename 976-largest-perimeter-triangle.js => 976.largest-perimeter-triangle.js (100%) rename 977-squares-of-a-sorted-array.js => 977.squares-of-a-sorted-array.js (100%) rename 978-longest-turbulent-subarray.js => 978.longest-turbulent-subarray.js (100%) rename 979-distribute-coins-in-binary-tree.js => 979.distribute-coins-in-binary-tree.js (100%) rename 98-validate-binary-search-tree.js => 98.validate-binary-search-tree.js (100%) rename 980-unique-paths-iii.js => 980.unique-paths-iii.js (100%) rename 981-time-based-key-value-store.js => 981.time-based-key-value-store.js (100%) rename 982-triples-with-bitwise-and-equal-to-zero.js => 982.triples-with-bitwise-and-equal-to-zero.js (100%) rename 983-minimum-cost-for-tickets.js => 983.minimum-cost-for-tickets.js (100%) rename 984-string-without-aaa-or-bbb.js => 984.string-without-aaa-or-bbb.js (100%) rename 985-sum-of-even-numbers-after-queries.js => 985.sum-of-even-numbers-after-queries.js (100%) rename 986-interval-list-intersections.js => 986.interval-list-intersections.js (100%) rename 987-vertical-order-traversal-of-a-binary-tree.js => 987.vertical-order-traversal-of-a-binary-tree.js (100%) rename 988-smallest-string-starting-from-leaf.js => 988.smallest-string-starting-from-leaf.js (100%) rename 989-add-to-array-form-of-integer.js => 989.add-to-array-form-of-integer.js (100%) rename 99-recover-binary-search-tree.js => 99.recover-binary-search-tree.js (100%) rename 990-satisfiability-of-equality-equations.js => 990.satisfiability-of-equality-equations.js (100%) rename 992-subarrays-with-k-different-integers.js => 992.subarrays-with-k-different-integers.js (100%) rename 993.cousins-in-binary-tree.js => 993.cousins.in-binary-tree.js (100%) rename 994-rotting-oranges.js => 994.rotting-oranges.js (100%) rename 995-minimum-number-of-k-consecutive-bit-flips.js => 995.minimum-number-of-k-consecutive-bit-flips.js (100%) rename 996-number-of-squareful-arrays.js => 996.number-of-squareful-arrays.js (100%) rename 997-find-the-town-judge.js => 997.find-the-town-judge.js (100%) rename 999-available-captures-for-rook.js => 999.available-captures-for-rook.js (100%) delete mode 100644 TBD-leftmost-column-with-at-least-a-one.js delete mode 100644 tmp.js diff --git a/1-two-sum.js b/1.two-sum.js old mode 100755 new mode 100644 similarity index 95% rename from 1-two-sum.js rename to 1.two-sum.js index 4b4b8e13..613d465d --- a/1-two-sum.js +++ b/1.two-sum.js @@ -1,15 +1,15 @@ -/** - * @param {number[]} nums - * @param {number} target - * @return {number[]} - */ -const twoSum = function(nums, target) { - const myObject = {}; - for (let i = 0; i < nums.length; i++) { - const complement = target - nums[i]; - if (myObject.hasOwnProperty(complement)) { - return [myObject[complement], i]; - } - myObject[nums[i]] = i; - } -}; +/** + * @param {number[]} nums + * @param {number} target + * @return {number[]} + */ +const twoSum = function(nums, target) { + const myObject = {}; + for (let i = 0; i < nums.length; i++) { + const complement = target - nums[i]; + if (myObject.hasOwnProperty(complement)) { + return [myObject[complement], i]; + } + myObject[nums[i]] = i; + } +}; diff --git a/10-regular-expression-matching.js b/10.regular-expression-matching.js old mode 100755 new mode 100644 similarity index 96% rename from 10-regular-expression-matching.js rename to 10.regular-expression-matching.js index 5bd7918e..dd9d6d53 --- a/10-regular-expression-matching.js +++ b/10.regular-expression-matching.js @@ -1,26 +1,26 @@ -/** - * @param {string} s - * @param {string} p - * @return {boolean} - */ -const isMatch = function(s, p) { - let memory = new Array(s.length + 1) - .fill(0) - .map(e => new Array(p.length + 1).fill(-1)); - return memorySearch(s, 0, p, 0, memory); -}; - -const memorySearch = (s, i, p, k, memory) => { - if (memory[i][k] != -1) return memory[i][k]; - if (k == p.length) return i == s.length; - - let firstMatch = i < s.length && (s[i] == p[k] || p[k] == "."); - if (k + 1 < p.length && p[k + 1] == "*") { - memory[i][k] = - (firstMatch && memorySearch(s, i + 1, p, k, memory)) || - memorySearch(s, i, p, k + 2, memory); - } else { - memory[i][k] = firstMatch && memorySearch(s, i + 1, p, k + 1, memory); - } - return memory[i][k]; -}; +/** + * @param {string} s + * @param {string} p + * @return {boolean} + */ +const isMatch = function(s, p) { + let memory = new Array(s.length + 1) + .fill(0) + .map(e => new Array(p.length + 1).fill(-1)); + return memorySearch(s, 0, p, 0, memory); +}; + +const memorySearch = (s, i, p, k, memory) => { + if (memory[i][k] != -1) return memory[i][k]; + if (k == p.length) return i == s.length; + + let firstMatch = i < s.length && (s[i] == p[k] || p[k] == "."); + if (k + 1 < p.length && p[k + 1] == "*") { + memory[i][k] = + (firstMatch && memorySearch(s, i + 1, p, k, memory)) || + memorySearch(s, i, p, k + 2, memory); + } else { + memory[i][k] = firstMatch && memorySearch(s, i + 1, p, k + 1, memory); + } + return memory[i][k]; +}; diff --git a/100-same-tree.js b/100.same-tree.js old mode 100755 new mode 100644 similarity index 96% rename from 100-same-tree.js rename to 100.same-tree.js index bdab1712..396c8f59 --- a/100-same-tree.js +++ b/100.same-tree.js @@ -1,19 +1,19 @@ -/** - * Definition for a binary tree node. - * function TreeNode(val, left, right) { - * this.val = (val===undefined ? 0 : val) - * this.left = (left===undefined ? null : left) - * this.right = (right===undefined ? null : right) - * } - */ -/** - * @param {TreeNode} p - * @param {TreeNode} q - * @return {boolean} - */ -const isSameTree = function(p, q) { - if(p == null && q == null) return true - if(p == null || q == null || p.val !== q.val) return false - return isSameTree(p.left, q.left) && isSameTree(p.right, q.right) -}; - +/** + * Definition for a binary tree node. + * function TreeNode(val, left, right) { + * this.val = (val===undefined ? 0 : val) + * this.left = (left===undefined ? null : left) + * this.right = (right===undefined ? null : right) + * } + */ +/** + * @param {TreeNode} p + * @param {TreeNode} q + * @return {boolean} + */ +const isSameTree = function(p, q) { + if(p == null && q == null) return true + if(p == null || q == null || p.val !== q.val) return false + return isSameTree(p.left, q.left) && isSameTree(p.right, q.right) +}; + diff --git a/1000-minimum-cost-to-merge-stones.js b/1000.minimum-cost-to-merge-stones.js similarity index 100% rename from 1000-minimum-cost-to-merge-stones.js rename to 1000.minimum-cost-to-merge-stones.js diff --git a/1001-grid-illumination.js b/1001.grid-illumination.js similarity index 100% rename from 1001-grid-illumination.js rename to 1001.grid-illumination.js diff --git a/1002-find-common-characters.js b/1002.find-common-characters.js similarity index 100% rename from 1002-find-common-characters.js rename to 1002.find-common-characters.js diff --git a/1004-max-consecutive-ones-iii.js b/1004.max-consecutive-ones-iii.js similarity index 100% rename from 1004-max-consecutive-ones-iii.js rename to 1004.max-consecutive-ones-iii.js diff --git a/1005-maximize-sum-of-array-after-k-negations.js b/1005.maximize-sum-of-array-after-k-negations.js similarity index 100% rename from 1005-maximize-sum-of-array-after-k-negations.js rename to 1005.maximize-sum-of-array-after-k-negations.js diff --git a/1006-clumsy-factorial.js b/1006.clumsy-factorial.js similarity index 100% rename from 1006-clumsy-factorial.js rename to 1006.clumsy-factorial.js diff --git a/1008-construct-binary-search-tree-from-preorder-traversal.js b/1008.construct-binary-search-tree-from-preorder-traversal.js similarity index 100% rename from 1008-construct-binary-search-tree-from-preorder-traversal.js rename to 1008.construct-binary-search-tree-from-preorder-traversal.js diff --git a/1009-complement-of-base-10-integer.js b/1009.complement-of-base-10-integer.js similarity index 100% rename from 1009-complement-of-base-10-integer.js rename to 1009.complement-of-base-10-integer.js diff --git a/101-symmetric-tree.js b/101.symmetric-tree.js similarity index 100% rename from 101-symmetric-tree.js rename to 101.symmetric-tree.js diff --git a/1011-capacity-to-ship-packages-within-d-days.js b/1011.capacity-to-ship-packages-within-d-days.js similarity index 100% rename from 1011-capacity-to-ship-packages-within-d-days.js rename to 1011.capacity-to-ship-packages-within-d-days.js diff --git a/1012-complement-of-base-10-integer.js b/1012.complement-of-base-10-integer.js similarity index 100% rename from 1012-complement-of-base-10-integer.js rename to 1012.complement-of-base-10-integer.js diff --git a/1013-pairs-of-songs-with-total-durations-divisible-by-60.js b/1013.pairs-of-songs-with-total-durations-divisible-by-60.js similarity index 100% rename from 1013-pairs-of-songs-with-total-durations-divisible-by-60.js rename to 1013.pairs-of-songs-with-total-durations-divisible-by-60.js diff --git a/1014-capacity-to-ship-packages-within-d-days.js b/1014.capacity-to-ship-packages-within-d-days.js similarity index 100% rename from 1014-capacity-to-ship-packages-within-d-days.js rename to 1014.capacity-to-ship-packages-within-d-days.js diff --git a/1015-numbers-with-repeated-digits.js b/1015.numbers-with-repeated-digits.js similarity index 100% rename from 1015-numbers-with-repeated-digits.js rename to 1015.numbers-with-repeated-digits.js diff --git a/1017-convert-to-base-2.js b/1017.convert-to-base-2.js similarity index 100% rename from 1017-convert-to-base-2.js rename to 1017.convert-to-base-2.js diff --git a/1018-binary-prefix-divisible-by-5.js b/1018.binary-prefix-divisible-by-5.js similarity index 100% rename from 1018-binary-prefix-divisible-by-5.js rename to 1018.binary-prefix-divisible-by-5.js diff --git a/102-binary-tree-level-order-traversal.js b/102.binary-tree-level-order-traversal.js old mode 100755 new mode 100644 similarity index 95% rename from 102-binary-tree-level-order-traversal.js rename to 102.binary-tree-level-order-traversal.js index 4df53ee9..1ea8f7fd --- a/102-binary-tree-level-order-traversal.js +++ b/102.binary-tree-level-order-traversal.js @@ -1,36 +1,36 @@ -/** - * Definition for a binary tree node. - * function TreeNode(val) { - * this.val = val; - * this.left = this.right = null; - * } - */ -/** - * @param {TreeNode} root - * @return {number[][]} - */ -const levelOrder = function(root) { - const res = []; - if (root == null) return res; - let next = [root]; - while (next.length > 0) { - next = tr(res, next); - } - return res; -}; - -function tr(res, nodeArr) { - const arr = []; - const nextLevelNodes = []; - for (let i = 0; i < nodeArr.length; i++) { - arr.push(nodeArr[i].val); - if (nodeArr[i].left) { - nextLevelNodes.push(nodeArr[i].left); - } - if (nodeArr[i].right) { - nextLevelNodes.push(nodeArr[i].right); - } - } - if (arr.length) res.push(arr); - return nextLevelNodes; -} +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ +/** + * @param {TreeNode} root + * @return {number[][]} + */ +const levelOrder = function(root) { + const res = []; + if (root == null) return res; + let next = [root]; + while (next.length > 0) { + next = tr(res, next); + } + return res; +}; + +function tr(res, nodeArr) { + const arr = []; + const nextLevelNodes = []; + for (let i = 0; i < nodeArr.length; i++) { + arr.push(nodeArr[i].val); + if (nodeArr[i].left) { + nextLevelNodes.push(nodeArr[i].left); + } + if (nodeArr[i].right) { + nextLevelNodes.push(nodeArr[i].right); + } + } + if (arr.length) res.push(arr); + return nextLevelNodes; +} diff --git a/1020-partition-array-into-three-parts-with-equal-sum.js b/1020.partition-array-into-three-parts-with-equal-sum.js similarity index 100% rename from 1020-partition-array-into-three-parts-with-equal-sum.js rename to 1020.partition-array-into-three-parts-with-equal-sum.js diff --git a/1021-best-sightseeing-pair.js b/1021.best-sightseeing-pair.js similarity index 100% rename from 1021-best-sightseeing-pair.js rename to 1021.best-sightseeing-pair.js diff --git a/1021-remove-outermost-parentheses.js b/1021.remove-outermost-parentheses.js similarity index 100% rename from 1021-remove-outermost-parentheses.js rename to 1021.remove-outermost-parentheses.js diff --git a/1022-smallest-integer-divisible-by-k.js b/1022.smallest-integer-divisible-by-k.js similarity index 100% rename from 1022-smallest-integer-divisible-by-k.js rename to 1022.smallest-integer-divisible-by-k.js diff --git a/1022-sum-of-root-to-leaf-binary-numbers.js b/1022.sum-of-root-to-leaf-binary-numbers.js similarity index 100% rename from 1022-sum-of-root-to-leaf-binary-numbers.js rename to 1022.sum-of-root-to-leaf-binary-numbers.js diff --git a/1023-binary-string-with-substrings-representing-1-to-n.js b/1023.binary-string-with-substrings-representing-1-to-n.js similarity index 100% rename from 1023-binary-string-with-substrings-representing-1-to-n.js rename to 1023.binary-string-with-substrings-representing-1-to-n.js diff --git a/1023-camelcase-matching.js b/1023.camelcase-matching.js similarity index 100% rename from 1023-camelcase-matching.js rename to 1023.camelcase-matching.js diff --git a/1024-video-stitching.js b/1024.video-stitching.js similarity index 100% rename from 1024-video-stitching.js rename to 1024.video-stitching.js diff --git a/1025-divisor-game.js b/1025.divisor-game.js similarity index 100% rename from 1025-divisor-game.js rename to 1025.divisor-game.js diff --git a/1026-maximum-difference-between-node-and-ancestor.js b/1026.maximum-difference-between-node-and-ancestor.js similarity index 100% rename from 1026-maximum-difference-between-node-and-ancestor.js rename to 1026.maximum-difference-between-node-and-ancestor.js diff --git a/1027-longest-arithmetic-sequence.js b/1027.longest-arithmetic-sequence.js similarity index 100% rename from 1027-longest-arithmetic-sequence.js rename to 1027.longest-arithmetic-sequence.js diff --git a/1028-recover-a-tree-from-preorder-traversal.js b/1028.recover-a-tree-from-preorder-traversal.js similarity index 100% rename from 1028-recover-a-tree-from-preorder-traversal.js rename to 1028.recover-a-tree-from-preorder-traversal.js diff --git a/1029-two-city-scheduling.js b/1029.two-city-scheduling.js similarity index 100% rename from 1029-two-city-scheduling.js rename to 1029.two-city-scheduling.js diff --git a/103-binary-tree-zigzag-level-order-traversal.js b/103.binary-tree-zigzag-level-order-traversal.js similarity index 100% rename from 103-binary-tree-zigzag-level-order-traversal.js rename to 103.binary-tree-zigzag-level-order-traversal.js diff --git a/1030-matrix-cells-in-distance-order.js b/1030.matrix-cells-in-distance-order.js similarity index 100% rename from 1030-matrix-cells-in-distance-order.js rename to 1030.matrix-cells-in-distance-order.js diff --git a/1030-next-greater-node-in-linked-list.js b/1030.next-greater-node-in-linked-list.js similarity index 100% rename from 1030-next-greater-node-in-linked-list.js rename to 1030.next-greater-node-in-linked-list.js diff --git a/1031-maximum-sum-of-two-non-overlapping-subarrays.js b/1031.maximum-sum-of-two-non-overlapping-subarrays.js similarity index 100% rename from 1031-maximum-sum-of-two-non-overlapping-subarrays.js rename to 1031.maximum-sum-of-two-non-overlapping-subarrays.js diff --git a/1031-number-of-enclaves.js b/1031.number-of-enclaves.js similarity index 100% rename from 1031-number-of-enclaves.js rename to 1031.number-of-enclaves.js diff --git a/1032-stream-of-characters.js b/1032.stream-of-characters.js similarity index 100% rename from 1032-stream-of-characters.js rename to 1032.stream-of-characters.js diff --git a/1033-moving-stones-until-consecutive.js b/1033.moving-stones-until-consecutive.js similarity index 100% rename from 1033-moving-stones-until-consecutive.js rename to 1033.moving-stones-until-consecutive.js diff --git a/1034-coloring-a-border.js b/1034.coloring-a-border.js similarity index 100% rename from 1034-coloring-a-border.js rename to 1034.coloring-a-border.js diff --git a/1035-uncrossed-lines.js b/1035.uncrossed-lines.js similarity index 100% rename from 1035-uncrossed-lines.js rename to 1035.uncrossed-lines.js diff --git a/1036-escape-a-large-maze.js b/1036.escape-a-large-maze.js similarity index 100% rename from 1036-escape-a-large-maze.js rename to 1036.escape-a-large-maze.js diff --git a/1037-valid-boomerang.js b/1037.valid-boomerang.js similarity index 100% rename from 1037-valid-boomerang.js rename to 1037.valid-boomerang.js diff --git a/1038-binary-search-tree-to-greater-sum-tree.js b/1038.binary-search-tree-to-greater-sum-tree.js similarity index 100% rename from 1038-binary-search-tree-to-greater-sum-tree.js rename to 1038.binary-search-tree-to-greater-sum-tree.js diff --git a/1039-minimum-score-triangulation-of-polygon.js b/1039.minimum-score-triangulation-of-polygon.js similarity index 100% rename from 1039-minimum-score-triangulation-of-polygon.js rename to 1039.minimum-score-triangulation-of-polygon.js diff --git a/104-maximum-depth-of-binary-tree.js b/104.maximum-depth-of-binary-tree.js old mode 100755 new mode 100644 similarity index 95% rename from 104-maximum-depth-of-binary-tree.js rename to 104.maximum-depth-of-binary-tree.js index 46d08f20..5c16f4f2 --- a/104-maximum-depth-of-binary-tree.js +++ b/104.maximum-depth-of-binary-tree.js @@ -1,18 +1,18 @@ -/** - * Definition for a binary tree node. - * function TreeNode(val) { - * this.val = val; - * this.left = this.right = null; - * } - */ -/** - * @param {TreeNode} root - * @return {number} - */ -const maxDepth = function(root) { - if (!root) return 0; - const left = maxDepth(root.left); - const right = maxDepth(root.right); - let depth = left > right ? left : right; - return (depth += 1); -}; +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ +/** + * @param {TreeNode} root + * @return {number} + */ +const maxDepth = function(root) { + if (!root) return 0; + const left = maxDepth(root.left); + const right = maxDepth(root.right); + let depth = left > right ? left : right; + return (depth += 1); +}; diff --git a/1040-moving-stones-until-consecutive-ii.js b/1040.moving-stones-until-consecutive-ii.js similarity index 100% rename from 1040-moving-stones-until-consecutive-ii.js rename to 1040.moving-stones-until-consecutive-ii.js diff --git a/1041-robot-bounded-in-circle.js b/1041.robot-bounded-in-circle.js similarity index 100% rename from 1041-robot-bounded-in-circle.js rename to 1041.robot-bounded-in-circle.js diff --git a/1042-flower-planting-with-no-adjacent.js b/1042.flower-planting-with-no-adjacent.js similarity index 100% rename from 1042-flower-planting-with-no-adjacent.js rename to 1042.flower-planting-with-no-adjacent.js diff --git a/1043-partition-array-for-maximum-sum.js b/1043.partition-array-for-maximum-sum.js similarity index 100% rename from 1043-partition-array-for-maximum-sum.js rename to 1043.partition-array-for-maximum-sum.js diff --git a/1044-longest-duplicate-substring.js b/1044.longest-duplicate-substring.js similarity index 100% rename from 1044-longest-duplicate-substring.js rename to 1044.longest-duplicate-substring.js diff --git a/1046-last-stone-weight.js b/1046.last-stone-weight.js similarity index 100% rename from 1046-last-stone-weight.js rename to 1046.last-stone-weight.js diff --git a/1047-remove-all-adjacent-duplicates-in-string.js b/1047.remove-all-adjacent-duplicates-in-string.js similarity index 100% rename from 1047-remove-all-adjacent-duplicates-in-string.js rename to 1047.remove-all-adjacent-duplicates-in-string.js diff --git a/1048-longest-string-chain.js b/1048.longest-string-chain.js similarity index 100% rename from 1048-longest-string-chain.js rename to 1048.longest-string-chain.js diff --git a/1049-last-stone-weight-ii.js b/1049.last-stone-weight-ii.js similarity index 100% rename from 1049-last-stone-weight-ii.js rename to 1049.last-stone-weight-ii.js diff --git a/105-construct-binary-tree-from-preorder-and-inorder-traversal.js b/105.construct-binary-tree-from-preorder-and-inorder-traversal.js similarity index 100% rename from 105-construct-binary-tree-from-preorder-and-inorder-traversal.js rename to 105.construct-binary-tree-from-preorder-and-inorder-traversal.js diff --git a/1051-height-checker.js b/1051.height-checker.js similarity index 100% rename from 1051-height-checker.js rename to 1051.height-checker.js diff --git a/1052-grumpy-bookstore-owner.js b/1052.grumpy-bookstore-owner.js similarity index 100% rename from 1052-grumpy-bookstore-owner.js rename to 1052.grumpy-bookstore-owner.js diff --git a/1053-previous-permutation-with-one-swap.js b/1053.previous-permutation-with-one-swap.js similarity index 100% rename from 1053-previous-permutation-with-one-swap.js rename to 1053.previous-permutation-with-one-swap.js diff --git a/1054-distant-barcodes.js b/1054.distant-barcodes.js similarity index 100% rename from 1054-distant-barcodes.js rename to 1054.distant-barcodes.js diff --git a/1056-confusing-number.js b/1056.confusing-number.js similarity index 100% rename from 1056-confusing-number.js rename to 1056.confusing-number.js diff --git a/1059-all-paths-from-source-lead-to-destination.js b/1059.all-paths-from-source-lead-to-destination.js similarity index 100% rename from 1059-all-paths-from-source-lead-to-destination.js rename to 1059.all-paths-from-source-lead-to-destination.js diff --git a/106-construct-binary-tree-from-inorder-and-postorder-traversal.js b/106.construct-binary-tree-from-inorder-and-postorder-traversal.js similarity index 100% rename from 106-construct-binary-tree-from-inorder-and-postorder-traversal.js rename to 106.construct-binary-tree-from-inorder-and-postorder-traversal.js diff --git a/1060-missing-element-in-sorted-array.js b/1060.missing-element-in-sorted-array.js similarity index 100% rename from 1060-missing-element-in-sorted-array.js rename to 1060.missing-element-in-sorted-array.js diff --git a/1061-lexicographically-smallest-equivalent-string.js b/1061.lexicographically-smallest-equivalent-string.js similarity index 100% rename from 1061-lexicographically-smallest-equivalent-string.js rename to 1061.lexicographically-smallest-equivalent-string.js diff --git a/1062-longest-repeating-substring.js b/1062.longest-repeating-substring.js similarity index 100% rename from 1062-longest-repeating-substring.js rename to 1062.longest-repeating-substring.js diff --git a/1064-fixed-point.js b/1064.fixed-point.js similarity index 100% rename from 1064-fixed-point.js rename to 1064.fixed-point.js diff --git a/1065-index-pairs-of-a-string.js b/1065.index-pairs-of-a-string.js similarity index 100% rename from 1065-index-pairs-of-a-string.js rename to 1065.index-pairs-of-a-string.js diff --git a/1066-campus-bikes-ii.js b/1066.campus-bikes-ii.js similarity index 100% rename from 1066-campus-bikes-ii.js rename to 1066.campus-bikes-ii.js diff --git a/1067-digit-count-in-range.js b/1067.digit-count-in-range.js similarity index 100% rename from 1067-digit-count-in-range.js rename to 1067.digit-count-in-range.js diff --git a/107-binary-tree-level-order-traversal-ii.js b/107.binary-tree-level-order-traversal-ii.js similarity index 100% rename from 107-binary-tree-level-order-traversal-ii.js rename to 107.binary-tree-level-order-traversal-ii.js diff --git a/1071-greatest-common-divisor-of-strings.js b/1071.greatest-common-divisor-of-strings.js similarity index 100% rename from 1071-greatest-common-divisor-of-strings.js rename to 1071.greatest-common-divisor-of-strings.js diff --git a/1072-flip-columns-for-maximum-number-of-equal-rows.js b/1072.flip-columns-for-maximum-number-of-equal-rows.js similarity index 100% rename from 1072-flip-columns-for-maximum-number-of-equal-rows.js rename to 1072.flip-columns-for-maximum-number-of-equal-rows.js diff --git a/1073-adding-two-negabinary-numbers.js b/1073.adding-two-negabinary-numbers.js similarity index 100% rename from 1073-adding-two-negabinary-numbers.js rename to 1073.adding-two-negabinary-numbers.js diff --git a/1074-number-of-submatrices-that-sum-to-target.js b/1074.number-of-submatrices-that-sum-to-target.js similarity index 100% rename from 1074-number-of-submatrices-that-sum-to-target.js rename to 1074.number-of-submatrices-that-sum-to-target.js diff --git a/1079-letter-tile-possibilities.js b/1079.letter-tile-possibilities.js similarity index 100% rename from 1079-letter-tile-possibilities.js rename to 1079.letter-tile-possibilities.js diff --git a/108-convert-sorted-array-to-binary-search-tree.js b/108.convert-sorted-array-to-binary-search-tree.js old mode 100755 new mode 100644 similarity index 95% rename from 108-convert-sorted-array-to-binary-search-tree.js rename to 108.convert-sorted-array-to-binary-search-tree.js index 16e7f4a5..cbc36d25 --- a/108-convert-sorted-array-to-binary-search-tree.js +++ b/108.convert-sorted-array-to-binary-search-tree.js @@ -1,30 +1,30 @@ -/** - * Definition for a binary tree node. - * function TreeNode(val) { - * this.val = val; - * this.left = this.right = null; - * } - */ -/** - * @param {number[]} nums - * @return {TreeNode} - */ -const sortedArrayToBST = function(nums) { - if (nums.length == 0) { - return null; - } - const head = helper(nums, 0, nums.length - 1); - return head; -}; - -function helper(num, low, high) { - if (low > high) { - // Done - return null; - } - let mid = Math.floor((low + high) / 2); - let node = new TreeNode(num[mid]); - node.left = helper(num, low, mid - 1); - node.right = helper(num, mid + 1, high); - return node; -} +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ +/** + * @param {number[]} nums + * @return {TreeNode} + */ +const sortedArrayToBST = function(nums) { + if (nums.length == 0) { + return null; + } + const head = helper(nums, 0, nums.length - 1); + return head; +}; + +function helper(num, low, high) { + if (low > high) { + // Done + return null; + } + let mid = Math.floor((low + high) / 2); + let node = new TreeNode(num[mid]); + node.left = helper(num, low, mid - 1); + node.right = helper(num, mid + 1, high); + return node; +} diff --git a/1081-smallest-subsequence-of-distinct-characters.js b/1081.smallest-subsequence-of-distinct-characters.js similarity index 100% rename from 1081-smallest-subsequence-of-distinct-characters.js rename to 1081.smallest-subsequence-of-distinct-characters.js diff --git a/1085-sum-of-digits-in-the-minimum-number.js b/1085.sum-of-digits-in-the-minimum-number.js similarity index 100% rename from 1085-sum-of-digits-in-the-minimum-number.js rename to 1085.sum-of-digits-in-the-minimum-number.js diff --git a/1086-high-five.js b/1086.high-five.js similarity index 100% rename from 1086-high-five.js rename to 1086.high-five.js diff --git a/1087-brace-expansion.js b/1087.brace-expansion.js similarity index 100% rename from 1087-brace-expansion.js rename to 1087.brace-expansion.js diff --git a/1088-confusing-number-ii.js b/1088.confusing-number-ii.js similarity index 100% rename from 1088-confusing-number-ii.js rename to 1088.confusing-number-ii.js diff --git a/1089-duplicate-zeros.js b/1089.duplicate-zeros.js similarity index 100% rename from 1089-duplicate-zeros.js rename to 1089.duplicate-zeros.js diff --git a/109-convert-sorted-list-to-binary-search-tree.js b/109.convert-sorted-list-to-binary-search-tree.js similarity index 100% rename from 109-convert-sorted-list-to-binary-search-tree.js rename to 109.convert-sorted-list-to-binary-search-tree.js diff --git a/1090-largest-values-from-labels.js b/1090.largest-values-from-labels.js similarity index 100% rename from 1090-largest-values-from-labels.js rename to 1090.largest-values-from-labels.js diff --git a/1091-shortest-path-in-binary-matrix.js b/1091.shortest-path-in-binary-matrix.js similarity index 100% rename from 1091-shortest-path-in-binary-matrix.js rename to 1091.shortest-path-in-binary-matrix.js diff --git a/1092-shortest-common-supersequence.js b/1092.shortest-common-supersequence.js similarity index 100% rename from 1092-shortest-common-supersequence.js rename to 1092.shortest-common-supersequence.js diff --git a/1093-statistics-from-a-large-sample.js b/1093.statistics-from-a-large-sample.js similarity index 100% rename from 1093-statistics-from-a-large-sample.js rename to 1093.statistics-from-a-large-sample.js diff --git a/1094-car-pooling.js b/1094.car-pooling.js similarity index 100% rename from 1094-car-pooling.js rename to 1094.car-pooling.js diff --git a/1095-find-in-mountain-array.js b/1095.find-in-mountain-array.js similarity index 100% rename from 1095-find-in-mountain-array.js rename to 1095.find-in-mountain-array.js diff --git a/1096-brace-expansion-ii.js b/1096.brace-expansion-ii.js similarity index 100% rename from 1096-brace-expansion-ii.js rename to 1096.brace-expansion-ii.js diff --git a/1099-two-sum-less-than-k.js b/1099.two-sum-less-than-k.js similarity index 100% rename from 1099-two-sum-less-than-k.js rename to 1099.two-sum-less-than-k.js diff --git a/11-container-with-most-water.js b/11.container-with-most-water.js old mode 100755 new mode 100644 similarity index 95% rename from 11-container-with-most-water.js rename to 11.container-with-most-water.js index 2b3aa808..69c6c997 --- a/11-container-with-most-water.js +++ b/11.container-with-most-water.js @@ -1,14 +1,14 @@ -/** - * @param {number[]} height - * @return {number} - */ -const maxArea = function(height) { - let res = 0, l = 0, r = height.length - 1 - while(l < r) { - const tmp = (r - l) * Math.min(height[l], height[r]) - if(tmp > res) res = tmp - if(height[l] < height[r]) l++ - else r-- - } - return res -}; +/** + * @param {number[]} height + * @return {number} + */ +const maxArea = function(height) { + let res = 0, l = 0, r = height.length - 1 + while(l < r) { + const tmp = (r - l) * Math.min(height[l], height[r]) + if(tmp > res) res = tmp + if(height[l] < height[r]) l++ + else r-- + } + return res +}; diff --git a/110-balanced-binary-tree.js b/110.balanced-binary-tree.js similarity index 100% rename from 110-balanced-binary-tree.js rename to 110.balanced-binary-tree.js diff --git a/1102-path-with-maximum-minimum-value.js b/1102.path-with-maximum-minimum-value.js similarity index 100% rename from 1102-path-with-maximum-minimum-value.js rename to 1102.path-with-maximum-minimum-value.js diff --git a/1103-distribute-candies-to-people.js b/1103.distribute-candies-to-people.js similarity index 100% rename from 1103-distribute-candies-to-people.js rename to 1103.distribute-candies-to-people.js diff --git a/1104.path-in-zigzag-labelled-binary-tree.js b/1104.path.in-zigzag-labelled-binary-tree.js similarity index 100% rename from 1104.path-in-zigzag-labelled-binary-tree.js rename to 1104.path.in-zigzag-labelled-binary-tree.js diff --git a/1105-filling-bookcase-shelves.js b/1105.filling-bookcase-shelves.js similarity index 100% rename from 1105-filling-bookcase-shelves.js rename to 1105.filling-bookcase-shelves.js diff --git a/1106-parsing-a-boolean-expression.js b/1106.parsing-a-boolean-expression.js similarity index 100% rename from 1106-parsing-a-boolean-expression.js rename to 1106.parsing-a-boolean-expression.js diff --git a/1108-defanging-an-ip-address.js b/1108.defanging-an-ip-address.js similarity index 100% rename from 1108-defanging-an-ip-address.js rename to 1108.defanging-an-ip-address.js diff --git a/1109-corporate-flight-bookings.js b/1109.corporate-flight-bookings.js similarity index 100% rename from 1109-corporate-flight-bookings.js rename to 1109.corporate-flight-bookings.js diff --git a/111-minimum-depth-of-binary-tree.js b/111.minimum-depth-of-binary-tree.js similarity index 100% rename from 111-minimum-depth-of-binary-tree.js rename to 111.minimum-depth-of-binary-tree.js diff --git a/1110-delete-nodes-and-return-forest.js b/1110.delete-nodes-and-return-forest.js similarity index 100% rename from 1110-delete-nodes-and-return-forest.js rename to 1110.delete-nodes-and-return-forest.js diff --git a/1111-maximum-nesting-depth-of-two-valid-parentheses-strings.js b/1111.maximum-nesting-depth-of-two-valid-parentheses-strings.js similarity index 100% rename from 1111-maximum-nesting-depth-of-two-valid-parentheses-strings.js rename to 1111.maximum-nesting-depth-of-two-valid-parentheses-strings.js diff --git a/1118-number-of-days-in-a-month.js b/1118.number-of-days-in-a-month.js similarity index 100% rename from 1118-number-of-days-in-a-month.js rename to 1118.number-of-days-in-a-month.js diff --git a/1119-remove-vowels-from-a-string.js b/1119.remove-vowels-from-a-string.js similarity index 100% rename from 1119-remove-vowels-from-a-string.js rename to 1119.remove-vowels-from-a-string.js diff --git a/112-path-sum.js b/112.path-sum.js similarity index 100% rename from 112-path-sum.js rename to 112.path-sum.js diff --git a/1120-maximum-average-subtree.js b/1120.maximum-average-subtree.js similarity index 100% rename from 1120-maximum-average-subtree.js rename to 1120.maximum-average-subtree.js diff --git a/1121-divide-array-into-increasing-sequences.js b/1121.divide-array-into-increasing-sequences.js similarity index 100% rename from 1121-divide-array-into-increasing-sequences.js rename to 1121.divide-array-into-increasing-sequences.js diff --git a/1122-relative-sort-array.js b/1122.relative-sort-array.js similarity index 100% rename from 1122-relative-sort-array.js rename to 1122.relative-sort-array.js diff --git a/1123-lowest-common-ancestor-of-deepest-leaves.js b/1123.lowest-common-ancestor-of-deepest-leaves.js similarity index 100% rename from 1123-lowest-common-ancestor-of-deepest-leaves.js rename to 1123.lowest-common-ancestor-of-deepest-leaves.js diff --git a/1124-longest-well-performing-interval.js b/1124.longest-well-performing-interval.js similarity index 100% rename from 1124-longest-well-performing-interval.js rename to 1124.longest-well-performing-interval.js diff --git a/1125-smallest-sufficient-team.js b/1125.smallest-sufficient-team.js similarity index 100% rename from 1125-smallest-sufficient-team.js rename to 1125.smallest-sufficient-team.js diff --git a/1128-number-of-equivalent-domino-pairsnumber-of-equivalent-domino-pairs.js b/1128.number-of-equivalent-domino-pairsnumber-of-equivalent-domino-pairs.js similarity index 100% rename from 1128-number-of-equivalent-domino-pairsnumber-of-equivalent-domino-pairs.js rename to 1128.number-of-equivalent-domino-pairsnumber-of-equivalent-domino-pairs.js diff --git a/1129-shortest-path-with-alternating-colors.js b/1129.shortest-path-with-alternating-colors.js similarity index 100% rename from 1129-shortest-path-with-alternating-colors.js rename to 1129.shortest-path-with-alternating-colors.js diff --git a/113-path-sum-ii.js b/113.path-sum-ii.js similarity index 100% rename from 113-path-sum-ii.js rename to 113.path-sum-ii.js diff --git a/1130-minimum-cost-tree-from-leaf-values.js b/1130.minimum-cost-tree-from-leaf-values.js similarity index 100% rename from 1130-minimum-cost-tree-from-leaf-values.js rename to 1130.minimum-cost-tree-from-leaf-values.js diff --git a/1133-largest-unique-number.js b/1133.largest-unique-number.js similarity index 100% rename from 1133-largest-unique-number.js rename to 1133.largest-unique-number.js diff --git a/1134-armstrong-number.js b/1134.armstrong-number.js similarity index 100% rename from 1134-armstrong-number.js rename to 1134.armstrong-number.js diff --git a/1136-parallel-courses.js b/1136.parallel-courses.js similarity index 100% rename from 1136-parallel-courses.js rename to 1136.parallel-courses.js diff --git a/1137-n-th-tribonacci-number.js b/1137.n-th-tribonacci-number.js similarity index 100% rename from 1137-n-th-tribonacci-number.js rename to 1137.n-th-tribonacci-number.js diff --git a/1138-alphabet-board-path.js b/1138.alphabet-board-path.js similarity index 100% rename from 1138-alphabet-board-path.js rename to 1138.alphabet-board-path.js diff --git a/1139-largest-1-bordered-square.js b/1139.largest-1-bordered-square.js similarity index 100% rename from 1139-largest-1-bordered-square.js rename to 1139.largest-1-bordered-square.js diff --git a/114-flatten-binary-tree-to-linked-list.js b/114.flatten-binary-tree-to-linked-list.js similarity index 100% rename from 114-flatten-binary-tree-to-linked-list.js rename to 114.flatten-binary-tree-to-linked-list.js diff --git a/1140-stone-game-ii.js b/1140.stone-game-ii.js similarity index 100% rename from 1140-stone-game-ii.js rename to 1140.stone-game-ii.js diff --git a/1143-longest-common-subsequence.js b/1143.longest-common-subsequence.js similarity index 100% rename from 1143-longest-common-subsequence.js rename to 1143.longest-common-subsequence.js diff --git a/1145-binary-tree-coloring-game.js b/1145.binary-tree-coloring-game.js similarity index 100% rename from 1145-binary-tree-coloring-game.js rename to 1145.binary-tree-coloring-game.js diff --git a/1146-snapshot-array.js b/1146.snapshot-array.js similarity index 100% rename from 1146-snapshot-array.js rename to 1146.snapshot-array.js diff --git a/1147-longest-chunked-palindrome-decomposition.js b/1147.longest-chunked-palindrome-decomposition.js similarity index 100% rename from 1147-longest-chunked-palindrome-decomposition.js rename to 1147.longest-chunked-palindrome-decomposition.js diff --git a/115-distinct-subsequences.js b/115.distinct-subsequences.js similarity index 100% rename from 115-distinct-subsequences.js rename to 115.distinct-subsequences.js diff --git a/1150-check-if-a-number-is-majority-element-in-a-sorted-array.js b/1150.check-if-a-number-is-majority-element-in-a-sorted-array.js similarity index 100% rename from 1150-check-if-a-number-is-majority-element-in-a-sorted-array.js rename to 1150.check-if-a-number-is-majority-element-in-a-sorted-array.js diff --git a/1151-minimum-swaps-to-group-all-1s-together.js b/1151.minimum-swaps-to-group-all-1s-together.js similarity index 100% rename from 1151-minimum-swaps-to-group-all-1s-together.js rename to 1151.minimum-swaps-to-group-all-1s-together.js diff --git a/1153-string-transforms-into-another-string.js b/1153.string-transforms-into-another-string.js similarity index 100% rename from 1153-string-transforms-into-another-string.js rename to 1153.string-transforms-into-another-string.js diff --git a/1154-day-of-the-year.js b/1154.day-of-the-year.js similarity index 100% rename from 1154-day-of-the-year.js rename to 1154.day-of-the-year.js diff --git a/1155-number-of-dice-rolls-with-target-sum.js b/1155.number-of-dice-rolls-with-target-sum.js similarity index 100% rename from 1155-number-of-dice-rolls-with-target-sum.js rename to 1155.number-of-dice-rolls-with-target-sum.js diff --git a/1156-swap-for-longest-repeated-character-substring.js b/1156.swap-for-longest-repeated-character-substring.js similarity index 100% rename from 1156-swap-for-longest-repeated-character-substring.js rename to 1156.swap-for-longest-repeated-character-substring.js diff --git a/1157-online-majority-element-in-subarray.js b/1157.online-majority-element-in-subarray.js similarity index 100% rename from 1157-online-majority-element-in-subarray.js rename to 1157.online-majority-element-in-subarray.js diff --git a/116-populating-next-right-pointers-in-each-node.js b/116.populating-next-right-pointers-in-each-node.js similarity index 100% rename from 116-populating-next-right-pointers-in-each-node.js rename to 116.populating-next-right-pointers-in-each-node.js diff --git a/1160-find-words-that-can-be-formed-by-characters.js b/1160.find-words-that-can-be-formed-by-characters.js similarity index 100% rename from 1160-find-words-that-can-be-formed-by-characters.js rename to 1160.find-words-that-can-be-formed-by-characters.js diff --git a/1161-maximum-level-sum-of-a-binary-tree.js b/1161.maximum-level-sum-of-a-binary-tree.js similarity index 100% rename from 1161-maximum-level-sum-of-a-binary-tree.js rename to 1161.maximum-level-sum-of-a-binary-tree.js diff --git a/1162-as-far-from-land-as-possible.js b/1162.as-far-from-land-as-possible.js similarity index 100% rename from 1162-as-far-from-land-as-possible.js rename to 1162.as-far-from-land-as-possible.js diff --git a/1163-last-substring-in-lexicographical-order.js b/1163.last-substring-in-lexicographical-order.js similarity index 100% rename from 1163-last-substring-in-lexicographical-order.js rename to 1163.last-substring-in-lexicographical-order.js diff --git a/1165-single-row-keyboard.js b/1165.single-row-keyboard.js similarity index 100% rename from 1165-single-row-keyboard.js rename to 1165.single-row-keyboard.js diff --git a/1166-design-file-system.js b/1166.design-file-system.js similarity index 100% rename from 1166-design-file-system.js rename to 1166.design-file-system.js diff --git a/1167-minimum-cost-to-connect-sticks.js b/1167.minimum-cost-to-connect-sticks.js similarity index 100% rename from 1167-minimum-cost-to-connect-sticks.js rename to 1167.minimum-cost-to-connect-sticks.js diff --git a/1168-optimize-water-distribution-in-a-village.js b/1168.optimize-water-distribution-in-a-village.js similarity index 100% rename from 1168-optimize-water-distribution-in-a-village.js rename to 1168.optimize-water-distribution-in-a-village.js diff --git a/117-populating-next-right-pointers-in-each-node-ii.js b/117.populating-next-right-pointers-in-each-node-ii.js similarity index 100% rename from 117-populating-next-right-pointers-in-each-node-ii.js rename to 117.populating-next-right-pointers-in-each-node-ii.js diff --git a/1170-compare-strings-by-frequency-of-the-smallest-character.js b/1170.compare-strings-by-frequency-of-the-smallest-character.js similarity index 100% rename from 1170-compare-strings-by-frequency-of-the-smallest-character.js rename to 1170.compare-strings-by-frequency-of-the-smallest-character.js diff --git a/1171-remove-zero-sum-consecutive-nodes-from-linked-list.js b/1171.remove-zero-sum-consecutive-nodes-from-linked-list.js similarity index 100% rename from 1171-remove-zero-sum-consecutive-nodes-from-linked-list.js rename to 1171.remove-zero-sum-consecutive-nodes-from-linked-list.js diff --git a/1172-dinner-plate-stacks.js b/1172.dinner-plate-stacks.js similarity index 100% rename from 1172-dinner-plate-stacks.js rename to 1172.dinner-plate-stacks.js diff --git a/1175-prime-arrangements.js b/1175.prime-arrangements.js similarity index 100% rename from 1175-prime-arrangements.js rename to 1175.prime-arrangements.js diff --git a/1176-diet-plan-performance.js b/1176.diet-plan-performance.js similarity index 100% rename from 1176-diet-plan-performance.js rename to 1176.diet-plan-performance.js diff --git a/1177-can-make-palindrome-from-substring.js b/1177.can-make-palindrome-from-substring.js similarity index 100% rename from 1177-can-make-palindrome-from-substring.js rename to 1177.can-make-palindrome-from-substring.js diff --git a/1178-number-of-valid-words-for-each-puzzle.js b/1178.number-of-valid-words-for-each-puzzle.js similarity index 100% rename from 1178-number-of-valid-words-for-each-puzzle.js rename to 1178.number-of-valid-words-for-each-puzzle.js diff --git a/118-pascal's-triangle.js b/118.pascal's-triangle.js old mode 100755 new mode 100644 similarity index 95% rename from 118-pascal's-triangle.js rename to 118.pascal's-triangle.js index 73fcb739..4709e64f --- a/118-pascal's-triangle.js +++ b/118.pascal's-triangle.js @@ -1,38 +1,38 @@ -/** - * @param {number} numRows - * @return {number[][]} - */ -const generate = function(numRows) { - // row 0 => [1] length 0 - // row 1 => [1, 1] length 1 - // row 2 => [1, 2, 1] length 2 - // row 3 => [1, 3, 3, 1] length 3 - - // current[i] = prev[i - 1] + prev[i] - - const res = []; - for (let row = 0; row < numRows; row += 1) { - if (row === 0) { - res.push([1]); - continue; - } - - if (row === 1) { - res.push([1, 1]); - continue; - } - - const newRow = []; - const maxIdx = row; - for (let i = 0; i <= maxIdx; i += 1) { - if (i === 0 || i === maxIdx) { - newRow.push(1); - } else { - newRow.push(res[row - 1][i - 1] + res[row - 1][i]); - } - } - res.push(newRow); - } - - return res; -}; +/** + * @param {number} numRows + * @return {number[][]} + */ +const generate = function(numRows) { + // row 0 => [1] length 0 + // row 1 => [1, 1] length 1 + // row 2 => [1, 2, 1] length 2 + // row 3 => [1, 3, 3, 1] length 3 + + // current[i] = prev[i - 1] + prev[i] + + const res = []; + for (let row = 0; row < numRows; row += 1) { + if (row === 0) { + res.push([1]); + continue; + } + + if (row === 1) { + res.push([1, 1]); + continue; + } + + const newRow = []; + const maxIdx = row; + for (let i = 0; i <= maxIdx; i += 1) { + if (i === 0 || i === maxIdx) { + newRow.push(1); + } else { + newRow.push(res[row - 1][i - 1] + res[row - 1][i]); + } + } + res.push(newRow); + } + + return res; +}; diff --git a/1180-count-substrings-with-only-one-distinct-letter.js b/1180.count-substrings-with-only-one-distinct-letter.js similarity index 100% rename from 1180-count-substrings-with-only-one-distinct-letter.js rename to 1180.count-substrings-with-only-one-distinct-letter.js diff --git a/1181-before-and-after-puzzle.js b/1181.before-and-after-puzzle.js similarity index 100% rename from 1181-before-and-after-puzzle.js rename to 1181.before-and-after-puzzle.js diff --git a/1183-maximum-number-of-ones.js b/1183.maximum-number-of-ones.js similarity index 100% rename from 1183-maximum-number-of-ones.js rename to 1183.maximum-number-of-ones.js diff --git a/1184-distance-between-bus-stops.js b/1184.distance-between-bus-stops.js similarity index 100% rename from 1184-distance-between-bus-stops.js rename to 1184.distance-between-bus-stops.js diff --git a/1185-day-of-the-week.js b/1185.day-of-the-week.js similarity index 100% rename from 1185-day-of-the-week.js rename to 1185.day-of-the-week.js diff --git a/1186-maximum-subarray-sum-with-one-deletion.js b/1186.maximum-subarray-sum-with-one-deletion.js similarity index 100% rename from 1186-maximum-subarray-sum-with-one-deletion.js rename to 1186.maximum-subarray-sum-with-one-deletion.js diff --git a/1187-make-array-strictly-increasing.js b/1187.make-array-strictly-increasing.js similarity index 100% rename from 1187-make-array-strictly-increasing.js rename to 1187.make-array-strictly-increasing.js diff --git a/1189-maximum-number-of-balloons.js b/1189.maximum-number-of-balloons.js similarity index 100% rename from 1189-maximum-number-of-balloons.js rename to 1189.maximum-number-of-balloons.js diff --git a/119-pascal's-triangle-II.js b/119.pascal's-triangle-II.js old mode 100755 new mode 100644 similarity index 95% rename from 119-pascal's-triangle-II.js rename to 119.pascal's-triangle-II.js index 1f39e402..f32c758c --- a/119-pascal's-triangle-II.js +++ b/119.pascal's-triangle-II.js @@ -1,16 +1,16 @@ -/** - * @param {number} rowIndex - * @return {number[]} - */ -const getRow = function(rowIndex) { - if (!rowIndex) return [1]; - if (rowIndex === 1) return [1, 1]; - const res = [1, 1]; - for (let i = 2; i <= rowIndex; i++) { - res[i] = 1; - for (let j = i - 1; j >= 1; j--) { - res[j] = res[j] + res[j - 1]; - } - } - return res; -}; +/** + * @param {number} rowIndex + * @return {number[]} + */ +const getRow = function(rowIndex) { + if (!rowIndex) return [1]; + if (rowIndex === 1) return [1, 1]; + const res = [1, 1]; + for (let i = 2; i <= rowIndex; i++) { + res[i] = 1; + for (let j = i - 1; j >= 1; j--) { + res[j] = res[j] + res[j - 1]; + } + } + return res; +}; diff --git a/1190-reverse-substrings-between-each-pair-of-parentheses.js b/1190.reverse-substrings-between-each-pair-of-parentheses.js similarity index 100% rename from 1190-reverse-substrings-between-each-pair-of-parentheses.js rename to 1190.reverse-substrings-between-each-pair-of-parentheses.js diff --git a/1191-k-concatenation-maximum-sum.js b/1191.k-concatenation-maximum-sum.js similarity index 100% rename from 1191-k-concatenation-maximum-sum.js rename to 1191.k-concatenation-maximum-sum.js diff --git a/1192-critical-connections-in-a-network.js b/1192.critical-connections-in-a-network.js similarity index 100% rename from 1192-critical-connections-in-a-network.js rename to 1192.critical-connections-in-a-network.js diff --git a/1199-minimum-time-to-build-blocks.js b/1199.minimum-time-to-build-blocks.js similarity index 100% rename from 1199-minimum-time-to-build-blocks.js rename to 1199.minimum-time-to-build-blocks.js diff --git a/12-integer-to-roman.js b/12.integer-to-roman.js old mode 100755 new mode 100644 similarity index 94% rename from 12-integer-to-roman.js rename to 12.integer-to-roman.js index 1f096b9c..2404dd6a --- a/12-integer-to-roman.js +++ b/12.integer-to-roman.js @@ -1,32 +1,32 @@ -/** - * @param {number} num - * @return {string} - */ -const map = { - "1000": "M", - "900": "CM", - "500": "D", - "400": "CD", - "100": "C", - "90": "XC", - "50": "L", - "40": "XL", - "10": "X", - "9": "IX", - "5": "V", - "4": "IV", - "1": "I" -}; -const intToRoman = function(number) { - const l = fkey(map, number); - if (number == +l) { - return map[number]; - } - return map[l] + intToRoman(number - +l); -}; - -function fkey(m, num) { - const keys = Object.keys(m); - const sArr = keys.filter(el => +el <= num); - return +Math.max.apply(Math, sArr); -} +/** + * @param {number} num + * @return {string} + */ +const map = { + "1000": "M", + "900": "CM", + "500": "D", + "400": "CD", + "100": "C", + "90": "XC", + "50": "L", + "40": "XL", + "10": "X", + "9": "IX", + "5": "V", + "4": "IV", + "1": "I" +}; +const intToRoman = function(number) { + const l = fkey(map, number); + if (number == +l) { + return map[number]; + } + return map[l] + intToRoman(number - +l); +}; + +function fkey(m, num) { + const keys = Object.keys(m); + const sArr = keys.filter(el => +el <= num); + return +Math.max.apply(Math, sArr); +} diff --git a/120-triangle.js b/120.triangle.js similarity index 100% rename from 120-triangle.js rename to 120.triangle.js diff --git a/1200-minimum-absolute-difference.js b/1200.minimum-absolute-difference.js similarity index 100% rename from 1200-minimum-absolute-difference.js rename to 1200.minimum-absolute-difference.js diff --git a/1201-ugly-number-iii.js b/1201.ugly-number-iii.js similarity index 100% rename from 1201-ugly-number-iii.js rename to 1201.ugly-number-iii.js diff --git a/1202-smallest-string-with-swaps.js b/1202.smallest-string-with-swaps.js similarity index 100% rename from 1202-smallest-string-with-swaps.js rename to 1202.smallest-string-with-swaps.js diff --git a/1203-sort-items-by-groups-respecting-dependencies.js b/1203.sort-items-by-groups-respecting-dependencies.js similarity index 100% rename from 1203-sort-items-by-groups-respecting-dependencies.js rename to 1203.sort-items-by-groups-respecting-dependencies.js diff --git a/1206-design-skiplist.js b/1206.design-skiplist.js similarity index 100% rename from 1206-design-skiplist.js rename to 1206.design-skiplist.js diff --git a/1207-unique-number-of-occurrences.js b/1207.unique-number-of-occurrences.js similarity index 100% rename from 1207-unique-number-of-occurrences.js rename to 1207.unique-number-of-occurrences.js diff --git a/1209-remove-all-adjacent-duplicates-in-string-ii.js b/1209.remove-all-adjacent-duplicates-in-string-ii.js similarity index 100% rename from 1209-remove-all-adjacent-duplicates-in-string-ii.js rename to 1209.remove-all-adjacent-duplicates-in-string-ii.js diff --git a/121-best-time-to-buy-and-sell-stock.js b/121.best-time-to-buy-and-sell-stock.js old mode 100755 new mode 100644 similarity index 95% rename from 121-best-time-to-buy-and-sell-stock.js rename to 121.best-time-to-buy-and-sell-stock.js index 20f1f59b..f09630e2 --- a/121-best-time-to-buy-and-sell-stock.js +++ b/121.best-time-to-buy-and-sell-stock.js @@ -1,17 +1,17 @@ -/** - * @param {number[]} prices - * @return {number} - */ -const maxProfit = function(prices) { - let minPrice = Number.MAX_SAFE_INTEGER; - let maxP = 0; - for (let i = 0; i < prices.length; i++) { - if (prices[i] < minPrice) { - minPrice = prices[i]; - } else if (prices[i] - minPrice > maxP) { - maxP = prices[i] - minPrice; - } - } - return maxP; -}; - +/** + * @param {number[]} prices + * @return {number} + */ +const maxProfit = function(prices) { + let minPrice = Number.MAX_SAFE_INTEGER; + let maxP = 0; + for (let i = 0; i < prices.length; i++) { + if (prices[i] < minPrice) { + minPrice = prices[i]; + } else if (prices[i] - minPrice > maxP) { + maxP = prices[i] - minPrice; + } + } + return maxP; +}; + diff --git a/1210-minimum-moves-to-reach-target-with-rotations.js b/1210.minimum-moves-to-reach-target-with-rotations.js similarity index 100% rename from 1210-minimum-moves-to-reach-target-with-rotations.js rename to 1210.minimum-moves-to-reach-target-with-rotations.js diff --git a/1213-intersection-of-three-sorted-arrays.js b/1213.intersection-of-three-sorted-arrays.js similarity index 100% rename from 1213-intersection-of-three-sorted-arrays.js rename to 1213.intersection-of-three-sorted-arrays.js diff --git a/1214-two-sum-bsts.js b/1214.two-sum-bsts.js similarity index 100% rename from 1214-two-sum-bsts.js rename to 1214.two-sum-bsts.js diff --git a/1216-valid-palindrome-iii.js b/1216.valid-palindrome-iii.js similarity index 100% rename from 1216-valid-palindrome-iii.js rename to 1216.valid-palindrome-iii.js diff --git a/1217-minimum-cost-to-move-chips-to-the-same-position.js b/1217.minimum-cost-to-move-chips-to-the-same-position.js similarity index 100% rename from 1217-minimum-cost-to-move-chips-to-the-same-position.js rename to 1217.minimum-cost-to-move-chips-to-the-same-position.js diff --git a/1219-path-with-maximum-gold.js b/1219.path-with-maximum-gold.js similarity index 100% rename from 1219-path-with-maximum-gold.js rename to 1219.path-with-maximum-gold.js diff --git a/122-best-time-to-buy-and-sell-stock-ii.js b/122.best-time-to-buy-and-sell-stock-ii.js similarity index 100% rename from 122-best-time-to-buy-and-sell-stock-ii.js rename to 122.best-time-to-buy-and-sell-stock-ii.js diff --git a/1220-count-vowels-permutation.js b/1220.count-vowels-permutation.js similarity index 100% rename from 1220-count-vowels-permutation.js rename to 1220.count-vowels-permutation.js diff --git a/1221-split-a-string-in-balanced-strings.js b/1221.split-a-string-in-balanced-strings.js similarity index 100% rename from 1221-split-a-string-in-balanced-strings.js rename to 1221.split-a-string-in-balanced-strings.js diff --git a/1223-dice-roll-simulation.js b/1223.dice-roll-simulation.js similarity index 100% rename from 1223-dice-roll-simulation.js rename to 1223.dice-roll-simulation.js diff --git a/1224-maximum-equal-frequency.js b/1224.maximum-equal-frequency.js similarity index 100% rename from 1224-maximum-equal-frequency.js rename to 1224.maximum-equal-frequency.js diff --git a/1228-missing-number-in-arithmetic-progression.js b/1228.missing-number-in-arithmetic-progression.js similarity index 100% rename from 1228-missing-number-in-arithmetic-progression.js rename to 1228.missing-number-in-arithmetic-progression.js diff --git a/1229-meeting-scheduler.js b/1229.meeting-scheduler.js similarity index 100% rename from 1229-meeting-scheduler.js rename to 1229.meeting-scheduler.js diff --git a/123-best-time-to-buy-and-sell-stock-iii.js b/123.best-time-to-buy-and-sell-stock-iii.js similarity index 100% rename from 123-best-time-to-buy-and-sell-stock-iii.js rename to 123.best-time-to-buy-and-sell-stock-iii.js diff --git a/1231-divide-chocolate.js b/1231.divide-chocolate.js similarity index 100% rename from 1231-divide-chocolate.js rename to 1231.divide-chocolate.js diff --git a/1232-check-if-it-is-a-straight-line.js b/1232.check-if-it-is-a-straight-line.js similarity index 100% rename from 1232-check-if-it-is-a-straight-line.js rename to 1232.check-if-it-is-a-straight-line.js diff --git a/1235-maximum-profit-in-job-scheduling.js b/1235.maximum-profit-in-job-scheduling.js similarity index 100% rename from 1235-maximum-profit-in-job-scheduling.js rename to 1235.maximum-profit-in-job-scheduling.js diff --git a/1239-maximum-length-of-a-concatenated-string-with-unique-characters.js b/1239.maximum-length-of-a-concatenated-string-with-unique-characters.js similarity index 100% rename from 1239-maximum-length-of-a-concatenated-string-with-unique-characters.js rename to 1239.maximum-length-of-a-concatenated-string-with-unique-characters.js diff --git a/124-binary-tree-maximum-path-sum.js b/124.binary-tree-maximum-path-sum.js similarity index 100% rename from 124-binary-tree-maximum-path-sum.js rename to 124.binary-tree-maximum-path-sum.js diff --git a/1240-tiling-a-rectangle-with-the-fewest-squares.js b/1240.tiling-a-rectangle-with-the-fewest-squares.js similarity index 100% rename from 1240-tiling-a-rectangle-with-the-fewest-squares.js rename to 1240.tiling-a-rectangle-with-the-fewest-squares.js diff --git a/1243-array-transformation.js b/1243.array-transformation.js similarity index 100% rename from 1243-array-transformation.js rename to 1243.array-transformation.js diff --git a/1244-design-a-leaderboard.js b/1244.design-a-leaderboard.js similarity index 100% rename from 1244-design-a-leaderboard.js rename to 1244.design-a-leaderboard.js diff --git a/1245-tree-diameter.js b/1245.tree-diameter.js similarity index 100% rename from 1245-tree-diameter.js rename to 1245.tree-diameter.js diff --git a/1246-palindrome-removal.js b/1246.palindrome-removal.js similarity index 100% rename from 1246-palindrome-removal.js rename to 1246.palindrome-removal.js diff --git a/1247-minimum-swaps-to-make-strings-equal.js b/1247.minimum-swaps-to-make-strings-equal.js similarity index 100% rename from 1247-minimum-swaps-to-make-strings-equal.js rename to 1247.minimum-swaps-to-make-strings-equal.js diff --git a/1249-minimum-remove-to-make-valid-parentheses.js b/1249.minimum-remove-to-make-valid-parentheses.js similarity index 100% rename from 1249-minimum-remove-to-make-valid-parentheses.js rename to 1249.minimum-remove-to-make-valid-parentheses.js diff --git a/125-valid-palindrome.js b/125.valid-palindrome.js similarity index 100% rename from 125-valid-palindrome.js rename to 125.valid-palindrome.js diff --git a/1252-cells-with-odd-values-in-a-matrix.js b/1252.cells-with-odd-values-in-a-matrix.js similarity index 100% rename from 1252-cells-with-odd-values-in-a-matrix.js rename to 1252.cells-with-odd-values-in-a-matrix.js diff --git a/1254-number-of-closed-islands.js b/1254.number-of-closed-islands.js similarity index 100% rename from 1254-number-of-closed-islands.js rename to 1254.number-of-closed-islands.js diff --git a/1255-maximum-score-words-formed-by-letters.js b/1255.maximum-score-words-formed-by-letters.js similarity index 100% rename from 1255-maximum-score-words-formed-by-letters.js rename to 1255.maximum-score-words-formed-by-letters.js diff --git a/1257-smallest-common-region.js b/1257.smallest-common-region.js similarity index 100% rename from 1257-smallest-common-region.js rename to 1257.smallest-common-region.js diff --git a/126-word-ladder-ii.js b/126.word-ladder-ii.js similarity index 100% rename from 126-word-ladder-ii.js rename to 126.word-ladder-ii.js diff --git a/1260-shift-2d-grid.js b/1260.shift-2d-grid.js similarity index 100% rename from 1260-shift-2d-grid.js rename to 1260.shift-2d-grid.js diff --git a/1261-find-elements-in-a-contaminated-binary-tree.js b/1261.find-elements-in-a-contaminated-binary-tree.js similarity index 100% rename from 1261-find-elements-in-a-contaminated-binary-tree.js rename to 1261.find-elements-in-a-contaminated-binary-tree.js diff --git a/1262-greatest-sum-divisible-by-three.js b/1262.greatest-sum-divisible-by-three.js similarity index 100% rename from 1262-greatest-sum-divisible-by-three.js rename to 1262.greatest-sum-divisible-by-three.js diff --git a/1263-minimum-moves-to-move-a-box-to-their-target-location.js b/1263.minimum-moves-to-move-a-box-to-their-target-location.js similarity index 100% rename from 1263-minimum-moves-to-move-a-box-to-their-target-location.js rename to 1263.minimum-moves-to-move-a-box-to-their-target-location.js diff --git a/1265-print-immutable-linked-list-in-reverse.js b/1265.print-immutable-linked-list-in-reverse.js similarity index 100% rename from 1265-print-immutable-linked-list-in-reverse.js rename to 1265.print-immutable-linked-list-in-reverse.js diff --git a/1266-minimum-time-visiting-all-points.js b/1266.minimum-time-visiting-all-points.js similarity index 100% rename from 1266-minimum-time-visiting-all-points.js rename to 1266.minimum-time-visiting-all-points.js diff --git a/1268-search-suggestions-system.js b/1268.search-suggestions-system.js similarity index 100% rename from 1268-search-suggestions-system.js rename to 1268.search-suggestions-system.js diff --git a/1269-number-of-ways-to-stay-in-the-same-place-after-some-steps.js b/1269.number-of-ways-to-stay-in-the-same-place-after-some-steps.js similarity index 100% rename from 1269-number-of-ways-to-stay-in-the-same-place-after-some-steps.js rename to 1269.number-of-ways-to-stay-in-the-same-place-after-some-steps.js diff --git a/127-word-ladder.js b/127.word-ladder.js similarity index 100% rename from 127-word-ladder.js rename to 127.word-ladder.js diff --git a/1271-hexspeak.js b/1271.hexspeak.js similarity index 100% rename from 1271-hexspeak.js rename to 1271.hexspeak.js diff --git a/1272-remove-interval.js b/1272.remove-interval.js similarity index 100% rename from 1272-remove-interval.js rename to 1272.remove-interval.js diff --git a/1273-delete-tree-nodes.js b/1273.delete-tree-nodes.js similarity index 100% rename from 1273-delete-tree-nodes.js rename to 1273.delete-tree-nodes.js diff --git a/1274-number-of-ships-in-a-rectangle.js b/1274.number-of-ships-in-a-rectangle.js similarity index 100% rename from 1274-number-of-ships-in-a-rectangle.js rename to 1274.number-of-ships-in-a-rectangle.js diff --git a/1275-find-winner-on-a-tic-tac-toe-game.js b/1275.find-winner-on-a-tic-tac-toe-game.js similarity index 100% rename from 1275-find-winner-on-a-tic-tac-toe-game.js rename to 1275.find-winner-on-a-tic-tac-toe-game.js diff --git a/1277-count-square-submatrices-with-all-ones.js b/1277.count-square-submatrices-with-all-ones.js similarity index 100% rename from 1277-count-square-submatrices-with-all-ones.js rename to 1277.count-square-submatrices-with-all-ones.js diff --git a/1278-palindrome-partitioning-iii.js b/1278.palindrome-partitioning-iii.js similarity index 100% rename from 1278-palindrome-partitioning-iii.js rename to 1278.palindrome-partitioning-iii.js diff --git a/128-longest-consecutive-sequence.js b/128.longest-consecutive-sequence.js similarity index 100% rename from 128-longest-consecutive-sequence.js rename to 128.longest-consecutive-sequence.js diff --git a/1281-subtract-the-product-and-sum-of-digits-of-an-integer.js b/1281.subtract-the-product-and-sum-of-digits-of-an-integer.js similarity index 100% rename from 1281-subtract-the-product-and-sum-of-digits-of-an-integer.js rename to 1281.subtract-the-product-and-sum-of-digits-of-an-integer.js diff --git a/1282-group-the-people-given-the-group-size-they-belong-to.js b/1282.group-the-people-given-the-group-size-they-belong-to.js similarity index 100% rename from 1282-group-the-people-given-the-group-size-they-belong-to.js rename to 1282.group-the-people-given-the-group-size-they-belong-to.js diff --git a/1283-find-the-smallest-divisor-given-a-threshold.js b/1283.find-the-smallest-divisor-given-a-threshold.js similarity index 100% rename from 1283-find-the-smallest-divisor-given-a-threshold.js rename to 1283.find-the-smallest-divisor-given-a-threshold.js diff --git a/1284-minimum-number-of-flips-to-convert-binary-matrix-to-zero-matrix.js b/1284.minimum-number-of-flips-to-convert-binary-matrix-to-zero-matrix.js similarity index 100% rename from 1284-minimum-number-of-flips-to-convert-binary-matrix-to-zero-matrix.js rename to 1284.minimum-number-of-flips-to-convert-binary-matrix-to-zero-matrix.js diff --git a/1286-iterator-for-combination.js b/1286.iterator-for-combination.js similarity index 100% rename from 1286-iterator-for-combination.js rename to 1286.iterator-for-combination.js diff --git a/1287-element-appearing-more-than-25-in-sorted-array.js b/1287.element-appearing-more-than-25-in-sorted-array.js similarity index 100% rename from 1287-element-appearing-more-than-25-in-sorted-array.js rename to 1287.element-appearing-more-than-25-in-sorted-array.js diff --git a/1288-remove-covered-intervals.js b/1288.remove-covered-intervals.js similarity index 100% rename from 1288-remove-covered-intervals.js rename to 1288.remove-covered-intervals.js diff --git a/1289-minimum-falling-path-sum-ii.js b/1289.minimum-falling-path-sum-ii.js similarity index 100% rename from 1289-minimum-falling-path-sum-ii.js rename to 1289.minimum-falling-path-sum-ii.js diff --git a/129-sum-root-to-leaf-numbers.js b/129.sum-root-to-leaf-numbers.js similarity index 100% rename from 129-sum-root-to-leaf-numbers.js rename to 129.sum-root-to-leaf-numbers.js diff --git a/1290-convert-binary-number-in-a-linked-list-to-integer.js b/1290.convert-binary-number-in-a-linked-list-to-integer.js similarity index 100% rename from 1290-convert-binary-number-in-a-linked-list-to-integer.js rename to 1290.convert-binary-number-in-a-linked-list-to-integer.js diff --git a/1291-sequential-digits.js b/1291.sequential-digits.js similarity index 100% rename from 1291-sequential-digits.js rename to 1291.sequential-digits.js diff --git a/1292-maximum-side-length-of-a-square-with-sum-less-than-or-equal-to-threshold.js b/1292.maximum-side-length-of-a-square-with-sum-less-than-or-equal-to-threshold.js similarity index 100% rename from 1292-maximum-side-length-of-a-square-with-sum-less-than-or-equal-to-threshold.js rename to 1292.maximum-side-length-of-a-square-with-sum-less-than-or-equal-to-threshold.js diff --git a/1293-shortest-path-in-a-grid-with-obstacles-elimination.js b/1293.shortest-path-in-a-grid-with-obstacles-elimination.js similarity index 100% rename from 1293-shortest-path-in-a-grid-with-obstacles-elimination.js rename to 1293.shortest-path-in-a-grid-with-obstacles-elimination.js diff --git a/1295-find-numbers-with-even-number-of-digits.js b/1295.find-numbers-with-even-number-of-digits.js similarity index 100% rename from 1295-find-numbers-with-even-number-of-digits.js rename to 1295.find-numbers-with-even-number-of-digits.js diff --git a/1299-replace-elements-with-greatest-element-on-right-side.js b/1299.replace-elements-with-greatest-element-on-right-side.js similarity index 100% rename from 1299-replace-elements-with-greatest-element-on-right-side.js rename to 1299.replace-elements-with-greatest-element-on-right-side.js diff --git a/13-roman-to-integer.js b/13.roman-to-integer.js similarity index 100% rename from 13-roman-to-integer.js rename to 13.roman-to-integer.js diff --git a/130-surrounded-regions.js b/130.surrounded-regions.js similarity index 100% rename from 130-surrounded-regions.js rename to 130.surrounded-regions.js diff --git a/1300-sum-of-mutated-array-closest-to-target.js b/1300.sum-of-mutated-array-closest-to-target.js similarity index 100% rename from 1300-sum-of-mutated-array-closest-to-target.js rename to 1300.sum-of-mutated-array-closest-to-target.js diff --git a/1301-number-of-paths-with-max-score.js b/1301.number-of-paths-with-max-score.js similarity index 100% rename from 1301-number-of-paths-with-max-score.js rename to 1301.number-of-paths-with-max-score.js diff --git a/1302-deepest-leaves-sum.js b/1302.deepest-leaves-sum.js similarity index 100% rename from 1302-deepest-leaves-sum.js rename to 1302.deepest-leaves-sum.js diff --git a/1304-find-n-unique-integers-sum-up-to-zero.js b/1304.find-n-unique-integers-sum-up-to-zero.js similarity index 100% rename from 1304-find-n-unique-integers-sum-up-to-zero.js rename to 1304.find-n-unique-integers-sum-up-to-zero.js diff --git a/1305-all-elements-in-two-binary-search-trees.js b/1305.all-elements-in-two-binary-search-trees.js similarity index 100% rename from 1305-all-elements-in-two-binary-search-trees.js rename to 1305.all-elements-in-two-binary-search-trees.js diff --git a/1306-jump-game-iii.js b/1306.jump-game-iii.js similarity index 100% rename from 1306-jump-game-iii.js rename to 1306.jump-game-iii.js diff --git a/1307-verbal-arithmetic-puzzle.js b/1307.verbal-arithmetic-puzzle.js similarity index 100% rename from 1307-verbal-arithmetic-puzzle.js rename to 1307.verbal-arithmetic-puzzle.js diff --git a/1309-decrypt-string-from-alphabet-to-integer-mapping.js b/1309.decrypt-string-from-alphabet-to-integer-mapping.js similarity index 100% rename from 1309-decrypt-string-from-alphabet-to-integer-mapping.js rename to 1309.decrypt-string-from-alphabet-to-integer-mapping.js diff --git a/131-palindrome-partitioning.js b/131.palindrome-partitioning.js similarity index 100% rename from 131-palindrome-partitioning.js rename to 131.palindrome-partitioning.js diff --git a/1310-xor-queries-of-a-subarray.js b/1310.xor-queries-of-a-subarray.js similarity index 100% rename from 1310-xor-queries-of-a-subarray.js rename to 1310.xor-queries-of-a-subarray.js diff --git a/1312-minimum-insertion-steps-to-make-a-string-palindrome.js b/1312.minimum-insertion-steps-to-make-a-string-palindrome.js similarity index 100% rename from 1312-minimum-insertion-steps-to-make-a-string-palindrome.js rename to 1312.minimum-insertion-steps-to-make-a-string-palindrome.js diff --git a/1313-decompress-run-length-encoded-list.js b/1313.decompress-run-length-encoded-list.js similarity index 100% rename from 1313-decompress-run-length-encoded-list.js rename to 1313.decompress-run-length-encoded-list.js diff --git a/1314-matrix-block-sum.js b/1314.matrix-block-sum.js similarity index 100% rename from 1314-matrix-block-sum.js rename to 1314.matrix-block-sum.js diff --git a/1315-sum-of-nodes-with-even-valued-grandparent.js b/1315.sum-of-nodes-with-even-valued-grandparent.js similarity index 100% rename from 1315-sum-of-nodes-with-even-valued-grandparent.js rename to 1315.sum-of-nodes-with-even-valued-grandparent.js diff --git a/1316-distinct-echo-substrings.js b/1316.distinct-echo-substrings.js similarity index 100% rename from 1316-distinct-echo-substrings.js rename to 1316.distinct-echo-substrings.js diff --git a/132-palindrome-partitioning-ii.js b/132.palindrome-partitioning-ii.js similarity index 100% rename from 132-palindrome-partitioning-ii.js rename to 132.palindrome-partitioning-ii.js diff --git a/1320-minimum-distance-to-type-a-word-using-two-fingers.js b/1320.minimum-distance-to-type-a-word-using-two-fingers.js similarity index 100% rename from 1320-minimum-distance-to-type-a-word-using-two-fingers.js rename to 1320.minimum-distance-to-type-a-word-using-two-fingers.js diff --git a/1323-maximum-69-number.js b/1323.maximum-69-number.js similarity index 100% rename from 1323-maximum-69-number.js rename to 1323.maximum-69-number.js diff --git a/1324-print-words-vertically.js b/1324.print-words-vertically.js similarity index 100% rename from 1324-print-words-vertically.js rename to 1324.print-words-vertically.js diff --git a/1325-delete-leaves-with-a-given-value.js b/1325.delete-leaves-with-a-given-value.js similarity index 100% rename from 1325-delete-leaves-with-a-given-value.js rename to 1325.delete-leaves-with-a-given-value.js diff --git a/1326-minimum-number-of-taps-to-open-to-water-a-garden.js b/1326.minimum-number-of-taps-to-open-to-water-a-garden.js similarity index 100% rename from 1326-minimum-number-of-taps-to-open-to-water-a-garden.js rename to 1326.minimum-number-of-taps-to-open-to-water-a-garden.js diff --git a/1329-sort-the-matrix-diagonally.js b/1329.sort-the-matrix-diagonally.js similarity index 100% rename from 1329-sort-the-matrix-diagonally.js rename to 1329.sort-the-matrix-diagonally.js diff --git a/133-clone-graph.js b/133.clone-graph.js similarity index 100% rename from 133-clone-graph.js rename to 133.clone-graph.js diff --git a/1330-reverse-subarray-to-maximize-array-value.js b/1330.reverse-subarray-to-maximize-array-value.js similarity index 100% rename from 1330-reverse-subarray-to-maximize-array-value.js rename to 1330.reverse-subarray-to-maximize-array-value.js diff --git a/1331-rank-transform-of-an-array.js b/1331.rank-transform-of-an-array.js similarity index 100% rename from 1331-rank-transform-of-an-array.js rename to 1331.rank-transform-of-an-array.js diff --git a/1332-remove-palindromic-subsequences.js b/1332.remove-palindromic-subsequences.js similarity index 100% rename from 1332-remove-palindromic-subsequences.js rename to 1332.remove-palindromic-subsequences.js diff --git a/1335-minimum-difficulty-of-a-job-schedule.js b/1335.minimum-difficulty-of-a-job-schedule.js similarity index 100% rename from 1335-minimum-difficulty-of-a-job-schedule.js rename to 1335.minimum-difficulty-of-a-job-schedule.js diff --git a/1337-the-k-weakest-rows-in-a-matrix.js b/1337.the-k-weakest-rows-in-a-matrix.js similarity index 100% rename from 1337-the-k-weakest-rows-in-a-matrix.js rename to 1337.the-k-weakest-rows-in-a-matrix.js diff --git a/1338-reduce-array-size-to-the-half.js b/1338.reduce-array-size-to-the-half.js similarity index 100% rename from 1338-reduce-array-size-to-the-half.js rename to 1338.reduce-array-size-to-the-half.js diff --git a/134-gas-station.js b/134.gas-station.js similarity index 100% rename from 134-gas-station.js rename to 134.gas-station.js diff --git a/1340-jump-game-v.js b/1340.jump-game-v.js similarity index 100% rename from 1340-jump-game-v.js rename to 1340.jump-game-v.js diff --git a/1342-number-of-steps-to-reduce-a-number-to-zero.js b/1342.number-of-steps-to-reduce-a-number-to-zero.js similarity index 100% rename from 1342-number-of-steps-to-reduce-a-number-to-zero.js rename to 1342.number-of-steps-to-reduce-a-number-to-zero.js diff --git a/1343-number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold.js b/1343.number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold.js similarity index 100% rename from 1343-number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold.js rename to 1343.number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold.js diff --git a/1344-angle-between-hands-of-a-clock.js b/1344.angle-between-hands-of-a-clock.js similarity index 100% rename from 1344-angle-between-hands-of-a-clock.js rename to 1344.angle-between-hands-of-a-clock.js diff --git a/1345-jump-game-iv.js b/1345.jump-game-iv.js similarity index 100% rename from 1345-jump-game-iv.js rename to 1345.jump-game-iv.js diff --git a/1347-minimum-number-of-steps-to-make-two-strings-anagram.js b/1347.minimum-number-of-steps-to-make-two-strings-anagram.js similarity index 100% rename from 1347-minimum-number-of-steps-to-make-two-strings-anagram.js rename to 1347.minimum-number-of-steps-to-make-two-strings-anagram.js diff --git a/1348-tweet-counts-per-frequency.js b/1348.tweet-counts-per-frequency.js similarity index 100% rename from 1348-tweet-counts-per-frequency.js rename to 1348.tweet-counts-per-frequency.js diff --git a/1349-maximum-students-taking-exam.js b/1349.maximum-students-taking-exam.js similarity index 100% rename from 1349-maximum-students-taking-exam.js rename to 1349.maximum-students-taking-exam.js diff --git a/135-candy.js b/135.candy.js similarity index 100% rename from 135-candy.js rename to 135.candy.js diff --git a/1351-count-negative-numbers-in-a-sorted-matrix.js b/1351.count-negative-numbers-in-a-sorted-matrix.js similarity index 100% rename from 1351-count-negative-numbers-in-a-sorted-matrix.js rename to 1351.count-negative-numbers-in-a-sorted-matrix.js diff --git a/1352-product-of-the-last-k-numbers.js b/1352.product-of-the-last-k-numbers.js similarity index 100% rename from 1352-product-of-the-last-k-numbers.js rename to 1352.product-of-the-last-k-numbers.js diff --git a/1353-maximum-number-of-events-that-can-be-attended.js b/1353.maximum-number-of-events-that-can-be-attended.js similarity index 100% rename from 1353-maximum-number-of-events-that-can-be-attended.js rename to 1353.maximum-number-of-events-that-can-be-attended.js diff --git a/1354-construct-target-array-with-multiple-sums.js b/1354.construct-target-array-with-multiple-sums.js similarity index 100% rename from 1354-construct-target-array-with-multiple-sums.js rename to 1354.construct-target-array-with-multiple-sums.js diff --git a/1356-sort-integers-by-the-number-of-1-bits.js b/1356.sort-integers-by-the-number-of-1-bits.js similarity index 100% rename from 1356-sort-integers-by-the-number-of-1-bits.js rename to 1356.sort-integers-by-the-number-of-1-bits.js diff --git a/1359-count-all-valid-pickup-and-delivery-options.js b/1359.count-all-valid-pickup-and-delivery-options.js similarity index 100% rename from 1359-count-all-valid-pickup-and-delivery-options.js rename to 1359.count-all-valid-pickup-and-delivery-options.js diff --git a/136-single-number.js b/136.single-number.js similarity index 100% rename from 136-single-number.js rename to 136.single-number.js diff --git a/1360-number-of-days-between-two-dates.js b/1360.number-of-days-between-two-dates.js similarity index 100% rename from 1360-number-of-days-between-two-dates.js rename to 1360.number-of-days-between-two-dates.js diff --git a/1363-largest-multiple-of-three.js b/1363.largest-multiple-of-three.js similarity index 100% rename from 1363-largest-multiple-of-three.js rename to 1363.largest-multiple-of-three.js diff --git a/1365-how-many-numbers-are-smaller-than-the-current-number.js b/1365.how-many-numbers-are-smaller-than-the-current-number.js similarity index 100% rename from 1365-how-many-numbers-are-smaller-than-the-current-number.js rename to 1365.how-many-numbers-are-smaller-than-the-current-number.js diff --git a/1366-rank-teams-by-votes.js b/1366.rank-teams-by-votes.js similarity index 100% rename from 1366-rank-teams-by-votes.js rename to 1366.rank-teams-by-votes.js diff --git a/1367-linked-list-in-binary-tree.js b/1367.linked-list-in-binary-tree.js similarity index 100% rename from 1367-linked-list-in-binary-tree.js rename to 1367.linked-list-in-binary-tree.js diff --git a/1368-minimum-cost-to-make-at-least-one-valid-path-in-a-grid.js b/1368.minimum-cost-to-make-at-least-one-valid-path-in-a-grid.js similarity index 100% rename from 1368-minimum-cost-to-make-at-least-one-valid-path-in-a-grid.js rename to 1368.minimum-cost-to-make-at-least-one-valid-path-in-a-grid.js diff --git a/137-single-number-ii.js b/137.single-number-ii.js similarity index 100% rename from 137-single-number-ii.js rename to 137.single-number-ii.js diff --git a/1370-increasing-decreasing-string.js b/1370.increasing-decreasing-string.js similarity index 100% rename from 1370-increasing-decreasing-string.js rename to 1370.increasing-decreasing-string.js diff --git a/1371-find-the-longest-substring-containing-vowels-in-even-counts.js b/1371.find-the-longest-substring-containing-vowels-in-even-counts.js similarity index 100% rename from 1371-find-the-longest-substring-containing-vowels-in-even-counts.js rename to 1371.find-the-longest-substring-containing-vowels-in-even-counts.js diff --git a/1373-maximum-sum-bst-in-binary-tree.js b/1373.maximum-sum-bst-in-binary-tree.js similarity index 100% rename from 1373-maximum-sum-bst-in-binary-tree.js rename to 1373.maximum-sum-bst-in-binary-tree.js diff --git a/1374-generate-a-string-with-characters-that-have-odd-counts.js b/1374.generate-a-string-with-characters-that-have-odd-counts.js similarity index 100% rename from 1374-generate-a-string-with-characters-that-have-odd-counts.js rename to 1374.generate-a-string-with-characters-that-have-odd-counts.js diff --git a/1375-number-of-times-binary-string-is-prefix-aligned.js b/1375.number-of-times-binary-string-is-prefix-aligned.js similarity index 100% rename from 1375-number-of-times-binary-string-is-prefix-aligned.js rename to 1375.number-of-times-binary-string-is-prefix-aligned.js diff --git a/1376-time-needed-to-inform-all-employees.js b/1376.time-needed-to-inform-all-employees.js similarity index 100% rename from 1376-time-needed-to-inform-all-employees.js rename to 1376.time-needed-to-inform-all-employees.js diff --git a/1377-frog-position-after-t-seconds.js b/1377.frog-position-after-t-seconds.js similarity index 100% rename from 1377-frog-position-after-t-seconds.js rename to 1377.frog-position-after-t-seconds.js diff --git a/138-copy-list-with-random-pointer.js b/138.copy-list-with-random-pointer.js similarity index 100% rename from 138-copy-list-with-random-pointer.js rename to 138.copy-list-with-random-pointer.js diff --git a/1380-lucky-numbers-in-a-matrix.js b/1380.lucky-numbers-in-a-matrix.js similarity index 100% rename from 1380-lucky-numbers-in-a-matrix.js rename to 1380.lucky-numbers-in-a-matrix.js diff --git a/1381-design-a-stack-with-increment-operation.js b/1381.design-a-stack-with-increment-operation.js similarity index 100% rename from 1381-design-a-stack-with-increment-operation.js rename to 1381.design-a-stack-with-increment-operation.js diff --git a/1382-balance-a-binary-search-tree.js b/1382.balance-a-binary-search-tree.js similarity index 100% rename from 1382-balance-a-binary-search-tree.js rename to 1382.balance-a-binary-search-tree.js diff --git a/1383-maximum-performance-of-a-team.js b/1383.maximum-performance-of-a-team.js similarity index 100% rename from 1383-maximum-performance-of-a-team.js rename to 1383.maximum-performance-of-a-team.js diff --git a/1385-find-the-distance-value-between-two-arrays.js b/1385.find-the-distance-value-between-two-arrays.js similarity index 100% rename from 1385-find-the-distance-value-between-two-arrays.js rename to 1385.find-the-distance-value-between-two-arrays.js diff --git a/1387-sort-integers-by-the-power-value.js b/1387.sort-integers-by-the-power-value.js similarity index 100% rename from 1387-sort-integers-by-the-power-value.js rename to 1387.sort-integers-by-the-power-value.js diff --git a/1388-pizza-with-3n-slices.js b/1388.pizza-with-3n-slices.js similarity index 100% rename from 1388-pizza-with-3n-slices.js rename to 1388.pizza-with-3n-slices.js diff --git a/1389-create-target-array-in-the-given-order.js b/1389.create-target-array-in-the-given-order.js similarity index 100% rename from 1389-create-target-array-in-the-given-order.js rename to 1389.create-target-array-in-the-given-order.js diff --git a/139-word-break.js b/139.word-break.js similarity index 100% rename from 139-word-break.js rename to 139.word-break.js diff --git a/1390-four-divisors.js b/1390.four-divisors.js similarity index 100% rename from 1390-four-divisors.js rename to 1390.four-divisors.js diff --git a/1392-longest-happy-prefix.js b/1392.longest-happy-prefix.js similarity index 100% rename from 1392-longest-happy-prefix.js rename to 1392.longest-happy-prefix.js diff --git a/1394-find-lucky-integer-in-an-array.js b/1394.find-lucky-integer-in-an-array.js similarity index 100% rename from 1394-find-lucky-integer-in-an-array.js rename to 1394.find-lucky-integer-in-an-array.js diff --git a/1395-count-number-of-teams.js b/1395.count-number-of-teams.js similarity index 100% rename from 1395-count-number-of-teams.js rename to 1395.count-number-of-teams.js diff --git a/1396-count-number-of-teams.js b/1396.count-number-of-teams.js similarity index 100% rename from 1396-count-number-of-teams.js rename to 1396.count-number-of-teams.js diff --git a/1396-design-underground-system.js b/1396.design-underground-system.js similarity index 100% rename from 1396-design-underground-system.js rename to 1396.design-underground-system.js diff --git a/1397-find-all-good-strings.js b/1397.find-all-good-strings.js similarity index 100% rename from 1397-find-all-good-strings.js rename to 1397.find-all-good-strings.js diff --git a/1399-count-largest-group.js b/1399.count-largest-group.js similarity index 100% rename from 1399-count-largest-group.js rename to 1399.count-largest-group.js diff --git a/14-longest-common-prefix.js b/14.longest-common-prefix.js old mode 100755 new mode 100644 similarity index 95% rename from 14-longest-common-prefix.js rename to 14.longest-common-prefix.js index e3d757f2..29f07093 --- a/14-longest-common-prefix.js +++ b/14.longest-common-prefix.js @@ -1,13 +1,13 @@ -/** - * @param {string[]} strs - * @return {string} - */ -const longestCommonPrefix = function(strs) { - const A = strs.concat().sort(), - a1 = A[0] || "", - a2 = A[A.length - 1] || "", - L = a1.length, - i = 0; - while (i < L && a1.charAt(i) === a2.charAt(i)) i++; - return a1.substring(0, i); -}; +/** + * @param {string[]} strs + * @return {string} + */ +const longestCommonPrefix = function(strs) { + const A = strs.concat().sort(), + a1 = A[0] || "", + a2 = A[A.length - 1] || "", + L = a1.length, + i = 0; + while (i < L && a1.charAt(i) === a2.charAt(i)) i++; + return a1.substring(0, i); +}; diff --git a/140-word-break-ii.js b/140.word-break-ii.js similarity index 100% rename from 140-word-break-ii.js rename to 140.word-break-ii.js diff --git a/1402-reducing-dishes.js b/1402.reducing-dishes.js similarity index 100% rename from 1402-reducing-dishes.js rename to 1402.reducing-dishes.js diff --git a/1404-number-of-steps-to-reduce-a-number-in-binary-representation-to-one.js b/1404.number-of-steps-to-reduce-a-number-in-binary-representation-to-one.js similarity index 100% rename from 1404-number-of-steps-to-reduce-a-number-in-binary-representation-to-one.js rename to 1404.number-of-steps-to-reduce-a-number-in-binary-representation-to-one.js diff --git a/1405-longest-happy-string.js b/1405.longest-happy-string.js similarity index 100% rename from 1405-longest-happy-string.js rename to 1405.longest-happy-string.js diff --git a/1406-stone-game-iii.js b/1406.stone-game-iii.js similarity index 100% rename from 1406-stone-game-iii.js rename to 1406.stone-game-iii.js diff --git a/1408-string-matching-in-an-array.js b/1408.string-matching-in-an-array.js similarity index 100% rename from 1408-string-matching-in-an-array.js rename to 1408.string-matching-in-an-array.js diff --git a/1409-queries-on-a-permutation-with-key.js b/1409.queries-on-a-permutation-with-key.js similarity index 100% rename from 1409-queries-on-a-permutation-with-key.js rename to 1409.queries-on-a-permutation-with-key.js diff --git a/141-linked-list-cycle.js b/141.linked-list-cycle.js similarity index 100% rename from 141-linked-list-cycle.js rename to 141.linked-list-cycle.js diff --git a/1410-html-entity-parser.js b/1410.html-entity-parser.js similarity index 100% rename from 1410-html-entity-parser.js rename to 1410.html-entity-parser.js diff --git a/1411-number-of-ways-to-paint-n-3-grid.js b/1411.number-of-ways-to-paint-n-3-grid.js similarity index 100% rename from 1411-number-of-ways-to-paint-n-3-grid.js rename to 1411.number-of-ways-to-paint-n-3-grid.js diff --git a/1413-minimum-value-to-get-positive-step-by-step-sum.js b/1413.minimum-value-to-get-positive-step-by-step-sum.js similarity index 100% rename from 1413-minimum-value-to-get-positive-step-by-step-sum.js rename to 1413.minimum-value-to-get-positive-step-by-step-sum.js diff --git a/1415-the-k-th-lexicographical-string-of-all-happy-strings-of-length-n.js b/1415.the-k-th-lexicographical-string-of-all-happy-strings-of-length-n.js similarity index 100% rename from 1415-the-k-th-lexicographical-string-of-all-happy-strings-of-length-n.js rename to 1415.the-k-th-lexicographical-string-of-all-happy-strings-of-length-n.js diff --git a/1416-restore-the-array.js b/1416.restore-the-array.js similarity index 100% rename from 1416-restore-the-array.js rename to 1416.restore-the-array.js diff --git a/1417-reformat-the-string.js b/1417.reformat-the-string.js similarity index 100% rename from 1417-reformat-the-string.js rename to 1417.reformat-the-string.js diff --git a/142-linked-list-cycle-ii.js b/142.linked-list-cycle-ii.js similarity index 100% rename from 142-linked-list-cycle-ii.js rename to 142.linked-list-cycle-ii.js diff --git a/1422-maximum-score-after-splitting-a-string.js b/1422.maximum-score-after-splitting-a-string.js similarity index 100% rename from 1422-maximum-score-after-splitting-a-string.js rename to 1422.maximum-score-after-splitting-a-string.js diff --git a/1424-diagonal-traverse-ii.js b/1424.diagonal-traverse-ii.js similarity index 100% rename from 1424-diagonal-traverse-ii.js rename to 1424.diagonal-traverse-ii.js diff --git a/1425-constrained-subsequence-sum.js b/1425.constrained-subsequence-sum.js similarity index 100% rename from 1425-constrained-subsequence-sum.js rename to 1425.constrained-subsequence-sum.js diff --git a/1429-build-array-where-you-can-find-the-maximum-exactly-k-comparisons.js b/1429.build-array-where-you-can-find-the-maximum-exactly-k-comparisons.js similarity index 100% rename from 1429-build-array-where-you-can-find-the-maximum-exactly-k-comparisons.js rename to 1429.build-array-where-you-can-find-the-maximum-exactly-k-comparisons.js diff --git a/143-reorder-list.js b/143.reorder-list.js similarity index 100% rename from 143-reorder-list.js rename to 143.reorder-list.js diff --git a/1431-kids-with-the-greatest-number-of-candies.js b/1431.kids-with-the-greatest-number-of-candies.js similarity index 100% rename from 1431-kids-with-the-greatest-number-of-candies.js rename to 1431.kids-with-the-greatest-number-of-candies.js diff --git a/1434-number-of-ways-to-wear-different-hats-to-each-other.js b/1434.number-of-ways-to-wear-different-hats-to-each-other.js similarity index 100% rename from 1434-number-of-ways-to-wear-different-hats-to-each-other.js rename to 1434.number-of-ways-to-wear-different-hats-to-each-other.js diff --git a/1436-destination-city.js b/1436.destination-city.js similarity index 100% rename from 1436-destination-city.js rename to 1436.destination-city.js diff --git a/1438-longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit.js b/1438.longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit.js similarity index 100% rename from 1438-longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit.js rename to 1438.longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit.js diff --git a/1439-find-the-kth-smallest-sum-of-a-matrix-with-sorted-rows.js b/1439.find-the-kth-smallest-sum-of-a-matrix-with-sorted-rows.js similarity index 100% rename from 1439-find-the-kth-smallest-sum-of-a-matrix-with-sorted-rows.js rename to 1439.find-the-kth-smallest-sum-of-a-matrix-with-sorted-rows.js diff --git a/144-binary-tree-preorder-traversal.js b/144.binary-tree-preorder-traversal.js old mode 100755 new mode 100644 similarity index 94% rename from 144-binary-tree-preorder-traversal.js rename to 144.binary-tree-preorder-traversal.js index a15f046d..ed092298 --- a/144-binary-tree-preorder-traversal.js +++ b/144.binary-tree-preorder-traversal.js @@ -1,27 +1,27 @@ -/** - * Definition for a binary tree node. - * function TreeNode(val) { - * this.val = val; - * this.left = this.right = null; - * } - */ -/** - * @param {TreeNode} root - * @return {number[]} - */ -const preorderTraversal = function(root) { - const res = []; - traversal(root, res); - return res; -}; - -function traversal(node, arr) { - if (node === null) return; - arr.push(node.val); - if (node.left) { - traversal(node.left, arr); - } - if (node.right) { - traversal(node.right, arr); - } -} +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ +/** + * @param {TreeNode} root + * @return {number[]} + */ +const preorderTraversal = function(root) { + const res = []; + traversal(root, res); + return res; +}; + +function traversal(node, arr) { + if (node === null) return; + arr.push(node.val); + if (node.left) { + traversal(node.left, arr); + } + if (node.right) { + traversal(node.right, arr); + } +} diff --git a/1441-build-an-array-with-stack-operations.js b/1441.build-an-array-with-stack-operations.js similarity index 100% rename from 1441-build-an-array-with-stack-operations.js rename to 1441.build-an-array-with-stack-operations.js diff --git a/1442-count-triplets-that-can-form-two-arrays-of-equal-xor.js b/1442.count-triplets-that-can-form-two-arrays-of-equal-xor.js similarity index 100% rename from 1442-count-triplets-that-can-form-two-arrays-of-equal-xor.js rename to 1442.count-triplets-that-can-form-two-arrays-of-equal-xor.js diff --git a/1444-number-of-ways-of-cutting-a-pizza.js b/1444.number-of-ways-of-cutting-a-pizza.js similarity index 100% rename from 1444-number-of-ways-of-cutting-a-pizza.js rename to 1444.number-of-ways-of-cutting-a-pizza.js diff --git a/1446-consecutive-characters.js b/1446.consecutive-characters.js similarity index 100% rename from 1446-consecutive-characters.js rename to 1446.consecutive-characters.js diff --git a/1448-count-good-nodes-in-binary-tree.js b/1448.count-good-nodes-in-binary-tree.js similarity index 100% rename from 1448-count-good-nodes-in-binary-tree.js rename to 1448.count-good-nodes-in-binary-tree.js diff --git a/1449-form-largest-integer-with-digits-that-add-up-to-target.js b/1449.form-largest-integer-with-digits-that-add-up-to-target.js similarity index 100% rename from 1449-form-largest-integer-with-digits-that-add-up-to-target.js rename to 1449.form-largest-integer-with-digits-that-add-up-to-target.js diff --git a/145-binary-tree-postorder-traversal.js b/145.binary-tree-postorder-traversal.js similarity index 100% rename from 145-binary-tree-postorder-traversal.js rename to 145.binary-tree-postorder-traversal.js diff --git a/1452-people-whose-list-of-favorite-companies-is-not-a-subset-of-another-list.js b/1452.people-whose-list-of-favorite-companies-is-not-a-subset-of-another-list.js similarity index 100% rename from 1452-people-whose-list-of-favorite-companies-is-not-a-subset-of-another-list.js rename to 1452.people-whose-list-of-favorite-companies-is-not-a-subset-of-another-list.js diff --git a/1458-max-dot-product-of-two-subsequences.js b/1458.max-dot-product-of-two-subsequences.js similarity index 100% rename from 1458-max-dot-product-of-two-subsequences.js rename to 1458.max-dot-product-of-two-subsequences.js diff --git a/146-lru-cache.js b/146.lru-cache.js old mode 100755 new mode 100644 similarity index 95% rename from 146-lru-cache.js rename to 146.lru-cache.js index 96a6bb83..35f9e39a --- a/146-lru-cache.js +++ b/146.lru-cache.js @@ -1,83 +1,83 @@ -class Node { - constructor(key, val) { - this.val = val; - this.key = key; - this.next = this.pre = null; - } -} - -const LRUCache = function(capacity) { - this.capacity = capacity; - this.count = 0; - this.start = new Node(-1, -1); - this.end = new Node(-1, -1); - this.start.next = this.end; - this.end.pre = this.start; - this.map = {}; -}; - -// insert node into the next of the start -const insertAfter = function(start, node) { - let next = start.next; - start.next = node; - node.pre = start; - node.next = next; - next.pre = node; -}; - -const detach = function(node) { - let pre = node.pre, - next = node.next; - pre.next = next; - next.pre = pre; - node.next = node.pre = null; -}; - -/** - * @param {number} key - * @return {number} - */ -LRUCache.prototype.get = function(key) { - let node = this.map[key]; - if (node != undefined) { - detach(node); - insertAfter(this.start, node); - return node.val; - } else { - return -1; - } -}; - -/** - * @param {number} key - * @param {number} value - * @return {void} - */ -LRUCache.prototype.put = function(key, value) { - let node = this.map[key]; - if (!node) { - if (this.count == this.capacity) { - // deleting last nodes - let t = this.end.pre; - detach(t); - delete this.map[t.key]; - } else { - this.count++; - } - node = new Node(key, value); - this.map[key] = node; - insertAfter(this.start, node); - } else { - node.val = value; - detach(node); - insertAfter(this.start, node); - } -}; - -/** - * Your LRUCache object will be instantiated and called as such: - * var obj = Object.create(LRUCache).createNew(capacity) - * var param_1 = obj.get(key) - * obj.put(key,value) - */ - +class Node { + constructor(key, val) { + this.val = val; + this.key = key; + this.next = this.pre = null; + } +} + +const LRUCache = function(capacity) { + this.capacity = capacity; + this.count = 0; + this.start = new Node(-1, -1); + this.end = new Node(-1, -1); + this.start.next = this.end; + this.end.pre = this.start; + this.map = {}; +}; + +// insert node into the next of the start +const insertAfter = function(start, node) { + let next = start.next; + start.next = node; + node.pre = start; + node.next = next; + next.pre = node; +}; + +const detach = function(node) { + let pre = node.pre, + next = node.next; + pre.next = next; + next.pre = pre; + node.next = node.pre = null; +}; + +/** + * @param {number} key + * @return {number} + */ +LRUCache.prototype.get = function(key) { + let node = this.map[key]; + if (node != undefined) { + detach(node); + insertAfter(this.start, node); + return node.val; + } else { + return -1; + } +}; + +/** + * @param {number} key + * @param {number} value + * @return {void} + */ +LRUCache.prototype.put = function(key, value) { + let node = this.map[key]; + if (!node) { + if (this.count == this.capacity) { + // deleting last nodes + let t = this.end.pre; + detach(t); + delete this.map[t.key]; + } else { + this.count++; + } + node = new Node(key, value); + this.map[key] = node; + insertAfter(this.start, node); + } else { + node.val = value; + detach(node); + insertAfter(this.start, node); + } +}; + +/** + * Your LRUCache object will be instantiated and called as such: + * var obj = Object.create(LRUCache).createNew(capacity) + * var param_1 = obj.get(key) + * obj.put(key,value) + */ + diff --git a/1460-make-two-arrays-equal-by-reversing-sub-arrays.js b/1460.make-two-arrays-equal-by-reversing-sub-arrays.js similarity index 100% rename from 1460-make-two-arrays-equal-by-reversing-sub-arrays.js rename to 1460.make-two-arrays-equal-by-reversing-sub-arrays.js diff --git a/1461-check-if-a-string-contains-all-binary-codes-of-size-k.js b/1461.check-if-a-string-contains-all-binary-codes-of-size-k.js similarity index 100% rename from 1461-check-if-a-string-contains-all-binary-codes-of-size-k.js rename to 1461.check-if-a-string-contains-all-binary-codes-of-size-k.js diff --git a/1462-course-schedule-iv.js b/1462.course-schedule-iv.js similarity index 100% rename from 1462-course-schedule-iv.js rename to 1462.course-schedule-iv.js diff --git a/1463-cherry-pickup-ii.js b/1463.cherry-pickup-ii.js similarity index 100% rename from 1463-cherry-pickup-ii.js rename to 1463.cherry-pickup-ii.js diff --git a/1464-maximum-product-of-two-elements-in-an-array.js b/1464.maximum-product-of-two-elements-in-an-array.js similarity index 100% rename from 1464-maximum-product-of-two-elements-in-an-array.js rename to 1464.maximum-product-of-two-elements-in-an-array.js diff --git a/1465-maximum-area-of-a-piece-of-cake-after-horizontal-and-vertical-cuts.js b/1465.maximum-area-of-a-piece-of-cake-after-horizontal-and-vertical-cuts.js similarity index 100% rename from 1465-maximum-area-of-a-piece-of-cake-after-horizontal-and-vertical-cuts.js rename to 1465.maximum-area-of-a-piece-of-cake-after-horizontal-and-vertical-cuts.js diff --git a/1467-probability-of-a-two-boxes-having-the-same-number-of-distinct-balls.js b/1467.probability-of-a-two-boxes-having-the-same-number-of-distinct-balls.js similarity index 100% rename from 1467-probability-of-a-two-boxes-having-the-same-number-of-distinct-balls.js rename to 1467.probability-of-a-two-boxes-having-the-same-number-of-distinct-balls.js diff --git a/147-insertion-sort-list.js b/147.insertion-sort-list.js similarity index 100% rename from 147-insertion-sort-list.js rename to 147.insertion-sort-list.js diff --git a/1470-shuffle-the-array.js b/1470.shuffle-the-array.js similarity index 100% rename from 1470-shuffle-the-array.js rename to 1470.shuffle-the-array.js diff --git a/1472-design-browser-history.js b/1472.design-browser-history.js similarity index 100% rename from 1472-design-browser-history.js rename to 1472.design-browser-history.js diff --git a/1473-paint-house-iii.js b/1473.paint-house-iii.js similarity index 100% rename from 1473-paint-house-iii.js rename to 1473.paint-house-iii.js diff --git a/1475-final-prices-with-a-special-discount-in-a-shop.js b/1475.final-prices-with-a-special-discount-in-a-shop.js similarity index 100% rename from 1475-final-prices-with-a-special-discount-in-a-shop.js rename to 1475.final-prices-with-a-special-discount-in-a-shop.js diff --git a/1476-subrectangle-queries.js b/1476.subrectangle-queries.js similarity index 100% rename from 1476-subrectangle-queries.js rename to 1476.subrectangle-queries.js diff --git a/1478-allocate-mailboxes.js b/1478.allocate-mailboxes.js similarity index 100% rename from 1478-allocate-mailboxes.js rename to 1478.allocate-mailboxes.js diff --git a/148-sort-list.js b/148.sort-list.js similarity index 100% rename from 148-sort-list.js rename to 148.sort-list.js diff --git a/1480-running-sum-of-1d-array.js b/1480.running-sum-of-1d-array.js similarity index 100% rename from 1480-running-sum-of-1d-array.js rename to 1480.running-sum-of-1d-array.js diff --git a/1481-least-number-of-unique-integers-after k-removals.js b/1481.least-number-of-unique-integers-after k-removals.js similarity index 100% rename from 1481-least-number-of-unique-integers-after k-removals.js rename to 1481.least-number-of-unique-integers-after k-removals.js diff --git a/1482-minimum-number-of-days-to-make-m-bouquets.js b/1482.minimum-number-of-days-to-make-m-bouquets.js similarity index 100% rename from 1482-minimum-number-of-days-to-make-m-bouquets.js rename to 1482.minimum-number-of-days-to-make-m-bouquets.js diff --git a/1483-kth-ancestor-of-a-tree-node.js b/1483.kth-ancestor-of-a-tree-node.js similarity index 100% rename from 1483-kth-ancestor-of-a-tree-node.js rename to 1483.kth-ancestor-of-a-tree-node.js diff --git a/1486-xor-operation-in-an-array.js b/1486.xor-operation-in-an-array.js similarity index 100% rename from 1486-xor-operation-in-an-array.js rename to 1486.xor-operation-in-an-array.js diff --git a/1489-find-critical-and-pseudo-critical-edges-in-minimum-spanning-tree.js b/1489.find-critical-and-pseudo-critical-edges-in-minimum-spanning-tree.js similarity index 100% rename from 1489-find-critical-and-pseudo-critical-edges-in-minimum-spanning-tree.js rename to 1489.find-critical-and-pseudo-critical-edges-in-minimum-spanning-tree.js diff --git a/149-max-points-on-a-line.js b/149.max-points-on-a-line.js similarity index 100% rename from 149-max-points-on-a-line.js rename to 149.max-points-on-a-line.js diff --git a/1490-clone-n-ary-tree.js b/1490.clone-n-ary-tree.js similarity index 100% rename from 1490-clone-n-ary-tree.js rename to 1490.clone-n-ary-tree.js diff --git a/1492-the-kth-factor-of-n.js b/1492.the-kth-factor-of-n.js similarity index 100% rename from 1492-the-kth-factor-of-n.js rename to 1492.the-kth-factor-of-n.js diff --git a/1493-longest-subarray-of-1s-after-deleting-one-element.js b/1493.longest-subarray-of-1s-after-deleting-one-element.js similarity index 100% rename from 1493-longest-subarray-of-1s-after-deleting-one-element.js rename to 1493.longest-subarray-of-1s-after-deleting-one-element.js diff --git a/1494-parallel-courses-ii.js b/1494.parallel-courses-ii.js similarity index 100% rename from 1494-parallel-courses-ii.js rename to 1494.parallel-courses-ii.js diff --git a/1498-number-of-subsequences-that-satisfy-the-given-sum-condition.js b/1498.number-of-subsequences-that-satisfy-the-given-sum-condition.js similarity index 100% rename from 1498-number-of-subsequences-that-satisfy-the-given-sum-condition.js rename to 1498.number-of-subsequences-that-satisfy-the-given-sum-condition.js diff --git a/1499-max-value-of-equation.js b/1499.max-value-of-equation.js similarity index 100% rename from 1499-max-value-of-equation.js rename to 1499.max-value-of-equation.js diff --git a/15-3sum.js b/15.3sum.js old mode 100755 new mode 100644 similarity index 95% rename from 15-3sum.js rename to 15.3sum.js index 661fd438..2e67d18c --- a/15-3sum.js +++ b/15.3sum.js @@ -1,31 +1,31 @@ -/** - * @param {number[]} nums - * @return {number[][]} - */ -const threeSum = function(nums) { - const res = [], n = nums.length - nums.sort((a, b) => a - b) - - for(let i = 0; i < n; i++) { - const target = -nums[i] - let l = i + 1, r = n - 1 - while(l < r) { - const sum = nums[l] + nums[r] - if(sum > target) r-- - else if(sum < target) l++ - else { - const e = [nums[i], nums[l], nums[r]] - res.push(e) - while(l + 1 < r && nums[l + 1] === nums[l]) l++ - while(r - 1 > l && nums[r - 1] === nums[r]) r-- - l++ - r-- - } - } - while(i + 1 < n && nums[i] === nums[i + 1]) i++ - } - - - return res -}; - +/** + * @param {number[]} nums + * @return {number[][]} + */ +const threeSum = function(nums) { + const res = [], n = nums.length + nums.sort((a, b) => a - b) + + for(let i = 0; i < n; i++) { + const target = -nums[i] + let l = i + 1, r = n - 1 + while(l < r) { + const sum = nums[l] + nums[r] + if(sum > target) r-- + else if(sum < target) l++ + else { + const e = [nums[i], nums[l], nums[r]] + res.push(e) + while(l + 1 < r && nums[l + 1] === nums[l]) l++ + while(r - 1 > l && nums[r - 1] === nums[r]) r-- + l++ + r-- + } + } + while(i + 1 < n && nums[i] === nums[i + 1]) i++ + } + + + return res +}; + diff --git a/150-evaluate-reverse-polish-notation.js b/150.evaluate-reverse-polish-notation.js similarity index 100% rename from 150-evaluate-reverse-polish-notation.js rename to 150.evaluate-reverse-polish-notation.js diff --git a/1503-last-moment-before-all-ants-fall-out-of-a-plank.js b/1503.last-moment-before-all-ants-fall-out-of-a-plank.js similarity index 100% rename from 1503-last-moment-before-all-ants-fall-out-of-a-plank.js rename to 1503.last-moment-before-all-ants-fall-out-of-a-plank.js diff --git a/1505-minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits.js b/1505.minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits.js similarity index 100% rename from 1505-minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits.js rename to 1505.minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits.js diff --git a/1506-find-root-of-n-ary-tree.js b/1506.find-root-of-n-ary-tree.js similarity index 100% rename from 1506-find-root-of-n-ary-tree.js rename to 1506.find-root-of-n-ary-tree.js diff --git a/1507-reformat-date.js b/1507.reformat-date.js similarity index 100% rename from 1507-reformat-date.js rename to 1507.reformat-date.js diff --git a/1509-minimum-difference-between-largest-and-smallest-value-in-three-moves.js b/1509.minimum-difference-between-largest-and-smallest-value-in-three-moves.js similarity index 100% rename from 1509-minimum-difference-between-largest-and-smallest-value-in-three-moves.js rename to 1509.minimum-difference-between-largest-and-smallest-value-in-three-moves.js diff --git a/151-reverse-words-in-a-string.js b/151.reverse-words-in-a-string.js old mode 100755 new mode 100644 similarity index 93% rename from 151-reverse-words-in-a-string.js rename to 151.reverse-words-in-a-string.js index e7e7b072..e9149aad --- a/151-reverse-words-in-a-string.js +++ b/151.reverse-words-in-a-string.js @@ -1,12 +1,12 @@ -/** - * @param {string} str - * @returns {string} - */ -const reverseWords = function(str) { - return str - .trim() - .split(/\s+/) - .reverse() - .join(" "); -}; - +/** + * @param {string} str + * @returns {string} + */ +const reverseWords = function(str) { + return str + .trim() + .split(/\s+/) + .reverse() + .join(" "); +}; + diff --git a/1510-stone-game-iv.js b/1510.stone-game-iv.js similarity index 100% rename from 1510-stone-game-iv.js rename to 1510.stone-game-iv.js diff --git a/1512-number-of-good-pairs.js b/1512.number-of-good-pairs.js similarity index 100% rename from 1512-number-of-good-pairs.js rename to 1512.number-of-good-pairs.js diff --git a/1514-path-with-maximum-probability.js b/1514.path-with-maximum-probability.js similarity index 100% rename from 1514-path-with-maximum-probability.js rename to 1514.path-with-maximum-probability.js diff --git a/1515-best-position-for-a-service-centre.js b/1515.best-position-for-a-service-centre.js similarity index 100% rename from 1515-best-position-for-a-service-centre.js rename to 1515.best-position-for-a-service-centre.js diff --git a/1516-move-sub-tree-of-n-ary-tree.js b/1516.move-sub-tree-of-n-ary-tree.js similarity index 100% rename from 1516-move-sub-tree-of-n-ary-tree.js rename to 1516.move-sub-tree-of-n-ary-tree.js diff --git a/152-maximum-product-subarray.js b/152.maximum-product-subarray.js similarity index 100% rename from 152-maximum-product-subarray.js rename to 152.maximum-product-subarray.js diff --git a/1521-find-a-value-of-a-mysterious-function-closest-to-target.js b/1521.find-a-value-of-a-mysterious-function-closest-to-target.js similarity index 100% rename from 1521-find-a-value-of-a-mysterious-function-closest-to-target.js rename to 1521.find-a-value-of-a-mysterious-function-closest-to-target.js diff --git a/1522-diameter-of-n-ary-tree.js b/1522.diameter-of-n-ary-tree.js similarity index 100% rename from 1522-diameter-of-n-ary-tree.js rename to 1522.diameter-of-n-ary-tree.js diff --git a/1523-count-odd-numbers-in-an-interval-range.js b/1523.count-odd-numbers-in-an-interval-range.js similarity index 100% rename from 1523-count-odd-numbers-in-an-interval-range.js rename to 1523.count-odd-numbers-in-an-interval-range.js diff --git a/1524-number-of-sub-arrays-with-odd-sum.js b/1524.number-of-sub-arrays-with-odd-sum.js similarity index 100% rename from 1524-number-of-sub-arrays-with-odd-sum.js rename to 1524.number-of-sub-arrays-with-odd-sum.js diff --git a/1525-number-of-good-ways-to-split-a-string.js b/1525.number-of-good-ways-to-split-a-string.js similarity index 100% rename from 1525-number-of-good-ways-to-split-a-string.js rename to 1525.number-of-good-ways-to-split-a-string.js diff --git a/1526-minimum-number-of-increments-on-subarrays-to-form-a-target-array.js b/1526.minimum-number-of-increments-on-subarrays-to-form-a-target-array.js similarity index 100% rename from 1526-minimum-number-of-increments-on-subarrays-to-form-a-target-array.js rename to 1526.minimum-number-of-increments-on-subarrays-to-form-a-target-array.js diff --git a/1528-shuffle-string.js b/1528.shuffle-string.js similarity index 100% rename from 1528-shuffle-string.js rename to 1528.shuffle-string.js diff --git a/1529-minimum-suffix-flips.js b/1529.minimum-suffix-flips.js similarity index 100% rename from 1529-minimum-suffix-flips.js rename to 1529.minimum-suffix-flips.js diff --git a/153-find-minimum-in-rotated-sorted-array.js b/153.find-minimum-in-rotated-sorted-array.js similarity index 100% rename from 153-find-minimum-in-rotated-sorted-array.js rename to 153.find-minimum-in-rotated-sorted-array.js diff --git a/1530-number-of-good-leaf-nodes-pairs.js b/1530.number-of-good-leaf-nodes-pairs.js similarity index 100% rename from 1530-number-of-good-leaf-nodes-pairs.js rename to 1530.number-of-good-leaf-nodes-pairs.js diff --git a/1531-string-compression-ii.js b/1531.string-compression-ii.js similarity index 100% rename from 1531-string-compression-ii.js rename to 1531.string-compression-ii.js diff --git a/1534-count-good-triplets.js b/1534.count-good-triplets.js similarity index 100% rename from 1534-count-good-triplets.js rename to 1534.count-good-triplets.js diff --git a/1537-get-the-maximum-score.js b/1537.get-the-maximum-score.js similarity index 100% rename from 1537-get-the-maximum-score.js rename to 1537.get-the-maximum-score.js diff --git a/1539-kth-missing-positive-number.js b/1539.kth-missing-positive-number.js similarity index 100% rename from 1539-kth-missing-positive-number.js rename to 1539.kth-missing-positive-number.js diff --git a/154-find-minimum-in-rotated-sorted-array-ii.js b/154.find-minimum-in-rotated-sorted-array-ii.js similarity index 100% rename from 154-find-minimum-in-rotated-sorted-array-ii.js rename to 154.find-minimum-in-rotated-sorted-array-ii.js diff --git a/1540-can-convert-string-in-k-moves.js b/1540.can-convert-string-in-k-moves.js similarity index 100% rename from 1540-can-convert-string-in-k-moves.js rename to 1540.can-convert-string-in-k-moves.js diff --git a/1541-minimum-insertions-to-balance-a-parentheses-string.js b/1541.minimum-insertions-to-balance-a-parentheses-string.js similarity index 100% rename from 1541-minimum-insertions-to-balance-a-parentheses-string.js rename to 1541.minimum-insertions-to-balance-a-parentheses-string.js diff --git a/1542-find-longest-awesome-substring.js b/1542.find-longest-awesome-substring.js similarity index 100% rename from 1542-find-longest-awesome-substring.js rename to 1542.find-longest-awesome-substring.js diff --git a/1546-maximum-number-of-non-overlapping-subarrays-with-sum-equals-target.js b/1546.maximum-number-of-non-overlapping-subarrays-with-sum-equals-target.js similarity index 100% rename from 1546-maximum-number-of-non-overlapping-subarrays-with-sum-equals-target.js rename to 1546.maximum-number-of-non-overlapping-subarrays-with-sum-equals-target.js diff --git a/1547-minimum-cost-to-cut-a-stick.js b/1547.minimum-cost-to-cut-a-stick.js similarity index 100% rename from 1547-minimum-cost-to-cut-a-stick.js rename to 1547.minimum-cost-to-cut-a-stick.js diff --git a/155-min-stack.js b/155.min-stack.js similarity index 100% rename from 155-min-stack.js rename to 155.min-stack.js diff --git a/1550-three-consecutive-odds.js b/1550.three-consecutive-odds.js similarity index 100% rename from 1550-three-consecutive-odds.js rename to 1550.three-consecutive-odds.js diff --git a/1551-minimum-operations-to-make-array-equal.js b/1551.minimum-operations-to-make-array-equal.js similarity index 100% rename from 1551-minimum-operations-to-make-array-equal.js rename to 1551.minimum-operations-to-make-array-equal.js diff --git a/1552-magnetic-force-between-two-balls.js b/1552.magnetic-force-between-two-balls.js similarity index 100% rename from 1552-magnetic-force-between-two-balls.js rename to 1552.magnetic-force-between-two-balls.js diff --git a/1553-minimum-number-of-days-to-eat-n-oranges.js b/1553.minimum-number-of-days-to-eat-n-oranges.js similarity index 100% rename from 1553-minimum-number-of-days-to-eat-n-oranges.js rename to 1553.minimum-number-of-days-to-eat-n-oranges.js diff --git a/1554-strings-differ-by-one-character.js b/1554.strings-differ-by-one-character.js similarity index 100% rename from 1554-strings-differ-by-one-character.js rename to 1554.strings-differ-by-one-character.js diff --git a/1557-minimum-number-of-vertices-to-reach-all-nodes.js b/1557.minimum-number-of-vertices-to-reach-all-nodes.js similarity index 100% rename from 1557-minimum-number-of-vertices-to-reach-all-nodes.js rename to 1557.minimum-number-of-vertices-to-reach-all-nodes.js diff --git a/1559-detect-cycles-in-2d-grid.js b/1559.detect-cycles-in-2d-grid.js similarity index 100% rename from 1559-detect-cycles-in-2d-grid.js rename to 1559.detect-cycles-in-2d-grid.js diff --git a/156-binary-tree-upside-down.js b/156.binary-tree-upside-down.js similarity index 100% rename from 156-binary-tree-upside-down.js rename to 156.binary-tree-upside-down.js diff --git a/1560-most-visited-sector-in-a-circular-track.js b/1560.most-visited-sector-in-a-circular-track.js similarity index 100% rename from 1560-most-visited-sector-in-a-circular-track.js rename to 1560.most-visited-sector-in-a-circular-track.js diff --git a/1561-maximum-number-of-coins-you-can-get.js b/1561.maximum-number-of-coins-you-can-get.js similarity index 100% rename from 1561-maximum-number-of-coins-you-can-get.js rename to 1561.maximum-number-of-coins-you-can-get.js diff --git a/1562-find-latest-group-of-size-m.js b/1562.find-latest-group-of-size-m.js similarity index 100% rename from 1562-find-latest-group-of-size-m.js rename to 1562.find-latest-group-of-size-m.js diff --git a/1564-put-boxes-into-the-warehouse-i.js b/1564.put-boxes-into-the-warehouse-i.js similarity index 100% rename from 1564-put-boxes-into-the-warehouse-i.js rename to 1564.put-boxes-into-the-warehouse-i.js diff --git a/1567-maximum-length-of-subarray-with-positive-product.js b/1567.maximum-length-of-subarray-with-positive-product.js similarity index 100% rename from 1567-maximum-length-of-subarray-with-positive-product.js rename to 1567.maximum-length-of-subarray-with-positive-product.js diff --git a/1568-minimum-number-of-days-to-disconnect-island.js b/1568.minimum-number-of-days-to-disconnect-island.js similarity index 100% rename from 1568-minimum-number-of-days-to-disconnect-island.js rename to 1568.minimum-number-of-days-to-disconnect-island.js diff --git a/1569-number-of-ways-to-reorder-array-to-get-same-bst.js b/1569.number-of-ways-to-reorder-array-to-get-same-bst.js similarity index 100% rename from 1569-number-of-ways-to-reorder-array-to-get-same-bst.js rename to 1569.number-of-ways-to-reorder-array-to-get-same-bst.js diff --git a/157-read-n-characters-given-read4.js b/157.read-n-characters-given-read4.js similarity index 100% rename from 157-read-n-characters-given-read4.js rename to 157.read-n-characters-given-read4.js diff --git a/1570-dot-product-of-two-sparse-vectors.js b/1570.dot-product-of-two-sparse-vectors.js similarity index 100% rename from 1570-dot-product-of-two-sparse-vectors.js rename to 1570.dot-product-of-two-sparse-vectors.js diff --git a/1572-matrix-diagonal-sum.js b/1572.matrix-diagonal-sum.js similarity index 100% rename from 1572-matrix-diagonal-sum.js rename to 1572.matrix-diagonal-sum.js diff --git a/1573-number-of-ways-to-split-a-string.js b/1573.number-of-ways-to-split-a-string.js similarity index 100% rename from 1573-number-of-ways-to-split-a-string.js rename to 1573.number-of-ways-to-split-a-string.js diff --git a/1574-shortest-subarray-to-be-removed-to-make-array-sorted.js b/1574.shortest-subarray-to-be-removed-to-make-array-sorted.js similarity index 100% rename from 1574-shortest-subarray-to-be-removed-to-make-array-sorted.js rename to 1574.shortest-subarray-to-be-removed-to-make-array-sorted.js diff --git a/1575-count-all-possible-routes.js b/1575.count-all-possible-routes.js similarity index 100% rename from 1575-count-all-possible-routes.js rename to 1575.count-all-possible-routes.js diff --git a/1576-replace-all-s-to-avoid-consecutive-repeating-characters.js b/1576.replace-all-s-to-avoid-consecutive-repeating-characters.js similarity index 100% rename from 1576-replace-all-s-to-avoid-consecutive-repeating-characters.js rename to 1576.replace-all-s-to-avoid-consecutive-repeating-characters.js diff --git a/1578-minimum-deletion-cost-to-avoid-repeating-letters.js b/1578.minimum-deletion-cost-to-avoid-repeating-letters.js similarity index 100% rename from 1578-minimum-deletion-cost-to-avoid-repeating-letters.js rename to 1578.minimum-deletion-cost-to-avoid-repeating-letters.js diff --git a/1579-remove-max-number-of-edges-to-keep-graph-fully-traversable.js b/1579.remove-max-number-of-edges-to-keep-graph-fully-traversable.js similarity index 100% rename from 1579-remove-max-number-of-edges-to-keep-graph-fully-traversable.js rename to 1579.remove-max-number-of-edges-to-keep-graph-fully-traversable.js diff --git a/158-read-n-characters-given-read4-ii-call-multiple-times.js b/158.read-n-characters-given-read4-ii-call-multiple-times.js similarity index 100% rename from 158-read-n-characters-given-read4-ii-call-multiple-times.js rename to 158.read-n-characters-given-read4-ii-call-multiple-times.js diff --git a/1584-min-cost-to-connect-all-points.js b/1584.min-cost-to-connect-all-points.js similarity index 100% rename from 1584-min-cost-to-connect-all-points.js rename to 1584.min-cost-to-connect-all-points.js diff --git a/1585-check-if-string-is-transformable-with-substring-sort-operations.js b/1585.check-if-string-is-transformable-with-substring-sort-operations.js similarity index 100% rename from 1585-check-if-string-is-transformable-with-substring-sort-operations.js rename to 1585.check-if-string-is-transformable-with-substring-sort-operations.js diff --git a/1586-binary-search-tree-iterator-ii.js b/1586.binary-search-tree-iterator-ii.js similarity index 100% rename from 1586-binary-search-tree-iterator-ii.js rename to 1586.binary-search-tree-iterator-ii.js diff --git a/1588-sum-of-all-odd-length-subarrays.js b/1588.sum-of-all-odd-length-subarrays.js similarity index 100% rename from 1588-sum-of-all-odd-length-subarrays.js rename to 1588.sum-of-all-odd-length-subarrays.js diff --git a/1589-maximum-sum-obtained-of-any-permutation.js b/1589.maximum-sum-obtained-of-any-permutation.js similarity index 100% rename from 1589-maximum-sum-obtained-of-any-permutation.js rename to 1589.maximum-sum-obtained-of-any-permutation.js diff --git a/159-longest-substring-with-at-most-two-distinct-characters.js b/159.longest-substring-with-at-most-two-distinct-characters.js similarity index 100% rename from 159-longest-substring-with-at-most-two-distinct-characters.js rename to 159.longest-substring-with-at-most-two-distinct-characters.js diff --git a/1590-make-sum-divisible-by-p.js b/1590.make-sum-divisible-by-p.js similarity index 100% rename from 1590-make-sum-divisible-by-p.js rename to 1590.make-sum-divisible-by-p.js diff --git a/1591-strange-printer-ii.js b/1591.strange-printer-ii.js similarity index 100% rename from 1591-strange-printer-ii.js rename to 1591.strange-printer-ii.js diff --git a/1592-rearrange-spaces-between-words.js b/1592.rearrange-spaces-between-words.js similarity index 100% rename from 1592-rearrange-spaces-between-words.js rename to 1592.rearrange-spaces-between-words.js diff --git a/1593-split-a-string-into-the-max-number-of-unique-substrings.js b/1593.split-a-string-into-the-max-number-of-unique-substrings.js similarity index 100% rename from 1593-split-a-string-into-the-max-number-of-unique-substrings.js rename to 1593.split-a-string-into-the-max-number-of-unique-substrings.js diff --git a/1594-maximum-non-negative-product-in-a-matrix.js b/1594.maximum-non-negative-product-in-a-matrix.js similarity index 100% rename from 1594-maximum-non-negative-product-in-a-matrix.js rename to 1594.maximum-non-negative-product-in-a-matrix.js diff --git a/1595-minimum-cost-to-connect-two-groups-of-points.js b/1595.minimum-cost-to-connect-two-groups-of-points.js similarity index 100% rename from 1595-minimum-cost-to-connect-two-groups-of-points.js rename to 1595.minimum-cost-to-connect-two-groups-of-points.js diff --git a/1597-build-binary-expression-tree-from-infix-expression.js b/1597.build-binary-expression-tree-from-infix-expression.js similarity index 100% rename from 1597-build-binary-expression-tree-from-infix-expression.js rename to 1597.build-binary-expression-tree-from-infix-expression.js diff --git a/1598-crawler-log-folder.js b/1598.crawler-log-folder.js similarity index 100% rename from 1598-crawler-log-folder.js rename to 1598.crawler-log-folder.js diff --git a/1599-maximum-profit-of-operating-a-centennial-wheel.js b/1599.maximum-profit-of-operating-a-centennial-wheel.js similarity index 100% rename from 1599-maximum-profit-of-operating-a-centennial-wheel.js rename to 1599.maximum-profit-of-operating-a-centennial-wheel.js diff --git a/16-3sum-closest.js b/16.3sum-closest.js old mode 100755 new mode 100644 similarity index 95% rename from 16-3sum-closest.js rename to 16.3sum-closest.js index fbe0b658..793591cf --- a/16-3sum-closest.js +++ b/16.3sum-closest.js @@ -1,39 +1,39 @@ -/** - * @param {number[]} nums - * @param {number} target - * @return {number} - */ -const threeSumClosest = function(nums, target) { - const nums = nums.sort((a, b) => a - b); - let result; - let lo; - let hi; - let sum; - result = nums[0] + nums[1] + nums[nums.length - 1]; - for (let i = 0; i < nums.length - 2; i++) { - lo = i + 1; - hi = nums.length - 1; - while (lo < hi) { - sum = nums[i] + nums[lo] + nums[hi]; - if (sum < target) { - while (lo < hi && nums[lo] === nums[lo + 1]) { - lo += 1; - } - lo += 1; - } else if (sum > target) { - while (lo < hi && nums[hi] === nums[hi - 1]) { - hi -= 1; - } - hi -= 1; - } else { - return sum; - } - - if (Math.abs(target - sum) < Math.abs(target - result)) { - result = sum; - } - } - } - - return result; -}; +/** + * @param {number[]} nums + * @param {number} target + * @return {number} + */ +const threeSumClosest = function(nums, target) { + const nums = nums.sort((a, b) => a - b); + let result; + let lo; + let hi; + let sum; + result = nums[0] + nums[1] + nums[nums.length - 1]; + for (let i = 0; i < nums.length - 2; i++) { + lo = i + 1; + hi = nums.length - 1; + while (lo < hi) { + sum = nums[i] + nums[lo] + nums[hi]; + if (sum < target) { + while (lo < hi && nums[lo] === nums[lo + 1]) { + lo += 1; + } + lo += 1; + } else if (sum > target) { + while (lo < hi && nums[hi] === nums[hi - 1]) { + hi -= 1; + } + hi -= 1; + } else { + return sum; + } + + if (Math.abs(target - sum) < Math.abs(target - result)) { + result = sum; + } + } + } + + return result; +}; diff --git a/160-intersection-of-two-linked-lists.js b/160.intersection-of-two-linked-lists.js similarity index 100% rename from 160-intersection-of-two-linked-lists.js rename to 160.intersection-of-two-linked-lists.js diff --git a/1600-throne-inheritance.js b/1600.throne-inheritance.js similarity index 100% rename from 1600-throne-inheritance.js rename to 1600.throne-inheritance.js diff --git a/1601-maximum-number-of-achievable-transfer-requests.js b/1601.maximum-number-of-achievable-transfer-requests.js similarity index 100% rename from 1601-maximum-number-of-achievable-transfer-requests.js rename to 1601.maximum-number-of-achievable-transfer-requests.js diff --git a/1602-find-nearest-right-node-in-binary-tree.js b/1602.find-nearest-right-node-in-binary-tree.js similarity index 100% rename from 1602-find-nearest-right-node-in-binary-tree.js rename to 1602.find-nearest-right-node-in-binary-tree.js diff --git a/1603-design-parking-system.js b/1603.design-parking-system.js similarity index 100% rename from 1603-design-parking-system.js rename to 1603.design-parking-system.js diff --git a/1605-find-valid-matrix-given-row-and-column-sums.js b/1605.find-valid-matrix-given-row-and-column-sums.js similarity index 100% rename from 1605-find-valid-matrix-given-row-and-column-sums.js rename to 1605.find-valid-matrix-given-row-and-column-sums.js diff --git a/1606-find-servers-that-handled-most-number-of-requests.js b/1606.find-servers-that-handled-most-number-of-requests.js similarity index 100% rename from 1606-find-servers-that-handled-most-number-of-requests.js rename to 1606.find-servers-that-handled-most-number-of-requests.js diff --git a/1608-special-array-with-x-elements-greater-than-or-equal-x.js b/1608.special-array-with-x-elements-greater-than-or-equal-x.js similarity index 100% rename from 1608-special-array-with-x-elements-greater-than-or-equal-x.js rename to 1608.special-array-with-x-elements-greater-than-or-equal-x.js diff --git a/1609-even-odd-tree.js b/1609.even-odd-tree.js similarity index 100% rename from 1609-even-odd-tree.js rename to 1609.even-odd-tree.js diff --git a/161-one-edit-distance.js b/161.one-edit-distance.js similarity index 100% rename from 161-one-edit-distance.js rename to 161.one-edit-distance.js diff --git a/1610-maximum-number-of-visible-points.js b/1610.maximum-number-of-visible-points.js similarity index 100% rename from 1610-maximum-number-of-visible-points.js rename to 1610.maximum-number-of-visible-points.js diff --git a/1611-minimum-one-bit-operations-to-make-integers-zero.js b/1611.minimum-one-bit-operations-to-make-integers-zero.js similarity index 100% rename from 1611-minimum-one-bit-operations-to-make-integers-zero.js rename to 1611.minimum-one-bit-operations-to-make-integers-zero.js diff --git a/1612-check-if-two-expression-trees-are-equivalent.js b/1612.check-if-two-expression-trees-are-equivalent.js similarity index 100% rename from 1612-check-if-two-expression-trees-are-equivalent.js rename to 1612.check-if-two-expression-trees-are-equivalent.js diff --git a/1614-maximum-nesting-depth-of-the-parentheses.js b/1614.maximum-nesting-depth-of-the-parentheses.js similarity index 100% rename from 1614-maximum-nesting-depth-of-the-parentheses.js rename to 1614.maximum-nesting-depth-of-the-parentheses.js diff --git a/1615-maximal-network-rank.js b/1615.maximal-network-rank.js similarity index 100% rename from 1615-maximal-network-rank.js rename to 1615.maximal-network-rank.js diff --git a/1616-split-two-strings-to-make-palindrome.js b/1616.split-two-strings-to-make-palindrome.js similarity index 100% rename from 1616-split-two-strings-to-make-palindrome.js rename to 1616.split-two-strings-to-make-palindrome.js diff --git a/1617-count-subtrees-with-max-distance-between-cities.js b/1617.count-subtrees-with-max-distance-between-cities.js similarity index 100% rename from 1617-count-subtrees-with-max-distance-between-cities.js rename to 1617.count-subtrees-with-max-distance-between-cities.js diff --git a/1619-mean-of-array-after-removing-some-elements.js b/1619.mean-of-array-after-removing-some-elements.js similarity index 100% rename from 1619-mean-of-array-after-removing-some-elements.js rename to 1619.mean-of-array-after-removing-some-elements.js diff --git a/162-find-peak-element.js b/162.find-peak-element.js similarity index 100% rename from 162-find-peak-element.js rename to 162.find-peak-element.js diff --git a/1621-number-of-sets-of-k-non-overlapping-line-segments.js b/1621.number-of-sets-of-k-non-overlapping-line-segments.js similarity index 100% rename from 1621-number-of-sets-of-k-non-overlapping-line-segments.js rename to 1621.number-of-sets-of-k-non-overlapping-line-segments.js diff --git a/1622-fancy-sequence.js b/1622.fancy-sequence.js similarity index 100% rename from 1622-fancy-sequence.js rename to 1622.fancy-sequence.js diff --git a/1624-largest-substring-between-two-equal-characters.js b/1624.largest-substring-between-two-equal-characters.js similarity index 100% rename from 1624-largest-substring-between-two-equal-characters.js rename to 1624.largest-substring-between-two-equal-characters.js diff --git a/1625-lexicographically-smallest-string-after-applying-operations.js b/1625.lexicographically-smallest-string-after-applying-operations.js similarity index 100% rename from 1625-lexicographically-smallest-string-after-applying-operations.js rename to 1625.lexicographically-smallest-string-after-applying-operations.js diff --git a/1626-best-team-with-no-conflicts.js b/1626.best-team-with-no-conflicts.js similarity index 100% rename from 1626-best-team-with-no-conflicts.js rename to 1626.best-team-with-no-conflicts.js diff --git a/1627-graph-connectivity-with-threshold.js b/1627.graph-connectivity-with-threshold.js similarity index 100% rename from 1627-graph-connectivity-with-threshold.js rename to 1627.graph-connectivity-with-threshold.js diff --git a/1629-slowest-key.js b/1629.slowest-key.js similarity index 100% rename from 1629-slowest-key.js rename to 1629.slowest-key.js diff --git a/163-missing-ranges.js b/163.missing-ranges.js similarity index 100% rename from 163-missing-ranges.js rename to 163.missing-ranges.js diff --git a/1630-arithmetic-subarrays.js b/1630.arithmetic-subarrays.js similarity index 100% rename from 1630-arithmetic-subarrays.js rename to 1630.arithmetic-subarrays.js diff --git a/1631-path-with-minimum-effort.js b/1631.path-with-minimum-effort.js similarity index 100% rename from 1631-path-with-minimum-effort.js rename to 1631.path-with-minimum-effort.js diff --git a/1632-rank-transform-of-a-matrix.js b/1632.rank-transform-of-a-matrix.js similarity index 100% rename from 1632-rank-transform-of-a-matrix.js rename to 1632.rank-transform-of-a-matrix.js diff --git a/1636-sort-array-by-increasing-frequency.js b/1636.sort-array-by-increasing-frequency.js similarity index 100% rename from 1636-sort-array-by-increasing-frequency.js rename to 1636.sort-array-by-increasing-frequency.js diff --git a/1637-widest-vertical-area-between-two-points-containing-no-points.js b/1637.widest-vertical-area-between-two-points-containing-no-points.js similarity index 100% rename from 1637-widest-vertical-area-between-two-points-containing-no-points.js rename to 1637.widest-vertical-area-between-two-points-containing-no-points.js diff --git a/1638-count-substrings-that-differ-by-one-character.js b/1638.count-substrings-that-differ-by-one-character.js similarity index 100% rename from 1638-count-substrings-that-differ-by-one-character.js rename to 1638.count-substrings-that-differ-by-one-character.js diff --git a/1639-number-of-ways-to-form-a-target-string-given-a-dictionary.js b/1639.number-of-ways-to-form-a-target-string-given-a-dictionary.js similarity index 100% rename from 1639-number-of-ways-to-form-a-target-string-given-a-dictionary.js rename to 1639.number-of-ways-to-form-a-target-string-given-a-dictionary.js diff --git a/164-maximum-gap.js b/164.maximum-gap.js similarity index 100% rename from 164-maximum-gap.js rename to 164.maximum-gap.js diff --git a/1640-check-array-formation-through-concatenation.js b/1640.check-array-formation-through-concatenation.js similarity index 100% rename from 1640-check-array-formation-through-concatenation.js rename to 1640.check-array-formation-through-concatenation.js diff --git a/1641-count-sorted-vowel-strings.js b/1641.count-sorted-vowel-strings.js similarity index 100% rename from 1641-count-sorted-vowel-strings.js rename to 1641.count-sorted-vowel-strings.js diff --git a/1642-furthest-building-you-can-reach.js b/1642.furthest-building-you-can-reach.js similarity index 100% rename from 1642-furthest-building-you-can-reach.js rename to 1642.furthest-building-you-can-reach.js diff --git a/1643-kth-smallest-instructions.js b/1643.kth-smallest-instructions.js similarity index 100% rename from 1643-kth-smallest-instructions.js rename to 1643.kth-smallest-instructions.js diff --git a/1644-lowest-common-ancestor-of-a-binary-tree-ii.js b/1644.lowest-common-ancestor-of-a-binary-tree-ii.js similarity index 100% rename from 1644-lowest-common-ancestor-of-a-binary-tree-ii.js rename to 1644.lowest-common-ancestor-of-a-binary-tree-ii.js diff --git a/1646-get-maximum-in-generated-array.js b/1646.get-maximum-in-generated-array.js similarity index 100% rename from 1646-get-maximum-in-generated-array.js rename to 1646.get-maximum-in-generated-array.js diff --git a/1647-minimum-deletions-to-make-character-frequencies-unique.js b/1647.minimum-deletions-to-make-character-frequencies-unique.js similarity index 100% rename from 1647-minimum-deletions-to-make-character-frequencies-unique.js rename to 1647.minimum-deletions-to-make-character-frequencies-unique.js diff --git a/1648-sell-diminishing-valued-colored-balls.js b/1648.sell-diminishing-valued-colored-balls.js similarity index 100% rename from 1648-sell-diminishing-valued-colored-balls.js rename to 1648.sell-diminishing-valued-colored-balls.js diff --git a/1649-create-sorted-array-through-instructions.js b/1649.create-sorted-array-through-instructions.js similarity index 100% rename from 1649-create-sorted-array-through-instructions.js rename to 1649.create-sorted-array-through-instructions.js diff --git a/165-compare-version-numbers.js b/165.compare-version-numbers.js similarity index 100% rename from 165-compare-version-numbers.js rename to 165.compare-version-numbers.js diff --git a/1650-lowest-common-ancestor-of-a-binary-tree-iii.js b/1650.lowest-common-ancestor-of-a-binary-tree-iii.js similarity index 100% rename from 1650-lowest-common-ancestor-of-a-binary-tree-iii.js rename to 1650.lowest-common-ancestor-of-a-binary-tree-iii.js diff --git a/1652-defuse-the-bomb.js b/1652.defuse-the-bomb.js similarity index 100% rename from 1652-defuse-the-bomb.js rename to 1652.defuse-the-bomb.js diff --git a/1653-minimum-deletions-to-make-string-balanced.js b/1653.minimum-deletions-to-make-string-balanced.js similarity index 100% rename from 1653-minimum-deletions-to-make-string-balanced.js rename to 1653.minimum-deletions-to-make-string-balanced.js diff --git a/1654-minimum-jumps-to-reach-home.js b/1654.minimum-jumps-to-reach-home.js similarity index 100% rename from 1654-minimum-jumps-to-reach-home.js rename to 1654.minimum-jumps-to-reach-home.js diff --git a/1655-distribute-repeating-integers.js b/1655.distribute-repeating-integers.js similarity index 100% rename from 1655-distribute-repeating-integers.js rename to 1655.distribute-repeating-integers.js diff --git a/1656-design-an-ordered-stream.js b/1656.design-an-ordered-stream.js similarity index 100% rename from 1656-design-an-ordered-stream.js rename to 1656.design-an-ordered-stream.js diff --git a/1657-determine-if-two-strings-are-close.js b/1657.determine-if-two-strings-are-close.js similarity index 100% rename from 1657-determine-if-two-strings-are-close.js rename to 1657.determine-if-two-strings-are-close.js diff --git a/1658-minimum-operations-to-reduce-x-to-zero.js b/1658.minimum-operations-to-reduce-x-to-zero.js similarity index 100% rename from 1658-minimum-operations-to-reduce-x-to-zero.js rename to 1658.minimum-operations-to-reduce-x-to-zero.js diff --git a/1659-maximize-grid-happiness.js b/1659.maximize-grid-happiness.js similarity index 100% rename from 1659-maximize-grid-happiness.js rename to 1659.maximize-grid-happiness.js diff --git a/166-fraction-to-recurring-decimal.js b/166.fraction-to-recurring-decimal.js similarity index 100% rename from 166-fraction-to-recurring-decimal.js rename to 166.fraction-to-recurring-decimal.js diff --git a/1660-correct-a-binary-tree.js b/1660.correct-a-binary-tree.js similarity index 100% rename from 1660-correct-a-binary-tree.js rename to 1660.correct-a-binary-tree.js diff --git a/1662-check-if-two-string-arrays-are-equivalent.js b/1662.check-if-two-string-arrays-are-equivalent.js similarity index 100% rename from 1662-check-if-two-string-arrays-are-equivalent.js rename to 1662.check-if-two-string-arrays-are-equivalent.js diff --git a/1663-smallest-string-with-a-given-numeric-value.js b/1663.smallest-string-with-a-given-numeric-value.js similarity index 100% rename from 1663-smallest-string-with-a-given-numeric-value.js rename to 1663.smallest-string-with-a-given-numeric-value.js diff --git a/1664-ways-to-make-a-fair-array.js b/1664.ways-to-make-a-fair-array.js similarity index 100% rename from 1664-ways-to-make-a-fair-array.js rename to 1664.ways-to-make-a-fair-array.js diff --git a/1665-minimum-initial-energy-to-finish-tasks.js b/1665.minimum-initial-energy-to-finish-tasks.js similarity index 100% rename from 1665-minimum-initial-energy-to-finish-tasks.js rename to 1665.minimum-initial-energy-to-finish-tasks.js diff --git a/1666-change-the-root-of-a-binary-tree.js b/1666.change-the-root-of-a-binary-tree.js similarity index 100% rename from 1666-change-the-root-of-a-binary-tree.js rename to 1666.change-the-root-of-a-binary-tree.js diff --git a/1668-maximum-repeating-substring.js b/1668.maximum-repeating-substring.js similarity index 100% rename from 1668-maximum-repeating-substring.js rename to 1668.maximum-repeating-substring.js diff --git a/1669-merge-in-between-linked-lists.js b/1669.merge-in-between-linked-lists.js similarity index 100% rename from 1669-merge-in-between-linked-lists.js rename to 1669.merge-in-between-linked-lists.js diff --git a/167-two-sum-II-input-array-is-sorted.js b/167.two-sum-II-input-array-is-sorted.js old mode 100755 new mode 100644 similarity index 95% rename from 167-two-sum-II-input-array-is-sorted.js rename to 167.two-sum-II-input-array-is-sorted.js index a99cd2ab..6e2c1128 --- a/167-two-sum-II-input-array-is-sorted.js +++ b/167.two-sum-II-input-array-is-sorted.js @@ -1,23 +1,23 @@ -/** - * @param {number[]} numbers - * @param {number} target - * @return {number[]} - */ -const twoSum = function(numbers, target) { - const res = []; - let remaining; - let next = 0; - for (let i = 0; i < numbers.length; i++) { - remaining = target - numbers[i]; - next = i + 1; - while (next < numbers.length && numbers[next] <= remaining) { - if (numbers[next] === remaining) { - res.push(i + 1, next + 1); - break; - } - next += 1; - } - } - - return res; -}; +/** + * @param {number[]} numbers + * @param {number} target + * @return {number[]} + */ +const twoSum = function(numbers, target) { + const res = []; + let remaining; + let next = 0; + for (let i = 0; i < numbers.length; i++) { + remaining = target - numbers[i]; + next = i + 1; + while (next < numbers.length && numbers[next] <= remaining) { + if (numbers[next] === remaining) { + res.push(i + 1, next + 1); + break; + } + next += 1; + } + } + + return res; +}; diff --git a/1670-design-front-middle-back-queue.js b/1670.design-front-middle-back-queue.js similarity index 100% rename from 1670-design-front-middle-back-queue.js rename to 1670.design-front-middle-back-queue.js diff --git a/1671-minimum-number-of-removals-to-make-mountain-array.js b/1671.minimum-number-of-removals-to-make-mountain-array.js similarity index 100% rename from 1671-minimum-number-of-removals-to-make-mountain-array.js rename to 1671.minimum-number-of-removals-to-make-mountain-array.js diff --git a/1672-richest-customer-wealth.js b/1672.richest-customer-wealth.js similarity index 100% rename from 1672-richest-customer-wealth.js rename to 1672.richest-customer-wealth.js diff --git a/1673-find-the-most-competitive-subsequence.js b/1673.find-the-most-competitive-subsequence.js similarity index 100% rename from 1673-find-the-most-competitive-subsequence.js rename to 1673.find-the-most-competitive-subsequence.js diff --git a/1674-minimum-moves-to-make-array-complementary.js b/1674.minimum-moves-to-make-array-complementary.js similarity index 100% rename from 1674-minimum-moves-to-make-array-complementary.js rename to 1674.minimum-moves-to-make-array-complementary.js diff --git a/1675-minimize-deviation-in-array.js b/1675.minimize-deviation-in-array.js similarity index 100% rename from 1675-minimize-deviation-in-array.js rename to 1675.minimize-deviation-in-array.js diff --git a/1676-lowest-common-ancestor-of-a-binary-tree-iv.js b/1676.lowest-common-ancestor-of-a-binary-tree-iv.js similarity index 100% rename from 1676-lowest-common-ancestor-of-a-binary-tree-iv.js rename to 1676.lowest-common-ancestor-of-a-binary-tree-iv.js diff --git a/1678-goal-parser-interpretation.js b/1678.goal-parser-interpretation.js similarity index 100% rename from 1678-goal-parser-interpretation.js rename to 1678.goal-parser-interpretation.js diff --git a/1679-max-number-of-k-sum-pairs.js b/1679.max-number-of-k-sum-pairs.js similarity index 100% rename from 1679-max-number-of-k-sum-pairs.js rename to 1679.max-number-of-k-sum-pairs.js diff --git a/168-excel-sheet-column-title.js b/168.excel-sheet-column-title.js similarity index 100% rename from 168-excel-sheet-column-title.js rename to 168.excel-sheet-column-title.js diff --git a/1680-concatenation-of-consecutive-binary-numbers.js b/1680.concatenation-of-consecutive-binary-numbers.js similarity index 100% rename from 1680-concatenation-of-consecutive-binary-numbers.js rename to 1680.concatenation-of-consecutive-binary-numbers.js diff --git a/1681-minimum-incompatibility.js b/1681.minimum-incompatibility.js similarity index 100% rename from 1681-minimum-incompatibility.js rename to 1681.minimum-incompatibility.js diff --git a/1684-count-the-number-of-consistent-strings.js b/1684.count-the-number-of-consistent-strings.js similarity index 100% rename from 1684-count-the-number-of-consistent-strings.js rename to 1684.count-the-number-of-consistent-strings.js diff --git a/1685-sum-of-absolute-differences-in-a-sorted-array.js b/1685.sum-of-absolute-differences-in-a-sorted-array.js similarity index 100% rename from 1685-sum-of-absolute-differences-in-a-sorted-array.js rename to 1685.sum-of-absolute-differences-in-a-sorted-array.js diff --git a/1686-stone-game-vi.js b/1686.stone-game-vi.js similarity index 100% rename from 1686-stone-game-vi.js rename to 1686.stone-game-vi.js diff --git a/1687-delivering-boxes-from-storage-to-ports.js b/1687.delivering-boxes-from-storage-to-ports.js similarity index 100% rename from 1687-delivering-boxes-from-storage-to-ports.js rename to 1687.delivering-boxes-from-storage-to-ports.js diff --git a/1688-count-of-matches-in-tournament.js b/1688.count-of-matches-in-tournament.js similarity index 100% rename from 1688-count-of-matches-in-tournament.js rename to 1688.count-of-matches-in-tournament.js diff --git a/1689-partitioning-into-minimum-number-of-deci-binary-numbers.js b/1689.partitioning-into-minimum-number-of-deci-binary-numbers.js similarity index 100% rename from 1689-partitioning-into-minimum-number-of-deci-binary-numbers.js rename to 1689.partitioning-into-minimum-number-of-deci-binary-numbers.js diff --git a/169-majority-element.js b/169.majority-element.js old mode 100755 new mode 100644 similarity index 95% rename from 169-majority-element.js rename to 169.majority-element.js index 4ac9f799..dcec473b --- a/169-majority-element.js +++ b/169.majority-element.js @@ -1,19 +1,19 @@ -/** - * @param {number[]} nums - * @return {number} - */ -const majorityElement = function(nums) { - const hash = {}; - nums.forEach(el => { - if (hash.hasOwnProperty(el)) { - hash[el] += 1; - } else { - hash[el] = 1; - } - }); - return Object.entries(hash) - .filter(el => el[1] > Math.floor(nums.length / 2)) - .map(el => +el[0]) - .sort((a, b) => b - a)[0]; -}; - +/** + * @param {number[]} nums + * @return {number} + */ +const majorityElement = function(nums) { + const hash = {}; + nums.forEach(el => { + if (hash.hasOwnProperty(el)) { + hash[el] += 1; + } else { + hash[el] = 1; + } + }); + return Object.entries(hash) + .filter(el => el[1] > Math.floor(nums.length / 2)) + .map(el => +el[0]) + .sort((a, b) => b - a)[0]; +}; + diff --git a/1690-stone-game-vii.js b/1690.stone-game-vii.js similarity index 100% rename from 1690-stone-game-vii.js rename to 1690.stone-game-vii.js diff --git a/1691-maximum-height-by-stacking-cuboids.js b/1691.maximum-height-by-stacking-cuboids.js similarity index 100% rename from 1691-maximum-height-by-stacking-cuboids.js rename to 1691.maximum-height-by-stacking-cuboids.js diff --git a/1694-reformat-phone-number.js b/1694.reformat-phone-number.js similarity index 100% rename from 1694-reformat-phone-number.js rename to 1694.reformat-phone-number.js diff --git a/1695-maximum-erasure-value.js b/1695.maximum-erasure-value.js similarity index 100% rename from 1695-maximum-erasure-value.js rename to 1695.maximum-erasure-value.js diff --git a/1696-jump-game-vi.js b/1696.jump-game-vi.js similarity index 100% rename from 1696-jump-game-vi.js rename to 1696.jump-game-vi.js diff --git a/1697-checking-existence-of-edge-length-limited-paths.js b/1697.checking-existence-of-edge-length-limited-paths.js similarity index 100% rename from 1697-checking-existence-of-edge-length-limited-paths.js rename to 1697.checking-existence-of-edge-length-limited-paths.js diff --git a/1698-number-of-distinct-substrings-in-a-string.js b/1698.number-of-distinct-substrings-in-a-string.js similarity index 100% rename from 1698-number-of-distinct-substrings-in-a-string.js rename to 1698.number-of-distinct-substrings-in-a-string.js diff --git a/17-letter-combinations-of-a-phone-number.js b/17.letter-combinations-of-a-phone-number.js old mode 100755 new mode 100644 similarity index 95% rename from 17-letter-combinations-of-a-phone-number.js rename to 17.letter-combinations-of-a-phone-number.js index 0601efe0..4512fcb1 --- a/17-letter-combinations-of-a-phone-number.js +++ b/17.letter-combinations-of-a-phone-number.js @@ -1,39 +1,39 @@ -/** - * @param {string} digits - * @return {string[]} - */ -const letterCombinations = function(digits) { - if (digits === "") { - return []; - } - const charMap = { - 2: ["a", "b", "c"], - 3: ["d", "e", "f"], - 4: ["g", "h", "i"], - 5: ["j", "k", "l"], - 6: ["m", "n", "o"], - 7: ["p", "q", "r", "s"], - 8: ["t", "u", "v"], - 9: ["w", "x", "y", "z"] - }; - const res = []; - const matrix = []; - for (let i = 0; i < digits.length; i++) { - matrix.push(charMap[digits.charAt(i)]); - } - let tmp = matrix[0]; - for (let j = 1; j < matrix.length; j++) { - tmp = helper(matrix, j, tmp); - } - return tmp; -}; -function helper(matrix, rowIdx, arr) { - const res = []; - for (let i = 0; i < arr.length; i++) { - const preStr = arr[i]; - for (let j = 0; j < matrix[rowIdx].length; j++) { - res.push(`${preStr}${matrix[rowIdx][j]}`); - } - } - return res; -} +/** + * @param {string} digits + * @return {string[]} + */ +const letterCombinations = function(digits) { + if (digits === "") { + return []; + } + const charMap = { + 2: ["a", "b", "c"], + 3: ["d", "e", "f"], + 4: ["g", "h", "i"], + 5: ["j", "k", "l"], + 6: ["m", "n", "o"], + 7: ["p", "q", "r", "s"], + 8: ["t", "u", "v"], + 9: ["w", "x", "y", "z"] + }; + const res = []; + const matrix = []; + for (let i = 0; i < digits.length; i++) { + matrix.push(charMap[digits.charAt(i)]); + } + let tmp = matrix[0]; + for (let j = 1; j < matrix.length; j++) { + tmp = helper(matrix, j, tmp); + } + return tmp; +}; +function helper(matrix, rowIdx, arr) { + const res = []; + for (let i = 0; i < arr.length; i++) { + const preStr = arr[i]; + for (let j = 0; j < matrix[rowIdx].length; j++) { + res.push(`${preStr}${matrix[rowIdx][j]}`); + } + } + return res; +} diff --git a/170-two-sum-iii-data-structure-design.js b/170.two-sum-iii-data-structure-design.js similarity index 100% rename from 170-two-sum-iii-data-structure-design.js rename to 170.two-sum-iii-data-structure-design.js diff --git a/1700-number-of-students-unable-to-eat-lunch.js b/1700.number-of-students-unable-to-eat-lunch.js similarity index 100% rename from 1700-number-of-students-unable-to-eat-lunch.js rename to 1700.number-of-students-unable-to-eat-lunch.js diff --git a/1701-average-waiting-time.js b/1701.average-waiting-time.js similarity index 100% rename from 1701-average-waiting-time.js rename to 1701.average-waiting-time.js diff --git a/1703-maximum-binary-string-after-change.js b/1703.maximum-binary-string-after-change.js similarity index 100% rename from 1703-maximum-binary-string-after-change.js rename to 1703.maximum-binary-string-after-change.js diff --git a/1704-determine-if-string-halves-are-alike.js b/1704.determine-if-string-halves-are-alike.js similarity index 100% rename from 1704-determine-if-string-halves-are-alike.js rename to 1704.determine-if-string-halves-are-alike.js diff --git a/1704-minimum-adjacent-swaps-for-k-consecutive-ones.js b/1704.minimum-adjacent-swaps-for-k-consecutive-ones.js similarity index 100% rename from 1704-minimum-adjacent-swaps-for-k-consecutive-ones.js rename to 1704.minimum-adjacent-swaps-for-k-consecutive-ones.js diff --git a/1705-maximum-number-of-eaten-apples.js b/1705.maximum-number-of-eaten-apples.js similarity index 100% rename from 1705-maximum-number-of-eaten-apples.js rename to 1705.maximum-number-of-eaten-apples.js diff --git a/1706-where-will-the-ball-fall.js b/1706.where-will-the-ball-fall.js similarity index 100% rename from 1706-where-will-the-ball-fall.js rename to 1706.where-will-the-ball-fall.js diff --git a/1707-maximum-xor-with-an-element-from-array.js b/1707.maximum-xor-with-an-element-from-array.js similarity index 100% rename from 1707-maximum-xor-with-an-element-from-array.js rename to 1707.maximum-xor-with-an-element-from-array.js diff --git a/1708-largest-subarray-length-k.js b/1708.largest-subarray-length-k.js similarity index 100% rename from 1708-largest-subarray-length-k.js rename to 1708.largest-subarray-length-k.js diff --git a/171-excel-sheet-column-number.js b/171.excel-sheet-column-number.js old mode 100755 new mode 100644 similarity index 95% rename from 171-excel-sheet-column-number.js rename to 171.excel-sheet-column-number.js index 1bca50e7..1b4d62d1 --- a/171-excel-sheet-column-number.js +++ b/171.excel-sheet-column-number.js @@ -1,16 +1,16 @@ -/** - * @param {string} s - * @return {number} - */ -const titleToNumber = function(s) { - const arr = s.split(""); - const len = arr.length; - const uac = "A".charCodeAt(0); - return arr.reduce((ac, el, idx, arr) => { - return ac + Math.pow(26, len - idx - 1) * (`${el}`.charCodeAt(0) - uac + 1); - }, 0); -}; - -console.log(titleToNumber("A")); -console.log(titleToNumber("AA")); - +/** + * @param {string} s + * @return {number} + */ +const titleToNumber = function(s) { + const arr = s.split(""); + const len = arr.length; + const uac = "A".charCodeAt(0); + return arr.reduce((ac, el, idx, arr) => { + return ac + Math.pow(26, len - idx - 1) * (`${el}`.charCodeAt(0) - uac + 1); + }, 0); +}; + +console.log(titleToNumber("A")); +console.log(titleToNumber("AA")); + diff --git a/1710-maximum-units-on-a-truck.js b/1710.maximum-units-on-a-truck.js similarity index 100% rename from 1710-maximum-units-on-a-truck.js rename to 1710.maximum-units-on-a-truck.js diff --git a/1711-count-good-meals.js b/1711.count-good-meals.js similarity index 100% rename from 1711-count-good-meals.js rename to 1711.count-good-meals.js diff --git a/1712-ways-to-split-array-into-three-subarrays.js b/1712.ways-to-split-array-into-three-subarrays.js similarity index 100% rename from 1712-ways-to-split-array-into-three-subarrays.js rename to 1712.ways-to-split-array-into-three-subarrays.js diff --git a/1713-minimum-operations-to-make-a-subsequence.js b/1713.minimum-operations-to-make-a-subsequence.js similarity index 100% rename from 1713-minimum-operations-to-make-a-subsequence.js rename to 1713.minimum-operations-to-make-a-subsequence.js diff --git a/1714-sum-of-special-evenly-spaced-elements-in-array.js b/1714.sum-of-special-evenly-spaced-elements-in-array.js similarity index 100% rename from 1714-sum-of-special-evenly-spaced-elements-in-array.js rename to 1714.sum-of-special-evenly-spaced-elements-in-array.js diff --git a/1716-calculate-money-in-leetcode-bank.js b/1716.calculate-money-in-leetcode-bank.js similarity index 100% rename from 1716-calculate-money-in-leetcode-bank.js rename to 1716.calculate-money-in-leetcode-bank.js diff --git a/1717-maximum-score-from-removing-substrings.js b/1717.maximum-score-from-removing-substrings.js similarity index 100% rename from 1717-maximum-score-from-removing-substrings.js rename to 1717.maximum-score-from-removing-substrings.js diff --git a/1718-construct-the-lexicographically-largest-valid-sequence.js b/1718.construct-the-lexicographically-largest-valid-sequence.js similarity index 100% rename from 1718-construct-the-lexicographically-largest-valid-sequence.js rename to 1718.construct-the-lexicographically-largest-valid-sequence.js diff --git a/1719-number-of-ways-to-reconstruct-a-tree.js b/1719.number-of-ways-to-reconstruct-a-tree.js similarity index 100% rename from 1719-number-of-ways-to-reconstruct-a-tree.js rename to 1719.number-of-ways-to-reconstruct-a-tree.js diff --git a/172-factorial-trailing-zeroes.js b/172.factorial-trailing-zeroes.js similarity index 100% rename from 172-factorial-trailing-zeroes.js rename to 172.factorial-trailing-zeroes.js diff --git a/1720-decode-xored-array.js b/1720.decode-xored-array.js similarity index 100% rename from 1720-decode-xored-array.js rename to 1720.decode-xored-array.js diff --git a/1721-swapping-nodes-in-a-linked-list.js b/1721.swapping-nodes-in-a-linked-list.js similarity index 100% rename from 1721-swapping-nodes-in-a-linked-list.js rename to 1721.swapping-nodes-in-a-linked-list.js diff --git a/1722-minimize-hamming-distance-after-swap-operations.js b/1722.minimize-hamming-distance-after-swap-operations.js similarity index 100% rename from 1722-minimize-hamming-distance-after-swap-operations.js rename to 1722.minimize-hamming-distance-after-swap-operations.js diff --git a/1723-find-minimum-time-to-finish-all-jobs.js b/1723.find-minimum-time-to-finish-all-jobs.js similarity index 100% rename from 1723-find-minimum-time-to-finish-all-jobs.js rename to 1723.find-minimum-time-to-finish-all-jobs.js diff --git a/1724-checking-existence-of-edge-length-limited-paths-ii.js b/1724.checking-existence-of-edge-length-limited-paths-ii.js similarity index 100% rename from 1724-checking-existence-of-edge-length-limited-paths-ii.js rename to 1724.checking-existence-of-edge-length-limited-paths-ii.js diff --git a/1725-number-of-rectangles-that-can-form-the-largest-square.js b/1725.number-of-rectangles-that-can-form-the-largest-square.js similarity index 100% rename from 1725-number-of-rectangles-that-can-form-the-largest-square.js rename to 1725.number-of-rectangles-that-can-form-the-largest-square.js diff --git a/1726-tuple-with-same-product.js b/1726.tuple-with-same-product.js similarity index 100% rename from 1726-tuple-with-same-product.js rename to 1726.tuple-with-same-product.js diff --git a/1727-largest-submatrix-with-rearrangements.js b/1727.largest-submatrix-with-rearrangements.js similarity index 100% rename from 1727-largest-submatrix-with-rearrangements.js rename to 1727.largest-submatrix-with-rearrangements.js diff --git a/1728-cat-and-mouse-ii.js b/1728.cat-and-mouse-ii.js similarity index 100% rename from 1728-cat-and-mouse-ii.js rename to 1728.cat-and-mouse-ii.js diff --git a/173-binary-search-tree-iterator.js b/173.binary-search-tree-iterator.js similarity index 100% rename from 173-binary-search-tree-iterator.js rename to 173.binary-search-tree-iterator.js diff --git a/1732-find-the-highest-altitude.js b/1732.find-the-highest-altitude.js similarity index 100% rename from 1732-find-the-highest-altitude.js rename to 1732.find-the-highest-altitude.js diff --git a/1733-minimum-number-of-people-to-teach.js b/1733.minimum-number-of-people-to-teach.js similarity index 100% rename from 1733-minimum-number-of-people-to-teach.js rename to 1733.minimum-number-of-people-to-teach.js diff --git a/1734-decode-xored-permutation.js b/1734.decode-xored-permutation.js similarity index 100% rename from 1734-decode-xored-permutation.js rename to 1734.decode-xored-permutation.js diff --git a/1735-count-ways-to-make-array-with-product.js b/1735.count-ways-to-make-array-with-product.js similarity index 100% rename from 1735-count-ways-to-make-array-with-product.js rename to 1735.count-ways-to-make-array-with-product.js diff --git a/1736-latest-time-by-replacing-hidden-digits.js b/1736.latest-time-by-replacing-hidden-digits.js similarity index 100% rename from 1736-latest-time-by-replacing-hidden-digits.js rename to 1736.latest-time-by-replacing-hidden-digits.js diff --git a/1737-change-minimum-characters-to-satisfy-one-of-three-conditions.js b/1737.change-minimum-characters-to-satisfy-one-of-three-conditions.js similarity index 100% rename from 1737-change-minimum-characters-to-satisfy-one-of-three-conditions.js rename to 1737.change-minimum-characters-to-satisfy-one-of-three-conditions.js diff --git a/1738-find-kth-largest-xor-coordinate-value.js b/1738.find-kth-largest-xor-coordinate-value.js similarity index 100% rename from 1738-find-kth-largest-xor-coordinate-value.js rename to 1738.find-kth-largest-xor-coordinate-value.js diff --git a/1739-building-boxes.js b/1739.building-boxes.js similarity index 100% rename from 1739-building-boxes.js rename to 1739.building-boxes.js diff --git a/174-dungeon-game.js b/174.dungeon-game.js similarity index 100% rename from 174-dungeon-game.js rename to 174.dungeon-game.js diff --git a/1740-find-distance-in-a-binary-tree.js b/1740.find-distance-in-a-binary-tree.js similarity index 100% rename from 1740-find-distance-in-a-binary-tree.js rename to 1740.find-distance-in-a-binary-tree.js diff --git a/1742-maximum-number-of-balls-in-a-box.js b/1742.maximum-number-of-balls-in-a-box.js similarity index 100% rename from 1742-maximum-number-of-balls-in-a-box.js rename to 1742.maximum-number-of-balls-in-a-box.js diff --git a/1743-restore-the-array-from-adjacent-pairs.js b/1743.restore-the-array-from-adjacent-pairs.js similarity index 100% rename from 1743-restore-the-array-from-adjacent-pairs.js rename to 1743.restore-the-array-from-adjacent-pairs.js diff --git a/1744-can-you-eat-your-favorite-candy-on-your-favorite-day.js b/1744.can-you-eat-your-favorite-candy-on-your-favorite-day.js similarity index 100% rename from 1744-can-you-eat-your-favorite-candy-on-your-favorite-day.js rename to 1744.can-you-eat-your-favorite-candy-on-your-favorite-day.js diff --git a/1745-palindrome-partitioning-iv.js b/1745.palindrome-partitioning-iv.js similarity index 100% rename from 1745-palindrome-partitioning-iv.js rename to 1745.palindrome-partitioning-iv.js diff --git a/1746-maximum-subarray-sum-after-one-operation.js b/1746.maximum-subarray-sum-after-one-operation.js similarity index 100% rename from 1746-maximum-subarray-sum-after-one-operation.js rename to 1746.maximum-subarray-sum-after-one-operation.js diff --git a/1748-sum-of-unique-elements.js b/1748.sum-of-unique-elements.js similarity index 100% rename from 1748-sum-of-unique-elements.js rename to 1748.sum-of-unique-elements.js diff --git a/1749-maximum-absolute-sum-of-any-subarray.js b/1749.maximum-absolute-sum-of-any-subarray.js similarity index 100% rename from 1749-maximum-absolute-sum-of-any-subarray.js rename to 1749.maximum-absolute-sum-of-any-subarray.js diff --git a/1750-minimum-length-of-string-after-deleting-similar-ends.js b/1750.minimum-length-of-string-after-deleting-similar-ends.js similarity index 100% rename from 1750-minimum-length-of-string-after-deleting-similar-ends.js rename to 1750.minimum-length-of-string-after-deleting-similar-ends.js diff --git a/1751-maximum-number-of-events-that-can-be-attended-ii.js b/1751.maximum-number-of-events-that-can-be-attended-ii.js similarity index 100% rename from 1751-maximum-number-of-events-that-can-be-attended-ii.js rename to 1751.maximum-number-of-events-that-can-be-attended-ii.js diff --git a/1752-check-if-array-is-sorted-and-rotated.js b/1752.check-if-array-is-sorted-and-rotated.js similarity index 100% rename from 1752-check-if-array-is-sorted-and-rotated.js rename to 1752.check-if-array-is-sorted-and-rotated.js diff --git a/1753-maximum-score-from-removing-stones.js b/1753.maximum-score-from-removing-stones.js similarity index 100% rename from 1753-maximum-score-from-removing-stones.js rename to 1753.maximum-score-from-removing-stones.js diff --git a/1754-largest-merge-of-two-strings.js b/1754.largest-merge-of-two-strings.js similarity index 100% rename from 1754-largest-merge-of-two-strings.js rename to 1754.largest-merge-of-two-strings.js diff --git a/1755-closest-subsequence-sum.js b/1755.closest-subsequence-sum.js similarity index 100% rename from 1755-closest-subsequence-sum.js rename to 1755.closest-subsequence-sum.js diff --git a/1758-minimum-changes-to-make-alternating-binary-string.js b/1758.minimum-changes-to-make-alternating-binary-string.js similarity index 100% rename from 1758-minimum-changes-to-make-alternating-binary-string.js rename to 1758.minimum-changes-to-make-alternating-binary-string.js diff --git a/1759-count-number-of-homogenous-substrings.js b/1759.count-number-of-homogenous-substrings.js similarity index 100% rename from 1759-count-number-of-homogenous-substrings.js rename to 1759.count-number-of-homogenous-substrings.js diff --git a/1760-minimum-limit-of-balls-in-a-bag.js b/1760.minimum-limit-of-balls-in-a-bag.js similarity index 100% rename from 1760-minimum-limit-of-balls-in-a-bag.js rename to 1760.minimum-limit-of-balls-in-a-bag.js diff --git a/1761-minimum-degree-of-a-connected-trio-in-a-graph.js b/1761.minimum-degree-of-a-connected-trio-in-a-graph.js similarity index 100% rename from 1761-minimum-degree-of-a-connected-trio-in-a-graph.js rename to 1761.minimum-degree-of-a-connected-trio-in-a-graph.js diff --git a/1762-buildings-with-an-ocean-view.js b/1762.buildings-with-an-ocean-view.js similarity index 100% rename from 1762-buildings-with-an-ocean-view.js rename to 1762.buildings-with-an-ocean-view.js diff --git a/1763-longest-nice-substring.js b/1763.longest-nice-substring.js similarity index 100% rename from 1763-longest-nice-substring.js rename to 1763.longest-nice-substring.js diff --git a/1764-form-array-by-concatenating-subarrays-of-another-array.js b/1764.form-array-by-concatenating-subarrays-of-another-array.js similarity index 100% rename from 1764-form-array-by-concatenating-subarrays-of-another-array.js rename to 1764.form-array-by-concatenating-subarrays-of-another-array.js diff --git a/1765-map-of-highest-peak.js b/1765.map-of-highest-peak.js similarity index 100% rename from 1765-map-of-highest-peak.js rename to 1765.map-of-highest-peak.js diff --git a/1766-tree-of-coprimes.js b/1766.tree-of-coprimes.js similarity index 100% rename from 1766-tree-of-coprimes.js rename to 1766.tree-of-coprimes.js diff --git a/1768-merge-strings-alternately.js b/1768.merge-strings-alternately.js similarity index 100% rename from 1768-merge-strings-alternately.js rename to 1768.merge-strings-alternately.js diff --git a/1769-minimum-number-of-operations-to-move-all-balls-to-each-box.js b/1769.minimum-number-of-operations-to-move-all-balls-to-each-box.js similarity index 100% rename from 1769-minimum-number-of-operations-to-move-all-balls-to-each-box.js rename to 1769.minimum-number-of-operations-to-move-all-balls-to-each-box.js diff --git a/1770-maximum-score-from-performing-multiplication-operations.js b/1770.maximum-score-from-performing-multiplication-operations.js similarity index 100% rename from 1770-maximum-score-from-performing-multiplication-operations.js rename to 1770.maximum-score-from-performing-multiplication-operations.js diff --git a/1771-maximize-palindrome-length-from-subsequences.js b/1771.maximize-palindrome-length-from-subsequences.js similarity index 100% rename from 1771-maximize-palindrome-length-from-subsequences.js rename to 1771.maximize-palindrome-length-from-subsequences.js diff --git a/1773-count-items-matching-a-rule.js b/1773.count-items-matching-a-rule.js similarity index 100% rename from 1773-count-items-matching-a-rule.js rename to 1773.count-items-matching-a-rule.js diff --git a/1774-closest-dessert-cost.js b/1774.closest-dessert-cost.js similarity index 100% rename from 1774-closest-dessert-cost.js rename to 1774.closest-dessert-cost.js diff --git a/1775-equal-sum-arrays-with-minimum-number-of-operations.js b/1775.equal-sum-arrays-with-minimum-number-of-operations.js similarity index 100% rename from 1775-equal-sum-arrays-with-minimum-number-of-operations.js rename to 1775.equal-sum-arrays-with-minimum-number-of-operations.js diff --git a/1776-car-fleet-ii.js b/1776.car-fleet-ii.js similarity index 100% rename from 1776-car-fleet-ii.js rename to 1776.car-fleet-ii.js diff --git a/1779-find-nearest-point-that-has-the-same-x-or-y-coordinate.js b/1779.find-nearest-point-that-has-the-same-x-or-y-coordinate.js similarity index 100% rename from 1779-find-nearest-point-that-has-the-same-x-or-y-coordinate.js rename to 1779.find-nearest-point-that-has-the-same-x-or-y-coordinate.js diff --git a/1780-check-if-number-is-a-sum-of-powers-of-three.js b/1780.check-if-number-is-a-sum-of-powers-of-three.js similarity index 100% rename from 1780-check-if-number-is-a-sum-of-powers-of-three.js rename to 1780.check-if-number-is-a-sum-of-powers-of-three.js diff --git a/1781-sum-of-beauty-of-all-substrings.js b/1781.sum-of-beauty-of-all-substrings.js similarity index 100% rename from 1781-sum-of-beauty-of-all-substrings.js rename to 1781.sum-of-beauty-of-all-substrings.js diff --git a/1784-check-if-binary-string-has-at-most-one-segment-of-ones.js b/1784.check-if-binary-string-has-at-most-one-segment-of-ones.js similarity index 100% rename from 1784-check-if-binary-string-has-at-most-one-segment-of-ones.js rename to 1784.check-if-binary-string-has-at-most-one-segment-of-ones.js diff --git a/1785-minimum-elements-to-add-to-form-a-given-sum.js b/1785.minimum-elements-to-add-to-form-a-given-sum.js similarity index 100% rename from 1785-minimum-elements-to-add-to-form-a-given-sum.js rename to 1785.minimum-elements-to-add-to-form-a-given-sum.js diff --git a/1786-number-of-restricted-paths-from-first-to-last-node.js b/1786.number-of-restricted-paths-from-first-to-last-node.js similarity index 100% rename from 1786-number-of-restricted-paths-from-first-to-last-node.js rename to 1786.number-of-restricted-paths-from-first-to-last-node.js diff --git a/1787-make-the-xor-of-all-segments-equal-to-zero.js b/1787.make-the-xor-of-all-segments-equal-to-zero.js similarity index 100% rename from 1787-make-the-xor-of-all-segments-equal-to-zero.js rename to 1787.make-the-xor-of-all-segments-equal-to-zero.js diff --git a/179-largest-number.js b/179.largest-number.js similarity index 100% rename from 179-largest-number.js rename to 179.largest-number.js diff --git a/1790-check-if-one-string-swap-can-make-strings-equal.js b/1790.check-if-one-string-swap-can-make-strings-equal.js similarity index 100% rename from 1790-check-if-one-string-swap-can-make-strings-equal.js rename to 1790.check-if-one-string-swap-can-make-strings-equal.js diff --git a/1791-find-center-of-star-graph.js b/1791.find-center-of-star-graph.js similarity index 100% rename from 1791-find-center-of-star-graph.js rename to 1791.find-center-of-star-graph.js diff --git a/1792-maximum-average-pass-ratio.js b/1792.maximum-average-pass-ratio.js similarity index 100% rename from 1792-maximum-average-pass-ratio.js rename to 1792.maximum-average-pass-ratio.js diff --git a/1793-maximum-score-of-a-good-subarray.js b/1793.maximum-score-of-a-good-subarray.js similarity index 100% rename from 1793-maximum-score-of-a-good-subarray.js rename to 1793.maximum-score-of-a-good-subarray.js diff --git a/1798-maximum-number-of-consecutive-values-you-can-make.js b/1798.maximum-number-of-consecutive-values-you-can-make.js similarity index 100% rename from 1798-maximum-number-of-consecutive-values-you-can-make.js rename to 1798.maximum-number-of-consecutive-values-you-can-make.js diff --git a/1799-maximize-score-after-n-operations.js b/1799.maximize-score-after-n-operations.js similarity index 100% rename from 1799-maximize-score-after-n-operations.js rename to 1799.maximize-score-after-n-operations.js diff --git a/18-4sum.js b/18.4sum.js similarity index 100% rename from 18-4sum.js rename to 18.4sum.js diff --git a/1800-maximum-ascending-subarray-sum.js b/1800.maximum-ascending-subarray-sum.js similarity index 100% rename from 1800-maximum-ascending-subarray-sum.js rename to 1800.maximum-ascending-subarray-sum.js diff --git a/1801-number-of-orders-in-the-backlog.js b/1801.number-of-orders-in-the-backlog.js similarity index 100% rename from 1801-number-of-orders-in-the-backlog.js rename to 1801.number-of-orders-in-the-backlog.js diff --git a/1802-maximum-value-at-a-given-index-in-a-bounded-array.js b/1802.maximum-value-at-a-given-index-in-a-bounded-array.js similarity index 100% rename from 1802-maximum-value-at-a-given-index-in-a-bounded-array.js rename to 1802.maximum-value-at-a-given-index-in-a-bounded-array.js diff --git a/1803-count-pairs-with-xor-in-a-range.js b/1803.count-pairs-with-xor-in-a-range.js similarity index 100% rename from 1803-count-pairs-with-xor-in-a-range.js rename to 1803.count-pairs-with-xor-in-a-range.js diff --git a/1804-implement-trie-ii-prefix-tree.js b/1804.implement-trie-ii-prefix-tree.js similarity index 100% rename from 1804-implement-trie-ii-prefix-tree.js rename to 1804.implement-trie-ii-prefix-tree.js diff --git a/1805-number-of-different-integers-in-a-string.js b/1805.number-of-different-integers-in-a-string.js similarity index 100% rename from 1805-number-of-different-integers-in-a-string.js rename to 1805.number-of-different-integers-in-a-string.js diff --git a/1806-minimum-number-of-operations-to-reinitialize-a-permutation.js b/1806.minimum-number-of-operations-to-reinitialize-a-permutation.js similarity index 100% rename from 1806-minimum-number-of-operations-to-reinitialize-a-permutation.js rename to 1806.minimum-number-of-operations-to-reinitialize-a-permutation.js diff --git a/1807-evaluate-the-bracket-pairs-of-a-string.js b/1807.evaluate-the-bracket-pairs-of-a-string.js similarity index 100% rename from 1807-evaluate-the-bracket-pairs-of-a-string.js rename to 1807.evaluate-the-bracket-pairs-of-a-string.js diff --git a/1808-maximize-number-of-nice-divisors.js b/1808.maximize-number-of-nice-divisors.js similarity index 100% rename from 1808-maximize-number-of-nice-divisors.js rename to 1808.maximize-number-of-nice-divisors.js diff --git a/1815-maximum-number-of-groups-getting-fresh-donuts.js b/1815.maximum-number-of-groups-getting-fresh-donuts.js similarity index 100% rename from 1815-maximum-number-of-groups-getting-fresh-donuts.js rename to 1815.maximum-number-of-groups-getting-fresh-donuts.js diff --git a/1816-truncate-sentence.js b/1816.truncate-sentence.js similarity index 100% rename from 1816-truncate-sentence.js rename to 1816.truncate-sentence.js diff --git a/1817-finding-the-users-active-minutes.js b/1817.finding-the-users-active-minutes.js similarity index 100% rename from 1817-finding-the-users-active-minutes.js rename to 1817.finding-the-users-active-minutes.js diff --git a/1818-minimum-absolute-sum-difference.js b/1818.minimum-absolute-sum-difference.js similarity index 100% rename from 1818-minimum-absolute-sum-difference.js rename to 1818.minimum-absolute-sum-difference.js diff --git a/1819-number-of-different-subsequences-gcds.js b/1819.number-of-different-subsequences-gcds.js similarity index 100% rename from 1819-number-of-different-subsequences-gcds.js rename to 1819.number-of-different-subsequences-gcds.js diff --git a/1822-sign-of-the-product-of-an-array.js b/1822.sign-of-the-product-of-an-array.js similarity index 100% rename from 1822-sign-of-the-product-of-an-array.js rename to 1822.sign-of-the-product-of-an-array.js diff --git a/1823-find-the-winner-of-the-circular-game.js b/1823.find-the-winner-of-the-circular-game.js similarity index 100% rename from 1823-find-the-winner-of-the-circular-game.js rename to 1823.find-the-winner-of-the-circular-game.js diff --git a/1824-minimum-sideway-jumps.js b/1824.minimum-sideway-jumps.js similarity index 100% rename from 1824-minimum-sideway-jumps.js rename to 1824.minimum-sideway-jumps.js diff --git a/1825-finding-mk-average.js b/1825.finding-mk-average.js similarity index 100% rename from 1825-finding-mk-average.js rename to 1825.finding-mk-average.js diff --git a/1827-minimum-operations-to-make-the-array-increasing.js b/1827.minimum-operations-to-make-the-array-increasing.js similarity index 100% rename from 1827-minimum-operations-to-make-the-array-increasing.js rename to 1827.minimum-operations-to-make-the-array-increasing.js diff --git a/1828-queries-on-number-of-points-inside-a-circle.js b/1828.queries-on-number-of-points-inside-a-circle.js similarity index 100% rename from 1828-queries-on-number-of-points-inside-a-circle.js rename to 1828.queries-on-number-of-points-inside-a-circle.js diff --git a/1829-maximum-xor-for-each-query.js b/1829.maximum-xor-for-each-query.js similarity index 100% rename from 1829-maximum-xor-for-each-query.js rename to 1829.maximum-xor-for-each-query.js diff --git a/1830-minimum-number-of-operations-to-make-string-sorted.js b/1830.minimum-number-of-operations-to-make-string-sorted.js similarity index 100% rename from 1830-minimum-number-of-operations-to-make-string-sorted.js rename to 1830.minimum-number-of-operations-to-make-string-sorted.js diff --git a/1832-check-if-the-sentence-is-pangram.js b/1832.check-if-the-sentence-is-pangram.js similarity index 100% rename from 1832-check-if-the-sentence-is-pangram.js rename to 1832.check-if-the-sentence-is-pangram.js diff --git a/1833-maximum-ice-cream-bars.js b/1833.maximum-ice-cream-bars.js similarity index 100% rename from 1833-maximum-ice-cream-bars.js rename to 1833.maximum-ice-cream-bars.js diff --git a/1834-single-threaded-cpu.js b/1834.single-threaded-cpu.js similarity index 100% rename from 1834-single-threaded-cpu.js rename to 1834.single-threaded-cpu.js diff --git a/1835-find-xor-sum-of-all-pairs-bitwise-and.js b/1835.find-xor-sum-of-all-pairs-bitwise-and.js similarity index 100% rename from 1835-find-xor-sum-of-all-pairs-bitwise-and.js rename to 1835.find-xor-sum-of-all-pairs-bitwise-and.js diff --git a/1836-remove-duplicates-from-an-unsorted-linked-list.js b/1836.remove-duplicates-from-an-unsorted-linked-list.js similarity index 100% rename from 1836-remove-duplicates-from-an-unsorted-linked-list.js rename to 1836.remove-duplicates-from-an-unsorted-linked-list.js diff --git a/1837-sum-of-digits-in-base-k.js b/1837.sum-of-digits-in-base-k.js similarity index 100% rename from 1837-sum-of-digits-in-base-k.js rename to 1837.sum-of-digits-in-base-k.js diff --git a/1838-frequency-of-the-most-frequent-element.js b/1838.frequency-of-the-most-frequent-element.js similarity index 100% rename from 1838-frequency-of-the-most-frequent-element.js rename to 1838.frequency-of-the-most-frequent-element.js diff --git a/1839-longest-substring-of-all-vowels-in-order.js b/1839.longest-substring-of-all-vowels-in-order.js similarity index 100% rename from 1839-longest-substring-of-all-vowels-in-order.js rename to 1839.longest-substring-of-all-vowels-in-order.js diff --git a/1840-maximum-building-height.js b/1840.maximum-building-height.js similarity index 100% rename from 1840-maximum-building-height.js rename to 1840.maximum-building-height.js diff --git a/1844-replace-all-digits-with-characters.js b/1844.replace-all-digits-with-characters.js similarity index 100% rename from 1844-replace-all-digits-with-characters.js rename to 1844.replace-all-digits-with-characters.js diff --git a/1846-maximum-element-after-decreasing-and-rearranging.js b/1846.maximum-element-after-decreasing-and-rearranging.js similarity index 100% rename from 1846-maximum-element-after-decreasing-and-rearranging.js rename to 1846.maximum-element-after-decreasing-and-rearranging.js diff --git a/1847-closest-room.js b/1847.closest-room.js similarity index 100% rename from 1847-closest-room.js rename to 1847.closest-room.js diff --git a/1848-minimum-distance-to-the-target-element.js b/1848.minimum-distance-to-the-target-element.js similarity index 100% rename from 1848-minimum-distance-to-the-target-element.js rename to 1848.minimum-distance-to-the-target-element.js diff --git a/1849-splitting-a-string-into-descending-consecutive-values.js b/1849.splitting-a-string-into-descending-consecutive-values.js similarity index 100% rename from 1849-splitting-a-string-into-descending-consecutive-values.js rename to 1849.splitting-a-string-into-descending-consecutive-values.js diff --git a/1850-minimum-adjacent-swaps-to-reach-the-kth-smallest-number.js b/1850.minimum-adjacent-swaps-to-reach-the-kth-smallest-number.js similarity index 100% rename from 1850-minimum-adjacent-swaps-to-reach-the-kth-smallest-number.js rename to 1850.minimum-adjacent-swaps-to-reach-the-kth-smallest-number.js diff --git a/1851-minimum-interval-to-include-each-query.js b/1851.minimum-interval-to-include-each-query.js similarity index 100% rename from 1851-minimum-interval-to-include-each-query.js rename to 1851.minimum-interval-to-include-each-query.js diff --git a/1854-maximum-population-year.js b/1854.maximum-population-year.js similarity index 100% rename from 1854-maximum-population-year.js rename to 1854.maximum-population-year.js diff --git a/1855-maximum-distance-between-a-pair-of-values.js b/1855.maximum-distance-between-a-pair-of-values.js similarity index 100% rename from 1855-maximum-distance-between-a-pair-of-values.js rename to 1855.maximum-distance-between-a-pair-of-values.js diff --git a/1856-maximum-subarray-min-product.js b/1856.maximum-subarray-min-product.js similarity index 100% rename from 1856-maximum-subarray-min-product.js rename to 1856.maximum-subarray-min-product.js diff --git a/1857-largest-color-value-in-a-directed-graph.js b/1857.largest-color-value-in-a-directed-graph.js similarity index 100% rename from 1857-largest-color-value-in-a-directed-graph.js rename to 1857.largest-color-value-in-a-directed-graph.js diff --git a/1859-sorting-the-sentence.js b/1859.sorting-the-sentence.js similarity index 100% rename from 1859-sorting-the-sentence.js rename to 1859.sorting-the-sentence.js diff --git a/186-reverse-words-in-a-string-ii.js b/186.reverse-words-in-a-string-ii.js similarity index 100% rename from 186-reverse-words-in-a-string-ii.js rename to 186.reverse-words-in-a-string-ii.js diff --git a/1860-incremental-memory-leak.js b/1860.incremental-memory-leak.js similarity index 100% rename from 1860-incremental-memory-leak.js rename to 1860.incremental-memory-leak.js diff --git a/1861-rotating-the-box.js b/1861.rotating-the-box.js similarity index 100% rename from 1861-rotating-the-box.js rename to 1861.rotating-the-box.js diff --git a/1862-sum-of-floored-pairs.js b/1862.sum-of-floored-pairs.js similarity index 100% rename from 1862-sum-of-floored-pairs.js rename to 1862.sum-of-floored-pairs.js diff --git a/1863-sum-of-all-subset-xor-totals.js b/1863.sum-of-all-subset-xor-totals.js similarity index 100% rename from 1863-sum-of-all-subset-xor-totals.js rename to 1863.sum-of-all-subset-xor-totals.js diff --git a/1864-minimum-number-of-swaps-to-make-the-binary-string-alternating.js b/1864.minimum-number-of-swaps-to-make-the-binary-string-alternating.js similarity index 100% rename from 1864-minimum-number-of-swaps-to-make-the-binary-string-alternating.js rename to 1864.minimum-number-of-swaps-to-make-the-binary-string-alternating.js diff --git a/1865-finding-pairs-with-a-certain-sum.js b/1865.finding-pairs-with-a-certain-sum.js similarity index 100% rename from 1865-finding-pairs-with-a-certain-sum.js rename to 1865.finding-pairs-with-a-certain-sum.js diff --git a/1866-number-of-ways-to-rearrange-sticks-with-k-sticks-visible.js b/1866.number-of-ways-to-rearrange-sticks-with-k-sticks-visible.js similarity index 100% rename from 1866-number-of-ways-to-rearrange-sticks-with-k-sticks-visible.js rename to 1866.number-of-ways-to-rearrange-sticks-with-k-sticks-visible.js diff --git a/1869-longer-contiguous-segments-of-ones-than-zeros.js b/1869.longer-contiguous-segments-of-ones-than-zeros.js similarity index 100% rename from 1869-longer-contiguous-segments-of-ones-than-zeros.js rename to 1869.longer-contiguous-segments-of-ones-than-zeros.js diff --git a/187-repeated-dna-sequences.js b/187.repeated-dna-sequences.js similarity index 100% rename from 187-repeated-dna-sequences.js rename to 187.repeated-dna-sequences.js diff --git a/1870-minimum-speed-to-arrive-on-time.js b/1870.minimum-speed-to-arrive-on-time.js similarity index 100% rename from 1870-minimum-speed-to-arrive-on-time.js rename to 1870.minimum-speed-to-arrive-on-time.js diff --git a/1871-jump-game-vii.js b/1871.jump-game-vii.js similarity index 100% rename from 1871-jump-game-vii.js rename to 1871.jump-game-vii.js diff --git a/1872-stone-game-viii.js b/1872.stone-game-viii.js similarity index 100% rename from 1872-stone-game-viii.js rename to 1872.stone-game-viii.js diff --git a/1874-minimize-product-sum-of-two-arrays.js b/1874.minimize-product-sum-of-two-arrays.js similarity index 100% rename from 1874-minimize-product-sum-of-two-arrays.js rename to 1874.minimize-product-sum-of-two-arrays.js diff --git a/1876-substrings-of-size-three-with-distinct-characters.js b/1876.substrings-of-size-three-with-distinct-characters.js similarity index 100% rename from 1876-substrings-of-size-three-with-distinct-characters.js rename to 1876.substrings-of-size-three-with-distinct-characters.js diff --git a/1877-minimize-maximum-pair-sum-in-array.js b/1877.minimize-maximum-pair-sum-in-array.js similarity index 100% rename from 1877-minimize-maximum-pair-sum-in-array.js rename to 1877.minimize-maximum-pair-sum-in-array.js diff --git a/1878-get-biggest-three-rhombus-sums-in-a-grid.js b/1878.get-biggest-three-rhombus-sums-in-a-grid.js similarity index 100% rename from 1878-get-biggest-three-rhombus-sums-in-a-grid.js rename to 1878.get-biggest-three-rhombus-sums-in-a-grid.js diff --git a/1879-minimum-xor-sum-of-two-arrays.js b/1879.minimum-xor-sum-of-two-arrays.js similarity index 100% rename from 1879-minimum-xor-sum-of-two-arrays.js rename to 1879.minimum-xor-sum-of-two-arrays.js diff --git a/188-best-time-to-buy-and-sell-stock-iv.js b/188.best-time-to-buy-and-sell-stock-iv.js similarity index 100% rename from 188-best-time-to-buy-and-sell-stock-iv.js rename to 188.best-time-to-buy-and-sell-stock-iv.js diff --git a/1880-check-if-word-equals-summation-of-two-words.js b/1880.check-if-word-equals-summation-of-two-words.js similarity index 100% rename from 1880-check-if-word-equals-summation-of-two-words.js rename to 1880.check-if-word-equals-summation-of-two-words.js diff --git a/1881-maximum-value-after-insertion.js b/1881.maximum-value-after-insertion.js similarity index 100% rename from 1881-maximum-value-after-insertion.js rename to 1881.maximum-value-after-insertion.js diff --git a/1882-process-tasks-using-servers.js b/1882.process-tasks-using-servers.js similarity index 100% rename from 1882-process-tasks-using-servers.js rename to 1882.process-tasks-using-servers.js diff --git a/1883-minimum-skips-to-arrive-at-meeting-on-time.js b/1883.minimum-skips-to-arrive-at-meeting-on-time.js similarity index 100% rename from 1883-minimum-skips-to-arrive-at-meeting-on-time.js rename to 1883.minimum-skips-to-arrive-at-meeting-on-time.js diff --git a/1884-egg-drop-with-2-eggs-and-n-floors.js b/1884.egg-drop-with-2-eggs-and-n-floors.js similarity index 100% rename from 1884-egg-drop-with-2-eggs-and-n-floors.js rename to 1884.egg-drop-with-2-eggs-and-n-floors.js diff --git a/1885-count-pairs-in-two-arrays.js b/1885.count-pairs-in-two-arrays.js similarity index 100% rename from 1885-count-pairs-in-two-arrays.js rename to 1885.count-pairs-in-two-arrays.js diff --git a/1886-determine-whether-matrix-can-be-obtained-by-rotation.js b/1886.determine-whether-matrix-can-be-obtained-by-rotation.js similarity index 100% rename from 1886-determine-whether-matrix-can-be-obtained-by-rotation.js rename to 1886.determine-whether-matrix-can-be-obtained-by-rotation.js diff --git a/1887-reduction-operations-to-make-the-array-elements-equal.js b/1887.reduction-operations-to-make-the-array-elements-equal.js similarity index 100% rename from 1887-reduction-operations-to-make-the-array-elements-equal.js rename to 1887.reduction-operations-to-make-the-array-elements-equal.js diff --git a/1888-minimum-number-of-flips-to-make-the-binary-string-alternating.js b/1888.minimum-number-of-flips-to-make-the-binary-string-alternating.js similarity index 100% rename from 1888-minimum-number-of-flips-to-make-the-binary-string-alternating.js rename to 1888.minimum-number-of-flips-to-make-the-binary-string-alternating.js diff --git a/1889-minimum-space-wasted-from-packaging.js b/1889.minimum-space-wasted-from-packaging.js similarity index 100% rename from 1889-minimum-space-wasted-from-packaging.js rename to 1889.minimum-space-wasted-from-packaging.js diff --git a/189-rotate-array.js b/189.rotate-array.js similarity index 100% rename from 189-rotate-array.js rename to 189.rotate-array.js diff --git a/1891-cutting-ribbons.js b/1891.cutting-ribbons.js similarity index 100% rename from 1891-cutting-ribbons.js rename to 1891.cutting-ribbons.js diff --git a/1893-check-if-all-the-integers-in-a-range-are-covered.js b/1893.check-if-all-the-integers-in-a-range-are-covered.js similarity index 100% rename from 1893-check-if-all-the-integers-in-a-range-are-covered.js rename to 1893.check-if-all-the-integers-in-a-range-are-covered.js diff --git a/1896-minimum-cost-to-change-the-final-value-of-expression.js b/1896.minimum-cost-to-change-the-final-value-of-expression.js similarity index 100% rename from 1896-minimum-cost-to-change-the-final-value-of-expression.js rename to 1896.minimum-cost-to-change-the-final-value-of-expression.js diff --git a/1897-redistribute-characters-to-make-all-strings-equal.js b/1897.redistribute-characters-to-make-all-strings-equal.js similarity index 100% rename from 1897-redistribute-characters-to-make-all-strings-equal.js rename to 1897.redistribute-characters-to-make-all-strings-equal.js diff --git a/1898-maximum-number-of-removable-characters.js b/1898.maximum-number-of-removable-characters.js similarity index 100% rename from 1898-maximum-number-of-removable-characters.js rename to 1898.maximum-number-of-removable-characters.js diff --git a/1899-merge-triplets-to-form-target-triplet.js b/1899.merge-triplets-to-form-target-triplet.js similarity index 100% rename from 1899-merge-triplets-to-form-target-triplet.js rename to 1899.merge-triplets-to-form-target-triplet.js diff --git a/19-remove-nth-node-from-end-of-list.js b/19.remove-nth-node-from-end-of-list.js old mode 100755 new mode 100644 similarity index 95% rename from 19-remove-nth-node-from-end-of-list.js rename to 19.remove-nth-node-from-end-of-list.js index e5390374..51f62d93 --- a/19-remove-nth-node-from-end-of-list.js +++ b/19.remove-nth-node-from-end-of-list.js @@ -1,31 +1,31 @@ -/** - * Definition for singly-linked list. - * function ListNode(val) { - * this.val = val; - * this.next = null; - * } - */ -/** - * @param {ListNode} head - * @param {number} n - * @return {ListNode} - */ -const removeNthFromEnd = (head, n) => { - if (head.next === null) return null; - - let ptrBeforeN = head; - let count = 1; - - // While there are more elements - let el = head.next; - while (el !== null) { - if (count > n) ptrBeforeN = ptrBeforeN.next; - el = el.next; - count++; - } - - if (count === n) return head.next; - - ptrBeforeN.next = ptrBeforeN.next.next; - return head; -}; +/** + * Definition for singly-linked list. + * function ListNode(val) { + * this.val = val; + * this.next = null; + * } + */ +/** + * @param {ListNode} head + * @param {number} n + * @return {ListNode} + */ +const removeNthFromEnd = (head, n) => { + if (head.next === null) return null; + + let ptrBeforeN = head; + let count = 1; + + // While there are more elements + let el = head.next; + while (el !== null) { + if (count > n) ptrBeforeN = ptrBeforeN.next; + el = el.next; + count++; + } + + if (count === n) return head.next; + + ptrBeforeN.next = ptrBeforeN.next.next; + return head; +}; diff --git a/190-reverse-bits.js b/190.reverse-bits.js similarity index 100% rename from 190-reverse-bits.js rename to 190.reverse-bits.js diff --git a/1900-the-earliest-and-latest-rounds-where-players-compete.js b/1900.the-earliest-and-latest-rounds-where-players-compete.js similarity index 100% rename from 1900-the-earliest-and-latest-rounds-where-players-compete.js rename to 1900.the-earliest-and-latest-rounds-where-players-compete.js diff --git a/1901-find-a-peak-element-ii.js b/1901.find-a-peak-element-ii.js similarity index 100% rename from 1901-find-a-peak-element-ii.js rename to 1901.find-a-peak-element-ii.js diff --git a/1903-largest-odd-number-in-string.js b/1903.largest-odd-number-in-string.js similarity index 100% rename from 1903-largest-odd-number-in-string.js rename to 1903.largest-odd-number-in-string.js diff --git a/1904-the-number-of-full-rounds-you-have-played.js b/1904.the-number-of-full-rounds-you-have-played.js similarity index 100% rename from 1904-the-number-of-full-rounds-you-have-played.js rename to 1904.the-number-of-full-rounds-you-have-played.js diff --git a/1905-count-sub-islands.js b/1905.count-sub-islands.js similarity index 100% rename from 1905-count-sub-islands.js rename to 1905.count-sub-islands.js diff --git a/1906-minimum-absolute-difference-queries.js b/1906.minimum-absolute-difference-queries.js similarity index 100% rename from 1906-minimum-absolute-difference-queries.js rename to 1906.minimum-absolute-difference-queries.js diff --git a/1909-remove-one-element-to-make-the-array-strictly-increasing.js b/1909.remove-one-element-to-make-the-array-strictly-increasing.js similarity index 100% rename from 1909-remove-one-element-to-make-the-array-strictly-increasing.js rename to 1909.remove-one-element-to-make-the-array-strictly-increasing.js diff --git a/191-number-of-1-bits.js b/191.number-of-1-bits.js similarity index 100% rename from 191-number-of-1-bits.js rename to 191.number-of-1-bits.js diff --git a/1910-remove-all-occurrences-of-a-substring.js b/1910.remove-all-occurrences-of-a-substring.js similarity index 100% rename from 1910-remove-all-occurrences-of-a-substring.js rename to 1910.remove-all-occurrences-of-a-substring.js diff --git a/1911-maximum-alternating-subsequence-sum.js b/1911.maximum-alternating-subsequence-sum.js similarity index 100% rename from 1911-maximum-alternating-subsequence-sum.js rename to 1911.maximum-alternating-subsequence-sum.js diff --git a/1913-maximum-product-difference-between-two-pairs.js b/1913.maximum-product-difference-between-two-pairs.js similarity index 100% rename from 1913-maximum-product-difference-between-two-pairs.js rename to 1913.maximum-product-difference-between-two-pairs.js diff --git a/1914-cyclically-rotating-a-gridsubmissions.js b/1914.cyclically-rotating-a-gridsubmissions.js similarity index 100% rename from 1914-cyclically-rotating-a-gridsubmissions.js rename to 1914.cyclically-rotating-a-gridsubmissions.js diff --git a/1915-number-of-wonderful-substrings.js b/1915.number-of-wonderful-substrings.js similarity index 100% rename from 1915-number-of-wonderful-substrings.js rename to 1915.number-of-wonderful-substrings.js diff --git a/1916-count-ways-to-build-rooms-in-an-ant-colony.js b/1916.count-ways-to-build-rooms-in-an-ant-colony.js similarity index 100% rename from 1916-count-ways-to-build-rooms-in-an-ant-colony.js rename to 1916.count-ways-to-build-rooms-in-an-ant-colony.js diff --git a/1918-kth-smallest-subarray-sum.js b/1918.kth-smallest-subarray-sum.js similarity index 100% rename from 1918-kth-smallest-subarray-sum.js rename to 1918.kth-smallest-subarray-sum.js diff --git a/1920-build-array-from-permutation.js b/1920.build-array-from-permutation.js similarity index 100% rename from 1920-build-array-from-permutation.js rename to 1920.build-array-from-permutation.js diff --git a/1921-eliminate-maximum-number-of-monsters.js b/1921.eliminate-maximum-number-of-monsters.js similarity index 100% rename from 1921-eliminate-maximum-number-of-monsters.js rename to 1921.eliminate-maximum-number-of-monsters.js diff --git a/1922-count-good-numbers.js b/1922.count-good-numbers.js similarity index 100% rename from 1922-count-good-numbers.js rename to 1922.count-good-numbers.js diff --git a/1923-longest-common-subpath.js b/1923.longest-common-subpath.js similarity index 100% rename from 1923-longest-common-subpath.js rename to 1923.longest-common-subpath.js diff --git a/1925-count-square-sum-triples.js b/1925.count-square-sum-triples.js similarity index 100% rename from 1925-count-square-sum-triples.js rename to 1925.count-square-sum-triples.js diff --git a/1928-minimum-cost-to-reach-destination-in-time.js b/1928.minimum-cost-to-reach-destination-in-time.js similarity index 100% rename from 1928-minimum-cost-to-reach-destination-in-time.js rename to 1928.minimum-cost-to-reach-destination-in-time.js diff --git a/1929-concatenation-of-array.js b/1929.concatenation-of-array.js similarity index 100% rename from 1929-concatenation-of-array.js rename to 1929.concatenation-of-array.js diff --git a/1930-unique-length-3-palindromic-subsequences.js b/1930.unique-length-3-palindromic-subsequences.js similarity index 100% rename from 1930-unique-length-3-palindromic-subsequences.js rename to 1930.unique-length-3-palindromic-subsequences.js diff --git a/1931-painting-a-grid-with-three-different-colors.js b/1931.painting-a-grid-with-three-different-colors.js similarity index 100% rename from 1931-painting-a-grid-with-three-different-colors.js rename to 1931.painting-a-grid-with-three-different-colors.js diff --git a/1932-merge-bsts-to-create-single-bst.js b/1932.merge-bsts-to-create-single-bst.js similarity index 100% rename from 1932-merge-bsts-to-create-single-bst.js rename to 1932.merge-bsts-to-create-single-bst.js diff --git a/1935-maximum-number-of-words-you-can-type.js b/1935.maximum-number-of-words-you-can-type.js similarity index 100% rename from 1935-maximum-number-of-words-you-can-type.js rename to 1935.maximum-number-of-words-you-can-type.js diff --git a/1936-add-minimum-number-of-rungs.js b/1936.add-minimum-number-of-rungs.js similarity index 100% rename from 1936-add-minimum-number-of-rungs.js rename to 1936.add-minimum-number-of-rungs.js diff --git a/1937-maximum-number-of-points-with-cost.js b/1937.maximum-number-of-points-with-cost.js similarity index 100% rename from 1937-maximum-number-of-points-with-cost.js rename to 1937.maximum-number-of-points-with-cost.js diff --git a/1938-maximum-genetic-difference-query.js b/1938.maximum-genetic-difference-query.js similarity index 100% rename from 1938-maximum-genetic-difference-query.js rename to 1938.maximum-genetic-difference-query.js diff --git a/1941-check-if-all-characters-have-equal-number-of-occurrences.js b/1941.check-if-all-characters-have-equal-number-of-occurrences.js similarity index 100% rename from 1941-check-if-all-characters-have-equal-number-of-occurrences.js rename to 1941.check-if-all-characters-have-equal-number-of-occurrences.js diff --git a/1942-the-number-of-the-smallest-unoccupied-chair.js b/1942.the-number-of-the-smallest-unoccupied-chair.js similarity index 100% rename from 1942-the-number-of-the-smallest-unoccupied-chair.js rename to 1942.the-number-of-the-smallest-unoccupied-chair.js diff --git a/1943-describe-the-painting.js b/1943.describe-the-painting.js similarity index 100% rename from 1943-describe-the-painting.js rename to 1943.describe-the-painting.js diff --git a/1944-number-of-visible-people-in-a-queue.js b/1944.number-of-visible-people-in-a-queue.js similarity index 100% rename from 1944-number-of-visible-people-in-a-queue.js rename to 1944.number-of-visible-people-in-a-queue.js diff --git a/1945-sum-of-digits-of-string-after-convert.js b/1945.sum-of-digits-of-string-after-convert.js similarity index 100% rename from 1945-sum-of-digits-of-string-after-convert.js rename to 1945.sum-of-digits-of-string-after-convert.js diff --git a/1946-largest-number-after-mutating-substring.js b/1946.largest-number-after-mutating-substring.js similarity index 100% rename from 1946-largest-number-after-mutating-substring.js rename to 1946.largest-number-after-mutating-substring.js diff --git a/1947-maximum-compatibility-score-sum.js b/1947.maximum-compatibility-score-sum.js similarity index 100% rename from 1947-maximum-compatibility-score-sum.js rename to 1947.maximum-compatibility-score-sum.js diff --git a/1948-delete-duplicate-folders-in-system.js b/1948.delete-duplicate-folders-in-system.js similarity index 100% rename from 1948-delete-duplicate-folders-in-system.js rename to 1948.delete-duplicate-folders-in-system.js diff --git a/1952-three-divisors.js b/1952.three-divisors.js similarity index 100% rename from 1952-three-divisors.js rename to 1952.three-divisors.js diff --git a/1953-maximum-number-of-weeks-for-which-you-can-work.js b/1953.maximum-number-of-weeks-for-which-you-can-work.js similarity index 100% rename from 1953-maximum-number-of-weeks-for-which-you-can-work.js rename to 1953.maximum-number-of-weeks-for-which-you-can-work.js diff --git a/1954-minimum-garden-perimeter-to-collect-enough-apples.js b/1954.minimum-garden-perimeter-to-collect-enough-apples.js similarity index 100% rename from 1954-minimum-garden-perimeter-to-collect-enough-apples.js rename to 1954.minimum-garden-perimeter-to-collect-enough-apples.js diff --git a/1955-count-number-of-special-subsequences.js b/1955.count-number-of-special-subsequences.js similarity index 100% rename from 1955-count-number-of-special-subsequences.js rename to 1955.count-number-of-special-subsequences.js diff --git a/1959-minimum-total-space-wasted-with-k-resizing-operations.js b/1959.minimum-total-space-wasted-with-k-resizing-operations.js similarity index 100% rename from 1959-minimum-total-space-wasted-with-k-resizing-operations.js rename to 1959.minimum-total-space-wasted-with-k-resizing-operations.js diff --git a/1960-maximum-product-of-the-length-of-two-palindromic-substrings.js b/1960.maximum-product-of-the-length-of-two-palindromic-substrings.js similarity index 100% rename from 1960-maximum-product-of-the-length-of-two-palindromic-substrings.js rename to 1960.maximum-product-of-the-length-of-two-palindromic-substrings.js diff --git a/1961-check-if-string-is-a-prefix-of-array.js b/1961.check-if-string-is-a-prefix-of-array.js similarity index 100% rename from 1961-check-if-string-is-a-prefix-of-array.js rename to 1961.check-if-string-is-a-prefix-of-array.js diff --git a/1962-remove-stones-to-minimize-the-total.js b/1962.remove-stones-to-minimize-the-total.js similarity index 100% rename from 1962-remove-stones-to-minimize-the-total.js rename to 1962.remove-stones-to-minimize-the-total.js diff --git a/1963-minimum-number-of-swaps-to-make-the-string-balanced.js b/1963.minimum-number-of-swaps-to-make-the-string-balanced.js similarity index 100% rename from 1963-minimum-number-of-swaps-to-make-the-string-balanced.js rename to 1963.minimum-number-of-swaps-to-make-the-string-balanced.js diff --git a/1964-find-the-longest-valid-obstacle-course-at-each-position.js b/1964.find-the-longest-valid-obstacle-course-at-each-position.js similarity index 100% rename from 1964-find-the-longest-valid-obstacle-course-at-each-position.js rename to 1964.find-the-longest-valid-obstacle-course-at-each-position.js diff --git a/1969-minimum-non-zero-product-of-the-array-elements.js b/1969.minimum-non-zero-product-of-the-array-elements.js similarity index 100% rename from 1969-minimum-non-zero-product-of-the-array-elements.js rename to 1969.minimum-non-zero-product-of-the-array-elements.js diff --git a/1970-last-day-where-you-can-still-cross.js b/1970.last-day-where-you-can-still-cross.js similarity index 100% rename from 1970-last-day-where-you-can-still-cross.js rename to 1970.last-day-where-you-can-still-cross.js diff --git a/1971-find-if-path-exists-in-graph.js b/1971.find-if-path-exists-in-graph.js similarity index 100% rename from 1971-find-if-path-exists-in-graph.js rename to 1971.find-if-path-exists-in-graph.js diff --git a/1976-number-of-ways-to-arrive-at-destination.js b/1976.number-of-ways-to-arrive-at-destination.js similarity index 100% rename from 1976-number-of-ways-to-arrive-at-destination.js rename to 1976.number-of-ways-to-arrive-at-destination.js diff --git a/1977-number-of-ways-to-separate-numbers.js b/1977.number-of-ways-to-separate-numbers.js similarity index 100% rename from 1977-number-of-ways-to-separate-numbers.js rename to 1977.number-of-ways-to-separate-numbers.js diff --git a/1979-find-greatest-common-divisor-of-array.js b/1979.find-greatest-common-divisor-of-array.js similarity index 100% rename from 1979-find-greatest-common-divisor-of-array.js rename to 1979.find-greatest-common-divisor-of-array.js diff --git a/198-house-robber.js b/198.house-robber.js similarity index 100% rename from 198-house-robber.js rename to 198.house-robber.js diff --git a/1980-find-unique-binary-string.js b/1980.find-unique-binary-string.js similarity index 100% rename from 1980-find-unique-binary-string.js rename to 1980.find-unique-binary-string.js diff --git a/1981-minimize-the-difference-between-target-and-chosen-elements.js b/1981.minimize-the-difference-between-target-and-chosen-elements.js similarity index 100% rename from 1981-minimize-the-difference-between-target-and-chosen-elements.js rename to 1981.minimize-the-difference-between-target-and-chosen-elements.js diff --git a/1982-find-array-given-subset-sums.js b/1982.find-array-given-subset-sums.js similarity index 100% rename from 1982-find-array-given-subset-sums.js rename to 1982.find-array-given-subset-sums.js diff --git a/1983-widest-pair-of-indices-with-equal-range-sum.js b/1983.widest-pair-of-indices-with-equal-range-sum.js similarity index 100% rename from 1983-widest-pair-of-indices-with-equal-range-sum.js rename to 1983.widest-pair-of-indices-with-equal-range-sum.js diff --git a/1984-minimum-difference-between-highest-and-lowest-of-k-scores.js b/1984.minimum-difference-between-highest-and-lowest-of-k-scores.js similarity index 100% rename from 1984-minimum-difference-between-highest-and-lowest-of-k-scores.js rename to 1984.minimum-difference-between-highest-and-lowest-of-k-scores.js diff --git a/1985-find-the-kth-largest-integer-in-the-array.js b/1985.find-the-kth-largest-integer-in-the-array.js similarity index 100% rename from 1985-find-the-kth-largest-integer-in-the-array.js rename to 1985.find-the-kth-largest-integer-in-the-array.js diff --git a/1986-minimum-number-of-work-sessions-to-finish-the-tasks.js b/1986.minimum-number-of-work-sessions-to-finish-the-tasks.js similarity index 100% rename from 1986-minimum-number-of-work-sessions-to-finish-the-tasks.js rename to 1986.minimum-number-of-work-sessions-to-finish-the-tasks.js diff --git a/1987-number-of-unique-good-subsequences.js b/1987.number-of-unique-good-subsequences.js similarity index 100% rename from 1987-number-of-unique-good-subsequences.js rename to 1987.number-of-unique-good-subsequences.js diff --git a/199-binary-tree-right-side-view.js b/199.binary-tree-right-side-view.js similarity index 100% rename from 199-binary-tree-right-side-view.js rename to 199.binary-tree-right-side-view.js diff --git a/1991-find-the-middle-index-in-array.js b/1991.find-the-middle-index-in-array.js similarity index 100% rename from 1991-find-the-middle-index-in-array.js rename to 1991.find-the-middle-index-in-array.js diff --git a/1994-the-number-of-good-subsets.js b/1994.the-number-of-good-subsets.js similarity index 100% rename from 1994-the-number-of-good-subsets.js rename to 1994.the-number-of-good-subsets.js diff --git a/1995-count-special-quadruplets.js b/1995.count-special-quadruplets.js similarity index 100% rename from 1995-count-special-quadruplets.js rename to 1995.count-special-quadruplets.js diff --git a/1996-the-number-of-weak-characters-in-the-game.js b/1996.the-number-of-weak-characters-in-the-game.js similarity index 100% rename from 1996-the-number-of-weak-characters-in-the-game.js rename to 1996.the-number-of-weak-characters-in-the-game.js diff --git a/1997-first-day-where-you-have-been-in-all-the-rooms.js b/1997.first-day-where-you-have-been-in-all-the-rooms.js similarity index 100% rename from 1997-first-day-where-you-have-been-in-all-the-rooms.js rename to 1997.first-day-where-you-have-been-in-all-the-rooms.js diff --git a/1998-gcd-sort-of-an-array.js b/1998.gcd-sort-of-an-array.js similarity index 100% rename from 1998-gcd-sort-of-an-array.js rename to 1998.gcd-sort-of-an-array.js diff --git a/2-add-two-numbers.js b/2.add-two-numbers.js old mode 100755 new mode 100644 similarity index 95% rename from 2-add-two-numbers.js rename to 2.add-two-numbers.js index 0bb60871..e2a683d0 --- a/2-add-two-numbers.js +++ b/2.add-two-numbers.js @@ -1,29 +1,29 @@ -/** - * Definition for singly-linked list. - * function ListNode(val, next) { - * this.val = (val===undefined ? 0 : val) - * this.next = (next===undefined ? null : next) - * } - */ -/** - * @param {ListNode} l1 - * @param {ListNode} l2 - * @return {ListNode} - */ -const addTwoNumbers = function(l1, l2) { - let res = new ListNode(null) - let inc = false - let cur = res - while(l1 || l2 || inc) { - const tmp = ((l1 && l1.val) || 0) + ((l2 && l2.val) || 0) + (inc ? 1 : 0) - if(tmp >= 10) inc = true - else inc = false - cur.next = new ListNode(tmp % 10) - cur = cur.next - if(l1) l1 = l1.next - if(l2) l2 = l2.next - } - - return res.next -}; - +/** + * Definition for singly-linked list. + * function ListNode(val, next) { + * this.val = (val===undefined ? 0 : val) + * this.next = (next===undefined ? null : next) + * } + */ +/** + * @param {ListNode} l1 + * @param {ListNode} l2 + * @return {ListNode} + */ +const addTwoNumbers = function(l1, l2) { + let res = new ListNode(null) + let inc = false + let cur = res + while(l1 || l2 || inc) { + const tmp = ((l1 && l1.val) || 0) + ((l2 && l2.val) || 0) + (inc ? 1 : 0) + if(tmp >= 10) inc = true + else inc = false + cur.next = new ListNode(tmp % 10) + cur = cur.next + if(l1) l1 = l1.next + if(l2) l2 = l2.next + } + + return res.next +}; + diff --git a/20-valid-parentheses.js b/20.valid-parentheses.js old mode 100755 new mode 100644 similarity index 95% rename from 20-valid-parentheses.js rename to 20.valid-parentheses.js index 3d9e0f06..f71eeeef --- a/20-valid-parentheses.js +++ b/20.valid-parentheses.js @@ -1,16 +1,16 @@ -/** - * @param {string} s - * @return {boolean} - */ - const isValid = function(s) { - const stack = [] - for(let c of s) { - if(c === '(') stack.push(')') - else if(c === '{') stack.push('}') - else if(c === '[') stack.push(']') - else if(stack.length === 0 || c !== stack.pop()) return false - } - return stack.length === 0 -}; - - +/** + * @param {string} s + * @return {boolean} + */ + const isValid = function(s) { + const stack = [] + for(let c of s) { + if(c === '(') stack.push(')') + else if(c === '{') stack.push('}') + else if(c === '[') stack.push(']') + else if(stack.length === 0 || c !== stack.pop()) return false + } + return stack.length === 0 +}; + + diff --git a/200-number-of-islands.js b/200.number-of-islands.js similarity index 100% rename from 200-number-of-islands.js rename to 200.number-of-islands.js diff --git a/2000-reverse-prefix-of-word.js b/2000.reverse-prefix-of-word.js similarity index 100% rename from 2000-reverse-prefix-of-word.js rename to 2000.reverse-prefix-of-word.js diff --git a/2001-number-of-pairs-of-interchangeable-rectangles.js b/2001.number-of-pairs-of-interchangeable-rectangles.js similarity index 100% rename from 2001-number-of-pairs-of-interchangeable-rectangles.js rename to 2001.number-of-pairs-of-interchangeable-rectangles.js diff --git a/2002-maximum-product-of-the-length-of-two-palindromic-subsequences.js b/2002.maximum-product-of-the-length-of-two-palindromic-subsequences.js similarity index 100% rename from 2002-maximum-product-of-the-length-of-two-palindromic-subsequences.js rename to 2002.maximum-product-of-the-length-of-two-palindromic-subsequences.js diff --git a/2003-smallest-missing-genetic-value-in-each-subtree.js b/2003.smallest-missing-genetic-value-in-each-subtree.js similarity index 100% rename from 2003-smallest-missing-genetic-value-in-each-subtree.js rename to 2003.smallest-missing-genetic-value-in-each-subtree.js diff --git a/2006-count-number-of-pairs-with-absolute-difference-k.js b/2006.count-number-of-pairs-with-absolute-difference-k.js similarity index 100% rename from 2006-count-number-of-pairs-with-absolute-difference-k.js rename to 2006.count-number-of-pairs-with-absolute-difference-k.js diff --git a/2007-find-original-array-from-doubled-array.js b/2007.find-original-array-from-doubled-array.js similarity index 100% rename from 2007-find-original-array-from-doubled-array.js rename to 2007.find-original-array-from-doubled-array.js diff --git a/2008-maximum-earnings-from-taxi.js b/2008.maximum-earnings-from-taxi.js similarity index 100% rename from 2008-maximum-earnings-from-taxi.js rename to 2008.maximum-earnings-from-taxi.js diff --git a/2009-minimum-number-of-operations-to-make-array-continuous.js b/2009.minimum-number-of-operations-to-make-array-continuous.js similarity index 100% rename from 2009-minimum-number-of-operations-to-make-array-continuous.js rename to 2009.minimum-number-of-operations-to-make-array-continuous.js diff --git a/201-bitwise-and-of-numbers-range.js b/201.bitwise-and-of-numbers-range.js similarity index 100% rename from 201-bitwise-and-of-numbers-range.js rename to 201.bitwise-and-of-numbers-range.js diff --git a/2011-final-value-of-variable-after-performing-operations.js b/2011.final-value-of-variable-after-performing-operations.js similarity index 100% rename from 2011-final-value-of-variable-after-performing-operations.js rename to 2011.final-value-of-variable-after-performing-operations.js diff --git a/2012-sum-of-beauty-in-the-array.js b/2012.sum-of-beauty-in-the-array.js similarity index 100% rename from 2012-sum-of-beauty-in-the-array.js rename to 2012.sum-of-beauty-in-the-array.js diff --git a/2013-detect-squares.js b/2013.detect-squares.js similarity index 100% rename from 2013-detect-squares.js rename to 2013.detect-squares.js diff --git a/2014-longest-subsequence-repeated-k-times.js b/2014.longest-subsequence-repeated-k-times.js similarity index 100% rename from 2014-longest-subsequence-repeated-k-times.js rename to 2014.longest-subsequence-repeated-k-times.js diff --git a/2015-average-height-of-buildings-in-each-segment.js b/2015.average-height-of-buildings-in-each-segment.js similarity index 100% rename from 2015-average-height-of-buildings-in-each-segment.js rename to 2015.average-height-of-buildings-in-each-segment.js diff --git a/2018-check-if-word-can-be-placed-in-crossword.js b/2018.check-if-word-can-be-placed-in-crossword.js similarity index 100% rename from 2018-check-if-word-can-be-placed-in-crossword.js rename to 2018.check-if-word-can-be-placed-in-crossword.js diff --git a/2019-the-score-of-students-solving-math-expression.js b/2019.the-score-of-students-solving-math-expression.js similarity index 100% rename from 2019-the-score-of-students-solving-math-expression.js rename to 2019.the-score-of-students-solving-math-expression.js diff --git a/202-happy-number.js b/202.happy-number.js old mode 100755 new mode 100644 similarity index 94% rename from 202-happy-number.js rename to 202.happy-number.js index 340a34ca..135632db --- a/202-happy-number.js +++ b/202.happy-number.js @@ -1,19 +1,19 @@ -/** - * @param {number} n - * @return {boolean} - */ -const isHappy = function(n) { - const arr = []; - let tmp = n; - while (arr.indexOf(tmp) === -1) { - arr.push(tmp); - let res = ("" + tmp) - .split("") - .reduce((ac, str) => ac + Math.pow(+str, 2), 0); - if (res === 1) { - return true; - } - tmp = res; - } - return false; -}; +/** + * @param {number} n + * @return {boolean} + */ +const isHappy = function(n) { + const arr = []; + let tmp = n; + while (arr.indexOf(tmp) === -1) { + arr.push(tmp); + let res = ("" + tmp) + .split("") + .reduce((ac, str) => ac + Math.pow(+str, 2), 0); + if (res === 1) { + return true; + } + tmp = res; + } + return false; +}; diff --git a/2023-number-of-pairs-of-strings-with-concatenation-equal-to-target.js b/2023.number-of-pairs-of-strings-with-concatenation-equal-to-target.js similarity index 100% rename from 2023-number-of-pairs-of-strings-with-concatenation-equal-to-target.js rename to 2023.number-of-pairs-of-strings-with-concatenation-equal-to-target.js diff --git a/2024-maximize-the-confusion-of-an-exam.js b/2024.maximize-the-confusion-of-an-exam.js similarity index 100% rename from 2024-maximize-the-confusion-of-an-exam.js rename to 2024.maximize-the-confusion-of-an-exam.js diff --git a/2025-maximum-number-of-ways-to-partition-an-array.js b/2025.maximum-number-of-ways-to-partition-an-array.js similarity index 100% rename from 2025-maximum-number-of-ways-to-partition-an-array.js rename to 2025.maximum-number-of-ways-to-partition-an-array.js diff --git a/2029-stone-game-ix.js b/2029.stone-game-ix.js similarity index 100% rename from 2029-stone-game-ix.js rename to 2029.stone-game-ix.js diff --git a/203-remove-linked-list-elements.js b/203.remove-linked-list-elements.js similarity index 100% rename from 203-remove-linked-list-elements.js rename to 203.remove-linked-list-elements.js diff --git a/2030-smallest-k-length-subsequence-with-occurrences-of-a-letter.js b/2030.smallest-k-length-subsequence-with-occurrences-of-a-letter.js similarity index 100% rename from 2030-smallest-k-length-subsequence-with-occurrences-of-a-letter.js rename to 2030.smallest-k-length-subsequence-with-occurrences-of-a-letter.js diff --git a/2031-count-subarrays-with-more-ones-than-zeros.js b/2031.count-subarrays-with-more-ones-than-zeros.js similarity index 100% rename from 2031-count-subarrays-with-more-ones-than-zeros.js rename to 2031.count-subarrays-with-more-ones-than-zeros.js diff --git a/2032-two-out-of-three.js b/2032.two-out-of-three.js similarity index 100% rename from 2032-two-out-of-three.js rename to 2032.two-out-of-three.js diff --git a/2033-minimum-operations-to-make-a-uni-value-grid.js b/2033.minimum-operations-to-make-a-uni-value-grid.js similarity index 100% rename from 2033-minimum-operations-to-make-a-uni-value-grid.js rename to 2033.minimum-operations-to-make-a-uni-value-grid.js diff --git a/2034-stock-price-fluctuation.js b/2034.stock-price-fluctuation.js similarity index 100% rename from 2034-stock-price-fluctuation.js rename to 2034.stock-price-fluctuation.js diff --git a/2035-partition-array-into-two-arrays-to-minimize-sum-difference.js b/2035.partition-array-into-two-arrays-to-minimize-sum-difference.js similarity index 100% rename from 2035-partition-array-into-two-arrays-to-minimize-sum-difference.js rename to 2035.partition-array-into-two-arrays-to-minimize-sum-difference.js diff --git a/2037-minimum-number-of-moves-to-seat-everyone.js b/2037.minimum-number-of-moves-to-seat-everyone.js similarity index 100% rename from 2037-minimum-number-of-moves-to-seat-everyone.js rename to 2037.minimum-number-of-moves-to-seat-everyone.js diff --git a/2038-remove-colored-pieces-if-both-neighbors-are-the-same-color.js b/2038.remove-colored-pieces-if-both-neighbors-are-the-same-color.js similarity index 100% rename from 2038-remove-colored-pieces-if-both-neighbors-are-the-same-color.js rename to 2038.remove-colored-pieces-if-both-neighbors-are-the-same-color.js diff --git a/204-count-primes.js b/204.count-primes.js old mode 100755 new mode 100644 similarity index 93% rename from 204-count-primes.js rename to 204.count-primes.js index fddff31b..d8ff00b6 --- a/204-count-primes.js +++ b/204.count-primes.js @@ -1,23 +1,23 @@ -/** - * @param {number} n - * @return {number} - */ -const countPrimes = function(n) { - const arr = Array(n).fill(0) - - for(let i = 2; i * i < n; i++) { - if(arr[i] !== 0) continue - let j = i * i - while(j < n) { - arr[j] = 1 - j += i - } - } - - let res = 0 - for(let i = 2; i < n; i++) { - if(arr[i] === 0) res++ - } - return res -}; - +/** + * @param {number} n + * @return {number} + */ +const countPrimes = function(n) { + const arr = Array(n).fill(0) + + for(let i = 2; i * i < n; i++) { + if(arr[i] !== 0) continue + let j = i * i + while(j < n) { + arr[j] = 1 + j += i + } + } + + let res = 0 + for(let i = 2; i < n; i++) { + if(arr[i] === 0) res++ + } + return res +}; + diff --git a/2040-kth-smallest-product-of-two-sorted-arrays.js b/2040.kth-smallest-product-of-two-sorted-arrays.js similarity index 100% rename from 2040-kth-smallest-product-of-two-sorted-arrays.js rename to 2040.kth-smallest-product-of-two-sorted-arrays.js diff --git a/2042-check-if-numbers-are-ascending-in-a-sentence.js b/2042.check-if-numbers-are-ascending-in-a-sentence.js similarity index 100% rename from 2042-check-if-numbers-are-ascending-in-a-sentence.js rename to 2042.check-if-numbers-are-ascending-in-a-sentence.js diff --git a/2043-simple-bank-system.js b/2043.simple-bank-system.js similarity index 100% rename from 2043-simple-bank-system.js rename to 2043.simple-bank-system.js diff --git a/2044-count-number-of-maximum-bitwise-or-subsets.js b/2044.count-number-of-maximum-bitwise-or-subsets.js similarity index 100% rename from 2044-count-number-of-maximum-bitwise-or-subsets.js rename to 2044.count-number-of-maximum-bitwise-or-subsets.js diff --git a/2045-second-minimum-time-to-reach-destination.js b/2045.second-minimum-time-to-reach-destination.js similarity index 100% rename from 2045-second-minimum-time-to-reach-destination.js rename to 2045.second-minimum-time-to-reach-destination.js diff --git a/2046-sort-linked-list-already-sorted-using-absolute-values.js b/2046.sort-linked-list-already-sorted-using-absolute-values.js similarity index 100% rename from 2046-sort-linked-list-already-sorted-using-absolute-values.js rename to 2046.sort-linked-list-already-sorted-using-absolute-values.js diff --git a/2047-number-of-valid-words-in-a-sentence.js b/2047.number-of-valid-words-in-a-sentence.js similarity index 100% rename from 2047-number-of-valid-words-in-a-sentence.js rename to 2047.number-of-valid-words-in-a-sentence.js diff --git a/2048-next-greater-numerically-balanced-number.js b/2048.next-greater-numerically-balanced-number.js similarity index 100% rename from 2048-next-greater-numerically-balanced-number.js rename to 2048.next-greater-numerically-balanced-number.js diff --git a/2049-count-nodes-with-the-highest-score.js b/2049.count-nodes-with-the-highest-score.js similarity index 100% rename from 2049-count-nodes-with-the-highest-score.js rename to 2049.count-nodes-with-the-highest-score.js diff --git a/205-isomorphic-strings.js b/205.isomorphic-strings.js similarity index 100% rename from 205-isomorphic-strings.js rename to 205.isomorphic-strings.js diff --git a/2050-parallel-courses-iii.js b/2050.parallel-courses-iii.js similarity index 100% rename from 2050-parallel-courses-iii.js rename to 2050.parallel-courses-iii.js diff --git a/2053-kth-distinct-string-in-an-array.js b/2053.kth-distinct-string-in-an-array.js similarity index 100% rename from 2053-kth-distinct-string-in-an-array.js rename to 2053.kth-distinct-string-in-an-array.js diff --git a/2054-two-best-non-overlapping-events.js b/2054.two-best-non-overlapping-events.js similarity index 100% rename from 2054-two-best-non-overlapping-events.js rename to 2054.two-best-non-overlapping-events.js diff --git a/2055-plates-between-candles.js b/2055.plates-between-candles.js similarity index 100% rename from 2055-plates-between-candles.js rename to 2055.plates-between-candles.js diff --git a/2057-smallest-index-with-equal-value.js b/2057.smallest-index-with-equal-value.js similarity index 100% rename from 2057-smallest-index-with-equal-value.js rename to 2057.smallest-index-with-equal-value.js diff --git a/2058-find-the-minimum-and-maximum-number-of-nodes-between-critical-points.js b/2058.find-the-minimum-and-maximum-number-of-nodes-between-critical-points.js similarity index 100% rename from 2058-find-the-minimum-and-maximum-number-of-nodes-between-critical-points.js rename to 2058.find-the-minimum-and-maximum-number-of-nodes-between-critical-points.js diff --git a/2059-minimum-operations-to-convert-number.js b/2059.minimum-operations-to-convert-number.js similarity index 100% rename from 2059-minimum-operations-to-convert-number.js rename to 2059.minimum-operations-to-convert-number.js diff --git a/206-reverse-linked-list.js b/206.reverse-linked-list.js old mode 100755 new mode 100644 similarity index 95% rename from 206-reverse-linked-list.js rename to 206.reverse-linked-list.js index 5c3ef392..c713b6ed --- a/206-reverse-linked-list.js +++ b/206.reverse-linked-list.js @@ -1,25 +1,25 @@ -/** - * Definition for singly-linked list. - * function ListNode(val, next) { - * this.val = (val===undefined ? 0 : val) - * this.next = (next===undefined ? null : next) - * } - */ -/** - * @param {ListNode} head - * @return {ListNode} - */ -const reverseList = function(head) { - if(head == null) return head - const pre = new ListNode(null, head) - let cur = head - while(cur.next) { - const tmp = pre.next - pre.next = cur.next - cur.next = cur.next.next - pre.next.next = tmp - } - - return pre.next -}; - +/** + * Definition for singly-linked list. + * function ListNode(val, next) { + * this.val = (val===undefined ? 0 : val) + * this.next = (next===undefined ? null : next) + * } + */ +/** + * @param {ListNode} head + * @return {ListNode} + */ +const reverseList = function(head) { + if(head == null) return head + const pre = new ListNode(null, head) + let cur = head + while(cur.next) { + const tmp = pre.next + pre.next = cur.next + cur.next = cur.next.next + pre.next.next = tmp + } + + return pre.next +}; + diff --git a/2060-check-if-an-original-string-exists-given-two-encoded-strings.js b/2060.check-if-an-original-string-exists-given-two-encoded-strings.js similarity index 100% rename from 2060-check-if-an-original-string-exists-given-two-encoded-strings.js rename to 2060.check-if-an-original-string-exists-given-two-encoded-strings.js diff --git a/2062-count-vowel-substrings-of-a-string.js b/2062.count-vowel-substrings-of-a-string.js similarity index 100% rename from 2062-count-vowel-substrings-of-a-string.js rename to 2062.count-vowel-substrings-of-a-string.js diff --git a/2063-vowels-of-all-substrings.js b/2063.vowels-of-all-substrings.js similarity index 100% rename from 2063-vowels-of-all-substrings.js rename to 2063.vowels-of-all-substrings.js diff --git a/2064-minimized-maximum-of-products-distributed-to-any-store.js b/2064.minimized-maximum-of-products-distributed-to-any-store.js similarity index 100% rename from 2064-minimized-maximum-of-products-distributed-to-any-store.js rename to 2064.minimized-maximum-of-products-distributed-to-any-store.js diff --git a/2065-maximum-path-quality-of-a-graph.js b/2065.maximum-path-quality-of-a-graph.js similarity index 100% rename from 2065-maximum-path-quality-of-a-graph.js rename to 2065.maximum-path-quality-of-a-graph.js diff --git a/2068-check-whether-two-strings-are-almost-equivalent.js b/2068.check-whether-two-strings-are-almost-equivalent.js similarity index 100% rename from 2068-check-whether-two-strings-are-almost-equivalent.js rename to 2068.check-whether-two-strings-are-almost-equivalent.js diff --git a/2069-walking-robot-simulation-ii.js b/2069.walking-robot-simulation-ii.js similarity index 100% rename from 2069-walking-robot-simulation-ii.js rename to 2069.walking-robot-simulation-ii.js diff --git a/207-course-schedule.js b/207.course-schedule.js similarity index 100% rename from 207-course-schedule.js rename to 207.course-schedule.js diff --git a/2071-maximum-number-of-tasks-you-can-assign.js b/2071.maximum-number-of-tasks-you-can-assign.js similarity index 100% rename from 2071-maximum-number-of-tasks-you-can-assign.js rename to 2071.maximum-number-of-tasks-you-can-assign.js diff --git a/2073-time-needed-to-buy-tickets.js b/2073.time-needed-to-buy-tickets.js similarity index 100% rename from 2073-time-needed-to-buy-tickets.js rename to 2073.time-needed-to-buy-tickets.js diff --git a/2074-reverse-nodes-in-even-length-groups.js b/2074.reverse-nodes-in-even-length-groups.js similarity index 100% rename from 2074-reverse-nodes-in-even-length-groups.js rename to 2074.reverse-nodes-in-even-length-groups.js diff --git a/2075-decode-the-slanted-ciphertext.js b/2075.decode-the-slanted-ciphertext.js similarity index 100% rename from 2075-decode-the-slanted-ciphertext.js rename to 2075.decode-the-slanted-ciphertext.js diff --git a/2076-process-restricted-friend-requests.js b/2076.process-restricted-friend-requests.js similarity index 100% rename from 2076-process-restricted-friend-requests.js rename to 2076.process-restricted-friend-requests.js diff --git a/2078-two-furthest-houses-with-different-colors.js b/2078.two-furthest-houses-with-different-colors.js similarity index 100% rename from 2078-two-furthest-houses-with-different-colors.js rename to 2078.two-furthest-houses-with-different-colors.js diff --git a/2079-watering-plants.js b/2079.watering-plants.js similarity index 100% rename from 2079-watering-plants.js rename to 2079.watering-plants.js diff --git a/208-implement-trie-prefix-tree.js b/208.implement-trie-prefix-tree.js similarity index 100% rename from 208-implement-trie-prefix-tree.js rename to 208.implement-trie-prefix-tree.js diff --git a/2080-range-frequency-queries.js b/2080.range-frequency-queries.js similarity index 100% rename from 2080-range-frequency-queries.js rename to 2080.range-frequency-queries.js diff --git a/2081-sum-of-k-mirror-numbers.js b/2081.sum-of-k-mirror-numbers.js similarity index 100% rename from 2081-sum-of-k-mirror-numbers.js rename to 2081.sum-of-k-mirror-numbers.js diff --git a/2083-substrings-that-begin-and-end-with-the-same-letter.js b/2083.substrings-that-begin-and-end-with-the-same-letter.js similarity index 100% rename from 2083-substrings-that-begin-and-end-with-the-same-letter.js rename to 2083.substrings-that-begin-and-end-with-the-same-letter.js diff --git a/2086-minimum-number-of-buckets-required-to-collect-rainwater-from-houses.js b/2086.minimum-number-of-buckets-required-to-collect-rainwater-from-houses.js similarity index 100% rename from 2086-minimum-number-of-buckets-required-to-collect-rainwater-from-houses.js rename to 2086.minimum-number-of-buckets-required-to-collect-rainwater-from-houses.js diff --git a/2088-count-fertile-pyramids-in-a-land.js b/2088.count-fertile-pyramids-in-a-land.js similarity index 100% rename from 2088-count-fertile-pyramids-in-a-land.js rename to 2088.count-fertile-pyramids-in-a-land.js diff --git a/2089-find-target-indices-after-sorting-array.js b/2089.find-target-indices-after-sorting-array.js similarity index 100% rename from 2089-find-target-indices-after-sorting-array.js rename to 2089.find-target-indices-after-sorting-array.js diff --git a/209-minimum-size-subarray-sum.js b/209.minimum-size-subarray-sum.js similarity index 100% rename from 209-minimum-size-subarray-sum.js rename to 209.minimum-size-subarray-sum.js diff --git a/2090-k-radius-subarray-averages.js b/2090.k-radius-subarray-averages.js similarity index 100% rename from 2090-k-radius-subarray-averages.js rename to 2090.k-radius-subarray-averages.js diff --git a/2091-removing-minimum-and-maximum-from-array.js b/2091.removing-minimum-and-maximum-from-array.js similarity index 100% rename from 2091-removing-minimum-and-maximum-from-array.js rename to 2091.removing-minimum-and-maximum-from-array.js diff --git a/2092-find-all-people-with-secret.js b/2092.find-all-people-with-secret.js similarity index 100% rename from 2092-find-all-people-with-secret.js rename to 2092.find-all-people-with-secret.js diff --git a/2094-finding-3-digit-even-numbers.js b/2094.finding-3-digit-even-numbers.js similarity index 100% rename from 2094-finding-3-digit-even-numbers.js rename to 2094.finding-3-digit-even-numbers.js diff --git a/2095-delete-the-middle-node-of-a-linked-list.js b/2095.delete-the-middle-node-of-a-linked-list.js similarity index 100% rename from 2095-delete-the-middle-node-of-a-linked-list.js rename to 2095.delete-the-middle-node-of-a-linked-list.js diff --git a/2096-step-by-step-directions-from-a-binary-tree-node-to-another.js b/2096.step-by-step-directions-from-a-binary-tree-node-to-another.js similarity index 100% rename from 2096-step-by-step-directions-from-a-binary-tree-node-to-another.js rename to 2096.step-by-step-directions-from-a-binary-tree-node-to-another.js diff --git a/2097-valid-arrangement-of-pairs.js b/2097.valid-arrangement-of-pairs.js similarity index 100% rename from 2097-valid-arrangement-of-pairs.js rename to 2097.valid-arrangement-of-pairs.js diff --git a/2098-subsequence-of-size-k-with-the-largest-even-sum.js b/2098.subsequence-of-size-k-with-the-largest-even-sum.js similarity index 100% rename from 2098-subsequence-of-size-k-with-the-largest-even-sum.js rename to 2098.subsequence-of-size-k-with-the-largest-even-sum.js diff --git a/21-merge-two-sorted-lists.js b/21.merge-two-sorted-lists.js old mode 100755 new mode 100644 similarity index 94% rename from 21-merge-two-sorted-lists.js rename to 21.merge-two-sorted-lists.js index d4e2dd87..c75fabf1 --- a/21-merge-two-sorted-lists.js +++ b/21.merge-two-sorted-lists.js @@ -1,25 +1,25 @@ -/** - * Definition for singly-linked list. - * function ListNode(val) { - * this.val = val; - * this.next = null; - * } - */ -/** - * @param {ListNode} l1 - * @param {ListNode} l2 - * @return {ListNode} - */ - -const mergeTwoLists = function(l1, l2) { - if (l1 === null) return l2; - if (l2 === null) return l1; - if (l1.val < l2.val) { - l1.next = mergeTwoLists(l1.next, l2); - return l1; - } else { - l2.next = mergeTwoLists(l1, l2.next); - return l2; - } -}; - +/** + * Definition for singly-linked list. + * function ListNode(val) { + * this.val = val; + * this.next = null; + * } + */ +/** + * @param {ListNode} l1 + * @param {ListNode} l2 + * @return {ListNode} + */ + +const mergeTwoLists = function(l1, l2) { + if (l1 === null) return l2; + if (l2 === null) return l1; + if (l1.val < l2.val) { + l1.next = mergeTwoLists(l1.next, l2); + return l1; + } else { + l2.next = mergeTwoLists(l1, l2.next); + return l2; + } +}; + diff --git a/210-course-schedule-ii.js b/210.course-schedule-ii.js similarity index 100% rename from 210-course-schedule-ii.js rename to 210.course-schedule-ii.js diff --git a/2100-find-good-days-to-rob-the-bank.js b/2100.find-good-days-to-rob-the-bank.js similarity index 100% rename from 2100-find-good-days-to-rob-the-bank.js rename to 2100.find-good-days-to-rob-the-bank.js diff --git a/2101-detonate-the-maximum-bombs.js b/2101.detonate-the-maximum-bombs.js similarity index 100% rename from 2101-detonate-the-maximum-bombs.js rename to 2101.detonate-the-maximum-bombs.js diff --git a/2102-sequentially-ordinal-rank-tracker.js b/2102.sequentially-ordinal-rank-tracker.js similarity index 100% rename from 2102-sequentially-ordinal-rank-tracker.js rename to 2102.sequentially-ordinal-rank-tracker.js diff --git a/2103-rings-and-rods.js b/2103.rings-and-rods.js similarity index 100% rename from 2103-rings-and-rods.js rename to 2103.rings-and-rods.js diff --git a/2104-sum-of-subarray-ranges.js b/2104.sum-of-subarray-ranges.js similarity index 100% rename from 2104-sum-of-subarray-ranges.js rename to 2104.sum-of-subarray-ranges.js diff --git a/2105-watering-plants-ii.js b/2105.watering-plants-ii.js similarity index 100% rename from 2105-watering-plants-ii.js rename to 2105.watering-plants-ii.js diff --git a/2106-maximum-fruits-harvested-after-at-most-k-steps.js b/2106.maximum-fruits-harvested-after-at-most-k-steps.js similarity index 100% rename from 2106-maximum-fruits-harvested-after-at-most-k-steps.js rename to 2106.maximum-fruits-harvested-after-at-most-k-steps.js diff --git a/211-add-and-search-word-data-structure-design.js b/211.add-and-search-word-data-structure-design.js similarity index 100% rename from 211-add-and-search-word-data-structure-design.js rename to 211.add-and-search-word-data-structure-design.js diff --git a/2111-minimum-operations-to-make-the-array-k-increasing.js b/2111.minimum-operations-to-make-the-array-k-increasing.js similarity index 100% rename from 2111-minimum-operations-to-make-the-array-k-increasing.js rename to 2111.minimum-operations-to-make-the-array-k-increasing.js diff --git a/2115-find-all-possible-recipes-from-given-supplies.js b/2115.find-all-possible-recipes-from-given-supplies.js similarity index 100% rename from 2115-find-all-possible-recipes-from-given-supplies.js rename to 2115.find-all-possible-recipes-from-given-supplies.js diff --git a/2116-check-if-a-parentheses-string-can-be-validcheck-if-a-parentheses-string-can-be-valid.js b/2116.check-if-a-parentheses-string-can-be-validcheck-if-a-parentheses-string-can-be-valid.js similarity index 100% rename from 2116-check-if-a-parentheses-string-can-be-validcheck-if-a-parentheses-string-can-be-valid.js rename to 2116.check-if-a-parentheses-string-can-be-validcheck-if-a-parentheses-string-can-be-valid.js diff --git a/2119-a-number-after-a-double-reversal.js b/2119.a-number-after-a-double-reversal.js similarity index 100% rename from 2119-a-number-after-a-double-reversal.js rename to 2119.a-number-after-a-double-reversal.js diff --git a/212-word-search-ii.js b/212.word-search-ii.js similarity index 100% rename from 212-word-search-ii.js rename to 212.word-search-ii.js diff --git a/2121-intervals-between-identical-elements.js b/2121.intervals-between-identical-elements.js similarity index 100% rename from 2121-intervals-between-identical-elements.js rename to 2121.intervals-between-identical-elements.js diff --git a/2122-recover-the-original-array.js b/2122.recover-the-original-array.js similarity index 100% rename from 2122-recover-the-original-array.js rename to 2122.recover-the-original-array.js diff --git a/2124-check-if-all-as-appears-before-all-bs.js b/2124.check-if-all-as-appears-before-all-bs.js similarity index 100% rename from 2124-check-if-all-as-appears-before-all-bs.js rename to 2124.check-if-all-as-appears-before-all-bs.js diff --git a/2125-number-of-laser-beams-in-a-bank.js b/2125.number-of-laser-beams-in-a-bank.js similarity index 100% rename from 2125-number-of-laser-beams-in-a-bank.js rename to 2125.number-of-laser-beams-in-a-bank.js diff --git a/2126-destroying-asteroids.js b/2126.destroying-asteroids.js similarity index 100% rename from 2126-destroying-asteroids.js rename to 2126.destroying-asteroids.js diff --git a/2127-maximum-employees-to-be-invited-to-a-meeting.js b/2127.maximum-employees-to-be-invited-to-a-meeting.js similarity index 100% rename from 2127-maximum-employees-to-be-invited-to-a-meeting.js rename to 2127.maximum-employees-to-be-invited-to-a-meeting.js diff --git a/2128-remove-all-ones-with-row-and-column-flips.js b/2128.remove-all-ones-with-row-and-column-flips.js similarity index 100% rename from 2128-remove-all-ones-with-row-and-column-flips.js rename to 2128.remove-all-ones-with-row-and-column-flips.js diff --git a/213-house-robber-ii.js b/213.house-robber-ii.js similarity index 100% rename from 213-house-robber-ii.js rename to 213.house-robber-ii.js diff --git a/2130-maximum-twin-sum-of-a-linked-list.js b/2130.maximum-twin-sum-of-a-linked-list.js similarity index 100% rename from 2130-maximum-twin-sum-of-a-linked-list.js rename to 2130.maximum-twin-sum-of-a-linked-list.js diff --git a/2132-stamping-the-grid.js b/2132.stamping-the-grid.js similarity index 100% rename from 2132-stamping-the-grid.js rename to 2132.stamping-the-grid.js diff --git a/2136-earliest-possible-day-of-full-bloom.js b/2136.earliest-possible-day-of-full-bloom.js similarity index 100% rename from 2136-earliest-possible-day-of-full-bloom.js rename to 2136.earliest-possible-day-of-full-bloom.js diff --git a/2138-divide-a-string-into-groups-of-size-k.js b/2138.divide-a-string-into-groups-of-size-k.js similarity index 100% rename from 2138-divide-a-string-into-groups-of-size-k.js rename to 2138.divide-a-string-into-groups-of-size-k.js diff --git a/2139-minimum-moves-to-reach-target-score.js b/2139.minimum-moves-to-reach-target-score.js similarity index 100% rename from 2139-minimum-moves-to-reach-target-score.js rename to 2139.minimum-moves-to-reach-target-score.js diff --git a/214-shortest-palindrome.js b/214.shortest-palindrome.js similarity index 100% rename from 214-shortest-palindrome.js rename to 214.shortest-palindrome.js diff --git a/2140-solving-questions-with-brainpower.js b/2140.solving-questions-with-brainpower.js similarity index 100% rename from 2140-solving-questions-with-brainpower.js rename to 2140.solving-questions-with-brainpower.js diff --git a/2141-maximum-running-time-of-n-computers.js b/2141.maximum-running-time-of-n-computers.js similarity index 100% rename from 2141-maximum-running-time-of-n-computers.js rename to 2141.maximum-running-time-of-n-computers.js diff --git a/2148-count-elements-with-strictly-smaller-and-greater-elements.js b/2148.count-elements-with-strictly-smaller-and-greater-elements.js similarity index 100% rename from 2148-count-elements-with-strictly-smaller-and-greater-elements.js rename to 2148.count-elements-with-strictly-smaller-and-greater-elements.js diff --git a/2149-rearrange-array-elements-by-sign.js b/2149.rearrange-array-elements-by-sign.js similarity index 100% rename from 2149-rearrange-array-elements-by-sign.js rename to 2149.rearrange-array-elements-by-sign.js diff --git a/215-kth-largest-element-in-an-array.js b/215.kth-largest-element-in-an-array.js old mode 100755 new mode 100644 similarity index 96% rename from 215-kth-largest-element-in-an-array.js rename to 215.kth-largest-element-in-an-array.js index f9fab197..3e14e63f --- a/215-kth-largest-element-in-an-array.js +++ b/215.kth-largest-element-in-an-array.js @@ -1,26 +1,26 @@ -/** - * @param {number[]} nums - * @param {number} k - * @return {number} - */ -const findKthLargest = function(nums, k) { - if (!nums || k > nums.length) return 0; - - const larger = []; - const smaller = []; - const pivot = nums[parseInt(nums.length / 2)]; - let pivotCount = 0; - - for (let i = 0; i < nums.length; i++) { - const ele = nums[i]; - - if (ele > pivot) larger.push(ele); - else if (ele === pivot) pivotCount++; - else smaller.push(ele); - } - - if (larger.length >= k) return findKthLargest(larger, k); - else if (k - larger.length - pivotCount <= 0) return pivot; - else return findKthLargest(smaller, k - larger.length - pivotCount); -}; - +/** + * @param {number[]} nums + * @param {number} k + * @return {number} + */ +const findKthLargest = function(nums, k) { + if (!nums || k > nums.length) return 0; + + const larger = []; + const smaller = []; + const pivot = nums[parseInt(nums.length / 2)]; + let pivotCount = 0; + + for (let i = 0; i < nums.length; i++) { + const ele = nums[i]; + + if (ele > pivot) larger.push(ele); + else if (ele === pivot) pivotCount++; + else smaller.push(ele); + } + + if (larger.length >= k) return findKthLargest(larger, k); + else if (k - larger.length - pivotCount <= 0) return pivot; + else return findKthLargest(smaller, k - larger.length - pivotCount); +}; + diff --git a/2150-find-all-lonely-numbers-in-the-array.js b/2150.find-all-lonely-numbers-in-the-array.js similarity index 100% rename from 2150-find-all-lonely-numbers-in-the-array.js rename to 2150.find-all-lonely-numbers-in-the-array.js diff --git a/2151-maximum-good-people-based-on-statements.js b/2151.maximum-good-people-based-on-statements.js similarity index 100% rename from 2151-maximum-good-people-based-on-statements.js rename to 2151.maximum-good-people-based-on-statements.js diff --git a/2152-minimum-number-of-lines-to-cover-points.js b/2152.minimum-number-of-lines-to-cover-points.js similarity index 100% rename from 2152-minimum-number-of-lines-to-cover-points.js rename to 2152.minimum-number-of-lines-to-cover-points.js diff --git a/2154-keep-multiplying-found-values-by-two.js b/2154.keep-multiplying-found-values-by-two.js similarity index 100% rename from 2154-keep-multiplying-found-values-by-two.js rename to 2154.keep-multiplying-found-values-by-two.js diff --git a/2155-all-divisions-with-the-highest-score-of-a-binary-array.js b/2155.all-divisions-with-the-highest-score-of-a-binary-array.js similarity index 100% rename from 2155-all-divisions-with-the-highest-score-of-a-binary-array.js rename to 2155.all-divisions-with-the-highest-score-of-a-binary-array.js diff --git a/2156-find-substring-with-given-hash-value.js b/2156.find-substring-with-given-hash-value.js similarity index 100% rename from 2156-find-substring-with-given-hash-value.js rename to 2156.find-substring-with-given-hash-value.js diff --git a/216-combination-sum-III.js b/216.combination-sum-III.js old mode 100755 new mode 100644 similarity index 95% rename from 216-combination-sum-III.js rename to 216.combination-sum-III.js index c2caf4f5..2fddcc1e --- a/216-combination-sum-III.js +++ b/216.combination-sum-III.js @@ -1,25 +1,25 @@ -/** - * @param {number} k - * @param {number} n - * @return {number[][]} - */ -const combinationSum3 = function(k, n) { - const ans = []; - combination(ans, [], k, 1, n); - return ans; -}; - -function combination(ans, comb, k, start, n) { - if (comb.length > k) { - return; - } - if (comb.length === k && n === 0) { - ans.push(comb.slice(0)); - return; - } - for (let i = start; i <= n && i <= 9; i++) { - comb.push(i); - combination(ans, comb, k, i + 1, n - i); - comb.pop(); - } -} +/** + * @param {number} k + * @param {number} n + * @return {number[][]} + */ +const combinationSum3 = function(k, n) { + const ans = []; + combination(ans, [], k, 1, n); + return ans; +}; + +function combination(ans, comb, k, start, n) { + if (comb.length > k) { + return; + } + if (comb.length === k && n === 0) { + ans.push(comb.slice(0)); + return; + } + for (let i = start; i <= n && i <= 9; i++) { + comb.push(i); + combination(ans, comb, k, i + 1, n - i); + comb.pop(); + } +} diff --git a/2161-partition-array-according-to-given-pivot.js b/2161.partition-array-according-to-given-pivot.js similarity index 100% rename from 2161-partition-array-according-to-given-pivot.js rename to 2161.partition-array-according-to-given-pivot.js diff --git a/2163-minimum-difference-in-sums-after-removal-of-elements.js b/2163.minimum-difference-in-sums-after-removal-of-elements.js similarity index 100% rename from 2163-minimum-difference-in-sums-after-removal-of-elements.js rename to 2163.minimum-difference-in-sums-after-removal-of-elements.js diff --git a/2164-sort-even-and-odd-indices-independently.js b/2164.sort-even-and-odd-indices-independently.js similarity index 100% rename from 2164-sort-even-and-odd-indices-independently.js rename to 2164.sort-even-and-odd-indices-independently.js diff --git a/2165-smallest-value-of-the-rearranged-number.js b/2165.smallest-value-of-the-rearranged-number.js similarity index 100% rename from 2165-smallest-value-of-the-rearranged-number.js rename to 2165.smallest-value-of-the-rearranged-number.js diff --git a/2166-design-bitset.js b/2166.design-bitset.js similarity index 100% rename from 2166-design-bitset.js rename to 2166.design-bitset.js diff --git a/2167-minimum-time-to-remove-all-cars-containing-illegal-goods.js b/2167.minimum-time-to-remove-all-cars-containing-illegal-goods.js similarity index 100% rename from 2167-minimum-time-to-remove-all-cars-containing-illegal-goods.js rename to 2167.minimum-time-to-remove-all-cars-containing-illegal-goods.js diff --git a/2169-count-operations-to-obtain-zero.js b/2169.count-operations-to-obtain-zero.js similarity index 100% rename from 2169-count-operations-to-obtain-zero.js rename to 2169.count-operations-to-obtain-zero.js diff --git a/217-contains-duplicate.js b/217.contains-duplicate.js old mode 100755 new mode 100644 similarity index 94% rename from 217-contains-duplicate.js rename to 217.contains-duplicate.js index 58c739b2..16096df7 --- a/217-contains-duplicate.js +++ b/217.contains-duplicate.js @@ -1,15 +1,15 @@ -/** - * @param {number[]} nums - * @return {boolean} - */ -const containsDuplicate = function(nums) { - const hash = {}; - for (let el of nums) { - if (hash.hasOwnProperty(el)) { - return true; - } else { - hash[el] = 1; - } - } - return false; -}; +/** + * @param {number[]} nums + * @return {boolean} + */ +const containsDuplicate = function(nums) { + const hash = {}; + for (let el of nums) { + if (hash.hasOwnProperty(el)) { + return true; + } else { + hash[el] = 1; + } + } + return false; +}; diff --git a/2172-maximum-and-sum-of-array.js b/2172.maximum-and-sum-of-array.js similarity index 100% rename from 2172-maximum-and-sum-of-array.js rename to 2172.maximum-and-sum-of-array.js diff --git a/2178-maximum-split-of-positive-even-integers.js b/2178.maximum-split-of-positive-even-integers.js similarity index 100% rename from 2178-maximum-split-of-positive-even-integers.js rename to 2178.maximum-split-of-positive-even-integers.js diff --git a/2179-count-good-triplets-in-an-array.js b/2179.count-good-triplets-in-an-array.js similarity index 100% rename from 2179-count-good-triplets-in-an-array.js rename to 2179.count-good-triplets-in-an-array.js diff --git a/218-the-skyline-problem.js b/218.the-skyline-problem.js similarity index 100% rename from 218-the-skyline-problem.js rename to 218.the-skyline-problem.js diff --git a/2180-count-integers-with-even-digit-sum.js b/2180.count-integers-with-even-digit-sum.js similarity index 100% rename from 2180-count-integers-with-even-digit-sum.js rename to 2180.count-integers-with-even-digit-sum.js diff --git a/2181-merge-nodes-in-between-zeros.js b/2181.merge-nodes-in-between-zeros.js similarity index 100% rename from 2181-merge-nodes-in-between-zeros.js rename to 2181.merge-nodes-in-between-zeros.js diff --git a/2182-construct-string-with-repeat-limit.js b/2182.construct-string-with-repeat-limit.js similarity index 100% rename from 2182-construct-string-with-repeat-limit.js rename to 2182.construct-string-with-repeat-limit.js diff --git a/2183-count-array-pairs-divisible-by-k.js b/2183.count-array-pairs-divisible-by-k.js similarity index 100% rename from 2183-count-array-pairs-divisible-by-k.js rename to 2183.count-array-pairs-divisible-by-k.js diff --git a/2184-number-of-ways-to-build-sturdy-brick-wall.js b/2184.number-of-ways-to-build-sturdy-brick-wall.js similarity index 100% rename from 2184-number-of-ways-to-build-sturdy-brick-wall.js rename to 2184.number-of-ways-to-build-sturdy-brick-wall.js diff --git a/2188-minimum-time-to-finish-the-race.js b/2188.minimum-time-to-finish-the-race.js similarity index 100% rename from 2188-minimum-time-to-finish-the-race.js rename to 2188.minimum-time-to-finish-the-race.js diff --git a/2189-number-of-ways-to-build-house-of-cards.js b/2189.number-of-ways-to-build-house-of-cards.js similarity index 100% rename from 2189-number-of-ways-to-build-house-of-cards.js rename to 2189.number-of-ways-to-build-house-of-cards.js diff --git a/219-contains-duplicate-ii.js b/219.contains-duplicate-ii.js similarity index 100% rename from 219-contains-duplicate-ii.js rename to 219.contains-duplicate-ii.js diff --git a/2192-all-ancestors-of-a-node-in-a-directed-acyclic-graph.js b/2192.all-ancestors-of-a-node-in-a-directed-acyclic-graph.js similarity index 100% rename from 2192-all-ancestors-of-a-node-in-a-directed-acyclic-graph.js rename to 2192.all-ancestors-of-a-node-in-a-directed-acyclic-graph.js diff --git a/2193-minimum-number-of-moves-to-make-palindrome.js b/2193.minimum-number-of-moves-to-make-palindrome.js similarity index 100% rename from 2193-minimum-number-of-moves-to-make-palindrome.js rename to 2193.minimum-number-of-moves-to-make-palindrome.js diff --git a/2197-replace-non-coprime-numbers-in-array.js b/2197.replace-non-coprime-numbers-in-array.js similarity index 100% rename from 2197-replace-non-coprime-numbers-in-array.js rename to 2197.replace-non-coprime-numbers-in-array.js diff --git a/22-generate-parentheses.js b/22.generate-parentheses.js old mode 100755 new mode 100644 similarity index 96% rename from 22-generate-parentheses.js rename to 22.generate-parentheses.js index e6318566..ef7a8f3e --- a/22-generate-parentheses.js +++ b/22.generate-parentheses.js @@ -1,17 +1,17 @@ -/** - * @param {number} n - * @return {string[]} - */ -const generateParenthesis = function(n) { - const res = []; - backtrack(res, "", 0, 0, n); - return res; -}; -function backtrack(arr, cur, open, close, max) { - if (cur.length === max * 2) { - arr.push(cur); - return; - } - if (open < max) backtrack(arr, cur + "(", open + 1, close, max); - if (close < open) backtrack(arr, cur + ")", open, close + 1, max); -} +/** + * @param {number} n + * @return {string[]} + */ +const generateParenthesis = function(n) { + const res = []; + backtrack(res, "", 0, 0, n); + return res; +}; +function backtrack(arr, cur, open, close, max) { + if (cur.length === max * 2) { + arr.push(cur); + return; + } + if (open < max) backtrack(arr, cur + "(", open + 1, close, max); + if (close < open) backtrack(arr, cur + ")", open, close + 1, max); +} diff --git a/220-contains-duplicate-iii.js b/220.contains-duplicate-iii.js similarity index 100% rename from 220-contains-duplicate-iii.js rename to 220.contains-duplicate-iii.js diff --git a/2204-distance-to-a-cycle-in-undirected-graph.js b/2204.distance-to-a-cycle-in-undirected-graph.js similarity index 100% rename from 2204-distance-to-a-cycle-in-undirected-graph.js rename to 2204.distance-to-a-cycle-in-undirected-graph.js diff --git a/2209-minimum-white-tiles-after-covering-with-carpets.js b/2209.minimum-white-tiles-after-covering-with-carpets.js similarity index 100% rename from 2209-minimum-white-tiles-after-covering-with-carpets.js rename to 2209.minimum-white-tiles-after-covering-with-carpets.js diff --git a/221-maximal-square.js b/221.maximal-square.js similarity index 100% rename from 221-maximal-square.js rename to 221.maximal-square.js diff --git a/2210-count-hills-and-valleys-in-an-array.js b/2210.count-hills-and-valleys-in-an-array.js similarity index 100% rename from 2210-count-hills-and-valleys-in-an-array.js rename to 2210.count-hills-and-valleys-in-an-array.js diff --git a/2211-count-collisions-on-a-road.js b/2211.count-collisions-on-a-road.js similarity index 100% rename from 2211-count-collisions-on-a-road.js rename to 2211.count-collisions-on-a-road.js diff --git a/2212-maximum-points-in-an-archery-competition.js b/2212.maximum-points-in-an-archery-competition.js similarity index 100% rename from 2212-maximum-points-in-an-archery-competition.js rename to 2212.maximum-points-in-an-archery-competition.js diff --git a/2213-longest-substring-of-one-repeating-character.js b/2213.longest-substring-of-one-repeating-character.js similarity index 100% rename from 2213-longest-substring-of-one-repeating-character.js rename to 2213.longest-substring-of-one-repeating-character.js diff --git a/2214-minimum-health-to-beat-game.js b/2214.minimum-health-to-beat-game.js similarity index 100% rename from 2214-minimum-health-to-beat-game.js rename to 2214.minimum-health-to-beat-game.js diff --git a/2215-find-the-difference-of-two-arrays.js b/2215.find-the-difference-of-two-arrays.js similarity index 100% rename from 2215-find-the-difference-of-two-arrays.js rename to 2215.find-the-difference-of-two-arrays.js diff --git a/2216-minimum-deletions-to-make-array-beautiful.js b/2216.minimum-deletions-to-make-array-beautiful.js similarity index 100% rename from 2216-minimum-deletions-to-make-array-beautiful.js rename to 2216.minimum-deletions-to-make-array-beautiful.js diff --git a/2217-find-palindrome-with-fixed-length.js b/2217.find-palindrome-with-fixed-length.js similarity index 100% rename from 2217-find-palindrome-with-fixed-length.js rename to 2217.find-palindrome-with-fixed-length.js diff --git a/2218-maximum-value-of-k-coins-from-piles.js b/2218.maximum-value-of-k-coins-from-piles.js similarity index 100% rename from 2218-maximum-value-of-k-coins-from-piles.js rename to 2218.maximum-value-of-k-coins-from-piles.js diff --git a/222-count-complete-tree-nodes.js b/222.count-complete-tree-nodes.js similarity index 100% rename from 222-count-complete-tree-nodes.js rename to 222.count-complete-tree-nodes.js diff --git a/2221-find-triangular-sum-of-an-array.js b/2221.find-triangular-sum-of-an-array.js similarity index 100% rename from 2221-find-triangular-sum-of-an-array.js rename to 2221.find-triangular-sum-of-an-array.js diff --git a/2222-number-of-ways-to-select-buildings.js b/2222.number-of-ways-to-select-buildings.js similarity index 100% rename from 2222-number-of-ways-to-select-buildings.js rename to 2222.number-of-ways-to-select-buildings.js diff --git a/2223-sum-of-scores-of-built-strings.js b/2223.sum-of-scores-of-built-strings.js similarity index 100% rename from 2223-sum-of-scores-of-built-strings.js rename to 2223.sum-of-scores-of-built-strings.js diff --git a/2224-minimum-number-of-operations-to-convert-time.js b/2224.minimum-number-of-operations-to-convert-time.js similarity index 100% rename from 2224-minimum-number-of-operations-to-convert-time.js rename to 2224.minimum-number-of-operations-to-convert-time.js diff --git a/2225-find-players-with-zero-or-one-losses.js b/2225.find-players-with-zero-or-one-losses.js similarity index 100% rename from 2225-find-players-with-zero-or-one-losses.js rename to 2225.find-players-with-zero-or-one-losses.js diff --git a/2226-maximum-candies-allocated-to-k-children.js b/2226.maximum-candies-allocated-to-k-children.js similarity index 100% rename from 2226-maximum-candies-allocated-to-k-children.js rename to 2226.maximum-candies-allocated-to-k-children.js diff --git a/2227-encrypt-and-decrypt-strings.js b/2227.encrypt-and-decrypt-strings.js similarity index 100% rename from 2227-encrypt-and-decrypt-strings.js rename to 2227.encrypt-and-decrypt-strings.js diff --git a/223-rectangle-area.js b/223.rectangle-area.js similarity index 100% rename from 223-rectangle-area.js rename to 223.rectangle-area.js diff --git a/2233-maximum-product-after-k-increments.js b/2233.maximum-product-after-k-increments.js similarity index 100% rename from 2233-maximum-product-after-k-increments.js rename to 2233.maximum-product-after-k-increments.js diff --git a/2234-maximum-total-beauty-of-the-gardens.js b/2234.maximum-total-beauty-of-the-gardens.js similarity index 100% rename from 2234-maximum-total-beauty-of-the-gardens.js rename to 2234.maximum-total-beauty-of-the-gardens.js diff --git a/2235-add-two-integers.js b/2235.add-two-integers.js similarity index 100% rename from 2235-add-two-integers.js rename to 2235.add-two-integers.js diff --git a/2237-count-positions-on-street-with-required-brightness.js b/2237.count-positions-on-street-with-required-brightness.js similarity index 100% rename from 2237-count-positions-on-street-with-required-brightness.js rename to 2237.count-positions-on-street-with-required-brightness.js diff --git a/224-basic-calculator.js b/224.basic-calculator.js similarity index 100% rename from 224-basic-calculator.js rename to 224.basic-calculator.js diff --git a/2242-maximum-score-of-a-node-sequence.js b/2242.maximum-score-of-a-node-sequence.js similarity index 100% rename from 2242-maximum-score-of-a-node-sequence.js rename to 2242.maximum-score-of-a-node-sequence.js diff --git a/2243-calculate-digit-sum-of-a-string.js b/2243.calculate-digit-sum-of-a-string.js similarity index 100% rename from 2243-calculate-digit-sum-of-a-string.js rename to 2243.calculate-digit-sum-of-a-string.js diff --git a/2244-minimum-rounds-to-complete-all-tasks.js b/2244.minimum-rounds-to-complete-all-tasks.js similarity index 100% rename from 2244-minimum-rounds-to-complete-all-tasks.js rename to 2244.minimum-rounds-to-complete-all-tasks.js diff --git a/2245-maximum-trailing-zeros-in-a-cornered-path.js b/2245.maximum-trailing-zeros-in-a-cornered-path.js similarity index 100% rename from 2245-maximum-trailing-zeros-in-a-cornered-path.js rename to 2245.maximum-trailing-zeros-in-a-cornered-path.js diff --git a/2246-determine-if-two-events-have-conflict.js b/2246.determine-if-two-events-have-conflict.js similarity index 100% rename from 2246-determine-if-two-events-have-conflict.js rename to 2246.determine-if-two-events-have-conflict.js diff --git a/2246-longest-path-with-different-adjacent-characters.js b/2246.longest-path-with-different-adjacent-characters.js similarity index 100% rename from 2246-longest-path-with-different-adjacent-characters.js rename to 2246.longest-path-with-different-adjacent-characters.js diff --git a/2248-intersection-of-multiple-arrays.js b/2248.intersection-of-multiple-arrays.js similarity index 100% rename from 2248-intersection-of-multiple-arrays.js rename to 2248.intersection-of-multiple-arrays.js diff --git a/2249-count-lattice-points-inside-a-circle.js b/2249.count-lattice-points-inside-a-circle.js similarity index 100% rename from 2249-count-lattice-points-inside-a-circle.js rename to 2249.count-lattice-points-inside-a-circle.js diff --git a/225-implement-stack-using-queues.js b/225.implement-stack-using-queues.js similarity index 100% rename from 225-implement-stack-using-queues.js rename to 225.implement-stack-using-queues.js diff --git a/2250-count-number-of-rectangles-containing-each-point.js b/2250.count-number-of-rectangles-containing-each-point.js similarity index 100% rename from 2250-count-number-of-rectangles-containing-each-point.js rename to 2250.count-number-of-rectangles-containing-each-point.js diff --git a/2251-number-of-flowers-in-full-bloom.js b/2251.number-of-flowers-in-full-bloom.js similarity index 100% rename from 2251-number-of-flowers-in-full-bloom.js rename to 2251.number-of-flowers-in-full-bloom.js diff --git a/2258-escape-the-spreading-fire.js b/2258.escape-the-spreading-fire.js similarity index 100% rename from 2258-escape-the-spreading-fire.js rename to 2258.escape-the-spreading-fire.js diff --git a/2259-remove-digit-from-number-to-maximize-result.js b/2259.remove-digit-from-number-to-maximize-result.js similarity index 100% rename from 2259-remove-digit-from-number-to-maximize-result.js rename to 2259.remove-digit-from-number-to-maximize-result.js diff --git a/226-invert-binary-tree.js b/226.invert-binary-tree.js similarity index 100% rename from 226-invert-binary-tree.js rename to 226.invert-binary-tree.js diff --git a/2260-minimum-consecutive-cards-to-pick-up.js b/2260.minimum-consecutive-cards-to-pick-up.js similarity index 100% rename from 2260-minimum-consecutive-cards-to-pick-up.js rename to 2260.minimum-consecutive-cards-to-pick-up.js diff --git a/2261-k-divisible-elements-subarrays.js b/2261.k-divisible-elements-subarrays.js similarity index 100% rename from 2261-k-divisible-elements-subarrays.js rename to 2261.k-divisible-elements-subarrays.js diff --git a/2262-total-appeal-of-a-string.js b/2262.total-appeal-of-a-string.js similarity index 100% rename from 2262-total-appeal-of-a-string.js rename to 2262.total-appeal-of-a-string.js diff --git a/2263-make-array-non-decreasing-or-non-increasing.js b/2263.make-array-non-decreasing-or-non-increasing.js similarity index 100% rename from 2263-make-array-non-decreasing-or-non-increasing.js rename to 2263.make-array-non-decreasing-or-non-increasing.js diff --git a/2264-largest-3-same-digit-number-in-string.js b/2264.largest-3-same-digit-number-in-string.js similarity index 100% rename from 2264-largest-3-same-digit-number-in-string.js rename to 2264.largest-3-same-digit-number-in-string.js diff --git a/2265-count-nodes-equal-to-average-of-subtree.js b/2265.count-nodes-equal-to-average-of-subtree.js similarity index 100% rename from 2265-count-nodes-equal-to-average-of-subtree.js rename to 2265.count-nodes-equal-to-average-of-subtree.js diff --git a/2266-count-number-of-texts.js b/2266.count-number-of-texts.js similarity index 100% rename from 2266-count-number-of-texts.js rename to 2266.count-number-of-texts.js diff --git a/2267-check-if-there-is-a-valid-parentheses-string-path.js b/2267.check-if-there-is-a-valid-parentheses-string-path.js similarity index 100% rename from 2267-check-if-there-is-a-valid-parentheses-string-path.js rename to 2267.check-if-there-is-a-valid-parentheses-string-path.js diff --git a/2268-minimum-number-of-keypresses.js b/2268.minimum-number-of-keypresses.js similarity index 100% rename from 2268-minimum-number-of-keypresses.js rename to 2268.minimum-number-of-keypresses.js diff --git a/227-basic-calculator-ii.js b/227.basic-calculator-ii.js similarity index 100% rename from 227-basic-calculator-ii.js rename to 227.basic-calculator-ii.js diff --git a/2270-number-of-ways-to-split-array.js b/2270.number-of-ways-to-split-array.js similarity index 100% rename from 2270-number-of-ways-to-split-array.js rename to 2270.number-of-ways-to-split-array.js diff --git a/2271-maximum-white-tiles-covered-by-a-carpet.js b/2271.maximum-white-tiles-covered-by-a-carpet.js similarity index 100% rename from 2271-maximum-white-tiles-covered-by-a-carpet.js rename to 2271.maximum-white-tiles-covered-by-a-carpet.js diff --git a/2272-substring-with-largest-variance.js b/2272.substring-with-largest-variance.js similarity index 100% rename from 2272-substring-with-largest-variance.js rename to 2272.substring-with-largest-variance.js diff --git a/2273-find-resultant-array-after-removing-anagrams.js b/2273.find-resultant-array-after-removing-anagrams.js similarity index 100% rename from 2273-find-resultant-array-after-removing-anagrams.js rename to 2273.find-resultant-array-after-removing-anagrams.js diff --git a/2274-maximum-consecutive-floors-without-special-floors.js b/2274.maximum-consecutive-floors-without-special-floors.js similarity index 100% rename from 2274-maximum-consecutive-floors-without-special-floors.js rename to 2274.maximum-consecutive-floors-without-special-floors.js diff --git a/2275-largest-combination-with-bitwise-and-greater-than-zero.js b/2275.largest-combination-with-bitwise-and-greater-than-zero.js similarity index 100% rename from 2275-largest-combination-with-bitwise-and-greater-than-zero.js rename to 2275.largest-combination-with-bitwise-and-greater-than-zero.js diff --git a/2276-count-integers-in-intervals.js b/2276.count-integers-in-intervals.js similarity index 100% rename from 2276-count-integers-in-intervals.js rename to 2276.count-integers-in-intervals.js diff --git a/2277-closest-node-to-path-in-tree.js b/2277.closest-node-to-path-in-tree.js similarity index 100% rename from 2277-closest-node-to-path-in-tree.js rename to 2277.closest-node-to-path-in-tree.js diff --git a/228-summary-ranges.js b/228.summary-ranges.js similarity index 100% rename from 228-summary-ranges.js rename to 228.summary-ranges.js diff --git a/2280-minimum-lines-to-represent-a-line-chart.js b/2280.minimum-lines-to-represent-a-line-chart.js similarity index 100% rename from 2280-minimum-lines-to-represent-a-line-chart.js rename to 2280.minimum-lines-to-represent-a-line-chart.js diff --git a/2281-sum-of-total-strength-of-wizards.js b/2281.sum-of-total-strength-of-wizards.js similarity index 100% rename from 2281-sum-of-total-strength-of-wizards.js rename to 2281.sum-of-total-strength-of-wizards.js diff --git a/2282-number-of-people-that-can-be-seen-in-a-grid.js b/2282.number-of-people-that-can-be-seen-in-a-grid.js similarity index 100% rename from 2282-number-of-people-that-can-be-seen-in-a-grid.js rename to 2282.number-of-people-that-can-be-seen-in-a-grid.js diff --git a/2286-booking-concert-tickets-in-groups.js b/2286.booking-concert-tickets-in-groups.js similarity index 100% rename from 2286-booking-concert-tickets-in-groups.js rename to 2286.booking-concert-tickets-in-groups.js diff --git a/2287-rearrange-characters-to-make-target-string.js b/2287.rearrange-characters-to-make-target-string.js similarity index 100% rename from 2287-rearrange-characters-to-make-target-string.js rename to 2287.rearrange-characters-to-make-target-string.js diff --git a/2288-apply-discount-to-prices.js b/2288.apply-discount-to-prices.js similarity index 100% rename from 2288-apply-discount-to-prices.js rename to 2288.apply-discount-to-prices.js diff --git a/2289-steps-to-make-array-non-decreasing.js b/2289.steps-to-make-array-non-decreasing.js similarity index 100% rename from 2289-steps-to-make-array-non-decreasing.js rename to 2289.steps-to-make-array-non-decreasing.js diff --git a/229-majority-element-ii.js b/229.majority-element-ii.js similarity index 100% rename from 229-majority-element-ii.js rename to 229.majority-element-ii.js diff --git a/2290-minimum-obstacle-removal-to-reach-corner.js b/2290.minimum-obstacle-removal-to-reach-corner.js similarity index 100% rename from 2290-minimum-obstacle-removal-to-reach-corner.js rename to 2290.minimum-obstacle-removal-to-reach-corner.js diff --git a/2291-maximum-profit-from-trading-stocks.js b/2291.maximum-profit-from-trading-stocks.js similarity index 100% rename from 2291-maximum-profit-from-trading-stocks.js rename to 2291.maximum-profit-from-trading-stocks.js diff --git a/2293-min-max-game.js b/2293.min-max-game.js similarity index 100% rename from 2293-min-max-game.js rename to 2293.min-max-game.js diff --git a/2294-partition-array-such-that-maximum-difference-is-k.js b/2294.partition-array-such-that-maximum-difference-is-k.js similarity index 100% rename from 2294-partition-array-such-that-maximum-difference-is-k.js rename to 2294.partition-array-such-that-maximum-difference-is-k.js diff --git a/2295-replace-elements-in-an-array.js b/2295.replace-elements-in-an-array.js similarity index 100% rename from 2295-replace-elements-in-an-array.js rename to 2295.replace-elements-in-an-array.js diff --git a/2296-design-a-text-editor.js b/2296.design-a-text-editor.js similarity index 100% rename from 2296-design-a-text-editor.js rename to 2296.design-a-text-editor.js diff --git a/23-merge-k-sorted-lists.js b/23.merge-k-sorted-lists.js similarity index 100% rename from 23-merge-k-sorted-lists.js rename to 23.merge-k-sorted-lists.js diff --git a/230-kth-smallest-element-in-a-bst.js b/230.kth-smallest-element-in-a-bst.js similarity index 100% rename from 230-kth-smallest-element-in-a-bst.js rename to 230.kth-smallest-element-in-a-bst.js diff --git a/2302-count-subarrays-with-score-less-than-k.js b/2302.count-subarrays-with-score-less-than-k.js similarity index 100% rename from 2302-count-subarrays-with-score-less-than-k.js rename to 2302.count-subarrays-with-score-less-than-k.js diff --git a/2303-calculate-amount-paid-in-taxes.js b/2303.calculate-amount-paid-in-taxes.js similarity index 100% rename from 2303-calculate-amount-paid-in-taxes.js rename to 2303.calculate-amount-paid-in-taxes.js diff --git a/2304-minimum-path-cost-in-a-grid.js b/2304.minimum-path-cost-in-a-grid.js similarity index 100% rename from 2304-minimum-path-cost-in-a-grid.js rename to 2304.minimum-path-cost-in-a-grid.js diff --git a/2305-fair-distribution-of-cookies.js b/2305.fair-distribution-of-cookies.js similarity index 100% rename from 2305-fair-distribution-of-cookies.js rename to 2305.fair-distribution-of-cookies.js diff --git a/2306-naming-a-company.js b/2306.naming-a-company.js similarity index 100% rename from 2306-naming-a-company.js rename to 2306.naming-a-company.js diff --git a/231-power-of-two.js b/231.power-of-two.js similarity index 100% rename from 231-power-of-two.js rename to 231.power-of-two.js diff --git a/2311-longest-binary-subsequence-less-than-or-equal-to-k.js b/2311.longest-binary-subsequence-less-than-or-equal-to-k.js similarity index 100% rename from 2311-longest-binary-subsequence-less-than-or-equal-to-k.js rename to 2311.longest-binary-subsequence-less-than-or-equal-to-k.js diff --git a/2312-selling-pieces-of-wood.js b/2312.selling-pieces-of-wood.js similarity index 100% rename from 2312-selling-pieces-of-wood.js rename to 2312.selling-pieces-of-wood.js diff --git a/2313-minimum-flips-in-binary-tree-to-get-result.js b/2313.minimum-flips-in-binary-tree-to-get-result.js similarity index 100% rename from 2313-minimum-flips-in-binary-tree-to-get-result.js rename to 2313.minimum-flips-in-binary-tree-to-get-result.js diff --git a/2317-maximum-xor-after-operations.js b/2317.maximum-xor-after-operations.js similarity index 100% rename from 2317-maximum-xor-after-operations.js rename to 2317.maximum-xor-after-operations.js diff --git a/2318-number-of-distinct-roll-sequences.js b/2318.number-of-distinct-roll-sequences.js similarity index 100% rename from 2318-number-of-distinct-roll-sequences.js rename to 2318.number-of-distinct-roll-sequences.js diff --git a/2319-check-if-matrix-is-x-matrix.js b/2319.check-if-matrix-is-x-matrix.js similarity index 100% rename from 2319-check-if-matrix-is-x-matrix.js rename to 2319.check-if-matrix-is-x-matrix.js diff --git a/232-implement-queue-using-stacks.js b/232.implement-queue-using-stacks.js similarity index 100% rename from 232-implement-queue-using-stacks.js rename to 232.implement-queue-using-stacks.js diff --git a/2320-count-number-of-ways-to-place-houses.js b/2320.count-number-of-ways-to-place-houses.js similarity index 100% rename from 2320-count-number-of-ways-to-place-houses.js rename to 2320.count-number-of-ways-to-place-houses.js diff --git a/2321-maximum-score-of-spliced-array.js b/2321.maximum-score-of-spliced-array.js similarity index 100% rename from 2321-maximum-score-of-spliced-array.js rename to 2321.maximum-score-of-spliced-array.js diff --git a/2322-minimum-score-after-removals-on-a-tree.js b/2322.minimum-score-after-removals-on-a-tree.js similarity index 100% rename from 2322-minimum-score-after-removals-on-a-tree.js rename to 2322.minimum-score-after-removals-on-a-tree.js diff --git a/2325-decode-the-message.js b/2325.decode-the-message.js similarity index 100% rename from 2325-decode-the-message.js rename to 2325.decode-the-message.js diff --git a/2326-spiral-matrix-iv.js b/2326.spiral-matrix-iv.js similarity index 100% rename from 2326-spiral-matrix-iv.js rename to 2326.spiral-matrix-iv.js diff --git a/2327-number-of-people-aware-of-a-secret.js b/2327.number-of-people-aware-of-a-secret.js similarity index 100% rename from 2327-number-of-people-aware-of-a-secret.js rename to 2327.number-of-people-aware-of-a-secret.js diff --git a/2328-number-of-increasing-paths-in-a-grid.js b/2328.number-of-increasing-paths-in-a-grid.js similarity index 100% rename from 2328-number-of-increasing-paths-in-a-grid.js rename to 2328.number-of-increasing-paths-in-a-grid.js diff --git a/233-number-of-digit-one.js b/233.number-of-digit-one.js similarity index 100% rename from 233-number-of-digit-one.js rename to 233.number-of-digit-one.js diff --git a/2332-the-latest-time-to-catch-a-bus.js b/2332.the-latest-time-to-catch-a-bus.js similarity index 100% rename from 2332-the-latest-time-to-catch-a-bus.js rename to 2332.the-latest-time-to-catch-a-bus.js diff --git a/2333-minimum-sum-of-squared-difference.js b/2333.minimum-sum-of-squared-difference.js similarity index 100% rename from 2333-minimum-sum-of-squared-difference.js rename to 2333.minimum-sum-of-squared-difference.js diff --git a/2334-subarray-with-elements-greater-than-varying-threshold.js b/2334.subarray-with-elements-greater-than-varying-threshold.js similarity index 100% rename from 2334-subarray-with-elements-greater-than-varying-threshold.js rename to 2334.subarray-with-elements-greater-than-varying-threshold.js diff --git a/2335-minimum-amount-of-time-to-fill-cups.js b/2335.minimum-amount-of-time-to-fill-cups.js similarity index 100% rename from 2335-minimum-amount-of-time-to-fill-cups.js rename to 2335.minimum-amount-of-time-to-fill-cups.js diff --git a/2336-smallest-number-in-infinite-set.js b/2336.smallest-number-in-infinite-set.js similarity index 100% rename from 2336-smallest-number-in-infinite-set.js rename to 2336.smallest-number-in-infinite-set.js diff --git a/2337-move-pieces-to-obtain-a-string.js b/2337.move-pieces-to-obtain-a-string.js similarity index 100% rename from 2337-move-pieces-to-obtain-a-string.js rename to 2337.move-pieces-to-obtain-a-string.js diff --git a/2338-count-the-number-of-ideal-arrays.js b/2338.count-the-number-of-ideal-arrays.js similarity index 100% rename from 2338-count-the-number-of-ideal-arrays.js rename to 2338.count-the-number-of-ideal-arrays.js diff --git a/234-palindrome-linked-list.js b/234.palindrome-linked-list.js similarity index 100% rename from 234-palindrome-linked-list.js rename to 234.palindrome-linked-list.js diff --git a/2340-minimum-adjacent-swaps-to-make-a-valid-array.js b/2340.minimum-adjacent-swaps-to-make-a-valid-array.js similarity index 100% rename from 2340-minimum-adjacent-swaps-to-make-a-valid-array.js rename to 2340.minimum-adjacent-swaps-to-make-a-valid-array.js diff --git a/2341-maximum-number-of-pairs-in-array.js b/2341.maximum-number-of-pairs-in-array.js similarity index 100% rename from 2341-maximum-number-of-pairs-in-array.js rename to 2341.maximum-number-of-pairs-in-array.js diff --git a/2342-max-sum-of-a-pair-with-equal-sum-of-digits.js b/2342.max-sum-of-a-pair-with-equal-sum-of-digits.js similarity index 100% rename from 2342-max-sum-of-a-pair-with-equal-sum-of-digits.js rename to 2342.max-sum-of-a-pair-with-equal-sum-of-digits.js diff --git a/2343-query-kth-smallest-trimmed-number.js b/2343.query-kth-smallest-trimmed-number.js similarity index 100% rename from 2343-query-kth-smallest-trimmed-number.js rename to 2343.query-kth-smallest-trimmed-number.js diff --git a/2344-minimum-deletions-to-make-array-divisible.js b/2344.minimum-deletions-to-make-array-divisible.js similarity index 100% rename from 2344-minimum-deletions-to-make-array-divisible.js rename to 2344.minimum-deletions-to-make-array-divisible.js diff --git a/2347-best-poker-hand.js b/2347.best-poker-hand.js similarity index 100% rename from 2347-best-poker-hand.js rename to 2347.best-poker-hand.js diff --git a/2348-number-of-zero-filled-subarrays.js b/2348.number-of-zero-filled-subarrays.js similarity index 100% rename from 2348-number-of-zero-filled-subarrays.js rename to 2348.number-of-zero-filled-subarrays.js diff --git a/235-lowest-common-ancestor-of-a-binary-search-tree.js b/235.lowest-common-ancestor-of-a-binary-search-tree.js similarity index 100% rename from 235-lowest-common-ancestor-of-a-binary-search-tree.js rename to 235.lowest-common-ancestor-of-a-binary-search-tree.js diff --git a/2350-shortest-impossible-sequence-of-rolls.js b/2350.shortest-impossible-sequence-of-rolls.js similarity index 100% rename from 2350-shortest-impossible-sequence-of-rolls.js rename to 2350.shortest-impossible-sequence-of-rolls.js diff --git a/2351-first-letter-to-appear-twice.js b/2351.first-letter-to-appear-twice.js similarity index 100% rename from 2351-first-letter-to-appear-twice.js rename to 2351.first-letter-to-appear-twice.js diff --git a/2352-equal-row-and-column-pairs.js b/2352.equal-row-and-column-pairs.js similarity index 100% rename from 2352-equal-row-and-column-pairs.js rename to 2352.equal-row-and-column-pairs.js diff --git a/2353-design-a-food-rating-system.js b/2353.design-a-food-rating-system.js similarity index 100% rename from 2353-design-a-food-rating-system.js rename to 2353.design-a-food-rating-system.js diff --git a/2354-number-of-excellent-pairs.js b/2354.number-of-excellent-pairs.js similarity index 100% rename from 2354-number-of-excellent-pairs.js rename to 2354.number-of-excellent-pairs.js diff --git a/2355-maximum-number-of-books-you-can-take.js b/2355.maximum-number-of-books-you-can-take.js similarity index 100% rename from 2355-maximum-number-of-books-you-can-take.js rename to 2355.maximum-number-of-books-you-can-take.js diff --git a/2357-make-array-zero-by-subtracting-equal-amounts.js b/2357.make-array-zero-by-subtracting-equal-amounts.js similarity index 100% rename from 2357-make-array-zero-by-subtracting-equal-amounts.js rename to 2357.make-array-zero-by-subtracting-equal-amounts.js diff --git a/2358-maximum-number-of-groups-entering-a-competition.js b/2358.maximum-number-of-groups-entering-a-competition.js similarity index 100% rename from 2358-maximum-number-of-groups-entering-a-competition.js rename to 2358.maximum-number-of-groups-entering-a-competition.js diff --git a/2359-find-closest-node-to-given-two-nodes.js b/2359.find-closest-node-to-given-two-nodes.js similarity index 100% rename from 2359-find-closest-node-to-given-two-nodes.js rename to 2359.find-closest-node-to-given-two-nodes.js diff --git a/236-lowest-common-ancestor-of-a-binary-tree.js b/236.lowest-common-ancestor-of-a-binary-tree.js similarity index 100% rename from 236-lowest-common-ancestor-of-a-binary-tree.js rename to 236.lowest-common-ancestor-of-a-binary-tree.js diff --git a/2360-longest-cycle-in-a-graph.js b/2360.longest-cycle-in-a-graph.js similarity index 100% rename from 2360-longest-cycle-in-a-graph.js rename to 2360.longest-cycle-in-a-graph.js diff --git a/2365-task-scheduler-ii.js b/2365.task-scheduler-ii.js similarity index 100% rename from 2365-task-scheduler-ii.js rename to 2365.task-scheduler-ii.js diff --git a/2366-minimum-replacements-to-sort-the-array.js b/2366.minimum-replacements-to-sort-the-array.js similarity index 100% rename from 2366-minimum-replacements-to-sort-the-array.js rename to 2366.minimum-replacements-to-sort-the-array.js diff --git a/2367-count-number-of-bad-pairs.js b/2367.count-number-of-bad-pairs.js similarity index 100% rename from 2367-count-number-of-bad-pairs.js rename to 2367.count-number-of-bad-pairs.js diff --git a/2367-number-of-arithmetic-triplets.js b/2367.number-of-arithmetic-triplets.js similarity index 100% rename from 2367-number-of-arithmetic-triplets.js rename to 2367.number-of-arithmetic-triplets.js diff --git a/2368-reachable-nodes-with-restrictions.js b/2368.reachable-nodes-with-restrictions.js similarity index 100% rename from 2368-reachable-nodes-with-restrictions.js rename to 2368.reachable-nodes-with-restrictions.js diff --git a/2369-check-if-there-is-a-valid-partition-for-the-array.js b/2369.check-if-there-is-a-valid-partition-for-the-array.js similarity index 100% rename from 2369-check-if-there-is-a-valid-partition-for-the-array.js rename to 2369.check-if-there-is-a-valid-partition-for-the-array.js diff --git a/237-delete-node-in-a-linked-list.js b/237.delete-node-in-a-linked-list.js old mode 100755 new mode 100644 similarity index 95% rename from 237-delete-node-in-a-linked-list.js rename to 237.delete-node-in-a-linked-list.js index 84ee87b1..03cb8214 --- a/237-delete-node-in-a-linked-list.js +++ b/237.delete-node-in-a-linked-list.js @@ -1,17 +1,17 @@ -/** - * Definition for singly-linked list. - * function ListNode(val) { - * this.val = val; - * this.next = null; - * } - */ -/** - * @param {ListNode} node - * @return {void} Do not return anything, modify node in-place instead. - */ -const deleteNode = function(node) { - if (node.next !== null) { - node.val = node.next.val; - node.next = node.next.next; - } -}; +/** + * Definition for singly-linked list. + * function ListNode(val) { + * this.val = val; + * this.next = null; + * } + */ +/** + * @param {ListNode} node + * @return {void} Do not return anything, modify node in-place instead. + */ +const deleteNode = function(node) { + if (node.next !== null) { + node.val = node.next.val; + node.next = node.next.next; + } +}; diff --git a/2370-longest-ideal-subsequence.js b/2370.longest-ideal-subsequence.js similarity index 100% rename from 2370-longest-ideal-subsequence.js rename to 2370.longest-ideal-subsequence.js diff --git a/2373-largest-local-values-in-a-matrix.js b/2373.largest-local-values-in-a-matrix.js similarity index 100% rename from 2373-largest-local-values-in-a-matrix.js rename to 2373.largest-local-values-in-a-matrix.js diff --git a/2374-node-with-highest-edge-score.js b/2374.node-with-highest-edge-score.js similarity index 100% rename from 2374-node-with-highest-edge-score.js rename to 2374.node-with-highest-edge-score.js diff --git a/2375-construct-smallest-number-from-di-string.js b/2375.construct-smallest-number-from-di-string.js similarity index 100% rename from 2375-construct-smallest-number-from-di-string.js rename to 2375.construct-smallest-number-from-di-string.js diff --git a/2376-count-special-integers.js b/2376.count-special-integers.js similarity index 100% rename from 2376-count-special-integers.js rename to 2376.count-special-integers.js diff --git a/238-product-of-array-except-self.js b/238.product-of-array-except-self.js old mode 100755 new mode 100644 similarity index 95% rename from 238-product-of-array-except-self.js rename to 238.product-of-array-except-self.js index ccf2aa3e..619d6aeb --- a/238-product-of-array-except-self.js +++ b/238.product-of-array-except-self.js @@ -1,27 +1,27 @@ -/** - * @param {number[]} nums - * @return {number[]} - */ -const productExceptSelf = function(nums) { - const zeroIdx = new Set(); - const p = nums.reduce((ac, el, idx) => { - if (el === 0) { - zeroIdx.add(idx); - return ac; - } else { - return ac * el; - } - }, 1); - const res = []; - for (let i = 0; i < nums.length; i++) { - if (zeroIdx.size > 1) { - res.push(0); - } else if (zeroIdx.size === 1) { - res.push(i === [...zeroIdx.values()][0] ? p : 0); - } else { - res.push(p / nums[i]); - } - } - return res; -}; - +/** + * @param {number[]} nums + * @return {number[]} + */ +const productExceptSelf = function(nums) { + const zeroIdx = new Set(); + const p = nums.reduce((ac, el, idx) => { + if (el === 0) { + zeroIdx.add(idx); + return ac; + } else { + return ac * el; + } + }, 1); + const res = []; + for (let i = 0; i < nums.length; i++) { + if (zeroIdx.size > 1) { + res.push(0); + } else if (zeroIdx.size === 1) { + res.push(i === [...zeroIdx.values()][0] ? p : 0); + } else { + res.push(p / nums[i]); + } + } + return res; +}; + diff --git a/2380-time-needed-to-rearrange-a-binary-string.js b/2380.time-needed-to-rearrange-a-binary-string.js similarity index 100% rename from 2380-time-needed-to-rearrange-a-binary-string.js rename to 2380.time-needed-to-rearrange-a-binary-string.js diff --git a/2381-shifting-letters-ii.js b/2381.shifting-letters-ii.js similarity index 100% rename from 2381-shifting-letters-ii.js rename to 2381.shifting-letters-ii.js diff --git a/2382-maximum-segment-sum-after-removals.js b/2382.maximum-segment-sum-after-removals.js similarity index 100% rename from 2382-maximum-segment-sum-after-removals.js rename to 2382.maximum-segment-sum-after-removals.js diff --git a/2383-minimum-hours-of-training-to-win-a-competition.js b/2383.minimum-hours-of-training-to-win-a-competition.js similarity index 100% rename from 2383-minimum-hours-of-training-to-win-a-competition.js rename to 2383.minimum-hours-of-training-to-win-a-competition.js diff --git a/2384-largest-palindromic-number.js b/2384.largest-palindromic-number.js similarity index 100% rename from 2384-largest-palindromic-number.js rename to 2384.largest-palindromic-number.js diff --git a/2385-amount-of-time-for-binary-tree-to-be-infected.js b/2385.amount-of-time-for-binary-tree-to-be-infected.js similarity index 100% rename from 2385-amount-of-time-for-binary-tree-to-be-infected.js rename to 2385.amount-of-time-for-binary-tree-to-be-infected.js diff --git a/2386-find-the-k-sum-of-an-array.js b/2386.find-the-k-sum-of-an-array.js similarity index 100% rename from 2386-find-the-k-sum-of-an-array.js rename to 2386.find-the-k-sum-of-an-array.js diff --git a/2389-longest-subsequence-with-limited-sum.js b/2389.longest-subsequence-with-limited-sum.js similarity index 100% rename from 2389-longest-subsequence-with-limited-sum.js rename to 2389.longest-subsequence-with-limited-sum.js diff --git a/239-sliding-window-maximum.js b/239.sliding-window-maximum.js similarity index 100% rename from 239-sliding-window-maximum.js rename to 239.sliding-window-maximum.js diff --git a/2390-removing-stars-from-a-string.js b/2390.removing-stars-from-a-string.js similarity index 100% rename from 2390-removing-stars-from-a-string.js rename to 2390.removing-stars-from-a-string.js diff --git a/2391-minimum-amount-of-time-to-collect-garbage.js b/2391.minimum-amount-of-time-to-collect-garbage.js similarity index 100% rename from 2391-minimum-amount-of-time-to-collect-garbage.js rename to 2391.minimum-amount-of-time-to-collect-garbage.js diff --git a/2392-build-a-matrix-with-conditions.js b/2392.build-a-matrix-with-conditions.js similarity index 100% rename from 2392-build-a-matrix-with-conditions.js rename to 2392.build-a-matrix-with-conditions.js diff --git a/2397-maximum-rows-covered-by-columns.js b/2397.maximum-rows-covered-by-columns.js similarity index 100% rename from 2397-maximum-rows-covered-by-columns.js rename to 2397.maximum-rows-covered-by-columns.js diff --git a/2398-maximum-number-of-robots-within-budget.js b/2398.maximum-number-of-robots-within-budget.js similarity index 100% rename from 2398-maximum-number-of-robots-within-budget.js rename to 2398.maximum-number-of-robots-within-budget.js diff --git a/2399-check-distances-between-same-letters.js b/2399.check-distances-between-same-letters.js similarity index 100% rename from 2399-check-distances-between-same-letters.js rename to 2399.check-distances-between-same-letters.js diff --git a/24-swap-nodes-in-pairs.js b/24.swap-nodes-in-pairs.js old mode 100755 new mode 100644 similarity index 94% rename from 24-swap-nodes-in-pairs.js rename to 24.swap-nodes-in-pairs.js index ca159f33..9e9766c3 --- a/24-swap-nodes-in-pairs.js +++ b/24.swap-nodes-in-pairs.js @@ -1,33 +1,33 @@ -/** - * Definition for singly-linked list. - * function ListNode(val) { - * this.val = val; - * this.next = null; - * } - */ -/** - * @param {ListNode} head - * @return {ListNode} - */ -const swapPairs = function(node) { - const head = new ListNode(-1); - let cur = head; - - while (node !== null) { - if (node.next !== null) { - let one = node; - let two = node.next; - let three = node.next.next; - cur.next = two; - two.next = one; - one.next = three; - cur = cur.next.next; - node = three; - } else { - cur.next = node; - break; - } - } - - return head.next; -}; +/** + * Definition for singly-linked list. + * function ListNode(val) { + * this.val = val; + * this.next = null; + * } + */ +/** + * @param {ListNode} head + * @return {ListNode} + */ +const swapPairs = function(node) { + const head = new ListNode(-1); + let cur = head; + + while (node !== null) { + if (node.next !== null) { + let one = node; + let two = node.next; + let three = node.next.next; + cur.next = two; + two.next = one; + one.next = three; + cur = cur.next.next; + node = three; + } else { + cur.next = node; + break; + } + } + + return head.next; +}; diff --git a/240-search-a-2d-matrix-ii.js b/240.search-a-2d-matrix-ii.js similarity index 100% rename from 240-search-a-2d-matrix-ii.js rename to 240.search-a-2d-matrix-ii.js diff --git a/2400-number-of-ways-to-reach-a-position-after-exactly-k-steps.js b/2400.number-of-ways-to-reach-a-position-after-exactly-k-steps.js similarity index 100% rename from 2400-number-of-ways-to-reach-a-position-after-exactly-k-steps.js rename to 2400.number-of-ways-to-reach-a-position-after-exactly-k-steps.js diff --git a/2401-longest-nice-subarray.js b/2401.longest-nice-subarray.js similarity index 100% rename from 2401-longest-nice-subarray.js rename to 2401.longest-nice-subarray.js diff --git a/2402-meeting-rooms-iii.js b/2402.meeting-rooms-iii.js similarity index 100% rename from 2402-meeting-rooms-iii.js rename to 2402.meeting-rooms-iii.js diff --git a/2403-minimum-time-to-kill-all-monsters.js b/2403.minimum-time-to-kill-all-monsters.js similarity index 100% rename from 2403-minimum-time-to-kill-all-monsters.js rename to 2403.minimum-time-to-kill-all-monsters.js diff --git a/2404-most-frequent-even-element.js b/2404.most-frequent-even-element.js similarity index 100% rename from 2404-most-frequent-even-element.js rename to 2404.most-frequent-even-element.js diff --git a/2405-optimal-partition-of-string.js b/2405.optimal-partition-of-string.js similarity index 100% rename from 2405-optimal-partition-of-string.js rename to 2405.optimal-partition-of-string.js diff --git a/2406.divide-intervals-into-minimum-number-of-groups.js b/2406.divide.intervals-into-minimum-number-of-groups.js similarity index 100% rename from 2406.divide-intervals-into-minimum-number-of-groups.js rename to 2406.divide.intervals-into-minimum-number-of-groups.js diff --git a/2407.longest-increasing-subsequence-ii.js b/2407.longest.increasing-subsequence-ii.js similarity index 100% rename from 2407.longest-increasing-subsequence-ii.js rename to 2407.longest.increasing-subsequence-ii.js diff --git a/241-different-ways-to-add-parentheses.js b/241.different-ways-to-add-parentheses.js old mode 100755 new mode 100644 similarity index 95% rename from 241-different-ways-to-add-parentheses.js rename to 241.different-ways-to-add-parentheses.js index 54e72956..4feb0b6f --- a/241-different-ways-to-add-parentheses.js +++ b/241.different-ways-to-add-parentheses.js @@ -1,36 +1,36 @@ -/** - * @param {string} input - * @return {number[]} - */ -const diffWaysToCompute = function(input) { - const res = []; - let left; - let right; - for (let i = 0; i < input.length; i++) { - if (input[i] < "0") { - left = diffWaysToCompute(input.slice(0, i)); - right = diffWaysToCompute(input.slice(i + 1)); - for (let rl of left) { - for (let rr of right) { - switch (input[i]) { - case "+": - res.push(rl + rr); - break; - case "-": - res.push(rl - rr); - break; - case "*": - res.push(rl * rr); - break; - default: - break; - } - } - } - } - } - if (res.length === 0) { - res.push(+input); - } - return res; -}; +/** + * @param {string} input + * @return {number[]} + */ +const diffWaysToCompute = function(input) { + const res = []; + let left; + let right; + for (let i = 0; i < input.length; i++) { + if (input[i] < "0") { + left = diffWaysToCompute(input.slice(0, i)); + right = diffWaysToCompute(input.slice(i + 1)); + for (let rl of left) { + for (let rr of right) { + switch (input[i]) { + case "+": + res.push(rl + rr); + break; + case "-": + res.push(rl - rr); + break; + case "*": + res.push(rl * rr); + break; + default: + break; + } + } + } + } + } + if (res.length === 0) { + res.push(+input); + } + return res; +}; diff --git a/2411-smallest-subarrays-with-maximum-bitwise-or.js b/2411.smallest-subarrays-with-maximum-bitwise-or.js similarity index 100% rename from 2411-smallest-subarrays-with-maximum-bitwise-or.js rename to 2411.smallest-subarrays-with-maximum-bitwise-or.js diff --git a/2412-minimum-money-required-before-transactions.js b/2412.minimum-money-required-before-transactions.js similarity index 100% rename from 2412-minimum-money-required-before-transactions.js rename to 2412.minimum-money-required-before-transactions.js diff --git a/2413-smallest-even-multiple.js b/2413.smallest-even-multiple.js similarity index 100% rename from 2413-smallest-even-multiple.js rename to 2413.smallest-even-multiple.js diff --git a/2414-length-of-the-longest-alphabetical-continuous-substring.js b/2414.length-of-the-longest-alphabetical-continuous-substring.js similarity index 100% rename from 2414-length-of-the-longest-alphabetical-continuous-substring.js rename to 2414.length-of-the-longest-alphabetical-continuous-substring.js diff --git a/2415-reverse-odd-levels-of-binary-tree.js b/2415.reverse-odd-levels-of-binary-tree.js similarity index 100% rename from 2415-reverse-odd-levels-of-binary-tree.js rename to 2415.reverse-odd-levels-of-binary-tree.js diff --git a/2416-sum-of-prefix-scores-of-strings.js b/2416.sum-of-prefix-scores-of-strings.js similarity index 100% rename from 2416-sum-of-prefix-scores-of-strings.js rename to 2416.sum-of-prefix-scores-of-strings.js diff --git a/2418-sort-the-people.js b/2418.sort-the-people.js similarity index 100% rename from 2418-sort-the-people.js rename to 2418.sort-the-people.js diff --git a/2419-longest-subarray-with-maximum-bitwise-and.js b/2419.longest-subarray-with-maximum-bitwise-and.js similarity index 100% rename from 2419-longest-subarray-with-maximum-bitwise-and.js rename to 2419.longest-subarray-with-maximum-bitwise-and.js diff --git a/242-valid-anagram.js b/242.valid-anagram.js old mode 100755 new mode 100644 similarity index 95% rename from 242-valid-anagram.js rename to 242.valid-anagram.js index c4b41da1..9fc04773 --- a/242-valid-anagram.js +++ b/242.valid-anagram.js @@ -1,28 +1,28 @@ -/** - * @param {string} s - * @param {string} t - * @return {boolean} - */ -const isAnagram = function(s, t) { - if (s.length !== t.length) return false; - const sh = strHash(s); - const th = strHash(t); - for (let key in sh) { - if (sh.hasOwnProperty(key) && sh[key] !== th[key]) { - return false; - } - } - return true; -}; - -function strHash(str) { - let res = {}; - for (let i = 0; i < str.length; i++) { - if (res.hasOwnProperty(str[i])) { - res[str[i]] += 1; - } else { - res[str[i]] = 1; - } - } - return res; -} +/** + * @param {string} s + * @param {string} t + * @return {boolean} + */ +const isAnagram = function(s, t) { + if (s.length !== t.length) return false; + const sh = strHash(s); + const th = strHash(t); + for (let key in sh) { + if (sh.hasOwnProperty(key) && sh[key] !== th[key]) { + return false; + } + } + return true; +}; + +function strHash(str) { + let res = {}; + for (let i = 0; i < str.length; i++) { + if (res.hasOwnProperty(str[i])) { + res[str[i]] += 1; + } else { + res[str[i]] = 1; + } + } + return res; +} diff --git a/2420-find-all-good-indices.js b/2420.find-all-good-indices.js similarity index 100% rename from 2420-find-all-good-indices.js rename to 2420.find-all-good-indices.js diff --git a/2421-number-of-good-paths.js b/2421.number-of-good-paths.js similarity index 100% rename from 2421-number-of-good-paths.js rename to 2421.number-of-good-paths.js diff --git a/2422-merge-operations-to-turn-array-into-a-palindrome.js b/2422.merge-operations-to-turn-array-into-a-palindrome.js similarity index 100% rename from 2422-merge-operations-to-turn-array-into-a-palindrome.js rename to 2422.merge-operations-to-turn-array-into-a-palindrome.js diff --git a/2423-remove-letter-to-equalize-frequency.js b/2423.remove-letter-to-equalize-frequency.js similarity index 100% rename from 2423-remove-letter-to-equalize-frequency.js rename to 2423.remove-letter-to-equalize-frequency.js diff --git a/2426-number-of-pairs-satisfying-inequality.js b/2426.number-of-pairs-satisfying-inequality.js similarity index 100% rename from 2426-number-of-pairs-satisfying-inequality.js rename to 2426.number-of-pairs-satisfying-inequality.js diff --git a/2427-number-of-common-factors.js b/2427.number-of-common-factors.js similarity index 100% rename from 2427-number-of-common-factors.js rename to 2427.number-of-common-factors.js diff --git a/2428-maximum-sum-of-an-hourglass.js b/2428.maximum-sum-of-an-hourglass.js similarity index 100% rename from 2428-maximum-sum-of-an-hourglass.js rename to 2428.maximum-sum-of-an-hourglass.js diff --git a/2429-minimize-xor.js b/2429.minimize-xor.js similarity index 100% rename from 2429-minimize-xor.js rename to 2429.minimize-xor.js diff --git a/243-shortest-word-distance.js b/243.shortest-word-distance.js similarity index 100% rename from 243-shortest-word-distance.js rename to 243.shortest-word-distance.js diff --git a/2430-maximum-deletions-on-a-string.js b/2430.maximum-deletions-on-a-string.js similarity index 100% rename from 2430-maximum-deletions-on-a-string.js rename to 2430.maximum-deletions-on-a-string.js diff --git a/2432-the-employee-that-worked-on-the-longest-task.js b/2432.the-employee-that-worked-on-the-longest-task.js similarity index 100% rename from 2432-the-employee-that-worked-on-the-longest-task.js rename to 2432.the-employee-that-worked-on-the-longest-task.js diff --git a/2433-find-the-original-array-of-prefix-xor.js b/2433.find-the-original-array-of-prefix-xor.js similarity index 100% rename from 2433-find-the-original-array-of-prefix-xor.js rename to 2433.find-the-original-array-of-prefix-xor.js diff --git a/2434-using-a-robot-to-print-the-lexicographically-smallest-string.js b/2434.using-a-robot-to-print-the-lexicographically-smallest-string.js similarity index 100% rename from 2434-using-a-robot-to-print-the-lexicographically-smallest-string.js rename to 2434.using-a-robot-to-print-the-lexicographically-smallest-string.js diff --git a/2435-paths-in-matrix-whose-sum-is-divisible-by-k.js b/2435.paths-in-matrix-whose-sum-is-divisible-by-k.js similarity index 100% rename from 2435-paths-in-matrix-whose-sum-is-divisible-by-k.js rename to 2435.paths-in-matrix-whose-sum-is-divisible-by-k.js diff --git a/2438-range-product-queries-of-powers.js b/2438.range-product-queries-of-powers.js similarity index 100% rename from 2438-range-product-queries-of-powers.js rename to 2438.range-product-queries-of-powers.js diff --git a/2439-minimize-maximum-of-array.js b/2439.minimize-maximum-of-array.js similarity index 100% rename from 2439-minimize-maximum-of-array.js rename to 2439.minimize-maximum-of-array.js diff --git a/244-shortest-word-distance-ii.js b/244.shortest-word-distance-ii.js similarity index 100% rename from 244-shortest-word-distance-ii.js rename to 244.shortest-word-distance-ii.js diff --git a/2441-largest-positive-integer-that-exists-with-its-negative.js b/2441.largest-positive-integer-that-exists-with-its-negative.js similarity index 100% rename from 2441-largest-positive-integer-that-exists-with-its-negative.js rename to 2441.largest-positive-integer-that-exists-with-its-negative.js diff --git a/2442-count-number-of-distinct-integers-after-reverse-operations.js b/2442.count-number-of-distinct-integers-after-reverse-operations.js similarity index 100% rename from 2442-count-number-of-distinct-integers-after-reverse-operations.js rename to 2442.count-number-of-distinct-integers-after-reverse-operations.js diff --git a/2443-sum-of-number-and-its-reverse.js b/2443.sum-of-number-and-its-reverse.js similarity index 100% rename from 2443-sum-of-number-and-its-reverse.js rename to 2443.sum-of-number-and-its-reverse.js diff --git a/2444-count-subarrays-with-fixed-bounds.js b/2444.count-subarrays-with-fixed-bounds.js similarity index 100% rename from 2444-count-subarrays-with-fixed-bounds.js rename to 2444.count-subarrays-with-fixed-bounds.js diff --git a/2447-number-of-subarrays-with-gcd-equal-to-k.js b/2447.number-of-subarrays-with-gcd-equal-to-k.js similarity index 100% rename from 2447-number-of-subarrays-with-gcd-equal-to-k.js rename to 2447.number-of-subarrays-with-gcd-equal-to-k.js diff --git a/2448-minimum-cost-to-make-array-equal.js b/2448.minimum-cost-to-make-array-equal.js similarity index 100% rename from 2448-minimum-cost-to-make-array-equal.js rename to 2448.minimum-cost-to-make-array-equal.js diff --git a/2449-minimum-number-of-operations-to-make-arrays-similar.js b/2449.minimum-number-of-operations-to-make-arrays-similar.js similarity index 100% rename from 2449-minimum-number-of-operations-to-make-arrays-similar.js rename to 2449.minimum-number-of-operations-to-make-arrays-similar.js diff --git a/245-shortest-word-distance-iii.js b/245.shortest-word-distance-iii.js similarity index 100% rename from 245-shortest-word-distance-iii.js rename to 245.shortest-word-distance-iii.js diff --git a/2453-destroy-sequential-targets.js b/2453.destroy-sequential-targets.js similarity index 100% rename from 2453-destroy-sequential-targets.js rename to 2453.destroy-sequential-targets.js diff --git a/2454-next-greater-element-iv.js b/2454.next-greater-element-iv.js similarity index 100% rename from 2454-next-greater-element-iv.js rename to 2454.next-greater-element-iv.js diff --git a/2457-minimum-addition-to-make-integer-beautiful.js b/2457.minimum-addition-to-make-integer-beautiful.js similarity index 100% rename from 2457-minimum-addition-to-make-integer-beautiful.js rename to 2457.minimum-addition-to-make-integer-beautiful.js diff --git a/246-strobogrammatic-number.js b/246.strobogrammatic-number.js similarity index 100% rename from 246-strobogrammatic-number.js rename to 246.strobogrammatic-number.js diff --git a/2460-apply-operations-to-an-array.js b/2460.apply-operations-to-an-array.js similarity index 100% rename from 2460-apply-operations-to-an-array.js rename to 2460.apply-operations-to-an-array.js diff --git a/2461-maximum-sum-of-distinct-subarrays-with-length-k.js b/2461.maximum-sum-of-distinct-subarrays-with-length-k.js similarity index 100% rename from 2461-maximum-sum-of-distinct-subarrays-with-length-k.js rename to 2461.maximum-sum-of-distinct-subarrays-with-length-k.js diff --git a/2462-total-cost-to-hire-k-workers.js b/2462.total-cost-to-hire-k-workers.js similarity index 100% rename from 2462-total-cost-to-hire-k-workers.js rename to 2462.total-cost-to-hire-k-workers.js diff --git a/2463-minimum-total-distance-traveled.js b/2463.minimum-total-distance-traveled.js similarity index 100% rename from 2463-minimum-total-distance-traveled.js rename to 2463.minimum-total-distance-traveled.js diff --git a/2469-convert-the-temperature.js b/2469.convert-the-temperature.js similarity index 100% rename from 2469-convert-the-temperature.js rename to 2469.convert-the-temperature.js diff --git a/247-strobogrammatic-number-ii.js b/247.strobogrammatic-number-ii.js similarity index 100% rename from 247-strobogrammatic-number-ii.js rename to 247.strobogrammatic-number-ii.js diff --git a/2470-number-of-subarrays-with-lcm-equal-to-k.js b/2470.number-of-subarrays-with-lcm-equal-to-k.js similarity index 100% rename from 2470-number-of-subarrays-with-lcm-equal-to-k.js rename to 2470.number-of-subarrays-with-lcm-equal-to-k.js diff --git a/2471-minimum-number-of-operations-to-sort-a-binary-tree-by-level.js b/2471.minimum-number-of-operations-to-sort-a-binary-tree-by-level.js similarity index 100% rename from 2471-minimum-number-of-operations-to-sort-a-binary-tree-by-level.js rename to 2471.minimum-number-of-operations-to-sort-a-binary-tree-by-level.js diff --git a/2472-maximum-number-of-non-overlapping-palindrome-substrings.js b/2472.maximum-number-of-non-overlapping-palindrome-substrings.js similarity index 100% rename from 2472-maximum-number-of-non-overlapping-palindrome-substrings.js rename to 2472.maximum-number-of-non-overlapping-palindrome-substrings.js diff --git a/2475-number-of-unequal-triplets-in-array.js b/2475.number-of-unequal-triplets-in-array.js similarity index 100% rename from 2475-number-of-unequal-triplets-in-array.js rename to 2475.number-of-unequal-triplets-in-array.js diff --git a/2476-closest-nodes-queries-in-a-binary-search-tree.js b/2476.closest-nodes-queries-in-a-binary-search-tree.js similarity index 100% rename from 2476-closest-nodes-queries-in-a-binary-search-tree.js rename to 2476.closest-nodes-queries-in-a-binary-search-tree.js diff --git a/2477-minimum-fuel-cost-to-report-to-the-capital.js b/2477.minimum-fuel-cost-to-report-to-the-capital.js similarity index 100% rename from 2477-minimum-fuel-cost-to-report-to-the-capital.js rename to 2477.minimum-fuel-cost-to-report-to-the-capital.js diff --git a/2478-number-of-beautiful-partitions.js b/2478.number-of-beautiful-partitions.js similarity index 100% rename from 2478-number-of-beautiful-partitions.js rename to 2478.number-of-beautiful-partitions.js diff --git a/248-strobogrammatic-number-iii.js b/248.strobogrammatic-number-iii.js similarity index 100% rename from 248-strobogrammatic-number-iii.js rename to 248.strobogrammatic-number-iii.js diff --git a/2484-count-palindromic-subsequences.js b/2484.count-palindromic-subsequences.js similarity index 100% rename from 2484-count-palindromic-subsequences.js rename to 2484.count-palindromic-subsequences.js diff --git a/2485-find-the-pivot-integer.js b/2485.find-the-pivot-integer.js similarity index 100% rename from 2485-find-the-pivot-integer.js rename to 2485.find-the-pivot-integer.js diff --git a/2486-append-characters-to-string-to-make-subsequence.js b/2486.append-characters-to-string-to-make-subsequence.js similarity index 100% rename from 2486-append-characters-to-string-to-make-subsequence.js rename to 2486.append-characters-to-string-to-make-subsequence.js diff --git a/2487-remove-nodes-from-linked-list.js b/2487.remove-nodes-from-linked-list.js similarity index 100% rename from 2487-remove-nodes-from-linked-list.js rename to 2487.remove-nodes-from-linked-list.js diff --git a/2488-count-subarrays-with-median-k.js b/2488.count-subarrays-with-median-k.js similarity index 100% rename from 2488-count-subarrays-with-median-k.js rename to 2488.count-subarrays-with-median-k.js diff --git a/249-group-shifted-strings.js b/249.group-shifted-strings.js similarity index 100% rename from 249-group-shifted-strings.js rename to 249.group-shifted-strings.js diff --git a/2490-circular-sentence.js b/2490.circular-sentence.js similarity index 100% rename from 2490-circular-sentence.js rename to 2490.circular-sentence.js diff --git a/2491-divide-players-into-teams-of-equal-skill.js b/2491.divide-players-into-teams-of-equal-skill.js similarity index 100% rename from 2491-divide-players-into-teams-of-equal-skill.js rename to 2491.divide-players-into-teams-of-equal-skill.js diff --git a/2492-minimum-score-of-a-path-between-two-cities.js b/2492.minimum-score-of-a-path-between-two-cities.js similarity index 100% rename from 2492-minimum-score-of-a-path-between-two-cities.js rename to 2492.minimum-score-of-a-path-between-two-cities.js diff --git a/2493-divide-nodes-into-the-maximum-number-of-groups.js b/2493.divide-nodes-into-the-maximum-number-of-groups.js similarity index 100% rename from 2493-divide-nodes-into-the-maximum-number-of-groups.js rename to 2493.divide-nodes-into-the-maximum-number-of-groups.js diff --git a/25-reverse-nodes-in-k-group.js b/25.reverse-nodes-in-k-group.js similarity index 100% rename from 25-reverse-nodes-in-k-group.js rename to 25.reverse-nodes-in-k-group.js diff --git a/250-count-univalue-subtrees.js b/250.count-univalue-subtrees.js similarity index 100% rename from 250-count-univalue-subtrees.js rename to 250.count-univalue-subtrees.js diff --git a/2503-maximum-number-of-points-from-grid-queries.js b/2503.maximum-number-of-points-from-grid-queries.js similarity index 100% rename from 2503-maximum-number-of-points-from-grid-queries.js rename to 2503.maximum-number-of-points-from-grid-queries.js diff --git a/2508-add-edges-to-make-degrees-of-all-nodes-even.js b/2508.add-edges-to-make-degrees-of-all-nodes-even.js similarity index 100% rename from 2508-add-edges-to-make-degrees-of-all-nodes-even.js rename to 2508.add-edges-to-make-degrees-of-all-nodes-even.js diff --git a/251-flatten-2d-vector.js b/251.flatten-2d-vector.js similarity index 100% rename from 251-flatten-2d-vector.js rename to 251.flatten-2d-vector.js diff --git a/2514-count-anagrams.js b/2514.count-anagrams.js similarity index 100% rename from 2514-count-anagrams.js rename to 2514.count-anagrams.js diff --git a/2516-take-k-of-each-character-from-left-and-right.js b/2516.take-k-of-each-character-from-left-and-right.js similarity index 100% rename from 2516-take-k-of-each-character-from-left-and-right.js rename to 2516.take-k-of-each-character-from-left-and-right.js diff --git a/2518-number-of-great-partitions.js b/2518.number-of-great-partitions.js similarity index 100% rename from 2518-number-of-great-partitions.js rename to 2518.number-of-great-partitions.js diff --git a/252-meeting-rooms.js b/252.meeting-rooms.js similarity index 100% rename from 252-meeting-rooms.js rename to 252.meeting-rooms.js diff --git a/2521-distinct-prime-factors-of-product-of-array.js b/2521.distinct-prime-factors-of-product-of-array.js similarity index 100% rename from 2521-distinct-prime-factors-of-product-of-array.js rename to 2521.distinct-prime-factors-of-product-of-array.js diff --git a/2523-closest-prime-numbers-in-range.js b/2523.closest-prime-numbers-in-range.js similarity index 100% rename from 2523-closest-prime-numbers-in-range.js rename to 2523.closest-prime-numbers-in-range.js diff --git a/2527-find-xor-beauty-of-array.js b/2527.find-xor-beauty-of-array.js similarity index 100% rename from 2527-find-xor-beauty-of-array.js rename to 2527.find-xor-beauty-of-array.js diff --git a/2529-maximum-count-of-positive-integer-and-negative-integer.js b/2529.maximum-count-of-positive-integer-and-negative-integer.js similarity index 100% rename from 2529-maximum-count-of-positive-integer-and-negative-integer.js rename to 2529.maximum-count-of-positive-integer-and-negative-integer.js diff --git a/253-meeting-rooms-ii.js b/253.meeting-rooms-ii.js similarity index 100% rename from 253-meeting-rooms-ii.js rename to 253.meeting-rooms-ii.js diff --git a/2530-maximal-score-after-applying-k-operations.js b/2530.maximal-score-after-applying-k-operations.js similarity index 100% rename from 2530-maximal-score-after-applying-k-operations.js rename to 2530.maximal-score-after-applying-k-operations.js diff --git a/2531-make-number-of-distinct-characters-equal.js b/2531.make-number-of-distinct-characters-equal.js similarity index 100% rename from 2531-make-number-of-distinct-characters-equal.js rename to 2531.make-number-of-distinct-characters-equal.js diff --git a/2537-count-the-number-of-good-subarrays.js b/2537.count-the-number-of-good-subarrays.js similarity index 100% rename from 2537-count-the-number-of-good-subarrays.js rename to 2537.count-the-number-of-good-subarrays.js diff --git a/2538-difference-between-maximum-and-minimum-price-sum.js b/2538.difference-between-maximum-and-minimum-price-sum.js similarity index 100% rename from 2538-difference-between-maximum-and-minimum-price-sum.js rename to 2538.difference-between-maximum-and-minimum-price-sum.js diff --git a/254-factor-combinations.js b/254.factor-combinations.js similarity index 100% rename from 254-factor-combinations.js rename to 254.factor-combinations.js diff --git a/2542-maximum-subsequence-score.js b/2542.maximum-subsequence-score.js similarity index 100% rename from 2542-maximum-subsequence-score.js rename to 2542.maximum-subsequence-score.js diff --git a/2543-check-if-point-is-reachable.js b/2543.check-if-point-is-reachable.js similarity index 100% rename from 2543-check-if-point-is-reachable.js rename to 2543.check-if-point-is-reachable.js diff --git a/255-verify-preorder-sequence-in-binary-search-tree.js b/255.verify-preorder-sequence-in-binary-search-tree.js similarity index 100% rename from 255-verify-preorder-sequence-in-binary-search-tree.js rename to 255.verify-preorder-sequence-in-binary-search-tree.js diff --git a/2551-put-marbles-in-bags.js b/2551.put-marbles-in-bags.js similarity index 100% rename from 2551-put-marbles-in-bags.js rename to 2551.put-marbles-in-bags.js diff --git a/2555-maximize-win-from-two-segments.js b/2555.maximize-win-from-two-segments.js similarity index 100% rename from 2555-maximize-win-from-two-segments.js rename to 2555.maximize-win-from-two-segments.js diff --git a/2556-disconnect-path-in-a-binary-matrix-by-at-most-one-flip.js b/2556.disconnect-path-in-a-binary-matrix-by-at-most-one-flip.js similarity index 100% rename from 2556-disconnect-path-in-a-binary-matrix-by-at-most-one-flip.js rename to 2556.disconnect-path-in-a-binary-matrix-by-at-most-one-flip.js diff --git a/2557-maximum-number-of-integers-to-choose-from-a-range-ii.js b/2557.maximum-number-of-integers-to-choose-from-a-range-ii.js similarity index 100% rename from 2557-maximum-number-of-integers-to-choose-from-a-range-ii.js rename to 2557.maximum-number-of-integers-to-choose-from-a-range-ii.js diff --git a/2558-take-gifts-from-the-richest-pile.js b/2558.take-gifts-from-the-richest-pile.js similarity index 100% rename from 2558-take-gifts-from-the-richest-pile.js rename to 2558.take-gifts-from-the-richest-pile.js diff --git a/2559-count-vowel-strings-in-ranges.js b/2559.count-vowel-strings-in-ranges.js similarity index 100% rename from 2559-count-vowel-strings-in-ranges.js rename to 2559.count-vowel-strings-in-ranges.js diff --git a/256-paint-house.js b/256.paint-house.js similarity index 100% rename from 256-paint-house.js rename to 256.paint-house.js diff --git a/2560-house-robber-iv.js b/2560.house-robber-iv.js similarity index 100% rename from 2560-house-robber-iv.js rename to 2560.house-robber-iv.js diff --git a/2561-rearranging-fruits.js b/2561.rearranging-fruits.js similarity index 100% rename from 2561-rearranging-fruits.js rename to 2561.rearranging-fruits.js diff --git a/2563-count-the-number-of-fair-pairs.js b/2563.count-the-number-of-fair-pairs.js similarity index 100% rename from 2563-count-the-number-of-fair-pairs.js rename to 2563.count-the-number-of-fair-pairs.js diff --git a/2564-substring-xor-queries.js b/2564.substring-xor-queries.js similarity index 100% rename from 2564-substring-xor-queries.js rename to 2564.substring-xor-queries.js diff --git a/2565-subsequence-with-the-minimum-score.js b/2565.subsequence-with-the-minimum-score.js similarity index 100% rename from 2565-subsequence-with-the-minimum-score.js rename to 2565.subsequence-with-the-minimum-score.js diff --git a/2567-minimum-score-by-changing-two-elements.js b/2567.minimum-score-by-changing-two-elements.js similarity index 100% rename from 2567-minimum-score-by-changing-two-elements.js rename to 2567.minimum-score-by-changing-two-elements.js diff --git a/2568-minimum-impossible-or.js b/2568.minimum-impossible-or.js similarity index 100% rename from 2568-minimum-impossible-or.js rename to 2568.minimum-impossible-or.js diff --git a/257-binary-tree-paths.js b/257.binary-tree-paths.js old mode 100755 new mode 100644 similarity index 95% rename from 257-binary-tree-paths.js rename to 257.binary-tree-paths.js index ffc2a96a..86035e59 --- a/257-binary-tree-paths.js +++ b/257.binary-tree-paths.js @@ -1,30 +1,30 @@ -/** - * Definition for a binary tree node. - * function TreeNode(val) { - * this.val = val; - * this.left = this.right = null; - * } - */ -/** - * @param {TreeNode} root - * @return {string[]} - */ -const binaryTreePaths = function(root) { - const res = []; - traverse(root, res, []); - return res; -}; - -function traverse(node, res, p) { - if (node === null) return; - p.push(node.val); - if (node.left === null && node.right === null) { - res.push(p.join("->")); - } - if (node.left) { - traverse(node.left, res, p.slice(0)); - } - if (node.right) { - traverse(node.right, res, p.slice(0)); - } -} +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ +/** + * @param {TreeNode} root + * @return {string[]} + */ +const binaryTreePaths = function(root) { + const res = []; + traverse(root, res, []); + return res; +}; + +function traverse(node, res, p) { + if (node === null) return; + p.push(node.val); + if (node.left === null && node.right === null) { + res.push(p.join("->")); + } + if (node.left) { + traverse(node.left, res, p.slice(0)); + } + if (node.right) { + traverse(node.right, res, p.slice(0)); + } +} diff --git a/2570-merge-two-2d-arrays-by-summing-values.js b/2570.merge-two-2d-arrays-by-summing-values.js similarity index 100% rename from 2570-merge-two-2d-arrays-by-summing-values.js rename to 2570.merge-two-2d-arrays-by-summing-values.js diff --git a/2571-minimum-operations-to-reduce-an-integer-to-0.js b/2571.minimum-operations-to-reduce-an-integer-to-0.js similarity index 100% rename from 2571-minimum-operations-to-reduce-an-integer-to-0.js rename to 2571.minimum-operations-to-reduce-an-integer-to-0.js diff --git a/2572-count-the-number-of-square-free-subsets.js b/2572.count-the-number-of-square-free-subsets.js similarity index 100% rename from 2572-count-the-number-of-square-free-subsets.js rename to 2572.count-the-number-of-square-free-subsets.js diff --git a/2572-find-the-divisibility-array-of-a-string.js b/2572.find-the-divisibility-array-of-a-string.js similarity index 100% rename from 2572-find-the-divisibility-array-of-a-string.js rename to 2572.find-the-divisibility-array-of-a-string.js diff --git a/2573-find-the-string-with-lcp.js b/2573.find-the-string-with-lcp.js similarity index 100% rename from 2573-find-the-string-with-lcp.js rename to 2573.find-the-string-with-lcp.js diff --git a/2574-left-and-right-sum-differences.js b/2574.left-and-right-sum-differences.js similarity index 100% rename from 2574-left-and-right-sum-differences.js rename to 2574.left-and-right-sum-differences.js diff --git a/2576-find-the-maximum-number-of-marked-indices.js b/2576.find-the-maximum-number-of-marked-indices.js similarity index 100% rename from 2576-find-the-maximum-number-of-marked-indices.js rename to 2576.find-the-maximum-number-of-marked-indices.js diff --git a/2577-minimum-time-to-visit-a-cell-in-a-grid.js b/2577.minimum-time-to-visit-a-cell-in-a-grid.js similarity index 100% rename from 2577-minimum-time-to-visit-a-cell-in-a-grid.js rename to 2577.minimum-time-to-visit-a-cell-in-a-grid.js diff --git a/258-add-digits.js b/258.add-digits.js old mode 100755 new mode 100644 similarity index 94% rename from 258-add-digits.js rename to 258.add-digits.js index 211f9029..b6f25758 --- a/258-add-digits.js +++ b/258.add-digits.js @@ -1,15 +1,15 @@ -/** - * @param {number} num - * @return {number} - */ -const addDigits = function(num) { - let arr = ("" + num).split(""); - let res = num; - - while (arr.length > 1) { - res = arr.reduce((ac, el) => +ac + +el, 0); - arr = ("" + res).split(""); - } - return +res; -}; - +/** + * @param {number} num + * @return {number} + */ +const addDigits = function(num) { + let arr = ("" + num).split(""); + let res = num; + + while (arr.length > 1) { + res = arr.reduce((ac, el) => +ac + +el, 0); + arr = ("" + res).split(""); + } + return +res; +}; + diff --git a/2582-pass-the-pillow.js b/2582.pass-the-pillow.js similarity index 100% rename from 2582-pass-the-pillow.js rename to 2582.pass-the-pillow.js diff --git a/2583-kth-largest-sum-in-a-binary-tree.js b/2583.kth-largest-sum-in-a-binary-tree.js similarity index 100% rename from 2583-kth-largest-sum-in-a-binary-tree.js rename to 2583.kth-largest-sum-in-a-binary-tree.js diff --git a/2584-split-the-array-to-make-coprime-products.js b/2584.split-the-array-to-make-coprime-products.js similarity index 100% rename from 2584-split-the-array-to-make-coprime-products.js rename to 2584.split-the-array-to-make-coprime-products.js diff --git a/2585-number-of-ways-to-earn-points.js b/2585.number-of-ways-to-earn-points.js similarity index 100% rename from 2585-number-of-ways-to-earn-points.js rename to 2585.number-of-ways-to-earn-points.js diff --git a/259-3sum-smaller.js b/259.3sum-smaller.js similarity index 100% rename from 259-3sum-smaller.js rename to 259.3sum-smaller.js diff --git a/2599-make-the-prefix-sum-non-negative.js b/2599.make-the-prefix-sum-non-negative.js similarity index 100% rename from 2599-make-the-prefix-sum-non-negative.js rename to 2599.make-the-prefix-sum-non-negative.js diff --git a/26-remove-duplicates-from-sorted-array.js b/26.remove-duplicates-from-sorted-array.js old mode 100755 new mode 100644 similarity index 95% rename from 26-remove-duplicates-from-sorted-array.js rename to 26.remove-duplicates-from-sorted-array.js index ed141188..cdf4f4cf --- a/26-remove-duplicates-from-sorted-array.js +++ b/26.remove-duplicates-from-sorted-array.js @@ -1,18 +1,18 @@ -/** - * @param {number[]} nums - * @return {number} - */ -const removeDuplicates = function(nums) { - if (nums.length === 0) return 0; - if (nums.length === 1) return 1; - let pre = nums[0]; - for (let i = 1; i < nums.length; ) { - if (nums[i] !== pre) { - pre = nums[i]; - i += 1; - } else { - nums.splice(i, 1); - } - } - return nums.length; -}; +/** + * @param {number[]} nums + * @return {number} + */ +const removeDuplicates = function(nums) { + if (nums.length === 0) return 0; + if (nums.length === 1) return 1; + let pre = nums[0]; + for (let i = 1; i < nums.length; ) { + if (nums[i] !== pre) { + pre = nums[i]; + i += 1; + } else { + nums.splice(i, 1); + } + } + return nums.length; +}; diff --git a/260-single-number-III.js b/260.single-number-III.js old mode 100755 new mode 100644 similarity index 100% rename from 260-single-number-III.js rename to 260.single-number-III.js diff --git a/2604-minimum-time-to-eat-all-grains.js b/2604.minimum-time-to-eat-all-grains.js similarity index 100% rename from 2604-minimum-time-to-eat-all-grains.js rename to 2604.minimum-time-to-eat-all-grains.js diff --git a/2607-make-k-subarray-sums-equal.js b/2607.make-k-subarray-sums-equal.js similarity index 100% rename from 2607-make-k-subarray-sums-equal.js rename to 2607.make-k-subarray-sums-equal.js diff --git a/2608-shortest-cycle-in-a-graph.js b/2608.shortest-cycle-in-a-graph.js similarity index 100% rename from 2608-shortest-cycle-in-a-graph.js rename to 2608.shortest-cycle-in-a-graph.js diff --git a/2609-find-the-longest-balanced-substring-of-a-binary-string.js b/2609.find-the-longest-balanced-substring-of-a-binary-string.js similarity index 100% rename from 2609-find-the-longest-balanced-substring-of-a-binary-string.js rename to 2609.find-the-longest-balanced-substring-of-a-binary-string.js diff --git a/261-graph-valid-tree.js b/261.graph-valid-tree.js similarity index 100% rename from 261-graph-valid-tree.js rename to 261.graph-valid-tree.js diff --git a/2610-convert-an-array-into-a-2d-array-with-conditions.js b/2610.convert-an-array-into-a-2d-array-with-conditions.js similarity index 100% rename from 2610-convert-an-array-into-a-2d-array-with-conditions.js rename to 2610.convert-an-array-into-a-2d-array-with-conditions.js diff --git a/2611-mice-and-cheese.js b/2611.mice-and-cheese.js similarity index 100% rename from 2611-mice-and-cheese.js rename to 2611.mice-and-cheese.js diff --git a/2614-prime-in-diagonal.js b/2614.prime-in-diagonal.js similarity index 100% rename from 2614-prime-in-diagonal.js rename to 2614.prime-in-diagonal.js diff --git a/2615-sum-of-distances.js b/2615.sum-of-distances.js similarity index 100% rename from 2615-sum-of-distances.js rename to 2615.sum-of-distances.js diff --git a/2616-minimize-the-maximum-difference-of-pairs.js b/2616.minimize-the-maximum-difference-of-pairs.js similarity index 100% rename from 2616-minimize-the-maximum-difference-of-pairs.js rename to 2616.minimize-the-maximum-difference-of-pairs.js diff --git a/2619-array-prototype-last.js b/2619.array-prototype-last.js similarity index 100% rename from 2619-array-prototype-last.js rename to 2619.array-prototype-last.js diff --git a/2626-array-reduce-transformation.js b/2626.array-reduce-transformation.js similarity index 100% rename from 2626-array-reduce-transformation.js rename to 2626.array-reduce-transformation.js diff --git a/2627-debounce.js b/2627.debounce.js similarity index 100% rename from 2627-debounce.js rename to 2627.debounce.js diff --git a/263-ugly-number.js b/263.ugly-number.js similarity index 100% rename from 263-ugly-number.js rename to 263.ugly-number.js diff --git a/2630-memoize-ii.js b/2630.memoize-ii.js similarity index 100% rename from 2630-memoize-ii.js rename to 2630.memoize-ii.js diff --git a/2632-curry.js b/2632.curry.js similarity index 100% rename from 2632-curry.js rename to 2632.curry.js diff --git a/2636-promise-pool.js b/2636.promise-pool.js similarity index 100% rename from 2636-promise-pool.js rename to 2636.promise-pool.js diff --git a/264-ugly-number-ii.js b/264.ugly-number-ii.js similarity index 100% rename from 264-ugly-number-ii.js rename to 264.ugly-number-ii.js diff --git a/2640-find-the-score-of-all-prefixes-of-an-array.js b/2640.find-the-score-of-all-prefixes-of-an-array.js similarity index 100% rename from 2640-find-the-score-of-all-prefixes-of-an-array.js rename to 2640.find-the-score-of-all-prefixes-of-an-array.js diff --git a/2642-design-graph-with-shortest-path-calculator.js b/2642.design-graph-with-shortest-path-calculator.js similarity index 100% rename from 2642-design-graph-with-shortest-path-calculator.js rename to 2642.design-graph-with-shortest-path-calculator.js diff --git a/2643-row-with-maximum-ones.js b/2643.row-with-maximum-ones.js similarity index 100% rename from 2643-row-with-maximum-ones.js rename to 2643.row-with-maximum-ones.js diff --git a/2644-find-the-maximum-divisibility-score.js b/2644.find-the-maximum-divisibility-score.js similarity index 100% rename from 2644-find-the-maximum-divisibility-score.js rename to 2644.find-the-maximum-divisibility-score.js diff --git a/2645-minimum-additions-to-make-valid-string.js b/2645.minimum-additions-to-make-valid-string.js similarity index 100% rename from 2645-minimum-additions-to-make-valid-string.js rename to 2645.minimum-additions-to-make-valid-string.js diff --git a/2646-minimize-the-total-price-of-the-trips.js b/2646.minimize-the-total-price-of-the-trips.js similarity index 100% rename from 2646-minimize-the-total-price-of-the-trips.js rename to 2646.minimize-the-total-price-of-the-trips.js diff --git a/265-paint-house-ii.js b/265.paint-house-ii.js similarity index 100% rename from 265-paint-house-ii.js rename to 265.paint-house-ii.js diff --git a/2651-calculate-delayed-arrival-time.js b/2651.calculate-delayed-arrival-time.js similarity index 100% rename from 2651-calculate-delayed-arrival-time.js rename to 2651.calculate-delayed-arrival-time.js diff --git a/2652-sum-multiples.js b/2652.sum-multiples.js similarity index 100% rename from 2652-sum-multiples.js rename to 2652.sum-multiples.js diff --git a/2653-sliding-subarray-beauty.js b/2653.sliding-subarray-beauty.js similarity index 100% rename from 2653-sliding-subarray-beauty.js rename to 2653.sliding-subarray-beauty.js diff --git a/2654-minimum-number-of-operations-to-make-all-array-elements-equal-to-1.js b/2654.minimum-number-of-operations-to-make-all-array-elements-equal-to-1.js similarity index 100% rename from 2654-minimum-number-of-operations-to-make-all-array-elements-equal-to-1.js rename to 2654.minimum-number-of-operations-to-make-all-array-elements-equal-to-1.js diff --git a/2658-maximum-number-of-fish-in-a-grid.js b/2658.maximum-number-of-fish-in-a-grid.js similarity index 100% rename from 2658-maximum-number-of-fish-in-a-grid.js rename to 2658.maximum-number-of-fish-in-a-grid.js diff --git a/266-palindrome-permutation.js b/266.palindrome-permutation.js similarity index 100% rename from 266-palindrome-permutation.js rename to 266.palindrome-permutation.js diff --git a/2660-determine-the-winner-of-a-bowling-game.js b/2660.determine-the-winner-of-a-bowling-game.js similarity index 100% rename from 2660-determine-the-winner-of-a-bowling-game.js rename to 2660.determine-the-winner-of-a-bowling-game.js diff --git a/2661-first-completely-painted-row-or-column.js b/2661.first-completely-painted-row-or-column.js similarity index 100% rename from 2661-first-completely-painted-row-or-column.js rename to 2661.first-completely-painted-row-or-column.js diff --git a/2662-minimum-cost-of-a-path-with-special-roads.js b/2662.minimum-cost-of-a-path-with-special-roads.js similarity index 100% rename from 2662-minimum-cost-of-a-path-with-special-roads.js rename to 2662.minimum-cost-of-a-path-with-special-roads.js diff --git a/2663-lexicographically-smallest-beautiful-string.js b/2663.lexicographically-smallest-beautiful-string.js similarity index 100% rename from 2663-lexicographically-smallest-beautiful-string.js rename to 2663.lexicographically-smallest-beautiful-string.js diff --git a/267-palindrome-permutation-ii.js b/267.palindrome-permutation-ii.js similarity index 100% rename from 267-palindrome-permutation-ii.js rename to 267.palindrome-permutation-ii.js diff --git a/2670-find-the-distinct-difference-array.js b/2670.find-the-distinct-difference-array.js similarity index 100% rename from 2670-find-the-distinct-difference-array.js rename to 2670.find-the-distinct-difference-array.js diff --git a/2671-frequency-tracker.js b/2671.frequency-tracker.js similarity index 100% rename from 2671-frequency-tracker.js rename to 2671.frequency-tracker.js diff --git a/2672-number-of-adjacent-elements-with-the-same-color.js b/2672.number-of-adjacent-elements-with-the-same-color.js similarity index 100% rename from 2672-number-of-adjacent-elements-with-the-same-color.js rename to 2672.number-of-adjacent-elements-with-the-same-color.js diff --git a/2673-make-costs-of-paths-equal-in-a-binary-tree.js b/2673.make-costs-of-paths-equal-in-a-binary-tree.js similarity index 100% rename from 2673-make-costs-of-paths-equal-in-a-binary-tree.js rename to 2673.make-costs-of-paths-equal-in-a-binary-tree.js diff --git a/268-missing-number.js b/268.missing-number.js old mode 100755 new mode 100644 similarity index 95% rename from 268-missing-number.js rename to 268.missing-number.js index c682e434..1d47f100 --- a/268-missing-number.js +++ b/268.missing-number.js @@ -1,11 +1,11 @@ -/** - * @param {number[]} nums - * @return {number} - */ -const missingNumber = function(nums) { - const n = nums.length - let xor = 0 ^ nums[0] - for(let i = 1; i < n; i++) xor = xor ^ i ^ nums[i] - return xor ^ n -}; - +/** + * @param {number[]} nums + * @return {number} + */ +const missingNumber = function(nums) { + const n = nums.length + let xor = 0 ^ nums[0] + for(let i = 1; i < n; i++) xor = xor ^ i ^ nums[i] + return xor ^ n +}; + diff --git a/2681-power-of-heroes.js b/2681.power-of-heroes.js similarity index 100% rename from 2681-power-of-heroes.js rename to 2681.power-of-heroes.js diff --git a/2682-find-the-losers-of-the-circular-game.js b/2682.find-the-losers-of-the-circular-game.js similarity index 100% rename from 2682-find-the-losers-of-the-circular-game.js rename to 2682.find-the-losers-of-the-circular-game.js diff --git a/2683-neighboring-bitwise-xor.js b/2683.neighboring-bitwise-xor.js similarity index 100% rename from 2683-neighboring-bitwise-xor.js rename to 2683.neighboring-bitwise-xor.js diff --git a/2684-maximum-number-of-moves-in-a-grid.js b/2684.maximum-number-of-moves-in-a-grid.js similarity index 100% rename from 2684-maximum-number-of-moves-in-a-grid.js rename to 2684.maximum-number-of-moves-in-a-grid.js diff --git a/2685-count-the-number-of-complete-components.js b/2685.count-the-number-of-complete-components.js similarity index 100% rename from 2685-count-the-number-of-complete-components.js rename to 2685.count-the-number-of-complete-components.js diff --git a/269-alien-dictionary.js b/269.alien-dictionary.js similarity index 100% rename from 269-alien-dictionary.js rename to 269.alien-dictionary.js diff --git a/27-remove-element.js b/27.remove-element.js old mode 100755 new mode 100644 similarity index 94% rename from 27-remove-element.js rename to 27.remove-element.js index da7e0702..b8f3f354 --- a/27-remove-element.js +++ b/27.remove-element.js @@ -1,15 +1,15 @@ -/** - * @param {number[]} nums - * @param {number} val - * @return {number} - */ -const removeElement = function(nums, val) { - let j = 0; - for (let i = 0; i < nums.length; i++) { - if (nums[i] !== val) { - nums[j] = nums[i]; - j++; - } - } - return j; -}; +/** + * @param {number[]} nums + * @param {number} val + * @return {number} + */ +const removeElement = function(nums, val) { + let j = 0; + for (let i = 0; i < nums.length; i++) { + if (nums[i] !== val) { + nums[j] = nums[i]; + j++; + } + } + return j; +}; diff --git a/270-closest-binary-search-tree-value.js b/270.closest-binary-search-tree-value.js similarity index 100% rename from 270-closest-binary-search-tree-value.js rename to 270.closest-binary-search-tree-value.js diff --git a/2702-minimum-operations-to-make-numbers-non-positive.js b/2702.minimum-operations-to-make-numbers-non-positive.js similarity index 100% rename from 2702-minimum-operations-to-make-numbers-non-positive.js rename to 2702.minimum-operations-to-make-numbers-non-positive.js diff --git a/2709-greatest-common-divisor-traversal.js b/2709.greatest-common-divisor-traversal.js similarity index 100% rename from 2709-greatest-common-divisor-traversal.js rename to 2709.greatest-common-divisor-traversal.js diff --git a/271-encode-and-decode-strings.js b/271.encode-and-decode-strings.js similarity index 100% rename from 271-encode-and-decode-strings.js rename to 271.encode-and-decode-strings.js diff --git a/2710-remove-trailing-zeros-from-a-string.js b/2710.remove-trailing-zeros-from-a-string.js similarity index 100% rename from 2710-remove-trailing-zeros-from-a-string.js rename to 2710.remove-trailing-zeros-from-a-string.js diff --git a/2711-difference-of-number-of-distinct-values-on-diagonals.js b/2711.difference-of-number-of-distinct-values-on-diagonals.js similarity index 100% rename from 2711-difference-of-number-of-distinct-values-on-diagonals.js rename to 2711.difference-of-number-of-distinct-values-on-diagonals.js diff --git a/2712-minimum-cost-to-make-all-characters-equal.js b/2712.minimum-cost-to-make-all-characters-equal.js similarity index 100% rename from 2712-minimum-cost-to-make-all-characters-equal.js rename to 2712.minimum-cost-to-make-all-characters-equal.js diff --git a/2713-maximum-strictly-increasing-cells-in-a-matrix.js b/2713.maximum-strictly-increasing-cells-in-a-matrix.js similarity index 100% rename from 2713-maximum-strictly-increasing-cells-in-a-matrix.js rename to 2713.maximum-strictly-increasing-cells-in-a-matrix.js diff --git a/272-closest-binary-search-tree-value-ii.js b/272.closest-binary-search-tree-value-ii.js similarity index 100% rename from 272-closest-binary-search-tree-value-ii.js rename to 272.closest-binary-search-tree-value-ii.js diff --git a/273-integer-to-english-words.js b/273.integer-to-english-words.js similarity index 100% rename from 273-integer-to-english-words.js rename to 273.integer-to-english-words.js diff --git a/2730-find-the-longest-semi-repetitive-substring.js b/2730.find-the-longest-semi-repetitive-substring.js similarity index 100% rename from 2730-find-the-longest-semi-repetitive-substring.js rename to 2730.find-the-longest-semi-repetitive-substring.js diff --git a/274-h-index.js b/274.h-index.js similarity index 100% rename from 274-h-index.js rename to 274.h-index.js diff --git a/2747-count-zero-request-servers.js b/2747.count-zero-request-servers.js similarity index 100% rename from 2747-count-zero-request-servers.js rename to 2747.count-zero-request-servers.js diff --git a/275-h-index-ii.js b/275.h-index-ii.js similarity index 100% rename from 275-h-index-ii.js rename to 275.h-index-ii.js diff --git a/276-paint-fence.js b/276.paint-fence.js similarity index 100% rename from 276-paint-fence.js rename to 276.paint-fence.js diff --git a/2760-longest-even-odd-subarray-with-threshold.js b/2760.longest-even-odd-subarray-with-threshold.js similarity index 100% rename from 2760-longest-even-odd-subarray-with-threshold.js rename to 2760.longest-even-odd-subarray-with-threshold.js diff --git a/2761-prime-pairs-with-target-sum.js b/2761.prime-pairs-with-target-sum.js similarity index 100% rename from 2761-prime-pairs-with-target-sum.js rename to 2761.prime-pairs-with-target-sum.js diff --git a/2762-continuous-subarrays.js b/2762.continuous-subarrays.js similarity index 100% rename from 2762-continuous-subarrays.js rename to 2762.continuous-subarrays.js diff --git a/2763-sum-of-imbalance-numbers-of-all-subarrays.js b/2763.sum-of-imbalance-numbers-of-all-subarrays.js similarity index 100% rename from 2763-sum-of-imbalance-numbers-of-all-subarrays.js rename to 2763.sum-of-imbalance-numbers-of-all-subarrays.js diff --git a/2769-find-the-maximum-achievable-number.js b/2769.find-the-maximum-achievable-number.js similarity index 100% rename from 2769-find-the-maximum-achievable-number.js rename to 2769.find-the-maximum-achievable-number.js diff --git a/277-find-the-celebrity.js b/277.find-the-celebrity.js similarity index 100% rename from 277-find-the-celebrity.js rename to 277.find-the-celebrity.js diff --git a/2770-maximum-number-of-jumps-to-reach-the-last-index.js b/2770.maximum-number-of-jumps-to-reach-the-last-index.js similarity index 100% rename from 2770-maximum-number-of-jumps-to-reach-the-last-index.js rename to 2770.maximum-number-of-jumps-to-reach-the-last-index.js diff --git a/2771-longest-non-decreasing-subarray-from-two-arrays.js b/2771.longest-non-decreasing-subarray-from-two-arrays.js similarity index 100% rename from 2771-longest-non-decreasing-subarray-from-two-arrays.js rename to 2771.longest-non-decreasing-subarray-from-two-arrays.js diff --git a/2772-apply-operations-to-make-all-array-elements-equal-to-zero.js b/2772.apply-operations-to-make-all-array-elements-equal-to-zero.js similarity index 100% rename from 2772-apply-operations-to-make-all-array-elements-equal-to-zero.js rename to 2772.apply-operations-to-make-all-array-elements-equal-to-zero.js diff --git a/2778-sum-of-squares-of-special-elements.js b/2778.sum-of-squares-of-special-elements.js similarity index 100% rename from 2778-sum-of-squares-of-special-elements.js rename to 2778.sum-of-squares-of-special-elements.js diff --git a/2779-maximum-beauty-of-an-array-after-applying-operation.js b/2779.maximum-beauty-of-an-array-after-applying-operation.js similarity index 100% rename from 2779-maximum-beauty-of-an-array-after-applying-operation.js rename to 2779.maximum-beauty-of-an-array-after-applying-operation.js diff --git a/278-first-bad-version.js b/278.first-bad-version.js similarity index 100% rename from 278-first-bad-version.js rename to 278.first-bad-version.js diff --git a/2780-minimum-index-of-a-valid-split.js b/2780.minimum-index-of-a-valid-split.js similarity index 100% rename from 2780-minimum-index-of-a-valid-split.js rename to 2780.minimum-index-of-a-valid-split.js diff --git a/2781-length-of-the-longest-valid-substring.js b/2781.length-of-the-longest-valid-substring.js similarity index 100% rename from 2781-length-of-the-longest-valid-substring.js rename to 2781.length-of-the-longest-valid-substring.js diff --git a/279-perfect-squares.js b/279.perfect-squares.js similarity index 100% rename from 279-perfect-squares.js rename to 279.perfect-squares.js diff --git a/28-implement-strStr().js b/28.implement-strStr().js old mode 100755 new mode 100644 similarity index 95% rename from 28-implement-strStr().js rename to 28.implement-strStr().js index f4faa4bf..00435ab1 --- a/28-implement-strStr().js +++ b/28.implement-strStr().js @@ -1,30 +1,30 @@ -/** - * @param {string} haystack - * @param {string} needle - * @return {number} - */ -const strStr = function(a, b) { - if(b === '') return 0 - if(a.length < b.length) return -1 - if(a.length === b.length) return a === b ? 0 : -1 - const m = a.length, n = b.length - const fail = Array(n).fill(-1) - // DFA - for(let i = 1; i < n; i++) { - let j = fail[i - 1] - while(j !== -1 && b[j + 1] !== b[i]) { - j = fail[j] - } - if(b[j + 1] === b[i]) fail[i] = j + 1 - } - let pos = -1 - for(let i = 0; i < m; i++) { - while(pos !== -1 && a[i] !== b[pos + 1]) pos = fail[pos] - if(a[i] === b[pos + 1]) { - pos++ - if(pos === n - 1) return i - n + 1 - } - } - return -1 -}; - +/** + * @param {string} haystack + * @param {string} needle + * @return {number} + */ +const strStr = function(a, b) { + if(b === '') return 0 + if(a.length < b.length) return -1 + if(a.length === b.length) return a === b ? 0 : -1 + const m = a.length, n = b.length + const fail = Array(n).fill(-1) + // DFA + for(let i = 1; i < n; i++) { + let j = fail[i - 1] + while(j !== -1 && b[j + 1] !== b[i]) { + j = fail[j] + } + if(b[j + 1] === b[i]) fail[i] = j + 1 + } + let pos = -1 + for(let i = 0; i < m; i++) { + while(pos !== -1 && a[i] !== b[pos + 1]) pos = fail[pos] + if(a[i] === b[pos + 1]) { + pos++ + if(pos === n - 1) return i - n + 1 + } + } + return -1 +}; + diff --git a/280-wiggle-sort.js b/280.wiggle-sort.js similarity index 100% rename from 280-wiggle-sort.js rename to 280.wiggle-sort.js diff --git a/281-zigzag-iterator.js b/281.zigzag-iterator.js similarity index 100% rename from 281-zigzag-iterator.js rename to 281.zigzag-iterator.js diff --git a/282-expression-add-operators.js b/282.expression-add-operators.js similarity index 100% rename from 282-expression-add-operators.js rename to 282.expression-add-operators.js diff --git a/283-move-zeroes.js b/283.move-zeroes.js old mode 100755 new mode 100644 similarity index 95% rename from 283-move-zeroes.js rename to 283.move-zeroes.js index 3b836d3f..6c991c3f --- a/283-move-zeroes.js +++ b/283.move-zeroes.js @@ -1,17 +1,17 @@ -/** - * @param {number[]} nums - * @return {void} Do not return anything, modify nums in-place instead. - */ -const moveZeroes = function(nums) { - const len = nums.length; - let l = len; - for (let i = 0; i < l; ) { - if (nums[i] === 0) { - nums.splice(i, 1); - nums.push(0); - l -= 1; - } else { - i += 1; - } - } -}; +/** + * @param {number[]} nums + * @return {void} Do not return anything, modify nums in-place instead. + */ +const moveZeroes = function(nums) { + const len = nums.length; + let l = len; + for (let i = 0; i < l; ) { + if (nums[i] === 0) { + nums.splice(i, 1); + nums.push(0); + l -= 1; + } else { + i += 1; + } + } +}; diff --git a/285-inorder-successor-in-bst.js b/285.inorder-successor-in-bst.js similarity index 100% rename from 285-inorder-successor-in-bst.js rename to 285.inorder-successor-in-bst.js diff --git a/286-walls-and-gates.js b/286.walls-and-gates.js similarity index 100% rename from 286-walls-and-gates.js rename to 286.walls-and-gates.js diff --git a/287-find-the-duplicate-number.js b/287.find-the-duplicate-number.js old mode 100755 new mode 100644 similarity index 95% rename from 287-find-the-duplicate-number.js rename to 287.find-the-duplicate-number.js index caf038ec..0a1e6bde --- a/287-find-the-duplicate-number.js +++ b/287.find-the-duplicate-number.js @@ -1,28 +1,28 @@ -/** - * @param {number[]} nums - * @return {number} - */ -const findDuplicate = function(nums) { - const n = nums.length; - let ans = 0; - let bit_max = 31; - while (((n - 1) >> bit_max) == 0) { - bit_max -= 1; - } - for (let bit = 0; bit <= bit_max; ++bit) { - let x = 0, y = 0; - for (let i = 0; i < n; ++i) { - if ((nums[i] & (1 << bit)) != 0) { - x += 1; - } - if (i >= 1 && ((i & (1 << bit)) != 0)) { - y += 1; - } - } - if (x > y) { - ans |= 1 << bit; - } - } - return ans; -}; - +/** + * @param {number[]} nums + * @return {number} + */ +const findDuplicate = function(nums) { + const n = nums.length; + let ans = 0; + let bit_max = 31; + while (((n - 1) >> bit_max) == 0) { + bit_max -= 1; + } + for (let bit = 0; bit <= bit_max; ++bit) { + let x = 0, y = 0; + for (let i = 0; i < n; ++i) { + if ((nums[i] & (1 << bit)) != 0) { + x += 1; + } + if (i >= 1 && ((i & (1 << bit)) != 0)) { + y += 1; + } + } + if (x > y) { + ans |= 1 << bit; + } + } + return ans; +}; + diff --git a/288-unique-word-abbreviation.js b/288.unique-word-abbreviation.js similarity index 100% rename from 288-unique-word-abbreviation.js rename to 288.unique-word-abbreviation.js diff --git a/289-game-of-life.js b/289.game-of-life.js similarity index 100% rename from 289-game-of-life.js rename to 289.game-of-life.js diff --git a/29-divide-two-integers.js b/29.divide-two-integers.js old mode 100755 new mode 100644 similarity index 96% rename from 29-divide-two-integers.js rename to 29.divide-two-integers.js index 68e42803..f06d3520 --- a/29-divide-two-integers.js +++ b/29.divide-two-integers.js @@ -1,29 +1,29 @@ -/** - * @param {number} dividend - * @param {number} divisor - * @return {number} - */ -const divide = function(dividend, divisor) { - if (!divisor || (dividend === Number.MIN_SAFE_INTEGER && divisor === -1)) { - return Number.MAX_SAFE_INTEGER; - } - const MAX_INT = Math.pow(2, 31) - 1; - if (dividend === -2147483648 && divisor === -1) return MAX_INT; - - const sign = (dividend < 0) ^ (divisor < 0) ? -1 : 1; - let dvd = Math.abs(dividend); - let dvs = Math.abs(divisor); - let res = 0; - - while (dvd >= dvs) { - let tmp = dvs; - let multiple = 1; - while (dvd >= tmp << 1 && tmp << 1 > 0) { - tmp <<= 1; - multiple <<= 1; - } - dvd -= tmp; - res += multiple; - } - return sign === 1 ? res : -res; -}; +/** + * @param {number} dividend + * @param {number} divisor + * @return {number} + */ +const divide = function(dividend, divisor) { + if (!divisor || (dividend === Number.MIN_SAFE_INTEGER && divisor === -1)) { + return Number.MAX_SAFE_INTEGER; + } + const MAX_INT = Math.pow(2, 31) - 1; + if (dividend === -2147483648 && divisor === -1) return MAX_INT; + + const sign = (dividend < 0) ^ (divisor < 0) ? -1 : 1; + let dvd = Math.abs(dividend); + let dvs = Math.abs(divisor); + let res = 0; + + while (dvd >= dvs) { + let tmp = dvs; + let multiple = 1; + while (dvd >= tmp << 1 && tmp << 1 > 0) { + tmp <<= 1; + multiple <<= 1; + } + dvd -= tmp; + res += multiple; + } + return sign === 1 ? res : -res; +}; diff --git a/290-word-pattern.js b/290.word-pattern.js similarity index 100% rename from 290-word-pattern.js rename to 290.word-pattern.js diff --git a/291-word-pattern-ii.js b/291.word-pattern-ii.js similarity index 100% rename from 291-word-pattern-ii.js rename to 291.word-pattern-ii.js diff --git a/292-nim-game.js b/292.nim-game.js similarity index 100% rename from 292-nim-game.js rename to 292.nim-game.js diff --git a/293-flip-game.js b/293.flip-game.js similarity index 100% rename from 293-flip-game.js rename to 293.flip-game.js diff --git a/294-flip-game-ii.js b/294.flip-game-ii.js similarity index 100% rename from 294-flip-game-ii.js rename to 294.flip-game-ii.js diff --git a/295-find-median-from-data-stream.js b/295.find-median-from-data-stream.js similarity index 100% rename from 295-find-median-from-data-stream.js rename to 295.find-median-from-data-stream.js diff --git a/296-best-meeting-point.js b/296.best-meeting-point.js similarity index 100% rename from 296-best-meeting-point.js rename to 296.best-meeting-point.js diff --git a/297-serialize-and-deserialize-binary-tree.js b/297.serialize-and-deserialize-binary-tree.js similarity index 100% rename from 297-serialize-and-deserialize-binary-tree.js rename to 297.serialize-and-deserialize-binary-tree.js diff --git a/298-binary-tree-longest-consecutive-sequence.js b/298.binary-tree-longest-consecutive-sequence.js similarity index 100% rename from 298-binary-tree-longest-consecutive-sequence.js rename to 298.binary-tree-longest-consecutive-sequence.js diff --git a/299-bulls-and-cows.js b/299.bulls-and-cows.js similarity index 100% rename from 299-bulls-and-cows.js rename to 299.bulls-and-cows.js diff --git a/3-longest-substring-without-repeating-characters.js b/3.longest-substring-without-repeating-characters.js old mode 100755 new mode 100644 similarity index 95% rename from 3-longest-substring-without-repeating-characters.js rename to 3.longest-substring-without-repeating-characters.js index 00030787..91fab5c9 --- a/3-longest-substring-without-repeating-characters.js +++ b/3.longest-substring-without-repeating-characters.js @@ -1,19 +1,19 @@ -/** - * @param {string} s - * @return {number} - */ -const lengthOfLongestSubstring = function(s) { - if(s.length < 2) return s.length - const hash = {} - let max = 0 - for(let i = 0, j = -1, len = s.length; i < len; i++) { - const cur = s[i] - if(hash[cur] != null) j = Math.max(j, hash[cur]) - - hash[cur] = i - max = Math.max(max, i - j) - } - - return max -}; - +/** + * @param {string} s + * @return {number} + */ +const lengthOfLongestSubstring = function(s) { + if(s.length < 2) return s.length + const hash = {} + let max = 0 + for(let i = 0, j = -1, len = s.length; i < len; i++) { + const cur = s[i] + if(hash[cur] != null) j = Math.max(j, hash[cur]) + + hash[cur] = i + max = Math.max(max, i - j) + } + + return max +}; + diff --git a/30-substring-with-concatenation-of-all-words.js b/30.substring-with-concatenation-of-all-words.js similarity index 100% rename from 30-substring-with-concatenation-of-all-words.js rename to 30.substring-with-concatenation-of-all-words.js diff --git a/300-longest-increasing-subsequence.js b/300.longest-increasing-subsequence.js similarity index 100% rename from 300-longest-increasing-subsequence.js rename to 300.longest-increasing-subsequence.js diff --git a/301-remove-invalid-parentheses.js b/301.remove-invalid-parentheses.js similarity index 100% rename from 301-remove-invalid-parentheses.js rename to 301.remove-invalid-parentheses.js diff --git a/302-smallest-rectangle-enclosing-black-pixels.js b/302.smallest-rectangle-enclosing-black-pixels.js similarity index 100% rename from 302-smallest-rectangle-enclosing-black-pixels.js rename to 302.smallest-rectangle-enclosing-black-pixels.js diff --git a/304-range-sum-query-2d-immutable.js b/304.range-sum-query-2d-immutable.js similarity index 100% rename from 304-range-sum-query-2d-immutable.js rename to 304.range-sum-query-2d-immutable.js diff --git a/305-number-of-islands-ii.js b/305.number-of-islands-ii.js similarity index 100% rename from 305-number-of-islands-ii.js rename to 305.number-of-islands-ii.js diff --git a/306-additive-number.js b/306.additive-number.js similarity index 100% rename from 306-additive-number.js rename to 306.additive-number.js diff --git a/307-range-sum-query-mutable.js b/307.range-sum-query-mutable.js similarity index 100% rename from 307-range-sum-query-mutable.js rename to 307.range-sum-query-mutable.js diff --git a/308-range-sum-query-2d-mutable.js b/308.range-sum-query-2d-mutable.js similarity index 100% rename from 308-range-sum-query-2d-mutable.js rename to 308.range-sum-query-2d-mutable.js diff --git a/309-best-time-to-buy-and-sell-stock-with-cooldown.js b/309.best-time-to-buy-and-sell-stock-with-cooldown.js old mode 100755 new mode 100644 similarity index 96% rename from 309-best-time-to-buy-and-sell-stock-with-cooldown.js rename to 309.best-time-to-buy-and-sell-stock-with-cooldown.js index 0bc3138d..e83471a1 --- a/309-best-time-to-buy-and-sell-stock-with-cooldown.js +++ b/309.best-time-to-buy-and-sell-stock-with-cooldown.js @@ -1,26 +1,26 @@ -/** - * @param {number[]} prices - * @return {number} - */ -const maxProfit = function(prices) { - if (prices === null || prices.length < 1) { - return 0; - } - - const length = prices.length; - // buy[i]: max profit if the first "i" days end with a "buy" day - const buy = Array(length + 1).fill(0); - // buy[i]: max profit if the first "i" days end with a "sell" day - const sell = Array(length + 1).fill(0); - - buy[1] = -prices[0]; - - for (let i = 2; i <= length; i++) { - const price = prices[i - 1]; - buy[i] = Math.max(buy[i - 1], sell[i - 2] - price); - sell[i] = Math.max(sell[i - 1], buy[i - 1] + price); - } - - // sell[length] >= buy[length] - return sell[length]; -}; +/** + * @param {number[]} prices + * @return {number} + */ +const maxProfit = function(prices) { + if (prices === null || prices.length < 1) { + return 0; + } + + const length = prices.length; + // buy[i]: max profit if the first "i" days end with a "buy" day + const buy = Array(length + 1).fill(0); + // buy[i]: max profit if the first "i" days end with a "sell" day + const sell = Array(length + 1).fill(0); + + buy[1] = -prices[0]; + + for (let i = 2; i <= length; i++) { + const price = prices[i - 1]; + buy[i] = Math.max(buy[i - 1], sell[i - 2] - price); + sell[i] = Math.max(sell[i - 1], buy[i - 1] + price); + } + + // sell[length] >= buy[length] + return sell[length]; +}; diff --git a/31-next-permutation.js b/31.next-permutation.js similarity index 100% rename from 31-next-permutation.js rename to 31.next-permutation.js diff --git a/310-minimum-height-trees.js b/310.minimum-height-trees.js similarity index 100% rename from 310-minimum-height-trees.js rename to 310.minimum-height-trees.js diff --git a/311-sparse-matrix-multiplication.js b/311.sparse-matrix-multiplication.js similarity index 100% rename from 311-sparse-matrix-multiplication.js rename to 311.sparse-matrix-multiplication.js diff --git a/312-burst-balloons.js b/312.burst-balloons.js similarity index 100% rename from 312-burst-balloons.js rename to 312.burst-balloons.js diff --git a/313-super-ugly-number.js b/313.super-ugly-number.js similarity index 100% rename from 313-super-ugly-number.js rename to 313.super-ugly-number.js diff --git a/314-binary-tree-vertical-order-traversal.js b/314.binary-tree-vertical-order-traversal.js similarity index 100% rename from 314-binary-tree-vertical-order-traversal.js rename to 314.binary-tree-vertical-order-traversal.js diff --git a/315-count-of-smaller-numbers-after-self.js b/315.count-of-smaller-numbers-after-self.js similarity index 100% rename from 315-count-of-smaller-numbers-after-self.js rename to 315.count-of-smaller-numbers-after-self.js diff --git a/316-remove-duplicate-letters.js b/316.remove-duplicate-letters.js similarity index 100% rename from 316-remove-duplicate-letters.js rename to 316.remove-duplicate-letters.js diff --git a/317-shortest-distance-from-all-buildings.js b/317.shortest-distance-from-all-buildings.js similarity index 100% rename from 317-shortest-distance-from-all-buildings.js rename to 317.shortest-distance-from-all-buildings.js diff --git a/318-maximum-product-of-word-lengths.js b/318.maximum-product-of-word-lengths.js old mode 100755 new mode 100644 similarity index 96% rename from 318-maximum-product-of-word-lengths.js rename to 318.maximum-product-of-word-lengths.js index 7eb8b017..f892c957 --- a/318-maximum-product-of-word-lengths.js +++ b/318.maximum-product-of-word-lengths.js @@ -1,26 +1,26 @@ -/** - * @param {string[]} words - * @return {number} - */ -const maxProduct = function(words) { - if (words == null || words.length === 0) return 0; - let len = words.length; - let value = []; - for (let i = 0; i < len; i++) { - let tmp = words[i]; - value[i] = 0; - for (let j = 0; j < tmp.length; j++) { - value[i] |= 1 << (tmp.charAt(j).charCodeAt(0) - "a".charCodeAt(0)); - } - } - let maxProductNum = 0; - for (let i = 0; i < len; i++) - for (let j = i + 1; j < len; j++) { - if ( - (value[i] & value[j]) === 0 && - words[i].length * words[j].length > maxProductNum - ) - maxProductNum = words[i].length * words[j].length; - } - return maxProductNum; -}; +/** + * @param {string[]} words + * @return {number} + */ +const maxProduct = function(words) { + if (words == null || words.length === 0) return 0; + let len = words.length; + let value = []; + for (let i = 0; i < len; i++) { + let tmp = words[i]; + value[i] = 0; + for (let j = 0; j < tmp.length; j++) { + value[i] |= 1 << (tmp.charAt(j).charCodeAt(0) - "a".charCodeAt(0)); + } + } + let maxProductNum = 0; + for (let i = 0; i < len; i++) + for (let j = i + 1; j < len; j++) { + if ( + (value[i] & value[j]) === 0 && + words[i].length * words[j].length > maxProductNum + ) + maxProductNum = words[i].length * words[j].length; + } + return maxProductNum; +}; diff --git a/319-bulb-switcher.js b/319.bulb-switcher.js old mode 100755 new mode 100644 similarity index 94% rename from 319-bulb-switcher.js rename to 319.bulb-switcher.js index ad48c1a5..d66cf4b2 --- a/319-bulb-switcher.js +++ b/319.bulb-switcher.js @@ -1,7 +1,7 @@ -/** - * @param {number} n - * @return {number} - */ -const bulbSwitch = function(n) { - return Math.floor(Math.sqrt(n)); -}; +/** + * @param {number} n + * @return {number} + */ +const bulbSwitch = function(n) { + return Math.floor(Math.sqrt(n)); +}; diff --git a/32-longest-valid-parentheses.js b/32.longest-valid-parentheses.js similarity index 100% rename from 32-longest-valid-parentheses.js rename to 32.longest-valid-parentheses.js diff --git a/320-generalized-abbreviation.js b/320.generalized-abbreviation.js similarity index 100% rename from 320-generalized-abbreviation.js rename to 320.generalized-abbreviation.js diff --git a/321-create-maximum-number.js b/321.create-maximum-number.js similarity index 100% rename from 321-create-maximum-number.js rename to 321.create-maximum-number.js diff --git a/322-coin-change.js b/322.coin-change.js similarity index 100% rename from 322-coin-change.js rename to 322.coin-change.js diff --git a/322-reconstruct-itinerary.js b/322.reconstruct-itinerary.js similarity index 100% rename from 322-reconstruct-itinerary.js rename to 322.reconstruct-itinerary.js diff --git a/323-number-of-connected-components-in-an-undirected-graph.js b/323.number-of-connected-components-in-an-undirected-graph.js similarity index 100% rename from 323-number-of-connected-components-in-an-undirected-graph.js rename to 323.number-of-connected-components-in-an-undirected-graph.js diff --git a/324-wiggle-sort-ii.js b/324.wiggle-sort-ii.js similarity index 100% rename from 324-wiggle-sort-ii.js rename to 324.wiggle-sort-ii.js diff --git a/325-maximum-size-subarray-sum-equals-k.js b/325.maximum-size-subarray-sum-equals-k.js similarity index 100% rename from 325-maximum-size-subarray-sum-equals-k.js rename to 325.maximum-size-subarray-sum-equals-k.js diff --git a/326-power-of-three.js b/326.power-of-three.js similarity index 100% rename from 326-power-of-three.js rename to 326.power-of-three.js diff --git a/327-count-of-range-sum.js b/327.count-of-range-sum.js similarity index 100% rename from 327-count-of-range-sum.js rename to 327.count-of-range-sum.js diff --git a/328-odd-even-linked-list.js b/328.odd-even-linked-list.js old mode 100755 new mode 100644 similarity index 95% rename from 328-odd-even-linked-list.js rename to 328.odd-even-linked-list.js index af1a50f2..454cd221 --- a/328-odd-even-linked-list.js +++ b/328.odd-even-linked-list.js @@ -1,26 +1,26 @@ -/** - * Definition for singly-linked list. - * function ListNode(val) { - * this.val = val; - * this.next = null; - * } - */ -/** - * @param {ListNode} head - * @return {ListNode} - */ -const oddEvenList = function(head) { - if (head === null) return null; - let odd = head, - even = head.next, - evenHead = even; - while (even !== null && even.next !== null) { - odd.next = even.next; - odd = odd.next; - even.next = odd.next; - even = even.next; - } - odd.next = evenHead; - return head; -}; - +/** + * Definition for singly-linked list. + * function ListNode(val) { + * this.val = val; + * this.next = null; + * } + */ +/** + * @param {ListNode} head + * @return {ListNode} + */ +const oddEvenList = function(head) { + if (head === null) return null; + let odd = head, + even = head.next, + evenHead = even; + while (even !== null && even.next !== null) { + odd.next = even.next; + odd = odd.next; + even.next = odd.next; + even = even.next; + } + odd.next = evenHead; + return head; +}; + diff --git a/329-longest-increasing-path-in-a-matrix.js b/329.longest-increasing-path-in-a-matrix.js similarity index 100% rename from 329-longest-increasing-path-in-a-matrix.js rename to 329.longest-increasing-path-in-a-matrix.js diff --git a/33-search-in-rotated-sorted-array.js b/33.search-in-rotated-sorted-array.js similarity index 100% rename from 33-search-in-rotated-sorted-array.js rename to 33.search-in-rotated-sorted-array.js diff --git a/330-patching-array.js b/330.patching-array.js similarity index 100% rename from 330-patching-array.js rename to 330.patching-array.js diff --git a/331-verify-preorder-serialization-of-a-binary-tree.js b/331.verify-preorder-serialization-of-a-binary-tree.js similarity index 100% rename from 331-verify-preorder-serialization-of-a-binary-tree.js rename to 331.verify-preorder-serialization-of-a-binary-tree.js diff --git a/333-largest-bst-subtree.js b/333.largest-bst-subtree.js similarity index 100% rename from 333-largest-bst-subtree.js rename to 333.largest-bst-subtree.js diff --git a/334-increasing-triplet-subsequence.js b/334.increasing-triplet-subsequence.js similarity index 100% rename from 334-increasing-triplet-subsequence.js rename to 334.increasing-triplet-subsequence.js diff --git a/335-self-crossing.js b/335.self-crossing.js similarity index 100% rename from 335-self-crossing.js rename to 335.self-crossing.js diff --git a/336-palindrome-pairs.js b/336.palindrome-pairs.js similarity index 100% rename from 336-palindrome-pairs.js rename to 336.palindrome-pairs.js diff --git a/337-house-robber-iii.js b/337.house-robber-iii.js similarity index 100% rename from 337-house-robber-iii.js rename to 337.house-robber-iii.js diff --git a/338-counting-bits.js b/338.counting-bits.js similarity index 100% rename from 338-counting-bits.js rename to 338.counting-bits.js diff --git a/339-nested-list-weight-sum.js b/339.nested-list-weight-sum.js similarity index 100% rename from 339-nested-list-weight-sum.js rename to 339.nested-list-weight-sum.js diff --git a/34-find-first-and-last-position-of-element-in-sorted-array.js b/34.find-first-and-last-position-of-element-in-sorted-array.js similarity index 100% rename from 34-find-first-and-last-position-of-element-in-sorted-array.js rename to 34.find-first-and-last-position-of-element-in-sorted-array.js diff --git a/340-longest-substring-with-at-most-k-distinct-characters.js b/340.longest-substring-with-at-most-k-distinct-characters.js similarity index 100% rename from 340-longest-substring-with-at-most-k-distinct-characters.js rename to 340.longest-substring-with-at-most-k-distinct-characters.js diff --git a/341-flatten-nested-list-iterator.js b/341.flatten-nested-list-iterator.js similarity index 100% rename from 341-flatten-nested-list-iterator.js rename to 341.flatten-nested-list-iterator.js diff --git a/342-power-of-four.js b/342.power-of-four.js similarity index 100% rename from 342-power-of-four.js rename to 342.power-of-four.js diff --git a/343-integer-break.js b/343.integer-break.js old mode 100755 new mode 100644 similarity index 95% rename from 343-integer-break.js rename to 343.integer-break.js index 4b9acb47..a9f7ab3c --- a/343-integer-break.js +++ b/343.integer-break.js @@ -1,15 +1,15 @@ -/** - * @param {number} n - * @return {number} - */ -const integerBreak = function(n) { - const dp = Array(n + 1).fill(0) - dp[2] = 1 - for(let i = 3; i <= n; i++) { - for(let j = 1; j < i; j++) { - dp[i] = Math.max(dp[i], j * Math.max(i - j, dp[i - j])) - } - } - return dp[n] -}; - +/** + * @param {number} n + * @return {number} + */ +const integerBreak = function(n) { + const dp = Array(n + 1).fill(0) + dp[2] = 1 + for(let i = 3; i <= n; i++) { + for(let j = 1; j < i; j++) { + dp[i] = Math.max(dp[i], j * Math.max(i - j, dp[i - j])) + } + } + return dp[n] +}; + diff --git a/344-reverse-string.js b/344.reverse-string.js similarity index 100% rename from 344-reverse-string.js rename to 344.reverse-string.js diff --git a/345-reverse-vowels-of-a-string.js b/345.reverse-vowels-of-a-string.js similarity index 100% rename from 345-reverse-vowels-of-a-string.js rename to 345.reverse-vowels-of-a-string.js diff --git a/346-moving-average-from-data-stream.js b/346.moving-average-from-data-stream.js similarity index 100% rename from 346-moving-average-from-data-stream.js rename to 346.moving-average-from-data-stream.js diff --git a/347-top-k-frequent-elements.js b/347.top-k-frequent-elements.js old mode 100755 new mode 100644 similarity index 95% rename from 347-top-k-frequent-elements.js rename to 347.top-k-frequent-elements.js index c5f2c1f5..23a83308 --- a/347-top-k-frequent-elements.js +++ b/347.top-k-frequent-elements.js @@ -1,33 +1,33 @@ -/** - * @param {number[]} nums - * @param {number} k - * @return {number[]} - */ -const topKFrequent = function(nums, k) { - const hash = {} - for(let i = 0; i < nums.length; i++) { - if(hash.hasOwnProperty(nums[i])) hash[nums[i]]++ - else hash[nums[i]] = 1 - } - const res = new Array() - const keys = Object.keys(hash) - - const bucket = new Array(nums.length) - - for(let k of keys) { - let freq = hash[k] - if(bucket[freq] == null) { - bucket[freq] = [] - } - bucket[freq].push(k) - } - - for(let i = bucket.length - 1; i >= 0 && res.length < k; i--) { - if(bucket[i] != null) { - res.push(...bucket[i]) - } - } - - return res -}; - +/** + * @param {number[]} nums + * @param {number} k + * @return {number[]} + */ +const topKFrequent = function(nums, k) { + const hash = {} + for(let i = 0; i < nums.length; i++) { + if(hash.hasOwnProperty(nums[i])) hash[nums[i]]++ + else hash[nums[i]] = 1 + } + const res = new Array() + const keys = Object.keys(hash) + + const bucket = new Array(nums.length) + + for(let k of keys) { + let freq = hash[k] + if(bucket[freq] == null) { + bucket[freq] = [] + } + bucket[freq].push(k) + } + + for(let i = bucket.length - 1; i >= 0 && res.length < k; i--) { + if(bucket[i] != null) { + res.push(...bucket[i]) + } + } + + return res +}; + diff --git a/348-design-tic-tac-toe.js b/348.design-tic-tac-toe.js similarity index 100% rename from 348-design-tic-tac-toe.js rename to 348.design-tic-tac-toe.js diff --git a/349-intersection-of-two-arrays.js b/349.intersection-of-two-arrays.js old mode 100755 new mode 100644 similarity index 94% rename from 349-intersection-of-two-arrays.js rename to 349.intersection-of-two-arrays.js index 6838bdb7..ca91e462 --- a/349-intersection-of-two-arrays.js +++ b/349.intersection-of-two-arrays.js @@ -1,18 +1,18 @@ -/** - * @param {number[]} nums1 - * @param {number[]} nums2 - * @return {number[]} - */ - -const intersection = function(nums1, nums2) { - const obj = {}; - nums1.forEach(i => (obj[i] = true)); - - return nums2.filter(j => { - if (obj[j]) { - delete obj[j]; - return true; - } - return false; - }); -}; +/** + * @param {number[]} nums1 + * @param {number[]} nums2 + * @return {number[]} + */ + +const intersection = function(nums1, nums2) { + const obj = {}; + nums1.forEach(i => (obj[i] = true)); + + return nums2.filter(j => { + if (obj[j]) { + delete obj[j]; + return true; + } + return false; + }); +}; diff --git a/35-search-insert-position.js b/35.search-insert-position.js old mode 100755 new mode 100644 similarity index 94% rename from 35-search-insert-position.js rename to 35.search-insert-position.js index 1422a85a..d4c63df3 --- a/35-search-insert-position.js +++ b/35.search-insert-position.js @@ -1,17 +1,17 @@ -/** - * @param {number[]} nums - * @param {number} target - * @return {number} - */ -const searchInsert = function(nums, target) { - for (let i = 0; i < nums.length; i++) { - if (nums[i] >= target) { - return i; - } else { - if (i === nums.length - 1) { - return i + 1; - } - } - } -}; - +/** + * @param {number[]} nums + * @param {number} target + * @return {number} + */ +const searchInsert = function(nums, target) { + for (let i = 0; i < nums.length; i++) { + if (nums[i] >= target) { + return i; + } else { + if (i === nums.length - 1) { + return i + 1; + } + } + } +}; + diff --git a/350-intersection-of-two-arrays-ii.js b/350.intersection-of-two-arrays-ii.js similarity index 100% rename from 350-intersection-of-two-arrays-ii.js rename to 350.intersection-of-two-arrays-ii.js diff --git a/351-android-unlock-patterns.js b/351.android-unlock-patterns.js similarity index 100% rename from 351-android-unlock-patterns.js rename to 351.android-unlock-patterns.js diff --git a/352-data-stream-as-disjoint-intervals.js b/352.data-stream-as-disjoint-intervals.js similarity index 100% rename from 352-data-stream-as-disjoint-intervals.js rename to 352.data-stream-as-disjoint-intervals.js diff --git a/353-design-snake-game.js b/353.design-snake-game.js similarity index 100% rename from 353-design-snake-game.js rename to 353.design-snake-game.js diff --git a/354-russian-doll-envelopes.js b/354.russian-doll-envelopes.js similarity index 100% rename from 354-russian-doll-envelopes.js rename to 354.russian-doll-envelopes.js diff --git a/355-design-twitter.js b/355.design-twitter.js similarity index 100% rename from 355-design-twitter.js rename to 355.design-twitter.js diff --git a/356-line-reflection.js b/356.line-reflection.js similarity index 100% rename from 356-line-reflection.js rename to 356.line-reflection.js diff --git a/357-count-numbers-with-unique-digits.js b/357.count-numbers-with-unique-digits.js old mode 100755 new mode 100644 similarity index 94% rename from 357-count-numbers-with-unique-digits.js rename to 357.count-numbers-with-unique-digits.js index e5cd8c27..4e2a9576 --- a/357-count-numbers-with-unique-digits.js +++ b/357.count-numbers-with-unique-digits.js @@ -1,18 +1,18 @@ -/** - * @param {number} n - * @return {number} - */ -const countNumbersWithUniqueDigits = function(n) { - if(n === 0) return 1 - let res = 10 - let uniqueDigits = 9, avail = 9 - while(n > 1 && avail) { - uniqueDigits = uniqueDigits * avail - res += uniqueDigits - avail-- - n-- - } - - return res -}; - +/** + * @param {number} n + * @return {number} + */ +const countNumbersWithUniqueDigits = function(n) { + if(n === 0) return 1 + let res = 10 + let uniqueDigits = 9, avail = 9 + while(n > 1 && avail) { + uniqueDigits = uniqueDigits * avail + res += uniqueDigits + avail-- + n-- + } + + return res +}; + diff --git a/358-rearrange-string-k-distance-apart.js b/358.rearrange-string-k-distance-apart.js similarity index 100% rename from 358-rearrange-string-k-distance-apart.js rename to 358.rearrange-string-k-distance-apart.js diff --git a/359-logger-rate-limiter.js b/359.logger-rate-limiter.js similarity index 100% rename from 359-logger-rate-limiter.js rename to 359.logger-rate-limiter.js diff --git a/36-valid-sudoku.js b/36.valid-sudoku.js similarity index 100% rename from 36-valid-sudoku.js rename to 36.valid-sudoku.js diff --git a/360-sort-transformed-array.js b/360.sort-transformed-array.js similarity index 100% rename from 360-sort-transformed-array.js rename to 360.sort-transformed-array.js diff --git a/361-bomb-enemy.js b/361.bomb-enemy.js similarity index 100% rename from 361-bomb-enemy.js rename to 361.bomb-enemy.js diff --git a/362-design-hit-counter.js b/362.design-hit-counter.js similarity index 100% rename from 362-design-hit-counter.js rename to 362.design-hit-counter.js diff --git a/363-max-sum-of-rectangle-no-larger-than-k.js b/363.max-sum-of-rectangle-no-larger-than-k.js similarity index 100% rename from 363-max-sum-of-rectangle-no-larger-than-k.js rename to 363.max-sum-of-rectangle-no-larger-than-k.js diff --git a/364-nested-list-weight-sum-ii.js b/364.nested-list-weight-sum-ii.js similarity index 100% rename from 364-nested-list-weight-sum-ii.js rename to 364.nested-list-weight-sum-ii.js diff --git a/365-water-and-jug-problem.js b/365.water-and-jug-problem.js similarity index 100% rename from 365-water-and-jug-problem.js rename to 365.water-and-jug-problem.js diff --git a/366-find-leaves-of-binary-tree.js b/366.find-leaves-of-binary-tree.js similarity index 100% rename from 366-find-leaves-of-binary-tree.js rename to 366.find-leaves-of-binary-tree.js diff --git a/367-valid-perfect-square.js b/367.valid-perfect-square.js similarity index 100% rename from 367-valid-perfect-square.js rename to 367.valid-perfect-square.js diff --git a/368-largest-divisible-subset.js b/368.largest-divisible-subset.js similarity index 100% rename from 368-largest-divisible-subset.js rename to 368.largest-divisible-subset.js diff --git a/369-plus-one-linked-list.js b/369.plus-one-linked-list.js similarity index 100% rename from 369-plus-one-linked-list.js rename to 369.plus-one-linked-list.js diff --git a/37-sudoku-solver.js b/37.sudoku-solver.js similarity index 100% rename from 37-sudoku-solver.js rename to 37.sudoku-solver.js diff --git a/370-range-addition.js b/370.range-addition.js similarity index 100% rename from 370-range-addition.js rename to 370.range-addition.js diff --git a/371-sum-of-two-integers.js b/371.sum-of-two-integers.js old mode 100755 new mode 100644 similarity index 95% rename from 371-sum-of-two-integers.js rename to 371.sum-of-two-integers.js index cf7edf8e..f0a1ce08 --- a/371-sum-of-two-integers.js +++ b/371.sum-of-two-integers.js @@ -1,8 +1,8 @@ -/** - * @param {number} a - * @param {number} b - * @return {number} - */ -const getSum = function(a, b) { - return b === 0 ? a : getSum(a ^ b, (a & b) << 1); -}; +/** + * @param {number} a + * @param {number} b + * @return {number} + */ +const getSum = function(a, b) { + return b === 0 ? a : getSum(a ^ b, (a & b) << 1); +}; diff --git a/372-super-pow.js b/372.super-pow.js similarity index 100% rename from 372-super-pow.js rename to 372.super-pow.js diff --git a/373-find-k-pairs-with-smallest-sums.js b/373.find-k-pairs-with-smallest-sums.js similarity index 100% rename from 373-find-k-pairs-with-smallest-sums.js rename to 373.find-k-pairs-with-smallest-sums.js diff --git a/375-guess-number-higher-or-lower-ii.js b/375.guess-number-higher-or-lower-ii.js similarity index 100% rename from 375-guess-number-higher-or-lower-ii.js rename to 375.guess-number-higher-or-lower-ii.js diff --git a/376-wiggle-subsequence.js b/376.wiggle-subsequence.js similarity index 100% rename from 376-wiggle-subsequence.js rename to 376.wiggle-subsequence.js diff --git a/377-combination-sum-IV.js b/377.combination-sum-IV.js old mode 100755 new mode 100644 similarity index 95% rename from 377-combination-sum-IV.js rename to 377.combination-sum-IV.js index 8a8e55be..58b402b1 --- a/377-combination-sum-IV.js +++ b/377.combination-sum-IV.js @@ -1,17 +1,17 @@ -/** - * @param {number[]} nums - * @param {number} target - * @return {number} - */ -const combinationSum4 = function(nums, target) { - const comb = [1]; - for (let i = 1; i <= target; i++) { - comb[i] || (comb[i] = 0); - for (let j = 0; j < nums.length; j++) { - if (i >= nums[j]) { - comb[i] += comb[i - nums[j]]; - } - } - } - return comb[target]; -}; +/** + * @param {number[]} nums + * @param {number} target + * @return {number} + */ +const combinationSum4 = function(nums, target) { + const comb = [1]; + for (let i = 1; i <= target; i++) { + comb[i] || (comb[i] = 0); + for (let j = 0; j < nums.length; j++) { + if (i >= nums[j]) { + comb[i] += comb[i - nums[j]]; + } + } + } + return comb[target]; +}; diff --git a/378-kth-smallest-element-in-a-sorted-matrix.js b/378.kth-smallest-element-in-a-sorted-matrix.js old mode 100755 new mode 100644 similarity index 96% rename from 378-kth-smallest-element-in-a-sorted-matrix.js rename to 378.kth-smallest-element-in-a-sorted-matrix.js index a955b4e8..406a9c6e --- a/378-kth-smallest-element-in-a-sorted-matrix.js +++ b/378.kth-smallest-element-in-a-sorted-matrix.js @@ -1,26 +1,26 @@ -/** - * @param {number[][]} matrix - * @param {number} k - * @return {number} - */ -const kthSmallest = function(matrix, k) { - let lo = matrix[0][0], - hi = matrix[matrix.length - 1][matrix[0].length - 1] + 1; //[lo, hi) - while (lo < hi) { - let mid = Math.floor(lo + (hi - lo) / 2); - let count = 0, - j = matrix[0].length - 1; - for (let i = 0; i < matrix.length; i++) { - while (j >= 0 && matrix[i][j] > mid) j--; - count += j + 1; - } - if (count < k) lo = mid + 1; - else hi = mid; - } - return lo; -}; - -console.log(kthSmallest([[-5]], 1)); -console.log(kthSmallest([[1, 2], [1, 3]], 4)); -console.log(kthSmallest([[1, 5, 9], [10, 11, 13], [12, 13, 15]], 8)); -console.log(kthSmallest([[1, 2], [1, 3]], 2)); +/** + * @param {number[][]} matrix + * @param {number} k + * @return {number} + */ +const kthSmallest = function(matrix, k) { + let lo = matrix[0][0], + hi = matrix[matrix.length - 1][matrix[0].length - 1] + 1; //[lo, hi) + while (lo < hi) { + let mid = Math.floor(lo + (hi - lo) / 2); + let count = 0, + j = matrix[0].length - 1; + for (let i = 0; i < matrix.length; i++) { + while (j >= 0 && matrix[i][j] > mid) j--; + count += j + 1; + } + if (count < k) lo = mid + 1; + else hi = mid; + } + return lo; +}; + +console.log(kthSmallest([[-5]], 1)); +console.log(kthSmallest([[1, 2], [1, 3]], 4)); +console.log(kthSmallest([[1, 5, 9], [10, 11, 13], [12, 13, 15]], 8)); +console.log(kthSmallest([[1, 2], [1, 3]], 2)); diff --git a/379-design-phone-directory.js b/379.design-phone-directory.js similarity index 100% rename from 379-design-phone-directory.js rename to 379.design-phone-directory.js diff --git a/38-count-and-say.js b/38.count-and-say.js similarity index 100% rename from 38-count-and-say.js rename to 38.count-and-say.js diff --git a/380-insert-delete-getrandom-o1.js b/380.insert-delete-getrandom-o1.js similarity index 100% rename from 380-insert-delete-getrandom-o1.js rename to 380.insert-delete-getrandom-o1.js diff --git a/381-insert-delete-getrandom-o1-duplicates-allowed.js b/381.insert-delete-getrandom-o1-duplicates-allowed.js similarity index 100% rename from 381-insert-delete-getrandom-o1-duplicates-allowed.js rename to 381.insert-delete-getrandom-o1-duplicates-allowed.js diff --git a/382-linked-list-random-node.js b/382.linked-list-random-node.js old mode 100755 new mode 100644 similarity index 95% rename from 382-linked-list-random-node.js rename to 382.linked-list-random-node.js index 4c9a37a7..026302cb --- a/382-linked-list-random-node.js +++ b/382.linked-list-random-node.js @@ -1,37 +1,37 @@ -/** - * Definition for singly-linked list. - * function ListNode(val) { - * this.val = val; - * this.next = null; - * } - */ -/** - * @param head The linked list's head. - Note that the head is guaranteed to be not null, so it contains at least one node. - * @param {ListNode} head - */ -const Solution = function(head) { - this.list = head; - this.arr = []; - loop(head, this.arr); -}; - -/** - * Returns a random node's value. - * @return {number} - */ -Solution.prototype.getRandom = function() { - const len = this.arr.length; - return this.arr[Math.floor(Math.random() * len)].val; -}; - -/** - * Your Solution object will be instantiated and called as such: - * var obj = Object.create(Solution).createNew(head) - * var param_1 = obj.getRandom() - */ -function loop(node, arr) { - if (node == null) return; - arr.push(node); - loop(node.next, arr); -} +/** + * Definition for singly-linked list. + * function ListNode(val) { + * this.val = val; + * this.next = null; + * } + */ +/** + * @param head The linked list's head. + Note that the head is guaranteed to be not null, so it contains at least one node. + * @param {ListNode} head + */ +const Solution = function(head) { + this.list = head; + this.arr = []; + loop(head, this.arr); +}; + +/** + * Returns a random node's value. + * @return {number} + */ +Solution.prototype.getRandom = function() { + const len = this.arr.length; + return this.arr[Math.floor(Math.random() * len)].val; +}; + +/** + * Your Solution object will be instantiated and called as such: + * var obj = Object.create(Solution).createNew(head) + * var param_1 = obj.getRandom() + */ +function loop(node, arr) { + if (node == null) return; + arr.push(node); + loop(node.next, arr); +} diff --git a/383-ransom-note.js b/383.ransom-note.js old mode 100755 new mode 100644 similarity index 95% rename from 383-ransom-note.js rename to 383.ransom-note.js index 81ad0d58..b8797404 --- a/383-ransom-note.js +++ b/383.ransom-note.js @@ -1,16 +1,16 @@ -/** - * @param {string} ransomNote - * @param {string} magazine - * @return {boolean} - */ -const canConstruct = function(ransomNote, magazine) { - const m = new Map() - for(let c of magazine) { - m.set(c, (m.get(c) || 0) + 1 ) - } - for(let c of ransomNote) { - if(!m.has(c) || m.get(c) <= 0) return false - m.set(c, m.get(c) - 1) - } - return true -}; +/** + * @param {string} ransomNote + * @param {string} magazine + * @return {boolean} + */ +const canConstruct = function(ransomNote, magazine) { + const m = new Map() + for(let c of magazine) { + m.set(c, (m.get(c) || 0) + 1 ) + } + for(let c of ransomNote) { + if(!m.has(c) || m.get(c) <= 0) return false + m.set(c, m.get(c) - 1) + } + return true +}; diff --git a/384-shuffle-an-array.js b/384.shuffle-an-array.js old mode 100755 new mode 100644 similarity index 95% rename from 384-shuffle-an-array.js rename to 384.shuffle-an-array.js index 7d86838b..166c1283 --- a/384-shuffle-an-array.js +++ b/384.shuffle-an-array.js @@ -1,39 +1,39 @@ -/** - * @param {number[]} nums - */ -const Solution = function(nums) { - this.original = nums; -}; -/** - * Resets the array to its original configuration and return it. - * @return {number[]} - */ -Solution.prototype.reset = function() { - return this.original; -}; - -/** - * Returns a random shuffling of the array. - * @return {number[]} - */ -Solution.prototype.shuffle = function() { - const res = []; - const len = this.original.length; - let idx = 0; - let i = 0; - while (idx <= len - 1) { - i = Math.floor(Math.random() * len); - if (res[i] == null) { - res[i] = this.original[idx]; - idx += 1; - } - } - return res; -}; - -/** - * Your Solution object will be instantiated and called as such: - * var obj = Object.create(Solution).createNew(nums) - * var param_1 = obj.reset() - * var param_2 = obj.shuffle() - */ +/** + * @param {number[]} nums + */ +const Solution = function(nums) { + this.original = nums; +}; +/** + * Resets the array to its original configuration and return it. + * @return {number[]} + */ +Solution.prototype.reset = function() { + return this.original; +}; + +/** + * Returns a random shuffling of the array. + * @return {number[]} + */ +Solution.prototype.shuffle = function() { + const res = []; + const len = this.original.length; + let idx = 0; + let i = 0; + while (idx <= len - 1) { + i = Math.floor(Math.random() * len); + if (res[i] == null) { + res[i] = this.original[idx]; + idx += 1; + } + } + return res; +}; + +/** + * Your Solution object will be instantiated and called as such: + * var obj = Object.create(Solution).createNew(nums) + * var param_1 = obj.reset() + * var param_2 = obj.shuffle() + */ diff --git a/385-mini-parser.js b/385.mini-parser.js similarity index 100% rename from 385-mini-parser.js rename to 385.mini-parser.js diff --git a/386-lexicographical-numbers.js b/386.lexicographical-numbers.js similarity index 100% rename from 386-lexicographical-numbers.js rename to 386.lexicographical-numbers.js diff --git a/387-first-unique-character-in-a-string.js b/387.first-unique-character-in-a-string.js old mode 100755 new mode 100644 similarity index 94% rename from 387-first-unique-character-in-a-string.js rename to 387.first-unique-character-in-a-string.js index e8a159e5..6484632c --- a/387-first-unique-character-in-a-string.js +++ b/387.first-unique-character-in-a-string.js @@ -1,29 +1,29 @@ -/** - * @param {string} s - * @return {number} - */ -const firstUniqChar = function(s) { - const arr = []; - const res = []; - const hash = {}; - let tmp; - let idx; - for (let i = 0; i < s.length; i++) { - tmp = s.charAt(i); - if (hash.hasOwnProperty(tmp)) { - idx = arr.indexOf(tmp); - if (idx >= 0) { - arr.splice(idx, 1); - res.splice(idx, 1); - } - - hash[tmp] += 1; - } else { - arr.push(tmp); - res.push(i); - hash[tmp] = 1; - } - } - return res[0] == null ? -1 : res[0]; -}; - +/** + * @param {string} s + * @return {number} + */ +const firstUniqChar = function(s) { + const arr = []; + const res = []; + const hash = {}; + let tmp; + let idx; + for (let i = 0; i < s.length; i++) { + tmp = s.charAt(i); + if (hash.hasOwnProperty(tmp)) { + idx = arr.indexOf(tmp); + if (idx >= 0) { + arr.splice(idx, 1); + res.splice(idx, 1); + } + + hash[tmp] += 1; + } else { + arr.push(tmp); + res.push(i); + hash[tmp] = 1; + } + } + return res[0] == null ? -1 : res[0]; +}; + diff --git a/388-longest-absolute-file-path.js b/388.longest-absolute-file-path.js similarity index 100% rename from 388-longest-absolute-file-path.js rename to 388.longest-absolute-file-path.js diff --git a/389-find-the-difference.js b/389.find-the-difference.js old mode 100755 new mode 100644 similarity index 96% rename from 389-find-the-difference.js rename to 389.find-the-difference.js index daa12007..8267a699 --- a/389-find-the-difference.js +++ b/389.find-the-difference.js @@ -1,12 +1,12 @@ -/** - * @param {string} s - * @param {string} t - * @return {character} - */ -const findTheDifference = function(s, t) { - let xor = 0 - for(let i = 0, len = s.length; i < len; i++) xor = xor ^ s.charCodeAt(i) ^ t.charCodeAt(i) - xor = xor ^ t.charCodeAt(t.length - 1) - return String.fromCharCode(xor) -}; - +/** + * @param {string} s + * @param {string} t + * @return {character} + */ +const findTheDifference = function(s, t) { + let xor = 0 + for(let i = 0, len = s.length; i < len; i++) xor = xor ^ s.charCodeAt(i) ^ t.charCodeAt(i) + xor = xor ^ t.charCodeAt(t.length - 1) + return String.fromCharCode(xor) +}; + diff --git a/39-combination-sum.js b/39.combination-sum.js old mode 100755 new mode 100644 similarity index 96% rename from 39-combination-sum.js rename to 39.combination-sum.js index 56ecc406..34815ce0 --- a/39-combination-sum.js +++ b/39.combination-sum.js @@ -1,23 +1,23 @@ -/** - * @param {number[]} candidates - * @param {number} target - * @return {number[][]} - */ -const combinationSum = function(candidates, target) { - candidates.sort((a, b) => a - b); - const res = []; - bt(candidates, target, res, [], 0); - return res; -}; - -function bt(candidates, target, res, combination, start) { - if (target === 0) { - res.push(combination.slice(0)); - return; - } - for (let i = start; i < candidates.length && target >= candidates[i]; i++) { - combination.push(candidates[i]); - bt(candidates, target - candidates[i], res, combination, i); - combination.pop(); - } -} +/** + * @param {number[]} candidates + * @param {number} target + * @return {number[][]} + */ +const combinationSum = function(candidates, target) { + candidates.sort((a, b) => a - b); + const res = []; + bt(candidates, target, res, [], 0); + return res; +}; + +function bt(candidates, target, res, combination, start) { + if (target === 0) { + res.push(combination.slice(0)); + return; + } + for (let i = start; i < candidates.length && target >= candidates[i]; i++) { + combination.push(candidates[i]); + bt(candidates, target - candidates[i], res, combination, i); + combination.pop(); + } +} diff --git a/390-elimination-game.js b/390.elimination-game.js similarity index 100% rename from 390-elimination-game.js rename to 390.elimination-game.js diff --git a/391-perfect-rectangle.js b/391.perfect-rectangle.js similarity index 100% rename from 391-perfect-rectangle.js rename to 391.perfect-rectangle.js diff --git a/392-is-subsequence.js b/392.is-subsequence.js similarity index 100% rename from 392-is-subsequence.js rename to 392.is-subsequence.js diff --git a/393-utf-8-validation.js b/393.utf-8-validation.js similarity index 100% rename from 393-utf-8-validation.js rename to 393.utf-8-validation.js diff --git a/394-decode-string.js b/394.decode-string.js old mode 100755 new mode 100644 similarity index 95% rename from 394-decode-string.js rename to 394.decode-string.js index 36a19f99..c3a4618e --- a/394-decode-string.js +++ b/394.decode-string.js @@ -1,42 +1,42 @@ -/** - * @param {string} s - * @return {string} - */ -const decodeString = function(s) { - const repeated = []; - const sbStack = []; - - let mul = 0; - let sb = ""; - for (let i = 0; i < s.length; i++) { - const c = s.charAt(i); - if (isDigit(c)) { - if (mul === 0) sbStack.push(sb); // here is the trick - mul = mul * 10 + +c; - } else if (c === "[") { - repeated.push(mul); - mul = 0; - sb = ""; - } else if (isLetter(c)) { - sb += c; - } else if (c === "]") { - let top = sbStack.pop(); - let r = repeated.pop(); - while (r-- > 0) top += sb; - sb = top; - } - } - - return sb; -}; - -function isDigit(c) { - return c.charCodeAt(0) >= 48 && c.charCodeAt(0) <= 57; -} - -function isLetter(c) { - return ( - (c.charCodeAt(0) >= 65 && c.charCodeAt(0) <= 90) || - (c.charCodeAt(0) >= 97 && c.charCodeAt(0) <= 122) - ); -} +/** + * @param {string} s + * @return {string} + */ +const decodeString = function(s) { + const repeated = []; + const sbStack = []; + + let mul = 0; + let sb = ""; + for (let i = 0; i < s.length; i++) { + const c = s.charAt(i); + if (isDigit(c)) { + if (mul === 0) sbStack.push(sb); // here is the trick + mul = mul * 10 + +c; + } else if (c === "[") { + repeated.push(mul); + mul = 0; + sb = ""; + } else if (isLetter(c)) { + sb += c; + } else if (c === "]") { + let top = sbStack.pop(); + let r = repeated.pop(); + while (r-- > 0) top += sb; + sb = top; + } + } + + return sb; +}; + +function isDigit(c) { + return c.charCodeAt(0) >= 48 && c.charCodeAt(0) <= 57; +} + +function isLetter(c) { + return ( + (c.charCodeAt(0) >= 65 && c.charCodeAt(0) <= 90) || + (c.charCodeAt(0) >= 97 && c.charCodeAt(0) <= 122) + ); +} diff --git a/395-longest-substring-with-at-least-k-repeating-characters.js b/395.longest-substring-with-at-least-k-repeating-characters.js similarity index 100% rename from 395-longest-substring-with-at-least-k-repeating-characters.js rename to 395.longest-substring-with-at-least-k-repeating-characters.js diff --git a/396-rotate-function.js b/396.rotate-function.js similarity index 100% rename from 396-rotate-function.js rename to 396.rotate-function.js diff --git a/397-integer-replacement.js b/397.integer-replacement.js similarity index 100% rename from 397-integer-replacement.js rename to 397.integer-replacement.js diff --git a/398-random-pick-index.js b/398.random-pick-index.js old mode 100755 new mode 100644 similarity index 95% rename from 398-random-pick-index.js rename to 398.random-pick-index.js index 545b9883..fad2b0b5 --- a/398-random-pick-index.js +++ b/398.random-pick-index.js @@ -1,26 +1,26 @@ -/** - * @param {number[]} nums - */ -const Solution = function(nums) { - this.nums = nums; -}; - -/** - * @param {number} target - * @return {number} - */ -Solution.prototype.pick = function(target) { - const res = []; - for (let i = 0; i < this.nums.length; i++) { - if (this.nums[i] === target) { - res.push(i); - } - } - return res[Math.floor(Math.random() * res.length)]; -}; - -/** - * Your Solution object will be instantiated and called as such: - * var obj = Object.create(Solution).createNew(nums) - * var param_1 = obj.pick(target) - */ +/** + * @param {number[]} nums + */ +const Solution = function(nums) { + this.nums = nums; +}; + +/** + * @param {number} target + * @return {number} + */ +Solution.prototype.pick = function(target) { + const res = []; + for (let i = 0; i < this.nums.length; i++) { + if (this.nums[i] === target) { + res.push(i); + } + } + return res[Math.floor(Math.random() * res.length)]; +}; + +/** + * Your Solution object will be instantiated and called as such: + * var obj = Object.create(Solution).createNew(nums) + * var param_1 = obj.pick(target) + */ diff --git a/399-evaluate-division.js b/399.evaluate-division.js old mode 100755 new mode 100644 similarity index 96% rename from 399-evaluate-division.js rename to 399.evaluate-division.js index 391adc93..66972b62 --- a/399-evaluate-division.js +++ b/399.evaluate-division.js @@ -1,40 +1,40 @@ -/** - * @param {string[][]} equations - * @param {number[]} values - * @param {string[][]} queries - * @return {number[]} - */ -const calcEquation = function(equations, values, queries) { - const m = {}; - for (let i = 0; i < values.length; i++) { - if (!m.hasOwnProperty(equations[i][0])) m[equations[i][0]] = {}; - if (!m.hasOwnProperty(equations[i][1])) m[equations[i][1]] = {}; - m[equations[i][0]][equations[i][1]] = values[i]; - m[equations[i][1]][equations[i][0]] = 1 / values[i]; - } - const r = []; - for (let i = 0; i < queries.length; i++) { - r[i] = dfs(queries[i][0], queries[i][1], 1, m, []); - } - return r; -}; - -function dfs(s, t, r, m, seen) { - if (!m.hasOwnProperty(s) || !hsetAdd(seen, s)) return -1; - if (s === t) return r; - let next = m[s]; - for (let c of Object.keys(next)) { - let result = dfs(c, t, r * next[c], m, seen); - if (result !== -1) return result; - } - return -1; -} - -function hsetAdd(arr, el) { - if (arr.indexOf(el) === -1) { - arr.push(el); - return true; - } else { - return false; - } -} +/** + * @param {string[][]} equations + * @param {number[]} values + * @param {string[][]} queries + * @return {number[]} + */ +const calcEquation = function(equations, values, queries) { + const m = {}; + for (let i = 0; i < values.length; i++) { + if (!m.hasOwnProperty(equations[i][0])) m[equations[i][0]] = {}; + if (!m.hasOwnProperty(equations[i][1])) m[equations[i][1]] = {}; + m[equations[i][0]][equations[i][1]] = values[i]; + m[equations[i][1]][equations[i][0]] = 1 / values[i]; + } + const r = []; + for (let i = 0; i < queries.length; i++) { + r[i] = dfs(queries[i][0], queries[i][1], 1, m, []); + } + return r; +}; + +function dfs(s, t, r, m, seen) { + if (!m.hasOwnProperty(s) || !hsetAdd(seen, s)) return -1; + if (s === t) return r; + let next = m[s]; + for (let c of Object.keys(next)) { + let result = dfs(c, t, r * next[c], m, seen); + if (result !== -1) return result; + } + return -1; +} + +function hsetAdd(arr, el) { + if (arr.indexOf(el) === -1) { + arr.push(el); + return true; + } else { + return false; + } +} diff --git a/4-median-of-two-sorted-arrays.js b/4.median-of-two-sorted-arrays.js old mode 100755 new mode 100644 similarity index 96% rename from 4-median-of-two-sorted-arrays.js rename to 4.median-of-two-sorted-arrays.js index be8ca9a2..c025d3ff --- a/4-median-of-two-sorted-arrays.js +++ b/4.median-of-two-sorted-arrays.js @@ -1,35 +1,35 @@ -/** - * @param {number[]} nums1 - * @param {number[]} nums2 - * @return {number} - */ -const findMedianSortedArrays = function(nums1, nums2) { - if(nums1.length > nums2.length) return findMedianSortedArrays(nums2, nums1) - const m = nums1.length, n = nums2.length - let low = 0, high = m - while(low <= high) { - - const px = Math.floor((low + high) / 2) - const py = Math.floor(( m + n + 1 ) / 2) - px - - const maxLeft1 = px === 0 ? -Infinity : nums1[px - 1] - const minRight1 = px === m ? Infinity : nums1[px] - - const maxLeft2 = py === 0 ? -Infinity : nums2[py - 1] - const minRight2 = py === n ? Infinity : nums2[py] - - if(maxLeft1 <= minRight2 && maxLeft2 <= minRight1) { - if((m + n) % 2 === 0) { - return (Math.max(maxLeft1, maxLeft2) + Math.min(minRight1, minRight2)) / 2 - } else { - return Math.max(maxLeft1, maxLeft2) - } - } else if(maxLeft1 > minRight2) { - high = px - 1 - } else { - low = px + 1 - } - - } -}; - +/** + * @param {number[]} nums1 + * @param {number[]} nums2 + * @return {number} + */ +const findMedianSortedArrays = function(nums1, nums2) { + if(nums1.length > nums2.length) return findMedianSortedArrays(nums2, nums1) + const m = nums1.length, n = nums2.length + let low = 0, high = m + while(low <= high) { + + const px = Math.floor((low + high) / 2) + const py = Math.floor(( m + n + 1 ) / 2) - px + + const maxLeft1 = px === 0 ? -Infinity : nums1[px - 1] + const minRight1 = px === m ? Infinity : nums1[px] + + const maxLeft2 = py === 0 ? -Infinity : nums2[py - 1] + const minRight2 = py === n ? Infinity : nums2[py] + + if(maxLeft1 <= minRight2 && maxLeft2 <= minRight1) { + if((m + n) % 2 === 0) { + return (Math.max(maxLeft1, maxLeft2) + Math.min(minRight1, minRight2)) / 2 + } else { + return Math.max(maxLeft1, maxLeft2) + } + } else if(maxLeft1 > minRight2) { + high = px - 1 + } else { + low = px + 1 + } + + } +}; + diff --git a/40-combination-sum-II.js b/40.combination-sum-II.js old mode 100755 new mode 100644 similarity index 96% rename from 40-combination-sum-II.js rename to 40.combination-sum-II.js index 4cea637b..53bfabf0 --- a/40-combination-sum-II.js +++ b/40.combination-sum-II.js @@ -1,24 +1,24 @@ -/** - * @param {number[]} candidates - * @param {number} target - * @return {number[][]} - */ -const combinationSum2 = function(candidates, target) { - candidates.sort((a, b) => a - b); - const res = []; - bt(candidates, target, res, [], 0); - return res; -}; - -function bt(candidates, target, res, combination, start) { - if (target === 0) { - res.push(combination.slice(0)); - return; - } - for (let i = start; i < candidates.length && target >= candidates[i]; i++) { - if (i > 0 && candidates[i] === candidates[i - 1] && i > start) continue; - combination.push(candidates[i]); - bt(candidates, target - candidates[i], res, combination, i + 1); - combination.pop(); - } -} +/** + * @param {number[]} candidates + * @param {number} target + * @return {number[][]} + */ +const combinationSum2 = function(candidates, target) { + candidates.sort((a, b) => a - b); + const res = []; + bt(candidates, target, res, [], 0); + return res; +}; + +function bt(candidates, target, res, combination, start) { + if (target === 0) { + res.push(combination.slice(0)); + return; + } + for (let i = start; i < candidates.length && target >= candidates[i]; i++) { + if (i > 0 && candidates[i] === candidates[i - 1] && i > start) continue; + combination.push(candidates[i]); + bt(candidates, target - candidates[i], res, combination, i + 1); + combination.pop(); + } +} diff --git a/400-nth-digit.js b/400.nth-digit.js similarity index 100% rename from 400-nth-digit.js rename to 400.nth-digit.js diff --git a/401-binary-watch.js b/401.binary-watch.js similarity index 100% rename from 401-binary-watch.js rename to 401.binary-watch.js diff --git a/402-remove-k-digits.js b/402.remove-k-digits.js similarity index 100% rename from 402-remove-k-digits.js rename to 402.remove-k-digits.js diff --git a/403-frog-jump.js b/403.frog-jump.js similarity index 100% rename from 403-frog-jump.js rename to 403.frog-jump.js diff --git a/404-sum-of-left-leaves.js b/404.sum-of-left-leaves.js similarity index 100% rename from 404-sum-of-left-leaves.js rename to 404.sum-of-left-leaves.js diff --git a/405-convert-a-number-to-hexadecimal.js b/405.convert-a-number-to-hexadecimal.js similarity index 100% rename from 405-convert-a-number-to-hexadecimal.js rename to 405.convert-a-number-to-hexadecimal.js diff --git a/406-queue-reconstruction-by-height.js b/406.queue-reconstruction-by-height.js old mode 100755 new mode 100644 similarity index 100% rename from 406-queue-reconstruction-by-height.js rename to 406.queue-reconstruction-by-height.js diff --git a/407-trapping-rain-water-ii.js b/407.trapping-rain-water-ii.js similarity index 100% rename from 407-trapping-rain-water-ii.js rename to 407.trapping-rain-water-ii.js diff --git a/408-valid-word-abbreviation.js b/408.valid-word-abbreviation.js similarity index 100% rename from 408-valid-word-abbreviation.js rename to 408.valid-word-abbreviation.js diff --git a/409-longest-palindrome.js b/409.longest-palindrome.js old mode 100755 new mode 100644 similarity index 94% rename from 409-longest-palindrome.js rename to 409.longest-palindrome.js index b7bc720f..9feb4709 --- a/409-longest-palindrome.js +++ b/409.longest-palindrome.js @@ -1,21 +1,21 @@ -/** - * @param {string} s - * @return {number} - */ -const longestPalindrome = function (s) { - const set = new Set() - let counter = 0 - for (let i = 0; i < s.length; i++) { - const currentChar = s[i] - if (set.has(currentChar)) { - counter++ - set.delete(currentChar) - } else { - set.add(currentChar) - } - } - counter *= 2 - if (set.size > 0) counter++ - return counter -} - +/** + * @param {string} s + * @return {number} + */ +const longestPalindrome = function (s) { + const set = new Set() + let counter = 0 + for (let i = 0; i < s.length; i++) { + const currentChar = s[i] + if (set.has(currentChar)) { + counter++ + set.delete(currentChar) + } else { + set.add(currentChar) + } + } + counter *= 2 + if (set.size > 0) counter++ + return counter +} + diff --git a/41-first-missing-positive.js b/41.first-missing-positive.js similarity index 100% rename from 41-first-missing-positive.js rename to 41.first-missing-positive.js diff --git a/410-split-array-largest-sum.js b/410.split-array-largest-sum.js similarity index 100% rename from 410-split-array-largest-sum.js rename to 410.split-array-largest-sum.js diff --git a/411-minimum-unique-word-abbreviation.js b/411.minimum-unique-word-abbreviation.js similarity index 100% rename from 411-minimum-unique-word-abbreviation.js rename to 411.minimum-unique-word-abbreviation.js diff --git a/412-fizz-buzz.js b/412.fizz-buzz.js old mode 100755 new mode 100644 similarity index 93% rename from 412-fizz-buzz.js rename to 412.fizz-buzz.js index 9eca78f2..2891d92c --- a/412-fizz-buzz.js +++ b/412.fizz-buzz.js @@ -1,28 +1,28 @@ -/** - * @param {number} n - * @return {string[]} - */ -const fizzBuzz = function(n) { - const res = []; - for (let i = 1; i <= n; i++) { - res.push(single(i)); - } - - return res; -}; - -function single(num) { - let str = ""; - if (num % 3 === 0) { - str += "Fizz"; - } - if (num % 5 === 0) { - str += "Buzz"; - } - if (str === "") { - str += num; - } - return str; -} - -console.log(fizzBuzz(15)); +/** + * @param {number} n + * @return {string[]} + */ +const fizzBuzz = function(n) { + const res = []; + for (let i = 1; i <= n; i++) { + res.push(single(i)); + } + + return res; +}; + +function single(num) { + let str = ""; + if (num % 3 === 0) { + str += "Fizz"; + } + if (num % 5 === 0) { + str += "Buzz"; + } + if (str === "") { + str += num; + } + return str; +} + +console.log(fizzBuzz(15)); diff --git a/413-arithmetic-slices.js b/413.arithmetic-slices.js old mode 100755 new mode 100644 similarity index 95% rename from 413-arithmetic-slices.js rename to 413.arithmetic-slices.js index ff307a6c..c5d85363 --- a/413-arithmetic-slices.js +++ b/413.arithmetic-slices.js @@ -1,24 +1,24 @@ -/** - * @param {number[]} A - * @return {number} - */ -const numberOfArithmeticSlices = function(A) { - const arr = []; - let count = 0; - for (let i = 1; i < A.length - 1; i++) { - if (A[i] - A[i - 1] === A[i + 1] - A[i]) { - count += 1; - } else { - arr.push(count); - count = 0; - } - } - arr.push(count); - return arr.reduce((ac, el) => ac + calc(el), 0); -}; - -function calc(num) { - return (num * (num + 1)) / 2; -} - -console.log(numberOfArithmeticSlices([1, 2, 3, 4])); +/** + * @param {number[]} A + * @return {number} + */ +const numberOfArithmeticSlices = function(A) { + const arr = []; + let count = 0; + for (let i = 1; i < A.length - 1; i++) { + if (A[i] - A[i - 1] === A[i + 1] - A[i]) { + count += 1; + } else { + arr.push(count); + count = 0; + } + } + arr.push(count); + return arr.reduce((ac, el) => ac + calc(el), 0); +}; + +function calc(num) { + return (num * (num + 1)) / 2; +} + +console.log(numberOfArithmeticSlices([1, 2, 3, 4])); diff --git a/414-third-maximum-number.js b/414.third-maximum-number.js similarity index 100% rename from 414-third-maximum-number.js rename to 414.third-maximum-number.js diff --git a/415-add-strings.js b/415.add-strings.js old mode 100755 new mode 100644 similarity index 95% rename from 415-add-strings.js rename to 415.add-strings.js index cf2cad2a..eec7a034 --- a/415-add-strings.js +++ b/415.add-strings.js @@ -1,20 +1,20 @@ -/** - * @param {string} num1 - * @param {string} num2 - * @return {string} - */ -const addStrings = function(num1, num2) { - let sb = ""; - let carry = 0; - for ( - let i = num1.length - 1, j = num2.length - 1; - i >= 0 || j >= 0 || carry == 1; - i--, j-- - ) { - let x = i < 0 ? 0 : +num1.charAt(i); - let y = j < 0 ? 0 : +num2.charAt(j); - sb = (+(x + y + carry) % 10) + sb; - carry = x + y + carry >= 10 ? 1 : 0; - } - return sb; -}; +/** + * @param {string} num1 + * @param {string} num2 + * @return {string} + */ +const addStrings = function(num1, num2) { + let sb = ""; + let carry = 0; + for ( + let i = num1.length - 1, j = num2.length - 1; + i >= 0 || j >= 0 || carry == 1; + i--, j-- + ) { + let x = i < 0 ? 0 : +num1.charAt(i); + let y = j < 0 ? 0 : +num2.charAt(j); + sb = (+(x + y + carry) % 10) + sb; + carry = x + y + carry >= 10 ? 1 : 0; + } + return sb; +}; diff --git a/416-partition-equal-subset-sum.js b/416.partition-equal-subset-sum.js similarity index 100% rename from 416-partition-equal-subset-sum.js rename to 416.partition-equal-subset-sum.js diff --git a/417-pacific-atlantic-water-flow.js b/417.pacific-atlantic-water-flow.js similarity index 100% rename from 417-pacific-atlantic-water-flow.js rename to 417.pacific-atlantic-water-flow.js diff --git a/418-sentence-screen-fitting.js b/418.sentence-screen-fitting.js similarity index 100% rename from 418-sentence-screen-fitting.js rename to 418.sentence-screen-fitting.js diff --git a/42-trapping-rain-water.js b/42.trapping-rain-water.js similarity index 100% rename from 42-trapping-rain-water.js rename to 42.trapping-rain-water.js diff --git a/420-strong-password-checker.js b/420.strong-password-checker.js similarity index 100% rename from 420-strong-password-checker.js rename to 420.strong-password-checker.js diff --git a/421-maximum-xor-of-two-numbers-in-an-array.js b/421.maximum-xor-of-two-numbers-in-an-array.js similarity index 100% rename from 421-maximum-xor-of-two-numbers-in-an-array.js rename to 421.maximum-xor-of-two-numbers-in-an-array.js diff --git a/422-valid-word-square.js b/422.valid-word-square.js similarity index 100% rename from 422-valid-word-square.js rename to 422.valid-word-square.js diff --git a/423-reconstruct-original-digits-from-english.js b/423.reconstruct-original-digits-from-english.js similarity index 100% rename from 423-reconstruct-original-digits-from-english.js rename to 423.reconstruct-original-digits-from-english.js diff --git a/424-longest-repeating-character-replacement.js b/424.longest-repeating-character-replacement.js old mode 100755 new mode 100644 similarity index 96% rename from 424-longest-repeating-character-replacement.js rename to 424.longest-repeating-character-replacement.js index ba2c5331..20366639 --- a/424-longest-repeating-character-replacement.js +++ b/424.longest-repeating-character-replacement.js @@ -1,27 +1,27 @@ -/** - * @param {string} s - * @param {number} k - * @return {number} - */ -const characterReplacement = function(s, k) { - const len = s.length; - const count = Array(26).fill(0); - let start = 0, - maxCount = 0, - maxLength = 0; - const ca = "A".charCodeAt(0); - for (let end = 0; end < len; end++) { - maxCount = Math.max(maxCount, ++count[s.charCodeAt(end) - ca]); - if (end - start + 1 - maxCount > k) { - count[s.charCodeAt(start) - ca]--; - start++; - } - maxLength = Math.max(maxLength, end - start + 1); - } - return maxLength; -}; - -console.log(characterReplacement("ABAB", 2)); -console.log(characterReplacement("AABABBA", 1)); - - +/** + * @param {string} s + * @param {number} k + * @return {number} + */ +const characterReplacement = function(s, k) { + const len = s.length; + const count = Array(26).fill(0); + let start = 0, + maxCount = 0, + maxLength = 0; + const ca = "A".charCodeAt(0); + for (let end = 0; end < len; end++) { + maxCount = Math.max(maxCount, ++count[s.charCodeAt(end) - ca]); + if (end - start + 1 - maxCount > k) { + count[s.charCodeAt(start) - ca]--; + start++; + } + maxLength = Math.max(maxLength, end - start + 1); + } + return maxLength; +}; + +console.log(characterReplacement("ABAB", 2)); +console.log(characterReplacement("AABABBA", 1)); + + diff --git a/425-word-squares.js b/425.word-squares.js similarity index 100% rename from 425-word-squares.js rename to 425.word-squares.js diff --git a/426-convert-binary-search-tree-to-sorted-doubly-linked-list.js b/426.convert-binary-search-tree-to-sorted-doubly-linked-list.js similarity index 100% rename from 426-convert-binary-search-tree-to-sorted-doubly-linked-list.js rename to 426.convert-binary-search-tree-to-sorted-doubly-linked-list.js diff --git a/427-construct-quad-tree.js b/427.construct-quad-tree.js similarity index 100% rename from 427-construct-quad-tree.js rename to 427.construct-quad-tree.js diff --git a/428-serialize-and-deserialize-n-ary-tree.js b/428.serialize-and-deserialize-n-ary-tree.js similarity index 100% rename from 428-serialize-and-deserialize-n-ary-tree.js rename to 428.serialize-and-deserialize-n-ary-tree.js diff --git a/429-n-ary-tree-level-order-traversal.js b/429.n-ary-tree-level-order-traversal.js similarity index 100% rename from 429-n-ary-tree-level-order-traversal.js rename to 429.n-ary-tree-level-order-traversal.js diff --git a/43-multiply-strings.js b/43.multiply-strings.js similarity index 100% rename from 43-multiply-strings.js rename to 43.multiply-strings.js diff --git a/430-flatten-a-multilevel-doubly-linked-list.js b/430.flatten-a-multilevel-doubly-linked-list.js similarity index 100% rename from 430-flatten-a-multilevel-doubly-linked-list.js rename to 430.flatten-a-multilevel-doubly-linked-list.js diff --git a/431-encode-n-ary-tree-to-binary-tree.js b/431.encode-n-ary-tree-to-binary-tree.js similarity index 100% rename from 431-encode-n-ary-tree-to-binary-tree.js rename to 431.encode-n-ary-tree-to-binary-tree.js diff --git a/432-all-oone-data-structure.js b/432.all-oone-data-structure.js similarity index 100% rename from 432-all-oone-data-structure.js rename to 432.all-oone-data-structure.js diff --git a/433-minimum-genetic-mutation.js b/433.minimum-genetic-mutation.js similarity index 100% rename from 433-minimum-genetic-mutation.js rename to 433.minimum-genetic-mutation.js diff --git a/434-number-of-segments-in-a-string.js b/434.number-of-segments-in-a-string.js similarity index 100% rename from 434-number-of-segments-in-a-string.js rename to 434.number-of-segments-in-a-string.js diff --git a/435-non-overlapping-intervals.js b/435.non-overlapping-intervals.js similarity index 100% rename from 435-non-overlapping-intervals.js rename to 435.non-overlapping-intervals.js diff --git a/436-find-right-interval.js b/436.find-right-interval.js similarity index 100% rename from 436-find-right-interval.js rename to 436.find-right-interval.js diff --git a/437-path-sum-iii.js b/437.path-sum-iii.js similarity index 100% rename from 437-path-sum-iii.js rename to 437.path-sum-iii.js diff --git a/438-find-all-anagrams-in-a-string.js b/438.find-all-anagrams-in-a-string.js similarity index 100% rename from 438-find-all-anagrams-in-a-string.js rename to 438.find-all-anagrams-in-a-string.js diff --git a/439-ternary-expression-parser.js b/439.ternary-expression-parser.js similarity index 100% rename from 439-ternary-expression-parser.js rename to 439.ternary-expression-parser.js diff --git a/44-wildcard-matching.js b/44.wildcard-matching.js similarity index 100% rename from 44-wildcard-matching.js rename to 44.wildcard-matching.js diff --git a/440-k-th-smallest-in-lexicographical-order.js b/440.k-th-smallest-in-lexicographical-order.js similarity index 100% rename from 440-k-th-smallest-in-lexicographical-order.js rename to 440.k-th-smallest-in-lexicographical-order.js diff --git a/441-arranging-coins.js b/441.arranging-coins.js similarity index 100% rename from 441-arranging-coins.js rename to 441.arranging-coins.js diff --git a/442-find-all-duplicates-in-an-array.js b/442.find-all-duplicates-in-an-array.js old mode 100755 new mode 100644 similarity index 95% rename from 442-find-all-duplicates-in-an-array.js rename to 442.find-all-duplicates-in-an-array.js index 329a1b4d..f4902e13 --- a/442-find-all-duplicates-in-an-array.js +++ b/442.find-all-duplicates-in-an-array.js @@ -1,22 +1,22 @@ -/** - * @param {number[]} nums - * @return {number[]} - */ - -const findDuplicates = function(nums) { - if (nums === null || nums.length <= 1) { - return []; - } - - let dup = []; - for (let i = 0, n = nums.length; i < n; i++) { - let next = Math.abs(nums[i]); - nums[next - 1] < 0 ? dup.push(next) : (nums[next - 1] = -nums[next - 1]); - } - - return dup; -}; - -console.log(findDuplicates([4, 3, 2, 7, 8, 2, 3, 1])); -console.log(findDuplicates([10, 2, 5, 10, 9, 1, 1, 4, 3, 7])); - +/** + * @param {number[]} nums + * @return {number[]} + */ + +const findDuplicates = function(nums) { + if (nums === null || nums.length <= 1) { + return []; + } + + let dup = []; + for (let i = 0, n = nums.length; i < n; i++) { + let next = Math.abs(nums[i]); + nums[next - 1] < 0 ? dup.push(next) : (nums[next - 1] = -nums[next - 1]); + } + + return dup; +}; + +console.log(findDuplicates([4, 3, 2, 7, 8, 2, 3, 1])); +console.log(findDuplicates([10, 2, 5, 10, 9, 1, 1, 4, 3, 7])); + diff --git a/443-string-compression.js b/443.string-compression.js similarity index 100% rename from 443-string-compression.js rename to 443.string-compression.js diff --git a/444-sequence-reconstruction.js b/444.sequence-reconstruction.js similarity index 100% rename from 444-sequence-reconstruction.js rename to 444.sequence-reconstruction.js diff --git a/445-add-two-numbers-II.js b/445.add-two-numbers-II.js old mode 100755 new mode 100644 similarity index 95% rename from 445-add-two-numbers-II.js rename to 445.add-two-numbers-II.js index 852dd980..be90acba --- a/445-add-two-numbers-II.js +++ b/445.add-two-numbers-II.js @@ -1,58 +1,58 @@ -/** - * Definition for singly-linked list. - * function ListNode(val, next) { - * this.val = (val===undefined ? 0 : val) - * this.next = (next===undefined ? null : next) - * } - */ -/** - * @param {ListNode} l1 - * @param {ListNode} l2 - * @return {ListNode} - */ -const addTwoNumbers = function(head1, head2) { - const r1 = reverse(head1), r2 = reverse(head2) - let l1 = r1, l2 = r2, inc = 0 - let dummy = new ListNode() - let pre = dummy - while(l1 || l2) { - let val = inc - if(l1) { - val += l1.val - l1 = l1.next - } - if(l2) { - val += l2.val - l2 = l2.next - } - if(val > 9) inc = 1 - else inc = 0 - const cur = new ListNode(val % 10) - pre.next = cur - pre = cur - } - if (inc) { - pre.next = new ListNode(1) - } - return reverse(dummy.next) -}; - -function reverse(head) { - const dummy = new ListNode() - dummy.next = head - let len = 0, cur = head - while(cur) { - len++ - cur = cur.next - } - let p = dummy, tail = head, tmp = null - for(let i = 0; i < len - 1; i++) { - tmp = p.next - p.next = tail.next - tail.next = tail.next.next - p.next.next = tmp - } - return dummy.next -} - - +/** + * Definition for singly-linked list. + * function ListNode(val, next) { + * this.val = (val===undefined ? 0 : val) + * this.next = (next===undefined ? null : next) + * } + */ +/** + * @param {ListNode} l1 + * @param {ListNode} l2 + * @return {ListNode} + */ +const addTwoNumbers = function(head1, head2) { + const r1 = reverse(head1), r2 = reverse(head2) + let l1 = r1, l2 = r2, inc = 0 + let dummy = new ListNode() + let pre = dummy + while(l1 || l2) { + let val = inc + if(l1) { + val += l1.val + l1 = l1.next + } + if(l2) { + val += l2.val + l2 = l2.next + } + if(val > 9) inc = 1 + else inc = 0 + const cur = new ListNode(val % 10) + pre.next = cur + pre = cur + } + if (inc) { + pre.next = new ListNode(1) + } + return reverse(dummy.next) +}; + +function reverse(head) { + const dummy = new ListNode() + dummy.next = head + let len = 0, cur = head + while(cur) { + len++ + cur = cur.next + } + let p = dummy, tail = head, tmp = null + for(let i = 0; i < len - 1; i++) { + tmp = p.next + p.next = tail.next + tail.next = tail.next.next + p.next.next = tmp + } + return dummy.next +} + + diff --git a/446-arithmetic-slices-ii-subsequence.js b/446.arithmetic-slices-ii-subsequence.js similarity index 100% rename from 446-arithmetic-slices-ii-subsequence.js rename to 446.arithmetic-slices-ii-subsequence.js diff --git a/447-number-of-boomerangs.js b/447.number-of-boomerangs.js similarity index 100% rename from 447-number-of-boomerangs.js rename to 447.number-of-boomerangs.js diff --git a/448-find-all-numbers-disappeared-in-an-array.js b/448.find-all-numbers-disappeared-in-an-array.js old mode 100755 new mode 100644 similarity index 94% rename from 448-find-all-numbers-disappeared-in-an-array.js rename to 448.find-all-numbers-disappeared-in-an-array.js index a3fda0b0..cc874f7e --- a/448-find-all-numbers-disappeared-in-an-array.js +++ b/448.find-all-numbers-disappeared-in-an-array.js @@ -1,18 +1,18 @@ -/** - * @param {number[]} nums - * @return {number[]} - */ -const findDisappearedNumbers = function(nums) { - const res = []; - nums.forEach((el, idx) => { - res[el - 1] = 1; - }); - const arr = []; - for (let i = 0; i < nums.length; i++) { - if (res[i] == null) { - arr.push(i + 1); - } - } - return arr; -}; - +/** + * @param {number[]} nums + * @return {number[]} + */ +const findDisappearedNumbers = function(nums) { + const res = []; + nums.forEach((el, idx) => { + res[el - 1] = 1; + }); + const arr = []; + for (let i = 0; i < nums.length; i++) { + if (res[i] == null) { + arr.push(i + 1); + } + } + return arr; +}; + diff --git a/449-serialize-and-deserialize-BST.js b/449.serialize-and-deserialize-BST.js old mode 100755 new mode 100644 similarity index 95% rename from 449-serialize-and-deserialize-BST.js rename to 449.serialize-and-deserialize-BST.js index a3c35b02..7b20ca75 --- a/449-serialize-and-deserialize-BST.js +++ b/449.serialize-and-deserialize-BST.js @@ -1,59 +1,59 @@ -/** - * Definition for a binary tree node. - * function TreeNode(val) { - * this.val = val; - * this.left = this.right = null; - * } - */ - -const splitter = ","; -/** - * Encodes a tree to a single string. - * - * @param {TreeNode} root - * @return {string} - */ -const serialize = function(root) { - const sb = []; - buildString(root, sb); - sb.pop(); - return sb.join(""); -}; -function buildString(node, sb) { - if (node == null) return; - sb.push(node.val); - sb.push(splitter); - buildString(node.left, sb); - buildString(node.right, sb); -} -/** - * Decodes your encoded data to tree. - * - * @param {string} data - * @return {TreeNode} - */ -const deserialize = function(data) { - if (data.length === 0) return null; - const pos = [0]; - return buildTree( - data.split(splitter), - pos, - Number.MIN_SAFE_INTEGER, - Number.MAX_SAFE_INTEGER - ); -}; -function buildTree(nodes, pos, min, max) { - if (pos[0] === nodes.length) return null; - let val = +nodes[pos[0]]; - if (val < min || val > max) return null; - const cur = new TreeNode(val); - pos[0] += 1; - cur.left = buildTree(nodes, pos, min, val); - cur.right = buildTree(nodes, pos, val, max); - return cur; -} - -/** - * Your functions will be called as such: - * deserialize(serialize(root)); - */ +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ + +const splitter = ","; +/** + * Encodes a tree to a single string. + * + * @param {TreeNode} root + * @return {string} + */ +const serialize = function(root) { + const sb = []; + buildString(root, sb); + sb.pop(); + return sb.join(""); +}; +function buildString(node, sb) { + if (node == null) return; + sb.push(node.val); + sb.push(splitter); + buildString(node.left, sb); + buildString(node.right, sb); +} +/** + * Decodes your encoded data to tree. + * + * @param {string} data + * @return {TreeNode} + */ +const deserialize = function(data) { + if (data.length === 0) return null; + const pos = [0]; + return buildTree( + data.split(splitter), + pos, + Number.MIN_SAFE_INTEGER, + Number.MAX_SAFE_INTEGER + ); +}; +function buildTree(nodes, pos, min, max) { + if (pos[0] === nodes.length) return null; + let val = +nodes[pos[0]]; + if (val < min || val > max) return null; + const cur = new TreeNode(val); + pos[0] += 1; + cur.left = buildTree(nodes, pos, min, val); + cur.right = buildTree(nodes, pos, val, max); + return cur; +} + +/** + * Your functions will be called as such: + * deserialize(serialize(root)); + */ diff --git a/45-jump-game-ii.js b/45.jump-game-ii.js similarity index 100% rename from 45-jump-game-ii.js rename to 45.jump-game-ii.js diff --git a/450-delete-node-in-a-bst.js b/450.delete-node-in-a-bst.js similarity index 100% rename from 450-delete-node-in-a-bst.js rename to 450.delete-node-in-a-bst.js diff --git a/451-sort-characters-by-frequency.js b/451.sort-characters-by-frequency.js old mode 100755 new mode 100644 similarity index 95% rename from 451-sort-characters-by-frequency.js rename to 451.sort-characters-by-frequency.js index 69fce9ff..c282fed3 --- a/451-sort-characters-by-frequency.js +++ b/451.sort-characters-by-frequency.js @@ -1,19 +1,19 @@ -/** - * @param {string} s - * @return {string} - */ -const frequencySort = function(s) { - const charMap = {}; - for (let i = 0; i < s.length; i++) { - const index = s.charAt(i); - charMap[index] = (charMap[index] || 0) + 1; - } - return Object.entries(charMap) - .sort((a, b) => { - return b[1] - a[1]; - }) - .map(x => { - return x[0].repeat(x[1]); - }) - .join(""); -}; +/** + * @param {string} s + * @return {string} + */ +const frequencySort = function(s) { + const charMap = {}; + for (let i = 0; i < s.length; i++) { + const index = s.charAt(i); + charMap[index] = (charMap[index] || 0) + 1; + } + return Object.entries(charMap) + .sort((a, b) => { + return b[1] - a[1]; + }) + .map(x => { + return x[0].repeat(x[1]); + }) + .join(""); +}; diff --git a/452-minimum-number-of-arrows-to-burst-balloons.js b/452.minimum-number-of-arrows-to-burst-balloons.js similarity index 100% rename from 452-minimum-number-of-arrows-to-burst-balloons.js rename to 452.minimum-number-of-arrows-to-burst-balloons.js diff --git a/453-minimum-moves-to-equal-array-elements.js b/453.minimum-moves-to-equal-array-elements.js old mode 100755 new mode 100644 similarity index 95% rename from 453-minimum-moves-to-equal-array-elements.js rename to 453.minimum-moves-to-equal-array-elements.js index 19c0882e..141066f3 --- a/453-minimum-moves-to-equal-array-elements.js +++ b/453.minimum-moves-to-equal-array-elements.js @@ -1,13 +1,13 @@ -/** - * @param {number[]} nums - * @return {number} - */ -const minMoves = function(nums) { - let min = Number.MAX_SAFE_INTEGER; - let sum = 0; - for (let i = 0; i < nums.length; i++) { - min = Math.min(min, nums[i]); - sum += nums[i]; - } - return sum - min * nums.length; -}; +/** + * @param {number[]} nums + * @return {number} + */ +const minMoves = function(nums) { + let min = Number.MAX_SAFE_INTEGER; + let sum = 0; + for (let i = 0; i < nums.length; i++) { + min = Math.min(min, nums[i]); + sum += nums[i]; + } + return sum - min * nums.length; +}; diff --git a/454-4sum-ii.js b/454.4sum-ii.js similarity index 100% rename from 454-4sum-ii.js rename to 454.4sum-ii.js diff --git a/455-assign-cookies.js b/455.assign-cookies.js old mode 100755 new mode 100644 similarity index 95% rename from 455-assign-cookies.js rename to 455.assign-cookies.js index 0acfd960..14c5ad4e --- a/455-assign-cookies.js +++ b/455.assign-cookies.js @@ -1,15 +1,15 @@ -/** - * @param {number[]} g - * @param {number[]} s - * @return {number} - */ -const findContentChildren = function(g, s) { - s.sort((a, b) => a - b); - g.sort((a, b) => a - b); - - let i = 0; - for (let j = 0; i < g.length && j < s.length; j++) { - if (g[i] <= s[j]) i++; - } - return i; -}; +/** + * @param {number[]} g + * @param {number[]} s + * @return {number} + */ +const findContentChildren = function(g, s) { + s.sort((a, b) => a - b); + g.sort((a, b) => a - b); + + let i = 0; + for (let j = 0; i < g.length && j < s.length; j++) { + if (g[i] <= s[j]) i++; + } + return i; +}; diff --git a/456-132-pattern.js b/456.132-pattern.js similarity index 100% rename from 456-132-pattern.js rename to 456.132-pattern.js diff --git a/457-circular-array-loop.js b/457.circular-array-loop.js similarity index 100% rename from 457-circular-array-loop.js rename to 457.circular-array-loop.js diff --git a/458-poor-pigs.js b/458.poor-pigs.js similarity index 100% rename from 458-poor-pigs.js rename to 458.poor-pigs.js diff --git a/459-repeated-substring-pattern.js b/459.repeated-substring-pattern.js similarity index 100% rename from 459-repeated-substring-pattern.js rename to 459.repeated-substring-pattern.js diff --git a/46-permutations.js b/46.permutations.js old mode 100755 new mode 100644 similarity index 95% rename from 46-permutations.js rename to 46.permutations.js index eece119b..618ffddc --- a/46-permutations.js +++ b/46.permutations.js @@ -1,24 +1,24 @@ -/** - * @param {number[]} nums - * @return {number[][]} - */ -function permute(nums) { - const list = []; - // Arrays.sort(nums); // not necessary - backtrack(list, [], nums); - return list; -} - -function backtrack(list, tempList, nums) { - if (tempList.length == nums.length) { - list.push(tempList.slice(0)); - } else { - for (let i = 0; i < nums.length; i++) { - if (tempList.includes(nums[i])) continue; // element already exists, skip - tempList.push(nums[i]); - backtrack(list, tempList, nums); - tempList.pop(); - } - } -} - +/** + * @param {number[]} nums + * @return {number[][]} + */ +function permute(nums) { + const list = []; + // Arrays.sort(nums); // not necessary + backtrack(list, [], nums); + return list; +} + +function backtrack(list, tempList, nums) { + if (tempList.length == nums.length) { + list.push(tempList.slice(0)); + } else { + for (let i = 0; i < nums.length; i++) { + if (tempList.includes(nums[i])) continue; // element already exists, skip + tempList.push(nums[i]); + backtrack(list, tempList, nums); + tempList.pop(); + } + } +} + diff --git a/460-lfu-cache.js b/460.lfu-cache.js similarity index 100% rename from 460-lfu-cache.js rename to 460.lfu-cache.js diff --git a/461-hamming-distance.js b/461.hamming-distance.js similarity index 100% rename from 461-hamming-distance.js rename to 461.hamming-distance.js diff --git a/462-minimum-moves-to-equal-array-elements-II.js b/462.minimum-moves-to-equal-array-elements-II.js old mode 100755 new mode 100644 similarity index 94% rename from 462-minimum-moves-to-equal-array-elements-II.js rename to 462.minimum-moves-to-equal-array-elements-II.js index 4919debd..27f14035 --- a/462-minimum-moves-to-equal-array-elements-II.js +++ b/462.minimum-moves-to-equal-array-elements-II.js @@ -1,16 +1,16 @@ -/** - * @param {number[]} nums - * @return {number} - */ -const minMoves2 = function(nums) { - nums.sort((a, b) => a - b); - let i = 0, - j = nums.length - 1; - let res = 0; - while (i < j) { - res += nums[j] - nums[i]; - i++; - j--; - } - return res; -}; +/** + * @param {number[]} nums + * @return {number} + */ +const minMoves2 = function(nums) { + nums.sort((a, b) => a - b); + let i = 0, + j = nums.length - 1; + let res = 0; + while (i < j) { + res += nums[j] - nums[i]; + i++; + j--; + } + return res; +}; diff --git a/463-island-perimeter.js b/463.island-perimeter.js old mode 100755 new mode 100644 similarity index 94% rename from 463-island-perimeter.js rename to 463.island-perimeter.js index 46c1d19e..9b168d2f --- a/463-island-perimeter.js +++ b/463.island-perimeter.js @@ -1,41 +1,41 @@ -/** - * @param {number[][]} grid - * @return {number} - */ -const islandPerimeter = function(grid) { - let len = 0; - for (let r = 0; r < grid.length; r++) { - for (let c = 0; c < grid[0].length; c++) { - if (grid[r][c] === 1) { - len += cell(grid, r, c); - } - } - } - return len; -}; - -function cell(grid, r, c) { - let len = 0; - // top - if (r === 0 || grid[r - 1][c] !== 1) { - len += 1; - } - // left - if (c === 0 || grid[r][c - 1] !== 1) { - len += 1; - } - // right - if (grid[r][c + 1] !== 1) { - len += 1; - } - // bottom - if (grid[r + 1] == null || grid[r + 1][c] !== 1) { - len += 1; - } - return len; -} - -console.log( - islandPerimeter([[0, 1, 0, 0], [1, 1, 1, 0], [0, 1, 0, 0], [1, 1, 0, 0]]) -); - +/** + * @param {number[][]} grid + * @return {number} + */ +const islandPerimeter = function(grid) { + let len = 0; + for (let r = 0; r < grid.length; r++) { + for (let c = 0; c < grid[0].length; c++) { + if (grid[r][c] === 1) { + len += cell(grid, r, c); + } + } + } + return len; +}; + +function cell(grid, r, c) { + let len = 0; + // top + if (r === 0 || grid[r - 1][c] !== 1) { + len += 1; + } + // left + if (c === 0 || grid[r][c - 1] !== 1) { + len += 1; + } + // right + if (grid[r][c + 1] !== 1) { + len += 1; + } + // bottom + if (grid[r + 1] == null || grid[r + 1][c] !== 1) { + len += 1; + } + return len; +} + +console.log( + islandPerimeter([[0, 1, 0, 0], [1, 1, 1, 0], [0, 1, 0, 0], [1, 1, 0, 0]]) +); + diff --git a/464-can-i-win.js b/464.can-i-win.js similarity index 100% rename from 464-can-i-win.js rename to 464.can-i-win.js diff --git a/465-optimal-account-balancing.js b/465.optimal-account-balancing.js similarity index 100% rename from 465-optimal-account-balancing.js rename to 465.optimal-account-balancing.js diff --git a/466-count-the-repetitions.js b/466.count-the-repetitions.js similarity index 100% rename from 466-count-the-repetitions.js rename to 466.count-the-repetitions.js diff --git a/467-unique-substrings-in-wraparound-string.js b/467.unique-substrings-in-wraparound-string.js similarity index 100% rename from 467-unique-substrings-in-wraparound-string.js rename to 467.unique-substrings-in-wraparound-string.js diff --git a/468-validate-ip-address.js b/468.validate-ip-address.js similarity index 100% rename from 468-validate-ip-address.js rename to 468.validate-ip-address.js diff --git a/469-convex-polygon.js b/469.convex-polygon.js similarity index 100% rename from 469-convex-polygon.js rename to 469.convex-polygon.js diff --git a/47-permutations-ii.js b/47.permutations-ii.js similarity index 100% rename from 47-permutations-ii.js rename to 47.permutations-ii.js diff --git a/470-implement-rand10-using-rand7.js b/470.implement-rand10-using-rand7.js similarity index 100% rename from 470-implement-rand10-using-rand7.js rename to 470.implement-rand10-using-rand7.js diff --git a/471-encode-string-with-shortest-length.js b/471.encode-string-with-shortest-length.js similarity index 100% rename from 471-encode-string-with-shortest-length.js rename to 471.encode-string-with-shortest-length.js diff --git a/472-concatenated-words.js b/472.concatenated-words.js similarity index 100% rename from 472-concatenated-words.js rename to 472.concatenated-words.js diff --git a/473-matchsticks-to-square.js b/473.matchsticks-to-square.js similarity index 100% rename from 473-matchsticks-to-square.js rename to 473.matchsticks-to-square.js diff --git a/474-ones-and-zeroes.js b/474.ones-and-zeroes.js similarity index 100% rename from 474-ones-and-zeroes.js rename to 474.ones-and-zeroes.js diff --git a/475-heaters.js b/475.heaters.js similarity index 100% rename from 475-heaters.js rename to 475.heaters.js diff --git a/476-number-complement.js b/476.number-complement.js similarity index 100% rename from 476-number-complement.js rename to 476.number-complement.js diff --git a/477-total-hamming-distance.js b/477.total-hamming-distance.js old mode 100755 new mode 100644 similarity index 95% rename from 477-total-hamming-distance.js rename to 477.total-hamming-distance.js index cf1bf8fc..fb86ef4a --- a/477-total-hamming-distance.js +++ b/477.total-hamming-distance.js @@ -1,14 +1,14 @@ -/** - * @param {number[]} nums - * @return {number} - */ -const totalHammingDistance = function(nums) { - let total = 0, - n = nums.length; - for (let j = 0; j < 32; j++) { - let bitCount = 0; - for (let i = 0; i < n; i++) bitCount += (nums[i] >> j) & 1; - total += bitCount * (n - bitCount); - } - return total; -}; +/** + * @param {number[]} nums + * @return {number} + */ +const totalHammingDistance = function(nums) { + let total = 0, + n = nums.length; + for (let j = 0; j < 32; j++) { + let bitCount = 0; + for (let i = 0; i < n; i++) bitCount += (nums[i] >> j) & 1; + total += bitCount * (n - bitCount); + } + return total; +}; diff --git a/478-generate-random-point-in-a-circle.js b/478.generate-random-point-in-a-circle.js similarity index 100% rename from 478-generate-random-point-in-a-circle.js rename to 478.generate-random-point-in-a-circle.js diff --git a/479-largest-palindrome-product.js b/479.largest-palindrome-product.js similarity index 100% rename from 479-largest-palindrome-product.js rename to 479.largest-palindrome-product.js diff --git a/48-rotate-image.js b/48.rotate-image.js similarity index 100% rename from 48-rotate-image.js rename to 48.rotate-image.js diff --git a/480-sliding-window-median.js b/480.sliding-window-median.js similarity index 100% rename from 480-sliding-window-median.js rename to 480.sliding-window-median.js diff --git a/481-magical-string.js b/481.magical-string.js similarity index 100% rename from 481-magical-string.js rename to 481.magical-string.js diff --git a/482-license-key-formatting.js b/482.license-key-formatting.js similarity index 100% rename from 482-license-key-formatting.js rename to 482.license-key-formatting.js diff --git a/483-smallest-good-base.js b/483.smallest-good-base.js similarity index 100% rename from 483-smallest-good-base.js rename to 483.smallest-good-base.js diff --git a/484-find-permutation.js b/484.find-permutation.js similarity index 100% rename from 484-find-permutation.js rename to 484.find-permutation.js diff --git a/485-max-consecutive-ones.js b/485.max-consecutive-ones.js old mode 100755 new mode 100644 similarity index 100% rename from 485-max-consecutive-ones.js rename to 485.max-consecutive-ones.js diff --git a/486-predict-the-winner.js b/486.predict-the-winner.js old mode 100755 new mode 100644 similarity index 96% rename from 486-predict-the-winner.js rename to 486.predict-the-winner.js index f9c69836..c0ef92f2 --- a/486-predict-the-winner.js +++ b/486.predict-the-winner.js @@ -1,23 +1,23 @@ -/** - * @param {number[]} nums - * @return {boolean} - */ -const PredictTheWinner = function(nums) { - // The dp[i][j] saves how much more scores that the first-in-action player will get from i to j than the second player. - const dp = []; - for (let i = 0; i <= nums.length; i++) { - dp.push(Array(nums.length).fill(0)); - } - for (let s = nums.length - 1; s >= 0; s--) { - dp[s][s] = nums[s]; - for (let e = s + 1; e < nums.length; e++) { - let a = nums[s] - dp[s + 1][e]; - let b = nums[e] - dp[s][e - 1]; - dp[s][e] = Math.max(a, b); - } - } - return dp[0][nums.length - 1] >= 0; -}; - -console.log(PredictTheWinner([1, 5, 233, 7])); -console.log(PredictTheWinner([3, 5, 3])); +/** + * @param {number[]} nums + * @return {boolean} + */ +const PredictTheWinner = function(nums) { + // The dp[i][j] saves how much more scores that the first-in-action player will get from i to j than the second player. + const dp = []; + for (let i = 0; i <= nums.length; i++) { + dp.push(Array(nums.length).fill(0)); + } + for (let s = nums.length - 1; s >= 0; s--) { + dp[s][s] = nums[s]; + for (let e = s + 1; e < nums.length; e++) { + let a = nums[s] - dp[s + 1][e]; + let b = nums[e] - dp[s][e - 1]; + dp[s][e] = Math.max(a, b); + } + } + return dp[0][nums.length - 1] >= 0; +}; + +console.log(PredictTheWinner([1, 5, 233, 7])); +console.log(PredictTheWinner([3, 5, 3])); diff --git a/487-max-consecutive-ones-ii.js b/487.max-consecutive-ones-ii.js similarity index 100% rename from 487-max-consecutive-ones-ii.js rename to 487.max-consecutive-ones-ii.js diff --git a/488-zuma-game.js b/488.zuma-game.js similarity index 100% rename from 488-zuma-game.js rename to 488.zuma-game.js diff --git a/489-robot-room-cleaner.js b/489.robot-room-cleaner.js similarity index 100% rename from 489-robot-room-cleaner.js rename to 489.robot-room-cleaner.js diff --git a/49-group-anagrams.js b/49.group-anagrams.js old mode 100755 new mode 100644 similarity index 95% rename from 49-group-anagrams.js rename to 49.group-anagrams.js index ecb486dd..6f55eff7 --- a/49-group-anagrams.js +++ b/49.group-anagrams.js @@ -1,27 +1,27 @@ -/** - * @param {string[]} strs - * @return {string[][]} - */ -const groupAnagrams = (strs) => { - const resp = new Array(), - termsGrouped = new Map() - strs.forEach((term) => { - const hashed = hash(term) - if (!termsGrouped.has(hashed)) termsGrouped.set(hashed, new Array()) - termsGrouped.get(hashed).push(term) - }) - termsGrouped.forEach((terms) => { - resp.push(terms) - }) - return resp -} - -const hash = (term) => { - const arr = Array(26).fill(0) - const a = 'a'.charCodeAt(0) - for(let i = 0, len = term.length; i < len; i++) { - arr[term[i].charCodeAt(0) - a]++ - } - return arr.join('-') -} - +/** + * @param {string[]} strs + * @return {string[][]} + */ +const groupAnagrams = (strs) => { + const resp = new Array(), + termsGrouped = new Map() + strs.forEach((term) => { + const hashed = hash(term) + if (!termsGrouped.has(hashed)) termsGrouped.set(hashed, new Array()) + termsGrouped.get(hashed).push(term) + }) + termsGrouped.forEach((terms) => { + resp.push(terms) + }) + return resp +} + +const hash = (term) => { + const arr = Array(26).fill(0) + const a = 'a'.charCodeAt(0) + for(let i = 0, len = term.length; i < len; i++) { + arr[term[i].charCodeAt(0) - a]++ + } + return arr.join('-') +} + diff --git a/490-the-maze.js b/490.the-maze.js similarity index 100% rename from 490-the-maze.js rename to 490.the-maze.js diff --git a/491-increasing-subsequences.js b/491.increasing-subsequences.js similarity index 100% rename from 491-increasing-subsequences.js rename to 491.increasing-subsequences.js diff --git a/492-construct-the-rectangle.js b/492.construct-the-rectangle.js similarity index 100% rename from 492-construct-the-rectangle.js rename to 492.construct-the-rectangle.js diff --git a/493-reverse-pairs.js b/493.reverse-pairs.js similarity index 100% rename from 493-reverse-pairs.js rename to 493.reverse-pairs.js diff --git a/494-target-sum.js b/494.target-sum.js old mode 100755 new mode 100644 similarity index 95% rename from 494-target-sum.js rename to 494.target-sum.js index 1916ee6b..81a29cc1 --- a/494-target-sum.js +++ b/494.target-sum.js @@ -1,26 +1,26 @@ -/** - * @param {number[]} nums - * @param {number} target - * @return {number} - */ -const findTargetSumWays = function(nums, target) { - const sum = nums.reduce((a, b) => a+b); - if(Math.abs(target) > sum) { - return 0; - } - if((target + sum) % 2) { - return 0; - } - const newTarget = (target + sum) / 2; - let dp = new Array(newTarget+1).fill(0); - dp[0] = 1; - for(let i = 0; i < nums.length; i++) { - for(let j = newTarget; j >= nums[i]; j--) { - dp[j] += dp[j - nums[i]]; - } - } - return dp[newTarget]; -}; - - - +/** + * @param {number[]} nums + * @param {number} target + * @return {number} + */ +const findTargetSumWays = function(nums, target) { + const sum = nums.reduce((a, b) => a+b); + if(Math.abs(target) > sum) { + return 0; + } + if((target + sum) % 2) { + return 0; + } + const newTarget = (target + sum) / 2; + let dp = new Array(newTarget+1).fill(0); + dp[0] = 1; + for(let i = 0; i < nums.length; i++) { + for(let j = newTarget; j >= nums[i]; j--) { + dp[j] += dp[j - nums[i]]; + } + } + return dp[newTarget]; +}; + + + diff --git a/495-teemo-attacking.js b/495.teemo-attacking.js similarity index 100% rename from 495-teemo-attacking.js rename to 495.teemo-attacking.js diff --git a/496-next-greater-element-I.js b/496.next-greater-element-I.js old mode 100755 new mode 100644 similarity index 100% rename from 496-next-greater-element-I.js rename to 496.next-greater-element-I.js diff --git a/497-random-point-in-non-overlapping-rectangles.js b/497.random-point-in-non-overlapping-rectangles.js similarity index 100% rename from 497-random-point-in-non-overlapping-rectangles.js rename to 497.random-point-in-non-overlapping-rectangles.js diff --git a/498-diagonal-traverse.js b/498.diagonal-traverse.js similarity index 100% rename from 498-diagonal-traverse.js rename to 498.diagonal-traverse.js diff --git a/499-the-maze-iii.js b/499.the-maze-iii.js similarity index 100% rename from 499-the-maze-iii.js rename to 499.the-maze-iii.js diff --git a/5-longest-palindromic-substring.js b/5.longest-palindromic-substring.js old mode 100755 new mode 100644 similarity index 95% rename from 5-longest-palindromic-substring.js rename to 5.longest-palindromic-substring.js index 9f65c6d3..0f9b4b3f --- a/5-longest-palindromic-substring.js +++ b/5.longest-palindromic-substring.js @@ -1,21 +1,21 @@ -/** - * @param {string} s - * @return {string} - */ -const longestPalindrome = function(s) { - let res = '' - for(let i = 0, len = s.length; i < len; i++) { - let s1 = chk(s,i,i), s2 = chk(s,i,i+1) - if(s1.length > res.length) res = s1 - if(s2.length > res.length) res = s2 - } - return res -}; - -function chk(s, i, j) { - for(; i>= 0 && j < s.length; i--, j++) { - if(s[i] !== s[j]) break - } - return s.slice(i+1, j) -} - +/** + * @param {string} s + * @return {string} + */ +const longestPalindrome = function(s) { + let res = '' + for(let i = 0, len = s.length; i < len; i++) { + let s1 = chk(s,i,i), s2 = chk(s,i,i+1) + if(s1.length > res.length) res = s1 + if(s2.length > res.length) res = s2 + } + return res +}; + +function chk(s, i, j) { + for(; i>= 0 && j < s.length; i--, j++) { + if(s[i] !== s[j]) break + } + return s.slice(i+1, j) +} + diff --git a/50-powx-n.js b/50.powx-n.js similarity index 100% rename from 50-powx-n.js rename to 50.powx-n.js diff --git a/500-keyboard-row.js b/500.keyboard-row.js old mode 100755 new mode 100644 similarity index 96% rename from 500-keyboard-row.js rename to 500.keyboard-row.js index c64139fa..817fc04b --- a/500-keyboard-row.js +++ b/500.keyboard-row.js @@ -1,11 +1,11 @@ -/** - * @param {string[]} words - * @return {string[]} - */ -const findWords = function(words) { - const regex = /^[qwertyuiop]*$|^[asdfghjkl]*$|^[zxcvbnm]*$/; - return words.filter( - s => (s.toLowerCase().match(regex) === null ? false : true) - ); -}; -console.log(findWords(["Hello", "Alaska", "Dad", "Peace"])); +/** + * @param {string[]} words + * @return {string[]} + */ +const findWords = function(words) { + const regex = /^[qwertyuiop]*$|^[asdfghjkl]*$|^[zxcvbnm]*$/; + return words.filter( + s => (s.toLowerCase().match(regex) === null ? false : true) + ); +}; +console.log(findWords(["Hello", "Alaska", "Dad", "Peace"])); diff --git a/501-find-mode-in-binary-search-tree.js b/501.find-mode-in-binary-search-tree.js similarity index 100% rename from 501-find-mode-in-binary-search-tree.js rename to 501.find-mode-in-binary-search-tree.js diff --git a/502-ipo.js b/502.ipo.js similarity index 100% rename from 502-ipo.js rename to 502.ipo.js diff --git a/503-next-greater-element-II.js b/503.next-greater-element-II.js old mode 100755 new mode 100644 similarity index 95% rename from 503-next-greater-element-II.js rename to 503.next-greater-element-II.js index e70d8997..799b52a8 --- a/503-next-greater-element-II.js +++ b/503.next-greater-element-II.js @@ -1,22 +1,22 @@ -/** - * @param {number[]} nums - * @return {number[]} - */ -const nextGreaterElements = function(nums) { - const arr = [] - const n = nums.length - const res = Array(n).fill(-1) - nums.push(...nums) - const stk = [] - for(let i = 0; i < 2 * n; i++) { - const e = nums[i] - while(stk.length && nums[stk.at(-1)] < e) { - const idx = stk.pop() - res[idx] = e - } - if(i < n) stk.push(i) - } - - return res -}; - +/** + * @param {number[]} nums + * @return {number[]} + */ +const nextGreaterElements = function(nums) { + const arr = [] + const n = nums.length + const res = Array(n).fill(-1) + nums.push(...nums) + const stk = [] + for(let i = 0; i < 2 * n; i++) { + const e = nums[i] + while(stk.length && nums[stk.at(-1)] < e) { + const idx = stk.pop() + res[idx] = e + } + if(i < n) stk.push(i) + } + + return res +}; + diff --git a/504-base-7.js b/504.base-7.js similarity index 100% rename from 504-base-7.js rename to 504.base-7.js diff --git a/505-the-maze-ii.js b/505.the-maze-ii.js similarity index 100% rename from 505-the-maze-ii.js rename to 505.the-maze-ii.js diff --git a/506-relative-ranks.js b/506.relative-ranks.js similarity index 100% rename from 506-relative-ranks.js rename to 506.relative-ranks.js diff --git a/507-perfect-number.js b/507.perfect-number.js similarity index 100% rename from 507-perfect-number.js rename to 507.perfect-number.js diff --git a/508-most-frequent-subtree-sum.js b/508.most-frequent-subtree-sum.js old mode 100755 new mode 100644 similarity index 95% rename from 508-most-frequent-subtree-sum.js rename to 508.most-frequent-subtree-sum.js index e63eabf1..a3976e9a --- a/508-most-frequent-subtree-sum.js +++ b/508.most-frequent-subtree-sum.js @@ -1,48 +1,48 @@ -/** - * Definition for a binary tree node. - * function TreeNode(val) { - * this.val = val; - * this.left = this.right = null; - * } - */ -/** - * @param {TreeNode} root - * @return {number[]} - */ -const findFrequentTreeSum = function(root) { - if (root == null) return []; - const valArr = []; - calc(root, valArr); - const hash = {}; - valArr.forEach((el, idx) => { - if (hash.hasOwnProperty(el)) { - hash[el] += 1; - } else { - hash[el] = 1; - } - }); - const arr = Object.entries(hash).sort((a, b) => b[1] - a[1]); - const max = arr[0][1]; - const res = [+arr[0][0]]; - for (let i = 1; i < arr.length; i++) { - if (arr[i][1] === max) { - res.push(+arr[i][0]); - } else { - return res; - } - } - return res; -}; - -function calc(node, arr) { - let sum = 0; - if (node.left) { - sum += calc(node.left, arr); - } - if (node.right) { - sum += calc(node.right, arr); - } - sum += node.val; - arr.push(sum); - return sum; -} +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ +/** + * @param {TreeNode} root + * @return {number[]} + */ +const findFrequentTreeSum = function(root) { + if (root == null) return []; + const valArr = []; + calc(root, valArr); + const hash = {}; + valArr.forEach((el, idx) => { + if (hash.hasOwnProperty(el)) { + hash[el] += 1; + } else { + hash[el] = 1; + } + }); + const arr = Object.entries(hash).sort((a, b) => b[1] - a[1]); + const max = arr[0][1]; + const res = [+arr[0][0]]; + for (let i = 1; i < arr.length; i++) { + if (arr[i][1] === max) { + res.push(+arr[i][0]); + } else { + return res; + } + } + return res; +}; + +function calc(node, arr) { + let sum = 0; + if (node.left) { + sum += calc(node.left, arr); + } + if (node.right) { + sum += calc(node.right, arr); + } + sum += node.val; + arr.push(sum); + return sum; +} diff --git a/5083-occurrences-after-bigram.js b/5083.occurrences-after-bigram.js similarity index 100% rename from 5083-occurrences-after-bigram.js rename to 5083.occurrences-after-bigram.js diff --git a/5084-insufficient-nodes-in-root-to-leaf-paths.js b/5084.insufficient-nodes-in-root-to-leaf-paths.js similarity index 100% rename from 5084-insufficient-nodes-in-root-to-leaf-paths.js rename to 5084.insufficient-nodes-in-root-to-leaf-paths.js diff --git a/509-fibonacci-number.js b/509.fibonacci-number.js similarity index 100% rename from 509-fibonacci-number.js rename to 509.fibonacci-number.js diff --git a/51-n-queens.js b/51.n-queens.js similarity index 100% rename from 51-n-queens.js rename to 51.n-queens.js diff --git a/510-inorder-successor-in-bst-ii.js b/510.inorder-successor-in-bst-ii.js similarity index 100% rename from 510-inorder-successor-in-bst-ii.js rename to 510.inorder-successor-in-bst-ii.js diff --git a/513-find-bottom-left-tree-value.js b/513.find-bottom-left-tree-value.js old mode 100755 new mode 100644 similarity index 100% rename from 513-find-bottom-left-tree-value.js rename to 513.find-bottom-left-tree-value.js diff --git a/514-freedom-trail.js b/514.freedom-trail.js similarity index 100% rename from 514-freedom-trail.js rename to 514.freedom-trail.js diff --git a/515-find-largest-value-in-each-tree-row.js b/515.find-largest-value-in-each-tree-row.js old mode 100755 new mode 100644 similarity index 100% rename from 515-find-largest-value-in-each-tree-row.js rename to 515.find-largest-value-in-each-tree-row.js diff --git a/516-longest-palindromic-subsequence.js b/516.longest-palindromic-subsequence.js similarity index 100% rename from 516-longest-palindromic-subsequence.js rename to 516.longest-palindromic-subsequence.js diff --git a/517-super-washing-machines.js b/517.super-washing-machines.js similarity index 100% rename from 517-super-washing-machines.js rename to 517.super-washing-machines.js diff --git a/518-coin-change-2.js b/518.coin-change-2.js similarity index 100% rename from 518-coin-change-2.js rename to 518.coin-change-2.js diff --git a/519-random-flip-matrix.js b/519.random-flip-matrix.js similarity index 100% rename from 519-random-flip-matrix.js rename to 519.random-flip-matrix.js diff --git a/52-n-queens-II.js b/52.n-queens-II.js old mode 100755 new mode 100644 similarity index 95% rename from 52-n-queens-II.js rename to 52.n-queens-II.js index 4bb41ffa..53e2c031 --- a/52-n-queens-II.js +++ b/52.n-queens-II.js @@ -1,37 +1,37 @@ -/** - * @param {number} n - * @return {number} - */ -const totalNQueens = function(n) { - //Keeps track of the # of valid solutions - let count = 0; - - //Helps identify valid solutions - const done = Math.pow(2, n) - 1; - - //Checks all possible board configurations - const innerRecurse = function(ld, col, rd) { - //All columns are occupied, - //so the solution must be complete - if (col === done) { - count++; - return; - } - - //Gets a bit sequence with "1"s - //whereever there is an open "slot" - let poss = ~(ld | rd | col); - - //Loops as long as there is a valid - //place to put another queen. - while (poss & done) { - let bit = poss & -poss; - poss -= bit; - innerRecurse((ld | bit) >> 1, col | bit, (rd | bit) << 1); - } - }; - - innerRecurse(0, 0, 0); - - return count; -}; +/** + * @param {number} n + * @return {number} + */ +const totalNQueens = function(n) { + //Keeps track of the # of valid solutions + let count = 0; + + //Helps identify valid solutions + const done = Math.pow(2, n) - 1; + + //Checks all possible board configurations + const innerRecurse = function(ld, col, rd) { + //All columns are occupied, + //so the solution must be complete + if (col === done) { + count++; + return; + } + + //Gets a bit sequence with "1"s + //whereever there is an open "slot" + let poss = ~(ld | rd | col); + + //Loops as long as there is a valid + //place to put another queen. + while (poss & done) { + let bit = poss & -poss; + poss -= bit; + innerRecurse((ld | bit) >> 1, col | bit, (rd | bit) << 1); + } + }; + + innerRecurse(0, 0, 0); + + return count; +}; diff --git a/520-detect-capital.js b/520.detect-capital.js old mode 100755 new mode 100644 similarity index 94% rename from 520-detect-capital.js rename to 520.detect-capital.js index 39235478..a317f951 --- a/520-detect-capital.js +++ b/520.detect-capital.js @@ -1,55 +1,55 @@ -/** - * @param {string} word - * @return {boolean} - */ -const ac = "A".charCodeAt(0); -const zc = "Z".charCodeAt(0); -const detectCapitalUse = function(word) { - if (allCap(word) || noCap(word) || capHead(word)) { - return true; - } - return false; -}; - -function allCap(str) { - let c; - for (let i = 0; i < str.length; i++) { - c = str.charCodeAt(i); - if (c < ac || c > zc) { - return false; - } - } - return true; -} - -function noCap(str) { - let c; - for (let i = 0; i < str.length; i++) { - c = str.charCodeAt(i); - if (c >= ac && c <= zc) { - return false; - } - } - return true; -} - -function capHead(str) { - let c; - let first; - for (let i = 0; i < str.length; i++) { - c = str.charCodeAt(i); - if (i === 0) { - first = c; - } else if (c >= ac && c <= zc) { - return false; - } - } - if (first >= ac && first <= zc) { - return true; - } else { - return false; - } -} - -console.log(detectCapitalUse("ffffffffffffffffffffF")); -console.log(detectCapitalUse("Leetcode")); +/** + * @param {string} word + * @return {boolean} + */ +const ac = "A".charCodeAt(0); +const zc = "Z".charCodeAt(0); +const detectCapitalUse = function(word) { + if (allCap(word) || noCap(word) || capHead(word)) { + return true; + } + return false; +}; + +function allCap(str) { + let c; + for (let i = 0; i < str.length; i++) { + c = str.charCodeAt(i); + if (c < ac || c > zc) { + return false; + } + } + return true; +} + +function noCap(str) { + let c; + for (let i = 0; i < str.length; i++) { + c = str.charCodeAt(i); + if (c >= ac && c <= zc) { + return false; + } + } + return true; +} + +function capHead(str) { + let c; + let first; + for (let i = 0; i < str.length; i++) { + c = str.charCodeAt(i); + if (i === 0) { + first = c; + } else if (c >= ac && c <= zc) { + return false; + } + } + if (first >= ac && first <= zc) { + return true; + } else { + return false; + } +} + +console.log(detectCapitalUse("ffffffffffffffffffffF")); +console.log(detectCapitalUse("Leetcode")); diff --git a/521-longest-uncommon-subsequence-i.js b/521.longest-uncommon-subsequence-i.js similarity index 100% rename from 521-longest-uncommon-subsequence-i.js rename to 521.longest-uncommon-subsequence-i.js diff --git a/522-longest-uncommon-subsequence-ii.js b/522.longest-uncommon-subsequence-ii.js similarity index 100% rename from 522-longest-uncommon-subsequence-ii.js rename to 522.longest-uncommon-subsequence-ii.js diff --git a/523-continuous-subarray-sum.js b/523.continuous-subarray-sum.js similarity index 100% rename from 523-continuous-subarray-sum.js rename to 523.continuous-subarray-sum.js diff --git a/524-longest-word-in-dictionary-through-deleting.js b/524.longest-word-in-dictionary-through-deleting.js similarity index 100% rename from 524-longest-word-in-dictionary-through-deleting.js rename to 524.longest-word-in-dictionary-through-deleting.js diff --git a/525-contiguous-array.js b/525.contiguous-array.js similarity index 100% rename from 525-contiguous-array.js rename to 525.contiguous-array.js diff --git a/526-beautiful-arrangement.js b/526.beautiful-arrangement.js similarity index 100% rename from 526-beautiful-arrangement.js rename to 526.beautiful-arrangement.js diff --git a/527-word-abbreviation.js b/527.word-abbreviation.js similarity index 100% rename from 527-word-abbreviation.js rename to 527.word-abbreviation.js diff --git a/528-random-pick-with-weight.js b/528.random-pick-with-weight.js similarity index 100% rename from 528-random-pick-with-weight.js rename to 528.random-pick-with-weight.js diff --git a/529-minesweeper.js b/529.minesweeper.js similarity index 100% rename from 529-minesweeper.js rename to 529.minesweeper.js diff --git a/53-maximum-subarray.js b/53.maximum-subarray.js old mode 100755 new mode 100644 similarity index 94% rename from 53-maximum-subarray.js rename to 53.maximum-subarray.js index 17cd919a..312ff42c --- a/53-maximum-subarray.js +++ b/53.maximum-subarray.js @@ -1,14 +1,14 @@ -/** - * @param {number[]} nums - * @return {number} - */ -const maxSubArray = function (nums) { - let res = -1e9, sum = 0 - for(const e of nums) { - sum += e - res = Math.max(res, sum) - if(sum < 0) sum = 0 - } - return res -} - +/** + * @param {number[]} nums + * @return {number} + */ +const maxSubArray = function (nums) { + let res = -1e9, sum = 0 + for(const e of nums) { + sum += e + res = Math.max(res, sum) + if(sum < 0) sum = 0 + } + return res +} + diff --git a/530-minimum-absolute-difference-in-BST.js b/530.minimum-absolute-difference-in-BST.js old mode 100755 new mode 100644 similarity index 95% rename from 530-minimum-absolute-difference-in-BST.js rename to 530.minimum-absolute-difference-in-BST.js index 173f1715..369d64d7 --- a/530-minimum-absolute-difference-in-BST.js +++ b/530.minimum-absolute-difference-in-BST.js @@ -1,31 +1,31 @@ -/** - * Definition for a binary tree node. - * function TreeNode(val) { - * this.val = val; - * this.left = this.right = null; - * } - */ -/** - * @param {TreeNode} root - * @return {number} - */ -const getMinimumDifference = function(root) { - const arr = []; - traversal(root, arr); - let min = Number.MAX_SAFE_INTEGER; - for (let i = 1; i < arr.length; i++) { - min = Math.min(min, arr[i] - arr[i - 1]); - } - return min; -}; - -function traversal(node, arr) { - if (node === null) return; - if (node.left) { - traversal(node.left, arr); - } - arr.push(node.val); - if (node.right) { - traversal(node.right, arr); - } -} +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ +/** + * @param {TreeNode} root + * @return {number} + */ +const getMinimumDifference = function(root) { + const arr = []; + traversal(root, arr); + let min = Number.MAX_SAFE_INTEGER; + for (let i = 1; i < arr.length; i++) { + min = Math.min(min, arr[i] - arr[i - 1]); + } + return min; +}; + +function traversal(node, arr) { + if (node === null) return; + if (node.left) { + traversal(node.left, arr); + } + arr.push(node.val); + if (node.right) { + traversal(node.right, arr); + } +} diff --git a/531-lonely-pixel-i.js b/531.lonely-pixel-i.js similarity index 100% rename from 531-lonely-pixel-i.js rename to 531.lonely-pixel-i.js diff --git a/532-k-diff-pairs-in-an-array.js b/532.k-diff-pairs-in-an-array.js similarity index 100% rename from 532-k-diff-pairs-in-an-array.js rename to 532.k-diff-pairs-in-an-array.js diff --git a/533-lonely-pixel-ii.js b/533.lonely-pixel-ii.js similarity index 100% rename from 533-lonely-pixel-ii.js rename to 533.lonely-pixel-ii.js diff --git a/536-construct-binary-tree-from-string.js b/536.construct-binary-tree-from-string.js similarity index 100% rename from 536-construct-binary-tree-from-string.js rename to 536.construct-binary-tree-from-string.js diff --git a/538-convert-BST-to-greater-tree.js b/538-convert-BST-to-greater-tree.js deleted file mode 100755 index 6d28ba26..00000000 --- a/538-convert-BST-to-greater-tree.js +++ /dev/null @@ -1,19 +0,0 @@ -/** - * Definition for a binary tree node. - * function TreeNode(val) { - * this.val = val; - * this.left = this.right = null; - * } - */ -/** - * @param {TreeNode} root - * @return {TreeNode} - */ -const convertBST = function(root) { - if (root == null) return null; - convertBST(root.right); - root.val += sum; - sum = root.val; - convertBST(root.left); - return root; -}; diff --git a/538-convert-bst-to-greater-tree.js b/538.convert-BST-to-greater-tree.js similarity index 100% rename from 538-convert-bst-to-greater-tree.js rename to 538.convert-BST-to-greater-tree.js diff --git a/539-minimum-time-difference.js b/539.minimum-time-difference.js old mode 100755 new mode 100644 similarity index 96% rename from 539-minimum-time-difference.js rename to 539.minimum-time-difference.js index 2941bbd4..01232915 --- a/539-minimum-time-difference.js +++ b/539.minimum-time-difference.js @@ -1,32 +1,32 @@ -/** - * @param {string[]} timePoints - * @return {number} - */ -const findMinDifference = function(timePoints) { - const sortedArr = timePoints - .map(el => { - const arr = el.trim().split(":"); - return arr[0] === "00" && arr[1] === "00" - ? 24 * 60 - : +arr[0] * 60 + +arr[1]; - }) - .sort((a, b) => a - b); - let prev = sortedArr[0]; - let res = Number.MAX_SAFE_INTEGER; - const mid = 12 * 60; - for (let i = 1; i < sortedArr.length; i++) { - res = Math.min(res, Math.abs(sortedArr[i] - prev)); - prev = sortedArr[i]; - } - if (sortedArr[0] < mid && sortedArr[sortedArr.length - 1] > mid) { - res = Math.min( - res, - sortedArr[0] + 2 * mid - sortedArr[sortedArr.length - 1] - ); - } - return res; -}; - -console.log(findMinDifference(["23:59", "00:00"])); -console.log(findMinDifference(["12:12", "00:13"])); -console.log(findMinDifference(["05:31", "22:08", "00:35"])); +/** + * @param {string[]} timePoints + * @return {number} + */ +const findMinDifference = function(timePoints) { + const sortedArr = timePoints + .map(el => { + const arr = el.trim().split(":"); + return arr[0] === "00" && arr[1] === "00" + ? 24 * 60 + : +arr[0] * 60 + +arr[1]; + }) + .sort((a, b) => a - b); + let prev = sortedArr[0]; + let res = Number.MAX_SAFE_INTEGER; + const mid = 12 * 60; + for (let i = 1; i < sortedArr.length; i++) { + res = Math.min(res, Math.abs(sortedArr[i] - prev)); + prev = sortedArr[i]; + } + if (sortedArr[0] < mid && sortedArr[sortedArr.length - 1] > mid) { + res = Math.min( + res, + sortedArr[0] + 2 * mid - sortedArr[sortedArr.length - 1] + ); + } + return res; +}; + +console.log(findMinDifference(["23:59", "00:00"])); +console.log(findMinDifference(["12:12", "00:13"])); +console.log(findMinDifference(["05:31", "22:08", "00:35"])); diff --git a/54-spiral-matrix.js b/54.spiral-matrix.js similarity index 100% rename from 54-spiral-matrix.js rename to 54.spiral-matrix.js diff --git a/540-single-element-in-a-sorted-array.js b/540.single-element-in-a-sorted-array.js old mode 100755 new mode 100644 similarity index 100% rename from 540-single-element-in-a-sorted-array.js rename to 540.single-element-in-a-sorted-array.js diff --git a/541-reverse-string-ii.js b/541.reverse-string-ii.js similarity index 100% rename from 541-reverse-string-ii.js rename to 541.reverse-string-ii.js diff --git a/542-01-matrix.js b/542.01-matrix.js similarity index 100% rename from 542-01-matrix.js rename to 542.01-matrix.js diff --git a/543-diameter-of-binary-tree.js b/543.diameter-of-binary-tree.js old mode 100755 new mode 100644 similarity index 95% rename from 543-diameter-of-binary-tree.js rename to 543.diameter-of-binary-tree.js index a8d6dd5f..088a6463 --- a/543-diameter-of-binary-tree.js +++ b/543.diameter-of-binary-tree.js @@ -1,25 +1,25 @@ -/** - * Definition for a binary tree node. - * function TreeNode(val) { - * this.val = val; - * this.left = this.right = null; - * } - */ -/** - * @param {TreeNode} root - * @return {number} - */ -const diameterOfBinaryTree = function (root) { - if (root === null) return 0 - let longest = 0 - function dfs(node) { - if (node === null) return 0 - let leftmax = dfs(node.left) - let rightmax = dfs(node.right) - longest = Math.max(longest, leftmax + 1 + rightmax) - return Math.max(leftmax, rightmax) + 1 - } - dfs(root) - return longest - 1 -} - +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ +/** + * @param {TreeNode} root + * @return {number} + */ +const diameterOfBinaryTree = function (root) { + if (root === null) return 0 + let longest = 0 + function dfs(node) { + if (node === null) return 0 + let leftmax = dfs(node.left) + let rightmax = dfs(node.right) + longest = Math.max(longest, leftmax + 1 + rightmax) + return Math.max(leftmax, rightmax) + 1 + } + dfs(root) + return longest - 1 +} + diff --git a/544-output-contest-matches.js b/544.output-contest-matches.js similarity index 100% rename from 544-output-contest-matches.js rename to 544.output-contest-matches.js diff --git a/545-boundary-of-binary-tree.js b/545.boundary-of-binary-tree.js similarity index 100% rename from 545-boundary-of-binary-tree.js rename to 545.boundary-of-binary-tree.js diff --git a/546-remove-boxes.js b/546.remove-boxes.js similarity index 100% rename from 546-remove-boxes.js rename to 546.remove-boxes.js diff --git a/547-friend-circles.js b/547.friend-circles.js similarity index 100% rename from 547-friend-circles.js rename to 547.friend-circles.js diff --git a/548-split-array-with-equal-sum.js b/548.split-array-with-equal-sum.js similarity index 100% rename from 548-split-array-with-equal-sum.js rename to 548.split-array-with-equal-sum.js diff --git a/549-binary-tree-longest-consecutive-sequence-ii.js b/549.binary-tree-longest-consecutive-sequence-ii.js similarity index 100% rename from 549-binary-tree-longest-consecutive-sequence-ii.js rename to 549.binary-tree-longest-consecutive-sequence-ii.js diff --git a/55-jump-game.js b/55.jump-game.js similarity index 100% rename from 55-jump-game.js rename to 55.jump-game.js diff --git a/551-student-attendance-record-i.js b/551.student-attendance-record-i.js similarity index 100% rename from 551-student-attendance-record-i.js rename to 551.student-attendance-record-i.js diff --git a/552-student-attendance-record-ii.js b/552.student-attendance-record-ii.js similarity index 100% rename from 552-student-attendance-record-ii.js rename to 552.student-attendance-record-ii.js diff --git a/553-optimal-division.js b/553.optimal-division.js similarity index 100% rename from 553-optimal-division.js rename to 553.optimal-division.js diff --git a/554-brick-wall.js b/554.brick-wall.js old mode 100755 new mode 100644 similarity index 95% rename from 554-brick-wall.js rename to 554.brick-wall.js index b412e0e2..ddb17c08 --- a/554-brick-wall.js +++ b/554.brick-wall.js @@ -1,25 +1,25 @@ -/** - * @param {number[][]} wall - * @return {number} - */ -const leastBricks = function(wall) { - const hash = {}; - let row; - let rowSum = 0; - for (let i = 0; i < wall.length; i++) { - rowSum = 0; - row = wall[i]; - for (let j = 0; j < row.length - 1; j++) { - rowSum += row[j]; - hash[rowSum] = hash.hasOwnProperty(rowSum) ? hash[rowSum] + 1 : 1; - } - } - return ( - wall.length - - (Object.keys(hash).length > 0 - ? Math.max(...Object.keys(hash).map(key => hash[key])) - : 0) - ); -}; - -console.log(leastBricks([[1], [1], [1]])); +/** + * @param {number[][]} wall + * @return {number} + */ +const leastBricks = function(wall) { + const hash = {}; + let row; + let rowSum = 0; + for (let i = 0; i < wall.length; i++) { + rowSum = 0; + row = wall[i]; + for (let j = 0; j < row.length - 1; j++) { + rowSum += row[j]; + hash[rowSum] = hash.hasOwnProperty(rowSum) ? hash[rowSum] + 1 : 1; + } + } + return ( + wall.length - + (Object.keys(hash).length > 0 + ? Math.max(...Object.keys(hash).map(key => hash[key])) + : 0) + ); +}; + +console.log(leastBricks([[1], [1], [1]])); diff --git a/555-split-concatenated-strings.js b/555.split-concatenated-strings.js similarity index 100% rename from 555-split-concatenated-strings.js rename to 555.split-concatenated-strings.js diff --git a/556-next-greater-element-iii.js b/556.next-greater-element-iii.js similarity index 100% rename from 556-next-greater-element-iii.js rename to 556.next-greater-element-iii.js diff --git a/558-logical-or-of-two-binary-grids-represented-as-quad-trees.js b/558.logical-or-of-two-binary-grids-represented-as-quad-trees.js similarity index 100% rename from 558-logical-or-of-two-binary-grids-represented-as-quad-trees.js rename to 558.logical-or-of-two-binary-grids-represented-as-quad-trees.js diff --git a/559-maximum-depth-of-n-ary-tree.js b/559.maximum-depth-of-n-ary-tree.js similarity index 100% rename from 559-maximum-depth-of-n-ary-tree.js rename to 559.maximum-depth-of-n-ary-tree.js diff --git a/56-merge-intervals.js b/56.merge-intervals.js similarity index 100% rename from 56-merge-intervals.js rename to 56.merge-intervals.js diff --git a/560-subarray-sum-equals-k.js b/560.subarray-sum-equals-k.js similarity index 100% rename from 560-subarray-sum-equals-k.js rename to 560.subarray-sum-equals-k.js diff --git a/562-longest-line-of-consecutive-one-in-matrix.js b/562.longest-line-of-consecutive-one-in-matrix.js similarity index 100% rename from 562-longest-line-of-consecutive-one-in-matrix.js rename to 562.longest-line-of-consecutive-one-in-matrix.js diff --git a/563-binary-tree-tilt.js b/563.binary-tree-tilt.js similarity index 100% rename from 563-binary-tree-tilt.js rename to 563.binary-tree-tilt.js diff --git a/564-find-the-closest-palindrome.js b/564.find-the-closest-palindrome.js similarity index 100% rename from 564-find-the-closest-palindrome.js rename to 564.find-the-closest-palindrome.js diff --git a/565-array-nesting.js b/565.array-nesting.js old mode 100755 new mode 100644 similarity index 95% rename from 565-array-nesting.js rename to 565.array-nesting.js index 621fa822..188f342e --- a/565-array-nesting.js +++ b/565.array-nesting.js @@ -1,21 +1,21 @@ -/** - * @param {number[]} nums - * @return {number} - */ -const arrayNesting = function(nums) { - let res = 0; - for (let i = 0; i < nums.length; i++) { - if (nums[i] !== Number.MAX_SAFE_INTEGER) { - let start = nums[i], - count = 0; - while (nums[start] !== Number.MAX_SAFE_INTEGER) { - let temp = start; - start = nums[start]; - count++; - nums[temp] = Number.MAX_SAFE_INTEGER; - } - res = Math.max(res, count); - } - } - return res; -}; +/** + * @param {number[]} nums + * @return {number} + */ +const arrayNesting = function(nums) { + let res = 0; + for (let i = 0; i < nums.length; i++) { + if (nums[i] !== Number.MAX_SAFE_INTEGER) { + let start = nums[i], + count = 0; + while (nums[start] !== Number.MAX_SAFE_INTEGER) { + let temp = start; + start = nums[start]; + count++; + nums[temp] = Number.MAX_SAFE_INTEGER; + } + res = Math.max(res, count); + } + } + return res; +}; diff --git a/566-reshape-the-matrix.js b/566.reshape-the-matrix.js old mode 100755 new mode 100644 similarity index 95% rename from 566-reshape-the-matrix.js rename to 566.reshape-the-matrix.js index 5d780ee5..6cbf3a10 --- a/566-reshape-the-matrix.js +++ b/566.reshape-the-matrix.js @@ -1,29 +1,29 @@ -/** - * @param {number[][]} nums - * @param {number} r - * @param {number} c - * @return {number[][]} - */ -const matrixReshape = function(nums, r, c) { - if (isValid(nums, r, c) === false) { - return nums; - } - const arr = []; - nums.forEach(el => arr.push(...el)); - const res = []; - for (let start = 0; start < arr.length; start = start + c) { - res.push(arr.slice(start, start + c)); - } - return res; -}; - -function isValid(matrix, r, c) { - if (matrix.length * matrix[0].length !== r * c) { - return false; - } else { - return true; - } -} - -console.log(matrixReshape([[1, 2], [3, 4]], 1, 4)); -console.log(matrixReshape([[1, 2], [3, 4]], 2, 4)); +/** + * @param {number[][]} nums + * @param {number} r + * @param {number} c + * @return {number[][]} + */ +const matrixReshape = function(nums, r, c) { + if (isValid(nums, r, c) === false) { + return nums; + } + const arr = []; + nums.forEach(el => arr.push(...el)); + const res = []; + for (let start = 0; start < arr.length; start = start + c) { + res.push(arr.slice(start, start + c)); + } + return res; +}; + +function isValid(matrix, r, c) { + if (matrix.length * matrix[0].length !== r * c) { + return false; + } else { + return true; + } +} + +console.log(matrixReshape([[1, 2], [3, 4]], 1, 4)); +console.log(matrixReshape([[1, 2], [3, 4]], 2, 4)); diff --git a/567-permutation-in-string.js b/567.permutation-in-string.js similarity index 100% rename from 567-permutation-in-string.js rename to 567.permutation-in-string.js diff --git a/568-maximum-vacation-days.js b/568.maximum-vacation-days.js similarity index 100% rename from 568-maximum-vacation-days.js rename to 568.maximum-vacation-days.js diff --git a/57-insert-interval.js b/57.insert-interval.js similarity index 100% rename from 57-insert-interval.js rename to 57.insert-interval.js diff --git a/572-subtree-of-another-tree.js b/572.subtree-of-another-tree.js similarity index 100% rename from 572-subtree-of-another-tree.js rename to 572.subtree-of-another-tree.js diff --git a/573-squirrel-simulation.js b/573.squirrel-simulation.js similarity index 100% rename from 573-squirrel-simulation.js rename to 573.squirrel-simulation.js diff --git a/5745-last-day-where-you-can-still-cross.js b/5745.last-day-where-you-can-still-cross.js similarity index 100% rename from 5745-last-day-where-you-can-still-cross.js rename to 5745.last-day-where-you-can-still-cross.js diff --git a/575-distribute-candies.js b/575.distribute-candies.js old mode 100755 new mode 100644 similarity index 96% rename from 575-distribute-candies.js rename to 575.distribute-candies.js index 2077d614..4d72865b --- a/575-distribute-candies.js +++ b/575.distribute-candies.js @@ -1,13 +1,13 @@ -/** - * @param {number[]} candies - * @return {number} - */ -const distributeCandies = function(candies) { - const uniqNum = candies.filter((el, idx, arr) => arr.indexOf(el) === idx) - .length; - const halfNum = candies.length / 2; - return halfNum > uniqNum ? uniqNum : halfNum; -}; - -console.log(distributeCandies([1, 1, 2, 2, 3, 3])); -console.log(distributeCandies([1, 1, 2, 3])); +/** + * @param {number[]} candies + * @return {number} + */ +const distributeCandies = function(candies) { + const uniqNum = candies.filter((el, idx, arr) => arr.indexOf(el) === idx) + .length; + const halfNum = candies.length / 2; + return halfNum > uniqNum ? uniqNum : halfNum; +}; + +console.log(distributeCandies([1, 1, 2, 2, 3, 3])); +console.log(distributeCandies([1, 1, 2, 3])); diff --git a/576-out-of-boundary-paths.js b/576.out-of-boundary-paths.js similarity index 100% rename from 576-out-of-boundary-paths.js rename to 576.out-of-boundary-paths.js diff --git a/58-length-of-last-word.js b/58.length-of-last-word.js old mode 100755 new mode 100644 similarity index 95% rename from 58-length-of-last-word.js rename to 58.length-of-last-word.js index 7ded6d23..b6d9c573 --- a/58-length-of-last-word.js +++ b/58.length-of-last-word.js @@ -1,11 +1,11 @@ -/** - * @param {string} s - * @return {number} - */ -const lengthOfLastWord = function(s) { - const arr = s.split(" "); - for (let i = arr.length - 1; i >= 0; i--) { - if (arr[i].length > 0) return arr[i].length; - } - return 0; -}; +/** + * @param {string} s + * @return {number} + */ +const lengthOfLastWord = function(s) { + const arr = s.split(" "); + for (let i = arr.length - 1; i >= 0; i--) { + if (arr[i].length > 0) return arr[i].length; + } + return 0; +}; diff --git a/581-shortest-unsorted-continuous-subarray.js b/581.shortest-unsorted-continuous-subarray.js similarity index 100% rename from 581-shortest-unsorted-continuous-subarray.js rename to 581.shortest-unsorted-continuous-subarray.js diff --git a/582-kill-process.js b/582.kill-process.js similarity index 100% rename from 582-kill-process.js rename to 582.kill-process.js diff --git a/583-delete-operation-for-two-strings.js b/583.delete-operation-for-two-strings.js similarity index 100% rename from 583-delete-operation-for-two-strings.js rename to 583.delete-operation-for-two-strings.js diff --git a/5832-array-with-elements-not-equal-to-average-of-neighbors.js b/5832.array-with-elements-not-equal-to-average-of-neighbors.js similarity index 100% rename from 5832-array-with-elements-not-equal-to-average-of-neighbors.js rename to 5832.array-with-elements-not-equal-to-average-of-neighbors.js diff --git a/5843-number-of-strings-that-appear-as-substrings-in-word.js b/5843.number-of-strings-that-appear-as-substrings-in-word.js similarity index 100% rename from 5843-number-of-strings-that-appear-as-substrings-in-word.js rename to 5843.number-of-strings-that-appear-as-substrings-in-word.js diff --git a/5844-minimum-non-zero-product-of-the-array-elements.js b/5844.minimum-non-zero-product-of-the-array-elements.js similarity index 100% rename from 5844-minimum-non-zero-product-of-the-array-elements.js rename to 5844.minimum-non-zero-product-of-the-array-elements.js diff --git a/587-erect-the-fence.js b/587.erect-the-fence.js similarity index 100% rename from 587-erect-the-fence.js rename to 587.erect-the-fence.js diff --git a/588-design-in-memory-file-system.js b/588.design-in-memory-file-system.js similarity index 100% rename from 588-design-in-memory-file-system.js rename to 588.design-in-memory-file-system.js diff --git a/589-n-ary-tree-preorder-traversal.js b/589.n-ary-tree-preorder-traversal.js similarity index 100% rename from 589-n-ary-tree-preorder-traversal.js rename to 589.n-ary-tree-preorder-traversal.js diff --git a/59-spiral-matrix-II.js b/59.spiral-matrix-II.js old mode 100755 new mode 100644 similarity index 94% rename from 59-spiral-matrix-II.js rename to 59.spiral-matrix-II.js index a05de8d9..b09ce63d --- a/59-spiral-matrix-II.js +++ b/59.spiral-matrix-II.js @@ -1,39 +1,39 @@ -/** - * @param {number} n - * @return {number[][]} - */ -const generateMatrix = function(n) { - const res = []; - for (let i = 0; i < n; i++) { - res[i] = []; - } - let i = 0, - j = 0, - cur = 1; - while (n > 0) { - res[i][j] = cur++; - n--; - let step = n; - while (step > 0) { - res[i][++j] = cur++; - step--; - } - step = n; - while (step > 0) { - res[++i][j] = cur++; - step--; - } - step = n--; - while (step > 0) { - res[i][--j] = cur++; - step--; - } - step = n; - while (step > 0) { - res[--i][j] = cur++; - step--; - } - j++; - } - return res; -}; +/** + * @param {number} n + * @return {number[][]} + */ +const generateMatrix = function(n) { + const res = []; + for (let i = 0; i < n; i++) { + res[i] = []; + } + let i = 0, + j = 0, + cur = 1; + while (n > 0) { + res[i][j] = cur++; + n--; + let step = n; + while (step > 0) { + res[i][++j] = cur++; + step--; + } + step = n; + while (step > 0) { + res[++i][j] = cur++; + step--; + } + step = n--; + while (step > 0) { + res[i][--j] = cur++; + step--; + } + step = n; + while (step > 0) { + res[--i][j] = cur++; + step--; + } + j++; + } + return res; +}; diff --git a/590-n-ary-tree-postorder-traversal.js b/590.n-ary-tree-postorder-traversal.js similarity index 100% rename from 590-n-ary-tree-postorder-traversal.js rename to 590.n-ary-tree-postorder-traversal.js diff --git a/591-tag-validator.js b/591.tag-validator.js similarity index 100% rename from 591-tag-validator.js rename to 591.tag-validator.js diff --git a/592-fraction-addition-and-subtraction.js b/592.fraction-addition-and-subtraction.js similarity index 100% rename from 592-fraction-addition-and-subtraction.js rename to 592.fraction-addition-and-subtraction.js diff --git a/593-valid-square.js b/593.valid-square.js similarity index 100% rename from 593-valid-square.js rename to 593.valid-square.js diff --git a/594-longest-harmonious-subsequence.js b/594.longest-harmonious-subsequence.js similarity index 100% rename from 594-longest-harmonious-subsequence.js rename to 594.longest-harmonious-subsequence.js diff --git a/5956-find-first-palindromic-string-in-the-array.js b/5956.find-first-palindromic-string-in-the-array.js similarity index 100% rename from 5956-find-first-palindromic-string-in-the-array.js rename to 5956.find-first-palindromic-string-in-the-array.js diff --git a/5957-adding-spaces-to-a-string.js b/5957.adding-spaces-to-a-string.js similarity index 100% rename from 5957-adding-spaces-to-a-string.js rename to 5957.adding-spaces-to-a-string.js diff --git a/5958-number-of-smooth-descent-periods-of-a-stock.js b/5958.number-of-smooth-descent-periods-of-a-stock.js similarity index 100% rename from 5958-number-of-smooth-descent-periods-of-a-stock.js rename to 5958.number-of-smooth-descent-periods-of-a-stock.js diff --git a/5976-check-if-every-row-and-column-contains-all-numbers.js b/5976.check-if-every-row-and-column-contains-all-numbers.js similarity index 100% rename from 5976-check-if-every-row-and-column-contains-all-numbers.js rename to 5976.check-if-every-row-and-column-contains-all-numbers.js diff --git a/5977-minimum-swaps-to-group-all-1s-together-ii.js b/5977.minimum-swaps-to-group-all-1s-together-ii.js similarity index 100% rename from 5977-minimum-swaps-to-group-all-1s-together-ii.js rename to 5977.minimum-swaps-to-group-all-1s-together-ii.js diff --git a/5978-count-words-obtained-after-adding-a-letter.js b/5978.count-words-obtained-after-adding-a-letter.js similarity index 100% rename from 5978-count-words-obtained-after-adding-a-letter.js rename to 5978.count-words-obtained-after-adding-a-letter.js diff --git a/598-range-addition-ii.js b/598.range-addition-ii.js similarity index 100% rename from 598-range-addition-ii.js rename to 598.range-addition-ii.js diff --git a/599-minimum-index-sum-of-two-lists.js b/599.minimum-index-sum-of-two-lists.js old mode 100755 new mode 100644 similarity index 95% rename from 599-minimum-index-sum-of-two-lists.js rename to 599.minimum-index-sum-of-two-lists.js index 6102fba8..b4fbb647 --- a/599-minimum-index-sum-of-two-lists.js +++ b/599.minimum-index-sum-of-two-lists.js @@ -1,36 +1,36 @@ -/** - * @param {string[]} list1 - * @param {string[]} list2 - * @return {string[]} - */ -const findRestaurant = function(list1, list2) { - const hash = {}; - for (let i = 0; i < list1.length; i++) { - if (!hash.hasOwnProperty(list1[i])) { - hash[list1[i]] = i; - } - } - const resArr = []; - for (let j = 0; j < list2.length; j++) { - if (hash.hasOwnProperty(list2[j])) { - resArr.push([list2[j], hash[list2[j]] + j]); - } - } - const resHash = {}; - resArr.forEach(el => { - if (resHash.hasOwnProperty(el[1])) { - resHash[el[1]].push(el[0]); - } else { - resHash[el[1]] = [el[0]]; - } - }); - resArr.sort((a, b) => a[1] - b[1]); - return resHash[resArr[0][1]]; -}; - -console.log( - findRestaurant( - ["Shogun", "Tapioca Express", "Burger King", "KFC"], - ["KFC", "Burger King", "Tapioca Express", "Shogun"] - ) -); +/** + * @param {string[]} list1 + * @param {string[]} list2 + * @return {string[]} + */ +const findRestaurant = function(list1, list2) { + const hash = {}; + for (let i = 0; i < list1.length; i++) { + if (!hash.hasOwnProperty(list1[i])) { + hash[list1[i]] = i; + } + } + const resArr = []; + for (let j = 0; j < list2.length; j++) { + if (hash.hasOwnProperty(list2[j])) { + resArr.push([list2[j], hash[list2[j]] + j]); + } + } + const resHash = {}; + resArr.forEach(el => { + if (resHash.hasOwnProperty(el[1])) { + resHash[el[1]].push(el[0]); + } else { + resHash[el[1]] = [el[0]]; + } + }); + resArr.sort((a, b) => a[1] - b[1]); + return resHash[resArr[0][1]]; +}; + +console.log( + findRestaurant( + ["Shogun", "Tapioca Express", "Burger King", "KFC"], + ["KFC", "Burger King", "Tapioca Express", "Shogun"] + ) +); diff --git a/6-zigzag-conversion.js b/6.zigzag-conversion.js old mode 100755 new mode 100644 similarity index 95% rename from 6-zigzag-conversion.js rename to 6.zigzag-conversion.js index 5b6a3881..6c17c397 --- a/6-zigzag-conversion.js +++ b/6.zigzag-conversion.js @@ -1,28 +1,28 @@ -/** - * @param {string} s - * @param {number} numRows - * @return {string} - */ -const convert = function(s, numRows) { - if (numRows === 1) { - return s; - } - let output = ""; - for (let i = 1; i <= numRows; i++) { - let j = i - 1; - let maxIncrement = 2 * numRows - 2; - let increment = 2 * numRows - 2 - 2 * j; - if (increment === 0) { - increment = maxIncrement; - } else { - increment = maxIncrement - increment; - } - for (j; j < s.length; j += increment) { - output += s[j]; - if (maxIncrement !== increment) { - increment = maxIncrement - increment; - } - } - } - return output; -}; +/** + * @param {string} s + * @param {number} numRows + * @return {string} + */ +const convert = function(s, numRows) { + if (numRows === 1) { + return s; + } + let output = ""; + for (let i = 1; i <= numRows; i++) { + let j = i - 1; + let maxIncrement = 2 * numRows - 2; + let increment = 2 * numRows - 2 - 2 * j; + if (increment === 0) { + increment = maxIncrement; + } else { + increment = maxIncrement - increment; + } + for (j; j < s.length; j += increment) { + output += s[j]; + if (maxIncrement !== increment) { + increment = maxIncrement - increment; + } + } + } + return output; +}; diff --git a/60-permutation-sequence.js b/60.permutation-sequence.js similarity index 100% rename from 60-permutation-sequence.js rename to 60.permutation-sequence.js diff --git a/600-non-negative-integers-without-consecutive-ones.js b/600.non-negative-integers-without-consecutive-ones.js similarity index 100% rename from 600-non-negative-integers-without-consecutive-ones.js rename to 600.non-negative-integers-without-consecutive-ones.js diff --git a/604-design-compressed-string-iterator.js b/604.design-compressed-string-iterator.js similarity index 100% rename from 604-design-compressed-string-iterator.js rename to 604.design-compressed-string-iterator.js diff --git a/605-can-place-flowers.js b/605.can-place-flowers.js similarity index 100% rename from 605-can-place-flowers.js rename to 605.can-place-flowers.js diff --git a/606-construct-string-from-binary-tree.js b/606.construct-string-from-binary-tree.js similarity index 100% rename from 606-construct-string-from-binary-tree.js rename to 606.construct-string-from-binary-tree.js diff --git a/609-find-duplicate-file-in-system.js b/609.find-duplicate-file-in-system.js similarity index 100% rename from 609-find-duplicate-file-in-system.js rename to 609.find-duplicate-file-in-system.js diff --git a/61-rotate-list.js b/61.rotate-list.js similarity index 100% rename from 61-rotate-list.js rename to 61.rotate-list.js diff --git a/611-valid-triangle-number.js b/611.valid-triangle-number.js old mode 100755 new mode 100644 similarity index 95% rename from 611-valid-triangle-number.js rename to 611.valid-triangle-number.js index fa449f85..52b08d2a --- a/611-valid-triangle-number.js +++ b/611.valid-triangle-number.js @@ -1,24 +1,24 @@ -/** - * @param {number[]} nums - * @return {number} - */ -const triangleNumber = function(nums) { - nums.sort((a, b) => a - b); - let count = 0; - let n = nums.length; - for (let i = n - 1; i >= 2; i--) { - let lo = 0; - let mid = i - 1; - while (lo < mid) { - if (nums[lo] + nums[mid] > nums[i]) { - count += mid - lo; - mid -= 1; - } else { - lo += 1; - } - } - } - return count; -}; - -console.log(triangleNumber([2, 2, 3, 4])); +/** + * @param {number[]} nums + * @return {number} + */ +const triangleNumber = function(nums) { + nums.sort((a, b) => a - b); + let count = 0; + let n = nums.length; + for (let i = n - 1; i >= 2; i--) { + let lo = 0; + let mid = i - 1; + while (lo < mid) { + if (nums[lo] + nums[mid] > nums[i]) { + count += mid - lo; + mid -= 1; + } else { + lo += 1; + } + } + } + return count; +}; + +console.log(triangleNumber([2, 2, 3, 4])); diff --git a/616-add-bold-tag-in-string.js b/616.add-bold-tag-in-string.js similarity index 100% rename from 616-add-bold-tag-in-string.js rename to 616.add-bold-tag-in-string.js diff --git a/62-unique-paths.js b/62.unique-paths.js similarity index 100% rename from 62-unique-paths.js rename to 62.unique-paths.js diff --git a/621-task-scheduler.js b/621.task-scheduler.js old mode 100755 new mode 100644 similarity index 95% rename from 621-task-scheduler.js rename to 621.task-scheduler.js index 2587c021..eac1fb6b --- a/621-task-scheduler.js +++ b/621.task-scheduler.js @@ -1,31 +1,31 @@ -/** - * @param {character[]} tasks - * @param {number} n - * @return {number} - */ -const leastInterval = function (tasks, n) { - const len = tasks.length - const cnt = Array(26).fill(0) - - const A = 'A'.charCodeAt(0) - let maxFreq = 0, - maxFreqCnt = 0 - for (const ch of tasks) { - const idx = ch.charCodeAt(0) - A - cnt[idx]++ - if (maxFreq === cnt[idx]) { - maxFreqCnt++ - } else if (maxFreq < cnt[idx]) { - maxFreqCnt = 1 - maxFreq = cnt[idx] - } - } - - const slot = maxFreq - 1 - const numOfPerSlot = n - (maxFreqCnt - 1) - const available = len - maxFreq * maxFreqCnt - const idles = Math.max(0, slot * numOfPerSlot - available) - return len + idles -} - - +/** + * @param {character[]} tasks + * @param {number} n + * @return {number} + */ +const leastInterval = function (tasks, n) { + const len = tasks.length + const cnt = Array(26).fill(0) + + const A = 'A'.charCodeAt(0) + let maxFreq = 0, + maxFreqCnt = 0 + for (const ch of tasks) { + const idx = ch.charCodeAt(0) - A + cnt[idx]++ + if (maxFreq === cnt[idx]) { + maxFreqCnt++ + } else if (maxFreq < cnt[idx]) { + maxFreqCnt = 1 + maxFreq = cnt[idx] + } + } + + const slot = maxFreq - 1 + const numOfPerSlot = n - (maxFreqCnt - 1) + const available = len - maxFreq * maxFreqCnt + const idles = Math.max(0, slot * numOfPerSlot - available) + return len + idles +} + + diff --git a/622-design-circular-queue.js b/622.design-circular-queue.js similarity index 100% rename from 622-design-circular-queue.js rename to 622.design-circular-queue.js diff --git a/623-add-one-row-to-tree.js b/623.add-one-row-to-tree.js similarity index 100% rename from 623-add-one-row-to-tree.js rename to 623.add-one-row-to-tree.js diff --git a/624-maximum-distance-in-arrays.js b/624.maximum-distance-in-arrays.js similarity index 100% rename from 624-maximum-distance-in-arrays.js rename to 624.maximum-distance-in-arrays.js diff --git a/625-minimum-factorization.js b/625.minimum-factorization.js similarity index 100% rename from 625-minimum-factorization.js rename to 625.minimum-factorization.js diff --git a/628-maximum-product-of-three-numbers.js b/628.maximum-product-of-three-numbers.js similarity index 100% rename from 628-maximum-product-of-three-numbers.js rename to 628.maximum-product-of-three-numbers.js diff --git a/629-k-inverse-pairs-array.js b/629.k-inverse-pairs-array.js similarity index 100% rename from 629-k-inverse-pairs-array.js rename to 629.k-inverse-pairs-array.js diff --git a/63-unique-paths-ii.js b/63.unique-paths-ii.js similarity index 100% rename from 63-unique-paths-ii.js rename to 63.unique-paths-ii.js diff --git a/630-course-schedule-iii.js b/630.course-schedule-iii.js similarity index 100% rename from 630-course-schedule-iii.js rename to 630.course-schedule-iii.js diff --git a/631-design-excel-sum-formula.js b/631.design-excel-sum-formula.js similarity index 100% rename from 631-design-excel-sum-formula.js rename to 631.design-excel-sum-formula.js diff --git a/632-smallest-range.js b/632.smallest-range.js similarity index 100% rename from 632-smallest-range.js rename to 632.smallest-range.js diff --git a/633-sum-of-square-numbers.js b/633.sum-of-square-numbers.js similarity index 100% rename from 633-sum-of-square-numbers.js rename to 633.sum-of-square-numbers.js diff --git a/634-find-the-derangement-of-an-array.js b/634.find-the-derangement-of-an-array.js similarity index 100% rename from 634-find-the-derangement-of-an-array.js rename to 634.find-the-derangement-of-an-array.js diff --git a/635-design-log-storage-system.js b/635.design-log-storage-system.js similarity index 100% rename from 635-design-log-storage-system.js rename to 635.design-log-storage-system.js diff --git a/636-exclusive-time-of-functions.js b/636.exclusive-time-of-functions.js similarity index 100% rename from 636-exclusive-time-of-functions.js rename to 636.exclusive-time-of-functions.js diff --git a/637-average-of-levels-in-binary-tree.js b/637.average-of-levels-in-binary-tree.js old mode 100755 new mode 100644 similarity index 100% rename from 637-average-of-levels-in-binary-tree.js rename to 637.average-of-levels-in-binary-tree.js diff --git a/638-shopping-offers.js b/638.shopping-offers.js similarity index 100% rename from 638-shopping-offers.js rename to 638.shopping-offers.js diff --git a/639-decode-ways-ii.js b/639.decode-ways-ii.js similarity index 100% rename from 639-decode-ways-ii.js rename to 639.decode-ways-ii.js diff --git a/64-minimum-path-sum.js b/64.minimum-path-sum.js old mode 100755 new mode 100644 similarity index 95% rename from 64-minimum-path-sum.js rename to 64.minimum-path-sum.js index d14f7e8b..6bbddab6 --- a/64-minimum-path-sum.js +++ b/64.minimum-path-sum.js @@ -1,20 +1,20 @@ -/** - * @param {number[][]} grid - * @return {number} - */ -const minPathSum = function(grid) { - const m = grid.length; - const n = grid[0].length; - for (let i = 1; i < n; i++) { - grid[0][i] += grid[0][i - 1]; - } - for (let i = 1; i < m; i++) { - grid[i][0] += grid[i - 1][0]; - } - for (let i = 1; i < m; i++) { - for (let j = 1; j < n; j++) { - grid[i][j] += Math.min(grid[i - 1][j], grid[i][j - 1]); - } - } - return grid[m - 1][n - 1]; -}; +/** + * @param {number[][]} grid + * @return {number} + */ +const minPathSum = function(grid) { + const m = grid.length; + const n = grid[0].length; + for (let i = 1; i < n; i++) { + grid[0][i] += grid[0][i - 1]; + } + for (let i = 1; i < m; i++) { + grid[i][0] += grid[i - 1][0]; + } + for (let i = 1; i < m; i++) { + for (let j = 1; j < n; j++) { + grid[i][j] += Math.min(grid[i - 1][j], grid[i][j - 1]); + } + } + return grid[m - 1][n - 1]; +}; diff --git a/640-solve-the-equation.js b/640.solve-the-equation.js similarity index 100% rename from 640-solve-the-equation.js rename to 640.solve-the-equation.js diff --git a/641-design-circular-deque.js b/641.design-circular-deque.js similarity index 100% rename from 641-design-circular-deque.js rename to 641.design-circular-deque.js diff --git a/642-design-search-autocomplete-system.js b/642.design-search-autocomplete-system.js similarity index 100% rename from 642-design-search-autocomplete-system.js rename to 642.design-search-autocomplete-system.js diff --git a/643-maximum-average-subarray-i.js b/643.maximum-average-subarray-i.js similarity index 100% rename from 643-maximum-average-subarray-i.js rename to 643.maximum-average-subarray-i.js diff --git a/644-maximum-average-subarray-ii.js b/644.maximum-average-subarray-ii.js similarity index 100% rename from 644-maximum-average-subarray-ii.js rename to 644.maximum-average-subarray-ii.js diff --git a/645-set-mismatch.js b/645.set-mismatch.js similarity index 100% rename from 645-set-mismatch.js rename to 645.set-mismatch.js diff --git a/646-maximum-length-of-pair-chain.js b/646.maximum-length-of-pair-chain.js old mode 100755 new mode 100644 similarity index 95% rename from 646-maximum-length-of-pair-chain.js rename to 646.maximum-length-of-pair-chain.js index 59a05b36..5dc110cb --- a/646-maximum-length-of-pair-chain.js +++ b/646.maximum-length-of-pair-chain.js @@ -1,16 +1,16 @@ -/** - * @param {number[][]} pairs - * @return {number} - */ -const findLongestChain = function(pairs) { - pairs.sort((a, b) => a[1] - b[1]) - let end = pairs[0][1], res = 1 - for(let i = 1, len = pairs.length; i < len; i++) { - if(pairs[i][0] > end) { - res++ - end = pairs[i][1] - } - } - return res -}; - +/** + * @param {number[][]} pairs + * @return {number} + */ +const findLongestChain = function(pairs) { + pairs.sort((a, b) => a[1] - b[1]) + let end = pairs[0][1], res = 1 + for(let i = 1, len = pairs.length; i < len; i++) { + if(pairs[i][0] > end) { + res++ + end = pairs[i][1] + } + } + return res +}; + diff --git a/647-palindromic-substrings.js b/647.palindromic-substrings.js old mode 100755 new mode 100644 similarity index 100% rename from 647-palindromic-substrings.js rename to 647.palindromic-substrings.js diff --git a/648-replace-words.js b/648.replace-words.js old mode 100755 new mode 100644 similarity index 95% rename from 648-replace-words.js rename to 648.replace-words.js index 7ed1742c..6e358f5f --- a/648-replace-words.js +++ b/648.replace-words.js @@ -1,39 +1,39 @@ -/** - * @param {string[]} dict - * @param {string} sentence - * @return {string} - */ -const replaceWords = function(dict, sentence) { - dict.sort(); - const unsortedParts = sentence.split(" "); - const parts = unsortedParts.slice(); - parts.sort(); - - let i = (j = 0); - const rootMap = {}; - while (i < dict.length && j < parts.length) { - let part = parts[j]; - let root = dict[i]; - // dict is ahead, increase part - if (root > part) { - j++; - } else { - if (part.startsWith(root)) { - rootMap[part] = root; - j++; - } else { - i++; - } - } - } - for (i = 0; i < unsortedParts.length; i++) { - if (rootMap[unsortedParts[i]]) { - unsortedParts[i] = rootMap[unsortedParts[i]]; - } - } - return unsortedParts.join(" "); -}; - -console.log( - replaceWords(["cat", "bat", "rat"], "the cattle was rattled by the battery") -); +/** + * @param {string[]} dict + * @param {string} sentence + * @return {string} + */ +const replaceWords = function(dict, sentence) { + dict.sort(); + const unsortedParts = sentence.split(" "); + const parts = unsortedParts.slice(); + parts.sort(); + + let i = (j = 0); + const rootMap = {}; + while (i < dict.length && j < parts.length) { + let part = parts[j]; + let root = dict[i]; + // dict is ahead, increase part + if (root > part) { + j++; + } else { + if (part.startsWith(root)) { + rootMap[part] = root; + j++; + } else { + i++; + } + } + } + for (i = 0; i < unsortedParts.length; i++) { + if (rootMap[unsortedParts[i]]) { + unsortedParts[i] = rootMap[unsortedParts[i]]; + } + } + return unsortedParts.join(" "); +}; + +console.log( + replaceWords(["cat", "bat", "rat"], "the cattle was rattled by the battery") +); diff --git a/649-dota2-senate.js b/649.dota2-senate.js similarity index 100% rename from 649-dota2-senate.js rename to 649.dota2-senate.js diff --git a/65-valid-number.js b/65.valid-number.js old mode 100755 new mode 100644 similarity index 96% rename from 65-valid-number.js rename to 65.valid-number.js index 1660b715..1708248a --- a/65-valid-number.js +++ b/65.valid-number.js @@ -1,35 +1,35 @@ -/** - * @param {string} str - * @returns {boolean} - */ -const isNumber = function(str) { - let i = 0; - let s = str; - // 跳过前导空格 - for (; i < s.length && " " == s[i]; ++i); - // 处理正负号 - if ("+" == s[i] || "-" == s[i]) ++i; - // 处理后面数字部分 - let digit = false, - dot = false, - exp = false; - for (; i < s.length; ++i) { - if ("." == s[i] && !dot) - // '.'不能出现2次,'.'前面可以没有数字 - dot = true; - else if ("e" == s[i] && !exp && digit) { - // 'e'不能出现2次,'e'前面必须有数字 - // 'e'后面不能出现'.','e'后面必须是整数(可以是正的或负的) - dot = exp = true; - if (i + 1 < s.length && ("+" == s[i + 1] || "-" == s[i + 1])) ++i; - if (i + 1 >= s.length || !(s[i + 1] >= "0" && s[i + 1] <= "9")) - return false; - } else if (s[i] >= "0" && s[i] <= "9") digit = true; - else break; - } - - // 跳过后面空格 - for (; i < s.length && " " == s[i]; ++i); - - return digit && i == s.length; -}; +/** + * @param {string} str + * @returns {boolean} + */ +const isNumber = function(str) { + let i = 0; + let s = str; + // 跳过前导空格 + for (; i < s.length && " " == s[i]; ++i); + // 处理正负号 + if ("+" == s[i] || "-" == s[i]) ++i; + // 处理后面数字部分 + let digit = false, + dot = false, + exp = false; + for (; i < s.length; ++i) { + if ("." == s[i] && !dot) + // '.'不能出现2次,'.'前面可以没有数字 + dot = true; + else if ("e" == s[i] && !exp && digit) { + // 'e'不能出现2次,'e'前面必须有数字 + // 'e'后面不能出现'.','e'后面必须是整数(可以是正的或负的) + dot = exp = true; + if (i + 1 < s.length && ("+" == s[i + 1] || "-" == s[i + 1])) ++i; + if (i + 1 >= s.length || !(s[i + 1] >= "0" && s[i + 1] <= "9")) + return false; + } else if (s[i] >= "0" && s[i] <= "9") digit = true; + else break; + } + + // 跳过后面空格 + for (; i < s.length && " " == s[i]; ++i); + + return digit && i == s.length; +}; diff --git a/650-2-keys-keyboard.js b/650.2-keys-keyboard.js old mode 100755 new mode 100644 similarity index 93% rename from 650-2-keys-keyboard.js rename to 650.2-keys-keyboard.js index ff4f513e..a77d8c51 --- a/650-2-keys-keyboard.js +++ b/650.2-keys-keyboard.js @@ -1,14 +1,14 @@ -/** - * @param {number} n - * @return {number} - */ -const minSteps = function(n) { - let res = 0 - for (let i = 2; i <= n; i++) { - while (n % i === 0) { - res += i - n /= i - } - } - return res -} +/** + * @param {number} n + * @return {number} + */ +const minSteps = function(n) { + let res = 0 + for (let i = 2; i <= n; i++) { + while (n % i === 0) { + res += i + n /= i + } + } + return res +} diff --git a/651-4-keys-keyboard.js b/651.4-keys-keyboard.js similarity index 100% rename from 651-4-keys-keyboard.js rename to 651.4-keys-keyboard.js diff --git a/652-find-duplicate-subtrees.js b/652.find-duplicate-subtrees.js similarity index 100% rename from 652-find-duplicate-subtrees.js rename to 652.find-duplicate-subtrees.js diff --git a/653-two-sum-iv-input-is-a-bst.js b/653.two-sum-iv-input-is-a-bst.js similarity index 100% rename from 653-two-sum-iv-input-is-a-bst.js rename to 653.two-sum-iv-input-is-a-bst.js diff --git a/655-print-binary-tree.js b/655.print-binary-tree.js similarity index 100% rename from 655-print-binary-tree.js rename to 655.print-binary-tree.js diff --git a/656-coin-path.js b/656.coin-path.js similarity index 100% rename from 656-coin-path.js rename to 656.coin-path.js diff --git a/658-find-k-closest-elements.js b/658.find-k-closest-elements.js similarity index 100% rename from 658-find-k-closest-elements.js rename to 658.find-k-closest-elements.js diff --git a/659-split-array-into-consecutive-subsequences.js b/659.split-array-into-consecutive-subsequences.js similarity index 100% rename from 659-split-array-into-consecutive-subsequences.js rename to 659.split-array-into-consecutive-subsequences.js diff --git a/66-plus-one.js b/66.plus-one.js similarity index 100% rename from 66-plus-one.js rename to 66.plus-one.js diff --git a/660-remove-9.js b/660.remove-9.js similarity index 100% rename from 660-remove-9.js rename to 660.remove-9.js diff --git a/661-image-smoother.js b/661.image-smoother.js similarity index 100% rename from 661-image-smoother.js rename to 661.image-smoother.js diff --git a/662-maximum-width-of-binary-tree.js b/662.maximum-width-of-binary-tree.js similarity index 100% rename from 662-maximum-width-of-binary-tree.js rename to 662.maximum-width-of-binary-tree.js diff --git a/663-equal-tree-partition.js b/663.equal-tree-partition.js similarity index 100% rename from 663-equal-tree-partition.js rename to 663.equal-tree-partition.js diff --git a/664-strange-printer.js b/664.strange-printer.js similarity index 100% rename from 664-strange-printer.js rename to 664.strange-printer.js diff --git a/665-non-decreasing-array.js b/665.non-decreasing-array.js similarity index 100% rename from 665-non-decreasing-array.js rename to 665.non-decreasing-array.js diff --git a/666-path-sum-iv.js b/666.path-sum-iv.js similarity index 100% rename from 666-path-sum-iv.js rename to 666.path-sum-iv.js diff --git a/667-beautiful-arrangement-ii.js b/667.beautiful-arrangement-ii.js similarity index 100% rename from 667-beautiful-arrangement-ii.js rename to 667.beautiful-arrangement-ii.js diff --git a/668-kth-smallest-number-in-multiplication-table.js b/668.kth-smallest-number-in-multiplication-table.js similarity index 100% rename from 668-kth-smallest-number-in-multiplication-table.js rename to 668.kth-smallest-number-in-multiplication-table.js diff --git a/669-trim-a-binary-search-tree.js b/669.trim-a-binary-search-tree.js old mode 100755 new mode 100644 similarity index 95% rename from 669-trim-a-binary-search-tree.js rename to 669.trim-a-binary-search-tree.js index 506a7578..323fc01e --- a/669-trim-a-binary-search-tree.js +++ b/669.trim-a-binary-search-tree.js @@ -1,35 +1,35 @@ -/** - * Definition for a binary tree node. - * function TreeNode(val) { - * this.val = val; - * this.left = this.right = null; - * } - */ -/** - * @param {TreeNode} root - * @param {number} L - * @param {number} R - * @return {TreeNode} - */ -const trimBST = function(root, L, R) { - return single(root, L, R); -}; - -function single(node, L, R) { - if (node === null) { - return null; - } - if (node.val > R) { - return single(node.left, L, R); - } - if (node.val < L) { - return single(node.right, L, R); - } - if (node.left !== null) { - node.left = single(node.left, L, R); - } - if (node.right !== null) { - node.right = single(node.right, L, R); - } - return node; -} +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ +/** + * @param {TreeNode} root + * @param {number} L + * @param {number} R + * @return {TreeNode} + */ +const trimBST = function(root, L, R) { + return single(root, L, R); +}; + +function single(node, L, R) { + if (node === null) { + return null; + } + if (node.val > R) { + return single(node.left, L, R); + } + if (node.val < L) { + return single(node.right, L, R); + } + if (node.left !== null) { + node.left = single(node.left, L, R); + } + if (node.right !== null) { + node.right = single(node.right, L, R); + } + return node; +} diff --git a/67-add-binary.js b/67.add-binary.js similarity index 100% rename from 67-add-binary.js rename to 67.add-binary.js diff --git a/670-maximum-swap.js b/670.maximum-swap.js similarity index 100% rename from 670-maximum-swap.js rename to 670.maximum-swap.js diff --git a/671-second-minimum-node-in-a-binary-tree.js b/671.second-minimum-node-in-a-binary-tree.js similarity index 100% rename from 671-second-minimum-node-in-a-binary-tree.js rename to 671.second-minimum-node-in-a-binary-tree.js diff --git a/672-bulb-switcher-ii.js b/672.bulb-switcher-ii.js similarity index 100% rename from 672-bulb-switcher-ii.js rename to 672.bulb-switcher-ii.js diff --git a/673-number-of-longest-increasing-subsequence.js b/673.number-of-longest-increasing-subsequence.js similarity index 100% rename from 673-number-of-longest-increasing-subsequence.js rename to 673.number-of-longest-increasing-subsequence.js diff --git a/674-longest-continuous-increasing-subsequence.js b/674.longest-continuous-increasing-subsequence.js old mode 100755 new mode 100644 similarity index 94% rename from 674-longest-continuous-increasing-subsequence.js rename to 674.longest-continuous-increasing-subsequence.js index 730d31e0..9f37c6ca --- a/674-longest-continuous-increasing-subsequence.js +++ b/674.longest-continuous-increasing-subsequence.js @@ -1,18 +1,18 @@ -/** - * @param {number[]} nums - * @return {number} - */ -const findLengthOfLCIS = function(nums) { - if (nums.length == 1) { - return 1; - } - let ans = 0, - anchor = 0; - for (let i = 1; i < nums.length; i++) { - if (nums[i - 1] >= nums[i]) { - anchor = i; - } - ans = Math.max(ans, i - anchor + 1); - } - return ans; -}; +/** + * @param {number[]} nums + * @return {number} + */ +const findLengthOfLCIS = function(nums) { + if (nums.length == 1) { + return 1; + } + let ans = 0, + anchor = 0; + for (let i = 1; i < nums.length; i++) { + if (nums[i - 1] >= nums[i]) { + anchor = i; + } + ans = Math.max(ans, i - anchor + 1); + } + return ans; +}; diff --git a/675-cut-off-trees-for-golf-event.js b/675.cut-off-trees-for-golf-event.js similarity index 100% rename from 675-cut-off-trees-for-golf-event.js rename to 675.cut-off-trees-for-golf-event.js diff --git a/676-implement-magic-dictionary.js b/676.implement-magic-dictionary.js old mode 100755 new mode 100644 similarity index 95% rename from 676-implement-magic-dictionary.js rename to 676.implement-magic-dictionary.js index 18e27e69..e24c6265 --- a/676-implement-magic-dictionary.js +++ b/676.implement-magic-dictionary.js @@ -1,53 +1,53 @@ -/** - * Initialize your data structure here. - */ -const MagicDictionary = function(dict) { - this.dict = []; -}; - -/** - * Build a dictionary through a list of words - * @param {string[]} dict - * @return {void} - */ -MagicDictionary.prototype.buildDict = function(dict) { - this.dict = dict; -}; - -/** - * Returns if there is any word in the trie that equals to the given word after modifying exactly one character - * @param {string} word - * @return {boolean} - */ -MagicDictionary.prototype.search = function(word) { - return check(word, this.dict); -}; - -/** - * Your MagicDictionary object will be instantiated and called as such: - * var obj = Object.create(MagicDictionary).createNew() - * obj.buildDict(dict) - * var param_2 = obj.search(word) - */ - -function check(str, arr) { - let el; - for (let i = 0; i < arr.length; i++) { - el = arr[i]; - if (el.length === str.length && oneCharDiff(el, str)) { - return true; - } - } - - return false; -} - -function oneCharDiff(str1, str2) { - let diff = 0; - for (let i = 0; i < str1.length; i++) { - if (str1[i] !== str2[i]) { - diff += 1; - } - } - return diff === 1 ? true : false; -} +/** + * Initialize your data structure here. + */ +const MagicDictionary = function(dict) { + this.dict = []; +}; + +/** + * Build a dictionary through a list of words + * @param {string[]} dict + * @return {void} + */ +MagicDictionary.prototype.buildDict = function(dict) { + this.dict = dict; +}; + +/** + * Returns if there is any word in the trie that equals to the given word after modifying exactly one character + * @param {string} word + * @return {boolean} + */ +MagicDictionary.prototype.search = function(word) { + return check(word, this.dict); +}; + +/** + * Your MagicDictionary object will be instantiated and called as such: + * var obj = Object.create(MagicDictionary).createNew() + * obj.buildDict(dict) + * var param_2 = obj.search(word) + */ + +function check(str, arr) { + let el; + for (let i = 0; i < arr.length; i++) { + el = arr[i]; + if (el.length === str.length && oneCharDiff(el, str)) { + return true; + } + } + + return false; +} + +function oneCharDiff(str1, str2) { + let diff = 0; + for (let i = 0; i < str1.length; i++) { + if (str1[i] !== str2[i]) { + diff += 1; + } + } + return diff === 1 ? true : false; +} diff --git a/677-map-sum-pairs.js b/677.map-sum-pairs.js old mode 100755 new mode 100644 similarity index 94% rename from 677-map-sum-pairs.js rename to 677.map-sum-pairs.js index 07e17464..4059a485 --- a/677-map-sum-pairs.js +++ b/677.map-sum-pairs.js @@ -1,36 +1,36 @@ -/** - * Initialize your data structure here. - */ -const MapSum = function() { - this.hash = {}; -}; - -/** - * @param {string} key - * @param {number} val - * @return {void} - */ -MapSum.prototype.insert = function(key, val) { - this.hash[key] = val; -}; - -/** - * @param {string} prefix - * @return {number} - */ -MapSum.prototype.sum = function(prefix) { - let res = 0; - Object.keys(this.hash).forEach(el => { - if (el.indexOf(prefix) === 0) { - res += this.hash[el]; - } - }); - return res; -}; - -/** - * Your MapSum object will be instantiated and called as such: - * var obj = Object.create(MapSum).createNew() - * obj.insert(key,val) - * var param_2 = obj.sum(prefix) - */ +/** + * Initialize your data structure here. + */ +const MapSum = function() { + this.hash = {}; +}; + +/** + * @param {string} key + * @param {number} val + * @return {void} + */ +MapSum.prototype.insert = function(key, val) { + this.hash[key] = val; +}; + +/** + * @param {string} prefix + * @return {number} + */ +MapSum.prototype.sum = function(prefix) { + let res = 0; + Object.keys(this.hash).forEach(el => { + if (el.indexOf(prefix) === 0) { + res += this.hash[el]; + } + }); + return res; +}; + +/** + * Your MapSum object will be instantiated and called as such: + * var obj = Object.create(MapSum).createNew() + * obj.insert(key,val) + * var param_2 = obj.sum(prefix) + */ diff --git a/678-valid-parenthesis-string.js b/678.valid-parenthesis-string.js similarity index 100% rename from 678-valid-parenthesis-string.js rename to 678.valid-parenthesis-string.js diff --git a/679-24-game.js b/679.24-game.js similarity index 100% rename from 679-24-game.js rename to 679.24-game.js diff --git a/68-text-justification.js b/68.text-justification.js old mode 100755 new mode 100644 similarity index 95% rename from 68-text-justification.js rename to 68.text-justification.js index d6213262..e38a4d85 --- a/68-text-justification.js +++ b/68.text-justification.js @@ -1,35 +1,35 @@ -/** - * @param {string[]} words - * @param {number} maxWidth - * @return {string[]} - */ -const fullJustify = function(words, maxWidth) { - const res = [] - let curRow = [] - let numOfChars = 0 - - for (let w of words) { - if (numOfChars + w.length + curRow.length > maxWidth) { - for(let i = 0; i < maxWidth - numOfChars; i++) { - if(curRow.length === 1) { - curRow[0] += ' ' - } else { - curRow[i % (curRow.length - 1)] += ' ' - } - } - res.push(curRow.join('')) - curRow = [] - numOfChars = 0 - } - curRow.push(w) - numOfChars += w.length - } - - const numOfSpace = maxWidth - numOfChars - (curRow.length - 1) - let tail = '' - for(let i = 0; i < numOfSpace; i++) tail += ' ' - res.push(curRow.join(' ') + tail) - - return res -}; - +/** + * @param {string[]} words + * @param {number} maxWidth + * @return {string[]} + */ +const fullJustify = function(words, maxWidth) { + const res = [] + let curRow = [] + let numOfChars = 0 + + for (let w of words) { + if (numOfChars + w.length + curRow.length > maxWidth) { + for(let i = 0; i < maxWidth - numOfChars; i++) { + if(curRow.length === 1) { + curRow[0] += ' ' + } else { + curRow[i % (curRow.length - 1)] += ' ' + } + } + res.push(curRow.join('')) + curRow = [] + numOfChars = 0 + } + curRow.push(w) + numOfChars += w.length + } + + const numOfSpace = maxWidth - numOfChars - (curRow.length - 1) + let tail = '' + for(let i = 0; i < numOfSpace; i++) tail += ' ' + res.push(curRow.join(' ') + tail) + + return res +}; + diff --git a/680-valid-palindrome-ii.js b/680.valid-palindrome-ii.js similarity index 100% rename from 680-valid-palindrome-ii.js rename to 680.valid-palindrome-ii.js diff --git a/681-next-closest-time.js b/681.next-closest-time.js similarity index 100% rename from 681-next-closest-time.js rename to 681.next-closest-time.js diff --git a/682-baseball-game.js b/682.baseball-game.js old mode 100755 new mode 100644 similarity index 95% rename from 682-baseball-game.js rename to 682.baseball-game.js index 5357131d..90a1d3e8 --- a/682-baseball-game.js +++ b/682.baseball-game.js @@ -1,52 +1,52 @@ -/** - * @param {string[]} ops - * @return {number} - */ -const calPoints = function(ops) { - const opArr = ["C", "D", "+"]; - const arr = []; - ops.forEach((el, idx) => { - const item = { - value: 0, - valid: true - }; - switch (el) { - case "C": - findValid(arr, idx, 1).forEach(el => { - el.value = 0; - el.valid = false; - }); - item.valid = false; - break; - case "D": - item.value = findValid(arr, idx, 1)[0].value * 2; - break; - case "+": - item.value = findValid(arr, idx, 2).reduce( - (ac, ele) => ac + ele.value, - 0 - ); - break; - default: - item.value = +el; - break; - } - arr.push(item); - }); - return arr.reduce((ac, el) => ac + el.value, 0); -}; - -function findValid(arr, idx, backStep) { - const res = []; - while (backStep > 0 && idx - 1 >= 0) { - if (arr[idx - 1].valid === true) { - backStep -= 1; - res.push(arr[idx - 1]); - } - idx -= 1; - } - return res; -} - -console.log(calPoints(["5", "2", "C", "D", "+"])); -console.log(calPoints(["5", "-2", "4", "C", "D", "9", "+", "+"])); +/** + * @param {string[]} ops + * @return {number} + */ +const calPoints = function(ops) { + const opArr = ["C", "D", "+"]; + const arr = []; + ops.forEach((el, idx) => { + const item = { + value: 0, + valid: true + }; + switch (el) { + case "C": + findValid(arr, idx, 1).forEach(el => { + el.value = 0; + el.valid = false; + }); + item.valid = false; + break; + case "D": + item.value = findValid(arr, idx, 1)[0].value * 2; + break; + case "+": + item.value = findValid(arr, idx, 2).reduce( + (ac, ele) => ac + ele.value, + 0 + ); + break; + default: + item.value = +el; + break; + } + arr.push(item); + }); + return arr.reduce((ac, el) => ac + el.value, 0); +}; + +function findValid(arr, idx, backStep) { + const res = []; + while (backStep > 0 && idx - 1 >= 0) { + if (arr[idx - 1].valid === true) { + backStep -= 1; + res.push(arr[idx - 1]); + } + idx -= 1; + } + return res; +} + +console.log(calPoints(["5", "2", "C", "D", "+"])); +console.log(calPoints(["5", "-2", "4", "C", "D", "9", "+", "+"])); diff --git a/683-k-empty-slots.js b/683.k-empty-slots.js similarity index 100% rename from 683-k-empty-slots.js rename to 683.k-empty-slots.js diff --git a/684-redundant-connection.js b/684.redundant-connection.js similarity index 100% rename from 684-redundant-connection.js rename to 684.redundant-connection.js diff --git a/685-redundant-connection-ii.js b/685.redundant-connection-ii.js similarity index 100% rename from 685-redundant-connection-ii.js rename to 685.redundant-connection-ii.js diff --git a/686-repeated-string-match.js b/686.repeated-string-match.js similarity index 100% rename from 686-repeated-string-match.js rename to 686.repeated-string-match.js diff --git a/687-longest-univalue-path.js b/687.longest-univalue-path.js similarity index 100% rename from 687-longest-univalue-path.js rename to 687.longest-univalue-path.js diff --git a/688-knight-probability-in-chessboard.js b/688.knight-probability-in-chessboard.js similarity index 100% rename from 688-knight-probability-in-chessboard.js rename to 688.knight-probability-in-chessboard.js diff --git a/689-maximum-sum-of-3-non-overlapping-subarrays.js b/689.maximum-sum-of-3-non-overlapping-subarrays.js similarity index 100% rename from 689-maximum-sum-of-3-non-overlapping-subarrays.js rename to 689.maximum-sum-of-3-non-overlapping-subarrays.js diff --git a/69-sqrt(x).js b/69.sqrt(x).js old mode 100755 new mode 100644 similarity index 94% rename from 69-sqrt(x).js rename to 69.sqrt(x).js index dfaeb2a4..b1e8913e --- a/69-sqrt(x).js +++ b/69.sqrt(x).js @@ -1,17 +1,17 @@ -/** - * @param {number} x - * @return {number} - */ -const mySqrt = function(x) { - let left = 0, right = x; - while (left < right) { - let mid = right - ((right - left) >> 1); - if (mid * mid > x) { - right = mid - 1; - } else { - left = mid; - } - } - return left; -}; - +/** + * @param {number} x + * @return {number} + */ +const mySqrt = function(x) { + let left = 0, right = x; + while (left < right) { + let mid = right - ((right - left) >> 1); + if (mid * mid > x) { + right = mid - 1; + } else { + left = mid; + } + } + return left; +}; + diff --git a/690-employee-importance.js b/690.employee-importance.js similarity index 100% rename from 690-employee-importance.js rename to 690.employee-importance.js diff --git a/691-stickers-to-spell-word.js b/691.stickers-to-spell-word.js similarity index 100% rename from 691-stickers-to-spell-word.js rename to 691.stickers-to-spell-word.js diff --git a/692-top-k-frequent-words.js b/692.top-k-frequent-words.js similarity index 100% rename from 692-top-k-frequent-words.js rename to 692.top-k-frequent-words.js diff --git a/693-binary-number-with-alternating-bits.js b/693.binary-number-with-alternating-bits.js old mode 100755 new mode 100644 similarity index 100% rename from 693-binary-number-with-alternating-bits.js rename to 693.binary-number-with-alternating-bits.js diff --git a/694-number-of-distinct-islands.js b/694.number-of-distinct-islands.js similarity index 100% rename from 694-number-of-distinct-islands.js rename to 694.number-of-distinct-islands.js diff --git a/695-max-area-of-island.js b/695.max-area-of-island.js similarity index 100% rename from 695-max-area-of-island.js rename to 695.max-area-of-island.js diff --git a/697-degree-of-an-array.js b/697.degree-of-an-array.js old mode 100755 new mode 100644 similarity index 96% rename from 697-degree-of-an-array.js rename to 697.degree-of-an-array.js index 9831e67f..b498559a --- a/697-degree-of-an-array.js +++ b/697.degree-of-an-array.js @@ -1,26 +1,26 @@ -/** - * @param {number[]} nums - * @return {number} - */ -const findShortestSubArray = function(nums) { - const left = {}; - const right = {}; - const count = {}; - - for (let i = 0; i < nums.length; i++) { - if (!left.hasOwnProperty(nums[i])) { - left[nums[i]] = i; - } - right[nums[i]] = i; - count[nums[i]] = count[nums[i]] ? count[nums[i]] + 1 : 1; - } - const degree = Math.max(...Object.keys(count).map(el => count[el])); - let res = nums.length; - for (let el in count) { - if (count.hasOwnProperty(el) && count[el] === degree) { - res = Math.min(res, right[el] - left[el] + 1); - } - } - - return res; -}; +/** + * @param {number[]} nums + * @return {number} + */ +const findShortestSubArray = function(nums) { + const left = {}; + const right = {}; + const count = {}; + + for (let i = 0; i < nums.length; i++) { + if (!left.hasOwnProperty(nums[i])) { + left[nums[i]] = i; + } + right[nums[i]] = i; + count[nums[i]] = count[nums[i]] ? count[nums[i]] + 1 : 1; + } + const degree = Math.max(...Object.keys(count).map(el => count[el])); + let res = nums.length; + for (let el in count) { + if (count.hasOwnProperty(el) && count[el] === degree) { + res = Math.min(res, right[el] - left[el] + 1); + } + } + + return res; +}; diff --git a/698-partition-to-k-equal-sum-subsets.js b/698.partition-to-k-equal-sum-subsets.js similarity index 100% rename from 698-partition-to-k-equal-sum-subsets.js rename to 698.partition-to-k-equal-sum-subsets.js diff --git a/699-falling-squares.js b/699.falling-squares.js similarity index 100% rename from 699-falling-squares.js rename to 699.falling-squares.js diff --git a/7-reverse-integer.js b/7.reverse-integer.js old mode 100755 new mode 100644 similarity index 95% rename from 7-reverse-integer.js rename to 7.reverse-integer.js index c628be7e..e694c15c --- a/7-reverse-integer.js +++ b/7.reverse-integer.js @@ -1,19 +1,19 @@ -/** - * @param {number} x - * @return {number} - */ -const reverse = function (x) { - let res = 0, tail, newResult - const low = -Math.pow(2, 31), high = Math.pow(2, 31) - while(x !== 0) { - tail = x % 10 - newResult = res * 10 + tail - // if((newResult - tail) / 10 !== res) return 0 - if(newResult < low || newResult >= high) return 0 - res = newResult - x = ~~(x / 10) - } - - return res -}; - +/** + * @param {number} x + * @return {number} + */ +const reverse = function (x) { + let res = 0, tail, newResult + const low = -Math.pow(2, 31), high = Math.pow(2, 31) + while(x !== 0) { + tail = x % 10 + newResult = res * 10 + tail + // if((newResult - tail) / 10 !== res) return 0 + if(newResult < low || newResult >= high) return 0 + res = newResult + x = ~~(x / 10) + } + + return res +}; + diff --git a/70-climbing-stairs.js b/70.climbing-stairs.js old mode 100755 new mode 100644 similarity index 94% rename from 70-climbing-stairs.js rename to 70.climbing-stairs.js index 29760510..01e7205e --- a/70-climbing-stairs.js +++ b/70.climbing-stairs.js @@ -1,25 +1,25 @@ -/** - * @param {number} n - * @return {number} - */ -const climbStairs = function(n) { - const hash = {}; - return single(n, hash); -}; - -function single(i, hash) { - if (hash.hasOwnProperty(i)) { - return hash[i]; - } - if (i === 1) { - hash[1] = 1; - return 1; - } - if (i === 2) { - hash[2] = 2; - return 2; - } - hash[i] = single(i - 1, hash) + single(i - 2, hash); - return hash[i]; -} - +/** + * @param {number} n + * @return {number} + */ +const climbStairs = function(n) { + const hash = {}; + return single(n, hash); +}; + +function single(i, hash) { + if (hash.hasOwnProperty(i)) { + return hash[i]; + } + if (i === 1) { + hash[1] = 1; + return 1; + } + if (i === 2) { + hash[2] = 2; + return 2; + } + hash[i] = single(i - 1, hash) + single(i - 2, hash); + return hash[i]; +} + diff --git a/700-search-in-a-binary-search-tree.js b/700.search-in-a-binary-search-tree.js old mode 100755 new mode 100644 similarity index 96% rename from 700-search-in-a-binary-search-tree.js rename to 700.search-in-a-binary-search-tree.js index 7b4b24df..6e53066d --- a/700-search-in-a-binary-search-tree.js +++ b/700.search-in-a-binary-search-tree.js @@ -1,19 +1,19 @@ -/** - * Definition for a binary tree node. - * function TreeNode(val, left, right) { - * this.val = (val===undefined ? 0 : val) - * this.left = (left===undefined ? null : left) - * this.right = (right===undefined ? null : right) - * } - */ -/** - * @param {TreeNode} root - * @param {number} val - * @return {TreeNode} - */ -const searchBST = function (root, val) { - if (!root || root.val === val) { - return root - } - return root.val < val ? searchBST(root.right, val) : searchBST(root.left, val) -} +/** + * Definition for a binary tree node. + * function TreeNode(val, left, right) { + * this.val = (val===undefined ? 0 : val) + * this.left = (left===undefined ? null : left) + * this.right = (right===undefined ? null : right) + * } + */ +/** + * @param {TreeNode} root + * @param {number} val + * @return {TreeNode} + */ +const searchBST = function (root, val) { + if (!root || root.val === val) { + return root + } + return root.val < val ? searchBST(root.right, val) : searchBST(root.left, val) +} diff --git a/701-insert-into-a-binary-search-tree.js b/701.insert-into-a-binary-search-tree.js similarity index 100% rename from 701-insert-into-a-binary-search-tree.js rename to 701.insert-into-a-binary-search-tree.js diff --git a/702-search-in-a-sorted-array-of-unknown-size.js b/702.search-in-a-sorted-array-of-unknown-size.js similarity index 100% rename from 702-search-in-a-sorted-array-of-unknown-size.js rename to 702.search-in-a-sorted-array-of-unknown-size.js diff --git a/703-kth-largest-element-in-a-stream.js b/703.kth-largest-element-in-a-stream.js similarity index 100% rename from 703-kth-largest-element-in-a-stream.js rename to 703.kth-largest-element-in-a-stream.js diff --git a/704-binary-search.js b/704.binary-search.js similarity index 100% rename from 704-binary-search.js rename to 704.binary-search.js diff --git a/705-design-hashset.js b/705.design-hashset.js similarity index 100% rename from 705-design-hashset.js rename to 705.design-hashset.js diff --git a/706-design-hashmap.js b/706.design-hashmap.js similarity index 100% rename from 706-design-hashmap.js rename to 706.design-hashmap.js diff --git a/707-design-linked-list.js b/707.design-linked-list.js similarity index 100% rename from 707-design-linked-list.js rename to 707.design-linked-list.js diff --git a/708-insert-into-a-sorted-circular-linked-list.js b/708.insert-into-a-sorted-circular-linked-list.js similarity index 100% rename from 708-insert-into-a-sorted-circular-linked-list.js rename to 708.insert-into-a-sorted-circular-linked-list.js diff --git a/71-simplify-path.js b/71.simplify-path.js similarity index 100% rename from 71-simplify-path.js rename to 71.simplify-path.js diff --git a/710-random-pick-with-blacklist.js b/710.random-pick-with-blacklist.js similarity index 100% rename from 710-random-pick-with-blacklist.js rename to 710.random-pick-with-blacklist.js diff --git a/711-number-of-distinct-islands-ii.js b/711.number-of-distinct-islands-ii.js similarity index 100% rename from 711-number-of-distinct-islands-ii.js rename to 711.number-of-distinct-islands-ii.js diff --git a/712-minimum-ASCII-delete-sum-for-two-strings.js b/712.minimum-ASCII-delete-sum-for-two-strings.js old mode 100755 new mode 100644 similarity index 96% rename from 712-minimum-ASCII-delete-sum-for-two-strings.js rename to 712.minimum-ASCII-delete-sum-for-two-strings.js index 033de353..bc250cb4 --- a/712-minimum-ASCII-delete-sum-for-two-strings.js +++ b/712.minimum-ASCII-delete-sum-for-two-strings.js @@ -1,36 +1,36 @@ -/** - * @param {string} s1 - * @param {string} s2 - * @return {number} - */ -const minimumDeleteSum = function(s1, s2) { - const l1 = s1.length; - const l2 = s2.length; - const dp = []; - for (let i = 0; i <= l1; i++) { - dp[i] = []; - } - let sum = 0; - for (let i = 0; i <= l1; i++) { - for (let j = 0; j <= l2; j++) { - if (i === 0 || j === 0) { - sum = 0; - for (let k = 0; k < Math.max(i, j); k++) { - sum += i > j ? s1.charCodeAt(k) : s2.charCodeAt(k); - } - dp[i][j] = sum; - } else { - if (s1[i - 1] === s2[j - 1]) { - dp[i][j] = dp[i - 1][j - 1]; - } else { - dp[i][j] = Math.min( - s1.charCodeAt(i - 1) + dp[i - 1][j], - s2.charCodeAt(j - 1) + dp[i][j - 1], - s1.charCodeAt(i - 1) + s2.charCodeAt(j - 1) + dp[i - 1][j - 1] - ); - } - } - } - } - return dp[l1][l2]; -}; +/** + * @param {string} s1 + * @param {string} s2 + * @return {number} + */ +const minimumDeleteSum = function(s1, s2) { + const l1 = s1.length; + const l2 = s2.length; + const dp = []; + for (let i = 0; i <= l1; i++) { + dp[i] = []; + } + let sum = 0; + for (let i = 0; i <= l1; i++) { + for (let j = 0; j <= l2; j++) { + if (i === 0 || j === 0) { + sum = 0; + for (let k = 0; k < Math.max(i, j); k++) { + sum += i > j ? s1.charCodeAt(k) : s2.charCodeAt(k); + } + dp[i][j] = sum; + } else { + if (s1[i - 1] === s2[j - 1]) { + dp[i][j] = dp[i - 1][j - 1]; + } else { + dp[i][j] = Math.min( + s1.charCodeAt(i - 1) + dp[i - 1][j], + s2.charCodeAt(j - 1) + dp[i][j - 1], + s1.charCodeAt(i - 1) + s2.charCodeAt(j - 1) + dp[i - 1][j - 1] + ); + } + } + } + } + return dp[l1][l2]; +}; diff --git a/713-subarray-product-less-than-k.js b/713.subarray-product-less-than-k.js similarity index 100% rename from 713-subarray-product-less-than-k.js rename to 713.subarray-product-less-than-k.js diff --git a/714-best-time-to-buy-and-sell-stock-with-transaction-fee.js b/714.best-time-to-buy-and-sell-stock-with-transaction-fee.js old mode 100755 new mode 100644 similarity index 95% rename from 714-best-time-to-buy-and-sell-stock-with-transaction-fee.js rename to 714.best-time-to-buy-and-sell-stock-with-transaction-fee.js index 4ab03237..38f4a617 --- a/714-best-time-to-buy-and-sell-stock-with-transaction-fee.js +++ b/714.best-time-to-buy-and-sell-stock-with-transaction-fee.js @@ -1,16 +1,16 @@ -/** - * @param {number[]} prices - * @param {number} fee - * @return {number} - */ -const maxProfit = function(prices, fee) { - let cash = 0, - hold = -prices[0]; - for (let i = 1; i < prices.length; i++) { - cash = Math.max(cash, hold + prices[i] - fee); - hold = Math.max(hold, cash - prices[i]); - } - return cash; -}; - -console.log(maxProfit([1, 3, 2, 8, 4, 9], 2)); +/** + * @param {number[]} prices + * @param {number} fee + * @return {number} + */ +const maxProfit = function(prices, fee) { + let cash = 0, + hold = -prices[0]; + for (let i = 1; i < prices.length; i++) { + cash = Math.max(cash, hold + prices[i] - fee); + hold = Math.max(hold, cash - prices[i]); + } + return cash; +}; + +console.log(maxProfit([1, 3, 2, 8, 4, 9], 2)); diff --git a/715-range-module.js b/715.range-module.js similarity index 100% rename from 715-range-module.js rename to 715.range-module.js diff --git a/716-max-stack.js b/716.max-stack.js similarity index 100% rename from 716-max-stack.js rename to 716.max-stack.js diff --git a/717-1-bit-and-2-bit-characters.js b/717.1-bit-and-2-bit-characters.js old mode 100755 new mode 100644 similarity index 96% rename from 717-1-bit-and-2-bit-characters.js rename to 717.1-bit-and-2-bit-characters.js index 44c488b8..e0e5b084 --- a/717-1-bit-and-2-bit-characters.js +++ b/717.1-bit-and-2-bit-characters.js @@ -1,12 +1,12 @@ -/** - * @param {number[]} bits - * @return {boolean} - */ -const isOneBitCharacter = function(bits) { - let ones = 0; - //Starting from one but last, as last one is always 0. - for (let i = bits.length - 2; i >= 0 && bits[i] != 0; i--) { - ones++; - } - return ones % 2 > 0 ? false : true; -}; +/** + * @param {number[]} bits + * @return {boolean} + */ +const isOneBitCharacter = function(bits) { + let ones = 0; + //Starting from one but last, as last one is always 0. + for (let i = bits.length - 2; i >= 0 && bits[i] != 0; i--) { + ones++; + } + return ones % 2 > 0 ? false : true; +}; diff --git a/718-maximum-length-of-repeated-subarray.js b/718.maximum-length-of-repeated-subarray.js old mode 100755 new mode 100644 similarity index 95% rename from 718-maximum-length-of-repeated-subarray.js rename to 718.maximum-length-of-repeated-subarray.js index 7db5b0ba..314caf10 --- a/718-maximum-length-of-repeated-subarray.js +++ b/718.maximum-length-of-repeated-subarray.js @@ -1,21 +1,21 @@ -/** - * @param {number[]} A - * @param {number[]} B - * @return {number} - */ -const findLength = function(A, B) { - let ans = 0; - let memo = []; - for (let i = 0; i < A.length + 1; i++) { - memo[i] = Array(B.length + 1).fill(0); - } - for (let i = A.length - 1; i >= 0; --i) { - for (let j = B.length - 1; j >= 0; --j) { - if (A[i] == B[j]) { - memo[i][j] = memo[i + 1][j + 1] + 1; - if (ans < memo[i][j]) ans = memo[i][j]; - } - } - } - return ans; -}; +/** + * @param {number[]} A + * @param {number[]} B + * @return {number} + */ +const findLength = function(A, B) { + let ans = 0; + let memo = []; + for (let i = 0; i < A.length + 1; i++) { + memo[i] = Array(B.length + 1).fill(0); + } + for (let i = A.length - 1; i >= 0; --i) { + for (let j = B.length - 1; j >= 0; --j) { + if (A[i] == B[j]) { + memo[i][j] = memo[i + 1][j + 1] + 1; + if (ans < memo[i][j]) ans = memo[i][j]; + } + } + } + return ans; +}; diff --git a/719-find-k-th-smallest-pair-distance.js b/719.find-k-th-smallest-pair-distance.js similarity index 100% rename from 719-find-k-th-smallest-pair-distance.js rename to 719.find-k-th-smallest-pair-distance.js diff --git a/72-edit-distance.js b/72.edit-distance.js similarity index 100% rename from 72-edit-distance.js rename to 72.edit-distance.js diff --git a/720-longest-word-in-dictionary.js b/720.longest-word-in-dictionary.js similarity index 100% rename from 720-longest-word-in-dictionary.js rename to 720.longest-word-in-dictionary.js diff --git a/721-accounts-merge.js b/721.accounts-merge.js similarity index 100% rename from 721-accounts-merge.js rename to 721.accounts-merge.js diff --git a/722-remove-comments.js b/722.remove-comments.js similarity index 100% rename from 722-remove-comments.js rename to 722.remove-comments.js diff --git a/723-candy-crush.js b/723.candy-crush.js similarity index 100% rename from 723-candy-crush.js rename to 723.candy-crush.js diff --git a/724-find-pivot-index.js b/724.find-pivot-index.js similarity index 100% rename from 724-find-pivot-index.js rename to 724.find-pivot-index.js diff --git a/725-split-linked-list-in-parts.js b/725.split-linked-list-in-parts.js old mode 100755 new mode 100644 similarity index 95% rename from 725-split-linked-list-in-parts.js rename to 725.split-linked-list-in-parts.js index 2762c3f9..a476a66f --- a/725-split-linked-list-in-parts.js +++ b/725.split-linked-list-in-parts.js @@ -1,37 +1,37 @@ -/** - * Definition for singly-linked list. - * function ListNode(val) { - * this.val = val; - * this.next = null; - * } - */ -/** - * @param {ListNode} root - * @param {number} k - * @return {ListNode[]} - */ -const splitListToParts = function(root, k) { - let cur = root; - let N = 0; - while (cur != null) { - cur = cur.next; - N++; - } - let width = Math.floor(N / k), - rem = N % k; - let ans = []; - cur = root; - for (let i = 0; i < k; ++i) { - let head = cur; - for (let j = 0; j < width + (i < rem ? 1 : 0) - 1; ++j) { - if (cur != null) cur = cur.next; - } - if (cur != null) { - let prev = cur; - cur = cur.next; - prev.next = null; - } - ans[i] = head; - } - return ans; -}; +/** + * Definition for singly-linked list. + * function ListNode(val) { + * this.val = val; + * this.next = null; + * } + */ +/** + * @param {ListNode} root + * @param {number} k + * @return {ListNode[]} + */ +const splitListToParts = function(root, k) { + let cur = root; + let N = 0; + while (cur != null) { + cur = cur.next; + N++; + } + let width = Math.floor(N / k), + rem = N % k; + let ans = []; + cur = root; + for (let i = 0; i < k; ++i) { + let head = cur; + for (let j = 0; j < width + (i < rem ? 1 : 0) - 1; ++j) { + if (cur != null) cur = cur.next; + } + if (cur != null) { + let prev = cur; + cur = cur.next; + prev.next = null; + } + ans[i] = head; + } + return ans; +}; diff --git a/726-number-of-atoms.js b/726.number-of-atoms.js similarity index 100% rename from 726-number-of-atoms.js rename to 726.number-of-atoms.js diff --git a/727-minimum-window-subsequence.js b/727.minimum-window-subsequence.js similarity index 100% rename from 727-minimum-window-subsequence.js rename to 727.minimum-window-subsequence.js diff --git a/729-my-calendar-i.js b/729.my-calendar-i.js similarity index 100% rename from 729-my-calendar-i.js rename to 729.my-calendar-i.js diff --git a/73-set-matrix-zeroes.js b/73.set-matrix-zeroes.js similarity index 100% rename from 73-set-matrix-zeroes.js rename to 73.set-matrix-zeroes.js diff --git a/730-count-different-palindromic-subsequences.js b/730.count-different-palindromic-subsequences.js similarity index 100% rename from 730-count-different-palindromic-subsequences.js rename to 730.count-different-palindromic-subsequences.js diff --git a/731-my-calendar-ii.js b/731.my-calendar-ii.js similarity index 100% rename from 731-my-calendar-ii.js rename to 731.my-calendar-ii.js diff --git a/732-my-calendar-iii.js b/732.my-calendar-iii.js similarity index 100% rename from 732-my-calendar-iii.js rename to 732.my-calendar-iii.js diff --git a/733-flood-fill.js b/733.flood-fill.js similarity index 100% rename from 733-flood-fill.js rename to 733.flood-fill.js diff --git a/734-sentence-similarity.js b/734.sentence-similarity.js similarity index 100% rename from 734-sentence-similarity.js rename to 734.sentence-similarity.js diff --git a/735-asteroid-collision.js b/735.asteroid-collision.js similarity index 100% rename from 735-asteroid-collision.js rename to 735.asteroid-collision.js diff --git a/736-parse-lisp-expression.js b/736.parse-lisp-expression.js similarity index 100% rename from 736-parse-lisp-expression.js rename to 736.parse-lisp-expression.js diff --git a/737-sentence-similarity-ii.js b/737.sentence-similarity-ii.js similarity index 100% rename from 737-sentence-similarity-ii.js rename to 737.sentence-similarity-ii.js diff --git a/738-monotone-increasing-digits.js b/738.monotone-increasing-digits.js similarity index 100% rename from 738-monotone-increasing-digits.js rename to 738.monotone-increasing-digits.js diff --git a/739-daily-temperatures.js b/739.daily-temperatures.js similarity index 100% rename from 739-daily-temperatures.js rename to 739.daily-temperatures.js diff --git a/74-search-a-2d-matrix.js b/74.search-a-2d-matrix.js similarity index 100% rename from 74-search-a-2d-matrix.js rename to 74.search-a-2d-matrix.js diff --git a/740-delete-and-earn.js b/740.delete-and-earn.js similarity index 100% rename from 740-delete-and-earn.js rename to 740.delete-and-earn.js diff --git a/741-cherry-pickup.js b/741.cherry-pickup.js similarity index 100% rename from 741-cherry-pickup.js rename to 741.cherry-pickup.js diff --git a/742-closest-leaf-in-a-binary-tree.js b/742.closest-leaf-in-a-binary-tree.js similarity index 100% rename from 742-closest-leaf-in-a-binary-tree.js rename to 742.closest-leaf-in-a-binary-tree.js diff --git a/743-network-delay-time.js b/743.network-delay-time.js similarity index 100% rename from 743-network-delay-time.js rename to 743.network-delay-time.js diff --git a/744-find-smallest-letter-greater-than-target.js b/744.find-smallest-letter-greater-than-target.js similarity index 100% rename from 744-find-smallest-letter-greater-than-target.js rename to 744.find-smallest-letter-greater-than-target.js diff --git a/745-prefix-and-suffix-search.js b/745.prefix-and-suffix-search.js similarity index 100% rename from 745-prefix-and-suffix-search.js rename to 745.prefix-and-suffix-search.js diff --git a/746-min-cost-climbing-stairs.js b/746.min-cost-climbing-stairs.js old mode 100755 new mode 100644 similarity index 95% rename from 746-min-cost-climbing-stairs.js rename to 746.min-cost-climbing-stairs.js index b2f7b419..3d4f6161 --- a/746-min-cost-climbing-stairs.js +++ b/746.min-cost-climbing-stairs.js @@ -1,14 +1,14 @@ -/** - * @param {number[]} cost - * @return {number} - */ -const minCostClimbingStairs = function(cost) { - let f1 = cost[0]; - let f2 = cost[1]; - for (let i = 2; i < cost.length; i++) { - let f_cur = cost[i] + Math.min(f1, f2); - f1 = f2; - f2 = f_cur; - } - return Math.min(f1, f2); -}; +/** + * @param {number[]} cost + * @return {number} + */ +const minCostClimbingStairs = function(cost) { + let f1 = cost[0]; + let f2 = cost[1]; + for (let i = 2; i < cost.length; i++) { + let f_cur = cost[i] + Math.min(f1, f2); + f1 = f2; + f2 = f_cur; + } + return Math.min(f1, f2); +}; diff --git a/747-largest-number-at-least-twice-of-others.js b/747.largest-number-at-least-twice-of-others.js similarity index 100% rename from 747-largest-number-at-least-twice-of-others.js rename to 747.largest-number-at-least-twice-of-others.js diff --git a/748-shortest-completing-word.js b/748.shortest-completing-word.js similarity index 100% rename from 748-shortest-completing-word.js rename to 748.shortest-completing-word.js diff --git a/749-contain-virus.js b/749.contain-virus.js similarity index 100% rename from 749-contain-virus.js rename to 749.contain-virus.js diff --git a/75-sort-colors.js b/75.sort-colors.js similarity index 100% rename from 75-sort-colors.js rename to 75.sort-colors.js diff --git a/750-number-of-corner-rectangles.js b/750.number-of-corner-rectangles.js similarity index 100% rename from 750-number-of-corner-rectangles.js rename to 750.number-of-corner-rectangles.js diff --git a/751-ip-to-cidr.js b/751.ip-to-cidr.js similarity index 100% rename from 751-ip-to-cidr.js rename to 751.ip-to-cidr.js diff --git a/752-open-the-lock.js b/752.open-the-lock.js old mode 100755 new mode 100644 similarity index 95% rename from 752-open-the-lock.js rename to 752.open-the-lock.js index 26747c8c..53c151e0 --- a/752-open-the-lock.js +++ b/752.open-the-lock.js @@ -1,56 +1,56 @@ -/** - * @param {string[]} deadends - * @param {string} target - * @return {number} - */ -const openLock = function(deadends, target, count = 0) { - let deadSet = new Set(deadends); - let visited = new Set(); - if (deadSet.has("0000")) { - return -1; - } - - let q = []; - q.push("0000"); - visited.add("0000"); - - let steps = 0; - while (q.length > 0) { - let len = q.length; - - for (let j = 0; j < len; j++) { - let cur = q.shift(); - - for (let i = 0; i < 4; i++) { - let slot = parseInt(cur[i]); - let before = cur.substr(0, i); - let after = cur.substr(i + 1); - - let left = (10 + slot - 1) % 10; - let leftCode = before + left + after; - if (!visited.has(leftCode) && !deadSet.has(leftCode)) { - if (leftCode === target) { - return steps + 1; - } - - visited.add(leftCode); - q.push(leftCode); - } - - let right = (10 + slot + 1) % 10; - let rightCode = before + right + after; - if (!visited.has(rightCode) && !deadSet.has(rightCode)) { - if (rightCode === target) { - return steps + 1; - } - - visited.add(rightCode); - q.push(rightCode); - } - } - } - steps++; - } - - return -1; -}; +/** + * @param {string[]} deadends + * @param {string} target + * @return {number} + */ +const openLock = function(deadends, target, count = 0) { + let deadSet = new Set(deadends); + let visited = new Set(); + if (deadSet.has("0000")) { + return -1; + } + + let q = []; + q.push("0000"); + visited.add("0000"); + + let steps = 0; + while (q.length > 0) { + let len = q.length; + + for (let j = 0; j < len; j++) { + let cur = q.shift(); + + for (let i = 0; i < 4; i++) { + let slot = parseInt(cur[i]); + let before = cur.substr(0, i); + let after = cur.substr(i + 1); + + let left = (10 + slot - 1) % 10; + let leftCode = before + left + after; + if (!visited.has(leftCode) && !deadSet.has(leftCode)) { + if (leftCode === target) { + return steps + 1; + } + + visited.add(leftCode); + q.push(leftCode); + } + + let right = (10 + slot + 1) % 10; + let rightCode = before + right + after; + if (!visited.has(rightCode) && !deadSet.has(rightCode)) { + if (rightCode === target) { + return steps + 1; + } + + visited.add(rightCode); + q.push(rightCode); + } + } + } + steps++; + } + + return -1; +}; diff --git a/754-reach-a-number.js b/754.reach-a-number.js similarity index 100% rename from 754-reach-a-number.js rename to 754.reach-a-number.js diff --git a/755-pour-water.js b/755.pour-water.js similarity index 100% rename from 755-pour-water.js rename to 755.pour-water.js diff --git a/756-pyramid-transition-matrix.js b/756.pyramid-transition-matrix.js similarity index 100% rename from 756-pyramid-transition-matrix.js rename to 756.pyramid-transition-matrix.js diff --git a/757-set-intersection-size-at-least-two.js b/757.set-intersection-size-at-least-two.js similarity index 100% rename from 757-set-intersection-size-at-least-two.js rename to 757.set-intersection-size-at-least-two.js diff --git a/758-bold-words-in-string.js b/758.bold-words-in-string.js similarity index 100% rename from 758-bold-words-in-string.js rename to 758.bold-words-in-string.js diff --git a/759-employee-free-time.js b/759.employee-free-time.js similarity index 100% rename from 759-employee-free-time.js rename to 759.employee-free-time.js diff --git a/76-minimum-window-substring.js b/76.minimum-window-substring.js similarity index 100% rename from 76-minimum-window-substring.js rename to 76.minimum-window-substring.js diff --git a/760-find-anagram-mappings.js b/760.find-anagram-mappings.js similarity index 100% rename from 760-find-anagram-mappings.js rename to 760.find-anagram-mappings.js diff --git a/761-special-binary-string.js b/761.special-binary-string.js similarity index 100% rename from 761-special-binary-string.js rename to 761.special-binary-string.js diff --git a/762-prime-number-of-set-bits-in-binary-representation.js b/762.prime-number-of-set-bits-in-binary-representation.js old mode 100755 new mode 100644 similarity index 100% rename from 762-prime-number-of-set-bits-in-binary-representation.js rename to 762.prime-number-of-set-bits-in-binary-representation.js diff --git a/763-partition-labels.js b/763.partition-labels.js similarity index 100% rename from 763-partition-labels.js rename to 763.partition-labels.js diff --git a/764-largest-plus-sign.js b/764.largest-plus-sign.js similarity index 100% rename from 764-largest-plus-sign.js rename to 764.largest-plus-sign.js diff --git a/765-couples-holding-hands.js b/765.couples-holding-hands.js similarity index 100% rename from 765-couples-holding-hands.js rename to 765.couples-holding-hands.js diff --git a/766-toeplitz-matrix.js b/766.toeplitz-matrix.js old mode 100755 new mode 100644 similarity index 96% rename from 766-toeplitz-matrix.js rename to 766.toeplitz-matrix.js index 19b658a1..b292c61b --- a/766-toeplitz-matrix.js +++ b/766.toeplitz-matrix.js @@ -1,18 +1,18 @@ -/** - * @param {number[][]} matrix - * @return {boolean} - */ -const isToeplitzMatrix = function(matrix) { - for (let r = 0; r < matrix.length; r++) { - for (let c = 0; c < matrix[0].length; c++) { - if (r > 0 && c > 0 && matrix[r - 1][c - 1] !== matrix[r][c]) { - return false; - } - } - } - return true; -}; - -console.log(isToeplitzMatrix([[1, 2, 3, 4], [5, 1, 2, 3], [9, 5, 1, 2]])); - -console.log(isToeplitzMatrix([[1, 2], [2, 2]])); +/** + * @param {number[][]} matrix + * @return {boolean} + */ +const isToeplitzMatrix = function(matrix) { + for (let r = 0; r < matrix.length; r++) { + for (let c = 0; c < matrix[0].length; c++) { + if (r > 0 && c > 0 && matrix[r - 1][c - 1] !== matrix[r][c]) { + return false; + } + } + } + return true; +}; + +console.log(isToeplitzMatrix([[1, 2, 3, 4], [5, 1, 2, 3], [9, 5, 1, 2]])); + +console.log(isToeplitzMatrix([[1, 2], [2, 2]])); diff --git a/767-reorganize-string.js b/767.reorganize-string.js similarity index 100% rename from 767-reorganize-string.js rename to 767.reorganize-string.js diff --git a/768-max-chunks-to-make-sorted-II.js b/768.max-chunks-to-make-sorted-II.js old mode 100755 new mode 100644 similarity index 95% rename from 768-max-chunks-to-make-sorted-II.js rename to 768.max-chunks-to-make-sorted-II.js index 41df4238..e3c8b47e --- a/768-max-chunks-to-make-sorted-II.js +++ b/768.max-chunks-to-make-sorted-II.js @@ -1,17 +1,17 @@ -/** - * @param {number[]} arr - * @return {number} - */ -const maxChunksToSorted = function(arr) { - const clonedArr = arr.slice(0); - let sum1 = 0; - let sum2 = 0; - let res = 0; - clonedArr.sort((a, b) => a - b); - for (let i = 0; i < arr.length; i++) { - sum1 += arr[i]; - sum2 += clonedArr[i]; - if (sum1 === sum2) res += 1; - } - return res; -}; +/** + * @param {number[]} arr + * @return {number} + */ +const maxChunksToSorted = function(arr) { + const clonedArr = arr.slice(0); + let sum1 = 0; + let sum2 = 0; + let res = 0; + clonedArr.sort((a, b) => a - b); + for (let i = 0; i < arr.length; i++) { + sum1 += arr[i]; + sum2 += clonedArr[i]; + if (sum1 === sum2) res += 1; + } + return res; +}; diff --git a/769-max-chunks-to-make-sorted.js b/769.max-chunks-to-make-sorted.js old mode 100755 new mode 100644 similarity index 95% rename from 769-max-chunks-to-make-sorted.js rename to 769.max-chunks-to-make-sorted.js index 0620c32e..6b1af0d1 --- a/769-max-chunks-to-make-sorted.js +++ b/769.max-chunks-to-make-sorted.js @@ -1,13 +1,13 @@ -/** - * @param {number[]} arr - * @return {number} - */ -const maxChunksToSorted = function(arr) { - let res = 0; - let max = 0; - for (let i = 0; i < arr.length; i++) { - max = Math.max(max, arr[i]); - if (max === i) res += 1; - } - return res; -}; +/** + * @param {number[]} arr + * @return {number} + */ +const maxChunksToSorted = function(arr) { + let res = 0; + let max = 0; + for (let i = 0; i < arr.length; i++) { + max = Math.max(max, arr[i]); + if (max === i) res += 1; + } + return res; +}; diff --git a/77-combinations.js b/77.combinations.js old mode 100755 new mode 100644 similarity index 94% rename from 77-combinations.js rename to 77.combinations.js index 6cae4560..dbc7f79d --- a/77-combinations.js +++ b/77.combinations.js @@ -1,22 +1,22 @@ -/** - * @param {number} n - * @param {number} k - * @return {number[][]} - */ -const combine = function(n, k) { - const res = []; - bt(res, [], 1, n, k); - return res; -}; - -function bt(res, tmp, start, n, k) { - if (k === 0) { - res.push(tmp.slice(0)); - return; - } - for (let i = start; i <= n - k + 1; i++) { - tmp.push(i); - bt(res, tmp, i + 1, n, k - 1); - tmp.pop(); - } -} +/** + * @param {number} n + * @param {number} k + * @return {number[][]} + */ +const combine = function(n, k) { + const res = []; + bt(res, [], 1, n, k); + return res; +}; + +function bt(res, tmp, start, n, k) { + if (k === 0) { + res.push(tmp.slice(0)); + return; + } + for (let i = start; i <= n - k + 1; i++) { + tmp.push(i); + bt(res, tmp, i + 1, n, k - 1); + tmp.pop(); + } +} diff --git a/770-basic-calculator-iv.js b/770.basic-calculator-iv.js similarity index 100% rename from 770-basic-calculator-iv.js rename to 770.basic-calculator-iv.js diff --git a/771-jewels-and-stones.js b/771.jewels-and-stones.js similarity index 100% rename from 771-jewels-and-stones.js rename to 771.jewels-and-stones.js diff --git a/772-basic-calculator-iii.js b/772.basic-calculator-iii.js similarity index 100% rename from 772-basic-calculator-iii.js rename to 772.basic-calculator-iii.js diff --git a/773-sliding-puzzle.js b/773.sliding-puzzle.js old mode 100755 new mode 100644 similarity index 96% rename from 773-sliding-puzzle.js rename to 773.sliding-puzzle.js index 04913069..8efe7639 --- a/773-sliding-puzzle.js +++ b/773.sliding-puzzle.js @@ -1,51 +1,51 @@ -/** - * @param {number[][]} board - * @return {number} - */ -const slidingPuzzle = function(board) { - const target = "123450"; - let start = ""; - for (let i = 0; i < board.length; i++) { - for (let j = 0; j < board[0].length; j++) { - start += board[i][j]; - } - } - const visited = {}; - // all the positions 0 can be swapped to - const dirs = [[1, 3], [0, 2, 4], [1, 5], [0, 4], [1, 3, 5], [2, 4]]; - const queue = []; - queue.push(start); - visited[start] = true; - let res = 0; - while (queue.length !== 0) { - // level count, has to use size control here, otherwise not needed - let size = queue.length; - for (let i = 0; i < size; i++) { - let cur = queue.shift(); - if (cur === target) { - return res; - } - let zero = cur.indexOf("0"); - // swap if possible - for (let dir of dirs[zero]) { - let next = swap(cur, zero, dir); - if (visited.hasOwnProperty(next)) { - continue; - } - visited[next] = true; - queue.push(next); - } - } - res++; - } - return -1; -}; - -function swap(str, i, j) { - const arr = str.split(""); - const ic = str[i]; - const jc = str[j]; - arr.splice(i, 1, jc); - arr.splice(j, 1, ic); - return arr.join(""); -} +/** + * @param {number[][]} board + * @return {number} + */ +const slidingPuzzle = function(board) { + const target = "123450"; + let start = ""; + for (let i = 0; i < board.length; i++) { + for (let j = 0; j < board[0].length; j++) { + start += board[i][j]; + } + } + const visited = {}; + // all the positions 0 can be swapped to + const dirs = [[1, 3], [0, 2, 4], [1, 5], [0, 4], [1, 3, 5], [2, 4]]; + const queue = []; + queue.push(start); + visited[start] = true; + let res = 0; + while (queue.length !== 0) { + // level count, has to use size control here, otherwise not needed + let size = queue.length; + for (let i = 0; i < size; i++) { + let cur = queue.shift(); + if (cur === target) { + return res; + } + let zero = cur.indexOf("0"); + // swap if possible + for (let dir of dirs[zero]) { + let next = swap(cur, zero, dir); + if (visited.hasOwnProperty(next)) { + continue; + } + visited[next] = true; + queue.push(next); + } + } + res++; + } + return -1; +}; + +function swap(str, i, j) { + const arr = str.split(""); + const ic = str[i]; + const jc = str[j]; + arr.splice(i, 1, jc); + arr.splice(j, 1, ic); + return arr.join(""); +} diff --git a/774-minimize-max-distance-to-gas-station.js b/774.minimize-max-distance-to-gas-station.js similarity index 100% rename from 774-minimize-max-distance-to-gas-station.js rename to 774.minimize-max-distance-to-gas-station.js diff --git a/775-global-and-local-inversions.js b/775.global-and-local-inversions.js similarity index 100% rename from 775-global-and-local-inversions.js rename to 775.global-and-local-inversions.js diff --git a/776-split-bst.js b/776.split-bst.js similarity index 100% rename from 776-split-bst.js rename to 776.split-bst.js diff --git a/777-swap-adjacent-in-lr-string.js b/777.swap-adjacent-in-lr-string.js similarity index 100% rename from 777-swap-adjacent-in-lr-string.js rename to 777.swap-adjacent-in-lr-string.js diff --git a/778-swim-in-rising-water.js b/778.swim-in-rising-water.js similarity index 100% rename from 778-swim-in-rising-water.js rename to 778.swim-in-rising-water.js diff --git a/779-k-th-symbol-in-grammar.js b/779.k-th-symbol-in-grammar.js similarity index 100% rename from 779-k-th-symbol-in-grammar.js rename to 779.k-th-symbol-in-grammar.js diff --git a/78-subsets.js b/78.subsets.js old mode 100755 new mode 100644 similarity index 94% rename from 78-subsets.js rename to 78.subsets.js index 0b45c7fd..af3fbdff --- a/78-subsets.js +++ b/78.subsets.js @@ -1,24 +1,24 @@ -/** - * @param {number[]} nums - * @return {number[][]} - */ - -function subsets(nums) { - const list = []; - const len = nums.length; - const subsetNum = Math.pow(2, len); - for (let n = 0; n < subsetNum; n++) { - list[n] = []; - } - for (let i = 0; i < len; i++) { - for (let j = 0; j < subsetNum; j++) { - if ((j >> i) & 1) { - list[j].push(nums[i]); - } - } - } - return list; -} - -console.log(subsets([1, 2, 3])); - +/** + * @param {number[]} nums + * @return {number[][]} + */ + +function subsets(nums) { + const list = []; + const len = nums.length; + const subsetNum = Math.pow(2, len); + for (let n = 0; n < subsetNum; n++) { + list[n] = []; + } + for (let i = 0; i < len; i++) { + for (let j = 0; j < subsetNum; j++) { + if ((j >> i) & 1) { + list[j].push(nums[i]); + } + } + } + return list; +} + +console.log(subsets([1, 2, 3])); + diff --git a/780-reaching-points.js b/780.reaching-points.js similarity index 100% rename from 780-reaching-points.js rename to 780.reaching-points.js diff --git a/782-transform-to-chessboard.js b/782.transform-to-chessboard.js similarity index 100% rename from 782-transform-to-chessboard.js rename to 782.transform-to-chessboard.js diff --git a/783-minimum-distance-between-BST-nodes.js b/783.minimum-distance-between-BST-nodes.js old mode 100755 new mode 100644 similarity index 95% rename from 783-minimum-distance-between-BST-nodes.js rename to 783.minimum-distance-between-BST-nodes.js index 1bc9b2db..1e5b2469 --- a/783-minimum-distance-between-BST-nodes.js +++ b/783.minimum-distance-between-BST-nodes.js @@ -1,27 +1,27 @@ -/** - * Definition for a binary tree node. - * function TreeNode(val) { - * this.val = val; - * this.left = this.right = null; - * } - */ -/** - * @param {TreeNode} root - * @return {number} - */ -const minDiffInBST = function(root) { - if (root === null) return 0; - let min = Number.MAX_SAFE_INTEGER, - res = []; - const bst = (node, res) => { - if (!node) return; - bst(node.left, res); - res.push(node.val); - bst(node.right, res); - }; - bst(root, res); - for (let i = 1; i < res.length; i++) { - min = Math.min(min, res[i] - res[i - 1]); - } - return min; -}; +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ +/** + * @param {TreeNode} root + * @return {number} + */ +const minDiffInBST = function(root) { + if (root === null) return 0; + let min = Number.MAX_SAFE_INTEGER, + res = []; + const bst = (node, res) => { + if (!node) return; + bst(node.left, res); + res.push(node.val); + bst(node.right, res); + }; + bst(root, res); + for (let i = 1; i < res.length; i++) { + min = Math.min(min, res[i] - res[i - 1]); + } + return min; +}; diff --git a/784-letter-case-permutation.js b/784.letter-case-permutation.js similarity index 100% rename from 784-letter-case-permutation.js rename to 784.letter-case-permutation.js diff --git a/785-is-graph-bipartite.js b/785.is-graph-bipartite.js similarity index 100% rename from 785-is-graph-bipartite.js rename to 785.is-graph-bipartite.js diff --git a/786-k-th-smallest-prime-fraction.js b/786.k-th-smallest-prime-fraction.js similarity index 100% rename from 786-k-th-smallest-prime-fraction.js rename to 786.k-th-smallest-prime-fraction.js diff --git a/787-cheapest-flights-within-k-stops.js b/787.cheapest-flights-within-k-stops.js similarity index 100% rename from 787-cheapest-flights-within-k-stops.js rename to 787.cheapest-flights-within-k-stops.js diff --git a/788-rotated-digits.js b/788.rotated-digits.js similarity index 100% rename from 788-rotated-digits.js rename to 788.rotated-digits.js diff --git a/789-escape-the-ghosts.js b/789.escape-the-ghosts.js similarity index 100% rename from 789-escape-the-ghosts.js rename to 789.escape-the-ghosts.js diff --git a/79-word-search.js b/79.word-search.js similarity index 100% rename from 79-word-search.js rename to 79.word-search.js diff --git a/791-custom-sort-string.js b/791.custom-sort-string.js old mode 100755 new mode 100644 similarity index 95% rename from 791-custom-sort-string.js rename to 791.custom-sort-string.js index 2dcf544f..42d165bf --- a/791-custom-sort-string.js +++ b/791.custom-sort-string.js @@ -1,46 +1,46 @@ -/** - * @param {string} S - * @param {string} T - * @return {string} - */ -const customSortString = function(S, T) { - const arr = []; - const remaining = []; - const hash = {}; - for (let i = 0; i < S.length; i++) { - if (T.indexOf(S.charAt(i)) !== -1) { - arr.push(S.charAt(i)); - } - } - let char; - for (let j = 0; j < T.length; j++) { - char = T.charAt(j); - if (arr.indexOf(char) === -1 && remaining.indexOf(char) === -1) { - remaining.push(char); - } - hash[char] = hash.hasOwnProperty(char) ? hash[char] + 1 : 1; - } - return `${genPart(arr, hash)}${genPart(remaining, hash)}`; -}; - -function constructStr(char, num) { - let str = ""; - for (let i = 0; i < num; i++) { - str += char; - } - return str; -} - -function genPart(arr, hash) { - return arr.reduce((ac, el) => { - return ac + constructStr(el, hash[el]); - }, ""); -} - -console.log(customSortString("kqep", "pekeq")); -console.log( - customSortString( - "hucw", - "utzoampdgkalexslxoqfkdjoczajxtuhqyxvlfatmptqdsochtdzgypsfkgqwbgqbcamdqnqztaqhqanirikahtmalzqjjxtqfnh" - ) -); +/** + * @param {string} S + * @param {string} T + * @return {string} + */ +const customSortString = function(S, T) { + const arr = []; + const remaining = []; + const hash = {}; + for (let i = 0; i < S.length; i++) { + if (T.indexOf(S.charAt(i)) !== -1) { + arr.push(S.charAt(i)); + } + } + let char; + for (let j = 0; j < T.length; j++) { + char = T.charAt(j); + if (arr.indexOf(char) === -1 && remaining.indexOf(char) === -1) { + remaining.push(char); + } + hash[char] = hash.hasOwnProperty(char) ? hash[char] + 1 : 1; + } + return `${genPart(arr, hash)}${genPart(remaining, hash)}`; +}; + +function constructStr(char, num) { + let str = ""; + for (let i = 0; i < num; i++) { + str += char; + } + return str; +} + +function genPart(arr, hash) { + return arr.reduce((ac, el) => { + return ac + constructStr(el, hash[el]); + }, ""); +} + +console.log(customSortString("kqep", "pekeq")); +console.log( + customSortString( + "hucw", + "utzoampdgkalexslxoqfkdjoczajxtuhqyxvlfatmptqdsochtdzgypsfkgqwbgqbcamdqnqztaqhqanirikahtmalzqjjxtqfnh" + ) +); diff --git a/792-number-of-matching-subsequences.js b/792.number-of-matching-subsequences.js similarity index 100% rename from 792-number-of-matching-subsequences.js rename to 792.number-of-matching-subsequences.js diff --git a/793-preimage-size-of-factorial-zeroes-function.js b/793.preimage-size-of-factorial-zeroes-function.js similarity index 100% rename from 793-preimage-size-of-factorial-zeroes-function.js rename to 793.preimage-size-of-factorial-zeroes-function.js diff --git a/794-valid-tic-tac-toe-state.js b/794.valid-tic-tac-toe-state.js similarity index 100% rename from 794-valid-tic-tac-toe-state.js rename to 794.valid-tic-tac-toe-state.js diff --git a/795-number-of-subarrays-with-bounded-maximum.js b/795.number-of-subarrays-with-bounded-maximum.js similarity index 100% rename from 795-number-of-subarrays-with-bounded-maximum.js rename to 795.number-of-subarrays-with-bounded-maximum.js diff --git a/796-rotate-string.js b/796.rotate-string.js old mode 100755 new mode 100644 similarity index 95% rename from 796-rotate-string.js rename to 796.rotate-string.js index 407664e8..335e0893 --- a/796-rotate-string.js +++ b/796.rotate-string.js @@ -1,9 +1,9 @@ -/** - * @param {string} A - * @param {string} B - * @return {boolean} - */ -const rotateString = function(A, B) { - if (A.length != B.length) return false; - return A.concat(A).includes(B); -}; +/** + * @param {string} A + * @param {string} B + * @return {boolean} + */ +const rotateString = function(A, B) { + if (A.length != B.length) return false; + return A.concat(A).includes(B); +}; diff --git a/797-all-paths-from-source-to-target.js b/797.all-paths-from-source-to-target.js similarity index 100% rename from 797-all-paths-from-source-to-target.js rename to 797.all-paths-from-source-to-target.js diff --git a/798-smallest-rotation-with-highest-score.js b/798.smallest-rotation-with-highest-score.js similarity index 100% rename from 798-smallest-rotation-with-highest-score.js rename to 798.smallest-rotation-with-highest-score.js diff --git a/799-champagne-tower.js b/799.champagne-tower.js similarity index 100% rename from 799-champagne-tower.js rename to 799.champagne-tower.js diff --git a/8-string-to-integer-(atoi).js b/8.string-to-integer-(atoi).js old mode 100755 new mode 100644 similarity index 95% rename from 8-string-to-integer-(atoi).js rename to 8.string-to-integer-(atoi).js index aa0ff301..485a042b --- a/8-string-to-integer-(atoi).js +++ b/8.string-to-integer-(atoi).js @@ -1,37 +1,37 @@ -/** - * @param {string} str - * @return {number} - */ -const myAtoi = function(str) { - return Math.max(Math.min(parseInt(str) || 0, 2147483647), -2147483648); -}; - -// anotther - -/** - * @param {string} s - * @return {number} - */ -const myAtoi = function(s) { - let n = s.length, i = 0, j = 0, sign = 1; - if(n === 0) { - return 0; - } - while(i < n && s[i] === ' ') { - i++; - } - if(i < n && (s[i] === '-' || s[i] === '+')) { - sign = (s[i] === '-') ? -1 : 1; - i++; - } - j = i - while(i < n) { - if(Number.isInteger(parseInt(s[i]))) i++; - else break; - } - let result = parseInt(s.slice(j, i)) - if(isNaN(result)) return 0 - if(sign * result < -(2**31)) return -(2**31); - else if(sign * result > (2**31-1)) return 2**31-1; - else return sign * result; -}; +/** + * @param {string} str + * @return {number} + */ +const myAtoi = function(str) { + return Math.max(Math.min(parseInt(str) || 0, 2147483647), -2147483648); +}; + +// anotther + +/** + * @param {string} s + * @return {number} + */ +const myAtoi = function(s) { + let n = s.length, i = 0, j = 0, sign = 1; + if(n === 0) { + return 0; + } + while(i < n && s[i] === ' ') { + i++; + } + if(i < n && (s[i] === '-' || s[i] === '+')) { + sign = (s[i] === '-') ? -1 : 1; + i++; + } + j = i + while(i < n) { + if(Number.isInteger(parseInt(s[i]))) i++; + else break; + } + let result = parseInt(s.slice(j, i)) + if(isNaN(result)) return 0 + if(sign * result < -(2**31)) return -(2**31); + else if(sign * result > (2**31-1)) return 2**31-1; + else return sign * result; +}; diff --git a/80-remove-duplicates-from-sorted-array-ii.js b/80.remove-duplicates-from-sorted-array-ii.js similarity index 100% rename from 80-remove-duplicates-from-sorted-array-ii.js rename to 80.remove-duplicates-from-sorted-array-ii.js diff --git a/800-similar-rgb-color.js b/800.similar-rgb-color.js similarity index 100% rename from 800-similar-rgb-color.js rename to 800.similar-rgb-color.js diff --git a/801-minimum-swaps-to-make-sequences-increasing.js b/801.minimum-swaps-to-make-sequences-increasing.js similarity index 100% rename from 801-minimum-swaps-to-make-sequences-increasing.js rename to 801.minimum-swaps-to-make-sequences-increasing.js diff --git a/802-find-eventual-safe-states.js b/802.find-eventual-safe-states.js similarity index 100% rename from 802-find-eventual-safe-states.js rename to 802.find-eventual-safe-states.js diff --git a/803-bricks-falling-when-hit.js b/803.bricks-falling-when-hit.js similarity index 100% rename from 803-bricks-falling-when-hit.js rename to 803.bricks-falling-when-hit.js diff --git a/805-split-array-with-same-average.js b/805.split-array-with-same-average.js similarity index 100% rename from 805-split-array-with-same-average.js rename to 805.split-array-with-same-average.js diff --git a/81-search-in-rotated-sorted-array-ii.js b/81.search-in-rotated-sorted-array-ii.js similarity index 100% rename from 81-search-in-rotated-sorted-array-ii.js rename to 81.search-in-rotated-sorted-array-ii.js diff --git a/810-chalkboard-xor-game.js b/810.chalkboard-xor-game.js similarity index 100% rename from 810-chalkboard-xor-game.js rename to 810.chalkboard-xor-game.js diff --git a/812-largest-triangle-area.js b/812.largest-triangle-area.js similarity index 100% rename from 812-largest-triangle-area.js rename to 812.largest-triangle-area.js diff --git a/813-largest-sum-of-averages.js b/813.largest-sum-of-averages.js similarity index 100% rename from 813-largest-sum-of-averages.js rename to 813.largest-sum-of-averages.js diff --git a/815-bus-routes.js b/815.bus-routes.js similarity index 100% rename from 815-bus-routes.js rename to 815.bus-routes.js diff --git a/818-race-car.js b/818.race-car.js similarity index 100% rename from 818-race-car.js rename to 818.race-car.js diff --git a/819-most-common-word.js b/819.most-common-word.js similarity index 100% rename from 819-most-common-word.js rename to 819.most-common-word.js diff --git a/82-remove-duplicates-from-sorted-list-ii.js b/82.remove-duplicates-from-sorted-list-ii.js similarity index 100% rename from 82-remove-duplicates-from-sorted-list-ii.js rename to 82.remove-duplicates-from-sorted-list-ii.js diff --git a/821-shortest-distance-to-a-character.js b/821.shortest-distance-to-a-character.js old mode 100755 new mode 100644 similarity index 96% rename from 821-shortest-distance-to-a-character.js rename to 821.shortest-distance-to-a-character.js index 13ed0ff4..baaf13f8 --- a/821-shortest-distance-to-a-character.js +++ b/821.shortest-distance-to-a-character.js @@ -1,41 +1,41 @@ -/** - * @param {string} S - * @param {character} C - * @return {number[]} - */ -const shortestToChar = function(S, C) { - const res = []; - const idxArr = []; - for (let i = 0; i < S.length; i++) { - S.charAt(i) === C ? idxArr.push(i) : null; - } - let coordIdx = 0; - let nextCoordIdx = 1; - let val; - for (let idx = 0; idx < S.length; idx++) { - if (S.charAt(idx) === C) { - val = 0; - nextCoordIdx = idxArr[coordIdx + 1] == null ? coordIdx : coordIdx + 1; - if ( - Math.abs(idxArr[coordIdx] - idx) >= Math.abs(idxArr[nextCoordIdx] - idx) - ) { - coordIdx = idxArr[coordIdx + 1] == null ? coordIdx : coordIdx + 1; - } - } else { - nextCoordIdx = idxArr[coordIdx + 1] == null ? coordIdx : coordIdx + 1; - if ( - Math.abs(idxArr[coordIdx] - idx) < Math.abs(idxArr[nextCoordIdx] - idx) - ) { - val = Math.abs(idxArr[coordIdx] - idx); - } else { - val = Math.abs(idxArr[nextCoordIdx] - idx); - coordIdx = idxArr[coordIdx + 1] == null ? coordIdx : coordIdx + 1; - } - } - res[idx] = val; - } - return res; -}; - -console.log(shortestToChar("aaab", "b")); -console.log(shortestToChar("bbba", "b")); +/** + * @param {string} S + * @param {character} C + * @return {number[]} + */ +const shortestToChar = function(S, C) { + const res = []; + const idxArr = []; + for (let i = 0; i < S.length; i++) { + S.charAt(i) === C ? idxArr.push(i) : null; + } + let coordIdx = 0; + let nextCoordIdx = 1; + let val; + for (let idx = 0; idx < S.length; idx++) { + if (S.charAt(idx) === C) { + val = 0; + nextCoordIdx = idxArr[coordIdx + 1] == null ? coordIdx : coordIdx + 1; + if ( + Math.abs(idxArr[coordIdx] - idx) >= Math.abs(idxArr[nextCoordIdx] - idx) + ) { + coordIdx = idxArr[coordIdx + 1] == null ? coordIdx : coordIdx + 1; + } + } else { + nextCoordIdx = idxArr[coordIdx + 1] == null ? coordIdx : coordIdx + 1; + if ( + Math.abs(idxArr[coordIdx] - idx) < Math.abs(idxArr[nextCoordIdx] - idx) + ) { + val = Math.abs(idxArr[coordIdx] - idx); + } else { + val = Math.abs(idxArr[nextCoordIdx] - idx); + coordIdx = idxArr[coordIdx + 1] == null ? coordIdx : coordIdx + 1; + } + } + res[idx] = val; + } + return res; +}; + +console.log(shortestToChar("aaab", "b")); +console.log(shortestToChar("bbba", "b")); diff --git a/823-binary-trees-with-factors.js b/823.binary-trees-with-factors.js similarity index 100% rename from 823-binary-trees-with-factors.js rename to 823.binary-trees-with-factors.js diff --git a/824-goat-latin.js b/824.goat-latin.js similarity index 100% rename from 824-goat-latin.js rename to 824.goat-latin.js diff --git a/826-most-profit-assigning-work.js b/826.most-profit-assigning-work.js similarity index 100% rename from 826-most-profit-assigning-work.js rename to 826.most-profit-assigning-work.js diff --git a/827-making-a-large-island.js b/827.making-a-large-island.js similarity index 100% rename from 827-making-a-large-island.js rename to 827.making-a-large-island.js diff --git a/828-unique-letter-string.js b/828.unique-letter-string.js similarity index 100% rename from 828-unique-letter-string.js rename to 828.unique-letter-string.js diff --git a/829-consecutive-numbers-sum.js b/829.consecutive-numbers-sum.js similarity index 100% rename from 829-consecutive-numbers-sum.js rename to 829.consecutive-numbers-sum.js diff --git a/83-remove-duplicates-from-sorted-list.js b/83.remove-duplicates-from-sorted-list.js old mode 100755 new mode 100644 similarity index 95% rename from 83-remove-duplicates-from-sorted-list.js rename to 83.remove-duplicates-from-sorted-list.js index 4fc25216..236e1dfa --- a/83-remove-duplicates-from-sorted-list.js +++ b/83.remove-duplicates-from-sorted-list.js @@ -1,23 +1,23 @@ -/** - * Definition for singly-linked list. - * function ListNode(val) { - * this.val = val; - * this.next = null; - * } - */ -/** - * @param {ListNode} head - * @return {ListNode} - */ -const deleteDuplicates = function(head) { - let current = head; - while (current !== null && current.next !== null) { - if (current.val === current.next.val) { - current.next = current.next.next; - } else { - current = current.next; - } - } - return head; -}; - +/** + * Definition for singly-linked list. + * function ListNode(val) { + * this.val = val; + * this.next = null; + * } + */ +/** + * @param {ListNode} head + * @return {ListNode} + */ +const deleteDuplicates = function(head) { + let current = head; + while (current !== null && current.next !== null) { + if (current.val === current.next.val) { + current.next = current.next.next; + } else { + current = current.next; + } + } + return head; +}; + diff --git a/830-positions-of-large-groups.js b/830.positions-of-large-groups.js old mode 100755 new mode 100644 similarity index 95% rename from 830-positions-of-large-groups.js rename to 830.positions-of-large-groups.js index a828815d..4c7fb231 --- a/830-positions-of-large-groups.js +++ b/830.positions-of-large-groups.js @@ -1,37 +1,37 @@ -/** - * @param {string} S - * @return {number[][]} - */ -const largeGroupPositions = function(S) { - if (S.length === 0) return []; - let prevChar = S[0]; - let curChar = S[0]; - let curStartIdx = 0; - let curCount = 1; - const res = []; - let tmpChar; - for (let i = 1; i < S.length; i++) { - tmpChar = S[i]; - if (tmpChar === prevChar) { - curCount += 1; - } else { - if (curCount >= 3) { - res.push([curStartIdx, curStartIdx + curCount - 1]); - } - - prevChar = S[i]; - curStartIdx = i; - curCount = 1; - } - } - - if (curCount >= 3) { - res.push([curStartIdx, curStartIdx + curCount - 1]); - } - - return res; -}; - -console.log(largeGroupPositions("aaa")); -console.log(largeGroupPositions("abbxxxxzzy")); -console.log(largeGroupPositions("abcdddeeeeaabbbcd")); +/** + * @param {string} S + * @return {number[][]} + */ +const largeGroupPositions = function(S) { + if (S.length === 0) return []; + let prevChar = S[0]; + let curChar = S[0]; + let curStartIdx = 0; + let curCount = 1; + const res = []; + let tmpChar; + for (let i = 1; i < S.length; i++) { + tmpChar = S[i]; + if (tmpChar === prevChar) { + curCount += 1; + } else { + if (curCount >= 3) { + res.push([curStartIdx, curStartIdx + curCount - 1]); + } + + prevChar = S[i]; + curStartIdx = i; + curCount = 1; + } + } + + if (curCount >= 3) { + res.push([curStartIdx, curStartIdx + curCount - 1]); + } + + return res; +}; + +console.log(largeGroupPositions("aaa")); +console.log(largeGroupPositions("abbxxxxzzy")); +console.log(largeGroupPositions("abcdddeeeeaabbbcd")); diff --git a/834-sum-of-distances-in-tree.js b/834.sum-of-distances-in-tree.js similarity index 100% rename from 834-sum-of-distances-in-tree.js rename to 834.sum-of-distances-in-tree.js diff --git a/835-image-overlap.js b/835.image-overlap.js similarity index 100% rename from 835-image-overlap.js rename to 835.image-overlap.js diff --git a/836-rectangle-overlap.js b/836.rectangle-overlap.js old mode 100755 new mode 100644 similarity index 100% rename from 836-rectangle-overlap.js rename to 836.rectangle-overlap.js diff --git a/837-new-21-game.js b/837.new-21-game.js old mode 100755 new mode 100644 similarity index 100% rename from 837-new-21-game.js rename to 837.new-21-game.js diff --git a/839-similar-string-groups.js b/839.similar-string-groups.js similarity index 100% rename from 839-similar-string-groups.js rename to 839.similar-string-groups.js diff --git a/84-largest-rectangle-in-histogram.js b/84.largest-rectangle-in-histogram.js similarity index 100% rename from 84-largest-rectangle-in-histogram.js rename to 84.largest-rectangle-in-histogram.js diff --git a/841-keys-and-rooms.js b/841.keys-and-rooms.js old mode 100755 new mode 100644 similarity index 95% rename from 841-keys-and-rooms.js rename to 841.keys-and-rooms.js index a4ae46de..e66723cc --- a/841-keys-and-rooms.js +++ b/841.keys-and-rooms.js @@ -1,26 +1,26 @@ -/** - * @param {number[][]} rooms - * @return {boolean} - */ -const canVisitAllRooms = function(rooms) { - const stack = []; - const seen = []; - for (let i = 0; i < rooms.length; i++) { - seen[i] = false; - } - seen[0] = true; - stack.push(0); - while (stack.length) { - let node = stack.pop(); - for (let el of rooms[node]) { - if (!seen[el]) { - seen[el] = true; - stack.push(el); - } - } - } - for (let el of seen) { - if (!el) return false; - } - return true; -}; +/** + * @param {number[][]} rooms + * @return {boolean} + */ +const canVisitAllRooms = function(rooms) { + const stack = []; + const seen = []; + for (let i = 0; i < rooms.length; i++) { + seen[i] = false; + } + seen[0] = true; + stack.push(0); + while (stack.length) { + let node = stack.pop(); + for (let el of rooms[node]) { + if (!seen[el]) { + seen[el] = true; + stack.push(el); + } + } + } + for (let el of seen) { + if (!el) return false; + } + return true; +}; diff --git a/843-guess-the-word.js b/843.guess-the-word.js similarity index 100% rename from 843-guess-the-word.js rename to 843.guess-the-word.js diff --git a/844-backspace-string-compare.js b/844.backspace-string-compare.js similarity index 100% rename from 844-backspace-string-compare.js rename to 844.backspace-string-compare.js diff --git a/845-longest-mountain-in-array.js b/845.longest-mountain-in-array.js similarity index 100% rename from 845-longest-mountain-in-array.js rename to 845.longest-mountain-in-array.js diff --git a/847-shortest-path-visiting-all-nodes.js b/847.shortest-path-visiting-all-nodes.js similarity index 100% rename from 847-shortest-path-visiting-all-nodes.js rename to 847.shortest-path-visiting-all-nodes.js diff --git a/848-shifting-letters.js b/848.shifting-letters.js similarity index 100% rename from 848-shifting-letters.js rename to 848.shifting-letters.js diff --git a/849-maximize-distance-to-closest-person.js b/849.maximize-distance-to-closest-person.js similarity index 100% rename from 849-maximize-distance-to-closest-person.js rename to 849.maximize-distance-to-closest-person.js diff --git a/85-maximal-rectangle.js b/85.maximal-rectangle.js similarity index 100% rename from 85-maximal-rectangle.js rename to 85.maximal-rectangle.js diff --git a/850-rectangle-area-ii.js b/850.rectangle-area-ii.js similarity index 100% rename from 850-rectangle-area-ii.js rename to 850.rectangle-area-ii.js diff --git a/854-k-similar-strings.js b/854.k-similar-strings.js similarity index 100% rename from 854-k-similar-strings.js rename to 854.k-similar-strings.js diff --git a/855-exam-room.js b/855.exam-room.js similarity index 100% rename from 855-exam-room.js rename to 855.exam-room.js diff --git a/856-score-of-parentheses.js b/856.score-of-parentheses.js old mode 100755 new mode 100644 similarity index 100% rename from 856-score-of-parentheses.js rename to 856.score-of-parentheses.js diff --git a/857-minimum-cost-to-hire-k-workers.js b/857.minimum-cost-to-hire-k-workers.js similarity index 100% rename from 857-minimum-cost-to-hire-k-workers.js rename to 857.minimum-cost-to-hire-k-workers.js diff --git a/858-mirror-reflection.js b/858.mirror-reflection.js old mode 100755 new mode 100644 similarity index 93% rename from 858-mirror-reflection.js rename to 858.mirror-reflection.js index d02c43ea..361404d1 --- a/858-mirror-reflection.js +++ b/858.mirror-reflection.js @@ -1,19 +1,19 @@ -/** - * @param {number} p - * @param {number} q - * @return {number} - */ -const mirrorReflection = function(p, q) { - while (p % 2 === 0 && q % 2 === 0) { - p /= 2; - q /= 2; - } - - if (p % 2 === 0) { - return 2; - } else if (q % 2 === 0) { - return 0; - } else { - return 1; - } -}; +/** + * @param {number} p + * @param {number} q + * @return {number} + */ +const mirrorReflection = function(p, q) { + while (p % 2 === 0 && q % 2 === 0) { + p /= 2; + q /= 2; + } + + if (p % 2 === 0) { + return 2; + } else if (q % 2 === 0) { + return 0; + } else { + return 1; + } +}; diff --git a/859-buddy-strings.js b/859.buddy-strings.js similarity index 100% rename from 859-buddy-strings.js rename to 859.buddy-strings.js diff --git a/86-partition-list.js b/86.partition-list.js similarity index 100% rename from 86-partition-list.js rename to 86.partition-list.js diff --git a/860-lemonade-change.js b/860.lemonade-change.js old mode 100755 new mode 100644 similarity index 94% rename from 860-lemonade-change.js rename to 860.lemonade-change.js index 57d282e0..eaed3e73 --- a/860-lemonade-change.js +++ b/860.lemonade-change.js @@ -1,28 +1,28 @@ -/** - * @param {number[]} bills - * @return {boolean} - */ -const lemonadeChange = function(bills) { - let five = 0; - let ten = 0; - for (let el of bills) { - if (el === 5) { - five += 1; - } else if (el === 10) { - if (five < 1) return false; - five -= 1; - ten += 1; - } else { - if (five > 0 && ten > 0) { - five -= 1; - ten -= 1; - } else if (five >= 3) { - five -= 3; - } else { - return false; - } - } - } - - return true; -}; +/** + * @param {number[]} bills + * @return {boolean} + */ +const lemonadeChange = function(bills) { + let five = 0; + let ten = 0; + for (let el of bills) { + if (el === 5) { + five += 1; + } else if (el === 10) { + if (five < 1) return false; + five -= 1; + ten += 1; + } else { + if (five > 0 && ten > 0) { + five -= 1; + ten -= 1; + } else if (five >= 3) { + five -= 3; + } else { + return false; + } + } + } + + return true; +}; diff --git a/861-score-after-flipping-matrix.js b/861.score-after-flipping-matrix.js similarity index 100% rename from 861-score-after-flipping-matrix.js rename to 861.score-after-flipping-matrix.js diff --git a/862-shortest-subarray-with-sum-at-least-k.js b/862.shortest-subarray-with-sum-at-least-k.js similarity index 100% rename from 862-shortest-subarray-with-sum-at-least-k.js rename to 862.shortest-subarray-with-sum-at-least-k.js diff --git a/863-all-nodes-distance-k-in-binary-tree.js b/863.all-nodes-distance-k-in-binary-tree.js similarity index 100% rename from 863-all-nodes-distance-k-in-binary-tree.js rename to 863.all-nodes-distance-k-in-binary-tree.js diff --git a/864-shortest-path-to-get-all-keys.js b/864.shortest-path-to-get-all-keys.js similarity index 100% rename from 864-shortest-path-to-get-all-keys.js rename to 864.shortest-path-to-get-all-keys.js diff --git a/865-smallest-subtree-with-all-the-deepest-nodes.js b/865.smallest-subtree-with-all-the-deepest-nodes.js old mode 100755 new mode 100644 similarity index 95% rename from 865-smallest-subtree-with-all-the-deepest-nodes.js rename to 865.smallest-subtree-with-all-the-deepest-nodes.js index eee29d20..6ae65569 --- a/865-smallest-subtree-with-all-the-deepest-nodes.js +++ b/865.smallest-subtree-with-all-the-deepest-nodes.js @@ -1,29 +1,29 @@ -/** - * Definition for a binary tree node. - * function TreeNode(val) { - * this.val = val; - * this.left = this.right = null; - * } - */ -/** - * @param {TreeNode} root - * @return {TreeNode} - */ -const subtreeWithAllDeepest = function(root) { - return dfs(root).node; -}; - -function dfs(node) { - if (node == null) return new result(null, 0); - const l = dfs(node.left); - const r = dfs(node.right); - if (l.dist > r.dist) return new result(l.node, l.dist + 1); - if (l.dist < r.dist) return new result(r.node, r.dist + 1); - return new result(node, l.dist + 1); -} - -function result(node, dist) { - this.node = node; - this.dist = dist; -} - +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ +/** + * @param {TreeNode} root + * @return {TreeNode} + */ +const subtreeWithAllDeepest = function(root) { + return dfs(root).node; +}; + +function dfs(node) { + if (node == null) return new result(null, 0); + const l = dfs(node.left); + const r = dfs(node.right); + if (l.dist > r.dist) return new result(l.node, l.dist + 1); + if (l.dist < r.dist) return new result(r.node, r.dist + 1); + return new result(node, l.dist + 1); +} + +function result(node, dist) { + this.node = node; + this.dist = dist; +} + diff --git a/866-prime-palindrome.js b/866.prime-palindrome.js similarity index 100% rename from 866-prime-palindrome.js rename to 866.prime-palindrome.js diff --git a/868-binary-gap.js b/868.binary-gap.js old mode 100755 new mode 100644 similarity index 95% rename from 868-binary-gap.js rename to 868.binary-gap.js index bfafb7fa..0034c853 --- a/868-binary-gap.js +++ b/868.binary-gap.js @@ -1,22 +1,22 @@ -/** - * @param {number} N - * @return {number} - */ -const binaryGap = function(N) { - const bin = (N >>> 0).toString(2); - const idxArr = []; - for (let i = 0; i < bin.length; i++) { - const num = bin.charAt(i); - if (num === "1") { - idxArr.push(i); - } - } - let maxConLen = 0; - for (let idx = 0; idx < idxArr.length - 1; idx++) { - if (idxArr[idx + 1] - idxArr[idx] > maxConLen) { - maxConLen = idxArr[idx + 1] - idxArr[idx]; - } - } - - return maxConLen; -}; +/** + * @param {number} N + * @return {number} + */ +const binaryGap = function(N) { + const bin = (N >>> 0).toString(2); + const idxArr = []; + for (let i = 0; i < bin.length; i++) { + const num = bin.charAt(i); + if (num === "1") { + idxArr.push(i); + } + } + let maxConLen = 0; + for (let idx = 0; idx < idxArr.length - 1; idx++) { + if (idxArr[idx + 1] - idxArr[idx] > maxConLen) { + maxConLen = idxArr[idx + 1] - idxArr[idx]; + } + } + + return maxConLen; +}; diff --git a/869-reordered-power-of-2.js b/869.reordered-power-of-2.js old mode 100755 new mode 100644 similarity index 95% rename from 869-reordered-power-of-2.js rename to 869.reordered-power-of-2.js index 910b20eb..e6b28435 --- a/869-reordered-power-of-2.js +++ b/869.reordered-power-of-2.js @@ -1,36 +1,36 @@ -/** - * @param {number} N - * @return {boolean} - */ -const reorderedPowerOf2 = function(N) { - const A = count(N); - for (let i = 0; i < 31; i++) { - if (arrayEqual(A, count(1 << i))) return true; - } - return false; -}; - -function count(num) { - const res = []; - while (num > 0) { - addOne(res, num % 10); - num = parseInt(num / 10); - } - return res; -} -function addOne(arr, idx) { - if (arr[idx]) { - arr[idx] += 1; - return; - } - arr[idx] = 1; -} -function arrayEqual(a1, a2) { - return JSON.stringify(a1) === JSON.stringify(a2); -} - -console.log(reorderedPowerOf2(1)); -console.log(reorderedPowerOf2(10)); -console.log(reorderedPowerOf2(16)); -console.log(reorderedPowerOf2(24)); -console.log(reorderedPowerOf2(46)); +/** + * @param {number} N + * @return {boolean} + */ +const reorderedPowerOf2 = function(N) { + const A = count(N); + for (let i = 0; i < 31; i++) { + if (arrayEqual(A, count(1 << i))) return true; + } + return false; +}; + +function count(num) { + const res = []; + while (num > 0) { + addOne(res, num % 10); + num = parseInt(num / 10); + } + return res; +} +function addOne(arr, idx) { + if (arr[idx]) { + arr[idx] += 1; + return; + } + arr[idx] = 1; +} +function arrayEqual(a1, a2) { + return JSON.stringify(a1) === JSON.stringify(a2); +} + +console.log(reorderedPowerOf2(1)); +console.log(reorderedPowerOf2(10)); +console.log(reorderedPowerOf2(16)); +console.log(reorderedPowerOf2(24)); +console.log(reorderedPowerOf2(46)); diff --git a/87-scramble-string.js b/87.scramble-string.js similarity index 100% rename from 87-scramble-string.js rename to 87.scramble-string.js diff --git a/870-advantage-shuffle.js b/870.advantage-shuffle.js similarity index 100% rename from 870-advantage-shuffle.js rename to 870.advantage-shuffle.js diff --git a/871-minimum-number-of-refueling-stops.js b/871.minimum-number-of-refueling-stops.js similarity index 100% rename from 871-minimum-number-of-refueling-stops.js rename to 871.minimum-number-of-refueling-stops.js diff --git a/873-length-of-longest-fibonacci-subsequence.js b/873.length-of-longest-fibonacci-subsequence.js old mode 100755 new mode 100644 similarity index 95% rename from 873-length-of-longest-fibonacci-subsequence.js rename to 873.length-of-longest-fibonacci-subsequence.js index 02c244a0..e892ced4 --- a/873-length-of-longest-fibonacci-subsequence.js +++ b/873.length-of-longest-fibonacci-subsequence.js @@ -1,27 +1,27 @@ -/** - * @param {number[]} A - * @return {number} - */ -const lenLongestFibSubseq = function(A) { - const n = A.length; - let max = 0; - const dp = Array(n).map(el => Array(n).fill(0)); - for (let i = 1; i < n; i++) { - let l = 0, - r = i - 1; - while (l < r) { - let sum = A[l] + A[r]; - if (sum > A[i]) { - r--; - } else if (sum < A[i]) { - l++; - } else { - dp[r][i] = dp[l][r] + 1; - max = Math.max(max, dp[r][i]); - r--; - l++; - } - } - } - return max == 0 ? 0 : max + 2; -}; +/** + * @param {number[]} A + * @return {number} + */ +const lenLongestFibSubseq = function(A) { + const n = A.length; + let max = 0; + const dp = Array(n).map(el => Array(n).fill(0)); + for (let i = 1; i < n; i++) { + let l = 0, + r = i - 1; + while (l < r) { + let sum = A[l] + A[r]; + if (sum > A[i]) { + r--; + } else if (sum < A[i]) { + l++; + } else { + dp[r][i] = dp[l][r] + 1; + max = Math.max(max, dp[r][i]); + r--; + l++; + } + } + } + return max == 0 ? 0 : max + 2; +}; diff --git a/874-walking-robot-simulation.js b/874.walking-robot-simulation.js similarity index 100% rename from 874-walking-robot-simulation.js rename to 874.walking-robot-simulation.js diff --git a/875-koko-eating-bananas.js b/875.koko-eating-bananas.js similarity index 100% rename from 875-koko-eating-bananas.js rename to 875.koko-eating-bananas.js diff --git a/876-middle-of-the-linked-list.js b/876.middle-of-the-linked-list.js similarity index 100% rename from 876-middle-of-the-linked-list.js rename to 876.middle-of-the-linked-list.js diff --git a/878-nth-magical-number.js b/878.nth-magical-number.js similarity index 100% rename from 878-nth-magical-number.js rename to 878.nth-magical-number.js diff --git a/879-profitable-schemes.js b/879.profitable-schemes.js similarity index 100% rename from 879-profitable-schemes.js rename to 879.profitable-schemes.js diff --git a/88-merge-sorted-array.js b/88.merge-sorted-array.js similarity index 100% rename from 88-merge-sorted-array.js rename to 88.merge-sorted-array.js diff --git a/880-decoded-string-at-index.js b/880.decoded-string-at-index.js similarity index 100% rename from 880-decoded-string-at-index.js rename to 880.decoded-string-at-index.js diff --git a/881-boats-to-save-people.js b/881.boats-to-save-people.js similarity index 100% rename from 881-boats-to-save-people.js rename to 881.boats-to-save-people.js diff --git a/882-reachable-nodes-in-subdivided-graph.js b/882.reachable-nodes-in-subdivided-graph.js similarity index 100% rename from 882-reachable-nodes-in-subdivided-graph.js rename to 882.reachable-nodes-in-subdivided-graph.js diff --git a/883-projection-area-of-3d-shapes.js b/883.projection-area-of-3d-shapes.js similarity index 100% rename from 883-projection-area-of-3d-shapes.js rename to 883.projection-area-of-3d-shapes.js diff --git a/884-uncommon-words-from-two-sentences.js b/884.uncommon-words-from-two-sentences.js similarity index 100% rename from 884-uncommon-words-from-two-sentences.js rename to 884.uncommon-words-from-two-sentences.js diff --git a/886-possible-bipartition.js b/886.possible-bipartition.js similarity index 100% rename from 886-possible-bipartition.js rename to 886.possible-bipartition.js diff --git a/887-super-egg-drop.js b/887.super-egg-drop.js similarity index 100% rename from 887-super-egg-drop.js rename to 887.super-egg-drop.js diff --git a/888-fair-candy-swap.js b/888.fair-candy-swap.js similarity index 100% rename from 888-fair-candy-swap.js rename to 888.fair-candy-swap.js diff --git a/889-construct-binary-tree-from-preorder-and-postorder-traversal.js b/889.construct-binary-tree-from-preorder-and-postorder-traversal.js similarity index 100% rename from 889-construct-binary-tree-from-preorder-and-postorder-traversal.js rename to 889.construct-binary-tree-from-preorder-and-postorder-traversal.js diff --git a/89-gray-code.js b/89.gray-code.js similarity index 100% rename from 89-gray-code.js rename to 89.gray-code.js diff --git a/890-find-and-replace-pattern.js b/890.find-and-replace-pattern.js similarity index 100% rename from 890-find-and-replace-pattern.js rename to 890.find-and-replace-pattern.js diff --git a/891-sum-of-subsequence-widths.js b/891.sum-of-subsequence-widths.js similarity index 100% rename from 891-sum-of-subsequence-widths.js rename to 891.sum-of-subsequence-widths.js diff --git a/892-surface-area-of-3d-shapes.js b/892.surface-area-of-3d-shapes.js similarity index 100% rename from 892-surface-area-of-3d-shapes.js rename to 892.surface-area-of-3d-shapes.js diff --git a/893-groups-of-special-equivalent-strings.js b/893.groups-of-special-equivalent-strings.js similarity index 100% rename from 893-groups-of-special-equivalent-strings.js rename to 893.groups-of-special-equivalent-strings.js diff --git a/894-all-possible-full-binary-trees.js b/894.all-possible-full-binary-trees.js similarity index 100% rename from 894-all-possible-full-binary-trees.js rename to 894.all-possible-full-binary-trees.js diff --git a/895-maximum-frequency-stack.js b/895.maximum-frequency-stack.js similarity index 100% rename from 895-maximum-frequency-stack.js rename to 895.maximum-frequency-stack.js diff --git a/896-monotonic-array.js b/896.monotonic-array.js similarity index 100% rename from 896-monotonic-array.js rename to 896.monotonic-array.js diff --git a/897-increasing-order-search-tree.js b/897.increasing-order-search-tree.js similarity index 100% rename from 897-increasing-order-search-tree.js rename to 897.increasing-order-search-tree.js diff --git a/898-bitwise-ors-of-subarrays.js b/898.bitwise-ors-of-subarrays.js similarity index 100% rename from 898-bitwise-ors-of-subarrays.js rename to 898.bitwise-ors-of-subarrays.js diff --git a/899-orderly-queue.js b/899.orderly-queue.js similarity index 100% rename from 899-orderly-queue.js rename to 899.orderly-queue.js diff --git a/9-palindrome-number.js b/9.palindrome-number.js old mode 100755 new mode 100644 similarity index 94% rename from 9-palindrome-number.js rename to 9.palindrome-number.js index c0ad42be..c0d5d1c9 --- a/9-palindrome-number.js +++ b/9.palindrome-number.js @@ -1,21 +1,21 @@ -/** - * @param {number} x - * @return {boolean} - */ -const isPalindrome = function(x) { - if (x < 0) return false; - const rev = reverseNum(x); - return x === rev; -}; - -function reverseNum(num) { - let n = num; - let rev = 0; - let dig; - while (num > 0) { - dig = num % 10; - rev = rev * 10 + dig; - num = Math.floor(num / 10); - } - return rev; -} +/** + * @param {number} x + * @return {boolean} + */ +const isPalindrome = function(x) { + if (x < 0) return false; + const rev = reverseNum(x); + return x === rev; +}; + +function reverseNum(num) { + let n = num; + let rev = 0; + let dig; + while (num > 0) { + dig = num % 10; + rev = rev * 10 + dig; + num = Math.floor(num / 10); + } + return rev; +} diff --git a/90-subsets-II.js b/90.subsets-II.js old mode 100755 new mode 100644 similarity index 95% rename from 90-subsets-II.js rename to 90.subsets-II.js index ac3cd152..ddc33cca --- a/90-subsets-II.js +++ b/90.subsets-II.js @@ -1,21 +1,21 @@ -/** - * @param {number[]} nums - * @return {number[][]} - */ -const subsetsWithDup = function(nums) { - nums.sort((a, b) => a - b); - const res = []; - bt(res, nums, [], 0); - return res; -}; - -function bt(res, nums, arr, start) { - res.push(arr.slice(0)); - for (let i = start; i < nums.length; i++) { - if (i === start || nums[i] !== nums[i - 1]) { - arr.push(nums[i]); - bt(res, nums, arr, i + 1); - arr.pop(); - } - } -} +/** + * @param {number[]} nums + * @return {number[][]} + */ +const subsetsWithDup = function(nums) { + nums.sort((a, b) => a - b); + const res = []; + bt(res, nums, [], 0); + return res; +}; + +function bt(res, nums, arr, start) { + res.push(arr.slice(0)); + for (let i = start; i < nums.length; i++) { + if (i === start || nums[i] !== nums[i - 1]) { + arr.push(nums[i]); + bt(res, nums, arr, i + 1); + arr.pop(); + } + } +} diff --git a/901-online-stock-span.js b/901.online-stock-span.js similarity index 100% rename from 901-online-stock-span.js rename to 901.online-stock-span.js diff --git a/902-numbers-at-most-n-given-digit-set.js b/902.numbers-at-most-n-given-digit-set.js similarity index 100% rename from 902-numbers-at-most-n-given-digit-set.js rename to 902.numbers-at-most-n-given-digit-set.js diff --git a/903-valid-permutations-for-di-sequence.js b/903.valid-permutations-for-di-sequence.js similarity index 100% rename from 903-valid-permutations-for-di-sequence.js rename to 903.valid-permutations-for-di-sequence.js diff --git a/904-fruit-into-baskets.js b/904.fruit-into-baskets.js similarity index 100% rename from 904-fruit-into-baskets.js rename to 904.fruit-into-baskets.js diff --git a/905-sort-array-by-parity.js b/905.sort-array-by-parity.js similarity index 100% rename from 905-sort-array-by-parity.js rename to 905.sort-array-by-parity.js diff --git a/906-super-palindromes.js b/906.super-palindromes.js similarity index 100% rename from 906-super-palindromes.js rename to 906.super-palindromes.js diff --git a/907-sum-of-subarray-minimums.js b/907.sum-of-subarray-minimums.js similarity index 100% rename from 907-sum-of-subarray-minimums.js rename to 907.sum-of-subarray-minimums.js diff --git a/908-smallest-range-i.js b/908.smallest-range-i.js similarity index 100% rename from 908-smallest-range-i.js rename to 908.smallest-range-i.js diff --git a/91-decode-ways.js b/91.decode-ways.js similarity index 100% rename from 91-decode-ways.js rename to 91.decode-ways.js diff --git a/910-smallest-range-ii.js b/910.smallest-range-ii.js similarity index 100% rename from 910-smallest-range-ii.js rename to 910.smallest-range-ii.js diff --git a/912-sort-an-array.js b/912.sort-an-array.js similarity index 100% rename from 912-sort-an-array.js rename to 912.sort-an-array.js diff --git a/913-cat-and-mouse.js b/913.cat-and-mouse.js similarity index 100% rename from 913-cat-and-mouse.js rename to 913.cat-and-mouse.js diff --git a/914-x-of-a-kind-in-a-deck-of-cards.js b/914.x-of-a-kind-in-a-deck-of-cards.js similarity index 100% rename from 914-x-of-a-kind-in-a-deck-of-cards.js rename to 914.x-of-a-kind-in-a-deck-of-cards.js diff --git a/915-partition-array-into-disjoint-intervals.js b/915.partition-array-into-disjoint-intervals.js similarity index 100% rename from 915-partition-array-into-disjoint-intervals.js rename to 915.partition-array-into-disjoint-intervals.js diff --git a/916-word-subsets.js b/916.word-subsets.js old mode 100755 new mode 100644 similarity index 95% rename from 916-word-subsets.js rename to 916.word-subsets.js index bc1f6e13..572f5942 --- a/916-word-subsets.js +++ b/916.word-subsets.js @@ -1,32 +1,32 @@ -/** - * @param {string[]} A - * @param {string[]} B - * @return {string[]} - */ -const wordSubsets = function(A, B) { - function counter(s) { - let count = Array(26).fill(0); - for (let i = 0; i < s.length; i++) count[s.charCodeAt(i) - 97]++; - return count; - } - let aux = Array(26).fill(0); - let result = []; - for (let i = 0; i < B.length; i++) { - let count = counter(B[i]); - for (let i = 0; i < 26; i++) { - aux[i] = Math.max(aux[i], count[i]); - } - } - for (let i = 0; i < A.length; i++) { - let count = counter(A[i]); - let flag = true; - for (let j = 0; j < 26; j++) { - if (aux[j] > 0 && count[j] - aux[j] < 0) { - flag = false; - break; - } - } - if (flag) result.push(A[i]); - } - return result; -}; +/** + * @param {string[]} A + * @param {string[]} B + * @return {string[]} + */ +const wordSubsets = function(A, B) { + function counter(s) { + let count = Array(26).fill(0); + for (let i = 0; i < s.length; i++) count[s.charCodeAt(i) - 97]++; + return count; + } + let aux = Array(26).fill(0); + let result = []; + for (let i = 0; i < B.length; i++) { + let count = counter(B[i]); + for (let i = 0; i < 26; i++) { + aux[i] = Math.max(aux[i], count[i]); + } + } + for (let i = 0; i < A.length; i++) { + let count = counter(A[i]); + let flag = true; + for (let j = 0; j < 26; j++) { + if (aux[j] > 0 && count[j] - aux[j] < 0) { + flag = false; + break; + } + } + if (flag) result.push(A[i]); + } + return result; +}; diff --git a/917-reverse-only-letters.js b/917.reverse-only-letters.js similarity index 100% rename from 917-reverse-only-letters.js rename to 917.reverse-only-letters.js diff --git a/918-maximum-sum-circular-subarray.js b/918.maximum-sum-circular-subarray.js similarity index 100% rename from 918-maximum-sum-circular-subarray.js rename to 918.maximum-sum-circular-subarray.js diff --git a/919-complete-binary-tree-inserter.js b/919.complete-binary-tree-inserter.js similarity index 100% rename from 919-complete-binary-tree-inserter.js rename to 919.complete-binary-tree-inserter.js diff --git a/92-reverse-linked-list-ii.js b/92.reverse-linked-list-ii.js similarity index 100% rename from 92-reverse-linked-list-ii.js rename to 92.reverse-linked-list-ii.js diff --git a/920-number-of-music-playlists.js b/920.number-of-music-playlists.js similarity index 100% rename from 920-number-of-music-playlists.js rename to 920.number-of-music-playlists.js diff --git a/921-minimum-add-to-make-parentheses-valid.js b/921.minimum-add-to-make-parentheses-valid.js similarity index 100% rename from 921-minimum-add-to-make-parentheses-valid.js rename to 921.minimum-add-to-make-parentheses-valid.js diff --git a/922-sort-array-by-parity-ii.js b/922.sort-array-by-parity-ii.js similarity index 100% rename from 922-sort-array-by-parity-ii.js rename to 922.sort-array-by-parity-ii.js diff --git a/923-3sum-with-multiplicity.js b/923.3sum-with-multiplicity.js similarity index 100% rename from 923-3sum-with-multiplicity.js rename to 923.3sum-with-multiplicity.js diff --git a/924-minimize-malware-spread.js b/924.minimize-malware-spread.js similarity index 100% rename from 924-minimize-malware-spread.js rename to 924.minimize-malware-spread.js diff --git a/925-long-pressed-name.js b/925.long-pressed-name.js similarity index 100% rename from 925-long-pressed-name.js rename to 925.long-pressed-name.js diff --git a/926-flip-string-to-monotone-increasing.js b/926.flip-string-to-monotone-increasing.js similarity index 100% rename from 926-flip-string-to-monotone-increasing.js rename to 926.flip-string-to-monotone-increasing.js diff --git a/927-three-equal-parts.js b/927.three-equal-parts.js similarity index 100% rename from 927-three-equal-parts.js rename to 927.three-equal-parts.js diff --git a/928-minimize-malware-spread-ii.js b/928.minimize-malware-spread-ii.js similarity index 100% rename from 928-minimize-malware-spread-ii.js rename to 928.minimize-malware-spread-ii.js diff --git a/929-unique-email-addresses.js b/929.unique-email-addresses.js similarity index 100% rename from 929-unique-email-addresses.js rename to 929.unique-email-addresses.js diff --git a/93-restore-ip-addresses.js b/93.restore-ip-addresses.js similarity index 100% rename from 93-restore-ip-addresses.js rename to 93.restore-ip-addresses.js diff --git a/930-binary-subarrays-with-sum.js b/930.binary-subarrays-with-sum.js similarity index 100% rename from 930-binary-subarrays-with-sum.js rename to 930.binary-subarrays-with-sum.js diff --git a/931-minimum-falling-path-sum.js b/931.minimum-falling-path-sum.js similarity index 100% rename from 931-minimum-falling-path-sum.js rename to 931.minimum-falling-path-sum.js diff --git a/932-beautiful-array.js b/932.beautiful-array.js similarity index 100% rename from 932-beautiful-array.js rename to 932.beautiful-array.js diff --git a/933-number-of-recent-calls.js b/933.number-of-recent-calls.js similarity index 100% rename from 933-number-of-recent-calls.js rename to 933.number-of-recent-calls.js diff --git a/934-shortest-bridge.js b/934.shortest-bridge.js similarity index 100% rename from 934-shortest-bridge.js rename to 934.shortest-bridge.js diff --git a/936-stamping-the-sequence.js b/936.stamping-the-sequence.js similarity index 100% rename from 936-stamping-the-sequence.js rename to 936.stamping-the-sequence.js diff --git a/937-reorder-data-in-log-files.js b/937.reorder-data-in-log-files.js similarity index 100% rename from 937-reorder-data-in-log-files.js rename to 937.reorder-data-in-log-files.js diff --git a/938-range-sum-of-bst.js b/938.range-sum-of-bst.js similarity index 100% rename from 938-range-sum-of-bst.js rename to 938.range-sum-of-bst.js diff --git a/939-minimum-area-rectangle.js b/939.minimum-area-rectangle.js similarity index 100% rename from 939-minimum-area-rectangle.js rename to 939.minimum-area-rectangle.js diff --git a/94-binary-tree-inorder-traversal.js b/94.binary-tree-inorder-traversal.js old mode 100755 new mode 100644 similarity index 94% rename from 94-binary-tree-inorder-traversal.js rename to 94.binary-tree-inorder-traversal.js index 0c15eb87..e3a4e78c --- a/94-binary-tree-inorder-traversal.js +++ b/94.binary-tree-inorder-traversal.js @@ -1,27 +1,27 @@ -/** - * Definition for a binary tree node. - * function TreeNode(val) { - * this.val = val; - * this.left = this.right = null; - * } - */ -/** - * @param {TreeNode} root - * @return {number[]} - */ -const inorderTraversal = function(root) { - const res = []; - if (root == null) return res; - traversal(root, res); - return res; -}; - -function traversal(node, res) { - if (node.left) { - traversal(node.left, res); - } - res.push(node.val); - if (node.right) { - traversal(node.right, res); - } -} +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ +/** + * @param {TreeNode} root + * @return {number[]} + */ +const inorderTraversal = function(root) { + const res = []; + if (root == null) return res; + traversal(root, res); + return res; +}; + +function traversal(node, res) { + if (node.left) { + traversal(node.left, res); + } + res.push(node.val); + if (node.right) { + traversal(node.right, res); + } +} diff --git a/940-distinct-subsequences-ii.js b/940.distinct-subsequences-ii.js similarity index 100% rename from 940-distinct-subsequences-ii.js rename to 940.distinct-subsequences-ii.js diff --git a/941-valid-mountain-array.js b/941.valid-mountain-array.js similarity index 100% rename from 941-valid-mountain-array.js rename to 941.valid-mountain-array.js diff --git a/942-di-string-match.js b/942.di-string-match.js similarity index 100% rename from 942-di-string-match.js rename to 942.di-string-match.js diff --git a/943-find-the-shortest-superstring.js b/943.find-the-shortest-superstring.js similarity index 100% rename from 943-find-the-shortest-superstring.js rename to 943.find-the-shortest-superstring.js diff --git a/944-delete-columns-to-make-sorted.js b/944.delete-columns-to-make-sorted.js similarity index 100% rename from 944-delete-columns-to-make-sorted.js rename to 944.delete-columns-to-make-sorted.js diff --git a/945-minimum-increment-to-make-array-unique.js b/945.minimum-increment-to-make-array-unique.js similarity index 100% rename from 945-minimum-increment-to-make-array-unique.js rename to 945.minimum-increment-to-make-array-unique.js diff --git a/946-validate-stack-sequences.js b/946.validate-stack-sequences.js similarity index 100% rename from 946-validate-stack-sequences.js rename to 946.validate-stack-sequences.js diff --git a/947-most-stones-removed-with-same-row-or-column.js b/947.most-stones-removed-with-same-row-or-column.js similarity index 100% rename from 947-most-stones-removed-with-same-row-or-column.js rename to 947.most-stones-removed-with-same-row-or-column.js diff --git a/948-bag-of-tokens.js b/948.bag-of-tokens.js similarity index 100% rename from 948-bag-of-tokens.js rename to 948.bag-of-tokens.js diff --git a/949-largest-time-for-given-digits.js b/949.largest-time-for-given-digits.js similarity index 100% rename from 949-largest-time-for-given-digits.js rename to 949.largest-time-for-given-digits.js diff --git a/95-unique-binary-search-trees-ii.js b/95.unique-binary-search-trees-ii.js similarity index 100% rename from 95-unique-binary-search-trees-ii.js rename to 95.unique-binary-search-trees-ii.js diff --git a/950-reveal-cards-in-increasing-order.js b/950.reveal-cards-in-increasing-order.js similarity index 100% rename from 950-reveal-cards-in-increasing-order.js rename to 950.reveal-cards-in-increasing-order.js diff --git a/951-flip-equivalent-binary-trees.js b/951.flip-equivalent-binary-trees.js similarity index 100% rename from 951-flip-equivalent-binary-trees.js rename to 951.flip-equivalent-binary-trees.js diff --git a/952-largest-component-size-by-common-factor.js b/952.largest-component-size-by-common-factor.js similarity index 100% rename from 952-largest-component-size-by-common-factor.js rename to 952.largest-component-size-by-common-factor.js diff --git a/953-verifying-an-alien-dictionary.js b/953.verifying-an-alien-dictionary.js similarity index 100% rename from 953-verifying-an-alien-dictionary.js rename to 953.verifying-an-alien-dictionary.js diff --git a/954-array-of-doubled-pairs.js b/954.array-of-doubled-pairs.js similarity index 100% rename from 954-array-of-doubled-pairs.js rename to 954.array-of-doubled-pairs.js diff --git a/955-delete-columns-to-make-sorted-ii.js b/955.delete-columns-to-make-sorted-ii.js similarity index 100% rename from 955-delete-columns-to-make-sorted-ii.js rename to 955.delete-columns-to-make-sorted-ii.js diff --git a/956-tallest-billboard.js b/956.tallest-billboard.js similarity index 100% rename from 956-tallest-billboard.js rename to 956.tallest-billboard.js diff --git a/957-prison-cells-after-n-days.js b/957.prison-cells-after-n-days.js similarity index 100% rename from 957-prison-cells-after-n-days.js rename to 957.prison-cells-after-n-days.js diff --git a/958-check-completeness-of-a-binary-tree.js b/958.check-completeness-of-a-binary-tree.js similarity index 100% rename from 958-check-completeness-of-a-binary-tree.js rename to 958.check-completeness-of-a-binary-tree.js diff --git a/959-regions-cut-by-slashes.js b/959.regions-cut-by-slashes.js similarity index 100% rename from 959-regions-cut-by-slashes.js rename to 959.regions-cut-by-slashes.js diff --git a/96-unique-binary-search-trees.js b/96.unique-binary-search-trees.js similarity index 100% rename from 96-unique-binary-search-trees.js rename to 96.unique-binary-search-trees.js diff --git a/960-delete-columns-to-make-sorted-iii.js b/960.delete-columns-to-make-sorted-iii.js similarity index 100% rename from 960-delete-columns-to-make-sorted-iii.js rename to 960.delete-columns-to-make-sorted-iii.js diff --git a/961-n-repeated-element-in-size-2n-array.js b/961.n-repeated-element-in-size-2n-array.js similarity index 100% rename from 961-n-repeated-element-in-size-2n-array.js rename to 961.n-repeated-element-in-size-2n-array.js diff --git a/964-least-operators-to-express-number.js b/964.least-operators-to-express-number.js similarity index 100% rename from 964-least-operators-to-express-number.js rename to 964.least-operators-to-express-number.js diff --git a/965-univalued-binary-tree.js b/965.univalued-binary-tree.js similarity index 100% rename from 965-univalued-binary-tree.js rename to 965.univalued-binary-tree.js diff --git a/967-numbers-with-same-consecutive-differences.js b/967.numbers-with-same-consecutive-differences.js similarity index 100% rename from 967-numbers-with-same-consecutive-differences.js rename to 967.numbers-with-same-consecutive-differences.js diff --git a/968-binary-tree-cameras.js b/968.binary-tree-cameras.js similarity index 100% rename from 968-binary-tree-cameras.js rename to 968.binary-tree-cameras.js diff --git a/969-pancake-sorting.js b/969.pancake-sorting.js similarity index 100% rename from 969-pancake-sorting.js rename to 969.pancake-sorting.js diff --git a/97-interleaving-string.js b/97.interleaving-string.js similarity index 100% rename from 97-interleaving-string.js rename to 97.interleaving-string.js diff --git a/971-flip-binary-tree-to-match-preorder-traversal.js b/971.flip-binary-tree-to-match-preorder-traversal.js similarity index 100% rename from 971-flip-binary-tree-to-match-preorder-traversal.js rename to 971.flip-binary-tree-to-match-preorder-traversal.js diff --git a/972-equal-rational-numbers.js b/972.equal-rational-numbers.js similarity index 100% rename from 972-equal-rational-numbers.js rename to 972.equal-rational-numbers.js diff --git a/973-k-closest-points-to-origin.js b/973.k-closest-points-to-origin.js similarity index 100% rename from 973-k-closest-points-to-origin.js rename to 973.k-closest-points-to-origin.js diff --git a/974-subarray-sums-divisible-by-k.js b/974.subarray-sums-divisible-by-k.js similarity index 100% rename from 974-subarray-sums-divisible-by-k.js rename to 974.subarray-sums-divisible-by-k.js diff --git a/975-odd-even-jump.js b/975.odd-even-jump.js similarity index 100% rename from 975-odd-even-jump.js rename to 975.odd-even-jump.js diff --git a/976-largest-perimeter-triangle.js b/976.largest-perimeter-triangle.js similarity index 100% rename from 976-largest-perimeter-triangle.js rename to 976.largest-perimeter-triangle.js diff --git a/977-squares-of-a-sorted-array.js b/977.squares-of-a-sorted-array.js similarity index 100% rename from 977-squares-of-a-sorted-array.js rename to 977.squares-of-a-sorted-array.js diff --git a/978-longest-turbulent-subarray.js b/978.longest-turbulent-subarray.js similarity index 100% rename from 978-longest-turbulent-subarray.js rename to 978.longest-turbulent-subarray.js diff --git a/979-distribute-coins-in-binary-tree.js b/979.distribute-coins-in-binary-tree.js similarity index 100% rename from 979-distribute-coins-in-binary-tree.js rename to 979.distribute-coins-in-binary-tree.js diff --git a/98-validate-binary-search-tree.js b/98.validate-binary-search-tree.js similarity index 100% rename from 98-validate-binary-search-tree.js rename to 98.validate-binary-search-tree.js diff --git a/980-unique-paths-iii.js b/980.unique-paths-iii.js similarity index 100% rename from 980-unique-paths-iii.js rename to 980.unique-paths-iii.js diff --git a/981-time-based-key-value-store.js b/981.time-based-key-value-store.js similarity index 100% rename from 981-time-based-key-value-store.js rename to 981.time-based-key-value-store.js diff --git a/982-triples-with-bitwise-and-equal-to-zero.js b/982.triples-with-bitwise-and-equal-to-zero.js similarity index 100% rename from 982-triples-with-bitwise-and-equal-to-zero.js rename to 982.triples-with-bitwise-and-equal-to-zero.js diff --git a/983-minimum-cost-for-tickets.js b/983.minimum-cost-for-tickets.js similarity index 100% rename from 983-minimum-cost-for-tickets.js rename to 983.minimum-cost-for-tickets.js diff --git a/984-string-without-aaa-or-bbb.js b/984.string-without-aaa-or-bbb.js similarity index 100% rename from 984-string-without-aaa-or-bbb.js rename to 984.string-without-aaa-or-bbb.js diff --git a/985-sum-of-even-numbers-after-queries.js b/985.sum-of-even-numbers-after-queries.js similarity index 100% rename from 985-sum-of-even-numbers-after-queries.js rename to 985.sum-of-even-numbers-after-queries.js diff --git a/986-interval-list-intersections.js b/986.interval-list-intersections.js similarity index 100% rename from 986-interval-list-intersections.js rename to 986.interval-list-intersections.js diff --git a/987-vertical-order-traversal-of-a-binary-tree.js b/987.vertical-order-traversal-of-a-binary-tree.js similarity index 100% rename from 987-vertical-order-traversal-of-a-binary-tree.js rename to 987.vertical-order-traversal-of-a-binary-tree.js diff --git a/988-smallest-string-starting-from-leaf.js b/988.smallest-string-starting-from-leaf.js similarity index 100% rename from 988-smallest-string-starting-from-leaf.js rename to 988.smallest-string-starting-from-leaf.js diff --git a/989-add-to-array-form-of-integer.js b/989.add-to-array-form-of-integer.js similarity index 100% rename from 989-add-to-array-form-of-integer.js rename to 989.add-to-array-form-of-integer.js diff --git a/99-recover-binary-search-tree.js b/99.recover-binary-search-tree.js similarity index 100% rename from 99-recover-binary-search-tree.js rename to 99.recover-binary-search-tree.js diff --git a/990-satisfiability-of-equality-equations.js b/990.satisfiability-of-equality-equations.js similarity index 100% rename from 990-satisfiability-of-equality-equations.js rename to 990.satisfiability-of-equality-equations.js diff --git a/992-subarrays-with-k-different-integers.js b/992.subarrays-with-k-different-integers.js similarity index 100% rename from 992-subarrays-with-k-different-integers.js rename to 992.subarrays-with-k-different-integers.js diff --git a/993.cousins-in-binary-tree.js b/993.cousins.in-binary-tree.js similarity index 100% rename from 993.cousins-in-binary-tree.js rename to 993.cousins.in-binary-tree.js diff --git a/994-rotting-oranges.js b/994.rotting-oranges.js similarity index 100% rename from 994-rotting-oranges.js rename to 994.rotting-oranges.js diff --git a/995-minimum-number-of-k-consecutive-bit-flips.js b/995.minimum-number-of-k-consecutive-bit-flips.js similarity index 100% rename from 995-minimum-number-of-k-consecutive-bit-flips.js rename to 995.minimum-number-of-k-consecutive-bit-flips.js diff --git a/996-number-of-squareful-arrays.js b/996.number-of-squareful-arrays.js similarity index 100% rename from 996-number-of-squareful-arrays.js rename to 996.number-of-squareful-arrays.js diff --git a/997-find-the-town-judge.js b/997.find-the-town-judge.js similarity index 100% rename from 997-find-the-town-judge.js rename to 997.find-the-town-judge.js diff --git a/999-available-captures-for-rook.js b/999.available-captures-for-rook.js similarity index 100% rename from 999-available-captures-for-rook.js rename to 999.available-captures-for-rook.js diff --git a/TBD-leftmost-column-with-at-least-a-one.js b/TBD-leftmost-column-with-at-least-a-one.js deleted file mode 100644 index bfd5e911..00000000 --- a/TBD-leftmost-column-with-at-least-a-one.js +++ /dev/null @@ -1,34 +0,0 @@ -/** - * // This is the BinaryMatrix's API interface. - * // You should not implement it, or speculate about its implementation - * function BinaryMatrix() { - * @param {integer} x, y - * @return {integer} - * this.get = function(x, y) { - * ... - * }; - * - * @return {[integer, integer]} - * this.dimensions = function() { - * ... - * }; - * }; - */ - -/** - * @param {BinaryMatrix} binaryMatrix - * @return {number} - */ -const leftMostColumnWithOne = function (binaryMatrix) { - const [rows, cols] = binaryMatrix.dimensions() - let candidate = -1 - for (let r = 0, c = cols - 1; r < rows && c >= 0; ) { - if (binaryMatrix.get(r, c) === 1) { - candidate = c - c-- - } else { - r++ - } - } - return candidate -} diff --git a/clean-dataset.py b/clean-dataset.py index 775c093f..d8e8c8a1 100644 --- a/clean-dataset.py +++ b/clean-dataset.py @@ -12,4 +12,6 @@ # Rewrite the file with open(js_file, 'w', encoding='utf-8') as f: f.writelines(new_lines) - + # Rename the file - replace first '-' with '.' + new_name = js_file.replace('-', '.', 1) + os.rename(js_file, new_name) diff --git a/tmp.js b/tmp.js deleted file mode 100644 index 4e53563a..00000000 --- a/tmp.js +++ /dev/null @@ -1,141 +0,0 @@ -/** - * @param {string} s - * @return {boolean} - */ -var isDecomposable = function(s) { - let hasTwo = false - let i = 0 - let j = 0 - - while(j < s.length) { - while(j + 1 < s.length && s[j + 1] === s[i]) { - j += 1 - } - - if (((j - i + 1) % 3) === 2) { - if (!hasTwo) { - hasTwo = true - } else { - return false - } - } else if (((j - i + 1) % 3) === 1) { - return false - } - j++ - i = j - } - - return hasTwo -}; - - -class Solution { -public: - vector longestCommomSubsequence(vector>& arrays) { - int n = arrays.size(); - vector nums = vector(100); // 100 possible numbers as stated in the question - for (int i = 0; i < n; i++) { - for (int j = 0; j < arrays[i].size(); j++) { - nums[arrays[i][j] - 1]++; // count occurrences - } - } - vector ans; - for (int i = 0; i < 100; i++) { - if (nums[i] == n) ans.push_back(i + 1); // save it if it appears in every array - } - return ans; - } -}; - -class Solution { - public int[] findMaximums(int[] nums) { - if(nums == null || nums.length == 0) return nums; - // calc the [l, r] for each ele where in [l, r]: ele is the min value - int len = nums.length; - TreeSet idx = new TreeSet<>(); - Integer[] indices = new Integer[len]; - for(int i = 0; i < len; i++) indices[i] = i; - Arrays.sort(indices, (l, r) -> nums[l] - nums[r]); - int prev = -1; - int[] ranges = new int[len]; - Queue sameLevel = new LinkedList<>(); - int[] ans = new int[len]; - for(int i = 0; i < len; i++) { - if(nums[indices[i]] > prev) { - while(!sameLevel.isEmpty()) { - idx.add(sameLevel.poll()); - } - } - Integer l = idx.lower(indices[i]); - Integer r = idx.higher(indices[i]); - ranges[indices[i]] = (r == null?len - 1:r - 1) - (l == null?0:l + 1) + 1; - prev = nums[indices[i]]; - sameLevel.add(indices[i]); - } - // we iterate ranges from maximum to minimum to construct the ans array - int j = len - 1; - for(int i = len - 1; i >= 0; i--) { - while(j >= 0 && ranges[indices[j]] < len - i) { - j--; - } - ans[len - 1 - i] = nums[indices[j]]; - } - return ans; - } -} - -class Solution: - def minDayskVariants(self, points: List[List[int]], k: int) -> int: - lo = 0 - hi = int(1e9) - - # binary search check helper function - def check(day): - lines = collections.defaultdict(collections.Counter) - - # 2d sweep line - for x, y in points: - lbx, lby = (x, y - day) # left point - ubx, uby = (x - day, y) # bottom point - - # lbx + lby == ubx + uby == new x axis's open line - lines[lbx+lby][lby-lbx] += 1 - lines[ubx+uby][uby-ubx+1] -= 1 # - - # lbx + lby == ubx + uby == new x axis's close line - lbx, lby = (x + day, y) # right point - ubx, uby = (x, y + day) # upper point - lines[lbx+lby+1][lby-lbx] -= 1 - lines[ubx+uby+1][uby-ubx+1] += 1 - - # hold a new ranges to sweep all lines from left to right on new x axis - ranges = collections.Counter() - - # for every critical points on new x axis (it's a diag on the original axis), - # add the sweep lines on new y axis - for diag in sorted(lines): - for num in sorted(lines[diag]): - cnt = lines[diag][num] - ranges[num] += cnt - - # for every critical points, check whether there is an area having - # overlapping points >= k - cur = 0 - for num in sorted(ranges): - cnt = ranges[num] - cur += cnt - - if cur >= k: - return True - - return False - - # binary search - while lo < hi: - mid = (lo + hi) // 2 - if check(mid): - hi = mid - else: - lo = mid + 1 - - return lo From 5dea89558d28654c0731588bb3f10bb6ffd2809b Mon Sep 17 00:00:00 2001 From: Ognjen Date: Tue, 25 Jul 2023 23:41:07 +0200 Subject: [PATCH 3/3] Remove problem 1514 (incorrect solution) --- 1514.path-with-maximum-probability.js | 105 -------------------------- 1 file changed, 105 deletions(-) delete mode 100644 1514.path-with-maximum-probability.js diff --git a/1514.path-with-maximum-probability.js b/1514.path-with-maximum-probability.js deleted file mode 100644 index bd47209e..00000000 --- a/1514.path-with-maximum-probability.js +++ /dev/null @@ -1,105 +0,0 @@ -/** - * @param {number} n - * @param {number[][]} edges - * @param {number[]} succProb - * @param {number} start - * @param {number} end - * @return {number} - */ -const maxProbability = function (n, edges, succProb, start, end) { - const g = {} - for (let i = 0; i < edges.length; ++i) { - const a = edges[i][0], - b = edges[i][1] - if (g[a] == null) g[a] = [] - if (g[b] == null) g[b] = [] - g[a].push([b, i]) - g[b].push([a, i]) - } - const p = new Array(n).fill(0) - p[start] = 1 - const pq = new PriorityQueue((a, b) => p[a] > p[b]) - pq.push(start) - while (!pq.isEmpty()) { - const cur = pq.pop() - if (cur === end) { - return p[end] - } - for (let a of g[cur] || []) { - const neighbor = a[0], - index = a[1] - if (p[cur] * succProb[index] > p[neighbor]) { - p[neighbor] = p[cur] * succProb[index] - pq.push(neighbor) - } - } - } - return 0 -} -class PriorityQueue { - constructor(comparator = (a, b) => a > b) { - this.heap = [] - this.top = 0 - this.comparator = comparator - } - size() { - return this.heap.length - } - isEmpty() { - return this.size() === 0 - } - peek() { - return this.heap[this.top] - } - push(...values) { - values.forEach((value) => { - this.heap.push(value) - this.siftUp() - }) - return this.size() - } - pop() { - const poppedValue = this.peek() - const bottom = this.size() - 1 - if (bottom > this.top) { - this.swap(this.top, bottom) - } - this.heap.pop() - this.siftDown() - return poppedValue - } - replace(value) { - const replacedValue = this.peek() - this.heap[this.top] = value - this.siftDown() - return replacedValue - } - - parent = (i) => ((i + 1) >>> 1) - 1 - left = (i) => (i << 1) + 1 - right = (i) => (i + 1) << 1 - greater = (i, j) => this.comparator(this.heap[i], this.heap[j]) - swap = (i, j) => ([this.heap[i], this.heap[j]] = [this.heap[j], this.heap[i]]) - siftUp = () => { - let node = this.size() - 1 - while (node > this.top && this.greater(node, this.parent(node))) { - this.swap(node, this.parent(node)) - node = this.parent(node) - } - } - siftDown = () => { - let node = this.top - while ( - (this.left(node) < this.size() && this.greater(this.left(node), node)) || - (this.right(node) < this.size() && this.greater(this.right(node), node)) - ) { - let maxChild = - this.right(node) < this.size() && - this.greater(this.right(node), this.left(node)) - ? this.right(node) - : this.left(node) - this.swap(node, maxChild) - node = maxChild - } - } -}