森田賢二のleetCodeの使い方、解説、解答を毎日更新しているキャラクター

[leetcode]問題解答と解説。1770. Maximum Score from Performing Multiplication Operations [JavaScript]

[leetcode]問題解答と解説。1770. Maximum Score from Performing Multiplication Operations [JavaScript]

皆さんleetCode好きですよね。私も好きです。
なかなかやる機会がないのですよね。
leetCodeの使い方や解説ではなく実際に問題を解決するために何をしたか
をメモ書きして行きます

私はある決意をして今挑んでいます。
モチベーションを保つためだけにこれをやっています。

では今日のleetCode。
【1770. Maximum Score from Performing Multiplication Operations】の解決策と方法。自分がまず挑戦して
やったことをさらけ出します。

※下にある自分のコードは自分で設けた制限時間内に書けたところまです。

これは
「私は年末までこれを続けたらどんなleetCoderになるか」のシリーズ。
実験的ブログ更新です。

森田賢二のleetCode
森田賢二のleetCode

[leetcode] 1770. Maximum Score from Performing Multiplication Operations ルール

You are given two 0-indexed integer arrays nums and multipliers of size n and m respectively, where n >= m.

You begin with a score of 0. You want to perform exactly m operations. On the ith operation (0-indexed) you will:

Choose one integer x from either the start or the end of the array nums.
Add multipliers[i] * x to your score.
Note that multipliers[0] corresponds to the first operation, multipliers[1] to the second operation, and so on.
Remove x from nums.
Return the maximum score after performing m operations.

Example 1:

Input: nums = [1,2,3], multipliers = [3,2,1]
Output: 14

Explanation: An optimal solution is as follows:

  • Choose from the end, [1,2,3], adding 3 * 3 = 9 to the score.
  • Choose from the end, [1,2], adding 2 * 2 = 4 to the score.
  • Choose from the end, [1], adding 1 * 1 = 1 to the score.
    The total score is 9 + 4 + 1 = 14.
    Example 2:

Input: nums = [-5,-3,-3,-2,7,1], multipliers = [-10,-5,3,4,6]
Output: 102
Explanation: An optimal solution is as follows:

  • Choose from the start, [-5,-3,-3,-2,7,1], adding -5 * -10 = 50 to the score.
  • Choose from the start, [-3,-3,-2,7,1], adding -3 * -5 = 15 to the score.
  • Choose from the start, [-3,-2,7,1], adding -3 * 3 = -9 to the score.
  • Choose from the end, [-2,7,1], adding 1 * 4 = 4 to the score.
  • Choose from the end, [-2,7], adding 7 * 6 = 42 to the score.
    The total score is 50 + 15 - 9 + 4 + 42 = 102.

2つの0添字の整数配列numsと乗数mがそれぞれ与えられ、n >= mである。

最初は0点で、ちょうどm回の演算を行いたい。i番目の演算(0番台)では、次のようになる。

配列numsの先頭または末尾から整数xを1つ選ぶ。
乗数[i] * x をスコアに加える。
multipliers[0]が最初の操作、multipliers[1]が2番目の操作に対応し、以下同様であることに注意せよ。
numsからxを取り除く。
m個の演算を行った後の最大得点を返す。

exampleは実際の問題ページを見てください

[leetcode] 1770. Maximum Score from Performing Multiplication Operations 問題ページ

1770. Maximum Score from Performing Multiplication Operations

[leetcode] 1770. Maximum Score from Performing Multiplication Operations

my code is not good(time out & not good)

var maximumScore = function(nums, multipliers) {
    const dp = new Array(nums.length).fill(0)
    for(let i = 0, j = nums.length-1; i < nums.length ; i++, j--){
        dp[i] = Math.max(Math.max(Math.abs(nums[i] * multipliers[i]), Math.abs(nums[i] * multipliers[j])), Math.max(Math.abs(nums[j] * multipliers[i]), Math.abs(nums[j] * multipliers[j])))
    }
    return dp[nums.length - 1]
};

時間切れ

[leetcode] 1770. Maximum Score from Performing Multiplication Operations。discussの中の一つの解答例

数ある中からJavaScriptのもので、理解しやすい解説をピックアップしました。
discussから見ればさらにもっと違う方法で真似したくなるものがあるかもしれない

JavaScriptでfilterされている、discussはこちらから

気に入った解答1

シンプルだったので

[leetcode] 1770. Maximum Score from Performing Multiplication Operations をやってみて感想

カテゴリー leetCode