diff --git a/README.md b/README.md index 66cdad4a..fe00cfc5 100644 --- a/README.md +++ b/README.md @@ -62,14 +62,14 @@ | 0283 |[Move Zeroes](src/main/kotlin/g0201_0300/s0283_move_zeroes)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Two_Pointers | 516 | 79.07 | 0001 |[Two Sum](src/main/kotlin/g0001_0100/s0001_two_sum)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table | 202 | 91.18 | 0217 |[Contains Duplicate](src/main/kotlin/g0201_0300/s0217_contains_duplicate)| Easy | Top_Interview_Questions, Array, Hash_Table, Sorting | 719 | 73.49 -| 0058 |[Length of Last Word](src/main/kotlin/g0001_0100/s0058_length_of_last_word)| Easy | String | 243 | 63.33 +| 0058 |[Length of Last Word](src/main/kotlin/g0001_0100/s0058_length_of_last_word)| Easy | String | 135 | 93.67 | 0605 |[Can Place Flowers](src/main/kotlin/g0601_0700/s0605_can_place_flowers)| Easy | Array, Greedy | 209 | 85.71 | 0122 |[Best Time to Buy and Sell Stock II](src/main/kotlin/g0101_0200/s0122_best_time_to_buy_and_sell_stock_ii)| Medium | Top_Interview_Questions, Array, Dynamic_Programming, Greedy | 197 | 95.10 -| 0080 |[Remove Duplicates from Sorted Array II](src/main/kotlin/g0001_0100/s0080_remove_duplicates_from_sorted_array_ii)| Medium | Array, Two_Pointers | 357 | 44.78 +| 0080 |[Remove Duplicates from Sorted Array II](src/main/kotlin/g0001_0100/s0080_remove_duplicates_from_sorted_array_ii)| Medium | Array, Two_Pointers | 192 | 80.67 | 0189 |[Rotate Array](src/main/kotlin/g0101_0200/s0189_rotate_array)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Math, Two_Pointers | 483 | 86.95 -| 0055 |[Jump Game](src/main/kotlin/g0001_0100/s0055_jump_game)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Greedy | 670 | 66.67 -| 0075 |[Sort Colors](src/main/kotlin/g0001_0100/s0075_sort_colors)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Two_Pointers | 198 | 85.66 -| 0066 |[Plus One](src/main/kotlin/g0001_0100/s0066_plus_one)| Easy | Top_Interview_Questions, Array, Math | 303 | 35.18 +| 0055 |[Jump Game](src/main/kotlin/g0001_0100/s0055_jump_game)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Greedy | 332 | 89.35 +| 0075 |[Sort Colors](src/main/kotlin/g0001_0100/s0075_sort_colors)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Two_Pointers | 164 | 64.43 +| 0066 |[Plus One](src/main/kotlin/g0001_0100/s0066_plus_one)| Easy | Top_Interview_Questions, Array, Math | 148 | 98.75 | 0238 |[Product of Array Except Self](src/main/kotlin/g0201_0300/s0238_product_of_array_except_self)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Prefix_Sum | 669 | 48.96 | 1291 |[Sequential Digits](src/main/kotlin/g1201_1300/s1291_sequential_digits)| Medium | Enumeration | 114 | 100.00 | 0448 |[Find All Numbers Disappeared in an Array](src/main/kotlin/g0401_0500/s0448_find_all_numbers_disappeared_in_an_array)| Easy | Array, Hash_Table | 394 | 100.00 @@ -98,7 +98,7 @@ | | | | | | |-|-|-|-|-|- -| 0053 |[Maximum Subarray](src/main/kotlin/g0001_0100/s0053_maximum_subarray)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Divide_and_Conquer | 662 | 82.48 +| 0053 |[Maximum Subarray](src/main/kotlin/g0001_0100/s0053_maximum_subarray)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Divide_and_Conquer | 510 | 78.81 | 0169 |[Majority Element](src/main/kotlin/g0101_0200/s0169_majority_element)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Sorting, Counting, Divide_and_Conquer | 460 | 51.25 #### Udemy Sorting Algorithms @@ -112,12 +112,12 @@ | | | | | | |-|-|-|-|-|- | 0304 |[Range Sum Query 2D - Immutable](src/main/kotlin/g0301_0400/s0304_range_sum_query_2d_immutable)| Medium | Array, Matrix, Design, Prefix_Sum | 1373 | 85.71 -| 0074 |[Search a 2D Matrix](src/main/kotlin/g0001_0100/s0074_search_a_2d_matrix)| Medium | Top_100_Liked_Questions, Array, Binary_Search, Matrix | 290 | 40.17 -| 0054 |[Spiral Matrix](src/main/kotlin/g0001_0100/s0054_spiral_matrix)| Medium | Top_Interview_Questions, Array, Matrix, Simulation | 224 | 62.50 +| 0074 |[Search a 2D Matrix](src/main/kotlin/g0001_0100/s0074_search_a_2d_matrix)| Medium | Top_100_Liked_Questions, Array, Binary_Search, Matrix | 159 | 74.46 +| 0054 |[Spiral Matrix](src/main/kotlin/g0001_0100/s0054_spiral_matrix)| Medium | Top_Interview_Questions, Array, Matrix, Simulation | 132 | 95.12 | 0048 |[Rotate Image](src/main/kotlin/g0001_0100/s0048_rotate_image)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Math, Matrix | 160 | 90.11 | 1572 |[Matrix Diagonal Sum](src/main/kotlin/g1501_1600/s1572_matrix_diagonal_sum)| Easy | Array, Matrix | 221 | 67.61 -| 0073 |[Set Matrix Zeroes](src/main/kotlin/g0001_0100/s0073_set_matrix_zeroes)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Matrix | 255 | 100.00 -| 0056 |[Merge Intervals](src/main/kotlin/g0001_0100/s0056_merge_intervals)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting | 320 | 94.22 +| 0073 |[Set Matrix Zeroes](src/main/kotlin/g0001_0100/s0073_set_matrix_zeroes)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Matrix | 248 | 97.33 +| 0056 |[Merge Intervals](src/main/kotlin/g0001_0100/s0056_merge_intervals)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting | 334 | 80.67 #### Udemy Linked List @@ -126,7 +126,7 @@ | 0114 |[Flatten Binary Tree to Linked List](src/main/kotlin/g0101_0200/s0114_flatten_binary_tree_to_linked_list)| Medium | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree, Stack, Linked_List | 191 | 93.10 | 0445 |[Add Two Numbers II](src/main/kotlin/g0401_0500/s0445_add_two_numbers_ii)| Medium | Math, Stack, Linked_List | 240 | 82.61 | 0328 |[Odd Even Linked List](src/main/kotlin/g0301_0400/s0328_odd_even_linked_list)| Medium | Top_Interview_Questions, Linked_List | 216 | 86.96 -| 0061 |[Rotate List](src/main/kotlin/g0001_0100/s0061_rotate_list)| Medium | Two_Pointers, Linked_List | 193 | 92.16 +| 0061 |[Rotate List](src/main/kotlin/g0001_0100/s0061_rotate_list)| Medium | Two_Pointers, Linked_List | 160 | 92.22 | 0024 |[Swap Nodes in Pairs](src/main/kotlin/g0001_0100/s0024_swap_nodes_in_pairs)| Medium | Top_100_Liked_Questions, Linked_List, Recursion | 149 | 44.20 | 0876 |[Middle of the Linked List](src/main/kotlin/g0801_0900/s0876_middle_of_the_linked_list)| Easy | Two_Pointers, Linked_List | 136 | 76.52 | 0142 |[Linked List Cycle II](src/main/kotlin/g0101_0200/s0142_linked_list_cycle_ii)| Medium | Top_100_Liked_Questions, Hash_Table, Two_Pointers, Linked_List | 192 | 63.39 @@ -145,23 +145,23 @@ | | | | | | |-|-|-|-|-|- | 0144 |[Binary Tree Preorder Traversal](src/main/kotlin/g0101_0200/s0144_binary_tree_preorder_traversal)| Easy | Depth_First_Search, Tree, Binary_Tree, Stack | 277 | 37.90 -| 0094 |[Binary Tree Inorder Traversal](src/main/kotlin/g0001_0100/s0094_binary_tree_inorder_traversal)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Tree, Binary_Tree, Stack | 269 | 38.80 +| 0094 |[Binary Tree Inorder Traversal](src/main/kotlin/g0001_0100/s0094_binary_tree_inorder_traversal)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Tree, Binary_Tree, Stack | 152 | 66.67 | 0145 |[Binary Tree Postorder Traversal](src/main/kotlin/g0101_0200/s0145_binary_tree_postorder_traversal)| Easy | Depth_First_Search, Tree, Binary_Tree, Stack | 211 | 80.00 -| 0102 |[Binary Tree Level Order Traversal](src/main/kotlin/g0101_0200/s0102_binary_tree_level_order_traversal)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Breadth_First_Search, Tree, Binary_Tree | 332 | 67.53 -| 0103 |[Binary Tree Zigzag Level Order Traversal](src/main/kotlin/g0101_0200/s0103_binary_tree_zigzag_level_order_traversal)| Medium | Top_Interview_Questions, Breadth_First_Search, Tree, Binary_Tree | 316 | 34.25 -| 0108 |[Convert Sorted Array to Binary Search Tree](src/main/kotlin/g0101_0200/s0108_convert_sorted_array_to_binary_search_tree)| Easy | Top_Interview_Questions, Array, Tree, Binary_Tree, Binary_Search_Tree, Divide_and_Conquer | 334 | 35.39 +| 0102 |[Binary Tree Level Order Traversal](src/main/kotlin/g0101_0200/s0102_binary_tree_level_order_traversal)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Breadth_First_Search, Tree, Binary_Tree | 198 | 95.14 +| 0103 |[Binary Tree Zigzag Level Order Traversal](src/main/kotlin/g0101_0200/s0103_binary_tree_zigzag_level_order_traversal)| Medium | Top_Interview_Questions, Breadth_First_Search, Tree, Binary_Tree | 176 | 71.62 +| 0108 |[Convert Sorted Array to Binary Search Tree](src/main/kotlin/g0101_0200/s0108_convert_sorted_array_to_binary_search_tree)| Easy | Top_Interview_Questions, Array, Tree, Binary_Tree, Binary_Search_Tree, Divide_and_Conquer | 167 | 80.29 | 1008 |[Construct Binary Search Tree from Preorder Traversal](src/main/kotlin/g1001_1100/s1008_construct_binary_search_tree_from_preorder_traversal)| Medium | Array, Tree, Binary_Tree, Stack, Monotonic_Stack, Binary_Search_Tree | 145 | 100.00 | 0543 |[Diameter of Binary Tree](src/main/kotlin/g0501_0600/s0543_diameter_of_binary_tree)| Easy | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree | 307 | 43.93 | 0938 |[Range Sum of BST](src/main/kotlin/g0901_1000/s0938_range_sum_of_bst)| Easy | Depth_First_Search, Tree, Binary_Tree, Binary_Search_Tree | 356 | 55.36 -| 0100 |[Same Tree](src/main/kotlin/g0001_0100/s0100_same_tree)| Easy | Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree | 208 | 72.24 +| 0100 |[Same Tree](src/main/kotlin/g0001_0100/s0100_same_tree)| Easy | Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree | 134 | 86.63 | 0226 |[Invert Binary Tree](src/main/kotlin/g0201_0300/s0226_invert_binary_tree)| Easy | Top_100_Liked_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree | 233 | 54.90 | 0111 |[Minimum Depth of Binary Tree](src/main/kotlin/g0101_0200/s0111_minimum_depth_of_binary_tree)| Easy | Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree | 525 | 90.51 -| 0104 |[Maximum Depth of Binary Tree](src/main/kotlin/g0101_0200/s0104_maximum_depth_of_binary_tree)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree | 236 | 83.39 -| 0110 |[Balanced Binary Tree](src/main/kotlin/g0101_0200/s0110_balanced_binary_tree)| Easy | Depth_First_Search, Tree, Binary_Tree | 310 | 63.63 +| 0104 |[Maximum Depth of Binary Tree](src/main/kotlin/g0101_0200/s0104_maximum_depth_of_binary_tree)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree | 166 | 83.53 +| 0110 |[Balanced Binary Tree](src/main/kotlin/g0101_0200/s0110_balanced_binary_tree)| Easy | Depth_First_Search, Tree, Binary_Tree | 182 | 71.30 | 0701 |[Insert into a Binary Search Tree](src/main/kotlin/g0701_0800/s0701_insert_into_a_binary_search_tree)| Medium | Tree, Binary_Tree, Binary_Search_Tree | 311 | 79.03 | 0297 |[Serialize and Deserialize Binary Tree](src/main/kotlin/g0201_0300/s0297_serialize_and_deserialize_binary_tree)| Hard | Top_Interview_Questions, String, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Design | 475 | 78.85 | 0124 |[Binary Tree Maximum Path Sum](src/main/kotlin/g0101_0200/s0124_binary_tree_maximum_path_sum)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Depth_First_Search, Tree, Binary_Tree | 331 | 74.42 -| 0098 |[Validate Binary Search Tree](src/main/kotlin/g0001_0100/s0098_validate_binary_search_tree)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Tree, Binary_Tree, Binary_Search_Tree | 330 | 41.38 +| 0098 |[Validate Binary Search Tree](src/main/kotlin/g0001_0100/s0098_validate_binary_search_tree)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Tree, Binary_Tree, Binary_Search_Tree | 190 | 61.62 | 0337 |[House Robber III](src/main/kotlin/g0301_0400/s0337_house_robber_iii)| Medium | Dynamic_Programming, Depth_First_Search, Tree, Binary_Tree | 282 | 84.62 | 0236 |[Lowest Common Ancestor of a Binary Tree](src/main/kotlin/g0201_0300/s0236_lowest_common_ancestor_of_a_binary_tree)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Tree, Binary_Tree | 386 | 45.21 | 0968 |[Binary Tree Cameras](src/main/kotlin/g0901_1000/s0968_binary_tree_cameras)| Hard | Dynamic_Programming, Depth_First_Search, Tree, Binary_Tree | 176 | 100.00 @@ -193,11 +193,11 @@ | 0198 |[House Robber](src/main/kotlin/g0101_0200/s0198_house_robber)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming | 156 | 92.24 | 0213 |[House Robber II](src/main/kotlin/g0201_0300/s0213_house_robber_ii)| Medium | Array, Dynamic_Programming | 257 | 59.62 | 0509 |[Fibonacci Number](src/main/kotlin/g0501_0600/s0509_fibonacci_number)| Easy | Dynamic_Programming, Math, Recursion, Memoization | 139 | 82.72 -| 0070 |[Climbing Stairs](src/main/kotlin/g0001_0100/s0070_climbing_stairs)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Math, Memoization | 127 | 97.06 -| 0064 |[Minimum Path Sum](src/main/kotlin/g0001_0100/s0064_minimum_path_sum)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Matrix | 222 | 95.70 +| 0070 |[Climbing Stairs](src/main/kotlin/g0001_0100/s0070_climbing_stairs)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Math, Memoization | 124 | 71.98 +| 0064 |[Minimum Path Sum](src/main/kotlin/g0001_0100/s0064_minimum_path_sum)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Matrix | 164 | 100.00 | 0300 |[Longest Increasing Subsequence](src/main/kotlin/g0201_0300/s0300_longest_increasing_subsequence)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Binary_Search | 318 | 82.28 | 1143 |[Longest Common Subsequence](src/main/kotlin/g1101_1200/s1143_longest_common_subsequence)| Medium | Top_100_Liked_Questions, String, Dynamic_Programming | 307 | 38.36 -| 0072 |[Edit Distance](src/main/kotlin/g0001_0100/s0072_edit_distance)| Hard | Top_100_Liked_Questions, String, Dynamic_Programming | 320 | 63.53 +| 0072 |[Edit Distance](src/main/kotlin/g0001_0100/s0072_edit_distance)| Hard | Top_100_Liked_Questions, String, Dynamic_Programming | 182 | 92.16 | 0044 |[Wildcard Matching](src/main/kotlin/g0001_0100/s0044_wildcard_matching)| Hard | Top_Interview_Questions, String, Dynamic_Programming, Greedy, Recursion | 220 | 84.85 | 0010 |[Regular Expression Matching](src/main/kotlin/g0001_0100/s0010_regular_expression_matching)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, String, Dynamic_Programming, Recursion | 171 | 85.26 @@ -208,7 +208,7 @@ | 0022 |[Generate Parentheses](src/main/kotlin/g0001_0100/s0022_generate_parentheses)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Dynamic_Programming, Backtracking | 161 | 85.45 | 0039 |[Combination Sum](src/main/kotlin/g0001_0100/s0039_combination_sum)| Medium | Top_100_Liked_Questions, Array, Backtracking | 226 | 88.89 | 0216 |[Combination Sum III](src/main/kotlin/g0201_0300/s0216_combination_sum_iii)| Medium | Array, Backtracking | 175 | 90.91 -| 0078 |[Subsets](src/main/kotlin/g0001_0100/s0078_subsets)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Bit_Manipulation, Backtracking | 191 | 97.44 +| 0078 |[Subsets](src/main/kotlin/g0001_0100/s0078_subsets)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Bit_Manipulation, Backtracking | 171 | 94.92 | 0017 |[Letter Combinations of a Phone Number](src/main/kotlin/g0001_0100/s0017_letter_combinations_of_a_phone_number)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Backtracking | 155 | 95.24 | 0046 |[Permutations](src/main/kotlin/g0001_0100/s0046_permutations)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Backtracking | 188 | 99.00 @@ -238,14 +238,14 @@ | | | | | | |-|-|-|-|-|- | 0217 |[Contains Duplicate](src/main/kotlin/g0201_0300/s0217_contains_duplicate)| Easy | Top_Interview_Questions, Array, Hash_Table, Sorting | 719 | 73.49 -| 0053 |[Maximum Subarray](src/main/kotlin/g0001_0100/s0053_maximum_subarray)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Divide_and_Conquer | 662 | 82.48 +| 0053 |[Maximum Subarray](src/main/kotlin/g0001_0100/s0053_maximum_subarray)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Divide_and_Conquer | 510 | 78.81 #### Day 2 Array | | | | | | |-|-|-|-|-|- | 0001 |[Two Sum](src/main/kotlin/g0001_0100/s0001_two_sum)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table | 202 | 91.18 -| 0088 |[Merge Sorted Array](src/main/kotlin/g0001_0100/s0088_merge_sorted_array)| Easy | Top_Interview_Questions, Array, Sorting, Two_Pointers | 311 | 33.40 +| 0088 |[Merge Sorted Array](src/main/kotlin/g0001_0100/s0088_merge_sorted_array)| Easy | Top_Interview_Questions, Array, Sorting, Two_Pointers | 170 | 69.74 #### Day 3 Array @@ -266,7 +266,7 @@ | | | | | | |-|-|-|-|-|- | 0036 |[Valid Sudoku](src/main/kotlin/g0001_0100/s0036_valid_sudoku)| Medium | Top_Interview_Questions, Array, Hash_Table, Matrix | 181 | 95.15 -| 0074 |[Search a 2D Matrix](src/main/kotlin/g0001_0100/s0074_search_a_2d_matrix)| Medium | Top_100_Liked_Questions, Array, Binary_Search, Matrix | 290 | 40.17 +| 0074 |[Search a 2D Matrix](src/main/kotlin/g0001_0100/s0074_search_a_2d_matrix)| Medium | Top_100_Liked_Questions, Array, Binary_Search, Matrix | 159 | 74.46 #### Day 6 String @@ -289,7 +289,7 @@ | | | | | | |-|-|-|-|-|- | 0206 |[Reverse Linked List](src/main/kotlin/g0201_0300/s0206_reverse_linked_list)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Linked_List, Recursion | 279 | 45.78 -| 0083 |[Remove Duplicates from Sorted List](src/main/kotlin/g0001_0100/s0083_remove_duplicates_from_sorted_list)| Easy | Linked_List | 274 | 77.82 +| 0083 |[Remove Duplicates from Sorted List](src/main/kotlin/g0001_0100/s0083_remove_duplicates_from_sorted_list)| Easy | Linked_List | 173 | 82.42 #### Day 9 Stack Queue @@ -303,16 +303,16 @@ | | | | | | |-|-|-|-|-|- | 0144 |[Binary Tree Preorder Traversal](src/main/kotlin/g0101_0200/s0144_binary_tree_preorder_traversal)| Easy | Depth_First_Search, Tree, Binary_Tree, Stack | 277 | 37.90 -| 0094 |[Binary Tree Inorder Traversal](src/main/kotlin/g0001_0100/s0094_binary_tree_inorder_traversal)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Tree, Binary_Tree, Stack | 269 | 38.80 +| 0094 |[Binary Tree Inorder Traversal](src/main/kotlin/g0001_0100/s0094_binary_tree_inorder_traversal)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Tree, Binary_Tree, Stack | 152 | 66.67 | 0145 |[Binary Tree Postorder Traversal](src/main/kotlin/g0101_0200/s0145_binary_tree_postorder_traversal)| Easy | Depth_First_Search, Tree, Binary_Tree, Stack | 211 | 80.00 #### Day 11 Tree | | | | | | |-|-|-|-|-|- -| 0102 |[Binary Tree Level Order Traversal](src/main/kotlin/g0101_0200/s0102_binary_tree_level_order_traversal)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Breadth_First_Search, Tree, Binary_Tree | 332 | 67.53 -| 0104 |[Maximum Depth of Binary Tree](src/main/kotlin/g0101_0200/s0104_maximum_depth_of_binary_tree)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree | 236 | 83.39 -| 0101 |[Symmetric Tree](src/main/kotlin/g0101_0200/s0101_symmetric_tree)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree | 190 | 91.36 +| 0102 |[Binary Tree Level Order Traversal](src/main/kotlin/g0101_0200/s0102_binary_tree_level_order_traversal)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Breadth_First_Search, Tree, Binary_Tree | 198 | 95.14 +| 0104 |[Maximum Depth of Binary Tree](src/main/kotlin/g0101_0200/s0104_maximum_depth_of_binary_tree)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree | 166 | 83.53 +| 0101 |[Symmetric Tree](src/main/kotlin/g0101_0200/s0101_symmetric_tree)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree | 153 | 82.35 #### Day 12 Tree @@ -332,7 +332,7 @@ | | | | | | |-|-|-|-|-|- -| 0098 |[Validate Binary Search Tree](src/main/kotlin/g0001_0100/s0098_validate_binary_search_tree)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Tree, Binary_Tree, Binary_Search_Tree | 330 | 41.38 +| 0098 |[Validate Binary Search Tree](src/main/kotlin/g0001_0100/s0098_validate_binary_search_tree)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Tree, Binary_Tree, Binary_Search_Tree | 190 | 61.62 | 0653 |[Two Sum IV - Input is a BST](src/main/kotlin/g0601_0700/s0653_two_sum_iv_input_is_a_bst)| Easy | Hash_Table, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Two_Pointers, Binary_Search_Tree | 231 | 96.08 | 0235 |[Lowest Common Ancestor of a Binary Search Tree](src/main/kotlin/g0201_0300/s0235_lowest_common_ancestor_of_a_binary_search_tree)| Easy | Depth_First_Search, Tree, Binary_Tree, Binary_Search_Tree | 404 | 75.59 @@ -350,8 +350,8 @@ | | | | | | |-|-|-|-|-|- -| 0075 |[Sort Colors](src/main/kotlin/g0001_0100/s0075_sort_colors)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Two_Pointers | 198 | 85.66 -| 0056 |[Merge Intervals](src/main/kotlin/g0001_0100/s0056_merge_intervals)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting | 320 | 94.22 +| 0075 |[Sort Colors](src/main/kotlin/g0001_0100/s0075_sort_colors)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Two_Pointers | 164 | 64.43 +| 0056 |[Merge Intervals](src/main/kotlin/g0001_0100/s0056_merge_intervals)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting | 334 | 80.67 | 0706 |[Design HashMap](src/main/kotlin/g0701_0800/s0706_design_hashmap)| Easy | Array, Hash_Table, Design, Linked_List, Hash_Function | 405 | 92.11 #### Day 3 Array @@ -360,7 +360,7 @@ |-|-|-|-|-|- | 0119 |[Pascal's Triangle II](src/main/kotlin/g0101_0200/s0119_pascals_triangle_ii)| Easy | Array, Dynamic_Programming | 157 | 97.27 | 0048 |[Rotate Image](src/main/kotlin/g0001_0100/s0048_rotate_image)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Math, Matrix | 160 | 90.11 -| 0059 |[Spiral Matrix II](src/main/kotlin/g0001_0100/s0059_spiral_matrix_ii)| Medium | Array, Matrix, Simulation | 153 | 100.00 +| 0059 |[Spiral Matrix II](src/main/kotlin/g0001_0100/s0059_spiral_matrix_ii)| Medium | Array, Matrix, Simulation | 153 | 41.70 #### Day 4 Array @@ -417,7 +417,7 @@ | | | | | | |-|-|-|-|-|- | 0160 |[Intersection of Two Linked Lists](src/main/kotlin/g0101_0200/s0160_intersection_of_two_linked_lists)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Hash_Table, Two_Pointers, Linked_List | 262 | 83.50 -| 0082 |[Remove Duplicates from Sorted List II](src/main/kotlin/g0001_0100/s0082_remove_duplicates_from_sorted_list_ii)| Medium | Two_Pointers, Linked_List | 241 | 91.04 +| 0082 |[Remove Duplicates from Sorted List II](src/main/kotlin/g0001_0100/s0082_remove_duplicates_from_sorted_list_ii)| Medium | Two_Pointers, Linked_List | 166 | 89.47 #### Day 12 Linked List @@ -445,9 +445,9 @@ | | | | | | |-|-|-|-|-|- -| 0108 |[Convert Sorted Array to Binary Search Tree](src/main/kotlin/g0101_0200/s0108_convert_sorted_array_to_binary_search_tree)| Easy | Top_Interview_Questions, Array, Tree, Binary_Tree, Binary_Search_Tree, Divide_and_Conquer | 334 | 35.39 -| 0105 |[Construct Binary Tree from Preorder and Inorder Traversal](src/main/kotlin/g0101_0200/s0105_construct_binary_tree_from_preorder_and_inorder_traversal)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Tree, Binary_Tree, Divide_and_Conquer | 370 | 58.31 -| 0103 |[Binary Tree Zigzag Level Order Traversal](src/main/kotlin/g0101_0200/s0103_binary_tree_zigzag_level_order_traversal)| Medium | Top_Interview_Questions, Breadth_First_Search, Tree, Binary_Tree | 316 | 34.25 +| 0108 |[Convert Sorted Array to Binary Search Tree](src/main/kotlin/g0101_0200/s0108_convert_sorted_array_to_binary_search_tree)| Easy | Top_Interview_Questions, Array, Tree, Binary_Tree, Binary_Search_Tree, Divide_and_Conquer | 167 | 80.29 +| 0105 |[Construct Binary Tree from Preorder and Inorder Traversal](src/main/kotlin/g0101_0200/s0105_construct_binary_tree_from_preorder_and_inorder_traversal)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Tree, Binary_Tree, Divide_and_Conquer | 183 | 95.45 +| 0103 |[Binary Tree Zigzag Level Order Traversal](src/main/kotlin/g0101_0200/s0103_binary_tree_zigzag_level_order_traversal)| Medium | Top_Interview_Questions, Breadth_First_Search, Tree, Binary_Tree | 176 | 71.62 #### Day 16 Tree @@ -570,7 +570,7 @@ | | | | | | |-|-|-|-|-|- -| 0077 |[Combinations](src/main/kotlin/g0001_0100/s0077_combinations)| Medium | Backtracking | 244 | 100.00 +| 0077 |[Combinations](src/main/kotlin/g0001_0100/s0077_combinations)| Medium | Backtracking | 232 | 100.00 | 0046 |[Permutations](src/main/kotlin/g0001_0100/s0046_permutations)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Backtracking | 188 | 99.00 | 0784 |[Letter Case Permutation](src/main/kotlin/g0701_0800/s0784_letter_case_permutation)| Medium | String, Bit_Manipulation, Backtracking | 219 | 84.62 @@ -578,7 +578,7 @@ | | | | | | |-|-|-|-|-|- -| 0070 |[Climbing Stairs](src/main/kotlin/g0001_0100/s0070_climbing_stairs)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Math, Memoization | 127 | 97.06 +| 0070 |[Climbing Stairs](src/main/kotlin/g0001_0100/s0070_climbing_stairs)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Math, Memoization | 124 | 71.98 | 0198 |[House Robber](src/main/kotlin/g0101_0200/s0198_house_robber)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming | 156 | 92.24 | 0120 |[Triangle](src/main/kotlin/g0101_0200/s0120_triangle)| Medium | Array, Dynamic_Programming | 194 | 97.87 @@ -604,7 +604,7 @@ |-|-|-|-|-|- | 0034 |[Find First and Last Position of Element in Sorted Array](src/main/kotlin/g0001_0100/s0034_find_first_and_last_position_of_element_in_sorted_array)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Binary_Search | 174 | 100.00 | 0033 |[Search in Rotated Sorted Array](src/main/kotlin/g0001_0100/s0033_search_in_rotated_sorted_array)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Binary_Search | 151 | 93.77 -| 0074 |[Search a 2D Matrix](src/main/kotlin/g0001_0100/s0074_search_a_2d_matrix)| Medium | Top_100_Liked_Questions, Array, Binary_Search, Matrix | 290 | 40.17 +| 0074 |[Search a 2D Matrix](src/main/kotlin/g0001_0100/s0074_search_a_2d_matrix)| Medium | Top_100_Liked_Questions, Array, Binary_Search, Matrix | 159 | 74.46 #### Day 2 Binary Search @@ -617,7 +617,7 @@ | | | | | | |-|-|-|-|-|- -| 0082 |[Remove Duplicates from Sorted List II](src/main/kotlin/g0001_0100/s0082_remove_duplicates_from_sorted_list_ii)| Medium | Two_Pointers, Linked_List | 241 | 91.04 +| 0082 |[Remove Duplicates from Sorted List II](src/main/kotlin/g0001_0100/s0082_remove_duplicates_from_sorted_list_ii)| Medium | Two_Pointers, Linked_List | 166 | 89.47 | 0015 |[3Sum](src/main/kotlin/g0001_0100/s0015_3sum)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Two_Pointers | 493 | 93.45 #### Day 4 Two Pointers @@ -662,8 +662,8 @@ | | | | | | |-|-|-|-|-|- -| 0078 |[Subsets](src/main/kotlin/g0001_0100/s0078_subsets)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Bit_Manipulation, Backtracking | 191 | 97.44 -| 0090 |[Subsets II](src/main/kotlin/g0001_0100/s0090_subsets_ii)| Medium | Array, Bit_Manipulation, Backtracking | 366 | 58.09 +| 0078 |[Subsets](src/main/kotlin/g0001_0100/s0078_subsets)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Bit_Manipulation, Backtracking | 171 | 94.92 +| 0090 |[Subsets II](src/main/kotlin/g0001_0100/s0090_subsets_ii)| Medium | Array, Bit_Manipulation, Backtracking | 200 | 88.33 #### Day 10 Recursion Backtracking @@ -679,21 +679,21 @@ |-|-|-|-|-|- | 0017 |[Letter Combinations of a Phone Number](src/main/kotlin/g0001_0100/s0017_letter_combinations_of_a_phone_number)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Backtracking | 155 | 95.24 | 0022 |[Generate Parentheses](src/main/kotlin/g0001_0100/s0022_generate_parentheses)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Dynamic_Programming, Backtracking | 161 | 85.45 -| 0079 |[Word Search](src/main/kotlin/g0001_0100/s0079_word_search)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Matrix, Backtracking | 463 | 68.49 +| 0079 |[Word Search](src/main/kotlin/g0001_0100/s0079_word_search)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Matrix, Backtracking | 392 | 72.92 #### Day 12 Dynamic Programming | | | | | | |-|-|-|-|-|- | 0213 |[House Robber II](src/main/kotlin/g0201_0300/s0213_house_robber_ii)| Medium | Array, Dynamic_Programming | 257 | 59.62 -| 0055 |[Jump Game](src/main/kotlin/g0001_0100/s0055_jump_game)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Greedy | 670 | 66.67 +| 0055 |[Jump Game](src/main/kotlin/g0001_0100/s0055_jump_game)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Greedy | 332 | 89.35 #### Day 13 Dynamic Programming | | | | | | |-|-|-|-|-|- | 0045 |[Jump Game II](src/main/kotlin/g0001_0100/s0045_jump_game_ii)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Greedy | 208 | 93.37 -| 0062 |[Unique Paths](src/main/kotlin/g0001_0100/s0062_unique_paths)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Math, Combinatorics | 209 | 49.18 +| 0062 |[Unique Paths](src/main/kotlin/g0001_0100/s0062_unique_paths)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Math, Combinatorics | 118 | 94.65 #### Day 14 Dynamic Programming @@ -706,7 +706,7 @@ | | | | | | |-|-|-|-|-|- -| 0091 |[Decode Ways](src/main/kotlin/g0001_0100/s0091_decode_ways)| Medium | Top_Interview_Questions, String, Dynamic_Programming | 237 | 76.88 +| 0091 |[Decode Ways](src/main/kotlin/g0001_0100/s0091_decode_ways)| Medium | Top_Interview_Questions, String, Dynamic_Programming | 148 | 79.07 | 0139 |[Word Break](src/main/kotlin/g0101_0200/s0139_word_break)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Dynamic_Programming, Trie, Memoization | 197 | 87.17 #### Day 16 Dynamic Programming @@ -727,7 +727,7 @@ | | | | | | |-|-|-|-|-|- -| 0072 |[Edit Distance](src/main/kotlin/g0001_0100/s0072_edit_distance)| Hard | Top_100_Liked_Questions, String, Dynamic_Programming | 320 | 63.53 +| 0072 |[Edit Distance](src/main/kotlin/g0001_0100/s0072_edit_distance)| Hard | Top_100_Liked_Questions, String, Dynamic_Programming | 182 | 92.16 | 0322 |[Coin Change](src/main/kotlin/g0301_0400/s0322_coin_change)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Breadth_First_Search | 332 | 50.68 | 0343 |[Integer Break](src/main/kotlin/g0301_0400/s0343_integer_break)| Medium | Dynamic_Programming, Math | 218 | 63.89 @@ -777,7 +777,7 @@ | | | | | | |-|-|-|-|-|- -| 0069 |[Sqrt(x)](src/main/kotlin/g0001_0100/s0069_sqrtx)| Easy | Top_Interview_Questions, Math, Binary_Search | 153 | 95.75 +| 0069 |[Sqrt(x)](src/main/kotlin/g0001_0100/s0069_sqrtx)| Easy | Top_Interview_Questions, Math, Binary_Search | 140 | 94.72 | 0744 |[Find Smallest Letter Greater Than Target](src/main/kotlin/g0701_0800/s0744_find_smallest_letter_greater_than_target)| Easy | Array, Binary_Search | 162 | 100.00 #### Day 5 @@ -806,7 +806,7 @@ | | | | | | |-|-|-|-|-|- | 1351 |[Count Negative Numbers in a Sorted Matrix](src/main/kotlin/g1301_1400/s1351_count_negative_numbers_in_a_sorted_matrix)| Easy | Array, Binary_Search, Matrix | 206 | 71.43 -| 0074 |[Search a 2D Matrix](src/main/kotlin/g0001_0100/s0074_search_a_2d_matrix)| Medium | Top_100_Liked_Questions, Array, Binary_Search, Matrix | 290 | 40.17 +| 0074 |[Search a 2D Matrix](src/main/kotlin/g0001_0100/s0074_search_a_2d_matrix)| Medium | Top_100_Liked_Questions, Array, Binary_Search, Matrix | 159 | 74.46 #### Day 9 @@ -918,7 +918,7 @@ | | | | | | |-|-|-|-|-|- -| 0081 |[Search in Rotated Sorted Array II](src/main/kotlin/g0001_0100/s0081_search_in_rotated_sorted_array_ii)| Medium | Array, Binary_Search | 352 | 42.31 +| 0081 |[Search in Rotated Sorted Array II](src/main/kotlin/g0001_0100/s0081_search_in_rotated_sorted_array_ii)| Medium | Array, Binary_Search | 170 | 96.30 | 0162 |[Find Peak Element](src/main/kotlin/g0101_0200/s0162_find_peak_element)| Medium | Top_Interview_Questions, Array, Binary_Search | 297 | 53.85 #### Day 13 @@ -990,7 +990,7 @@ | | | | | | |-|-|-|-|-|- -| 0070 |[Climbing Stairs](src/main/kotlin/g0001_0100/s0070_climbing_stairs)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Math, Memoization | 127 | 97.06 +| 0070 |[Climbing Stairs](src/main/kotlin/g0001_0100/s0070_climbing_stairs)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Math, Memoization | 124 | 71.98 | 0746 |[Min Cost Climbing Stairs](src/main/kotlin/g0701_0800/s0746_min_cost_climbing_stairs)| Easy | Array, Dynamic_Programming | 171 | 96.76 #### Day 3 @@ -1005,14 +1005,14 @@ | | | | | | |-|-|-|-|-|- -| 0055 |[Jump Game](src/main/kotlin/g0001_0100/s0055_jump_game)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Greedy | 670 | 66.67 +| 0055 |[Jump Game](src/main/kotlin/g0001_0100/s0055_jump_game)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Greedy | 332 | 89.35 | 0045 |[Jump Game II](src/main/kotlin/g0001_0100/s0045_jump_game_ii)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Greedy | 208 | 93.37 #### Day 5 | | | | | | |-|-|-|-|-|- -| 0053 |[Maximum Subarray](src/main/kotlin/g0001_0100/s0053_maximum_subarray)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Divide_and_Conquer | 662 | 82.48 +| 0053 |[Maximum Subarray](src/main/kotlin/g0001_0100/s0053_maximum_subarray)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Divide_and_Conquer | 510 | 78.81 | 0918 |[Maximum Sum Circular Subarray](src/main/kotlin/g0901_1000/s0918_maximum_sum_circular_subarray)| Medium | Array, Dynamic_Programming, Divide_and_Conquer, Queue, Monotonic_Queue | 339 | 86.96 #### Day 6 @@ -1049,14 +1049,14 @@ | | | | | | |-|-|-|-|-|- | 0413 |[Arithmetic Slices](src/main/kotlin/g0401_0500/s0413_arithmetic_slices)| Medium | Array, Dynamic_Programming | 156 | 100.00 -| 0091 |[Decode Ways](src/main/kotlin/g0001_0100/s0091_decode_ways)| Medium | Top_Interview_Questions, String, Dynamic_Programming | 237 | 76.88 +| 0091 |[Decode Ways](src/main/kotlin/g0001_0100/s0091_decode_ways)| Medium | Top_Interview_Questions, String, Dynamic_Programming | 148 | 79.07 #### Day 11 | | | | | | |-|-|-|-|-|- | 0264 |[Ugly Number II](src/main/kotlin/g0201_0300/s0264_ugly_number_ii)| Medium | Hash_Table, Dynamic_Programming, Math, Heap_Priority_Queue | 182 | 95.45 -| 0096 |[Unique Binary Search Trees](src/main/kotlin/g0001_0100/s0096_unique_binary_search_trees)| Medium | Top_100_Liked_Questions, Dynamic_Programming, Math, Tree, Binary_Tree, Binary_Search_Tree | 147 | 88.52 +| 0096 |[Unique Binary Search Trees](src/main/kotlin/g0001_0100/s0096_unique_binary_search_trees)| Medium | Top_100_Liked_Questions, Dynamic_Programming, Math, Tree, Binary_Tree, Binary_Search_Tree | 116 | 92.31 #### Day 12 @@ -1083,14 +1083,14 @@ | | | | | | |-|-|-|-|-|- -| 0062 |[Unique Paths](src/main/kotlin/g0001_0100/s0062_unique_paths)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Math, Combinatorics | 209 | 49.18 -| 0063 |[Unique Paths II](src/main/kotlin/g0001_0100/s0063_unique_paths_ii)| Medium | Array, Dynamic_Programming, Matrix | 187 | 84.62 +| 0062 |[Unique Paths](src/main/kotlin/g0001_0100/s0062_unique_paths)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Math, Combinatorics | 118 | 94.65 +| 0063 |[Unique Paths II](src/main/kotlin/g0001_0100/s0063_unique_paths_ii)| Medium | Array, Dynamic_Programming, Matrix | 151 | 81.94 #### Day 16 | | | | | | |-|-|-|-|-|- -| 0064 |[Minimum Path Sum](src/main/kotlin/g0001_0100/s0064_minimum_path_sum)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Matrix | 222 | 95.70 +| 0064 |[Minimum Path Sum](src/main/kotlin/g0001_0100/s0064_minimum_path_sum)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Matrix | 164 | 100.00 | 0221 |[Maximal Square](src/main/kotlin/g0201_0300/s0221_maximal_square)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Matrix | 614 | 44.00 #### Day 17 @@ -1113,7 +1113,7 @@ |-|-|-|-|-|- | 0392 |[Is Subsequence](src/main/kotlin/g0301_0400/s0392_is_subsequence)| Easy | String, Dynamic_Programming, Two_Pointers | 156 | 87.74 | 1143 |[Longest Common Subsequence](src/main/kotlin/g1101_1200/s1143_longest_common_subsequence)| Medium | Top_100_Liked_Questions, String, Dynamic_Programming | 307 | 38.36 -| 0072 |[Edit Distance](src/main/kotlin/g0001_0100/s0072_edit_distance)| Hard | Top_100_Liked_Questions, String, Dynamic_Programming | 320 | 63.53 +| 0072 |[Edit Distance](src/main/kotlin/g0001_0100/s0072_edit_distance)| Hard | Top_100_Liked_Questions, String, Dynamic_Programming | 182 | 92.16 #### Day 20 @@ -1207,7 +1207,7 @@ |-|-|-|-|-|- | 1290 |[Convert Binary Number in a Linked List to Integer](src/main/kotlin/g1201_1300/s1290_convert_binary_number_in_a_linked_list_to_integer)| Easy | Math, Linked_List | 145 | 25.93 | 0876 |[Middle of the Linked List](src/main/kotlin/g0801_0900/s0876_middle_of_the_linked_list)| Easy | Two_Pointers, Linked_List | 136 | 76.52 -| 0104 |[Maximum Depth of Binary Tree](src/main/kotlin/g0101_0200/s0104_maximum_depth_of_binary_tree)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree | 236 | 83.39 +| 0104 |[Maximum Depth of Binary Tree](src/main/kotlin/g0101_0200/s0104_maximum_depth_of_binary_tree)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree | 166 | 83.53 | 0404 |[Sum of Left Leaves](src/main/kotlin/g0401_0500/s0404_sum_of_left_leaves)| Easy | Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree | 173 | 86.05 #### Day 11 Containers and Libraries @@ -1239,7 +1239,7 @@ | | | | | | |-|-|-|-|-|- -| 0110 |[Balanced Binary Tree](src/main/kotlin/g0101_0200/s0110_balanced_binary_tree)| Easy | Depth_First_Search, Tree, Binary_Tree | 310 | 63.63 +| 0110 |[Balanced Binary Tree](src/main/kotlin/g0101_0200/s0110_balanced_binary_tree)| Easy | Depth_First_Search, Tree, Binary_Tree | 182 | 71.30 | 0459 |[Repeated Substring Pattern](src/main/kotlin/g0401_0500/s0459_repeated_substring_pattern)| Easy | String, String_Matching | 201 | 100.00 #### Day 3 @@ -1247,7 +1247,7 @@ | | | | | | |-|-|-|-|-|- | 0150 |[Evaluate Reverse Polish Notation](src/main/kotlin/g0101_0200/s0150_evaluate_reverse_polish_notation)| Medium | Top_Interview_Questions, Array, Math, Stack | 233 | 88.82 -| 0066 |[Plus One](src/main/kotlin/g0001_0100/s0066_plus_one)| Easy | Top_Interview_Questions, Array, Math | 303 | 35.18 +| 0066 |[Plus One](src/main/kotlin/g0001_0100/s0066_plus_one)| Easy | Top_Interview_Questions, Array, Math | 148 | 98.75 #### Day 4 @@ -1260,7 +1260,7 @@ | | | | | | |-|-|-|-|-|- -| 0067 |[Add Binary](src/main/kotlin/g0001_0100/s0067_add_binary)| Easy | String, Math, Bit_Manipulation, Simulation | 327 | 32.67 +| 0067 |[Add Binary](src/main/kotlin/g0001_0100/s0067_add_binary)| Easy | String, Math, Bit_Manipulation, Simulation | 164 | 90.60 | 0989 |[Add to Array-Form of Integer](src/main/kotlin/g0901_1000/s0989_add_to_array_form_of_integer)| Easy | Array, Math | 350 | 70.00 #### Day 6 @@ -1268,7 +1268,7 @@ | | | | | | |-|-|-|-|-|- | 0739 |[Daily Temperatures](src/main/kotlin/g0701_0800/s0739_daily_temperatures)| Medium | Top_100_Liked_Questions, Array, Stack, Monotonic_Stack | 936 | 80.54 -| 0058 |[Length of Last Word](src/main/kotlin/g0001_0100/s0058_length_of_last_word)| Easy | String | 243 | 63.33 +| 0058 |[Length of Last Word](src/main/kotlin/g0001_0100/s0058_length_of_last_word)| Easy | String | 135 | 93.67 #### Day 7 @@ -1281,7 +1281,7 @@ | | | | | | |-|-|-|-|-|- -| 0054 |[Spiral Matrix](src/main/kotlin/g0001_0100/s0054_spiral_matrix)| Medium | Top_Interview_Questions, Array, Matrix, Simulation | 224 | 62.50 +| 0054 |[Spiral Matrix](src/main/kotlin/g0001_0100/s0054_spiral_matrix)| Medium | Top_Interview_Questions, Array, Matrix, Simulation | 132 | 95.12 | 0973 |[K Closest Points to Origin](src/main/kotlin/g0901_1000/s0973_k_closest_points_to_origin)| Medium | Array, Math, Sorting, Heap_Priority_Queue, Divide_and_Conquer, Geometry, Quickselect | 800 | 37.89 #### Day 9 @@ -1337,7 +1337,7 @@ | | | | | | |-|-|-|-|-|- -| 0061 |[Rotate List](src/main/kotlin/g0001_0100/s0061_rotate_list)| Medium | Two_Pointers, Linked_List | 193 | 92.16 +| 0061 |[Rotate List](src/main/kotlin/g0001_0100/s0061_rotate_list)| Medium | Two_Pointers, Linked_List | 160 | 92.22 | 0173 |[Binary Search Tree Iterator](src/main/kotlin/g0101_0200/s0173_binary_search_tree_iterator)| Medium | Tree, Binary_Tree, Stack, Design, Binary_Search_Tree, Iterator | 563 | 46.91 #### Day 17 @@ -1600,7 +1600,7 @@ | | | | | | |-|-|-|-|-|- | 0589 |[N-ary Tree Preorder Traversal](src/main/kotlin/g0501_0600/s0589_n_ary_tree_preorder_traversal)| Easy | Depth_First_Search, Tree, Stack | 233 | 84.02 -| 0102 |[Binary Tree Level Order Traversal](src/main/kotlin/g0101_0200/s0102_binary_tree_level_order_traversal)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Breadth_First_Search, Tree, Binary_Tree | 332 | 67.53 +| 0102 |[Binary Tree Level Order Traversal](src/main/kotlin/g0101_0200/s0102_binary_tree_level_order_traversal)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Breadth_First_Search, Tree, Binary_Tree | 198 | 95.14 #### Day 7 Binary Search @@ -1613,7 +1613,7 @@ | | | | | | |-|-|-|-|-|- -| 0098 |[Validate Binary Search Tree](src/main/kotlin/g0001_0100/s0098_validate_binary_search_tree)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Tree, Binary_Tree, Binary_Search_Tree | 330 | 41.38 +| 0098 |[Validate Binary Search Tree](src/main/kotlin/g0001_0100/s0098_validate_binary_search_tree)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Tree, Binary_Tree, Binary_Search_Tree | 190 | 61.62 | 0235 |[Lowest Common Ancestor of a Binary Search Tree](src/main/kotlin/g0201_0300/s0235_lowest_common_ancestor_of_a_binary_search_tree)| Easy | Depth_First_Search, Tree, Binary_Tree, Binary_Search_Tree | 404 | 75.59 #### Day 9 Graph/BFS/DFS @@ -1628,14 +1628,14 @@ | | | | | | |-|-|-|-|-|- | 0509 |[Fibonacci Number](src/main/kotlin/g0501_0600/s0509_fibonacci_number)| Easy | Dynamic_Programming, Math, Recursion, Memoization | 139 | 82.72 -| 0070 |[Climbing Stairs](src/main/kotlin/g0001_0100/s0070_climbing_stairs)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Math, Memoization | 127 | 97.06 +| 0070 |[Climbing Stairs](src/main/kotlin/g0001_0100/s0070_climbing_stairs)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Math, Memoization | 124 | 71.98 #### Day 11 Dynamic Programming | | | | | | |-|-|-|-|-|- | 0746 |[Min Cost Climbing Stairs](src/main/kotlin/g0701_0800/s0746_min_cost_climbing_stairs)| Easy | Array, Dynamic_Programming | 171 | 96.76 -| 0062 |[Unique Paths](src/main/kotlin/g0001_0100/s0062_unique_paths)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Math, Combinatorics | 209 | 49.18 +| 0062 |[Unique Paths](src/main/kotlin/g0001_0100/s0062_unique_paths)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Math, Combinatorics | 118 | 94.65 #### Day 12 Sliding Window/Two Pointer @@ -1672,7 +1672,7 @@ | | | | | | |-|-|-|-|-|- | 0202 |[Happy Number](src/main/kotlin/g0201_0300/s0202_happy_number)| Easy | Top_Interview_Questions, Hash_Table, Math, Two_Pointers | 261 | 45.08 -| 0054 |[Spiral Matrix](src/main/kotlin/g0001_0100/s0054_spiral_matrix)| Medium | Top_Interview_Questions, Array, Matrix, Simulation | 224 | 62.50 +| 0054 |[Spiral Matrix](src/main/kotlin/g0001_0100/s0054_spiral_matrix)| Medium | Top_Interview_Questions, Array, Matrix, Simulation | 132 | 95.12 | 1706 |[Where Will the Ball Fall](src/main/kotlin/g1701_1800/s1706_where_will_the_ball_fall)| Medium | Array, Dynamic_Programming, Depth_First_Search, Matrix, Simulation | 270 | 67.86 #### Day 2 String @@ -1708,7 +1708,7 @@ | | | | | | |-|-|-|-|-|- | 0226 |[Invert Binary Tree](src/main/kotlin/g0201_0300/s0226_invert_binary_tree)| Easy | Top_100_Liked_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree | 233 | 54.90 -| 0110 |[Balanced Binary Tree](src/main/kotlin/g0101_0200/s0110_balanced_binary_tree)| Easy | Depth_First_Search, Tree, Binary_Tree | 310 | 63.63 +| 0110 |[Balanced Binary Tree](src/main/kotlin/g0101_0200/s0110_balanced_binary_tree)| Easy | Depth_First_Search, Tree, Binary_Tree | 182 | 71.30 #### Day 7 Tree @@ -1721,14 +1721,14 @@ | | | | | | |-|-|-|-|-|- -| 0074 |[Search a 2D Matrix](src/main/kotlin/g0001_0100/s0074_search_a_2d_matrix)| Medium | Top_100_Liked_Questions, Array, Binary_Search, Matrix | 290 | 40.17 +| 0074 |[Search a 2D Matrix](src/main/kotlin/g0001_0100/s0074_search_a_2d_matrix)| Medium | Top_100_Liked_Questions, Array, Binary_Search, Matrix | 159 | 74.46 | 0033 |[Search in Rotated Sorted Array](src/main/kotlin/g0001_0100/s0033_search_in_rotated_sorted_array)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Binary_Search | 151 | 93.77 #### Day 9 Binary Search Tree | | | | | | |-|-|-|-|-|- -| 0108 |[Convert Sorted Array to Binary Search Tree](src/main/kotlin/g0101_0200/s0108_convert_sorted_array_to_binary_search_tree)| Easy | Top_Interview_Questions, Array, Tree, Binary_Tree, Binary_Search_Tree, Divide_and_Conquer | 334 | 35.39 +| 0108 |[Convert Sorted Array to Binary Search Tree](src/main/kotlin/g0101_0200/s0108_convert_sorted_array_to_binary_search_tree)| Easy | Top_Interview_Questions, Array, Tree, Binary_Tree, Binary_Search_Tree, Divide_and_Conquer | 167 | 80.29 | 0230 |[Kth Smallest Element in a BST](src/main/kotlin/g0201_0300/s0230_kth_smallest_element_in_a_bst)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Tree, Binary_Tree, Binary_Search_Tree | 393 | 33.33 | 0173 |[Binary Search Tree Iterator](src/main/kotlin/g0101_0200/s0173_binary_search_tree_iterator)| Medium | Tree, Binary_Tree, Stack, Design, Binary_Search_Tree, Iterator | 563 | 46.91 @@ -1766,14 +1766,14 @@ |-|-|-|-|-|- | 0003 |[Longest Substring Without Repeating Characters](src/main/kotlin/g0001_0100/s0003_longest_substring_without_repeating_characters)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Sliding_Window | 201 | 87.28 | 0016 |[3Sum Closest](src/main/kotlin/g0001_0100/s0016_3sum_closest)| Medium | Array, Sorting, Two_Pointers | 163 | 100.00 -| 0076 |[Minimum Window Substring](src/main/kotlin/g0001_0100/s0076_minimum_window_substring)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Sliding_Window | 346 | 85.20 +| 0076 |[Minimum Window Substring](src/main/kotlin/g0001_0100/s0076_minimum_window_substring)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Sliding_Window | 191 | 96.38 #### Day 15 Tree | | | | | | |-|-|-|-|-|- -| 0100 |[Same Tree](src/main/kotlin/g0001_0100/s0100_same_tree)| Easy | Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree | 208 | 72.24 -| 0101 |[Symmetric Tree](src/main/kotlin/g0101_0200/s0101_symmetric_tree)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree | 190 | 91.36 +| 0100 |[Same Tree](src/main/kotlin/g0001_0100/s0100_same_tree)| Easy | Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree | 134 | 86.63 +| 0101 |[Symmetric Tree](src/main/kotlin/g0101_0200/s0101_symmetric_tree)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree | 153 | 82.35 | 0199 |[Binary Tree Right Side View](src/main/kotlin/g0101_0200/s0199_binary_tree_right_side_view)| Medium | Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree | 194 | 92.89 #### Day 16 Design @@ -1788,8 +1788,8 @@ | | | | | | |-|-|-|-|-|- -| 0057 |[Insert Interval](src/main/kotlin/g0001_0100/s0057_insert_interval)| Medium | Array | 257 | 99.52 -| 0056 |[Merge Intervals](src/main/kotlin/g0001_0100/s0056_merge_intervals)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting | 320 | 94.22 +| 0057 |[Insert Interval](src/main/kotlin/g0001_0100/s0057_insert_interval)| Medium | Array | 249 | 75.63 +| 0056 |[Merge Intervals](src/main/kotlin/g0001_0100/s0056_merge_intervals)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting | 334 | 80.67 #### Day 18 Stack @@ -1816,6 +1816,56 @@ | # | Title | Difficulty | Tag | Time, ms | Time, % |------|----------------|-------------|-------------|----------|-------- +| 2598 |[Smallest Missing Non-negative Integer After Operations](src/main/kotlin/g2501_2600/s2598_smallest_missing_non_negative_integer_after_operations)| ||| +| 2597 |[The Number of Beautiful Subsets](src/main/kotlin/g2501_2600/s2597_the_number_of_beautiful_subsets)| Medium | Array, Dynamic_Programming, Backtracking | 194 | 100.00 +| 2596 |[Check Knight Tour Configuration](src/main/kotlin/g2501_2600/s2596_check_knight_tour_configuration)| Medium | Array, Matrix, Simulation, Depth_First_Search, Breadth_First_Search | 179 | 100.00 +| 2595 |[Number of Even and Odd Bits](src/main/kotlin/g2501_2600/s2595_number_of_even_and_odd_bits)| Easy | Bit_Manipulation | 177 | 100.00 +| 2594 |[Minimum Time to Repair Cars](src/main/kotlin/g2501_2600/s2594_minimum_time_to_repair_cars)| ||| +| 2593 |[Find Score of an Array After Marking All Elements](src/main/kotlin/g2501_2600/s2593_find_score_of_an_array_after_marking_all_elements)| Medium | Array, Sorting, Heap_Priority_Queue, Simulation | 738 | 100.00 +| 2592 |[Maximize Greatness of an Array](src/main/kotlin/g2501_2600/s2592_maximize_greatness_of_an_array)| Medium | Array, Sorting, Greedy, Two_Pointers | 748 | 100.00 +| 2591 |[Distribute Money to Maximum Children](src/main/kotlin/g2501_2600/s2591_distribute_money_to_maximum_children)| Easy | Math, Greedy | 155 | 100.00 +| 2589 |[Minimum Time to Complete All Tasks](src/main/kotlin/g2501_2600/s2589_minimum_time_to_complete_all_tasks)| Hard | Array, Sorting, Greedy, Binary_Search, Stack | 422 | 100.00 +| 2588 |[Count the Number of Beautiful Subarrays](src/main/kotlin/g2501_2600/s2588_count_the_number_of_beautiful_subarrays)| Medium | Array, Hash_Table, Bit_Manipulation, Prefix_Sum | 785 | 100.00 +| 2587 |[Rearrange Array to Maximize Prefix Score](src/main/kotlin/g2501_2600/s2587_rearrange_array_to_maximize_prefix_score)| Medium | Array, Sorting, Greedy, Prefix_Sum | 612 | 100.00 +| 2586 |[Count the Number of Vowel Strings in Range](src/main/kotlin/g2501_2600/s2586_count_the_number_of_vowel_strings_in_range)| Easy | Array, String | 195 | 100.00 +| 2585 |[Number of Ways to Earn Points](src/main/kotlin/g2501_2600/s2585_number_of_ways_to_earn_points)| Hard | Array, Dynamic_Programming | 263 | 100.00 +| 2584 |[Split the Array to Make Coprime Products](src/main/kotlin/g2501_2600/s2584_split_the_array_to_make_coprime_products)| Hard | Array, Hash_Table, Math, Number_Theory | 341 | 100.00 +| 2583 |[Kth Largest Sum in a Binary Tree](src/main/kotlin/g2501_2600/s2583_kth_largest_sum_in_a_binary_tree)| Medium | Tree, Binary_Search, Breadth_First_Search | 557 | 83.33 +| 2582 |[Pass the Pillow](src/main/kotlin/g2501_2600/s2582_pass_the_pillow)| Easy | Math, Simulation | 122 | 85.71 +| 2581 |[Count Number of Possible Root Nodes](src/main/kotlin/g2501_2600/s2581_count_number_of_possible_root_nodes)| Hard | Hash_Table, Dynamic_Programming, Tree, Depth_First_Search | 1352 | 100.00 +| 2580 |[Count Ways to Group Overlapping Ranges](src/main/kotlin/g2501_2600/s2580_count_ways_to_group_overlapping_ranges)| Medium | Array, Sorting | 669 | 100.00 +| 2579 |[Count Total Number of Colored Cells](src/main/kotlin/g2501_2600/s2579_count_total_number_of_colored_cells)| Medium | Math | 109 | 100.00 +| 2578 |[Split With Minimum Sum](src/main/kotlin/g2501_2600/s2578_split_with_minimum_sum)| Easy | Math, Sorting, Greedy | 121 | 75.00 +| 2577 |[Minimum Time to Visit a Cell In a Grid](src/main/kotlin/g2501_2600/s2577_minimum_time_to_visit_a_cell_in_a_grid)| Hard | Array, Matrix, Heap_Priority_Queue, Graph, Shortest_Path, Breadth_First_Search | 1446 | 100.00 +| 2576 |[Find the Maximum Number of Marked Indices](src/main/kotlin/g2501_2600/s2576_find_the_maximum_number_of_marked_indices)| Medium | Array, Sorting, Greedy, Binary_Search, Two_Pointers | 610 | 100.00 +| 2575 |[Find the Divisibility Array of a String](src/main/kotlin/g2501_2600/s2575_find_the_divisibility_array_of_a_string)| Medium | Array, String, Math | 507 | 100.00 +| 2574 |[Left and Right Sum Differences](src/main/kotlin/g2501_2600/s2574_left_and_right_sum_differences)| Easy | Array, Prefix_Sum | 203 | 100.00 +| 2573 |[Find the String with LCP](src/main/kotlin/g2501_2600/s2573_find_the_string_with_lcp)| Hard | String, Dynamic_Programming, Greedy, Union_Find | 853 | 100.00 +| 2572 |[Count the Number of Square-Free Subsets](src/main/kotlin/g2501_2600/s2572_count_the_number_of_square_free_subsets)| Medium | Array, Dynamic_Programming, Math, Bit_Manipulation, Bitmask | 218 | 100.00 +| 2571 |[Minimum Operations to Reduce an Integer to 0](src/main/kotlin/g2501_2600/s2571_minimum_operations_to_reduce_an_integer_to_0)| Medium | Dynamic_Programming, Greedy, Bit_Manipulation | 131 | 100.00 +| 2570 |[Merge Two 2D Arrays by Summing Values](src/main/kotlin/g2501_2600/s2570_merge_two_2d_arrays_by_summing_values)| Easy | Array, Hash_Table, Two_Pointers | 221 | 91.67 +| 2569 |[Handling Sum Queries After Update](src/main/kotlin/g2501_2600/s2569_handling_sum_queries_after_update)| Hard | Array, Segment_Tree | 1751 | 100.00 +| 2568 |[Minimum Impossible OR](src/main/kotlin/g2501_2600/s2568_minimum_impossible_or)| Medium | Array, Bit_Manipulation, Brainteaser | 468 | 100.00 +| 2567 |[Minimum Score by Changing Two Elements](src/main/kotlin/g2501_2600/s2567_minimum_score_by_changing_two_elements)| Medium | Array, Sorting, Greedy | 496 | 100.00 +| 2566 |[Maximum Difference by Remapping a Digit](src/main/kotlin/g2501_2600/s2566_maximum_difference_by_remapping_a_digit)| Easy | Math, Greedy | 125 | 100.00 +| 2565 |[Subsequence With the Minimum Score](src/main/kotlin/g2501_2600/s2565_subsequence_with_the_minimum_score)| Hard | String, Binary_Search, Two_Pointers | 204 | 100.00 +| 2564 |[Substring XOR Queries](src/main/kotlin/g2501_2600/s2564_substring_xor_queries)| Medium | Array, String, Hash_Table, Bit_Manipulation | 1603 | 100.00 +| 2563 |[Count the Number of Fair Pairs](src/main/kotlin/g2501_2600/s2563_count_the_number_of_fair_pairs)| Medium | Array, Sorting, Binary_Search, Two_Pointers | 553 | 100.00 +| 2562 |[Find the Array Concatenation Value](src/main/kotlin/g2501_2600/s2562_find_the_array_concatenation_value)| Easy | Array, Two_Pointers, Simulation | 175 | 100.00 +| 2561 |[Rearranging Fruits](src/main/kotlin/g2501_2600/s2561_rearranging_fruits)| Hard | Array, Hash_Table, Greedy | 746 | 100.00 +| 2560 |[House Robber IV](src/main/kotlin/g2501_2600/s2560_house_robber_iv)| Medium | Array, Binary_Search | 467 | 100.00 +| 2559 |[Count Vowel Strings in Ranges](src/main/kotlin/g2501_2600/s2559_count_vowel_strings_in_ranges)| Medium | Array, String, Prefix_Sum | 638 | 100.00 +| 2558 |[Take Gifts From the Richest Pile](src/main/kotlin/g2501_2600/s2558_take_gifts_from_the_richest_pile)| Easy | Array, Heap_Priority_Queue, Simulation | 169 | 100.00 +| 2556 |[Disconnect Path in a Binary Matrix by at Most One Flip](src/main/kotlin/g2501_2600/s2556_disconnect_path_in_a_binary_matrix_by_at_most_one_flip)| Medium | Array, Dynamic_Programming, Matrix, Depth_First_Search, Breadth_First_Search | 571 | 100.00 +| 2555 |[Maximize Win From Two Segments](src/main/kotlin/g2501_2600/s2555_maximize_win_from_two_segments)| Medium | Array, Binary_Search, Sliding_Window | 388 | 100.00 +| 2554 |[Maximum Number of Integers to Choose From a Range I](src/main/kotlin/g2501_2600/s2554_maximum_number_of_integers_to_choose_from_a_range_i)| Medium | Array, Hash_Table, Sorting, Greedy, Binary_Search | 543 | 100.00 +| 2553 |[Separate the Digits in an Array](src/main/kotlin/g2501_2600/s2553_separate_the_digits_in_an_array)| Easy | Array, Simulation | 210 | 100.00 +| 2552 |[Count Increasing Quadruplets](src/main/kotlin/g2501_2600/s2552_count_increasing_quadruplets)| Hard | Array, Dynamic_Programming, Prefix_Sum, Enumeration, Binary_Indexed_Tree | 268 | 66.67 +| 2551 |[Put Marbles in Bags](src/main/kotlin/g2501_2600/s2551_put_marbles_in_bags)| Hard | Array, Sorting, Greedy, Heap_Priority_Queue | 818 | 100.00 +| 2550 |[Count Collisions of Monkeys on a Polygon](src/main/kotlin/g2501_2600/s2550_count_collisions_of_monkeys_on_a_polygon)| Medium | Math, Recursion | 111 | 100.00 +| 2549 |[Count Distinct Numbers on Board](src/main/kotlin/g2501_2600/s2549_count_distinct_numbers_on_board)| Easy | Array, Hash_Table, Math, Simulation | 113 | 100.00 +| 2547 |[Minimum Cost to Split an Array](src/main/kotlin/g2501_2600/s2547_minimum_cost_to_split_an_array)| Hard | Array, Hash_Table, Dynamic_Programming, Counting | 400 | 100.00 +| 2546 |[Apply Bitwise Operations to Make Strings Equal](src/main/kotlin/g2501_2600/s2546_apply_bitwise_operations_to_make_strings_equal)| Medium | String, Bit_Manipulation | 246 | 100.00 | 2545 |[Sort the Students by Their Kth Score](src/main/kotlin/g2501_2600/s2545_sort_the_students_by_their_kth_score)| Medium | Array, Sorting, Matrix | 442 | 100.00 | 2544 |[Alternating Digit Sum](src/main/kotlin/g2501_2600/s2544_alternating_digit_sum)| Easy | Math | 122 | 60.00 | 2543 |[Check if Point Is Reachable](src/main/kotlin/g2501_2600/s2543_check_if_point_is_reachable)| Hard | Math, Number_Theory | 134 | 100.00 @@ -3754,66 +3804,66 @@ | 0113 |[Path Sum II](src/main/kotlin/g0101_0200/s0113_path_sum_ii)| Medium | Depth_First_Search, Tree, Binary_Tree, Backtracking, Data_Structure_II_Day_16_Tree | 364 | 78.67 | 0112 |[Path Sum](src/main/kotlin/g0101_0200/s0112_path_sum)| Easy | Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Data_Structure_I_Day_12_Tree | 322 | 36.41 | 0111 |[Minimum Depth of Binary Tree](src/main/kotlin/g0101_0200/s0111_minimum_depth_of_binary_tree)| Easy | Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Udemy_Tree_Stack_Queue | 525 | 90.51 -| 0110 |[Balanced Binary Tree](src/main/kotlin/g0101_0200/s0110_balanced_binary_tree)| Easy | Depth_First_Search, Tree, Binary_Tree, Programming_Skills_II_Day_2, Level_2_Day_6_Tree, Udemy_Tree_Stack_Queue | 310 | 63.63 -| 0109 |[Convert Sorted List to Binary Search Tree](src/main/kotlin/g0101_0200/s0109_convert_sorted_list_to_binary_search_tree)| Medium | Tree, Binary_Tree, Linked_List, Binary_Search_Tree, Divide_and_Conquer | 376 | 59.26 -| 0108 |[Convert Sorted Array to Binary Search Tree](src/main/kotlin/g0101_0200/s0108_convert_sorted_array_to_binary_search_tree)| Easy | Top_Interview_Questions, Array, Tree, Binary_Tree, Binary_Search_Tree, Divide_and_Conquer, Data_Structure_II_Day_15_Tree, Level_2_Day_9_Binary_Search_Tree, Udemy_Tree_Stack_Queue | 334 | 35.39 -| 0107 |[Binary Tree Level Order Traversal II](src/main/kotlin/g0101_0200/s0107_binary_tree_level_order_traversal_ii)| Medium | Breadth_First_Search, Tree, Binary_Tree | 204 | 98.04 -| 0106 |[Construct Binary Tree from Inorder and Postorder Traversal](src/main/kotlin/g0101_0200/s0106_construct_binary_tree_from_inorder_and_postorder_traversal)| Medium | Array, Hash_Table, Tree, Binary_Tree, Divide_and_Conquer | 358 | 61.29 -| 0105 |[Construct Binary Tree from Preorder and Inorder Traversal](src/main/kotlin/g0101_0200/s0105_construct_binary_tree_from_preorder_and_inorder_traversal)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Tree, Binary_Tree, Divide_and_Conquer, Data_Structure_II_Day_15_Tree | 370 | 58.31 -| 0104 |[Maximum Depth of Binary Tree](src/main/kotlin/g0101_0200/s0104_maximum_depth_of_binary_tree)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Data_Structure_I_Day_11_Tree, Programming_Skills_I_Day_10_Linked_List_and_Tree, Udemy_Tree_Stack_Queue | 236 | 83.39 -| 0103 |[Binary Tree Zigzag Level Order Traversal](src/main/kotlin/g0101_0200/s0103_binary_tree_zigzag_level_order_traversal)| Medium | Top_Interview_Questions, Breadth_First_Search, Tree, Binary_Tree, Data_Structure_II_Day_15_Tree, Udemy_Tree_Stack_Queue | 316 | 34.25 -| 0102 |[Binary Tree Level Order Traversal](src/main/kotlin/g0101_0200/s0102_binary_tree_level_order_traversal)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Breadth_First_Search, Tree, Binary_Tree, Data_Structure_I_Day_11_Tree, Level_1_Day_6_Tree, Udemy_Tree_Stack_Queue | 332 | 67.53 -| 0101 |[Symmetric Tree](src/main/kotlin/g0101_0200/s0101_symmetric_tree)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Data_Structure_I_Day_11_Tree, Level_2_Day_15_Tree | 190 | 91.36 -| 0100 |[Same Tree](src/main/kotlin/g0001_0100/s0100_same_tree)| Easy | Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Level_2_Day_15_Tree, Udemy_Tree_Stack_Queue | 208 | 72.24 -| 0099 |[Recover Binary Search Tree](src/main/kotlin/g0001_0100/s0099_recover_binary_search_tree)| Medium | Depth_First_Search, Tree, Binary_Tree, Binary_Search_Tree | 492 | 39.39 -| 0098 |[Validate Binary Search Tree](src/main/kotlin/g0001_0100/s0098_validate_binary_search_tree)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Tree, Binary_Tree, Binary_Search_Tree, Data_Structure_I_Day_14_Tree, Level_1_Day_8_Binary_Search_Tree, Udemy_Tree_Stack_Queue | 330 | 41.38 -| 0097 |[Interleaving String](src/main/kotlin/g0001_0100/s0097_interleaving_string)| Medium | String, Dynamic_Programming | 240 | 57.50 -| 0096 |[Unique Binary Search Trees](src/main/kotlin/g0001_0100/s0096_unique_binary_search_trees)| Medium | Top_100_Liked_Questions, Dynamic_Programming, Math, Tree, Binary_Tree, Binary_Search_Tree, Dynamic_Programming_I_Day_11 | 147 | 88.52 -| 0095 |[Unique Binary Search Trees II](src/main/kotlin/g0001_0100/s0095_unique_binary_search_trees_ii)| Medium | Dynamic_Programming, Tree, Binary_Tree, Backtracking, Binary_Search_Tree | 360 | 41.38 -| 0094 |[Binary Tree Inorder Traversal](src/main/kotlin/g0001_0100/s0094_binary_tree_inorder_traversal)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Tree, Binary_Tree, Stack, Data_Structure_I_Day_10_Tree, Udemy_Tree_Stack_Queue | 269 | 38.80 -| 0093 |[Restore IP Addresses](src/main/kotlin/g0001_0100/s0093_restore_ip_addresses)| Medium | String, Backtracking | 304 | 73.33 -| 0092 |[Reverse Linked List II](src/main/kotlin/g0001_0100/s0092_reverse_linked_list_ii)| Medium | Linked_List | 191 | 82.35 -| 0091 |[Decode Ways](src/main/kotlin/g0001_0100/s0091_decode_ways)| Medium | Top_Interview_Questions, String, Dynamic_Programming, Algorithm_II_Day_15_Dynamic_Programming, Dynamic_Programming_I_Day_10 | 237 | 76.88 -| 0090 |[Subsets II](src/main/kotlin/g0001_0100/s0090_subsets_ii)| Medium | Array, Bit_Manipulation, Backtracking, Algorithm_II_Day_9_Recursion_Backtracking | 366 | 58.09 -| 0089 |[Gray Code](src/main/kotlin/g0001_0100/s0089_gray_code)| Medium | Math, Bit_Manipulation, Backtracking | 273 | 100.00 -| 0088 |[Merge Sorted Array](src/main/kotlin/g0001_0100/s0088_merge_sorted_array)| Easy | Top_Interview_Questions, Array, Sorting, Two_Pointers, Data_Structure_I_Day_2_Array | 311 | 33.40 -| 0087 |[Scramble String](src/main/kotlin/g0001_0100/s0087_scramble_string)| Hard | String, Dynamic_Programming | 366 | 85.00 -| 0086 |[Partition List](src/main/kotlin/g0001_0100/s0086_partition_list)| Medium | Two_Pointers, Linked_List | 172 | 94.00 -| 0085 |[Maximal Rectangle](src/main/kotlin/g0001_0100/s0085_maximal_rectangle)| Hard | Array, Dynamic_Programming, Matrix, Stack, Monotonic_Stack | 463 | 55.17 -| 0084 |[Largest Rectangle in Histogram](src/main/kotlin/g0001_0100/s0084_largest_rectangle_in_histogram)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Array, Stack, Monotonic_Stack | 525 | 100.00 -| 0083 |[Remove Duplicates from Sorted List](src/main/kotlin/g0001_0100/s0083_remove_duplicates_from_sorted_list)| Easy | Linked_List, Data_Structure_I_Day_8_Linked_List | 274 | 77.82 -| 0082 |[Remove Duplicates from Sorted List II](src/main/kotlin/g0001_0100/s0082_remove_duplicates_from_sorted_list_ii)| Medium | Two_Pointers, Linked_List, Data_Structure_II_Day_11_Linked_List, Algorithm_II_Day_3_Two_Pointers | 241 | 91.04 -| 0081 |[Search in Rotated Sorted Array II](src/main/kotlin/g0001_0100/s0081_search_in_rotated_sorted_array_ii)| Medium | Array, Binary_Search, Binary_Search_II_Day_12 | 352 | 42.31 -| 0080 |[Remove Duplicates from Sorted Array II](src/main/kotlin/g0001_0100/s0080_remove_duplicates_from_sorted_array_ii)| Medium | Array, Two_Pointers, Udemy_Arrays | 357 | 44.78 -| 0079 |[Word Search](src/main/kotlin/g0001_0100/s0079_word_search)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Matrix, Backtracking, Algorithm_II_Day_11_Recursion_Backtracking | 463 | 68.49 -| 0078 |[Subsets](src/main/kotlin/g0001_0100/s0078_subsets)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Bit_Manipulation, Backtracking, Algorithm_II_Day_9_Recursion_Backtracking, Udemy_Backtracking/Recursion | 191 | 97.44 -| 0077 |[Combinations](src/main/kotlin/g0001_0100/s0077_combinations)| Medium | Backtracking, Algorithm_I_Day_11_Recursion_Backtracking | 244 | 100.00 -| 0076 |[Minimum Window Substring](src/main/kotlin/g0001_0100/s0076_minimum_window_substring)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Sliding_Window, Level_2_Day_14_Sliding_Window/Two_Pointer | 346 | 85.20 -| 0075 |[Sort Colors](src/main/kotlin/g0001_0100/s0075_sort_colors)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Two_Pointers, Data_Structure_II_Day_2_Array, Udemy_Arrays | 198 | 85.66 -| 0074 |[Search a 2D Matrix](src/main/kotlin/g0001_0100/s0074_search_a_2d_matrix)| Medium | Top_100_Liked_Questions, Array, Binary_Search, Matrix, Data_Structure_I_Day_5_Array, Algorithm_II_Day_1_Binary_Search, Binary_Search_I_Day_8, Level_2_Day_8_Binary_Search, Udemy_2D_Arrays/Matrix | 290 | 40.17 -| 0073 |[Set Matrix Zeroes](src/main/kotlin/g0001_0100/s0073_set_matrix_zeroes)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Matrix, Udemy_2D_Arrays/Matrix | 255 | 100.00 -| 0072 |[Edit Distance](src/main/kotlin/g0001_0100/s0072_edit_distance)| Hard | Top_100_Liked_Questions, String, Dynamic_Programming, Algorithm_II_Day_18_Dynamic_Programming, Dynamic_Programming_I_Day_19, Udemy_Dynamic_Programming | 320 | 63.53 -| 0071 |[Simplify Path](src/main/kotlin/g0001_0100/s0071_simplify_path)| Medium | String, Stack | 318 | 64.44 -| 0070 |[Climbing Stairs](src/main/kotlin/g0001_0100/s0070_climbing_stairs)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Math, Memoization, Algorithm_I_Day_12_Dynamic_Programming, Dynamic_Programming_I_Day_2, Level_1_Day_10_Dynamic_Programming, Udemy_Dynamic_Programming | 127 | 97.06 -| 0069 |[Sqrt(x)](src/main/kotlin/g0001_0100/s0069_sqrtx)| Easy | Top_Interview_Questions, Math, Binary_Search, Binary_Search_I_Day_4 | 153 | 95.75 -| 0068 |[Text Justification](src/main/kotlin/g0001_0100/s0068_text_justification)| Hard | Array, String, Simulation | 269 | 50.00 -| 0067 |[Add Binary](src/main/kotlin/g0001_0100/s0067_add_binary)| Easy | String, Math, Bit_Manipulation, Simulation, Programming_Skills_II_Day_5 | 327 | 32.67 -| 0066 |[Plus One](src/main/kotlin/g0001_0100/s0066_plus_one)| Easy | Top_Interview_Questions, Array, Math, Programming_Skills_II_Day_3, Udemy_Arrays | 303 | 35.18 -| 0065 |[Valid Number](src/main/kotlin/g0001_0100/s0065_valid_number)| Hard | String | 310 | 81.82 -| 0064 |[Minimum Path Sum](src/main/kotlin/g0001_0100/s0064_minimum_path_sum)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Matrix, Dynamic_Programming_I_Day_16, Udemy_Dynamic_Programming | 222 | 95.70 -| 0063 |[Unique Paths II](src/main/kotlin/g0001_0100/s0063_unique_paths_ii)| Medium | Array, Dynamic_Programming, Matrix, Dynamic_Programming_I_Day_15 | 187 | 84.62 -| 0062 |[Unique Paths](src/main/kotlin/g0001_0100/s0062_unique_paths)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Math, Combinatorics, Algorithm_II_Day_13_Dynamic_Programming, Dynamic_Programming_I_Day_15, Level_1_Day_11_Dynamic_Programming | 209 | 49.18 -| 0061 |[Rotate List](src/main/kotlin/g0001_0100/s0061_rotate_list)| Medium | Two_Pointers, Linked_List, Programming_Skills_II_Day_16, Udemy_Linked_List | 193 | 92.16 -| 0060 |[Permutation Sequence](src/main/kotlin/g0001_0100/s0060_permutation_sequence)| Hard | Math, Recursion | 146 | 100.00 -| 0059 |[Spiral Matrix II](src/main/kotlin/g0001_0100/s0059_spiral_matrix_ii)| Medium | Array, Matrix, Simulation, Data_Structure_II_Day_3_Array | 153 | 100.00 -| 0058 |[Length of Last Word](src/main/kotlin/g0001_0100/s0058_length_of_last_word)| Easy | String, Programming_Skills_II_Day_6, Udemy_Arrays | 243 | 63.33 -| 0057 |[Insert Interval](src/main/kotlin/g0001_0100/s0057_insert_interval)| Medium | Array, Level_2_Day_17_Interval | 257 | 99.52 -| 0056 |[Merge Intervals](src/main/kotlin/g0001_0100/s0056_merge_intervals)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Data_Structure_II_Day_2_Array, Level_2_Day_17_Interval, Udemy_2D_Arrays/Matrix | 320 | 94.22 -| 0055 |[Jump Game](src/main/kotlin/g0001_0100/s0055_jump_game)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Greedy, Algorithm_II_Day_12_Dynamic_Programming, Dynamic_Programming_I_Day_4, Udemy_Arrays | 670 | 66.67 -| 0054 |[Spiral Matrix](src/main/kotlin/g0001_0100/s0054_spiral_matrix)| Medium | Top_Interview_Questions, Array, Matrix, Simulation, Programming_Skills_II_Day_8, Level_2_Day_1_Implementation/Simulation, Udemy_2D_Arrays/Matrix | 224 | 62.50 -| 0053 |[Maximum Subarray](src/main/kotlin/g0001_0100/s0053_maximum_subarray)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Divide_and_Conquer, Data_Structure_I_Day_1_Array, Dynamic_Programming_I_Day_5, Udemy_Famous_Algorithm | 662 | 82.48 -| 0052 |[N-Queens II](src/main/kotlin/g0001_0100/s0052_n_queens_ii)| Hard | Backtracking | 231 | 80.00 -| 0051 |[N-Queens](src/main/kotlin/g0001_0100/s0051_n_queens)| Hard | Top_100_Liked_Questions, Array, Backtracking | 243 | 95.10 +| 0110 |[Balanced Binary Tree](src/main/kotlin/g0101_0200/s0110_balanced_binary_tree)| Easy | Depth_First_Search, Tree, Binary_Tree, Programming_Skills_II_Day_2, Level_2_Day_6_Tree, Udemy_Tree_Stack_Queue | 182 | 71.30 +| 0109 |[Convert Sorted List to Binary Search Tree](src/main/kotlin/g0101_0200/s0109_convert_sorted_list_to_binary_search_tree)| Medium | Tree, Binary_Tree, Linked_List, Binary_Search_Tree, Divide_and_Conquer | 191 | 100.00 +| 0108 |[Convert Sorted Array to Binary Search Tree](src/main/kotlin/g0101_0200/s0108_convert_sorted_array_to_binary_search_tree)| Easy | Top_Interview_Questions, Array, Tree, Binary_Tree, Binary_Search_Tree, Divide_and_Conquer, Data_Structure_II_Day_15_Tree, Level_2_Day_9_Binary_Search_Tree, Udemy_Tree_Stack_Queue | 167 | 80.29 +| 0107 |[Binary Tree Level Order Traversal II](src/main/kotlin/g0101_0200/s0107_binary_tree_level_order_traversal_ii)| Medium | Breadth_First_Search, Tree, Binary_Tree | 187 | 87.50 +| 0106 |[Construct Binary Tree from Inorder and Postorder Traversal](src/main/kotlin/g0101_0200/s0106_construct_binary_tree_from_inorder_and_postorder_traversal)| Medium | Array, Hash_Table, Tree, Binary_Tree, Divide_and_Conquer | 167 | 100.00 +| 0105 |[Construct Binary Tree from Preorder and Inorder Traversal](src/main/kotlin/g0101_0200/s0105_construct_binary_tree_from_preorder_and_inorder_traversal)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Tree, Binary_Tree, Divide_and_Conquer, Data_Structure_II_Day_15_Tree | 183 | 95.45 +| 0104 |[Maximum Depth of Binary Tree](src/main/kotlin/g0101_0200/s0104_maximum_depth_of_binary_tree)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Data_Structure_I_Day_11_Tree, Programming_Skills_I_Day_10_Linked_List_and_Tree, Udemy_Tree_Stack_Queue | 166 | 83.53 +| 0103 |[Binary Tree Zigzag Level Order Traversal](src/main/kotlin/g0101_0200/s0103_binary_tree_zigzag_level_order_traversal)| Medium | Top_Interview_Questions, Breadth_First_Search, Tree, Binary_Tree, Data_Structure_II_Day_15_Tree, Udemy_Tree_Stack_Queue | 176 | 71.62 +| 0102 |[Binary Tree Level Order Traversal](src/main/kotlin/g0101_0200/s0102_binary_tree_level_order_traversal)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Breadth_First_Search, Tree, Binary_Tree, Data_Structure_I_Day_11_Tree, Level_1_Day_6_Tree, Udemy_Tree_Stack_Queue | 198 | 95.14 +| 0101 |[Symmetric Tree](src/main/kotlin/g0101_0200/s0101_symmetric_tree)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Data_Structure_I_Day_11_Tree, Level_2_Day_15_Tree | 153 | 82.35 +| 0100 |[Same Tree](src/main/kotlin/g0001_0100/s0100_same_tree)| Easy | Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Level_2_Day_15_Tree, Udemy_Tree_Stack_Queue | 134 | 86.63 +| 0099 |[Recover Binary Search Tree](src/main/kotlin/g0001_0100/s0099_recover_binary_search_tree)| Medium | Depth_First_Search, Tree, Binary_Tree, Binary_Search_Tree | 221 | 90.00 +| 0098 |[Validate Binary Search Tree](src/main/kotlin/g0001_0100/s0098_validate_binary_search_tree)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Tree, Binary_Tree, Binary_Search_Tree, Data_Structure_I_Day_14_Tree, Level_1_Day_8_Binary_Search_Tree, Udemy_Tree_Stack_Queue | 190 | 61.62 +| 0097 |[Interleaving String](src/main/kotlin/g0001_0100/s0097_interleaving_string)| Medium | String, Dynamic_Programming | 149 | 81.58 +| 0096 |[Unique Binary Search Trees](src/main/kotlin/g0001_0100/s0096_unique_binary_search_trees)| Medium | Top_100_Liked_Questions, Dynamic_Programming, Math, Tree, Binary_Tree, Binary_Search_Tree, Dynamic_Programming_I_Day_11 | 116 | 92.31 +| 0095 |[Unique Binary Search Trees II](src/main/kotlin/g0001_0100/s0095_unique_binary_search_trees_ii)| Medium | Dynamic_Programming, Tree, Binary_Tree, Backtracking, Binary_Search_Tree | 167 | 100.00 +| 0094 |[Binary Tree Inorder Traversal](src/main/kotlin/g0001_0100/s0094_binary_tree_inorder_traversal)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Tree, Binary_Tree, Stack, Data_Structure_I_Day_10_Tree, Udemy_Tree_Stack_Queue | 152 | 66.67 +| 0093 |[Restore IP Addresses](src/main/kotlin/g0001_0100/s0093_restore_ip_addresses)| Medium | String, Backtracking | 152 | 100.00 +| 0092 |[Reverse Linked List II](src/main/kotlin/g0001_0100/s0092_reverse_linked_list_ii)| Medium | Linked_List | 144 | 75.00 +| 0091 |[Decode Ways](src/main/kotlin/g0001_0100/s0091_decode_ways)| Medium | Top_Interview_Questions, String, Dynamic_Programming, Algorithm_II_Day_15_Dynamic_Programming, Dynamic_Programming_I_Day_10 | 148 | 79.07 +| 0090 |[Subsets II](src/main/kotlin/g0001_0100/s0090_subsets_ii)| Medium | Array, Bit_Manipulation, Backtracking, Algorithm_II_Day_9_Recursion_Backtracking | 200 | 88.33 +| 0089 |[Gray Code](src/main/kotlin/g0001_0100/s0089_gray_code)| Medium | Math, Bit_Manipulation, Backtracking | 271 | 69.23 +| 0088 |[Merge Sorted Array](src/main/kotlin/g0001_0100/s0088_merge_sorted_array)| Easy | Top_Interview_Questions, Array, Sorting, Two_Pointers, Data_Structure_I_Day_2_Array | 170 | 69.74 +| 0087 |[Scramble String](src/main/kotlin/g0001_0100/s0087_scramble_string)| Hard | String, Dynamic_Programming | 174 | 87.50 +| 0086 |[Partition List](src/main/kotlin/g0001_0100/s0086_partition_list)| Medium | Two_Pointers, Linked_List | 160 | 70.73 +| 0085 |[Maximal Rectangle](src/main/kotlin/g0001_0100/s0085_maximal_rectangle)| Hard | Array, Dynamic_Programming, Matrix, Stack, Monotonic_Stack | 209 | 100.00 +| 0084 |[Largest Rectangle in Histogram](src/main/kotlin/g0001_0100/s0084_largest_rectangle_in_histogram)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Array, Stack, Monotonic_Stack | 476 | 90.79 +| 0083 |[Remove Duplicates from Sorted List](src/main/kotlin/g0001_0100/s0083_remove_duplicates_from_sorted_list)| Easy | Linked_List, Data_Structure_I_Day_8_Linked_List | 173 | 82.42 +| 0082 |[Remove Duplicates from Sorted List II](src/main/kotlin/g0001_0100/s0082_remove_duplicates_from_sorted_list_ii)| Medium | Two_Pointers, Linked_List, Data_Structure_II_Day_11_Linked_List, Algorithm_II_Day_3_Two_Pointers | 166 | 89.47 +| 0081 |[Search in Rotated Sorted Array II](src/main/kotlin/g0001_0100/s0081_search_in_rotated_sorted_array_ii)| Medium | Array, Binary_Search, Binary_Search_II_Day_12 | 170 | 96.30 +| 0080 |[Remove Duplicates from Sorted Array II](src/main/kotlin/g0001_0100/s0080_remove_duplicates_from_sorted_array_ii)| Medium | Array, Two_Pointers, Udemy_Arrays | 192 | 80.67 +| 0079 |[Word Search](src/main/kotlin/g0001_0100/s0079_word_search)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Matrix, Backtracking, Algorithm_II_Day_11_Recursion_Backtracking | 392 | 72.92 +| 0078 |[Subsets](src/main/kotlin/g0001_0100/s0078_subsets)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Bit_Manipulation, Backtracking, Algorithm_II_Day_9_Recursion_Backtracking, Udemy_Backtracking/Recursion | 171 | 94.92 +| 0077 |[Combinations](src/main/kotlin/g0001_0100/s0077_combinations)| Medium | Backtracking, Algorithm_I_Day_11_Recursion_Backtracking | 232 | 100.00 +| 0076 |[Minimum Window Substring](src/main/kotlin/g0001_0100/s0076_minimum_window_substring)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Sliding_Window, Level_2_Day_14_Sliding_Window/Two_Pointer | 191 | 96.38 +| 0075 |[Sort Colors](src/main/kotlin/g0001_0100/s0075_sort_colors)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Two_Pointers, Data_Structure_II_Day_2_Array, Udemy_Arrays | 164 | 64.43 +| 0074 |[Search a 2D Matrix](src/main/kotlin/g0001_0100/s0074_search_a_2d_matrix)| Medium | Top_100_Liked_Questions, Array, Binary_Search, Matrix, Data_Structure_I_Day_5_Array, Algorithm_II_Day_1_Binary_Search, Binary_Search_I_Day_8, Level_2_Day_8_Binary_Search, Udemy_2D_Arrays/Matrix | 159 | 74.46 +| 0073 |[Set Matrix Zeroes](src/main/kotlin/g0001_0100/s0073_set_matrix_zeroes)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Matrix, Udemy_2D_Arrays/Matrix | 248 | 97.33 +| 0072 |[Edit Distance](src/main/kotlin/g0001_0100/s0072_edit_distance)| Hard | Top_100_Liked_Questions, String, Dynamic_Programming, Algorithm_II_Day_18_Dynamic_Programming, Dynamic_Programming_I_Day_19, Udemy_Dynamic_Programming | 182 | 92.16 +| 0071 |[Simplify Path](src/main/kotlin/g0001_0100/s0071_simplify_path)| Medium | String, Stack | 185 | 92.86 +| 0070 |[Climbing Stairs](src/main/kotlin/g0001_0100/s0070_climbing_stairs)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Math, Memoization, Algorithm_I_Day_12_Dynamic_Programming, Dynamic_Programming_I_Day_2, Level_1_Day_10_Dynamic_Programming, Udemy_Dynamic_Programming | 124 | 71.98 +| 0069 |[Sqrt(x)](src/main/kotlin/g0001_0100/s0069_sqrtx)| Easy | Top_Interview_Questions, Math, Binary_Search, Binary_Search_I_Day_4 | 140 | 94.72 +| 0068 |[Text Justification](src/main/kotlin/g0001_0100/s0068_text_justification)| Hard | Array, String, Simulation | 153 | 92.96 +| 0067 |[Add Binary](src/main/kotlin/g0001_0100/s0067_add_binary)| Easy | String, Math, Bit_Manipulation, Simulation, Programming_Skills_II_Day_5 | 164 | 90.60 +| 0066 |[Plus One](src/main/kotlin/g0001_0100/s0066_plus_one)| Easy | Top_Interview_Questions, Array, Math, Programming_Skills_II_Day_3, Udemy_Arrays | 148 | 98.75 +| 0065 |[Valid Number](src/main/kotlin/g0001_0100/s0065_valid_number)| Hard | String | 151 | 100.00 +| 0064 |[Minimum Path Sum](src/main/kotlin/g0001_0100/s0064_minimum_path_sum)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Matrix, Dynamic_Programming_I_Day_16, Udemy_Dynamic_Programming | 164 | 100.00 +| 0063 |[Unique Paths II](src/main/kotlin/g0001_0100/s0063_unique_paths_ii)| Medium | Array, Dynamic_Programming, Matrix, Dynamic_Programming_I_Day_15 | 151 | 81.94 +| 0062 |[Unique Paths](src/main/kotlin/g0001_0100/s0062_unique_paths)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Math, Combinatorics, Algorithm_II_Day_13_Dynamic_Programming, Dynamic_Programming_I_Day_15, Level_1_Day_11_Dynamic_Programming | 118 | 94.65 +| 0061 |[Rotate List](src/main/kotlin/g0001_0100/s0061_rotate_list)| Medium | Two_Pointers, Linked_List, Programming_Skills_II_Day_16, Udemy_Linked_List | 160 | 92.22 +| 0060 |[Permutation Sequence](src/main/kotlin/g0001_0100/s0060_permutation_sequence)| Hard | Math, Recursion | 132 | 100.00 +| 0059 |[Spiral Matrix II](src/main/kotlin/g0001_0100/s0059_spiral_matrix_ii)| Medium | Array, Matrix, Simulation, Data_Structure_II_Day_3_Array | 153 | 41.70 +| 0058 |[Length of Last Word](src/main/kotlin/g0001_0100/s0058_length_of_last_word)| Easy | String, Programming_Skills_II_Day_6, Udemy_Arrays | 135 | 93.67 +| 0057 |[Insert Interval](src/main/kotlin/g0001_0100/s0057_insert_interval)| Medium | Array, Level_2_Day_17_Interval | 249 | 75.63 +| 0056 |[Merge Intervals](src/main/kotlin/g0001_0100/s0056_merge_intervals)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Data_Structure_II_Day_2_Array, Level_2_Day_17_Interval, Udemy_2D_Arrays/Matrix | 334 | 80.67 +| 0055 |[Jump Game](src/main/kotlin/g0001_0100/s0055_jump_game)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Greedy, Algorithm_II_Day_12_Dynamic_Programming, Dynamic_Programming_I_Day_4, Udemy_Arrays | 332 | 89.35 +| 0054 |[Spiral Matrix](src/main/kotlin/g0001_0100/s0054_spiral_matrix)| Medium | Top_Interview_Questions, Array, Matrix, Simulation, Programming_Skills_II_Day_8, Level_2_Day_1_Implementation/Simulation, Udemy_2D_Arrays/Matrix | 132 | 95.12 +| 0053 |[Maximum Subarray](src/main/kotlin/g0001_0100/s0053_maximum_subarray)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Divide_and_Conquer, Data_Structure_I_Day_1_Array, Dynamic_Programming_I_Day_5, Udemy_Famous_Algorithm | 510 | 78.81 +| 0052 |[N-Queens II](src/main/kotlin/g0001_0100/s0052_n_queens_ii)| Hard | Backtracking | 128 | 97.56 +| 0051 |[N-Queens](src/main/kotlin/g0001_0100/s0051_n_queens)| Hard | Top_100_Liked_Questions, Array, Backtracking | 192 | 90.74 | 0050 |[Pow(x, n)](src/main/kotlin/g0001_0100/s0050_powx_n)| Medium | Top_Interview_Questions, Math, Recursion, Udemy_Integers | 172 | 38.68 | 0049 |[Group Anagrams](src/main/kotlin/g0001_0100/s0049_group_anagrams)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, String, Hash_Table, Sorting, Data_Structure_II_Day_8_String, Programming_Skills_II_Day_11, Udemy_Strings | 308 | 96.34 | 0048 |[Rotate Image](src/main/kotlin/g0001_0100/s0048_rotate_image)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Math, Matrix, Data_Structure_II_Day_3_Array, Programming_Skills_II_Day_7, Udemy_2D_Arrays/Matrix | 160 | 90.11 diff --git a/src/main/kotlin/g0001_0100/s0058_length_of_last_word/readme.md b/src/main/kotlin/g0001_0100/s0058_length_of_last_word/readme.md index ab75ce7b..16ad6f1f 100644 --- a/src/main/kotlin/g0001_0100/s0058_length_of_last_word/readme.md +++ b/src/main/kotlin/g0001_0100/s0058_length_of_last_word/readme.md @@ -43,10 +43,10 @@ A **word** is a maximal substring consisting of non-space characters only. ```kotlin class Solution { - fun lengthOfLastWord(str: String): Int { + fun lengthOfLastWord(s: String): Int { var len = 0 - for (i in str.length - 1 downTo 0) { - val ch = str[i] + for (i in s.length - 1 downTo 0) { + val ch = s[i] if (ch == ' ' && len > 0) { break } else if (ch != ' ') { diff --git a/src/main/kotlin/g0001_0100/s0072_edit_distance/readme.md b/src/main/kotlin/g0001_0100/s0072_edit_distance/readme.md index e9268eca..159e2b05 100644 --- a/src/main/kotlin/g0001_0100/s0072_edit_distance/readme.md +++ b/src/main/kotlin/g0001_0100/s0072_edit_distance/readme.md @@ -54,11 +54,11 @@ exection -> execution (insert 'u') ```kotlin class Solution { - fun minDistance(w1: String, w2: String): Int { - val n1 = w1.length - val n2 = w2.length + fun minDistance(word1: String, word2: String): Int { + val n1 = word1.length + val n2 = word2.length if (n2 > n1) { - return minDistance(w2, w1) + return minDistance(word2, word1) } val dp = IntArray(n2 + 1) for (j in 0..n2) { @@ -69,7 +69,7 @@ class Solution { dp[0] = i for (j in 1..n2) { val tmp = dp[j] - dp[j] = if (w1[i - 1] != w2[j - 1]) 1 + Math.min(pre, Math.min(dp[j], dp[j - 1])) else pre + dp[j] = if (word1[i - 1] != word2[j - 1]) 1 + Math.min(pre, Math.min(dp[j], dp[j - 1])) else pre pre = tmp } } diff --git a/src/main/kotlin/g0001_0100/s0090_subsets_ii/readme.md b/src/main/kotlin/g0001_0100/s0090_subsets_ii/readme.md index 64c8e7cd..297180ff 100644 --- a/src/main/kotlin/g0001_0100/s0090_subsets_ii/readme.md +++ b/src/main/kotlin/g0001_0100/s0090_subsets_ii/readme.md @@ -30,9 +30,10 @@ The solution set **must not** contain duplicate subsets. Return the solution in ```kotlin class Solution { - var allComb: MutableList> = ArrayList() - var comb: MutableList = ArrayList() - lateinit var nums: IntArray + private var allComb: MutableList> = ArrayList() + private var comb: MutableList = ArrayList() + private lateinit var nums: IntArray + fun subsetsWithDup(nums: IntArray): List> { nums.sort() this.nums = nums diff --git a/src/main/kotlin/g0101_0200/s0105_construct_binary_tree_from_preorder_and_inorder_traversal/readme.md b/src/main/kotlin/g0101_0200/s0105_construct_binary_tree_from_preorder_and_inorder_traversal/readme.md index 7c50b5d2..c9796765 100644 --- a/src/main/kotlin/g0101_0200/s0105_construct_binary_tree_from_preorder_and_inorder_traversal/readme.md +++ b/src/main/kotlin/g0101_0200/s0105_construct_binary_tree_from_preorder_and_inorder_traversal/readme.md @@ -35,7 +35,6 @@ Given two integer arrays `preorder` and `inorder` where `preorder` is the preord ```kotlin import com_github_leetcode.TreeNode -import java.util.HashMap /* * Example: diff --git a/src/main/kotlin/g0101_0200/s0107_binary_tree_level_order_traversal_ii/readme.md b/src/main/kotlin/g0101_0200/s0107_binary_tree_level_order_traversal_ii/readme.md index 69ed358c..aea18640 100644 --- a/src/main/kotlin/g0101_0200/s0107_binary_tree_level_order_traversal_ii/readme.md +++ b/src/main/kotlin/g0101_0200/s0107_binary_tree_level_order_traversal_ii/readme.md @@ -51,6 +51,7 @@ import kotlin.collections.ArrayList */ class Solution { private val order: MutableList> = ArrayList() + fun levelOrderBottom(root: TreeNode?): List> { getOrder(root, 0) Collections.reverse(order) diff --git a/src/main/kotlin/g0701_0800/s0770_basic_calculator_iv/readme.md b/src/main/kotlin/g0701_0800/s0770_basic_calculator_iv/readme.md index 077ea9b6..024fe99f 100644 --- a/src/main/kotlin/g0701_0800/s0770_basic_calculator_iv/readme.md +++ b/src/main/kotlin/g0701_0800/s0770_basic_calculator_iv/readme.md @@ -64,7 +64,7 @@ The format of the output is as follows: import java.util.Collections import java.util.Stack -internal class Solution { +class Solution { internal inner class Node { var mem: MutableMap, Int> = HashMap() fun update(cur: List, cnt: Int) { diff --git a/src/main/kotlin/g0901_1000/s0938_range_sum_of_bst/readme.md b/src/main/kotlin/g0901_1000/s0938_range_sum_of_bst/readme.md index 3804d188..a36985ad 100644 --- a/src/main/kotlin/g0901_1000/s0938_range_sum_of_bst/readme.md +++ b/src/main/kotlin/g0901_1000/s0938_range_sum_of_bst/readme.md @@ -49,7 +49,7 @@ import com_github_leetcode.TreeNode * var right: TreeNode? = null * } */ -internal class Solution { +class Solution { fun rangeSumBST(root: TreeNode?, low: Int, high: Int): Int { var ans = 0 if (root == null) return 0 diff --git a/src/main/kotlin/g2501_2600/s2546_apply_bitwise_operations_to_make_strings_equal/readme.md b/src/main/kotlin/g2501_2600/s2546_apply_bitwise_operations_to_make_strings_equal/readme.md new file mode 100644 index 00000000..99c39846 --- /dev/null +++ b/src/main/kotlin/g2501_2600/s2546_apply_bitwise_operations_to_make_strings_equal/readme.md @@ -0,0 +1,62 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork) + +## 2546\. Apply Bitwise Operations to Make Strings Equal + +Medium + +You are given two **0-indexed binary** strings `s` and `target` of the same length `n`. You can do the following operation on `s` **any** number of times: + +* Choose two **different** indices `i` and `j` where `0 <= i, j < n`. +* Simultaneously, replace `s[i]` with (`s[i]` **OR** `s[j]`) and `s[j]` with (`s[i]` **XOR** `s[j]`). + +For example, if `s = "0110"`, you can choose `i = 0` and `j = 2`, then simultaneously replace `s[0]` with (`s[0]` **OR** `s[2]` = `0` **OR** `1` = `1`), and `s[2]` with (`s[0]` **XOR** `s[2]` = `0` **XOR** `1` = `1`), so we will have `s = "1110"`. + +Return `true` _if you can make the string_ `s` _equal to_ `target`_, or_ `false` _otherwise_. + +**Example 1:** + +**Input:** s = "1010", target = "0110" + +**Output:** true + +**Explanation:** We can do the following operations: + +- Choose i = 2 and j = 0. We have now s = "**0**0**1**0". + +- Choose i = 2 and j = 1. We have now s = "0**11**0". Since we can make s equal to target, we return true. + +**Example 2:** + +**Input:** s = "11", target = "00" + +**Output:** false + +**Explanation:** It is not possible to make s equal to target with any number of operations. + +**Constraints:** + +* `n == s.length == target.length` +* 2 <= n <= 105 +* `s` and `target` consist of only the digits `0` and `1`. + +## Solution + +```kotlin +class Solution { + fun makeStringsEqual(s: String, target: String): Boolean { + val strLen = s.length + var ans1 = false + var ans2 = false + for (i in 0 until strLen) { + if (s[i] == '1') { + ans1 = true + } + if (target[i] == '1') { + ans2 = true + } + } + return ans1 == ans2 + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g2501_2600/s2547_minimum_cost_to_split_an_array/readme.md b/src/main/kotlin/g2501_2600/s2547_minimum_cost_to_split_an_array/readme.md new file mode 100644 index 00000000..4923154a --- /dev/null +++ b/src/main/kotlin/g2501_2600/s2547_minimum_cost_to_split_an_array/readme.md @@ -0,0 +1,105 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork) + +## 2547\. Minimum Cost to Split an Array + +Hard + +You are given an integer array `nums` and an integer `k`. + +Split the array into some number of non-empty subarrays. The **cost** of a split is the sum of the **importance value** of each subarray in the split. + +Let `trimmed(subarray)` be the version of the subarray where all numbers which appear only once are removed. + +* For example, `trimmed([3,1,2,4,3,4]) = [3,4,3,4].` + +The **importance value** of a subarray is `k + trimmed(subarray).length`. + +* For example, if a subarray is `[1,2,3,3,3,4,4]`, then trimmed(`[1,2,3,3,3,4,4]) = [3,3,3,4,4].`The importance value of this subarray will be `k + 5`. + +Return _the minimum possible cost of a split of_ `nums`. + +A **subarray** is a contiguous **non-empty** sequence of elements within an array. + +**Example 1:** + +**Input:** nums = [1,2,1,2,1,3,3], k = 2 + +**Output:** 8 + +**Explanation:** We split nums to have two subarrays: [1,2], [1,2,1,3,3]. ' + +The importance value of [1,2] is 2 + (0) = 2. + +The importance value of [1,2,1,3,3] is 2 + (2 + 2) = 6. + +The cost of the split is 2 + 6 = 8. It can be shown that this is the minimum possible cost among all the possible splits. + +**Example 2:** + +**Input:** nums = [1,2,1,2,1], k = 2 + +**Output:** 6 + +**Explanation:** We split nums to have two subarrays: [1,2], [1,2,1]. + +The importance value of [1,2] is 2 + (0) = 2. + +The importance value of [1,2,1] is 2 + (2) = 4. + +The cost of the split is 2 + 4 = 6. It can be shown that this is the minimum possible cost among all the possible splits. + +**Example 3:** + +**Input:** nums = [1,2,1,2,1], k = 5 + +**Output:** 10 + +**Explanation:** We split nums to have one subarray: [1,2,1,2,1]. + +The importance value of [1,2,1,2,1] is 5 + (3 + 2) = 10. + +The cost of the split is 10. It can be shown that this is the minimum possible cost among all the possible splits. + +**Constraints:** + +* `1 <= nums.length <= 1000` +* `0 <= nums[i] < nums.length` +* 1 <= k <= 109 + +## Solution + +```kotlin +class Solution { + fun minCost(nums: IntArray, k: Int): Int { + val n = nums.size + val dp = IntArray(n) + dp.fill(-1) + val len = Array(n) { IntArray(n) } + for (r in len) r.fill(0) + for (i in 0 until n) { + val count = IntArray(n) + count.fill(0) + var c = 0 + for (j in i until n) { + count[nums[j]] += 1 + if (count[nums[j]] == 2) c += 2 else if (count[nums[j]] > 2) c += 1 + len[i][j] = c + } + } + return f(0, nums, k, len, dp) + } + + private fun f(ind: Int, nums: IntArray, k: Int, len: Array, dp: IntArray): Int { + if (ind >= nums.size) return 0 + if (dp[ind] != -1) return dp[ind] + dp[ind] = Int.MAX_VALUE + for (i in ind until nums.size) { + val current = len[ind][i] + k + val next = f(i + 1, nums, k, len, dp) + dp[ind] = dp[ind].coerceAtMost(current + next) + } + return dp[ind] + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g2501_2600/s2549_count_distinct_numbers_on_board/readme.md b/src/main/kotlin/g2501_2600/s2549_count_distinct_numbers_on_board/readme.md new file mode 100644 index 00000000..0e87d35c --- /dev/null +++ b/src/main/kotlin/g2501_2600/s2549_count_distinct_numbers_on_board/readme.md @@ -0,0 +1,58 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork) + +## 2549\. Count Distinct Numbers on Board + +Easy + +You are given a positive integer `n`, that is initially placed on a board. Every day, for 109 days, you perform the following procedure: + +* For each number `x` present on the board, find all numbers `1 <= i <= n` such that `x % i == 1`. +* Then, place those numbers on the board. + +Return _the number of **distinct** integers present on the board after_ 109 _days have elapsed_. + +**Note:** + +* Once a number is placed on the board, it will remain on it until the end. +* `%` stands for the modulo operation. For example, `14 % 3` is `2`. + +**Example 1:** + +**Input:** n = 5 + +**Output:** 4 + +**Explanation:** Initially, 5 is present on the board. + +The next day, 2 and 4 will be added since 5 % 2 == 1 and 5 % 4 == 1. + +After that day, 3 will be added to the board because 4 % 3 == 1. + +At the end of a billion days, the distinct numbers on the board will be 2, 3, 4, and 5. + +**Example 2:** + +**Input:** n = 3 + +**Output:** 2 + +**Explanation:** + +Since 3 % 2 == 1, 2 will be added to the board. + +After a billion days, the only two distinct numbers on the board are 2 and 3. + +**Constraints:** + +* `1 <= n <= 100` + +## Solution + +```kotlin +class Solution { + fun distinctIntegers(n: Int): Int { + return 1.coerceAtLeast(n - 1) + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g2501_2600/s2550_count_collisions_of_monkeys_on_a_polygon/readme.md b/src/main/kotlin/g2501_2600/s2550_count_collisions_of_monkeys_on_a_polygon/readme.md new file mode 100644 index 00000000..0a818276 --- /dev/null +++ b/src/main/kotlin/g2501_2600/s2550_count_collisions_of_monkeys_on_a_polygon/readme.md @@ -0,0 +1,63 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork) + +## 2550\. Count Collisions of Monkeys on a Polygon + +Medium + +There is a regular convex polygon with `n` vertices. The vertices are labeled from `0` to `n - 1` in a clockwise direction, and each vertex has **exactly one monkey**. The following figure shows a convex polygon of `6` vertices. + +![](https://assets.leetcode.com/uploads/2023/01/22/hexagon.jpg) + +Each monkey moves simultaneously to a neighboring vertex. A neighboring vertex for a vertex `i` can be: + +* the vertex `(i + 1) % n` in the clockwise direction, or +* the vertex `(i - 1 + n) % n` in the counter-clockwise direction. + +A **collision** happens if at least two monkeys reside on the same vertex after the movement or intersect on an edge. + +Return _the number of ways the monkeys can move so that at least **one collision**_ _happens_. Since the answer may be very large, return it modulo 109 + 7. + +**Note** that each monkey can only move once. + +**Example 1:** + +**Input:** n = 3 + +**Output:** 6 + +**Explanation:** There are 8 total possible movements. + +Two ways such that they collide at some point are: + +- Monkey 1 moves in a clockwise direction; monkey 2 moves in an anticlockwise direction; monkey 3 moves in a clockwise direction. Monkeys 1 and 2 collide. + +- Monkey 1 moves in an anticlockwise direction; monkey 2 moves in an anticlockwise direction; monkey 3 moves in a clockwise direction. Monkeys 1 and 3 collide. It can be shown 6 total movements result in a collision. + +**Example 2:** + +**Input:** n = 4 + +**Output:** 14 + +**Explanation:** It can be shown that there are 14 ways for the monkeys to collide. + +**Constraints:** + +* 3 <= n <= 109 + +## Solution + +```kotlin +class Solution { + fun monkeyMove(n: Int): Int { + return (((modPow2(n - 2) - 1 shl 2) + 2) % 1000000007).toInt() + } + + private fun modPow2(n: Int): Long { + if (n == 0) return 1 + val b = modPow2(n shr 1) + return (b * b shl (n and 1)) % 1000000007 + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g2501_2600/s2551_put_marbles_in_bags/readme.md b/src/main/kotlin/g2501_2600/s2551_put_marbles_in_bags/readme.md new file mode 100644 index 00000000..0c76f897 --- /dev/null +++ b/src/main/kotlin/g2501_2600/s2551_put_marbles_in_bags/readme.md @@ -0,0 +1,82 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork) + +## 2551\. Put Marbles in Bags + +Hard + +You have `k` bags. You are given a **0-indexed** integer array `weights` where `weights[i]` is the weight of the ith marble. You are also given the integer `k.` + +Divide the marbles into the `k` bags according to the following rules: + +* No bag is empty. +* If the ith marble and jth marble are in a bag, then all marbles with an index between the ith and jth indices should also be in that same bag. +* If a bag consists of all the marbles with an index from `i` to `j` inclusively, then the cost of the bag is `weights[i] + weights[j]`. + +The **score** after distributing the marbles is the sum of the costs of all the `k` bags. + +Return _the **difference** between the **maximum** and **minimum** scores among marble distributions_. + +**Example 1:** + +**Input:** weights = [1,3,5,1], k = 2 + +**Output:** 4 + +**Explanation:** + +The distribution [1],[3,5,1] results in the minimal score of (1+1) + (3+1) = 6. + +The distribution [1,3],[5,1], results in the maximal score of (1+3) + (5+1) = 10. + +Thus, we return their difference 10 - 6 = 4. + +**Example 2:** + +**Input:** weights = [1, 3], k = 2 + +**Output:** 0 + +**Explanation:** + +The only distribution possible is [1],[3]. + +Since both the maximal and minimal score are the same, we return 0. + +**Constraints:** + +* 1 <= k <= weights.length <= 105 +* 1 <= weights[i] <= 109 + +## Solution + +```kotlin +import java.util.PriorityQueue + +class Solution { + fun putMarbles(weights: IntArray, k: Int): Long { + // Map, long[]> memo = new HashMap<>(); + // long[] res = dfs(weights, 0, k, memo); + // return res[1] - res[0]; + if (k == 1 || k == weights.size) return 0 + val min = PriorityQueue() + val max = PriorityQueue { a: Long?, b: Long? -> + java.lang.Long.compare( + b!!, a!! + ) + } + for (i in 0 until weights.size - 1) { + val sum = weights[i].toLong() + weights[i + 1] + min.offer(sum) + max.offer(sum) + if (min.size == k) min.poll() + if (max.size == k) max.poll() + } + var res: Long = 0 + while (max.isNotEmpty()) { + res += min.poll() - max.poll()!! + } + return res + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g2501_2600/s2552_count_increasing_quadruplets/readme.md b/src/main/kotlin/g2501_2600/s2552_count_increasing_quadruplets/readme.md new file mode 100644 index 00000000..694c5399 --- /dev/null +++ b/src/main/kotlin/g2501_2600/s2552_count_increasing_quadruplets/readme.md @@ -0,0 +1,65 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork) + +## 2552\. Count Increasing Quadruplets + +Hard + +Given a **0-indexed** integer array `nums` of size `n` containing all numbers from `1` to `n`, return _the number of increasing quadruplets_. + +A quadruplet `(i, j, k, l)` is increasing if: + +* `0 <= i < j < k < l < n`, and +* `nums[i] < nums[k] < nums[j] < nums[l]`. + +**Example 1:** + +**Input:** nums = [1,3,2,4,5] + +**Output:** 2 + +**Explanation:** + +- When i = 0, j = 1, k = 2, and l = 3, nums[i] < nums[k] < nums[j] < nums[l]. + +- When i = 0, j = 1, k = 2, and l = 4, nums[i] < nums[k] < nums[j] < nums[l]. There are no other quadruplets, so we return 2. + +**Example 2:** + +**Input:** nums = [1,2,3,4] + +**Output:** 0 + +**Explanation:** + +There exists only one quadruplet with i = 0, j = 1, k = 2, l = 3, but since nums[j] < nums[k], we return 0. + +**Constraints:** + +* `4 <= nums.length <= 4000` +* `1 <= nums[i] <= nums.length` +* All the integers of `nums` are **unique**. `nums` is a permutation. + +## Solution + +```kotlin +class Solution { + fun countQuadruplets(nums: IntArray): Long { + val len = nums.size + val dp = LongArray(len) + var ans: Long = 0 + for (i in 0 until len) { + var smallerThanK = 0 + for (j in 0 until i) { + if (nums[i] > nums[j]) { + smallerThanK++ + ans += dp[j] + } else { + dp[j] += smallerThanK.toLong() + } + } + } + return ans + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g2501_2600/s2553_separate_the_digits_in_an_array/readme.md b/src/main/kotlin/g2501_2600/s2553_separate_the_digits_in_an_array/readme.md new file mode 100644 index 00000000..c4c3809a --- /dev/null +++ b/src/main/kotlin/g2501_2600/s2553_separate_the_digits_in_an_array/readme.md @@ -0,0 +1,68 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork) + +## 2553\. Separate the Digits in an Array + +Easy + +Given an array of positive integers `nums`, return _an array_ `answer` _that consists of the digits of each integer in_ `nums` _after separating them in **the same order** they appear in_ `nums`. + +To separate the digits of an integer is to get all the digits it has in the same order. + +* For example, for the integer `10921`, the separation of its digits is `[1,0,9,2,1]`. + +**Example 1:** + +**Input:** nums = [13,25,83,77] + +**Output:** [1,3,2,5,8,3,7,7] + +**Explanation:** + +- The separation of 13 is [1,3]. + +- The separation of 25 is [2,5]. + +- The separation of 83 is [8,3]. + +- The separation of 77 is [7,7]. + +answer = [1,3,2,5,8,3,7,7]. Note that answer contains the separations in the same order. + +**Example 2:** + +**Input:** nums = [7,1,3,9] + +**Output:** [7,1,3,9] + +**Explanation:** + +The separation of each integer in nums is itself. answer = [7,1,3,9]. + +**Constraints:** + +* `1 <= nums.length <= 1000` +* 1 <= nums[i] <= 105 + +## Solution + +```kotlin +class Solution { + fun separateDigits(nums: IntArray): IntArray { + val arr = ArrayList() + for (i in nums.indices.reversed()) { + while (nums[i] > 0) { + val r = nums[i] % 10 + arr.add(r) + nums[i] = nums[i] / 10 + } + } + val num = IntArray(arr.size) + var i = arr.size - 1 + for (I in arr) { + num[i--] = I + } + return num + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g2501_2600/s2554_maximum_number_of_integers_to_choose_from_a_range_i/readme.md b/src/main/kotlin/g2501_2600/s2554_maximum_number_of_integers_to_choose_from_a_range_i/readme.md new file mode 100644 index 00000000..1de854b2 --- /dev/null +++ b/src/main/kotlin/g2501_2600/s2554_maximum_number_of_integers_to_choose_from_a_range_i/readme.md @@ -0,0 +1,77 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork) + +## 2554\. Maximum Number of Integers to Choose From a Range I + +Medium + +You are given an integer array `banned` and two integers `n` and `maxSum`. You are choosing some number of integers following the below rules: + +* The chosen integers have to be in the range `[1, n]`. +* Each integer can be chosen **at most once**. +* The chosen integers should not be in the array `banned`. +* The sum of the chosen integers should not exceed `maxSum`. + +Return _the **maximum** number of integers you can choose following the mentioned rules_. + +**Example 1:** + +**Input:** banned = [1,6,5], n = 5, maxSum = 6 + +**Output:** 2 + +**Explanation:** + +You can choose the integers 2 and 4. + +2 and 4 are from the range [1, 5], both did not appear in banned, and their sum is 6, which did not exceed maxSum. + +**Example 2:** + +**Input:** banned = [1,2,3,4,5,6,7], n = 8, maxSum = 1 + +**Output:** 0 + +**Explanation:** + +You cannot choose any integer while following the mentioned conditions. + +**Example 3:** + +**Input:** banned = [11], n = 7, maxSum = 50 + +**Output:** 7 + +**Explanation:** + +You can choose the integers 1, 2, 3, 4, 5, 6, and 7. They are from the range [1, 7], all did not appear in banned, and their sum is 28, which did not exceed maxSum. + +**Constraints:** + +* 1 <= banned.length <= 104 +* 1 <= banned[i], n <= 104 +* 1 <= maxSum <= 109 + +## Solution + +```kotlin +class Solution { + fun maxCount(banned: IntArray, n: Int, maxSum: Int): Int { + val ban = BooleanArray(20000) + var count = 0 + var sum = 0 + for (i in banned) ban[i] = true + for (i in 1..n) { + if (ban[i]) { + continue + } else if (sum + i <= maxSum) { + sum += i + count++ + } else { + break + } + } + return count + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g2501_2600/s2555_maximize_win_from_two_segments/readme.md b/src/main/kotlin/g2501_2600/s2555_maximize_win_from_two_segments/readme.md new file mode 100644 index 00000000..37ccf334 --- /dev/null +++ b/src/main/kotlin/g2501_2600/s2555_maximize_win_from_two_segments/readme.md @@ -0,0 +1,87 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork) + +## 2555\. Maximize Win From Two Segments + +Medium + +There are some prizes on the **X-axis**. You are given an integer array `prizePositions` that is **sorted in non-decreasing order**, where `prizePositions[i]` is the position of the ith prize. There could be different prizes at the same position on the line. You are also given an integer `k`. + +You are allowed to select two segments with integer endpoints. The length of each segment must be `k`. You will collect all prizes whose position falls within at least one of the two selected segments (including the endpoints of the segments). The two selected segments may intersect. + +* For example if `k = 2`, you can choose segments `[1, 3]` and `[2, 4]`, and you will win any prize i that satisfies `1 <= prizePositions[i] <= 3` or `2 <= prizePositions[i] <= 4`. + +Return _the **maximum** number of prizes you can win if you choose the two segments optimally_. + +**Example 1:** + +**Input:** prizePositions = [1,1,2,2,3,3,5], k = 2 + +**Output:** 7 + +**Explanation:** + +In this example, you can win all 7 prizes by selecting two segments [1, 3] and [3, 5]. + +**Example 2:** + +**Input:** prizePositions = [1,2,3,4], k = 0 + +**Output:** 2 + +**Explanation:** + +For this example, **one choice** for the segments is `[3, 3]` and `[4, 4],` and you will be able to get `2` prizes. + +**Constraints:** + +* 1 <= prizePositions.length <= 105 +* 1 <= prizePositions[i] <= 109 +* 0 <= k <= 109 +* `prizePositions` is sorted in non-decreasing order. + +## Solution + +```kotlin +class Solution { + fun maximizeWin(p: IntArray, k: Int): Int { + val n = p.size + if (p[n - 1] - p[0] <= 2 * k) { + return n + } + // segment ending in pre[j] + val pre = IntArray(n) + // segment starting in post[i] + val post = IntArray(n) + var i = 0 + var max = 0 + var j = 0 + while (j < n) { + if (p[j] - p[i] > k) { + i++ + } + max = Math.max(max, j - i + 1) + pre[j] = max + j++ + } + max = 0 + j = n - 1 + i = n - 1 + while (i >= 0) { + if (p[j] - p[i] > k) { + j-- + } + max = Math.max(max, j - i + 1) + post[i] = max + i-- + } + var res = 0 + var b = 0 + while (b + 1 < n) { + res = Math.max(res, pre[b] + post[b + 1]) + b++ + } + return res + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g2501_2600/s2556_disconnect_path_in_a_binary_matrix_by_at_most_one_flip/readme.md b/src/main/kotlin/g2501_2600/s2556_disconnect_path_in_a_binary_matrix_by_at_most_one_flip/readme.md new file mode 100644 index 00000000..a52481e0 --- /dev/null +++ b/src/main/kotlin/g2501_2600/s2556_disconnect_path_in_a_binary_matrix_by_at_most_one_flip/readme.md @@ -0,0 +1,68 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork) + +## 2556\. Disconnect Path in a Binary Matrix by at Most One Flip + +Medium + +You are given a **0-indexed** `m x n` **binary** matrix `grid`. You can move from a cell `(row, col)` to any of the cells `(row + 1, col)` or `(row, col + 1)` that has the value `1`. The matrix is **disconnected** if there is no path from `(0, 0)` to `(m - 1, n - 1)`. + +You can flip the value of **at most one** (possibly none) cell. You **cannot flip** the cells `(0, 0)` and `(m - 1, n - 1)`. + +Return `true` _if it is possible to make the matrix disconnect or_ `false` _otherwise_. + +**Note** that flipping a cell changes its value from `0` to `1` or from `1` to `0`. + +**Example 1:** + +![](https://assets.leetcode.com/uploads/2022/12/07/yetgrid2drawio.png) + +**Input:** grid = \[\[1,1,1],[1,0,0],[1,1,1]] + +**Output:** true + +**Explanation:** + +We can change the cell shown in the diagram above. There is no path from (0, 0) to (2, 2) in the resulting grid. + +**Example 2:** + +![](https://assets.leetcode.com/uploads/2022/12/07/yetgrid3drawio.png) + +**Input:** grid = \[\[1,1,1],[1,0,1],[1,1,1]] + +**Output:** false + +**Explanation:** + +It is not possible to change at most one cell such that there is not path from (0, 0) to (2, 2). + +**Constraints:** + +* `m == grid.length` +* `n == grid[i].length` +* `1 <= m, n <= 1000` +* 1 <= m * n <= 105 +* `grid[i][j]` is either `0` or `1`. +* `grid[0][0] == grid[m - 1][n - 1] == 1` + +## Solution + +```kotlin +class Solution { + fun isPossibleToCutPath(grid: Array): Boolean { + val n = grid.size + val m = grid[0].size + dfs(0, 0, grid, n, m) + grid[0][0] = 1 + return !dfs(0, 0, grid, n, m) + } + + private fun dfs(i: Int, j: Int, a: Array, n: Int, m: Int): Boolean { + if (i >= n || j >= m || i < 0 || j < 0 || a[i][j] == 0) return false + if (i == n - 1 && j == m - 1) return true + a[i][j] = 0 + return dfs(i + 1, j, a, n, m) || dfs(i, j + 1, a, n, m) + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g2501_2600/s2558_take_gifts_from_the_richest_pile/readme.md b/src/main/kotlin/g2501_2600/s2558_take_gifts_from_the_richest_pile/readme.md new file mode 100644 index 00000000..7818c585 --- /dev/null +++ b/src/main/kotlin/g2501_2600/s2558_take_gifts_from_the_richest_pile/readme.md @@ -0,0 +1,70 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork) + +## 2558\. Take Gifts From the Richest Pile + +Easy + +You are given an integer array `gifts` denoting the number of gifts in various piles. Every second, you do the following: + +* Choose the pile with the maximum number of gifts. +* If there is more than one pile with the maximum number of gifts, choose any. +* Leave behind the floor of the square root of the number of gifts in the pile. Take the rest of the gifts. + +Return _the number of gifts remaining after_ `k` _seconds._ + +**Example 1:** + +**Input:** gifts = [25,64,9,4,100], k = 4 + +**Output:** 29 + +**Explanation:** The gifts are taken in the following way: + +- In the first second, the last pile is chosen and 10 gifts are left behind. +- Then the second pile is chosen and 8 gifts are left behind. +- After that the first pile is chosen and 5 gifts are left behind. +- Finally, the last pile is chosen again and 3 gifts are left behind. + +The final remaining gifts are [5,8,9,4,3], so the total number of gifts remaining is 29. + +**Example 2:** + +**Input:** gifts = [1,1,1,1], k = 4 + +**Output:** 4 + +**Explanation:** + +In this case, regardless which pile you choose, you have to leave behind 1 gift in each pile. + +That is, you can't take any pile with you. + +So, the total gifts remaining are 4. + +**Constraints:** + +* 1 <= gifts.length <= 103 +* 1 <= gifts[i] <= 109 +* 1 <= k <= 103 + +## Solution + +```kotlin +import java.util.PriorityQueue +import kotlin.math.sqrt + +class Solution { + fun pickGifts(gifts: IntArray, k: Int): Long { + val queue = PriorityQueue { a, b -> b - a } + for (gift in gifts) queue.add(gift) + var result = 0L + var seconds = k + while (seconds-- > 0 && queue.isNotEmpty()) { + queue.add(sqrt(queue.poll().toDouble()).toInt()) + } + while (queue.isNotEmpty()) result += queue.poll() + return result + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g2501_2600/s2559_count_vowel_strings_in_ranges/readme.md b/src/main/kotlin/g2501_2600/s2559_count_vowel_strings_in_ranges/readme.md new file mode 100644 index 00000000..9e8b4457 --- /dev/null +++ b/src/main/kotlin/g2501_2600/s2559_count_vowel_strings_in_ranges/readme.md @@ -0,0 +1,76 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork) + +## 2559\. Count Vowel Strings in Ranges + +Medium + +You are given a **0-indexed** array of strings `words` and a 2D array of integers `queries`. + +Each query queries[i] = [li, ri] asks us to find the number of strings present in the range li to ri (both **inclusive**) of `words` that start and end with a vowel. + +Return _an array_ `ans` _of size_ `queries.length`_, where_ `ans[i]` _is the answer to the_ `i`th _query_. + +**Note** that the vowel letters are `'a'`, `'e'`, `'i'`, `'o'`, and `'u'`. + +**Example 1:** + +**Input:** words = ["aba","bcb","ece","aa","e"], queries = \[\[0,2],[1,4],[1,1]] + +**Output:** [2,3,0] + +**Explanation:** The strings starting and ending with a vowel are "aba", "ece", "aa" and "e". + +The answer to the query [0,2] is 2 (strings "aba" and "ece"). + +to query [1,4] is 3 (strings "ece", "aa", "e"). + +to query [1,1] is 0. + +We return [2,3,0]. + +**Example 2:** + +**Input:** words = ["a","e","i"], queries = \[\[0,2],[0,1],[2,2]] + +**Output:** [3,2,1] + +**Explanation:** Every string satisfies the conditions, so we return [3,2,1]. + +**Constraints:** + +* 1 <= words.length <= 105 +* `1 <= words[i].length <= 40` +* `words[i]` consists only of lowercase English letters. +* sum(words[i].length) <= 3 * 105 +* 1 <= queries.length <= 105 +* 0 <= li <= ri < words.length + +## Solution + +```kotlin +class Solution { + fun vowelStrings(words: Array, queries: Array): IntArray { + val vowels = HashSet(listOf('a', 'e', 'i', 'o', 'u')) + val n = words.size + val validWords = IntArray(n) + for (i in 0 until n) { + val startChar = words[i][0] + val endChar = words[i][words[i].length - 1] + validWords[i] = if (vowels.contains(startChar) && vowels.contains(endChar)) 1 else 0 + } + val prefix = IntArray(n) + prefix[0] = validWords[0] + for (i in 1 until n) { + prefix[i] = prefix[i - 1] + validWords[i] + } + val output = IntArray(queries.size) + for (i in queries.indices) { + val start = queries[i][0] + val end = queries[i][1] + output[i] = if (start == 0) prefix[end] else prefix[end] - prefix[start - 1] + } + return output + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g2501_2600/s2560_house_robber_iv/readme.md b/src/main/kotlin/g2501_2600/s2560_house_robber_iv/readme.md new file mode 100644 index 00000000..8232ac4a --- /dev/null +++ b/src/main/kotlin/g2501_2600/s2560_house_robber_iv/readme.md @@ -0,0 +1,82 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork) + +## 2560\. House Robber IV + +Medium + +There are several consecutive houses along a street, each of which has some money inside. There is also a robber, who wants to steal money from the homes, but he **refuses to steal from adjacent homes**. + +The **capability** of the robber is the maximum amount of money he steals from one house of all the houses he robbed. + +You are given an integer array `nums` representing how much money is stashed in each house. More formally, the ith house from the left has `nums[i]` dollars. + +You are also given an integer `k`, representing the **minimum** number of houses the robber will steal from. It is always possible to steal at least `k` houses. + +Return _the **minimum** capability of the robber out of all the possible ways to steal at least_ `k` _houses_. + +**Example 1:** + +**Input:** nums = [2,3,5,9], k = 2 + +**Output:** 5 + +**Explanation:** There are three ways to rob at least 2 houses: + +- Rob the houses at indices 0 and 2. Capability is max(nums[0], nums[2]) = 5. + +- Rob the houses at indices 0 and 3. Capability is max(nums[0], nums[3]) = 9. + +- Rob the houses at indices 1 and 3. Capability is max(nums[1], nums[3]) = 9. + +Therefore, we return min(5, 9, 9) = 5. + +**Example 2:** + +**Input:** nums = [2,7,9,3,1], k = 2 + +**Output:** 2 + +**Explanation:** There are 7 ways to rob the houses. The way which leads to minimum capability is to rob the house at index 0 and 4. Return max(nums[0], nums[4]) = 2. + +**Constraints:** + +* 1 <= nums.length <= 105 +* 1 <= nums[i] <= 109 +* `1 <= k <= (nums.length + 1)/2` + +## Solution + +```kotlin +class Solution { + fun minCapability(nums: IntArray, k: Int): Int { + var l = 1 + var r = 1e9.toInt() + while (l < r) { + val mid = l + (r - l) / 2 + if (isPossible(nums, mid, k)) { + r = mid + } else { + l = mid + 1 + } + } + return r + } + + private fun isPossible(nums: IntArray, maxMoney: Int, k: Int): Boolean { + var houseStolen = 0 + var lastStolenIdx = -2 + for (i in nums.indices) { + if (nums[i] > maxMoney) { + continue + } + if (i == lastStolenIdx + 1) { + continue + } + houseStolen++ + lastStolenIdx = i + } + return houseStolen >= k + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g2501_2600/s2561_rearranging_fruits/readme.md b/src/main/kotlin/g2501_2600/s2561_rearranging_fruits/readme.md new file mode 100644 index 00000000..6dbc10cd --- /dev/null +++ b/src/main/kotlin/g2501_2600/s2561_rearranging_fruits/readme.md @@ -0,0 +1,96 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork) + +## 2561\. Rearranging Fruits + +Hard + +You have two fruit baskets containing `n` fruits each. You are given two **0-indexed** integer arrays `basket1` and `basket2` representing the cost of fruit in each basket. You want to make both baskets **equal**. To do so, you can use the following operation as many times as you want: + +* Chose two indices `i` and `j`, and swap the ith fruit of `basket1` with the jth fruit of `basket2`. +* The cost of the swap is `min(basket1[i],basket2[j])`. + +Two baskets are considered equal if sorting them according to the fruit cost makes them exactly the same baskets. + +Return _the minimum cost to make both the baskets equal or_ `-1` _if impossible._ + +**Example 1:** + +**Input:** basket1 = [4,2,2,2], basket2 = [1,4,1,2] + +**Output:** 1 + +**Explanation:** Swap index 1 of basket1 with index 0 of basket2, which has cost 1. Now basket1 = [4,1,2,2] and basket2 = [2,4,1,2]. Rearranging both the arrays makes them equal. + +**Example 2:** + +**Input:** basket1 = [2,3,4,1], basket2 = [3,2,5,1] + +**Output:** -1 + +**Explanation:** It can be shown that it is impossible to make both the baskets equal. + +**Constraints:** + +* `basket1.length == bakste2.length` +* 1 <= basket1.length <= 105 +* 1 <= basket1[i],basket2[i] <= 109 + +## Solution + +```kotlin +class Solution { + fun minCost(basket1: IntArray, basket2: IntArray): Long { + val n = basket1.size + val map1: MutableMap = HashMap() + val map2: MutableMap = HashMap() + var minVal = Int.MAX_VALUE // Use on indirect swap + + // Counting the basket's number to each HashMap + for (i in 0 until n) { + map1[basket1[i]] = map1.getOrDefault(basket1[i], 0) + 1 + map2[basket2[i]] = map2.getOrDefault(basket2[i], 0) + 1 + minVal = minVal.coerceAtMost(basket1[i]) + minVal = minVal.coerceAtMost(basket2[i]) + } + + // build swap list, if any number is too more, add numbers to prepare swap list + val swapList1: MutableList = ArrayList() + for (key in map1.keys) { + val c1 = map1[key]!! + val c2 = map2.getOrDefault(key, 0) + if ((c1 + c2) % 2 == 1) return -1 + if (c1 > c2) { + var addCnt = (c1 - c2) / 2 + while (addCnt-- > 0) { + swapList1.add(key) + } + } + } + val swapList2: MutableList = ArrayList() + for (key in map2.keys) { + val c1 = map1.getOrDefault(key, 0) + val c2 = map2[key]!! + if ((c1 + c2) % 2 == 1) return -1 + if (c2 > c1) { + var addCnt = (c2 - c1) / 2 + while (addCnt-- > 0) { + swapList2.add(key) + } + } + } + + // Sorting + swapList1.sort() + swapList2.sortWith { a: Int, b: Int -> b - a } + + // visite swap list + var res: Long = 0 + for (i in swapList1.indices) { + // Two choices to swap, direct swap or indirect swap + res += (2 * minVal).coerceAtMost(swapList1[i].coerceAtMost(swapList2[i])).toLong() + } + return res + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g2501_2600/s2562_find_the_array_concatenation_value/readme.md b/src/main/kotlin/g2501_2600/s2562_find_the_array_concatenation_value/readme.md new file mode 100644 index 00000000..af235a7b --- /dev/null +++ b/src/main/kotlin/g2501_2600/s2562_find_the_array_concatenation_value/readme.md @@ -0,0 +1,68 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork) + +## 2562\. Find the Array Concatenation Value + +Easy + +You are given a **0-indexed** integer array `nums`. + +The **concatenation** of two numbers is the number formed by concatenating their numerals. + +* For example, the concatenation of `15`, `49` is `1549`. + +The **concatenation value** of `nums` is initially equal to `0`. Perform this operation until `nums` becomes empty: + +* If there exists more than one number in `nums`, pick the first element and last element in `nums` respectively and add the value of their concatenation to the **concatenation value** of `nums`, then delete the first and last element from `nums`. +* If one element exists, add its value to the **concatenation value** of `nums`, then delete it. + +Return _the concatenation value of the `nums`_. + +**Example 1:** + +**Input:** nums = [7,52,2,4] + +**Output:** 596 + +**Explanation:** Before performing any operation, nums is [7,52,2,4] and concatenation value is 0. + +- In the first operation: We pick the first element, 7, and the last element, 4. Their concatenation is 74, and we add it to the concatenation value, so it becomes equal to 74. Then we delete them from nums, so nums becomes equal to [52,2]. + +- In the second operation: We pick the first element, 52, and the last element, 2. Their concatenation is 522, and we add it to the concatenation value, so it becomes equal to 596. Then we delete them from the nums, so nums becomes empty. Since the concatenation value is 596 so the answer is 596. + +**Example 2:** + +**Input:** nums = [5,14,13,8,12] + +**Output:** 673 + +**Explanation:** Before performing any operation, nums is [5,14,13,8,12] and concatenation value is 0. + +- In the first operation: We pick the first element, 5, and the last element, 12. Their concatenation is 512, and we add it to the concatenation value, so it becomes equal to 512. Then we delete them from the nums, so nums becomes equal to [14,13,8]. + +- In the second operation: We pick the first element, 14, and the last element, 8. Their concatenation is 148, and we add it to the concatenation value, so it becomes equal to 660. Then we delete them from the nums, so nums becomes equal to [13]. + +- In the third operation: nums has only one element, so we pick 13 and add it to the concatenation value, so it becomes equal to 673. Then we delete it from nums, so nums become empty. Since the concatenation value is 673 so the answer is 673. + +**Constraints:** + +* `1 <= nums.length <= 1000` +* 1 <= nums[i] <= 104 + +## Solution + +```kotlin +class Solution { + fun findTheArrayConcVal(nums: IntArray): Long { + val n = nums.size + var result = 0L + for (i in 0..(n - 1) / 2) { + result += if (i < n - 1 - i) { + val concat = "" + nums[i] + nums[n - 1 - i] + concat.toLong() + } else nums[i].toLong() + } + return result + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g2501_2600/s2563_count_the_number_of_fair_pairs/readme.md b/src/main/kotlin/g2501_2600/s2563_count_the_number_of_fair_pairs/readme.md new file mode 100644 index 00000000..5ad46580 --- /dev/null +++ b/src/main/kotlin/g2501_2600/s2563_count_the_number_of_fair_pairs/readme.md @@ -0,0 +1,63 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork) + +## 2563\. Count the Number of Fair Pairs + +Medium + +Given a **0-indexed** integer array `nums` of size `n` and two integers `lower` and `upper`, return _the number of fair pairs_. + +A pair `(i, j)` is **fair** if: + +* `0 <= i < j < n`, and +* `lower <= nums[i] + nums[j] <= upper` + +**Example 1:** + +**Input:** nums = [0,1,7,4,4,5], lower = 3, upper = 6 + +**Output:** 6 + +**Explanation:** There are 6 fair pairs: (0,3), (0,4), (0,5), (1,3), (1,4), and (1,5). + +**Example 2:** + +**Input:** nums = [1,7,9,2,5], lower = 11, upper = 11 + +**Output:** 1 + +**Explanation:** There is a single fair pair: (2,3). + +**Constraints:** + +* 1 <= nums.length <= 105 +* `nums.length == n` +* -109 <= nums[i] <= 109 +* -109 <= lower <= upper <= 109 + +## Solution + +```kotlin +class Solution { + fun countFairPairs(nums: IntArray, lower: Int, upper: Int): Long { + nums.sort() + return smaller(nums, upper) - smaller(nums, lower - 1) + } + + private fun smaller(nums: IntArray, value: Int): Long { + var l = 0 + var r = nums.size - 1 + var result: Long = 0 + while (l < r) { + val sum = nums[l] + nums[r] + if (sum <= value) { + result += (r - l).toLong() + l++ + } else { + r-- + } + } + return result + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g2501_2600/s2564_substring_xor_queries/readme.md b/src/main/kotlin/g2501_2600/s2564_substring_xor_queries/readme.md new file mode 100644 index 00000000..fe2b49e1 --- /dev/null +++ b/src/main/kotlin/g2501_2600/s2564_substring_xor_queries/readme.md @@ -0,0 +1,81 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork) + +## 2564\. Substring XOR Queries + +Medium + +You are given a **binary string** `s`, and a **2D** integer array `queries` where queries[i] = [firsti, secondi]. + +For the ith query, find the **shortest substring** of `s` whose **decimal value**, `val`, yields secondi when **bitwise XORed** with firsti. In other words, val ^ firsti == secondi. + +The answer to the ith query is the endpoints (**0-indexed**) of the substring [lefti, righti] or `[-1, -1]` if no such substring exists. If there are multiple answers, choose the one with the **minimum** lefti. + +_Return an array_ `ans` _where_ ans[i] = [lefti, righti] _is the answer to the_ ith _query._ + +A **substring** is a contiguous non-empty sequence of characters within a string. + +**Example 1:** + +**Input:** s = "101101", queries = \[\[0,5],[1,2]] + +**Output:** [[0,2],[2,3]] + +**Explanation:** For the first query the substring in range `[0,2]` is **"101"** which has a decimal value of **`5`**, and **`5 ^ 0 = 5`**, hence the answer to the first query is `[0,2]`. In the second query, the substring in range `[2,3]` is **"11",** and has a decimal value of **3**, and **3 `^ 1 = 2`**. So, `[2,3]` is returned for the second query. + +**Example 2:** + +**Input:** s = "0101", queries = \[\[12,8]] + +**Output:** [[-1,-1]] + +**Explanation:** In this example there is no substring that answers the query, hence `[-1,-1] is returned`. + +**Example 3:** + +**Input:** s = "1", queries = \[\[4,5]] + +**Output:** [[0,0]] + +**Explanation:** For this example, the substring in range `[0,0]` has a decimal value of **`1`**, and **`1 ^ 4 = 5`**. So, the answer is `[0,0]`. + +**Constraints:** + +* 1 <= s.length <= 104 +* `s[i]` is either `'0'` or `'1'`. +* 1 <= queries.length <= 105 +* 0 <= firsti, secondi <= 109 + +## Solution + +```kotlin +class Solution { + fun substringXorQueries(s: String, queries: Array): Array { + val n = s.length + val indices = queries.withIndex().groupBy( + keySelector = { it.value[0] xor it.value[1] }, + valueTransform = { it.index } + ).toMutableMap() + val res = Array(queries.size) { IntArray(2) { -1 } } + fun helper(value: Int, left: Int, right: Int) { + (indices.remove(value) ?: return).forEach { + res[it][0] = left + res[it][1] = right + } + } + for (i in 0 until n) { + if (s[i] == '0') { + helper(0, i, i) + } else { + var tmp = 0L + for (j in i until n) { + tmp = (tmp shl 1) + (s[j] - '0') + if (tmp > Int.MAX_VALUE) break + helper(tmp.toInt(), i, j) + } + } + } + return res + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g2501_2600/s2565_subsequence_with_the_minimum_score/readme.md b/src/main/kotlin/g2501_2600/s2565_subsequence_with_the_minimum_score/readme.md new file mode 100644 index 00000000..43bcb619 --- /dev/null +++ b/src/main/kotlin/g2501_2600/s2565_subsequence_with_the_minimum_score/readme.md @@ -0,0 +1,84 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork) + +## 2565\. Subsequence With the Minimum Score + +Hard + +You are given two strings `s` and `t`. + +You are allowed to remove any number of characters from the string `t`. + +The score of the string is `0` if no characters are removed from the string `t`, otherwise: + +* Let `left` be the minimum index among all removed characters. +* Let `right` be the maximum index among all removed characters. + +Then the score of the string is `right - left + 1`. + +Return _the minimum possible score to make_ `t`_a subsequence of_ `s`_._ + +A **subsequence** of a string is a new string that is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (i.e., `"ace"` is a subsequence of "abcde" while `"aec"` is not). + +**Example 1:** + +**Input:** s = "abacaba", t = "bzaa" + +**Output:** 1 + +**Explanation:** In this example, we remove the character "z" at index 1 (0-indexed). The string t becomes "baa" which is a subsequence of the string "abacaba" and the score is 1 - 1 + 1 = 1. It can be proven that 1 is the minimum score that we can achieve. + +**Example 2:** + +**Input:** s = "cde", t = "xyz" + +**Output:** 3 + +**Explanation:** In this example, we remove characters "x", "y" and "z" at indices 0, 1, and 2 (0-indexed). The string t becomes "" which is a subsequence of the string "cde" and the score is 2 - 0 + 1 = 3. It can be proven that 3 is the minimum score that we can achieve. + +**Constraints:** + +* 1 <= s.length, t.length <= 105 +* `s` and `t` consist of only lowercase English letters. + +## Solution + +```kotlin +class Solution { + fun minimumScore(s: String, t: String): Int { + val m = s.length + val n = t.length + val left = IntArray(m) + run { + var i = 0 + var j = 0 + while (i < m) { + if (j < n && s[i] == t[j]) { + ++j + } + left[i] = j + i++ + } + } + val right = IntArray(m) + run { + var i = m - 1 + var j = n - 1 + while (i >= 0) { + if (j >= 0 && s[i] == t[j]) { + --j + } + right[i] = j + i-- + } + } + var min = (n - left[m - 1]).coerceAtMost(right[0] + 1) + var i = 0 + while (i + 1 < m) { + min = min.coerceAtMost(0.coerceAtLeast(right[i + 1] - left[i] + 1)) + i++ + } + return min + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g2501_2600/s2566_maximum_difference_by_remapping_a_digit/readme.md b/src/main/kotlin/g2501_2600/s2566_maximum_difference_by_remapping_a_digit/readme.md new file mode 100644 index 00000000..11d291c0 --- /dev/null +++ b/src/main/kotlin/g2501_2600/s2566_maximum_difference_by_remapping_a_digit/readme.md @@ -0,0 +1,66 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork) + +## 2566\. Maximum Difference by Remapping a Digit + +Easy + +You are given an integer `num`. You know that Danny Mittal will sneakily **remap** one of the `10` possible digits (`0` to `9`) to another digit. + +Return _the difference between the maximum and minimum__ values Danny can make by remapping **exactly** **one** digit_ _in_ `num`. + +**Notes:** + +* When Danny remaps a digit d1 to another digit d2, Danny replaces all occurrences of `d1` in `num` with `d2`. +* Danny can remap a digit to itself, in which case `num` does not change. +* Danny can remap different digits for obtaining minimum and maximum values respectively. +* The resulting number after remapping can contain leading zeroes. +* We mentioned "Danny Mittal" to congratulate him on being in the top 10 in Weekly Contest 326. + +**Example 1:** + +**Input:** num = 11891 + +**Output:** 99009 + +**Explanation:** + +To achieve the maximum value, Danny can remap the digit 1 to the digit 9 to yield 99899. + +To achieve the minimum value, Danny can remap the digit 1 to the digit 0, yielding 890. The difference between these two numbers is 99009. + +**Example 2:** + +**Input:** num = 90 + +**Output:** 99 + +**Explanation:** + +The maximum value that can be returned by the function is 99 (if 0 is replaced by 9) and the minimum value that can be returned by the function is 0 (if 9 is replaced by 0). + +Thus, we return 99. + +**Constraints:** + +* 1 <= num <= 108 + +## Solution + +```kotlin +class Solution { + fun minMaxDifference(num: Int): Int { + val org = num.toString() + + val helper = { want: Char -> + val firstCharNot = org.firstOrNull { + it != want + } + if (firstCharNot == null) num + else org.replace(firstCharNot, want).toInt() + } + + return helper('9') - helper('0') + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g2501_2600/s2567_minimum_score_by_changing_two_elements/readme.md b/src/main/kotlin/g2501_2600/s2567_minimum_score_by_changing_two_elements/readme.md new file mode 100644 index 00000000..0546e624 --- /dev/null +++ b/src/main/kotlin/g2501_2600/s2567_minimum_score_by_changing_two_elements/readme.md @@ -0,0 +1,58 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork) + +## 2567\. Minimum Score by Changing Two Elements + +Medium + +You are given a **0-indexed** integer array `nums`. + +* The **low** score of `nums` is the minimum value of `|nums[i] - nums[j]|` over all `0 <= i < j < nums.length`. +* The **high** score of `nums` is the maximum value of `|nums[i] - nums[j]|` over all `0 <= i < j < nums.length`. +* The **score** of `nums` is the sum of the **high** and **low** scores of nums. + +To minimize the score of `nums`, we can change the value of **at most two** elements of `nums`. + +Return _the **minimum** possible **score** after changing the value of **at most two** elements o_f `nums`. + +Note that `|x|` denotes the absolute value of `x`. + +**Example 1:** + +**Input:** nums = [1,4,3] + +**Output:** 0 + +**Explanation:** Change value of nums[1] and nums[2] to 1 so that nums becomes [1,1,1]. Now, the value of `|nums[i] - nums[j]|` is always equal to 0, so we return 0 + 0 = 0. + +**Example 2:** + +**Input:** nums = [1,4,7,8,5] + +**Output:** 3 + +**Explanation:** Change nums[0] and nums[1] to be 6. Now nums becomes [6,6,7,8,5]. + +Our low score is achieved when i = 0 and j = 1, in which case |`nums[i] - nums[j]`| = |6 - 6| = 0. + +Our high score is achieved when i = 3 and j = 4, in which case |`nums[i] - nums[j]`| = |8 - 5| = 3. + +The sum of our high and low score is 3, which we can prove to be minimal. + +**Constraints:** + +* 3 <= nums.length <= 105 +* 1 <= nums[i] <= 109 + +## Solution + +```kotlin +class Solution { + fun minimizeSum(nums: IntArray): Int { + nums.sort() + return (0 until 3).map { + nums[nums.size - 3 + it] - nums[it] + }.min() + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g2501_2600/s2568_minimum_impossible_or/readme.md b/src/main/kotlin/g2501_2600/s2568_minimum_impossible_or/readme.md new file mode 100644 index 00000000..20bbf019 --- /dev/null +++ b/src/main/kotlin/g2501_2600/s2568_minimum_impossible_or/readme.md @@ -0,0 +1,48 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork) + +## 2568\. Minimum Impossible OR + +Medium + +You are given a **0-indexed** integer array `nums`. + +We say that an integer x is **expressible** from `nums` if there exist some integers 0 <= index1 < index2 < ... < indexk < nums.length for which nums[index1] | nums[index2] | ... | nums[indexk] = x. In other words, an integer is expressible if it can be written as the bitwise OR of some subsequence of `nums`. + +Return _the minimum **positive non-zero integer** that is not_ _expressible from_ `nums`. + +**Example 1:** + +**Input:** nums = [2,1] + +**Output:** 4 + +**Explanation:** 1 and 2 are already present in the array. We know that 3 is expressible, since nums[0] \| nums[1] = 2 \| 1 = 3. Since 4 is not expressible, we return 4. + +**Example 2:** + +**Input:** nums = [5,3,2] + +**Output:** 1 + +**Explanation:** We can show that 1 is the smallest number that is not expressible. + +**Constraints:** + +* 1 <= nums.length <= 105 +* 1 <= nums[i] <= 109 + +## Solution + +```kotlin +class Solution { + fun minImpossibleOR(nums: IntArray): Int { + val pow2s = nums.filter { + it and (it - 1) == 0 + }.toSet() + return 1 shl (0..31).first { + (1 shl it) !in pow2s + } + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g2501_2600/s2569_handling_sum_queries_after_update/readme.md b/src/main/kotlin/g2501_2600/s2569_handling_sum_queries_after_update/readme.md new file mode 100644 index 00000000..c6c168b4 --- /dev/null +++ b/src/main/kotlin/g2501_2600/s2569_handling_sum_queries_after_update/readme.md @@ -0,0 +1,74 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork) + +## 2569\. Handling Sum Queries After Update + +Hard + +You are given two **0-indexed** arrays `nums1` and `nums2` and a 2D array `queries` of queries. There are three types of queries: + +1. For a query of type 1, `queries[i] = [1, l, r]`. Flip the values from `0` to `1` and from `1` to `0` in `nums1` from index `l` to index `r`. Both `l` and `r` are **0-indexed**. +2. For a query of type 2, `queries[i] = [2, p, 0]`. For every index `0 <= i < n`, set `nums2[i] = nums2[i] + nums1[i] * p`. +3. For a query of type 3, `queries[i] = [3, 0, 0]`. Find the sum of the elements in `nums2`. + +Return _an array containing all the answers to the third type queries._ + +**Example 1:** + +**Input:** nums1 = [1,0,1], nums2 = [0,0,0], queries = \[\[1,1,1],[2,1,0],[3,0,0]] + +**Output:** [3] + +**Explanation:** After the first query nums1 becomes [1,1,1]. After the second query, nums2 becomes [1,1,1], so the answer to the third query is 3. Thus, [3] is returned. + +**Example 2:** + +**Input:** nums1 = [1], nums2 = [5], queries = \[\[2,0,0],[3,0,0]] + +**Output:** [5] + +**Explanation:** After the first query, nums2 remains [5], so the answer to the second query is 5. Thus, [5] is returned. + +**Constraints:** + +* 1 <= nums1.length,nums2.length <= 105 +* `nums1.length = nums2.length` +* 1 <= queries.length <= 105 +* `queries[i].length = 3` +* `0 <= l <= r <= nums1.length - 1` +* 0 <= p <= 106 +* `0 <= nums1[i] <= 1` +* 0 <= nums2[i] <= 109 + +## Solution + +```kotlin +import java.util.BitSet + +class Solution { + fun handleQuery(nums1: IntArray, nums2: IntArray, queries: Array): LongArray { + val results: MutableList = ArrayList() + val n = nums1.size + val bs = BitSet(n) + var sum: Long = 0 + for (i in 0 until n) { + sum += 1L * nums2[i] + if (nums1[i] == 1) { + bs.set(i) + } + } + for (query in queries) { + when (query[0]) { + 1 -> bs.flip(query[1], query[2] + 1) + 2 -> sum += 1L * query[1] * bs.cardinality() + else -> results.add(sum) + } + } + val ans = LongArray(results.size) + for (i in ans.indices) { + ans[i] = results[i] + } + return ans + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g2501_2600/s2570_merge_two_2d_arrays_by_summing_values/readme.md b/src/main/kotlin/g2501_2600/s2570_merge_two_2d_arrays_by_summing_values/readme.md new file mode 100644 index 00000000..1be9ae8f --- /dev/null +++ b/src/main/kotlin/g2501_2600/s2570_merge_two_2d_arrays_by_summing_values/readme.md @@ -0,0 +1,80 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork) + +## 2570\. Merge Two 2D Arrays by Summing Values + +Easy + +You are given two **2D** integer arrays `nums1` and `nums2.` + +* nums1[i] = [idi, vali] indicate that the number with the id idi has a value equal to vali. +* nums2[i] = [idi, vali] indicate that the number with the id idi has a value equal to vali. + +Each array contains **unique** ids and is sorted in **ascending** order by id. + +Merge the two arrays into one array that is sorted in ascending order by id, respecting the following conditions: + +* Only ids that appear in at least one of the two arrays should be included in the resulting array. +* Each id should be included **only once** and its value should be the sum of the values of this id in the two arrays. If the id does not exist in one of the two arrays then its value in that array is considered to be `0`. + +Return _the resulting array_. The returned array must be sorted in ascending order by id. + +**Example 1:** + +**Input:** nums1 = \[\[1,2],[2,3],[4,5]], nums2 = \[\[1,4],[3,2],[4,1]] + +**Output:** [[1,6],[2,3],[3,2],[4,6]] + +**Explanation:** The resulting array contains the following: + +- id = 1, the value of this id is 2 + 4 = 6. +- id = 2, the value of this id is 3. +- id = 3, the value of this id is 2. +- id = 4, the value of this id is 5 + 1 = 6. + +**Example 2:** + +**Input:** nums1 = \[\[2,4],[3,6],[5,5]], nums2 = \[\[1,3],[4,3]] + +**Output:** [[1,3],[2,4],[3,6],[4,3],[5,5]] + +**Explanation:** There are no common ids, so we just include each id with its value in the resulting list. + +**Constraints:** + +* `1 <= nums1.length, nums2.length <= 200` +* `nums1[i].length == nums2[j].length == 2` +* 1 <= idi, vali <= 1000 +* Both arrays contain unique ids. +* Both arrays are in strictly ascending order by id. + +## Solution + +```kotlin +class Solution { + fun mergeArrays(nums1: Array, nums2: Array): Array { + var p1 = 0 + var p2 = 0 + val result = mutableListOf() + while (p1 < nums1.size) { + if (p2 == nums2.size || nums1[p1][0] < nums2[p2][0]) { + result.add(intArrayOf(nums1[p1][0], nums1[p1][1])) + p1++ + } else if (nums1[p1][0] > nums2[p2][0]) { + result.add(intArrayOf(nums2[p2][0], nums2[p2][1])) + p2++ + } else { + result.add(intArrayOf(nums1[p1][0], nums1[p1][1] + nums2[p2][1])) + p1++ + p2++ + } + } + // Merge leftovers from nums2 + while (p2 < nums2.size) { + result.add(intArrayOf(nums2[p2][0], nums2[p2][1])) + p2++ + } + return result.toTypedArray() + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g2501_2600/s2571_minimum_operations_to_reduce_an_integer_to_0/readme.md b/src/main/kotlin/g2501_2600/s2571_minimum_operations_to_reduce_an_integer_to_0/readme.md new file mode 100644 index 00000000..8a2666a7 --- /dev/null +++ b/src/main/kotlin/g2501_2600/s2571_minimum_operations_to_reduce_an_integer_to_0/readme.md @@ -0,0 +1,69 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork) + +## 2571\. Minimum Operations to Reduce an Integer to 0 + +Medium + +You are given a positive integer `n`, you can do the following operation **any** number of times: + +* Add or subtract a **power** of `2` from `n`. + +Return _the **minimum** number of operations to make_ `n` _equal to_ `0`. + +A number `x` is power of `2` if x == 2i where `i >= 0`_._ + +**Example 1:** + +**Input:** n = 39 + +**Output:** 3 + +**Explanation:** We can do the following operations: + +- Add 20 = 1 to n, so now n = 40. + +- Subtract 23 = 8 from n, so now n = 32. + +- Subtract 25 = 32 from n, so now n = 0. It can be shown that 3 is the minimum number of operations we need to make n equal to 0. + +**Example 2:** + +**Input:** n = 54 + +**Output:** 3 + +**Explanation:** We can do the following operations: + +- Add 21 = 2 to n, so now n = 56. + +- Add 23 = 8 to n, so now n = 64. + +- Subtract 26 = 64 from n, so now n = 0. So the minimum number of operations is 3. + +**Constraints:** + +* 1 <= n <= 105 + +## Solution + +```kotlin +@Suppress("NAME_SHADOWING") +class Solution { + fun minOperations(n: Int): Int { + var n = n + var count = 0 + while (n > 0) { + val x = kotlin.math.ln(n.toDouble()) / kotlin.math.ln(2.0) + if (x % 1.0 < 0.5) { + n = kotlin.math.abs(n - Math.pow(2.0, x.toInt().toDouble()).toInt()) + count++ + } else { + n = kotlin.math.abs(n - Math.pow(2.0, (x.toInt() + 1).toDouble()).toInt()) + count++ + } + } + return count + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g2501_2600/s2572_count_the_number_of_square_free_subsets/readme.md b/src/main/kotlin/g2501_2600/s2572_count_the_number_of_square_free_subsets/readme.md new file mode 100644 index 00000000..e12ce875 --- /dev/null +++ b/src/main/kotlin/g2501_2600/s2572_count_the_number_of_square_free_subsets/readme.md @@ -0,0 +1,94 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork) + +## 2572\. Count the Number of Square-Free Subsets + +Medium + +You are given a positive integer **0-indexed** array `nums`. + +A subset of the array `nums` is **square-free** if the product of its elements is a **square-free integer**. + +A **square-free integer** is an integer that is divisible by no square number other than `1`. + +Return _the number of square-free non-empty subsets of the array_ **nums**. Since the answer may be too large, return it **modulo** 109 + 7. + +A **non-empty** **subset** of `nums` is an array that can be obtained by deleting some (possibly none but not all) elements from `nums`. Two subsets are different if and only if the chosen indices to delete are different. + +**Example 1:** + +**Input:** nums = [3,4,4,5] + +**Output:** 3 + +**Explanation:** There are 3 square-free subsets in this example: + +- The subset consisting of the 0th element [3]. The product of its elements is 3, which is a square-free integer. + +- The subset consisting of the 3rd element [5]. The product of its elements is 5, which is a square-free integer. + +- The subset consisting of 0th and 3rd elements [3,5]. The product of its elements is 15, which is a square-free integer. + +It can be proven that there are no more than 3 square-free subsets in the given array. + +**Example 2:** + +**Input:** nums = [1] + +**Output:** 1 + +**Explanation:** There is 1 square-free subset in this example: + +- The subset consisting of the 0th element [1]. The product of its elements is 1, which is a square-free integer. + +It can be proven that there is no more than 1 square-free subset in the given array. + +**Constraints:** + +* `1 <= nums.length <= 1000` +* `1 <= nums[i] <= 30` + +## Solution + +```kotlin +class Solution { + private val primes = intArrayOf(2, 3, 5, 7, 11, 13, 17, 19, 23, 29) + private val badNums = (1..30).filter { primes.any { p -> it % (p * p) == 0 } }.toSet() + private val nonPrimes = (2..30).filter { it !in primes }.filter { it !in badNums }.toList() + + private fun gcd(a: Int, b: Int): Int { + return if (b == 0) a else gcd(b, a % b) + } + + fun squareFreeSubsets(nums: IntArray): Int { + val mod: Long = 1_000_000_007 + // Get the frequency map + val freqMap = nums.toTypedArray().groupingBy { it }.eachCount() + var dp = mutableMapOf() + for (v in nonPrimes) { + if (v !in freqMap) continue + val howmany = freqMap[v]!! + val prev = HashMap(dp) + dp[v] = (dp[v] ?: 0) + howmany + for ((product, quantity) in prev) + if (gcd(product, v) == 1) + dp[product * v] = ((dp[product * v] ?: 0L) + quantity * howmany.toLong()) % mod + } + for (v in primes) { + if (v !in freqMap) continue + val howmany = freqMap[v]!!.toLong() + val prev = dp + dp = mutableMapOf() + dp[v] = howmany + for ((product, quantity) in prev) + dp[product] = if (product % v != 0) quantity * (1 + howmany) else quantity + } + // Getting all possible subsets without `1` + var res = dp.values.sum() % mod + // Find the permutations of `1` + val possible = (1..(freqMap[1] ?: 0)).fold(1L) { sum, _ -> (sum shl 1) % mod } + res = (res * possible + possible + mod - 1) % mod + return res.toInt() + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g2501_2600/s2573_find_the_string_with_lcp/readme.md b/src/main/kotlin/g2501_2600/s2573_find_the_string_with_lcp/readme.md new file mode 100644 index 00000000..cc126229 --- /dev/null +++ b/src/main/kotlin/g2501_2600/s2573_find_the_string_with_lcp/readme.md @@ -0,0 +1,114 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork) + +## 2573\. Find the String with LCP + +Hard + +We define the `lcp` matrix of any **0-indexed** string `word` of `n` lowercase English letters as an `n x n` grid such that: + +* `lcp[i][j]` is equal to the length of the **longest common prefix** between the substrings `word[i,n-1]` and `word[j,n-1]`. + +Given an `n x n` matrix `lcp`, return the alphabetically smallest string `word` that corresponds to `lcp`. If there is no such string, return an empty string. + +A string `a` is lexicographically smaller than a string `b` (of the same length) if in the first position where `a` and `b` differ, string `a` has a letter that appears earlier in the alphabet than the corresponding letter in `b`. For example, `"aabd"` is lexicographically smaller than `"aaca"` because the first position they differ is at the third letter, and `'b'` comes before `'c'`. + +**Example 1:** + +**Input:** lcp = \[\[4,0,2,0],[0,3,0,1],[2,0,2,0],[0,1,0,1]] + +**Output:** "abab" + +**Explanation:** lcp corresponds to any 4 letter string with two alternating letters. The lexicographically smallest of them is "abab". + +**Example 2:** + +**Input:** lcp = \[\[4,3,2,1],[3,3,2,1],[2,2,2,1],[1,1,1,1]] + +**Output:** "aaaa" + +**Explanation:** lcp corresponds to any 4 letter string with a single distinct letter. The lexicographically smallest of them is "aaaa". + +**Example 3:** + +**Input:** lcp = \[\[4,3,2,1],[3,3,2,1],[2,2,2,1],[1,1,1,3]] + +**Output:** "" + +**Explanation:** lcp[3][3] cannot be equal to 3 since word[3,...,3] consists of only a single letter; Thus, no answer exists. + +**Constraints:** + +* `1 <= n == ``lcp.length ==` `lcp[i].length` `<= 1000` +* `0 <= lcp[i][j] <= n` + +## Solution + +```kotlin +@Suppress("NAME_SHADOWING") +class Solution { + fun findTheString(lcp: Array): String { + val n = lcp.size + val parent = IntArray(n) + val rank = IntArray(n) + val chars = IntArray(n) + val str = IntArray(n) + for (i in 0 until n) { + parent[i] = i + rank[i] = 1 + } + for (i in 0 until n) { + for (j in i + 1 until n) { + if (lcp[i][j] > 0) { + union(parent, rank, i, j) + } + } + } + var c = 0 + var par: Int + for (i in 0 until n) { + par = find(parent, i) + if (chars[par] == 0) { + chars[par] = ++c + } + if (c > 26) return "" + str[i] = chars[par] + } + var `val`: Int + val lcpNew = Array(n) { IntArray(n) } + for (i in n - 1 downTo 0) { + for (j in n - 1 downTo 0) { + `val` = if (i + 1 < n && j + 1 < n) lcpNew[i + 1][j + 1] else 0 + `val` = if (str[i] == str[j]) 1 + `val` else 0 + lcpNew[i][j] = `val` + if (lcpNew[i][j] != lcp[i][j]) return "" + } + } + val sb = StringBuilder() + for (e in str) { + sb.append((e + 'a'.code - 1).toChar()) + } + return sb.toString() + } + + private fun find(parent: IntArray, x: Int): Int { + return if (x == parent[x]) x else find(parent, parent[x]).also { parent[x] = it } + } + + private fun union(parent: IntArray, rank: IntArray, u: Int, v: Int) { + var u = u + var v = v + u = find(parent, u) + v = find(parent, v) + if (u == v) return + if (rank[u] >= rank[v]) { + parent[v] = u + rank[u] += rank[v] + } else { + parent[u] = v + rank[v] += rank[u] + } + return + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g2501_2600/s2574_left_and_right_sum_differences/readme.md b/src/main/kotlin/g2501_2600/s2574_left_and_right_sum_differences/readme.md new file mode 100644 index 00000000..75a0ab29 --- /dev/null +++ b/src/main/kotlin/g2501_2600/s2574_left_and_right_sum_differences/readme.md @@ -0,0 +1,60 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork) + +## 2574\. Left and Right Sum Differences + +Easy + +Given a **0-indexed** integer array `nums`, find a **0-indexed** integer array `answer` where: + +* `answer.length == nums.length`. +* `answer[i] = |leftSum[i] - rightSum[i]|`. + +Where: + +* `leftSum[i]` is the sum of elements to the left of the index `i` in the array `nums`. If there is no such element, `leftSum[i] = 0`. +* `rightSum[i]` is the sum of elements to the right of the index `i` in the array `nums`. If there is no such element, `rightSum[i] = 0`. + +Return _the array_ `answer`. + +**Example 1:** + +**Input:** nums = [10,4,8,3] + +**Output:** [15,1,11,22] + +**Explanation:** The array leftSum is [0,10,14,22] and the array rightSum is [15,11,3,0]. The array answer is [\|0 - 15\|,\|10 - 11\|,\|14 - 3\|,\|22 - 0\|] = [15,1,11,22]. + +**Example 2:** + +**Input:** nums = [1] + +**Output:** [0] + +**Explanation:** The array leftSum is [0] and the array rightSum is [0]. The array answer is [\|0 - 0\|] = [0]. + +**Constraints:** + +* `1 <= nums.length <= 1000` +* 1 <= nums[i] <= 105 + +## Solution + +```kotlin +class Solution { + fun leftRightDifference(nums: IntArray): IntArray { + val n = nums.size + val ans: IntArray = IntArray(n) + var rightSum = nums.sum() + var leftSum = 0 + + for (i in nums.indices) { + rightSum = rightSum - nums[i] + ans[i] = Math.abs(leftSum - rightSum) + leftSum += nums[i] + } + + return ans + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g2501_2600/s2575_find_the_divisibility_array_of_a_string/readme.md b/src/main/kotlin/g2501_2600/s2575_find_the_divisibility_array_of_a_string/readme.md new file mode 100644 index 00000000..60577ccd --- /dev/null +++ b/src/main/kotlin/g2501_2600/s2575_find_the_divisibility_array_of_a_string/readme.md @@ -0,0 +1,67 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork) + +## 2575\. Find the Divisibility Array of a String + +Medium + +You are given a **0-indexed** string `word` of length `n` consisting of digits, and a positive integer `m`. + +The **divisibility array** `div` of `word` is an integer array of length `n` such that: + +* `div[i] = 1` if the **numeric value** of `word[0,...,i]` is divisible by `m`, or +* `div[i] = 0` otherwise. + +Return _the divisibility array of_ `word`. + +**Example 1:** + +**Input:** word = "998244353", m = 3 + +**Output:** [1,1,0,0,0,1,1,0,0] + +**Explanation:** There are only 4 prefixes that are divisible by 3: "9", "99", "998244", and "9982443". + +**Example 2:** + +**Input:** word = "1010", m = 10 + +**Output:** [0,1,0,1] + +**Explanation:** There are only 2 prefixes that are divisible by 10: "10", and "1010". + +**Constraints:** + +* 1 <= n <= 105 +* `word.length == n` +* `word` consists of digits from `0` to `9` +* 1 <= m <= 109 + +## Solution + +```kotlin +class Solution { + fun divisibilityArray(word: String, m: Int): IntArray { + val ans = IntArray(word.length) + val prevRemainder = StringBuilder() + + for (i in word.indices) { + val number = prevRemainder.append(word[i]).toString().toLong() + if (number != 0L && number < m) { + continue + } + + val remainder = number % m + prevRemainder.clear() + + if (remainder == 0L) { + ans[i] = 1 + } else { + prevRemainder.append(remainder) + } + } + + return ans + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g2501_2600/s2576_find_the_maximum_number_of_marked_indices/readme.md b/src/main/kotlin/g2501_2600/s2576_find_the_maximum_number_of_marked_indices/readme.md new file mode 100644 index 00000000..07b11cc0 --- /dev/null +++ b/src/main/kotlin/g2501_2600/s2576_find_the_maximum_number_of_marked_indices/readme.md @@ -0,0 +1,74 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork) + +## 2576\. Find the Maximum Number of Marked Indices + +Medium + +You are given a **0-indexed** integer array `nums`. + +Initially, all of the indices are unmarked. You are allowed to make this operation any number of times: + +* Pick two **different unmarked** indices `i` and `j` such that `2 * nums[i] <= nums[j]`, then mark `i` and `j`. + +Return _the maximum possible number of marked indices in `nums` using the above operation any number of times_. + +**Example 1:** + +**Input:** nums = [3,5,2,4] + +**Output:** 2 + +**Explanation:** In the first operation: pick i = 2 and j = 1, the operation is allowed because 2 \* nums[2] <= nums[1]. Then mark index 2 and 1. It can be shown that there's no other valid operation so the answer is 2. + +**Example 2:** + +**Input:** nums = [9,2,5,4] + +**Output:** 4 + +**Explanation:** In the first operation: pick i = 3 and j = 0, the operation is allowed because 2 \* nums[3] <= nums[0]. + +Then mark index 3 and 0. + +In the second operation: pick i = 1 and j = 2, the operation is allowed because 2 \* nums[1] <= nums[2]. Then mark index 1 and 2. + +Since there is no other operation, the answer is 4. + +**Example 3:** + +**Input:** nums = [7,6,8] + +**Output:** 0 + +**Explanation:** There is no valid operation to do, so the answer is 0. + +**Constraints:** + +* 1 <= nums.length <= 105 +* 1 <= nums[i] <= 109 + +## Solution + +```kotlin +class Solution { + fun maxNumOfMarkedIndices(nums: IntArray): Int { + nums.sort() + var count = 0 + var i = 0 + var j = nums.size / 2 + + while (j < nums.size && i < nums.size / 2) { + if (nums[i] * 2 <= nums[j]) { + count += 2 + i++ + j++ + } else { + j++ + } + } + + return count + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g2501_2600/s2577_minimum_time_to_visit_a_cell_in_a_grid/readme.md b/src/main/kotlin/g2501_2600/s2577_minimum_time_to_visit_a_cell_in_a_grid/readme.md new file mode 100644 index 00000000..d0fd4e40 --- /dev/null +++ b/src/main/kotlin/g2501_2600/s2577_minimum_time_to_visit_a_cell_in_a_grid/readme.md @@ -0,0 +1,96 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork) + +## 2577\. Minimum Time to Visit a Cell In a Grid + +Hard + +You are given a `m x n` matrix `grid` consisting of **non-negative** integers where `grid[row][col]` represents the **minimum** time required to be able to visit the cell `(row, col)`, which means you can visit the cell `(row, col)` only when the time you visit it is greater than or equal to `grid[row][col]`. + +You are standing in the **top-left** cell of the matrix in the 0th second, and you must move to **any** adjacent cell in the four directions: up, down, left, and right. Each move you make takes 1 second. + +Return _the **minimum** time required in which you can visit the bottom-right cell of the matrix_. If you cannot visit the bottom-right cell, then return `-1`. + +**Example 1:** + +![](https://assets.leetcode.com/uploads/2023/02/14/yetgriddrawio-8.png) + +**Input:** grid = \[\[0,1,3,2],[5,1,2,5],[4,3,8,6]] + +**Output:** 7 + +**Explanation:** One of the paths that we can take is the following: +- at t = 0, we are on the cell (0,0). +- at t = 1, we move to the cell (0,1). It is possible because grid[0][1] <= 1. +- at t = 2, we move to the cell (1,1). It is possible because grid[1][1] <= 2. +- at t = 3, we move to the cell (1,2). It is possible because grid[1][2] <= 3. +- at t = 4, we move to the cell (1,1). It is possible because grid[1][1] <= 4. +- at t = 5, we move to the cell (1,2). It is possible because grid[1][2] <= 5. +- at t = 6, we move to the cell (1,3). It is possible because grid[1][3] <= 6. +- at t = 7, we move to the cell (2,3). It is possible because grid[2][3] <= 7. + +The final time is 7. It can be shown that it is the minimum time possible. + +**Example 2:** + +![](https://assets.leetcode.com/uploads/2023/02/14/yetgriddrawio-9.png) + +**Input:** grid = \[\[0,2,4],[3,2,1],[1,0,4]] + +**Output:** -1 + +**Explanation:** There is no path from the top left to the bottom-right cell. + +**Constraints:** + +* `m == grid.length` +* `n == grid[i].length` +* `2 <= m, n <= 1000` +* 4 <= m * n <= 105 +* 0 <= grid[i][j] <= 105 +* `grid[0][0] == 0` + +## Solution + +```kotlin +import java.util.PriorityQueue + +class Solution { + fun minimumTime(grid: Array): Int { + if (grid[0][1] <= 1 || grid[1][0] <= 1) { + val m = grid.size + val n = grid[0].size + val pq = PriorityQueue { a: IntArray, b: IntArray -> + a[0] - b[0] + } + val dist: MutableMap = HashMap() + pq.offer(intArrayOf(0, 0, 0)) + dist.put("0,0", 0) + while (pq.isNotEmpty()) { + val curr = pq.poll() + val x = curr[0] + val i = curr[1] + val j = curr[2] + if (i == m - 1 && j == n - 1) { + return x + } + val directions = + arrayOf(intArrayOf(i - 1, j), intArrayOf(i, j - 1), intArrayOf(i, j + 1), intArrayOf(i + 1, j)) + for (dir in directions) { + val ii = dir[0] + val jj = dir[1] + if (ii in 0 until m && jj >= 0 && jj < n) { + val xx = x + 1 + 0.coerceAtLeast((grid[ii][jj] - x) / 2 * 2) + val key = "$ii,$jj" + if (!dist.containsKey(key) || dist[key]!! > xx) { + pq.offer(intArrayOf(xx, ii, jj)) + dist.put(key, xx) + } + } + } + } + } + return -1 + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g2501_2600/s2578_split_with_minimum_sum/readme.md b/src/main/kotlin/g2501_2600/s2578_split_with_minimum_sum/readme.md new file mode 100644 index 00000000..fa378df5 --- /dev/null +++ b/src/main/kotlin/g2501_2600/s2578_split_with_minimum_sum/readme.md @@ -0,0 +1,67 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork) + +## 2578\. Split With Minimum Sum + +Easy + +Given a positive integer `num`, split it into two non-negative integers `num1` and `num2` such that: + +* The concatenation of `num1` and `num2` is a permutation of `num`. + * In other words, the sum of the number of occurrences of each digit in `num1` and `num2` is equal to the number of occurrences of that digit in `num`. +* `num1` and `num2` can contain leading zeros. + +Return _the **minimum** possible sum of_ `num1` _and_ `num2`. + +**Notes:** + +* It is guaranteed that `num` does not contain any leading zeros. +* The order of occurrence of the digits in `num1` and `num2` may differ from the order of occurrence of `num`. + +**Example 1:** + +**Input:** num = 4325 + +**Output:** 59 + +**Explanation:** We can split 4325 so that `num1` is 24 and num2 `is` 35, giving a sum of 59. We can prove that 59 is indeed the minimal possible sum. + +**Example 2:** + +**Input:** num = 687 + +**Output:** 75 + +**Explanation:** We can split 687 so that `num1` is 68 and `num2` is 7, which would give an optimal sum of 75. + +**Constraints:** + +* 10 <= num <= 109 + +## Solution + +```kotlin +@Suppress("NAME_SHADOWING") +class Solution { + fun splitNum(num: Int): Int { + var num = num + val ans = IntArray(10) + while (num > 0) { + ans[num % 10]++ + num /= 10 + } + var num1 = 0 + var num2 = 0 + for (i in 0..9) { + for (j in 0 until ans[i]) { + if (num1 <= num2) { + num1 = num1 * 10 + i + } else { + num2 = num2 * 10 + i + } + } + } + return num1 + num2 + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g2501_2600/s2579_count_total_number_of_colored_cells/readme.md b/src/main/kotlin/g2501_2600/s2579_count_total_number_of_colored_cells/readme.md new file mode 100644 index 00000000..d4f2ab5f --- /dev/null +++ b/src/main/kotlin/g2501_2600/s2579_count_total_number_of_colored_cells/readme.md @@ -0,0 +1,47 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork) + +## 2579\. Count Total Number of Colored Cells + +Medium + +There exists an infinitely large two-dimensional grid of uncolored unit cells. You are given a positive integer `n`, indicating that you must do the following routine for `n` minutes: + +* At the first minute, color **any** arbitrary unit cell blue. +* Every minute thereafter, color blue **every** uncolored cell that touches a blue cell. + +Below is a pictorial representation of the state of the grid after minutes 1, 2, and 3. + +![](https://assets.leetcode.com/uploads/2023/01/10/example-copy-2.png) + +Return _the number of **colored cells** at the end of_ `n` _minutes_. + +**Example 1:** + +**Input:** n = 1 + +**Output:** 1 + +**Explanation:** After 1 minute, there is only 1 blue cell, so we return 1. + +**Example 2:** + +**Input:** n = 2 + +**Output:** 5 + +**Explanation:** After 2 minutes, there are 4 colored cells on the boundary and 1 in the center, so we return 5. + +**Constraints:** + +* 1 <= n <= 105 + +## Solution + +```kotlin +class Solution { + fun coloredCells(n: Int): Long { + return Math.pow(n.toDouble(), 2.0).toLong() + Math.pow((n - 1).toDouble(), 2.0).toLong() + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g2501_2600/s2580_count_ways_to_group_overlapping_ranges/readme.md b/src/main/kotlin/g2501_2600/s2580_count_ways_to_group_overlapping_ranges/readme.md new file mode 100644 index 00000000..01a20d07 --- /dev/null +++ b/src/main/kotlin/g2501_2600/s2580_count_ways_to_group_overlapping_ranges/readme.md @@ -0,0 +1,107 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork) + +## 2580\. Count Ways to Group Overlapping Ranges + +Medium + +You are given a 2D integer array `ranges` where ranges[i] = [starti, endi] denotes that all integers between starti and endi (both **inclusive**) are contained in the ith range. + +You are to split `ranges` into **two** (possibly empty) groups such that: + +* Each range belongs to exactly one group. +* Any two **overlapping** ranges must belong to the **same** group. + +Two ranges are said to be **overlapping** if there exists at least **one** integer that is present in both ranges. + +* For example, `[1, 3]` and `[2, 5]` are overlapping because `2` and `3` occur in both ranges. + +Return _the **total number** of ways to split_ `ranges` _into two groups_. Since the answer may be very large, return it **modulo** 109 + 7. + +**Example 1:** + +**Input:** ranges = \[\[6,10],[5,15]] + +**Output:** 2 + +**Explanation:** + +The two ranges are overlapping, so they must be in the same group. + +Thus, there are two possible ways: + +- Put both the ranges together in group 1. + +- Put both the ranges together in group 2. + +**Example 2:** + +**Input:** ranges = \[\[1,3],[10,20],[2,5],[4,8]] + +**Output:** 4 + +**Explanation:** + +Ranges [1,3], and [2,5] are overlapping. So, they must be in the same group. + +Again, ranges [2,5] and [4,8] are also overlapping. So, they must also be in the same group. + +Thus, there are four possible ways to group them: + +- All the ranges in group 1. + +- All the ranges in group 2. + +- Ranges [1,3], [2,5], and [4,8] in group 1 and [10,20] in group 2. + +- Ranges [1,3], [2,5], and [4,8] in group 2 and [10,20] in group 1. + +**Constraints:** + +* 1 <= ranges.length <= 105 +* `ranges[i].length == 2` +* 0 <= starti <= endi <= 109 + +## Solution + +```kotlin +import java.util.Arrays + +@Suppress("NAME_SHADOWING") +class Solution { + fun countWays(ranges: Array): Int { + var cnt = 1 + Arrays.sort(ranges) { a, b -> if (a[0] != b[0]) a[0] - b[0] else a[1] - b[1] } + var curr = ranges[0] + for (i in 1 until ranges.size) { + if (ranges[i][1] < curr[0] || ranges[i][0] > curr[1]) { + ++cnt + curr = ranges[i] + } else { + curr[1] = Math.max(curr[1], ranges[i][1]) + } + } + return powMod(2, cnt.toLong()).toInt() + } + + private fun powMod(b: Long, e: Long): Long { + var b = b + var e = e + var ans: Long = 1 + while (e != 0L) { + if (e and 1L == 1L) { + ans *= b + ans %= MOD.toLong() + } + b *= b + b %= MOD.toLong() + e = e shr 1 + } + return ans + } + + companion object { + var MOD = 1e9.toInt() + 7 + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g2501_2600/s2581_count_number_of_possible_root_nodes/readme.md b/src/main/kotlin/g2501_2600/s2581_count_number_of_possible_root_nodes/readme.md new file mode 100644 index 00000000..86c0f2aa --- /dev/null +++ b/src/main/kotlin/g2501_2600/s2581_count_number_of_possible_root_nodes/readme.md @@ -0,0 +1,137 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork) + +## 2581\. Count Number of Possible Root Nodes + +Hard + +Alice has an undirected tree with `n` nodes labeled from `0` to `n - 1`. The tree is represented as a 2D integer array `edges` of length `n - 1` where edges[i] = [ai, bi] indicates that there is an edge between nodes ai and bi in the tree. + +Alice wants Bob to find the root of the tree. She allows Bob to make several **guesses** about her tree. In one guess, he does the following: + +* Chooses two **distinct** integers `u` and `v` such that there exists an edge `[u, v]` in the tree. +* He tells Alice that `u` is the **parent** of `v` in the tree. + +Bob's guesses are represented by a 2D integer array `guesses` where guesses[j] = [uj, vj] indicates Bob guessed uj to be the parent of vj. + +Alice being lazy, does not reply to each of Bob's guesses, but just says that **at least** `k` of his guesses are `true`. + +Given the 2D integer arrays `edges`, `guesses` and the integer `k`, return _the **number of possible nodes** that can be the root of Alice's tree_. If there is no such tree, return `0`. + +**Example 1:** + +![](https://assets.leetcode.com/uploads/2022/12/19/ex-1.png) + +**Input:** edges = \[\[0,1],[1,2],[1,3],[4,2]], guesses = \[\[1,3],[0,1],[1,0],[2,4]], k = 3 + +**Output:** 3 + +**Explanation:** + +Root = 0, correct guesses = [1,3], [0,1], [2,4] + +Root = 1, correct guesses = [1,3], [1,0], [2,4] + +Root = 2, correct guesses = [1,3], [1,0], [2,4] + +Root = 3, correct guesses = [1,0], [2,4] + +Root = 4, correct guesses = [1,3], [1,0] + +Considering 0, 1, or 2 as root node leads to 3 correct guesses. + +**Example 2:** + +![](https://assets.leetcode.com/uploads/2022/12/19/ex-2.png) + +**Input:** edges = \[\[0,1],[1,2],[2,3],[3,4]], guesses = \[\[1,0],[3,4],[2,1],[3,2]], k = 1 + +**Output:** 5 + +**Explanation:** + +Root = 0, correct guesses = [3,4] + +Root = 1, correct guesses = [1,0], [3,4] + +Root = 2, correct guesses = [1,0], [2,1], [3,4] + +Root = 3, correct guesses = [1,0], [2,1], [3,2], [3,4] + +Root = 4, correct guesses = [1,0], [2,1], [3,2] + +Considering any node as root will give at least 1 correct guess. + +**Constraints:** + +* `edges.length == n - 1` +* 2 <= n <= 105 +* 1 <= guesses.length <= 105 +* 0 <= ai, bi, uj, vj <= n - 1 +* ai != bi +* uj != vj +* `edges` represents a valid tree. +* `guesses[j]` is an edge of the tree. +* `guesses` is unique. +* `0 <= k <= guesses.length` + +## Solution + +```kotlin +import java.util.ArrayList +import java.util.HashSet + +@Suppress("NAME_SHADOWING") +class Solution { + private lateinit var parents: IntArray + private lateinit var graph: Array?> + private lateinit var guess: Array?> + private var ans = 0 + + fun rootCount(edges: Array, guesses: Array, k: Int): Int { + val n = edges.size + 1 + graph = arrayOfNulls(n) + guess = arrayOfNulls(n) + for (i in 0 until n) { + graph[i] = ArrayList() + guess[i] = HashSet() + } + // Create tree + for (i in edges.indices) { + graph[edges[i][0]]!!.add(edges[i][1]) + graph[edges[i][1]]!!.add(edges[i][0]) + } + // Create guess array + for (i in guesses.indices) { + guess[guesses[i][0]]!!.add(guesses[i][1]) + } + parents = IntArray(n) + fill(0, -1) + var c = 0 + for (i in 1 until n) { + if (guess[parents[i]]!!.contains(i)) c++ + } + if (c >= k) ans++ + for (i in graph[0]!!) dfs(i, 0, c, k) + return ans + } + + // Fill the parent array + private fun fill(v: Int, p: Int) { + parents[v] = p + for (child in graph[v]!!) { + if (child == p) continue + fill(child, v) + } + } + + // Use DFS to make all nodes as root one by one + private fun dfs(v: Int, p: Int, c: Int, k: Int) { + var c = c + if (guess[p]!!.contains(v)) c-- + if (guess[v]!!.contains(p)) c++ + if (c >= k) ans++ + for (child in graph[v]!!) if (child != p) dfs(child, v, c, k) + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g2501_2600/s2582_pass_the_pillow/readme.md b/src/main/kotlin/g2501_2600/s2582_pass_the_pillow/readme.md new file mode 100644 index 00000000..5cc5e817 --- /dev/null +++ b/src/main/kotlin/g2501_2600/s2582_pass_the_pillow/readme.md @@ -0,0 +1,46 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork) + +## 2582\. Pass the Pillow + +Easy + +There are `n` people standing in a line labeled from `1` to `n`. The first person in the line is holding a pillow initially. Every second, the person holding the pillow passes it to the next person standing in the line. Once the pillow reaches the end of the line, the direction changes, and people continue passing the pillow in the opposite direction. + +* For example, once the pillow reaches the nth person they pass it to the n - 1th person, then to the n - 2th person and so on. + +Given the two positive integers `n` and `time`, return _the index of the person holding the pillow after_ `time` _seconds_. + +**Example 1:** + +**Input:** n = 4, time = 5 + +**Output:** 2 + +**Explanation:** People pass the pillow in the following way: 1 -> 2 -> 3 -> 4 -> 3 -> 2. Afer five seconds, the pillow is given to the 2nd person. + +**Example 2:** + +**Input:** n = 3, time = 2 + +**Output:** 3 + +**Explanation:** People pass the pillow in the following way: 1 -> 2 -> 3. Afer two seconds, the pillow is given to the 3rd person. + +**Constraints:** + +* `2 <= n <= 1000` +* `1 <= time <= 1000` + +## Solution + +```kotlin +@Suppress("NAME_SHADOWING") +class Solution { + fun passThePillow(n: Int, time: Int): Int { + var time = time + time %= ((n - 1) * 2) + return n - kotlin.math.abs(n - 1 - time) + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g2501_2600/s2583_kth_largest_sum_in_a_binary_tree/readme.md b/src/main/kotlin/g2501_2600/s2583_kth_largest_sum_in_a_binary_tree/readme.md new file mode 100644 index 00000000..3bdb9b8e --- /dev/null +++ b/src/main/kotlin/g2501_2600/s2583_kth_largest_sum_in_a_binary_tree/readme.md @@ -0,0 +1,81 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork) + +## 2583\. Kth Largest Sum in a Binary Tree + +Medium + +You are given the `root` of a binary tree and a positive integer `k`. + +The **level sum** in the tree is the sum of the values of the nodes that are on the **same** level. + +Return _the_ kth _**largest** level sum in the tree (not necessarily distinct)_. If there are fewer than `k` levels in the tree, return `-1`. + +**Note** that two nodes are on the same level if they have the same distance from the root. + +**Example 1:** + +![](https://assets.leetcode.com/uploads/2022/12/14/binaryytreeedrawio-2.png) + +**Input:** root = [5,8,9,2,1,3,7,4,6], k = 2 + +**Output:** 13 + +**Explanation:** The level sums are the following: + +- Level 1: 5. - Level 2: 8 + 9 = 17. + +- Level 3: 2 + 1 + 3 + 7 = 13. + +- Level 4: 4 + 6 = 10. The 2nd largest level sum is 13. + +**Example 2:** + +![](https://assets.leetcode.com/uploads/2022/12/14/treedrawio-3.png) + +**Input:** root = [1,2,null,3], k = 1 + +**Output:** 3 + +**Explanation:** The largest level sum is 3. + +**Constraints:** + +* The number of nodes in the tree is `n`. +* 2 <= n <= 105 +* 1 <= Node.val <= 106 +* `1 <= k <= n` + +## Solution + +```kotlin +import com_github_leetcode.TreeNode +import java.util.LinkedList +import java.util.PriorityQueue +import java.util.Queue + +class Solution { + fun kthLargestLevelSum(root: TreeNode?, k: Int): Long { + val heap: Queue = PriorityQueue() + val levels: Queue = LinkedList() + levels.offer(root) + while (levels.isNotEmpty()) { + var sum: Long = 0 + val size: Int = levels.size + for (i in 0 until size) { + val node = levels.poll() + sum += node!!.`val` + if (node.left != null) levels.offer(node.left) + if (node.right != null) levels.offer(node.right) + } + if (heap.size < k) { + heap.offer(sum) + } else if (heap.peek() < sum) { + heap.poll() + heap.offer(sum) + } + } + return if (heap.size < k) -1 else heap.peek() + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g2501_2600/s2584_split_the_array_to_make_coprime_products/readme.md b/src/main/kotlin/g2501_2600/s2584_split_the_array_to_make_coprime_products/readme.md new file mode 100644 index 00000000..fdad0f21 --- /dev/null +++ b/src/main/kotlin/g2501_2600/s2584_split_the_array_to_make_coprime_products/readme.md @@ -0,0 +1,99 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork) + +## 2584\. Split the Array to Make Coprime Products + +Hard + +You are given a **0-indexed** integer array `nums` of length `n`. + +A **split** at an index `i` where `0 <= i <= n - 2` is called **valid** if the product of the first `i + 1` elements and the product of the remaining elements are coprime. + +* For example, if `nums = [2, 3, 3]`, then a split at the index `i = 0` is valid because `2` and `9` are coprime, while a split at the index `i = 1` is not valid because `6` and `3` are not coprime. A split at the index `i = 2` is not valid because `i == n - 1`. + +Return _the smallest index_ `i` _at which the array can be split validly or_ `-1` _if there is no such split_. + +Two values `val1` and `val2` are coprime if `gcd(val1, val2) == 1` where `gcd(val1, val2)` is the greatest common divisor of `val1` and `val2`. + +**Example 1:** + +![](https://assets.leetcode.com/uploads/2022/12/14/second.PNG) + +**Input:** nums = [4,7,8,15,3,5] + +**Output:** 2 + +**Explanation:** The table above shows the values of the product of the first i + 1 elements, the remaining elements, and their gcd at each index i. The only valid split is at index 2. + +**Example 2:** + +![](https://assets.leetcode.com/uploads/2022/12/14/capture.PNG) + +**Input:** nums = [4,7,15,8,3,5] + +**Output:** -1 + +**Explanation:** The table above shows the values of the product of the first i + 1 elements, the remaining elements, and their gcd at each index i. There is no valid split. + +**Constraints:** + +* `n == nums.length` +* 1 <= n <= 104 +* 1 <= nums[i] <= 106 + +## Solution + +```kotlin +class Solution { + private fun fillDivideArray() { + for (i in 1 until divideTo.size) { + divideTo[i] = i + } + var i = 2 + while (i * i < divideTo.size) { + if (divideTo[i] != i) { + i++ + continue + } + var j = i + i + while (j < divideTo.size) { + if (divideTo[j] == j) { + divideTo[j] = i + } + j += i + } + i++ + } + } + + fun findValidSplit(nums: IntArray): Int { + if (divideTo[1] == 0) { + fillDivideArray() + } + + val dMap: MutableMap = HashMap() + val dividers: Array?> = arrayOfNulls(nums.size) + for (i in nums.indices) { + dividers[i] = ArrayList() + while (nums[i] != 1) { + dividers[i]?.add(divideTo[nums[i]]) + dMap.put(divideTo[nums[i]], i) + nums[i] = nums[i] / divideTo[nums[i]] + } + } + var nextEnd = 0 + var i = 0 + while (i <= nextEnd) { + for (j in dividers[i]!!.indices) { + nextEnd = nextEnd.coerceAtLeast(dMap[dividers[i]!![j]]!!) + } + i++ + } + return if (i == nums.size) -1 else i - 1 + } + + companion object { + var divideTo = IntArray(1e6.toInt() + 1) + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g2501_2600/s2585_number_of_ways_to_earn_points/readme.md b/src/main/kotlin/g2501_2600/s2585_number_of_ways_to_earn_points/readme.md new file mode 100644 index 00000000..6257a9a3 --- /dev/null +++ b/src/main/kotlin/g2501_2600/s2585_number_of_ways_to_earn_points/readme.md @@ -0,0 +1,79 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork) + +## 2585\. Number of Ways to Earn Points + +Hard + +There is a test that has `n` types of questions. You are given an integer `target` and a **0-indexed** 2D integer array `types` where types[i] = [counti, marksi] indicates that there are counti questions of the ith type, and each one of them is worth marksi points. + +Return _the number of ways you can earn **exactly**_ `target` _points in the exam_. Since the answer may be too large, return it **modulo** 109 + 7. + +**Note** that questions of the same type are indistinguishable. + +* For example, if there are `3` questions of the same type, then solving the 1st and 2nd questions is the same as solving the 1st and 3rd questions, or the 2nd and 3rd questions. + +**Example 1:** + +**Input:** target = 6, types = \[\[6,1],[3,2],[2,3]] + +**Output:** 7 + +**Explanation:** You can earn 6 points in one of the seven ways: + +- Solve 6 questions of the 0th type: 1 + 1 + 1 + 1 + 1 + 1 = 6 +- Solve 4 questions of the 0th type and 1 question of the 1st type: 1 + 1 + 1 + 1 + 2 = 6 +- Solve 2 questions of the 0th type and 2 questions of the 1st type: 1 + 1 + 2 + 2 = 6 +- Solve 3 questions of the 0th type and 1 question of the 2nd type: 1 + 1 + 1 + 3 = 6 +- Solve 1 question of the 0th type, 1 question of the 1st type and 1 question of the 2nd type: 1 + 2 + 3 = 6 +- Solve 3 questions of the 1st type: 2 + 2 + 2 = 6 - Solve 2 questions of the 2nd type: 3 + 3 = 6 + +**Example 2:** + +**Input:** target = 5, types = \[\[50,1],[50,2],[50,5]] + +**Output:** 4 + +**Explanation:** You can earn 5 points in one of the four ways: + +- Solve 5 questions of the 0th type: 1 + 1 + 1 + 1 + 1 = 5 +- Solve 3 questions of the 0th type and 1 question of the 1st type: 1 + 1 + 1 + 2 = 5 +- Solve 1 questions of the 0th type and 2 questions of the 1st type: 1 + 2 + 2 = 5 +- Solve 1 question of the 2nd type: 5 + +**Example 3:** + +**Input:** target = 18, types = \[\[6,1],[3,2],[2,3]] + +**Output:** 1 + +**Explanation:** You can only earn 18 points by answering all questions. + +**Constraints:** + +* `1 <= target <= 1000` +* `n == types.length` +* `1 <= n <= 50` +* `types[i].length == 2` +* 1 <= counti, marksi <= 50 + +## Solution + +```kotlin +class Solution { + fun waysToReachTarget(target: Int, types: Array): Int { + val kMod = 1000000007 + val dp = Array(types.size + 1) { IntArray(target + 1) } + dp[0][0] = 1 + for (i in 1..types.size) { + val count = types[i - 1][0] + val mark = types[i - 1][1] + for (j in 0..target) for (solved in 0..count) if (j - solved * mark >= 0) { + dp[i][j] += dp[i - 1][j - solved * mark] + dp[i][j] %= kMod + } + } + return dp[types.size][target] + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g2501_2600/s2586_count_the_number_of_vowel_strings_in_range/readme.md b/src/main/kotlin/g2501_2600/s2586_count_the_number_of_vowel_strings_in_range/readme.md new file mode 100644 index 00000000..3f5fbfeb --- /dev/null +++ b/src/main/kotlin/g2501_2600/s2586_count_the_number_of_vowel_strings_in_range/readme.md @@ -0,0 +1,71 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork) + +## 2586\. Count the Number of Vowel Strings in Range + +Easy + +You are given a **0-indexed** array of string `words` and two integers `left` and `right`. + +A string is called a **vowel string** if it starts with a vowel character and ends with a vowel character where vowel characters are `'a'`, `'e'`, `'i'`, `'o'`, and `'u'`. + +Return _the number of vowel strings_ `words[i]` _where_ `i` _belongs to the inclusive range_ `[left, right]`. + +**Example 1:** + +**Input:** words = ["are","amy","u"], left = 0, right = 2 + +**Output:** 2 + +**Explanation:** + +- "are" is a vowel string because it starts with 'a' and ends with 'e'. + +- "amy" is not a vowel string because it does not end with a vowel. + +- "u" is a vowel string because it starts with 'u' and ends with 'u'. The number of vowel strings in the mentioned range is 2. + +**Example 2:** + +**Input:** words = ["hey","aeo","mu","ooo","artro"], left = 1, right = 4 + +**Output:** 3 + +**Explanation:** + +- "aeo" is a vowel string because it starts with 'a' and ends with 'o'. + +- "mu" is not a vowel string because it does not start with a vowel. + +- "ooo" is a vowel string because it starts with 'o' and ends with 'o'. + +- "artro" is a vowel string because it starts with 'a' and ends with 'o'. + +The number of vowel strings in the mentioned range is 3. + +**Constraints:** + +* `1 <= words.length <= 1000` +* `1 <= words[i].length <= 10` +* `words[i]` consists of only lowercase English letters. +* `0 <= left <= right < words.length` + +## Solution + +```kotlin +class Solution { + fun vowelStrings(words: Array, left: Int, right: Int): Int { + var count = 0 + for (i in left..right) { + if (isVowel(words[i][0]) && isVowel(words[i][words[i].length - 1])) { + count++ + } + } + return count + } + + private fun isVowel(ch: Char): Boolean { + return ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g2501_2600/s2587_rearrange_array_to_maximize_prefix_score/readme.md b/src/main/kotlin/g2501_2600/s2587_rearrange_array_to_maximize_prefix_score/readme.md new file mode 100644 index 00000000..7043a10a --- /dev/null +++ b/src/main/kotlin/g2501_2600/s2587_rearrange_array_to_maximize_prefix_score/readme.md @@ -0,0 +1,56 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork) + +## 2587\. Rearrange Array to Maximize Prefix Score + +Medium + +You are given a **0-indexed** integer array `nums`. You can rearrange the elements of `nums` to **any order** (including the given order). + +Let `prefix` be the array containing the prefix sums of `nums` after rearranging it. In other words, `prefix[i]` is the sum of the elements from `0` to `i` in `nums` after rearranging it. The **score** of `nums` is the number of positive integers in the array `prefix`. + +Return _the maximum score you can achieve_. + +**Example 1:** + +**Input:** nums = [2,-1,0,1,-3,3,-3] + +**Output:** 6 + +**Explanation:** + +We can rearrange the array into nums = [2,3,1,-1,-3,0,-3]. + +prefix = [2,5,6,5,2,2,-1], so the score is 6. + +It can be shown that 6 is the maximum score we can obtain. + +**Example 2:** + +**Input:** nums = [-2,-3,0] + +**Output:** 0 + +**Explanation:** Any rearrangement of the array will result in a score of 0. + +**Constraints:** + +* 1 <= nums.length <= 105 +* -106 <= nums[i] <= 106 + +## Solution + +```kotlin +class Solution { + fun maxScore(nums: IntArray): Int { + nums.sort() + var count = 0 + var sum: Long = 0 + for (i in nums.indices.reversed()) { + sum += nums[i].toLong() + if (sum > 0) count++ + } + return count + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g2501_2600/s2588_count_the_number_of_beautiful_subarrays/readme.md b/src/main/kotlin/g2501_2600/s2588_count_the_number_of_beautiful_subarrays/readme.md new file mode 100644 index 00000000..95cb745c --- /dev/null +++ b/src/main/kotlin/g2501_2600/s2588_count_the_number_of_beautiful_subarrays/readme.md @@ -0,0 +1,66 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork) + +## 2588\. Count the Number of Beautiful Subarrays + +Medium + +You are given a **0-indexed** integer array `nums`. In one operation, you can: + +* Choose two different indices `i` and `j` such that `0 <= i, j < nums.length`. +* Choose a non-negative integer `k` such that the kth bit (**0-indexed**) in the binary representation of `nums[i]` and `nums[j]` is `1`. +* Subtract 2k from `nums[i]` and `nums[j]`. + +A subarray is **beautiful** if it is possible to make all of its elements equal to `0` after applying the above operation any number of times. + +Return _the number of **beautiful subarrays** in the array_ `nums`. + +A subarray is a contiguous **non-empty** sequence of elements within an array. + +**Example 1:** + +**Input:** nums = [4,3,1,2,4] + +**Output:** 2 + +**Explanation:** There are 2 beautiful subarrays in nums: [4,3,1,2,4] and [4,3,1,2,4]. +- We can make all elements in the subarray [3,1,2] equal to 0 in the following way: + - Choose [3, 1, 2] and k = 1. Subtract 21 from both numbers. The subarray becomes [1, 1, 0]. + - Choose [1, 1, 0] and k = 0. Subtract 20 from both numbers. The subarray becomes [0, 0, 0]. +- We can make all elements in the subarray [4,3,1,2,4] equal to 0 in the following way: + - Choose [4, 3, 1, 2, 4] and k = 2. Subtract 22 from both numbers. The subarray becomes [0, 3, 1, 2, 0]. + - Choose [0, 3, 1, 2, 0] and k = 0. Subtract 20 from both numbers. The subarray becomes [0, 2, 0, 2, 0]. + - Choose [0, 2, 0, 2, 0] and k = 1. Subtract 21 from both numbers. The subarray becomes [0, 0, 0, 0, 0]. + +**Example 2:** + +**Input:** nums = [1,10,4] + +**Output:** 0 + +**Explanation:** There are no beautiful subarrays in nums. + +**Constraints:** + +* 1 <= nums.length <= 105 +* 0 <= nums[i] <= 106 + +## Solution + +```kotlin +class Solution { + fun beautifulSubarrays(nums: IntArray): Long { + val map = mutableMapOf() + map[0] = 1 + var res = 0L + var sum = 0 + for (v in nums) { + sum = sum xor v + val count = map.getOrDefault(sum, 0) + res += count + map[sum] = count + 1 + } + return res + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g2501_2600/s2589_minimum_time_to_complete_all_tasks/readme.md b/src/main/kotlin/g2501_2600/s2589_minimum_time_to_complete_all_tasks/readme.md new file mode 100644 index 00000000..51093110 --- /dev/null +++ b/src/main/kotlin/g2501_2600/s2589_minimum_time_to_complete_all_tasks/readme.md @@ -0,0 +1,82 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork) + +## 2589\. Minimum Time to Complete All Tasks + +Hard + +There is a computer that can run an unlimited number of tasks **at the same time**. You are given a 2D integer array `tasks` where tasks[i] = [starti, endi, durationi] indicates that the ith task should run for a total of durationi seconds (not necessarily continuous) within the **inclusive** time range [starti, endi]. + +You may turn on the computer only when it needs to run a task. You can also turn it off if it is idle. + +Return _the minimum time during which the computer should be turned on to complete all tasks_. + +**Example 1:** + +**Input:** tasks = \[\[2,3,1],[4,5,1],[1,5,2]] + +**Output:** 2 + +**Explanation:** +- The first task can be run in the inclusive time range [2, 2]. +- The second task can be run in the inclusive time range [5, 5]. +- The third task can be run in the two inclusive time ranges [2, 2] and [5, 5]. The computer will be on for a total of 2 seconds. + +**Example 2:** + +**Input:** tasks = \[\[1,3,2],[2,5,3],[5,6,2]] + +**Output:** 4 + +**Explanation:** +- The first task can be run in the inclusive time range [2, 3]. +- The second task can be run in the inclusive time ranges [2, 3] and [5, 5]. +- The third task can be run in the two inclusive time range [5, 6]. + +The computer will be on for a total of 4 seconds. + +**Constraints:** + +* `1 <= tasks.length <= 2000` +* `tasks[i].length == 3` +* 1 <= starti, endi <= 2000 +* 1 <= durationi <= endi - starti + 1 + +## Solution + +```kotlin +import java.util.Arrays + +class Solution { + fun findMinimumTime(tasks: Array): Int { + var res = 0 + val arr = BooleanArray(2001) + Arrays.sort(tasks) { a: IntArray, b: IntArray -> + a[1] - b[1] + } + for (task in tasks) { + val start = task[0] + val end = task[1] + val dur = task[2] + var cur = 0 + for (i in start..end) { + if (arr[i]) { + cur++ + } + } + if (cur < dur) { + var i = end + while (i >= start && cur < dur) { + if (!arr[i]) { + arr[i] = true + res++ + cur++ + } + i-- + } + } + } + return res + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g2501_2600/s2591_distribute_money_to_maximum_children/readme.md b/src/main/kotlin/g2501_2600/s2591_distribute_money_to_maximum_children/readme.md new file mode 100644 index 00000000..e90959c0 --- /dev/null +++ b/src/main/kotlin/g2501_2600/s2591_distribute_money_to_maximum_children/readme.md @@ -0,0 +1,60 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork) + +## 2591\. Distribute Money to Maximum Children + +Easy + +You are given an integer `money` denoting the amount of money (in dollars) that you have and another integer `children` denoting the number of children that you must distribute the money to. + +You have to distribute the money according to the following rules: + +* All money must be distributed. +* Everyone must receive at least `1` dollar. +* Nobody receives `4` dollars. + +Return _the **maximum** number of children who may receive **exactly**_ `8` _dollars if you distribute the money according to the aforementioned rules_. If there is no way to distribute the money, return `-1`. + +**Example 1:** + +**Input:** money = 20, children = 3 + +**Output:** 1 + +**Explanation:** The maximum number of children with 8 dollars will be 1. One of the ways to distribute the money is: +- 8 dollars to the first child. +- 9 dollars to the second child. +- 3 dollars to the third child. + +It can be proven that no distribution exists such that number of children getting 8 dollars is greater than 1. + +**Example 2:** + +**Input:** money = 16, children = 2 + +**Output:** 2 + +**Explanation:** Each child can be given 8 dollars. + +**Constraints:** + +* `1 <= money <= 200` +* `2 <= children <= 30` + +## Solution + +```kotlin +@Suppress("NAME_SHADOWING") +class Solution { + fun distMoney(money: Int, children: Int): Int { + var money = money + if (children > money) return -1 + money -= children + var ans: Int = money / 7 + if (ans == children && money % 7 == 0) return ans + if (ans >= children) return children - 1 + if (money % 7 == 3 && ans == children - 1) ans-- + return ans + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g2501_2600/s2592_maximize_greatness_of_an_array/readme.md b/src/main/kotlin/g2501_2600/s2592_maximize_greatness_of_an_array/readme.md new file mode 100644 index 00000000..1a5456d1 --- /dev/null +++ b/src/main/kotlin/g2501_2600/s2592_maximize_greatness_of_an_array/readme.md @@ -0,0 +1,58 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork) + +## 2592\. Maximize Greatness of an Array + +Medium + +You are given a 0-indexed integer array `nums`. You are allowed to permute `nums` into a new array `perm` of your choosing. + +We define the **greatness** of `nums` be the number of indices `0 <= i < nums.length` for which `perm[i] > nums[i]`. + +Return _the **maximum** possible greatness you can achieve after permuting_ `nums`. + +**Example 1:** + +**Input:** nums = [1,3,5,2,1,3,1] + +**Output:** 4 + +**Explanation:** One of the optimal rearrangements is perm = [2,5,1,3,3,1,1]. At indices = 0, 1, 3, and 4, perm[i] > nums[i]. Hence, we return 4. + +**Example 2:** + +**Input:** nums = [1,2,3,4] + +**Output:** 3 + +**Explanation:** We can prove the optimal perm is [2,3,4,1]. At indices = 0, 1, and 2, perm[i] > nums[i]. Hence, we return 3. + +**Constraints:** + +* 1 <= nums.length <= 105 +* 0 <= nums[i] <= 109 + +## Solution + +```kotlin +import java.util.TreeMap + +class Solution { + fun maximizeGreatness(nums: IntArray): Int { + val map = TreeMap() + for (num in nums) map.compute(num) { _, n -> (n ?: 0) + 1 } + + var count = 0 + for (num in nums) { + val entry = map.higherEntry(num) + if (entry != null && entry.key != num) { + count++ + if (entry.value - 1 == 0) map.remove(entry.key) + else map[entry.key] = entry.value - 1 + } + } + + return count + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g2501_2600/s2593_find_score_of_an_array_after_marking_all_elements/readme.md b/src/main/kotlin/g2501_2600/s2593_find_score_of_an_array_after_marking_all_elements/readme.md new file mode 100644 index 00000000..8b119fa3 --- /dev/null +++ b/src/main/kotlin/g2501_2600/s2593_find_score_of_an_array_after_marking_all_elements/readme.md @@ -0,0 +1,78 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork) + +## 2593\. Find Score of an Array After Marking All Elements + +Medium + +You are given an array `nums` consisting of positive integers. + +Starting with `score = 0`, apply the following algorithm: + +* Choose the smallest integer of the array that is not marked. If there is a tie, choose the one with the smallest index. +* Add the value of the chosen integer to `score`. +* Mark **the chosen element and its two adjacent elements if they exist**. +* Repeat until all the array elements are marked. + +Return _the score you get after applying the above algorithm_. + +**Example 1:** + +**Input:** nums = [2,1,3,4,5,2] + +**Output:** 7 + +**Explanation:** We mark the elements as follows: +- 1 is the smallest unmarked element, so we mark it and its two adjacent elements: [2,1,3,4,5,2]. +- 2 is the smallest unmarked element, so we mark it and its left adjacent element: [2,1,3,4,5,2]. +- 4 is the only remaining unmarked element, so we mark it: [2,1,3,4,5,2]. Our score is 1 + 2 + 4 = 7. + +**Example 2:** + +**Input:** nums = [2,3,5,1,3,2] + +**Output:** 5 + +**Explanation:** We mark the elements as follows: +- 1 is the smallest unmarked element, so we mark it and its two adjacent elements: [2,3,5,1,3,2]. +- 2 is the smallest unmarked element, since there are two of them, we choose the left-most one, so we mark the one at index 0 and its right adjacent element: [2,3,5,1,3,2]. +- 2 is the only remaining unmarked element, so we mark it: [2,3,5,1,3,2]. Our score is 1 + 2 + 2 = 5. + +**Constraints:** + +* 1 <= nums.length <= 105 +* 1 <= nums[i] <= 106 + +## Solution + +```kotlin +import java.util.PriorityQueue + +class Solution { + fun findScore(nums: IntArray): Long { + var ans: Long = 0 + val pq = PriorityQueue { a: IntArray, b: IntArray -> + if (a[0] == b[0] + ) a[1] - b[1] else a[0] - b[0] + } + val vis = BooleanArray(nums.size) + for (i in nums.indices) { + pq.offer(intArrayOf(nums[i], i)) + } + while (pq.isNotEmpty()) { + val it = pq.poll() + if (!vis[it[1]]) { + vis[it[1]] = true + ans += it[0].toLong() + if (it[1] > 0) { + vis[it[1] - 1] = true + } + if (it[1] < nums.size - 1) { + vis[it[1] + 1] = true + } + } + } + return ans + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g2501_2600/s2594_minimum_time_to_repair_cars/readme.md b/src/main/kotlin/g2501_2600/s2594_minimum_time_to_repair_cars/readme.md new file mode 100644 index 00000000..dd485432 --- /dev/null +++ b/src/main/kotlin/g2501_2600/s2594_minimum_time_to_repair_cars/readme.md @@ -0,0 +1,89 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork) + +## 2594\. Minimum Time to Repair Cars + +Medium + +You are given an integer array `ranks` representing the **ranks** of some mechanics. ranksi is the rank of the ith mechanic. A mechanic with a rank `r` can repair n cars in r * n2 minutes. + +You are also given an integer `cars` representing the total number of cars waiting in the garage to be repaired. + +Return _the **minimum** time taken to repair all the cars._ + +**Note:** All the mechanics can repair the cars simultaneously. + +**Example 1:** + +**Input:** ranks = [4,2,3,1], cars = 10 + +**Output:** 16 + +**Explanation:** + +- The first mechanic will repair two cars. The time required is 4 \* 2 \* 2 = 16 minutes. + +- The second mechanic will repair two cars. The time required is 2 \* 2 \* 2 = 8 minutes. + +- The third mechanic will repair two cars. The time required is 3 \* 2 \* 2 = 12 minutes. + +- The fourth mechanic will repair four cars. The time required is 1 \* 4 \* 4 = 16 minutes. + +It can be proved that the cars cannot be repaired in less than 16 minutes. + +**Example 2:** + +**Input:** ranks = [5,1,8], cars = 6 + +**Output:** 16 + +**Explanation:** + +- The first mechanic will repair one car. The time required is 5 \* 1 \* 1 = 5 minutes. + +- The second mechanic will repair four cars. The time required is 1 \* 4 \* 4 = 16 minutes. + +- The third mechanic will repair one car. The time required is 8 \* 1 \* 1 = 8 minutes. + +It can be proved that the cars cannot be repaired in less than 16 minutes. + +**Constraints:** + +* 1 <= ranks.length <= 105 +* `1 <= ranks[i] <= 100` +* 1 <= cars <= 106 + +## Solution + +```kotlin +@Suppress("NAME_SHADOWING") +class Solution { + fun repairCars(ranks: IntArray, cars: Int): Long { + ranks.sort() + var low: Long = 0 + var hi = Long.MAX_VALUE + var ans: Long = -1 + while (low <= hi) { + val mid = (low + hi) / 2 + if (isPossible(mid, ranks, cars)) { + hi = mid - 1 + ans = mid + } else { + low = mid + 1 + } + } + return ans + } + + private fun isPossible(target: Long, ranks: IntArray, cars: Int): Boolean { + var cars = cars + for (i in ranks.indices) { + cars -= Math.sqrt((target / ranks[i]).toDouble()).toInt() + if (cars <= 0) { + return true + } + } + return false + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g2501_2600/s2595_number_of_even_and_odd_bits/readme.md b/src/main/kotlin/g2501_2600/s2595_number_of_even_and_odd_bits/readme.md new file mode 100644 index 00000000..d77b856c --- /dev/null +++ b/src/main/kotlin/g2501_2600/s2595_number_of_even_and_odd_bits/readme.md @@ -0,0 +1,72 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork) + +## 2595\. Number of Even and Odd Bits + +Easy + +You are given a **positive** integer `n`. + +Let `even` denote the number of even indices in the binary representation of `n` (**0-indexed**) with value `1`. + +Let `odd` denote the number of odd indices in the binary representation of `n` (**0-indexed**) with value `1`. + +Return _an integer array_ `answer` _where_ `answer = [even, odd]`. + +**Example 1:** + +**Input:** n = 17 + +**Output:** [2,0] + +**Explanation:** + +The binary representation of 17 is 10001. + +It contains 1 on the 0th and 4th indices. + +There are 2 even and 0 odd indices. + +**Example 2:** + +**Input:** n = 2 + +**Output:** [0,1] + +**Explanation:** + +The binary representation of 2 is 10. + +It contains 1 on the 1st index. + +There are 0 even and 1 odd indices. + +**Constraints:** + +* `1 <= n <= 1000` + +## Solution + +```kotlin +@Suppress("NAME_SHADOWING") +class Solution { + fun evenOddBit(n: Int): IntArray { + var n = n + var flag = 1 + var even = 0 + var odd = 0 + while (n != 0) { + val bit = n and 1 + if (bit == 1 && flag == 1) { + even++ + } + if (bit == 1 && flag != 1) { + odd++ + } + flag = Math.abs(flag - 1) + n = n ushr 1 + } + return intArrayOf(even, odd) + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g2501_2600/s2596_check_knight_tour_configuration/readme.md b/src/main/kotlin/g2501_2600/s2596_check_knight_tour_configuration/readme.md new file mode 100644 index 00000000..697f10a5 --- /dev/null +++ b/src/main/kotlin/g2501_2600/s2596_check_knight_tour_configuration/readme.md @@ -0,0 +1,78 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork) + +## 2596\. Check Knight Tour Configuration + +Medium + +There is a knight on an `n x n` chessboard. In a valid configuration, the knight starts **at the top-left cell** of the board and visits every cell on the board **exactly once**. + +You are given an `n x n` integer matrix `grid` consisting of distinct integers from the range `[0, n * n - 1]` where `grid[row][col]` indicates that the cell `(row, col)` is the grid[row][col]th cell that the knight visited. The moves are **0-indexed**. + +Return `true` _if_ `grid` _represents a valid configuration of the knight's movements or_ `false` _otherwise_. + +**Note** that a valid knight move consists of moving two squares vertically and one square horizontally, or two squares horizontally and one square vertically. The figure below illustrates all the possible eight moves of a knight from some cell. + +![](https://assets.leetcode.com/uploads/2018/10/12/knight.png) + +**Example 1:** + +![](https://assets.leetcode.com/uploads/2022/12/28/yetgriddrawio-5.png) + +**Input:** grid = \[\[0,11,16,5,20],[17,4,19,10,15],[12,1,8,21,6],[3,18,23,14,9],[24,13,2,7,22]] + +**Output:** true + +**Explanation:** The above diagram represents the grid. It can be shown that it is a valid configuration. + +**Example 2:** + +![](https://assets.leetcode.com/uploads/2022/12/28/yetgriddrawio-6.png) + +**Input:** grid = \[\[0,3,6],[5,8,1],[2,7,4]] + +**Output:** false + +**Explanation:** The above diagram represents the grid. The 8th move of the knight is not valid considering its position after the 7th move. + +**Constraints:** + +* `n == grid.length == grid[i].length` +* `3 <= n <= 7` +* `0 <= grid[row][col] < n * n` +* All integers in `grid` are **unique**. + +## Solution + +```kotlin +class Solution { + fun checkValidGrid(grid: Array): Boolean { + if (grid[0][0] != 0) { + return false + } + val n = grid.size + val m = grid[0].size + val rmove = intArrayOf(2, 2, -2, -2, 1, 1, -1, -1) + val cmove = intArrayOf(1, -1, 1, -1, 2, -2, 2, -2) + var cnt = 0 + for (i in 0 until n) { + for (j in 0 until m) { + val `val` = grid[i][j] + var isPoss = false + for (d in 0..7) { + val r = i + rmove[d] + val c = j + cmove[d] + if (r >= 0 && c >= 0 && r < n && c < m && grid[r][c] == `val` + 1) { + isPoss = true + } + } + if (!isPoss) { + cnt++ + } + if (cnt > 1) return false + } + } + return true + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g2501_2600/s2597_the_number_of_beautiful_subsets/readme.md b/src/main/kotlin/g2501_2600/s2597_the_number_of_beautiful_subsets/readme.md new file mode 100644 index 00000000..f5642fc1 --- /dev/null +++ b/src/main/kotlin/g2501_2600/s2597_the_number_of_beautiful_subsets/readme.md @@ -0,0 +1,93 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork) + +## 2597\. The Number of Beautiful Subsets + +Medium + +You are given an array `nums` of positive integers and a **positive** integer `k`. + +A subset of `nums` is **beautiful** if it does not contain two integers with an absolute difference equal to `k`. + +Return _the number of **non-empty beautiful** subsets of the array_ `nums`. + +A **subset** of `nums` is an array that can be obtained by deleting some (possibly none) elements from `nums`. Two subsets are different if and only if the chosen indices to delete are different. + +**Example 1:** + +**Input:** nums = [2,4,6], k = 2 + +**Output:** 4 + +**Explanation:** + +The beautiful subsets of the array nums are: [2], [4], [6], [2, 6]. + +It can be proved that there are only 4 beautiful subsets in the array [2,4,6]. + +**Example 2:** + +**Input:** nums = [1], k = 1 + +**Output:** 1 + +**Explanation:** + +The beautiful subset of the array nums is [1]. + +It can be proved that there is only 1 beautiful subset in the array [1]. + +**Constraints:** + +* `1 <= nums.length <= 20` +* `1 <= nums[i], k <= 1000` + +## Solution + +```kotlin +class Solution { + fun beautifulSubsets(nums: IntArray, k: Int): Int { + val map: MutableMap = HashMap() + for (n in nums) { + map[n] = map.getOrDefault(n, 0) + 1 + } + var res = 1 + for (key in map.keys) { + if (!map.containsKey(key - k)) { + if (!map.containsKey(key + k)) { + res *= 1 shl map[key]!! + } else { + val freq: MutableList = ArrayList() + var localKey = key + while (map.containsKey(localKey)) { + freq.add(map[localKey]) + localKey += k + } + res *= helper(freq) + } + } + } + return res - 1 + } + + private fun helper(freq: List): Int { + val n = freq.size + if (n == 1) { + return 1 shl freq[0]!! + } + val dp = IntArray(n) + dp[0] = (1 shl freq[0]!!) - 1 + dp[1] = (1 shl freq[1]!!) - 1 + if (n == 2) { + return dp[0] + dp[1] + 1 + } + for (i in 2 until n) { + if (i > 2) { + dp[i - 2] += dp[i - 3] + } + dp[i] = (dp[i - 2] + 1) * ((1 shl freq[i]!!) - 1) + } + return dp[n - 1] + dp[n - 2] + dp[n - 3] + 1 + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g2501_2600/s2598_smallest_missing_non_negative_integer_after_operations/readme.md b/src/main/kotlin/g2501_2600/s2598_smallest_missing_non_negative_integer_after_operations/readme.md new file mode 100644 index 00000000..e4b4554c --- /dev/null +++ b/src/main/kotlin/g2501_2600/s2598_smallest_missing_non_negative_integer_after_operations/readme.md @@ -0,0 +1,89 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork) + +## 2598\. Smallest Missing Non-negative Integer After Operations + +Medium + +You are given a **0-indexed** integer array `nums` and an integer `value`. + +In one operation, you can add or subtract `value` from any element of `nums`. + +* For example, if `nums = [1,2,3]` and `value = 2`, you can choose to subtract `value` from `nums[0]` to make `nums = [-1,2,3]`. + +The MEX (minimum excluded) of an array is the smallest missing **non-negative** integer in it. + +* For example, the MEX of `[-1,2,3]` is `0` while the MEX of `[1,0,3]` is `2`. + +Return _the maximum MEX of_ `nums` _after applying the mentioned operation **any number of times**_. + +**Example 1:** + +**Input:** nums = [1,-10,7,13,6,8], value = 5 + +**Output:** 4 + +**Explanation:** + +One can achieve this result by applying the following operations: + +- Add value to nums[1] twice to make nums = [1,**0**,7,13,6,8] + +- Subtract value from nums[2] once to make nums = [1,0,**2**,13,6,8] + +- Subtract value from nums[3] twice to make nums = [1,0,2,**3**,6,8] + +The MEX of nums is 4. It can be shown that 4 is the maximum MEX we can achieve. + +**Example 2:** + +**Input:** nums = [1,-10,7,13,6,8], value = 7 + +**Output:** 2 + +**Explanation:** + +One can achieve this result by applying the following operation: + +- subtract value from nums[2] once to make nums = [1,-10,**0**,13,6,8] + +The MEX of nums is 2. It can be shown that 2 is the maximum MEX we can achieve. + +**Constraints:** + +* 1 <= nums.length, value <= 105 +* -109 <= nums[i] <= 109 + +## Solution + +```kotlin +class Solution { + fun findSmallestInteger(nums: IntArray, value: Int): Int { + val n = nums.size + if (value == 1) return n + val a = IntArray(value) + for (i in 0 until n) { + var k = nums[i] % value + if (k < 0) k = (value + k) % value + a[k]++ + } + val mins = mins(a) + val min = mins[0] + val minIndex = mins[1] + return min * value + minIndex + } + + private fun mins(a: IntArray): IntArray { + val n = a.size + var min = 100001 + var minIndex = -1 + for (i in 0 until n) { + if (a[i] < min) { + min = a[i] + minIndex = i + } + } + return intArrayOf(min, minIndex) + } +} +``` \ No newline at end of file