HackerRank: 2D Array DS
Tags: hackerrank, 紮馬步
2D Array - DS
- l
水唷! 越來越有FU~
- python
# Complete the hourglassSum function below.
def hourglassSum(arr):
num_sum_dict = {}
for nr in range(0, 4):
for nc in range(0, 4):
num_sum_dict['{}{}'.format(str(nr), str(nc))] = \
arr[nr][nc] + arr[nr][nc+1] + arr[nr][nc+2]\
+ arr[nr+1][nc+1] \
+ arr[nr+2][nc] + arr[nr+2][nc+1] + arr[nr+2][nc+2]
return max(num_sum_dict.values())
// Complete the hourglassSum function below.
function hourglassSum(arr) {
let maxValue = -Infinity;
for (let nr = 0 ; nr <=3; nr++){
for (let nc=0; nc <=3; nc++){
let ncnrValue = arr[nr][nc] + arr[nr][nc+1] + arr[nr][nc+2]
+ arr[nr+1][nc+1]
+ arr[nr+2][nc] + arr[nr+2][nc+1] + arr[nr+2][nc+2];
if (maxValue <= ncnrValue) {
maxValue = ncnrValue
}
}
}
return maxValue
}