"Let’s say the matrix is m x n (i.e., m rows and n columns).
Start from the top-right corner of the matrix.
Move left if you see a 1.
Move down if you see a 0.
Keep track of the row index where you last saw the leftmost 1 — that row has the most 1s.
public class MaxOnesRow {
public static int rowWithMostOnes(int matrix) {
int rows = matrix.length;
int cols = matrix[0].length;
int maxRowIndex = -1;
int j = cols - 1; /"
Khushbu R. - "Let’s say the matrix is m x n (i.e., m rows and n columns).
Start from the top-right corner of the matrix.
Move left if you see a 1.
Move down if you see a 0.
Keep track of the row index where you last saw the leftmost 1 — that row has the most 1s.
public class MaxOnesRow {
public static int rowWithMostOnes(int matrix) {
int rows = matrix.length;
int cols = matrix[0].length;
int maxRowIndex = -1;
int j = cols - 1; /"See full answer
"select
sub.name subreddit_name,
count(distinct us.userid) totalusers
from user_subreddit as us
left join subreddit as sub
on us.subredditid = sub.subredditid
group by
us.subreddit_id
having
count(distinct us.user_id) > 3"
Lucas G. - "select
sub.name subreddit_name,
count(distinct us.userid) totalusers
from user_subreddit as us
left join subreddit as sub
on us.subredditid = sub.subredditid
group by
us.subreddit_id
having
count(distinct us.user_id) > 3"See full answer
Coding
SQL
🧠 Want an expert answer to a question? Saving questions lets us know what content to make next.
"Binary Search on the array and after than compare the numbers at low and the high pointers whichever is closest is the answer. Because after the binary search low will be pointing to a number which is immediate greater than x and high will be pointing to a number which is immediate lesser than x.
int low = 0;
int high = n-1;
while(low <= high){
int mid = (low + high) / 2;
if(x == arr[mid]) return mid; //if x is already present then it will be the closest
else if(x < arr[mid]) high"
Shashwat K. - "Binary Search on the array and after than compare the numbers at low and the high pointers whichever is closest is the answer. Because after the binary search low will be pointing to a number which is immediate greater than x and high will be pointing to a number which is immediate lesser than x.
int low = 0;
int high = n-1;
while(low <= high){
int mid = (low + high) / 2;
if(x == arr[mid]) return mid; //if x is already present then it will be the closest
else if(x < arr[mid]) high"See full answer
"Here is my implementation:
select
marketing_channel,
AVG(purchasevalue) as avgpurchase_value
from
attribution
group by marketing_channel
order by avgpurchasevalue DESC
;
There is no need to copy and past the line of code for calculating the average into order by, just Alias is enough because going by the order of execution in sql, Always, order by is executed after executing select clause."
Maliki U. - "Here is my implementation:
select
marketing_channel,
AVG(purchasevalue) as avgpurchase_value
from
attribution
group by marketing_channel
order by avgpurchasevalue DESC
;
There is no need to copy and past the line of code for calculating the average into order by, just Alias is enough because going by the order of execution in sql, Always, order by is executed after executing select clause."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
"Problem: Given a modified binary tree, where each node also has a pointer to it's parent, find the first common ancestor of two nodes.
Answer: As it happens, the structure that we're looking at is actually a linked list (one pointer up), so the problem is identical to trying to find if two linked lists share a common node.
How this works is by stacking the two chains of nodes together so they're the same length.
chain1 = node1
chain2= node2
while True:
chain1 = chain1.next
chain2=chain"
Michael B. - "Problem: Given a modified binary tree, where each node also has a pointer to it's parent, find the first common ancestor of two nodes.
Answer: As it happens, the structure that we're looking at is actually a linked list (one pointer up), so the problem is identical to trying to find if two linked lists share a common node.
How this works is by stacking the two chains of nodes together so they're the same length.
chain1 = node1
chain2= node2
while True:
chain1 = chain1.next
chain2=chain"See full answer
"-- The text of the task is a bit confusing. If the status is repeated several
-- times, then in the end you should show as start_date the date of the first
-- occurrence, and in end_date the date of the last occurrence of this status,
-- and not the date of the beginning of the next status
with t1 as (select
order_id,
status,
orderdate as startdate,
lead(orderdate) over (partition by orderid order by orderdate) as enddate,
ifnull(lag(status) over (partition by order_id order by or"
Alexey T. - "-- The text of the task is a bit confusing. If the status is repeated several
-- times, then in the end you should show as start_date the date of the first
-- occurrence, and in end_date the date of the last occurrence of this status,
-- and not the date of the beginning of the next status
with t1 as (select
order_id,
status,
orderdate as startdate,
lead(orderdate) over (partition by orderid order by orderdate) as enddate,
ifnull(lag(status) over (partition by order_id order by or"See full answer
"function areSentencesSimilar(sentence1, sentence2, similarPairs) {
if (sentence1.length !== sentence2.length) return false;
for (let i=0; i (w1 === word1 && !visited.has(w2)) || (w2 === word1 && !visited.has(w1)));
if (!edge) {
"
Tiago R. - "function areSentencesSimilar(sentence1, sentence2, similarPairs) {
if (sentence1.length !== sentence2.length) return false;
for (let i=0; i (w1 === word1 && !visited.has(w2)) || (w2 === word1 && !visited.has(w1)));
if (!edge) {
"See full answer
"https://www.geeksforgeeks.org/find-local-minima-array/
I coded O(N) but after that gave a binary approach aswell.
After that he also gave a varient of this problem in which, local minima means that the number is strictly less than its adjacent (we cannot do binary search there sample test case [1,1,1,1,1,1,0,1] or [1,0,1,1,1,1,1,1] using mid we cannot determine if the minima is on left or right). we have to do a linear search or find recursively."
Anonymous Porcupine - "https://www.geeksforgeeks.org/find-local-minima-array/
I coded O(N) but after that gave a binary approach aswell.
After that he also gave a varient of this problem in which, local minima means that the number is strictly less than its adjacent (we cannot do binary search there sample test case [1,1,1,1,1,1,0,1] or [1,0,1,1,1,1,1,1] using mid we cannot determine if the minima is on left or right). we have to do a linear search or find recursively."See full answer
"select
customer_id,
order_date,
orderid as earliestorder_id
from (
select customer_id,
order_date,
order_id,
rownumber() over (partition by customerid, orderdate order by orderdate) as orderrankper_customer
from orders
) sub_table
where orderrankper_customer=1
order by orderdate, customerid;
Standard solution assumed that the orderid indicates which order comes in first. However this is not always the case, and sometime orderid can be random number withou"
Jessica C. - "select
customer_id,
order_date,
orderid as earliestorder_id
from (
select customer_id,
order_date,
order_id,
rownumber() over (partition by customerid, orderdate order by orderdate) as orderrankper_customer
from orders
) sub_table
where orderrankper_customer=1
order by orderdate, customerid;
Standard solution assumed that the orderid indicates which order comes in first. However this is not always the case, and sometime orderid can be random number withou"See full answer
"1) create the experimental and control groups.
2) Then calculate the proportion (mean) of the true conversion rates for both groups using the convert column which counts True as 1 and False as 0. This is their conversion rates
3) calculate the statistic of the two groups by subtracting the proportion and standardizing.
4) get the p-value and compare with 0.05.
5) conclude the difference is statistically significant if the p-value is less than 0.05 otherwise no statistical difference"
Frank A. - "1) create the experimental and control groups.
2) Then calculate the proportion (mean) of the true conversion rates for both groups using the convert column which counts True as 1 and False as 0. This is their conversion rates
3) calculate the statistic of the two groups by subtracting the proportion and standardizing.
4) get the p-value and compare with 0.05.
5) conclude the difference is statistically significant if the p-value is less than 0.05 otherwise no statistical difference"See full answer
"-- Write your query here
select
u.userid as userid,
IFNULL(sum(purchase_value), 0) AS LTV
FROM
user_sessions u
JOIN attribution a
ON u.sessionid = a.sessionid
group by
user_id
order by
LTV desc
;
Needs a full join. Wondering why cant we do a left outer join here. All the sessions should have complete data."
Aneesha K. - "-- Write your query here
select
u.userid as userid,
IFNULL(sum(purchase_value), 0) AS LTV
FROM
user_sessions u
JOIN attribution a
ON u.sessionid = a.sessionid
group by
user_id
order by
LTV desc
;
Needs a full join. Wondering why cant we do a left outer join here. All the sessions should have complete data."See full answer