Nomad
BAN USERAs per my interpretation, the boxes of the marbles are not labelled with any particular colour. As an example consider two boxes B1 and B2
B1 has 70 Green and 20 Red balls
B2 has 30 Green and 10 Red balls
Optimal way would be to put 30 Green from box B2 to B1 and 20 Red from B1 to B2.
For a tree to be balance, following conditions must hold true.
1. Left sub tree should be balanced
2. Right sub tree should be balanced
3. absolute difference between the heights of the two subtrees should be at most 1.
A recursive solution can be framed using the above mentioned conditions and can be done in O(n)
@oOZz As per your comment
This will work, but it's very inefficient. Imagine a tree that goes left->left->left->...->left or right->right->right->...->right, then your array will have a lot of sparse elements
Even your solution in Method Two is using the same approach, storing null values as markers. So if the tree is very unbalanced you end up storing a lot of null values.
What about the case wherein the buffer BBB gets overridden,for example the first B in BBB might contain a number from List 1 which is now being compare against 2. if(2 < 1), then say we need to insert 2 at the first B position.where will the value of 1 at the 1st B position go, we will have to push all elements one position to the right, increasing the complexity.
- Nomad August 27, 2013Here is a DP approach.
Arrange the tasks in some order (any order)
Let M(i) denote the maximum work done using the first i tasks.
We need to find the value of M(n).
To do this let's say we need to calculate M(i+1) from M(i)
If inserting (i + 1)th task in the previous max sequence increases the Max work spent, then change the sequence and include this (i+1)th task in the already existing sequence.
If inserting (i + 1)th task in the previous max sequence is equal to the M(i) then keep both sequences.
Therefore M(i) will not be a single sequence but there might be a list of tasks all with same max work value
Can be done by using a dynamic programming approach. Create a matrix say A such that
A[i][j] = sum of the numbers from index i to j in the original array. and also i <= j
To build this matrix use
A[i][j] = A[i][j-1] + Input[j]
Input is the input array.
Print the i and j indices for which the value of A[i][j] = 0
Hi,
I need a clarification from Saran.
Are we given access to only the node we need to delete and nothing else. What I mean here is in a SLL it is possible to delete a node with just the information of the node to be deleted and nothing else such as head , tail etc. Is such info available here
Thanks
Using and OR is correct since we need to find if a such a sequence exists by removing one character at a time.
For example restated -> restate -> estate -> state -> stat -> sat -> at -> a
This chain is obtained by removing d from restated
Such a chain might not exist for say removing 2nd alphabet since "rstated" is an invalid word and should return false
Hi Yashpal,
Can you please clarify a few questions?
1. Will there be such list for each customer?
list of webpages visited on each day for all the n days?
2. Each website w has some number of webpages
3.Need to find all the customers who visited a website on exactly "k" days (not more not less) with at least m webpages belonging to that website to be different?
Thanks in Advance
@King@Work But in the question it is mentioned to use a single counter variable.
- Nomad July 22, 2014