Trying a simple leetcode

Hello everyone, this is my first post here and I don't know if this is the right place to do so but, I have a simple question on leetcode: Loading... , basically for any vector of 0 and 1 you need to return an integer corresponding to the number of simultaneous 1. ex: for [0,1,0,1,1,0,1,1] the correct answer is 2.

My code seems to compile but gives 0. what do I miss here?

fn main() {
println!("{}", find_max_consecutive_ones([0,1,1,0,0,1,1,0,1,0].to_vec()));
}

pub fn find_max_consecutive_ones(nums: Vec<i32>) -> i32 {
        let mut counter = 0;
        for num in nums {
        if num == 1 && num+1 == 1 {
            counter += 1;
        }}
        
        return counter;
}

(Playground)

Output:

0

Errors:

   Compiling playground v0.0.1 (/playground)
    Finished dev [unoptimized + debuginfo] target(s) in 0.62s
     Running `target/debug/playground`

num == 1 && num+1 == 1

This is never true. Try use windows or array_windows.

2 Likes

You don't need to check adjacent numbers in any one loop iteration. It's sufficient to only look at the current number in isolation and check whether it's a 1 or a 0:

  • If it's a 1, increment the running count.
  • If it's a 0, reset the running count to 0.

You also need to keep track of the highest value the running count reaches. That's the trickiest part. There might be an edge case or two that trips you up, depending on how you go about it.

1 Like

You can use scan function on iterators to get this result.

fn main() {
    let nums = vec![1,1,0,1,1,1];
    let res = nums.iter().scan(0, |state, &x| {
        let r = if x == 0 {
            *state = 0;
            *state
        } else {
            *state = *state + 1;
            *state
        };
        Some(r)
    }).max().unwrap();
    dbg!(res);
}

Link to playground

num + 1 literally means the number plus one. It's not the next element in the vector. You are confusing indexes and elements.

Building on @jkugelman's suggestion:

pub fn find_max_consecutive_ones(nums: Vec<i32>) -> i32 {
    let mut longest = 0;
    let mut counter = 0;
 
    for num in nums {
        if num == 1 {
            counter += 1;
        } else {
            longest = longest.max(counter);
            counter = 0;
        }
    }
        
    longest.max(counter)
}
2 Likes

This is almost correct. It doesn't handle the case where the longest sequence of 1's is at the end and there's no trailing 0. For example: [1] or [1, 0, 1, 1].

Tricky. :slight_smile:

Good catch, it now does!

Thank you everyone for your replies!

So yes I get it with your methods, but I would like to find it more about why isn’t it working the way I tried

“ num + 1 literally means the number plus one. It's not the next element in the vector. You are confusing indexes and elements”

Can someone explain it to me more deeply like what did I try do do? What represent num and num +1 in my code?

Thank you again !

There's nothing more deep here. The variable num is the induction variable of the for loop, hence it gets successive elements of the vector being iterated over. If you add 1 to it, well, then you added 1 to it. If it was a 0, you get a 1, if it was a 1, you get a 2. You don't get the next element of the vector by adding 1 to the current element. (You would get the next index by adding 1 to the current index, but your code doesn't deal with any indices in any way.)