-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of github.com:LetMeFly666/LeetCode
- Loading branch information
Showing
13 changed files
with
826 additions
and
6 deletions.
There are no files selected for viewing
123 changes: 123 additions & 0 deletions
123
Codes/3244-shortest-distance-after-road-addition-queries-ii.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
24
Codes/3244-shortest-distance-after-road-addition-queries-ii.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
27
Codes/3244-shortest-distance-after-road-addition-queries-ii.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
19
Codes/3244-shortest-distance-after-road-addition-queries-ii.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.