Merge branch 'master' of git.rrricardo.top:jackfiled/leetcode
This commit is contained in:
commit
b5c93ffb61
|
@ -79,4 +79,7 @@ mod p2684_maximum_number_of_moves_in_a_grid;
|
|||
mod p310_minimum_height_trees;
|
||||
mod p303_range_sum_query_immutable;
|
||||
mod p1793_maximum_score_of_a_good_subarray;
|
||||
mod p1969_minimum_non_zero_product_of_the_array_elements;
|
||||
mod p2671_frequency_tracker;
|
||||
mod p2617_minimum_number_of_visited_cells_in_a_grid;
|
||||
mod p2549_count_distinct_numbers_on_board;
|
|
@ -0,0 +1,55 @@
|
|||
use std::convert::TryInto;
|
||||
|
||||
/**
|
||||
* [1969] Minimum Non-Zero Product of the Array Elements
|
||||
*/
|
||||
pub struct Solution {}
|
||||
|
||||
|
||||
// submission codes start here
|
||||
|
||||
impl Solution {
|
||||
pub fn min_non_zero_product(p: i32) -> i32 {
|
||||
let p = p as i64;
|
||||
if p == 1 {
|
||||
return 1;
|
||||
}
|
||||
|
||||
let m = 1e9 as i64 + 7;
|
||||
let x = Solution::fast_power(2, p, m) - 1;
|
||||
let y = 1 << (p - 1);
|
||||
dbg!(x, y);
|
||||
|
||||
return (Solution::fast_power(x - 1, y - 1, m) * x % m).try_into().unwrap();
|
||||
}
|
||||
|
||||
fn fast_power(x: i64, n: i64, m: i64) -> i64 {
|
||||
let mut result = 1;
|
||||
let mut n = n;
|
||||
let mut x = x;
|
||||
|
||||
while n != 0 {
|
||||
if n & 1 != 0 {
|
||||
result = result * x % m;
|
||||
}
|
||||
|
||||
x = x * x % m;
|
||||
n = n >> 1;
|
||||
}
|
||||
|
||||
result
|
||||
}
|
||||
}
|
||||
|
||||
// submission codes end
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_1969() {
|
||||
assert_eq!(1, Solution::min_non_zero_product(1));
|
||||
assert_eq!(6, Solution::min_non_zero_product(2));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,71 @@
|
|||
/**
|
||||
* [2617] Minimum Number of Visited Cells in a Grid
|
||||
*/
|
||||
pub struct Solution {}
|
||||
|
||||
|
||||
// submission codes start here
|
||||
use std::{cmp::Reverse, collections::BinaryHeap};
|
||||
|
||||
impl Solution {
|
||||
pub fn minimum_visited_cells(grid: Vec<Vec<i32>>) -> i32 {
|
||||
let height = grid.len();
|
||||
let width = grid[0].len();
|
||||
|
||||
let mut column_heaps: Vec<BinaryHeap<(Reverse<i32>, usize)>> = vec![BinaryHeap::new(); width];
|
||||
let mut row_heap: BinaryHeap<(Reverse<i32>, usize)> = BinaryHeap::new();
|
||||
|
||||
let mut dis = 0;
|
||||
for (i, row) in grid.iter().enumerate() {
|
||||
row_heap.clear();
|
||||
|
||||
for (j, &node) in row.iter().enumerate() {
|
||||
while !row_heap.is_empty() && row_heap.peek().unwrap().1 < j {
|
||||
row_heap.pop();
|
||||
}
|
||||
|
||||
let column_heap = &mut column_heaps[j];
|
||||
while !column_heap.is_empty() && column_heap.peek().unwrap().1 < i {
|
||||
column_heap.pop();
|
||||
}
|
||||
|
||||
dis = if i > 0 || j > 0 {
|
||||
i32::MAX
|
||||
} else {
|
||||
1
|
||||
};
|
||||
|
||||
if let Some((d, _)) = row_heap.peek() {
|
||||
dis = d.0 + 1;
|
||||
}
|
||||
|
||||
if let Some((d, _)) = column_heap.peek() {
|
||||
dis = dis.min(d.0 + 1);
|
||||
}
|
||||
|
||||
if node > 0 && dis < i32::MAX {
|
||||
let node = node as usize;
|
||||
row_heap.push((Reverse(dis), node + j));
|
||||
column_heap.push((Reverse(dis), node + i));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if dis < i32::MAX {
|
||||
dis
|
||||
} else {
|
||||
-1
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// submission codes end
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_2617() {
|
||||
}
|
||||
}
|
104
src/problem/p2671_frequency_tracker.rs
Normal file
104
src/problem/p2671_frequency_tracker.rs
Normal file
|
@ -0,0 +1,104 @@
|
|||
/**
|
||||
* [2671] Frequency Tracker
|
||||
*/
|
||||
pub struct Solution {}
|
||||
|
||||
|
||||
// submission codes start here
|
||||
use std::collections::{HashMap, HashSet};
|
||||
|
||||
struct FrequencyTracker {
|
||||
number_map : HashMap<i32, i32>,
|
||||
frequency_map : HashMap<i32, HashSet<i32>>,
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* `&self` means the method takes an immutable reference.
|
||||
* If you need a mutable reference, change it to `&mut self` instead.
|
||||
*/
|
||||
impl FrequencyTracker {
|
||||
|
||||
fn new() -> Self {
|
||||
FrequencyTracker {
|
||||
number_map: HashMap::new(),
|
||||
frequency_map: HashMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
fn add(&mut self, number: i32) {
|
||||
let mut entry = self.number_map.entry(number).or_insert(0);
|
||||
|
||||
if *entry != 0 {
|
||||
if let Some(set) = self.frequency_map.get_mut(&entry) {
|
||||
set.remove(&number);
|
||||
}
|
||||
}
|
||||
|
||||
*entry += 1;
|
||||
let set = self.frequency_map.entry(*entry)
|
||||
.or_insert(HashSet::new());
|
||||
set.insert(number);
|
||||
}
|
||||
|
||||
fn delete_one(&mut self, number: i32) {
|
||||
if let Some(entry) = self.number_map.get_mut(&number) {
|
||||
if *entry == 0 {
|
||||
return;
|
||||
}
|
||||
|
||||
if let Some(set) = self.frequency_map.get_mut(&entry) {
|
||||
set.remove(&number);
|
||||
}
|
||||
|
||||
*entry -= 1;
|
||||
let set = self.frequency_map.entry(*entry)
|
||||
.or_insert(HashSet::new());
|
||||
set.insert(number);
|
||||
}
|
||||
}
|
||||
|
||||
fn has_frequency(&self, frequency: i32) -> bool {
|
||||
let set = self.frequency_map.get(&frequency);
|
||||
|
||||
match set {
|
||||
Some(set) => set.len() != 0,
|
||||
None => false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Your FrequencyTracker object will be instantiated and called as such:
|
||||
* let obj = FrequencyTracker::new();
|
||||
* obj.add(number);
|
||||
* obj.delete_one(number);
|
||||
* let ret_3: bool = obj.has_frequency(frequency);
|
||||
*/
|
||||
|
||||
// submission codes end
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_2671() {
|
||||
let mut tracker = FrequencyTracker::new();
|
||||
|
||||
tracker.add(10);
|
||||
tracker.delete_one(5);
|
||||
assert!(tracker.has_frequency(1));
|
||||
tracker.delete_one(10);
|
||||
tracker.delete_one(9);
|
||||
tracker.delete_one(10);
|
||||
assert!(!tracker.has_frequency(1));
|
||||
tracker.add(4);
|
||||
assert!(tracker.has_frequency(1));
|
||||
tracker.delete_one(4);
|
||||
assert!(!tracker.has_frequency(1));
|
||||
assert!(!tracker.has_frequency(1));
|
||||
tracker.add(10);
|
||||
assert!(tracker.has_frequency(1));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user