├── .github ├── ISSUE_TEMPLATE │ ├── bug_report.md │ ├── feature_request.md │ └── script-addition.md ├── auto_assign.yml ├── config.yml └── pullrequest_template.md ├── .vscode ├── c_cpp_properties.json ├── launch.json └── settings.json ├── 03NextGreaterEle.cpp ├── 04MedianofTwoSortedArrays.cpp ├── ALGORITHMS ├── 01.Searching Algorithms │ ├── C++ │ │ ├── Binary Search.cpp │ │ ├── Linear Search.cpp │ │ ├── Ternary Search.cpp │ │ └── context.txt │ ├── C │ │ ├── Binary Search.c │ │ ├── Linear Search.c │ │ └── contents.txt │ ├── Java │ │ ├── BinarySearch.java │ │ ├── Linear search │ │ └── context.txt │ └── Python │ │ ├── Binary Search.py │ │ ├── Linear Search.py │ │ ├── Ternary-search.py │ │ └── context.txt ├── 02.Sorting Algorithms │ ├── C++ │ │ ├── Bucket Sort │ │ ├── Radix Sort │ │ ├── Selectionn Sort.cpp │ │ ├── bubble_sort.cpp │ │ ├── context.txt │ │ ├── countingSort.cpp │ │ ├── countingSort.exe │ │ ├── heapSort.cpp │ │ ├── insertion_sort.cpp │ │ ├── merge_sort.cpp │ │ ├── quickSort.cpp │ │ └── shellSort.cpp │ ├── C │ │ ├── 10_bucketsort.c │ │ ├── 1_insertionsort.c │ │ ├── 2_mergesort.c │ │ ├── 3_quicksort_hoares.c │ │ ├── 4_quicksort_cormen.c │ │ ├── 6_heapsort.c │ │ ├── 8_countingsort.c │ │ ├── 9_radixsort.c │ │ ├── Selection Sort.c │ │ └── context.txt │ ├── Java │ │ ├── Insertion Sort │ │ ├── Quick sort │ │ ├── Shell sort │ │ ├── bubblesort.java │ │ └── context.txt │ └── Python │ │ ├── BubbleSort.py │ │ ├── Quicksort.py │ │ ├── Selection Sort.py │ │ ├── bubble_sort.py │ │ ├── context.txt │ │ └── insertion_sort.py ├── 03.Divide and Conquer │ ├── C++ │ │ └── context.txt │ ├── C │ │ └── context.txt │ ├── Java │ │ └── context.txt │ └── Python │ │ └── context.txt ├── 04.Bitwise Algorithms │ ├── C++ │ │ ├── calc_set_bit.cpp │ │ ├── calc_set_bit.exe │ │ ├── cnvrt_one_numtoanother.cpp │ │ ├── cnvrt_one_numtoanother.exe │ │ ├── context.txt │ │ ├── even_odd.cpp │ │ ├── find_unique.cpp │ │ ├── find_unique.exe │ │ ├── n_n-1.cpp │ │ └── n_n-1.exe │ ├── C │ │ └── context.txt │ ├── Java │ │ └── context.txt │ └── Python │ │ └── context.txt ├── 05.Greedy Algorithms │ ├── C++ │ │ ├── context.txt │ │ ├── krushkalsAlgo.cpp │ │ └── primsAlgo.cpp │ ├── C │ │ └── context.txt │ ├── Java │ │ └── context.txt │ └── Python │ │ └── context.txt ├── 06.Recursion │ ├── C++ │ │ └── context.txt │ ├── C │ │ └── context.txt │ ├── Java │ │ └── context.txt │ └── Python │ │ └── context.txt ├── 07.BackTracking Algorithms │ ├── C++ │ │ ├── N Queen Problem │ │ ├── Rat-in-the-maze.cpp │ │ └── context.txt │ ├── C │ │ └── context.txt │ ├── Java │ │ └── context.txt │ └── Python │ │ ├── Rat-in-the-maze.py │ │ └── context.txt ├── 08.Dynamic Programming │ ├── C++ │ │ ├── Maximum Square Matrix With All Zeros.c │ │ ├── Maximum_Square_Matrix_With_All_Zeros.cpp │ │ ├── Multistage graph │ │ ├── Nth Catalan.cpp │ │ ├── context.txt │ │ ├── fibonacci_memoization.cpp │ │ └── fibonacci_tabulation.cpp │ ├── C │ │ └── context.txt │ ├── Java │ │ ├── MinimumPathSum.java │ │ └── context.txt │ └── Python │ │ └── context.txt └── 09.Sieve of Eratosthenes │ └── Sieve of Eratosthenes.cpp ├── CODECHEF ├── 1) BottomViewOfBinaryTree.cpp ├── 2) CeilinginBST.cpp ├── 3) CheckForBST.cpp ├── 6) PairSum.cpp ├── 8) VerticalSumOfBinaryTree.cpp ├── Array Equality.cpp ├── ComputerGame.cpp ├── DSA_12.c ├── DSA_2.c ├── DSA_4.c ├── DSA_5.c ├── DeleteTwoElements.cpp ├── Palin.cpp ├── REMOVECARDS.cpp ├── RING_GAME.cpp ├── Reverse_Array.cpp ├── Tree Coloring.cpp ├── Vaccine.cpp ├── codechef_alternating_strings.cpp ├── codechef_car_choice.c ├── contents.txt ├── numberconversion.cpp └── tut14.cpp ├── CODEFORCES ├── B_Also_Try_Minecraft.cpp ├── B_Getting_Zero.cpp ├── CodeforcesCodes │ ├── 110A - Nearly Lucky Number.cpp │ ├── 112A - Petya and Strings.cpp │ ├── 116A - Tram.cpp │ ├── 136A - Presents.cpp │ ├── 141A - Amusing Joke.cpp │ ├── 144A - Arrival of the General.cpp │ ├── 148A - Insomnia cure.cpp │ ├── 155A - I_love_%username%.cpp │ ├── 158A - Next Round.cpp │ ├── 1647A.CPP │ ├── 1735A.cpp │ ├── 1735B.cpp │ ├── 200B - Drinks.cpp │ ├── 228A - Is your horseshoe on the other hoof.cpp │ ├── 231A - Team.cpp │ ├── 233A - Perfect Permutation.cpp │ ├── 236A - Boy or Girl.cpp │ ├── 263A - Beautiful Matrix.cpp │ ├── 266A - Stones on the Table.cpp │ ├── 281A - Word Capitalization.cpp │ ├── 282A - Bit++.cpp │ ├── 4A - Watermelon.cpp │ ├── 50A - Domino piling.cpp │ ├── 59A - Word.cpp │ ├── 61A - Ultra-Fast Mathematician.cpp │ └── 71A - Way Too Long Words.cpp ├── Codeforces_#824 │ ├── ProblemA.cpp │ ├── ProblemB.cpp │ └── ProblemC.cpp ├── Codeforces_Round _#751 │ ├── ProblemA.cpp │ ├── ProblemB.cpp │ └── ProblemC.cpp ├── DoorsandKeys.cpp ├── EpicGame.cpp ├── Immobile_Knight.cpp ├── Odd_Divisor.cpp ├── contents.txt └── imp_TheatreSquare.cpp ├── CODE_OF_CONDUCT.md ├── CONTRIBUTING_GUIDELINES.md ├── DATA STRUCTURES ├── 01.Arrays │ ├── C++ │ │ ├── All pair whose sum is k.cpp │ │ ├── Array subset of anothe array.cpp │ │ ├── Best time to buy stock.cpp │ │ ├── Choclate distribution problem.cpp │ │ ├── Common element in 3 sorted arrays.cpp │ │ ├── Count inversion.cpp │ │ ├── Cyclically rotate by 1.cpp │ │ ├── Kadane's algorithms.cpp │ │ ├── Kth max min.cpp │ │ ├── Largest continous sum.cpp │ │ ├── Longest consequecutive subsequence.cpp │ │ ├── Maximum subarray product.cpp │ │ ├── Median of two sorted arrays (diff size).cpp │ │ ├── Median of two sorted arrays (equal size).cpp │ │ ├── Merge 2 sorted arrays.cpp │ │ ├── Merge intervals.cpp │ │ ├── Min swaps requires to group together.cpp │ │ ├── Minimize the height.cpp │ │ ├── Minimum jumps to reach end.cpp │ │ ├── Negative element to one side of array.cpp │ │ ├── Next permutation.cpp │ │ ├── Rearrange position neg in alter.cpp │ │ ├── Reverse array.cpp │ │ ├── Sell buy stock almost twice.cpp │ │ ├── Smallest subarray with sum greater than k.cpp │ │ ├── Subarray with sum 0.cpp │ │ ├── Three way partitioning array.cpp │ │ ├── Trap rain water problem.cpp │ │ ├── Triplet of element sum.cpp │ │ ├── Union and intersection.cpp │ │ └── contents.txt │ ├── C │ │ └── contents.txt │ ├── Java │ │ └── contents.txt │ └── Python │ │ └── contents.txt ├── 02.2D Arrays │ ├── C++ │ │ ├── Matrix search.cpp │ │ └── contents.txt │ ├── C │ │ └── contents.txt │ ├── Java │ │ └── contents.txt │ └── Python │ │ └── contents.txt ├── 03.Linked List │ ├── C++ │ │ ├── Remove Duplicates from singly-linked list │ │ └── contents.txt │ ├── C │ │ ├── contents.txt │ │ └── linked_list.c │ ├── Java │ │ └── contents.txt │ └── Python │ │ └── contents.txt ├── 04.Stack │ ├── C++ │ │ └── contents.txt │ ├── C │ │ ├── Reverse_a_String.c │ │ └── contents.txt │ ├── Java │ │ └── contents.txt │ └── Python │ │ └── contents.txt ├── 05.Queue │ ├── C++ │ │ └── contents.txt │ ├── C │ │ └── contents.txt │ ├── Java │ │ └── contents.txt │ └── Python │ │ └── contents.txt ├── 06.Binary Trees │ ├── C++ │ │ └── contents.txt │ ├── C │ │ └── contents.txt │ ├── Java │ │ └── contents.txt │ └── Python │ │ └── contents.txt ├── 07.Binary Search Trees │ ├── C++ │ │ └── contents.txt │ ├── C │ │ └── contents.txt │ ├── Java │ │ └── contents.txt │ └── Python │ │ └── contents.txt ├── 08.Heap │ ├── C++ │ │ └── contents.txt │ ├── C │ │ └── contents.txt │ ├── Java │ │ └── contents.txt │ └── Python │ │ └── contents.txt ├── 09.Hashing │ ├── C++ │ │ └── contents.txt │ ├── C │ │ └── contents.txt │ ├── Java │ │ └── contents.txt │ └── Python │ │ └── contents.txt ├── 10.Graphs │ ├── C++ │ │ ├── contents.txt │ │ ├── dijkstra's_algorithm.cpp │ │ └── kruskal's_algorithm.cpp │ ├── C │ │ └── contents.txt │ ├── Java │ │ └── contents.txt │ └── Python │ │ └── contents.txt └── contents.txt ├── GFG ├── contents.txt ├── insertion_sort.cpp └── merge_sort.cpp ├── Graph Algorithms ├── BFS for a Graph.txt ├── Bellman Ford Algorithm.cpp ├── Detecting a cycle in a graph (Using three color mechanism).cpp ├── DisjointSetUnion.cpp └── code.cpp ├── Groups.cpp ├── HACKEREARTH └── contents.txt ├── HACKERRANK └── contents.txt ├── Heapsort.py ├── Insertion&Searching_BST.c ├── LEETCODE ├── 198-house-robber │ ├── 198-house-robber.cpp │ ├── NOTES.md │ └── README.md ├── 213-house-robber-ii │ ├── 213-house-robber-ii.cpp │ ├── NOTES.md │ └── README.md ├── 3Sum.java ├── C++ │ ├── 01-matrix.cpp │ ├── 1-bit-and-2-bit-characters.cpp │ ├── 132-pattern.cpp │ ├── 2-keys-keyboard.cpp │ ├── 24-game.cpp │ ├── 3sum-closest.cpp │ ├── 3sum-smaller.cpp │ ├── 3sum-with-multiplicity.cpp │ ├── 3sum.cpp │ ├── 4-keys-keyboard.cpp │ ├── 4sum-ii.cpp │ ├── 4sum.cpp │ ├── AddTwoNumbers.cpp │ ├── Integer_to_Roman.cpp │ ├── Median_of_Two_Sorted_Arrays.cpp │ ├── TwoSum.cpp │ ├── accounts-merge.cpp │ ├── add-and-search-word-data-structure-design.cpp │ ├── add-binary.cpp │ ├── add-bold-tag-in-string.cpp │ ├── add-digits.cpp │ ├── add-one-row-to-tree.cpp │ ├── add-strings.cpp │ ├── add-to-array-form-of-integer.cpp │ ├── add-two-numbers-ii.cpp │ ├── add-two-numbers.cpp │ ├── add-two-polynomials-represented-as-linked-lists.cpp │ ├── adding-two-negabinary-numbers.cpp │ ├── additive-number.cpp │ ├── advantage-shuffle.cpp │ ├── airplane-seat-assignment-probability.cpp │ ├── alert-using-same-key-card-three-or-more-times-in-a-one-hour-period.cpp │ ├── alien-dictionary.cpp │ ├── all-elements-in-two-binary-search-trees.cpp │ ├── all-nodes-distance-k-in-binary-tree.cpp │ ├── all-oone-data-structure.cpp │ ├── all-paths-from-source-lead-to-destination.cpp │ ├── all-paths-from-source-to-target.cpp │ ├── all-possible-full-binary-trees.cpp │ ├── allocate-mailboxes.cpp │ ├── alphabet-board-path.cpp │ ├── ambiguous-coordinates.cpp │ ├── analyze-user-website-visit-pattern.cpp │ ├── android-unlock-patterns.cpp │ ├── angle-between-hands-of-a-clock.cpp │ ├── apply-discount-every-n-orders.cpp │ ├── arithmetic-slices-ii-subsequence.cpp │ ├── arithmetic-slices.cpp │ ├── arithmetic-subarrays.cpp │ ├── armstrong-number.cpp │ ├── arranging-coins.cpp │ ├── array-nesting.cpp │ ├── array-of-doubled-pairs.cpp │ ├── array-partition-i.cpp │ ├── array-transformation.cpp │ ├── as-far-from-land-as-possible.cpp │ ├── assign-cookies.cpp │ ├── asteroid-collision.cpp │ ├── available-captures-for-rook.cpp │ ├── average-of-levels-in-binary-tree.cpp │ ├── average-salary-excluding-the-minimum-and-maximum-salary.cpp │ ├── average-waiting-time.cpp │ ├── avoid-flood-in-the-city.cpp │ ├── backspace-string-compare.cpp │ ├── bag-of-tokens.cpp │ ├── balance-a-binary-search-tree.cpp │ ├── base-7.cpp │ ├── baseball-game.cpp │ ├── basic-calculator-ii.cpp │ ├── basic-calculator-iii.cpp │ ├── basic-calculator-iv.cpp │ ├── basic-calculator.cpp │ ├── battleships-in-a-board.cpp │ ├── beautiful-arrangement-ii.cpp │ ├── beautiful-arrangement.cpp │ ├── beautiful-array.cpp │ ├── before-and-after-puzzle.cpp │ ├── best-meeting-point.cpp │ ├── best-position-for-a-service-centre.cpp │ ├── best-sightseeing-pair.cpp │ ├── best-team-with-no-conflicts.cpp │ ├── best-time-to-buy-and-sell-stock-ii.cpp │ ├── best-time-to-buy-and-sell-stock-iv.cpp │ ├── best-time-to-buy-and-sell-stock-with-cooldown.cpp │ ├── best-time-to-buy-and-sell-stock-with-transaction-fee.cpp │ ├── best-time-to-buy-and-sell-stock.cpp │ ├── binary-gap.cpp │ ├── binary-number-with-alternating-bits.cpp │ ├── binary-prefix-divisible-by-5.cpp │ ├── binary-search-tree-iterator-ii.cpp │ ├── binary-search-tree-iterator.cpp │ ├── binary-search-tree-to-greater-sum-tree.cpp │ ├── binary-search.cpp │ ├── binary-string-with-substrings-representing-1-to-n.cpp │ ├── binary-subarrays-with-sum.cpp │ ├── binary-tree-cameras.cpp │ ├── binary-tree-coloring-game.cpp │ ├── binary-tree-inorder-traversal.cpp │ ├── binary-tree-level-order-traversal-ii.cpp │ ├── binary-tree-level-order-traversal.cpp │ ├── binary-tree-longest-consecutive-sequence-ii.cpp │ ├── binary-tree-longest-consecutive-sequence.cpp │ ├── binary-tree-maximum-path-sum.cpp │ ├── binary-tree-paths.cpp │ ├── binary-tree-postorder-traversal.cpp │ ├── binary-tree-preorder-traversal.cpp │ ├── binary-tree-pruning.cpp │ ├── binary-tree-tilt.cpp │ ├── binary-tree-vertical-order-traversal.cpp │ ├── binary-trees-with-factors.cpp │ ├── binary-watch.cpp │ ├── bitwise-and-of-numbers-range.cpp │ ├── bitwise-ors-of-subarrays.cpp │ ├── boats-to-save-people.cpp │ ├── bold-words-in-string.cpp │ ├── bomb-enemy.cpp │ ├── boundary-of-binary-tree.cpp │ ├── brace-expansion-ii.cpp │ ├── brace-expansion.cpp │ ├── break-a-palindrome.cpp │ ├── brick-wall.cpp │ ├── bricks-falling-when-hit.cpp │ ├── broken-calculator.cpp │ ├── buddy-strings.cpp │ ├── build-an-array-with-stack-operations.cpp │ ├── build-array-where-you-can-find-the-maximum-exactly-k-comparisons.cpp │ ├── build-binary-expression-tree-from-infix-expression.cpp │ ├── building-boxes.cpp │ ├── building-h2o.cpp │ ├── buildings-with-an-ocean-view.cpp │ ├── bulb-switcher-ii.cpp │ ├── bulb-switcher-iii.cpp │ ├── bulb-switcher-iv.cpp │ ├── bulb-switcher.cpp │ ├── bulls-and-cows.cpp │ ├── burst-balloons.cpp │ ├── bus-routes.cpp │ ├── calculate-money-in-leetcode-bank.cpp │ ├── camelcase-matching.cpp │ ├── campus-bikes-ii.cpp │ ├── campus-bikes.cpp │ ├── can-convert-string-in-k-moves.cpp │ ├── can-i-win.cpp │ ├── can-make-arithmetic-progression-from-sequence.cpp │ ├── can-make-palindrome-from-substring.cpp │ ├── can-place-flowers.cpp │ ├── can-you-eat-your-favorite-candy-on-your-favorite-day.cpp │ ├── canCompleteCircuit.cpp │ ├── candy-crush.cpp │ ├── candy.cpp │ ├── capacity-to-ship-packages-within-d-days.cpp │ ├── car-fleet-ii.cpp │ ├── car-fleet.cpp │ ├── car-pooling.cpp │ ├── card-flipping-game.cpp │ ├── cat-and-mouse-ii.cpp │ ├── cat-and-mouse.cpp │ ├── cells-with-odd-values-in-a-matrix.cpp │ ├── chalkboard-xor-gam.cpp │ ├── champagne-tower.cpp │ ├── change-minimum-characters-to-satisfy-one-of-three-conditions.cpp │ ├── change-the-root-of-a-binary-tree.cpp │ ├── cheapest-flights-within-k-stops.cpp │ ├── check-array-formation-through-concatenation.cpp │ ├── check-completeness-of-a-binary-tree.cpp │ ├── check-if-a-number-is-majority-element-in-a-sorted-array.cpp │ ├── check-if-a-string-can-break-another-string.cpp │ ├── check-if-a-string-contains-all-binary-codes-of-size-k.cpp │ ├── check-if-a-string-is-a-valid-sequence-from-root-to-leaves-path-in-a-binary-tree.cpp │ ├── check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence.cpp │ ├── check-if-all-1s-are-at-least-length-k-places-away.cpp │ ├── check-if-array-is-sorted-and-rotated.cpp │ ├── check-if-array-pairs-are-divisible-by-k.cpp │ ├── check-if-binary-string-has-at-most-one-segment-of-ones.cpp │ ├── check-if-it-is-a-good-array.cpp │ ├── check-if-it-is-a-straight-line.cpp │ ├── check-if-n-and-its-double-exist.cpp │ ├── check-if-number-is-a-sum-of-powers-of-three.cpp │ ├── check-if-one-string-swap-can-make-strings-equal.cpp │ ├── check-if-string-is-transformable-with-substring-sort-operations.cpp │ ├── check-if-the-sentence-is-pangram.cpp │ ├── check-if-there-is-a-valid-path-in-a-grid.cpp │ ├── check-if-two-expression-trees-are-equivalent.cpp │ ├── check-if-two-string-arrays-are-equivalent.cpp │ ├── check-if-word-is-valid-after-substitutions.cpp │ ├── checking-existence-of-edge-length-limited-paths-ii.cpp │ ├── checking-existence-of-edge-length-limited-paths.cpp │ ├── cherry-pickup-ii.cpp │ ├── cherry-pickup.cpp │ ├── cinema-seat-allocation.cpp │ ├── circle-and-rectangle-overlapping.cpp │ ├── circular-array-loop.cpp │ ├── circular-permutation-in-binary-representation.cpp │ ├── climbing-stairs.cpp │ ├── clone-binary-tree-with-random-pointer.cpp │ ├── clone-n-ary-tree.cpp │ ├── cloneGraph.cpp │ ├── closest-binary-search-tree-value-ii.cpp │ ├── closest-binary-search-tree-value.cpp │ ├── closest-dessert-cost.cpp │ ├── closest-divisors.cpp │ ├── closest-leaf-in-a-binary-tree.cpp │ ├── closest-room.cpp │ ├── closest-subsequence-sum.cpp │ ├── clumsy-factorial.cpp │ ├── coin-change-2.cpp │ ├── coin-change.cpp │ ├── coin-path.cpp │ ├── coloring-a-border.cpp │ ├── combination-sum-iii.cpp │ ├── combination-sum-iv.cpp │ ├── combinationSum.cpp │ ├── combinationSum2.cpp │ ├── combinations.cpp │ ├── compare-strings-by-frequency-of-the-smallest-character.cpp │ ├── compare-version-numbers.cpp │ ├── complement-of-base-10-integer.cpp │ ├── complete-binary-tree-inserter.cpp │ ├── complex-number-multiplication.cpp │ ├── concatenated-words.cpp │ ├── concatenation-of-consecutive-binary-numbers.cpp │ ├── confusing-number-ii.cpp │ ├── confusing-number.cpp │ ├── connect.cpp │ ├── connecting-cities-with-minimum-cost.cpp │ ├── consecutive-characters.cpp │ ├── consecutive-numbers-sum.cpp │ ├── constrained-subset-sum.cpp │ ├── construct-binary-search-tree-from-preorder-traversal.cpp │ ├── construct-binary-tree-from-inorder-and-postorder-traversal.cpp │ ├── construct-binary-tree-from-preorder-and-inorder-traversal.cpp │ ├── construct-binary-tree-from-preorder-and-postorder-traversal.cpp │ ├── construct-binary-tree-from-string.cpp │ ├── construct-k-palindrome-strings.cpp │ ├── construct-quad-tree.cpp │ ├── construct-string-from-binary-tree.cpp │ ├── construct-target-array-with-multiple-sums.cpp │ ├── construct-the-lexicographically-largest-valid-sequence.cpp │ ├── construct-the-rectangle.cpp │ ├── contain-virus.cpp │ ├── container-with-most-water.cpp │ ├── contains-duplicate-ii.cpp │ ├── contains-duplicate-iii.cpp │ ├── contains-duplicate.cpp │ ├── contiguous-array.cpp │ ├── continuous-subarray-sum.cpp │ ├── convert-a-number-to-hexadecimal.cpp │ ├── convert-binary-number-in-a-linked-list-to-integer.cpp │ ├── convert-binary-search-tree-to-sorted-doubly-linked-list.cpp │ ├── convert-bst-to-greater-tree.cpp │ ├── convert-integer-to-the-sum-of-two-no-zero-integers.cpp │ ├── convert-sorted-array-to-binary-search-tree.cpp │ ├── convert-sorted-list-to-binary-search-tree.cpp │ ├── convert-to-base-2.cpp │ ├── convert.cpp │ ├── convex-polygon.cpp │ ├── coordinate-with-maximum-network-quality.cpp │ ├── copy-list-with-random-pointer.cpp │ ├── corporate-flight-bookings.cpp │ ├── correct-a-binary-tree.cpp │ ├── count-all-possible-routes.cpp │ ├── count-all-valid-pickup-and-delivery-options.cpp │ ├── count-and-say.cpp │ ├── count-binary-substrings.cpp │ ├── count-complete-tree-nodes.cpp │ ├── count-different-palindromic-subsequences.cpp │ ├── count-good-meals.cpp │ ├── count-good-nodes-in-binary-tree.cpp │ ├── count-good-triplets.cpp │ ├── count-items-matching-a-rule.cpp │ ├── count-largest-group.cpp │ ├── count-negative-numbers-in-a-sorted-matrix.cpp │ ├── count-nice-pairs-in-an-array.cpp │ ├── count-number-of-homogenous-substrings.cpp │ ├── count-number-of-nice-subarrays.cpp │ ├── count-number-of-teams.cpp │ ├── count-numbers-with-unique-digits.cpp │ ├── count-odd-numbers-in-an-interval-range.cpp │ ├── count-of-matches-in-tournament.cpp │ ├── count-of-range-sum.cpp │ ├── count-of-smaller-numbers-after-self.cpp │ ├── count-pairs-of-equal-substrings-with-minimum-difference.cpp │ ├── count-pairs-of-nodes.cpp │ ├── count-pairs-with-xor-in-a-range.cpp │ ├── count-primes.cpp │ ├── count-servers-that-communicate.cpp │ ├── count-sorted-vowel-strings.cpp │ ├── count-square-submatrices-with-all-ones.cpp │ ├── count-submatrices-with-all-ones.cpp │ ├── count-substrings-that-differ-by-one-character.cpp │ ├── count-substrings-with-only-one-distinct-letter.cpp │ ├── count-subtrees-with-max-distance-between-cities.cpp │ ├── count-the-number-of-consistent-strings.cpp │ ├── count-the-repetitions.cpp │ ├── count-triplets-that-can-form-two-arrays-of-equal-xor.cpp │ ├── count-unhappy-friends.cpp │ ├── count-unique-characters-of-all-substrings-of-a-given-string.cpp │ ├── count-univalue-subtrees.cpp │ ├── count-vowels-permutation.cpp │ ├── count-ways-to-distribute-candies.cpp │ ├── count-ways-to-make-array-with-product.cpp │ ├── counting-bits.cpp │ ├── counting-elements.cpp │ ├── couples-holding-hands.cpp │ ├── course-schedule-ii.cpp │ ├── course-schedule-iii.cpp │ ├── course-schedule-iv.cpp │ ├── course-schedule.cpp │ ├── cousins-in-binary-tree.cpp │ ├── cracking-the-safe.cpp │ ├── crawler-log-folder.cpp │ ├── create-maximum-number.cpp │ ├── create-sorted-array-through-instructions.cpp │ ├── create-target-array-in-the-given-order.cpp │ ├── critical-connections-in-a-network.cpp │ ├── custom-sort-string.cpp │ ├── cut-off-trees-for-golf-event.cpp │ ├── daily-temperatures.cpp │ ├── data-stream-as-disjoint-intervals.cpp │ ├── day-of-the-week.cpp │ ├── day-of-the-year.cpp │ ├── decode-string.cpp │ ├── decode-ways-ii.cpp │ ├── decode-ways.cpp │ ├── decode-xored-array.cpp │ ├── decode-xored-permutation.cpp │ ├── decoded-string-at-index.cpp │ ├── decompress-run-length-encoded-list.cpp │ ├── decrease-elements-to-make-array-zigzag.cpp │ ├── decrypt-string-from-alphabet-to-integer-mapping.cpp │ ├── deepest-leaves-sum.cpp │ ├── defanging-an-ip-address.cpp │ ├── defuse-the-bomb.cpp │ ├── degree-of-an-array.cpp │ ├── delete-and-earn.cpp │ ├── delete-columns-to-make-sorted-ii.cpp │ ├── delete-columns-to-make-sorted-iii.cpp │ ├── delete-columns-to-make-sorted.cpp │ ├── delete-leaves-with-a-given-value.cpp │ ├── delete-n-nodes-after-m-nodes-of-a-linked-list.cpp │ ├── delete-node-in-a-bst.cpp │ ├── delete-node-in-a-linked-list.cpp │ ├── delete-nodes-and-return-forest.cpp │ ├── delete-operation-for-two-strings.cpp │ ├── delete-tree-nodes.cpp │ ├── delivering-boxes-from-storage-to-ports.cpp │ ├── design-a-file-sharing-system.cpp │ ├── design-a-leaderboard.cpp │ ├── design-a-stack-with-increment-operation.cpp │ ├── design-an-expression-tree-with-evaluate-function.cpp │ ├── design-an-ordered-stream.cpp │ ├── design-authentication-manager.cpp │ ├── design-bounded-blocking-queue.cpp │ ├── design-browser-history.cpp │ ├── design-circular-deque.cpp │ ├── design-circular-queue.cpp │ ├── design-compressed-string-iterator.cpp │ ├── design-excel-sum-formula.cpp │ ├── design-file-system.cpp │ ├── design-front-middle-back-queue.cpp │ ├── design-hashmap.cpp │ ├── design-hashset.cpp │ ├── design-hit-counter.cpp │ ├── design-in-memory-file-system.cpp │ ├── design-linked-list.cpp │ ├── design-log-storage-system.cpp │ ├── design-most-recently-used-queue.cpp │ ├── design-parking-system.cpp │ ├── design-phone-directory.cpp │ ├── design-search-autocomplete-system.cpp │ ├── design-skiplist.cpp │ ├── design-snake-game.cpp │ ├── design-tic-tac-toe.cpp │ ├── design-twitter.cpp │ ├── design-underground-system.cpp │ ├── destination-city.cpp │ ├── detect-capital.cpp │ ├── detect-cycles-in-2d-grid.cpp │ ├── detect-pattern-of-length-m-repeated-k-or-more-times.cpp │ ├── determine-color-of-a-chessboard-square.cpp │ ├── determine-if-string-halves-are-alike.cpp │ ├── determine-if-two-strings-are-close.cpp │ ├── di-string-match.cpp │ ├── diagonal-traverse-ii.cpp │ ├── diagonal-traverse.cpp │ ├── diameter-of-binary-tree.cpp │ ├── diameter-of-n-ary-tree.cpp │ ├── dice-roll-simulation.cpp │ ├── diet-plan-performance.cpp │ ├── different-ways-to-add-parentheses.cpp │ ├── digit-count-in-range.cpp │ ├── dinner-plate-stacks.cpp │ ├── display-table-of-food-orders-in-a-restaurant.cpp │ ├── distance-between-bus-stops.cpp │ ├── distant-barcodes.cpp │ ├── distinct-echo-substrings.cpp │ ├── distinct-numbers-in-each-subarray.cpp │ ├── distinct-subsequences-ii.cpp │ ├── distribute-candies-to-people.cpp │ ├── distribute-candies.cpp │ ├── distribute-coins-in-binary-tree.cpp │ ├── distribute-repeating-integers.cpp │ ├── divide-array-in-sets-of-k-consecutive-numbers.cpp │ ├── divide-array-into-increasing-sequences.cpp │ ├── divide-chocolate.cpp │ ├── divide-two-integers.cpp │ ├── divisor-game.cpp │ ├── domino-and-tromino-tiling.cpp │ ├── dot-product-of-two-sparse-vectors.cpp │ ├── dota2-senate.cpp │ ├── duplicate-zeros.cpp │ ├── element-appearing-more-than-25-in-sorted-array.cpp │ ├── elimination-game.cpp │ ├── employee-free-time.cpp │ ├── employee-importance.cpp │ ├── encode-and-decode-strings.cpp │ ├── encode-and-decode-tinyurl.cpp │ ├── encode-n-ary-tree-to-binary-tree.cpp │ ├── encode-number.cpp │ ├── encode-string-with-shortest-length.cpp │ ├── equal-rational-numbers.cpp │ ├── equal-sum-arrays-with-minimum-number-of-operations.cpp │ ├── equal-tree-partition.cpp │ ├── erect-the-fence.cpp │ ├── escape-a-large-maze.cpp │ ├── escape-the-ghosts.cpp │ ├── evaluate-division.cpp │ ├── evaluate-reverse-polish-notation.cpp │ ├── evaluate-the-bracket-pairs-of-a-string.cpp │ ├── even-odd-tree.cpp │ ├── exam-room.cpp │ ├── excel-sheet-column-number.cpp │ ├── excel-sheet-column-title.cpp │ ├── exclusive-time-of-functions.cpp │ ├── expression-add-operators.cpp │ ├── expressive-words.cpp │ ├── factor-combinations.cpp │ ├── factorial-trailing-zeroes.cpp │ ├── fair-candy-swap.cpp │ ├── falling-squares.cpp │ ├── fancy-sequence.cpp │ ├── faulty-sensor.cpp │ ├── fibonacci-number.cpp │ ├── filling-bookcase-shelves.cpp │ ├── filter-restaurants-by-vegan-friendly-price-and-distance.cpp │ ├── final-prices-with-a-special-discount-in-a-shop.cpp │ ├── find-a-corresponding-node-of-a-binary-tree-in-a-clone-of-that-tree.cpp │ ├── find-a-value-of-a-mysterious-function-closest-to-target.cpp │ ├── find-all-anagrams-in-a-string.cpp │ ├── find-all-duplicates-in-an-array.cpp │ ├── find-all-good-strings.cpp │ ├── find-all-numbers-disappeared-in-an-array.cpp │ ├── find-all-the-lonely-nodes.cpp │ ├── find-anagram-mappings.cpp │ ├── find-and-replace-in-string.cpp │ ├── find-and-replace-pattern.cpp │ ├── find-bottom-left-tree-value.cpp │ ├── find-center-of-star-graph.cpp │ ├── find-common-characters.cpp │ ├── find-critical-and-pseudo-critical-edges-in-minimum-spanning-tree.cpp │ ├── find-distance-in-a-binary-tree.cpp │ ├── find-duplicate-file-in-system.cpp │ ├── find-duplicate-subtrees.cpp │ ├── find-elements-in-a-contaminated-binary-tree.cpp │ ├── find-eventual-safe-states.cpp │ ├── find-in-mountain-array.cpp │ ├── find-k-closest-elements.cpp │ ├── find-k-length-substrings-with-no-repeated-characters.cpp │ ├── find-k-pairs-with-smallest-sums.cpp │ ├── find-k-th-smallest-pair-distance.cpp │ ├── find-kth-bit-in-nth-binary-string.cpp │ ├── find-kth-largest-xor-coordinate-value.cpp │ ├── find-largest-value-in-each-tree-row.cpp │ ├── find-latest-group-of-size-m.cpp │ ├── find-leaves-of-binary-tree.cpp │ ├── find-longest-awesome-substring.cpp │ ├── find-lucky-integer-in-an-array.cpp │ ├── find-median-from-data-stream.cpp │ ├── find-minimum-in-rotated-sorted-array-ii.cpp │ ├── find-minimum-in-rotated-sorted-array.cpp │ ├── find-minimum-time-to-finish-all-jobs.cpp │ ├── find-mode-in-binary-search-tree.cpp │ ├── find-n-unique-integers-sum-up-to-zero.cpp │ ├── find-nearest-point-that-has-the-same-x-or-y-coordinate.cpp │ ├── find-nearest-right-node-in-binary-tree.cpp │ ├── find-numbers-with-even-number-of-digits.cpp │ ├── find-peak-element.cpp │ ├── find-permutation.cpp │ ├── find-pivot-index.cpp │ ├── find-positive-integer-solution-for-a-given-equation.cpp │ ├── find-right-interval.cpp │ ├── find-root-of-n-ary-tree.cpp │ ├── find-servers-that-handled-most-number-of-requests.cpp │ ├── find-smallest-common-element-in-all-rows.cpp │ ├── find-smallest-letter-greater-than-target.cpp │ ├── find-the-celebrity.cpp │ ├── find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance.cpp │ ├── find-the-closest-palindrome.cpp │ ├── find-the-derangement-of-an-array.cpp │ ├── find-the-difference.cpp │ ├── find-the-distance-value-between-two-arrays.cpp │ ├── find-the-duplicate-number.cpp │ ├── find-the-highest-altitude.cpp │ ├── find-the-index-of-the-large-integer.cpp │ ├── find-the-kth-smallest-sum-of-a-matrix-with-sorted-rows.cpp │ ├── find-the-longest-substring-containing-vowels-in-even-counts.cpp │ ├── find-the-minimum-number-of-fibonacci-numbers-whose-sum-is-k.cpp │ ├── find-the-most-competitive-subsequence.cpp │ ├── find-the-shortest-superstring.cpp │ ├── find-the-smallest-divisor-given-a-threshold.cpp │ ├── find-the-town-judge.cpp │ ├── find-the-winner-of-an-array-game.cpp │ ├── find-the-winner-of-the-circular-game.cpp │ ├── find-two-non-overlapping-sub-arrays-each-with-target-sum.cpp │ ├── find-valid-matrix-given-row-and-column-sums.cpp │ ├── find-winner-on-a-tic-tac-toe-game.cpp │ ├── find-words-that-can-be-formed-by-characters.cpp │ ├── find-xor-sum-of-all-pairs-bitwise-and.cpp │ ├── finding-mk-average.cpp │ ├── finding-pairs-with-a-certain-sum.cpp │ ├── finding-the-users-active-minutes.cpp │ ├── first-bad-version.cpp │ ├── first-missing-positive.cpp │ ├── first-unique-character-in-a-string.cpp │ ├── first-unique-number.cpp │ ├── fixed-point.cpp │ ├── fizz-buzz-multithreaded.cpp │ ├── fizz-buzz.cpp │ ├── flatten-2d-vector.cpp │ ├── flatten-a-multilevel-doubly-linked-list.cpp │ ├── flatten-nested-list-iterator.cpp │ ├── flatten.cpp │ ├── flip-binary-tree-to-match-preorder-traversal.cpp │ ├── flip-columns-for-maximum-number-of-equal-rows.cpp │ ├── flip-equivalent-binary-trees.cpp │ ├── flip-game-ii.cpp │ ├── flip-game.cpp │ ├── flip-string-to-monotone-increasing.cpp │ ├── flipping-an-image.cpp │ ├── flood-fill.cpp │ ├── flower-planting-with-no-adjacent.cpp │ ├── form-array-by-concatenating-subarrays-of-another-array.cpp │ ├── form-largest-integer-with-digits-that-add-up-to-target.cpp │ ├── four-divisors.cpp │ ├── fraction-addition-and-subtraction.cpp │ ├── fraction-to-recurring-decimal.cpp │ ├── freedom-trail.cpp │ ├── frequency-of-the-most-frequent-element.cpp │ ├── friend-circles.cpp │ ├── friends-of-appropriate-ages.cpp │ ├── frog-jump.cpp │ ├── frog-position-after-t-seconds.cpp │ ├── fruit-into-baskets.cpp │ ├── furthest-building-you-can-reach.cpp │ ├── game-of-life.cpp │ ├── generalized-abbreviation.cpp │ ├── generate-a-string-with-characters-that-have-odd-counts.cpp │ ├── generate-parentheses.cpp │ ├── generate-random-point-in-a-circle.cpp │ ├── get-equal-substrings-within-budget.cpp │ ├── get-maximum-in-generated-array.cpp │ ├── get-the-maximum-score.cpp │ ├── get-watched-videos-by-your-friends.cpp │ ├── getRow.cpp │ ├── global-and-local-inversions.cpp │ ├── goal-parser-interpretation.cpp │ ├── goat-latin.cpp │ ├── graph-connectivity-with-threshold.cpp │ ├── graph-valid-tree.cpp │ ├── gray-code.cpp │ ├── greatest-common-divisor-of-strings.cpp │ ├── greatest-sum-divisible-by-three.cpp │ ├── grid-illumination.cpp │ ├── group-anagrams.cpp │ ├── group-shifted-strings.cpp │ ├── group-the-people-given-the-group-size-they-belong-to.cpp │ ├── groups-of-special-equivalent-strings.cpp │ ├── grumpy-bookstore-owner.cpp │ ├── guess-number-higher-or-lower-ii.cpp │ ├── guess-number-higher-or-lower.cpp │ ├── guess-the-majority-in-a-hidden-array.cpp │ ├── guess-the-word.cpp │ ├── h-index-ii.cpp │ ├── h-index.cpp │ ├── hamming-distance.cpp │ ├── hand-of-straights.cpp │ ├── handshakes-that-dont-cross.cpp │ ├── happy-number.cpp │ ├── hasPathSum.cpp │ ├── heaters.cpp │ ├── height-checker.cpp │ ├── hexspeak.cpp │ ├── high-five.cpp │ ├── house-robber-ii.cpp │ ├── house-robber-iii.cpp │ ├── house-robber.cpp │ ├── how-many-apples-can-you-put-into-the-basket.cpp │ ├── how-many-numbers-are-smaller-than-the-current-number.cpp │ ├── html-entity-parser.cpp │ ├── image-overlap.cpp │ ├── image-smoother.cpp │ ├── implement-magic-dictionary.cpp │ ├── implement-queue-using-stacks.cpp │ ├── implement-rand10-using-rand7.cpp │ ├── implement-stack-using-queues.cpp │ ├── implement-strstr.cpp │ ├── implement-trie-ii-prefix-tree.cpp │ ├── implement-trie-prefix-tree.cpp │ ├── increasing-decreasing-string.cpp │ ├── increasing-order-search-tree.cpp │ ├── increasing-subsequences.cpp │ ├── increasing-triplet-subsequence.cpp │ ├── incremental-memory-leak.cpp │ ├── index-pairs-of-a-string.cpp │ ├── inorder-successor-in-bst-ii.cpp │ ├── inorder-successor-in-bst.cpp │ ├── insert-delete-getrandom-o1-duplicates-allowed.cpp │ ├── insert-delete-getrandom-o1.cpp │ ├── insert-interval.cpp │ ├── insert-into-a-binary-search-tree.cpp │ ├── insert-into-a-cyclic-sorted-list.cpp │ ├── insertion-sort-list.cpp │ ├── insufficient-nodes-in-root-to-leaf-paths.cpp │ ├── integer-break.cpp │ ├── integer-replacement.cpp │ ├── integer-to-english-words.cpp │ ├── integer-to-roman.cpp │ ├── intersection-of-three-sorted-arrays.cpp │ ├── intersection-of-two-arrays-ii.cpp │ ├── intersection-of-two-arrays.cpp │ ├── intersection-of-two-linked-lists.cpp │ ├── interval-list-intersections.cpp │ ├── invalid-transactions.cpp │ ├── invert-binary-tree.cpp │ ├── ip-to-cidr.cpp │ ├── ipo.cpp │ ├── is-graph-bipartite.cpp │ ├── is-subsequence.cpp │ ├── isInterleave.cpp │ ├── isMatch.cpp │ ├── isScramble.cpp │ ├── isValidBST2.cpp │ ├── island-perimeter.cpp │ ├── isomorphic-strings.cpp │ ├── iterator-for-combination.cpp │ ├── jewels-and-stones.cpp │ ├── jump-game-iii.cpp │ ├── jump-game-iv.cpp │ ├── jump-game-v.cpp │ ├── jump-game-vi.cpp │ ├── jump-game-vii.cpp │ ├── jump-game.cpp │ ├── jump.cpp │ ├── k-closest-points-to-origin.cpp │ ├── k-concatenation-maximum-sum.cpp │ ├── k-diff-pairs-in-an-array.cpp │ ├── k-empty-slots.cpp │ ├── k-inverse-pairs-array.cpp │ ├── k-similar-strings.cpp │ ├── k-th-smallest-in-lexicographical-order.cpp │ ├── k-th-smallest-prime-fraction.cpp │ ├── k-th-symbol-in-grammar.cpp │ ├── keyboard-row.cpp │ ├── keys-and-rooms.cpp │ ├── kids-with-the-greatest-number-of-candies.cpp │ ├── kill-process.cpp │ ├── knight-dialer.cpp │ ├── knight-probability-in-chessboard.cpp │ ├── koko-eating-bananas.cpp │ ├── kth-ancestor-of-a-tree-node.cpp │ ├── kth-largest-element-in-a-stream.cpp │ ├── kth-largest-element-in-an-array.cpp │ ├── kth-missing-positive-number.cpp │ ├── kth-smallest-element-in-a-bst.cpp │ ├── kth-smallest-element-in-a-sorted-matrix.cpp │ ├── kth-smallest-instructions.cpp │ ├── kth-smallest-number-in-multiplication-table.cpp │ ├── largest-1-bordered-square.cpp │ ├── largest-bst-subtree.cpp │ ├── largest-color-value-in-a-directed-graph.cpp │ ├── largest-component-size-by-common-factor.cpp │ ├── largest-divisible-subset.cpp │ ├── largest-merge-of-two-strings.cpp │ ├── largest-multiple-of-three.cpp │ ├── largest-number-at-least-twice-of-others.cpp │ ├── largest-number.cpp │ ├── largest-palindrome-product.cpp │ ├── largest-perimeter-triangle.cpp │ ├── largest-plus-sign.cpp │ ├── largest-rectangle-in-histogram.cpp │ ├── largest-subarray-length-k.cpp │ ├── largest-submatrix-with-rearrangements.cpp │ ├── largest-substring-between-two-equal-characters.cpp │ ├── largest-sum-of-averages.cpp │ ├── largest-time-for-given-digits.cpp │ ├── largest-triangle-area.cpp │ ├── largest-unique-number.cpp │ ├── largest-values-from-labels.cpp │ ├── last-moment-before-all-ants-fall-out-of-a-plank.cpp │ ├── last-stone-weight-ii.cpp │ ├── last-stone-weight.cpp │ ├── last-substring-in-lexicographical-order.cpp │ ├── latest-time-by-replacing-hidden-digits.cpp │ ├── leaf-similar-trees.cpp │ ├── least-number-of-unique-integers-after-k-removals.cpp │ ├── least-operators-to-express-number.cpp │ ├── leftmost-column-with-at-least-a-one.cpp │ ├── lemonade-change.cpp │ ├── length-of-last-word.cpp │ ├── length-of-longest-fibonacci-subsequence.cpp │ ├── letter-case-permutation.cpp │ ├── letter-combinations-of-a-phone-number.cpp │ ├── letter-tile-possibilities.cpp │ ├── lexicographical-numbers.cpp │ ├── lexicographically-smallest-equivalent-string.cpp │ ├── lexicographically-smallest-string-after-applying-operations.cpp │ ├── lfu-cache.cpp │ ├── license-key-formatting.cpp │ ├── line-reflection.cpp │ ├── linked-list-components.cpp │ ├── linked-list-cycle-ii.cpp │ ├── linked-list-cycle.cpp │ ├── linked-list-in-binary-tree.cpp │ ├── linked-list-random-node.cpp │ ├── logger-rate-limiter.cpp │ ├── lonely-pixel-i.cpp │ ├── lonely-pixel-ii.cpp │ ├── long-pressed-name.cpp │ ├── longer-contiguous-segments-of-ones-than-zeros.cpp │ ├── longest-absolute-file-path.cpp │ ├── longest-arithmetic-sequence.cpp │ ├── longest-arithmetic-subsequence-of-given-difference.cpp │ ├── longest-chunked-palindrome-decomposition.cpp │ ├── longest-common-prefix.cpp │ ├── longest-common-subsequence.cpp │ ├── longest-consecutive-sequence.cpp │ ├── longest-continuous-increasing-subsequence.cpp │ ├── longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit.cpp │ ├── longest-duplicate-substring.cpp │ ├── longest-happy-prefix.cpp │ ├── longest-happy-string.cpp │ ├── longest-harmonious-subsequence.cpp │ ├── longest-increasing-path-in-a-matrix.cpp │ ├── longest-increasing-subsequence.cpp │ ├── longest-line-of-consecutive-one-in-matrix.cpp │ ├── longest-mountain-in-array.cpp │ ├── longest-nice-substring.cpp │ ├── longest-palindrome.cpp │ ├── longest-palindromic-subsequence-ii.cpp │ ├── longest-palindromic-subsequence.cpp │ ├── longest-palindromic-substring.cpp │ ├── longest-repeating-character-replacement.cpp │ ├── longest-repeating-substring.cpp │ ├── longest-string-chain.cpp │ ├── longest-subarray-of-1s-after-deleting-one-element.cpp │ ├── longest-substring-of-all-vowels-in-order.cpp │ ├── longest-substring-with-at-least-k-repeating-characters.cpp │ ├── longest-substring-with-at-most-k-distinct-characters.cpp │ ├── longest-substring-with-at-most-two-distinct-characters.cpp │ ├── longest-substring-without-repeating-characters.cpp │ ├── longest-turbulent-subarray.cpp │ ├── longest-uncommon-subsequence-i.cpp │ ├── longest-uncommon-subsequence-ii.cpp │ ├── longest-univalue-path.cpp │ ├── longest-valid-parentheses.cpp │ ├── longest-well-performing-interval.cpp │ ├── longest-word-in-dictionary-through-deleting.cpp │ ├── longest-word-in-dictionary.cpp │ ├── longest-word-with-all-prefixes.cpp │ ├── longest-zigzag-path-in-a-binary-tree.cpp │ ├── longestCommonLength.cpp │ ├── loud-and-rich.cpp │ ├── lowest-common-ancestor-of-a-binary-search-tree.cpp │ ├── lowest-common-ancestor-of-a-binary-tree-ii.cpp │ ├── lowest-common-ancestor-of-a-binary-tree-iii.cpp │ ├── lowest-common-ancestor-of-a-binary-tree-iv.cpp │ ├── lowest-common-ancestor-of-a-binary-tree.cpp │ ├── lowest-common-ancestor-of-deepest-leaves.cpp │ ├── lru-cache.cpp │ ├── lucky-numbers-in-a-matrix.cpp │ ├── magic-squares-in-grid.cpp │ ├── magical-string.cpp │ ├── magnetic-force-between-two-balls.cpp │ ├── majority-element-ii.cpp │ ├── majority-element.cpp │ ├── make-array-strictly-increasing.cpp │ ├── make-sum-divisible-by-p.cpp │ ├── make-the-string-great.cpp │ ├── make-the-xor-of-all-segments-equal-to-zero.cpp │ ├── make-two-arrays-equal-by-reversing-sub-arrays.cpp │ ├── making-a-large-island.cpp │ ├── making-file-names-unique.cpp │ ├── map-of-highest-peak.cpp │ ├── map-sum-pairs.cpp │ ├── masking-personal-information.cpp │ ├── matchsticks-to-square.cpp │ ├── matrix-block-sum.cpp │ ├── matrix-cells-in-distance-order.cpp │ ├── matrix-diagonal-sum.cpp │ ├── max-area-of-island.cpp │ ├── max-chunks-to-make-sorted-ii.cpp │ ├── max-chunks-to-make-sorted.cpp │ ├── max-consecutive-ones-ii.cpp │ ├── max-consecutive-ones-iii.cpp │ ├── max-consecutive-ones.cpp │ ├── max-difference-you-can-get-from-changing-an-integer.cpp │ ├── max-dot-product-of-two-subsequences.cpp │ ├── max-increase-to-keep-city-skyline.cpp │ ├── max-number-of-k-sum-pairs.cpp │ ├── max-points-on-a-line.cpp │ ├── max-stack.cpp │ ├── max-sum-of-sub-matrix-no-larger-than-k.cpp │ ├── max-value-of-equation.cpp │ ├── maxPathSum.cpp │ ├── maxProfitII.cpp │ ├── maxProfitIII.cpp │ ├── maximal-network-rank.cpp │ ├── maximal-rectangle.cpp │ ├── maximal-square.cpp │ ├── maximize-distance-to-closest-person.cpp │ ├── maximize-grid-happiness.cpp │ ├── maximize-number-of-nice-divisors.cpp │ ├── maximize-palindrome-length-from-subsequences.cpp │ ├── maximize-score-after-n-operations.cpp │ ├── maximize-sum-of-array-after-k-negations.cpp │ ├── maximize-the-beauty-of-the-garden.cpp │ ├── maximum-69-number.cpp │ ├── maximum-absolute-sum-of-any-subarray.cpp │ ├── maximum-area-of-a-piece-of-cake-after-horizontal-and-vertical-cuts.cpp │ ├── maximum-ascending-subarray-sum.cpp │ ├── maximum-average-pass-ratio.cpp │ ├── maximum-average-subarray-i.cpp │ ├── maximum-average-subarray-ii.cpp │ ├── maximum-average-subtree.cpp │ ├── maximum-binary-string-after-change.cpp │ ├── maximum-binary-tree-ii.cpp │ ├── maximum-binary-tree.cpp │ ├── maximum-building-height.cpp │ ├── maximum-candies-you-can-get-from-boxes.cpp │ ├── maximum-depth-of-binary-tree.cpp │ ├── maximum-depth-of-n-ary-tree.cpp │ ├── maximum-difference-between-node-and-ancestor.cpp │ ├── maximum-distance-between-a-pair-of-values.cpp │ ├── maximum-distance-in-arrays.cpp │ ├── maximum-element-after-decreasing-and-rearranging.cpp │ ├── maximum-equal-frequency.cpp │ ├── maximum-erasure-value.cpp │ ├── maximum-font-to-fit-a-sentence-in-a-screen.cpp │ ├── maximum-frequency-stack.cpp │ ├── maximum-gap.cpp │ ├── maximum-height-by-stacking-cuboids.cpp │ ├── maximum-ice-cream-bars.cpp │ ├── maximum-length-of-a-concatenated-string-with-unique-characters.cpp │ ├── maximum-length-of-pair-chain.cpp │ ├── maximum-length-of-repeated-subarray.cpp │ ├── maximum-length-of-subarray-with-positive-product.cpp │ ├── maximum-level-sum-of-a-binary-tree.cpp │ ├── maximum-nesting-depth-of-the-parentheses.cpp │ ├── maximum-nesting-depth-of-two-valid-parentheses-strings.cpp │ ├── maximum-non-negative-product-in-a-matrix.cpp │ ├── maximum-number-of-accepted-invitations.cpp │ ├── maximum-number-of-achievable-transfer-requests.cpp │ ├── maximum-number-of-balloons.cpp │ ├── maximum-number-of-balls-in-a-box.cpp │ ├── maximum-number-of-coins-you-can-get.cpp │ ├── maximum-number-of-consecutive-values-you-can-make.cpp │ ├── maximum-number-of-darts-inside-of-a-circular-dartboard.cpp │ ├── maximum-number-of-eaten-apples.cpp │ ├── maximum-number-of-events-that-can-be-attended-ii.cpp │ ├── maximum-number-of-events-that-can-be-attended.cpp │ ├── maximum-number-of-groups-getting-fresh-donuts.cpp │ ├── maximum-number-of-non-overlapping-subarrays-with-sum-equals-target.cpp │ ├── maximum-number-of-non-overlapping-substrings.cpp │ ├── maximum-number-of-occurrences-of-a-substring.cpp │ ├── maximum-number-of-ones.cpp │ ├── maximum-number-of-visible-points.cpp │ ├── maximum-number-of-vowels-in-a-substring-of-given-length.cpp │ ├── maximum-of-absolute-value-expression.cpp │ ├── maximum-performance-of-a-team.cpp │ ├── maximum-points-you-can-obtain-from-cards.cpp │ ├── maximum-population-year.cpp │ ├── maximum-product-of-splitted-binary-tree.cpp │ ├── maximum-product-of-three-numbers.cpp │ ├── maximum-product-of-two-elements-in-an-array.cpp │ ├── maximum-product-of-word-lengths.cpp │ ├── maximum-product-subarray.cpp │ ├── maximum-profit-in-job-scheduling.cpp │ ├── maximum-profit-of-operating-a-centennial-wheel.cpp │ ├── maximum-repeating-substring.cpp │ ├── maximum-score-after-splitting-a-string.cpp │ ├── maximum-score-from-performing-multiplication-operations.cpp │ ├── maximum-score-from-removing-stones.cpp │ ├── maximum-score-from-removing-substrings.cpp │ ├── maximum-score-of-a-good-subarray.cpp │ ├── maximum-score-words-formed-by-letters.cpp │ ├── maximum-side-length-of-a-square-with-sum-less-than-or-equal-to-threshold.cpp │ ├── maximum-size-subarray-sum-equals-k.cpp │ ├── maximum-students-taking-exam.cpp │ ├── maximum-subarray-min-product.cpp │ ├── maximum-subarray-sum-after-one-operation.cpp │ ├── maximum-subarray-sum-with-one-deletion.cpp │ ├── maximum-subarray.cpp │ ├── maximum-sum-bst-in-binary-tree.cpp │ ├── maximum-sum-circular-subarray.cpp │ ├── maximum-sum-obtained-of-any-permutation.cpp │ ├── maximum-sum-of-3-non-overlapping-subarrays.cpp │ ├── maximum-sum-of-two-non-overlapping-subarrays.cpp │ ├── maximum-swap.cpp │ ├── maximum-units-on-a-truck.cpp │ ├── maximum-vacation-days.cpp │ ├── maximum-value-at-a-given-index-in-a-bounded-array.cpp │ ├── maximum-width-of-binary-tree.cpp │ ├── maximum-width-ramp.cpp │ ├── maximum-xor-for-each-query.cpp │ ├── maximum-xor-of-two-numbers-in-an-array.cpp │ ├── maximum-xor-with-an-element-from-array.cpp │ ├── mean-of-array-after-removing-some-elements.cpp │ ├── median-of-two-sorted-arrays.cpp │ ├── meeting-rooms-ii.cpp │ ├── meeting-rooms.cpp │ ├── meeting-scheduler.cpp │ ├── merge-in-between-linked-lists.cpp │ ├── merge-intervals.cpp │ ├── merge-k-sorted-lists.cpp │ ├── merge-sorted-array.cpp │ ├── merge-strings-alternately.cpp │ ├── merge-two-binary-trees.cpp │ ├── merge-two-sorted-lists.cpp │ ├── merge2.cpp │ ├── middle-of-the-linked-list.cpp │ ├── min-cost-climbing-stairs.cpp │ ├── min-cost-to-connect-all-points.cpp │ ├── min-stack.cpp │ ├── minCut.cpp │ ├── minDepth.cpp │ ├── minDistance.cpp │ ├── minPathSum.cpp │ ├── minesweeper.cpp │ ├── mini-parser.cpp │ ├── minimize-deviation-in-array.cpp │ ├── minimize-hamming-distance-after-swap-operations.cpp │ ├── minimize-malware-spread-ii.cpp │ ├── minimize-malware-spread.cpp │ ├── minimize-max-distance-to-gas-station.cpp │ ├── minimize-rounding-error-to-meet-target.cpp │ ├── minimum-absolute-difference-in-bst.cpp │ ├── minimum-absolute-difference.cpp │ ├── minimum-absolute-sum-difference.cpp │ ├── minimum-add-to-make-parentheses-valid.cpp │ ├── minimum-adjacent-swaps-for-k-consecutive-ones.cpp │ ├── minimum-adjacent-swaps-to-reach-the-kth-smallest-number.cpp │ ├── minimum-area-rectangle-ii.cpp │ ├── minimum-area-rectangle.cpp │ ├── minimum-ascii-delete-sum-for-two-strings.cpp │ ├── minimum-changes-to-make-alternating-binary-string.cpp │ ├── minimum-cost-for-tickets.cpp │ ├── minimum-cost-to-connect-sticks.cpp │ ├── minimum-cost-to-connect-two-groups-of-points.cpp │ ├── minimum-cost-to-cut-a-stick.cpp │ ├── minimum-cost-to-hire-k-workers.cpp │ ├── minimum-cost-to-make-at-least-one-valid-path-in-a-grid.cpp │ ├── minimum-cost-to-merge-stones.cpp │ ├── minimum-cost-tree-from-leaf-values.cpp │ ├── minimum-degree-of-a-connected-trio-in-a-graph.cpp │ ├── minimum-deletion-cost-to-avoid-repeating-letters.cpp │ ├── minimum-deletions-to-make-character-frequencies-unique.cpp │ ├── minimum-deletions-to-make-string-balanced.cpp │ ├── minimum-difference-between-largest-and-smallest-value-in-three-moves.cpp │ ├── minimum-difficulty-of-a-job-schedule.cpp │ ├── minimum-distance-between-bst-nodes.cpp │ ├── minimum-distance-to-the-target-element.cpp │ ├── minimum-distance-to-type-a-word-using-two-fingers.cpp │ ├── minimum-domino-rotations-for-equal-row.cpp │ ├── minimum-elements-to-add-to-form-a-given-sum.cpp │ ├── minimum-factorization.cpp │ ├── minimum-falling-path-sum-ii.cpp │ ├── minimum-falling-path-sum.cpp │ ├── minimum-flips-to-make-a-or-b-equal-to-c.cpp │ ├── minimum-genetic-mutation.cpp │ ├── minimum-height-trees.cpp │ ├── minimum-incompatibility.cpp │ ├── minimum-increment-to-make-array-unique.cpp │ ├── minimum-index-sum-of-two-lists.cpp │ ├── minimum-initial-energy-to-finish-tasks.cpp │ ├── minimum-insertion-steps-to-make-a-string-palindrome.cpp │ ├── minimum-insertions-to-balance-a-parentheses-string.cpp │ ├── minimum-interval-to-include-each-query.cpp │ ├── minimum-jumps-to-reach-home.cpp │ ├── minimum-knight-moves.cpp │ ├── minimum-length-of-string-after-deleting-similar-ends.cpp │ ├── minimum-limit-of-balls-in-a-bag.cpp │ ├── minimum-moves-to-equal-array-elements-ii.cpp │ ├── minimum-moves-to-equal-array-elements.cpp │ ├── minimum-moves-to-make-array-complementary.cpp │ ├── minimum-moves-to-move-a-box-to-their-target-location.cpp │ ├── minimum-moves-to-reach-target-with-rotations.cpp │ ├── minimum-number-of-arrows-to-burst-balloons.cpp │ ├── minimum-number-of-days-to-disconnect-island.cpp │ ├── minimum-number-of-days-to-eat-n-oranges.cpp │ ├── minimum-number-of-days-to-make-m-bouquets.cpp │ ├── minimum-number-of-flips-to-convert-binary-matrix-to-zero-matrix.cpp │ ├── minimum-number-of-frogs-croaking.cpp │ ├── minimum-number-of-increments-on-subarrays-to-form-a-target-array.cpp │ ├── minimum-number-of-k-consecutive-bit-flips.cpp │ ├── minimum-number-of-operations-to-make-string-sorted.cpp │ ├── minimum-number-of-operations-to-move-all-balls-to-each-box.cpp │ ├── minimum-number-of-operations-to-reinitialize-a-permutation.cpp │ ├── minimum-number-of-people-to-teach.cpp │ ├── minimum-number-of-refueling-stops.cpp │ ├── minimum-number-of-removals-to-make-mountain-array.cpp │ ├── minimum-number-of-steps-to-make-two-strings-anagram.cpp │ ├── minimum-number-of-swaps-to-make-the-binary-string-alternating.cpp │ ├── minimum-number-of-taps-to-open-to-water-a-garden.cpp │ ├── minimum-number-of-vertices-to-reach-all-nodes.cpp │ ├── minimum-numbers-of-function-calls-to-make-target-array.cpp │ ├── minimum-one-bit-operations-to-make-integers-zero.cpp │ ├── minimum-operations-to-make-a-subsequence.cpp │ ├── minimum-operations-to-make-array-equal.cpp │ ├── minimum-operations-to-make-the-array-increasing.cpp │ ├── minimum-operations-to-reduce-x-to-zero.cpp │ ├── minimum-path-cost-in-a-hidden-grid.cpp │ ├── minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits.cpp │ ├── minimum-remove-to-make-valid-parentheses.cpp │ ├── minimum-score-triangulation-of-polygon.cpp │ ├── minimum-sideway-jumps.cpp │ ├── minimum-size-subarray-sum.cpp │ ├── minimum-speed-to-arrive-on-time.cpp │ ├── minimum-subsequence-in-non-increasing-order.cpp │ ├── minimum-swaps-to-arrange-a-binary-grid.cpp │ ├── minimum-swaps-to-group-all-1s-together.cpp │ ├── minimum-swaps-to-make-sequences-increasing.cpp │ ├── minimum-swaps-to-make-strings-equal.cpp │ ├── minimum-time-difference.cpp │ ├── minimum-time-to-build-blocks.cpp │ ├── minimum-time-to-collect-all-apples-in-a-tree.cpp │ ├── minimum-time-visiting-all-points.cpp │ ├── minimum-unique-word-abbreviation.cpp │ ├── minimum-value-to-get-positive-step-by-step-sum.cpp │ ├── minimum-window-subsequence.cpp │ ├── minimum-window-substring.cpp │ ├── minimumTotal.cpp │ ├── minimumTotal2.cpp │ ├── mirror-reflection.cpp │ ├── missing-element-in-sorted-array.cpp │ ├── missing-number-in-arithmetic-progression.cpp │ ├── missing-number.cpp │ ├── missing-ranges.cpp │ ├── monotone-increasing-digits.cpp │ ├── monotonic-array.cpp │ ├── most-common-word.cpp │ ├── most-frequent-subtree-sum.cpp │ ├── most-profit-assigning-work.cpp │ ├── most-stones-removed-with-same-row-or-column.cpp │ ├── most-visited-sector-in-a-circular-track.cpp │ ├── move-sub-tree-of-n-ary-tree.cpp │ ├── move-zeroes.cpp │ ├── moving-average-from-data-stream.cpp │ ├── moving-stones-until-consecutive-ii.cpp │ ├── moving-stones-until-consecutive.cpp │ ├── multiply-strings.cpp │ ├── my-calendar-i.cpp │ ├── my-calendar-ii.cpp │ ├── my-calendar-iii.cpp │ ├── n-ary-tree-level-order-traversal.cpp │ ├── n-ary-tree-postorder-traversal.cpp │ ├── n-ary-tree-preorder-traversal.cpp │ ├── n-repeated-element-in-size-2n-array.cpp │ ├── n-th-tribonacci-number.cpp │ ├── nested-list-weight-sum-ii.cpp │ ├── nested-list-weight-sum.cpp │ ├── network-delay-time.cpp │ ├── new-21-game.cpp │ ├── next-closest-time.cpp │ ├── next-greater-element-i.cpp │ ├── next-greater-element-ii.cpp │ ├── next-greater-element-iii.cpp │ ├── next-greater-node-in-linked-list.cpp │ ├── next-palindrome-using-same-digits.cpp │ ├── next-permutation.cpp │ ├── nim-game.cpp │ ├── non-decreasing-array.cpp │ ├── non-negative-integers-without-consecutive-ones.cpp │ ├── non-overlapping-intervals.cpp │ ├── nth-digit.cpp │ ├── nth-magical-number.cpp │ ├── numDistinct.cpp │ ├── number-complement.cpp │ ├── number-of-1-bits.cpp │ ├── number-of-atoms.cpp │ ├── number-of-boomerangs.cpp │ ├── number-of-burgers-with-no-waste-of-ingredients.cpp │ ├── number-of-closed-islands.cpp │ ├── number-of-connected-components-in-an-undirected-graph.cpp │ ├── number-of-corner-rectangles.cpp │ ├── number-of-days-between-two-dates.cpp │ ├── number-of-days-in-a-month.cpp │ ├── number-of-dice-rolls-with-target-sum.cpp │ ├── number-of-different-integers-in-a-string.cpp │ ├── number-of-different-subsequences-gcds.cpp │ ├── number-of-digit-one.cpp │ ├── number-of-distinct-islands-ii.cpp │ ├── number-of-distinct-islands.cpp │ ├── number-of-distinct-substrings-in-a-string.cpp │ ├── number-of-enclaves.cpp │ ├── number-of-equivalent-domino-pairs.cpp │ ├── number-of-good-leaf-nodes-pairs.cpp │ ├── number-of-good-pairs.cpp │ ├── number-of-good-ways-to-split-a-string.cpp │ ├── number-of-islands-ii.cpp │ ├── number-of-islands.cpp │ ├── number-of-lines-to-write-string.cpp │ ├── number-of-longest-increasing-subsequence.cpp │ ├── number-of-matching-subsequences.cpp │ ├── number-of-music-playlists.cpp │ ├── number-of-nodes-in-the-sub-tree-with-the-same-label.cpp │ ├── number-of-operations-to-make-network-connected.cpp │ ├── number-of-orders-in-the-backlog.cpp │ ├── number-of-paths-with-max-score.cpp │ ├── number-of-recent-calls.cpp │ ├── number-of-rectangles-that-can-form-the-largest-square.cpp │ ├── number-of-restricted-paths-from-first-to-last-node.cpp │ ├── number-of-segments-in-a-string.cpp │ ├── number-of-sets-of-k-non-overlapping-line-segments.cpp │ ├── number-of-ships-in-a-rectangle.cpp │ ├── number-of-squareful-arrays.cpp │ ├── number-of-steps-to-reduce-a-number-in-binary-representation-to-one.cpp │ ├── number-of-steps-to-reduce-a-number-to-zero.cpp │ ├── number-of-students-doing-homework-at-a-given-time.cpp │ ├── number-of-students-unable-to-eat-lunch.cpp │ ├── number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold.cpp │ ├── number-of-sub-arrays-with-odd-sum.cpp │ ├── number-of-subarrays-with-bounded-maximum.cpp │ ├── number-of-submatrices-that-sum-to-target.cpp │ ├── number-of-subsequences-that-satisfy-the-given-sum-condition.cpp │ ├── number-of-substrings-containing-all-three-characters.cpp │ ├── number-of-substrings-with-only-1s.cpp │ ├── number-of-valid-subarrays.cpp │ ├── number-of-valid-words-for-each-puzzle.cpp │ ├── number-of-ways-of-cutting-a-pizza.cpp │ ├── number-of-ways-to-form-a-target-string-given-a-dictionary.cpp │ ├── number-of-ways-to-paint-n-3-grid.cpp │ ├── number-of-ways-to-rearrange-sticks-with-k-sticks-visible.cpp │ ├── number-of-ways-to-reconstruct-a-tree.cpp │ ├── number-of-ways-to-reorder-array-to-get-same-bst.cpp │ ├── number-of-ways-to-split-a-string.cpp │ ├── number-of-ways-to-stay-in-the-same-place-after-some-steps.cpp │ ├── number-of-ways-to-wear-different-hats-to-each-other.cpp │ ├── number-of-ways-where-square-of-number-is-equal-to-product-of-two-numbers.cpp │ ├── numbers-at-most-n-given-digit-set.cpp │ ├── numbers-with-repeated-digits.cpp │ ├── numbers-with-same-consecutive-differences.cpp │ ├── occurrences-after-bigram.cpp │ ├── odd-even-jump.cpp │ ├── odd-even-linked-list.cpp │ ├── one-edit-distance.cpp │ ├── ones-and-zeroes.cpp │ ├── online-election.cpp │ ├── online-majority-element-in-subarray.cpp │ ├── online-stock-span.cpp │ ├── open-the-lock.cpp │ ├── optimal-account-balancing.cpp │ ├── optimal-division.cpp │ ├── optimize-water-distribution-in-a-village.cpp │ ├── orderly-queue.cpp │ ├── out-of-boundary-paths.cpp │ ├── output-contest-matches.cpp │ ├── pacific-atlantic-water-flow.cpp │ ├── paint-fence.cpp │ ├── paint-house-ii.cpp │ ├── paint-house-iii.cpp │ ├── paint-house.cpp │ ├── pairs-of-songs-with-total-durations-divisible-by-60.cpp │ ├── palindrome-linked-list.cpp │ ├── palindrome-number.cpp │ ├── palindrome-pairs.cpp │ ├── palindrome-partitioning-iii.cpp │ ├── palindrome-partitioning-iv.cpp │ ├── palindrome-permutation-ii.cpp │ ├── palindrome-permutation.cpp │ ├── palindrome-removal.cpp │ ├── palindromePartition.cpp │ ├── palindromePartition2.cpp │ ├── palindromic-substrings.cpp │ ├── pancake-sorting.cpp │ ├── parallel-courses-ii.cpp │ ├── parallel-courses.cpp │ ├── parse-lisp-expression.cpp │ ├── parsing-a-boolean-expression.cpp │ ├── partition-array-for-maximum-sum.cpp │ ├── partition-array-into-disjoint-intervals.cpp │ ├── partition-array-into-three-parts-with-equal-sum.cpp │ ├── partition-equal-subset-sum.cpp │ ├── partition-labels.cpp │ ├── partition-list.cpp │ ├── partition-to-k-equal-sum-subsets.cpp │ ├── partitioning-into-minimum-number-of-deci-binary-numbers.cpp │ ├── pascals-triangle-ii.cpp │ ├── pascals-triangle.cpp │ ├── patching-array.cpp │ ├── path-crossing.cpp │ ├── path-in-zigzag-labelled-binary-tree.cpp │ ├── path-sum-iii.cpp │ ├── path-sum-iv.cpp │ ├── path-with-maximum-gold.cpp │ ├── path-with-maximum-minimum-value.cpp │ ├── path-with-maximum-probability.cpp │ ├── path-with-minimum-effort.cpp │ ├── pathSum.cpp │ ├── peak-index-in-a-mountain-array.cpp │ ├── peeking-iterator.cpp │ ├── people-whose-list-of-favorite-companies-is-not-a-subset-of-another-list.cpp │ ├── perfect-number.cpp │ ├── perfect-rectangle.cpp │ ├── perfect-squares.cpp │ ├── perform-string-shifts.cpp │ ├── permutation-in-string.cpp │ ├── permutation-sequence.cpp │ ├── permuteUnique.cpp │ ├── pizza-with-3n-slices.cpp │ ├── play-with-chips.cpp │ ├── plus-one-linked-list.cpp │ ├── plus-one.cpp │ ├── poor-pigs.cpp │ ├── positions-of-large-groups.cpp │ ├── possible-bipartition.cpp │ ├── pour-water.cpp │ ├── power-of-four.cpp │ ├── power-of-three.cpp │ ├── power-of-two.cpp │ ├── powerful-integers.cpp │ ├── powx-n.cpp │ ├── predict-the-winner.cpp │ ├── prefix-and-suffix-search.cpp │ ├── preimage-size-of-factorial-zeroes-function.cpp │ ├── previous-permutation-with-one-swap.cpp │ ├── prime-arrangements.cpp │ ├── prime-number-of-set-bits-in-binary-representation.cpp │ ├── prime-palindrome.cpp │ ├── print-binary-tree.cpp │ ├── print-foobar-alternately.cpp │ ├── print-immutable-linked-list-in-reverse.cpp │ ├── print-in-order.cpp │ ├── print-words-vertically.cpp │ ├── print-zero-even-odd.cpp │ ├── prison-cells-after-n-days.cpp │ ├── probability-of-a-two-boxes-having-the-same-number-of-distinct-balls.cpp │ ├── product-of-array-except-self.cpp │ ├── product-of-the-last-k-numbers.cpp │ ├── product-of-two-run-length-encoded-arrays.cpp │ ├── profitable-schemes.cpp │ ├── projection-area-of-3d-shapes.cpp │ ├── pseudo-palindromic-paths-in-a-binary-tree.cpp │ ├── push-dominoes.cpp │ ├── put-boxes-into-the-warehouse-i.cpp │ ├── put-boxes-into-the-warehouse-ii.cpp │ ├── pyramid-transition-matrix.cpp │ ├── quad-tree-intersection.cpp │ ├── queens-that-can-attack-the-king.cpp │ ├── queries-on-a-permutation-with-key.cpp │ ├── queries-on-number-of-points-inside-a-circle.cpp │ ├── queue-reconstruction-by-height.cpp │ ├── rabbits-in-forest.cpp │ ├── race-car.cpp │ ├── random-flip-matrix.cpp │ ├── random-pick-index.cpp │ ├── random-pick-with-blacklist.cpp │ ├── random-pick-with-weight.cpp │ ├── random-point-in-non-overlapping-rectangles.cpp │ ├── range-addition-ii.cpp │ ├── range-addition.cpp │ ├── range-module.cpp │ ├── range-sum-of-bst.cpp │ ├── range-sum-of-sorted-subarray-sums.cpp │ ├── range-sum-query-2d-immutable.cpp │ ├── range-sum-query-2d-mutable.cpp │ ├── range-sum-query-immutable.cpp │ ├── range-sum-query-mutable.cpp │ ├── rank-teams-by-votes.cpp │ ├── rank-transform-of-a-matrix.cpp │ ├── rank-transform-of-an-array.cpp │ ├── ransom-note.cpp │ ├── reach-a-number.cpp │ ├── reachable-nodes-in-subdivided-graph.cpp │ ├── reaching-points.cpp │ ├── read-n-characters-given-read4-ii-call-multiple-times.cpp │ ├── read-n-characters-given-read4.cpp │ ├── rearrange-spaces-between-words.cpp │ ├── rearrange-string-k-distance-apart.cpp │ ├── rearrange-words-in-a-sentence.cpp │ ├── reconstruct-a-2-row-binary-matrix.cpp │ ├── reconstruct-itinerary.cpp │ ├── reconstruct-original-digits-from-english.cpp │ ├── recover-a-tree-from-preorder-traversal.cpp │ ├── recover-binary-search-tree.cpp │ ├── rectangle-area-ii.cpp │ ├── rectangle-area.cpp │ ├── rectangle-overlap.cpp │ ├── reduce-array-size-to-the-half.cpp │ ├── reducing-dishes.cpp │ ├── redundant-connection-ii.cpp │ ├── redundant-connection.cpp │ ├── reformat-date.cpp │ ├── reformat-phone-number.cpp │ ├── reformat-the-string.cpp │ ├── regions-cut-by-slashes.cpp │ ├── relative-ranks.cpp │ ├── relative-sort-array.cpp │ ├── remove-9.cpp │ ├── remove-all-adjacent-duplicates-in-string-ii.cpp │ ├── remove-all-adjacent-duplicates-in-string.cpp │ ├── remove-boxes.cpp │ ├── remove-comments.cpp │ ├── remove-covered-intervals.cpp │ ├── remove-duplicate-letters.cpp │ ├── remove-duplicates-from-an-unsorted-linked-list.cpp │ ├── remove-duplicates-from-sorted-array-ii.cpp │ ├── remove-duplicates-from-sorted-array.cpp │ ├── remove-duplicates-from-sorted-list-ii.cpp │ ├── remove-duplicates-from-sorted-list.cpp │ ├── remove-element.cpp │ ├── remove-interval.cpp │ ├── remove-invalid-parentheses.cpp │ ├── remove-k-digits.cpp │ ├── remove-linked-list-elements.cpp │ ├── remove-max-number-of-edges-to-keep-graph-fully-traversable.cpp │ ├── remove-nth-node-from-end-of-list.cpp │ ├── remove-outermost-parentheses.cpp │ ├── remove-palindromic-subsequences.cpp │ ├── remove-sub-folders-from-the-filesystem.cpp │ ├── remove-vowels-from-a-string.cpp │ ├── remove-zero-sum-consecutive-nodes-from-linked-list.cpp │ ├── reorder-list.cpp │ ├── reorder-log-files.cpp │ ├── reorder-routes-to-make-all-paths-lead-to-the-city-zero.cpp │ ├── reordered-power-of-2.cpp │ ├── reorganize-string.cpp │ ├── repeated-string-match.cpp │ ├── repeated-substring-pattern.cpp │ ├── replace-all-digits-with-characters.cpp │ ├── replace-all-s-to-avoid-consecutive-repeating-characters.cpp │ ├── replace-elements-with-greatest-element-on-right-side.cpp │ ├── replace-the-substring-for-balanced-string.cpp │ ├── replace-words.cpp │ ├── reshape-the-matrix.cpp │ ├── restore-the-array-from-adjacent-pairs.cpp │ ├── restore-the-array.cpp │ ├── restoreIpAddresses.cpp │ ├── reveal-cards-in-increasing-order.cpp │ ├── reverse-bits.cpp │ ├── reverse-integer.cpp │ ├── reverse-linked-list-ii.cpp │ ├── reverse-linked-list.cpp │ ├── reverse-nodes-in-k-group.cpp │ ├── reverse-only-letters.cpp │ ├── reverse-pairs.cpp │ ├── reverse-string-ii.cpp │ ├── reverse-string.cpp │ ├── reverse-subarray-to-maximize-array-value.cpp │ ├── reverse-substrings-between-each-pair-of-parentheses.cpp │ ├── reverse-vowels-of-a-string.cpp │ ├── reverse-words-in-a-string-ii.cpp │ ├── reverse-words-in-a-string-iii.cpp │ ├── reverse-words-in-a-string.cpp │ ├── richest-customer-wealth.cpp │ ├── rle-iterator.cpp │ ├── robot-bounded-in-circle.cpp │ ├── robot-return-to-origin.cpp │ ├── robot-room-cleaner.cpp │ ├── roman-to-integer.cpp │ ├── rotate-array.cpp │ ├── rotate-function.cpp │ ├── rotate-image.cpp │ ├── rotate-list.cpp │ ├── rotate-string.cpp │ ├── rotated-digits.cpp │ ├── rotating-the-box.cpp │ ├── rotting-oranges.cpp │ ├── running-sum-of-1d-array.cpp │ ├── russian-doll-envelopes.cpp │ ├── same-tree.cpp │ ├── satisfiability-of-equality-equations.cpp │ ├── score-after-flipping-matrix.cpp │ ├── score-of-parentheses.cpp │ ├── search-a-2d-matrix-ii.cpp │ ├── search-a-2d-matrix.cpp │ ├── search-for-a-range.cpp │ ├── search-in-a-binary-search-tree.cpp │ ├── search-in-a-sorted-array-of-unknown-size.cpp │ ├── search-in-rotated-sorted-array-ii.cpp │ ├── search-in-rotated-sorted-array.cpp │ ├── search-insert-position.cpp │ ├── search-suggestions-system.cpp │ ├── searchMatrix2.cpp │ ├── seat-reservation-manager.cpp │ ├── second-largest-digit-in-a-string.cpp │ ├── second-minimum-node-in-a-binary-tree.cpp │ ├── self-crossing.cpp │ ├── self-dividing-numbers.cpp │ ├── sell-diminishing-valued-colored-balls.cpp │ ├── sentence-screen-fitting.cpp │ ├── sentence-similarity-ii.cpp │ ├── sentence-similarity-iii.cpp │ ├── sentence-similarity.cpp │ ├── sequence-reconstruction.cpp │ ├── sequential-digits.cpp │ ├── serialize-and-deserialize-binary-tree.cpp │ ├── serialize-and-deserialize-bst.cpp │ ├── serialize-and-deserialize-n-ary-tree.cpp │ ├── set-intersection-size-at-least-two.cpp │ ├── set-matrix-zeroes.cpp │ ├── set-mismatch.cpp │ ├── shift-2d-grid.cpp │ ├── shifting-letters.cpp │ ├── shopping-offers.cpp │ ├── short-encoding-of-words.cpp │ ├── shortest-bridge.cpp │ ├── shortest-common-supersequence.cpp │ ├── shortest-completing-word.cpp │ ├── shortest-distance-from-all-buildings.cpp │ ├── shortest-distance-to-a-character.cpp │ ├── shortest-distance-to-target-color.cpp │ ├── shortest-palindrome.cpp │ ├── shortest-path-in-a-grid-with-obstacles-elimination.cpp │ ├── shortest-path-in-a-hidden-grid.cpp │ ├── shortest-path-in-binary-matrix.cpp │ ├── shortest-path-to-get-all-keys.cpp │ ├── shortest-path-to-get-food.cpp │ ├── shortest-path-visiting-all-nodes.cpp │ ├── shortest-path-with-alternating-colors.cpp │ ├── shortest-subarray-to-be-removed-to-make-array-sorted.cpp │ ├── shortest-subarray-with-sum-at-least-k.cpp │ ├── shortest-unsorted-continuous-subarray.cpp │ ├── shortest-way-to-form-string.cpp │ ├── shortest-word-distance-ii.cpp │ ├── shortest-word-distance-iii.cpp │ ├── shortest-word-distance.cpp │ ├── shuffle-an-array.cpp │ ├── shuffle-string.cpp │ ├── shuffle-the-array.cpp │ ├── sign-of-the-product-of-an-array.cpp │ ├── similar-rgb-color.cpp │ ├── similar-string-groups.cpp │ ├── simplified-fractions.cpp │ ├── simplify-path.cpp │ ├── single-element-in-a-sorted-array.cpp │ ├── single-number-ii.cpp │ ├── single-number-iii.cpp │ ├── single-number.cpp │ ├── single-row-keyboard.cpp │ ├── single-threaded-cpu.cpp │ ├── sliding-puzzle.cpp │ ├── sliding-window-maximum.cpp │ ├── sliding-window-median.cpp │ ├── slowest-key.cpp │ ├── smallest-common-region.cpp │ ├── smallest-good-base.cpp │ ├── smallest-integer-divisible-by-k.cpp │ ├── smallest-range-i.cpp │ ├── smallest-range-ii.cpp │ ├── smallest-range.cpp │ ├── smallest-rectangle-enclosing-black-pixels.cpp │ ├── smallest-rotation-with-highest-score.cpp │ ├── smallest-string-starting-from-leaf.cpp │ ├── smallest-string-with-a-given-numeric-value.cpp │ ├── smallest-string-with-swaps.cpp │ ├── smallest-subsequence-of-distinct-characters.cpp │ ├── smallest-subtree-with-all-the-deepest-nodes.cpp │ ├── smallest-sufficient-team.cpp │ ├── snakes-and-ladders.cpp │ ├── snapshot-array.cpp │ ├── solve-the-equation.cpp │ ├── solveNQueens.cpp │ ├── solveSudoku.cpp │ ├── sort-an-array.cpp │ ├── sort-array-by-increasing-frequency.cpp │ ├── sort-array-by-parity-ii.cpp │ ├── sort-array-by-parity.cpp │ ├── sort-characters-by-frequency.cpp │ ├── sort-colors.cpp │ ├── sort-features-by-popularity.cpp │ ├── sort-integers-by-the-number-of-1-bits.cpp │ ├── sort-integers-by-the-power-value.cpp │ ├── sort-items-by-groups-respecting-dependencies.cpp │ ├── sort-list.cpp │ ├── sort-the-matrix-diagonally.cpp │ ├── sort-transformed-array.cpp │ ├── sortColors.cpp │ ├── sorting-the-sentence.cpp │ ├── soup-servings.cpp │ ├── sparse-matrix-multiplication.cpp │ ├── special-array-with-x-elements-greater-than-or-equal-x.cpp │ ├── special-binary-string.cpp │ ├── special-positions-in-a-binary-matrix.cpp │ ├── spiral-matrix-ii.cpp │ ├── spiral-matrix-iii.cpp │ ├── spiral-matrix.cpp │ ├── split-a-string-in-balanced-strings.cpp │ ├── split-a-string-into-the-max-number-of-unique-substrings.cpp │ ├── split-array-into-consecutive-subsequences.cpp │ ├── split-array-into-fibonacci-sequence.cpp │ ├── split-array-largest-sum.cpp │ ├── split-array-with-equal-sum.cpp │ ├── split-array-with-same-average.cpp │ ├── split-bst.cpp │ ├── split-concatenated-strings.cpp │ ├── split-linked-list-in-parts.cpp │ ├── split-two-strings-to-make-palindrome.cpp │ ├── splitting-a-string-into-descending-consecutive-values.cpp │ ├── sqrtx.cpp │ ├── squares-of-a-sorted-array.cpp │ ├── squirrel-simulation.cpp │ ├── stamping-the-sequence.cpp │ ├── statistics-from-a-large-sample.cpp │ ├── stepping-numbers.cpp │ ├── stickers-to-spell-word.cpp │ ├── stone-game-ii.cpp │ ├── stone-game-iii.cpp │ ├── stone-game-iv.cpp │ ├── stone-game-v.cpp │ ├── stone-game-vi.cpp │ ├── stone-game-vii.cpp │ ├── stone-game-viii.cpp │ ├── stone-game.cpp │ ├── strange-printer-ii.cpp │ ├── strange-printer.cpp │ ├── stream-of-characters.cpp │ ├── stream-of-characters2.cpp │ ├── string-compression-ii.cpp │ ├── string-compression.cpp │ ├── string-matching-in-an-array.cpp │ ├── string-to-integer-atoi.cpp │ ├── string-transforms-into-another-string.cpp │ ├── string-without-aaa-or-bbb.cpp │ ├── strings-differ-by-one-character.cpp │ ├── strobogrammatic-number-ii.cpp │ ├── strobogrammatic-number-iii.cpp │ ├── strobogrammatic-number.cpp │ ├── strong-password-checker.cpp │ ├── student-attendance-record-i.cpp │ ├── student-attendance-record-ii.cpp │ ├── subarray-product-less-than-k.cpp │ ├── subarray-sum-equals-k.cpp │ ├── subarray-sums-divisible-by-k.cpp │ ├── subarrays-with-k-different-integers.cpp │ ├── subdomain-visit-count.cpp │ ├── subrectangle-queries.cpp │ ├── subsets-ii.cpp │ ├── subsets.cpp │ ├── substring-with-concatenation-of-all-words.cpp │ ├── subtract-the-product-and-sum-of-digits-of-an-integer.cpp │ ├── subtree-of-another-tree.cpp │ ├── sum-of-absolute-differences-in-a-sorted-array.cpp │ ├── sum-of-all-odd-length-subarrays.cpp │ ├── sum-of-all-subset-xor-totals.cpp │ ├── sum-of-beauty-of-all-substrings.cpp │ ├── sum-of-digits-in-base-k.cpp │ ├── sum-of-digits-in-the-minimum-number.cpp │ ├── sum-of-distances-in-tree.cpp │ ├── sum-of-even-numbers-after-queries.cpp │ ├── sum-of-floored-pairs.cpp │ ├── sum-of-left-leaves.cpp │ ├── sum-of-mutated-array-closest-to-target.cpp │ ├── sum-of-nodes-with-even-valued-grandparent.cpp │ ├── sum-of-root-to-leaf-binary-numbers.cpp │ ├── sum-of-special-evenly-spaced-elements-in-array.cpp │ ├── sum-of-square-numbers.cpp │ ├── sum-of-subarray-minimums.cpp │ ├── sum-of-subsequence-widths.cpp │ ├── sum-of-two-integers.cpp │ ├── sum-of-unique-elements.cpp │ ├── sumNumbers.cpp │ ├── summary-ranges.cpp │ ├── super-egg-drop.cpp │ ├── super-palindromes.cpp │ ├── super-pow.cpp │ ├── super-ugly-number.cpp │ ├── super-washing-machines.cpp │ ├── surface-area-of-3d-shapes.cpp │ ├── surrounded-regions.cpp │ ├── surroundedRegions.cpp │ ├── swap-adjacent-in-lr-string.cpp │ ├── swap-for-longest-repeated-character-substring.cpp │ ├── swap-nodes-in-pairs.cpp │ ├── swapping-nodes-in-a-linked-list.cpp │ ├── swim-in-rising-water.cpp │ ├── symmetric-tree.cpp │ ├── synonymous-sentences.cpp │ ├── tag-validator.cpp │ ├── tallest-billboard.cpp │ ├── target-sum.cpp │ ├── task-scheduler.cpp │ ├── teemo-attacking.cpp │ ├── ternary-expression-parser.cpp │ ├── text-justification.cpp │ ├── the-dining-philosophers.cpp │ ├── the-earliest-moment-when-everyone-become-friends.cpp │ ├── the-k-strongest-values-in-an-array.cpp │ ├── the-k-th-lexicographical-string-of-all-happy-strings-of-length-n.cpp │ ├── the-k-weakest-rows-in-a-matrix.cpp │ ├── the-kth-factor-of-n.cpp │ ├── the-maze-ii.cpp │ ├── the-maze-iii.cpp │ ├── the-maze.cpp │ ├── the-most-similar-path-in-a-graph.cpp │ ├── the-skyline-problem.cpp │ ├── third-maximum-number.cpp │ ├── thousand-separator.cpp │ ├── three-consecutive-odds.cpp │ ├── three-equal-parts.cpp │ ├── threeSum2.cpp │ ├── throne-inheritance.cpp │ ├── tiling-a-rectangle-with-the-fewest-squares.cpp │ ├── time-based-key-value-store.cpp │ ├── time-needed-to-inform-all-employees.cpp │ ├── to-lower-case.cpp │ ├── toeplitz-matrix.cpp │ ├── top-k-frequent-elements.cpp │ ├── top-k-frequent-words.cpp │ ├── toss-strange-coins.cpp │ ├── total-hamming-distance.cpp │ ├── totalNQueens.cpp │ ├── traffic-light-controlled-intersection.cpp │ ├── transform-to-chessboard.cpp │ ├── transpose-matrix.cpp │ ├── trapping-rain-water-ii.cpp │ ├── trapping-rain-water.cpp │ ├── tree-diameter.cpp │ ├── tree-of-coprimes.cpp │ ├── trim-a-binary-search-tree.cpp │ ├── triples-with-bitwise-and-equal-to-zero.cpp │ ├── truncate-sentence.cpp │ ├── tuple-with-same-product.cpp │ ├── tweet-counts-per-frequency.cpp │ ├── two-city-scheduling.cpp │ ├── two-sum-bsts.cpp │ ├── two-sum-ii-input-array-is-sorted.cpp │ ├── two-sum-iii-data-structure-design.cpp │ ├── two-sum-iv-input-is-a-bst.cpp │ ├── two-sum-less-than-k.cpp │ ├── two-sum.cpp │ ├── ugly-number-ii.cpp │ ├── ugly-number-iii.cpp │ ├── ugly-number.cpp │ ├── uncommon-words-from-two-sentences.cpp │ ├── uncrossed-lines.cpp │ ├── unique-binary-search-trees-ii.cpp │ ├── unique-email-addresses.cpp │ ├── unique-morse-code-words.cpp │ ├── unique-number-of-occurrences.cpp │ ├── unique-paths-iii.cpp │ ├── unique-substrings-in-wraparound-string.cpp │ ├── unique-word-abbreviation.cpp │ ├── uniquePathWithObstacles.cpp │ ├── uniquePaths.cpp │ ├── uniquePaths2.cpp │ ├── univalued-binary-tree.cpp │ ├── utf-8-validation.cpp │ ├── valid-anagram.cpp │ ├── valid-boomerang.cpp │ ├── valid-mountain-array.cpp │ ├── valid-number.cpp │ ├── valid-palindrome-ii.cpp │ ├── valid-palindrome-iii.cpp │ ├── valid-palindrome.cpp │ ├── valid-parentheses.cpp │ ├── valid-parenthesis-string.cpp │ ├── valid-perfect-square.cpp │ ├── valid-permutations-for-di-sequence.cpp │ ├── valid-square.cpp │ ├── valid-sudoku.cpp │ ├── valid-tic-tac-toe-state.cpp │ ├── valid-triangle-number.cpp │ ├── valid-word-abbreviation.cpp │ ├── valid-word-square.cpp │ ├── validate-binary-search-tree.cpp │ ├── validate-binary-tree-nodes.cpp │ ├── validate-ip-address.cpp │ ├── validate-stack-sequences.cpp │ ├── verbal-arithmetic-puzzle.cpp │ ├── verify-preorder-sequence-in-binary-search-tree.cpp │ ├── verify-preorder-serialization-of-a-binary-tree.cpp │ ├── verifying-an-alien-dictionary.cpp │ ├── vertical-order-traversal-of-a-binary-tree.cpp │ ├── video-stitching.cpp │ ├── vowel-spellchecker.cpp │ ├── walking-robot-simulation.cpp │ ├── walls-and-gates.cpp │ ├── water-and-jug-problem.cpp │ ├── water-bottles.cpp │ ├── ways-to-make-a-fair-array.cpp │ ├── ways-to-split-array-into-three-subarrays.cpp │ ├── web-crawler-multithreaded.cpp │ ├── web-crawler.cpp │ ├── where-will-the-ball-fall.cpp │ ├── widest-vertical-area-between-two-points-containing-no-points.cpp │ ├── wiggle-sort-ii.cpp │ ├── wiggle-sort.cpp │ ├── wiggle-subsequence.cpp │ ├── word-abbreviation.cpp │ ├── word-break-ii.cpp │ ├── word-break.cpp │ ├── word-ladder-ii.cpp │ ├── word-ladder.cpp │ ├── word-pattern-ii.cpp │ ├── word-pattern.cpp │ ├── word-search-ii.cpp │ ├── word-search.cpp │ ├── word-squares.cpp │ ├── word-subsets.cpp │ ├── wordBreakII.cpp │ ├── x-of-a-kind-in-a-deck-of-cards.cpp │ ├── xor-operation-in-an-array.cpp │ ├── xor-queries-of-a-subarray.cpp │ ├── zigzag-conversion.cpp │ ├── zigzag-iterator.cpp │ ├── zigzagLevelOrder.cpp │ └── zuma-game.cpp ├── KadanesAlgo.java ├── Knapsack.java ├── Largest rectangular area in histogram.cpp ├── Next_greater_element_II.cpp ├── NodeWithHighestEdgeScore.cpp ├── Python │ ├── 01-matrix.py │ ├── 1-bit-and-2-bit-characters.py │ ├── 132-pattern.py │ ├── 2-keys-keyboard.py │ ├── 24-game.py │ ├── 3sum-closest.py │ ├── 3sum-smaller.py │ ├── 3sum-with-multiplicity.py │ ├── 3sum.py │ ├── 4-keys-keyboard.py │ ├── 4sum-ii.py │ ├── 4sum.py │ ├── accounts-merge.py │ ├── add-and-search-word-data-structure-design.py │ ├── add-binary.py │ ├── add-bold-tag-in-string.py │ ├── add-digits.py │ ├── add-one-row-to-tree.py │ ├── add-strings.py │ ├── add-to-array-form-of-integer.py │ ├── add-two-numbers-ii.py │ ├── add-two-numbers.py │ ├── add-two-polynomials-represented-as-linked-lists.py │ ├── adding-two-negabinary-numbers.py │ ├── additive-number.py │ ├── advantage-shuffle.py │ ├── airplane-seat-assignment-probability.py │ ├── alert-using-same-key-card-three-or-more-times-in-a-one-hour-period.py │ ├── alien-dictionary.py │ ├── all-elements-in-two-binary-search-trees.py │ ├── all-nodes-distance-k-in-binary-tree.py │ ├── all-oone-data-structure.py │ ├── all-paths-from-source-lead-to-destination.py │ ├── all-paths-from-source-to-target.py │ ├── all-possible-full-binary-trees.py │ ├── allocate-mailboxes.py │ ├── alphabet-board-path.py │ ├── ambiguous-coordinates.py │ ├── analyze-user-website-visit-pattern.py │ ├── android-unlock-patterns.py │ ├── angle-between-hands-of-a-clock.py │ ├── apply-discount-every-n-orders.py │ ├── arithmetic-slices-ii-subsequence.py │ ├── arithmetic-slices.py │ ├── arithmetic-subarrays.py │ ├── armstrong-number.py │ ├── arranging-coins.py │ ├── array-nesting.py │ ├── array-of-doubled-pairs.py │ ├── array-partition-i.py │ ├── array-transformation.py │ ├── as-far-from-land-as-possible.py │ ├── assign-cookies.py │ ├── asteroid-collision.py │ ├── available-captures-for-rook.py │ ├── average-of-levels-in-binary-tree.py │ ├── average-salary-excluding-the-minimum-and-maximum-salary.py │ ├── average-waiting-time.py │ ├── avoid-flood-in-the-city.py │ ├── backspace-string-compare.py │ ├── bag-of-tokens.py │ ├── balance-a-binary-search-tree.py │ ├── balanced-binary-tree.py │ ├── base-7.py │ ├── baseball-game.py │ ├── basic-calculator-ii.py │ ├── basic-calculator-iii.py │ ├── basic-calculator-iv.py │ ├── basic-calculator.py │ ├── battleships-in-a-board.py │ ├── beautiful-arrangement-ii.py │ ├── beautiful-arrangement.py │ ├── beautiful-array.py │ ├── before-and-after-puzzle.py │ ├── best-meeting-point.py │ ├── best-position-for-a-service-centre.py │ ├── best-sightseeing-pair.py │ ├── best-team-with-no-conflicts.py │ ├── best-time-to-buy-and-sell-stock-ii.py │ ├── best-time-to-buy-and-sell-stock-iii.py │ ├── best-time-to-buy-and-sell-stock-iv.py │ ├── best-time-to-buy-and-sell-stock-with-cooldown.py │ ├── best-time-to-buy-and-sell-stock-with-transaction-fee.py │ ├── best-time-to-buy-and-sell-stock.py │ ├── binary-gap.py │ ├── binary-number-with-alternating-bits.py │ ├── binary-prefix-divisible-by-5.py │ ├── binary-search-tree-iterator-ii.py │ ├── binary-search-tree-iterator.py │ ├── binary-search-tree-to-greater-sum-tree.py │ ├── binary-search.py │ ├── binary-string-with-substrings-representing-1-to-n.py │ ├── binary-subarrays-with-sum.py │ ├── binary-tree-cameras.py │ ├── binary-tree-coloring-game.py │ ├── binary-tree-inorder-traversal.py │ ├── binary-tree-level-order-traversal-ii.py │ ├── binary-tree-level-order-traversal.py │ ├── binary-tree-longest-consecutive-sequence-ii.py │ ├── binary-tree-longest-consecutive-sequence.py │ ├── binary-tree-maximum-path-sum.py │ ├── binary-tree-paths.py │ ├── binary-tree-postorder-traversal.py │ ├── binary-tree-preorder-traversal.py │ ├── binary-tree-pruning.py │ ├── binary-tree-right-side-view.py │ ├── binary-tree-tilt.py │ ├── binary-tree-upside-down.py │ ├── binary-tree-vertical-order-traversal.py │ ├── binary-tree-zigzag-level-order-traversal.py │ ├── binary-trees-with-factors.py │ ├── binary-watch.py │ ├── bitwise-and-of-numbers-range.py │ ├── bitwise-ors-of-subarrays.py │ ├── boats-to-save-people.py │ ├── bold-words-in-string.py │ ├── bomb-enemy.py │ ├── boundary-of-binary-tree.py │ ├── brace-expansion-ii.py │ ├── brace-expansion.py │ ├── break-a-palindrome.py │ ├── brick-wall.py │ ├── bricks-falling-when-hit.py │ ├── broken-calculator.py │ ├── buddy-strings.py │ ├── build-an-array-with-stack-operations.py │ ├── build-array-where-you-can-find-the-maximum-exactly-k-comparisons.py │ ├── build-binary-expression-tree-from-infix-expression.py │ ├── building-boxes.py │ ├── building-h2o.py │ ├── buildings-with-an-ocean-view.py │ ├── bulb-switcher-ii.py │ ├── bulb-switcher-iii.py │ ├── bulb-switcher-iv.py │ ├── bulb-switcher.py │ ├── bulls-and-cows.py │ ├── burst-balloons.py │ ├── bus-routes.py │ ├── calculate-money-in-leetcode-bank.py │ ├── camelcase-matching.py │ ├── campus-bikes-ii.py │ ├── campus-bikes.py │ ├── can-convert-string-in-k-moves.py │ ├── can-i-win.py │ ├── can-make-arithmetic-progression-from-sequence.py │ ├── can-make-palindrome-from-substring.py │ ├── can-place-flowers.py │ ├── can-you-eat-your-favorite-candy-on-your-favorite-day.py │ ├── candy-crush.py │ ├── candy.py │ ├── capacity-to-ship-packages-within-d-days.py │ ├── car-fleet-ii.py │ ├── car-fleet.py │ ├── car-pooling.py │ ├── card-flipping-game.py │ ├── cat-and-mouse-ii.py │ ├── cat-and-mouse.py │ ├── cells-with-odd-values-in-a-matrix.py │ ├── chalkboard-xor-game.py │ ├── champagne-tower.py │ ├── change-minimum-characters-to-satisfy-one-of-three-conditions.py │ ├── change-the-root-of-a-binary-tree.py │ ├── cheapest-flights-within-k-stops.py │ ├── check-array-formation-through-concatenation.py │ ├── check-completeness-of-a-binary-tree.py │ ├── check-if-a-number-is-majority-element-in-a-sorted-array.py │ ├── check-if-a-string-can-break-another-string.py │ ├── check-if-a-string-contains-all-binary-codes-of-size-k.py │ ├── check-if-a-string-is-a-valid-sequence-from-root-to-leaves-path-in-a-binary-tree.py │ ├── check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence.py │ ├── check-if-all-1s-are-at-least-length-k-places-away.py │ ├── check-if-array-is-sorted-and-rotated.py │ ├── check-if-array-pairs-are-divisible-by-k.py │ ├── check-if-binary-string-has-at-most-one-segment-of-ones.py │ ├── check-if-it-is-a-good-array.py │ ├── check-if-it-is-a-straight-line.py │ ├── check-if-n-and-its-double-exist.py │ ├── check-if-number-is-a-sum-of-powers-of-three.py │ ├── check-if-one-string-swap-can-make-strings-equal.py │ ├── check-if-string-is-transformable-with-substring-sort-operations.py │ ├── check-if-the-sentence-is-pangram.py │ ├── check-if-there-is-a-valid-path-in-a-grid.py │ ├── check-if-two-expression-trees-are-equivalent.py │ ├── check-if-two-string-arrays-are-equivalent.py │ ├── check-if-word-is-valid-after-substitutions.py │ ├── checking-existence-of-edge-length-limited-paths-ii.py │ ├── checking-existence-of-edge-length-limited-paths.py │ ├── cherry-pickup-ii.py │ ├── cherry-pickup.py │ ├── cinema-seat-allocation.py │ ├── circle-and-rectangle-overlapping.py │ ├── circular-array-loop.py │ ├── circular-permutation-in-binary-representation.py │ ├── climbing-stairs.py │ ├── clone-binary-tree-with-random-pointer.py │ ├── clone-graph.py │ ├── clone-n-ary-tree.py │ ├── closest-binary-search-tree-value-ii.py │ ├── closest-binary-search-tree-value.py │ ├── closest-dessert-cost.py │ ├── closest-divisors.py │ ├── closest-leaf-in-a-binary-tree.py │ ├── closest-room.py │ ├── closest-subsequence-sum.py │ ├── clumsy-factorial.py │ ├── coin-change-2.py │ ├── coin-change.py │ ├── coin-path.py │ ├── coloring-a-border.py │ ├── combination-sum-ii.py │ ├── combination-sum-iii.py │ ├── combination-sum-iv.py │ ├── combination-sum.py │ ├── combinations.py │ ├── compare-strings-by-frequency-of-the-smallest-character.py │ ├── compare-version-numbers.py │ ├── complement-of-base-10-integer.py │ ├── complete-binary-tree-inserter.py │ ├── complex-number-multiplication.py │ ├── concatenated-words.py │ ├── concatenation-of-consecutive-binary-numbers.py │ ├── confusing-number-ii.py │ ├── confusing-number.py │ ├── connecting-cities-with-minimum-cost.py │ ├── consecutive-characters.py │ ├── consecutive-numbers-sum.py │ ├── constrained-subset-sum.py │ ├── construct-binary-search-tree-from-preorder-traversal.py │ ├── construct-binary-tree-from-inorder-and-postorder-traversal.py │ ├── construct-binary-tree-from-preorder-and-inorder-traversal.py │ ├── construct-binary-tree-from-preorder-and-postorder-traversal.py │ ├── construct-binary-tree-from-string.py │ ├── construct-k-palindrome-strings.py │ ├── construct-quad-tree.py │ ├── construct-string-from-binary-tree.py │ ├── construct-target-array-with-multiple-sums.py │ ├── construct-the-lexicographically-largest-valid-sequence.py │ ├── construct-the-rectangle.py │ ├── contain-virus.py │ ├── container-with-most-water.py │ ├── contains-duplicate-ii.py │ ├── contains-duplicate-iii.py │ ├── contains-duplicate.py │ ├── contiguous-array.py │ ├── continuous-subarray-sum.py │ ├── convert-a-number-to-hexadecimal.py │ ├── convert-binary-number-in-a-linked-list-to-integer.py │ ├── convert-binary-search-tree-to-sorted-doubly-linked-list.py │ ├── convert-bst-to-greater-tree.py │ ├── convert-integer-to-the-sum-of-two-no-zero-integers.py │ ├── convert-sorted-array-to-binary-search-tree.py │ ├── convert-sorted-list-to-binary-search-tree.py │ ├── convert-to-base-2.py │ ├── convex-polygon.py │ ├── coordinate-with-maximum-network-quality.py │ ├── copy-list-with-random-pointer.py │ ├── corporate-flight-bookings.py │ ├── correct-a-binary-tree.py │ ├── count-all-possible-routes.py │ ├── count-all-valid-pickup-and-delivery-options.py │ ├── count-and-say.py │ ├── count-binary-substrings.py │ ├── count-complete-tree-nodes.py │ ├── count-different-palindromic-subsequences.py │ ├── count-good-meals.py │ ├── count-good-nodes-in-binary-tree.py │ ├── count-good-triplets.py │ ├── count-items-matching-a-rule.py │ ├── count-largest-group.py │ ├── count-negative-numbers-in-a-sorted-matrix.py │ ├── count-nice-pairs-in-an-array.py │ ├── count-number-of-homogenous-substrings.py │ ├── count-number-of-nice-subarrays.py │ ├── count-number-of-teams.py │ ├── count-numbers-with-unique-digits.py │ ├── count-odd-numbers-in-an-interval-range.py │ ├── count-of-matches-in-tournament.py │ ├── count-of-range-sum.py │ ├── count-of-smaller-numbers-after-self.py │ ├── count-pairs-of-equal-substrings-with-minimum-difference.py │ ├── count-pairs-of-nodes.py │ ├── count-pairs-with-xor-in-a-range.py │ ├── count-primes.py │ ├── count-servers-that-communicate.py │ ├── count-sorted-vowel-strings.py │ ├── count-square-submatrices-with-all-ones.py │ ├── count-submatrices-with-all-ones.py │ ├── count-substrings-that-differ-by-one-character.py │ ├── count-substrings-with-only-one-distinct-letter.py │ ├── count-subtrees-with-max-distance-between-cities.py │ ├── count-the-number-of-consistent-strings.py │ ├── count-the-repetitions.py │ ├── count-triplets-that-can-form-two-arrays-of-equal-xor.py │ ├── count-unhappy-friends.py │ ├── count-unique-characters-of-all-substrings-of-a-given-string.py │ ├── count-univalue-subtrees.py │ ├── count-vowels-permutation.py │ ├── count-ways-to-distribute-candies.py │ ├── count-ways-to-make-array-with-product.py │ ├── counting-bits.py │ ├── counting-elements.py │ ├── couples-holding-hands.py │ ├── course-schedule-ii.py │ ├── course-schedule-iii.py │ ├── course-schedule-iv.py │ ├── course-schedule.py │ ├── cousins-in-binary-tree.py │ ├── cracking-the-safe.py │ ├── crawler-log-folder.py │ ├── create-maximum-number.py │ ├── create-sorted-array-through-instructions.py │ ├── create-target-array-in-the-given-order.py │ ├── critical-connections-in-a-network.py │ ├── custom-sort-string.py │ ├── cut-off-trees-for-golf-event.py │ ├── daily-temperatures.py │ ├── data-stream-as-disjoint-intervals.py │ ├── day-of-the-week.py │ ├── day-of-the-year.py │ ├── decode-string.py │ ├── decode-ways-ii.py │ ├── decode-ways.py │ ├── decode-xored-array.py │ ├── decode-xored-permutation.py │ ├── decoded-string-at-index.py │ ├── decompress-run-length-encoded-list.py │ ├── decrease-elements-to-make-array-zigzag.py │ ├── decrypt-string-from-alphabet-to-integer-mapping.py │ ├── deepest-leaves-sum.py │ ├── defanging-an-ip-address.py │ ├── defuse-the-bomb.py │ ├── degree-of-an-array.py │ ├── delete-and-earn.py │ ├── delete-columns-to-make-sorted-ii.py │ ├── delete-columns-to-make-sorted-iii.py │ ├── delete-columns-to-make-sorted.py │ ├── delete-leaves-with-a-given-value.py │ ├── delete-n-nodes-after-m-nodes-of-a-linked-list.py │ ├── delete-node-in-a-bst.py │ ├── delete-node-in-a-linked-list.py │ ├── delete-nodes-and-return-forest.py │ ├── delete-operation-for-two-strings.py │ ├── delete-tree-nodes.py │ ├── delivering-boxes-from-storage-to-ports.py │ ├── design-a-file-sharing-system.py │ ├── design-a-leaderboard.py │ ├── design-a-stack-with-increment-operation.py │ ├── design-an-expression-tree-with-evaluate-function.py │ ├── design-an-ordered-stream.py │ ├── design-authentication-manager.py │ ├── design-bounded-blocking-queue.py │ ├── design-browser-history.py │ ├── design-circular-deque.py │ ├── design-circular-queue.py │ ├── design-compressed-string-iterator.py │ ├── design-excel-sum-formula.py │ ├── design-file-system.py │ ├── design-front-middle-back-queue.py │ ├── design-hashmap.py │ ├── design-hashset.py │ ├── design-hit-counter.py │ ├── design-in-memory-file-system.py │ ├── design-linked-list.py │ ├── design-log-storage-system.py │ ├── design-most-recently-used-queue.py │ ├── design-parking-system.py │ ├── design-phone-directory.py │ ├── design-search-autocomplete-system.py │ ├── design-skiplist.py │ ├── design-snake-game.py │ ├── design-tic-tac-toe.py │ ├── design-twitter.py │ ├── design-underground-system.py │ ├── destination-city.py │ ├── detect-capital.py │ ├── detect-cycles-in-2d-grid.py │ ├── detect-pattern-of-length-m-repeated-k-or-more-times.py │ ├── determine-color-of-a-chessboard-square.py │ ├── determine-if-string-halves-are-alike.py │ ├── determine-if-two-strings-are-close.py │ ├── di-string-match.py │ ├── diagonal-traverse-ii.py │ ├── diagonal-traverse.py │ ├── diameter-of-binary-tree.py │ ├── diameter-of-n-ary-tree.py │ ├── dice-roll-simulation.py │ ├── diet-plan-performance.py │ ├── different-ways-to-add-parentheses.py │ ├── digit-count-in-range.py │ ├── dinner-plate-stacks.py │ ├── display-table-of-food-orders-in-a-restaurant.py │ ├── distance-between-bus-stops.py │ ├── distant-barcodes.py │ ├── distinct-echo-substrings.py │ ├── distinct-numbers-in-each-subarray.py │ ├── distinct-subsequences-ii.py │ ├── distinct-subsequences.py │ ├── distribute-candies-to-people.py │ ├── distribute-candies.py │ ├── distribute-coins-in-binary-tree.py │ ├── distribute-repeating-integers.py │ ├── divide-array-in-sets-of-k-consecutive-numbers.py │ ├── divide-array-into-increasing-sequences.py │ ├── divide-chocolate.py │ ├── divide-two-integers.py │ ├── divisor-game.py │ ├── domino-and-tromino-tiling.py │ ├── dot-product-of-two-sparse-vectors.py │ ├── dota2-senate.py │ ├── dungeon-game.py │ ├── duplicate-zeros.py │ ├── edit-distance.py │ ├── element-appearing-more-than-25-in-sorted-array.py │ ├── elimination-game.py │ ├── employee-free-time.py │ ├── employee-importance.py │ ├── encode-and-decode-strings.py │ ├── encode-and-decode-tinyurl.py │ ├── encode-n-ary-tree-to-binary-tree.py │ ├── encode-number.py │ ├── encode-string-with-shortest-length.py │ ├── equal-rational-numbers.py │ ├── equal-sum-arrays-with-minimum-number-of-operations.py │ ├── equal-tree-partition.py │ ├── erect-the-fence.py │ ├── escape-a-large-maze.py │ ├── escape-the-ghosts.py │ ├── evaluate-division.py │ ├── evaluate-reverse-polish-notation.py │ ├── evaluate-the-bracket-pairs-of-a-string.py │ ├── even-odd-tree.py │ ├── exam-room.py │ ├── excel-sheet-column-number.py │ ├── excel-sheet-column-title.py │ ├── exclusive-time-of-functions.py │ ├── expression-add-operators.py │ ├── expressive-words.py │ ├── factor-combinations.py │ ├── factorial-trailing-zeroes.py │ ├── fair-candy-swap.py │ ├── falling-squares.py │ ├── fancy-sequence.py │ ├── faulty-sensor.py │ ├── fibonacci-number.py │ ├── filling-bookcase-shelves.py │ ├── filter-restaurants-by-vegan-friendly-price-and-distance.py │ ├── final-prices-with-a-special-discount-in-a-shop.py │ ├── find-a-corresponding-node-of-a-binary-tree-in-a-clone-of-that-tree.py │ ├── find-a-value-of-a-mysterious-function-closest-to-target.py │ ├── find-all-anagrams-in-a-string.py │ ├── find-all-duplicates-in-an-array.py │ ├── find-all-good-strings.py │ ├── find-all-numbers-disappeared-in-an-array.py │ ├── find-all-the-lonely-nodes.py │ ├── find-anagram-mappings.py │ ├── find-and-replace-in-string.py │ ├── find-and-replace-pattern.py │ ├── find-bottom-left-tree-value.py │ ├── find-center-of-star-graph.py │ ├── find-common-characters.py │ ├── find-critical-and-pseudo-critical-edges-in-minimum-spanning-tree.py │ ├── find-distance-in-a-binary-tree.py │ ├── find-duplicate-file-in-system.py │ ├── find-duplicate-subtrees.py │ ├── find-elements-in-a-contaminated-binary-tree.py │ ├── find-eventual-safe-states.py │ ├── find-in-mountain-array.py │ ├── find-k-closest-elements.py │ ├── find-k-length-substrings-with-no-repeated-characters.py │ ├── find-k-pairs-with-smallest-sums.py │ ├── find-k-th-smallest-pair-distance.py │ ├── find-kth-bit-in-nth-binary-string.py │ ├── find-kth-largest-xor-coordinate-value.py │ ├── find-largest-value-in-each-tree-row.py │ ├── find-latest-group-of-size-m.py │ ├── find-leaves-of-binary-tree.py │ ├── find-longest-awesome-substring.py │ ├── find-lucky-integer-in-an-array.py │ ├── find-median-from-data-stream.py │ ├── find-minimum-in-rotated-sorted-array-ii.py │ ├── find-minimum-in-rotated-sorted-array.py │ ├── find-minimum-time-to-finish-all-jobs.py │ ├── find-mode-in-binary-search-tree.py │ ├── find-n-unique-integers-sum-up-to-zero.py │ ├── find-nearest-point-that-has-the-same-x-or-y-coordinate.py │ ├── find-nearest-right-node-in-binary-tree.py │ ├── find-numbers-with-even-number-of-digits.py │ ├── find-peak-element.py │ ├── find-permutation.py │ ├── find-pivot-index.py │ ├── find-positive-integer-solution-for-a-given-equation.py │ ├── find-right-interval.py │ ├── find-root-of-n-ary-tree.py │ ├── find-servers-that-handled-most-number-of-requests.py │ ├── find-smallest-common-element-in-all-rows.py │ ├── find-smallest-letter-greater-than-target.py │ ├── find-the-celebrity.py │ ├── find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance.py │ ├── find-the-closest-palindrome.py │ ├── find-the-derangement-of-an-array.py │ ├── find-the-difference.py │ ├── find-the-distance-value-between-two-arrays.py │ ├── find-the-duplicate-number.py │ ├── find-the-highest-altitude.py │ ├── find-the-index-of-the-large-integer.py │ ├── find-the-kth-smallest-sum-of-a-matrix-with-sorted-rows.py │ ├── find-the-longest-substring-containing-vowels-in-even-counts.py │ ├── find-the-minimum-number-of-fibonacci-numbers-whose-sum-is-k.py │ ├── find-the-most-competitive-subsequence.py │ ├── find-the-shortest-superstring.py │ ├── find-the-smallest-divisor-given-a-threshold.py │ ├── find-the-town-judge.py │ ├── find-the-winner-of-an-array-game.py │ ├── find-the-winner-of-the-circular-game.py │ ├── find-two-non-overlapping-sub-arrays-each-with-target-sum.py │ ├── find-valid-matrix-given-row-and-column-sums.py │ ├── find-winner-on-a-tic-tac-toe-game.py │ ├── find-words-that-can-be-formed-by-characters.py │ ├── find-xor-sum-of-all-pairs-bitwise-and.py │ ├── finding-mk-average.py │ ├── finding-pairs-with-a-certain-sum.py │ ├── finding-the-users-active-minutes.py │ ├── first-bad-version.py │ ├── first-missing-positive.py │ ├── first-unique-character-in-a-string.py │ ├── first-unique-number.py │ ├── fixed-point.py │ ├── fizz-buzz-multithreaded.py │ ├── fizz-buzz.py │ ├── flatten-2d-vector.py │ ├── flatten-a-multilevel-doubly-linked-list.py │ ├── flatten-binary-tree-to-linked-list.py │ ├── flatten-nested-list-iterator.py │ ├── flip-binary-tree-to-match-preorder-traversal.py │ ├── flip-columns-for-maximum-number-of-equal-rows.py │ ├── flip-equivalent-binary-trees.py │ ├── flip-game-ii.py │ ├── flip-game.py │ ├── flip-string-to-monotone-increasing.py │ ├── flipping-an-image.py │ ├── flood-fill.py │ ├── flower-planting-with-no-adjacent.py │ ├── form-array-by-concatenating-subarrays-of-another-array.py │ ├── form-largest-integer-with-digits-that-add-up-to-target.py │ ├── four-divisors.py │ ├── fraction-addition-and-subtraction.py │ ├── fraction-to-recurring-decimal.py │ ├── freedom-trail.py │ ├── frequency-of-the-most-frequent-element.py │ ├── friend-circles.py │ ├── friends-of-appropriate-ages.py │ ├── frog-jump.py │ ├── frog-position-after-t-seconds.py │ ├── fruit-into-baskets.py │ ├── furthest-building-you-can-reach.py │ ├── game-of-life.py │ ├── gas-station.py │ ├── generalized-abbreviation.py │ ├── generate-a-string-with-characters-that-have-odd-counts.py │ ├── generate-parentheses.py │ ├── generate-random-point-in-a-circle.py │ ├── get-equal-substrings-within-budget.py │ ├── get-maximum-in-generated-array.py │ ├── get-the-maximum-score.py │ ├── get-watched-videos-by-your-friends.py │ ├── global-and-local-inversions.py │ ├── goal-parser-interpretation.py │ ├── goat-latin.py │ ├── graph-connectivity-with-threshold.py │ ├── graph-valid-tree.py │ ├── gray-code.py │ ├── greatest-common-divisor-of-strings.py │ ├── greatest-sum-divisible-by-three.py │ ├── grid-illumination.py │ ├── group-anagrams.py │ ├── group-shifted-strings.py │ ├── group-the-people-given-the-group-size-they-belong-to.py │ ├── groups-of-special-equivalent-strings.py │ ├── grumpy-bookstore-owner.py │ ├── guess-number-higher-or-lower-ii.py │ ├── guess-number-higher-or-lower.py │ ├── guess-the-majority-in-a-hidden-array.py │ ├── guess-the-word.py │ ├── h-index-ii.py │ ├── h-index.py │ ├── hamming-distance.py │ ├── hand-of-straights.py │ ├── handshakes-that-dont-cross.py │ ├── happy-number.py │ ├── heaters.py │ ├── height-checker.py │ ├── hexspeak.py │ ├── high-five.py │ ├── house-robber-ii.py │ ├── house-robber-iii.py │ ├── house-robber.py │ ├── how-many-apples-can-you-put-into-the-basket.py │ ├── how-many-numbers-are-smaller-than-the-current-number.py │ ├── html-entity-parser.py │ ├── image-overlap.py │ ├── image-smoother.py │ ├── implement-magic-dictionary.py │ ├── implement-queue-using-stacks.py │ ├── implement-rand10-using-rand7.py │ ├── implement-stack-using-queues.py │ ├── implement-strstr.py │ ├── implement-trie-ii-prefix-tree.py │ ├── implement-trie-prefix-tree.py │ ├── increasing-decreasing-string.py │ ├── increasing-order-search-tree.py │ ├── increasing-subsequences.py │ ├── increasing-triplet-subsequence.py │ ├── incremental-memory-leak.py │ ├── index-pairs-of-a-string.py │ ├── inorder-successor-in-bst-ii.py │ ├── inorder-successor-in-bst.py │ ├── insert-delete-getrandom-o1-duplicates-allowed.py │ ├── insert-delete-getrandom-o1.py │ ├── insert-interval.py │ ├── insert-into-a-binary-search-tree.py │ ├── insert-into-a-cyclic-sorted-list.py │ ├── insertion-sort-list.py │ ├── insufficient-nodes-in-root-to-leaf-paths.py │ ├── integer-break.py │ ├── integer-replacement.py │ ├── integer-to-english-words.py │ ├── integer-to-roman.py │ ├── interleaving-string.py │ ├── intersection-of-three-sorted-arrays.py │ ├── intersection-of-two-arrays-ii.py │ ├── intersection-of-two-arrays.py │ ├── intersection-of-two-linked-lists.py │ ├── interval-list-intersections.py │ ├── invalid-transactions.py │ ├── invert-binary-tree.py │ ├── ip-to-cidr.py │ ├── ipo.py │ ├── is-graph-bipartite.py │ ├── is-subsequence.py │ ├── island-perimeter.py │ ├── isomorphic-strings.py │ ├── iterator-for-combination.py │ ├── jewels-and-stones.py │ ├── jump-game-ii.py │ ├── jump-game-iii.py │ ├── jump-game-iv.py │ ├── jump-game-v.py │ ├── jump-game-vi.py │ ├── jump-game-vii.py │ ├── jump-game.py │ ├── k-closest-points-to-origin.py │ ├── k-concatenation-maximum-sum.py │ ├── k-diff-pairs-in-an-array.py │ ├── k-empty-slots.py │ ├── k-inverse-pairs-array.py │ ├── k-similar-strings.py │ ├── k-th-smallest-in-lexicographical-order.py │ ├── k-th-smallest-prime-fraction.py │ ├── k-th-symbol-in-grammar.py │ ├── keyboard-row.py │ ├── keys-and-rooms.py │ ├── kids-with-the-greatest-number-of-candies.py │ ├── kill-process.py │ ├── knight-dialer.py │ ├── knight-probability-in-chessboard.py │ ├── koko-eating-bananas.py │ ├── kth-ancestor-of-a-tree-node.py │ ├── kth-largest-element-in-a-stream.py │ ├── kth-largest-element-in-an-array.py │ ├── kth-missing-positive-number.py │ ├── kth-smallest-element-in-a-bst.py │ ├── kth-smallest-element-in-a-sorted-matrix.py │ ├── kth-smallest-instructions.py │ ├── kth-smallest-number-in-multiplication-table.py │ ├── largest-1-bordered-square.py │ ├── largest-bst-subtree.py │ ├── largest-color-value-in-a-directed-graph.py │ ├── largest-component-size-by-common-factor.py │ ├── largest-divisible-subset.py │ ├── largest-merge-of-two-strings.py │ ├── largest-multiple-of-three.py │ ├── largest-number-at-least-twice-of-others.py │ ├── largest-number.py │ ├── largest-palindrome-product.py │ ├── largest-perimeter-triangle.py │ ├── largest-plus-sign.py │ ├── largest-rectangle-in-histogram.py │ ├── largest-subarray-length-k.py │ ├── largest-submatrix-with-rearrangements.py │ ├── largest-substring-between-two-equal-characters.py │ ├── largest-sum-of-averages.py │ ├── largest-time-for-given-digits.py │ ├── largest-triangle-area.py │ ├── largest-unique-number.py │ ├── largest-values-from-labels.py │ ├── last-moment-before-all-ants-fall-out-of-a-plank.py │ ├── last-stone-weight-ii.py │ ├── last-stone-weight.py │ ├── last-substring-in-lexicographical-order.py │ ├── latest-time-by-replacing-hidden-digits.py │ ├── leaf-similar-trees.py │ ├── least-number-of-unique-integers-after-k-removals.py │ ├── least-operators-to-express-number.py │ ├── leftmost-column-with-at-least-a-one.py │ ├── lemonade-change.py │ ├── length-of-last-word.py │ ├── length-of-longest-fibonacci-subsequence.py │ ├── letter-case-permutation.py │ ├── letter-combinations-of-a-phone-number.py │ ├── letter-tile-possibilities.py │ ├── lexicographical-numbers.py │ ├── lexicographically-smallest-equivalent-string.py │ ├── lexicographically-smallest-string-after-applying-operations.py │ ├── lfu-cache.py │ ├── license-key-formatting.py │ ├── line-reflection.py │ ├── linked-list-components.py │ ├── linked-list-cycle-ii.py │ ├── linked-list-cycle.py │ ├── linked-list-in-binary-tree.py │ ├── linked-list-random-node.py │ ├── logger-rate-limiter.py │ ├── lonely-pixel-i.py │ ├── lonely-pixel-ii.py │ ├── long-pressed-name.py │ ├── longer-contiguous-segments-of-ones-than-zeros.py │ ├── longest-absolute-file-path.py │ ├── longest-arithmetic-sequence.py │ ├── longest-arithmetic-subsequence-of-given-difference.py │ ├── longest-chunked-palindrome-decomposition.py │ ├── longest-common-prefix.py │ ├── longest-common-subsequence.py │ ├── longest-consecutive-sequence.py │ ├── longest-continuous-increasing-subsequence.py │ ├── longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit.py │ ├── longest-duplicate-substring.py │ ├── longest-happy-prefix.py │ ├── longest-happy-string.py │ ├── longest-harmonious-subsequence.py │ ├── longest-increasing-path-in-a-matrix.py │ ├── longest-increasing-subsequence.py │ ├── longest-line-of-consecutive-one-in-matrix.py │ ├── longest-mountain-in-array.py │ ├── longest-nice-substring.py │ ├── longest-palindrome.py │ ├── longest-palindromic-subsequence-ii.py │ ├── longest-palindromic-subsequence.py │ ├── longest-palindromic-substring.py │ ├── longest-repeating-character-replacement.py │ ├── longest-repeating-substring.py │ ├── longest-string-chain.py │ ├── longest-subarray-of-1s-after-deleting-one-element.py │ ├── longest-substring-of-all-vowels-in-order.py │ ├── longest-substring-with-at-least-k-repeating-characters.py │ ├── longest-substring-with-at-most-k-distinct-characters.py │ ├── longest-substring-with-at-most-two-distinct-characters.py │ ├── longest-substring-without-repeating-characters.py │ ├── longest-turbulent-subarray.py │ ├── longest-uncommon-subsequence-i.py │ ├── longest-uncommon-subsequence-ii.py │ ├── longest-univalue-path.py │ ├── longest-valid-parentheses.py │ ├── longest-well-performing-interval.py │ ├── longest-word-in-dictionary-through-deleting.py │ ├── longest-word-in-dictionary.py │ ├── longest-word-with-all-prefixes.py │ ├── longest-zigzag-path-in-a-binary-tree.py │ ├── loud-and-rich.py │ ├── lowest-common-ancestor-of-a-binary-search-tree.py │ ├── lowest-common-ancestor-of-a-binary-tree-ii.py │ ├── lowest-common-ancestor-of-a-binary-tree-iii.py │ ├── lowest-common-ancestor-of-a-binary-tree-iv.py │ ├── lowest-common-ancestor-of-a-binary-tree.py │ ├── lowest-common-ancestor-of-deepest-leaves.py │ ├── lru-cache.py │ ├── lucky-numbers-in-a-matrix.py │ ├── magic-squares-in-grid.py │ ├── magical-string.py │ ├── magnetic-force-between-two-balls.py │ ├── majority-element-ii.py │ ├── majority-element.py │ ├── make-array-strictly-increasing.py │ ├── make-sum-divisible-by-p.py │ ├── make-the-string-great.py │ ├── make-the-xor-of-all-segments-equal-to-zero.py │ ├── make-two-arrays-equal-by-reversing-sub-arrays.py │ ├── making-a-large-island.py │ ├── making-file-names-unique.py │ ├── map-of-highest-peak.py │ ├── map-sum-pairs.py │ ├── masking-personal-information.py │ ├── matchsticks-to-square.py │ ├── matrix-block-sum.py │ ├── matrix-cells-in-distance-order.py │ ├── matrix-diagonal-sum.py │ ├── max-area-of-island.py │ ├── max-chunks-to-make-sorted-ii.py │ ├── max-chunks-to-make-sorted.py │ ├── max-consecutive-ones-ii.py │ ├── max-consecutive-ones-iii.py │ ├── max-consecutive-ones.py │ ├── max-difference-you-can-get-from-changing-an-integer.py │ ├── max-dot-product-of-two-subsequences.py │ ├── max-increase-to-keep-city-skyline.py │ ├── max-number-of-k-sum-pairs.py │ ├── max-points-on-a-line.py │ ├── max-stack.py │ ├── max-sum-of-sub-matrix-no-larger-than-k.py │ ├── max-value-of-equation.py │ ├── maximal-network-rank.py │ ├── maximal-rectangle.py │ ├── maximal-square.py │ ├── maximize-distance-to-closest-person.py │ ├── maximize-grid-happiness.py │ ├── maximize-number-of-nice-divisors.py │ ├── maximize-palindrome-length-from-subsequences.py │ ├── maximize-score-after-n-operations.py │ ├── maximize-sum-of-array-after-k-negations.py │ ├── maximize-the-beauty-of-the-garden.py │ ├── maximum-69-number.py │ ├── maximum-absolute-sum-of-any-subarray.py │ ├── maximum-area-of-a-piece-of-cake-after-horizontal-and-vertical-cuts.py │ ├── maximum-ascending-subarray-sum.py │ ├── maximum-average-pass-ratio.py │ ├── maximum-average-subarray-i.py │ ├── maximum-average-subarray-ii.py │ ├── maximum-average-subtree.py │ ├── maximum-binary-string-after-change.py │ ├── maximum-binary-tree-ii.py │ ├── maximum-binary-tree.py │ ├── maximum-building-height.py │ ├── maximum-candies-you-can-get-from-boxes.py │ ├── maximum-depth-of-binary-tree.py │ ├── maximum-depth-of-n-ary-tree.py │ ├── maximum-difference-between-node-and-ancestor.py │ ├── maximum-distance-between-a-pair-of-values.py │ ├── maximum-distance-in-arrays.py │ ├── maximum-element-after-decreasing-and-rearranging.py │ ├── maximum-equal-frequency.py │ ├── maximum-erasure-value.py │ ├── maximum-font-to-fit-a-sentence-in-a-screen.py │ ├── maximum-frequency-stack.py │ ├── maximum-gap.py │ ├── maximum-height-by-stacking-cuboids.py │ ├── maximum-ice-cream-bars.py │ ├── maximum-length-of-a-concatenated-string-with-unique-characters.py │ ├── maximum-length-of-pair-chain.py │ ├── maximum-length-of-repeated-subarray.py │ ├── maximum-length-of-subarray-with-positive-product.py │ ├── maximum-level-sum-of-a-binary-tree.py │ ├── maximum-nesting-depth-of-the-parentheses.py │ ├── maximum-nesting-depth-of-two-valid-parentheses-strings.py │ ├── maximum-non-negative-product-in-a-matrix.py │ ├── maximum-number-of-accepted-invitations.py │ ├── maximum-number-of-achievable-transfer-requests.py │ ├── maximum-number-of-balloons.py │ ├── maximum-number-of-balls-in-a-box.py │ ├── maximum-number-of-coins-you-can-get.py │ ├── maximum-number-of-consecutive-values-you-can-make.py │ ├── maximum-number-of-darts-inside-of-a-circular-dartboard.py │ ├── maximum-number-of-eaten-apples.py │ ├── maximum-number-of-events-that-can-be-attended-ii.py │ ├── maximum-number-of-events-that-can-be-attended.py │ ├── maximum-number-of-groups-getting-fresh-donuts.py │ ├── maximum-number-of-non-overlapping-subarrays-with-sum-equals-target.py │ ├── maximum-number-of-non-overlapping-substrings.py │ ├── maximum-number-of-occurrences-of-a-substring.py │ ├── maximum-number-of-ones.py │ ├── maximum-number-of-visible-points.py │ ├── maximum-number-of-vowels-in-a-substring-of-given-length.py │ ├── maximum-of-absolute-value-expression.py │ ├── maximum-performance-of-a-team.py │ ├── maximum-points-you-can-obtain-from-cards.py │ ├── maximum-population-year.py │ ├── maximum-product-of-splitted-binary-tree.py │ ├── maximum-product-of-three-numbers.py │ ├── maximum-product-of-two-elements-in-an-array.py │ ├── maximum-product-of-word-lengths.py │ ├── maximum-product-subarray.py │ ├── maximum-profit-in-job-scheduling.py │ ├── maximum-profit-of-operating-a-centennial-wheel.py │ ├── maximum-repeating-substring.py │ ├── maximum-score-after-splitting-a-string.py │ ├── maximum-score-from-performing-multiplication-operations.py │ ├── maximum-score-from-removing-stones.py │ ├── maximum-score-from-removing-substrings.py │ ├── maximum-score-of-a-good-subarray.py │ ├── maximum-score-words-formed-by-letters.py │ ├── maximum-side-length-of-a-square-with-sum-less-than-or-equal-to-threshold.py │ ├── maximum-size-subarray-sum-equals-k.py │ ├── maximum-students-taking-exam.py │ ├── maximum-subarray-min-product.py │ ├── maximum-subarray-sum-after-one-operation.py │ ├── maximum-subarray-sum-with-one-deletion.py │ ├── maximum-subarray.py │ ├── maximum-sum-bst-in-binary-tree.py │ ├── maximum-sum-circular-subarray.py │ ├── maximum-sum-obtained-of-any-permutation.py │ ├── maximum-sum-of-3-non-overlapping-subarrays.py │ ├── maximum-sum-of-two-non-overlapping-subarrays.py │ ├── maximum-swap.py │ ├── maximum-units-on-a-truck.py │ ├── maximum-vacation-days.py │ ├── maximum-value-at-a-given-index-in-a-bounded-array.py │ ├── maximum-width-of-binary-tree.py │ ├── maximum-width-ramp.py │ ├── maximum-xor-for-each-query.py │ ├── maximum-xor-of-two-numbers-in-an-array.py │ ├── maximum-xor-with-an-element-from-array.py │ ├── mean-of-array-after-removing-some-elements.py │ ├── median-of-two-sorted-arrays.py │ ├── meeting-rooms-ii.py │ ├── meeting-rooms.py │ ├── meeting-scheduler.py │ ├── merge-in-between-linked-lists.py │ ├── merge-intervals.py │ ├── merge-k-sorted-lists.py │ ├── merge-sorted-array.py │ ├── merge-strings-alternately.py │ ├── merge-two-binary-trees.py │ ├── merge-two-sorted-lists.py │ ├── middle-of-the-linked-list.py │ ├── min-cost-climbing-stairs.py │ ├── min-cost-to-connect-all-points.py │ ├── min-stack.py │ ├── minesweeper.py │ ├── mini-parser.py │ ├── minimize-deviation-in-array.py │ ├── minimize-hamming-distance-after-swap-operations.py │ ├── minimize-malware-spread-ii.py │ ├── minimize-malware-spread.py │ ├── minimize-max-distance-to-gas-station.py │ ├── minimize-rounding-error-to-meet-target.py │ ├── minimum-absolute-difference-in-bst.py │ ├── minimum-absolute-difference.py │ ├── minimum-absolute-sum-difference.py │ ├── minimum-add-to-make-parentheses-valid.py │ ├── minimum-adjacent-swaps-for-k-consecutive-ones.py │ ├── minimum-adjacent-swaps-to-reach-the-kth-smallest-number.py │ ├── minimum-area-rectangle-ii.py │ ├── minimum-area-rectangle.py │ ├── minimum-ascii-delete-sum-for-two-strings.py │ ├── minimum-changes-to-make-alternating-binary-string.py │ ├── minimum-cost-for-tickets.py │ ├── minimum-cost-to-connect-sticks.py │ ├── minimum-cost-to-connect-two-groups-of-points.py │ ├── minimum-cost-to-cut-a-stick.py │ ├── minimum-cost-to-hire-k-workers.py │ ├── minimum-cost-to-make-at-least-one-valid-path-in-a-grid.py │ ├── minimum-cost-to-merge-stones.py │ ├── minimum-cost-tree-from-leaf-values.py │ ├── minimum-degree-of-a-connected-trio-in-a-graph.py │ ├── minimum-deletion-cost-to-avoid-repeating-letters.py │ ├── minimum-deletions-to-make-character-frequencies-unique.py │ ├── minimum-deletions-to-make-string-balanced.py │ ├── minimum-depth-of-binary-tree.py │ ├── minimum-difference-between-largest-and-smallest-value-in-three-moves.py │ ├── minimum-difficulty-of-a-job-schedule.py │ ├── minimum-distance-between-bst-nodes.py │ ├── minimum-distance-to-the-target-element.py │ ├── minimum-distance-to-type-a-word-using-two-fingers.py │ ├── minimum-domino-rotations-for-equal-row.py │ ├── minimum-elements-to-add-to-form-a-given-sum.py │ ├── minimum-factorization.py │ ├── minimum-falling-path-sum-ii.py │ ├── minimum-falling-path-sum.py │ ├── minimum-flips-to-make-a-or-b-equal-to-c.py │ ├── minimum-genetic-mutation.py │ ├── minimum-height-trees.py │ ├── minimum-incompatibility.py │ ├── minimum-increment-to-make-array-unique.py │ ├── minimum-index-sum-of-two-lists.py │ ├── minimum-initial-energy-to-finish-tasks.py │ ├── minimum-insertion-steps-to-make-a-string-palindrome.py │ ├── minimum-insertions-to-balance-a-parentheses-string.py │ ├── minimum-interval-to-include-each-query.py │ ├── minimum-jumps-to-reach-home.py │ ├── minimum-knight-moves.py │ ├── minimum-length-of-string-after-deleting-similar-ends.py │ ├── minimum-limit-of-balls-in-a-bag.py │ ├── minimum-moves-to-equal-array-elements-ii.py │ ├── minimum-moves-to-equal-array-elements.py │ ├── minimum-moves-to-make-array-complementary.py │ ├── minimum-moves-to-move-a-box-to-their-target-location.py │ ├── minimum-moves-to-reach-target-with-rotations.py │ ├── minimum-number-of-arrows-to-burst-balloons.py │ ├── minimum-number-of-days-to-disconnect-island.py │ ├── minimum-number-of-days-to-eat-n-oranges.py │ ├── minimum-number-of-days-to-make-m-bouquets.py │ ├── minimum-number-of-flips-to-convert-binary-matrix-to-zero-matrix.py │ ├── minimum-number-of-frogs-croaking.py │ ├── minimum-number-of-increments-on-subarrays-to-form-a-target-array.py │ ├── minimum-number-of-k-consecutive-bit-flips.py │ ├── minimum-number-of-operations-to-make-string-sorted.py │ ├── minimum-number-of-operations-to-move-all-balls-to-each-box.py │ ├── minimum-number-of-operations-to-reinitialize-a-permutation.py │ ├── minimum-number-of-people-to-teach.py │ ├── minimum-number-of-refueling-stops.py │ ├── minimum-number-of-removals-to-make-mountain-array.py │ ├── minimum-number-of-steps-to-make-two-strings-anagram.py │ ├── minimum-number-of-swaps-to-make-the-binary-string-alternating.py │ ├── minimum-number-of-taps-to-open-to-water-a-garden.py │ ├── minimum-number-of-vertices-to-reach-all-nodes.py │ ├── minimum-numbers-of-function-calls-to-make-target-array.py │ ├── minimum-one-bit-operations-to-make-integers-zero.py │ ├── minimum-operations-to-make-a-subsequence.py │ ├── minimum-operations-to-make-array-equal.py │ ├── minimum-operations-to-make-the-array-increasing.py │ ├── minimum-operations-to-reduce-x-to-zero.py │ ├── minimum-path-cost-in-a-hidden-grid.py │ ├── minimum-path-sum.py │ ├── minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits.py │ ├── minimum-remove-to-make-valid-parentheses.py │ ├── minimum-score-triangulation-of-polygon.py │ ├── minimum-sideway-jumps.py │ ├── minimum-size-subarray-sum.py │ ├── minimum-speed-to-arrive-on-time.py │ ├── minimum-subsequence-in-non-increasing-order.py │ ├── minimum-swaps-to-arrange-a-binary-grid.py │ ├── minimum-swaps-to-group-all-1s-together.py │ ├── minimum-swaps-to-make-sequences-increasing.py │ ├── minimum-swaps-to-make-strings-equal.py │ ├── minimum-time-difference.py │ ├── minimum-time-to-build-blocks.py │ ├── minimum-time-to-collect-all-apples-in-a-tree.py │ ├── minimum-time-visiting-all-points.py │ ├── minimum-unique-word-abbreviation.py │ ├── minimum-value-to-get-positive-step-by-step-sum.py │ ├── minimum-window-subsequence.py │ ├── minimum-window-substring.py │ ├── mirror-reflection.py │ ├── missing-element-in-sorted-array.py │ ├── missing-number-in-arithmetic-progression.py │ ├── missing-number.py │ ├── missing-ranges.py │ ├── monotone-increasing-digits.py │ ├── monotonic-array.py │ ├── most-common-word.py │ ├── most-frequent-subtree-sum.py │ ├── most-profit-assigning-work.py │ ├── most-stones-removed-with-same-row-or-column.py │ ├── most-visited-sector-in-a-circular-track.py │ ├── move-sub-tree-of-n-ary-tree.py │ ├── move-zeroes.py │ ├── moving-average-from-data-stream.py │ ├── moving-stones-until-consecutive-ii.py │ ├── moving-stones-until-consecutive.py │ ├── multiply-strings.py │ ├── my-calendar-i.py │ ├── my-calendar-ii.py │ ├── my-calendar-iii.py │ ├── n-ary-tree-level-order-traversal.py │ ├── n-ary-tree-postorder-traversal.py │ ├── n-ary-tree-preorder-traversal.py │ ├── n-queens-ii.py │ ├── n-queens.py │ ├── n-repeated-element-in-size-2n-array.py │ ├── n-th-tribonacci-number.py │ ├── nested-list-weight-sum-ii.py │ ├── nested-list-weight-sum.py │ ├── network-delay-time.py │ ├── next-closest-time.py │ ├── next-greater-element-i.py │ ├── next-greater-element-ii.py │ ├── next-greater-element-iii.py │ ├── next-greater-node-in-linked-list.py │ ├── next-palindrome-using-same-digits.py │ ├── next-permutation.py │ ├── nim-game.py │ ├── non-decreasing-array.py │ ├── non-negative-integers-without-consecutive-ones.py │ ├── non-overlapping-intervals.py │ ├── nth-digit.py │ ├── nth-magical-number.py │ ├── number-complement.py │ ├── number-of-1-bits.py │ ├── number-of-atoms.py │ ├── number-of-boomerangs.py │ ├── number-of-burgers-with-no-waste-of-ingredients.py │ ├── number-of-closed-islands.py │ ├── number-of-connected-components-in-an-undirected-graph.py │ ├── number-of-corner-rectangles.py │ ├── number-of-days-between-two-dates.py │ ├── number-of-days-in-a-month.py │ ├── number-of-dice-rolls-with-target-sum.py │ ├── number-of-different-integers-in-a-string.py │ ├── number-of-different-subsequences-gcds.py │ ├── number-of-digit-one.py │ ├── number-of-distinct-islands-ii.py │ ├── number-of-distinct-islands.py │ ├── number-of-distinct-subarrays-with-at-most-k-odd-integers.py │ ├── number-of-distinct-substrings-in-a-string.py │ ├── number-of-enclaves.py │ ├── number-of-equivalent-domino-pairs.py │ ├── number-of-good-leaf-nodes-pairs.py │ ├── number-of-good-pairs.py │ ├── number-of-good-ways-to-split-a-string.py │ ├── number-of-islands-ii.py │ ├── number-of-islands.py │ ├── number-of-lines-to-write-string.py │ ├── number-of-longest-increasing-subsequence.py │ ├── number-of-matching-subsequences.py │ ├── number-of-music-playlists.py │ ├── number-of-nodes-in-the-sub-tree-with-the-same-label.py │ ├── number-of-operations-to-make-network-connected.py │ ├── number-of-orders-in-the-backlog.py │ ├── number-of-paths-with-max-score.py │ ├── number-of-recent-calls.py │ ├── number-of-rectangles-that-can-form-the-largest-square.py │ ├── number-of-restricted-paths-from-first-to-last-node.py │ ├── number-of-segments-in-a-string.py │ ├── number-of-sets-of-k-non-overlapping-line-segments.py │ ├── number-of-ships-in-a-rectangle.py │ ├── number-of-squareful-arrays.py │ ├── number-of-steps-to-reduce-a-number-in-binary-representation-to-one.py │ ├── number-of-steps-to-reduce-a-number-to-zero.py │ ├── number-of-students-doing-homework-at-a-given-time.py │ ├── number-of-students-unable-to-eat-lunch.py │ ├── number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold.py │ ├── number-of-sub-arrays-with-odd-sum.py │ ├── number-of-subarrays-with-bounded-maximum.py │ ├── number-of-submatrices-that-sum-to-target.py │ ├── number-of-subsequences-that-satisfy-the-given-sum-condition.py │ ├── number-of-substrings-containing-all-three-characters.py │ ├── number-of-substrings-with-only-1s.py │ ├── number-of-valid-subarrays.py │ ├── number-of-valid-words-for-each-puzzle.py │ ├── number-of-ways-of-cutting-a-pizza.py │ ├── number-of-ways-to-form-a-target-string-given-a-dictionary.py │ ├── number-of-ways-to-paint-n-3-grid.py │ ├── number-of-ways-to-rearrange-sticks-with-k-sticks-visible.py │ ├── number-of-ways-to-reconstruct-a-tree.py │ ├── number-of-ways-to-reorder-array-to-get-same-bst.py │ ├── number-of-ways-to-split-a-string.py │ ├── number-of-ways-to-stay-in-the-same-place-after-some-steps.py │ ├── number-of-ways-to-wear-different-hats-to-each-other.py │ ├── number-of-ways-where-square-of-number-is-equal-to-product-of-two-numbers.py │ ├── numbers-at-most-n-given-digit-set.py │ ├── numbers-with-repeated-digits.py │ ├── numbers-with-same-consecutive-differences.py │ ├── occurrences-after-bigram.py │ ├── odd-even-jump.py │ ├── odd-even-linked-list.py │ ├── one-edit-distance.py │ ├── ones-and-zeroes.py │ ├── online-election.py │ ├── online-majority-element-in-subarray.py │ ├── online-stock-span.py │ ├── open-the-lock.py │ ├── optimal-account-balancing.py │ ├── optimal-division.py │ ├── optimize-water-distribution-in-a-village.py │ ├── orderly-queue.py │ ├── out-of-boundary-paths.py │ ├── output-contest-matches.py │ ├── pacific-atlantic-water-flow.py │ ├── paint-fence.py │ ├── paint-house-ii.py │ ├── paint-house-iii.py │ ├── paint-house.py │ ├── pairs-of-songs-with-total-durations-divisible-by-60.py │ ├── palindrome-linked-list.py │ ├── palindrome-number.py │ ├── palindrome-pairs.py │ ├── palindrome-partitioning-ii.py │ ├── palindrome-partitioning-iii.py │ ├── palindrome-partitioning-iv.py │ ├── palindrome-partitioning.py │ ├── palindrome-permutation-ii.py │ ├── palindrome-permutation.py │ ├── palindrome-removal.py │ ├── palindromic-substrings.py │ ├── pancake-sorting.py │ ├── parallel-courses-ii.py │ ├── parallel-courses.py │ ├── parse-lisp-expression.py │ ├── parsing-a-boolean-expression.py │ ├── partition-array-for-maximum-sum.py │ ├── partition-array-into-disjoint-intervals.py │ ├── partition-array-into-three-parts-with-equal-sum.py │ ├── partition-equal-subset-sum.py │ ├── partition-labels.py │ ├── partition-list.py │ ├── partition-to-k-equal-sum-subsets.py │ ├── partitioning-into-minimum-number-of-deci-binary-numbers.py │ ├── pascals-triangle-ii.py │ ├── pascals-triangle.py │ ├── patching-array.py │ ├── path-crossing.py │ ├── path-in-zigzag-labelled-binary-tree.py │ ├── path-sum-ii.py │ ├── path-sum-iii.py │ ├── path-sum-iv.py │ ├── path-sum.py │ ├── path-with-maximum-gold.py │ ├── path-with-maximum-minimum-value.py │ ├── path-with-maximum-probability.py │ ├── path-with-minimum-effort.py │ ├── peak-index-in-a-mountain-array.py │ ├── peeking-iterator.py │ ├── people-whose-list-of-favorite-companies-is-not-a-subset-of-another-list.py │ ├── perfect-number.py │ ├── perfect-rectangle.py │ ├── perfect-squares.py │ ├── perform-string-shifts.py │ ├── permutation-in-string.py │ ├── permutation-sequence.py │ ├── permutations-ii.py │ ├── permutations.py │ ├── pizza-with-3n-slices.py │ ├── play-with-chips.py │ ├── plus-one-linked-list.py │ ├── plus-one.py │ ├── poor-pigs.py │ ├── populating-next-right-pointers-in-each-node-ii.py │ ├── populating-next-right-pointers-in-each-node.py │ ├── positions-of-large-groups.py │ ├── possible-bipartition.py │ ├── pour-water.py │ ├── power-of-four.py │ ├── power-of-three.py │ ├── power-of-two.py │ ├── powerful-integers.py │ ├── powx-n.py │ ├── predict-the-winner.py │ ├── prefix-and-suffix-search.py │ ├── preimage-size-of-factorial-zeroes-function.py │ ├── previous-permutation-with-one-swap.py │ ├── prime-arrangements.py │ ├── prime-number-of-set-bits-in-binary-representation.py │ ├── prime-palindrome.py │ ├── print-binary-tree.py │ ├── print-foobar-alternately.py │ ├── print-immutable-linked-list-in-reverse.py │ ├── print-in-order.py │ ├── print-words-vertically.py │ ├── print-zero-even-odd.py │ ├── prison-cells-after-n-days.py │ ├── probability-of-a-two-boxes-having-the-same-number-of-distinct-balls.py │ ├── product-of-array-except-self.py │ ├── product-of-the-last-k-numbers.py │ ├── product-of-two-run-length-encoded-arrays.py │ ├── profitable-schemes.py │ ├── projection-area-of-3d-shapes.py │ ├── pseudo-palindromic-paths-in-a-binary-tree.py │ ├── push-dominoes.py │ ├── put-boxes-into-the-warehouse-i.py │ ├── put-boxes-into-the-warehouse-ii.py │ ├── pyramid-transition-matrix.py │ ├── quad-tree-intersection.py │ ├── queens-that-can-attack-the-king.py │ ├── queries-on-a-permutation-with-key.py │ ├── queries-on-number-of-points-inside-a-circle.py │ ├── queue-reconstruction-by-height.py │ ├── rabbits-in-forest.py │ ├── race-car.py │ ├── random-flip-matrix.py │ ├── random-pick-index.py │ ├── random-pick-with-blacklist.py │ ├── random-pick-with-weight.py │ ├── random-point-in-non-overlapping-rectangles.py │ ├── range-addition-ii.py │ ├── range-addition.py │ ├── range-module.py │ ├── range-sum-of-bst.py │ ├── range-sum-of-sorted-subarray-sums.py │ ├── range-sum-query-2d-immutable.py │ ├── range-sum-query-2d-mutable.py │ ├── range-sum-query-immutable.py │ ├── range-sum-query-mutable.py │ ├── rank-teams-by-votes.py │ ├── rank-transform-of-a-matrix.py │ ├── rank-transform-of-an-array.py │ ├── ransom-note.py │ ├── reach-a-number.py │ ├── reachable-nodes-in-subdivided-graph.py │ ├── reaching-points.py │ ├── read-n-characters-given-read4-ii-call-multiple-times.py │ ├── read-n-characters-given-read4.py │ ├── rearrange-spaces-between-words.py │ ├── rearrange-string-k-distance-apart.py │ ├── rearrange-words-in-a-sentence.py │ ├── reconstruct-a-2-row-binary-matrix.py │ ├── reconstruct-itinerary.py │ ├── reconstruct-original-digits-from-english.py │ ├── recover-a-tree-from-preorder-traversal.py │ ├── recover-binary-search-tree.py │ ├── rectangle-area-ii.py │ ├── rectangle-area.py │ ├── rectangle-overlap.py │ ├── reduce-array-size-to-the-half.py │ ├── reducing-dishes.py │ ├── redundant-connection-ii.py │ ├── redundant-connection.py │ ├── reformat-date.py │ ├── reformat-phone-number.py │ ├── reformat-the-string.py │ ├── regions-cut-by-slashes.py │ ├── regular-expression-matching.py │ ├── relative-ranks.py │ ├── relative-sort-array.py │ ├── remove-9.py │ ├── remove-all-adjacent-duplicates-in-string-ii.py │ ├── remove-all-adjacent-duplicates-in-string.py │ ├── remove-boxes.py │ ├── remove-comments.py │ ├── remove-covered-intervals.py │ ├── remove-duplicate-letters.py │ ├── remove-duplicates-from-an-unsorted-linked-list.py │ ├── remove-duplicates-from-sorted-array-ii.py │ ├── remove-duplicates-from-sorted-array.py │ ├── remove-duplicates-from-sorted-list-ii.py │ ├── remove-duplicates-from-sorted-list.py │ ├── remove-element.py │ ├── remove-interval.py │ ├── remove-invalid-parentheses.py │ ├── remove-k-digits.py │ ├── remove-linked-list-elements.py │ ├── remove-max-number-of-edges-to-keep-graph-fully-traversable.py │ ├── remove-nth-node-from-end-of-list.py │ ├── remove-outermost-parentheses.py │ ├── remove-palindromic-subsequences.py │ ├── remove-sub-folders-from-the-filesystem.py │ ├── remove-vowels-from-a-string.py │ ├── remove-zero-sum-consecutive-nodes-from-linked-list.py │ ├── reorder-list.py │ ├── reorder-log-files.py │ ├── reorder-routes-to-make-all-paths-lead-to-the-city-zero.py │ ├── reordered-power-of-2.py │ ├── reorganize-string.py │ ├── repeated-dna-sequences.py │ ├── repeated-string-match.py │ ├── repeated-substring-pattern.py │ ├── replace-all-digits-with-characters.py │ ├── replace-all-s-to-avoid-consecutive-repeating-characters.py │ ├── replace-elements-with-greatest-element-on-right-side.py │ ├── replace-the-substring-for-balanced-string.py │ ├── replace-words.py │ ├── reshape-the-matrix.py │ ├── restore-ip-addresses.py │ ├── restore-the-array-from-adjacent-pairs.py │ ├── restore-the-array.py │ ├── reveal-cards-in-increasing-order.py │ ├── reverse-bits.py │ ├── reverse-integer.py │ ├── reverse-linked-list-ii.py │ ├── reverse-linked-list.py │ ├── reverse-nodes-in-k-group.py │ ├── reverse-only-letters.py │ ├── reverse-pairs.py │ ├── reverse-string-ii.py │ ├── reverse-string.py │ ├── reverse-subarray-to-maximize-array-value.py │ ├── reverse-substrings-between-each-pair-of-parentheses.py │ ├── reverse-vowels-of-a-string.py │ ├── reverse-words-in-a-string-ii.py │ ├── reverse-words-in-a-string-iii.py │ ├── reverse-words-in-a-string.py │ ├── richest-customer-wealth.py │ ├── rle-iterator.py │ ├── robot-bounded-in-circle.py │ ├── robot-return-to-origin.py │ ├── robot-room-cleaner.py │ ├── roman-to-integer.py │ ├── rotate-array.py │ ├── rotate-function.py │ ├── rotate-image.py │ ├── rotate-list.py │ ├── rotate-string.py │ ├── rotated-digits.py │ ├── rotating-the-box.py │ ├── rotting-oranges.py │ ├── running-sum-of-1d-array.py │ ├── russian-doll-envelopes.py │ ├── same-tree.py │ ├── satisfiability-of-equality-equations.py │ ├── score-after-flipping-matrix.py │ ├── score-of-parentheses.py │ ├── scramble-string.py │ ├── search-a-2d-matrix-ii.py │ ├── search-a-2d-matrix.py │ ├── search-for-a-range.py │ ├── search-in-a-binary-search-tree.py │ ├── search-in-a-sorted-array-of-unknown-size.py │ ├── search-in-rotated-sorted-array-ii.py │ ├── search-in-rotated-sorted-array.py │ ├── search-insert-position.py │ ├── search-suggestions-system.py │ ├── seat-reservation-manager.py │ ├── second-largest-digit-in-a-string.py │ ├── second-minimum-node-in-a-binary-tree.py │ ├── self-crossing.py │ ├── self-dividing-numbers.py │ ├── sell-diminishing-valued-colored-balls.py │ ├── sentence-screen-fitting.py │ ├── sentence-similarity-ii.py │ ├── sentence-similarity-iii.py │ ├── sentence-similarity.py │ ├── sequence-reconstruction.py │ ├── sequential-digits.py │ ├── serialize-and-deserialize-binary-tree.py │ ├── serialize-and-deserialize-bst.py │ ├── serialize-and-deserialize-n-ary-tree.py │ ├── set-intersection-size-at-least-two.py │ ├── set-matrix-zeroes.py │ ├── set-mismatch.py │ ├── shift-2d-grid.py │ ├── shifting-letters.py │ ├── shopping-offers.py │ ├── short-encoding-of-words.py │ ├── shortest-bridge.py │ ├── shortest-common-supersequence.py │ ├── shortest-completing-word.py │ ├── shortest-distance-from-all-buildings.py │ ├── shortest-distance-to-a-character.py │ ├── shortest-distance-to-target-color.py │ ├── shortest-palindrome.py │ ├── shortest-path-in-a-grid-with-obstacles-elimination.py │ ├── shortest-path-in-a-hidden-grid.py │ ├── shortest-path-in-binary-matrix.py │ ├── shortest-path-to-get-all-keys.py │ ├── shortest-path-to-get-food.py │ ├── shortest-path-visiting-all-nodes.py │ ├── shortest-path-with-alternating-colors.py │ ├── shortest-subarray-to-be-removed-to-make-array-sorted.py │ ├── shortest-subarray-with-sum-at-least-k.py │ ├── shortest-unsorted-continuous-subarray.py │ ├── shortest-way-to-form-string.py │ ├── shortest-word-distance-ii.py │ ├── shortest-word-distance-iii.py │ ├── shortest-word-distance.py │ ├── shuffle-an-array.py │ ├── shuffle-string.py │ ├── shuffle-the-array.py │ ├── sign-of-the-product-of-an-array.py │ ├── similar-rgb-color.py │ ├── similar-string-groups.py │ ├── simplified-fractions.py │ ├── simplify-path.py │ ├── single-element-in-a-sorted-array.py │ ├── single-number-ii.py │ ├── single-number-iii.py │ ├── single-number.py │ ├── single-row-keyboard.py │ ├── single-threaded-cpu.py │ ├── sliding-puzzle.py │ ├── sliding-window-maximum.py │ ├── slowest-key.py │ ├── smallest-common-region.py │ ├── smallest-good-base.py │ ├── smallest-integer-divisible-by-k.py │ ├── smallest-range-i.py │ ├── smallest-range-ii.py │ ├── smallest-range.py │ ├── smallest-rectangle-enclosing-black-pixels.py │ ├── smallest-rotation-with-highest-score.py │ ├── smallest-string-starting-from-leaf.py │ ├── smallest-string-with-a-given-numeric-value.py │ ├── smallest-string-with-swaps.py │ ├── smallest-subsequence-of-distinct-characters.py │ ├── smallest-subtree-with-all-the-deepest-nodes.py │ ├── smallest-sufficient-team.py │ ├── snakes-and-ladders.py │ ├── snapshot-array.py │ ├── solve-the-equation.py │ ├── sort-an-array.py │ ├── sort-array-by-increasing-frequency.py │ ├── sort-array-by-parity-ii.py │ ├── sort-array-by-parity.py │ ├── sort-characters-by-frequency.py │ ├── sort-colors.py │ ├── sort-features-by-popularity.py │ ├── sort-integers-by-the-number-of-1-bits.py │ ├── sort-integers-by-the-power-value.py │ ├── sort-items-by-groups-respecting-dependencies.py │ ├── sort-list.py │ ├── sort-the-matrix-diagonally.py │ ├── sort-transformed-array.py │ ├── sorting-the-sentence.py │ ├── soup-servings.py │ ├── sparse-matrix-multiplication.py │ ├── special-array-with-x-elements-greater-than-or-equal-x.py │ ├── special-binary-string.py │ ├── special-positions-in-a-binary-matrix.py │ ├── spiral-matrix-ii.py │ ├── spiral-matrix-iii.py │ ├── spiral-matrix.py │ ├── split-a-string-in-balanced-strings.py │ ├── split-a-string-into-the-max-number-of-unique-substrings.py │ ├── split-array-into-consecutive-subsequences.py │ ├── split-array-into-fibonacci-sequence.py │ ├── split-array-largest-sum.py │ ├── split-array-with-equal-sum.py │ ├── split-array-with-same-average.py │ ├── split-bst.py │ ├── split-concatenated-strings.py │ ├── split-linked-list-in-parts.py │ ├── split-two-strings-to-make-palindrome.py │ ├── splitting-a-string-into-descending-consecutive-values.py │ ├── sqrtx.py │ ├── squares-of-a-sorted-array.py │ ├── squirrel-simulation.py │ ├── stamping-the-sequence.py │ ├── statistics-from-a-large-sample.py │ ├── stepping-numbers.py │ ├── stickers-to-spell-word.py │ ├── stone-game-ii.py │ ├── stone-game-iii.py │ ├── stone-game-iv.py │ ├── stone-game-v.py │ ├── stone-game-vi.py │ ├── stone-game-vii.py │ ├── stone-game-viii.py │ ├── stone-game.py │ ├── strange-printer-ii.py │ ├── strange-printer.py │ ├── stream-of-characters.py │ ├── stream-of-characters2.py │ ├── string-compression-ii.py │ ├── string-compression.py │ ├── string-matching-in-an-array.py │ ├── string-to-integer-atoi.py │ ├── string-transforms-into-another-string.py │ ├── string-without-aaa-or-bbb.py │ ├── strings-differ-by-one-character.py │ ├── strobogrammatic-number-ii.py │ ├── strobogrammatic-number-iii.py │ ├── strobogrammatic-number.py │ ├── strong-password-checker.py │ ├── student-attendance-record-i.py │ ├── student-attendance-record-ii.py │ ├── subarray-product-less-than-k.py │ ├── subarray-sum-equals-k.py │ ├── subarray-sums-divisible-by-k.py │ ├── subarrays-with-k-different-integers.py │ ├── subdomain-visit-count.py │ ├── subrectangle-queries.py │ ├── subsets-ii.py │ ├── subsets.py │ ├── substring-with-concatenation-of-all-words.py │ ├── subtract-the-product-and-sum-of-digits-of-an-integer.py │ ├── subtree-of-another-tree.py │ ├── sudoku-solver.py │ ├── sum-of-absolute-differences-in-a-sorted-array.py │ ├── sum-of-all-odd-length-subarrays.py │ ├── sum-of-all-subset-xor-totals.py │ ├── sum-of-beauty-of-all-substrings.py │ ├── sum-of-digits-in-base-k.py │ ├── sum-of-digits-in-the-minimum-number.py │ ├── sum-of-distances-in-tree.py │ ├── sum-of-even-numbers-after-queries.py │ ├── sum-of-floored-pairs.py │ ├── sum-of-left-leaves.py │ ├── sum-of-mutated-array-closest-to-target.py │ ├── sum-of-nodes-with-even-valued-grandparent.py │ ├── sum-of-root-to-leaf-binary-numbers.py │ ├── sum-of-special-evenly-spaced-elements-in-array.py │ ├── sum-of-square-numbers.py │ ├── sum-of-subarray-minimums.py │ ├── sum-of-subsequence-widths.py │ ├── sum-of-two-integers.py │ ├── sum-of-unique-elements.py │ ├── sum-root-to-leaf-numbers.py │ ├── summary-ranges.py │ ├── super-egg-drop.py │ ├── super-palindromes.py │ ├── super-pow.py │ ├── super-ugly-number.py │ ├── super-washing-machines.py │ ├── surface-area-of-3d-shapes.py │ ├── surrounded-regions.py │ ├── swap-adjacent-in-lr-string.py │ ├── swap-for-longest-repeated-character-substring.py │ ├── swap-nodes-in-pairs.py │ ├── swapping-nodes-in-a-linked-list.py │ ├── swim-in-rising-water.py │ ├── symmetric-tree.py │ ├── synonymous-sentences.py │ ├── tag-validator.py │ ├── tallest-billboard.py │ ├── target-sum.py │ ├── task-scheduler.py │ ├── teemo-attacking.py │ ├── ternary-expression-parser.py │ ├── text-justification.py │ ├── the-dining-philosophers.py │ ├── the-earliest-moment-when-everyone-become-friends.py │ ├── the-k-strongest-values-in-an-array.py │ ├── the-k-th-lexicographical-string-of-all-happy-strings-of-length-n.py │ ├── the-k-weakest-rows-in-a-matrix.py │ ├── the-kth-factor-of-n.py │ ├── the-maze-ii.py │ ├── the-maze-iii.py │ ├── the-maze.py │ ├── the-most-similar-path-in-a-graph.py │ ├── the-skyline-problem.py │ ├── third-maximum-number.py │ ├── thousand-separator.py │ ├── three-consecutive-odds.py │ ├── three-equal-parts.py │ ├── throne-inheritance.py │ ├── tiling-a-rectangle-with-the-fewest-squares.py │ ├── time-based-key-value-store.py │ ├── time-needed-to-inform-all-employees.py │ ├── to-lower-case.py │ ├── toeplitz-matrix.py │ ├── top-k-frequent-elements.py │ ├── top-k-frequent-words.py │ ├── toss-strange-coins.py │ ├── total-hamming-distance.py │ ├── traffic-light-controlled-intersection.py │ ├── transform-to-chessboard.py │ ├── transpose-matrix.py │ ├── trapping-rain-water-ii.py │ ├── trapping-rain-water.py │ ├── tree-diameter.py │ ├── tree-of-coprimes.py │ ├── triangle.py │ ├── trim-a-binary-search-tree.py │ ├── triples-with-bitwise-and-equal-to-zero.py │ ├── truncate-sentence.py │ ├── tuple-with-same-product.py │ ├── tweet-counts-per-frequency.py │ ├── two-city-scheduling.py │ ├── two-sum-bsts.py │ ├── two-sum-ii-input-array-is-sorted.py │ ├── two-sum-iii-data-structure-design.py │ ├── two-sum-iv-input-is-a-bst.py │ ├── two-sum-less-than-k.py │ ├── two-sum.py │ ├── ugly-number-ii.py │ ├── ugly-number-iii.py │ ├── ugly-number.py │ ├── uncommon-words-from-two-sentences.py │ ├── uncrossed-lines.py │ ├── unique-binary-search-trees-ii.py │ ├── unique-binary-search-trees.py │ ├── unique-email-addresses.py │ ├── unique-morse-code-words.py │ ├── unique-number-of-occurrences.py │ ├── unique-paths-ii.py │ ├── unique-paths-iii.py │ ├── unique-paths.py │ ├── unique-substrings-in-wraparound-string.py │ ├── unique-word-abbreviation.py │ ├── univalued-binary-tree.py │ ├── utf-8-validation.py │ ├── valid-anagram.py │ ├── valid-boomerang.py │ ├── valid-mountain-array.py │ ├── valid-number.py │ ├── valid-palindrome-ii.py │ ├── valid-palindrome-iii.py │ ├── valid-palindrome.py │ ├── valid-parentheses.py │ ├── valid-parenthesis-string.py │ ├── valid-perfect-square.py │ ├── valid-permutations-for-di-sequence.py │ ├── valid-square.py │ ├── valid-sudoku.py │ ├── valid-tic-tac-toe-state.py │ ├── valid-triangle-number.py │ ├── valid-word-abbreviation.py │ ├── valid-word-square.py │ ├── validate-binary-search-tree.py │ ├── validate-binary-tree-nodes.py │ ├── validate-ip-address.py │ ├── validate-stack-sequences.py │ ├── verbal-arithmetic-puzzle.py │ ├── verify-preorder-sequence-in-binary-search-tree.py │ ├── verify-preorder-serialization-of-a-binary-tree.py │ ├── verifying-an-alien-dictionary.py │ ├── vertical-order-traversal-of-a-binary-tree.py │ ├── video-stitching.py │ ├── vowel-spellchecker.py │ ├── walking-robot-simulation.py │ ├── walls-and-gates.py │ ├── water-and-jug-problem.py │ ├── water-bottles.py │ ├── ways-to-make-a-fair-array.py │ ├── ways-to-split-array-into-three-subarrays.py │ ├── web-crawler-multithreaded.py │ ├── web-crawler.py │ ├── where-will-the-ball-fall.py │ ├── widest-vertical-area-between-two-points-containing-no-points.py │ ├── wiggle-sort-ii.py │ ├── wiggle-sort.py │ ├── wiggle-subsequence.py │ ├── wildcard-matching.py │ ├── word-abbreviation.py │ ├── word-break-ii.py │ ├── word-break.py │ ├── word-ladder-ii.py │ ├── word-ladder.py │ ├── word-pattern-ii.py │ ├── word-pattern.py │ ├── word-search-ii.py │ ├── word-search.py │ ├── word-squares.py │ ├── word-subsets.py │ ├── x-of-a-kind-in-a-deck-of-cards.py │ ├── xor-operation-in-an-array.py │ ├── xor-queries-of-a-subarray.py │ ├── zigzag-conversion.py │ ├── zigzag-iterator.py │ └── zuma-game.py ├── Target Sum.java ├── blockedMaze.cpp ├── contents.txt ├── find-peak-element.cpp ├── matrix rotation.cpp └── min_stack.cpp ├── LICENSE ├── Lang_Detection_Excel.ipynb ├── MergeSort.cpp ├── QuickSort.cpp ├── README.md ├── Rabin-Karp-pattern.cpp ├── React_Cookbook_Recipes_for_Mastering_the_React_Framework_by_David.pdf ├── Untitled-1.cpp ├── circularqueues.cpp ├── datatypes.cpp ├── example.cpp ├── josephus2.cpp ├── numberconversion.cpp ├── prac.cpp ├── program.cpp ├── tut24.cpp ├── tut27.cpp └── tut9.exe /.github/ISSUE_TEMPLATE/script-addition.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Script Addition 3 | about: Describe this Script you want to add 4 | title: '' 5 | labels: '' 6 | assignees: '' 7 | 8 | --- 9 | 10 | # Aim 11 | 12 | - What is the objective of the Script 13 | 14 | # Details 15 | 16 | - What the features will your script have 17 | 18 | ### Programming language 19 | 22 | - [ ] C 23 | - [ ] C++ 24 | - [ ] Java 25 | - [ ] Python 26 | -------------------------------------------------------------------------------- /.github/auto_assign.yml: -------------------------------------------------------------------------------- 1 | # Set to true to add reviewers to pull requests 2 | addReviewers: true 3 | # Set to true to add assignees to pull requests 4 | addAssignees: false 5 | # A list of reviewers to be added to pull requests (GitHub user name) 6 | reviewers: 7 | - Shubhankar Poddar 8 | - Rishika Ghosh 9 | - Rounak Neogy 10 | - Siddhi Bhanusali 11 | - Mr Twinkle Sharma 12 | - Arpit Jain 13 | - Jyoti Kumari 14 | -------------------------------------------------------------------------------- /.vscode/c_cpp_properties.json: -------------------------------------------------------------------------------- 1 | { 2 | "configurations": [ 3 | { 4 | "name": "windows-gcc-x86", 5 | "includePath": [ 6 | "${workspaceFolder}/**" 7 | ], 8 | "compilerPath": "C:/MinGW/bin/gcc.exe", 9 | "cStandard": "${default}", 10 | "cppStandard": "${default}", 11 | "intelliSenseMode": "windows-gcc-x86", 12 | "compilerArgs": [ 13 | "" 14 | ] 15 | } 16 | ], 17 | "version": 4 18 | } -------------------------------------------------------------------------------- /ALGORITHMS/01.Searching Algorithms/C++/context.txt: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /ALGORITHMS/01.Searching Algorithms/C/contents.txt: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /ALGORITHMS/01.Searching Algorithms/Java/context.txt: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /ALGORITHMS/01.Searching Algorithms/Python/context.txt: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /ALGORITHMS/02.Sorting Algorithms/C++/context.txt: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /ALGORITHMS/02.Sorting Algorithms/C++/countingSort.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codeaholic-shub/ALGO-ADDICT/013983e3d07c61c5e90f9f64f82db97e98a1d4c4/ALGORITHMS/02.Sorting Algorithms/C++/countingSort.exe -------------------------------------------------------------------------------- /ALGORITHMS/02.Sorting Algorithms/C/context.txt: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /ALGORITHMS/02.Sorting Algorithms/Java/context.txt: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /ALGORITHMS/02.Sorting Algorithms/Python/context.txt: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /ALGORITHMS/03.Divide and Conquer/C++/context.txt: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /ALGORITHMS/03.Divide and Conquer/C/context.txt: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /ALGORITHMS/03.Divide and Conquer/Java/context.txt: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /ALGORITHMS/03.Divide and Conquer/Python/context.txt: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /ALGORITHMS/04.Bitwise Algorithms/C++/calc_set_bit.cpp: -------------------------------------------------------------------------------- 1 | //This is another method to calculate sit bit in any binary number 2 | 3 | #include 4 | using namespace std; 5 | int main() 6 | { 7 | int num,count=0,original; 8 | cout<<"Enter the decimal number: "; 9 | cin>>num; 10 | original=num; 11 | while(num>0) 12 | { 13 | if(num&1 == 1) 14 | count++; 15 | num=num>>1; 16 | } 17 | cout<<"Number of set bit in "< 2 | using namespace std; 3 | int main() 4 | { 5 | int num; 6 | cout<<"Enter the number: "; 7 | cin>>num; 8 | if(num & 1==0) // last bit of every even number is 0 and last bit of every odd number is 1 9 | cout<<"Number is Even"; 10 | else 11 | cout<<"Number is Odd"; 12 | } -------------------------------------------------------------------------------- /ALGORITHMS/04.Bitwise Algorithms/C++/find_unique.cpp: -------------------------------------------------------------------------------- 1 | //This program is to calculate unique number from list of array using XOR Operator 2 | #include 3 | using namespace std; 4 | int main() 5 | { 6 | int num[5]; 7 | cout<<"Enter 5 number: "; 8 | for(int i=0;i<5;i++) 9 | cin>>num[i]; 10 | int a=num[0]; 11 | for(int i=1;i<5;i++) 12 | { 13 | a=a^num[i]; 14 | } 15 | cout<<"Unique no. in this set is: "< 2 | using namespace std; 3 | 4 | int main() { 5 | int n, k; 6 | cin>>n>>k; 7 | int *arr = new int[n]; 8 | for(int i=0; i>arr[i]; 10 | int limit=arr[k-1], ans=0; 11 | 12 | for(int i=0; i=limit && arr[i]!=0) 14 | ans++; 15 | else 16 | break; 17 | } 18 | 19 | cout< 2 | #include 3 | #include 4 | #include 5 | #define ll long long 6 | #include 7 | #include 8 | 9 | using namespace std; 10 | 11 | int main() 12 | { 13 | int t; 14 | cin>>t; 15 | 16 | while (t--) 17 | { 18 | ll n; 19 | cin>>n; 20 | 21 | cout<<(n/3) -2<<"\n"; 22 | } 23 | 24 | } -------------------------------------------------------------------------------- /CODEFORCES/CodeforcesCodes/281A - Word Capitalization.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | string str; 7 | cin>>str; 8 | if(str[0]>='a' && str[0]<='z') 9 | str[0] -= 32; 10 | cout< 2 | #include 3 | using namespace std; 4 | 5 | int main(){ 6 | int n; 7 | cin>>n; 8 | int x = 0; 9 | 10 | while(n--) { 11 | string str; 12 | cin>>str; 13 | if(str[1]=='+') 14 | x += 1; 15 | else 16 | x -= 1; 17 | } 18 | 19 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int w; 6 | cin>>w; 7 | 8 | if(w%2 || w==2) 9 | cout<<"NO"; 10 | else 11 | cout<<"YES"; 12 | 13 | return 0; 14 | } -------------------------------------------------------------------------------- /CODEFORCES/CodeforcesCodes/50A - Domino piling.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int m, n; 6 | cin>>m>>n; 7 | cout<<(m*n)/2; 8 | return 0; 9 | } -------------------------------------------------------------------------------- /CODEFORCES/Codeforces_#824/ProblemA.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define ll long long 4 | 5 | void solve() 6 | { 7 | 8 | ll n; 9 | cin >> n; 10 | cout << (n - 3) / 3 - 1 << endl; 11 | return; 12 | } 13 | 14 | int main() 15 | { 16 | ll t; 17 | cin >> t; 18 | while (t--) 19 | { 20 | solve(); 21 | } 22 | 23 | return 0; 24 | } -------------------------------------------------------------------------------- /CODEFORCES/contents.txt: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /CODEFORCES/imp_TheatreSquare.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | typedef long long ll; 5 | int main() 6 | { 7 | ll m, n, a; 8 | cin >> m >> n >> a; 9 | cout << fixed; 10 | cout << setprecision(0) << ceil(m * 1.0 / a) * ceil(n * 1.0 / a); 11 | return 0; 12 | } -------------------------------------------------------------------------------- /DATA STRUCTURES/01.Arrays/C++/Cyclically rotate by 1.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | link: https://practice.geeksforgeeks.org/problems/cyclically-rotate-an-array-by-one2614/1 3 | */ 4 | 5 | 6 | // ----------------------------------------------------------------------------------------------------------------------- // 7 | void rotate(int arr[], int n) 8 | { 9 | int temp = arr[n - 1]; 10 | 11 | for (int i = n - 1;i > 0;i--) { 12 | arr[i] = arr[i - 1]; 13 | } 14 | arr[0] = temp; 15 | } -------------------------------------------------------------------------------- /DATA STRUCTURES/01.Arrays/C++/Kth max min.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | link: https://practice.geeksforgeeks.org/problems/kth-smallest-element5635/1 3 | 4 | sol: https://www.geeksforgeeks.org/kth-smallestlargest-element-unsorted-array/ 5 | or 6 | refer heap/5th_kth....cpp 7 | 8 | */ 9 | 10 | 11 | -------------------------------------------------------------------------------- /DATA STRUCTURES/01.Arrays/C++/contents.txt: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /DATA STRUCTURES/01.Arrays/C/contents.txt: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /DATA STRUCTURES/01.Arrays/Java/contents.txt: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /DATA STRUCTURES/01.Arrays/Python/contents.txt: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /DATA STRUCTURES/02.2D Arrays/C++/contents.txt: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /DATA STRUCTURES/02.2D Arrays/C/contents.txt: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /DATA STRUCTURES/02.2D Arrays/Java/contents.txt: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /DATA STRUCTURES/02.2D Arrays/Python/contents.txt: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /DATA STRUCTURES/03.Linked List/C++/contents.txt: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /DATA STRUCTURES/03.Linked List/C/contents.txt: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /DATA STRUCTURES/03.Linked List/Java/contents.txt: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /DATA STRUCTURES/03.Linked List/Python/contents.txt: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /DATA STRUCTURES/04.Stack/C++/contents.txt: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /DATA STRUCTURES/04.Stack/C/contents.txt: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /DATA STRUCTURES/04.Stack/Java/contents.txt: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /DATA STRUCTURES/04.Stack/Python/contents.txt: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /DATA STRUCTURES/05.Queue/C++/contents.txt: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /DATA STRUCTURES/05.Queue/C/contents.txt: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /DATA STRUCTURES/05.Queue/Java/contents.txt: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /DATA STRUCTURES/05.Queue/Python/contents.txt: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /DATA STRUCTURES/06.Binary Trees/C++/contents.txt: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /DATA STRUCTURES/06.Binary Trees/C/contents.txt: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /DATA STRUCTURES/06.Binary Trees/Java/contents.txt: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /DATA STRUCTURES/06.Binary Trees/Python/contents.txt: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /DATA STRUCTURES/07.Binary Search Trees/C++/contents.txt: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /DATA STRUCTURES/07.Binary Search Trees/C/contents.txt: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /DATA STRUCTURES/07.Binary Search Trees/Java/contents.txt: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /DATA STRUCTURES/07.Binary Search Trees/Python/contents.txt: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /DATA STRUCTURES/08.Heap/C++/contents.txt: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /DATA STRUCTURES/08.Heap/C/contents.txt: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /DATA STRUCTURES/08.Heap/Java/contents.txt: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /DATA STRUCTURES/08.Heap/Python/contents.txt: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /DATA STRUCTURES/09.Hashing/C++/contents.txt: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /DATA STRUCTURES/09.Hashing/C/contents.txt: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /DATA STRUCTURES/09.Hashing/Java/contents.txt: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /DATA STRUCTURES/09.Hashing/Python/contents.txt: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /DATA STRUCTURES/10.Graphs/C++/contents.txt: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /DATA STRUCTURES/10.Graphs/C/contents.txt: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /DATA STRUCTURES/10.Graphs/Java/contents.txt: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /DATA STRUCTURES/10.Graphs/Python/contents.txt: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /DATA STRUCTURES/contents.txt: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /GFG/contents.txt: -------------------------------------------------------------------------------- 1 | Like QuickSort, Merge Sort is a Divide and Conquer algorithm. It divides the input array into two halves, calls itself for the two halves, and then merges the two sorted halves. The merge() function is used for merging two halves. The merge(arr, l, m, r) is a key process that assumes that arr[l..m] and arr[m+1..r] are sorted and merges the two sorted sub-arrays into one. 2 | -------------------------------------------------------------------------------- /Graph Algorithms/BFS for a Graph.txt: -------------------------------------------------------------------------------- 1 | Breadth First Traversal (or Search) for a graph is similar to Breadth First Traversal of a tree (See method 2 of this post). 2 | The only catch here is, unlike trees, graphs may contain cycles, so we may come to the same node again. 3 | To avoid processing a node more than once, we use a boolean visited array. For simplicity, it is assumed that all vertices are reachable from the starting vertex. 4 | -------------------------------------------------------------------------------- /HACKEREARTH/contents.txt: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /HACKERRANK/contents.txt: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /LEETCODE/198-house-robber/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /LEETCODE/213-house-robber-ii/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /LEETCODE/C++/1-bit-and-2-bit-characters.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(n) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | bool isOneBitCharacter(vector& bits) { 7 | auto parity = 0; 8 | for (int i = static_cast(bits.size()) - 2; 9 | i >= 0 && bits[i]; --i) { 10 | parity ^= bits[i]; 11 | } 12 | return parity == 0; 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /LEETCODE/C++/2-keys-keyboard.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(sqrt(n)) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | int minSteps(int n) { 7 | auto result = 0; 8 | // the answer is the sum of prime factors 9 | for (auto p = 2 ; p * p <= n; ++p) { 10 | while (n % p == 0) { 11 | result += p; 12 | n /= p; 13 | } 14 | } 15 | result += (n > 1) ? n : 0; 16 | return result; 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /LEETCODE/C++/add-digits.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(1) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | int addDigits(int num) { 7 | return (num - 1) % 9 + 1; 8 | } 9 | }; 10 | -------------------------------------------------------------------------------- /LEETCODE/C++/angle-between-hands-of-a-clock.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(1) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | double angleClock(int hour, int minutes) { 7 | double angle1 = (hour % 12 * 60 + minutes) / 720.0; 8 | double angle2 = minutes / 60.0; 9 | double diff = abs(angle1 - angle2); 10 | return min(diff, 1.0 - diff) * 360.0; 11 | } 12 | }; 13 | -------------------------------------------------------------------------------- /LEETCODE/C++/arithmetic-slices.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(n) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | int numberOfArithmeticSlices(vector& A) { 7 | int res = 0, i = 0; 8 | for (int i = 0; i + 2 < A.size(); ++i) { 9 | const auto start = i; 10 | while (i + 2 < A.size() && A[i + 2] + A[i] == 2 * A[i + 1]) { 11 | res += (i++) - start + 1; 12 | } 13 | } 14 | return res; 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /LEETCODE/C++/armstrong-number.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(klogk) 2 | // Space: O(k) 3 | 4 | class Solution { 5 | public: 6 | bool isArmstrong(int N) { 7 | const auto& n_str = to_string(N); 8 | return accumulate(n_str.cbegin(), n_str.cend(), 0, 9 | [&](const auto& x, const auto& y) { 10 | return x + pow(y - '0', n_str.length()); 11 | }) == N; 12 | } 13 | }; 14 | -------------------------------------------------------------------------------- /LEETCODE/C++/average-waiting-time.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(n) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | double averageWaitingTime(vector>& customers) { 7 | double avai = 0, wait = 0; 8 | for (const auto& c : customers) { 9 | avai = max(avai, double(c[0])) + c[1]; 10 | wait += avai - c[0]; 11 | } 12 | return wait / size(customers); 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /LEETCODE/C++/beautiful-arrangement-ii.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(n) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | vector constructArray(int n, int k) { 7 | vector result; 8 | int left = 1, right = n; 9 | while (left <= right) { 10 | result.emplace_back(k % 2 ? left++ : right--); 11 | if (k > 1) { 12 | --k; 13 | } 14 | } 15 | return result; 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /LEETCODE/C++/best-sightseeing-pair.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(n) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | int maxScoreSightseeingPair(vector& A) { 7 | int result = 0, curr = 0; 8 | for (const auto& x : A) { 9 | result = max(result, curr + x); 10 | curr = max(curr, x) - 1; 11 | } 12 | return result; 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /LEETCODE/C++/best-time-to-buy-and-sell-stock-ii.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(n) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | int maxProfit(vector& prices) { 7 | int result = 0; 8 | for (int i = 1; i < prices.size(); ++i) { 9 | result += max(0, prices[i] - prices[i - 1]); 10 | } 11 | return result; 12 | } 13 | }; 14 | -------------------------------------------------------------------------------- /LEETCODE/C++/best-time-to-buy-and-sell-stock-with-transaction-fee.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(n) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | int maxProfit(vector& prices, int fee) { 7 | int cash = 0, hold = -prices[0]; 8 | for (int i = 1; i < prices.size(); ++i) { 9 | cash = max(cash, hold + prices[i] - fee); 10 | hold = max(hold, cash - prices[i]); 11 | } 12 | return cash; 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /LEETCODE/C++/binary-number-with-alternating-bits.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(1) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | bool hasAlternatingBits(int n) { 7 | auto curr = n % 2; 8 | n /= 2; 9 | while (n > 0) { 10 | if (curr == n % 2) { 11 | return false; 12 | } 13 | curr = n % 2; 14 | n /= 2; 15 | } 16 | return true; 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /LEETCODE/C++/binary-prefix-divisible-by-5.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(n) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | vector prefixesDivBy5(vector& A) { 7 | for (int i = 1; i < A.size(); ++i) { 8 | A[i] += A[i - 1] * 2 % 5; 9 | } 10 | vector result(A.size()); 11 | transform(A.cbegin(), A.cend(), result.begin(), 12 | [](int x) { return x % 5 == 0; }); 13 | return result; 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /LEETCODE/C++/bitwise-and-of-numbers-range.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(1) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | int rangeBitwiseAnd(int m, int n) { 7 | while (m < n) { // Remove the last bit 1 until n <= m. 8 | n &= n - 1; 9 | } 10 | return n; 11 | } 12 | }; 13 | -------------------------------------------------------------------------------- /LEETCODE/C++/broken-calculator.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(logn) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | int brokenCalc(int X, int Y) { 7 | int result = 0; 8 | while (X < Y) { 9 | if (Y % 2) { 10 | ++Y; 11 | } else { 12 | Y /= 2; 13 | } 14 | ++result; 15 | } 16 | return result + X - Y; 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /LEETCODE/C++/bulb-switcher-ii.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(1) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | int flipLights(int n, int m) { 7 | if (m == 0) return 1; 8 | if (n == 1) return 2; 9 | if (m == 1 && n == 2) return 3; 10 | if (m == 1 || n == 2) return 4; 11 | if (m == 2) return 7; 12 | return 8; 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /LEETCODE/C++/bulb-switcher-iii.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(n) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | int numTimesAllBlue(vector& light) { 7 | int result = 0, right = 0; 8 | for (int i = 0; i < light.size(); ++i) { 9 | right = max(right, light[i]); 10 | result += (i + 1 == right); 11 | } 12 | return result; 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /LEETCODE/C++/bulb-switcher-iv.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(n) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | int minFlips(string target) { 7 | int result = 0; 8 | char curr = '0'; 9 | for (const auto& c : target) { 10 | if (c == curr) { 11 | continue; 12 | } 13 | curr = c; 14 | ++result; 15 | } 16 | return result; 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /LEETCODE/C++/bulb-switcher.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(1) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | int bulbSwitch(int n) { 7 | // The number of full squares. 8 | return static_cast(sqrt(n)); 9 | } 10 | }; 11 | -------------------------------------------------------------------------------- /LEETCODE/C++/chalkboard-xor-gam.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(n) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | bool xorGame(vector& nums) { 7 | return accumulate(nums.cbegin(), nums.cend(), 8 | 0, std::bit_xor()) == 0 || 9 | nums.size() % 2 == 0; 10 | } 11 | }; 12 | -------------------------------------------------------------------------------- /LEETCODE/C++/check-if-array-is-sorted-and-rotated.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(n) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | bool check(vector& nums) { 7 | for (int i = 0, count = 0; i < size(nums); ++i) { 8 | if (nums[i] > nums[(i + 1) % size(nums)] && ++count > 1) { 9 | return false; 10 | } 11 | } 12 | return true; 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /LEETCODE/C++/check-if-binary-string-has-at-most-one-segment-of-ones.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(n) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | bool checkOnesSegment(string s) { 7 | return s.find("01") == string::npos; 8 | } 9 | }; 10 | -------------------------------------------------------------------------------- /LEETCODE/C++/check-if-it-is-a-good-array.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(n) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | bool isGoodArray(vector& nums) { 7 | // Bézout's identity 8 | int result = nums[0]; 9 | for (const auto& num : nums) { 10 | result = std::gcd(result, num); // built-in gcd since C++17, O(logn) 11 | if (result == 1) { 12 | break; 13 | } 14 | } 15 | return result == 1; 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /LEETCODE/C++/check-if-n-and-its-double-exist.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(n) 2 | // Space: O(n) 3 | 4 | class Solution { 5 | public: 6 | bool checkIfExist(vector& arr) { 7 | unordered_set lookup; 8 | for (const auto& x : arr) { 9 | if (lookup.count(2 * x) || 10 | (x % 2 == 0 && lookup.count(x / 2))) { 11 | return true; 12 | } 13 | lookup.emplace(x); 14 | } 15 | return false; 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /LEETCODE/C++/check-if-number-is-a-sum-of-powers-of-three.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(logn) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | bool checkPowersOfThree(int n) { 7 | for (; n > 0 && n % 3 != 2; n /= 3); 8 | return n == 0; 9 | } 10 | }; 11 | -------------------------------------------------------------------------------- /LEETCODE/C++/check-if-the-sentence-is-pangram.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(n) 2 | // Space: O(26) = O(1) 3 | 4 | class Solution { 5 | public: 6 | bool checkIfPangram(string sentence) { 7 | return size(unordered_set(cbegin(sentence), cend(sentence))) == 26; 8 | } 9 | }; 10 | -------------------------------------------------------------------------------- /LEETCODE/C++/circular-permutation-in-binary-representation.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(2^n) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | vector circularPermutation(int n, int start) { 7 | vector result; 8 | for (int i = 0; i < 1 << n; ++i) { 9 | result.emplace_back(start ^ (i >> 1) ^ i); 10 | } 11 | return result; 12 | } 13 | }; 14 | -------------------------------------------------------------------------------- /LEETCODE/C++/coin-change-2.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(n * m) 2 | // Space: O(m) 3 | 4 | class Solution { 5 | public: 6 | int change(int amount, vector& coins) { 7 | vector dp(amount + 1); 8 | dp[0] = 1; 9 | for (const auto& coin : coins) { 10 | for (int i = coin; i <= amount; ++i) { 11 | dp[i] += dp[i - coin]; 12 | } 13 | } 14 | return dp[amount]; 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /LEETCODE/C++/complement-of-base-10-integer.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(logn) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | int bitwiseComplement(int N) { 7 | int mask = 1; 8 | while (N > mask) { 9 | mask = mask * 2 + 1; 10 | } 11 | return mask - N; 12 | } 13 | }; 14 | -------------------------------------------------------------------------------- /LEETCODE/C++/concatenation-of-consecutive-binary-numbers.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(n) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | int concatenatedBinary(int n) { 7 | static int MOD = 1e9 + 7; 8 | uint64_t result = 0, len = 0; 9 | for (int i = 1; i <= n; ++i) { 10 | if ((i & (i - 1)) == 0) { 11 | ++len; 12 | } 13 | result = ((result << len) % MOD + i) % MOD; 14 | } 15 | return result; 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /LEETCODE/C++/consecutive-characters.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(n) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | int maxPower(string s) { 7 | int result = 1, count = 1; 8 | for (int i = 1; i < s.length(); ++i) { 9 | if (s[i] == s[i - 1]) { 10 | ++count; 11 | } else { 12 | count = 1; 13 | } 14 | result = max(result, count); 15 | } 16 | return result; 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /LEETCODE/C++/construct-the-rectangle.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(1) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | vector constructRectangle(int area) { 7 | int w = sqrt(area); 8 | while (area % w) { 9 | --w; 10 | } 11 | return {area / w, w}; 12 | } 13 | }; 14 | -------------------------------------------------------------------------------- /LEETCODE/C++/count-all-valid-pickup-and-delivery-options.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(n) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | int countOrders(int n) { 7 | static const int MOD = 1e9 + 7; 8 | int64_t result = 1ll; 9 | for (int i = 2 * n; i > 0; i -= 2) { 10 | result = result * i * (i - 1) / 2 % MOD; 11 | } 12 | return result; 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /LEETCODE/C++/count-negative-numbers-in-a-sorted-matrix.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(m + n) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | int countNegatives(vector>& grid) { 7 | int result = 0, c = grid[0].size() - 1; 8 | for (const auto& row : grid) { 9 | while (c >= 0 && row[c] < 0) { 10 | --c; 11 | } 12 | result += grid[0].size() - 1 - c; 13 | } 14 | return result; 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /LEETCODE/C++/count-odd-numbers-in-an-interval-range.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(n) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | int countOdds(int low, int high) { 7 | return (high + 1) / 2 - ((low - 1) + 1) / 2; 8 | } 9 | }; 10 | -------------------------------------------------------------------------------- /LEETCODE/C++/count-of-matches-in-tournament.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(1) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | int numberOfMatches(int n) { 7 | return n - 1; 8 | } 9 | }; 10 | -------------------------------------------------------------------------------- /LEETCODE/C++/count-ways-to-distribute-candies.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(n * k) 2 | // Space: O(k) 3 | 4 | class Solution { 5 | public: 6 | int waysToDistribute(int n, int k) { 7 | static int MOD = 1e9 + 7; 8 | vector dp(k, 1); 9 | for (int i = 1; i < n; ++i) { 10 | for (int j = min(i, k) - 1; j >= 1; --j) { 11 | dp[j] = ((j + 1) * dp[j] + dp[j - 1]) % MOD; 12 | } 13 | } 14 | return dp[k - 1]; 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /LEETCODE/C++/decode-xored-array.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(n) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | vector decode(vector& encoded, int first) { 7 | vector result = {first}; 8 | for (const auto& x: encoded) { 9 | result.emplace_back(result.back() ^ x); 10 | } 11 | return result; 12 | } 13 | }; 14 | -------------------------------------------------------------------------------- /LEETCODE/C++/decompress-run-length-encoded-list.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(n) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | vector decompressRLElist(vector& nums) { 7 | vector result; 8 | for (int i = 0; i < nums.size(); i += 2) { 9 | for (int j = 0; j < nums[i]; ++j) { 10 | result.emplace_back(nums[i + 1]); 11 | } 12 | } 13 | return result; 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /LEETCODE/C++/defanging-an-ip-address.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(n) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | string defangIPaddr(string address) { 7 | string result; 8 | for (const auto& c : address) { 9 | if (c == '.') { 10 | result += "[.]"; 11 | } else { 12 | result.push_back(c); 13 | } 14 | } 15 | return result; 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /LEETCODE/C++/design-parking-system.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(1) 2 | // Space: O(1) 3 | 4 | class ParkingSystem { 5 | public: 6 | ParkingSystem(int big, int medium, int small) 7 | : space_{0, big, medium, small} { 8 | } 9 | 10 | bool addCar(int carType) { 11 | if (space_[carType] > 0) { 12 | --space_[carType]; 13 | return true; 14 | } 15 | return false; 16 | } 17 | 18 | private: 19 | vector space_; 20 | }; 21 | -------------------------------------------------------------------------------- /LEETCODE/C++/detect-capital.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(l) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | bool detectCapitalUse(string word) { 7 | int count = count_if(word.begin(), word.end(), [](char c){ return isupper(c); }); 8 | return count == word.length() || count == 0 || (count == 1 && isupper(word[0])); 9 | } 10 | }; 11 | -------------------------------------------------------------------------------- /LEETCODE/C++/determine-color-of-a-chessboard-square.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(1) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | bool squareIsWhite(string coordinates) { 7 | return (coordinates[0] - 'a') % 2 != (coordinates[1] - '1') % 2; 8 | } 9 | }; 10 | -------------------------------------------------------------------------------- /LEETCODE/C++/distribute-candies.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(n) 2 | // Space: O(n) 3 | 4 | class Solution { 5 | public: 6 | int distributeCandies(vector& candies) { 7 | unordered_set lookup; 8 | for (const auto& candy: candies) { 9 | lookup.emplace(candy); 10 | } 11 | return min(lookup.size(), candies.size() / 2); 12 | } 13 | }; 14 | -------------------------------------------------------------------------------- /LEETCODE/C++/divide-array-into-increasing-sequences.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(n) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | bool canDivideIntoSubsequences(vector& nums, int K) { 7 | int curr = 1, max_count = 1; 8 | for (int i = 1; i < nums.size(); ++i) { 9 | curr = (nums[i - 1] < nums[i]) ? 1 : curr + 1; 10 | max_count = max(max_count, curr); 11 | } 12 | return K * max_count <= nums.size(); 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /LEETCODE/C++/elimination-game.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(logn) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | int lastRemaining(int n) { 7 | int start = 1; 8 | 9 | for (int step = 2, direction = 1; n > 1; 10 | n /= 2, step *= 2, direction *= -1) { 11 | 12 | start += direction * (step * (n / 2) - step / 2); 13 | } 14 | 15 | return start; 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /LEETCODE/C++/encode-number.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(logn) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | string encode(int num) { 7 | string result; 8 | while (num) { 9 | result.push_back((num % 2) ? '0' : '1'); 10 | num = (num - 1) / 2; 11 | } 12 | reverse(result.begin(), result.end()); 13 | return result; 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /LEETCODE/C++/factorial-trailing-zeroes.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(logn) = O(1) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | int trailingZeroes(int n) { 7 | int number = 0; 8 | while (n > 0) { 9 | number += n / 5; 10 | n /= 5; 11 | } 12 | return number; 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /LEETCODE/C++/find-center-of-star-graph.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(1) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | int findCenter(vector>& edges) { 7 | return edges[0][0] == edges[1][0] || edges[0][0] == edges[1][1] ? edges[0][0] : edges[0][1]; 8 | } 9 | }; 10 | -------------------------------------------------------------------------------- /LEETCODE/C++/find-n-unique-integers-sum-up-to-zero.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(n) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | vector sumZero(int n) { 7 | vector result; 8 | for (int i = -(n / 2); i <= (n / 2); ++i) { 9 | if (i == 0 && n % 2 == 0) { 10 | continue; 11 | } 12 | result.emplace_back(i); 13 | } 14 | return result; 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /LEETCODE/C++/find-smallest-letter-greater-than-target.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(logn) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | char nextGreatestLetter(vector& letters, char target) { 7 | const auto cit = upper_bound(letters.cbegin(), letters.cend(), target); 8 | return cit != letters.cend() ? *cit : letters.front(); 9 | } 10 | }; 11 | -------------------------------------------------------------------------------- /LEETCODE/C++/find-the-derangement-of-an-array.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(n) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | int findDerangement(int n) { 7 | static const int M = 1000000007; 8 | long long mul = 1, sum = 0; 9 | for (int i = n; i >= 0; --i) { 10 | sum = (sum + M + (i % 2 == 0 ? 1 : -1) * mul) % M; 11 | mul = (mul * i) % M; 12 | } 13 | return static_cast(sum); 14 | } 15 | }; 16 | 17 | -------------------------------------------------------------------------------- /LEETCODE/C++/find-the-difference.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(n) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | char findTheDifference(string s, string t) { 7 | return accumulate(s.cbegin(), s.cend(), 0, std::bit_xor()) ^ 8 | accumulate(t.cbegin(), t.cend(), 0, std::bit_xor()); 9 | } 10 | }; 11 | -------------------------------------------------------------------------------- /LEETCODE/C++/find-the-highest-altitude.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(n) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | int largestAltitude(vector& gain) { 7 | int result = 0, curr = 0; 8 | for (const auto& g : gain) { 9 | curr += g; 10 | result = max(result, curr); 11 | } 12 | return result; 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /LEETCODE/C++/find-xor-sum-of-all-pairs-bitwise-and.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(n) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | int getXORSum(vector& arr1, vector& arr2) { 7 | const auto& op = [](int total, int x) { return total ^ x; }; 8 | return accumulate(cbegin(arr1), cend(arr1), 0, op) & 9 | accumulate(cbegin(arr2), cend(arr2), 0, op); 10 | } 11 | }; 12 | -------------------------------------------------------------------------------- /LEETCODE/C++/flip-string-to-monotone-increasing.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(n) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | int minFlipsMonoIncr(string S) { 7 | int flip0 = 0, flip1 = 0; 8 | for (const auto& c : S) { 9 | flip0 += static_cast(c == '1'); 10 | flip1 = min(flip0, flip1 + static_cast(c == '0')); 11 | } 12 | return flip1; 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /LEETCODE/C++/generate-a-string-with-characters-that-have-odd-counts.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(n) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | string generateTheString(int n) { 7 | auto result = string(n - 1, 'a'); 8 | result.push_back(n % 2 ? 'a' : 'b'); 9 | return result; 10 | } 11 | }; 12 | -------------------------------------------------------------------------------- /LEETCODE/C++/getRow.cpp: -------------------------------------------------------------------------------- 1 | // Time Complexity: O(n) 2 | // Space Complexity: O(1) 3 | 4 | class Solution { 5 | public: 6 | vector getRow(int rowIndex) { 7 | vector ans(rowIndex + 1, 1); 8 | 9 | for(int i = 2; i <= rowIndex; ++i) { 10 | for(int j = i - 1; j > 0; --j) { 11 | ans[j] += ans[j - 1]; 12 | } 13 | } 14 | 15 | return ans; 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /LEETCODE/C++/global-and-local-inversions.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(n) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | bool isIdealPermutation(vector& A) { 7 | for (int i = 0; i < A.size(); ++i) { 8 | if (abs(A[i] - i) > 1) { 9 | return false; 10 | } 11 | } 12 | return true; 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /LEETCODE/C++/hamming-distance.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(1) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | int hammingDistance(int x, int y) { 7 | int distance = 0; 8 | for (int z = x ^ y; z; z &= z - 1) { 9 | ++distance; 10 | } 11 | return distance; 12 | } 13 | }; 14 | -------------------------------------------------------------------------------- /LEETCODE/C++/height-checker.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(nlogn) 2 | // Space: O(n) 3 | 4 | class Solution { 5 | public: 6 | int heightChecker(vector& heights) { 7 | vector sorted_heights{heights}; 8 | sort(sorted_heights.begin(), sorted_heights.end()); 9 | int result = 0; 10 | for (int i = 0; i < heights.size(); ++i) { 11 | result += static_cast(heights[i] != sorted_heights[i]); 12 | } 13 | return result; 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /LEETCODE/C++/house-robber.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(n) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | int rob(vector& nums) { 7 | int last = 0, result = 0; 8 | for (const auto& i : nums) { 9 | auto tmp = result; 10 | result = max(last + i, result); 11 | last = tmp; 12 | } 13 | return result; 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /LEETCODE/C++/jewels-and-stones.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(m + n) 2 | // Space: O(n) 3 | 4 | class Solution { 5 | public: 6 | int numJewelsInStones(string J, string S) { 7 | unordered_set lookup; 8 | for (const auto& j : J) { 9 | lookup.emplace(j); 10 | } 11 | int result = 0; 12 | for (const auto& s : S) { 13 | result += lookup.count(s); 14 | } 15 | return result; 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /LEETCODE/C++/jump-game.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(n) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | bool canJump(vector& nums) { 7 | int reachable = 0; 8 | for (int i = 0; i <= reachable && i < nums.size(); ++i) { 9 | reachable = max(reachable, i + nums[i]); 10 | } 11 | return reachable >= nums.size() - 1; 12 | } 13 | }; 14 | -------------------------------------------------------------------------------- /LEETCODE/C++/k-th-symbol-in-grammar.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(logn) = O(1) because n is 32-bit integer 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | int kthGrammar(int N, int K) { 7 | return bitCount(K - 1) % 2; 8 | } 9 | 10 | private: 11 | int bitCount(int n) { 12 | int count = 0; 13 | for (; n; n &= n - 1) { 14 | ++count; 15 | } 16 | return count; 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /LEETCODE/C++/largest-number-at-least-twice-of-others.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(n) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | int dominantIndex(vector& nums) { 7 | const auto result = distance(nums.begin(), max_element(nums.begin(),nums.end())); 8 | for (int i = 0; i < nums.size(); ++i) { 9 | if (i != result && 2 * nums[i] > nums[result]) { 10 | return -1; 11 | } 12 | } 13 | return result; 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /LEETCODE/C++/largest-perimeter-triangle.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(nlogn) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | int largestPerimeter(vector& A) { 7 | sort(A.begin(), A.end()); 8 | for (int i = A.size() - 3; i >= 0; --i) { 9 | if (A[i] + A[i + 1] > A[i + 2]) { 10 | return A[i] + A[i + 1] + A[i + 2]; 11 | } 12 | } 13 | return 0; 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /LEETCODE/C++/last-moment-before-all-ants-fall-out-of-a-plank.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(n) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | int getLastMoment(int n, vector& left, vector& right) { 7 | return max(!left.empty() ? *max_element(cbegin(left), cend(left)) : 0, 8 | !right.empty() ? n - *min_element(cbegin(right), cend(right)) : 0); 9 | } 10 | }; 11 | -------------------------------------------------------------------------------- /LEETCODE/C++/length-of-last-word.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(n) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | int lengthOfLastWord(string s) { 7 | const auto is_space = [](const char c) { return isspace(c); }; 8 | const auto it = find_if_not(s.rbegin(), s.rend(), is_space); 9 | const auto jt = find_if(it, s.rend(), is_space); 10 | return distance(it, jt); 11 | } 12 | }; 13 | -------------------------------------------------------------------------------- /LEETCODE/C++/longest-palindrome.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(n) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | int longestPalindrome(string s) { 7 | int odds = 0; 8 | for (auto c = 'A'; c <= 'z'; ++c) { 9 | odds += count(s.cbegin(), s.cend(), c) & 1; 10 | } 11 | return s.length() - odds + (odds > 0); 12 | } 13 | }; 14 | -------------------------------------------------------------------------------- /LEETCODE/C++/longest-uncommon-subsequence-i.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(min(a, b)) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | int findLUSlength(string a, string b) { 7 | if (a == b) { 8 | return -1; 9 | } 10 | return max(a.length(), b.length()); 11 | } 12 | }; 13 | -------------------------------------------------------------------------------- /LEETCODE/C++/magical-string.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(n) 2 | // Space: O(n) 3 | 4 | class Solution { 5 | public: 6 | int magicalString(int n) { 7 | string S = "122"; 8 | for (int i = 2; S.length() < n; ++i) { 9 | S += string(S[i] - '0', S.back() ^ 3); 10 | } 11 | return count(S.begin(), S.begin() + n, '1'); 12 | } 13 | }; 14 | -------------------------------------------------------------------------------- /LEETCODE/C++/matrix-diagonal-sum.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(n) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | int diagonalSum(vector>& mat) { 7 | const int n = mat.size(); 8 | int result = 0; 9 | for (int i = 0; i < n; ++i) { 10 | result += mat[i][i] + mat[n - 1 - i][i]; 11 | } 12 | if (n % 2) { 13 | result -= mat[n / 2][n / 2]; 14 | } 15 | return result; 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /LEETCODE/C++/max-consecutive-ones.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(n) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | int findMaxConsecutiveOnes(vector& nums) { 7 | int result = 0, local_max = 0; 8 | for (const auto& n : nums) { 9 | local_max = n ? local_max + 1 : 0; 10 | result = max(result, local_max); 11 | } 12 | return result; 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /LEETCODE/C++/maxProfitII.cpp: -------------------------------------------------------------------------------- 1 | // Time Complexity: O(n) 2 | // Space Complexity: O(1) 3 | 4 | class Solution { 5 | public: 6 | int maxProfit(vector &prices) { 7 | const int n = prices.size(); 8 | int ans = 0; 9 | 10 | for(int i = 1; i < n; ++i) { 11 | int diff = prices[i] - prices[i - 1]; 12 | if(diff > 0) 13 | ans += diff; 14 | } 15 | 16 | return ans; 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /LEETCODE/C++/maximum-ascending-subarray-sum.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(n) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | int maxAscendingSum(vector& nums) { 7 | int result = 0, curr = 0; 8 | for (int i = 0; i < size(nums); ++i) { 9 | if (!(i && nums[i - 1] < nums[i])) { 10 | curr = 0; 11 | } 12 | curr += nums[i]; 13 | result = max(result, curr); 14 | } 15 | return result; 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /LEETCODE/C++/maximum-element-after-decreasing-and-rearranging.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(nlogn) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | int maximumElementAfterDecrementingAndRearranging(vector& arr) { 7 | sort(begin(arr), end(arr)); 8 | int result = 1; 9 | for (int i = 1; i < size(arr); ++i) { 10 | result = min(result + 1, arr[i]); 11 | } 12 | return result; 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /LEETCODE/C++/maximum-ice-cream-bars.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(nlogn) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | int maxIceCream(vector& costs, int coins) { 7 | sort(begin(costs), end(costs)); 8 | for (int i = 0; i < size(costs); ++i) { 9 | coins -= costs[i]; 10 | if (coins < 0) { 11 | return i; 12 | } 13 | } 14 | return size(costs); 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /LEETCODE/C++/maximum-nesting-depth-of-the-parentheses.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(n) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | int maxDepth(string s) { 7 | int result = 0, curr = 0; 8 | for (const auto& c : s) { 9 | if (c == '(') { 10 | result = max(result, ++curr); 11 | } else if (c == ')') { 12 | --curr; 13 | } 14 | } 15 | return result; 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /LEETCODE/C++/maximum-number-of-coins-you-can-get.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(nlogn) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | int maxCoins(vector& piles) { 7 | sort(begin(piles), end(piles)); 8 | int result = 0; 9 | for (int i = piles.size() / 3; i < piles.size(); i += 2) { 10 | result += piles[i]; 11 | } 12 | return result; 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /LEETCODE/C++/maximum-number-of-consecutive-values-you-can-make.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(nlogn) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | int getMaximumConsecutive(vector& coins) { 7 | sort(begin(coins), end(coins)); 8 | int result = 1; 9 | for (const auto& c : coins) { 10 | if (c > result) { 11 | break; 12 | } 13 | result += c; 14 | } 15 | return result; 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /LEETCODE/C++/maximum-product-of-two-elements-in-an-array.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(n) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | int maxProduct(vector& nums) { 7 | int m1 = 0, m2 = 0; 8 | for (const auto& num : nums) { 9 | if (num > m1) { 10 | m2 = m1; 11 | m1 = num; 12 | } else if (num > m2) { 13 | m2 = num; 14 | } 15 | } 16 | return (m1 - 1) * (m2 - 1); 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /LEETCODE/C++/maximum-score-after-splitting-a-string.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(n) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | int maxScore(string s) { 7 | int result = 0, zeros = 0, ones = 0; 8 | for (int i = 1; i + 1 < s.length(); ++i) { 9 | (s[i] == '0') ? ++zeros : ++ones; 10 | result = max(result, zeros - ones); 11 | } 12 | return result + ones + (s.front() == '0') + (s.back() == '1'); 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /LEETCODE/C++/maximum-score-from-removing-stones.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(1) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | int maximumScore(int a, int b, int c) { 7 | // assumed c is the max size 8 | // case1: a+b > c 9 | // => (a+b-c)//2 + c = (a+b+c)//2 < a+b 10 | // case2: a+b <= c 11 | // => a+b <= (a+b+c)//2 12 | return min((a + b + c) / 2, a + b + c - max({a, b, c})); 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /LEETCODE/C++/maximum-subarray.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(n) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | int maxSubArray(vector& nums) { 7 | int result = numeric_limits::min(); 8 | int curr = numeric_limits::min(); 9 | for (const auto &x : nums) { 10 | curr = (curr == numeric_limits::min()) ? x : max(curr + x, x); 11 | result = max(result, curr); 12 | } 13 | return result; 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /LEETCODE/C++/maximum-xor-for-each-query.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(n) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | vector getMaximumXor(vector& nums, int maximumBit) { 7 | vector result(size(nums)); 8 | int mask = (1 << maximumBit) - 1; 9 | for (int i = 0; i < size(nums); ++i) { 10 | result[size(nums) - 1 - i] = mask ^= nums[i]; 11 | } 12 | return result; 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /LEETCODE/C++/mean-of-array-after-removing-some-elements.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(n) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | double trimMean(vector& arr) { 7 | static const int P = 20; 8 | const int k = size(arr) / P; 9 | nth_element(begin(arr), begin(arr) + k - 1, end(arr)); 10 | nth_element(begin(arr) + k, end(arr) - k, end(arr)); 11 | return accumulate(begin(arr) + k, end(arr) - k, 0.0) / (size(arr) - 2 * k); 12 | } 13 | }; 14 | -------------------------------------------------------------------------------- /LEETCODE/C++/meeting-rooms.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(nlogn) 2 | // Space: O(n) 3 | 4 | class Solution { 5 | public: 6 | bool canAttendMeetings(vector>& intervals) { 7 | sort(intervals.begin(), intervals.end()); 8 | for (int i = 1; i < intervals.size(); ++i) { 9 | if (intervals[i][0] < intervals[i - 1][1]) { 10 | return false; 11 | } 12 | } 13 | return true; 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /LEETCODE/C++/min-cost-climbing-stairs.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(n) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | int minCostClimbingStairs(vector& cost) { 7 | vector dp(3); 8 | for (int i = cost.size() - 1; i >= 0; --i) { 9 | dp[i % 3] = cost[i] + 10 | min(dp[(i + 1) % 3], 11 | dp[(i + 2) % 3]); 12 | } 13 | return min(dp[0], dp[1]); 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /LEETCODE/C++/minimum-add-to-make-parentheses-valid.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(n) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | int minAddToMakeValid(string S) { 7 | int add = 0, bal = 0; 8 | for (const auto& c : S) { 9 | bal += c == '(' ? 1 : -1; 10 | if (bal == -1) { 11 | ++add; 12 | ++bal; 13 | } 14 | } 15 | return add + bal; 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /LEETCODE/C++/minimum-changes-to-make-alternating-binary-string.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(n) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | int minOperations(string s) { 7 | int cnt = 0; 8 | for (int i = 0; i < size(s); ++i) { 9 | if (s[i] - '0' == i % 2) { 10 | ++cnt; 11 | } 12 | } 13 | return min(cnt, int(size(s)) - cnt); 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /LEETCODE/C++/minimum-deletions-to-make-string-balanced.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(n) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | int minimumDeletions(string s) { 7 | int result = 0, b_cnt = 0; 8 | for (const auto& c : s) { 9 | if (c == 'b') { 10 | ++b_cnt; 11 | } else if (b_cnt) { 12 | --b_cnt; 13 | ++result; 14 | } 15 | } 16 | return result; 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /LEETCODE/C++/minimum-elements-to-add-to-form-a-given-sum.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(n) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | int minElements(vector& nums, int limit, int goal) { 7 | int64_t total = accumulate(cbegin(nums), cend(nums), 0LL); 8 | return (abs(total - goal) + (limit - 1)) / limit; 9 | } 10 | }; 11 | -------------------------------------------------------------------------------- /LEETCODE/C++/minimum-moves-to-equal-array-elements.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(n) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | int minMoves(vector& nums) { 7 | return accumulate(nums.cbegin(), nums.cend(), 0) - 8 | nums.size() * *min_element(nums.cbegin(), nums.cend()); 9 | } 10 | }; 11 | -------------------------------------------------------------------------------- /LEETCODE/C++/minimum-number-of-increments-on-subarrays-to-form-a-target-array.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(n) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | int minNumberOperations(vector& target) { 7 | int result = target[0]; 8 | for (int i = 1; i < target.size(); ++i) { 9 | result += max(target[i] - target[i - 1], 0); 10 | } 11 | return result; 12 | } 13 | }; 14 | -------------------------------------------------------------------------------- /LEETCODE/C++/minimum-operations-to-make-array-equal.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(1) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | int minOperations(int n) { 7 | // total = (2i+1 for i in xrange(n)) = n^2 8 | // left_half_total = (2i+1 for i in xrange(n//2)) = (n//2)^2 9 | // result = (n//2) * (total//n) - left_half_total = (n//2)*(n-n//2) = (n//2)*((n+1)//2) 10 | return (n / 2) * ((n + 1) / 2); 11 | } 12 | }; 13 | -------------------------------------------------------------------------------- /LEETCODE/C++/minimum-operations-to-make-the-array-increasing.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(n) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | int minOperations(vector& nums) { 7 | int result = 0, prev = 0; 8 | for (const auto& curr : nums) { 9 | if (prev < curr) { 10 | prev = curr; 11 | continue; 12 | } 13 | result += (++prev) - curr; 14 | } 15 | return result; 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /LEETCODE/C++/minimum-time-visiting-all-points.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(n) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | int minTimeToVisitAllPoints(vector>& points) { 7 | int result = 0; 8 | for(int i = 0; i + 1 < points.size(); ++i) { 9 | result += max(abs(points[i + 1][1] - points[i][1]), 10 | abs(points[i + 1][0] - points[i][0])); 11 | } 12 | return result; 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /LEETCODE/C++/minimum-value-to-get-positive-step-by-step-sum.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(n) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | int minStartValue(vector& nums) { 7 | int min_prefix = 0, prefix = 0; 8 | for (const auto& num : nums) { 9 | prefix += num; 10 | min_prefix = min(min_prefix, prefix); 11 | } 12 | return 1 - min_prefix; 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /LEETCODE/C++/monotonic-array.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(n) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | bool isMonotonic(vector& A) { 7 | bool inc = false, dec = false; 8 | for (int i = 0; i + 1 < A.size(); ++i) { 9 | if (A[i] < A[i + 1]) { 10 | inc = true; 11 | } else if (A[i] > A[i + 1]) { 12 | dec = true; 13 | } 14 | } 15 | return !inc || !dec; 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /LEETCODE/C++/n-repeated-element-in-size-2n-array.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(n) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | int repeatedNTimes(vector& A) { 7 | for (int i = 2; i < A.size(); ++i) { 8 | if (A[i - 1] == A[i] || A[i - 2] == A[i]) { 9 | return A[i]; 10 | } 11 | } 12 | return A[0]; 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /LEETCODE/C++/nim-game.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(1) 2 | // Soace: O(1) 3 | 4 | class Solution { 5 | public: 6 | bool canWinNim(int n) { 7 | return n % 4 != 0; 8 | } 9 | }; 10 | -------------------------------------------------------------------------------- /LEETCODE/C++/number-complement.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(1) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | int findComplement(int num) { 7 | unsigned int i = 1; 8 | while (i <= num) { 9 | i <<= 1; 10 | } 11 | return (i - 1) ^ num; 12 | } 13 | }; 14 | -------------------------------------------------------------------------------- /LEETCODE/C++/number-of-days-in-a-month.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(1) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | int numberOfDays(int Y, int M) { 7 | const auto& leap = (((Y % 4 == 0) && (Y % 100 != 0)) || (Y % 400 == 0)) ? 1 : 0; 8 | return (M == 2) ? 28 + leap : 31 - (M - 1) % 7 % 2; 9 | } 10 | }; 11 | -------------------------------------------------------------------------------- /LEETCODE/C++/number-of-segments-in-a-string.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(n) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | int countSegments(string s) { 7 | int result = static_cast(!s.empty() && s.back() != ' '); 8 | for (int i = 1; i < s.size(); ++i) { 9 | if (s[i] == ' ' && s[i - 1] != ' ') { 10 | ++result; 11 | } 12 | } 13 | return result; 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /LEETCODE/C++/number-of-steps-to-reduce-a-number-to-zero.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(logn) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | int numberOfSteps (int num) { 7 | int result = 0; 8 | while (num) { 9 | result += (num & 1) ? 2 : 1; 10 | num >>= 1; 11 | } 12 | return max(result - 1, 0); 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /LEETCODE/C++/number-of-students-doing-homework-at-a-given-time.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(n) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | int busyStudent(vector& startTime, vector& endTime, int queryTime) { 7 | int result = 0; 8 | for (int i = 0; i < startTime.size(); ++i) { 9 | if (startTime[i] <= queryTime && queryTime <= endTime[i]) { 10 | ++result; 11 | } 12 | } 13 | return result; 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /LEETCODE/C++/number-of-substrings-with-only-1s.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(n) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | int numSub(string s) { 7 | static const int MOD = 1e9 + 7; 8 | int result = 0, count = 0; 9 | for (const auto& c : s) { 10 | count = (c == '1') ? count + 1 : 0; 11 | result = (result + count) % MOD; 12 | } 13 | return result; 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /LEETCODE/C++/number-of-valid-subarrays.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(n) 2 | // Space: O(n) 3 | 4 | class Solution { 5 | public: 6 | int validSubarrays(vector& nums) { 7 | int result = 0; 8 | vector s; 9 | for (const auto& num : nums) { 10 | while (!s.empty() && s.back() > num) { 11 | s.pop_back(); 12 | } 13 | s.emplace_back(num); 14 | result += s.size(); 15 | } 16 | return result; 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /LEETCODE/C++/orderly-queue.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(n^2) 2 | // Space: O(n) 3 | 4 | class Solution { 5 | public: 6 | string orderlyQueue(string S, int K) { 7 | if (K == 1) { 8 | auto result{S}; 9 | for (int i = 1; i < S.length(); ++i) { 10 | result = min(result, S.substr(i) + S.substr(0, i)); 11 | } 12 | return result; 13 | } 14 | sort(S.begin(), S.end()); 15 | return S; 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /LEETCODE/C++/pairs-of-songs-with-total-durations-divisible-by-60.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(n) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | int numPairsDivisibleBy60(vector& time) { 7 | int result = 0; 8 | vector counter(60); 9 | for (const auto& t : time) { 10 | result += counter[(60 - t % 60) % 60]; 11 | ++counter[t % 60]; 12 | } 13 | return result; 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /LEETCODE/C++/palindrome-permutation.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(n) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | bool canPermutePalindrome(string s) { 7 | bitset<256> bits; 8 | for (const auto& c : s) { 9 | bits.flip(c); 10 | } 11 | return bits.count() < 2; 12 | } 13 | }; 14 | -------------------------------------------------------------------------------- /LEETCODE/C++/partitioning-into-minimum-number-of-deci-binary-numbers.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(n) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | int minPartitions(string n) { 7 | return *max_element(cbegin(n), cend(n)) - '0'; 8 | } 9 | }; 10 | -------------------------------------------------------------------------------- /LEETCODE/C++/play-with-chips.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(n) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | int minCostToMoveChips(vector& chips) { 7 | vector count(2); 8 | for (const auto& p : chips) { 9 | ++count[p % 2]; 10 | } 11 | return min(count[0], count[1]); 12 | } 13 | }; 14 | -------------------------------------------------------------------------------- /LEETCODE/C++/poor-pigs.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(1) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | int poorPigs(int buckets, int minutesToDie, int minutesToTest) { 7 | return ceil(log(buckets) / log(minutesToTest / minutesToDie + 1)); 8 | } 9 | }; 10 | -------------------------------------------------------------------------------- /LEETCODE/C++/power-of-three.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(1) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | static const int max_log3 = log(numeric_limits::max()) / log(3); 7 | static const int max_pow3 = pow(3, max_log3); 8 | 9 | bool isPowerOfThree(int n) { 10 | return n > 0 && max_pow3 % n == 0; 11 | } 12 | }; 13 | -------------------------------------------------------------------------------- /LEETCODE/C++/power-of-two.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(1) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | bool isPowerOfTwo(int n) { 7 | return n > 0 && (n & (n - 1)) == 0; 8 | } 9 | }; 10 | 11 | class Solution2 { 12 | public: 13 | bool isPowerOfTwo(int n) { 14 | return n > 0 && (n & ~-n) == 0; 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /LEETCODE/C++/range-addition-ii.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(p), p is the number of ops 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | int maxCount(int m, int n, vector>& ops) { 7 | for (const auto& op : ops) { 8 | m = min(m, op[0]); 9 | n = min(n, op[1]); 10 | } 11 | return m * n; 12 | } 13 | }; 14 | -------------------------------------------------------------------------------- /LEETCODE/C++/rectangle-area.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(1) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) { 7 | return (D - B) * (C - A) + 8 | (G - E) * (H - F) - 9 | max(0, (min(C, G) - max(A, E))) * 10 | max(0, (min(D, H) - max(B, F))); 11 | } 12 | }; 13 | -------------------------------------------------------------------------------- /LEETCODE/C++/rectangle-overlap.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(1) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | bool isRectangleOverlap(vector& rec1, vector& rec2) { 7 | return (intersect(rec1[0], rec1[2], rec2[0], rec2[2]) && 8 | intersect(rec1[1], rec1[3], rec2[1], rec2[3])); 9 | } 10 | 11 | private: 12 | bool intersect(int p_left, int p_right, int q_left, int q_right) { 13 | return max(p_left, q_left) < min(p_right, q_right); 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /LEETCODE/C++/reducing-dishes.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(nlogn) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | int maxSatisfaction(vector& satisfaction) { 7 | sort(begin(satisfaction), end(satisfaction), greater()); 8 | int result = 0; 9 | for (int i = 0, curr = 0; i < satisfaction.size() && curr + satisfaction[i] > 0; ++i) { 10 | curr += satisfaction[i]; 11 | result += curr; 12 | } 13 | return result; 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /LEETCODE/C++/remove-9.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(logn) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | int newInteger(int n) { 7 | int result = 0, base = 1; 8 | while (n > 0) { 9 | result += (n % 9) * base; 10 | n /= 9; 11 | base *= 10; 12 | } 13 | return result; 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /LEETCODE/C++/remove-all-adjacent-duplicates-in-string.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(n) 2 | // Space: O(n) 3 | 4 | class Solution { 5 | public: 6 | string removeDuplicates(string S) { 7 | string result; 8 | for (const auto& c : S) { 9 | if (!result.empty() && result.back() == c) { 10 | result.pop_back(); 11 | } else { 12 | result.push_back(c); 13 | } 14 | } 15 | return result; 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /LEETCODE/C++/remove-duplicates-from-sorted-array.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(n) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | int removeDuplicates(vector& nums) { 7 | int last = -1; 8 | for (const auto& num : nums) { 9 | if (last == -1 || nums[last] != num) { 10 | nums[++last] = num; 11 | } 12 | } 13 | return last + 1; 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /LEETCODE/C++/remove-element.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(n) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | int removeElement(vector& nums, int val) { 7 | int left = 0, right = nums.size(); 8 | while (left < right) { 9 | if (nums[left] != val) { 10 | ++left; 11 | } else { 12 | swap(nums[left], nums[--right]); 13 | } 14 | } 15 | return right; 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /LEETCODE/C++/replace-all-digits-with-characters.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(n) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | string replaceDigits(string s) { 7 | for (int i = 1; i < size(s); i += 2) { 8 | s[i] += s[i - 1] - '0'; 9 | } 10 | return s; 11 | } 12 | }; 13 | -------------------------------------------------------------------------------- /LEETCODE/C++/replace-elements-with-greatest-element-on-right-side.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(n) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | vector replaceElements(vector& arr) { 7 | int curr_max = -1; 8 | for (int i = arr.size() - 1; i >= 0; --i) { 9 | tie(arr[i], curr_max) = make_pair(curr_max, max(curr_max, arr[i])); 10 | } 11 | return arr; 12 | } 13 | }; 14 | -------------------------------------------------------------------------------- /LEETCODE/C++/reverse-string-ii.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(n) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | string reverseStr(string s, int k) { 7 | for (int left = 0; left < s.size(); left += 2 * k) { 8 | for (int i = left, j = min(left + k - 1, static_cast(s.size()) - 1); 9 | i < j; ++i, --j) { 10 | swap(s[i], s[j]); 11 | } 12 | } 13 | return s; 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /LEETCODE/C++/reverse-string.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(n) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | string reverseString(string s) { 7 | for (int i = 0, j = s.length() - 1; i < j; ++i, --j) { 8 | swap(s[i], s[j]); 9 | } 10 | return s; 11 | } 12 | }; 13 | 14 | // Time: O(n) 15 | // Space: O(1) 16 | class Solution2 { 17 | public: 18 | string reverseString(string s) { 19 | reverse(s.begin(), s.end()); 20 | return s; 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /LEETCODE/C++/reverse-words-in-a-string-ii.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(n) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | void reverseWords(string &s) { 7 | reverse(s.begin(), s.end()); 8 | for (int i = 0, j = 0; j <= s.length(); ++j) { 9 | if (j == s.length() || s[j] == ' ') { 10 | reverse(s.begin() + i, s.begin() + j); 11 | i = j + 1; 12 | } 13 | } 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /LEETCODE/C++/reverse-words-in-a-string-iii.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(n) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | string reverseWords(string s) { 7 | for (int i = 0, j = 0; j <= s.length(); ++j) { 8 | if (j == s.length() || s[j] == ' ') { 9 | reverse(s.begin() + i, s.begin() + j); 10 | i = j + 1; 11 | } 12 | } 13 | return s; 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /LEETCODE/C++/richest-customer-wealth.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(m * n) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | int maximumWealth(vector>& accounts) { 7 | int result = 0; 8 | for (const auto& account : accounts) { 9 | int total = accumulate(cbegin(account), cend(account), 0); 10 | result = max(result, total); 11 | } 12 | return result; 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /LEETCODE/C++/rotate-array.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(n) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | void rotate(vector& nums, int k) { 7 | if (!nums.empty()) { 8 | k %= nums.size(); 9 | reverse(nums.begin(), nums.begin() + nums.size() - k); 10 | reverse(nums.begin() + nums.size() - k, nums.end()); 11 | reverse(nums.begin(), nums.end()); 12 | } 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /LEETCODE/C++/running-sum-of-1d-array.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(n) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | vector runningSum(vector& nums) { 7 | partial_sum(begin(nums), end(nums), begin(nums)); 8 | return nums; 9 | } 10 | }; 11 | -------------------------------------------------------------------------------- /LEETCODE/C++/sign-of-the-product-of-an-array.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(n) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | int arraySign(vector& nums) { 7 | uint8_t flag = 0; 8 | for (const auto& x : nums) { 9 | if (x == 0) { 10 | return 0; 11 | } 12 | if (x < 0) { 13 | flag ^= 1; 14 | } 15 | } 16 | return flag % 2 ? -1 : 1; 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /LEETCODE/C++/single-number-ii.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(n) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | int singleNumber(vector& nums) { 7 | int one = 0, two = 0; 8 | 9 | for (const auto& i : nums) { 10 | int new_one = (~i & one) | (i & ~one & ~two); 11 | int new_two = (~i & two) | (i & one); 12 | one = new_one, two = new_two; 13 | } 14 | 15 | return one; 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /LEETCODE/C++/single-number.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(n) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | int singleNumber(vector& nums) { 7 | return accumulate(nums.cbegin(), nums.cend(), 8 | 0, std::bit_xor()); 9 | } 10 | }; 11 | -------------------------------------------------------------------------------- /LEETCODE/C++/smallest-range-i.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(n) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | int smallestRangeI(vector& A, int K) { 7 | return max(0, *max_element(A.cbegin(), A.cend()) - 8 | *min_element(A.cbegin(), A.cend()) - 9 | 2 * K); 10 | } 11 | }; 12 | -------------------------------------------------------------------------------- /LEETCODE/C++/smallest-string-with-a-given-numeric-value.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(n) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | string getSmallestString(int n, int k) { 7 | static const int MAX_DIFF = 'z' - 'a'; 8 | 9 | k -= n; 10 | string result(n, 'a'); 11 | for (int i = n - 1; i >= 0 && k > 0; --i, k -= min(k, MAX_DIFF)) { 12 | result[i] = 'a' + min(k, MAX_DIFF); 13 | } 14 | return result; 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /LEETCODE/C++/sort-array-by-parity-ii.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(n) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | vector sortArrayByParityII(vector& A) { 7 | int j = 1; 8 | for (int i = 0; i < A.size(); i += 2) { 9 | if (A[i] % 2) { 10 | while (A[j] % 2) { 11 | j += 2; 12 | } 13 | swap(A[i], A[j]); 14 | } 15 | } 16 | return A; 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /LEETCODE/C++/sort-array-by-parity.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(n) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | vector sortArrayByParity(vector& A) { 7 | for (int i = 0, j = 0; j < A.size(); ++j) { 8 | if (A[j] % 2 == 0) { 9 | swap(A[i++], A[j]); 10 | } 11 | } 12 | return A; 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /LEETCODE/C++/sort-integers-by-the-number-of-1-bits.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(nlogn) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | vector sortByBits(vector& arr) { 7 | sort(begin(arr), end(arr), 8 | [](const auto& a, const auto& b) { 9 | return make_pair(__builtin_popcount(a), a) < 10 | make_pair(__builtin_popcount(b), b); 11 | }); 12 | return arr; 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /LEETCODE/C++/split-a-string-in-balanced-strings.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(n) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | int balancedStringSplit(string s) { 7 | int result = 0, count = 0; 8 | for (const auto& c : s) { 9 | count += (c == 'L') ? 1 : -1; 10 | if (count == 0) { 11 | ++result; 12 | } 13 | } 14 | return result; 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /LEETCODE/C++/stone-game-iv.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(n * sqrt(n)) 2 | // Space: O(n) 3 | 4 | class Solution { 5 | public: 6 | bool winnerSquareGame(int n) { 7 | vector dp(n + 1); 8 | for (int i = 1; i <= n; ++i) { 9 | for (int j = 1; j * j <= i; ++j) { 10 | if (!dp[i - j * j]) { 11 | dp[i] = true; 12 | break; 13 | } 14 | } 15 | } 16 | return dp.back(); 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /LEETCODE/C++/stone-game-viii.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(n) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | int stoneGameVIII(vector& stones) { 7 | partial_sum(begin(stones), end(stones), begin(stones)); 8 | return accumulate(next(rbegin(stones)), prev(rend(stones)), stones.back(), 9 | [](int curr, int x) { return max(curr, x - curr); }); 10 | } 11 | }; 12 | -------------------------------------------------------------------------------- /LEETCODE/C++/subarray-sums-divisible-by-k.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(n) 2 | // Space: O(k) 3 | 4 | class Solution { 5 | public: 6 | int subarraysDivByK(vector& A, int K) { 7 | unordered_map count; 8 | count[0] = 1; 9 | int prefix = 0, result = 0; 10 | for (const auto& a : A) { 11 | prefix = (prefix + (a % K + K)) % K; 12 | result += count[prefix]++; 13 | } 14 | return result; 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /LEETCODE/C++/subtract-the-product-and-sum-of-digits-of-an-integer.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(n) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | int subtractProductAndSum(int n) { 7 | int product = 1, total = 0; 8 | for (; n; n /= 10) { 9 | product *= n % 10; 10 | total += n % 10; 11 | } 12 | return product - total; 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /LEETCODE/C++/sum-of-digits-in-base-k.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(logn) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | int sumBase(int n, int k) { 7 | int result = 0; 8 | for (; n; n /= k) { 9 | result += n % k; 10 | } 11 | return result; 12 | } 13 | }; 14 | -------------------------------------------------------------------------------- /LEETCODE/C++/sum-of-digits-in-the-minimum-number.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(n * l), l is the max length of numbers 2 | // Space: O(l) 3 | 4 | class Solution { 5 | public: 6 | int sumOfDigits(vector& A) { 7 | int min_num = *min_element(A.begin(),A.end()); 8 | int total = 0; 9 | while (min_num) { 10 | total += min_num % 10; 11 | min_num /= 10; 12 | } 13 | return total % 2 == 0; 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /LEETCODE/C++/sum-of-square-numbers.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(sqrt(c) * logc) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | bool judgeSquareSum(int c) { 7 | for (long long a = 0; a * a <= c; ++a) { 8 | auto b = static_cast(sqrt(c - a * a)); 9 | if (a * a + b * b == c) { 10 | return true; 11 | } 12 | } 13 | return false; 14 | } 15 | }; 16 | 17 | -------------------------------------------------------------------------------- /LEETCODE/C++/sum-of-two-integers.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(1) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | int getSum(int a, int b) { 7 | while (b) { 8 | int carry = a & b; 9 | a ^= b; 10 | b = carry << 1; 11 | } 12 | return a; 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /LEETCODE/C++/teemo-attacking.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(n) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | int findPoisonedDuration(vector& timeSeries, int duration) { 7 | int result = duration * timeSeries.size(); 8 | for (int i = 1; i < timeSeries.size(); ++i){ 9 | result -= max(0, duration - (timeSeries[i] - timeSeries[i - 1])); 10 | } 11 | return result; 12 | } 13 | }; 14 | -------------------------------------------------------------------------------- /LEETCODE/C++/thousand-separator.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(n) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | string thousandSeparator(int n) { 7 | string result; 8 | const auto& s = to_string(n); 9 | for (int i = 0; i < s.length(); ++i) { 10 | if (i > 0 && (s.length() - i) % 3 == 0) { 11 | result += "."; 12 | } 13 | result += s[i]; 14 | } 15 | return result; 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /LEETCODE/C++/three-consecutive-odds.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(n) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | bool threeConsecutiveOdds(vector& arr) { 7 | int count = 0; 8 | for (const auto& x : arr) { 9 | count = x % 2 ? count + 1 : 0; 10 | if (count == 3) { 11 | return true; 12 | } 13 | } 14 | return false; 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /LEETCODE/C++/to-lower-case.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(n) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | string toLowerCase(string str) { 7 | string result; 8 | for (const auto c : str) { 9 | if ('A' <= c && c <= 'Z') { 10 | result.push_back('a' + c - 'A'); 11 | } else { 12 | result.push_back(c); 13 | } 14 | } 15 | return result; 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /LEETCODE/C++/toss-strange-coins.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(n^2) 2 | // Space: O(n) 3 | 4 | class Solution { 5 | public: 6 | double probabilityOfHeads(vector& prob, int target) { 7 | vector dp(target + 1); 8 | dp[0] = 1.0; 9 | for (const auto& p : prob) { 10 | for (int i = target; i >= 0; --i) { 11 | dp[i] = ((i >= 1) ? dp[i - 1] : 0.0) * p + dp[i] * (1 - p); 12 | } 13 | } 14 | return dp[target]; 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /LEETCODE/C++/transpose-matrix.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(r * c) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | vector> transpose(vector>& A) { 7 | vector> result(A[0].size(), vector(A.size())); 8 | for (int r = 0; r < A.size(); ++r) { 9 | for (int c = 0; c < A[0].size(); ++c) { 10 | result[c][r] = A[r][c]; 11 | } 12 | } 13 | return result; 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /LEETCODE/C++/truncate-sentence.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(n) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | string truncateSentence(string s, int k) { 7 | for (int i = 0; i < size(s); ++i) { 8 | if (s[i] == ' ' && --k == 0) { 9 | return s.substr(0, i); 10 | } 11 | } 12 | return s; 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /LEETCODE/C++/tuple-with-same-product.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(n^2) 2 | // Space: O(n^2) 3 | 4 | class Solution { 5 | public: 6 | int tupleSameProduct(vector& nums) { 7 | int result = 0; 8 | unordered_map count; 9 | for (int i = 0; i < size(nums); ++i) { 10 | for (int j = i + 1; j < size(nums); ++j) { 11 | result += count[nums[i] * nums[j]]++; 12 | } 13 | } 14 | return 8 * result; 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /LEETCODE/C++/two-sum.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(n) 2 | // Space: O(n) 3 | 4 | class Solution { 5 | public: 6 | vector twoSum(vector& nums, int target) { 7 | unordered_map lookup; 8 | for (int i = 0; i < nums.size(); ++i) { 9 | if (lookup.count(target - nums[i])) { 10 | return {lookup[target - nums[i]], i}; 11 | } 12 | lookup[nums[i]] = i; 13 | } 14 | return {}; 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /LEETCODE/C++/ugly-number.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(logn) = O(1) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | bool isUgly(int num) { 7 | if (num == 0) { 8 | return false; 9 | } 10 | for (const auto& i : {2, 3, 5}) { 11 | while (num % i == 0) { 12 | num /= i; 13 | } 14 | } 15 | return num == 1; 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /LEETCODE/C++/uniquePaths2.cpp: -------------------------------------------------------------------------------- 1 | // Time Complexity: O(mn) 2 | // Space Complexity: O(n) 3 | 4 | class Solution { 5 | public: 6 | int uniquePaths(int m, int n) { 7 | vector f(n, 1); 8 | for(int i = 1; i < m; ++i) { 9 | for(int j = 1; j < n; ++j) { 10 | f[j] += f[j-1]; 11 | } 12 | } 13 | return f[n-1]; 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /LEETCODE/C++/valid-boomerang.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(1) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | bool isBoomerang(vector>& points) { 7 | return (points[0][0] - points[1][0]) * (points[0][1] - points[2][1]) - 8 | (points[0][0] - points[2][0]) * (points[0][1] - points[1][1]) != 0; 9 | } 10 | }; 11 | -------------------------------------------------------------------------------- /LEETCODE/C++/valid-mountain-array.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(n) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | bool validMountainArray(vector& A) { 7 | int i = 0; 8 | while (i + 1 < A.size() && A[i] < A[i + 1]) { 9 | ++i; 10 | } 11 | int j = A.size() - 1; 12 | while (j - 1 >= 0 && A[j - 1] > A[j]) { 13 | --j; 14 | } 15 | return 0 < i && i == j && j < A.size() - 1; 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /LEETCODE/C++/water-bottles.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(logn/logm), n is numBottles, m is numExchange 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | int numWaterBottles(int numBottles, int numExchange) { 7 | int result = numBottles; 8 | while (numBottles >= numExchange) { 9 | result += numBottles / numExchange; 10 | numBottles = numBottles / numExchange + numBottles % numExchange; 11 | } 12 | return result; 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /LEETCODE/C++/wiggle-sort.cpp: -------------------------------------------------------------------------------- 1 | // Time: O(n) 2 | // Space: O(1) 3 | 4 | class Solution { 5 | public: 6 | void wiggleSort(vector& nums) { 7 | for (int i = 1; i < nums.size(); ++i) { 8 | if (((i % 2) && nums[i] < nums[i - 1]) || 9 | (!(i % 2) && nums[i] > nums[i - 1])) { 10 | // Swap unordered elements. 11 | swap(nums[i], nums[i - 1]); 12 | } 13 | } 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /LEETCODE/Python/1-bit-and-2-bit-characters.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(1) 3 | 4 | 5 | class Solution(object): 6 | def isOneBitCharacter(self, bits): 7 | """ 8 | :type bits: List[int] 9 | :rtype: bool 10 | """ 11 | parity = 0 12 | for i in reversed(xrange(len(bits)-1)): 13 | if bits[i] == 0: 14 | break 15 | parity ^= bits[i] 16 | return parity == 0 17 | 18 | -------------------------------------------------------------------------------- /LEETCODE/Python/4sum-ii.py: -------------------------------------------------------------------------------- 1 | # Time: O(n^2) 2 | # Space: O(n^2) 3 | 4 | import collections 5 | 6 | 7 | class Solution(object): 8 | def fourSumCount(self, A, B, C, D): 9 | """ 10 | :type A: List[int] 11 | :type B: List[int] 12 | :type C: List[int] 13 | :type D: List[int] 14 | :rtype: int 15 | """ 16 | A_B_sum = collections.Counter(a+b for a in A for b in B) 17 | return sum(A_B_sum[-c-d] for c in C for d in D) 18 | 19 | -------------------------------------------------------------------------------- /LEETCODE/Python/add-digits.py: -------------------------------------------------------------------------------- 1 | # Time: O(1) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | """ 6 | :type num: int 7 | :rtype: int 8 | """ 9 | def addDigits(self, num): 10 | return (num - 1) % 9 + 1 if num > 0 else 0 11 | 12 | -------------------------------------------------------------------------------- /LEETCODE/Python/angle-between-hands-of-a-clock.py: -------------------------------------------------------------------------------- 1 | # Time: O(1) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def angleClock(self, hour, minutes): 6 | """ 7 | :type hour: int 8 | :type minutes: int 9 | :rtype: float 10 | """ 11 | angle1 = (hour % 12 * 60.0 + minutes) / 720.0 12 | angle2 = minutes / 60.0 13 | diff = abs(angle1-angle2) 14 | return min(diff, 1.0-diff) * 360.0 15 | -------------------------------------------------------------------------------- /LEETCODE/Python/armstrong-number.py: -------------------------------------------------------------------------------- 1 | # Time: O(klogk) 2 | # Space: O(k) 3 | 4 | class Solution(object): 5 | def isArmstrong(self, N): 6 | """ 7 | :type N: int 8 | :rtype: bool 9 | """ 10 | n_str = str(N) 11 | return sum(int(i)**len(n_str) for i in n_str) == N 12 | -------------------------------------------------------------------------------- /LEETCODE/Python/average-waiting-time.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def averageWaitingTime(self, customers): 6 | """ 7 | :type customers: List[List[int]] 8 | :rtype: float 9 | """ 10 | avai = wait = 0.0 11 | for a, t in customers: 12 | avai = max(avai, a)+t 13 | wait += avai-a 14 | return wait/len(customers) 15 | -------------------------------------------------------------------------------- /LEETCODE/Python/beautiful-array.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(n) 3 | 4 | class Solution(object): 5 | def beautifulArray(self, N): 6 | """ 7 | :type N: int 8 | :rtype: List[int] 9 | """ 10 | result = [1] 11 | while len(result) < N: 12 | result = [i*2 - 1 for i in result] + [i*2 for i in result] 13 | return [i for i in result if i <= N] 14 | -------------------------------------------------------------------------------- /LEETCODE/Python/best-sightseeing-pair.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def maxScoreSightseeingPair(self, A): 6 | """ 7 | :type A: List[int] 8 | :rtype: int 9 | """ 10 | result, curr = 0, 0 11 | for x in A: 12 | result = max(result, curr+x) 13 | curr = max(curr, x)-1 14 | return result 15 | -------------------------------------------------------------------------------- /LEETCODE/Python/best-time-to-buy-and-sell-stock.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | # @param prices, a list of integer 6 | # @return an integer 7 | def maxProfit(self, prices): 8 | max_profit, min_price = 0, float("inf") 9 | for price in prices: 10 | min_price = min(min_price, price) 11 | max_profit = max(max_profit, price - min_price) 12 | return max_profit 13 | 14 | -------------------------------------------------------------------------------- /LEETCODE/Python/binary-number-with-alternating-bits.py: -------------------------------------------------------------------------------- 1 | # Time: O(1) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def hasAlternatingBits(self, n): 6 | """ 7 | :type n: int 8 | :rtype: bool 9 | """ 10 | n, curr = divmod(n, 2) 11 | while n > 0: 12 | if curr == n % 2: 13 | return False 14 | n, curr = divmod(n, 2) 15 | return True 16 | 17 | -------------------------------------------------------------------------------- /LEETCODE/Python/binary-prefix-divisible-by-5.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def prefixesDivBy5(self, A): 6 | """ 7 | :type A: List[int] 8 | :rtype: List[bool] 9 | """ 10 | for i in xrange(1, len(A)): 11 | A[i] += A[i-1] * 2 % 5 12 | return [x % 5 == 0 for x in A] 13 | -------------------------------------------------------------------------------- /LEETCODE/Python/bitwise-ors-of-subarrays.py: -------------------------------------------------------------------------------- 1 | # Time: O(32 * n) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def subarrayBitwiseORs(self, A): 6 | """ 7 | :type A: List[int] 8 | :rtype: int 9 | """ 10 | result, curr = set(), {0} 11 | for i in A: 12 | curr = {i} | {i | j for j in curr} 13 | result |= curr 14 | return len(result) 15 | 16 | -------------------------------------------------------------------------------- /LEETCODE/Python/break-a-palindrome.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def breakPalindrome(self, palindrome): 6 | """ 7 | :type palindrome: str 8 | :rtype: str 9 | """ 10 | for i in xrange(len(palindrome)//2): 11 | if palindrome[i] != 'a': 12 | return palindrome[:i] + 'a' + palindrome[i+1:] 13 | return palindrome[:-1] + 'b' if len(palindrome) >= 2 else "" 14 | -------------------------------------------------------------------------------- /LEETCODE/Python/broken-calculator.py: -------------------------------------------------------------------------------- 1 | # Time: O(logn) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def brokenCalc(self, X, Y): 6 | """ 7 | :type X: int 8 | :type Y: int 9 | :rtype: int 10 | """ 11 | result = 0 12 | while X < Y: 13 | if Y%2: 14 | Y += 1 15 | else: 16 | Y /= 2 17 | result += 1 18 | return result + X-Y 19 | -------------------------------------------------------------------------------- /LEETCODE/Python/build-an-array-with-stack-operations.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def buildArray(self, target, n): 6 | """ 7 | :type target: List[int] 8 | :type n: int 9 | :rtype: List[str] 10 | """ 11 | result, curr = [], 1 12 | for t in target: 13 | result.extend(["Push", "Pop"]*(t-curr)) 14 | result.append("Push") 15 | curr = t+1 16 | return result 17 | -------------------------------------------------------------------------------- /LEETCODE/Python/bulb-switcher-iii.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def numTimesAllBlue(self, light): 6 | """ 7 | :type light: List[int] 8 | :rtype: int 9 | """ 10 | result, right = 0, 0 11 | for i, num in enumerate(light, 1): 12 | right = max(right, num) 13 | result += (right == i) 14 | return result 15 | -------------------------------------------------------------------------------- /LEETCODE/Python/bulb-switcher-iv.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def minFlips(self, target): 6 | """ 7 | :type target: str 8 | :rtype: int 9 | """ 10 | result, curr = 0, '0' 11 | for c in target: 12 | if c == curr: 13 | continue 14 | curr = c 15 | result += 1 16 | return result 17 | -------------------------------------------------------------------------------- /LEETCODE/Python/bulb-switcher.py: -------------------------------------------------------------------------------- 1 | # Time: O(1) 2 | # Space: O(1) 3 | 4 | import math 5 | 6 | 7 | class Solution(object): 8 | def bulbSwitch(self, n): 9 | """ 10 | type n: int 11 | rtype: int 12 | """ 13 | # The number of full squares. 14 | return int(math.sqrt(n)) 15 | 16 | -------------------------------------------------------------------------------- /LEETCODE/Python/chalkboard-xor-game.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(1) 3 | 4 | from operator import xor 5 | from functools import reduce 6 | 7 | 8 | class Solution(object): 9 | def xorGame(self, nums): 10 | """ 11 | :type nums: List[int] 12 | :rtype: bool 13 | """ 14 | return reduce(xor, nums) == 0 or \ 15 | len(nums) % 2 == 0 16 | 17 | -------------------------------------------------------------------------------- /LEETCODE/Python/check-if-array-is-sorted-and-rotated.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def check(self, nums): 6 | """ 7 | :type nums: List[int] 8 | :rtype: bool 9 | """ 10 | count = 0 11 | for i in xrange(len(nums)): 12 | if nums[i] > nums[(i+1)%len(nums)]: 13 | count += 1 14 | if count > 1: 15 | return False 16 | return True 17 | -------------------------------------------------------------------------------- /LEETCODE/Python/check-if-binary-string-has-at-most-one-segment-of-ones.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def checkOnesSegment(self, s): 6 | """ 7 | :type s: str 8 | :rtype: bool 9 | """ 10 | return "01" not in s 11 | -------------------------------------------------------------------------------- /LEETCODE/Python/check-if-it-is-a-straight-line.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def checkStraightLine(self, coordinates): 6 | """ 7 | :type coordinates: List[List[int]] 8 | :rtype: bool 9 | """ 10 | i, j = coordinates[:2] 11 | return all(i[0] * j[1] - j[0] * i[1] + 12 | j[0] * k[1] - k[0] * j[1] + 13 | k[0] * i[1] - i[0] * k[1] == 0 14 | for k in coordinates) 15 | -------------------------------------------------------------------------------- /LEETCODE/Python/check-if-n-and-its-double-exist.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(n) 3 | 4 | class Solution(object): 5 | def checkIfExist(self, arr): 6 | """ 7 | :type arr: List[int] 8 | :rtype: bool 9 | """ 10 | lookup = set() 11 | for x in arr: 12 | if 2*x in lookup or \ 13 | (x%2 == 0 and x//2 in lookup): 14 | return True 15 | lookup.add(x) 16 | return False 17 | -------------------------------------------------------------------------------- /LEETCODE/Python/check-if-number-is-a-sum-of-powers-of-three.py: -------------------------------------------------------------------------------- 1 | # Time: O(logn) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def checkPowersOfThree(self, n): 6 | """ 7 | :type n: int 8 | :rtype: bool 9 | """ 10 | while n > 0: 11 | if n%3 == 2: 12 | return False 13 | n //= 3 14 | return True 15 | -------------------------------------------------------------------------------- /LEETCODE/Python/check-if-the-sentence-is-pangram.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(26) = O(1) 3 | 4 | class Solution(object): 5 | def checkIfPangram(self, sentence): 6 | """ 7 | :type sentence: str 8 | :rtype: bool 9 | """ 10 | return len(set(sentence)) == 26 11 | -------------------------------------------------------------------------------- /LEETCODE/Python/circular-permutation-in-binary-representation.py: -------------------------------------------------------------------------------- 1 | # Time: O(2^n) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def circularPermutation(self, n, start): 6 | """ 7 | :type n: int 8 | :type start: int 9 | :rtype: List[int] 10 | """ 11 | return [start ^ (i>>1) ^ i for i in xrange(1< mask: 12 | mask = mask*2+1 13 | return mask-N 14 | -------------------------------------------------------------------------------- /LEETCODE/Python/complex-number-multiplication.py: -------------------------------------------------------------------------------- 1 | # Time: O(1) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def complexNumberMultiply(self, a, b): 6 | """ 7 | :type a: str 8 | :type b: str 9 | :rtype: str 10 | """ 11 | ra, ia = map(int, a[:-1].split('+')) 12 | rb, ib = map(int, b[:-1].split('+')) 13 | return '%d+%di' % (ra * rb - ia * ib, ra * ib + ia * rb) 14 | 15 | -------------------------------------------------------------------------------- /LEETCODE/Python/concatenation-of-consecutive-binary-numbers.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def concatenatedBinary(self, n): 6 | """ 7 | :type n: int 8 | :rtype: int 9 | """ 10 | MOD = 10**9+7 11 | result = l = 0 12 | for i in xrange(1, n+1): 13 | if i&(i-1) == 0: 14 | l += 1 15 | result = ((result< len(set(nums)) 9 | 10 | -------------------------------------------------------------------------------- /LEETCODE/Python/count-all-valid-pickup-and-delivery-options.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def countOrders(self, n): 6 | """ 7 | :type n: int 8 | :rtype: int 9 | """ 10 | MOD = 10**9+7 11 | result = 1 12 | for i in reversed(xrange(2, 2*n+1, 2)): 13 | result = result * i*(i-1)//2 % MOD 14 | return result 15 | -------------------------------------------------------------------------------- /LEETCODE/Python/count-items-matching-a-rule.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def countMatches(self, items, ruleKey, ruleValue): 6 | """ 7 | :type items: List[List[str]] 8 | :type ruleKey: str 9 | :type ruleValue: str 10 | :rtype: int 11 | """ 12 | rule = {"type":0, "color":1, "name":2} 13 | return sum(item[rule[ruleKey]] == ruleValue for item in items) 14 | -------------------------------------------------------------------------------- /LEETCODE/Python/count-negative-numbers-in-a-sorted-matrix.py: -------------------------------------------------------------------------------- 1 | # Time: O(m + n) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def countNegatives(self, grid): 6 | """ 7 | :type grid: List[List[int]] 8 | :rtype: int 9 | """ 10 | result, c = 0, len(grid[0])-1 11 | for row in grid: 12 | while c >= 0 and row[c] < 0: 13 | c -= 1 14 | result += len(grid[0])-1-c 15 | return result 16 | -------------------------------------------------------------------------------- /LEETCODE/Python/count-numbers-with-unique-digits.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def countNumbersWithUniqueDigits(self, n): 6 | """ 7 | :type n: int 8 | :rtype: int 9 | """ 10 | if n == 0: 11 | return 1 12 | count, fk = 10, 9 13 | for k in xrange(2, n+1): 14 | fk *= 10 - (k-1) 15 | count += fk 16 | return count 17 | 18 | -------------------------------------------------------------------------------- /LEETCODE/Python/count-odd-numbers-in-an-interval-range.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def countOdds(self, low, high): 6 | """ 7 | :type low: int 8 | :type high: int 9 | :rtype: int 10 | """ 11 | return (high+1)//2 - ((low-1)+1)//2 12 | -------------------------------------------------------------------------------- /LEETCODE/Python/count-of-matches-in-tournament.py: -------------------------------------------------------------------------------- 1 | # Time: O(1) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def numberOfMatches(self, n): 6 | """ 7 | :type n: int 8 | :rtype: int 9 | """ 10 | return n-1 11 | -------------------------------------------------------------------------------- /LEETCODE/Python/count-substrings-with-only-one-distinct-letter.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def countLetters(self, S): 6 | """ 7 | :type S: str 8 | :rtype: int 9 | """ 10 | result = len(S) 11 | left = 0 12 | for right in xrange(1, len(S)): 13 | if S[right] == S[left]: 14 | result += right-left 15 | else: 16 | left = right 17 | return result 18 | -------------------------------------------------------------------------------- /LEETCODE/Python/count-ways-to-distribute-candies.py: -------------------------------------------------------------------------------- 1 | # Time: O(n * k) 2 | # Space: O(k) 3 | 4 | class Solution(object): 5 | def waysToDistribute(self, n, k): 6 | """ 7 | :type n: int 8 | :type k: int 9 | :rtype: int 10 | """ 11 | MOD = 10**9+7 12 | dp = [1]*k 13 | for i in xrange(1, n): 14 | for j in reversed(xrange(1, min(i, k))): 15 | dp[j] = ((j+1)*dp[j] + dp[j-1]) % MOD 16 | return dp[k-1] 17 | -------------------------------------------------------------------------------- /LEETCODE/Python/crawler-log-folder.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def minOperations(self, logs): 6 | """ 7 | :type logs: List[str] 8 | :rtype: int 9 | """ 10 | result = 0 11 | for log in logs: 12 | if log == "../": 13 | if result > 0: 14 | result -= 1 15 | elif log != "./": 16 | result += 1 17 | return result 18 | -------------------------------------------------------------------------------- /LEETCODE/Python/decode-xored-array.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def decode(self, encoded, first): 6 | """ 7 | :type encoded: List[int] 8 | :type first: int 9 | :rtype: List[int] 10 | """ 11 | result = [first] 12 | for x in encoded: 13 | result.append(result[-1]^x) 14 | return result 15 | -------------------------------------------------------------------------------- /LEETCODE/Python/decompress-run-length-encoded-list.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def decompressRLElist(self, nums): 6 | """ 7 | :type nums: List[int] 8 | :rtype: List[int] 9 | """ 10 | return [nums[i+1] for i in xrange(0, len(nums), 2) for _ in xrange(nums[i])] 11 | -------------------------------------------------------------------------------- /LEETCODE/Python/defanging-an-ip-address.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def defangIPaddr(self, address): 6 | """ 7 | :type address: str 8 | :rtype: str 9 | """ 10 | result = [] 11 | for c in address: 12 | if c == '.': 13 | result.append("[.]") 14 | else: 15 | result.append(c) 16 | return "".join(result) 17 | -------------------------------------------------------------------------------- /LEETCODE/Python/delete-node-in-a-linked-list.py: -------------------------------------------------------------------------------- 1 | # Time: O(1) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | # @param {ListNode} node 6 | # @return {void} Do not return anything, modify node in-place instead. 7 | def deleteNode(self, node): 8 | if node and node.next: 9 | node_to_delete = node.next 10 | node.val = node_to_delete.val 11 | node.next = node_to_delete.next 12 | del node_to_delete 13 | 14 | -------------------------------------------------------------------------------- /LEETCODE/Python/destination-city.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(n) 3 | 4 | import itertools 5 | 6 | 7 | class Solution(object): 8 | def destCity(self, paths): 9 | """ 10 | :type paths: List[List[str]] 11 | :rtype: str 12 | """ 13 | A, B = map(set, itertools.izip(*paths)) 14 | return (B-A).pop() 15 | -------------------------------------------------------------------------------- /LEETCODE/Python/detect-capital.py: -------------------------------------------------------------------------------- 1 | # Time: O(l) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def detectCapitalUse(self, word): 6 | """ 7 | :type word: str 8 | :rtype: bool 9 | """ 10 | return word.isupper() or word.islower() or word.istitle() 11 | 12 | -------------------------------------------------------------------------------- /LEETCODE/Python/determine-color-of-a-chessboard-square.py: -------------------------------------------------------------------------------- 1 | # Time: O(1) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def squareIsWhite(self, coordinates): 6 | """ 7 | :type coordinates: str 8 | :rtype: bool 9 | """ 10 | return (ord(coordinates[0])-ord('a'))%2 != (ord(coordinates[1])-ord('1'))%2 11 | -------------------------------------------------------------------------------- /LEETCODE/Python/distinct-subsequences-ii.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(1) 3 | 4 | import collections 5 | 6 | 7 | class Solution(object): 8 | def distinctSubseqII(self, S): 9 | """ 10 | :type S: str 11 | :rtype: int 12 | """ 13 | M = 10**9 + 7 14 | result, dp = 0, [0]*26 15 | for c in S: 16 | result, dp[ord(c)-ord('a')] = 2*result-dp[ord(c)-ord('a')]+1, result+1 17 | return result % M 18 | -------------------------------------------------------------------------------- /LEETCODE/Python/distinct-subsequences.py: -------------------------------------------------------------------------------- 1 | # Time: O(n^2) 2 | # Space: O(n) 3 | 4 | class Solution(object): 5 | # @return an integer 6 | def numDistinct(self, S, T): 7 | ways = [0 for _ in xrange(len(T) + 1)] 8 | ways[0] = 1 9 | for S_char in S: 10 | for j, T_char in reversed(list(enumerate(T))): 11 | if S_char == T_char: 12 | ways[j + 1] += ways[j] 13 | return ways[len(T)] 14 | 15 | -------------------------------------------------------------------------------- /LEETCODE/Python/distribute-candies.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(n) 3 | 4 | class Solution(object): 5 | 6 | def distributeCandies(self, candies): 7 | """ 8 | :type candies: List[int] 9 | :rtype: int 10 | """ 11 | lookup = set(candies) 12 | return min(len(lookup), len(candies)/2) 13 | 14 | -------------------------------------------------------------------------------- /LEETCODE/Python/elimination-game.py: -------------------------------------------------------------------------------- 1 | # Time: O(logn) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def lastRemaining(self, n): 6 | """ 7 | :type n: int 8 | :rtype: int 9 | """ 10 | start, step, direction = 1, 2, 1 11 | while n > 1: 12 | start += direction * (step * (n//2) - step//2) 13 | n //= 2 14 | step *= 2 15 | direction *= -1 16 | return start 17 | -------------------------------------------------------------------------------- /LEETCODE/Python/encode-number.py: -------------------------------------------------------------------------------- 1 | # Time: O(logn) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def encode(self, num): 6 | """ 7 | :type num: int 8 | :rtype: str 9 | """ 10 | result = [] 11 | while num: 12 | result.append('0' if num%2 else '1') 13 | num = (num-1)//2 14 | return "".join(reversed(result)) 15 | -------------------------------------------------------------------------------- /LEETCODE/Python/escape-the-ghosts.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def escapeGhosts(self, ghosts, target): 6 | """ 7 | :type ghosts: List[List[int]] 8 | :type target: List[int] 9 | :rtype: bool 10 | """ 11 | total = abs(target[0])+abs(target[1]) 12 | return all(total < abs(target[0]-i)+abs(target[1]-j) for i, j in ghosts) 13 | 14 | -------------------------------------------------------------------------------- /LEETCODE/Python/excel-sheet-column-number.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def titleToNumber(self, s): 6 | """ 7 | :type s: str 8 | :rtype: int 9 | """ 10 | result = 0 11 | for i in xrange(len(s)): 12 | result *= 26 13 | result += ord(s[i]) - ord('A') + 1 14 | return result 15 | 16 | 17 | -------------------------------------------------------------------------------- /LEETCODE/Python/excel-sheet-column-title.py: -------------------------------------------------------------------------------- 1 | # Time: O(logn) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def convertToTitle(self, n): 6 | """ 7 | :type n: int 8 | :rtype: str 9 | """ 10 | result = [] 11 | while n: 12 | result += chr((n-1)%26 + ord('A')) 13 | n = (n-1)//26 14 | result.reverse() 15 | return "".join(result) 16 | 17 | -------------------------------------------------------------------------------- /LEETCODE/Python/factorial-trailing-zeroes.py: -------------------------------------------------------------------------------- 1 | # Time: O(logn) = O(1) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | # @return an integer 6 | def trailingZeroes(self, n): 7 | result = 0 8 | while n > 0: 9 | result += n / 5 10 | n /= 5 11 | return result 12 | 13 | -------------------------------------------------------------------------------- /LEETCODE/Python/fair-candy-swap.py: -------------------------------------------------------------------------------- 1 | # Time: O(m + n) 2 | # Space: O(m + n) 3 | 4 | class Solution(object): 5 | def fairCandySwap(self, A, B): 6 | """ 7 | :type A: List[int] 8 | :type B: List[int] 9 | :rtype: List[int] 10 | """ 11 | diff = (sum(A)-sum(B))//2 12 | setA = set(A) 13 | for b in set(B): 14 | if diff+b in setA: 15 | return [diff+b, b] 16 | return [] 17 | 18 | -------------------------------------------------------------------------------- /LEETCODE/Python/final-prices-with-a-special-discount-in-a-shop.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(n) 3 | 4 | class Solution(object): 5 | def finalPrices(self, prices): 6 | """ 7 | :type prices: List[int] 8 | :rtype: List[int] 9 | """ 10 | stk = [] 11 | for i, p in enumerate(prices): 12 | while stk and prices[stk[-1]] >= p: 13 | prices[stk.pop()] -= p 14 | stk.append(i) 15 | return prices 16 | -------------------------------------------------------------------------------- /LEETCODE/Python/find-center-of-star-graph.py: -------------------------------------------------------------------------------- 1 | # Time: O(1) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def findCenter(self, edges): 6 | """ 7 | :type edges: List[List[int]] 8 | :rtype: int 9 | """ 10 | return edges[0][edges[0][1] in edges[1]] 11 | -------------------------------------------------------------------------------- /LEETCODE/Python/find-common-characters.py: -------------------------------------------------------------------------------- 1 | # Time: O(n * l) 2 | # Space: O(1) 3 | 4 | import collections 5 | 6 | 7 | class Solution(object): 8 | def commonChars(self, A): 9 | """ 10 | :type A: List[str] 11 | :rtype: List[str] 12 | """ 13 | result = collections.Counter(A[0]) 14 | for a in A: 15 | result &= collections.Counter(a) 16 | return list(result.elements()) 17 | -------------------------------------------------------------------------------- /LEETCODE/Python/find-lucky-integer-in-an-array.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(n) 3 | 4 | import collections 5 | 6 | 7 | class Solution(object): 8 | def findLucky(self, arr): 9 | """ 10 | :type arr: List[int] 11 | :rtype: int 12 | """ 13 | count = collections.Counter(arr) 14 | result = -1 15 | for k, v in count.iteritems(): 16 | if k == v: 17 | result = max(result, k) 18 | return result 19 | -------------------------------------------------------------------------------- /LEETCODE/Python/find-n-unique-integers-sum-up-to-zero.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def sumZero(self, n): 6 | """ 7 | :type n: int 8 | :rtype: List[int] 9 | """ 10 | return [i for i in xrange(-(n//2), n//2+1) if not (i == 0 and n%2 == 0)] 11 | -------------------------------------------------------------------------------- /LEETCODE/Python/find-permutation.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def findPermutation(self, s): 6 | """ 7 | :type s: str 8 | :rtype: List[int] 9 | """ 10 | result = [] 11 | for i in xrange(len(s)+1): 12 | if i == len(s) or s[i] == 'I': 13 | result += range(i+1, len(result), -1) 14 | return result 15 | 16 | -------------------------------------------------------------------------------- /LEETCODE/Python/find-pivot-index.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def pivotIndex(self, nums): 6 | """ 7 | :type nums: List[int] 8 | :rtype: int 9 | """ 10 | total = sum(nums) 11 | left_sum = 0 12 | for i, num in enumerate(nums): 13 | if left_sum == (total-left_sum-num): 14 | return i 15 | left_sum += num 16 | return -1 17 | 18 | 19 | -------------------------------------------------------------------------------- /LEETCODE/Python/find-smallest-letter-greater-than-target.py: -------------------------------------------------------------------------------- 1 | # Time: O(logn) 2 | # Space: O(1) 3 | 4 | import bisect 5 | 6 | 7 | class Solution(object): 8 | def nextGreatestLetter(self, letters, target): 9 | """ 10 | :type letters: List[str] 11 | :type target: str 12 | :rtype: str 13 | """ 14 | i = bisect.bisect_right(letters, target) 15 | return letters[0] if i == len(letters) else letters[i] 16 | 17 | -------------------------------------------------------------------------------- /LEETCODE/Python/find-the-derangement-of-an-array.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def findDerangement(self, n): 6 | """ 7 | :type n: int 8 | :rtype: int 9 | """ 10 | M = 1000000007 11 | mul, total = 1, 0 12 | for i in reversed(xrange(n+1)): 13 | total = (total + M + (1 if i % 2 == 0 else -1) * mul) % M 14 | mul = (mul * i) % M 15 | return total 16 | 17 | -------------------------------------------------------------------------------- /LEETCODE/Python/find-the-highest-altitude.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def largestAltitude(self, gain): 6 | """ 7 | :type gain: List[int] 8 | :rtype: int 9 | """ 10 | result = curr = 0 11 | for g in gain: 12 | curr += g 13 | result = max(result, curr) 14 | return result 15 | -------------------------------------------------------------------------------- /LEETCODE/Python/find-xor-sum-of-all-pairs-bitwise-and.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(1) 3 | 4 | import operator 5 | 6 | 7 | class Solution(object): 8 | def getXORSum(self, arr1, arr2): 9 | """ 10 | :type arr1: List[int] 11 | :type arr2: List[int] 12 | :rtype: int 13 | """ 14 | return reduce(operator.xor, arr1) & reduce(operator.xor, arr2) 15 | -------------------------------------------------------------------------------- /LEETCODE/Python/flip-columns-for-maximum-number-of-equal-rows.py: -------------------------------------------------------------------------------- 1 | # Time: O(m * n) 2 | # Space: O(m * n) 3 | 4 | import collections 5 | 6 | 7 | class Solution(object): 8 | def maxEqualRowsAfterFlips(self, matrix): 9 | """ 10 | :type matrix: List[List[int]] 11 | :rtype: int 12 | """ 13 | count = collections.Counter(tuple(x^row[0] for x in row) 14 | for row in matrix) 15 | return max(count.itervalues()) 16 | -------------------------------------------------------------------------------- /LEETCODE/Python/flip-string-to-monotone-increasing.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def minFlipsMonoIncr(self, S): 6 | """ 7 | :type S: str 8 | :rtype: int 9 | """ 10 | flip0, flip1 = 0, 0 11 | for c in S: 12 | flip0 += int(c == '1') 13 | flip1 = min(flip0, flip1 + int(c == '0')) 14 | return flip1 15 | -------------------------------------------------------------------------------- /LEETCODE/Python/flipping-an-image.py: -------------------------------------------------------------------------------- 1 | # Time: O(n^2) 2 | # Space: O(1) 3 | 4 | 5 | class Solution(object): 6 | def flipAndInvertImage(self, A): 7 | """ 8 | :type A: List[List[int]] 9 | :rtype: List[List[int]] 10 | """ 11 | for row in A: 12 | for i in xrange((len(row)+1) // 2): 13 | row[i], row[~i] = row[~i] ^ 1, row[i] ^ 1 14 | return A 15 | 16 | -------------------------------------------------------------------------------- /LEETCODE/Python/generate-a-string-with-characters-that-have-odd-counts.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def generateTheString(self, n): 6 | """ 7 | :type n: int 8 | :rtype: str 9 | """ 10 | result = ['a']*(n-1) 11 | result.append('a' if n%2 else 'b') 12 | return "".join(result) 13 | -------------------------------------------------------------------------------- /LEETCODE/Python/global-and-local-inversions.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def isIdealPermutation(self, A): 6 | """ 7 | :type A: List[int] 8 | :rtype: bool 9 | """ 10 | return all(abs(v-i) <= 1 for i,v in enumerate(A)) 11 | 12 | 13 | -------------------------------------------------------------------------------- /LEETCODE/Python/greatest-sum-divisible-by-three.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def maxSumDivThree(self, nums): 6 | """ 7 | :type nums: List[int] 8 | :rtype: int 9 | """ 10 | dp = [0, 0, 0] 11 | for num in nums: 12 | for i in [num+x for x in dp]: 13 | dp[i%3] = max(dp[i%3], i) 14 | return dp[0] 15 | -------------------------------------------------------------------------------- /LEETCODE/Python/height-checker.py: -------------------------------------------------------------------------------- 1 | # Time: O(nlogn) 2 | # Space: O(n) 3 | 4 | import itertools 5 | 6 | 7 | class Solution(object): 8 | def heightChecker(self, heights): 9 | """ 10 | :type heights: List[int] 11 | :rtype: int 12 | """ 13 | return sum(i != j for i, j in itertools.izip(heights, sorted(heights))) 14 | -------------------------------------------------------------------------------- /LEETCODE/Python/house-robber.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | # @param num, a list of integer 6 | # @return an integer 7 | def rob(self, nums): 8 | """ 9 | :type nums: List[int] 10 | :rtype: int 11 | """ 12 | last, now = 0, 0 13 | for i in nums: 14 | last, now = now, max(last + i, now) 15 | return now 16 | 17 | 18 | -------------------------------------------------------------------------------- /LEETCODE/Python/is-subsequence.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def isSubsequence(self, s, t): 6 | """ 7 | :type s: str 8 | :type t: str 9 | :rtype: bool 10 | """ 11 | if not s: 12 | return True 13 | 14 | i = 0 15 | for c in t: 16 | if c == s[i]: 17 | i += 1 18 | if i == len(s): 19 | break 20 | return i == len(s) 21 | 22 | -------------------------------------------------------------------------------- /LEETCODE/Python/jewels-and-stones.py: -------------------------------------------------------------------------------- 1 | # Time: O(m + n) 2 | # Space: O(n) 3 | 4 | class Solution(object): 5 | def numJewelsInStones(self, J, S): 6 | """ 7 | :type J: str 8 | :type S: str 9 | :rtype: int 10 | """ 11 | lookup = set(J) 12 | return sum(s in lookup for s in S) 13 | 14 | 15 | -------------------------------------------------------------------------------- /LEETCODE/Python/jump-game.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | # @param A, a list of integers 6 | # @return a boolean 7 | def canJump(self, A): 8 | reachable = 0 9 | for i, length in enumerate(A): 10 | if i > reachable: 11 | break 12 | reachable = max(reachable, i + length) 13 | return reachable >= len(A) - 1 14 | 15 | -------------------------------------------------------------------------------- /LEETCODE/Python/kids-with-the-greatest-number-of-candies.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def kidsWithCandies(self, candies, extraCandies): 6 | """ 7 | :type candies: List[int] 8 | :type extraCandies: int 9 | :rtype: List[bool] 10 | """ 11 | max_num = max(candies) 12 | return [x + extraCandies >= max_num for x in candies] 13 | -------------------------------------------------------------------------------- /LEETCODE/Python/largest-number-at-least-twice-of-others.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def dominantIndex(self, nums): 6 | """ 7 | :type nums: List[int] 8 | :rtype: int 9 | """ 10 | m = max(nums) 11 | if all(m >= 2*x for x in nums if x != m): 12 | return nums.index(m) 13 | return -1 14 | 15 | -------------------------------------------------------------------------------- /LEETCODE/Python/largest-number.py: -------------------------------------------------------------------------------- 1 | # Time: O(nlogn) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | # @param num, a list of integers 6 | # @return a string 7 | def largestNumber(self, num): 8 | num = [str(x) for x in num] 9 | num.sort(cmp=lambda x, y: cmp(y + x, x + y)) 10 | largest = ''.join(num) 11 | return largest.lstrip('0') or '0' 12 | 13 | -------------------------------------------------------------------------------- /LEETCODE/Python/largest-perimeter-triangle.py: -------------------------------------------------------------------------------- 1 | # Time: O(nlogn) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def largestPerimeter(self, A): 6 | """ 7 | :type A: List[int] 8 | :rtype: int 9 | """ 10 | A.sort() 11 | for i in reversed(xrange(len(A) - 2)): 12 | if A[i] + A[i+1] > A[i+2]: 13 | return A[i] + A[i+1] + A[i+2] 14 | return 0 15 | -------------------------------------------------------------------------------- /LEETCODE/Python/largest-substring-between-two-equal-characters.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def maxLengthBetweenEqualCharacters(self, s): 6 | """ 7 | :type s: str 8 | :rtype: int 9 | """ 10 | result, lookup = -1, {} 11 | for i, c in enumerate(s): 12 | result = max(result, i-lookup.setdefault(c, i)-1) 13 | return result 14 | -------------------------------------------------------------------------------- /LEETCODE/Python/largest-unique-number.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(n) 3 | 4 | import collections 5 | 6 | 7 | class Solution(object): 8 | def largestUniqueNumber(self, A): 9 | """ 10 | :type A: List[int] 11 | :rtype: int 12 | """ 13 | A.append(-1) 14 | return max(k for k,v in collections.Counter(A).items() if v == 1) 15 | -------------------------------------------------------------------------------- /LEETCODE/Python/last-moment-before-all-ants-fall-out-of-a-plank.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def getLastMoment(self, n, left, right): 6 | """ 7 | :type n: int 8 | :type left: List[int] 9 | :type right: List[int] 10 | :rtype: int 11 | """ 12 | return max(max(left or [0]), n-min(right or [n])) 13 | -------------------------------------------------------------------------------- /LEETCODE/Python/last-stone-weight-ii.py: -------------------------------------------------------------------------------- 1 | # Time: O(2^n) 2 | # Space: O(2^n) 3 | 4 | class Solution(object): 5 | def lastStoneWeightII(self, stones): 6 | """ 7 | :type stones: List[int] 8 | :rtype: int 9 | """ 10 | dp = {0} 11 | for stone in stones: 12 | dp |= {stone+i for i in dp} 13 | S = sum(stones) 14 | return min(abs(i-(S-i)) for i in dp) 15 | -------------------------------------------------------------------------------- /LEETCODE/Python/longest-uncommon-subsequence-i.py: -------------------------------------------------------------------------------- 1 | # Time: O(min(a, b)) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def findLUSlength(self, a, b): 6 | """ 7 | :type a: str 8 | :type b: str 9 | :rtype: int 10 | """ 11 | if a == b: 12 | return -1 13 | return max(len(a), len(b)) 14 | 15 | -------------------------------------------------------------------------------- /LEETCODE/Python/matrix-diagonal-sum.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def diagonalSum(self, mat): 6 | """ 7 | :type mat: List[List[int]] 8 | :rtype: int 9 | """ 10 | return sum(mat[i][i]+mat[~i][i] for i in xrange(len(mat))) - (mat[len(mat)//2][len(mat)//2] if len(mat)%2 == 1 else 0) 11 | -------------------------------------------------------------------------------- /LEETCODE/Python/max-consecutive-ones.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def findMaxConsecutiveOnes(self, nums): 6 | """ 7 | :type nums: List[int] 8 | :rtype: int 9 | """ 10 | result, local_max = 0, 0 11 | for n in nums: 12 | local_max = (local_max + 1 if n else 0) 13 | result = max(result, local_max) 14 | return result 15 | 16 | -------------------------------------------------------------------------------- /LEETCODE/Python/maximum-absolute-sum-of-any-subarray.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def maxAbsoluteSum(self, nums): 6 | """ 7 | :type nums: List[int] 8 | :rtype: int 9 | """ 10 | curr = mx = mn = 0 11 | for num in nums: 12 | curr += num 13 | mx = max(mx, curr) 14 | mn = min(mn, curr) 15 | return mx-mn 16 | -------------------------------------------------------------------------------- /LEETCODE/Python/maximum-ascending-subarray-sum.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def maxAscendingSum(self, nums): 6 | """ 7 | :type nums: List[int] 8 | :rtype: int 9 | """ 10 | result = curr = 0 11 | for i in xrange(len(nums)): 12 | if not (i and nums[i-1] < nums[i]): 13 | curr = 0 14 | curr += nums[i] 15 | result = max(result, curr) 16 | return result 17 | -------------------------------------------------------------------------------- /LEETCODE/Python/maximum-average-subarray-i.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def findMaxAverage(self, nums, k): 6 | """ 7 | :type nums: List[int] 8 | :type k: int 9 | :rtype: float 10 | """ 11 | result = total = sum(nums[:k]) 12 | for i in xrange(k, len(nums)): 13 | total += nums[i] - nums[i-k] 14 | result = max(result, total) 15 | return float(result) / k 16 | 17 | -------------------------------------------------------------------------------- /LEETCODE/Python/maximum-element-after-decreasing-and-rearranging.py: -------------------------------------------------------------------------------- 1 | # Time: O(nlogn) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def maximumElementAfterDecrementingAndRearranging(self, arr): 6 | """ 7 | :type arr: List[int] 8 | :rtype: int 9 | """ 10 | arr.sort() 11 | result = 1 12 | for i in xrange(1, len(arr)): 13 | result = min(result+1, arr[i]) 14 | return result 15 | -------------------------------------------------------------------------------- /LEETCODE/Python/maximum-ice-cream-bars.py: -------------------------------------------------------------------------------- 1 | # Time: O(nlogn) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def maxIceCream(self, costs, coins): 6 | """ 7 | :type costs: List[int] 8 | :type coins: int 9 | :rtype: int 10 | """ 11 | costs.sort() 12 | for i, c in enumerate(costs): 13 | coins -= c 14 | if coins < 0: 15 | return i 16 | return len(costs) 17 | -------------------------------------------------------------------------------- /LEETCODE/Python/maximum-nesting-depth-of-the-parentheses.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def maxDepth(self, s): 6 | """ 7 | :type s: str 8 | :rtype: int 9 | """ 10 | result = curr = 0 11 | for c in s: 12 | if c == '(': 13 | curr += 1 14 | result = max(result, curr) 15 | elif c == ')': 16 | curr -= 1 17 | return result 18 | -------------------------------------------------------------------------------- /LEETCODE/Python/maximum-number-of-balloons.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(1) 3 | 4 | import collections 5 | 6 | 7 | class Solution(object): 8 | def maxNumberOfBalloons(self, text): 9 | """ 10 | :type text: str 11 | :rtype: int 12 | """ 13 | TARGET = "balloon" 14 | source_count = collections.Counter(text) 15 | target_count = collections.Counter(TARGET) 16 | return min(source_count[c]//target_count[c] for c in target_count.iterkeys()) 17 | -------------------------------------------------------------------------------- /LEETCODE/Python/maximum-number-of-coins-you-can-get.py: -------------------------------------------------------------------------------- 1 | # Time: O(nlogn) 2 | # Space: O(1) 3 | 4 | import itertools 5 | 6 | 7 | class Solution(object): 8 | def maxCoins(self, piles): 9 | """ 10 | :type piles: List[int] 11 | :rtype: int 12 | """ 13 | piles.sort() 14 | return sum(itertools.islice(piles, len(piles)//3, len(piles), 2)) 15 | -------------------------------------------------------------------------------- /LEETCODE/Python/maximum-number-of-consecutive-values-you-can-make.py: -------------------------------------------------------------------------------- 1 | # Time: O(nlogn) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def getMaximumConsecutive(self, coins): 6 | """ 7 | :type coins: List[int] 8 | :rtype: int 9 | """ 10 | coins.sort() 11 | result = 1 12 | for c in coins: 13 | if c > result: 14 | break 15 | result += c 16 | return result 17 | -------------------------------------------------------------------------------- /LEETCODE/Python/maximum-product-of-two-elements-in-an-array.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def maxProduct(self, nums): 6 | """ 7 | :type nums: List[int] 8 | :rtype: int 9 | """ 10 | m1 = m2 = 0 11 | for num in nums: 12 | if num > m1: 13 | m1, m2 = num, m1 14 | elif num > m2: 15 | m2 = num 16 | return (m1-1)*(m2-1) 17 | -------------------------------------------------------------------------------- /LEETCODE/Python/maximum-subarray-sum-with-one-deletion.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def maximumSum(self, arr): 3 | """ 4 | :type arr: List[int] 5 | :rtype: int 6 | """ 7 | result, prev, curr = float("-inf"), float("-inf"), float("-inf") 8 | for x in arr: 9 | curr = max(prev, curr+x, x) 10 | result = max(result, curr) 11 | prev = max(prev+x, x) 12 | return result 13 | -------------------------------------------------------------------------------- /LEETCODE/Python/maximum-subarray.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def maxSubArray(self, nums): 6 | """ 7 | :type nums: List[int] 8 | :rtype: int 9 | """ 10 | result, curr = float("-inf"), float("-inf") 11 | for x in nums: 12 | curr = max(curr+x, x) 13 | result = max(result, curr) 14 | return result 15 | -------------------------------------------------------------------------------- /LEETCODE/Python/maximum-xor-for-each-query.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def getMaximumXor(self, nums, maximumBit): 6 | """ 7 | :type nums: List[int] 8 | :type maximumBit: int 9 | :rtype: List[int] 10 | """ 11 | result = [0]*len(nums) 12 | mask = 2**maximumBit-1 13 | for i in xrange(len(nums)): 14 | mask ^= nums[i] 15 | result[-1-i] = mask 16 | return result 17 | -------------------------------------------------------------------------------- /LEETCODE/Python/meeting-rooms.py: -------------------------------------------------------------------------------- 1 | # Time: O(nlogn) 2 | # Space: O(n) 3 | 4 | class Solution(object): 5 | def canAttendMeetings(self, intervals): 6 | """ 7 | :type intervals: List[List[int]] 8 | :rtype: bool 9 | """ 10 | intervals.sort(key=lambda x: x[0]) 11 | 12 | for i in xrange(1, len(intervals)): 13 | if intervals[i][0] < intervals[i-1][1]: 14 | return False 15 | return True 16 | -------------------------------------------------------------------------------- /LEETCODE/Python/min-cost-climbing-stairs.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def minCostClimbingStairs(self, cost): 6 | """ 7 | :type cost: List[int] 8 | :rtype: int 9 | """ 10 | dp = [0] * 3 11 | for i in reversed(xrange(len(cost))): 12 | dp[i%3] = cost[i] + min(dp[(i+1)%3], dp[(i+2)%3]) 13 | return min(dp[0], dp[1]) 14 | 15 | -------------------------------------------------------------------------------- /LEETCODE/Python/minimum-add-to-make-parentheses-valid.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def minAddToMakeValid(self, S): 6 | """ 7 | :type S: str 8 | :rtype: int 9 | """ 10 | add, bal, = 0, 0 11 | for c in S: 12 | bal += 1 if c == '(' else -1 13 | if bal == -1: 14 | add += 1 15 | bal += 1 16 | return add + bal 17 | -------------------------------------------------------------------------------- /LEETCODE/Python/minimum-changes-to-make-alternating-binary-string.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def minOperations(self, s): 6 | """ 7 | :type s: str 8 | :rtype: int 9 | """ 10 | cnt = sum(int(c) == i%2 for i, c in enumerate(s)) 11 | return min(cnt, len(s)-cnt) 12 | -------------------------------------------------------------------------------- /LEETCODE/Python/minimum-deletions-to-make-string-balanced.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def minimumDeletions(self, s): 6 | """ 7 | :type s: str 8 | :rtype: int 9 | """ 10 | result = b_cnt = 0 11 | for c in s: 12 | if c == 'b': 13 | b_cnt += 1 14 | elif b_cnt: 15 | b_cnt -= 1 16 | result += 1 17 | return result 18 | -------------------------------------------------------------------------------- /LEETCODE/Python/minimum-elements-to-add-to-form-a-given-sum.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def minElements(self, nums, limit, goal): 6 | """ 7 | :type nums: List[int] 8 | :type limit: int 9 | :type goal: int 10 | :rtype: int 11 | """ 12 | return (abs(sum(nums)-goal) + (limit-1))//limit 13 | -------------------------------------------------------------------------------- /LEETCODE/Python/minimum-falling-path-sum.py: -------------------------------------------------------------------------------- 1 | # Time: O(n^2) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def minFallingPathSum(self, A): 6 | """ 7 | :type A: List[List[int]] 8 | :rtype: int 9 | """ 10 | for i in xrange(1, len(A)): 11 | for j in xrange(len(A[i])): 12 | A[i][j] += min(A[i-1][max(j-1, 0):j+2]) 13 | return min(A[-1]) 14 | -------------------------------------------------------------------------------- /LEETCODE/Python/minimum-moves-to-equal-array-elements.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def minMoves(self, nums): 6 | """ 7 | :type nums: List[int] 8 | :rtype: int 9 | """ 10 | return sum(nums) - len(nums) * min(nums) 11 | 12 | -------------------------------------------------------------------------------- /LEETCODE/Python/minimum-number-of-steps-to-make-two-strings-anagram.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(1) 3 | 4 | import collections 5 | 6 | 7 | class Solution(object): 8 | def minSteps(self, s, t): 9 | """ 10 | :type s: str 11 | :type t: str 12 | :rtype: int 13 | """ 14 | diff = collections.Counter(s) - collections.Counter(t) 15 | return sum(diff.itervalues()) 16 | -------------------------------------------------------------------------------- /LEETCODE/Python/minimum-operations-to-make-array-equal.py: -------------------------------------------------------------------------------- 1 | # Time: O(1) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def minOperations(self, n): 6 | """ 7 | :type n: int 8 | :rtype: int 9 | """ 10 | # total = (2i+1 for i in xrange(n)) = n^2 11 | # left_half_total = (2i+1 for i in xrange(n//2)) = (n//2)^2 12 | # result = (n//2) * (total//n) - left_half_total = (n//2)*(n-n//2) = (n//2)*((n+1)//2) 13 | return (n//2)*((n+1)//2) 14 | -------------------------------------------------------------------------------- /LEETCODE/Python/minimum-time-difference.py: -------------------------------------------------------------------------------- 1 | # Time: O(nlogn) 2 | # Space: O(n) 3 | 4 | class Solution(object): 5 | def findMinDifference(self, timePoints): 6 | """ 7 | :type timePoints: List[str] 8 | :rtype: int 9 | """ 10 | minutes = map(lambda x: int(x[:2]) * 60 + int(x[3:]), timePoints) 11 | minutes.sort() 12 | return min((y - x) % (24 * 60) \ 13 | for x, y in zip(minutes, minutes[1:] + minutes[:1])) 14 | 15 | -------------------------------------------------------------------------------- /LEETCODE/Python/minimum-time-visiting-all-points.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def minTimeToVisitAllPoints(self, points): 6 | """ 7 | :type points: List[List[int]] 8 | :rtype: int 9 | """ 10 | return sum(max(abs(points[i+1][0] - points[i][0]), 11 | abs(points[i+1][1] - points[i][1])) 12 | for i in xrange(len(points)-1)) 13 | -------------------------------------------------------------------------------- /LEETCODE/Python/minimum-value-to-get-positive-step-by-step-sum.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def minStartValue(self, nums): 6 | """ 7 | :type nums: List[int] 8 | :rtype: int 9 | """ 10 | min_prefix, prefix = 0, 0 11 | for num in nums: 12 | prefix += num 13 | min_prefix = min(min_prefix, prefix) 14 | return 1-min_prefix 15 | -------------------------------------------------------------------------------- /LEETCODE/Python/monotonic-array.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def isMonotonic(self, A): 6 | """ 7 | :type A: List[int] 8 | :rtype: bool 9 | """ 10 | inc, dec = False, False 11 | for i in xrange(len(A)-1): 12 | if A[i] < A[i+1]: 13 | inc = True 14 | elif A[i] > A[i+1]: 15 | dec = True 16 | return not inc or not dec 17 | 18 | -------------------------------------------------------------------------------- /LEETCODE/Python/most-visited-sector-in-a-circular-track.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def mostVisited(self, n, rounds): 6 | """ 7 | :type n: int 8 | :type rounds: List[int] 9 | :rtype: List[int] 10 | """ 11 | return range(rounds[0], rounds[-1]+1) or \ 12 | range(1, rounds[-1]+1) + range(rounds[0], n+1) 13 | -------------------------------------------------------------------------------- /LEETCODE/Python/n-repeated-element-in-size-2n-array.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def repeatedNTimes(self, A): 6 | """ 7 | :type A: List[int] 8 | :rtype: int 9 | """ 10 | for i in xrange(2, len(A)): 11 | if A[i-1] == A[i] or A[i-2] == A[i]: 12 | return A[i] 13 | return A[0] 14 | -------------------------------------------------------------------------------- /LEETCODE/Python/nim-game.py: -------------------------------------------------------------------------------- 1 | # Time: O(1) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def canWinNim(self, n): 6 | """ 7 | :type n: int 8 | :rtype: bool 9 | """ 10 | return n % 4 != 0 11 | 12 | -------------------------------------------------------------------------------- /LEETCODE/Python/number-of-days-in-a-month.py: -------------------------------------------------------------------------------- 1 | # Time: O(1) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def numberOfDays(self, Y, M): 6 | """ 7 | :type Y: int 8 | :type M: int 9 | :rtype: int 10 | """ 11 | leap = 1 if ((Y % 4 == 0) and (Y % 100 != 0)) or (Y % 400 == 0) else 0 12 | return (28+leap if (M == 2) else 31-(M-1)%7%2) 13 | 14 | -------------------------------------------------------------------------------- /LEETCODE/Python/number-of-equivalent-domino-pairs.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(n) 3 | 4 | import collections 5 | 6 | 7 | class Solution(object): 8 | def numEquivDominoPairs(self, dominoes): 9 | """ 10 | :type dominoes: List[List[int]] 11 | :rtype: int 12 | """ 13 | counter = collections.Counter((min(x), max(x)) for x in dominoes) 14 | return sum(v*(v-1)//2 for v in counter.itervalues()) 15 | -------------------------------------------------------------------------------- /LEETCODE/Python/number-of-good-pairs.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(n) 3 | 4 | import collections 5 | 6 | 7 | class Solution(object): 8 | def numIdenticalPairs(self, nums): 9 | """ 10 | :type nums: List[int] 11 | :rtype: int 12 | """ 13 | return sum(c*(c-1)//2 for c in collections.Counter(nums).itervalues()) 14 | -------------------------------------------------------------------------------- /LEETCODE/Python/number-of-steps-to-reduce-a-number-to-zero.py: -------------------------------------------------------------------------------- 1 | # Time: O(logn) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def numberOfSteps (self, num): 6 | """ 7 | :type num: int 8 | :rtype: int 9 | """ 10 | result = 0 11 | while num: 12 | result += 2 if num%2 else 1 13 | num //= 2 14 | return max(result-1, 0) 15 | -------------------------------------------------------------------------------- /LEETCODE/Python/number-of-students-doing-homework-at-a-given-time.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(1) 3 | 4 | import itertools 5 | 6 | 7 | class Solution(object): 8 | def busyStudent(self, startTime, endTime, queryTime): 9 | """ 10 | :type startTime: List[int] 11 | :type endTime: List[int] 12 | :type queryTime: int 13 | :rtype: int 14 | """ 15 | return sum(s <= queryTime <= e for s, e in itertools.izip(startTime, endTime)) 16 | -------------------------------------------------------------------------------- /LEETCODE/Python/number-of-sub-arrays-with-odd-sum.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def numOfSubarrays(self, arr): 6 | """ 7 | :type arr: List[int] 8 | :rtype: int 9 | """ 10 | MOD = 10**9+7 11 | result, accu = 0, 0 12 | dp = [1, 0] 13 | for x in arr: 14 | accu ^= x&1 15 | dp[accu] += 1 16 | result = (result + dp[accu^1]) % MOD 17 | return result 18 | -------------------------------------------------------------------------------- /LEETCODE/Python/number-of-substrings-with-only-1s.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def numSub(self, s): 6 | """ 7 | :type s: str 8 | :rtype: int 9 | """ 10 | MOD = 10**9+7 11 | result, count = 0, 0 12 | for c in s: 13 | count = count+1 if c == '1' else 0 14 | result = (result+count)%MOD 15 | return result 16 | -------------------------------------------------------------------------------- /LEETCODE/Python/number-of-valid-subarrays.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(n) 3 | 4 | class Solution(object): 5 | def validSubarrays(self, nums): 6 | """ 7 | :type nums: List[int] 8 | :rtype: int 9 | """ 10 | result = 0 11 | s = [] 12 | for num in nums: 13 | while s and s[-1] > num: 14 | s.pop() 15 | s.append(num); 16 | result += len(s) 17 | return result 18 | -------------------------------------------------------------------------------- /LEETCODE/Python/numbers-with-same-consecutive-differences.py: -------------------------------------------------------------------------------- 1 | # Time: O(2^n) 2 | # Space: O(2^n) 3 | 4 | class Solution(object): 5 | def numsSameConsecDiff(self, N, K): 6 | """ 7 | :type N: int 8 | :type K: int 9 | :rtype: List[int] 10 | """ 11 | curr = range(10) 12 | for i in xrange(N-1): 13 | curr = [x*10 + y for x in curr for y in set([x%10 + K, x%10 - K]) 14 | if x and 0 <= y < 10] 15 | return curr 16 | -------------------------------------------------------------------------------- /LEETCODE/Python/online-stock-span.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(n) 3 | 4 | class StockSpanner(object): 5 | 6 | def __init__(self): 7 | self.__s = [] 8 | 9 | def next(self, price): 10 | """ 11 | :type price: int 12 | :rtype: int 13 | """ 14 | result = 1 15 | while self.__s and self.__s[-1][0] <= price: 16 | result += self.__s.pop()[1] 17 | self.__s.append([price, result]) 18 | return result 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /LEETCODE/Python/orderly-queue.py: -------------------------------------------------------------------------------- 1 | # Time: O(n^2) 2 | # Space: O(n) 3 | 4 | class Solution(object): 5 | def orderlyQueue(self, S, K): 6 | """ 7 | :type S: str 8 | :type K: int 9 | :rtype: str 10 | """ 11 | if K == 1: 12 | return min(S[i:] + S[:i] for i in xrange(len(S))) 13 | return "".join(sorted(S)) 14 | 15 | -------------------------------------------------------------------------------- /LEETCODE/Python/output-contest-matches.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(n) 3 | 4 | class Solution(object): 5 | def findContestMatch(self, n): 6 | """ 7 | :type n: int 8 | :rtype: str 9 | """ 10 | matches = map(str, range(1, n+1)) 11 | while len(matches)/2: 12 | matches = ["({},{})".format(matches[i], matches[-i-1]) for i in xrange(len(matches)/2)] 13 | return matches[0] 14 | 15 | 16 | -------------------------------------------------------------------------------- /LEETCODE/Python/pairs-of-songs-with-total-durations-divisible-by-60.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(1) 3 | 4 | import collections 5 | 6 | 7 | class Solution(object): 8 | def numPairsDivisibleBy60(self, time): 9 | """ 10 | :type time: List[int] 11 | :rtype: int 12 | """ 13 | result = 0 14 | count = collections.Counter() 15 | for t in time: 16 | result += count[-t%60] 17 | count[t%60] += 1 18 | return result 19 | -------------------------------------------------------------------------------- /LEETCODE/Python/palindrome-number.py: -------------------------------------------------------------------------------- 1 | # Time: O(1) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | # @return a boolean 6 | def isPalindrome(self, x): 7 | if x < 0: 8 | return False 9 | copy, reverse = x, 0 10 | 11 | while copy: 12 | reverse *= 10 13 | reverse += copy % 10 14 | copy //= 10 15 | 16 | return x == reverse 17 | 18 | -------------------------------------------------------------------------------- /LEETCODE/Python/palindrome-permutation.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(1) 3 | 4 | import collections 5 | 6 | 7 | class Solution(object): 8 | def canPermutePalindrome(self, s): 9 | """ 10 | :type s: str 11 | :rtype: bool 12 | """ 13 | return sum(v % 2 for v in collections.Counter(s).values()) < 2 14 | 15 | -------------------------------------------------------------------------------- /LEETCODE/Python/partitioning-into-minimum-number-of-deci-binary-numbers.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def minPartitions(self, n): 6 | """ 7 | :type n: str 8 | :rtype: int 9 | """ 10 | return int(max(n)) 11 | -------------------------------------------------------------------------------- /LEETCODE/Python/perfect-squares.py: -------------------------------------------------------------------------------- 1 | # Time: O(n * sqrt(n)) 2 | # Space: O(n) 3 | 4 | class Solution(object): 5 | _num = [0] 6 | def numSquares(self, n): 7 | """ 8 | :type n: int 9 | :rtype: int 10 | """ 11 | num = self._num 12 | while len(num) <= n: 13 | num += min(num[-i*i] for i in xrange(1, int(len(num)**0.5+1))) + 1, 14 | return num[n] 15 | 16 | -------------------------------------------------------------------------------- /LEETCODE/Python/play-with-chips.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def minCostToMoveChips(self, chips): 6 | """ 7 | :type chips: List[int] 8 | :rtype: int 9 | """ 10 | count = [0]*2 11 | for p in chips: 12 | count[p%2] += 1 13 | return min(count) 14 | -------------------------------------------------------------------------------- /LEETCODE/Python/poor-pigs.py: -------------------------------------------------------------------------------- 1 | # Time: O(1) 2 | # Space: O(1) 3 | 4 | import math 5 | 6 | 7 | class Solution(object): 8 | def poorPigs(self, buckets, minutesToDie, minutesToTest): 9 | """ 10 | :type buckets: int 11 | :type minutesToDie: int 12 | :type minutesToTest: int 13 | :rtype: int 14 | """ 15 | return int(math.ceil(math.log(buckets) / math.log(minutesToTest / minutesToDie + 1))) 16 | 17 | -------------------------------------------------------------------------------- /LEETCODE/Python/power-of-two.py: -------------------------------------------------------------------------------- 1 | # Time: O(1) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | # @param {integer} n 6 | # @return {boolean} 7 | def isPowerOfTwo(self, n): 8 | return n > 0 and (n & (n - 1)) == 0 9 | 10 | 11 | class Solution2(object): 12 | # @param {integer} n 13 | # @return {boolean} 14 | def isPowerOfTwo(self, n): 15 | return n > 0 and (n & ~-n) == 0 16 | 17 | -------------------------------------------------------------------------------- /LEETCODE/Python/print-words-vertically.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(n) 3 | 4 | import itertools 5 | 6 | 7 | class Solution(object): 8 | def printVertically(self, s): 9 | """ 10 | :type s: str 11 | :rtype: List[str] 12 | """ 13 | return ["".join(c).rstrip() for c in itertools.izip_longest(*s.split(), fillvalue=' ')] 14 | -------------------------------------------------------------------------------- /LEETCODE/Python/rabbits-in-forest.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(n) 3 | 4 | import collections 5 | 6 | 7 | class Solution(object): 8 | def numRabbits(self, answers): 9 | """ 10 | :type answers: List[int] 11 | :rtype: int 12 | """ 13 | count = collections.Counter(answers) 14 | return sum((((k+1)+v-1)//(k+1))*(k+1) for k, v in count.iteritems()) 15 | 16 | -------------------------------------------------------------------------------- /LEETCODE/Python/range-addition-ii.py: -------------------------------------------------------------------------------- 1 | # Time: O(p), p is the number of ops 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def maxCount(self, m, n, ops): 6 | """ 7 | :type m: int 8 | :type n: int 9 | :type ops: List[List[int]] 10 | :rtype: int 11 | """ 12 | for op in ops: 13 | m = min(m, op[0]) 14 | n = min(n, op[1]) 15 | return m*n 16 | 17 | -------------------------------------------------------------------------------- /LEETCODE/Python/rank-transform-of-an-array.py: -------------------------------------------------------------------------------- 1 | # Time: O(nlogn) 2 | # Space: O(n) 3 | 4 | class Solution(object): 5 | def arrayRankTransform(self, arr): 6 | """ 7 | :type arr: List[int] 8 | :rtype: List[int] 9 | """ 10 | return map({x: i+1 for i, x in enumerate(sorted(set(arr)))}.get, arr) 11 | -------------------------------------------------------------------------------- /LEETCODE/Python/rearrange-words-in-a-sentence.py: -------------------------------------------------------------------------------- 1 | # Time: O(nlogn) 2 | # Space: O(n) 3 | 4 | class Solution(object): 5 | def arrangeWords(self, text): 6 | """ 7 | :type text: str 8 | :rtype: str 9 | """ 10 | result = text.split() 11 | result[0] = result[0].lower() 12 | result.sort(key=len) 13 | result[0] = result[0].title() 14 | return " ".join(result) 15 | -------------------------------------------------------------------------------- /LEETCODE/Python/relative-ranks.py: -------------------------------------------------------------------------------- 1 | # Time: O(nlogn) 2 | # Space: O(n) 3 | 4 | class Solution(object): 5 | def findRelativeRanks(self, nums): 6 | """ 7 | :type nums: List[int] 8 | :rtype: List[str] 9 | """ 10 | sorted_nums = sorted(nums)[::-1] 11 | ranks = ["Gold Medal", "Silver Medal", "Bronze Medal"] + map(str, range(4, len(nums) + 1)) 12 | return map(dict(zip(sorted_nums, ranks)).get, nums) 13 | 14 | -------------------------------------------------------------------------------- /LEETCODE/Python/relative-sort-array.py: -------------------------------------------------------------------------------- 1 | # Time: O(nlogn) 2 | # Space: O(n) 3 | 4 | class Solution(object): 5 | def relativeSortArray(self, arr1, arr2): 6 | """ 7 | :type arr1: List[int] 8 | :type arr2: List[int] 9 | :rtype: List[int] 10 | """ 11 | lookup = {v: i for i, v in enumerate(arr2)} 12 | return sorted(arr1, key=lambda i: lookup.get(i, len(arr2)+i)) 13 | -------------------------------------------------------------------------------- /LEETCODE/Python/remove-9.py: -------------------------------------------------------------------------------- 1 | # Time: O(logn) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def newInteger(self, n): 6 | """ 7 | :type n: int 8 | :rtype: int 9 | """ 10 | result, base = 0, 1 11 | while n > 0: 12 | result += (n%9) * base 13 | n /= 9 14 | base *= 10 15 | return result 16 | 17 | -------------------------------------------------------------------------------- /LEETCODE/Python/remove-all-adjacent-duplicates-in-string.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(n) 3 | 4 | class Solution(object): 5 | def removeDuplicates(self, S): 6 | """ 7 | :type S: str 8 | :rtype: str 9 | """ 10 | result = [] 11 | for c in S: 12 | if result and result[-1] == c: 13 | result.pop() 14 | else: 15 | result.append(c) 16 | return "".join(result) 17 | -------------------------------------------------------------------------------- /LEETCODE/Python/remove-duplicates-from-sorted-array.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | # @param a list of integers 6 | # @return an integer 7 | def removeDuplicates(self, A): 8 | if not A: 9 | return 0 10 | 11 | last = 0 12 | for i in xrange(len(A)): 13 | if A[last] != A[i]: 14 | last += 1 15 | A[last] = A[i] 16 | return last + 1 17 | 18 | -------------------------------------------------------------------------------- /LEETCODE/Python/remove-interval.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def removeInterval(self, intervals, toBeRemoved): 6 | """ 7 | :type intervals: List[List[int]] 8 | :type toBeRemoved: List[int] 9 | :rtype: List[List[int]] 10 | """ 11 | A, B = toBeRemoved 12 | return [[x, y] for a, b in intervals 13 | for x, y in ((a, min(A, b)), (max(a, B), b)) 14 | if x < y] 15 | -------------------------------------------------------------------------------- /LEETCODE/Python/remove-palindromic-subsequences.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def removePalindromeSub(self, s): 6 | """ 7 | :type s: str 8 | :rtype: int 9 | """ 10 | def is_palindrome(s): 11 | for i in xrange(len(s)//2): 12 | if s[i] != s[-1-i]: 13 | return False 14 | return True 15 | 16 | return 2 - is_palindrome(s) - (s == "") 17 | -------------------------------------------------------------------------------- /LEETCODE/Python/remove-vowels-from-a-string.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def removeVowels(self, S): 6 | """ 7 | :type S: str 8 | :rtype: str 9 | """ 10 | lookup = set("aeiou") 11 | return "".join(c for c in S if c not in lookup) 12 | -------------------------------------------------------------------------------- /LEETCODE/Python/reordered-power-of-2.py: -------------------------------------------------------------------------------- 1 | # Time: O((logn)^2) = O(1) due to n is a 32-bit number 2 | # Space: O(logn) = O(1) 3 | 4 | import collections 5 | 6 | 7 | class Solution(object): 8 | def reorderedPowerOf2(self, N): 9 | """ 10 | :type N: int 11 | :rtype: bool 12 | """ 13 | count = collections.Counter(str(N)) 14 | return any(count == collections.Counter(str(1 << i)) 15 | for i in xrange(31)) 16 | 17 | -------------------------------------------------------------------------------- /LEETCODE/Python/replace-all-digits-with-characters.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def replaceDigits(self, s): 6 | """ 7 | :type s: str 8 | :rtype: str 9 | """ 10 | return "".join(chr(ord(s[i-1])+int(s[i])) if i%2 else s[i] for i in xrange(len(s))) 11 | -------------------------------------------------------------------------------- /LEETCODE/Python/replace-elements-with-greatest-element-on-right-side.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def replaceElements(self, arr): 6 | """ 7 | :type arr: List[int] 8 | :rtype: List[int] 9 | """ 10 | curr_max = -1 11 | for i in reversed(xrange(len(arr))): 12 | arr[i], curr_max = curr_max, max(curr_max, arr[i]) 13 | return arr 14 | -------------------------------------------------------------------------------- /LEETCODE/Python/reverse-string-ii.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def reverseStr(self, s, k): 6 | """ 7 | :type s: str 8 | :type k: int 9 | :rtype: str 10 | """ 11 | s = list(s) 12 | for i in xrange(0, len(s), 2*k): 13 | s[i:i+k] = reversed(s[i:i+k]) 14 | return "".join(s) 15 | 16 | -------------------------------------------------------------------------------- /LEETCODE/Python/reverse-words-in-a-string.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(n) 3 | 4 | class Solution(object): 5 | # @param s, a string 6 | # @return a string 7 | def reverseWords(self, s): 8 | return ' '.join(reversed(s.split())) 9 | 10 | -------------------------------------------------------------------------------- /LEETCODE/Python/richest-customer-wealth.py: -------------------------------------------------------------------------------- 1 | # Time: O(m * n) 2 | # Space: O(1) 3 | 4 | import itertools 5 | 6 | 7 | class Solution(object): 8 | def maximumWealth(self, accounts): 9 | """ 10 | :type accounts: List[List[int]] 11 | :rtype: int 12 | """ 13 | return max(itertools.imap(sum, accounts)) 14 | -------------------------------------------------------------------------------- /LEETCODE/Python/running-sum-of-1d-array.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def runningSum(self, nums): 6 | """ 7 | :type nums: List[int] 8 | :rtype: List[int] 9 | """ 10 | for i in xrange(len(nums)-1): 11 | nums[i+1] += nums[i] 12 | return nums 13 | -------------------------------------------------------------------------------- /LEETCODE/Python/sign-of-the-product-of-an-array.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def arraySign(self, nums): 6 | """ 7 | :type nums: List[int] 8 | :rtype: int 9 | """ 10 | flag = 0 11 | for x in nums: 12 | if not x: 13 | return 0 14 | if x < 0: 15 | flag ^= 1 16 | return -1 if flag else 1 17 | -------------------------------------------------------------------------------- /LEETCODE/Python/single-number.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(1) 3 | 4 | import operator 5 | from functools import reduce 6 | 7 | 8 | class Solution(object): 9 | """ 10 | :type nums: List[int] 11 | :rtype: int 12 | """ 13 | def singleNumber(self, A): 14 | return reduce(operator.xor, A) 15 | 16 | -------------------------------------------------------------------------------- /LEETCODE/Python/single-row-keyboard.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def calculateTime(self, keyboard, word): 6 | """ 7 | :type keyboard: str 8 | :type word: str 9 | :rtype: int 10 | """ 11 | lookup = {c:i for i, c in enumerate(keyboard)} 12 | result, prev = 0, 0 13 | for c in word: 14 | result += abs(lookup[c]-prev) 15 | prev = lookup[c] 16 | return result 17 | -------------------------------------------------------------------------------- /LEETCODE/Python/smallest-range-i.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def smallestRangeI(self, A, K): 6 | """ 7 | :type A: List[int] 8 | :type K: int 9 | :rtype: int 10 | """ 11 | return max(0, max(A) - min(A) - 2*K) 12 | 13 | -------------------------------------------------------------------------------- /LEETCODE/Python/sort-array-by-increasing-frequency.py: -------------------------------------------------------------------------------- 1 | # Time: O(nlogn) 2 | # Space: O(n) 3 | 4 | import collections 5 | 6 | 7 | class Solution(object): 8 | def frequencySort(self, nums): 9 | """ 10 | :type nums: List[int] 11 | :rtype: List[int] 12 | """ 13 | count = collections.Counter(nums) 14 | return sorted(nums, key=lambda x: (count[x], -x)) 15 | -------------------------------------------------------------------------------- /LEETCODE/Python/sort-array-by-parity-ii.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def sortArrayByParityII(self, A): 6 | """ 7 | :type A: List[int] 8 | :rtype: List[int] 9 | """ 10 | j = 1 11 | for i in xrange(0, len(A), 2): 12 | if A[i] % 2: 13 | while A[j] % 2: 14 | j += 2 15 | A[i], A[j] = A[j], A[i] 16 | return A 17 | -------------------------------------------------------------------------------- /LEETCODE/Python/sort-array-by-parity.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def sortArrayByParity(self, A): 6 | """ 7 | :type A: List[int] 8 | :rtype: List[int] 9 | """ 10 | i = 0 11 | for j in xrange(len(A)): 12 | if A[j] % 2 == 0: 13 | A[i], A[j] = A[j], A[i] 14 | i += 1 15 | return A 16 | 17 | -------------------------------------------------------------------------------- /LEETCODE/Python/split-a-string-in-balanced-strings.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def balancedStringSplit(self, s): 6 | """ 7 | :type s: str 8 | :rtype: int 9 | """ 10 | result, count = 0, 0 11 | for c in s: 12 | count += 1 if c == 'L' else -1 13 | if count == 0: 14 | result += 1 15 | return result 16 | -------------------------------------------------------------------------------- /LEETCODE/Python/stone-game-vi.py: -------------------------------------------------------------------------------- 1 | # Time: O(nlogn) 2 | # Space: O(n) 3 | 4 | class Solution(object): 5 | def stoneGameVI(self, aliceValues, bobValues): 6 | """ 7 | :type aliceValues: List[int] 8 | :type bobValues: List[int] 9 | :rtype: int 10 | """ 11 | sorted_vals = sorted(((a, b) for a, b in zip(aliceValues, bobValues)), key=sum, reverse=True) 12 | return cmp(sum(a for a, _ in sorted_vals[::2]), sum(b for _, b in sorted_vals[1::2])) 13 | -------------------------------------------------------------------------------- /LEETCODE/Python/stone-game-viii.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def stoneGameVIII(self, stones): 6 | """ 7 | :type stones: List[int] 8 | :rtype: int 9 | """ 10 | for i in xrange(len(stones)-1): 11 | stones[i+1] += stones[i] 12 | return reduce(lambda curr, i: max(curr, stones[i]-curr), reversed(xrange(1, len(stones)-1)), stones[-1]) 13 | -------------------------------------------------------------------------------- /LEETCODE/Python/sum-of-digits-in-base-k.py: -------------------------------------------------------------------------------- 1 | # Time: O(logn) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def sumBase(self, n, k): 6 | """ 7 | :type n: int 8 | :type k: int 9 | :rtype: int 10 | """ 11 | result = 0 12 | while n: 13 | n, r = divmod(n, k) 14 | result += r 15 | return result 16 | -------------------------------------------------------------------------------- /LEETCODE/Python/sum-of-digits-in-the-minimum-number.py: -------------------------------------------------------------------------------- 1 | # Time: O(n * l) 2 | # Space: O(l) 3 | 4 | class Solution(object): 5 | def sumOfDigits(self, A): 6 | """ 7 | :type A: List[int] 8 | :rtype: int 9 | """ 10 | total = sum([int(c) for c in str(min(A))]) 11 | return 1 if total % 2 == 0 else 0 12 | -------------------------------------------------------------------------------- /LEETCODE/Python/sum-of-square-numbers.py: -------------------------------------------------------------------------------- 1 | # Time: O(sqrt(c) * logc) 2 | # Space: O(1) 3 | 4 | import math 5 | 6 | 7 | class Solution(object): 8 | def judgeSquareSum(self, c): 9 | """ 10 | :type c: int 11 | :rtype: bool 12 | """ 13 | for a in xrange(int(math.sqrt(c))+1): 14 | b = int(math.sqrt(c-a**2)) 15 | if a**2 + b**2 == c: 16 | return True 17 | return False 18 | 19 | -------------------------------------------------------------------------------- /LEETCODE/Python/sum-of-unique-elements.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(n) 3 | 4 | import collections 5 | 6 | 7 | class Solution(object): 8 | def sumOfUnique(self, nums): 9 | """ 10 | :type nums: List[int] 11 | :rtype: int 12 | """ 13 | return sum(x for x, c in collections.Counter(nums).iteritems() if c == 1) 14 | -------------------------------------------------------------------------------- /LEETCODE/Python/teemo-attacking.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def findPoisonedDuration(self, timeSeries, duration): 6 | """ 7 | :type timeSeries: List[int] 8 | :type duration: int 9 | :rtype: int 10 | """ 11 | result = duration * len(timeSeries) 12 | for i in xrange(1, len(timeSeries)): 13 | result -= max(0, duration - (timeSeries[i] - timeSeries[i-1])) 14 | return result 15 | 16 | -------------------------------------------------------------------------------- /LEETCODE/Python/thousand-separator.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def thousandSeparator(self, n): 6 | """ 7 | :type n: int 8 | :rtype: str 9 | """ 10 | result = [] 11 | s = str(n) 12 | for i, c in enumerate(str(n)): 13 | if i and (len(s)-i)%3 == 0: 14 | result.append(".") 15 | result.append(c) 16 | return "".join(result) 17 | -------------------------------------------------------------------------------- /LEETCODE/Python/three-consecutive-odds.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def threeConsecutiveOdds(self, arr): 6 | """ 7 | :type arr: List[int] 8 | :rtype: bool 9 | """ 10 | count = 0 11 | for x in arr: 12 | count = count+1 if x%2 else 0 13 | if count == 3: 14 | return True 15 | return False 16 | -------------------------------------------------------------------------------- /LEETCODE/Python/to-lower-case.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def toLowerCase(self, str): 6 | """ 7 | :type str: str 8 | :rtype: str 9 | """ 10 | return "".join([chr(ord('a')+ord(c)-ord('A')) 11 | if 'A' <= c <= 'Z' else c for c in str]) 12 | 13 | -------------------------------------------------------------------------------- /LEETCODE/Python/truncate-sentence.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def truncateSentence(self, s, k): 6 | """ 7 | :type s: str 8 | :type k: int 9 | :rtype: str 10 | """ 11 | for i in xrange(len(s)): 12 | if s[i] == ' ': 13 | k -= 1 14 | if not k: 15 | return s[:i] 16 | return s 17 | -------------------------------------------------------------------------------- /LEETCODE/Python/two-sum-ii-input-array-is-sorted.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def twoSum(self, nums, target): 6 | start, end = 0, len(nums) - 1 7 | 8 | while start != end: 9 | sum = nums[start] + nums[end] 10 | if sum > target: 11 | end -= 1 12 | elif sum < target: 13 | start += 1 14 | else: 15 | return [start + 1, end + 1] 16 | 17 | -------------------------------------------------------------------------------- /LEETCODE/Python/ugly-number.py: -------------------------------------------------------------------------------- 1 | # Time: O(logn) = O(1) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | # @param {integer} num 6 | # @return {boolean} 7 | def isUgly(self, num): 8 | if num == 0: 9 | return False 10 | for i in [2, 3, 5]: 11 | while num % i == 0: 12 | num /= i 13 | return num == 1 14 | 15 | -------------------------------------------------------------------------------- /LEETCODE/Python/uncommon-words-from-two-sentences.py: -------------------------------------------------------------------------------- 1 | # Time: O(m + n) 2 | # Space: O(m + n) 3 | 4 | import collections 5 | 6 | 7 | class Solution(object): 8 | def uncommonFromSentences(self, A, B): 9 | """ 10 | :type A: str 11 | :type B: str 12 | :rtype: List[str] 13 | """ 14 | count = collections.Counter(A.split()) 15 | count += collections.Counter(B.split()) 16 | return [word for word in count if count[word] == 1] 17 | 18 | -------------------------------------------------------------------------------- /LEETCODE/Python/unique-paths.py: -------------------------------------------------------------------------------- 1 | # Time: O(m * n) 2 | # Space: O(m + n) 3 | 4 | class Solution(object): 5 | # @return an integer 6 | def uniquePaths(self, m, n): 7 | if m < n: 8 | return self.uniquePaths(n, m) 9 | ways = [1] * n 10 | 11 | for i in xrange(1, m): 12 | for j in xrange(1, n): 13 | ways[j] += ways[j - 1] 14 | 15 | return ways[n - 1] 16 | 17 | -------------------------------------------------------------------------------- /LEETCODE/Python/valid-boomerang.py: -------------------------------------------------------------------------------- 1 | # Time: O(1) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def isBoomerang(self, points): 6 | """ 7 | :type points: List[List[int]] 8 | :rtype: bool 9 | """ 10 | return (points[0][0] - points[1][0]) * (points[0][1] - points[2][1]) - \ 11 | (points[0][0] - points[2][0]) * (points[0][1] - points[1][1]) != 0 12 | -------------------------------------------------------------------------------- /LEETCODE/Python/valid-mountain-array.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def validMountainArray(self, A): 6 | """ 7 | :type A: List[int] 8 | :rtype: bool 9 | """ 10 | i = 0 11 | while i+1 < len(A) and A[i] < A[i+1]: 12 | i += 1 13 | j = len(A)-1 14 | while j-1 >= 0 and A[j-1] > A[j]: 15 | j -= 1 16 | return 0 < i == j < len(A)-1 17 | -------------------------------------------------------------------------------- /LEETCODE/Python/valid-parentheses.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(n) 3 | 4 | class Solution(object): 5 | # @return a boolean 6 | def isValid(self, s): 7 | stack, lookup = [], {"(": ")", "{": "}", "[": "]"} 8 | for parenthese in s: 9 | if parenthese in lookup: 10 | stack.append(parenthese) 11 | elif len(stack) == 0 or lookup[stack.pop()] != parenthese: 12 | return False 13 | return len(stack) == 0 14 | 15 | -------------------------------------------------------------------------------- /LEETCODE/Python/widest-vertical-area-between-two-points-containing-no-points.py: -------------------------------------------------------------------------------- 1 | # Time: O(nlogn) 2 | # Space: O(n) 3 | 4 | import itertools 5 | 6 | 7 | class Solution(object): 8 | def maxWidthOfVerticalArea(self, points): 9 | """ 10 | :type points: List[List[int]] 11 | :rtype: int 12 | """ 13 | sorted_x = sorted({x for x, y in points}) 14 | return max([b-a for a, b in itertools.izip(sorted_x, sorted_x[1:])] + [0]) 15 | -------------------------------------------------------------------------------- /LEETCODE/Python/xor-queries-of-a-subarray.py: -------------------------------------------------------------------------------- 1 | # Time: O(n) 2 | # Space: O(1) 3 | 4 | class Solution(object): 5 | def xorQueries(self, arr, queries): 6 | """ 7 | :type arr: List[int] 8 | :type queries: List[List[int]] 9 | :rtype: List[int] 10 | """ 11 | for i in xrange(1, len(arr)): 12 | arr[i] ^= arr[i-1] 13 | return [arr[right] ^ arr[left-1] if left else arr[right] for left, right in queries] 14 | -------------------------------------------------------------------------------- /LEETCODE/contents.txt: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /LEETCODE/find-peak-element.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findPeakElement(vector& nums) { 4 | int l=0; 5 | int r=nums.size()-1; 6 | while(lnums[mid+1]) 10 | r=mid; 11 | else 12 | l=mid+1; 13 | } 14 | return r; 15 | } 16 | -------------------------------------------------------------------------------- /React_Cookbook_Recipes_for_Mastering_the_React_Framework_by_David.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codeaholic-shub/ALGO-ADDICT/013983e3d07c61c5e90f9f64f82db97e98a1d4c4/React_Cookbook_Recipes_for_Mastering_the_React_Framework_by_David.pdf -------------------------------------------------------------------------------- /Untitled-1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | cout<<"HELLO WOELD"; 6 | } -------------------------------------------------------------------------------- /example.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | cout<<"hello world"; 6 | return 0; 7 | } 8 | -------------------------------------------------------------------------------- /tut9.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codeaholic-shub/ALGO-ADDICT/013983e3d07c61c5e90f9f64f82db97e98a1d4c4/tut9.exe --------------------------------------------------------------------------------