Skip to content

Commit 10e72f8

Browse files
partial solution to 1937 [WIP]
1 parent 9a6f084 commit 10e72f8

File tree

1 file changed

+51
-2
lines changed

1 file changed

+51
-2
lines changed

problems/1937/jeremymanning.md

+51-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,60 @@
11
# [Problem 1937: Maximum Number of Points with Cost](https://leetcode.com/problems/maximum-number-of-points-with-cost/description/?envType=daily-question)
22

33
## Initial thoughts (stream-of-consciousness)
4+
- One observation is that we're definitely going to need to visit every number at least once, so we're looking at an $O(mn)$ algorithm, at minimum. So any steps that are $O(mn)$ or faster are "free." (Here $m$ is the number of columns and $n$ is the number of rows in the matrix.)
5+
- I wonder if we could do something like:
6+
- In an intial pass through (or maybe this can be done in a first pass...) start by selecting the cell with the maximum point value (ignoring the previous vs. next selections).
7+
- Then ask: if we shift each value...or...actually, here's a better idea
8+
- What if we do this:
9+
- If `len(points) == 1` then just return `max(points[0])`
10+
- Initialize `score = 0`
11+
- For each row, `i in range(1, len(points))`:
12+
- Figure out which pick of the *current* row would maximize the score up to and including the *previous* row:
13+
```python
14+
prev_best = -1
15+
max_score = -1
16+
for i, a in enumerate(points[i - 1]):
17+
for j, b in enumerate(points[i]):
18+
if a + b - abs(i - j) > max_score:
19+
max_score = a + b - abs(i - j)
20+
prev_best = i
21+
```
22+
- Then if `i < len(points) - 1`, increment the total score by `points[i - 1][prev_best]`. Otherwise increment the total score by `max_score`.
23+
- Side note: I can't re-use `i` as an index-- so for those inner loops we should instead use `j` and `k`, respectively.
24+
- Now just return `score`
425

526
## Refining the problem, round 2 thoughts
27+
- This seems straightforward to implement. I'm not 100% sure it's *correct* though.
28+
- Walking through this one is going to be annoying, and I'm feeling tired, so as a poor substitute I'm going to intentionally not learn from my previous mistakes and just "test" this by making up a bunch of test problems using random `points` matrices.
629

730
## Attempted solution(s)
831
```python
9-
class Solution: # paste your code here!
10-
...
32+
class Solution:
33+
def maxPoints(self, points: List[List[int]]) -> int:
34+
if len(points) == 1:
35+
return max(points[0])
36+
score = 0
37+
for i in range(1, len(points)):
38+
prev_best = -1
39+
max_score = -1
40+
for j, a in enumerate(points[i - 1]):
41+
for k, b in enumerate(points[i]):
42+
next_score = a + b - abs(j - k)
43+
if next_score > max_score:
44+
max_score = next_score
45+
prev_best = j
46+
if i < len(points) - 1:
47+
score += points[i - 1][prev_best]
48+
else:
49+
score += max_score
50+
return score
1151
```
52+
- Given test cases pass, which is promising...
53+
- `points = [[91, 0, 35, 50, 71], [77, 35, 95, 3, 2], [67, 61, 81, 76, 42], [19, 89, 29, 80, 68], [88, 74, 62, 99, 6]]`: fail...hmmm
54+
- What about: `points = [[56, 19, 26, 75], [83, 31, 47, 100], [69, 100, 3, 4], [12, 100, 43, 12]]`: ok, also wrong... but this is a little smaller, so easier to debug?
55+
- I think the problem is actually a bit trickier than I am accounting for here:
56+
- There could be several picks with the same score
57+
- Choosing one vs. the other may be the same for *this* row (`i - 1`), but that decision could affect *previous* rows' scores.
58+
- So actually, I need to somehow track all decisions for each new row that would have maximized the score up to that point. Then somehow I'll need to go back through and figure out which specific pics to make.
59+
- I think I'm too tired to think through this fully, so I'm going to take a break here and revisit tomorrow!
60+

0 commit comments

Comments
 (0)