[ํ๋ก๊ทธ๋๋จธ์ค] ๐จโ๐ป ๊ธฐ๋ฅ ๊ฐ๋ฐ / python
๐จโ๐ป ๊ธฐ๋ฅ ๊ฐ๋ฐ ๋ฌธ์ ํ์ด๋ณด๊ธฐ
๐ ๋์ ์ฝ๋
import math
def solution(progresses, speeds):
answer = []
progresses = [math.ceil((100-a)/b) for a, b in zip(progresses, speeds)]
front = 0
for idx in range(len(progresses)):
if progresses[front] < progresses[idx]:
answer.append(idx-front)
front = idx
answer.append(len(progresses)-front)
return answer
์คํstack/ํqueue ์๋ฃ๊ตฌ์กฐ์ ์ํ ๋ฌธ์ ์
๋๋ค. ์ฐ์ progresses
์ speeds
๋ก ๋๋์ด์ง ๋ list
๋ฅผ ํ๋์ list
๋ก ๋ง๋ค์ด๋ด
์๋ค. ํด๋น list
์๋ ๊ฐ ์์
๋ณ ์๋ฃ๋๊ธฐ๊น์ง(>100%) ๊ฑธ๋ฆฌ๋ ์๊ฐ์ ๋ํ ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํ๊ฒ ๋ฉ๋๋ค. ๊ทธ์ ๋ํ ์ฝ๋๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
progresses = [math.ceil((100-a)/b) for a, b in zip(progresses, speeds)]
๋ค์์ผ๋ก ํด๋น list
๋ฅผ ํ์ธํ๋ฉด์ ๊ฒฐ๊ณผ๊ฐ์ ์ฐพ์์ผํฉ๋๋ค. ์ฝ๊ฒ ์๊ฐํ๋ฉด ์ ์ฒด for loop
๋ฅผ ๋๋ฉด์ ๋งจ ์์ ๋ฐ์ดํฐ๋ณด๋ค ์์ ์ง ํ์ธํ๋ for loop
๋ฅผ ๋ค์ ์จ์ ํ ์ ์์ต๋๋ค. ํ์ง๋ง ์ด๋ ๊ฒ ํ๋ฉด ์๊ฐ ๋ณต์ก๋๊ฐ O(n^2)
๊ฐ ๋์ด ํน์ ํ
์คํธ ์ผ์ด์ค์์ ์๊ฐ ์ด๊ณผ๋ฅผ ๋ฐ๊ฒ ๋ฉ๋๋ค. ๋ฐ๋ผ์ ๊ทธ ๋ณด๋ค ๋ฎ์ ์๊ฐ ๋ณต์ก๋๋ฅผ ๊ฐ์ง๋ ๋ฐฉ์์ผ๋ก ์ฝ๋๋ฅผ ์ง์ผํฉ๋๋ค.
for idx in range(len(progresses)):
if progresses[front] < progresses[idx]:
answer.append(idx-front)
front = idx
answer.append(len(progresses)-front)
์ ๋ ๋จ ํ ๋ฒ๋ง ๋ฐฐ์ด์ ๋๊ฒํ์ฌ ์ด ์์ ๋๋ด๋๋ก ์ฝ๋๋ฅผ ์งฐ์ต๋๋ค. ๋์ front ๊ฐ์ ๊ณ์ ์
๋ฐ์ดํธ ํ๋ฉด์ ๋งจ ์์ ์๋ ์์
์ด ๋ฌด์์ธ์ง ํ์ธ ํ ์ ์๊ฒํ์ต๋๋ค. ์ด๋ ๊ฒ ์ง๋ฉด ์๊ฐ ๋ณต์ก๋๊ฐ O(n)
์ด ๋์ด ์๊ฐ ์ด๊ณผ๋ฅผ ๋ฐ์ง ์๊ณ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ ์ ์์ต๋๋ค.
Written with StackEdit.
'Algorithm > Problem Solving' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ํ๋ก๊ทธ๋๋จธ์ค] ๐ข ํฐ ์ ๋ง๋ค๊ธฐ / python (0) | 2019.05.08 |
---|---|
[ํ๋ก๊ทธ๋๋จธ์ค] ๐จ ํ๋ฆฐํฐ ๋ฌธ์ / python (0) | 2019.02.18 |
[ํ๋ก๊ทธ๋๋จธ์ค] ๐จโ๐ป ๊ธฐ๋ฅ ๊ฐ๋ฐ / python (2) | 2019.02.18 |
[ํ๋ก๊ทธ๋๋จธ์ค] ๐ทโโ๏ธ ํ์ผ ์ฅ์๋ฌผ / python (0) | 2019.02.18 |
[ํ๋ก๊ทธ๋๋จธ์ค] ๐จโ๐ซ ์ ์ ์ผ๊ฐํ / python (0) | 2019.02.18 |
[ํ๋ก๊ทธ๋๋จธ์ค] โพ ์ซ์ ์ผ๊ตฌ / python (4) | 2019.02.15 |
๋๊ธ
์ด ๊ธ ๊ณต์ ํ๊ธฐ
-
๊ตฌ๋
ํ๊ธฐ
๊ตฌ๋ ํ๊ธฐ
-
์นด์นด์คํก
์นด์นด์คํก
-
๋ผ์ธ
๋ผ์ธ
-
ํธ์ํฐ
ํธ์ํฐ
-
Facebook
Facebook
-
์นด์นด์ค์คํ ๋ฆฌ
์นด์นด์ค์คํ ๋ฆฌ
-
๋ฐด๋
๋ฐด๋
-
๋ค์ด๋ฒ ๋ธ๋ก๊ทธ
๋ค์ด๋ฒ ๋ธ๋ก๊ทธ
-
Pocket
Pocket
-
Evernote
Evernote
๋ค๋ฅธ ๊ธ
-
[ํ๋ก๊ทธ๋๋จธ์ค] ๐ข ํฐ ์ ๋ง๋ค๊ธฐ / python
[ํ๋ก๊ทธ๋๋จธ์ค] ๐ข ํฐ ์ ๋ง๋ค๊ธฐ / python
2019.05.08 -
[ํ๋ก๊ทธ๋๋จธ์ค] ๐จ ํ๋ฆฐํฐ ๋ฌธ์ / python
[ํ๋ก๊ทธ๋๋จธ์ค] ๐จ ํ๋ฆฐํฐ ๋ฌธ์ / python
2019.02.18 -
[ํ๋ก๊ทธ๋๋จธ์ค] ๐ทโโ๏ธ ํ์ผ ์ฅ์๋ฌผ / python
[ํ๋ก๊ทธ๋๋จธ์ค] ๐ทโโ๏ธ ํ์ผ ์ฅ์๋ฌผ / python
2019.02.18 -
[ํ๋ก๊ทธ๋๋จธ์ค] ๐จโ๐ซ ์ ์ ์ผ๊ฐํ / python
[ํ๋ก๊ทธ๋๋จธ์ค] ๐จโ๐ซ ์ ์ ์ผ๊ฐํ / python
2019.02.18
front๋ฅผ ์จ์ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ์ ๋ฐฐ์ ์ต๋๋ค. ๊ฐ์ฌํด์!