Skip to main content

656 Coin Path

 

Solution


Approach #1 Brute Force[Time Limit Exceeded]

In this approach, we make use of a next array of size n. Here, n refers to the size of the given A array. The array nums is used such that nums[i] is used to store the minimum number of coins needed to jump till the end of the array A, starting from the index i.

We start by filling the next array with all -1's. Then, in order to fill this next array, we make use of a recursive function jump(A, B, i, next) which fills the next array starting from the index i onwards, given A as the coins array and B as the largest jump value.

With i as the current index, we can consider every possible index from i+1 to i+B as the next place to be jumped to. For every such next index, j, if this place can be jumped to, we determine the cost of reaching the end of the array starting from the index i, and with j as the next index jumped from i, as A[i] + jump(A, B, j, next). If this cost is lesser than the minimum cost required till now, for the same starting index, we can update the minimum cost and the value of next[i] as well.

For every such function call, we also need to return this minimum cost.

At the end, we traverse over the next array starting from the index 1. At every step, we add the current index to the res list to be returned and also jump/move to the index pointed by next[i], since this refers to the next index for the minimum cost. We continue in the same manner till we reach the end of the array A.

Complexity Analysis

  • Time complexity : O(B^n). The size of the recursive tree can grow upto O(b^n) in the worst case. This is because, we have B possible branches at every step. Here, B refers to the limit of the largest jump and n refers to the size of the given A array.

  • Space complexity : O(n). The depth of the recursive tree can grow upto nnext array of size n is used.


Approach #2 Using Memoization [Accepted]

Algorithm

In the recursive solution just discussed, a lot of duplicate function calls are made, since we are considering the same index through multiple paths. To remove this redundancy, we can make use of memoization.

We keep a memo array, such that memo[i] is used to store the minimum cost of jumps to reach the end of the array A. Whenever the value for any index is calculated once, it is stored in its appropriate location. Thus, next time whenever the same function call is made, we can return the result directly from this memo array, pruning the search space to a great extent.

Complexity Analysis

  • Time complexity : O(nB)memo array of size n is filled only once. We also do a traversal over the next array, which will go upto B steps. Here, n refers to the number of nodes in the given tree.

  • Space complexity : O(n). The depth of the recursive tree can grow upto nnext array of size n is used.


Approach #3 Using Dynamic Programming [Accepted]

Algorithm

From the solutions discussed above, we can observe that the cost of jumping till the end of the array A starting from the index i is only dependent on the elements following the index i and not the ones before it. This inspires us to make use of Dynamic Programming to solve the current problem.

We again make use of a next array to store the next jump locations. We also make use of a dp with the same size as that of the given A array. dp[i] is used to store the minimum cost of jumping till the end of the array A, starting from the index i. We start with the last index as the current index and proceed backwards for filling the next and dp array.

With i as the current index, we consider all the next possible positions from i+1i+2,..., i+B, and determine the position, j, which leads to a minimum cost of reaching the end of A, which is given by A[i]+dp[j]. We update next[i] with this corresponding index. We also update dp[i] with the minimum cost, to be used by the previous indices' cost calculations.

At the end, we again jump over the indices as per the next array and put these indices in the res array to be returned.

Complexity Analysis

  • Time complexity : O(nB). We need to consider all the possible B positions for every current index considered in the A array. Here, A refers to the number of elements in A.

  • Space complexity : O(n)dp and next array of size n are used.

Comments

Popular posts from this blog

Python - List - Append, Count, Extend, Index, Insert, Pop, Remove, Reverse, Sort

🐍 Advance List List is widely used and it's functionalities are heavily useful. Append Adds one element at the end of the list. Syntax list1.append(value) Input l1 = [1, 2, 3] l1.append(4) l1 Output [1, 2, 3, 4] append can be used to add any datatype in a list. It can even add list inside list. Caution: Append does not return anything. It just appends the list. Count .count(value) counts the number of occurrences of an element in the list. Syntax list1.count(value) Input l1 = [1, 2, 3, 4, 3] l1.count(3) Output 2 It returns 0 if the value is not found in the list. Extend .count(value) counts the number of occurrences of an element in the list. Syntax list1.extend(list) Input l1 = [1, 2, 3] l1.extend([4, 5]) Output [1, 2, 3, 4, 5] If we use append, entire list will be added to the first list like one element. Extend, i nstead of considering a list as one element, it joins the two lists one after other. Append works in the following way. Input l1 = [1, 2, 3] l1.append([4, 5]) Output...

Difference between .exec() and .execPopulate() in Mongoose?

Here I answer what is the difference between .exec() and .execPopulate() in Mongoose? .exec() is used with a query while .execPopulate() is used with a document Syntax for .exec() is as follows: Model.query() . populate ( 'field' ) . exec () // returns promise . then ( function ( document ) { console . log ( document ); }); Syntax for .execPopulate() is as follows: fetchedDocument . populate ( 'field' ) . execPopulate () // returns promise . then ( function ( document ) { console . log ( document ); }); When working with individual document use .execPopulate(), for model query use .exec(). Both returns a promise. One can do without .exec() or .execPopulate() but then has to pass a callback in populate.

683 K Empty Slots

  Approach #1: Insert Into Sorted Structure [Accepted] Intuition Let's add flowers in the order they bloom. When each flower blooms, we check it's neighbors to see if they can satisfy the condition with the current flower. Algorithm We'll maintain  active , a sorted data structure containing every flower that has currently bloomed. When we add a flower to  active , we should check it's lower and higher neighbors. If some neighbor satisfies the condition, we know the condition occurred first on this day. Complexity Analysis Time Complexity (Java):  O(N \log N) O ( N lo g N ) , where  N N  is the length of  flowers . Every insertion and search is  O(\log N) O ( lo g N ) . Time Complexity (Python):  O(N^2) O ( N 2 ) . As above, except  list.insert  is  O(N) O ( N ) . Space Complexity:  O(N) O ( N ) , the size of  active . Approach #2: Min Queue [Accepted] Intuition For each contiguous block ("window") of  k  po...