ProCodebaseProCodebase
  • ModusA Growth OS for your business, on WhatsAppKiosqSell more. Chase less.AI InterviewerAutomated screening & AI-led interviewsXperto AIAI prep companion for candidatesAI Tools HubResume builder, learning paths & more
  • Pre-Vetted DevelopersScreened, scored and ready to interviewAI-Native DevelopersSenior engineers on an hourly basis
  • Services
  • Features
  • AI Coaching
  • Jobs
  • FAQs
Sign inBook a demo
  • Services
  • Features
  • AI Coaching
  • Jobs
  • FAQs
Sign inBook a demo
ProCodebaseProCodebase

ProCodebase Technologies builds AI products for hiring and growth, and ships software for clients as a technical consultancy. We source, screen and deliver pre-vetted developers — so you only interview high-signal candidates.

Products

  • Modus
  • Kiosq
  • AI Interviewer
  • Xperto AI
  • AI Tools Hub

Hire & build

  • Pre-Vetted Developers
  • AI-Native Developers
  • Technical Consultancy
  • MVP Development
  • Features

Resources

  • Articles
  • Topics
  • Certifications
  • Collections
  • Jobs

Company

  • About Us
  • Contact Us
  • Book a Demo
  • FAQs

© 2026 ProCodebase Technologies. All rights reserved.

  • Privacy Policy
  • Terms & Conditions
  • Refund & Cancellation

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

  • DSA Mastery for Interviews

    23/09/2024 · DSA

  • Trees Interview Questions Using Java

    13/10/2024 · DSA

  • Mastering Arrays : The Basic Data Structure

    06/12/2024 · DSA

  • Advanced String-based Interview Techniques

    15/11/2024 · DSA

  • Advanced Recursion and Backtracking Problems Using Java

    13/10/2024 · DSA

Related articles

  • Understanding the Edit Distance Problem in Advanced Dynamic Programming

    15/11/2024 · DSA

  • Working with the AND Operator in Data Structures and Algorithms

    08/12/2024 · DSA

  • String Compression

    15/11/2024 · DSA

  • Sliding Window Maximum Using Priority Queue

    16/11/2024 · DSA

  • Heap Operations

    16/11/2024 · DSA

  • Understanding Bridges and Articulation Points in Graphs

    16/11/2024 · DSA

  • Frequency Sort Using Priority Queue

    16/11/2024 · DSA

Popular category

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