Greedy Algorithm

A problem I tried

--

Recently, I’ve tried this problem.

https://leetcode.com/problems/jump-game/description/?envType=study-plan-v2&envId=top-interview-150

At first, my algorithm was O(N²). Personally, I was confident about the solution because I used recursion and computing it from the tail was excellent for me.

https://leetcode.com/problems/jump-game/submissions/1205565105/?envType=study-plan-v2&envId=top-interview-150

However, there is a better solution than mine, which is greedy algorithm. The time complexity is O(n).

What is greedy?

Greedy algorithm is literally greedy, which means it keeps selecting the biggest number. This the example is below.

https://leetcode.com/problems/jump-game/submissions/1216903845/?envType=study-plan-v2&envId=top-interview-150

The algo doesn’t care about if the pointer goes beyond the last index or not. It just selects the furthest distance at any rate. This is a trick of greedy algo.

Feel free to reach out to me on LinkedIn, which you can find below. Looking forward to connecting!

https://www.linkedin.com/in/tomoharu-tsutsumi-56051a126/

--

--

Tomoharu Tsutsumi

Senior Software Engineer at two industry-leading startups ( Go | Ruby | TypeScript | JavaScript | Gin | Echo | Rails | React | Redux | Next)