Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
jiseung-kang opened this issue Mar 22, 2023 · 0 comments
Open

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

jiseung-kang opened this issue Mar 22, 2023 · 0 comments
Labels
확인중 👀 제출된 정답을 확인중입니다.

Comments

@jiseung-kang
Copy link

@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
}
@codeisneverodd codeisneverodd added the 확인중 👀 제출된 정답을 확인중입니다. label Mar 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
확인중 👀 제출된 정답을 확인중입니다.
Projects
None yet
Development

No branches or pull requests

2 participants