[BaekJoon] #2751 - 수 정렬하기 2 [Java][C++]
[BaekJoon] #2751 - 수 정렬하기 2 [Java][C++]
1. 아이디어
주어진 $N$개의 수를 정렬하는 문제로 수의 개수가 최대 $1{,}000{,}000$개라서 $O(N^2)$보다 빠른 정렬 알고리즘을 활용해야 한다.
2. 복잡도
| 접근 | 시간 | 공간 |
|---|---|---|
| 풀이 | $O(N \log N)$ | $O(N)$ |
($N$ = 입력값 n)
3. 코드
풀이 [Java][C++]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
int n = Integer.parseInt(br.readLine());
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = Integer.parseInt(br.readLine());
}
Arrays.sort(arr);
for (int x : arr) {
sb.append(x).append("\n");
}
System.out.println(sb);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
vector<int> v(n);
for (int& x : v) cin >> x;
sort(v.begin(), v.end());
for (int x : v) {
cout << x << '\n';
}
}
This post is licensed under CC BY 4.0 by the author.