[Programmers] ๐Ÿ’ก๋ฌธ์ž์—ด ๋‚ด ๋งˆ์Œ๋Œ€๋กœ ์ •๋ ฌํ•˜๊ธฐ๐Ÿ’ก

Jay Mild Leeยท2022๋…„ 11์›” 22์ผ
0

Algorithm Problems

๋ชฉ๋ก ๋ณด๊ธฐ
10/16

I. ๋ฌธ์ž์—ด ๋‚ด ๋งˆ์Œ๋Œ€๋กœ ์ •๋ ฌํ•˜๊ธฐ

https://school.programmers.co.kr/learn/courses/30/lessons/12915

1. ๋ฌธ์ œ ์„ค๋ช…

๋ฌธ์ž์—ด๋กœ ๊ตฌ์„ฑ๋œ ๋ฆฌ์ŠคํŠธ strings์™€, ์ •์ˆ˜ n์ด ์ฃผ์–ด์กŒ์„ ๋•Œ, ๊ฐ ๋ฌธ์ž์—ด์˜ ์ธ๋ฑ์Šค n๋ฒˆ์งธ ๊ธ€์ž๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌํ•˜๋ ค ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด strings๊ฐ€ ["sun", "bed", "car"]์ด๊ณ  n์ด 1์ด๋ฉด ๊ฐ ๋‹จ์–ด์˜ ์ธ๋ฑ์Šค 1์˜ ๋ฌธ์ž "u", "e", "a"๋กœ strings๋ฅผ ์ •๋ ฌํ•ฉ๋‹ˆ๋‹ค.

2. ์ œํ•œ ์‚ฌํ•ญ

  • strings๋Š” ๊ธธ์ด 1 ์ด์ƒ, 50์ดํ•˜์ธ ๋ฐฐ์—ด์ž…๋‹ˆ๋‹ค.
  • strings์˜ ์›์†Œ๋Š” ์†Œ๋ฌธ์ž ์•ŒํŒŒ๋ฒณ์œผ๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค.
  • strings์˜ ์›์†Œ๋Š” ๊ธธ์ด 1 ์ด์ƒ, 100์ดํ•˜์ธ ๋ฌธ์ž์—ด์ž…๋‹ˆ๋‹ค.
  • ๋ชจ๋“  strings์˜ ์›์†Œ์˜ ๊ธธ์ด๋Š” n๋ณด๋‹ค ํฝ๋‹ˆ๋‹ค.
  • ์ธ๋ฑ์Šค 1์˜ ๋ฌธ์ž๊ฐ€ ๊ฐ™์€ ๋ฌธ์ž์—ด์ด ์—ฌ๋Ÿฟ ์ผ ๊ฒฝ์šฐ, ์‚ฌ์ „์ˆœ์œผ๋กœ ์•ž์„  ๋ฌธ์ž์—ด์ด ์•ž์ชฝ์— ์œ„์น˜ํ•ฉ๋‹ˆ๋‹ค.

3. ํ’€์ด

์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด๋“ค์˜ ์ผ์ •ํ•œ ์ธ๋ฑ์Šค์— ์œ„์น˜ํ•œ ๊ธ€์ž๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•˜๋Š” ๋ฌธ์ œ. ์—ฌ๋Ÿฌ ๋ฐฉ๋ฒ•์ด ์žˆ๊ฒ ์ง€๋งŒ, Comparable Interface์™€ Class๋ฅผ ์‚ฌ์šฉํ•ด ๊ตฌํ˜„ํ•˜๊ณ ์ž ํ–ˆ๋‹ค.

๊ฒฐ๋ก ์ ์œผ๋กœ, ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ง„ํ–‰ํ–ˆ๋‹ค.

  1. class StringSet๋ฅผ Comparable์„ implementํ•ด ์„ ์–ธํ•œ๋‹ค.
  2. class์˜ field๋Š” n๋ฒˆ์งธ ๋ฌธ์ž์˜ ์•„์Šคํ‚ค ์ฝ”๋“œ๋ฅผ ์ €์žฅํ•˜๋Š” int asci์™€ ์ „์ฒด ๋ฌธ์ž์—ด์„ ์ €์žฅํ•˜๋Š” String str๋กœ ๊ตฌ์„ฑ๋œ๋‹ค.
  3. @Override๋ฅผ ํ†ตํ•ด compareTo์˜ ๋กœ์ง์„ ์ˆ˜์ •ํ•œ๋‹ค.
        @Override
        public int compareTo(Object o) {
            StringSet s = (StringSet) o;
            if(this.asci>s.asci){
            	// ํ˜„์žฌ ๋ณ€์ˆ˜์˜ asci๊ฐ€ ๋น„๊ต ๋Œ€์ƒ ๋ณ€์ˆ˜์˜ asci๋ณด๋‹ค ํด ๋•Œ 1 ๋ฆฌํ„ด
                return 1;
            }
            else if(this.asci<s.asci){
            	// ํ˜„์žฌ ๋ณ€์ˆ˜์˜ asci๊ฐ€ ๋น„๊ต ๋Œ€์ƒ ๋ณ€์ˆ˜์˜ asci๋ณด๋‹ค ์ž‘์„ ๋•Œ 1 ๋ฆฌํ„ด
                return -1;
            }
            else{
            	// ๊ฐ’์ด ๊ฐ™์€ ๊ฒฝ์šฐ str๋ผ๋ฆฌ compareTo๋ฅผ ์‚ฌ์šฉํ•ด ๋‹ค์‹œ ๋น„๊ต
                return str.compareTo(s.str);
            }
        }
  1. List<StringSet> listarr์„ ์„ ์–ธํ•˜๊ณ , ์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด ๋ฐฐ์—ด์˜ ๋ฌธ์ž์—ด๋งˆ๋‹ค StringSet์˜ ๊ตฌ์กฐ์— ๋งž์ถฐ ๋ณ€ํ™˜ํ•ด ์‚ฝ์ž…ํ•œ๋‹ค.
  2. Collections.sort(listarr)์„ ํ†ตํ•ด ์žฌ์ •์˜๋œ compareTo๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๊ฐ’๋“ค์„ ์ •๋ ฌํ•œ๋‹ค.
  3. ์ •๋ ฌ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

4. ์†Œ์Šค ์ฝ”๋“œ

import java.util.*;
import java.lang.*;

class Solution {
    static class StringSet implements Comparable {
        // field
        int asci;
        String str;

        // constructor
        public StringSet(int asci, String str){
            this.asci = asci;
            this.str = str;
        }

        @Override
        public int compareTo(Object o) {
            StringSet s = (StringSet) o;
            if(this.asci>s.asci){
                return 1;
            }
            else if(this.asci<s.asci){
                return -1;
            }
            else{
                return str.compareTo(s.str);
            }
        }
    }

    static public String[] solution(String[] strings, int n) {
        String[] answer = {};
        List<StringSet> listarr = new ArrayList<>();
        for (int i = 0; i < strings.length; i++) {
            listarr.add(new StringSet(strings[i].charAt(n), strings[i]));
        }
        Collections.sort(listarr);
        answer = new String[strings.length];
        for (int i = 0; i < answer.length; i++) {
            answer[i] = listarr.get(i).str;
        }
        return answer;
    }
}

0๊ฐœ์˜ ๋Œ“๊ธ€