File size: 922 Bytes
75cb225
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
impl Solution {
    pub fn max_score(a: Vec<i64>, s: Vec<u8>) -> i64 {
        let n = a.len();
        let mut prefix: Vec<i64> = Vec::with_capacity(n + 1);
        prefix.push(0);
        let mut i: usize = 0;
        while i < n {
            let next = prefix[i] + a[i];
            prefix.push(next);
            i = i + 1;
        }
        let mut total: i64 = 0;
        let mut lo: usize = 0;
        let mut hi: usize = n;
        let mut keep_going: bool = true;
        while keep_going {
            while lo < hi && s[lo] != 1 {
                lo = lo + 1;
            }
            while lo < hi && s[hi - 1] != 2 {
                hi = hi - 1;
            }
            if lo + 1 < hi {
                total = total + prefix[hi] - prefix[lo];
                lo = lo + 1;
                hi = hi - 1;
            } else {
                keep_going = false;
            }
        }
        total
    }
}