Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

Minimum number of Jumps

Given a sequence of numbers, find the minimum number of jumps to go from the starting position to the end and come back to the starting position again.

Each element of the sequence denotes the maximum number of moves that one can move from that position.

At any position, you can make a jump of at max k moves, where k is the value stored at that position. After reaching the end, you can use only those positions for jumping which have not been used previously for jumping.

The input will be given as a sequence of numbers separated by single-spaces. Output should be a single number which is the minimum number of jumps used. If its not possible to go to the end and come back to the starting position, then print -1

Input:

2 4 2 2 3 4 2 2

Output:

6 (3 to reach end and 3 to come back)

Input

1 0

Output

-1

Note

  • Assume all the numbers of the sequence are non-negative

EDIT 1

The line "Thus, it should be clear that one can always jump from the last position." might be confusing, so I removed it from the question. It will have no effect on the question.

Winning Criteria :

The winner will be the one with the shortest code.

Answer*

Draft saved
Draft discarded
Cancel
2
  • \$\begingroup\$ Data.List.permutations does not work in GHC, but only in GHCi. According to our Guide to Golfing Rules in Haskell, you should either add the import or mark your answer as "Haskell GHCi". The first option is generally preferred by Haskell golfers on this site. \$\endgroup\$ Commented Jan 5, 2018 at 14:40
  • \$\begingroup\$ Instead of a<-permutations[0..t l-1],let f=takeWhile(/=0)a, you can write f<-map(takeWhile(/=0))(permutations[0..t l-1]), which again can be golfed to f<-fst.span(>0)<$>permutations[0..t l-1]. With this you are back to 166 bytes even by adding the import: Try it online! \$\endgroup\$ Commented Jan 5, 2018 at 14:41

AltStyle によって変換されたページ (->オリジナル) /