๊ด€๋ฆฌ ๋ฉ”๋‰ด

Coding Planet

level1. ๊ฐœ์ธ์ •๋ณด ์ˆ˜์ง‘ ์œ ํšจ๊ธฐ๊ฐ„ Java(2023 KAKAO BLIND RECRUITMENT) ๋ณธ๋ฌธ

๐ŸŽ ์ฝ”๋”ฉํ…Œ์ŠคํŠธ/ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

level1. ๊ฐœ์ธ์ •๋ณด ์ˆ˜์ง‘ ์œ ํšจ๊ธฐ๊ฐ„ Java(2023 KAKAO BLIND RECRUITMENT)

jhj.sharon 2023. 8. 21. 23:37
๋ฐ˜์‘ํ˜•

โœจ ๋ฌธ์ œ

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

๐Ÿ’ป์ฝ”๋“œ

import java.util.HashMap;
import java.util.Map;
import java.util.List;
import java.util.ArrayList;


class Solution {
    public int[] solution(String today, String[] terms, String[] privacies) {
    //1. ์•ฝ๊ด€์˜ ์œ ํšจ๊ธฐ๊ฐ„์„ ๋งคํ•‘ํ•œ๋‹ค.
    // - termMap : ["A", "6"]
        Map<String, Integer> termMap = new HashMap<>();
        for(String term : terms){
            String [] parts = term.split(" ");
            termMap.put(parts[0], Integer.parseInt(parts[1]));
        }
    //2. ๊ฐ ๊ฐœ์ธ์ •๋ณด์˜ ์œ ํšจ๊ธฐ๊ฐ„ ์ข…๋ฃŒ์ผ์„ ๊ณ„์‚ฐํ•œ๋‹ค.
        List<Integer> expiredList = new ArrayList<>();
        //์ •๊ทœํ‘œํ˜„์‹ ๊ตฌ๋ถ„์ž
        String [] todayParts = today.split("\\.");
        
        int todayYear = Integer.parseInt(todayParts[0]);
        int todayMonth = Integer.parseInt(todayParts[1]);
        int todayDay = Integer.parseInt(todayParts[2]);
        
        for(int i =0; i <privacies.length; i++){
            String[] info = privacies[i].split(" ");
            String[] dateParts = info[0].split("\\.");
            
            int year = Integer.parseInt(dateParts[0]);
            int month = Integer.parseInt(dateParts[1]);
            int day = Integer.parseInt(dateParts[2]);
            
            int calcYear = year;
            int calcMonth = month + termMap.get(info[1]);
            int calcDay = day;

            while (calcMonth > 12) {
                calcYear += 1;
                calcMonth -= 12;
            }

            if (calcYear < todayYear || (calcYear == todayYear && calcMonth < todayMonth) ||
            (calcYear == todayYear && calcMonth == todayMonth && calcDay <= todayDay))
             {
                expiredList.add(i + 1);
            }

        }
        
        int[] result = new int[expiredList.size()];
        for(int i =0; i<expiredList.size(); i++){
            result[i] = expiredList.get(i);
        }
        
        return result;
    }
}

 

๐Ÿ‘ฉ‍๐Ÿ’ป ํ’€์ด ๋ฐฉ๋ฒ•

  • terms ๋ฐฐ์—ด์„ ํ†ตํ•ด ๊ฐ ์•ฝ๊ด€์˜ ์œ ํšจ๊ธฐ๊ฐ„์„ ๋งคํ•‘ํ•œ๋‹ค.
  • ๊ฐ ๊ฐœ์ธ์ •๋ณด์˜ ์œ ํšจ๊ธฐ๊ฐ„ ์ข…๋ฃŒ์ผ์„ ๊ณ„์‚ฐํ•œ๋‹ค.
  • ์˜ค๋Š˜ ๋‚ ์งœ์™€ ๋น„๊ตํ•˜์—ฌ ์œ ํšจ๊ธฐ๊ฐ„์ด ์ง€๋‚œ ๊ฐœ์ธ์ •๋ณด๋ฅผ ์ฐพ๋Š”๋‹ค.
  • ์œ ํšจ๊ธฐ๊ฐ„์ด ์ง€๋‚œ ๊ฐœ์ธ์ •๋ณด์˜ ๋ฒˆํ˜ธ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

๐Ÿ”” ๋Š๋‚€์ 

  • ์‹œ๊ฐ„ ์—„์ฒญ ๊ฑธ๋ฆฌ๊ณ  ์˜ค๋ฅ˜๋„ ๋งŽ์•˜์Œ ใ…  ๊ตฌ๊ธ€๋งํ•ด์„œ ํžŒํŠธ ๋ณด๊ณ  ๊ฒจ์šฐ ํ’€์—ˆ๋‹ค..
  • String[ ] todayParts = today.split("\\.") : ์ ์„ ๊ตฌ๋ถ„์ž๋กœํ•ด์„œ split์„ ์“ฐ๋ ค๊ณ  ํ–‡๋Š”๋ฐ split ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ ์ธ์ž๋กœ ์ „๋‹ฌ๋˜๋Š” ๋ฌธ์ž์—ด์€ ์ •๊ทœํ‘œํ˜„์‹์ด๊ธฐ ๋•Œ๋ฌธ์— ์ด์Šค์ผ€์ดํ”„๋ฅผ ํ•ด์•ผํ•œ๋‹ค. ํ•˜์ง€๋งŒ ๋ฐฑ์Šฌ๋ž˜์‹œ ์ž์ฒด๋„ ์ด์Šค์ผ€์ดํ”„ ๋ฌธ์ž์ด๋ฏ€๋กœ ๋ฐฑ์Šฌ๋ž˜์‹œ๋ฅผ ๋ฌธ์ž์—ด์— ํฌํ•จ์‹œํ‚ค๋ ค๋ฉด ๋‘ ๋ฒˆ ๋ฐ˜๋ณตํ•ด์•ผํ•œ๋‹ค..!  ์ฆ‰, '.'์„ ์ด์Šค์ผ€์ดํ”„ ํ•˜๋Š” ๋ฌธ์ž์—ด์„ ๋‹ค์‹œ ์ด์Šค์ผ€์ดํ”„ ํ•˜๋Š” ๊ฒƒ์ด๋‹ค(??)
  • ์ž๋ฐ” ๋ฌธ์ž์—ด์—์„œ ๋ฐฑ์Šฌ๋ž˜์‹œ: \\
  • ์ •๊ทœ ํ‘œํ˜„์‹์—์„œ ์ : \.ใ„ฑ
๋ฐ˜์‘ํ˜•
Comments