Skip to content

Commit 3ff3994

Browse files
authoredOct 9, 2023
feat: add solutions to lc/lcci problems (#1768)
* lc No.2579.Count Total Number of Colored Cells * lcci No.16.13.Bisect Squares
1 parent ae1ca48 commit 3ff3994

File tree

13 files changed

+515
-11
lines changed

13 files changed

+515
-11
lines changed
 

‎lcci/16.13.Bisect Squares/README.md

+170-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
## 题目描述
66

77
<!-- 这里写题目描述 -->
8+
89
<p>给定两个正方形及一个二维平面。请找出将这两个正方形分割成两半的一条直线。假设正方形顶边和底边与 x 轴平行。</p>
910
<p>每个正方形的数据<code>square</code>包含3个数值,正方形的左下顶点坐标<code>[X,Y] = [square[0],square[1]]</code>,以及正方形的边长<code>square[2]</code>。所求直线穿过两个正方形会形成4个交点,请返回4个交点形成线段的两端点坐标(两个端点即为4个交点中距离最远的2个点,这2个点所连成的线段一定会穿过另外2个交点)。2个端点坐标<code>[X<sub>1</sub>,Y<sub>1</sub>]</code>和<code>[X<sub>2</sub>,Y<sub>2</sub>]</code>的返回格式为<code>{X<sub>1</sub>,Y<sub>1</sub>,X<sub>2</sub>,Y<sub>2</sub>}</code>,要求若<code>X<sub>1</sub> != X<sub>2</sub></code>,需保证<code>X<sub>1</sub> &lt; X<sub>2</sub></code>,否则需保证<code>Y<sub>1</sub> &lt;= Y<sub>2</sub></code>。</p>
1011
<p>若同时有多条直线满足要求,则选择斜率最大的一条计算并返回(与Y轴平行的直线视为斜率无穷大)。</p>
@@ -25,22 +26,190 @@ square2 = {0, -1, 2}
2526

2627
<!-- 这里可写通用的实现逻辑 -->
2728

29+
**方法一:几何数学**
30+
31+
我们知道,如果一条直线可以将两个正方形平分,那么这条直线一定会经过两个正方形的中心点。因此,我们可以先求出两个正方形的中心点,分别记为 $(x_1, y_1)$ 和 $(x_2, y_2)$。
32+
33+
如果 $x_1 = x_2$,那么这条直线一定垂直于 $x$ 轴,此时我们只需要求出两个正方形的上下边界的交点即可。
34+
35+
否则,我们可以根据两个中心点求出这条直线的斜率 $k$ 和截距 $b$,然后根据斜率的绝对值的大小,分为两种情况:
36+
37+
- 当 $|k| \gt 1$ 时,经过两个中心点的直线与两个正方形的上下边相交,我们求出上下边的纵坐标的最大值和最小值,然后根据直线方程求出对应的横坐标,即为两个正方形的交点。
38+
- 当 $|k| \le 1$ 时,经过两个中心点的直线与两个正方形的左右边相交,我们求出左右边的横坐标的最大值和最小值,然后根据直线方程求出对应的纵坐标,即为两个正方形的交点。
39+
40+
时间复杂度 $O(1)$,空间复杂度 $O(1)$。
41+
2842
<!-- tabs:start -->
2943

3044
### **Python3**
3145

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

3448
```python
35-
49+
class Solution:
50+
def cutSquares(self, square1: List[int], square2: List[int]) -> List[float]:
51+
x1, y1 = square1[0] + square1[2] / 2, square1[1] + square1[2] / 2
52+
x2, y2 = square2[0] + square2[2] / 2, square2[1] + square2[2] / 2
53+
if x1 == x2:
54+
y3 = min(square1[1], square2[1])
55+
y4 = max(square1[1] + square1[2], square2[1] + square2[2])
56+
return [x1, y3, x2, y4]
57+
k = (y2 - y1) / (x2 - x1)
58+
b = y1 - k * x1
59+
if abs(k) > 1:
60+
y3 = min(square1[1], square2[1])
61+
x3 = (y3 - b) / k
62+
y4 = max(square1[1] + square1[2], square2[1] + square2[2])
63+
x4 = (y4 - b) / k
64+
if x3 > x4 or (x3 == x4 and y3 > y4):
65+
x3, y3, x4, y4 = x4, y4, x3, y3
66+
else:
67+
x3 = min(square1[0], square2[0])
68+
y3 = k * x3 + b
69+
x4 = max(square1[0] + square1[2], square2[0] + square2[2])
70+
y4 = k * x4 + b
71+
return [x3, y3, x4, y4]
3672
```
3773

3874
### **Java**
3975

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

4278
```java
79+
class Solution {
80+
public double[] cutSquares(int[] square1, int[] square2) {
81+
double x1 = square1[0] + square1[2] / 2.0;
82+
double y1 = square1[1] + square1[2] / 2.0;
83+
double x2 = square2[0] + square2[2] / 2.0;
84+
double y2 = square2[1] + square2[2] / 2.0;
85+
if (x1 == x2) {
86+
double y3 = Math.min(square1[1], square2[1]);
87+
double y4 = Math.max(square1[1] + square1[2], square2[1] + square2[2]);
88+
return new double[] {x1, y3, x2, y4};
89+
}
90+
double k = (y2 - y1) / (x2 - x1);
91+
double b = y1 - k * x1;
92+
if (Math.abs(k) > 1) {
93+
double y3 = Math.min(square1[1], square2[1]);
94+
double x3 = (y3 - b) / k;
95+
double y4 = Math.max(square1[1] + square1[2], square2[1] + square2[2]);
96+
double x4 = (y4 - b) / k;
97+
if (x3 > x4 || (x3 == x4 && y3 > y4)) {
98+
return new double[] {x4, y4, x3, y3};
99+
}
100+
return new double[] {x3, y3, x4, y4};
101+
} else {
102+
double x3 = Math.min(square1[0], square2[0]);
103+
double y3 = k * x3 + b;
104+
double x4 = Math.max(square1[0] + square1[2], square2[0] + square2[2]);
105+
double y4 = k * x4 + b;
106+
return new double[] {x3, y3, x4, y4};
107+
}
108+
}
109+
}
110+
```
111+
112+
### **C++**
113+
114+
```cpp
115+
class Solution {
116+
public:
117+
vector<double> cutSquares(vector<int>& square1, vector<int>& square2) {
118+
double x1 = square1[0] + square1[2] / 2.0;
119+
double y1 = square1[1] + square1[2] / 2.0;
120+
double x2 = square2[0] + square2[2] / 2.0;
121+
double y2 = square2[1] + square2[2] / 2.0;
122+
if (x1 == x2) {
123+
double y3 = min(square1[1], square2[1]);
124+
double y4 = max(square1[1] + square1[2], square2[1] + square2[2]);
125+
return {x1, y3, x2, y4};
126+
}
127+
double k = (y2 - y1) / (x2 - x1);
128+
double b = y1 - k * x1;
129+
if (abs(k) > 1) {
130+
double y3 = min(square1[1], square2[1]);
131+
double x3 = (y3 - b) / k;
132+
double y4 = max(square1[1] + square1[2], square2[1] + square2[2]);
133+
double x4 = (y4 - b) / k;
134+
if (x3 > x4 || (x3 == x4 && y3 > y4)) {
135+
return {x4, y4, x3, y3};
136+
}
137+
return {x3, y3, x4, y4};
138+
} else {
139+
double x3 = min(square1[0], square2[0]);
140+
double y3 = k * x3 + b;
141+
double x4 = max(square1[0] + square1[2], square2[0] + square2[2]);
142+
double y4 = k * x4 + b;
143+
return {x3, y3, x4, y4};
144+
}
145+
}
146+
};
147+
```
148+
149+
### **Go**
150+
151+
```go
152+
func cutSquares(square1 []int, square2 []int) []float64 {
153+
x1, y1 := float64(square1[0])+float64(square1[2])/2, float64(square1[1])+float64(square1[2])/2
154+
x2, y2 := float64(square2[0])+float64(square2[2])/2, float64(square2[1])+float64(square2[2])/2
155+
if x1 == x2 {
156+
y3 := math.Min(float64(square1[1]), float64(square2[1]))
157+
y4 := math.Max(float64(square1[1]+square1[2]), float64(square2[1]+square2[2]))
158+
return []float64{x1, y3, x2, y4}
159+
}
160+
k := (y2 - y1) / (x2 - x1)
161+
b := y1 - k*x1
162+
if math.Abs(k) > 1 {
163+
y3 := math.Min(float64(square1[1]), float64(square2[1]))
164+
x3 := (y3 - b) / k
165+
y4 := math.Max(float64(square1[1]+square1[2]), float64(square2[1]+square2[2]))
166+
x4 := (y4 - b) / k
167+
if x3 > x4 || (x3 == x4 && y3 > y4) {
168+
return []float64{x4, y4, x3, y3}
169+
}
170+
return []float64{x3, y3, x4, y4}
171+
} else {
172+
x3 := math.Min(float64(square1[0]), float64(square2[0]))
173+
y3 := k*x3 + b
174+
x4 := math.Max(float64(square1[0]+square1[2]), float64(square2[0]+square2[2]))
175+
y4 := k*x4 + b
176+
return []float64{x3, y3, x4, y4}
177+
}
178+
}
179+
```
43180

181+
### **TypeScript**
182+
183+
```ts
184+
function cutSquares(square1: number[], square2: number[]): number[] {
185+
const x1 = square1[0] + square1[2] / 2;
186+
const y1 = square1[1] + square1[2] / 2;
187+
const x2 = square2[0] + square2[2] / 2;
188+
const y2 = square2[1] + square2[2] / 2;
189+
if (x1 === x2) {
190+
const y3 = Math.min(square1[1], square2[1]);
191+
const y4 = Math.max(square1[1] + square1[2], square2[1] + square2[2]);
192+
return [x1, y3, x2, y4];
193+
}
194+
const k = (y2 - y1) / (x2 - x1);
195+
const b = y1 - k * x1;
196+
if (Math.abs(k) > 1) {
197+
const y3 = Math.min(square1[1], square2[1]);
198+
const x3 = (y3 - b) / k;
199+
const y4 = Math.max(square1[1] + square1[2], square2[1] + square2[2]);
200+
const x4 = (y4 - b) / k;
201+
if (x3 > x4 || (x3 === x4 && y3 > y4)) {
202+
return [x4, y4, x3, y3];
203+
}
204+
return [x3, y3, x4, y4];
205+
} else {
206+
const x3 = Math.min(square1[0], square2[0]);
207+
const y3 = k * x3 + b;
208+
const x4 = Math.max(square1[0] + square1[2], square2[0] + square2[2]);
209+
const y4 = k * x4 + b;
210+
return [x3, y3, x4, y4];
211+
}
212+
}
44213
```
45214

46215
### **...**

‎lcci/16.13.Bisect Squares/README_EN.md

+169-1
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,186 @@ square2 = {0, -1, 2}
2929

3030
## Solutions
3131

32+
**Solution 1: Geometric Mathematics**
33+
34+
We know that if a line can bisect two squares, then the line must pass through the centers of the two squares. Therefore, we can first calculate the centers of the two squares, denoted as $(x_1, y_1)$ and $(x_2, y_2)$, respectively.
35+
36+
If $x_1 = x_2$, then the line is perpendicular to the $x$-axis, and we only need to find the intersection point of the top and bottom edges of the two squares.
37+
38+
Otherwise, we can calculate the slope $k$ and the intercept $b$ of the line passing through the two centers, and then divide into two cases based on the absolute value of the slope:
39+
40+
- When $|k| \gt 1$, the line passing through the two centers intersects with the top and bottom edges of the two squares. We calculate the maximum and minimum values of the vertical coordinates of the top and bottom edges, and then calculate the corresponding horizontal coordinate using the equation of the line, which is the intersection point of the two squares.
41+
- When $|k| \le 1$, the line passing through the two centers intersects with the left and right edges of the two squares. We calculate the maximum and minimum values of the horizontal coordinates of the left and right edges, and then calculate the corresponding vertical coordinate using the equation of the line, which is the intersection point of the two squares.
42+
43+
The time complexity is $O(1)$, and the space complexity is $O(1)$.
44+
3245
<!-- tabs:start -->
3346

3447
### **Python3**
3548

3649
```python
37-
50+
class Solution:
51+
def cutSquares(self, square1: List[int], square2: List[int]) -> List[float]:
52+
x1, y1 = square1[0] + square1[2] / 2, square1[1] + square1[2] / 2
53+
x2, y2 = square2[0] + square2[2] / 2, square2[1] + square2[2] / 2
54+
if x1 == x2:
55+
y3 = min(square1[1], square2[1])
56+
y4 = max(square1[1] + square1[2], square2[1] + square2[2])
57+
return [x1, y3, x2, y4]
58+
k = (y2 - y1) / (x2 - x1)
59+
b = y1 - k * x1
60+
if abs(k) > 1:
61+
y3 = min(square1[1], square2[1])
62+
x3 = (y3 - b) / k
63+
y4 = max(square1[1] + square1[2], square2[1] + square2[2])
64+
x4 = (y4 - b) / k
65+
if x3 > x4 or (x3 == x4 and y3 > y4):
66+
x3, y3, x4, y4 = x4, y4, x3, y3
67+
else:
68+
x3 = min(square1[0], square2[0])
69+
y3 = k * x3 + b
70+
x4 = max(square1[0] + square1[2], square2[0] + square2[2])
71+
y4 = k * x4 + b
72+
return [x3, y3, x4, y4]
3873
```
3974

4075
### **Java**
4176

4277
```java
78+
class Solution {
79+
public double[] cutSquares(int[] square1, int[] square2) {
80+
double x1 = square1[0] + square1[2] / 2.0;
81+
double y1 = square1[1] + square1[2] / 2.0;
82+
double x2 = square2[0] + square2[2] / 2.0;
83+
double y2 = square2[1] + square2[2] / 2.0;
84+
if (x1 == x2) {
85+
double y3 = Math.min(square1[1], square2[1]);
86+
double y4 = Math.max(square1[1] + square1[2], square2[1] + square2[2]);
87+
return new double[] {x1, y3, x2, y4};
88+
}
89+
double k = (y2 - y1) / (x2 - x1);
90+
double b = y1 - k * x1;
91+
if (Math.abs(k) > 1) {
92+
double y3 = Math.min(square1[1], square2[1]);
93+
double x3 = (y3 - b) / k;
94+
double y4 = Math.max(square1[1] + square1[2], square2[1] + square2[2]);
95+
double x4 = (y4 - b) / k;
96+
if (x3 > x4 || (x3 == x4 && y3 > y4)) {
97+
return new double[] {x4, y4, x3, y3};
98+
}
99+
return new double[] {x3, y3, x4, y4};
100+
} else {
101+
double x3 = Math.min(square1[0], square2[0]);
102+
double y3 = k * x3 + b;
103+
double x4 = Math.max(square1[0] + square1[2], square2[0] + square2[2]);
104+
double y4 = k * x4 + b;
105+
return new double[] {x3, y3, x4, y4};
106+
}
107+
}
108+
}
109+
```
110+
111+
### **C++**
112+
113+
```cpp
114+
class Solution {
115+
public:
116+
vector<double> cutSquares(vector<int>& square1, vector<int>& square2) {
117+
double x1 = square1[0] + square1[2] / 2.0;
118+
double y1 = square1[1] + square1[2] / 2.0;
119+
double x2 = square2[0] + square2[2] / 2.0;
120+
double y2 = square2[1] + square2[2] / 2.0;
121+
if (x1 == x2) {
122+
double y3 = min(square1[1], square2[1]);
123+
double y4 = max(square1[1] + square1[2], square2[1] + square2[2]);
124+
return {x1, y3, x2, y4};
125+
}
126+
double k = (y2 - y1) / (x2 - x1);
127+
double b = y1 - k * x1;
128+
if (abs(k) > 1) {
129+
double y3 = min(square1[1], square2[1]);
130+
double x3 = (y3 - b) / k;
131+
double y4 = max(square1[1] + square1[2], square2[1] + square2[2]);
132+
double x4 = (y4 - b) / k;
133+
if (x3 > x4 || (x3 == x4 && y3 > y4)) {
134+
return {x4, y4, x3, y3};
135+
}
136+
return {x3, y3, x4, y4};
137+
} else {
138+
double x3 = min(square1[0], square2[0]);
139+
double y3 = k * x3 + b;
140+
double x4 = max(square1[0] + square1[2], square2[0] + square2[2]);
141+
double y4 = k * x4 + b;
142+
return {x3, y3, x4, y4};
143+
}
144+
}
145+
};
146+
```
147+
148+
### **Go**
149+
150+
```go
151+
func cutSquares(square1 []int, square2 []int) []float64 {
152+
x1, y1 := float64(square1[0])+float64(square1[2])/2, float64(square1[1])+float64(square1[2])/2
153+
x2, y2 := float64(square2[0])+float64(square2[2])/2, float64(square2[1])+float64(square2[2])/2
154+
if x1 == x2 {
155+
y3 := math.Min(float64(square1[1]), float64(square2[1]))
156+
y4 := math.Max(float64(square1[1]+square1[2]), float64(square2[1]+square2[2]))
157+
return []float64{x1, y3, x2, y4}
158+
}
159+
k := (y2 - y1) / (x2 - x1)
160+
b := y1 - k*x1
161+
if math.Abs(k) > 1 {
162+
y3 := math.Min(float64(square1[1]), float64(square2[1]))
163+
x3 := (y3 - b) / k
164+
y4 := math.Max(float64(square1[1]+square1[2]), float64(square2[1]+square2[2]))
165+
x4 := (y4 - b) / k
166+
if x3 > x4 || (x3 == x4 && y3 > y4) {
167+
return []float64{x4, y4, x3, y3}
168+
}
169+
return []float64{x3, y3, x4, y4}
170+
} else {
171+
x3 := math.Min(float64(square1[0]), float64(square2[0]))
172+
y3 := k*x3 + b
173+
x4 := math.Max(float64(square1[0]+square1[2]), float64(square2[0]+square2[2]))
174+
y4 := k*x4 + b
175+
return []float64{x3, y3, x4, y4}
176+
}
177+
}
178+
```
43179

180+
### **TypeScript**
181+
182+
```ts
183+
function cutSquares(square1: number[], square2: number[]): number[] {
184+
const x1 = square1[0] + square1[2] / 2;
185+
const y1 = square1[1] + square1[2] / 2;
186+
const x2 = square2[0] + square2[2] / 2;
187+
const y2 = square2[1] + square2[2] / 2;
188+
if (x1 === x2) {
189+
const y3 = Math.min(square1[1], square2[1]);
190+
const y4 = Math.max(square1[1] + square1[2], square2[1] + square2[2]);
191+
return [x1, y3, x2, y4];
192+
}
193+
const k = (y2 - y1) / (x2 - x1);
194+
const b = y1 - k * x1;
195+
if (Math.abs(k) > 1) {
196+
const y3 = Math.min(square1[1], square2[1]);
197+
const x3 = (y3 - b) / k;
198+
const y4 = Math.max(square1[1] + square1[2], square2[1] + square2[2]);
199+
const x4 = (y4 - b) / k;
200+
if (x3 > x4 || (x3 === x4 && y3 > y4)) {
201+
return [x4, y4, x3, y3];
202+
}
203+
return [x3, y3, x4, y4];
204+
} else {
205+
const x3 = Math.min(square1[0], square2[0]);
206+
const y3 = k * x3 + b;
207+
const x4 = Math.max(square1[0] + square1[2], square2[0] + square2[2]);
208+
const y4 = k * x4 + b;
209+
return [x3, y3, x4, y4];
210+
}
211+
}
44212
```
45213

46214
### **...**

0 commit comments

Comments
 (0)
Please sign in to comment.