varunsh17's blog

By varunsh17, 8 months ago, In English

The concept is explained in the Image, Image Link

DP[i][0] is when you want to be at ith floor but not in the lift (2 cases arrises)

- WHEN you are the lift on i-1 th floor

- When you are at stairs at the i-1 th floor

DP[i][1] is when you want to be at ith floor but in the lift (2 cases arrises)

- WHEN you are the lift on i-1 th floor

- When you are at stairs at the i-1 th floor (YOU WILL ADD "c" as given in the ques)

So, for each floor we will take (min(When at ith floor in lift, When at ith floor reached with stairs))

-> Min(dp[i][0],dp[i][1])

Base cases at straightforward.

int[][] dp = new int[n][2];
for (int[] x : dp) Arrays.fill(x, Integer.MAX_VALUE);
dp[0][0] = 0;
dp[0][1] = c;
System.out.print(0 + " ");
for (int i = 1; i < n; i++) {
    dp[i][1] = Math.min(dp[i - 1][1] + elevator[i - 1], dp[i][1]);
    dp[i][1] = Math.min(dp[i - 1][0] + elevator[i - 1] + c, dp[i][1]);
    dp[i][0] = Math.min(dp[i - 1][1] + stair[i - 1], dp[i][0]);
    dp[i][0] = Math.min(dp[i - 1][0] + stair[i - 1], dp[i][0]);
    System.out.print(Math.min(dp[i][0], dp[i][1]) + " ");
}

The Problem Link- https://codeforces.com/contest/1249/problem/E

Code — 219533289

Full text and comments »

  • Vote: I like it
  • 0
  • Vote: I do not like it