File size: 1,515 Bytes
4c0e24e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
fn merge_capped_exec(a: &Vec<i32>, b: &Vec<i32>, cap: usize) -> Vec<i32> {
    let mut result: Vec<i32> = Vec::new();
    let mut i: usize = 0;
    let mut j: usize = 0;
    while result.len() < cap && (i < a.len() || j < b.len()) {
        if j >= b.len() || (i < a.len() && a[i] <= b[j]) {
            result.push(a[i]);
            i += 1;
        } else {
            result.push(b[j]);
            j += 1;
        }
    }
    result
}

fn shift_exec(s: &Vec<i32>, shift: i32) -> Vec<i32> {
    let mut result: Vec<i32> = Vec::new();
    let mut idx: usize = 0;
    while idx < s.len() {
        result.push(s[idx] + shift);
        idx += 1;
    }
    result
}

fn fold_cols_exec(mat: &Vec<Vec<i32>>, row: usize, tail: &Vec<i32>, cap: usize) -> Vec<i32> {
    let n = mat[row].len();
    let mut acc: Vec<i32> = Vec::new();
    let mut c: usize = n;
    while c > 0 {
        c -= 1;
        let shifted = shift_exec(tail, mat[row][c]);
        acc = merge_capped_exec(&shifted, &acc, cap);
    }
    acc
}

fn capped_sums_exec(mat: &Vec<Vec<i32>>, cap: usize) -> Vec<i32> {
    let mut tail: Vec<i32> = Vec::new();
    if cap >= 1 {
        tail.push(0);
    }
    let m = mat.len();
    let mut row: usize = m;
    while row > 0 {
        row -= 1;
        tail = fold_cols_exec(mat, row, &tail, cap);
    }
    tail
}

impl Solution {
    pub fn kth_smallest(mat: Vec<Vec<i32>>, k: i32) -> i32 {
        let l = capped_sums_exec(&mat, k as usize);
        let ans = l[(k - 1) as usize];
        ans
    }
}