integer로 구성된 sticks라는 input array의 모든 요소를 이어 붙이는 문제다. 이 때 더할 때 마다 발생하는 합이 최소가 되도록 하는 문제다. sorting을 통해 풀이를 해야하나 잠시 생각했지만 Min heap을 사용하면 쉽게 구현이 가능하다. class Solution: def connectSticks(self, sticks: List[int]) -> int: mh = sticks ret = 0 heapify(mh) while len(mh) > 1: m1 = heappop(mh) m2 = heappop(mh) sum_ = m1 + m2 ret += sum_ heappush(mh, sum_) return ret
![](http://i1.daumcdn.net/thumb/C148x148.fwebp.q85/?fname=https://blog.kakaocdn.net/dn/ckqfBc/btrOzs0XW6H/jLUtGU8twG6aJhVBPFwwbk/img.png)
Heapify :Heapify is the process of creating a heap data structure from a binary tree represented using an array. : Heapify란 배열(Array)로 표현된 이진 트리(Binary tree)의 자료구조를 갖는 힙(Heap)을 생성하는 과정이다. 알고리즘 과제로 Heap을 Build하는 문제에 대해 알고리즘을 제시해야했고, 목표 시간복잡도(Time-Complexity)는 Linear time(O(n))이었다. Heap을 접할 때 기본적으로 함께 알게 되는 함수 중 하나가 Insert() 함수다. Insert() 함수의 시간복잡도는 Tree의 Height(높이)만큼 비교가 필요해 O(log n)이다. 이를 n개의 노드..
- Total
- Today
- Yesterday
- 리트코드
- The Economist
- Python
- ml
- 머신 러닝
- 안드로이드
- DICTIONARY
- machine learning
- Hash Map
- 이코노미스트
- 소켓 프로그래밍
- 딕셔너리
- 이코노미스트 에스프레소
- socket programming
- Android
- join
- Computer Graphics
- min heap
- vertex shader
- defaultdict
- leetcode
- 티스토리챌린지
- I2C
- 오블완
- C++
- tf-idf
- 투 포인터
- java
- 파이썬
- The Economist Espresso
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |