"function spiralCopy(inputMatrix) {
if (inputMatrix.length === 1) return inputMatrix[0];
let lowerY = 0;
let upperY = inputMatrix.length-1;
let lowerX = 0;
let upperX = inputMatrix[0].length-1;
const output = [];
while (true) {
if (lowerX > upperX) break;
for (let x = lowerX; x upperY) break;
for (let y = lowerY; y <= upperY; y++) {
output.push(inputMatrix[y][u"
Tiago R. - "function spiralCopy(inputMatrix) {
if (inputMatrix.length === 1) return inputMatrix[0];
let lowerY = 0;
let upperY = inputMatrix.length-1;
let lowerX = 0;
let upperX = inputMatrix[0].length-1;
const output = [];
while (true) {
if (lowerX > upperX) break;
for (let x = lowerX; x upperY) break;
for (let y = lowerY; y <= upperY; y++) {
output.push(inputMatrix[y][u"See full answer
"function knapsack(weights, values, cap) {
const indicesByValue = Object.keys(weights).map(weight => parseInt(weight));
indicesByValue.sort((a, b) => values[b]-values[a]);
const steps = new Map();
function knapsackStep(cap, sack) {
if (steps.has(sack)) {
return steps.get(sack);
}
let maxOutput = 0;
for (let index of indicesByValue) {
if (!sack.has(index) && weights[index] <= cap) {
maxOutput ="
Tiago R. - "function knapsack(weights, values, cap) {
const indicesByValue = Object.keys(weights).map(weight => parseInt(weight));
indicesByValue.sort((a, b) => values[b]-values[a]);
const steps = new Map();
function knapsackStep(cap, sack) {
if (steps.has(sack)) {
return steps.get(sack);
}
let maxOutput = 0;
for (let index of indicesByValue) {
if (!sack.has(index) && weights[index] <= cap) {
maxOutput ="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
"def getcheapestcost(rootNode):
\# need to do DFS for each branch
\# but this can be done recursively
n = len(rootNode.children)
if n == 0:
return 0
else:
min_cost = float('inf')
for i in range(len(n)):
tempcost = getcheapest_cost(rootNode.children[i])
if (tempcost < mincost):
mincost = tempcost
return min_cost + rootNode.cost
\# A node
class Node:
\# Constructor to create a new node
def init\(self, cost):
self.cost = cost
self.children = []
self.parent = None"
Anonymous Owl - "def getcheapestcost(rootNode):
\# need to do DFS for each branch
\# but this can be done recursively
n = len(rootNode.children)
if n == 0:
return 0
else:
min_cost = float('inf')
for i in range(len(n)):
tempcost = getcheapest_cost(rootNode.children[i])
if (tempcost < mincost):
mincost = tempcost
return min_cost + rootNode.cost
\# A node
class Node:
\# Constructor to create a new node
def init\(self, cost):
self.cost = cost
self.children = []
self.parent = None"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
"I couldn't follow the solution offered here, but my solution seemed to pass 6/6 tests. Any feedback is welcome, thank you!
def encrypt(word):
en_word = ""
for i in range(len(word)):
if i == 0:
en_word += chr(ord(word[0])+1)
else:
num = ord(word[i]) + ord(en_word[i-1])
while num > 122:
num -= 26
en_word += chr(num)
return en_word
def decrypt(word):
de_word = ""
for i in range(len(word)):
if i == 0:
de_word += chr(ord(word[i]"
Anonymous Armadillo - "I couldn't follow the solution offered here, but my solution seemed to pass 6/6 tests. Any feedback is welcome, thank you!
def encrypt(word):
en_word = ""
for i in range(len(word)):
if i == 0:
en_word += chr(ord(word[0])+1)
else:
num = ord(word[i]) + ord(en_word[i-1])
while num > 122:
num -= 26
en_word += chr(num)
return en_word
def decrypt(word):
de_word = ""
for i in range(len(word)):
if i == 0:
de_word += chr(ord(word[i]"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
"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
"function getDifferentNumber(arr) {
// your code goes here
const n = arr.length;
//Define Max Integer
const MAX_INT = Math.pow(2, 31) - 1;
//Coppy arr to arr1 then sort arr1.
const arr1 = arr;
arr1.sort(function(a,b) {return a -b});
// Put arr1 in Set to optimize lo
const uniqueSet = new Set(arr1);
console.log(uniqueSet);
// Check for the smallest nonnegative integer not in the array
for (let i = 0; i < n; i++) {
if (!uniqueSet.has(i)) {
return i;
}
}
if(n<MAX_INT) return n+1;
else return -1;
}"
Anonymous Hare - "function getDifferentNumber(arr) {
// your code goes here
const n = arr.length;
//Define Max Integer
const MAX_INT = Math.pow(2, 31) - 1;
//Coppy arr to arr1 then sort arr1.
const arr1 = arr;
arr1.sort(function(a,b) {return a -b});
// Put arr1 in Set to optimize lo
const uniqueSet = new Set(arr1);
console.log(uniqueSet);
// Check for the smallest nonnegative integer not in the array
for (let i = 0; i < n; i++) {
if (!uniqueSet.has(i)) {
return i;
}
}
if(n<MAX_INT) return n+1;
else return -1;
}"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