๋ฐ์ํ
Notice
Recent Posts
Recent Comments
Link
| ์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
|---|---|---|---|---|---|---|
| 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 |
Tags
- kakao
- Python
- SSAFY
- SWEA
- BFS
- javascript
- algorithm
- boj
- ์ธํผ
- ์คํ
- sort
- DFS
- ์๊ณ ๋ฆฌ์ฆ
- Backjoon
- ๋ฐฑ์ค
- ๊ทธ๋ํ
- Blind
- SW์ญ๋ํ ์คํธ
- ์๋ฃ๊ตฌ์กฐ
- ์ฝํ
- ์๋ฐ์คํฌ๋ฆฝํธ
- ๋ค์ด๋๋ฏนํ๋ก๊ทธ๋๋ฐ
- DP
- ์ผ์ฑ
- ์์ ํ์
- ํ๋ก๊ทธ๋๋จธ์ค
- ์ฝ๋ฉํ ์คํธ
- ํ์ด์ฌ
- ์นด์นด์ค
- ํํ
Archives
- Today
- Total
๋ง์ํ
[python] ํ๋ก๊ทธ๋๋จธ์ค - ์คํ ์์ด(์๊ฐ ์ฝ๋ ์ฑ๋ฆฐ์ง ์์ฆ1) ๋ณธ๋ฌธ
Algorithm Problem/Python
[python] ํ๋ก๊ทธ๋๋จธ์ค - ์คํ ์์ด(์๊ฐ ์ฝ๋ ์ฑ๋ฆฐ์ง ์์ฆ1)
deo2kim 2022. 2. 9. 22:06๋ฐ์ํ

๐ค๋ฌธ์ ํด๊ฒฐ
1. ์ฃผ์ด์ง ๋ฐฐ์ด์ ์ซ์์ ๊ฐ์๋ฅผ ์ผ๋ค. -> Counter ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ด์ฉ

2. ์ฃผ์ด์ง ๋ฐฐ์ด์์ ๊บผ๋ธ ์ซ์์ ์ธ๋ฑ์ค๋ฅผ ๊ตฌํ๋ค.

3. ๊ตฌํ ์ธ๋ฑ์ค์ ์ผ์ชฝ or ์ค๋ฅธ์ชฝ์ ์ซ์๋ฅผ ์ ์ ํ๊ฒ ์ฌ์ฉํ ์ ์๋์ง ํ๋จํ๊ณ ์คํ ์์ด์ ๋ง๋ ๋ค.

๐จ Counter ๋ผ์ด๋ธ๋ฌ๋ฆฌ ๋์ { 1: [0, 3, 6, 9], ... } ์ด๋ฐ์์ผ๋ก ์ธ๋ฑ์ค๋ฅผ ๋ฃ์ด์ค์ ๊ธธ์ด์ ์ธ๋ฑ์ค๋ฅผ ํ๊บผ๋ฒ์ ์๊ธฐ ์ฝ๊ฒํ๋ ๋ฐฉ๋ฒ๋ ๋ดค๋ค.
๐จ ์ค๊ฐ ์ค๊ฐ ๊ธธ์ด๊ฐ ์งง์ ์ ๋ค์ ๋์ด์ฃผ์ง ์์ผ๋ฉด ์๊ฐ์ด๊ณผ ๋ ๊ฒ ๊ฐ๋ค.
๐ป์์ค ์ฝ๋
from collections import Counter
def solution(a):
# ์ฃผ์ด์ง ๋ฐฐ์ด์ ์ซ์์ ๊ฐ์๋ฅผ ๊ตฌํ๋ค.
number_cnt = Counter(a)
answer = 0
# ๊ตฌํ ์ซ์๋ฅผ ํ๋ ์ฉ ๊บผ๋ด์
for standard_number in number_cnt.keys():
if number_cnt[standard_number] * 2 < answer: # ์ด์ ์ ์์ฑํ ์คํ์์ด์ ๊ธธ์ด๋ณด๋ค ๊บผ๋ธ ์ซ์์ ๊ฐ์๊ฐ ์ ์ผ๋ฉด
continue # ์คํ์์ด์ ์์ฑํด๋ ๊ธธ์ด๊ฐ ์งง์ผ๋ฏ๋ก ํจ์ค
number_index_list = [] # ์ฃผ์ด์ง ๋ฐฐ์ด์์ ๊บผ๋ธ ์ซ์์ ์ธ๋ฑ์ค๋ฅผ ๊ตฌํ๋ค.
for i in range(len(a)):
if a[i] == standard_number:
number_index_list.append(i)
seq = [] # ์คํ์์ด์ ์ ์ฅํ ๋ฐฐ์ด
before_selected_idx = -1 # ๊บผ๋ธ ์ซ์์ ์ผ์ชฝ or ์ค๋ฅธ์ชฝ์ ์ซ์๋ฅผ ์ฌ์ฉํ ๋ ๊ฒน์ณ์ ์ฌ์ฉํ์ง ์๊ธฐ ์ํด์
cnt = 0 # ์คํ์์ด์ ๊ธธ์ด ์ ์ฅ
for idx in number_index_list:
if 0 <= idx - 1 != before_selected_idx and a[idx] != a[idx - 1]: # ๊ธฐ์ค ์ซ์์ ์ผ์ชฝ ์ซ์ ์ฌ์ฉ
seq.append([a[idx - 1], a[idx]])
before_selected_idx = idx - 1
cnt += 1
elif len(a) > idx + 1 != before_selected_idx and a[idx] != a[idx + 1]: # ๊ธฐ์ค ์ซ์์ ์ค๋ฅธ์ชฝ ์ซ์ ์ฌ์ฉ
seq.append([a[idx], a[idx + 1]])
before_selected_idx = idx + 1
cnt += 1
else:
answer = cnt * 2
return answer
๐๋ฌธ์ ํ์ธ
์ถ์ฒ: ํ๋ก๊ทธ๋๋จธ์ค
๋งํฌ: ์คํ ์์ด
๋ฐ์ํ