Skip to content

Commit 634b88a

Browse files
authored
Added tasks 3065-3069
1 parent 36cfd40 commit 634b88a

File tree

7 files changed

+474
-9
lines changed
  • src/main/kotlin
    • g0201_0300/s0295_find_median_from_data_stream
    • g3001_3100
      • s3065_minimum_operations_to_exceed_threshold_value_i
      • s3066_minimum_operations_to_exceed_threshold_value_ii
      • s3067_count_pairs_of_connectable_servers_in_a_weighted_tree_network
      • s3068_find_the_maximum_sum_of_node_values
      • s3069_distribute_elements_into_two_arrays_i

7 files changed

+474
-9
lines changed

README.md

+12-7
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@
425425
|-|-|-|-|-|-
426426
| 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
427427
| 0511 |[Game Play Analysis I](src/main/kotlin/g0501_0600/s0511_game_play_analysis_i)| Easy | LeetCode_Curated_SQL_70, Database | 790 | 45.04
428-
| 1890 |[The Latest Login in 2020](src/main/kotlin/g1801_1900/s1890_the_latest_login_in_2020)| Easy | Database | 1486 | 22.18
428+
| 1890 |[The Latest Login in 2020](src/main/kotlin/g1801_1900/s1890_the_latest_login_in_2020)| Easy | Database | 1280 | 43.62
429429
| 1741 |[Find Total Time Spent by Each Employee](src/main/kotlin/g1701_1800/s1741_find_total_time_spent_by_each_employee)| Easy | Database | 1101 | 51.40
430430

431431
#### Day 9 Control of Flow
@@ -451,7 +451,7 @@
451451

452452
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
453453
|-|-|-|-|-|-
454-
| 1480 |[Running Sum of 1d Array](src/main/kotlin/g1401_1500/s1480_running_sum_of_1d_array)| Easy | Array, Prefix_Sum | 195 | 21.52
454+
| 1480 |[Running Sum of 1d Array](src/main/kotlin/g1401_1500/s1480_running_sum_of_1d_array)| Easy | Array, Prefix_Sum | 161 | 84.80
455455
| 0724 |[Find Pivot Index](src/main/kotlin/g0701_0800/s0724_find_pivot_index)| Easy | Array, Prefix_Sum | 255 | 88.92
456456

457457
#### Day 2 String
@@ -1816,6 +1816,11 @@
18161816

18171817
| # | Title | Difficulty | Tag | Time, ms | Time, %
18181818
|------|----------------|-------------|-------------|----------|--------
1819+
| 3069 |[Distribute Elements Into Two Arrays I](src/main/kotlin/g3001_3100/s3069_distribute_elements_into_two_arrays_i)| Easy | Array, Simulation | 207 | 96.92
1820+
| 3068 |[Find the Maximum Sum of Node Values](src/main/kotlin/g3001_3100/s3068_find_the_maximum_sum_of_node_values)| Hard | Array, Dynamic_Programming, Sorting, Greedy, Tree, Bit_Manipulation | 531 | 66.67
1821+
| 3067 |[Count Pairs of Connectable Servers in a Weighted Tree Network](src/main/kotlin/g3001_3100/s3067_count_pairs_of_connectable_servers_in_a_weighted_tree_network)| Medium | Array, Depth_First_Search, Tree | 578 | 83.33
1822+
| 3066 |[Minimum Operations to Exceed Threshold Value II](src/main/kotlin/g3001_3100/s3066_minimum_operations_to_exceed_threshold_value_ii)| Medium | Array, Heap_Priority_Queue, Simulation | 543 | 98.11
1823+
| 3065 |[Minimum Operations to Exceed Threshold Value I](src/main/kotlin/g3001_3100/s3065_minimum_operations_to_exceed_threshold_value_i)| Easy | Array | 180 | 71.76
18191824
| 3049 |[Earliest Second to Mark Indices II](src/main/kotlin/g3001_3100/s3049_earliest_second_to_mark_indices_ii)| Hard | Array, Greedy, Binary_Search, Heap_Priority_Queue | 220 | 100.00
18201825
| 3048 |[Earliest Second to Mark Indices I](src/main/kotlin/g3001_3100/s3048_earliest_second_to_mark_indices_i)| Medium | Array, Binary_Search | 223 | 75.00
18211826
| 3047 |[Find the Largest Area of Square Inside Two Rectangles](src/main/kotlin/g3001_3100/s3047_find_the_largest_area_of_square_inside_two_rectangles)| Medium | Array, Math, Geometry | 753 | 40.42
@@ -1888,7 +1893,7 @@
18881893
| 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
18891894
| 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
18901895
| 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
1891-
| 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
1896+
| 2954 |[Count the Number of Infection Sequences](src/main/kotlin/g2901_3000/s2954_count_the_number_of_infection_sequences)| Hard | Array, Math, Combinatorics | 1493 | 100.00
18921897
| 2953 |[Count Complete Substrings](src/main/kotlin/g2901_3000/s2953_count_complete_substrings)| Hard | String, Hash_Table, Sliding_Window | 315 | 100.00
18931898
| 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
18941899
| 2951 |[Find the Peaks](src/main/kotlin/g2901_3000/s2951_find_the_peaks)| Easy | Array, Enumeration | 188 | 93.75
@@ -1910,7 +1915,7 @@
19101915
| 2932 |[Maximum Strong Pair XOR I](src/main/kotlin/g2901_3000/s2932_maximum_strong_pair_xor_i)| Easy | Array, Hash_Table, Bit_Manipulation, Sliding_Window, Trie | 192 | 43.08
19111916
| 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
19121917
| 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
1913-
| 2929 |[Distribute Candies Among Children II](src/main/kotlin/g2901_3000/s2929_distribute_candies_among_children_ii)| Medium | Math, Enumeration, Combinatorics | 193 | 18.18
1918+
| 2929 |[Distribute Candies Among Children II](src/main/kotlin/g2901_3000/s2929_distribute_candies_among_children_ii)| Medium | Math, Enumeration, Combinatorics | 163 | 66.67
19141919
| 2928 |[Distribute Candies Among Children I](src/main/kotlin/g2901_3000/s2928_distribute_candies_among_children_i)| Easy | Math, Enumeration, Combinatorics | 141 | 86.36
19151920
| 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
19161921
| 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
@@ -2613,7 +2618,7 @@
26132618
| 2042 |[Check if Numbers Are Ascending in a Sentence](src/main/kotlin/g2001_2100/s2042_check_if_numbers_are_ascending_in_a_sentence)| Easy | String | 161 | 64.29
26142619
| 2040 |[Kth Smallest Product of Two Sorted Arrays](src/main/kotlin/g2001_2100/s2040_kth_smallest_product_of_two_sorted_arrays)| Hard | Array, Binary_Search | 1001 | 100.00
26152620
| 2039 |[The Time When the Network Becomes Idle](src/main/kotlin/g2001_2100/s2039_the_time_when_the_network_becomes_idle)| Medium | Array, Breadth_First_Search, Graph | 1197 | 100.00
2616-
| 2038 |[Remove Colored Pieces if Both Neighbors are the Same Color](src/main/kotlin/g2001_2100/s2038_remove_colored_pieces_if_both_neighbors_are_the_same_color)| Medium | String, Math, Greedy, Game_Theory | 251 | 22.22
2621+
| 2038 |[Remove Colored Pieces if Both Neighbors are the Same Color](src/main/kotlin/g2001_2100/s2038_remove_colored_pieces_if_both_neighbors_are_the_same_color)| Medium | String, Math, Greedy, Game_Theory | 216 | 74.36
26172622
| 2037 |[Minimum Number of Moves to Seat Everyone](src/main/kotlin/g2001_2100/s2037_minimum_number_of_moves_to_seat_everyone)| Easy | Array, Sorting | 189 | 80.00
26182623
| 2035 |[Partition Array Into Two Arrays to Minimize Sum Difference](src/main/kotlin/g2001_2100/s2035_partition_array_into_two_arrays_to_minimize_sum_difference)| Hard | Array, Dynamic_Programming, Binary_Search, Two_Pointers, Bit_Manipulation, Ordered_Set, Bitmask | 1318 | 100.00
26192624
| 2034 |[Stock Price Fluctuation](src/main/kotlin/g2001_2100/s2034_stock_price_fluctuation)| Medium | Hash_Table, Design, Heap_Priority_Queue, Ordered_Set, Data_Stream | 985 | 100.00
@@ -2729,7 +2734,7 @@
27292734
| 1895 |[Largest Magic Square](src/main/kotlin/g1801_1900/s1895_largest_magic_square)| Medium | Array, Matrix, Prefix_Sum | 202 | 100.00
27302735
| 1894 |[Find the Student that Will Replace the Chalk](src/main/kotlin/g1801_1900/s1894_find_the_student_that_will_replace_the_chalk)| Medium | Array, Binary_Search, Simulation, Prefix_Sum, Binary_Search_II_Day_2 | 520 | 50.00
27312736
| 1893 |[Check if All the Integers in a Range Are Covered](src/main/kotlin/g1801_1900/s1893_check_if_all_the_integers_in_a_range_are_covered)| Easy | Array, Hash_Table, Prefix_Sum | 140 | 100.00
2732-
| 1890 |[The Latest Login in 2020](src/main/kotlin/g1801_1900/s1890_the_latest_login_in_2020)| Easy | Database, SQL_I_Day_8_Function | 1486 | 22.18
2737+
| 1890 |[The Latest Login in 2020](src/main/kotlin/g1801_1900/s1890_the_latest_login_in_2020)| Easy | Database, SQL_I_Day_8_Function | 1280 | 43.62
27332738
| 1889 |[Minimum Space Wasted From Packaging](src/main/kotlin/g1801_1900/s1889_minimum_space_wasted_from_packaging)| Hard | Array, Sorting, Binary_Search, Prefix_Sum | 910 | 100.00
27342739
| 1888 |[Minimum Number of Flips to Make the Binary String Alternating](src/main/kotlin/g1801_1900/s1888_minimum_number_of_flips_to_make_the_binary_string_alternating)| Medium | String, Greedy | 259 | 87.50
27352740
| 1887 |[Reduction Operations to Make the Array Elements Equal](src/main/kotlin/g1801_1900/s1887_reduction_operations_to_make_the_array_elements_equal)| Medium | Array, Sorting | 457 | 100.00
@@ -3055,7 +3060,7 @@
30553060
| 1483 |[Kth Ancestor of a Tree Node](src/main/kotlin/g1401_1500/s1483_kth_ancestor_of_a_tree_node)| Hard | Dynamic_Programming, Depth_First_Search, Breadth_First_Search, Tree, Binary_Search, Design | 956 | 100.00
30563061
| 1482 |[Minimum Number of Days to Make m Bouquets](src/main/kotlin/g1401_1500/s1482_minimum_number_of_days_to_make_m_bouquets)| Medium | Array, Binary_Search, Binary_Search_II_Day_7 | 538 | 50.00
30573062
| 1481 |[Least Number of Unique Integers after K Removals](src/main/kotlin/g1401_1500/s1481_least_number_of_unique_integers_after_k_removals)| Medium | Array, Hash_Table, Sorting, Greedy, Counting | 564 | 76.19
3058-
| 1480 |[Running Sum of 1d Array](src/main/kotlin/g1401_1500/s1480_running_sum_of_1d_array)| Easy | Array, Prefix_Sum, Level_1_Day_1_Prefix_Sum | 195 | 21.52
3063+
| 1480 |[Running Sum of 1d Array](src/main/kotlin/g1401_1500/s1480_running_sum_of_1d_array)| Easy | Array, Prefix_Sum, Level_1_Day_1_Prefix_Sum | 161 | 84.80
30593064
| 1478 |[Allocate Mailboxes](src/main/kotlin/g1401_1500/s1478_allocate_mailboxes)| Hard | Array, Dynamic_Programming, Math, Sorting | 226 | 100.00
30603065
| 1477 |[Find Two Non-overlapping Sub-arrays Each With Target Sum](src/main/kotlin/g1401_1500/s1477_find_two_non_overlapping_sub_arrays_each_with_target_sum)| Medium | Array, Hash_Table, Dynamic_Programming, Binary_Search, Sliding_Window | 746 | 100.00
30613066
| 1476 |[Subrectangle Queries](src/main/kotlin/g1401_1500/s1476_subrectangle_queries)| Medium | Array, Matrix, Design | 332 | 81.82

src/main/kotlin/g0201_0300/s0295_find_median_from_data_stream/readme.md

+12-2
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,21 @@ Implement the MedianFinder class:
1818

1919
**Example 1:**
2020

21-
**Input** ["MedianFinder", "addNum", "addNum", "findMedian", "addNum", "findMedian"] [[], [1], [2], [], [3], []]
21+
**Input**
22+
23+
["MedianFinder", "addNum", "addNum", "findMedian", "addNum", "findMedian"]
24+
[[], [1], [2], [], [3], []]
2225

2326
**Output:** [null, null, null, 1.5, null, 2.0]
2427

25-
**Explanation:** MedianFinder medianFinder = new MedianFinder(); medianFinder.addNum(1); // arr = [1] medianFinder.addNum(2); // arr = [1, 2] medianFinder.findMedian(); // return 1.5 (i.e., (1 + 2) / 2) medianFinder.addNum(3); // arr[1, 2, 3] medianFinder.findMedian(); // return 2.0
28+
**Explanation:**
29+
30+
MedianFinder medianFinder = new MedianFinder();
31+
medianFinder.addNum(1); // arr = [1]
32+
medianFinder.addNum(2); // arr = [1, 2]
33+
medianFinder.findMedian(); // return 1.5 (i.e., (1 + 2) / 2)
34+
medianFinder.addNum(3); // arr[1, 2, 3]
35+
medianFinder.findMedian(); // return 2.0
2636

2737
**Constraints:**
2838

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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+
## 3065\. Minimum Operations to Exceed Threshold Value I
5+
6+
Easy
7+
8+
You are given a **0-indexed** integer array `nums`, and an integer `k`.
9+
10+
In one operation, you can remove one occurrence of the smallest element of `nums`.
11+
12+
Return _the **minimum** number of operations needed so that all elements of the array are greater than or equal to_ `k`.
13+
14+
**Example 1:**
15+
16+
**Input:** nums = [2,11,10,1,3], k = 10
17+
18+
**Output:** 3
19+
20+
**Explanation:** After one operation, nums becomes equal to [2, 11, 10, 3].
21+
22+
After two operations, nums becomes equal to [11, 10, 3].
23+
24+
After three operations, nums becomes equal to [11, 10].
25+
26+
At this stage, all the elements of nums are greater than or equal to 10 so we can stop.
27+
28+
It can be shown that 3 is the minimum number of operations needed so that all elements of the array are greater than or equal to 10.
29+
30+
**Example 2:**
31+
32+
**Input:** nums = [1,1,2,4,9], k = 1
33+
34+
**Output:** 0
35+
36+
**Explanation:** All elements of the array are greater than or equal to 1 so we do not need to apply any operations on nums.
37+
38+
**Example 3:**
39+
40+
**Input:** nums = [1,1,2,4,9], k = 9
41+
42+
**Output:** 4
43+
44+
**Explanation:** only a single element of nums is greater than or equal to 9 so we need to apply the operations 4 times on nums.
45+
46+
**Constraints:**
47+
48+
* `1 <= nums.length <= 50`
49+
* <code>1 <= nums[i] <= 10<sup>9</sup></code>
50+
* <code>1 <= k <= 10<sup>9</sup></code>
51+
* The input is generated such that there is at least one index `i` such that `nums[i] >= k`.
52+
53+
## Solution
54+
55+
```kotlin
56+
class Solution {
57+
fun minOperations(nums: IntArray, k: Int): Int {
58+
var count = 0
59+
for (num in nums) {
60+
if (num >= k) {
61+
count++
62+
}
63+
}
64+
return nums.size - count
65+
}
66+
}
67+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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+
## 3066\. Minimum Operations to Exceed Threshold Value II
5+
6+
Medium
7+
8+
You are given a **0-indexed** integer array `nums`, and an integer `k`.
9+
10+
In one operation, you will:
11+
12+
* Take the two smallest integers `x` and `y` in `nums`.
13+
* Remove `x` and `y` from `nums`.
14+
* Add `min(x, y) * 2 + max(x, y)` anywhere in the array.
15+
16+
**Note** that you can only apply the described operation if `nums` contains at least two elements.
17+
18+
Return _the **minimum** number of operations needed so that all elements of the array are greater than or equal to_ `k`.
19+
20+
**Example 1:**
21+
22+
**Input:** nums = [2,11,10,1,3], k = 10
23+
24+
**Output:** 2
25+
26+
**Explanation:** In the first operation, we remove elements 1 and 2, then add 1 \* 2 + 2 to nums. nums becomes equal to [4, 11, 10, 3].
27+
28+
In the second operation, we remove elements 3 and 4, then add 3 \* 2 + 4 to nums. nums becomes equal to [10, 11, 10].
29+
30+
At this stage, all the elements of nums are greater than or equal to 10 so we can stop.
31+
32+
It can be shown that 2 is the minimum number of operations needed so that all elements of the array are greater than or equal to 10.
33+
34+
**Example 2:**
35+
36+
**Input:** nums = [1,1,2,4,9], k = 20
37+
38+
**Output:** 4
39+
40+
**Explanation:** After one operation, nums becomes equal to [2, 4, 9, 3].
41+
42+
After two operations, nums becomes equal to [7, 4, 9].
43+
44+
After three operations, nums becomes equal to [15, 9].
45+
46+
After four operations, nums becomes equal to [33].
47+
48+
At this stage, all the elements of nums are greater than 20 so we can stop.
49+
50+
It can be shown that 4 is the minimum number of operations needed so that all elements of the array are greater than or equal to 20.
51+
52+
**Constraints:**
53+
54+
* <code>2 <= nums.length <= 2 * 10<sup>5</sup></code>
55+
* <code>1 <= nums[i] <= 10<sup>9</sup></code>
56+
* <code>1 <= k <= 10<sup>9</sup></code>
57+
* The input is generated such that an answer always exists. That is, there exists some sequence of operations after which all elements of the array are greater than or equal to `k`.
58+
59+
## Solution
60+
61+
```kotlin
62+
class Solution {
63+
fun minOperations(nums: IntArray, k: Int): Int {
64+
val n = nums.size
65+
var steps = 0
66+
nums.sort()
67+
val extra: MutableList<Int> = ArrayList()
68+
var i = 0
69+
var j = 0
70+
while ((i < n && nums[i] < k) || (j < extra.size && extra[j] < k)) {
71+
val min = if (i < n && (j >= extra.size || extra[j] > nums[i])) {
72+
nums[i++]
73+
} else {
74+
extra[j++]
75+
}
76+
val max = if (i < n && (j >= extra.size || extra[j] > nums[i])) {
77+
nums[i++]
78+
} else {
79+
extra[j++]
80+
}
81+
steps++
82+
var res = min.toLong()
83+
res = 2 * res + max
84+
if (res > Int.MAX_VALUE) {
85+
extra.add(Int.MAX_VALUE)
86+
} else {
87+
extra.add(res.toInt())
88+
}
89+
}
90+
return steps
91+
}
92+
}
93+
```

0 commit comments

Comments
 (0)