0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

paizaラーニング レベルアップ問題集 DPメニュー JavaScript 最安値を達成するには 1

Posted at

最安値を達成するには 1 (paizaランク B 相当)

解答例

問題文に沿って実装します。

const fs = require("fs");
const input = fs.readFileSync("/dev/stdin", "utf-8").trim();
const lines = input.split("\n");

const [n, a, b] = lines[0].split(" ").map(Number);

const dp = Array(n + 1).fill(0);

dp[0] = 0;//りんご0個、0円
dp[1] = a;//りんご1個はa円のみ
//りんご2個以降
for (let i = 2; i <= n; i++) {
  //i個買う時の最小金額=小さい方(i-1個の最小金額+りんご1個の金額、i-2個の最小金額+りんご2個の金額)
  dp[i] = Math.min(dp[i - 1] + a, dp[i - 2] + b);
}
console.log(dp[n]);
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?