Skip to content

Commit 9e41c94

Browse files
committed
Merge branch 'master' of github.com:LetMeFly666/LeetCode
2 parents 39f5d1d + 2044274 commit 9e41c94

13 files changed

+826
-6
lines changed
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2024-11-20 12:11:48
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2024-11-20 12:56:12
6+
*/
7+
#ifdef _WIN32
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
/*
12+
每个点记录“自己跃迁到的点”
13+
初始值每个点能跃迁到的点都是自己的下一个节点
14+
15+
新来一条“跃迁通道”有两种可能:
16+
+ 被一条更长(或等长)的跃迁通道覆盖
17+
+ 覆盖n条跃迁通道
18+
反正不可能和其他跃迁通道有交叉
19+
两种情况的判断方式是“跃迁起点”指向的“能跃迁到的点”是否大于(等于)自己的“跃迁终点”
20+
+ 对于第一种情况:直接continue
21+
+ 对于第二种情况:修改所有“最大被覆盖子跃迁通道”的起点的“能跃迁到的点”
22+
*/
23+
#ifdef TirstTry // WA
24+
class Solution {
25+
public:
26+
vector<int> shortestDistanceAfterQueries(int n, vector<vector<int>>& queries) {
27+
vector<int> transitionTo(n);
28+
for (int i = 0; i < n; i++) {
29+
transitionTo[i] = i + 1;
30+
}
31+
int transitionToEndTimes = n - 1;
32+
vector<int> ans(queries.size());
33+
for (int i = 0; i < queries.size(); i++) {
34+
int from = queries[i][0], to = queries[i][1];
35+
while (transitionTo[from] < to) {
36+
int originalTo = transitionTo[from];
37+
transitionToEndTimes -= originalTo - from;
38+
transitionTo[from] = to;
39+
from = originalTo;
40+
}
41+
ans[i] = transitionToEndTimes;
42+
}
43+
return ans;
44+
}
45+
};
46+
#else // TirstTry
47+
#ifdef SecondTry // WA
48+
class Solution {
49+
public:
50+
vector<int> shortestDistanceAfterQueries(int n, vector<vector<int>>& queries) {
51+
vector<int> transitionTo(n);
52+
for (int i = 0; i < n; i++) {
53+
transitionTo[i] = i + 1;
54+
}
55+
int transitionToEndTimes = n - 1;
56+
vector<int> ans(queries.size());
57+
for (int i = 0; i < queries.size(); i++) {
58+
int from = queries[i][0], to = queries[i][1];
59+
int originalJumpTimes = 0;
60+
while (transitionTo[from] <= to) {
61+
originalJumpTimes++;
62+
int originalTo = transitionTo[from];
63+
transitionTo[from] = to;
64+
from = originalTo;
65+
}
66+
transitionToEndTimes -= originalJumpTimes - 1;
67+
ans[i] = transitionToEndTimes;
68+
}
69+
return ans;
70+
}
71+
};
72+
#else // SecondTry
73+
#ifdef ThirdTry // AC
74+
class Solution {
75+
public:
76+
vector<int> shortestDistanceAfterQueries(int n, vector<vector<int>>& queries) {
77+
vector<int> transitionTo(n);
78+
for (int i = 0; i < n; i++) {
79+
transitionTo[i] = i + 1;
80+
}
81+
int transitionToEndTimes = n - 1;
82+
vector<int> ans(queries.size());
83+
for (int i = 0; i < queries.size(); i++) {
84+
int from = queries[i][0], to = queries[i][1];
85+
int originalJumpTimes = 0;
86+
while (transitionTo[from] <= to) {
87+
originalJumpTimes++;
88+
int originalTo = transitionTo[from];
89+
transitionTo[from] = to;
90+
from = originalTo;
91+
}
92+
transitionToEndTimes -= max(0, originalJumpTimes - 1);
93+
ans[i] = transitionToEndTimes;
94+
}
95+
return ans;
96+
}
97+
};
98+
#else // ThirdTry
99+
// FourthTry // 简化版 // 执行用时分布:2ms,击败98.51%;消耗内存分布:108.84MB,击败83.86%.
100+
class Solution {
101+
public:
102+
vector<int> shortestDistanceAfterQueries(int n, vector<vector<int>>& queries) {
103+
vector<int> transitionTo(n), ans(queries.size());
104+
for (int i = 0; i < n; i++) {
105+
transitionTo[i] = i + 1;
106+
}
107+
int transitionToEndTimes = n - 1;
108+
for (int i = 0; i < queries.size(); i++) {
109+
int from = queries[i][0], to = queries[i][1], now = from;
110+
while (transitionTo[now] < to) {
111+
transitionToEndTimes--;
112+
int originalTo = transitionTo[now];
113+
transitionTo[now] = to;
114+
now = originalTo;
115+
}
116+
ans[i] = transitionToEndTimes;
117+
}
118+
return ans;
119+
}
120+
};
121+
#endif // ThirdTry
122+
#endif // SecondTry
123+
#endif // TirstTry
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2024-11-20 13:03:17
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2024-11-20 13:06:36
6+
*/
7+
package main
8+
9+
func shortestDistanceAfterQueries(n int, queries [][]int) (ans []int) {
10+
transitionTo := make([]int, n)
11+
for i := range transitionTo {
12+
transitionTo[i] = i + 1
13+
}
14+
minTimes := n - 1
15+
for _, query := range queries {
16+
from, to := query[0], query[1]
17+
for transitionTo[from] < to {
18+
minTimes--
19+
transitionTo[from], from = to, transitionTo[from]
20+
}
21+
ans = append(ans, minTimes)
22+
}
23+
return
24+
} // AC,81.82%,29.41%
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2024-11-20 12:58:48
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2024-11-20 13:02:59
6+
*/
7+
class Solution {
8+
public int[] shortestDistanceAfterQueries(int n, int[][] queries) {
9+
int[] transitionTo = new int[n];
10+
for (int i = 0; i < n; i++) {
11+
transitionTo[i] = i + 1;
12+
}
13+
int[] ans = new int[queries.length];
14+
int minTimes = n - 1;
15+
for (int i = 0; i < queries.length; i++) {
16+
int from = queries[i][0], to = queries[i][1];
17+
while (transitionTo[from] < to) {
18+
minTimes--;
19+
int originalTo = transitionTo[from];
20+
transitionTo[from] = to;
21+
from = originalTo;
22+
}
23+
ans[i] = minTimes;
24+
}
25+
return ans;
26+
}
27+
} // AC,100.00%,79.09%
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
'''
2+
Author: LetMeFly
3+
Date: 2024-11-20 12:46:44
4+
LastEditors: LetMeFly.xyz
5+
LastEditTime: 2024-11-20 12:54:50
6+
'''
7+
from typing import List
8+
9+
class Solution:
10+
def shortestDistanceAfterQueries(self, n: int, queries: List[List[int]]) -> List[int]:
11+
transitionTo = [i + 1 for i in range(n)]
12+
ans = []
13+
shortestTimes = n - 1
14+
for from_, to in queries:
15+
while transitionTo[from_] < to:
16+
shortestTimes -= 1
17+
transitionTo[from_], from_ = to, transitionTo[from_]
18+
ans.append(shortestTimes)
19+
return ans

Codes/3248-snake-in-matrix.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2024-11-21 22:59:40
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2024-11-21 23:02:59
6+
*/
7+
#ifdef _WIN32
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
// 开始看题
12+
// 开始解题
13+
class Solution {
14+
public:
15+
int finalPositionOfSnake(int n, vector<string>& commands) {
16+
int ans = 0;
17+
for (string& command : commands) {
18+
switch (command[0])
19+
{
20+
case 'U':
21+
ans -= n;
22+
break;
23+
case 'D':
24+
ans += n;
25+
break;
26+
case 'L':
27+
ans--;
28+
break;
29+
default: // 'R'
30+
ans++;
31+
break;
32+
}
33+
}
34+
return ans;
35+
}
36+
};

Codes/3248-snake-in-matrix.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2024-11-21 23:07:12
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2024-11-21 23:08:30
6+
*/
7+
package main
8+
9+
func finalPositionOfSnake(n int, commands []string) (ans int) {
10+
for _, c := range commands {
11+
switch c[0] {
12+
case 'U':
13+
ans -= n;
14+
case 'D':
15+
ans += n;
16+
case 'L':
17+
ans--;
18+
case 'R':
19+
ans++;
20+
}
21+
}
22+
return
23+
}

Codes/3248-snake-in-matrix.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2024-11-21 23:05:25
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2024-11-21 23:06:44
6+
*/
7+
class Solution {
8+
public int finalPositionOfSnake(int n, List<String> commands) {
9+
int ans = 0;
10+
for (String c : commands) {
11+
switch (c.charAt(0)) {
12+
case 'U':
13+
ans -= n;
14+
break;
15+
case 'D':
16+
ans += n;
17+
break;
18+
case 'L':
19+
ans--;
20+
break;
21+
default:
22+
ans++;
23+
break;
24+
}
25+
}
26+
return ans;
27+
}
28+
}

Codes/3248-snake-in-matrix.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'''
2+
Author: LetMeFly
3+
Date: 2024-11-21 23:04:02
4+
LastEditors: LetMeFly.xyz
5+
LastEditTime: 2024-11-21 23:04:09
6+
'''
7+
from typing import List
8+
9+
class Solution:
10+
def finalPositionOfSnake(self, n: int, commands: List[str]) -> int:
11+
ans = 0
12+
for c in commands:
13+
if c[0] == 'U':
14+
ans -= n
15+
elif c[0] == 'D':
16+
ans += n
17+
elif c[0] == 'L':
18+
ans -= 1
19+
else:
20+
ans += 1
21+
return ans

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -724,6 +724,8 @@
724724
|3240.最少翻转次数使二进制矩阵回文II|中等|<a href="https://leetcode.cn/problems/minimum-number-of-flips-to-make-binary-grid-palindromic-ii/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/11/16/LeetCode%203240.%E6%9C%80%E5%B0%91%E7%BF%BB%E8%BD%AC%E6%AC%A1%E6%95%B0%E4%BD%BF%E4%BA%8C%E8%BF%9B%E5%88%B6%E7%9F%A9%E9%98%B5%E5%9B%9E%E6%96%87II/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/143816332" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/minimum-number-of-flips-to-make-binary-grid-palindromic-ii/solutions/2990374/letmefly-3240zui-shao-fan-zhuan-ci-shu-s-qbh3/" target="_blank">LeetCode题解</a>|
725725
|3242.设计相邻元素求和服务|简单|<a href="https://leetcode.cn/problems/design-neighbor-sum-service/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/11/11/LeetCode%203242.%E8%AE%BE%E8%AE%A1%E7%9B%B8%E9%82%BB%E5%85%83%E7%B4%A0%E6%B1%82%E5%92%8C%E6%9C%8D%E5%8A%A1/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/143698347" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/design-neighbor-sum-service/solutions/2985408/letmefly-3242she-ji-xiang-lin-yuan-su-qi-mc7m/" target="_blank">LeetCode题解</a>|
726726
|3243.新增道路查询后的最短距离I|中等|<a href="https://leetcode.cn/problems/shortest-distance-after-road-addition-queries-i/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/11/19/LeetCode%203243.%E6%96%B0%E5%A2%9E%E9%81%93%E8%B7%AF%E6%9F%A5%E8%AF%A2%E5%90%8E%E7%9A%84%E6%9C%80%E7%9F%AD%E8%B7%9D%E7%A6%BBI/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/143897977" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/shortest-distance-after-road-addition-queries-i/solutions/2994264/letmefly-3243xin-zeng-dao-lu-cha-xun-hou-3k6p/" target="_blank">LeetCode题解</a>|
727+
|3244.新增道路查询后的最短距离II|困难|<a href="https://leetcode.cn/problems/shortest-distance-after-road-addition-queries-ii/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/11/20/LeetCode%203244.%E6%96%B0%E5%A2%9E%E9%81%93%E8%B7%AF%E6%9F%A5%E8%AF%A2%E5%90%8E%E7%9A%84%E6%9C%80%E7%9F%AD%E8%B7%9D%E7%A6%BBII/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/143908539" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/shortest-distance-after-road-addition-queries-ii/solutions/2994690/letmefly-3244xin-zeng-dao-lu-cha-xun-hou-7tna/" target="_blank">LeetCode题解</a>|
728+
|3248.矩阵中的蛇|简单|<a href="https://leetcode.cn/problems/snake-in-matrix/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/11/21/LeetCode%203248.%E7%9F%A9%E9%98%B5%E4%B8%AD%E7%9A%84%E8%9B%87/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/143957408" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/snake-in-matrix/solutions/2996578/letmefly-3248ju-zhen-zhong-de-she-mo-ni-ttfkl/" target="_blank">LeetCode题解</a>|
727729
|3249.统计好节点的数目|中等|<a href="https://leetcode.cn/problems/count-the-number-of-good-nodes/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/11/14/LeetCode%203249.%E7%BB%9F%E8%AE%A1%E5%A5%BD%E8%8A%82%E7%82%B9%E7%9A%84%E6%95%B0%E7%9B%AE/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/143768804" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/count-the-number-of-good-nodes/solutions/2988295/letmefly-3249tong-ji-hao-jie-dian-de-shu-zojm/" target="_blank">LeetCode题解</a>|
728730
|3254.长度为K的子数组的能量值I|中等|<a href="https://leetcode.cn/problems/find-the-power-of-k-size-subarrays-i/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/11/06/LeetCode%203254.%E9%95%BF%E5%BA%A6%E4%B8%BAK%E7%9A%84%E5%AD%90%E6%95%B0%E7%BB%84%E7%9A%84%E8%83%BD%E9%87%8F%E5%80%BCI/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/143575677" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/find-the-power-of-k-size-subarrays-i/solutions/2979562/letmefly-3254chang-du-wei-k-de-zi-shu-zu-iu8u/" target="_blank">LeetCode题解</a>|
729731
|3255.长度为K的子数组的能量值II|中等|<a href="https://leetcode.cn/problems/find-the-power-of-k-size-subarrays-ii/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/11/07/LeetCode%203255.%E9%95%BF%E5%BA%A6%E4%B8%BAK%E7%9A%84%E5%AD%90%E6%95%B0%E7%BB%84%E7%9A%84%E8%83%BD%E9%87%8F%E5%80%BCII/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/143591327" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/find-the-power-of-k-size-subarrays-ii/solutions/2980432/letmefly-3255chang-du-wei-k-de-zi-shu-zu-rags/" target="_blank">LeetCode题解</a>|

Solutions/LeetCode 0661.图片平滑器.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,16 @@ tags: [题解, LeetCode, 简单, 数组, 矩阵, 模拟]
1616

1717
<p><img src="https://assets.leetcode.com/uploads/2021/05/03/smoother-grid.jpg" style="height: 493px; width: 493px;" /></p>
1818

19+
<!-- https://i-blog.csdnimg.cn/direct/c0079440db3d44c182a1a7505fa91db1.png -->
20+
1921
<p>给你一个表示图像灰度的 <code>m x n</code> 整数矩阵 <code>img</code> ,返回对图像的每个单元格平滑处理后的图像&nbsp;。</p>
2022

2123
<p>&nbsp;</p>
2224

2325
<p><strong>示例 1:</strong></p>
2426

2527
<p><img src="https://assets.leetcode.com/uploads/2021/05/03/smooth-grid.jpg" /></p>
28+
<!-- ![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/0535b69bc6c343f3a2e3caa5c747939c.png) -->
2629

2730
<pre>
2831
<strong>输入:</strong>img = [[1,1,1],[1,0,1],[1,1,1]]
@@ -35,6 +38,7 @@ tags: [题解, LeetCode, 简单, 数组, 矩阵, 模拟]
3538

3639
<p><strong>示例 2:</strong></p>
3740
<img alt="" src="https://assets.leetcode.com/uploads/2021/05/03/smooth2-grid.jpg" />
41+
<!-- ![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/48d226238db34b39bbf7712fe4791185.png) -->
3842
<pre>
3943
<strong>输入:</strong> img = [[100,200,100],[200,50,200],[100,200,100]]
4044
<strong>输出:</strong> [[137,141,137],[141,138,141],[137,141,137]]

0 commit comments

Comments
 (0)