有個(gè)馬戲團(tuán)正在設(shè)計(jì)疊羅漢的表演節(jié)目,一個(gè)人要站在另一人的肩膀上。出于實(shí)際和美觀的考慮,在上面的人要比下面的人矮一點(diǎn)且輕一點(diǎn)。已知馬戲團(tuán)每個(gè)人的身高和體重,請(qǐng)編寫(xiě)代碼計(jì)算疊羅漢最多能疊幾個(gè)人。
示例:
輸入:height = [65,70,56,75,60,68] weight = [100,150,90,190,95,110] 輸出:6 解釋?zhuān)簭纳贤聰?shù),疊羅漢最多能疊 6 層:(56,90), (60,95), (65,100), (68,110), (70,150), (75,190)
提示:
height.length == weight.length <= 10000
class Solution {
public int bestSeqAtIndex(int[] height, int[] weight) {
int len = height.length;
int[][] person = new int[len][2];
for (int i = 0; i < len; ++i)
person[i] = new int[]{height[i], weight[i]};
Arrays.sort(person, (a, b) -> a[0] == b[0] ? b[1] - a[1] : a[0] - b[0]);
int[] dp = new int[len];
int res = 0;
for (int[] pair : person) {
int i = Arrays.binarySearch(dp, 0, res, pair[1]);
if (i < 0)
i = -(i + 1);
dp[i] = pair[1];
if (i == res)
++res;
}
return res;
}
}
更多建議: