├── testcases ├── 7 ├── 21 ├── 22 ├── 30 ├── 32 ├── 39 ├── 45 ├── 53 ├── 57 ├── 61 ├── 71 ├── 72 ├── 74 ├── 76 ├── 78 ├── 80 ├── 82 ├── 91 ├── 99 ├── 100 ├── 107 ├── 111 ├── 115 ├── 119 ├── 121 ├── 122 ├── 125 ├── 127 ├── 133 ├── 134 ├── 139 ├── 142 ├── 143 ├── 147 ├── 151 ├── 152 ├── 153 ├── 154 ├── 159 ├── 165 ├── 171 ├── 179 ├── 187 ├── 190 ├── 198 ├── 207 ├── 211 ├── 213 ├── 216 ├── 218 ├── 221 ├── 222 ├── 227 ├── 228 ├── 229 ├── 230 ├── 231 ├── 234 ├── 239 ├── 252 ├── 264 ├── 268 ├── 273 ├── 284 ├── 290 ├── 295 ├── 299 ├── 310 ├── 316 ├── 317 ├── 319 ├── 320 ├── 328 ├── 338 ├── 342 ├── 347 ├── 348 ├── 351 ├── 361 ├── 362 ├── 367 ├── 378 ├── 380 ├── 394 ├── 395 ├── 399 ├── 402 ├── 406 ├── 417 ├── 419 ├── 428 ├── 431 ├── 435 ├── 436 ├── 437 ├── 442 ├── 445 ├── 449 ├── 450 ├── 451 ├── 456 ├── 458 ├── 462 ├── 463 ├── 474 ├── 520 ├── 525 ├── 532 ├── 540 ├── 563 ├── 567 ├── 573 ├── 593 ├── 605 ├── 622 ├── 623 ├── 646 ├── 655 ├── 658 ├── 659 ├── 662 ├── 665 ├── 673 ├── 677 ├── 700 ├── 701 ├── 702 ├── 705 ├── 708 ├── 714 ├── 721 ├── 729 ├── 733 ├── 738 ├── 767 ├── 768 ├── 769 ├── 775 ├── 784 ├── 785 ├── 791 ├── 794 ├── 797 ├── 799 ├── 820 ├── 823 ├── 825 ├── 830 ├── 833 ├── 835 ├── 838 ├── 845 ├── 846 ├── 849 ├── 858 ├── 859 ├── 863 ├── 880 ├── 881 ├── 886 ├── 901 ├── 902 ├── 905 ├── 906 ├── 910 ├── 918 ├── 919 ├── 930 ├── 933 ├── 935 ├── 946 ├── 958 ├── 962 ├── 967 ├── 968 ├── 969 ├── 971 ├── 973 ├── 977 ├── 980 ├── 986 ├── 987 ├── 991 ├── 993 ├── 1004 ├── 1007 ├── 1015 ├── 1024 ├── 1026 ├── 1029 ├── 1032 ├── 1035 ├── 1052 ├── 1053 ├── 1074 ├── 1094 ├── 1130 ├── 1162 ├── 1192 ├── 1217 ├── 1219 ├── 1232 ├── 1239 ├── 1249 ├── 1283 ├── 1305 ├── 1306 ├── 1332 ├── 1344 ├── 1354 ├── 1375 ├── 1437 ├── 1441 ├── 1442 ├── 1444 ├── 1446 ├── 1447 ├── 1448 ├── 1450 ├── 1451 ├── 1452 ├── 1455 ├── 1456 ├── 1460 ├── 1464 ├── 1466 ├── 1472 ├── 1475 ├── 1476 ├── 1482 ├── 1486 ├── 1491 ├── 1492 ├── 1493 ├── 1496 ├── 1502 ├── 1507 ├── 1508 ├── 1509 ├── 1510 ├── 1512 ├── 1513 ├── 1518 ├── 1523 ├── 1525 ├── 1529 ├── 1530 ├── 1533 ├── 1539 ├── 1544 ├── 1545 ├── 1550 ├── 1551 ├── 1552 ├── 1556 ├── 1557 ├── 1560 ├── 1566 ├── 1572 ├── 1576 ├── 1578 ├── 1582 ├── 1583 ├── 1588 ├── 1592 ├── 1598 ├── 1600 ├── 1602 ├── 1604 ├── 1608 ├── 1609 ├── 1615 ├── 1618 ├── 1624 ├── 1629 ├── 1634 ├── 1640 ├── 1641 ├── 1642 ├── 1644 ├── 1647 ├── 1652 ├── 1653 ├── 1657 ├── 1663 ├── 1664 ├── 1673 ├── 1705 ├── 1721 ├── 1749 ├── 1780 └── 1839 └── src ├── 1426.counting-elements.py ├── 178.rank-scores.sql ├── 1929.concatenation-of-array.py ├── 344.reverse-string.py ├── 1920.build-array-from-permutation.py ├── 1427.perform-string-shifts.py ├── 1816.truncate-sentence.py ├── 2185.counting-words-with-a-given-prefix.py ├── 2119.a-number-after-a-double-reversal.py ├── 1784.check-if-binary-string-has-at-most-one-segment-of-ones.py ├── 1480.running-sum-of-1-d-array.py ├── 46.permutations.py ├── 1332.remove-palindromic-subsequences.py ├── 1460.make-two-arrays-equal-by-reversing-sub-arrays.py ├── 1812.determine-color-of-a-chessboard-square.py ├── 1491.average-salary-excluding-the-minimum-and-maximum-salary.py ├── 1523.count-odd-numbers-in-an-interval-range.py ├── 1529.bulb-switcher-iv.py ├── 1528.shuffle-string.py ├── 3084.count-substrings-starting-and-ending-with-given-character.py ├── 1832.check-if-the-sentence-is-pangram.py ├── 717.1-bit-and-2-bit-characters.py ├── 171.excel-sheet-column-number.py ├── 1837.sum-of-digits-in-base-k.py ├── 387.first-unique-character-in-a-string.py ├── 1684.count-the-number-of-consistent-strings.py ├── 1344.angle-between-hands-of-a-clock.py ├── 2000.reverse-prefix-of-word.py ├── 1450.number-of-students-doing-homework-at-a-given-time.py ├── 1903.largest-odd-number-in-string.py ├── 869.reordered-power-of-2.py ├── 1217.minimum-cost-to-move-chips-to-the-same-position.py ├── 1556.thousand-separator.py ├── 1773.count-items-matching-a-rule.py ├── 2073.time-needed-to-buy-tickets.py ├── 1572.matrix-diagonal-sum.py ├── 1732.find-the-highest-altitude.py ├── 2103.rings-and-rods.py ├── 532.k-diff-pairs-in-an-array.py ├── 462.minimum-moves-to-equal-array-elements-ii.py ├── 1446.consecutive-characters.py ├── 1748.sum-of-unique-elements.py ├── 1598.crawler-log-folder.py ├── 89.gray-code.py ├── 2186.minimum-number-of-steps-to-make-two-strings-anagram-ii.py ├── 7.reverse-integer.py ├── 1768.merge-strings-alternately.py ├── 495.teemo-attacking.py ├── 520.detect-capital.py ├── 1375.bulb-switcher-iii.py ├── 1877.minimize-maximum-pair-sum-in-array.py ├── 1561.maximum-number-of-coins-you-can-get.py ├── 406.queue-reconstruction-by-height.py ├── 454.4-sum-ii.py ├── 633.sum-of-square-numbers.py ├── 3083.existence-of-a-substring-in-a-string-and-its-reverse.py ├── 343.integer-break.py ├── 476.number-complement.py ├── 724.find-pivot-index.py ├── 1451.rearrange-words-in-a-sentence.py ├── 1550.three-consecutive-odds.py ├── 2139.minimum-moves-to-reach-target-score.py ├── 402.remove-k-digits.py ├── 1507.reformat-date.py ├── 1614.maximum-nesting-depth-of-the-parentheses.py ├── 1848.minimum-distance-to-the-target-element.py ├── 1844.replace-all-digits-with-characters.py ├── 12.integer-to-roman.py ├── 2176.count-equal-and-divisible-pairs-in-an-array.py ├── 80.remove-duplicates-from-sorted-array-ii.py ├── 848.shifting-letters.py ├── 1502.can-make-arithmetic-progression-from-sequence.py ├── 2042.check-if-numbers-are-ascending-in-a-sentence.py ├── 252.py ├── 1827.minimum-operations-to-make-the-array-increasing.py ├── 2138.divide-a-string-into-groups-of-size-k.py ├── 165.compare-version-numbers.py ├── 1833.maximum-ice-cream-bars.py ├── 789.escape-the-ghosts.py ├── 1897.redistribute-characters-to-make-all-strings-equal.py ├── 456.132-pattern.py ├── 342.power-of-four.py ├── 377.combination-sum-iv.py ├── 1657.determine-if-two-strings-are-close.py ├── 2190.most-frequent-number-following-key-in-an-array.py ├── 320.py ├── 71.simplify-path.py ├── 1544.make-the-string-great.py ├── 1828.queries-on-number-of-points-inside-a-circle.py ├── 376.wiggle-subsequence.py ├── 739.daily-temperatures.py ├── 946.validate-stack-sequences.py ├── 1716.calculate-money-in-leetcode-bank.py ├── 357.count-numbers-with-unique-digits.py ├── 1003.check-if-word-is-valid-after-substitutions.py ├── 1592.rearrange-spaces-between-words.py ├── 1646.get-maximum-in-generated-array.py ├── 2085.count-common-words-with-one-occurrence.py ├── 78.subsets.py ├── 853.car-fleet.py ├── 916.word-subsets.py ├── 1647.minimum-deletions-to-make-character-frequencies-unique.py ├── 2062.count-vowel-substrings-of-a-string.py ├── 2191.sort-the-jumbled-numbers.py ├── 1015.smallest-integer-divisible-by-k.py ├── 1464.maximum-product-of-two-elements-in-an-array.py ├── 1496.path-crossing.py ├── 791.custom-sort-string.py ├── 1289.minimum-falling-path-sum-ii.py ├── 1470.shuffle-the-array.py ├── 3005.count-elements-with-maximum-frequency.py ├── 881.boats-to-save-people.py ├── 976.largest-perimeter-triangle.py ├── 830.positions-of-large-groups.py ├── 1624.largest-substring-between-two-equal-characters.py ├── 941.valid-mountain-array.py ├── 1218.longest-arithmetic-subsequence-of-given-difference.py ├── 1679.max-number-of-k-sum-pairs.py ├── 1752.check-if-array-is-sorted-and-rotated.py ├── 702.py ├── 1508.range-sum-of-sorted-subarray-sums.py ├── 3107.minimum-operations-to-make-median-of-array-equal-to-k.py ├── 1962.remove-stones-to-minimize-the-total.py ├── 2824.count-pairs-whose-sum-is-less-than-target.py ├── 13.roman-to-integer.py ├── 1306.jump-game-iii.py ├── 1512.number-of-good-pairs.py ├── 1759.count-number-of-homogenous-substrings.py ├── 1974.minimum-time-to-type-word-using-special-typewriter.py ├── 354.russian-doll-envelopes.py ├── 605.can-place-flowers.py ├── 991.broken-calculator.py ├── 141.linked-list-cycle.py ├── 1582.special-positions-in-a-binary-matrix.py ├── 2303.calculate-amount-paid-in-taxes.py ├── 2347.best-poker-hand.py ├── 2370.longest-ideal-subsequence.py ├── 153.find-minimum-in-rotated-sorted-array.py ├── 1277.count-square-submatrices-with-all-ones.py ├── 1791.find-center-of-star-graph.py ├── 300.longest-increasing-subsequence.py ├── 970.powerful-integers.py ├── 1290.convert-binary-number-in-a-linked-list-to-integer.py ├── 1710.maximum-units-on-a-truck.py ├── 1991.find-the-middle-index-in-array.py ├── 458.poor-pigs.py ├── 1481.least-number-of-unique-integers-after-k-removals.py ├── 1725.number-of-rectangles-that-can-form-the-largest-square.py ├── 528.random-pick-with-weight.py ├── 1557.minimum-number-of-vertices-to-reach-all-nodes.py ├── 1758.minimum-changes-to-make-alternating-binary-string.py ├── 1859.sorting-the-sentence.py ├── 3105.longest-strictly-increasing-or-strictly-decreasing-subarray.py ├── 2140.solving-questions-with-brainpower.py ├── 470.implement-rand-10-using-rand-7.py ├── 797.all-paths-from-source-to-target.py ├── 1493.longest-subarray-of-1-s-after-deleting-one-element.py ├── 1525.number-of-good-ways-to-split-a-string.py ├── 1685.sum-of-absolute-differences-in-a-sorted-array.py ├── 1792.maximum-average-pass-ratio.py ├── 2087.minimum-cost-homecoming-of-a-robot-in-a-grid.py ├── 3090.maximum-length-substring-with-two-occurrences.py ├── 667.beautiful-arrangement-ii.py ├── 846.hand-of-straights.py ├── 954.array-of-doubled-pairs.py ├── 1534.count-good-triplets.py ├── 2389.longest-subsequence-with-limited-sum.py ├── 554.brick-wall.py ├── 583.delete-operation-for-two-strings.py ├── 104.maximum-depth-of-binary-tree.py ├── 1963.minimum-number-of-swaps-to-make-the-string-balanced.py ├── 389.find-the-difference.py ├── 2091.removing-minimum-and-maximum-from-array.py ├── 32.longest-valid-parentheses.py ├── 1545.find-kth-bit-in-nth-binary-string.py ├── 2270.number-of-ways-to-split-array.py ├── 861.score-after-flipping-matrix.py ├── 91.decode-ways.py ├── 1860.incremental-memory-leak.py ├── 20.valid-parentheses.py ├── 24.swap-nodes-in-pairs.py ├── 231.power-of-two.py └── 611.valid-triangle-number.py /testcases/1641: -------------------------------------------------------------------------------- 1 | 1 2 | 2 3 | 30 -------------------------------------------------------------------------------- /testcases/222: -------------------------------------------------------------------------------- 1 | [1,2,3,4,5,6] -------------------------------------------------------------------------------- /testcases/347: -------------------------------------------------------------------------------- 1 | [1,1,1,2,2,3] 2 | 2 -------------------------------------------------------------------------------- /testcases/700: -------------------------------------------------------------------------------- 1 | [4,2,7,1,3] 2 | 2 -------------------------------------------------------------------------------- /testcases/797: -------------------------------------------------------------------------------- 1 | [[1,2],[3],[3],[]] -------------------------------------------------------------------------------- /testcases/107: -------------------------------------------------------------------------------- 1 | [3,9,20,null,null,15,7] -------------------------------------------------------------------------------- /testcases/1130: -------------------------------------------------------------------------------- 1 | [6,2,4] 2 | [6,15,5,2] -------------------------------------------------------------------------------- /testcases/252: -------------------------------------------------------------------------------- 1 | [[0,30],[5,10],[15,20]] -------------------------------------------------------------------------------- /testcases/445: -------------------------------------------------------------------------------- 1 | [7,2,4,3] 2 | [5,6,4] -------------------------------------------------------------------------------- /testcases/458: -------------------------------------------------------------------------------- 1 | 1000 2 | 15 3 | 60 4 | -------------------------------------------------------------------------------- /testcases/1508: -------------------------------------------------------------------------------- 1 | [1,2,3,4] 2 | 4 3 | 1 4 | 5 -------------------------------------------------------------------------------- /testcases/1624: -------------------------------------------------------------------------------- 1 | "aa" 2 | "aaa" 3 | "ababab" -------------------------------------------------------------------------------- /testcases/451: -------------------------------------------------------------------------------- 1 | "tree" 2 | "cccaaa" 3 | "Aabb" -------------------------------------------------------------------------------- /testcases/969: -------------------------------------------------------------------------------- 1 | [1,2,3] 2 | [3,2,4,1] 3 | -------------------------------------------------------------------------------- /testcases/1464: -------------------------------------------------------------------------------- 1 | [1,5,4,5] 2 | [3,4,5,2] 3 | [3,7] -------------------------------------------------------------------------------- /testcases/171: -------------------------------------------------------------------------------- 1 | ""A" 2 | "AB" 3 | "ZY" 4 | "AAA"" -------------------------------------------------------------------------------- /testcases/190: -------------------------------------------------------------------------------- 1 | 00000010100101000001111010011100 -------------------------------------------------------------------------------- /testcases/1029: -------------------------------------------------------------------------------- 1 | [[10,20],[30,200],[400,50],[30,20]] -------------------------------------------------------------------------------- /testcases/1447: -------------------------------------------------------------------------------- 1 | 1 2 | 2 3 | 3 4 | 41 5 | 20 6 | 100 -------------------------------------------------------------------------------- /testcases/147: -------------------------------------------------------------------------------- 1 | [4,2,1,3] 2 | [2,1] 3 | [1] 4 | [] 5 | -------------------------------------------------------------------------------- /testcases/1545: -------------------------------------------------------------------------------- 1 | 3 2 | 1 3 | 4 4 | 11 5 | 1 6 | 1 7 | -------------------------------------------------------------------------------- /testcases/1652: -------------------------------------------------------------------------------- 1 | [5,7,1,4] 2 | 3 3 | [2,4,9,3] 4 | -2 -------------------------------------------------------------------------------- /testcases/1780: -------------------------------------------------------------------------------- 1 | 6378022 2 | 12 3 | 10000000 4 | 4852229 -------------------------------------------------------------------------------- /testcases/228: -------------------------------------------------------------------------------- 1 | [0,1,2,4,5,7] 2 | [] 3 | [1] 4 | [1,2] -------------------------------------------------------------------------------- /testcases/406: -------------------------------------------------------------------------------- 1 | [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]] -------------------------------------------------------------------------------- /testcases/456: -------------------------------------------------------------------------------- 1 | [1,2,3,4] 2 | [3,1,4,2] 3 | [-1,3,2,0] -------------------------------------------------------------------------------- /testcases/665: -------------------------------------------------------------------------------- 1 | [3,4,2,3] 2 | [4,2,3] 3 | [1,2,7,2,5,9] -------------------------------------------------------------------------------- /testcases/78: -------------------------------------------------------------------------------- 1 | [1] 2 | [1,2] 3 | [1,2,3] 4 | [1,2,3,4] -------------------------------------------------------------------------------- /testcases/905: -------------------------------------------------------------------------------- 1 | [1] 2 | [1,2] 3 | [1,2,3] 4 | [1,2,3,4] -------------------------------------------------------------------------------- /testcases/935: -------------------------------------------------------------------------------- 1 | 1 2 | 2 3 | 4 4 | 10 5 | 3131 6 | 5000 -------------------------------------------------------------------------------- /testcases/99: -------------------------------------------------------------------------------- 1 | [1,3,null,null,2] 2 | [3,1,4,null,null,2] -------------------------------------------------------------------------------- /testcases/119: -------------------------------------------------------------------------------- 1 | 0 2 | 1 3 | 2 4 | 3 5 | 4 6 | 5 7 | 10 8 | -------------------------------------------------------------------------------- /testcases/1475: -------------------------------------------------------------------------------- 1 | [8,4,6,2,3] 2 | [1,2,3,4,5] 3 | [10,1,1,6] -------------------------------------------------------------------------------- /testcases/1486: -------------------------------------------------------------------------------- 1 | 5 2 | 0 3 | 4 4 | 3 5 | 1 6 | 7 7 | 10 8 | 5 -------------------------------------------------------------------------------- /testcases/1502: -------------------------------------------------------------------------------- 1 | [5,1,3] 2 | [4,1,3] 3 | [1,2] 4 | [1,10] -------------------------------------------------------------------------------- /testcases/1510: -------------------------------------------------------------------------------- 1 | 17 2 | 1 3 | 2 4 | 3 5 | 100 6 | 1000 7 | 47 -------------------------------------------------------------------------------- /testcases/153: -------------------------------------------------------------------------------- 1 | [3,4,5,1,2] 2 | [4,5,6,7,0,1,2] 3 | [3,1,2] -------------------------------------------------------------------------------- /testcases/351: -------------------------------------------------------------------------------- 1 | 1 2 | 1 3 | 1 4 | 9 5 | 4 6 | 4 7 | 2 8 | 5 -------------------------------------------------------------------------------- /testcases/45: -------------------------------------------------------------------------------- 1 | [2,3,1,1,4] 2 | [0] 3 | [2,3,0,1,4] 4 | [2,1] -------------------------------------------------------------------------------- /testcases/463: -------------------------------------------------------------------------------- 1 | [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]] -------------------------------------------------------------------------------- /testcases/520: -------------------------------------------------------------------------------- 1 | "USA" 2 | "FlaG" 3 | "Google" 4 | "leetcode" -------------------------------------------------------------------------------- /testcases/767: -------------------------------------------------------------------------------- 1 | "aab" 2 | "aaab" 3 | "ccccdddbba" 4 | "a" -------------------------------------------------------------------------------- /testcases/125: -------------------------------------------------------------------------------- 1 | "A man, a plan, a canal: Panama" 2 | "race a car" -------------------------------------------------------------------------------- /testcases/1448: -------------------------------------------------------------------------------- 1 | [3,1,4,3,null,1,5] 2 | [3,3,null,4,2] 3 | [1] -------------------------------------------------------------------------------- /testcases/1629: -------------------------------------------------------------------------------- 1 | [9,29,49,50] 2 | "cbcd" 3 | [1,2,3,4] 4 | "adcb" -------------------------------------------------------------------------------- /testcases/22: -------------------------------------------------------------------------------- 1 | 0 2 | 1 3 | 2 4 | 3 5 | 4 6 | 5 7 | 6 8 | 7 9 | -------------------------------------------------------------------------------- /testcases/227: -------------------------------------------------------------------------------- 1 | "3+2*22" 2 | " 3/2 " 3 | " 3+5 / 2 " 4 | "14-3/2" -------------------------------------------------------------------------------- /testcases/264: -------------------------------------------------------------------------------- 1 | 1 2 | 5 3 | 10 4 | 100 5 | 1000 6 | 1500 7 | 1690 -------------------------------------------------------------------------------- /testcases/442: -------------------------------------------------------------------------------- 1 | [4,3,2,7,8,2,3,1] 2 | [1,2,3,4,5,6,7,8,9,10,1] -------------------------------------------------------------------------------- /testcases/830: -------------------------------------------------------------------------------- 1 | "abbxxxxzzy" 2 | "abc" 3 | "abcdddeeeeaabbbcd" -------------------------------------------------------------------------------- /testcases/846: -------------------------------------------------------------------------------- 1 | [1,2,3,6,2,3,4,7,8] 2 | 3 3 | [1,2,3,4,5] 4 | 4 -------------------------------------------------------------------------------- /testcases/1217: -------------------------------------------------------------------------------- 1 | [1,2,3] 2 | [1,1000000000] 3 | [2,2,2,3,3] 4 | [1,3] -------------------------------------------------------------------------------- /testcases/133: -------------------------------------------------------------------------------- 1 | [[2,4],[1,3],[2,4],[1,3]] 2 | [] 3 | [[]] 4 | [[2],[1]] -------------------------------------------------------------------------------- /testcases/230: -------------------------------------------------------------------------------- 1 | [3,1,4,null,2] 2 | 1 3 | [5,3,6,2,4,null,null,1] 4 | 3 -------------------------------------------------------------------------------- /testcases/973: -------------------------------------------------------------------------------- 1 | [[1,3],[-2,2]] 2 | 1 3 | [[3,3],[5,-1],[-2,4]] 4 | 2 -------------------------------------------------------------------------------- /testcases/977: -------------------------------------------------------------------------------- 1 | [-4,-1,0,3,10] 2 | [-7,-3,2,3,11] 3 | [-5,-3,-2,-1] -------------------------------------------------------------------------------- /testcases/111: -------------------------------------------------------------------------------- 1 | [3,9,20,null,null,15,7] 2 | [2,null,3,null,4,null,5,null,6] -------------------------------------------------------------------------------- /testcases/1507: -------------------------------------------------------------------------------- 1 | ""20th Oct 2052" 2 | "6th Jun 1933" 3 | "26th May 1960"" -------------------------------------------------------------------------------- /testcases/1608: -------------------------------------------------------------------------------- 1 | [3,5] 2 | [0,0] 3 | [0,4,3,0,4] 4 | [3,6,7,7,0] 5 | [3,3,3] -------------------------------------------------------------------------------- /testcases/21: -------------------------------------------------------------------------------- 1 | [1,2,4] 2 | [1,3,4] 3 | [1,3,5,7] 4 | [1,2,4,6,8,9,10] 5 | -------------------------------------------------------------------------------- /testcases/234: -------------------------------------------------------------------------------- 1 | [1,2,2,1] 2 | [1,2] 3 | [1,2,3] 4 | [1,2,1] 5 | [1,2,3,2,1] -------------------------------------------------------------------------------- /testcases/320: -------------------------------------------------------------------------------- 1 | "word" 2 | "" 3 | "a" 4 | "aaaaaa" 5 | "aaaaaaaaaaaaaaaaaa" -------------------------------------------------------------------------------- /testcases/76: -------------------------------------------------------------------------------- 1 | "ADOBECODEBANC" 2 | "ABC" 3 | "a" 4 | "a" 5 | "a" 6 | "aa" -------------------------------------------------------------------------------- /testcases/784: -------------------------------------------------------------------------------- 1 | "a1b2" 2 | "3z4" 3 | "1234" 4 | "abcdee" 5 | "aaaaaaaaaaaa" -------------------------------------------------------------------------------- /testcases/962: -------------------------------------------------------------------------------- 1 | [6,0,8,2,1,5] 2 | [9,8,1,0,1,9,4,0,4,1] 3 | [1,2] 4 | [2,1] -------------------------------------------------------------------------------- /testcases/143: -------------------------------------------------------------------------------- 1 | [] 2 | [1] 3 | [1,2] 4 | [1,2,3] 5 | [1,2,3,4] 6 | [1,2,3,4,5] -------------------------------------------------------------------------------- /testcases/151: -------------------------------------------------------------------------------- 1 | "the sky is blue" 2 | " hello world! " 3 | "a good example" -------------------------------------------------------------------------------- /testcases/159: -------------------------------------------------------------------------------- 1 | "eceba" 2 | "ccaabbb" 3 | "a" 4 | "" 5 | "aabaac" 6 | "baabaacaa" -------------------------------------------------------------------------------- /testcases/229: -------------------------------------------------------------------------------- 1 | [3,2,3] 2 | [0,0,0] 3 | [1,1,1,3,3,2,2,2] 4 | [1,1,1,1,2,2,2,2,3] -------------------------------------------------------------------------------- /testcases/319: -------------------------------------------------------------------------------- 1 | 0 2 | 1 3 | 4 4 | 10 5 | 22 6 | 29 7 | 100000 8 | 9999999 9 | -------------------------------------------------------------------------------- /testcases/328: -------------------------------------------------------------------------------- 1 | [] 2 | [1] 3 | [1,2] 4 | [1,2,3] 5 | [1,2,3,4] 6 | [1,2,3,4,5] -------------------------------------------------------------------------------- /testcases/655: -------------------------------------------------------------------------------- 1 | [1,2] 2 | [1,2,3,null,4] 3 | [1,2,5,3,null,null,null,4] 4 | [1] -------------------------------------------------------------------------------- /testcases/7: -------------------------------------------------------------------------------- 1 | 123 2 | -123 3 | 120 4 | 1534236469 5 | -1563847412 6 | 1463847412 -------------------------------------------------------------------------------- /testcases/738: -------------------------------------------------------------------------------- 1 | 10 2 | 34442 3 | 1234 4 | 333 5 | 4321 6 | 1000 7 | 0 8 | 1 9 | 2 -------------------------------------------------------------------------------- /testcases/791: -------------------------------------------------------------------------------- 1 | ""cba" 2 | "abcd" 3 | "" 4 | "asdasd" 5 | "dasgfh" 6 | "aaaaa"" -------------------------------------------------------------------------------- /testcases/100: -------------------------------------------------------------------------------- 1 | [1,2,3] 2 | [1,2,3] 3 | [1,2] 4 | [1,null,2] 5 | [1,2,1] 6 | [1,1,2] -------------------------------------------------------------------------------- /testcases/1509: -------------------------------------------------------------------------------- 1 | [5,3,2,4] 2 | [1,5,0,10,14] 3 | [6,6,0,1,1,4,6] 4 | [1,5,6,14,15] -------------------------------------------------------------------------------- /testcases/1550: -------------------------------------------------------------------------------- 1 | [2,6,4,1] 2 | [1,2,34,3,4,5,7,23,12] 3 | [2,1,2,3] 4 | [2,1,2,3,2] -------------------------------------------------------------------------------- /testcases/1721: -------------------------------------------------------------------------------- 1 | [1,2,3,4,5] 2 | 2 3 | [1] 4 | 1 5 | [1,2,3] 6 | 2 7 | [1,2] 8 | 2 -------------------------------------------------------------------------------- /testcases/394: -------------------------------------------------------------------------------- 1 | ""3[a]2[bc]" 2 | "3[z]2[2[y]pq4[2[jk]e1[f]]]ef" 3 | "3[a]2[b4[F]c]"" -------------------------------------------------------------------------------- /testcases/1026: -------------------------------------------------------------------------------- 1 | [8,3,10,1,6,null,14,null,null,4,7,13] 2 | [1,null,2,null,0,3] 3 | [1,1] -------------------------------------------------------------------------------- /testcases/1344: -------------------------------------------------------------------------------- 1 | 12 2 | 30 3 | 3 4 | 30 5 | 3 6 | 15 7 | 4 8 | 50 9 | 12 10 | 0 -------------------------------------------------------------------------------- /testcases/1451: -------------------------------------------------------------------------------- 1 | "Leetcode is cool" 2 | "Keep calm and code on" 3 | "To be or not to be" -------------------------------------------------------------------------------- /testcases/1578: -------------------------------------------------------------------------------- 1 | "abaac" 2 | [1,2,3,4,5] 3 | "abc" 4 | [1,2,3] 5 | "aabaa" 6 | [1,2,3,4,1] -------------------------------------------------------------------------------- /testcases/1602: -------------------------------------------------------------------------------- 1 | [1,2,3,null,4,5,6] 2 | 4 3 | [3,null,4,2] 4 | 2 5 | [3,null,4,2] 6 | 3 -------------------------------------------------------------------------------- /testcases/1653: -------------------------------------------------------------------------------- 1 | "aababbab" 2 | "bbaaaaabb" 3 | "aaa" 4 | "bbb" 5 | "bbaa" 6 | "bababa" -------------------------------------------------------------------------------- /testcases/449: -------------------------------------------------------------------------------- 1 | [2,1,3] 2 | [10,5,15,1,9,null,20,null,3,6,null,17,21,null,4,null,8] 3 | -------------------------------------------------------------------------------- /testcases/563: -------------------------------------------------------------------------------- 1 | [1,2,3] 2 | [21,7,14,1,1,2,2,3,3] 3 | [4,2,9,3,5,null,7] 4 | [] 5 | [1] -------------------------------------------------------------------------------- /testcases/702: -------------------------------------------------------------------------------- 1 | [-1,0,3,5,9,12] 2 | 9 3 | [-1,0,3,5,9,12] 4 | 2 5 | [1] 6 | 2 7 | [1] 8 | 1 -------------------------------------------------------------------------------- /testcases/918: -------------------------------------------------------------------------------- 1 | [1,-2,3,-2] 2 | [5,-3,5] 3 | [3,-1,2,-1] 4 | [3,-2,2,-3] 5 | [-2,-3,-1] -------------------------------------------------------------------------------- /testcases/268: -------------------------------------------------------------------------------- 1 | [3,0,1] 2 | [0,1] 3 | [1,2] 4 | [0,2] 5 | [9,6,4,2,3,5,7,0,1] 6 | [0] 7 | [2,0] -------------------------------------------------------------------------------- /testcases/462: -------------------------------------------------------------------------------- 1 | [1,2,3] 2 | [0,5,2,5,1,2,3,54,3,4,6,7,5,4,1,2,4,5,6,7,65,3,0] 3 | [1] 4 | [0] -------------------------------------------------------------------------------- /testcases/933: -------------------------------------------------------------------------------- 1 | ["RecentCounter","ping","ping","ping","ping"] 2 | [[],[1],[100],[3001],[3002]] -------------------------------------------------------------------------------- /testcases/1053: -------------------------------------------------------------------------------- 1 | [3,2,1] 2 | [4,3,4,1,1,3] 3 | [4,3,1,1,3] 4 | [1,1,5] 5 | [1,9,4,6,7] 6 | [3,1,1,3] -------------------------------------------------------------------------------- /testcases/1332: -------------------------------------------------------------------------------- 1 | "ababa" 2 | "" 3 | "aaabb" 4 | "ababbbabababab" 5 | "bbaab" 6 | "a" 7 | "b" 8 | "ab" -------------------------------------------------------------------------------- /testcases/1493: -------------------------------------------------------------------------------- 1 | [1,1,0,1] 2 | [0,1,1,1,0,1,1,0,1] 3 | [1,1,1] 4 | [1,1,0,0,1,1,1,0,1] 5 | [0,0,0] -------------------------------------------------------------------------------- /testcases/1557: -------------------------------------------------------------------------------- 1 | 6 2 | [[0,1],[0,2],[2,5],[3,4],[4,2]] 3 | 5 4 | [[0,1],[2,1],[3,1],[1,4],[2,4]] 5 | -------------------------------------------------------------------------------- /testcases/1576: -------------------------------------------------------------------------------- 1 | ""?zs" 2 | "j?qg??b" 3 | "j?qg??b" 4 | "??yw?ipkj?" 5 | "???" 6 | "a?" 7 | "?zs"" -------------------------------------------------------------------------------- /testcases/216: -------------------------------------------------------------------------------- 1 | 3 2 | 7 3 | 3 4 | 9 5 | 4 6 | 20 7 | 5 8 | 30 9 | 6 10 | 30 11 | 9 12 | 45 -------------------------------------------------------------------------------- /testcases/39: -------------------------------------------------------------------------------- 1 | [2,3,6,7] 2 | 7 3 | [2,3,5] 4 | 8 5 | [2] 6 | 1 7 | [1] 8 | 1 9 | [1] 10 | 2 -------------------------------------------------------------------------------- /testcases/733: -------------------------------------------------------------------------------- 1 | [[1,1,1],[1,1,0],[1,0,1]] 2 | 1 3 | 1 4 | 2 5 | [[0,0,0],[0,1,1]] 6 | 1 7 | 1 8 | 1 -------------------------------------------------------------------------------- /testcases/768: -------------------------------------------------------------------------------- 1 | [5,4,3,2,1] 2 | [1,1,0,0,1] 3 | [1,2,2,3,4,5] 4 | [5,5,4,2,0,1] 5 | [1] 6 | [1,1,1] -------------------------------------------------------------------------------- /testcases/825: -------------------------------------------------------------------------------- 1 | [16,16] 2 | [101,98,80,20,1,97,3,77,114,109] 3 | [108,115,5,24,82] 4 | [1] 5 | [] -------------------------------------------------------------------------------- /testcases/91: -------------------------------------------------------------------------------- 1 | "0" 2 | "00" 3 | "10" 4 | "100" 5 | "1211212121212121211212121" 6 | "1020" 7 | "002" -------------------------------------------------------------------------------- /testcases/1354: -------------------------------------------------------------------------------- 1 | [9,3,5] 2 | [1,1,1,2] 3 | [8,5] 4 | [1,1000000000] 5 | [1,1,2] 6 | [2,900000002] 7 | [2] -------------------------------------------------------------------------------- /testcases/1442: -------------------------------------------------------------------------------- 1 | [2,3,1,6,7] 2 | [1,1,1,1,1] 3 | [2,3] 4 | [1,3,5,7,9] 5 | [7,11,12,9,5,2,7,17,22] 6 | [1] -------------------------------------------------------------------------------- /testcases/1512: -------------------------------------------------------------------------------- 1 | [1,1,1,1] 2 | [1,2,3,1,1,3] 3 | [1,2,3] 4 | [2,2,1,1,1,1,1,1,1,2,2,3,4,3,4,3] 5 | [1,1] -------------------------------------------------------------------------------- /testcases/221: -------------------------------------------------------------------------------- 1 | [["1","0","1","0","0"],["1","0","1","1","1"],["1","1","1","1","1"],["1","0","0","1","0"]] -------------------------------------------------------------------------------- /testcases/435: -------------------------------------------------------------------------------- 1 | [[1,2],[2,3],[3,4],[1,3]] 2 | [[1,2],[1,2],[1,2]] 3 | [[1,2],[2,3]] 4 | [] 5 | [[1,2]] -------------------------------------------------------------------------------- /testcases/436: -------------------------------------------------------------------------------- 1 | [[1,2]] 2 | [ [3,4], [2,3], [1,2] ] 3 | [ [1,4], [2,3], [3,4] ] 4 | [[4,5],[2,3],[1,2]] -------------------------------------------------------------------------------- /testcases/714: -------------------------------------------------------------------------------- 1 | [1,3,2,8,4,9] 2 | 2 3 | [1,3,7,5,10,3] 4 | 3 5 | [1,1,1,1] 6 | 2 7 | [8,1,10,11,9] 8 | 2 -------------------------------------------------------------------------------- /testcases/930: -------------------------------------------------------------------------------- 1 | [1,0,1,0,1] 2 | 2 3 | [] 4 | 0 5 | [1] 6 | 1 7 | [0,1,0] 8 | 2 9 | [0,1,0] 10 | 1 -------------------------------------------------------------------------------- /testcases/1219: -------------------------------------------------------------------------------- 1 | [[0,6,0],[5,8,7],[0,9,0]] 2 | [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]] 3 | [[1]] 4 | [[0]] -------------------------------------------------------------------------------- /testcases/142: -------------------------------------------------------------------------------- 1 | [3,2,0,-4,2] 2 | 1 3 | [1] 4 | -1 5 | [1,2] 6 | 0 7 | [2,2,3,4,2] 8 | 1 9 | [] 10 | -1 -------------------------------------------------------------------------------- /testcases/1647: -------------------------------------------------------------------------------- 1 | "aab" 2 | "aaabbbcc" 3 | "ceabaacb" 4 | "aaaaaabbbbbbccccccddddddeeeeeegggggg" 5 | "bbcebab" -------------------------------------------------------------------------------- /testcases/1663: -------------------------------------------------------------------------------- 1 | 5 2 | 130 3 | 3 4 | 27 5 | 5 6 | 73 7 | 50 8 | 100 9 | 100 10 | 100 11 | 10 12 | 200 -------------------------------------------------------------------------------- /testcases/1749: -------------------------------------------------------------------------------- 1 | [1,-3,2,3,-4] 2 | [2,-5,1,-4,3,-2] 3 | [-7,-1,0,-2,1,3,8,-2,-6,-1,-10,-6,-6,8,-4,-9,-4,1,4,-9] -------------------------------------------------------------------------------- /testcases/342: -------------------------------------------------------------------------------- 1 | 1 2 | 2 3 | 4 4 | 5 5 | 8 6 | 12 7 | 16 8 | 32 9 | 64 10 | 128 11 | 120 12 | 100 13 | -------------------------------------------------------------------------------- /testcases/419: -------------------------------------------------------------------------------- 1 | [["X",".",".","X"],[".",".",".","X"],[".",".",".","X"]] 2 | [] 3 | [[]] 4 | [["X"]] 5 | [["."]] -------------------------------------------------------------------------------- /testcases/662: -------------------------------------------------------------------------------- 1 | [1,3,2,5,3,null,9] 2 | [1,3,null,5,3] 3 | [1,3,2,5] 4 | [1,3,2,5,null,null,9,6,null,null,7] -------------------------------------------------------------------------------- /testcases/677: -------------------------------------------------------------------------------- 1 | ["MapSum", "insert", "sum", "insert", "sum"] 2 | [[], ["apple",3], ["ap"], ["app",2], ["ap"]] -------------------------------------------------------------------------------- /testcases/849: -------------------------------------------------------------------------------- 1 | [1,0,0,0,1,0,1] 2 | [0,0,0,0,1,1,1,0] 3 | [0,0,0,0,1,1,1,0,0,0,0,0,0,0] 4 | [1,0,0,0] 5 | [0,1] -------------------------------------------------------------------------------- /testcases/967: -------------------------------------------------------------------------------- 1 | 1 2 | 0 3 | 3 4 | 7 5 | 2 6 | 1 7 | 1 8 | 0 9 | 4 10 | 2 11 | 1 12 | 9 13 | 9 14 | 1 -------------------------------------------------------------------------------- /testcases/1466: -------------------------------------------------------------------------------- 1 | 6 2 | [[0,1],[1,3],[2,3],[4,0],[4,5]] 3 | 5 4 | [[1,0],[1,2],[3,2],[3,4]] 5 | 3 6 | [[1,0],[2,0]] -------------------------------------------------------------------------------- /testcases/179: -------------------------------------------------------------------------------- 1 | [10,2] 2 | [3,30,34,5,9] 3 | [121,12] 4 | [824,938,1399,5607,6973,5703,9609,4398,8247] 5 | [0,0] -------------------------------------------------------------------------------- /testcases/239: -------------------------------------------------------------------------------- 1 | [1,3,-1,-3,5,3,6,7] 2 | 3 3 | [1] 4 | 1 5 | [1,-1] 6 | 1 7 | [9,11] 8 | 2 9 | [4,-2] 10 | 2 -------------------------------------------------------------------------------- /testcases/417: -------------------------------------------------------------------------------- 1 | [[1,2,2,3,5],[3,2,3,4,4],[2,4,5,3,1],[6,7,1,4,5],[5,1,1,2,4]] 2 | [[10,10,10],[10,1,10],[10,10,10]] -------------------------------------------------------------------------------- /testcases/82: -------------------------------------------------------------------------------- 1 | [1,1,1,1,2,3,3,3] 2 | [1,2,2,2,2,3] 3 | [1,1,2,2,3,3] 4 | [1,2,3,3,3] 5 | [1,1,1,1,2] 6 | [1] 7 | [] -------------------------------------------------------------------------------- /testcases/880: -------------------------------------------------------------------------------- 1 | "a23" 2 | 6 3 | "abc3" 4 | 6 5 | "leet2code3" 6 | 10 7 | "ha22" 8 | 5 9 | "a9999" 10 | 1000 -------------------------------------------------------------------------------- /testcases/1556: -------------------------------------------------------------------------------- 1 | 1 2 | 12 3 | 112 4 | 1112 5 | 11112 6 | 111112 7 | 1111112 8 | 11111112 9 | 111111112 10 | -------------------------------------------------------------------------------- /testcases/207: -------------------------------------------------------------------------------- 1 | 2 2 | [[1,0]] 3 | 2 4 | [[1,0],[0,1]] 5 | 8 6 | [[1,0],[2,6],[1,7],[5,1],[6,4],[7,0],[0,5]] 7 | 1 8 | [] -------------------------------------------------------------------------------- /testcases/310: -------------------------------------------------------------------------------- 1 | 4 2 | [[1,0],[1,2],[1,3]] 3 | 1 4 | [] 5 | 2 6 | [[0,1]] 7 | 7 8 | [[0,1],[1,2],[1,3],[2,4],[3,5],[4,6]] -------------------------------------------------------------------------------- /testcases/701: -------------------------------------------------------------------------------- 1 | [4,2,7,1,3] 2 | 5 3 | [40,20,60,10,30,50,70] 4 | 25 5 | [4,2,7,1,3,null,null,null,null,null,null] 6 | 5 -------------------------------------------------------------------------------- /testcases/1441: -------------------------------------------------------------------------------- 1 | [1,3] 2 | 3 3 | [1,2,3] 4 | 3 5 | [1,2] 6 | 4 7 | [2,3,4] 8 | 4 9 | [10] 10 | 100 11 | [1] 12 | 100 -------------------------------------------------------------------------------- /testcases/1513: -------------------------------------------------------------------------------- 1 | ""000" 2 | "101" 3 | "0110111" 4 | "111111111" 5 | "111111111111111111111111111111111111111111111111111"" -------------------------------------------------------------------------------- /testcases/1560: -------------------------------------------------------------------------------- 1 | 4 2 | [1,3,1,2] 3 | 2 4 | [2,1,2,1,2,1,2,1,2] 5 | 7 6 | [1,3,5,7] 7 | 3 8 | [3,2,1,2,1,3,2,1,2,1,3,2,3,1] -------------------------------------------------------------------------------- /testcases/1592: -------------------------------------------------------------------------------- 1 | " this is a sentence " 2 | " practice makes perfect" 3 | "hello world" 4 | "a" 5 | "a " 6 | " a" -------------------------------------------------------------------------------- /testcases/646: -------------------------------------------------------------------------------- 1 | [[1,2], [2,3], [3,4]] 2 | [[-10,-8],[8,9],[-5,0],[6,10],[-6,-4],[1,7],[9,10],[-4,7]] 3 | [[1,7],[2,5],[6,7]] -------------------------------------------------------------------------------- /testcases/71: -------------------------------------------------------------------------------- 1 | "/home/" 2 | "///" 3 | "/home//foo/" 4 | "/a/./b/../../c/" 5 | "/home/" 6 | "/../../../" 7 | "/./.../a/../c" -------------------------------------------------------------------------------- /testcases/901: -------------------------------------------------------------------------------- 1 | ["StockSpanner","next","next","next","next","next","next","next"] 2 | [[],[100],[80],[60],[70],[60],[75],[85]] -------------------------------------------------------------------------------- /testcases/1491: -------------------------------------------------------------------------------- 1 | [4000,3000,1000,2000] 2 | [1000,2000,3000] 3 | [6000,5000,4000,3000,2000,1000] 4 | [8000,9000,2000,3000,6000,1000] -------------------------------------------------------------------------------- /testcases/1657: -------------------------------------------------------------------------------- 1 | "abc" 2 | "bca" 3 | "a" 4 | "aa" 5 | "abcde" 6 | "aecdb" 7 | "cabbba" 8 | "abbccc" 9 | "uau" 10 | "ssx" -------------------------------------------------------------------------------- /testcases/361: -------------------------------------------------------------------------------- 1 | [["0","E","0","0"],["E","0","W","E"],["0","E","0","0"]] 2 | [] 3 | [[]] 4 | [["E"]] 5 | [["W","E","0","W","E"]] -------------------------------------------------------------------------------- /testcases/362: -------------------------------------------------------------------------------- 1 | ["HitCounter","hit","hit","hit","getHits","hit","getHits","getHits"] 2 | [[],[1],[2],[3],[4],[300],[300],[301]] -------------------------------------------------------------------------------- /testcases/378: -------------------------------------------------------------------------------- 1 | [[1,5,9],[10,11,13],[12,13,15]] 2 | 8 3 | [[1,5,90],[10,11,130],[12,13,150]] 4 | 8 5 | [] 6 | 1 7 | [[-5]] 8 | 1 -------------------------------------------------------------------------------- /testcases/769: -------------------------------------------------------------------------------- 1 | [4,3,2,1,0] 2 | [1,0,2,3,4] 3 | [0,1,3,4,2] 4 | [0,1,3,2,4] 5 | [0,1,2,3,4] 6 | [0,1,2,4,3] 7 | [4,3,1,2,0] 8 | [0] -------------------------------------------------------------------------------- /testcases/1446: -------------------------------------------------------------------------------- 1 | "j" 2 | "cc" 3 | "leetcode" 4 | "abbcccddddeeeeedcba" 5 | "triplepillooooow" 6 | "hooraaaaaaaaaaay" 7 | "tourist" 8 | -------------------------------------------------------------------------------- /testcases/1523: -------------------------------------------------------------------------------- 1 | 3 2 | 7 3 | 8 4 | 10 5 | 213 6 | 6548 7 | 100 8 | 1000 9 | 99 10 | 1000 11 | 100 12 | 999 13 | 99 14 | 999 -------------------------------------------------------------------------------- /testcases/1529: -------------------------------------------------------------------------------- 1 | ""0" 2 | "1" 3 | "10111" 4 | "101" 5 | "00000" 6 | "001011101" 7 | "0101010100010010101111101010101000010111011111"" -------------------------------------------------------------------------------- /testcases/165: -------------------------------------------------------------------------------- 1 | "0.1" 2 | "1.1" 3 | "1.0.1" 4 | "1" 5 | "7.5.2.4" 6 | "7.5.3" 7 | "1.0" 8 | "1.0.0" 9 | "1.01" 10 | "1.001" -------------------------------------------------------------------------------- /testcases/431: -------------------------------------------------------------------------------- 1 | [1,null,3,2,4,null,5,6, 7] 2 | [1,null,2,3,4,null,5,6,null,7,null,8,9,null,null,10,11,null,12,13,14,null,null,15] 3 | [] -------------------------------------------------------------------------------- /testcases/525: -------------------------------------------------------------------------------- 1 | [0,1] 2 | [0,1,0] 3 | [0,1,0,0,1,0] 4 | [0,1,0,0,0,1] 5 | [0,0,0,1,1,1,0] 6 | [0,1,0,0,0,0,1] 7 | [0,1,0,0,0,0,1,0] 8 | -------------------------------------------------------------------------------- /testcases/605: -------------------------------------------------------------------------------- 1 | [1,0,0,0,1] 2 | 1 3 | [1,0,0,0,1] 4 | 2 5 | [0,0,0,0,0,0] 6 | 3 7 | [0,0,0,0,0] 8 | 3 9 | [0,0,1,0,0] 10 | 1 -------------------------------------------------------------------------------- /testcases/532: -------------------------------------------------------------------------------- 1 | [3,1,4,1,5] 2 | 2 3 | [1,2,3,4,5] 4 | 1 5 | [1,3,1,5,4] 6 | 0 7 | [1,2,4,4,3,3,0,9,2,3] 8 | 3 9 | [-1,-2,-3] 10 | 1 -------------------------------------------------------------------------------- /testcases/540: -------------------------------------------------------------------------------- 1 | [1] 2 | [1,1,2,3,3,4,4,8,8] 3 | [3,3,7,7,10,11,11] 4 | [1,1,2] 5 | [1,2,2] 6 | [1,2,2,3,3] 7 | [1,1,2,3,3] 8 | [1,1,2,2,3] -------------------------------------------------------------------------------- /testcases/705: -------------------------------------------------------------------------------- 1 | ["MyHashSet","add","add","contains","contains","add","contains","remove","contains"] 2 | [[],[1],[2],[1],[3],[2],[2],[2],[2]] -------------------------------------------------------------------------------- /testcases/881: -------------------------------------------------------------------------------- 1 | [1,2] 2 | 3 3 | [3,2,2,1] 4 | 3 5 | [1,2,3,4,5] 6 | 3 7 | [1,2,3,4,5] 8 | 5 9 | [3,5,3,4] 10 | 5 11 | [6] 12 | 6 -------------------------------------------------------------------------------- /testcases/906: -------------------------------------------------------------------------------- 1 | "4" 2 | "1000" 3 | "1" 4 | "1000" 5 | "1" 6 | "99999999999999" 7 | "1" 8 | "2" 9 | "1" 10 | "5" 11 | "1" 12 | "12" -------------------------------------------------------------------------------- /testcases/1052: -------------------------------------------------------------------------------- 1 | [1,0,1,2,1,1,7,5] 2 | [0,1,0,1,0,1,0,1] 3 | 3 4 | [2,6,6,9] 5 | [0,0,1,1] 6 | 1 7 | [1,2,3,4,5,6,7] 8 | [1,1,1,1,1,1,1] 9 | 3 -------------------------------------------------------------------------------- /testcases/1496: -------------------------------------------------------------------------------- 1 | ""NES" 2 | "NESWW" 3 | "ENEWNNSSWNEWWWWWSEEWSNWWWWWNNESWWNSWNWNWWWWSEEESSNWWSSNESNWNNSEESWWWEWENSWSESSNWSWSWESWNNSWNNWWSNNWW"" -------------------------------------------------------------------------------- /testcases/1551: -------------------------------------------------------------------------------- 1 | 1 2 | 2 3 | 3 4 | 4 5 | 5 6 | 6 7 | 7 8 | 8 9 | 9 10 | 10 11 | 11 12 | 12 13 | 13 14 | 14 15 | 15 16 | 16 17 | 17 18 | -------------------------------------------------------------------------------- /testcases/1609: -------------------------------------------------------------------------------- 1 | [1,10,4,3,null,7,9,12,8,6,null,null,2] 2 | [5,4,2,3,3,7] 3 | [5,9,1,3,5,7] 4 | [1] 5 | [11,8,6,1,3,9,11,30,20,18,16,12,10,4,2,17] -------------------------------------------------------------------------------- /testcases/348: -------------------------------------------------------------------------------- 1 | ["TicTacToe","move","move","move","move","move","move","move"] 2 | [[3],[0,0,1],[0,2,2],[2,2,1],[1,1,2],[2,0,1],[1,0,2],[2,1,1]] -------------------------------------------------------------------------------- /testcases/380: -------------------------------------------------------------------------------- 1 | ["RandomizedSet","insert","remove","insert","getRandom","remove","insert","getRandom"] 2 | [[],[1],[2],[2],[],[1],[2],[]] 3 | -------------------------------------------------------------------------------- /testcases/968: -------------------------------------------------------------------------------- 1 | [0,0,null,0,0] 2 | [0,0,null,0,null,0,null,null,0] 3 | [0,0,null,0,null,0,null,null,0,0] 4 | [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] -------------------------------------------------------------------------------- /testcases/1305: -------------------------------------------------------------------------------- 1 | [2,1,4] 2 | [1,0,3] 3 | [0,-10,10] 4 | [5,1,7,0,2] 5 | [] 6 | [5,1,7,0,2] 7 | [0,-10,10] 8 | [] 9 | [1,null,8] 10 | [8,1] -------------------------------------------------------------------------------- /testcases/1572: -------------------------------------------------------------------------------- 1 | [[1,2,3],[4,5,6],[7,8,9]] 2 | [[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1]] 3 | [[5]] 4 | [[7,3,1,9],[3,4,6,9],[6,9,6,6],[9,5,8,5]] 5 | -------------------------------------------------------------------------------- /testcases/1598: -------------------------------------------------------------------------------- 1 | ["d1/","d2/","../","d21/","./"] 2 | ["d1/","d2/","./","d3/","../","d31/"] 3 | ["d1/","../","../","../"] 4 | ["./","../","./"] 5 | -------------------------------------------------------------------------------- /testcases/428: -------------------------------------------------------------------------------- 1 | [1,null,3,2,4,null,5,6] 2 | [44] 3 | [] 4 | [1,null,2,3,4,5,null,null,6,7,null,8,null,9,10,null,null,11,null,12,null,13,null,null,14] -------------------------------------------------------------------------------- /testcases/820: -------------------------------------------------------------------------------- 1 | ["time", "me", "bell"] 2 | ["time", "me", "bell","be","bel","ell","ime"] 3 | ["aaa","aaaaa","leetcode","leet","code","leetco","eetc"] -------------------------------------------------------------------------------- /testcases/859: -------------------------------------------------------------------------------- 1 | ""ab" 2 | "ba" 3 | "ab" 4 | "ab" 5 | "" 6 | "aa" 7 | "aaaaaaabc" 8 | "aaaaaaacb" 9 | "aa" 10 | "aa" 11 | "abac" 12 | "abad"" -------------------------------------------------------------------------------- /testcases/1306: -------------------------------------------------------------------------------- 1 | [4,2,3,0,3,1,2] 2 | 5 3 | [3,0,2,1,2] 4 | 2 5 | [4,2,3,0,3,1,2] 6 | 0 7 | [1,1,1,1,1,1,1,1] 8 | 0 9 | [0,1,1,1,1,1,1,1,1] 10 | 0 -------------------------------------------------------------------------------- /testcases/1533: -------------------------------------------------------------------------------- 1 | [10,7,7,7,7] 2 | [7,10,7,7,7,7] 3 | [7,7,10,7,7,7] 4 | [7,7,7,10,7,7,7] 5 | [7,7,7,7,10,7,7,7] 6 | [7,7,7,7,7,10,7] 7 | [7,7,7,7,7,7,10] -------------------------------------------------------------------------------- /testcases/1673: -------------------------------------------------------------------------------- 1 | [3,5,2,6] 2 | 2 3 | [2,4,3,3,5,4,9,6] 4 | 4 5 | [5,4,3,2] 6 | 3 7 | [5,4,3,2] 8 | 4 9 | [1,2,3,4] 10 | 3 11 | [1,2,3,4] 12 | 4 -------------------------------------------------------------------------------- /testcases/1839: -------------------------------------------------------------------------------- 1 | "uaeiou" 2 | "aeiaaioaaaaeiiiiouuuooaauuaeiu" 3 | "aeeeiiiioooauuuaeiou" 4 | "a" 5 | "eauoiouieaaoueiuaieoeauoiaueoiaeoiuieuaoiaeouiaueo" -------------------------------------------------------------------------------- /testcases/910: -------------------------------------------------------------------------------- 1 | [1] 2 | 0 3 | [0,10] 4 | 2 5 | [1,3,6] 6 | 3 7 | [1,6,3] 8 | 1 9 | [1,6,3] 10 | 20 11 | [10,7,1] 12 | 3 13 | [1,6,2,4,8,8] 14 | 2 -------------------------------------------------------------------------------- /testcases/1249: -------------------------------------------------------------------------------- 1 | "lee(t(c)o)de)" 2 | "a)b(c)d" 3 | "))((" 4 | "(a(b(c)d)" 5 | "()))(())())))()())))((((()(()(((())(()((()(()()()(" 6 | "(" 7 | ")" 8 | "a" 9 | -------------------------------------------------------------------------------- /testcases/1437: -------------------------------------------------------------------------------- 1 | [1,0,0,0,1,0,0,1] 2 | 2 3 | [1,0,0,1,0,1] 4 | 2 5 | [1,1,1,1,1] 6 | 0 7 | [0,1,0,1] 8 | 1 9 | [1,1,1,1,1] 10 | 1 11 | [0,1,0,1] 12 | 2 -------------------------------------------------------------------------------- /testcases/1544: -------------------------------------------------------------------------------- 1 | ""leEeetcode" 2 | "abBAcC" 3 | "s" 4 | "DDbdeCecAbCBdECCBdabbcBBAeDdabBAcdDEAEaAACECaEbeEeEcEdcAABaAadeEdECeABdCCabEaEECcECeDAbBDDCaEDADEdCa"" -------------------------------------------------------------------------------- /testcases/1705: -------------------------------------------------------------------------------- 1 | [1,2,3,5,2] 2 | [3,2,1,4,2] 3 | [3,0,0,0,0,2] 4 | [3,0,0,0,0,2] 5 | [3,5,0,0,2] 6 | [10,6,0,0,2] 7 | [30] 8 | [10] 9 | [10] 10 | [30] -------------------------------------------------------------------------------- /testcases/218: -------------------------------------------------------------------------------- 1 | [[2,9,10],[3,7,15],[5,12,12],[15,20,10],[19,24,8]] 2 | [] 3 | [[1,2,10]] 4 | [[0,2,3],[2,5,3]] 5 | [[1,3,10], [2,3,11]] 6 | [[1,3,10], [1,3,11]] -------------------------------------------------------------------------------- /testcases/338: -------------------------------------------------------------------------------- 1 | 0 2 | 1 3 | 2 4 | 3 5 | 4 6 | 5 7 | 6 8 | 7 9 | 8 10 | 9 11 | 10 12 | 11 13 | 12 14 | 13 15 | 14 16 | 15 17 | 16 18 | 17 19 | 100000 -------------------------------------------------------------------------------- /testcases/1375: -------------------------------------------------------------------------------- 1 | [2,1,3,5,4] 2 | [3,2,4,1,5] 3 | [4,1,2,3] 4 | [2,1,4,3,6,5] 5 | [1,2,3,4,5,6,7,8,9,10] 6 | [6,5,4,3,2,1,7,8,9,10,13, 11, 12, 19, 14, 18, 15, 16, 17] -------------------------------------------------------------------------------- /testcases/1456: -------------------------------------------------------------------------------- 1 | "abciiidef" 2 | 3 3 | "aeiou" 4 | 2 5 | "leetcode" 6 | 4 7 | "rhythms" 8 | 4 9 | "tryhard" 10 | 4 11 | "a" 12 | 1 13 | "weallloveyou" 14 | 7 -------------------------------------------------------------------------------- /testcases/211: -------------------------------------------------------------------------------- 1 | ["WordDictionary","addWord","addWord","addWord","search","search","search","search"] 2 | [[],["bad"],["dad"],["mad"],["pad"],["bad"],[".ad"],["b.."]] -------------------------------------------------------------------------------- /testcases/290: -------------------------------------------------------------------------------- 1 | ""abc" 2 | "b c a" 3 | "abc" 4 | "dog cat dog" 5 | "abba" 6 | "dog dog dog dog" 7 | "abba" 8 | "dog cat cat dog" 9 | "" 10 | "beef"" 11 | -------------------------------------------------------------------------------- /testcases/1094: -------------------------------------------------------------------------------- 1 | [[2,1,5],[3,3,7]] 2 | 4 3 | [[2,1,5],[3,3,7]] 4 | 5 5 | [[2,1,5],[3,5,7]] 6 | 3 7 | [[3,2,7],[3,7,9],[8,3,9]] 8 | 11 9 | [[1,1,1]] 10 | 1 11 | -------------------------------------------------------------------------------- /testcases/1283: -------------------------------------------------------------------------------- 1 | [1,2,5,9] 2 | 6 3 | [2,3,5,7,11] 4 | 11 5 | [19] 6 | 5 7 | [2,2,2,2,2] 8 | 5 9 | [2,2,2,2,2] 10 | 6 11 | [2,2,2,2,2] 12 | 7 13 | [1,2,3] 14 | 6 -------------------------------------------------------------------------------- /testcases/1539: -------------------------------------------------------------------------------- 1 | [2,3,4,7,11] 2 | 5 3 | [1,2,3,4] 4 | 2 5 | [1,2,3,4] 6 | 1 7 | [1,2,3,5] 8 | 1 9 | [1,2,3,5] 10 | 2 11 | [2,3,5,10] 12 | 2 13 | [2,3,5,10] 14 | 1 -------------------------------------------------------------------------------- /testcases/231: -------------------------------------------------------------------------------- 1 | 0 2 | 1 3 | 2 4 | 3 5 | 4 6 | 5 7 | 6 8 | 7 9 | 8 10 | 9 11 | 10 12 | 11 13 | 12 14 | 13 15 | 14 16 | 15 17 | 16 18 | 17 19 | 18 20 | 19 21 | 20 -------------------------------------------------------------------------------- /testcases/1460: -------------------------------------------------------------------------------- 1 | [1,2,3,4] 2 | [2,4,1,3] 3 | [7] 4 | [7] 5 | [1,12] 6 | [12,1] 7 | [1,2,2,3] 8 | [1,2,3,3] 9 | [1,1,1,1,1] 10 | [1,1,1,1,1] 11 | [3,7,9] 12 | [3,7,11] -------------------------------------------------------------------------------- /testcases/1566: -------------------------------------------------------------------------------- 1 | [1,2,4,4,4,4] 2 | 1 3 | 3 4 | [1,2,1,2,1,1,1,3] 5 | 2 6 | 2 7 | [1,2,1,2,1,3] 8 | 2 9 | 3 10 | [1,2,3,1,2] 11 | 2 12 | 2 13 | [2,2,2,2] 14 | 2 15 | 3 -------------------------------------------------------------------------------- /testcases/1640: -------------------------------------------------------------------------------- 1 | [85] 2 | [[85]] 3 | [15,88] 4 | [[88],[15]] 5 | [49,18,16] 6 | [[16,18,49]] 7 | [91,4,64,78] 8 | [[78],[4,64],[91]] 9 | [1,3,5,7] 10 | [[2,4,6,8]] -------------------------------------------------------------------------------- /testcases/567: -------------------------------------------------------------------------------- 1 | "ab" 2 | "eidbaooo" 3 | "ab" 4 | "eidboaoo" 5 | "ab" 6 | "cccba" 7 | "ab" 8 | "baccc" 9 | "cc" 10 | "c" 11 | "aia" 12 | "aiia" 13 | "adc" 14 | "dcda" -------------------------------------------------------------------------------- /testcases/1004: -------------------------------------------------------------------------------- 1 | [1,1,1,0,0,0,1,1,1,1,0] 2 | 2 3 | [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1] 4 | 3 5 | [1,0,1,0,1,0] 6 | 0 7 | [1,0,1,0,1,1,1,1,1,1,0,0,0,0,1,0,1,0,1,0,0,0,0,0] 8 | 1 -------------------------------------------------------------------------------- /testcases/980: -------------------------------------------------------------------------------- 1 | [[1,0,0,0],[0,0,0,0],[0,0,2,-1]] 2 | [[1,0,0,0],[0,0,0,0],[0,0,0,2]] 3 | [[0,1],[2,0]] 4 | [[1, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 2]] -------------------------------------------------------------------------------- /testcases/1035: -------------------------------------------------------------------------------- 1 | [1,4,2] 2 | [1,2,4] 3 | [2,5,1,2,5] 4 | [10,5,2,1,5,2] 5 | [1,3,7,1,7,5] 6 | [1,9,2,5,1] 7 | [1] 8 | [1] 9 | [2,1] 10 | [1,2,1,3,3,2] 11 | [3,2] 12 | [2,2,2,3] -------------------------------------------------------------------------------- /testcases/1074: -------------------------------------------------------------------------------- 1 | [[0,1,0],[1,1,1],[0,1,0]] 2 | 0 3 | [[0,1,0],[1,1,1],[0,1,0]] 4 | 0 5 | [[1,-1],[-1,1]] 6 | 0 7 | [[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0]] 8 | 0 -------------------------------------------------------------------------------- /testcases/1444: -------------------------------------------------------------------------------- 1 | ["A..","AAA","..."] 2 | 3 3 | [".A..A","A.A..","A.AA.","AAAA.","A.AA."] 4 | 5 5 | ["A..","AA.","..."] 6 | 3 7 | ["A..","A..","..."] 8 | 1 9 | ["..."] 10 | 2 -------------------------------------------------------------------------------- /testcases/1634: -------------------------------------------------------------------------------- 1 | [[1,1]] 2 | [[1,0]] 3 | [[2,2],[4,1],[3,0]] 4 | [[3,2],[-4,1],[-1,0]] 5 | [[1,2]] 6 | [[-1,2]] 7 | [] 8 | [] 9 | [[2,2],[4,1],[3,0]] 10 | [[3,3],[-4,2],[-1,1]] -------------------------------------------------------------------------------- /testcases/785: -------------------------------------------------------------------------------- 1 | [[1,2,3],[0,2],[0,1,3],[0,2]] 2 | [[],[]] 3 | [[1,3],[0,2],[1,3],[0,2]] 4 | [[],[2,4,6],[1,4,8,9],[7,8],[1,2,8,9],[6,9],[1,5,7,8,9],[3,6,9],[2,3,4,6,9],[2,4,5,6,7,8]] -------------------------------------------------------------------------------- /testcases/127: -------------------------------------------------------------------------------- 1 | "hit" 2 | "cog" 3 | ["hot","dot","dog","lot","log","cog"] 4 | "hit" 5 | "hot" 6 | ["hot","dot","dog","lot","log","cog"] 7 | "hit" 8 | "cog" 9 | ["hot","dot","dog","lot","log"] -------------------------------------------------------------------------------- /testcases/858: -------------------------------------------------------------------------------- 1 | 2 2 | 1 3 | 3 4 | 1 5 | 4 6 | 1 7 | 4 8 | 3 9 | 7 10 | 3 11 | 3 12 | 2 13 | 6 14 | 3 15 | 9 16 | 1 17 | 13 18 | 11 19 | 1 20 | 0 21 | 4 22 | 4 23 | 4 24 | 2 25 | 6 26 | 2 -------------------------------------------------------------------------------- /testcases/1450: -------------------------------------------------------------------------------- 1 | [1,2,3] 2 | [3,2,7] 3 | 4 4 | [4] 5 | [4] 6 | 4 7 | [4] 8 | [4] 9 | 5 10 | [1,1,1,1] 11 | [1,2,3,4] 12 | 7 13 | [9,8,7,6,5,4,3,2,1] 14 | [10,10,10,10,10,10,10,10,10] 15 | 5 -------------------------------------------------------------------------------- /testcases/623: -------------------------------------------------------------------------------- 1 | [4,2,6,3,1,5] 2 | 1 3 | 2 4 | [10] 5 | 1 6 | 1 7 | [1,2,3,4,null,5,6,null,7,8,null,9,10,11,12] 8 | 20 9 | 2 10 | [4,2,null,3,1] 11 | 1 12 | 3 13 | [4,2,6,3,1,5] 14 | 1 15 | 1 -------------------------------------------------------------------------------- /testcases/946: -------------------------------------------------------------------------------- 1 | [1,2,3,4,5] 2 | [4,5,3,2,1] 3 | [1,2,3,4,5] 4 | [5,2,1,3,4] 5 | [1,2,3,4,5] 6 | [1,2,3,4,5] 7 | [1,2,3,4,5] 8 | [5,4,3,2,1] 9 | [1,2,3,4,5] 10 | [3,2,1,5,4] 11 | [] 12 | [] -------------------------------------------------------------------------------- /testcases/958: -------------------------------------------------------------------------------- 1 | [1,2,3,4,5,6] 2 | [1,2,3,4,5,null,7] 3 | [1] 4 | [1,2,3] 5 | [1,2,3,4,5] 6 | [1,2,3,4,null,5] 7 | [1,2,null,3,4] 8 | [1,2,3,null,4,5,6] 9 | [1,2,3,4,5,6,7,8,9,10,11,12,13,null,null,15] -------------------------------------------------------------------------------- /testcases/991: -------------------------------------------------------------------------------- 1 | 2 2 | 3 3 | 1 4 | 33 5 | 5 6 | 8 7 | 3 8 | 10 9 | 1024 10 | 1 11 | 1 12 | 1000000 13 | 1 14 | 1 15 | 1 16 | 2 17 | 1024 18 | 1025 19 | 100000000 20 | 1000000000 21 | 1 22 | 64 -------------------------------------------------------------------------------- /testcases/1007: -------------------------------------------------------------------------------- 1 | [2,1,2,4,2,2] 2 | [5,2,6,2,3,2] 3 | [3,5,1,2,3] 4 | [3,6,3,3,4] 5 | [1, 2, 1, 1, 2, 1, 1, 1, 1, 2, 1, 2, 1, 2, 1, 1, 2, 2, 2, 1] 6 | [2, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 2, 1, 2, 2, 1, 1, 1, 1] -------------------------------------------------------------------------------- /testcases/1525: -------------------------------------------------------------------------------- 1 | ""aacaba" 2 | "abcd" 3 | "aaaaa" 4 | "acbadbaada" 5 | "aaaaaaaaaaaaaaaaa" 6 | "a" 7 | "beddcadbaceacedbdbdbdedcadacdccbccbdcecdaaebbcbaebdcadcccaeabbdbcedaecacbcaaecedbbaddadbcabadcabdeee"" 8 | -------------------------------------------------------------------------------- /testcases/1644: -------------------------------------------------------------------------------- 1 | [3,5,1,6,2,0,8,null,null,7,4] 2 | 5 3 | 1 4 | [3,5,1,6,2,0,8,null,null,7,4] 5 | 5 6 | 4 7 | [3,5,1,6,2,0,8,null,null,7,4] 8 | 5 9 | 10 10 | [1,2] 11 | 1 12 | 2 13 | [1] 14 | 1 15 | 2 -------------------------------------------------------------------------------- /testcases/32: -------------------------------------------------------------------------------- 1 | "(()" 2 | ")()())" 3 | "" 4 | "()(()" 5 | ")))((" 6 | "((()))()()()" 7 | "((((" 8 | "))))" 9 | "(()" 10 | "())" 11 | ")()())()()(" 12 | "(())))()()()()()" 13 | "((()))((()()()()()()()" -------------------------------------------------------------------------------- /testcases/402: -------------------------------------------------------------------------------- 1 | "1234567890" 2 | 9 3 | "0001" 4 | 1 5 | "1234" 6 | 2 7 | "0123" 8 | 2 9 | "10200" 10 | 1 11 | "9999" 12 | 2 13 | "1432219" 14 | 3 15 | "4321" 16 | 2 17 | "3210" 18 | 2 19 | "10" 20 | 2 -------------------------------------------------------------------------------- /testcases/1455: -------------------------------------------------------------------------------- 1 | "i love eating burger" 2 | "burg" 3 | "this problem is an easy problem" 4 | "pro" 5 | "i am tired" 6 | "you" 7 | "i use triple pillow" 8 | "pill" 9 | "hello from the other side" 10 | "they" -------------------------------------------------------------------------------- /testcases/53: -------------------------------------------------------------------------------- 1 | [-2,1,-3,4,-1,2,1,-5,4] 2 | [-8, 17, -10, 15, -6, 0, -8, 2, -9, -10, 7, -19, -13, 12, -16, -10, -10, -14, -7, -7, -9, 2, -8, -6, -16, -17, -4, -16, -17, -6, -19, -19, -6, -17, -11, -10, 14, -20, -3, 9] -------------------------------------------------------------------------------- /testcases/708: -------------------------------------------------------------------------------- 1 | [3,4,1] 2 | 2 3 | [] 4 | 1 5 | [3,4,1] 6 | 2 7 | [1] 8 | 0 9 | [4,4,4] 10 | 3 11 | [4,4,4] 12 | 5 13 | [2,2,4,4,4] 14 | 5 15 | [2,2,4,4,4] 16 | 3 17 | [2,2,4,4,4] 18 | 1 19 | [5,1,3] 20 | 2 -------------------------------------------------------------------------------- /testcases/794: -------------------------------------------------------------------------------- 1 | ["O "," "," "] 2 | ["XOX", " X ", " "] 3 | ["XXX", " ", "OOO"] 4 | ["XOX", "O O", "XOX"] 5 | ["XOX", "OXO", "XOX"] 6 | [" ", " ", " "] 7 | ["X O", "X O", "X O"] 8 | ["XXX","XOO","OO "] -------------------------------------------------------------------------------- /testcases/902: -------------------------------------------------------------------------------- 1 | ["1","3","5","7"] 2 | 100 3 | ["7"] 4 | 8 5 | ["1","2","3","4","5","6","7","8","9"] 6 | 1000000000 7 | ["1","2","3"] 8 | 123456 9 | ["1"] 10 | 1 11 | ["1"] 12 | 83450 13 | ["1","2"] 14 | 12 -------------------------------------------------------------------------------- /testcases/987: -------------------------------------------------------------------------------- 1 | [3,9,20,null,null,15,7] 2 | [1,2,3,4,5,6,7] 3 | [12, 24, 0, 20, 24, 24, 10, 14, 22, 8, 13, 28, 16, 4, 2, 24, 4, 14, 26, 19, 17, 0, 26, 15, 6, 15, 10, 12, 13, 28, 29, 14, 20, 21, 9, 8, 8, 26, 11, 4] -------------------------------------------------------------------------------- /testcases/1015: -------------------------------------------------------------------------------- 1 | 1 2 | 2 3 | 3 4 | 4 5 | 5 6 | 6 7 | 7 8 | 8 9 | 9 10 | 10 11 | 11 12 | 12 13 | 13 14 | 14 15 | 15 16 | 16 17 | 17 18 | 18 19 | 19 20 | 20 21 | 21 22 | 22 23 | 23 24 | 24 25 | 25 26 | 26 27 | -------------------------------------------------------------------------------- /testcases/121: -------------------------------------------------------------------------------- 1 | [7,1,5,3,6,4] 2 | [7,6,4,3,1] 3 | [86, 25, 58, 28, 76, 26, 86, 0, 22, 13, 52, 26, 74, 22, 12, 76, 51, 41, 2, 47, 60, 6, 98, 16, 42, 65, 22, 31, 25, 17, 95, 42, 9, 25, 58, 35, 93, 17, 19, 45] 4 | [] 5 | [1] -------------------------------------------------------------------------------- /testcases/154: -------------------------------------------------------------------------------- 1 | [1,3,5] 2 | [2,2,2,0,1] 3 | [2,2,2,0,0,0,1] 4 | [10,1,10,10,10] 5 | [10,10,10,1,10] 6 | [10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,1,10] -------------------------------------------------------------------------------- /testcases/395: -------------------------------------------------------------------------------- 1 | "aaabb" 2 | 3 3 | "ababbc" 4 | 2 5 | "abaaaccaabb" 6 | 3 7 | "a" 8 | 1 9 | "aabbaaabbccddeeaa" 10 | 3 11 | "baaaccaaabdd" 12 | 2 13 | "baaacccaaabdd" 14 | 3 15 | "aa" 16 | 3 17 | "baaabcb" 18 | 3 -------------------------------------------------------------------------------- /testcases/1162: -------------------------------------------------------------------------------- 1 | [[1,0,1],[0,0,0],[1,0,1]] 2 | [[1,0,0],[0,0,0],[0,0,0]] 3 | [[1]] 4 | [[0]] 5 | [[1,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,1]] 6 | [[1,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,1,0,0,0],[0,0,0,0,1]] -------------------------------------------------------------------------------- /testcases/122: -------------------------------------------------------------------------------- 1 | [7,1,5,3,6,4] 2 | [1,2,3,4,5] 3 | [7,6,4,3,1] 4 | [86, 25, 58, 28, 76, 26, 86, 0, 22, 13, 52, 26, 74, 22, 12, 76, 51, 41, 2, 47, 60, 6, 98, 16, 42, 65, 22, 31, 25, 17, 95, 42, 9, 25, 58, 35, 93, 17, 19, 45] -------------------------------------------------------------------------------- /testcases/213: -------------------------------------------------------------------------------- 1 | [2,3,2] 2 | [1,2,3,1] 3 | [0] 4 | [5, 26, 96, 42, 79, 41, 40, 25, 86, 75, 49, 40, 20, 8, 53, 11, 73, 26, 18, 30, 11, 22, 57, 42, 7, 60, 84, 83, 91, 29, 0, 59, 58, 34, 43, 84, 31, 86, 62, 89] 5 | [4] 6 | [1,3] -------------------------------------------------------------------------------- /testcases/658: -------------------------------------------------------------------------------- 1 | [1,2,3,4,5] 2 | 4 3 | 3 4 | [1, 3, 5, 7, 8, 9] 5 | 2 6 | 6 7 | [1, 3, 5, 7, 8, 9,11,13] 8 | 3 9 | 6 10 | [1, 3, 5, 7, 8, 9,11,13] 11 | 3 12 | 10 13 | [2,3,4] 14 | 2 15 | 1 16 | [2,3,4] 17 | 2 18 | 5 -------------------------------------------------------------------------------- /testcases/1642: -------------------------------------------------------------------------------- 1 | [4,2,7,6,9,14,12] 2 | 5 3 | 1 4 | [4,12,2,7,3,18,20,3,19] 5 | 10 6 | 2 7 | [14,3,19,3] 8 | 17 9 | 0 10 | [1,2,2,2] 11 | 0 12 | 0 13 | [1,1,1,1] 14 | 0 15 | 0 16 | [1,10,11,12,13,14,15,16] 17 | 10 18 | 1 -------------------------------------------------------------------------------- /testcases/799: -------------------------------------------------------------------------------- 1 | 1 2 | 1 3 | 1 4 | 2 5 | 1 6 | 1 7 | 6 8 | 2 9 | 0 10 | 2 11 | 1 12 | 1 13 | 8 14 | 2 15 | 0 16 | 100000009 17 | 33 18 | 17 19 | 1000000000 20 | 29 21 | 0 22 | 2 23 | 1 24 | 0 25 | 1000000000 26 | 99 27 | 99 -------------------------------------------------------------------------------- /testcases/1588: -------------------------------------------------------------------------------- 1 | [1,4,2,5,3] 2 | [1,2] 3 | [10,11,12] 4 | [15, 4, 14, 2, 2, 13, 12, 9, 2, 16, 8, 5, 15, 11, 3, 11, 7, 5, 11, 9, 13, 8, 15, 14, 2, 5, 1, 16, 12, 3, 16, 12, 15, 6, 18, 2, 3, 13, 4, 18, 5, 13, 18, 2, 10, 1, 8, 11, 2, 10] -------------------------------------------------------------------------------- /testcases/971: -------------------------------------------------------------------------------- 1 | [1,2] 2 | [2,1] 3 | [1,2,3] 4 | [1,3,2] 5 | [1] 6 | [1] 7 | [1,2,3,null,null,4,5] 8 | [1,3,2,4,5] 9 | [1,2,3,null,null,4,5] 10 | [1,3,2,5,4] 11 | [1,2,3] 12 | [1,2,3] 13 | [1,2,3,null,null,4,5] 14 | [1,3,5,4,2] -------------------------------------------------------------------------------- /testcases/1492: -------------------------------------------------------------------------------- 1 | 12 2 | 3 3 | 7 4 | 2 5 | 4 6 | 4 7 | 1 8 | 1 9 | 1000 10 | 3 11 | 1000 12 | 500 13 | 12 14 | 3 15 | 12 16 | 4 17 | 7 18 | 2 19 | 1 20 | 1 21 | 1000 22 | 3 23 | 16 24 | 3 25 | 16 26 | 4 27 | 16 28 | 5 29 | 16 30 | 7 -------------------------------------------------------------------------------- /testcases/367: -------------------------------------------------------------------------------- 1 | 0 2 | 1 3 | 2 4 | 3 5 | 4 6 | 5 7 | 6 8 | 7 9 | 8 10 | 9 11 | 10 12 | 11 13 | 12 14 | 13 15 | 14 16 | 15 17 | 16 18 | 17 19 | 18 20 | 19 21 | 20 22 | 21 23 | 22 24 | 23 25 | 24 26 | 25 27 | 26 28 | 27 29 | 28 30 | 29 -------------------------------------------------------------------------------- /testcases/593: -------------------------------------------------------------------------------- 1 | [0,0] 2 | [1,1] 3 | [1,0] 4 | [0,1] 5 | [1,0] 6 | [-1,0] 7 | [0,1] 8 | [0,-1] 9 | [2,1] 10 | [-1,2] 11 | [-2,-1] 12 | [1,-2] 13 | [0,0] 14 | [5,0] 15 | [5,4] 16 | [0,4] 17 | [0,0] 18 | [1,1] 19 | [0,0] 20 | [0,0] -------------------------------------------------------------------------------- /testcases/919: -------------------------------------------------------------------------------- 1 | ["CBTInserter","insert","get_root"] 2 | [[[1]],[2],[]] 3 | ["CBTInserter","insert","insert","get_root"] 4 | [[[1,2,3,4,5,6]],[7],[8],[]] 5 | ["CBTInserter","insert","insert","insert","get_root"] 6 | [[[1]],[2],[3],[4],[]] -------------------------------------------------------------------------------- /testcases/115: -------------------------------------------------------------------------------- 1 | "rabbbit" 2 | "rabbit" 3 | "babgbag" 4 | "bag" 5 | "aaaaaa" 6 | "ab" 7 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 8 | "aaaaa" -------------------------------------------------------------------------------- /testcases/295: -------------------------------------------------------------------------------- 1 | ["MedianFinder","addNum","addNum","findMedian","addNum","findMedian"] 2 | [[],[1],[2],[],[3],[]] 3 | ["MedianFinder","addNum","findMedian"] 4 | [[],[1],[]] 5 | ["MedianFinder","addNum","addNum","findMedian"] 6 | [[],[0],[0],[]] -------------------------------------------------------------------------------- /testcases/673: -------------------------------------------------------------------------------- 1 | [] 2 | [1] 3 | [1,3,5,4,7] 4 | [6, 19, 41, 85, 91, 19, 29, 15, 24, 30, 13, 87, 30, 52, 65, 62, 64, 20, 77, 17, 34, 83, 58, 17, 64, 91, 39, 94, 10, 6, 21, 17, 40, 41, 23, 50, 55, 57, 76, 43, 60, 18, 51, 74, 19, 86, 27, 64, 22, 99] -------------------------------------------------------------------------------- /testcases/659: -------------------------------------------------------------------------------- 1 | [1,2,3,3,4,5] 2 | [1,1,2,2,3] 3 | [1,2,4] 4 | [1,1,2,2,4] 5 | [1,2,3,4,5,5,6] 6 | [1,2,3,3,4,4,5,5] 7 | [1,2,3,4,4,5] 8 | [1,2,3,5,6,7] 9 | [1,2,2,3,3,4,4,5] 10 | [1] 11 | [1,2] 12 | [1,2,3] 13 | [1,2,2,3,3] 14 | [1,2,2,3,3,4] -------------------------------------------------------------------------------- /src/1426.counting-elements.py: -------------------------------------------------------------------------------- 1 | # TAGS: Array, Premium 2 | class Solution: 3 | # 64 ms, 15.55%. O(N), O(N) 4 | def countElements(self, arr: List[int]) -> int: 5 | seen = set(arr) 6 | return sum(1 for val in arr if val + 1 in seen) -------------------------------------------------------------------------------- /testcases/1232: -------------------------------------------------------------------------------- 1 | [[1,2],[2,3],[3,4],[4,5],[5,6],[6,7]] 2 | [[1,1],[2,2],[3,4],[4,5],[5,6],[7,7]] 3 | [[-1,1],[-6,-4],[-6,2],[2,0],[-1,-2],[0,-4]] 4 | [[-3,-2],[-1,-2],[2,-2],[-2,-2],[0,-2]] 5 | [[-7,-3],[-7,-1],[-2,-2],[0,-8],[2,-2],[5,-6],[5,-5],[1,7]] -------------------------------------------------------------------------------- /testcases/1452: -------------------------------------------------------------------------------- 1 | [["leetcode","google","facebook"],["google","microsoft"],["google","facebook"],["google"],["amazon"]] 2 | [["leetcode","google","facebook"],["leetcode","amazon"],["facebook","google"]] 3 | [["leetcode"],["google"],["facebook"],["amazon"]] -------------------------------------------------------------------------------- /testcases/1618: -------------------------------------------------------------------------------- 1 | "helloworld" 2 | 80 3 | 20 4 | [6,8,10,12,14,16,18,24,36] 5 | "leetcode" 6 | 1000 7 | 50 8 | [1,2,4] 9 | "easyquestion" 10 | 100 11 | 100 12 | [10,15,20,25] 13 | "abc" 14 | 10 15 | 10 16 | [1] 17 | "abc" 18 | 10 19 | 10 20 | [100] -------------------------------------------------------------------------------- /testcases/1518: -------------------------------------------------------------------------------- 1 | 9 2 | 3 3 | 15 4 | 4 5 | 5 6 | 5 7 | 2 8 | 3 9 | 100 10 | 2 11 | 100 12 | 3 13 | 100 14 | 5 15 | 100 16 | 6 17 | 100 18 | 7 19 | 100 20 | 9 21 | 100 22 | 10 23 | 100 24 | 11 25 | 100 26 | 12 27 | 100 28 | 13 29 | 100 30 | 14 31 | 15 32 | 4 -------------------------------------------------------------------------------- /src/178.rank-scores.sql: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | SELECT score, 3 | @rank := (@rank + (@prev <> (@prev := score))) `Rank` 4 | FROM Scores, 5 | ( 6 | SELECT @rank := 0, 7 | @prev := -1 8 | ) init 9 | ORDER BY score DESC -------------------------------------------------------------------------------- /testcases/886: -------------------------------------------------------------------------------- 1 | 4 2 | [[1,2],[1,3],[2,4]] 3 | 3 4 | [[1,2],[1,3],[2,3]] 5 | 5 6 | [[1,2],[2,3],[3,4],[4,5],[1,5]] 7 | 10 8 | [[4,7],[4,8],[5,6],[1,6],[3,7],[2,5],[5,8],[1,2],[4,9],[6,10],[8,10],[3,6],[2,10],[9,10],[3,9],[2,3],[1,9],[4,6],[5,7],[3,8],[1,8],[1,7],[2,4]] -------------------------------------------------------------------------------- /testcases/437: -------------------------------------------------------------------------------- 1 | [10,5,-3,3,2,null,11,3,-2,null,1] 2 | 8 3 | [1,null,2,null,3,null,4,null,5] 4 | 3 5 | [9, -7, 6, 3, 4, 1, 0, 7, 8, 3, 3, 9, 9, 8, 7, 6, -5, 1, -1, -7, -10, -6, 7, -1, 1, 8, -8, -4, -1, 3, -4, 6, 1, 9, 1, -3, -1, -8, -10, 9] 6 | 0 7 | [0,0,0,0,0,0,0,0] 8 | 0 -------------------------------------------------------------------------------- /testcases/833: -------------------------------------------------------------------------------- 1 | "abcd" 2 | [0, 2] 3 | ["a", "cd"] 4 | ["eee", "ffff"] 5 | "abcd" 6 | [2, 0] 7 | ["cd", "a"] 8 | ["ffff", "eee"] 9 | "abcd" 10 | [0, 2] 11 | ["ab","ec"] 12 | ["eee","ffff"] 13 | "abcdefgh" 14 | [0, 2, 5] 15 | ["be","c","fg"] 16 | ["eee","ffff","u"] -------------------------------------------------------------------------------- /testcases/573: -------------------------------------------------------------------------------- 1 | 5 2 | 7 3 | [2,2] 4 | [4,4] 5 | [[3,0], [2,5]] 6 | 1 7 | 3 8 | [0,1] 9 | [0,0] 10 | [[0,2]] 11 | 5 12 | 5 13 | [3,2] 14 | [0,1] 15 | [[2,0],[4,1],[0,4],[1,3],[1,0],[3,4],[3,0],[2,3],[0,2],[0,0],[2,2],[4,2],[3,3],[4,4],[4,0],[4,3],[3,1],[2,1],[1,4],[2,4]] -------------------------------------------------------------------------------- /testcases/823: -------------------------------------------------------------------------------- 1 | [2] 2 | [2,4] 3 | [2,4,5,10] 4 | [2,3,4,5,6,7,8,9,10] 5 | [2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, 131072, 262144, 524288, 1048576, 2097152, 4194304, 8388608, 16777216, 33554432, 67108864, 134217728, 268435456, 536870912] -------------------------------------------------------------------------------- /testcases/284: -------------------------------------------------------------------------------- 1 | ["PeekingIterator","next","peek","next","next","hasNext"] 2 | [[[1,2,3]],[],[],[],[],[]] 3 | ["PeekingIterator","hasNext","peek","peek","next","next","peek","peek","next","hasNext","peek","hasNext","next","hasNext"] 4 | [[[1,2,3,4]],[],[],[],[],[],[],[],[],[],[],[],[],[]] -------------------------------------------------------------------------------- /testcases/61: -------------------------------------------------------------------------------- 1 | [1,2,3,4,5] 2 | 2 3 | [0,1,2,3,4] 4 | 200 5 | [0,1,2,3,4] 6 | 199 7 | [0,1,2,3,4] 8 | 198 9 | [1] 10 | 10 11 | [] 12 | 2 13 | [0,1,2,3,4] 14 | 1 15 | [0,1,2,3,4] 16 | 2 17 | [0,1,2,3,4] 18 | 3 19 | [0,1,2,3,4] 20 | 4 21 | [0,1,2,3,4] 22 | 5 23 | [0,1,2,3,4] 24 | 6 -------------------------------------------------------------------------------- /testcases/1192: -------------------------------------------------------------------------------- 1 | 4 2 | [[0,1],[1,2],[2,0],[1,3]] 3 | 5 4 | [[1,0],[2,0],[3,2],[4,2],[4,3],[3,0],[4,0]] 5 | 3 6 | [[0,1],[1,2],[0,2]] 7 | 10 8 | [[1,0],[2,0],[3,0],[4,1],[5,3],[6,1],[7,2],[8,1],[9,6],[9,3],[3,2],[4,2],[7,4],[6,2],[8,3],[4,0],[8,6],[6,5],[6,3],[7,5],[8,0],[8,5],[5,4],[2,1],[9,5],[9,7],[9,4],[4,3]] -------------------------------------------------------------------------------- /testcases/1482: -------------------------------------------------------------------------------- 1 | [1,10,3,10,2] 2 | 3 3 | 1 4 | [4,1,1,1,4,1] 5 | 2 6 | 3 7 | [1,10,3,10,2] 8 | 3 9 | 2 10 | [7,7,7,7,12,7,7] 11 | 2 12 | 3 13 | [1000000000,1000000000] 14 | 1 15 | 1 16 | [1,10,2,9,3,8,4,7,5,6] 17 | 4 18 | 2 19 | [1] 20 | 1 21 | 1 22 | [1, 2] 23 | 2 24 | 1 25 | [1,10,3,10,2] 26 | 3 27 | 1 -------------------------------------------------------------------------------- /testcases/1615: -------------------------------------------------------------------------------- 1 | 4 2 | [[0,1],[0,3],[1,2],[1,3]] 3 | 5 4 | [[0,1],[0,3],[1,2],[1,3],[2,3],[2,4]] 5 | 8 6 | [[0,1],[1,2],[2,3],[2,4],[5,6],[5,7]] 7 | 10 8 | [[6, 9], [2, 8], [0, 2], [0, 7], [3, 7], [1, 2], [6, 7], [4, 9], [2, 6], [1, 4], [3, 9], [0, 5], [1, 9], [3, 5], [2, 7], [4, 6], [5, 7], [0, 6], [1, 8], [2, 4]] -------------------------------------------------------------------------------- /src/1929.concatenation-of-array.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=1929 lang=python3 3 | # 4 | # [1929] Concatenation of Array 5 | # 6 | 7 | # @lc code=start 8 | 9 | 10 | class Solution: 11 | def getConcatenation(self, nums: List[int]) -> List[int]: 12 | return nums * 2 13 | # @lc code=end 14 | -------------------------------------------------------------------------------- /src/344.reverse-string.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=344 lang=python3 3 | # 4 | # [344] Reverse String 5 | # 6 | 7 | # @lc code=start 8 | class Solution: 9 | # Time: O(N), Space: O(1) 10 | for i in range(len(s)//2): 11 | s[i], s[~i] = s[~i], s[i] 12 | 13 | # @lc code=end 14 | 15 | -------------------------------------------------------------------------------- /testcases/1664: -------------------------------------------------------------------------------- 1 | [2,1,6,4] 2 | [1,2,2,1,1,2,2,1,1,2,2,1,1,2,2] 3 | [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1] 4 | [1] 5 | [2] 6 | [1,1] 7 | [1,2] -------------------------------------------------------------------------------- /testcases/72: -------------------------------------------------------------------------------- 1 | "horse" 2 | "ros" 3 | "intention" 4 | "execution" 5 | "" 6 | "" 7 | "uxznqtpmauznsgmrczcjfswinddjrevaywvmoyodjfsievquoiupwoxbysxkeemllomfrcanqiitqutjwwimcwezbqizvexbahgc" 8 | "hyeuzdmidbpyyloiyzgmzswqwslyevmedpjnonishekyssnafspbqswtifykmfkvkfltgjhhbtlkejamwxweydgfcegcqrfrjazp" 9 | "b" 10 | "" -------------------------------------------------------------------------------- /testcases/986: -------------------------------------------------------------------------------- 1 | [[0,2],[5,10],[13,23],[24,25]] 2 | [[1,5],[8,12],[15,24],[25,26]] 3 | [[0,2],[5,10],[13,23],[24,25]] 4 | [[0,25]] 5 | [[0,2],[5,10],[13,23],[24,25]] 6 | [[25,25]] 7 | [[0,2],[5,10],[13,23],[24,25]] 8 | [] 9 | [] 10 | [] 11 | [[0,2],[5,10],[13,23],[24,25]] 12 | [[2,5],[10,13],[23,24],[25,26]] -------------------------------------------------------------------------------- /testcases/273: -------------------------------------------------------------------------------- 1 | 0 2 | 1 3 | 2 4 | 3 5 | 4 6 | 5 7 | 6 8 | 7 9 | 8 10 | 9 11 | 10 12 | 11 13 | 12 14 | 13 15 | 14 16 | 15 17 | 16 18 | 17 19 | 18 20 | 19 21 | 20 22 | 21 23 | 22 24 | 100 25 | 1000 26 | 10000 27 | 300000 28 | 4000000 29 | 40000000 30 | 560000000 31 | 1000000 32 | 1234567 33 | 1234567891 34 | 12345 -------------------------------------------------------------------------------- /testcases/299: -------------------------------------------------------------------------------- 1 | ""1807" 2 | "7810" 3 | "1123" 4 | "0111" 5 | "07349101443352110140" 6 | "75946065378355313014" 7 | "5588763736892460426237214140858061229541486871691407668332002818841549836889100793410876434111851040" 8 | "2893037046691966257855827107244577620959574483791681509433887483324781100091219058161205918850948467"" -------------------------------------------------------------------------------- /testcases/993: -------------------------------------------------------------------------------- 1 | [1,2,3,null,4,null,5] 2 | 5 3 | 4 4 | [1,2,3,4] 5 | 4 6 | 3 7 | [1,2,3,null,4] 8 | 2 9 | 3 10 | [1,2,3,4,5,6,7] 11 | 2 12 | 4 13 | [1,2,3,4,5,6,7] 14 | 4 15 | 5 16 | [1,2,3,4,5,6,7] 17 | 4 18 | 6 19 | [1,2,3,4,5,6,7] 20 | 5 21 | 6 22 | [1,2,3,4,5,6,7] 23 | 5 24 | 7 25 | [1,2,3,4,5,6,7] 26 | 4 27 | 7 -------------------------------------------------------------------------------- /testcases/1582: -------------------------------------------------------------------------------- 1 | [[0,0,0,1],[1,0,0,0],[0,1,1,0],[0,0,0,0]] 2 | [[1,0,0],[0,0,1],[1,0,0]] 3 | [[1,0,0],[0,1,0],[0,0,1]] 4 | [[0,0,0,1],[1,0,0,0],[0,1,1,0],[0,0,0,0]] 5 | [[0,0,0,0,0],[1,0,0,0,0],[0,1,0,0,0],[0,0,1,0,0],[0,0,0,1,1]] 6 | [[0,0,0,0,0,1,0,0],[0,0,0,0,1,0,0,1],[0,0,0,0,1,0,0,0],[1,0,0,0,1,0,0,0],[0,0,1,1,0,0,0,0]] 7 | -------------------------------------------------------------------------------- /src/1920.build-array-from-permutation.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=1920 lang=python3 3 | # 4 | # [1920] Build Array from Permutation 5 | # 6 | 7 | # @lc code=start 8 | 9 | 10 | class Solution: 11 | def buildArray(self, nums: List[int]) -> List[int]: 12 | return [nums[i] for i in nums] 13 | # @lc code=end 14 | -------------------------------------------------------------------------------- /testcases/317: -------------------------------------------------------------------------------- 1 | [[1,0,2,0,1],[0,0,0,0,0],[0,0,1,0,0]] 2 | [[1,2,0,2,1],[1,1,2,1,1],[1,1,1,1,1],[1,1,1,1,1],[0,0,0,0,0]] 3 | [[1,1,1,1],[1,1,1,1]] 4 | [[2,2,2,2],[1,1,1,1]] 5 | [[2,2,2,2],[0,1,1,0]] 6 | [[0,0,1,0,0],[0,0,1,0,0],[0,0,1,0,0],[0,0,1,0,0]] 7 | [[0,0,0,0,0,0],[1,1,1,1,1,0],[0,0,0,0,0,0],[0,1,1,1,1,1]] 8 | [[1]] 9 | [[2]] 10 | [[0]] -------------------------------------------------------------------------------- /testcases/845: -------------------------------------------------------------------------------- 1 | [2,1,4,7,3,2,5] 2 | [1,2,1,1,2,1] 3 | [10,20,30] 4 | [1,4,3] 5 | [3,2,1] 6 | [2,1,1,2,3,2,2,3,1] 7 | [1,2,3,3,2,1] 8 | [1,2,3,4,5,4,3,3,3,3,3,2,1] 9 | [1,2,2,2,3,4,3,3,2,1] 10 | [5,4,4,3,4,5,2] 11 | [1,5,4,4,3] 12 | [1,1,5,4,4,3] 13 | [2,1,1,5,4,4,3] 14 | [2,1,1,5,5,4,4,3] 15 | [2,1,1,5,1,5,4,4,3] 16 | [875,884,239,731,723,685] -------------------------------------------------------------------------------- /src/1427.perform-string-shifts.py: -------------------------------------------------------------------------------- 1 | # REVIEWME: 2 | class Solution: 3 | # 32 ms, 61.03%. Time: O(N + S), Space: O(1) 4 | def stringShift(self, s: str, shift: List[List[int]]) -> str: 5 | index = 0 6 | for dr, amnt in shift: 7 | index += amnt if dr == 0 else -amnt 8 | index %= len(s) 9 | return s[index:] + s[:index] -------------------------------------------------------------------------------- /testcases/863: -------------------------------------------------------------------------------- 1 | [3,5,1,6,2,0,8,null,null,7,4] 2 | 5 3 | 2 4 | [3,5,1,6,2,0,8,null,null,7,4] 5 | 5 6 | 0 7 | [3,5,1,6,2,0,8,null,null,7,4] 8 | 5 9 | 1 10 | [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,null,16,null,17,18,null,null,null,19,null,null,null,20,21,null,22,null,23,24,25,26,27,28,29,30,31,null,32,null,33,null,34,null,35,36,null,null,null,37,38] 11 | 10 12 | 6 -------------------------------------------------------------------------------- /src/1816.truncate-sentence.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=1816 lang=python3 3 | # 4 | # [1816] Truncate Sentence 5 | # 6 | 7 | # @lc code=start 8 | 9 | 10 | class Solution: 11 | # 28 ms, 84.68%. Time: O(N). Space: O(1) 12 | def truncateSentence(self, s: str, k: int) -> str: 13 | return " ".join(s.split(" ", k)[:k]) 14 | # @lc code=end 15 | -------------------------------------------------------------------------------- /testcases/721: -------------------------------------------------------------------------------- 1 | [["John","johnsmith@mail.com","john_newyork@mail.com"],["John","johnsmith@mail.com","john00@mail.com"],["Mary","mary@mail.com"],["John","johnnybravo@mail.com"]] 2 | [["David","David0@m.co","David1@m.co"],["David","David3@m.co","David4@m.co"],["David","David4@m.co","David5@m.co"],["David","David2@m.co","David3@m.co"],["David","David1@m.co","David2@m.co"]] -------------------------------------------------------------------------------- /testcases/1583: -------------------------------------------------------------------------------- 1 | 4 2 | [[1, 2, 3], [3, 2, 0], [3, 1, 0], [1, 2, 0]] 3 | [[0, 1], [2, 3]] 4 | 6 5 | [[4,1,2,3,5],[5,3,2,0,4],[3,1,0,4,5],[5,1,2,0,4],[5,0,1,2,3],[4,0,1,2,3]] 6 | [[0,4],[3,5],[1,2]] 7 | 2 8 | [[1], [0]] 9 | [[1,0]] 10 | 4 11 | [[1, 3, 2], [2, 3, 0], [1, 3, 0], [0, 2, 1]] 12 | [[1, 3], [0, 2]] 13 | 4 14 | [[1,3,2],[2,3,0],[1,0,3],[1,0,2]] 15 | [[2,1],[3,0]] -------------------------------------------------------------------------------- /testcases/1239: -------------------------------------------------------------------------------- 1 | ["zog","nvwsuikgndmfexxgjtkb","nxko"] 2 | ["aa","bb"] 3 | ["un","iq","ue"] 4 | ["yy","bkhwmpbiisbldzknpm"] 5 | ["cha","r","act","ers"] 6 | ["abcdefghijklmnopqrstuvwxyz"] 7 | ["a","b","c","d","e","f","g","h","j","i","k","l","m","n","o","p"] 8 | ["pxjq", "fisr", "dlfg", "dbpj", "wsmk", "pvtn", "jyfm", "uwfo", "hnm", "dklz", "nslj", "vkmr", "blsr", "puxr", "ultr", "vaon"] -------------------------------------------------------------------------------- /testcases/1472: -------------------------------------------------------------------------------- 1 | ["BrowserHistory","visit","visit","visit","back","back","forward","visit","forward","back","back"] 2 | [["leetcode.com"],["google.com"],["facebook.com"],["youtube.com"],[1],[1],[1],["linkedin.com"],[2],[2],[7]] 3 | ["BrowserHistory","visit","back","back","forward","forward","visit","visit","back"] 4 | [["zav.com"],["kni.com"],[7],[7],[5],[1],["pwrrbnw.com"],["mosohif.com"],[9]] -------------------------------------------------------------------------------- /testcases/1600: -------------------------------------------------------------------------------- 1 | ["ThroneInheritance","birth","birth","birth","birth","birth","birth","getInheritanceOrder","death","getInheritanceOrder","death","getInheritanceOrder","birth","birth","getInheritanceOrder"] 2 | [["king"],["king","andy"],["king","bob"],["king","catherine"],["andy","matthew"],["bob","alex"],["bob","asha"],[null],["bob"],[null],["king"],[null],["andy","toy"],["alex","alextwo"],[null]] -------------------------------------------------------------------------------- /src/2185.counting-words-with-a-given-prefix.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=2185 lang=python3 3 | # 4 | # [2185] Counting Words With a Given Prefix 5 | # 6 | 7 | # @lc code=start 8 | from typing import List 9 | 10 | 11 | class Solution: 12 | def prefixCount(self, words: List[str], pref: str) -> int: 13 | return sum(w.startswith(pref) for w in words) 14 | # @lc code=end 15 | -------------------------------------------------------------------------------- /testcases/134: -------------------------------------------------------------------------------- 1 | [1,2,3,4,5] 2 | [3,4,5,1,2] 3 | [2,3,4] 4 | [3,4,3] 5 | [3, 4, 1, 4, 4, 2, 3, 4, 4, 4, 4, 2, 3, 1, 3, 1, 1, 3, 3, 4] 6 | [2, 2, 1, 2, 2, 3, 3, 1, 4, 4, 2, 4, 3, 2, 3, 4, 2, 2, 2, 4] 7 | [1, 4, 1, 2, 5, 7, 6, 3, 7, 2, 1, 7, 6, 1, 4, 4, 5, 3, 5, 3] 8 | [2, 7, 3, 1, 3, 2, 4, 1, 6, 6, 3, 6, 2, 4, 7, 2, 3, 7, 5, 1] 9 | [] 10 | [] 11 | [1] 12 | [1] 13 | [1] 14 | [2] 15 | [2] 16 | [1] -------------------------------------------------------------------------------- /testcases/74: -------------------------------------------------------------------------------- 1 | [[1,3,5,7],[10,11,16,20],[23,30,34,50]] 2 | 3 3 | [] 4 | 1 5 | [[]] 6 | 1 7 | [[1,3,5,7],[10,11,16,20],[23,30,34,50]] 8 | 13 9 | [[1,3,5,7],[10,11,16,20],[23,30,34,50]] 10 | 10 11 | [[1,3,5,7],[10,11,16,20],[23,30,34,50]] 12 | 20 13 | [[1,3,5,7],[10,11,16,20],[23,30,34,50]] 14 | 21 15 | [[1,3,5,7],[10,11,16,20],[23,30,34,50]] 16 | 16 17 | [[1,3,5,7],[10,11,16,20],[23,30,34,50]] 18 | 17 -------------------------------------------------------------------------------- /src/2119.a-number-after-a-double-reversal.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=2119 lang=python3 3 | # 4 | # [2119] A Number After a Double Reversal 5 | # 6 | 7 | # @lc code=start 8 | class Solution: 9 | # 28 ms, 85.96%. Time and Space: O(1) 10 | def isSameAfterReversals(self, num: int) -> bool: 11 | if num == 0: 12 | return True 13 | return num % 10 14 | # @lc code=end 15 | -------------------------------------------------------------------------------- /src/1784.check-if-binary-string-has-at-most-one-segment-of-ones.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=1784 lang=python3 3 | # 4 | # [1784] Check if Binary String Has at Most One Segment of Ones 5 | # 6 | 7 | # @lc code=start 8 | 9 | 10 | class Solution: 11 | # 28 ms, 88.60%. Time: O(N). Space: O(1) 12 | def checkOnesSegment(self, s: str) -> bool: 13 | return "01" not in s 14 | 15 | # @lc code=end 16 | -------------------------------------------------------------------------------- /testcases/1476: -------------------------------------------------------------------------------- 1 | ["SubrectangleQueries","getValue","updateSubrectangle","getValue","getValue","updateSubrectangle","getValue","getValue"] 2 | [[[[1,2,1],[4,3,4],[3,2,1],[1,1,1]]],[0,2],[0,0,3,2,5],[0,2],[3,1],[3,0,3,2,10],[3,1],[0,2]] 3 | ["SubrectangleQueries","getValue","updateSubrectangle","getValue","getValue","updateSubrectangle","getValue"] 4 | [[[[1,1,1],[2,2,2],[3,3,3]]],[0,0],[0,0,2,2,100],[0,0],[2,2],[1,1,2,2,20],[2,2]] -------------------------------------------------------------------------------- /testcases/1604: -------------------------------------------------------------------------------- 1 | ["daniel","daniel","daniel","luis","luis","luis","luis"] 2 | ["10:00","10:40","11:00","09:00","11:00","13:00","15:00"] 3 | ["alice","alice","alice","bob","bob","bob","bob"] 4 | ["12:01","12:00","18:00","21:00","21:20","21:30","23:00"] 5 | ["john","john","john"] 6 | ["23:58","23:59","00:01"] 7 | ["leslie","leslie","leslie","clare","clare","clare","clare"] 8 | ["13:00","13:20","14:00","18:00","18:51","19:30","19:49"] -------------------------------------------------------------------------------- /src/1480.running-sum-of-1-d-array.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=1480 lang=python3 3 | # 4 | # [1480] Running Sum of 1d Array 5 | # 6 | 7 | # @lc code=start 8 | class Solution: 9 | # 36 ms, 91.05%. Time: O(N), Space: O(1) 10 | def runningSum(self, nums: List[int]) -> List[int]: 11 | for i in range(1, len(nums)): 12 | nums[i] += nums[i - 1] 13 | return nums 14 | # @lc code=end 15 | 16 | -------------------------------------------------------------------------------- /src/46.permutations.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=46 lang=python3 3 | # 4 | # [46] Permutations 5 | # 6 | 7 | class Solution: 8 | def permute(self, nums: List[int]) -> List[List[int]]: 9 | import itertools 10 | return itertools.permutations(nums) 11 | 12 | def permute1(self, nums: List[int]) -> List[List[int]]: 13 | import itertools 14 | return itertools.permutations(nums) 15 | 16 | -------------------------------------------------------------------------------- /src/1332.remove-palindromic-subsequences.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=1332 lang=python3 3 | # 4 | # [1332] Remove Palindromic Subsequences 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: String 9 | 10 | 11 | class Solution: 12 | def removePalindromeSub(self, s: str) -> int: 13 | if not s: 14 | return 0 15 | if s == s[::-1]: 16 | return 1 17 | return 2 18 | # @lc code=end 19 | -------------------------------------------------------------------------------- /src/1460.make-two-arrays-equal-by-reversing-sub-arrays.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=1460 lang=python3 3 | # 4 | # [1460] Make Two Arrays Equal by Reversing Sub-arrays 5 | # 6 | 7 | # @lc code=start 8 | class Solution: 9 | # 76 ms, 95.63%. O(N) 10 | def canBeEqual(self, target: List[int], arr: List[int]) -> bool: 11 | return collections.Counter(target) == collections.Counter(arr) 12 | # @lc code=end 13 | 14 | -------------------------------------------------------------------------------- /src/1812.determine-color-of-a-chessboard-square.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=1812 lang=python3 3 | # 4 | # [1812] Determine Color of a Chessboard Square 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: Math, String 9 | class Solution: 10 | def squareIsWhite(self, coordinates: str) -> bool: 11 | r, c = coordinates 12 | r = ord(r) - ord('a') 13 | c = int(c) - 1 14 | return (r + c) % 2 15 | # @lc code=end 16 | -------------------------------------------------------------------------------- /testcases/152: -------------------------------------------------------------------------------- 1 | [] 2 | [2,3,-2,4] 3 | [-2,-2,-2] 4 | [-1,0,-1,0,-1,0,-1] 5 | [0,1,0,1,0,1] 6 | [-3, 3, -1, 4, 5, -2, -3, 0, 2, 4, 0, 0, 1, 5, -4, -3, 3, 2, 1, 4, 0, -5, -2, 4, -5, -2, -5, 2, 2, 3, 4, 5, -2, 3, -2, 0, -1, 2, -1, 4, 3, -5, -2, 0, -4, -4, -3, 1, -1, 3, -1, -5, -2, 2, -1, 4, 2, 3, -2, 3, 0, 3, -3, 3, -5, -5, 1, 2, 4, 1, -3, -1, 5, -4, 3, 2, -1, 1, 1, 0, -4, 1, -4, -3, -5, -5, 4, 0, -4, 3, 0, 1, 4, -3, -3, 5, -2, -1, -2, 4] 7 | [1, -1000, 2, 3, 4, -5, -1] -------------------------------------------------------------------------------- /src/1491.average-salary-excluding-the-minimum-and-maximum-salary.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=1491 lang=python3 3 | # 4 | # [1491] Average Salary Excluding the Minimum and Maximum Salary 5 | # 6 | 7 | # @lc code=start 8 | class Solution: 9 | # 32 ms 78.72%. Time: O(N). Space: O(1) 10 | def average(self, salary: List[int]) -> float: 11 | return (sum(salary) - min(salary) - max(salary)) / (len(salary) - 2) 12 | 13 | # @lc code=end 14 | 15 | -------------------------------------------------------------------------------- /src/1523.count-odd-numbers-in-an-interval-range.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=1523 lang=python3 3 | # 4 | # [1523] Count Odd Numbers in an Interval Range 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: Math 9 | class Solution: 10 | # 32 ms, 66.46 %. Time and Space: O(1) 11 | def countOdds(self, low: int, high: int) -> int: 12 | low_no = low // 2 13 | high_no = high // 2 + high % 2 14 | return high_no - low_no 15 | # @lc code=end 16 | 17 | -------------------------------------------------------------------------------- /src/1529.bulb-switcher-iv.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=1529 lang=python3 3 | # 4 | # [1529] Bulb Switcher IV 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: String 9 | class Solution: 10 | # 116 ms, 49.82 %. Time: O(N). Space: O(1) 11 | def minFlips(self, target: str) -> int: 12 | cnt = int(target[0]) 13 | for b1, b2 in zip(target, target[1:]): 14 | if b1 != b2: cnt += 1 15 | return cnt 16 | 17 | 18 | # @lc code=end -------------------------------------------------------------------------------- /testcases/187: -------------------------------------------------------------------------------- 1 | "" 2 | "ACTG" 3 | "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT" 4 | "AAAAACCCCCAAAAACCCCCAAAAA" 5 | "ATAGAGGTTAATTTCCAAGGGTCCTCATCGAACTACGGCTGTTCCCGCGTTGTTTACACCACTGATACCCACTTCAATTTCTTCGTTGGCTACAAGCACGTATGAGGTCTCTTGCCAAGCCCCCTCTGGGGTTTTCGAACGCGTGGGTGCTTCTTACCAGCACAAAGTTATATACAGGCCTGCGTCACTATAGAGGTTAATTTCCAAGGGTCCTCATCGAACTACGGCTGTTCCCGCGTTGTTTACACCACTGATACCCACTTCAATTTCTTCGTTGGCTACAAGCACGTATGAGGTCTCTTGCCAAGCCCCCTCTGGGGTTTTCGAACGCGTGGGTGCTTCTTACCAGCACAAAGTTATATACAGGCCTGCGTCACT" -------------------------------------------------------------------------------- /testcases/450: -------------------------------------------------------------------------------- 1 | [1,2] 2 | 1 3 | [1,null, 2] 4 | 1 5 | [] 6 | 0 7 | [0] 8 | 0 9 | [5,3,6,2,4,null,7] 10 | 3 11 | [0,-8,2,null,-5,null,4,null,null,null,6,null,9,7] 12 | -5 13 | [0,-8,2,null,-5,null,4,null,null,null,6,null,9,7] 14 | 2 15 | [0,-8,2,null,-5,null,4,null,null,null,6,null,9,7] 16 | 4 17 | [0,-8,2,null,-5,null,4,null,null,null,6,null,9,7] 18 | 7 19 | [0,-8,2,null,-5,null,4,null,null,null,6,null,9,7] 20 | 9 21 | [0,-8,2,null,-5,null,4,null,null,null,6,null,9,7] 22 | 0 -------------------------------------------------------------------------------- /src/1528.shuffle-string.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=1528 lang=python3 3 | # 4 | # [1528] Shuffle String 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: String 9 | class Solution: 10 | # 68 ms, 41.4%. Time and Space: O(N) 11 | def restoreString(self, s: str, indices: List[int]) -> str: 12 | new_s = [0] * len(indices) 13 | for i in range(len(indices)): 14 | new_s[indices[i]] = s[i] 15 | return "".join(new_s) 16 | # @lc code=end 17 | 18 | -------------------------------------------------------------------------------- /src/3084.count-substrings-starting-and-ending-with-given-character.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=3084 lang=python3 3 | # 4 | # [3084] Count Substrings Starting and Ending with Given Character 5 | # 6 | 7 | 8 | # @lc code=start 9 | # TAGS: Math, String, Counting 10 | class Solution: 11 | # Time: O(N). Space: O(1) 12 | def countSubstrings(self, s: str, c: str) -> int: 13 | n = s.count(c) 14 | return (n + 1) * n // 2 15 | 16 | 17 | # @lc code=end 18 | -------------------------------------------------------------------------------- /src/1832.check-if-the-sentence-is-pangram.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=1832 lang=python3 3 | # 4 | # [1832] Check if the Sentence Is Pangram 5 | # 6 | 7 | # @lc code=start 8 | 9 | 10 | class Solution: 11 | # 56 ms, 100%. Time: O(N). Space: O(1) 12 | def checkIfPangram(self, sentence: str) -> bool: 13 | counters = [0] * 26 14 | for c in sentence: 15 | counters[ord(c) - ord('a')] += 1 16 | return 0 not in counters 17 | # @lc code=end 18 | -------------------------------------------------------------------------------- /src/717.1-bit-and-2-bit-characters.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=717 lang=python3 3 | # 4 | # [717] 1-bit and 2-bit Characters 5 | # 6 | 7 | 8 | # @lc code=start 9 | # TAGS: Array, Greedy 10 | # REVIEWME: Greedy 11 | class Solution: 12 | def isOneBitCharacter(self, bits: List[int]) -> bool: 13 | ptr = 0 14 | while ptr < len(bits) - 1: 15 | ptr += 2 if bits[ptr] == 1 else 1 16 | return ptr == len(bits) - 1 17 | 18 | 19 | # @lc code=end 20 | -------------------------------------------------------------------------------- /src/171.excel-sheet-column-number.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=171 lang=python3 3 | # 4 | # [171] Excel Sheet Column Number 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: String 9 | class Solution: 10 | # 20 ms, 99%. Time: O(N), Space: O(1) 11 | def titleToNumber(self, s: str) -> int: 12 | total = 0 13 | for i, c in enumerate(s): 14 | total += (ord(c) - ord("A") + 1) * 26 ** (len(s) - i - 1) 15 | return total 16 | 17 | # @lc code=end 18 | 19 | -------------------------------------------------------------------------------- /src/1837.sum-of-digits-in-base-k.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=1837 lang=python3 3 | # 4 | # [1837] Sum of Digits in Base K 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: Math, Bit Manipulation 9 | 10 | 11 | class Solution: 12 | # 28 ms, 85.83%. Time: O(logN). Space: O(1) 13 | def sumBase(self, n: int, k: int) -> int: 14 | ans = 0 15 | while n >= k: 16 | n, r = divmod(n, k) 17 | ans += r 18 | return ans + n 19 | # @lc code=end 20 | -------------------------------------------------------------------------------- /src/387.first-unique-character-in-a-string.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=387 lang=python3 3 | # 4 | # [387] First Unique Character in a String 5 | # 6 | 7 | # @lc code=start 8 | class Solution: 9 | # Time Complexity: O(N) 10 | # Space Complexity: O(N) 11 | def firstUniqChar(self, s: str) -> int: 12 | d=collections.Counter(s) 13 | 14 | for i in range(len(s)): 15 | if d[s[i]]==1: return i 16 | return -1 17 | # @lc code=end 18 | 19 | -------------------------------------------------------------------------------- /src/1684.count-the-number-of-consistent-strings.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=1684 lang=python3 3 | # 4 | # [1684] Count the Number of Consistent Strings 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: String 9 | class Solution: 10 | # 272 ms, 36.61%. Time: O(N). Space: O(1) 11 | def countConsistentStrings(self, allowed: str, words: List[str]) -> int: 12 | base = set(allowed) 13 | return sum(not bool(set(word) - base) for word in words) 14 | 15 | # @lc code=end 16 | 17 | -------------------------------------------------------------------------------- /testcases/1530: -------------------------------------------------------------------------------- 1 | [1,2,3,null,4] 2 | 3 3 | [1,2,3,4,5,6,7] 4 | 3 5 | [7,1,4,6,null,5,3,null,null,null,null,null,2] 6 | 3 7 | [100] 8 | 1 9 | [1,1,1] 10 | 2 11 | [1,1,1,null,null,1,1,null,null,1,1,null,null,1,1,null,null,1,1,null,null,1,1,null,null,1,1,null,null,1,1,null,null,1,1,null,1,1,null,null,1,1,null,null,1,1,null,null,1,1,null,null,1,1,null,null,1,1,null,null,1,1,null,null,1,1,null,null,1,1,null,null,1,1,null,null,1,1,null,null,1,1,null,1,1,null,null,1,1,null,null,1,1,null,null,1,1] 12 | 10 -------------------------------------------------------------------------------- /testcases/622: -------------------------------------------------------------------------------- 1 | ["MyCircularQueue","enQueue","enQueue","enQueue","enQueue","Rear","isFull","deQueue","enQueue","Rear"] 2 | [[3],[1],[2],[3],[4],[],[],[],[4],[]] 3 | ["MyCircularQueue","enQueue","Rear","Front","deQueue","Front","deQueue","Front","enQueue","enQueue","enQueue","enQueue"] 4 | [[3],[2],[],[],[],[],[],[],[4],[2],[2],[3]] 5 | ["MyCircularQueue","enQueue","Rear","enQueue","deQueue","Front","deQueue","deQueue","isEmpty","deQueue","enQueue","enQueue"] 6 | [[2],[4],[],[9],[],[],[],[],[],[],[6],[4]] -------------------------------------------------------------------------------- /src/1344.angle-between-hands-of-a-clock.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=1344 lang=python3 3 | # 4 | # [1344] Angle Between Hands of a Clock 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: Math 9 | class Solution: 10 | # 28 ms, 33%. Time, Space: O(1) 11 | def angleClock(self, hour: int, minutes: int) -> float: 12 | h = ((hour/12 + (minutes/60)/12) * 360 )% 360 13 | m = (minutes/60) * 360 14 | return min(abs(h-m), 360 - abs(h-m)) 15 | 16 | # @lc code=end 17 | 18 | -------------------------------------------------------------------------------- /src/2000.reverse-prefix-of-word.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=2000 lang=python3 3 | # 4 | # [2000] Reverse Prefix of Word 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: Two Pointers, String 9 | class Solution: 10 | # 24 ms, 96.39%. Time: O(N). Space: O(1) 11 | def reversePrefix(self, word: str, ch: str) -> str: 12 | for i, c in enumerate(word): 13 | if c == ch: 14 | return word[:i + 1][::-1] + word[i + 1:] 15 | return word 16 | # @lc code=end 17 | -------------------------------------------------------------------------------- /testcases/80: -------------------------------------------------------------------------------- 1 | [1,1,1,2,2,3] 2 | [1,1,1,1,2,2,2,2,3,3,3,3] 3 | [0,0,1,1,1,1,2,3,3] 4 | [0] 5 | [] 6 | [1,1,1,1,1,1,2] 7 | [-20, -20, -19, -19, -19, -19, -17, -15, -15, -15, -15, -14, -13, -12, -12, -12, -11, -11, -11, -10, -10, -10, -9, -9, -9, -8, -7, -7, -7, -7, -6, -6, -5, -5, -5, -5, -5, -5, -4, -4, -4, -3, -3, -3, -2, -2, -1, -1, -1, 0, 0, 0, 1, 1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 7, 8, 8, 8, 8, 9, 10, 10, 11, 11, 12, 12, 12, 13, 14, 14, 14, 14, 15, 15, 15, 16, 16, 17, 17, 18, 19, 19, 19] -------------------------------------------------------------------------------- /src/1450.number-of-students-doing-homework-at-a-given-time.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=1450 lang=python3 3 | # 4 | # [1450] Number of Students Doing Homework at a Given Time 5 | # 6 | 7 | # @lc code=start 8 | class Solution: 9 | """ 10 | 64 ms, 35.27%. Time: O(N). Space: O(1) 11 | """ 12 | def busyStudent(self, startTime: List[int], endTime: List[int], queryTime: int) -> int: 13 | return sum(lo <= queryTime <= hi for lo, hi in zip(startTime, endTime)) 14 | # @lc code=end 15 | 16 | -------------------------------------------------------------------------------- /src/1903.largest-odd-number-in-string.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=1903 lang=python3 3 | # 4 | # [1903] Largest Odd Number in String 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: Math, String, Greedy 9 | 10 | 11 | class Solution: 12 | # 52 ms, 61.71%. Time: O(N). Space: O(1) 13 | def largestOddNumber(self, num: str) -> str: 14 | for i in reversed(range(len(num))): 15 | if int(num[i]) % 2: 16 | return num[: i + 1] 17 | return '' 18 | # @lc code=end 19 | -------------------------------------------------------------------------------- /src/869.reordered-power-of-2.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=869 lang=python3 3 | # 4 | # [869] Reordered Power of 2 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: Math 9 | from collections import Counter 10 | class Solution: 11 | # 24 ms, 98.39%. Time: O(logN) Space: O(N) 12 | def reorderedPowerOf2(self, N: int) -> bool: 13 | order_of_2 = [2**val for val in range(32)] 14 | return any(Counter(str(N)) == Counter(str(val)) for val in order_of_2) 15 | 16 | # @lc code=end 17 | 18 | -------------------------------------------------------------------------------- /src/1217.minimum-cost-to-move-chips-to-the-same-position.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=1217 lang=python3 3 | # 4 | # [1217] Minimum Cost to Move Chips to The Same Position 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: Array, Greedy. Math 9 | class Solution: 10 | # Time: O(N). Space: O(1) 11 | def minCostToMoveChips(self, position: List[int]) -> int: 12 | cnt = [0, 0] 13 | for chip in position: 14 | cnt[chip % 2] += 1 15 | return min(cnt) 16 | # @lc code=end 17 | 18 | -------------------------------------------------------------------------------- /src/1556.thousand-separator.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=1556 lang=python3 3 | # 4 | # [1556] Thousand Separator 5 | # 6 | 7 | # @lc code=start 8 | class Solution: 9 | # 32 ms, 81.48%. Time and Space: O(N) 10 | def thousandSeparator(self, n: int) -> str: 11 | n = str(n) 12 | start = len(n) % 3 13 | rv = [n[:start]] if start else [] 14 | for i in range(start, len(n), 3): 15 | rv.append(n[i:i+3]) 16 | return ".".join(rv) 17 | # @lc code=end 18 | 19 | -------------------------------------------------------------------------------- /src/1773.count-items-matching-a-rule.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=1773 lang=python3 3 | # 4 | # [1773] Count Items Matching a Rule 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: Array, String 9 | 10 | 11 | class Solution: 12 | # 240 ms, 100%. Time: O(N). Space: O(1) 13 | def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int: 14 | pos = {"type": 0, "color": 1, "name": 2} 15 | return sum(item[pos[ruleKey]] == ruleValue for item in items) 16 | # @lc code=end 17 | -------------------------------------------------------------------------------- /src/2073.time-needed-to-buy-tickets.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=2073 lang=python3 3 | # 4 | # [2073] Time Needed to Buy Tickets 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: Array, Queue, Simulation 9 | class Solution: 10 | # 32 ms, 85.71%. Time: O(N). Space: O(1) 11 | def timeRequiredToBuy(self, tickets: List[int], k: int) -> int: 12 | cnt = 0 13 | for i, val in enumerate(tickets): 14 | cnt += min(tickets[k] - bool(k < i), val) 15 | return cnt 16 | # @lc code=end 17 | -------------------------------------------------------------------------------- /src/1572.matrix-diagonal-sum.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=1572 lang=python3 3 | # 4 | # [1572] Matrix Diagonal Sum 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: Array 9 | class Solution: 10 | # 104 ms, 96.75%. Time: O(N). Space: O(1) 11 | def diagonalSum(self, mat: List[List[int]]) -> int: 12 | N = len(mat) 13 | total = - mat[N//2][N//2] if N % 2 else 0 14 | for i in range(N): 15 | total += mat[i][i] + mat[i][~i] 16 | return total 17 | 18 | # @lc code=end 19 | 20 | -------------------------------------------------------------------------------- /src/1732.find-the-highest-altitude.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=1732 lang=python3 3 | # 4 | # [1732] Find the Highest Altitude 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: Array 9 | 10 | 11 | class Solution: 12 | # 36 ms, 57.22%. Time: O(N). Space: O(1) 13 | def largestAltitude(self, gain: List[int]) -> int: 14 | running_sum = result = 0 15 | for g in gain: 16 | running_sum += g 17 | result = max(result, running_sum) 18 | return result 19 | # @lc code=end 20 | -------------------------------------------------------------------------------- /src/2103.rings-and-rods.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=2103 lang=python3 3 | # 4 | # [2103] Rings and Rods 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: Hash Table, String 9 | class Solution: 10 | def countPoints(self, rings: str) -> int: 11 | rods = [set() for _ in range(10)] 12 | for i in range(0, len(rings), 2): 13 | c = rings[i] 14 | r = int(rings[i + 1]) 15 | rods[r].add(c) 16 | return sum(len(rod) == 3 for rod in rods) 17 | 18 | # @lc code=end 19 | -------------------------------------------------------------------------------- /src/532.k-diff-pairs-in-an-array.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=532 lang=python3 3 | # 4 | # [532] K-diff Pairs in an Array 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: Array, Two Pointers 9 | class Solution: 10 | # 68 ms, 99.66%. Time and Space: O(N) 11 | def findPairs(self, nums: List[int], k: int) -> int: 12 | C = collections.Counter(nums) 13 | if k == 0: 14 | return sum(val > 1 for val in C.values()) 15 | return sum(num + k in C for num in C) 16 | # @lc code=end 17 | 18 | -------------------------------------------------------------------------------- /testcases/139: -------------------------------------------------------------------------------- 1 | "catsandog" 2 | ["cats", "dog", "sand", "and", "cat"] 3 | "applepenapple" 4 | ["apple", "pen"] 5 | "leetcode" 6 | ["leet", "code"] 7 | "catsandog" 8 | ["cats", "dog", "sand", "and", "cat","og"] 9 | "catsandog" 10 | ["cats", "dog", "sand", "and", "cat","sandog"] 11 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab" 12 | ["a","aa","aaa","aaaa","aaaaa","aaaaaa","aaaaaaa","aaaaaaaa","aaaaaaaaa","aaaaaaaaaa"] -------------------------------------------------------------------------------- /src/462.minimum-moves-to-equal-array-elements-ii.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=462 lang=python3 3 | # 4 | # [462] Minimum Moves to Equal Array Elements II 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: Math 9 | class Solution: 10 | # 72 ms, 78.59%. Time: O(NlogN). Space: O(N). Median 11 | def minMoves2(self, nums: List[int]) -> int: 12 | if not nums: return 0 13 | nums.sort() 14 | median = nums[len(nums)//2] 15 | return sum(abs(val - median) for val in nums) 16 | # @lc code=end 17 | 18 | -------------------------------------------------------------------------------- /testcases/198: -------------------------------------------------------------------------------- 1 | [1,2,3,1] 2 | [2,7,9,3,1] 3 | [1,9,1,1,9] 4 | [] 5 | [] 6 | [134, 72, 179, 97, 261, 29, 191, 222, 385, 367, 17, 230, 363, 47, 261, 74, 393, 322, 11, 296, 291, 153, 363, 347, 164, 386, 178, 389, 12, 181, 9, 126, 215, 22, 164, 3, 385, 231, 148, 153, 27, 236, 193, 274, 274, 311, 355, 50, 366, 381, 2, 84, 46, 330, 289, 238, 169, 243, 7, 316, 108, 324, 196, 233, 350, 171, 278, 282, 200, 294, 235, 198, 59, 230, 32, 378, 52, 77, 351, 253, 86, 47, 65, 108, 157, 164, 225, 386, 134, 303, 279, 19, 302, 207, 185, 67, 267, 357, 17, 368] -------------------------------------------------------------------------------- /testcases/775: -------------------------------------------------------------------------------- 1 | [0] 2 | [0, 1, 2] 3 | [0, 2, 1] 4 | [1, 0, 2] 5 | [1, 2, 0] 6 | [2, 0, 1] 7 | [2, 1, 0] 8 | [0, 1, 2, 3] 9 | [0, 1, 3, 2] 10 | [0, 2, 1, 3] 11 | [0, 2, 3, 1] 12 | [0, 3, 1, 2] 13 | [0, 3, 2, 1] 14 | [1, 0, 2, 3] 15 | [1, 0, 3, 2] 16 | [1, 2, 0, 3] 17 | [1, 2, 3, 0] 18 | [1, 3, 0, 2] 19 | [1, 3, 2, 0] 20 | [2, 0, 1, 3] 21 | [2, 0, 3, 1] 22 | [2, 1, 0, 3] 23 | [2, 1, 3, 0] 24 | [2, 3, 0, 1] 25 | [2, 3, 1, 0] 26 | [3, 0, 1, 2] 27 | [3, 0, 2, 1] 28 | [3, 1, 0, 2] 29 | [3, 1, 2, 0] 30 | [3, 2, 0, 1] 31 | [3, 2, 1, 0] -------------------------------------------------------------------------------- /src/1446.consecutive-characters.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=1446 lang=python3 3 | # 4 | # [1446] Consecutive Characters 5 | # 6 | # TAGS: String 7 | # @lc code=start 8 | class Solution: 9 | # 44 ms, 54.86%. O(N), O(1) 10 | def maxPower(self, s: str) -> int: 11 | cnt = ans = 1 12 | for c1, c2 in zip(s, s[1:]): 13 | if c1 == c2: 14 | cnt += 1 15 | else: 16 | cnt = 1 17 | ans = max(ans, cnt) 18 | return ans 19 | # @lc code=end 20 | 21 | -------------------------------------------------------------------------------- /src/1748.sum-of-unique-elements.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=1748 lang=python3 3 | # 4 | # [1748] Sum of Unique Elements 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: Array, Hash Table 9 | 10 | 11 | class Solution: 12 | # 36 ms, 68.56%. Time and Space: O(N) 13 | def sumOfUnique(self, nums: List[int]) -> int: 14 | counter = collections.Counter(nums) 15 | total = 0 16 | for v, f in counter.items(): 17 | if f == 1: 18 | total += v 19 | return total 20 | # @lc code=end 21 | -------------------------------------------------------------------------------- /testcases/1552: -------------------------------------------------------------------------------- 1 | [1,2,3,4,7] 2 | 3 3 | [5,4,3,2,1,1000000000] 4 | 2 5 | [5, 901, 277, 789, 919, 164, 424, 555, 814, 819, 951, 185, 835, 708, 963, 838, 586, 976, 85, 731, 860, 222, 480, 868, 102, 616, 873, 880, 632, 761] 6 | 20 7 | [5, 901, 277, 789, 919, 164, 424, 555, 814, 819, 951, 185, 835, 708, 963, 838, 586, 976, 85, 731, 860, 222, 480, 868, 102, 616, 873, 880, 632, 761] 8 | 10 9 | [5, 901, 277, 789, 919, 164, 424, 555, 814, 819, 951, 185, 835, 708, 963, 838, 586, 976, 85, 731, 860, 222, 480, 868, 102, 616, 873, 880, 632, 761] 10 | 30 -------------------------------------------------------------------------------- /src/1598.crawler-log-folder.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=1598 lang=python3 3 | # 4 | # [1598] Crawler Log Folder 5 | # 6 | 7 | # @lc code=start 8 | class Solution: 9 | # 40 ms, 94.64 %. Time: O(N). Space: O(1) 10 | def minOperations(self, logs: List[str]) -> int: 11 | depth = 0 12 | for log in logs: 13 | if log == "../": 14 | if depth: depth -= 1 15 | elif log != "./": 16 | depth += 1 17 | return depth 18 | 19 | # @lc code=end 20 | 21 | -------------------------------------------------------------------------------- /src/89.gray-code.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=89 lang=python3 3 | # 4 | # [89] Gray Code 5 | # 6 | 7 | 8 | # @lc code=start 9 | # TAGS: Math, Backtracking, Bit Manipulation 10 | class Solution: 11 | # Time and Space: O(2^N) 12 | def grayCode(self, n: int) -> List[int]: 13 | ans = [0] 14 | for i in range(n): 15 | highest_bit = 1 << i 16 | for j in reversed(range(highest_bit)): 17 | ans.append(ans[j] + highest_bit) 18 | return ans 19 | 20 | 21 | # @lc code=end 22 | -------------------------------------------------------------------------------- /testcases/1024: -------------------------------------------------------------------------------- 1 | [[0,2],[4,6],[8,10],[1,9],[1,5],[5,9]] 2 | 10 3 | [[0,1],[1,2]] 4 | 5 5 | [[0,1],[6,8],[0,2],[5,6],[0,4],[0,3],[6,7],[1,3],[4,7],[1,4],[2,5],[2,6],[3,4],[4,5],[5,7],[6,9]] 6 | 9 7 | [[0,4],[2,8]] 8 | 5 9 | [[0,4]] 10 | 3 11 | [[0,4]] 12 | 5 13 | [[0,2],[4,8]] 14 | 5 15 | [[2,4]] 16 | 0 17 | [[2,4]] 18 | 1 19 | [[5,7],[1,8],[0,0],[2,3],[4,5],[0,6],[5,10],[7,10]] 20 | 5 21 | [[8,10],[17,39],[18,19],[8,16],[13,35],[33,39],[11,19],[18,35]] 22 | 20 23 | [[0,0], [1, 2]] 24 | 2 25 | [[0, 0], [0,1]] 26 | 1 27 | [[0, 0], [0,1]] 28 | 2 -------------------------------------------------------------------------------- /src/2186.minimum-number-of-steps-to-make-two-strings-anagram-ii.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=2186 lang=python3 3 | # 4 | # [2186] Minimum Number of Steps to Make Two Strings Anagram II 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: Hash Table, String, Counting 9 | import collections 10 | 11 | 12 | class Solution: 13 | def minSteps(self, s: str, t: str) -> int: 14 | c1 = collections.Counter(s) 15 | c2 = collections.Counter(t) 16 | return sum((c1 - c2).values()) + sum((c2 - c1).values()) 17 | 18 | # @lc code=end 19 | -------------------------------------------------------------------------------- /src/7.reverse-integer.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=7 lang=python3 3 | # 4 | # [7] Reverse Integer 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: Tricky 9 | class Solution: 10 | # 32 ms. 60.66% 11 | # Note that `<<`` does not take priority over `-`. 12 | # This means (1<<31) - 1 != (1<<31 - 1) 13 | # Use input to test this 14 | def reverse(self, x: int) -> int: 15 | ans = -int(str(-x)[::-1]) if x < 0 else int(str(x)[::-1]) 16 | return ans if (-1<<31) <= ans <= ((1<<31) - 1) else 0 17 | # @lc code=end 18 | 19 | -------------------------------------------------------------------------------- /src/1768.merge-strings-alternately.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=1768 lang=python3 3 | # 4 | # [1768] Merge Strings Alternately 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: String 9 | 10 | 11 | class Solution: 12 | # 20 ms, 100%. Time and Space: O(N) 13 | def mergeAlternately(self, word1: str, word2: str) -> str: 14 | rv = "" 15 | i = 0 16 | while i < len(word1) and i < len(word2): 17 | rv += word1[i] + word2[i] 18 | i += 1 19 | return rv + word1[i:] + word2[i:] 20 | # @lc code=end 21 | -------------------------------------------------------------------------------- /src/495.teemo-attacking.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=495 lang=python3 3 | # 4 | # [495] Teemo Attacking 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: Array 9 | class Solution: 10 | # 248 ms, 99.52%. Time: O(N). Space: O(1) 11 | def findPoisonedDuration(self, timeSeries: List[int], duration: int) -> int: 12 | if not timeSeries: return 0 13 | total = 0 14 | for t1, t2 in zip(timeSeries, timeSeries[1:]): 15 | total += min(duration, t2 - t1) 16 | return total + duration 17 | 18 | # @lc code=end 19 | 20 | -------------------------------------------------------------------------------- /src/520.detect-capital.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=520 lang=python3 3 | # 4 | # [520] Detect Capital 5 | # 6 | 7 | # @lc code=start 8 | class Solution: 9 | # 24 ms, 95.83%. Time O(N) Space O(1) 10 | def detectCapitalUse(self, word: str) -> bool: 11 | return word in (word.lower(), word.upper(), word.capitalize()) 12 | 13 | # 24 ms, 95.83%. Time O(N) Space O(1) 14 | def detectCapitalUse(self, word: str) -> bool: 15 | return word.islower() or word.isupper() or word.istitle() 16 | 17 | # @lc code=end 18 | 19 | -------------------------------------------------------------------------------- /testcases/474: -------------------------------------------------------------------------------- 1 | ["10","0001","111001","1","0"] 2 | 5 3 | 3 4 | ["10","0","1"] 5 | 1 6 | 1 7 | ["01", "11", "00", "00", "11", "01", "01", "00", "11", "00", "11", "00", "00", "00", "01", "00", "11", "00", "00", "01", "00", "11", "01", "01", "00", "11", "00", "11", "00", "00", "00", "01", "00", "11", "00", "00", "01", "00", "11", "11", "00", "00", "11", "01", "01", "00", "11", "00", "11", "00", "00", "00", "01", "00", "11", "00", "00", "01", "00", "11", "01", "01", "00", "11", "00", "11", "00", "00", "00", "01", "00", "11", "00", "00", "01", "00", "11"] 8 | 20 9 | 20 -------------------------------------------------------------------------------- /src/1375.bulb-switcher-iii.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=1375 lang=python3 3 | # 4 | # [1375] Bulb Switcher III 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: Array 9 | class Solution: 10 | # 432 ms, 90%. O(N), O(1). Optimal 11 | def numTimesAllBlue(self, light: List[int]) -> int: 12 | high_bulb = blue_state_cnt = 0 13 | for i, bulb in enumerate(light, 1): 14 | high_bulb = max(high_bulb, bulb) 15 | if high_bulb == i: blue_state_cnt += 1 16 | return blue_state_cnt 17 | 18 | 19 | # @lc code=end 20 | 21 | -------------------------------------------------------------------------------- /src/1877.minimize-maximum-pair-sum-in-array.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=1877 lang=python3 3 | # 4 | # [1877] Minimize Maximum Pair Sum in Array 5 | # 6 | # @lc code=start 7 | # TAGS: Array, Two Pointers, Greedy, Sorting 8 | 9 | 10 | class Solution: 11 | # 1224 ms, 56.54%. Time: O(NlogN). Space: O(Sort) 12 | def minPairSum(self, nums: List[int]) -> int: 13 | nums.sort() 14 | ans = 0 15 | for i in range(len(nums) // 2): 16 | ans = max(ans, nums[i] + nums[~i]) 17 | return ans1 18 | # @lc code=end 19 | -------------------------------------------------------------------------------- /testcases/57: -------------------------------------------------------------------------------- 1 | [[1,3],[6,9]] 2 | [2,5] 3 | [[0,5],[9,12]] 4 | [7,16] 5 | [[3,5],[6,7],[8,10],[12,16]] 6 | [1,2] 7 | [[3,5],[6,7],[8,10],[12,16]] 8 | [1,3] 9 | [[3,5],[6,7],[8,10],[12,16]] 10 | [1,17] 11 | [[3,5],[6,7],[8,10],[12,16]] 12 | [6,8] 13 | [[2,3],[6,7],[8,10],[12,16]] 14 | [4,5] 15 | [[2,3],[6,7],[8,10],[12,16]] 16 | [4,8] 17 | [[2,3],[6,7],[8,10],[12,16]] 18 | [16,17] 19 | [[2,3],[6,7],[8,10],[12,16]] 20 | [17,18] 21 | [] 22 | [1,2] 23 | [[3,5]] 24 | [1,3] 25 | [[3,5]] 26 | [1,4] 27 | [[3,5]] 28 | [1,6] 29 | [[3,5]] 30 | [4,6] 31 | [[3,5]] 32 | [5,6] -------------------------------------------------------------------------------- /src/1561.maximum-number-of-coins-you-can-get.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=1561 lang=python3 3 | # 4 | # [1561] Maximum Number of Coins You Can Get 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: Sort 9 | class Solution: 10 | # 656 ms, 82.22 %. Time: O(NlogN) because of sorting. Space: O(1) 11 | def maxCoins(self, piles: List[int]) -> int: 12 | n = len(piles) // 3 13 | piles.sort(reverse=True) 14 | total = 0 15 | for i in range(n): 16 | total += piles[i*2 + 1] 17 | return total 18 | # @lc code=end 19 | 20 | -------------------------------------------------------------------------------- /src/406.queue-reconstruction-by-height.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=406 lang=python3 3 | # 4 | # [406] Queue Reconstruction by Height 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: Greedy 9 | # REVIEWME: Clever solution 10 | class Solution: 11 | # 96 ms, 87.9%. Time: O(N^2). Space: O(N) 12 | def reconstructQueue(self, people: List[List[int]]) -> List[List[int]]: 13 | people.sort(key=lambda x: (-x[0], x[1])) 14 | rv = [] 15 | for h, k in people: 16 | rv.insert(k, [h,k]) 17 | return rv 18 | # @lc code=end 19 | 20 | -------------------------------------------------------------------------------- /src/454.4-sum-ii.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=454 lang=python3 3 | # 4 | # [454] 4Sum II 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: Array 9 | class Solution: 10 | def fourSumCount(self, A: List[int], B: List[int], C: List[int], D: List[int]) -> int: 11 | ans = 0 12 | seenAB = collections.defaultdict(int) 13 | for a in A: 14 | for b in B: 15 | seenAB[a + b] += 1 16 | for c in C: 17 | for d in D: 18 | ans += seenAB[-c-d] 19 | return ans 20 | # @lc code=end 21 | 22 | -------------------------------------------------------------------------------- /src/633.sum-of-square-numbers.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=633 lang=python3 3 | # 4 | # [633] Sum of Square Numbers 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: Math, Two Pointers, Binary Search 9 | 10 | 11 | class Solution: 12 | # Time: O(logN). Space: O(logN) 13 | def judgeSquareSum(self, c: int) -> bool: 14 | squares = set(n*n for n in range(int(c**0.5) + 1)) 15 | 16 | for square in squares: 17 | if square <= c and (c - square) in squares: 18 | return True 19 | return False 20 | # @lc code=end 21 | -------------------------------------------------------------------------------- /src/3083.existence-of-a-substring-in-a-string-and-its-reverse.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=3083 lang=python3 3 | # 4 | # [3083] Existence of a Substring in a String and Its Reverse 5 | # 6 | 7 | 8 | # @lc code=start 9 | # TAGS: Hash Table, String 10 | class Solution: 11 | def isSubstringPresent(self, s: str) -> bool: 12 | seen = set() 13 | for c1, c2 in zip(s, s[1:]): 14 | if c1 == c2 or (c1, c2) in seen: 15 | return True 16 | seen.add((c2, c1)) 17 | return False 18 | 19 | 20 | # @lc code=end 21 | -------------------------------------------------------------------------------- /src/343.integer-break.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=343 lang=python3 3 | # 4 | # [343] Integer Break 5 | # 6 | 7 | 8 | # @lc code=start 9 | # TAGS: Math, Dynamic Programming 10 | class Solution: 11 | # 28 ms, 95.47% 12 | # Time: O(N). Space: O(1) 13 | def integerBreak(self, n: int) -> int: 14 | """Just Math""" 15 | ans = 0 16 | for k in range(2, n + 1): 17 | d, left_over = divmod(n, k) 18 | ans = max(ans, d ** (k - left_over) * (d + 1) ** left_over) 19 | return ans 20 | 21 | 22 | # @lc code=end 23 | -------------------------------------------------------------------------------- /src/476.number-complement.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=476 lang=python3 3 | # 4 | # [476] Number Complement 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: Bit Manipulation 9 | # REVIEWME: 10 | class Solution: 11 | # 28 ms, 61%. O(N) 12 | def findComplement(self, num: int) -> int: 13 | return int("".join(["1" if b == "0" else "0" for b in bin(num)[2:]]), 2) 14 | 15 | # 28 ms, 61%. Bitwise op 16 | def findComplement(self, num: int) -> int: 17 | temp = 1 << num.bit_length() 18 | return (temp - 1) ^ num 19 | # @lc code=end 20 | 21 | -------------------------------------------------------------------------------- /src/724.find-pivot-index.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=724 lang=python3 3 | # 4 | # [724] Find Pivot Index 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: Array, Prefix Sum 9 | from typing import List 10 | 11 | 12 | class Solution: 13 | # 176 ms, 61% 14 | def pivotIndex(self, nums: List[int]) -> int: 15 | left, right = 0, sum(nums) 16 | for i, num in enumerate(nums): 17 | right -= num 18 | if left == right: 19 | return i 20 | left += num 21 | return -1 22 | 23 | 24 | # @lc code=end 25 | -------------------------------------------------------------------------------- /src/1451.rearrange-words-in-a-sentence.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=1451 lang=python3 3 | # 4 | # [1451] Rearrange Words in a Sentence 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: String, Sort 9 | class Solution: 10 | """ 11 | 56 ms, 75.95%. 12 | Note that capitalize() will make the first character upper case and the rest lower case 13 | Time: O(N * logN) because of sorting 14 | Space: O(N) 15 | """ 16 | def arrangeWords(self, text: str) -> str: 17 | return " ".join(sorted(text.split(), key=len)).capitalize() 18 | # @lc code=end 19 | 20 | -------------------------------------------------------------------------------- /src/1550.three-consecutive-odds.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=1550 lang=python3 3 | # 4 | # [1550] Three Consecutive Odds 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: Array 9 | class Solution: 10 | # 52 ms, 58.75 %. Time: O(N). Space: O(1) 11 | def threeConsecutiveOdds(self, arr: List[int]) -> bool: 12 | cnt = 0 13 | for n in arr: 14 | if n % 2: 15 | cnt += 1 16 | else: 17 | cnt = 0 18 | if cnt == 3: return True 19 | return False 20 | 21 | # @lc code=end 22 | 23 | -------------------------------------------------------------------------------- /src/2139.minimum-moves-to-reach-target-score.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=2139 lang=python3 3 | # 4 | # [2139] Minimum Moves to Reach Target Score 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: Math, Greedy 9 | class Solution: 10 | # 48 ms, 100%. Time: O(logN). Space: O(1). Greedy 11 | def minMoves(self, target: int, maxDoubles: int) -> int: 12 | cnt = 0 13 | while target > 1 and maxDoubles: 14 | cnt += 1 + target % 2 15 | target //= 2 16 | maxDoubles -= 1 17 | return cnt + (target - 1) 18 | # @lc code=end 19 | -------------------------------------------------------------------------------- /src/402.remove-k-digits.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=402 lang=python3 3 | # 4 | # [402] Remove K Digits 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: Greedy 9 | # REVIEWME: 10 | class Solution: 11 | # 32 ms, 90.71%. 12 | def removeKdigits(self, num: str, k: int) -> str: 13 | rv = [] 14 | for n in num: 15 | while rv and rv[-1] > n and k: 16 | rv.pop() 17 | k -= 1 18 | rv.append(n) 19 | if k: rv = rv[:-k] 20 | return str(int("".join(rv))) if rv else "0" 21 | # @lc code=end 22 | 23 | -------------------------------------------------------------------------------- /src/1507.reformat-date.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=1507 lang=python3 3 | # 4 | # [1507] Reformat Date 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: String 9 | class Solution: 10 | # 44 ms, 42.11%. Time and Space: O(1) 11 | def reformatDate(self, date: str) -> str: 12 | Month = {"Jan":1, "Feb":2, "Mar":3, "Apr":4, "May":5, "Jun":6, "Jul":7, "Aug":8, "Sep":9, "Oct":10, "Nov":11, "Dec":12} 13 | dd, mm, yy = date.split() 14 | dd = dd[:-2] 15 | mm = Month[mm] 16 | return f"{yy}-{mm:02}-{dd:0>2}" 17 | 18 | # @lc code=end 19 | 20 | -------------------------------------------------------------------------------- /src/1614.maximum-nesting-depth-of-the-parentheses.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=1614 lang=python3 3 | # 4 | # [1614] Maximum Nesting Depth of the Parentheses 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: String 9 | class Solution: 10 | # 24 ms, 94.84%. Time: O(N). Space: O(1) 11 | def maxDepth(self, s: str) -> int: 12 | op = ans = 0 13 | for c in s: 14 | if c == '(': 15 | op += 1 16 | elif c == ')': 17 | op -= 1 18 | ans = max(ans, op) 19 | return ans 20 | # @lc code=end 21 | 22 | -------------------------------------------------------------------------------- /src/1848.minimum-distance-to-the-target-element.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=1848 lang=python3 3 | # 4 | # [1848] Minimum Distance to the Target Element 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: Array 9 | 10 | 11 | class Solution: 12 | # 52 ms, 86.17%. Time: O(N). Space: O(1) 13 | def getMinDistance(self, nums: List[int], target: int, start: int) -> int: 14 | diff = float("inf") 15 | for i, num in enumerate(nums): 16 | if num == target: 17 | diff = min(diff, abs(i - start)) 18 | return diff 19 | # @lc code=end 20 | -------------------------------------------------------------------------------- /src/1844.replace-all-digits-with-characters.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=1844 lang=python3 3 | # 4 | # [1844] Replace All Digits with Characters 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: String 9 | 10 | 11 | class Solution: 12 | # 32 ms, 68.53%. Time and Space: O(N) 13 | def replaceDigits(self, s: str) -> str: 14 | ans = [] 15 | for i, c in enumerate(s): 16 | if i % 2: 17 | ans.append(chr(ord(s[i - 1]) + int(c))) 18 | else: 19 | ans.append(c) 20 | return "".join(ans) 21 | # @lc code=end 22 | -------------------------------------------------------------------------------- /src/12.integer-to-roman.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=12 lang=python3 3 | # 4 | # [12] Integer to Roman 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: Math, String 9 | class Solution: 10 | def intToRoman(self, num): 11 | return_str="" 12 | pair={1:"I",4:"IV",5:"V",9:"IX",10:"X",40:"XL",50:"L",90:"XC",100:"C",400:"CD",500:"D",900:"CM",1000:"M"} 13 | for key in [1000,900,500,400,100,90,50,40,10,9,5,4,1]: 14 | return_str += pair[key] * (num // key) 15 | num = num % key 16 | return return_str 17 | 18 | # @lc code=end 19 | 20 | -------------------------------------------------------------------------------- /src/2176.count-equal-and-divisible-pairs-in-an-array.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=2176 lang=python3 3 | # 4 | # [2176] Count Equal and Divisible Pairs in an Array 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: Array 9 | from typing import List 10 | 11 | 12 | class Solution: 13 | def countPairs(self, nums: List[int], k: int) -> int: 14 | cnt = 0 15 | for i in range(len(nums)): 16 | for j in range(i + 1, len(nums)): 17 | if nums[i] == nums[j] and (i * j) % k == 0: 18 | cnt += 1 19 | return cnt 20 | # @lc code=end 21 | -------------------------------------------------------------------------------- /src/80.remove-duplicates-from-sorted-array-ii.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=80 lang=python3 3 | # 4 | # [80] Remove Duplicates from Sorted Array II 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: Array, Two Pointers 9 | from typing import List 10 | 11 | 12 | class Solution: 13 | # 89 ms, 30.19%. Time: O(N). Space: O(1) 14 | def removeDuplicates(self, nums: List[int]) -> int: 15 | ptr = 0 16 | for n in nums: 17 | if ptr < 2 or n != nums[ptr - 2]: 18 | nums[ptr] = n 19 | ptr += 1 20 | return ptr 21 | # @lc code=end 22 | -------------------------------------------------------------------------------- /src/848.shifting-letters.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=848 lang=python3 3 | # 4 | # [848] Shifting Letters 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: Array, String 9 | 10 | 11 | class Solution: 12 | # 200 ms, 63.61%. Time and Space: O(N) 13 | def shiftingLetters(self, S: str, shifts: List[int]) -> str: 14 | ans = [] 15 | shift = 0 16 | for i in range(len(shifts)): 17 | shift = (shift + shifts[~i]) % 26 18 | ans.append(chr(97 + (ord(S[~i]) - 97 + shift) % 26)) 19 | return "".join(reversed(ans)) 20 | 21 | # @lc code=end 22 | -------------------------------------------------------------------------------- /src/1502.can-make-arithmetic-progression-from-sequence.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=1502 lang=python3 3 | # 4 | # [1502] Can Make Arithmetic Progression From Sequence 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: Array 9 | class Solution: 10 | # 44 ms, 78.95 %. Time O(NlogN), Space: O(1) 11 | def canMakeArithmeticProgression(self, arr: List[int]) -> bool: 12 | arr.sort() 13 | diff = arr[1] - arr[0] 14 | for v1, v2 in zip(arr[1:], arr[2:]): 15 | if v2 - v1 != diff: 16 | return False 17 | return True 18 | # @lc code=end 19 | 20 | -------------------------------------------------------------------------------- /src/2042.check-if-numbers-are-ascending-in-a-sentence.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=2042 lang=python3 3 | # 4 | # [2042] Check if Numbers Are Ascending in a Sentence 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: String 9 | 10 | 11 | class Solution: 12 | def areNumbersAscending(self, s: str) -> bool: 13 | prev = 0 14 | for w in s.split(): 15 | if not w.isdigit(): 16 | continue 17 | if int(w) > prev: 18 | prev = int(w) 19 | else: 20 | return False 21 | return True 22 | # @lc code=end 23 | -------------------------------------------------------------------------------- /src/252.py: -------------------------------------------------------------------------------- 1 | # TAGS: Premium, Sort 2 | class Solution: 3 | # 76 ms, 91% 4 | def canAttendMeetings(self, intervals: List[List[int]]) -> bool: 5 | intervals.sort(key=lambda interval: interval[0]) 6 | for (_, end0), (start1, _) in zip(intervals, intervals[1:]): 7 | if end0 > start1: 8 | return False 9 | return True 10 | 11 | # 76 ms, 91% 12 | def canAttendMeetings(self, intervals: List[List[int]]) -> bool: 13 | intervals.sort() 14 | return all(end <= start for (_, end), (start, _) in zip(intervals, intervals[1:])) -------------------------------------------------------------------------------- /src/1827.minimum-operations-to-make-the-array-increasing.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=1827 lang=python3 3 | # 4 | # [1827] Minimum Operations to Make the Array Increasing 5 | # 6 | 7 | # @lc code=start 8 | 9 | 10 | class Solution: 11 | # 136 ms, 100%. Time: O(N). Space: O(1) 12 | def minOperations(self, nums: List[int]) -> int: 13 | total = 0 14 | for i in range(1, len(nums)): 15 | if nums[i - 1] >= nums[i]: 16 | total += nums[i - 1] - nums[i] + 1 17 | nums[i] = nums[i - 1] + 1 18 | return total 19 | # @lc code=end 20 | -------------------------------------------------------------------------------- /src/2138.divide-a-string-into-groups-of-size-k.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=2138 lang=python3 3 | # 4 | # [2138] Divide a String Into Groups of Size k 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: String, Simulation 9 | from typing import List 10 | 11 | 12 | class Solution: 13 | # 52 ms, 32.9%. Time and Space: O(N) 14 | def divideString(self, s: str, k: int, fill: str) -> List[str]: 15 | ans = [] 16 | for i in range(0, len(s), k): 17 | ans.append(s[i: i + k]) 18 | 19 | ans[-1] += fill * (k - len(ans[-1])) 20 | return ans 21 | # @lc code=end 22 | -------------------------------------------------------------------------------- /src/165.compare-version-numbers.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=165 lang=python3 3 | # 4 | # [165] Compare Version Numbers 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: String 9 | 10 | class Solution: 11 | # 32 ms, 58.83 %. Time: O(N). Space: O(1) 12 | def compareVersion(self, version1: str, version2: str) -> int: 13 | for d1, d2 in zip_longest(version1.split('.'), version2.split('.'), fillvalue="0"): 14 | if int(d1) > int(d2): 15 | return 1 16 | elif int(d1) < int(d2): 17 | return -1 18 | return 0 19 | # @lc code=end 20 | 21 | -------------------------------------------------------------------------------- /src/1833.maximum-ice-cream-bars.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=1833 lang=python3 3 | # 4 | # [1833] Maximum Ice Cream Bars 5 | # 6 | 7 | # @lc code=start 8 | 9 | 10 | class Solution: 11 | """ 12 | Approach: Just sort the array 13 | """ 14 | # 968 ms, 100%. Time: O(NlogN) 15 | 16 | def maxIceCream(self, costs: List[int], coins: int) -> int: 17 | costs.sort() 18 | total = 0 19 | for i, cost in enumerate(costs): 20 | total += cost 21 | if total > coins: 22 | return i 23 | return len(costs) 24 | # @lc code=end 25 | -------------------------------------------------------------------------------- /src/789.escape-the-ghosts.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=789 lang=python3 3 | # 4 | # [789] Escape The Ghosts 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: Math 9 | 10 | 11 | class Solution: 12 | # 36 ms, 100.00%. Time: O(N). Space: O(1) 13 | def escapeGhosts(self, ghosts: List[List[int]], target: List[int]) -> bool: 14 | dist_2_target = abs(target[0]) + abs(target[1]) 15 | for x, y in ghosts: 16 | dist = abs(target[0] - x) + abs(target[1] - y) 17 | if dist <= dist_2_target: 18 | return False 19 | return True 20 | # @lc code=end 21 | -------------------------------------------------------------------------------- /src/1897.redistribute-characters-to-make-all-strings-equal.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=1897 lang=python3 3 | # 4 | # [1897] Redistribute Characters to Make All Strings Equal 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: Hash Table, String, Couting 9 | 10 | 11 | class Solution: 12 | # 80 ms, 39.51%. Time and Space: O(N) 13 | def makeEqual(self, words: List[str]) -> bool: 14 | counter = collections.Counter() 15 | for word in words: 16 | counter += collections.Counter(word) 17 | 18 | return all(f % len(words) == 0 for f in counter.values()) 19 | # @lc code=end 20 | -------------------------------------------------------------------------------- /src/456.132-pattern.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=456 lang=python3 3 | # 4 | # [456] 132 Pattern 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: Stack 9 | # REVIEWME: Stack, Hard 10 | class Solution: 11 | # 99.18%. Time and Space: O(N) 12 | def find132pattern(self, nums: List[int]) -> bool: 13 | stack = [] 14 | mid = float('-inf') 15 | for num in reversed(nums): 16 | if num < mid: return True 17 | while stack and stack[-1] < num: 18 | mid = stack.pop() 19 | stack.append(num) 20 | return False 21 | # @lc code=end 22 | 23 | -------------------------------------------------------------------------------- /src/342.power-of-four.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=342 lang=python3 3 | # 4 | # [342] Power of Four 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: Math, Bit Manipulation 9 | class Solution: 10 | # 56 ms, 10.52%. Time: O(logN). Space: O(1) 11 | def isPowerOfFour(self, num: int) -> bool: 12 | while num > 1: 13 | num /= 4 14 | return num == 1 15 | 16 | # There are several O(1) solutions in the articles 17 | # Time and Soace: O(1) 18 | def isPowerOfFour(self, num: int) -> bool: 19 | return num > 0 and math.log2(num) % 2 == 0 20 | 21 | # @lc code=end 22 | 23 | -------------------------------------------------------------------------------- /src/377.combination-sum-iv.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=377 lang=python3 3 | # 4 | # [377] Combination Sum IV 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: Dynamic Programming 9 | 10 | 11 | class Solution: 12 | # 48 ms, 28.27%. Time: O(T). Space: O(T) 13 | def combinationSum4(self, nums: List[int], target: int) -> int: 14 | @cache 15 | def dfs(sofar=0): 16 | if sofar > target: 17 | return 0 18 | if sofar == target: 19 | return 1 20 | return sum(dfs(sofar + n) for n in nums) 21 | return dfs() 22 | # @lc code=end 23 | -------------------------------------------------------------------------------- /src/1657.determine-if-two-strings-are-close.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=1657 lang=python3 3 | # 4 | # [1657] Determine if Two Strings Are Close 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: Greedy 9 | class Solution: 10 | # 144 ms, 64.42%. Time: O(N) Space: O(1) 11 | def closeStrings(self, word1: str, word2: str) -> bool: 12 | c1 = collections.Counter(word1) 13 | c2 = collections.Counter(word2) 14 | c1_val = collections.Counter(c1.values()) 15 | c2_val = collections.Counter(c2.values()) 16 | return c1_val == c2_val and set(word1) == set(word2) 17 | # @lc code=end 18 | 19 | -------------------------------------------------------------------------------- /src/2190.most-frequent-number-following-key-in-an-array.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=2190 lang=python3 3 | # 4 | # [2190] Most Frequent Number Following Key In an Array 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: Array, Hash Table, Counting 9 | from typing import List 10 | import collections 11 | 12 | 13 | class Solution: 14 | def mostFrequent(self, nums: List[int], key: int) -> int: 15 | cnt = collections.Counter() 16 | for v1, v2 in zip(nums, nums[1:]): 17 | if v1 == key: 18 | cnt[v2] += 1 19 | return max(cnt, key=lambda x: cnt.get(x)) 20 | # @lc code=end 21 | -------------------------------------------------------------------------------- /src/320.py: -------------------------------------------------------------------------------- 1 | # TAGS: Premium, Backtracking 2 | class Solution: 3 | # 144 ms, 92.57%. Time: O(N*2^N). Space: O(N) 4 | def generateAbbreviations(self, word: str) -> List[str]: 5 | self.ans = [] 6 | def dfs(word, sofar=''): 7 | if not word: 8 | self.ans.append(sofar) 9 | return 10 | for i in range(len(word) + 1): 11 | if i == 0: 12 | dfs(word[i+1:], sofar + word[i]) 13 | else: 14 | dfs(word[i+1:], sofar + str(i) + word[i:i+1]) 15 | dfs(word) 16 | return self.ans -------------------------------------------------------------------------------- /src/71.simplify-path.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=71 lang=python3 3 | # 4 | # [71] Simplify Path 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: String, Stack 9 | class Solution: 10 | # 28 ms, 89.90%. Time and Space: O(N) 11 | def simplifyPath(self, path: str) -> str: 12 | stack = [] 13 | for pos in path.split("/"): 14 | if not pos: continue 15 | if pos == ".": continue 16 | if pos == "..": 17 | if stack: stack.pop() 18 | else: 19 | stack.append(pos) 20 | return "/" + "/".join(stack) 21 | # @lc code=end 22 | 23 | -------------------------------------------------------------------------------- /src/1544.make-the-string-great.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=1544 lang=python3 3 | # 4 | # [1544] Make The String Great 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: String, Stack 9 | class Solution: 10 | # 32 ms, 90.71 %. Time and Space: O(N) 11 | def makeGood(self, s: str) -> str: 12 | stack = [] 13 | for c in s: 14 | if stack \ 15 | and c != stack[-1] \ 16 | and c.lower() == stack[-1].lower(): 17 | stack.pop() 18 | else: 19 | stack.append(c) 20 | return "".join(stack) 21 | 22 | # @lc code=end 23 | 24 | -------------------------------------------------------------------------------- /src/1828.queries-on-number-of-points-inside-a-circle.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=1828 lang=python3 3 | # 4 | # [1828] Queries on Number of Points Inside a Circle 5 | # 6 | 7 | # @lc code=start 8 | 9 | 10 | class Solution: 11 | # 2492 ms, 100%. Time: O(N*M). Space: O(M) 12 | def countPoints(self, points: List[List[int]], queries: List[List[int]]) -> List[int]: 13 | ans = [] 14 | for x, y, r in queries: 15 | cnt = sum((x - xp) ** 2 + (y - yp) ** 2 <= 16 | r ** 2 for xp, yp in points) 17 | ans.append(cnt) 18 | return ans 19 | # @lc code=end 20 | -------------------------------------------------------------------------------- /src/376.wiggle-subsequence.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=376 lang=python3 3 | # 4 | # [376] Wiggle Subsequence 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: Greedy, Dynamic Programming 9 | 10 | 11 | class Solution: 12 | # 32 ms, 83.94 %. Time: O(N). Space: O(1) 13 | def wiggleMaxLength(self, nums: List[int]) -> int: 14 | total = 1 15 | prev = None 16 | for n1, n2 in zip(nums, nums[1:]): 17 | if n1 == n2: 18 | continue 19 | cur = n1 < n2 20 | total += (prev != cur) 21 | prev = cur 22 | return total 23 | # @lc code=end 24 | -------------------------------------------------------------------------------- /testcases/838: -------------------------------------------------------------------------------- 1 | ".." 2 | "L." 3 | ".R" 4 | ".L" 5 | "R." 6 | ".L.R...LR..L.." 7 | "RR.L" 8 | "LLRLRR..RRL.L.RLRLRLLR.RRR..LLRR..R.L......RLL.L.R.R.LLLLLL..L.LL.R.RR.RRRL.LRR...LRRLL.LRRRRRLRRL.." 9 | "LLL....R.....R.R..LR.L.RR.R.RR..........LRLR.R..R.LR.R.R..L.R.LLR.RLR...LRLR.L....L..LR....L..L..R..L.LR........RLL..LRL.R.R.R...RRR.LL...LLLL.LR.RLL..L.LL...L.RL....R.L..R......R.L..L..L.RL.L.R.LR.RL.L...R..R.RR..R...R...LL...L.L.R......R...L.RR..R.L.LR.L.L.L....R.RR.R..RRL...LRL...L....R..LL........L...L...L..L...R....L..RR.RL.......R....L..L...RLL.........L.L....RRLRRRL...L......R..L....R...RL." 10 | "" 11 | "." 12 | "L" 13 | "R" -------------------------------------------------------------------------------- /src/739.daily-temperatures.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=739 lang=python3 3 | # 4 | # [739] Daily Temperatures 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: Hash Table, Stack 9 | 10 | 11 | class Solution: 12 | # 504 ms, 75%. Time and Space: O(N) 13 | def dailyTemperatures(self, T: List[int]) -> List[int]: 14 | results = [0] * len(T) 15 | stack = [] 16 | for i, t in enumerate(T): 17 | while stack and T[stack[-1]] < t: 18 | prev = stack.pop() 19 | results[prev] = i - prev 20 | stack.append(i) 21 | return results 22 | # @lc code=end 23 | -------------------------------------------------------------------------------- /src/946.validate-stack-sequences.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=946 lang=python3 3 | # 4 | # [946] Validate Stack Sequences 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: Stack 9 | class Solution: 10 | # 64 ms, 94.09%. Time and Space: O(N) 11 | def validateStackSequences(self, pushed: List[int], popped: List[int]) -> bool: 12 | ptr = 0 13 | stack = [] 14 | for val in pushed: 15 | stack.append(val) 16 | while stack and stack[-1] == popped[ptr]: 17 | ptr += 1 18 | stack.pop() 19 | return stack == [] 20 | 21 | # @lc code=end 22 | 23 | -------------------------------------------------------------------------------- /src/1716.calculate-money-in-leetcode-bank.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=1716 lang=python3 3 | # 4 | # [1716] Calculate Money in Leetcode Bank 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: Math, Greedy 9 | class Solution: 10 | # 28 ms, 93.9%. Time: O(N). Space: 11 | def totalMoney(self, n: int) -> int: 12 | one_week = 28 13 | d, c = divmod(n, 7) 14 | total = 0 15 | for week in range(d): 16 | total += one_week + week * 7 17 | if c: 18 | for i in range(1, c + 1): 19 | total += i + d 20 | return total 21 | 22 | # @lc code=end 23 | 24 | -------------------------------------------------------------------------------- /src/357.count-numbers-with-unique-digits.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=357 lang=python3 3 | # 4 | # [357] Count Numbers with Unique Digits 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: Math, Dynamic Programming, Backtracking 9 | 10 | 11 | class Solution: 12 | # 24 ms, 93.40%. Time and Space: O(1) 13 | def countNumbersWithUniqueDigits(self, n: int) -> int: 14 | def f(n, step=0): 15 | if n == 0: 16 | return 1 17 | if n == 1: 18 | return 9 19 | return f(n - 1, step + 1) * (9 - step) 20 | return sum(f(i) for i in range(n + 1)) 21 | # @lc code=end 22 | -------------------------------------------------------------------------------- /src/1003.check-if-word-is-valid-after-substitutions.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=1003 lang=python3 3 | # 4 | # [1003] Check If Word Is Valid After Substitutions 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: String, Stack 9 | class Solution: 10 | # 88 ms, 37.74%. Time: O(N). Space: O(N) 11 | def isValid(self, s: str) -> bool: 12 | stack = [] 13 | for c in s: 14 | stack.append(c) 15 | if c == "c" and len(stack) >= 3: 16 | if stack[-3:] == ['a', 'b', 'c']: 17 | stack.pop(); stack.pop(); stack.pop() 18 | return len(stack) == 0 19 | # @lc code=end 20 | 21 | -------------------------------------------------------------------------------- /src/1592.rearrange-spaces-between-words.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=1592 lang=python3 3 | # 4 | # [1592] Rearrange Spaces Between Words 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: String 9 | class Solution: 10 | # 24 ms, 94.72%. Time: O(N). Space: O(N) 11 | def reorderSpaces(self, text: str) -> str: 12 | spaces = text.count(' ') 13 | words = text.split() 14 | if len(words) == 1: 15 | return words[0] + ' ' * spaces 16 | div, rem = divmod(spaces, len(words) - 1) 17 | delimiter = ' ' * div 18 | return delimiter.join(words) + ' ' * rem 19 | 20 | # @lc code=end 21 | 22 | -------------------------------------------------------------------------------- /src/1646.get-maximum-in-generated-array.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=1646 lang=python3 3 | # 4 | # [1646] Get Maximum in Generated Array 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: Array 9 | class Solution: 10 | # 28 ms, 86.65%. Time and Space: O(N) 11 | def getMaximumGenerated(self, n: int) -> int: 12 | if n <= 1: return n 13 | nums = [0, 1] 14 | for i in range(2, n + 1): 15 | if i % 2 == 0: 16 | nums.append(nums[i // 2]) 17 | else: 18 | nums.append(nums[i // 2] + nums[i // 2 + 1]) 19 | return max(nums) 20 | 21 | # @lc code=end 22 | 23 | -------------------------------------------------------------------------------- /src/2085.count-common-words-with-one-occurrence.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=2085 lang=python3 3 | # 4 | # [2085] Count Common Words With One Occurrence 5 | # 6 | 7 | # @lc code=start 8 | import collections 9 | from typing import List 10 | 11 | 12 | class Solution: 13 | # 105 ms, 30.70%. Time and Space: O(N) 14 | def countWords(self, words1: List[str], words2: List[str]) -> int: 15 | c1 = collections.Counter(words1) 16 | c2 = collections.Counter(words2) 17 | ans = 0 18 | for k in c1: 19 | if c1[k] == c2[k] == 1: 20 | ans += 1 21 | return ans 22 | # @lc code=end 23 | -------------------------------------------------------------------------------- /src/78.subsets.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=78 lang=python3 3 | # 4 | # [78] Subsets 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: Backtracking, Bit Manipulation 9 | # REVIEWME: There are other solutions with similar complexity 10 | class Solution: 11 | # 36 ms, 70%. Time and Space: O(N 2^N) 12 | def subsets(self, nums: List[int]) -> List[List[int]]: 13 | ans = [] 14 | def recurse(nums, sofar = []): 15 | ans.append(sofar) 16 | for i, num in enumerate(nums): 17 | recurse(nums[i + 1 :], sofar + [num]) 18 | recurse(nums) 19 | return ans 20 | # @lc code=end 21 | 22 | -------------------------------------------------------------------------------- /src/853.car-fleet.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=853 lang=python3 3 | # 4 | # [853] Car Fleet 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: Sort 9 | class Solution: 10 | # 168 ms, 78.59%. Time: O(NlogN). Space: O(N) because of Sort 11 | def carFleet(self, target: int, position: List[int], speed: List[int]) -> int: 12 | time = [(target - pos) / sp for pos, sp in zip(position, speed)] 13 | stack = [] 14 | for _, t in sorted(zip(position, time)): 15 | while stack and stack[-1] <= t: 16 | stack.pop() 17 | stack.append(t) 18 | return len(stack) 19 | # @lc code=end 20 | 21 | -------------------------------------------------------------------------------- /src/916.word-subsets.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=916 lang=python3 3 | # 4 | # [916] Word Subsets 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: String 9 | 10 | 11 | class Solution: 12 | # 1060 ms, 41.94%. Time: O(A_t + B_t). Space: O(L). L = len(A) + len(B) 13 | # A_t is total amount of information in A 14 | # B_t is total amount of information in B 15 | def wordSubsets(self, A: List[str], B: List[str]) -> List[str]: 16 | Bc = collections.Counter() 17 | for b in B: 18 | Bc |= collections.Counter(b) 19 | return [a for a in A if not Bc - collections.Counter(a)] 20 | 21 | # @lc code=end 22 | -------------------------------------------------------------------------------- /testcases/316: -------------------------------------------------------------------------------- 1 | ""bcabc" 2 | "cbacdcbc" 3 | "a" 4 | "iohepyqhcxlkmyfnlmwumiuqhakknriggylrmvrxejocbzhdwespwsnqiflkoperqthyvudyxvykwldvyijeskvlvydsfdhxpiha" 5 | "nhwibsbezkpizzbvltdxttqmefmtnrusyemcaobmkplyyyvxlduzwecwxbgyakbprnxspcvneyrzyzxqwqnzsqlhdjaugiqsmctzcigndzhghwszwkrgvauqciepahpywctgszvasnfjouxaaihbwymhreamjshukpwrxrafbrmygcrhcbgqwyjkcqwumtrmunjwhcgblmglsdiyygwszalmzydegzzjxkzsxuwpozgnuijlhgbtgtrcsbagshgqpicfsqvnixtxdcjhdgxyvbcxpnktvxhyvepemzlatnztaifwclgdruxyncfzimhhhbhwvgclaxyjgpnbfeacczpmnqcyjgagzupjyamhdjvlgxgnahoctoeltomqoawaemrwoobopxqbaqtxdznxlqmajnzdtcktxqbgydlulpbvtwbizajxaxhfiagxezajohdqzelunsigrmaxifoojdjrvlniszcdpfsiefygxzcrwnxevetl"" -------------------------------------------------------------------------------- /src/1647.minimum-deletions-to-make-character-frequencies-unique.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=1647 lang=python3 3 | # 4 | # [1647] Minimum Deletions to Make Character Frequencies Unique 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: Greedy, Sort 9 | class Solution: 10 | # 104 ms, 97.48% 11 | def minDeletions(self, s: str) -> int: 12 | counter = collections.Counter(s) 13 | used = set(); ans = 0 14 | for _, freq in counter.items(): 15 | while freq > 0 and freq in used: 16 | freq -= 1 17 | ans += 1 18 | used.add(freq) 19 | return ans 20 | # @lc code=end 21 | 22 | -------------------------------------------------------------------------------- /src/2062.count-vowel-substrings-of-a-string.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=2062 lang=python3 3 | # 4 | # [2062] Count Vowel Substrings of a String 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: Hash Table, String 9 | 10 | class Solution: 11 | # Time: O(N^3). Space: O(N) 12 | def countVowelSubstrings(self, word: str) -> int: 13 | def is_valid(sub): 14 | return set(sub) == set("aeoui") 15 | 16 | cnt = 0 17 | for i in range(len(word)): 18 | for j in range(i + 1, len(word) + 1): 19 | sub = word[i:j] 20 | cnt += is_valid(sub) 21 | return cnt 22 | # @lc code=end 23 | -------------------------------------------------------------------------------- /src/2191.sort-the-jumbled-numbers.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=2191 lang=python3 3 | # 4 | # [2191] Sort the Jumbled Numbers 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: Array, Sorting 9 | from typing import List 10 | 11 | 12 | class Solution: 13 | def sortJumbled(self, mapping: List[int], nums: List[int]) -> List[int]: 14 | mapping = {str(i): str(v) for i, v in enumerate(mapping)} 15 | 16 | def custom_key(num): 17 | new = [] 18 | for d in str(num): 19 | new.append(mapping[d]) 20 | return int("".join(new)) 21 | return sorted(nums, key=custom_key) 22 | # @lc code=end 23 | -------------------------------------------------------------------------------- /testcases/729: -------------------------------------------------------------------------------- 1 | ["MyCalendar","book","book","book","book","book","book","book","book","book"] 2 | [[],[10,20],[15,25],[20,30],[30,31],[10,11],[10,30],[0,1],[0,10],[1,10]] 3 | ["MyCalendar","book","book","book","book","book","book","book","book","book","book"] 4 | [[],[47,50],[33,41],[39,45],[33,42],[25,32],[26,35],[19,25],[3,8],[8,13],[18,27]] 5 | ["MyCalendar","book","book","book","book","book","book","book","book","book","book","book","book","book","book","book","book","book","book","book","book"] 6 | [[],[97,100],[33,51],[89,100],[83,100],[75,92],[76,95],[19,30],[53,63],[8,23],[18,37],[87,100],[83,100],[54,67],[35,48],[58,75],[70,89],[13,32],[44,63],[51,62],[2,15]] -------------------------------------------------------------------------------- /src/1015.smallest-integer-divisible-by-k.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=1015 lang=python3 3 | # 4 | # [1015] Smallest Integer Divisible by K 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: Math 9 | # REVIEWME: Math 10 | class Solution: 11 | # 48 ms, 76.4%. Time: O(K). Space: O(K) 12 | def smallestRepunitDivByK(self, K: int) -> int: 13 | visited = set() 14 | cnt = rem = 0 15 | while 1: 16 | cnt += 1 17 | rem = (rem * 10 + 1) % K 18 | if rem in visited: return -1 19 | visited.add(rem) 20 | if rem == 0: return cnt 21 | 22 | # @lc code=end 23 | 24 | -------------------------------------------------------------------------------- /src/1464.maximum-product-of-two-elements-in-an-array.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=1464 lang=python3 3 | # 4 | # [1464] Maximum Product of Two Elements in an Array 5 | # 6 | 7 | # @lc code=start 8 | class Solution: 9 | # 52 ms, 85.11%. Time: O(N). Space: O(1) 10 | def maxProduct(self, nums: List[int]) -> int: 11 | lo, hi = nums[:2] 12 | if lo > hi: 13 | lo, hi = hi, lo 14 | for i in range(2, len(nums)): 15 | if nums[i] > lo: 16 | lo = nums[i] 17 | if lo > hi: 18 | lo, hi = hi, lo 19 | return (lo - 1) * (hi - 1) 20 | # @lc code=end 21 | 22 | -------------------------------------------------------------------------------- /src/1496.path-crossing.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=1496 lang=python3 3 | # 4 | # [1496] Path Crossing 5 | # 6 | 7 | # @lc code=start 8 | class Solution: 9 | # 24 ms, 95.26%. Time and Space: O(N) 10 | def isPathCrossing(self, path: str) -> bool: 11 | visited = set() 12 | x = y = 0 13 | for di in path: 14 | visited.add((x, y)) 15 | if di == "N": y += 1 16 | elif di == "S": y -= 1 17 | elif di == "E": x += 1 18 | elif di == "W": x -= 1 19 | if (x, y) in visited: 20 | return True 21 | return False 22 | 23 | # @lc code=end 24 | 25 | -------------------------------------------------------------------------------- /src/791.custom-sort-string.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=791 lang=python3 3 | # 4 | # [791] Custom Sort String 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: String 9 | import string 10 | import collections 11 | 12 | 13 | class Solution: 14 | # 28 ms, 80.98%. Time and Space: O(N). N = len(T) 15 | def customSortString(self, S: str, T: str) -> str: 16 | counter = collections.Counter(T) 17 | ans = [] 18 | for c in S: 19 | ans.append(c * counter[c]) 20 | 21 | for c in set(string.ascii_lowercase) - set(S): 22 | ans.append(c * counter[c]) 23 | 24 | return ''.join(ans) 25 | # @lc code=end 26 | -------------------------------------------------------------------------------- /src/1289.minimum-falling-path-sum-ii.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=1289 lang=python3 3 | # 4 | # [1289] Minimum Falling Path Sum II 5 | # 6 | 7 | 8 | # @lc code=start 9 | # TAGS: Array, Dynamic Programming, Matrix 10 | class Solution: 11 | # Time: O(R*C*C). Space: O(1) 12 | def minFallingPathSum(self, grid: List[List[int]]) -> int: 13 | R, C = len(grid), len(grid[0]) 14 | for i in range(1, R): 15 | for j in range(C): 16 | grid[i][j] += min( 17 | [grid[i - 1][k] for k in range(C) if k != j], default=0 18 | ) 19 | return min(grid[-1]) 20 | 21 | 22 | # @lc code=end 23 | -------------------------------------------------------------------------------- /src/1470.shuffle-the-array.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=1470 lang=python3 3 | # 4 | # [1470] Shuffle the Array 5 | # 6 | 7 | # @lc code=start 8 | class Solution: 9 | # 80 ms, 50.80%. Time: O(N). Space: O(N). Could be better using Bit manipulation to make it O(1) Space 10 | # More here: https://leetcode.com/problems/shuffle-the-array/discuss/675956/In-Place-O(n)-Time-O(1)-Space-With-Explanation-and-Analysis 11 | def shuffle(self, nums: List[int], n: int) -> List[int]: 12 | ans = [] 13 | for i in range(n): 14 | ans.append(nums[i]) 15 | ans.append(nums[i + n]) 16 | return ans 17 | # @lc code=end 18 | 19 | -------------------------------------------------------------------------------- /src/3005.count-elements-with-maximum-frequency.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=3005 lang=python3 3 | # 4 | # [3005] Count Elements With Maximum Frequency 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: Array, Hash Table, Counting 9 | from collections import defaultdict 10 | 11 | class Solution: 12 | # Time and Space: O(N) 13 | def maxFrequencyElements(self, nums: List[int]) -> int: 14 | max_cnt = 0 15 | counter = defaultdict(int) 16 | for num in nums: 17 | counter[num] += 1 18 | max_cnt = max(max_cnt, counter[num]) 19 | return sum(v for k, v in counter.items() if v == max_cnt) 20 | # @lc code=end 21 | 22 | -------------------------------------------------------------------------------- /src/881.boats-to-save-people.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=881 lang=python3 3 | # 4 | # [881] Boats to Save People 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: Two Pointers, Greedy 9 | class Solution: 10 | # 448 ms, 74.18%. Time : O(NlogN). Space: O(N) because of sort 11 | def numRescueBoats(self, people: List[int], limit: int) -> int: 12 | people.sort() 13 | left = 0; right = len(people) - 1 14 | cnt = 0 15 | while left <= right: 16 | if people[left] + people[right] <= limit: 17 | left += 1 18 | right -= 1 19 | cnt += 1 20 | return cnt 21 | # @lc code=end 22 | 23 | -------------------------------------------------------------------------------- /src/976.largest-perimeter-triangle.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=976 lang=python3 3 | # 4 | # [976] Largest Perimeter Triangle 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: Math, Sort 9 | class Solution: 10 | # 180 ms, 84.15%. Time: O(NlogN). Space: O(N) 11 | def largestPerimeter(self, A: List[int]) -> int: 12 | def valid(a, b, c): 13 | return a + b > c and a + c > b and b + c > a 14 | 15 | A.sort(reverse=True) 16 | for a, b, c in zip(A, A[1:], A[2:]): 17 | if valid(a, b, c): 18 | return a + b + c 19 | return 0 20 | 21 | # @lc code=end 22 | 23 | -------------------------------------------------------------------------------- /src/830.positions-of-large-groups.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=830 lang=python3 3 | # 4 | # [830] Positions of Large Groups 5 | # 6 | 7 | # @lc code=start 8 | class Solution: 9 | # 36 ms, 75.04 %. O(N). 10 | def largeGroupPositions(self, S: str) -> List[List[int]]: 11 | rv = [] 12 | cnt = 1 13 | S += "." 14 | for i in range(len(S) - 1): 15 | a, b = S[i], S[i + 1] 16 | if a == b: 17 | cnt += 1 18 | else: 19 | if cnt >= 3: 20 | rv.append([i - cnt + 1, i]) 21 | cnt = 1 22 | return rv 23 | 24 | # @lc code=end 25 | 26 | -------------------------------------------------------------------------------- /src/1624.largest-substring-between-two-equal-characters.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=1624 lang=python3 3 | # 4 | # [1624] Largest Substring Between Two Equal Characters 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: String 9 | class Solution: 10 | # 16 ms, 100%. Time: O(N). Space: O(1) 11 | def maxLengthBetweenEqualCharacters(self, s: str) -> int: 12 | ans = -1 13 | indices = {} 14 | for i, c in enumerate(s): 15 | if c not in indices: 16 | indices[c] = [i, i] 17 | indices[c][1] = i 18 | ans = max(ans, indices[c][1] - indices[c][0] - 1) 19 | return ans 20 | # @lc code=end 21 | 22 | -------------------------------------------------------------------------------- /src/941.valid-mountain-array.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=941 lang=python3 3 | # 4 | # [941] Valid Mountain Array 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: Array 9 | class Solution: 10 | # 188 ms, 93.89%. Time: O(N). Space: O(1) 11 | def validMountainArray(self, arr: List[int]) -> bool: 12 | if len(arr) == 1: return False 13 | if arr[0] >= arr[1] or arr[-1] >= arr[-2]: return False 14 | up = True 15 | for v1, v2 in zip(arr, arr[1:]): 16 | if up and v1 > v2: 17 | up = False 18 | elif not up and v1 <= v2: 19 | return False 20 | return True 21 | # @lc code=end 22 | 23 | -------------------------------------------------------------------------------- /src/1218.longest-arithmetic-subsequence-of-given-difference.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=1218 lang=python3 3 | # 4 | # [1218] Longest Arithmetic Subsequence of Given Difference 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: Hash Table, Math, Dynamic Programming. 9 | 10 | 11 | class Solution: 12 | """ 13 | DP with Hash Table. 14 | """ 15 | # 532 ms, 77.89%. Time and Space: O(N) 16 | 17 | def longestSubsequence(self, arr: List[int], diff: int) -> int: 18 | visited = collections.defaultdict(int) 19 | for num in arr: 20 | visited[num] = visited[num - diff] + 1 21 | return max(visited.values()) 22 | 23 | # @lc code=end 24 | -------------------------------------------------------------------------------- /src/1679.max-number-of-k-sum-pairs.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=1679 lang=python3 3 | # 4 | # [1679] Max Number of K-Sum Pairs 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: Hash Table 9 | class Solution: 10 | # 632 ms, 95.03%. Time and Space: O(N) 11 | def maxOperations(self, nums: List[int], k: int) -> int: 12 | counter = collections.Counter(nums) 13 | ans = 0 14 | for val, freq in counter.items(): 15 | key = k - val 16 | if key in counter: 17 | ans += min(freq, counter[key]) 18 | return ans // 2 # this will deal with duplicates and when key == val 19 | 20 | # @lc code=end 21 | 22 | -------------------------------------------------------------------------------- /src/1752.check-if-array-is-sorted-and-rotated.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=1752 lang=python3 3 | # 4 | # [1752] Check if Array Is Sorted and Rotated 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: Array 9 | 10 | 11 | class Solution: 12 | # 36 ms, 51.30 %. Time: O(N). Space: O(1) 13 | def check(self, nums: List[int]) -> bool: 14 | cnt = sum(n1 > n2 for n1, n2 in zip(nums, nums[1:])) 15 | if nums[0] >= nums[-1]: 16 | return cnt <= 1 17 | return cnt == 0 18 | 19 | # Cleaner solution 20 | def check(self, nums: List[int]) -> bool: 21 | return sum(nums[i] < nums[i-1] for i in range(len(nums))) <= 1 22 | # @lc code=end 23 | -------------------------------------------------------------------------------- /src/702.py: -------------------------------------------------------------------------------- 1 | # TAGS: Premium, Binary Search 2 | # """ 3 | # This is ArrayReader's API interface. 4 | # You should not implement it, or speculate about its implementation 5 | # """ 6 | #class ArrayReader: 7 | # def get(self, index: int) -> int: 8 | 9 | class Solution: 10 | # Time: O(logN). Space: O(1) 11 | def search(self, reader, target): 12 | 13 | lo, hi = 0, 10000 14 | while lo < hi: 15 | mid = (lo + hi) // 2 16 | if reader.get(mid) >= target: 17 | hi = mid 18 | else: 19 | lo = mid + 1 20 | if reader.get(lo) == target: 21 | return lo 22 | return -1 -------------------------------------------------------------------------------- /src/1508.range-sum-of-sorted-subarray-sums.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=1508 lang=python3 3 | # 4 | # [1508] Range Sum of Sorted Subarray Sums 5 | # 6 | 7 | # @lc code=start 8 | class Solution: 9 | # 476 ms, 43.22 %. Time: O(N^2logN). Space: O(N^2). There is better solution in the discussion 10 | def rangeSum(self, nums: List[int], n: int, left: int, right: int) -> int: 11 | arr = [] 12 | for i in range(n): 13 | cumm = 0 14 | for j in range(i, n): 15 | cumm += nums[j] 16 | arr.append(cumm) 17 | arr.sort() 18 | return sum(arr[left - 1 : right]) % (10**9 + 7) 19 | # @lc code=end 20 | 21 | -------------------------------------------------------------------------------- /src/3107.minimum-operations-to-make-median-of-array-equal-to-k.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=3107 lang=python3 3 | # 4 | # [3107] Minimum Operations to Make Median of Array Equal to K 5 | # 6 | 7 | 8 | # @lc code=start 9 | import bisect 10 | 11 | 12 | class Solution: 13 | # Time: O(NlogN). Space: O(N) 14 | def minOperationsToMakeMedianK(self, nums: List[int], k: int) -> int: 15 | nums.sort() 16 | mid = len(nums) // 2 17 | index = bisect.bisect(nums, k) 18 | if index <= mid: 19 | return sum(num - k for num in nums[index : mid + 1]) 20 | return sum(k - num for num in nums[mid:index]) 21 | 22 | 23 | # @lc code=end 24 | -------------------------------------------------------------------------------- /src/1962.remove-stones-to-minimize-the-total.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=1962 lang=python3 3 | # 4 | # [1962] Remove Stones to Minimize the Total 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: Array, Heap 9 | 10 | 11 | class Solution: 12 | # 1748 ms, 80.97%. Time: O(KlogN). Space: O(N) 13 | def minStoneSum(self, piles: List[int], k: int) -> int: 14 | heap = [-p for p in piles] 15 | heapq.heapify(heap) 16 | for _ in range(k): 17 | biggest_pile = heapq.heappop(heap) 18 | # floor division of a negative number 19 | heapq.heappush(heap, biggest_pile // 2) 20 | return - sum(heap) 21 | 22 | 23 | # @lc code=end 24 | -------------------------------------------------------------------------------- /src/2824.count-pairs-whose-sum-is-less-than-target.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=2824 lang=python3 3 | # 4 | # [2824] Count Pairs Whose Sum is Less than Target 5 | # 6 | 7 | 8 | # @lc code=start 9 | # TAGS: Array, Two Pointers, Binary Search, Sorting 10 | class Solution: 11 | # Time: O(NlogN). Space: O(1) 12 | def countPairs(self, nums: List[int], target: int) -> int: 13 | nums.sort() 14 | left = ans = 0 15 | for right in reversed(range(len(nums))): 16 | while left < right and nums[left] + nums[right] < target: 17 | left += 1 18 | ans += min(left, right) 19 | return ans 20 | 21 | 22 | # @lc code=end 23 | -------------------------------------------------------------------------------- /src/13.roman-to-integer.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=13 lang=python3 3 | # 4 | # [13] Roman to Integer 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: Math, String 9 | class Solution: 10 | def romanToInt(self, s: str) -> int: 11 | return_val = 0 12 | pair={"I":1,"V":5,"X":10,"L":50,"C":100,"D":500,"M":1000} 13 | for i in range(len(s)): 14 | return_val += pair[s[i]] 15 | if i== 0: continue 16 | # Subtract the amount already added (times 2) if next number is bigger 17 | if pair[s[i]] > pair[s[i - 1]]: 18 | return_val -= pair[s[i - 1]] * 2 19 | return return_val 20 | 21 | # @lc code=end 22 | 23 | -------------------------------------------------------------------------------- /src/1306.jump-game-iii.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=1306 lang=python3 3 | # 4 | # [1306] Jump Game III 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: BFS, DFS, Recursion 9 | class Solution: 10 | # 208 ms, 91.23%. Time and Space: O(N) 11 | def canReach(self, arr: List[int], start: int) -> bool: 12 | stack = [start] 13 | visited = set() 14 | while stack: 15 | i = stack.pop() 16 | if i < 0 or i >= len(arr) or i in visited: continue 17 | visited.add(i) 18 | if arr[i] == 0: return True 19 | stack.extend([i - arr[i], i + arr[i]]) 20 | return False 21 | 22 | 23 | # @lc code=end 24 | 25 | -------------------------------------------------------------------------------- /src/1512.number-of-good-pairs.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=1512 lang=python3 3 | # 4 | # [1512] Number of Good Pairs 5 | # 6 | 7 | # @lc code=start 8 | import collections 9 | class Solution: 10 | # 32 ms, 84.24%. Time and Space: O(N) 11 | def numIdenticalPairs(self, nums: List[int]) -> int: 12 | freq = collections.defaultdict(int) 13 | cnt = 0 14 | for num in nums: 15 | cnt += freq[num] 16 | freq[num] += 1 17 | return cnt 18 | 19 | # Similar solution but cleaner. 20 | def numIdenticalPairs(self, A): 21 | return sum(k * (k - 1) / 2 for k in collections.Counter(A).values()) 22 | 23 | # @lc code=end 24 | 25 | -------------------------------------------------------------------------------- /src/1759.count-number-of-homogenous-substrings.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=1759 lang=python3 3 | # 4 | # [1759] Count Number of Homogenous Substrings 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: String, Greedy 9 | 10 | 11 | class Solution: 12 | # 168 ms, 68.75%. Time: O(N). Space: O(1) 13 | def countHomogenous(self, s: str) -> int: 14 | def cal(n): 15 | return n * (n + 1) // 2 16 | MOD = 10**9 + 7 17 | total = ptr = 0 18 | for i in range(len(s) + 1): 19 | if i == len(s) or s[i] != s[ptr]: 20 | total = (total + cal(i - ptr)) % MOD 21 | ptr = i 22 | return total 23 | 24 | # @lc code=end 25 | -------------------------------------------------------------------------------- /src/1974.minimum-time-to-type-word-using-special-typewriter.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=1974 lang=python3 3 | # 4 | # [1974] Minimum Time to Type Word Using Special Typewriter 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: String, Greedy 9 | 10 | 11 | class Solution: 12 | # 28 ms, Time: O(N). Space: O(1) 13 | def minTimeToType(self, word: str) -> int: 14 | 15 | def get_min_dist(a, b): 16 | dist = abs(ord(b) - ord(a)) 17 | return min(dist, 26 - dist) 18 | 19 | ans = 0 20 | prev = 'a' 21 | for c in word: 22 | ans += get_min_dist(prev, c) + 1 23 | prev = c 24 | return ans 25 | 26 | # @lc code=end 27 | -------------------------------------------------------------------------------- /src/354.russian-doll-envelopes.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=354 lang=python3 3 | # 4 | # [354] Russian Doll Envelopes 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: Binary Search, Dynamic Programming 9 | # REVIEWME: Binary Search, Dynamic Programming 10 | import bisect 11 | 12 | 13 | class Solution: 14 | # Base on discussion 15 | # 140 ms, 96.79%. Time: O(NlogN). Space: O(N) 16 | def maxEnvelopes(self, envelopes: List[List[int]]) -> int: 17 | envelopes.sort(key=lambda x: (x[0], -x[1])) 18 | lis = [] 19 | for _, e in envelopes: 20 | i = bisect.bisect_left(lis, e) 21 | lis[i: i+1] = [e] 22 | return len(lis) 23 | # @lc code=end 24 | -------------------------------------------------------------------------------- /src/605.can-place-flowers.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=605 lang=python3 3 | # 4 | # [605] Can Place Flowers 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: Array, Greedy 9 | class Solution: 10 | # 184 ms, 10.86%. Time: O(N). Space: O(1) 11 | def canPlaceFlowers(self, flowerbed: List[int], n: int) -> bool: 12 | cnt = 0 13 | for i in range(len(flowerbed)): 14 | v1 = flowerbed[i - 1] if i else 0 15 | v2 = flowerbed[i] 16 | v3 = flowerbed[i + 1] if i < len(flowerbed) - 1 else 0 17 | if v1 == v2 == v3 == 0: 18 | flowerbed[i] = 1 19 | cnt += 1 20 | return cnt >= n 21 | # @lc code=end 22 | 23 | -------------------------------------------------------------------------------- /src/991.broken-calculator.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=991 lang=python3 3 | # 4 | # [991] Broken Calculator 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: Math, Greedy 9 | class Solution: 10 | # 20 ms, 99%. Time: O(logN). Space: O(1). 11 | def brokenCalc(self, X: int, Y: int) -> int: 12 | if X >= Y: return X - Y 13 | 14 | cnt = 0 15 | while X < Y: 16 | X *= 2 17 | cnt += 1 18 | 19 | diff = X - Y 20 | for i in range(cnt, -1, -1): 21 | d, diff = divmod(diff, 2**i) 22 | cnt += d 23 | return cnt 24 | 25 | 26 | 27 | # @lc code=end 28 | 29 | -------------------------------------------------------------------------------- /testcases/835: -------------------------------------------------------------------------------- 1 | [[1,1,0],[0,1,0],[0,1,0]] 2 | [[0,0,0],[0,1,1],[0,0,1]] 3 | [[1, 1, 0, 1, 0, 1, 1, 0, 1, 0], [0, 0, 1, 0, 1, 0, 0, 1, 0, 1], [0, 0, 1, 1, 0, 0, 0, 1, 1, 1], [0, 0, 0, 0, 1, 1, 0, 0, 1, 0], [1, 1, 1, 0, 1, 0, 1, 0, 0, 1], [1, 0, 1, 0, 0, 0, 1, 0, 0, 1], [1, 0, 0, 0, 0, 1, 0, 0, 1, 1], [1, 0, 1, 1, 1, 1, 0, 0, 1, 0], [0, 1, 0, 0, 0, 0, 0, 1, 1, 0], [0, 0, 1, 1, 1, 0, 0, 0, 1, 0]] 4 | [[1, 1, 0, 1, 0, 1, 1, 0, 1, 0], [0, 0, 1, 0, 1, 0, 0, 1, 0, 1], [0, 0, 1, 1, 0, 0, 0, 1, 1, 1], [0, 0, 0, 0, 1, 1, 0, 0, 1, 0], [1, 1, 1, 0, 1, 0, 1, 0, 0, 1], [1, 0, 1, 0, 0, 0, 1, 0, 0, 1], [1, 0, 0, 0, 0, 1, 0, 0, 1, 1], [1, 0, 1, 1, 1, 1, 0, 0, 1, 0], [0, 1, 0, 0, 0, 0, 0, 1, 1, 0], [0, 0, 1, 1, 1, 0, 0, 0, 1, 0]] -------------------------------------------------------------------------------- /src/141.linked-list-cycle.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=141 lang=python3 3 | # 4 | # [141] Linked List Cycle 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: Linked List 9 | # Definition for singly-linked list. 10 | # class ListNode: 11 | # def __init__(self, x): 12 | # self.val = x 13 | # self.next = None 14 | 15 | class Solution: 16 | def hasCycle(self, head: ListNode) -> bool: 17 | if not head: return False 18 | fast = head.next 19 | slow = head 20 | while fast and fast.next: 21 | if slow == fast: return True 22 | slow = slow.next 23 | fast = fast.next.next 24 | return False 25 | # @lc code=end 26 | 27 | -------------------------------------------------------------------------------- /src/1582.special-positions-in-a-binary-matrix.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=1582 lang=python3 3 | # 4 | # [1582] Special Positions in a Binary Matrix 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: Array 9 | class Solution: 10 | # 164 ms, 96.86%. Time and Space: O(N) 11 | def numSpecial(self, mat: List[List[int]]) -> int: 12 | sum_of_rows = [sum(row) for row in mat] 13 | sum_of_cols = [sum(col) for col in zip(*mat)] 14 | ans = 0 15 | for r in range(len(mat)): 16 | for c in range(len(mat[0])): 17 | if mat[r][c] == sum_of_cols[c] == sum_of_rows[r] == 1: 18 | ans += 1 19 | return ans 20 | # @lc code=end 21 | 22 | -------------------------------------------------------------------------------- /src/2303.calculate-amount-paid-in-taxes.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=2303 lang=python3 3 | # 4 | # [2303] Calculate Amount Paid in Taxes 5 | # 6 | 7 | 8 | # @lc code=start 9 | # TAGS: Array, Simulation 10 | class Solution: 11 | # Time: O(N). Space: O(1) 12 | def calculateTax(self, brackets: List[List[int]], income: int) -> float: 13 | ans = prev_upper = 0 14 | for upper, tax in brackets: 15 | amount = min(income, upper - prev_upper) 16 | ans += amount * tax 17 | prev_upper = upper 18 | income -= amount 19 | if income == 0: 20 | break 21 | return ans / 100 22 | 23 | 24 | # @lc code=end 25 | -------------------------------------------------------------------------------- /src/2347.best-poker-hand.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=2347 lang=python3 3 | # 4 | # [2347] Best Poker Hand 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: Array, Hash Table, Counting 9 | class Solution: 10 | def bestHand(self, ranks: List[int], suits: List[str]) -> str: 11 | if len(set(suits)) == 1: 12 | return "Flush" 13 | 14 | counter = collections.defaultdict(int) 15 | for r in ranks: 16 | counter[r] += 1 17 | mx = max(counter.values()) 18 | 19 | if mx >= 3: 20 | return "Three of a Kind" 21 | 22 | if mx == 2: 23 | return "Pair" 24 | 25 | return "High Card" 26 | # @lc code=end 27 | 28 | -------------------------------------------------------------------------------- /src/2370.longest-ideal-subsequence.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=2370 lang=python3 3 | # 4 | # [2370] Longest Ideal Subsequence 5 | # 6 | 7 | 8 | # @lc code=start 9 | # TAGS: Hash Table, String, Dynamic Programming 10 | # REVIEWME: Dynamic Programming 11 | 12 | 13 | class Solution: 14 | # Time and Space: O(N) 15 | def longestIdealString(self, s: str, k: int) -> int: 16 | counter = [0] * 26 17 | for c in s: 18 | i = ord(c) - ord("a") 19 | for n in range(max(i - k, 0), min(i + k, 25) + 1): 20 | counter[i] = max(counter[i], counter[n]) 21 | counter[i] += 1 22 | return max(counter) 23 | 24 | 25 | # @lc code=end 26 | -------------------------------------------------------------------------------- /testcases/1032: -------------------------------------------------------------------------------- 1 | ["StreamChecker","query","query","query","query","query","query","query","query","query","query","query","query"] 2 | [[["cd","f","kl","a","ab","abc"]],["a"],["b"],["c"],["d"],["e"],["f"],["g"],["h"],["i"],["j"],["k"],["l"]] 3 | ["StreamChecker","query","query","query","query","query","query","query","query","query","query","query","query","query","query","query","query","query","query","query","query","query","query","query","query","query","query","query","query","query","query"] 4 | [[["ab","ba","aaab","abab","baa"]],["a"],["a"],["a"],["a"],["a"],["b"],["a"],["b"],["a"],["b"],["b"],["b"],["a"],["b"],["a"],["b"],["b"],["b"],["b"],["a"],["b"],["a"],["b"],["a"],["a"],["a"],["b"],["a"],["a"],["a"]] -------------------------------------------------------------------------------- /src/153.find-minimum-in-rotated-sorted-array.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=153 lang=python3 3 | # 4 | # [153] Find Minimum in Rotated Sorted Array 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: Binary Search 9 | # REVIEWME: 10 | class Solution: 11 | # 44 ms, 45%. O(log N), O(1) 12 | def findMin(self, nums: List[int]) -> int: 13 | if nums[0] < nums[-1]: return nums[0] 14 | 15 | lo, hi = 0, len(nums) - 1 16 | while lo < hi: 17 | mid = (lo + hi)//2 18 | val = nums[mid] 19 | if val < nums[hi]: 20 | hi = mid 21 | else: 22 | lo = mid + 1 23 | return nums[lo] 24 | # @lc code=end 25 | 26 | -------------------------------------------------------------------------------- /testcases/30: -------------------------------------------------------------------------------- 1 | "barfoothefoobarman" 2 | ["foo","bar"] 3 | "wordgoodgoodgoodbestword" 4 | ["word","good","best","word"] 5 | "barfoofoobarthefoobarman" 6 | ["bar","foo","the"] 7 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 8 | ["aaaaaaaaaaaaaa","aaaaaaaaaaaaaa","aaaaaaaaaaaaaa","aaaaaaaaaaaaaa","aaaaaaaaaaaaaa"] -------------------------------------------------------------------------------- /src/1277.count-square-submatrices-with-all-ones.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=1277 lang=python3 3 | # 4 | # [1277] Count Square Submatrices with All Ones 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: Array, Dynamic Programming 9 | class Solution: 10 | # 644 ms, 86.64% 11 | # Exactly the same as problem 221 12 | def countSquares(self, matrix: List[List[int]]) -> int: 13 | for r in range(1, len(matrix)): 14 | for c in range(1, len(matrix[0])): 15 | if matrix[r][c]: 16 | matrix[r][c] = min(matrix[r-1][c-1], matrix[r][c-1], matrix[r-1][c]) + 1 17 | return sum(val for row in matrix for val in row) 18 | 19 | # @lc code=end 20 | 21 | -------------------------------------------------------------------------------- /src/1791.find-center-of-star-graph.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=1791 lang=python3 3 | # 4 | # [1791] Find Center of Star Graph 5 | # 6 | 7 | # @lc code=start 8 | 9 | 10 | class Solution: 11 | # 860 ms, 50%. Time and Space: O(N) 12 | def findCenter(self, edges: List[List[int]]) -> int: 13 | counter = collections.Counter() 14 | for s, e in edges: 15 | counter[s] += 1 16 | counter[e] += 1 17 | return max(counter, key=counter.get) 18 | 19 | # 836 ms, 58.15%. Time and Space: O(1) 20 | def findCenter(self, edges: List[List[int]]) -> int: 21 | intersect = set(edges[0]) & set(edges[1]) 22 | return intersect.pop() 23 | # @lc code=end 24 | -------------------------------------------------------------------------------- /src/300.longest-increasing-subsequence.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=300 lang=python3 3 | # 4 | # [300] Longest Increasing Subsequence 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: Binary Search, Dynamic Programming 9 | # REVIEWME: Binary Search, Dynamic Programming 10 | import bisect 11 | 12 | 13 | class Solution: 14 | # Based on article solution. Binary Search + DP 15 | def lengthOfLIS(self, nums: List[int]) -> int: 16 | lis = [] 17 | for num in nums: 18 | i = bisect.bisect_left(lis, num) 19 | if i >= len(lis): 20 | lis.append(num) 21 | else: 22 | lis[i] = num 23 | return len(lis) 24 | 25 | # @lc code=end 26 | -------------------------------------------------------------------------------- /src/970.powerful-integers.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=970 lang=python3 3 | # 4 | # [970] Powerful Integers 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: Hash Table, Math 9 | 10 | 11 | class Solution: 12 | # 32 ms, 69.17%. Time: O(logx*logy). Space: O(logx+logy) 13 | def powerfulIntegers(self, x: int, y: int, bound: int) -> List[int]: 14 | powers_x = set(x**i for i in range(21) if x**i <= bound) 15 | powers_y = set(y**i for i in range(21) if y**i <= bound) 16 | answer = set() 17 | for xi in powers_x: 18 | for yi in powers_y: 19 | if xi + yi <= bound: 20 | answer.add(xi + yi) 21 | return answer 22 | # @lc code=end 23 | -------------------------------------------------------------------------------- /src/1290.convert-binary-number-in-a-linked-list-to-integer.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=1290 lang=python3 3 | # 4 | # [1290] Convert Binary Number in a Linked List to Integer 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: Linked List, Bit Manipulation 9 | # Definition for singly-linked list. 10 | # class ListNode: 11 | # def __init__(self, val=0, next=None): 12 | # self.val = val 13 | # self.next = next 14 | class Solution: 15 | def getDecimalValue(self, head: ListNode) -> int: 16 | ans = 0 17 | while head: 18 | ans = (ans << 1) | head.val 19 | # ans = ans * 2 + head.val 20 | head = head.next 21 | return ans 22 | # @lc code=end 23 | 24 | -------------------------------------------------------------------------------- /src/1710.maximum-units-on-a-truck.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=1710 lang=python3 3 | # 4 | # [1710] Maximum Units on a Truck 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: Sort, Greedy 9 | class Solution: 10 | # 144 ms, 98.10%. Time: O(NlogN). Space: O(N) 11 | def maximumUnits(self, boxTypes: List[List[int]], truckSize: int) -> int: 12 | boxTypes.sort(key=lambda x: x[-1]) 13 | total_unit = 0 14 | while boxTypes and truckSize: 15 | boxes, units_per = boxTypes.pop() 16 | new_box = min(truckSize, boxes) 17 | truckSize -= new_box 18 | total_unit += new_box * units_per 19 | return total_unit 20 | 21 | # @lc code=end 22 | 23 | -------------------------------------------------------------------------------- /src/1991.find-the-middle-index-in-array.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=1991 lang=python3 3 | # 4 | # [1991] Find the Middle Index in Array 5 | # 6 | 7 | # @lc code=start 8 | from typing import List 9 | 10 | 11 | class Solution: 12 | # Time and Space: O(N) 13 | def findMiddleIndex(self, nums: List[int]) -> int: 14 | prefix_sum = [0] 15 | for val in nums: 16 | prefix_sum.append(prefix_sum[-1] + val) 17 | 18 | for i in range(1, len(prefix_sum)): 19 | left = prefix_sum[i - 1] - prefix_sum[0] 20 | right = prefix_sum[-1] - prefix_sum[i] 21 | if left == right: 22 | return i - 1 23 | return -1 24 | # @lc code=end 25 | -------------------------------------------------------------------------------- /src/458.poor-pigs.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=458 lang=python3 3 | # 4 | # [458] Poor Pigs 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: Math 9 | class Solution: 10 | # Time and Space: O(1) 11 | # Explanation from Stefan: https://leetcode.com/problems/poor-pigs/discuss/94266/Another-explanation-and-solution 12 | def poorPigs(self, buckets: int, minutesToDie: int, minutesToTest: int) -> int: 13 | # The important point is: pig can drink as many buckets as they want. 14 | # Thus, we can assign each pig to a dimension 15 | trials = minutesToTest // minutesToDie + 1 16 | return ceil(math.log(buckets, trials)) 17 | 18 | 19 | 20 | # @lc code=end 21 | 22 | -------------------------------------------------------------------------------- /src/1481.least-number-of-unique-integers-after-k-removals.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=1481 lang=python3 3 | # 4 | # [1481] Least Number of Unique Integers after K Removals 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: Array, Sort 9 | class Solution: 10 | # 464 ms, 96.27%. Time: O(NlogN), Time: O(N) 11 | def findLeastNumOfUniqueInts(self, arr: List[int], k: int) -> int: 12 | freq = collections.Counter(arr) 13 | freqs = sorted(freq.values()) 14 | accum = itertools.accumulate(freqs) 15 | for i, val in enumerate(accum, 1): 16 | if val == k: return len(freqs) - i 17 | elif val > k: return len(freqs) - (i - 1) 18 | return 0 19 | 20 | # @lc code=end 21 | 22 | -------------------------------------------------------------------------------- /src/1725.number-of-rectangles-that-can-form-the-largest-square.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=1725 lang=python3 3 | # 4 | # [1725] Number Of Rectangles That Can Form The Largest Square 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: Greedy 9 | class Solution: 10 | # 172 ms, 99.41%. Time: O(N). Space: O(1) 11 | def countGoodRectangles(self, rectangles: List[List[int]]) -> int: 12 | max_size = 0 13 | cnt = 0 14 | for rectangle in rectangles: 15 | temp = min(rectangle) 16 | if temp == max_size: 17 | cnt += 1 18 | elif temp > max_size: 19 | max_size = temp 20 | cnt = 1 21 | return cnt 22 | # @lc code=end 23 | 24 | -------------------------------------------------------------------------------- /src/528.random-pick-with-weight.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=528 lang=python3 3 | # 4 | # [528] Random Pick with Weight 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: Binary Search, Random 9 | class Solution: 10 | # 228 ms, 92.45%. Pythonic solution 11 | 12 | # O(N) 13 | def __init__(self, w: List[int]): 14 | self.nums = list(itertools.accumulate(w, operator.add)) 15 | 16 | # O(logN) 17 | def pickIndex(self) -> int: 18 | num = random.randint(1, self.nums[-1]) 19 | return bisect.bisect_left(self.nums, num) 20 | 21 | 22 | 23 | # Your Solution object will be instantiated and called as such: 24 | # obj = Solution(w) 25 | # param_1 = obj.pickIndex() 26 | # @lc code=end 27 | 28 | -------------------------------------------------------------------------------- /src/1557.minimum-number-of-vertices-to-reach-all-nodes.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=1557 lang=python3 3 | # 4 | # [1557] Minimum Number of Vertices to Reach All Nodes 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: Graph 9 | import collections 10 | import itertools 11 | class Solution: 12 | # 1288 ms, 81.78 %. Time: O(V+E). Space: O(E) 13 | def findSmallestSetOfVertices(self, n: int, edges: List[List[int]]) -> List[int]: 14 | graph = collections.defaultdict(list) 15 | for f, t in edges: 16 | graph[f].append(t) 17 | 18 | tos = set(item for val in graph.values() for item in val) 19 | froms = set(graph.keys()) 20 | return froms - tos 21 | 22 | # @lc code=end 23 | 24 | -------------------------------------------------------------------------------- /src/1758.minimum-changes-to-make-alternating-binary-string.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=1758 lang=python3 3 | # 4 | # [1758] Minimum Changes To Make Alternating Binary String 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: Array, Greedy 9 | 10 | 11 | class Solution: 12 | # 44 ms, 96.73%. Time: O(N). Space: O(1) 13 | def minOperations(self, s: str) -> int: 14 | o1 = '1' 15 | o2 = '0' 16 | cnt1 = cnt2 = 0 17 | for c in s: 18 | if c != o1: 19 | cnt1 += 1 20 | if c != o2: 21 | cnt2 += 1 22 | o1 = '0' if o1 == '1' else '1' 23 | o2 = '0' if o2 == '1' else '1' 24 | return min(cnt1, cnt2) 25 | # @lc code=end 26 | -------------------------------------------------------------------------------- /src/1859.sorting-the-sentence.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=1859 lang=python3 3 | # 4 | # [1859] Sorting the Sentence 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: String, Sorting 9 | 10 | 11 | class Solution: 12 | # 24 ms, 94.80%. Time: O(N). Space: O(N) 13 | def sortSentence(self, s: str) -> str: 14 | def get_index(word): 15 | for i, c in enumerate(word): 16 | if c in "0123456789": 17 | return word[:i], word[i:] 18 | words = s.split() 19 | answer = [""] * (len(words) + 1) 20 | for word in words: 21 | w, index = get_index(word) 22 | answer[int(index)] = w 23 | return " ".join(answer[1:]) 24 | # @lc code=end 25 | -------------------------------------------------------------------------------- /src/3105.longest-strictly-increasing-or-strictly-decreasing-subarray.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=3105 lang=python3 3 | # 4 | # [3105] Longest Strictly Increasing or Strictly Decreasing Subarray 5 | # 6 | 7 | 8 | # @lc code=start 9 | class Solution: 10 | def longestMonotonicSubarray(self, nums: List[int]) -> int: 11 | inc = dec = ans = 1 12 | for n1, n2 in zip(nums, nums[1:]): 13 | if n1 > n2: 14 | dec += 1 15 | inc = 1 16 | elif n1 < n2: 17 | inc += 1 18 | dec = 1 19 | else: 20 | dec = inc = 1 21 | ans = max([ans, inc, dec]) 22 | return ans 23 | 24 | 25 | # @lc code=end 26 | -------------------------------------------------------------------------------- /src/2140.solving-questions-with-brainpower.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=2140 lang=python3 3 | # 4 | # [2140] Solving Questions With Brainpower 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: Array, Dynamic Programming 9 | from typing import List 10 | 11 | 12 | class Solution: 13 | # 3090 ms. Time and Space: O(N) 14 | def mostPoints(self, questions: List[List[int]]) -> int: 15 | N = len(questions) 16 | dp = [0] * (N + 1) 17 | for i in reversed(range(N)): 18 | pts, brain = questions[i] 19 | total = pts 20 | if i + brain + 1 < N: 21 | total += dp[i + brain + 1] 22 | dp[i] = max(total, dp[i + 1]) 23 | return dp[0] 24 | # @lc code=end 25 | -------------------------------------------------------------------------------- /src/470.implement-rand-10-using-rand-7.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=470 lang=python3 3 | # 4 | # [470] Implement Rand10() Using Rand7() 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: Math 9 | # REVIEWME: Math, Design 10 | # The rand7() API is already defined for you. 11 | # def rand7(): 12 | # @return a random integer in the range 1 to 7 13 | 14 | class Solution: 15 | # From discussion: https://leetcode.com/problems/implement-rand10-using-rand7/discuss/816210/Python-rejection-sampling-2-lines-explained 16 | # This is related to geometric distribution. 17 | def rand10(self): 18 | c = (rand7() - 1)*7 + rand7() - 1 19 | return self.rand10() if c >= 40 else (c % 10) + 1 20 | 21 | # @lc code=end 22 | 23 | -------------------------------------------------------------------------------- /src/797.all-paths-from-source-to-target.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=797 lang=python3 3 | # 4 | # [797] All Paths From Source to Target 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: Backtracking, Depth First Search 9 | class Solution: 10 | # 92 ms, 99%. Time and Space: O(2^N * N) 11 | def allPathsSourceTarget(self, graph: List[List[int]]) -> List[List[int]]: 12 | self.ans = [] 13 | def dfs(start=0, sofar=[]): 14 | if start == len(graph)-1: 15 | self.ans.append(sofar + [start]) 16 | return 17 | for node in graph[start]: 18 | dfs(node, sofar + [start]) 19 | dfs() 20 | return self.ans 21 | 22 | # @lc code=end 23 | 24 | -------------------------------------------------------------------------------- /src/1493.longest-subarray-of-1-s-after-deleting-one-element.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=1493 lang=python3 3 | # 4 | # [1493] Longest Subarray of 1's After Deleting One Element 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: Array 9 | class Solution: 10 | # 38.82%. Time: O(N), Space: O(1) 11 | def longestSubarray(self, nums: List[int]) -> int: 12 | cnt1 = ans = 0 13 | cnt2 = -1 # Need this because we must delete 1 character. 14 | for n in nums: 15 | if n: 16 | cnt1 += 1 17 | else: 18 | cnt2 = cnt1 19 | cnt1 = 0 20 | ans = max(ans, cnt1 + cnt2) 21 | return ans 22 | 23 | 24 | # @lc code=end 25 | 26 | -------------------------------------------------------------------------------- /src/1525.number-of-good-ways-to-split-a-string.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=1525 lang=python3 3 | # 4 | # [1525] Number of Good Ways to Split a String 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: String 9 | class Solution: 10 | # 168 ms, 88.72 %. Time and Space: O(N) 11 | def numSplits(self, s: str) -> int: 12 | left = [0] * len(s) 13 | right = [0] * len(s) 14 | 15 | set1 = set() 16 | set2 = set() 17 | 18 | for i in range(len(s)): 19 | set1.add(s[i]) 20 | left[i] = len(set1) 21 | 22 | set2.add(s[~i]) 23 | right[~i] = len(set2) 24 | return sum(l == r for l, r in zip(left, right[1:])) 25 | 26 | # @lc code=end 27 | 28 | -------------------------------------------------------------------------------- /src/1685.sum-of-absolute-differences-in-a-sorted-array.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=1685 lang=python3 3 | # 4 | # [1685] Sum of Absolute Differences in a Sorted Array 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: Math, Greedy 9 | class Solution: 10 | # 960 ms, 68.22 %. Time and Space: O(N) 11 | def getSumAbsoluteDifferences(self, nums: List[int]) -> List[int]: 12 | total = sum(nums) 13 | ans = [] 14 | prefix_sum = 0 15 | for i, num in enumerate(nums, 1): 16 | prefix_sum += num 17 | postfix_sum = total - prefix_sum 18 | val = postfix_sum - (len(nums) - i) * num + i * num - prefix_sum 19 | ans.append(val) 20 | return ans 21 | # @lc code=end 22 | 23 | -------------------------------------------------------------------------------- /src/1792.maximum-average-pass-ratio.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=1792 lang=python3 3 | # 4 | # [1792] Maximum Average Pass Ratio 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: Heap 9 | 10 | 11 | class Solution: 12 | # 2588 ms, 89.07%. Time: O(N + KlogN). Space: O(N) 13 | def maxAverageRatio(self, classes: List[List[int]], extraStudents: int) -> float: 14 | heap = [(p/t - (p+1)/(t+1), p, t) for p, t in classes] 15 | heapq.heapify(heap) 16 | for _ in range(extraStudents): 17 | _, p, t = heapq.heappop(heap) 18 | p += 1 19 | t += 1 20 | heapq.heappush(heap, (p/t - (p+1)/(t+1), p, t)) 21 | return sum(p/t for _, p, t in heap) / len(heap) 22 | # @lc code=end 23 | -------------------------------------------------------------------------------- /src/2087.minimum-cost-homecoming-of-a-robot-in-a-grid.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=2087 lang=python3 3 | # 4 | # [2087] Minimum Cost Homecoming of a Robot in a Grid 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: Array, Greedy, Matrix 9 | from typing import List 10 | 11 | 12 | class Solution: 13 | # 2165 ms, 9.33%. Time: O(N). Space: O(1) 14 | def minCost(self, startPos: List[int], homePos: List[int], rowCosts: List[int], colCosts: List[int]) -> int: 15 | r0, c0 = startPos 16 | r1, c1 = homePos 17 | ans = 0 18 | ans += sum(rowCosts[r0 + 1: r1 + 1] if r0 < r1 else rowCosts[r1: r0]) 19 | ans += sum(colCosts[c0 + 1: c1 + 1] if c0 < c1 else colCosts[c1: c0]) 20 | return ans 21 | # @lc code=end 22 | -------------------------------------------------------------------------------- /src/3090.maximum-length-substring-with-two-occurrences.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=3090 lang=python3 3 | # 4 | # [3090] Maximum Length Substring With Two Occurrences 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: Hash Table, String, Sliding Window 9 | from collections import Counter 10 | 11 | 12 | class Solution: 13 | # Time: O(N) Space: O(1) 14 | def maximumLengthSubstring(self, s: str) -> int: 15 | counter = Counter() 16 | ans = ptr = 0 17 | for i, c in enumerate(s): 18 | counter[c] += 1 19 | while counter[c] > 2: 20 | counter[s[ptr]] -= 1 21 | ptr += 1 22 | ans = max(ans, i - ptr + 1) 23 | return ans 24 | 25 | 26 | # @lc code=end 27 | -------------------------------------------------------------------------------- /src/667.beautiful-arrangement-ii.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=667 lang=python3 3 | # 4 | # [667] Beautiful Arrangement II 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: Array 9 | 10 | 11 | class Solution: 12 | """ 13 | Greedily construct the array: 14 | 1 2 3 4 15 | 7 6 5 16 | """ 17 | # 44 ms, 86.00 %. Time and Space: O(N) 18 | 19 | def constructArray(self, n: int, k: int) -> List[int]: 20 | lo = 1 21 | hi = lo + k 22 | output = [] 23 | while lo <= hi: 24 | output.append(lo) 25 | if hi != lo: 26 | output.append(hi) 27 | lo += 1 28 | hi -= 1 29 | return output + list(range(k + 2, n + 1)) 30 | # @lc code=end 31 | -------------------------------------------------------------------------------- /src/846.hand-of-straights.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=846 lang=python3 3 | # 4 | # [846] Hand of Straights 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: Ordered Map 9 | class Solution: 10 | # 152 ms, 99.95%. Time: O(MlogM+NW). Space: O(N). N is unique number. M is len(hand) 11 | def isNStraightHand(self, hand: List[int], W: int) -> bool: 12 | counter = collections.Counter(hand) 13 | for curr in sorted(counter): 14 | quantity = counter[curr] 15 | if quantity <= 0: continue 16 | for i in range(curr, curr + W): 17 | if counter[i] < quantity: return False 18 | counter[i] -= quantity 19 | return True 20 | 21 | # @lc code=end 22 | 23 | -------------------------------------------------------------------------------- /src/954.array-of-doubled-pairs.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=954 lang=python3 3 | # 4 | # [954] Array of Doubled Pairs 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: Array, Hash Table, Greedy, Sorting 9 | 10 | 11 | class Solution: 12 | # 680 ms, 63.64%. Time: O(NlogN). Space: O(N) 13 | def canReorderDoubled(self, arr: List[int]) -> bool: 14 | arr.sort(key=abs, reverse=True) 15 | counter = collections.Counter() 16 | cnt = 0 17 | for num in arr: 18 | double = num * 2 19 | if counter[double]: 20 | counter[double] -= 1 21 | cnt += 1 22 | else: 23 | counter[num] += 1 24 | return cnt >= len(arr) / 2 25 | # @lc code=end 26 | -------------------------------------------------------------------------------- /src/1534.count-good-triplets.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=1534 lang=python3 3 | # 4 | # [1534] Count Good Triplets 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: Array, Enumeration 9 | from typing import List 10 | 11 | 12 | class Solution: 13 | # Time: O(N^3). Space: O(N) 14 | def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int: 15 | ans = 0 16 | for i in range(len(arr)): 17 | for j in range(i + 1, len(arr)): 18 | for k in range(j + 1, len(arr)): 19 | ans += (abs(arr[i] - arr[j]) <= a) \ 20 | and (abs(arr[j] - arr[k]) <= b) \ 21 | and (abs(arr[k] - arr[i]) <= c) 22 | return ans 23 | # @lc code=end 24 | -------------------------------------------------------------------------------- /src/2389.longest-subsequence-with-limited-sum.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=2389 lang=python3 3 | # 4 | # [2389] Longest Subsequence With Limited Sum 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: Array, Binary Search, Greedy, Sorting, Prefix Sum 9 | class Solution: 10 | # Time: O(NlogN). Space: O(N) 11 | def answerQueries(self, nums: List[int], queries: List[int]) -> List[int]: 12 | nums.sort() 13 | 14 | prefix = [0] 15 | for num in nums: 16 | prefix.append(prefix[-1] + num) 17 | print(prefix) 18 | ans = [] 19 | for query in queries: 20 | idx = bisect.bisect_right(prefix, query) 21 | ans.append(idx - 1) 22 | return ans 23 | 24 | 25 | # @lc code=end 26 | -------------------------------------------------------------------------------- /src/554.brick-wall.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=554 lang=python3 3 | # 4 | # [554] Brick Wall 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: Hash Table 9 | 10 | 11 | class Solution: 12 | """ 13 | Approach: Count Prefix sum 14 | """ 15 | # 176 ms, 84.56 %. Time and Space: O(N). N is number of total bricks 16 | 17 | def leastBricks(self, wall: List[List[int]]) -> int: 18 | counter = collections.defaultdict(int) 19 | for row in wall: 20 | prefix = 0 21 | for brick in row[:-1]: 22 | prefix += brick 23 | counter[prefix] += 1 24 | if not counter: 25 | return len(wall) 26 | return len(wall) - max(counter.values()) 27 | # @lc code=end 28 | -------------------------------------------------------------------------------- /src/583.delete-operation-for-two-strings.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=583 lang=python3 3 | # 4 | # [583] Delete Operation for Two Strings 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: Dynamic Programming, String 9 | class Solution: 10 | # 276 ms, 74.96%. Time: O(N^2). Space: O(N) assuming words have the same length 11 | def minDistance(self, word1: str, word2: str) -> int: 12 | prev = list(range(len(word1) + 1)) 13 | for c2 in word2: 14 | cur = [prev[0] + 1] 15 | for i in range(len(word1)): 16 | if c2 != word1[i]: prev[i] += 2 17 | cur.append(min(prev[i], cur[-1] + 1, prev[i + 1] + 1)) 18 | prev = cur 19 | return prev[-1] 20 | # @lc code=end 21 | 22 | -------------------------------------------------------------------------------- /src/104.maximum-depth-of-binary-tree.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=104 lang=python3 3 | # 4 | # [104] Maximum Depth of Binary Tree 5 | # 6 | 7 | # @lc code=start 8 | # Definition for a binary tree node. 9 | # TAGS: Tree, BFS, DFS 10 | from typing import Optional 11 | 12 | 13 | class TreeNode: 14 | def __init__(self, val=0, left=None, right=None): 15 | self.val = val 16 | self.left = left 17 | self.right = right 18 | 19 | 20 | class Solution: 21 | def maxDepth(self, root: Optional[TreeNode]) -> int: 22 | def dfs(node): 23 | if not node: 24 | return 0 25 | return max(dfs(node.left), dfs(node.right)) + 1 26 | 27 | return dfs(root) 28 | 29 | 30 | # @lc code=end 31 | -------------------------------------------------------------------------------- /src/1963.minimum-number-of-swaps-to-make-the-string-balanced.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=1963 lang=python3 3 | # 4 | # [1963] Minimum Number of Swaps to Make the String Balanced 5 | # 6 | 7 | 8 | # @lc code=start 9 | # TAGS: Two Pointers,String,Stack,Greedy 10 | class Solution: 11 | # After realization: 1 swap reduce 2 pairs (4 chars) 12 | # Time: O(N). Space: O(1) 13 | def minSwaps(self, s: str) -> int: 14 | op = cl = 0 15 | for c in s: 16 | if c == "[": 17 | op += 1 18 | elif c == "]" and op: 19 | op -= 1 20 | else: 21 | cl += 1 22 | return (cl + op + 2) // 4 # trick to account for (sum % 4 == 0) 23 | 24 | 25 | # @lc code=end 26 | -------------------------------------------------------------------------------- /src/389.find-the-difference.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=389 lang=python3 3 | # 4 | # [389] Find the Difference 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: Hash Table, Bit Manipulation 9 | # REVIEWME: Bit Manipulation 10 | class Solution: 11 | # 32 ms, 98%. Time: O(N). Space: O(1) because of alphabet 12 | def findTheDifference1(self, s: str, t: str) -> str: 13 | rv = collections.Counter(t) - collections.Counter(s) 14 | return "".join(rv.keys()) 15 | 16 | # Bit Manipulation. Detection odd occurence. Time: O(N). Space: O(1) 17 | def findTheDifference(self, s: str, t: str) -> str: 18 | temp = 0 19 | for c in s + t: 20 | temp ^= ord(c) 21 | return chr(temp) 22 | # @lc code=end 23 | 24 | -------------------------------------------------------------------------------- /src/2091.removing-minimum-and-maximum-from-array.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=2091 lang=python3 3 | # 4 | # [2091] Removing Minimum and Maximum From Array 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: Greedy, Array 9 | from typing import List 10 | 11 | 12 | class Solution: 13 | # Time: O(N). Space: O(1) 14 | def minimumDeletions(self, nums: List[int]) -> int: 15 | if len(nums) == 1: 16 | return 1 17 | 18 | max_arg = nums.index(max(nums)) 19 | min_arg = nums.index(min(nums)) 20 | 21 | op1 = max(max_arg, min_arg) + 1 22 | op2 = len(nums) - min(max_arg, min_arg) 23 | op3 = min(min_arg, max_arg) + 1 + len(nums) - max(max_arg, min_arg) 24 | return min(op1, op2, op3) 25 | # @lc code=end 26 | -------------------------------------------------------------------------------- /src/32.longest-valid-parentheses.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=32 lang=python3 3 | # 4 | # [32] Longest Valid Parentheses 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: String, Dynamic Programming 9 | 10 | 11 | class Solution: 12 | # 40 ms, 80%. Time and Space: O(N) 13 | def longestValidParentheses(self, s: str) -> int: 14 | max_cnt = cnt = 0 15 | stack = [] 16 | for c in s: 17 | if c == "(": 18 | stack.append(cnt) 19 | cnt = 0 20 | else: 21 | if stack: 22 | cnt += 2 + stack.pop() 23 | else: 24 | cnt = 0 25 | max_cnt = max(max_cnt, cnt) 26 | return max_cnt 27 | 28 | # @lc code=end 29 | -------------------------------------------------------------------------------- /src/1545.find-kth-bit-in-nth-binary-string.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=1545 lang=python3 3 | # 4 | # [1545] Find Kth Bit in Nth Binary String 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: String 9 | class Solution: 10 | """ 11 | More solution from lee: https://leetcode.com/problems/find-kth-bit-in-nth-binary-string/discuss/781181/JavaC%2B%2BPython-Iterative-Solutions 12 | """ 13 | # 124 ms, 61.68 %. Time and Space: O(N^2) because as n grows, the number of digit doubles. 14 | def findKthBit(self, n: int, k: int) -> str: 15 | S = '0' 16 | for _ in range(n): 17 | L = len(S) 18 | num = f"{~int(S, 2) % (1 << L) :b}" 19 | S += '1' + num[::-1] 20 | return S[k-1] 21 | # @lc code=end 22 | 23 | -------------------------------------------------------------------------------- /src/2270.number-of-ways-to-split-array.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=2270 lang=python3 3 | # 4 | # [2270] Number of Ways to Split Array 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: Array, Prefix Sum 9 | from typing import List 10 | 11 | 12 | class Solution: 13 | # Time and SPace: O(N) 14 | def waysToSplitArray(self, nums: List[int]) -> int: 15 | prefix_sum = nums.copy() 16 | for i in range(1, len(nums)): 17 | prefix_sum[i] += prefix_sum[i - 1] 18 | 19 | cnt = 0 20 | for i in range(len(nums) - 1): 21 | left = prefix_sum[i] 22 | right = prefix_sum[-1] - prefix_sum[i] 23 | if left >= right: 24 | cnt += 1 25 | return cnt 26 | 27 | 28 | # @lc code=end 29 | -------------------------------------------------------------------------------- /src/861.score-after-flipping-matrix.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=861 lang=python3 3 | # 4 | # [861] Score After Flipping Matrix 5 | # 6 | 7 | 8 | # @lc code=start 9 | # TAGS: Array, Greedy, Bit Manipulation, Matrix 10 | class Solution: 11 | def matrixScore(self, grid: List[List[int]]) -> int: 12 | R, C = len(grid), len(grid[0]) 13 | for r in range(R): 14 | if grid[r][0] == 0: 15 | for c in range(C): 16 | grid[r][c] = 1 - grid[r][c] 17 | ans = 0 18 | for c in range(C): 19 | ones = sum(grid[r][c] == 1 for r in range(R)) 20 | ones = max(ones, R - ones) 21 | ans += ones * 1 << (C - c - 1) 22 | return ans 23 | 24 | 25 | # @lc code=end 26 | -------------------------------------------------------------------------------- /src/91.decode-ways.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=91 lang=python3 3 | # 4 | # [91] Decode Ways 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: String, Dynamic Programming 9 | class Solution: 10 | # 28 ms, 86.26%. topdown with memoization. Time and Space: O(N). 11 | def numDecodings(self, s: str) -> int: 12 | visited = {} 13 | def dfs(i = 0): 14 | if i in visited: return visited[i] 15 | if i >= len(s): return 1 16 | if s[i] == '0': return 0 17 | rv = dfs(i + 1) 18 | if i + 1 < len(s) and int("".join(s[i : i + 2])) <= 26: 19 | rv += dfs(i + 2) 20 | visited[i] = rv 21 | return rv 22 | return dfs() 23 | 24 | # @lc code=end 25 | 26 | -------------------------------------------------------------------------------- /testcases/399: -------------------------------------------------------------------------------- 1 | [["a","b"],["b","c"]] 2 | [2.0,3.0] 3 | [["a","c"],["b","a"],["a","e"],["a","a"],["x","x"]] 4 | [["a","b"],["b","c"],["bc","cd"]] 5 | [1.5,2.5,5.0] 6 | [["a","c"],["c","b"],["bc","cd"],["cd","bc"]] 7 | [["a","b"]] 8 | [0.5] 9 | [["a","b"],["b","a"],["a","c"],["x","y"]] 10 | [["a","b"],["b","c"],["c","d"]] 11 | [2.0,3.0, 4.0] 12 | [["a","c"],["b","a"],["a","e"],["a","a"],["x","x"],["a","d"], ["d", "a"]] 13 | [["a","e"],["b","e"]] 14 | [4.0,3.0] 15 | [["a","b"],["e","e"],["x","x"]] 16 | [["a","b"],["c","d"]] 17 | [1.0,1.0] 18 | [["a","c"],["b","d"],["b","a"],["d","c"]] 19 | [["x1","x2"],["x2","x3"],["x1","x4"],["x2","x5"]] 20 | [3.0,0.5,3.4,5.6] 21 | [["x2","x4"],["x1","x5"],["x1","x3"],["x5","x5"],["x5","x1"],["x3","x4"],["x4","x3"],["x6","x6"],["x0","x0"]] -------------------------------------------------------------------------------- /src/1860.incremental-memory-leak.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=1860 lang=python3 3 | # 4 | # [1860] Incremental Memory Leak 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: Math 9 | 10 | 11 | class Solution: 12 | """ 13 | There is a Math solution with O(1) time complexity 14 | """ 15 | # 296 ms, 80.05%. Brute Force. Time: O(logN). Space: O(1) 16 | 17 | def memLeak(self, memory1: int, memory2: int) -> List[int]: 18 | i = 0 19 | while 1: 20 | i += 1 21 | if memory1 >= memory2 and memory1 >= i: 22 | memory1 -= i 23 | elif memory2 >= memory1 and memory2 >= i: 24 | memory2 -= i 25 | else: 26 | return i, memory1, memory2 27 | 28 | # @lc code=end 29 | -------------------------------------------------------------------------------- /src/20.valid-parentheses.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=20 lang=python3 3 | # 4 | # [20] Valid Parentheses 5 | # 6 | # TAGS: String, Stack 7 | # @lc code=start 8 | class Solution: 9 | # time complexity: O(N). N = len(s) 10 | # Space complexity: O(N). N = len(s) 11 | # Other ways: Use map, constant space 12 | def isValid(self, s: str) -> bool: 13 | stack = [] 14 | for c in s: 15 | if len(stack): 16 | if (c == ")" and stack[-1] == "(") \ 17 | or (c == "]" and stack[-1] == "[") \ 18 | or (c == "}" and stack[-1] == "{"): 19 | stack.pop() 20 | continue 21 | stack.append(c) 22 | return len(stack) == 0 23 | 24 | # @lc code=end -------------------------------------------------------------------------------- /src/24.swap-nodes-in-pairs.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=24 lang=python3 3 | # 4 | # [24] Swap Nodes in Pairs 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: Linked List 9 | # Definition for singly-linked list. 10 | # class ListNode: 11 | # def __init__(self, val=0, next=None): 12 | # self.val = val 13 | # self.next = next 14 | class Solution: 15 | # 24 ms, 94.63%. Time: O(N). Space: O(N) 16 | def swapPairs(self, head: ListNode) -> ListNode: 17 | def dfs(node): 18 | if not node or not node.next: return node 19 | cur = node 20 | nxt = node.next 21 | cur.next = dfs(nxt.next) 22 | nxt.next = cur 23 | return nxt 24 | return dfs(head) 25 | # @lc code=end 26 | 27 | -------------------------------------------------------------------------------- /src/231.power-of-two.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=231 lang=python3 3 | # 4 | # [231] Power of Two 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: Bit Manipulation 9 | # REVIEWME: important technique in bitwise manipulation 10 | class Solution: 11 | # Naive solution. Time O(log N), Space O(1) 12 | def isPowerOfTwo(self, n: int) -> bool: 13 | while n: 14 | if n == 1: 15 | return True 16 | elif n % 2: 17 | return False 18 | n /= 2 19 | return False 20 | 21 | # Better solution. Turn off right most bit and compare with 0 22 | def isPowerOfTwo(self, n: int) -> bool: 23 | if n == 0: 24 | return False 25 | return n & (n - 1) == 0 26 | # @lc code=end 27 | 28 | -------------------------------------------------------------------------------- /src/611.valid-triangle-number.py: -------------------------------------------------------------------------------- 1 | # 2 | # @lc app=leetcode id=611 lang=python3 3 | # 4 | # [611] Valid Triangle Number 5 | # 6 | 7 | # @lc code=start 8 | # TAGS: Array, Two Pointers, Binary Search, Greedy, Sorting 9 | # REVIEWME: Two Pointers, Greedy 10 | 11 | 12 | class Solution: 13 | # 1140 ms, 64.29%. Time: O(N^2). Space: O(1) 14 | def triangleNumber(self, nums: List[int]) -> int: 15 | nums.sort() 16 | total = 0 17 | for k in range(2, len(nums)): 18 | i, j = 0, k - 1 19 | while j > i: 20 | if nums[i] + nums[j] > nums[k]: 21 | total += j - i 22 | j -= 1 23 | else: 24 | i += 1 25 | return total 26 | # @lc code=end 27 | --------------------------------------------------------------------------------