프로그래머스(파이썬)

점프와 순간 이동→재귀함수(DFS)★★ + 동적 계획법
효율성 실패한 나의 첫번째 풀이 def solution(n): if n == 1 or n == 2: return 1 result = [0] * (n + 1) result[1] = 1 result[2] = 1 for i in range(3, n+1): # 이전에 구한 결과값을 재사용 if i % 2 == 0: result[i] = result[i // 2] else: result[i] = result[i-1] + 1 return result[n] ▶ for문 때문에 작은 수부터 큰 수까지 쭈욱 훑어서 효율성 테스트에서 계속 실패했다. 효율성 테스트를 통과한 풀이 def solution(n): if n==1 or n==2: return 1 if n % 2 ==0: return solution(n//2) el..
N개의 최소공배수→연쇄법칙★ + if not stack: 스택이 비어있다면
나의 풀이 import math def gcd(a, b): """ 이 함수는 주어진 두 숫자 a, b의 최대공약수(GCD)를 계산합니다. """ for i in range(min(a, b), 0, -1): if a % i == 0 and b % i == 0: return i def lcm(a, b): """ 이 함수는 주어진 두 숫자 a, b의 최소공배수(LCM)를 계산합니다. """ return int( a * b /gcd(a, b)) def solution(arr): """ 이 함수는 주어진 리스트 arr의 모든 요소들의 LCM을 계산합니다. """ stack = [] # 리스트의 각 요소를 순회합니다. for element in arr: # 스택이 비어 있으면 요소를 스택에 추가합니다. if not..
구명보트→ 침몰하는 타이타닉(그리디)★★
나의 풀이 import java.util.*; class Solution { public int solution(int[] people, int limit) { Arrays.sort(people); int cnt=0; int left=0; int right= people.length-1; while(left
끝말잇기→인덱스 순환 +stack사용해서 중복체크, index+1은 사람번호, 인덱스 별도 저장★★+ turn//n+1
나의 풀이 def solution(n, words): check_list=[0]*(n) #체크리스트의 index+1은 사람의 번호를 의미한다. #체크리스트의 value는 각 사람의 "차례"를 의미한다. check_list[0]=1 stack=[words[0]] for i in range(1, len(words)): if words[i-1][-1] == words[i][0]: stack.append(words[i]) check_list[i%n]+=1 if stack.count(words[i])== 2: return [(i%n)+1, check_list[i%n]] else: check_list[i%n]+=1 return [(i%n)+1, check_list[i%n]] #끝말잇기가 정상적으로 종료된 경우 el..