classSolution{public:vector<vector<int>>getSkyline(constvector<vector<int>>&buildings){constintn=buildings.size();if(n==0)return{};if(n==1){constintleft=buildings[0][0];constintright=buildings[0][1];constintheight=buildings[0][2];return{{left,height},{right,0}};}constvector<vector<int>>left=getSkyline({begin(buildings),begin(buildings)+n/2});constvector<vector<int>>right=getSkyline({begin(buildings)+n/2,end(buildings)});returnmerge(left,right);}private:vector<vector<int>>merge(constvector<vector<int>>&left,constvector<vector<int>>&right){vector<vector<int>>ans;inti=0;// left's indexintj=0;// right's indexintleftY=0;intrightY=0;while(i<left.size()&&j<right.size())// Choose the point with smaller xif(left[i][0]<right[j][0]){leftY=left[i][1];// Update the ongoing leftYaddPoint(ans,left[i][0],max(left[i++][1],rightY));}else{rightY=right[j][1];// Update the ongoing rightYaddPoint(ans,right[j][0],max(right[j++][1],leftY));}while(i<left.size())addPoint(ans,left[i][0],left[i++][1]);while(j<right.size())addPoint(ans,right[j][0],right[j++][1]);returnans;}voidaddPoint(vector<vector<int>>&ans,intx,inty){if(!ans.empty()&&ans.back()[0]==x){ans.back()[1]=y;return;}if(!ans.empty()&&ans.back()[1]==y)return;ans.push_back({x,y});}};
classSolution{publicList<List<Integer>>getSkyline(int[][]buildings){finalintn=buildings.length;if(n==0)returnnewArrayList<>();if(n==1){finalintleft=buildings[0][0];finalintright=buildings[0][1];finalintheight=buildings[0][2];List<List<Integer>>ans=newArrayList<>();ans.add(newArrayList<>(Arrays.asList(left,height)));ans.add(newArrayList<>(Arrays.asList(right,0)));returnans;}List<List<Integer>>leftSkyline=getSkyline(Arrays.copyOfRange(buildings,0,n/2));List<List<Integer>>rightSkyline=getSkyline(Arrays.copyOfRange(buildings,n/2,n));returnmerge(leftSkyline,rightSkyline);}privateList<List<Integer>>merge(List<List<Integer>>left,List<List<Integer>>right){List<List<Integer>>ans=newArrayList<>();inti=0;// left's indexintj=0;// right's indexintleftY=0;intrightY=0;while(i<left.size()&&j<right.size())// Choose the point with smaller xif(left.get(i).get(0)<right.get(j).get(0)){leftY=left.get(i).get(1);// Update the ongoing leftYaddPoint(ans,left.get(i).get(0),Math.max(left.get(i++).get(1),rightY));}else{rightY=right.get(j).get(1);// Update the ongoing rightYaddPoint(ans,right.get(j).get(0),Math.max(right.get(j++).get(1),leftY));}while(i<left.size())addPoint(ans,left.get(i).get(0),left.get(i++).get(1));while(j<right.size())addPoint(ans,right.get(j).get(0),right.get(j++).get(1));returnans;}privatevoidaddPoint(List<List<Integer>>ans,intx,inty){if(!ans.isEmpty()&&ans.get(ans.size()-1).get(0)==x){ans.get(ans.size()-1).set(1,y);return;}if(!ans.isEmpty()&&ans.get(ans.size()-1).get(1)==y)return;ans.add(newArrayList<>(Arrays.asList(x,y)));}}
classSolution:defgetSkyline(self,buildings:List[List[int]])->List[List[int]]:n=len(buildings)ifn==0:return[]ifn==1:left,right,height=buildings[0]return[[left,height],[right,0]]left=self.getSkyline(buildings[:n//2])right=self.getSkyline(buildings[n//2:])returnself._merge(left,right)def_merge(self,left:List[List[int]],right:List[List[int]])->List[List[int]]:ans=[]i=0# left's indexj=0# right's indexleftY=0rightY=0whilei<len(left)andj<len(right):# Choose the powith smaller xifleft[i][0]<right[j][0]:leftY=left[i][1]# Update the ongoing leftYself._addPoint(ans,left[i][0],max(left[i][1],rightY))i+=1else:rightY=right[j][1]# Update the ongoing rightYself._addPoint(ans,right[j][0],max(right[j][1],leftY))j+=1whilei<len(left):self._addPoint(ans,left[i][0],left[i][1])i+=1whilej<len(right):self._addPoint(ans,right[j][0],right[j][1])j+=1returnansdef_addPoint(self,ans:List[List[int]],x:int,y:int)->None:ifansandans[-1][0]==x:ans[-1][1]=yreturnifansandans[-1][1]==y:returnans.append([x,y])