question_slug
stringlengths
3
77
title
stringlengths
1
183
slug
stringlengths
12
45
summary
stringlengths
1
160
⌀
author
stringlengths
2
30
certification
stringclasses
2 values
created_at
stringdate
2013-10-25 17:32:12
2025-04-12 09:38:24
updated_at
stringdate
2013-10-25 17:32:12
2025-04-12 09:38:24
hit_count
int64
0
10.6M
has_video
bool
2 classes
content
stringlengths
4
576k
upvotes
int64
0
11.5k
downvotes
int64
0
358
tags
stringlengths
2
193
comments
int64
0
2.56k
semi-ordered-permutation
1 ms solution
1-ms-solution-by-developersusername-yx1p
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
DevelopersUsername
NORMAL
2024-11-16T11:36:31.819560+00:00
2024-11-16T11:36:31.819592+00:00
1
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\nO(n)\n\n- Space complexity:\nO(1)\n\n# Code\n```java []\nclass Solution {\n public int semiOrderedPermutation(int[] nums) {\n\n ...
0
0
['Java']
0
semi-ordered-permutation
Java || Beats 100%
java-beats-100-by-swapnil-chhatre-4g7h
\nclass Solution {\n public int semiOrderedPermutation(int[] nums) {\n int pos1 = 0;\n int pos2 = 0;\n \n for(int i = 0; i < nums
swapnil-chhatre
NORMAL
2024-11-11T05:19:16.937175+00:00
2024-11-11T05:19:16.937216+00:00
3
false
```\nclass Solution {\n public int semiOrderedPermutation(int[] nums) {\n int pos1 = 0;\n int pos2 = 0;\n \n for(int i = 0; i < nums.length; i++) {\n if(nums[i] == 1)\n pos1 = i;\n else if(nums[i] == nums.length)\n pos2 = i;\n }\n...
0
0
['Java']
0
semi-ordered-permutation
Easy C++ Code (Beats 100%)
easy-c-code-beats-100-by-ai1a_2310812-n2xo
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
ai1a_2310812
NORMAL
2024-10-31T17:51:43.602972+00:00
2024-10-31T17:51:43.602997+00:00
1
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
0
0
['C++']
0
semi-ordered-permutation
Java&JS&TS Solution (JW)
javajsts-solution-jw-by-specter01wj-eou6
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
specter01wj
NORMAL
2024-10-29T20:33:24.045019+00:00
2024-10-29T20:33:32.499091+00:00
7
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
0
0
['Array', 'Java', 'TypeScript', 'JavaScript']
0
semi-ordered-permutation
Easy JAVA Solution!
easy-java-solution-by-priyadarsan2509-491a
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
Priyadarsan2509
NORMAL
2024-10-23T02:38:13.314461+00:00
2024-10-23T02:38:13.314490+00:00
4
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
0
0
['Java']
0
semi-ordered-permutation
The Fastest 100% Runtime JS/TS Solution
the-fastest-100-runtime-jsts-solution-by-x80d
Approach\n Describe your approach to solving the problem. \nWe find indexes of the smallest firstIndex and the largest lastIndex integers in the array.\nIf they
mirzaianov
NORMAL
2024-10-19T11:56:09.116021+00:00
2024-10-19T11:56:09.116053+00:00
3
false
# Approach\n<!-- Describe your approach to solving the problem. -->\nWe find indexes of the smallest `firstIndex` and the largest `lastIndex` integers in the array.\nIf they are placed at the start and at the end of the array respectively, we immediately return `0`.\nThen we check whether the index of the smallest digi...
0
0
['TypeScript', 'JavaScript']
0
semi-ordered-permutation
JS | Swapping | Runtime 4 ms | Beats 100.00% | Bloated Code warning
js-swapping-runtime-4-ms-beats-10000-blo-dkh7
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
aimalkhan177
NORMAL
2024-10-18T15:37:21.798654+00:00
2024-10-18T15:37:21.798683+00:00
2
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
0
0
['JavaScript']
0
semi-ordered-permutation
solution for C#
solution-for-c-by-annyhuuuu-5os5
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
Annyhuuuu
NORMAL
2024-10-17T13:05:44.202620+00:00
2024-10-17T13:05:44.202655+00:00
2
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
0
0
['C#']
0
semi-ordered-permutation
✅ Rust Solution
rust-solution-by-erikrios-7mdt
\nimpl Solution {\n pub fn semi_ordered_permutation(nums: Vec<i32>) -> i32 {\n let n = nums.len();\n let mut nums = nums;\n\n let mut mi
erikrios
NORMAL
2024-10-08T03:42:22.627112+00:00
2024-10-08T03:42:22.627149+00:00
3
false
```\nimpl Solution {\n pub fn semi_ordered_permutation(nums: Vec<i32>) -> i32 {\n let n = nums.len();\n let mut nums = nums;\n\n let mut min_num_operations = 0;\n while !(nums[0] == 1 && nums[n - 1] == n as i32) {\n for i in 0..n {\n let num = nums[i];\n ...
0
0
['Rust']
0
semi-ordered-permutation
Simple Approach
simple-approach-by-sssvchaitanya-dldr
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
sssvchaitanya
NORMAL
2024-10-06T10:44:44.407543+00:00
2024-10-06T10:44:44.407593+00:00
2
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
0
0
['Java']
0
semi-ordered-permutation
Easy To Read | Python Solution
easy-to-read-python-solution-by-werrt832-3hvk
Code\npython3 []\nclass Solution:\n def semiOrderedPermutation(self, nums: List[int]) -> int:\n min_pos = max_pos = -1\n for i in range(len(num
werrt8321
NORMAL
2024-09-09T09:19:09.725812+00:00
2024-09-09T09:19:09.725840+00:00
6
false
# Code\n```python3 []\nclass Solution:\n def semiOrderedPermutation(self, nums: List[int]) -> int:\n min_pos = max_pos = -1\n for i in range(len(nums)):\n if nums[i] == 1:\n min_pos = i\n if nums[i] == len(nums):\n max_pos = i\n if min_pos ...
0
0
['Python3']
0
semi-ordered-permutation
Cool 3 to 1 line solution
cool-3-to-1-line-solution-by-jafisik-yqp7
Intuition\n Describe your first thoughts on how to solve this problem. \nCalculate the index of 1 and n (they are also the steps needed to get to the edge)\nThe
Jafisik
NORMAL
2024-09-08T01:33:09.796840+00:00
2024-09-08T01:33:09.796864+00:00
1
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nCalculate the index of 1 and n (they are also the steps needed to get to the edge)\nThen if 1 is after n in the array subtract one, because the swap would move n closer to the end. Like this 2413 -> 2143 (distance from 4 to the end was 2 ...
0
0
['C#']
0
semi-ordered-permutation
JavaScript 2 pointer
javascript-2-pointer-by-donovanodom-6q93
\nconst semiOrderedPermutation = function(nums) {\n let i = 0, j = nums.length - 1\n while(nums[i] != 1) i++\n while(nums[j] != nums.length) j--\n if(i < j)
donovanodom
NORMAL
2024-09-06T23:34:03.768695+00:00
2024-09-06T23:34:03.768718+00:00
1
false
```\nconst semiOrderedPermutation = function(nums) {\n let i = 0, j = nums.length - 1\n while(nums[i] != 1) i++\n while(nums[j] != nums.length) j--\n if(i < j) return nums.length - 1 - j + i\n return nums.length - 2 - j + i\n};\n```
0
0
['JavaScript']
0
semi-ordered-permutation
C++ Solution
c-solution-by-user1122v-nqps
\n# Code\ncpp []\nclass Solution {\npublic:\n int semiOrderedPermutation(vector<int>& nums) {\n int idxOfOne, idxOfN, n = nums.size();\n for(in
user1122v
NORMAL
2024-09-03T09:36:49.443004+00:00
2024-09-03T09:36:49.443037+00:00
1
false
\n# Code\n```cpp []\nclass Solution {\npublic:\n int semiOrderedPermutation(vector<int>& nums) {\n int idxOfOne, idxOfN, n = nums.size();\n for(int i = 0; i < n; i++){\n if(nums[i] == 1) idxOfOne = i;\n else if(nums[i] == n) idxOfN = i;\n }\n int res = idxOfOne + (n ...
0
0
['C++']
0
semi-ordered-permutation
Python for New Leet Coder - Super Readable Solution
python-for-new-leet-coder-super-readable-rwl0
\nclass Solution:\n def semiOrderedPermutation(self, nums: List[int]) -> int:\n start, end = 0, len(nums)-1\n if nums[0] == 1 and nums[-1] == l
wohcderfla
NORMAL
2024-08-24T07:38:17.266577+00:00
2024-08-24T07:38:17.266613+00:00
4
false
```\nclass Solution:\n def semiOrderedPermutation(self, nums: List[int]) -> int:\n start, end = 0, len(nums)-1\n if nums[0] == 1 and nums[-1] == len(nums):\n return 0\n \n for i in range(len(nums)):\n if nums[i] == 1:\n start = i\n if nums[i...
0
0
['Python']
0
semi-ordered-permutation
Python3 easy solution
python3-easy-solution-by-xnxq1-tsa0
Since we can change neighboring elements, the result will be the summation of the indices(idx_fst_nm + n - 1 - idx_lst_nm) of the desired elements to their ref
xnxq1
NORMAL
2024-08-22T13:29:46.291111+00:00
2024-08-22T13:29:46.291136+00:00
2
false
Since we can change neighboring elements, the result will be the summation of the indices(**idx_fst_nm + n - 1 - idx_lst_nm**) of the desired elements to their reference points. \nif idx_fst_nm > idx_lst_nm then we must subtract 1, since in the end they will meet and 1 less action will be needed\n\n\n**Complexity\nTim...
0
0
['Array', 'Math', 'Python3']
0
number-of-restricted-paths-from-first-to-last-node
[Python/Java] Dijkstra & Cached DFS - Clean & Concise
pythonjava-dijkstra-cached-dfs-clean-con-k6m2
Idea\n- We use Dijkstra to calculate shortest distance paths from last node n to any other nodes x, the result is distanceToLastNode(x), where x in 1..n. Comple
hiepit
NORMAL
2021-03-07T04:01:54.325354+00:00
2021-03-08T02:44:40.990084+00:00
11,037
false
**Idea**\n- We use Dijkstra to calculate shortest distance paths from last node `n` to any other nodes `x`, the result is `distanceToLastNode(x)`, where `x in 1..n`. Complexity: `O(E * logV) = O(M logN)`, where `M` is number of edges, `N` is number of nodes.\n- In the restricted path, `[z0, z1, z2, ..., zk]`, node `zi...
147
8
[]
27
number-of-restricted-paths-from-first-to-last-node
[Java] - Dijkstra's + DFS + Memoization + Explanation of problem description
java-dijkstras-dfs-memoization-explanati-hog9
As a note I spent 40 minutes trying to understand what this problem was asking. In classic fashion it finally dawned on me with 5 minutes left in the contest. I
kniffina
NORMAL
2021-03-07T04:37:56.775675+00:00
2021-03-08T04:38:26.494150+00:00
4,733
false
As a note I spent 40 minutes trying to understand what this problem was asking. In classic fashion it finally dawned on me with 5 minutes left in the contest. I will try to explain below what the problem failed to explain (in my opinion). \n\n* Everything is pretty straight forward in the first two paragraphs. We get s...
124
0
[]
11
number-of-restricted-paths-from-first-to-last-node
c++ Djikstra and dfs
c-djikstra-and-dfs-by-codingmission-bp66
Please upvote if you find this useful\n\nDefinition of Restricted Paths : Path from source node(n) to target node(1) should have strictly increasing shortest di
codingmission
NORMAL
2021-03-07T04:00:40.718814+00:00
2021-03-07T04:24:05.807387+00:00
6,693
false
**Please upvote if you find this useful**\n\nDefinition of Restricted Paths : Path from source node(n) to target node(1) should have strictly increasing shortest distance to source(n) for all nodes in path.\n\n- Idea is to calculate shortest distance from node n to all nodes and save it in dist array.\n- Then use dfs f...
79
1
['C']
8
number-of-restricted-paths-from-first-to-last-node
C++ Dijkstra + DP
c-dijkstra-dp-by-lzl124631x-nm3u
See my latest update in repo LeetCode\n\n## Solution 1. Dijkstra + DP\n\nWe run Dijkstra algorithm starting from the nth node. \n\nLet dist[u] be the distance f
lzl124631x
NORMAL
2021-03-07T05:45:02.654055+00:00
2021-03-07T05:49:18.023391+00:00
3,258
false
See my latest update in repo [LeetCode](https://github.com/lzl124631x/LeetCode)\n\n## Solution 1. Dijkstra + DP\n\nWe run Dijkstra algorithm starting from the `n`th node. \n\nLet `dist[u]` be the distance from the `u` node to `n`th node.\n\nLet `cnt[u]` be the number of restricted path from `u` node to `n`th node.\n\nE...
50
0
[]
6
number-of-restricted-paths-from-first-to-last-node
C++ BFS + DFS
c-bfs-dfs-by-votrubac-wuvv
This problem was difficult for me to understand. Though, when I finally was able to unpack it, implementing it did not seem that hard.\n\n> Update: the initial
votrubac
NORMAL
2021-03-07T16:18:10.507729+00:00
2021-03-12T17:32:48.315682+00:00
2,582
false
This problem was difficult for me to understand. Though, when I finally was able to unpack it, implementing it did not seem that hard.\n\n> Update: the initial solution was accepted (< 500 ms), but now it gives TLE. We now need to use a priority queue for picking the next number during BFS.\n\n**BFS**\nTo figure out re...
23
1
[]
4
number-of-restricted-paths-from-first-to-last-node
[Python] One-pass Dijkstra with PriorityQueue, no extra DP
python-one-pass-dijkstra-with-priorityqu-3djj
\n def countRestrictedPaths(self, n: int, edges: List[List[int]]) -> int:\n adj = [dict() for i in range(n + 1)]\n for u, v, wgt in edges:\n
otoc
NORMAL
2021-03-07T04:05:09.332494+00:00
2021-03-07T04:29:23.550925+00:00
1,665
false
```\n def countRestrictedPaths(self, n: int, edges: List[List[int]]) -> int:\n adj = [dict() for i in range(n + 1)]\n for u, v, wgt in edges:\n adj[u][v] = wgt\n adj[v][u] = wgt\n # Dijkstra\'s algorithm\n dist = [float(\'inf\') for _ in range(n)] + [0]\n num_...
17
1
[]
5
number-of-restricted-paths-from-first-to-last-node
C++ | Explanation | Easy to understand
c-explanation-easy-to-understand-by-vina-x6hx
\tC++ Code with explanation \n\t\n\t\n\tWe can use dijsktra Algorithm for finding the distance from src = lastNode to each node. \n\tBecause the constraints 0<
vinayakdhimang
NORMAL
2021-03-07T04:23:15.471673+00:00
2021-07-03T09:46:23.596252+00:00
1,272
false
\tC++ Code with explanation \n\t\n\t\n\tWe can use dijsktra Algorithm for finding the distance from src = lastNode to each node. \n\tBecause the constraints 0<=n<=2 * 10 ^4 so that dijsktra (n^2) won\'t be work for this we \n\tuse Min-Heap (in C++ standard template library (STL) we use Priority queue).\n\t\n\t\n\tAnd ...
10
0
[]
4
number-of-restricted-paths-from-first-to-last-node
[C++] Detailed Explanation | Easy Approach | DFS
c-detailed-explanation-easy-approach-dfs-bd6x
Question Explanation:\nConsider ntoxSD[x] as the shortest distance of a path between node n and node x.\nFind the number of paths from node 1 that leads to node
isha_070_
NORMAL
2021-10-18T10:52:37.818099+00:00
2022-07-16T16:09:19.672358+00:00
959
false
**Question Explanation:**\nConsider ```ntoxSD[x]``` as the shortest distance of a path between ```node n``` and ```node x```.\nFind the number of paths from ```node 1``` that leads to ```node n``` such that all the nodes you come across while traversing that path has ```ntoxSD[node]``` in decreasing order.\n\n\n**Solut...
7
0
['Depth-First Search', 'Memoization', 'C']
2
number-of-restricted-paths-from-first-to-last-node
Python Djikstra and count path along the way (no need dfs or dp)
python-djikstra-and-count-path-along-the-2dw3
Just run Djikstra and use a dictionary count to keep track of the number of restricted paths to any node. Note that if we reach node v from u, then it must be t
yanx1
NORMAL
2021-04-06T05:48:27.938106+00:00
2021-04-06T05:48:27.938149+00:00
781
false
Just run Djikstra and use a dictionary `count` to keep track of the number of restricted paths to any node. Note that if we reach node `v` from `u`, then it must be the case that` d[u] <= d[v] `, and we only update `count[v]` if `d[u] < d[v]` \uFF08 think about why :-) )\n```\nclass Solution:\n def countRestrictedPa...
7
0
[]
2
number-of-restricted-paths-from-first-to-last-node
Python [Graph DP]
python-graph-dp-by-gsan-uvpa
This question combines graph algorithms with dynamic programming. For shortest distances from each source to target use Dijkstra, which I wrote in a standalone
gsan
NORMAL
2021-03-07T04:01:57.743151+00:00
2021-03-07T04:01:57.743196+00:00
909
false
This question combines graph algorithms with dynamic programming. For shortest distances from each source to target use Dijkstra, which I wrote in a standalone class.\n\nOnce you get the distances, consider a dynamic programming where `dp(u)` is the number of restricted paths from vertex `u` to target. We can write a t...
6
0
[]
3
number-of-restricted-paths-from-first-to-last-node
Detailed Solution || BFS-DFS-DP
detailed-solution-bfs-dfs-dp-by-ragnar11-vmh1
Please copy the code in your compiler for better visiblity\n\n# Code\n\n//In this question first we need to find the minimum weight(smiliar to minimum distance)
ragnar1101
NORMAL
2023-09-03T18:33:30.677991+00:00
2023-09-03T18:33:30.678019+00:00
650
false
Please copy the code in your compiler for better visiblity\n\n# Code\n```\n//In this question first we need to find the minimum weight(smiliar to minimum distance) it takes to reach the end node from every node\n// after that we traverse from first node in search of a valid path and we keep a count and return it\nclass...
5
0
['Dynamic Programming', 'Graph', 'Heap (Priority Queue)', 'Shortest Path', 'C++']
0
number-of-restricted-paths-from-first-to-last-node
[Python3] Dijkstra + Cache DFS ~ DP Top-down Memorization - Simple
python3-dijkstra-cache-dfs-dp-top-down-m-xvh6
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
dolong2110
NORMAL
2023-08-06T16:45:56.887147+00:00
2023-08-06T16:45:56.887164+00:00
379
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: $$O(V)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(V + E)$$\n<!-- Add your space complexity ...
5
0
['Depth-First Search', 'Breadth-First Search', 'Shortest Path', 'Python3']
0
number-of-restricted-paths-from-first-to-last-node
c++ | easy | fast
c-easy-fast-by-venomhighs7-hcr5
\n\n# Code\n\n\nclass Solution {\n typedef pair<int, int> PII;\npublic:\n int countRestrictedPaths(int n, vector<vector<int>>& E) {\n long mod = 1e
venomhighs7
NORMAL
2022-11-15T04:00:42.616009+00:00
2022-11-15T04:00:42.616050+00:00
1,017
false
\n\n# Code\n```\n\nclass Solution {\n typedef pair<int, int> PII;\npublic:\n int countRestrictedPaths(int n, vector<vector<int>>& E) {\n long mod = 1e9 + 7;\n vector<vector<pair<long, int>>> G(n);\n for (auto &e : E) {\n int u = e[0] - 1, v = e[1] - 1, w = e[2];\n G[u].e...
5
0
['C++']
0
number-of-restricted-paths-from-first-to-last-node
[ C++ ], Beats 100% on space and time
c-beats-100-on-space-and-time-by-harshjo-9uto
\n\t#define pi pair<int, int>\n const int mod = 1e9+7;\n vector<int> dis, vis, dp;\n vector<vector<pi>> g;\n int n;\n\t\n // FIND SHORTEST PATH F
harshjoeyit
NORMAL
2021-03-15T04:52:55.736843+00:00
2021-03-15T04:52:55.736886+00:00
735
false
```\n\t#define pi pair<int, int>\n const int mod = 1e9+7;\n vector<int> dis, vis, dp;\n vector<vector<pi>> g;\n int n;\n\t\n // FIND SHORTEST PATH FROM \'n\' to ALL OTHER NODES\n void dijkstra(int u) {\n priority_queue<pi, vector<pi>, greater<pi>> pq;\n dis[u] = 0;\n pq.push({0, u...
5
0
['Dynamic Programming', 'Depth-First Search', 'Shortest Path']
1
number-of-restricted-paths-from-first-to-last-node
[Python3] Dijkstra + dp
python3-dijkstra-dp-by-ye15-ewi5
\n\nclass Solution:\n def countRestrictedPaths(self, n: int, edges: List[List[int]]) -> int:\n graph = {} # graph as adjacency list \n for u, v
ye15
NORMAL
2021-03-07T04:02:44.298236+00:00
2021-03-08T04:30:40.734378+00:00
895
false
\n```\nclass Solution:\n def countRestrictedPaths(self, n: int, edges: List[List[int]]) -> int:\n graph = {} # graph as adjacency list \n for u, v, w in edges: \n graph.setdefault(u, []).append((v, w))\n graph.setdefault(v, []).append((u, w))\n \n queue = [n]\n ...
5
0
['Python3']
1
number-of-restricted-paths-from-first-to-last-node
Easy One Pass Dijkstra Solution || No DFS + DP required
easy-one-pass-dijkstra-solution-no-dfs-d-ciuk
Approach\nApproach:\nThe approach to solve this problem is based on a combination of Dijkstra\'s algorithm for shortest paths and dynamic programming.\n\nShorte
sakshamsharma809
NORMAL
2024-06-19T11:42:56.139146+00:00
2024-06-19T11:42:56.139176+00:00
308
false
# Approach\nApproach:\nThe approach to solve this problem is based on a combination of Dijkstra\'s algorithm for shortest paths and dynamic programming.\n\nShortest Path Calculation (Dijkstra\'s Algorithm):\n\nWe start by calculating the shortest distance from every node to node n using Dijkstra\'s algorithm.\nWe use a...
4
0
['Dynamic Programming', 'Graph', 'Topological Sort', 'Heap (Priority Queue)', 'Shortest Path', 'Java']
1
number-of-restricted-paths-from-first-to-last-node
Python 3 | Dijkstra, DFS, DAG pruning | Explanation
python-3-dijkstra-dfs-dag-pruning-explan-zmrr
Implementation\n- The solution is basically an implementation based on hint, please seee below comments for detail\n### Explanation\n\nclass Solution:\n def
idontknoooo
NORMAL
2021-08-29T06:43:08.548232+00:00
2021-08-29T06:43:08.548265+00:00
767
false
### Implementation\n- The solution is basically an implementation based on `hint`, please seee below comments for detail\n### Explanation\n```\nclass Solution:\n def countRestrictedPaths(self, n: int, edges: List[List[int]]) -> int:\n graph = collections.defaultdict(list) # build graph\n for a, b...
4
1
['Depth-First Search', 'Python', 'Python3']
2
number-of-restricted-paths-from-first-to-last-node
[Java] Dijkshtra + memoization
java-dijkshtra-memoization-by-valarmorgh-4w3w
\nclass Solution {\n int dp[];\n //We use memoization\n public int countRestrictedPaths(int n, int[][] edges) {\n int[] dist = new int[n+1];\n
valarmorghulis89
NORMAL
2021-03-07T04:02:56.860524+00:00
2021-03-07T04:02:56.860566+00:00
937
false
```\nclass Solution {\n int dp[];\n //We use memoization\n public int countRestrictedPaths(int n, int[][] edges) {\n int[] dist = new int[n+1];\n dp = new int[n+1];\n Arrays.fill(dp,-1);\n Map<Integer, Map<Integer, Integer>> graph = new HashMap<>();\n //Create the graph from ...
4
1
['Java']
2
number-of-restricted-paths-from-first-to-last-node
[C++] : Using dikstra + Toposort + DP
c-using-dikstra-toposort-dp-by-zanhd-lkmu
\n#define ll long long int\nconst ll M = 1e9 + 7;\n#define MOD_ADD(a,b,m) ((a % m) + (b % m)) % m\nclass Solution {\npublic:\n \n static bool compare(pair
zanhd
NORMAL
2022-04-15T12:54:30.286889+00:00
2022-04-15T12:54:56.824288+00:00
414
false
```\n#define ll long long int\nconst ll M = 1e9 + 7;\n#define MOD_ADD(a,b,m) ((a % m) + (b % m)) % m\nclass Solution {\npublic:\n \n static bool compare(pair<ll,ll> a, pair<ll,ll> b)\n {\n return a.second > b.second; // for min heap\n }\n \n vector<ll> dikstra(ll src, vector<vector<pair<ll,ll>...
3
0
['Dynamic Programming', 'Topological Sort']
0
number-of-restricted-paths-from-first-to-last-node
Straightforward Python Dijkstra + DP - Beats 98% Runtime and Memory!
straightforward-python-dijkstra-dp-beats-kdg4
\nclass Solution:\n def countRestrictedPaths(self, n: int, edges: List[List[int]]) -> int:\n graph = defaultdict(dict)\n for u, v, w in edges:\
tullya
NORMAL
2021-05-09T02:13:40.812940+00:00
2021-07-30T02:16:29.300267+00:00
473
false
```\nclass Solution:\n def countRestrictedPaths(self, n: int, edges: List[List[int]]) -> int:\n graph = defaultdict(dict)\n for u, v, w in edges:\n graph[u][v] = w\n graph[v][u] = w\n \n paths = [0] * (n+1)\n paths[n] = 1\n dists = [-1] * (n+1)\n ...
3
0
['Dynamic Programming', 'Python']
1
number-of-restricted-paths-from-first-to-last-node
C# Dijkstra’s with SortedSet + DFS with Memo | O(E*Log(N^2)) time
c-dijkstras-with-sortedset-dfs-with-memo-nu0e
SortedSet works as a priority queue here. SortedSet.Add method takes Log(N^2) time in our case which leads to O(ELog(N^2)) time complexity overall where E is th
xenfruit
NORMAL
2021-03-08T04:05:18.807507+00:00
2021-04-11T04:27:50.951981+00:00
193
false
SortedSet works as a priority queue here. SortedSet.Add method takes Log(N^2) time in our case which leads to O(ELog(N^2)) time complexity overall where E is the number of edges and N is the number of nodes\n\n```\npublic class Solution\n{\n public int CountRestrictedPaths(int n, int[][] edges)\n {\n var g...
3
1
['Depth-First Search']
1
number-of-restricted-paths-from-first-to-last-node
Clean Python 3, Dijkstra and top-down dp
clean-python-3-dijkstra-and-top-down-dp-ujldz
Use Dijkstra algorithm to find the shortest path from n.\nAnd use top-down dp to find all valid paths.\n\nTime: O(E + (V + E)logV + 2 * E) = O((V + E)logV)\nSpa
lenchen1112
NORMAL
2021-03-07T04:01:42.414336+00:00
2021-03-07T09:35:46.154705+00:00
340
false
Use Dijkstra algorithm to find the shortest path from `n`.\nAnd use top-down dp to find all valid paths.\n\nTime: `O(E + (V + E)logV + 2 * E) = O((V + E)logV)`\nSpace: `O(E + V)`\n\n```\nimport collections\nimport functools\nimport heapq\nclass Solution:\n def countRestrictedPaths(self, n: int, edges: List[List[int]...
3
0
[]
1
number-of-restricted-paths-from-first-to-last-node
C++✅✅| Simplest approach (Dijkstra + DP)🚀🚀
c-simplest-approach-dijkstra-dp-by-aayu_-yeyp
\n\n# Code\n\nclass Solution {\npublic:\n int countRestrictedPaths(int n, vector<vector<int>>& edges) {\n vector<pair<int, int>> adj[n + 1];\n
aayu_t
NORMAL
2024-06-29T04:54:54.446617+00:00
2024-06-29T04:54:54.446641+00:00
614
false
\n\n# Code\n```\nclass Solution {\npublic:\n int countRestrictedPaths(int n, vector<vector<int>>& edges) {\n vector<pair<int, int>> adj[n + 1];\n for(auto it : edges){\n adj[it[0]].push_back({it[1], it[2]});\n adj[it[1]].push_back({it[0], it[2]});\n }\n vector<int> d...
2
0
['Dynamic Programming', 'Graph', 'Heap (Priority Queue)', 'Shortest Path', 'C++']
0
number-of-restricted-paths-from-first-to-last-node
beats 99% c++ explaination
beats-99-c-explaination-by-mnikhil1011-u1ep
Approach\n Describe your approach to solving the problem. \nthere are multiple seperate steps to reach to the final answer\n\n1)form the adj list\n\n2)find the
mnikhil1011
NORMAL
2023-07-15T14:19:28.636111+00:00
2023-07-15T14:19:28.636136+00:00
376
false
# Approach\n<!-- Describe your approach to solving the problem. -->\nthere are multiple seperate steps to reach to the final answer\n\n1)form the adj list\n\n2)find the dist of every node from last node. to do this we can use [Dijkstra\u2019s Algorithm](https://www.geeksforgeeks.org/dijkstras-shortest-path-algorithm-gr...
2
0
['Dynamic Programming', 'Depth-First Search', 'Graph', 'Heap (Priority Queue)', 'C++']
0
number-of-restricted-paths-from-first-to-last-node
Simple Solution
simple-solution-by-java_programmer_ketan-lxaa
\nclass Solution {\n public int countRestrictedPaths(int n, int[][] edges) {\n List<List<int[]>> undirectedEdges = new ArrayList<>();\n long[] c
Java_Programmer_Ketan
NORMAL
2022-03-10T06:40:03.194367+00:00
2022-03-10T06:40:03.194411+00:00
244
false
```\nclass Solution {\n public int countRestrictedPaths(int n, int[][] edges) {\n List<List<int[]>> undirectedEdges = new ArrayList<>();\n long[] cache = new long[n+1];\n Arrays.fill(cache,-1);\n cache[n]=1;\n for(int i=0;i<=n;i++) \n undirectedEdges.add(new ArrayList<>()...
2
0
['Dynamic Programming']
0
number-of-restricted-paths-from-first-to-last-node
[Python] Clean + Short | Dijkstra's Algorithm + DFS + Memoization | 40 Line Solution
python-clean-short-dijkstras-algorithm-d-hupd
```\nfrom queue import PriorityQueue\n\nclass Solution:\n def countRestrictedPaths(self, n: int, edges: List[List[int]]) -> int:\n g = defaultdict(dic
viktorb1
NORMAL
2022-02-15T23:59:59.935022+00:00
2022-02-16T00:07:13.058363+00:00
398
false
```\nfrom queue import PriorityQueue\n\nclass Solution:\n def countRestrictedPaths(self, n: int, edges: List[List[int]]) -> int:\n g = defaultdict(dict)\n \n for u, v, w in edges: # create adjacency list\n g[u][v] = w\n g[v][u] = w\n \n def dijkstras(start): #...
2
0
['Depth-First Search', 'Python']
0
number-of-restricted-paths-from-first-to-last-node
Dijkstra + DFS | C++
dijkstra-dfs-c-by-prakhar3099-96u4
\nconst int mod = 1e9+7;\n\tint dp[20005];\n int countRestrictedPaths(int n, vector<vector<int>>& edges) {\n \n memset(dp,-1,sizeof(dp));\n
prakhar3099
NORMAL
2021-11-10T04:23:28.992243+00:00
2021-11-10T04:23:28.992308+00:00
324
false
```\nconst int mod = 1e9+7;\n\tint dp[20005];\n int countRestrictedPaths(int n, vector<vector<int>>& edges) {\n \n memset(dp,-1,sizeof(dp));\n vector<vector<pair<int,int>>> adj(n+1);\n \n //building the adjacency matrix\n for(int i = 0; i<edges.size(); i++){\n adj...
2
0
['Depth-First Search', 'C']
1
number-of-restricted-paths-from-first-to-last-node
java
java-by-jjyz-vi83
```\nclass Solution {\n int mod = 1_000_000_007;\n public int countRestrictedPaths(int n, int[][] edges) {\n List[] nei = new List[n+1];\n f
JJYZ
NORMAL
2021-04-06T03:34:15.577322+00:00
2021-04-06T03:34:15.577354+00:00
170
false
```\nclass Solution {\n int mod = 1_000_000_007;\n public int countRestrictedPaths(int n, int[][] edges) {\n List<int[]>[] nei = new List[n+1];\n for(int i=1; i<=n; i++)\n nei[i] = new ArrayList<int[]>();\n for(int[] edge : edges){\n nei[edge[0]].add(new int[]{edge[1], e...
2
0
[]
0
number-of-restricted-paths-from-first-to-last-node
C++ beats 100 100 using dijksta and dfs with memoization
c-beats-100-100-using-dijksta-and-dfs-wi-s3dn
\nclass Solution {\npublic:\n// Declaring variables gloabally so that we don\'t need to pass every time in function and we can directly access in dfs function\n
18bce192
NORMAL
2021-03-11T17:05:49.376725+00:00
2021-03-27T16:59:26.168668+00:00
338
false
```\nclass Solution {\npublic:\n// Declaring variables gloabally so that we don\'t need to pass every time in function and we can directly access in dfs function\n unordered_map <int,vector <pair<int,int>>> m;\n vector <int> dist;\n vector <int> dp;\n int N;\n int MOD=1000000007;\n int countRestricted...
2
1
['Dynamic Programming', 'Ordered Set', 'C++']
2
number-of-restricted-paths-from-first-to-last-node
[Python] Dijikstra(early stop) + graph DP, 1660ms
python-dijikstraearly-stop-graph-dp-1660-xg6w
Dijikstra to find the shortest distance from end node(n) to all other nodes. Early stop when node 1 is met. Since the nodes left unvisited will have larger dist
18768897529b
NORMAL
2021-03-07T04:52:05.872206+00:00
2021-03-07T04:52:05.872242+00:00
82
false
Dijikstra to find the shortest distance from end node(n) to all other nodes. Early stop when node 1 is met. Since the nodes left unvisited will have larger distance to the end node. They won\'t be used to construct restricted paths.\n\nThen graph DP idea is that traversing from closer (to end node) nodes to further nod...
2
1
[]
0
number-of-restricted-paths-from-first-to-last-node
Bellman-Ford (TLE) and SPFA (AC)
bellman-ford-tle-and-spfa-ac-by-claytonj-2mn1
Synopsis:\n\nBellman-Ford results in TLE. SPFA is a natural progression from Bellman-Ford which only considers each next round of edge relaxations to be perfor
claytonjwong
NORMAL
2021-03-07T04:00:23.912904+00:00
2021-03-19T21:41:28.545913+00:00
367
false
**Synopsis:**\n\n[Bellman-Ford](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm) results in TLE. [SPFA](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm) is a natural progression from [Bellman-Ford](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm) which only considers each next ro...
2
0
[]
1
number-of-restricted-paths-from-first-to-last-node
C++ | Dijkstra's + DFS + Memoization/DP | Easy to Understand
c-dijkstras-dfs-memoizationdp-easy-to-un-vdat
Intuition use Dijkstra's Algorithm to find the minimum distance from node n to all the other nodes after calculating the minimum distances traverse the graph st
ghozt777
NORMAL
2025-04-09T19:58:22.226693+00:00
2025-04-09T19:59:43.998363+00:00
12
false
# Intuition - use [Dijkstra's Algorithm](https://cp-algorithms.com/graph/dijkstra.html) to find the minimum distance from node `n` to all the other nodes - after calculating the minimum distances traverse the graph starting from node `1` to node `n` - the only catch is that if you are currently on node `u` and want to ...
1
0
['Dynamic Programming', 'Graph', 'Memoization', 'C++']
0
number-of-restricted-paths-from-first-to-last-node
Very Easy C++ Problem: Just Needed One Night to Understand
very-easy-c-problem-just-needed-one-nigh-5583
Code
Yash_Bhoomkar
NORMAL
2025-02-18T03:48:08.044822+00:00
2025-02-18T03:48:08.044822+00:00
80
false
# Code ```cpp [] #define P pair<int,int> const int MOD = 1e9+7; class Solution { vector<vector<int>> paths; vector<int> dist; int dfs(vector<vector<P>>& adj, int current_node, vector<int>& visited, int& n, vector<int>& current_path , vector<int>& dp) { // visited[current_node] = 1; i...
1
0
['Dynamic Programming', 'Graph', 'Shortest Path', 'C++']
0
number-of-restricted-paths-from-first-to-last-node
DYNAMIC PROGRAMMING + DIJKSTRA ALGORITHM
dynamic-programming-dijkstra-algorithm-b-ak0w
just find the minimum distance from nth node to all other nodes and the do a dfs with memoization to count all the restricted partsComplexity Time complexity:O(
samman_varshney
NORMAL
2025-02-10T14:36:41.798297+00:00
2025-02-10T14:36:41.798297+00:00
64
false
just find the minimum distance from nth node to all other nodes and the do a dfs with memoization to count all the restricted parts # Complexity - Time complexity:O(ElogV) <!-- Add your time complexity here, e.g. $$O(n)$$ --> - Space complexity:O(E+V) <!-- Add your space complexity here, e.g. $$O(n)$$ --> # Code ```...
1
0
['Dynamic Programming', 'Graph', 'Heap (Priority Queue)', 'Shortest Path', 'Java']
0
number-of-restricted-paths-from-first-to-last-node
O(eloge) Time Simple Dijkstra Solution || Commented Code
oeloge-time-simple-dijkstra-solution-com-b4lg
Code
ammar-a-khan
NORMAL
2025-01-25T11:06:33.277075+00:00
2025-01-25T11:06:33.277075+00:00
82
false
# Code ```cpp int helper(int node, vector<vector<pair<int, int>>> &g, vector<int> &dist, vector<int> &dp){ //simply travels from 1->n cuz dist constraint makes the graph a DAG if (node == g.size() - 1){ return 1; } if (dp[node] == -1){ //compute if not done already int sum = 0; for (int i = 0; ...
1
0
['Dynamic Programming', 'Graph', 'Topological Sort', 'Heap (Priority Queue)', 'Shortest Path', 'C++']
0
number-of-restricted-paths-from-first-to-last-node
DijKstra + DP in C++
dijkstra-dp-in-c-by-wait_watch-4ftm
\n#define pii pair<long,int>\n# define mod 1000000007\nclass Solution {\npublic:\n \n long DFS(int x, int n,vector<long>&dp , vector<long>&dis, vector<pa
wait_watch
NORMAL
2024-07-20T11:01:14.027543+00:00
2024-07-20T11:01:14.027576+00:00
3
false
```\n#define pii pair<long,int>\n# define mod 1000000007\nclass Solution {\npublic:\n \n long DFS(int x, int n,vector<long>&dp , vector<long>&dis, vector<pair<int,long>>*g){\n if(x==n)\n return 1;\n else if(dp[x]!=-1)\n return dp[x];\n long ans =0;\n for(int i=0;...
1
0
[]
0
number-of-restricted-paths-from-first-to-last-node
clean short code using heap in python
clean-short-code-using-heap-in-python-by-n5wd
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
Akuma-Greyy
NORMAL
2024-06-20T11:32:25.899915+00:00
2024-06-20T11:32:25.899938+00:00
210
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
1
0
['Dynamic Programming', 'Graph', 'Topological Sort', 'Heap (Priority Queue)', 'Shortest Path', 'Python3']
0
number-of-restricted-paths-from-first-to-last-node
BFS + Dijkstra + Topological Sort Java Solution
bfs-dijkstra-topological-sort-java-solut-1vjb
Intuition\nAll the restricted path belongs to a DAG, topological sort is a straightforward solution. Calculating the number of paths at the final step requires
ericwangirvine
NORMAL
2024-03-06T01:27:53.730376+00:00
2024-03-06T01:27:53.730429+00:00
92
false
# Intuition\nAll the restricted path belongs to a DAG, topological sort is a straightforward solution. Calculating the number of paths at the final step requires a bit of thinking, topological sort helps in this case since before moving to the next node, it naturally waits for all the incoming edges to finish counting....
1
0
['Java']
0
number-of-restricted-paths-from-first-to-last-node
Dijkstra+DFS+Memoization beats 100% python3
dijkstradfsmemoization-beats-100-python3-kfho
Explanation of the Soultion: \nWe need to find the number of paths between 1st to nth node in which the shortest path from the end node to each of the nodes of
jaidev2
NORMAL
2023-12-24T09:01:08.761373+00:00
2023-12-24T09:01:08.761398+00:00
3
false
Explanation of the Soultion: \nWe need to find the number of paths between 1st to nth node in which the **shortest path from the end node** to each of the nodes of the path is in **strictly decreasing order**, for an example\nlets say there exist a path where n=5: 1->3->5, now lets say shortest path from 1 to 5 is 3 an...
1
0
['Depth-First Search', 'Memoization']
0
number-of-restricted-paths-from-first-to-last-node
Easy C++ Solution
easy-c-solution-by-astha_jj-hghw
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
astha_jj
NORMAL
2023-10-28T11:39:17.160422+00:00
2023-10-28T11:39:17.160441+00:00
109
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:O(N){dfs}+O(V+E){djksta}\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(N)\n<!-- Add your space compl...
1
0
['Dynamic Programming', 'Depth-First Search', 'Shortest Path', 'C++']
0
number-of-restricted-paths-from-first-to-last-node
Dijkstra + DFS with DP || C++ || explained
dijkstra-dfs-with-dp-c-explained-by-avi_-bpzg
First find the minimum distance to each node from the last node. Using dijkstra algorithm\n- Using DFS go to each path and check if dist[adjnode] < dist[node]\n
avi_1344
NORMAL
2023-08-23T17:16:03.727464+00:00
2023-08-23T17:16:03.727494+00:00
62
false
- First find the minimum distance to each node from the last node. Using dijkstra algorithm\n- Using DFS go to each path and check if ```dist[adjnode] < dist[node]```\n# Code\n```\nclass Solution {\npublic:\n int mod = 1e9 + 7;\n vector<int> dijkstra(int n, vector<vector<pair<int,int>>> &adj){\n vector<int...
1
0
['Dynamic Programming', 'Depth-First Search', 'Graph', 'Shortest Path', 'C++']
0
number-of-restricted-paths-from-first-to-last-node
DP + Dijkstra + DFS = SOLUTION
dp-dijkstra-dfs-solution-by-whoisslimsha-cek5
\n\n# Code\n\nclass Solution {\npublic:\n int mod = 1e9+7;\n // simple dijkstra to find shortest distance from node n \n vector<int> dijkstra(vector<ve
whoisslimshady
NORMAL
2023-08-10T08:20:30.903995+00:00
2023-08-10T08:20:30.904014+00:00
9
false
\n\n# Code\n```\nclass Solution {\npublic:\n int mod = 1e9+7;\n // simple dijkstra to find shortest distance from node n \n vector<int> dijkstra(vector<vector<pair<int,int>>>&adj, int n){\n\t priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int>>> pq;\n vector<int> distTo(n+1,INT_...
1
1
['Dynamic Programming', 'Graph', 'C++']
1
number-of-restricted-paths-from-first-to-last-node
Dijkstra + DFS approach | Java solution | Clean Code
dijkstra-dfs-approach-java-solution-clea-fz1y
Complexity\nLet v = number of nodes\ne = number of adges\n- Time complexity: $O((v + e)\log v)$\n Add your time complexity here, e.g. O(n) \n\n- Space complexit
vrutik2809
NORMAL
2023-04-19T19:39:36.696300+00:00
2023-04-19T19:39:36.696340+00:00
134
false
# Complexity\nLet `v = number of nodes`\n`e = number of adges`\n- Time complexity: $O((v + e)\\log v)$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $O(v)$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```java\nclass Solution {\n List<List<int[]>> adj;\n int va...
1
0
['Dynamic Programming', 'Depth-First Search', 'Java']
0
number-of-restricted-paths-from-first-to-last-node
Easy Video Walkthrough
easy-video-walkthrough-by-bnchn-0w33
Video Walkthrough\n\nclass Solution:\n def countRestrictedPaths(self, n: int, edges: List[List[int]]) -> int:\n G = [[] for _ in range(n+1)]\n
bnchn
NORMAL
2022-11-02T16:59:29.313718+00:00
2022-11-02T16:59:29.313765+00:00
99
false
[Video Walkthrough](https://youtu.be/hXrcIbzaVHQ)\n```\nclass Solution:\n def countRestrictedPaths(self, n: int, edges: List[List[int]]) -> int:\n G = [[] for _ in range(n+1)]\n SRC = n\n pq = [(0,SRC)]\n \n for u,v,w in edges: G[u].append((v,w)), G[v].append((u,w))\n distTo...
1
0
['Python']
0
number-of-restricted-paths-from-first-to-last-node
Dijkstra and memo, java solution, beats 99%
dijkstra-and-memo-java-solution-beats-99-acro
\nclass Solution {\n Integer[]memo;\n static final int INF = Integer.MAX_VALUE;\n static final int MOD = (int)(1E9) + 7;\n public int countRestricte
yasmine-ya
NORMAL
2022-09-28T23:06:42.699579+00:00
2022-09-28T23:51:16.437662+00:00
336
false
```\nclass Solution {\n Integer[]memo;\n static final int INF = Integer.MAX_VALUE;\n static final int MOD = (int)(1E9) + 7;\n public int countRestrictedPaths(int n, int[][] edges) {\n List<int[]>[]graph = new ArrayList[n];\n memo = new Integer[n];\n int[]dis = new int[n];\n for(i...
1
0
['Memoization']
0
number-of-restricted-paths-from-first-to-last-node
Elegant. Dijktra's Algorithm and Dynamic Programming
elegant-dijktras-algorithm-and-dynamic-p-xfbm
\nimport math\nfrom heapq import heapify, heappop as hpop, heappush as hpush\nclass Solution:\n # O((|E| + |V|) * log(V)) Time and O(V + E) Space\n def co
kunal5042
NORMAL
2022-08-13T19:00:19.517363+00:00
2022-08-13T19:00:19.517393+00:00
105
false
```\nimport math\nfrom heapq import heapify, heappop as hpop, heappush as hpush\nclass Solution:\n # O((|E| + |V|) * log(V)) Time and O(V + E) Space\n def countRestrictedPaths(self, n: int, edges: List[List[int]]) -> int:\n mod = (10**9) + 7\n graph = [set() for _ in range(n+1)]\n distances =...
1
0
['Dynamic Programming', 'Python3']
0
number-of-restricted-paths-from-first-to-last-node
Fast Easy JS Solution | dijkstras + dfs + dp | 70% faster
fast-easy-js-solution-dijkstras-dfs-dp-7-slcg
\nvar countRestrictedPaths = function(n, edges) {\n const g = Array.from({ length: n + 1}, () => []);\n for(let [a, b, c] of edges) {\n g[a].push([
nontoxic
NORMAL
2022-06-19T05:42:31.756780+00:00
2022-06-19T05:42:31.756831+00:00
127
false
```\nvar countRestrictedPaths = function(n, edges) {\n const g = Array.from({ length: n + 1}, () => []);\n for(let [a, b, c] of edges) {\n g[a].push([b, c]);\n g[b].push([a, c]);\n }\n // do dijkstras to find shortest path from n to all nodes\n const dis = new Array(n + 1).fill(Infinity);\n...
1
0
['JavaScript']
1
number-of-restricted-paths-from-first-to-last-node
[C++] Dijkstra + DP + DFS
c-dijkstra-dp-dfs-by-makhonya-9tix
\nclass Solution {\npublic:\n int MOD = 1e9 + 7;\n unordered_map<int, vector<pair<int, int>>> hash;\n int countRestrictedPaths(int n, vector<vector<int
makhonya
NORMAL
2022-06-08T02:58:31.989359+00:00
2022-06-08T02:58:31.989395+00:00
219
false
```\nclass Solution {\npublic:\n int MOD = 1e9 + 7;\n unordered_map<int, vector<pair<int, int>>> hash;\n int countRestrictedPaths(int n, vector<vector<int>>& e) {\n for(auto& a: e){\n hash[a[0]].push_back({a[1], a[2]});\n hash[a[1]].push_back({a[0], a[2]});\n }\n vect...
1
0
['Dynamic Programming', 'Depth-First Search', 'C', 'C++']
0
number-of-restricted-paths-from-first-to-last-node
C++ Solution { Dijkstra + DFS}
c-solution-dijkstra-dfs-by-_mrvariable-2dyw
\nclass Solution {\npublic:\n int mod = 1e9 + 7;\n int dfs(int node, vector<int> &dist, vector<vector<pair<int, int>>> &graph, vector<int> &dp) {\n
_MrVariable
NORMAL
2022-06-03T12:50:42.386504+00:00
2022-06-03T12:50:42.386544+00:00
311
false
```\nclass Solution {\npublic:\n int mod = 1e9 + 7;\n int dfs(int node, vector<int> &dist, vector<vector<pair<int, int>>> &graph, vector<int> &dp) {\n if(node == dp.size()-1) return 1;\n if(dp[node] != -1) return dp[node];\n int ans = 0;\n for(auto nbr : graph[node]) {\n if(...
1
0
['Depth-First Search', 'C']
0
number-of-restricted-paths-from-first-to-last-node
C++ BACKTRACKING DP RECURSIVE MEMOIZATION
c-backtracking-dp-recursive-memoization-lsulz
```\n//classical src to destination dp using backtracking then mempoize it\nclass Solution {\npublic:\n int mod = 1000000007;\n vector>> g;\n vector di
njcoder
NORMAL
2022-02-02T11:58:11.997778+00:00
2022-02-02T11:58:11.997809+00:00
122
false
```\n//classical src to destination dp using backtracking then mempoize it\nclass Solution {\npublic:\n int mod = 1000000007;\n vector<vector<pair<int,int>>> g;\n vector<int> dis;\n vector<bool> vis;\n vector<int> dp;\n int fun(int src,int des){\n if(src == des) return 1;\n // vis[src...
1
0
['Dynamic Programming', 'Backtracking', 'Depth-First Search']
0
number-of-restricted-paths-from-first-to-last-node
C++ | Djikstra + DFS + Memoization | Recursive and Iterative Solutions
c-djikstra-dfs-memoization-recursive-and-q244
Method 1 - Djikstra + Recursive DFS \n\n#define MOD 1000000007\nclass Solution\n{\n public:\n int dfs(unordered_map<int,vector<pair<int,int>>>& graph, int
mhdareeb
NORMAL
2022-01-31T07:13:09.890987+00:00
2022-01-31T07:13:09.891035+00:00
187
false
Method 1 - Djikstra + Recursive DFS \n```\n#define MOD 1000000007\nclass Solution\n{\n public:\n int dfs(unordered_map<int,vector<pair<int,int>>>& graph, int u, int n, vector<long long int>& distance, vector<int>& paths)\n {\n if(paths[u]==-1)\n {\n paths[u]=0;\n for(auto p:...
1
0
['Depth-First Search', 'Memoization']
0
number-of-restricted-paths-from-first-to-last-node
C++ Short Solution
c-short-solution-by-electronaota-v8jn
\n#define MOD 1000000007\n#define INF INT_MAX\nclass Solution {\npublic:\n using pii = pair<int, int>;\n int countRestrictedPaths(int n, vector<vector<int>>&
Electronaota
NORMAL
2021-12-31T08:13:15.856534+00:00
2021-12-31T08:14:19.107558+00:00
234
false
```\n#define MOD 1000000007\n#define INF INT_MAX\nclass Solution {\npublic:\n using pii = pair<int, int>;\n int countRestrictedPaths(int n, vector<vector<int>>& edges) {\n vector<int> dist(n, INF); dist[n - 1] = 0;\n priority_queue<pii, vector<pii>, greater<pii>> pq; pq.push({0, n - 1});\n vector<vector<pii>...
1
0
['C']
0
number-of-restricted-paths-from-first-to-last-node
C++ || DIJKSTRA || DFS + MEMOIZATION || MOST EASY || NEAT || CODE ✅
c-dijkstra-dfs-memoization-most-easy-nea-uk1z
\n#define ll long long\n\nclass Solution {\npublic:\n \n int mod = 1e9+7;\n \n \n // step1> Reverse Dijkstra\n // step2> dfs + memo\n \n
tbne1905
NORMAL
2021-11-15T11:42:16.459751+00:00
2021-11-15T11:47:50.166472+00:00
140
false
```\n#define ll long long\n\nclass Solution {\npublic:\n \n int mod = 1e9+7;\n \n \n // step1> Reverse Dijkstra\n // step2> dfs + memo\n \n int n;\n vector < vector < pair <int,ll> > > adj;\n \n \n //-------STEP 1: Find the minimum distance from all nodes to node \'n\' using Dijkstra----...
1
0
[]
0
number-of-restricted-paths-from-first-to-last-node
Shortest Path + DFS || Djikstra + DFS + DP || C++ Clean Code
shortest-path-dfs-djikstra-dfs-dp-c-clea-ltse
Idea here is to first calculate Path with Minimum Cost from source = n i.e last node. For this we will use Djikstra\'s Algo. \n\n Now, that we have created dist
i_quasar
NORMAL
2021-10-26T01:35:57.406741+00:00
2021-10-26T01:35:57.406784+00:00
208
false
Idea here is to first calculate Path with Minimum Cost from `source = n` i.e last node. For this we will use Djikstra\'s Algo. \n\n* Now, that we have created distance array, such that `dist[x] = distanceToLastNode(x)`\n* Then, we need to find paths from 1 to n, such that \n\n\t\t distanceToLastNode(node) > distanceToL...
1
0
['Dynamic Programming', 'Depth-First Search', 'Graph', 'C', 'Shortest Path']
0
number-of-restricted-paths-from-first-to-last-node
Using Djikstra's To find the shortest paths and then conditional DFS to reach 1.
using-djikstras-to-find-the-shortest-pat-7x6m
If you find it Helpful, Do upvote! Thanks! Cheers. This is a good one.\n\nclass Solution {\npublic: \n \n /*\n This is a typical dp on graphs probl
SahilAnower
NORMAL
2021-10-17T13:50:36.914974+00:00
2021-10-17T13:50:36.915009+00:00
95
false
If you find it Helpful, Do upvote! Thanks! Cheers. This is a good one.\n```\nclass Solution {\npublic: \n \n /*\n This is a typical dp on graphs problem where we first find out the shortest routes from n to every other node, then we perform dfs on\n graph starting from node n,such that we go to next...
1
0
[]
0
number-of-restricted-paths-from-first-to-last-node
[Python3] | Dijkstra+Memoization
python3-dijkstramemoization-by-swapnilsi-ifum
Solution - We use Dijkstra algo to find shortest distance from end node to all other nodes. Then we dfs from node 1 and traverse to its neighbours by applying
swapnilsingh421
NORMAL
2021-09-30T14:44:43.475333+00:00
2021-09-30T15:42:27.746554+00:00
92
false
Solution - We use Dijkstra algo to find shortest distance from end node to all other nodes. Then we dfs from node 1 and traverse to its neighbours by applying condition dist[src]>dist[neigh] and when we reach node=n we return 1 means we have found 1 path and we will keep memorizing the result in intermediate process.\...
1
0
[]
0
number-of-restricted-paths-from-first-to-last-node
C++ Dijkstra + DP Solution
c-dijkstra-dp-solution-by-ahsan83-zaca
Runtime: 664 ms, faster than 26.59% of C++ online submissions for Number of Restricted Paths From First to Last Node.\nMemory Usage: 199 MB, less than 8.35% of
ahsan83
NORMAL
2021-08-25T17:35:06.071965+00:00
2021-08-25T17:36:51.814915+00:00
244
false
Runtime: 664 ms, faster than 26.59% of C++ online submissions for Number of Restricted Paths From First to Last Node.\nMemory Usage: 199 MB, less than 8.35% of C++ online submissions for Number of Restricted Paths From First to Last Node.\n\nNote : Solution taken from other post.\n\n```\nHere we have to find the restri...
1
0
['Dynamic Programming', 'Graph', 'C']
0
number-of-restricted-paths-from-first-to-last-node
[C++] [Dijkstra + DFS + DP (Top Down)] [O(ElogE) Time O(E) Space]
c-dijkstra-dfs-dp-top-down-oeloge-time-o-qu5a
```\nclass Solution {\n public:\n int mod = 1e9 + 7;\n int countRestrictedPaths(int n, vector>& edges) {\n vector distanceToLastNode(n + 1, INT_MAX);\n
amanjainnn
NORMAL
2021-07-23T21:24:12.674069+00:00
2021-07-23T21:24:12.674111+00:00
179
false
```\nclass Solution {\n public:\n int mod = 1e9 + 7;\n int countRestrictedPaths(int n, vector<vector<int>>& edges) {\n vector<int> distanceToLastNode(n + 1, INT_MAX);\n vector<pair<int, int>> graph[n + 1];\n for (vector<int>& edge : edges) {\n graph[edge[0]].push_back({edge[1], edge[2]});\n graph[e...
1
0
[]
0
number-of-restricted-paths-from-first-to-last-node
Dijkstra + DP
dijkstra-dp-by-drashtikoladiya-peep
C++ Solution:\nTime Complexity: O(ElogE)\nSpace complexity: O(V+E)\n\nclass Solution {\npublic:\n vector<vector<pair<int,int>>> adj1;\n vector<vector<int>
drashtikoladiya
NORMAL
2021-06-24T17:35:24.377122+00:00
2021-06-24T17:38:15.823720+00:00
393
false
**C++ Solution:**\n**Time Complexity: O(ElogE)**\n**Space complexity: O(V+E)**\n```\nclass Solution {\npublic:\n vector<vector<pair<int,int>>> adj1;\n vector<vector<int>> adj2;\n vector<int> dis,vis,dp;\n priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> q;\n int mod=1e9+7;\n ...
1
0
['Dynamic Programming', 'Depth-First Search', 'Memoization', 'C', 'Shortest Path']
0
number-of-restricted-paths-from-first-to-last-node
[Java] Dijkstra + DFS Solution
java-dijkstra-dfs-solution-by-maddetecti-px22
\n// Dijkstra + DFS Solution\n// Dijkstra Algorithm to the shortest distances from n to other nodes.\n// Then DFS to explore the restricted pathes.\n// Time com
maddetective
NORMAL
2021-06-24T16:26:19.571655+00:00
2021-08-25T04:23:27.348595+00:00
468
false
```\n// Dijkstra + DFS Solution\n// Dijkstra Algorithm to the shortest distances from n to other nodes.\n// Then DFS to explore the restricted pathes.\n// Time complexity: O(ElogN + N)\n// Space complexity: O(N + E)\nclass Solution {\n private static final int MOD = (int) 1E9 + 7;\n public int countRestrictedPath...
1
0
['Java']
0
number-of-restricted-paths-from-first-to-last-node
C++ | Djikstra + DFS + Memorization |
c-djikstra-dfs-memorization-by-saumiasin-t7nf
\nclass Solution {\n vector<vector<pair<int, int>>> graph;\n vector<int> distance;\n vector<int> memo;\n int N;\n\n // O(E)\n int dfs(int u) {
saumiasinghal
NORMAL
2021-06-12T09:17:42.626079+00:00
2021-06-12T09:17:42.626210+00:00
132
false
```\nclass Solution {\n vector<vector<pair<int, int>>> graph;\n vector<int> distance;\n vector<int> memo;\n int N;\n\n // O(E)\n int dfs(int u) {\n if (memo[u] != -1) return memo[u];\n if (u == N) return 1;\n int ans = 0;\n\n for (auto e : graph[u]) {\n int v = e...
1
0
[]
0
number-of-restricted-paths-from-first-to-last-node
Java readable version with summary from other solutions
java-readable-version-with-summary-from-fg3zw
\n it was hard question to me\n I tried to made it simpler from @kniffina solution \n \n key 3 things I could not figure out
youngsam
NORMAL
2021-05-03T09:52:17.331994+00:00
2021-05-03T09:52:17.332025+00:00
146
false
\n it was hard question to me\n I tried to made it simpler from @kniffina solution \n \n key 3 things I could not figure out\n after construct a graph\n \n 1. BFS to find distance from N to 1 **(NOT 1 to N) ** \n **because of #2 condtion**\n\t\t\n ...
1
0
[]
0
number-of-restricted-paths-from-first-to-last-node
JAVA solution shows Memory Limit Exceeded
java-solution-shows-memory-limit-exceede-gpbo
\nclass Pair implements Comparable<Pair>\n{\n int len,ele;\n Pair(int len,int ele)\n {\n this.len=len;\n this.ele=ele;\n }\n public
ayushyadav685
NORMAL
2021-05-01T12:22:40.249142+00:00
2021-05-01T12:27:02.440585+00:00
112
false
```\nclass Pair implements Comparable<Pair>\n{\n int len,ele;\n Pair(int len,int ele)\n {\n this.len=len;\n this.ele=ele;\n }\n public int compareTo(Pair p)\n {\n return this.len-p.len;\n }\n}\nclass Solution {\n public int countRestrictedPaths(int n, int[][] edges) {\n ...
1
0
[]
0
number-of-restricted-paths-from-first-to-last-node
Last Testcase TLE
last-testcase-tle-by-xxsidxx-p0wn
\n\nclass Solution {\n int mod=(int)1e9+7;\n class Pair{\n int vrtx;\n int weight;\n Pair(int vrtx,in
xxsidxx
NORMAL
2021-03-19T18:36:44.022777+00:00
2021-03-19T18:36:44.022807+00:00
123
false
```\n\nclass Solution {\n int mod=(int)1e9+7;\n class Pair{\n int vrtx;\n int weight;\n Pair(int vrtx,int weight){\n this.vrtx=vrtx;\n this.weight=weight;\n }\n }\n public int countRestricte...
1
0
[]
1
number-of-restricted-paths-from-first-to-last-node
Simple Dijkstra and dfs with memoization(c++)
simple-dijkstra-and-dfs-with-memoization-ht3x
\n#define F first\n#define S second\nclass Solution {\npublic:\n int dfs( vector<vector<pair<int,int>>> &adj, vector<int> &dis,int node,int pre_dis, vector<
royalshashanks
NORMAL
2021-03-16T09:38:22.332064+00:00
2021-03-16T09:38:22.332106+00:00
125
false
```\n#define F first\n#define S second\nclass Solution {\npublic:\n int dfs( vector<vector<pair<int,int>>> &adj, vector<int> &dis,int node,int pre_dis, vector<int> &dp)\n {\n int mod = 1000000007;\n if(node == adj.size()-1){\n return 1;\n }\n if(pre_dis <= dis[node])return 0;\n if(dp[node...
1
0
[]
0
number-of-restricted-paths-from-first-to-last-node
java dijkstra+dp beats 100% easy to understand
java-dijkstradp-beats-100-easy-to-unders-2dop
\nclass Solution {\n public int countRestrictedPaths(int n, int[][] edges) {\n List<int[]>[] graph=new List[n+1];\n for(int i=0;i<graph.length;
scott_bunny
NORMAL
2021-03-15T11:10:48.657686+00:00
2021-03-15T11:10:48.657725+00:00
177
false
```\nclass Solution {\n public int countRestrictedPaths(int n, int[][] edges) {\n List<int[]>[] graph=new List[n+1];\n for(int i=0;i<graph.length;i++)graph[i]=new ArrayList<>();\n for(int[] edge:edges)\n {\n graph[edge[0]].add(new int[]{edge[1],edge[2]});\n graph[edg...
1
0
[]
0
number-of-restricted-paths-from-first-to-last-node
[Ruby] Dijkstra + sorting by distance and iterating nodes
ruby-dijkstra-sorting-by-distance-and-it-i66w
\nruby\ndef dijkstra(n, edge_list, init)\n verts = *0...n\n dist = Array.new(n, Float::INFINITY)\n dist[init] = 0\n prev = Array.new(n, nil)\n adj = Hash.n
shhavel
NORMAL
2021-03-09T19:16:59.319978+00:00
2021-03-09T19:16:59.320044+00:00
102
false
\n```ruby\ndef dijkstra(n, edge_list, init)\n verts = *0...n\n dist = Array.new(n, Float::INFINITY)\n dist[init] = 0\n prev = Array.new(n, nil)\n adj = Hash.new { |adj, v| adj[v] = [] }\n edge_list.each do |u, v, w|\n adj[u] << [v, w]\n adj[v] << [u, w]\n end\n pq = [[0, init]] # priority queue\n visited...
1
0
['Ruby']
0
number-of-restricted-paths-from-first-to-last-node
Dijikstra and BFS C++
dijikstra-and-bfs-c-by-haoel-43mh
\n\n\nclass Solution {\npublic:\n int countRestrictedPaths(int n, vector<vector<int>>& edges) {\n \n // construct the graph map\n vector
haoel
NORMAL
2021-03-08T01:42:22.091189+00:00
2021-03-08T01:44:25.937704+00:00
100
false
\n\n```\nclass Solution {\npublic:\n int countRestrictedPaths(int n, vector<vector<int>>& edges) {\n \n // construct the graph map\n vector<unordered_map<int, int>> graph(n);\n for(auto e : edges) {\n int x = e[0]-1;\n int y = e[1]-1;\n int d = e[2];\n ...
1
0
[]
1
number-of-restricted-paths-from-first-to-last-node
Java Dijkstra + DFS with memo
java-dijkstra-dfs-with-memo-by-chipn13-kfuz
```\nclass Item {\n int node;\n int max;\n}\n\nclass Solution {\n private long response = 0;\n \n public int countRestrictedPaths(int n, int[][]
chipn13
NORMAL
2021-03-07T19:25:05.672559+00:00
2021-03-07T19:25:36.585909+00:00
252
false
```\nclass Item {\n int node;\n int max;\n}\n\nclass Solution {\n private long response = 0;\n \n public int countRestrictedPaths(int n, int[][] edges) {\n int[] distance = new int[n + 1];\n int[] visited = new int[n + 1];\n \n PriorityQueue<int[]> heap = new PriorityQueue<>(...
1
0
['Depth-First Search', 'Memoization', 'Java']
0
number-of-restricted-paths-from-first-to-last-node
C++ implementation: Dijkstra + dfs with memorization
c-implementation-dijkstra-dfs-with-memor-trg3
The algo is divided in two parts:\n1. Finding path using Dikstra algorithm: shortest path from last node (n) to all other nodes.\n2. Applying dfs to find the re
a1exrebys
NORMAL
2021-03-07T18:34:17.019535+00:00
2021-03-07T18:34:17.019584+00:00
124
false
The algo is divided in two parts:\n1. Finding path using Dikstra algorithm: shortest path from last node (n) to all other nodes.\n2. Applying dfs to find the restricted path: our task property mentioned in the description.\n\n```\nclass Solution {\npublic:\n using ip = pair<int, int>;\n const int mod = 1e9 + 7;\n...
1
0
['Depth-First Search', 'Memoization', 'C']
0
number-of-restricted-paths-from-first-to-last-node
[Python3] Memoized Dijkstra + Memoized DFS Beats 100%
python3-memoized-dijkstra-memoized-dfs-b-vgf8
\nclass Solution:\n def countRestrictedPaths(self, n: int, edges: List[List[int]]) -> int:\n mod = 10 ** 9 + 7\n dist = [float(\'inf\')] * (n +
blackspinner
NORMAL
2021-03-07T17:38:56.446485+00:00
2021-03-07T17:38:56.446541+00:00
65
false
```\nclass Solution:\n def countRestrictedPaths(self, n: int, edges: List[List[int]]) -> int:\n mod = 10 ** 9 + 7\n dist = [float(\'inf\')] * (n + 1)\n graph = defaultdict(list)\n def dijkstra():\n pq = [(0, n)]\n dist[n] = 0\n while pq:\n w...
1
0
[]
0
number-of-restricted-paths-from-first-to-last-node
C++ Dijkstra and DFS DP
c-dijkstra-and-dfs-dp-by-varkey98-7d3i
First we run dijkstra from n to 1 so that we can find shortest distance from n to all nodes. Since, it is an undirected graph, the shortest distance is same for
varkey98
NORMAL
2021-03-07T17:31:50.174768+00:00
2021-03-07T17:34:35.486332+00:00
144
false
First we run dijkstra from n to 1 so that we can find shortest distance from n to all nodes. Since, it is an undirected graph, the shortest distance is same for reverse path also. Then we do a DFS DP for findin the path according to the given constraints.\n\n```\nstruct fun\n{\n\tbool operator() (pair<int,int>& p1, pai...
1
0
['Dynamic Programming', 'C']
0
number-of-restricted-paths-from-first-to-last-node
Share My Java Dijkstra + DFS (w/ cache) Solution
share-my-java-dijkstra-dfs-w-cache-solut-vm6o
It took me a long time to understand the question...\n\n\n// Dijkstra + DFS with cache\npublic int countRestrictedPaths(int n, int[][] edges) {\n\tif (n <= 0 ||
violinviolin
NORMAL
2021-03-07T06:12:21.290388+00:00
2021-03-07T06:12:21.290445+00:00
70
false
It took me a long time to understand the question...\n\n```\n// Dijkstra + DFS with cache\npublic int countRestrictedPaths(int n, int[][] edges) {\n\tif (n <= 0 || edges == null || edges.length == 0) return 0;\n\n\t// step 1. build graph\n\tMap<Integer, Map<Integer, Integer>> map = new HashMap<>(); // from -> {to -> w...
1
0
[]
0
number-of-restricted-paths-from-first-to-last-node
C++ dijastra + cached dfs 100% time/space
c-dijastra-cached-dfs-100-timespace-by-h-s9a8
\n/*\n First, find out the shortest ditance between node x to node n.\n DFS to find restricted path.\n*/\ntypedef pair<long,int> pii;\nclass Solution {\np
halleywang
NORMAL
2021-03-07T05:40:11.591127+00:00
2021-03-07T05:40:11.591159+00:00
117
false
```\n/*\n First, find out the shortest ditance between node x to node n.\n DFS to find restricted path.\n*/\ntypedef pair<long,int> pii;\nclass Solution {\npublic:\n int countRestrictedPaths(int n, vector<vector<int>>& edges) \n {\n vector<vector<pii>> graph(n+1,vector<pii>{});\n vector<long> ...
1
0
[]
0
number-of-restricted-paths-from-first-to-last-node
Python - Dijstra and DP DFS (No @lru_cache)
python-dijstra-and-dp-dfs-no-lru_cache-b-gbnm
python\nfrom heapq import heappush, heappop\n\nclass Solution:\n def countRestrictedPaths(self, n: int, edges: List[List[int]]) -> int:\n graph = { i:
nuclearoreo
NORMAL
2021-03-07T05:02:12.423099+00:00
2021-03-07T16:17:18.125150+00:00
68
false
```python\nfrom heapq import heappush, heappop\n\nclass Solution:\n def countRestrictedPaths(self, n: int, edges: List[List[int]]) -> int:\n graph = { i: [] for i in range(1, n + 1) }\n \n for u, v, w in edges:\n graph[u].append((v, w))\n graph[v].append((u, w))\n ...
1
1
[]
0
number-of-restricted-paths-from-first-to-last-node
c++ djkstra readable code
c-djkstra-readable-code-by-cxky-1pg2
\nclass Solution {\npublic:\n int mod = 1e9 + 7;\n int dfs(int n, vector<int> &dp, vector<int> &dist, vector<vector<pair<int,int>>> &graph, int d) {\n
cxky
NORMAL
2021-03-07T04:41:10.651319+00:00
2021-03-07T04:44:20.695720+00:00
91
false
```\nclass Solution {\npublic:\n int mod = 1e9 + 7;\n int dfs(int n, vector<int> &dp, vector<int> &dist, vector<vector<pair<int,int>>> &graph, int d) {\n if(n == 1) \n return 1;\n long ans = 0;\n if(dp[n] != -1) \n return dp[n];\n\n for(auto [i, w]: graph[n]) {\n ...
1
0
['C']
0
number-of-restricted-paths-from-first-to-last-node
Dijkstra's shortest paths for each node, then DFS + Memo, O(V + E * log V)
dijkstras-shortest-paths-for-each-node-t-awch
find each node shortest distance to node n;\n2. dfs from n to 1, + memoization, to get total number of paths;\n\nSortedList, O(log n) remove.\n\ntime: O(V + E *
goodgoodwish
NORMAL
2021-03-07T04:39:08.267497+00:00
2021-03-07T04:47:53.801842+00:00
238
false
1. find each node shortest distance to node n;\n2. dfs from n to 1, + memoization, to get total number of paths;\n\nSortedList, O(log n) remove.\n\ntime: O(V + E * log V).\nhttps://en.wikipedia.org/wiki/Dijkstra%27s_algorithm\n\n```\nfrom sortedcontainers import SortedList, SortedDict, SortedSet\nclass Solution:\n d...
1
0
['Python']
0
number-of-restricted-paths-from-first-to-last-node
Java, Dijikstra + DFS with memo
java-dijikstra-dfs-with-memo-by-fighting-v4ah
\nLong[] cache;\n public int countRestrictedPaths(int n, int[][] edges) {\n cache=new Long[n+1];\n Map<Integer,Map<Integer,Integer>> graph=new
fighting_for_flag
NORMAL
2021-03-07T04:09:10.234923+00:00
2021-03-07T04:10:12.074775+00:00
225
false
```\nLong[] cache;\n public int countRestrictedPaths(int n, int[][] edges) {\n cache=new Long[n+1];\n Map<Integer,Map<Integer,Integer>> graph=new HashMap<>();\n for(int[] edge:edges){\n graph.computeIfAbsent(edge[0],k->new HashMap<>()).put(edge[1],edge[2]);\n graph.computeI...
1
1
[]
0
number-of-restricted-paths-from-first-to-last-node
Clean Java
clean-java-by-rexue70-hcfo
This question is a combination of \n\n1. 787. Cheapest Flights Within K Stops https://leetcode.com/problems/cheapest-flights-within-k-stops/\n2. cout of increas
rexue70
NORMAL
2021-03-07T04:06:48.781342+00:00
2021-03-07T04:08:05.131266+00:00
145
false
This question is a combination of \n\n1. 787. Cheapest Flights Within K Stops https://leetcode.com/problems/cheapest-flights-within-k-stops/\n2. cout of increasing path\n\n\nwe first use dijikstra to calculate the distance to last node value of every node\nthen we do a dfs from 1 to n, use memo to cache.\n\n```\nclass ...
1
0
[]
0
number-of-restricted-paths-from-first-to-last-node
[C++] BFS + DP
c-bfs-dp-by-louis1992-s1ru
\nclass Solution {\npublic:\n \n void bfs(int n, const vector<vector<pair<int, int>>>& graph, vector<int>& distances, vector<int>& closest_nodes) {\n
louis1992
NORMAL
2021-03-07T04:02:51.422994+00:00
2021-03-07T04:53:01.222272+00:00
261
false
```\nclass Solution {\npublic:\n \n void bfs(int n, const vector<vector<pair<int, int>>>& graph, vector<int>& distances, vector<int>& closest_nodes) {\n auto cmp = [](const pair<int, int> &a, const pair<int, int> &b) {\n return a.second > b.second;\n };\n priority_queue<pair<int, i...
1
0
['C']
1
number-of-restricted-paths-from-first-to-last-node
Simple dijkstra's/dfs with dp
simple-dijkstrasdfs-with-dp-by-nikhilc15-edng
cpp\nclass Solution {\npublic:\n void dijkstra(vector<vector<pair<int, int>>> &adj, vector<int> &dists) {\n vector<bool> seen(adj.size(), false);\n
nikhilc1527
NORMAL
2021-03-07T04:02:49.753363+00:00
2021-03-07T04:03:32.719098+00:00
226
false
```cpp\nclass Solution {\npublic:\n void dijkstra(vector<vector<pair<int, int>>> &adj, vector<int> &dists) {\n vector<bool> seen(adj.size(), false);\n auto comp = [&dists](int a, int b) {return dists[a]>dists[b];};\n priority_queue<int, vector<int>, decltype(comp)> pq(comp);\n pq.push(adj...
1
0
['Dynamic Programming', 'C']
0
number-of-restricted-paths-from-first-to-last-node
JavaScript Djikstras + Memoization
javascript-djikstras-memoization-by-stev-2gu5
javascript\nvar countRestrictedPaths = function(n, edges) {\n // create an adjacency list to store the neighbors with weight for each node\n const adjList
stevenkinouye
NORMAL
2021-03-07T04:02:20.073616+00:00
2021-03-07T04:06:14.455438+00:00
228
false
```javascript\nvar countRestrictedPaths = function(n, edges) {\n // create an adjacency list to store the neighbors with weight for each node\n const adjList = new Array(n + 1).fill(null).map(() => new Array(0));\n for (const [node1, node2, weight] of edges) {\n adjList[node1].push([node2, weight]);\n ...
1
0
['Dynamic Programming', 'JavaScript']
0
number-of-restricted-paths-from-first-to-last-node
Dijkstra + DFS + DP (Does it get any better than this)
dijkstra-dfs-dp-does-it-get-any-better-t-kpfh
\nclass Solution {\n int MOD = (int) (Math.pow(10, 9) + 7);\n public int countRestrictedPaths(int n, int[][] edges) {\n Map<Integer, List<int[]>> g
banty
NORMAL
2021-03-07T04:01:03.236608+00:00
2021-03-07T04:01:03.236657+00:00
284
false
```\nclass Solution {\n int MOD = (int) (Math.pow(10, 9) + 7);\n public int countRestrictedPaths(int n, int[][] edges) {\n Map<Integer, List<int[]>> graph = new HashMap<>();\n for (int[] edge : edges) {\n int from = edge[0];\n int to = edge[1];\n int dist = edge[2];\...
1
0
[]
0
number-of-restricted-paths-from-first-to-last-node
c++ dynamic programming(memoization+dfs) + dijktra's algorithm..
c-dynamic-programmingmemoizationdfs-dijk-uq66
// c++ dynamic programming + dijktra\'s algorithm..\n// step1--> use dijktra\'s shortest path algorithm to obtain shortes distance of all node from "nth node ie
Kumar_Neelesh
NORMAL
2021-03-07T04:00:36.530336+00:00
2021-03-07T05:06:26.836509+00:00
284
false
// c++ dynamic programming + dijktra\'s algorithm..\n// step1--> use dijktra\'s shortest path algorithm to obtain shortes distance of all node from "nth node ie last node"..\n// step 2---> run dfs from nth to 1st node with condition distanceToLastNode(zi) > distanceToLastNode(zi+1)...\n// step 3--->store dp[node number...
1
1
[]
0