์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ/BOJ_Java

[BOJ/Step12] 1427 : ์†ŒํŠธ์ธ์‚ฌ์ด๋“œ (JAVA)

NaNaRin๐Ÿ™ƒ 2021. 1. 30. 22:28

www.acmicpc.net/problem/1427

 

1427๋ฒˆ: ์†ŒํŠธ์ธ์‚ฌ์ด๋“œ

์ฒซ์งธ ์ค„์— ์ •๋ ฌํ•˜๊ณ ์žํ•˜๋Š” ์ˆ˜ N์ด ์ฃผ์–ด์ง„๋‹ค. N์€ 1,000,000,000๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ž์—ฐ์ˆ˜์ด๋‹ค.

www.acmicpc.net


๋ฌธ์ œ

๋ฐฐ์—ด์„ ์ •๋ ฌํ•˜๋Š” ๊ฒƒ์€ ์‰ฝ๋‹ค. ์ˆ˜๊ฐ€ ์ฃผ์–ด์ง€๋ฉด, ๊ทธ ์ˆ˜์˜ ๊ฐ ์ž๋ฆฌ์ˆ˜๋ฅผ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•ด๋ณด์ž.

 

์ž…๋ ฅ

์ฒซ์งธ ์ค„์— ์ •๋ ฌํ•˜๊ณ ์žํ•˜๋Š” ์ˆ˜ N์ด ์ฃผ์–ด์ง„๋‹ค. N์€ 1,000,000,000๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ž์—ฐ์ˆ˜์ด๋‹ค.

 

์ถœ๋ ฅ

์ฒซ์งธ ์ค„์— ์ž๋ฆฌ์ˆ˜๋ฅผ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•œ ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

 

์˜ˆ์ œ ์ž…๋ ฅ 1

2143

 

์˜ˆ์ œ ์ถœ๋ ฅ 1

4321


ํ’€์ด

๊ทธ๋ƒฅ ์ž…๋ ฅ๋ฐ›์€ ์ˆ˜๋ฅผ ํ•œ์ž๋ฆฌ์”ฉ ๋ถ„๋ฆฌํ•œ ํ›„ ์ •๋ ฌํ•˜์—ฌ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ค„๋ฐ”๊ฟˆ ์—†์ด ์ถœ๋ ฅํ•˜๋ฉด ๋œ๋‹ค

1. ์ฃผ์–ด์ง€๋Š” ์ˆ˜ n์„ String s์— ์ €์žฅํ•œ๋‹ค

2. s์˜ ๊ธธ์ด๋งŒํผ์˜ ๋ฐฐ์—ด์„ ๋งŒ๋“ค๊ณ , s๋ฅผ ํ•œ์ž๋ฆฌ์”ฉ ๋ถ„๋ฆฌํ•˜์—ฌ ์ €์žฅํ•œ๋‹ค.

   => charAt() , getNumericValue() ๋ฉ”์†Œ๋“œ ์‚ฌ์šฉ

3. ๋ฐฐ์—ด์„ ์ •๋ ฌํ•œ๋‹ค. 0~9๊นŒ์ง€์˜ ๋ฒ”์œ„๋ฐ–์— ์กด์žฌํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ์นด์šดํŒ… ์ •๋ ฌ์„ ์‚ฌ์šฉํ•˜์˜€์ง€๋งŒ ๋‹ค๋ฅธ ์ •๋ ฌ๋„ ์ƒ๊ด€ ์—†๋‹ค

4. ์ •๋ ฌ๋œ ๋ฐฐ์—ด์„ ๊ฑฐ๊พธ๋กœ ์ถœ๋ ฅํ•œ๋‹ค.

 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class B1427 {
	
	static void countingSort(int[] a, int n) {
		int max = a[0];
		for(int i = 0; i < n; i++) {
			if(a[i] > max)
				max = a[i];
		}
		
		int[] counting = new int[max+1];
		int[] b = new int[n];
		
		for(int i = 0; i < n; i++) counting[a[i]]++;
		for(int i = 1; i <= max; i++) counting[i] += counting[i-1];
		for(int i = n-1; i >= 0; i--) b[--counting[a[i]]] = a[i];
		for(int i = 0; i < n; i++) a[i] = b[i];
	}

	public static void main(String[] args) throws NumberFormatException, IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

		String s = br.readLine();
		int[] num = new int[s.length()];

		for(int i = 0; i < num.length; i++) {
			num[i] = Character.getNumericValue(s.charAt(i));
		}
		
		countingSort(num, num.length);
		
		for(int i = num.length-1; i >= 0; i--) {
			System.out.print(num[i]);
		}
	}
}