๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
4๏ธโƒฃ Python/Problem Solving

[Python Algorithm] N๊ฐœ์˜ ์ตœ์†Œ๊ณต๋ฐฐ์ˆ˜ Programmers(Lv.2)

by seolhee2750 2022. 2. 4.
๋ฌธ์ œ

https://programmers.co.kr/learn/courses/30/lessons/12953

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - N๊ฐœ์˜ ์ตœ์†Œ๊ณต๋ฐฐ์ˆ˜

๋‘ ์ˆ˜์˜ ์ตœ์†Œ๊ณต๋ฐฐ์ˆ˜(Least Common Multiple)๋ž€ ์ž…๋ ฅ๋œ ๋‘ ์ˆ˜์˜ ๋ฐฐ์ˆ˜ ์ค‘ ๊ณตํ†ต์ด ๋˜๋Š” ๊ฐ€์žฅ ์ž‘์€ ์ˆซ์ž๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด 2์™€ 7์˜ ์ตœ์†Œ๊ณต๋ฐฐ์ˆ˜๋Š” 14๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ์ •์˜๋ฅผ ํ™•์žฅํ•ด์„œ, n๊ฐœ์˜ ์ˆ˜์˜ ์ตœ์†Œ๊ณต๋ฐฐ

programmers.co.kr

 

๋‚ด ๋ฌธ์ œ ํ’€์ด
def solution(arr):
    if len(arr) == 1:
        return arr[0]
    else:
        for i in range(len(arr) - 1):
            if arr[i] < arr[i + 1]:
                a = arr[i]
                b = arr[i + 1]
            else:
                a = arr[i + 1]
                b = arr[i]
            for j in range(b, a * b + 1):
                if j % a == 0 and j % b == 0:
                    arr[i + 1] = j
                    break

        return arr[-1]

๐Ÿ‘‰ ๊ตฌํ˜„ ๋ฌธ์ œ๋กœ, ์ฃผ์–ด์ง„ ์ž…๋ ฅ์˜ ์•ž์—์„œ๋ถ€ํ„ฐ ๋‘ ์š”์†Œ์˜ ์ตœ์†Œ๊ณต๋ฐฐ์ˆ˜๋ฅผ ๊ตฌํ•ด์ฃผ๋Š” ๊ฒƒ์„ ๋ฐ˜๋ณตํ–ˆ๋‹ค.

 

๐Ÿ’ก ํ”ผ๋“œ๋ฐฑ
  • ์ตœ์†Œ ๊ณต๋ฐฐ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ๋ฐฉ๋ฒ•๋งŒ ์•Œ๊ณ  ์žˆ์œผ๋ฉด ์‰ฌ์šด ๋ฌธ์ œ์˜€๋‹ค.

๋Œ“๊ธ€