educative.io

Educative

Java space O(1) solution

class ExpressNumber {

  public int CountWays(int n) {
    // bottom up 
    int n0 = 1, n1 = 1, n2 = 1, n3 = 2;
    for(int i = 4; i <= n; i++){
      int temp = n0 + n1 + n3;
      n0 = n1;
      n1 = n2;
      n2 = n3;
      n3 = temp;
    }
    return n3;
  }

  public static void main(String[] args) {
    ExpressNumber en = new ExpressNumber();
    System.out.println(en.CountWays(4));
    System.out.println(en.CountWays(5));
    System.out.println(en.CountWays(6));
  }
}

Type your question above this line.

Course: https://www.educative.io/collection/5668639101419520/5633779737559040
Lesson: https://www.educative.io/collection/page/5668639101419520/5633779737559040/5724313353191424

Hi Annabelle!
Thank you for your precious feedback. The course is updated now.

1 Like