LeetCode Russian Doll Envelopes
来源:互联网 发布:mac os x 10.11正式版 编辑:程序博客网 时间:2024/05/17 03:30
Description:
You have a number of envelopes with widths and heights given as a pair of integers (w, h)
. One envelope can fit into another if and only if both the width and height of one envelope is greater than the width and height of the other envelope.
What is the maximum number of envelopes can you Russian doll? (put one inside other)
Solution:
将weight从小打到排序,一样的weight就按照height从大到小排序。然后将height单独拿出来,求LIS即可。
import java.util.Arrays;class Envelop implements Comparable<Envelop> {int width;int height;Envelop(int w, int h) {this.width = w;this.height = h;}public int compareTo(Envelop o) {if (this.width == o.width)return o.height - this.height;return this.width - o.width;}}public class Solution {public int maxEnvelopes(int[][] envelopes) {int len = envelopes.length;if (len == 0)return 0;Envelop[] arr = new Envelop[len];for (int i = 0; i < len; i++)arr[i] = new Envelop(envelopes[i][0], envelopes[i][1]);Arrays.sort(arr);int index = 1;int[] dp = new int[len];dp[0] = arr[0].height;for (int i = 1; i < len; i++) {if (arr[i].height > dp[index - 1]) {dp[index++] = arr[i].height;} else {int left = 0;int right = index - 1;int mid = left;while (left < right) {mid = (left + right) / 2;if (dp[mid] < arr[i].height) {left = mid + 1;} else {right = mid;}}dp[left] = arr[i].height;}}return index;}}
0 0
- 【Leetcode】Russian Doll Envelopes
- leetcode Russian Doll Envelopes
- LeetCode Russian Doll Envelopes
- LeetCode -- Russian Doll Envelopes
- 【Leetcode】Russian Doll Envelopes
- [LeetCode] Russian Doll Envelopes
- LeetCode 354. Russian Doll Envelopes
- leetcode 354. Russian Doll Envelopes
- [leetcode] 354. Russian Doll Envelopes
- leetcode之Russian Doll Envelopes
- leetcode.354. Russian Doll Envelopes
- leetcode 354. Russian Doll Envelopes
- leetcode 354.Russian Doll Envelopes
- LeetCode 354. Russian Doll Envelopes
- Leetcode-354. Russian Doll Envelopes
- [LeetCode]354. Russian Doll Envelopes
- [leetcode] 354. Russian Doll Envelopes
- Leetcode 354. Russian Doll Envelopes
- 负温度系数热敏电阻根据B值计算电阻的方法以及温度保护电路的设计
- 大话linux(五)之互斥锁mutex
- 知识梳理------进程(三)
- Office2Pdf工具开发
- MySQL使用教程(一)
- LeetCode Russian Doll Envelopes
- 【机器学习】在工程上机器学习特征选择的方法
- 你如何对网站的文件和资源进行优化?
- (三)ArcGIS API For Javascript之调用动态地图服务
- 用Python+Django在Eclipse环境下开发web网站
- 用Fragment填充Activity实现选项卡的功能
- BZOJ2809可合并堆或启发式合并
- 滴滴面试
- Matlab中提高m文件执行效率的小技巧