日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

LeetCode 406. Queue Reconstruction by Height

發(fā)布時間:2025/3/15 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 LeetCode 406. Queue Reconstruction by Height 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

原題鏈接在這里:https://leetcode.com/problems/queue-reconstruction-by-height/description/

題目:

Suppose you have a random list of people standing in a queue. Each person is described by a pair of integers?(h, k), where?h?is the height of the person and?k?is the number of people in front of this person who have a height greater than or equal to?h. Write an algorithm to reconstruct the queue.

Note:
The number of people is less than 1,100.

Example

Input: [[7,0], [4,4], [7,1], [5,0], [6,1], [5,2]]Output: [[5,0], [7,0], [5,2], [6,1], [4,4], [7,1]]

題解:

先把最高的人挑出來,然后按照k把他們插入到ArrayList<int []> resList中. 然后再插第二高的,以此類推.

Time Complexity: O(nlogn). n = people.length. sort用時O(nlogn), 插入時因為使用ArrayList, 可以忽略resize的用時, 所以插入共用時O(n).

Space: O(n).

AC Java:

1 class Solution { 2 public int[][] reconstructQueue(int[][] people) { 3 if(people == null || people.length == 0|| people[0].length != 2){ 4 return people; 5 } 6 7 Arrays.sort(people, new Comparator<int []>(){ 8 public int compare(int [] a, int [] b){ 9 if(a[0] == b[0]){ 10 return a[1]-b[1]; 11 } 12 return b[0]-a[0]; 13 } 14 }); 15 16 ArrayList<int []> resList = new ArrayList<int []>(); 17 for(int [] item : people){ 18 resList.add(item[1], item); 19 } 20 21 return resList.toArray(new int[people.length][2]); 22 } 23 }

跟上315. Count of Smaller Numbers After Self.?

轉(zhuǎn)載于:https://www.cnblogs.com/Dylan-Java-NYC/p/7568309.html

總結(jié)

以上是生活随笔為你收集整理的LeetCode 406. Queue Reconstruction by Height的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。