【leetcode】LCS 01. Download plug-in (js implementation)

1. Topic

LCS 01. Download plugin
insert image description here

2. Idea (greedy algorithm)

insert image description here

3. Code implementation

/**
 * @param {number} n
 * @return {number}
 */
var leastMinutes = function(n) {
    
    
    let cnt = 0, v = 1, min = Infinity // 升级次数、初始网速、最少时间
    while (v <= n) {
    
    
        // 贪心,先提速再下载
        min = Math.min(min, cnt + Math.ceil(n / v))
        cnt++
        v *= 2
    }
    return min
};

4. Reference

JS: greedy ideas + code

Guess you like

Origin blog.csdn.net/weixin_44109827/article/details/129434873