阅读 136

LeetCode第20天🐱‍🏍

1894. 找到需要补充粉笔的学生编号

var chalkReplacer = function(chalk, k) {
    let sum = 0
    for (const num of chalk) {
        sum += num
    }
    k %= sum
    for (let i = 0; i < chalk.length; i++) {
        if (chalk[i] > k) return i
        k -= chalk[i]
    }
};
复制代码

42. 接雨水

var trap = function(height) {
    let l = 0, 
        r = height.length - 1, 
        lmax = height[l], 
        rmax = height[r],
        res = 0;
    while (l < r) {
        if (height[l] < height[r]) {
            l++
            lmax = Math.max(lmax, height[l])
            res += lmax - height[l]
        }
        else {
            r--
            rmax = Math.max(rmax, height[r])
            res += rmax - height[r]
        }
    }
    return res
};
复制代码
文章分类
前端