Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions src/array/no_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,11 +206,50 @@ pub fn solve_sudoku(_board: &mut Vec<Vec<char>>) {
todo!()
}

/// [1793. 好子数组的最大分数](https://leetcode.cn/problems/maximum-score-of-a-good-subarray/?envType=daily-question&envId=2024-03-19)
///
/// 思路:
/// 1. 在不改变最小值的前提下, 延展最多的区间
/// 2. 然后对比这些区间
pub fn maximum_score(nums: Vec<i32>, k: i32) -> i32 {
unimplemented!("暂时不知道怎么解")
}

#[cfg(test)]
mod tests {
use super::*;
use crate::vec2;

#[test]
fn test_maximum_score(){
struct Testcase{
nums: Vec<i32>,
k: i32,
expect: i32,
}
vec![
Testcase{
nums: vec![1, 4, 3, 7, 4, 5],
k: 3,
expect: 15,
},
Testcase{
nums: vec![5, 5, 4, 5, 4, 1, 1, 1],
k: 0,
expect: 20,
},
Testcase{
nums: vec![8182,1273,9847,6230,52,1467,6062,726,4852,4507,2460,2041,500,1025,5524],
k: 8,
expect: 9014
}
].into_iter().enumerate().for_each(|(idx, testcase)|{
let Testcase{nums, k, expect} = testcase;
let actual = maximum_score(nums, k);
assert_eq!(expect, actual, "case {} failed", idx);
});
}

#[test]
#[ignore = "暂时不知道怎么解"]
fn test_solve_sudoku() {
Expand Down
92 changes: 91 additions & 1 deletion src/array/ser/binary_search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,7 @@ where
/// 要求序列满足 `[from, x]` cmp 返回 true, `[x+1, end]` 返回 false
/// 即一开始必须时 true
///
/// 返回的 `I` 不保证 `cmp(I) == true`
/// 返回的 `I` 不保证 `cmp(I) == false`
/// 这个函数保证的是 `(I, end]` cmp不会返回true, `(I, end]` 可能是空
///
/// 做了防溢出, `from`和`end`可以是0, 即传入索引
Expand Down Expand Up @@ -623,10 +623,100 @@ pub fn min_capability(nums: Vec<i32>, k: i32) -> i32 {
min
}


/// [2529. 正整数和负整数的最大计数](https://leetcode.cn/problems/maximum-count-of-positive-integer-and-negative-integer)
///
/// 注意: 0 既不是正整数也不是负整数
///
/// 思路: 利用单调性, 找到第一个正整数和最后一个负整数, 然后计算各自的长度
pub fn maximum_count(nums: Vec<i32>) -> i32 {
let first_positive = {
let tmp = first_occur(0, nums.len()-1, |i| nums[i] > 0);
if tmp == 0{
if nums[0] > 0{
0i32
} else {
1i32
}
} else if tmp == nums.len(){
nums.len() as i32
} else {
tmp as i32
}

};
let last_negative = {
let tmp = last_occur(0, nums.len()-1, |i| nums[i] < 0);
if tmp == 0 {
if nums[0] < 0{
1i32
} else {
0i32
}
} else if tmp == nums.len(){
nums.len() as i32
} else {
tmp as i32 + 1
}
};

let pos_len = nums.len() as i32 - first_positive;
let neg_len = last_negative;

pos_len.max(neg_len)
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_maximum_count(){
struct TestCase{
name: &'static str,
nums: Vec<i32>,
expect: i32,
}

vec![
TestCase{
name: "basic",
nums: vec![-5, -4, -3, -2, -1, 1, 2, 3, 4, 5],
expect: 5,
},
TestCase{
name: "basic 2",
nums: vec![-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5],
expect: 5,
},
TestCase{
name: "basic 3",
nums: vec![1, 2, 3, 4, 5],
expect: 5,
},
TestCase{
name: "basic 4",
nums: vec![-5, -4, -3, -2, -1],
expect: 5,
},
TestCase{
name: "basic 5",
nums: vec![0, 1, 2, 3, 4, 5],
expect: 5,
},
TestCase{
name: "basic 6",
nums: vec![-5, -4, -3, -2, -1, 0],
expect: 5,
},
]
.iter()
.for_each(|TestCase{name, nums, expect}|{
let actual = maximum_count(nums.to_vec());
assert_eq!(*expect, actual, "{} failed", name);
});
}

#[test]
fn test_min_capability() {
struct TestCase {
Expand Down
7 changes: 5 additions & 2 deletions src/dp/no_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,13 +338,16 @@ pub fn num_factored_binary_trees(mut arr: Vec<i32>) -> i32 {
result as i32
}



#[cfg(test)]
mod test {
use super::*;
use crate::vec2;

#[test]
fn test_combination_sum4(){

}

#[test]
fn test_num_factored_binary_trees() {
struct TestCase {
Expand Down
3 changes: 2 additions & 1 deletion src/dp/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ pub mod stair;
pub mod stock;
pub mod tree;
pub mod path;
pub mod rob;
pub mod rob;
pub mod combination;
66 changes: 66 additions & 0 deletions src/dp/ser/combination.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
//! 组合问题

/// [377. 组合总和 Ⅳ](https://leetcode.cn/problems/combination-sum-iv)
///
/// 思路:
/// 整体问题可以变成f(n) = f(n-nums[0]) + f(n-nums[1]) + ... + f(n-nums[i])
/// 但是这种朴素的递归操作, 会因为重复计算, 使整体的计算规模扩大
///
/// 可以和 [70. 爬楼梯](https://leetcode-cn.com/problems/climbing-stairs/) 一样, 用dp来解决
///
/// 起点一定是0, 从0开始, 递推到target
pub fn combination_sum4(nums: Vec<i32>, target: i32) -> i32 {
let mut dp = vec![0; target as usize + 1];
dp[0] = 1;
for i in 1..=target {
for &num in nums.iter() {
if i >= num {
dp[i as usize] += dp[(i - num) as usize];
}
}
}
dp[target as usize]
}

#[cfg(test)]
mod test {
use super::*;
use crate::vec2;

#[test]
fn test_combination_sum4() {
struct TestCase {
nums: Vec<i32>,
target: i32,
expected: i32,
}

vec![
TestCase {
nums: vec![1, 2, 3],
target: 4,
expected: 7,
},
TestCase {
nums: vec![9],
target: 3,
expected: 0,
},
]
.into_iter()
.enumerate()
.for_each(
|(
idx,
TestCase {
nums,
target,
expected,
},
)| {
let actual = combination_sum4(nums, target);
assert_eq!(expected, actual, "case {} failed", idx);
},
);
}
}
1 change: 0 additions & 1 deletion src/tree/dfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ mod tests {
use std::vec;

use super::*;
use datastructure::TreeNode;

#[test]
fn test_distance_k() {
Expand Down
2 changes: 1 addition & 1 deletion src/tree/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub mod dfs;
mod tests {
#[test]
fn test_macro() {
let t = macros::tree!({val: 1, left: {2, right: {3}}});
let t = macros::tree!(tree!{val: 16, left: {val: 8, left: {val: 1, right: {val: 2, right: {val: 7}}}, right: {val:12, left: {val: 9}}}, right: {val: 18, right: {val: 20}}});
dbg!(t);
}
}
Loading