Baekjoon - 11870

Tadap·2023년 10월 11일
0

Baekjoon

목록 보기
49/94
post-thumbnail
post-custom-banner

문제

Solved.ac Class3+

1차시도

public class Main {
	public static void main(String[] args) throws Exception{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringBuilder sb = new StringBuilder();
		int size = Integer.parseInt(br.readLine());
		int[] data = new int[size];

		String[] stringData = br.readLine().split(" ");
		for (int i = 0; i < size; i++) {
			data[i] = Integer.parseInt(stringData[i]);
		}
		int[] copyData = Arrays.copyOf(data, data.length);
		Arrays.sort(copyData);
		Map<Integer, Integer> rankMap = new HashMap<>();

		int count = 0;
		for (int i = 0; i < size; i++) {
			if (!rankMap.containsKey(copyData[i])) {
				rankMap.put(copyData[i], count++);
			}
		}

		for (int i = 0; i < size; i++) {
			sb.append(rankMap.get(data[i])).append(" ");
		}
		System.out.println(sb);
	}
}

성공

ToKotlin

fun main() {
    print(MainToKotlin().solve())
}
class MainToKotlin {
    fun solve() :String{
        val stringBuilder = StringBuilder()
        val size = readln().toInt()

        val data = mutableListOf<Int>()

        val stringData = readln().split(" ")
        for (d in stringData) {
            data.add(d.toInt())
        }

        val sortedData = data.sorted()

        val rankMap = mutableMapOf<Int, Int>()

        var count = 0

        for (i in 0..<size) {
            if (!rankMap.containsKey(sortedData[i])) {
                rankMap.put(sortedData[i], count++)
            }
        }

        for (i in 0..<size) {
            stringBuilder.append(rankMap[data[i]]).append(" ")
        }

        return stringBuilder.toString()
    }
}

post-custom-banner

0개의 댓글