Skip to content

Commit 806a282

Browse files
authored
Added tasks 2952-3000
1 parent 21c5906 commit 806a282

File tree

35 files changed

+2999
-10
lines changed
  • src/main/kotlin/g2901_3000
    • s2918_minimum_equal_sum_of_two_arrays_after_replacing_zeros
    • s2951_find_the_peaks
    • s2952_minimum_number_of_coins_to_be_added
    • s2953_count_complete_substrings
    • s2954_count_the_number_of_infection_sequences
    • s2956_find_common_elements_between_two_arrays
    • s2957_remove_adjacent_almost_equal_characters
    • s2958_length_of_longest_subarray_with_at_most_k_frequency
    • s2959_number_of_possible_sets_of_closing_branches
    • s2960_count_tested_devices_after_test_operations
    • s2961_double_modular_exponentiation
    • s2962_count_subarrays_where_max_element_appears_at_least_k_times
    • s2963_count_the_number_of_good_partitions
    • s2965_find_missing_and_repeated_values
    • s2966_divide_array_into_arrays_with_max_difference
    • s2967_minimum_cost_to_make_array_equalindromic
    • s2968_apply_operations_to_maximize_frequency_score
    • s2970_count_the_number_of_incremovable_subarrays_i
    • s2971_find_polygon_with_the_largest_perimeter
    • s2972_count_the_number_of_incremovable_subarrays_ii
    • s2973_find_number_of_coins_to_place_in_tree_nodes
    • s2974_minimum_number_game
    • s2975_maximum_square_area_by_removing_fences_from_a_field
    • s2976_minimum_cost_to_convert_string_i
    • s2977_minimum_cost_to_convert_string_ii
    • s2980_check_if_bitwise_or_has_trailing_zeros
    • s2981_find_longest_special_substring_that_occurs_thrice_i
    • s2982_find_longest_special_substring_that_occurs_thrice_ii
    • s2983_palindrome_rearrangement_queries
    • s2996_smallest_missing_integer_greater_than_sequential_prefix_sum
    • s2997_minimum_number_of_operations_to_make_array_xor_equal_to_k
    • s2998_minimum_number_of_operations_to_make_x_and_y_equal
    • s2999_count_the_number_of_powerful_integers
    • s3000_maximum_area_of_longest_diagonal_rectangle

35 files changed

+2999
-10
lines changed

README.md

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1816,8 +1816,41 @@
18161816

18171817
| # | Title | Difficulty | Tag | Time, ms | Time, %
18181818
|------|----------------|-------------|-------------|----------|--------
1819+
| 3000 |[Maximum Area of Longest Diagonal Rectangle](src/main/kotlin/g2901_3000/s3000_maximum_area_of_longest_diagonal_rectangle)| Easy | Array | 167 | 94.44
1820+
| 2999 |[Count the Number of Powerful Integers](src/main/kotlin/g2901_3000/s2999_count_the_number_of_powerful_integers)| Hard | String, Dynamic_Programming, Math | 163 | 100.00
1821+
| 2998 |[Minimum Number of Operations to Make X and Y Equal](src/main/kotlin/g2901_3000/s2998_minimum_number_of_operations_to_make_x_and_y_equal)| Medium | Dynamic_Programming, Breadth_First_Search, Memoization | 149 | 100.00
1822+
| 2997 |[Minimum Number of Operations to Make Array XOR Equal to K](src/main/kotlin/g2901_3000/s2997_minimum_number_of_operations_to_make_array_xor_equal_to_k)| Medium | Array, Bit_Manipulation | 452 | 84.38
1823+
| 2996 |[Smallest Missing Integer Greater Than Sequential Prefix Sum](src/main/kotlin/g2901_3000/s2996_smallest_missing_integer_greater_than_sequential_prefix_sum)| Easy | Array, Hash_Table, Sorting | 174 | 83.33
1824+
| 2983 |[Palindrome Rearrangement Queries](src/main/kotlin/g2901_3000/s2983_palindrome_rearrangement_queries)| Hard | String, Hash_Table, Prefix_Sum | 905 | 87.50
1825+
| 2982 |[Find Longest Special Substring That Occurs Thrice II](src/main/kotlin/g2901_3000/s2982_find_longest_special_substring_that_occurs_thrice_ii)| Medium | String, Hash_Table, Binary_Search, Counting, Sliding_Window | 343 | 100.00
1826+
| 2981 |[Find Longest Special Substring That Occurs Thrice I](src/main/kotlin/g2901_3000/s2981_find_longest_special_substring_that_occurs_thrice_i)| Medium | String, Hash_Table, Binary_Search, Counting, Sliding_Window | 208 | 90.70
1827+
| 2980 |[Check if Bitwise OR Has Trailing Zeros](src/main/kotlin/g2901_3000/s2980_check_if_bitwise_or_has_trailing_zeros)| Easy | Array, Bit_Manipulation | 183 | 89.58
1828+
| 2977 |[Minimum Cost to Convert String II](src/main/kotlin/g2901_3000/s2977_minimum_cost_to_convert_string_ii)| Hard | Array, String, Dynamic_Programming, Graph, Trie, Shortest_Path | 697 | 100.00
1829+
| 2976 |[Minimum Cost to Convert String I](src/main/kotlin/g2901_3000/s2976_minimum_cost_to_convert_string_i)| Medium | Array, String, Graph, Shortest_Path | 421 | 85.29
1830+
| 2975 |[Maximum Square Area by Removing Fences From a Field](src/main/kotlin/g2901_3000/s2975_maximum_square_area_by_removing_fences_from_a_field)| Medium | Array, Hash_Table, Enumeration | 1283 | 96.77
1831+
| 2974 |[Minimum Number Game](src/main/kotlin/g2901_3000/s2974_minimum_number_game)| Easy | Array, Sorting, Heap_Priority_Queue, Simulation | 213 | 97.92
1832+
| 2973 |[Find Number of Coins to Place in Tree Nodes](src/main/kotlin/g2901_3000/s2973_find_number_of_coins_to_place_in_tree_nodes)| Hard | Dynamic_Programming, Sorting, Depth_First_Search, Tree, Heap_Priority_Queue | 1134 | 90.91
1833+
| 2972 |[Count the Number of Incremovable Subarrays II](src/main/kotlin/g2901_3000/s2972_count_the_number_of_incremovable_subarrays_ii)| Hard | Array, Binary_Search, Two_Pointers | 483 | 100.00
1834+
| 2971 |[Find Polygon With the Largest Perimeter](src/main/kotlin/g2901_3000/s2971_find_polygon_with_the_largest_perimeter)| Medium | Array, Sorting, Greedy, Prefix_Sum | 534 | 97.06
1835+
| 2970 |[Count the Number of Incremovable Subarrays I](src/main/kotlin/g2901_3000/s2970_count_the_number_of_incremovable_subarrays_i)| Easy | Array, Binary_Search, Two_Pointers, Enumeration | 170 | 100.00
1836+
| 2968 |[Apply Operations to Maximize Frequency Score](src/main/kotlin/g2901_3000/s2968_apply_operations_to_maximize_frequency_score)| Hard | Array, Sorting, Binary_Search, Prefix_Sum, Sliding_Window | 566 | 90.00
1837+
| 2967 |[Minimum Cost to Make Array Equalindromic](src/main/kotlin/g2901_3000/s2967_minimum_cost_to_make_array_equalindromic)| Medium | Array, Math, Sorting, Greedy | 363 | 100.00
1838+
| 2966 |[Divide Array Into Arrays With Max Difference](src/main/kotlin/g2901_3000/s2966_divide_array_into_arrays_with_max_difference)| Medium | Array, Sorting, Greedy | 977 | 60.00
1839+
| 2965 |[Find Missing and Repeated Values](src/main/kotlin/g2901_3000/s2965_find_missing_and_repeated_values)| Easy | Array, Hash_Table, Math, Matrix | 235 | 91.67
1840+
| 2963 |[Count the Number of Good Partitions](src/main/kotlin/g2901_3000/s2963_count_the_number_of_good_partitions)| Hard | Array, Hash_Table, Math, Combinatorics | 600 | 100.00
1841+
| 2962 |[Count Subarrays Where Max Element Appears at Least K Times](src/main/kotlin/g2901_3000/s2962_count_subarrays_where_max_element_appears_at_least_k_times)| Medium | Array, Sliding_Window | 587 | 88.37
1842+
| 2961 |[Double Modular Exponentiation](src/main/kotlin/g2901_3000/s2961_double_modular_exponentiation)| Medium | Array, Math, Simulation | 197 | 92.50
1843+
| 2960 |[Count Tested Devices After Test Operations](src/main/kotlin/g2901_3000/s2960_count_tested_devices_after_test_operations)| Easy | Array, Simulation | 168 | 86.96
1844+
| 2959 |[Number of Possible Sets of Closing Branches](src/main/kotlin/g2901_3000/s2959_number_of_possible_sets_of_closing_branches)| Hard | Bit_Manipulation, Heap_Priority_Queue, Graph, Enumeration, Shortest_Path | 231 | 87.50
1845+
| 2958 |[Length of Longest Subarray With at Most K Frequency](src/main/kotlin/g2901_3000/s2958_length_of_longest_subarray_with_at_most_k_frequency)| Medium | Array, Hash_Table, Sliding_Window | 485 | 100.00
1846+
| 2957 |[Remove Adjacent Almost-Equal Characters](src/main/kotlin/g2901_3000/s2957_remove_adjacent_almost_equal_characters)| Medium | String, Dynamic_Programming, Greedy | 158 | 56.52
1847+
| 2956 |[Find Common Elements Between Two Arrays](src/main/kotlin/g2901_3000/s2956_find_common_elements_between_two_arrays)| Easy | Array, Hash_Table | 271 | 94.20
1848+
| 2954 |[Count the Number of Infection Sequences](src/main/kotlin/g2901_3000/s2954_count_the_number_of_infection_sequences)| Hard | Array, Math, Combinatorics | 1446 | 14.29
1849+
| 2953 |[Count Complete Substrings](src/main/kotlin/g2901_3000/s2953_count_complete_substrings)| Hard | String, Hash_Table, Sliding_Window | 315 | 100.00
1850+
| 2952 |[Minimum Number of Coins to be Added](src/main/kotlin/g2901_3000/s2952_minimum_number_of_coins_to_be_added)| Medium | Array, Sorting, Greedy | 439 | 87.10
1851+
| 2951 |[Find the Peaks](src/main/kotlin/g2901_3000/s2951_find_the_peaks)| Easy | Array, Enumeration | 188 | 93.75
18191852
| 2949 |[Count Beautiful Substrings II](src/main/kotlin/g2901_3000/s2949_count_beautiful_substrings_ii)| Hard | String, Hash_Table, Math, Prefix_Sum, Number_Theory | 299 | 100.00
1820-
| 2948 |[Make Lexicographically Smallest Array by Swapping Elements](src/main/kotlin/g2901_3000/s2948_make_lexicographically_smallest_array_by_swapping_elements)| Medium | Array, Sorting, Union_Find | 1831 | 6.45
1853+
| 2948 |[Make Lexicographically Smallest Array by Swapping Elements](src/main/kotlin/g2901_3000/s2948_make_lexicographically_smallest_array_by_swapping_elements)| Medium | Array, Sorting, Union_Find | 928 | 94.59
18211854
| 2947 |[Count Beautiful Substrings I](src/main/kotlin/g2901_3000/s2947_count_beautiful_substrings_i)| Medium | String, Prefix_Sum, Enumeration | 291 | 59.52
18221855
| 2946 |[Matrix Similarity After Cyclic Shifts](src/main/kotlin/g2901_3000/s2946_matrix_similarity_after_cyclic_shifts)| Easy | Array, Math, Matrix, Simulation | 210 | 75.00
18231856
| 2945 |[Find Maximum Non-decreasing Array Length](src/main/kotlin/g2901_3000/s2945_find_maximum_non_decreasing_array_length)| Hard | Array, Dynamic_Programming, Binary_Search, Stack, Monotonic_Stack, Queue, Monotonic_Queue | 636 | 87.50
@@ -1835,14 +1868,14 @@
18351868
| 2931 |[Maximum Spending After Buying Items](src/main/kotlin/g2901_3000/s2931_maximum_spending_after_buying_items)| Hard | Array, Sorting, Greedy, Matrix, Heap_Priority_Queue | 541 | 93.75
18361869
| 2930 |[Number of Strings Which Can Be Rearranged to Contain Substring](src/main/kotlin/g2901_3000/s2930_number_of_strings_which_can_be_rearranged_to_contain_substring)| Medium | Dynamic_Programming, Math, Combinatorics | 132 | 100.00
18371870
| 2929 |[Distribute Candies Among Children II](src/main/kotlin/g2901_3000/s2929_distribute_candies_among_children_ii)| Medium | Math, Enumeration, Combinatorics | 193 | 18.18
1838-
| 2928 |[Distribute Candies Among Children I](src/main/kotlin/g2901_3000/s2928_distribute_candies_among_children_i)| Easy | Math, Enumeration, Combinatorics | 282 | 6.38
1839-
| 2926 |[Maximum Balanced Subsequence Sum](src/main/kotlin/g2901_3000/s2926_maximum_balanced_subsequence_sum)| Hard | Array, Dynamic_Programming, Binary_Search, Segment_Tree, Binary_Indexed_Tree | 1067 | 12.50
1840-
| 2925 |[Maximum Score After Applying Operations on a Tree](src/main/kotlin/g2901_3000/s2925_maximum_score_after_applying_operations_on_a_tree)| Medium | Dynamic_Programming, Depth_First_Search, Tree | 1274 | 6.67
1841-
| 2924 |[Find Champion II](src/main/kotlin/g2901_3000/s2924_find_champion_ii)| Medium | Graph | 724 | 6.98
1842-
| 2923 |[Find Champion I](src/main/kotlin/g2901_3000/s2923_find_champion_i)| Easy | Array, Matrix | 591 | 5.63
1871+
| 2928 |[Distribute Candies Among Children I](src/main/kotlin/g2901_3000/s2928_distribute_candies_among_children_i)| Easy | Math, Enumeration, Combinatorics | 141 | 86.36
1872+
| 2926 |[Maximum Balanced Subsequence Sum](src/main/kotlin/g2901_3000/s2926_maximum_balanced_subsequence_sum)| Hard | Array, Dynamic_Programming, Binary_Search, Segment_Tree, Binary_Indexed_Tree | 636 | 100.00
1873+
| 2925 |[Maximum Score After Applying Operations on a Tree](src/main/kotlin/g2901_3000/s2925_maximum_score_after_applying_operations_on_a_tree)| Medium | Dynamic_Programming, Depth_First_Search, Tree | 706 | 81.82
1874+
| 2924 |[Find Champion II](src/main/kotlin/g2901_3000/s2924_find_champion_ii)| Medium | Graph | 422 | 81.82
1875+
| 2923 |[Find Champion I](src/main/kotlin/g2901_3000/s2923_find_champion_i)| Easy | Array, Matrix | 320 | 58.62
18431876
| 2920 |[Maximum Points After Collecting Coins From All Nodes](src/main/kotlin/g2901_3000/s2920_maximum_points_after_collecting_coins_from_all_nodes)| Hard | Array, Dynamic_Programming, Depth_First_Search, Tree, Bit_Manipulation | 2255 | 25.00
18441877
| 2919 |[Minimum Increment Operations to Make Array Beautiful](src/main/kotlin/g2901_3000/s2919_minimum_increment_operations_to_make_array_beautiful)| Medium | Array, Dynamic_Programming | 520 | 62.50
1845-
| 2918 |[Minimum Equal Sum of Two Arrays After Replacing Zeros](src/main/kotlin/g2901_3000/s2918_minimum_equal_sum_of_two_arrays_after_replacing_zeros)| Medium | Array, Greedy | 1410 | 7.69
1878+
| 2918 |[Minimum Equal Sum of Two Arrays After Replacing Zeros](src/main/kotlin/g2901_3000/s2918_minimum_equal_sum_of_two_arrays_after_replacing_zeros)| Medium | Array, Greedy | 598 | 100.00
18461879
| 2917 |[Find the K-or of an Array](src/main/kotlin/g2901_3000/s2917_find_the_k_or_of_an_array)| Easy | Array, Bit_Manipulation | 191 | 76.47
18471880
| 2916 |[Subarrays Distinct Element Sum of Squares II](src/main/kotlin/g2901_3000/s2916_subarrays_distinct_element_sum_of_squares_ii)| Hard | Array, Dynamic_Programming, Segment_Tree, Binary_Indexed_Tree | 467 | 100.00
18481881
| 2915 |[Length of the Longest Subsequence That Sums to Target](src/main/kotlin/g2901_3000/s2915_length_of_the_longest_subsequence_that_sums_to_target)| Medium | Array, Dynamic_Programming | 552 | 66.67
@@ -3982,7 +4015,7 @@
39824015
| 0210 |[Course Schedule II](src/main/kotlin/g0201_0300/s0210_course_schedule_ii)| Medium | Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Graph, Topological_Sort, Level_2_Day_11_Graph/BFS/DFS | 266 | 96.32
39834016
| 0209 |[Minimum Size Subarray Sum](src/main/kotlin/g0201_0300/s0209_minimum_size_subarray_sum)| Medium | Array, Binary_Search, Prefix_Sum, Sliding_Window, Algorithm_II_Day_5_Sliding_Window, Binary_Search_II_Day_1 | 315 | 96.73
39844017
| 0208 |[Implement Trie (Prefix Tree)](src/main/kotlin/g0201_0300/s0208_implement_trie_prefix_tree)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Design, Trie, Level_2_Day_16_Design, Udemy_Trie_and_Heap, Big_O_Time_O(word.length())_or_O(prefix.length())_Space_O(N) | 689 | 61.00
3985-
| 0207 |[Course Schedule](src/main/kotlin/g0201_0300/s0207_course_schedule)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Graph, Topological_Sort, Big_O_Time_O(N)_Space_O(N) | 356 | 10.58
4018+
| 0207 |[Course Schedule](src/main/kotlin/g0201_0300/s0207_course_schedule)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Graph, Topological_Sort, Big_O_Time_O(N)_Space_O(N) | 183 | 92.07
39864019
| 0206 |[Reverse Linked List](src/main/kotlin/g0201_0300/s0206_reverse_linked_list)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Linked_List, Recursion, Data_Structure_I_Day_8_Linked_List, Algorithm_I_Day_10_Recursion_Backtracking, Level_1_Day_3_Linked_List, Udemy_Linked_List, Big_O_Time_O(N)_Space_O(1) | 279 | 45.78
39874020
| 0205 |[Isomorphic Strings](src/main/kotlin/g0201_0300/s0205_isomorphic_strings)| Easy | String, Hash_Table, Level_1_Day_2_String | 278 | 79.96
39884021
| 0204 |[Count Primes](src/main/kotlin/g0201_0300/s0204_count_primes)| Medium | Top_Interview_Questions, Array, Math, Enumeration, Number_Theory | 360 | 96.61

src/main/kotlin/g2901_3000/s2918_minimum_equal_sum_of_two_arrays_after_replacing_zeros/readme.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,8 @@ class Solution {
4141
fun minSum(nums1: IntArray, nums2: IntArray): Long {
4242
val sum1 = nums1.fold(0L) { sum, element -> sum + element }
4343
val zeroCount1 = nums1.count { it == 0 }
44-
4544
val sum2 = nums2.fold(0L) { sum, element -> sum + element }
4645
val zeroCount2 = nums2.count { it == 0 }
47-
4846
if (
4947
(zeroCount1 == 0 && sum1 < sum2 + zeroCount2) ||
5048
(zeroCount2 == 0 && sum2 < sum1 + zeroCount1)
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 2951\. Find the Peaks
5+
6+
Easy
7+
8+
You are given a **0-indexed** array `mountain`. Your task is to find all the **peaks** in the `mountain` array.
9+
10+
Return _an array that consists of_ indices _of **peaks** in the given array in **any order**._
11+
12+
**Notes:**
13+
14+
* A **peak** is defined as an element that is **strictly greater** than its neighboring elements.
15+
* The first and last elements of the array are **not** a peak.
16+
17+
**Example 1:**
18+
19+
**Input:** mountain = [2,4,4]
20+
21+
**Output:** []
22+
23+
**Explanation:** mountain[0] and mountain[2] can not be a peak because they are first and last elements of the array.
24+
25+
mountain[1] also can not be a peak because it is not strictly greater than mountain[2].
26+
27+
So the answer is [].
28+
29+
**Example 2:**
30+
31+
**Input:** mountain = [1,4,3,8,5]
32+
33+
**Output:** [1,3]
34+
35+
**Explanation:** mountain[0] and mountain[4] can not be a peak because they are first and last elements of the array.
36+
37+
mountain[2] also can not be a peak because it is not strictly greater than mountain[3] and mountain[1].
38+
39+
But mountain [1] and mountain[3] are strictly greater than their neighboring elements. So the answer is [1,3].
40+
41+
**Constraints:**
42+
43+
* `3 <= mountain.length <= 100`
44+
* `1 <= mountain[i] <= 100`
45+
46+
## Solution
47+
48+
```kotlin
49+
class Solution {
50+
fun findPeaks(mountain: IntArray): List<Int> {
51+
val list: MutableList<Int> = ArrayList()
52+
for (i in 1 until mountain.size - 1) {
53+
if ((mountain[i - 1] < mountain[i]) && (mountain[i] > mountain[i + 1])) {
54+
list.add(i)
55+
}
56+
}
57+
return list
58+
}
59+
}
60+
```
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 2952\. Minimum Number of Coins to be Added
5+
6+
Medium
7+
8+
You are given a **0-indexed** integer array `coins`, representing the values of the coins available, and an integer `target`.
9+
10+
An integer `x` is **obtainable** if there exists a subsequence of `coins` that sums to `x`.
11+
12+
Return _the **minimum** number of coins **of any value** that need to be added to the array so that every integer in the range_ `[1, target]` _is **obtainable**_.
13+
14+
A **subsequence** of an array is a new **non-empty** array that is formed from the original array by deleting some (**possibly none**) of the elements without disturbing the relative positions of the remaining elements.
15+
16+
**Example 1:**
17+
18+
**Input:** coins = [1,4,10], target = 19
19+
20+
**Output:** 2
21+
22+
**Explanation:** We need to add coins 2 and 8. The resulting array will be [1,2,4,8,10].
23+
24+
It can be shown that all integers from 1 to 19 are obtainable from the resulting array, and that 2 is the minimum number of coins that need to be added to the array.
25+
26+
**Example 2:**
27+
28+
**Input:** coins = [1,4,10,5,7,19], target = 19
29+
30+
**Output:** 1
31+
32+
**Explanation:** We only need to add the coin 2. The resulting array will be [1,2,4,5,7,10,19].
33+
34+
It can be shown that all integers from 1 to 19 are obtainable from the resulting array, and that 1 is the minimum number of coins that need to be added to the array.
35+
36+
**Example 3:**
37+
38+
**Input:** coins = [1,1,1], target = 20
39+
40+
**Output:** 3
41+
42+
**Explanation:** We need to add coins 4, 8, and 16. The resulting array will be [1,1,1,4,8,16].
43+
44+
It can be shown that all integers from 1 to 20 are obtainable from the resulting array, and that 3 is the minimum number of coins that need to be added to the array.
45+
46+
**Constraints:**
47+
48+
* <code>1 <= target <= 10<sup>5</sup></code>
49+
* <code>1 <= coins.length <= 10<sup>5</sup></code>
50+
* `1 <= coins[i] <= target`
51+
52+
## Solution
53+
54+
```kotlin
55+
class Solution {
56+
fun minimumAddedCoins(coins: IntArray, target: Int): Int {
57+
var res = 0
58+
var num = 0
59+
var i = 0
60+
coins.sort()
61+
while (num < target) {
62+
if (i < coins.size && coins[i] <= num + 1) {
63+
num += coins[i]
64+
i++
65+
} else {
66+
res += 1
67+
num += num + 1
68+
}
69+
}
70+
return res
71+
}
72+
}
73+
```

0 commit comments

Comments
 (0)