LeetCode Weekly Contest 171 4問目 - Minimum Distance to Type a Word Using Two Fingers [1320]

Source

LeetCode Weekly Contest 171
問題文

問題概要

省略

解法

省略

cLayversion 20200119-1)のコード

C++に変換後のコードはこちら

#define main dummy_main
{}
#undef main

int dist(int a, int b){
  return abs(a/6 - b/6) + abs(a%6 - b%6);
}

int dp[26][26], nx[26][26];

class Solution {
public:
  int minimumDistance(string word) {
    int N = word.size(), c;
    int res = int_inf;
    rep(i,26) rep(j,26) dp[i][j] = 0;

    rep(k,N){
      c = word[k] - 'A';
      rep(i,26) rep(j,26) nx[i][j] = int_inf;
      rep(i,26) rep(j,26){
        nx[i][c] <?= dp[i][j] + dist(j,c);
        nx[c][j] <?= dp[i][j] + dist(i,c);
      }
      rep(i,26) rep(j,26) dp[i][j] = nx[i][j];
    }

    rep(i,26) rep(j,26) res <?= dp[i][j];
    return res;
  }
};

Current time: 2024年04月27日09時07分02秒
Last modified: 2020年01月25日23時07分21秒 (by laycrs)
Tags: Competitive_Programming_Incomplete LeetCode
トップページに戻る

Logged in as: unknown user (not login)

ログイン: