"from typing import List
def three_sum(nums: List[int]) -> List[List[int]]:
nums.sort()
triplets = set()
for i in range(len(nums) - 2):
firstNum = nums[i]
l = i + 1
r = len(nums) - 1
while l 0:
r -= 1
elif potentialSum < 0:
l += 1
"
Anonymous Roadrunner - "from typing import List
def three_sum(nums: List[int]) -> List[List[int]]:
nums.sort()
triplets = set()
for i in range(len(nums) - 2):
firstNum = nums[i]
l = i + 1
r = len(nums) - 1
while l 0:
r -= 1
elif potentialSum < 0:
l += 1
"See full answer
"const ops = {
'+': (a, b) => a+b,
'-': (a, b) => a-b,
'/': (a, b) => a/b,
'': (a, b) => ab,
};
function calc(expr) {
// Search for + or -
for (let i=expr.length-1; i >= 0; i--) {
const char = expr.charAt(i);
if (['+', '-'].includes(char)) {
return opschar), calc(expr.slice(i+1)));
}
}
// Search for / or *
for (let i=expr.length-1; i >= 0; i--) {
const char = expr.charAt(i);
if"
Tiago R. - "const ops = {
'+': (a, b) => a+b,
'-': (a, b) => a-b,
'/': (a, b) => a/b,
'': (a, b) => ab,
};
function calc(expr) {
// Search for + or -
for (let i=expr.length-1; i >= 0; i--) {
const char = expr.charAt(i);
if (['+', '-'].includes(char)) {
return opschar), calc(expr.slice(i+1)));
}
}
// Search for / or *
for (let i=expr.length-1; i >= 0; i--) {
const char = expr.charAt(i);
if"See full answer
"a. Sort the array elements.
b. take two pointers at index 0 and index Len-1;
c. if the sum at the two pointers is target; break and return the pair
d. if the sum is smaller, then move left pointer by 1
e. else move right pointer by 1;
run the logic till the target is met or right pointer crosses the left pointer."
Komal S. - "a. Sort the array elements.
b. take two pointers at index 0 and index Len-1;
c. if the sum at the two pointers is target; break and return the pair
d. if the sum is smaller, then move left pointer by 1
e. else move right pointer by 1;
run the logic till the target is met or right pointer crosses the left pointer."See full answer
"function preorderToInorder(preorder) {
let inorder = [];
let stack = [];
let root = preorder[0];
stack.push(root);
for (let i = 1; i 0 && stack[stack.length - 1] 0) {
root = stack.pop();
inorder.push(r"
Ugo C. - "function preorderToInorder(preorder) {
let inorder = [];
let stack = [];
let root = preorder[0];
stack.push(root);
for (let i = 1; i 0 && stack[stack.length - 1] 0) {
root = stack.pop();
inorder.push(r"See full answer
"Problem Statement: The Fibonacci sequence is defined as F(n) = F(n-1) + F(n-2) with F(0) = 1 and F(1) = 1.
The solution is given in the problem statement itself.
If the value of n = 0, return 1.
If the value of n = 1, return 1.
Otherwise, return the sum of data at (n - 1) and (n - 2).
Explanation: The Fibonacci sequence is a series of numbers where each number is the sum of the two preceding ones, typically starting with 0 and 1.
Java Solution:
public static int fib(int n"
Rishi G. - "Problem Statement: The Fibonacci sequence is defined as F(n) = F(n-1) + F(n-2) with F(0) = 1 and F(1) = 1.
The solution is given in the problem statement itself.
If the value of n = 0, return 1.
If the value of n = 1, return 1.
Otherwise, return the sum of data at (n - 1) and (n - 2).
Explanation: The Fibonacci sequence is a series of numbers where each number is the sum of the two preceding ones, typically starting with 0 and 1.
Java Solution:
public static int fib(int n"See full answer
"A much better solution than the one in the article, below:
It looks like the ones writing articles here in Javascript do not understand the time/space complexity of javascript methods.
shift, splice, sort, etc... In the solution article you have a shift and a sort being done inside a while, that is, the multiplication of Ns.
My solution, below, iterates through the list once and then sorts it, separately. It´s O(N+Log(N))
class ListNode {
constructor(val = 0, next = null) {
th"
Guilherme F. - "A much better solution than the one in the article, below:
It looks like the ones writing articles here in Javascript do not understand the time/space complexity of javascript methods.
shift, splice, sort, etc... In the solution article you have a shift and a sort being done inside a while, that is, the multiplication of Ns.
My solution, below, iterates through the list once and then sorts it, separately. It´s O(N+Log(N))
class ListNode {
constructor(val = 0, next = null) {
th"See full answer
"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
"
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
"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
"function isPalindrome(s, start, end) {
while (s[start] === s[end] && end >= start) {
start++;
end--;
}
return end <= start;
}
function longestPalindromicSubstring(s) {
let longestPalindrome = '';
for (let i=0; i < s.length; i++) {
let j = s.length-1;
while (s[i] !== s[j] && i <= j) {
j--;
}
if (s[i] === s[j]) {
if (isPalindrome(s, i, j)) {
const validPalindrome = s.substring(i, j+1"
Tiago R. - "function isPalindrome(s, start, end) {
while (s[start] === s[end] && end >= start) {
start++;
end--;
}
return end <= start;
}
function longestPalindromicSubstring(s) {
let longestPalindrome = '';
for (let i=0; i < s.length; i++) {
let j = s.length-1;
while (s[i] !== s[j] && i <= j) {
j--;
}
if (s[i] === s[j]) {
if (isPalindrome(s, i, j)) {
const validPalindrome = s.substring(i, j+1"See full answer
"Leetcode 347: Heap + Hashtable
Follow up question: create heap with the length of K instead of N (more time complexity but less space )"
Chen J. - "Leetcode 347: Heap + Hashtable
Follow up question: create heap with the length of K instead of N (more time complexity but less space )"See full answer