Count Number of Ways to Reach End of Stare

package org.wesome.dsalgo;

public class StareCase {
    static int countWaysUtil(int maxStare, int maxJump) {
        int[] waysCount = new int[maxStare];
        waysCount[0] = 1;
        waysCount[1] = 1;
//        loop till maxStare, for every stare count, start another loop, check , max jump cannot exceed stare count
//        loop 1, stare counter should not more then max StareCase
//        loop 2 jump should not be more then max jump and it cannot be more then no of stares
        for (int stareCntr = 2; stareCntr < maxStare; stareCntr++) {
            for (int jumpCntr = 1; jumpCntr <= maxJump && jumpCntr <= stareCntr; jumpCntr++) {
//                if m is 4, the person can climb 1 stair or 2 stairs or 3 stairs or 4 stairs at a time.
//                    ways(n, m) = ways(n-1, m) + ways(n-2, m) + ... ways(n-m, m)
                waysCount[stareCntr] = waysCount[stareCntr] + waysCount[stareCntr - jumpCntr];
            }
        }
        return waysCount[maxStare - 1];
    }
}
package org.wesome.dsalgo;

import org.junit.jupiter.api.Test;

import static org.wesome.dsalgo.StareCase.countWaysUtil;

public class StareCaseTest {
    @Test
    void stareCaseTest() {
        int stares = 5, maxJump = 4;
        System.out.println("Number of ways = " + countWaysUtil(stares + 1, maxJump));
    }
}
plugins {
    id 'java'
    id "io.freefair.lombok" version "6.2.0"
}

group = 'org.wesome'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = JavaVersion.VERSION_1_8

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
}

dependencies {
    testImplementation 'org.junit.jupiter:junit-jupiter:5.6.2'
}

test {
    useJUnitPlatform()
}

 

follow us on