Back-end/Algorithm

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์ถ”์–ต ์ ์ˆ˜ (java)

Nellie Kim 2023. 8. 19. 23:25
728x90

๐Ÿ’‍โ™€๏ธ  ๋ฌธ์ œ ์„ค๋ช…

์‚ฌ์ง„๋“ค์„ ๋ณด๋ฉฐ ์ถ”์–ต์— ์ –์–ด ์žˆ๋˜ ๋ฃจ๋Š” ์‚ฌ์ง„๋ณ„๋กœ ์ถ”์–ต ์ ์ˆ˜๋ฅผ ๋งค๊ธธ๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์‚ฌ์ง„ ์†์— ๋‚˜์˜ค๋Š” ์ธ๋ฌผ์˜ ๊ทธ๋ฆฌ์›€ ์ ์ˆ˜๋ฅผ ๋ชจ๋‘ ํ•ฉ์‚ฐํ•œ ๊ฐ’์ด ํ•ด๋‹น ์‚ฌ์ง„์˜ ์ถ”์–ต ์ ์ˆ˜๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์‚ฌ์ง„ ์† ์ธ๋ฌผ์˜ ์ด๋ฆ„์ด ["may", "kein", "kain"]์ด๊ณ  ๊ฐ ์ธ๋ฌผ์˜ ๊ทธ๋ฆฌ์›€ ์ ์ˆ˜๊ฐ€ [5์ , 10์ , 1์ ]์ผ ๋•Œ ํ•ด๋‹น ์‚ฌ์ง„์˜ ์ถ”์–ต ์ ์ˆ˜๋Š” 16(5 + 10 + 1)์ ์ด ๋ฉ๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ์‚ฌ์ง„ ์† ์ธ๋ฌผ์˜ ์ด๋ฆ„์ด ["kali", "mari", "don", "tony"]์ด๊ณ  ["kali", "mari", "don"]์˜ ๊ทธ๋ฆฌ์›€ ์ ์ˆ˜๊ฐ€ ๊ฐ๊ฐ [11์ , 1์ , 55์ ]]์ด๊ณ , "tony"๋Š” ๊ทธ๋ฆฌ์›€ ์ ์ˆ˜๊ฐ€ ์—†์„ ๋•Œ, ์ด ์‚ฌ์ง„์˜ ์ถ”์–ต ์ ์ˆ˜๋Š” 3๋ช…์˜ ๊ทธ๋ฆฌ์›€ ์ ์ˆ˜๋ฅผ ํ•ฉํ•œ 67(11 + 1 + 55)์ ์ž…๋‹ˆ๋‹ค.

๊ทธ๋ฆฌ์›Œํ•˜๋Š” ์‚ฌ๋žŒ์˜ ์ด๋ฆ„์„ ๋‹ด์€ ๋ฌธ์ž์—ด ๋ฐฐ์—ด name, ๊ฐ ์‚ฌ๋žŒ๋ณ„ ๊ทธ๋ฆฌ์›€ ์ ์ˆ˜๋ฅผ ๋‹ด์€ ์ •์ˆ˜ ๋ฐฐ์—ด yearning, ๊ฐ ์‚ฌ์ง„์— ์ฐํžŒ ์ธ๋ฌผ์˜ ์ด๋ฆ„์„ ๋‹ด์€ ์ด์ฐจ์› ๋ฌธ์ž์—ด ๋ฐฐ์—ด photo๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ, ์‚ฌ์ง„๋“ค์˜ ์ถ”์–ต ์ ์ˆ˜๋ฅผ photo์— ์ฃผ์–ด์ง„ ์ˆœ์„œ๋Œ€๋กœ ๋ฐฐ์—ด์— ๋‹ด์•„ returnํ•˜๋Š” solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.


โŒ ์ œํ•œ์‚ฌํ•ญ

  • 3 ≤ name์˜ ๊ธธ์ด = yearning์˜ ๊ธธ์ด≤ 100
    • 3 ≤ name์˜ ์›์†Œ์˜ ๊ธธ์ด ≤ 7
    • name์˜ ์›์†Œ๋“ค์€ ์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž๋กœ๋งŒ ์ด๋ฃจ์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค.
    • name์—๋Š” ์ค‘๋ณต๋œ ๊ฐ’์ด ๋“ค์–ด๊ฐ€์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
    • 1 ≤ yearning[i] ≤ 100
    • yearning[i]๋Š” i๋ฒˆ์งธ ์‚ฌ๋žŒ์˜ ๊ทธ๋ฆฌ์›€ ์ ์ˆ˜์ž…๋‹ˆ๋‹ค.
  • 3 ≤ photo์˜ ๊ธธ์ด ≤ 100
    • 1 ≤ photo[i]์˜ ๊ธธ์ด ≤ 100
    • 3 ≤ photo[i]์˜ ์›์†Œ(๋ฌธ์ž์—ด)์˜ ๊ธธ์ด ≤ 7
    • photo[i]์˜ ์›์†Œ๋“ค์€ ์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž๋กœ๋งŒ ์ด๋ฃจ์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค.
    • photo[i]์˜ ์›์†Œ๋“ค์€ ์ค‘๋ณต๋œ ๊ฐ’์ด ๋“ค์–ด๊ฐ€์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

 

 

๐Ÿ’ก  ์ž…์ถœ๋ ฅ ์˜ˆ

 

 

์ž…์ถœ๋ ฅ ์˜ˆ #1

  • ์ฒซ ๋ฒˆ์งธ ์‚ฌ์ง„ ์† "may", "kein", "kain", "radi"์˜ ๊ทธ๋ฆฌ์›€ ์ ์ˆ˜๋ฅผ ํ•ฉ์น˜๋ฉด 19(5 + 10 + 1 + 3)์  ์ž…๋‹ˆ๋‹ค. ๋‘ ๋ฒˆ์งธ ์‚ฌ์ง„ ์† ๊ทธ๋ฆฌ์›Œํ•˜๋Š” ์‚ฌ๋žŒ๋“ค์ธ "may"์™€ "kein"์˜ ๊ทธ๋ฆฌ์›€ ์ ์ˆ˜๋ฅผ ํ•ฉ์น˜๋ฉด 15(5 + 10)์ ์ž…๋‹ˆ๋‹ค. ์„ธ ๋ฒˆ์งธ ์‚ฌ์ง„์˜ ๊ฒฝ์šฐ "kain"๊ณผ "may"๋งŒ ๊ทธ๋ฆฌ์›Œํ•˜๋ฏ€๋กœ ๋‘˜์˜ ๊ทธ๋ฆฌ์›€ ์ ์ˆ˜๋ฅผ ํ•ฉํ•œ 6(1 + 5)์ ์ด ์‚ฌ์ง„์˜ ์ถ”์–ต ์ ์ˆ˜์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ [19, 15, 6]์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

 

์ž…์ถœ๋ ฅ ์˜ˆ #2

  • ์ฒซ ๋ฒˆ์งธ ์‚ฌ์ง„ ์† ๊ทธ๋ฆฌ์›Œํ•˜๋Š” ์‚ฌ๋žŒ๋“ค์ธ "kali", "mari", "don"์˜ ๊ทธ๋ฆฌ์›€ ์ ์ˆ˜๋ฅผ ํ•ฉ์น˜๋ฉด 67(11 + 1 + 55)์ ์ž…๋‹ˆ๋‹ค. ๋‘ ๋ฒˆ์งธ ์‚ฌ์ง„ ์†์—” ๊ทธ๋ฆฌ์›Œํ•˜๋Š” ์ธ๋ฌผ์ด ์—†์œผ๋ฏ€๋กœ 0์ ์ž…๋‹ˆ๋‹ค. ์„ธ ๋ฒˆ์งธ ์‚ฌ์ง„ ์† ๊ทธ๋ฆฌ์›Œํ•˜๋Š” ์‚ฌ๋žŒ์€ "don"๋งŒ ์žˆ์œผ๋ฏ€๋กœ 55์ ์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ [67, 0, 55]๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

 

๐Ÿ‘ฉ ๋‚ด ํ’€์ด

class Solution{
   public int[] solution(String[] name, int[] yearning, String[][] photo) {
        int[] answer = new int[photo.length];
        
        HashMap<String,Integer> map = new LinkedHashMap<>();
        
        for(int i=0; i< name.length; i++){
            map.put(name[i], yearning[i]); // ์ถ”์–ตํ•˜๋Š” ์‚ฌ๋žŒ ์ด๋ฆ„์— ์ ์ˆ˜ ์ €์žฅ
        }

        for(int i=0; i< photo.length; i++){
            String[] persons = photo[i]; // ๊ทธ๋ฃน๋ณ„ ๋ฐฐ์—ด ์ƒ์„ฑ
            int score = 0;
            
            for(int j=0; j<persons.length; j++){
            	
                String person = persons[j];
                if(map.containsKey(person)){ // map์˜ key์— photo ์† ์‚ฌ๋žŒ์ด ์žˆ๋‹ค๋ฉด
                    score+=map.get(person); // ์ ์ˆ˜ ์ถ”๊ฐ€
                }
            }
            answer[i]=score;
        }
        return answer;
    }
  }

map์„ ํ™œ์šฉํ•ด ํ’€์—ˆ๋‹ค.