Skip to content

Commit 56755cd

Browse files
authored
Added tasks 1825-1837
1 parent 79db585 commit 56755cd

File tree

31 files changed

+1016
-0
lines changed

31 files changed

+1016
-0
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1874,6 +1874,16 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.13'
18741874
| 1904 |[The Number of Full Rounds You Have Played](src/main/kotlin/g1901_2000/s1904_the_number_of_full_rounds_you_have_played/Solution.kt)| Medium | String, Math | 149 | 100.00
18751875
| 1903 |[Largest Odd Number in String](src/main/kotlin/g1901_2000/s1903_largest_odd_number_in_string/Solution.kt)| Easy | String, Math, Greedy | 256 | 75.00
18761876
| 1901 |[Find a Peak Element II](src/main/kotlin/g1901_2000/s1901_find_a_peak_element_ii/Solution.kt)| Medium | Array, Binary_Search, Matrix, Divide_and_Conquer, Binary_Search_II_Day_17 | 726 | 100.00
1877+
| 1837 |[Sum of Digits in Base K](src/main/kotlin/g1801_1900/s1837_sum_of_digits_in_base_k/Solution.kt)| Easy | Math | 120 | 100.00
1878+
| 1835 |[Find XOR Sum of All Pairs Bitwise AND](src/main/kotlin/g1801_1900/s1835_find_xor_sum_of_all_pairs_bitwise_and/Solution.kt)| Hard | Array, Math, Bit_Manipulation | 604 | 100.00
1879+
| 1834 |[Single-Threaded CPU](src/main/kotlin/g1801_1900/s1834_single_threaded_cpu/Solution.kt)| Medium | Array, Sorting, Heap_Priority_Queue | 1050 | 100.00
1880+
| 1833 |[Maximum Ice Cream Bars](src/main/kotlin/g1801_1900/s1833_maximum_ice_cream_bars/Solution.kt)| Medium | Array, Sorting, Greedy | 439 | 100.00
1881+
| 1832 |[Check if the Sentence Is Pangram](src/main/kotlin/g1801_1900/s1832_check_if_the_sentence_is_pangram/Solution.kt)| Easy | String, Hash_Table | 127 | 98.00
1882+
| 1830 |[Minimum Number of Operations to Make String Sorted](src/main/kotlin/g1801_1900/s1830_minimum_number_of_operations_to_make_string_sorted/Solution.kt)| Hard | String, Math, Combinatorics | 226 | 100.00
1883+
| 1829 |[Maximum XOR for Each Query](src/main/kotlin/g1801_1900/s1829_maximum_xor_for_each_query/Solution.kt)| Medium | Array, Bit_Manipulation, Prefix_Sum | 680 | 100.00
1884+
| 1828 |[Queries on Number of Points Inside a Circle](src/main/kotlin/g1801_1900/s1828_queries_on_number_of_points_inside_a_circle/Solution.kt)| Medium | Array, Math, Geometry | 284 | 100.00
1885+
| 1827 |[Minimum Operations to Make the Array Increasing](src/main/kotlin/g1801_1900/s1827_minimum_operations_to_make_the_array_increasing/Solution.kt)| Easy | Array, Greedy | 208 | 100.00
1886+
| 1825 |[Finding MK Average](src/main/kotlin/g1801_1900/s1825_finding_mk_average/MKAverage.kt)| Hard | Design, Heap_Priority_Queue, Ordered_Set, Queue | 1101 | 100.00
18771887
| 1824 |[Minimum Sideway Jumps](src/main/kotlin/g1801_1900/s1824_minimum_sideway_jumps/Solution.kt)| Medium | Array, Dynamic_Programming, Greedy | 726 | 100.00
18781888
| 1823 |[Find the Winner of the Circular Game](src/main/kotlin/g1801_1900/s1823_find_the_winner_of_the_circular_game/Solution.kt)| Medium | Array, Math, Simulation, Recursion, Queue, Data_Structure_II_Day_14_Stack_Queue | 119 | 87.50
18791889
| 1822 |[Sign of the Product of an Array](src/main/kotlin/g1801_1900/s1822_sign_of_the_product_of_an_array/Solution.kt)| Easy | Array, Math, Programming_Skills_I_Day_4_Loop | 170 | 92.51
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
package g1801_1900.s1825_finding_mk_average
2+
3+
// #Hard #Design #Heap_Priority_Queue #Ordered_Set #Queue
4+
// #2023_06_21_Time_1101_ms_(100.00%)_Space_122.8_MB_(100.00%)
5+
6+
import java.util.Deque
7+
import java.util.LinkedList
8+
import java.util.TreeMap
9+
10+
@Suppress("NAME_SHADOWING")
11+
class MKAverage(m: Int, k: Int) {
12+
private val m: Double
13+
private val k: Double
14+
private val c: Double
15+
private var avg: Double
16+
private val middle: Bst
17+
private val min: Bst
18+
private val max: Bst
19+
private val q: Deque<Int>
20+
21+
init {
22+
this.m = m.toDouble()
23+
this.k = k.toDouble()
24+
c = (m - k * 2).toDouble()
25+
avg = 0.0
26+
middle = Bst()
27+
min = Bst()
28+
max = Bst()
29+
q = LinkedList()
30+
}
31+
32+
fun addElement(num: Int) {
33+
var num = num
34+
if (min.size < k) {
35+
min.add(num)
36+
q.offer(num)
37+
return
38+
}
39+
if (max.size < k) {
40+
min.add(num)
41+
max.add(min.removeMax())
42+
q.offer(num)
43+
return
44+
}
45+
if (num >= min.lastKey() && num <= max.firstKey()) {
46+
middle.add(num)
47+
avg += num / c
48+
} else if (num < min.lastKey()) {
49+
min.add(num)
50+
val `val` = min.removeMax()
51+
middle.add(`val`)
52+
avg += `val` / c
53+
} else if (num > max.firstKey()) {
54+
max.add(num)
55+
val `val` = max.removeMin()
56+
middle.add(`val`)
57+
avg += `val` / c
58+
}
59+
q.offer(num)
60+
if (q.size > m) {
61+
num = q.poll()
62+
if (middle.containsKey(num)) {
63+
avg -= num / c
64+
middle.remove(num)
65+
} else if (min.containsKey(num)) {
66+
min.remove(num)
67+
val `val` = middle.removeMin()
68+
avg -= `val` / c
69+
min.add(`val`)
70+
} else if (max.containsKey(num)) {
71+
max.remove(num)
72+
val `val` = middle.removeMax()
73+
avg -= `val` / c
74+
max.add(`val`)
75+
}
76+
}
77+
}
78+
79+
fun calculateMKAverage(): Int {
80+
return if (q.size < m) {
81+
-1
82+
} else avg.toInt()
83+
}
84+
85+
internal class Bst {
86+
var map: TreeMap<Int, Int> = TreeMap()
87+
var size: Int = 0
88+
89+
fun add(num: Int) {
90+
val count = map.getOrDefault(num, 0) + 1
91+
map[num] = count
92+
size++
93+
}
94+
95+
fun remove(num: Int) {
96+
val count = map.getOrDefault(num, 1) - 1
97+
if (count > 0) {
98+
map[num] = count
99+
} else {
100+
map.remove(num)
101+
}
102+
size--
103+
}
104+
105+
fun removeMin(): Int {
106+
val key = map.firstKey()
107+
remove(key)
108+
return key
109+
}
110+
111+
fun removeMax(): Int {
112+
val key = map.lastKey()
113+
remove(key)
114+
return key
115+
}
116+
117+
fun containsKey(key: Int): Boolean {
118+
return map.containsKey(key)
119+
}
120+
121+
fun firstKey(): Int {
122+
return map.firstKey()
123+
}
124+
125+
fun lastKey(): Int {
126+
return map.lastKey()
127+
}
128+
}
129+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
1825\. Finding MK Average
2+
3+
Hard
4+
5+
You are given two integers, `m` and `k`, and a stream of integers. You are tasked to implement a data structure that calculates the **MKAverage** for the stream.
6+
7+
The **MKAverage** can be calculated using these steps:
8+
9+
1. If the number of the elements in the stream is less than `m` you should consider the **MKAverage** to be `-1`. Otherwise, copy the last `m` elements of the stream to a separate container.
10+
2. Remove the smallest `k` elements and the largest `k` elements from the container.
11+
3. Calculate the average value for the rest of the elements **rounded down to the nearest integer**.
12+
13+
Implement the `MKAverage` class:
14+
15+
* `MKAverage(int m, int k)` Initializes the **MKAverage** object with an empty stream and the two integers `m` and `k`.
16+
* `void addElement(int num)` Inserts a new element `num` into the stream.
17+
* `int calculateMKAverage()` Calculates and returns the **MKAverage** for the current stream **rounded down to the nearest integer**.
18+
19+
**Example 1:**
20+
21+
**Input** ["MKAverage", "addElement", "addElement", "calculateMKAverage", "addElement", "calculateMKAverage", "addElement", "addElement", "addElement", "calculateMKAverage"] [[3, 1], [3], [1], [], [10], [], [5], [5], [5], []]
22+
23+
**Output:** [null, null, null, -1, null, 3, null, null, null, 5]
24+
25+
**Explanation:** MKAverage obj = new MKAverage(3, 1); obj.addElement(3); // current elements are [3] obj.addElement(1); // current elements are [3,1] obj.calculateMKAverage(); // return -1, because m = 3 and only 2 elements exist. obj.addElement(10); // current elements are [3,1,10] obj.calculateMKAverage(); // The last 3 elements are [3,1,10]. // After removing smallest and largest 1 element the container will be ```[3]. // The average of [3] equals 3/1 = 3, return 3 obj.addElement(5); // current elements are [3,1,10,5] obj.addElement(5); // current elements are [3,1,10,5,5] obj.addElement(5); // current elements are [3,1,10,5,5,5] obj.calculateMKAverage(); // The last 3 elements are [5,5,5]. // After removing smallest and largest 1 element the container will be `[5]. // The average of [5] equals 5/1 = 5, return 5` ```
26+
27+
**Constraints:**
28+
29+
* <code>3 <= m <= 10<sup>5</sup></code>
30+
* `1 <= k*2 < m`
31+
* <code>1 <= num <= 10<sup>5</sup></code>
32+
* At most <code>10<sup>5</sup></code> calls will be made to `addElement` and `calculateMKAverage`.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package g1801_1900.s1827_minimum_operations_to_make_the_array_increasing
2+
3+
// #Easy #Array #Greedy #2023_06_21_Time_208_ms_(100.00%)_Space_38.9_MB_(77.78%)
4+
5+
class Solution {
6+
fun minOperations(nums: IntArray): Int {
7+
var minsOps = 0
8+
for (i in 1 until nums.size) {
9+
if (nums[i] <= nums[i - 1]) {
10+
minsOps += nums[i - 1] - nums[i] + 1
11+
nums[i] = nums[i - 1] + 1
12+
}
13+
}
14+
return minsOps
15+
}
16+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
1827\. Minimum Operations to Make the Array Increasing
2+
3+
Easy
4+
5+
You are given an integer array `nums` (**0-indexed**). In one operation, you can choose an element of the array and increment it by `1`.
6+
7+
* For example, if `nums = [1,2,3]`, you can choose to increment `nums[1]` to make `nums = [1,**3**,3]`.
8+
9+
Return _the **minimum** number of operations needed to make_ `nums` _**strictly** **increasing**._
10+
11+
An array `nums` is **strictly increasing** if `nums[i] < nums[i+1]` for all `0 <= i < nums.length - 1`. An array of length `1` is trivially strictly increasing.
12+
13+
**Example 1:**
14+
15+
**Input:** nums = [1,1,1]
16+
17+
**Output:** 3
18+
19+
**Explanation:** You can do the following operations:
20+
21+
1) Increment nums[2], so nums becomes [1,1,**2**].
22+
23+
2) Increment nums[1], so nums becomes [1,**2**,2].
24+
25+
3) Increment nums[2], so nums becomes [1,2,**3**].
26+
27+
**Example 2:**
28+
29+
**Input:** nums = [1,5,2,4,1]
30+
31+
**Output:** 14
32+
33+
**Example 3:**
34+
35+
**Input:** nums = [8]
36+
37+
**Output:** 0
38+
39+
**Constraints:**
40+
41+
* `1 <= nums.length <= 5000`
42+
* <code>1 <= nums[i] <= 10<sup>4</sup></code>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package g1801_1900.s1828_queries_on_number_of_points_inside_a_circle
2+
3+
// #Medium #Array #Math #Geometry #2023_06_21_Time_284_ms_(100.00%)_Space_45.8_MB_(100.00%)
4+
5+
class Solution {
6+
fun countPoints(points: Array<IntArray>, queries: Array<IntArray>): IntArray {
7+
val result = IntArray(queries.size)
8+
for ((i, query) in queries.withIndex()) {
9+
var pts = 0
10+
for (point in points) {
11+
if ((point[0] - query[0]) * (point[0] - query[0]) +
12+
(point[1] - query[1]) * (point[1] - query[1])
13+
<= query[2] * query[2]
14+
) {
15+
pts++
16+
}
17+
}
18+
result[i] = pts
19+
}
20+
return result
21+
}
22+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
1828\. Queries on Number of Points Inside a Circle
2+
3+
Medium
4+
5+
You are given an array `points` where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> is the coordinates of the <code>i<sup>th</sup></code> point on a 2D plane. Multiple points can have the **same** coordinates.
6+
7+
You are also given an array `queries` where <code>queries[j] = [x<sub>j</sub>, y<sub>j</sub>, r<sub>j</sub>]</code> describes a circle centered at <code>(x<sub>j</sub>, y<sub>j</sub>)</code> with a radius of <code>r<sub>j</sub></code>.
8+
9+
For each query `queries[j]`, compute the number of points **inside** the <code>j<sup>th</sup></code> circle. Points **on the border** of the circle are considered **inside**.
10+
11+
Return _an array_ `answer`_, where_ `answer[j]` _is the answer to the_ <code>j<sup>th</sup></code> _query_.
12+
13+
**Example 1:**
14+
15+
![](https://assets.leetcode.com/uploads/2021/03/25/chrome_2021-03-25_22-34-16.png)
16+
17+
**Input:** points = [[1,3],[3,3],[5,3],[2,2]], queries = [[2,3,1],[4,3,1],[1,1,2]]
18+
19+
**Output:** [3,2,2]
20+
21+
**Explanation:** The points and circles are shown above. queries[0] is the green circle, queries[1] is the red circle, and queries[2] is the blue circle.
22+
23+
**Example 2:**
24+
25+
![](https://assets.leetcode.com/uploads/2021/03/25/chrome_2021-03-25_22-42-07.png)
26+
27+
**Input:** points = [[1,1],[2,2],[3,3],[4,4],[5,5]], queries = [[1,2,2],[2,2,2],[4,3,2],[4,3,3]]
28+
29+
**Output:** [2,3,2,4]
30+
31+
**Explanation:** The points and circles are shown above. queries[0] is green, queries[1] is red, queries[2] is blue, and queries[3] is purple.
32+
33+
**Constraints:**
34+
35+
* `1 <= points.length <= 500`
36+
* `points[i].length == 2`
37+
* <code>0 <= x<sub>i</sub>, y<sub>i</sub> <= 500</code>
38+
* `1 <= queries.length <= 500`
39+
* `queries[j].length == 3`
40+
* <code>0 <= x<sub>j</sub>, y<sub>j</sub> <= 500</code>
41+
* <code>1 <= r<sub>j</sub> <= 500</code>
42+
* All coordinates are integers.
43+
44+
**Follow up:** Could you find the answer for each query in better complexity than `O(n)`?
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package g1801_1900.s1829_maximum_xor_for_each_query
2+
3+
// #Medium #Array #Bit_Manipulation #Prefix_Sum
4+
// #2023_06_21_Time_680_ms_(100.00%)_Space_52.8_MB_(100.00%)
5+
6+
class Solution {
7+
fun getMaximumXor(nums: IntArray, maximumBit: Int): IntArray {
8+
val result = IntArray(nums.size)
9+
var `val` = nums[0]
10+
val target = (1 shl maximumBit) - 1
11+
for (i in 1 until nums.size) {
12+
`val` = `val` xor nums[i]
13+
}
14+
for (i in result.indices) {
15+
result[i] = target xor `val`
16+
`val` = `val` xor nums[nums.size - i - 1]
17+
}
18+
return result
19+
}
20+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
1829\. Maximum XOR for Each Query
2+
3+
Medium
4+
5+
You are given a **sorted** array `nums` of `n` non-negative integers and an integer `maximumBit`. You want to perform the following query `n` **times**:
6+
7+
1. Find a non-negative integer <code>k < 2<sup>maximumBit</sup></code> such that `nums[0] XOR nums[1] XOR ... XOR nums[nums.length-1] XOR k` is **maximized**. `k` is the answer to the <code>i<sup>th</sup></code> query.
8+
2. Remove the **last** element from the current array `nums`.
9+
10+
Return _an array_ `answer`_, where_ `answer[i]` _is the answer to the_ <code>i<sup>th</sup></code> _query_.
11+
12+
**Example 1:**
13+
14+
**Input:** nums = [0,1,1,3], maximumBit = 2
15+
16+
**Output:** [0,3,2,3]
17+
18+
**Explanation:** The queries are answered as follows:
19+
20+
1<sup>st</sup> query: nums = [0,1,1,3], k = 0 since 0 XOR 1 XOR 1 XOR 3 XOR 0 = 3.
21+
22+
2<sup>nd</sup> query: nums = [0,1,1], k = 3 since 0 XOR 1 XOR 1 XOR 3 = 3.
23+
24+
3<sup>rd</sup> query: nums = [0,1], k = 2 since 0 XOR 1 XOR 2 = 3.
25+
26+
4<sup>th</sup> query: nums = [0], k = 3 since 0 XOR 3 = 3.
27+
28+
**Example 2:**
29+
30+
**Input:** nums = [2,3,4,7], maximumBit = 3
31+
32+
**Output:** [5,2,6,5]
33+
34+
**Explanation:** The queries are answered as follows:
35+
36+
1<sup>st</sup> query: nums = [2,3,4,7], k = 5 since 2 XOR 3 XOR 4 XOR 7 XOR 5 = 7.
37+
38+
2<sup>nd</sup> query: nums = [2,3,4], k = 2 since 2 XOR 3 XOR 4 XOR 2 = 7.
39+
40+
3<sup>rd</sup> query: nums = [2,3], k = 6 since 2 XOR 3 XOR 6 = 7.
41+
42+
4<sup>th</sup> query: nums = [2], k = 5 since 2 XOR 5 = 7.
43+
44+
**Example 3:**
45+
46+
**Input:** nums = [0,1,2,2,5,7], maximumBit = 3
47+
48+
**Output:** [4,3,6,4,6,7]
49+
50+
**Constraints:**
51+
52+
* `nums.length == n`
53+
* <code>1 <= n <= 10<sup>5</sup></code>
54+
* `1 <= maximumBit <= 20`
55+
* <code>0 <= nums[i] < 2<sup>maximumBit</sup></code>
56+
* `nums` is sorted in **ascending** order.

0 commit comments

Comments
 (0)