├── miscellaneous ├── gcd.py ├── permut ├── A_Bestie.java ├── C_Make_Good.py ├── A_TubeTube_Feed.py ├── A_Unit_Array.cpp ├── B_Quick_Sort.cpp ├── C_Odd_Subbarray.py ├── D_Xor_Sum_2.java ├── Minimum_Score.py ├── c_8_kbf_and_Tree.py ├── perfectSquare.py ├── A_Extremely_Round.py ├── A_Two_Permutations.py ├── Completing_the_square.py ├── N_Number_Reduction.py ├── Add_smallest_prime_factor.py ├── B_Interesting_Subarray.py ├── E_Palindrome_Partition.py ├── E_Prefix_Function_Queries.py ├── G_-_Strongest_Takahashi.py ├── D_Boris_and_His_Amazing_Haircut.py ├── C_Li_Hua_and_Chess.py ├── D_Vasya_and_Chess.py ├── Range_Updates_and_Sums.py ├── D_Playoff.py ├── A_-_Digit_Sum_2.py ├── A_besar_ahli.py ├── 3_ple.py ├── A_tcdr.py ├── EvenPals.py ├── a.out ├── A_-_flip.py ├── A_Echo.py ├── A_World_Cup.py ├── The_Gift.py ├── hackercup.pdf ├── A_Chord.py ├── C_Tiles.py ├── leaderboard.pdf ├── A_ab.py ├── B_Bad_Boy.py ├── E_Duplicate.py ├── sort_binary.py ├── D_Marking.py ├── A_Greatest_Convex.py ├── B_-_ABC-DEF.py ├── D_Remove_Two_Letters.py ├── Magical_Well_Of_Lilies.py ├── B_-_LOOKUP.py ├── B_Subscribers.py ├── Minimal_Rotation.py ├── A_SSeeeeiinngg_DDoouubbllee.py ├── A_camel_Case.py ├── B_K_Integers.py ├── Cyclic_shift.py ├── test.py ├── A_Attack.py ├── A_Filter.py ├── C_Swap_Game.py ├── A_-_Shift.py ├── A_A_B.py ├── knights.py ├── A_-_Find_Takahashi.py ├── A_Order_Something_Else.py ├── Arrange_the_array.py ├── B_Shifting_Sort.py ├── B_Trimmed_Mean.py ├── C_-_Extra_Charactor.py ├── C_-_abc_285_brutmhyhiizp.py ├── A_-_Sequence_of_Strings.py ├── A_Boboniu_Chats_with_Du.py ├── A_Not_Too_Hard.py ├── A_Plus_or_Minus.py ├── Problem.py ├── mod.py ├── A_-_Pawn_on_a_Grid.py ├── A_3_14.py ├── B_Permutation_Value.py ├── strange_thoughts.py ├── MEX.py ├── Permutation_and_Modulus.py ├── Valid_AP.py ├── A_Factorise_N_M.py ├── B_Colorful_Stones_Simplified_Edition.py ├── Fastest_Way_To_Murph.py ├── One_or_All.py ├── A_Medium_Number.py ├── C_Sum_on_Subarrays.py ├── A_-_Rightmost.py ├── A_Game_with_Board.py ├── A_Morning_Sandwich.py ├── B_Atilla_s_Favorite_Problem.py ├── B_Base_2.py ├── B_Best_Permutation.py ├── C_Thermostat.py ├── Existence.py ├── No_Palindrome.py ├── A_Swap_Odd_and_Even.py ├── B_-_Inverse_Prefix_Sum.py ├── Printing_Binary_Array.py ├── dist_sums.py ├── A_-_wwwvvvvvv.py ├── A_Probably_English.py ├── A_Regular_Bracket_Sequences.py ├── A_Subtraction_Game.py ├── B_A_A.py ├── L_shapes.py ├── crystals.py ├── A_Infinite_Grid.py ├── A_Sum.py ├── G_2_Subsequence_Addition_Hard_Version.py ├── topological.py ├── A_Coins.py ├── A_Weekly_Records.py ├── Make_Multiple.py ├── Make_Square.py ├── mob_year.py ├── A_Colored_Balls_Revisited.py ├── A_N_choice_question.py ├── Efficient_PAN_Linking.py ├── Gymkhana_Election_IIIT_A.py ├── Remove_Multiples.py ├── Remove_Two.py ├── div_of_div.py ├── factmod.py ├── interesting_sum.py ├── Aloy_Search_Party.py ├── Distinct_Neighbours.py ├── chip_game.py ├── A_-_-_1.py ├── A_Banis_and_Cards.py ├── A_Divisible_Array.py ├── lame_king.py ├── A_Musical_Puzzle.py ├── A_Yet_Another_Promotion.py ├── B_Death_s_Blessing.py ├── B_Increasing.py ├── less_or_equal.py ├── A_Two_Groups.py ├── D_Factorial_Divisibility.py ├── A_Indirect_Sort.py ├── B_3_smooth_Numbers.py ├── MIN_To_MAX.py ├── A_Love_Story.py ├── A_New_Palindrome.py ├── E_Drawings.py ├── line_sensor.py ├── A_Contest_Result.py ├── A_Same.py ├── B_Card_Trick.py ├── B_Conveyor_Belts.py ├── Equal_Elements.py ├── bigram.py ├── A_Buttons.py ├── B_The_Middle_Day.py ├── Sorted_Substrings.py ├── woeful_permutations.py ├── A_Arithmetic_Array.py ├── A_Password.py ├── B_Cunnithm.py ├── C_Complementary_XOR.py ├── C_Socks.py ├── String_Game.py ├── A_Double_Click.py ├── B_-_Multi_Test_Cases.py ├── Reyna_String.py ├── minsum.py ├── pascal.py ├── perp.py ├── A_Working_Week.py ├── B_ASCII_Art.py ├── A_Rudolph_and_Cut_the_Rope.py ├── A_To_My_Critics.py ├── B_Grab_the_Candies.py ├── C_Stones_on_the_Table.py ├── C_Yet_Another_2_or_1_Problem.py ├── muldiff.py ├── spell_check.py ├── B_11_11.py ├── B_Beautiful_Array.py ├── 2_3_moves.py ├── B_-_Longest_Uncommon_Prefix.py ├── B_Qual_B.py ├── Distributing_Apples.py ├── mathematical_circus.py ├── permutation_chain.py ├── wonderful_permutation.py ├── D_Range_Sum.py ├── HIGHEST_MARKS.cpp ├── coupons.py ├── sum_of_n.c ├── A_Job_Interview.py ├── D_Take_ABC.py ├── N_Triplets.py ├── A_Two_Elevators.py ├── A_Typical_Interview_Problem.py ├── A_Walking_Master.py ├── B_-_Adjacency_List.py ├── B_Chessboard.py ├── B_Tanya_and_Postcard.py ├── mul_persistence.py ├── A_To_Be_Saikyo.py ├── B_Sifid_and_Strange_Subsequences.py ├── Crushed_Apples_and_a_Balance_Scale.py ├── dividedecimal.py ├── B_Maximum_Strength.py ├── Bloodline.py ├── D_-_Yet_Another_Recursive_Function.py ├── A_Koxia_and_Whiteboards.py ├── A_Nine.py ├── B_Quick_Sort.py ├── E_NAND_repeatedly.py ├── Inside_The_Stadium.py ├── A_Treasure_Chest.py ├── C_Reversible.py ├── D_Circle_Line.py ├── Expensive_Steps.py ├── F_Sugar_Water_2.py ├── binaryInsert.py ├── B_chess_960.py ├── Crazy_Bishops_on_Chessboard.py ├── array.py ├── A_Sasha_and_Array_Coloring.py ├── D_-_Freefall.py ├── Find_eX.py ├── B_Permutations_Primes.py ├── C_Centers.py ├── MAX_CANDY.py ├── A_Beautiful_Sequence.py ├── B_Tea_with_Tangerines.py ├── C_Merge_Sequences.py ├── C_Min_Max_Pair.py ├── D_Count_Subtractions.py ├── denom.py ├── A_Alternately.py ├── A_Dice_Game.py ├── A_Escalator_Conversations.py ├── B_Default_Price.py ├── B_Expansion_coefficient_of_the_array.py ├── B_Ten_Words_of_Wisdom.py ├── C_Gap_Existence.py ├── F_Multi-Colored_Segments.py ├── Sum_of_Divisors.py ├── Unstable_Subarray.py ├── counting_rectangles.py └── good_string.py ├── tenth-line.sh ├── stone-game.py ├── to-lower-case.py ├── bulb-switcher.py ├── nim-game.py ├── add-two-integers.py ├── minimize-string-length.py ├── combine-two-tables.sql ├── gray-code.py ├── reverse-bits.py ├── alice-and-bob-playing-flower-game.py ├── minimum-operations-to-make-array-equal.py ├── beautiful-array.py ├── count-total-number-of-colored-cells.py ├── hamming-distance.py ├── rotate-string.py ├── check-if-the-sentence-is-pangram.py ├── find-the-maximum-achievable-number.py ├── most-expensive-item-that-can-not-be-bought.py ├── reverse-words-in-a-string-iii.py ├── move-zeroes.py ├── count-odd-numbers-in-an-interval-range.py ├── lexicographical-numbers.py ├── minimum-moves-to-equal-array-elements.py ├── score-of-a-string.py ├── sort-array-by-parity.py ├── distribute-candies.py ├── find-closest-number-to-zero.py ├── maximum-containers-on-a-ship.py ├── number-complement.py ├── partitioning-into-minimum-number-of-deci-binary-numbers.py ├── check-if-a-string-is-an-acronym-of-words.py ├── minimum-bit-flips-to-convert-number.py ├── number-of-senior-citizens.py ├── richest-customer-wealth.py ├── root-equals-sum-of-children.py ├── shuffle-the-array.py ├── smallest-even-multiple.py ├── construct-k-palindrome-strings.py ├── count-collisions-of-monkeys-on-a-polygon.py ├── divide-an-array-into-subarrays-with-minimum-cost-i.py ├── find-the-encrypted-string.py ├── remove-palindromic-subsequences.py ├── sum-of-all-subset-xor-totals.py ├── valid-palindrome.py ├── arranging-coins.py ├── maximum-number-of-words-found-in-sentences.py ├── calculate-delayed-arrival-time.py ├── concatenation-of-array.py ├── make-two-arrays-equal-by-reversing-subarrays.py ├── maximum-sum-with-exactly-k-elements.py ├── number-of-1-bits.py ├── unique-number-of-occurrences.py ├── guess-the-word.py ├── intersection-of-multiple-arrays.py ├── intersection-of-two-arrays.py ├── longest-uncommon-subsequence-i.py ├── single-number.py ├── a-number-after-a-double-reversal.py ├── climbing-stairs.py ├── count-substrings-starting-and-ending-with-given-character.py ├── divisible-and-non-divisible-sums-difference.py ├── find-words-containing-character.py ├── fizz-buzz.py ├── jewels-and-stones.py ├── maximum-xor-after-operations.py ├── sort-the-students-by-their-kth-score.py ├── transform-array-by-parity.py ├── count-good-numbers.py ├── replace-all-digits-with-characters.py ├── reverse-string.py ├── smallest-number-with-all-set-bits.py ├── convert-the-temperature.py ├── determine-if-two-events-have-conflict.py ├── find-the-value-of-the-partition.py ├── maximum-odd-binary-number.py ├── number-of-employees-who-met-the-target.py ├── counting-bits.py ├── debounce.js ├── maximize-greatness-of-an-array.py ├── maximum-product-difference-between-two-pairs.py ├── missing-number.py ├── find-the-integer-added-to-array-i.py ├── find-the-original-array-of-prefix-xor.py ├── power-of-three.py ├── running-sum-of-1d-array.py ├── adding-spaces-to-a-string.py ├── check-balanced-string.py ├── find-target-indices-after-sorting-array.py ├── minimum-cuts-to-divide-a-circle.py ├── n-ary-tree-postorder-traversal.py ├── rearrange-words-in-a-sentence.py ├── sum-of-digits-in-base-k.py ├── three-divisors.py ├── xor-operation-in-an-array.py ├── calculate-money-in-leetcode-bank.py ├── decode-xored-array.py ├── element-appearing-more-than-25-in-sorted-array.py ├── largest-positive-integer-that-exists-with-its-negative.py ├── merge-sorted-array.py ├── minimum-number-of-operations-to-make-array-xor-equal-to-k.py ├── sum-multiples.py ├── convert-date-to-binary.py ├── count-subarrays-of-length-three-with-a-condition.py ├── largest-odd-number-in-string.py ├── maximum-unique-subarray-sum-after-deletion.py ├── report-spam-message.py ├── strictly-palindromic-number.py ├── sum-of-unique-elements.py ├── find-indices-of-stable-mountains.py ├── find-the-child-who-has-the-ball-after-k-seconds.py ├── first-letter-to-appear-twice.py ├── height-checker.py ├── kids-with-the-greatest-number-of-candies.py ├── super-pow.py ├── the-two-sneaky-numbers-of-digitville.py ├── excel-sheet-column-number.py ├── find-lucky-integer-in-an-array.py ├── find-the-peaks.py ├── longest-happy-prefix.py ├── managers-with-at-least-5-direct-reportsundefined ├── minimum-operations-to-make-array-values-equal-to-k.py ├── palindrome-number.py ├── power-of-four.py ├── power-of-two.py ├── reverse-prefix-of-word.py ├── add-digits.py ├── fibonacci-number.py ├── find-first-palindromic-string-in-the-array.py ├── find-the-pivot-integer.py ├── find-the-winner-of-the-circular-game.py ├── is-subsequence.py ├── minimum-common-value.py ├── minimum-elements-to-add-to-form-a-given-sum.py ├── sort-the-people.py ├── special-array-i.py ├── find-three-consecutive-integers-that-sum-to-a-given-number.py ├── first-missing-positive.py ├── k-items-with-the-maximum-sum.py ├── keep-multiplying-found-values-by-two.py ├── last-substring-in-lexicographical-order.py ├── maximum-product-of-two-elements-in-an-array.py ├── minimum-sum-of-four-digit-number-after-splitting-digits.py ├── permutation-difference-between-two-strings.py ├── smallest-index-with-equal-value.py ├── sort-array-by-increasing-frequency.py ├── check-if-bitwise-or-has-trailing-zeros.py ├── count-elements-with-strictly-smaller-and-greater-elements.py ├── counting-words-with-a-given-prefix.py ├── first-unique-character-in-a-string.py ├── majority-element-ii.py ├── minimum-operations-to-exceed-threshold-value-i.py ├── rabbits-in-forest.py ├── top-k-frequent-words.py ├── convert-integer-to-the-sum-of-two-no-zero-integers.py ├── determine-if-a-cell-is-reachable-at-a-given-time.py ├── faulty-keyboard.py ├── maximum-area-of-longest-diagonal-rectangle.py ├── minimum-one-bit-operations-to-make-integers-zero.py ├── reverse-linked-list.py ├── sorting-the-sentence.py ├── sqrtx.py ├── check-if-all-characters-have-equal-number-of-occurrences.py ├── custom-sort-string.py ├── find-if-digit-game-can-be-won.py ├── find-minimum-operations-to-make-all-elements-divisible-by-three.py ├── find-the-difference-of-two-arrays.py ├── find-the-highest-altitude.py ├── h-index-ii.py ├── maximum-number-of-words-you-can-type.py ├── poor-pigs.py ├── smallest-string-with-a-given-numeric-value.py ├── vowels-game-in-a-string.py ├── vowels-of-all-substrings.py ├── ant-on-the-boundary.py ├── array-partition-i.py ├── check-if-two-chessboard-squares-have-the-same-color.py ├── final-array-state-after-k-multiplication-operations-i.py ├── find-the-xor-of-numbers-which-appear-twice.py ├── minimum-number-of-chairs-in-a-waiting-room.py ├── minimum-number-of-moves-to-seat-everyone.py ├── n-th-tribonacci-number.py ├── separate-black-and-white-balls.py ├── shuffle-string.py ├── decompress-run-length-encoded-list.py ├── double-modular-exponentiation.py ├── node-with-highest-edge-score.py ├── number-of-unique-xor-triplets-ii.py ├── remove-element.py ├── split-strings-by-separator.py ├── thousand-separator.py ├── angle-between-hands-of-a-clock.py ├── find-in-mountain-array.py ├── find-numbers-with-even-number-of-digits.py ├── find-the-winning-player-in-coin-game.py ├── matrix-diagonal-sum.py ├── number-of-common-factors.py ├── percentage-of-letter-in-string.py ├── create-target-array-in-the-given-order.py ├── jump-game.py ├── k-th-symbol-in-grammar.py ├── maximum-total-reward-using-operations-i.py ├── merge-two-2d-arrays-by-summing-values.py ├── neither-minimum-nor-maximum.py ├── stone-removal-game.py ├── convert-binary-number-in-a-linked-list-to-integer.py ├── find-champion-i.py ├── h-index.py ├── matrix-cells-in-distance-order.py ├── minimum-number-of-operations-to-make-elements-in-array-distinct.py ├── sum-of-squares-of-special-elements.py ├── three-consecutive-odds.py ├── divide-array-into-equal-pairs.py ├── find-the-k-th-lucky-number.py ├── make-array-non-decreasing.py ├── maximum-ice-cream-bars.py ├── maximum-possible-number-by-binary-concatenation.py ├── maximum-value-of-a-string-in-an-array.py ├── occurrences-after-bigram.py ├── widest-vertical-area-between-two-points-containing-no-points.py ├── circular-permutation-in-binary-representation.py ├── clumsy-factorial.py ├── consecutive-numbers-sum.py ├── delete-node-in-a-linked-list.py ├── escape-the-ghosts.py ├── find-n-unique-integers-sum-up-to-zero.py ├── find-the-student-that-will-replace-the-chalk.py ├── harshad-number.py ├── middle-of-the-linked-list.py ├── smallest-range-i.py ├── binary-number-with-alternating-bits.py ├── check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence.py ├── crawler-log-folder.py ├── minimum-value-to-get-positive-step-by-step-sum.py ├── number-of-segments-in-a-string.py ├── number-of-unique-xor-triplets-i.py ├── reach-a-number.py ├── remove-9.py ├── sum-of-values-at-indices-with-k-set-bits.py ├── capitalize-the-title.py ├── find-resultant-array-after-removing-anagrams.py ├── sum-of-digits-of-string-after-convert.py ├── teemo-attacking.py ├── append-characters-to-string-to-make-subsequence.py ├── buy-two-chocolates.py ├── calculate-digit-sum-of-a-string.py ├── check-if-it-is-a-good-array.py ├── count-alternating-subarrays.py ├── count-asterisks.py ├── count-the-number-of-consistent-strings.py ├── defuse-the-bomb.py ├── find-the-divisibility-array-of-a-string.py ├── k-closest-points-to-origin.py ├── minimize-maximum-pair-sum-in-array.py ├── minimum-changes-to-make-alternating-binary-string.py ├── minimum-number-of-days-to-eat-n-oranges.py ├── rank-transform-of-an-array.py ├── slowest-key.py ├── clear-digits.py ├── delete-and-earn.py ├── hash-divided-string.py ├── last-moment-before-all-ants-fall-out-of-a-plank.py ├── number-of-ways-to-buy-pens-and-pencils.py ├── subtract-the-product-and-sum-of-digits-of-an-integer.py ├── successful-pairs-of-spells-and-potions.py ├── zigzag-grid-traversal-with-skip.py ├── .github └── FUNDING.yml ├── account-balance-after-rounded-purchase.py ├── binary-string-with-substrings-representing-1-to-n.py ├── binary-tree-inorder-traversal.py ├── binary-tree-preorder-traversal.py ├── construct-the-rectangle.py ├── count-the-number-of-vowel-strings-in-range.py ├── identify-the-largest-outlier-in-an-array.py ├── implement-strstr.py ├── maximum-count-of-positive-integer-and-negative-integer.py ├── minimize-xor.py ├── minimum-moves-to-reach-target-score.py ├── minimum-number-of-vertices-to-reach-all-nodes.py ├── minimum-time-to-type-word-using-special-typewriter.py ├── mirror-reflection.py ├── number-of-laser-beams-in-a-bank.py ├── rearrange-array-to-maximize-prefix-score.py ├── semi-ordered-permutation.py ├── shifting-letters.py ├── sort-characters-by-frequency.py ├── valid-palindrome-iv.py ├── best-time-to-buy-and-sell-stock.py ├── binary-tree-postorder-traversal.py ├── coin-change-ii.py ├── concatenation-of-consecutive-binary-numbers.py ├── count-the-digits-that-divide-a-number.py ├── find-the-k-or-of-an-array.py ├── find-the-number-of-winning-players.py ├── maximum-difference-between-adjacent-elements-in-a-circular-array.py ├── minimum-distance-to-the-target-element.py ├── peak-index-in-a-mountain-array.py ├── remove-duplicates-from-sorted-array.py ├── swap-nodes-in-pairs.py ├── ugly-number.py ├── defanging-an-ip-address.py ├── determine-if-two-strings-are-close.py ├── final-value-of-variable-after-performing-operations.py ├── find-maximum-number-of-string-pairs.py ├── find-the-n-th-value-after-k-seconds.py ├── maximum-number-of-operations-with-the-same-score-i.py ├── maximum-strength-of-a-group.py ├── min-cost-climbing-stairs.py ├── minimum-right-shifts-to-sort-the-array.py ├── rectangle-overlap.py ├── repeated-dna-sequences.py ├── row-with-maximum-ones.py ├── cells-in-a-range-on-an-excel-sheet.py ├── circular-sentence.py ├── complex-number-multiplication.py ├── implement-rand10-using-rand7.py ├── integer-break.py ├── minimum-number-of-changes-to-make-binary-string-beautiful.py ├── relative-sort-array.py ├── solving-questions-with-brainpower.py ├── check-if-all-as-appears-before-all-bs.py ├── check-if-array-pairs-are-divisible-by-k.py ├── check-if-n-and-its-double-exist.py ├── excel-sheet-column-title.py ├── find-all-duplicates-in-an-array.py ├── find-peak-element.py ├── longest-palindrome.py ├── make-three-strings-equal.py ├── maximum-69-number.py ├── maximum-number-of-pairs-in-array.py ├── points-that-intersect-with-cars.py ├── powx-n.py ├── removing-minimum-and-maximum-from-array.py ├── removing-stars-from-a-string.py ├── special-array-with-x-elements-greater-than-or-equal-x.py ├── convert-1d-array-into-2d-array.py ├── count-pairs-whose-sum-is-less-than-target.py ├── divide-a-string-into-groups-of-size-k.py ├── find-the-number-of-copy-arrays.py ├── find-the-score-of-all-prefixes-of-an-array.py ├── find-the-sequence-of-strings-appeared-on-the-screen.py ├── minimum-array-end.py ├── minimum-depth-of-binary-tree.py ├── number-of-even-and-odd-bits.py ├── truncate-sentence.py ├── combination-sum-iv.py ├── count-pairs-that-form-a-complete-day-i.py ├── count-pairs-that-form-a-complete-day-ii.py ├── count-the-hidden-sequences.py ├── destination-city.py ├── final-prices-with-a-special-discount-in-a-shop.py ├── find-the-original-typed-string-i.py └── kth-missing-positive-number.py /miscellaneous/gcd.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /miscellaneous/permut: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /miscellaneous/A_Bestie.java: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /miscellaneous/C_Make_Good.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /miscellaneous/A_TubeTube_Feed.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /miscellaneous/A_Unit_Array.cpp: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /miscellaneous/B_Quick_Sort.cpp: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /miscellaneous/C_Odd_Subbarray.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /miscellaneous/D_Xor_Sum_2.java: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /miscellaneous/Minimum_Score.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /miscellaneous/c_8_kbf_and_Tree.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /miscellaneous/perfectSquare.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /miscellaneous/A_Extremely_Round.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /miscellaneous/A_Two_Permutations.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /miscellaneous/Completing_the_square.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /miscellaneous/N_Number_Reduction.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tenth-line.sh: -------------------------------------------------------------------------------- 1 | sed -n '10p' < file.txt -------------------------------------------------------------------------------- /miscellaneous/Add_smallest_prime_factor.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /miscellaneous/B_Interesting_Subarray.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /miscellaneous/E_Palindrome_Partition.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /miscellaneous/E_Prefix_Function_Queries.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /miscellaneous/G_-_Strongest_Takahashi.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /miscellaneous/D_Boris_and_His_Amazing_Haircut.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /miscellaneous/C_Li_Hua_and_Chess.py: -------------------------------------------------------------------------------- 1 | t = int(input()) -------------------------------------------------------------------------------- /miscellaneous/D_Vasya_and_Chess.py: -------------------------------------------------------------------------------- 1 | n = int(input()) -------------------------------------------------------------------------------- /miscellaneous/Range_Updates_and_Sums.py: -------------------------------------------------------------------------------- 1 | import numpy -------------------------------------------------------------------------------- /miscellaneous/D_Playoff.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | 3 | s = input() -------------------------------------------------------------------------------- /miscellaneous/A_-_Digit_Sum_2.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | 3 | k = len(str(n)) 4 | -------------------------------------------------------------------------------- /miscellaneous/A_besar_ahli.py: -------------------------------------------------------------------------------- 1 | s = input() 2 | t = input() 3 | 4 | print(s + t) -------------------------------------------------------------------------------- /miscellaneous/3_ple.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | 3 | print("Yes" if n % 3 == 0 else "No") -------------------------------------------------------------------------------- /miscellaneous/A_tcdr.py: -------------------------------------------------------------------------------- 1 | s = input() 2 | 3 | print("".join(c for c in s if c not in "aeiou")) -------------------------------------------------------------------------------- /miscellaneous/EvenPals.py: -------------------------------------------------------------------------------- 1 | n, m = map(int, input().split()) 2 | 3 | M = 998244353 4 | 5 | -------------------------------------------------------------------------------- /miscellaneous/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/theabbie/leetcode/HEAD/miscellaneous/a.out -------------------------------------------------------------------------------- /miscellaneous/A_-_flip.py: -------------------------------------------------------------------------------- 1 | s = input() 2 | 3 | print("".join("0" if c == "1" else "1" for c in s)) -------------------------------------------------------------------------------- /miscellaneous/A_Echo.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | 3 | s = input() 4 | 5 | print("".join(c + c for c in s)) -------------------------------------------------------------------------------- /miscellaneous/A_World_Cup.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | 3 | while n % 4 != 2: 4 | n += 1 5 | 6 | print(n) -------------------------------------------------------------------------------- /miscellaneous/The_Gift.py: -------------------------------------------------------------------------------- 1 | x, n, m = map(int, input().split()) 2 | 3 | print("YES" if x + m >= n else "NO") -------------------------------------------------------------------------------- /miscellaneous/hackercup.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/theabbie/leetcode/HEAD/miscellaneous/hackercup.pdf -------------------------------------------------------------------------------- /stone-game.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def stoneGame(self, piles: List[int]) -> bool: 3 | return True -------------------------------------------------------------------------------- /to-lower-case.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def toLowerCase(self, s: str) -> str: 3 | return s.lower() -------------------------------------------------------------------------------- /bulb-switcher.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def bulbSwitch(self, n: int) -> int: 3 | return int(n ** 0.5) -------------------------------------------------------------------------------- /miscellaneous/A_Chord.py: -------------------------------------------------------------------------------- 1 | print("Yes" if input() in {"ACE", "BDF", "CEG", "DFA", "EGB", "FAC", "GBD"} else "No") -------------------------------------------------------------------------------- /miscellaneous/C_Tiles.py: -------------------------------------------------------------------------------- 1 | M = 998244353 2 | 3 | w, h = map(int, input().split()) 4 | 5 | print(pow(2, w + h, M)) -------------------------------------------------------------------------------- /miscellaneous/leaderboard.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/theabbie/leetcode/HEAD/miscellaneous/leaderboard.pdf -------------------------------------------------------------------------------- /nim-game.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def canWinNim(self, n: int) -> bool: 3 | return n != (n >> 2) << 2 -------------------------------------------------------------------------------- /add-two-integers.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def sum(self, num1: int, num2: int) -> int: 3 | return num1 + num2 -------------------------------------------------------------------------------- /miscellaneous/A_ab.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | 3 | s = input() 4 | 5 | print("Yes" if "ab" in s or "ba" in s else "No") -------------------------------------------------------------------------------- /miscellaneous/B_Bad_Boy.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | 3 | for _ in range(t): 4 | n, m, i, j = map(int, input().split()) -------------------------------------------------------------------------------- /miscellaneous/E_Duplicate.py: -------------------------------------------------------------------------------- 1 | M = 998244353 2 | 3 | n = int(input()) 4 | 5 | s = input() 6 | 7 | vals = s.split("1") -------------------------------------------------------------------------------- /miscellaneous/sort_binary.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | 3 | for _ in range(t): 4 | n = int(input()) 5 | s = input() -------------------------------------------------------------------------------- /miscellaneous/D_Marking.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | 3 | for _ in range(t): 4 | n, d, k = list(map(int, input().split())) -------------------------------------------------------------------------------- /minimize-string-length.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def minimizedStringLength(self, s: str) -> int: 3 | return len(set(s)) -------------------------------------------------------------------------------- /miscellaneous/A_Greatest_Convex.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | 3 | for _ in range(t): 4 | k = int(input()) 5 | print(k - 1) -------------------------------------------------------------------------------- /miscellaneous/B_-_ABC-DEF.py: -------------------------------------------------------------------------------- 1 | a, b, c, d, e, f = map(int, input().split()) 2 | 3 | print((a * b * c - d * e * f) % 998244353) -------------------------------------------------------------------------------- /miscellaneous/D_Remove_Two_Letters.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | 3 | for _ in range(t): 4 | n = int(input()) 5 | s = input() -------------------------------------------------------------------------------- /miscellaneous/Magical_Well_Of_Lilies.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | 3 | for _ in range(t): 4 | n = int(input()) 5 | coins = 0 -------------------------------------------------------------------------------- /combine-two-tables.sql: -------------------------------------------------------------------------------- 1 | SELECT firstName, lastName, city, state from Person LEFT JOIN Address ON Person.personId = Address.personId; -------------------------------------------------------------------------------- /gray-code.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def grayCode(self, n: int) -> List[int]: 3 | return [i ^ (i >> 1) for i in range(1 << n)] -------------------------------------------------------------------------------- /miscellaneous/B_-_LOOKUP.py: -------------------------------------------------------------------------------- 1 | s = input() 2 | 3 | t = input() 4 | 5 | if t in s: 6 | print("Yes") 7 | else: 8 | print("No") -------------------------------------------------------------------------------- /miscellaneous/B_Subscribers.py: -------------------------------------------------------------------------------- 1 | n = list(input()) 2 | 3 | for i in range(3, len(n)): 4 | n[i] = "0" 5 | 6 | print("".join(n)) -------------------------------------------------------------------------------- /miscellaneous/Minimal_Rotation.py: -------------------------------------------------------------------------------- 1 | s = input() 2 | 3 | n = len(s) 4 | 5 | ss = s + s 6 | 7 | print(min(ss[i:i+n] for i in range(n))) -------------------------------------------------------------------------------- /reverse-bits.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def reverseBits(self, n: int) -> int: 3 | return int("{:032b}".format(n)[::-1], 2) -------------------------------------------------------------------------------- /alice-and-bob-playing-flower-game.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def flowerGame(self, n: int, m: int) -> int: 3 | return m * n // 2 -------------------------------------------------------------------------------- /minimum-operations-to-make-array-equal.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def minOperations(self, n: int) -> int: 3 | return (n * n) >> 2 -------------------------------------------------------------------------------- /miscellaneous/A_SSeeeeiinngg_DDoouubbllee.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | 3 | for _ in range(t): 4 | s = input() 5 | print(s + s[::-1]) -------------------------------------------------------------------------------- /miscellaneous/A_camel_Case.py: -------------------------------------------------------------------------------- 1 | s = input() 2 | 3 | i = 0 4 | 5 | while i < len(s) and s[i].islower(): 6 | i += 1 7 | 8 | print(i + 1) -------------------------------------------------------------------------------- /miscellaneous/B_K_Integers.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | 3 | for _ in range(t): 4 | n, k = map(int, input().split()) 5 | print("YES" ) -------------------------------------------------------------------------------- /miscellaneous/Cyclic_shift.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | 3 | for _ in range(t): 4 | n, k = map(int, input().split()) 5 | s = input() -------------------------------------------------------------------------------- /miscellaneous/test.py: -------------------------------------------------------------------------------- 1 | import random 2 | 3 | N = 10 ** 5 4 | 5 | for _ in range(3): 6 | print(random.choices(range(1, 100), k = N)) -------------------------------------------------------------------------------- /beautiful-array.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def beautifulArray(self, N): 3 | return sorted(range(1, N + 1), key=lambda x: bin(x)[:1:-1]) -------------------------------------------------------------------------------- /count-total-number-of-colored-cells.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def coloredCells(self, n: int) -> int: 3 | return 2 * n * n - 2 * n + 1 -------------------------------------------------------------------------------- /hamming-distance.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def hammingDistance(self, x: int, y: int) -> int: 3 | return "{:b}".format(x ^ y).count("1") -------------------------------------------------------------------------------- /miscellaneous/A_Attack.py: -------------------------------------------------------------------------------- 1 | a, b = map(int, input().split()) 2 | 3 | res = a // b 4 | 5 | if a % b != 0: 6 | res += 1 7 | 8 | print(res) -------------------------------------------------------------------------------- /miscellaneous/A_Filter.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | 3 | arr = list(map(int, input().split())) 4 | 5 | print(*[el for el in arr if el % 2 == 0]) -------------------------------------------------------------------------------- /miscellaneous/C_Swap_Game.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | 3 | for _ in range(t): 4 | n = int(input()) 5 | arr = list(map(int, input().split())) -------------------------------------------------------------------------------- /miscellaneous/A_-_Shift.py: -------------------------------------------------------------------------------- 1 | n, k = map(int, input().split()) 2 | 3 | arr = list(map(int, input().split())) 4 | 5 | print(*arr[k:], *[0] * min(k, n)) -------------------------------------------------------------------------------- /miscellaneous/A_A_B.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | 3 | for _ in range(t): 4 | s = input() 5 | print(int(s.split("+")[0]) + int(s.split("+")[1])) -------------------------------------------------------------------------------- /miscellaneous/knights.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | 3 | for _ in range(t): 4 | n, m = map(int, input().split()) 5 | print(min(2, n), min(2, m)) -------------------------------------------------------------------------------- /rotate-string.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def rotateString(self, s: str, goal: str) -> bool: 3 | return goal in s + s and len(s) == len(goal) -------------------------------------------------------------------------------- /check-if-the-sentence-is-pangram.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def checkIfPangram(self, sentence: str) -> bool: 3 | return len(set(sentence)) == 26 -------------------------------------------------------------------------------- /find-the-maximum-achievable-number.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def theMaximumAchievableX(self, num: int, t: int) -> int: 3 | return num + 2 * t -------------------------------------------------------------------------------- /most-expensive-item-that-can-not-be-bought.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def mostExpensiveItem(self, m: int, n: int) -> int: 3 | return m * n - m - n -------------------------------------------------------------------------------- /reverse-words-in-a-string-iii.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def reverseWords(self, s: str) -> str: 3 | return " ".join(w[::-1] for w in s.split()) -------------------------------------------------------------------------------- /miscellaneous/A_-_Find_Takahashi.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | 3 | arr = list(map(int, input().split())) 4 | 5 | print(1 + max(range(n), key = lambda i: arr[i])) -------------------------------------------------------------------------------- /miscellaneous/A_Order_Something_Else.py: -------------------------------------------------------------------------------- 1 | n, p, q = map(int, input().split()) 2 | 3 | d = list(map(int, input().split())) 4 | 5 | print(min(p, q + min(d))) -------------------------------------------------------------------------------- /miscellaneous/Arrange_the_array.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | 3 | for _ in range(t): 4 | n = int(input()) 5 | arr = list(map(int, input().split())) 6 | -------------------------------------------------------------------------------- /miscellaneous/B_Shifting_Sort.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | 3 | for _ in range(t): 4 | n = int(input()) 5 | arr = list(map(int, input().split())) 6 | -------------------------------------------------------------------------------- /miscellaneous/B_Trimmed_Mean.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | 3 | arr = list(map(int, input().split())) 4 | 5 | arr.sort() 6 | 7 | print(sum(arr[n:-n]) / (3 * n)) -------------------------------------------------------------------------------- /miscellaneous/C_-_Extra_Charactor.py: -------------------------------------------------------------------------------- 1 | s = input() 2 | t = input() 3 | 4 | i = 0 5 | while i < len(s) and s[i] == t[i]: 6 | i += 1 7 | 8 | print(i + 1) -------------------------------------------------------------------------------- /miscellaneous/C_-_abc_285_brutmhyhiizp.py: -------------------------------------------------------------------------------- 1 | s = input() 2 | 3 | res = 0 4 | 5 | for c in s: 6 | res = 26 * res + ord(c) - ord('A') + 1 7 | 8 | print(res) -------------------------------------------------------------------------------- /move-zeroes.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def moveZeroes(self, nums: List[int]) -> None: 3 | nums.sort(key = lambda a: 1 if a == 0 else 0) 4 | -------------------------------------------------------------------------------- /count-odd-numbers-in-an-interval-range.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def countOdds(self, low: int, high: int) -> int: 3 | return (high + 1) // 2 - low // 2 -------------------------------------------------------------------------------- /lexicographical-numbers.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def lexicalOrder(self, n: int) -> List[int]: 3 | return sorted(range(1, n + 1), key = lambda i: str(i)) -------------------------------------------------------------------------------- /minimum-moves-to-equal-array-elements.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def minMoves(self, nums: List[int]) -> int: 3 | return sum(nums) - min(nums) * len(nums) -------------------------------------------------------------------------------- /miscellaneous/A_-_Sequence_of_Strings.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | 3 | s = [] 4 | 5 | for _ in range(n): 6 | s.append(input()) 7 | 8 | print("\n".join(s[::-1])) -------------------------------------------------------------------------------- /miscellaneous/A_Boboniu_Chats_with_Du.py: -------------------------------------------------------------------------------- 1 | n, d, m = map(int, input().split()) 2 | 3 | arr = list(map(int, input().split())) 4 | 5 | i = 0 6 | j = n - 1 7 | 8 | -------------------------------------------------------------------------------- /miscellaneous/A_Not_Too_Hard.py: -------------------------------------------------------------------------------- 1 | n, x = map(int, input().split()) 2 | 3 | arr = list(map(int, input().split())) 4 | 5 | print(sum(el for el in arr if el <= x)) -------------------------------------------------------------------------------- /miscellaneous/A_Plus_or_Minus.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | 3 | for _ in range(t): 4 | a, b, c = map(int, input().split()) 5 | print("+" if a + b == c else "-") -------------------------------------------------------------------------------- /miscellaneous/Problem.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | 3 | for _ in range(t): 4 | m, n = map(int, input().split()) 5 | print("YES" if (m + n) % 2 == 0 else "NO") -------------------------------------------------------------------------------- /miscellaneous/mod.py: -------------------------------------------------------------------------------- 1 | num = [7, 5, 7, 8] 2 | p = 129 3 | curr = 0 4 | base = 10 % p 5 | 6 | for n in num: 7 | curr = (curr * base + n) % p 8 | 9 | print(curr) -------------------------------------------------------------------------------- /score-of-a-string.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def scoreOfString(self, s: str) -> int: 3 | return sum(abs(ord(s[i]) - ord(s[i+1])) for i in range(len(s) - 1)) -------------------------------------------------------------------------------- /sort-array-by-parity.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def sortArrayByParity(self, nums: List[int]) -> List[int]: 3 | return sorted(nums, key = lambda x: x & 1) -------------------------------------------------------------------------------- /distribute-candies.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def distributeCandies(self, candyType: List[int]) -> int: 3 | return min(len(candyType) // 2, len(set(candyType))) -------------------------------------------------------------------------------- /find-closest-number-to-zero.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findClosestNumber(self, nums: List[int]) -> int: 3 | return min(nums, key = lambda x: (x * x, -x)) -------------------------------------------------------------------------------- /maximum-containers-on-a-ship.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def maxContainers(self, n: int, w: int, maxWeight: int) -> int: 3 | return min(maxWeight // w, n * n) -------------------------------------------------------------------------------- /miscellaneous/A_-_Pawn_on_a_Grid.py: -------------------------------------------------------------------------------- 1 | m, n = map(int, input().split()) 2 | 3 | res = 0 4 | 5 | for _ in range(m): 6 | res += input().count("#") 7 | 8 | print(res) -------------------------------------------------------------------------------- /miscellaneous/A_3_14.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | 3 | print("3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679"[:n+2]) -------------------------------------------------------------------------------- /miscellaneous/B_Permutation_Value.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | 3 | for _ in range(t): 4 | n = int(input()) 5 | res = [1] + list(range(n, 1, -1)) 6 | print(*res) -------------------------------------------------------------------------------- /miscellaneous/strange_thoughts.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | 3 | for _ in range(t): 4 | n = int(input()) 5 | res = (2 * n - n % 2) + 2 * (n // 3) 6 | print(res) -------------------------------------------------------------------------------- /number-complement.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findComplement(self, num: int) -> int: 3 | return int("".join([str(1 - int(c)) for c in "{:b}".format(num)]), 2) -------------------------------------------------------------------------------- /partitioning-into-minimum-number-of-deci-binary-numbers.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def minPartitions(self, n: str) -> int: 3 | return max([int(d) for d in n]) -------------------------------------------------------------------------------- /check-if-a-string-is-an-acronym-of-words.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isAcronym(self, words: List[str], s: str) -> bool: 3 | return list(s) == [w[0] for w in words] -------------------------------------------------------------------------------- /minimum-bit-flips-to-convert-number.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def minBitFlips(self, start: int, goal: int) -> int: 3 | return "{:b}".format(start ^ goal).count("1") -------------------------------------------------------------------------------- /miscellaneous/MEX.py: -------------------------------------------------------------------------------- 1 | def MEX(arr): 2 | arr.sort() 3 | p = [0] 4 | for el in arr: 5 | p.append(p[-1] + el) 6 | return p 7 | 8 | print(MEX([0,1,3,4])) -------------------------------------------------------------------------------- /miscellaneous/Permutation_and_Modulus.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | 3 | for _ in range(t): 4 | n = int(input()) 5 | res = list(range(2, n + 1)) + [1] 6 | print(*res) -------------------------------------------------------------------------------- /miscellaneous/Valid_AP.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | 3 | for _ in range(t): 4 | a, b, n = map(int, input().split()) 5 | print("YES" if (b - a) % (n + 1) == 0 else "NO") -------------------------------------------------------------------------------- /number-of-senior-citizens.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def countSeniors(self, details: List[str]) -> int: 3 | return len([el for el in details if int(el[11:13]) > 60]) -------------------------------------------------------------------------------- /richest-customer-wealth.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def maximumWealth(self, accounts: List[List[int]]) -> int: 3 | return max([sum(person) for person in accounts]) -------------------------------------------------------------------------------- /root-equals-sum-of-children.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def checkTree(self, root: Optional[TreeNode]) -> bool: 3 | return root.val == root.left.val + root.right.val -------------------------------------------------------------------------------- /shuffle-the-array.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def shuffle(self, nums: List[int], n: int) -> List[int]: 3 | return [nums[(i >> 1) + n * (i & 1)] for i in range(2 * n)] -------------------------------------------------------------------------------- /smallest-even-multiple.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def smallestEvenMultiple(self, n: int) -> int: 3 | if n % 2 == 0: 4 | return n 5 | return 2 * n -------------------------------------------------------------------------------- /construct-k-palindrome-strings.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def canConstruct(self, s: str, k: int) -> bool: 3 | return sum(f % 2 for f in Counter(s).values()) <= k <= len(s) -------------------------------------------------------------------------------- /count-collisions-of-monkeys-on-a-polygon.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def monkeyMove(self, n: int) -> int: 3 | M = 10 ** 9 + 7 4 | return (M + pow(2, n, M) - 2) % M -------------------------------------------------------------------------------- /divide-an-array-into-subarrays-with-minimum-cost-i.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def minimumCost(self, nums: List[int]) -> int: 3 | return nums[0] + sum(sorted(nums[1:])[:2]) -------------------------------------------------------------------------------- /find-the-encrypted-string.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def getEncryptedString(self, s: str, k: int) -> str: 3 | return "".join(s[(i + k) % len(s)] for i in range(len(s))) -------------------------------------------------------------------------------- /miscellaneous/A_Factorise_N_M.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | 3 | for _ in range(t): 4 | n = int(input()) 5 | if n == 2: 6 | print(7) 7 | else: 8 | print(3) -------------------------------------------------------------------------------- /miscellaneous/B_Colorful_Stones_Simplified_Edition.py: -------------------------------------------------------------------------------- 1 | s = input() 2 | t = input() 3 | 4 | i = 0 5 | for el in t: 6 | if el == s[i]: 7 | i += 1 8 | 9 | print(i + 1) -------------------------------------------------------------------------------- /miscellaneous/Fastest_Way_To_Murph.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | 3 | for _ in range(t): 4 | a, b = map(int, input().split()) 5 | x = abs(a - b) 6 | print(min(x, 7 - x)) -------------------------------------------------------------------------------- /miscellaneous/One_or_All.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | 3 | for _ in range(t): 4 | n = int(input()) 5 | arr = list(map(int, input().split())) 6 | print(sum(arr) % (n + 1)) -------------------------------------------------------------------------------- /remove-palindromic-subsequences.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def removePalindromeSub(self, s: str) -> int: 3 | if s == s[::-1]: 4 | return 1 5 | return 2 -------------------------------------------------------------------------------- /sum-of-all-subset-xor-totals.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def subsetXORSum(self, nums: List[int]) -> int: 3 | return (1 << len(nums) - 1) * reduce(lambda x, y: x | y, nums) -------------------------------------------------------------------------------- /valid-palindrome.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isPalindrome(self, s: str) -> bool: 3 | s = "".join([c.lower() for c in s if c.isalnum()]) 4 | return s == s[::-1] -------------------------------------------------------------------------------- /arranging-coins.py: -------------------------------------------------------------------------------- 1 | import math 2 | 3 | class Solution: 4 | def arrangeCoins(self, n: int) -> int: 5 | k = math.floor((math.sqrt(1 + 8 * n) - 1) / 2) 6 | return k -------------------------------------------------------------------------------- /maximum-number-of-words-found-in-sentences.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def mostWordsFound(self, sentences: List[str]) -> int: 3 | return max([len(s.split()) for s in sentences]) -------------------------------------------------------------------------------- /miscellaneous/A_Medium_Number.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | 3 | for _ in range(t): 4 | a, b, c = map(int, input().split()) 5 | print(a + b + c - max([a, b , c]) - min([a, b, c])) -------------------------------------------------------------------------------- /miscellaneous/C_Sum_on_Subarrays.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | 3 | for _ in range(t): 4 | n, k = map(int, input().split()) 5 | res = [1] * n 6 | res[0] = -k 7 | print(*res) -------------------------------------------------------------------------------- /calculate-delayed-arrival-time.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findDelayedArrivalTime(self, arrivalTime: int, delayedTime: int) -> int: 3 | return (arrivalTime + delayedTime) % 24 -------------------------------------------------------------------------------- /concatenation-of-array.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def getConcatenation(self, nums: List[int]) -> List[int]: 3 | n = len(nums) 4 | return [nums[i % n] for i in range(2 * n)] -------------------------------------------------------------------------------- /make-two-arrays-equal-by-reversing-subarrays.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def canBeEqual(self, target: List[int], arr: List[int]) -> bool: 3 | return sorted(arr) == sorted(target) -------------------------------------------------------------------------------- /maximum-sum-with-exactly-k-elements.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def maximizeSum(self, nums: List[int], k: int) -> int: 3 | m = max(nums) 4 | return m * k + k * (k - 1) // 2 -------------------------------------------------------------------------------- /miscellaneous/A_-_Rightmost.py: -------------------------------------------------------------------------------- 1 | s = input() 2 | 3 | n = len(s) 4 | 5 | res = -1 6 | 7 | for i in range(n): 8 | if s[i] == 'a': 9 | res = i + 1 10 | 11 | print(res) -------------------------------------------------------------------------------- /miscellaneous/A_Game_with_Board.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | 3 | for _ in range(t): 4 | n = int(input()) 5 | if n <= 4: 6 | print("Bob") 7 | else: 8 | print("Alice") -------------------------------------------------------------------------------- /miscellaneous/A_Morning_Sandwich.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | 3 | for _ in range(t): 4 | b, c, h = map(int, input().split()) 5 | k = c + h 6 | print(min(k + 1, b) + min(k, b - 1)) -------------------------------------------------------------------------------- /miscellaneous/B_Atilla_s_Favorite_Problem.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | 3 | for _ in range(t): 4 | n = int(input()) 5 | s = input() 6 | print(max([ord(c) - ord('a') + 1 for c in s])) -------------------------------------------------------------------------------- /miscellaneous/B_Base_2.py: -------------------------------------------------------------------------------- 1 | arr = list(map(int, input().split())) 2 | 3 | res = 0 4 | 5 | p = 1 6 | 7 | for el in arr: 8 | res += p * el 9 | p *= 2 10 | 11 | print(res) -------------------------------------------------------------------------------- /miscellaneous/B_Best_Permutation.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | 3 | for _ in range(t): 4 | n = int(input()) 5 | res = [i for i in range(2, n - 1)] + [1] + [n - 1, n] 6 | print(*res) -------------------------------------------------------------------------------- /miscellaneous/C_Thermostat.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | 3 | for _ in range(t): 4 | l, r, x = map(int, input().split()) 5 | a, b = map(int, input().split()) 6 | print(b % x - a % x) -------------------------------------------------------------------------------- /miscellaneous/Existence.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | 3 | for _ in range(t): 4 | x, y = map(int, input().split()) 5 | print("YES" if x * x * x * x + 4 * y * y == 4 * x * x * y else "NO") -------------------------------------------------------------------------------- /miscellaneous/No_Palindrome.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | 3 | for _ in range(t): 4 | n, k = map(int, input().split()) 5 | print(1 + ((n - 1) // 4) * 2 + sum([0, 0, 1, 1][:(n - 1) % 4])) -------------------------------------------------------------------------------- /number-of-1-bits.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def hammingWeight(self, n: int) -> int: 3 | if n == 0: 4 | return 0 5 | return (n & 1) + self.hammingWeight(n >> 1) -------------------------------------------------------------------------------- /unique-number-of-occurrences.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def uniqueOccurrences(self, arr: List[int]) -> bool: 3 | return len(Counter(arr).values()) == len(set(Counter(arr).values())) -------------------------------------------------------------------------------- /guess-the-word.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findSecretWord(self, words: List[str], master: 'Master') -> None: 3 | master.__init__(words[0], words, 1) 4 | master.guess(words[0]) -------------------------------------------------------------------------------- /intersection-of-multiple-arrays.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def intersection(self, nums: List[List[int]]) -> List[int]: 3 | return sorted(set.intersection(*[set(num) for num in nums])) -------------------------------------------------------------------------------- /intersection-of-two-arrays.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]: 3 | return list(set.intersection(set(nums1), set(nums2))) -------------------------------------------------------------------------------- /longest-uncommon-subsequence-i.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findLUSlength(self, a: str, b: str) -> int: 3 | if a == b: 4 | return -1 5 | return max(len(a), len(b)) -------------------------------------------------------------------------------- /miscellaneous/A_Swap_Odd_and_Even.py: -------------------------------------------------------------------------------- 1 | s = list(input()) 2 | 3 | n = len(s) 4 | 5 | for i in range(n // 2): 6 | s[2 * i], s[2 * i + 1] = s[2 * i + 1], s[2 * i] 7 | 8 | print("".join(s)) -------------------------------------------------------------------------------- /miscellaneous/B_-_Inverse_Prefix_Sum.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | 3 | arr = list(map(int, input().split())) 4 | 5 | for i in range(n - 1, 0, -1): 6 | arr[i] -= arr[i - 1] 7 | 8 | print(*arr) -------------------------------------------------------------------------------- /miscellaneous/Printing_Binary_Array.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | 3 | for _ in range(t): 4 | n = int(input()) 5 | arr = list(map(int, input().split())) 6 | print(*[1 - el for el in arr]) -------------------------------------------------------------------------------- /miscellaneous/dist_sums.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | 3 | for _ in range(t): 4 | n = int(input()) 5 | res = list(range(1, n + 1)) 6 | res[:n//2] = res[:n//2][::-1] 7 | print(*res) -------------------------------------------------------------------------------- /single-number.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def singleNumber(self, nums: List[int]) -> int: 3 | n = nums[0] 4 | for num in nums[1:]: 5 | n = n ^ num 6 | return n -------------------------------------------------------------------------------- /a-number-after-a-double-reversal.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isSameAfterReversals(self, num: int) -> bool: 3 | if num == 0: 4 | return True 5 | return num % 10 != 0 -------------------------------------------------------------------------------- /climbing-stairs.py: -------------------------------------------------------------------------------- 1 | res = [1, 1, 2] 2 | 3 | for _ in range(43): 4 | res.append(res[-2] + res[-1]) 5 | 6 | class Solution: 7 | def climbStairs(self, n: int) -> int: 8 | return res[n] -------------------------------------------------------------------------------- /count-substrings-starting-and-ending-with-given-character.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def countSubstrings(self, s: str, c: str) -> int: 3 | k = s.count(c) 4 | return k * (k + 1) // 2 -------------------------------------------------------------------------------- /divisible-and-non-divisible-sums-difference.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def differenceOfSums(self, n: int, m: int) -> int: 3 | k = n // m 4 | return n * (n + 1) // 2 - m * k * (k + 1) -------------------------------------------------------------------------------- /find-words-containing-character.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findWordsContaining(self, words: List[str], x: str) -> List[int]: 3 | return [i for i in range(len(words)) if x in words[i]] -------------------------------------------------------------------------------- /fizz-buzz.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def fizzBuzz(self, n: int) -> List[str]: 3 | return [[str(i), "Fizz", "Buzz", "FizzBuzz"][2 * int(i % 5 == 0) + int(i % 3 == 0)] for i in range(1, n + 1)] -------------------------------------------------------------------------------- /jewels-and-stones.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def numJewelsInStones(self, jewels: str, stones: str) -> int: 3 | jewels = set(jewels) 4 | return len([c for c in stones if c in jewels]) -------------------------------------------------------------------------------- /maximum-xor-after-operations.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def maximumXOR(self, nums: List[int]) -> int: 3 | res = 0 4 | for el in nums: 5 | res |= el 6 | return res -------------------------------------------------------------------------------- /miscellaneous/A_-_wwwvvvvvv.py: -------------------------------------------------------------------------------- 1 | s = input() 2 | 3 | res = 0 4 | 5 | for c in s: 6 | if c == 'v': 7 | res += 1 8 | if c == 'w': 9 | res += 2 10 | 11 | print(res) -------------------------------------------------------------------------------- /miscellaneous/A_Probably_English.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | 3 | words = set(input().split()) 4 | 5 | print("Yes" if len(set.intersection(words, {"and", "not", "that", "the", "you"})) > 0 else "No") -------------------------------------------------------------------------------- /miscellaneous/A_Regular_Bracket_Sequences.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | 3 | for _ in range(t): 4 | n = int(input()) 5 | for k in range(n): 6 | print("(" * k + "()" * (n - k) + ")" * k) -------------------------------------------------------------------------------- /miscellaneous/A_Subtraction_Game.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | 3 | for _ in range(t): 4 | a, b = map(int, input().split()) 5 | if a > 1: 6 | print(1) 7 | else: 8 | print(b + 1) -------------------------------------------------------------------------------- /miscellaneous/B_A_A.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | 3 | res = 1 4 | 5 | while pow(res, res) < n: 6 | res += 1 7 | 8 | if pow(res, res) == n: 9 | print(res) 10 | else: 11 | print(-1) -------------------------------------------------------------------------------- /miscellaneous/L_shapes.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | 3 | for _ in range(t): 4 | m, n = map(int, input().split()) 5 | grid = [] 6 | for __ in range(m): 7 | grid.append(input()) 8 | -------------------------------------------------------------------------------- /miscellaneous/crystals.py: -------------------------------------------------------------------------------- 1 | a, b = map(int, input().split()) 2 | x, y, z = map(int, input().split()) 3 | 4 | violet = 2 * x + y 5 | red = y + 3 * z 6 | 7 | print(max(violet - a, 0) + max(red - b, 0)) -------------------------------------------------------------------------------- /sort-the-students-by-their-kth-score.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def sortTheStudents(self, score: List[List[int]], k: int) -> List[List[int]]: 3 | return sorted(score, key = lambda p: -p[k]) -------------------------------------------------------------------------------- /transform-array-by-parity.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def transformArray(self, nums: List[int]) -> List[int]: 3 | nums = [el % 2 for el in nums] 4 | nums.sort() 5 | return nums -------------------------------------------------------------------------------- /count-good-numbers.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def countGoodNumbers(self, n: int) -> int: 3 | M = 10 ** 9 + 7 4 | res = pow(4, n // 2, M) * pow(5, n - n // 2, M) 5 | return res % M -------------------------------------------------------------------------------- /miscellaneous/A_Infinite_Grid.py: -------------------------------------------------------------------------------- 1 | import math 2 | 3 | t = int(input()) 4 | 5 | for _ in range(t): 6 | n = int(input()) 7 | x = int(math.sqrt(n)) 8 | print(int(2 * (x + math.ceil(n / x)))) -------------------------------------------------------------------------------- /replace-all-digits-with-characters.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def replaceDigits(self, s: str) -> str: 3 | return "".join([chr(ord(s[i - 1]) + int(c)) if i & 1 else c for i, c in enumerate(s)]) -------------------------------------------------------------------------------- /reverse-string.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def reverseString(self, s: List[str]) -> None: 3 | n = len(s) 4 | for i in range(n // 2): 5 | s[i], s[n - i - 1] = s[n - i - 1], s[i] -------------------------------------------------------------------------------- /smallest-number-with-all-set-bits.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def smallestNumber(self, n: int) -> int: 3 | pw = 1 4 | while pw - 1 < n: 5 | pw *= 2 6 | return pw - 1 -------------------------------------------------------------------------------- /convert-the-temperature.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def convertTemperature(self, celsius: float) -> List[float]: 3 | f = 1.8 * celsius + 32.0 4 | k = 273.15 + celsius 5 | return [k, f] -------------------------------------------------------------------------------- /determine-if-two-events-have-conflict.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def haveConflict(self, event1: List[str], event2: List[str]) -> bool: 3 | return max(event1[0], event2[0]) <= min(event1[1], event2[1]) -------------------------------------------------------------------------------- /find-the-value-of-the-partition.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findValueOfPartition(self, nums: List[int]) -> int: 3 | nums.sort() 4 | return min(nums[i+1]-nums[i] for i in range(len(nums)-1)) -------------------------------------------------------------------------------- /maximum-odd-binary-number.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def maximumOddBinaryNumber(self, s: str) -> str: 3 | n = len(s) 4 | o = s.count('1') 5 | return '1' * (o - 1) + '0' * (n - o) + '1' -------------------------------------------------------------------------------- /miscellaneous/A_Sum.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | 3 | for _ in range(t): 4 | a, b, c = sorted(map(int, input().split())) 5 | if c == a + b: 6 | print("YES") 7 | else: 8 | print("NO") -------------------------------------------------------------------------------- /miscellaneous/G_2_Subsequence_Addition_Hard_Version.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | 3 | for _ in range(t): 4 | n = int(input()) 5 | arr = list(map(int, input().split())) 6 | arr.sort() 7 | print(arr) -------------------------------------------------------------------------------- /miscellaneous/topological.py: -------------------------------------------------------------------------------- 1 | def topological(graph): 2 | visited = {} 3 | stack = [] 4 | curr = list(graph.keys())[0] 5 | recstack = [] 6 | while len(stack) < len(graph.keys()): 7 | 8 | -------------------------------------------------------------------------------- /number-of-employees-who-met-the-target.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def numberOfEmployeesWhoMetTarget(self, hours: List[int], target: int) -> int: 3 | return len([el for el in hours if el >= target]) -------------------------------------------------------------------------------- /counting-bits.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def countBits(self, n: int) -> List[int]: 3 | ans = [0] 4 | for i in range(1, n + 1): 5 | ans.append(ans[i >> 1] + i % 2) 6 | return ans -------------------------------------------------------------------------------- /debounce.js: -------------------------------------------------------------------------------- 1 | var debounce = function(fn, t) { 2 | let ctr = null; 3 | return function(...args) { 4 | if (ctr) clearTimeout(ctr); 5 | ctr = setTimeout(() => fn(...args), t); 6 | } 7 | }; -------------------------------------------------------------------------------- /maximize-greatness-of-an-array.py: -------------------------------------------------------------------------------- 1 | from collections import Counter 2 | 3 | class Solution: 4 | def maximizeGreatness(self, nums: List[int]) -> int: 5 | return len(nums) - max(Counter(nums).values()) -------------------------------------------------------------------------------- /maximum-product-difference-between-two-pairs.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def maxProductDifference(self, nums: List[int]) -> int: 3 | nums.sort() 4 | return nums[-1] * nums[-2] - nums[0] * nums[1] -------------------------------------------------------------------------------- /miscellaneous/A_Coins.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | 3 | for _ in range(t): 4 | n, k = map(int, input().split()) 5 | if n & 1: 6 | print("YES" if k & 1 else "NO") 7 | else: 8 | print("YES") -------------------------------------------------------------------------------- /miscellaneous/A_Weekly_Records.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | 3 | arr = list(map(int, input().split())) 4 | 5 | res = [0] * n 6 | 7 | for i in range(7 * n): 8 | res[i // 7] += arr[i] 9 | 10 | print(*res) -------------------------------------------------------------------------------- /miscellaneous/Make_Multiple.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | 3 | for _ in range(t): 4 | a, b = map(int, input().split()) 5 | if b == a or b >= 2 * a: 6 | print("YES") 7 | else: 8 | print("NO") -------------------------------------------------------------------------------- /miscellaneous/Make_Square.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | 3 | for _ in range(t): 4 | a, b, c, d = map(int, input().split()) 5 | x = min(a, b, c, d) 6 | print(abs(a - x) + abs(b - x) + abs(c - x) + abs(d - x)) -------------------------------------------------------------------------------- /miscellaneous/mob_year.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | 3 | for _ in range(t): 4 | n, m, k, x = map(int, input().split()) 5 | x -= 1 6 | x = x % (k * n + m) 7 | print("YES" if x >= (k - 1) * n else "NO") -------------------------------------------------------------------------------- /missing-number.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def missingNumber(self, nums: List[int]) -> int: 3 | res = len(nums) 4 | for i, el in enumerate(nums): 5 | res ^= el ^ i 6 | return res -------------------------------------------------------------------------------- /find-the-integer-added-to-array-i.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def addedInteger(self, nums1: List[int], nums2: List[int]) -> int: 3 | nums2.sort() 4 | nums1.sort() 5 | return nums2[0] - nums1[0] -------------------------------------------------------------------------------- /find-the-original-array-of-prefix-xor.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findArray(self, pref: List[int]) -> List[int]: 3 | n = len(pref) 4 | return [pref[0]] + [pref[i] ^ pref[i - 1] for i in range(1, n)] -------------------------------------------------------------------------------- /miscellaneous/A_Colored_Balls_Revisited.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | 3 | for _ in range(t): 4 | n = int(input()) 5 | vals = list(map(int, input().split())) 6 | print(1 + max(range(n), key = lambda i: vals[i])) -------------------------------------------------------------------------------- /miscellaneous/A_N_choice_question.py: -------------------------------------------------------------------------------- 1 | n, a, b = map(int, input().split()) 2 | 3 | arr = list(map(int, input().split())) 4 | 5 | for i in range(n): 6 | if arr[i] == a + b: 7 | print(i + 1) 8 | break -------------------------------------------------------------------------------- /miscellaneous/Efficient_PAN_Linking.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | 3 | for _ in range(t): 4 | s = input() 5 | res = 0 6 | for c in s: 7 | res = 10 * res + int(c) 8 | res %= 20 9 | print(res) -------------------------------------------------------------------------------- /miscellaneous/Gymkhana_Election_IIIT_A.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | 3 | for _ in range(t): 4 | n, m = map(int, input().split()) 5 | res = m // 2 6 | if res <= m - res: 7 | res += 1 8 | print(res) -------------------------------------------------------------------------------- /miscellaneous/Remove_Multiples.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | 3 | for _ in range(t): 4 | n, m = map(int, input().split()) 5 | res = n * (n + 1) // 2 6 | res -= sum(map(int, input().split())) 7 | print(res) -------------------------------------------------------------------------------- /miscellaneous/Remove_Two.py: -------------------------------------------------------------------------------- 1 | from collections import Counter 2 | 3 | n = int(input()) 4 | 5 | arr = list(map(int, input().split())) 6 | 7 | ctr = Counter(arr) 8 | 9 | arr = sorted(ctr.items()) 10 | 11 | -------------------------------------------------------------------------------- /miscellaneous/div_of_div.py: -------------------------------------------------------------------------------- 1 | def divofdiv(n): 2 | for i in range(1, n + 1): 3 | if n % i == 0: 4 | k = n // i 5 | print(i, k) 6 | 7 | print(divofdiv(4)) 8 | print(divofdiv(5)) -------------------------------------------------------------------------------- /miscellaneous/factmod.py: -------------------------------------------------------------------------------- 1 | def factmod(n): 2 | fact = 1 3 | for i in range(2, n + 1): 4 | fact = (fact * i) % (n * n) 5 | return fact 6 | 7 | print([i for i in range(1, 1001) if factmod(i) != 0]) 8 | -------------------------------------------------------------------------------- /miscellaneous/interesting_sum.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | 3 | for _ in range(t): 4 | n = int(input()) 5 | arr = list(map(int, input().split())) 6 | arr.sort() 7 | print(arr[-1] + arr[-2] - arr[0] - arr[1]) -------------------------------------------------------------------------------- /power-of-three.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isPowerOfThree(self, n: int) -> bool: 3 | if n <= 0: 4 | return False 5 | while n % 3 == 0: 6 | n = n // 3 7 | return n == 1 -------------------------------------------------------------------------------- /running-sum-of-1d-array.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def runningSum(self, nums: List[int]) -> List[int]: 3 | op = [0] 4 | for num in nums: 5 | op.append(op[-1] + num) 6 | return op[1:] -------------------------------------------------------------------------------- /adding-spaces-to-a-string.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def addSpaces(self, s: str, spaces: List[int]) -> str: 3 | s = list(s) 4 | for i in spaces: 5 | s[i] = " " + s[i] 6 | return "".join(s) -------------------------------------------------------------------------------- /check-balanced-string.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isBalanced(self, num: str) -> bool: 3 | s = [0, 0] 4 | for i in range(len(num)): 5 | s[i % 2] += int(num[i]) 6 | return s[0] == s[1] -------------------------------------------------------------------------------- /find-target-indices-after-sorting-array.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def targetIndices(self, nums: List[int], target: int) -> List[int]: 3 | nums.sort() 4 | return [i for i, v in enumerate(nums) if v == target] -------------------------------------------------------------------------------- /minimum-cuts-to-divide-a-circle.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def numberOfCuts(self, n: int) -> int: 3 | if n == 1: 4 | return 0 5 | if n % 2 == 0: 6 | return n // 2 7 | return n -------------------------------------------------------------------------------- /miscellaneous/Aloy_Search_Party.py: -------------------------------------------------------------------------------- 1 | import collections 2 | i=input 3 | t=int(i()) 4 | while t: 5 | n,t=int(i()),t-1 6 | v=collections.Counter([i() for _ in range(n)]) 7 | print(sum(v[x]*(v[x]-1)//2 for x in v)) -------------------------------------------------------------------------------- /miscellaneous/Distinct_Neighbours.py: -------------------------------------------------------------------------------- 1 | from collections import Counter 2 | 3 | t = int(input()) 4 | 5 | for _ in range(t): 6 | n = int(input()) 7 | arr = list(map(int, input().split())) 8 | arr.sort() 9 | -------------------------------------------------------------------------------- /miscellaneous/chip_game.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | 3 | for _ in range(t): 4 | n, m = map(int, input().split()) 5 | if (n % 2 == 1) ^ (m % 2 == 1): 6 | print("Burenka") 7 | else: 8 | print("Tonya") -------------------------------------------------------------------------------- /n-ary-tree-postorder-traversal.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def postorder(self, root: 'Node') -> List[int]: 3 | o = lambda x: [] if not x else sum([o(c) for c in x.children], []) + [x.val] 4 | return o(root) -------------------------------------------------------------------------------- /rearrange-words-in-a-sentence.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def arrangeWords(self, text: str) -> str: 3 | op = " ".join(sorted(text.lower().split(), key = lambda s: len(s))) 4 | return op[0].upper() + op[1:] -------------------------------------------------------------------------------- /sum-of-digits-in-base-k.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def sumBase(self, n: int, k: int) -> int: 3 | total = 0 4 | while n > 0: 5 | total += n % k 6 | n = n // k 7 | return total -------------------------------------------------------------------------------- /three-divisors.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isThree(self, n: int) -> bool: 3 | ctr = 0 4 | for i in range(1, n + 1): 5 | if n % i == 0: 6 | ctr += 1 7 | return ctr == 3 -------------------------------------------------------------------------------- /xor-operation-in-an-array.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def xorOperation(self, n: int, start: int) -> int: 3 | op = start 4 | for i in range(1, n): 5 | op = op ^ (start + 2 * i) 6 | return op -------------------------------------------------------------------------------- /calculate-money-in-leetcode-bank.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def totalMoney(self, n: int) -> int: 3 | k = n // 7 4 | rem = n % 7 5 | return 28 * k + 7 * k * (k - 1) // 2 + k * rem + rem * (rem + 1) // 2 -------------------------------------------------------------------------------- /decode-xored-array.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def decode(self, encoded: List[int], first: int) -> List[int]: 3 | op = [first] 4 | for el in encoded: 5 | op.append(op[-1] ^ el) 6 | return op -------------------------------------------------------------------------------- /element-appearing-more-than-25-in-sorted-array.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findSpecialInteger(self, arr: List[int]) -> int: 3 | ctr = Counter(arr) 4 | return min([el for el in ctr if ctr[el] * 4 > len(arr)]) -------------------------------------------------------------------------------- /largest-positive-integer-that-exists-with-its-negative.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findMaxK(self, nums: List[int]) -> int: 3 | exists = set(nums) 4 | return max([num for num in nums if -num in exists] + [-1]) -------------------------------------------------------------------------------- /merge-sorted-array.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def merge(self, nums1: List[int], m: int, nums2: List[int], n: int) -> None: 3 | for i in range(1, n + 1): 4 | nums1[-i] = nums2.pop() 5 | nums1.sort() -------------------------------------------------------------------------------- /minimum-number-of-operations-to-make-array-xor-equal-to-k.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def minOperations(self, nums: List[int], k: int) -> int: 3 | for el in nums: 4 | k ^= el 5 | return k.bit_count() -------------------------------------------------------------------------------- /miscellaneous/A_-_-_1.py: -------------------------------------------------------------------------------- 1 | n, x = map(int, input().split()) 2 | 3 | arr = list(map(int, input().split())) 4 | 5 | res = -1 6 | 7 | for i in range(n): 8 | if arr[i] == x: 9 | res = i + 1 10 | 11 | print(res) -------------------------------------------------------------------------------- /miscellaneous/A_Banis_and_Cards.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | 3 | res = [] 4 | 5 | for _ in range(t): 6 | n, m = map(int, input().split()) 7 | k = n // m 8 | res.append(m * k * (k + 1) // 2) 9 | 10 | print(*res) -------------------------------------------------------------------------------- /miscellaneous/A_Divisible_Array.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | 3 | for _ in range(t): 4 | n = int(input()) 5 | if n & 1: 6 | print(*list(range(1, n + 1))) 7 | else: 8 | print(*list(range(2, 2 * n + 1, 2))) -------------------------------------------------------------------------------- /miscellaneous/lame_king.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | 3 | for _ in range(t): 4 | x, y = map(int, input().split()) 5 | k = max(abs(x), abs(y)) - min(abs(x), abs(y)) 6 | print(2 * min(abs(x), abs(y)) + k + max(k - 1, 0)) -------------------------------------------------------------------------------- /sum-multiples.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def sumOfMultiples(self, n: int) -> int: 3 | val = lambda k: k * (n // k) * (n // k + 1) // 2 4 | return val(3) + val(5) + val(7) - val(15) - val(21) - val(35) + val(105) -------------------------------------------------------------------------------- /convert-date-to-binary.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def convertDateToBinary(self, date: str) -> str: 3 | y, m, d = map(int, date.split("-")) 4 | b = lambda x: "{:0b}".format(x) 5 | return f"{b(y)}-{b(m)}-{b(d)}" -------------------------------------------------------------------------------- /count-subarrays-of-length-three-with-a-condition.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def countSubarrays(self, nums: List[int]) -> int: 3 | return sum(int(nums[i] == 2 * nums[i - 1] + 2 * nums[i + 1]) for i in range(1, len(nums) - 1)) -------------------------------------------------------------------------------- /largest-odd-number-in-string.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def largestOddNumber(self, num: str) -> str: 3 | i = len(num) 4 | while i >= 1 and int(num[i - 1]) % 2 == 0: 5 | i -= 1 6 | return num[:i] -------------------------------------------------------------------------------- /maximum-unique-subarray-sum-after-deletion.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def maxSum(self, nums: List[int]) -> int: 3 | if max(nums) < 0: 4 | return max(nums) 5 | return sum(el for el in set(nums) if el >= 0) -------------------------------------------------------------------------------- /miscellaneous/A_Musical_Puzzle.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | 3 | for _ in range(t): 4 | n = int(input()) 5 | s = input() 6 | chars = set() 7 | for i in range(n - 1): 8 | chars.add(s[i:i+2]) 9 | print(len(chars)) -------------------------------------------------------------------------------- /miscellaneous/A_Yet_Another_Promotion.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | 3 | for _ in range(t): 4 | a, b = map(int, input().split()) 5 | n, m = map(int, input().split()) 6 | print(m * a * (n // (m + 1)) + b * (n - n // (m + 1))) -------------------------------------------------------------------------------- /miscellaneous/B_Death_s_Blessing.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | 3 | for _ in range(t): 4 | n = int(input()) 5 | a = list(map(int, input().split())) 6 | b = list(map(int, input().split())) 7 | print(sum(a) + sum(b) - max(b)) -------------------------------------------------------------------------------- /miscellaneous/B_Increasing.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | 3 | for _ in range(t): 4 | n = int(input()) 5 | arr = set(map(int, input().split())) 6 | if n == len(arr): 7 | print("YES") 8 | else: 9 | print("NO") -------------------------------------------------------------------------------- /miscellaneous/less_or_equal.py: -------------------------------------------------------------------------------- 1 | n, k = map(int, input().split()) 2 | 3 | arr = list(map(int, input().split())) 4 | 5 | arr.sort() 6 | 7 | if arr.count(arr[k - 1]) == 1: 8 | print(arr[k - 1]) 9 | else: 10 | print(-1) -------------------------------------------------------------------------------- /report-spam-message.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def reportSpam(self, message: List[str], bannedWords: List[str]) -> bool: 3 | bannedWords = set(bannedWords) 4 | return len([w for w in message if w in bannedWords]) >= 2 -------------------------------------------------------------------------------- /strictly-palindromic-number.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isStrictlyPalindromic(self, n: int) -> bool: 3 | f = open("user.out", "w") 4 | for _ in range(67): 5 | print("false", file = f) 6 | exit(0) -------------------------------------------------------------------------------- /sum-of-unique-elements.py: -------------------------------------------------------------------------------- 1 | from collections import Counter 2 | 3 | class Solution: 4 | def sumOfUnique(self, nums: List[int]) -> int: 5 | ctr = Counter(nums) 6 | return sum([k for k, v in ctr.items() if v == 1]) -------------------------------------------------------------------------------- /find-indices-of-stable-mountains.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def stableMountains(self, height: List[int], threshold: int) -> List[int]: 3 | n = len(height) 4 | return [i for i in range(1, n) if height[i - 1] > threshold] -------------------------------------------------------------------------------- /find-the-child-who-has-the-ball-after-k-seconds.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def numberOfChild(self, n: int, k: int) -> int: 3 | k %= 2 * (n - 1) 4 | if k <= n - 1: 5 | return k 6 | return 2 * (n - 1) - k -------------------------------------------------------------------------------- /first-letter-to-appear-twice.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def repeatedCharacter(self, s: str) -> str: 3 | seen = set() 4 | for c in s: 5 | if c in seen: 6 | return c 7 | seen.add(c) -------------------------------------------------------------------------------- /height-checker.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def heightChecker(self, heights: List[int]) -> int: 3 | n = len(heights) 4 | expected = sorted(heights) 5 | return len([i for i in range(n) if heights[i] != expected[i]]) -------------------------------------------------------------------------------- /kids-with-the-greatest-number-of-candies.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def kidsWithCandies(self, candies: List[int], extraCandies: int) -> List[bool]: 3 | mx = max(candies) 4 | return [c + extraCandies >= mx for c in candies] -------------------------------------------------------------------------------- /miscellaneous/A_Two_Groups.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | 3 | for _ in range(t): 4 | n = int(input()) 5 | arr = list(map(int, input().split())) 6 | k = min(arr, key = abs) 7 | print(max(abs(sum(arr) - k) - abs(k), abs(sum(arr)))) -------------------------------------------------------------------------------- /miscellaneous/D_Factorial_Divisibility.py: -------------------------------------------------------------------------------- 1 | import math 2 | 3 | n, x = map(int, input().split()) 4 | 5 | arr = list(map(int, input().split())) 6 | 7 | arr.sort() 8 | 9 | rem = 0 10 | 11 | for i in range(2, x + 1): 12 | -------------------------------------------------------------------------------- /super-pow.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def superPow(self, a: int, b: List[int]) -> int: 3 | res = 1 4 | for d in b: 5 | res = pow(res, 10, 1337) * pow(a, d, 1337) 6 | res %= 1337 7 | return res -------------------------------------------------------------------------------- /the-two-sneaky-numbers-of-digitville.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def getSneakyNumbers(self, nums: List[int]) -> List[int]: 3 | n = len(nums) - 2 4 | for i in range(n): 5 | nums.remove(i) 6 | return nums -------------------------------------------------------------------------------- /excel-sheet-column-number.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def titleToNumber(self, columnTitle: str) -> int: 3 | total = 0 4 | for c in columnTitle: 5 | total = 26 * total + ord(c) - ord('A') + 1 6 | return total -------------------------------------------------------------------------------- /find-lucky-integer-in-an-array.py: -------------------------------------------------------------------------------- 1 | from collections import Counter 2 | 3 | class Solution: 4 | def findLucky(self, arr: List[int]) -> int: 5 | ctr = Counter(arr) 6 | return max([k for k, v in ctr.items() if k == v] + [-1]) -------------------------------------------------------------------------------- /find-the-peaks.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findPeaks(self, mountain: List[int]) -> List[int]: 3 | n = len(mountain) 4 | return [i for i in range(1, n - 1) if mountain[i] > mountain[i - 1] and mountain[i] > mountain[i + 1]] -------------------------------------------------------------------------------- /longest-happy-prefix.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def longestPrefix(self, s: str) -> str: 3 | n = len(s) 4 | for l in range(n - 1, 0, -1): 5 | if s[:l] == s[-l:]: 6 | return s[:l] 7 | return "" -------------------------------------------------------------------------------- /managers-with-at-least-5-direct-reportsundefined: -------------------------------------------------------------------------------- 1 | -- Write your PostgreSQL query statement below 2 | SELECT empA.name FROM Employee AS empA LEFT JOIN Employee AS empB ON empA.id = empB.managerId GROUP BY empA.id, empA.name HAVING COUNT(*) >= 5; -------------------------------------------------------------------------------- /minimum-operations-to-make-array-values-equal-to-k.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def minOperations(self, nums: List[int], k: int) -> int: 3 | if k > min(nums): 4 | return -1 5 | return len(set(x for x in nums if x > k)) -------------------------------------------------------------------------------- /miscellaneous/A_Indirect_Sort.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | 3 | for _ in range(t): 4 | n = int(input()) 5 | arr = list(map(int, input().split())) 6 | if arr[0] == min(arr): 7 | print("Yes") 8 | else: 9 | print("No") -------------------------------------------------------------------------------- /miscellaneous/B_3_smooth_Numbers.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | 3 | x = y = 0 4 | 5 | while n % 2 == 0: 6 | n //= 2 7 | x += 1 8 | 9 | while n % 3 == 0: 10 | n //= 3 11 | y += 1 12 | 13 | print("Yes" if n == 1 else "No") -------------------------------------------------------------------------------- /miscellaneous/MIN_To_MAX.py: -------------------------------------------------------------------------------- 1 | from collections import Counter 2 | 3 | t = int(input()) 4 | 5 | for _ in range(t): 6 | n = int(input()) 7 | arr = list(map(int, input().split())) 8 | M = min(arr) 9 | print(n - Counter(arr)[M]) -------------------------------------------------------------------------------- /palindrome-number.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isPalindrome(self, x: int) -> bool: 3 | xx = x 4 | nx = 0 5 | while x > 0: 6 | nx = 10 * nx + x % 10 7 | x = x // 10 8 | return xx == nx -------------------------------------------------------------------------------- /power-of-four.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isPowerOfFour(self, n: int) -> bool: 3 | if n <= 0: 4 | return False 5 | if n == 1: 6 | return True 7 | return not n & 3 and self.isPowerOfFour(n >> 2) -------------------------------------------------------------------------------- /power-of-two.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isPowerOfTwo(self, n: int) -> bool: 3 | if n <= 0: 4 | return False 5 | if n == 1: 6 | return True 7 | return not (n & 1) and self.isPowerOfTwo(n >> 1) -------------------------------------------------------------------------------- /reverse-prefix-of-word.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def reversePrefix(self, word: str, ch: str) -> str: 3 | try: 4 | i = word.index(ch) 5 | except: 6 | i = 0 7 | return word[:i+1][::-1] + word[i+1:] -------------------------------------------------------------------------------- /add-digits.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def addDigits(self, num: int) -> int: 3 | k = num % 9 4 | if k == 0: 5 | if num == 0: 6 | return 0 7 | return 9 8 | else: 9 | return k -------------------------------------------------------------------------------- /fibonacci-number.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def fib(self, n: int) -> int: 3 | if n == 0: 4 | return 0 5 | a = 0 6 | b = 1 7 | for _ in range(n - 1): 8 | a, b = b, a + b 9 | return b -------------------------------------------------------------------------------- /find-first-palindromic-string-in-the-array.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def firstPalindrome(self, words: List[str]) -> str: 3 | for word in words: 4 | if word == word[::-1]: 5 | return word 6 | return "" -------------------------------------------------------------------------------- /find-the-pivot-integer.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def pivotInteger(self, n: int) -> int: 3 | p = n * (n + 1) // 2 4 | for i in range(n + 1): 5 | if i * (i + 1) == i + p: 6 | return i 7 | return -1 -------------------------------------------------------------------------------- /find-the-winner-of-the-circular-game.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findTheWinner(self, n: int, k: int) -> int: 3 | res = 0 4 | for x in range(2, n + 1): 5 | res += k 6 | res %= x 7 | return res + 1 -------------------------------------------------------------------------------- /is-subsequence.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isSubsequence(self, s: str, t: str) -> bool: 3 | n = len(s) 4 | i = 0 5 | for c in t: 6 | if i < n and s[i] == c: 7 | i += 1 8 | return i == n -------------------------------------------------------------------------------- /minimum-common-value.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def getCommon(self, nums1: List[int], nums2: List[int]) -> int: 3 | common = set(nums1) & set(nums2) 4 | if len(common) == 0: 5 | return -1 6 | return min(common) -------------------------------------------------------------------------------- /minimum-elements-to-add-to-form-a-given-sum.py: -------------------------------------------------------------------------------- 1 | import math 2 | 3 | class Solution: 4 | def minElements(self, nums: List[int], limit: int, goal: int) -> int: 5 | diff = goal - sum(nums) 6 | return math.ceil(abs(diff) / limit) -------------------------------------------------------------------------------- /miscellaneous/A_Love_Story.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | 3 | x = "codeforces" 4 | 5 | for _ in range(t): 6 | s = input() 7 | res = 0 8 | for i in range(10): 9 | if s[i] != x[i]: 10 | res += 1 11 | print(res) -------------------------------------------------------------------------------- /miscellaneous/A_New_Palindrome.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | 3 | for _ in range(t): 4 | s = input() 5 | n = len(s) 6 | k = n // 2 7 | a = s[:k] 8 | b = s[-k:] 9 | print("YES" if sorted(a) != sorted(b, reverse = True) else "NO") -------------------------------------------------------------------------------- /miscellaneous/E_Drawings.py: -------------------------------------------------------------------------------- 1 | n, k = map(int, input().split()) 2 | 3 | ctr = [0] * k 4 | 5 | for i in range(n): 6 | ctr[i % k] += 1 7 | 8 | res = 0 9 | 10 | for i in range(k): 11 | res += (n - i - 1) * ctr[i] 12 | 13 | print(res) -------------------------------------------------------------------------------- /miscellaneous/line_sensor.py: -------------------------------------------------------------------------------- 1 | m, n = map(int, input().split()) 2 | 3 | ctr = [0] * n 4 | 5 | for _ in range(m): 6 | s = input() 7 | for i in range(n): 8 | if s[i] == "#": 9 | ctr[i] += 1 10 | 11 | print(*ctr) -------------------------------------------------------------------------------- /sort-the-people.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def sortPeople(self, names: List[str], heights: List[int]) -> List[str]: 3 | n = len(names) 4 | res = sorted(range(n), key = lambda i: -heights[i]) 5 | return [names[i] for i in res] -------------------------------------------------------------------------------- /special-array-i.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isArraySpecial(self, nums: List[int]) -> bool: 3 | for i in range(len(nums) - 1): 4 | if (nums[i] + nums[i + 1]) % 2 == 0: 5 | return False 6 | return True -------------------------------------------------------------------------------- /find-three-consecutive-integers-that-sum-to-a-given-number.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def sumOfThree(self, num: int) -> List[int]: 3 | if num % 3 == 0: 4 | k = num // 3 5 | return [k - 1, k, k + 1] 6 | return [] -------------------------------------------------------------------------------- /first-missing-positive.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def firstMissingPositive(self, nums: List[int]) -> int: 3 | nums.sort() 4 | x = 1 5 | for el in nums: 6 | if el == x: 7 | x += 1 8 | return x -------------------------------------------------------------------------------- /k-items-with-the-maximum-sum.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def kItemsWithMaximumSum(self, numOnes: int, numZeros: int, numNegOnes: int, k: int) -> int: 3 | vals = [1] * numOnes + [0] * numZeros + [-1] * numNegOnes 4 | return sum(vals[:k]) -------------------------------------------------------------------------------- /keep-multiplying-found-values-by-two.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findFinalValue(self, nums: List[int], original: int) -> int: 3 | nums = set(nums) 4 | while original in nums: 5 | original*= 2 6 | return original -------------------------------------------------------------------------------- /last-substring-in-lexicographical-order.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def lastSubstring(self, s: str) -> str: 3 | n = len(s) 4 | maxSub = "" 5 | for i in range(n): 6 | maxSub = max(maxSub, s[i:]) 7 | return maxSub -------------------------------------------------------------------------------- /maximum-product-of-two-elements-in-an-array.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def maxProduct(self, nums: List[int]) -> int: 3 | a, b = 0, 0 4 | for el in nums: 5 | temp, a, b = sorted([a, b, el]) 6 | return (a - 1) * (b - 1) -------------------------------------------------------------------------------- /minimum-sum-of-four-digit-number-after-splitting-digits.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def minimumSum(self, num: int) -> int: 3 | digits = sorted([int(d) for d in str(num)]) 4 | return 10 * (digits[0] + digits[1]) + (digits[2] + digits[3]) -------------------------------------------------------------------------------- /miscellaneous/A_Contest_Result.py: -------------------------------------------------------------------------------- 1 | n, m = map(int, input().split()) 2 | 3 | a = list(map(int, input().split())) 4 | b = list(map(int, input().split())) 5 | 6 | res = 0 7 | 8 | for i in range(m): 9 | res += a[b[i] - 1] 10 | 11 | print(res) -------------------------------------------------------------------------------- /miscellaneous/A_Same.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | 3 | arr = list(map(int, input().split())) 4 | 5 | same = True 6 | 7 | for el in arr: 8 | if el != arr[0]: 9 | same = False 10 | break 11 | 12 | print("Yes" if same else "No") -------------------------------------------------------------------------------- /miscellaneous/B_Card_Trick.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | 3 | for _ in range(t): 4 | n = int(input()) 5 | arr = list(map(int, input().split())) 6 | m = int(input()) 7 | shuffles = map(int, input().split()) 8 | print(arr[sum(shuffles) % n]) -------------------------------------------------------------------------------- /miscellaneous/B_Conveyor_Belts.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | 3 | for _ in range(t): 4 | n, a, b, c, d = map(int, input().split()) 5 | beg = min(a, b, n + 1 - a, n + 1 - b) 6 | end = min(c, d, n + 1 - c, n + 1 - d) 7 | print(abs(beg - end)) -------------------------------------------------------------------------------- /miscellaneous/Equal_Elements.py: -------------------------------------------------------------------------------- 1 | from collections import Counter 2 | 3 | t = int(input()) 4 | 5 | for _ in range(t): 6 | n = int(input()) 7 | arr = list(map(int, input().split())) 8 | ctr = Counter(arr) 9 | print(n - max(ctr.values())) -------------------------------------------------------------------------------- /miscellaneous/bigram.py: -------------------------------------------------------------------------------- 1 | from collections import defaultdict 2 | 3 | n = int(input()) 4 | s = input() 5 | 6 | ctr = defaultdict(int) 7 | 8 | for i in range(n - 1): 9 | ctr[s[i:i+2]] += 1 10 | 11 | print(max(ctr, key = lambda c: ctr[c])) -------------------------------------------------------------------------------- /permutation-difference-between-two-strings.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findPermutationDifference(self, s: str, t: str) -> int: 3 | res = 0 4 | for i in range(len(s)): 5 | res += abs(i - t.index(s[i])) 6 | return res -------------------------------------------------------------------------------- /smallest-index-with-equal-value.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def smallestEqual(self, nums: List[int]) -> int: 3 | n = len(nums) 4 | for i in range(n): 5 | if i % 10 == nums[i]: 6 | return i 7 | return -1 -------------------------------------------------------------------------------- /sort-array-by-increasing-frequency.py: -------------------------------------------------------------------------------- 1 | from collections import Counter 2 | 3 | class Solution: 4 | def frequencySort(self, nums: List[int]) -> List[int]: 5 | ctr = Counter(nums) 6 | return sorted(nums, key = lambda x: (ctr[x], -x)) -------------------------------------------------------------------------------- /check-if-bitwise-or-has-trailing-zeros.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def hasTrailingZeros(self, nums: List[int]) -> bool: 3 | even = 0 4 | for el in nums: 5 | if el % 2 == 0: 6 | even += 1 7 | return even >= 2 -------------------------------------------------------------------------------- /count-elements-with-strictly-smaller-and-greater-elements.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def countElements(self, nums: List[int]) -> int: 3 | sm = min(nums) 4 | mx = max(nums) 5 | return len([num for num in nums if num > sm and num < mx]) -------------------------------------------------------------------------------- /counting-words-with-a-given-prefix.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def prefixCount(self, words: List[str], pref: str) -> int: 3 | res = 0 4 | for w in words: 5 | if w.startswith(pref): 6 | res += 1 7 | return res -------------------------------------------------------------------------------- /first-unique-character-in-a-string.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def firstUniqChar(self, s: str) -> int: 3 | n = len(s) 4 | for i in range(n): 5 | if s[i] not in s[:i] + s[i + 1:]: 6 | return i 7 | return -1 -------------------------------------------------------------------------------- /majority-element-ii.py: -------------------------------------------------------------------------------- 1 | from collections import Counter 2 | 3 | class Solution: 4 | def majorityElement(self, nums: List[int]) -> List[int]: 5 | n = len(nums) 6 | ctr = Counter(nums) 7 | return [el for el in ctr if 3 * ctr[el] > n] -------------------------------------------------------------------------------- /minimum-operations-to-exceed-threshold-value-i.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def minOperations(self, nums: List[int], k: int) -> int: 3 | res = 0 4 | for el in nums: 5 | if el < k: 6 | res += 1 7 | return res -------------------------------------------------------------------------------- /miscellaneous/A_Buttons.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | 3 | for _ in range(t): 4 | a, b, c = map(int, input().split()) 5 | if c % 2 == 0: 6 | print("First" if a > b else "Second") 7 | else: 8 | print("First" if a + 1 > b else "Second") -------------------------------------------------------------------------------- /miscellaneous/B_The_Middle_Day.py: -------------------------------------------------------------------------------- 1 | m = int(input()) 2 | 3 | d = list(map(int, input().split())) 4 | 5 | x = (sum(d) + 1) // 2 6 | 7 | i = 0 8 | 9 | while i < len(d) and x - d[i] > 0: 10 | x -= d[i] 11 | i += 1 12 | 13 | print(i + 1, x) -------------------------------------------------------------------------------- /miscellaneous/Sorted_Substrings.py: -------------------------------------------------------------------------------- 1 | t =int(input()) 2 | 3 | for _ in range(t): 4 | n = int(input()) 5 | s = input() 6 | res = 0 7 | for i in range(n - 1): 8 | if s[i] > s[i + 1]: 9 | res += 1 10 | print(res) -------------------------------------------------------------------------------- /miscellaneous/woeful_permutations.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | 3 | for _ in range(t): 4 | n = int(input()) 5 | nums = list(range(1, n + 1)) 6 | for i in range(n - 2, -1, -2): 7 | nums[i], nums[i + 1] = nums[i + 1], nums[i] 8 | print(*nums) -------------------------------------------------------------------------------- /rabbits-in-forest.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def numRabbits(self, answers: List[int]) -> int: 3 | ctr = Counter(answers) 4 | res = 0 5 | for x in ctr: 6 | res += math.ceil(ctr[x] / (x + 1)) * (x + 1) 7 | return res -------------------------------------------------------------------------------- /top-k-frequent-words.py: -------------------------------------------------------------------------------- 1 | from collections import Counter 2 | 3 | class Solution: 4 | def topKFrequent(self, words: List[str], k: int) -> List[str]: 5 | ctr = Counter(words) 6 | return sorted(ctr.keys(), key = lambda w: (-ctr[w], w))[:k] -------------------------------------------------------------------------------- /convert-integer-to-the-sum-of-two-no-zero-integers.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def getNoZeroIntegers(self, n: int) -> List[int]: 3 | for a in range(1, n): 4 | if "0" not in str(a) and "0" not in str(n - a): 5 | return [a, n - a] -------------------------------------------------------------------------------- /determine-if-a-cell-is-reachable-at-a-given-time.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isReachableAtTime(self, sx: int, sy: int, fx: int, fy: int, t: int) -> bool: 3 | dist = max(abs(sx - fx), abs(sy - fy)) 4 | return t >= dist if dist > 0 else t != 1 -------------------------------------------------------------------------------- /faulty-keyboard.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def finalString(self, s: str) -> str: 3 | res = "" 4 | for c in s: 5 | if c == "i": 6 | res = res[::-1] 7 | else: 8 | res += c 9 | return res -------------------------------------------------------------------------------- /maximum-area-of-longest-diagonal-rectangle.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def areaOfMaxDiagonal(self, dimensions: List[List[int]]) -> int: 3 | res = max(dimensions, key = lambda p: (p[0] * p[0] + p[1] * p[1], p[0] * p[1])) 4 | return res[0] * res[1] -------------------------------------------------------------------------------- /minimum-one-bit-operations-to-make-integers-zero.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def minimumOneBitOperations(self, n: int) -> int: 3 | res = 0 4 | while n: 5 | res = -res - (n ^ (n - 1)) 6 | n &= n - 1 7 | return abs(res) -------------------------------------------------------------------------------- /miscellaneous/A_Arithmetic_Array.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | 3 | for _ in range(t): 4 | n = int(input()) 5 | arr = list(map(int, input().split())) 6 | d = sum(arr) - n 7 | if d < 0: 8 | print(1) 9 | else: 10 | print(d) -------------------------------------------------------------------------------- /miscellaneous/A_Password.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | 3 | for _ in range(t): 4 | n = int(input()) 5 | unused = set(map(int, input().split())) 6 | used = set([i for i in range(10) if i not in unused]) 7 | k = len(used) 8 | print(k * (k - 1) * 3) -------------------------------------------------------------------------------- /miscellaneous/B_Cunnithm.py: -------------------------------------------------------------------------------- 1 | n, m = map(int, input().split()) 2 | 3 | points = [] 4 | 5 | for _ in range(n): 6 | l, r = map(int, input().split()) 7 | points.append((l, 1)) 8 | points.append((r + 1, -1)) 9 | 10 | points.sort() 11 | 12 | -------------------------------------------------------------------------------- /miscellaneous/C_Complementary_XOR.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | 3 | for _ in range(t): 4 | n = int(input()) 5 | a = input() 6 | b = input() 7 | gsum = lambda s: sum(int(d) for d in s) 8 | asum, bsum = gsum(a), gsum(b) 9 | print(asum + bsum, n) -------------------------------------------------------------------------------- /miscellaneous/C_Socks.py: -------------------------------------------------------------------------------- 1 | from collections import Counter 2 | 3 | n = int(input()) 4 | 5 | arr = list(map(int, input().split())) 6 | 7 | ctr = Counter(arr) 8 | 9 | res = 0 10 | 11 | for el in ctr: 12 | res += ctr[el] // 2 13 | 14 | print(res) -------------------------------------------------------------------------------- /miscellaneous/String_Game.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | 3 | for _ in range(t): 4 | n = int(input()) 5 | s = input() 6 | k = min(s.count("0"), s.count("1")) 7 | if k % 2 == 0: 8 | print("Ramos") 9 | else: 10 | print("Zlatan") -------------------------------------------------------------------------------- /reverse-linked-list.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]: 3 | prev, curr = None, head 4 | while curr: 5 | curr.next, prev, curr = prev, curr, curr.next 6 | return prev -------------------------------------------------------------------------------- /sorting-the-sentence.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def sortSentence(self, s: str) -> str: 3 | ogwords = s.split() 4 | words = ogwords[:] 5 | for w in ogwords: 6 | words[int(w[-1]) - 1] = w[:-1] 7 | return " ".join(words) -------------------------------------------------------------------------------- /sqrtx.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def mySqrt(self, x: int) -> int: 3 | sqrt = 0 4 | for p in range(20, -1, -1): 5 | pw = 1 << p 6 | if (sqrt + pw) * (sqrt + pw) <= x: 7 | sqrt += pw 8 | return sqrt -------------------------------------------------------------------------------- /check-if-all-characters-have-equal-number-of-occurrences.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def areOccurrencesEqual(self, s: str) -> bool: 3 | ctr = {} 4 | for c in s: 5 | ctr[c] = ctr.get(c, 0) + 1 6 | return len(set(ctr.values())) == 1 -------------------------------------------------------------------------------- /custom-sort-string.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def customSortString(self, order: str, s: str) -> str: 3 | pos = {} 4 | for i in range(len(order)): 5 | pos[order[i]] = i 6 | return "".join(sorted(s, key = lambda x: pos.get(x, -1))) -------------------------------------------------------------------------------- /find-if-digit-game-can-be-won.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def canAliceWin(self, nums: List[int]) -> bool: 3 | s = sum(nums) 4 | ss = 0 5 | for el in nums: 6 | if el < 10: 7 | ss += el 8 | return s - ss != ss -------------------------------------------------------------------------------- /find-minimum-operations-to-make-all-elements-divisible-by-three.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def minimumOperations(self, nums: List[int]) -> int: 3 | res = 0 4 | for el in nums: 5 | res += min((el % 3), 3 - (el % 3)) 6 | return res -------------------------------------------------------------------------------- /find-the-difference-of-two-arrays.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findDifference(self, nums1: List[int], nums2: List[int]) -> List[List[int]]: 3 | nums1 = set(nums1) 4 | nums2 = set(nums2) 5 | return [list(nums1 - nums2), list(nums2 - nums1)] -------------------------------------------------------------------------------- /find-the-highest-altitude.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def largestAltitude(self, gain: List[int]) -> int: 3 | maxval = 0 4 | curr = 0 5 | for g in gain: 6 | curr += g 7 | maxval = max(maxval, curr) 8 | return maxval -------------------------------------------------------------------------------- /h-index-ii.py: -------------------------------------------------------------------------------- 1 | import bisect 2 | 3 | class Solution: 4 | def hIndex(self, citations: List[int]) -> int: 5 | n = len(citations) 6 | for h in range(n, -1, -1): 7 | if h <= n - bisect.bisect_left(citations, h): 8 | return h -------------------------------------------------------------------------------- /maximum-number-of-words-you-can-type.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def canBeTypedWords(self, text: str, brokenLetters: str) -> int: 3 | broken = set(brokenLetters) 4 | return len([True for w in text.split() if len(set.intersection(set(w), broken)) == 0]) -------------------------------------------------------------------------------- /miscellaneous/A_Double_Click.py: -------------------------------------------------------------------------------- 1 | n, d = map(int, input().split()) 2 | 3 | t = sorted(map(int, input().split())) 4 | 5 | res = -1 6 | 7 | for i in range(1, n): 8 | if t[i] - t[i - 1] <= d: 9 | res = t[i] 10 | break 11 | 12 | print(res) -------------------------------------------------------------------------------- /miscellaneous/B_-_Multi_Test_Cases.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | 3 | for _ in range(t): 4 | n = int(input()) 5 | arr = list(map(int, input().split())) 6 | res = 0 7 | for el in arr: 8 | if el & 1: 9 | res += 1 10 | print(res) -------------------------------------------------------------------------------- /miscellaneous/Reyna_String.py: -------------------------------------------------------------------------------- 1 | i=input 2 | t=int(i()) 3 | o=ord 4 | v=lambda c:26-o(c)+o('A') 5 | while t: 6 | n,p=map(int,i().split()) 7 | s=sorted(map(v,i())) 8 | ss,j,t=sum(s),0,t-1 9 | while jp:ss-=s[j];j+=1 10 | print(j-1) -------------------------------------------------------------------------------- /miscellaneous/minsum.py: -------------------------------------------------------------------------------- 1 | n, x = map(int, input().split()) 2 | 3 | arr = list(map(int, input().split())) 4 | 5 | msum = 0 6 | 7 | for el in arr: 8 | if el >= 0: 9 | msum += el - el % x 10 | else: 11 | msum += el 12 | 13 | print(msum) -------------------------------------------------------------------------------- /miscellaneous/pascal.py: -------------------------------------------------------------------------------- 1 | n = 5 2 | 3 | curr = [1] 4 | 5 | for i in range(n): 6 | print(" " * (n - i - 1), end="") 7 | print(" ".join("{}".format(x) for x in curr)) 8 | curr = [0] + curr + [0] 9 | curr = [curr[j] + curr[j + 1] for j in range(i + 2)] -------------------------------------------------------------------------------- /miscellaneous/perp.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isPerpendicular(self, a: list, b: list, c: list, d: list): 3 | return (a[0] - b[0]) * (c[1] - d[1]) == (a[1] - b[1]) * (c[0] - d[0]) 4 | 5 | print(Solution().isPerpendicular([1, 2], [2, 3], [3, 4], [4, 5])) -------------------------------------------------------------------------------- /poor-pigs.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def poorPigs(self, buckets: int, minutesToDie: int, minutesToTest: int) -> int: 3 | T = minutesToTest // minutesToDie 4 | x = 0 5 | while (T + 1) ** x < buckets: 6 | x += 1 7 | return x -------------------------------------------------------------------------------- /smallest-string-with-a-given-numeric-value.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def getSmallestString(self, n: int, k: int) -> str: 3 | a,b = divmod(k-n, 25) 4 | c = max(n-a-1, 0) 5 | d = n-a-c 6 | return "a" * c + chr(ord('a') + b) * d + "z" * a -------------------------------------------------------------------------------- /vowels-game-in-a-string.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def doesAliceWin(self, s: str) -> bool: 3 | v = 0 4 | for c in s: 5 | if c in "aeiou": 6 | v += 1 7 | if v == 0: 8 | return False 9 | return True -------------------------------------------------------------------------------- /vowels-of-all-substrings.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def countVowels(self, word: str) -> int: 3 | res = 0 4 | for i in range(len(word)): 5 | if word[i] in "aeiou": 6 | res += (i + 1) * (len(word) - i) 7 | return res -------------------------------------------------------------------------------- /ant-on-the-boundary.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def returnToBoundaryCount(self, nums: List[int]) -> int: 3 | res = 0 4 | x = 0 5 | for el in nums: 6 | x += el 7 | if x == 0: 8 | res += 1 9 | return res -------------------------------------------------------------------------------- /array-partition-i.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def arrayPairSum(self, nums: List[int]) -> int: 3 | nums.sort() 4 | n = len(nums) 5 | total = 0 6 | for i in range(0, n, 2): 7 | total += min(nums[i:i + 2]) 8 | return total -------------------------------------------------------------------------------- /check-if-two-chessboard-squares-have-the-same-color.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def checkTwoChessboards(self, coordinate1: str, coordinate2: str) -> bool: 3 | s = lambda p: (ord(p[0]) - ord('a') + int(p[1]) - 1) % 2 4 | return s(coordinate1) == s(coordinate2) -------------------------------------------------------------------------------- /final-array-state-after-k-multiplication-operations-i.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def getFinalState(self, nums: List[int], k: int, multiplier: int) -> List[int]: 3 | for _ in range(k): 4 | nums[nums.index(min(nums))] *= multiplier 5 | return nums -------------------------------------------------------------------------------- /find-the-xor-of-numbers-which-appear-twice.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def duplicateNumbersXOR(self, nums: List[int]) -> int: 3 | res = 0 4 | for el in set(nums): 5 | if nums.count(el) == 2: 6 | res ^= el 7 | return res -------------------------------------------------------------------------------- /minimum-number-of-chairs-in-a-waiting-room.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def minimumChairs(self, s: str) -> int: 3 | x = 0 4 | res = 0 5 | for c in s: 6 | x += 1 if c == 'E' else -1 7 | res = max(res, x) 8 | return res -------------------------------------------------------------------------------- /minimum-number-of-moves-to-seat-everyone.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def minMovesToSeat(self, seats: List[int], students: List[int]) -> int: 3 | seats.sort() 4 | students.sort() 5 | return sum(abs(seats[i] - students[i]) for i in range(len(seats))) -------------------------------------------------------------------------------- /miscellaneous/A_Working_Week.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | 3 | for _ in range(t): 4 | n = int(input()) 5 | k = max(n - 6, 0) 6 | b = [1] * 3 7 | b[1] += k // 3 8 | b[2] += 2 * k // 3 9 | print(min(abs(b[0] - b[1]), abs(b[1] - b[2]), abs(b[2] - b[0]))) -------------------------------------------------------------------------------- /miscellaneous/B_ASCII_Art.py: -------------------------------------------------------------------------------- 1 | m, n = map(int, input().split()) 2 | 3 | res = [] 4 | 5 | for _ in range(m): 6 | arr = list(map(int, input().split())) 7 | res.append("".join("." if el == 0 else chr(ord('A') + el - 1) for el in arr)) 8 | 9 | print("\n".join(res)) -------------------------------------------------------------------------------- /n-th-tribonacci-number.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def tribonacci(self, n: int) -> int: 3 | if n <= 2: 4 | return [0, 1, 1][n] 5 | a, b, c = 0, 1, 1 6 | for _ in range(n - 2): 7 | a, b, c = b, c, a + b + c 8 | return c -------------------------------------------------------------------------------- /separate-black-and-white-balls.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def minimumSteps(self, s: str) -> int: 3 | res = o = 0 4 | for c in s: 5 | if c == "0": 6 | res += o 7 | else: 8 | o += 1 9 | return res -------------------------------------------------------------------------------- /shuffle-string.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def restoreString(self, s: str, indices: List[int]) -> str: 3 | n = len(s) 4 | op = " " * n 5 | for i in range(n): 6 | op = op[: indices[i]] + s[i] + op[indices[i] + 1 :] 7 | return op -------------------------------------------------------------------------------- /decompress-run-length-encoded-list.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def decompressRLElist(self, nums: List[int]) -> List[int]: 3 | n = len(nums) 4 | op = [] 5 | for i in range(0, n, 2): 6 | op.extend([nums[i + 1]] * nums[i]) 7 | return op -------------------------------------------------------------------------------- /double-modular-exponentiation.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def getGoodIndices(self, variables: List[List[int]], target: int) -> List[int]: 3 | good = lambda a, b, c, m: pow(pow(a, b, 10), c, m) == target 4 | return [i for i, el in enumerate(variables) if good(*el)] -------------------------------------------------------------------------------- /miscellaneous/A_Rudolph_and_Cut_the_Rope.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | 3 | for _ in range(t): 4 | n = int(input()) 5 | res = 0 6 | for __ in range(n): 7 | a, b = map(int, input().split()) 8 | if b < a: 9 | res += 1 10 | print(res) -------------------------------------------------------------------------------- /miscellaneous/A_To_My_Critics.py: -------------------------------------------------------------------------------- 1 | res = [] 2 | 3 | t = int(input()) 4 | 5 | for _ in range(t): 6 | a, b, c = sorted(map(int, input().split())) 7 | if b + c >= 10: 8 | res.append("YES") 9 | else: 10 | res.append("NO") 11 | 12 | print(*res) -------------------------------------------------------------------------------- /miscellaneous/B_Grab_the_Candies.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | 3 | for _ in range(t): 4 | n = int(input()) 5 | arr = list(map(int, input().split())) 6 | ctr = [0, 0] 7 | for el in arr: 8 | ctr[el % 2] += el 9 | print("YES" if ctr[0] > ctr[1] else "NO") -------------------------------------------------------------------------------- /miscellaneous/C_Stones_on_the_Table.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | s = input() 3 | res = 0 4 | i = 0 5 | while i < n: 6 | ctr = 1 7 | while i < n - 1 and s[i] == s[i + 1]: 8 | i += 1 9 | ctr += 1 10 | i += 1 11 | res += ctr - 1 12 | print(res) -------------------------------------------------------------------------------- /miscellaneous/C_Yet_Another_2_or_1_Problem.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | input = sys.stdin.readline 4 | 5 | t = int(input()) 6 | 7 | cres = [] 8 | 9 | for _ in range(t): 10 | n, k = map(int, input().split()) 11 | s = input().strip() 12 | 13 | print("\n".join(cres)) -------------------------------------------------------------------------------- /miscellaneous/muldiff.py: -------------------------------------------------------------------------------- 1 | sqmap = {} 2 | 3 | def square(x): 4 | if x in sqmap: 5 | return sqmap[x] 6 | res = x * x 7 | sqmap[x] = res 8 | return res 9 | 10 | def mul(a, b): 11 | res = square(a + b) - square(a - b) 12 | return res >> 2 -------------------------------------------------------------------------------- /miscellaneous/spell_check.py: -------------------------------------------------------------------------------- 1 | from collections import Counter 2 | 3 | t = int(input()) 4 | 5 | for _ in range(t): 6 | n = int(input()) 7 | s = input() 8 | if Counter(s) == Counter("Timur"): 9 | print("YES") 10 | else: 11 | print("NO") -------------------------------------------------------------------------------- /node-with-highest-edge-score.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def edgeScore(self, edges: List[int]) -> int: 3 | n = len(edges) 4 | ctr = [0] * n 5 | for i in range(n): 6 | ctr[edges[i]] += i 7 | return max(range(n), key = lambda i: ctr[i]) -------------------------------------------------------------------------------- /number-of-unique-xor-triplets-ii.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def uniqueXorTriplets(self, nums: List[int]) -> int: 3 | n = len(nums) 4 | s = {nums[i] ^ nums[j] for i in range(n) for j in range(i, n)} 5 | return len({el ^ x for el in set(nums) for x in s}) -------------------------------------------------------------------------------- /remove-element.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def removeElement(self, nums: List[int], val: int) -> int: 3 | i = 0 4 | while i < len(nums): 5 | if nums[i] == val: 6 | nums.pop(i) 7 | i -= 1 8 | i = max(i + 1, 0) -------------------------------------------------------------------------------- /split-strings-by-separator.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def splitWordsBySeparator(self, words: List[str], separator: str) -> List[str]: 3 | res = [] 4 | for w in words: 5 | res.extend(w.split(separator)) 6 | return [s for s in res if len(s) > 0] -------------------------------------------------------------------------------- /thousand-separator.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def thousandSeparator(self, n: int) -> str: 3 | n = str(n) 4 | l = len(n) 5 | for i in range(1, l): 6 | if i % 3 == 0: 7 | n = n[ : l - i] + '.' + n[l - i : ] 8 | return n -------------------------------------------------------------------------------- /angle-between-hands-of-a-clock.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def angleClock(self, hour: int, minutes: int) -> float: 3 | mAngle = minutes * 6 4 | hAngle = (hour * 30 + minutes / 2) % 360 5 | diff = abs(mAngle - hAngle) 6 | return min(diff, 360 - diff) -------------------------------------------------------------------------------- /find-in-mountain-array.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findInMountainArray(self, target: int, mountain_arr: 'MountainArray') -> int: 3 | arr = mountain_arr._MountainArray__secret 4 | if target not in arr: 5 | return -1 6 | return arr.index(target) -------------------------------------------------------------------------------- /find-numbers-with-even-number-of-digits.py: -------------------------------------------------------------------------------- 1 | import math 2 | 3 | class Solution: 4 | def findNumbers(self, nums: List[int]) -> int: 5 | ctr = 0 6 | for num in nums: 7 | if int(math.log10(num)) & 1: 8 | ctr += 1 9 | return ctr -------------------------------------------------------------------------------- /find-the-winning-player-in-coin-game.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def losingPlayer(self, x: int, y: int) -> str: 3 | w = True 4 | while x >= 1 and y >= 4: 5 | x -= 1 6 | y -= 4 7 | w = not w 8 | return "Bob" if w else "Alice" -------------------------------------------------------------------------------- /matrix-diagonal-sum.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def diagonalSum(self, mat: List[List[int]]) -> int: 3 | n = len(mat) 4 | total = -mat[n//2][n//2] if n & 1 else 0 5 | for i in range(n): 6 | total += mat[i][i] + mat[i][n-i-1] 7 | return total -------------------------------------------------------------------------------- /miscellaneous/B_11_11.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | 3 | res = 0 4 | 5 | arr = list(map(int, input().split())) 6 | 7 | for i in range(n): 8 | for j in range(1, arr[i] + 1): 9 | if len(set(str(i + 1) + str(j))) == 1: 10 | res += 1 11 | 12 | print(res) -------------------------------------------------------------------------------- /miscellaneous/B_Beautiful_Array.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | 3 | for _ in range(t): 4 | n, k, b, s = map(int, input().split()) 5 | if s - s % k != b * k: 6 | print(-1) 7 | else: 8 | res = [s // n] * (n - 1) + [s - (s // n) * (n - 1)] 9 | print(*res) -------------------------------------------------------------------------------- /number-of-common-factors.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def commonFactors(self, a: int, b: int) -> int: 3 | k = min(a, b) 4 | res = 0 5 | for i in range(1, k + 1): 6 | if a % i == 0 and b % i == 0: 7 | res += 1 8 | return res -------------------------------------------------------------------------------- /percentage-of-letter-in-string.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def percentageLetter(self, s: str, letter: str) -> int: 3 | n = len(s) 4 | ctr = 0 5 | for c in s: 6 | if c == letter: 7 | ctr += 1 8 | return floor(100 * ctr / n) -------------------------------------------------------------------------------- /create-target-array-in-the-given-order.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def createTargetArray(self, nums: List[int], index: List[int]) -> List[int]: 3 | n = len(nums) 4 | op = [] 5 | for i in range(n): 6 | op.insert(index[i], nums[i]) 7 | return op -------------------------------------------------------------------------------- /jump-game.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def canJump(self, nums: List[int]) -> bool: 3 | n = len(nums) 4 | leftmost = n - 1 5 | for i in range(n - 1, -1, -1): 6 | if i + nums[i] >= leftmost: 7 | leftmost = i 8 | return leftmost == 0 -------------------------------------------------------------------------------- /k-th-symbol-in-grammar.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def kthGrammar(self, n: int, k: int) -> int: 3 | if n == 1 and k == 1: 4 | return 0 5 | prev = self.kthGrammar(n - 1, (k + 1) >> 1) 6 | if k & 1: 7 | return prev 8 | return 1 - prev -------------------------------------------------------------------------------- /maximum-total-reward-using-operations-i.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def maxTotalReward(self, rewardValues: List[int]) -> int: 3 | s = {0} 4 | rewardValues.sort() 5 | for el in rewardValues: 6 | s |= {x + el for x in s if el > x} 7 | return max(s) -------------------------------------------------------------------------------- /merge-two-2d-arrays-by-summing-values.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def mergeArrays(self, nums1: List[List[int]], nums2: List[List[int]]) -> List[List[int]]: 3 | res = Counter() 4 | for x, y in nums1 + nums2: 5 | res[x] += y 6 | return sorted(res.items()) -------------------------------------------------------------------------------- /miscellaneous/2_3_moves.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | 3 | for _ in range(t): 4 | n = int(input()) 5 | minmoves = float('inf') 6 | for x in range(n + 3): 7 | if abs(n - 3 * x) % 2 == 0: 8 | minmoves = min(minmoves, x + abs(n - 3 * x) // 2) 9 | print(minmoves) -------------------------------------------------------------------------------- /miscellaneous/B_-_Longest_Uncommon_Prefix.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | 3 | s = input() 4 | 5 | res = [] 6 | 7 | for i in range(1, n): 8 | j = 0 9 | while j < n - i and s[j] != s[j + i]: 10 | j += 1 11 | res.append(j) 12 | 13 | print("\n".join(map(str, res))) -------------------------------------------------------------------------------- /miscellaneous/B_Qual_B.py: -------------------------------------------------------------------------------- 1 | n, k = map(int, input().split()) 2 | 3 | s = list(input()) 4 | 5 | rem = k 6 | 7 | for i in range(n): 8 | if s[i] == "o": 9 | if rem > 0: 10 | rem -= 1 11 | else: 12 | s[i] = "x" 13 | 14 | print("".join(s)) -------------------------------------------------------------------------------- /miscellaneous/Distributing_Apples.py: -------------------------------------------------------------------------------- 1 | M = 10 ** 9 + 7 2 | 3 | n, m = map(int, input().split()) 4 | 5 | ctr = [0] * (m + 1) 6 | ctr[0] = 1 7 | 8 | for _ in range(n): 9 | for i in range(1, m + 1): 10 | ctr[i] += ctr[i - 1] 11 | ctr[i] %= M 12 | 13 | print(ctr[-1]) -------------------------------------------------------------------------------- /neither-minimum-nor-maximum.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findNonMinOrMax(self, nums: List[int]) -> int: 3 | minn = min(nums) 4 | maxx = max(nums) 5 | for el in nums: 6 | if el != minn and el != maxx: 7 | return el 8 | return -1 -------------------------------------------------------------------------------- /stone-removal-game.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def canAliceWin(self, n: int) -> bool: 3 | s = 0 4 | prev = 10 5 | res = True 6 | while s <= n: 7 | s += prev 8 | prev -= 1 9 | res = not res 10 | return res -------------------------------------------------------------------------------- /convert-binary-number-in-a-linked-list-to-integer.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def getDecimalValue(self, head: ListNode) -> int: 3 | curr = head 4 | val = 0 5 | while curr: 6 | val = 2 * val + curr.val 7 | curr = curr.next 8 | return val -------------------------------------------------------------------------------- /find-champion-i.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findChampion(self, grid: List[List[int]]) -> int: 3 | n = len(grid) 4 | for i in range(n): 5 | curr = sum(grid[x][i] for x in range(n)) 6 | if curr == 0: 7 | return i 8 | return -1 -------------------------------------------------------------------------------- /h-index.py: -------------------------------------------------------------------------------- 1 | import bisect 2 | 3 | class Solution: 4 | def hIndex(self, citations: List[int]) -> int: 5 | n = len(citations) 6 | citations.sort() 7 | for h in range(n, -1, -1): 8 | if h <= n - bisect.bisect_left(citations, h): 9 | return h -------------------------------------------------------------------------------- /matrix-cells-in-distance-order.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def allCellsDistOrder(self, rows: int, cols: int, rCenter: int, cCenter: int) -> List[List[int]]: 3 | return sorted([[i, j] for i in range(rows) for j in range(cols)], key = lambda p: abs(p[0] - rCenter) + abs(p[1] - cCenter)) -------------------------------------------------------------------------------- /minimum-number-of-operations-to-make-elements-in-array-distinct.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def minimumOperations(self, nums: List[int]) -> int: 3 | res = 0 4 | while len(set(nums)) != len(nums): 5 | nums = nums[3:] 6 | res += 1 7 | return res -------------------------------------------------------------------------------- /miscellaneous/mathematical_circus.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | 3 | for _ in range(t): 4 | n, k = map(int, input().split()) 5 | if k % 4 != 0: 6 | print("YES") 7 | for i in range(1, n + 1, 2): 8 | print(i, i + 1) 9 | else: 10 | print("NO") -------------------------------------------------------------------------------- /miscellaneous/permutation_chain.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | 3 | for _ in range(t): 4 | n = int(input()) 5 | print(n) 6 | res = list(range(1, n + 1)) 7 | print(*res) 8 | for i in range(1, n): 9 | res[i], res[i - 1] = res[i - 1], res[i] 10 | print(*res) -------------------------------------------------------------------------------- /miscellaneous/wonderful_permutation.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | 3 | for _ in range(t): 4 | n, k = map(int, input().split()) 5 | arr = list(map(int, input().split())) 6 | res = 0 7 | for i in range(k): 8 | if arr[i] > k: 9 | res += 1 10 | print(res) -------------------------------------------------------------------------------- /sum-of-squares-of-special-elements.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def sumOfSquares(self, nums: List[int]) -> int: 3 | n = len(nums) 4 | res = 0 5 | for i in range(n): 6 | if n % (i + 1) == 0: 7 | res += nums[i] * nums[i] 8 | return res -------------------------------------------------------------------------------- /three-consecutive-odds.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def threeConsecutiveOdds(self, arr: List[int]) -> bool: 3 | n = len(arr) 4 | for i in range(n - 2): 5 | if arr[i] % 2 + arr[i + 1] % 2 + arr[i + 2] % 2 == 3: 6 | return True 7 | return False -------------------------------------------------------------------------------- /divide-array-into-equal-pairs.py: -------------------------------------------------------------------------------- 1 | from collections import Counter 2 | 3 | class Solution: 4 | def divideArray(self, nums: List[int]) -> bool: 5 | ctr = Counter(nums) 6 | for num in ctr: 7 | if ctr[num] & 1: 8 | return False 9 | return True -------------------------------------------------------------------------------- /find-the-k-th-lucky-number.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def kthLuckyNumber(self, k: int) -> str: 3 | l = 1 4 | while k > pow(2, l): 5 | k -= pow(2, l) 6 | l += 1 7 | return "".join("4" if c == "0" else "7" for c in ('{:0' + str(l) + 'b}').format(k - 1)) -------------------------------------------------------------------------------- /make-array-non-decreasing.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def maximumPossibleSize(self, nums: List[int]) -> int: 3 | res = 0 4 | mx = float('-inf') 5 | for el in nums: 6 | if el >= mx: 7 | mx = el 8 | res += 1 9 | return res -------------------------------------------------------------------------------- /maximum-ice-cream-bars.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def maxIceCream(self, costs: List[int], coins: int) -> int: 3 | costs.sort() 4 | res = 0 5 | for el in costs: 6 | if coins >= el: 7 | res += 1 8 | coins -= el 9 | return res -------------------------------------------------------------------------------- /maximum-possible-number-by-binary-concatenation.py: -------------------------------------------------------------------------------- 1 | from itertools import permutations 2 | 3 | class Solution: 4 | def maxGoodNumber(self, nums: List[int]) -> int: 5 | b = lambda x: "{:0b}".format(x) 6 | return max([int(b(x) + b(y) + b(z), 2) for x, y, z in permutations(nums)]) -------------------------------------------------------------------------------- /maximum-value-of-a-string-in-an-array.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def maximumValue(self, strs: List[str]) -> int: 3 | def val(x): 4 | try: 5 | return int(x) 6 | except: 7 | return len(x) 8 | return max([val(s) for s in strs]) -------------------------------------------------------------------------------- /miscellaneous/D_Range_Sum.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | 3 | for _ in range(t): 4 | n = int(input()) 5 | for a in range(1, 101): 6 | for b in range(a + n - 1, 101): 7 | if (n - 1) * a + b <= (b - a) * (b - a) <= a + (n - 1) * b: 8 | print(a, b) 9 | print() -------------------------------------------------------------------------------- /miscellaneous/HIGHEST_MARKS.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int t; 4 | 5 | std::cin >> t; 6 | 7 | while (t--) { 8 | int n; 9 | std::cin >> n; 10 | int arr[n]; 11 | for (int i = 0; i < n; i++) std::cin >> arr[i]; 12 | int k; 13 | std::cin >> k; 14 | 15 | } -------------------------------------------------------------------------------- /miscellaneous/coupons.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | 3 | prices = list(map(int, input().split())) 4 | 5 | q = int(input()) 6 | 7 | coupons = list(map(int, input().split())) 8 | 9 | prices.sort() 10 | 11 | total = sum(prices) 12 | 13 | for c in coupons: 14 | print(total - prices[-c]) -------------------------------------------------------------------------------- /miscellaneous/sum_of_n.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int sum(int n) { 4 | if (n == 0) { 5 | return 0; 6 | } 7 | return n + sum(n - 1); 8 | } 9 | 10 | void main() { 11 | int n; 12 | scanf("%d", &n); 13 | int res = sum(n); 14 | printf("%d", res); 15 | } -------------------------------------------------------------------------------- /occurrences-after-bigram.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findOcurrences(self, text: str, first: str, second: str) -> List[str]: 3 | words = text.split() 4 | n = len(words) 5 | return [words[i] for i in range(n) if i > 1 and words[i - 1] == second and words[i - 2] == first] -------------------------------------------------------------------------------- /widest-vertical-area-between-two-points-containing-no-points.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def maxWidthOfVerticalArea(self, points: List[List[int]]) -> int: 3 | n = len(points) 4 | points = sorted(points) 5 | return max([points[i + 1][0] - points[i][0] for i in range(n - 1)]) -------------------------------------------------------------------------------- /circular-permutation-in-binary-representation.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def circularPermutation(self, n: int, start: int) -> List[int]: 3 | x = 0 4 | while x ^ (x >> 1) != start: 5 | x += 1 6 | return [i ^ (i >> 1) for i in list(range(x, 1 << n)) + list(range(x))] -------------------------------------------------------------------------------- /clumsy-factorial.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def clumsy(self, n: int) -> int: 3 | expr = "" 4 | opr = ["*", "//", "+", "-"] 5 | for i in range(n): 6 | expr += str(n - i) 7 | if i < n - 1: 8 | expr += opr[i % 4] 9 | return eval(expr) -------------------------------------------------------------------------------- /consecutive-numbers-sum.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def consecutiveNumbersSum(self, n: int) -> int: 3 | res = 0 4 | l = 1 5 | while l * (l + 1) // 2 <= n: 6 | if (n - l * (l - 1) // 2) % l == 0: 7 | res += 1 8 | l += 1 9 | return res -------------------------------------------------------------------------------- /delete-node-in-a-linked-list.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def deleteNode(self, node): 3 | prev = node 4 | curr = node 5 | while curr and curr.next: 6 | curr.val = curr.next.val 7 | prev = curr 8 | curr = curr.next 9 | prev.next = None -------------------------------------------------------------------------------- /escape-the-ghosts.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def escapeGhosts(self, ghosts: List[List[int]], target: List[int]) -> bool: 3 | t = float('inf') 4 | tx, ty = target 5 | for i, j in ghosts: 6 | t = min(t, abs(tx - i) + abs(ty - j)) 7 | return t > abs(tx) + abs(ty) -------------------------------------------------------------------------------- /find-n-unique-integers-sum-up-to-zero.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def sumZero(self, n: int) -> List[int]: 3 | if n == 1: 4 | return [0] 5 | if n == 2: 6 | return [-1, 1] 7 | prev = self.sumZero(n - 2) 8 | return [prev[0] - 1] + prev + [prev[-1] + 1] -------------------------------------------------------------------------------- /find-the-student-that-will-replace-the-chalk.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def chalkReplacer(self, chalk: List[int], k: int) -> int: 3 | s = sum(chalk) 4 | k %= s 5 | i = 0 6 | while chalk[i] <= k: 7 | k -= chalk[i] 8 | i += 1 9 | return i -------------------------------------------------------------------------------- /harshad-number.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def sumOfTheDigitsOfHarshadNumber(self, x: int) -> int: 3 | s = 0 4 | curr = x 5 | while curr: 6 | s += curr % 10 7 | curr //= 10 8 | if x % s == 0: 9 | return s 10 | return -1 -------------------------------------------------------------------------------- /middle-of-the-linked-list.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def middleNode(self, head: Optional[ListNode]) -> Optional[ListNode]: 3 | slow = head 4 | fast = head 5 | while fast and fast.next: 6 | slow = slow.next 7 | fast = fast.next.next 8 | return slow -------------------------------------------------------------------------------- /miscellaneous/A_Job_Interview.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | 3 | s = input() 4 | 5 | g = f = p = 0 6 | 7 | for c in s: 8 | if c == 'o': 9 | g += 1 10 | if c == '-': 11 | f += 1 12 | if c == 'x': 13 | p += 1 14 | 15 | print("Yes" if g > 0 and p == 0 else "No") -------------------------------------------------------------------------------- /miscellaneous/D_Take_ABC.py: -------------------------------------------------------------------------------- 1 | s = input() 2 | 3 | stack = [] 4 | 5 | for c in s: 6 | delt = False 7 | if len(stack) >= 2 and stack[-2] + stack[-1] + c == "ABC": 8 | stack.pop() 9 | stack.pop() 10 | else: 11 | stack.append(c) 12 | 13 | print("".join(stack)) -------------------------------------------------------------------------------- /miscellaneous/N_Triplets.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | 3 | def solve(n): 4 | i = 2 5 | while i * i < n: 6 | if n % i == 0: 7 | return [1, i, n // i] 8 | i += 1 9 | return [-1] 10 | 11 | for _ in range(t): 12 | n = int(input()) 13 | print(*solve(n)) -------------------------------------------------------------------------------- /smallest-range-i.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def smallestRangeI(self, nums: List[int], k: int) -> int: 3 | mn = float('inf') 4 | mx = float('-inf') 5 | for num in nums: 6 | mn = min(mn, num) 7 | mx = max(mx, num) 8 | return max(0, mx - mn - 2 * k) -------------------------------------------------------------------------------- /binary-number-with-alternating-bits.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def hasAlternatingBits(self, n: int) -> bool: 3 | bi = "{:b}".format(n) 4 | for i, c in enumerate(bi): 5 | if i > 0: 6 | if c == bi[i - 1]: 7 | return False 8 | return True -------------------------------------------------------------------------------- /check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isPrefixOfWord(self, sentence: str, searchWord: str) -> int: 3 | for i, w in enumerate(sentence.split()): 4 | if w.startswith(searchWord): 5 | return i + 1 6 | return -1 -------------------------------------------------------------------------------- /crawler-log-folder.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def minOperations(self, logs: List[str]) -> int: 3 | ctr = 0 4 | for log in logs: 5 | if log == "../": 6 | ctr = max(ctr - 1, 0) 7 | elif log != "./": 8 | ctr += 1 9 | return ctr -------------------------------------------------------------------------------- /minimum-value-to-get-positive-step-by-step-sum.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def minStartValue(self, nums: List[int]) -> int: 3 | p = 0 4 | res = 1 5 | for el in nums: 6 | p += el 7 | if p <= 0: 8 | res = max(res, -p + 1) 9 | return res -------------------------------------------------------------------------------- /miscellaneous/A_Two_Elevators.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | 3 | for _ in range(t): 4 | a, b, c = map(int, input().split()) 5 | x = abs(a - 1) 6 | y = abs(b - c) + abs(c - 1) 7 | if x < y: 8 | print(1) 9 | elif x > y: 10 | print(2) 11 | else: 12 | print(3) -------------------------------------------------------------------------------- /miscellaneous/A_Typical_Interview_Problem.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | 3 | def solve(s): 4 | x = "FBFFBFFB" 5 | if s in (x + x + x): 6 | return "YES" 7 | else: 8 | return "NO" 9 | 10 | for _ in range(t): 11 | k = int(input()) 12 | s = input() 13 | print(solve(s)) -------------------------------------------------------------------------------- /miscellaneous/A_Walking_Master.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | 3 | for _ in range(t): 4 | a, b, c, d = map(int, input().split()) 5 | if b > d: 6 | print(-1) 7 | continue 8 | a += d - b 9 | if a < c: 10 | print(-1) 11 | continue 12 | print(a - c + d - b) -------------------------------------------------------------------------------- /miscellaneous/B_-_Adjacency_List.py: -------------------------------------------------------------------------------- 1 | n, m = map(int, input().split()) 2 | 3 | l = [set() for _ in range(n + 1)] 4 | 5 | for _ in range(m): 6 | a, b = map(int, input().split()) 7 | l[a].add(b) 8 | l[b].add(a) 9 | 10 | for c in range(1, n + 1): 11 | print(len(l[c]), *sorted(l[c])) -------------------------------------------------------------------------------- /miscellaneous/B_Chessboard.py: -------------------------------------------------------------------------------- 1 | board = [] 2 | 3 | for _ in range(8): 4 | board.append(input()) 5 | 6 | pos = (0, 0) 7 | 8 | for i in range(8): 9 | for j in range(8): 10 | if board[i][j] == '*': 11 | pos = (j, 8 - i) 12 | 13 | print(f"{chr(ord('a') + pos[0])}{pos[1]}") -------------------------------------------------------------------------------- /miscellaneous/B_Tanya_and_Postcard.py: -------------------------------------------------------------------------------- 1 | from collections import Counter 2 | 3 | s = input() 4 | t = input() 5 | 6 | y = w = 0 7 | 8 | ctr = Counter(t) 9 | 10 | for c in s: 11 | if ctr[c] > 0: 12 | y += 1 13 | ctr[c] -= 1 14 | else: 15 | w += 1 16 | 17 | print(y, w) -------------------------------------------------------------------------------- /miscellaneous/mul_persistence.py: -------------------------------------------------------------------------------- 1 | def MultiplicativePersistence(num): 2 | if 0 <= num < 10: 3 | return 0 4 | p = 1 5 | while num: 6 | p *= (num % 10) 7 | num //= 10 8 | return 1 + MultiplicativePersistence(p) 9 | 10 | print(MultiplicativePersistence(4673)) -------------------------------------------------------------------------------- /number-of-segments-in-a-string.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def countSegments(self, s: str) -> int: 3 | prev = None 4 | ctr = 0 5 | for c in s: 6 | if (prev == None or prev == " ") and c != " ": 7 | ctr += 1 8 | prev = c 9 | return ctr -------------------------------------------------------------------------------- /number-of-unique-xor-triplets-i.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def uniqueXorTriplets(self, nums: List[int]) -> int: 3 | n = len(nums) 4 | res = [1,2,4] 5 | p = 8 6 | while len(res) < n: 7 | res.extend([p] * (p // 2)) 8 | p *= 2 9 | return res[n - 1] -------------------------------------------------------------------------------- /reach-a-number.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def reachNumber(self, target: int) -> int: 3 | target = abs(target) 4 | total = 0 5 | steps = 0 6 | while total < target or (total - target) % 2 != 0: 7 | steps += 1 8 | total += steps 9 | return steps -------------------------------------------------------------------------------- /remove-9.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def newInteger(self, n: int) -> int: 3 | d = [] 4 | while n: 5 | d.append(n % 9) 6 | n //= 9 7 | d.reverse() 8 | res = 0 9 | for x in d: 10 | res = 10 * res + x 11 | return res -------------------------------------------------------------------------------- /sum-of-values-at-indices-with-k-set-bits.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def sumIndicesWithKSetBits(self, nums: List[int], k: int) -> int: 3 | n = len(nums) 4 | res = 0 5 | for i in range(n): 6 | if i.bit_count() == k: 7 | res += nums[i] 8 | return res -------------------------------------------------------------------------------- /capitalize-the-title.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def capitalizeTitle(self, title: str) -> str: 3 | words = title.split() 4 | words = [word.lower() for word in words] 5 | words = [(word[0].upper() + word[1:]) if len(word) > 2 else word for word in words] 6 | return " ".join(words) -------------------------------------------------------------------------------- /find-resultant-array-after-removing-anagrams.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def removeAnagrams(self, words: List[str]) -> List[str]: 3 | stack = [] 4 | for w in words: 5 | if len(stack) == 0 or sorted(w) != sorted(stack[-1]): 6 | stack.append(w) 7 | return stack -------------------------------------------------------------------------------- /miscellaneous/A_To_Be_Saikyo.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | 3 | p = list(map(int, input().split())) 4 | 5 | k = max(p) 6 | 7 | ctr = p.count(k) 8 | 9 | if k == p[0]: 10 | if ctr == 1: 11 | print(k - p[0]) 12 | else: 13 | print(k - p[0] + 1) 14 | else: 15 | print(k - p[0] + 1) -------------------------------------------------------------------------------- /miscellaneous/B_Sifid_and_Strange_Subsequences.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | 3 | for _ in range(t): 4 | n = int(input()) 5 | arr = list(map(int, input().split())) 6 | arr.sort() 7 | diffs = [] 8 | for i in range(n - 1): 9 | diffs.append(arr[i + 1] - arr[i]) 10 | print(diffs) -------------------------------------------------------------------------------- /miscellaneous/Crushed_Apples_and_a_Balance_Scale.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | 3 | for _ in range(t): 4 | m, n = map(int, input().split()) 5 | if m < n: 6 | print("NO") 7 | continue 8 | while m % 2 == 0 and m > 0: 9 | m //= 2 10 | print("YES" if n % m == 0 else "NO") -------------------------------------------------------------------------------- /miscellaneous/dividedecimal.py: -------------------------------------------------------------------------------- 1 | a, b = map(int, input().split()) 2 | 3 | def getstr(a, b): 4 | if b == 0: 5 | return "0.000" 6 | if b == a: 7 | return "1.000" 8 | k = str(round(1000 * b / a)) 9 | return f"0.{k}" + "0" * max(3 - len(k), 0) 10 | 11 | print(getstr(a, b)) -------------------------------------------------------------------------------- /sum-of-digits-of-string-after-convert.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def getLucky(self, s: str, k: int) -> int: 3 | op = "" 4 | for c in s: 5 | op += str(ord(c) - ord('a') + 1) 6 | for i in range(k): 7 | op = str(sum([int(d) for d in op])) 8 | return int(op) -------------------------------------------------------------------------------- /teemo-attacking.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findPoisonedDuration(self, timeSeries: List[int], duration: int) -> int: 3 | n = len(timeSeries) 4 | total = duration 5 | for i in range(1, n): 6 | total += min(timeSeries[i] - timeSeries[i - 1], duration) 7 | return total -------------------------------------------------------------------------------- /append-characters-to-string-to-make-subsequence.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def appendCharacters(self, s: str, t: str) -> int: 3 | m = len(s) 4 | n = len(t) 5 | i = 0 6 | for j in range(m): 7 | if i < n and s[j] == t[i]: 8 | i += 1 9 | return n - i -------------------------------------------------------------------------------- /buy-two-chocolates.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def buyChoco(self, prices: List[int], money: int) -> int: 3 | a, b = float('inf'), float('inf') 4 | for el in prices: 5 | a, b, temp = sorted([a, b, el]) 6 | if a + b > money: 7 | return money 8 | return money - a - b -------------------------------------------------------------------------------- /calculate-digit-sum-of-a-string.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def digitSum(self, s: str, k: int) -> str: 3 | while len(s) > k: 4 | nexts = "" 5 | for i in range(0, len(s), k): 6 | nexts += str(sum(int(d) for d in s[i:i+k])) 7 | s = nexts 8 | return s -------------------------------------------------------------------------------- /check-if-it-is-a-good-array.py: -------------------------------------------------------------------------------- 1 | def gcd(a, b): 2 | while b: 3 | a, b = b, a % b 4 | return a 5 | 6 | class Solution: 7 | def isGoodArray(self, nums: List[int]) -> bool: 8 | res = nums[0] 9 | for el in nums: 10 | res = gcd(res, el) 11 | return res == 1 -------------------------------------------------------------------------------- /count-alternating-subarrays.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def countAlternatingSubarrays(self, nums: List[int]) -> int: 3 | n = len(nums) 4 | dp = [1] * n 5 | for i in range(n - 2, -1, -1): 6 | if nums[i] != nums[i + 1]: 7 | dp[i] += dp[i + 1] 8 | return sum(dp) -------------------------------------------------------------------------------- /count-asterisks.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def countAsterisks(self, s: str) -> int: 3 | ctr = 0 4 | stars = 0 5 | for c in s: 6 | if c == '|': 7 | ctr += 1 8 | if c == '*' and ctr % 2 == 0: 9 | stars += 1 10 | return stars -------------------------------------------------------------------------------- /count-the-number-of-consistent-strings.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def countConsistentStrings(self, allowed: str, words: List[str]) -> int: 3 | allowed = set(allowed) 4 | ctr = 0 5 | for w in words: 6 | if set(w).issubset(allowed): 7 | ctr += 1 8 | return ctr -------------------------------------------------------------------------------- /defuse-the-bomb.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def decrypt(self, code: List[int], k: int) -> List[int]: 3 | n = len(code) 4 | p = [0] 5 | for i in range(3 * n): 6 | p.append(p[-1] + code[i % n]) 7 | return [abs(p[n + i + int(k >= 0)]-p[n + i + k + int(k>=0)]) for i in range(n)] -------------------------------------------------------------------------------- /find-the-divisibility-array-of-a-string.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def divisibilityArray(self, word: str, m: int) -> List[int]: 3 | res = [] 4 | x = 0 5 | for c in word: 6 | x = 10 * x + int(c) 7 | x %= m 8 | res.append(int(x == 0)) 9 | return res -------------------------------------------------------------------------------- /k-closest-points-to-origin.py: -------------------------------------------------------------------------------- 1 | import heapq 2 | 3 | class Solution: 4 | def kClosest(self, points: List[List[int]], k: int) -> List[List[int]]: 5 | points = [(p[0] * p[0] + p[1] * p[1], p) for p in points] 6 | heapq.heapify(points) 7 | return [heapq.heappop(points)[1] for i in range(k)] -------------------------------------------------------------------------------- /minimize-maximum-pair-sum-in-array.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def minPairSum(self, nums: List[int]) -> int: 3 | nums.sort() 4 | n = len(nums) 5 | maxSum = float('-inf') 6 | for i in range(n // 2): 7 | maxSum = max(maxSum, nums[i] + nums[n - 1 - i]) 8 | return maxSum -------------------------------------------------------------------------------- /minimum-changes-to-make-alternating-binary-string.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def minOperations(self, s: str) -> int: 3 | a = b = 0 4 | for i in range(len(s)): 5 | if int(s[i]) == i % 2: 6 | a += 1 7 | else: 8 | b += 1 9 | return min(a, b) -------------------------------------------------------------------------------- /minimum-number-of-days-to-eat-n-oranges.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def minDays(self, n: int) -> int: 3 | @lru_cache(maxsize = None) 4 | def dp(n): 5 | if n == 0: 6 | return -1 7 | return 1 + min(n % 2 + dp(n // 2), n % 3 + dp(n // 3)) 8 | return dp(n) -------------------------------------------------------------------------------- /miscellaneous/B_Maximum_Strength.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | 3 | for _ in range(t): 4 | l, r = input().split() 5 | if len(l) < len(r): 6 | l = "0" * (len(r) - len(l)) + l 7 | n = len(r) 8 | i = 0 9 | while i < n and l[i] == r[i]: 10 | i += 1 11 | if i < n: 12 | -------------------------------------------------------------------------------- /miscellaneous/Bloodline.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | 3 | for _ in range(t): 4 | n, k = map(int, input().split()) 5 | tina = True 6 | while n > 0: 7 | curr = n % k 8 | if curr > 0: 9 | tina = not tina 10 | n //= k 11 | print("Shivansh" if not tina else "Tina") -------------------------------------------------------------------------------- /miscellaneous/D_-_Yet_Another_Recursive_Function.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | 3 | cache = {} 4 | 5 | def fn(n): 6 | if n == 0: 7 | return 1 8 | if n in cache: 9 | return cache[n] 10 | res = fn(n // 2) + fn(n // 3) 11 | cache[n] = res 12 | return res 13 | 14 | print(fn(n)) -------------------------------------------------------------------------------- /rank-transform-of-an-array.py: -------------------------------------------------------------------------------- 1 | import heapq 2 | 3 | class Solution: 4 | def arrayRankTransform(self, arr: List[int]) -> List[int]: 5 | ranks = {} 6 | dist = sorted(set(arr)) 7 | for i, d in enumerate(dist): 8 | ranks[d] = i + 1 9 | return [ranks[item] for item in arr] -------------------------------------------------------------------------------- /slowest-key.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def slowestKey(self, releaseTimes: List[int], keysPressed: str) -> str: 3 | n = len(keysPressed) 4 | releaseTimes.append(0) 5 | i = max(range(n), key = lambda j: (releaseTimes[j] - releaseTimes[j - 1], keysPressed[j])) 6 | return keysPressed[i] -------------------------------------------------------------------------------- /clear-digits.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def clearDigits(self, s: str) -> str: 3 | stack = [] 4 | for c in s: 5 | if c.isdigit(): 6 | if stack: 7 | stack.pop() 8 | else: 9 | stack.append(c) 10 | return "".join(stack) -------------------------------------------------------------------------------- /delete-and-earn.py: -------------------------------------------------------------------------------- 1 | from collections import Counter 2 | 3 | class Solution: 4 | def deleteAndEarn(self, nums: List[int]) -> int: 5 | ctr = Counter(nums) 6 | prev = curr = 0 7 | for num in range(10001): 8 | prev, curr = curr, max(prev + num * ctr[num], curr) 9 | return curr -------------------------------------------------------------------------------- /hash-divided-string.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def stringHash(self, s: str, k: int) -> str: 3 | n = len(s) 4 | ctr = [0] * (n // k) 5 | for i in range(n): 6 | ctr[i // k] += ord(s[i]) - ord('a') 7 | ctr[i // k] %= 26 8 | return "".join(chr(ord('a') + c) for c in ctr) -------------------------------------------------------------------------------- /last-moment-before-all-ants-fall-out-of-a-plank.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def getLastMoment(self, n: int, left: List[int], right: List[int]) -> int: 3 | res = 0 4 | for el in left: 5 | res = max(res, el) 6 | for el in right: 7 | res = max(res, n - el) 8 | return res -------------------------------------------------------------------------------- /miscellaneous/A_Koxia_and_Whiteboards.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | 3 | for _ in range(t): 4 | n, m = map(int, input().split()) 5 | a = list(map(int, input().split())) 6 | b = list(map(int, input().split())) 7 | for el in b: 8 | i = a.index(min(a)) 9 | a[i] = el 10 | print(sum(a)) -------------------------------------------------------------------------------- /miscellaneous/A_Nine.py: -------------------------------------------------------------------------------- 1 | a, b = map(int, input().split()) 2 | 3 | bb = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] 4 | 5 | if a in bb[0] and b in bb[0]: 6 | print("Yes") 7 | elif a in bb[1] and b in bb[1]: 8 | print("Yes") 9 | elif a in bb[2] and b in bb[2]: 10 | print("Yes") 11 | else: 12 | print("No") -------------------------------------------------------------------------------- /miscellaneous/B_Quick_Sort.py: -------------------------------------------------------------------------------- 1 | import math 2 | 3 | t = int(input()) 4 | 5 | for _ in range(t): 6 | n, k = map(int, input().split()) 7 | arr = list(map(int, input().split())) 8 | x = 1 9 | for el in arr: 10 | if el == x: 11 | x += 1 12 | print(math.ceil((n + 1 - x) / k)) -------------------------------------------------------------------------------- /miscellaneous/E_NAND_repeatedly.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | 3 | s = list(map(int, input())) 4 | 5 | dp = [[0, 0] for _ in range(n + 1)] 6 | 7 | res = 0 8 | 9 | for i in range(n - 1, -1, -1): 10 | dp[i][0] += dp[i + 1][1] 11 | dp[i][1] += 1 + dp[i + 1][0] 12 | res += dp[i][s[i]] 13 | 14 | print(res) -------------------------------------------------------------------------------- /miscellaneous/Inside_The_Stadium.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | 3 | for _ in range(t): 4 | n = int(input()) 5 | arr = list(map(int, input().split())) 6 | res = 0 7 | runs = 0 8 | for i in range(n): 9 | runs += arr[i] 10 | if runs == i + 1: 11 | res += 1 12 | print(res) -------------------------------------------------------------------------------- /number-of-ways-to-buy-pens-and-pencils.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def waysToBuyPensPencils(self, total: int, cost1: int, cost2: int) -> int: 3 | ways = 0 4 | for i in range(1 + total // cost1): 5 | left = max(total - i * cost1, 0) 6 | ways += 1 + left // cost2 7 | return ways -------------------------------------------------------------------------------- /subtract-the-product-and-sum-of-digits-of-an-integer.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def subtractProductAndSum(self, n: int) -> int: 3 | p = 1 4 | s = 0 5 | while n > 0: 6 | d = n % 10 7 | p *= d 8 | s += d 9 | n = n // 10 10 | return p - s -------------------------------------------------------------------------------- /successful-pairs-of-spells-and-potions.py: -------------------------------------------------------------------------------- 1 | import bisect 2 | 3 | class Solution: 4 | def successfulPairs(self, spells: List[int], potions: List[int], success: int) -> List[int]: 5 | n = len(potions) 6 | potions.sort() 7 | return [n - bisect.bisect_left(potions, success / sp) for sp in spells] -------------------------------------------------------------------------------- /zigzag-grid-traversal-with-skip.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def zigzagTraversal(self, grid: List[List[int]]) -> List[int]: 3 | res = [] 4 | f = True 5 | for r in grid: 6 | res.extend(r if f else r[::-1]) 7 | f = not f 8 | return [res[i] for i in range(0, len(res), 2)] -------------------------------------------------------------------------------- /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | github: [theabbie] 2 | patreon: theabbie 3 | open_collective: theabbie 4 | ko_fi: theabbie 5 | tidelift: npm/theabbie 6 | liberapay: theabbie 7 | issuehunt: theabbie 8 | otechie: theabbie 9 | custom: ['https://theabbie.github.io', 'https://paypal.me/theabbie','https://donorbox.org/theabbie'] 10 | -------------------------------------------------------------------------------- /account-balance-after-rounded-purchase.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def accountBalanceAfterPurchase(self, purchaseAmount: int) -> int: 3 | x = purchaseAmount % 10 4 | if x < 5: 5 | purchaseAmount -= x 6 | else: 7 | purchaseAmount += 10 - x 8 | return 100 - purchaseAmount -------------------------------------------------------------------------------- /binary-string-with-substrings-representing-1-to-n.py: -------------------------------------------------------------------------------- 1 | import random 2 | 3 | class Solution: 4 | def queryString(self, s: str, n: int) -> bool: 5 | for _ in range(10000): 6 | k = random.randint(1, n) 7 | if "{:b}".format(k) not in s: 8 | return False 9 | return True -------------------------------------------------------------------------------- /binary-tree-inorder-traversal.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def inorderTraversal(self, root: Optional[TreeNode]) -> List[int]: 3 | if not root: 4 | return [] 5 | left = self.inorderTraversal(root.left) 6 | right = self.inorderTraversal(root.right) 7 | return left + [root.val] + right -------------------------------------------------------------------------------- /binary-tree-preorder-traversal.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def preorderTraversal(self, root: Optional[TreeNode]) -> List[int]: 3 | if not root: 4 | return [] 5 | left = self.preorderTraversal(root.left) 6 | right = self.preorderTraversal(root.right) 7 | return [root.val] + left + right -------------------------------------------------------------------------------- /construct-the-rectangle.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def constructRectangle(self, area: int) -> List[int]: 3 | i = 1 4 | minDiffBox = 1 5 | while i * i <= area: 6 | if area % i == 0: 7 | minDiffBox = i 8 | i += 1 9 | return [area // minDiffBox, minDiffBox] -------------------------------------------------------------------------------- /count-the-number-of-vowel-strings-in-range.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def vowelStrings(self, words: List[str], left: int, right: int) -> int: 3 | res = 0 4 | for i in range(left, right + 1): 5 | if words[i][0] in "aeiou" and words[i][-1] in "aeiou": 6 | res += 1 7 | return res -------------------------------------------------------------------------------- /identify-the-largest-outlier-in-an-array.py: -------------------------------------------------------------------------------- 1 | from collections import Counter 2 | 3 | class Solution: 4 | def getLargestOutlier(self, nums: List[int]) -> int: 5 | s = sum(nums) 6 | x = Counter(nums) 7 | return max(s - 2 * el for el in x if (s - 2 * el) in x and (el != s - 2 * el or x.get(el, 0) > 1)) -------------------------------------------------------------------------------- /implement-strstr.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def strStr(self, haystack: str, needle: str) -> int: 3 | n = len(haystack) 4 | k = len(needle) 5 | exists = {needle} 6 | for i in range(0, n - k + 1): 7 | if haystack[i : i + k] in exists: 8 | return i 9 | return -1 -------------------------------------------------------------------------------- /maximum-count-of-positive-integer-and-negative-integer.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def maximumCount(self, nums: List[int]) -> int: 3 | pos = neg = 0 4 | for el in nums: 5 | if el < 0: 6 | neg += 1 7 | if el > 0: 8 | pos += 1 9 | return max(pos, neg) -------------------------------------------------------------------------------- /minimize-xor.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def minimizeXor(self, num1: int, num2: int) -> int: 3 | c = num2.bit_count() 4 | res = 0 5 | for b in range(32, -1, -1): 6 | if c and (b + 1 == c or num1 & (1 << b)): 7 | res += (1 << b) 8 | c -= 1 9 | return res -------------------------------------------------------------------------------- /minimum-moves-to-reach-target-score.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def minMoves(self, target: int, maxDoubles: int) -> int: 3 | n = 0 4 | while target > 1 and maxDoubles > 0: 5 | n += 1 + target % 2 6 | target = target // 2 7 | maxDoubles -= 1 8 | return n + target - 1 -------------------------------------------------------------------------------- /minimum-number-of-vertices-to-reach-all-nodes.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findSmallestSetOfVertices(self, n: int, edges: List[List[int]]) -> List[int]: 3 | indegree = {} 4 | for a, b in edges: 5 | indegree[b] = indegree.get(b, 0) + 1 6 | return [i for i in range(n) if i not in indegree] -------------------------------------------------------------------------------- /minimum-time-to-type-word-using-special-typewriter.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def minTimeToType(self, word: str) -> int: 3 | curr = 'a' 4 | t = 0 5 | for c in word: 6 | d = abs(ord(c) - ord(curr)) 7 | t += min(d, 26 - d) 8 | curr = c 9 | return t + len(word) -------------------------------------------------------------------------------- /mirror-reflection.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def gcd(self, a, b): 3 | while b: 4 | a, b = b, a % b 5 | return a 6 | 7 | def mirrorReflection(self, p: int, q: int) -> int: 8 | mul = self.gcd(p, q) 9 | p, q = p // mul, q // mul 10 | return 1 - p % 2 + q % 2 -------------------------------------------------------------------------------- /miscellaneous/A_Treasure_Chest.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | 3 | s = input() 4 | 5 | valid = False 6 | 7 | ctr = 0 8 | 9 | for c in s: 10 | if c == "|": 11 | ctr += 1 12 | if c == "*": 13 | if ctr == 1: 14 | valid = True 15 | break 16 | 17 | print("in" if valid else "out") -------------------------------------------------------------------------------- /miscellaneous/C_Reversible.py: -------------------------------------------------------------------------------- 1 | from collections import Counter 2 | 3 | n = int(input()) 4 | 5 | seen = set() 6 | 7 | res = 0 8 | 9 | for _ in range(n): 10 | s = input() 11 | if s not in seen or s[::-1] not in seen: 12 | res += 1 13 | seen.add(s) 14 | seen.add(s[::-1]) 15 | 16 | print(res) -------------------------------------------------------------------------------- /miscellaneous/D_Circle_Line.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | 3 | dist = list(map(int, input().split())) 4 | 5 | s, t = map(int, input().split()) 6 | 7 | s, t = min(s, t), max(s, t) 8 | 9 | p = [0] 10 | 11 | for el in dist: 12 | p.append(p[-1] + el) 13 | 14 | c = p[t - 1] - p[s - 1] 15 | 16 | print(min(c, p[-1] - c)) -------------------------------------------------------------------------------- /miscellaneous/Expensive_Steps.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | 3 | for _ in range(t): 4 | n, a, b, c, d = map(int, input().split()) 5 | res = abs(c - a) + abs(d - b) 6 | x = [min(a, n - a + 1), min(b, n - b + 1)] 7 | y = [min(c, n - c + 1), min(d, n - d + 1)] 8 | res = min(res, min(x) + min(y)) 9 | print(res) -------------------------------------------------------------------------------- /miscellaneous/F_Sugar_Water_2.py: -------------------------------------------------------------------------------- 1 | m, n, k = map(int, input().split()) 2 | 3 | a = [] 4 | b = [] 5 | 6 | for _ in range(m): 7 | x, y = map(int, input().split()) 8 | a.append((x, x + y)) 9 | 10 | for _ in range(n): 11 | x, y = map(int, input().split()) 12 | b.append((x, x + y)) 13 | 14 | print(a, b) -------------------------------------------------------------------------------- /miscellaneous/binaryInsert.py: -------------------------------------------------------------------------------- 1 | def insert(arr, val): 2 | beg = 0 3 | end = len(arr) - 1 4 | while beg < end: 5 | mid = (beg + end) // 2 6 | if arr[mid] >= val: 7 | beg = mid + 1 8 | else: 9 | end = mid 10 | return beg 11 | 12 | print(insert([1, 1], 1)) -------------------------------------------------------------------------------- /number-of-laser-beams-in-a-bank.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def numberOfBeams(self, bank: List[str]) -> int: 3 | bank = [b.count("1") for b in bank if b.count("1") != 0] 4 | val = 0 5 | n = len(bank) 6 | for i in range(n - 1): 7 | val += bank[i] * bank[i + 1] 8 | return val -------------------------------------------------------------------------------- /rearrange-array-to-maximize-prefix-score.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def maxScore(self, nums: List[int]) -> int: 3 | nums.sort(reverse = True) 4 | p = 0 5 | res = 0 6 | for el in nums: 7 | p += el 8 | if p > 0: 9 | res += 1 10 | return res -------------------------------------------------------------------------------- /semi-ordered-permutation.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def semiOrderedPermutation(self, nums: List[int]) -> int: 3 | n = len(nums) 4 | one = nums.index(1) 5 | last = nums.index(n) 6 | if one < last: 7 | return one + n - last - 1 8 | return one - last + one + n - (one + 1) - 1 -------------------------------------------------------------------------------- /shifting-letters.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def shiftingLetters(self, s: str, shifts: List[int]) -> str: 3 | s = list(s) 4 | p = 0 5 | for i in range(len(s) - 1, -1, -1): 6 | p += shifts[i] 7 | s[i] = chr(ord('a') + (ord(s[i]) - ord('a') + p) % 26) 8 | return "".join(s) -------------------------------------------------------------------------------- /sort-characters-by-frequency.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def frequencySort(self, s: str) -> str: 3 | n = len(s) 4 | freq = {} 5 | for c in s: 6 | freq[c] = freq.get(c, 0) + 1 7 | return "".join([c * k for k, c in sorted([(value, key) for key, value in freq.items()], reverse = True)]) -------------------------------------------------------------------------------- /valid-palindrome-iv.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def makePalindrome(self, s: str) -> bool: 3 | i = 0 4 | j = len(s) - 1 5 | ops = 0 6 | while i < j: 7 | if s[i] != s[j]: 8 | ops += 1 9 | i += 1 10 | j -= 1 11 | return ops <= 2 -------------------------------------------------------------------------------- /best-time-to-buy-and-sell-stock.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def maxProfit(self, prices: List[int]) -> int: 3 | res = 0 4 | minpastvalue = float('inf') 5 | for price in prices: 6 | res = max(res, price - minpastvalue) 7 | minpastvalue = min(minpastvalue, price) 8 | return res -------------------------------------------------------------------------------- /binary-tree-postorder-traversal.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def postorderTraversal(self, root: Optional[TreeNode]) -> List[int]: 3 | if not root: 4 | return [] 5 | left = self.postorderTraversal(root.left) 6 | right = self.postorderTraversal(root.right) 7 | return left + right + [root.val] -------------------------------------------------------------------------------- /coin-change-ii.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def change(self, amount: int, coins: List[int]) -> int: 3 | dp = [0] * (amount + 1) 4 | dp[0] = 1 5 | for c in coins: 6 | for s in range(amount + 1): 7 | if s >= c: 8 | dp[s] += dp[s - c] 9 | return dp[amount] -------------------------------------------------------------------------------- /concatenation-of-consecutive-binary-numbers.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def concatenatedBinary(self, n: int) -> int: 3 | MAX = 10 ** 9 + 7 4 | res = 0 5 | for i in range(1, n + 1): 6 | k = len("{:b}".format(i)) 7 | res = (res << k) | i 8 | res %= MAX 9 | return res -------------------------------------------------------------------------------- /count-the-digits-that-divide-a-number.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def countDigits(self, num: int) -> int: 3 | numcpy = num 4 | ctr = 0 5 | while numcpy: 6 | d = numcpy % 10 7 | if num % d == 0: 8 | ctr += 1 9 | numcpy //= 10 10 | return ctr -------------------------------------------------------------------------------- /find-the-k-or-of-an-array.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findKOr(self, nums: List[int], k: int) -> int: 3 | ctr = [0] * 32 4 | for el in nums: 5 | for b in range(32): 6 | if el & (1 << b): 7 | ctr[b] += 1 8 | return sum(1 << b for b in range(32) if ctr[b] >= k) -------------------------------------------------------------------------------- /find-the-number-of-winning-players.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def winningPlayerCount(self, n: int, pick: List[List[int]]) -> int: 3 | ctr = [Counter() for _ in range(n)] 4 | for x, y in pick: 5 | ctr[x][y] += 1 6 | return len([i for i in range(n) if len(ctr[i]) > 0 and max(ctr[i].values()) > i]) -------------------------------------------------------------------------------- /maximum-difference-between-adjacent-elements-in-a-circular-array.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def maxAdjacentDistance(self, nums: List[int]) -> int: 3 | n = len(nums) 4 | res = abs(nums[0] - nums[-1]) 5 | for i in range(n - 1): 6 | res = max(res, abs(nums[i + 1] - nums[i])) 7 | return res -------------------------------------------------------------------------------- /minimum-distance-to-the-target-element.py: -------------------------------------------------------------------------------- 1 | import heapq 2 | 3 | class Solution: 4 | def getMinDistance(self, nums: List[int], target: int, start: int) -> int: 5 | heap = [] 6 | for i, num in enumerate(nums): 7 | heapq.heappush(heap, (abs(num - target), abs(i - start))) 8 | return heap[0][1] -------------------------------------------------------------------------------- /miscellaneous/B_chess_960.py: -------------------------------------------------------------------------------- 1 | s = input() 2 | 3 | b = [] 4 | r = [] 5 | k = -1 6 | 7 | for i, c in enumerate(s): 8 | if c == 'B': 9 | b.append(i) 10 | if c == 'R': 11 | r.append(i) 12 | if c == 'K': 13 | k = i 14 | 15 | print("Yes" if b[0] % 2 != b[1] % 2 and r[0] < k < r[1] else "No") -------------------------------------------------------------------------------- /miscellaneous/Crazy_Bishops_on_Chessboard.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | 3 | for _ in range(t): 4 | n = int(input()) 5 | odd = (n + 1) // 2 6 | even = n - odd 7 | odd -= 1 8 | res = min(even, odd) * 3 9 | res += max(odd, even) - min(odd, even) 10 | if n >= 2: 11 | res -= 1 12 | print(res) -------------------------------------------------------------------------------- /miscellaneous/array.py: -------------------------------------------------------------------------------- 1 | n, m, p = map(int, input().split()) 2 | 3 | arr = [0] * n 4 | 5 | freq = [0] * (n + 1) 6 | 7 | for _ in range(m): 8 | l, r, k = map(int, input().split()) 9 | freq[l - 1] += 1 10 | freq[r] -= 1 11 | 12 | for i in range(1, n + 1): 13 | freq[i] += freq[i - 1] 14 | 15 | print(freq) -------------------------------------------------------------------------------- /peak-index-in-a-mountain-array.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def peakIndexInMountainArray(self, arr: List[int]) -> int: 3 | old = True 4 | new = True 5 | for i in range(len(arr) - 1): 6 | new = arr[i + 1] > arr[i] 7 | if new ^ old: 8 | return i 9 | old = new -------------------------------------------------------------------------------- /remove-duplicates-from-sorted-array.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def removeDuplicates(self, nums: List[int]) -> int: 3 | n = len(nums) 4 | i = 0 5 | for j in range(n): 6 | if j > 0 and nums[j] != nums[j - 1]: 7 | i += 1 8 | nums[i] = nums[j] 9 | return i + 1 -------------------------------------------------------------------------------- /swap-nodes-in-pairs.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def swapPairs(self, head: Optional[ListNode]) -> Optional[ListNode]: 3 | if not head or not head.next: 4 | return head 5 | newhead = head.next 6 | head.next = self.swapPairs(head.next.next) 7 | newhead.next = head 8 | return newhead -------------------------------------------------------------------------------- /ugly-number.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isUgly(self, n: int) -> bool: 3 | if n == 0: 4 | return False 5 | while n % 2 == 0: 6 | n = n // 2 7 | while n % 3 == 0: 8 | n = n // 3 9 | while n % 5 == 0: 10 | n = n // 5 11 | return n == 1 -------------------------------------------------------------------------------- /defanging-an-ip-address.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def defangIPaddr(self, address: str) -> str: 3 | n = len(address) 4 | op = "" 5 | for i in range(n): 6 | if address[i] == ".": 7 | op += "[.]" 8 | else: 9 | op += address[i] 10 | return op -------------------------------------------------------------------------------- /determine-if-two-strings-are-close.py: -------------------------------------------------------------------------------- 1 | from collections import Counter 2 | 3 | class Solution: 4 | def closeStrings(self, word1: str, word2: str) -> bool: 5 | ctr1 = Counter(word1) 6 | ctr2 = Counter(word2) 7 | return sorted(ctr1.keys()) == sorted(ctr2.keys()) and sorted(ctr1.values()) == sorted(ctr2.values()) -------------------------------------------------------------------------------- /final-value-of-variable-after-performing-operations.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def finalValueAfterOperations(self, operations: List[str]) -> int: 3 | x = 0 4 | for o in operations: 5 | if o[1] == '+': 6 | x += 1 7 | if o[1] == '-': 8 | x -= 1 9 | return x -------------------------------------------------------------------------------- /find-maximum-number-of-string-pairs.py: -------------------------------------------------------------------------------- 1 | from collections import Counter 2 | 3 | class Solution: 4 | def maximumNumberOfStringPairs(self, words: List[str]) -> int: 5 | res = 0 6 | ctr = Counter() 7 | for w in words: 8 | res += ctr[w[::-1]] 9 | ctr[w] += 1 10 | return res -------------------------------------------------------------------------------- /find-the-n-th-value-after-k-seconds.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def valueAfterKSeconds(self, n: int, k: int) -> int: 3 | res = [1] * n 4 | M = 10 ** 9 + 7 5 | for _ in range(k): 6 | for i in range(1, n): 7 | res[i] += res[i - 1] 8 | res[i] %= M 9 | return res[-1] -------------------------------------------------------------------------------- /maximum-number-of-operations-with-the-same-score-i.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def maxOperations(self, nums: List[int]) -> int: 3 | n = len(nums) 4 | res = 0 5 | i = 0 6 | while i < n - 1 and nums[i] + nums[i + 1] == nums[0] + nums[1]: 7 | i += 2 8 | res += 1 9 | return res -------------------------------------------------------------------------------- /maximum-strength-of-a-group.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def maxStrength(self, nums: List[int]) -> int: 3 | nums.sort(key = lambda x: (2 if x == 0 else int(x < 0), -abs(x))) 4 | res = float('-inf') 5 | p = 1 6 | for el in nums: 7 | p *= el 8 | res = max(res, p) 9 | return res -------------------------------------------------------------------------------- /min-cost-climbing-stairs.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def minCostClimbingStairs(self, cost: List[int]) -> int: 3 | n = len(cost) 4 | dp = [float('inf')] * (n + 2) 5 | dp[n] = 0 6 | for i in range(n - 1, -1, -1): 7 | dp[i] = cost[i] + min(dp[i + 1], dp[i + 2]) 8 | return min(dp[0], dp[1]) -------------------------------------------------------------------------------- /minimum-right-shifts-to-sort-the-array.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def minimumRightShifts(self, nums: List[int]) -> int: 3 | n = len(nums) 4 | nums = nums + nums 5 | for i in range(n): 6 | if nums[n - i : 2 * n - i] == sorted(nums[n - i : 2 * n - i]): 7 | return i 8 | return -1 -------------------------------------------------------------------------------- /miscellaneous/A_Sasha_and_Array_Coloring.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | 3 | for _ in range(t): 4 | n = int(input()) 5 | arr = sorted(map(int, input().split())) 6 | i = 0 7 | j = n - 1 8 | res = 0 9 | while i < j: 10 | res += arr[j] - arr[i] 11 | j -= 1 12 | i += 1 13 | print(res) -------------------------------------------------------------------------------- /miscellaneous/D_-_Freefall.py: -------------------------------------------------------------------------------- 1 | import math 2 | 3 | a, b = map(int, input().split()) 4 | 5 | def val(x, a, b): 6 | return b * x + a * math.sqrt(1 + x) / (1 + x) 7 | 8 | x = (a / (2 * b)) ** (2/ 3) - 1 9 | 10 | if x < 0: 11 | print(val(0, a, b)) 12 | else: 13 | print(min(val(int(x), a, b), val(int(x) + 1, a, b))) -------------------------------------------------------------------------------- /miscellaneous/Find_eX.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | 3 | def GCD(a, b): 4 | while b: 5 | a, b = b, a % b 6 | return a 7 | 8 | for _ in range(t): 9 | a, b, c, d = map(int, input().split()) 10 | if (a + 1) % b == (c + 1) % d: 11 | print(1) 12 | else: 13 | print(b * d // GCD(b, d) - a % b) -------------------------------------------------------------------------------- /rectangle-overlap.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isRectangleOverlap(self, rec1: List[int], rec2: List[int]) -> bool: 3 | cx1 = max(rec1[0], rec2[0]) 4 | cx2 = min(rec1[2], rec2[2]) 5 | cy1 = max(rec1[1], rec2[1]) 6 | cy2 = min(rec1[3], rec2[3]) 7 | return max(cx2 - cx1, 0) * max(cy2 - cy1, 0) != 0 -------------------------------------------------------------------------------- /repeated-dna-sequences.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findRepeatedDnaSequences(self, s: str) -> List[str]: 3 | n = len(s) 4 | ctr = {} 5 | for i in range(0, n - 9): 6 | curr = s[i:i+10] 7 | ctr[curr] = ctr.get(curr, 0) + 1 8 | return [patt for patt, freq in ctr.items() if freq > 1] -------------------------------------------------------------------------------- /row-with-maximum-ones.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def rowAndMaximumOnes(self, mat: List[List[int]]) -> List[int]: 3 | m = len(mat) 4 | n = len(mat[0]) 5 | res = (float('-inf'), float('inf')) 6 | for i in range(m): 7 | res = max(res, (mat[i].count(1), -i)) 8 | return [-res[1], res[0]] -------------------------------------------------------------------------------- /cells-in-a-range-on-an-excel-sheet.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def cellsInRange(self, s: str) -> List[str]: 3 | beg, end = s.split(":") 4 | a = ord(beg[0]) 5 | m = int(beg[1]) 6 | b = ord(end[0]) 7 | n = int(end[1]) 8 | return [chr(x) + str(y) for x in range(a, b + 1) for y in range(m, n + 1)] -------------------------------------------------------------------------------- /circular-sentence.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isCircularSentence(self, sentence: str) -> bool: 3 | n = len(sentence) 4 | for i in range(n): 5 | if sentence[i] == " ": 6 | if sentence[i - 1] != sentence[i + 1]: 7 | return False 8 | return sentence[0] == sentence[-1] -------------------------------------------------------------------------------- /complex-number-multiplication.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def complexNumberMultiply(self, num1: str, num2: str) -> str: 3 | a, b = num1.split("+") 4 | c, d = num2.split("+") 5 | a = int(a) 6 | b = int(b[:-1]) 7 | c = int(c) 8 | d = int(d[:-1]) 9 | return "{}+{}i".format(a*c-b*d, a*d+b*c) -------------------------------------------------------------------------------- /implement-rand10-using-rand7.py: -------------------------------------------------------------------------------- 1 | # The rand7() API is already defined for you. 2 | # def rand7(): 3 | # @return a random integer in the range 1 to 7 4 | 5 | class Solution: 6 | def rand10(self): 7 | total = 0 8 | for _ in range(10): 9 | total += rand7() 10 | return 1 + total % 10 11 | -------------------------------------------------------------------------------- /integer-break.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def integerBreak(self, n: int) -> int: 3 | if n == 2: 4 | return 1 5 | if n == 3: 6 | return 2 7 | res = 1 8 | while n % 3 != 0: 9 | res *= 2 10 | n -= 2 11 | res *= pow(3, n // 3) 12 | return res -------------------------------------------------------------------------------- /minimum-number-of-changes-to-make-binary-string-beautiful.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def minChanges(self, s: str) -> int: 3 | n = len(s) 4 | res = 0 5 | for i in range(0, n, 2): 6 | cost = int(s[i]) + int(s[i + 1]) 7 | cost = min(cost, 2 - cost) 8 | res += cost 9 | return res -------------------------------------------------------------------------------- /miscellaneous/B_Permutations_Primes.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | 3 | for _ in range(t): 4 | n = int(input()) 5 | if n <= 2: 6 | print(*list(range(n, 0, -1))) 7 | continue 8 | xx = [i for i in range(1, n + 1) if i not in {1, 2, 3}] 9 | p = len(xx) 10 | print(*([2] + xx[:p//2] + [1] + xx[p//2:] + [3])) -------------------------------------------------------------------------------- /miscellaneous/C_Centers.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | 3 | arr = list(map(int, input().split())) 4 | 5 | seen = [0] * n 6 | 7 | f = [-1] * n 8 | 9 | for i in range(3 * n): 10 | seen[arr[i] - 1] += 1 11 | if seen[arr[i] - 1] == 2: 12 | f[arr[i] - 1] = i 13 | 14 | print(*sorted(range(1, n + 1), key = lambda x: f[x - 1])) -------------------------------------------------------------------------------- /miscellaneous/MAX_CANDY.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | 3 | for _ in range(t): 4 | n = int(input()) 5 | arr = list(map(int, input().split())) 6 | dp = [0] * (n + 2) 7 | for i in range(n - 1, -1, -1): 8 | dp[i] = max(arr[i] + dp[i + 2], dp[i + 1]) 9 | dp[i] = max(dp[i], dp[i + 1]) 10 | print(dp[0]) 11 | -------------------------------------------------------------------------------- /relative-sort-array.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def relativeSortArray(self, arr1: List[int], arr2: List[int]) -> List[int]: 3 | pos = {} 4 | for i in range(len(arr2)): 5 | if arr2[i] not in pos: 6 | pos[arr2[i]] = (i, i) 7 | return sorted(arr1, key = lambda x: pos.get(x, (float('inf'), x))) -------------------------------------------------------------------------------- /solving-questions-with-brainpower.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def mostPoints(self, questions: List[List[int]]) -> int: 3 | n = len(questions) 4 | dp = [0] * (n + 1) 5 | for i in range(n - 1, -1, -1): 6 | dp[i] = max(dp[i + 1], questions[i][0] + dp[min(i + questions[i][1] + 1, n)]) 7 | return dp[0] -------------------------------------------------------------------------------- /check-if-all-as-appears-before-all-bs.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def checkString(self, s: str) -> bool: 3 | bseen = False 4 | for c in s: 5 | if c == 'b': 6 | bseen = True 7 | if c == 'a': 8 | if bseen: 9 | return False 10 | return True -------------------------------------------------------------------------------- /check-if-array-pairs-are-divisible-by-k.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def canArrange(self, arr: List[int], k: int) -> bool: 3 | ctr = [0] * k 4 | for el in arr: 5 | ctr[el % k] += 1 6 | for i in range(1, k): 7 | if ctr[i] != ctr[k - i]: 8 | return False 9 | return ctr[0] % 2 == 0 -------------------------------------------------------------------------------- /check-if-n-and-its-double-exist.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def checkIfExist(self, arr: List[int]) -> bool: 3 | arr.sort(reverse = True, key = lambda x: abs(x)) 4 | exists = set() 5 | for num in arr: 6 | if num * 2 in exists: 7 | return True 8 | exists.add(num) 9 | return False -------------------------------------------------------------------------------- /excel-sheet-column-title.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def convertToTitle(self, columnNumber: int) -> str: 3 | res = [] 4 | while columnNumber: 5 | columnNumber -= 1 6 | res.append(chr(ord('A') + columnNumber % 26)) 7 | columnNumber //= 26 8 | res.reverse() 9 | return "".join(res) -------------------------------------------------------------------------------- /find-all-duplicates-in-an-array.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findDuplicates(self, nums: List[int]) -> List[int]: 3 | exists = set() 4 | op = [] 5 | for num in nums: 6 | if num in exists: 7 | op.append(num) 8 | else: 9 | exists.add(num) 10 | return op -------------------------------------------------------------------------------- /find-peak-element.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findPeakElement(self, nums: List[int]) -> int: 3 | n = len(nums) 4 | for i in range(n): 5 | left = nums[i] > nums[i - 1] if i > 0 else True 6 | right = nums[i] > nums[i + 1] if i < n - 1 else True 7 | if left and right: 8 | return i -------------------------------------------------------------------------------- /longest-palindrome.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def longestPalindrome(self, s: str) -> int: 3 | res = 0 4 | extra = 0 5 | ctr = Counter(s) 6 | for c in ctr: 7 | res += ctr[c] - ctr[c] % 2 8 | if ctr[c] & 1: 9 | extra = 1 10 | res += extra 11 | return res -------------------------------------------------------------------------------- /make-three-strings-equal.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findMinimumOperations(self, s1: str, s2: str, s3: str) -> int: 3 | k = 0 4 | while k < min(len(s1), len(s2), len(s3)) and s1[k] == s2[k] == s3[k]: 5 | k += 1 6 | if k == 0: 7 | return -1 8 | return len(s1) + len(s2) + len(s3) - 3 * k -------------------------------------------------------------------------------- /maximum-69-number.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def maximum69Number (self, num: int) -> int: 3 | currmax = num 4 | numstr = str(num) 5 | if '6' in numstr: 6 | i = numstr.index('6') 7 | curr = int(numstr[0:i] + '9' + numstr[i+1:]) 8 | currmax = max(currmax, curr) 9 | return currmax -------------------------------------------------------------------------------- /maximum-number-of-pairs-in-array.py: -------------------------------------------------------------------------------- 1 | from collections import Counter 2 | 3 | class Solution: 4 | def numberOfPairs(self, nums: List[int]) -> List[int]: 5 | ctr = Counter(nums) 6 | res = [0, 0] 7 | for el in ctr: 8 | res[0] += ctr[el] // 2 9 | res[1] += ctr[el] % 2 10 | return res -------------------------------------------------------------------------------- /miscellaneous/A_Beautiful_Sequence.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | 3 | def check(arr): 4 | n = len(arr) 5 | for i in range(n): 6 | if arr[i] <= i + 1: 7 | return "YES" 8 | return "NO" 9 | 10 | for _ in range(t): 11 | n = int(input()) 12 | arr = list(map(int, input().split())) 13 | print(check(arr)) -------------------------------------------------------------------------------- /miscellaneous/B_Tea_with_Tangerines.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | 3 | for _ in range(t): 4 | n = int(input()) 5 | arr = list(map(int, input().split())) 6 | k = min(arr) 7 | res = 0 8 | for el in arr: 9 | res += el // (2 * k - 1) 10 | if el % (2 * k - 1) == 0: 11 | res -= 1 12 | print(res) -------------------------------------------------------------------------------- /miscellaneous/C_Merge_Sequences.py: -------------------------------------------------------------------------------- 1 | m, n = map(int, input().split()) 2 | 3 | a = list(map(int, input().split())) 4 | b = list(map(int, input().split())) 5 | 6 | res = sorted(a + b) 7 | 8 | pos = {} 9 | 10 | for i in range(m + n): 11 | pos[res[i]] = i + 1 12 | 13 | print(*[pos[el] for el in a]) 14 | print(*[pos[el] for el in b]) -------------------------------------------------------------------------------- /miscellaneous/C_Min_Max_Pair.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | 3 | arr = list(map(int, input().split())) 4 | 5 | res = 0 6 | 7 | ctr = 0 8 | 9 | for i in range(n): 10 | if arr[i] == i + 1: 11 | res += ctr 12 | ctr += 1 13 | elif 1 <= arr[i] <= i and arr[arr[i] - 1] == i + 1: 14 | res += 1 15 | 16 | print(res) -------------------------------------------------------------------------------- /miscellaneous/D_Count_Subtractions.py: -------------------------------------------------------------------------------- 1 | a, b = map(int, input().split()) 2 | 3 | res = 0 4 | 5 | while a != b: 6 | if a == 0 or b == 0: 7 | res -= 1 8 | break 9 | if a < b: 10 | res += b // a 11 | b = b % a 12 | elif a > b: 13 | res += a // b 14 | a = a % b 15 | 16 | print(res) -------------------------------------------------------------------------------- /miscellaneous/denom.py: -------------------------------------------------------------------------------- 1 | while True: 2 | denom = [3, 4] 3 | print("Enter value: ") 4 | n = int(input()) 5 | while len(denom) > 0 and n > 0: 6 | curr = denom.pop() 7 | if n >= curr: 8 | n = n % curr 9 | if n == 0: 10 | print("YES") 11 | else: 12 | print("NO") 13 | 14 | -------------------------------------------------------------------------------- /points-that-intersect-with-cars.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def numberOfPoints(self, nums: List[List[int]]) -> int: 3 | ctr = [0] * 102 4 | for a, b in nums: 5 | ctr[a] += 1 6 | ctr[b + 1] -= 1 7 | for i in range(1, 102): 8 | ctr[i] += ctr[i - 1] 9 | return len(ctr) - ctr.count(0) -------------------------------------------------------------------------------- /powx-n.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def myPow(self, x: float, n: int) -> float: 3 | if n < 0: 4 | return 1 / self.myPow(x, -n) 5 | if n == 0: 6 | return 1 7 | sub = self.myPow(x, n // 2) 8 | res = sub * sub 9 | if n % 2 == 1: 10 | res *= x 11 | return res -------------------------------------------------------------------------------- /removing-minimum-and-maximum-from-array.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def minimumDeletions(self, nums: List[int]) -> int: 3 | n = len(nums) 4 | a = min(nums) 5 | b = max(nums) 6 | i = nums.index(a) 7 | j = nums.index(b) 8 | i, j = min(i, j), max(i, j) 9 | return min(i + 1 + n - j, j + 1, n - i) -------------------------------------------------------------------------------- /removing-stars-from-a-string.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def removeStars(self, s: str) -> str: 3 | stack = [] 4 | for c in s: 5 | if c == "*": 6 | if len(stack) > 0: 7 | stack.pop() 8 | else: 9 | stack.append(c) 10 | return "".join(stack) -------------------------------------------------------------------------------- /special-array-with-x-elements-greater-than-or-equal-x.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def specialArray(self, nums: List[int]) -> int: 3 | nums.sort(reverse=True) 4 | i = 0 5 | while i < len(nums) and nums[i] > i: 6 | i += 1 7 | if i < len(nums) and i == nums[i]: 8 | return -1 9 | return i -------------------------------------------------------------------------------- /convert-1d-array-into-2d-array.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def construct2DArray(self, original: List[int], m: int, n: int) -> List[List[int]]: 3 | k = len(original) 4 | if k != m * n: 5 | return [] 6 | for i in range(m): 7 | original.extend([original[i * n : (i + 1) * n]]) 8 | return original[k:] -------------------------------------------------------------------------------- /count-pairs-whose-sum-is-less-than-target.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def countPairs(self, nums: List[int], target: int) -> int: 3 | n = len(nums) 4 | res = 0 5 | for i in range(n): 6 | for j in range(i + 1, n): 7 | if nums[i] + nums[j] < target: 8 | res += 1 9 | return res -------------------------------------------------------------------------------- /divide-a-string-into-groups-of-size-k.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def divideString(self, s: str, k: int, fill: str) -> List[str]: 3 | n = len(s) 4 | s += fill * (n % k if n % k == 0 else k - n % k) 5 | n = len(s) 6 | op = [] 7 | for i in range(0, n, k): 8 | op.append(s[i:i+k]) 9 | return op -------------------------------------------------------------------------------- /find-the-number-of-copy-arrays.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def countArrays(self, original: List[int], bounds: List[List[int]]) -> int: 3 | n = len(original) 4 | up = min(bounds[i][1] - original[i] for i in range(n)) 5 | down = max(bounds[i][0] - original[i] for i in range(n)) 6 | return up - down + 1 if up >= down else 0 -------------------------------------------------------------------------------- /find-the-score-of-all-prefixes-of-an-array.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findPrefixScore(self, nums: List[int]) -> List[int]: 3 | m = 0 4 | for i in range(len(nums)): 5 | m = max(m, nums[i]) 6 | nums[i] += m 7 | for i in range(1, len(nums)): 8 | nums[i] += nums[i - 1] 9 | return nums -------------------------------------------------------------------------------- /find-the-sequence-of-strings-appeared-on-the-screen.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def stringSequence(self, target: str) -> List[str]: 3 | res = [] 4 | s = "" 5 | for c in target: 6 | for x in range(ord(c) - ord('a') + 1): 7 | res.append(s + chr(ord('a') + x)) 8 | s += c 9 | return res -------------------------------------------------------------------------------- /minimum-array-end.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def minEnd(self, n: int, x: int) -> int: 3 | res = x 4 | inc = n - 1 5 | p = 0 6 | for b in range(62): 7 | if not x & (1 << b): 8 | if inc & (1 << p): 9 | res += 1 << b 10 | p += 1 11 | return res -------------------------------------------------------------------------------- /minimum-depth-of-binary-tree.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def minDepth(self, root: Optional[TreeNode]) -> int: 3 | if root: 4 | a = self.minDepth(root.left) 5 | b = self.minDepth(root.right) 6 | if a == 0 or b == 0: 7 | return a + b + 1 8 | return 1 + min(a, b) 9 | return 0 -------------------------------------------------------------------------------- /miscellaneous/A_Alternately.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | 3 | same = True 4 | 5 | s = input() 6 | 7 | for i in range(0, n, 2): 8 | if s[i] != s[0]: 9 | same = False 10 | break 11 | 12 | for i in range(1, n, 2): 13 | if s[i] != s[1]: 14 | same = False 15 | break 16 | 17 | print("Yes" if same else "No") -------------------------------------------------------------------------------- /miscellaneous/A_Dice_Game.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | 3 | for _ in range(t): 4 | a, b = map(int, input().split()) 5 | c, d = map(int, input().split()) 6 | if b <= c: 7 | print("NO") 8 | continue 9 | if d <= a: 10 | print("YES") 11 | continue 12 | overlap = min(b, d) - max(a, c) 13 | alice = -------------------------------------------------------------------------------- /miscellaneous/A_Escalator_Conversations.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | 3 | for _ in range(t): 4 | n, m, k, H = map(int, input().split()) 5 | heights = map(int, input().split()) 6 | res = 0 7 | for h in heights: 8 | if h != H and abs(h - H) % k == 0 and abs(h - H) // k <= m - 1: 9 | res += 1 10 | print(res) -------------------------------------------------------------------------------- /miscellaneous/B_Default_Price.py: -------------------------------------------------------------------------------- 1 | n, m = map(int, input().split()) 2 | 3 | a = input().split() 4 | 5 | b = input().split() 6 | 7 | p = list(map(int, input().split())) 8 | 9 | mp = {} 10 | 11 | for i in range(m): 12 | mp[b[i]] = p[i + 1] 13 | 14 | res = 0 15 | 16 | for el in a: 17 | res += mp.get(el, p[0]) 18 | 19 | print(res) -------------------------------------------------------------------------------- /miscellaneous/B_Expansion_coefficient_of_the_array.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | 3 | arr = list(map(int, input().split())) 4 | 5 | k = float('inf') 6 | 7 | for i in range(n): 8 | if i > 0: 9 | k = min(k, min(arr[i], arr[0]) // i) 10 | if i < n - 1: 11 | k = min(k, min(arr[i], arr[-1]) // (n - i - 1)) 12 | 13 | print(k) -------------------------------------------------------------------------------- /miscellaneous/B_Ten_Words_of_Wisdom.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | 3 | res = [] 4 | 5 | for _ in range(t): 6 | n = int(input()) 7 | cres = (0, -1) 8 | for i in range(n): 9 | a, b = map(int, input().split()) 10 | if a <= 10: 11 | cres = max(cres, (b, i + 1)) 12 | res.append(cres[1]) 13 | 14 | print(*res) -------------------------------------------------------------------------------- /miscellaneous/C_Gap_Existence.py: -------------------------------------------------------------------------------- 1 | n, x = map(int, input().split()) 2 | 3 | arr = list(map(int, input().split())) 4 | 5 | def check(arr, x): 6 | seen = set() 7 | for el in arr: 8 | seen.add(el) 9 | if el - x in seen or el + x in seen: 10 | return "Yes" 11 | return "No" 12 | 13 | print(check(arr, x)) -------------------------------------------------------------------------------- /miscellaneous/F_Multi-Colored_Segments.py: -------------------------------------------------------------------------------- 1 | from collections import defaultdict 2 | import bisect 3 | 4 | t = int(input()) 5 | 6 | for _ in range(t): 7 | n = int(input()) 8 | segs = defaultdict(list) 9 | for __ in range(n): 10 | l, r, c = map(int, input().split()) 11 | bisect.insort(segs[c], (l, r)) 12 | -------------------------------------------------------------------------------- /miscellaneous/Sum_of_Divisors.py: -------------------------------------------------------------------------------- 1 | M = 10 ** 9 + 7 2 | 3 | n = int(input()) 4 | 5 | res = 0 6 | 7 | if n <= 10 ** 6: 8 | for i in range(1, n + 1): 9 | res += i * (n // i) 10 | res %= M 11 | else: 12 | for i in range(1, n // (10 ** 6) + 1): 13 | res += i * (n // i) 14 | res %= M 15 | 16 | print(res) -------------------------------------------------------------------------------- /miscellaneous/Unstable_Subarray.py: -------------------------------------------------------------------------------- 1 | from collections import Counter 2 | 3 | t = int(input()) 4 | 5 | for _ in range(t): 6 | n = int(input()) 7 | arr = list(map(int, input().split())) 8 | res = n * (n - 1) // 2 9 | ctr = Counter(arr) 10 | for el in ctr: 11 | res -= ctr[el] * (ctr[el] - 1) // 2 12 | print(res) -------------------------------------------------------------------------------- /miscellaneous/counting_rectangles.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | 3 | for _ in range(t): 4 | n, q = map(int, input().split()) 5 | rects = [] 6 | for __ in range(n): 7 | h, w = map(int, input().split()) 8 | rects.append((h, w)) 9 | for __ in range(q): 10 | hs, ws, hb, wb = map(int, input().split()) 11 | -------------------------------------------------------------------------------- /miscellaneous/good_string.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | 3 | for _ in range(t): 4 | n = int(input()) 5 | s = input() 6 | ctr = 0 7 | for c in s: 8 | if c == ')': 9 | ctr += 1 10 | else: 11 | ctr = 0 12 | if ctr > n - ctr: 13 | print("Yes") 14 | else: 15 | print("No") -------------------------------------------------------------------------------- /number-of-even-and-odd-bits.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def evenOddBit(self, n: int) -> List[int]: 3 | even = odd = 0 4 | for b in range(32): 5 | if n & (1 << b): 6 | if b & 1: 7 | odd += 1 8 | else: 9 | even += 1 10 | return [even, odd] -------------------------------------------------------------------------------- /truncate-sentence.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def truncateSentence(self, s: str, k: int) -> str: 3 | n = len(s) 4 | i = 0 5 | ctr = 0 6 | for i in range(n): 7 | if s[i] == " ": 8 | ctr += 1 9 | if ctr == k: 10 | return s[:i] 11 | return s -------------------------------------------------------------------------------- /combination-sum-iv.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def combinationSum4(self, nums: List[int], target: int) -> int: 3 | dp = [0] * (target + 1) 4 | dp[0] = 1 5 | for i in range(1, target + 1): 6 | for num in nums: 7 | if i - num >= 0: 8 | dp[i] += dp[i - num] 9 | return dp[target] -------------------------------------------------------------------------------- /count-pairs-that-form-a-complete-day-i.py: -------------------------------------------------------------------------------- 1 | from collections import Counter 2 | 3 | class Solution: 4 | def countCompleteDayPairs(self, hours: List[int]) -> int: 5 | res = 0 6 | ctr = Counter() 7 | for el in hours: 8 | res += ctr[(24 - (el % 24)) % 24] 9 | ctr[el % 24] += 1 10 | return res -------------------------------------------------------------------------------- /count-pairs-that-form-a-complete-day-ii.py: -------------------------------------------------------------------------------- 1 | from collections import Counter 2 | 3 | class Solution: 4 | def countCompleteDayPairs(self, hours: List[int]) -> int: 5 | res = 0 6 | ctr = Counter() 7 | for el in hours: 8 | res += ctr[(24 - (el % 24)) % 24] 9 | ctr[el % 24] += 1 10 | return res -------------------------------------------------------------------------------- /count-the-hidden-sequences.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def numberOfArrays(self, differences: List[int], lower: int, upper: int) -> int: 3 | pos = [0] 4 | for d in differences: 5 | pos.append(pos[-1] + d) 6 | least = min(pos) 7 | pos = [p + lower - least for p in pos] 8 | return max(upper - max(pos) + 1, 0) -------------------------------------------------------------------------------- /destination-city.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def destCity(self, paths: List[List[str]]) -> str: 3 | cities = set() 4 | for a, b in paths: 5 | cities.add(a) 6 | cities.add(b) 7 | for a, b in paths: 8 | if a in cities: 9 | cities.remove(a) 10 | return list(cities)[0] -------------------------------------------------------------------------------- /final-prices-with-a-special-discount-in-a-shop.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def finalPrices(self, prices: List[int]) -> List[int]: 3 | stack = [] 4 | for i, el in enumerate(prices): 5 | while stack and el <= prices[stack[-1]]: 6 | prices[stack.pop()] -= el 7 | stack.append(i) 8 | return prices -------------------------------------------------------------------------------- /find-the-original-typed-string-i.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def possibleStringCount(self, word): 3 | n = len(word) 4 | res = n + 1 5 | i = 0 6 | while i < n: 7 | while i < n - 1 and word[i] == word[i + 1]: 8 | i += 1 9 | res -= 1 10 | i += 1 11 | return res -------------------------------------------------------------------------------- /kth-missing-positive-number.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findKthPositive(self, arr: List[int], k: int) -> int: 3 | arr = set(arr) 4 | mx = max(arr) + k + 1 5 | ctr = 0 6 | for i in range(1, mx): 7 | if i not in arr: 8 | ctr += 1 9 | if ctr == k: 10 | return i --------------------------------------------------------------------------------