```
백준 11728번 정렬된 배열 합치기 JAVA 구현해보기
```

이번 글을 통해 배워갈 내용
- 백준 11728번 풀이
https://www.acmicpc.net/problem/11728
11728번: 배열 합치기
첫째 줄에 배열 A의 크기 N, 배열 B의 크기 M이 주어진다. (1 ≤ N, M ≤ 1,000,000) 둘째 줄에는 배열 A의 내용이, 셋째 줄에는 배열 B의 내용이 주어진다. 배열에 들어있는 수는 절댓값이 109보다 작거
www.acmicpc.net
백준 11728번 정렬된 배열 합치 기는
난이도 실버 등급의 문제로서
(1부터 백만까지의 크기를 가질 수 있는) 두 개의 정렬된 배열을 정렬해서 합치면 되는 문제입니다.
30분 정도 위에 링크를 방문하셔서 풀어보시고
안 풀리시는 경우에만 아래 해답을 봐주시면 감사하겠습니다.
정렬된 배열을 입력받고
한 자리씩 순서대로 정렬되게 출력해주면 되는 간단한 문제입니다.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
StringBuilder sb = new StringBuilder();
// 배열을 입력받는다.
int arraySizeA = Integer.parseInt(st.nextToken());
int arraySizeB = Integer.parseInt(st.nextToken());
List<Integer> listA = new ArrayList<>();
List<Integer> listB = new ArrayList<>();
st = new StringTokenizer(br.readLine());
while (st.hasMoreTokens()) {
listA.add(Integer.parseInt(st.nextToken()));
}
st = new StringTokenizer(br.readLine());
while (st.hasMoreTokens()) {
listB.add(Integer.parseInt(st.nextToken()));
}
// 두 정렬된 배열을 Merge Sort 한다.
int idxA = 0;
int idxB = 0;
while (arraySizeA > idxA && arraySizeB > idxB) {
final int firstElemOfListA = listA.get(idxA);
final int firstElemOfListB = listB.get(idxB);
if (firstElemOfListA > firstElemOfListB) {
sb.append(firstElemOfListB).append(" ");
idxB++;
} else {
sb.append(firstElemOfListA).append(" ");
idxA++;
}
}
while (arraySizeA > idxA) {
final int firstElemOfListA = listA.get(idxA);
sb.append(firstElemOfListA).append(" ");
idxA++;
}
while (arraySizeB > idxB) {
final int firstElemOfListB = listB.get(idxB);
sb.append(firstElemOfListB).append(" ");
idxB++;
}
// 출력한다.
sb.setLength(sb.length() - 1);
System.out.print(sb);
}
}
읽어주셔서 감사합니다
무엇인가 얻어가셨기를 바라며
오늘도 즐거운 코딩 하시길 바랍니다 ~ :)
'Java > Java 알고리즘' 카테고리의 다른 글
백준 2010번 플러그 JAVA 구현해보기 (0) | 2022.01.24 |
---|---|
백준 1676번 팩토리얼 0의 개수 JAVA 구현해보기 (0) | 2022.01.19 |
백준 13701번 중복제거 구현해보기 (0) | 2022.01.04 |
백준 10808번 알파벳 개수 구현해보기 (0) | 2021.12.29 |
백준 3285번 DECODE JAVA 구현해보기 (0) | 2021.12.26 |
```
백준 11728번 정렬된 배열 합치기 JAVA 구현해보기
```

이번 글을 통해 배워갈 내용
- 백준 11728번 풀이
https://www.acmicpc.net/problem/11728
11728번: 배열 합치기
첫째 줄에 배열 A의 크기 N, 배열 B의 크기 M이 주어진다. (1 ≤ N, M ≤ 1,000,000) 둘째 줄에는 배열 A의 내용이, 셋째 줄에는 배열 B의 내용이 주어진다. 배열에 들어있는 수는 절댓값이 109보다 작거
www.acmicpc.net
백준 11728번 정렬된 배열 합치 기는
난이도 실버 등급의 문제로서
(1부터 백만까지의 크기를 가질 수 있는) 두 개의 정렬된 배열을 정렬해서 합치면 되는 문제입니다.
30분 정도 위에 링크를 방문하셔서 풀어보시고
안 풀리시는 경우에만 아래 해답을 봐주시면 감사하겠습니다.
정렬된 배열을 입력받고
한 자리씩 순서대로 정렬되게 출력해주면 되는 간단한 문제입니다.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
StringBuilder sb = new StringBuilder();
// 배열을 입력받는다.
int arraySizeA = Integer.parseInt(st.nextToken());
int arraySizeB = Integer.parseInt(st.nextToken());
List<Integer> listA = new ArrayList<>();
List<Integer> listB = new ArrayList<>();
st = new StringTokenizer(br.readLine());
while (st.hasMoreTokens()) {
listA.add(Integer.parseInt(st.nextToken()));
}
st = new StringTokenizer(br.readLine());
while (st.hasMoreTokens()) {
listB.add(Integer.parseInt(st.nextToken()));
}
// 두 정렬된 배열을 Merge Sort 한다.
int idxA = 0;
int idxB = 0;
while (arraySizeA > idxA && arraySizeB > idxB) {
final int firstElemOfListA = listA.get(idxA);
final int firstElemOfListB = listB.get(idxB);
if (firstElemOfListA > firstElemOfListB) {
sb.append(firstElemOfListB).append(" ");
idxB++;
} else {
sb.append(firstElemOfListA).append(" ");
idxA++;
}
}
while (arraySizeA > idxA) {
final int firstElemOfListA = listA.get(idxA);
sb.append(firstElemOfListA).append(" ");
idxA++;
}
while (arraySizeB > idxB) {
final int firstElemOfListB = listB.get(idxB);
sb.append(firstElemOfListB).append(" ");
idxB++;
}
// 출력한다.
sb.setLength(sb.length() - 1);
System.out.print(sb);
}
}
읽어주셔서 감사합니다
무엇인가 얻어가셨기를 바라며
오늘도 즐거운 코딩 하시길 바랍니다 ~ :)
'Java > Java 알고리즘' 카테고리의 다른 글
백준 2010번 플러그 JAVA 구현해보기 (0) | 2022.01.24 |
---|---|
백준 1676번 팩토리얼 0의 개수 JAVA 구현해보기 (0) | 2022.01.19 |
백준 13701번 중복제거 구현해보기 (0) | 2022.01.04 |
백준 10808번 알파벳 개수 구현해보기 (0) | 2021.12.29 |
백준 3285번 DECODE JAVA 구현해보기 (0) | 2021.12.26 |