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

51-xxubin04 #194

Merged
merged 2 commits into from
Sep 5, 2024
Merged

51-xxubin04 #194

merged 2 commits into from
Sep 5, 2024

Conversation

xxubin04
Copy link
Member

@xxubin04 xxubin04 commented Jul 1, 2024

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

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค - ์ ์ฐ๊ธฐ


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

20๋ถ„

์ƒ๊ฐ๋ณด๋‹ค ๋„ˆ๋ฌด ์‰ฝ๊ฒŒ ํ’€์–ด์„œ ๊ฐ„๋‹จํ•˜๊ฒŒ ์˜ฌ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค..!


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

์ฒ˜์Œ์—๋Š” ๋‹จ์ˆœํ•˜๊ฒŒ ์›์ ๊ณผ ํ•œ ์  ์‚ฌ์ด์˜ ๊ฑฐ๋ฆฌ์˜ ๊ณต์‹์ธ (a^2 + d^2)^0.5๋ฅผ ์‚ฌ์šฉํ•ด์„œ a=0๋ถ€ํ„ฐ a=d/k๊นŒ์ง€ ํ™•์ธํ•ด์ฃผ๋Š” ๊ณผ์ •์„ ๊ฑฐ์ณค๋Š”๋ฐ ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋–ด๋‹ค...

def solution(k, d):
    answer = 0
    a = 0
    for i in range(int(d/k)+1):
        answer += (int((d**2 - (a*k)**2)**0.5/k) + 1)
        a += 1
    return answer

0๋ถ€ํ„ฐ int(d/k)๊นŒ์ง€์˜ a๋ฅผ for๋ฌธ์œผ๋กœ ์ˆœํšŒํ•œ๋‹ค. (int(d/k) = ๊ฑฐ๋ฆฌ d๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ตœ๋Œ€ ak๋ฅผ ๋งŒ์กฑํ•˜๋Š” a)
(int((d**2 - (a*k)**2)**0.5/k) + 1)๋Š” ๊ฑฐ๋ฆฌ d๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ตœ๋Œ€ b
k๋ฅผ ๋งŒ์กฑํ•˜๋Š” b์— 1์„ ๋”ํ•œ ์‹์ด๋‹ค. 1์€ b์˜ ์ขŒํ‘œ 0์„ ์œ„ํ•ด ์ถ”๊ฐ€ํ•ด์ค€ ๊ฒƒ์ด๋‹ค.

์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š”์ง€ ๋ชจ๋“  ์ ์˜ ์ขŒํ‘œ์—์„œ์˜ ๊ฑฐ๋ฆฌ๋ฅผ ํ™•์ธํ•ด์ฃผ๋Š” ๊ฒƒ์ด ์•„๋‹Œ, ๊ฑฐ๋ฆฌ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ์ตœ๋Œ€ ์  ์ขŒํ‘œ๋ฅผ ๊ฐœ์ˆ˜๋กœ์„œ ๋”ํ•ด์ฃผ๋ฉด ์‹œ๊ฐ„์ดˆ๊ณผ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค..!


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

์‹ ์กฐ๊ฑด๋งŒ ์ž˜ ์„ธ์›Œ์ฃผ๋ฉด ์‰ฝ๊ฒŒ ํ’€ ์ˆ˜ ์žˆ์–ด์„œ ์˜ฌ๋ ค๋„ ๋˜๋Š”์ง€ ๊ณ ๋ฏผํ•˜๊ธด ํ–ˆ์ง€๋งŒ ์˜ฌ๋ฆฝ๋‹ˆ๋‹ค~!

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.

์ฒจ์— (int((d**2 - (a*k)**2)**0.5/k) + 1) ์ด๊ฒŒ ๋Œ€์ฒด ๋ฌด์Šจ ์‹์ธ๊ฐ€... ํ–ˆ๋Š”๋ฐ
d = sqrt((a*k)**2 + (b*k)**2)์—์„œ b๋งŒ ๋‚จ๊ธฐ๋„๋ก ๋ณ€ํ˜•ํ•œ ์‹์ด์—ˆ๊ตฐ์š”... ํ—ˆํ—ˆ
๋‚ ์ด ๊ฐˆ ์ˆ˜๋ก ์ˆ˜ํ•™ ๋จธ๋ฆฌ๊ฐ€ ํ‡ดํ™”ํ•˜๋Š” ๊ฒƒ ๊ฐ™์•„์š” ๐Ÿฅน

์ €๋„ ํฐ ์ฐจ์ด๋Š” ์—†๊ณ , ๋ฐ˜๋ณต๋ฌธ ๋ณ€์ˆ˜์˜ step ์ •๋„๋งŒ ๋ฐ”๊ฟจ๋„ค์šฉ

def solution(k, d):
    answer = 0
    th = d**2
    
    for a in range(0, d + 1, k):
        b = int((th - a**2)**0.5 / k + 1)
        answer += b
    
    return answer

@@ -0,0 +1,7 @@
def solution(k, d):
answer = 0
a = 0
Copy link
Collaborator

Choose a reason for hiding this comment

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

์ž˜ ์ƒ๊ฐํ•ด๋ณด๋ฉด, for๋ฌธ์˜ i ๋ณ€์ˆ˜๊ฐ€ a์™€ ๋™์ผํ•œ ์—ญํ• ์„ ํ•œ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค :)

answer = 0
a = 0
for i in range(int(d/k)+1):
answer += (int((d**2 - (a*k)**2)**0.5/k) + 1)
Copy link
Collaborator

Choose a reason for hiding this comment

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

d**2 ์—ฐ์‚ฐ ๊ฒฐ๊ณผ๋Š” ๋ฐ˜๋ณต๋ฌธ ๋‚ด์—์„œ ๋ถˆ๋ณ€์ด์ฃ ? ๊ทธ๋ ‡๋‹ค๋ฉด ๋ฐ˜๋ณต๋ฌธ ๋ฐ–์—์„œ ๋ฏธ๋ฆฌ ๊ณ„์‚ฐํ•ด๋‘๋Š” ๊ฑธ๋กœ ์ตœ์ ํ™” ํ•ด๋ด…์‹œ๋‹ค :)

threshold = d ** 2
for a in range(int(d/k) + 1):
    answer += (int((threshold - (a*k)**2)**0.5 / k) + 1)

Copy link
Collaborator

@mjj111 mjj111 left a comment

Choose a reason for hiding this comment

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

์ €๋Š”์ฃผ์–ด์ง„ d ๋ฒ”์œ„ ์—์„œ ์ ‘๊ทผ๊ฐ€๋Šฅํ•œ x์˜ ์ตœ๋Œ€๊ฐ’์„ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด dSquared - x^2๋ฅผ ๊ณ„์‚ฐํ•ด์„œ ๊ฐ€๋Šฅํ•œ ๋ฒ”์œ„๋ฅผ ๊ตฌํ–ˆ์–ด์š”

๊ทธ๋ฆฌ๊ณ  y ์ตœ๋Œ€๊ฐ’์„ k ๊ฐ„๊ฒฉ์œผ๋กœ ๋‚˜๋ˆ„์–ด ๊ฐ€๋Šฅํ•œ x์˜ ๊ฐœ์ˆ˜๋ฅผ ์„ธ๊ณ , ์ด๋ฅผ count์— ๋”ํ–ˆ์Šต๋‹ˆ๋‹ค!

class Solution {
    public static long solution(int k, int d) {
        long count = 0;
        long dSquared = (long) d * d;

        for (int x = 0; x <= d; x += k) {
            long maxXSquared = dSquared - (long) x * x;
            int maxX = (int) Math.sqrt(maxXSquared);
            count += (maxX / k) + 1;
        }

        return count;
    }
}

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.

import math

def solution(k, d):
    
    answer = 0
    
    for i in range(0, d+1, k):
        length = int(math.sqrt(pow(d, 2)-pow(i, 2)))
        answer += (length//k) + 1
    
    return answer

๊ฑฐ์˜ ๋™์ผํ•˜์ง€๋งŒ... ์ €๋Š” math ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์— sqrt(๋ฃจํŠธ)ํ•จ์ˆ˜๋ฅผ ๊ฐ€์ ธ์™€์„œ ์ผ์Šต๋‹ˆ๋‹ค!

answer = 0
a = 0
for i in range(int(d/k)+1):
answer += (int((d**2 - (a*k)**2)**0.5/k) + 1)
Copy link
Member

Choose a reason for hiding this comment

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

๋ฃจํŠธ๋Š” 0.5 ์ œ๊ณฑํ•˜๋ฉด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์—ˆ๋„ค์š”..!

@9kyo-hwang 9kyo-hwang merged commit b7449f6 into AlgoLeadMe:main Sep 5, 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