Skip to content

Commit 9391811

Browse files
update: 添加问题“3740.三个相等元素之间的最小距离I”的代码和题解 (#1491)
* archive: modifies yesterday * clean: rename Signed-off-by: LetMeFly666 <Tisfy@qq.com> * word: en 2025.3.24 * archive: before.sleep * 3740: init.cpp (#1489) * 3740: AC.cpp (#1489) - AC,58.02%,93.83% * 3740: CE.py (#1489) * 3740: AC.py (#1489) - AC,12.14%,39.81% * update: 添加问题“3740.三个相等元素之间的最小距离I”的代码和题解 (#1491) Signed-off-by: LetMeFly666 <Tisfy@qq.com> * typo: 答案最小值而非最大值 Update Solutions/LeetCode 3740.三个相等元素之间的最小距离I.md Co-authored-by: gh-pr-review[bot] <268385713+gh-pr-review[bot]@users.noreply.github.com> * docs: word(en) --------- Signed-off-by: LetMeFly666 <Tisfy@qq.com> Co-authored-by: gh-pr-review[bot] <268385713+gh-pr-review[bot]@users.noreply.github.com>
1 parent ab80193 commit 9391811

8 files changed

Lines changed: 224 additions & 3 deletions

.commitmsg

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2026-04-10 23:22:00
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2026-04-10 23:25:26
6+
*/
7+
#ifdef _DEBUG
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
class Solution {
12+
public:
13+
int minimumDistance(vector<int>& nums) {
14+
int ans = 201;
15+
for (int i = 0, n = nums.size(); i < n; i++) {
16+
for (int j = i + 1; j < n; j++) {
17+
if (nums[j] != nums[i]) {
18+
continue;
19+
}
20+
for (int k = j + 1; k < n; k++) {
21+
if (nums[k] == nums[i]) {
22+
ans = min(ans, 2 * (k - i));
23+
}
24+
}
25+
}
26+
}
27+
return ans == 201 ? -1 : ans;
28+
}
29+
};
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
'''
2+
Author: LetMeFly
3+
Date: 2026-04-10 23:22:00
4+
LastEditors: LetMeFly.xyz
5+
LastEditTime: 2026-04-11 10:31:48
6+
'''
7+
from typing import List
8+
9+
class Solution:
10+
def minimumDistance(self, nums: List[int]) -> int:
11+
ans = 201
12+
for i, a in enumerate(nums):
13+
for j in range(i + 1, len(nums)):
14+
if a != nums[j]:
15+
continue
16+
for k in range(j + 1, len(nums)):
17+
if a == nums[k]:
18+
ans = min(ans, 2 * (k - i))
19+
return -1 if ans == 201 else ans

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1149,6 +1149,7 @@
11491149
|3713.最长的平衡子串I|中等|<a href="https://leetcode.cn/problems/longest-balanced-substring-i/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2026/02/12/LeetCode%203713.%E6%9C%80%E9%95%BF%E7%9A%84%E5%B9%B3%E8%A1%A1%E5%AD%90%E4%B8%B2I/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/158013353" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/longest-balanced-substring-i/solutions/3901766/letmefly-3713zui-chang-de-ping-heng-zi-c-idk1/" target="_blank">LeetCode题解</a>|
11501150
|3714.最长的平衡子串II|中等|<a href="https://leetcode.cn/problems/longest-balanced-substring-ii/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2026/02/15/LeetCode%203714.%E6%9C%80%E9%95%BF%E7%9A%84%E5%B9%B3%E8%A1%A1%E5%AD%90%E4%B8%B2II/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/158100379" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/longest-balanced-substring-ii/solutions/3903202/letmefly-3714zui-chang-de-ping-heng-zi-c-qngc/" target="_blank">LeetCode题解</a>|
11511151
|3719.最长平衡子数组I|中等|<a href="https://leetcode.cn/problems/longest-balanced-subarray-i/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2026/02/10/LeetCode%203719.%E6%9C%80%E9%95%BF%E5%B9%B3%E8%A1%A1%E5%AD%90%E6%95%B0%E7%BB%84I/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/157947059" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/longest-balanced-subarray-i/solutions/3900598/letmefly-3719zui-chang-ping-heng-zi-shu-z6a81/" target="_blank">LeetCode题解</a>|
1152+
|3740.三个相等元素之间的最小距离I|简单|<a href="https://leetcode.cn/problems/minimum-distance-between-three-equal-elements-i/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2026/04/10/LeetCode%203740.%E4%B8%89%E4%B8%AA%E7%9B%B8%E7%AD%89%E5%85%83%E7%B4%A0%E4%B9%8B%E9%97%B4%E7%9A%84%E6%9C%80%E5%B0%8F%E8%B7%9D%E7%A6%BBI/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/160058233" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/minimum-distance-between-three-equal-elements-i/solutions/3949609/letmefly-3740san-ge-xiang-deng-yuan-su-z-8ir2/" target="_blank">LeetCode题解</a>|
11521153
|剑指Offer0047.礼物的最大价值|简单|<a href="https://leetcode.cn/problems/li-wu-de-zui-da-jie-zhi-lcof/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/03/08/LeetCode%20%E5%89%91%E6%8C%87%20Offer%2047.%20%E7%A4%BC%E7%89%A9%E7%9A%84%E6%9C%80%E5%A4%A7%E4%BB%B7%E5%80%BC/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/129408765" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/li-wu-de-zui-da-jie-zhi-lcof/solutions/2155672/letmefly-jian-zhi-offer-47li-wu-de-zui-d-rekb/" target="_blank">LeetCode题解</a>|
11531154
|剑指OfferII0041.滑动窗口的平均值|简单|<a href="https://leetcode.cn/problems/qIsx9U/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/07/16/LeetCode%20%E5%89%91%E6%8C%87%20Offer%20II%200041.%20%E6%BB%91%E5%8A%A8%E7%AA%97%E5%8F%A3%E7%9A%84%E5%B9%B3%E5%9D%87%E5%80%BC/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/125819216" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/qIsx9U/solution/by-tisfy-30mq/" target="_blank">LeetCode题解</a>|
11541155
|剑指OfferII0091.粉刷房子|中等|<a href="https://leetcode.cn/problems/JEj789/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/06/25/LeetCode%20%E5%89%91%E6%8C%87%20Offer%20II%200091.%20%E7%B2%89%E5%88%B7%E6%88%BF%E5%AD%90/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/125456885" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/JEj789/solution/letmefly-jian-zhi-offer-ii-091fen-shua-f-3olz/" target="_blank">LeetCode题解</a>|
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
---
2+
title: 3740.三个相等元素之间的最小距离 I:今日先暴力,"明日"再哈希
3+
date: 2026-04-11 18:43:10
4+
tags: [题解, LeetCode, 简单, 数组, 暴力, 模拟, 遍历]
5+
categories: [题解, LeetCode]
6+
---
7+
8+
# 【LetMeFly】3740.三个相等元素之间的最小距离 I:今日先暴力,"明日"再哈希
9+
10+
力扣题目链接:[https://leetcode.cn/problems/minimum-distance-between-three-equal-elements-i/](https://leetcode.cn/problems/minimum-distance-between-three-equal-elements-i/)
11+
12+
<p>给你一个整数数组 <code>nums</code>。</p>
13+
14+
<p>如果满足 <code>nums[i] == nums[j] == nums[k]</code>,且 <code>(i, j, k)</code> 是 3 个&nbsp;<strong>不同&nbsp;</strong>下标,那么三元组 <code>(i, j, k)</code> 被称为&nbsp;<strong>有效三元组&nbsp;</strong>。</p>
15+
16+
<p><strong>有效三元组&nbsp;</strong>的&nbsp;<strong>距离&nbsp;</strong>被定义为 <code>abs(i - j) + abs(j - k) + abs(k - i)</code>,其中 <code>abs(x)</code> 表示 <code>x</code> 的&nbsp;<strong>绝对值&nbsp;</strong>。</p>
17+
18+
<p>返回一个整数,表示 <strong>有效三元组&nbsp;</strong>的&nbsp;<strong>最小&nbsp;</strong>可能距离。如果不存在&nbsp;<strong>有效三元组&nbsp;</strong>,返回 <code>-1</code>。</p>
19+
20+
<p>&nbsp;</p>
21+
22+
<p><strong class="example">示例 1:</strong></p>
23+
24+
<div class="example-block">
25+
<p><strong>输入:</strong> <span class="example-io">nums = [1,2,1,1,3]</span></p>
26+
27+
<p><strong>输出:</strong> <span class="example-io">6</span></p>
28+
29+
<p><strong>解释:</strong></p>
30+
31+
<p>最小距离对应的有效三元组是&nbsp;<code>(0, 2, 3)</code>&nbsp;。</p>
32+
33+
<p><code>(0, 2, 3)</code> 是一个有效三元组,因为 <code>nums[0] == nums[2] == nums[3] == 1</code>。它的距离为 <code>abs(0 - 2) + abs(2 - 3) + abs(3 - 0) = 2 + 1 + 3 = 6</code>。</p>
34+
</div>
35+
36+
<p><strong class="example">示例 2:</strong></p>
37+
38+
<div class="example-block">
39+
<p><strong>输入:</strong> <span class="example-io">nums = [1,1,2,3,2,1,2]</span></p>
40+
41+
<p><strong>输出:</strong> <span class="example-io">8</span></p>
42+
43+
<p><strong>解释:</strong></p>
44+
45+
<p>最小距离对应的有效三元组是&nbsp;<code>(2, 4, 6)</code>&nbsp;。</p>
46+
47+
<p><code>(2, 4, 6)</code> 是一个有效三元组,因为 <code>nums[2] == nums[4] == nums[6] == 2</code>。它的距离为 <code>abs(2 - 4) + abs(4 - 6) + abs(6 - 2) = 2 + 2 + 4 = 8</code>。</p>
48+
</div>
49+
50+
<p><strong class="example">示例 3:</strong></p>
51+
52+
<div class="example-block">
53+
<p><strong>输入:</strong> <span class="example-io">nums = [1]</span></p>
54+
55+
<p><strong>输出:</strong> <span class="example-io">-1</span></p>
56+
57+
<p><strong>解释:</strong></p>
58+
59+
<p>不存在有效三元组,因此答案为 -1。</p>
60+
</div>
61+
62+
<p>&nbsp;</p>
63+
64+
<p><strong>提示:</strong></p>
65+
66+
<ul>
67+
<li><code>1 &lt;= n == nums.length &lt;= 100</code></li>
68+
<li><code>1 &lt;= nums[i] &lt;= n</code></li>
69+
</ul>
70+
71+
72+
73+
## 解题方法:暴力模拟
74+
75+
三层遍历,第一层使用$i$从$0$遍历到$n-1$,第二层使用$j$从$i+1$到$n-1$遍历,第三层使用$k$从$j+1$到$n-1$遍历。如果$nums[i]$、$nums[j]$、$nums[k]$都相等,则更新答案最小值。
76+
77+
Tips:由于遍历过程中$i$小于$j$小于$k$,所以有`abs(i - j) + abs(j - k) + abs(k - i) = 2 * (k - i)`
78+
79+
+ 时间复杂度$O(n^3)$,其中$n=len(nums)$
80+
+ 空间复杂度$O(1)$
81+
82+
### AC代码
83+
84+
#### C++
85+
86+
```cpp
87+
/*
88+
* @LastEditTime: 2026-04-10 23:25:26
89+
*/
90+
class Solution {
91+
public:
92+
int minimumDistance(vector<int>& nums) {
93+
int ans = 201;
94+
for (int i = 0, n = nums.size(); i < n; i++) {
95+
for (int j = i + 1; j < n; j++) {
96+
if (nums[j] != nums[i]) {
97+
continue;
98+
}
99+
for (int k = j + 1; k < n; k++) {
100+
if (nums[k] == nums[i]) {
101+
ans = min(ans, 2 * (k - i));
102+
}
103+
}
104+
}
105+
}
106+
return ans == 201 ? -1 : ans;
107+
}
108+
};
109+
110+
```
111+
112+
#### Python
113+
114+
```python
115+
'''
116+
LastEditTime: 2026-04-11 10:31:48
117+
'''
118+
from typing import List
119+
120+
class Solution:
121+
def minimumDistance(self, nums: List[int]) -> int:
122+
ans = 201
123+
for i, a in enumerate(nums):
124+
for j in range(i + 1, len(nums)):
125+
if a != nums[j]:
126+
continue
127+
for k in range(j + 1, len(nums)):
128+
if a == nums[k]:
129+
ans = min(ans, 2 * (k - i))
130+
return -1 if ans == 201 else ans
131+
132+
```
133+
134+
> 今天要是就使用哈希表了,明天就没得写了[Doge]。
135+
136+
> 同步发文于[CSDN](https://letmefly.blog.csdn.net/article/details/160058233)和我的[个人博客](https://blog.letmefly.xyz/),原创不易,转载经作者同意后请附上[原文链接](https://blog.letmefly.xyz/2026/04/10/LeetCode%203740.%E4%B8%89%E4%B8%AA%E7%9B%B8%E7%AD%89%E5%85%83%E7%B4%A0%E4%B9%8B%E9%97%B4%E7%9A%84%E6%9C%80%E5%B0%8F%E8%B7%9D%E7%A6%BBI/)哦~
137+
>
138+
> 千篇源码题解[已开源](https://github.com/LetMeFly666/LeetCode)

Solutions/Other-English-LearningNotes-SomeWords.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -955,7 +955,7 @@ categories: [自用]
955955
|||
956956
|disparity|n. 差异|
957957
|||
958-
|scorching|adj. 酷热的,猛烈的,激烈的,有力的<br/>adv. 〈口〉灼热地,热得灼人<br/>v. scorch的现在分词|
958+
|<font color="#28bea0" title="二次复习">scorching</font>|adj. 酷热的,猛烈的,激烈的,有力的<br/>adv. 〈口〉灼热地,热得灼人<br/>v. scorch的现在分词|
959959
|wagon|n. (铁路)货车车厢,四轮在中马车(/牛车)<br/>v. 用 运货马车/货车/手推车 运送【较少用作动词】|
960960
|||
961961
|bazaar|n. 集市,义卖会|
@@ -1884,6 +1884,8 @@ categories: [自用]
18841884
|crouch|v. 蹲,蜷缩<br/>n. 蹲|
18851885
|||
18861886
|triplicate|n. 一式三份<br/>adj. 一式三份的<br/>v. 使成三倍,分成三份|
1887+
|||
1888+
|possessive|adj. 占有欲强的,不愿分享的<br/>n. 所有格|
18871889

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

scale.vbs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'
2+
' @Author: LetMeFly
3+
' @Date: 2026-04-07 10:04:17
4+
' @LastEditors: LetMeFly.xyz
5+
' @LastEditTime: 2026-04-09 21:49:22
6+
'
7+
Sub ScaleShapesOnSlide()
8+
Dim s As Shape
9+
Dim ratio As Double
10+
Dim slideIndex As Long
11+
12+
ratio = 1.5 '放大倍数,按需改
13+
slideIndex = 1 '要操作的幻灯片页码
14+
15+
For Each s In ActivePresentation.Slides(slideIndex).Shapes
16+
'等比缩放位置和尺寸
17+
s.Left = s.Left * ratio
18+
s.Top = s.Top * ratio
19+
s.Width = s.Width * ratio
20+
s.Height = s.Height * ratio
21+
Next
22+
End Sub

todo

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
1+
# 我希望实现以下功能,具体怎样实现比较好:
2+
# 我有一台iphone设备,我喜欢使用其中的reminders功能。我有一台服务器,有logs数据库。
3+
# 我有自建的nextcloud tasks服务,支持caldav协议,iphone已经同步。
4+
# 我想通过创建一个tag或列表的方式,在桌面添加一个小组件,显示今天复习的单词
5+
# 所以我希望有个一便捷的方法,可以让我记录和自动完成每日单词。具体来说
6+
# - 当我背单词遇到一个生词,我可以快速记录(使用手机记录太麻烦了,最好可以通过电脑记录)
7+
# - 旧的一天单词删除的条件是:新的一天有新的单词记录。
8+
# - 添加单词时候不要在reminder中直接记录时间(可以记录到备注等但是不要真的设置时间,不然我reminder中通过时间筛选todo就会很慢),删除单词的时候设置单词截止时间是添加时间再“完成”
9+
10+
# record ppt字体
11+
112
# newSolution.py go自动4space2tab
213
# 源码转md时自动tab24space
3-
# remove last empty line
14+
# auto remove last empty line
415

516
# 记录iphone快捷指令 自动化 当到达某位置时触发
617

0 commit comments

Comments
 (0)