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 int currSum=0, maxSum=Integer.MIN_VALUE; for(int i=0; i<arr.size()-2; i++){ for(int j=0; j<arr.size()-2; j++){ currSum = arr.get(i).get(j)+arr.get(i).get(j+1)+arr.get(i).get(j+2); currSum += arr.get(i+1).get(j+1); currSum += arr.get(i+2).get(j)+arr.get(i+2).get(j+1)+arr.get(i+2).get(j+2); maxSum = Math.max(currSum, maxSum); } currSum = 0; } System.out.println(maxSum);
+ 0 comments Pyhton3 solution
def houglass(): hourglass_list = [] for i in range(4): for j in range(4): r1 = arr[i][j] + arr[i][j+1] + arr[i][j+2] r2 = arr[i+1][j+1] r3 = arr[i+2][j] + arr[i+2][j+1] + arr[i+2][j+2] total = r1 + r2 + r3 hourglass_list.append(total) print(max(hourglass_list)) return houglass()
+ 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); } }); Iterator <List<Integer>> itrarrs= arr.iterator(); Integer [][] matrix=new Integer[arr.size()][arr.size()]; int i=0; int h=0; while(itrarrs.hasNext()){ Iterator <Integer> itrnums=itrarrs.next().iterator(); while(itrnums.hasNext()){ matrix[i][h]=itrnums.next(); h++; } h=0; i++; } int mayor=-99; int acum=0; int count=0; while(count<16){ for(int l=0;l < 4;l++){ for (int x = 0; x < 4; x++) { for (int y = 0; y < 3; y++) { acum+=matrix[l][y+x]; acum+=matrix[l+2][y+x]; } acum+=matrix[l+1][x+1]; if(acum>=mayor){ mayor=acum; } acum=0; count++; } count++; } } System.out.println(mayor); bufferedReader.close(); }
}
+ 0 comments JS
let maxSum = -Infinity; for(let i =0; i < arr.length - 2; i++) { for(let j = 0; j < arr.length - 2; j++) { let sum = arr[i][j] + arr[i+1][j+1] + arr[i+2][j] + arr[i][j+1] + arr[i+2][j+1] + arr[i][j+2] + arr[i+2][j+2]; if(maxSum < sum) { maxSum = sum; } } } console.log(maxSum);
+ 0 comments C++
int soma (int linha, vector<vector<int>> &arr){ int maior = 0; if(linha > 4){ return maior; } int h1 =0, h2=0, h3=0, h4=0; for (int i = linha; i < linha + 3; i++) { for (int j = 0; j < 6; j++) { if(j < 3 && i != (linha+1)){ h1 += arr[i][j]; h2 += arr[i][j+1]; h3 += arr[i][j+2]; h4 += arr[i][j+3]; } else if (i == (linha+1)){ h1 += arr[i][j+1]; h2 += arr[i][j+2]; h3 += arr[i][j+3]; h4 += arr[i][j+4]; j = 6; } else { j = 6; } } if (i == (linha +2)){ if(h1 > h2){ maior = h1; } else { maior = h2; } if (h3 > maior){ maior = h3; } if (h4 > maior){ maior = h4; } } } return maior; } int main() { vector<vector<int>> arr(6); 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; } } int maior_temp = 0, maior = 0; maior_temp = soma(0, arr); maior = soma(1, arr); if (maior < maior_temp){ maior = maior_temp; } for(int i = 2; i < 4; i ++){ maior_temp = soma(i, arr); if (maior < maior_temp){ maior = maior_temp; } } cout << maior; return 0; }
Load more conversations
Sort 1835 Discussions, By:
Please Login in order to post a comment