├── .gitignore ├── .idea ├── .gitignore ├── Competitive-Practice.iml ├── misc.xml ├── modules.xml └── vcs.xml ├── .vscode ├── settings.json └── template.code-snippets ├── CP-Prac.iml ├── Contest ├── .vscode │ └── template.code-snippets ├── Classes │ ├── CountOddSegTree.class │ ├── Main$FastReader.class │ ├── Main$pair.class │ ├── Main.class │ ├── MaxSegTree.class │ ├── SegTree.class │ └── SumSegTree.class ├── Main.java ├── Template.java ├── file.py ├── input.txt ├── javarun.py └── output.txt ├── DOCS ├── Kartik's CF Specialist Sheet.docx └── Leetcode_FULL.xlsx ├── Leetcode ├── BIT_Trie │ ├── Maximum_XOR_With_an_Element_From_Array.java │ └── Maximum_XOR_of_Two_Numbers_in_an_Array.java ├── Binary_Search │ ├── 132_Pattern.java │ ├── Avoid_Flood_in_The_City.java │ ├── Binary_Search.java │ ├── Capacity_To_Ship_Packages_Within_D_Days.java │ ├── Contains_Duplicate_III.java │ ├── Count_the_Number_of_Fair_Pairs.java │ ├── Cutting_Ribbons.java │ ├── Data_Stream_as_Disjoint_Intervals.java │ ├── Escape_the_Spreading_Fire.java │ ├── Find_First_and_Last_Position_of_Element_in_Sorted_Array.java │ ├── Find_K_Closest_Elements.py │ ├── Find_K_th_Smallest_Pair_Distance.java │ ├── Find_Minimum_Time_to_Finish_All_Jobs.java │ ├── Find_Minimum_in_Rotated_Sorted_Array.java │ ├── Find_Peak_Element.java │ ├── Find_Smallest_Common_Element_in_All_Rows.java │ ├── First_Bad_Version.md │ ├── Get_Equal_Substrings_Within_Budget.java │ ├── Heaters.java │ ├── Koko_Eating_Bananas.java │ ├── Kth_Smallest_Element_in_a_Sorted_Matrix.java │ ├── Last Day Where You Can Still Cross.md │ ├── Longest_Duplicate_Substring.java │ ├── Magnetic_Force_Between_Two_Balls.java │ ├── Maximum_Distance_Between_a_Pair_of_Values.java │ ├── Maximum_Value_at_a_Given_Index_in_a_Bounded_Array.java │ ├── Maximum_Width_Ramp.java │ ├── Minimum_Limit_of_Balls_in_a_Bag.java │ ├── Minimum_Number_of_Days_to_Make_m_Bouquets.java │ ├── Minimum_Operations_to_Make_the_Array_K_Increasing.java │ ├── Minimum_Speed_to_Arrive_on_Time.java │ ├── Minimum_Time_to_Complete_Trips.java │ ├── My_Calendar_I.java │ ├── Path_With_Minimum_Effort.java │ ├── Range_Frequency_Queries.java │ ├── Russian_Doll_Envelopes.java │ ├── Search_in_Rotated_Sorted_Array.java │ ├── Single_Element_in_a_Sorted_Array.java │ ├── Split_Array_Largest_Sum.java │ ├── Stock_Price_Fluctuation.java │ ├── Time_Based_Key_Value_Store.java │ ├── TopVotedCandidate.java │ └── Ways_to_Split_Array_Into_Three_Subarrays.java ├── Bucket │ └── Maximum_Gap.java ├── CyclicSort │ ├── Find_All_Duplicates_in_an_Array.java │ └── First_Missing_Positive.java ├── DP │ ├── 2_Keys_Keyboard.java │ ├── All_Possible_Full_Binary_Trees.java │ ├── Allocate_Mailboxes.java │ ├── Arithmetic_Slices.java │ ├── BUY-SELL STOCK PROBLEMS.md │ ├── Best_Sightseeing_Pair.java │ ├── Best_Team_With_No_Conflicts.py │ ├── Best_Time_to_Buy_and_Sell_Stock.java │ ├── Best_Time_to_Buy_and_Sell_Stock_2.java │ ├── Best_Time_to_Buy_and_Sell_Stock_3.java │ ├── Best_Time_to_Buy_and_Sell_Stock_4.java │ ├── Best_Time_to_Buy_and_Sell_Stock_with_Transaction_Fee.java │ ├── Binary_Tree_Cameras.java │ ├── Binary_Trees_With_Factors.py │ ├── Can_I_Win.java │ ├── Cat_and_Mouse_II.java │ ├── Champagne_Tower.java │ ├── Cherry_Pickup.java │ ├── Cherry_Pickup_II.java │ ├── Climbing_Stairs.py │ ├── Coin_Change.java │ ├── Coin_Change_2.java │ ├── Combination_Sum.md │ ├── Combination_Sum_IV.java │ ├── Constrained_Subsequence_Sum.java │ ├── Count_All_Possible_Routes.java │ ├── Count_Special_Integers.java │ ├── Count_Square_Submatrices_with_All_Ones.java │ ├── Count_Submatrices_With_All_Ones.java │ ├── Count_Vowels_Permutation.java │ ├── Count_and_Say.java │ ├── Decode_Ways.java │ ├── Delete_Operation_for_Two_Strings.java │ ├── Delete_and_Earn.java │ ├── Different_Ways_to_Add_Parentheses.java │ ├── Domino_and_Tromino_Tiling.java │ ├── Dungeon_Game.java │ ├── Edit_Distance.java │ ├── Flip_String_to_Monotone_Increasing.java │ ├── Freedom_Trail.java │ ├── Frog_Jump.java │ ├── Get_the_Maximum_Score.py │ ├── Guess_Number_Higher_or_Lower_II.java │ ├── House_Robber.java │ ├── House_Robber_II.java │ ├── Integer_Replacement.java │ ├── Interleaving_String.java │ ├── Jump_Game_II.java │ ├── Knight_Probability_in_Chess.java │ ├── Largest_Divisible_Subset.java │ ├── Last_Stone_Weight_II.py │ ├── Longest_Arithmetic_Subsequence.java │ ├── Longest_Arithmetic_Subsequence_of_Given_Difference.java │ ├── Longest_Common_Subsequence.java │ ├── Longest_Increasing_Path_in_a_Matrix.java │ ├── Longest_Increasing_Subsequence.java │ ├── Longest_Palindromic_Subsequence.java │ ├── Longest_Palindromic_Substring.java │ ├── Longest_String_Chain.java │ ├── Maximal_Square.java │ ├── Maximize_Score_After_N_Operations.java │ ├── Maximum_Absolute_Sum_of_Any_Subarray.java │ ├── Maximum_Alternating_Subsequence_Sum.java │ ├── Maximum_Deletions_on_a_String.java │ ├── Maximum_Height_by_Stacking_Cuboids.java │ ├── Maximum_Length_of_Pair_Chain.java │ ├── Maximum_Length_of_Subarray_With_Positive_Product.java │ ├── Maximum_Non_Negative_Product_in_a_Matrix.py │ ├── Maximum_Number_of_Events_That_Can_Be_Attended_II.java │ ├── Maximum_Product_Subarray.py │ ├── Maximum_Profit_in_Job_Scheduling.java │ ├── Maximum_Score_from_Performing_Multiplication_Operations.java │ ├── Maximum_Score_from_Performing_Multiplication_Operations.py │ ├── Maximum_Value_of_K_Coins_From_Piles.java │ ├── Minimum_ASCII_Delete_Sum_for_Two_Strings.java │ ├── Minimum_Cost_Tree_From_Leaf_Values.java │ ├── Minimum_Cost_to_Connect_Two_Groups_of_Points.java │ ├── Minimum_Cost_to_Cut_a_Stick.java │ ├── Minimum_Cost_to_Split_an_Array.java │ ├── Minimum_Deletions_to_Make_String_Balanced.java │ ├── Minimum_Difficulty_of_a_Job_Schedule.java │ ├── Minimum_Falling_Path_Sum.java │ ├── Minimum_Falling_Path_Sum_II.java │ ├── Minimum_Insertion_Steps_to_Make_a_String_Palindrome.java │ ├── Minimum_Number_of_Days_to_Eat_N_Oranges.py │ ├── Minimum_Number_of_Removals_to_Make_Mountain_Array.java │ ├── Minimum_Number_of_Work_Sessions_to_Finish_the_Tasks.java │ ├── Minimum_One_Bit_Operations_to_Make_Integers_Zero.java │ ├── Minimum_Path_Sum.java │ ├── Minimum_Sideway_Jumps.java │ ├── Minimum_Swaps_To_Make_Sequences_Increasing.java │ ├── Minimum_Total_Distance_Traveled.java │ ├── Non-negative_Integers_without_Consecutive_Ones.java │ ├── Number_of_Dice_Rolls_With_Target_Sum.java │ ├── Number_of_Increasing_Paths_in_a_Grid.java │ ├── Number_of_Longest_Increasing_Subsequence.java │ ├── Number_of_People_Aware_of_a_Secret.java │ ├── Number_of_Sets_of_K_Non-Overlapping_Line_Segments.py │ ├── Number_of_Smooth_Descent_Periods_of_a_Stock.java │ ├── Number_of_Ways_to_Form_a_Target_String_Given_a_Dictionary.py │ ├── Number_of_Ways_to_Reach_a_Position_After_Exactly_k_Steps.py │ ├── Number_of_Ways_to_Stay_in_the_Same_Place_After_Some_Steps.java │ ├── Number_of_Ways_to_Wear_Different_Hats_to_Each_Other.java │ ├── Numbers_At_Most_N_Given_Digit_Set.java │ ├── Numbers_With_Repeated_Digits.java │ ├── Odd_Even_Jump.java │ ├── Ones_and_Zeroes.java │ ├── Out_of_Boundary_Paths.java │ ├── Paint_house_III.java │ ├── Palindrome_Partitioning_II.java │ ├── Palindrome_Partitioning_III.java │ ├── Palindrome_Partitioning_IV.java │ ├── Palindromic_Substrings.java │ ├── Parallel_Courses_II.java │ ├── Partition_Array_for_Maximum_Sum.java │ ├── Partition_Equal_Subset_Sum.java │ ├── Predict_the_Winner.java │ ├── Reducing_Dishes.java │ ├── Regular_Expression_Matching.java │ ├── Restore_The_Array.java │ ├── Selling_Pieces_of_Wood.java │ ├── Shortest_Common_Supersequence.java │ ├── Solving_Questions_With_Brainpower.java │ ├── Stone_Game_II.java │ ├── Stone_Game_III.java │ ├── Stone_Game_IV.java │ ├── Stone_Game_V.java │ ├── Stone_Game_VII.java │ ├── Stone_Game_VIII.java │ ├── Strange_Printer.java │ ├── String_Compression_II.java │ ├── Target_Sum.java │ ├── Unique_Binary_Search_Trees.java │ ├── Unique_Paths.py │ ├── Unique_Paths_II.java │ ├── Video_Stitching.java │ ├── Wiggle_Subsequence.java │ ├── Word_Break.java │ └── Word_Break_II.java ├── December Coding Challenge │ ├── 4Sum_II.java │ ├── Binary_Search_Tree_Iterator.java │ ├── Burst_Baloons.java │ ├── Can_Place_Flowers.py │ ├── Cherry_Pickup_II.java │ ├── Diagonal_Traverse.java │ ├── Increasing_Order_Search_Tree.java │ ├── Increasing_Triplet_Subsequence.java │ ├── Jump_Game_IV.java │ ├── Maximum_Depth_of_Binary_Tree.java │ ├── Pairs_of_Songs_With_Total_Durations_Divisible_by_60.java │ ├── Palindrome_Partitioning.java │ ├── Plus_One_Linked_List.java │ ├── Populating_Next_Right_Pointers_in_Each_Node_II.java │ ├── Reach_a_Number.java │ ├── Remove_Duplicates_from_Sorted_Array_II.java │ ├── Shortest_Word_Distance.java │ ├── Smallest_Range_II.java │ ├── Smallest_Subtree_with_all_the_Deepest_Nodes.java │ ├── Spiral_Matrix_II.java │ ├── Squares_of_a_Sorted_Array.py │ ├── Swap_Nodes_in_Pairs.java │ ├── The_kth_Factor_of_n.java │ └── Valid_Mountain_Array.java ├── FenwickTree │ ├── Count_of_Range_Sum.java │ ├── HashMap.java │ ├── Minimum_Possible_Integer_After_at_Most_K_Adjacent_Swaps_On_Digits.java │ ├── Number_of_Pairs_Satisfying_Inequality.java │ ├── Queries_on_a_Permutation_With_Key.java │ └── Range_Sum_Query_Mutable.java ├── Geometry │ ├── Erect_the_Fence.java │ ├── Max_Points_on_a_Line.java │ ├── Minimum_Area_Rectangle.java │ └── Minimum_Area_Rectangle_II.java ├── Graphs │ ├── 01_Matrix.java │ ├── Accounts_Merge.java │ ├── All_Ancestors_of_a_Node_in_a_Directed_Acyclic_Graph.java │ ├── All_Paths_From_Source_to_Target.java │ ├── Array_Nesting.java │ ├── As_Far_from_Land_as_Possible.java │ ├── Cheapest_Flights_Within_K_Stops.java │ ├── Checking_Existence_of_Edge_Length_Limited_Paths.java │ ├── Count_Subtrees_With_Max_Distance_Between_Cities.java │ ├── Course_Schedule.py │ ├── Course_Schedule_II.java │ ├── Course_Schedule_IV.java │ ├── Critical_Connections_in_a_Network.java │ ├── Detect_Cycles_in_2D_Grid.java │ ├── Evaluate_Division.java │ ├── Find_Closest_Node_to_Given_Two_Nodes.java │ ├── Find_Critical_and_Pseudo_Critical_Edges_in_Minimum_Spanning_Tree.java │ ├── Find_Eventual_Safe_States.java │ ├── Find_Latest_Group_of_Size_M.py │ ├── Flower_Planting_With_No_Adjacent.java │ ├── Graph_Connectivity_With_Threshold.java │ ├── Height_of_Binary_Tree_After_Subtree_Removal_Queries.java │ ├── Is_Graph_Bipartite.java │ ├── Kth_Ancestor_of_a_Tree_Node.java │ ├── Largest_Component_Size_by_Common_Factor.java │ ├── Lexicographically_Smallest_Equivalent_String.java │ ├── Longest_Consecutive_Sequence.java │ ├── Loud_and_Rich.java │ ├── Making_A_Large_Island.java │ ├── Maximal_Network_Rank.java │ ├── Maximum_Number_of_Achievable_Transfer_Requests.java │ ├── Min_Cost_to_Connect_All_Points.java │ ├── Minimize_Hamming_Distance_After_Swap_Operations.java │ ├── Minimize_Malware_Spread.java │ ├── Minimize_Malware_Spread_II.java │ ├── Minimum_Genetic_Mutation.java │ ├── Minimum_Height_Trees.java │ ├── Minimum_Number_of_Days_to_Disconnect_Island.java │ ├── Minimum_Number_of_Vertices_to_Reach_All_Nodes.java │ ├── Most_Stones_Removed_with_Same_Row_or_Column.java │ ├── Network_Delay_Time.java │ ├── Node_With_Highest_Edge_Score.java │ ├── Number_of_Closed_Islands.java │ ├── Number_of_Enclaves.java │ ├── Number_of_Good_Paths.java │ ├── Number_of_Operations_to_Make_Network_Connected.java │ ├── Number_of_Provinces.java │ ├── Number_of_Ways_to_Arrive_at_Destination.java │ ├── Pacific_Atlantic_Water_Flow.java │ ├── Path_with_Maximum_Gold.java │ ├── Path_with_Maximum_Probability.java │ ├── Reconstruct_Itinerary.java │ ├── Redundant_Connection.java │ ├── Remove_Max_Number_of_Edges_to_Keep_Graph_Fully_Traversable.java │ ├── Reorder_Routes_to_Make_All_Paths_Lead_to_the_City_Zero.java │ ├── Satisfiability_of_Equality_Equations.java │ ├── Shortest_Bridge.java │ ├── Shortest_Path_Visiting_All_Nodes.java │ ├── Shortest_Path_in_Binary_Matrix.java │ ├── Shortest_Path_to_Get_All_Keys.java │ ├── Shortest_Path_with_Alternating_Colors.java │ ├── Sliding_Puzzle.java │ ├── Smallest_Common_Region.java │ ├── Smallest_String_With_Swaps.java │ ├── Snakes_and_Ladders.java │ ├── Strange_Printer_II.py │ ├── Sum_of_Distances_in_Tree.java │ ├── Unique_Paths_III.java │ ├── Word_Ladder.java │ ├── Word_Ladder_II.java │ └── Word_Search.java ├── Greedy │ ├── Break_a_Palindrome.java │ ├── Can_Convert_String_in_K_Moves.java │ ├── Candy.java │ ├── Check_If_String_Is_Transformable_With_Substring_Sort_Operations.java │ ├── Construct_String_With_Repeat_Limit.java │ ├── Container_With_Most_Water.java │ ├── Course_Schedule_III.java │ ├── Delete_Columns_to_Make_Sorted_II.java │ ├── Divide_Array_in_Sets_of_K_Consecutive_Numbers.java │ ├── Earliest_Possible_Day_of_Full_Bloom.java │ ├── Find_Median_from_Data_Stream.java │ ├── Find_the_Minimum_Number_of_Fibonacci_Numbers_Whose_Sum_Is_K.java │ ├── Furthest_Building_You_Can_Reach.java │ ├── Insert_Interval.java │ ├── Jump_Game_II.java │ ├── K_Closest_Points_to_Origin.java │ ├── Kth_Smallest_Instructions.py │ ├── Largest_Values_From_Labels.java │ ├── Least_Number_of_Unique_Integers_after_K_Removals.java │ ├── Longest_Happy_String.java │ ├── Max_Chunks_To_Make_Sorted.java │ ├── Max_Chunks_To_Make_Sorted_II.java │ ├── Maximal_Score_After_Applying_K_Operations.java │ ├── Maximum_Area_of_a_Piece_of_Cake_After_Horizontal_and_Vertical_Cuts.java │ ├── Maximum_Bags_With_Full_Capacity_of_Rocks.java │ ├── Maximum_Number_of_Coins_You_Can_Get.java │ ├── Maximum_Number_of_Consecutive_Values_You_Can_Make.java │ ├── Maximum_Performance_of_a_Team.java │ ├── Maximum_Product_After_K_Increments.java │ ├── Maximum_Score_From_Removing_Stones.java │ ├── Merge_Intervals.java │ ├── Minimize_Deviation_in_Array.java │ ├── Minimize_Maximum_Pair_Sum_in_Array.java │ ├── Minimize_XOR.java │ ├── Minimum_Addition_to_Make_Integer_Beautiful.java │ ├── Minimum_Deletions_to_Make_Character_Frequencies_Unique.java │ ├── Minimum_Difference_Between_Largest_and_Smallest_Value_in_Three_Moves.java │ ├── Minimum_Domino_Rotations_For_Equal_Row.java │ ├── Minimum_Moves_to_Equal_Array_Elements_II.java │ ├── Minimum_Number_of_Arrows_to_Burst_Balloons.java │ ├── Minimum_Number_of_Increments_on_Subarrays_to_Form_a_Target_Array.java │ ├── Minimum_Number_of_Refueling_Stops.java │ ├── Minimum_Number_of_Taps_to_Open_to_Water_a_Garden.java │ ├── Minimum_Swaps_to_Arrange_a_Binary_Grid.java │ ├── Minimum_Time_to_Make_Rope_Colorful.java │ ├── Most_Profit_Assigning_Work.java │ ├── Next_Greater_Element_III.java │ ├── Non-overlapping_Intervals.py │ ├── Number_of_Orders_in_the_Backlog.java │ ├── Optimal_Partition_of_String.java │ ├── Orderly_Queue.java │ ├── Permutation_Sequence.java │ ├── Put_Boxes_Into_the_Warehouse_I.java │ ├── Put_Boxes_Into_the_Warehouse_II.java │ ├── Reaching_Points.java │ ├── Remove_Covered_Intervals.java │ ├── Shortest_Unsorted_Continuous_Subarray.java │ ├── Single_Threaded_CPU.java │ ├── Sliding_Window_Median.java │ ├── Smallest_Missing_Non_negative_Integer_After_Operations.java │ ├── Smallest_String_With_A_Given_Numeric_Value.java │ ├── Split_Array_into_Consecutive_Subsequences.java │ ├── Split_Two_Strings_to_Make_Palindrome.java │ ├── Stamping_The_Sequence.java │ ├── Stone_Game.java │ ├── Stone_Game_VI.java │ ├── Task_Scheduler.java │ ├── Total_Cost_to_Hire_K_Workers.java │ ├── Trapping_Rain_Water.java │ ├── Two_City_Scheduling.java │ └── Video_Stitiching.java ├── LinkedList │ └── Add_Two_Polynomials_Represented_as_Linked_Lists.java ├── MergeSort │ ├── Global_and_Local_Inversions.java │ ├── Reverse_Pairs.java │ └── Sort_List.java ├── Miscellaneous │ ├── 3Sum_Closest.java │ ├── 3Sum_With_Multiplicity.java │ ├── Add_Two_Numbers.java │ ├── Adding_Spaces_to_a_String.java │ ├── Additive_Number.java │ ├── Alert_Using_Same_Key-Card_Three_or_More_Times_in_a_One_Hour_Period.java │ ├── All_Nodes_Distance_K_in_Binary_Tree.java │ ├── All_O_one_Data_Structure.java │ ├── Apply_Bitwise_Operations_to_Make_Strings_Equal.java │ ├── Array_With_Elements_Not_Equal_to_Average_of_Neighbors.py │ ├── Average_Waiting_Time.java │ ├── Binary_String_With_Substrings_Representing_1_To_N.py │ ├── Bitwise_XOR_of_All_Pairings.java │ ├── Broken_Calculator.java │ ├── Bulls_and_Cows.java │ ├── Change_Minimum_Characters_to_Satisfy_One_of_Three_Conditions.java │ ├── Check_If_Array_Pairs_Are_Divisible_by_k.java │ ├── Check_If_a_String_Contains_All_Binary_Codes_of_Size_K.java │ ├── Check_If_a_String_Contains_All_Binary_Codes_of_Size_K.py │ ├── Check_Knight_Tour_Configuration.java │ ├── Check_if_Number_is_a_Sum_of_Powers_of_Three.java │ ├── Check_if_Word_Equals_Summation_of_Two_Words.py │ ├── Check_if_the_Sentence_Is_Pangram.py │ ├── Closest_Divisors.java │ ├── Combination_Sum_II.java │ ├── Combination_Sum_III.java │ ├── Construct_K_Palindrome_Strings.py │ ├── Contains_Duplicate.py │ ├── Coordinate_With_Maximum_Network_Quality.java │ ├── Copy_List_with_Random_Pointer.java │ ├── Count_Array_Pairs_Divisible_by_K.java │ ├── Count_Good_Meals.java │ ├── Count_Nice_Pairs_in_an_Array.py │ ├── Count_Number_of_Distinct_Integers_After_Reverse_Operations.java │ ├── Count_Number_of_Homogenous_Substrings.java │ ├── Count_Number_of_Teams.java │ ├── Count_Servers_that_Communicate.java │ ├── Count_Sorted_Vowel_Strings.java │ ├── Count_Substrings_That_Differ_by_One_Character.py │ ├── Count_Unguarded_Cells_in_the_Grid.java │ ├── Count_Unhappy_Friends.java │ ├── Count_Unique_Characters_of_All_Substrings_of_a_Given_String.java │ ├── Count_the_Hidden_Sequences.java │ ├── Count_the_Number_of_Consistent_Strings.py │ ├── Decrease_Elements_To_Make_Array_Zigzag.java │ ├── Delete_the_Middle_Node_of_a_Linked_List.java │ ├── Design_Authentication_Manager.java │ ├── Design_Circular_Deque.java │ ├── Design_Circular_Queue.java │ ├── Design_Front_Middle_Back_Queue.java │ ├── Design_HashMap.java │ ├── Destroy_Sequential_Targets.java │ ├── Destroying_Asteroids.java │ ├── Determine_if_String_Halves_Are_Alike.py │ ├── Determine_if_Two_Strings_Are_Close.py │ ├── Dinner_Plate_Stacks.java │ ├── Distribute_Candies.py │ ├── Eliminate_Maximum_Number_of_Monsters.java │ ├── Execution_of_All_Suffix_Instructions_Staying_in_a_Grid.java │ ├── Factorial_Trailing_Zeroes.java │ ├── Fancy_Sequence.py │ ├── Find_Duplicate_File_in_System.py │ ├── Find_Kth_Bit_in_Nth_Binary_String.py │ ├── Find_Players_With_Zero_or_One_Losses.java │ ├── Find_The_Duplicate_Number.java │ ├── Find_Three_Consecutive_Integers_That_Sum_to_a_Given_Number.java │ ├── Find_Valid_Matrix_Given_Row_and_Column_Sums.java │ ├── Find_XOR_Sum_of_All_Pairs_Bitwise_AND.java │ ├── Find_and_Replace_Pattern.java │ ├── Find_the_Longest_Substring_Containing_Vowels_in_Even_Counts.java │ ├── Find_the_Most_Competitive_Subsequence.java │ ├── Find_the_Winner_of_an_Array_Game.java │ ├── First_Unique_Character_in_a_String.py │ ├── Flatten_Nested_List_Iterator.java │ ├── Game_of_Life.java │ ├── Goal_Parser_Interpretation.py │ ├── Group_Anagrams.py │ ├── Group_the_People_Given_the_Group_Size_They_Belong_To.java │ ├── Groups_of_Special-Equivalent_Strings.java │ ├── HTML_Entity_Parser.java │ ├── Image_Overlap.java │ ├── Increasing_Subsequences.java │ ├── Incremental_Memory_Leak.py │ ├── Insert_Delete_GetRandom_O(1)_-_Duplicates_allowed.java │ ├── Insert_Delete_GetRandom_O1.java │ ├── Insertion_Sort_List.java │ ├── Integer_to_Roman.py │ ├── Intersection_of_Two_Linked_Lists.java │ ├── Island_Perimeter.java │ ├── Jump_Game.java │ ├── Jump_Game_III.java │ ├── K_diff_Pairs_in_an_Array.java │ ├── Keys_and_Rooms.java │ ├── LRU_Cache.java │ ├── Largest_Number.java │ ├── Last_Moment_Before_All_Ants_Fall_Out_of_a_Plank.java │ ├── Last_Stone_Weight.java │ ├── Length_of_the_Longest_Alphabetical_Continuous_Substring.java │ ├── Letter_Case_Permutation.java │ ├── Letter_Combinations_of_a_Phone_Number.java │ ├── Letter_Tile_Possibilities.java │ ├── Lexicographical_Numbers.java │ ├── Lexicographically_Smallest_String_After_Applying_Operations.py │ ├── Linked_List_Cycle.java │ ├── Linked_List_Cycle_II.java │ ├── Longest_Subarray_With_Maximum_Bitwise_AND.java │ ├── Longest_Subarray_of_1_s_After_Deleting_One_Element.java │ ├── Longest_Uploaded_Prefix.java │ ├── Majority_Element.java │ ├── Make_Number_of_Distinct_Characters_Equal.java │ ├── Making_File_Names_Unique.java │ ├── Max_Area_of_Island.java │ ├── Max_Increase_to_Keep_City_Skyline.java │ ├── Max_Number_of_K_Sum_Pairs.py │ ├── Max_Sum_of_Rectangle_No_Larger_Than_K.java │ ├── Max_Sum_of_a_Pair_With_Equal_Sum_of_Digits.java │ ├── Maximize_Distance_to_Closest_Person.java │ ├── Maximum_Element_After_Decreasing_and_Rearranging.py │ ├── Maximum_Ice_Cream_Bars.java │ ├── Maximum_Length_of_a_Concatenated_String_with_Unique_Characters.java │ ├── Maximum_Number_of_Balls_in_a_Box.py │ ├── Maximum_Population_Year.py │ ├── Maximum_Product_Difference_Between_Two_Pairs.py │ ├── Maximum_Product_of_Word_Lengths.java │ ├── Maximum_Profit_of_Operating_a_Centennial_Wheel.py │ ├── Maximum_Rows_Covered_by_Columns.java │ ├── Maximum_Score_Words_Formed_by_Letters.java │ ├── Maximum_Split_of_Positive_Even_Integers.java │ ├── Maximum_Subarray.java │ ├── Maximum_Sum_Circular_Subarray.java │ ├── Maximum_Sum_of_an_Hourglass.java │ ├── Maximum_XOR_for_Each_Query.java │ ├── Merge_In_Between_Linked_Lists.java │ ├── Merge_Nodes_in_Between_Zeros.java │ ├── Merge_k_Sorted_Lists.java │ ├── Middle_of_the_Linked_List.java │ ├── Minimum_Deletions_to_Make_Array_Divisible.java │ ├── Minimum_Distance_to_the_Target_Element.py │ ├── Minimum_Elements_to_Add_to_Form_a_Given_Sum.py │ ├── Minimum_Flips_to_Make_a_OR_b_Equal_to_c.java │ ├── Minimum_Lines_to_Represent_a_Line_Chart.py │ ├── Minimum_Number_of_Moves_to_Make_Palindrome.java │ ├── Minimum_Number_of_Operations_to_Reinitialize_a_Permutation.java │ ├── Minimum_Number_of_Steps_to_Make_Two_Strings_Anagram_II.py │ ├── Minimum_Number_of_Swaps_to_Make_the_Binary_String_Alternating.java │ ├── Minimum_Numbers_of_Function_Calls_to_Make_Target_Array.java │ ├── Minimum_Operations_to_Make_the_Array_Alternating.java │ ├── Minimum_Operations_to_Reduce_X_to_Zero.java │ ├── Minimum_Rounds_to_Complete_All_Tasks.PY │ ├── Minimum_Suffix_Flips.java │ ├── Missing_Number.py │ ├── Most_Popular_Video_Creator.java │ ├── N-Queens_II.java │ ├── N_Queens.java │ ├── Nearest_Exit_from_Entrance_in_Maze.java │ ├── Next_Permutation.java │ ├── Nim_Game.py │ ├── Non-decreasing_Array.py │ ├── Number_of_1_Bits.py │ ├── Number_of_Different_Subsequences_GCDs.py │ ├── Number_of_Islands.java │ ├── Number_of_Laser_Beams_in_a_Bank.java │ ├── Number_of_Nodes_in_the_Sub_Tree_With_the_Same_Label.java │ ├── Number_of_Students_Unable_to_Eat_Lunch.java │ ├── Number_of_Substrings_With_Only_1s.java │ ├── Number_of_Times_Binary_String_Is_Prefix_Aligned.py │ ├── Number_of_Valid_Words_for_Each_Puzzle.java │ ├── Number_of_Ways_Where_Square_of_Number_Is_Equal_to_Product_of_Two_Numbers.py │ ├── Number_of_Ways_to_Divide_a_Long_Corridor.java │ ├── Numbers_With_Same_Consecutive_Differences.java │ ├── Odd_Even_Linked_List.java │ ├── OrderedStream.java │ ├── Palindrome_Pairs.py │ ├── Pancake_Sorting.java │ ├── Partition_Array_According_to_Given_Pivot.java │ ├── Partition_List.java │ ├── Partitioning_Into_Minimum_Number_Of_Deci-Binary_Numbers.java │ ├── Perfect_Rectangle.java │ ├── Permutations.java │ ├── Permutations_II.java │ ├── Plus_One.java │ ├── Powerful_Integers.py │ ├── Print Words Vertically.py │ ├── Product_of_Array_Except_Self.py │ ├── Put_Marbles_in_Bags.java │ ├── Queens_That_Can_Attack_the_King.java │ ├── Queries_on_Number_of_Points_Inside_a_Circle.java │ ├── Query_Kth_Smallest_Trimmed_Number.py │ ├── Queue_Reconstruction_by_Height.java │ ├── RLE_Iterator.java │ ├── Rank_Teams_by_Votes.java │ ├── Reduce_Array_Size_to_The_Half.py │ ├── Remove_Duplicates_from_Sorted_List_II.java │ ├── Remove_Nth_Node_From_End_of_List.java │ ├── Remove_Palindromic_Subsequences.py │ ├── Reorder_List.java │ ├── Reordered_Power_of_2.java │ ├── Replace_All_Digits_with_Characters.py │ ├── Restore_IP_Addresses.java │ ├── Reverse_Integer.java │ ├── Reverse_Linked_List.java │ ├── Reverse_Linked_List_II.java │ ├── Reverse_Nodes_in_k_Group.java │ ├── Reverse_Words_in_a_String_III.py │ ├── Richest_Customer_Wealth.java │ ├── Robot_Bounded_In_Circle.py │ ├── Rotate_Array.java │ ├── Rotate_Image.java │ ├── Rotate_List.java │ ├── Rotating_the_Box.java │ ├── Rotting_Oranges.java │ ├── Search_a_2D_Matrix_II.java │ ├── Seat_Reservation_Manager.java │ ├── Second Largest_Digit_in_a_String.py │ ├── Sequential_Digits.java │ ├── Set_Matrix_Zeroes.java │ ├── Set_Mismatch.py │ ├── Shifting_Letters.java │ ├── Short_Encoding_of_Words.java │ ├── Simplified_Fractions.java │ ├── Single_Number_III.java │ ├── Smallest_Number_in_Infinite_Set.java │ ├── Smallest_Range_I.py │ ├── Sort_Colors.java │ ├── Sort_the_Jumbled_Numbers.java │ ├── Sort_the_Matrix_Diagonally.java │ ├── Sorting_the_Sentence.py │ ├── Spiral_Matrix.java │ ├── Spiral_Matrix_IV.java │ ├── Split_a_String_Into_the_Max_Number_of_Unique_Substrings.java │ ├── Splitting_a_String_Into_Descending_Consecutive_Values.py │ ├── Strictly_Palindromic_Number.py │ ├── Subsets.java │ ├── Substring_XOR_Queries.java │ ├── Sum_of_Absolute_Differences_in_a_Sorted_Array.py │ ├── Sum_of_Even_Numbers_After_Queries.java │ ├── Sum_of_Number_and_Its_Reverse.java │ ├── Sum_of_Square_Numbers.java │ ├── Surrounded_Regions.java │ ├── Swapping_Nodes_in_a_Linked_List.java │ ├── The_Number_of_Beautiful_Subsets.java │ ├── The_k-th_Lexicographical_String_of_All_Happy_Strings_of_Length_n.java │ ├── Throne_Inheritance.java │ ├── Top_K_Frequent_Elements.java │ ├── Top_K_Frequent_Elements.py │ ├── Top_K_Frequent_Words.java │ ├── Total_Hamming_Distance.java │ ├── Triples_with_Bitwise_AND_Equal_To_Zero.java │ ├── Two_Sum.java │ ├── UndergroundSystem.java │ ├── Valid_Square.java │ ├── Valid_Sudoku.py │ ├── Valid_Triangle_Number.java │ ├── Vowels_of_All_Substrings.java │ ├── Watering_Plants.java │ ├── Watering_Plants_II.java │ ├── Where_Will_the_Ball_Fall.java │ ├── Word_Subsets.java │ └── Words_Within_Two_Edits_of_Dictionary.py ├── MonoQueue │ ├── Jump_Game_VI.java │ ├── Max_Value_of_Equation.java │ ├── Maximum_Number_of_Robots_Within_Budget.java │ ├── Remove_K_Digits.java │ ├── Shortest_Subarray_with_Sum_at_Least_K.java │ └── Sliding_Window_Maximum.java ├── MonoStack │ ├── Daily_Temperatures.java │ ├── Largest_Rectangle_in_Histogram.java │ ├── Maximum_of_Minimum_Values_in_All_Subarrays.java │ ├── Next_Greater_Element_II.java │ ├── Online_Stock_Span.java │ ├── Remove_Duplicate_Letters.java │ ├── Subarray_With_Elements_Greater_Than_Varying_Threshold.java │ ├── Sum_of_Subarray_Minimums.java │ └── Sum_of_Subarray_Ranges.java ├── NumberTheory │ ├── Count_Primes.java │ ├── Number_of_Pairs_of_Interchangeable_Rectangles.java │ └── Number_of_Ways_to_Reorder_Array_to_Get_Same_BST.java ├── Prefix_Suffix │ ├── Binary_Subarrays_With_Sum.java │ ├── Car_Pooling.java │ ├── Contiguous_Array.py │ ├── Continuous_Subarray_Sum.java │ ├── Corporate_Flight_Bookings.java │ ├── Count_Triplets_That_Can_Form_Two_Arrays_of_Equal_XOR.java │ ├── Find_All_Good_Indices.java │ ├── Find_Longest_Awesome_Substring.java │ ├── Find_Two_Non-overlapping_Sub-arrays_Each_With_Target_Sum.java │ ├── Grid_Game.java │ ├── Intervals_Between_Identical_Elements.java │ ├── Longest_Subarray_of_1_s_After_Deleting_One_Element.java │ ├── Longest_Well_Performing_Interval.java │ ├── Make_Sum_Divisible_by_P.java │ ├── Matrix_Block_Sum.java │ ├── Maximum_Fruits_Harvested_After_at_Most_K_Steps.java │ ├── Maximum_Number_of_Non-Overlapping_Subarrays_With_Sum_Equals_Target.java │ ├── Maximum_Points_You_Can_Obtain_from_Cards.java │ ├── Maximum_Score_Of_Spliced_Array.java │ ├── Maximum_Side_Length_of_a_Square_with_Sum_Less_than_or_Equal_to_Threshold.java │ ├── Maximum_Sum_Obtained_of_Any_Permutation.java │ ├── Minimum_Average_Difference.java │ ├── Move_Pieces_to_Obtain_a_String.java │ ├── Number_of_Good_Ways_to_Split_a_String.java │ ├── Number_of_Sub_arrays_With_Odd_Sum.java │ ├── Number_of_Submatrices_That_Sum_to_Target.java │ ├── Number_of_Ways_to_Split_a_String.java │ ├── Partition_Array_into_Disjoint_Intervals.java │ ├── Product_of_the_Last_K_Numbers.java │ ├── Range_Sum_Query_2D_Immutable.java │ ├── Range_Sum_of_Sorted_Subarray_Sums.java │ ├── Removing_Minimum_Number_of_Magic_Beans.java │ ├── Subarray_Sum_Equals_K.java │ ├── Subarray_Sums_Divisible_by_K.java │ ├── Subsequence_With_the_Minimum_Score.java │ ├── Sum_of_Beauty_in_the_Array.java │ ├── Sum_of_Total_Strength_of_Wizards.py │ ├── The_Number_of_Weak_Characters_in_the_Game.java │ ├── Ways_to_Make_a_Fair_Array.java │ ├── Ways_to_Split_Array_Into_Three_Subarrays.java │ └── XOR_Queries_of_a_Subarray.java ├── QuickSelect │ └── Kth_Largest_Element_in_an_Array.java ├── Randomized Algorithms │ ├── Linked_List_Random_Node.java │ ├── Random_Pick_with_Weight.java │ └── Shuffle_an_Array.java ├── SegTree │ ├── Count_of_Smaller_Numbers_After_Self.java │ ├── Longest_Increasing_Subsequence_II.java │ ├── Next_Greater_Element.java │ ├── Online_Majority_Element_In_Subarray.java │ └── Range_Sum_Query_-_Mutable.java ├── Sliding_Window │ ├── Count_Number_of_Nice_Subarrays.java │ ├── Count_Subarrays_With_Fixed_Bounds.java │ ├── Count_the_Number_of_Good_Subarrays.java │ ├── Find_All_Anagrams_in_a_String.java │ ├── Fruit_Into_Baskets.java │ ├── Longest_Nice_Subarray.java │ ├── Longest_Repeating_Character_Replacement.java │ ├── Longest_Substring_Without_Repeating_Characters.java │ ├── Longest_Substring_with_At_Least_K_Repeating_Characters.java │ ├── Max_Consecutive_Ones_III.java │ ├── Maximize_the_Confusion_of_an_Exam.java │ ├── Maximum_Erasure_Value.java │ ├── Maximum_Number_of_Visible_Points.java │ ├── Maximum_Number_of_Vowels_in_a_Substring_of_Given_Length.java │ ├── Maximum_Sum_of_Distinct_Subarrays_With_Length_K.java │ ├── Minimum_Size_Subarray_Sum.java │ ├── Minimum_Swaps_to_Group_All_1s_Together_II.java │ ├── Minimum_Window_Substring.java │ ├── Number_of_Sub-arrays_of_Size_K_and_Average_Greater_than_or_Equal_to_Threshold.java │ ├── Number_of_Substrings_Containing_All_Three_Characters.java │ ├── Permutation_in_String.java │ ├── Repeated_DNA_Sequences.java │ └── Subarrays_with_K_Different_Integers.java ├── Stack │ ├── Asteroid_Collision.java │ ├── Decode_String.java │ ├── Design_a_Stack_With_Increment_Operation.java │ ├── Evaluate_Reverse_Polish_Notation.java │ ├── Evaluate_the_Bracket_Pairs_of_a_String.java │ ├── Longest_Valid_Parentheses.java │ ├── Maximum_Frequency_Stack.java │ ├── Minimum_Add_to_Make_Parentheses_Valid.py │ ├── Minimum_Insertions_to_Balance_a_Parentheses_String.py │ ├── Minimum_Remove_to_Make_Valid_Parentheses.java │ ├── Parsing_A_Boolean_Expression.java │ ├── Remove_All_Adjacent_Duplicates_in_String_II.java │ ├── Reverse_Substrings_Between_Each_Pair_of_Parentheses.java │ ├── Score_of_Parentheses.java │ ├── Simplify_Path.java │ └── Validate_Stack_Sequences.java ├── StringAlgo │ ├── Find_the_Index_of_the_First_Occurrence_in_a_String.java │ ├── Longest_Happy_Prefix.java │ └── Shortest_Palindrome.java ├── StringHashing │ ├── Distinct_Echo_Substrings.java │ ├── Longest_Chunked_Palindrome_Decomposition.java │ ├── Longest_Duplicate_Substring.java │ └── Maximum_Length_of_Repeated_Subarray.java ├── SweepLine │ ├── Divide_Intervals_Into_Minimum_Number_of_Groups.java │ ├── Find_Servers_That_Handled_Most_Number_of_Requests.java │ ├── Maximum_Number_of_Events_That_Can_Be_Attended.java │ ├── Meeting_Rooms_II.java │ ├── Minimum_Interval_to_Include_Each_Query.java │ ├── Minimum_Number_of_Arrows_to_Burst_Balloons.java │ └── The_Skyline_Problem.java ├── Tree │ ├── Add_One_Row_to_Tree.java │ ├── All_Elements_in_Two_Binary_Search_Trees.java │ ├── Average_of_Levels_in_Binary_Tree.java │ ├── Balance_a_Binary_Search_Tree.java │ ├── Binary_Search_Tree_to_Greater_Sum_Tree.java │ ├── Binary_Tree_Inorder_Traversal.java │ ├── Binary_Tree_Level_Order_Traversal_II.java │ ├── Binary_Tree_Maximum_Path_Sum.java │ ├── Binary_Tree_Pruning.java │ ├── Binary_Tree_Right_Side_View.java │ ├── Binary_Tree_Zigzag_Level_Order_Traversal.java │ ├── Boundary_of_Binary_Tree.java │ ├── Check_Completeness_of_a_Binary_Tree.java │ ├── Complete_Binary_Tree_Inserter.java │ ├── Construct_Binary_Search_Tree_from_Preorder_Traversal.java │ ├── Construct_Binary_Tree_from_Inorder_and_Postorder_Traversal.java │ ├── Construct_Binary_Tree_from_Preorder_and_Inorder_Traversal.java │ ├── Convert_BST_to_Greater_Tree.java │ ├── Convert_Sorted_List_to_Binary_Search_Tree.java │ ├── Count_Complete_Tree_Nodes.java │ ├── Count_Good_Nodes_in_Binary_Tree.java │ ├── Count_Nodes_Equal_to_Average_of_Subtree.java │ ├── Deepest_Leaves_Sum.java │ ├── Delete_Leaves_With_a_Given_Value.java │ ├── Delete_Node_in_a_BST.java │ ├── Delete_Nodes_And_Return_Forest.java │ ├── Distribute_Coins_in_Binary_Tree.java │ ├── Even_Odd_Tree.java │ ├── Find_Bottom_Left_Tree_Value.java │ ├── Find_Duplicate_Subtrees.java │ ├── Find_Largest_Value_in_Each_Tree_Row.java │ ├── Find_a_Corresponding_Node_of_a_Binary_Tree_in_a_Clone_of_That_Tree.java │ ├── Flatten_Binary_Tree_to_Linked_List.java │ ├── House_Robber_III.java │ ├── Increasing_Order_Search_Tree.java │ ├── Insert_into_a_Binary_Search_Tree.java │ ├── Invert_Binary_Tree.java │ ├── Kth_Smallest_Element_in_a_BST.java │ ├── Largest_BST_Subtree.java │ ├── Longest_Path_With_Different_Adjacent_Characters.java │ ├── Longest_Univalue_Path.java │ ├── Longest_ZigZag_Path_in_a_Binary_Tree.java │ ├── Lowest_Common_Ancestor_of_Deepest_Leaves.java │ ├── Lowest_Common_Ancestor_of_a_Binary_Tree.java │ ├── Maximum_Difference_Between_Node_and_Ancestor.java │ ├── Maximum_Level_Sum_of_a_Binary_Tree.java │ ├── Maximum_Product_of_Splitted_Binary_Tree.java │ ├── Maximum_Sum_BST_in_Binary_Tree.java │ ├── Maximum_Width_of_Binary_Tree.java │ ├── Minimum_Time_to_Collect_All_Apples_in_a_Tree.java │ ├── Number_of_Good_Leaf_Nodes_Pairs.java │ ├── Path_Sum.java │ ├── Path_Sum_II.java │ ├── Path_Sum_III.java │ ├── Pseudo_Palindromic_Paths_in_a_Binary_Tree.java │ ├── Range_Sum_of_BST.java │ ├── Recover_Binary_Search_Tree.java │ ├── Reverse_Odd_Levels_of_Binary_Tree.java │ ├── Same_Tree.java │ ├── Search_in_a_Binary_Search_Tree.java │ ├── Serialize_and_Deserialize_Binary_Tree.java │ ├── Sum_Root_to_Leaf_Numbers.java │ ├── Sum_of_Nodes_with_Even_Valued_Grandparent.java │ ├── Symmetric_Tree.java │ ├── Trim_a_Binary_Search_Tree.java │ ├── Unique_Binary_Search_Trees_II.java │ ├── Validate_Binary_Search_Tree.java │ └── Vertical_Order_Traversal_of_a_Binary_Tree.java ├── Trie │ ├── Count_Words_Obtained_After_Adding_a_Letter.java │ ├── Design_Add_and_Search_Words_Data_Structure.java │ ├── Implement_Magic_Dictionary.java │ ├── Implement_Trie.java │ ├── Implement_Trie_II.java │ ├── Longest_Word_With_All_Prefixes.java │ ├── Longest_Word_in_Dictionary.java │ ├── Map_Sum_Pairs.java │ ├── Prefix_and_Suffix_Search.java │ ├── Remove_Sub-Folders_from_the_Filesystem.java │ ├── Replace_Words.java │ ├── Search_Suggestions_System.java │ ├── Sum_of_Prefix_Scores_of_Strings.java │ └── Word_Search_II.java └── TwoPointers │ ├── 3Sum.java │ ├── 4Sum.java │ ├── Bag_of_Tokens.java │ ├── Boats_to_Save_People.java │ ├── Construct_Smallest_Number_From_DI_String.java │ ├── Find_Original_Array_From_Doubled_Array.java │ ├── Gas_Station.java │ ├── Longest_Word_in_Dictionary_through_Deleting.java │ ├── Maximum_Score_of_a_Good_Subarray.java │ ├── Meeting_Scheduler.java │ ├── Number_of_Subsequences_That_Satisfy_the_Given_Sum_Condition.java │ ├── Partition_Labels.java │ ├── Push_Dominoes.java │ └── Shortest_Subarray_to_be_Removed_to_Make_Array_Sorted.java ├── Main.java ├── Platforms ├── Atcoder │ ├── Atcoder DP │ │ ├── A.java │ │ ├── B.java │ │ ├── C.java │ │ ├── D.java │ │ ├── F.java │ │ ├── H.java │ │ ├── K.java │ │ ├── S.java │ │ ├── input.txt │ │ ├── javarun.py │ │ └── output.txt │ └── Contests │ │ ├── abc209 │ │ ├── A.java │ │ └── B.java │ │ ├── abc210 │ │ ├── A.py │ │ ├── B.py │ │ └── C.py │ │ ├── abc211 │ │ ├── A.py │ │ ├── B.py │ │ ├── C.py │ │ └── D.java │ │ ├── abc214 │ │ ├── A.py │ │ ├── B.py │ │ └── C.py │ │ └── abc222 │ │ └── D.java ├── CSES │ ├── Dynamic Programming │ │ ├── Classes │ │ │ ├── Coin_Change.class │ │ │ ├── Dice_Combinations.class │ │ │ ├── Main$FastReader.class │ │ │ ├── Main$pair.class │ │ │ └── Main.class │ │ ├── Main.java │ │ ├── input.txt │ │ ├── javarun.py │ │ └── output.txt │ └── Graphs Algorithms │ │ ├── Classes │ │ ├── Main$FastReader.class │ │ ├── Main$MinIndexedDHeap.class │ │ ├── Main$pair.class │ │ └── Main.class │ │ ├── Main.java │ │ ├── README.md │ │ ├── input.txt │ │ ├── javarun.py │ │ ├── output.txt │ │ └── prog.py ├── Codechef │ ├── .gitkeep │ ├── ADADISH.py │ ├── ANDOR.py │ ├── ANITGUY2.java │ ├── ASP.java │ ├── BINSUBS.py │ ├── CHEFEZQ.py │ ├── CVDRUN.py │ ├── Chef_and_IPC_Certificates.py │ ├── DECREM.py │ ├── DREDIV.py │ ├── EVENPSUM.py │ ├── Flip_the_String.py │ ├── HXOR.java │ ├── Java-Classes │ │ ├── ANITGUY2$FastReader.class │ │ ├── ANITGUY2$pair.class │ │ ├── ASP$FastReader.class │ │ ├── ASP$pair.class │ │ ├── ASP.class │ │ ├── POLYREL$FastReader.class │ │ ├── POLYREL.class │ │ ├── Template$FastReader.class │ │ └── Template.class │ ├── POLYREL.java │ ├── POSAND.py │ ├── POSPREFS.py │ ├── RESTORE.py │ ├── SPLITIT.py │ ├── STROPERS.java │ ├── SWAP10HG.py │ ├── THREE.py │ ├── UWCOI21A.py │ ├── UWCOI21B.py │ ├── UWCOI21C.py │ ├── VACCINE1.py │ ├── VACCINE2.py │ ├── input.txt │ ├── javarun.py │ └── output.txt ├── Codeforces │ ├── .gitkeep │ ├── Add_Candies.java │ ├── Array_Rearrangment.java │ ├── Avoid_Trygub.py │ ├── Balls_of_Steel.java │ ├── Boring_Apartments.java │ ├── Buy_the_String.py │ ├── Challenging_Cliffs.py │ ├── Cut_Ribbon.java │ ├── Do_Not_Be_Distracted.py │ ├── Dominant_Piranha.py │ ├── Domino_piling.py │ ├── Eshag_Loves_Big_Arrays.java │ ├── Find_The_Array.py │ ├── Graph_Without_Long_Directed_Paths.java │ ├── Ilya_And_Queries.java │ ├── Jumps.java │ ├── Karen_and_Coffee.java │ ├── Kefa_and_First_Steps.java │ ├── Mahmoud_and_Ehab_and_the_bipartiteness.java │ ├── Maximum_GCD.py │ ├── Minimal_Diameter_Forest.java │ ├── Mocha_and_Diana_Easy.java │ ├── Non_Substring_Subsequence.py │ ├── Numbers_Box.java │ ├── Ordinary_Numbers.py │ ├── Pleasant Pairs.py │ ├── Same Differences.py │ ├── Sequence_Transformation.py │ ├── Shifting Stacks.py │ ├── Soldier_and_Bananas.py │ ├── Spanning_Tree_with_Maximum_Degree.java │ ├── Special_Permutation.java │ ├── Strange_Functions.py │ ├── Subtract_or_Divide.py │ ├── Theatre_Square.py │ ├── Unique_Bid_Auction.py │ ├── XORwice.java │ ├── Yet_Another_Bookshelf.java │ ├── input.txt │ ├── javarun.py │ └── output.txt ├── EXTRA │ ├── Coursera-Algorithms-On-Graphs │ │ ├── week1_graph_decomposition1 │ │ │ ├── 1_finding_exit_from_maze │ │ │ │ ├── Graph.class │ │ │ │ ├── Pair.class │ │ │ │ ├── Reachability.class │ │ │ │ ├── Reachability.java │ │ │ │ ├── reachability.cpp │ │ │ │ └── reachability.py │ │ │ ├── 2_adding_exits_to_maze │ │ │ │ ├── ConnectedComponents.class │ │ │ │ ├── ConnectedComponents.java │ │ │ │ ├── connected_components.cpp │ │ │ │ └── connected_components.py │ │ │ ├── debug.log │ │ │ └── week1_graph_decomposition1.pdf │ │ ├── week2_graph_decomposition2 │ │ │ ├── 1_cs_curriculum │ │ │ │ ├── Acyclicity.class │ │ │ │ ├── Acyclicity.java │ │ │ │ ├── acyclicity.cpp │ │ │ │ └── acyclicity.py │ │ │ ├── 2_order_of_courses │ │ │ │ ├── Toposort$FastReader.class │ │ │ │ ├── Toposort.class │ │ │ │ ├── Toposort.java │ │ │ │ ├── toposort.cpp │ │ │ │ └── toposort.py │ │ │ ├── 3_intersection_reachability │ │ │ │ ├── StronglyConnected$FastReader.class │ │ │ │ ├── StronglyConnected.class │ │ │ │ ├── StronglyConnected.java │ │ │ │ ├── strongly_connected.cpp │ │ │ │ └── strongly_connected.py │ │ │ ├── debug.log │ │ │ └── week2_graph_decomposition2.pdf │ │ ├── week3_paths_in_graphs1 │ │ │ ├── 1_flight_segments │ │ │ │ ├── BFS$FastReader.class │ │ │ │ ├── BFS.class │ │ │ │ ├── BFS.java │ │ │ │ ├── bfs.cpp │ │ │ │ └── bfs.py │ │ │ ├── 2_bipartite │ │ │ │ ├── Bipartite$FastReader.class │ │ │ │ ├── Bipartite.class │ │ │ │ ├── Bipartite.java │ │ │ │ ├── bipartite.cpp │ │ │ │ └── bipartite.py │ │ │ └── week3_paths_in_graphs1.pdf │ │ ├── week4_paths_in_graphs2 │ │ │ ├── 1_minimum_flight_cost │ │ │ │ ├── Dijkstra$pair.class │ │ │ │ ├── Dijkstra.class │ │ │ │ ├── Dijkstra.java │ │ │ │ ├── dijkstra.cpp │ │ │ │ └── dijkstra.py │ │ │ ├── 2_detecting_anomalies │ │ │ │ ├── NegativeCycle$pair.class │ │ │ │ ├── NegativeCycle.class │ │ │ │ ├── NegativeCycle.java │ │ │ │ ├── negative_cycle.cpp │ │ │ │ └── negative_cycle.py │ │ │ ├── 3_exchanging_money │ │ │ │ ├── ShortestPaths$pair.class │ │ │ │ ├── ShortestPaths.class │ │ │ │ ├── ShortestPaths.java │ │ │ │ ├── shortest_paths.cpp │ │ │ │ └── shortest_paths.py │ │ │ ├── debug.log │ │ │ └── week4_paths_in_graphs2.pdf │ │ └── week5_spanning_trees │ │ │ ├── 1_connecting_points │ │ │ ├── ConnectingPoints.java │ │ │ ├── connecting_points.cpp │ │ │ └── connecting_points.py │ │ │ ├── 2_clustering │ │ │ ├── Clustering.java │ │ │ ├── clustering.cpp │ │ │ └── clustering.py │ │ │ └── week5_spanning_trees.pdf │ ├── Google Kickstart │ │ └── Round A │ │ │ ├── K-Goodness String │ │ │ └── Solution.py │ │ │ └── L Shaped Plots │ │ │ └── Solution.java │ ├── Largest-Independent-Set-Problem │ │ ├── Question.md │ │ └── Solution.md │ ├── Maximum-sum-increasing-subsequence │ │ ├── Maximum_sum_increasing_subsequence.java │ │ └── README.md │ ├── Maximum-sum-of-Non-adjacent-nodes │ │ ├── Question.md │ │ └── Solution.md │ ├── Minimize-the-max-diff-between-heights │ │ └── README.md │ ├── Minimum Difference Subsets! │ │ ├── Minimum_Difference_Subsets.java │ │ └── README.md │ └── String-Subsequence │ │ ├── Question.md │ │ └── Solution.md ├── GFG │ ├── Count pairs with given sum.md │ ├── Inversion Count.md │ ├── Longest substring to form a Palindrome.md │ ├── Matrix Chain Multiplication.md │ └── N meetings in one room.md └── Hackerearth │ ├── .gitkeep │ ├── Input_expectations.java │ ├── Java-Classes │ ├── Monk_And_Champions_League$FastReader.class │ ├── Monk_And_Champions_League$pair.class │ ├── Monk_And_Champions_League.class │ ├── Monk_and_Multiplication$FastReader.class │ ├── Monk_and_Multiplication$pair.class │ └── Monk_and_Multiplication.class │ ├── Minimum_and_maximum_differences.java │ ├── Minimum_indexes.java │ ├── Monk_And_Champions_League.java │ ├── Monk_and_Multiplication.java │ ├── cpprun.py │ ├── input.txt │ ├── javarun.py │ └── output.txt ├── README.md ├── Template.py ├── autocommit.py └── file_name_maker.py /.gitignore: -------------------------------------------------------------------------------- 1 | autocommit_manual.py 2 | autocommit.py 3 | FINAL450.xlsx 4 | .venv -------------------------------------------------------------------------------- /.idea/.gitignore: -------------------------------------------------------------------------------- 1 | # Default ignored files 2 | /shelf/ 3 | /workspace.xml 4 | # Editor-based HTTP Client requests 5 | /httpRequests/ 6 | # Datasource local storage ignored files 7 | /dataSources/ 8 | /dataSources.local.xml 9 | -------------------------------------------------------------------------------- /.idea/Competitive-Practice.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "files.associations": { 3 | "chrono": "cpp" 4 | }, 5 | "python.formatting.provider": "autopep8" 6 | } -------------------------------------------------------------------------------- /CP-Prac.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /Contest/Classes/CountOddSegTree.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RohanTrix/Competitive-Practice/b14aae7dbd778d8cdf743c77a1f8d25b3048937e/Contest/Classes/CountOddSegTree.class -------------------------------------------------------------------------------- /Contest/Classes/Main$FastReader.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RohanTrix/Competitive-Practice/b14aae7dbd778d8cdf743c77a1f8d25b3048937e/Contest/Classes/Main$FastReader.class -------------------------------------------------------------------------------- /Contest/Classes/Main$pair.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RohanTrix/Competitive-Practice/b14aae7dbd778d8cdf743c77a1f8d25b3048937e/Contest/Classes/Main$pair.class -------------------------------------------------------------------------------- /Contest/Classes/Main.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RohanTrix/Competitive-Practice/b14aae7dbd778d8cdf743c77a1f8d25b3048937e/Contest/Classes/Main.class -------------------------------------------------------------------------------- /Contest/Classes/MaxSegTree.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RohanTrix/Competitive-Practice/b14aae7dbd778d8cdf743c77a1f8d25b3048937e/Contest/Classes/MaxSegTree.class -------------------------------------------------------------------------------- /Contest/Classes/SegTree.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RohanTrix/Competitive-Practice/b14aae7dbd778d8cdf743c77a1f8d25b3048937e/Contest/Classes/SegTree.class -------------------------------------------------------------------------------- /Contest/Classes/SumSegTree.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RohanTrix/Competitive-Practice/b14aae7dbd778d8cdf743c77a1f8d25b3048937e/Contest/Classes/SumSegTree.class -------------------------------------------------------------------------------- /Contest/file.py: -------------------------------------------------------------------------------- 1 | # list(map(int,input().split())) 2 | 3 | import math 4 | from collections import defaultdict, Counter 5 | import sys 6 | 7 | 8 | # sys.stdin = open('input.txt', 'r') 9 | # sys.stdout = open('output.txt', 'w+') 10 | 11 | # for t in range(int(input())): 12 | 13 | n = int(input()) 14 | l = list(map(int, int)) 15 | -------------------------------------------------------------------------------- /Contest/input.txt: -------------------------------------------------------------------------------- 1 | 4 2 | 10 8 3 1 3 | -------------------------------------------------------------------------------- /Contest/javarun.py: -------------------------------------------------------------------------------- 1 | import os 2 | import time 3 | prog_name = "Main" 4 | JAVAC_PATH = "D:\\Programs\\Java\\jdk-17.0.5\\bin\\javac.exe" 5 | JAVA_PATH = "D:\\Programs\\Java\\jdk-17.0.5\\bin\\java.exe" 6 | os.system(f'{JAVAC_PATH} -d Classes {prog_name}.java') 7 | t1 = time.time() 8 | os.system(f'{JAVA_PATH} -cp Classes -Xss30m {prog_name} local') 9 | t2 = time.time() 10 | print("\n\nTIME TAKEN: ",t2-t1) 11 | -------------------------------------------------------------------------------- /Contest/output.txt: -------------------------------------------------------------------------------- 1 | 4 2 | -------------------------------------------------------------------------------- /DOCS/Kartik's CF Specialist Sheet.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RohanTrix/Competitive-Practice/b14aae7dbd778d8cdf743c77a1f8d25b3048937e/DOCS/Kartik's CF Specialist Sheet.docx -------------------------------------------------------------------------------- /DOCS/Leetcode_FULL.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RohanTrix/Competitive-Practice/b14aae7dbd778d8cdf743c77a1f8d25b3048937e/DOCS/Leetcode_FULL.xlsx -------------------------------------------------------------------------------- /Leetcode/Binary_Search/Binary_Search.java: -------------------------------------------------------------------------------- 1 | package Leetcode.Binary_Search; 2 | 3 | 4 | class Solution { 5 | public int search(int[] nums, int target) { 6 | int n = nums.length; 7 | int left = 0, right = n - 1 ,mid; 8 | while(left<=right) 9 | { 10 | mid = left + (right - left)/2; 11 | if(nums[mid] == target) 12 | return mid; 13 | else if( target > nums[mid] ) 14 | left = mid+1; 15 | else 16 | right = mid-1; 17 | } 18 | return -1; 19 | } 20 | } -------------------------------------------------------------------------------- /Leetcode/Binary_Search/Find_K_Closest_Elements.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | 3 | # REFER EXPLANATION : https://youtu.be/t4gPEqlaNjc 4 | # After above done, REFER : https://leetcode.com/problems/find-k-closest-elements/discuss/106426/JavaC%2B%2BPython-Binary-Search-O(log(N-K)-%2B-K) 5 | 6 | def findClosestElements(self, arr: List[int], k: int, x: int) -> List[int]: 7 | left = 0 8 | right = len(arr) - k 9 | 10 | while(left < right): 11 | mid = left + (right - left) //2 12 | 13 | if x- arr[mid] > arr[mid + k] - x: 14 | left = mid + 1 15 | else: 16 | right = mid; 17 | return arr[left:left+k] -------------------------------------------------------------------------------- /Leetcode/Binary_Search/Find_Minimum_in_Rotated_Sorted_Array.java: -------------------------------------------------------------------------------- 1 | public class Find_Minimum_in_Rotated_Sorted_Array 2 | { 3 | public int findMin(int[] nums) { 4 | int l = 0, r = nums.length-1; 5 | int bestAns = nums[0]; 6 | while(l<=r) 7 | { 8 | int mid = l+(r-l)/2; 9 | if(nums[mid] nums[mid-1]) 11 | { 12 | ind = mid; 13 | l = mid + 1; 14 | } 15 | else r = mid - 1; 16 | } 17 | return ind; 18 | } 19 | } -------------------------------------------------------------------------------- /Leetcode/Binary_Search/First_Bad_Version.md: -------------------------------------------------------------------------------- 1 | /* The isBadVersion API is defined in the parent class VersionControl. 2 | boolean isBadVersion(int version); */ 3 | ```java 4 | public class First_Bad_Version extends VersionControl { 5 | public int firstBadVersion(int n) { 6 | 7 | int l = 1, r = n; 8 | int ans = 1; 9 | while(l<=r) 10 | { 11 | int mid = l+(r-l)/2; 12 | if(isBadVersion(mid)) 13 | 14 | { 15 | ans = mid; 16 | r = mid-1; 17 | } 18 | else 19 | l = mid+1; 20 | 21 | } 22 | return ans; 23 | 24 | } 25 | } 26 | 27 | ``` -------------------------------------------------------------------------------- /Leetcode/CyclicSort/First_Missing_Positive.java: -------------------------------------------------------------------------------- 1 | public class First_Missing_Positive { 2 | public int firstMissingPositive(int[] nums) { 3 | 4 | // REFER : https://youtu.be/Whhpvk2k5qk 5 | int n = nums.length; 6 | for(int i = 0; i0 && nums[i]<=n && nums[i]!= nums[nums[i] - 1]) 9 | { 10 | int tmp = nums[i]; 11 | nums[i] = nums[tmp - 1]; 12 | nums[tmp - 1] = tmp; 13 | } 14 | 15 | //System.out.println(Arrays.toString(nums)); 16 | } 17 | for(int i = 0; i int: 3 | dp = [] 4 | if n==1: return 1 5 | dp.append(1) 6 | dp.append(1) 7 | for i in range(2,n+1): 8 | dp.append(dp[i-1]+ dp[i-2]) 9 | return dp[n] -------------------------------------------------------------------------------- /Leetcode/DP/Coin_Change_2.java: -------------------------------------------------------------------------------- 1 | package Leetcode.DP; 2 | import java.util.*; 3 | public class Coin_Change_2 { 4 | public int change(int amount, int[] coins) { 5 | 6 | 7 | int ways[] = new int[amount+1]; 8 | Arrays.fill(ways, 0); 9 | ways[0] = 1; 10 | 11 | for(int currCoin : coins) 12 | { 13 | for(int currAmt=1; currAmt<=amount ;currAmt++) 14 | { 15 | if( currCoin <= currAmt) 16 | { 17 | ways[currAmt] += ways[currAmt- currCoin]; 18 | } 19 | } 20 | } 21 | //System.out.println(Arrays.toString(ways)); 22 | return ways[amount]; 23 | } 24 | } -------------------------------------------------------------------------------- /Leetcode/DP/Dungeon_Game.java: -------------------------------------------------------------------------------- 1 | package Leetcode.DP; 2 | 3 | // Check Youtube for explanation, Tricky Problem 4 | 5 | public class Dungeon_Game { 6 | int dp[][]; 7 | int m, n; 8 | // dp[i][j] = Min Health req top reach from (i,j) to end 9 | public int calculateMinimumHP(int[][] dun) { 10 | m = dun.length; n = dun[0].length; 11 | dp = new int[m+1][n+1]; 12 | for(int a[]: dp) Arrays.fill(a, Integer.MAX_VALUE); 13 | for(int i = m-1;i>=0; i--) 14 | { 15 | for(int j = n-1; j>=0; j--) 16 | { 17 | if(i == m-1 && j == n-1) dp[i][j] = Math.max(1, 1-dun[i][j]); 18 | else dp[i][j] = Math.max(1,Math.min(dp[i+1][j],dp[i][j+1]) - dun[i][j]); 19 | } 20 | } 21 | return dp[0][0]; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Leetcode/DP/House_Robber.java: -------------------------------------------------------------------------------- 1 | public class House_Robber { 2 | public int rob(int[] nums) { 3 | 4 | int n = nums.length; 5 | if(n==0) 6 | return 0; 7 | 8 | else if(n==1) 9 | return nums[0]; 10 | 11 | else if( n==2) 12 | return Math.max(nums[0],nums[1]); 13 | 14 | else 15 | { 16 | int maxSum[] = new int[n]; 17 | maxSum[0] = nums[0]; 18 | maxSum[1] = Math.max(nums[0],nums[1]); 19 | for(int i=2;i dp = new HashMap<>(); 5 | public int moves(long i) 6 | { 7 | if(i==1)return 0; 8 | if(dp.containsKey(i)) return dp.get(i); 9 | if(i%2==0) 10 | dp.put(i, moves(i/2)+1); 11 | else 12 | dp.put(i, Math.min(moves(i+1), moves(i-1))+1); 13 | return dp.get(i); 14 | } 15 | public int integerReplacement(int n) { 16 | 17 | return moves((long)n); 18 | } 19 | } -------------------------------------------------------------------------------- /Leetcode/DP/Jump_Game_II.java: -------------------------------------------------------------------------------- 1 | package Leetcode.DP; 2 | 3 | public class Jump_Game_II { 4 | public int jump(int[] nums) { 5 | int n = nums.length; 6 | int jumps = 1; 7 | 8 | if( n==1 || nums[0]==0) 9 | { 10 | return 0; 11 | } 12 | int farthest = nums[0]; 13 | int curr_end = nums[0]; 14 | 15 | for(int i = 1; i=n || y<0 || y>=n) return 0; 10 | if(k == 0) return 1.0; 11 | if(dp[x][y][k] !=null) return dp[x][y][k]; 12 | double cnt = 0; 13 | for(int i = 0; i<8; i++) 14 | cnt+=favways(x+dr[i], y+dc[i], k-1)/8.0; 15 | return dp[x][y][k] = cnt; 16 | } 17 | public double knightProbability(int N, int k, int row, int column) { 18 | n = N; 19 | dp = new Double[N+1][N+1][k+1]; 20 | return favways(row, column, k); 21 | } 22 | } -------------------------------------------------------------------------------- /Leetcode/DP/Longest_Arithmetic_Subsequence.java: -------------------------------------------------------------------------------- 1 | public class Longest_Arithmetic_Subsequence 2 | { 3 | // IDEA: LIS type O(n2) dp with DP STATE : dp[i][d] = Longest subseq ending at i with common diff = d 4 | // Since we can have a min common diff of -500... 5 | public int longestArithSeqLength(int[] nums) { 6 | int n = nums.length; 7 | int dp[][] = new int[n][1000+5]; 8 | int maxi = 1; 9 | for(int a[] : dp) Arrays.fill(a, 1); 10 | for(int i = 0; i map = new HashMap<>(); 4 | int maxi = -1; 5 | for(int num : arr) 6 | { 7 | int newLen = 1 + map.getOrDefault(num - difference, 0); 8 | maxi = Math.max(maxi, newLen); 9 | map.put(num, newLen); 10 | } 11 | return maxi; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Leetcode/DP/Maximum_Length_of_Pair_Chain.java: -------------------------------------------------------------------------------- 1 | package Leetcode.DP; 2 | 3 | import java.util.stream.*; 4 | public class Maximum_Length_of_Pair_Chain { 5 | public int findLongestChain(int[][] pairs) { 6 | Arrays.sort(pairs, (a,b) -> Integer.compare(a[1], b[1])); 7 | int len = pairs.length; 8 | int dp[] = new int[len]; 9 | Arrays.fill(dp,1); 10 | for(int i = 1; i pairs[j][1]) 15 | { 16 | dp[i] = Math.max(dp[i], dp[j]+1); 17 | } 18 | } 19 | } 20 | return IntStream.of(dp).max().getAsInt(); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Leetcode/DP/Maximum_Product_Subarray.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | 3 | def maxProduct(self, nums: List[int]) -> int: 4 | max_prod, min_prod, ans = nums[0], nums[0], nums[0] 5 | for i in range(1, len(nums)): 6 | x = max(nums[i], max_prod*nums[i], min_prod*nums[i]) 7 | y = min(nums[i], max_prod*nums[i], min_prod*nums[i]) 8 | max_prod, min_prod = x, y 9 | ans = max(max_prod, ans) 10 | return ans -------------------------------------------------------------------------------- /Leetcode/DP/Maximum_Score_from_Performing_Multiplication_Operations.py: -------------------------------------------------------------------------------- 1 | from functools import lru_cache 2 | 3 | class Solution: 4 | def maximumScore(self, nums: List[int], mul: List[int]) -> int: 5 | 6 | @lru_cache(maxsize = None) 7 | def maxScore(i,m): 8 | j = len(nums)-1 + (i - m) 9 | if m==len(mul): return 0 10 | 11 | a = nums[i]*mul[m] + maxScore(i+1,m+1) 12 | b = nums[j]*mul[m] + maxScore(i,m+1) 13 | return max(a,b) 14 | 15 | return maxScore(0,0); -------------------------------------------------------------------------------- /Leetcode/DP/Minimum_One_Bit_Operations_to_Make_Integers_Zero.java: -------------------------------------------------------------------------------- 1 | public class Minimum_One_Bit_Operations_to_Make_Integers_Zero 2 | { 3 | // REFER FOR EXPLANATION : https://youtu.be/Lp2pncT8FXc 4 | Map dp; 5 | public int minMoves(int n) 6 | { 7 | if(n == 0) return 0; 8 | if(dp.containsKey(n)) return dp.get(n); 9 | 10 | int msb = Integer.highestOneBit(n); 11 | int ans = minMoves(msb) - minMoves(n-msb); 12 | dp.put(n, ans); 13 | return ans; 14 | } 15 | public int minimumOneBitOperations(int n) { 16 | dp = new HashMap<>(); 17 | dp.put(1,1); 18 | for(int i = 2; i<(1<<30); i*=2) 19 | dp.put(i, 2*dp.get(i/2)+1); 20 | return minMoves(n); 21 | } 22 | } -------------------------------------------------------------------------------- /Leetcode/DP/Minimum_Path_Sum.java: -------------------------------------------------------------------------------- 1 | 2 | import java.util.*; 3 | public class Minimum_Path_Sum { 4 | public int minPathSum(int[][] grid) { 5 | int dp[][] = new int[grid.length][grid[0].length]; 6 | int n = grid.length; 7 | int m = grid[0].length; 8 | for(int arr[] : dp) 9 | Arrays.fill(arr, Integer.MAX_VALUE/2); 10 | dp[0][0] = grid[0][0]; 11 | for(int i =0; i0) dp[i][j] = Math.min(dp[i][j], dp[i-1][j]); 17 | if(j>0) dp[i][j] = Math.min(dp[i][j], dp[i][j-1]); 18 | dp[i][j]+=grid[i][j]; 19 | } 20 | } 21 | return dp[n-1][m-1]; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Leetcode/DP/Number_of_Smooth_Descent_Periods_of_a_Stock.java: -------------------------------------------------------------------------------- 1 | public class Number_of_Smooth_Descent_Periods_of_a_Stock { 2 | 3 | // If i and i-1 th elements form a valid descent, then ith can extend all the descents that i-1 has. 4 | // DP STATE : dp[i] = No. of descents that end at ith 5 | // DP TRANS : dp[i] = 1 + dp[i-1] 6 | public long getDescentPeriods(int[] prices) { 7 | int n = prices.length; 8 | long dp[] = new long[n]; 9 | Arrays.fill(dp, 1L); 10 | for(int i = 1; i int: 3 | 4 | from functools import cache 5 | mod = 10**9 + 7 6 | @cache 7 | def dp(pos, k): 8 | if k == 0: 9 | return 1 if pos == endPos else 0 10 | 11 | return dp(pos+1, k-1) + dp(pos-1,k-1) 12 | 13 | return dp(startPos, k) % mod -------------------------------------------------------------------------------- /Leetcode/DP/Solving_Questions_With_Brainpower.java: -------------------------------------------------------------------------------- 1 | public class Solving_Questions_With_Brainpower 2 | { 3 | // Simple 0/1 knapsack type DP. 4 | // Your either dont take curr and move to next element or you take curr and jump to next allowed 5 | long dp[]; 6 | public long maxScore(int i, int ques[][]) 7 | { 8 | if(i >= ques.length) return 0; 9 | if(dp[i]!=-1) return dp[i]; 10 | 11 | return dp[i] = Math.max(maxScore(i+1, ques), maxScore(i+ques[i][1]+1, ques) + ques[i][0]); 12 | } 13 | public long mostPoints(int[][] questions) { 14 | dp = new long[questions.length]; 15 | Arrays.fill(dp, -1); 16 | return maxScore(0, questions); 17 | } 18 | } -------------------------------------------------------------------------------- /Leetcode/DP/Unique_Binary_Search_Trees.java: -------------------------------------------------------------------------------- 1 | public class Unique_Binary_Search_Trees 2 | { 3 | Integer dp[][]; 4 | public int count(int l, int r) 5 | { 6 | if(l > r) return 1; 7 | if(dp[l][r] != null) return dp[l][r]; 8 | int cnt = 0; 9 | for(int mid = l; mid<=r; mid++) 10 | { 11 | int left = count(l, mid - 1); 12 | int right = count(mid + 1, r); 13 | cnt+=(left*right); 14 | } 15 | return dp[l][r] = cnt; 16 | } 17 | public int numTrees(int n) { 18 | dp = new Integer[n+1][n+1]; 19 | return count(1, n); 20 | } 21 | } -------------------------------------------------------------------------------- /Leetcode/DP/Unique_Paths.py: -------------------------------------------------------------------------------- 1 | ''' 2 | The dp value at any position in the matrix is sum of paths coming from above it(if cell exists) 3 | and to the left of it(if cell exists). 4 | ''' 5 | 6 | class Solution: 7 | def uniquePaths(self, m: int, n: int) -> int: 8 | dp = [[0 for j in range(n)] for i in range(m)] 9 | dp[0][0] = 1 10 | 11 | for i in range(m): 12 | for j in range(n): 13 | if i==0 and j==0: continue 14 | 15 | if(j-1>=0): 16 | dp[i][j]+=dp[i][j-1] 17 | if(i-1>=0): 18 | dp[i][j]+=dp[i-1][j] 19 | return dp[m-1][n-1] 20 | -------------------------------------------------------------------------------- /Leetcode/DP/Unique_Paths_II.java: -------------------------------------------------------------------------------- 1 | package Leetcode.DP; 2 | 3 | public class Unique_Paths_II 4 | { 5 | public int uniquePathsWithObstacles(int[][] obs) { 6 | int m = obs.length, n = obs[0].length; 7 | int dp[][] = new int[m][n]; 8 | 9 | if(obs[0][0]!=1)dp[0][0] = 1; 10 | for(int i =0; i=0 && obs[i-1][j]!=1)dp[i][j]+=dp[i-1][j]; 16 | if((j-1)>=0 && obs[i][j-1]!=1)dp[i][j]+=dp[i][j-1]; 17 | } 18 | } 19 | return dp[m-1][n-1]; 20 | } 21 | } -------------------------------------------------------------------------------- /Leetcode/DP/Wiggle_Subsequence.java: -------------------------------------------------------------------------------- 1 | package Leetcode.DP; 2 | 3 | class Wiggle_Subsequence { 4 | //REFER Here: https://youtu.be/iJIAPtd9Z9s 5 | public int wiggleMaxLength(int[] nums) { 6 | if (nums.length == 0) return 0; 7 | int up = 1, down = 1; 8 | for (int i = 1; i < nums.length; i++) { 9 | if (nums[i] < nums[i - 1]) down = up + 1; 10 | else if (nums[i] > nums[i - 1]) up = down + 1; 11 | } 12 | return Math.max(up, down); 13 | } 14 | } -------------------------------------------------------------------------------- /Leetcode/December Coding Challenge/Can_Place_Flowers.py: -------------------------------------------------------------------------------- 1 | class Can_Place_Flowers: 2 | def canPlaceFlowers(self, flowerbed: List[int], n: int) -> bool: 3 | count = 0 4 | i = 0 5 | while i < len(flowerbed): 6 | if flowerbed[i] == 0 and (i == 0 or flowerbed[i - 1] == 0) and (i == len(flowerbed) - 1 or flowerbed[i + 1] == 0): 7 | flowerbed[i] = 1 8 | count = count + 1 9 | i+=1 10 | return count >= n -------------------------------------------------------------------------------- /Leetcode/December Coding Challenge/Maximum_Depth_of_Binary_Tree.java: -------------------------------------------------------------------------------- 1 | public class Maximum_Depth_of_Binary_Tree { 2 | public int maxDepth(TreeNode root) { 3 | if (root==null) 4 | return 0; 5 | if(root.left==null && root.right==null) 6 | return 1; 7 | int val1 = 1,val2 = 1; 8 | if(root.left!=null) 9 | val1 += maxDepth(root.left); 10 | if(root.right!=null) 11 | val2 += maxDepth(root.right); 12 | return Math.max(val1,val2); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Leetcode/December Coding Challenge/Pairs_of_Songs_With_Total_Durations_Divisible_by_60.java: -------------------------------------------------------------------------------- 1 | public class Pairs_of_Songs_With_Total_Durations_Divisible_by_60 { 2 | // REFER : https://youtu.be/wxqN1HX4Djk 3 | public int numPairsDivisibleBy60(int[] time) { 4 | int n = time.length; 5 | for (int i = 0; i < n; i++) 6 | time[i] = time[i] % 60; 7 | 8 | int freq[] = new int[60]; 9 | for (int i = 0; i < n; i++) 10 | freq[time[i] % 60]++; 11 | 12 | int cnt = 0; 13 | for (int i = 0; i <= 30; i++) { 14 | if (i == 0 || i == 30) 15 | cnt += (freq[i] * (freq[i] - 1)) / 2; 16 | else 17 | cnt += freq[i] * freq[60 - i]; 18 | } 19 | return cnt; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Leetcode/December Coding Challenge/Reach_a_Number.java: -------------------------------------------------------------------------------- 1 | public class Reach_a_Number 2 | { 3 | // REFER : https://youtu.be/kz_0GjhFOzc 4 | public int reachNumber(int target) { 5 | int moves = 0; 6 | int sum = 0; 7 | target = Math.abs(target); 8 | while(sum List[int]: 3 | nums = sorted(list(map(abs,nums))) 4 | return [i*i for i in nums] -------------------------------------------------------------------------------- /Leetcode/December Coding Challenge/The_kth_Factor_of_n.java: -------------------------------------------------------------------------------- 1 | class The_kth_Factor_of_n { 2 | public int kthFactor(int n, int k) { 3 | ArrayDeque arr = new ArrayDeque(); 4 | for(int i= (int)Math.sqrt(n);i>0;i--) 5 | { 6 | if(n%i==0) 7 | { 8 | if(i !=n/i){ 9 | arr.offerFirst(i); 10 | arr.offerLast(n/i); 11 | } 12 | else 13 | arr.offerFirst(i); 14 | } 15 | } 16 | //System.out.println(arr); 17 | ArrayList al = new ArrayList(arr); 18 | if( arr.size()arr[i-1] ) 9 | { 10 | if(arr[i]>arr[i+1]){ 11 | ind = i; 12 | break; 13 | } 14 | } 15 | else 16 | { 17 | return false; 18 | } 19 | } 20 | 21 | 22 | while(ind < n-1) 23 | { 24 | if(arr[ind] <= arr[ind+1]) 25 | return false; 26 | ind++; 27 | } 28 | return true; 29 | } 30 | } -------------------------------------------------------------------------------- /Leetcode/FenwickTree/HashMap.java: -------------------------------------------------------------------------------- 1 | 2 | public class HashMap<> { 3 | 4 | } 5 | -------------------------------------------------------------------------------- /Leetcode/Graphs/Minimum_Number_of_Vertices_to_Reach_All_Nodes.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | // IDEA : Calculate the indegree of each node. All nodes with 0 indegree are part of answer. 4 | 5 | public class Minimum_Number_of_Vertices_to_Reach_All_Nodes { 6 | public List findSmallestSetOfVertices(int n, List> edges) { 7 | List res = new ArrayList<>(); 8 | int indeg[] = new int[n]; 9 | for(int i =0; i pq = new PriorityQueue<>(); 6 | 7 | for(int i = 1; iladders) 15 | { 16 | bricks -= pq.poll(); 17 | } 18 | if(bricks<0) return i-1; 19 | } 20 | return n-1; 21 | 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Leetcode/Greedy/Jump_Game_II.java: -------------------------------------------------------------------------------- 1 | package Leetcode.Greedy; 2 | 3 | // REFER : https://leetcode.com/problems/jump-game-ii/discuss/18014/Concise-O(n)-one-loop-JAVA-solution-based-on-Greedy 4 | 5 | public class Jump_Game_II { 6 | public int jump(int[] nums) { 7 | int jumps = 0; 8 | int n = nums.length; 9 | int bestPosCanReach = 0; 10 | int currEnd = 0; 11 | for(int i =0; i pq = new PriorityQueue<>(Collections.reverseOrder()); 9 | for(int num : nums) pq.offer(num); 10 | long score = 0; 11 | while(k>0) 12 | { 13 | score+=pq.peek(); 14 | int ceil = (pq.poll()+2)/3; 15 | pq.offer(ceil); 16 | k--; 17 | } 18 | return score; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Leetcode/Greedy/Maximum_Bags_With_Full_Capacity_of_Rocks.java: -------------------------------------------------------------------------------- 1 | public class Maximum_Bags_With_Full_Capacity_of_Rocks 2 | { 3 | // REFER : Pick greedily least difference 4 | public int maximumBags(int[] capacity, int[] rocks, int extra) { 5 | List list = new ArrayList<>(); 6 | int n = rocks.length; 7 | for(int i = 0; i=(n/3); i-=2) coins+=piles[i]; 9 | return coins; 10 | } 11 | } -------------------------------------------------------------------------------- /Leetcode/Greedy/Maximum_Product_After_K_Increments.java: -------------------------------------------------------------------------------- 1 | public class Maximum_Product_After_K_Increments { 2 | public int maximumProduct(int[] nums, int k) { 3 | PriorityQueue pq = new PriorityQueue<>(); 4 | int mod = (int) 1e9+7; 5 | for(int num : nums) pq.add(num); 6 | while(k>0) 7 | { 8 | pq.add((pq.poll()+1)%mod); 9 | k--; 10 | } 11 | long res = 1L; 12 | while(!pq.isEmpty()) 13 | { 14 | res*=pq.poll(); 15 | res%=mod; 16 | } 17 | return (int)res; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Leetcode/Greedy/Maximum_Score_From_Removing_Stones.java: -------------------------------------------------------------------------------- 1 | public class Maximum_Score_From_Removing_Stones { 2 | public int maximumScore(int a, int b, int c) { 3 | PriorityQueue pq = new PriorityQueue<>(Collections.reverseOrder()); 4 | pq.add(a);pq.add(b);pq.add(c); 5 | int score = 0; 6 | while(pq.size()>1) 7 | { 8 | a = pq.poll() - 1; 9 | b = pq.poll() - 1; 10 | if(a!=0) pq.add(a); 11 | if(b!=0) pq.add(b); 12 | score++; 13 | } 14 | return score; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Leetcode/Greedy/Minimize_Maximum_Pair_Sum_in_Array.java: -------------------------------------------------------------------------------- 1 | 2 | // IDEA : Since we want to minimise the sum of pairs, we want to pair up the smallest with largest, 3 | // then next smallest with next largest. Now, any one of these can be the max sum, so we take max of 4 | // all such pairs 5 | 6 | public class Minimize_Maximum_Pair_Sum_in_Array { 7 | public int minPairSum(int[] nums) { 8 | Arrays.sort(nums); 9 | int n = nums.length; 10 | int maxisum = Integer.MIN_VALUE; 11 | for(int i=0; i set = new HashSet<>(); 10 | Arrays.sort(freq); 11 | int moves = 0; 12 | for(int num : freq) 13 | { 14 | if(num == 0) 15 | continue; 16 | while(num!=0 && set.contains(num)) 17 | { 18 | num--; 19 | moves++; 20 | } 21 | set.add(num); 22 | } 23 | return moves; 24 | } 25 | } 26 | } -------------------------------------------------------------------------------- /Leetcode/Greedy/Minimum_Difference_Between_Largest_and_Smallest_Value_in_Three_Moves.java: -------------------------------------------------------------------------------- 1 | public class Minimum_Difference_Between_Largest_and_Smallest_Value_in_Three_Moves { 2 | // IDEA : Same as in Discuss section. 3 | public int minDifference(int[] nums) { 4 | Arrays.sort(nums); 5 | // System.out.println(Arrays.toString(nums)); 6 | int n = nums.length; 7 | if(nums.length <=4) 8 | return 0; 9 | int mini = Integer.MAX_VALUE; 10 | for(int i = 0;i<4; i++) 11 | { 12 | int small = nums[i]; 13 | int large = nums[n-4+i]; 14 | mini = Math.min(mini, large - small); 15 | } 16 | return mini; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Leetcode/Greedy/Minimum_Moves_to_Equal_Array_Elements_II.java: -------------------------------------------------------------------------------- 1 | public class Minimum_Moves_to_Equal_Array_Elements_II { 2 | // Answer is distance of all other elements to median 3 | public int minMoves2(int[] nums) { 4 | Arrays.sort(nums); 5 | int n = nums.length; 6 | int median = nums[n/2]; 7 | int sum = 0; 8 | for(int num : nums) sum+=Math.abs(median - num); 9 | return sum; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Leetcode/Greedy/Minimum_Number_of_Increments_on_Subarrays_to_Form_a_Target_Array.java: -------------------------------------------------------------------------------- 1 | package Leetcode.Greedy; 2 | // REFER : https://leetcode.com/problems/minimum-number-of-increments-on-subarrays-to-form-a-target-array/discuss/754623/Detailed-Explanation 3 | public class Minimum_Number_of_Increments_on_Subarrays_to_Form_a_Target_Array { 4 | public int minNumberOperations(int[] target) { 5 | int moves = target[0]; 6 | 7 | for(int i=1; i=target[i-1]) 10 | { 11 | moves+=target[i] - target[i-1]; 12 | } 13 | } 14 | return moves; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Leetcode/Greedy/Optimal_Partition_of_String.java: -------------------------------------------------------------------------------- 1 | public class Optimal_Partition_of_String 2 | { 3 | // IDEA : Using a bitmask to check whether a letter is already present 4 | // Greedily extend a partition until u can 5 | public int partitionString(String str) { 6 | char s[] = str.toCharArray(); 7 | int cnt = 1; 8 | int mask = 0; 9 | for(int i = 0; i 1) { 6 | char[] arr = s.toCharArray(); 7 | Arrays.sort(arr); 8 | return String.valueOf(arr); 9 | } else { 10 | String min = s; 11 | for (int i = 0; i < s.length(); i++) { 12 | s = s.substring(1) + s.charAt(0); 13 | min = min.compareTo(s) < 0 ? min : s; 14 | } 15 | return min; 16 | } 17 | } 18 | } -------------------------------------------------------------------------------- /Leetcode/Greedy/Reaching_Points.java: -------------------------------------------------------------------------------- 1 | public class Reaching_Points 2 | { 3 | // REFER : https://leetcode.com/problems/reaching-points/discuss/230588/Easy-to-understand-diagram-and-recursive-solution 4 | // TOUGH MODULO APPLICATION CONCEPT 5 | public boolean reachingPoints(int sx, int sy, int tx, int ty) { 6 | while(sx < tx && sy < ty) 7 | { 8 | if(tx=sy && (ty-sy)%sx == 0; 15 | if(sy == ty) 16 | return tx>=sx && (tx-sx)%sy == 0; 17 | return false; 18 | } 19 | } -------------------------------------------------------------------------------- /Leetcode/Greedy/Stone_Game.java: -------------------------------------------------------------------------------- 1 | package Leetcode.Greedy; 2 | 3 | public class Stone_Game { 4 | // REFER :https://youtu.be/ww4V7vRIzSk 5 | public boolean stoneGame(int[] piles) { 6 | return true; 7 | 8 | 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /Leetcode/Miscellaneous/3Sum_Closest.java: -------------------------------------------------------------------------------- 1 | class _3Sum_Closest { 2 | public int threeSumClosest(int[] nums, int target) { 3 | Arrays.sort(nums); 4 | int n = nums.length; 5 | int bestAns = -target; 6 | if(target==0)bestAns = Integer.MAX_VALUE/2; 7 | for(int i = 0; i List[int]: 9 | nums.sort() 10 | med = 0 11 | n = len(nums) 12 | 13 | ans = [] 14 | for i in range(n//2): 15 | ans.append(nums[i]) 16 | ans.append(nums[n-i-1]) 17 | if(n%2!=0): 18 | ans.append(nums[n//2]) 19 | 20 | return ans -------------------------------------------------------------------------------- /Leetcode/Miscellaneous/Average_Waiting_Time.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public double averageWaitingTime(int[][] cust) { 3 | int prevEnd = 1; 4 | int n = cust.length; 5 | double time=0; 6 | for(int i=0;icust[i][0]){ 10 | time+=prevEnd-cust[i][0]; 11 | prevEnd =prevEnd+cust[i][1]; 12 | } 13 | else 14 | { 15 | prevEnd = cust[i][0]+cust[i][1]; 16 | } 17 | System.out.println(prevEnd); 18 | } 19 | return time/n; 20 | } 21 | } -------------------------------------------------------------------------------- /Leetcode/Miscellaneous/Binary_String_With_Substrings_Representing_1_To_N.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def queryString(self, s: str, n: int) -> bool: 3 | 4 | for num in range(1, n+1): 5 | binrep = bin(num)[2:] 6 | if binrep not in s: return False 7 | return True -------------------------------------------------------------------------------- /Leetcode/Miscellaneous/Bulls_and_Cows.java: -------------------------------------------------------------------------------- 1 | public class Bulls_and_Cows { 2 | public String getHint(String secret, String guess) { 3 | char sec[] = secret.toCharArray(); 4 | int freq[] = new int[10]; 5 | for (char s : sec) 6 | freq[s - '0']++; 7 | char g[] = guess.toCharArray(); 8 | int n = g.length; 9 | int x = 0, y = 0; 10 | for (int i = 0; i < n; i++) 11 | if (sec[i] == g[i]) { 12 | x++; 13 | freq[g[i] - '0']--; 14 | } 15 | for (int i = 0; i < n; i++) { 16 | if (sec[i] != g[i] && freq[g[i] - '0'] > 0) { 17 | y++; 18 | freq[g[i] - '0']--; 19 | } 20 | } 21 | return "" + x + "A" + y + "B"; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Leetcode/Miscellaneous/Check_If_a_String_Contains_All_Binary_Codes_of_Size_K.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def hasAllCodes(self, s: str, k: int) -> bool: #(NAIVE APPROACH) (ACCEPTED) 3 | #Algorithm: 4 | # For a value k, there will be 2^k binary strings of length k 5 | # Since we only need to check the k length substrings...we put al the substrings in a set 6 | # and check if the length is equal to 2^k 7 | # Note: 2^k can be written as 1<0) 9 | { 10 | if(pow<=n) 11 | n-=pow; 12 | pow/=3; 13 | } 14 | return n == 0; 15 | 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Leetcode/Miscellaneous/Check_if_Word_Equals_Summation_of_Two_Words.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isSumEqual(self, f: str, s: str, t: str) -> bool: 3 | def calc(x): 4 | c = '' 5 | for i in x: 6 | c+=str(ord(i) - 97) 7 | return int(c) 8 | return calc(f) + calc(s) == calc(t) -------------------------------------------------------------------------------- /Leetcode/Miscellaneous/Check_if_the_Sentence_Is_Pangram.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def checkIfPangram(self, sentence: str) -> bool: 3 | return len(set(sentence))==26 -------------------------------------------------------------------------------- /Leetcode/Miscellaneous/Combination_Sum_III.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class Combination_Sum_III { 3 | 4 | List> res = new ArrayList<>(); 5 | public void recur(int ind, int k, int n, List curr) 6 | { 7 | if(n == 0 && k==0) 8 | { 9 | res.add(new ArrayList<>(curr)); 10 | return; 11 | } 12 | if(n<0 || ind==10) return; 13 | recur(ind+1, k, n, curr); 14 | curr.add(ind); 15 | recur(ind+1, k-1, n-ind, curr); 16 | curr.remove(curr.size()-1); 17 | } 18 | public List> combinationSum3(int k, int n) { 19 | recur(1, k, n, new ArrayList<>()); 20 | return res; 21 | } 22 | } -------------------------------------------------------------------------------- /Leetcode/Miscellaneous/Construct_K_Palindrome_Strings.py: -------------------------------------------------------------------------------- 1 | from collections 2 | from typing import Counter 3 | class Construct_K_Palindrome_Strings: 4 | def canConstruct(self, s: str, k: int) -> bool: 5 | if len(s) < k: 6 | return False 7 | 8 | d = Counter(s) 9 | odd_cnt = 0 10 | for i in d: 11 | if d[i]%2!=0: 12 | odd_cnt+=1 13 | if odd_cnt>k: 14 | return False 15 | return True -------------------------------------------------------------------------------- /Leetcode/Miscellaneous/Contains_Duplicate.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def containsDuplicate(self, nums: List[int]) -> bool: 3 | d = defaultdict(int) 4 | for i in nums: 5 | d[i]+=1 6 | return True if max(d.values())>1 else False -------------------------------------------------------------------------------- /Leetcode/Miscellaneous/Count_Nice_Pairs_in_an_Array.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | # rearrange eqn to get i's on one side and j's on one side...then its just a counting problem 3 | def countNicePairs(self, nums) -> int: 4 | from collections import Counter 5 | d = Counter() 6 | for num in nums: 7 | v = num - int(str(num)[::-1]) 8 | d[v]+=1 9 | ans = 0 10 | for v in d.values(): 11 | ans+=(v*(v-1))//2 12 | return ans % (10**9+7) -------------------------------------------------------------------------------- /Leetcode/Miscellaneous/Count_Number_of_Distinct_Integers_After_Reverse_Operations.java: -------------------------------------------------------------------------------- 1 | public class Count_Number_of_Distinct_Integers_After_Reverse_Operations { 2 | public int countDistinctIntegers(int[] nums) { 3 | Set set = new HashSet<>(); 4 | for(int num : nums) 5 | { 6 | set.add(num); 7 | set.add(Integer.valueOf(new StringBuilder(""+num).reverse().toString())); 8 | } 9 | return set.size(); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Leetcode/Miscellaneous/Count_Number_of_Homogenous_Substrings.java: -------------------------------------------------------------------------------- 1 | public class Count_Number_of_Homogenous_Substrings 2 | { 3 | // Count consecutive characters and cnt*(cnt+1)/2 (total substrings formed) to the answer 4 | public int countHomogenous(String s) { 5 | long cnt = 0, mod = (int)1e9+7; 6 | char prev = '\0'; 7 | long res = 0; 8 | for(char ch : s.toCharArray()) 9 | { 10 | if(ch == prev) cnt++; 11 | else 12 | { 13 | res+=(cnt*(cnt+1))/2; 14 | res%=mod; 15 | prev = ch; 16 | cnt = 1; 17 | } 18 | } 19 | res+=(cnt*(cnt+1))/2; 20 | res%=mod; 21 | return (int) res; 22 | } 23 | } -------------------------------------------------------------------------------- /Leetcode/Miscellaneous/Count_Sorted_Vowel_Strings.java: -------------------------------------------------------------------------------- 1 | /* 2 | IDEA : dp[n][ind] = No. of lexicographic sorted combinations with n letters left and current vowel as 3 | ind'th vowel 4 | */ 5 | public class Count_Sorted_Vowel_Strings { 6 | Integer dp[][]; 7 | public int dfs(int n, int ind) 8 | { 9 | if(n == 0) return 1; 10 | if(ind == 5) return 0; 11 | if(dp[n][ind]!=null) return dp[n][ind]; 12 | return dp[n][ind] = dfs(n-1, ind) + dfs(n,ind+1); 13 | } 14 | public int countVowelStrings(int n) { 15 | dp = new Integer[n+1][5]; 16 | return dfs(n, 0); 17 | } 18 | } -------------------------------------------------------------------------------- /Leetcode/Miscellaneous/Count_the_Hidden_Sequences.java: -------------------------------------------------------------------------------- 1 | // REFER : https://leetcode.com/problems/count-the-hidden-sequences/discuss/1709710/C%2B%2B-One-pass-O(N)-time 2 | public class Count_the_Hidden_Sequences { 3 | 4 | public int numberOfArrays(int[] diff, int lower, int upper) { 5 | long mini = 0; 6 | long maxi = 0; 7 | long ai = 0; 8 | for(int i : diff) 9 | { 10 | ai+=i; 11 | mini = Math.min(mini, ai); 12 | maxi = Math.max(maxi, ai); 13 | } 14 | long uprange = upper - maxi; 15 | long downrange = mini - lower; 16 | long totElements = uprange + downrange + 1L; 17 | return (int)Math.max(totElements, 0); 18 | } 19 | } -------------------------------------------------------------------------------- /Leetcode/Miscellaneous/Count_the_Number_of_Consistent_Strings.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def countConsistentStrings(self, allowed: str, words: List[str]) -> int: 3 | cnt = 0 4 | for i in words: 5 | b = True 6 | for ch in i: 7 | if ch not in allowed: 8 | b =False 9 | if b: 10 | cnt+=1 11 | return cnt; -------------------------------------------------------------------------------- /Leetcode/Miscellaneous/Delete_the_Middle_Node_of_a_Linked_List.java: -------------------------------------------------------------------------------- 1 | public class Delete_the_Middle_Node_of_a_Linked_List { 2 | public ListNode deleteMiddle(ListNode head) { 3 | ListNode gen = new ListNode(); 4 | gen.next = head; 5 | ListNode prev = gen; 6 | ListNode slow = head, fast = head; 7 | while(fast!=null && fast.next!=null) 8 | { 9 | prev = slow; 10 | slow = slow.next; 11 | fast = fast.next.next; 12 | } 13 | prev.next = slow.next; 14 | return gen.next; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Leetcode/Miscellaneous/Destroying_Asteroids.java: -------------------------------------------------------------------------------- 1 | public class Destroying_Asteroids { 2 | // Sort, sequentially destroy and add to mass 3 | public boolean asteroidsDestroyed(int mass, int[] ast) { 4 | Arrays.sort(ast); 5 | long store = mass; 6 | for(int i :ast) 7 | { 8 | if(store bool: 3 | n = len(s) 4 | s1 = s[:n//2] 5 | s2 = s[n//2:] 6 | c1 = c2 = 0 7 | for i in s1.lower(): 8 | if i in "aeiou": 9 | c1+=1 10 | for i in s2.lower(): 11 | if i in "aeiou": 12 | c2+=1 13 | return c1==c2 14 | -------------------------------------------------------------------------------- /Leetcode/Miscellaneous/Determine_if_Two_Strings_Are_Close.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def closeStrings(self, word1: str, word2: str) -> bool: 3 | d1 = {} 4 | d2 = {} 5 | for i in word1: 6 | if i not in d1.keys(): 7 | d1[i] = 1 8 | else: 9 | d1[i]+=1 10 | for i in word2: 11 | if i not in d2.keys(): 12 | d2[i] = 1 13 | else: 14 | d2[i]+=1 15 | l1 = sorted(d1.values()) 16 | l2 = sorted(d2.values()) 17 | #print(l1,l2) 18 | if l1!=l2: 19 | return False 20 | if sorted(d1.keys()) != sorted(d2.keys()): 21 | return False 22 | return True -------------------------------------------------------------------------------- /Leetcode/Miscellaneous/Distribute_Candies.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def distributeCandies(self, can: List[int]) -> int: 3 | s = set(can) 4 | if len(s)>len(can)//2: 5 | return len(can)//2 6 | return len(s) -------------------------------------------------------------------------------- /Leetcode/Miscellaneous/Eliminate_Maximum_Number_of_Monsters.java: -------------------------------------------------------------------------------- 1 | class Eliminate_Maximum_Number_of_Monsters { 2 | public int eliminateMaximum(int[] dist, int[] speed) { 3 | double time[] = new double[dist.length]; 4 | for( int i = 0; i pq = new PriorityQueue(); 9 | for( int i = 0; i0) 16 | { 17 | pq.poll(); 18 | passedMinute++; 19 | 20 | } 21 | return passedMinute; 22 | } 23 | } -------------------------------------------------------------------------------- /Leetcode/Miscellaneous/Find_Kth_Bit_in_Nth_Binary_String.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findKthBit(self, n: int, k: int) -> str: 3 | num = [0] 4 | k-=1 5 | while(k>=len(num)): 6 | num.append(1) 7 | for i in range(len(num)-2, -1, -1): 8 | num.append(num[i]^1) 9 | return str(num[k]) -------------------------------------------------------------------------------- /Leetcode/Miscellaneous/Find_Three_Consecutive_Integers_That_Sum_to_a_Given_Number.java: -------------------------------------------------------------------------------- 1 | pulic class Find_Three_Consecutive_Integers_That_Sum_to_a_Given_Number 2 | { 3 | // Consider 3 elements as mid-1, mid, mid+1 4 | // Sum of these is 3*mid 5 | public long[] sumOfThree(long num) { 6 | if(num%3==0) 7 | { 8 | num/=3; 9 | return new long[]{num-1, num, num+1}; 10 | } 11 | return new long[]{}; 12 | } 13 | } -------------------------------------------------------------------------------- /Leetcode/Miscellaneous/Find_XOR_Sum_of_All_Pairs_Bitwise_AND.java: -------------------------------------------------------------------------------- 1 | public class Find_XOR_Sum_of_All_Pairs_Bitwise_AND 2 | { 3 | // REFER : https://leetcode.com/problems/find-xor-sum-of-all-pairs-bitwise-and/discuss/1163992/JavaC++Python-Easy-and-Concise-O(1)-Space/910278 4 | public int getXORSum(int[] arr1, int[] arr2) { 5 | int xor1 = 0, xor2 = 0; 6 | for(int num : arr1) 7 | xor1^=num; 8 | for(int num : arr2) 9 | xor2^=num; 10 | return xor1 & xor2; 11 | 12 | } 13 | } -------------------------------------------------------------------------------- /Leetcode/Miscellaneous/First_Unique_Character_in_a_String.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def firstUniqChar(self, s: str) -> int: 3 | d = {} 4 | for i in s: 5 | if i not in d.keys(): 6 | d[i] = 1 7 | else: 8 | d[i]+=1 9 | 10 | for i in range(len(s)): 11 | if d[s[i]]==1: 12 | return i 13 | return -1 -------------------------------------------------------------------------------- /Leetcode/Miscellaneous/Goal_Parser_Interpretation.py: -------------------------------------------------------------------------------- 1 | class Goal_Parser_Interpretation: 2 | def interpret(self, command: str) -> str: 3 | return command.replace("()","o").replace("(al)", "al") -------------------------------------------------------------------------------- /Leetcode/Miscellaneous/Group_Anagrams.py: -------------------------------------------------------------------------------- 1 | # Sort each string...and add to the list of its corresponding key 2 | class Solution: 3 | def groupAnagrams(self, strs: List[str]) -> List[List[str]]: 4 | d = Counter() 5 | for s in strs: 6 | ss = "".join(sorted(s)) 7 | if ss not in d: 8 | d[ss] = [] 9 | d[ss].append(s) 10 | 11 | res = [] 12 | for s in d.values(): 13 | res.append(s) 14 | 15 | return res -------------------------------------------------------------------------------- /Leetcode/Miscellaneous/HTML_Entity_Parser.java: -------------------------------------------------------------------------------- 1 | package Leetcode.Miscellaneous; 2 | 3 | public class HTML_Entity_Parser { 4 | public String entityParser(String text) { 5 | text = text.replaceAll(""", "\""); 6 | text = text.replaceAll("'", "'"); 7 | text = text.replaceAll(">", ">"); 8 | text = text.replaceAll("<", "<"); 9 | text = text.replaceAll("⁄", "/"); 10 | text = text.replaceAll("&", "&"); 11 | return text; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Leetcode/Miscellaneous/Incremental_Memory_Leak.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def memLeak(self, m1: int, m2: int) -> List[int]: 3 | i = 1 4 | 5 | while(True): 6 | if(i>m1 and i>m2): 7 | return [i,m1,m2] 8 | if(m1=0) 7 | { 8 | if( i + nums[i] >=lastPosToReach) 9 | lastPosToReach = i; 10 | i-=1; 11 | } 12 | return lastPosToReach==0; 13 | } 14 | } -------------------------------------------------------------------------------- /Leetcode/Miscellaneous/Keys_and_Rooms.java: -------------------------------------------------------------------------------- 1 | package Leetcode; 2 | import java.util.*; 3 | public class Keys_and_Rooms { 4 | HashSet hs = new HashSet(); 5 | public void DFS(List> rooms, int currRoom) 6 | { 7 | //Simple DFS....At the end..check if no. of visited rooms == no. of rooms 8 | if(hs.contains(currRoom)) return; 9 | hs.add(currRoom); 10 | for( int i = 0;i> rooms) { 16 | DFS(rooms, 0); 17 | return hs.size()==rooms.size(); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Leetcode/Miscellaneous/Largest_Number.java: -------------------------------------------------------------------------------- 1 | public class Largest_Number { 2 | 3 | } 4 | -------------------------------------------------------------------------------- /Leetcode/Miscellaneous/Last_Stone_Weight.java: -------------------------------------------------------------------------------- 1 | class Last_Stone_Weight { 2 | public int lastStoneWeight(int[] stones) { 3 | 4 | PriorityQueue heap = new PriorityQueue<>(Collections.reverseOrder()); 5 | for(int i=0;i1) 10 | { 11 | int a = heap.poll(); 12 | int b = heap.poll(); 13 | if(a!=b) 14 | heap.add(a-b); 15 | } 16 | if(heap.size()==0) 17 | return 0; 18 | else 19 | return heap.poll(); 20 | } 21 | } -------------------------------------------------------------------------------- /Leetcode/Miscellaneous/Length_of_the_Longest_Alphabetical_Continuous_Substring.java: -------------------------------------------------------------------------------- 1 | package Leetcode.Miscellaneous; 2 | 3 | // IDEA : Keep forming the continuous string till you can. When you cannot, resent the count. 4 | // At each step, take the max of the continuous substring length. 5 | public class Length_of_the_Longest_Alphabetical_Continuous_Substring { 6 | public int longestContinuousSubstring(String s) { 7 | char str[] = s.toCharArray(); 8 | int maxi = 1, cnt = 1; 9 | for(int i = 1; i set = new HashSet<>(); 5 | public void dfs(String s, int mask, String tiles) 6 | { 7 | set.add(s); 8 | if(mask == (1< list; 4 | public void recur(int num, int n) 5 | { 6 | if(num > n) return; 7 | list.add(num); 8 | for(int d = 0; d<10; d++) 9 | recur(num*10+d, n); 10 | } 11 | public List lexicalOrder(int n) { 12 | list = new ArrayList<>(); 13 | for(int i = 1; i<10; i++) 14 | recur(i,n); 15 | return list; 16 | } 17 | } -------------------------------------------------------------------------------- /Leetcode/Miscellaneous/Linked_List_Cycle.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * class ListNode { 4 | * int val; 5 | * ListNode next; 6 | * ListNode(int x) { 7 | * val = x; 8 | * next = null; 9 | * } 10 | * } 11 | */ 12 | public class Solution { 13 | public boolean hasCycle(ListNode head) { 14 | 15 | ListNode slowptr = head; 16 | ListNode fastptr = head; 17 | if(head==null || head.next==null) return false; 18 | do 19 | { 20 | slowptr = slowptr.next; 21 | if(slowptr==null) return false; 22 | fastptr = fastptr.next.next; 23 | if(fastptr == null || fastptr.next==null) return false; 24 | }while(slowptr!=fastptr); 25 | return true; 26 | 27 | } 28 | } -------------------------------------------------------------------------------- /Leetcode/Miscellaneous/Longest_Subarray_With_Maximum_Bitwise_AND.java: -------------------------------------------------------------------------------- 1 | package Leetcode.Miscellaneous; 2 | 3 | // IDEA : For a subarray, AND <= MAX(subarray)....so we only find the longest subarray with all maxi 4 | 5 | public class Longest_Subarray_With_Maximum_Bitwise_AND { 6 | public int longestSubarray(int[] nums) { 7 | 8 | int maxi = -1; 9 | for(int num : nums) maxi = Math.max(maxi, num); 10 | int cnt = 0, ans = 1; 11 | for(int num : nums) 12 | { 13 | if(num == maxi) cnt++; 14 | else cnt = 0; 15 | ans = Math.max(ans, cnt); 16 | } 17 | return ans; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Leetcode/Miscellaneous/Longest_Uploaded_Prefix.java: -------------------------------------------------------------------------------- 1 | public class Longest_Uploaded_Prefix { 2 | int endPoint,n; 3 | boolean arr[]; 4 | public LUPrefix(int n) { 5 | this.n = n; 6 | endPoint = 0; 7 | arr = new boolean[n+1]; 8 | } 9 | 10 | public void upload(int video) { 11 | arr[video] = true; 12 | while(endPoint int: 3 | nums.sort() 4 | n = len(nums) 5 | i=0 6 | j=n-1 7 | cnt = 0 8 | while(inums[i]: 12 | i+=1 13 | else: 14 | cnt+=1 15 | i+=1 16 | j-=1 17 | return cnt 18 | -------------------------------------------------------------------------------- /Leetcode/Miscellaneous/Maximum_Element_After_Decreasing_and_Rearranging.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def maximumElementAfterDecrementingAndRearranging(self, arr: List[int]) -> int: 3 | arr.sort() 4 | n = len(arr) 5 | arr[0] = 1 6 | for i in range(1,n): 7 | if arr[i]-arr[i-1]>1: 8 | arr[i] = arr[i-1]+1 9 | return arr[-1] 10 | -------------------------------------------------------------------------------- /Leetcode/Miscellaneous/Maximum_Ice_Cream_Bars.java: -------------------------------------------------------------------------------- 1 | package Leetcode; 2 | 3 | class Maximum_ice_Cream_Bars { 4 | public int maxIceCream(int[] costs, int coins) { 5 | int n =costs.length; 6 | Arrays.sort(costs); 7 | int rem = coins, c=0; 8 | for( int i =0;i int: 3 | def digitSum(x): 4 | s = 0 5 | while(x>0): 6 | d = x%10 7 | s+=d 8 | x//=10 9 | return s 10 | d = {} 11 | for i in range(low,high+1): 12 | s = digitSum(i) 13 | if s not in d.keys(): 14 | d[s]=1 15 | else: 16 | d[s]+=1 17 | 18 | return max(d.values()) -------------------------------------------------------------------------------- /Leetcode/Miscellaneous/Maximum_Population_Year.py: -------------------------------------------------------------------------------- 1 | class Maximum_Population_Year: 2 | def maximumPopulation(self, logs: List[List[int]]) -> int: 3 | d = {} 4 | for i in range(len(logs)): 5 | for y in range(logs[i][0], logs[i][1]): 6 | if y not in d: 7 | d[y] = 1 8 | else: 9 | d[y]+=1 10 | maxi = -float(inf) 11 | ans = 0 12 | for i in sorted(d.keys()): 13 | if d[i]>maxi: 14 | ans = i 15 | maxi = d[i] 16 | return ans -------------------------------------------------------------------------------- /Leetcode/Miscellaneous/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] -------------------------------------------------------------------------------- /Leetcode/Miscellaneous/Maximum_Profit_of_Operating_a_Centennial_Wheel.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | # REFER : https://youtu.be/5QGk9NGmiNI (Alex Wice Interview Weekly) 3 | def minOperationsMaxProfit(self, customers: List[int], boardingCost: int, runningCost: int) -> int: 4 | cust = currProf = maxProf = 0 5 | rot = -1 6 | i = 0 7 | while cust or i maxProf: 16 | rot = i 17 | maxProf = currProf 18 | return -1 if maxProf<0 else rot -------------------------------------------------------------------------------- /Leetcode/Miscellaneous/Maximum_Subarray.java: -------------------------------------------------------------------------------- 1 | class Maximum_Subarray { 2 | public int maxSubArray(int[] nums) { 3 | int meh=0,msf = Integer.MIN_VALUE; 4 | for(int i = 0;i< nums.length;i++) 5 | { 6 | meh = meh + nums[i]; 7 | if ( meh < nums[i]) 8 | meh = nums[i]; 9 | if ( msf < meh) 10 | msf = meh; 11 | } 12 | return msf; 13 | 14 | 15 | } 16 | } -------------------------------------------------------------------------------- /Leetcode/Miscellaneous/Maximum_Sum_of_an_Hourglass.java: -------------------------------------------------------------------------------- 1 | public class Maximum_Sum_of_an_Hourglass { 2 | // Brute force 3 | public int maxSum(int[][] grid) { 4 | int m = grid.length; 5 | int n = grid[0].length; 6 | long maxi = Long.MIN_VALUE; 7 | for(int x1 = 0; x1 int: 3 | l = [] 4 | for i, v in enumerate(nums): 5 | if v==target: 6 | l.append(i) 7 | mini = 9999999999 8 | for i in l: 9 | mini = min(mini, abs(i-start)) 10 | return mini -------------------------------------------------------------------------------- /Leetcode/Miscellaneous/Minimum_Elements_to_Add_to_Form_a_Given_Sum.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def minElements(self, nums: List[int], limit: int, goal: int) -> int: 3 | s = sum(nums) 4 | amt = abs(goal-s) 5 | limit = abs(limit) 6 | return (amt+limit-1)//limit # Taking Ceil -------------------------------------------------------------------------------- /Leetcode/Miscellaneous/Minimum_Flips_to_Make_a_OR_b_Equal_to_c.java: -------------------------------------------------------------------------------- 1 | public class Minimum_Flips_to_Make_a_OR_b_Equal_to_c { 2 | public int minFlips(int a, int b, int c) { 3 | int cnt = 0; 4 | for(int i = 0; i<31; i++) 5 | { 6 | int c_bit = (c & (1< 0)?0:1; 13 | } 14 | return cnt; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Leetcode/Miscellaneous/Minimum_Lines_to_Represent_a_Line_Chart.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def minimumLines(self, stocks: List[List[int]]) -> int: 3 | stocks.sort() 4 | n = len(stocks) 5 | if(len(stocks) == 1): return 0 6 | cnt = 1 7 | numer = stocks[1][1] - stocks[0][1] 8 | denom = stocks[1][0] - stocks[0][0] 9 | for i in range(1,n): 10 | curr_numer = stocks[i][1] - stocks[i-1][1] 11 | curr_denom = stocks[i][0] - stocks[i-1][0] 12 | if( curr_numer*denom == numer*curr_denom): 13 | continue 14 | cnt+=1 15 | numer = curr_numer 16 | denom = curr_denom 17 | return cnt -------------------------------------------------------------------------------- /Leetcode/Miscellaneous/Minimum_Number_of_Steps_to_Make_Two_Strings_Anagram_II.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def minSteps(self, s: str, t: str) -> int: 3 | d = Counter() 4 | for i in s: 5 | d[i]+=1 6 | for i in t: 7 | d[i]-=1 8 | 9 | #print(d) 10 | cnt = 0 11 | for i in d.values(): 12 | cnt+=abs(i) 13 | return cnt 14 | -------------------------------------------------------------------------------- /Leetcode/Miscellaneous/Minimum_Rounds_to_Complete_All_Tasks.PY: -------------------------------------------------------------------------------- 1 | # REFER : https://youtu.be/gHR4VTNlVEI 2 | 3 | from collections import Counter 4 | class Solution: 5 | def minimumRounds(self, tasks: List[int]) -> int: 6 | 7 | d = Counter(tasks) 8 | cnt = 0 9 | for v in d.values(): 10 | if v == 1: 11 | return -1 12 | elif v%3==0: 13 | cnt+=v//3 14 | elif v%3==1: 15 | cnt+=2 + (v-4)//3 16 | else: 17 | cnt+=1 + (v-2)//3 18 | return cnt 19 | 20 | -------------------------------------------------------------------------------- /Leetcode/Miscellaneous/Missing_Number.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def missingNumber(self, nums: List[int]) -> int: 3 | n = len(nums) 4 | return n*(n+1)//2 - sum(nums) -------------------------------------------------------------------------------- /Leetcode/Miscellaneous/Nim_Game.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def canWinNim(self, n: int) -> bool: 3 | return n%4!=0 -------------------------------------------------------------------------------- /Leetcode/Miscellaneous/Non-decreasing_Array.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def checkPossibility(self, nums: List[int]) -> bool: 3 | found = False 4 | n = len(nums) 5 | for i in range(1, n): 6 | if nums[i]=2 and nums[i] int: 3 | s = bin(n)[2:] 4 | cnt = 0 5 | for i in s: 6 | if i=='1': 7 | cnt+=1 8 | return cnt -------------------------------------------------------------------------------- /Leetcode/Miscellaneous/Number_of_Times_Binary_String_Is_Prefix_Aligned.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def numTimesAllBlue(self, flips: List[int]) -> int: 3 | # IDEA : If the count of SET bits at any moment is EQUAL to the maximum bit that is set...then that moment is pref-aligned 4 | setBits = maxSetBit = ans = 0 5 | for bit in flips: 6 | setBits+=1 7 | maxSetBit = max(maxSetBit, bit) 8 | if maxSetBit == setBits: 9 | ans+=1 10 | return ans -------------------------------------------------------------------------------- /Leetcode/Miscellaneous/Number_of_Ways_Where_Square_of_Number_Is_Equal_to_Product_of_Two_Numbers.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | # Couting probelm using hashmap...done in O(n^2) 3 | def numTriplets(self, nums1: List[int], nums2: List[int]) -> int: 4 | from collections import Counter 5 | d = Counter() 6 | cnt = 0 7 | for c in nums1: 8 | d.clear() 9 | target = c*c 10 | for num in nums2: 11 | if target%num == 0: 12 | cnt+=d[target//num] 13 | d[num]+=1 14 | 15 | for c in nums2: 16 | d.clear() 17 | target = c*c 18 | for num in nums1: 19 | if target%num == 0: 20 | cnt+=d[target//num] 21 | d[num]+=1 22 | return cnt -------------------------------------------------------------------------------- /Leetcode/Miscellaneous/Partitioning_Into_Minimum_Number_Of_Deci-Binary_Numbers.java: -------------------------------------------------------------------------------- 1 | class Partitioning_Into_Minimum_Number_Of_Deci-Binary_Numbers { 2 | public int minPartitions(String n) { 3 | char s[] = n.toCharArray(); 4 | int maxi = 0; 5 | for(char c : s) 6 | { 7 | maxi = Math.max(maxi, (c-'0')); 8 | } 9 | return maxi; 10 | } 11 | } -------------------------------------------------------------------------------- /Leetcode/Miscellaneous/Plus_One.java: -------------------------------------------------------------------------------- 1 | public class Plus_One { 2 | public int[] plusOne(int[] digits) { 3 | int carry = 1; 4 | 5 | for(int i =digits.length -1; i>=0 ; i--) 6 | { 7 | digits[i]+=carry; 8 | if(digits[i]==10) digits[i] = 0; 9 | else carry = 0; 10 | } 11 | if(carry==0) return digits; 12 | int arr[] = new int[digits.length+1]; 13 | System.arraycopy(digits, 0, arr, 1, digits.length); 14 | arr[0] = 1; 15 | return arr; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Leetcode/Miscellaneous/Powerful_Integers.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | 3 | # X and Y will have log amt of powers uptil bound. So we can brute force each combination in O(log(x)*log(y)). Check discussion for better math explanation 4 | def powerfulIntegers(self, x: int, y: int, bound: int) -> List[int]: 5 | 6 | X, Y, res = [1], [1], set() 7 | while x>1 and X[-1] <=bound: 8 | X.append(X[-1]*x) 9 | 10 | while y>1 and Y[-1] <=bound: 11 | Y.append(Y[-1]*y) 12 | for i in X: 13 | for j in Y: 14 | if i+j<=bound: 15 | res.add(i+j) 16 | return list(res) -------------------------------------------------------------------------------- /Leetcode/Miscellaneous/Print Words Vertically.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def printVertically(self, s: str) -> List[str]: 3 | arr = s.split() 4 | maxi = len(max(arr, key= lambda x: len(x))) 5 | 6 | narr = [] 7 | for ss in arr: 8 | spaces = " "*(maxi-len(ss)) 9 | ss+=spaces 10 | narr.append(ss) 11 | 12 | finarr = [] 13 | for i in range(maxi): 14 | tmp = "" 15 | for j in narr: 16 | tmp+=j[i] 17 | tmp = tmp.rstrip() 18 | finarr.append(tmp) 19 | return finarr -------------------------------------------------------------------------------- /Leetcode/Miscellaneous/Product_of_Array_Except_Self.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def productExceptSelf(self, nums: List[int]) -> List[int]: 3 | n = len(nums) 4 | left, right = [],[0 for i in range(n)] 5 | for i in range(n): 6 | if(i==0): 7 | left.append(nums[i]) 8 | else: 9 | left.append(nums[i]*left[i-1]) 10 | for i in range(n-1,-1,-1): 11 | if(i==n-1): 12 | right[i] = nums[i] 13 | else: 14 | right[i]=nums[i]*right[i+1] 15 | print(left, right) 16 | ans = [1 for i in range(n)] 17 | for i in range(1,n): 18 | ans[i] = left[i-1] 19 | for i in range(n-1): 20 | ans[i]*=right[i+1] 21 | return ans -------------------------------------------------------------------------------- /Leetcode/Miscellaneous/Query_Kth_Smallest_Trimmed_Number.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def smallestTrimmedNumbers(self, nums: List[str], queries: List[List[int]]) -> List[int]: 3 | ans = [] 4 | d = {} 5 | for k,trim in queries: 6 | tmp = [] 7 | for i,s in enumerate(nums): 8 | tmp.append([s[-trim:],i]) 9 | tmp.sort() 10 | ans.append(tmp[k-1][1]) 11 | return ans 12 | 13 | -------------------------------------------------------------------------------- /Leetcode/Miscellaneous/Queue_Reconstruction_by_Height.java: -------------------------------------------------------------------------------- 1 | public class Queue_Reconstruction_by_Height 2 | { 3 | // REFER : https://youtu.be/40H5vRK_H2E 4 | public int[][] reconstructQueue(int[][] people) { 5 | Arrays.sort(people, (a,b) -> b[0]==a[0]?a[1]-b[1]:b[0]-a[0]); 6 | 7 | for(int i = 1; i=k;j--) 11 | people[j+1] = people[j].clone(); 12 | people[k][0] = h; 13 | people[k][1] = k; 14 | } 15 | return people; 16 | } 17 | } -------------------------------------------------------------------------------- /Leetcode/Miscellaneous/RLE_Iterator.java: -------------------------------------------------------------------------------- 1 | // We keep a pointer to the encoding array 2 | public class RLE_Iterator { 3 | int p, encoding[]; 4 | public RLEIterator(int[] encoding) { 5 | p = 0; 6 | this.encoding = encoding; 7 | } 8 | 9 | public int next(int n) { 10 | while(pencoding[p]) 11 | { 12 | n-=encoding[p]; 13 | p+=2; 14 | } 15 | if(p == encoding.length) return -1; 16 | int val = encoding[p+1]; 17 | encoding[p] = encoding[p] - n; 18 | if(encoding[p] == 0) p+=2; 19 | return val; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Leetcode/Miscellaneous/Reduce_Array_Size_to_The_Half.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def minSetSize(self, arr: List[int]) -> int: 3 | d = {} 4 | for i in arr: 5 | if i not in d.keys(): 6 | d[i] = 1 7 | else: 8 | d[i]+=1 9 | 10 | print(list(d.keys()), flush = True) 11 | l = sorted(list(d.keys()),key = lambda x: d[x], reverse = True) 12 | s = 0 13 | i = 0 14 | while(s int: 3 | se = set(s) 4 | if s=="": 5 | return 0 6 | if s==s[::-1]: 7 | return 1 8 | return len(se) 9 | -------------------------------------------------------------------------------- /Leetcode/Miscellaneous/Reordered_Power_of_2.java: -------------------------------------------------------------------------------- 1 | package Leetcode; 2 | 3 | public class Reordered_Power_of_2 { 4 | public boolean reorderedPowerOf2(int N) { 5 | int k = 1; 6 | int narr[] = count(N); 7 | while(k<=Math.pow(10,9)) 8 | { 9 | if(Arrays.equals(narr, count(k))) 10 | return true; 11 | k<<=1; 12 | } 13 | return false; 14 | } 15 | public int[] count(int n) 16 | { 17 | int arr[] = new int[10]; 18 | while(n>0) 19 | { 20 | arr[n%10]++; 21 | n/=10; 22 | } 23 | return arr; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Leetcode/Miscellaneous/Replace_All_Digits_with_Characters.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def replaceDigits(self, s: str) -> str: 3 | n = len(s) 4 | l = list(s) 5 | for i in range(1,n,2): 6 | num = int(s[i]) 7 | l[i] = chr(ord(s[i-1])+num) 8 | return ''.join(l) -------------------------------------------------------------------------------- /Leetcode/Miscellaneous/Reverse_Integer.java: -------------------------------------------------------------------------------- 1 | public class Reverse_Integer 2 | { 3 | // To check overflow, we can just check if the after adding the digit to the end, if 4 | // we are able to retrieve it back, then no overflow, else yes 5 | public int reverse(int x) { 6 | boolean sign = Integer.signum(x) == -1; 7 | int res = 0; x = Math.abs(x); 8 | while(x>0) 9 | { 10 | res = res*10 + (x%10); 11 | if(res%10 != x%10) return 0; 12 | x/=10; 13 | } 14 | return sign?-res:res; 15 | } 16 | } -------------------------------------------------------------------------------- /Leetcode/Miscellaneous/Reverse_Linked_List.java: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * Definition for singly-linked list. 4 | * public class ListNode { 5 | * int val; 6 | * ListNode next; 7 | * ListNode() {} 8 | * ListNode(int val) { this.val = val; } 9 | * ListNode(int val, ListNode next) { this.val = val; this.next = next; } 10 | * } 11 | */ 12 | class Solution { 13 | public ListNode reverseList(ListNode head) { 14 | ListNode q = null, r = null; 15 | while(head!=null) 16 | { 17 | r = q; 18 | q = head; 19 | head = head.next; 20 | q.next = r; 21 | } 22 | return q; 23 | 24 | } 25 | } -------------------------------------------------------------------------------- /Leetcode/Miscellaneous/Reverse_Words_in_a_String_III.py: -------------------------------------------------------------------------------- 1 | def reverseWords(self, s: str) -> str: 2 | return ' '.join(list(map(lambda x: x[::-1], s.split()))) -------------------------------------------------------------------------------- /Leetcode/Miscellaneous/Richest_Customer_Wealth.java: -------------------------------------------------------------------------------- 1 | class Richest_Customer_Wealth { 2 | public int maximumWealth(int[][] accounts) { 3 | int m =accounts.length; 4 | int max = Integer.MIN_VALUE, sum =0; 5 | int n = accounts[0].length; 6 | for(int i=0;i bool: 4 | x = y = 0 5 | dirx, diry = 0,1 6 | for c in instructions: 7 | if c=='G': 8 | x+=dirx 9 | y+=diry 10 | elif c=='L': 11 | dirx,diry = -diry, dirx 12 | else: 13 | dirx, diry = diry, -dirx 14 | 15 | return (x,y)==(0,0) or (dirx,diry)!=(0,1) 16 | 17 | -------------------------------------------------------------------------------- /Leetcode/Miscellaneous/Rotate_Array.java: -------------------------------------------------------------------------------- 1 | package Leetcode.Miscellaneous; 2 | 3 | public class Rotate_Array { 4 | public void rotate(int[] nums, int k) { 5 | k %= nums.length; 6 | reverse(nums, 0, nums.length-1); // reverse the whole array 7 | reverse(nums, 0, k-1); // reverse the first part 8 | reverse(nums, k, nums.length-1); // reverse the second part 9 | } 10 | 11 | public void reverse(int[] nums, int l, int r) { 12 | while (l < r) { 13 | int tmp = nums[l]; 14 | nums[l++] = nums[r]; 15 | nums[r--] = tmp; 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Leetcode/Miscellaneous/Rotate_Image.java: -------------------------------------------------------------------------------- 1 | public class Rotate_Image { 2 | // IDEA : https://youtu.be/uB0RgD4p3LY 3 | public void rotate(int[][] mat) { 4 | int n = mat.length; 5 | for(int i = 0; i int: 3 | maxi = -1 4 | for i in s: 5 | if i in "1234567890": 6 | maxi = max(int(i),maxi) 7 | #print(maxi) 8 | maxi2 = -1 9 | for i in s: 10 | if i in "1234567890" and int(i) List[int]: 3 | s = set() 4 | n = len(nums) 5 | rep = 0 6 | for i in range(n): 7 | if nums[i] in s: 8 | rep = nums[i] 9 | else: 10 | s.add(nums[i]) 11 | fs = n*(n+1)//2 12 | fs = fs - (sum(nums) - rep) 13 | return [rep,fs] -------------------------------------------------------------------------------- /Leetcode/Miscellaneous/Short_Encoding_of_Words.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int minimumLengthEncoding(String[] words) { 3 | /* Logic: Put all strings in the set. 4 | Then remove all strings which are suffixes of any string by taking a substring from ith to end 5 | Now add the (length of word) +1 (for #) to the answer for each of remaining string 6 | */ 7 | HashSet hs = new HashSet(); 8 | for(String w: words) hs.add(w); 9 | for(String w : words) 10 | { 11 | for(int i = 1;i simplifiedFractions(int n) { 9 | List res = new ArrayList<>(); 10 | for(int i = 1; i<=n; i++) 11 | { 12 | for(int j = 1; j ts; 3 | public SmallestInfiniteSet() { 4 | ts = new TreeSet<>(); 5 | for(int i = 1; i<=1000; i++) 6 | ts.add(i); 7 | } 8 | 9 | public int popSmallest() { 10 | return ts.pollFirst(); 11 | } 12 | 13 | public void addBack(int num) { 14 | ts.add(num); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Leetcode/Miscellaneous/Smallest_Range_I.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | # REFER: https://leetcode.com/problems/smallest-range-i/discuss/173367/C++JavaPython-Check-Max-Min/965230 3 | def smallestRangeI(self, nums: List[int], k: int) -> int: 4 | maxi, mini = max(nums), min(nums) 5 | best_diff = (maxi - k) - (mini + k) 6 | return best_diff if best_diff>0 else 0 -------------------------------------------------------------------------------- /Leetcode/Miscellaneous/Sort_Colors.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class Sort_Colors { 3 | public void sortColors(int[] nums) { 4 | 5 | int a=0, b=0,c=0; 6 | for(int i=0;i str: 3 | s = s.split() 4 | s.sort(key= lambda x: x[-1]) 5 | s = map(lambda x: x[:-1], s) 6 | #print(s) 7 | return ' '.join(s) -------------------------------------------------------------------------------- /Leetcode/Miscellaneous/Splitting_a_String_Into_Descending_Consecutive_Values.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | # REFER : https://youtu.be/eDtMmysldaw 3 | def splitString(self, s: str) -> bool: 4 | def dfs(index, prev): 5 | if index == len(s): 6 | return True 7 | 8 | for j in range(index, len(s)): 9 | val = int(s[index:j+1]) 10 | if prev - val==1 and dfs(j+1,val): 11 | return True 12 | return False 13 | for i in range(len(s) -1): 14 | val = int(s[:i+1]) 15 | if dfs(i+1, val): 16 | return True 17 | return False -------------------------------------------------------------------------------- /Leetcode/Miscellaneous/Strictly_Palindromic_Number.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | # Bad problem 3 | def isStrictlyPalindromic(self, n: int) -> bool: 4 | return False -------------------------------------------------------------------------------- /Leetcode/Miscellaneous/Subsets.java: -------------------------------------------------------------------------------- 1 | public class Subsets 2 | { 3 | List> res = new ArrayList<>(); 4 | public void makeSubset(int i, int nums[], List curr) 5 | { 6 | if(i == nums.length) 7 | { 8 | res.add(new ArrayList<>(curr)); 9 | return; 10 | } 11 | makeSubset(i+1, nums, curr); 12 | curr.add(nums[i]); 13 | makeSubset(i+1, nums, curr); 14 | curr.remove(curr.size()-1); 15 | } 16 | public List> subsets(int[] nums) { 17 | makeSubset(0, nums, new ArrayList<>()); 18 | return res; 19 | } 20 | } -------------------------------------------------------------------------------- /Leetcode/Miscellaneous/Sum_of_Absolute_Differences_in_a_Sorted_Array.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def getSumAbsoluteDifferences(self, nums: List[int]) -> List[int]: 3 | n = len(nums) 4 | ans = [] 5 | prefix = [] 6 | su=0 7 | for i in range(n): 8 | su+=nums[i] 9 | prefix.append(su) 10 | #print(prefix) 11 | for i in range(n): 12 | before = i 13 | after = n-1-i 14 | p1 = 0 15 | p2 = 0 16 | if before==0: 17 | ans.append(prefix[n-1]-prefix[0] - (after*nums[i])) 18 | elif after == 0: 19 | ans.append((before*nums[i])-prefix[n-2]) 20 | else: 21 | ans.append((before*nums[i]) -prefix[i-1] + (prefix[n-1]-prefix[i]) - after*nums[i]) 22 | return ans -------------------------------------------------------------------------------- /Leetcode/Miscellaneous/Sum_of_Number_and_Its_Reverse.java: -------------------------------------------------------------------------------- 1 | public class Sum_of_Number_and_Its_Reverse { 2 | public boolean sumOfNumberAndReverse(int num) { 3 | for(int i = 0; i<=num; i++) 4 | { 5 | if(i + Integer.valueOf(new StringBuilder(""+i).reverse().toString()) == num) 6 | return true; 7 | } 8 | return false; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /Leetcode/Miscellaneous/Sum_of_Square_Numbers.java: -------------------------------------------------------------------------------- 1 | public class Sum_of_Square_Numbers { 2 | // Math.sqrt takes O(log n) time....so total time complexity is (n log n); 3 | public boolean judgeSquareSum(int c) { 4 | for(long a = 0; a*a<=c; a++) 5 | { 6 | double sqrt = Math.sqrt(c - (a*a)); 7 | if(sqrt == (int)sqrt) 8 | return true; 9 | } 10 | return false; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /Leetcode/Miscellaneous/The_k-th_Lexicographical_String_of_All_Happy_Strings_of_Length_n.java: -------------------------------------------------------------------------------- 1 | public class The_k-th_Lexicographical_String_of_All_Happy_Strings_of_Length_n { 2 | // Go for DFS in a way that creates sorted strings 3 | public List res = new ArrayList<>(); 4 | public void dfs(String curr, int n, char prev) 5 | { 6 | if(n==0) 7 | { 8 | res.add(curr); 9 | return; 10 | } 11 | char arr[] = {'a','b','c'}; 12 | for(int i =0; i<3; i++) 13 | { 14 | char ch = arr[i]; 15 | if(ch==prev) continue; 16 | dfs(curr+ch, n-1, ch); 17 | } 18 | } 19 | public String getHappyString(int n, int k) { 20 | dfs("", n, '#'); 21 | return (res.size() List[int]: 5 | freq = Counter(nums) 6 | maxFreq = max(freq.values()) 7 | d = {} 8 | boxes = [[] for i in range(maxFreq+1)] 9 | 10 | for i in freq.keys(): 11 | boxes[freq[i]].append(i) 12 | 13 | res,cnt = [],0 14 | for f in range(maxFreq,0,-1): 15 | for i in boxes[f]: 16 | res.append(i) 17 | cnt+=1 18 | if cnt==k: 19 | return res -------------------------------------------------------------------------------- /Leetcode/Miscellaneous/Triples_with_Bitwise_AND_Equal_To_Zero.java: -------------------------------------------------------------------------------- 1 | // IDEA : Simple brute force with storing and of first two elements in a map 2 | 3 | public class Triples_with_Bitwise_AND_Equal_To_Zero { 4 | public int countTriplets(int[] nums) { 5 | Map map = new HashMap<>(); 6 | for(int first : nums) 7 | for(int second : nums) 8 | map.put(first & second, map.getOrDefault(first & second,0)+1); 9 | 10 | int res = 0; 11 | for(int third : nums) 12 | for(int key : map.keySet()) 13 | if((third & key) == 0) 14 | res+=map.get(key); 15 | return res; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Leetcode/Miscellaneous/Two_Sum.java: -------------------------------------------------------------------------------- 1 | class Two_Sum { 2 | public int[] twoSum(int[] nums, int target) { 3 | Map map = new HashMap<>(); 4 | for (int i = 0; i < nums.length; i++) { 5 | int complement = target - nums[i]; 6 | if (map.containsKey(complement)) { 7 | return new int[] { map.get(complement), i }; 8 | } 9 | map.put(nums[i], i); 10 | } 11 | throw new IllegalArgumentException("No two sum solution"); 12 | } 13 | } -------------------------------------------------------------------------------- /Leetcode/Miscellaneous/Valid_Triangle_Number.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | //REFER : https://leetcode.com/problems/valid-triangle-number/discuss/128135/A-similar-O(n2)-solution-to-3-Sum 3 | public int triangleNumber(int[] nums) { 4 | int count = 0; 5 | Arrays.sort(nums); 6 | for (int k = nums.length-1; k>1; k--) { 7 | int i = 0; 8 | int j = k-1; 9 | while (i nums[k]) { 11 | count += j-i; 12 | j--; 13 | } else { 14 | i++; 15 | } 16 | } 17 | } 18 | return count; 19 | } 20 | } -------------------------------------------------------------------------------- /Leetcode/Miscellaneous/Watering_Plants.java: -------------------------------------------------------------------------------- 1 | public class Watering_Plants 2 | { 3 | public int wateringPlants(int[] plants, int capacity) { 4 | int bucket = capacity; 5 | int moves = 0; 6 | for(int i = 0; i List[str]: 5 | ans = [] 6 | for word in queries: 7 | for dword in dictionary: 8 | c = 0 9 | for ch1, ch2 in zip(word, dword): 10 | if ch1!=ch2: 11 | c+=1 12 | if c<=2: 13 | ans.append(word) 14 | break 15 | return ans -------------------------------------------------------------------------------- /Leetcode/MonoQueue/Jump_Game_VI.java: -------------------------------------------------------------------------------- 1 | public class Jump_Game_VI { 2 | public int maxResult(int[] nums, int k) { 3 | // REFER for Explanation : https://youtu.be/dHAsYTvbbj4 4 | int n = nums.length; 5 | Deque q = new ArrayDeque<>(); 6 | int dp[] = new int[n]; 7 | dp[0] = nums[0]; 8 | for(int i = 0; ik && dp[q.peekFirst()] == dp[i-k-1]) 11 | q.pollFirst(); 12 | 13 | dp[i] = nums[i]; 14 | if(!q.isEmpty()) dp[i]+=dp[q.peekFirst()]; 15 | 16 | while(!q.isEmpty() && dp[q.peekLast()] < dp[i]) 17 | q.pollLast(); 18 | q.offerLast(i); 19 | } 20 | return dp[n-1]; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Leetcode/MonoStack/Online_Stock_Span.java: -------------------------------------------------------------------------------- 1 | class StockSpanner { 2 | // LOGIC : https://leetcode.com/problems/online-stock-span/discuss/640358/JAVA-Solution-With-visualization-and-easy-explained! 3 | Deque stack; 4 | public StockSpanner() { 5 | stack = new ArrayDeque<>(); 6 | } 7 | 8 | public int next(int price) { 9 | int p[] = {price, 1}; 10 | while(!stack.isEmpty() && stack.peek()[0] <= price) 11 | p[1]+=stack.poll()[1]; 12 | stack.push(p); 13 | return p[1]; 14 | } 15 | } 16 | 17 | /** 18 | * Your StockSpanner object will be instantiated and called as such: 19 | * StockSpanner obj = new StockSpanner(); 20 | * int param_1 = obj.next(price); 21 | */ -------------------------------------------------------------------------------- /Leetcode/NumberTheory/Count_Primes.java: -------------------------------------------------------------------------------- 1 | public class Count_Primes { 2 | public int genPrimes(int n) 3 | { 4 | boolean[] prime = new boolean[n]; 5 | Arrays.fill(prime, 2, n, true); 6 | n--; 7 | for(int i = 2; i*i<=n; i++) 8 | { 9 | if(prime[i]) 10 | { 11 | 12 | for(int j = i*i; j<=n; j+=i) 13 | { 14 | prime[j] = false; 15 | } 16 | } 17 | } 18 | int cnt = 0; 19 | for(int i =0; i hm = new HashMap<>(); 7 | int n = nums.length; 8 | hm.put(0,1); 9 | int pref = 0, cnt = 0; 10 | for(int i =0; i int: 4 | d = {0:-1} 5 | s = 0 6 | bestLen = 0 7 | nums = list(map(lambda x: -1 if x==0 else 1, nums)) 8 | for i in range(len(nums)): 9 | s+=nums[i] 10 | if s not in d.keys(): 11 | d[s] = i 12 | else: 13 | bestLen = max(bestLen,i-(d[s]+1)+1) 14 | return bestLen -------------------------------------------------------------------------------- /Leetcode/Prefix_Suffix/Corporate_Flight_Bookings.java: -------------------------------------------------------------------------------- 1 | public class Corporate_Flight_Bookings 2 | { 3 | // Partial Sums 4 | public int[] corpFlightBookings(int[][] bookings, int n) { 5 | int pref[] = new int[n+2]; 6 | for(int booking[]: bookings) 7 | { 8 | pref[booking[0]]+=booking[2]; 9 | pref[booking[1]+1]-=booking[2]; 10 | } 11 | for(int i = 1; i<=n; i++) pref[i]+=pref[i-1]; 12 | int res[] = new int[n]; 13 | for(int i = 1;i<=n; i++) res[i-1] = pref[i]; 14 | return res; 15 | } 16 | } -------------------------------------------------------------------------------- /Leetcode/Prefix_Suffix/Maximum_Points_You_Can_Obtain_from_Cards.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | // IDEA : Removing from beginning or end of a fixed no. of elements...is same as keeping some middle subarray. 3 | // Problem is of Finding Min sum fixed sized window of n-k elements 4 | public int maxScore(int[] cards, int k) { 5 | int n = cards.length; 6 | k = n - k; 7 | int sum = 0, mini = Integer.MAX_VALUE, totSum = 0; 8 | for(int right = 0; right=0) sum-=cards[right - k]; 12 | if(right >= k-1) mini = Math.min(mini, sum); 13 | } 14 | return totSum - mini; 15 | } 16 | } -------------------------------------------------------------------------------- /Leetcode/Prefix_Suffix/Partition_Array_into_Disjoint_Intervals.java: -------------------------------------------------------------------------------- 1 | public class Partition_Array_into_Disjoint_Intervals { 2 | public int partitionDisjoint(int[] nums) { 3 | int prefMax = nums[0]; 4 | int n = nums.length; 5 | int suffMin[] = new int[n]; 6 | suffMin[n-1] = nums[n-1]; 7 | for(int i = n-2; i>=0; i--) 8 | suffMin[i] = Math.min(nums[i], suffMin[i+1]); 9 | 10 | for(int i = 0; i hm = new HashMap<>(); 4 | int n = nums.length; 5 | hm.put(0,1); 6 | int pref = 0, cnt = 0; 7 | for(int i =0; i map = new HashMap<>(); 11 | map.put(0,1); 12 | int currSum = 0, cnt = 0; 13 | for(int i = 0; i=1; i--) 16 | { 17 | int j = rand.nextInt(i+1); 18 | 19 | // Swapping elements 20 | int t = nums[j]; 21 | nums[j] = nums[i]; 22 | nums[i] = t; 23 | } 24 | return nums; 25 | } 26 | } -------------------------------------------------------------------------------- /Leetcode/Sliding_Window/Maximum_Erasure_Value.java: -------------------------------------------------------------------------------- 1 | 2 | import java.util.*; 3 | class Solution { 4 | public int maximumUniqueSubarray(int[] nums) { 5 | 6 | int n = nums.length; 7 | int s = 0, left =0, maxi = -1; 8 | HashSet hs = new HashSet(); 9 | for(int right = 0; right left && hs.contains(nums[right])) 13 | { 14 | hs.remove(nums[left]); 15 | s-=nums[left]; 16 | left++; 17 | } 18 | //System.out.println(s); 19 | hs.add(nums[right]); 20 | maxi = Math.max(maxi, s); 21 | 22 | } 23 | return maxi; 24 | 25 | } 26 | } -------------------------------------------------------------------------------- /Leetcode/Sliding_Window/Maximum_Number_of_Vowels_in_a_Substring_of_Given_Length.java: -------------------------------------------------------------------------------- 1 | public class Maximum_Number_of_Vowels_in_a_Substring_of_Given_Length 2 | { 3 | public int maxVowels(String s, int k) { 4 | char str[] = s.toCharArray(); 5 | String vowels = "aeiou"; 6 | int left = 0, maxi = 0; 7 | int vowelcnt = 0; 8 | for(int right = 0; right=k) 12 | { 13 | if(vowels.contains(""+str[left++])) vowelcnt--; 14 | } 15 | maxi = Math.max(maxi, vowelcnt); 16 | } 17 | return maxi; 18 | } 19 | } -------------------------------------------------------------------------------- /Leetcode/Sliding_Window/Number_of_Sub-arrays_of_Size_K_and_Average_Greater_than_or_Equal_to_Threshold.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int numOfSubarrays(int[] arr, int k, int thresh) { 3 | 4 | int sum = 0, c=0; 5 | int l = 0; 6 | for( int i =0 ; i=thresh) c++; 8 | for(int i =k; i=thresh) c++; 13 | } 14 | return c; 15 | } 16 | } -------------------------------------------------------------------------------- /Leetcode/Sliding_Window/Number_of_Substrings_Containing_All_Three_Characters.java: -------------------------------------------------------------------------------- 1 | public class Number_of_Substrings_Containing_All_Three_Characters 2 | { 3 | // REFER : https://leetcode.com/problems/number-of-substrings-containing-all-three-characters/discuss/723408/Sliding-Window-with-explanation-and-very-readable-with-comments 4 | public int numberOfSubstrings(String str) { 5 | char s[] = str.toCharArray(); 6 | int freq[] = new int[3]; 7 | int left = 0, n = s.length, cnt = 0; 8 | for(int right = 0; right0 && freq[1]>0 && freq[2]>0) 12 | { 13 | freq[s[left++]-'a']--; 14 | } 15 | cnt+=left; 16 | } 17 | return cnt; 18 | } 19 | } -------------------------------------------------------------------------------- /Leetcode/Sliding_Window/Repeated_DNA_Sequences.java: -------------------------------------------------------------------------------- 1 | public class Repeated_DNA_Sequences { 2 | // IDEA : Since str length is only 10, ..the complexity of this program is O(10n) 3 | public List findRepeatedDnaSequences(String s) { 4 | // Checking duplicates for every 1- length substring 5 | int n = s.length(); 6 | Set hs = new HashSet<>(); 7 | Set ansSet = new HashSet<>(); 8 | for(int i = 0; i(ansSet); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Leetcode/Stack/Minimum_Add_to_Make_Parentheses_Valid.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def minAddToMakeValid(self, s: str) -> int: 3 | # c = Count of unmatched opening '(' brackets 4 | # ans = Count of unmatched closing ')' brackets 5 | # If opening bracket found, we increase counter by one and if decreasing found, we decrease counter by 1 6 | # If c<0..then we just found an unmatched closing bracket....so we add 1 to the ans and reset c = 0 7 | 8 | # Final answer = ans + cnt = Count of unmatched closing bracket + Count of unmatched opening brackets 9 | ans, c = 0,0 10 | for ch in s: 11 | if ch == '(': 12 | c+=1 13 | else: 14 | c-=1 15 | if c<0: 16 | ans+=1 17 | c = 0 18 | return ans + c -------------------------------------------------------------------------------- /Leetcode/Stack/Minimum_Insertions_to_Balance_a_Parentheses_String.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | # DIFFICULT PROBLEM 3 | # IDEA : ALEX WICE INTERVIEW WEEKLY 9 4 | def minInsertions(self, s: str) -> int: 5 | ans = balance = 0 6 | for ch in s: 7 | if ch == '(': 8 | balance+=2 9 | if balance%2==1: 10 | balance-=1 11 | ans+=1 12 | else: 13 | balance-=1 14 | if balance < 0: 15 | ans+=1 16 | balance+=2 17 | return ans + balance 18 | -------------------------------------------------------------------------------- /Leetcode/Stack/Validate_Stack_Sequences.java: -------------------------------------------------------------------------------- 1 | // IDEA : Simulation Type Problem 2 | public class Validate_Stack_Sequences { 3 | public boolean validateStackSequences(int[] pushed, int[] popped) { 4 | Stack st = new Stack<>(); 5 | int k = 0; 6 | for(int i = 0; i sorted = new ArrayList<>(); 5 | public TreeNode balanceBST(TreeNode root) { 6 | inOrder(root); 7 | return arrToBST(0,sorted.size()-1); 8 | } 9 | public void inOrder(TreeNode root) 10 | { 11 | if(root==null) return; 12 | inOrder(root.left); 13 | sorted.add(root); 14 | inOrder(root.right); 15 | } 16 | public TreeNode arrToBST(int left, int right) 17 | { 18 | if(left>right) return null; 19 | int mid = left+(right-left)/2; 20 | TreeNode root = sorted.get(mid); 21 | root.left = arrToBST(left, mid - 1); 22 | root.right = arrToBST(mid + 1, right); 23 | return root; 24 | } 25 | } -------------------------------------------------------------------------------- /Leetcode/Tree/Binary_Search_Tree_to_Greater_Sum_Tree.java: -------------------------------------------------------------------------------- 1 | public class Binary_Search_Tree_to_Greater_Sum_Tree 2 | { 3 | // SAME AS Convert_BST_to_Greater_Tree 4 | int sum = 0; 5 | public void dfs(TreeNode root) 6 | { 7 | if(root == null) return; 8 | dfs(root.right); 9 | sum+=root.val; 10 | root.val = sum; 11 | dfs(root.left); 12 | } 13 | public TreeNode bstToGst(TreeNode root) { 14 | dfs(root); 15 | return root; 16 | } 17 | } -------------------------------------------------------------------------------- /Leetcode/Tree/Convert_BST_to_Greater_Tree.java: -------------------------------------------------------------------------------- 1 | public class Convert_BST_to_Greater_Tree 2 | { 3 | int currSum = 0; 4 | public void dfs(TreeNode root) 5 | { 6 | if(root==null) return; 7 | dfs(root.right); 8 | int val = root.val; 9 | root.val+=currSum; 10 | currSum+=val; 11 | dfs(root.left); 12 | 13 | } 14 | public TreeNode convertBST(TreeNode root) { 15 | dfs(root); 16 | return root; 17 | } 18 | } -------------------------------------------------------------------------------- /Leetcode/Tree/Count_Good_Nodes_in_Binary_Tree.java: -------------------------------------------------------------------------------- 1 | public class Count_Good_Nodes_in_Binary_Tree { 2 | // Send the running max to dfs. If max before coming to curr node is more than 3 | // the node's val, then dont add it to answer. From current root, return 4 | // left subtrees good nodes + right subtree's good nodes + 0/1 depending on current node being a 5 | // good node 6 | public int dfs(TreeNode root, int maxi) 7 | { 8 | if(root==null) return 0; 9 | return (maxi>root.val?0:1) + dfs(root.left, Math.max(maxi, root.val)) + dfs(root.right, Math.max(maxi, root.val)); 10 | } 11 | public int goodNodes(TreeNode root) { 12 | return dfs(root, Integer.MIN_VALUE); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Leetcode/Tree/Count_Nodes_Equal_to_Average_of_Subtree.java: -------------------------------------------------------------------------------- 1 | public class Count_Nodes_Equal_to_Average_of_Subtree { 2 | int ans = 0; 3 | // Function returns [totalSum, totCnt] of all elements from current nodes' subtree(including itself); 4 | public int[] dfs(TreeNode root) 5 | { 6 | if(root == null) return new int[]{0,0}; 7 | int left[] = dfs(root.left); 8 | int right[] = dfs(root.right); 9 | int totSum = left[0]+right[0]+root.val; 10 | int totCnt = left[1]+right[1]+1; 11 | ans+=(totSum/totCnt==root.val)?1:0; 12 | return new int[]{totSum, totCnt}; 13 | } 14 | public int averageOfSubtree(TreeNode root) { 15 | dfs(root); 16 | return ans; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Leetcode/Tree/Delete_Leaves_With_a_Given_Value.java: -------------------------------------------------------------------------------- 1 | /* 2 | IDEA : CLASSIC POST ORDER TRAVERSAL. We know we need to process both children first 3 | Because only then we would know whether or not the current node has become a 4 | leaf node or not. 5 | 6 | */ 7 | 8 | public class Delete_Leaves_With_a_Given_Value { 9 | public TreeNode removeLeafNodes(TreeNode root, int target) { 10 | 11 | if(root == null) return null; 12 | root.left = removeLeafNodes(root.left, target); 13 | root.right = removeLeafNodes(root.right, target); 14 | 15 | if(root.left == null && root.right==null && root.val == target) 16 | return null; 17 | return root; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Leetcode/Tree/Distribute_Coins_in_Binary_Tree.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | // REFER :- https://leetcode.com/problems/distribute-coins-in-binary-tree/discuss/221939/C%2B%2B-with-picture-post-order-traversal 3 | int moves = 0; 4 | public int dfs(TreeNode root) 5 | { 6 | if(root == null) return 0; 7 | int left = distributeCoins(root.left); 8 | int right = distributeCoins(root.right); 9 | moves+=Math.abs(left) + Math.abs(right); 10 | int coinsInCurrSubTree = left + right + root.val; 11 | return coinsInCurrSubTree-1; 12 | } 13 | public int distributeCoins(TreeNode root) { 14 | dfs(root); 15 | return moves; 16 | } 17 | } -------------------------------------------------------------------------------- /Leetcode/Tree/Lowest_Common_Ancestor_of_a_Binary_Tree.java: -------------------------------------------------------------------------------- 1 | public class Lowest_Common_Ancestor_of_a_Binary_Tree { 2 | // REFER : Best explanantion at https://youtu.be/13m9ZCB8gjw 3 | public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { 4 | 5 | if(root == null) return null; 6 | if(root == p || root == q) return root; 7 | TreeNode left = lowestCommonAncestor(root.left, p, q); 8 | TreeNode right = lowestCommonAncestor(root.right, p, q); 9 | if(left!=null && right!=null) return root; 10 | if(left!=null) return left; 11 | else if(right!=null) return right; 12 | return null; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Leetcode/Tree/Range_Sum_of_BST.java: -------------------------------------------------------------------------------- 1 | package Leetcode.Tree; 2 | 3 | public class Range_Sum_of_BST { 4 | int sum; 5 | public void dfs(TreeNode root, int low, int high) 6 | { 7 | if(root==null) return; 8 | if(root.val>=low && root.val<=high) sum+=root.val; 9 | dfs(root.left, low, high); 10 | dfs(root.right, low, high); 11 | } 12 | public int rangeSumBST(TreeNode root, int low, int high) { 13 | sum = 0; 14 | dfs(root, low, high); 15 | return sum; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Leetcode/Tree/Recover_Binary_Search_Tree.java: -------------------------------------------------------------------------------- 1 | public class Recover_Binary_Search_Tree { 2 | // REFER : https://youtu.be/2uPKWpMHmZA 3 | TreeNode first, second; 4 | TreeNode prev; 5 | public void inorder(TreeNode root) 6 | { 7 | if(root == null) return; 8 | inorder(root.left); 9 | if(first == null && prev.val > root.val) 10 | first = prev; 11 | if(first!=null && prev.val > root.val) 12 | second = root; 13 | prev = root; 14 | inorder(root.right); 15 | } 16 | public void recoverTree(TreeNode root) { 17 | first = null; second = null; 18 | prev = new TreeNode(Integer.MIN_VALUE); 19 | inorder(root); 20 | int tmp = first.val; 21 | first.val = second.val; 22 | second.val = tmp; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Leetcode/Tree/Search_in_a_Binary_Search_Tree.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * public class TreeNode { 4 | * int val; 5 | * TreeNode left; 6 | * TreeNode right; 7 | * TreeNode() {} 8 | * TreeNode(int val) { this.val = val; } 9 | * TreeNode(int val, TreeNode left, TreeNode right) { 10 | * this.val = val; 11 | * this.left = left; 12 | * this.right = right; 13 | * } 14 | * } 15 | */ 16 | class Solution { 17 | public TreeNode searchBST(TreeNode root, int val) { 18 | if(root.val == val) return root; 19 | if(root.left!=null && valroot.val) return searchBST(root.right,val); 21 | return null; 22 | } 23 | } -------------------------------------------------------------------------------- /Leetcode/Tree/Sum_Root_to_Leaf_Numbers.java: -------------------------------------------------------------------------------- 1 | package Leetcode.Tree; 2 | 3 | public class Sum_Root_to_Leaf_Numbers { 4 | int sum = 0; 5 | public void dfs(TreeNode root, int num) 6 | { 7 | if(root == null) return; 8 | if(root.left == null && root.right == null) 9 | { 10 | sum+=(num*10+root.val); 11 | return; 12 | } 13 | dfs(root.left, num*10+root.val); 14 | dfs(root.right, num*10+root.val); 15 | } 16 | public int sumNumbers(TreeNode root) { 17 | dfs(root, 0); 18 | return sum; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Leetcode/Tree/Sum_of_Nodes_with_Even_Valued_Grandparent.java: -------------------------------------------------------------------------------- 1 | package Leetcode.Tree; 2 | 3 | public class Sum_of_Nodes_with_Even_Valued_Grandparent { 4 | static int count; 5 | public void DFS(TreeNode root, TreeNode p, TreeNode gp) 6 | { 7 | if(root==null) return; 8 | if(gp!=null && gp.val%2==0) 9 | { 10 | count+=root.val; 11 | } 12 | DFS(root.left, root, p); 13 | DFS(root.right, root ,p); 14 | 15 | 16 | } 17 | public int sumEvenGrandparent(TreeNode root) { 18 | count = 0; 19 | DFS(root, null, null); 20 | return count; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Platforms/Atcoder/Atcoder DP/input.txt: -------------------------------------------------------------------------------- 1 | abracadabra 2 | avadakedavra 3 | -------------------------------------------------------------------------------- /Platforms/Atcoder/Atcoder DP/javarun.py: -------------------------------------------------------------------------------- 1 | import os 2 | import time 3 | s = "F" 4 | prog_name = f"{s}.java" 5 | os.system(f'javac -d Classes {prog_name}') 6 | t1 = time.time() 7 | os.system(f'java -cp Classes {s} local') 8 | t2 = time.time() 9 | print("TIME TAKEN: ",t2-t1) -------------------------------------------------------------------------------- /Platforms/Atcoder/Atcoder DP/output.txt: -------------------------------------------------------------------------------- 1 | a -------------------------------------------------------------------------------- /Platforms/Atcoder/Contests/abc210/A.py: -------------------------------------------------------------------------------- 1 | n,a,x,y = map(int,input().split()) 2 | a = min(n,a) 3 | n-=a 4 | print(a*x + n*y) 5 | -------------------------------------------------------------------------------- /Platforms/Atcoder/Contests/abc210/B.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | s = input() 3 | for i in range(len(s)): 4 | if s[i]=='1': 5 | print('Takahashi') if i%2==0 else print("Aoki") 6 | break -------------------------------------------------------------------------------- /Platforms/Atcoder/Contests/abc210/C.py: -------------------------------------------------------------------------------- 1 | n,k = map(int,input().split()) 2 | can = list(map(int,input().split())) 3 | d = {} 4 | for i in can[:k]: 5 | if i not in d: 6 | d[i] = 1 7 | else: 8 | d[i]+=1 9 | 10 | maxlen = len(d) 11 | for i in range(1, n-k+1): 12 | d[can[i-1]]-=1 13 | if d[can[i-1]]==0: 14 | del d[can[i-1]] 15 | if can[i+k-1] not in d: 16 | d[can[i+k-1]] = 1 17 | else: 18 | d[can[i+k-1]]+=1 19 | maxlen = max(maxlen, len(d)) 20 | print(maxlen) -------------------------------------------------------------------------------- /Platforms/Atcoder/Contests/abc211/A.py: -------------------------------------------------------------------------------- 1 | 2 | a,b = map(int,input().split()) 3 | 4 | print((a-b)/3 + b) -------------------------------------------------------------------------------- /Platforms/Atcoder/Contests/abc211/B.py: -------------------------------------------------------------------------------- 1 | s = set(['H', '2B', '3B','HR']) 2 | s1,s2,s3,s4 = [input() for i in range(4)] 3 | 4 | s.discard(s1) 5 | s.discard(s2) 6 | s.discard(s3) 7 | s.discard(s4) 8 | if len(s)==0: 9 | print('Yes') 10 | else: 11 | print('No') -------------------------------------------------------------------------------- /Platforms/Atcoder/Contests/abc211/C.py: -------------------------------------------------------------------------------- 1 | s = 'chokudai' 2 | n = len(s) 3 | inp = input() 4 | -------------------------------------------------------------------------------- /Platforms/Atcoder/Contests/abc214/A.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | if n<=125: 3 | print(4) 4 | elif n<=211: 5 | print(6) 6 | else: 7 | print(8) 8 | -------------------------------------------------------------------------------- /Platforms/Atcoder/Contests/abc214/B.py: -------------------------------------------------------------------------------- 1 | s,t = map(int,input().split()) 2 | cnt = 0 3 | for a in range(101): 4 | for b in range(101): 5 | for c in range(101): 6 | if(a+b+c)<=s and a*b*c<=t: 7 | cnt+=1 8 | print(cnt) 9 | -------------------------------------------------------------------------------- /Platforms/Atcoder/Contests/abc214/C.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | s = list(map(int,input().split())) 3 | t = list(map(int,input().split())) 4 | 5 | 6 | 7 | ans = [t[i] for i in range(n)] 8 | 9 | for i in range(2*n): 10 | ans[(i+1)%n] = min(ans[(i+1)%n], ans[i%n] + s[i%n]) 11 | #print(club) 12 | 13 | print(*ans, sep = '\n') -------------------------------------------------------------------------------- /Platforms/CSES/Dynamic Programming/Classes/Coin_Change.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RohanTrix/Competitive-Practice/b14aae7dbd778d8cdf743c77a1f8d25b3048937e/Platforms/CSES/Dynamic Programming/Classes/Coin_Change.class -------------------------------------------------------------------------------- /Platforms/CSES/Dynamic Programming/Classes/Dice_Combinations.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RohanTrix/Competitive-Practice/b14aae7dbd778d8cdf743c77a1f8d25b3048937e/Platforms/CSES/Dynamic Programming/Classes/Dice_Combinations.class -------------------------------------------------------------------------------- /Platforms/CSES/Dynamic Programming/Classes/Main$FastReader.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RohanTrix/Competitive-Practice/b14aae7dbd778d8cdf743c77a1f8d25b3048937e/Platforms/CSES/Dynamic Programming/Classes/Main$FastReader.class -------------------------------------------------------------------------------- /Platforms/CSES/Dynamic Programming/Classes/Main$pair.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RohanTrix/Competitive-Practice/b14aae7dbd778d8cdf743c77a1f8d25b3048937e/Platforms/CSES/Dynamic Programming/Classes/Main$pair.class -------------------------------------------------------------------------------- /Platforms/CSES/Dynamic Programming/Classes/Main.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RohanTrix/Competitive-Practice/b14aae7dbd778d8cdf743c77a1f8d25b3048937e/Platforms/CSES/Dynamic Programming/Classes/Main.class -------------------------------------------------------------------------------- /Platforms/CSES/Dynamic Programming/input.txt: -------------------------------------------------------------------------------- 1 | 3 9 2 | 2 3 5 -------------------------------------------------------------------------------- /Platforms/CSES/Dynamic Programming/javarun.py: -------------------------------------------------------------------------------- 1 | import os 2 | import time 3 | prog_name = "Main.java" 4 | os.system(f'javac -d Classes {prog_name}') 5 | t1 = time.time() 6 | os.system(f'java -cp Classes Main local') 7 | t2 = time.time() 8 | print("TIME TAKEN: ",t2-t1) 9 | -------------------------------------------------------------------------------- /Platforms/CSES/Dynamic Programming/output.txt: -------------------------------------------------------------------------------- 1 | 8 2 | -------------------------------------------------------------------------------- /Platforms/CSES/Graphs Algorithms/Classes/Main$FastReader.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RohanTrix/Competitive-Practice/b14aae7dbd778d8cdf743c77a1f8d25b3048937e/Platforms/CSES/Graphs Algorithms/Classes/Main$FastReader.class -------------------------------------------------------------------------------- /Platforms/CSES/Graphs Algorithms/Classes/Main$MinIndexedDHeap.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RohanTrix/Competitive-Practice/b14aae7dbd778d8cdf743c77a1f8d25b3048937e/Platforms/CSES/Graphs Algorithms/Classes/Main$MinIndexedDHeap.class -------------------------------------------------------------------------------- /Platforms/CSES/Graphs Algorithms/Classes/Main$pair.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RohanTrix/Competitive-Practice/b14aae7dbd778d8cdf743c77a1f8d25b3048937e/Platforms/CSES/Graphs Algorithms/Classes/Main$pair.class -------------------------------------------------------------------------------- /Platforms/CSES/Graphs Algorithms/Classes/Main.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RohanTrix/Competitive-Practice/b14aae7dbd778d8cdf743c77a1f8d25b3048937e/Platforms/CSES/Graphs Algorithms/Classes/Main.class -------------------------------------------------------------------------------- /Platforms/CSES/Graphs Algorithms/README.md: -------------------------------------------------------------------------------- 1 | 1. [Counting Rooms](http://p.ip.fi/SrLZ) 2 | 2. [Labyrinth](http://p.ip.fi/tYFL) 3 | 3. [Building Roads](http://p.ip.fi/1Mev) 4 | 4. [Message Route](http://p.ip.fi/_kVA) 5 | 5. [Building Teams](http://p.ip.fi/dgDB) 6 | 6. [Round Trip](http://p.ip.fi/2kjk) 7 | 7. [Monsters](http://p.ip.fi/K8HZ) -------------------------------------------------------------------------------- /Platforms/CSES/Graphs Algorithms/input.txt: -------------------------------------------------------------------------------- 1 | 3 4 2 | 1 2 6 3 | 1 3 2 4 | 3 2 3 5 | 1 3 4 -------------------------------------------------------------------------------- /Platforms/CSES/Graphs Algorithms/javarun.py: -------------------------------------------------------------------------------- 1 | import os 2 | import time 3 | prog_name = "Main.java" 4 | os.system(f'javac -d Classes {prog_name}') 5 | t1 = time.time() 6 | os.system(f'java -cp Classes Main local') 7 | t2 = time.time() 8 | print("TIME TAKEN: ",t2-t1) 9 | -------------------------------------------------------------------------------- /Platforms/CSES/Graphs Algorithms/output.txt: -------------------------------------------------------------------------------- 1 | 0 4611686018427387903 4611686018427387903 -------------------------------------------------------------------------------- /Platforms/Codechef/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RohanTrix/Competitive-Practice/b14aae7dbd778d8cdf743c77a1f8d25b3048937e/Platforms/Codechef/.gitkeep -------------------------------------------------------------------------------- /Platforms/Codechef/ADADISH.py: -------------------------------------------------------------------------------- 1 | for t in range(int(input())): 2 | n = int(input()) 3 | l = list(map(int,input().split())) 4 | l.sort(reverse=True) 5 | s =l[0] 6 | rem = l[0] 7 | if len(l)==1: 8 | print(l[0]) 9 | continue 10 | for i in range(1,n): 11 | if rem>=l[i]: 12 | rem = rem - l[i] 13 | else: 14 | rem = l[i]-rem 15 | s = s+rem 16 | #print("Sum = ", s) 17 | #print("rem = ",rem) 18 | print(s) 19 | -------------------------------------------------------------------------------- /Platforms/Codechef/ANDOR.py: -------------------------------------------------------------------------------- 1 | for t in range(int(input())): 2 | x = int(input()) 3 | print(0,x) -------------------------------------------------------------------------------- /Platforms/Codechef/BINSUBS.py: -------------------------------------------------------------------------------- 1 | for t in range(int(input())): 2 | n = input() 3 | s = input() 4 | zeros =0 5 | ones=0 6 | cnt=0 7 | s1 = 0 8 | for i in s: 9 | if i=='1': 10 | cnt+=1 11 | if i=='0' and cnt>0: 12 | s1+=1 13 | cnt-=1 14 | print(s1) -------------------------------------------------------------------------------- /Platforms/Codechef/CHEFEZQ.py: -------------------------------------------------------------------------------- 1 | import sys 2 | sys.stdin = open('input.txt', 'r') 3 | sys.stdout = open('output.txt', 'w+') 4 | 5 | for _ in range(int(input())): 6 | n,k = map(int,input().split()) 7 | l = list(map(int,input().split())) 8 | s = 0 9 | res = -1 10 | for i in range(n): 11 | s+=l[i] 12 | if (i+1)*k > s: 13 | res = i+1 14 | break 15 | if res==-1: 16 | sto = s - (n*k) 17 | print((sto//k)+1 +n) 18 | else: 19 | print(res) 20 | -------------------------------------------------------------------------------- /Platforms/Codechef/CVDRUN.py: -------------------------------------------------------------------------------- 1 | for _ in range(int(input())): 2 | n,k,x,y = map(int,input().split()) 3 | s = {x} 4 | f= True 5 | while((x+k)%n!=y): 6 | if (x+k)%n in s: 7 | f= False 8 | break 9 | x = (x+k)%n 10 | s.add(x) 11 | if f: 12 | print('YES') 13 | else: 14 | print('NO') 15 | -------------------------------------------------------------------------------- /Platforms/Codechef/Chef_and_IPC_Certificates.py: -------------------------------------------------------------------------------- 1 | import sys 2 | sys.stdin = open('input.txt', 'r') 3 | sys.stdout = open('output.txt', 'w') 4 | n, max_lect, k = map(int,input().split()) 5 | cnt=0 6 | for i in range(n): 7 | l = list(map(int,input().split())) 8 | if sum(l[:-1])>=max_lect and l[-1]<=10: 9 | cnt+=1 10 | print(cnt) -------------------------------------------------------------------------------- /Platforms/Codechef/DECREM.py: -------------------------------------------------------------------------------- 1 | import sys 2 | sys.stdin = open('input.txt', 'r') 3 | sys.stdout = open('output.txt', 'w+') 4 | for t in range(int(input())): 5 | l,r = map(int, input().split()) 6 | if 2*l <=r: 7 | print(-1) 8 | elif l==1: 9 | print(-1) 10 | else: 11 | print(r) -------------------------------------------------------------------------------- /Platforms/Codechef/EVENPSUM.py: -------------------------------------------------------------------------------- 1 | import sys 2 | sys.stdin = open('input.txt', 'r') 3 | sys.stdout = open('output.txt', 'w+') 4 | t = int(input()) 5 | for _ in range(t): 6 | x,y = map(int,input().split()) 7 | x_even = x//2 8 | y_even = y//2 9 | if x%2==0: 10 | x_odd = x_even 11 | else: 12 | x_odd = x_even+1 13 | if y%2==0: 14 | y_odd = y_even 15 | else: 16 | y_odd = y_even+1 17 | #print(x_odd, y_odd, x_odd, x_even) 18 | ans1 = y_even*x_even 19 | ans2 = y_odd*x_odd 20 | print(ans1+ans2) 21 | -------------------------------------------------------------------------------- /Platforms/Codechef/Flip_the_String.py: -------------------------------------------------------------------------------- 1 | for _ in range(int(input())): 2 | a = list(input()) 3 | b = list(input()) 4 | moves=0 5 | cnt=0 6 | i=0 7 | while(i=P: 12 | print(D1-1) 13 | else: 14 | while(amt=P: 23 | print(D2-1) 24 | else: 25 | while(amt9 and i<80: 11 | nrisk+=1 12 | else: 13 | risk+=1 14 | num = risk//d + (nrisk//d) 15 | if risk%d!=0: 16 | num+=1 17 | if nrisk%d!=0: 18 | num+=1 19 | print(num) -------------------------------------------------------------------------------- /Platforms/Codechef/input.txt: -------------------------------------------------------------------------------- 1 | 1 2 | 3 3 | 3 4 | 1 2 3 -------------------------------------------------------------------------------- /Platforms/Codechef/javarun.py: -------------------------------------------------------------------------------- 1 | import os 2 | import time 3 | prog_name = "*.java" 4 | os.system(f'javac -d Java-Classes {prog_name}') 5 | t1 = time.time() 6 | os.system(f'java -cp Java-Classes ANITGUY2 local') 7 | t2 = time.time() 8 | print("TIME TAKEN: ",t2-t1) -------------------------------------------------------------------------------- /Platforms/Codechef/output.txt: -------------------------------------------------------------------------------- 1 | 3 2 | -------------------------------------------------------------------------------- /Platforms/Codeforces/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RohanTrix/Competitive-Practice/b14aae7dbd778d8cdf743c77a1f8d25b3048937e/Platforms/Codeforces/.gitkeep -------------------------------------------------------------------------------- /Platforms/Codeforces/Avoid_Trygub.py: -------------------------------------------------------------------------------- 1 | for t in range(int(input())): 2 | n = int(input()) 3 | s = input() 4 | ss = "trygub" 5 | k =0 6 | cnt=0 7 | l=[] 8 | for i in range(n): 9 | if s[i]=='b': 10 | cnt+=1 11 | for i in range(cnt): 12 | print('b',end='') 13 | for i in range(n): 14 | if s[i]=='b': 15 | continue 16 | print(s[i], end='') 17 | print() 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /Platforms/Codeforces/Buy_the_String.py: -------------------------------------------------------------------------------- 1 | for _ in " "*int(input()): 2 | n,c1,c2,h = map(int, input().split()) 3 | s = input() 4 | cnt1, cnt2 =0, 0 5 | for i in s: 6 | if i=='0': 7 | cnt1+=1 8 | else: 9 | cnt2+=1 10 | 11 | if c1+h 0 and i< n-1 and (l[i+1] < m or l[i-1]< m)): 18 | print(i+1) 19 | return 20 | print(-1) 21 | 22 | 23 | for t in range(int(input())): 24 | solve() 25 | -------------------------------------------------------------------------------- /Platforms/Codeforces/Domino_piling.py: -------------------------------------------------------------------------------- 1 | m,n = map(int, input().split()) 2 | print(m*n//2) -------------------------------------------------------------------------------- /Platforms/Codeforces/Find_The_Array.py: -------------------------------------------------------------------------------- 1 | import math 2 | for t in range(int(input())): 3 | n = int(input()) 4 | a = list(map(int,input().split())) 5 | b = [] 6 | for i in a: 7 | k = 2**int(math.log2(i)) 8 | if i-k<(2*k)-i or 2*k>10**9: 9 | b.append(k) 10 | else: 11 | b.append(2*k) 12 | print(*b, sep = ' ') -------------------------------------------------------------------------------- /Platforms/Codeforces/Maximum_GCD.py: -------------------------------------------------------------------------------- 1 | 2 | 3 | def main(): 4 | n = int(input()) 5 | print(n//2) 6 | 7 | t = int(input()) 8 | for _ in range(t): 9 | main() -------------------------------------------------------------------------------- /Platforms/Codeforces/Non_Substring_Subsequence.py: -------------------------------------------------------------------------------- 1 | import sys 2 | sys.stdin = open('input.txt', 'r') 3 | sys.stdout = open('output.txt', 'w') 4 | for _ in range(int(input())): 5 | n, q = map(int,input().split()) 6 | s = input() 7 | 8 | for i in range(q): 9 | i, j = map(int,input().split()) 10 | i = i-1 11 | j = j-1 12 | left = s[i] 13 | right = s[j] 14 | a=b=0 15 | f = "NO" 16 | i -=1 17 | j+=1 18 | while(i>=0 or j=0 and s[i]==left: 20 | f="YES" 21 | break 22 | if j=bridge: 21 | #print(bridge, n) 22 | ans+=int(str(n)[0]) 23 | else: 24 | ans+=int(str(n)[0])-1 25 | print(ans) 26 | 27 | 28 | for t in range(int(input())): 29 | main() -------------------------------------------------------------------------------- /Platforms/Codeforces/Pleasant Pairs.py: -------------------------------------------------------------------------------- 1 | import math 2 | # import sys 3 | # sys.stdin = open('input.txt', 'r') 4 | # sys.stdout = open('output.txt', 'w+') 5 | 6 | # Check Youtube for explanation 7 | 8 | for t in range(int(input())): 9 | n = int(input()) 10 | arr = [(el, i+1) for i,el in enumerate(list(map(int,input().split())))] 11 | arr.sort() 12 | cnt, limit = 0, 2*n 13 | 14 | limReached = False 15 | for i in range(n-1): 16 | for j in range(i+1, n): 17 | mul = arr[i][0]*arr[j][0] 18 | if(mul>limit): 19 | 20 | break 21 | if(mul==arr[i][1]+arr[j][1]): 22 | #print(arr[i], arr[j]) 23 | cnt+=1 24 | 25 | print(cnt) 26 | 27 | -------------------------------------------------------------------------------- /Platforms/Codeforces/Same Differences.py: -------------------------------------------------------------------------------- 1 | import math 2 | from collections import defaultdict 3 | # import sys 4 | # sys.stdin = open('input.txt', 'r') 5 | # sys.stdout = open('output.txt', 'w+') 6 | 7 | 8 | for t in range(int(input())): 9 | d = defaultdict(int) 10 | n = int(input()) 11 | arr = list(map(int,input().split())) 12 | cnt = 0 13 | for i in range(n): 14 | d[arr[i]-(i+1)]+=1 15 | for val in d.values(): 16 | if val>1: 17 | cnt+= val*(val-1)//2 18 | # print(*d.values()) 19 | print(cnt) -------------------------------------------------------------------------------- /Platforms/Codeforces/Shifting Stacks.py: -------------------------------------------------------------------------------- 1 | import sys 2 | sys.stdin = open('input.txt', 'r') 3 | sys.stdout = open('output.txt', 'w') 4 | 5 | for t in range(int(input())): 6 | n = int(input()) 7 | l = list(map(int,input().split())) 8 | f = True 9 | if len(l)==1: 10 | print("YES") 11 | continue 12 | psum = [] 13 | s = 0 14 | for i in range(len(l)): 15 | psum.append(i+s) 16 | s = s+i 17 | currSum = 0 18 | for i in range(len(l)): 19 | currSum+=l[i] 20 | if currSum 2 | #include 3 | 4 | using std::vector; 5 | using std::pair; 6 | 7 | 8 | int reach(vector > &adj, int x, int y) { 9 | //write your code here 10 | return 0; 11 | } 12 | 13 | int main() { 14 | size_t n, m; 15 | std::cin >> n >> m; 16 | vector > adj(n, vector()); 17 | for (size_t i = 0; i < m; i++) { 18 | int x, y; 19 | std::cin >> x >> y; 20 | adj[x - 1].push_back(y - 1); 21 | adj[y - 1].push_back(x - 1); 22 | } 23 | int x, y; 24 | std::cin >> x >> y; 25 | std::cout << reach(adj, x - 1, y - 1); 26 | } 27 | -------------------------------------------------------------------------------- /Platforms/EXTRA/Coursera-Algorithms-On-Graphs/week1_graph_decomposition1/1_finding_exit_from_maze/reachability.py: -------------------------------------------------------------------------------- 1 | #Uses python3 2 | 3 | import sys 4 | 5 | def reach(adj, x, y): 6 | #write your code here 7 | return 0 8 | 9 | if __name__ == '__main__': 10 | input = sys.stdin.read() 11 | data = list(map(int, input.split())) 12 | n, m = data[0:2] 13 | data = data[2:] 14 | edges = list(zip(data[0:(2 * m):2], data[1:(2 * m):2])) 15 | x, y = data[2 * m:] 16 | adj = [[] for _ in range(n)] 17 | x, y = x - 1, y - 1 18 | for (a, b) in edges: 19 | adj[a - 1].append(b - 1) 20 | adj[b - 1].append(a - 1) 21 | print(reach(adj, x, y)) 22 | -------------------------------------------------------------------------------- /Platforms/EXTRA/Coursera-Algorithms-On-Graphs/week1_graph_decomposition1/2_adding_exits_to_maze/ConnectedComponents.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RohanTrix/Competitive-Practice/b14aae7dbd778d8cdf743c77a1f8d25b3048937e/Platforms/EXTRA/Coursera-Algorithms-On-Graphs/week1_graph_decomposition1/2_adding_exits_to_maze/ConnectedComponents.class -------------------------------------------------------------------------------- /Platforms/EXTRA/Coursera-Algorithms-On-Graphs/week1_graph_decomposition1/2_adding_exits_to_maze/connected_components.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using std::vector; 5 | using std::pair; 6 | 7 | int number_of_components(vector > &adj) { 8 | int res = 0; 9 | //write your code here 10 | return res; 11 | } 12 | 13 | int main() { 14 | size_t n, m; 15 | std::cin >> n >> m; 16 | vector > adj(n, vector()); 17 | for (size_t i = 0; i < m; i++) { 18 | int x, y; 19 | std::cin >> x >> y; 20 | adj[x - 1].push_back(y - 1); 21 | adj[y - 1].push_back(x - 1); 22 | } 23 | std::cout << number_of_components(adj); 24 | } 25 | -------------------------------------------------------------------------------- /Platforms/EXTRA/Coursera-Algorithms-On-Graphs/week1_graph_decomposition1/2_adding_exits_to_maze/connected_components.py: -------------------------------------------------------------------------------- 1 | #Uses python3 2 | 3 | import sys 4 | 5 | 6 | def number_of_components(adj): 7 | result = 0 8 | #write your code here 9 | return result 10 | 11 | if __name__ == '__main__': 12 | input = sys.stdin.read() 13 | data = list(map(int, input.split())) 14 | n, m = data[0:2] 15 | data = data[2:] 16 | edges = list(zip(data[0:(2 * m):2], data[1:(2 * m):2])) 17 | adj = [[] for _ in range(n)] 18 | for (a, b) in edges: 19 | adj[a - 1].append(b - 1) 20 | adj[b - 1].append(a - 1) 21 | print(number_of_components(adj)) 22 | -------------------------------------------------------------------------------- /Platforms/EXTRA/Coursera-Algorithms-On-Graphs/week1_graph_decomposition1/debug.log: -------------------------------------------------------------------------------- 1 | [1104/123102.978:ERROR:directory_reader_win.cc(43)] FindFirstFile: The system cannot find the path specified. (0x3) 2 | -------------------------------------------------------------------------------- /Platforms/EXTRA/Coursera-Algorithms-On-Graphs/week1_graph_decomposition1/week1_graph_decomposition1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RohanTrix/Competitive-Practice/b14aae7dbd778d8cdf743c77a1f8d25b3048937e/Platforms/EXTRA/Coursera-Algorithms-On-Graphs/week1_graph_decomposition1/week1_graph_decomposition1.pdf -------------------------------------------------------------------------------- /Platforms/EXTRA/Coursera-Algorithms-On-Graphs/week2_graph_decomposition2/1_cs_curriculum/Acyclicity.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RohanTrix/Competitive-Practice/b14aae7dbd778d8cdf743c77a1f8d25b3048937e/Platforms/EXTRA/Coursera-Algorithms-On-Graphs/week2_graph_decomposition2/1_cs_curriculum/Acyclicity.class -------------------------------------------------------------------------------- /Platforms/EXTRA/Coursera-Algorithms-On-Graphs/week2_graph_decomposition2/1_cs_curriculum/acyclicity.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using std::vector; 5 | using std::pair; 6 | 7 | int acyclic(vector > &adj) { 8 | //write your code here 9 | return 0; 10 | } 11 | 12 | int main() { 13 | size_t n, m; 14 | std::cin >> n >> m; 15 | vector > adj(n, vector()); 16 | for (size_t i = 0; i < m; i++) { 17 | int x, y; 18 | std::cin >> x >> y; 19 | adj[x - 1].push_back(y - 1); 20 | } 21 | std::cout << acyclic(adj); 22 | } 23 | -------------------------------------------------------------------------------- /Platforms/EXTRA/Coursera-Algorithms-On-Graphs/week2_graph_decomposition2/1_cs_curriculum/acyclicity.py: -------------------------------------------------------------------------------- 1 | #Uses python3 2 | 3 | import sys 4 | 5 | 6 | def acyclic(adj): 7 | return 0 8 | 9 | if __name__ == '__main__': 10 | input = sys.stdin.read() 11 | data = list(map(int, input.split())) 12 | n, m = data[0:2] 13 | data = data[2:] 14 | edges = list(zip(data[0:(2 * m):2], data[1:(2 * m):2])) 15 | adj = [[] for _ in range(n)] 16 | for (a, b) in edges: 17 | adj[a - 1].append(b - 1) 18 | print(acyclic(adj)) 19 | -------------------------------------------------------------------------------- /Platforms/EXTRA/Coursera-Algorithms-On-Graphs/week2_graph_decomposition2/2_order_of_courses/Toposort$FastReader.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RohanTrix/Competitive-Practice/b14aae7dbd778d8cdf743c77a1f8d25b3048937e/Platforms/EXTRA/Coursera-Algorithms-On-Graphs/week2_graph_decomposition2/2_order_of_courses/Toposort$FastReader.class -------------------------------------------------------------------------------- /Platforms/EXTRA/Coursera-Algorithms-On-Graphs/week2_graph_decomposition2/2_order_of_courses/Toposort.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RohanTrix/Competitive-Practice/b14aae7dbd778d8cdf743c77a1f8d25b3048937e/Platforms/EXTRA/Coursera-Algorithms-On-Graphs/week2_graph_decomposition2/2_order_of_courses/Toposort.class -------------------------------------------------------------------------------- /Platforms/EXTRA/Coursera-Algorithms-On-Graphs/week2_graph_decomposition2/2_order_of_courses/toposort.py: -------------------------------------------------------------------------------- 1 | #Uses python3 2 | 3 | import sys 4 | 5 | def dfs(adj, used, order, x): 6 | #write your code here 7 | pass 8 | 9 | 10 | def toposort(adj): 11 | used = [0] * len(adj) 12 | order = [] 13 | #write your code here 14 | return order 15 | 16 | if __name__ == '__main__': 17 | input = sys.stdin.read() 18 | data = list(map(int, input.split())) 19 | n, m = data[0:2] 20 | data = data[2:] 21 | edges = list(zip(data[0:(2 * m):2], data[1:(2 * m):2])) 22 | adj = [[] for _ in range(n)] 23 | for (a, b) in edges: 24 | adj[a - 1].append(b - 1) 25 | order = toposort(adj) 26 | for x in order: 27 | print(x + 1, end=' ') 28 | 29 | -------------------------------------------------------------------------------- /Platforms/EXTRA/Coursera-Algorithms-On-Graphs/week2_graph_decomposition2/3_intersection_reachability/StronglyConnected$FastReader.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RohanTrix/Competitive-Practice/b14aae7dbd778d8cdf743c77a1f8d25b3048937e/Platforms/EXTRA/Coursera-Algorithms-On-Graphs/week2_graph_decomposition2/3_intersection_reachability/StronglyConnected$FastReader.class -------------------------------------------------------------------------------- /Platforms/EXTRA/Coursera-Algorithms-On-Graphs/week2_graph_decomposition2/3_intersection_reachability/StronglyConnected.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RohanTrix/Competitive-Practice/b14aae7dbd778d8cdf743c77a1f8d25b3048937e/Platforms/EXTRA/Coursera-Algorithms-On-Graphs/week2_graph_decomposition2/3_intersection_reachability/StronglyConnected.class -------------------------------------------------------------------------------- /Platforms/EXTRA/Coursera-Algorithms-On-Graphs/week2_graph_decomposition2/3_intersection_reachability/strongly_connected.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using std::vector; 6 | using std::pair; 7 | 8 | int number_of_strongly_connected_components(vector > adj) { 9 | int result = 0; 10 | //write your code here 11 | return result; 12 | } 13 | 14 | int main() { 15 | size_t n, m; 16 | std::cin >> n >> m; 17 | vector > adj(n, vector()); 18 | for (size_t i = 0; i < m; i++) { 19 | int x, y; 20 | std::cin >> x >> y; 21 | adj[x - 1].push_back(y - 1); 22 | } 23 | std::cout << number_of_strongly_connected_components(adj); 24 | } 25 | -------------------------------------------------------------------------------- /Platforms/EXTRA/Coursera-Algorithms-On-Graphs/week2_graph_decomposition2/3_intersection_reachability/strongly_connected.py: -------------------------------------------------------------------------------- 1 | #Uses python3 2 | 3 | import sys 4 | 5 | sys.setrecursionlimit(200000) 6 | 7 | 8 | def number_of_strongly_connected_components(adj): 9 | result = 0 10 | #write your code here 11 | return result 12 | 13 | if __name__ == '__main__': 14 | input = sys.stdin.read() 15 | data = list(map(int, input.split())) 16 | n, m = data[0:2] 17 | data = data[2:] 18 | edges = list(zip(data[0:(2 * m):2], data[1:(2 * m):2])) 19 | adj = [[] for _ in range(n)] 20 | for (a, b) in edges: 21 | adj[a - 1].append(b - 1) 22 | print(number_of_strongly_connected_components(adj)) 23 | -------------------------------------------------------------------------------- /Platforms/EXTRA/Coursera-Algorithms-On-Graphs/week2_graph_decomposition2/debug.log: -------------------------------------------------------------------------------- 1 | [1111/201509.793:ERROR:directory_reader_win.cc(43)] FindFirstFile: The system cannot find the path specified. (0x3) 2 | -------------------------------------------------------------------------------- /Platforms/EXTRA/Coursera-Algorithms-On-Graphs/week2_graph_decomposition2/week2_graph_decomposition2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RohanTrix/Competitive-Practice/b14aae7dbd778d8cdf743c77a1f8d25b3048937e/Platforms/EXTRA/Coursera-Algorithms-On-Graphs/week2_graph_decomposition2/week2_graph_decomposition2.pdf -------------------------------------------------------------------------------- /Platforms/EXTRA/Coursera-Algorithms-On-Graphs/week3_paths_in_graphs1/1_flight_segments/BFS$FastReader.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RohanTrix/Competitive-Practice/b14aae7dbd778d8cdf743c77a1f8d25b3048937e/Platforms/EXTRA/Coursera-Algorithms-On-Graphs/week3_paths_in_graphs1/1_flight_segments/BFS$FastReader.class -------------------------------------------------------------------------------- /Platforms/EXTRA/Coursera-Algorithms-On-Graphs/week3_paths_in_graphs1/1_flight_segments/BFS.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RohanTrix/Competitive-Practice/b14aae7dbd778d8cdf743c77a1f8d25b3048937e/Platforms/EXTRA/Coursera-Algorithms-On-Graphs/week3_paths_in_graphs1/1_flight_segments/BFS.class -------------------------------------------------------------------------------- /Platforms/EXTRA/Coursera-Algorithms-On-Graphs/week3_paths_in_graphs1/1_flight_segments/bfs.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using std::vector; 6 | using std::queue; 7 | 8 | int distance(vector > &adj, int s, int t) { 9 | //write your code here 10 | return -1; 11 | } 12 | 13 | int main() { 14 | int n, m; 15 | std::cin >> n >> m; 16 | vector > adj(n, vector()); 17 | for (int i = 0; i < m; i++) { 18 | int x, y; 19 | std::cin >> x >> y; 20 | adj[x - 1].push_back(y - 1); 21 | adj[y - 1].push_back(x - 1); 22 | } 23 | int s, t; 24 | std::cin >> s >> t; 25 | s--, t--; 26 | std::cout << distance(adj, s, t); 27 | } 28 | -------------------------------------------------------------------------------- /Platforms/EXTRA/Coursera-Algorithms-On-Graphs/week3_paths_in_graphs1/2_bipartite/Bipartite$FastReader.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RohanTrix/Competitive-Practice/b14aae7dbd778d8cdf743c77a1f8d25b3048937e/Platforms/EXTRA/Coursera-Algorithms-On-Graphs/week3_paths_in_graphs1/2_bipartite/Bipartite$FastReader.class -------------------------------------------------------------------------------- /Platforms/EXTRA/Coursera-Algorithms-On-Graphs/week3_paths_in_graphs1/2_bipartite/Bipartite.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RohanTrix/Competitive-Practice/b14aae7dbd778d8cdf743c77a1f8d25b3048937e/Platforms/EXTRA/Coursera-Algorithms-On-Graphs/week3_paths_in_graphs1/2_bipartite/Bipartite.class -------------------------------------------------------------------------------- /Platforms/EXTRA/Coursera-Algorithms-On-Graphs/week3_paths_in_graphs1/2_bipartite/bipartite.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using std::vector; 6 | using std::queue; 7 | 8 | int bipartite(vector > &adj) { 9 | //write your code here 10 | return -1; 11 | } 12 | 13 | int main() { 14 | int n, m; 15 | std::cin >> n >> m; 16 | vector > adj(n, vector()); 17 | for (int i = 0; i < m; i++) { 18 | int x, y; 19 | std::cin >> x >> y; 20 | adj[x - 1].push_back(y - 1); 21 | adj[y - 1].push_back(x - 1); 22 | } 23 | std::cout << bipartite(adj); 24 | } 25 | -------------------------------------------------------------------------------- /Platforms/EXTRA/Coursera-Algorithms-On-Graphs/week3_paths_in_graphs1/2_bipartite/bipartite.py: -------------------------------------------------------------------------------- 1 | #Uses python3 2 | 3 | import sys 4 | import queue 5 | 6 | def bipartite(adj): 7 | #write your code here 8 | return -1 9 | 10 | if __name__ == '__main__': 11 | input = sys.stdin.read() 12 | data = list(map(int, input.split())) 13 | n, m = data[0:2] 14 | data = data[2:] 15 | edges = list(zip(data[0:(2 * m):2], data[1:(2 * m):2])) 16 | adj = [[] for _ in range(n)] 17 | for (a, b) in edges: 18 | adj[a - 1].append(b - 1) 19 | adj[b - 1].append(a - 1) 20 | print(bipartite(adj)) 21 | -------------------------------------------------------------------------------- /Platforms/EXTRA/Coursera-Algorithms-On-Graphs/week3_paths_in_graphs1/week3_paths_in_graphs1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RohanTrix/Competitive-Practice/b14aae7dbd778d8cdf743c77a1f8d25b3048937e/Platforms/EXTRA/Coursera-Algorithms-On-Graphs/week3_paths_in_graphs1/week3_paths_in_graphs1.pdf -------------------------------------------------------------------------------- /Platforms/EXTRA/Coursera-Algorithms-On-Graphs/week4_paths_in_graphs2/1_minimum_flight_cost/Dijkstra$pair.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RohanTrix/Competitive-Practice/b14aae7dbd778d8cdf743c77a1f8d25b3048937e/Platforms/EXTRA/Coursera-Algorithms-On-Graphs/week4_paths_in_graphs2/1_minimum_flight_cost/Dijkstra$pair.class -------------------------------------------------------------------------------- /Platforms/EXTRA/Coursera-Algorithms-On-Graphs/week4_paths_in_graphs2/1_minimum_flight_cost/Dijkstra.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RohanTrix/Competitive-Practice/b14aae7dbd778d8cdf743c77a1f8d25b3048937e/Platforms/EXTRA/Coursera-Algorithms-On-Graphs/week4_paths_in_graphs2/1_minimum_flight_cost/Dijkstra.class -------------------------------------------------------------------------------- /Platforms/EXTRA/Coursera-Algorithms-On-Graphs/week4_paths_in_graphs2/1_minimum_flight_cost/dijkstra.py: -------------------------------------------------------------------------------- 1 | #Uses python3 2 | 3 | import sys 4 | import queue 5 | 6 | 7 | def distance(adj, cost, s, t): 8 | #write your code here 9 | return -1 10 | 11 | 12 | if __name__ == '__main__': 13 | input = sys.stdin.read() 14 | data = list(map(int, input.split())) 15 | n, m = data[0:2] 16 | data = data[2:] 17 | edges = list(zip(zip(data[0:(3 * m):3], data[1:(3 * m):3]), data[2:(3 * m):3])) 18 | data = data[3 * m:] 19 | adj = [[] for _ in range(n)] 20 | cost = [[] for _ in range(n)] 21 | for ((a, b), w) in edges: 22 | adj[a - 1].append(b - 1) 23 | cost[a - 1].append(w) 24 | s, t = data[0] - 1, data[1] - 1 25 | print(distance(adj, cost, s, t)) 26 | -------------------------------------------------------------------------------- /Platforms/EXTRA/Coursera-Algorithms-On-Graphs/week4_paths_in_graphs2/2_detecting_anomalies/NegativeCycle$pair.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RohanTrix/Competitive-Practice/b14aae7dbd778d8cdf743c77a1f8d25b3048937e/Platforms/EXTRA/Coursera-Algorithms-On-Graphs/week4_paths_in_graphs2/2_detecting_anomalies/NegativeCycle$pair.class -------------------------------------------------------------------------------- /Platforms/EXTRA/Coursera-Algorithms-On-Graphs/week4_paths_in_graphs2/2_detecting_anomalies/NegativeCycle.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RohanTrix/Competitive-Practice/b14aae7dbd778d8cdf743c77a1f8d25b3048937e/Platforms/EXTRA/Coursera-Algorithms-On-Graphs/week4_paths_in_graphs2/2_detecting_anomalies/NegativeCycle.class -------------------------------------------------------------------------------- /Platforms/EXTRA/Coursera-Algorithms-On-Graphs/week4_paths_in_graphs2/2_detecting_anomalies/negative_cycle.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using std::vector; 5 | 6 | int negative_cycle(vector > &adj, vector > &cost) { 7 | //write your code here 8 | return 0; 9 | } 10 | 11 | int main() { 12 | int n, m; 13 | std::cin >> n >> m; 14 | vector > adj(n, vector()); 15 | vector > cost(n, vector()); 16 | for (int i = 0; i < m; i++) { 17 | int x, y, w; 18 | std::cin >> x >> y >> w; 19 | adj[x - 1].push_back(y - 1); 20 | cost[x - 1].push_back(w); 21 | } 22 | std::cout << negative_cycle(adj, cost); 23 | } 24 | -------------------------------------------------------------------------------- /Platforms/EXTRA/Coursera-Algorithms-On-Graphs/week4_paths_in_graphs2/2_detecting_anomalies/negative_cycle.py: -------------------------------------------------------------------------------- 1 | #Uses python3 2 | 3 | import sys 4 | 5 | 6 | def negative_cycle(adj, cost): 7 | #write your code here 8 | return 0 9 | 10 | 11 | if __name__ == '__main__': 12 | input = sys.stdin.read() 13 | data = list(map(int, input.split())) 14 | n, m = data[0:2] 15 | data = data[2:] 16 | edges = list(zip(zip(data[0:(3 * m):3], data[1:(3 * m):3]), data[2:(3 * m):3])) 17 | data = data[3 * m:] 18 | adj = [[] for _ in range(n)] 19 | cost = [[] for _ in range(n)] 20 | for ((a, b), w) in edges: 21 | adj[a - 1].append(b - 1) 22 | cost[a - 1].append(w) 23 | print(negative_cycle(adj, cost)) 24 | -------------------------------------------------------------------------------- /Platforms/EXTRA/Coursera-Algorithms-On-Graphs/week4_paths_in_graphs2/3_exchanging_money/ShortestPaths$pair.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RohanTrix/Competitive-Practice/b14aae7dbd778d8cdf743c77a1f8d25b3048937e/Platforms/EXTRA/Coursera-Algorithms-On-Graphs/week4_paths_in_graphs2/3_exchanging_money/ShortestPaths$pair.class -------------------------------------------------------------------------------- /Platforms/EXTRA/Coursera-Algorithms-On-Graphs/week4_paths_in_graphs2/3_exchanging_money/ShortestPaths.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RohanTrix/Competitive-Practice/b14aae7dbd778d8cdf743c77a1f8d25b3048937e/Platforms/EXTRA/Coursera-Algorithms-On-Graphs/week4_paths_in_graphs2/3_exchanging_money/ShortestPaths.class -------------------------------------------------------------------------------- /Platforms/EXTRA/Coursera-Algorithms-On-Graphs/week4_paths_in_graphs2/debug.log: -------------------------------------------------------------------------------- 1 | [1120/213810.562:ERROR:directory_reader_win.cc(43)] FindFirstFile: The system cannot find the path specified. (0x3) 2 | -------------------------------------------------------------------------------- /Platforms/EXTRA/Coursera-Algorithms-On-Graphs/week4_paths_in_graphs2/week4_paths_in_graphs2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RohanTrix/Competitive-Practice/b14aae7dbd778d8cdf743c77a1f8d25b3048937e/Platforms/EXTRA/Coursera-Algorithms-On-Graphs/week4_paths_in_graphs2/week4_paths_in_graphs2.pdf -------------------------------------------------------------------------------- /Platforms/EXTRA/Coursera-Algorithms-On-Graphs/week5_spanning_trees/1_connecting_points/ConnectingPoints.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class ConnectingPoints { 4 | private static double minimumDistance(int[] x, int[] y) { 5 | double result = 0.; 6 | //write your code here 7 | return result; 8 | } 9 | 10 | public static void main(String[] args) { 11 | Scanner scanner = new Scanner(System.in); 12 | int n = scanner.nextInt(); 13 | int[] x = new int[n]; 14 | int[] y = new int[n]; 15 | for (int i = 0; i < n; i++) { 16 | x[i] = scanner.nextInt(); 17 | y[i] = scanner.nextInt(); 18 | } 19 | System.out.println(minimumDistance(x, y)); 20 | } 21 | } 22 | 23 | -------------------------------------------------------------------------------- /Platforms/EXTRA/Coursera-Algorithms-On-Graphs/week5_spanning_trees/1_connecting_points/connecting_points.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | using std::vector; 8 | 9 | double minimum_distance(vector x, vector y) { 10 | double result = 0.; 11 | //write your code here 12 | return result; 13 | } 14 | 15 | int main() { 16 | size_t n; 17 | std::cin >> n; 18 | vector x(n), y(n); 19 | for (size_t i = 0; i < n; i++) { 20 | std::cin >> x[i] >> y[i]; 21 | } 22 | std::cout << std::setprecision(10) << minimum_distance(x, y) << std::endl; 23 | } 24 | -------------------------------------------------------------------------------- /Platforms/EXTRA/Coursera-Algorithms-On-Graphs/week5_spanning_trees/1_connecting_points/connecting_points.py: -------------------------------------------------------------------------------- 1 | #Uses python3 2 | import sys 3 | import math 4 | 5 | def minimum_distance(x, y): 6 | result = 0. 7 | #write your code here 8 | return result 9 | 10 | 11 | if __name__ == '__main__': 12 | input = sys.stdin.read() 13 | data = list(map(int, input.split())) 14 | n = data[0] 15 | x = data[1::2] 16 | y = data[2::2] 17 | print("{0:.9f}".format(minimum_distance(x, y))) 18 | -------------------------------------------------------------------------------- /Platforms/EXTRA/Coursera-Algorithms-On-Graphs/week5_spanning_trees/2_clustering/Clustering.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Clustering { 4 | private static double clustering(int[] x, int[] y, int k) { 5 | //write your code here 6 | return -1.; 7 | } 8 | 9 | public static void main(String[] args) { 10 | Scanner scanner = new Scanner(System.in); 11 | int n = scanner.nextInt(); 12 | int[] x = new int[n]; 13 | int[] y = new int[n]; 14 | for (int i = 0; i < n; i++) { 15 | x[i] = scanner.nextInt(); 16 | y[i] = scanner.nextInt(); 17 | } 18 | int k = scanner.nextInt(); 19 | System.out.println(clustering(x, y, k)); 20 | } 21 | } 22 | 23 | -------------------------------------------------------------------------------- /Platforms/EXTRA/Coursera-Algorithms-On-Graphs/week5_spanning_trees/2_clustering/clustering.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | using std::vector; 9 | using std::pair; 10 | 11 | 12 | 13 | double clustering(vector x, vector y, int k) { 14 | //write your code here 15 | return -1.; 16 | } 17 | 18 | int main() { 19 | size_t n; 20 | int k; 21 | std::cin >> n; 22 | vector x(n), y(n); 23 | for (size_t i = 0; i < n; i++) { 24 | std::cin >> x[i] >> y[i]; 25 | } 26 | std::cin >> k; 27 | std::cout << std::setprecision(10) << clustering(x, y, k) << std::endl; 28 | } 29 | -------------------------------------------------------------------------------- /Platforms/EXTRA/Coursera-Algorithms-On-Graphs/week5_spanning_trees/2_clustering/clustering.py: -------------------------------------------------------------------------------- 1 | #Uses python3 2 | import sys 3 | import math 4 | 5 | def clustering(x, y, k): 6 | #write your code here 7 | return -1. 8 | 9 | 10 | if __name__ == '__main__': 11 | input = sys.stdin.read() 12 | data = list(map(int, input.split())) 13 | n = data[0] 14 | data = data[1:] 15 | x = data[0:2 * n:2] 16 | y = data[1:2 * n:2] 17 | data = data[2 * n:] 18 | k = data[0] 19 | print("{0:.9f}".format(clustering(x, y, k))) 20 | -------------------------------------------------------------------------------- /Platforms/EXTRA/Coursera-Algorithms-On-Graphs/week5_spanning_trees/week5_spanning_trees.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RohanTrix/Competitive-Practice/b14aae7dbd778d8cdf743c77a1f8d25b3048937e/Platforms/EXTRA/Coursera-Algorithms-On-Graphs/week5_spanning_trees/week5_spanning_trees.pdf -------------------------------------------------------------------------------- /Platforms/EXTRA/Google Kickstart/Round A/K-Goodness String/Solution.py: -------------------------------------------------------------------------------- 1 | for t in range(int(input())): 2 | n, k = map(int,input().split()) 3 | s = input() 4 | c =0 5 | for i in range(1,n//2+1): 6 | if s[i-1]!=s[n-i]: 7 | c+=1 8 | ans = abs(k-c) 9 | print(f"Case #{t+1}: {ans}") -------------------------------------------------------------------------------- /Platforms/EXTRA/Largest-Independent-Set-Problem/Question.md: -------------------------------------------------------------------------------- 1 | # Largest Largest Independent Set Problem 2 | > Given a Binary Tree of size N, find the size of the Largest Independent Set(LIS) in it. A subset of all tree nodes is an independent set if there is no edge between any two nodes of the subset. Your task is to complete the function LISS(), which finds the size of the Largest Independent Set. 3 | 4 | For example: 5 | Consider the following binary tree.The LIS is 6 | LIS: [10, 40, 60, 70, 80] 7 | Size: 5. 8 | 9 | ![](https://www.geeksforgeeks.org/wp-content/uploads/LargestIndependentSet.png) -------------------------------------------------------------------------------- /Platforms/EXTRA/Maximum-sum-increasing-subsequence/README.md: -------------------------------------------------------------------------------- 1 | # Question: 2 | Given an array arr of N positive integers, the task is to find the maximum sum increasing subsequence of the given array. -------------------------------------------------------------------------------- /Platforms/EXTRA/Maximum-sum-of-Non-adjacent-nodes/Question.md: -------------------------------------------------------------------------------- 1 | # Maximum sum of Non-adjacent nodes 2 | 3 | > Given a binary tree with a value associated with each node, we need to choose a subset of these nodes such that sum of chosen nodes is maximum under a constraint that no two chosen node in subset should be directly connected that is, if we have taken a node in our sum then we can’t take its any children in consideration and vice versa. 4 | 5 | ![](http://cdncontribute.geeksforgeeks.org/wp-content/uploads/nodeSubsetWithMaxSum.png) 6 | -------------------------------------------------------------------------------- /Platforms/EXTRA/Minimum Difference Subsets!/README.md: -------------------------------------------------------------------------------- 1 | # Problem Description 2 | 3 | Given an integer array A containing N integers. 4 | 5 | You need to divide the array A into two subsets S1 and S2 such that the absolute difference between their sums is minimum. 6 | 7 | Find and return this minimum possible absolute difference. 8 | 9 | NOTE: 10 | 11 | Subsets can contain elements from A in any order (not necessary to be contiguous). 12 | Each element of A should belong to any one subset S1 or S2, not both. 13 | It may be possible that one subset remains empty. 14 | 15 | 16 | Problem Constraints 17 | 1 <= N <= 100 18 | 19 | 1 <= A[i] <= 100 20 | 21 | 22 | 23 | Input Format 24 | First and only argument is an integer array A. 25 | 26 | 27 | 28 | Output Format 29 | Return an integer denoting the minimum possible difference among the sums of two subsets. -------------------------------------------------------------------------------- /Platforms/EXTRA/String-Subsequence/Question.md: -------------------------------------------------------------------------------- 1 | # String Subsequence 2 | > Given two strings S1 and S2, find the number of times the second string occurs in the first string, whether continuous or discontinuous. 3 | 4 | Example 1: 5 | 6 | Input: 7 | S1 = geeksforgeeks 8 | S2 = gks 9 | Output: 4 10 | Explaination: For the first 'g' there 11 | are 3 ways and for the second 'g' there 12 | is one way. Total 4 ways. -------------------------------------------------------------------------------- /Platforms/GFG/Count pairs with given sum.md: -------------------------------------------------------------------------------- 1 | ```java 2 | class Solution { 3 | int getPairsCount(int[] arr, int n, int k) { 4 | // code here 5 | int cnt =0; 6 | Map map = new HashMap<>(); 7 | for (int i = 0; i < arr.length; i++) { 8 | int complement = k - arr[i]; 9 | if (map.containsKey(complement)) { 10 | cnt+= map.get(complement); 11 | } 12 | map.put(arr[i], map.getOrDefault(arr[i],0)+1); 13 | } 14 | return cnt; 15 | } 16 | } 17 | ``` -------------------------------------------------------------------------------- /Platforms/GFG/Longest substring to form a Palindrome.md: -------------------------------------------------------------------------------- 1 | ### Given a string S which only contains lowercase alphabets. Find the length of the longest substring of S such that the characters in it can be rearranged to form a palindrome. 2 | 3 | > REFER Explanation : https://www.geeksforgeeks.org/longest-substring-whose-characters-can-be-rearranged-to-form-a-palindrome/ 4 | 5 | -------------------------------------------------------------------------------- /Platforms/Hackerearth/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RohanTrix/Competitive-Practice/b14aae7dbd778d8cdf743c77a1f8d25b3048937e/Platforms/Hackerearth/.gitkeep -------------------------------------------------------------------------------- /Platforms/Hackerearth/Input_expectations.java: -------------------------------------------------------------------------------- 1 | package Hackerearth; 2 | 3 | import java.util.*; 4 | public class Input_expectations { 5 | public static void main(String args[] ) throws Exception { 6 | Scanner sc= new Scanner(System.in); 7 | int t = sc.nextInt(); 8 | int cnt=0; 9 | for(int i = 0; i < t; i++){ 10 | 11 | int n; 12 | if(sc.hasNext()){ 13 | n = sc.nextInt(); 14 | while(n>0 && sc.hasNext()) 15 | { 16 | sc.nextInt(); 17 | n-=1; 18 | } 19 | cnt+=n; 20 | } 21 | else 22 | { 23 | cnt+=1; 24 | } 25 | } 26 | sc.close(); 27 | System.out.println(cnt); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Platforms/Hackerearth/Java-Classes/Monk_And_Champions_League$FastReader.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RohanTrix/Competitive-Practice/b14aae7dbd778d8cdf743c77a1f8d25b3048937e/Platforms/Hackerearth/Java-Classes/Monk_And_Champions_League$FastReader.class -------------------------------------------------------------------------------- /Platforms/Hackerearth/Java-Classes/Monk_And_Champions_League$pair.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RohanTrix/Competitive-Practice/b14aae7dbd778d8cdf743c77a1f8d25b3048937e/Platforms/Hackerearth/Java-Classes/Monk_And_Champions_League$pair.class -------------------------------------------------------------------------------- /Platforms/Hackerearth/Java-Classes/Monk_And_Champions_League.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RohanTrix/Competitive-Practice/b14aae7dbd778d8cdf743c77a1f8d25b3048937e/Platforms/Hackerearth/Java-Classes/Monk_And_Champions_League.class -------------------------------------------------------------------------------- /Platforms/Hackerearth/Java-Classes/Monk_and_Multiplication$FastReader.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RohanTrix/Competitive-Practice/b14aae7dbd778d8cdf743c77a1f8d25b3048937e/Platforms/Hackerearth/Java-Classes/Monk_and_Multiplication$FastReader.class -------------------------------------------------------------------------------- /Platforms/Hackerearth/Java-Classes/Monk_and_Multiplication$pair.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RohanTrix/Competitive-Practice/b14aae7dbd778d8cdf743c77a1f8d25b3048937e/Platforms/Hackerearth/Java-Classes/Monk_and_Multiplication$pair.class -------------------------------------------------------------------------------- /Platforms/Hackerearth/Java-Classes/Monk_and_Multiplication.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RohanTrix/Competitive-Practice/b14aae7dbd778d8cdf743c77a1f8d25b3048937e/Platforms/Hackerearth/Java-Classes/Monk_and_Multiplication.class -------------------------------------------------------------------------------- /Platforms/Hackerearth/cpprun.py: -------------------------------------------------------------------------------- 1 | import os 2 | import time 3 | prog_name = ".c" 4 | os.system(f'g++ -o out {prog_name}') 5 | t1 = time.time() 6 | os.system("out") 7 | t2 = time.time() 8 | print("TIME TAKEN: ",t2-t1) -------------------------------------------------------------------------------- /Platforms/Hackerearth/input.txt: -------------------------------------------------------------------------------- 1 | 3 4 2 | 1 2 4 -------------------------------------------------------------------------------- /Platforms/Hackerearth/javarun.py: -------------------------------------------------------------------------------- 1 | import os 2 | import time 3 | prog_name = "*.java" 4 | os.system(f'javac -d Java-Classes {prog_name}') 5 | t1 = time.time() 6 | os.system(f'java -cp Java-Classes Monk_And_Champions_League local') 7 | t2 = time.time() 8 | print("TIME TAKEN: ",t2-t1) -------------------------------------------------------------------------------- /Platforms/Hackerearth/output.txt: -------------------------------------------------------------------------------- 1 | 11 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Problem Solving Solution Repo -------------------------------------------------------------------------------- /autocommit.py: -------------------------------------------------------------------------------- 1 | import os 2 | import time 3 | while(True): 4 | if os.system('git diff --exit-code')==0: 5 | print(0) 6 | time.sleep(60) 7 | f = open('helloworld.txt','w') 8 | f.write(str(time.time())) 9 | f.close() 10 | continue 11 | os.system('git add-commit -m "Committed changes"') 12 | os.system('git push -u origin master') 13 | time.sleep(60) 14 | -------------------------------------------------------------------------------- /file_name_maker.py: -------------------------------------------------------------------------------- 1 | s = input("File Name: ") 2 | dtype = input("File type: ") 3 | print('_'.join(s.split())+f'.{dtype}') --------------------------------------------------------------------------------