zoukankan      html  css  js  c++  java
  • Daliy Algorithm (动态规划,思维)-- day 77

    Nothing to fear


    种一棵树最好的时间是十年前,其次是现在!

    那些你早出晚归付出的刻苦努力,你不想训练,当你觉的太累了但还是要咬牙坚持的时候,那就是在追逐梦想,不要在意终点有什么,要享受路途的过程,或许你不能成就梦想,但一定会有更伟大的事情随之而来。 mamba out~

    2020.5.9


    人一我十,人十我百,追逐青春的梦想,怀着自信的心,永不言弃!

    lc-96. 不同的二叉搜索树

    class Solution {
    public:
        int numTrees(int n) {
            vector<long long> dp(n + 1);
            dp[0] = 1,dp[1] = 1;
            for(int i = 2;i <= n ;i ++)
            {
                dp[i] = dp[i-1]*(4*i-2)/(i+1);
            }
            return dp[n];
        }
    };
    

    lc-714. 买卖股票的最佳时机含手续费

    dp1 : 第i天手上有股票得最大价值
    dp2 : 第i天手上无股票得最大价值

    dp1[i] = max(前i天股票得最大收益,买入今天得股票得收益)
    dp2[i] = max(前一天没股票得最大收益,前i天得股票再今天卖掉)

    class Solution {
    public:
        int maxProfit(vector<int>& prices, int fee) {
            int n = prices.size();
            vector<int> dp1(n+1),dp2(n+1);
            dp1[0] = -prices[0];
            for(int i = 1;i < n ;i ++)
            {
                dp1[i] = max(dp1[i-1],dp2[i-1] - prices[i]);
                dp2[i] = max(dp2[i-1],dp1[i-1] + prices[i] - fee);
            }
            return dp2[n-1];
        }
    };
    

    lc-120. 三角形最小路径和

    数字三角形模型

    class Solution {
    public:
        int minimumTotal(vector<vector<int>>& triangle) {
            int n = triangle.size();
            vector<vector<int>> f(n , vector<int>(n,0x3f3f3f));
            for(int i = 0;i < n ;i ++)
            {
                for(int j = 0;j < triangle[i].size() ;j ++)
                {
                    if(i == 0)f[i][j] = triangle[i][j];
                    else if(i != 0 && j == 0)f[i][j] = f[i-1][j] + triangle[i][j];
                    else f[i][j] = min(f[i-1][j],f[i-1][j-1]) + triangle[i][j];
                }
            }
            int ans = 0x3f3f3f;
            for(int i = 0;i < n ;i ++)ans = min(ans,f[n-1][i]);
            return ans;
        }
    };
    

    lc-1227. 飞机座位分配概率

    智力题吧 我也能秒杀智力题了

    
    class Solution {
    public:
        double nthPersonGetsNthSeat(int n) {
            return (n == 1) ? 1 : 0.5;
        }
    };
    
  • 相关阅读:
    记一次安装python umysql模块的报错
    elasticsearch 6.2.4添加用户密码认证
    mysqldump 备份数据和恢复
    记一次线上Java程序导致服务器CPU占用率过高的问题排除过程
    配置rpm包安装的jdk环境变量
    centos6 & centos 7 防火墙设置
    MySQL启动出现The server quit without updating PID file错误解决办法
    ptmalloc,tcmalloc和jemalloc内存分配策略研究 ? I'm OWen..
    为什么要内存对齐 Data alignment: Straighten up and fly right
    linux驱动学习_1
  • 原文地址:https://www.cnblogs.com/wlw-x/p/12866014.html
Copyright © 2011-2022 走看看