/**
* @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;
};
/**
* @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--;
}
};
/**
* @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()
*/