"from typing import List
def traprainwater(height: List[int]) -> int:
if not height:
return 0
l, r = 0, len(height) - 1
leftMax, rightMax = height[l], height[r]
res = 0
while l < r:
if leftMax < rightMax:
l += 1
leftMax = max(leftMax, height[l])
res += leftMax - height[l]
else:
r -= 1
rightMax = max(rightMax, height[r])
"
Anonymous Roadrunner - "from typing import List
def traprainwater(height: List[int]) -> int:
if not height:
return 0
l, r = 0, len(height) - 1
leftMax, rightMax = height[l], height[r]
res = 0
while l < r:
if leftMax < rightMax:
l += 1
leftMax = max(leftMax, height[l])
res += leftMax - height[l]
else:
r -= 1
rightMax = max(rightMax, height[r])
"See full answer
"Definitely nice to think of this without memorization, but there is a well known algorithm for this problem, which is the Levenshtein Distance.
Lev(a,b) = len(a) if len(b) == 0
= len(b) if len(a) == 0
= lev(a[1:], b[1:] if a[0] == b[0]
= 1 + min (lev(a, b[1:]), lev(a[1:], b), lev(a[1:], b[1:]))
https://en.wikipedia.org/wiki/Levenshtein_distance
I'm sure some optimizations could be made with heuristic."
Nicholas S. - "Definitely nice to think of this without memorization, but there is a well known algorithm for this problem, which is the Levenshtein Distance.
Lev(a,b) = len(a) if len(b) == 0
= len(b) if len(a) == 0
= lev(a[1:], b[1:] if a[0] == b[0]
= 1 + min (lev(a, b[1:]), lev(a[1:], b), lev(a[1:], b[1:]))
https://en.wikipedia.org/wiki/Levenshtein_distance
I'm sure some optimizations could be made with heuristic."See full answer
"As we can pass info to only one child at a time, I told that from any given node, we have to pass the info to that child(of this node) which has the largest subtree rooted at it. To calculate the subtree sizes, I used DFS. And then to calculate the minimum time to pass info to all the nodes, I used BFS picking the largest subtree child first at every node. I couldn't write the complete code in the given time and also made a mistake in telling the overall time complexity of my approach. I think t"
Lakshman B. - "As we can pass info to only one child at a time, I told that from any given node, we have to pass the info to that child(of this node) which has the largest subtree rooted at it. To calculate the subtree sizes, I used DFS. And then to calculate the minimum time to pass info to all the nodes, I used BFS picking the largest subtree child first at every node. I couldn't write the complete code in the given time and also made a mistake in telling the overall time complexity of my approach. I think t"See full answer
Software Engineer
Data Structures & Algorithms
+1 more
🧠Want an expert answer to a question? Saving questions lets us know what content to make next.
"
read_dir(path: str) -> list[str] returns the full path of all files and sub- directories of a given directory.
is_file(path: str) -> bool: returns true if the path points to a regular file.
is_dir(path: str) -> bool: returns true if the path points to a directory.
read_file(path: str) -> str: reads and returns the content of the file.
The algorithm: notice that storing all the file contents' is too space intensive, so we can't read all the files' contents to store and compare with each"
Idan R. - "
read_dir(path: str) -> list[str] returns the full path of all files and sub- directories of a given directory.
is_file(path: str) -> bool: returns true if the path points to a regular file.
is_dir(path: str) -> bool: returns true if the path points to a directory.
read_file(path: str) -> str: reads and returns the content of the file.
The algorithm: notice that storing all the file contents' is too space intensive, so we can't read all the files' contents to store and compare with each"See full answer
"if decreasing arr, start from end and keep checking if next element increases by 1 or not. wherever not, put that value there."
Rishabh R. - "if decreasing arr, start from end and keep checking if next element increases by 1 or not. wherever not, put that value there."See full answer
"
This is mostly correct and fairly fast.
My code has a bug somewhere where it fails on cases like the last case, where there are negative number on both ends of the array and the sums .
from collections import deque
debug = True # False
def prdbg(*x):
global debug
debug = True # False
if debug:
print(x)
else:
return
def max_sum(arr, start, end):
if type(arr) == type('''
"
Nathan B. - "
This is mostly correct and fairly fast.
My code has a bug somewhere where it fails on cases like the last case, where there are negative number on both ends of the array and the sums .
from collections import deque
debug = True # False
def prdbg(*x):
global debug
debug = True # False
if debug:
print(x)
else:
return
def max_sum(arr, start, end):
if type(arr) == type('''
"See full answer
"ArrayList allows constant time access (O(1)) to elements using their index because it uses a dynamic array internally, whereas LinkedList requires traversal from the head node, resulting in linear time complexity (O(n))."
Aziz V. - "ArrayList allows constant time access (O(1)) to elements using their index because it uses a dynamic array internally, whereas LinkedList requires traversal from the head node, resulting in linear time complexity (O(n))."See full answer
"Race Condition i,e multiple threads modifying simultaneously can lead to data inconsistency
Operations like putIfAbsent() or computeIfAbsent() are not atomoic i.e duplicate entries or missing updates when multiple threads perform operations
Data Corruption : during resizing of a hashmap by a thread, if another thread is accessing the same data , buckets can get corrupted, leading to a loss of data"
Sue G. - "Race Condition i,e multiple threads modifying simultaneously can lead to data inconsistency
Operations like putIfAbsent() or computeIfAbsent() are not atomoic i.e duplicate entries or missing updates when multiple threads perform operations
Data Corruption : during resizing of a hashmap by a thread, if another thread is accessing the same data , buckets can get corrupted, leading to a loss of data"See full answer