LeetCode 218. The Skyline Problem--Divide and Conquer(分治法)

本文详细解析了一道关于计算城市天际线的算法题,通过分治法和归并排序的思想,实现了高效求解不同建筑物集合形成的天际线轮廓。文章提供了完整的代码实现,并附带详细的解释。

题目链接

218. The Skyline Problem

A city's skyline is the outer contour of the silhouette formed by all the buildings in that city when viewed from a distance. Now suppose you are given the locations and height of all the buildings as shown on a cityscape photo (Figure A), write a program to output the skyline formed by these buildings collectively (Figure B).


           


The geometric information of each building is represented by a triplet of integers [Li, Ri, Hi], where Li and Ri are the x coordinates of the left and right edge of the ith building, respectively, and Hi is its height. It is guaranteed that 0 ≤ Li, Ri ≤ INT_MAX0 < Hi ≤ INT_MAX, and Ri - Li > 0. You may assume all buildings are perfect rectangles grounded on an absolutely flat surface at height 0.

For instance, the dimensions of all buildings in Figure A are recorded as: [ [2 9 10], [3 7 15], [5 12 12], [15 20 10], [19 24 8] ] .

The output is a list of "key points" (red dots in Figure B) in the format of [ [x1,y1], [x2, y2], [x3, y3], ... ] that uniquely defines a skyline. A key point is the left endpoint of a horizontal line segment. Note that the last key point, where the rightmost building ends, is merely used to mark the termination of the skyline, and always has zero height. Also, the ground in between any two adjacent buildings should be considered part of the skyline contour.

For instance, the skyline in Figure B should be represented as:[ [2 10], [3 15], [7 12], [12 0], [15 10], [20 8], [24, 0] ].

Notes:

  • The number of buildings in any input list is guaranteed to be in the range [0, 10000].
  • The input list is already sorted in ascending order by the left x position Li
  • The output list must be sorted by the x position. 
  • There must be no consecutive horizontal lines of equal height in the output skyline. For instance, [...[2 3], [4 5], [7 5], [11 5], [12 7]...] is not acceptable; the three lines of height 5 should be merged into one in the final output as such: [...[2 3], [4 5], [12 7], ...]


解:题目很复杂,最初看起来可以说是毫无头绪,根据题目要求,如果只有一个建筑,那么输出的点一定是建筑的左上顶点和右下顶点,根据这个思路,想到用分治法来解决,每次将buildings一分为二做递归,当buildings的数量为1时,返回2个点,那么难点在于合并两组点集,并且要始终保持点集有序。

合并点集时,和归并排序的思路有点相似,分别使用2个指针(int值)指向当前left和right的位置,每次选择横坐标较小的点插入结果中,保证结果有序。结果点的横坐标等于原横坐标,至于结果点的纵坐标,应该是取right和left中的较大值,而这个高度的值也应有2个变量实时记录,代表的含义是left和right当前的高度,为了更好的说明,如下图:


画圈的点是left中的一个点,在合并时,因为点的高度小于right的高度,结果中点的纵坐标就是right的高度。

合并时还需要注意的两点:

1.合并的第一个点可以无条件插入到结果中,当left或者right全部遍历后,一方的剩余点不再需要比较直接插入结果中

2.点插入到结果中时,应注意点的高度一定不能和上一个点相同,如果多个连续的点纵坐标相同,则结果中只能有第一个点(结果中不会出现横坐标相等的点,也不会出现连续的纵坐标相等的点)

代码:

class Solution {
public:
    vector<pair<int, int> > merge(vector<pair<int, int> >& left, vector<pair<int, int> >& right) {
    	int l = 0, r = 0;
    	int lheight = 0, rheight = 0;
    	vector<pair<int, int> > temp;
    	while (l < left.size() && r < right.size()) {
    		if (left[l].first == right[r].first) {
    			lheight = left[l].second;
    			rheight = right[r].second;
    			if (temp.empty() || temp.back().second != max(lheight, rheight)) 
    				temp.push_back(make_pair(left[l].first, max(lheight, rheight)));
    			l++;
    			r++;
    			
    		}
    		else if(left[l].first < right[r].first) {
    			lheight = left[l].second;
    			if (temp.empty() || temp.back().second != max(lheight, rheight)) 
    				temp.push_back(make_pair(left[l].first, max(lheight, rheight)));
    			l++;
    		} else{
    			rheight = right[r].second;
    			if (temp.empty() || temp.back().second != max(lheight, rheight)) 
    				temp.push_back(make_pair(right[r].first, max(lheight, rheight)));
    		    r++;
    		}
    	}
    	for(int i = l; i < left.size(); i++)
            if(temp.empty() || temp.back().second != left[i].second) temp.push_back(left[i]);
        for(int i = r; i < right.size(); i++)
            if(temp.empty() || temp.back().second != right[i].second) temp.push_back(right[i]);
	    return temp;
    }
    vector<pair<int, int>> getSkyline(vector<vector<int>>& buildings) {
        int size = buildings.size();
        if (size == 0) return {};
        if (size == 1) {
        	vector<pair<int, int> > temp;
        	temp.push_back(make_pair(buildings[0][0], buildings[0][2]));
        	temp.push_back(make_pair(buildings[0][1], 0));
        	return temp;
        }
        vector<vector<int> > buildings1(buildings.begin(), buildings.begin()+size/2);
        vector<vector<int> > buildings2(buildings.begin()+size/2, buildings.end());
        vector<pair<int, int> > left = getSkyline(buildings1);
        vector<pair<int, int> > right = getSkyline(buildings2);
        return merge(left, right);
    }
};



<think>好的,我现在需要帮用户解决一个Node.js中的TypeError问题,错误信息是“TypeError: cb is nota function”。用户提供的错误堆栈显示这个问题发生在parseCookie函数中,具体是在调用cb的时候。首先,我得分析可能的原因。 首先,错误信息指出cb不是一个函数。这通常意味着在调用某个函数时,传入的回调参数不是函数类型。可能的情况包括: 1. **回调未正确传递**:可能在调用parseCookie时,没有正确传递回调函数,或者传递了错误的参数类型。 2.**函数定义参数不匹配**:parseCookie函数可能期望一个回调函数作为参数,但在调用时可能没有传入或者传入了其他类型的值。 3. **异步处理问题**:可能使用了错误的回调方式,比如应该使用函数的地方用了Promise或其他异步处理方式。 4. **第三方库版本问题**:可能使用的库版本过旧或存在bug,导致回调参数处理异常。 接下来,我需要根据用户提供的堆栈信息来定位具体的位置。错误发生在leetcode.js的parseCookie函数第542行,调用cb的地方。这可能是一个插件或模块中的函数,用户可能在使用VSCode的LeetCode插件时遇到了问题,尤其是在处理cookie登录时。 考虑到用户提到的环境是Node.js v22.17.0,可能存在兼容性问题。某些旧版本的库可能不兼容较新的Node.js版本,导致回调参数未被正确处理。 用户可能需要检查以下几点: -检查parseCookie函数的定义,确认其参数是否正确要求一个回调函数。 -查看调用parseCookie的地方是否正确传递了回调函数。 -确认第三方库(如vsc-leetcode-cli)是否支持当前Node.js版本,可能需要升级或降级版本。- 查看是否有相关的Issue或更新日志,可能其他用户也遇到了类似的问题。 可能的解决方案包括:1. 更新LeetCode插件到最新版本,修复可能存在的bug。2. 降级Node.js版本到更稳定的版本,比如LTS版本,以确保兼容性。 3. 如果问题由特定代码引起,检查代码中回调函数的使用是否正确。 4.清除缓存或重新安装插件,解决可能的配置错误。 此外,用户可能需要提供更多的上下文信息,比如他们正在执行的操作,或者相关的代码片段,以便更准确地定位问题。例如,是否在自定义代码中调用了parseCookie函数,或者这完全是由插件内部触发的错误。 总结来说,用户需要检查回调函数的传递和定义,确认第三方库的兼容性,并考虑环境配置是否合适。如果问题持续存在,建议查看插件的官方文档或提交Issue寻求帮助。</think>`TypeError: cb is not a function` 表示在调用 `parseCookie` 函数时,传递的 `cb` 参数 **不是函数类型**。以下是关键分析及解决方案: --- ### ❌ 错误原因 1. **回调参数类型错误** `parseCookie` 函数内部尝试调用 `cb(...)`,但传入的 `cb` 可能是 `null`、`undefined` 或其他非函数类型(如字符串、数字)。 2. **函数定义与调用不匹配** - 函数定义时要求传入回调函数: ```javascript function parseCookie(cookieStr, cb) { ... } ``` - 调用时却未传递有效回调: ```javascript parseCookie("key=value", "错误参数"); // 第二个参数不是函数 ``` 3. **第三方库兼容性问题** 根据路径 `vsc-leetcode-cli` 和 Node.js v22.17.0,可能是 **LeetCode 插件与最新 Node.js 版本不兼容**,导致内部回调参数传递异常。 --- ### ✅ 解决方案 1. **检查调用方代码** 定位 `parseCookie` 的调用位置(如 `leetcode.js` 第 565 行),确保传递正确的回调函数: ```javascript parseCookie("key=value", (err, result) => { if (err) throw err; console.log(result); }); ``` 2. **更新插件版本** `vsc-leetcode-cli` 可能存在旧版 bug,尝试更新插件: ```bash npm install -g vsc-leetcode-cli@latest ``` 或重新安装 VSCode LeetCode 插件。 3. **降级 Node.js 版本** Node.js v22.x 是较新版本,可能存在兼容性问题。建议切换到 **LTS 版本(如 v18.x 或 v20.x)**: - 使用 [nvm](https://github.com/nvm-sh/nvm) 管理多版本 Node.js。 4. **临时修复(绕过错误)** 在调用 `parseCookie` 前增加类型检查: ```javascript if (typeof cb === 'function') { cb(null, parsedCookie); } else { console.error("回调参数必须为函数"); } ``` --- ### 🧪 验证步骤 1. 确认 Node.js 版本: ```bash node -v # 推荐使用 v18.x 或 v20.x ``` 2. 检查插件依赖: 进入插件目录并更新依赖: ```bash cd ~/.vscode-huawei/extensions/leetcode.vscode-leetcode-0.18.1 npm install ``` --- ### ❓ 常见相关问题
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值