https://www.codewars.com/kata/55f5efd21ad2b48895000040
My answer / AC
function maxSumDig(nmax, maxsm) {
var res = [];
var isSatisfy;
for(var i=1000; i<=nmax; i++) {
let arr = i.toString().split("");
isSatisfy = true;
for(var j=0; j<=arr.length-4; j++){
if(Number(arr[j]) + Number(arr[j+1]) + Number(arr[j+2]) + Number(arr[j+3]) > maxsm) isSatisfy = false;
}
if(isSatisfy) res.push(i);
}
var sum = res.reduce((a,b)=>Number(a)+Number(b));
var mean = sum/res.length;
var meanmap = res.map(a=>Math.abs(Number(a)-Number(mean)));
var index = meanmap.indexOf(Math.min(...meanmap));
return [res.length, res[index], sum];
}
Recap
- 不算难,我的答案和最佳也基本一致
- 主要问题是搞错了题目的用意,没看清”the maximum sum of 4 contiguous digits is maxSum“,是一个大数字中每连续四个数进行一次是否小于
maxSum
判断
网友评论