Algorithm
-
[Python] 체육복Algorithm/프로그래머스 2021. 5. 1. 00:17
1. 문제 📚 2. 입출력 예 📋 3. 알고리즘 ✅ 제한사항에 여벌 체육복을 가져온 사람도 도난 당할 수 있다 라고 적혀있다 이점을 고려하여, 중복되는 것들은 빼준다. 우선순위를 왼쪽으로 둘지, 오른쪽으로 둘지 선택한다 n = 5, lost = [2, 4], reserve[3, 5] 가정 오른쪽으로 우선순위를 두게 되면, 최대값이 나올 수 없다. 따라서 왼쪽부터 우선순위를 둬야한다. 여분학생 -1 값과 도난 학생 이 같으면 도난학생을 없앤다. 총 학생의 수에서 도난 학생의 수를 빼준다. 4. 소스코드 💻 def solution(n, lost, reserve): set_reserve = set(reserve) - set(lost) set_lost = set(lost) - set(reserve) for i ..
-
[Python] 모의고사Algorithm/프로그래머스 2021. 4. 29. 17:23
1. 문제 📚 2. 입출력 예 📋 3. 알고리즘 ✅ 각자 학생이 맞춘 정답의 개수를 list를 만든다 학생마다 반복되는 답의 길이가 다르므로, 정답의 길이를 나눈 나머지로 비교 정답이면 각자 개수에 +1을 해준다 가장 많이 맞춘 학생을 리턴하면 되므로, 가장 많이 맞춘 개수와 비교하여 같으면 answer에 넣어준다. 4. 소스코드 💻 def solution(answers): st1 = [1, 2, 3, 4, 5] st2 = [2, 1, 2, 3, 2, 4, 2, 5] st3 = [3, 3, 1, 1, 2, 2, 4, 4, 5, 5] answer = [] cnt = [0, 0, 0] for i in range(len(answers)): if answers[i] == st1[i%len(st1)]: cnt[0..
-
[Python] 완주하지 못한 선수Algorithm/프로그래머스 2021. 4. 27. 22:31
http:// programmers.co.kr/learn/courses/30/lessons/42576?language=python3 1. 문제 📚 수많은 마라톤 선수들이 마라톤에 참여하였습니다. 단 한 명의 선수를 제외하고는 모든 선수가 마라톤을 완주하였습니다. 마라톤에 참여한 선수들의 이름이 담긴 배열 participant와 완주한 선수들의 이름이 담긴 배열 completion이 주어질 때, 완주하지 못한 선수의 이름을 return 하도록 solution 함수를 작성해주세요. 2. 입출력 예 📋 3. 알고리즘 ✅ 완주하지 못한 선수는 무조건 1명이기 때문에 Counter 함수를 이용한다 Counter함수를 이용하여 두 객체를 빼주게 되면, 결국 완주하지 못한 선수 한명만 남게된다 Counter는 key..
-
[Python] 크레인 인형뽑기Algorithm/프로그래머스 2021. 4. 26. 23:59
1. 문제 📚 게임 화면의 격자의 상태가 담긴 2차원 배열 board와 인형을 집기 위해 크레인을 작동시킨 위치가 담긴 배열 moves가 매개변수로 주어질 때, 크레인을 모두 작동시킨 후 터트려져 사라진 인형의 개수를 return 하도록 solution 함수를 완성해주세요. 2. 입출력 예 📋 3. 알고리즘 ✅ 하나 씩 비교하기 위해 moves와 board를 각각 for문으로 꺼내준다 0이 아닐 시 (인형이 있는 것) basket 배열에 넣어준다 배열이 마지막 숫자와 같을 경우 answer 에 2를 더해준다 → 총 2개가 사라지는 것이기 때문에 사라진 2개를 basket에서 없애준다 4. 소스코드 💻 def solution(board, moves): answer = 0 bucket = [] for mov..
-
[Python] 에라토스테네스의 체 (Sieve of Eratosthenes)Algorithm/Basic 2021. 4. 4. 22:00
1. 정의 - 2부터 시작하여, 소수인 경우 값을 출력하고, 해당 소수의 모든 배수들을 소거해 나가는 방법 2. 방법 - 2 -> 소수, N보다 작은 모든 2의 배수 제거 - 3 -> 소수, N보다 작은 모든 3의 배수 제거 - .... - k -> 소수, N보다 작은 모든 k의 배수 제거 예를들어, 20까지의 소수 구하기를 한다면, 2의 배수부터 20까지의 배수를 지워주는 방식이다. import time def isPrime(v): i = 2 while i*i = 2: primeList = list..
-
[Python] 유클리드 호제법(Euclidean Algorithm)Algorithm/Basic 2021. 3. 19. 18:46
1. 정의 - 두 수의 최대공약수를 구하는 알고리즘 2. 방법 - 2개의 자연수 a, b(a > b) 에 대해서 a를 b로 나눈 나머지를 r이라 하면, a와 b의 최대 공약수는 b와 r의 최대공약수와 같음 -> 이 성질에 따라, b를 r로 나눈 나머지 r'를 구하고, 다시 r을 r'로 나눈 나머지를 구하는 과정을 반복하여 나머지가 0이 되었을 때 나누는 수가 a와 b의 최대공약수가 된다. 3. 비교 유클리드 호제법이 아닌 다른 방법으로 코드를 작성한 후, 시간을 비교해보았습니다. 2초 가량 차이가 난다. 결론: 최대공약수를 구할 땐 유클리드 호제법을 이용하자
-
[Java] 백준 1110번 더하기 사이클Algorithm/백준 2020. 2. 19. 17:45
문제 접근방법 1. 새로운 수의 십의 자릿수 = input의 일의 자릿수 2. 새로운 수의 일의 자릿수 = ( input의 십의 자릿수 + input의 일의 자릿수 ) 의 일의 자릿수 3. 새로운 수 = input 멈추기 해결 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 import java.io.*; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int input = Integer.parseInt(br.readLine()..