"bool isValidBST(TreeNode* root, long min = LONGMIN, long max = LONGMAX){
if (root == NULL)
return true;
if (root->val val >= max)
return false;
return isValidBST(root->left, min, root->val) &&
isValidBST(root->right, root->val, max);
}
`"
Alvaro R. - "bool isValidBST(TreeNode* root, long min = LONGMIN, long max = LONGMAX){
if (root == NULL)
return true;
if (root->val val >= max)
return false;
return isValidBST(root->left, min, root->val) &&
isValidBST(root->right, root->val, max);
}
`"See full answer
"This could be done using two-pointer approach assuming array is sorted: left and right pointers. We need track two sums (left and right) as we move pointers. For moving pointers we will move left to right by 1 (increment) when right sum is greater. We will move right pointer to left by 1 (decrement) when left sum is greater. at some point we will either get the sum same and that's when we exit from the loop. 0-left will be one array and right-(n-1) will be another array.
We are not going to mo"
Bhaskar B. - "This could be done using two-pointer approach assuming array is sorted: left and right pointers. We need track two sums (left and right) as we move pointers. For moving pointers we will move left to right by 1 (increment) when right sum is greater. We will move right pointer to left by 1 (decrement) when left sum is greater. at some point we will either get the sum same and that's when we exit from the loop. 0-left will be one array and right-(n-1) will be another array.
We are not going to mo"See full answer
"public static List Merge(List array1, List array2)
{
if (array1.Count == 0) return array2;
if (array2.Count == 0) return array1;
var result = new List();
// Identify starting array, array with largest starting element
var array = array1[0] > array2[0] ? array2 : array1;
var other = array1[0] > array2[0] ? array1 : array2;
// loop until we hit end of an array
int arrayIndex = 0;
int otherIndex = 0;
while(arrayIndex"
Mark S. - "public static List Merge(List array1, List array2)
{
if (array1.Count == 0) return array2;
if (array2.Count == 0) return array1;
var result = new List();
// Identify starting array, array with largest starting element
var array = array1[0] > array2[0] ? array2 : array1;
var other = array1[0] > array2[0] ? array1 : array2;
// loop until we hit end of an array
int arrayIndex = 0;
int otherIndex = 0;
while(arrayIndex"See full answer
"We are asked to calculate Sum(over value) for time in (t - window_size, t) where key in (key criteria).
To develop a function to set this up.
Let w be the window size. I would have an observer of some kind note the key-value, and for the first w windows just add the value to a temporary variable in memory if the key meets the key criteria. Then it would delete the oldest value and add the new value if the new key meets the criteria. At each step after "w", we would take the sum / w and store"
Prashanth A. - "We are asked to calculate Sum(over value) for time in (t - window_size, t) where key in (key criteria).
To develop a function to set this up.
Let w be the window size. I would have an observer of some kind note the key-value, and for the first w windows just add the value to a temporary variable in memory if the key meets the key criteria. Then it would delete the oldest value and add the new value if the new key meets the criteria. At each step after "w", we would take the sum / w and store"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.
"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
"
O(n) time, O(1) space
from typing import List
def maxsubarraysum(nums: List[int]) -> int:
if len(nums) == 0:
return 0
maxsum = currsum = nums[0]
for i in range(1, len(nums)):
currsum = max(currsum + nums[i], nums[i])
maxsum = max(currsum, max_sum)
return max_sum
debug your code below
print(maxsubarraysum([-1, 2, -3, 4]))
`"
Rick E. - "
O(n) time, O(1) space
from typing import List
def maxsubarraysum(nums: List[int]) -> int:
if len(nums) == 0:
return 0
maxsum = currsum = nums[0]
for i in range(1, len(nums)):
currsum = max(currsum + nums[i], nums[i])
maxsum = max(currsum, max_sum)
return max_sum
debug your code below
print(maxsubarraysum([-1, 2, -3, 4]))
`"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