ํฐ์คํ ๋ฆฌ ๋ทฐ
๐จ๐ป ์ฝ๋ฉํ
์คํธ/Codility
Lesson 3: Complexity → Tape Equilibrium
dirmathfl 2020. 6. 6. 19:35728x90
๋ฐ์ํ
๋ฌธ์
https://app.codility.com/programmers/lessons/3-time_complexity/tape_equilibrium/
๋ฐฐ์ด์ ๋ค์ฏ๊ฐ์ ๊ฐ๋ค์ด ์์ผ๋ฉด ๊ฐ ๊ตฌ๊ฐ ๋ณ๋ก ๋๋์ด ๊ฐ์ ํฉ์ฐ ํ์ ์ฐจ๋ฅผ ๊ณ์ฐํ์ฌ ๊ฐ์ฅ ์์ ์ฐจ๊ฐ ๋๋ ๊ฐ์ ์ฐพ๋ ๋ฌธ์ ์ด๋ค.
๋ฌธ์ ํ์ด
์ฒ์์๋ list๋ฅผ ์ฌ๋ผ์ด์ฑํ์ฌ ๋ฌธ์ ์ ์ ๊ทผํ์๋ค. ์ฌ๋ผ์ด์ฑ์ผ๋ก ์ธํ ์ค๋ฒํค๋๊ฐ ๊ทธ๋ ๊ฒ ํฐ์ง๋ ์ฒ์ ์์๋ค.
def soultion(A):
min_diff = None
for i in range(len(A) - 1):
diff = abs(sum(A[:i + 1]) - sum(A[i + 1:]))
if min_diff is None:
min_diff = diff
else:
min_diff = min(min_diff, diff)
- ์ต์ด์ ์์ฑํ ์ฝ๋๋ ๋ณต์ก๋๊ฐ N^2์ด์ด์ ํ ์คํธ ์ผ์ด์ค๋ฅผ ํต๊ณผํ์ง ๋ชปํ๋ค.
์ฝ๋
def solution(A):
left = 0
right = sum(A)
min_diff = None
for i in range(1, len(A)):
left += A[i - 1]
right -= A[i - 1]
diff = abs(left - right)
if min_diff is None:
min_diff = diff
else:
min_diff = min(min_diff, diff)
return min_diff
- ๋ฆฌ์คํธ๋ฅผ ์ฌ๋ผ์ด์ฑ ํ๋ ๊ฒ์ด ์๋, ์ต์ด์ ํฉ์ ๊ตฌํด๋๊ณ left์๋ ๊ฐ์ ๋ํ๊ณ , right์๋ ๊ฐ์ ๋นผ์ฃผ์ด ๊ฐ์ฅ ์์ diff๋ฅผ ์ฐพ๋๋ก ํ๋ค.
- ์ด ๊ฒฝ์ฐ ์๊ฐ ๋ณต์ก๋๋ N์ด๋ค.
728x90
๋ฐ์ํ
'๐จโ๐ป ์ฝ๋ฉํ ์คํธ > Codility' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Lesson 4: Counter Elements โ Max Counters (0) | 2020.06.06 |
---|---|
Lesson 4: Counting Elements โ Frog River One (0) | 2020.06.06 |
Lesson 3: Time Complexity โ Perm Missing Elem (0) | 2020.06.06 |
Lesson 3: Time Complexity โ Flog Jmp (0) | 2020.06.06 |
Lesson 2: Arrays -> Odd Occurrences In Array (0) | 2020.06.06 |
๋๊ธ
๊ธ ๋ณด๊ดํจ
์ต๊ทผ์ ์ฌ๋ผ์จ ๊ธ
์ต๊ทผ์ ๋ฌ๋ฆฐ ๋๊ธ