Notice
Recent Posts
Recent Comments
Link
나의 개발일지
[프로그래머스] Lv.2 프로세스 [Python, Java] 본문
파이썬
def solution(priorities, location):
queue = [(i,p) for i,p in enumerate(priorities)] # 인덱스와 값 같이 저장
answer = 0
while True:
cur = queue.pop(0)
if any(cur[1] < q[1] for q in queue): # 큰 값이 하나라도 있으면
queue.append(cur) # 다시 추가
else: # 큰 값이 없으면
answer += 1 # 카운트 증가 (빼냄)
if cur[0] == location: # 이게 타겟 값이면
return answer # 정답 리턴
자바
import java.util.*;
class Solution {
public int solution(int[] priorities, int location) {
int answer = 1;
List<Integer> queue = new LinkedList<>();
int target = priorities[location]; // 타겟 정보 저장
priorities[location] = 0; // 큐에 있을 수 없는 값으로 변경
for (int n : priorities) { // 리스트에 복사
queue.add(n);
}
while (!queue.isEmpty()) {
int now = queue.remove(0);
int check = 0;
for (int i = 0; i < queue.size(); i++) {
if (now == 0) { // 현재가 타겟일 때는
if (target < queue.get(i)) { // 저장한 타겟 값과 비교
queue.add(now); check = 1; break;
} else continue;
} else if (queue.get(i) == 0 && now < target) { // 타겟과 만나면 타겟 값으로 비교
queue.add(now); check = 1; break;
} else if (now < queue.get(i)) { // 일반적으로 비교
queue.add(now); check = 1; break;
}
}
if (check == 0 && now == 0) return answer; // add가 일어나지 않고 현재가 타겟이면 리턴
if (check == 0) answer++; // add가 일어나지 않고 현재가 타겟이 아니면 숫자만 올림
}
return answer;
}
}'프로그래머스' 카테고리의 다른 글
| [프로그래머스] Lv.3 순위 [Python, 파이썬] (0) | 2023.07.10 |
|---|---|
| [프로그래머스] Lv.2 더 맵게 [Python, 파이썬] (0) | 2023.07.09 |
| [프로그래머스] Lv. 2 의상 [Python, Java] (0) | 2023.06.29 |
| [프로그래머스] Lv.1 완주하지 못한 선수 [Python, Java] (0) | 2023.06.29 |
| [프로그래머스] Lv.1 폰켓몬 [Python, Java] (0) | 2023.06.29 |
Comments