Skip to content

Commit bffa913

Browse files
committed
1937_Maximum_Number_of_Points_with_Cost
1 parent 37b21b4 commit bffa913

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -781,7 +781,9 @@ BFS, DFS, Dijkstra, Floyd–Warshall, Bellman-Ford, Kruskal, Prim's, Minimum Spa
781781
|57| **[338. Counting Bits](https://tinyurl.com/y5ly38nj)** | [Python](https://tinyurl.com/wu6rdaw/338_Counting_Bits.py), [Swift](https://tinyurl.com/wuja3c4/338_Counting_Bits.swift)| | Medium | --- |
782782
|58| **[1269. Number of Ways to Stay in the Same Place After Some Steps](https://tinyurl.com/y43jo8j3)** | [Python](https://tinyurl.com/wu6rdaw/1269_Number_of_Ways_to_Stay_in_the_Same_Place_After_Some_Steps.py), [Swift](https://tinyurl.com/wuja3c4/1269_Number_of_Ways_to_Stay_in_the_Same_Place_After_Some_Steps.swift)| [Art 1](https://tinyurl.com/yynv9xe2) | Hard | Loved the problem. Very versatile |
783783
|59| **[1277. Count Square Submatrices with All Ones](https://tinyurl.com/y6a82a9r)** | [Python](https://tinyurl.com/wu6rdaw/1277_Count_Square_Submatrices_with_All_Ones.py), [Swift](https://tinyurl.com/wuja3c4/1277_Count_Square_Submatrices_with_All_Ones.swift)| [Must](https://tinyurl.com/y4sa8zgk) | Medium | --- |
784-
|59| [418. Sentence Screen Fitting](https://tinyurl.com/yhtu7lld) | [Swift](https://tinyurl.com/wuja3c4/418_Sentence_Screen_Fitting.swift)| [Art 1](https://tinyurl.com/yh4xt84c) | Medium | --- |
784+
|60| [418. Sentence Screen Fitting](https://tinyurl.com/yhtu7lld) | [Swift](https://tinyurl.com/wuja3c4/418_Sentence_Screen_Fitting.swift)| [Art 1](https://tinyurl.com/yh4xt84c) | Medium | --- |
785+
|61| [1937. Maximum Number of Points with Cost](https://tinyurl.com/yhh6dmeh) | [Swift](https://tinyurl.com/wuja3c4/1937_Maximum_Number_of_Points_with_Cost.swift)| [Art 1](https://tinyurl.com/yg6hs4md) | Medium | --- |
786+
785787

786788

787789
</p>
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Brute force
2+
class Solution {
3+
func maxPoints(_ points: [[Int]]) -> Int {
4+
guard points.count > 1 else {
5+
return points[0].max()!
6+
}
7+
var (prevRowDP, currentRowDP) = (points[0], Array(repeating: Int.min, count: points[0].count))
8+
for row in 1..<points.count {
9+
for col in 0..<points[0].count {
10+
for prevCol in 0..<prevRowDP.count {
11+
currentRowDP[col] = [currentRowDP[col], points[row][col] + prevRowDP[prevCol] - abs(col - prevCol)].max()!
12+
}
13+
}
14+
prevRowDP = currentRowDP
15+
}
16+
return currentRowDP.max()!
17+
}
18+
}
19+
20+
// DP. Main algorithm from here, https://tinyurl.com/yg6hs4md
21+
class Solution {
22+
func maxPoints(_ points: [[Int]]) -> Int {
23+
if points.count == 1 {
24+
return points[0].max()!
25+
}
26+
if points[0].count == 1 {
27+
return points.compactMap { $0.reduce(0, +) }.reduce(0, +)
28+
}
29+
30+
var prevRowDP = points[0]
31+
for row in 0..<points.count - 1 {
32+
var currentRowDP = Array(repeating: 0, count: points[0].count)
33+
34+
// Build left
35+
var left = [prevRowDP[0]] + Array(repeating: 0, count: prevRowDP.count - 1)
36+
for index in 1..<left.count {
37+
left[index] = [prevRowDP[index], left[index - 1] - 1].max()!
38+
}
39+
40+
// Build right
41+
var right = Array(repeating: 0, count: prevRowDP.count - 1) + [prevRowDP.last!]
42+
for index in stride(from: right.count - 2, through: 0, by: -1) {
43+
right[index] = [prevRowDP[index], right[index + 1] - 1].max()!
44+
}
45+
46+
47+
for col in 0..<prevRowDP.count {
48+
currentRowDP[col] = [left[col], right[col]].max()! + points[row + 1][col]
49+
}
50+
prevRowDP = currentRowDP
51+
}
52+
return prevRowDP.max()!
53+
}
54+
}

0 commit comments

Comments
 (0)