logologo
  • AI Tools

    DB Query GeneratorMock InterviewResume BuilderLearning Path GeneratorCheatsheet GeneratorAgentic Prompt GeneratorCompany ResearchCover Letter Generator
  • XpertoAI
  • AI Interviewer
  • MVP Ready
  • Resources

    CertificationsTopicsExpertsCollectionsArticlesQuestionsVideosJobs
logologo

Elevate Your Coding with our comprehensive articles and niche collections.

Useful Links

  • Contact Us
  • Privacy Policy
  • Terms & Conditions
  • Refund & Cancellation
  • About Us

Resources

  • Xperto-AI
  • Certifications
  • Python
  • GenAI
  • Machine Learning

Interviews

  • DSA
  • System Design
  • Design Patterns
  • Frontend System Design
  • ReactJS

Procodebase © 2024. All rights reserved.

Level Up Your Skills with Xperto-AI

A multi-AI agent platform that helps you level up your development skills and ace your interview preparation to secure your dream job.

Launch Xperto-AI

Path with Maximum Sum

author
Generated by
ProCodebase AI

15/11/2024

Dynamic Programming

Sign in to read full article

Dynamic programming (DP) is a powerful technique used to solve complex problems by breaking them down into simpler subproblems. One of the fascinating challenges within this domain is the "Path with Maximum Sum." This involves finding the most profitable route through a grid from the top-left corner to the bottom-right corner, where each cell in the grid contains a certain integer value.

Understanding the Problem

Given a 2D grid filled with integers, our goal is to find a path that maximizes the sum of its values. Moving can only occur downwards or rightwards, meaning you can progress to an adjacent cell in the row below or the next cell in the same row.

Let’s illustrate this with a simple example:

Grid:
1  2  3
4  5  6
7  8  9

For the above grid, one optimal path is 1 → 2 → 5 → 8 → 9, which gives us a sum of 25.

Dynamic Programming Approach

To solve this problem, dynamic programming can be utilized to build a solution that optimally calculates the maximum sum at each cell based on its predecessors. Here’s a step-by-step explanation of how to implement this:

Step 1: Initialization

We will create a 2D array dp where dp[i][j] represents the maximum sum that can be achieved to reach cell (i, j) in the grid. The dimensions of dp will be the same as the grid.

Step 2: Base Case

The value at our starting point (dp[0][0]) will be equal to grid[0][0] since that’s where we begin our path.

Step 3: Fill in the first row and first column

Since we can only move right on the first row and down on the first column, we can fill these out easily.

  • For the first row: dp[0][j] = dp[0][j-1] + grid[0][j]
  • For the first column: dp[i][0] = dp[i-1][0] + grid[i][0]

Step 4: Fill in the rest of the dp table

For each cell (i, j), the maximum sum will be the maximum of the two possible preceding cells:

  • From the left: dp[i][j-1]
  • From above: dp[i-1][j]

Thus, we can express our relationship as:

dp[i][j] = max(dp[i-1][j], dp[i][j-1]) + grid[i][j]

Step 5: The Final Result

The maximum sum path will ultimately be stored in dp[m-1][n-1], where m and n are the dimensions of the grid.

Implementation

Here is how you can implement the above approach in Python:

def maxPathSum(grid): if not grid: return 0 m, n = len(grid), len(grid[0]) dp = [[0] * n for _ in range(m)] dp[0][0] = grid[0][0] # Fill the first row for j in range(1, n): dp[0][j] = dp[0][j-1] + grid[0][j] # Fill the first column for i in range(1, m): dp[i][0] = dp[i-1][0] + grid[i][0] # Fill the rest of the dp table for i in range(1, m): for j in range(1, n): dp[i][j] = max(dp[i-1][j], dp[i][j-1]) + grid[i][j] return dp[m-1][n-1] # Example usage grid = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ] print(maxPathSum(grid)) # Output: 25

Time Complexity and Space Complexity

  • Time Complexity: O(m*n), where m is the number of rows and n is the number of columns in the grid. We have to visit each cell once.
  • Space Complexity: O(m*n) for the dp table. However, we can optimize space to O(n) if we only keep track of the current and previous rows.

By using this dynamic programming technique, you can effectively tackle the "Path with Maximum Sum" problem and similar pathfinding challenges! Happy coding!

Popular Tags

Dynamic ProgrammingDSAInterview Questions

Share now!

Like & Bookmark!

Related Collections

  • Top 20 DSA Interview Questions Mastery

    23/09/2024 | DSA

  • DSA Mastery for Interviews

    23/09/2024 | DSA

  • Mastering Bit Manipulation: Unlocking Binary Power

    08/12/2024 | DSA

  • Advanced Graph Interview Questions in Java

    16/11/2024 | DSA

  • Advanced Priority Queue and Heap Interview Questions in Java

    16/11/2024 | DSA

Related Articles

  • Basics of Bitwise Operators

    08/12/2024 | DSA

  • The Two-Pointer Technique

    06/12/2024 | DSA

  • Understanding Queues in Data Structures and Algorithms

    06/12/2024 | DSA

  • Understanding Arrays and Strings in Data Structures and Algorithms

    06/12/2024 | DSA

  • Understanding Kosaraju's Algorithm for Strongly Connected Components in Graphs

    16/11/2024 | DSA

  • Master NOT Operator in DSA

    08/12/2024 | DSA

  • Implementing Max Heap and Min Heap in Java

    16/11/2024 | DSA

Popular Category

  • Python
  • Generative AI
  • Machine Learning
  • ReactJS
  • System Design