01-09 18:44
Recent Posts
Recent Comments
๊ด€๋ฆฌ ๋ฉ”๋‰ด

miinsun

[BAEKJOON] ๋ฐฑ์ค€ ๋ถ„ํ• ์ •๋ณต 11728 :: ๋ฐฐ์—ด ํ•ฉ์น˜๊ธฐ ๋ณธ๋ฌธ

Algorithm/Baekjoon

[BAEKJOON] ๋ฐฑ์ค€ ๋ถ„ํ• ์ •๋ณต 11728 :: ๋ฐฐ์—ด ํ•ฉ์น˜๊ธฐ

miinsun 2022. 4. 10. 00:04

 

๐Ÿ’ฌ  ๋ฌธ์ œ ์„ค๋ช…

์ •๋ ฌ๋˜์–ด์žˆ๋Š” ๋‘ ๋ฐฐ์—ด A์™€ B๊ฐ€ ์ฃผ์–ด์ง„๋‹ค.

๋‘ ๋ฐฐ์—ด์„ ํ•ฉ์นœ ๋‹ค์Œ ์ •๋ ฌํ•ด์„œ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

 

๐Ÿ”จ  ์ž…์ถœ๋ ฅ ์˜ˆ

์ž…๋ ฅ 

  • ์ฒซ์งธ ์ค„์— ๋ฐฐ์—ด A์˜ ํฌ๊ธฐ N, ๋ฐฐ์—ด B์˜ ํฌ๊ธฐ M์ด ์ฃผ์–ด์ง„๋‹ค. (1 ≤ N, M ≤ 1,000,000)
  • ๋‘˜์งธ ์ค„์—๋Š” ๋ฐฐ์—ด A์˜ ๋‚ด์šฉ์ด, ์…‹์งธ ์ค„์—๋Š” ๋ฐฐ์—ด B์˜ ๋‚ด์šฉ์ด ์ฃผ์–ด์ง„๋‹ค.
  • ๋ฐฐ์—ด์— ๋“ค์–ด์žˆ๋Š” ์ˆ˜๋Š” ์ ˆ๋Œ“๊ฐ’์ด 109๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ •์ˆ˜์ด๋‹ค.

 

์ถœ๋ ฅ

  • ์ฒซ์งธ ์ค„์— ๋‘ ๋ฐฐ์—ด์„ ํ•ฉ์นœ ํ›„ ์ •๋ ฌํ•œ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

 

์˜ˆ์ œ ์ž…๋ ฅ 1)

2 2
3 5
2 9

 

์˜ˆ์ œ ์ถœ๋ ฅ 1)

2 3 5 9

 

์˜ˆ์ œ ์ž…๋ ฅ 2)

2 1
4 7
1

 

์˜ˆ์ œ ์ถœ๋ ฅ 2)

1 4 7

โ€‹

์˜ˆ์ œ ์ž…๋ ฅ 3)

4 3
2 3 5 9
1 4 7

 

์˜ˆ์ œ ์ถœ๋ ฅ 3)

1 2 3 4 5 7 9

โ€‹

โ€‹

๐Ÿ’ป  Main.java

  • ํˆฌ ํฌ์ธํ„ฐ, ๋ถ„ํ•  ์ •๋ณต ๋ฌธ์ œ
  • ๊ฐ๊ฐ์˜ ํฌ์ธํ„ฐ i,j๋ฅผ ์ด์šฉํ•ด ๋ฐฐ์—ด์˜ ํฌ๊ธฐ๋ฅผ ๋น„๊ต ํ›„, ์˜ค๋ฆ„์ฐจ ์ˆœ ์ •๋ ฌํ•ด์ค€๋‹ค.
  • ํฌ๊ธฐ๋ฅผ ๋ชจ๋‘ ๋น„๊ตํ›„ ๋‚จ์€ ๋ฐฐ์—ด์„ ์ถœ๋ ฅํ•ด์ค€๋‹ค.
/* ๋ฐฑ์ค€ ๋ถ„ํ• ์ •๋ณต - 11728 :: ๋ฐฐ์—ด ํ•ฉ์น˜๊ธฐ */
import java.util.*;
import java.io.*;

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());
		int n = Integer.parseInt(st.nextToken());
		int m = Integer.parseInt(st.nextToken());
		
		int[] arr1 = new int[n];
		int[] arr2 = new int[m];
		
		st = new StringTokenizer(br.readLine());
		for(int k = 0; k < n; k++)
			arr1[k] = Integer.parseInt(st.nextToken());
		
		st = new StringTokenizer(br.readLine());
		for(int k = 0; k < m; k++)
			arr2[k] = Integer.parseInt(st.nextToken());
		
		int i = 0, j = 0;
		StringBuilder sb = new StringBuilder();
		while(i < n && j < m) {
			if(arr1[i] < arr2[j]) {
				sb.append(arr1[i] + " ");
				i++;
			}
			else {
				sb.append(arr2[j] + " ");
				j++;
			}
		}
		
		while(i < n) {
			sb.append(arr1[i] + " ");
			i++;
		}
		
		while(j < m) {
			sb.append(arr2[j] + " ");
			j++;
		}
		
		System.out.println(sb);
		
		br.close();
	}
}
Comments