Skip to content

Commit a8350f8

Browse files
authored
update: 添加问题“3453.分割正方形I”的代码和题解 (#1324)
* feat: test master (#1318) * feat: test master (#1318) - sync action.yml 没忘记再修改下.md哈哈 * fix: test master (#1318) - jq read file error * fix: test master (#1318) - ... * fix: test master (#1318) - error jq * fix: lastModifiedTime (#1318) * feat * feat: modify time(#1318) * feat: modify time(#1318) * fix: modify time(#1318) - mo tmp json * fix: modify time(#1318) - tmp json path * fix: modify time(#1318) - tmp json indent+emptyLine * fix: modifiy time(#1318) * test: modify time json order (#1318) 刚刚是因为小写? * feat: checkout faster (#1318) - checkout needed branches * feat: checkout faster (#1318) No Solutions changes, skip deploy * feat: blog gen优化 (#791) * feat: update with master(dev) (#791) a1dbc2d * docs: 大型网站架构 * cp: sync with master(dev) * 3453: init (#1322) * 3453: WA.cpp (#1322) 1000000000.00000 * 3453: AC.cpp (#1322) - AC,50.66%,77.15% * word: en (#1322) * fix: article update time (#1318) - 这告诉我们没事儿别盲目升级,能不用动就不要动 - fake commit msg * update: 添加问题“3453.分割正方形I”的代码和题解 (#1324) Signed-off-by: LetMeFly666 <Tisfy@qq.com> --------- Signed-off-by: LetMeFly666 <Tisfy@qq.com>
1 parent 8f47772 commit a8350f8

7 files changed

Lines changed: 298 additions & 21 deletions

.github/workflows/deployHexo.yml

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -78,23 +78,36 @@ jobs:
7878
- name: Fetch and checkout each branch (shallow)
7979
if: env.SKIP_DEPLOY != 'true'
8080
run: |
81-
# TODO: Let's change this(only needed branch)
8281
# git clone git@github.com:LetMeFly666/LeetCode.git --depth=1
8382
# cd LeetCode
8483
# git rev-list --count HEAD
85-
git fetch --prune --depth=1 origin +refs/heads/*:refs/remotes/origin/*
86-
for branch in $(git branch -r | grep -v '\->'); do
87-
local_branch="${branch#origin/}"
88-
if [[ "$local_branch" =~ ^[0-9] ]] || [[ "$local_branch" == "From_GitCode_CSDN" ]]; then
89-
echo "Skipping branch $local_branch"
84+
branches=(
85+
website
86+
website_Static
87+
website_modifyTime
88+
)
89+
# 非 website_testMaster 才需要 master
90+
if [ "${GITHUB_REF}" != "refs/heads/website_testMaster" ]; then
91+
branches+=(master)
92+
else
93+
echo "Triggered by website_testMaster, skip fetching master"
94+
fi
95+
96+
echo "Branches to process: ${branches[*]}"
97+
98+
# 只 fetch 需要的分支
99+
for branch in "${branches[@]}"; do
100+
# 如果本地已存在,直接跳过
101+
if git show-ref --quiet "refs/heads/$branch"; then
102+
echo "Local branch $branch already exists, skipping fetch/checkout"
90103
continue
91104
fi
92-
if ! git show-ref --quiet "refs/heads/$local_branch"; then
93-
echo "Setting branch $local_branch"
94-
git switch -c $local_branch origin/$local_branch
95-
else
96-
echo "Branch $local_branch already exists, skipping..."
97-
fi
105+
106+
echo "Fetching branch $branch"
107+
git fetch --depth=1 origin "$branch"
108+
109+
echo "Checking out branch $branch"
110+
git switch -c "$branch" "origin/$branch"
98111
done
99112
100113
if [ "${GITHUB_REF}" = "refs/heads/website_testMaster" ]; then
@@ -185,11 +198,12 @@ jobs:
185198
echo "Commit time: $commit_time"
186199
187200
has_modified=false
188-
while read -r status file; do
201+
while IFS= read -r -d '' status && IFS= read -r -d '' file; do
189202
[[ "$file" == Solutions/*.md ]] || continue
190203
191204
filename=$(basename "$file")
192205
has_modified=true
206+
echo "($status)$filename"
193207
194208
case "$status" in
195209
A|M)
@@ -206,7 +220,7 @@ jobs:
206220
'del(.[$k])')
207221
;;
208222
esac
209-
done < <(git diff --name-status HEAD~1 HEAD)
223+
done < <(git diff --name-status -z HEAD~1 HEAD)
210224
211225
if [ "$has_modified" = false ]; then
212226
echo "no modified Solutions/*.md files"
@@ -236,7 +250,7 @@ jobs:
236250
file_name=$(echo "$line" | jq -r 'keys[0]')
237251
file_time=$(echo "$line" | jq -r '.[keys[0]]')
238252
touch -d "$file_time" "Solutions/$file_name"
239-
echo "set Solutions/$file_name -> $file_time"
253+
# echo "set Solutions/$file_name -> $file_time"
240254
done < <(jq -c 'to_entries | map({(.key): .value}) | .[]' "$json_file")
241255
242256
- name: Install hexo and init
@@ -271,8 +285,8 @@ jobs:
271285
272286
# sub theme: 明日方舟arknights
273287
cp -a ../hexoWorkspace ../hexoWorkspace_arknights
274-
ls -l ../hexoWorkspace/source/_posts
275-
ls -l ../hexoWorkspace_arknights/source/_posts
288+
# ls -l ../hexoWorkspace/source/_posts
289+
# ls -l ../hexoWorkspace_arknights/source/_posts
276290
277291
- name: Install dependencies
278292
if: env.SKIP_DEPLOY != 'true'
@@ -319,18 +333,16 @@ jobs:
319333
- name: Modify sitemap
320334
if: env.SKIP_DEPLOY != 'true'
321335
run: |
322-
# TODO: Let's change this
323-
# TODO: Let's change tag orders
324336
echo "" > ../hexoWorkspace/public/sitemap.txt # 主站优先
325337
sed 's/theme\/arknights\///g' ../hexoWorkspace/public/theme/arknights/sitemap.txt >> ../hexoWorkspace/public/sitemap.txt
326-
cat ../hexoWorkspace/public/theme/arknights/sitemap.txt >> ../hexoWorkspace/public/sitemap.txt
338+
# cat ../hexoWorkspace/public/theme/arknights/sitemap.txt >> ../hexoWorkspace/public/sitemap.txt
327339
# echo "https://blog.letmefly.xyz/theme/arknights/sitemap.txt" >> ../hexoWorkspace/public/sitemap.txt
328340
329341
- name: Push to github
330342
if: env.SKIP_DEPLOY != 'true'
331343
run: |
332344
git checkout website
333-
ls -la
345+
# ls -la
334346
cp -r .git ../hexoWorkspace/public
335347
cd ../hexoWorkspace/public
336348
# DECODED_MESSAGE=$(echo "$ENCODED_MESSAGE" | base64 --decode)

Codes/3453-separate-squares-i.cpp

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2026-01-13 21:41:10
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2026-01-13 22:21:20
6+
*/
7+
#if defined(_WIN32) || defined(__APPLE__)
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
class Solution {
12+
private:
13+
double half = 0;
14+
vector<vector<int>> squares;
15+
16+
bool check(double h) {
17+
double total = 0;
18+
for (vector<int>& s : squares) {
19+
double from = max(double(s[1]), h);
20+
double to = s[1] + s[2];
21+
total += max(0., (to - from) * s[2]);
22+
// printf("(%llf -> %llf), total = %llf\n", from, to, total);
23+
}
24+
25+
return total > half;
26+
}
27+
public:
28+
double separateSquares(vector<vector<int>>& squares) {
29+
long long total = 0; // !!!!!记得初始化
30+
for (vector<int>& s : squares) {
31+
total += ((long long) s[2]) * s[2];
32+
}
33+
this->squares = move(squares);
34+
half = 1. * total / 2;
35+
36+
double l = 0, r = 1000000000;
37+
for (int _ = 0; _ < 50; _++) {
38+
double mid = (l + r) / 2;
39+
if (check(mid)) {
40+
l = mid;
41+
} else {
42+
r = mid;
43+
}
44+
// printf("l = %llf, r = %llf, mid = %llf\n", l, r, mid);
45+
}
46+
return l;
47+
}
48+
};
49+
50+
/*
51+
>>> 10**9 / 2**46
52+
1.4210854715202004e-05
53+
>>> 10**9 / 2**47
54+
7.105427357601002e-06
55+
*/
56+
57+
#if defined(_WIN32) || defined(__APPLE__)
58+
/*
59+
[[0,0,1],[2,2,1]]
60+
61+
*/
62+
int main() {
63+
string s;
64+
while (cin >> s) {
65+
vector<vector<int>> a = stringToVectorVector(s);
66+
Solution sol;
67+
cout << sol.separateSquares(a) << endl;
68+
}
69+
return 0;
70+
}
71+
#endif

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1075,6 +1075,7 @@
10751075
|3433.统计用户被提及情况|中等|<a href="https://leetcode.cn/problems/count-mentions-per-user/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/12/12/LeetCode%203433.%E7%BB%9F%E8%AE%A1%E7%94%A8%E6%88%B7%E8%A2%AB%E6%8F%90%E5%8F%8A%E6%83%85%E5%86%B5/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/155861707" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/count-mentions-per-user/solutions/3855685/letmefly-3433tong-ji-yong-hu-bei-ti-ji-q-6b8p/" target="_blank">LeetCode题解</a>|
10761076
|3439.重新安排会议得到最多空余时间I|中等|<a href="https://leetcode.cn/problems/reschedule-meetings-for-maximum-free-time-i/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/07/13/LeetCode%203439.%E9%87%8D%E6%96%B0%E5%AE%89%E6%8E%92%E4%BC%9A%E8%AE%AE%E5%BE%97%E5%88%B0%E6%9C%80%E5%A4%9A%E7%A9%BA%E4%BD%99%E6%97%B6%E9%97%B4I/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/149317430" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/reschedule-meetings-for-maximum-free-time-i/solutions/3722937/letmefly-3439zhong-xin-an-pai-hui-yi-de-r3t9e/" target="_blank">LeetCode题解</a>|
10771077
|3442.奇偶频次间的最大差值I|简单|<a href="https://leetcode.cn/problems/maximum-difference-between-even-and-odd-frequency-i/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/06/10/LeetCode%203442.%E5%A5%87%E5%81%B6%E9%A2%91%E6%AC%A1%E9%97%B4%E7%9A%84%E6%9C%80%E5%A4%A7%E5%B7%AE%E5%80%BCI/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/148570777" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/maximum-difference-between-even-and-odd-frequency-i/solutions/3697605/letmefly-3442qi-ou-pin-ci-jian-de-zui-da-nwwx/" target="_blank">LeetCode题解</a>|
1078+
|3453.分割正方形I|中等|<a href="https://leetcode.cn/problems/separate-squares-i/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2026/01/13/LeetCode%203453.%E5%88%86%E5%89%B2%E6%AD%A3%E6%96%B9%E5%BD%A2I/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/156915633" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/separate-squares-i/solutions/3880005/letmefly-3453fen-ge-zheng-fang-xing-ier-vem4p/" target="_blank">LeetCode题解</a>|
10781079
|3459.最长V形对角线段的长度|困难|<a href="https://leetcode.cn/problems/length-of-longest-v-shaped-diagonal-segment/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/08/31/LeetCode%203459.%E6%9C%80%E9%95%BFV%E5%BD%A2%E5%AF%B9%E8%A7%92%E7%BA%BF%E6%AE%B5%E7%9A%84%E9%95%BF%E5%BA%A6/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/151050078" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/length-of-longest-v-shaped-diagonal-segment/solutions/3768545/letmefly-3459zui-chang-v-xing-dui-jiao-x-gu8q/" target="_blank">LeetCode题解</a>|
10791080
|3461.判断操作后字符串中的数字是否相等I|简单|<a href="https://leetcode.cn/problems/check-if-digits-are-equal-in-string-after-operations-i/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/10/23/LeetCode%203461.%E5%88%A4%E6%96%AD%E6%93%8D%E4%BD%9C%E5%90%8E%E5%AD%97%E7%AC%A6%E4%B8%B2%E4%B8%AD%E7%9A%84%E6%95%B0%E5%AD%97%E6%98%AF%E5%90%A6%E7%9B%B8%E7%AD%89I/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/153800840" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/check-if-digits-are-equal-in-string-after-operations-i/solutions/3814120/letmefly-3461pan-duan-cao-zuo-hou-zi-fu-8nd1t/" target="_blank">LeetCode题解</a>|
10801081
|3494.酿造药水需要的最少总时间|中等|<a href="https://leetcode.cn/problems/find-the-minimum-amount-of-time-to-brew-potions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/10/09/LeetCode%203494.%E9%85%BF%E9%80%A0%E8%8D%AF%E6%B0%B4%E9%9C%80%E8%A6%81%E7%9A%84%E6%9C%80%E5%B0%91%E6%80%BB%E6%97%B6%E9%97%B4/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/152847907" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/find-the-minimum-amount-of-time-to-brew-potions/solutions/3801656/letmefly-3494niang-zao-yao-shui-xu-yao-d-bjxv/" target="_blank">LeetCode题解</a>|
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
---
2+
title: 3453.分割正方形 I:二分查找
3+
date: 2026-01-13 23:46:15
4+
tags: [题解, LeetCode, 中等, 数组, 二分查找, 二分]
5+
categories: [题解, LeetCode]
6+
index_img: https://pic.leetcode.cn/1739609465-UaFzhk-4062example1drawio.png
7+
---
8+
9+
# 【LetMeFly】3453.分割正方形 I:二分查找
10+
11+
力扣题目链接:[https://leetcode.cn/problems/separate-squares-i/](https://leetcode.cn/problems/separate-squares-i/)
12+
13+
<p>给你一个二维整数数组 <code>squares</code>&nbsp;,其中&nbsp;<code>squares[i] = [x<sub>i</sub>, y<sub>i</sub>, l<sub>i</sub>]</code> 表示一个与 x 轴平行的正方形的左下角坐标和正方形的边长。</p>
14+
15+
<p>找到一个<strong>最小的</strong> y 坐标,它对应一条水平线,该线需要满足它以上正方形的总面积 <strong>等于</strong> 该线以下正方形的总面积。</p>
16+
17+
<p>答案如果与实际答案的误差在 <code>10<sup>-5</sup></code> 以内,将视为正确答案。</p>
18+
19+
<p><strong>注意</strong>:正方形&nbsp;<strong>可能会&nbsp;</strong>重叠。重叠区域应该被&nbsp;<strong>多次计数&nbsp;</strong>。</p>
20+
21+
<p>&nbsp;</p>
22+
23+
<p><strong class="example">示例 1:</strong></p>
24+
25+
<div class="example-block">
26+
<p><strong>输入:</strong> <span class="example-io">squares = [[0,0,1],[2,2,1]]</span></p>
27+
28+
<p><strong>输出:</strong> <span class="example-io">1.00000</span></p>
29+
30+
<p><strong>解释:</strong></p>
31+
32+
<p><img alt="" src="https://pic.leetcode.cn/1739609465-UaFzhk-4062example1drawio.png" style="width: 378px; height: 352px;" /></p>
33+
34+
<p>任何在 <code>y = 1</code> 和 <code>y = 2</code> 之间的水平线都会有 1 平方单位的面积在其上方,1 平方单位的面积在其下方。最小的 y 坐标是 1。</p>
35+
</div>
36+
37+
<p><strong class="example">示例 2:</strong></p>
38+
39+
<div class="example-block">
40+
<p><strong>输入:</strong> <span class="example-io">squares = [[0,0,2],[1,1,1]]</span></p>
41+
42+
<p><strong>输出:</strong> <span class="example-io">1.16667</span></p>
43+
44+
<p><strong>解释:</strong></p>
45+
46+
<p><img alt="" src="https://pic.leetcode.cn/1739609527-TWqefZ-4062example2drawio.png" style="width: 378px; height: 352px;" /></p>
47+
48+
<p>面积如下:</p>
49+
50+
<ul>
51+
<li>线下的面积:<code>7/6 * 2 (红色) + 1/6 (蓝色) = 15/6 = 2.5</code>。</li>
52+
<li>线上的面积:<code>5/6 * 2 (红色) + 5/6 (蓝色) = 15/6 = 2.5</code>。</li>
53+
</ul>
54+
55+
<p>由于线以上和线以下的面积相等,输出为 <code>7/6 = 1.16667</code>。</p>
56+
</div>
57+
58+
<p>&nbsp;</p>
59+
60+
<p><strong>提示:</strong></p>
61+
62+
<ul>
63+
<li><code>1 &lt;= squares.length &lt;= 5 * 10<sup>4</sup></code></li>
64+
<li><code>squares[i] = [x<sub>i</sub>, y<sub>i</sub>, l<sub>i</sub>]</code></li>
65+
<li><code>squares[i].length == 3</code></li>
66+
<li><code>0 &lt;= x<sub>i</sub>, y<sub>i</sub> &lt;= 10<sup>9</sup></code></li>
67+
<li><code>1 &lt;= l<sub>i</sub> &lt;= 10<sup>9</sup></code></li>
68+
<li>所有正方形的总面积不超过 <code>10<sup>12</sup></code>。</li>
69+
</ul>
70+
71+
72+
73+
## 解题方法:二分查找
74+
75+
先算下所有正方形的总面积,然后二分分割线高度,太低就高点太高就低点。
76+
77+
终止条件:两次计算结果分割线移动返回不超过$10^{-5}$或直接进行$50$次求值。
78+
79+
```python
80+
>>> 10**9 / 2**46
81+
1.4210854715202004e-05
82+
>>> 10**9 / 2**47
83+
7.105427357601002e-06
84+
```
85+
86+
+ 时间复杂度$O(C\times len(squares))$,其中$C=50$或$C=\log_2{max(squires[i][1])-min(squires[i][1])}$
87+
+ 空间复杂度$O(1)$
88+
89+
### AC代码
90+
91+
#### C++
92+
93+
```cpp
94+
/*
95+
* @LastEditTime: 2026-01-13 22:21:20
96+
*/
97+
class Solution {
98+
private:
99+
double half = 0;
100+
vector<vector<int>> squares;
101+
102+
bool check(double h) {
103+
double total = 0;
104+
for (vector<int>& s : squares) {
105+
double from = max(double(s[1]), h);
106+
double to = s[1] + s[2];
107+
total += max(0., (to - from) * s[2]);
108+
}
109+
110+
return total > half;
111+
}
112+
public:
113+
double separateSquares(vector<vector<int>>& squares) {
114+
long long total = 0; // !!!!!记得初始化
115+
for (vector<int>& s : squares) {
116+
total += ((long long) s[2]) * s[2];
117+
}
118+
this->squares = move(squares);
119+
half = 1. * total / 2;
120+
121+
double l = 0, r = 1000000000;
122+
for (int _ = 0; _ < 50; _++) {
123+
double mid = (l + r) / 2;
124+
if (check(mid)) {
125+
l = mid;
126+
} else {
127+
r = mid;
128+
}
129+
}
130+
return l;
131+
}
132+
};
133+
```
134+
135+
> 同步发文于[CSDN](https://letmefly.blog.csdn.net/article/details/156915633)和我的[个人博客](https://blog.letmefly.xyz/),原创不易,转载经作者同意后请附上[原文链接](https://blog.letmefly.xyz/2026/01/13/LeetCode%203453.%E5%88%86%E5%89%B2%E6%AD%A3%E6%96%B9%E5%BD%A2I/)哦~
136+
>
137+
> 千篇源码题解[已开源](https://github.com/LetMeFly666/LeetCode)

Solutions/Other-Accumulation-SomeTips.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1085,6 +1085,11 @@ certbot renew
10851085
certbot renew --force-renewal
10861086
```
10871087

1088+
### 读《大型网站技术架构——核心原理与案例分析(李智慧)》有感
1089+
1090+
* 网站的价值在于它能为用户提供什么价值,在于网站能做什么,而不在于它是怎么做的。所以**在网站还很小的时候就去追求网站的架构师舍本逐末,得不偿失的**
1091+
* 在业务问题还没有理清楚的时候就从外面挖来许多技术高手,仿照成功的互联网平台打造技术平台,这无疑是南辕北辙,缘木求鱼。而这些技术高手离开了他们熟悉的环境和工作模式,也是张飞拿着绣花针使不上劲来。
1092+
10881093
## About API
10891094

10901095
### bilibili API

Solutions/Other-English-LearningNotes-SomeWords.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1735,6 +1735,9 @@ categories: [自用]
17351735
|tyrant|n. 暴君|
17361736
|||
17371737
|exoneration|n. 免罪,免除|
1738+
|||
1739+
|Oceania|n. 大洋洲|
1740+
|phonetics|n. 语音学|
17381741

17391742
+ 这个web要是能设计得可以闭眼(完全不睁眼)键盘控制背单词就好了。
17401743
+ 也许可以加个AI用最近词编故事功能(返回接口中支持标注所使用单词高亮?)

0 commit comments

Comments
 (0)