We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Day 11: 2D Arrays
Day 11: 2D Arrays
+ 0 comments In C++:
int main() { vector<vector<int>> arr(6); int max = -63; for (int i = 0; i < 6; i++) { arr[i].resize(6); string arr_row_temp_temp; getline(cin, arr_row_temp_temp); vector<string> arr_row_temp = split(rtrim(arr_row_temp_temp)); for (int j = 0; j < 6; j++) { int arr_row_item = stoi(arr_row_temp[j]); arr[i][j] = arr_row_item; } } for(int i = 0; i < arr.size() - 2; i++) { for(int j = 0; j < arr[i].size() - 2; j++) { int currentMax = arr[i][j] + arr[i][j+1] +arr[i][j+2] + arr[i+1][j+1] + arr[i+2][j] + arr[i+2][j+1] + arr[i+2][j+2]; max = max < currentMax ? currentMax:max; } } cout << max << endl; return 0; }
+ 0 comments public class Solution { public static void main(String[] args) throws IOException { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
List<List<Integer>> arr = new ArrayList<>(); for (int i = 0; i < 6; i++) { String[] arrRowTempItems = bufferedReader.readLine().replaceAll("\\s+$", "").split(" "); List<Integer> arrRowItems = new ArrayList<>(); for (int j = 0; j < 6; j++) { int arrItem = Integer.parseInt(arrRowTempItems[j]); arrRowItems.add(arrItem); } arr.add(arrRowItems); } bufferedReader.close(); int sum = -100; for(int i = 0; i < 4; i++) { int temp; for(int j = 0; j < 4; j++) { temp = 0; temp += arr.get(i).get(j); temp += arr.get(i).get(j+1); temp += arr.get(i).get(j+2); temp += arr.get(i+1).get(j+1); temp += arr.get(i+2).get(j); temp += arr.get(i+2).get(j+1); temp += arr.get(i+2).get(j+2); if(temp > sum ) { sum = temp; } } } System.out.println(sum); }
}
+ 0 comments public class Solution { public static void main(String[] args) throws IOException { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); List<List<Integer>> arr = new ArrayList<>(); IntStream.range(0, 6).forEach(i -> { try { arr.add( Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" ")) .map(Integer::parseInt) .collect(toList()) ); } catch (IOException ex) { throw new RuntimeException(ex); } }); bufferedReader.close(); int hourGlassMaxTotal = Integer.MIN_VALUE; for(int i = 0; i<=3; i++){ int j = 0; int iterationTotal = 0; List<Integer> firstRow = arr.get(i); List<Integer> secondRow = arr.get(i + 1); List<Integer> thirdRow = arr.get(i + 2); while(j<=3){ int x = (firstRow.subList(j, j+3)).stream().reduce(0,Integer::sum).intValue(); int y = (thirdRow.subList(j, j+3)).stream().reduce(0,Integer::sum).intValue(); iterationTotal = x + y; iterationTotal = iterationTotal + secondRow.get(j + 1); if(iterationTotal > hourGlassMaxTotal){ hourGlassMaxTotal = iterationTotal; } j++; } } System.out.println(hourGlassMaxTotal); } }
+ 1 comment python3
if __name__ == '__main__': arr = [] sums = [] for _ in range(6): arr.append(list(map(int, input().rstrip().split()))) for i in range(4): for j in range(4): current_sum = ( arr[i][j] + arr[i][j+1] + arr[i][j+2] + arr[i+1][j+1] + arr[i+2][j] + arr[i+2][j+1] + arr[i+2][j+2] ) sums.append(current_sum) max_sum = max(sums) print(max_sum)
+ 0 comments import math
import os
import random
import re
import sys
def hourglassSum(arr):
max_sum = float('-inf') for i in range(4): for j in range(4): current_sum = ( arr[i][j] + arr[i][j+1] + arr[i][j+2] + arr[i+1][j+1] + arr[i+2][j] + arr[i+2][j+1] + arr[i+2][j+2] ) max_sum = max(max_sum, current_sum) return max_sum
if name == 'main':
arr = [] for _ in range(6): arr.append(list(map(int, input().rstrip().split()))) result = hourglassSum(arr) print(result)
Load more conversations
Sort 1868 Discussions, By:
Please Login in order to post a comment