[Programmers] #181885 - 할 일 목록 [Java][C++][Python]
할 일 목록과 각 항목의 완료 여부가 주어질 때, 아직 끝내지 못한 항목만 원래 순서대로 추린 목록을 구하는 워밍업 문제.
[Programmers] #181885 - 할 일 목록 [Java][C++][Python]
1. 아이디어
todo_list와 finished를 인덱스를 맞춰 함께 순회하며 finished가 거짓인 항목만 원래 순서대로 결과 배열에 담는다.
2. 복잡도
| 접근 | 시간 | 공간 |
|---|---|---|
| 풀이 | $O(N)$ | $O(N)$ |
($N$ = todo_list의 길이)
3. 코드
풀이 [Java][C++][Python]
1
2
3
4
5
6
7
8
9
10
11
12
import java.util.*;
class Solution {
public String[] solution(String[] todo_list, boolean[] finished) {
List<String> list = new ArrayList<>();
for (int i = 0; i < finished.length; i++) {
if (!finished[i]) list.add(todo_list[i]);
}
return list.toArray(new String[0]);
}
}
1
2
3
4
5
6
7
8
9
10
11
#include <bits/stdc++.h>
using namespace std;
vector<string> solution(vector<string> todo_list, vector<bool> finished) {
vector<string> v;
for (int i = 0; i < todo_list.size(); i++) {
if (!finished[i]) v.push_back(todo_list[i]);
}
return v;
}
1
2
def solution(todo_list, finished):
return [todo for todo, done in zip(todo_list, finished) if not done]
This post is licensed under CC BY 4.0 by the author.