美文网首页动态规划
8.4 extra added later

8.4 extra added later

作者: 陈十十 | 来源:发表于2016-08-06 08:10 被阅读3次

补一下

1] Min Stack

class MinStack {
public:
    /** initialize your data structure here. */
    stack<int> s;
    stack<int> mins;
    MinStack() {
    }
    
    void push(int x) {
        s.push(x);
        if (mins.empty() || mins.top()>=x) mins.push(x);
    }
    
    void pop() {
        if (!s.empty()) {
            if (mins.top()==s.top()) mins.pop();
            s.pop();
        }
    }
    
    int top() {
        if (!s.empty()) return s.top();
        return -1;
    }
    
    int getMin() {
        if (!mins.empty()) return mins.top();
        return -1;
    }
};

2] Minimum Path Sum

这么简单的dp做这么久不开森,,练练练
。。。minpath递增着index不是一样的吗orz

optimize: see hint at the end https://discuss.leetcode.com/topic/15269/10-lines-28ms-o-n-space-dp-solution-in-c-with-explanations/2
(keep only two arrays)

    int minPathSum(vector<vector<int>>& grid) {
        int m = grid.size(), n = grid[0].size();
        int dp[m+1][n+1];
        dp[m-1][n-1] = grid[m-1][n-1];
        for (int i=n-2; i>=0; --i) {
            dp[m-1][i] = grid[m-1][i] + dp[m-1][i+1]; 
        }
        for (int j=m-2;j>=0; --j) {
            dp[j][n-1] = grid[j][n-1] + dp[j+1][n-1];
        }
        
        for (int i=n-2; i>=0; --i) {
            for (int j=m-2; j>=0; --j) {
                dp[j][i] = grid[j][i] + min(dp[j+1][i], dp[j][i+1]);
            }
        }
        return dp[0][0];
    }

相关文章

  • 8.4 extra added later

    补一下 1] Min Stack 2] Minimum Path Sum 这么简单的dp做这么久不开森,,练练练。...

  • 70. Is that you , John? 是你吗,约翰?

    1. 单词部分 extra 额外的extra work 加班extra time 额外的时间extra-large...

  • gerrit回调

    patchset-created comment-added comment-added之Verified jen...

  • Extra Credits 额外加分系列视频章节列表

    Extra CreditsAll episodes of Extra Credits in chronologic...

  • Later

    不忘初心,方得始终,初心易得,始终难守。何为初心,何为始终。始于初心,终于白首。

  • later

    what a beautiful day,what a beautiful world.

  • LATER

    我又开始了深深的反复中,但是我好累,心累 2015-03-24 让自己激动的心情在最短时间内平复下来,是很不容易的...

  • Later

    I'm sorry.

  • later

  • later

    Later l see shine in her eyes like l before as l like Cau...

网友评论

    本文标题:8.4 extra added later

    本文链接:https://www.haomeiwen.com/subject/ecjksttx.html