"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
"
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
"int main()
{
int a1[7]={1,2,3,4,5,6,7};
int a2[7]={1,9,10,11,12,13,14};
vectorv;
v.insert(v.begin(),begin(a1),end(a1));
v.insert(v.begin(),begin(a2),end(a2));
int a3[v.size()];
sort(v.begin(),v.end());
for(int i=0;i<v.size();i++)
{
a3[i]=v[i];
}
}
`"
Aryan D. - "int main()
{
int a1[7]={1,2,3,4,5,6,7};
int a2[7]={1,9,10,11,12,13,14};
vectorv;
v.insert(v.begin(),begin(a1),end(a1));
v.insert(v.begin(),begin(a2),end(a2));
int a3[v.size()];
sort(v.begin(),v.end());
for(int i=0;i<v.size();i++)
{
a3[i]=v[i];
}
}
`"See full answer
"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
"My solution is simple; it does an in-order DFS traversal to create an array of in-order elements then it searches through the array to find the node we want the successor of. finally we return the node that is 1 after the input node, in the case our input node is the last element of our DFS we know there is no successor, therefore it returns None/null.
CODE INSTRUCTIONS:
1) The method fi"
Rohan M. - "My solution is simple; it does an in-order DFS traversal to create an array of in-order elements then it searches through the array to find the node we want the successor of. finally we return the node that is 1 after the input node, in the case our input node is the last element of our DFS we know there is no successor, therefore it returns None/null.
CODE INSTRUCTIONS:
1) The method fi"See full answer
Coding
Data Structures & Algorithms
🧠 Want an expert answer to a question? Saving questions lets us know what content to make next.
"with base as (
select viewerid, year, month, watchhours,
lag(watchhours, 2) over (partition by viewerid order by year, month) as p3,
lag(watchhours, 1) over (partition by viewerid order by year, month) as p2
from watch_time
)
select
viewer_id
from base
where p3 < p2 and p2 < watch_hours
group by 1
`"
- Z. - "with base as (
select viewerid, year, month, watchhours,
lag(watchhours, 2) over (partition by viewerid order by year, month) as p3,
lag(watchhours, 1) over (partition by viewerid order by year, month) as p2
from watch_time
)
select
viewer_id
from base
where p3 < p2 and p2 < watch_hours
group by 1
`"See full answer
"SELECT
i.item_category,
o.order_date,
SUM(o.orderquantity) AS totalunits_ordered
FROM
orders o
JOIN
items i ON o.itemid = i.itemid
WHERE
o.order_date >= DATE('now', '-6 days')
GROUP BY
i.item_category,
o.order_date
ORDER BY
i.item_category ASC,
o.order_date ASC;"
Anonymous Tortoise - "SELECT
i.item_category,
o.order_date,
SUM(o.orderquantity) AS totalunits_ordered
FROM
orders o
JOIN
items i ON o.itemid = i.itemid
WHERE
o.order_date >= DATE('now', '-6 days')
GROUP BY
i.item_category,
o.order_date
ORDER BY
i.item_category ASC,
o.order_date ASC;"See full answer
"with login_data as (
select *
from useractivitylog
where activity_type = 'LOGIN'
)
,cte as (
select userid, timestamp as currentlogin
,lag(timestamp,1,timestamp) over (partition by user_id order by timestamp asc)
as previous_login,
round((julianday(timestamp) - julianday(lag(timestamp,1,timestamp) over (partition by user_id order by timestamp asc))) * 24 * 60)
as minutes_elapsed
from login_data
)
select * from cte where currentlogin previouslogin;"
Kedar W. - "with login_data as (
select *
from useractivitylog
where activity_type = 'LOGIN'
)
,cte as (
select userid, timestamp as currentlogin
,lag(timestamp,1,timestamp) over (partition by user_id order by timestamp asc)
as previous_login,
round((julianday(timestamp) - julianday(lag(timestamp,1,timestamp) over (partition by user_id order by timestamp asc))) * 24 * 60)
as minutes_elapsed
from login_data
)
select * from cte where currentlogin previouslogin;"See full answer
"Hi, my solution gives the exact numerical values as the proposed solution, but it doesn't pass the tests. Am I missing something, or is this a bug?
def findrevenueby_city(transactions: pd.DataFrame,
users: pd.DataFrame,
exchange_rate: pd.DataFrame) -> pd.DataFrame:
gets user city for each user id
userids = users[['id', 'usercity']]
and merge on transactions
transactions = transactions.merge(user_ids, how='left"
Gabriel P. - "Hi, my solution gives the exact numerical values as the proposed solution, but it doesn't pass the tests. Am I missing something, or is this a bug?
def findrevenueby_city(transactions: pd.DataFrame,
users: pd.DataFrame,
exchange_rate: pd.DataFrame) -> pd.DataFrame:
gets user city for each user id
userids = users[['id', 'usercity']]
and merge on transactions
transactions = transactions.merge(user_ids, how='left"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
"function findAllCharsSmallestWindow(a, b) {
const bFrequency = new Map();
for (let char of b) {
bFrequency.set(char, (bFrequency.get(char) || 0) + 1);
}
let win = [];
const winFrequency = new Map();
let right = 0;
while (right 0) {
for (let char of win) {
"
Tiago R. - "function findAllCharsSmallestWindow(a, b) {
const bFrequency = new Map();
for (let char of b) {
bFrequency.set(char, (bFrequency.get(char) || 0) + 1);
}
let win = [];
const winFrequency = new Map();
let right = 0;
while (right 0) {
for (let char of win) {
"See full answer
"Test case is wrong. It expects to sort in asc order of month_year.
-- Write your query here
SELECT
strftime('%Y-%m', createdat) AS monthyear,
COUNT(DISTINCT userid) AS numcustomers,
COUNT(t.id) AS num_orders,
SUM(price * quantity) AS order_amt
FROM
transactions t
INNER JOIN products p
ON t.product_id = p.id
GROUP BY
month_year
ORDER BY
month_year
;
"
Aneesha K. - "Test case is wrong. It expects to sort in asc order of month_year.
-- Write your query here
SELECT
strftime('%Y-%m', createdat) AS monthyear,
COUNT(DISTINCT userid) AS numcustomers,
COUNT(t.id) AS num_orders,
SUM(price * quantity) AS order_amt
FROM
transactions t
INNER JOIN products p
ON t.product_id = p.id
GROUP BY
month_year
ORDER BY
month_year
;
"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
"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