"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
"To determine if a graph is not a tree, you can check for the following conditions:
Presence of cycles: A graph is not a tree if it contains cycles. In a tree, there should be exactly one unique path between any two vertices. If you can find a cycle in the graph, it cannot be a tree.
Insufficient number of edges: A tree with N vertices will have exactly N-1 edges. If the graph has fewer or more than N-1 edges, then it is not a tree.
Disconnected components: A tree is a connected graph, m"
Vaibhav C. - "To determine if a graph is not a tree, you can check for the following conditions:
Presence of cycles: A graph is not a tree if it contains cycles. In a tree, there should be exactly one unique path between any two vertices. If you can find a cycle in the graph, it cannot be a tree.
Insufficient number of edges: A tree with N vertices will have exactly N-1 edges. If the graph has fewer or more than N-1 edges, then it is not a tree.
Disconnected components: A tree is a connected graph, m"See full answer
"check the count of both sentences if the same then start loop on words count and check the presence of words are the same."
Suleman A. - "check the count of both sentences if the same then start loop on words count and check the presence of words are the same."See full answer
"Implemented the Java code to find the largest island. It is similar to count the island. But in this we need to keep track of max island and compute its perimeter."
Techzen I. - "Implemented the Java code to find the largest island. It is similar to count the island. But in this we need to keep track of max island and compute its perimeter."See full answer
Software Engineer
Coding
+2 more
🧠 Want an expert answer to a question? Saving questions lets us know what content to make next.
"A red-black tree is a self-balancing binary search tree. The motivation for this is that the benefits of O(logN) search, insertion, and deletion that a binary tree provides us will disappear if we let the tree get too "imbalanced" (e.g. there are too many nodes on one side of the tree or some branches have a depth that is way out of proportion to the average branch depth). This imbalance will occur if we don't adjust the tree after inserting or deleting nodes, hence our need for self-balancing c"
Alex M. - "A red-black tree is a self-balancing binary search tree. The motivation for this is that the benefits of O(logN) search, insertion, and deletion that a binary tree provides us will disappear if we let the tree get too "imbalanced" (e.g. there are too many nodes on one side of the tree or some branches have a depth that is way out of proportion to the average branch depth). This imbalance will occur if we don't adjust the tree after inserting or deleting nodes, hence our need for self-balancing c"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
"This problem can be solved with two approaches
Iterative approach
Recursive approach
Quite easy to think about the iterative approach, you can make use of a while loop in that case. But what if you want to make use of previously computed values? That case going for the recursive solution is quite useful.
class Collatz:
def init(self) -> None:
self.cache = {}
self.steps = 0
def steps_from(self, n) -> int:
# base case
if n == 1:
"
Frederick A. - "This problem can be solved with two approaches
Iterative approach
Recursive approach
Quite easy to think about the iterative approach, you can make use of a while loop in that case. But what if you want to make use of previously computed values? That case going for the recursive solution is quite useful.
class Collatz:
def init(self) -> None:
self.cache = {}
self.steps = 0
def steps_from(self, n) -> int:
# base case
if n == 1:
"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
"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 class sample {
public int [] merge(int [] a, int [] b)
{
if(a == null || a.length == 0 || b == null || b.length == 0) return null;
int i = 0, j = 0, index = -1;
int [] merged = new int[a.length + b.length];
while (i < a.length && j < b.length)
{
if(a[i] < b[i]) merged[++index] = a[i++];
else merged[++index] = b[j++];
}
while (i < a.length)
{
merged[++index] = a[i++];
}
"
Nikhil R. - "public class sample {
public int [] merge(int [] a, int [] b)
{
if(a == null || a.length == 0 || b == null || b.length == 0) return null;
int i = 0, j = 0, index = -1;
int [] merged = new int[a.length + b.length];
while (i < a.length && j < b.length)
{
if(a[i] < b[i]) merged[++index] = a[i++];
else merged[++index] = b[j++];
}
while (i < a.length)
{
merged[++index] = a[i++];
}
"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