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

13 mong3125 #44

Merged
merged 2 commits into from
May 2, 2024
Merged

13 mong3125 #44

merged 2 commits into from
May 2, 2024

Conversation

mong3125
Copy link
Collaborator

@mong3125 mong3125 commented Apr 1, 2024

πŸ”— 문제 링크

K번째 수

βœ”οΈ μ†Œμš”λœ μ‹œκ°„

머리가 잘 μ•ˆλŒμ•„κ°‘λ‹ˆλ‹€.. λ‹€μ‹œ 봐야할 것 κ°™λ„€μš”.

3μ‹œκ°„ λ„˜κ²Œ κ±Έλ ΈμŠ΅λ‹ˆλ‹€.

✨ μ½”λ“œ

import java.util.Scanner;

public class Main {
    static int n;
    static int k;
    static int answer;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        k = sc.nextInt();

        index(0, k);
        System.out.println(answer);
    }

    public static void index(int front, int back) {
        if (front > back) return;

        int mid = (front + back) / 2;
        
        int sum = 0;
        for (int i = 1; i <= n; i++) {
            sum += Math.min(mid / i, n);
        }

        if (sum < k) {
            index(mid + 1, back);
        } else {
            answer = mid;
            index(front, mid - 1);
        }
    }
}

❓ μ½”λ“œ μ„€λͺ…

μ–΄λ–»κ²Œ? β†’ https://st-lab.tistory.com/281

μœ„ λ°©λ²•μœΌλ‘œ 이뢄 탐색을 κ΅¬ν˜„ν•΄λ΄€μŠ΅λ‹ˆλ‹€. μ„€λͺ…이 κΈΈμ–΄μ„œ μ œκ°€ 아이디어λ₯Ό 얻은 μ‚¬μ΄νŠΈλ‘œ λŒ€μ²΄ν•˜κ³ μž ν•©λ‹ˆλ‹€β€¦ μ „ 머리가 λ„ˆλ¬΄ μ•ˆκ΅΄λŸ¬κ°€μ„œ λ΄λ²„λ ΈμŠ΅λ‹ˆλ‹€. λ‚΄κ»„λ‘œ λ§Œλ“€μ–΄μ•Όμ£ .

이 λ¬Έμ œλŠ” 이뢄탐색 λ¬Έμ œλΌλŠ” 것을 μ•ŒκΈ° μ–΄λ €μš΄ λ¬Έμ œμ˜€μŠ΅λ‹ˆλ‹€.
λŒ€μ²΄ μ–΄λ””κ°€ 이뢄탐색 λ¬Έμ œμΈμ§€ νžŒνŠΈκ°€ μ „ν˜€ μ—†κΈ°λ•Œλ¬Έμ΄μ£ .

해닡은 K번째 μˆ˜κ°€ 무엇인지λ₯Ό μ°ΎλŠ” 과정을 μ΄λΆ„νƒμƒ‰μœΌλ‘œ μ°ΎμŠ΅λ‹ˆλ‹€.

K번째 μˆ˜λŠ” K보닀 μž‘κ±°λ‚˜ 같을 κ±°κ±°λ“ μš”. 첫번째 λ°°μ—΄λ§ŒμœΌλ‘œ K번째 μˆ˜λŠ” μ΅œλŒ€ Kκ°€ λ©λ‹ˆλ‹€.

이제 μ–΄λ–€ 수 N에 λŒ€ν•΄μ„œ λ§žλŠ”μ§€ μ—¬λΆ€λ₯Ό μ²΄ν¬ν•˜λŠ” 방법은 N보닀 μž‘μ€ 수λ₯Ό μ„Έμ•„λ¦¬λŠ” λ°©λ²•μž…λ‹ˆλ‹€. μžμ„Έν•œ 방법은 μœ„μ˜ μ‚¬μ΄νŠΈλ₯Ό μ°Έκ³ ν•΄μ£Όμ„Έμš”. μ„€λͺ…ν•˜λ €λ©΄ μ’€ 마~~μ•Šμ΄ κΈΈμ–΄μš”.

// μ‹œμž‘ λ²”μœ„μ™€ 끝 λ²”μœ„ μ‚¬μ΄λ‘œ μ°ΎλŠ” μˆ«μžκ°€ μžˆλ‹€κ³  μƒκ°ν•©λ‹ˆλ‹€.
index(μ‹œμž‘ λ²”μœ„, 끝 λ²”μœ„) {
    μ‹œμž‘ λ²”μœ„κ°€ 끝 λ²”μœ„λ³΄λ‹€ 크닀면 μ€‘λ‹¨ν•©λ‹ˆλ‹€.
    
    쀑간값을 κ΅¬ν•©λ‹ˆλ‹€.
    
    배열에 ν¬ν•¨λœ 쀑간값보닀 μž‘μ€ μˆ˜λ“€μ˜ 개수λ₯Ό κ΅¬ν•΄μ€λ‹ˆλ‹€.
    
    μž‘μ€ μˆ˜λ“€μ˜ κ°œμˆ˜κ°€ k보닀 μž‘λ‹€λ©΄ index(쀑간값 + 1, 끝)
    μž‘μ€ μˆ˜λ“€μ˜ κ°œμˆ˜κ°€ k이상이라면 answer = 쀑간값, index(μ‹œμž‘, 쀑간값 - 1)
}

μž‘μ€ μˆ˜λ“€μ˜ κ°œμˆ˜κ°€ μ •ν™•νžˆ kκ°€ μ•ˆλ  μˆ˜λ„ μžˆμ–΄μ„œ μœ„μ²˜λŸΌ ν•œμͺ½μ— answerλ₯Ό 두고 계속 λ°˜λ³΅ν•΄μ•Όν•©λ‹ˆλ‹€.

πŸ“š μƒˆλ‘­κ²Œ μ•Œκ²Œλœ λ‚΄μš©

  • 이뢄 νƒμƒ‰μœΌλ‘œ ν’€ 수 μžˆλŠ” 문제λ₯Ό μƒˆλ‘­κ²Œ μ•Œκ²Œ λ˜μ—ˆμŠ΅λ‹ˆλ‹€.

Copy link
Collaborator

@YIM2UL2ET YIM2UL2ET left a comment

Choose a reason for hiding this comment

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

이뢄 탐색을 μ‚¬μš©ν•˜λŠ” 것을 λΉ λ₯΄κ²Œ μ•Œ 수만 μžˆλ‹€λ©΄ μ‰¬μš΄ λ¬Έμ œλ„€μš”.
μ§„μ§œ 이뢄 탐색은 μ–Έμ œ μ μž¬μ μ†Œμ— μ‚¬μš©ν•΄μ•Ό ν•  지 μ•ŒκΈ° νž˜λ“  μ•Œκ³ λ¦¬μ¦˜ 쀑에 ν•˜λ‚˜λΌκ³  μƒκ°ν•©λ‹ˆλ‹€.. μˆ˜κ³ ν•˜μ…¨μŠ΅λ‹ˆλ‹€.

@mong3125
Copy link
Collaborator Author

이뢄 탐색을 μ‚¬μš©ν•˜λŠ” 것을 λΉ λ₯΄κ²Œ μ•Œ 수만 μžˆλ‹€λ©΄ μ‰¬μš΄ λ¬Έμ œλ„€μš”. μ§„μ§œ 이뢄 탐색은 μ–Έμ œ μ μž¬μ μ†Œμ— μ‚¬μš©ν•΄μ•Ό ν•  지 μ•ŒκΈ° νž˜λ“  μ•Œκ³ λ¦¬μ¦˜ 쀑에 ν•˜λ‚˜λΌκ³  μƒκ°ν•©λ‹ˆλ‹€.. μˆ˜κ³ ν•˜μ…¨μŠ΅λ‹ˆλ‹€.

이 λ¬Έμ œλŠ” μ§„μ§œ 이뢄탐색인걸 μ•ŒκΈ° νž˜λ“€λ”λΌκ³ μš”.. 심지어 μ €λŠ” 이뢄탐색 λ¬Έμ œμž„μ„ μ•Œκ³  ν’€μ—ˆλŠ”λ°λ„ κ·Έλ ‡μŠ΅λ‹ˆλ‹€. ν•˜ν•˜..;

Copy link
Collaborator

@rivkms rivkms left a comment

Choose a reason for hiding this comment

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

문제λ₯Ό λ³΄λ©΄μ„œ 이게 μ™œ 이진탐색...?μ΄λΌλŠ” 생각을 ν•˜μ˜€μ§€λ§Œ 첨뢀해주신 μ„€λͺ…을 λ³΄λ©΄μ„œ μ™œ 이진탐색인지, κ·Έ μ‚¬κ³ μ˜ 흐름에 λŒ€ν•΄ μ•Œ 수 μžˆμ—ˆμŠ΅λ‹ˆλ‹€.
μ–΄λ €μš΄ 문제인 것 κ°™κ³ , 또 μ΄λŸ¬ν•œ λ¬Έμ œμ—μ„œλ„ 이진탐색을 μ‚¬μš©ν•  수 μžˆλ‹€λŠ” 점을 μ•Œμ•˜κΈ°μ— μ‹ κΈ°ν•œ λ¬Έμ œμ˜€λ˜ 것 κ°™μŠ΅λ‹ˆλ‹€. 이진탐색이 κ°„λ‹¨ν•˜μ§€λ§Œ, 이λ₯Ό μ–΄λ–»κ²Œ μ‘μš©ν•˜λŠ”κ°€μ— 따라 정말 λ‹€μ–‘ν•œ λΆ„μ•Όμ—μ„œ μ‚¬μš© κ°€λŠ₯ν•˜λ„€μš”...

μˆ˜κ³ ν•˜μ…¨μŠ΅λ‹ˆλ‹€ 😁

@mong3125 mong3125 merged commit 3c921b6 into main May 2, 2024
1 check passed
@mong3125 mong3125 deleted the 13-mong3125 branch May 2, 2024 15:17
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.

3 participants