Skip to content

Commit

Permalink
Merge branch 'master' of github.com:LetMeFly666/LeetCode
Browse files Browse the repository at this point in the history
  • Loading branch information
LetMeFly666 committed Nov 22, 2024
2 parents 39f5d1d + 2044274 commit 9e41c94
Show file tree
Hide file tree
Showing 13 changed files with 826 additions and 6 deletions.
123 changes: 123 additions & 0 deletions Codes/3244-shortest-distance-after-road-addition-queries-ii.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/*
* @Author: LetMeFly
* @Date: 2024-11-20 12:11:48
* @LastEditors: LetMeFly.xyz
* @LastEditTime: 2024-11-20 12:56:12
*/
#ifdef _WIN32
#include "_[1,2]toVector.h"
#endif

/*
每个点记录“自己跃迁到的点”
初始值每个点能跃迁到的点都是自己的下一个节点
新来一条“跃迁通道”有两种可能:
+ 被一条更长(或等长)的跃迁通道覆盖
+ 覆盖n条跃迁通道
反正不可能和其他跃迁通道有交叉
两种情况的判断方式是“跃迁起点”指向的“能跃迁到的点”是否大于(等于)自己的“跃迁终点”
+ 对于第一种情况:直接continue
+ 对于第二种情况:修改所有“最大被覆盖子跃迁通道”的起点的“能跃迁到的点”
*/
#ifdef TirstTry // WA
class Solution {
public:
vector<int> shortestDistanceAfterQueries(int n, vector<vector<int>>& queries) {
vector<int> transitionTo(n);
for (int i = 0; i < n; i++) {
transitionTo[i] = i + 1;
}
int transitionToEndTimes = n - 1;
vector<int> ans(queries.size());
for (int i = 0; i < queries.size(); i++) {
int from = queries[i][0], to = queries[i][1];
while (transitionTo[from] < to) {
int originalTo = transitionTo[from];
transitionToEndTimes -= originalTo - from;
transitionTo[from] = to;
from = originalTo;
}
ans[i] = transitionToEndTimes;
}
return ans;
}
};
#else // TirstTry
#ifdef SecondTry // WA
class Solution {
public:
vector<int> shortestDistanceAfterQueries(int n, vector<vector<int>>& queries) {
vector<int> transitionTo(n);
for (int i = 0; i < n; i++) {
transitionTo[i] = i + 1;
}
int transitionToEndTimes = n - 1;
vector<int> ans(queries.size());
for (int i = 0; i < queries.size(); i++) {
int from = queries[i][0], to = queries[i][1];
int originalJumpTimes = 0;
while (transitionTo[from] <= to) {
originalJumpTimes++;
int originalTo = transitionTo[from];
transitionTo[from] = to;
from = originalTo;
}
transitionToEndTimes -= originalJumpTimes - 1;
ans[i] = transitionToEndTimes;
}
return ans;
}
};
#else // SecondTry
#ifdef ThirdTry // AC
class Solution {
public:
vector<int> shortestDistanceAfterQueries(int n, vector<vector<int>>& queries) {
vector<int> transitionTo(n);
for (int i = 0; i < n; i++) {
transitionTo[i] = i + 1;
}
int transitionToEndTimes = n - 1;
vector<int> ans(queries.size());
for (int i = 0; i < queries.size(); i++) {
int from = queries[i][0], to = queries[i][1];
int originalJumpTimes = 0;
while (transitionTo[from] <= to) {
originalJumpTimes++;
int originalTo = transitionTo[from];
transitionTo[from] = to;
from = originalTo;
}
transitionToEndTimes -= max(0, originalJumpTimes - 1);
ans[i] = transitionToEndTimes;
}
return ans;
}
};
#else // ThirdTry
// FourthTry // 简化版 // 执行用时分布:2ms,击败98.51%;消耗内存分布:108.84MB,击败83.86%.
class Solution {
public:
vector<int> shortestDistanceAfterQueries(int n, vector<vector<int>>& queries) {
vector<int> transitionTo(n), ans(queries.size());
for (int i = 0; i < n; i++) {
transitionTo[i] = i + 1;
}
int transitionToEndTimes = n - 1;
for (int i = 0; i < queries.size(); i++) {
int from = queries[i][0], to = queries[i][1], now = from;
while (transitionTo[now] < to) {
transitionToEndTimes--;
int originalTo = transitionTo[now];
transitionTo[now] = to;
now = originalTo;
}
ans[i] = transitionToEndTimes;
}
return ans;
}
};
#endif // ThirdTry
#endif // SecondTry
#endif // TirstTry
24 changes: 24 additions & 0 deletions Codes/3244-shortest-distance-after-road-addition-queries-ii.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* @Author: LetMeFly
* @Date: 2024-11-20 13:03:17
* @LastEditors: LetMeFly.xyz
* @LastEditTime: 2024-11-20 13:06:36
*/
package main

func shortestDistanceAfterQueries(n int, queries [][]int) (ans []int) {
transitionTo := make([]int, n)
for i := range transitionTo {
transitionTo[i] = i + 1
}
minTimes := n - 1
for _, query := range queries {
from, to := query[0], query[1]
for transitionTo[from] < to {
minTimes--
transitionTo[from], from = to, transitionTo[from]
}
ans = append(ans, minTimes)
}
return
} // AC,81.82%,29.41%
27 changes: 27 additions & 0 deletions Codes/3244-shortest-distance-after-road-addition-queries-ii.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* @Author: LetMeFly
* @Date: 2024-11-20 12:58:48
* @LastEditors: LetMeFly.xyz
* @LastEditTime: 2024-11-20 13:02:59
*/
class Solution {
public int[] shortestDistanceAfterQueries(int n, int[][] queries) {
int[] transitionTo = new int[n];
for (int i = 0; i < n; i++) {
transitionTo[i] = i + 1;
}
int[] ans = new int[queries.length];
int minTimes = n - 1;
for (int i = 0; i < queries.length; i++) {
int from = queries[i][0], to = queries[i][1];
while (transitionTo[from] < to) {
minTimes--;
int originalTo = transitionTo[from];
transitionTo[from] = to;
from = originalTo;
}
ans[i] = minTimes;
}
return ans;
}
} // AC,100.00%,79.09%
19 changes: 19 additions & 0 deletions Codes/3244-shortest-distance-after-road-addition-queries-ii.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'''
Author: LetMeFly
Date: 2024-11-20 12:46:44
LastEditors: LetMeFly.xyz
LastEditTime: 2024-11-20 12:54:50
'''
from typing import List

class Solution:
def shortestDistanceAfterQueries(self, n: int, queries: List[List[int]]) -> List[int]:
transitionTo = [i + 1 for i in range(n)]
ans = []
shortestTimes = n - 1
for from_, to in queries:
while transitionTo[from_] < to:
shortestTimes -= 1
transitionTo[from_], from_ = to, transitionTo[from_]
ans.append(shortestTimes)
return ans
36 changes: 36 additions & 0 deletions Codes/3248-snake-in-matrix.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* @Author: LetMeFly
* @Date: 2024-11-21 22:59:40
* @LastEditors: LetMeFly.xyz
* @LastEditTime: 2024-11-21 23:02:59
*/
#ifdef _WIN32
#include "_[1,2]toVector.h"
#endif

// 开始看题
// 开始解题
class Solution {
public:
int finalPositionOfSnake(int n, vector<string>& commands) {
int ans = 0;
for (string& command : commands) {
switch (command[0])
{
case 'U':
ans -= n;
break;
case 'D':
ans += n;
break;
case 'L':
ans--;
break;
default: // 'R'
ans++;
break;
}
}
return ans;
}
};
23 changes: 23 additions & 0 deletions Codes/3248-snake-in-matrix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* @Author: LetMeFly
* @Date: 2024-11-21 23:07:12
* @LastEditors: LetMeFly.xyz
* @LastEditTime: 2024-11-21 23:08:30
*/
package main

func finalPositionOfSnake(n int, commands []string) (ans int) {
for _, c := range commands {
switch c[0] {
case 'U':
ans -= n;
case 'D':
ans += n;
case 'L':
ans--;
case 'R':
ans++;
}
}
return
}
28 changes: 28 additions & 0 deletions Codes/3248-snake-in-matrix.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* @Author: LetMeFly
* @Date: 2024-11-21 23:05:25
* @LastEditors: LetMeFly.xyz
* @LastEditTime: 2024-11-21 23:06:44
*/
class Solution {
public int finalPositionOfSnake(int n, List<String> commands) {
int ans = 0;
for (String c : commands) {
switch (c.charAt(0)) {
case 'U':
ans -= n;
break;
case 'D':
ans += n;
break;
case 'L':
ans--;
break;
default:
ans++;
break;
}
}
return ans;
}
}
21 changes: 21 additions & 0 deletions Codes/3248-snake-in-matrix.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'''
Author: LetMeFly
Date: 2024-11-21 23:04:02
LastEditors: LetMeFly.xyz
LastEditTime: 2024-11-21 23:04:09
'''
from typing import List

class Solution:
def finalPositionOfSnake(self, n: int, commands: List[str]) -> int:
ans = 0
for c in commands:
if c[0] == 'U':
ans -= n
elif c[0] == 'D':
ans += n
elif c[0] == 'L':
ans -= 1
else:
ans += 1
return ans
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,8 @@
|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>|
|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>|
|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>|
|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>|
|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>|
|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>|
|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>|
|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>|
Expand Down
4 changes: 4 additions & 0 deletions Solutions/LeetCode 0661.图片平滑器.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@ tags: [题解, LeetCode, 简单, 数组, 矩阵, 模拟]

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

<!-- https://i-blog.csdnimg.cn/direct/c0079440db3d44c182a1a7505fa91db1.png -->

<p>给你一个表示图像灰度的 <code>m x n</code> 整数矩阵 <code>img</code> ,返回对图像的每个单元格平滑处理后的图像&nbsp;。</p>

<p>&nbsp;</p>

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

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

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

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

0 comments on commit 9e41c94

Please sign in to comment.