-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add problem 1498: Number of Subsequences That Satisfy the Given Sum C…
…ondition
- Loading branch information
Showing
3 changed files
with
69 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
23 changes: 23 additions & 0 deletions
23
src/problem_1498_number_of_subsequences_that_satisfy_the_given_sum_condition/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,23 @@ | ||
pub mod two_pointer_and_math; | ||
|
||
pub trait Solution { | ||
fn num_subseq(nums: Vec<i32>, target: i32) -> i32; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::Solution; | ||
|
||
pub fn run<S: Solution>() { | ||
let test_cases = [ | ||
((&[3, 5, 6, 7] as &[_], 9), 4), | ||
((&[3, 3, 6, 8], 10), 6), | ||
((&[2, 3, 3, 4, 6, 7], 12), 61), | ||
((&[5, 2, 4, 1, 7, 6, 8], 16), 127), | ||
]; | ||
|
||
for ((nums, target), expected) in test_cases { | ||
assert_eq!(S::num_subseq(nums.to_vec(), target), expected); | ||
} | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
..._1498_number_of_subsequences_that_satisfy_the_given_sum_condition/two_pointer_and_math.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,45 @@ | ||
pub struct Solution; | ||
|
||
impl Solution { | ||
pub fn num_subseq(nums: Vec<i32>, target: i32) -> i32 { | ||
let modulo = 1_000_000_007; | ||
|
||
let mut result = 0; | ||
let mut nums = nums; | ||
nums.sort_unstable(); | ||
|
||
let mut pow2 = vec![1; nums.len()]; | ||
for i in 1..nums.len() { | ||
pow2[i] = (pow2[i - 1] * 2) % modulo; | ||
} | ||
|
||
let (mut left, mut right) = (0, nums.len() - 1); | ||
while left <= right { | ||
if nums[left] + nums[right] <= target { | ||
result = (result + pow2[right - left]) % modulo; | ||
left += 1; | ||
} else { | ||
if right == 0 { | ||
break; | ||
}; | ||
right -= 1; | ||
} | ||
} | ||
|
||
result | ||
} | ||
} | ||
|
||
impl super::Solution for Solution { | ||
fn num_subseq(nums: Vec<i32>, target: i32) -> i32 { | ||
Self::num_subseq(nums, target) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn test_solution() { | ||
super::super::tests::run::<super::Solution>(); | ||
} | ||
} |