Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/wnsdk/algorithm-for-coding-test

๐Ÿง  ์ฝ”๋”ฉํ…Œ์ŠคํŠธ๋ฅผ ์œ„ํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ด๋ก  ์ •๋ฆฌ
https://github.com/wnsdk/algorithm-for-coding-test

Last synced: 11 days ago
JSON representation

๐Ÿง  ์ฝ”๋”ฉํ…Œ์ŠคํŠธ๋ฅผ ์œ„ํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ด๋ก  ์ •๋ฆฌ

Awesome Lists containing this project

README

        

# ๐Ÿง  ์ฝ”๋”ฉํ…Œ์ŠคํŠธ๋ฅผ ์œ„ํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์œ ํ˜• ์ •๋ฆฌ

์˜ˆ์‹œ ์ฝ”๋“œ๋Š” ๋ชจ๋‘ ํŒŒ์ด์ฌ์œผ๋กœ ์ž‘์„ฑ๋˜์—ˆ๋‹ค.
ํŒŒ์ด์ฌ ๋ฌธ๋ฒ•์€ [์—ฌ๊ธฐ](./sub/Python.md)์—์„œ ํ™•์ธ.
์•„๋ž˜ ๋ชฉ์ฐจ์—์„œ ๋‹ค๋ฃจ์ง€ ์•Š์€ ๊ธฐํƒ€ ์ž์ž˜ํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ [์—ฌ๊ธฐ](./sub/etc.md)์—์„œ ํ™•์ธ.

### ๋ชฉ์ฐจ
- [์ž๋ฃŒ๊ตฌ์กฐ](#์ž๋ฃŒ๊ตฌ์กฐ)
- [์™„์ „ ํƒ์ƒ‰](#์™„์ „-ํƒ์ƒ‰)
- [์ด๋ถ„ ํƒ์ƒ‰](#์ด๋ถ„-ํƒ์ƒ‰)
- [๋™์  ๊ณ„ํš๋ฒ•](#๋™์ -๊ณ„ํš๋ฒ•)
- [ํƒ์š•๋ฒ•](#ํƒ์š•๋ฒ•)
- [์†Œ์ˆ˜](#์†Œ์ˆ˜)
- [์•ฝ์ˆ˜์™€ ๋ฐฐ์ˆ˜](#์•ฝ์ˆ˜์™€-๋ฐฐ์ˆ˜)
- [์œ„์ƒ์ •๋ ฌ](#์œ„์ƒ์ •๋ ฌ)
- [๊ฐ€์ค‘์น˜๊ฐ€ ์žˆ๋Š” ์ตœ๋‹จ๊ฑฐ๋ฆฌ](#๊ฐ€์ค‘์น˜๊ฐ€-์žˆ๋Š”-์ตœ๋‹จ๊ฑฐ๋ฆฌ)
- ๋ˆ„์  ํ•ฉ (์˜ˆ์ •)
- ๋ถ„ํ•  ์ •๋ณต (์˜ˆ์ •)
- ์ตœ์†Œ ์‹ ์žฅ ํŠธ๋ฆฌ (์˜ˆ์ •)
- ์ปจ๋ฒก์Šค ํ— (์˜ˆ์ •)
- ์„ธ๊ทธ๋จผํŠธ ํŠธ๋ฆฌ (์˜ˆ์ •)
- ๋ฌธ์ž์—ด ์•Œ๊ณ ๋ฆฌ์ฆ˜ (์˜ˆ์ •)






# ์ž๋ฃŒ๊ตฌ์กฐ

์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ๋ฅผ ํ’€ ๋•Œ ํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ๋“ค์„ ์–ด๋–ค ์ž๋ฃŒ๊ตฌ์กฐ์— ๋‹ด์„ ๊ฒƒ์ธ์ง€๋ฅผ ๊ณ ๋ฏผํ•ด์•ผํ•œ๋‹ค.

## ๋ฐฐ์—ด / ์Šคํƒ

โ†’ list

```python
list = []

list.append(x)
list.pop()
```

- ๋ฐฐ์—ด Array
- ์Šคํƒ Stack : ํ•œ ์ชฝ ๋์—์„œ๋งŒ ๋ฐ์ดํ„ฐ๋ฅผ ๋„ฃ๊ณ  ๋บ„ ์ˆ˜ ์žˆ๋Š” ์„ ํ˜• ์ž๋ฃŒ๊ตฌ์กฐ (FILO ๋˜๋Š” LIFO)

## ํ / ๋ฑ

โ†’ deque

```python
from collections import deque

dq = deque()

dq.append(x)
dq.pop()

dq.appendleft(x)
dq.popleft()
```

- ํ Queue : ํ•œ ์ชฝ์—์„œ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๋„ฃ์„ ์ˆ˜๋งŒ ์žˆ๊ณ , ๋‹ค๋ฅธ ํ•œ ์ชฝ์€ ๋ฐ์ดํ„ฐ๋ฅผ ๋บ„ ์ˆ˜๋งŒ ์žˆ๋Š” ์„ ํ˜• ์ž๋ฃŒ๊ตฌ์กฐ (FIFO ๋˜๋Š” LILO)
- ๋ฑ Deque : ์–‘ ์ชฝ ๋ชจ๋‘์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๋„ฃ๊ณ  ๋บ„ ์ˆ˜ ์žˆ๋Š” ์„ ํ˜• ์ž๋ฃŒ๊ตฌ์กฐ

## ์šฐ์„ ์ˆœ์œ„ ํ

โ†’ heap

```python
import heapq

h = []

heapq.heappush(h, x)
heapq.heappop(h)
```

- ์šฐ์„ ์ˆœ์œ„ ํ Priority Queue : ์šฐ์„ ์ˆœ์œ„ ํ๋Š” ๋‚ด๋ถ€์ ์œผ๋กœ ํž™(heap)์ด๋ผ๋Š” ์™„์ „์ด์ง„ํŠธ๋ฆฌ๋กœ ๊ตฌ์„ฑ๋˜์–ด์žˆ๋‹ค. ํŒŒ์ด์ฌ์˜ ๊ฒฝ์šฐ ํž™์˜ ๋ฃจํŠธ ๋…ธ๋“œ์—๋Š” โ€˜์–ธ์ œ๋‚˜โ€™ ์ตœ์†Ÿ๊ฐ’์ด ๋“ค์–ด์žˆ๋‹ค. (์ตœ์†Œํž™)

## ๋งต

โ†’ dict

```python

```

## ์ง‘ํ•ฉ

โ†’ set

## ๊ทธ๋ž˜ํ”„

โ†’ ์ธ์ ‘ ํ–‰๋ ฌ / ์ธ์ ‘ ๋ฆฌ์ŠคํŠธ

**๊ตฌ์„ฑ**

- ์ •์  (Vertex ๋˜๋Š” Node)
- ๊ฐ„์„  (Edge)

**๊ทธ๋ž˜ํ”„์˜ ์—ฐ๊ฒฐ ์š”์†Œ Connected Component**

- ์„œ๋กœ ์™„์ „ํžˆ ๋ถ„๋ฆฌ๋œ ์š”์†Œ๋“ค์„ '์—ฐ๊ฒฐ ์š”์†Œ'๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค.
- ์œ„ ๊ทธ๋ฆผ์€ ๊ฐ๊ฐ์˜ ๊ทธ๋ž˜ํ”„ 2๊ฐœ๊ฐ€ ์•„๋‹Œ, ์—ฐ๊ฒฐ ์š”์†Œ 2๊ฐœ๋กœ ์ด๋ค„์ง„ ๊ทธ๋ž˜ํ”„ 1๊ฐœ ์ž…๋‹ˆ๋‹ค.

### **[๊ทธ๋ž˜ํ”„์˜ ๋ถ„๋ฅ˜](https://beluga9.tistory.com/369#%EA%B-%B-%EB%-E%--%ED%--%--%EC%-D%--%--%EB%B-%--%EB%A-%--)**

**1. ๊ทธ๋ž˜ํ”„์˜ ๋ฐฉํ–ฅ์„ฑ**

![https://blog.kakaocdn.net/dn/bsyTRB/btrAcQbq66m/2DOJGKE0xA2pgbz9k5lWD0/img.png](https://blog.kakaocdn.net/dn/bsyTRB/btrAcQbq66m/2DOJGKE0xA2pgbz9k5lWD0/img.png)

| ๋ฐฉํ–ฅ ๊ทธ๋ž˜ํ”„ | ๋ฐฉํ–ฅ์ด ์ •ํ•ด์ ธ ์žˆ์Œ |
| --- | --- |
| ๋ฌด๋ฐฉํ–ฅ ๊ทธ๋ž˜ํ”„ | ์–ด๋Š ๋ฐฉํ–ฅ์œผ๋กœ ๊ฐ€๋„ ์ƒ๊ด€ ์—†์Œ (์–‘๋ฐฉํ–ฅ ๊ทธ๋ž˜ํ”„) |

**2. ๊ทธ๋ž˜ํ”„์˜ ์ˆœํ™˜์„ฑ**

![https://blog.kakaocdn.net/dn/bamCjZ/btrAcuGq9dD/zTlfXjZ1QwdFB90QpiIvcK/img.png](https://blog.kakaocdn.net/dn/bamCjZ/btrAcuGq9dD/zTlfXjZ1QwdFB90QpiIvcK/img.png)

| ์ˆœํ™˜ ๊ทธ๋ž˜ํ”„ | ์ˆœํ™˜ํ•˜๋Š” ๋ถ€๋ถ„์ด ํ•œ ๊ตฐ๋ฐ๋งŒ ์žˆ์–ด๋„ ์ˆœํ™˜ ๊ทธ๋ž˜ํ”„ |
| --- | --- |
| ๋น„์ˆœํ™˜ ๊ทธ๋ž˜ํ”„ | ์ˆœํ™˜ํ•˜๋Š” ๋ถ€๋ถ„์ด ์ „ํ˜€ ์—†์Œ |

๋ฌด์—‡์„ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ์ธ๊ฐ€?

1. ์—ฐ๊ฒฐ ์š”์†Œ ๊ฐœ์ˆ˜ ๊ตฌํ•˜๊ธฐ

for๋ฌธ์œผ๋กœ ๋ชจ๋“  ๋…ธ๋“œ๋ฅผ ๋Œ๋ฉด์„œ, ๋ฐฉ๋ฌธํ•œ ์  ์—†๋Š” ๋…ธ๋“œ๋ผ๋ฉด bfs์˜ ์‹œ์ž‘ ๋…ธ๋“œ๋กœ ๋Œ€์ž…ํ•œ๋‹ค.

bfs ํ•จ์ˆ˜๊ฐ€ ๋ฐœ๋™๋œ ํšŸ์ˆ˜๊ฐ€ ์—ฐ๊ฒฐ ์š”์†Œ์˜ ๊ฐœ์ˆ˜์ด๋‹ค.

2. ์ตœ๋‹จ๊ฑฐ๋ฆฌ ๊ตฌํ•˜๊ธฐ

bfs์˜ ํ์— ํ˜„์žฌ ์ขŒํ‘œ๋ฅผ appendํ•  ๋•Œ, ํ˜„์žฌ ์ขŒํ‘œ๊นŒ์ง€ ์˜ค๋Š”๋ฐ ๊ฑธ๋ ธ๋˜ ๊ฑฐ๋ฆฌ d๊ฐ’์„ ๊ฐ™์ด appendํ•œ๋‹ค.

๋ชฉ์ ์ง€์— ๋„๋‹ฌํ–ˆ์„ ๋•Œ์˜ d๋Š” ์ตœ๋‹จ๊ฑฐ๋ฆฌ์ž„์ด ๋ณด์žฅ๋œ๋‹ค.

3. ๊ฒฝ๋กœ ๊ตฌํ•˜๊ธฐ

chk ๋ฆฌ์ŠคํŠธ๋Š” ๋ณดํ†ต, ๊ฐ ๋…ธ๋“œ๋ณ„๋กœ True/False ๊ฐ’๋งŒ ๋‹ด๊ธฐ๊ฒŒ๋” ๊ตฌ์„ฑํ•˜์ง€๋งŒ,

๊ฐ ๋…ธ๋“œ๋ณ„๋กœ "์ด ๋…ธ๋“œ์— ์˜ฌ ๋•Œ๊นŒ์ง€ ๊ฑธ๋ฆฐ ๊ฒฝ๋กœ๋“ค(์ž๋ฃŒํ˜•์€ ์ง‘ํ•ฉ)"์„ ๋‹ด์„ ์ˆ˜๋„ ์žˆ๋‹ค.

๊ฒฝ๋กœ๋งŒ ๊ตฌํ•ด์•ผ๋˜๋Š” ๋ฌธ์ œ์ผ ์ˆ˜๋„ ์žˆ๊ณ ,

๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ๊ฒฝ๋กœ์˜ ์ˆ˜๋ฅผ ๊ตฌํ•ด์•ผ๋˜๋Š” ๋ฌธ์ œ์ผ ์ˆ˜๋„ ์žˆ๊ณ ,

์„ธ์„ธํ•œ ๊ฒฝ๋กœ๋“ค์„ ๋งค์ˆœ๊ฐ„ ํŒŒ์•…ํ•˜๊ณ  ์žˆ์–ด์•ผ ํ’€๋ฆฌ๋Š” ๋ฌธ์ œ๋„ ์žˆ์„ ์ˆ˜ ์žˆ๋‹ค.

๋ชฉ์ ์ง€๊ฐ€ ์ •ํ•ด์ ธ์žˆ๋Š”๊ฐ€?

1. ๋ชฉ์ ์ง€๊ฐ€ ์ •ํ•ด์ ธ ์žˆ๋‹ค.

๋‹จ์ˆœํžˆ ํƒ์ƒ‰ ๋…ธ๋“œ๊ฐ€ ๋ชฉ์  ๋…ธ๋“œ์™€ ์ผ์น˜ํ•˜๋Š”์ง€๋งŒ ๋”ฐ์ ธ์„œ ํƒ์ƒ‰์„ ์ข…๋ฃŒํ•œ๋‹ค.

2. ๋ชฉ์ ์ง€๊ฐ€ ์ •ํ•ด์ ธ ์žˆ์ง€ ์•Š๋‹ค.

๋ชฉ์ ์ง€๊ฐ€ ๋ช…์‹œ๋˜์–ด ์žˆ์ง€ ์•Š๋”๋ผ๋„, ๋ถ„๋ช… ํƒ์ƒ‰ ์ข…๋ฃŒ ์กฐ๊ฑด์ด ์žˆ์„ ๊ฒƒ์ด๋‹ค.

ํƒ์ƒ‰ ์ข…๋ฃŒ ์กฐ๊ฑด์„ ๋”ฐ๋กœ ํŒŒ์•…ํ•ด์ค˜์•ผ ํ•œ๋‹ค.

์ž๋ฃŒ๊ตฌ์กฐ์ธ ๊ทธ๋ž˜ํ”„๋ฅผ ์ €์žฅํ•˜๋Š” ๋ฐฉ๋ฒ•

1. ๋…ธ๋“œ๊ฐ„์˜ ์—ฐ๊ฒฐ ์ •๋ณด๋งŒ ์ฃผ์–ด์กŒ์„ ๋•Œ

์ธ์ ‘ ํ–‰๋ ฌ(adj)๋กœ ๊ตฌํ˜„ํ•œ๋‹ค.

2. ๊ฒฉ์žํŒ์ด ์ฃผ์–ด์กŒ์„ ๋•Œ

๊ฒฉ์žํŒ(board) ๊ทธ๋Œ€๋กœ ์ €์žฅํ•œ๋‹ค.

์ด๋™์€ dy, dx๋ฅผ ์ด์šฉํ•œ๋‹ค.


# ์™„์ „ ํƒ์ƒ‰

- ๋ชจ๋“  ๊ฒฝ์šฐ๋ฅผ ๋‹ค ์‚ดํŽด๋ณธ๋‹ค!
- ๊ฑฐ์˜ ๊ฐ™์€ ์˜๋ฏธ์ธ '๋ธŒ๋ฃจํŠธ ํฌ์Šค(๋ฌด์ฐจ๋ณ„ ๋Œ€์ž…)'๋ผ๋Š” ์šฉ์–ด๋กœ๋„ ๋ถˆ๋ฆฐ๋‹ค.
- ์™„์ „ํƒ์ƒ‰์€ ๋ชจ๋“  ๊ฒฝ์šฐ๋ฅผ ์‚ดํŽด๋ณด๊ธฐ์— ์‹œ๊ฐ„์ด ์˜ค๋ž˜ ๊ฑธ๋ฆฝ๋‹ˆ๋‹ค. ์‹œ๊ฐ„์„ ๋‹จ์ถ•์‹œํ‚ค๊ณ  ์‹ถ๋‹ค๋ฉดย **๋ฐฑํŠธ๋ž˜ํ‚น**์„ ์‚ฌ์šฉํ•œ๋‹ค.ย ๋ฐฑํŠธ๋ž˜ํ‚น์ด๋ž€ ํƒ์ƒ‰ ๊ณผ์ •์—์„œ ๋” ์ด์ƒ ๋‹ต์ด ๋˜์ง€ ์•Š๋Š” ๋ถ„๊ธฐ๋ฅผ ๋ฐœ๊ฒฌํ–ˆ์„ ๋•Œ ๋˜๋Œ์•„๊ฐ€๋Š” ๊ธฐ๋ฒ•์„ ์˜๋ฏธํ•œ๋‹ค.
- ์™„์ „ํƒ์ƒ‰์€ ํฌ๊ฒŒ ๋ฐ˜๋ณต๋ฌธ / ์žฌ๊ท€ / ํ๋กœ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.
- ์žฌ๊ท€(dfs)๋กœ ํ’€์—ˆ๋Š”๋ฐ ์‹œ๊ฐ„ / ๋ฉ”๋ชจ๋ฆฌ ์ดˆ๊ณผ๋‚˜๋ฉด ๋ฐ˜๋ณต๋ฌธ์œผ๋กœ ํ’€์–ด๋ณด๊ธฐ

- ์กฐํ•ฉ์˜ ๊ฐ€์ง“์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ๋Š” ํŒŒ์Šค์นผ์˜ ์‚ผ๊ฐํ˜• ๊ณต์‹์„ ์ด์šฉํ•˜์—ฌ dp๋กœ ํ’€ ์ˆ˜๋„ ์žˆ๋‹ค.

| | |
| --- | --- |
| ์ˆœ์—ด | dfs, ๋ฐฑํŠธ๋ž˜ํ‚น์œผ๋กœ ๊ตฌํ˜„ |
| ์ค‘๋ณต์ˆœ์—ด | ์ˆœ์—ด๊ณผ ๋˜‘๊ฐ™์ด ๊ตฌํ˜„ํ•˜๋˜, ๋ฐฉ๋ฌธ์ฒดํฌ๋ฅผ ํ•˜์ง€ ์•Š์Œ |
| ์กฐํ•ฉ | ์ˆœ์—ด๊ณผ ๋˜‘๊ฐ™์ด ๊ตฌํ˜„ํ•˜๋˜, ์ธ๋ฑ์Šค๊ฐ€ ์˜ค๋ฆ„์ฐจ์ˆœ์„ ๋งŒ์กฑํ•˜๊ณ , ๋ฐฉ๋ฌธ์ฒดํฌ๋ฅผ ํ•˜์ง€ ์•Š์Œ |
| ์ค‘๋ณต์กฐํ•ฉ | ์ˆœ์—ด๊ณผ ๋˜‘๊ฐ™์ด ๊ตฌํ˜„ํ•˜๋˜, ์ธ๋ฑ์Šค๊ฐ€ ๋น„๋‚ด๋ฆผ์ฐจ์ˆœ์„ ๋งŒ์กฑํ•˜๊ณ , ๋ฐฉ๋ฌธ์ฒดํฌ๋ฅผ ํ•˜์ง€ ์•Š์Œ |

## ์ˆœ์—ด

n๊ฐœ์˜ ์ˆ˜ ์ค‘ r๊ฐœ๋ฅผ ๋ฝ‘์•„ ์ค„์„ ์„ธ์šฐ๋Š” ๋ฐฉ๋ฒ•์˜ ์ˆ˜

- ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•

```python
from itertools import permutations

arr = [0, 1, 2, 3]

for i in permutations(arr, 4):
print(i)
```

- ์ง์ ‘ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•

```python
chk = [False] * (N + 1)
ans = []

def permutation(n, m):
if m == 0:
print(*ans)
else:
for i in range(1, n + 1):
if not chk[i]:
chk[i] = True
ans.append(i)
permutation(n, m - 1)
ans.pop()
chk[i] = False

permutation(N, M)
```

## ์ค‘๋ณต ์ˆœ์—ด

n๊ฐœ์˜ ์ˆ˜ ์ค‘ ์ค‘๋ณต์„ ํ—ˆ์šฉํ•˜์—ฌ r๊ฐœ๋ฅผ ๋ฝ‘์•„ ์ค„์„ ์„ธ์šฐ๋Š” ๋ฐฉ๋ฒ•์˜ ์ˆ˜

- ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•

```python
from itertools import product

arr = [0, 1, 2 ,3]

for i in product(arr, repeat = 2):
print(i)
```

- ์ง์ ‘ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•

```python
ans = []

def permutation(n, m):
if not m:
print(*ans)
else:
for i in range(1, n + 1):
ans.append(i)
permutation(n, m - 1)
ans.pop()

permutation(N, M)
```

## ์กฐํ•ฉ

n๊ฐœ์˜ ์ˆ˜ ์ค‘ r๊ฐœ๋ฅผ ๋ฝ‘๋Š” ๋ฐฉ๋ฒ•์˜ ์ˆ˜

- ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•

```python
from itertools import combinations

arr = [0, 1, 2, 3]

for i in combinations(arr, 2):
print(i)
```

- ์ง์ ‘ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•

```python
ans = []

def combination(n, m, idx):
if not m:
print(*ans)
else:
for i in range(idx, n):
ans.append(i)
combination(n, m - 1, i + 1)
ans.pop()

combination(N, M, 0)
```

## ์ค‘๋ณต ์กฐํ•ฉ

n๊ฐœ์˜ ์ˆ˜ ์ค‘ ์ค‘๋ณต์„ ํ—ˆ์šฉํ•˜์—ฌ r๊ฐœ๋ฅผ ๋ฝ‘๋Š” ๋ฐฉ๋ฒ•์˜ ์ˆ˜

- ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•

```python
from itertools import combinations_with_replacement

arr = [0, 1, 2, 3]

for i in combinations_with_replacement(arr, 2):
print(i)
```

- ์ง์ ‘ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•

```python
ans = []

def combination(n, m, idx):
if not m:
print(*ans)
else:
for i in range(idx, n + 1):
ans.append(i)
permutation(n, m - 1, i)
ans.pop()

combination(N, M, 1)
```

## DFS

๊นŠ์ด์šฐ์„ ํƒ์ƒ‰

์žฌ๊ท€

## BFS

๋„ˆ๋น„์šฐ์„ ํƒ์ƒ‰

ํ

```python
# ๊ธธ์ฐพ๊ธฐ ๋ฌธ์ œ
from collections import deque

dy = (0, 1, 0, -1)
dx = (-1, 0, 1, 0)
chk = [[False] * X for _ in range(Y)]

def is_valid_coord(y, x):
return 0 <= y < Y and 0 <= x < X

def dfs(y, x):
if adj[y][x] == ans:
return

for k in range(4):
ny = y + dy[k]
nx = x + dx[k]
if is_valid_coord(ny, nx) and not chk[ny][nx]:
chk[ny][nx] = True
dfs(ny, nx)

def bfs(sy, sx):
q = deque()
chk[sy][sx] = True
q.append((sy, sx))
while q:
y, x = q.popleft()
if adj[y][x] == ans:
return

for k in range(4):
ny = y + dy[k]
nx = x + dx[k]
if is_valid_coord(ny, nx) and not chk[ny][nx]:
chk[ny][nx] = True
q.append((ny, nx))
```


# ์ด๋ถ„ ํƒ์ƒ‰

## ์„ ํ˜• ํƒ์ƒ‰

์•ž์—์„œ๋ถ€ํ„ฐ ์ฐจ๋ก€๋Œ€๋กœ ์ฐพ๋Š” ๊ฒƒ

## ์ด๋ถ„ ํƒ์ƒ‰

ํƒ์ƒ‰ ๋ฒ”์œ„๋ฅผ ์ ˆ๋ฐ˜์œผ๋กœ ์ค„์—ฌ๊ฐ€๋ฉด์„œ ์ฐพ๋Š” ๊ฒƒ

๋ฐฐ์—ด ์•ˆ์˜ ๊ฐ’๋“ค์ด ๋ฐ˜๋“œ์‹œ ์ •๋ ฌ๋˜์–ด ์žˆ์–ด์•ผ ํ•œ๋‹ค.

- ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•

```python
from bisect import bisect_left, bisect_right

l = bisect_left(arr, key) #key๊ฐ’ ์ด์ƒ์ธ ๊ฐ’์˜ ์ฒ˜์Œ ๋“ฑ์žฅ ์œ„์น˜
r = bisect_right(arr, key) #key๊ฐ’ ์ดˆ๊ณผ์ธ ๊ฐ’์˜ ์ฒ˜์Œ ๋“ฑ์žฅ ์œ„์น˜

r - l #๋ฐฐ์—ด์— ๋“ค์–ด์žˆ๋Š” key๊ฐ’์˜ ๊ฐœ์ˆ˜ (๋ฆฌ์ŠคํŠธ์˜ count ํ•จ์ˆ˜๋ณด๋‹ค ๋น ๋ฅด๋‹ค)
```

**bisect_left()**ย : ๋ชฉํ‘œ ๊ฐ’๋ณด๋‹ค ๊ฐ™๊ฑฐ๋‚˜ ํฐ ์ฒซ ๋ฒˆ์งธ ๊ฐ’์˜ ์œ„์น˜ ๋ฐ˜ํ™˜

**bisect_right()**ย : ๋ชฉํ‘œ ๊ฐ’๋„๊ฐ€ ํฐ ์ฒซ ๋ฒˆ์งธ ๊ฐ’์˜ ์œ„์น˜ ๋ฐ˜ํ™˜

**bisect_right() - bisect_left()**ย : ๋ชฉํ‘œ ๊ฐ’์ด ๋ช‡ ๊ฐœ ์žˆ๋Š”์ง€ ์•Œ ์ˆ˜ ์žˆ์Œ

- ์ง์ ‘ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•

```python
l = 0
r = len(arr) - 1
m = (l + r) // 2

while l <= r:
if arr[m] == key:
break
elif arr[m] > key:
r = m - 1
else:
l = m + 1

m = (l + r) // 2
```

## ๋งค๊ฐœ๋ณ€์ˆ˜ ํƒ์ƒ‰

Parametric Search

์ตœ์ ํ™” ๋ฌธ์ œ๋ฅผ ๊ฒฐ์ • ๋ฌธ์ œ๋กœ ๋ฐ”๊ฟ” ํ‘ธ๋Š” ํƒ์ƒ‰ ์•Œ๊ณ ๋ฆฌ์ฆ˜

| ์ตœ์ ํ™” ๋ฌธ์ œOptimization Problem | ๊ฒฐ์ • ๋ฌธ์ œDecision Problem |
| --- | --- |
| ์ƒํ™ฉ์„ ๋งŒ์กฑํ•˜๋Š” ๋ณ€์ˆ˜์˜ ์ตœ์†Ÿ๊ฐ’, ์ตœ๋Œ“๊ฐ’์„ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ | Yes, No ์ค‘ ํ•˜๋‚˜๋กœ ๋‹ตํ•  ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ |
- ์ด๋ถ„ ํƒ์ƒ‰ ๋ฐฉ์‹์œผ๋กœ ์–ด๋–ค ๋‘ ๊ทธ๋ฃน์˜ ๊ฒฝ๊ณ„์„ ์„ ์ฐพ์•„๋ƒ…๋‹ˆ๋‹ค.
- ์ตœ์ข…์ ์œผ๋กœ ๊ฒฝ๊ณ„์„ ์˜ ์™ผ์ชฝ ๋ฒ”์œ„๋ฅผ ํƒํ• ์ง€, ์˜ค๋ฅธ์ชฝ ๋ฒ”์œ„๋ฅผ ํƒํ• ์ง€ ์—ฌ๋ถ€๋ฅผ Yes/No ๊ฒฐ์ • ๋ฌธ์ œ์˜ ๊ฒฐ๊ณผ๋กœ ์ •ํ•ฉ๋‹ˆ๋‹ค.
- ํŒŒ๋ผ๋ฉ”ํŠธ๋ฆญ ์„œ์น˜์˜ ๋™์ž‘ ๋ฐฉ์‹์€ ์‚ฌ์‹ค์ƒ ์ด๋ถ„ํƒ์ƒ‰๊ณผ ๋‹ค๋ฅผ ๋ฐ” ์—†์Šต๋‹ˆ๋‹ค.
- Binary Search์™€ย Parametric Search์˜ ์ฐจ์ด์ ์€,ย Binary Search์—์„œ๋Š” ๋ฐฐ์—ด์—์„œ X์™€ ์ผ์น˜ํ•˜๋Š” ๊ฐ’์„ ์ฐพ์œผ๋ฉด ๋ฐ”๋กœ ํ•จ์ˆ˜๋ฅผ ์ข…๋ฃŒํ•˜๊ณ  ์œ„์น˜๋ฅผ ๋ฐ˜ํ™˜ํ•˜์ง€๋งŒ,ย Parametric Search๋Š” ํ•จ์ˆ˜๋ฅผ ์ข…๋ฃŒํ•˜์ง€ ์•Š๊ณ  ๋” ์ด์ƒ ์‚ดํŽด๋ณผ ๋ฐฐ์—ด์ด ๋‚จ์•„์žˆ์ง€ ์•Š์„๋•Œ๊นŒ์ง€ ํƒ์ƒ‰์„ ๊ณ„์†ํ•ฉ๋‹ˆ๋‹ค.
- ํŒŒ๋ผ๋ฉ”ํŠธ๋ฆญ ์„œ์น˜ ๊ด€๋ จ ๋ฌธ์ œ

**[https://beluga9.tistory.com/375](https://beluga9.tistory.com/375)**


# ๋™์  ๊ณ„ํš๋ฒ•

Dynamic Programming(DP)

๋ฌธ์ œ๋ฅผ ์ชผ๊ฐœ๊ณ , ๋ถ€๋ถ„ ๋ฌธ์ œ๋“ค์˜ ๋‹ต์„ ๊ตฌํ•˜๋Š” ๊ณผ์ •์„ ๋ฐ˜๋ณตํ•˜๋ฉฐ ํ‘ธ๋Š” ๋ฐฉ์‹

๋Œ€์ถฉ n๋ฒˆ์งธ ๊ฐ’์„ ๊ตฌํ•˜๊ณ  ์‹ถ์€๋ฐ n๋ฒˆ์งธ ๊ฐ’์ด ์•ž์—์„œ ๊ตฌํ•ด๋†จ๋˜ ๊ฐ’๋“ค(0~n-1๋ฒˆ์งธ ๊ฐ’๋“ค)์„ ํ™œ์šฉํ•ด์„œ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ๋“ค์„ dp๋ผ๊ณ  ํ•จ

์ ํ™”์‹์„ ์„ธ์šฐ๋Š” ๋ฌธ์ œ๋“ค์ด dp๋ฌธ์ œ

๋ฉ”๋ชจ์ด์ œ์ด์…˜ Memoization

- ํ•œ ๋ฒˆ ๊ตฌํ•œ ๋ถ€๋ถ„ ๋ฌธ์ œ์˜ ๋‹ต์„ ๋”ฐ๋กœ ์ €์žฅํ•ด๋‘๊ณ  ํ•„์š”ํ•  ๋•Œ ๊บผ๋‚ด์“ฐ๋Š” ๊ธฐ๋ฒ•์ž…๋‹ˆ๋‹ค.
- ์บ์‹ฑ(caching)์ด๋ผ๊ณ ๋„ ํ•ฉ๋‹ˆ๋‹ค.
- ๋ฉ”๋ชจ์ด์ œ์ด์…˜์€ DP์™€๋Š” ๋ณ„๊ฐœ์˜ ๊ฐœ๋…์ด๊ธด ํ•˜๋‚˜, DP ๋ฌธ์ œ๋ฅผ ํ’€ ๋•Œ ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋‚˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์•„ ์‹œ๊ฐ„ ๋‹จ์ถ•์„ ์œ„ํ•ด ๊ฐ™์ด ์ž์ฃผ ์“ฐ์ด๋Š” ๊ฐœ๋…์ž…๋‹ˆ๋‹ค.

## ์ ‘๊ทผ ๋ฐฉ์‹

### Top-down

- ์žฌ๊ท€ ์‚ฌ์šฉ
- ํ•˜ํ–ฅ์‹ ์ ‘๊ทผ(Top-down) ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค.
- ํฐ ๋ฌธ์ œ์—์„œ ์‹œ์ž‘ํ•˜์—ฌ ์ ์  ์ž‘์€ ๋ถ€๋ถ„ ๋ฌธ์ œ์˜ ๋‹ต์„ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด ๋‚ด๋ ค๊ฐ€๋Š” ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค.
- ์–ด๋–ค ๋ถ€๋ถ„ ๋ฌธ์ œ์˜ ๋‹ต์ด ํ•„์š”ํ•œ ๊ฒฝ์šฐ์— ๋‹ฅ์น˜๋ฉด ๊ทธ ๋•Œ ๊ตฌํ•ฉ๋‹ˆ๋‹ค. (Lazy-Evaluation ๋ฐฉ์‹)

### Bottom-up

- ๋ฐ˜๋ณต๋ฌธ ์‚ฌ์šฉ
- ์ƒํ–ฅ์‹ ์ ‘๊ทผ(Bottom-up) ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค.
- ์ž‘์€ ๋ถ€๋ถ„ ๋ฌธ์ œ๋ถ€ํ„ฐ ์ˆœ์ฐจ์ ์œผ๋กœ ์ ์  ํฐ ๋ฌธ์ œ๋ฅผ ํ’€์–ด๊ฐ€๋Š” ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค.
- ๋ชจ๋“  ๋ถ€๋ถ„ ๋ฌธ์ œ์˜ ๋‹ต์„ ๊ตฌํ•ด ๋‘๊ณ  ๋ฌธ์ œ๋ฅผ ํ’‰๋‹ˆ๋‹ค. (Eager-Evaluation ๋ฐฉ์‹)
- ์ด๋ ‡๊ฒŒ ํ‘ธ๋Š” ๋ฐฉ์‹์„ ํƒ€๋ทธ๋ ˆ์ด์…˜(Tabulation)์ด๋ผ๊ณ ๋„ ๋ถ€๋ฆ…๋‹ˆ๋‹ค.

### Top-down vs Bottom-up

| ๋น„๊ต | Top-down | Bottom-up |
| --- | --- | --- |
| ๊ตฌํ˜„ | ์žฌ๊ท€ | ๋ฐ˜๋ณต๋ฌธ |
| ์žฅ์  | ์ง๊ด€์ , ์ฝ”๋“œ ๊ฐ€๋…์„ฑ ์ข‹์Œ | ์‹œ๊ฐ„, ๊ณต๊ฐ„ ํšจ์œจ์„ฑ์ด ์ข‹์Œ |
| ๋‹จ์  | ์žฌ๊ท€ํ•จ์ˆ˜ ํ˜ธ์ถœ์ด ๋งŽ์ด ๋Š๋ฆด ์ˆ˜ ์žˆ์Œ | DPํ…Œ์ด๋ธ”์„ ์ฑ„์›Œ ๋‚˜๊ฐ€๋Š” ์ˆœ์„œ ํŒŒ์•…์ด ํž˜๋“ฆ |

## ๋Œ€ํ‘œ ์œ ํ˜•

### LIS

Longest Increasing Sequence

๊ฐ€์žฅ ๊ธด ์ฆ๊ฐ€ํ•˜๋Š” ๋ถ€๋ถ„ ์ˆ˜์—ด

### Knapsack


# ํƒ์š•๋ฒ•

- ํ•ญ์ƒ ํ˜„์žฌ ์ƒํƒœ์—์„œ ์ตœ์„ ์˜ ๊ฒฝ์šฐ๋งŒ ํƒ์š•์Šค๋Ÿฝ๊ฒŒ ๊ณ ๋ฅด๋Š” ์ „๋žต
- ์™„์ „ํƒ์ƒ‰๊ณผ ๋‹ฌ๋ฆฌ ๋ชจ๋“  ๊ฒฝ์šฐ๋ฅผ ์‚ดํŽด๋ณด์ง€๋Š” ์•Š๋Š”๋‹ค.
- ๊ทธ๋ฆฌ๋”” ๋ฌธ์ œ๋“ค์€ ํŠน๋ณ„ํ•œ ๋ฐฐ๊ฒฝ์ง€์‹์ด ์—†๋”๋„ ๋ฌธ์ œ์—์„œ ๊ทœ์น™์„ฑ์„ ์ฐพ์œผ๋ฉด ํ’€ ์ˆ˜ ์žˆ๋Š” ํŽธ์ด๋‹ค.
- ๋‹ค๋งŒ ๋‚œ์ด๋„๋ฅผ ์•„์ฃผ ์‰ฌ์šด ๋ฌธ์ œ๋ถ€ํ„ฐ ๋งค์šฐ ์–ด๋ ต๊ฒŒ๊นŒ์ง€, ์ถœ์ œ์ž ๋งˆ์Œ๋Œ€๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.
- ๊ทธ๋ฆฌ๋”” ๋ฌธ์ œ์˜ ์ง„์งœ ์–ด๋ ค์šด ๋ถ€๋ถ„์€ ๋ฐ”๋กœ '**์ด ๋ฌธ์ œ๊ฐ€ ๊ทธ๋ฆฌ๋”” ๋ฌธ์ œ์ธ์ง€ ํŒ๋ณ„ํ•˜๋Š” ๊ฒƒ**'

## ํ™œ๋™ ์„ ํƒ ๋ฌธ์ œ

[**[https://www.acmicpc.net/problem/1931](https://www.acmicpc.net/problem/1931)**](https://www.notion.so/https-www-acmicpc-net-problem-1931-b0df1f84c94e4296beea983da8ffedda)

# ์†Œ์ˆ˜

# ์•ฝ์ˆ˜์™€ ๋ฐฐ์ˆ˜
์ตœ๋Œ€๊ณต์•ฝ์ˆ˜ / ์ตœ์†Œ๊ณต๋ฐฐ์ˆ˜
๊ทœ์น™ 1. x์™€ y์˜ ์ตœ๋Œ€๊ณต์•ฝ์ˆ˜๋Š” y์™€ x % y์˜ ์ตœ๋Œ€๊ณต์•ฝ์ˆ˜์™€ ๊ฐ™๋‹ค. (์œ ํด๋ฆฌ๋“œ ํ˜ธ์ œ๋ฒ•)

๊ทœ์น™ 2. ๋‘ ์ˆ˜์˜ ๊ณฑ(x * y)๋Š” ๋‘ ์ˆ˜์˜ ์ตœ๋Œ€๊ณต์•ฝ์ˆ˜์™€ ์ตœ์†Œ๊ณต๋ฐฐ์ˆ˜์˜ ๊ณฑ๊ณผ ๊ฐ™๋‹ค.

์œ„ ๋‘ ๊ทœ์น™์„ ์ด์šฉํ•˜์—ฌ ์•„๋ž˜์™€ ๊ฐ™์ด ์ตœ๋Œ€๊ณต์•ฝ์ˆ˜์™€ ์ตœ์†Œ๊ณต๋ฐฐ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ์งค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

```python
# x > y
def gcd(x, y):
while y > 0:
x, y = y, x % y
return x

def lcm(x, y):
return x * y // gcd(x, y)
```

์•„๋‹ˆ๋ฉด ๊ฐ„๋‹จํ•˜๊ฒŒ math ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ importํ•ด์„œ gcd์™€ lcm ๋ฉ”์„œ๋“œ๋ฅผ ์“ธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

(lcm ๋ฉ”์„œ๋“œ๋Š” ํŒŒ์ด์ฌ ๋ฒ„์ „์— ๋”ฐ๋ผ ์ง€์›ํ•˜์ง€ ์•Š์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ, gcd๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•ด์„œ ์ตœ์†Œ๊ณต๋ฐฐ์ˆ˜๋ฅผ ๊ตฌํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.)

```python
import math

math.gcd(a, b)
math.lcm(a, b)
```

# ์œ„์ƒ์ •๋ ฌ

-> bfs ์‚ฌ์šฉ

1. ์ง„์ž…์ฐจ์ˆ˜๊ฐ€ 0์ธ ๋…ธ๋“œ(์‹œ์ž‘์ )์„ ํ์— ๋ชจ๋‘ ๋„ฃ๋Š”๋‹ค
2. ํ์—์„œ ์ง„์ž… ์ฐจ์ˆ˜๊ฐ€ 0์ธ ๋…ธ๋“œ๋ฅผ ๊บผ๋‚ด์–ด ์ž์‹ ๊ณผ ์ธ์ ‘ํ•œ ๋…ธ๋“œ์˜ ๊ฐ„์„ ์„ ์ œ๊ฑฐํ•œ๋‹ค.
3. ๊ฐ„์„  ์ œ๊ฑฐ ํ›„ ์ง„์ž…์ฐจ์ˆ˜๊ฐ€ 0์ด ๋œ ๋…ธ๋“œ๋ฅผ ํ์— ๋„ฃ๋Š”๋‹ค.
- > ํ๊ฐ€ ๋นŒ ๋•Œ๊นŒ์ง€ 2-3์ž‘์—…์„ ๋ฐ˜๋ณตํ•œ๋‹ค.
- ๋ชจ๋“  ๋…ธ๋“œ๊ฐ€ ์ฒ˜๋ฆฌ๋˜์—ˆ๋‹ค๋ฉด ์œ„์ƒ์ •๋ ฌ ์™„์„ฑ
- ๋ชจ๋“  ๋…ธ๋“œ๊ฐ€ ์ฒ˜๋ฆฌ๋˜์ง€ ์•Š์•˜๋‹ค๋ฉด ์‚ฌ์ดํด ๋ฐœ์ƒ


# ๊ฐ€์ค‘์น˜๊ฐ€ ์žˆ๋Š” ์ตœ๋‹จ๊ฑฐ๋ฆฌ
๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜ : ์–ด๋Š ํ•œ ์‹œ์ž‘ ์ •์ ์„ ๊ธฐ์ค€์œผ๋กœ, ๋ชจ๋“  ๋‹ค๋ฅธ ์ •์ ๊นŒ์ง€์˜ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜
ํ”Œ๋กœ์ด๋“œ์›Œ์…œ..
MST (ํฌ๋ฃจ์Šค์นผ, ํ”„๋ฆผ)...