<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Tankery Chen on Medium]]></title>
        <description><![CDATA[Stories by Tankery Chen on Medium]]></description>
        <link>https://medium.com/@tankery?source=rss-63de2a639b59------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*YNrbIFa3JgV3m2Ly.jpg</url>
            <title>Stories by Tankery Chen on Medium</title>
            <link>https://medium.com/@tankery?source=rss-63de2a639b59------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 16:14:26 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@tankery/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Solve Jump Game Problem using State Machine]]></title>
            <link>https://medium.com/@tankery/solve-jump-game-problem-using-state-machine-4a3b1d6e5fc0?source=rss-63de2a639b59------2</link>
            <guid isPermaLink="false">https://medium.com/p/4a3b1d6e5fc0</guid>
            <category><![CDATA[jump-game-problem]]></category>
            <category><![CDATA[state-machine]]></category>
            <category><![CDATA[algorithms]]></category>
            <dc:creator><![CDATA[Tankery Chen]]></dc:creator>
            <pubDate>Tue, 27 Mar 2018 05:46:17 GMT</pubDate>
            <atom:updated>2018-03-27T05:46:17.697Z</atom:updated>
            <cc:license>http://creativecommons.org/licenses/by/4.0/</cc:license>
            <content:encoded><![CDATA[<p>Recently I solved an interesting algorithm problem, <a href="https://leetcode.com/problems/jump-game/description/">Jump Game Problem</a>, with State Machine.</p><p>The problem is here:</p><blockquote>Given an array of non-negative integers, you are initially positioned at the first index of the array.</blockquote><blockquote>Each element in the array represents your maximum jump length at that position.</blockquote><blockquote>Determine if you are able to reach the last index.</blockquote><blockquote>For example: A = [2,3,1,1,4], return true.</blockquote><blockquote>A = [3,2,1,0,4], return false.</blockquote><p>I’ve seen some solutions also come with an <strong>O(n)</strong> and constant space performance, but the solution is not easy to understood, and hard to reuse to other problem, but <strong>State Machine</strong> is a really strong and flexible way to solve limited state (like true/false state) problem.</p><p>For this problem, we know that the target is find a way to jump to last index, so we can have a instinct that this problem can be solve by inverse iteration. So let’s have a try in this way.</p><p>First, the last index can be skip, as it already the last index, so the state always <strong>true</strong> no mater what.</p><p>Then we come to the 2nd number, we need to jump to last index, that’s next index for this case, so this number should be 1 or above to jump to next. So if this number is &gt; 0, state keep to <strong>true</strong>, or = 0, state transit to <strong>false</strong>.</p><p>For 3rd number, if 2nd number state is <strong>true</strong>, we only need to jump to next index. But if 2nd number state is <strong>false</strong>, we need to jump across 2nd, so this number needs to be &gt; 1 to jump direct to the end.</p><p>And we can take more round to iterate all numbers.</p><p>Here is a diagram shows the state transition when iterating:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/458/1*wuRs4VVZMnxDSw_qK9J68w.png" /><figcaption>Jump Game State</figcaption></figure><p>In this diagram, we can find the simple pattern of state transition:</p><ol><li>When current state is <strong>true</strong>, next state can be <strong>true</strong> if &gt; 0, else transit to <strong>false</strong>.</li><li>When current state is <strong>false</strong>, next state transit to <strong>true</strong> only if the number is larger than count of continuous <strong>false</strong>s, else, state keep in <strong>false</strong>.</li></ol><p>So we can write code now:</p><pre>public boolean canJump(int[] nums) {<br>    boolean stateCanJump = true;<br>    int falseCount = 0;<br>    for (int i = 1; i &lt; nums.length; i++) {<br>        int pos = nums.length - i - 1;<br>        int value = nums[pos];<br>        if (stateCanJump) {<br>            if (value == 0) {<br>                stateCanJump = false;<br>                falseCount = 1;<br>            }<br>        } else {<br>            if (value &gt; falseCount) {<br>                stateCanJump = true;<br>                falseCount = 0;<br>            } else {<br>                falseCount++;<br>            }<br>        }<br>    }<br>    return stateCanJump;<br>}</pre><p>And problem solved! Congratulations!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4a3b1d6e5fc0" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>