Deep_Dev
article thumbnail

๋ฌธ์ œ ์„ค๋ช…

์™ธ๊ณผ์˜์‚ฌ ๋จธ์“ฑ์ด๋Š” ์‘๊ธ‰์‹ค์— ์˜จ ํ™˜์ž์˜ ์‘๊ธ‰๋„๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ง„๋ฃŒ ์ˆœ์„œ๋ฅผ ์ •ํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ •์ˆ˜ ๋ฐฐ์—ด emergency๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ ์‘๊ธ‰๋„๊ฐ€ ๋†’์€ ์ˆœ์„œ๋Œ€๋กœ ์ง„๋ฃŒ ์ˆœ์„œ๋ฅผ ์ •ํ•œ ๋ฐฐ์—ด์„ returnํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

 
 
์ œํ•œ์‚ฌํ•ญ
  • ์ค‘๋ณต๋œ ์›์†Œ๋Š” ์—†์Šต๋‹ˆ๋‹ค.
  • 1 ≤ emergency์˜ ๊ธธ์ด ≤ 10
  • 1 ≤ emergency์˜ ์›์†Œ ≤ 100

 

 

์ž…์ถœ๋ ฅ ์˜ˆ     emergency     result
[3, 76, 24] [3, 1, 2]
[1, 2, 3, 4, 5, 6, 7] [7, 6, 5, 4, 3, 2, 1]
[30, 10, 23, 6, 100] [2, 4, 3, 5, 1]

 

 


emergency๋ฅผ ๋ณต์‚ฌํ•˜์—ฌ ๊ฐ™์€ temp๋ฐฐ์—ด์„ ์ƒ์„ฑํ•˜๊ณ ,

๊ธฐ์กด์˜ emergency๋ฅผ ์ •๋ ฌํ•จ ( temp๋ฅผ ์ •๋ ฌํ•ด๋„ ๋จ )

1์ˆœ์œ„์ˆœ์„œ๋ถ€ํ„ฐ ์ •ํ•ด์ค„ ๋ณ€์ˆ˜ num=1 ์„ ์„ ์–ธํ•˜๊ณ 

์ •๋ ฌ๋œ emergency์˜ length-1์„ ์™ธ๋ถ€ for๋ฌธ ( ์ œ์ผ ๋†’์€ ๊ฐ’๋ถ€ํ„ฐ 1์ˆœ์œ„๋กœ ์ค„๊ฑฐ๋‹ˆ๊นŒ )

๊ทธ๋ฆฌ๊ณ  ๋ณต์ œํ•œ temp์˜ 0๋ฒˆ์งธ ์ธ๋ฑ์Šค๋ถ€ํ„ฐ ๋‚ด๋ถ€ for๋ฌธ

์ •๋ ฌํ•œ emergency์˜ ์ œ์ผ ํฐ ๊ฐ’๊ณผ, ๊ธฐ์กด์˜ ๋ฐฐ์—ด temp์™€ ๊ฐ™์€ ๊ฐ’์„ ์ฐพ๋Š”๋‹ค๋ฉด

์ •๋ ฌ๋˜์–ด ์žˆ์ง€ ์•Š์•˜๋˜ temp์˜ ์œ„์น˜๊ฐ’(j)๋ฅผ ๋ฐ˜ํ™˜ํ•˜์—ฌ, answer์˜ j์ž๋ฆฌ์— 1์ˆœ์„œ(num)์„ ๋„ฃ์–ด์ฃผ๊ณ  num++;

 


 

import java.util.Arrays;
class Solution {
    public int[] solution(int[] emergency) {
        int[] answer = new int[emergency.length];
        int[] temp=emergency.clone();
        Arrays.sort(emergency);
        int num=1;
        for(int i=emergency.length-1; i>=0; i--){
            for(int j=0; j<temp.length; j++){
                if(emergency[i]==temp[j]){
                    answer[j]=num;
                    num++;
                }
            }
        }
        return answer;
    }
}