-
[해커랭크] 2D Array - DS #JAVAAlgorithm Solving/BAEKJOON 2021. 1. 27. 21:55
해커랭크 2D Array - DS
코드
내용(본문2)
import java.io.*; import java.math.*; import java.security.*; import java.text.*; import java.util.*; import java.util.concurrent.*; import java.util.regex.*; public class Solution { // Complete the hourglassSum function below. static int hourglassSum(int[][] arr) { int[] dx = {-1,0,1,-1,0,1}; int[] dy = {-1,-1,-1,1,1,1}; int max = Integer.MIN_VALUE; for(int i=1 ; i< 5 ; i++){ //y for(int j=1 ; j< 5 ; j++){ //x int temp = arr[i][j]; for(int k=0 ; k< 6 ; k++){ int x = j+dx[k]; int y = i+dy[k]; temp += arr[y][x]; } max = Math.max(temp,max); } } return max; } private static final Scanner scanner = new Scanner(System.in); public static void main(String[] args) throws IOException { BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH"))); int[][] arr = new int[6][6]; for (int i = 0; i < 6; i++) { String[] arrRowItems = scanner.nextLine().split(" "); scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?"); for (int j = 0; j < 6; j++) { int arrItem = Integer.parseInt(arrRowItems[j]); arr[i][j] = arrItem; } } int result = hourglassSum(arr); bufferedWriter.write(String.valueOf(result)); bufferedWriter.newLine(); bufferedWriter.close(); scanner.close(); } }
풀이
- x,y 의 순서는... arr[y][x] 입니다... ㅋㅋㅋ
잘못된 코드나 내용이 있다면 댓글을 남겨주세요. 즉시 수정하도록 하겠습니다! :)
'Algorithm Solving > BAEKJOON' 카테고리의 다른 글
[백준] 1874 스택 수열 #JAVA (0) 2021.01.30 [백준] 1158 요세푸스 #JAVA (0) 2021.01.29 [백준] 1406 에디터 #JAVA (0) 2021.01.24 [백준] 18258 큐2 #JAVA (0) 2021.01.24 [백준] 3273 두 수의 합 #JAVA (0) 2021.01.16