ํฐ์คํ ๋ฆฌ ๋ทฐ
algorithm/baekjoon
[๋ฐฑ์ค] 2003. ์๋ค์ ํฉ 2 / python ํ์ด์ฌ / ํฌํฌ์ธํฐ, ๊ตฌ๊ฐํฉ
jen jen 2021. 6. 30. 00:14728x90
๐ ํฌํฌ์ธํฐ(Two Pointers)
๋ฆฌ์คํธ์ ์์ฐจ์ ์ผ๋ก ์ ๊ทผํด์ผ ํ ๋ start point์ end point์ ์์น๋ฅผ ๊ธฐ๋กํ๋ฉด์ ํด๊ฒฐํ๋ ๋ฐฉ๋ฒ
๐ ๊ตฌ๊ฐ ํฉ(Prefix Sum)
โพ ๋ถ๋ถ ํฉ : 0~k
๊น์ง์ ํฉ
โพ ๊ตฌ๊ฐ ํฉ : i~j
๊น์ง์ ํฉ
N = 5
lst = [10, 20, 30, 40, 50]
# Prefix Sum ๋ฐฐ์ด ๊ณ์ฐ
tmp = 0
prefix_sum = [0]
for num in lst:
tmp += num
prefix_sum.append(tmp)
# ๊ตฌ๊ฐ ํฉ ๊ณ์ฐ
left = 3
right = 4
print(prefix_sum[right] - prefix_sum[left-1])
boj 2003 ์ฝ๋
def sol(N, M, a):
s, e = 0, 0
subsum = a[0]
cnt = 0
while True:
if subsum < M: # ์์ผ๋ฉด end point +1
e += 1
if e >= N: # ์ธ๋ฑ์ค ๋ฒ์ ์ฃผ์
break
subsum += a[e]
elif subsum == M: # ๊ฐ์ผ๋ฉด cnt +1
cnt += 1
subsum -= a[s]
s += 1
else: # ํฌ๋ฉด start point +1
subsum -= a[s]
s += 1
return cnt
N, M = map(int, input().split())
a = list(map(int, input().split()))
print(sol(N, M, a))
'algorithm > baekjoon' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฐฑ์ค] 20057. ๋ง๋ฒ์ฌ ์์ด์ ํ ๋ค์ด๋ / python ํ์ด์ฌ (1) | 2021.07.05 |
---|---|
[๋ฐฑ์ค] 2579. ๊ณ๋จ์ค๋ฅด๊ธฐ / python ํ์ด์ฌ (0) | 2021.06.30 |
[๋ฐฑ์ค] 1927. ์ต์ํ / 11279. ์ต๋ํ / python ํ์ด์ฌ (0) | 2021.06.06 |
[๋ฐฑ์ค] 10026. ์ ๋ก์์ฝ / python ํ์ด์ฌ (0) | 2021.06.03 |
[๋ฐฑ์ค] 1107. ๋ฆฌ๋ชจ์ปจ / python ํ์ด์ฌ (0) | 2021.06.03 |
๋๊ธ
๊ธ ๋ณด๊ดํจ
TAG
- Python
- dp
- ํ๋ก๊ทธ๋๋จธ์ค
- react
- ์์ด๋๋ง์๊ธฐ
- 2579 ๊ณ๋จ์ค๋ฅด๊ธฐ
- 20057 ๋ง๋ฒ์ฌ ์์ด์ ํ ๋ค์ด๋
- BFS
- 17406 ๋ฐฐ์ด๋๋ฆฌ๊ธฐ4
- merge ์๋ฌ
- ๊ธฐ์ง๊ตญ์ค์น
- ๋ณด์์ผํ
- ์๊ณ ๋ฆฌ์ฆ
- 20056 ๋ง๋ฒ์ฌ ์์ด์ ํ์ด์ด๋ณผ
- ์ผ์ฑ๊ธฐ์ถ
- git ๋ฏธ๋ฌ๋ง
- swea
- dfs
- ๋ฐฑ์ค
- ์ผ์ฑ์ฝํ
- 21609 ์์ด ์คํ๊ต
- merge์๋ฌ
- ํ์ด์ฌ
- 2018 ์นด์นด์ค ๊ณต์ฑ
- ๋ธ๋ฃจํธํฌ์ค
์ต๊ทผ์ ์ฌ๋ผ์จ ๊ธ
- Total
- Today
- Yesterday
์ต๊ทผ์ ๋ฌ๋ฆฐ ๋๊ธ