0
0

More than 1 year has passed since last update.

Leetcode easy: 1672. Richest Customer Wealth(Javascript)

Last updated at Posted at 2022-07-09

Leetcodeを始めたので記録しています。

やった問題

1672.Richest Customer Wealth

You are given an m x n integer grid accounts where accounts[i][j] is the amount of money the i​​​​​​​​​​​th​​​​ customer has in the j​​​​​​​​​​​th​​​​ bank. Return the wealth that the richest customer has.
A customer's wealth is the amount of money they have in all their bank accounts. The richest customer is the customer that has the maximum wealth.

Example:

Input: accounts = [[1,2,3],[3,2,1]]
Output: 6
Explanation:
1st customer has wealth = 1 + 2 + 3 = 6
2nd customer has wealth = 3 + 2 + 1 = 6
Both customers are considered the richest with a wealth of 6 each, so return 6.

処理の流れをざっくり考える

処理の流れをイメージする

  • forで一番外側の配列を回す。
  • 配列の合計を取得する。これが使えそう。

  • 毎回比較してその時点で一番大きければその値で上書きする。

最初に書いたコード

/**
 * @param {number[][]} accounts
 * @return {number}
 */
var maximumWealth = function(accounts) {
    let maximum = 0
    for (i=0; i<accounts.length; i++){
        var sum = accounts[i].reduce((sum,element) => sum + element, 0);
        if (maximum < sum) {
        maximum = sum
        }
    }
    return maximum
};

よりシンプルな方法がある気がしてならない。。

Runtime: 85 ms, faster than 55.69% of JavaScript online submissions for Richest Customer Wealth.
Memory Usage: 42.7 MB, less than 12.99% of JavaScript online submissions for Richest Customer Wealth.

調べたこと

アロー関数

ビデオを見る前にアロー関数を復習する。

function dispNum(x, y, func){
  console.log(func(x, y));
}

dispNum(7, 3, (x, y) => (x + y) / 2);
>> 5

ここでいうdispNumは、x,y,funcを引数にとって、funcの実行結果をconsole.logに吐くという関数。funcはx,yを引数に取る関数であることが示されている。
dispNumに渡す関数の中で何をするかをアロー関数で定義している。

なんだか改めて書くとスッキリしたので次に。

reduce関数

とにかく第一引数にはそれまでの処理結果、第二引数には次の値が入るというもの。

以下のように書くと合計が得られるし

const array = [1,2,3];
const result = array.reduce((prev,current)=>prev + current,0);
>>>6

以下のように書くと最大値が得られる。(上のページより引用。)

const array = [1,2,3];
const result = array.reduce((prev,current)=> prev > current? prev : current);
>>>3

オブジェクトを回すときはキーを指定してあげるだけ。
ここで使われているfindが気になったので調べておく。

find関数

ベンリー!

答えを見て改善したこと

答えを見たが考え方は間違ってなかった。
改善するとしたら最大かどうかくらべて代入するところかな?

var maximumWealth = function(accounts) {
    let maximumWealthSoFar = 0
    for (i=0; i<accounts.length; i++){
        var currentCustomerWealth = accounts[i].reduce((sum,element) => sum + element, 0);
        maximumWealthSoFar = Math.max(maximumWealthSoFar,currentCustomerWealth);
    }
    return maximumWealthSoFar;
};

ちょっと改善してみた。
変数名も回答に合わせた。

Runtime: 77 ms, faster than 68.41% of JavaScript online submissions for Richest Customer Wealth.
Memory Usage: 42.6 MB, less than 12.99% of JavaScript online submissions for Richest Customer Wealth.

ろくに早くならなかったのでちょっと綺麗になったというだけだった。

上に書いた配列の最大を求める方法でMaxを使う方法が示されていた。

var arr = [1,2,3];
var max = arr.reduce(function(a, b) {
    return Math.max(a, b);
}, -Infinity);

なるほどである。

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