1937. Maximum Number of Points with Cost
UnknownView on LeetCode
Time: O(mn)
Space: O(n)
Problem Overview
Maximum Number of Points with Cost (Unknown) asks you to solve a structured algorithmic task. This is a common Array / Dynamic Programming pattern in coding interviews. DP row by row; left-right sweeps track running max to avoid O(n²) per row.
A full step-by-step explanation is being added. See the study guide for pattern-based practice.
Approach
DP row by row; left-right sweeps track running max to avoid O(n²) per row.
Related patterns: Array, Dynamic Programming
1937.cs
C#
// Approach: DP row by row; left-right sweeps track running max to avoid O(n²) per row.
// Time: O(mn) Space: O(n)
public class Solution
{
public long MaxPoints(int[][] points)
{
int n = points[0].Length;
long[] dp = new long[n];
foreach (var row in points)
{
long[] leftToRight = new long[n];
long runningMax = 0;
for (int j = 0; j < n; ++j)
{
runningMax = Math.Max(runningMax - 1, dp[j]);
leftToRight[j] = runningMax;
}
long[] rightToLeft = new long[n];
runningMax = 0;
for (int j = n - 1; j >= 0; --j)
{
runningMax = Math.Max(runningMax - 1, dp[j]);
rightToLeft[j] = runningMax;
}
for (int j = 0; j < n; ++j)
dp[j] = Math.Max(leftToRight[j], rightToLeft[j]) + row[j];
}
return dp.Max();
}
}Was this solution helpful?
Related Problems
- 4. Median of Two Sorted Arrays(Hard)
- 11. Container With Most Water(Medium)
- 15. 3Sum(Medium)
- 16. 3Sum Closest(Medium)
- 22. Generate Parentheses(Medium)
- 26. Remove Duplicates from Sorted Array(Easy)