for (let i = path.at(-1) + 1; i < this.nums.length; i++) { if (Math.abs(this.nums[i] - this.nums[path.at(-1)]) <= this.k) { path.push(i); this.fn(path); path.pop(); } } } } let res;
let nums_size = readInt();
let nums = newArray(); let nums_item; for (let nums_i = 0; nums_i < nums_size; nums_i++) { nums.push(readInt()); } let k = readInt(); let acmSolution = newSolution(); res = acmSolution.maximumSteps(nums, k); print(res);
classSolution { /* Write Code Here */ maximumOccurrences(nums, k) { let maxnum = 1; // nums.sort(); for (let i = 0; i < nums.length; i++) { let curnum = 0; for (let j = 0; j < nums.length; j++) { if (Math.abs(nums[i] - nums[j]) <= k) { curnum++; } } maxnum = Math.max(curnum, maxnum); } return maxnum; } } let res;
let nums_size = readInt();
let nums = newArray(); let nums_item; for (let nums_i = 0; nums_i < nums_size; nums_i++) { nums.push(readInt()); } let k = readInt(); let acmSolution = newSolution(); res = acmSolution.maximumOccurrences(nums, k); print(res);
通过率88%,其他的兴许是超时了。
总结
这次算法题挺简单,都算是力扣中等难度。
虽然牛客是acm模式,但是提前写好了IO处理,好评!
hinak0
Recording personal notes, covering various aspects.