Given an array, rotate the array to the right byĀ kĀ steps, whereĀ kĀ is non-negative.
Example 1:
Input: [1,2,3,4,5,6,7] and k = 3
Output: [5,6,7,1,2,3,4]
Explanation: rotate 1 steps to the right: [7,1,2,3,4,5,6] rotate 2 steps to the right: [6,7,1,2,3,4,5]
rotate 3 steps to the right: [5,6,7,1,2,3,4]
Example 2:
Input:[-1,-100,3,99] and k = 2
Output: [3,99,-1,-100]
Explanation: rotate 1 steps to the right: [99,-1,-100,3] rotate 2 steps to the right: [3,99,-1,-100]
Note:
Try to come up as many solutions as you can, there are at least 3 different ways to solve this problem.
Could you do it in-place with O(1) extra space?
Solution 1: Simulate rotation with three reverses.
If k >= n, rotating k times has the same effect as rotating k % n times.
[1,2,3,4,5,6,7], K = 3
[5,6,7,1,2,3,4]
We can simulate the rotation with three reverses.
reverse the whole array O(n) [7,6,5,4,3,2,1]
reverse the left part 0 ~ k – 1 O(k) [5,6,7,4,3,2,1]
reverse the right part k ~ n – 1 O(n-k)Ā [5,6,7,1,2,3,4]
Starting with anĀ undirectedĀ graph (the “original graph”) with nodes fromĀ 0Ā toĀ N-1, subdivisions are made to some of the edges.
The graph is given as follows:Ā edges[k]Ā is a list of integer pairsĀ (i, j, n)Ā such thatĀ (i, j)Ā is an edge of the original graph,
andĀ nĀ is the total number ofĀ newĀ nodes on that edge.
Then, the edgeĀ (i, j)Ā is deleted from the original graph,Ā nĀ new nodesĀ (x_1, x_2, ..., x_n)Ā are added to the original graph,
andĀ n+1Ā newĀ edgesĀ (i, x_1), (x_1, x_2), (x_2, x_3), ..., (x_{n-1}, x_n), (x_n, j)Ā are added to the originalĀ graph.
Now, you start at nodeĀ 0Ā from the original graph, and in each move, you travel along oneĀ edge.
Return how many nodes you can reach in at mostĀ MĀ moves.
Example 1:
Input: edge = [[0,1,10],[0,2,1],[1,2,2]], M = 6, N = 3Output: 13Explanation: The nodes that are reachable in the final graph after M = 6 moves are indicated below.
Example 2:
Input: edges = [[0,1,4],[1,2,6],[0,2,8],[1,3,1]], M = 10, N = 4
Output: 23
Note:
0 <= edges.length <= 10000
0 <= edges[i][0] <Ā edges[i][1] < N
There does not exist anyĀ i != jĀ for whichĀ edges[i][0] == edges[j][0]Ā andĀ edges[i][1] == edges[j][1].
The original graphĀ has no parallel edges.
0 <= edges[i][2] <= 10000
0 <= M <= 10^9
1 <= N <= 3000
Solution: Dijkstra Shortest Path
Compute the shortest from 0 to rest of the nodes. Use HP to mark the maximum moves left to reach each node.
HP[u] = a, HP[v] = b, new_nodes[u][v] = c
nodes covered between a<->b = min(c, a + b)
Time complexity: O(ElogE)
Space complexity: O(E)
C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// Author: Huahua
// Running time: 88 ms
classSolution{
public:
intreachableNodes(vector<vector<int>>& edges, int M, int N) {
unordered_map<int, unordered_map<int, int>> g;
for(constauto& e : edges)
g[e[0]][e[1]] = g[e[1]][e[0]] = e[2];
priority_queue<pair<int,int>>q;// {hp, node}, sort by HP desc
unordered_map<int,int>HP;// node -> max HP left
q.push({M,0});
while(!q.empty()){
inthp=q.top().first;
intcur=q.top().second;
q.pop();
if(HP.count(cur))continue;
HP[cur]=hp;
for(constauto& pair : g[cur]) {
int nxt = pair.first;
intnxt_hp=hp-pair.second-1;
if(HP.count(nxt)||nxt_hp<0)continue;
q.push({nxt_hp,nxt});
}
}
intans=HP.size();// Original nodes covered.
for(constauto& e : edges) {
int uv = HP.count(e[0]) ? HP[e[0]] : 0;
intvu=HP.count(e[1])?HP[e[1]]:0;
ans+=min(e[2],uv+vu);
}
returnans;
}
};
Optimized Dijkstra (replace hashmap with vector)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// Author: Huahua
// Running time: 56 ms (beats 88%)
classSolution{
public:
intreachableNodes(vector<vector<int>>& edges, int M, int N) {