Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

32-xxubin04 #121

Merged
merged 2 commits into from
Mar 7, 2024
Merged

32-xxubin04 #121

merged 2 commits into from
Mar 7, 2024

Conversation

xxubin04
Copy link
Member

@xxubin04 xxubin04 commented Feb 29, 2024

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

172๋ฒˆ: Factorial Trailing Zeroes


โœ”๏ธ ์†Œ์š”๋œ ์‹œ๊ฐ„

15๋ถ„


โœจ ์ˆ˜๋„ ์ฝ”๋“œ

1. ๋ฌธ์ œ ์ดํ•ด

Trailing zeroes์˜ ๊ฐœ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฌธ์ œ

์—ฌ๊ธฐ์„œ ์ฃผ์˜ํ•ด์•ผ ํ•˜๋Š” ๊ฒƒ์ด Trailing Zeroes์ด๋‹ค.
๋‚˜๋„ ๋‹จ์ˆœํžˆ ํŒฉํ† ๋ฆฌ์–ผ์˜ 0์˜ ๊ฐœ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฌธ์ œ์ธ์ค„ ์•Œ์•˜๋Š”๋ฐ ๊ณ„์† ํ‹€๋ฆฌ๊ธธ๋ž˜ Trailing Zeroes๋ฅผ ๊ฒ€์ƒ‰ํ•ด๋ณด๊ณ  ๋‚˜์„œ ์•Œ์•˜๋‹ค.

Trailing Zeroes์€ ๋’ค์— ๋ถ™์€ 0์„ ๋งํ•œ๋‹ค.
์˜ˆ์‹œ๋ฅผ ๋ณด๋ฉด ๋” ์ดํ•ดํ•˜๊ธฐ ์‰ฌ์šธ ๊ฒƒ์ด๋‹ค.

123000 -> Trailing Zeroes์˜ ๊ฐœ์ˆ˜๋Š” 3
120030 -> Trailing Zeores์˜ ๊ฐœ์ˆ˜๋Š” 1
102003 -> Trailing Zeroes์˜ ๊ฐœ์ˆ˜๋Š” 0


2. ์ฝ”๋“œ ๋ถ„์„ + ์ „์ฒด ์ฝ”๋“œ

Trailing Zeroes๊ฐ€ ๋ญ”์ง€ ์•Œ๊ณ ์žˆ๋‹ค๋ฉด ์ฝ”๋“œ๋กœ ๊ตฌํ˜„ํ•˜๋Š” ๊ฒƒ์€ ๋งค์šฐ ์‰ฌ์šธ ๊ฒƒ์ด๋‹ค.
์ƒ๊ฐ๋ณด๋‹ค ์‰ฝ๊ฒŒ ํ’€๋ ค PR ์˜ฌ๋ ค๋„ ๋˜๋‚˜ ๊ณ ๋ฏผํ•˜๊ธด ํ–ˆ์Šต๋‹ˆ๋‹ค....

class Solution(object):
    def trailingZeroes(self, n):
        total = 1
        while n >= 2:
            total *= n
            n -= 1
        cnt = 0
        for i in reversed(list(str(total))):
            if i == '0':
                cnt += 1
            else:
                break
        return cnt

ํŒฉํ† ๋ฆฌ์–ผ n! ์—์„œ์˜ n์€ ์ฃผ์–ด์ง€๋ฏ€๋กœ while n >= 2๋กœ ํŒฉํ† ๋ฆฌ์–ผ n!์„ ๊ตฌํ•˜์—ฌ total์— ์ €์žฅํ•œ๋‹ค.

total์˜ ๊ฐ ์ž๋ฆฟ์ˆ˜๋ฅผ ๋ฆฌ์ŠคํŠธํ™”ํ•˜๊ณ  ์—ญ์ˆœ์œผ๋กœ ์ •๋ ฌํ•ด์ค€๋‹ค.
-> ์šฐ๋ฆฌ๊ฐ€ ๊ถ๊ธˆํ•œ ๊ฒƒ์€ ํŒฉํ† ๋ฆฌ์–ผ n!์˜ ๊ผฌ๋ฆฌ๋ถ€๋ถ„์˜ 0 ๊ฐœ์ˆ˜๊ฐ€ ์ค‘์š”ํ•˜๋ฏ€๋กœ!

for๋ฌธ์œผ๋กœ '0'์ธ์ง€ ํ™•์ธํ•ด์ฃผ๊ณ  '0'์ด ์•„๋‹Œ ๊ฒฝ์šฐ๊ฐ€ ๋‚˜์˜ค๋ฉด ๋ฐ”๋กœ breakํ•ด์ฃผ๊ณ  0์˜ ๊ฐœ์ˆ˜์ธ cnt๋ฅผ ๋ฐ˜ํ™˜ํ•ด์ค€๋‹ค.


๐Ÿ“š ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ๋œ ๋‚ด์šฉ

image

12์‹œ๊ฐ€ ๋„˜์–ด์„œ ๋ฌธ์ œ๋ฅผ ํ’€๊ฒŒ ๋˜์–ด์„œ ์ด ๋ฌธ์ œ๋กœ PR ์˜ฌ๋ฆฝ๋‹ˆ๋‹ค..๐Ÿฅฒ
์ข€ ๋” ํšจ์œจ์ ์œผ๋กœ ํ’€ ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ๋‚˜ ๊ถ๊ธˆํ•˜๋„ค์š”.

+์ถ”๊ฐ€

class Solution(object):
    def trailingZeroes(self, n):
        total = 1
        while n >= 2:
            total *= n
            n -= 1
        for i in range(1, len(str(total))+1):
            if total%(10**i) == 0:
                continue
            else:
                return i-1

์ด ์ฝ”๋“œ๋Š” ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋œจ๋„ค์š”๐Ÿค”

Copy link
Collaborator

@9kyo-hwang 9kyo-hwang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

๊ฑฐ์˜ ์˜จ๋ชธ ๋น„ํ‹€๊ธฐํ•ด์„œ ์–ด๋–ป๊ฒŒ๋“  ํ†ต๊ณผ์‹œํ‚จ ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค ใ…‹ใ…‹
๊ทผ๋ฐ $O(log N)$ ์‹œ๊ฐ„๋ณต์žก๋„์— ํ’€๋ฆฐ๋‹ค๊ธธ๋ž˜ ๋ญ์ง€...? ํ•˜๊ณ  ์ฐพ์•„๋ณด๋‹ˆ๊นŒ...

๊ผฌ๋ฆฌ์— ๋ถ™์€ 0
image

ํ—ˆํ—ˆ... ์‹ ๊ธฐํ•˜๋„ค์š”.

์˜์–ด ๋Œ“๊ธ€์„ ๋ณด๋‹ˆ๊นŒ
"๊ฒฐ๊ตญ ๋์— 0์ด ๋ช‡ ๊ฐœ ๋ถ™์—ˆ๋Š” ์ง€ ์ฐพ๋Š” ๊ฑด '10์œผ๋กœ ๋‚˜๋ˆ„์–ด์ง€๋Š” ํšŸ์ˆ˜'๋ฅผ ๊ตฌํ•˜๋Š” ๊ฑฐ๋ž‘ ๊ฐ™๋‹ค. 10์€ 2์™€ 5๋ฅผ ์•ฝ์ˆ˜๋กœ ๊ฐ€์ง€๋ฏ€๋กœ ๋‘˜ ์ค‘ ๋” ์ ์€ ๊ฐœ์ˆ˜๋ฅผ ๊ฐ–๋Š” '5'์˜ ๊ฐœ์ˆ˜๋ฅผ ์นด์šดํŒ…ํ•˜๋ฉด ๋œ๋‹ค." ํ•˜๋ฉด์„œ ์„ค๋ช…์ด ์ฃผ๋ฅด๋ฅต ์žˆ๋Š”๋ฐ ์ œ๋Œ€๋กœ ์ดํ•ด๊ฐ€ ๋˜์ง€๋Š” ์•Š๋„ค์š” ํ—ˆํ—ˆ...

Copy link
Collaborator

@Dolchae Dolchae left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์–ด์—„์ฒญ ๊ฐ„๋‹จํ•˜๊ฒŒ ์ž˜ ๊ตฌํ˜„ํ•˜์…จ๋„ค์š”!! ์ˆ˜๊ณ ํ•˜์…จ์Šต๋‹ˆ๋‹ค๐Ÿ‘๐Ÿ˜Š

Copy link
Member

@gjsk132 gjsk132 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๋ฌธ์ œ๊ฐ€ ๊ทธ๋ƒฅ 0์˜ ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ์ค„ ์•Œ๊ณ  ํ—ค๋งธ๋Š”๋ฐ, ์•Œ๊ณ ๋ณด๋‹ˆ ๋’ค์— ๋ถ™๋Š” ์ˆ˜์˜€๋„ค์š”...ใ…œใ… 

ํ•˜์ง€๋งŒ ๋’ค์— ๋ถ™๋Š” 0์˜ ์ˆ˜๊ฐ€ ์˜คํžˆ๋ ค ๊ตฌํ•˜๊ธฐ ์‰ฝ์Šต๋‹ˆ๋‹ค!
์ด๋ฏธ ๊ตํ™ฉ์ฟ ์„ ๋ฐฐ๊ฐ€ ์–ธ๊ธ‰ํ•œ ์›๋ฆฌ์ธ๋ฐ... ์ž์„ธํžˆ ์„ค๋ช…๋“œ๋ฆฌ๋ฉด

n! ์€ 1๋ถ€ํ„ฐ n๊นŒ์ง€์˜ ์ˆ˜๋ฅผ ๊ณฑํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค.
๊ทธ๋ฆฌ๊ณ  ์ €ํฌ๊ฐ€ ๊ตฌํ•ด์•ผํ•˜๋Š” ๊ฐ’์€ ๋’ค์— ๋ถ™๋Š” 0์˜ ์ˆ˜์ด๋ฏ€๋กœ 10์ด ๋ช‡ ๋ฒˆ ๊ณฑํ•ด์กŒ๋Š”์ง€๋ฅผ ์•Œ์•„์•ผํ•ฉ๋‹ˆ๋‹ค.
10์˜ ์ธ์ˆ˜๋Š” 2์™€ 5๋กœ 2๊ฐœ๊ฐ€ ๊ณฑํ•ด์ง€๋Š” ์ˆœ๊ฐ„ 0์ด ๋’ค์— ๋งŒ๋“ค์–ด์ง„๋‹ค๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.
์—ฌ๊ธฐ์„œ factorial์€ 1๋ถ€ํ„ฐ ๊ณ„์†ํ•ด์„œ ๊ณฑํ•ด์ง€๊ธฐ ๋•Œ๋ฌธ์— 5์ด์ „์— 2๋Š” ์ตœ์†Œ 1๋ฒˆ ์ด์ƒ ๊ณฑํ•ด์งˆ ์ˆ˜ ๋ฐ–์— ์—†์Šต๋‹ˆ๋‹ค.

์œ„์— ๋‚ด์šฉ์„ ์ž˜ ์ •๋ฆฌํ•ด๋ณด๋ฉด
๊ฒฐ๊ตญ์—” 5๊ฐ€ ๊ณฑํ•ด์ง€๋Š” ํšŸ์ˆ˜๋งŒ ๊ตฌํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.
( 25์˜ ๊ฒฝ์šฐ 5๊ฐ€ 2๋ฒˆ ๋‚˜์™€์„œ 2๊ฐ€ ๋”ํ•ด์ ธ์•ผํ•œ๋‹ค. )

๋”ฐ๋ผ์„œ n = 30์ด๋ผ๊ณ  ์ •์˜ํ•  ๋•Œ,
5์˜ ๋ฐฐ์ˆ˜๋Š” 30//5 = 6
25์˜ ๋ฐฐ์ˆ˜๋Š” 30//25 = 1
6 + 1 = 7 ์ด๋ผ๊ณ  ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค!

class Solution:
    def trailingZeroes(self, n: int) -> int:
        cnt_five = 0

        squared_5 = 5

        while squared_5 <= n:
            cnt_five += n//squared_5
            squared_5 *= 5

        return cnt_five

@xxubin04 xxubin04 merged commit 00a2974 into AlgoLeadMe:main Mar 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants