Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added solution to q45 #39

Merged
merged 1 commit into from
Oct 3, 2022
Merged

Conversation

rakshitgondwal
Copy link
Contributor

This is a template on how to fill out a PR properly.

Description

The main idea here is to create an array of the same size as nums and progress so that each slot of it will represent the minimum amount of steps to reach that position.

To do so, we will create just 2 variables:

  • len will store the length of our input;
  • steps is the array we mentioned above.

We will initialised all cells of steps to INT_MAX, but the first one, set to 0 (since we start there and it takes no movement to reach it).

Then we go for the main loop, going with i from 0 up to the last element of our input and we will:

  • loop with j from the minimum value between len - 1 (last element of the array) and i + nums[i]) (last element you can reach with the current jump) up to i (excluded);
  • check if the current value of step[j] is greater than what we can achieve jumping from the currently considered cell (step[i] + 1) and in case update step[j] to a smaller, more convenient value;
  • otherwise, since we know that we cannot offer anything better to both step[j] and all the cells left on the left of j, we break out of the inner loop and move on with another value of i.

Once we are done, we can just return the last value of steps

Related Issue

Acceptance Criteria

Type of Changes

Type
🐛 Bug fix
✨ New feature
🔨 Refactoring
💯 Add tests
🔗 Update dependencies
📜 Docs

Testing Steps / QA Criteria

@DugarRishab DugarRishab merged commit 4694339 into DugarRishab:main Oct 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants