Блог пользователя FO7

Автор FO7, история, 7 недель назад, По-английски

https://leetcode.com/discuss/interview-question/5846629/SquarePoint-OA-Desk-Quant/

Any ideas how to solve this problem ? some of my observations: (i) all k's are independent to each other (ii) looking for corners ?? Thanks in advance

  • Проголосовать: нравится
  • +4
  • Проголосовать: не нравится

»
6 недель назад, # |
Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится
/* 
as k's are independent we can treat them each differently
we say that dp[i][j][l] = min moves to solve till (i, j) for a particular 1 <= l <= k

now the transitions are straight forward
we take min at each step
if(shelf[i][j] != k)
        dp[i][j][k] = max(dp[i-1][j][k], dp[i][j-1][k])
        else dp[i][j][k] = dp[i-1][j-1][k];

*/
for(int i=0;i<=n;i++) for(int j=0;j<=k;j++)dp[i][0][j] = 0; 
    for(int i=0;i<=m;i++) for(int j=0;j<=k;j++)dp[0][i][j] = 0; 
    ll ans = 0;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            for(int l=1;l<=k;l++){
                if(shelf[i][j] != l){
                    dp[i][j][l] = min(dp[i][j][l], max(dp[i-1][j][l], dp[i][j-1][l]));
                }
                else 
                    dp[i][j][l] = min(dp[i][j][l], dp[i-1][j-1][l] + 1);
            }
        }
    }
    for(int i=1;i<=k;i++) ans += dp[n][m][i];
    cout << ans << '\n';

This solved the given two testcases, You have any different ideas pls share