Skip to content

Commit bb2782c

Browse files
committed
feat: add solutions to lc problem: No.0573
No.0573.Squirrel Simulation
1 parent 5f033d3 commit bb2782c

File tree

7 files changed

+294
-3
lines changed

7 files changed

+294
-3
lines changed

solution/0500-0599/0573.Squirrel Simulation/README.md

Lines changed: 106 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,22 +35,127 @@
3535

3636
<!-- 这里可写通用的实现逻辑 -->
3737

38+
**方法一:路径分析**
39+
40+
我们观察松鼠的移动路径,可以发现,松鼠会首先移动到某个坚果的位置,然后移动到树的位置。接下来,松鼠的移动路径之和等于“其余坚果到树的位置之和”再乘以 $2$。
41+
42+
因此,我们只需要选出一个坚果,作为松鼠的第一个目标,使得其到树的位置之和最小,即可得到最小路径。
43+
44+
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为坚果的数量。
45+
3846
<!-- tabs:start -->
3947

4048
### **Python3**
4149

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

4452
```python
45-
53+
class Solution:
54+
def minDistance(
55+
self,
56+
height: int,
57+
width: int,
58+
tree: List[int],
59+
squirrel: List[int],
60+
nuts: List[List[int]],
61+
) -> int:
62+
x, y, a, b = *tree, *squirrel
63+
s = sum(abs(i - x) + abs(j - y) for i, j in nuts) * 2
64+
ans = inf
65+
for i, j in nuts:
66+
c = abs(i - x) + abs(j - y)
67+
d = abs(i - a) + abs(j - b) + c
68+
ans = min(ans, s + d - c * 2)
69+
return ans
4670
```
4771

4872
### **Java**
4973

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

5276
```java
77+
class Solution {
78+
public int minDistance(int height, int width, int[] tree, int[] squirrel, int[][] nuts) {
79+
int ans = Integer.MAX_VALUE;
80+
int s = 0;
81+
for (int[] a : nuts) {
82+
s += f(a, tree);
83+
}
84+
s *= 2;
85+
for (int[] a : nuts) {
86+
int c = f(a, tree);
87+
int d = f(a, squirrel) + c;
88+
ans = Math.min(ans, s + d - c * 2);
89+
}
90+
return ans;
91+
}
92+
93+
private int f(int[] a, int[] b) {
94+
return Math.abs(a[0] - b[0]) + Math.abs(a[1] - b[1]);
95+
}
96+
}
97+
```
98+
99+
### **C++**
100+
101+
```cpp
102+
class Solution {
103+
public:
104+
int minDistance(int height, int width, vector<int>& tree, vector<int>& squirrel, vector<vector<int>>& nuts) {
105+
int ans = INT_MAX;
106+
int s = 0;
107+
for (auto& a : nuts) {
108+
s += f(a, tree);
109+
}
110+
s *= 2;
111+
for (auto& a : nuts) {
112+
int c = f(a, tree);
113+
int d = f(a, squirrel) + c;
114+
ans = min(ans, s + d - c * 2);
115+
}
116+
return ans;
117+
}
118+
119+
int f(vector<int>& a, vector<int>& b) {
120+
return abs(a[0] - b[0]) + abs(a[1] - b[1]);
121+
}
122+
};
123+
```
53124

125+
### **Go**
126+
127+
```go
128+
func minDistance(height int, width int, tree []int, squirrel []int, nuts [][]int) int {
129+
f := func(a, b []int) int {
130+
return abs(a[0]-b[0]) + abs(a[1]-b[1])
131+
}
132+
ans := math.MaxInt32
133+
s := 0
134+
for _, a := range nuts {
135+
s += f(a, tree)
136+
}
137+
s *= 2
138+
for _, a := range nuts {
139+
c := f(a, tree)
140+
d := f(a, squirrel) + c
141+
ans = min(ans, s+d-c*2)
142+
}
143+
return ans
144+
}
145+
146+
func min(a, b int) int {
147+
if a < b {
148+
return a
149+
}
150+
return b
151+
}
152+
153+
func abs(x int) int {
154+
if x < 0 {
155+
return -x
156+
}
157+
return x
158+
}
54159
```
55160

56161
### **...**

solution/0500-0599/0573.Squirrel Simulation/README_EN.md

Lines changed: 98 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,110 @@
5454
### **Python3**
5555

5656
```python
57-
57+
class Solution:
58+
def minDistance(
59+
self,
60+
height: int,
61+
width: int,
62+
tree: List[int],
63+
squirrel: List[int],
64+
nuts: List[List[int]],
65+
) -> int:
66+
x, y, a, b = *tree, *squirrel
67+
s = sum(abs(i - x) + abs(j - y) for i, j in nuts) * 2
68+
ans = inf
69+
for i, j in nuts:
70+
c = abs(i - x) + abs(j - y)
71+
d = abs(i - a) + abs(j - b) + c
72+
ans = min(ans, s + d - c * 2)
73+
return ans
5874
```
5975

6076
### **Java**
6177

6278
```java
79+
class Solution {
80+
public int minDistance(int height, int width, int[] tree, int[] squirrel, int[][] nuts) {
81+
int ans = Integer.MAX_VALUE;
82+
int s = 0;
83+
for (int[] a : nuts) {
84+
s += f(a, tree);
85+
}
86+
s *= 2;
87+
for (int[] a : nuts) {
88+
int c = f(a, tree);
89+
int d = f(a, squirrel) + c;
90+
ans = Math.min(ans, s + d - c * 2);
91+
}
92+
return ans;
93+
}
94+
95+
private int f(int[] a, int[] b) {
96+
return Math.abs(a[0] - b[0]) + Math.abs(a[1] - b[1]);
97+
}
98+
}
99+
```
100+
101+
### **C++**
102+
103+
```cpp
104+
class Solution {
105+
public:
106+
int minDistance(int height, int width, vector<int>& tree, vector<int>& squirrel, vector<vector<int>>& nuts) {
107+
int ans = INT_MAX;
108+
int s = 0;
109+
for (auto& a : nuts) {
110+
s += f(a, tree);
111+
}
112+
s *= 2;
113+
for (auto& a : nuts) {
114+
int c = f(a, tree);
115+
int d = f(a, squirrel) + c;
116+
ans = min(ans, s + d - c * 2);
117+
}
118+
return ans;
119+
}
120+
121+
int f(vector<int>& a, vector<int>& b) {
122+
return abs(a[0] - b[0]) + abs(a[1] - b[1]);
123+
}
124+
};
125+
```
63126

127+
### **Go**
128+
129+
```go
130+
func minDistance(height int, width int, tree []int, squirrel []int, nuts [][]int) int {
131+
f := func(a, b []int) int {
132+
return abs(a[0]-b[0]) + abs(a[1]-b[1])
133+
}
134+
ans := math.MaxInt32
135+
s := 0
136+
for _, a := range nuts {
137+
s += f(a, tree)
138+
}
139+
s *= 2
140+
for _, a := range nuts {
141+
c := f(a, tree)
142+
d := f(a, squirrel) + c
143+
ans = min(ans, s+d-c*2)
144+
}
145+
return ans
146+
}
147+
148+
func min(a, b int) int {
149+
if a < b {
150+
return a
151+
}
152+
return b
153+
}
154+
155+
func abs(x int) int {
156+
if x < 0 {
157+
return -x
158+
}
159+
return x
160+
}
64161
```
65162

66163
### **...**
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public:
3+
int minDistance(int height, int width, vector<int>& tree, vector<int>& squirrel, vector<vector<int>>& nuts) {
4+
int ans = INT_MAX;
5+
int s = 0;
6+
for (auto& a : nuts) {
7+
s += f(a, tree);
8+
}
9+
s *= 2;
10+
for (auto& a : nuts) {
11+
int c = f(a, tree);
12+
int d = f(a, squirrel) + c;
13+
ans = min(ans, s + d - c * 2);
14+
}
15+
return ans;
16+
}
17+
18+
int f(vector<int>& a, vector<int>& b) {
19+
return abs(a[0] - b[0]) + abs(a[1] - b[1]);
20+
}
21+
};
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
func minDistance(height int, width int, tree []int, squirrel []int, nuts [][]int) int {
2+
f := func(a, b []int) int {
3+
return abs(a[0]-b[0]) + abs(a[1]-b[1])
4+
}
5+
ans := math.MaxInt32
6+
s := 0
7+
for _, a := range nuts {
8+
s += f(a, tree)
9+
}
10+
s *= 2
11+
for _, a := range nuts {
12+
c := f(a, tree)
13+
d := f(a, squirrel) + c
14+
ans = min(ans, s+d-c*2)
15+
}
16+
return ans
17+
}
18+
19+
func min(a, b int) int {
20+
if a < b {
21+
return a
22+
}
23+
return b
24+
}
25+
26+
func abs(x int) int {
27+
if x < 0 {
28+
return -x
29+
}
30+
return x
31+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public int minDistance(int height, int width, int[] tree, int[] squirrel, int[][] nuts) {
3+
int ans = Integer.MAX_VALUE;
4+
int s = 0;
5+
for (int[] a : nuts) {
6+
s += f(a, tree);
7+
}
8+
s *= 2;
9+
for (int[] a : nuts) {
10+
int c = f(a, tree);
11+
int d = f(a, squirrel) + c;
12+
ans = Math.min(ans, s + d - c * 2);
13+
}
14+
return ans;
15+
}
16+
17+
private int f(int[] a, int[] b) {
18+
return Math.abs(a[0] - b[0]) + Math.abs(a[1] - b[1]);
19+
}
20+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution:
2+
def minDistance(
3+
self,
4+
height: int,
5+
width: int,
6+
tree: List[int],
7+
squirrel: List[int],
8+
nuts: List[List[int]],
9+
) -> int:
10+
x, y, a, b = *tree, *squirrel
11+
s = sum(abs(i - x) + abs(j - y) for i, j in nuts) * 2
12+
ans = inf
13+
for i, j in nuts:
14+
c = abs(i - x) + abs(j - y)
15+
d = abs(i - a) + abs(j - b) + c
16+
ans = min(ans, s + d - c * 2)
17+
return ans

solution/1700-1799/1708.Largest Subarray Length K/Solution.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ class Solution:
22
def largestSubarray(self, nums: List[int], k: int) -> List[int]:
33
mx = max(nums[: len(nums) - k + 1])
44
i = nums.index(mx)
5-
return nums[i: i + k]
5+
return nums[i : i + k]

0 commit comments

Comments
 (0)