728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ ๐Ÿ˜ต‍๐Ÿ’ซ

https://school.programmers.co.kr/learn/courses/30/lessons/12909?language=java

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

SW๊ฐœ๋ฐœ์ž๋ฅผ ์œ„ํ•œ ํ‰๊ฐ€, ๊ต์œก, ์ฑ„์šฉ๊นŒ์ง€ Total Solution์„ ์ œ๊ณตํ•˜๋Š” ๊ฐœ๋ฐœ์ž ์„ฑ์žฅ์„ ์œ„ํ•œ ๋ฒ ์ด์Šค์บ ํ”„

programmers.co.kr

 

๋‚ด ํ’€๐Ÿฆท

import java.util.*;

class Solution {
    boolean solution(String s) {
        Stack<String> str = new Stack<>();
        boolean answer = true;
        
        for(int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            
            if(c == '(') {
                str.push(String.valueOf(c));
            } else if(c ==')') {
                if(str.isEmpty()) {
                    answer = false;
                    break;
                }
                str.pop();
            }
        }
        
        if (!str.isEmpty()) {
            answer = false;
        }
        return answer;
    }
}

 

์ •๋ง์ •๋ง ์˜ค๋žœ๋งŒ์— ๊ด„ํ˜ธ ๊ฒ€์‚ฌ ๋ฌธ์ œ๋ฅผ ํ’€์—ˆ๋‹ค ใ…Žใ…Žใ…Ž

์ง„์งœ ๊ด„ํ˜ธ ๊ฒ€์‚ฌ ์ถ”์–ต์ด ๋งŽ์€ ๋ฌธ์ œ๋ผ ๋„ˆ๋ฌด ๊ธฐ์–ต๋‚˜๋Š”๋ฐ ํŒŒ์ด์ฌ ํ–ˆ๋˜์‹œ์ ˆ์ด๋ผ ์ž ์‹œ ํ ์นซํ–ˆ๋‹ค ใ…Žใ…Žใ…Žใ…Ž

๋‹น์—ฐํžˆ ์Šคํƒ ์จ์„œ ํ’€์–ด์•ผ ๊ฒ ๋‹ค ํ–ˆ๋Š”๋ฐ ๋‹ค๋ฅธ ์‚ฌ๋žŒ ํ’€์ด๊ฐ€ ์Šคํƒ๋„ ์“ฐ์ง€ ์•Š๊ณ  ์ •๋ง ๊ฐ„๋‹จํ•˜๊ฒŒ ์ผ๋˜ ํ’€์ด๊ฐ€ ์žˆ์–ด์„œ ํ•œ๋ฒˆ ๊ฐ€์ ธ์™€ ๋ดค๋‹ค...

 

// ๋‹ค๋ฅธ ์‚ฌ๋žŒ ํ’€์ด

class Solution {
    boolean solution(String s) {
        boolean answer = false;
        int count = 0;
        for(int i = 0; i<s.length();i++){
            if(s.charAt(i) == '('){
                count++;
            }
            if(s.charAt(i) == ')'){
                count--;
            }
            if(count < 0){
                break;
            }
        }
        if(count == 0){
            answer = true;
        }

        return answer;
    }
}

 

์ฐธ๊ณ ํ•˜๋ฉด ์ข‹์„ ๊ฐœ๋…๋“ค โœ๏ธ

์ŠคํŠธ๋ง์œผ๋กœ ๋ณ€ํ™˜ : String.valueOf(๋ฐ”๊ฟ€ ๊ฐ’);

728x90
๋ฐ˜์‘ํ˜•
728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ ๐Ÿ˜ต‍๐Ÿ’ซ

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

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

SW๊ฐœ๋ฐœ์ž๋ฅผ ์œ„ํ•œ ํ‰๊ฐ€, ๊ต์œก, ์ฑ„์šฉ๊นŒ์ง€ Total Solution์„ ์ œ๊ณตํ•˜๋Š” ๊ฐœ๋ฐœ์ž ์„ฑ์žฅ์„ ์œ„ํ•œ ๋ฒ ์ด์Šค์บ ํ”„

programmers.co.kr

 

๋‚ด ํ’€๐Ÿฆท

import java.util.*;

class Solution {
    public int[] solution(int[] answers) {
        // ์ˆ˜ํฌ์ž๋“ค์˜ ํŒจํ„ด ์ •์˜
        int[] pattern1 = {1, 2, 3, 4, 5};
        int[] pattern2 = {2, 1, 2, 3, 2, 4, 2, 5};
        int[] pattern3 = {3, 3, 1, 1, 2, 2, 4, 4, 5, 5};
        
        // ์ ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•  ๋ฐฐ์—ด
        int[] scores = new int[3];
        
        // ๊ฐ ๋ฌธ์ œ์— ๋Œ€ํ•ด ์ˆ˜ํฌ์ž๋“ค์˜ ๋‹ต์•ˆ์„ ๋น„๊ตํ•˜์—ฌ ์ ์ˆ˜ ๊ณ„์‚ฐ
        for(int i = 0; i < answers.length; i++) {
            if(answers[i] == pattern1[i % pattern1.length]) {
                scores[0]++;
            }
            if(answers[i] == pattern2[i % pattern2.length]) {
                scores[1]++;
            }
            if(answers[i] == pattern3[i % pattern3.length]) {
                scores[2]++;
            }
        }
        
        // ์ œ์ผ ๋†’์€ ์ ์ˆ˜๋ฅผ ์ฐพ์Œ
        int maxScore = Math.max(scores[0], Math.max(scores[1], scores[2]));
        
        // ๊ฐ€์žฅ ๋†’์€ ์ ์ˆ˜๋ฅผ ๋ฐ›์€ ์ˆ˜ํฌ์ž๋ฅผ ๋ฆฌ์ŠคํŠธ์— ์ถ”๊ฐ€
        List<Integer> result = new ArrayList<>();
        for(int i = 0; i < scores.length; i++) {
            if(scores[i] == maxScore) {
                result.add(i+1);
            }
        }
        
        return result.stream().mapToInt(i -> i).toArray();
    }
}

 

๊ฐ€์žฅ ๋งŽ์€ ์ ์ˆ˜๋ฅผ ํš๋“ํ•œ ์‚ฌ๋žŒ์„ ๋ฐฐ์—ด๋กœ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฐ„๋‹จํ•œ ๋ฌธ์ œ์˜€๋‹ค!!

๋‹ด๋Š”๊ฒƒ ๊นŒ์ง€๋Š” ์ •๋ง ์‰ฌ์šด๋ฐ return ๋ฌธ ์ž‘์„ฑํ•˜๋Š” ๊ณผ์ •์—์„œ ์–ด๋ ค์›€์ด ์žˆ์—ˆ๋‹ค!

 

์ฐธ๊ณ ํ•˜๋ฉด ์ข‹์„ ๊ฐœ๋…๋“ค โœ๏ธ

  • .stream()
    • stream : ๋ฐ์ดํ„ฐ ์ปฌ๋ ‰์…˜์˜ ์ถ”์ƒํ™”๋กœ, ์š”์†Œ๋“ค์„ ์ฐจ๋ก€์ฐจ๋ก€ ๊บผ๋‚ด ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋Š” "๋ฐ์ดํ„ฐ ํ๋ฆ„" 
    • List<Integer> ๊ฐ์ฒด์—์„œ ๊ฐ ์š”์†Œ์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋Š” ์ŠคํŠธ๋ฆผ์„ ์ƒ์„ฑํ•œ๋‹ค 
    • ์ŠคํŠธ๋ฆผ์€ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ ์ž‘์—…์„ ์‰ฝ๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋„๋ก ๋„์™€์ฃผ๋Š” ์ถ”์ƒํ™” ๋„๊ตฌ์ด๋‹ค
  • .mapToInt(i -> i) 
    • ์ŠคํŠธ๋ฆผ์˜ ๊ฐ ์š”์†Œ๋ฅผ ์ •์ˆ˜๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค
    • i -> i๋Š” ๋žŒ๋‹ค ํ‘œํ˜„์‹์œผ๋กœ, ๋ฆฌ์ŠคํŠธ์˜ ๊ฐ ์š”์†Œ๋ฅผ i ๊ทธ๋Œ€๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค๋Š” ์˜๋ฏธ์ด๋‹ค
    • ์ด ๊ณผ์ •์—์„œ ๊ฐ์ฒด(Integer)๋ฅผ ์›์‹œ ํƒ€์ž…(int)์œผ๋กœ ๋ณ€ํ™˜ํ•˜๊ฒŒ ๋œ๋‹ค
  • .toArray() 
    • ์ŠคํŠธ๋ฆผ์˜ ๊ฒฐ๊ณผ๋ฅผ ๋ฐฐ์—ด๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค
    • ์ตœ์ข…์ ์œผ๋กœ List<Integer> ๊ฐ€ int[] ๋ฐฐ์—ด๋กœ ๋ณ€ํ™˜๋œ๋‹ค
728x90
๋ฐ˜์‘ํ˜•
728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ ๐Ÿ˜ต‍๐Ÿ’ซ

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

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

SW๊ฐœ๋ฐœ์ž๋ฅผ ์œ„ํ•œ ํ‰๊ฐ€, ๊ต์œก, ์ฑ„์šฉ๊นŒ์ง€ Total Solution์„ ์ œ๊ณตํ•˜๋Š” ๊ฐœ๋ฐœ์ž ์„ฑ์žฅ์„ ์œ„ํ•œ ๋ฒ ์ด์Šค์บ ํ”„

programmers.co.kr

 

๋‚ด ํ’€๐Ÿฆท

class Solution {
    public int solution(int[] numbers, int target) {
        return dfs(numbers, target, 0, 0);
    }
    
    private int dfs(int[] numbers, int target, int index, int sum) {
        // ๋ชจ๋“  ์ˆซ์ž๋ฅผ ๋‹ค ์‚ฌ์šฉํ–ˆ์„ ๋–„
        if(index == numbers.length) {
            // ๋ชฉํ‘œ ์ˆซ์ž์™€ ํ•ฉ์ด ๊ฐ™์œผ๋ฉด ๊ฒฝ์šฐ์˜ ์ˆ˜ 1 ์ฆ๊ฐ€
            return sum == target ? 1 : 0;
        }
        // ํ˜„์žฌ ์ˆซ์ž๋ฅผ ๋”ํ•˜๋Š” ๊ฒฝ์šฐ์™€ ๋นผ๋Š” ๊ฒฝ์šฐ๋กœ ์žฌ๊ท€ ํ˜ธ์ถฉ..
        return dfs(numbers, target, index + 1, sum + numbers[index]) + dfs(numbers, target, index + 1, sum - numbers[index]);
    }
}

 

์ด๋ฏธ ํ•œ๋ฒˆ ํ’€์—ˆ๋˜ dfs ๋ฌธ์ œ!!

๋ชจ๋“  ๊ฐ€๋Šฅํ•œ ๊ฒฝ๋กœ๋ฅผ ๋‹ค ํƒ์ƒ‰ํ•˜์—ฌ ํŠน์ • ์กฐ๊ฑด์„ ์ฐพ์•„์•ผํ•˜๋Š” ๋ฌธ์ œ์ด๋ฏ€๋กœ dfs๋ฅผ ์‚ฌ์šฉํ•˜์˜€๋‹ค

if๋ฌธ์„ ์“ฐ๋Š”๊ฒƒ๊นŒ์ง€๋Š” ์‰ฌ์šด๋ฐ ๋งˆ์ง€๋ง‰์— ์žฌ๊ท€๋ฅผ ์–ด๋–ป๊ฒŒ ์‹œ์ผœ์•ผํ•˜๋Š”์ง€๋Š” ํ•ญ์ƒ ๊ณ ๋ฏผ๋˜๋Š” ๋ถ€๋ถ„์ธ๊ฑฐ ๊ฐ™๋‹ค..

ํ•˜์ง€๋งŒ ์ด๋ฒˆ๋ฌธ์ œ๋Š” ์ˆ˜์›”ํ•˜๊ฒŒ ํ†ต๊ณผ~~

 

์ฐธ๊ณ ํ•˜๋ฉด ์ข‹์„ ๊ฐœ๋…๋“ค โœ๏ธ

 

 DFS๋ฅผ ์‚ฌ์šฉํ–ˆ์„ ๋•Œ ํšจ๊ณผ์ ์ธ ๋ฌธ์ œ ์œ ํ˜•

  1. ๋ชจ๋“  ๊ฒฝ๋กœ๋ฅผ ํƒ์ƒ‰ํ•˜์—ฌ ํŠน์ • ์กฐ๊ฑด์„ ์ฐพ๋Š” ๊ฒฝ์šฐ : ์ฃผ์–ด์ง„ ์ˆซ์ž ๋ฐฐ์—ด์„ ๋”ํ•˜๊ณ  ๋นผ์„œ ํŠน์ • ๊ฐ’์„ ๋งŒ๋“œ๋Š” ๋ฌธ์ œ
  2. ํ•ด๋‹ค ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธํ•˜๋Š” ๊ฒฝ์šฐ : ๋ฏธ๋กœ์—์„œ ์ถœ๊ตฌ๊นŒ์ง€ ๊ฐ€๋Š” ๊ฒฝ๋กœ๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธํ•  ๋•Œ
  3. ์กฐํ•ฉ๊ณผ ์ˆœ์—ด์„ ์ƒ์„ฑํ•˜๋Š” ๋ฌธ์ œ : ํŠน์ • ๊ธธ์ด์˜ ์กฐํ•ฉ์„ ์ƒ์„ฑํ•˜๊ฑฐ๋‚˜, ์ˆœ์—ด์„ ๋งŒ๋“œ๋Š” ๋ฌธ์ œ
  4. ๋ฐฑํŠธ๋ž˜ํ‚น๊ณผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•  ๋•Œ : N-Queens ๋ฌธ์ œ์ฒ˜๋Ÿผ ์ œํ•œ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ์ฐพ์„ ๋•Œ

 

DFS ์‚ฌ์šฉ ์‹œ ์ฃผ์˜์‚ฌํ•ญ

- ๋ฌดํ•œ ๋ฃจํ”„ ์ฃผ์˜ : ์žฌ๊ท€ ํ˜ธ์ถœ์—์„œ ์ข…๋ฃŒ ์กฐ๊ฑด์„ ๋ช…ํ™•ํžˆ ์„ค์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค

- ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰ : ๊นŠ์ด๊ฐ€ ๊นŠ์–ด์งˆ ๊ฒฝ์šฐ ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰์ด ์ปค์งˆ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ, ๋„ˆ๋ฌด ๊นŠ์€ ์žฌ๊ท€ ํ˜ธ์ถœ์ด ํ•„์š”ํ•œ ๋ฌธ์ œ์—์„œ๋Š” ์Šคํƒ ์˜ค๋ฒ„ํ”Œ๋กœ์šฐ์— ์ฃผ์˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค

- ํƒ์ƒ‰ ์ตœ์ ํ™” : ๋ฐฑํŠธ๋ž˜ํ‚น์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ถˆํ•„์š”ํ•œ ๊ฒฝ๋กœ๋Š” ์กฐ๊ธฐ์— ์ค‘๋‹จํ•˜์—ฌ ํšจ์œจ์„ฑ์„ ๋†’์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค

 

728x90
๋ฐ˜์‘ํ˜•
728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ ๐Ÿ˜ต‍๐Ÿ’ซ

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

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

SW๊ฐœ๋ฐœ์ž๋ฅผ ์œ„ํ•œ ํ‰๊ฐ€, ๊ต์œก, ์ฑ„์šฉ๊นŒ์ง€ Total Solution์„ ์ œ๊ณตํ•˜๋Š” ๊ฐœ๋ฐœ์ž ์„ฑ์žฅ์„ ์œ„ํ•œ ๋ฒ ์ด์Šค์บ ํ”„

programmers.co.kr

 

๋‚ด ํ’€๐Ÿฆท

import java.util.*;

class Solution {
    public int[] solution(int[] array, int[][] commands) {
        int[] answer = new int[commands.length];
        
        for(int i = 0; i < commands.length; i++) { 
            int start = commands[i][0] -1;
            int end = commands[i][1];
            int k = commands[i][2] - 1;
            
            // ๋ฐฐ์—ด์˜ ํŠน์ • ๊ตฌ๊ฐ„์„ ๋ณต์‚ฌํ•˜๊ณ  ์ •๋ ฌ
            int[] arr = Arrays.copyOfRange(array, start, end);
            Arrays.sort(arr);
            
            // k๋ฒˆ์งธ ์ˆซ์ž๋ฅผ ๊ฒฐ๊ณผ ๋ฐฐ์—ด์— ์ถ”๊ฐ€
            answer[i] = arr[k];
            
            }
        return answer;
    }
}

 

์˜ค๋žœ๋งŒ์— ํ’€์–ด๋ณด๋Š” ์ •๋ ฌ๋ฌธ์ œ๋ผ ๊ฐ„๋‹จํ•œ ๋ฌธ์ œ์ง€๋งŒ ๋‹นํ™ฉํ–ˆ๋‹ค ใ…Žใ…Ž

์–ด๋–ป๊ฒŒ ๋ฐฐ์—ด์„ ๊ฐ€์ ธ์˜ค๋Š”๊ฒƒ์ด ์ข‹์„์ง€ for๋ฌธ ๋Œ๋ฉด์„œ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ๋งŒ๋“ค์–ด์„œ ๋„ฃ์–ด์ค„๊นŒํ–ˆ๋Š”๋ฐ ์ฐพ์•„๋ณด๋‹ˆ copyOfRange๋ผ๋Š” ๋ฐฐ์—ด ๋ณต์‚ฌ ๋ฉ”์„œ๋“œ๊ฐ€ ์กด์žฌํ•˜์—ฌ ์‚ฌ์šฉํ•ด์„œ ์“ฐ๋‹ˆ๊นŒ ๊ธˆ๋ฐฉ ํ•ด๊ฒฐ !!!

copyOfRange ๊ผญ ๊ธฐ์–ตํ•ด ๋‘๊ฒ ์Œ~

์ฐธ๊ณ ํ•˜๋ฉด ์ข‹์„ ๊ฐœ๋…๋“ค โœ๏ธ

Arrays.copyOfRange(์›๋ณธ๋ฐฐ์—ด, ์‹œ์ž‘์ธ๋ฑ์Šค, ๋์ธ๋ฑ์Šค);

์›๋ณธ๋ฐฐ์—ด : ๋ณต์‚ฌํ•˜๋ ค๋Š” ๋ฐฐ์—ด
์‹œ์ž‘ ์ธ๋ฑ์Šค : ๋ณต์‚ฌ๋ฅผ ์‹œ์ž‘ํ•  ์ธ๋ฑ์Šค(ํฌํ•จ)
๋์ธ๋ฑ์Šค : ๋ณต์‚ฌ๋ฅผ ์ข…๋ฃŒํ•  ์ธ๋ฑ์Šค(๋ฏธํฌํ•จ)

int[] array = {1, 5, 2, 6, 3, 7, 4};
int[] slicedArray = Arrays.copyOfRange(array, 1, 5); // {5, 2, 6, 3}
728x90
๋ฐ˜์‘ํ˜•
728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ ๐Ÿ˜ต‍๐Ÿ’ซ

https://school.programmers.co.kr/learn/courses/30/lessons/68935?language=java

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”.

programmers.co.kr

 

๋ฌธ์ œ ์„ค๋ช…

์ž์—ฐ์ˆ˜ n์ด ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. n์„ 3์ง„๋ฒ• ์ƒ์—์„œ ์•ž๋’ค๋กœ ๋’ค์ง‘์€ ํ›„, ์ด๋ฅผ ๋‹ค์‹œ 10์ง„๋ฒ•์œผ๋กœ ํ‘œํ˜„ํ•œ ์ˆ˜๋ฅผ return ํ•˜๋„๋ก

solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”

 

 

์ œํ•œ์‚ฌํ•ญ

- n์€ 1์ด์ƒ 100,000,000 ์ดํ•˜์ธ ์ž์—ฐ์ˆ˜์ž…๋‹ˆ๋‹ค.

 

 

์ž…์ถœ๋ ฅ ์˜ˆ

n result
45 7
125 229

 

๋‚ด ํ’€๐Ÿฆท

 

class Solution {
    public int solution(int n) {
        String sam = Integer.toString(n, 3);
        String reverse_sam = new StringBuilder(sam).reverse().toString();
        int decimal = Integer.parseInt(reverse_sam, 3);

        return decimal;
    }
    
}

 

๋‹ค์‹œ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ํ•˜๊ธฐ๋กœ ํ•˜๊ณ  ์ฒ˜์Œ์œผ๋กœ ์œ ๋‚˜๊ฐ€ ๊ณจ๋ผ์ค€ ๋ฌธ์ œ๋ผ ๊ทธ๋Ÿฐ์ง€ ์‰ฌ์šด ๋ฌธ์ œ์˜€๋‹ค!!

์ดํ•ด๋ ฅ์ด ์•ˆ ์ข‹์€๋ฐ ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค๋Š” ์˜ˆ์‹œ๋ฅผ ์„ค๋ช…ํ•ด์ค˜์„œ ์ข€ ๋” ํ’€๊ธฐ ์ข‹์€ ๋“ฏ ํ•˜๋‹ค

10์ง„๋ฒ•์„ 3์ง„๋ฒ• ์œผ๋กœ ๋ฐ”๊พธ๊ณ  3์ง„๋ฒ•์„ ๋’ค์ง‘์–ด์„œ ๋‹ค์‹œ 10์ง„๋ฒ•์œผ๋กœ ๋ฐ”๊พธ์–ด์„œ ๋„์ถœ!!

๋’ค์ง‘๋Š” ๋ฉ”์„œ๋“œ์ธ reverse()๋Š” ํŒŒ์ด์ฌ๊ณผ ๋™์ผํ•˜๊ฒŒ ์žˆ๋‹ค

๊ทธ๋ฆฌ๊ณ  Intger,toString()์œผ๋กœ ๋ฐ”๋กœ ์ง„๋ฒ• ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ๊ฒŒ๋˜์—ˆ๋‹ค!!!

 

 

๋‹ค๋ฅธ ์‚ฌ๋žŒ ํ’€์ด

class Solution {
    public int solution(int n) {
        String a = "";

        while(n > 0){
            a = (n % 3) + a;
            n /= 3;
        }
        a = new StringBuilder(a).reverse().toString();


        return Integer.parseInt(a,3);
    }
}

 

๊น”๋”ํ•˜๋‹ค!!

๋ณ€์ˆ˜๋ช…์„ ํ•œ ๋‹จ์–ด๋กœ ํ•˜๋Š”๊ฒƒ๋„ ์ข‹์€ ๋ฐฉ๋ฒ•์ผ๋“ฏ!!

 

์ฐธ๊ณ ํ•˜๋ฉด ์ข‹์„ ๊ฐœ๋…๋“ค โœ๏ธ

  • Integer.parseInt(String s) : ๋ฌธ์ž์—ด(s)์„ ์ธ์ž๊ฐ’์œผ๋กœ ๋ฐ›์œผ๋ฉด ํ•ด๋‹น ๊ฐ’์„ 10์ง„์ˆ˜์˜ Integer ํ˜•์œผ๋กœ ๋ณ€ํ™˜
    ex) Integer.parseInt("1004")    // 1004
  • Integer.parseInt(String s, int radix) : ์ˆซ์žํ˜•์˜ ๋ฌธ์ž์—ด์„ ์ฒซ๋ฒˆ์งธ ์ธ์ž๊ฐ’์œผ๋กœ ๋ฐ›๊ณ  ๋ณ€ํ™˜ํ•  ์ง„์ˆ˜๊ฐ’์„ ์ž…๋ ฅํ•˜๋ฉด ํ•ด๋‹น ์ง„์ˆ˜์— ๋งž์ถฐ์„œ Integer ํ˜•์œผ๋กœ ๋ณ€ํ™˜
    ex) Integer.psrseInt("45", 3)    // 1200
  • Integer.toString() : ์ˆซ์ž๋ฅผ ๋ฌธ์ž๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ํ˜• ๋ณ€ํ™˜ ๋ฐฉ๋ฒ•!!!
    ex) int num = 123;
          String str1 = Integer.toString(num);  .// ์ˆซ์ž๊ฐ€ ์•„๋‹ˆ๊ณ  ๋ฌธ์ž 123์ž„
728x90
๋ฐ˜์‘ํ˜•
728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ ๐Ÿ˜ต‍๐Ÿ’ซ

๋งค์ผ ์•„์นจ, ์„ธ์ค€์ด๋Š” ํ•™๊ต์— ๊ฐ€๊ธฐ ์œ„ํ•ด์„œ ์ฐจ๋ฅผ ํƒ€๊ณ  D ํ‚ฌ๋กœ๋ฏธํ„ฐ ๊ธธ์ด์˜ ๊ณ ์†๋„๋กœ๋ฅผ ์ง€๋‚œ๋‹ค. ์ด ๊ณ ์†๋„๋กœ๋Š” ์‹ฌ๊ฐํ•˜๊ฒŒ ์ปค๋ธŒ๊ฐ€ ๋งŽ์•„์„œ ์ •๋ง ์šด์ „ํ•˜๊ธฐ๋„ ํž˜๋“ค๋‹ค. ์–ด๋Š ๋‚ , ์„ธ์ค€์ด๋Š” ์ด ๊ณ ์†๋„๋กœ์— ์ง€๋ฆ„๊ธธ์ด ์กด์žฌํ•œ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ๊ฒŒ๋˜์—ˆ๋‹ค. ๋ชจ๋“  ์ง€๋ฆ„๊ธธ์€ ์ผ๋ฐฉํ†ตํ–‰์ด๊ณ , ๊ณ ์†๋„๋กœ๋ฅผ ์—ญ์ฃผํ–‰ํ•  ์ˆ˜๋Š” ์—†๋‹ค. ์„ธ์ค€์ด๊ฐ€ ์šด์ „ํ•ด์•ผ ํ•˜๋Š” ๊ฑฐ๋ฆฌ์˜ ์ตœ์†Ÿ๊ฐ’์„ ์ถœ๋ ฅํ•˜์‹œ์˜ค

 

๋‚ด ํ’€๐Ÿฆท

# 1446. ์ง€๋ฆ„๊ธธ
import sys
import heapq
input = sys.stdin.readline
INF = 1e9

n, d = map(int, input().split())
graph = [[] for _ in range(d + 1)]
distance = [INF] * (d + 1)

for i in range(d):
    graph[i].append((i + 1, 1))

# ์ง€๋ฆ„๊ธธ ์žˆ๋Š” ๊ฒฝ์šฐ ์ธ์ ‘ ๋ฆฌ์ŠคํŠธ์— ์ถ”๊ฐ€ํ•ด์ฃผ๊ธฐ
for _ in range(n):
    s, e, l = map(int, input().split())
    # ๋งˆ์ง€๋ง‰ ๋„์ฐฉ์ง€์ ์ด ์‹ค์ œ ๊ณ ์†๋„๋กœ ๋ณด๋‹ค ๋ฉ€๋ฉด ๋„˜๊ธด๋‹ค
    if e > d:
        continue
    graph[s].append((e, l))

# print(graph)
distance[0] = 0
heap = [[0, 0]]
while heap:
    dist, now = heapq.heappop(heap)

    if dist > distance[now]:
        continue

    for i in graph[now]:
        mn_dist = dist + i[1]
        if mn_dist < distance[i[0]]:
            distance[i[0]] = mn_dist
            heapq.heappush(heap, (mn_dist, i[0]))

print(distance[d])

 

์—ญ์ฃผํ–‰์ด ์•ˆ๋˜๋Š”๋ฐ ๋“ค์–ด์˜ค๊ธฐ๋Š” ์‹ค์ œ ๊ณ ์†๋„๋กœ๋ณด๋‹ค ๋จผ๊ณณ๋„ ๋“ค์–ด์˜ค๊ณ ํ•ด์„œ ์–ด๋–ป๊ฒŒ ์ฒ˜๋ฆฌํ•ด์ค˜์•ผ ํ• ์ง€ ๊ณ ๋ฏผ์„ ์˜ค๋ž˜ํ–ˆ๋‹ค... ๊ทธ๋ฆฌ๊ณ  ์ง€๋ฆ„๊ธธ์ด ์žˆ์ง€๋งŒ ๋” ๋ฉ€์–ด์„œ ์ด์šฉ์„ ์•ˆ ํ•˜๋Š” ๊ฒฝ์šฐ๋Š” ์–ด๋–ป๊ฒŒ ์ฒ˜๋ฆฌํ•ด์•ผํ• ๊นŒ ๊ณ ๋ฏผ์„ ํ–ˆ๋‹ค...

 

๊ฐ€์žฅ ๋จผ์ € ๋ชจ๋“  ๋‘ ๊ธธ์ด1๋งŒํผ ๊ฐˆ๋•Œ 1์ด ๊ฑธ๋ฆฐ๋‹ค๊ณ  ์ดˆ๊ธฐํ™”ํ•ด์คฌ๊ณ ... ์ง€๋ฆ„๊ธธ ๊ฐ’์„ ์ž…๋ ฅํ•˜๋ฉด์„œ ์—ญ์ฃผํ–‰์€ ํ•  ์ˆ˜ ์—†์œผ๋ฏ€๋กœ ๋งˆ์ง€๋ง‰ ๊ฐ’์ด ๊ณ ์†๋„๋กœ์˜ ๋์„ ๋„˜์–ด๋ฒ„๋ฆฌ๋Š” ๊ฒฝ์šฐ๋Š” ๋„ฃ์–ด์ฃผ์ง€ ์•Š์•˜๋‹ค... 

๊ทธ๋ฆฌ๊ณ  ๋‚˜๋จธ์ง€๋Š” ์šฐ๋ฆฌ๊ฐ€ ์•„๋Š” ๋‹ค์ต์ŠคํŠธ๋ผ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๊ตฌํ˜„ํ•˜๋ฉด ๋!!! ๐Ÿ˜Š๐Ÿ˜Š๐Ÿ˜Š

 

์ฐธ๊ณ ํ•˜๋ฉด ์ข‹์„ ๊ฐœ๋…๋“ค โœ๏ธ

Dijkstra

 

1. ์ž‘๋™์›๋ฆฌ

  • ๊ฐ„์„  : ์ธ์ ‘ ๋ฆฌ์ŠคํŠธ, ๊ฑฐ๋ฆฌ ๋ฐฐ์—ด : ์ดˆ๊ธฐ๊ฐ’ ๋ฌดํ•œ๋Œ€๋กœ ์„ค์ •, ํž™ ์‹œ์ž‘์  ์ถ”๊ฐ€
  • ํž™์—์„œ ํ˜„์žฌ ๋…ธ๋“œ ๋นผ๋ฉด์„œ, ๊ฐ„์„  ํ†ตํ•  ๋•Œ ๋” ๊ฑฐ๋ฆฌ๊ฐ€ ์งง์•„์ง„๋‹ค๋ฉด ๊ฑฐ๋ฆฌ ๊ฐฑ์‹  ๋ฐ ํž™์— ์ถ”๊ฐ€ํ•ด์ค€๋‹ค
  • ํž™ : ์ตœ๋Œ“๊ฐ’, ์ตœ์†Ÿ๊ฐ’์„ ๋น ๋ฅด๊ฒŒ ์ฐพ์„ ์ˆ˜ ์žˆ๋Š” ์ž๋ฃŒ ๊ตฌ์กฐ

 

2. ๊ธฐ๋ณธ ์ฝ”๋“œ

# ๋”์ต์ŠคํŠธ๋ผ ๊ตฌํ˜„ํ•˜๊ธฐ

# k : ์‹œ์ž‘์ 
# dist : ๊ฑฐ๋ฆฌ๋ฐฐ์—ด

dist[k] = 0 
heapq.heappush(heap, (0, K))

while heap:
	w, y = heapq.heappop(heap)
    if w > dist[v] : continue
    for nw, nv in edge[v]:
    	if dist[nv] > dist[v] + nw:
        	dist[nv] = dist[v] + nw
            heapq.heappush(heap, dist[nv], nv))

 

3,  ์•„์ด๋””์–ด

  • ํ•œ์ ์—์„œ ๋‹ค๋ฅธ ๋ชจ๋“  ์ ์œผ๋กœ์˜ ์ตœ๋‹จ ๊ฒฝ๋กœ > ๋‹ค์ต์ŠคํŠธ๋ผ ์‚ฌ์šฉ
  • ๋ชจ๋“  ์  ๊ฑฐ๋ฆฌ ์ดˆ๊ธฐ๊ฐ’ ๋ฌดํ•œ๋Œ€๋กœ ์„ค์ •
  • ์‹œ์ž‘์  ๊ฑฐ๋ฆฌ 0 ์„ค์ • ๋ฐ ํž™์— ์ถ”๊ฐ€
  • ํž™์—์„œ ํ•˜๋‚˜์”ฉ ๋นผ๋ฉด์„œ ์ˆ˜ํ–‰ํ•  ๊ฒƒ
  • ํ˜„์žฌ ๊ฑฐ๋ฆฌ๊ฐ€ ์ƒˆ๋กœ์šด ๊ฐ„์„ ์„ ๊ฑฐ์น ๋•Œ๋ณด๋‹ค ํฌ๋‹ค๋ฉด ๊ฐฑ์‹ 
  • ์ƒˆ๋กœ์šด ๊ฑฐ๋ฆฌํž™์— ์ถ”๊ฐ€!!!
728x90
๋ฐ˜์‘ํ˜•
728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ ๐Ÿ˜ต‍๐Ÿ’ซ

ํ•˜๋‚˜ ์ด์ƒ์˜ ์—ฐ์†๋œ ์†Œ์ˆ˜์˜ ํ•ฉ์œผ๋กœ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ๋Š” ์ž์—ฐ์ˆ˜๋“ค์ด ์žˆ๋‹ค. ๋ช‡๊ฐ€์ง€ ์ž์—ฐ์ˆ˜์˜ ์˜ˆ๋ฅผ ๋“ค์–ด ๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

  • 3: 3(ํ•œ ๊ฐ€์ง€)
  • 41 : 2+3+4+7+11+13 = 11+13+17 = 41(์„ธ ๊ฐ€์ง€)
  • 53 : 5+7+11+13+17 = 53(๋‘ ๊ฐ€์ง€)

ํ•˜์ง€๋งŒ ์—ฐ์†๋œ ์†Œ์ˆ˜์˜ ํ•ฉ์œผ๋กœ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์—†๋Š” ์ž์—ฐ์ˆ˜๋“ค๋„ ์žˆ๋Š”๋ฐ, 20์ด ๊ทธ ์˜ˆ์ด๋‹ค. 7+13์„ ๊ณ„์‚ฐํ•˜๋ฉด 20์ด ๋˜๊ธฐ๋Š” ํ•˜๋‚˜ 7๊ณผ 13์ด ์—ฐ์†์ด ์•„๋‹ˆ๊ธฐ์— ์ ํ•ฉํ•œ ํ‘œํ˜„์ด ์•„๋‹ˆ๋‹ค. ๋˜ํ•œ ํ•œ ์†Œ์ˆ˜๋Š” ๋ฐ˜๋“œ์‹œ ํ•œ ๋ฒˆ๋งŒ ๋ง์…ˆ์— ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์—, 3+5+5+7๊ณผ ๊ฐ™์€ ํ‘œํ˜„๋„ ์ ํ•ฉํ•˜์ง€ ์•Š๋‹ค. 

์ž์—ฐ์ˆ˜๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ์ด ์ž์—ฐ์ˆ˜๋ฅผ ์—ฐ์†๋œ ์†Œ์ˆ˜์˜ ํ•ฉ์œผ๋กœ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

 

๋‚ด ํ’€๐Ÿฆท

# 1644. ์†Œ์ˆ˜์˜ ์—ฐ์†ํ•ฉ

import sys
input = sys.stdin.readline

N = int(input())
prime = []
arr = [True for _ in range(N+1)] 
arr[0] = arr[1] = False
for i in range(2, N+1):
    if arr[i]:
        prime.append(i)
        # i์˜ 2๋ฐฐ ์ด์ƒ ๋ถ€ํ„ฐ๋Š” ๋‹ค false...
        for j in range(2*i, N+1, i):
            arr[j] = False

result = start = end = 0
while end <= len(prime):
    each = sum(prime[start:end])
    if each == N:
        result += 1
        end += 1
    elif each < N:
        end += 1
    else:
        start += 1
print(result)

๊ณจ๋“œ 3 ์š”๋Ÿ‰ํ•˜๊ณ ๋Š” ์—„์ฒญ ์–ด๋ ค์šด ๋ฌธ์ œ๋Š” ์•„๋‹ˆ์ง€๋งŒ ๋น„๊ต์  ๋น ๋ฅธ ์‹œ๊ฐ„์— ์†Œ์ˆ˜๋ฅผ ๊ตฌํ•˜๊ณ  ๋˜ ํˆฌํฌ์ธํ„ฐ๋ฅผ ์จ์•ผ ํ•˜๊ธฐ ๋•จ๋ฌธ์— ๋‘๊ฐ€์ง€ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ฐœ๋…์„ ์•Œ๊ณ  ์žˆ์–ด์•ผ ํ•œ๋‹ค... ๊ทธ๋ž˜์„œ ์ด๊ฑฐ์ „์— ํ’€ ๋ฌธ์ œ๋กœ ์†Œ์ˆ˜ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ ์ถ”์ฒœํ•œ๋‹ค!!!!

 

์ฐธ๊ณ ํ•˜๋ฉด ์ข‹์„ ๊ฐœ๋…๋“ค โœ๏ธ

  • ์—๋ผํ† ์Šค ํ…Œ๋„ค์Šค์˜ ์ฒด(์†Œ์ˆ˜ ํŒ๋ณ„ ์•Œ๊ณ ๋ฆฌ์ฆ˜ : ์†Œ์ˆ˜๋“ค์„ ๋Œ€๋Ÿ‰์œผ๋กœ ๋น ๋ฅด๊ณ  ์ •ํ™•ํ•˜๊ฒŒ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค)
// ์—๋ผํ† ์Šคํ…Œ๋„ค์Šค์˜ ์ฒด๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์†Œ์ˆ˜ ๊ตฌํ•˜๊ธฐ

// ์ฃผ์–ด์ง„ ๋ฒ”์œ„ ๋‚ด์— ์žˆ๋Š” ๊ฐ ์ˆซ์ž์— ๋Œ€ํ•ด ์ดˆ๊ธฐ์—๋Š” ๋ชจ๋‘ True์ธ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค
arr = [True for _ in range(N+1)] 
// 0๊ณผ 1์€ ์†Œ์ˆ˜๊ฐ€ ์•„๋‹ˆ๋ฏ€๋กœ False๋กœ ๊ฐ’์„ ์„ค์ •ํ•œ๋‹ค
arr[0] = arr[1] = False
// 2๋ถ€ํ„ฐ N๊นŒ์ง€์˜ ๊ฐ ์ˆซ์ž์— ๋Œ€ํ•ด ์†Œ์ˆ˜์ธ์ง€ ํŒ๋ณ„ํ•œ๋‹ค
for i in range(2, N+1):
	// ํ˜„์žฌ ์ˆซ์ž๊ฐ€ ์†Œ์ˆ˜์ธ ๊ฒฝ์šฐ, ํ•ด๋‹น ์ˆซ์ž๋ฅผ ์†Œ์ˆ˜ ๋ชฉ๋ก์— ์ถ”๊ฐ€ํ•˜๊ณ ,,,
    // ํ˜„์žฌ ์ˆซ์ž์˜ ๋ฐฐ์ˆ˜๋“ค์„ ๋ชจ๋‘ False๋กœ ์„ค์ •ํ•œ๋‹ค... (์ ์–ด๋„ i๋ฅผ ํฌํ•จํ•˜๋‹ˆ๊นŒ ์†Œ์ˆ˜๊ฐ€ ์•„๋‹ˆ๋‹ค)
    if arr[i]:
        prime.append(i)
        # i์˜ 2๋ฐฐ ์ด์ƒ ๋ถ€ํ„ฐ๋Š” ๋‹ค false...
        for j in range(2*i, N+1, i):
            arr[j] = False

 

  • ํˆฌ ํฌ์ธํ„ฐ (๋ฆฌ์ŠคํŠธ์— ์ˆœ์ฐจ์ ์œผ๋กœ ์ ‘๊ทผํ•ด์•ผ ํ•  ๋•Œ ๋‘ ๊ฐœ์˜ ์ ์˜ ์œ„์น˜๋ฅผ ๊ธฐ๋กํ•˜๋ฉด์„œ ์ฒ˜๋ฆฌํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ex) ํŠน์ •ํ•œ ํ•ฉ์„ ๊ฐ€์ง€๋Š” ๋ถ€๋ถ„ ์—ฐ์† ์ˆ˜์—ด ์ฐพ๊ธฐ )
// ํˆฌ ํฌ์ธํ„ฐ ๊ตฌํ˜„ํ•˜๊ธฐ

// ๋‹ค ๋”ํ–ˆ์„ ๊ฒฝ์šฐ N์ด ๋˜๋Š” ๊ฒฐ๊ณผ์™€ ์‹œ์ž‘, ๋ ์ ์„ 0์œผ๋กœ ์ดˆ๊ธฐํ™”
result = start = end = 0
// ๋ ํฌ์ธํ„ฐ๊ฐ€ ์†Œ์ˆ˜์˜ ๊ธธ์ด๋ณด๋‹ค ์ž‘์„œ๋‚˜ ๊ฐ™์€ ๋™์•ˆ ์ˆ˜ํ–‰
while end <= len(prime):
	// ํ˜„์žฌ ์‹œ์ž‘๋ถ€ํ„ฐ ๊ธ‘๊ฐ€์ง€์˜ ์†Œ์ˆ˜ ํ•ฉ์„ ๊ณ„์‚ฐ
    each = sum(prime[start:end])
    // ํ•ฉ์ด N๊ณผ ๊ฐ™๋‹ค๋ฉด ๊ฒฐ๊ณผ๋ฅผ 1 ์ฆ๊ฐ€, ๋ ํฌ์ธํ„ฐ ๋˜ํ•œ 1 ์ฆ๊ฐ€
    if each == N:
        result += 1
        end += 1
    // ํ•ฉ์ด N๋ณด๋‹ค ์ž‘๋‹ค๋ฉด ๋ ํฌ์ธํ„ฐ 1 ์ฆ๊ฐ€
    elif each < N:
        end += 1
    // ํ•ฉ์ด N๋ณด๋‹ค ํฌ๋‹ค๋ฉด ์‹œ์ž‘ ํฌ์ธํ„ฐ 1 ์ฆ๊ฐ€
    else:
        start += 1
728x90
๋ฐ˜์‘ํ˜•
728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ ๐Ÿ˜ต‍๐Ÿ’ซ

๋…์ผ ๋กœ๋˜๋Š” {1, 2,...,49}์—์„œ ์ˆ˜ 6๊ฐœ๋ฅผ ๊ณ ๋ฅธ๋‹ค.

๋กœ๋˜ ๋ฒˆํ˜ธ๋ฅผ ์„ ํƒํ•˜๋Š”๋ฐ ์‚ฌ์šฉ๋˜๋Š” ๊ฐ€์žฅ ์œ ๋ช…ํ•œ ์ „๋žต์€ 49๊ฐ€์ง€ ์ˆ˜ ์ค‘ k(k>6)๊ฐœ์˜ ์ˆ˜๋ฅผ ๊ณจ๋ผ ์ง‘ํ•ฉ S๋ฅผ ๋งŒ๋“  ๋‹ค์Œ ๊ทธ ์ˆ˜๋งŒ ๊ฐ€์ง€๊ณ  ๋ฒˆํ˜ธ๋ฅผ ์„ ํƒํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด, k=8, S={1, 2, 3, 5, 8, 13, 21, 34}์ธ ๊ฒฝ์šฐ ์ด ์ง‘ํ•ฉ S์—์„œ ์ˆ˜๋ฅผ ๊ณ ๋ฅผ ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜๋Š” ์ด 28๊ฐ€์ง€์ด๋‹ค.

{[1, 2, 3, 5, 8, 13], [1, 2, 3, 5, 8, 21], [1,2,3,5,8,34], ..., [3,5,8,13,21,34])

์ง‘ํ•ฉ S์™€ k๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ์ˆ˜๋ฅผ ๊ณ ๋ฅด๋Š” ๋ชจ๋“  ๋ฐฉ๋ฒ•์„ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจใ…‡์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

 

์ž…๋ ฅ

์ž…๋ ฅ์€ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋กœ ์ด๋ฃจ์–ด์ ธ ์ด์‹ฟ. ๊ฐ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋Š” ํ•œ ์ค„๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๋‹ค, ์ฒซ ๋ฒˆ์งธ ์ˆ˜๋Š” k(6 < k < 13)์ด๊ณ , ๋‹ค์Œ k๊ฐœ ์ˆ˜๋Š” ์ง‘ํ•ฉ S์— ํฌํ•จ๋˜๋Š” ์ˆ˜์ด๋‹ค. S์˜ ์›์†Œ๋Š” ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ฃผ์–ด์ง„๋‹ค.

์ž…๋ ฅ์˜ ๋งˆ์ง€๋ง‰ ์ค„์—๋Š” 0์ด ํ•˜๋‚˜ ์ฃผ์–ด์ง„๋‹ค.

 

์ถœ๋ ฅ

๊ฐ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋งˆ๋‹ค ์ˆ˜๋ฅผ ๊ณ ๋ฅด๋Š” ๋ชจ๋“  ๋ฐฉ๋ฒ•์„ ์ถœ๋ ฅํ•œ๋‹ค. ์ด๋•Œ, ์‚ฌ์ „ ์ˆœ์œผ๋กœ ์ถœ๋ ฅํ•œ๋‹ค.

๊ฐ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค ์‚ฌ์ด์—๋Š” ๋นˆ ์ค„์„ ํ•˜๋‚˜ ์ถœ๋ ฅํ•œ๋‹ค.

 

๋‚ด ํ’€๐Ÿฆท

# 6603. ๋กœ๋˜
def backtracking(depth, beginwith):
    if depth == 6:
        print(*result)
        return

    for i in range(beginwith, k):
        if not visited[i]:
            result[depth] = nums[i]
            backtracking(depth+1, i+1)
            visited[i] = False


while True:
    lotto = list(map(int, input().split()))
    k = lotto[0]
    nums = lotto[1:]
    result = [0] * 6
    visited = [False] * k
    backtracking(0, 0) // ๊นŠ์ด, ์–ด๋””์„œ ๋ถ€ํ„ฐ ์‹œ์ž‘ํ• ๊ฑด์ง€

    if k == 0:
        quit()
    print()

 

N๊ณผ M์œผ๋กœ ๋‹ค์ง„ ๋ฐฑํŠธ๋ž˜ํ‚น ๊ฐœ๋…์„ ๋‹ค๋ฅธ ๋ฌธ์ œ์—๋„ ์ ์šฉ์‹œํ‚ค๊ณ  ์‹ถ์–ด์„œ ๋กœ๋˜๋ผ๋Š” ๋ฌธ์ œ๋ฅผ ํ’€์–ด๋ณด์•˜์Šต๋‹ˆ๋‹ค!!!!

์–ด๋ ค์› ๋˜ ์ ....

1. ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋งˆ๋‹ค ํ•œ์ค„ ๋›ฐ์šฐ๋Š”๊ฑธ ์–ด๋–ป๊ฒŒ ํ•˜์ง€....

2. ๋งˆ์ง€๋ง‰ 0์ด ๋‚˜์™”์„ ๋•Œ ์ข…๋ฃŒ๋ฅผ ์–ด๋–ป๊ฒŒ ํ•˜์ง€...

3. ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค์˜ ์ˆ˜๊ฐ€ ์•ˆ์ฃผ์–ด์กŒ๋Š”๋ฐ ์–ด๋–ป๊ฒŒ ํ•˜์ง€...

 

 

๋ฐฑํŠธ๋ž˜ํ‚น ๊ฐœ๋…์ ์ธ๋ถ€๋ถ„ ๋ณด๋‹ค๋Š” ๋‹ค๋ฅธ๊ฒƒ๋“ค์— ๋” ์‹œ๊ฐ„์„ ๋งŽ์ด ์žก์•„๋จน์—ˆ๋‹ค....

์ˆœ,์กฐ์— ๋Œ€ํ•œ ๊ฐœ๋…์€ ๐Ÿ‘‡๐Ÿ‘‡๐Ÿ‘‡

https://jeniffer0812techstory.tistory.com/94

 

์ˆœ์—ด๊ณผ ์กฐํ•ฉ๐Ÿ’ช

์œ ํŠœ๋ฒ„ ๋”ฉ์ฝ”๋”ฉ์”จ ๋ณด๊ณ  ์ •๋ฆฌํ•ด๋ณด๋Š” ์ˆœ์—ด๊ณผ ์กฐํ•ฉ... ์žฌ๊ท€, ๋ฐฑํŠธ๋ž˜ํ‚น, dfs... ๋“ฑ๋“ฑ ์ •๋ง ๋งŽ์ด ์—ฐ๊ด€๋˜์–ด์žˆ์–ด์„œ ์ •๋ฆฌํ•˜๋ฉด ์ข‹์„๊ฒƒ ๊ฐ™์•„ ์˜ฌ๋ ค๋ด…๋‹ˆ๋‹ค... ##### permutation(์ˆœ์—ด) ''' ์ˆœ์—ด : permutation(ํฌํ•จ๋˜์–ด ์žˆ

jeniffer0812techstory.tistory.com

 

 

์ฐธ๊ณ ํ•˜๋ฉด ์ข‹์„ ๊ฐœ๋…๋“ค โœ๏ธ

  • exit() : ํ”„๋กœ๊ทธ๋žจ ๊ฐ•์ œ ์ข…๋ฃŒ
for i in range(100):
    if i == 5:
        quit()
    print(i)
    
/* 
0
1
2
3
4
*/
728x90
๋ฐ˜์‘ํ˜•

+ Recent posts