Implement the Greedy Solution
Implement the greedy algorithm to solve the problem statement and wrap up the project.
We'll cover the following...
Introduction
By now, we've put all the functionalities for our frog game in place. We just need to implement the greedy solution to generate the result. We will generate the result in the form of an array, whose every element is another array of exactly 2 values: the first one denotes the maximum number of jumps to take from each island, and the second value denotes the minimum number of steps taken to reach the current island.
First, let's go over how our solution will work with an example array.
Greedy solution approach
Let's assume that we are given an array [2, 3, 1, 3, 1, 2, 2, 3, 1]
, which denotes the maximum number of jumps that can be taken from each index (island). We'll follow the steps mentioned below to solve the problem and get the required array:
To generate the final result, we need to first find an intermediate result (say
res
). To do this, generate an array whose every index contains two elements. At each index of the array, the first element denotes the number of steps taken to reach the current index (island) and the second element denotes the number of jumps taken to reach the current index (island).For each array index, except the
...