728x90
1. 최소 직사각형
import java.util.*;
class Solution {
public int solution(int[][] sizes) {
//0. 주어지는 sizes 배열 형태는 int[n][2]
//1. 단순하게 배열 안의 두 숫자 중 큰 것끼리 비교, 작은 것끼리 비교
//5. big 배열, small 배열 선언
int[] big = new int[sizes.length];
int[] small = new int[sizes.length];
//2. 그럴려면 먼저 n만큼 반복되어야함
for (int i=0; i<sizes.length; i++) {
int a = sizes[i][0];
int b = sizes[i][1];
//3. a와 b중 더 큰 값을 big으로 정함
int bigNum = (a>=b) ? a : b;
int smallNum = (a<b) ? a : b;
//4. big은 big 배열에 넣고 small은 small 배열에 넣음
big[i] = bigNum;
small[i] = smallNum;
}
//6. 완성된 big, small 배열에서 max값을 꺼냄
int maxW = Arrays.stream(big).max().getAsInt();
int maxH= Arrays.stream(small).max().getAsInt();
//7. 지갑의 최소 크기
int answer = maxW * maxH;
return answer;
}
}
2. 타켓 넘버
import java.util.*;
class Solution {
/*0. 문제 이해하기: [부호1] [숫자2] [부호2] [숫자2] .. [부호5] [숫자5]
이 때 부호에는 +와 -만 들어감(2^5개의 경우의 수)
모든 경우의 수를 계산하고 그 중 target number와 같은 경우의 개수
*/
/*1. 더하기 빼기 반복 .. 인데 함수가 계속 반복되므로
한 배열을 함수 5번 반복하면 됨
근데 그럼 파람으로 몇 번째 인덱스 숫자인지랑 계산값도 필요하니까..
새로운 함수가 하나 더 필요함
*/
int index = 0;
int result = 0;
int answer = 0;
int target = 0;
//5. 입력받는 numbers와 target을 반복함수에 넣어줌
public int solution(int[] numbers, int target) {
addOrSubstract(numbers, target, 0, 0);
return answer;
}
//2. 함수 설정..
public void addOrSubstract(int[] arr, int tar, int index, int result) {
//4. 5개 다 했을 때
if(index == arr.length) {
//result가 target 값이면
if(result == tar) {
answer ++;
}
return; //종료
}//end of roop
//3. 5개 다 안한 상태면
addOrSubstract(arr, tar, index + 1, result + arr[index]); //더하거나
addOrSubstract(arr, tar, index + 1, result - arr[index]); //빼거나
}
}
3. 완주하지 못한 선수
import java.util.*;
class Solution {
public String solution(String[] participant, String[] completion) {
/*0. 문제 이해: participant 배열 - completion 배열 해서 남는거 출력
a b c 순서대로 정렬하고 두 개의 배열 앞에서부터 비교
다른 순간
a b c c c d : participant
a b c c d -> c 출력
a b c c c -> 다 같으므로 마지막이 문제
*/
String answer = "";
int check = 0;
//1. 두 배열 정렬
Arrays.sort(participant);
Arrays.sort(completion);
//2. 두 배열 비교
for (int i=0; i<participant.length-1; i++) {
//3. 두 개가 달라진 순간 !
if (!participant[i].equals(completion[i])) {
answer = participant[i];
check ++;
break;
}
}
//4. check가 0이다 = for문 안에서 다 같았다 = 마지막이 문제
if (check == 0) {
answer = participant[participant.length -1];
}
return answer;
}
}
4. 모의고사
import java.util.*;
class Solution {
public int[] solution(int[] answers) {
/*0. 문제 이해: answer과 1 2 3번의 답안지 비교해서 점수 내고,
점수가 제일 높은 사람 리턴
점수가 동일하면 오름차순 정렬
*/
//1. 1 2 3 번의 답안지 만들기
int[] one = new int[answers.length];
int[] two = new int[answers.length];
int[] three = new int[answers.length];
//1. 1번은 12345 반복
int test = 1;
for(int n=0; n<answers.length; n++) {
one[n] = test;
test ++;
if (test > 5) {
test = 1; //5가 되면 1로 초기화
}
}
//2. 2번은 짝수 인덱스에는 2, 홀수 인덱스에는 1 3 4 5 반복
two[0] = 2;
for(int n=0; n<answers.length; n++) {
if(n % 2 == 0) { //일단 짝수면 2
two[n] = 2;
} else if (n % 8 == 1) {
two[n] = 1;
} else if (n % 8 == 3) {
two[n] = 3;
} else if (n % 8 == 5) {
two[n] = 4;
} else if (n % 8 == 7) {
two[n] = 5;
}
}
//3. 3번은 33 11 22 44 55 반복
three[0] = 3;
for(int n=0; n<answers.length; n++) {
switch (n%10) {
case 0:
case 1:
three[n] = 3;break;
case 2:
case 3:
three[n] = 1;break;
case 4:
case 5:
three[n] = 2;break;
case 6:
case 7:
three[n] = 4;break;
case 8:
case 9:
three[n] = 5;break;
}
}
//4. 문제지 답안지 비교
int oneScore = 0;
int twoScore = 0;
int threeScore = 0;
for(int i=0; i< answers.length; i++) {
if (one[i] == answers[i]) {
oneScore ++;
}
if (two[i] == answers[i]) {
twoScore ++;
}
if (three[i] == answers[i]) {
threeScore ++;
}
}
//5. 최고점은
int highest = Math.max(oneScore, Math.max(twoScore, threeScore));
//6. 최고점과 동일한 점수를 가진 사람 수 세기
ArrayList<Integer> answerList = new ArrayList<>();
if(oneScore == highest) { answerList.add(1); }
if(twoScore == highest) { answerList.add(2); }
if(threeScore == highest) { answerList.add(3); }
Collections.sort(answerList);
//7. ArrayList를 Array로 변환하기..
int[] answer = new int[answerList.size()];
for (int i=0; i<answerList.size(); i++) {
answer[i] = answerList.get(i).intValue();
}
return answer;
}
}
728x90
'Algorithm Study > Programmers' 카테고리의 다른 글
[프로그래머스] 과일 장수 / 영어 끝말잇기 (Java) (0) | 2023.07.10 |
---|---|
[프로그래머스] 귤 고르기 / 삼총사 / 자연수 뒤집어 배열로 만들기 (Java) (0) | 2023.07.09 |
[프로그래머스] 체육복 / 가장 가까운 같은 글자 (Java) (0) | 2023.07.03 |
[프로그래머스] 소수찾기 / 의상 (Java) (0) | 2023.06.26 |
[프로그래머스] 폰켓몬 / 같은 숫자는 싫어 / K번째 수 (Java) (0) | 2023.06.05 |