Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
You are given a 2D integer array items where items[i] = [pricei, beautyi] denotes the price and beauty of an item respectively.
You are also given a 0-indexed integer array queries. For each queries[j], you want to determine the maximum beauty 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 0.
Return an array answer of the same length as queries where answer[j] is the answer to the jth query.
Example 1:
Input: items = [[1,2],[3,2],[2,4],[5,6],[3,5]], queries = [1,2,3,4,5,6] Output: [2,4,5,5,6,6] Explanation: - For queries[0]=1, [1,2] is the only item which has price <= 1. Hence, the answer for this query is 2. - For queries[1]=2, the items which can be considered are [1,2] and [2,4]. The maximum beauty among them is 4. - For queries[2]=3 and queries[3]=4, the items which can be considered are [1,2], [3,2], [2,4], and [3,5]. The maximum beauty among them is 5. - For queries[4]=5 and queries[5]=6, all items can be considered. Hence, the answer for them is the maximum beauty of all items, i.e., 6.
Example 2:
Input: items = [[1,2],[1,2],[1,3],[1,4]], queries = [1] Output: [4] Explanation: The price of every item is equal to 1, so we choose the item with the maximum beauty 4. Note that multiple items can have the same price and/or beauty.
Example 3:
Input: items = [[10,1000]], queries = [5] Output: [0] Explanation: No item has a price less than or equal to 5, so no item can be chosen. Hence, the answer to the query is 0.
Constraints:
1 <= items.length, queries.length <= 105items[i].length == 21 <= pricei, beautyi, queries[j] <= 109Problem summary: You are given a 2D integer array items where items[i] = [pricei, beautyi] denotes the price and beauty of an item respectively. You are also given a 0-indexed integer array queries. For each queries[j], you want to determine the maximum beauty 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 0. Return an array answer of the same length as queries where answer[j] is the answer to the jth query.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Binary Search
[[1,2],[3,2],[2,4],[5,6],[3,5]] [1,2,3,4,5,6]
[[1,2],[1,2],[1,3],[1,4]] [1]
[[10,1000]] [5]
closest-room)find-the-score-of-all-prefixes-of-an-array)maximum-sum-queries)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2070: Most Beautiful Item for Each Query
class Solution {
public int[] maximumBeauty(int[][] items, int[] queries) {
Arrays.sort(items, (a, b) -> a[0] - b[0]);
int n = items.length;
int m = queries.length;
int[] ans = new int[m];
Integer[] idx = new Integer[m];
for (int i = 0; i < m; ++i) {
idx[i] = i;
}
Arrays.sort(idx, (i, j) -> queries[i] - queries[j]);
int i = 0, mx = 0;
for (int j : idx) {
while (i < n && items[i][0] <= queries[j]) {
mx = Math.max(mx, items[i][1]);
++i;
}
ans[j] = mx;
}
return ans;
}
}
// Accepted solution for LeetCode #2070: Most Beautiful Item for Each Query
func maximumBeauty(items [][]int, queries []int) []int {
sort.Slice(items, func(i, j int) bool {
return items[i][0] < items[j][0]
})
n, m := len(items), len(queries)
idx := make([]int, m)
for i := range idx {
idx[i] = i
}
sort.Slice(idx, func(i, j int) bool { return queries[idx[i]] < queries[idx[j]] })
ans := make([]int, m)
i, mx := 0, 0
for _, j := range idx {
for i < n && items[i][0] <= queries[j] {
mx = max(mx, items[i][1])
i++
}
ans[j] = mx
}
return ans
}
# Accepted solution for LeetCode #2070: Most Beautiful Item for Each Query
class Solution:
def maximumBeauty(self, items: List[List[int]], queries: List[int]) -> List[int]:
items.sort()
n, m = len(items), len(queries)
ans = [0] * len(queries)
i = mx = 0
for q, j in sorted(zip(queries, range(m))):
while i < n and items[i][0] <= q:
mx = max(mx, items[i][1])
i += 1
ans[j] = mx
return ans
// Accepted solution for LeetCode #2070: Most Beautiful Item for Each Query
/**
* [2070] Most Beautiful Item for Each Query
*
* You are given a 2D integer array items where items[i] = [pricei, beautyi] denotes the price and beauty of an item respectively.
* You are also given a 0-indexed integer array queries. For each queries[j], you want to determine the maximum beauty 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 0.
* Return an array answer of the same length as queries where answer[j] is the answer to the j^th query.
*
* Example 1:
*
* Input: items = [[1,2],[3,2],[2,4],[5,6],[3,5]], queries = [1,2,3,4,5,6]
* Output: [2,4,5,5,6,6]
* Explanation:
* - For queries[0]=1, [1,2] is the only item which has price <= 1. Hence, the answer for this query is 2.
* - For queries[1]=2, the items which can be considered are [1,2] and [2,4].
* The maximum beauty among them is 4.
* - For queries[2]=3 and queries[3]=4, the items which can be considered are [1,2], [3,2], [2,4], and [3,5].
* The maximum beauty among them is 5.
* - For queries[4]=5 and queries[5]=6, all items can be considered.
* Hence, the answer for them is the maximum beauty of all items, i.e., 6.
*
* Example 2:
*
* Input: items = [[1,2],[1,2],[1,3],[1,4]], queries = [1]
* Output: [4]
* Explanation:
* The price of every item is equal to 1, so we choose the item with the maximum beauty 4.
* Note that multiple items can have the same price and/or beauty.
*
* Example 3:
*
* Input: items = [[10,1000]], queries = [5]
* Output: [0]
* Explanation:
* No item has a price less than or equal to 5, so no item can be chosen.
* Hence, the answer to the query is 0.
*
*
* Constraints:
*
* 1 <= items.length, queries.length <= 10^5
* items[i].length == 2
* 1 <= pricei, beautyi, queries[j] <= 10^9
*
*/
pub struct Solution {}
// problem: https://leetcode.com/problems/most-beautiful-item-for-each-query/
// discuss: https://leetcode.com/problems/most-beautiful-item-for-each-query/discuss/?currentPage=1&orderBy=most_votes&query=
// submission codes start here
impl Solution {
pub fn maximum_beauty(items: Vec<Vec<i32>>, queries: Vec<i32>) -> Vec<i32> {
vec![]
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[ignore]
fn test_2070_example_1() {
let items = vec![vec![1, 2], vec![3, 2], vec![2, 4], vec![5, 6], vec![3, 5]];
let queries = vec![1, 2, 3, 4, 5, 6];
let result = vec![2, 4, 5, 5, 6, 6];
assert_eq!(Solution::maximum_beauty(items, queries), result);
}
#[test]
#[ignore]
fn test_2070_example_2() {
let items = vec![vec![1, 2], vec![1, 2], vec![1, 3], vec![1, 4]];
let queries = vec![1];
let result = vec![4];
assert_eq!(Solution::maximum_beauty(items, queries), result);
}
#[test]
#[ignore]
fn test_2070_example_3() {
let items = vec![vec![10, 100]];
let queries = vec![5];
let result = vec![0];
assert_eq!(Solution::maximum_beauty(items, queries), result);
}
}
// Accepted solution for LeetCode #2070: Most Beautiful Item for Each Query
function maximumBeauty(items: number[][], queries: number[]): number[] {
const n = items.length;
const m = queries.length;
items.sort((a, b) => a[0] - b[0]);
const idx: number[] = Array(m)
.fill(0)
.map((_, i) => i);
idx.sort((i, j) => queries[i] - queries[j]);
let [i, mx] = [0, 0];
const ans: number[] = Array(m).fill(0);
for (const j of idx) {
while (i < n && items[i][0] <= queries[j]) {
mx = Math.max(mx, items[i][1]);
++i;
}
ans[j] = mx;
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Check every element from left to right until we find the target or exhaust the array. Each comparison is O(1), and we may visit all n elements, giving O(n). No extra space needed.
Each comparison eliminates half the remaining search space. After k comparisons, the space is n/2ᵏ. We stop when the space is 1, so k = log₂ n. No extra memory needed — just two pointers (lo, hi).
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Setting `lo = mid` or `hi = mid` can stall and create an infinite loop.
Usually fails on: Two-element ranges never converge.
Fix: Use `lo = mid + 1` or `hi = mid - 1` where appropriate.