-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add problem 2146: K Highest Ranked Items Within a Price Range
- Loading branch information
Showing
3 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
src/problem_2146_k_highest_ranked_items_within_a_price_range/bfs_and_quick_select.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>(); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
src/problem_2146_k_highest_ranked_items_within_a_price_range/mod.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
); | ||
} | ||
} | ||
} |