Notice
Recent Posts
Recent Comments
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 백준 수정렬하기
- 백준 2566 자바
- 백준 2738 행렬덧셈
- 배열
- 백준 행렬덧셈
- 자바
- Intermediate Device
- 백준 2738
- 백준 2587
- 백준 2750 자바
- 백준 25305번
- 백준 2738번
- 백준 5597 자바
- 백준 2738 자바
- 백준 최댓값 2566
- 백준 대표값2
- 백준 25305번 커트라인
- 백준 커트라인
- counting sort java
- RFC1918
- 백준
- 백준 25305번 커트라인 자바
- 백준 과제안내신분 자바
- 네트워크
- 백준 2587 자바
- 카운팅배열 자바
- 백준 대표값2 자바
- LAN port
- 기본수학2
- 카운팅배열
Archives
- Today
- Total
ddubi
프로그래머스 : 땅따먹기 Java 본문
https://school.programmers.co.kr/learn/courses/30/lessons/12913
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
틀린 풀이
class Solution {
static int dontStep = -1;
static int solution(int[][] land) {
int answer = 0;
for(int i=0; i<land.length; i++) {
int max = maxStep(land[i]);
answer += max;
System.out.println("max : " + max);
}
return answer;
}
private static int maxStep(int[] arr) {
int max = -1;
int idx = -1;
// 처음 시작인 경우
if(dontStep == -1) {
for(int i=0; i<arr.length; i++) {
if(arr[i]>max) {
max = arr[i];
idx = i;
}
}
}
// 처음 시작이 아닌 경우
else {
for(int i=0; i<arr.length; i++) {
if(i != dontStep && arr[i]>max) {
max = arr[i];
idx = i;
}
}
}
dontStep = idx;
return max;
}
}
올바른 풀이
import java.util.Arrays;
class Solution {
public static void main(String[] args) {
int[][] arr = {{1,2,3,5},{5,6,7,8},{4,3,2,1}};
System.out.println(solution(arr));
}
static int solution(int[][] land) {
for(int i=1; i<land.length; i++) {
land[i][0] += Math.max(Math.max(land[i-1][1], land[i-1][2]), land[i-1][3]);
land[i][1] += Math.max(Math.max(land[i-1][0], land[i-1][2]), land[i-1][3]);
land[i][2] += Math.max(Math.max(land[i-1][1], land[i-1][0]), land[i-1][3]);
land[i][3] += Math.max(Math.max(land[i-1][1], land[i-1][2]), land[i-1][0]);
}
int[] answerArr = land[land.length-1];
Arrays.sort(answerArr);
int answer = answerArr[answerArr.length-1];
return answer;
}
}
처음 배열부터 큰수를 찾아서 간다면 뒤에 반전이 있을 수 있는 경우를 무시하는것이 된다.
그렇다고 모든 경우를 따지기엔 효율성 문제가 생긴다.
불가능한 열을 제외한 윗줄의 가장 큰 수를 더해준다.
마지막 행까지 반복해 마지막 행의 가장 큰 수로 답을 도출 할 수 있다.
'코테 문제풀이' 카테고리의 다른 글
백준 11047 : 동전 0 (Java) (0) | 2022.10.25 |
---|---|
프로그래머스 : 오랜기간 보호한 동물(2) (0) | 2022.10.25 |
백준 19583 : 사이버 개강총회 (java) (0) | 2022.10.21 |
백준 16165 : 걸그룹 마스터 준석이 java (0) | 2022.10.21 |
백준 1764 : 듣보잡 (Java) (0) | 2022.10.21 |
Comments