Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit deaf2ae

Browse files
feat: add solutions to lc problem: No.1937 (doocs#1635)
No.1937.Maximum Number of Points with Cost
1 parent a043f1b commit deaf2ae

File tree

7 files changed

+384
-2
lines changed

7 files changed

+384
-2
lines changed

‎solution/1900-1999/1937.Maximum Number of Points with Cost/README.md‎

Lines changed: 146 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,22 +63,167 @@
6363

6464
<!-- 这里可写通用的实现逻辑 -->
6565

66+
**方法一:动态规划**
67+
68+
我们定义 $f[i][j]$ 表示选取前 $i-1$ 行,并且第 $i-1$ 行选择第 $j$ 列的格子时的最大得分。初始时 $f[0][j] = points[0][j]$。
69+
70+
对于 $i > 0$ 的情况,对于 $f[i][j],ドル我们考虑是从上一行的哪一列转移过来的,记上一行选择的列为 $k,ドル那么有:
71+
72+
$$
73+
f[i][j]=
74+
\begin{cases}
75+
\max(f[i - 1][k] + k - j + points[i][j]), & 0 \le k < j \\
76+
\max(f[i - 1][k] - k + j + points[i][j]), & j < k < n
77+
\end{cases}
78+
$$
79+
80+
其中 $n$ 表示列数。答案为 $\max\limits_{0 \le j < n} f[m - 1][j]$。
81+
82+
我们注意到 $f[i]$ 的值只跟 $f[i-1]$ 的值有关,因此我们可以使用滚动数组优化空间复杂度。
83+
84+
时间复杂度 $O(m \times n),ドル空间复杂度 $O(n)$。其中 $m$ 和 $n$ 分别是矩阵的行数和列数。
85+
6686
<!-- tabs:start -->
6787

6888
### **Python3**
6989

7090
<!-- 这里可写当前语言的特殊实现逻辑 -->
7191

7292
```python
73-
93+
class Solution:
94+
def maxPoints(self, points: List[List[int]]) -> int:
95+
n = len(points[0])
96+
f = points[0][:]
97+
for p in points[1:]:
98+
g = [0] * n
99+
lmx = -inf
100+
for j in range(n):
101+
lmx = max(lmx, f[j] + j)
102+
g[j] = max(g[j], p[j] + lmx - j)
103+
rmx = -inf
104+
for j in range(n - 1, -1, -1):
105+
rmx = max(rmx, f[j] - j)
106+
g[j] = max(g[j], p[j] + rmx + j)
107+
f = g
108+
return max(f)
74109
```
75110

76111
### **Java**
77112

78113
<!-- 这里可写当前语言的特殊实现逻辑 -->
79114

80115
```java
116+
class Solution {
117+
public long maxPoints(int[][] points) {
118+
int n = points[0].length;
119+
long[] f = new long[n];
120+
final long inf = 1L << 60;
121+
for (int[] p : points) {
122+
long[] g = new long[n];
123+
long lmx = -inf, rmx = -inf;
124+
for (int j = 0; j < n; ++j) {
125+
lmx = Math.max(lmx, f[j] + j);
126+
g[j] = Math.max(g[j], p[j] + lmx - j);
127+
}
128+
for (int j = n - 1; j >= 0; --j) {
129+
rmx = Math.max(rmx, f[j] - j);
130+
g[j] = Math.max(g[j], p[j] + rmx + j);
131+
}
132+
f = g;
133+
}
134+
long ans = 0;
135+
for (long x : f) {
136+
ans = Math.max(ans, x);
137+
}
138+
return ans;
139+
}
140+
}
141+
```
142+
143+
### **C++**
144+
145+
```cpp
146+
class Solution {
147+
public:
148+
long long maxPoints(vector<vector<int>>& points) {
149+
using ll = long long;
150+
int n = points[0].size();
151+
vector<ll> f(n);
152+
const ll inf = 1e18;
153+
for (auto& p : points) {
154+
vector<ll> g(n);
155+
ll lmx = -inf, rmx = -inf;
156+
for (int j = 0; j < n; ++j) {
157+
lmx = max(lmx, f[j] + j);
158+
g[j] = max(g[j], p[j] + lmx - j);
159+
}
160+
for (int j = n - 1; ~j; --j) {
161+
rmx = max(rmx, f[j] - j);
162+
g[j] = max(g[j], p[j] + rmx + j);
163+
}
164+
f = move(g);
165+
}
166+
return *max_element(f.begin(), f.end());
167+
}
168+
};
169+
```
170+
171+
### **Go**
172+
173+
```go
174+
func maxPoints(points [][]int) (ans int64) {
175+
n := len(points[0])
176+
const inf int64 = 1e18
177+
f := make([]int64, n)
178+
for _, p := range points {
179+
g := make([]int64, n)
180+
lmx, rmx := -inf, -inf
181+
for j := range p {
182+
lmx = max(lmx, f[j]+int64(j))
183+
g[j] = max(g[j], int64(p[j])+lmx-int64(j))
184+
}
185+
for j := n - 1; j >= 0; j-- {
186+
rmx = max(rmx, f[j]-int64(j))
187+
g[j] = max(g[j], int64(p[j])+rmx+int64(j))
188+
}
189+
f = g
190+
}
191+
for _, x := range f {
192+
ans = max(ans, x)
193+
}
194+
return
195+
}
196+
197+
func max(a, b int64) int64 {
198+
if a > b {
199+
return a
200+
}
201+
return b
202+
}
203+
```
81204

205+
### **TypeScript**
206+
207+
```ts
208+
function maxPoints(points: number[][]): number {
209+
const n = points[0].length;
210+
const f: number[] = new Array(n).fill(0);
211+
for (const p of points) {
212+
const g: number[] = new Array(n).fill(0);
213+
let lmx = -Infinity;
214+
let rmx = -Infinity;
215+
for (let j = 0; j < n; ++j) {
216+
lmx = Math.max(lmx, f[j] + j);
217+
g[j] = Math.max(g[j], p[j] + lmx - j);
218+
}
219+
for (let j = n - 1; ~j; --j) {
220+
rmx = Math.max(rmx, f[j] - j);
221+
g[j] = Math.max(g[j], p[j] + rmx + j);
222+
}
223+
f.splice(0, n, ...g);
224+
}
225+
return Math.max(...f);
226+
}
82227
```
83228

84229
### **...**

‎solution/1900-1999/1937.Maximum Number of Points with Cost/README_EN.md‎

Lines changed: 126 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,138 @@ Your final score is 12 - 1 = 11.
6262
### **Python3**
6363

6464
```python
65-
65+
class Solution:
66+
def maxPoints(self, points: List[List[int]]) -> int:
67+
n = len(points[0])
68+
f = points[0][:]
69+
for p in points[1:]:
70+
g = [0] * n
71+
lmx = -inf
72+
for j in range(n):
73+
lmx = max(lmx, f[j] + j)
74+
g[j] = max(g[j], p[j] + lmx - j)
75+
rmx = -inf
76+
for j in range(n - 1, -1, -1):
77+
rmx = max(rmx, f[j] - j)
78+
g[j] = max(g[j], p[j] + rmx + j)
79+
f = g
80+
return max(f)
6681
```
6782

6883
### **Java**
6984

7085
```java
86+
class Solution {
87+
public long maxPoints(int[][] points) {
88+
int n = points[0].length;
89+
long[] f = new long[n];
90+
final long inf = 1L << 60;
91+
for (int[] p : points) {
92+
long[] g = new long[n];
93+
long lmx = -inf, rmx = -inf;
94+
for (int j = 0; j < n; ++j) {
95+
lmx = Math.max(lmx, f[j] + j);
96+
g[j] = Math.max(g[j], p[j] + lmx - j);
97+
}
98+
for (int j = n - 1; j >= 0; --j) {
99+
rmx = Math.max(rmx, f[j] - j);
100+
g[j] = Math.max(g[j], p[j] + rmx + j);
101+
}
102+
f = g;
103+
}
104+
long ans = 0;
105+
for (long x : f) {
106+
ans = Math.max(ans, x);
107+
}
108+
return ans;
109+
}
110+
}
111+
```
112+
113+
### **C++**
114+
115+
```cpp
116+
class Solution {
117+
public:
118+
long long maxPoints(vector<vector<int>>& points) {
119+
using ll = long long;
120+
int n = points[0].size();
121+
vector<ll> f(n);
122+
const ll inf = 1e18;
123+
for (auto& p : points) {
124+
vector<ll> g(n);
125+
ll lmx = -inf, rmx = -inf;
126+
for (int j = 0; j < n; ++j) {
127+
lmx = max(lmx, f[j] + j);
128+
g[j] = max(g[j], p[j] + lmx - j);
129+
}
130+
for (int j = n - 1; ~j; --j) {
131+
rmx = max(rmx, f[j] - j);
132+
g[j] = max(g[j], p[j] + rmx + j);
133+
}
134+
f = move(g);
135+
}
136+
return *max_element(f.begin(), f.end());
137+
}
138+
};
139+
```
140+
141+
### **Go**
142+
143+
```go
144+
func maxPoints(points [][]int) (ans int64) {
145+
n := len(points[0])
146+
const inf int64 = 1e18
147+
f := make([]int64, n)
148+
for _, p := range points {
149+
g := make([]int64, n)
150+
lmx, rmx := -inf, -inf
151+
for j := range p {
152+
lmx = max(lmx, f[j]+int64(j))
153+
g[j] = max(g[j], int64(p[j])+lmx-int64(j))
154+
}
155+
for j := n - 1; j >= 0; j-- {
156+
rmx = max(rmx, f[j]-int64(j))
157+
g[j] = max(g[j], int64(p[j])+rmx+int64(j))
158+
}
159+
f = g
160+
}
161+
for _, x := range f {
162+
ans = max(ans, x)
163+
}
164+
return
165+
}
166+
167+
func max(a, b int64) int64 {
168+
if a > b {
169+
return a
170+
}
171+
return b
172+
}
173+
```
71174

175+
### **TypeScript**
176+
177+
```ts
178+
function maxPoints(points: number[][]): number {
179+
const n = points[0].length;
180+
const f: number[] = new Array(n).fill(0);
181+
for (const p of points) {
182+
const g: number[] = new Array(n).fill(0);
183+
let lmx = -Infinity;
184+
let rmx = -Infinity;
185+
for (let j = 0; j < n; ++j) {
186+
lmx = Math.max(lmx, f[j] + j);
187+
g[j] = Math.max(g[j], p[j] + lmx - j);
188+
}
189+
for (let j = n - 1; ~j; --j) {
190+
rmx = Math.max(rmx, f[j] - j);
191+
g[j] = Math.max(g[j], p[j] + rmx + j);
192+
}
193+
f.splice(0, n, ...g);
194+
}
195+
return Math.max(...f);
196+
}
72197
```
73198

74199
### **...**
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public:
3+
long long maxPoints(vector<vector<int>>& points) {
4+
using ll = long long;
5+
int n = points[0].size();
6+
vector<ll> f(n);
7+
const ll inf = 1e18;
8+
for (auto& p : points) {
9+
vector<ll> g(n);
10+
ll lmx = -inf, rmx = -inf;
11+
for (int j = 0; j < n; ++j) {
12+
lmx = max(lmx, f[j] + j);
13+
g[j] = max(g[j], p[j] + lmx - j);
14+
}
15+
for (int j = n - 1; ~j; --j) {
16+
rmx = max(rmx, f[j] - j);
17+
g[j] = max(g[j], p[j] + rmx + j);
18+
}
19+
f = move(g);
20+
}
21+
return *max_element(f.begin(), f.end());
22+
}
23+
};
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
func maxPoints(points [][]int) (ans int64) {
2+
n := len(points[0])
3+
const inf int64 = 1e18
4+
f := make([]int64, n)
5+
for _, p := range points {
6+
g := make([]int64, n)
7+
lmx, rmx := -inf, -inf
8+
for j := range p {
9+
lmx = max(lmx, f[j]+int64(j))
10+
g[j] = max(g[j], int64(p[j])+lmx-int64(j))
11+
}
12+
for j := n - 1; j >= 0; j-- {
13+
rmx = max(rmx, f[j]-int64(j))
14+
g[j] = max(g[j], int64(p[j])+rmx+int64(j))
15+
}
16+
f = g
17+
}
18+
for _, x := range f {
19+
ans = max(ans, x)
20+
}
21+
return
22+
}
23+
24+
func max(a, b int64) int64 {
25+
if a > b {
26+
return a
27+
}
28+
return b
29+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
public long maxPoints(int[][] points) {
3+
int n = points[0].length;
4+
long[] f = new long[n];
5+
final long inf = 1L << 60;
6+
for (int[] p : points) {
7+
long[] g = new long[n];
8+
long lmx = -inf, rmx = -inf;
9+
for (int j = 0; j < n; ++j) {
10+
lmx = Math.max(lmx, f[j] + j);
11+
g[j] = Math.max(g[j], p[j] + lmx - j);
12+
}
13+
for (int j = n - 1; j >= 0; --j) {
14+
rmx = Math.max(rmx, f[j] - j);
15+
g[j] = Math.max(g[j], p[j] + rmx + j);
16+
}
17+
f = g;
18+
}
19+
long ans = 0;
20+
for (long x : f) {
21+
ans = Math.max(ans, x);
22+
}
23+
return ans;
24+
}
25+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /