Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 12b5438

Browse files
2070: Most Beautiful Item for Each Query
1 parent 8888940 commit 12b5438

File tree

4 files changed

+120
-0
lines changed

4 files changed

+120
-0
lines changed

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ My solutions to LeetCode problems in Kotlin.
2424
| [796](https://leetcode.com/problems/rotate-string/) | [Rotate String](src/main/kotlin/com/schmoczer/leetcode/_0796/RotateString.kt) | Easy |
2525
| [1829](https://leetcode.com/problems/maximum-xor-for-each-query/) | [Maximum XOR for Each Query](src/main/kotlin/com/schmoczer/leetcode/_1829/MaximumXorForEachQuery.kt) | Medium |
2626
| [1957](https://leetcode.com/problems/delete-characters-to-make-fancy-string/) | [Delete Characters to Make Fancy String](src/main/kotlin/com/schmoczer/leetcode/_1957/DeleteCharactersToMakeFancyString.kt) | Easy |
27+
| [2070](https://leetcode.com/problems/most-beautiful-item-for-each-query/) | [Most Beautiful Item for Each Query](src/main/kotlin/com/schmoczer/leetcode/_2070/MostBeautifulItemForEachQuery.kt) | Medium |
2728
| [2275](https://leetcode.com/problems/largest-combination-with-bitwise-and-greater-than-zero/) | [Largest Combination With Bitwise AND Greater Than Zero](src/main/kotlin/com/schmoczer/leetcode/_2275/LargestCombination.kt) | Medium |
2829
| [2490](https://leetcode.com/problems/circular-sentence/) | [Circular Sentence](src/main/kotlin/com/schmoczer/leetcode/_2490/CircularSentence.kt) | Easy |
2930
| [2914](https://leetcode.com/problems/minimum-number-of-changes-to-make-binary-string-beautiful/) | [Minimum Number of Changes to Make Binary String Beautiful](src/main/kotlin/com/schmoczer/leetcode/_2914/MinChanges.kt) | Medium |
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.schmoczer.leetcode._2070
2+
3+
class MostBeautifulItemForEachQuery {
4+
// Runtime 93ms Beats 100.00%
5+
fun maximumBeauty(items: Array<IntArray>, queries: IntArray): IntArray {
6+
val result = mutableListOf<Int>()
7+
val alreadyQueried = mutableMapOf<Int, Int>()
8+
9+
items.sortBy { it[0] }
10+
val filteredItems = mutableListOf<IntArray>()
11+
var currentMax = 0
12+
for (i in 0 until items.size) {
13+
val item = items[i]
14+
if (currentMax < item[1]) {
15+
currentMax = item[1]
16+
filteredItems.add(item)
17+
}
18+
}
19+
20+
for (query in queries) {
21+
if (alreadyQueried[query] != null) {
22+
result.add(alreadyQueried[query]!!)
23+
} else {
24+
var maximumBeauty = 0
25+
var j = 0
26+
while (j < filteredItems.size && filteredItems[j][0] <= query) {
27+
if (filteredItems[j][1] > maximumBeauty) {
28+
maximumBeauty = filteredItems[j][1]
29+
}
30+
j++
31+
}
32+
result.add(maximumBeauty)
33+
alreadyQueried[query] = maximumBeauty
34+
}
35+
}
36+
return result.toIntArray()
37+
}
38+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Most Beautiful Item for Each Query
2+
3+
You are given a 2D integer array `items` where `items[i] = [price-of-i, beauty-of-i]` denotes the price and beauty of an
4+
item respectively.
5+
6+
You are also given a 0-indexed integer array `queries`. For each `queries[j]`, you want to determine the maximum beauty
7+
of an item whose price is less than or equal to `queries[j]`. If no such item exists, then the answer to this query is
8+
`0`.
9+
10+
Return an array `answer` of the same length as `queries` where `answer[j]` is the answer to the `j`'th query.
11+
12+
Example 1:
13+
14+
> Input: items = [[1,2],[3,2],[2,4],[5,6],[3,5]], queries = [1,2,3,4,5,6]
15+
>
16+
> Output: [2,4,5,5,6,6]
17+
18+
Example 2:
19+
20+
> Input: items = [[1,2],[1,2],[1,3],[1,4]], queries = [1]
21+
>
22+
> Output: [4]
23+
24+
Example 3:
25+
26+
> Input: items = [[10,1000]], queries = [5]
27+
>
28+
> Output: [0]
29+
30+
Constraints:
31+
32+
- `1 <= items.length, queries.length <= 10^5`
33+
- `items[i].length == 2`
34+
- `1 <= pricei, beautyi, queries[j] <= 10^9`
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.schmoczer.leetcode._2070
2+
3+
import org.junit.jupiter.api.BeforeEach
4+
import kotlin.test.Test
5+
import kotlin.test.assertContentEquals
6+
7+
class MostBeautifulItemForEachQueryTest {
8+
private lateinit var sut: MostBeautifulItemForEachQuery
9+
10+
@BeforeEach
11+
fun setUp() {
12+
sut = MostBeautifulItemForEachQuery()
13+
}
14+
15+
@Test
16+
fun `array with different prices`() {
17+
val items = arrayOf(intArrayOf(1, 2), intArrayOf(3, 2), intArrayOf(2, 4), intArrayOf(5, 6), intArrayOf(3, 5))
18+
val queries = intArrayOf(1, 2, 3, 4, 5, 6)
19+
val expected = intArrayOf(2, 4, 5, 5, 6, 6)
20+
21+
val result = sut.maximumBeauty(items, queries)
22+
23+
assertContentEquals(expected, result)
24+
}
25+
26+
@Test
27+
fun `array where all items have the same price`() {
28+
val items = arrayOf(intArrayOf(1, 2), intArrayOf(1, 2), intArrayOf(1, 3), intArrayOf(1, 4))
29+
val queries = intArrayOf(1)
30+
val expected = intArrayOf(4)
31+
32+
val result = sut.maximumBeauty(items, queries)
33+
34+
assertContentEquals(expected, result)
35+
}
36+
37+
@Test
38+
fun `array where no price fulfills the query`() {
39+
val items = arrayOf(intArrayOf(10, 1000))
40+
val queries = intArrayOf(5)
41+
val expected = intArrayOf(0)
42+
43+
val result = sut.maximumBeauty(items, queries)
44+
45+
assertContentEquals(expected, result)
46+
}
47+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /