LeetCode 62件の質問:異なるパス(中)

LeetCode 62件の質問:異なるパス(中)

  • タイトル:MXNトップでのロボットは、(ラベル「スタート」以下の点図を開始)、グリッドの隅を残しました。ロボットは右または下に一歩を移動することができます。(「完了」と表示され、次の図の)グリッドの右下隅に到達しようとしているロボット。合計でQ.どのように多くの異なるパスがありますか?
  • 思考:あなたがする数学の問題にそれを有効にする場合は、しかし、M = 10、N = 10、整数以上の階乗最大結果よりますが、Pythonはあると思われます。
class Solution {
    public int uniquePaths(int m, int n) {
        int ans=0;
        if(m==0 || n==0 ) return ans;

        int a = JC(m + n - 2);
        int b = JC(m-1);
        int c = JC(n-1);
        ans=a/(b*c);
        return ans;
    }

    public int JC(int n){
        int ans=1;
        for(int i=1;i<=n;i++){
            ans*=i;
        }
        return ans;
    }
}
  • 二つのアイデア:最初の世代の22の問題のブラケットからのアイデアが、同じの再帰的な実装を見つけるように見えるしませんでした。私の分析では、整数がそのように使用することができないということです。その後、余分なパラメータ文字列の行を追加して、例のほとんどはを通じて利用可能ですが、いくつかの例には、まだ時間制限を超えました。
    これは、変更前のコードです。
class Solution {
    public int uniquePaths(int m, int n) {
        int ans = 0;
        if(m==0 || n==0 ) return ans;

        int R = m-1;
        int D = n-1;

        ans = Path(R,D,ans);
        return ans;
    }

    public int Path(int R,int D,int ans){
        if(R==0 && D==0){
            ans++;
            return ans;
        }
        if(R>0){
            Path(R-1,D,ans);
        }
        if(D>0){
            Path(R,D-1,ans);
        }
        return ans;
    }
}

これは、コード文字列パラメータが増加しています。

class Solution {
    public int uniquePaths(int m, int n) {
        List<String> ans1=new ArrayList<>();
        int ans = 0;
        if(m==0 || n==0 ) return ans;

        int R = m-1;
        int D = n-1;

        ans1 = Path(R,D,"",ans1);
        ans=ans1.size();
        return ans;
    }

    public List<String> Path(int R,int D,String s,List<String> ans1){
        if(R==0 && D==0){
            ans1.add(s);
            return ans1;
        }
        if(R>0){
            Path(R-1,D,s+"a",ans1);
        }
        if(D>0){
            Path(R,D-1,s+"b",ans1);
        }
        return ans1;
    }
}

ここに画像を挿入説明

  • 三つのアイデアは:動的方程式はDP [I] [J] = DP [I-1]〜[J] + DP [I]、[J-1]トリッキー。
class Solution {
    public int uniquePaths(int m, int n) {
        int[][] dp = new int[m][n];
        for (int i = 0; i < n; i++) dp[0][i] = 1;
        for (int i = 0; i < m; i++) dp[i][0] = 1;
        for (int i = 1; i < m; i++) {
            for (int j = 1; j < n; j++) {
                dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
            }
        }
        return dp[m - 1][n - 1];  
    }
}

作者:powcai
链接:https://leetcode-cn.com/problems/unique-paths/solution/dong-tai-gui-hua-by-powcai-2/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

ここに画像を挿入説明
最適化1:1次元圧縮の2次元配列は、あなただけ左に1行のデータとデータ上の現在位置を計算します。

class Solution {
    public int uniquePaths(int m, int n) {
        int[] pre = new int[n];
        int[] cur = new int[n];
        Arrays.fill(pre, 1);
        Arrays.fill(cur,1);
        for (int i = 1; i < m;i++){
            for (int j = 1; j < n; j++){
                cur[j] = cur[j-1] + pre[j];
            }
            pre = cur.clone();
        }
        return pre[n-1]; 
    }
}

作者:powcai
链接:https://leetcode-cn.com/problems/unique-paths/solution/dong-tai-gui-hua-by-powcai-2/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

ここに画像を挿入説明
両者の最適化:1〜2行に基づいて最適化された結果は、前者はラインの結果に更新されないことがあり、行となります。

class Solution {
    public int uniquePaths(int m, int n) {
        int[] cur = new int[n];
        Arrays.fill(cur,1);
        for (int i = 1; i < m;i++){
            for (int j = 1; j < n; j++){
                cur[j] += cur[j-1] ;
            }
        }
        return cur[n-1];
    }
}

作者:powcai
链接:https://leetcode-cn.com/problems/unique-paths/solution/dong-tai-gui-hua-by-powcai-2/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

ここに画像を挿入説明

公開された79元の記事 ウォン称賛7 ビュー1376

おすすめ

転載: blog.csdn.net/new_whiter/article/details/104264548