1. 문제
https://programmers.co.kr/learn/courses/30/lessons/12913#qna
코딩테스트 연습 - 땅따먹기
땅따먹기 게임을 하려고 합니다. 땅따먹기 게임의 땅(land)은 총 N행 4열로 이루어져 있고, 모든 칸에는 점수가 쓰여 있습니다. 1행부터 땅을 밟으며 한 행씩 내려올 때, 각 행의 4칸 중 한 칸만 밟
programmers.co.kr
2. 나의 풀이
import java.util.*;
class Solution {
int solution(int[][] land) {
for(int i = 1; i < land.length; i = i + 1) {
land[i][0] += Math.max(land[i-1][1], Math.max(land[i-1][2], land[i-1][3]));
land[i][1] += Math.max(land[i-1][0], Math.max(land[i-1][2], land[i-1][3]));
land[i][2] += Math.max(land[i-1][0], Math.max(land[i-1][1], land[i-1][3]));
land[i][3] += Math.max(land[i-1][0], Math.max(land[i-1][1], land[i-1][2]));
}
return Math.max(Math.max(land[land.length-1][0], land[land.length-1][1]), Math.max(land[land.length-1][2], land[land.length-1][3]));
}
}
3. 다른 사람 풀이
class Hopscotch {
int hopscotch(int[][] board, int size) {
return hopscotch(board, size, 0, -1);
}
private int hopscotch(int[][] board, int size, int y, int idx) {
if (y >= size) return 0;
int answer = Integer.MIN_VALUE;
for (int i = 0; i < 4; i++) {
if (i != idx) {
answer = Math.max(hopscotch(board, size, y + 1, i) + board[y][i], answer);
}
}
return answer;
}
public static void main(String[] args) {
Hopscotch c = new Hopscotch();
int[][] test = { { 1, 2, 3, 5 }, { 5, 6, 7, 8 }, { 4, 3, 2, 1 } };
//아래는 테스트로 출력해 보기 위한 코드입니다.
System.out.println(c.hopscotch(test, 3));
}
}
'알고리즘 공부 > 프로그래머스' 카테고리의 다른 글
(Java)프로그래머스 코딩테스트 연습 - 연습문제 - 숫자의 표현 (0) | 2020.11.22 |
---|---|
(Java)프로그래머스 코딩테스트 연습 - 찾아라 프로그래밍 마에스터 - 폰켓몬 (0) | 2020.11.22 |
(Java)프로그래머스 코딩테스트 연습 - 연습문제 - 다음 큰 숫자 (0) | 2020.11.20 |
(Java)프로그래머스 코딩테스트 연습 - 연습문제 - 올바른 괄호 (0) | 2020.11.20 |
(Java)프로그래머스 코딩테스트 연습 - 2017 카카오코드 본선 - 단체사진 찍기 (0) | 2020.11.18 |