Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2251ab9

Browse files
Merge pull request doocs#240 from phuclhv/master
Add Python Solution to 559,774 and 743
2 parents baafff8 + 04e79ff commit 2251ab9

File tree

3 files changed

+93
-0
lines changed

3 files changed

+93
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
3+
"""
4+
# Definition for a Node.
5+
class Node:
6+
def __init__(self, val=None, children=None):
7+
self.val = val
8+
self.children = children
9+
"""
10+
# Performance
11+
'''
12+
Runtime: 36 ms, faster than 99.57% of Python3 online submissions for Maximum Depth of N-ary Tree.
13+
Memory Usage: 14.5 MB, less than 100.00% of Python3 online submissions for Maximum Depth of N-ary Tree.
14+
'''
15+
class Solution:
16+
def maxDepth(self, root: 'Node') -> int:
17+
if not root:
18+
return 0
19+
max_depth = 1
20+
for child in root.children:
21+
max_depth = max(self.maxDepth(child) + 1 , max_depth)
22+
#print(max_depth, root.val)
23+
return max_depth
24+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
'''
2+
Given a sorted (in ascending order) integer array nums of n elements and a target value, write a function to search target in nums. If target exists, then return its index, otherwise return -1.
3+
'''
4+
5+
# Perfomance
6+
'''
7+
Runtime: 268 ms, faster than 84.25% of Python3 online submissions for Binary Search.
8+
Memory Usage: 14 MB, less than 100.00% of Python3 online submissions for Binary Search.
9+
'''
10+
11+
12+
class Solution:
13+
def search(self, nums: List[int], target: int) -> int:
14+
low, high = 0, len(nums) - 1
15+
while low <= high:
16+
mid = low + (high - low) // 2
17+
if nums[mid] == target:
18+
return mid
19+
elif nums[mid] < target:
20+
low = mid + 1
21+
else:
22+
high = mid - 1
23+
return -1
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
'''
2+
There are N network nodes, labelled 1 to N.
3+
4+
Given times, a list of travel times as directed edges times[i] = (u, v, w), where u is the source node, v is the target node, and w is the time it takes for a signal to travel from source to target.
5+
6+
Now, we send a signal from a certain node K. How long will it take for all nodes to receive the signal? If it is impossible, return -1.
7+
'''
8+
9+
# Performance
10+
'''
11+
Runtime: 496 ms, faster than 91.63% of Python3 online submissions for Network Delay Time.
12+
Memory Usage: 14.7 MB, less than 46.15% of Python3 online submissions for Network Delay Time.
13+
'''
14+
from collections import deque
15+
class Solution:
16+
def networkDelayTime(self, times: List[List[int]], N: int, K: int) -> int:
17+
18+
# Build N+1 because index is from 1-N
19+
travel_times = [[] for y in range(N+1)]
20+
21+
# Build the array of travel times to reduce cost of searching later
22+
for time in times:
23+
origin, dest, time_travel = time
24+
travel_times[origin].append((dest, time_travel))
25+
26+
# Store the shortest amount of time to reach i-th node
27+
visited_times = [float('inf') for x in range(N+1)]
28+
visited_times[0] = 0
29+
visited_times[K] = 0
30+
31+
32+
# Store next traverse in line
33+
visited_queue = deque()
34+
visited_queue.append(K)
35+
36+
# BFS
37+
while visited_queue:
38+
cur_node = visited_queue.popleft()
39+
for time in travel_times[cur_node]:
40+
(dest, time_travel) = time
41+
if time_travel + visited_times[cur_node] < visited_times[dest]:
42+
visited_times[dest] = time_travel + visited_times[cur_node]
43+
visited_queue.append(dest)
44+
45+
# Only return the max if all were traversed. Return -1 otherwise
46+
return max(visited_times) if max(visited_times) != float('inf') else -1

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /