Skip to content

Commit ce92c8c

Browse files
committed
feat: add solutions to lc problem: No.2087
No.2087.Minimum Cost Homecoming of a Robot in a Grid
1 parent 9350e85 commit ce92c8c

File tree

7 files changed

+277
-3
lines changed

7 files changed

+277
-3
lines changed

solution/2000-2099/2085.Count Common Words With One Occurrence/README.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,11 @@
5353

5454
<!-- 这里可写通用的实现逻辑 -->
5555

56-
**方法一:哈希表计数**
56+
**方法一:哈希表**
57+
58+
我们可以用两个哈希表分别统计两个字符串数组中每个字符串出现的次数,然后遍历其中一个哈希表,如果某个字符串在另一个哈希表中出现了一次,且在当前哈希表中也出现了一次,则答案加一。
59+
60+
时间复杂度 $O(n + m)$,空间复杂度 $O(n + m)$。其中 $n$ 和 $m$ 分别是两个字符串数组的长度。
5761

5862
<!-- tabs:start -->
5963

solution/2000-2099/2087.Minimum Cost Homecoming of a Robot in a Grid/README.md

+101-1
Original file line numberDiff line numberDiff line change
@@ -59,22 +59,122 @@
5959

6060
<!-- 这里可写通用的实现逻辑 -->
6161

62+
**方法一:贪心**
63+
64+
设机器人当前位置为 $(i, j)$,目标位置为 $(x, y)$。
65+
66+
- 如果 $i \lt x$,则机器人往下移动,代价为 $rowCosts[i + 1] + rowCosts[i + 2] + \cdots + rowCosts[x]$。
67+
- 如果 $i \gt x$,则机器人往上移动,代价为 $rowCosts[x] + rowCosts[x + 1] + \cdots + rowCosts[i - 1]$。
68+
- 如果 $j \lt y$,则机器人往右移动,代价为 $colCosts[j + 1] + colCosts[j + 2] + \cdots + colCosts[y]$。
69+
- 如果 $j \gt y$,则机器人往左移动,代价为 $colCosts[y] + colCosts[y + 1] + \cdots + colCosts[j - 1]$。
70+
71+
时间复杂度 $O(m + n)$,空间复杂度 $O(1)$。其中 $m$ 和 $n$ 分别为 $rowCosts$ 和 $colCosts$ 的长度。
72+
6273
<!-- tabs:start -->
6374

6475
### **Python3**
6576

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

6879
```python
69-
80+
class Solution:
81+
def minCost(self, startPos: List[int], homePos: List[int], rowCosts: List[int], colCosts: List[int]) -> int:
82+
i, j = startPos
83+
x, y = homePos
84+
ans = 0
85+
if i < x:
86+
ans += sum(rowCosts[i + 1: x + 1])
87+
else:
88+
ans += sum(rowCosts[x: i])
89+
if j < y:
90+
ans += sum(colCosts[j + 1: y + 1])
91+
else:
92+
ans += sum(colCosts[y: j])
93+
return ans
7094
```
7195

7296
### **Java**
7397

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

76100
```java
101+
class Solution {
102+
public int minCost(int[] startPos, int[] homePos, int[] rowCosts, int[] colCosts) {
103+
int i = startPos[0], j = startPos[1];
104+
int x = homePos[0], y = homePos[1];
105+
int ans = 0;
106+
if (i < x) {
107+
for (int k = i + 1; k <= x; ++k) {
108+
ans += rowCosts[k];
109+
}
110+
} else {
111+
for (int k = x; k < i; ++k) {
112+
ans += rowCosts[k];
113+
}
114+
}
115+
if (j < y) {
116+
for (int k = j + 1; k <= y; ++k) {
117+
ans += colCosts[k];
118+
}
119+
} else {
120+
for (int k = y; k < j; ++k) {
121+
ans += colCosts[k];
122+
}
123+
}
124+
return ans;
125+
}
126+
}
127+
```
128+
129+
### **C++**
130+
131+
```cpp
132+
class Solution {
133+
public:
134+
int minCost(vector<int>& startPos, vector<int>& homePos, vector<int>& rowCosts, vector<int>& colCosts) {
135+
int i = startPos[0], j = startPos[1];
136+
int x = homePos[0], y = homePos[1];
137+
int ans = 0;
138+
if (i < x) {
139+
ans += accumulate(rowCosts.begin() + i + 1, rowCosts.begin() + x + 1, 0);
140+
} else {
141+
ans += accumulate(rowCosts.begin() + x, rowCosts.begin() + i, 0);
142+
}
143+
if (j < y) {
144+
ans += accumulate(colCosts.begin() + j + 1, colCosts.begin() + y + 1, 0);
145+
} else {
146+
ans += accumulate(colCosts.begin() + y, colCosts.begin() + j, 0);
147+
}
148+
return ans;
149+
}
150+
};
151+
```
77152
153+
### **Go**
154+
155+
```go
156+
func minCost(startPos []int, homePos []int, rowCosts []int, colCosts []int) (ans int) {
157+
i, j := startPos[0], startPos[1]
158+
x, y := homePos[0], homePos[1]
159+
if i < x {
160+
ans += sum(rowCosts, i+1, x+1)
161+
} else {
162+
ans += sum(rowCosts, x, i)
163+
}
164+
if j < y {
165+
ans += sum(colCosts, j+1, y+1)
166+
} else {
167+
ans += sum(colCosts, y, j)
168+
}
169+
return
170+
}
171+
172+
func sum(nums []int, i, j int) (s int) {
173+
for k := i; k < j; k++ {
174+
s += nums[k]
175+
}
176+
return
177+
}
78178
```
79179

80180
### **...**

solution/2000-2099/2087.Minimum Cost Homecoming of a Robot in a Grid/README_EN.md

+90-1
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,102 @@ The total cost is 3 + 2 + 6 + 7 = 18</pre>
5858
### **Python3**
5959

6060
```python
61-
61+
class Solution:
62+
def minCost(self, startPos: List[int], homePos: List[int], rowCosts: List[int], colCosts: List[int]) -> int:
63+
i, j = startPos
64+
x, y = homePos
65+
ans = 0
66+
if i < x:
67+
ans += sum(rowCosts[i + 1: x + 1])
68+
else:
69+
ans += sum(rowCosts[x: i])
70+
if j < y:
71+
ans += sum(colCosts[j + 1: y + 1])
72+
else:
73+
ans += sum(colCosts[y: j])
74+
return ans
6275
```
6376

6477
### **Java**
6578

6679
```java
80+
class Solution {
81+
public int minCost(int[] startPos, int[] homePos, int[] rowCosts, int[] colCosts) {
82+
int i = startPos[0], j = startPos[1];
83+
int x = homePos[0], y = homePos[1];
84+
int ans = 0;
85+
if (i < x) {
86+
for (int k = i + 1; k <= x; ++k) {
87+
ans += rowCosts[k];
88+
}
89+
} else {
90+
for (int k = x; k < i; ++k) {
91+
ans += rowCosts[k];
92+
}
93+
}
94+
if (j < y) {
95+
for (int k = j + 1; k <= y; ++k) {
96+
ans += colCosts[k];
97+
}
98+
} else {
99+
for (int k = y; k < j; ++k) {
100+
ans += colCosts[k];
101+
}
102+
}
103+
return ans;
104+
}
105+
}
106+
```
107+
108+
### **C++**
109+
110+
```cpp
111+
class Solution {
112+
public:
113+
int minCost(vector<int>& startPos, vector<int>& homePos, vector<int>& rowCosts, vector<int>& colCosts) {
114+
int i = startPos[0], j = startPos[1];
115+
int x = homePos[0], y = homePos[1];
116+
int ans = 0;
117+
if (i < x) {
118+
ans += accumulate(rowCosts.begin() + i + 1, rowCosts.begin() + x + 1, 0);
119+
} else {
120+
ans += accumulate(rowCosts.begin() + x, rowCosts.begin() + i, 0);
121+
}
122+
if (j < y) {
123+
ans += accumulate(colCosts.begin() + j + 1, colCosts.begin() + y + 1, 0);
124+
} else {
125+
ans += accumulate(colCosts.begin() + y, colCosts.begin() + j, 0);
126+
}
127+
return ans;
128+
}
129+
};
130+
```
67131
132+
### **Go**
133+
134+
```go
135+
func minCost(startPos []int, homePos []int, rowCosts []int, colCosts []int) (ans int) {
136+
i, j := startPos[0], startPos[1]
137+
x, y := homePos[0], homePos[1]
138+
if i < x {
139+
ans += sum(rowCosts, i+1, x+1)
140+
} else {
141+
ans += sum(rowCosts, x, i)
142+
}
143+
if j < y {
144+
ans += sum(colCosts, j+1, y+1)
145+
} else {
146+
ans += sum(colCosts, y, j)
147+
}
148+
return
149+
}
150+
151+
func sum(nums []int, i, j int) (s int) {
152+
for k := i; k < j; k++ {
153+
s += nums[k]
154+
}
155+
return
156+
}
68157
```
69158

70159
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public:
3+
int minCost(vector<int>& startPos, vector<int>& homePos, vector<int>& rowCosts, vector<int>& colCosts) {
4+
int i = startPos[0], j = startPos[1];
5+
int x = homePos[0], y = homePos[1];
6+
int ans = 0;
7+
if (i < x) {
8+
ans += accumulate(rowCosts.begin() + i + 1, rowCosts.begin() + x + 1, 0);
9+
} else {
10+
ans += accumulate(rowCosts.begin() + x, rowCosts.begin() + i, 0);
11+
}
12+
if (j < y) {
13+
ans += accumulate(colCosts.begin() + j + 1, colCosts.begin() + y + 1, 0);
14+
} else {
15+
ans += accumulate(colCosts.begin() + y, colCosts.begin() + j, 0);
16+
}
17+
return ans;
18+
}
19+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
func minCost(startPos []int, homePos []int, rowCosts []int, colCosts []int) (ans int) {
2+
i, j := startPos[0], startPos[1]
3+
x, y := homePos[0], homePos[1]
4+
if i < x {
5+
ans += sum(rowCosts, i+1, x+1)
6+
} else {
7+
ans += sum(rowCosts, x, i)
8+
}
9+
if j < y {
10+
ans += sum(colCosts, j+1, y+1)
11+
} else {
12+
ans += sum(colCosts, y, j)
13+
}
14+
return
15+
}
16+
17+
func sum(nums []int, i, j int) (s int) {
18+
for k := i; k < j; k++ {
19+
s += nums[k]
20+
}
21+
return
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
public int minCost(int[] startPos, int[] homePos, int[] rowCosts, int[] colCosts) {
3+
int i = startPos[0], j = startPos[1];
4+
int x = homePos[0], y = homePos[1];
5+
int ans = 0;
6+
if (i < x) {
7+
for (int k = i + 1; k <= x; ++k) {
8+
ans += rowCosts[k];
9+
}
10+
} else {
11+
for (int k = x; k < i; ++k) {
12+
ans += rowCosts[k];
13+
}
14+
}
15+
if (j < y) {
16+
for (int k = j + 1; k <= y; ++k) {
17+
ans += colCosts[k];
18+
}
19+
} else {
20+
for (int k = y; k < j; ++k) {
21+
ans += colCosts[k];
22+
}
23+
}
24+
return ans;
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution:
2+
def minCost(self, startPos: List[int], homePos: List[int], rowCosts: List[int], colCosts: List[int]) -> int:
3+
i, j = startPos
4+
x, y = homePos
5+
ans = 0
6+
if i < x:
7+
ans += sum(rowCosts[i + 1: x + 1])
8+
else:
9+
ans += sum(rowCosts[x: i])
10+
if j < y:
11+
ans += sum(colCosts[j + 1: y + 1])
12+
else:
13+
ans += sum(colCosts[y: j])
14+
return ans

0 commit comments

Comments
 (0)