Skip to content

Commit 8640db6

Browse files
authored
Added tasks 526-587
1 parent e35b5f9 commit 8640db6

File tree

37 files changed

+2775
-0
lines changed

37 files changed

+2775
-0
lines changed

README.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757

5858
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
5959
|-|-|-|-|-|-
60+
| 0566 |[Reshape the Matrix](src/main/kotlin/g0501_0600/s0566_reshape_the_matrix)| Easy | Array, Matrix, Simulation | 239 | 99.05
6061

6162
#### Day 8 String
6263

@@ -154,6 +155,7 @@
154155
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
155156
|-|-|-|-|-|-
156157
| 0503 |[Next Greater Element II](src/main/kotlin/g0501_0600/s0503_next_greater_element_ii)| Medium | Array, Stack, Monotonic_Stack | 331 | 92.68
158+
| 0556 |[Next Greater Element III](src/main/kotlin/g0501_0600/s0556_next_greater_element_iii)| Medium | String, Math, Two_Pointers | 137 | 80.00
157159

158160
#### Day 11
159161

@@ -245,6 +247,7 @@
245247

246248
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
247249
|-|-|-|-|-|-
250+
| 0542 |[01 Matrix](src/main/kotlin/g0501_0600/s0542_01_matrix)| Medium | Array, Dynamic_Programming, Breadth_First_Search, Matrix | 441 | 94.06
248251

249252
#### Day 6 Matrix Related Problems
250253

@@ -260,6 +263,7 @@
260263

261264
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
262265
|-|-|-|-|-|-
266+
| 0547 |[Number of Provinces](src/main/kotlin/g0501_0600/s0547_number_of_provinces)| Medium | Depth_First_Search, Breadth_First_Search, Graph, Union_Find | 229 | 79.73
263267

264268
#### Day 9 Standard Traversal
265269

@@ -300,6 +304,7 @@
300304

301305
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
302306
|-|-|-|-|-|-
307+
| 0584 |[Find Customer Referee](src/main/kotlin/g0501_0600/s0584_find_customer_referee)| Easy | Database | 779 | 43.48
303308
| 0183 |[Customers Who Never Order](src/main/kotlin/g0101_0200/s0183_customers_who_never_order)| Easy | Database | 712 | 33.67
304309

305310
#### Day 2 Select and Order
@@ -340,6 +345,7 @@
340345

341346
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
342347
|-|-|-|-|-|-
348+
| 0586 |[Customer Placing the Largest Number of Orders](src/main/kotlin/g0501_0600/s0586_customer_placing_the_largest_number_of_orders)| Easy | LeetCode_Curated_SQL_70, Database | 768 | 44.85
343349
| 0511 |[Game Play Analysis I](src/main/kotlin/g0501_0600/s0511_game_play_analysis_i)| Easy | LeetCode_Curated_SQL_70, Database | 790 | 45.04
344350

345351
#### Day 9 Control of Flow
@@ -582,6 +588,7 @@
582588

583589
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
584590
|-|-|-|-|-|-
591+
| 0547 |[Number of Provinces](src/main/kotlin/g0501_0600/s0547_number_of_provinces)| Medium | Depth_First_Search, Breadth_First_Search, Graph, Union_Find | 229 | 79.73
585592

586593
#### Day 20 Brute Force/Backtracking
587594

@@ -645,6 +652,7 @@
645652
| 0448 |[Find All Numbers Disappeared in an Array](src/main/kotlin/g0401_0500/s0448_find_all_numbers_disappeared_in_an_array)| Easy | Array, Hash_Table | 394 | 100.00
646653
| 0442 |[Find All Duplicates in an Array](src/main/kotlin/g0401_0500/s0442_find_all_duplicates_in_an_array)| Medium | Array, Hash_Table | 480 | 73.81
647654
| 0041 |[First Missing Positive](src/main/kotlin/g0001_0100/s0041_first_missing_positive)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table | 345 | 100.00
655+
| 0532 |[K-diff Pairs in an Array](src/main/kotlin/g0501_0600/s0532_k_diff_pairs_in_an_array)| Medium | Array, Hash_Table, Sorting, Binary_Search, Two_Pointers | 230 | 84.62
648656
| 0456 |[132 Pattern](src/main/kotlin/g0401_0500/s0456_132_pattern)| Medium | Array, Binary_Search, Stack, Ordered_Set, Monotonic_Stack | 434 | 100.00
649657
| 0239 |[Sliding Window Maximum](src/main/kotlin/g0201_0300/s0239_sliding_window_maximum)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Array, Heap_Priority_Queue, Sliding_Window, Queue, Monotonic_Queue | 1059 | 86.14
650658

@@ -812,6 +820,7 @@
812820

813821
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
814822
|-|-|-|-|-|-
823+
| 0566 |[Reshape the Matrix](src/main/kotlin/g0501_0600/s0566_reshape_the_matrix)| Easy | Array, Matrix, Simulation | 239 | 99.05
815824
| 0118 |[Pascal's Triangle](src/main/kotlin/g0101_0200/s0118_pascals_triangle)| Easy | Top_Interview_Questions, Array, Dynamic_Programming | 277 | 33.22
816825

817826
#### Day 5 Array
@@ -1062,6 +1071,7 @@
10621071
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
10631072
|-|-|-|-|-|-
10641073
| 0344 |[Reverse String](src/main/kotlin/g0301_0400/s0344_reverse_string)| Easy | Top_Interview_Questions, String, Two_Pointers, Recursion | 445 | 69.75
1074+
| 0557 |[Reverse Words in a String III](src/main/kotlin/g0501_0600/s0557_reverse_words_in_a_string_iii)| Easy | String, Two_Pointers | 215 | 98.10
10651075

10661076
#### Day 5 Two Pointers
10671077

@@ -1074,6 +1084,7 @@
10741084
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
10751085
|-|-|-|-|-|-
10761086
| 0003 |[Longest Substring Without Repeating Characters](src/main/kotlin/g0001_0100/s0003_longest_substring_without_repeating_characters)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Sliding_Window | 258 | 91.09
1087+
| 0567 |[Permutation in String](src/main/kotlin/g0501_0600/s0567_permutation_in_string)| Medium | String, Hash_Table, Two_Pointers, Sliding_Window | 169 | 100.00
10771088

10781089
#### Day 7 Breadth First Search Depth First Search
10791090

@@ -1090,6 +1101,7 @@
10901101

10911102
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
10921103
|-|-|-|-|-|-
1104+
| 0542 |[01 Matrix](src/main/kotlin/g0501_0600/s0542_01_matrix)| Medium | Array, Dynamic_Programming, Breadth_First_Search, Matrix | 441 | 94.06
10931105
| 0994 |[Rotting Oranges](src/main/kotlin/g0901_1000/s0994_rotting_oranges)| Medium | Array, Breadth_First_Search, Matrix | 308 | 57.93
10941106

10951107
#### Day 10 Recursion Backtracking
@@ -1170,12 +1182,14 @@
11701182
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
11711183
|-|-|-|-|-|-
11721184
| 0200 |[Number of Islands](src/main/kotlin/g0101_0200/s0200_number_of_islands)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Depth_First_Search, Breadth_First_Search, Matrix, Union_Find | 252 | 95.41
1185+
| 0547 |[Number of Provinces](src/main/kotlin/g0501_0600/s0547_number_of_provinces)| Medium | Depth_First_Search, Breadth_First_Search, Graph, Union_Find | 229 | 79.73
11731186

11741187
#### Day 7 Breadth First Search Depth First Search
11751188

11761189
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
11771190
|-|-|-|-|-|-
11781191
| 0117 |[Populating Next Right Pointers in Each Node II](src/main/kotlin/g0101_0200/s0117_populating_next_right_pointers_in_each_node_ii)| Medium | Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Linked_List | 199 | 94.67
1192+
| 0572 |[Subtree of Another Tree](src/main/kotlin/g0501_0600/s0572_subtree_of_another_tree)| Easy | Depth_First_Search, Tree, Binary_Tree, Hash_Function, String_Matching | 214 | 92.39
11791193

11801194
#### Day 8 Breadth First Search Depth First Search
11811195

@@ -1245,6 +1259,7 @@
12451259
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
12461260
|-|-|-|-|-|-
12471261
| 1143 |[Longest Common Subsequence](src/main/kotlin/g1101_1200/s1143_longest_common_subsequence)| Medium | Top_100_Liked_Questions, String, Dynamic_Programming | 307 | 38.36
1262+
| 0583 |[Delete Operation for Two Strings](src/main/kotlin/g0501_0600/s0583_delete_operation_for_two_strings)| Medium | String, Dynamic_Programming | 197 | 100.00
12481263

12491264
#### Day 18 Dynamic Programming
12501265

@@ -1398,6 +1413,7 @@
13981413

13991414
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
14001415
|-|-|-|-|-|-
1416+
| 0540 |[Single Element in a Sorted Array](src/main/kotlin/g0501_0600/s0540_single_element_in_a_sorted_array)| Medium | Array, Binary_Search | 274 | 86.67
14011417

14021418
#### Day 10
14031419

@@ -1423,6 +1439,7 @@
14231439
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
14241440
|-|-|-|-|-|-
14251441
| 0154 |[Find Minimum in Rotated Sorted Array II](src/main/kotlin/g0101_0200/s0154_find_minimum_in_rotated_sorted_array_ii)| Hard | Array, Binary_Search | 275 | 84.00
1442+
| 0528 |[Random Pick with Weight](src/main/kotlin/g0501_0600/s0528_random_pick_with_weight)| Medium | Math, Binary_Search, Prefix_Sum, Randomized | 393 | 91.38
14261443

14271444
#### Day 14
14281445

@@ -1613,8 +1630,44 @@
16131630
| 0763 |[Partition Labels](src/main/kotlin/g0701_0800/s0763_partition_labels)| Medium | Top_100_Liked_Questions, String, Hash_Table, Greedy, Two_Pointers, Data_Structure_II_Day_7_String | 235 | 84.75
16141631
| 0739 |[Daily Temperatures](src/main/kotlin/g0701_0800/s0739_daily_temperatures)| Medium | Top_100_Liked_Questions, Array, Stack, Monotonic_Stack, Programming_Skills_II_Day_6 | 936 | 80.54
16151632
| 0647 |[Palindromic Substrings](src/main/kotlin/g0601_0700/s0647_palindromic_substrings)| Medium | Top_100_Liked_Questions, String, Dynamic_Programming | 266 | 67.83
1633+
| 0587 |[Erect the Fence](src/main/kotlin/g0501_0600/s0587_erect_the_fence)| Hard | Array, Math, Geometry | 470 | 100.00
1634+
| 0586 |[Customer Placing the Largest Number of Orders](src/main/kotlin/g0501_0600/s0586_customer_placing_the_largest_number_of_orders)| Easy | LeetCode_Curated_SQL_70, Database, SQL_I_Day_8_Function | 768 | 44.85
1635+
| 0584 |[Find Customer Referee](src/main/kotlin/g0501_0600/s0584_find_customer_referee)| Easy | Database, SQL_I_Day_1_Select | 779 | 43.48
1636+
| 0583 |[Delete Operation for Two Strings](src/main/kotlin/g0501_0600/s0583_delete_operation_for_two_strings)| Medium | String, Dynamic_Programming, Algorithm_II_Day_17_Dynamic_Programming | 197 | 100.00
1637+
| 0581 |[Shortest Unsorted Continuous Subarray](src/main/kotlin/g0501_0600/s0581_shortest_unsorted_continuous_subarray)| Medium | Array, Sorting, Greedy, Two_Pointers, Stack, Monotonic_Stack | 246 | 100.00
1638+
| 0576 |[Out of Boundary Paths](src/main/kotlin/g0501_0600/s0576_out_of_boundary_paths)| Medium | Dynamic_Programming | 153 | 100.00
1639+
| 0575 |[Distribute Candies](src/main/kotlin/g0501_0600/s0575_distribute_candies)| Easy | Array, Hash_Table | 538 | 76.92
1640+
| 0572 |[Subtree of Another Tree](src/main/kotlin/g0501_0600/s0572_subtree_of_another_tree)| Easy | Depth_First_Search, Tree, Binary_Tree, Hash_Function, String_Matching, Algorithm_II_Day_7_Breadth_First_Search_Depth_First_Search | 214 | 92.39
1641+
| 0567 |[Permutation in String](src/main/kotlin/g0501_0600/s0567_permutation_in_string)| Medium | String, Hash_Table, Two_Pointers, Sliding_Window, Algorithm_I_Day_6_Sliding_Window | 169 | 100.00
1642+
| 0566 |[Reshape the Matrix](src/main/kotlin/g0501_0600/s0566_reshape_the_matrix)| Easy | Array, Matrix, Simulation, Data_Structure_I_Day_4_Array, Programming_Skills_I_Day_7_Array | 239 | 99.05
1643+
| 0565 |[Array Nesting](src/main/kotlin/g0501_0600/s0565_array_nesting)| Medium | Array, Depth_First_Search | 553 | 100.00
1644+
| 0564 |[Find the Closest Palindrome](src/main/kotlin/g0501_0600/s0564_find_the_closest_palindrome)| Hard | String, Math | 179 | 100.00
1645+
| 0563 |[Binary Tree Tilt](src/main/kotlin/g0501_0600/s0563_binary_tree_tilt)| Easy | Depth_First_Search, Tree, Binary_Tree | 197 | 100.00
1646+
| 0561 |[Array Partition](src/main/kotlin/g0501_0600/s0561_array_partition_i)| Easy | Array, Sorting, Greedy, Counting_Sort | 337 | 90.48
16161647
| 0560 |[Subarray Sum Equals K](src/main/kotlin/g0501_0600/s0560_subarray_sum_equals_k)| Medium | Top_100_Liked_Questions, Array, Hash_Table, Prefix_Sum, Data_Structure_II_Day_5_Array | 692 | 53.27
1648+
| 0559 |[Maximum Depth of N-ary Tree](src/main/kotlin/g0501_0600/s0559_maximum_depth_of_n_ary_tree)| Easy | Depth_First_Search, Breadth_First_Search, Tree | 196 | 100.00
1649+
| 0558 |[Logical OR of Two Binary Grids Represented as Quad-Trees](src/main/kotlin/g0501_0600/s0558_logical_or_of_two_binary_grids_represented_as_quad_trees)| Medium | Tree, Divide_and_Conquer | 268 | 100.00
1650+
| 0557 |[Reverse Words in a String III](src/main/kotlin/g0501_0600/s0557_reverse_words_in_a_string_iii)| Easy | String, Two_Pointers, Algorithm_I_Day_4_Two_Pointers | 215 | 98.10
1651+
| 0556 |[Next Greater Element III](src/main/kotlin/g0501_0600/s0556_next_greater_element_iii)| Medium | String, Math, Two_Pointers, Programming_Skills_II_Day_10 | 137 | 80.00
1652+
| 0554 |[Brick Wall](src/main/kotlin/g0501_0600/s0554_brick_wall)| Medium | Array, Hash_Table | 307 | 100.00
1653+
| 0553 |[Optimal Division](src/main/kotlin/g0501_0600/s0553_optimal_division)| Medium | Array, Dynamic_Programming, Math | 154 | 100.00
1654+
| 0552 |[Student Attendance Record II](src/main/kotlin/g0501_0600/s0552_student_attendance_record_ii)| Hard | Dynamic_Programming | 151 | 100.00
1655+
| 0551 |[Student Attendance Record I](src/main/kotlin/g0501_0600/s0551_student_attendance_record_i)| Easy | String | 151 | 95.00
1656+
| 0547 |[Number of Provinces](src/main/kotlin/g0501_0600/s0547_number_of_provinces)| Medium | Depth_First_Search, Breadth_First_Search, Graph, Union_Find, Algorithm_II_Day_6_Breadth_First_Search_Depth_First_Search, Graph_Theory_I_Day_8_Standard_Traversal, Level_2_Day_19_Union_Find | 229 | 79.73
1657+
| 0546 |[Remove Boxes](src/main/kotlin/g0501_0600/s0546_remove_boxes)| Hard | Array, Dynamic_Programming, Memoization | 283 | 100.00
16171658
| 0543 |[Diameter of Binary Tree](src/main/kotlin/g0501_0600/s0543_diameter_of_binary_tree)| Easy | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree, Level_2_Day_7_Tree, Udemy_Tree_Stack_Queue | 307 | 43.93
1659+
| 0542 |[01 Matrix](src/main/kotlin/g0501_0600/s0542_01_matrix)| Medium | Array, Dynamic_Programming, Breadth_First_Search, Matrix, Algorithm_I_Day_9_Breadth_First_Search_Depth_First_Search, Graph_Theory_I_Day_5_Matrix_Related_Problems | 441 | 94.06
1660+
| 0541 |[Reverse String II](src/main/kotlin/g0501_0600/s0541_reverse_string_ii)| Easy | String, Two_Pointers | 200 | 83.33
1661+
| 0540 |[Single Element in a Sorted Array](src/main/kotlin/g0501_0600/s0540_single_element_in_a_sorted_array)| Medium | Array, Binary_Search, Binary_Search_II_Day_9 | 274 | 86.67
1662+
| 0539 |[Minimum Time Difference](src/main/kotlin/g0501_0600/s0539_minimum_time_difference)| Medium | Array, String, Math, Sorting | 183 | 100.00
1663+
| 0538 |[Convert BST to Greater Tree](src/main/kotlin/g0501_0600/s0538_convert_bst_to_greater_tree)| Medium | Depth_First_Search, Tree, Binary_Tree, Binary_Search_Tree | 252 | 77.78
1664+
| 0537 |[Complex Number Multiplication](src/main/kotlin/g0501_0600/s0537_complex_number_multiplication)| Medium | String, Math, Simulation | 171 | 75.00
1665+
| 0535 |[Encode and Decode TinyURL](src/main/kotlin/g0501_0600/s0535_encode_and_decode_tinyurl)| Medium | String, Hash_Table, Design, Hash_Function | 183 | 81.25
1666+
| 0532 |[K-diff Pairs in an Array](src/main/kotlin/g0501_0600/s0532_k_diff_pairs_in_an_array)| Medium | Array, Hash_Table, Sorting, Binary_Search, Two_Pointers, Udemy_Arrays | 230 | 84.62
1667+
| 0530 |[Minimum Absolute Difference in BST](src/main/kotlin/g0501_0600/s0530_minimum_absolute_difference_in_bst)| Easy | Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Binary_Search_Tree | 209 | 86.96
1668+
| 0529 |[Minesweeper](src/main/kotlin/g0501_0600/s0529_minesweeper)| Medium | Array, Depth_First_Search, Breadth_First_Search, Matrix | 243 | 87.50
1669+
| 0528 |[Random Pick with Weight](src/main/kotlin/g0501_0600/s0528_random_pick_with_weight)| Medium | Math, Binary_Search, Prefix_Sum, Randomized, Binary_Search_II_Day_13 | 393 | 91.38
1670+
| 0526 |[Beautiful Arrangement](src/main/kotlin/g0501_0600/s0526_beautiful_arrangement)| Medium | Array, Dynamic_Programming, Bit_Manipulation, Backtracking, Bitmask | 107 | 100.00
16181671
| 0525 |[Contiguous Array](src/main/kotlin/g0501_0600/s0525_contiguous_array)| Medium | Array, Hash_Table, Prefix_Sum | 471 | 100.00
16191672
| 0524 |[Longest Word in Dictionary through Deleting](src/main/kotlin/g0501_0600/s0524_longest_word_in_dictionary_through_deleting)| Medium | Array, String, Sorting, Two_Pointers | 307 | 100.00
16201673
| 0523 |[Continuous Subarray Sum](src/main/kotlin/g0501_0600/s0523_continuous_subarray_sum)| Medium | Array, Hash_Table, Math, Prefix_Sum | 682 | 95.45
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
## 526\. Beautiful Arrangement
5+
6+
Medium
7+
8+
Suppose you have `n` integers labeled `1` through `n`. A permutation of those `n` integers `perm` (**1-indexed**) is considered a **beautiful arrangement** if for every `i` (`1 <= i <= n`), **either** of the following is true:
9+
10+
* `perm[i]` is divisible by `i`.
11+
* `i` is divisible by `perm[i]`.
12+
13+
Given an integer `n`, return _the **number** of the **beautiful arrangements** that you can construct_.
14+
15+
**Example 1:**
16+
17+
**Input:** n = 2
18+
19+
**Output:** 2
20+
21+
**Explanation:**
22+
23+
The first beautiful arrangement is [1,2]:
24+
25+
- perm[1] = 1 is divisible by i = 1
26+
27+
- perm[2] = 2 is divisible by i = 2
28+
29+
The second beautiful arrangement is [2,1]:
30+
31+
- perm[1] = 2 is divisible by i = 1
32+
33+
- i = 2 is divisible by perm[2] = 1
34+
35+
**Example 2:**
36+
37+
**Input:** n = 1
38+
39+
**Output:** 1
40+
41+
**Constraints:**
42+
43+
* `1 <= n <= 15`
44+
45+
## Solution
46+
47+
```kotlin
48+
class Solution {
49+
fun countArrangement(n: Int): Int {
50+
return backtrack(n, n, arrayOfNulls(1 shl n + 1), 0)
51+
}
52+
53+
private fun backtrack(n: Int, index: Int, cache: Array<Int?>, cacheindex: Int): Int {
54+
if (index == 0) {
55+
return 1
56+
}
57+
var result = 0
58+
if (cache[cacheindex] != null) {
59+
return cache[cacheindex]!!
60+
}
61+
for (i in n downTo 1) {
62+
if (cacheindex and (1 shl i) == 0 && (i % index == 0 || index % i == 0)) {
63+
result += backtrack(n, index - 1, cache, cacheindex or (1 shl i))
64+
}
65+
}
66+
cache[cacheindex] = result
67+
return result
68+
}
69+
}
70+
```
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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+
## 528\. Random Pick with Weight
5+
6+
Medium
7+
8+
You are given a **0-indexed** array of positive integers `w` where `w[i]` describes the **weight** of the <code>i<sup>th</sup></code> index.
9+
10+
You need to implement the function `pickIndex()`, which **randomly** picks an index in the range `[0, w.length - 1]` (**inclusive**) and returns it. The **probability** of picking an index `i` is `w[i] / sum(w)`.
11+
12+
* For example, if `w = [1, 3]`, the probability of picking index `0` is `1 / (1 + 3) = 0.25` (i.e., `25%`), and the probability of picking index `1` is `3 / (1 + 3) = 0.75` (i.e., `75%`).
13+
14+
**Example 1:**
15+
16+
**Input** ["Solution","pickIndex"] [[[1]],[]]
17+
18+
**Output:** [null,0]
19+
20+
**Explanation:**
21+
22+
Solution solution = new Solution([1]);
23+
solution.pickIndex(); // return 0. The only option is to return 0 since there is only one element in w.
24+
25+
**Example 2:**
26+
27+
**Input**
28+
29+
["Solution","pickIndex","pickIndex","pickIndex","pickIndex","pickIndex"]
30+
[[[1,3]],[],[],[],[],[]]
31+
32+
**Output:** [null,1,1,1,1,0]
33+
34+
**Explanation:**
35+
36+
Solution solution = new Solution([1, 3]);
37+
solution.pickIndex(); // return 1. It is returning the second element (index = 1) that has a probability of 3/4.
38+
solution.pickIndex(); // return 1
39+
solution.pickIndex(); // return 1
40+
solution.pickIndex(); // return 1
41+
solution.pickIndex(); // return 0. It is returning the first element (index = 0) that has a probability of 1/4.
42+
43+
Since this is a randomization problem, multiple answers are allowed.
44+
All of the following outputs can be considered correct:
45+
[null,1,1,1,1,0]
46+
[null,1,1,1,1,1]
47+
[null,1,1,1,0,0]
48+
[null,1,1,1,0,1]
49+
[null,1,0,1,0,0]
50+
......
51+
and so on.
52+
53+
**Constraints:**
54+
55+
* <code>1 <= w.length <= 10<sup>4</sup></code>
56+
* <code>1 <= w[i] <= 10<sup>5</sup></code>
57+
* `pickIndex` will be called at most <code>10<sup>4</sup></code> times.
58+
59+
## Solution
60+
61+
```kotlin
62+
import java.util.Random
63+
import java.util.TreeMap
64+
65+
@Suppress("kotlin:S2245")
66+
class Solution(val w: IntArray) {
67+
68+
var x: IntArray = IntArray(w.size) { 0 }
69+
val rand = Random()
70+
val tree = TreeMap<Int, Int>()
71+
var sum = 0
72+
73+
init {
74+
for (i in w.indices) {
75+
tree.put(sum, i)
76+
sum += w[i]
77+
}
78+
}
79+
80+
fun pickIndex(): Int {
81+
val r = rand.nextInt(sum)
82+
return tree.floorEntry(r).value!!
83+
}
84+
}
85+
86+
/*
87+
* Your Solution object will be instantiated and called as such:
88+
* var obj = Solution(w)
89+
* var param_1 = obj.pickIndex()
90+
*/
91+
```

0 commit comments

Comments
 (0)