Skip to content

Commit

Permalink
Add problem 2146: K Highest Ranked Items Within a Price Range
Browse files Browse the repository at this point in the history
  • Loading branch information
EFanZh committed Sep 5, 2024
1 parent f84e0a2 commit 826374a
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1583,6 +1583,7 @@ pub mod problem_2139_minimum_moves_to_reach_target_score;
pub mod problem_2140_solving_questions_with_brainpower;
pub mod problem_2144_minimum_cost_of_buying_candies_with_discount;
pub mod problem_2145_count_the_hidden_sequences;
pub mod problem_2146_k_highest_ranked_items_within_a_price_range;
pub mod problem_2147_number_of_ways_to_divide_a_long_corridor;
pub mod problem_2148_count_elements_with_strictly_smaller_and_greater_elements;
pub mod problem_2149_rearrange_array_elements_by_sign;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
pub struct Solution;

// ------------------------------------------------------ snip ------------------------------------------------------ //

use std::collections::VecDeque;
use std::mem;

impl Solution {
pub fn highest_ranked_k_items(grid: Vec<Vec<i32>>, pricing: Vec<i32>, start: Vec<i32>, k: i32) -> Vec<Vec<i32>> {
let mut grid = grid;
let rows = grid.len();
let columns = grid.first().map_or(0, Vec::len);
let [low, high]: [_; 2] = pricing.try_into().ok().unwrap();
let [start_row, start_col]: [_; 2] = start.try_into().ok().unwrap();
let price_range = low as u32..=high as u32;
let k = k as u32 as usize;
let node = (start_row as u32, start_col as u32);
let mut distance = 1;
let mut queue = VecDeque::from([node]);
let mut candidates = Vec::<(u32, u32, u32, u32)>::with_capacity(columns * rows);
let start_price = mem::take(&mut grid[node.0 as usize][node.1 as usize]) as _;

if price_range.contains(&start_price) {
candidates.push((0, start_price, node.0, node.1));
}

loop {
for _ in 0..queue.len() {
let node = queue.pop_front().unwrap();

for next in [
(node.0.wrapping_sub(1), node.1),
(node.0, node.1.wrapping_sub(1)),
(node.0, node.1 + 1),
(node.0 + 1, node.1),
] {
if let Some(cell) = grid
.get_mut(next.0 as usize)
.and_then(|row| row.get_mut(next.1 as usize))
{
if *cell != 0 {
let price = mem::take(cell) as _;

if price_range.contains(&price) {
candidates.push((distance, price, next.0, next.1));
}

queue.push_back(next);
}
}
}
}

if queue.is_empty() {
break;
}

distance += 1;
}

if k < candidates.len() {
candidates.select_nth_unstable(k);
candidates.truncate(k);
}

candidates.sort_unstable();

candidates
.into_iter()
.map(|(_, _, row, column)| vec![row as _, column as _])
.collect()
}
}

// ------------------------------------------------------ snip ------------------------------------------------------ //

impl super::Solution for Solution {
fn highest_ranked_k_items(grid: Vec<Vec<i32>>, pricing: Vec<i32>, start: Vec<i32>, k: i32) -> Vec<Vec<i32>> {
Self::highest_ranked_k_items(grid, pricing, start, k)
}
}

#[cfg(test)]
mod tests {
#[test]
fn test_solution() {
super::super::tests::run::<super::Solution>();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
pub mod bfs_and_quick_select;

pub trait Solution {
fn highest_ranked_k_items(grid: Vec<Vec<i32>>, pricing: Vec<i32>, start: Vec<i32>, k: i32) -> Vec<Vec<i32>>;
}

#[cfg(test)]
mod tests {
use super::Solution;
use crate::test_utilities::Matrix;

pub fn run<S: Solution>() {
let test_cases = [
(
(
&[[1, 2, 0, 1], [1, 3, 0, 1], [0, 2, 5, 1]] as &dyn Matrix<_>,
[2, 5],
[0, 0],
3,
),
&[[0, 1], [1, 1], [2, 1]] as &[_],
),
(
(&[[1, 2, 0, 1], [1, 3, 3, 1], [0, 2, 5, 1]], [2, 3], [2, 3], 2),
&[[2, 1], [1, 2]],
),
(
(&[[1, 1, 1], [0, 0, 1], [2, 3, 4]], [2, 3], [0, 0], 3),
&[[2, 1], [2, 0]],
),
(
(&[[1, 0, 1], [3, 5, 2], [1, 0, 1]], [2, 5], [1, 1], 9),
&[[1, 1], [1, 2], [1, 0]],
),
];

for ((grid, pricing, start, k), expected) in test_cases {
assert_eq!(
S::highest_ranked_k_items(grid.to_vec(), pricing.to_vec(), start.to_vec(), k),
expected,
);
}
}
}

0 comments on commit 826374a

Please sign in to comment.