Cookie Consent by Free Privacy Policy Generator Update cookies preferences 📌 DP with Simple Matrix Traversal

🏠 Team IT Security News

TSecurity.de ist eine Online-Plattform, die sich auf die Bereitstellung von Informationen,alle 15 Minuten neuste Nachrichten, Bildungsressourcen und Dienstleistungen rund um das Thema IT-Sicherheit spezialisiert hat.
Ob es sich um aktuelle Nachrichten, Fachartikel, Blogbeiträge, Webinare, Tutorials, oder Tipps & Tricks handelt, TSecurity.de bietet seinen Nutzern einen umfassenden Überblick über die wichtigsten Aspekte der IT-Sicherheit in einer sich ständig verändernden digitalen Welt.

16.12.2023 - TIP: Wer den Cookie Consent Banner akzeptiert, kann z.B. von Englisch nach Deutsch übersetzen, erst Englisch auswählen dann wieder Deutsch!

Google Android Playstore Download Button für Team IT Security



📚 DP with Simple Matrix Traversal


💡 Newskategorie: Programmierung
🔗 Quelle: dev.to

64. Minimum Path Sum

Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right, which minimizes the sum of all numbers along its path.
Note: You can only move either down or right at any point in time.

Image description

Example 1:

Input: grid = [[1,3,1],[1,5,1],[4,2,1]]
Output: 7
Explanation: `Because the path 1 → 3 → 1 → 1 → 1 minimizes the sum.

Example 2:

Input: grid = [[1,2,3],[4,5,6]]
Output: 12

1. Bottom-Up Recursive without memoization: TLE

We start building our solution from lowest values of row and col in a bottom-up fashion.

State Transition Equation
dp(i, j) = grid[i][j] + Min(dp(i+1, j), dp(i, j+1))

  • 0 >= row <= m-1
  • 0 >= col <= n-1

lowest values of row and col: 0
max values of row and col: m-1 & n-1

base case : When we are left with only 1 row and 1 col, min cost to reach to this cell from this cell is cost of this cell only.

`Java
class Solution {
int m;
int n;
int[][] grid;

public int minPathSum(int[][] grid) {

    int m = grid.length; 
    int n = grid[0].length; 
    this.m = m;
    this.n = n;
    this.grid = grid; 
    return dp(0, 0);
}

private int dp(int row, int col){
    System.out.println("["+row+"]["+col+"]");
    if(row  == m-1 && col == n-1){
        return grid[row][col];
    }

    if(!isValid(row, col)){
        return Integer.MAX_VALUE;
    }

    return grid[row][col] + Math.min(dp(row+1, col), dp(row, col+1));
}

private boolean isValid(int row, int col){
    return row >= 0 && col >= 0 && row <= m-1 && col <= n-1;
}

}
`

2. Bottom-Up Recursive with memoization: Accepted

`java
class Solution {
int m;
int n;
int[][] grid;
Integer[][] memo;

//dp(i, j) = grid[i][j] + Min(dp(i+1, j), dp(i, j+1));       
public int minPathSum(int[][] grid) {

    int m = grid.length; 
    int n = grid[0].length; 
    this.m = m;
    this.n = n;
    this.grid = grid; 
    memo = new Integer[m+1][n+1];
    return dp(0, 0);
}

private int dp(int row, int col){
    System.out.println("["+row+"]["+col+"]");
    if(row  == m-1 && col == n-1){
        return grid[row][col];
    }

    if(!isValid(row, col)){
        return Integer.MAX_VALUE;
    }

    if(memo[row][col] != null){
        return memo[row][col];
    }

     memo[row][col] = grid[row][col] + Math.min(dp(row+1, col), dp(row, col+1));
     return memo[row][col];
}

private boolean isValid(int row, int col){
    return row >= 0 && col >= 0 && row <= m-1 && col <= n-1;
}

}
`

3. Top-Down Recurisve with memoization: Accepted

We start building our solution from largest values of row and col in a top-down fashion

State Transition Equation
dp(i, j) = grid[i][j] + Min(dp(i-1, j), dp(i, j-1))

`
class Solution {
int m;
int n;
int[][] grid;
Integer[][] memo;

public int minPathSum(int[][] grid) {
    int m = grid.length;
    int n = grid[0].length;
    this.m = m;
    this.n = n;
    this.grid = grid;
    memo = new Integer[m + 1][n + 1];
    return dp(m - 1, n - 1);
}

private int dp(int row, int col) {
    if (row == 0 && col == 0) {
        return grid[row][col];
    }

    if (!isValid(row, col)) {
        return Integer.MAX_VALUE;
    }

    if (memo[row][col] != null) {
        return memo[row][col];
    }

    memo[row][col] = grid[row][col] + Math.min(dp(row - 1, col), dp(row, col - 1));
    return memo[row][col];
}

private boolean isValid(int row, int col) {
    return row >= 0 && col >= 0 && row <= m - 1 && col <= n - 1;
}

}
`

4. Bottom-Up Iterative with Memoization: Accepted

`
class Solution {
int m;
int n;
int[][] grid;
Integer[][] memo;

public int minPathSum(int[][] grid) {
    int m = grid.length;
    int n = grid[0].length;
    this.m = m;
    this.n = n;
    this.grid = grid;
    memo = new Integer[m][n];
    memo[m - 1][n - 1] = grid[m - 1][n - 1];
    dp(m - 1, n - 1);
    return memo[0][0];
}

private void dp(int row, int col) {
    for (int i = row; i >= 0; i--) {
        for (int j = col; j >= 0; j--) {
            if(i == m-1 && j == n-1) continue; // last cell is already processed
            int rightVal = isValid(i, j + 1) ? memo[i][j + 1] : Integer.MAX_VALUE;
            int downVal = isValid(i + 1, j) ? memo[i + 1][j] : Integer.MAX_VALUE;
            memo[i][j] = grid[i][j] + Math.min(rightVal, downVal);
        }
    }
}

private boolean isValid(int row, int col) {
    return row >= 0 && col >= 0 && row <= m - 1 && col <= n - 1;
}

}
`

`
class Solution {
int count;
int m;
int n;
int[][] DP;
int[][] GRID;
int ans = Integer.MAX_VALUE;

public int minPathSum(int[][] grid) {

    this.m = grid.length;
    this.n = grid[0].length;
    this.count = 0;
    this.GRID = new int[this.m][this.n];
    this.DP = new int[this.m][this.n];

    for (int i = 0; i < this.m; i++) {
        System.arraycopy(grid[i], 0, GRID[i], 0, this.n);
    }

    // Fill all Dp with Int max
    Arrays.stream(DP)
            .parallel()
            .forEach(row -> Arrays.fill(row, Integer.MAX_VALUE));

    DP[m-1][n-1] = grid[m-1][n-1];

    visit(0,0);
    return DP[0][0];
}

private int visit(int r, int c) {
    if (!isValidCell(r, c)) {
        return Integer.MAX_VALUE;
    }

    // If already solved
    if(DP[r][c] != Integer.MAX_VALUE){
        return DP[r][c];
    }

    if ((r == (m - 1) && c == (n - 1))) {
        return DP[r][c];
    }

    // 1. Analyze maximum sum of a path in worst case
    // for a 200 x 200 grid where each cell has 200 and for the longest path
    // max-sum= 200 * 200 * 200 = 8000000 << Int max(2147483647)

    // 2. We can never get both down and right sum as Int max simultaneously. 
    // Its only possible for bottom right cell and for we are returning a predifned value. 

    DP[r][c] = GRID[r][c] + Math.min(visit(r + 1, c), visit(r, c+1));

    return DP[r][c];
}

private boolean isValidCell(final int r, final int c) {
    return r >= 0 && r < m && c >= 0 && c < n;
}

}
`

...



📌 DP with Simple Matrix Traversal


📈 24.61 Punkte

📌 Introducing Matrix 1.0 and the Matrix.org Foundation


📈 22.76 Punkte

📌 Matrix 1.0 und die »Matrix.org Foundation« vorgestellt


📈 22.76 Punkte

📌 News: Matrix 1.0 und die »Matrix.org Foundation« vorgestellt


📈 22.76 Punkte

📌 Matrix 1.0: Matrix-Projekt beendet Betaphase


📈 22.76 Punkte

📌 New Vector (developers of Matrix) raises $8.5M to accelerate Matrix/Riot/Modular


📈 22.76 Punkte

📌 Lumos Matrix: "Next-Gen"-Helm mit LED-Matrix bei Apple verfügbar


📈 22.76 Punkte

📌 Introducing P2P (peer-to-peer) Matrix | Matrix.org


📈 22.76 Punkte

📌 The Matrix: Matrix 4 mit Keanu Reeves kommt früher als erwartet


📈 22.76 Punkte

📌 Combating abuse in Matrix - without backdoors (Matrix blog)


📈 22.76 Punkte

📌 Arch Conf 2020 - Enter the Matrix: Install your own Matrix server on Arch Linux


📈 22.76 Punkte

📌 matrix-media-repo up to 1.2.6 on Matrix resource consumption


📈 22.76 Punkte

📌 FOSDEM 2023: Matrix 2.0 — How we're making Matrix go voom!


📈 22.76 Punkte

📌 Matrix 2.0 — How we're making Matrix go voom!


📈 22.76 Punkte

📌 Matrix: Would it be reasonable to run a matrix home server in a basic VPS?


📈 22.76 Punkte

📌 Google Flights Matrix - Google Matrix Airfare Search (ITA Software) Deutsch


📈 22.76 Punkte

📌 CVE-2023-26922 | Varisicte matrix-gui 2.0 matrix-gui-2.0 shell_exect sql injection


📈 22.76 Punkte

📌 Matrix Live S08E23 — MLS, Matrix Public Archive, Element X


📈 22.76 Punkte

📌 Matrix Live S08E31 — Element Call Encryption, Linearised Matrix


📈 22.76 Punkte

📌 Matrix Live S09E07 — Element X and Matrix Authentication Service


📈 22.76 Punkte

📌 Matrix Live S09E14 — The Matrix Elm SDK


📈 22.76 Punkte

📌 Matrix Live S09E17 — Element X, Call and Matrix Auth Service updates


📈 22.76 Punkte

📌 Matrix Live S09E20 — Moodle adopts Matrix


📈 22.76 Punkte

📌 Matrix Live S09E21 — Matrix & Element demos!


📈 22.76 Punkte

📌 Matrix Live S09E18 — Matrix crypto update. Able to decrypt?


📈 22.76 Punkte

📌 Matrix 5: Mehr Matrix ist ein Grund zur Freude!


📈 22.76 Punkte

📌 [webapps] IBM InfoPrint 4247-Z03 Impact Matrix Printer - Directory Traversal


📈 17.17 Punkte

📌 IBM InfoPrint 4247-Z03 Impact Matrix Printer Directory Traversal


📈 17.17 Punkte

📌 #0daytoday #IBM InfoPrint 4247-Z03 Impact Matrix Printer - Directory Traversal Vulnerability [#0day #Exploit]


📈 17.17 Punkte

📌 CVE-2024-23900 | Matrix Project Plugin up to 822.v01b_8c85d16d2 on Jenkins REST API path traversal


📈 17.17 Punkte

📌 Medium CVE-2017-17593: Simple chatting system project Simple chatting system


📈 14.88 Punkte

📌 Low CVE-2018-5213: Simple download monitor project Simple download monitor


📈 14.88 Punkte

📌 Low CVE-2018-5212: Simple download monitor project Simple download monitor


📈 14.88 Punkte

📌 [utility] bkp - simple utility for creating simple backups


📈 14.88 Punkte

📌 MTS Simple Booking C/Simple Booking Business up to 1.28.0 cross site scripting


📈 14.88 Punkte











matomo