increasing triplet subsequence

This commit is contained in:
Joe Ardent 2024-04-25 14:34:56 -07:00
parent 06b15921fd
commit 15fd997707
4 changed files with 39 additions and 8 deletions

View file

@ -66,15 +66,14 @@ fn parse_packet(scanner: &mut Scanner) -> Packet {
fn parse_list(scanner: &mut Scanner) -> Vec<Element> {
let mut out = Vec::new();
while !scanner.is_done() {
match scanner.peek().unwrap() {
while let Some(c) = scanner.peek() {
match c {
'[' => {
scanner.pop();
let v = parse_list(scanner);
out.push(Element::List(v));
}
c if c.is_ascii_digit() => {
//
let mut s = String::new();
while let Some(c) = scanner.peek() {
if c.is_ascii_digit() {

View file

@ -0,0 +1,6 @@
[package]
name = "increasing_triplet_subsequence"
version = "0.1.0"
edition = "2021"
[dependencies]

View 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
}

View file

@ -21,8 +21,8 @@ impl Solution {
}
Zeros::One(z) => {
let (left, right) = nums.split_at(z);
let left = if z == 0 { 1 } else { left.iter().product() };
let right = if z == nums.len() - 1 {
let left: i32 = left.iter().product();
let right: i32 = if right.len() < 2 {
1
} else {
right[1..].iter().product()
@ -32,7 +32,7 @@ impl Solution {
}
nums[z] = left * right;
}
_ => {}
Zeros::Many => *nums = vec![0; nums.len()],
}
out
}
@ -40,11 +40,12 @@ impl Solution {
fn count_zeros(nums: &[i32]) -> Zeros {
let mut out = Zeros::None;
let zs = 0;
let mut zs = false;
for (i, n) in nums.iter().enumerate() {
if *n == 0i32 {
if zs == 0 {
if !zs {
out = Zeros::One(i);
zs = true;
} else {
out = Zeros::Many;
break;
@ -57,4 +58,6 @@ fn count_zeros(nums: &[i32]) -> Zeros {
fn main() {
let p = Solution::product_except_self(vec![-1, 1, 0, -3, 3]);
dbg!(p);
let p = Solution::product_except_self(vec![1, 2, 0, 3]);
dbg!(p);
}