Skip to content

Latest commit

 

History

History
27 lines (22 loc) · 694 Bytes

1558. 得到目标数组的最少函数调用次数.md

File metadata and controls

27 lines (22 loc) · 694 Bytes
  • 归纳总结
function minOperations(nums: number[]): number {
    let divTwoTimes: number = 0,
        plusOneTimes: number = 0;

    for (let num of nums) {
        let currentDiv2Times: number = 0;
        while (num) {
            if (num & 1) {
                ++plusOneTimes;
                --num;
            } else {
                ++currentDiv2Times;
                num >>>= 1;
            }
        }
        divTwoTimes = Math.max(divTwoTimes, currentDiv2Times);
    }

    return plusOneTimes + divTwoTimes;
};