Skip to content

코딩 테스트 공부 정답 | jiseung #147

Open
@jiseung-kang

Description

@jiseung-kang

@jiseung 님의 정답이에요! 👏👏👏

제출한 정답

function solution(alp, cop, problems) {
  let [alpMax, copMax] = [0, 0];

  for (const [alp_req, cop_req] of problems) {
    alpMax = alpMax > alp_req ? alpMax : alp_req;
    copMax = copMax > cop_req ? copMax : cop_req;
  }

  const dp = Array.from({ length: alpMax + 1 }, () =>
    Array.from({ length: copMax + 1 }, () => Infinity),
  );

  alp = alp < alpMax ? alp : alpMax;
  cop = cop < copMax ? cop : copMax;

  for (let a = 0; a < alp + 1; a++) {
    for (let c = 0; c < cop + 1; c++) {
      dp[a][c] = 0;
    }
  }

  for (let a = alp; a < alpMax + 1; a++) {
    for (let c = cop; c < copMax + 1; c++) {
      if (a < alpMax)
        dp[a + 1][c] =
          dp[a + 1][c] < dp[a][c] + 1 ? dp[a + 1][c] : dp[a][c] + 1;
      if (c < copMax)
        dp[a][c + 1] =
          dp[a][c + 1] < dp[a][c] + 1 ? dp[a][c + 1] : dp[a][c] + 1;

      for (const [alp_req, cop_req, alp_rwd, cop_rwd, cost] of problems) {
        if (a >= alp_req && c >= cop_req) {
          const na = alpMax < a + alp_rwd ? alpMax : a + alp_rwd;
          const nc = copMax < c + cop_rwd ? copMax : c + cop_rwd;
          dp[na][nc] =
            dp[na][nc] < dp[a][c] + cost ? dp[na][nc] : dp[a][c] + cost;
        }
      }
    }
  }

  return dp[alpMax][copMax];
}

풀이 데이터

{
  "probId": "118668",
  "author": "jiseung",
  "lang": "JavaScript",
  "createdAt": 1679485614483
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    확인중 👀제출된 정답을 확인중입니다.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions