第 236 场周赛

1822. 数组元素积的符号

/**
 * @param {number[]} nums
 * @return {number}
 */
var arraySign = function(nums) {
    let cnt = 0;
    for(let i=0;i<nums.length;i++){
        if(nums[i]===0) return 0;
        if(nums[i]<0) cnt++;
    }
    
    if(cnt%2===0) return 1;
    else return -1;
};

1823. 找出游戏的获胜者

/**
 * @param {number} n
 * @param {number} k
 * @return {number}
 */
var findTheWinner = function(n, k) {
    let arr = [];
    for(let i=1;i<=n;i++){
        arr.push(i);
    }
    let idx = 0;
    let len = n;
    
    while(true) {
        if(arr.length === 1) return arr.pop();
        idx = (idx+k-1) % len;
        arr.splice(idx, 1);
        len--;
    }
};

1824. 最少侧跳次数

/**
 * @param {number[]} obstacles
 * @return {number}
 */
var minSideJumps = function(obstacles) {
    let cur=2, res=0, arr=obstacles;
    for(let i=0;i<arr.length;){
        if(arr[i+1]===0) i++;
        else if(arr[i+1]!==cur) i++;
        else if(arr[i]!==0 && arr[i+1]!==0) {
            let tmp = 0;
            for(let k=1;k<=3;k++) {
                if(k!==arr[i] && k!=cur && k!=arr[i+1]){
                    tmp=k;
                }
            }
            cur = tmp;
            res++;
        }
        else {
            let next = 0;
            for(let m=i+1;m<arr.length;m++){
              if(arr[m]!==0&&arr[m]!==cur){
                    next=arr[m];
                    break;
                }  
            }
                
            for(let j=1;j<=3;j++) {
                if(next===0 && j!==cur) {
                    cur=j;
                    break;
                }
                else if(next!=j && cur!=j){
                    cur=j;
                    break;
                }
            }
            res++;
        }
    }
    return res;
};

第四题不会,线段树做法来源自idonteatcookie
1825. 求出 MK 平均值

function SegTree(left, right) {
    this.left = left;     // 节点的左端点
    this.right = right;    // 节点的右端点
    this.leftChild = null;
    this.rightChild = null;
    this.countSum = 0;
    this.valueSum = 0;
}
SegTree.buildTree = function buildTree(left, right) {
    const node = new SegTree(left, right);
    if (left !== right) {
        const mid = (left + right) >> 1;
        node.leftChild = buildTree(left, mid);
        node.rightChild = buildTree(mid + 1, right);
    }
    return node;
}
SegTree.prototype.isLeafNode = function() {
    return !this.leftChild;
}
SegTree.prototype.includes = function(position) {
    return this.left <= position && this.right >= position;
}
SegTree.prototype.pushUp = function() {
    this.valueSum = this.leftChild.valueSum + this.rightChild.valueSum;
    this.countSum = this.leftChild.countSum + this.rightChild.countSum;
}
// 在 position 处增加 addCount 个数字
SegTree.prototype.update = function(position, addCount) {
    if (this.isLeafNode()) {
        this.countSum += addCount;
        this.valueSum += addCount * position;
        return;
    }
    if (this.leftChild.includes(position)) {
        this.leftChild.update(position, addCount);
    } else {
        this.rightChild.update(position, addCount);
    }
    this.pushUp();
}
// 查询前 count 个数的和
SegTree.prototype.query = function (count) {
    if (this.isLeafNode()) return this.left * count;
    if (this.countSum === count) return this.valueSum;
    if (this.leftChild.countSum === count) return this.leftChild.valueSum;
    if (this.leftChild.countSum < count)
        return this.leftChild.valueSum + this.rightChild.query(count - this.leftChild.countSum);
    return this.leftChild.query(count);
}

/**
 * @param {number} m
 * @param {number} k
 */
var MKAverage = function(m, k) {
    // n <= 100000
    this.m = m;
    this.k = k;
    this.segTree = SegTree.buildTree(1, 10000);
    this.queue = [];
};

/**
 * @param {number} num
 * @return {void}
 */
MKAverage.prototype.addElement = function(num) {
    this.queue.push(num);
    this.segTree.update(num, 1);
    if (this.queue.length > this.m) {
        this.segTree.update(this.queue.shift(), -1);
    }
};

/**
 * @return {number}
 */
MKAverage.prototype.calculateMKAverage = function() {
    const { m, k } = this;
    if (this.queue.length < m) return -1;
    const minK = this.segTree.query(k);
    const minM_K = this.segTree.query(m - k);
    return Math.floor((minM_K - minK) / (m - 2 * k));
};

/**
 * Your MKAverage object will be instantiated and called as such:
 * var obj = new MKAverage(m, k)
 * obj.addElement(num)
 * var param_2 = obj.calculateMKAverage()
 */
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值