Skip to content

Commit db0272d

Browse files
authored
Added tasks 1838-1850
1 parent fee3f27 commit db0272d

File tree

31 files changed

+1063
-0
lines changed

31 files changed

+1063
-0
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1071,6 +1071,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.13'
10711071

10721072
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
10731073
|-|-|-|-|-|-
1074+
| 1838 |[Frequency of the Most Frequent Element](src/main/kotlin/g1801_1900/s1838_frequency_of_the_most_frequent_element/Solution.kt)| Medium | Array, Sorting, Greedy, Binary_Search, Prefix_Sum, Sliding_Window | 564 | 88.89
10741075
| 0540 |[Single Element in a Sorted Array](src.save/main/kotlin/g0501_0600/s0540_single_element_in_a_sorted_array/Solution.kt)| Medium | Array, Binary_Search | 274 | 86.67
10751076

10761077
#### Day 10
@@ -1516,6 +1517,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.13'
15161517

15171518
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
15181519
|-|-|-|-|-|-
1520+
| 1845 |[Seat Reservation Manager](src/main/kotlin/g1801_1900/s1845_seat_reservation_manager/SeatManager.kt)| Medium | Design, Heap_Priority_Queue | 834 | 100.00
15191521
| 0860 |[Lemonade Change](src.save/main/kotlin/g0801_0900/s0860_lemonade_change/Solution.kt)| Easy | Array, Greedy | 413 | 86.96
15201522

15211523
#### Day 18
@@ -1914,6 +1916,16 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.13'
19141916
| 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
19151917
| 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
19161918
| 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
1919+
| 1850 |[Minimum Adjacent Swaps to Reach the Kth Smallest Number](src/main/kotlin/g1801_1900/s1850_minimum_adjacent_swaps_to_reach_the_kth_smallest_number/Solution.kt)| Medium | String, Greedy, Two_Pointers | 193 | 100.00
1920+
| 1849 |[Splitting a String Into Descending Consecutive Values](src/main/kotlin/g1801_1900/s1849_splitting_a_string_into_descending_consecutive_values/Solution.kt)| Medium | String, Backtracking | 136 | 100.00
1921+
| 1848 |[Minimum Distance to the Target Element](src/main/kotlin/g1801_1900/s1848_minimum_distance_to_the_target_element/Solution.kt)| Easy | Array | 172 | 80.00
1922+
| 1847 |[Closest Room](src/main/kotlin/g1801_1900/s1847_closest_room/Solution.kt)| Hard | Array, Sorting, Binary_Search | 1179 | 100.00
1923+
| 1846 |[Maximum Element After Decreasing and Rearranging](src/main/kotlin/g1801_1900/s1846_maximum_element_after_decreasing_and_rearranging/Solution.kt)| Medium | Array, Sorting, Greedy | 412 | 100.00
1924+
| 1845 |[Seat Reservation Manager](src/main/kotlin/g1801_1900/s1845_seat_reservation_manager/SeatManager.kt)| Medium | Design, Heap_Priority_Queue, Programming_Skills_II_Day_17 | 834 | 100.00
1925+
| 1844 |[Replace All Digits with Characters](src/main/kotlin/g1801_1900/s1844_replace_all_digits_with_characters/Solution.kt)| Easy | String | 137 | 100.00
1926+
| 1840 |[Maximum Building Height](src/main/kotlin/g1801_1900/s1840_maximum_building_height/Solution.kt)| Hard | Array, Math | 1210 | 100.00
1927+
| 1839 |[Longest Substring Of All Vowels in Order](src/main/kotlin/g1801_1900/s1839_longest_substring_of_all_vowels_in_order/Solution.kt)| Medium | String, Sliding_Window | 290 | 80.00
1928+
| 1838 |[Frequency of the Most Frequent Element](src/main/kotlin/g1801_1900/s1838_frequency_of_the_most_frequent_element/Solution.kt)| Medium | Array, Sorting, Greedy, Binary_Search, Prefix_Sum, Sliding_Window, Binary_Search_II_Day_9 | 564 | 88.89
19171929
| 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
19181930
| 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
19191931
| 1834 |[Single-Threaded CPU](src/main/kotlin/g1801_1900/s1834_single_threaded_cpu/Solution.kt)| Medium | Array, Sorting, Heap_Priority_Queue | 1050 | 100.00
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package g1801_1900.s1838_frequency_of_the_most_frequent_element
2+
3+
// #Medium #Array #Sorting #Greedy #Binary_Search #Prefix_Sum #Sliding_Window
4+
// #Binary_Search_II_Day_9 #2023_06_22_Time_564_ms_(88.89%)_Space_50.8_MB_(100.00%)
5+
6+
class Solution {
7+
fun maxFrequency(nums: IntArray, k: Int): Int {
8+
countingSort(nums)
9+
var start = 0
10+
var preSum = 0
11+
var total = 1
12+
for (i in nums.indices) {
13+
var length = i - start + 1
14+
var product = nums[i] * length
15+
preSum += nums[i]
16+
while (product - preSum > k) {
17+
preSum -= nums[start++]
18+
length--
19+
product = nums[i] * length
20+
}
21+
total = total.coerceAtLeast(length)
22+
}
23+
return total
24+
}
25+
26+
private fun countingSort(nums: IntArray) {
27+
var max = Int.MIN_VALUE
28+
for (num in nums) {
29+
max = max.coerceAtLeast(num)
30+
}
31+
val map = IntArray(max + 1)
32+
for (num in nums) {
33+
map[num]++
34+
}
35+
var i = 0
36+
var j = 0
37+
while (i <= max) {
38+
if (map[i]-- > 0) {
39+
nums[j++] = i
40+
} else {
41+
i++
42+
}
43+
}
44+
}
45+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
1838\. Frequency of the Most Frequent Element
2+
3+
Medium
4+
5+
The **frequency** of an element is the number of times it occurs in an array.
6+
7+
You are given an integer array `nums` and an integer `k`. In one operation, you can choose an index of `nums` and increment the element at that index by `1`.
8+
9+
Return _the **maximum possible frequency** of an element after performing **at most**_ `k` _operations_.
10+
11+
**Example 1:**
12+
13+
**Input:** nums = [1,2,4], k = 5
14+
15+
**Output:** 3
16+
17+
**Explanation:** Increment the first element three times and the second element two times to make nums = [4,4,4]. 4 has a frequency of 3.
18+
19+
**Example 2:**
20+
21+
**Input:** nums = [1,4,8,13], k = 5
22+
23+
**Output:** 2
24+
25+
**Explanation:** There are multiple optimal solutions:
26+
27+
- Increment the first element three times to make nums = [4,4,8,13]. 4 has a frequency of 2.
28+
29+
- Increment the second element four times to make nums = [1,8,8,13]. 8 has a frequency of 2.
30+
31+
- Increment the third element five times to make nums = [1,4,13,13]. 13 has a frequency of 2.
32+
33+
**Example 3:**
34+
35+
**Input:** nums = [3,9,6], k = 2
36+
37+
**Output:** 1
38+
39+
**Constraints:**
40+
41+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
42+
* <code>1 <= nums[i] <= 10<sup>5</sup></code>
43+
* <code>1 <= k <= 10<sup>5</sup></code>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package g1801_1900.s1839_longest_substring_of_all_vowels_in_order
2+
3+
// #Medium #String #Sliding_Window #2023_06_22_Time_290_ms_(80.00%)_Space_48.7_MB_(80.00%)
4+
5+
class Solution {
6+
fun longestBeautifulSubstring(word: String): Int {
7+
var cnt = 1
8+
var len = 1
9+
var maxLen = 0
10+
for (i in 1 until word.length) {
11+
if (word[i - 1] == word[i]) {
12+
++len
13+
} else if (word[i - 1] < word[i]) {
14+
++len
15+
++cnt
16+
} else {
17+
cnt = 1
18+
len = 1
19+
}
20+
if (cnt == 5) {
21+
maxLen = maxLen.coerceAtLeast(len)
22+
}
23+
}
24+
return maxLen
25+
}
26+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
1839\. Longest Substring Of All Vowels in Order
2+
3+
Medium
4+
5+
A string is considered **beautiful** if it satisfies the following conditions:
6+
7+
* Each of the 5 English vowels (`'a'`, `'e'`, `'i'`, `'o'`, `'u'`) must appear **at least once** in it.
8+
* The letters must be sorted in **alphabetical order** (i.e. all `'a'`s before `'e'`s, all `'e'`s before `'i'`s, etc.).
9+
10+
For example, strings `"aeiou"` and `"aaaaaaeiiiioou"` are considered **beautiful**, but `"uaeio"`, `"aeoiu"`, and `"aaaeeeooo"` are **not beautiful**.
11+
12+
Given a string `word` consisting of English vowels, return _the **length of the longest beautiful substring** of_ `word`_. If no such substring exists, return_ `0`.
13+
14+
A **substring** is a contiguous sequence of characters in a string.
15+
16+
**Example 1:**
17+
18+
**Input:** word = "aeiaaioaaaaeiiiiouuuooaauuaeiu"
19+
20+
**Output:** 13
21+
22+
**Explanation:** The longest beautiful substring in word is "aaaaeiiiiouuu" of length 13.
23+
24+
**Example 2:**
25+
26+
**Input:** word = "aeeeiiiioooauuuaeiou"
27+
28+
**Output:** 5
29+
30+
**Explanation:** The longest beautiful substring in word is "aeiou" of length 5.
31+
32+
**Example 3:**
33+
34+
**Input:** word = "a"
35+
36+
**Output:** 0
37+
38+
**Explanation:** There is no beautiful substring, so return 0.
39+
40+
**Constraints:**
41+
42+
* <code>1 <= word.length <= 5 * 10<sup>5</sup></code>
43+
* `word` consists of characters `'a'`, `'e'`, `'i'`, `'o'`, and `'u'`.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package g1801_1900.s1840_maximum_building_height
2+
3+
// #Hard #Array #Math #2023_06_22_Time_1210_ms_(100.00%)_Space_118.6_MB_(100.00%)
4+
5+
class Solution {
6+
fun maxBuilding(n: Int, restrictions: Array<IntArray>): Int {
7+
if (restrictions.isEmpty()) {
8+
return n - 1
9+
}
10+
val m = restrictions.size
11+
restrictions.sortWith(compareBy { a: IntArray -> a[0] })
12+
for (i in m - 2 downTo 0) {
13+
restrictions[i][1] =
14+
restrictions[i][1].coerceAtMost(restrictions[i + 1][1] + restrictions[i + 1][0] - restrictions[i][0])
15+
}
16+
var id = 1
17+
var height = 0
18+
var res = 0
19+
for (r in restrictions) {
20+
var currMax: Int
21+
if (r[1] >= height + r[0] - id) {
22+
currMax = height + r[0] - id
23+
height = currMax
24+
} else {
25+
currMax = (height + r[0] - id + r[1]) / 2
26+
height = r[1]
27+
}
28+
id = r[0]
29+
res = res.coerceAtLeast(currMax)
30+
}
31+
if (id != n) {
32+
res = res.coerceAtLeast(height + n - id)
33+
}
34+
return res
35+
}
36+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
1840\. Maximum Building Height
2+
3+
Hard
4+
5+
You want to build `n` new buildings in a city. The new buildings will be built in a line and are labeled from `1` to `n`.
6+
7+
However, there are city restrictions on the heights of the new buildings:
8+
9+
* The height of each building must be a non-negative integer.
10+
* The height of the first building **must** be `0`.
11+
* The height difference between any two adjacent buildings **cannot exceed** `1`.
12+
13+
Additionally, there are city restrictions on the maximum height of specific buildings. These restrictions are given as a 2D integer array `restrictions` where <code>restrictions[i] = [id<sub>i</sub>, maxHeight<sub>i</sub>]</code> indicates that building <code>id<sub>i</sub></code> must have a height **less than or equal to** <code>maxHeight<sub>i</sub></code>.
14+
15+
It is guaranteed that each building will appear **at most once** in `restrictions`, and building `1` will **not** be in `restrictions`.
16+
17+
Return _the **maximum possible height** of the **tallest** building_.
18+
19+
**Example 1:**
20+
21+
![](https://assets.leetcode.com/uploads/2021/04/08/ic236-q4-ex1-1.png)
22+
23+
**Input:** n = 5, restrictions = [[2,1],[4,1]]
24+
25+
**Output:** 2
26+
27+
**Explanation:** The green area in the image indicates the maximum allowed height for each building. We can build the buildings with heights [0,1,2,1,2], and the tallest building has a height of 2.
28+
29+
**Example 2:**
30+
31+
![](https://assets.leetcode.com/uploads/2021/04/08/ic236-q4-ex2.png)
32+
33+
**Input:** n = 6, restrictions = []
34+
35+
**Output:** 5
36+
37+
**Explanation:** The green area in the image indicates the maximum allowed height for each building. We can build the buildings with heights [0,1,2,3,4,5], and the tallest building has a height of 5.
38+
39+
**Example 3:**
40+
41+
![](https://assets.leetcode.com/uploads/2021/04/08/ic236-q4-ex3.png)
42+
43+
**Input:** n = 10, restrictions = [[5,3],[2,5],[7,4],[10,3]]
44+
45+
**Output:** 5
46+
47+
**Explanation:** The green area in the image indicates the maximum allowed height for each building. We can build the buildings with heights [0,1,2,3,3,4,4,5,4,3], and the tallest building has a height of 5.
48+
49+
**Constraints:**
50+
51+
* <code>2 <= n <= 10<sup>9</sup></code>
52+
* <code>0 <= restrictions.length <= min(n - 1, 10<sup>5</sup>)</code>
53+
* <code>2 <= id<sub>i</sub> <= n</code>
54+
* <code>id<sub>i</sub></code> is **unique**.
55+
* <code>0 <= maxHeight<sub>i</sub> <= 10<sup>9</sup></code>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package g1801_1900.s1844_replace_all_digits_with_characters
2+
3+
// #Easy #String #2023_06_22_Time_137_ms_(100.00%)_Space_34.1_MB_(100.00%)
4+
5+
class Solution {
6+
fun replaceDigits(s: String): String {
7+
val sb = StringBuilder()
8+
for (c in s.toCharArray()) {
9+
if (Character.isAlphabetic(c.code)) {
10+
sb.append(c)
11+
} else {
12+
sb.append((sb[sb.length - 1].code + Character.getNumericValue(c)).toChar())
13+
}
14+
}
15+
return sb.toString()
16+
}
17+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
1844\. Replace All Digits with Characters
2+
3+
Easy
4+
5+
You are given a **0-indexed** string `s` that has lowercase English letters in its **even** indices and digits in its **odd** indices.
6+
7+
There is a function `shift(c, x)`, where `c` is a character and `x` is a digit, that returns the <code>x<sup>th</sup></code> character after `c`.
8+
9+
* For example, `shift('a', 5) = 'f'` and `shift('x', 0) = 'x'`.
10+
11+
For every **odd** index `i`, you want to replace the digit `s[i]` with `shift(s[i-1], s[i])`.
12+
13+
Return `s` _after replacing all digits. It is **guaranteed** that_ `shift(s[i-1], s[i])` _will never exceed_ `'z'`.
14+
15+
**Example 1:**
16+
17+
**Input:** s = "a1c1e1"
18+
19+
**Output:** "abcdef"
20+
21+
**Explanation:** The digits are replaced as follows:
22+
23+
- s[1] -> shift('a',1) = 'b'
24+
25+
- s[3] -> shift('c',1) = 'd'
26+
27+
- s[5] -> shift('e',1) = 'f'
28+
29+
**Example 2:**
30+
31+
**Input:** s = "a1b2c3d4e"
32+
33+
**Output:** "abbdcfdhe"
34+
35+
**Explanation:** The digits are replaced as follows:
36+
37+
- s[1] -> shift('a',1) = 'b'
38+
39+
- s[3] -> shift('b',2) = 'd'
40+
41+
- s[5] -> shift('c',3) = 'f'
42+
43+
- s[7] -> shift('d',4) = 'h'
44+
45+
**Constraints:**
46+
47+
* `1 <= s.length <= 100`
48+
* `s` consists only of lowercase English letters and digits.
49+
* `shift(s[i-1], s[i]) <= 'z'` for all **odd** indices `i`.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package g1801_1900.s1845_seat_reservation_manager
2+
3+
// #Medium #Design #Heap_Priority_Queue #Programming_Skills_II_Day_17
4+
// #2023_06_22_Time_834_ms_(100.00%)_Space_94.6_MB_(91.67%)
5+
6+
import java.util.PriorityQueue
7+
import java.util.Queue
8+
9+
class SeatManager(n: Int) {
10+
private val seats: Queue<Int>
11+
private var smallest: Int
12+
13+
init {
14+
seats = PriorityQueue()
15+
smallest = 0
16+
}
17+
18+
fun reserve(): Int {
19+
return if (seats.isEmpty()) ++smallest else seats.poll()
20+
}
21+
22+
fun unreserve(seatNumber: Int) {
23+
seats.offer(seatNumber)
24+
}
25+
}

0 commit comments

Comments
 (0)