본문 바로가기

전체 글267

[프로그래머스] 올바른 괄호 파이썬 stack 비우기 다른사람의 풀이 def solution(s): answer = True stack = [] for i in s: if i == '(': stack.append('(') else: try: stack.pop() except: return False if len(stack) == 0: return True else: return False 출처: eda-ai-lab.tistory.com/478 2021. 3. 15.
[프로그래머스] 가장 큰 정사각형 찾기 파이썬 (DP) 예전에 fastcampus에서 한번 다뤘던 문제라고 기억을 하는데 이미 기억에서 지워진 문제 DP로 푸는것만 기억하고 나머지는 다 잊어버림 def solution(board): width = len(board[0]) height = len(board) for x in range(1,height): for y in range(1,width): if board[x][y] == 1: board[x][y] = min(board[x-1][y-1], min(board[x-1][y], board[x][y-1])) + 1 return max([item for row in board for item in row])**2 출처:geonlee.tistory.com/111 굳이 DP list를 만들겠다면 def solutio.. 2021. 3. 15.
[프로그래머스] 쿼드압축 후 개수 세기 파이썬 다른 사람의 풀이 def solution(arr): answer = [0, 0] N = len(arr) def comp(x, y, n): init = arr[x][y] # 해당 네모값중 하나 # 모두 같아야 통과임 for i in range(x, x + n): for j in range(y, y + n): if arr[i][j] != init: # 한번이라도 다르면 그 네모는 압축불가 nn = n // 2 comp(x, y, nn) comp(x, y + nn, nn) comp(x + nn, y, nn) comp(x + nn, y + nn, nn) return # 무사히 다 통과했다면 압축가능 answer[init] += 1 comp(0, 0, N) return answer 이해하기도 쉽고 깔끔한 풀이 같.. 2021. 3. 15.
[프로그래머스] 타겟 넘버 (BFS/DFS) 파이썬 다른 사람의 풀이 from collections import deque def solution(numbers, target): answer = 0 queue = deque([(0, 0)]) # sum, level while queue: s, l = queue.popleft() if l > len(numbers): break elif l == len(numbers) and s == target: answer += 1 queue.append((s+numbers[l-1], l+1)) queue.append((s-numbers[l-1], l+1)) return answer sum, level이라는 2개의 tuple를 queue를 활용해서 만든풀이 queue에 들어가는 tuple를 append하는 코드를 보면, .. 2021. 3. 15.
[프로그래머스] 카펫 파이썬 완전탐색으로 푸는 방법 def solution(brown, red): for a in range(1, int(red**0.5)+1): if not red % a: b = red // a if 2*a + 2*b + 4 == brown: return [b+2, a+2] 출처: geonlee.tistory.com/114 def solution(brown, red): for index in range(1,red+1): if red%index == 0: length = red//index if (((index+2)*(length+2))-(index*length)) == brown: return [max(index+2,length+2),min(index+2,length+2)] m.blog.naver.com/Post.. 2021. 3. 12.
[프로그래머스] 구명보트 파이썬 다른 사람의 풀이 def solution(people, limit): from collections import deque people.sort() people = deque(people) length = len(people) answer = 0 while length != 0: limit_p = limit temp = 0 pop = people.pop() length -= 1 temp += pop limit_p -= pop if length >= 1: pop_r = people[0] if limit_p >= pop_r: people.popleft() length -=1 answer+=1 return answer 최상위 조건으로는 pop 2021. 3. 12.
[프로그래머스] H-index 파이썬 def solution(citations): if min(citations) >= len(citations): return len(citations) answer = [] h_list = list(range(min(citations), max(citations)+1)) for h in h_list: more = [c for c in citations if c >= h] less = [c for c in citations if c = h and len(less) = l-i: return l-i return 0 pythonic한 풀이 def solution(citations): citations.sort(reverse=True) answer = max(map(min, enumerate(citations, s.. 2021. 3. 11.
[프로그래머스] 전화번호 목록 파이썬 Hash 테스트 케이스가 바뀌기도 했고 해쉬 카테고리인테 해쉬가 어떻게 쓰이는지 궁금하던 찰나 발견한 블로그 파이썬 의 dictionary 자료형이 hash table 이다. def solution(phone_book): answer = True hash_map = {} # 등장한 숫자들을 count 딕셔너리로 만듦 for phone_number in phone_book: hash_map[phone_number] = 1 # 다시 숫자들을 꺼낸뒤 for phone_number in phone_book: temp = "" for number in phone_number: #숫자 하나씩 뜯어보기 temp += number #딕셔너리 키로 같은게 있지만! 전체 숫자는 다른 경우! if temp in hash_map an.. 2021. 3. 11.
[프로그래머스] 가장 큰 수 파이썬 정렬 import functools def comparator(a,b): t1 = a+b t2 = b+a return (int(t1) > int(t2)) - (int(t1) < int(t2)) # t1이 크다면 1 // t2가 크다면 -1 // 같으면 0 def solution(numbers): n = [str(x) for x in numbers] n = sorted(n, key=functools.cmp_to_key(comparator),reverse=True) answer = str(int(''.join(n))) return answer comparator 이건 JS에서도 비슷하게 했던걸로 기억 (sort에서 arrow function으로) cmp_to_key는 처음보는 함수 python2 에서 사용되.. 2021. 3. 11.