Java | Leetcode Java题解之第354题俄罗斯套娃信封问题

题目:

题解:

java 复制代码
class Solution {
    public int maxEnvelopes(int[][] envelopes) {
        if (envelopes.length == 0) {
            return 0;
        }
        
        int n = envelopes.length;
        Arrays.sort(envelopes, new Comparator<int[]>() {
            public int compare(int[] e1, int[] e2) {
                if (e1[0] != e2[0]) {
                    return e1[0] - e2[0];
                } else {
                    return e2[1] - e1[1];
                }
            }
        });

        int[] f = new int[n];
        Arrays.fill(f, 1);
        int ans = 1;
        for (int i = 1; i < n; ++i) {
            for (int j = 0; j < i; ++j) {
                if (envelopes[j][1] < envelopes[i][1]) {
                    f[i] = Math.max(f[i], f[j] + 1);
                }
            }
            ans = Math.max(ans, f[i]);
        }
        return ans;
    }
}
相关推荐
数据知道15 分钟前
反序列化漏洞:Java、PHP、Python 三条线各讲透
java·网络·python·安全·网络安全·php
Navigator_Z35 分钟前
LeetCode //C - 1192. Critical Connections in a Network
c语言·算法·leetcode
2601_9499506339 分钟前
Java 面试准备,用练题簿把“背过八股”变成“真正会答”
java·开发语言·面试·刷题·小程序推荐
無限進步D1 小时前
Java 函数式编程(Lambda)
java·开发语言
rannn_1111 小时前
【力扣hot100】链表专题下|138、148、23、146
java·算法·leetcode·链表·开发
lv__pf1 小时前
Spring配置类解析 【TL spring 11】
java·前端·spring
zhougl9962 小时前
Java Spring Boot 爬虫技术全面介绍
java·spring boot·爬虫
奥莱维2 小时前
【无标题】
java·前端·javascript
hanlin032 小时前
刷题笔记:力扣第189题-轮转数组
笔记·算法·leetcode
琥珀色糖2 小时前
leetcode hot100题(持续更新)移动零(双指针)
算法·leetcode·职场和发展·双指针·移动零