All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g1801_1900.s1871_jump_game_vii.Solution Maven / Gradle / Ivy

There is a newer version: 1.38
Show newest version
package g1801_1900.s1871_jump_game_vii;

// #Medium #String #Two_Pointers #Prefix_Sum #2022_05_11_Time_14_ms_(76.25%)_Space_54.9_MB_(41.39%)

/**
 * 1871 - Jump Game VII\.
 *
 * Medium
 *
 * You are given a **0-indexed** binary string `s` and two integers `minJump` and `maxJump`. In the beginning, you are standing at index `0`, which is equal to `'0'`. You can move from index `i` to index `j` if the following conditions are fulfilled:
 *
 * *   `i + minJump <= j <= min(i + maxJump, s.length - 1)`, and
 * *   `s[j] == '0'`.
 *
 * Return `true` _if you can reach index_ `s.length - 1` _in_ `s`_, or_ `false` _otherwise._
 *
 * **Example 1:**
 *
 * **Input:** s = "011010", minJump = 2, maxJump = 3
 *
 * **Output:** true
 *
 * **Explanation:**
 *
 * In the first step, move from index 0 to index 3.
 *
 * In the second step, move from index 3 to index 5. 
 *
 * **Example 2:**
 *
 * **Input:** s = "01101110", minJump = 2, maxJump = 3
 *
 * **Output:** false 
 *
 * **Constraints:**
 *
 * *   2 <= s.length <= 105
 * *   `s[i]` is either `'0'` or `'1'`.
 * *   `s[0] == '0'`
 * *   `1 <= minJump <= maxJump < s.length`
**/
public class Solution {
    public boolean canReach(String s, int minJump, int maxJump) {
        int i;
        int j = 0;
        int n = s.length();
        char[] li = s.toCharArray();
        for (i = 0; i < n; i++) {
            // o == ok
            if (i == 0 || li[i] == 'o') {
                for (j = Math.max(j, i + minJump); j < Math.min(n, i + maxJump + 1); j++) {
                    if (li[j] == '0') {
                        li[j] = 'o';
                    }
                }
            }
            if (j > n) {
                break;
            }
        }
        return li[n - 1] == 'o';
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy