Hi, There!
안녕하세요, 바오밥입니다.
목차
- 문제
- 풀이
문제
문제 내용
https://school.programmers.co.kr/learn/courses/30/lessons/181851
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
풀이
나의 풀이
- HashMap, ArrayList 자료구조 활용
import java.util.HashMap;
import java.util.Collections;
import java.util.ArrayList;
class Solution {
public int solution(int[] rank, boolean[] attendance) {
// K : rank
// V : rankIndex
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
ArrayList<Integer> intList = new ArrayList<Integer>();
for(int i=0; i<rank.length; i++) map.put(rank[i], i);
for(int i=0; i<rank.length; i++)
if(attendance[i])
intList.add(rank[i]);
Collections.sort(intList);
int a = map.getOrDefault(intList.get(0), 0);
int b = map.getOrDefault(intList.get(1), 0);
int c = map.getOrDefault(intList.get(2), 0);
int answer = a*10000 + b*100 + c;
return answer;
}
}
다른 사람의 풀이
- PriorityQueue 자료구조 활용
import java.util.PriorityQueue;
class Solution {
public int solution(int[] rank, boolean[] attendance) {
PriorityQueue<Student> que = new PriorityQueue<>();
for (int i = 0;i < attendance.length;i++) {
if (attendance[i])
que.add(new Student(i, rank[i]));
}
return 10000 * que.poll().index + 100 * que.poll().index + que.poll().index;
}
public class Student implements Comparable<Student> {
int index;
int number;
public Student(int index, int number) {
this.index = index;
this.number = number;
}
public int getNumber() {
return this.number;
}
@Override
public int compareTo(Student obj) {
if (this.number > obj.getNumber()) return 1;
else if (this.number < obj.getNumber()) return -1;
return 0;
}
}
}
'Dev > PS' 카테고리의 다른 글
[프로그래머스-코딩 기초 트레이닝] 문자열 정수의 합 (0) | 2023.06.15 |
---|---|
[프로그래머스-코딩 기초 트레이닝] 정수 부분 (0) | 2023.06.15 |
[프로그래머스-코딩 기초 트레이닝] 뒤에서 5등 위로 (0) | 2023.06.15 |
[프로그래머스-코딩 기초 트레이닝] 뒤에서 5등까지 (0) | 2023.06.14 |
[프로그래머스-코딩 기초 트레이닝] 배열의 길이에 따라 다른 연산하기 (0) | 2023.06.14 |