From 4bcdaaa12b5fdff712cefa36e2858a996bee60ff Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Sun, 24 Sep 2023 15:02:52 +0300 Subject: [PATCH] Added tasks 2730-2747, 2762-2772 --- README.md | 89 ++++++++----- .../readme.md | 20 ++- .../s0239_sliding_window_maximum/readme.md | 24 +++- .../s0240_search_a_2d_matrix_ii/readme.md | 16 ++- .../g0201_0300/s0283_move_zeroes/readme.md | 12 +- .../s0287_find_the_duplicate_number/readme.md | 12 +- .../readme.md | 18 ++- .../readme.md | 15 ++- .../g0301_0400/s0322_coin_change/readme.md | 15 ++- .../g0301_0400/s0338_counting_bits/readme.md | 18 ++- .../s0347_top_k_frequent_elements/readme.md | 21 +++- .../g0301_0400/s0394_decode_string/readme.md | 30 ++++- .../readme.md | 62 ++++++++++ .../s2731_movement_of_robots/readme.md | 89 +++++++++++++ .../readme.md | 105 ++++++++++++++++ .../readme.md | 61 +++++++++ .../readme.md | 67 ++++++++++ .../s2735_collecting_chocolates/readme.md | 68 ++++++++++ .../s2736_maximum_sum_queries/readme.md | 117 ++++++++++++++++++ .../s2739_total_distance_traveled/readme.md | 52 ++++++++ .../readme.md | 73 +++++++++++ .../s2741_special_permutations/readme.md | 71 +++++++++++ .../s2742_painting_the_walls/readme.md | 59 +++++++++ .../readme.md | 88 +++++++++++++ .../readme.md | 51 ++++++++ .../readme.md | 111 +++++++++++++++++ .../readme.md | 90 ++++++++++++++ .../s2762_continuous_subarrays/readme.md | 86 +++++++++++++ .../readme.md | 78 ++++++++++++ .../readme.md | 69 +++++++++++ .../s2766_relocate_marbles/readme.md | 82 ++++++++++++ .../readme.md | 96 ++++++++++++++ .../s2768_number_of_black_blocks/readme.md | 83 +++++++++++++ .../readme.md | 58 +++++++++ .../readme.md | 108 ++++++++++++++++ .../readme.md | 92 ++++++++++++++ .../readme.md | 67 ++++++++++ 37 files changed, 2230 insertions(+), 43 deletions(-) create mode 100644 src/main/java/g2701_2800/s2730_find_the_longest_semi_repetitive_substring/readme.md create mode 100644 src/main/java/g2701_2800/s2731_movement_of_robots/readme.md create mode 100644 src/main/java/g2701_2800/s2732_find_a_good_subset_of_the_matrix/readme.md create mode 100644 src/main/java/g2701_2800/s2733_neither_minimum_nor_maximum/readme.md create mode 100644 src/main/java/g2701_2800/s2734_lexicographically_smallest_string_after_substring_operation/readme.md create mode 100644 src/main/java/g2701_2800/s2735_collecting_chocolates/readme.md create mode 100644 src/main/java/g2701_2800/s2736_maximum_sum_queries/readme.md create mode 100644 src/main/java/g2701_2800/s2739_total_distance_traveled/readme.md create mode 100644 src/main/java/g2701_2800/s2740_find_the_value_of_the_partition/readme.md create mode 100644 src/main/java/g2701_2800/s2741_special_permutations/readme.md create mode 100644 src/main/java/g2701_2800/s2742_painting_the_walls/readme.md create mode 100644 src/main/java/g2701_2800/s2744_find_maximum_number_of_string_pairs/readme.md create mode 100644 src/main/java/g2701_2800/s2745_construct_the_longest_new_string/readme.md create mode 100644 src/main/java/g2701_2800/s2746_decremental_string_concatenation/readme.md create mode 100644 src/main/java/g2701_2800/s2747_count_zero_request_servers/readme.md create mode 100644 src/main/java/g2701_2800/s2762_continuous_subarrays/readme.md create mode 100644 src/main/java/g2701_2800/s2763_sum_of_imbalance_numbers_of_all_subarrays/readme.md create mode 100644 src/main/java/g2701_2800/s2765_longest_alternating_subarray/readme.md create mode 100644 src/main/java/g2701_2800/s2766_relocate_marbles/readme.md create mode 100644 src/main/java/g2701_2800/s2767_partition_string_into_minimum_beautiful_substrings/readme.md create mode 100644 src/main/java/g2701_2800/s2768_number_of_black_blocks/readme.md create mode 100644 src/main/java/g2701_2800/s2769_find_the_maximum_achievable_number/readme.md create mode 100644 src/main/java/g2701_2800/s2770_maximum_number_of_jumps_to_reach_the_last_index/readme.md create mode 100644 src/main/java/g2701_2800/s2771_longest_non_decreasing_subarray_from_two_arrays/readme.md create mode 100644 src/main/java/g2701_2800/s2772_apply_operations_to_make_all_array_elements_equal_to_zero/readme.md diff --git a/README.md b/README.md index 0799b537..d841064a 100644 --- a/README.md +++ b/README.md @@ -47,7 +47,7 @@ | | | | | | |-|-|-|-|-|- -| 0240 |[Search a 2D Matrix II](src/main/java/g0201_0300/s0240_search_a_2d_matrix_ii)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Binary_Search, Matrix, Divide_and_Conquer | 7 | 86.73 +| 0240 |[Search a 2D Matrix II](src/main/java/g0201_0300/s0240_search_a_2d_matrix_ii)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Binary_Search, Matrix, Divide_and_Conquer, Big_O_Time_O(n+m)_Space_O(1) | 7 | 86.73 | 0435 |[Non-overlapping Intervals](src/main/java/g0401_0500/s0435_non_overlapping_intervals)| Medium | Array, Dynamic_Programming, Sorting, Greedy | 96 | 47.37 #### Day 5 Array @@ -55,7 +55,7 @@ | | | | | | |-|-|-|-|-|- | 0334 |[Increasing Triplet Subsequence](src/main/java/g0301_0400/s0334_increasing_triplet_subsequence)| Medium | Top_Interview_Questions, Array, Greedy | 2 | 99.33 -| 0238 |[Product of Array Except Self](src/main/java/g0201_0300/s0238_product_of_array_except_self)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Prefix_Sum | 1 | 100.00 +| 0238 |[Product of Array Except Self](src/main/java/g0201_0300/s0238_product_of_array_except_self)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Prefix_Sum, Big_O_Time_O(n^2)_Space_O(n) | 1 | 100.00 | 0560 |[Subarray Sum Equals K](src/main/java/g0501_0600/s0560_subarray_sum_equals_k)| Medium | Top_100_Liked_Questions, Array, Hash_Table, Prefix_Sum | 21 | 98.97 #### Day 6 String @@ -165,7 +165,7 @@ | | | | | | |-|-|-|-|-|- | 0215 |[Kth Largest Element in an Array](src/main/java/g0201_0300/s0215_kth_largest_element_in_an_array)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Heap_Priority_Queue, Divide_and_Conquer, Quickselect, Big_O_Time_O(n*log(n))_Space_O(log(n)) | 5 | 70.82 -| 0347 |[Top K Frequent Elements](src/main/java/g0301_0400/s0347_top_k_frequent_elements)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Sorting, Heap_Priority_Queue, Counting, Divide_and_Conquer, Quickselect, Bucket_Sort | 9 | 97.93 +| 0347 |[Top K Frequent Elements](src/main/java/g0301_0400/s0347_top_k_frequent_elements)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Sorting, Heap_Priority_Queue, Counting, Divide_and_Conquer, Quickselect, Bucket_Sort, Big_O_Time_O(n*log(n))_Space_O(k) | 9 | 97.93 #### Day 21 Heap Priority Queue @@ -195,7 +195,7 @@ | | | | | | |-|-|-|-|-|- -| 0283 |[Move Zeroes](src/main/java/g0201_0300/s0283_move_zeroes)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Two_Pointers | 2 | 79.54 +| 0283 |[Move Zeroes](src/main/java/g0201_0300/s0283_move_zeroes)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Two_Pointers, Big_O_Time_O(n)_Space_O(1) | 2 | 79.54 | 0167 |[Two Sum II - Input Array Is Sorted](src/main/java/g0101_0200/s0167_two_sum_ii_input_array_is_sorted)| Medium | Array, Binary_Search, Two_Pointers | 1 | 99.21 #### Day 4 Two Pointers @@ -394,7 +394,7 @@ | | | | | | |-|-|-|-|-|- -| 0300 |[Longest Increasing Subsequence](src/main/java/g0201_0300/s0300_longest_increasing_subsequence)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Binary_Search | 3 | 98.63 +| 0300 |[Longest Increasing Subsequence](src/main/java/g0201_0300/s0300_longest_increasing_subsequence)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Binary_Search, Big_O_Time_O(n*log_n)_Space_O(n) | 3 | 98.63 | 0673 |[Number of Longest Increasing Subsequence](src/main/java/g0601_0700/s0673_number_of_longest_increasing_subsequence)| Medium | Array, Dynamic_Programming, Segment_Tree, Binary_Indexed_Tree | 25 | 68.75 #### Day 17 Dynamic Programming @@ -409,7 +409,7 @@ | | | | | | |-|-|-|-|-|- | 0072 |[Edit Distance](src/main/java/g0001_0100/s0072_edit_distance)| Hard | Top_100_Liked_Questions, String, Dynamic_Programming, Big_O_Time_O(n^2)_Space_O(n2) | 4 | 90.13 -| 0322 |[Coin Change](src/main/java/g0301_0400/s0322_coin_change)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Breadth_First_Search | 17 | 91.77 +| 0322 |[Coin Change](src/main/java/g0301_0400/s0322_coin_change)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Breadth_First_Search, Big_O_Time_O(m*n)_Space_O(amount) | 17 | 91.77 | 0343 |[Integer Break](src/main/java/g0301_0400/s0343_integer_break)| Medium | Dynamic_Programming, Math | 0 | 100.00 #### Day 19 Bit Manipulation @@ -536,7 +536,7 @@ | | | | | | |-|-|-|-|-|- -| 0300 |[Longest Increasing Subsequence](src/main/java/g0201_0300/s0300_longest_increasing_subsequence)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Binary_Search | 3 | 98.63 +| 0300 |[Longest Increasing Subsequence](src/main/java/g0201_0300/s0300_longest_increasing_subsequence)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Binary_Search, Big_O_Time_O(n*log_n)_Space_O(n) | 3 | 98.63 | 1760 |[Minimum Limit of Balls in a Bag](src/main/java/g1701_1800/s1760_minimum_limit_of_balls_in_a_bag)| Medium | Array, Binary_Search | 44 | 78.49 #### Day 4 @@ -550,7 +550,7 @@ | | | | | | |-|-|-|-|-|- -| 0287 |[Find the Duplicate Number](src/main/java/g0201_0300/s0287_find_the_duplicate_number)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Binary_Search, Two_Pointers, Bit_Manipulation | 2 | 99.82 +| 0287 |[Find the Duplicate Number](src/main/java/g0201_0300/s0287_find_the_duplicate_number)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Binary_Search, Two_Pointers, Bit_Manipulation, Big_O_Time_O(n)_Space_O(n) | 2 | 99.82 | 1283 |[Find the Smallest Divisor Given a Threshold](src/main/java/g1201_1300/s1283_find_the_smallest_divisor_given_a_threshold)| Medium | Array, Binary_Search | 9 | 95.49 #### Day 6 @@ -571,7 +571,7 @@ | | | | | | |-|-|-|-|-|- -| 0240 |[Search a 2D Matrix II](src/main/java/g0201_0300/s0240_search_a_2d_matrix_ii)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Binary_Search, Matrix, Divide_and_Conquer | 7 | 86.73 +| 0240 |[Search a 2D Matrix II](src/main/java/g0201_0300/s0240_search_a_2d_matrix_ii)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Binary_Search, Matrix, Divide_and_Conquer, Big_O_Time_O(n+m)_Space_O(1) | 7 | 86.73 | 0275 |[H-Index II](src/main/java/g0201_0300/s0275_h_index_ii)| Medium | Array, Binary_Search | 0 | 100.00 #### Day 9 @@ -785,7 +785,7 @@ | | | | | | |-|-|-|-|-|- -| 0300 |[Longest Increasing Subsequence](src/main/java/g0201_0300/s0300_longest_increasing_subsequence)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Binary_Search | 3 | 98.63 +| 0300 |[Longest Increasing Subsequence](src/main/java/g0201_0300/s0300_longest_increasing_subsequence)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Binary_Search, Big_O_Time_O(n*log_n)_Space_O(n) | 3 | 98.63 | 0376 |[Wiggle Subsequence](src/main/java/g0301_0400/s0376_wiggle_subsequence)| Medium | Array, Dynamic_Programming, Greedy | 0 | 100.00 #### Day 19 @@ -800,7 +800,7 @@ | | | | | | |-|-|-|-|-|- -| 0322 |[Coin Change](src/main/java/g0301_0400/s0322_coin_change)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Breadth_First_Search | 17 | 91.77 +| 0322 |[Coin Change](src/main/java/g0301_0400/s0322_coin_change)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Breadth_First_Search, Big_O_Time_O(m*n)_Space_O(amount) | 17 | 91.77 | 0518 |[Coin Change 2](src/main/java/g0501_0600/s0518_coin_change_2)| Medium | Array, Dynamic_Programming | 4 | 84.67 #### Day 21 @@ -856,7 +856,7 @@ | | | | | | |-|-|-|-|-|- | 1588 |[Sum of All Odd Length Subarrays](src/main/java/g1501_1600/s1588_sum_of_all_odd_length_subarrays)| Easy | Array, Math, Prefix_Sum | 0 | 100.00 -| 0283 |[Move Zeroes](src/main/java/g0201_0300/s0283_move_zeroes)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Two_Pointers | 2 | 79.54 +| 0283 |[Move Zeroes](src/main/java/g0201_0300/s0283_move_zeroes)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Two_Pointers, Big_O_Time_O(n)_Space_O(1) | 2 | 79.54 | 1672 |[Richest Customer Wealth](src/main/java/g1601_1700/s1672_richest_customer_wealth)| Easy | Array, Matrix | 0 | 100.00 #### Day 7 Array @@ -1337,7 +1337,7 @@ | | | | | | |-|-|-|-|-|- | 0844 |[Backspace String Compare](src/main/java/g0801_0900/s0844_backspace_string_compare)| Easy | String, Two_Pointers, Stack, Simulation | 0 | 100.00 -| 0394 |[Decode String](src/main/java/g0301_0400/s0394_decode_string)| Medium | Top_100_Liked_Questions, String, Stack, Recursion | 1 | 87.68 +| 0394 |[Decode String](src/main/java/g0301_0400/s0394_decode_string)| Medium | Top_100_Liked_Questions, String, Stack, Recursion, Big_O_Time_O(n)_Space_O(n) | 1 | 87.68 #### Day 15 Heap @@ -1432,7 +1432,7 @@ | | | | | | |-|-|-|-|-|- | 0198 |[House Robber](src/main/java/g0101_0200/s0198_house_robber)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Big_O_Time_O(n)_Space_O(n) | 0 | 100.00 -| 0322 |[Coin Change](src/main/java/g0301_0400/s0322_coin_change)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Breadth_First_Search | 17 | 91.77 +| 0322 |[Coin Change](src/main/java/g0301_0400/s0322_coin_change)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Breadth_First_Search, Big_O_Time_O(m*n)_Space_O(amount) | 17 | 91.77 #### Day 13 Dynamic Programming @@ -1516,7 +1516,7 @@ | 0003 |[Longest Substring Without Repeating Characters](src/main/java/g0001_0100/s0003_longest_substring_without_repeating_characters)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Sliding_Window, Big_O_Time_O(n)_Space_O(1) | 2 | 99.88 | 0020 |[Valid Parentheses](src/main/java/g0001_0100/s0020_valid_parentheses)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, String, Stack, Big_O_Time_O(n)_Space_O(n) | 2 | 90.49 | 0005 |[Longest Palindromic Substring](src/main/java/g0001_0100/s0005_longest_palindromic_substring)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Dynamic_Programming, Big_O_Time_O(n)_Space_O(n) | 7 | 97.94 -| 0394 |[Decode String](src/main/java/g0301_0400/s0394_decode_string)| Medium | Top_100_Liked_Questions, String, Stack, Recursion | 1 | 87.68 +| 0394 |[Decode String](src/main/java/g0301_0400/s0394_decode_string)| Medium | Top_100_Liked_Questions, String, Stack, Recursion, Big_O_Time_O(n)_Space_O(n) | 1 | 87.68 | 0242 |[Valid Anagram](src/main/java/g0201_0300/s0242_valid_anagram)| Easy | Top_Interview_Questions, String, Hash_Table, Sorting | 2 | 99.01 | 0049 |[Group Anagrams](src/main/java/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) | 6 | 92.28 | 0151 |[Reverse Words in a String](src/main/java/g0101_0200/s0151_reverse_words_in_a_string)| Medium | String, Two_Pointers | 2 | 99.94 @@ -1535,7 +1535,7 @@ | | | | | | |-|-|-|-|-|- | 0121 |[Best Time to Buy and Sell Stock](src/main/java/g0101_0200/s0121_best_time_to_buy_and_sell_stock)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Big_O_Time_O(N)_Space_O(1) | 1 | 100.00 -| 0283 |[Move Zeroes](src/main/java/g0201_0300/s0283_move_zeroes)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Two_Pointers | 2 | 79.54 +| 0283 |[Move Zeroes](src/main/java/g0201_0300/s0283_move_zeroes)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Two_Pointers, Big_O_Time_O(n)_Space_O(1) | 2 | 79.54 | 0001 |[Two Sum](src/main/java/g0001_0100/s0001_two_sum)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Big_O_Time_O(n)_Space_O(n) | 1 | 99.37 | 0217 |[Contains Duplicate](src/main/java/g0201_0300/s0217_contains_duplicate)| Easy | Top_Interview_Questions, Array, Hash_Table, Sorting | 6 | 96.68 | 0058 |[Length of Last Word](src/main/java/g0001_0100/s0058_length_of_last_word)| Easy | String | 0 | 100.00 @@ -1546,7 +1546,7 @@ | 0055 |[Jump Game](src/main/java/g0001_0100/s0055_jump_game)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Greedy, Big_O_Time_O(n)_Space_O(1) | 2 | 79.47 | 0075 |[Sort Colors](src/main/java/g0001_0100/s0075_sort_colors)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Two_Pointers, Big_O_Time_O(n)_Space_O(1) | 0 | 100.00 | 0066 |[Plus One](src/main/java/g0001_0100/s0066_plus_one)| Easy | Top_Interview_Questions, Array, Math | 0 | 100.00 -| 0238 |[Product of Array Except Self](src/main/java/g0201_0300/s0238_product_of_array_except_self)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Prefix_Sum | 1 | 100.00 +| 0238 |[Product of Array Except Self](src/main/java/g0201_0300/s0238_product_of_array_except_self)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Prefix_Sum, Big_O_Time_O(n^2)_Space_O(n) | 1 | 100.00 | 1291 |[Sequential Digits](src/main/java/g1201_1300/s1291_sequential_digits)| Medium | Enumeration | 0 | 100.00 | 0448 |[Find All Numbers Disappeared in an Array](src/main/java/g0401_0500/s0448_find_all_numbers_disappeared_in_an_array)| Easy | Array, Hash_Table | 3 | 100.00 | 0442 |[Find All Duplicates in an Array](src/main/java/g0401_0500/s0442_find_all_duplicates_in_an_array)| Medium | Array, Hash_Table | 5 | 98.83 @@ -1557,7 +1557,7 @@ | 1007 |[Minimum Domino Rotations For Equal Row](src/main/java/g1001_1100/s1007_minimum_domino_rotations_for_equal_row)| Medium | Array, Greedy | 5 | 79.64 | 1306 |[Jump Game III](src/main/java/g1301_1400/s1306_jump_game_iii)| Medium | Array, Depth_First_Search, Breadth_First_Search | 2 | 96.23 | 0456 |[132 Pattern](src/main/java/g0401_0500/s0456_132_pattern)| Medium | Array, Binary_Search, Stack, Ordered_Set, Monotonic_Stack | 16 | 82.41 -| 0239 |[Sliding Window Maximum](src/main/java/g0201_0300/s0239_sliding_window_maximum)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Array, Heap_Priority_Queue, Sliding_Window, Queue, Monotonic_Queue | 58 | 52.28 +| 0239 |[Sliding Window Maximum](src/main/java/g0201_0300/s0239_sliding_window_maximum)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Array, Heap_Priority_Queue, Sliding_Window, Queue, Monotonic_Queue, Big_O_Time_O(n*k)_Space_O(n+k) | 58 | 52.28 #### Udemy Two Pointers @@ -1671,7 +1671,7 @@ | 0509 |[Fibonacci Number](src/main/java/g0501_0600/s0509_fibonacci_number)| Easy | Dynamic_Programming, Math, Recursion, Memoization | 0 | 100.00 | 0070 |[Climbing Stairs](src/main/java/g0001_0100/s0070_climbing_stairs)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Math, Memoization, Big_O_Time_O(n)_Space_O(n) | 0 | 100.00 | 0064 |[Minimum Path Sum](src/main/java/g0001_0100/s0064_minimum_path_sum)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Matrix, Big_O_Time_O(m*n)_Space_O(m*n) | 0 | 100.00 -| 0300 |[Longest Increasing Subsequence](src/main/java/g0201_0300/s0300_longest_increasing_subsequence)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Binary_Search | 3 | 98.63 +| 0300 |[Longest Increasing Subsequence](src/main/java/g0201_0300/s0300_longest_increasing_subsequence)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Binary_Search, Big_O_Time_O(n*log_n)_Space_O(n) | 3 | 98.63 | 1143 |[Longest Common Subsequence](src/main/java/g1101_1200/s1143_longest_common_subsequence)| Medium | Top_100_Liked_Questions, String, Dynamic_Programming | 33 | 46.23 | 0072 |[Edit Distance](src/main/java/g0001_0100/s0072_edit_distance)| Hard | Top_100_Liked_Questions, String, Dynamic_Programming, Big_O_Time_O(n^2)_Space_O(n2) | 4 | 90.13 | 0044 |[Wildcard Matching](src/main/java/g0001_0100/s0044_wildcard_matching)| Hard | Top_Interview_Questions, String, Dynamic_Programming, Greedy, Recursion | 2 | 99.87 @@ -1697,7 +1697,7 @@ | 0190 |[Reverse Bits](src/main/java/g0101_0200/s0190_reverse_bits)| Easy | Top_Interview_Questions, Bit_Manipulation, Divide_and_Conquer | 1 | 98.66 | 0461 |[Hamming Distance](src/main/java/g0401_0500/s0461_hamming_distance)| Easy | Bit_Manipulation | 0 | 100.00 | 1009 |[Complement of Base 10 Integer](src/main/java/g1001_1100/s1009_complement_of_base_10_integer)| Easy | Bit_Manipulation | 1 | 41.56 -| 0338 |[Counting Bits](src/main/java/g0301_0400/s0338_counting_bits)| Easy | Top_100_Liked_Questions, Dynamic_Programming, Bit_Manipulation | 2 | 86.73 +| 0338 |[Counting Bits](src/main/java/g0301_0400/s0338_counting_bits)| Easy | Top_100_Liked_Questions, Dynamic_Programming, Bit_Manipulation, Big_O_Time_O(num)_Space_O(num) | 2 | 86.73 | 0371 |[Sum of Two Integers](src/main/java/g0301_0400/s0371_sum_of_two_integers)| Medium | Top_Interview_Questions, Math, Bit_Manipulation | 0 | 100.00 | 0029 |[Divide Two Integers](src/main/java/g0001_0100/s0029_divide_two_integers)| Medium | Top_Interview_Questions, Math, Bit_Manipulation | 1 | 97.44 @@ -1831,6 +1831,31 @@ | 2780 |[Minimum Index of a Valid Split](src/main/java/g2701_2800/s2780_minimum_index_of_a_valid_split)| Medium | Array, Hash_Table, Sorting | 5 | 99.77 | 2779 |[Maximum Beauty of an Array After Applying Operation](src/main/java/g2701_2800/s2779_maximum_beauty_of_an_array_after_applying_operation)| Medium | Array, Sorting, Binary_Search, Sliding_Window | 37 | 93.81 | 2778 |[Sum of Squares of Special Elements](src/main/java/g2701_2800/s2778_sum_of_squares_of_special_elements)| Easy | Array, Simulation | 1 | 100.00 +| 2772 |[Apply Operations to Make All Array Elements Equal to Zero](src/main/java/g2701_2800/s2772_apply_operations_to_make_all_array_elements_equal_to_zero)| Medium | Array, Prefix_Sum | 1 | 100.00 +| 2771 |[Longest Non-decreasing Subarray From Two Arrays](src/main/java/g2701_2800/s2771_longest_non_decreasing_subarray_from_two_arrays)| Medium | Array, Dynamic_Programming | 4 | 97.62 +| 2770 |[Maximum Number of Jumps to Reach the Last Index](src/main/java/g2701_2800/s2770_maximum_number_of_jumps_to_reach_the_last_index)| Medium | Array, Dynamic_Programming | 17 | 60.93 +| 2769 |[Find the Maximum Achievable Number](src/main/java/g2701_2800/s2769_find_the_maximum_achievable_number)| Easy | Math | 1 | 100.00 +| 2768 |[Number of Black Blocks](src/main/java/g2701_2800/s2768_number_of_black_blocks)| Medium | Array, Hash_Table, Enumeration | 94 | 98.91 +| 2767 |[Partition String Into Minimum Beautiful Substrings](src/main/java/g2701_2800/s2767_partition_string_into_minimum_beautiful_substrings)| Medium | String, Hash_Table, Dynamic_Programming, Backtracking | 1 | 100.00 +| 2766 |[Relocate Marbles](src/main/java/g2701_2800/s2766_relocate_marbles)| Medium | Array, Hash_Table, Sorting, Simulation | 47 | 91.67 +| 2765 |[Longest Alternating Subarray](src/main/java/g2701_2800/s2765_longest_alternating_subarray)| Easy | Array, Enumeration | 2 | 82.60 +| 2763 |[Sum of Imbalance Numbers of All Subarrays](src/main/java/g2701_2800/s2763_sum_of_imbalance_numbers_of_all_subarrays)| Hard | Array, Hash_Table, Ordered_Set | 1 | 100.00 +| 2762 |[Continuous Subarrays](src/main/java/g2701_2800/s2762_continuous_subarrays)| Medium | Array, Heap_Priority_Queue, Sliding_Window, Ordered_Set, Queue, Monotonic_Queue | 3 | 98.28 +| 2747 |[Count Zero Request Servers](src/main/java/g2701_2800/s2747_count_zero_request_servers)| Medium | Array, Hash_Table, Sorting, Sliding_Window | 43 | 76.92 +| 2746 |[Decremental String Concatenation](src/main/java/g2701_2800/s2746_decremental_string_concatenation)| Medium | Array, String, Dynamic_Programming | 34 | 85.89 +| 2745 |[Construct the Longest New String](src/main/java/g2701_2800/s2745_construct_the_longest_new_string)| Medium | Math, Greedy, Brainteaser | 1 | 100.00 +| 2744 |[Find Maximum Number of String Pairs](src/main/java/g2701_2800/s2744_find_maximum_number_of_string_pairs)| Easy | Array, String, Hash_Table, Simulation | 1 | 100.00 +| 2742 |[Painting the Walls](src/main/java/g2701_2800/s2742_painting_the_walls)| Hard | Array, Dynamic_Programming | 8 | 98.78 +| 2741 |[Special Permutations](src/main/java/g2701_2800/s2741_special_permutations)| Medium | Array, Dynamic_Programming, Bit_Manipulation, Bitmask | 105 | 96.58 +| 2740 |[Find the Value of the Partition](src/main/java/g2701_2800/s2740_find_the_value_of_the_partition)| Medium | Array, Sorting | 18 | 100.00 +| 2739 |[Total Distance Traveled](src/main/java/g2701_2800/s2739_total_distance_traveled)| Easy | Math, Simulation | 4 | 100.00 +| 2736 |[Maximum Sum Queries](src/main/java/g2701_2800/s2736_maximum_sum_queries)| Hard | Array, Sorting, Binary_Search, Stack, Monotonic_Stack, Segment_Tree, Binary_Indexed_Tree | 66 | 78.43 +| 2735 |[Collecting Chocolates](src/main/java/g2701_2800/s2735_collecting_chocolates)| Medium | Array, Enumeration | 24 | 96.97 +| 2734 |[Lexicographically Smallest String After Substring Operation](src/main/java/g2701_2800/s2734_lexicographically_smallest_string_after_substring_operation)| Medium | String, Greedy | 12 | 86.26 +| 2733 |[Neither Minimum nor Maximum](src/main/java/g2701_2800/s2733_neither_minimum_nor_maximum)| Easy | Array, Sorting | 4 | 99.76 +| 2732 |[Find a Good Subset of the Matrix](src/main/java/g2701_2800/s2732_find_a_good_subset_of_the_matrix)| Hard | Array, Greedy, Matrix, Bit_Manipulation | 7 | 70.65 +| 2731 |[Movement of Robots](src/main/java/g2701_2800/s2731_movement_of_robots)| Medium | Array, Sorting, Prefix_Sum, Brainteaser | 9 | 100.00 +| 2730 |[Find the Longest Semi-Repetitive Substring](src/main/java/g2701_2800/s2730_find_the_longest_semi_repetitive_substring)| Medium | String, Sliding_Window | 5 | 100.00 | 2729 |[Check if The Number is Fascinating](src/main/java/g2701_2800/s2729_check_if_the_number_is_fascinating)| Easy | Hash_Table, Math | 1 | 96.07 | 2727 |[Is Object Empty](src/main/java/g2701_2800/s2727_is_object_empty)| Easy || 52 | 79.26 | 2726 |[Calculator with Method Chaining](src/main/java/g2701_2800/s2726_calculator_with_method_chaining)| Easy || 39 | 99.67 @@ -1841,7 +1866,7 @@ | 2721 |[Execute Asynchronous Functions in Parallel](src/main/java/g2701_2800/s2721_execute_asynchronous_functions_in_parallel)| Medium || 67 | 84.73 | 2719 |[Count of Integers](src/main/java/g2701_2800/s2719_count_of_integers)| Hard | String, Dynamic_Programming, Math | 15 | 97.70 | 2718 |[Sum of Matrix After Queries](src/main/java/g2701_2800/s2718_sum_of_matrix_after_queries)| Medium | Array, Hash_Table | 3 | 100.00 -| 2717 |[Semi-Ordered Permutation](src/main/java/g2701_2800/s2717_semi_ordered_permutation)| Easy | String, Hash_Table | 3 | 100.00 +| 2717 |[Semi-Ordered Permutation](src/main/java/g2701_2800/s2717_semi_ordered_permutation)| Easy | Array, String, Hash_Table, Simulation | 3 | 100.00 | 2716 |[Minimize String Length](src/main/java/g2701_2800/s2716_minimize_string_length)| Easy | String, Hash_Table | 3 | 100.00 | 2715 |[Timeout Cancellation](src/main/java/g2701_2800/s2715_timeout_cancellation)| Easy || 53 | 97.68 | 2713 |[Maximum Strictly Increasing Cells in a Matrix](src/main/java/g2701_2800/s2713_maximum_strictly_increasing_cells_in_a_matrix)| Hard | Array, Dynamic_Programming, Sorting, Binary_Search, Matrix, Memoization | 116 | 96.53 @@ -3707,7 +3732,7 @@ | 0397 |[Integer Replacement](src/main/java/g0301_0400/s0397_integer_replacement)| Medium | Dynamic_Programming, Greedy, Bit_Manipulation, Memoization | 1 | 66.03 | 0396 |[Rotate Function](src/main/java/g0301_0400/s0396_rotate_function)| Medium | Array, Dynamic_Programming, Math | 4 | 81.33 | 0395 |[Longest Substring with At Least K Repeating Characters](src/main/java/g0301_0400/s0395_longest_substring_with_at_least_k_repeating_characters)| Medium | Top_Interview_Questions, String, Hash_Table, Sliding_Window, Divide_and_Conquer | 0 | 100.00 -| 0394 |[Decode String](src/main/java/g0301_0400/s0394_decode_string)| Medium | Top_100_Liked_Questions, String, Stack, Recursion, Level_1_Day_14_Stack, Udemy_Strings | 1 | 87.68 +| 0394 |[Decode String](src/main/java/g0301_0400/s0394_decode_string)| Medium | Top_100_Liked_Questions, String, Stack, Recursion, Level_1_Day_14_Stack, Udemy_Strings, Big_O_Time_O(n)_Space_O(n) | 1 | 87.68 | 0393 |[UTF-8 Validation](src/main/java/g0301_0400/s0393_utf_8_validation)| Medium | Array, Bit_Manipulation | 1 | 100.00 | 0392 |[Is Subsequence](src/main/java/g0301_0400/s0392_is_subsequence)| Easy | String, Dynamic_Programming, Two_Pointers, Dynamic_Programming_I_Day_19, Level_1_Day_2_String, Udemy_Two_Pointers | 1 | 93.01 | 0391 |[Perfect Rectangle](src/main/java/g0301_0400/s0391_perfect_rectangle)| Hard | Array, Line_Sweep | 67 | 54.55 @@ -3740,13 +3765,13 @@ | 0352 |[Data Stream as Disjoint Intervals](src/main/java/g0301_0400/s0352_data_stream_as_disjoint_intervals)| Hard | Binary_Search, Design, Ordered_Set | 109 | 62.48 | 0350 |[Intersection of Two Arrays II](src/main/java/g0301_0400/s0350_intersection_of_two_arrays_ii)| Easy | Top_Interview_Questions, Array, Hash_Table, Sorting, Binary_Search, Two_Pointers, Data_Structure_I_Day_3_Array, Binary_Search_I_Day_10 | 4 | 69.62 | 0349 |[Intersection of Two Arrays](src/main/java/g0301_0400/s0349_intersection_of_two_arrays)| Easy | Array, Hash_Table, Sorting, Binary_Search, Two_Pointers | 1 | 99.77 -| 0347 |[Top K Frequent Elements](src/main/java/g0301_0400/s0347_top_k_frequent_elements)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Sorting, Heap_Priority_Queue, Counting, Divide_and_Conquer, Quickselect, Bucket_Sort, Data_Structure_II_Day_20_Heap_Priority_Queue | 9 | 97.93 +| 0347 |[Top K Frequent Elements](src/main/java/g0301_0400/s0347_top_k_frequent_elements)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Sorting, Heap_Priority_Queue, Counting, Divide_and_Conquer, Quickselect, Bucket_Sort, Data_Structure_II_Day_20_Heap_Priority_Queue, Big_O_Time_O(n*log(n))_Space_O(k) | 9 | 97.93 | 0345 |[Reverse Vowels of a String](src/main/java/g0301_0400/s0345_reverse_vowels_of_a_string)| Easy | String, Two_Pointers | 3 | 98.02 | 0344 |[Reverse String](src/main/java/g0301_0400/s0344_reverse_string)| Easy | Top_Interview_Questions, String, Two_Pointers, Recursion, Algorithm_I_Day_4_Two_Pointers, Udemy_Strings | 1 | 99.91 | 0343 |[Integer Break](src/main/java/g0301_0400/s0343_integer_break)| Medium | Dynamic_Programming, Math, Algorithm_II_Day_18_Dynamic_Programming, Dynamic_Programming_I_Day_21 | 0 | 100.00 | 0342 |[Power of Four](src/main/java/g0301_0400/s0342_power_of_four)| Easy | Math, Bit_Manipulation, Recursion | 1 | 100.00 | 0341 |[Flatten Nested List Iterator](src/main/java/g0301_0400/s0341_flatten_nested_list_iterator)| Medium | Top_Interview_Questions, Depth_First_Search, Tree, Stack, Design, Queue, Iterator, Programming_Skills_II_Day_18 | 2 | 99.95 -| 0338 |[Counting Bits](src/main/java/g0301_0400/s0338_counting_bits)| Easy | Top_100_Liked_Questions, Dynamic_Programming, Bit_Manipulation, Udemy_Bit_Manipulation | 2 | 86.73 +| 0338 |[Counting Bits](src/main/java/g0301_0400/s0338_counting_bits)| Easy | Top_100_Liked_Questions, Dynamic_Programming, Bit_Manipulation, Udemy_Bit_Manipulation, Big_O_Time_O(num)_Space_O(num) | 2 | 86.73 | 0337 |[House Robber III](src/main/java/g0301_0400/s0337_house_robber_iii)| Medium | Dynamic_Programming, Depth_First_Search, Tree, Binary_Tree, Udemy_Tree_Stack_Queue | 1 | 91.77 | 0336 |[Palindrome Pairs](src/main/java/g0301_0400/s0336_palindrome_pairs)| Hard | Array, String, Hash_Table, Trie | 1165 | 57.51 | 0335 |[Self Crossing](src/main/java/g0301_0400/s0335_self_crossing)| Hard | Array, Math, Geometry | 5 | 79.38 @@ -3759,7 +3784,7 @@ | 0327 |[Count of Range Sum](src/main/java/g0301_0400/s0327_count_of_range_sum)| Hard | Array, Binary_Search, Ordered_Set, Divide_and_Conquer, Segment_Tree, Binary_Indexed_Tree, Merge_Sort | 111 | 66.02 | 0326 |[Power of Three](src/main/java/g0301_0400/s0326_power_of_three)| Easy | Top_Interview_Questions, Math, Recursion | 18 | 85.35 | 0324 |[Wiggle Sort II](src/main/java/g0301_0400/s0324_wiggle_sort_ii)| Medium | Top_Interview_Questions, Array, Sorting, Divide_and_Conquer, Quickselect | 4 | 93.22 -| 0322 |[Coin Change](src/main/java/g0301_0400/s0322_coin_change)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Breadth_First_Search, Algorithm_II_Day_18_Dynamic_Programming, Dynamic_Programming_I_Day_20, Level_2_Day_12_Dynamic_Programming | 17 | 91.77 +| 0322 |[Coin Change](src/main/java/g0301_0400/s0322_coin_change)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Breadth_First_Search, Algorithm_II_Day_18_Dynamic_Programming, Dynamic_Programming_I_Day_20, Level_2_Day_12_Dynamic_Programming, Big_O_Time_O(m*n)_Space_O(amount) | 17 | 91.77 | 0321 |[Create Maximum Number](src/main/java/g0301_0400/s0321_create_maximum_number)| Hard | Greedy, Stack, Monotonic_Stack | 11 | 93.20 | 0319 |[Bulb Switcher](src/main/java/g0301_0400/s0319_bulb_switcher)| Medium | Math, Brainteaser | 0 | 100.00 | 0318 |[Maximum Product of Word Lengths](src/main/java/g0301_0400/s0318_maximum_product_of_word_lengths)| Medium | Array, String, Bit_Manipulation | 12 | 74.92 @@ -3774,16 +3799,16 @@ | 0304 |[Range Sum Query 2D - Immutable](src/main/java/g0301_0400/s0304_range_sum_query_2d_immutable)| Medium | Array, Matrix, Design, Prefix_Sum, Dynamic_Programming_I_Day_14, Programming_Skills_II_Day_13, Udemy_2D_Arrays/Matrix | 153 | 87.51 | 0303 |[Range Sum Query - Immutable](src/main/java/g0301_0400/s0303_range_sum_query_immutable)| Easy | Array, Design, Prefix_Sum, Programming_Skills_I_Day_12_Class_and_Object | 7 | 100.00 | 0301 |[Remove Invalid Parentheses](src/main/java/g0301_0400/s0301_remove_invalid_parentheses)| Hard | String, Breadth_First_Search, Backtracking | 3 | 91.59 -| 0300 |[Longest Increasing Subsequence](src/main/java/g0201_0300/s0300_longest_increasing_subsequence)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Binary_Search, Algorithm_II_Day_16_Dynamic_Programming, Binary_Search_II_Day_3, Dynamic_Programming_I_Day_18, Udemy_Dynamic_Programming | 3 | 98.63 +| 0300 |[Longest Increasing Subsequence](src/main/java/g0201_0300/s0300_longest_increasing_subsequence)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Binary_Search, Algorithm_II_Day_16_Dynamic_Programming, Binary_Search_II_Day_3, Dynamic_Programming_I_Day_18, Udemy_Dynamic_Programming, Big_O_Time_O(n*log_n)_Space_O(n) | 3 | 98.63 | 0299 |[Bulls and Cows](src/main/java/g0201_0300/s0299_bulls_and_cows)| Medium | String, Hash_Table, Counting, Level_1_Day_13_Hashmap | 6 | 86.69 | 0297 |[Serialize and Deserialize Binary Tree](src/main/java/g0201_0300/s0297_serialize_and_deserialize_binary_tree)| Hard | Top_Interview_Questions, String, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Design, Data_Structure_II_Day_18_Tree, Udemy_Tree_Stack_Queue | 7 | 98.13 -| 0295 |[Find Median from Data Stream](src/main/java/g0201_0300/s0295_find_median_from_data_stream)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Sorting, Two_Pointers, Design, Heap_Priority_Queue, Data_Stream | 151 | 80.24 +| 0295 |[Find Median from Data Stream](src/main/java/g0201_0300/s0295_find_median_from_data_stream)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Sorting, Two_Pointers, Design, Heap_Priority_Queue, Data_Stream, Big_O_Time_O(n*log_n)_Space_O(n) | 151 | 80.24 | 0292 |[Nim Game](src/main/java/g0201_0300/s0292_nim_game)| Easy | Math, Game_Theory, Brainteaser | 0 | 100.00 | 0290 |[Word Pattern](src/main/java/g0201_0300/s0290_word_pattern)| Easy | String, Hash_Table, Data_Structure_II_Day_7_String | 1 | 97.26 | 0289 |[Game of Life](src/main/java/g0201_0300/s0289_game_of_life)| Medium | Top_Interview_Questions, Array, Matrix, Simulation | 0 | 100.00 -| 0287 |[Find the Duplicate Number](src/main/java/g0201_0300/s0287_find_the_duplicate_number)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Binary_Search, Two_Pointers, Bit_Manipulation, Binary_Search_II_Day_5 | 2 | 99.82 +| 0287 |[Find the Duplicate Number](src/main/java/g0201_0300/s0287_find_the_duplicate_number)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Binary_Search, Two_Pointers, Bit_Manipulation, Binary_Search_II_Day_5, Big_O_Time_O(n)_Space_O(n) | 2 | 99.82 | 0284 |[Peeking Iterator](src/main/java/g0201_0300/s0284_peeking_iterator)| Medium | Array, Design, Iterator | 6 | 72.91 -| 0283 |[Move Zeroes](src/main/java/g0201_0300/s0283_move_zeroes)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Two_Pointers, Algorithm_I_Day_3_Two_Pointers, Programming_Skills_I_Day_6_Array, Udemy_Arrays | 2 | 79.54 +| 0283 |[Move Zeroes](src/main/java/g0201_0300/s0283_move_zeroes)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Two_Pointers, Algorithm_I_Day_3_Two_Pointers, Programming_Skills_I_Day_6_Array, Udemy_Arrays, Big_O_Time_O(n)_Space_O(1) | 2 | 79.54 | 0282 |[Expression Add Operators](src/main/java/g0201_0300/s0282_expression_add_operators)| Hard | String, Math, Backtracking | 9 | 98.48 | 0279 |[Perfect Squares](src/main/java/g0201_0300/s0279_perfect_squares)| Medium | Top_Interview_Questions, Dynamic_Programming, Math, Breadth_First_Search, Dynamic_Programming_I_Day_21 | 1 | 100.00 | 0278 |[First Bad Version](src/main/java/g0201_0300/s0278_first_bad_version)| Easy | Binary_Search, Interactive, Algorithm_I_Day_1_Binary_Search, Binary_Search_I_Day_5, Level_1_Day_7_Binary_Search | 15 | 87.89 @@ -3799,9 +3824,9 @@ | 0257 |[Binary Tree Paths](src/main/java/g0201_0300/s0257_binary_tree_paths)| Easy | String, Depth_First_Search, Tree, Binary_Tree, Backtracking | 2 | 93.02 | 0242 |[Valid Anagram](src/main/java/g0201_0300/s0242_valid_anagram)| Easy | Top_Interview_Questions, String, Hash_Table, Sorting, Data_Structure_I_Day_6_String, Programming_Skills_I_Day_11_Containers_and_Libraries, Udemy_Strings | 2 | 99.01 | 0241 |[Different Ways to Add Parentheses](src/main/java/g0201_0300/s0241_different_ways_to_add_parentheses)| Medium | String, Dynamic_Programming, Math, Recursion, Memoization | 3 | 70.81 -| 0240 |[Search a 2D Matrix II](src/main/java/g0201_0300/s0240_search_a_2d_matrix_ii)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Binary_Search, Matrix, Divide_and_Conquer, Data_Structure_II_Day_4_Array, Binary_Search_II_Day_8 | 7 | 86.73 -| 0239 |[Sliding Window Maximum](src/main/java/g0201_0300/s0239_sliding_window_maximum)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Array, Heap_Priority_Queue, Sliding_Window, Queue, Monotonic_Queue, Udemy_Arrays | 58 | 52.28 -| 0238 |[Product of Array Except Self](src/main/java/g0201_0300/s0238_product_of_array_except_self)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Prefix_Sum, Data_Structure_II_Day_5_Array, Udemy_Arrays | 1 | 100.00 +| 0240 |[Search a 2D Matrix II](src/main/java/g0201_0300/s0240_search_a_2d_matrix_ii)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Binary_Search, Matrix, Divide_and_Conquer, Data_Structure_II_Day_4_Array, Binary_Search_II_Day_8, Big_O_Time_O(n+m)_Space_O(1) | 7 | 86.73 +| 0239 |[Sliding Window Maximum](src/main/java/g0201_0300/s0239_sliding_window_maximum)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Array, Heap_Priority_Queue, Sliding_Window, Queue, Monotonic_Queue, Udemy_Arrays, Big_O_Time_O(n*k)_Space_O(n+k) | 58 | 52.28 +| 0238 |[Product of Array Except Self](src/main/java/g0201_0300/s0238_product_of_array_except_self)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Prefix_Sum, Data_Structure_II_Day_5_Array, Udemy_Arrays, Big_O_Time_O(n^2)_Space_O(n) | 1 | 100.00 | 0237 |[Delete Node in a Linked List](src/main/java/g0201_0300/s0237_delete_node_in_a_linked_list)| Easy | Top_Interview_Questions, Linked_List | 0 | 100.00 | 0236 |[Lowest Common Ancestor of a Binary Tree](src/main/java/g0201_0300/s0236_lowest_common_ancestor_of_a_binary_tree)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Tree, Binary_Tree, Data_Structure_II_Day_18_Tree, Udemy_Tree_Stack_Queue, Big_O_Time_O(n)_Space_O(n) | 10 | 56.51 | 0235 |[Lowest Common Ancestor of a Binary Search Tree](src/main/java/g0201_0300/s0235_lowest_common_ancestor_of_a_binary_search_tree)| Easy | Depth_First_Search, Tree, Binary_Tree, Binary_Search_Tree, Data_Structure_I_Day_14_Tree, Level_1_Day_8_Binary_Search_Tree | 4 | 100.00 diff --git a/src/main/java/g0201_0300/s0238_product_of_array_except_self/readme.md b/src/main/java/g0201_0300/s0238_product_of_array_except_self/readme.md index 8b8831e1..0b4683ee 100644 --- a/src/main/java/g0201_0300/s0238_product_of_array_except_self/readme.md +++ b/src/main/java/g0201_0300/s0238_product_of_array_except_self/readme.md @@ -57,4 +57,22 @@ public class Solution { return ans; } } -``` \ No newline at end of file +``` + +**Time Complexity (Big O Time):** + +1. First Loop: In the first loop, the program calculates the product of all elements in the input array. This loop has a time complexity of O(n), where "n" is the length of the input array. + +2. Second Loop: In the second loop, the program calculates the product of all elements in the input array except the current element. This loop is also O(n), where "n" is the length of the input array. Inside this loop, there's another loop that multiplies all elements except the current element. So, this inner loop has a time complexity of O(n) as well. + +Overall, the time complexity of the program is dominated by the two nested loops, making it O(n^2) in the worst case. + +**Space Complexity (Big O Space):** + +1. The program uses an integer array `ans` to store the result, which has the same length as the input array. Therefore, the space complexity of the `ans` array is O(n). + +2. The program uses integer variables `product`, `p`, and `j`, which consume a constant amount of space regardless of the input size. These variables do not depend on the length of the input array. + +Therefore, the overall space complexity of the program is O(n) due to the `ans` array, where "n" is the length of the input array. + +In summary, the provided program has a time complexity of O(n^2) and a space complexity of O(n), making it inefficient for larger input arrays. You can optimize the solution to achieve a better time complexity of O(n) and a space complexity of O(1) by calculating the product of all elements in two passes without using division or extra arrays. diff --git a/src/main/java/g0201_0300/s0239_sliding_window_maximum/readme.md b/src/main/java/g0201_0300/s0239_sliding_window_maximum/readme.md index c9e6053c..e1d84710 100644 --- a/src/main/java/g0201_0300/s0239_sliding_window_maximum/readme.md +++ b/src/main/java/g0201_0300/s0239_sliding_window_maximum/readme.md @@ -87,4 +87,26 @@ public class Solution { return res; } } -``` \ No newline at end of file +``` + +**Time Complexity (Big O Time):** + +The time complexity of this program is primarily determined by the two nested loops: + +1. The outer `while` loop runs from `j = 0` to `j = n - 1`, where `n` is the length of the input array `nums`. Therefore, it has a time complexity of O(n). + +2. Inside the outer loop, there is an inner `while` loop that removes elements from the back of the deque (`dq`). In the worst case, this inner loop can run up to `k` times, where `k` is the size of the sliding window. So, the inner `while` loop contributes O(k) to the time complexity. + +Overall, the time complexity of the program is O(n * k) in the worst case, where `n` is the length of the input array, and `k` is the size of the sliding window. In practice, when `k` is much smaller than `n`, the algorithm approaches O(n). + +**Space Complexity (Big O Space):** + +1. The program uses an integer array `res` to store the results, which has a length of `n - k + 1`. Therefore, the space complexity of the `res` array is O(n). + +2. The program uses a `LinkedList` named `dq` as a deque to store elements. In the worst case, the deque can store up to `k` elements at any given time. Therefore, the space complexity of the deque is O(k). + +3. The program uses a few integer variables (`x`, `i`, `j`) that consume a constant amount of space regardless of the input size. These variables do not depend on the length of the input array. + +Overall, the space complexity of the program is O(n + k), where `n` is the length of the input array, and `k` is the size of the sliding window. In practice, when `k` is much smaller than `n`, the space complexity approaches O(n). + +In summary, the provided program has a time complexity of O(n * k) and a space complexity of O(n + k), but in practice, it can be more efficient when `k` is small compared to `n`. diff --git a/src/main/java/g0201_0300/s0240_search_a_2d_matrix_ii/readme.md b/src/main/java/g0201_0300/s0240_search_a_2d_matrix_ii/readme.md index 30da02dc..886602d4 100644 --- a/src/main/java/g0201_0300/s0240_search_a_2d_matrix_ii/readme.md +++ b/src/main/java/g0201_0300/s0240_search_a_2d_matrix_ii/readme.md @@ -55,4 +55,18 @@ public class Solution { return false; } } -``` \ No newline at end of file +``` + +**Time Complexity (Big O Time):** + +The time complexity of this program is primarily determined by the `while` loop. In the worst case, the `while` loop iterates until either `r` exceeds the number of rows (`matrix.length`) or `c` becomes negative. The loop condition ensures that we move either left (decreasing `c`) or down (increasing `r`) in each iteration. Since each iteration reduces the search space by one row or one column, the number of iterations in the worst case will be at most `n + m`, where `n` is the number of rows and `m` is the number of columns in the matrix. + +Therefore, the time complexity of the program is O(n + m), where `n` is the number of rows and `m` is the number of columns in the matrix. + +**Space Complexity (Big O Space):** + +The program uses a few integer variables (`r`, `c`, `target`) that consume a constant amount of space regardless of the input size. These variables do not depend on the dimensions of the matrix. + +Therefore, the space complexity of the program is O(1), which indicates constant space usage. + +In summary, the provided program has a time complexity of O(n + m), where `n` is the number of rows and `m` is the number of columns in the matrix, and it has a space complexity of O(1), indicating constant space usage. diff --git a/src/main/java/g0201_0300/s0283_move_zeroes/readme.md b/src/main/java/g0201_0300/s0283_move_zeroes/readme.md index 68c42631..35bf5338 100644 --- a/src/main/java/g0201_0300/s0283_move_zeroes/readme.md +++ b/src/main/java/g0201_0300/s0283_move_zeroes/readme.md @@ -48,4 +48,14 @@ public class Solution { numbers[index1] = val2; } } -``` \ No newline at end of file +``` + +**Time Complexity (Big O Time):** + +The time complexity of this program is O(n), where 'n' is the length of the input array `nums`. This is because the program iterates through the entire array exactly once in a single pass. The loop runs 'n' times, and inside the loop, there are constant-time operations such as checking and swapping elements. + +**Space Complexity (Big O Space):** + +The space complexity of the program is O(1), which indicates constant space usage. Regardless of the size of the input array, the program only uses a constant amount of extra space to store integer variables (`firstZero`, `i`, and `val2`) and does not use any additional data structures whose space requirements depend on the input size. The space used for these variables remains constant. + +In summary, the provided program has a time complexity of O(n), where 'n' is the length of the input array `nums`, and it has a space complexity of O(1), indicating constant space usage. diff --git a/src/main/java/g0201_0300/s0287_find_the_duplicate_number/readme.md b/src/main/java/g0201_0300/s0287_find_the_duplicate_number/readme.md index f45d52ae..ba4082cd 100644 --- a/src/main/java/g0201_0300/s0287_find_the_duplicate_number/readme.md +++ b/src/main/java/g0201_0300/s0287_find_the_duplicate_number/readme.md @@ -62,4 +62,14 @@ public class Solution { return 0; } } -``` \ No newline at end of file +``` + +**Time Complexity (Big O Time):** + +The time complexity of this program is O(n), where 'n' is the length of the input array `nums`. This is because the program iterates through the entire array exactly once in a single pass. In the worst case, it might need to iterate through the entire array to find the duplicate. + +**Space Complexity (Big O Space):** + +The space complexity of the program is O(n), where 'n' is the length of the input array `nums`. The program creates an integer array `arr` of size `nums.length + 1`, which requires additional space. The size of this array is directly proportional to the size of the input array `nums`. + +In summary, the provided program has a time complexity of O(n), where 'n' is the length of the input array `nums`, and it has a space complexity of O(n), indicating linear space usage. diff --git a/src/main/java/g0201_0300/s0295_find_median_from_data_stream/readme.md b/src/main/java/g0201_0300/s0295_find_median_from_data_stream/readme.md index 84a4d181..f57ac1bd 100644 --- a/src/main/java/g0201_0300/s0295_find_median_from_data_stream/readme.md +++ b/src/main/java/g0201_0300/s0295_find_median_from_data_stream/readme.md @@ -97,4 +97,20 @@ public class MedianFinder { * obj.addNum(num); * double param_2 = obj.findMedian(); */ -``` \ No newline at end of file +``` + +**Time Complexity (Big O Time):** + +1. The `addNum` method inserts an element into one of the two priority queues (maxHeap or minHeap) and balances the heaps if necessary. The balancing operation has a time complexity of O(log n), where 'n' is the number of elements in the larger heap. Since the elements are inserted one by one, the total time complexity for 'n' insertions is O(n * log n). + +2. The `findMedian` method retrieves the median based on the sizes of the two heaps, which takes constant time O(1). + +Overall, the time complexity of the program is O(n * log n) for 'n' insertions, where 'n' is the number of elements inserted. + +**Space Complexity (Big O Space):** + +1. The program uses two priority queues (`maxHeap` and `minHeap`) to store elements. The space complexity is determined by the space required for these priority queues. In the worst case, both heaps can store up to n/2 elements each, where 'n' is the number of elements inserted. + +2. Therefore, the space complexity of the program is O(n/2 + n/2) = O(n). + +In summary, the provided program has a time complexity of O(n * log n) for 'n' insertions and a space complexity of O(n) due to the storage of elements in two priority queues. diff --git a/src/main/java/g0201_0300/s0300_longest_increasing_subsequence/readme.md b/src/main/java/g0201_0300/s0300_longest_increasing_subsequence/readme.md index 265f7bb3..803ca9f8 100644 --- a/src/main/java/g0201_0300/s0300_longest_increasing_subsequence/readme.md +++ b/src/main/java/g0201_0300/s0300_longest_increasing_subsequence/readme.md @@ -76,4 +76,17 @@ public class Solution { return right; } } -``` \ No newline at end of file +``` + +**Time Complexity (Big O Time):** + +1. The program uses dynamic programming to find the LIS. It iterates through the input array `nums`, which has 'n' elements. +2. Inside the loop, a binary search is performed on the `dp` array to find the appropriate position to update. Binary search has a time complexity of O(log n). +3. The binary search is performed for each element in `nums`, so the total time complexity for the binary search part is O(n * log n). +4. The loop also includes constant-time operations, so the overall time complexity of the program is O(n * log n). + +**Space Complexity (Big O Space):** + +1. The program uses an additional integer array `dp` of length `nums.length + 1` to store intermediate results. Therefore, the space complexity is O(n) for the `dp` array. + +In summary, the provided program has a time complexity of O(n * log n) and a space complexity of O(n) due to the dynamic programming array `dp`. diff --git a/src/main/java/g0301_0400/s0322_coin_change/readme.md b/src/main/java/g0301_0400/s0322_coin_change/readme.md index d35c01bb..56dde556 100644 --- a/src/main/java/g0301_0400/s0322_coin_change/readme.md +++ b/src/main/java/g0301_0400/s0322_coin_change/readme.md @@ -59,4 +59,17 @@ public class Solution { return dp[amount] - 1; } } -``` \ No newline at end of file +``` + +**Time Complexity (Big O Time):** + +1. The program uses dynamic programming to compute the minimum number of coins needed for each possible amount from 0 to `amount`. +2. It iterates through the `coins` array, which has 'm' elements, and for each coin, it iterates from the coin value to `amount`, which has 'n' values. +3. Inside the inner loop, there is a constant-time operation to update the `dp` array. +4. Therefore, the overall time complexity of the program is O(m * n), where 'm' is the number of coin denominations, and 'n' is the given `amount`. + +**Space Complexity (Big O Space):** + +1. The program uses an integer array `dp` of size `amount + 1` to store the minimum number of coins needed for each amount from 0 to `amount`. Therefore, the space complexity is O(amount). + +In summary, the provided program has a time complexity of O(m * n) and a space complexity of O(amount), where 'm' is the number of coin denominations, 'n' is the given `amount`, and the space complexity depends on the value of `amount`. diff --git a/src/main/java/g0301_0400/s0338_counting_bits/readme.md b/src/main/java/g0301_0400/s0338_counting_bits/readme.md index 0fecb4fa..805f0757 100644 --- a/src/main/java/g0301_0400/s0338_counting_bits/readme.md +++ b/src/main/java/g0301_0400/s0338_counting_bits/readme.md @@ -64,4 +64,20 @@ public class Solution { return result; } } -``` \ No newline at end of file +``` + +**Time Complexity (Big O Time):** + +The program iterates through numbers from 1 to `num` and calculates the number of 1s in each number's binary representation. The key part is that it efficiently reuses previously calculated results for smaller numbers. + +1. The outer loop runs `num` times, where `num` is the input integer. +2. The inner loop is based on the value of `borderPos`, which is reset every time `incrPos` reaches a power of 2 (e.g., 1, 2, 4, 8, ...). +3. Inside the inner loop, there is a constant-time operation to update the `result` array. + +Therefore, the overall time complexity of the program is O(num), where 'num' is the input integer. + +**Space Complexity (Big O Space):** + +1. The program uses an integer array `result` of size `num + 1` to store the count of 1s for each number from 0 to `num`. Therefore, the space complexity is O(num). + +In summary, the provided program has a time complexity of O(num) and a space complexity of O(num), where 'num' is the input integer. diff --git a/src/main/java/g0301_0400/s0347_top_k_frequent_elements/readme.md b/src/main/java/g0301_0400/s0347_top_k_frequent_elements/readme.md index dbc172cf..ca014427 100644 --- a/src/main/java/g0301_0400/s0347_top_k_frequent_elements/readme.md +++ b/src/main/java/g0301_0400/s0347_top_k_frequent_elements/readme.md @@ -58,4 +58,23 @@ public class Solution { return result; } } -``` \ No newline at end of file +``` + +**Time Complexity (Big O Time):** + +1. Sorting the `nums` array using `Arrays.sort(nums)` takes O(n*log(n)), where 'n' is the length of the input array. +2. The program then iterates through the sorted array 'nums' once. Inside the loop, it checks for distinct elements and counts their frequencies. This loop takes O(n) time. +3. The program uses a priority queue (`queue`) to keep track of the top k frequent elements. Adding an element to the priority queue takes O(log(k)) time, and it's done for each unique element in the input array. +4. After the loop, the program constructs the result array by extracting elements from the priority queue. This step takes O(k*log(k)), as it involves removing k elements from the priority queue. + +Considering the dominant operations, the overall time complexity of the program is O(n*log(n)) due to the sorting step. + +**Space Complexity (Big O Space):** + +1. The program uses a priority queue (`queue`) to store the distinct elements along with their frequencies. The maximum size of this priority queue can be 'k + 1' (to ensure 'k' elements are retained at most). +2. The program uses an integer array `result` of size 'k' to store the top k frequent elements. +3. Other than these data structures, the space complexity is dominated by constant space used for variables and indices. + +Therefore, the overall space complexity of the program is O(k) because the priority queue dominates the space complexity, and 'k' is the maximum size it can have. + +In summary, the provided program has a time complexity of O(n*log(n)) and a space complexity of O(k), where 'n' is the length of the input array, and 'k' is the value of 'k' specified as an argument. diff --git a/src/main/java/g0301_0400/s0394_decode_string/readme.md b/src/main/java/g0301_0400/s0394_decode_string/readme.md index f34c7635..593ec29e 100644 --- a/src/main/java/g0301_0400/s0394_decode_string/readme.md +++ b/src/main/java/g0301_0400/s0394_decode_string/readme.md @@ -74,4 +74,32 @@ public class Solution { return sb.toString(); } } -``` \ No newline at end of file +``` + +**Time Complexity (Big O Time):** + +The time complexity of the program depends on the length of the input string 's' and the number of times it encounters square brackets ('[' and ']'). Here's a breakdown of the operations: + +1. The program iterates through the input string 's' character by character, visiting each character exactly once. This operation takes O(n) time, where 'n' is the length of the input string. + +2. When the program encounters a digit character, it starts building a count value by parsing the numeric part of a potential repeat count. This process also takes O(n) time since each digit character is processed once. + +3. When the program encounters a square bracket ('['), it recursively calls the `decodeString` function to process the substring inside the brackets. The number of recursive calls depends on the nesting level of brackets and the length of substrings inside the brackets. + +4. In each recursive call, the program processes the substring inside the brackets and appends it to the result StringBuilder. The maximum number of iterations inside the recursion is proportional to the count value, which can be at most the length of the input string 's'. Therefore, the recursive part takes O(n) time in total. + +Combining all these factors, the overall time complexity of the program is O(n), where 'n' is the length of the input string 's'. + +**Space Complexity (Big O Space):** + +1. The program uses a StringBuilder `sb` to store the decoded string. The space complexity of the StringBuilder is O(n) because it can grow to accommodate the entire input string 's' in the worst case. + +2. The program uses an integer variable `count` to store the repeat count for a substring inside square brackets. This variable requires constant space, O(1). + +3. The program uses an integer variable `i` for tracking the current position in the input string. This variable also requires constant space, O(1). + +4. The program uses recursion to process substrings inside square brackets. The maximum depth of recursion depends on the nesting level of brackets in the input string. In the worst case, if there are 'k' levels of nesting, the space complexity due to recursion is O(k). + +Overall, the dominant factor in the space complexity is the StringBuilder `sb`, which can grow up to O(n) in size. Therefore, the space complexity of the program is O(n). + +In summary, the provided program has a time complexity of O(n) and a space complexity of O(n), where 'n' is the length of the input string 's'. diff --git a/src/main/java/g2701_2800/s2730_find_the_longest_semi_repetitive_substring/readme.md b/src/main/java/g2701_2800/s2730_find_the_longest_semi_repetitive_substring/readme.md new file mode 100644 index 00000000..21611326 --- /dev/null +++ b/src/main/java/g2701_2800/s2730_find_the_longest_semi_repetitive_substring/readme.md @@ -0,0 +1,62 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Java?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Java) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Java?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Java/fork) + +## 2730\. Find the Longest Semi-Repetitive Substring + +Medium + +You are given a **0-indexed** string `s` that consists of digits from `0` to `9`. + +A string `t` is called a **semi-repetitive** if there is at most one consecutive pair of the same digits inside `t`. For example, `0010`, `002020`, `0123`, `2002`, and `54944` are semi-repetitive while `00101022`, and `1101234883` are not. + +Return _the length of the longest semi-repetitive substring inside_ `s`. + +A **substring** is a contiguous **non-empty** sequence of characters within a string. + +**Example 1:** + +**Input:** s = "52233" + +**Output:** 4 + +**Explanation:** The longest semi-repetitive substring is "5223", which starts at i = 0 and ends at j = 3. + +**Example 2:** + +**Input:** s = "5494" + +**Output:** 4 + +**Explanation:** s is a semi-reptitive string, so the answer is 4. + +**Example 3:** + +**Input:** s = "1111111" + +**Output:** 2 + +**Explanation:** The longest semi-repetitive substring is "11", which starts at i = 0 and ends at j = 1. + +**Constraints:** + +* `1 <= s.length <= 50` +* `'0' <= s[i] <= '9'` + +## Solution + +```java +public class Solution { + public int longestSemiRepetitiveSubstring(String s) { + int i = 0; + int cur = 0; + int n = s.length(); + for (int j = 1; j < n; j++) { + cur += (s.charAt(j) == s.charAt(j - 1)) ? 1 : 0; + if (cur > 1) { + cur -= (s.charAt(++i) == s.charAt(i - 1)) ? 1 : 0; + } + } + return n - i; + } +} +``` \ No newline at end of file diff --git a/src/main/java/g2701_2800/s2731_movement_of_robots/readme.md b/src/main/java/g2701_2800/s2731_movement_of_robots/readme.md new file mode 100644 index 00000000..d93f8728 --- /dev/null +++ b/src/main/java/g2701_2800/s2731_movement_of_robots/readme.md @@ -0,0 +1,89 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Java?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Java) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Java?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Java/fork) + +## 2731\. Movement of Robots + +Medium + +Some robots are standing on an infinite number line with their initial coordinates given by a **0-indexed** integer array `nums` and will start moving once given the command to move. The robots will move a unit distance each second. + +You are given a string `s` denoting the direction in which robots will move on command. `'L'` means the robot will move towards the left side or negative side of the number line, whereas `'R'` means the robot will move towards the right side or positive side of the number line. + +If two robots collide, they will start moving in opposite directions. + +Return _the sum of distances between all the pairs of robots_ `d` _seconds after the command._ Since the sum can be very large, return it modulo 109 + 7. + +**Note:** + +* For two robots at the index `i` and `j`, pair `(i,j)` and pair `(j,i)` are considered the same pair. +* When robots collide, they **instantly change** their directions without wasting any time. +* Collision happens when two robots share the same place in a moment. + * For example, if a robot is positioned in 0 going to the right and another is positioned in 2 going to the left, the next second they'll be both in 1 and they will change direction and the next second the first one will be in 0, heading left, and another will be in 2, heading right. + * For example, if a robot is positioned in 0 going to the right and another is positioned in 1 going to the left, the next second the first one will be in 0, heading left, and another will be in 1, heading right. + +**Example 1:** + +**Input:** nums = [-2,0,2], s = "RLL", d = 3 + +**Output:** 8 + +**Explanation:** + +After 1 second, the positions are [-1,-1,1]. Now, the robot at index 0 will move left, and the robot at index 1 will move right. + +After 2 seconds, the positions are [-2,0,0]. Now, the robot at index 1 will move left, and the robot at index 2 will move right. + +After 3 seconds, the positions are [-3,-1,1]. + +The distance between the robot at index 0 and 1 is abs(-3 - (-1)) = 2. + +The distance between the robot at index 0 and 2 is abs(-3 - 1) = 4. + +The distance between the robot at index 1 and 2 is abs(-1 - 1) = 2. + +The sum of the pairs of all distances = 2 + 4 + 2 = 8. + +**Example 2:** + +**Input:** nums = [1,0], s = "RL", d = 2 + +**Output:** 5 + +**Explanation:** + +After 1 second, the positions are [2,-1]. + +After 2 seconds, the positions are [3,-2]. + +The distance between the two robots is abs(-2 - 3) = 5. + +**Constraints:** + +* 2 <= nums.length <= 105 +* -2 * 109 <= nums[i] <= 2 * 109 +* 0 <= d <= 109 +* `nums.length == s.length` +* `s` consists of 'L' and 'R' only +* `nums[i]` will be unique. + +## Solution + +```java +import java.util.Arrays; + +public class Solution { + public int sumDistance(int[] nums, String s, int d) { + int n = nums.length; + int mod = (int) (1e9 + 7); + for (int i = 0; i < n; i++) { + nums[i] += (s.charAt(i) == 'R') ? d : -d; + } + Arrays.sort(nums); + long res = 0; + for (int i = 0; i < n; i++) { + res = (res + (1L + i + i - n) * nums[i]) % mod; + } + return (int) ((res + mod) % mod); + } +} +``` \ No newline at end of file diff --git a/src/main/java/g2701_2800/s2732_find_a_good_subset_of_the_matrix/readme.md b/src/main/java/g2701_2800/s2732_find_a_good_subset_of_the_matrix/readme.md new file mode 100644 index 00000000..34e236d2 --- /dev/null +++ b/src/main/java/g2701_2800/s2732_find_a_good_subset_of_the_matrix/readme.md @@ -0,0 +1,105 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Java?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Java) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Java?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Java/fork) + +## 2732\. Find a Good Subset of the Matrix + +Hard + +You are given a **0-indexed** `m x n` binary matrix `grid`. + +Let us call a **non-empty** subset of rows **good** if the sum of each column of the subset is at most half of the length of the subset. + +More formally, if the length of the chosen subset of rows is `k`, then the sum of each column should be at most `floor(k / 2)`. + +Return _an integer array that contains row indices of a good subset sorted in **ascending** order._ + +If there are multiple good subsets, you can return any of them. If there are no good subsets, return an empty array. + +A **subset** of rows of the matrix `grid` is any matrix that can be obtained by deleting some (possibly none or all) rows from `grid`. + +**Example 1:** + +**Input:** grid = \[\[0,1,1,0],[0,0,0,1],[1,1,1,1]] + +**Output:** [0,1] + +**Explanation:** We can choose the 0th and 1st rows to create a good subset of rows. The length of the chosen subset is 2. +- The sum of the 0th column is 0 + 0 = 0, which is at most half of the length of the subset. +- The sum of the 1st column is 1 + 0 = 1, which is at most half of the length of the subset. +- The sum of the 2nd column is 1 + 0 = 1, which is at most half of the length of the subset. +- The sum of the 3rd column is 0 + 1 = 1, which is at most half of the length of the subset. + +**Example 2:** + +**Input:** grid = \[\[0]] + +**Output:** [0] + +**Explanation:** We can choose the 0th row to create a good subset of rows. The length of the chosen subset is 1. +- The sum of the 0th column is 0, which is at most half of the length of the subset. + +**Example 3:** + +**Input:** grid = \[\[1,1,1],[1,1,1]] + +**Output:** [] + +**Explanation:** It is impossible to choose any subset of rows to create a good subset. + +**Constraints:** + +* `m == grid.length` +* `n == grid[i].length` +* 1 <= m <= 104 +* `1 <= n <= 5` +* `grid[i][j]` is either `0` or `1`. + +## Solution + +```java +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class Solution { + public List goodSubsetofBinaryMatrix(int[][] grid) { + int m = grid.length; + int n = grid[0].length; + if (m == 1 && sumArray(grid[0]) == 0) { + return List.of(0); + } + Map pos = new HashMap<>(); + for (int i = 0; i < m; i++) { + for (int mask = 0; mask < (1 << n); mask++) { + boolean valid = true; + for (int j = 0; j < n; j++) { + if ((mask & (1 << j)) != 0 && grid[i][j] + 1 > 1) { + valid = false; + break; + } + } + if (valid && pos.containsKey(mask)) { + return List.of(pos.get(mask), i); + } + } + int curr = 0; + for (int j = 0; j < n; j++) { + if (grid[i][j] == 1) { + curr = curr | (1 << j); + } + } + pos.put(curr, i); + } + return new ArrayList<>(); + } + + private int sumArray(int[] arr) { + int sum = 0; + for (int num : arr) { + sum += num; + } + return sum; + } +} +``` \ No newline at end of file diff --git a/src/main/java/g2701_2800/s2733_neither_minimum_nor_maximum/readme.md b/src/main/java/g2701_2800/s2733_neither_minimum_nor_maximum/readme.md new file mode 100644 index 00000000..0e1cdc3c --- /dev/null +++ b/src/main/java/g2701_2800/s2733_neither_minimum_nor_maximum/readme.md @@ -0,0 +1,61 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Java?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Java) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Java?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Java/fork) + +## 2733\. Neither Minimum nor Maximum + +Easy + +Given an integer array `nums` containing **distinct** **positive** integers, find and return **any** number from the array that is neither the **minimum** nor the **maximum** value in the array, or **`-1`** if there is no such number. + +Return _the selected integer._ + +**Example 1:** + +**Input:** nums = [3,2,1,4] + +**Output:** 2 + +**Explanation:** In this example, the minimum value is 1 and the maximum value is 4. Therefore, either 2 or 3 can be valid answers. + +**Example 2:** + +**Input:** nums = [1,2] + +**Output:** -1 + +**Explanation:** Since there is no number in nums that is neither the maximum nor the minimum, we cannot select a number that satisfies the given condition. Therefore, there is no answer. + +**Example 3:** + +**Input:** nums = [2,1,3] + +**Output:** 2 + +**Explanation:** Since 2 is neither the maximum nor the minimum value in nums, it is the only valid answer. + +**Constraints:** + +* `1 <= nums.length <= 100` +* `1 <= nums[i] <= 100` +* All values in `nums` are distinct + +## Solution + +```java +public class Solution { + public int findNonMinOrMax(int[] nums) { + int mn = 999; + int mx = -1; + for (int num : nums) { + mn = Math.min(num, mn); + mx = Math.max(num, mx); + } + for (int num : nums) { + if (num != mn && num != mx) { + return num; + } + } + return -1; + } +} +``` \ No newline at end of file diff --git a/src/main/java/g2701_2800/s2734_lexicographically_smallest_string_after_substring_operation/readme.md b/src/main/java/g2701_2800/s2734_lexicographically_smallest_string_after_substring_operation/readme.md new file mode 100644 index 00000000..1db4f9b4 --- /dev/null +++ b/src/main/java/g2701_2800/s2734_lexicographically_smallest_string_after_substring_operation/readme.md @@ -0,0 +1,67 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Java?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Java) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Java?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Java/fork) + +## 2734\. Lexicographically Smallest String After Substring Operation + +Medium + +You are given a string `s` consisting of only lowercase English letters. In one operation, you can do the following: + +* Select any non-empty substring of `s`, possibly the entire string, then replace each one of its characters with the previous character of the English alphabet. For example, 'b' is converted to 'a', and 'a' is converted to 'z'. + +Return _the **lexicographically smallest** string you can obtain after performing the above operation **exactly once**._ + +A **substring** is a contiguous sequence of characters in a string. + +A string `x` is **lexicographically smaller** than a string `y` of the same length if `x[i]` comes before `y[i]` in alphabetic order for the first position `i` such that `x[i] != y[i]`. + +**Example 1:** + +**Input:** s = "cbabc" + +**Output:** "baabc" + +**Explanation:** We apply the operation on the substring starting at index 0, and ending at index 1 inclusive. It can be proven that the resulting string is the lexicographically smallest. + +**Example 2:** + +**Input:** s = "acbbc" + +**Output:** "abaab" + +**Explanation:** We apply the operation on the substring starting at index 1, and ending at index 4 inclusive. It can be proven that the resulting string is the lexicographically smallest. + +**Example 3:** + +**Input:** s = "leetcode" + +**Output:** "kddsbncd" + +**Explanation:** We apply the operation on the entire string. It can be proven that the resulting string is the lexicographically smallest. + +**Constraints:** + +* 1 <= s.length <= 3 * 105 +* `s` consists of lowercase English letters + +## Solution + +```java +public class Solution { + public String smallestString(String s) { + int i = 0; + int n = s.length(); + char[] a = s.toCharArray(); + while (i < n && a[i] == 'a') { + i++; + if (i == n) { + a[n - 1] = 'z'; + } + } + while (i < n && a[i] != 'a') { + --a[i++]; + } + return String.valueOf(a); + } +} +``` \ No newline at end of file diff --git a/src/main/java/g2701_2800/s2735_collecting_chocolates/readme.md b/src/main/java/g2701_2800/s2735_collecting_chocolates/readme.md new file mode 100644 index 00000000..6c618ab0 --- /dev/null +++ b/src/main/java/g2701_2800/s2735_collecting_chocolates/readme.md @@ -0,0 +1,68 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Java?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Java) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Java?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Java/fork) + +## 2735\. Collecting Chocolates + +Medium + +You are given a **0-indexed** integer array `nums` of size `n` representing the cost of collecting different chocolates. The cost of collecting the chocolate at the index `i` is `nums[i]`. Each chocolate is of a different type, and initially, the chocolate at the index `i` is of ith type. + +In one operation, you can do the following with an incurred **cost** of `x`: + +* Simultaneously change the chocolate of ith type to ((i + 1) mod n)th type for all chocolates. + +Return _the minimum cost to collect chocolates of all types, given that you can perform as many operations as you would like._ + +**Example 1:** + +**Input:** nums = [20,1,15], x = 5 + +**Output:** 13 + +**Explanation:** Initially, the chocolate types are [0,1,2]. We will buy the 1st type of chocolate at a cost of 1. + +Now, we will perform the operation at a cost of 5, and the types of chocolates will become [1,2,0]. We will buy the 2nd type of chocolate at a cost of 1. + +Now, we will again perform the operation at a cost of 5, and the chocolate types will become [2,0,1]. We will buy the 0th type of chocolate at a cost of 1. + +Thus, the total cost will become (1 + 5 + 1 + 5 + 1) = 13. We can prove that this is optimal. + +**Example 2:** + +**Input:** nums = [1,2,3], x = 4 + +**Output:** 6 + +**Explanation:** We will collect all three types of chocolates at their own price without performing any operations. Therefore, the total cost is 1 + 2 + 3 = 6. + +**Constraints:** + +* `1 <= nums.length <= 1000` +* 1 <= nums[i] <= 109 +* 1 <= x <= 109 + +## Solution + +```java +public class Solution { + public long minCost(int[] nums, int x) { + int n = nums.length; + int[] dp = new int[n]; + long res = 0; + for (int i = 0; i < n; i++) { + dp[i] = nums[i]; + res += nums[i]; + } + for (int i = 1; i < n; i++) { + long sum = (long) i * x; + for (int j = 0; j < n; j++) { + int currIndex = (j + i >= n) ? j + i - n : j + i; + dp[j] = Math.min(dp[j], nums[currIndex]); + sum += dp[j]; + } + res = Math.min(res, sum); + } + return res; + } +} +``` \ No newline at end of file diff --git a/src/main/java/g2701_2800/s2736_maximum_sum_queries/readme.md b/src/main/java/g2701_2800/s2736_maximum_sum_queries/readme.md new file mode 100644 index 00000000..1d71a4ee --- /dev/null +++ b/src/main/java/g2701_2800/s2736_maximum_sum_queries/readme.md @@ -0,0 +1,117 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Java?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Java) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Java?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Java/fork) + +## 2736\. Maximum Sum Queries + +Hard + +You are given two **0-indexed** integer arrays `nums1` and `nums2`, each of length `n`, and a **1-indexed 2D array** `queries` where queries[i] = [xi, yi]. + +For the ith query, find the **maximum value** of `nums1[j] + nums2[j]` among all indices `j` `(0 <= j < n)`, where nums1[j] >= xi and nums2[j] >= yi, or **\-1** if there is no `j` satisfying the constraints. + +Return _an array_ `answer` _where_ `answer[i]` _is the answer to the_ ith _query._ + +**Example 1:** + +**Input:** nums1 = [4,3,1,2], nums2 = [2,4,9,5], queries = \[\[4,1],[1,3],[2,5]] + +**Output:** [6,10,7] + +**Explanation:** + +For the 1st query xi = 4 and yi = 1, we can select index `j = 0` since `nums1[j] >= 4` and `nums2[j] >= 1`. The sum `nums1[j] + nums2[j]` is 6, and we can show that 6 is the maximum we can obtain. + +For the 2nd query xi = 1 and yi = 3, we can select index `j = 2` since `nums1[j] >= 1` and `nums2[j] >= 3`. The sum `nums1[j] + nums2[j]` is 10, and we can show that 10 is the maximum we can obtain. + +For the 3rd query xi = 2 and yi = 5, we can select index `j = 3` since `nums1[j] >= 2` and `nums2[j] >= 5`. The sum `nums1[j] + nums2[j]` is 7, and we can show that 7 is the maximum we can obtain. + +Therefore, we return `[6,10,7]`. + +**Example 2:** + +**Input:** nums1 = [3,2,5], nums2 = [2,3,4], queries = \[\[4,4],[3,2],[1,1]] + +**Output:** [9,9,9] + +**Explanation:** For this example, we can use index `j = 2` for all the queries since it satisfies the constraints for each query. + +**Example 3:** + +**Input:** nums1 = [2,1], nums2 = [2,3], queries = \[\[3,3]] + +**Output:** [-1] + +**Explanation:** There is one query in this example with xi = 3 and yi = 3. For every index, j, either nums1[j] < xi or nums2[j] < yi. Hence, there is no solution. + +**Constraints:** + +* `nums1.length == nums2.length` +* `n == nums1.length` +* 1 <= n <= 105 +* 1 <= nums1[i], nums2[i] <= 109 +* 1 <= queries.length <= 105 +* `queries[i].length == 2` +* xi == queries[i][1] +* yi == queries[i][2] +* 1 <= xi, yi <= 109 + +## Solution + +```java +import java.util.ArrayList; +import java.util.Comparator; +import java.util.List; +import java.util.Map; +import java.util.NavigableMap; +import java.util.TreeMap; + +public class Solution { + private void update(NavigableMap map, int num, int sum) { + Map.Entry entry = map.floorEntry(num); + while (entry != null && entry.getValue() <= sum) { + map.remove(entry.getKey()); + int x = entry.getKey(); + entry = map.floorEntry(x); + } + entry = map.ceilingEntry(num); + if (entry == null || entry.getValue() < sum) { + map.put(num, sum); + } + } + + private int queryVal(NavigableMap map, int num) { + Map.Entry entry = map.ceilingEntry(num); + if (entry == null) { + return -1; + } + return entry.getValue(); + } + + public int[] maximumSumQueries(int[] nums1, int[] nums2, int[][] queries) { + int n = nums1.length; + int m = queries.length; + List v = new ArrayList<>(); + for (int i = 0; i < n; i++) { + v.add(new int[] {nums1[i], nums2[i]}); + } + v.sort(Comparator.comparingInt(a -> a[0])); + List ind = new ArrayList<>(); + for (int i = 0; i < m; i++) { + ind.add(i); + } + ind.sort((a, b) -> queries[b][0] - queries[a][0]); + TreeMap values = new TreeMap<>(); + int j = n - 1; + int[] ans = new int[m]; + for (int i : ind) { + int a = queries[i][0]; + int b = queries[i][1]; + for (; j >= 0 && v.get(j)[0] >= a; j--) { + update(values, v.get(j)[1], v.get(j)[0] + v.get(j)[1]); + } + ans[i] = queryVal(values, b); + } + return ans; + } +} +``` \ No newline at end of file diff --git a/src/main/java/g2701_2800/s2739_total_distance_traveled/readme.md b/src/main/java/g2701_2800/s2739_total_distance_traveled/readme.md new file mode 100644 index 00000000..85435394 --- /dev/null +++ b/src/main/java/g2701_2800/s2739_total_distance_traveled/readme.md @@ -0,0 +1,52 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Java?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Java) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Java?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Java/fork) + +## 2739\. Total Distance Traveled + +Easy + +A truck has two fuel tanks. You are given two integers, `mainTank` representing the fuel present in the main tank in liters and `additionalTank` representing the fuel present in the additional tank in liters. + +The truck has a mileage of `10` km per liter. Whenever `5` liters of fuel get used up in the main tank, if the additional tank has at least `1` liters of fuel, `1` liters of fuel will be transferred from the additional tank to the main tank. + +Return _the maximum distance which can be traveled._ + +**Note:** Injection from the additional tank is not continuous. It happens suddenly and immediately for every 5 liters consumed. + +**Example 1:** + +**Input:** mainTank = 5, additionalTank = 10 + +**Output:** 60 + +**Explanation:** + +After spending 5 litre of fuel, fuel remaining is (5 - 5 + 1) = 1 litre and distance traveled is 50km. + +After spending another 1 litre of fuel, no fuel gets injected in the main tank and the main tank becomes empty. + +Total distance traveled is 60km. + +**Example 2:** + +**Input:** mainTank = 1, additionalTank = 2 + +**Output:** 10 + +**Explanation:** After spending 1 litre of fuel, the main tank becomes empty. Total distance traveled is 10km. + +**Constraints:** + +* `1 <= mainTank, additionalTank <= 100` + +## Solution + +```java +public class Solution { + public int distanceTraveled(int mainTank, int additionalTank) { + int transferableTimes = (mainTank - 1) / 4; + int transferredLiters = Math.min(transferableTimes, additionalTank); + return (mainTank + transferredLiters) * 10; + } +} +``` \ No newline at end of file diff --git a/src/main/java/g2701_2800/s2740_find_the_value_of_the_partition/readme.md b/src/main/java/g2701_2800/s2740_find_the_value_of_the_partition/readme.md new file mode 100644 index 00000000..e19a6b98 --- /dev/null +++ b/src/main/java/g2701_2800/s2740_find_the_value_of_the_partition/readme.md @@ -0,0 +1,73 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Java?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Java) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Java?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Java/fork) + +## 2740\. Find the Value of the Partition + +Medium + +You are given a **positive** integer array `nums`. + +Partition `nums` into two arrays, `nums1` and `nums2`, such that: + +* Each element of the array `nums` belongs to either the array `nums1` or the array `nums2`. +* Both arrays are **non-empty**. +* The value of the partition is **minimized**. + +The value of the partition is `|max(nums1) - min(nums2)|`. + +Here, `max(nums1)` denotes the maximum element of the array `nums1`, and `min(nums2)` denotes the minimum element of the array `nums2`. + +Return _the integer denoting the value of such partition_. + +**Example 1:** + +**Input:** nums = [1,3,2,4] + +**Output:** 1 + +**Explanation:** We can partition the array nums into nums1 = [1,2] and nums2 = [3,4]. +- The maximum element of the array nums1 is equal to 2. +- The minimum element of the array nums2 is equal to 3. + +The value of the partition is \|2 - 3\| = 1. + +It can be proven that 1 is the minimum value out of all partitions. + +**Example 2:** + +**Input:** nums = [100,1,10] + +**Output:** 9 + +**Explanation:** We can partition the array nums into nums1 = [10] and nums2 = [100,1]. +- The maximum element of the array nums1 is equal to 10. +- The minimum element of the array nums2 is equal to 1. + +The value of the partition is \|10 - 1\| = 9. + +It can be proven that 9 is the minimum value out of all partitions. + +**Constraints:** + +* 2 <= nums.length <= 105 +* 1 <= nums[i] <= 109 + +## Solution + +```java +import java.util.Arrays; + +public class Solution { + public int findValueOfPartition(int[] nums) { + Arrays.sort(nums); + int minDifference = Integer.MAX_VALUE; + for (int i = 1; i < nums.length; i++) { + int difference = nums[i] - nums[i - 1]; + if (difference < minDifference) { + minDifference = difference; + } + } + return minDifference; + } +} +``` \ No newline at end of file diff --git a/src/main/java/g2701_2800/s2741_special_permutations/readme.md b/src/main/java/g2701_2800/s2741_special_permutations/readme.md new file mode 100644 index 00000000..3d4ed367 --- /dev/null +++ b/src/main/java/g2701_2800/s2741_special_permutations/readme.md @@ -0,0 +1,71 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Java?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Java) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Java?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Java/fork) + +## 2741\. Special Permutations + +Medium + +You are given a **0-indexed** integer array `nums` containing `n` **distinct** positive integers. A permutation of `nums` is called special if: + +* For all indexes `0 <= i < n - 1`, either `nums[i] % nums[i+1] == 0` or `nums[i+1] % nums[i] == 0`. + +Return _the total number of special permutations. _As the answer could be large, return it **modulo **109 + 7. + +**Example 1:** + +**Input:** nums = [2,3,6] + +**Output:** 2 + +**Explanation:** [3,6,2] and [2,6,3] are the two special permutations of nums. + +**Example 2:** + +**Input:** nums = [1,4,3] + +**Output:** 2 + +**Explanation:** [3,1,4] and [4,1,3] are the two special permutations of nums. + +**Constraints:** + +* `2 <= nums.length <= 14` +* 1 <= nums[i] <= 109 + +## Solution + +```java +public class Solution { + private int n; + private Integer[][] memo; + private int[] nums; + + public int specialPerm(int[] nums) { + this.n = nums.length; + this.memo = new Integer[n][1 << n]; + this.nums = nums; + return backtrack(0, 0); + } + + private int backtrack(int preIndex, int mask) { + if (mask == (1 << n) - 1) { + return 1; + } + if (memo[preIndex][mask] != null) { + return memo[preIndex][mask]; + } + int count = 0; + int mod = (int) 1e9 + 7; + for (int i = 0; i < n; i++) { + if ((mask & (1 << i)) == 0 + && (mask == 0 + || nums[i] % nums[preIndex] == 0 + || nums[preIndex] % nums[i] == 0)) { + count = (count + backtrack(i, mask | (1 << i))) % mod; + } + } + memo[preIndex][mask] = count; + return memo[preIndex][mask]; + } +} +``` \ No newline at end of file diff --git a/src/main/java/g2701_2800/s2742_painting_the_walls/readme.md b/src/main/java/g2701_2800/s2742_painting_the_walls/readme.md new file mode 100644 index 00000000..b25ed01c --- /dev/null +++ b/src/main/java/g2701_2800/s2742_painting_the_walls/readme.md @@ -0,0 +1,59 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Java?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Java) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Java?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Java/fork) + +## 2742\. Painting the Walls + +Hard + +You are given two **0-indexed** integer arrays, `cost` and `time`, of size `n` representing the costs and the time taken to paint `n` different walls respectively. There are two painters available: + +* A** paid painter** that paints the ith wall in `time[i]` units of time and takes `cost[i]` units of money. +* A** free painter** that paints **any** wall in `1` unit of time at a cost of `0`. But the free painter can only be used if the paid painter is already **occupied**. + +Return _the minimum amount of money required to paint the_ `n` _walls._ + +**Example 1:** + +**Input:** cost = [1,2,3,2], time = [1,2,3,2] + +**Output:** 3 + +**Explanation:** The walls at index 0 and 1 will be painted by the paid painter, and it will take 3 units of time; meanwhile, the free painter will paint the walls at index 2 and 3, free of cost in 2 units of time. Thus, the total cost is 1 + 2 = 3. + +**Example 2:** + +**Input:** cost = [2,3,4,2], time = [1,1,1,1] + +**Output:** 4 + +**Explanation:** The walls at index 0 and 3 will be painted by the paid painter, and it will take 2 units of time; meanwhile, the free painter will paint the walls at index 1 and 2, free of cost in 2 units of time. Thus, the total cost is 2 + 2 = 4. + +**Constraints:** + +* `1 <= cost.length <= 500` +* `cost.length == time.length` +* 1 <= cost[i] <= 106 +* `1 <= time[i] <= 500` + +## Solution + +```java +import java.util.Arrays; + +public class Solution { + public int paintWalls(int[] cost, int[] time) { + int n = cost.length; + int[] dp = new int[n + 1]; + Arrays.fill(dp, (int) 1e9); + dp[0] = 0; + + for (int i = 0; i < n; ++i) { + for (int j = n; j > 0; --j) { + dp[j] = Math.min(dp[j], dp[Math.max(j - time[i] - 1, 0)] + cost[i]); + } + } + + return dp[n]; + } +} +``` \ No newline at end of file diff --git a/src/main/java/g2701_2800/s2744_find_maximum_number_of_string_pairs/readme.md b/src/main/java/g2701_2800/s2744_find_maximum_number_of_string_pairs/readme.md new file mode 100644 index 00000000..c64fcdd5 --- /dev/null +++ b/src/main/java/g2701_2800/s2744_find_maximum_number_of_string_pairs/readme.md @@ -0,0 +1,88 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Java?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Java) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Java?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Java/fork) + +## 2744\. Find Maximum Number of String Pairs + +Easy + +You are given a **0-indexed** array `words` consisting of **distinct** strings. + +The string `words[i]` can be paired with the string `words[j]` if: + +* The string `words[i]` is equal to the reversed string of `words[j]`. +* `0 <= i < j < words.length`. + +Return _the **maximum** number of pairs that can be formed from the array_ `words`_._ + +Note that each string can belong in **at most one** pair. + +**Example 1:** + +**Input:** words = ["cd","ac","dc","ca","zz"] + +**Output:** 2 + +**Explanation:** In this example, we can form 2 pair of strings in the following way: +- We pair the 0th string with the 2nd string, as the reversed string of word[0] is "dc" and is equal to words[2]. +- We pair the 1st string with the 3rd string, as the reversed string of word[1] is "ca" and is equal to words[3]. + +It can be proven that 2 is the maximum number of pairs that can be formed. + +**Example 2:** + +**Input:** words = ["ab","ba","cc"] + +**Output:** 1 + +**Explanation:** In this example, we can form 1 pair of strings in the following way: +- We pair the 0th string with the 1st string, as the reversed string of words[1] is "ab" and is equal to words[0]. + +It can be proven that 1 is the maximum number of pairs that can be formed. + +**Example 3:** + +**Input:** words = ["aa","ab"] + +**Output:** 0 + +**Explanation:** In this example, we are unable to form any pair of strings. + +**Constraints:** + +* `1 <= words.length <= 50` +* `words[i].length == 2` +* `words` consists of distinct strings. +* `words[i]` contains only lowercase English letters. + +## Solution + +```java +public class Solution { + private boolean func(String a, String b) { + int n = a.length(); + int m = b.length(); + if (n != m) { + return false; + } + for (int i = 0; i < n; i++) { + if (a.charAt(i) != b.charAt(n - 1 - i)) { + return false; + } + } + return true; + } + + public int maximumNumberOfStringPairs(String[] words) { + int ans = 0; + int n = words.length; + for (int i = 0; i < n - 1; i++) { + for (int j = i + 1; j < n; j++) { + if (func(words[i], words[j])) { + ans++; + } + } + } + return ans; + } +} +``` \ No newline at end of file diff --git a/src/main/java/g2701_2800/s2745_construct_the_longest_new_string/readme.md b/src/main/java/g2701_2800/s2745_construct_the_longest_new_string/readme.md new file mode 100644 index 00000000..7e1866e5 --- /dev/null +++ b/src/main/java/g2701_2800/s2745_construct_the_longest_new_string/readme.md @@ -0,0 +1,51 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Java?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Java) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Java?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Java/fork) + +## 2745\. Construct the Longest New String + +Medium + +You are given three integers `x`, `y`, and `z`. + +You have `x` strings equal to `"AA"`, `y` strings equal to `"BB"`, and `z` strings equal to `"AB"`. You want to choose some (possibly all or none) of these strings and concactenate them in some order to form a new string. This new string must not contain `"AAA"` or `"BBB"` as a substring. + +Return _the maximum possible length of the new string_. + +A **substring** is a contiguous **non-empty** sequence of characters within a string. + +**Example 1:** + +**Input:** x = 2, y = 5, z = 1 + +**Output:** 12 + +**Explanation:** We can concactenate the strings "BB", "AA", "BB", "AA", "BB", and "AB" in that order. Then, our new string is "BBAABBAABBAB". That string has length 12, and we can show that it is impossible to construct a string of longer length. + +**Example 2:** + +**Input:** x = 3, y = 2, z = 2 + +**Output:** 14 + +**Explanation:** We can concactenate the strings "AB", "AB", "AA", "BB", "AA", "BB", and "AA" in that order. Then, our new string is "ABABAABBAABBAA". That string has length 14, and we can show that it is impossible to construct a string of longer length. + +**Constraints:** + +* `1 <= x, y, z <= 50` + +## Solution + +```java +public class Solution { + public int longestString(int x, int y, int z) { + int min = Math.min(x, y); + int res = 0; + if (x == y) { + res = 2 * min + z; + } else { + res = 2 * min + 1 + z; + } + return res * 2; + } +} +``` \ No newline at end of file diff --git a/src/main/java/g2701_2800/s2746_decremental_string_concatenation/readme.md b/src/main/java/g2701_2800/s2746_decremental_string_concatenation/readme.md new file mode 100644 index 00000000..aa9c4e25 --- /dev/null +++ b/src/main/java/g2701_2800/s2746_decremental_string_concatenation/readme.md @@ -0,0 +1,111 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Java?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Java) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Java?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Java/fork) + +## 2746\. Decremental String Concatenation + +Medium + +You are given a **0-indexed** array `words` containing `n` strings. + +Let's define a **join** operation `join(x, y)` between two strings `x` and `y` as concatenating them into `xy`. However, if the last character of `x` is equal to the first character of `y`, one of them is **deleted**. + +For example `join("ab", "ba") = "aba"` and `join("ab", "cde") = "abcde"`. + +You are to perform `n - 1` **join** operations. Let str0 = words[0]. Starting from `i = 1` up to `i = n - 1`, for the ith operation, you can do one of the following: + +* Make stri = join(stri - 1, words[i]) +* Make stri = join(words[i], stri - 1) + +Your task is to **minimize** the length of strn - 1. + +Return _an integer denoting the minimum possible length of_ strn - 1. + +**Example 1:** + +**Input:** words = ["aa","ab","bc"] + +**Output:** 4 + +**Explanation:** In this example, we can perform join operations in the following order to minimize the length of str2: + +str0 = "aa" + +str1 = join(str0, "ab") = "aab" + +str2 = join(str1, "bc") = "aabc" + +It can be shown that the minimum possible length of str2 is 4. + +**Example 2:** + +**Input:** words = ["ab","b"] + +**Output:** 2 + +**Explanation:** In this example, str0 = "ab", there are two ways to get str1: join(str0, "b") = "ab" or join("b", str0) = "bab". The first string, "ab", has the minimum length. Hence, the answer is 2. + +**Example 3:** + +**Input:** words = ["aaa","c","aba"] + +**Output:** 6 + +**Explanation:** In this example, we can perform join operations in the following order to minimize the length of str2: + +str0 = "aaa" + +str1 = join(str0, "c") = "aaac" + +str2 = join("aba", str1) = "abaaac" + +It can be shown that the minimum possible length of str2 is 6. + +**Constraints:** + +* `1 <= words.length <= 1000` +* `1 <= words[i].length <= 50` +* Each character in `words[i]` is an English lowercase letter + +## Solution + +```java +public class Solution { + private Integer[][][] dp; + + public int minimizeConcatenatedLength(String[] words) { + int n = words.length; + dp = new Integer[n][26][26]; + String curWord = words[0]; + int curLen = curWord.length(); + char curFirst = curWord.charAt(0); + char curLast = curWord.charAt(curLen - 1); + return curLen + solve(1, curFirst, curLast, n, words); + } + + private int solve(int idx, char prevFirst, char prevLast, int n, String[] words) { + if (idx == n) { + return 0; + } + if (dp[idx][prevFirst - 'a'][prevLast - 'a'] != null) { + return dp[idx][prevFirst - 'a'][prevLast - 'a']; + } + String curWord = words[idx]; + int curLen = curWord.length(); + char curFirst = curWord.charAt(0); + char curLast = curWord.charAt(curLen - 1); + int ans = (int) 1e9; + if (prevFirst == curLast) { + ans = Math.min(ans, curLen - 1 + solve(idx + 1, curFirst, prevLast, n, words)); + } else { + ans = Math.min(ans, curLen + solve(idx + 1, curFirst, prevLast, n, words)); + } + if (prevLast == curFirst) { + ans = Math.min(ans, curLen - 1 + solve(idx + 1, prevFirst, curLast, n, words)); + } else { + ans = Math.min(ans, curLen + solve(idx + 1, prevFirst, curLast, n, words)); + } + dp[idx][prevFirst - 'a'][prevLast - 'a'] = ans; + return ans; + } +} +``` \ No newline at end of file diff --git a/src/main/java/g2701_2800/s2747_count_zero_request_servers/readme.md b/src/main/java/g2701_2800/s2747_count_zero_request_servers/readme.md new file mode 100644 index 00000000..8bd38b5c --- /dev/null +++ b/src/main/java/g2701_2800/s2747_count_zero_request_servers/readme.md @@ -0,0 +1,90 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Java?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Java) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Java?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Java/fork) + +## 2747\. Count Zero Request Servers + +Medium + +You are given an integer `n` denoting the total number of servers and a **2D** **0-indexed** integer array `logs`, where `logs[i] = [server_id, time]` denotes that the server with id `server_id` received a request at time `time`. + +You are also given an integer `x` and a **0-indexed** integer array `queries`. + +Return _a **0-indexed** integer array_ `arr` _of length_ `queries.length` _where_ `arr[i]` _represents the number of servers that **did not receive** any requests during the time interval_ `[queries[i] - x, queries[i]]`. + +Note that the time intervals are inclusive. + +**Example 1:** + +**Input:** n = 3, logs = \[\[1,3],[2,6],[1,5]], x = 5, queries = [10,11] + +**Output:** [1,2] + +**Explanation:** + +For queries[0]: The servers with ids 1 and 2 get requests in the duration of [5, 10]. + +Hence, only server 3 gets zero requests. + +For queries[1]: Only the server with id 2 gets a request in duration of [6,11]. Hence, the servers with ids 1 and 3 are the only servers that do not receive any requests during that time period. + +**Example 2:** + +**Input:** n = 3, logs = \[\[2,4],[2,1],[1,2],[3,1]], x = 2, queries = [3,4] + +**Output:** [0,1] + +**Explanation:** + +For queries[0]: All servers get at least one request in the duration of [1, 3]. + +For queries[1]: Only server with id 3 gets no request in the duration [2,4]. + +**Constraints:** + +* 1 <= n <= 105 +* 1 <= logs.length <= 105 +* 1 <= queries.length <= 105 +* `logs[i].length == 2` +* `1 <= logs[i][0] <= n` +* 1 <= logs[i][1] <= 106 +* 1 <= x <= 105 +* x < queries[i] <= 106 + +## Solution + +```java +import java.util.Arrays; +import java.util.Comparator; +import java.util.HashMap; + +public class Solution { + public int[] countServers(int n, int[][] logs, int x, int[] qs) { + int m = qs.length; + var valIdx = new int[m][2]; + for (int i = 0; i < m; i++) { + valIdx[i] = new int[] {qs[i], i}; + } + Arrays.sort(valIdx, Comparator.comparingInt(a -> a[0])); + Arrays.sort(logs, Comparator.comparingInt(a -> a[1])); + int l = 0; + int r = 0; + var res = new int[m]; + var servCount = new HashMap(); + for (var q : valIdx) { + int rVal = q[0]; + int lVal = q[0] - x; + int i = q[1]; + while (r < logs.length && logs[r][1] <= rVal) { + servCount.merge(logs[r++][0], 1, Integer::sum); + } + while (l < r && logs[l][1] < lVal) { + servCount.compute(logs[l][0], (k, v) -> v - 1); + servCount.remove(logs[l][0], 0); + l++; + } + res[i] = n - servCount.size(); + } + return res; + } +} +``` \ No newline at end of file diff --git a/src/main/java/g2701_2800/s2762_continuous_subarrays/readme.md b/src/main/java/g2701_2800/s2762_continuous_subarrays/readme.md new file mode 100644 index 00000000..a5adee31 --- /dev/null +++ b/src/main/java/g2701_2800/s2762_continuous_subarrays/readme.md @@ -0,0 +1,86 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Java?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Java) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Java?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Java/fork) + +## 2762\. Continuous Subarrays + +Medium + +You are given a **0-indexed** integer array `nums`. A subarray of `nums` is called **continuous** if: + +* Let `i`, `i + 1`, ..., `j` be the indices in the subarray. Then, for each pair of indices i <= i1, i2 <= j, 0 <= |nums[i1] - nums[i2]| <= 2. + +Return _the total number of **continuous** subarrays._ + +A subarray is a contiguous **non-empty** sequence of elements within an array. + +**Example 1:** + +**Input:** nums = [5,4,2,4] + +**Output:** 8 + +**Explanation:** + +Continuous subarray of size 1: [5], [4], [2], [4]. + +Continuous subarray of size 2: [5,4], [4,2], [2,4]. + +Continuous subarray of size 3: [4,2,4]. + +Thereare no subarrys of size 4. + +Total continuous subarrays = 4 + 3 + 1 = 8. + +It can be shown that there are no more continuous subarrays. + +**Example 2:** + +**Input:** nums = [1,2,3] + +**Output:** 6 + +**Explanation:** + +Continuous subarray of size 1: [1], [2], [3]. + +Continuous subarray of size 2: [1,2], [2,3]. + +Continuous subarray of size 3: [1,2,3]. + +Total continuous subarrays = 3 + 2 + 1 = 6. + +**Constraints:** + +* 1 <= nums.length <= 105 +* 1 <= nums[i] <= 109 + +## Solution + +```java +public class Solution { + public long continuousSubarrays(int[] nums) { + long res = 1; + int lower = nums[0] - 2; + int higher = nums[0] + 2; + int j = 0; + for (int i = 1; i < nums.length; i++) { + if (nums[i] >= lower && nums[i] <= higher) { + lower = Math.max(lower, nums[i] - 2); + higher = Math.min(higher, nums[i] + 2); + } else { + j = i - 1; + lower = nums[i] - 2; + higher = nums[i] + 2; + while (j >= 0 && nums[j] >= lower && nums[j] <= higher) { + lower = Math.max(lower, nums[j] - 2); + higher = Math.min(higher, nums[j] + 2); + j--; + } + j++; + } + res += i - j + 1; + } + return res; + } +} +``` \ No newline at end of file diff --git a/src/main/java/g2701_2800/s2763_sum_of_imbalance_numbers_of_all_subarrays/readme.md b/src/main/java/g2701_2800/s2763_sum_of_imbalance_numbers_of_all_subarrays/readme.md new file mode 100644 index 00000000..46ca9b9e --- /dev/null +++ b/src/main/java/g2701_2800/s2763_sum_of_imbalance_numbers_of_all_subarrays/readme.md @@ -0,0 +1,78 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Java?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Java) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Java?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Java/fork) + +## 2763\. Sum of Imbalance Numbers of All Subarrays + +Hard + +The **imbalance number** of a **0-indexed** integer array `arr` of length `n` is defined as the number of indices in `sarr = sorted(arr)` such that: + +* `0 <= i < n - 1`, and +* `sarr[i+1] - sarr[i] > 1` + +Here, `sorted(arr)` is the function that returns the sorted version of `arr`. + +Given a **0-indexed** integer array `nums`, return _the **sum of imbalance numbers** of all its **subarrays**_. + +A **subarray** is a contiguous **non-empty** sequence of elements within an array. + +**Example 1:** + +**Input:** nums = [2,3,1,4] + +**Output:** 3 + +**Explanation:** There are 3 subarrays with non-zero imbalance numbers: +- Subarray [3, 1] with an imbalance number of 1. +- Subarray [3, 1, 4] with an imbalance number of 1. +- Subarray [1, 4] with an imbalance number of 1. + +The imbalance number of all other subarrays is 0. Hence, the sum of imbalance numbers of all the subarrays of nums is 3. + +**Example 2:** + +**Input:** nums = [1,3,3,3,5] + +**Output:** 8 + +**Explanation:** There are 7 subarrays with non-zero imbalance numbers: +- Subarray [1, 3] with an imbalance number of 1. +- Subarray [1, 3, 3] with an imbalance number of 1. +- Subarray [1, 3, 3, 3] with an imbalance number of 1. +- Subarray [1, 3, 3, 3, 5] with an imbalance number of 2. +- Subarray [3, 3, 3, 5] with an imbalance number of 1. +- Subarray [3, 3, 5] with an imbalance number of 1. +- Subarray [3, 5] with an imbalance number of 1. + +The imbalance number of all other subarrays is 0. Hence, the sum of imbalance numbers of all the subarrays of nums is 8. + +**Constraints:** + +* `1 <= nums.length <= 1000` +* `1 <= nums[i] <= nums.length` + +## Solution + +```java +import java.util.Arrays; + +public class Solution { + public int sumImbalanceNumbers(int[] nums) { + int n = nums.length; + int s = 0; + int[] left = new int[n]; + int[] seen = new int[n + 2]; + Arrays.fill(seen, -1); + for (int i = 0; i < n; i++) { + left[i] = Math.max(seen[nums[i]], seen[nums[i] + 1]); + seen[nums[i]] = i; + } + Arrays.fill(seen, n); + for (int i = n - 1; i >= 0; i--) { + s += (seen[nums[i] + 1] - i) * (i - left[i]); + seen[nums[i]] = i; + } + return s - (n + 1) * n / 2; + } +} +``` \ No newline at end of file diff --git a/src/main/java/g2701_2800/s2765_longest_alternating_subarray/readme.md b/src/main/java/g2701_2800/s2765_longest_alternating_subarray/readme.md new file mode 100644 index 00000000..d42624da --- /dev/null +++ b/src/main/java/g2701_2800/s2765_longest_alternating_subarray/readme.md @@ -0,0 +1,69 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Java?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Java) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Java?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Java/fork) + +## 2765\. Longest Alternating Subarray + +Easy + +You are given a **0-indexed** integer array `nums`. A subarray `s` of length `m` is called **alternating** if: + +* `m` is greater than `1`. +* s1 = s0 + 1. +* The **0-indexed** subarray `s` looks like [s0, s1, s0, s1,...,s(m-1) % 2]. In other words, s1 - s0 = 1, s2 - s1 = -1, s3 - s2 = 1, s4 - s3 = -1, and so on up to s[m - 1] - s[m - 2] = (-1)m. + +Return _the maximum length of all **alternating** subarrays present in_ `nums` _or_ `-1` _if no such subarray exists__._ + +A subarray is a contiguous **non-empty** sequence of elements within an array. + +**Example 1:** + +**Input:** nums = [2,3,4,3,4] + +**Output:** 4 + +**Explanation:** The alternating subarrays are [3,4], [3,4,3], and [3,4,3,4]. The longest of these is [3,4,3,4], which is of length 4. + +**Example 2:** + +**Input:** nums = [4,5,6] + +**Output:** 2 + +**Explanation:** [4,5] and [5,6] are the only two alternating subarrays. They are both of length 2. + +**Constraints:** + +* `2 <= nums.length <= 100` +* 1 <= nums[i] <= 104 + +## Solution + +```java +@SuppressWarnings("java:S135") +public class Solution { + public int alternatingSubarray(int[] nums) { + int result = -1; + int previous = 0; + int sum = 1; + for (int i = 1; i < nums.length; i++) { + int diff = nums[i] - nums[i - 1]; + if (Math.abs(diff) != 1) { + sum = 1; + continue; + } + if (diff == previous) { + sum = 2; + } + if (diff != previous) { + if (diff != (sum % 2 == 0 ? -1 : 1)) { + continue; + } + sum++; + previous = diff; + } + result = Math.max(result, sum); + } + return result; + } +} +``` \ No newline at end of file diff --git a/src/main/java/g2701_2800/s2766_relocate_marbles/readme.md b/src/main/java/g2701_2800/s2766_relocate_marbles/readme.md new file mode 100644 index 00000000..25dc4fe0 --- /dev/null +++ b/src/main/java/g2701_2800/s2766_relocate_marbles/readme.md @@ -0,0 +1,82 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Java?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Java) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Java?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Java/fork) + +## 2766\. Relocate Marbles + +Medium + +You are given a **0-indexed** integer array `nums` representing the initial positions of some marbles. You are also given two **0-indexed** integer arrays `moveFrom` and `moveTo` of **equal** length. + +Throughout `moveFrom.length` steps, you will change the positions of the marbles. On the ith step, you will move **all** marbles at position `moveFrom[i]` to position `moveTo[i]`. + +After completing all the steps, return _the sorted list of **occupied** positions_. + +**Notes:** + +* We call a position **occupied** if there is at least one marble in that position. +* There may be multiple marbles in a single position. + +**Example 1:** + +**Input:** nums = [1,6,7,8], moveFrom = [1,7,2], moveTo = [2,9,5] + +**Output:** [5,6,8,9] + +**Explanation:** Initially, the marbles are at positions 1,6,7,8. + +At the i = 0th step, we move the marbles at position 1 to position 2. Then, positions 2,6,7,8 are occupied. + +At the i = 1st step, we move the marbles at position 7 to position 9. Then, positions 2,6,8,9 are occupied. + +At the i = 2nd step, we move the marbles at position 2 to position 5. Then, positions 5,6,8,9 are occupied. + +At the end, the final positions containing at least one marbles are [5,6,8,9]. + +**Example 2:** + +**Input:** nums = [1,1,3,3], moveFrom = [1,3], moveTo = [2,2] + +**Output:** [2] + +**Explanation:** Initially, the marbles are at positions [1,1,3,3]. + +At the i = 0th step, we move all the marbles at position 1 to position 2. Then, the marbles are at positions [2,2,3,3]. + +At the i = 1st step, we move all the marbles at position 3 to position 2. Then, the marbles are at positions [2,2,2,2]. + +Since 2 is the only occupied position, we return [2]. + +**Constraints:** + +* 1 <= nums.length <= 105 +* 1 <= moveFrom.length <= 105 +* `moveFrom.length == moveTo.length` +* 1 <= nums[i], moveFrom[i], moveTo[i] <= 109 +* The test cases are generated such that there is at least a marble in `moveFrom[i]` at the moment we want to apply the ith move. + +## Solution + +```java +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +public class Solution { + public List relocateMarbles(int[] nums, int[] moveFrom, int[] moveTo) { + Set set = new HashSet<>(); + for (int num : nums) { + set.add(num); + } + for (int i = 0; i < moveTo.length; i++) { + if (set.contains(moveFrom[i])) { + set.remove(moveFrom[i]); + set.add(moveTo[i]); + } + } + List result = new ArrayList<>(set); + result.sort(null); + return result; + } +} +``` \ No newline at end of file diff --git a/src/main/java/g2701_2800/s2767_partition_string_into_minimum_beautiful_substrings/readme.md b/src/main/java/g2701_2800/s2767_partition_string_into_minimum_beautiful_substrings/readme.md new file mode 100644 index 00000000..8069e678 --- /dev/null +++ b/src/main/java/g2701_2800/s2767_partition_string_into_minimum_beautiful_substrings/readme.md @@ -0,0 +1,96 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Java?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Java) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Java?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Java/fork) + +## 2767\. Partition String Into Minimum Beautiful Substrings + +Medium + +Given a binary string `s`, partition the string into one or more **substrings** such that each substring is **beautiful**. + +A string is **beautiful** if: + +* It doesn't contain leading zeros. +* It's the **binary** representation of a number that is a power of `5`. + +Return _the **minimum** number of substrings in such partition._ If it is impossible to partition the string `s` into beautiful substrings, return `-1`. + +A **substring** is a contiguous sequence of characters in a string. + +**Example 1:** + +**Input:** s = "1011" + +**Output:** 2 + +**Explanation:** We can paritition the given string into ["101", "1"]. +- The string "101" does not contain leading zeros and is the binary representation of integer 51 = 5. +- The string "1" does not contain leading zeros and is the binary representation of integer 50 = 1. + +It can be shown that 2 is the minimum number of beautiful substrings that s can be partitioned into. + +**Example 2:** + +**Input:** s = "111" + +**Output:** 3 + +**Explanation:** We can paritition the given string into ["1", "1", "1"]. +- The string "1" does not contain leading zeros and is the binary representation of integer 50 = 1. + +It can be shown that 3 is the minimum number of beautiful substrings that s can be partitioned into. + +**Example 3:** + +**Input:** s = "0" + +**Output:** -1 + +**Explanation:** We can not partition the given string into beautiful substrings. + +**Constraints:** + +* `1 <= s.length <= 15` +* `s[i]` is either `'0'` or `'1'`. + +## Solution + +```java +public class Solution { + private boolean ispower(int num) { + int pow = 1; + while (pow < num) { + pow = pow * 5; + } + return pow == num; + } + + private int backtrack(int ind, String s) { + if (ind >= s.length()) { + return 0; + } + if (s.charAt(ind) == '0') { + return 999; + } + int temp = 999; + int runCount = 0; + for (int i = ind; i < s.length(); i++) { + runCount = runCount * 2; + if (s.charAt(i) == '1') { + runCount++; + } + if (this.ispower(runCount)) { + temp = Math.min(temp, 1 + this.backtrack(i + 1, s)); + } + } + return temp; + } + + public int minimumBeautifulSubstrings(String s) { + int res = this.backtrack(0, s); + if (res < 999) { + return res; + } + return -1; + } +} +``` \ No newline at end of file diff --git a/src/main/java/g2701_2800/s2768_number_of_black_blocks/readme.md b/src/main/java/g2701_2800/s2768_number_of_black_blocks/readme.md new file mode 100644 index 00000000..5fdedeaa --- /dev/null +++ b/src/main/java/g2701_2800/s2768_number_of_black_blocks/readme.md @@ -0,0 +1,83 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Java?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Java) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Java?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Java/fork) + +## 2768\. Number of Black Blocks + +Medium + +You are given two integers `m` and `n` representing the dimensions of a **0-indexed** `m x n` grid. + +You are also given a **0-indexed** 2D integer matrix `coordinates`, where `coordinates[i] = [x, y]` indicates that the cell with coordinates `[x, y]` is colored **black**. All cells in the grid that do not appear in `coordinates` are **white**. + +A block is defined as a `2 x 2` submatrix of the grid. More formally, a block with cell `[x, y]` as its top-left corner where `0 <= x < m - 1` and `0 <= y < n - 1` contains the coordinates `[x, y]`, `[x + 1, y]`, `[x, y + 1]`, and `[x + 1, y + 1]`. + +Return _a **0-indexed** integer array_ `arr` _of size_ `5` _such that_ `arr[i]` _is the number of blocks that contains exactly_ `i` _**black** cells_. + +**Example 1:** + +**Input:** m = 3, n = 3, coordinates = \[\[0,0]] + +**Output:** [3,1,0,0,0] + +**Explanation:** The grid looks like this: ![](https://assets.leetcode.com/uploads/2023/06/18/screen-shot-2023-06-18-at-44656-am.png) + +There is only 1 block with one black cell, and it is the block starting with cell [0,0]. + +The other 3 blocks start with cells [0,1], [1,0] and [1,1]. They all have zero black cells. + +Thus, we return [3,1,0,0,0]. + +**Example 2:** + +**Input:** m = 3, n = 3, coordinates = \[\[0,0],[1,1],[0,2]] + +**Output:** [0,2,2,0,0] + +**Explanation:** The grid looks like this: ![](https://assets.leetcode.com/uploads/2023/06/18/screen-shot-2023-06-18-at-45018-am.png) + +There are 2 blocks with two black cells (the ones starting with cell coordinates [0,0] and [0,1]). + +The other 2 blocks have starting cell coordinates of [1,0] and [1,1]. They both have 1 black cell. + +Therefore, we return [0,2,2,0,0]. + +**Constraints:** + +* 2 <= m <= 105 +* 2 <= n <= 105 +* 0 <= coordinates.length <= 104 +* `coordinates[i].length == 2` +* `0 <= coordinates[i][0] < m` +* `0 <= coordinates[i][1] < n` +* It is guaranteed that `coordinates` contains pairwise distinct coordinates. + +## Solution + +```java +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; + +public class Solution { + public long[] countBlackBlocks(int m, int n, int[][] coordinates) { + long[] ans = new long[5]; + Map count = new HashMap<>(); + for (int[] coordinate : coordinates) { + int x = coordinate[0]; + int y = coordinate[1]; + for (int i = x; i < x + 2; i++) { + for (int j = y; j < y + 2; j++) { + if (i - 1 >= 0 && i < m && j - 1 >= 0 && j < n) { + count.merge(i * n + j, 1, (a, b) -> a + b); + } + } + } + } + for (int freq : count.values()) { + ans[freq]++; + } + ans[0] = (m - 1L) * (n - 1) - Arrays.stream(ans).sum(); + return ans; + } +} +``` \ No newline at end of file diff --git a/src/main/java/g2701_2800/s2769_find_the_maximum_achievable_number/readme.md b/src/main/java/g2701_2800/s2769_find_the_maximum_achievable_number/readme.md new file mode 100644 index 00000000..dad54b62 --- /dev/null +++ b/src/main/java/g2701_2800/s2769_find_the_maximum_achievable_number/readme.md @@ -0,0 +1,58 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Java?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Java) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Java?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Java/fork) + +## 2769\. Find the Maximum Achievable Number + +Easy + +You are given two integers, `num` and `t`. + +An integer `x` is called **achievable** if it can become equal to `num` after applying the following operation no more than `t` times: + +* Increase or decrease `x` by `1`, and simultaneously increase or decrease `num` by `1`. + +Return _the maximum possible achievable number_. It can be proven that there exists at least one achievable number. + +**Example 1:** + +**Input:** num = 4, t = 1 + +**Output:** 6 + +**Explanation:** + +The maximum achievable number is x = 6; it can become equal to num after performing this operation: + +1- Decrease x by 1, and increase num by 1. Now, x = 5 and num = 5. + +It can be proven that there is no achievable number larger than 6. + +**Example 2:** + +**Input:** num = 3, t = 2 + +**Output:** 7 + +**Explanation:** + +The maximum achievable number is x = 7; after performing these operations, x will equal num: + +1- Decrease x by 1, and increase num by 1. Now, x = 6 and num = 4. + +2- Decrease x by 1, and increase num by 1. Now, x = 5 and num = 5. + +It can be proven that there is no achievable number larger than 7. + +**Constraints:** + +* `1 <= num, t <= 50` + +## Solution + +```java +public class Solution { + public int theMaximumAchievableX(int num, int t) { + return num + t * 2; + } +} +``` \ No newline at end of file diff --git a/src/main/java/g2701_2800/s2770_maximum_number_of_jumps_to_reach_the_last_index/readme.md b/src/main/java/g2701_2800/s2770_maximum_number_of_jumps_to_reach_the_last_index/readme.md new file mode 100644 index 00000000..c58810d8 --- /dev/null +++ b/src/main/java/g2701_2800/s2770_maximum_number_of_jumps_to_reach_the_last_index/readme.md @@ -0,0 +1,108 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Java?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Java) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Java?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Java/fork) + +## 2770\. Maximum Number of Jumps to Reach the Last Index + +Medium + +You are given a **0-indexed** array `nums` of `n` integers and an integer `target`. + +You are initially positioned at index `0`. In one step, you can jump from index `i` to any index `j` such that: + +* `0 <= i < j < n` +* `-target <= nums[j] - nums[i] <= target` + +Return _the **maximum number of jumps** you can make to reach index_ `n - 1`. + +If there is no way to reach index `n - 1`, return `-1`. + +**Example 1:** + +**Input:** nums = [1,3,6,4,1,2], target = 2 + +**Output:** 3 + +**Explanation:** + +To go from index 0 to index n - 1 with the maximum number of jumps, you can perform the following jumping sequence: + +- Jump from index 0 to index 1. + +- Jump from index 1 to index 3. + +- Jump from index 3 to index 5. + +It can be proven that there is no other jumping sequence that goes from 0 to n - 1 with more than 3 jumps. + +Hence, the answer is 3. + +**Example 2:** + +**Input:** nums = [1,3,6,4,1,2], target = 3 + +**Output:** 5 + +**Explanation:** + +To go from index 0 to index n - 1 with the maximum number of jumps, you can perform the following jumping sequence: + +- Jump from index 0 to index 1. + +- Jump from index 1 to index 2. + +- Jump from index 2 to index 3. + +- Jump from index 3 to index 4. + +- Jump from index 4 to index 5. + +It can be proven that there is no other jumping sequence that goes from 0 to n - 1 with more than 5 jumps. + +Hence, the answer is 5. + +**Example 3:** + +**Input:** nums = [1,3,6,4,1,2], target = 0 + +**Output:** -1 + +**Explanation:** It can be proven that there is no jumping sequence that goes from 0 to n - 1. Hence, the answer is -1. + +**Constraints:** + +* `2 <= nums.length == n <= 1000` +* -109 <= nums[i] <= 109 +* 0 <= target <= 2 * 109 + +## Solution + +```java +public class Solution { + private static class Pair { + int prev; + int len; + + Pair(int prev, int len) { + this.prev = prev; + this.len = len; + } + } + + public int maximumJumps(int[] nums, int target) { + Pair[] arr = new Pair[nums.length]; + arr[0] = new Pair(0, 0); + for (int i = 1; i < nums.length; i++) { + arr[i] = new Pair(-1, 0); + for (int j = i - 1; j >= 0; j--) { + if (Math.abs(nums[i] - nums[j]) <= target + && arr[j].prev != -1 + && arr[j].len + 1 > arr[i].len) { + arr[i].prev = j; + arr[i].len = arr[j].len + 1; + } + } + } + return arr[nums.length - 1].len > 0 ? arr[nums.length - 1].len : -1; + } +} +``` \ No newline at end of file diff --git a/src/main/java/g2701_2800/s2771_longest_non_decreasing_subarray_from_two_arrays/readme.md b/src/main/java/g2701_2800/s2771_longest_non_decreasing_subarray_from_two_arrays/readme.md new file mode 100644 index 00000000..680736d2 --- /dev/null +++ b/src/main/java/g2701_2800/s2771_longest_non_decreasing_subarray_from_two_arrays/readme.md @@ -0,0 +1,92 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Java?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Java) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Java?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Java/fork) + +## 2771\. Longest Non-decreasing Subarray From Two Arrays + +Medium + +You are given two **0-indexed** integer arrays `nums1` and `nums2` of length `n`. + +Let's define another **0-indexed** integer array, `nums3`, of length `n`. For each index `i` in the range `[0, n - 1]`, you can assign either `nums1[i]` or `nums2[i]` to `nums3[i]`. + +Your task is to maximize the length of the **longest non-decreasing subarray** in `nums3` by choosing its values optimally. + +Return _an integer representing the length of the **longest non-decreasing** subarray in_ `nums3`. + +**Note:** A **subarray** is a contiguous **non-empty** sequence of elements within an array. + +**Example 1:** + +**Input:** nums1 = [2,3,1], nums2 = [1,2,1] + +**Output:** 2 + +**Explanation:** + +One way to construct nums3 is: + +nums3 = [nums1[0], nums2[1], nums2[2]] => [2,2,1]. + +The subarray starting from index 0 and ending at index 1, [2,2], forms a non-decreasing subarray of length 2. + +We can show that 2 is the maximum achievable length. + +**Example 2:** + +**Input:** nums1 = [1,3,2,1], nums2 = [2,2,3,4] + +**Output:** 4 + +**Explanation:** + +One way to construct nums3 is: + +nums3 = [nums1[0], nums2[1], nums2[2], nums2[3]] => [1,2,3,4]. + +The entire array forms a non-decreasing subarray of length 4, making it the maximum achievable length. + +**Example 3:** + +**Input:** nums1 = [1,1], nums2 = [2,2] + +**Output:** 2 + +**Explanation:** + +One way to construct nums3 is: + +nums3 = [nums1[0], nums1[1]] => [1,1]. + +The entire array forms a non-decreasing subarray of length 2, making it the maximum achievable length. + +**Constraints:** + +* 1 <= nums1.length == nums2.length == n <= 105 +* 1 <= nums1[i], nums2[i] <= 109 + +## Solution + +```java +public class Solution { + public int maxNonDecreasingLength(int[] nums1, int[] nums2) { + int res = 1; + int dp1 = 1; + int dp2 = 1; + int n = nums1.length; + int t11; + int t12; + int t21; + int t22; + for (int i = 1; i < n; i++) { + t11 = (nums1[i - 1] <= nums1[i]) ? dp1 + 1 : 1; + t12 = (nums1[i - 1] <= nums2[i]) ? dp1 + 1 : 1; + t21 = (nums2[i - 1] <= nums1[i]) ? dp2 + 1 : 1; + t22 = (nums2[i - 1] <= nums2[i]) ? dp2 + 1 : 1; + dp1 = Math.max(t11, t21); + dp2 = Math.max(t12, t22); + res = Math.max(res, Math.max(dp1, dp2)); + } + return res; + } +} +``` \ No newline at end of file diff --git a/src/main/java/g2701_2800/s2772_apply_operations_to_make_all_array_elements_equal_to_zero/readme.md b/src/main/java/g2701_2800/s2772_apply_operations_to_make_all_array_elements_equal_to_zero/readme.md new file mode 100644 index 00000000..64aa7623 --- /dev/null +++ b/src/main/java/g2701_2800/s2772_apply_operations_to_make_all_array_elements_equal_to_zero/readme.md @@ -0,0 +1,67 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Java?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Java) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Java?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Java/fork) + +## 2772\. Apply Operations to Make All Array Elements Equal to Zero + +Medium + +You are given a **0-indexed** integer array `nums` and a positive integer `k`. + +You can apply the following operation on the array **any** number of times: + +* Choose **any** subarray of size `k` from the array and **decrease** all its elements by `1`. + +Return `true` _if you can make all the array elements equal to_ `0`_, or_ `false` _otherwise_. + +A **subarray** is a contiguous non-empty part of an array. + +**Example 1:** + +**Input:** nums = [2,2,3,1,1,0], k = 3 + +**Output:** true + +**Explanation:** + +We can do the following operations: + +- Choose the subarray [2,2,3]. The resulting array will be nums = [**1**,**1**,**2**,1,1,0]. + +- Choose the subarray [2,1,1]. The resulting array will be nums = [1,1,**1**,**0**,**0**,0]. + +- Choose the subarray [1,1,1]. The resulting array will be nums = [**0**,**0**,**0**,0,0,0]. + +**Example 2:** + +**Input:** nums = [1,3,1,1], k = 2 + +**Output:** false + +**Explanation:** It is not possible to make all the array elements equal to 0. + +**Constraints:** + +* 1 <= k <= nums.length <= 105 +* 0 <= nums[i] <= 106 + +## Solution + +```java +public class Solution { + public boolean checkArray(int[] nums, int k) { + int cur = 0; + int n = nums.length; + for (int i = 0; i < n; i++) { + if (cur > nums[i]) { + return false; + } + nums[i] -= cur; + cur += nums[i]; + if (i >= k - 1) { + cur -= nums[i - k + 1]; + } + } + return cur == 0; + } +} +``` \ No newline at end of file