"Bitshift the number to the right and keep track of the 1's you encounter. If you bitshift it completely and only encounter one 1, it is a power of two."
Nils G. - "Bitshift the number to the right and keep track of the 1's you encounter. If you bitshift it completely and only encounter one 1, it is a power of two."See full answer
"Construct a min-heap either inplace, or by making a copy of the array and then applying heapify on that copy. This is done in O(n) time.
Maintain two zero-initialised variables - sum and count.
Keep popping off the heap while sum < k, and update count.
In the worst case you will have to do n pops, and each pop is O(log n), so the algorithm would take O(n log n) in total. Space complexity depends on whether you're allowed to modify inplace or not, so either O(1) or O(n) respectively."
Anonymous Wolf - "Construct a min-heap either inplace, or by making a copy of the array and then applying heapify on that copy. This is done in O(n) time.
Maintain two zero-initialised variables - sum and count.
Keep popping off the heap while sum < k, and update count.
In the worst case you will have to do n pops, and each pop is O(log n), so the algorithm would take O(n log n) in total. Space complexity depends on whether you're allowed to modify inplace or not, so either O(1) or O(n) respectively."See full answer
"Assuming that trades will have information like
trade_type buy or sell
trade_price
with these tuples, one can iterate over each trade while maintaining a stack which maintains all the open buy trades.
If we encounter a sell trade then we pop one element make it a buy/sell pair and calculate the profit/loss for that pair. Moreover, keep adding pair-wise profit/loss to calculate overall profit as we continue iterating over trades.
At the end print pairs and their profit/loss along with"
Parth S. - "Assuming that trades will have information like
trade_type buy or sell
trade_price
with these tuples, one can iterate over each trade while maintaining a stack which maintains all the open buy trades.
If we encounter a sell trade then we pop one element make it a buy/sell pair and calculate the profit/loss for that pair. Moreover, keep adding pair-wise profit/loss to calculate overall profit as we continue iterating over trades.
At the end print pairs and their profit/loss along with"See full answer
"def getdifferentnumber(arr):
\# arr of nonnegative ints
\# find the smallest non negative int that isn't in the array
MAX_INT = 2^31-1
\# not allowed to modify the input arr
seen = []
smallest = 0
for i in arr:
if smallest in arr:
smallest += 1
seen.append(i)
return smallest"
Anonymous Owl - "def getdifferentnumber(arr):
\# arr of nonnegative ints
\# find the smallest non negative int that isn't in the array
MAX_INT = 2^31-1
\# not allowed to modify the input arr
seen = []
smallest = 0
for i in arr:
if smallest in arr:
smallest += 1
seen.append(i)
return smallest"See full answer
"def flatten_dictionary(dictionary):
\# return a flattened dictionary - int/string/another dictionary values
\# if the key is empty, exclude from the output
\# concat using a "." btwn them
\# add to res which is { "key.a.b.etc": "value" }
\# iterate through the key value pairs
\# while there is a key value pair in the value
\# continue going through that, until the value is an int/string
flatDic = {}
flatDicHelper("", dictionary, flatDic)
print(flatDic)
return flatDic
def flatDicHelper(initialKey"
Anonymous Owl - "def flatten_dictionary(dictionary):
\# return a flattened dictionary - int/string/another dictionary values
\# if the key is empty, exclude from the output
\# concat using a "." btwn them
\# add to res which is { "key.a.b.etc": "value" }
\# iterate through the key value pairs
\# while there is a key value pair in the value
\# continue going through that, until the value is an int/string
flatDic = {}
flatDicHelper("", dictionary, flatDic)
print(flatDic)
return flatDic
def flatDicHelper(initialKey"See full answer
"Sorting is a technique to arrange data in either increasing order or decreasing order, and, the function that implements this functionality is called sort function"
Farhan -. - "Sorting is a technique to arrange data in either increasing order or decreasing order, and, the function that implements this functionality is called sort function"See full answer
"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
"def findAlibaba(countOfRooms, strategy):
#countofrooms: num rooms
#listRooms rooms to look for alibabba
possiblePlaces = []
#initialize rooms
for i in range(countOfRooms):
possiblePlaces.append(True)
for i in range(len(strategy)):
roomToCheck = strategy[i]
#Room is marked as unavailable
possiblePlaces[roomToCheck] = False
#Next day calculatins
nextDayPlaces = []
for j in range(countOfRooms):
nextDayPla"
JOBHUNTER - "def findAlibaba(countOfRooms, strategy):
#countofrooms: num rooms
#listRooms rooms to look for alibabba
possiblePlaces = []
#initialize rooms
for i in range(countOfRooms):
possiblePlaces.append(True)
for i in range(len(strategy)):
roomToCheck = strategy[i]
#Room is marked as unavailable
possiblePlaces[roomToCheck] = False
#Next day calculatins
nextDayPlaces = []
for j in range(countOfRooms):
nextDayPla"See full answer