📜  hackerrank min max sum 解决方案 - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:01:28.805000             🧑  作者: Mango

代码示例2
function miniMaxSum(arr) {
  let x1 = 0
  let x2 = 0
  let x3 = 0
  let x4 = 0
  let x5 = 0
  
  for(let i = 0; i < arr.length; i++) {
    
       if(arr[i] != arr[0]) {
        x1 += arr[i]
       }
    
       if(arr[i] != arr[1]) {
        x2 += arr[i]
      }

       if(arr[i] != arr[2]) {
        x3 += arr[i]
      }

       if(arr[i] != arr[3]) {
        x4 += arr[i]
      }

       if(arr[i] != arr[4]) {
        x5 += arr[i]
      }
  }
  
  let min = Math.min(x1, x2, x3, x4, x5)
  let max = Math.max(x1, x2, x3, x4, x5)
  
  if(min == max) {
     console.log(arr.length * 4, arr.length * 4)
  } else {
     console.log(min, max)
  }
}