본문 바로가기

전체 글267

[프로그래머스] 예상 대진표 파이썬 (DP) def solution(n,a,b): answer = 0 while a != b: answer += 1 a, b = (a+1)//2, (b+1)//2 return answer 2021. 3. 16.
[프로그래머스] 짝지어 제거하기 파이썬 (stack, pop) 다른 사람의 풀이 def solution(s): stack = [] stack.append(s[0]) for i in s: if len(stack) == 0: # 최초에 stack이 비었더라도 loop진행중 stack이 비워지면 다시 채워넣기 stack.append(i) elif stack[-1] == i: stack.pop() else: stack.append(i) if len(stack) == 0: return 1 pair 로 하는 문제는 괄호같은 경우도 그렇고 stack으로 pop [프로그래머스] 올바른 괄호 파이썬 ygseo.tistory.com/163 2021. 3. 16.
[프로그래머스] N개의 최소공배수 파이썬 from math import gcd def lcm(x,y): return x*y // gcd(x,y) def solution(arr): while True: arr.append(lcm(arr.pop(), arr.pop())) if len(arr) == 1: return arr[0] 최대공약수, 최소공배수를 활용해서 N개의 최소공배수를 구한다. list에서 2개의 원소를 꺼내 최대공약수를 구한뒤 다시 list에 삽입 출처: brownbears.tistory.com/454 2021. 3. 16.
[프로그래머스] JadenCase 문자열 만들기 파이썬 def solution(s): s = list(s.split(" ")) answer = "" for word in s: for i in range(len(word)): if i==0: answer+=word[i].upper() else: answer+=word[i].lower() answer+=" " return answer[:-1] 다른 사람의 풀이 중 title이라는 내장함수가 있어서 어떻게 동작하는지 확인 print(("for the last week").title()) # output # For The Last Week 다만 숫자가 먼저 올 경우 숫자 제외하고 인식하기 때문에 문제 조건에 부적합 print('1ab'.title()) # output # 1Ab 검색하다보니 uppercase로 바꾸는.. 2021. 3. 16.
[프로그래머스] 행렬의 곱 파이썬 def solution(a, b): c = [] for i in range(0,len(a)): # row of a temp=[] for j in range(0,len(b[0])): # col of b s = 0 for k in range(0,len(a[0])): # col of a or row of b s += a[i][k]*b[k][j] temp.append(s) c.append(temp) return c a = b = [1, 4] [3, 3] [3, 2] [3, 3] [4, 1] matrix muliplication answer를 list라고 선언했을때 answer[0][0] = (a[0][0] * b[0][0]) + (a[0][1] * b[1][0]) . . . answer[2][1] = (a[2].. 2021. 3. 16.
[프로그래머스] 피보나치 수 파이썬 역시 재귀로 풀었더니 런타임오류 DP로 수정 def solution(n): fibo = [] for x in range(0,n): if x < 2: fibo.append(1) else: fibo.append(fibo[x-2] + fibo[x-1]) answer = fibo[-1]%1234567 return answer Pythonic way def solution(num): a,b = 0,1 for i in range(num): a,b = b,a+b return a 2021. 3. 16.
[프로그래머스] 숫자의 표현 파이썬 그냥 무식하게 brute force로 def solution(num): answer = 0 for i in range(1, num + 1): s = 0 while s < num: s += i i += 1 if s == num: answer += 1 return answer 2021. 3. 15.
[프로그래머스] 땅따먹기 파이썬 다른사람의 풀이 def solution(land): for i in range(0, len(land)-1): land[i+1][0] += max(land[i][1],land[i][2],land[i][3]) land[i+1][1] += max(land[i][0],land[i][2],land[i][3]) land[i+1][2] += max(land[i][0],land[i][1],land[i][3]) land[i+1][3] += max(land[i][0],land[i][1],land[i][2]) return max(land[len(land)-1]) def solution(land): for i in range(1, len(land)): for j in range(len(land[0])): land[i][j] .. 2021. 3. 15.
[프로그래머스] 다음 큰 숫자 def solution(num): b_num = bin(num) b_num_1 = b_num.count("1") new_num = num+1 while True: b_new_num = bin(new_num) new_num_1 = b_new_num.count("1") if b_num_1 == new_num_1: break new_num += 1 return new_num 지저분하지만 맞긴했따. def solution(num): new_num = num+1 while True: if bin(num).count("1") == bin(new_num).count("1"): break new_num += 1 return new_num 깔끔하게 2021. 3. 15.