Sales Path #114
Replies: 5 comments
-
|
Beta Was this translation helpful? Give feedback.
-
Pseudocode:
|
Beta Was this translation helpful? Give feedback.
-
`Obviously iterating through all paths again and again is not a good solution, since its wasteful in terms of time and memory. But intuitively if we find a solution that uses previous calculations somehow. This hints that the solution should involve recursion in some manner. First we notice that if the root is also a leaf, the best Sales Path, is simply the value in the node itself. This is the base case for the solution. If the root has children, then the minimal Sales Path is also a minimal path from the root’s child. Thus, if we already know the minimal cost for the root’s children, then the minimal cost for the root is simply the minimum of the values for its children plus the value stored in the root itself. A solution to this question, using these facts is given below:` |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
current cost coming to the root there is a floor, here is 5. I am doing a recursion for the 4 of 5, so cost would update it. |
Beta Was this translation helpful? Give feedback.
-
https://leetcode.com/problems/sum-of-root-to-leaf-binary-numbers/
Beta Was this translation helpful? Give feedback.
All reactions