不积跬步,无以至千里;不积小流,无以成江海
时间:2020-5-15 参赛入口:https://leetcode.cn/contest/weekly-contest-293/ 讨论: 代码地址:
A: 移除字母异位词后的结果数组
思路:字符统计
关键点:
function removeAnagrams(words: string[]): string[] {
const n = words.length;
let ans = [];
ans.push(words[0]);
let pre = countWord(words[0]).join('');
for (let i = 1; i < n; i++) {
let cur = countWord(words[i]).join('');
if (pre !== cur) {
ans.push(words[i]);
pre = cur;
}
}
return ans;
};
function countWord (word: string): number[] {
let count = new Array(128).fill(0);
for (let i = 0; i < word.length; i++) {
count[word.charCodeAt(i)]++;
}
return count;
}
总结:
B: 不含特殊楼层的最大连续楼层数
思路:排序
关键点:
function maxConsecutive(bottom: number, top: number, special: number[]): number {
let nums = special.slice().sort((a, b) => a - b);
nums.unshift(bottom - 1);
nums.push(top + 1);
let ans = 0;
const n = nums.length;
for (let i = 1; i < n; i++) {
ans = Math.max(ans, nums[i] - nums[i - 1] - 1);
}
return ans;
};
总结:
C: 按位与结果大于零的最长组合
思路:逐个考虑每一个比特位
关键点:
由于 2^{23} < 10^7<2^{24},枚举比特位可以从 0 枚举到 23
function largestCombination(candidates: number[]): number {
const n = 24;
let ans = 0;
for (let i = 0; i < n; i++) {
let count = 0;
for (let num of candidates) {
if (num >> i & 1) count++;
}
ans = Math.max(ans, count);
}
return ans;
};
总结:
D: 统计区间中的整数数目
思路:(动态开点)线段树
关键点:
class CountIntervals {
left: null | CountIntervals;
right: null | CountIntervals;
start: number;
end: number;
sum: number;
constructor(start: number = 0, end: number = 10 ** 9) {
this.left = null;
this.right = null;
this.start = start;
this.end = end;
this.sum = 0;
}
add(left: number, right: number): void {
if (this.sum == (this.end - this.start + 1)) return;
if (left <= this.start && right >= this.end) {
this.sum = this.end - this.start + 1;
return;
}
let mid = (this.start + this.end) >> 1;
if (!this.left) this.left = new CountIntervals(this.start, mid);
if (!this.right) this.right = new CountIntervals(mid + 1, this.end);
if (left <= mid) this.left.add(left, right);
if (right > mid) this.right.add(left, right);
this.sum = this.left.sum + this.right.sum;
}
count(): number {
return this.sum;
}
}
/**
* Your CountIntervals object will be instantiated and called as such:
* var obj = new CountIntervals()
* obj.add(left,right)
* var param_2 = obj.count()
*/
总结:
715. Range 模块
复盘
c 初步分析采用BFS, 存值出现问题卡壳。原因对位运算, 缺乏本质理解。
d 初步分析, 采用合并区间。 原因线段树类型未涉及过, 方案针对类型训练。