Skip to content

working solution#1539

Open
avcode3 wants to merge 1 commit into
super30admin:masterfrom
avcode3:master
Open

working solution#1539
avcode3 wants to merge 1 commit into
super30admin:masterfrom
avcode3:master

Conversation

@avcode3

@avcode3 avcode3 commented Apr 25, 2026

Copy link
Copy Markdown

No description provided.

@super30admin

Copy link
Copy Markdown
Owner

Least Falling Path Sum (problem1.py)

It seems there was a misunderstanding: you solved the wrong problem. The problem you were given is "Least Falling Path Sum", but your code is for "Delete and Earn". Please make sure to read the problem statement carefully and verify that your solution matches the problem.

For the "Least Falling Path Sum" problem, you need to work with a matrix and find a falling path with minimum sum. A common approach is to use dynamic programming where you start from the bottom row and work upwards, or from the top row downwards with memoization. Here's a hint:

  • You can use a DP table of the same size as the matrix. The value dp[i][j] represents the minimum falling path sum starting from (i,j) to the bottom.
  • Alternatively, you can update the matrix in place from the second row to the last: for each cell, add the minimum of the three possible cells from the row above (considering boundaries).
  • The answer will be the minimum value in the first row after processing.

Example of a correct solution for "Least Falling Path Sum" in Python:

class Solution:
    def minFallingPathSum(self, matrix: List[List[int]]) -> int:
        n = len(matrix)
        for i in range(1, n):
            for j in range(n):
                min_val = matrix[i-1][j]
                if j > 0:
                    min_val = min(min_val, matrix[i-1][j-1])
                if j < n-1:
                    min_val = min(min_val, matrix[i-1][j+1])
                matrix[i][j] += min_val
        return min(matrix[-1])

This solution has O(n^2) time complexity and O(1) space if modified in place (or O(n^2) if not).

VERDICT: NEEDS_IMPROVEMENT


Delete and Earn (problem2.py)

It seems there has been a mix-up in the code submission. The code you provided is for "Minimum Falling Path Sum", but the problem you need to solve is "Delete and Earn". Please ensure you are working on the correct problem and submit the appropriate solution.

For the "Delete and Earn" problem, you need to consider the following approach:

  1. The problem is similar to the "House Robber" problem but with a twist: when you take a number, you cannot take its adjacent numbers (i.e., num-1 and num+1). However, note that the numbers are not necessarily consecutive in the array, but you can precompute the total points for each number by summing all occurrences of that number.
  2. Steps:
    • Find the maximum number in the array to determine the range.
    • Create an array arr of size max+1, where arr[i] is the total points from all occurrences of the number i.
    • Then, use dynamic programming similar to "House Robber":
      dp[0] = arr[0]
      dp[1] = max(arr[0], arr[1])
      for i from 2 to max:
      dp[i] = max(dp[i-1], arr[i] + dp[i-2])
  3. This approach efficiently computes the maximum points by considering whether to take the current number (and skip the adjacent) or skip it.

Please implement the correct solution for "Delete and Earn". If you need help, refer to the reference solution provided.

VERDICT: NEEDS_IMPROVEMENT

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants