본문 바로가기

Category64

[프로그래머스] [Python] Level2_기능개발 https://programmers.co.kr/learn/courses/30/lessons/42586 코딩테스트 연습 - 기능개발 프로그래머스 팀에서는 기능 개선 작업을 수행 중입니다. 각 기능은 진도가 100%일 때 서비스에 반영할 수 있습니다. 또, 각 기능의 개발속도는 모두 다르기 때문에 뒤에 있는 기능이 앞에 있는 programmers.co.kr - 반환 순서에 집착하면 Stack / Queue문제이다 - 업데이트 되는 값을 꼭 다 저장할 필요는 없다 - While은 조건이 True일 때, 실행의 반복이다 조건문이 아니다 def solution(progresses, speeds): answer = [] cnt = 0 #FIFO -> stack while(progresses): if progress.. 2021. 8. 3.
[프로그래머스] [Python] Level2_위장 https://programmers.co.kr/learn/courses/30/lessons/42578 코딩테스트 연습 - 위장 programmers.co.kr 풀이 유형 : 해시 Hash 1차 시도 def solution(clothes): dic = {} answer = 1 for cloth in clothes: dic[cloth[1]] = dic.get(cloth[1], []) + [cloth[0]] for i in dic: answer *= len(dic[i])+1 return answer-1 💡 최종 로직 0. 주어진 clothes를 사용하여 dictionary를 만든다 1. dic를 순회하며 (각 value의 갯수 +1)을 answer에 곱한다 +1을 하는 이유 : 각 의상 타입에 "안입음"이라는.. 2021. 8. 2.
[프로그래머스] [Python] Level2 - 더 맵게 https://programmers.co.kr/learn/courses/30/lessons/42626 코딩테스트 연습 - 더 맵게 매운 것을 좋아하는 Leo는 모든 음식의 스코빌 지수를 K 이상으로 만들고 싶습니다. 모든 음식의 스코빌 지수를 K 이상으로 만들기 위해 Leo는 스코빌 지수가 가장 낮은 두 개의 음식을 아래와 같 programmers.co.kr 풀이 유형 : 힙 Heap 1차 시도 import heapq def solution(scoville, K): sum = 0 answer = 0 heapq.heapify(scoville) while(True): min1 = heapq.heappop(scoville) if len(scoville) == 0 : answer = -1 break if min.. 2021. 8. 2.
[자료구조] [ Python ] 리스트 List 사용법 1. 정의 문자열 2. 선언 list = [ ] #길이가 정해진 리스트 선언 list = [0 for i in range(n)] #0으로 초기화 된 2차원 배열 matrix = [[o for col in range(m)] for row in range(n)] # m x n matrix 3. 기본 매소드 #Slicing list[start : end : term] #길이구하기 len(list) #삭제하기 del list[idx] #요소 추가 list.append(x) list.insert(idx, x) # idx번째에 x를 list += [x, y] #확장extend #정렬 list.sort() list_new = sorted(list) #마지막요소 반환 list.pop() 참고 : https://jobc.. 2021. 8. 2.
[ Python ] | heapq 사용법 | heapsort, 우선순위큐 1. 정의 complete binary tree 이진 트리(binary tree) 기반의 최소 힙(min heap) 자료구조를 제공 => 원소들이 항상 정렬된 상태로 추가되고 삭제 2. 선언 import heapq #다른 list를 heap으로 만든다 heapq.heapify(list) #새로 선언 heap = [] heapq 모듈은 일반 list로 선언해도 heap으로 쓸 수 있다. 자바의 PriorityQueue 클래스처럼 리스트와 별개의 자료구조가 아닌 점에 유의해야함. 3. 기본 메소드 #원소추가 heapq.heappush(list, x) #(heap, 원소) #원소삭제 -> 최소값 삭제(반환) heapq.heappop(list) #삭제하지않고 최소값 얻기 heap[0] 4. 활용 기본 제공되는 .. 2021. 8. 2.
[ Python ] | enumerate 사용법 1. 정의 get a counter in a loop display item counts Use enumerate() with conditional statements Unpack values returned by enumerate() String처럼 변경불가능(immutable)한 객체를 변경가능(mutable)한 객체로 변경 index를 함께 저장한다 - 변경불가능(immutable) : Int, String, Tuple - 변경가능(mutable) : List, Dictionary, Set 2. 선언 enumerate(iterable, start=0) list = ["eat", "pray", "love"] for i in enumerate(list): print(i) #(0, 'eat') #(1, .. 2021. 8. 1.