increasing triplet subsequence
This commit is contained in:
parent
06b15921fd
commit
15fd997707
4 changed files with 39 additions and 8 deletions
|
@ -66,15 +66,14 @@ fn parse_packet(scanner: &mut Scanner) -> Packet {
|
||||||
|
|
||||||
fn parse_list(scanner: &mut Scanner) -> Vec<Element> {
|
fn parse_list(scanner: &mut Scanner) -> Vec<Element> {
|
||||||
let mut out = Vec::new();
|
let mut out = Vec::new();
|
||||||
while !scanner.is_done() {
|
while let Some(c) = scanner.peek() {
|
||||||
match scanner.peek().unwrap() {
|
match c {
|
||||||
'[' => {
|
'[' => {
|
||||||
scanner.pop();
|
scanner.pop();
|
||||||
let v = parse_list(scanner);
|
let v = parse_list(scanner);
|
||||||
out.push(Element::List(v));
|
out.push(Element::List(v));
|
||||||
}
|
}
|
||||||
c if c.is_ascii_digit() => {
|
c if c.is_ascii_digit() => {
|
||||||
//
|
|
||||||
let mut s = String::new();
|
let mut s = String::new();
|
||||||
while let Some(c) = scanner.peek() {
|
while let Some(c) = scanner.peek() {
|
||||||
if c.is_ascii_digit() {
|
if c.is_ascii_digit() {
|
||||||
|
|
6
increasing_triplet_subsequence/Cargo.toml
Normal file
6
increasing_triplet_subsequence/Cargo.toml
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
[package]
|
||||||
|
name = "increasing_triplet_subsequence"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
23
increasing_triplet_subsequence/src/main.rs
Normal file
23
increasing_triplet_subsequence/src/main.rs
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
fn main() {
|
||||||
|
dbg!(increasing_triplet(&[1, 5, 2, 3]));
|
||||||
|
}
|
||||||
|
|
||||||
|
fn increasing_triplet(nums: &[i32]) -> bool {
|
||||||
|
let mut sub_seq = [i64::MAX; 4];
|
||||||
|
sub_seq[0] = i64::MIN;
|
||||||
|
for &v in nums {
|
||||||
|
let v = v as i64;
|
||||||
|
for j in 1..4 {
|
||||||
|
if sub_seq[j - 1] < v && v < sub_seq[j] {
|
||||||
|
sub_seq[j] = v;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let mut m = 0;
|
||||||
|
for &v in &sub_seq[1..] {
|
||||||
|
if v < i64::MAX {
|
||||||
|
m += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
m > 2
|
||||||
|
}
|
|
@ -21,8 +21,8 @@ impl Solution {
|
||||||
}
|
}
|
||||||
Zeros::One(z) => {
|
Zeros::One(z) => {
|
||||||
let (left, right) = nums.split_at(z);
|
let (left, right) = nums.split_at(z);
|
||||||
let left = if z == 0 { 1 } else { left.iter().product() };
|
let left: i32 = left.iter().product();
|
||||||
let right = if z == nums.len() - 1 {
|
let right: i32 = if right.len() < 2 {
|
||||||
1
|
1
|
||||||
} else {
|
} else {
|
||||||
right[1..].iter().product()
|
right[1..].iter().product()
|
||||||
|
@ -32,7 +32,7 @@ impl Solution {
|
||||||
}
|
}
|
||||||
nums[z] = left * right;
|
nums[z] = left * right;
|
||||||
}
|
}
|
||||||
_ => {}
|
Zeros::Many => *nums = vec![0; nums.len()],
|
||||||
}
|
}
|
||||||
out
|
out
|
||||||
}
|
}
|
||||||
|
@ -40,11 +40,12 @@ impl Solution {
|
||||||
|
|
||||||
fn count_zeros(nums: &[i32]) -> Zeros {
|
fn count_zeros(nums: &[i32]) -> Zeros {
|
||||||
let mut out = Zeros::None;
|
let mut out = Zeros::None;
|
||||||
let zs = 0;
|
let mut zs = false;
|
||||||
for (i, n) in nums.iter().enumerate() {
|
for (i, n) in nums.iter().enumerate() {
|
||||||
if *n == 0i32 {
|
if *n == 0i32 {
|
||||||
if zs == 0 {
|
if !zs {
|
||||||
out = Zeros::One(i);
|
out = Zeros::One(i);
|
||||||
|
zs = true;
|
||||||
} else {
|
} else {
|
||||||
out = Zeros::Many;
|
out = Zeros::Many;
|
||||||
break;
|
break;
|
||||||
|
@ -57,4 +58,6 @@ fn count_zeros(nums: &[i32]) -> Zeros {
|
||||||
fn main() {
|
fn main() {
|
||||||
let p = Solution::product_except_self(vec![-1, 1, 0, -3, 3]);
|
let p = Solution::product_except_self(vec![-1, 1, 0, -3, 3]);
|
||||||
dbg!(p);
|
dbg!(p);
|
||||||
|
let p = Solution::product_except_self(vec![1, 2, 0, 3]);
|
||||||
|
dbg!(p);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue