【动态规划——接龙型(一维坐标型)】Lintcode 602. 俄罗斯套娃信封

Lintcode 602. 俄罗斯套娃信封

题目描述:给一定数量的信封,带有整数对 (w, h) 分别代表信封宽度和高度。一个信封的宽高均大于另一个信封时可以放下另一个信封。求最大的信封嵌套层数。
在这里插入图片描述
这道题如果用最常规的想法(类似【动态规划——接龙型(一维坐标型)】Lintcode 76. 最长上升子序列)处理的话,因为有两层循环,时间复杂度会超时。需要将原来算法中的第二层循环通过一定的优化去掉。

这里先给出普通的方法(时间复杂度会超,无法AC),再给出优化之后的方法。

//1. 普通方法,时间复杂度会超
class Solution {
public:
    /**
     * @param envelopes: a number of envelopes with widths and heights
     * @return: the maximum number of envelopes
     */
    int maxEnvelopes(vector<vector<int>> &envelopes) {
        if (0 == envelopes.size()) {
            return 0;
        }

        //1. 按第一维排序
        sort(envelopes.begin(), envelopes.end());

        //2. 对第二维使用接龙型动态规划
        //2.1 初始化
        vector<int> f(envelopes.size(), 1);//任何一个位置都可能是起始位置

        //2.2 状态转移
        for (int i = 1; i < envelopes.size(); ++i) {
            for (int j = 0; j < i; ++j) {
                if (envelopes[j][1] < envelopes[i][1] && 
                envelopes[j][0] < envelopes[i][0]) {
                    f[i] = max(f[i], f[j] + 1);
                }
            }
        }

        //2.3 答案
        auto maxPlies = max_element(f.begin(), f.end());
        return *maxPlies;
    }
};


//2. 进行了优化,可以通过OJ
class Solution {
public:
    /**
     * @param envelopes: a number of envelopes with widths and heights
     * @return: the maximum number of envelopes
     */
    int maxEnvelopes(vector<vector<int>> &envelopes) {
        if (envelopes.empty()) {
            return 0;
        }
        
        sort(envelopes.begin(), envelopes.end(), cmp);
        int n = envelopes.size();
        vector<int> dp;
        for (int i = 0; i < n; i++) {
            auto it = lower_bound(dp.begin(), dp.end(), envelopes[i][1]);
            if (it == dp.end()) {
                dp.push_back(envelopes[i][1]);
            }
            else {
                *it = envelopes[i][1];
            }
        }
        return dp.size();
    }
    
    static bool cmp(vector<int> &a, vector<int> &b) {
        if (a[0] == b[0]) {
            return a[1] > b[1];
        }
        return a[0] < b[0];
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您提供一个美观的成语接龙软件。这个软件使用Python编写,使用PyQt5库来实现图形界面。以下是程序代码: ```python import sys import random from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QLabel, QLineEdit, QVBoxLayout, QHBoxLayout, QMessageBox # 成语列表 idioms = ['一鸣惊人', '二话不说', '三心二意', '四面楚歌', '五湖四海', '六神无主', '七上八下', '八仙过海', '九牛二虎之力', '十年树木,百年树人'] # 随机选择一个成语 current_idiom = random.choice(idioms) class IdiomGame(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): # 创建组件 self.setWindowTitle('成语接龙') self.setGeometry(300, 300, 400, 300) self.current_idiom_label = QLabel('当前成语:' + current_idiom, self) self.current_idiom_label.setGeometry(50, 50, 300, 30) self.input_label = QLabel('请输入一个成语:', self) self.input_label.setGeometry(50, 100, 120, 30) self.input_edit = QLineEdit(self) self.input_edit.setGeometry(180, 100, 150, 30) self.submit_button = QPushButton('提交', self) self.submit_button.setGeometry(180, 150, 80, 30) self.submit_button.clicked.connect(self.submit) self.quit_button = QPushButton('退出', self) self.quit_button.setGeometry(250, 150, 80, 30) self.quit_button.clicked.connect(QApplication.quit) # 创建布局并添加组件 vbox = QVBoxLayout() vbox.addWidget(self.current_idiom_label) hbox = QHBoxLayout() hbox.addWidget(self.input_label) hbox.addWidget(self.input_edit) vbox.addLayout(hbox) hbox = QHBoxLayout() hbox.addWidget(self.submit_button) hbox.addWidget(self.quit_button) vbox.addLayout(hbox) self.setLayout(vbox) def submit(self): user_idiom = self.input_edit.text() # 判断用户输入的成语是否符合规则 if user_idiom[0] == current_idiom[-1]: # 判断用户输入的成语是否在成语列表中 if user_idiom in idioms: current_idiom = user_idiom self.current_idiom_label.setText('当前成语:' + current_idiom) self.input_edit.setText('') else: QMessageBox.warning(self, '错误', '您输入的成语不在成语列表中,请重新输入。') else: QMessageBox.warning(self, '错误', '您输入的成语不符合规则,请重新输入。') if __name__ == '__main__': app = QApplication(sys.argv) game = IdiomGame() game.show() sys.exit(app.exec_()) ``` 这个程序使用PyQt5库来实现图形界面,界面中有一个标签用于显示当前成语,一个文本框用于用户输入成语,一个提交按钮和一个退出按钮。程序会不断循环,直到用户输入的成语不符合规则或不在成语列表中。如果用户输入的成语不符合规则或不在成语列表中,程序会通过消息框提示用户重新输入。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值