[leetcode]Jump Game II

小数据过了,大数据超时了,DP方法,先放这,mark一下

#define SIZE 1000

class Solution {
public:
    int jump(int A[], int n) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        int f[SIZE];
        for (int i = 0; i < SIZE; i++)
            f[i] = INT_MAX;
        f[0] = 0;
        
        for (int i = 1; i < n; i++){
            for (int j = 0; j < i; j++){
                if (j + A[j] >= i){            //可以达到i位置
                    if (f[j] + 1 < f[i])    //代价也小
                        f[i] = f[j] + 1;
                }
            }
            if (f[i] == INT_MAX)
                f[i] = 0;
        }
        
        return f[n-1];
    }
};

 

 

 

EOF

posted on 2013-01-10 10:33  kkmm  阅读(160)  评论(0编辑  收藏  举报