From 96b389d70b74c1733473e8aa75c3f7be6495ec4f Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Sun, 5 May 2024 19:49:00 +0300 Subject: [PATCH] Added tasks 3127-3134 --- README.md | 300 +++++++++--------- .../g0001_0100/s0089_gray_code/readme.md | 8 +- .../readme.md | 14 +- .../s1020_number_of_enclaves/readme.md | 57 ++-- .../s1396_design_underground_system/readme.md | 8 +- .../readme.md | 10 +- .../readme.md | 41 ++- .../readme.md | 6 +- .../readme.md | 6 +- .../readme.md | 6 +- .../readme.md | 4 +- .../readme.md | 3 +- .../readme.md | 4 +- .../readme.md | 4 +- .../readme.md | 3 +- .../readme.md | 4 +- .../readme.md | 7 +- .../readme.md | 4 +- .../readme.md | 4 +- .../s2402_meeting_rooms_iii/readme.md | 4 +- .../readme.md | 4 +- .../readme.md | 5 +- .../s2542_maximum_subsequence_score/readme.md | 6 +- .../readme.md | 4 +- .../readme.md | 4 +- .../readme.md | 4 +- .../s2719_count_of_integers/readme.md | 4 +- .../readme.md | 6 +- .../readme.md | 2 +- .../readme.md | 78 +++++ .../s3128_right_triangles/readme.md | 106 +++++++ .../readme.md | 120 +++++++ .../readme.md | 142 +++++++++ .../readme.md | 69 ++++ .../readme.md | 76 +++++ .../s3133_minimum_array_end/readme.md | 69 ++++ .../readme.md | 103 ++++++ 37 files changed, 1014 insertions(+), 285 deletions(-) create mode 100644 src/main/kotlin/g3101_3200/s3127_make_a_square_with_the_same_color/readme.md create mode 100644 src/main/kotlin/g3101_3200/s3128_right_triangles/readme.md create mode 100644 src/main/kotlin/g3101_3200/s3129_find_all_possible_stable_binary_arrays_i/readme.md create mode 100644 src/main/kotlin/g3101_3200/s3130_find_all_possible_stable_binary_arrays_ii/readme.md create mode 100644 src/main/kotlin/g3101_3200/s3131_find_the_integer_added_to_array_i/readme.md create mode 100644 src/main/kotlin/g3101_3200/s3132_find_the_integer_added_to_array_ii/readme.md create mode 100644 src/main/kotlin/g3101_3200/s3133_minimum_array_end/readme.md create mode 100644 src/main/kotlin/g3101_3200/s3134_find_the_median_of_the_uniqueness_array/readme.md diff --git a/README.md b/README.md index 0ed2502a..f90d2ea2 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,6 @@ [![](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) > ["For coding interview preparation, LeetCode is one of the best online resource providing a rich library of more than 300 real coding interview questions for you to practice from using one of the 7 supported languages - C, C++, Java, Python, C#, JavaScript, Ruby."](https://www.quora.com/How-effective-is-Leetcode-for-preparing-for-technical-interviews) -* [Programming Skills II](#programming-skills-ii) * [Graph Theory I](#graph-theory-i) * [SQL I](#sql-i) * [Level 1](#level-1) @@ -16,149 +15,7 @@ * [Binary Search II](#binary-search-ii) * [Dynamic Programming I](#dynamic-programming-i) * [Programming Skills I](#programming-skills-i) - -### Programming Skills II - -#### Day 1 - -| | | | | | -|-|-|-|-|-|- -| 0896 |[Monotonic Array](src/main/kotlin/g0801_0900/s0896_monotonic_array)| Easy | Array | 576 | 90.91 -| 0028 |[Find the Index of the First Occurrence in a String](src/main/kotlin/g0001_0100/s0028_find_the_index_of_the_first_occurrence_in_a_string)| Easy | Top_Interview_Questions, String, Two_Pointers, String_Matching | 126 | 97.58 - -#### Day 2 - -| | | | | | -|-|-|-|-|-|- -| 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 - -| | | | | | -|-|-|-|-|-|- -| 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 | 148 | 98.75 - -#### Day 4 - -| | | | | | -|-|-|-|-|-|- -| 1367 |[Linked List in Binary Tree](src/main/kotlin/g1301_1400/s1367_linked_list_in_binary_tree)| Medium | Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Linked_List | 237 | 92.86 -| 0043 |[Multiply Strings](src/main/kotlin/g0001_0100/s0043_multiply_strings)| Medium | String, Math, Simulation | 165 | 96.72 - -#### Day 5 - -| | | | | | -|-|-|-|-|-|- -| 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 - -| | | | | | -|-|-|-|-|-|- -| 0739 |[Daily Temperatures](src/main/kotlin/g0701_0800/s0739_daily_temperatures)| Medium | Top_100_Liked_Questions, Array, Stack, Monotonic_Stack, Big_O_Time_O(n)_Space_O(n) | 936 | 80.54 -| 0058 |[Length of Last Word](src/main/kotlin/g0001_0100/s0058_length_of_last_word)| Easy | String | 135 | 93.67 - -#### Day 7 - -| | | | | | -|-|-|-|-|-|- -| 0048 |[Rotate Image](src/main/kotlin/g0001_0100/s0048_rotate_image)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Math, Matrix, Big_O_Time_O(n^2)_Space_O(1) | 160 | 90.11 -| 1886 |[Determine Whether Matrix Can Be Obtained By Rotation](src/main/kotlin/g1801_1900/s1886_determine_whether_matrix_can_be_obtained_by_rotation)| Easy | Array, Matrix | 147 | 85.71 - -#### Day 8 - -| | | | | | -|-|-|-|-|-|- -| 0054 |[Spiral Matrix](src/main/kotlin/g0001_0100/s0054_spiral_matrix)| Medium | Top_100_Liked_Questions, 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 - -| | | | | | -|-|-|-|-|-|- -| 1630 |[Arithmetic Subarrays](src/main/kotlin/g1601_1700/s1630_arithmetic_subarrays)| Medium | Array, Sorting | 264 | 100.00 -| 0429 |[N-ary Tree Level Order Traversal](src/main/kotlin/g0401_0500/s0429_n_ary_tree_level_order_traversal)| Medium | Breadth_First_Search, Tree | 248 | 75.86 - -#### Day 10 - -| | | | | | -|-|-|-|-|-|- -| 0503 |[Next Greater Element II](src/main/kotlin/g0501_0600/s0503_next_greater_element_ii)| Medium | Array, Stack, Monotonic_Stack | 331 | 92.68 -| 0556 |[Next Greater Element III](src/main/kotlin/g0501_0600/s0556_next_greater_element_iii)| Medium | String, Math, Two_Pointers | 137 | 80.00 - -#### Day 11 - -| | | | | | -|-|-|-|-|-|- -| 1376 |[Time Needed to Inform All Employees](src/main/kotlin/g1301_1400/s1376_time_needed_to_inform_all_employees)| Medium | Depth_First_Search, Breadth_First_Search, Tree | 915 | 37.62 -| 0049 |[Group Anagrams](src/main/kotlin/g0001_0100/s0049_group_anagrams)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, String, Hash_Table, Sorting, Big_O_Time_O(n\*k_log_k)_Space_O(n) | 308 | 96.34 - -#### Day 12 - -| | | | | | -|-|-|-|-|-|- -| 0438 |[Find All Anagrams in a String](src/main/kotlin/g0401_0500/s0438_find_all_anagrams_in_a_string)| Medium | Top_100_Liked_Questions, String, Hash_Table, Sliding_Window, Big_O_Time_O(n+m)_Space_O(1) | 561 | 54.68 -| 0713 |[Subarray Product Less Than K](src/main/kotlin/g0701_0800/s0713_subarray_product_less_than_k)| Medium | Array, Sliding_Window | 336 | 92.11 - -#### Day 13 - -| | | | | | -|-|-|-|-|-|- -| 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 -| 0910 |[Smallest Range II](src/main/kotlin/g0901_1000/s0910_smallest_range_ii)| Medium | Array, Math, Sorting, Greedy | 234 | 100.00 - -#### Day 14 - -| | | | | | -|-|-|-|-|-|- -| 0143 |[Reorder List](src/main/kotlin/g0101_0200/s0143_reorder_list)| Medium | Two_Pointers, Stack, Linked_List, Recursion | 395 | 82.26 -| 0138 |[Copy List with Random Pointer](src/main/kotlin/g0101_0200/s0138_copy_list_with_random_pointer)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Hash_Table, Linked_List, Big_O_Time_O(N)_Space_O(N) | 274 | 80.58 - -#### Day 15 - -| | | | | | -|-|-|-|-|-|- -| 0002 |[Add Two Numbers](src/main/kotlin/g0001_0100/s0002_add_two_numbers)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Math, Linked_List, Recursion, Big_O_Time_O(max(N,M))_Space_O(max(N,M)) | 203 | 96.13 -| 0445 |[Add Two Numbers II](src/main/kotlin/g0401_0500/s0445_add_two_numbers_ii)| Medium | Math, Stack, Linked_List | 240 | 82.61 - -#### Day 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 - -| | | | | | -|-|-|-|-|-|- -| 1845 |[Seat Reservation Manager](src/main/kotlin/g1801_1900/s1845_seat_reservation_manager)| Medium | Design, Heap_Priority_Queue | 834 | 100.00 -| 0860 |[Lemonade Change](src/main/kotlin/g0801_0900/s0860_lemonade_change)| Easy | Array, Greedy | 413 | 86.96 - -#### Day 18 - -| | | | | | -|-|-|-|-|-|- -| 0155 |[Min Stack](src/main/kotlin/g0101_0200/s0155_min_stack)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Stack, Design, Big_O_Time_O(1)_Space_O(N) | 331 | 84.88 -| 0341 |[Flatten Nested List Iterator](src/main/kotlin/g0301_0400/s0341_flatten_nested_list_iterator)| Medium | Depth_First_Search, Tree, Stack, Design, Queue, Iterator | 210 | 100.00 - -#### Day 19 - -| | | | | | -|-|-|-|-|-|- -| 1797 |[Design Authentication Manager](src/main/kotlin/g1701_1800/s1797_design_authentication_manager)| Medium | Hash_Table, Design | 334 | 100.00 -| 0707 |[Design Linked List](src/main/kotlin/g0701_0800/s0707_design_linked_list)| Medium | Design, Linked_List | 243 | 100.00 - -#### Day 20 - -| | | | | | -|-|-|-|-|-|- -| 0380 |[Insert Delete GetRandom O(1)](src/main/kotlin/g0301_0400/s0380_insert_delete_getrandom_o1)| Medium | Array, Hash_Table, Math, Design, Randomized | 1326 | 68.23 -| 0622 |[Design Circular Queue](src/main/kotlin/g0601_0700/s0622_design_circular_queue)| Medium | Array, Design, Linked_List, Queue | 234 | 92.68 -| 0729 |[My Calendar I](src/main/kotlin/g0701_0800/s0729_my_calendar_i)| Medium | Binary_Search, Design, Ordered_Set, Segment_Tree | 378 | 69.70 +* [Programming Skills II](#programming-skills-ii) ### Graph Theory I @@ -180,7 +37,7 @@ | | | | | | |-|-|-|-|-|- -| 1020 |[Number of Enclaves](src/main/kotlin/g1001_1100/s1020_number_of_enclaves)| Medium | Array, Depth_First_Search, Breadth_First_Search, Matrix, Union_Find | 369 | 76.26 +| 1020 |[Number of Enclaves](src/main/kotlin/g1001_1100/s1020_number_of_enclaves)| Medium | Array, Depth_First_Search, Breadth_First_Search, Matrix, Union_Find | 283 | 90.70 | 1905 |[Count Sub Islands](src/main/kotlin/g1901_2000/s1905_count_sub_islands)| Medium | Array, Depth_First_Search, Breadth_First_Search, Matrix, Union_Find | 866 | 100.00 #### Day 4 Matrix Related Problems @@ -1812,10 +1669,161 @@ | 1603 |[Design Parking System](src/main/kotlin/g1601_1700/s1603_design_parking_system)| Easy | Design, Simulation, Counting | 376 | 31.83 | 0303 |[Range Sum Query - Immutable](src/main/kotlin/g0301_0400/s0303_range_sum_query_immutable)| Easy | Array, Design, Prefix_Sum | 472 | 63.64 +### Programming Skills II + +#### Day 1 + +| | | | | | +|-|-|-|-|-|- +| 0896 |[Monotonic Array](src/main/kotlin/g0801_0900/s0896_monotonic_array)| Easy | Array | 576 | 90.91 +| 0028 |[Find the Index of the First Occurrence in a String](src/main/kotlin/g0001_0100/s0028_find_the_index_of_the_first_occurrence_in_a_string)| Easy | Top_Interview_Questions, String, Two_Pointers, String_Matching | 126 | 97.58 + +#### Day 2 + +| | | | | | +|-|-|-|-|-|- +| 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 + +| | | | | | +|-|-|-|-|-|- +| 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 | 148 | 98.75 + +#### Day 4 + +| | | | | | +|-|-|-|-|-|- +| 1367 |[Linked List in Binary Tree](src/main/kotlin/g1301_1400/s1367_linked_list_in_binary_tree)| Medium | Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Linked_List | 237 | 92.86 +| 0043 |[Multiply Strings](src/main/kotlin/g0001_0100/s0043_multiply_strings)| Medium | String, Math, Simulation | 165 | 96.72 + +#### Day 5 + +| | | | | | +|-|-|-|-|-|- +| 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 + +| | | | | | +|-|-|-|-|-|- +| 0739 |[Daily Temperatures](src/main/kotlin/g0701_0800/s0739_daily_temperatures)| Medium | Top_100_Liked_Questions, Array, Stack, Monotonic_Stack, Big_O_Time_O(n)_Space_O(n) | 936 | 80.54 +| 0058 |[Length of Last Word](src/main/kotlin/g0001_0100/s0058_length_of_last_word)| Easy | String | 135 | 93.67 + +#### Day 7 + +| | | | | | +|-|-|-|-|-|- +| 0048 |[Rotate Image](src/main/kotlin/g0001_0100/s0048_rotate_image)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Math, Matrix, Big_O_Time_O(n^2)_Space_O(1) | 160 | 90.11 +| 1886 |[Determine Whether Matrix Can Be Obtained By Rotation](src/main/kotlin/g1801_1900/s1886_determine_whether_matrix_can_be_obtained_by_rotation)| Easy | Array, Matrix | 147 | 85.71 + +#### Day 8 + +| | | | | | +|-|-|-|-|-|- +| 0054 |[Spiral Matrix](src/main/kotlin/g0001_0100/s0054_spiral_matrix)| Medium | Top_100_Liked_Questions, 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 + +| | | | | | +|-|-|-|-|-|- +| 1630 |[Arithmetic Subarrays](src/main/kotlin/g1601_1700/s1630_arithmetic_subarrays)| Medium | Array, Sorting | 264 | 100.00 +| 0429 |[N-ary Tree Level Order Traversal](src/main/kotlin/g0401_0500/s0429_n_ary_tree_level_order_traversal)| Medium | Breadth_First_Search, Tree | 248 | 75.86 + +#### Day 10 + +| | | | | | +|-|-|-|-|-|- +| 0503 |[Next Greater Element II](src/main/kotlin/g0501_0600/s0503_next_greater_element_ii)| Medium | Array, Stack, Monotonic_Stack | 331 | 92.68 +| 0556 |[Next Greater Element III](src/main/kotlin/g0501_0600/s0556_next_greater_element_iii)| Medium | String, Math, Two_Pointers | 137 | 80.00 + +#### Day 11 + +| | | | | | +|-|-|-|-|-|- +| 1376 |[Time Needed to Inform All Employees](src/main/kotlin/g1301_1400/s1376_time_needed_to_inform_all_employees)| Medium | Depth_First_Search, Breadth_First_Search, Tree | 915 | 37.62 +| 0049 |[Group Anagrams](src/main/kotlin/g0001_0100/s0049_group_anagrams)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, String, Hash_Table, Sorting, Big_O_Time_O(n\*k_log_k)_Space_O(n) | 308 | 96.34 + +#### Day 12 + +| | | | | | +|-|-|-|-|-|- +| 0438 |[Find All Anagrams in a String](src/main/kotlin/g0401_0500/s0438_find_all_anagrams_in_a_string)| Medium | Top_100_Liked_Questions, String, Hash_Table, Sliding_Window, Big_O_Time_O(n+m)_Space_O(1) | 561 | 54.68 +| 0713 |[Subarray Product Less Than K](src/main/kotlin/g0701_0800/s0713_subarray_product_less_than_k)| Medium | Array, Sliding_Window | 336 | 92.11 + +#### Day 13 + +| | | | | | +|-|-|-|-|-|- +| 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 +| 0910 |[Smallest Range II](src/main/kotlin/g0901_1000/s0910_smallest_range_ii)| Medium | Array, Math, Sorting, Greedy | 234 | 100.00 + +#### Day 14 + +| | | | | | +|-|-|-|-|-|- +| 0143 |[Reorder List](src/main/kotlin/g0101_0200/s0143_reorder_list)| Medium | Two_Pointers, Stack, Linked_List, Recursion | 395 | 82.26 +| 0138 |[Copy List with Random Pointer](src/main/kotlin/g0101_0200/s0138_copy_list_with_random_pointer)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Hash_Table, Linked_List, Big_O_Time_O(N)_Space_O(N) | 274 | 80.58 + +#### Day 15 + +| | | | | | +|-|-|-|-|-|- +| 0002 |[Add Two Numbers](src/main/kotlin/g0001_0100/s0002_add_two_numbers)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Math, Linked_List, Recursion, Big_O_Time_O(max(N,M))_Space_O(max(N,M)) | 203 | 96.13 +| 0445 |[Add Two Numbers II](src/main/kotlin/g0401_0500/s0445_add_two_numbers_ii)| Medium | Math, Stack, Linked_List | 240 | 82.61 + +#### Day 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 + +| | | | | | +|-|-|-|-|-|- +| 1845 |[Seat Reservation Manager](src/main/kotlin/g1801_1900/s1845_seat_reservation_manager)| Medium | Design, Heap_Priority_Queue | 834 | 100.00 +| 0860 |[Lemonade Change](src/main/kotlin/g0801_0900/s0860_lemonade_change)| Easy | Array, Greedy | 413 | 86.96 + +#### Day 18 + +| | | | | | +|-|-|-|-|-|- +| 0155 |[Min Stack](src/main/kotlin/g0101_0200/s0155_min_stack)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Stack, Design, Big_O_Time_O(1)_Space_O(N) | 331 | 84.88 +| 0341 |[Flatten Nested List Iterator](src/main/kotlin/g0301_0400/s0341_flatten_nested_list_iterator)| Medium | Depth_First_Search, Tree, Stack, Design, Queue, Iterator | 210 | 100.00 + +#### Day 19 + +| | | | | | +|-|-|-|-|-|- +| 1797 |[Design Authentication Manager](src/main/kotlin/g1701_1800/s1797_design_authentication_manager)| Medium | Hash_Table, Design | 334 | 100.00 +| 0707 |[Design Linked List](src/main/kotlin/g0701_0800/s0707_design_linked_list)| Medium | Design, Linked_List | 243 | 100.00 + +#### Day 20 + +| | | | | | +|-|-|-|-|-|- +| 0380 |[Insert Delete GetRandom O(1)](src/main/kotlin/g0301_0400/s0380_insert_delete_getrandom_o1)| Medium | Array, Hash_Table, Math, Design, Randomized | 1326 | 68.23 +| 0622 |[Design Circular Queue](src/main/kotlin/g0601_0700/s0622_design_circular_queue)| Medium | Array, Design, Linked_List, Queue | 234 | 92.68 +| 0729 |[My Calendar I](src/main/kotlin/g0701_0800/s0729_my_calendar_i)| Medium | Binary_Search, Design, Ordered_Set, Segment_Tree | 378 | 69.70 + ## Algorithms | # | Title | Difficulty | Tag | Time, ms | Time, % |------|----------------|-------------|-------------|----------|-------- +| 3134 |[Find the Median of the Uniqueness Array](src/main/kotlin/g3101_3200/s3134_find_the_median_of_the_uniqueness_array)| Hard | Array, Hash_Table, Binary_Search, Sliding_Window | 733 | 100.00 +| 3133 |[Minimum Array End](src/main/kotlin/g3101_3200/s3133_minimum_array_end)| Medium | Bit_Manipulation | 131 | 90.91 +| 3132 |[Find the Integer Added to Array II](src/main/kotlin/g3101_3200/s3132_find_the_integer_added_to_array_ii)| Medium | Array, Sorting, Two_Pointers, Enumeration | 189 | 84.85 +| 3131 |[Find the Integer Added to Array I](src/main/kotlin/g3101_3200/s3131_find_the_integer_added_to_array_i)| Easy | Array | 162 | 98.15 +| 3130 |[Find All Possible Stable Binary Arrays II](src/main/kotlin/g3101_3200/s3130_find_all_possible_stable_binary_arrays_ii)| Hard | Dynamic_Programming, Prefix_Sum | 242 | 100.00 +| 3129 |[Find All Possible Stable Binary Arrays I](src/main/kotlin/g3101_3200/s3129_find_all_possible_stable_binary_arrays_i)| Medium | Dynamic_Programming, Prefix_Sum | 169 | 92.86 +| 3128 |[Right Triangles](src/main/kotlin/g3101_3200/s3128_right_triangles)| Medium | Array, Hash_Table, Math, Counting, Combinatorics | 975 | 40.63 +| 3127 |[Make a Square with the Same Color](src/main/kotlin/g3101_3200/s3127_make_a_square_with_the_same_color)| Easy | Array, Matrix, Enumeration | 149 | 80.00 | 3123 |[Find Edges in Shortest Paths](src/main/kotlin/g3101_3200/s3123_find_edges_in_shortest_paths)| Hard | Depth_First_Search, Breadth_First_Search, Heap_Priority_Queue, Graph, Shortest_Path | 931 | 100.00 | 3122 |[Minimum Number of Operations to Satisfy Conditions](src/main/kotlin/g3101_3200/s3122_minimum_number_of_operations_to_satisfy_conditions)| Medium | Array, Dynamic_Programming, Matrix | 948 | 80.77 | 3121 |[Count the Number of Special Characters II](src/main/kotlin/g3101_3200/s3121_count_the_number_of_special_characters_ii)| Medium | String, Hash_Table | 274 | 90.74 @@ -3441,7 +3449,7 @@ | 1023 |[Camelcase Matching](src/main/kotlin/g1001_1100/s1023_camelcase_matching)| Medium | String, Two_Pointers, Trie, String_Matching | 149 | 60.00 | 1022 |[Sum of Root To Leaf Binary Numbers](src/main/kotlin/g1001_1100/s1022_sum_of_root_to_leaf_binary_numbers)| Easy | Depth_First_Search, Tree, Binary_Tree | 158 | 88.89 | 1021 |[Remove Outermost Parentheses](src/main/kotlin/g1001_1100/s1021_remove_outermost_parentheses)| Easy | String, Stack | 156 | 60.00 -| 1020 |[Number of Enclaves](src/main/kotlin/g1001_1100/s1020_number_of_enclaves)| Medium | Array, Depth_First_Search, Breadth_First_Search, Matrix, Union_Find, Graph_Theory_I_Day_3_Matrix_Related_Problems | 369 | 76.26 +| 1020 |[Number of Enclaves](src/main/kotlin/g1001_1100/s1020_number_of_enclaves)| Medium | Array, Depth_First_Search, Breadth_First_Search, Matrix, Union_Find, Graph_Theory_I_Day_3_Matrix_Related_Problems | 283 | 90.70 | 1019 |[Next Greater Node In Linked List](src/main/kotlin/g1001_1100/s1019_next_greater_node_in_linked_list)| Medium | Array, Stack, Linked_List, Monotonic_Stack | 472 | 75.00 | 1018 |[Binary Prefix Divisible By 5](src/main/kotlin/g1001_1100/s1018_binary_prefix_divisible_by_5)| Easy | Array | 297 | 100.00 | 1017 |[Convert to Base -2](src/main/kotlin/g1001_1100/s1017_convert_to_base_2)| Medium | Math | 138 | 100.00 diff --git a/src/main/kotlin/g0001_0100/s0089_gray_code/readme.md b/src/main/kotlin/g0001_0100/s0089_gray_code/readme.md index 09b82c9d..8fb8e3a7 100644 --- a/src/main/kotlin/g0001_0100/s0089_gray_code/readme.md +++ b/src/main/kotlin/g0001_0100/s0089_gray_code/readme.md @@ -38,18 +38,18 @@ Given an integer `n`, return _any valid **n-bit gray code sequence**_. ```kotlin @Suppress("NAME_SHADOWING") class Solution { - fun grayCode(n: Int): List { + fun grayCode(n: Int): List { var n = n - var n1 = arrayOf(0) + var n1 = arrayOf(0) var shift = 1 while (n > 0) { - val temp = arrayOfNulls(n1.size * 2) + val temp = Array(n1.size * 2) { 0 } var pos = 0 for (integer in n1) { temp[pos++] = integer } for (i in n1.indices.reversed()) { - temp[pos++] = n1[i]!! or shift + temp[pos++] = n1[i] or shift } n1 = temp shift = shift shl 1 diff --git a/src/main/kotlin/g0101_0200/s0150_evaluate_reverse_polish_notation/readme.md b/src/main/kotlin/g0101_0200/s0150_evaluate_reverse_polish_notation/readme.md index 575e31f3..efc1b883 100644 --- a/src/main/kotlin/g0101_0200/s0150_evaluate_reverse_polish_notation/readme.md +++ b/src/main/kotlin/g0101_0200/s0150_evaluate_reverse_polish_notation/readme.md @@ -57,14 +57,12 @@ It is guaranteed that the given RPN expression is always valid. That means the e ## Solution ```kotlin -import java.util.function.BiFunction - class Solution { - val op = mapOf>( - "/" to BiFunction { a, b -> a / b }, - "*" to BiFunction { a, b -> a * b }, - "+" to BiFunction { a, b -> a + b }, - "-" to BiFunction { a, b -> a - b } + val op = mapOf Int>( + "/" to { a, b -> a / b }, + "*" to { a, b -> a * b }, + "+" to { a, b -> a + b }, + "-" to { a, b -> a - b } ) fun evalRPN(tokens: Array): Int { val stack = ArrayDeque() @@ -72,7 +70,7 @@ class Solution { if (op.contains(t)) { val b = stack.removeFirst().toInt() val a = stack.removeFirst().toInt() - val c = op.getValue(t).apply(a, b) + val c = op.getValue(t).invoke(a, b) stack.addFirst(c.toString()) } else { stack.addFirst(t) diff --git a/src/main/kotlin/g1001_1100/s1020_number_of_enclaves/readme.md b/src/main/kotlin/g1001_1100/s1020_number_of_enclaves/readme.md index bdc18953..0fd87224 100644 --- a/src/main/kotlin/g1001_1100/s1020_number_of_enclaves/readme.md +++ b/src/main/kotlin/g1001_1100/s1020_number_of_enclaves/readme.md @@ -42,37 +42,44 @@ Return _the number of land cells in_ `grid` _for which we cannot walk off the bo ```kotlin class Solution { - fun numEnclaves(grid: Array): Int { - val visited = Array(grid.size) { - BooleanArray( - grid[0].size - ) + private fun walk(a: Array, visited: Array, x: Int, y: Int) { + if (x >= a.size || x < 0 || y >= a[0].size || y < 0) { + return } - for (i in grid.indices) { - for (j in grid[0].indices) { - if (grid[i][j] == 1 && (i == 0 || j == 0 || i == grid.size - 1 || j == grid[0].size - 1)) { - move(grid, i, j, visited) - } - } + if (visited[x][y]) { + return } - var count = 0 - for (i in 1 until visited.size - 1) { - for (j in 1 until visited[0].size - 1) { - if (!visited[i][j] && grid[i][j] == 1) count++ - } + if (a[x][y] == 0) { + return } - return count + visited[x][y] = true + walk(a, visited, x - 1, y) + walk(a, visited, x, y - 1) + walk(a, visited, x, y + 1) + walk(a, visited, x + 1, y) } - companion object { - fun move(g: Array, i: Int, j: Int, b: Array) { - if (i < 0 || j < 0 || i == g.size || j == g[0].size || g[i][j] == 0 || b[i][j]) return - b[i][j] = true - move(g, i + 1, j, b) - move(g, i - 1, j, b) - move(g, i, j - 1, b) - move(g, i, j + 1, b) + fun numEnclaves(a: Array): Int { + val n = a.size + val m = a[0].size + val visited = Array(n) { BooleanArray(m) } + for (i in 0 until n) { + walk(a, visited, i, 0) + walk(a, visited, i, m - 1) + } + for (j in 0 until m) { + walk(a, visited, 0, j) + walk(a, visited, n - 1, j) + } + var unreachables = 0 + for (i in 0 until n) { + for (j in 0 until m) { + if (a[i][j] == 1 && !visited[i][j]) { + ++unreachables + } + } } + return unreachables } } ``` \ No newline at end of file diff --git a/src/main/kotlin/g1301_1400/s1396_design_underground_system/readme.md b/src/main/kotlin/g1301_1400/s1396_design_underground_system/readme.md index 1c875f01..b9d20e1e 100644 --- a/src/main/kotlin/g1301_1400/s1396_design_underground_system/readme.md +++ b/src/main/kotlin/g1301_1400/s1396_design_underground_system/readme.md @@ -74,13 +74,11 @@ You may assume all calls to the `checkIn` and `checkOut` methods are consistent. ## Solution ```kotlin -import java.util.LinkedList - class UndergroundSystem { private class StationAndTime(var station: String, var time: Int) private val averageTimeMap: MutableMap - private val travelerMap: MutableMap> + private val travelerMap: MutableMap> init { averageTimeMap = HashMap() @@ -88,13 +86,13 @@ class UndergroundSystem { } fun checkIn(id: Int, stationName: String, t: Int) { - travelerMap.putIfAbsent(id, LinkedList()) + travelerMap.putIfAbsent(id, ArrayList()) travelerMap[id]!!.add(StationAndTime(stationName, t)) } fun checkOut(id: Int, stationName: String, t: Int) { val list = travelerMap[id]!! - val stationAndTime = list.last + val stationAndTime = list.last() val startToEndStation: String = stationAndTime.station + "->" + stationName val duration: Int = t - stationAndTime.time if (averageTimeMap.containsKey(startToEndStation)) { diff --git a/src/main/kotlin/g1401_1500/s1425_constrained_subsequence_sum/readme.md b/src/main/kotlin/g1401_1500/s1425_constrained_subsequence_sum/readme.md index 31f4003b..b2d7b90b 100644 --- a/src/main/kotlin/g1401_1500/s1425_constrained_subsequence_sum/readme.md +++ b/src/main/kotlin/g1401_1500/s1425_constrained_subsequence_sum/readme.md @@ -41,23 +41,21 @@ A _subsequence_ of an array is obtained by deleting some number of elements (can ## Solution ```kotlin -import java.util.LinkedList - class Solution { fun constrainedSubsetSum(nums: IntArray, k: Int): Int { val n = nums.size var res = Int.MIN_VALUE - val mono = LinkedList() + val mono = ArrayList() for (i in 0 until n) { var take = nums[i] - while (mono.isNotEmpty() && i - mono.first[0] > k) { + while (mono.isNotEmpty() && i - mono.first()[0] > k) { mono.removeFirst() } if (mono.isNotEmpty()) { - val mx = Math.max(0, mono.first[1]) + val mx = Math.max(0, mono.first()[1]) take += mx } - while (mono.isNotEmpty() && take > mono.last[1]) { + while (mono.isNotEmpty() && take > mono.last()[1]) { mono.removeLast() } mono.add(intArrayOf(i, take)) diff --git a/src/main/kotlin/g1501_1600/s1594_maximum_non_negative_product_in_a_matrix/readme.md b/src/main/kotlin/g1501_1600/s1594_maximum_non_negative_product_in_a_matrix/readme.md index eb99e7e1..3af925a8 100644 --- a/src/main/kotlin/g1501_1600/s1594_maximum_non_negative_product_in_a_matrix/readme.md +++ b/src/main/kotlin/g1501_1600/s1594_maximum_non_negative_product_in_a_matrix/readme.md @@ -56,44 +56,39 @@ Notice that the modulo is performed after getting the maximum product. class Solution { private class Tuple(var max: Long, var min: Long) - fun maxProductPath(grid: Array?): Int { + fun maxProductPath(grid: Array): Int { // DP - if (grid == null || grid.size == 0 || grid[0] == null || grid[0]!!.size == 0) { + if (grid.isEmpty() || grid[0].isEmpty()) { return 0 } val rows = grid.size - val cols = grid[0]!!.size - val dp = Array(rows) { arrayOfNulls(cols) } - for (i in 0 until rows) { - for (j in 0 until cols) { - dp[i][j] = Tuple(1, 1) - } - } + val cols = grid[0].size + val dp = Array(rows) { Array(cols) { Tuple(1, 1) } } // Init first row and column - dp[0][0]!!.max = grid[0]!![0].toLong() - dp[0][0]!!.min = grid[0]!![0].toLong() + dp[0][0].max = grid[0][0].toLong() + dp[0][0].min = grid[0][0].toLong() for (i in 1 until rows) { - dp[i][0]!!.max = grid[i]!![0] * dp[i - 1][0]!!.max - dp[i][0]!!.min = grid[i]!![0] * dp[i - 1][0]!!.min + dp[i][0].max = grid[i][0] * dp[i - 1][0].max + dp[i][0].min = grid[i][0] * dp[i - 1][0].min } for (i in 1 until cols) { - dp[0][i]!!.max = grid[0]!![i] * dp[0][i - 1]!!.max - dp[0][i]!!.min = grid[0]!![i] * dp[0][i - 1]!!.min + dp[0][i].max = grid[0][i] * dp[0][i - 1].max + dp[0][i].min = grid[0][i] * dp[0][i - 1].min } // DP for (i in 1 until rows) { for (j in 1 until cols) { - val up1 = dp[i - 1][j]!!.max * grid[i]!![j] - val up2 = dp[i - 1][j]!!.min * grid[i]!![j] - val left1 = dp[i][j - 1]!!.max * grid[i]!![j] - val left2 = dp[i][j - 1]!!.min * grid[i]!![j] - dp[i][j]!!.max = Math.max(up1, Math.max(up2, Math.max(left1, left2))) - dp[i][j]!!.min = Math.min(up1, Math.min(up2, Math.min(left1, left2))) + val up1 = dp[i - 1][j].max * grid[i][j] + val up2 = dp[i - 1][j].min * grid[i][j] + val left1 = dp[i][j - 1].max * grid[i][j] + val left2 = dp[i][j - 1].min * grid[i][j] + dp[i][j].max = Math.max(up1, Math.max(up2, Math.max(left1, left2))) + dp[i][j].min = Math.min(up1, Math.min(up2, Math.min(left1, left2))) } } - return if (dp[rows - 1][cols - 1]!!.max < 0) { + return if (dp[rows - 1][cols - 1].max < 0) { -1 - } else (dp[rows - 1][cols - 1]!!.max % (1e9 + 7)).toInt() + } else (dp[rows - 1][cols - 1].max % (1e9 + 7)).toInt() } } ``` \ No newline at end of file diff --git a/src/main/kotlin/g1801_1900/s1886_determine_whether_matrix_can_be_obtained_by_rotation/readme.md b/src/main/kotlin/g1801_1900/s1886_determine_whether_matrix_can_be_obtained_by_rotation/readme.md index c3ae44e8..b6acd64a 100644 --- a/src/main/kotlin/g1801_1900/s1886_determine_whether_matrix_can_be_obtained_by_rotation/readme.md +++ b/src/main/kotlin/g1801_1900/s1886_determine_whether_matrix_can_be_obtained_by_rotation/readme.md @@ -47,12 +47,10 @@ Given two `n x n` binary matrices `mat` and `target`, return `true` _if it is po ## Solution ```kotlin -import java.util.Arrays - class Solution { - fun findRotation(mat: Array, target: Array?): Boolean { + fun findRotation(mat: Array, target: Array): Boolean { for (i in 0..3) { - if (Arrays.deepEquals(mat, target)) { + if (mat.contentDeepEquals(target)) { return true } rotate(mat) diff --git a/src/main/kotlin/g1901_2000/s1942_the_number_of_the_smallest_unoccupied_chair/readme.md b/src/main/kotlin/g1901_2000/s1942_the_number_of_the_smallest_unoccupied_chair/readme.md index 5c8771ef..fd72ab1d 100644 --- a/src/main/kotlin/g1901_2000/s1942_the_number_of_the_smallest_unoccupied_chair/readme.md +++ b/src/main/kotlin/g1901_2000/s1942_the_number_of_the_smallest_unoccupied_chair/readme.md @@ -69,7 +69,6 @@ Since friend 0 sat on chair 2, we return 2. ## Solution ```kotlin -import java.util.Arrays import java.util.PriorityQueue class Solution { @@ -81,9 +80,8 @@ class Solution { all[2 * i] = Person(i, times[i][0], false, true) all[2 * i + 1] = Person(i, times[i][1], true, false) } - Arrays.sort( - all - ) { a: Person?, b: Person? -> + + all.sortWith { a: Person?, b: Person? -> val i = if (a!!.leave) -1 else 1 val j = if (b!!.leave) -1 else 1 if (a.time == b.time) i - j else a.time - b.time diff --git a/src/main/kotlin/g1901_2000/s1985_find_the_kth_largest_integer_in_the_array/readme.md b/src/main/kotlin/g1901_2000/s1985_find_the_kth_largest_integer_in_the_array/readme.md index e8a97fc8..7753c24c 100644 --- a/src/main/kotlin/g1901_2000/s1985_find_the_kth_largest_integer_in_the_array/readme.md +++ b/src/main/kotlin/g1901_2000/s1985_find_the_kth_largest_integer_in_the_array/readme.md @@ -57,11 +57,9 @@ The 2nd largest integer in nums is "0". ## Solution ```kotlin -import java.util.Arrays - class Solution { fun kthLargestNumber(nums: Array, k: Int): String { - Arrays.sort(nums) { n1: String, n2: String -> compareStringInt(n2, n1) } + nums.sortWith { n1: String, n2: String -> compareStringInt(n2, n1) } return nums[k - 1] } @@ -69,7 +67,7 @@ class Solution { if (n1.length != n2.length) { return if (n1.length < n2.length) -1 else 1 } - for (i in 0 until n1.length) { + for (i in n1.indices) { val n1Digit = n1[i].code - '0'.code val n2Digit = n2[i].code - '0'.code if (n1Digit > n2Digit) { diff --git a/src/main/kotlin/g1901_2000/s1996_the_number_of_weak_characters_in_the_game/readme.md b/src/main/kotlin/g1901_2000/s1996_the_number_of_weak_characters_in_the_game/readme.md index 891ad6ee..41a55cc5 100644 --- a/src/main/kotlin/g1901_2000/s1996_the_number_of_weak_characters_in_the_game/readme.md +++ b/src/main/kotlin/g1901_2000/s1996_the_number_of_weak_characters_in_the_game/readme.md @@ -44,11 +44,9 @@ Return _the number of **weak** characters_. ## Solution ```kotlin -import java.util.Arrays - class Solution { fun numberOfWeakCharacters(properties: Array): Int { - Arrays.sort(properties) { a: IntArray, b: IntArray -> if (a[0] == b[0]) b[1] - a[1] else a[0] - b[0] } + properties.sortWith { a: IntArray, b: IntArray -> if (a[0] == b[0]) b[1] - a[1] else a[0] - b[0] } var max = properties[properties.size - 1][1] var count = 0 for (i in properties.size - 2 downTo 0) { diff --git a/src/main/kotlin/g2001_2100/s2008_maximum_earnings_from_taxi/readme.md b/src/main/kotlin/g2001_2100/s2008_maximum_earnings_from_taxi/readme.md index 7965237e..7a4d75b5 100644 --- a/src/main/kotlin/g2001_2100/s2008_maximum_earnings_from_taxi/readme.md +++ b/src/main/kotlin/g2001_2100/s2008_maximum_earnings_from_taxi/readme.md @@ -50,14 +50,13 @@ We earn 9 + 5 + 6 = 20 dollars in total. ## Solution ```kotlin -import java.util.Arrays import java.util.PriorityQueue @Suppress("UNUSED_PARAMETER") class Solution { fun maxTaxiEarnings(n: Int, rides: Array): Long { // Sort based on start time - Arrays.sort(rides) { a: IntArray, b: IntArray -> + rides.sortWith { a: IntArray, b: IntArray -> a[0] - b[0] } var max: Long = 0 diff --git a/src/main/kotlin/g2001_2100/s2054_two_best_non_overlapping_events/readme.md b/src/main/kotlin/g2001_2100/s2054_two_best_non_overlapping_events/readme.md index 76c3e631..b1637c21 100644 --- a/src/main/kotlin/g2001_2100/s2054_two_best_non_overlapping_events/readme.md +++ b/src/main/kotlin/g2001_2100/s2054_two_best_non_overlapping_events/readme.md @@ -51,11 +51,9 @@ Note that the start time and end time is **inclusive**: that is, you cannot atte ## Solution ```kotlin -import java.util.Arrays - class Solution { fun maxTwoEvents(events: Array): Int { - Arrays.sort(events) { a: IntArray, b: IntArray -> a[0] - b[0] } + events.sortWith { a: IntArray, b: IntArray -> a[0] - b[0] } val max = IntArray(events.size) for (i in events.indices.reversed()) { if (i == events.size - 1) { diff --git a/src/main/kotlin/g2001_2100/s2092_find_all_people_with_secret/readme.md b/src/main/kotlin/g2001_2100/s2092_find_all_people_with_secret/readme.md index 1363ca10..7bf3a107 100644 --- a/src/main/kotlin/g2001_2100/s2092_find_all_people_with_secret/readme.md +++ b/src/main/kotlin/g2001_2100/s2092_find_all_people_with_secret/readme.md @@ -78,12 +78,10 @@ Thus, people 0, 1, 2, 3, and 4 know the secret after all the meetings. ## Solution ```kotlin -import java.util.Arrays - @Suppress("NAME_SHADOWING") class Solution { fun findAllPeople(n: Int, meetings: Array, firstPerson: Int): List { - Arrays.sort(meetings) { a: IntArray, b: IntArray -> a[2] - b[2] } + meetings.sortWith { a: IntArray, b: IntArray -> a[2] - b[2] } val uf = UF(n) // base uf.union(0, firstPerson) diff --git a/src/main/kotlin/g2101_2200/s2136_earliest_possible_day_of_full_bloom/readme.md b/src/main/kotlin/g2101_2200/s2136_earliest_possible_day_of_full_bloom/readme.md index c2e5ced2..ae77f1e3 100644 --- a/src/main/kotlin/g2101_2200/s2136_earliest_possible_day_of_full_bloom/readme.md +++ b/src/main/kotlin/g2101_2200/s2136_earliest_possible_day_of_full_bloom/readme.md @@ -75,7 +75,6 @@ Thus, on day 2, all the seeds are blooming. ## Solution ```kotlin -import java.util.Arrays import java.util.Collections class Solution { @@ -88,7 +87,7 @@ class Solution { for (i in 0 until n) { arr[i] = Seed(plantTime[i], growTime[i]) } - Arrays.sort(arr, Collections.reverseOrder()) + arr.sortWith(Collections.reverseOrder()) var ans = arr[0]!!.plantTime + arr[0]!!.growTime var lastPlantDay = arr[0]!!.plantTime for (i in 1 until n) { diff --git a/src/main/kotlin/g2201_2300/s2250_count_number_of_rectangles_containing_each_point/readme.md b/src/main/kotlin/g2201_2300/s2250_count_number_of_rectangles_containing_each_point/readme.md index 6f44d5a6..0296c41f 100644 --- a/src/main/kotlin/g2201_2300/s2250_count_number_of_rectangles_containing_each_point/readme.md +++ b/src/main/kotlin/g2201_2300/s2250_count_number_of_rectangles_containing_each_point/readme.md @@ -69,8 +69,6 @@ Therefore, we return [1, 3]. ## Solution ```kotlin -import java.util.Arrays - class Solution { fun countRectangles(rectangles: Array, points: Array): IntArray { val n = rectangles.size @@ -80,7 +78,7 @@ class Solution { for (i in 0 until q) { es[n + i] = intArrayOf(points[i][0], points[i][1], i) } - Arrays.sort(es) { x: IntArray?, y: IntArray? -> if (x!![0] != y!![0]) -(x[0] - y[0]) else x.size - y.size } + es.sortWith { x: IntArray?, y: IntArray? -> if (x!![0] != y!![0]) -(x[0] - y[0]) else x.size - y.size } val ct = IntArray(101) val ans = IntArray(q) for (e in es) { diff --git a/src/main/kotlin/g2201_2300/s2251_number_of_flowers_in_full_bloom/readme.md b/src/main/kotlin/g2201_2300/s2251_number_of_flowers_in_full_bloom/readme.md index ccfb1ed6..31380fed 100644 --- a/src/main/kotlin/g2201_2300/s2251_number_of_flowers_in_full_bloom/readme.md +++ b/src/main/kotlin/g2201_2300/s2251_number_of_flowers_in_full_bloom/readme.md @@ -40,21 +40,20 @@ Return _an integer array_ `answer` _of size_ `n`_, where_ `answer[i]` _is the ** ## Solution ```kotlin -import java.util.Arrays import java.util.PriorityQueue class Solution { fun fullBloomFlowers(flowers: Array, persons: IntArray): IntArray { - Arrays.sort(flowers, { a: IntArray, b: IntArray -> a[0].compareTo(b[0]) }) + flowers.sortWith { a: IntArray, b: IntArray -> a[0].compareTo(b[0]) } val ans = IntArray(persons.size) - val pq = PriorityQueue({ a: Pair, b: Pair -> a.j.compareTo(b.j) }) + val pq = PriorityQueue { a: Pair, b: Pair -> a.j.compareTo(b.j) } var j = 0 val t = Array(persons.size) { IntArray(2) } for (i in persons.indices) { t[i][0] = persons[i] t[i][1] = i } - Arrays.sort(t, { a: IntArray, b: IntArray -> a[0].compareTo(b[0]) }) + t.sortWith { a: IntArray, b: IntArray -> a[0].compareTo(b[0]) } for (ints in t) { while (pq.isNotEmpty()) { if (pq.peek().j < ints[0]) { diff --git a/src/main/kotlin/g2201_2300/s2271_maximum_white_tiles_covered_by_a_carpet/readme.md b/src/main/kotlin/g2201_2300/s2271_maximum_white_tiles_covered_by_a_carpet/readme.md index 2fc022b8..c4d7d570 100644 --- a/src/main/kotlin/g2201_2300/s2271_maximum_white_tiles_covered_by_a_carpet/readme.md +++ b/src/main/kotlin/g2201_2300/s2271_maximum_white_tiles_covered_by_a_carpet/readme.md @@ -50,11 +50,9 @@ It covers 2 white tiles, so we return 2. ## Solution ```kotlin -import java.util.Arrays - class Solution { fun maximumWhiteTiles(tiles: Array, carpetLength: Int): Int { - Arrays.sort(tiles, { x: IntArray, y: IntArray -> x[0].compareTo(y[0]) }) + tiles.sortWith { x: IntArray, y: IntArray -> x[0].compareTo(y[0]) } var currentCover = Math.min(tiles[0][1] - tiles[0][0] + 1, carpetLength) var maxCover = currentCover var head = 1 diff --git a/src/main/kotlin/g2201_2300/s2280_minimum_lines_to_represent_a_line_chart/readme.md b/src/main/kotlin/g2201_2300/s2280_minimum_lines_to_represent_a_line_chart/readme.md index b99e14d8..c2f55456 100644 --- a/src/main/kotlin/g2201_2300/s2280_minimum_lines_to_represent_a_line_chart/readme.md +++ b/src/main/kotlin/g2201_2300/s2280_minimum_lines_to_represent_a_line_chart/readme.md @@ -53,14 +53,12 @@ It can be shown that it is not possible to represent the line chart using less t ## Solution ```kotlin -import java.util.Arrays - class Solution { fun minimumLines(stockPrices: Array): Int { if (stockPrices.size == 1) { return 0 } - Arrays.sort(stockPrices) { a: IntArray, b: IntArray -> a[0] - b[0] } + stockPrices.sortWith { a: IntArray, b: IntArray -> a[0] - b[0] } // multiply with 1.0 to make it double and multiply with 100 for making it big so that // difference won't come out to be very less and after division it become 0. // failing for one of the case without multiply 100 diff --git a/src/main/kotlin/g2401_2500/s2402_meeting_rooms_iii/readme.md b/src/main/kotlin/g2401_2500/s2402_meeting_rooms_iii/readme.md index 4a562070..19922e80 100644 --- a/src/main/kotlin/g2401_2500/s2402_meeting_rooms_iii/readme.md +++ b/src/main/kotlin/g2401_2500/s2402_meeting_rooms_iii/readme.md @@ -76,13 +76,11 @@ Room 0 held 1 meeting while rooms 1 and 2 each held 2 meetings, so we return 1. ## Solution ```kotlin -import java.util.Arrays - class Solution { fun mostBooked(n: Int, meetings: Array): Int { val counts = IntArray(n) val endTimes = LongArray(n) - Arrays.sort(meetings) { a: IntArray, b: IntArray -> Integer.compare(a[0], b[0]) } + meetings.sortWith { a: IntArray, b: IntArray -> Integer.compare(a[0], b[0]) } for (meeting in meetings) { val id = findRoomId(endTimes, meeting[0]) counts[id]++ diff --git a/src/main/kotlin/g2401_2500/s2463_minimum_total_distance_traveled/readme.md b/src/main/kotlin/g2401_2500/s2463_minimum_total_distance_traveled/readme.md index 55b9c7bd..a01d551a 100644 --- a/src/main/kotlin/g2401_2500/s2463_minimum_total_distance_traveled/readme.md +++ b/src/main/kotlin/g2401_2500/s2463_minimum_total_distance_traveled/readme.md @@ -74,14 +74,12 @@ The total distance is \|2 - 1\| + \|(-2) - (-1)\| = 2. It can be shown that we c ## Solution ```kotlin -import java.util.Arrays - class Solution { fun minimumTotalDistance(robot: List, f: Array): Long { // sort factories : // 1. move all factories with 0-capacity to the end // 2. sort everything else by x-position in asc order - Arrays.sort(f) { a: IntArray, b: IntArray -> if (a[1] == 0) 1 else if (b[1] == 0) -1 else a[0] - b[0] } + f.sortWith { a: IntArray, b: IntArray -> if (a[1] == 0) 1 else if (b[1] == 0) -1 else a[0] - b[0] } // Sort robots by x-position in asc order // As we don't know the implementation of the List that is passed, it is better to map it to // an array explicitly diff --git a/src/main/kotlin/g2501_2600/s2503_maximum_number_of_points_from_grid_queries/readme.md b/src/main/kotlin/g2501_2600/s2503_maximum_number_of_points_from_grid_queries/readme.md index b675509c..57f9e690 100644 --- a/src/main/kotlin/g2501_2600/s2503_maximum_number_of_points_from_grid_queries/readme.md +++ b/src/main/kotlin/g2501_2600/s2503_maximum_number_of_points_from_grid_queries/readme.md @@ -50,7 +50,6 @@ Return _the resulting array_ `answer`. ```kotlin import java.util.ArrayDeque -import java.util.Arrays import java.util.PriorityQueue import java.util.Queue @@ -65,9 +64,9 @@ class Solution { for (i in queries.indices) { index[i] = i } - Arrays.sort(index, { o: Int?, m: Int? -> queries[o!!].compareTo(queries[m!!]) }) + index.sortWith { o: Int?, m: Int? -> queries[o!!].compareTo(queries[m!!]) } val q1: Queue = ArrayDeque() - val q2 = PriorityQueue({ a: IntArray, b: IntArray -> a[2].compareTo(b[2]) }) + val q2 = PriorityQueue { a: IntArray, b: IntArray -> a[2].compareTo(b[2]) } q2.offer(intArrayOf(0, 0, grid[0][0])) val visited = Array(r) { BooleanArray(c) } var count = 0 diff --git a/src/main/kotlin/g2501_2600/s2542_maximum_subsequence_score/readme.md b/src/main/kotlin/g2501_2600/s2542_maximum_subsequence_score/readme.md index c972d98f..d21de250 100644 --- a/src/main/kotlin/g2501_2600/s2542_maximum_subsequence_score/readme.md +++ b/src/main/kotlin/g2501_2600/s2542_maximum_subsequence_score/readme.md @@ -54,7 +54,6 @@ Therefore, we return the max score, which is 12. ## Solution ```kotlin -import java.util.Arrays import java.util.PriorityQueue class Solution { @@ -66,9 +65,8 @@ class Solution { for (i in 0 until n) { nums[i] = PairInfo(nums1[i], nums2[i]) } - Arrays.sort( - nums - ) { a: PairInfo?, b: PairInfo? -> + + nums.sortWith sort@{ a: PairInfo?, b: PairInfo? -> if (a!!.val2 == b!!.val2) { return@sort a.val1 - b.val1 } diff --git a/src/main/kotlin/g2501_2600/s2545_sort_the_students_by_their_kth_score/readme.md b/src/main/kotlin/g2501_2600/s2545_sort_the_students_by_their_kth_score/readme.md index 51be74e5..b4d59890 100644 --- a/src/main/kotlin/g2501_2600/s2545_sort_the_students_by_their_kth_score/readme.md +++ b/src/main/kotlin/g2501_2600/s2545_sort_the_students_by_their_kth_score/readme.md @@ -53,11 +53,9 @@ Return _the matrix after sorting it._ ## Solution ```kotlin -import java.util.Arrays - class Solution { fun sortTheStudents(score: Array, k: Int): Array { - Arrays.sort(score) { o1: IntArray, o2: IntArray -> o2[k] - o1[k] } + score.sortWith { o1: IntArray, o2: IntArray -> o2[k] - o1[k] } return score } } 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 index 01a20d07..ecee3299 100644 --- 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 @@ -65,13 +65,11 @@ Thus, there are four possible ways to group them: ## 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] } + ranges.sortWith { 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]) { 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 index 51093110..3d7151ad 100644 --- 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 @@ -45,13 +45,11 @@ The computer will be on for a total of 4 seconds. ## 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 -> + tasks.sortWith { a: IntArray, b: IntArray -> a[1] - b[1] } for (task in tasks) { diff --git a/src/main/kotlin/g2701_2800/s2719_count_of_integers/readme.md b/src/main/kotlin/g2701_2800/s2719_count_of_integers/readme.md index 2ec3685d..e55abe4f 100644 --- a/src/main/kotlin/g2701_2800/s2719_count_of_integers/readme.md +++ b/src/main/kotlin/g2701_2800/s2719_count_of_integers/readme.md @@ -38,8 +38,6 @@ Note that `digit_sum(x)` denotes the sum of the digits of `x`. ## Solution ```kotlin -import java.util.Arrays - class Solution { private lateinit var dp: Array>> private fun countStrings(i: Int, tight1: Boolean, tight2: Boolean, sum: Int, num1: String, num2: String): Int { @@ -77,7 +75,7 @@ class Solution { for (dim1 in dp) { for (dim2 in dim1) { for (dim3 in dim2) { - Arrays.fill(dim3, -1) + dim3.fill(-1) } } } diff --git a/src/main/kotlin/g2701_2800/s2747_count_zero_request_servers/readme.md b/src/main/kotlin/g2701_2800/s2747_count_zero_request_servers/readme.md index 0c5d73b4..ce70d24c 100644 --- a/src/main/kotlin/g2701_2800/s2747_count_zero_request_servers/readme.md +++ b/src/main/kotlin/g2701_2800/s2747_count_zero_request_servers/readme.md @@ -53,17 +53,15 @@ For queries[1]: Only server with id 3 gets no request in the duration [2,4]. ## Solution ```kotlin -import java.util.Arrays - class Solution { fun countServers(n: Int, logs: Array, x: Int, qs: IntArray): IntArray { val m = qs.size val valIdx = Array(m) { IntArray(2) } for (i in 0 until m) valIdx[i] = intArrayOf(qs[i], i) - Arrays.sort(valIdx) { a: IntArray, b: IntArray -> + valIdx.sortWith { a: IntArray, b: IntArray -> a[0] - b[0] } - Arrays.sort(logs) { a: IntArray, b: IntArray -> + logs.sortWith { a: IntArray, b: IntArray -> a[1] - b[1] } var l = 0 diff --git a/src/main/kotlin/g3001_3100/s3068_find_the_maximum_sum_of_node_values/readme.md b/src/main/kotlin/g3001_3100/s3068_find_the_maximum_sum_of_node_values/readme.md index cdceb31d..b6eeacd4 100644 --- a/src/main/kotlin/g3001_3100/s3068_find_the_maximum_sum_of_node_values/readme.md +++ b/src/main/kotlin/g3001_3100/s3068_find_the_maximum_sum_of_node_values/readme.md @@ -76,7 +76,7 @@ import kotlin.math.min @Suppress("UNUSED_PARAMETER") class Solution { - fun maximumValueSum(nums: IntArray, k: Int, edges: Array?): Long { + fun maximumValueSum(nums: IntArray, k: Int, edges: Array): Long { var res: Long = 0 var d = 1 shl 30 var c = 0 diff --git a/src/main/kotlin/g3101_3200/s3127_make_a_square_with_the_same_color/readme.md b/src/main/kotlin/g3101_3200/s3127_make_a_square_with_the_same_color/readme.md new file mode 100644 index 00000000..7c28545a --- /dev/null +++ b/src/main/kotlin/g3101_3200/s3127_make_a_square_with_the_same_color/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) + +## 3127\. Make a Square with the Same Color + +Easy + +You are given a 2D matrix `grid` of size `3 x 3` consisting only of characters `'B'` and `'W'`. Character `'W'` represents the white color, and character `'B'` represents the black color. + +Your task is to change the color of **at most one** cell so that the matrix has a `2 x 2` square where all cells are of the same color. + +Return `true` if it is possible to create a `2 x 2` square of the same color, otherwise, return `false`. + +**Example 1:** + +**Input:** grid = \[\["B","W","B"],["B","W","W"],["B","W","B"]] + +**Output:** true + +**Explanation:** + +It can be done by changing the color of the `grid[0][2]`. + +**Example 2:** + +**Input:** grid = \[\["B","W","B"],["W","B","W"],["B","W","B"]] + +**Output:** false + +**Explanation:** + +It cannot be done by changing at most one cell. + +**Example 3:** + +**Input:** grid = \[\["B","W","B"],["B","W","W"],["B","W","W"]] + +**Output:** true + +**Explanation:** + +The `grid` already contains a `2 x 2` square of the same color. + +**Constraints:** + +* `grid.length == 3` +* `grid[i].length == 3` +* `grid[i][j]` is either `'W'` or `'B'`. + +## Solution + +```kotlin +class Solution { + fun canMakeSquare(grid: Array): Boolean { + val n = grid.size + val m = grid[0].size + for (i in 0 until n - 1) { + for (j in 0 until m - 1) { + var countBlack = 0 + var countWhite = 0 + for (k in i..i + 1) { + for (l in j..j + 1) { + if (grid[k][l] == 'W') { + countWhite++ + } else { + countBlack++ + } + } + } + if (countBlack >= 3 || countWhite >= 3) { + return true + } + } + } + return false + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g3101_3200/s3128_right_triangles/readme.md b/src/main/kotlin/g3101_3200/s3128_right_triangles/readme.md new file mode 100644 index 00000000..9ecbc796 --- /dev/null +++ b/src/main/kotlin/g3101_3200/s3128_right_triangles/readme.md @@ -0,0 +1,106 @@ +[![](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) + +## 3128\. Right Triangles + +Medium + +You are given a 2D boolean matrix `grid`. + +Return an integer that is the number of **right triangles** that can be made with the 3 elements of `grid` such that **all** of them have a value of 1. + +**Note:** + +* A collection of 3 elements of `grid` is a **right triangle** if one of its elements is in the **same row** with another element and in the **same column** with the third element. The 3 elements do not have to be next to each other. + +**Example 1:** + +0 **1** 0 + +0 **1 1** + +0 1 0 + +0 1 0 + +0 **1 1** + +0 **1** 0 + +**Input:** grid = \[\[0,1,0],[0,1,1],[0,1,0]] + +**Output:** 2 + +**Explanation:** + +There are two right triangles. + +**Example 2:** + +1 0 0 0 + +0 1 0 1 + +1 0 0 0 + +**Input:** grid = \[\[1,0,0,0],[0,1,0,1],[1,0,0,0]] + +**Output:** 0 + +**Explanation:** + +There are no right triangles. + +**Example 3:** + +**1** 0 **1** + +**1** 0 0 + +1 0 0 + +**1** 0 **1** + +1 0 0 + +**1** 0 0 + +**Input:** grid = \[\[1,0,1],[1,0,0],[1,0,0]] + +**Output: **2 + +**Explanation:** + +There are two right triangles. + +**Constraints:** + +* `1 <= grid.length <= 1000` +* `1 <= grid[i].length <= 1000` +* `0 <= grid[i][j] <= 1` + +## Solution + +```kotlin +class Solution { + fun numberOfRightTriangles(grid: Array): Long { + val n = grid.size + val m = grid[0].size + val columns = IntArray(n) + val rows = IntArray(m) + var sum: Long = 0 + for (i in 0 until n) { + for (j in 0 until m) { + columns[i] += grid[i][j] + rows[j] += grid[i][j] + } + } + for (i in 0 until n) { + for (j in 0 until m) { + sum += grid[i][j].toLong() * (rows[j] - 1) * (columns[i] - 1) + } + } + return sum + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g3101_3200/s3129_find_all_possible_stable_binary_arrays_i/readme.md b/src/main/kotlin/g3101_3200/s3129_find_all_possible_stable_binary_arrays_i/readme.md new file mode 100644 index 00000000..45c053ab --- /dev/null +++ b/src/main/kotlin/g3101_3200/s3129_find_all_possible_stable_binary_arrays_i/readme.md @@ -0,0 +1,120 @@ +[![](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) + +## 3129\. Find All Possible Stable Binary Arrays I + +Medium + +You are given 3 positive integers `zero`, `one`, and `limit`. + +A binary array `arr` is called **stable** if: + +* The number of occurrences of 0 in `arr` is **exactly** `zero`. +* The number of occurrences of 1 in `arr` is **exactly** `one`. +* Each subarray of `arr` with a size greater than `limit` must contain **both** 0 and 1. + +Return the _total_ number of **stable** binary arrays. + +Since the answer may be very large, return it **modulo** 109 + 7. + +**Example 1:** + +**Input:** zero = 1, one = 1, limit = 2 + +**Output:** 2 + +**Explanation:** + +The two possible stable binary arrays are `[1,0]` and `[0,1]`, as both arrays have a single 0 and a single 1, and no subarray has a length greater than 2. + +**Example 2:** + +**Input:** zero = 1, one = 2, limit = 1 + +**Output:** 1 + +**Explanation:** + +The only possible stable binary array is `[1,0,1]`. + +Note that the binary arrays `[1,1,0]` and `[0,1,1]` have subarrays of length 2 with identical elements, hence, they are not stable. + +**Example 3:** + +**Input:** zero = 3, one = 3, limit = 2 + +**Output:** 14 + +**Explanation:** + +All the possible stable binary arrays are `[0,0,1,0,1,1]`, `[0,0,1,1,0,1]`, `[0,1,0,0,1,1]`, `[0,1,0,1,0,1]`, `[0,1,0,1,1,0]`, `[0,1,1,0,0,1]`, `[0,1,1,0,1,0]`, `[1,0,0,1,0,1]`, `[1,0,0,1,1,0]`, `[1,0,1,0,0,1]`, `[1,0,1,0,1,0]`, `[1,0,1,1,0,0]`, `[1,1,0,0,1,0]`, and `[1,1,0,1,0,0]`. + +**Constraints:** + +* `1 <= zero, one, limit <= 200` + +## Solution + +```kotlin +import kotlin.math.abs +import kotlin.math.max +import kotlin.math.min + +class Solution { + private fun add(x: Int, y: Int): Int { + return (x + y) % MODULUS + } + + private fun subtract(x: Int, y: Int): Int { + return (x + MODULUS - y) % MODULUS + } + + private fun multiply(x: Int, y: Int): Int { + return (x.toLong() * y % MODULUS).toInt() + } + + fun numberOfStableArrays(zero: Int, one: Int, limit: Int): Int { + if (limit == 1) { + return max((2 - abs((zero - one))), 0) + } + val max = max(zero, one) + val min = min(zero, one) + val lcn = Array(max + 1) { IntArray(max + 1) } + var row0 = lcn[0] + var row1: IntArray + var row2: IntArray + row0[0] = 1 + var s = 1 + var sLim = s - limit + while (s <= max) { + row2 = if (sLim > 0) lcn[sLim - 1] else intArrayOf() + row1 = row0 + row0 = lcn[s] + var c = (s - 1) / limit + 1 + while (c <= sLim) { + row0[c] = subtract(add(row1[c], row1[c - 1]), row2[c - 1]) + c++ + } + while (c <= s) { + row0[c] = add(row1[c], row1[c - 1]) + c++ + } + s++ + sLim++ + } + row1 = lcn[min] + var result = 0 + var s0 = add(if (min < max) row0[min + 1] else 0, row0[min]) + for (c in min downTo 1) { + val s1 = s0 + s0 = add(row0[c], row0[c - 1]) + result = add(result, multiply(row1[c], add(s0, s1))) + } + return result + } + + companion object { + private const val MODULUS = 1e9.toInt() + 7 + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g3101_3200/s3130_find_all_possible_stable_binary_arrays_ii/readme.md b/src/main/kotlin/g3101_3200/s3130_find_all_possible_stable_binary_arrays_ii/readme.md new file mode 100644 index 00000000..b08f16f9 --- /dev/null +++ b/src/main/kotlin/g3101_3200/s3130_find_all_possible_stable_binary_arrays_ii/readme.md @@ -0,0 +1,142 @@ +[![](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) + +## 3130\. Find All Possible Stable Binary Arrays II + +Hard + +You are given 3 positive integers `zero`, `one`, and `limit`. + +A binary array `arr` is called **stable** if: + +* The number of occurrences of 0 in `arr` is **exactly** `zero`. +* The number of occurrences of 1 in `arr` is **exactly** `one`. +* Each subarray of `arr` with a size greater than `limit` must contain **both** 0 and 1. + +Return the _total_ number of **stable** binary arrays. + +Since the answer may be very large, return it **modulo** 109 + 7. + +**Example 1:** + +**Input:** zero = 1, one = 1, limit = 2 + +**Output:** 2 + +**Explanation:** + +The two possible stable binary arrays are `[1,0]` and `[0,1]`. + +**Example 2:** + +**Input:** zero = 1, one = 2, limit = 1 + +**Output:** 1 + +**Explanation:** + +The only possible stable binary array is `[1,0,1]`. + +**Example 3:** + +**Input:** zero = 3, one = 3, limit = 2 + +**Output:** 14 + +**Explanation:** + +All the possible stable binary arrays are `[0,0,1,0,1,1]`, `[0,0,1,1,0,1]`, `[0,1,0,0,1,1]`, `[0,1,0,1,0,1]`, `[0,1,0,1,1,0]`, `[0,1,1,0,0,1]`, `[0,1,1,0,1,0]`, `[1,0,0,1,0,1]`, `[1,0,0,1,1,0]`, `[1,0,1,0,0,1]`, `[1,0,1,0,1,0]`, `[1,0,1,1,0,0]`, `[1,1,0,0,1,0]`, and `[1,1,0,1,0,0]`. + +**Constraints:** + +* `1 <= zero, one, limit <= 1000` + +## Solution + +```kotlin +import kotlin.math.max +import kotlin.math.min + +class Solution { + private var factorial: LongArray? = null + private lateinit var reverse: LongArray + + fun numberOfStableArrays(zero: Int, one: Int, limit: Int): Int { + if (factorial == null) { + factorial = LongArray(N + 1) + reverse = LongArray(N + 1) + factorial!![0] = 1 + reverse[0] = 1 + var x: Long = 1 + for (i in 1..N) { + x = (x * i) % MOD + factorial!![i] = x.toInt().toLong() + reverse[i] = getInverse(x, MOD.toLong()) + } + } + var ans: Long = 0 + val s = LongArray(one + 1) + val n = (min(zero, one) + 1).toInt() + for ( + groups0 in (zero + limit - 1) / limit..min(zero, n) + .toInt() + ) { + val s0 = calc(groups0, zero, limit) + for ( + groups1 in max( + groups0 - 1, + (one + limit - 1) / limit + )..min((groups0 + 1), one) + ) { + var s1: Long + if (s[groups1] != 0L) { + s1 = s[groups1] + } else { + s[groups1] = calc(groups1, one, limit) + s1 = s[groups1] + } + ans = (ans + s0 * s1 * (if (groups1 == groups0) 2 else 1)) % MOD + } + } + return ((ans + MOD) % MOD).toInt() + } + + fun calc(groups: Int, x: Int, limit: Int): Long { + var s: Long = 0 + var sign = 1 + var k = 0 + while (k * limit <= x - groups && k <= groups) { + s = (s + sign * comb(groups, k) * comb(x - k * limit - 1, groups - 1)) % MOD + sign *= -1 + k++ + } + return s + } + + fun comb(n: Int, k: Int): Long { + return (factorial!![n] * reverse[k] % MOD) * reverse[n - k] % MOD + } + + fun getInverse(n: Long, mod: Long): Long { + var n = n + var p = mod + var x: Long = 1 + var y: Long = 0 + while (p > 0) { + val quotient = n / p + val remainder = n % p + val tempY = x - quotient * y + x = y + y = tempY + n = p + p = remainder + } + return ((x % mod) + mod) % mod + } + + companion object { + private const val MOD = 1e9.toInt() + 7 + private const val N = 1000 + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g3101_3200/s3131_find_the_integer_added_to_array_i/readme.md b/src/main/kotlin/g3101_3200/s3131_find_the_integer_added_to_array_i/readme.md new file mode 100644 index 00000000..0fd83526 --- /dev/null +++ b/src/main/kotlin/g3101_3200/s3131_find_the_integer_added_to_array_i/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) + +## 3131\. Find the Integer Added to Array I + +Easy + +You are given two arrays of equal length, `nums1` and `nums2`. + +Each element in `nums1` has been increased (or decreased in the case of negative) by an integer, represented by the variable `x`. + +As a result, `nums1` becomes **equal** to `nums2`. Two arrays are considered **equal** when they contain the same integers with the same frequencies. + +Return the integer `x`. + +**Example 1:** + +**Input:** nums1 = [2,6,4], nums2 = [9,7,5] + +**Output:** 3 + +**Explanation:** + +The integer added to each element of `nums1` is 3. + +**Example 2:** + +**Input:** nums1 = [10], nums2 = [5] + +**Output:** \-5 + +**Explanation:** + +The integer added to each element of `nums1` is -5. + +**Example 3:** + +**Input:** nums1 = [1,1,1,1], nums2 = [1,1,1,1] + +**Output:** 0 + +**Explanation:** + +The integer added to each element of `nums1` is 0. + +**Constraints:** + +* `1 <= nums1.length == nums2.length <= 100` +* `0 <= nums1[i], nums2[i] <= 1000` +* The test cases are generated in a way that there is an integer `x` such that `nums1` can become equal to `nums2` by adding `x` to each element of `nums1`. + +## Solution + +```kotlin +class Solution { + fun addedInteger(nums1: IntArray, nums2: IntArray): Int { + val n1 = nums1.size + var s1 = 0 + var s2 = 0 + for (i in nums1) { + s1 += i + } + for (i in nums2) { + s2 += i + } + return (s2 - s1) / n1 + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g3101_3200/s3132_find_the_integer_added_to_array_ii/readme.md b/src/main/kotlin/g3101_3200/s3132_find_the_integer_added_to_array_ii/readme.md new file mode 100644 index 00000000..f6949bbf --- /dev/null +++ b/src/main/kotlin/g3101_3200/s3132_find_the_integer_added_to_array_ii/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) + +## 3132\. Find the Integer Added to Array II + +Medium + +You are given two integer arrays `nums1` and `nums2`. + +From `nums1` two elements have been removed, and all other elements have been increased (or decreased in the case of negative) by an integer, represented by the variable `x`. + +As a result, `nums1` becomes **equal** to `nums2`. Two arrays are considered **equal** when they contain the same integers with the same frequencies. + +Return the **minimum** possible integer `x` that achieves this equivalence. + +**Example 1:** + +**Input:** nums1 = [4,20,16,12,8], nums2 = [14,18,10] + +**Output:** \-2 + +**Explanation:** + +After removing elements at indices `[0,4]` and adding -2, `nums1` becomes `[18,14,10]`. + +**Example 2:** + +**Input:** nums1 = [3,5,5,3], nums2 = [7,7] + +**Output:** 2 + +**Explanation:** + +After removing elements at indices `[0,3]` and adding 2, `nums1` becomes `[7,7]`. + +**Constraints:** + +* `3 <= nums1.length <= 200` +* `nums2.length == nums1.length - 2` +* `0 <= nums1[i], nums2[i] <= 1000` +* The test cases are generated in a way that there is an integer `x` such that `nums1` can become equal to `nums2` by removing two elements and adding `x` to each element of `nums1`. + +## Solution + +```kotlin +@Suppress("NAME_SHADOWING") +class Solution { + fun minimumAddedInteger(nums1: IntArray, nums2: IntArray): Int { + nums1.sort() + nums2.sort() + return if (checkOk(nums1, nums2, 2)) { + nums2[0] - nums1[2] + } else if (checkOk(nums1, nums2, 1)) { + nums2[0] - nums1[1] + } else { + nums2[0] - nums1[0] + } + } + + private fun checkOk(nums1: IntArray, nums2: IntArray, start: Int): Boolean { + var start = start + var i = 0 + val diff = nums2[i] - nums1[start] + while (i < nums2.size) { + if (start - i > 2) { + return false + } + if (nums2[i] == nums1[start] + diff) { + i++ + } + start++ + } + return i == nums2.size + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g3101_3200/s3133_minimum_array_end/readme.md b/src/main/kotlin/g3101_3200/s3133_minimum_array_end/readme.md new file mode 100644 index 00000000..a3a9bc0f --- /dev/null +++ b/src/main/kotlin/g3101_3200/s3133_minimum_array_end/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) + +## 3133\. Minimum Array End + +Medium + +You are given two integers `n` and `x`. You have to construct an array of **positive** integers `nums` of size `n` where for every `0 <= i < n - 1`, `nums[i + 1]` is **greater than** `nums[i]`, and the result of the bitwise `AND` operation between all elements of `nums` is `x`. + +Return the **minimum** possible value of `nums[n - 1]`. + +**Example 1:** + +**Input:** n = 3, x = 4 + +**Output:** 6 + +**Explanation:** + +`nums` can be `[4,5,6]` and its last element is 6. + +**Example 2:** + +**Input:** n = 2, x = 7 + +**Output:** 15 + +**Explanation:** + +`nums` can be `[7,15]` and its last element is 15. + +**Constraints:** + +* 1 <= n, x <= 108 + +## Solution + +```kotlin +@Suppress("NAME_SHADOWING") +class Solution { + fun minEnd(n: Int, x: Int): Long { + var n = n + n -= 1 + val xb = IntArray(64) + val nb = IntArray(64) + for (i in 0..31) { + xb[i] = (x shr i) and 1 + nb[i] = (n shr i) and 1 + } + var i = 0 + var j = 0 + while (i < 64) { + if (xb[i] != 1) { + xb[i] = nb[j++] + } + i++ + } + var ans: Long = 0 + var p: Long = 1 + i = 0 + while (i < 64) { + ans += (xb[i]) * p + p *= 2 + i++ + } + return ans + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g3101_3200/s3134_find_the_median_of_the_uniqueness_array/readme.md b/src/main/kotlin/g3101_3200/s3134_find_the_median_of_the_uniqueness_array/readme.md new file mode 100644 index 00000000..c6cf38ea --- /dev/null +++ b/src/main/kotlin/g3101_3200/s3134_find_the_median_of_the_uniqueness_array/readme.md @@ -0,0 +1,103 @@ +[![](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) + +## 3134\. Find the Median of the Uniqueness Array + +Hard + +You are given an integer array `nums`. The **uniqueness array** of `nums` is the sorted array that contains the number of distinct elements of all the subarrays of `nums`. In other words, it is a sorted array consisting of `distinct(nums[i..j])`, for all `0 <= i <= j < nums.length`. + +Here, `distinct(nums[i..j])` denotes the number of distinct elements in the subarray that starts at index `i` and ends at index `j`. + +Return the **median** of the **uniqueness array** of `nums`. + +**Note** that the **median** of an array is defined as the middle element of the array when it is sorted in non-decreasing order. If there are two choices for a median, the **smaller** of the two values is taken. + +**Example 1:** + +**Input:** nums = [1,2,3] + +**Output:** 1 + +**Explanation:** + +The uniqueness array of `nums` is `[distinct(nums[0..0]), distinct(nums[1..1]), distinct(nums[2..2]), distinct(nums[0..1]), distinct(nums[1..2]), distinct(nums[0..2])]` which is equal to `[1, 1, 1, 2, 2, 3]`. The uniqueness array has a median of 1. Therefore, the answer is 1. + +**Example 2:** + +**Input:** nums = [3,4,3,4,5] + +**Output:** 2 + +**Explanation:** + +The uniqueness array of `nums` is `[1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3]`. The uniqueness array has a median of 2. Therefore, the answer is 2. + +**Example 3:** + +**Input:** nums = [4,3,5,4] + +**Output:** 2 + +**Explanation:** + +The uniqueness array of `nums` is `[1, 1, 1, 1, 2, 2, 2, 3, 3, 3]`. The uniqueness array has a median of 2. Therefore, the answer is 2. + +**Constraints:** + +* 1 <= nums.length <= 105 +* 1 <= nums[i] <= 105 + +## Solution + +```kotlin +import kotlin.math.max + +class Solution { + fun medianOfUniquenessArray(nums: IntArray): Int { + var max = 0 + for (x in nums) { + max = max(max, x) + } + val n = nums.size + val k = (n.toLong() * (n + 1) / 2 + 1) / 2 + var left = 0 + var right = n / 2 + while (left <= right) { + val mid = left + right shr 1 + if (check(nums, max, mid, k)) { + right = mid - 1 + } else { + left = mid + 1 + } + } + return left + } + + private fun check(nums: IntArray, max: Int, target: Int, k: Long): Boolean { + var count: Long = 0 + var distinct = 0 + val n = nums.size + var left = 0 + var right = 0 + val freq = IntArray(max + 1) + while (right < n) { + var x = nums[right++] + if (++freq[x] == 1) { + distinct++ + } + while (distinct > target) { + x = nums[left++] + if (--freq[x] == 0) { + distinct-- + } + } + count += (right - left).toLong() + if (count >= k) { + return true + } + } + return false + } +} +``` \ No newline at end of file