문제 설명
외과의사 머쓱이는 응급실에 온 환자의 응급도를 기준으로 진료 순서를 정하려고 합니다. 정수 배열 emergency가 매개변수로 주어질 때 응급도가 높은 순서대로 진료 순서를 정한 배열을 return하도록 solution 함수를 완성해주세요.
제한사항
- 중복된 원소는 없습니다.
- 1 ≤ emergency의 길이 ≤ 10
- 1 ≤ emergency의 원소 ≤ 100
입출력 예emergencyresult
[3, 76, 24] | [3, 1, 2] |
[1, 2, 3, 4, 5, 6, 7] | [7, 6, 5, 4, 3, 2, 1] |
[30, 10, 23, 6, 100] | [2, 4, 3, 5, 1] |
입출력 예 설명
입출력 예 #1
- emergency가 [3, 76, 24]이므로 응급도의 크기 순서대로 번호를 매긴 [3, 1, 2]를 return합니다.
입출력 예 #2
- emergency가 [1, 2, 3, 4, 5, 6, 7]이므로 응급도의 크기 순서대로 번호를 매긴 [7, 6, 5, 4, 3, 2, 1]를 return합니다.
입출력 예 #3
- emergency가 [30, 10, 23, 6, 100]이므로 응급도의 크기 순서대로 번호를 매긴 [2, 4, 3, 5, 1]를 return합니다.
import java.util.Arrays;
import java.util.Comparator;
import java.util.stream.Collectors;
class Solution {
public int[] solution(int[] e) {
return Arrays.stream(e).map(i -> Arrays.stream(e).boxed().sorted(Comparator.reverseOrder()).collect(Collectors.toList()).indexOf(i) + 1).toArray();
}
}
class Solution {
public int[] solution(int[] emergency) {
int[] answer = new int[emergency.length];
for(int i = 0; i < answer.length; i++){
if(answer[i] != 0){
continue;
}
int idx = 1;
for(int j = 0; j < answer.length; j++){
if(emergency[i] < emergency[j]){
idx++;
}
}
answer[i] = idx;
}
return answer;
}
}
하나하나 돌아가면서 자신보다 큰 지 확인하는것.
첫 if문은 없어도됨
import java.util.*;
class Solution {
public int[] solution(int[] emergency) {
int[] answer = new int[emergency.length];
int[] temp= new int[emergency.length];
System.arraycopy( emergency, 0, temp, 0, emergency.length);
Arrays.sort(temp);
for(int i=0;i<emergency.length;i++){
for(int j=0;j<emergency.length;j++){
if(emergency[i]==temp[j]) answer[i]=emergency.length-j;
}
}
return answer;
}
}
하나 하나 돌면서 같은 값을 찾고 그 인덱스를 활용
import java.util.*;
class Solution {
public int[] solution(int[] emergency) {
// max 값을 찾은 후 해당 인덱스 0로 변경하여 반복할 예정이므로, 배열 복사
int[] temp = emergency.clone();
// 응급도를 key로, 진료순서를 value로 저장하기 위한 Map(문제_중복원소 없음)
Map<Integer, Integer> map = new HashMap<>();
// 응급도 max를 찾기 위한 반복문
// max값 찾은 후, 해당 인덱스 값은 0으로 변경
// (응급도, 진료순위) 쌍을 Map에 저장
for(int i=1; i<=temp.length; i++) {
int max = temp[0];
int maxIdx = 0;
for(int j=1; j<temp.length; j++) {
if(temp[j] > max) {max = temp[j]; maxIdx = j;}
}
map.put(max, i);
temp[maxIdx] = 0;
}
// 원래의 emergency배열에 진료순위를 치환
for(int i=0; i< temp.length; i++){
emergency[i] = map.get(emergency[i]);
}
return emergency;
}
}
지워가면서 풀기
import java.util.*;
class Solution {
class Emergency implements Comparable<Emergency> {
int idx;
int val;
Emergency (int idx, int val) {
this.idx = idx;
this.val = val;
}
public int compareTo(Emergency other) {
return other.val - val;
}
}
public int[] solution(int[] emergency) {
Emergency[] arr = new Emergency[emergency.length];
for (int i = 0; i < emergency.length; ++i) {
arr[i] = new Emergency(i, emergency[i]);
}
Arrays.sort(arr);
for (int i = 0; i < arr.length; ++i) {
emergency[arr[i].idx] = i + 1;
}
return emergency;
}
}
클래스를활용
import java.util.*;
class Solution {
public int[] solution(int[] emergency) {
int len = emergency.length;
int[] answer = new int[len];
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < len; i++) {
map.put(emergency[i], i);
}
Arrays.sort(emergency);
for (int i = len-1; i >= 0; i--) {
answer[map.get(emergency[i])] = len - i;
}
return answer;
}
}
이게 가장 일반적인듯
출처:프로그래머스
'Java > Java 프로그래머스' 카테고리의 다른 글
잘라서 배열로 저장하기 (0) | 2022.11.17 |
---|---|
공던지기( 배열에서 한칸 띄고 이동) (0) | 2022.11.17 |
이진수 더하기 (0) | 2022.11.16 |
한번만 등장한 문자 (0) | 2022.11.15 |
가까운수 (배열에서 가장 가까운 수 찾기) (0) | 2022.11.15 |