본문 바로가기
알고리즘/Codewars

[Codewars] Sum of Minimums! (7 kyu) / JavaScript

by fluss 2023. 4. 2.

https://www.codewars.com/kata/5d5ee4c35162d9001af7d699

 

Codewars - Achieve mastery through coding practice and developer mentorship

A coding practice website for all programming levels – Join a community of over 3 million developers and improve your coding skills in over 55 programming languages!

www.codewars.com

 

DESCRIPTION:

Given a 2D ( nested ) list ( array, vector, .. ) of size m * n, your task is to find the sum of the minimum values in each row.

For Example:

[ [ 1, 2, 3, 4, 5 ]        #  minimum value of row is 1
, [ 5, 6, 7, 8, 9 ]        #  minimum value of row is 5
, [ 20, 21, 34, 56, 100 ]  #  minimum value of row is 20
]

So the function should return 26 because the sum of the minimums is 1 + 5 + 20 = 26.

Note: You will always be given a non-empty list containing positive values.

ENJOY CODING :)

 

코드

function sumOfMinimums(arr) {
  let sum = 0;
  for(let i = 0; i < arr.length; i++){
    sum += Math.min(...arr[i]);
  }
  return sum;
}

댓글