Skip to content

Commit

Permalink
75: Submit
Browse files Browse the repository at this point in the history
  • Loading branch information
sankichi92 committed Jan 1, 2024
1 parent f100a2c commit 38ef9bf
Showing 1 changed file with 31 additions and 5 deletions.
36 changes: 31 additions & 5 deletions 75-online-stock-span/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,40 @@
pub fn add(left: usize, right: usize) -> usize {
left + right
pub struct StockSpanner {
prices: Vec<i32>,
}

impl StockSpanner {
#[allow(clippy::new_without_default)]
pub fn new() -> Self {
StockSpanner { prices: vec![] }
}

pub fn next(&mut self, price: i32) -> i32 {
let mut span = 1;
for prev_price in self.prices.iter().rev() {
if price >= *prev_price {
span += 1;
} else {
break;
}
}
self.prices.push(price);
span
}
}

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

#[test]
fn it_works() {
let result = add(2, 2);
assert_eq!(result, 4);
fn case1() {
let mut stock_spanner = StockSpanner::new();
assert_eq!(stock_spanner.next(100), 1);
assert_eq!(stock_spanner.next(80), 1);
assert_eq!(stock_spanner.next(60), 1);
assert_eq!(stock_spanner.next(70), 2);
assert_eq!(stock_spanner.next(60), 1);
assert_eq!(stock_spanner.next(75), 4);
assert_eq!(stock_spanner.next(85), 6);
}
}

0 comments on commit 38ef9bf

Please sign in to comment.