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

43-tgyuuAn #157

Merged
merged 2 commits into from
Mar 24, 2024
Merged

43-tgyuuAn #157

merged 2 commits into from
Mar 24, 2024

Conversation

tgyuuAn
Copy link
Member

@tgyuuAn tgyuuAn commented Mar 10, 2024

πŸ”— 문제 링크

징검닀리 κ±΄λ„ˆκΈ°

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

30λΆ„

✨ μˆ˜λ„ μ½”λ“œ

μ™„νƒμœΌλ‘œ ν’€κ²Œ 될 경우, μ΅œλŒ€ $200,000,000 X 200,000$ μ΄λ―€λ‘œ μ ˆλŒ€ λΆˆκ°€λŠ₯ν•˜λ‹€.


파이썬으둜 ν’€ 경우, νŒŒμ΄μ¬μ€ 1μ΄ˆμ— 2천만번의 연산이 κ°€λŠ₯ν•œλ°,

ν”„λ‘œκ·Έλž˜λ¨ΈμŠ€λŠ” λŒ€λΆ€λΆ„ 5μ΄ˆκ°€ μ£Όμ–΄μ Έμ„œ 1μ–΅λ²ˆ κΉŒμ§€ 연산이 κ°€λŠ₯ν•˜λ‹€.







μ΄λ ‡κ²Œ ν„°λ¬΄λ‹ˆ 없이 높은 μ œν•œμ„ 보고 λ°”λ‘œ 이뢄 νƒμƒ‰μ˜ λƒ„μƒˆλ₯Ό λ§‘μ•˜κ³ ,

이뢄 νƒμƒ‰μ˜ midκ°’μœΌλ‘œ μ–΄λ–€ 것을 μ„ μ •ν•˜λ©΄ 쒋을지 곰곰히 μƒκ°ν–ˆλ‹€.







λ¬Έμ œκ°€ 이뢄 νƒμƒ‰μž„μ„ νŒŒμ•…ν–ˆλ”λΌλ©΄, 이뢄 탐색 ν˜Ήμ€ νŒŒλΌλ©”νŠΈλ¦­ μ„œμΉ˜μ—μ„œ κ°€μž₯ μ€‘μš”ν•œ μš”μ†ŒλŠ”

"κ³Όμ—° 주어진 λ¬Έμ œμ—μ„œ μ–΄λ–€ 것을 "예, μ•„λ‹ˆμ˜€" λΌλŠ” λ‹΅λ³€μœΌλ‘œ κ°ˆλΌλ‚Όκ±°λƒλŠ” 것" 이닀.







λ‚˜λŠ” 이 λ¬Έμ œμ—μ„œ 이뢄 νƒμƒ‰μ˜ midκ°’μœΌλ‘œ μ§€λ‚˜κ°ˆ 수 μžˆλŠ” μ΅œλŒ€ μ‚¬λžŒ 수둜 μ„ μ •ν–ˆλ‹€.




즉, λ§Œμ•½ midκ°€ 5이며 stones μ—μ„œ -5λ₯Ό λΊ€ κ°’μœΌλ‘œ μˆœνšŒν•˜λ©΄μ„œ 0 μ΄ν•˜κ°€ μ•„λ‹Œ κ°’μ˜ 간격이 k λ₯Ό λ„˜λŠλƒ μ•ˆ λ„˜λŠλƒλ‘œ νŒλ‹¨ν•  수 μžˆλ‹€.

λ§Œμ•½ 이 check() ν•¨μˆ˜μ—μ„œ λ°˜λ³΅λ¬Έμ„ λ„λŠ” 쀑간에 Falseλ₯Ό λ°˜ν™˜ν•˜μ§€ μ•Šκ³  Trueλ₯Ό λ°˜ν™˜ν•˜λ©΄ 5λͺ…은 λͺ¨λ‘ 건널 수 μžˆλ‹€λŠ” μ†Œλ¦¬κ°€ λœλ‹€.







μ•„λž˜λŠ” 건널 수 μžˆλŠ” 경우,

image




μ•„λž˜λŠ” 건널 수 μ—†λŠ” κ²½μš°λ‹€.

image







μ˜ˆμ „μ— ν’€λ‹€κ°€ ν¬κΈ°ν•œ λ¬Έμ œμ˜€λŠ”λ°,

λ°”λ‘œ 이뢄 νƒμƒ‰μž„μ„ 캐치 ν•˜μ˜€κ³  μ½”λ“œλ„ λ°”λ‘œ μ§œμ—¬μ§€λŠ” 것을 λ³΄λ‹ˆκΉŒ μ–΄λŠμ •λ„ μ„±μž₯을 ν•˜κΈ΄ ν•œ 것 κ°™λ‹€.







이뢄 νƒμƒ‰μž„μ„ μΊμΉ˜ν•˜μ—¬λ„ left, right의 κ°’ μ„€μ •κ³Ό whileλ¬Έ νƒˆμΆœ 쑰건을 μ„€μ •ν•˜κΈ°κ°€ κΉŒλ‹€λ‘œμšΈ μˆ˜κ°€ μžˆλŠ”λ°,

이뢄탐색 ν—·κ°ˆλ¦¬μ§€ μ•Šκ²Œ κ΅¬ν˜„ν•˜κΈ°λ₯Ό μ°Έκ³ ν•˜μ—¬ μ²΄ν™”ν•˜λ©΄ 쒋을 것 κ°™λ‹€!

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

@tgyuuAn tgyuuAn added tgyuuAn ν•œ μ€„λ‘œλŠ” μ†Œκ°œν•  수 μ—†λŠ” λ‚¨μž. μž‘μ„± 쀑 ⏱️ labels Mar 10, 2024
@tgyuuAn tgyuuAn self-assigned this Mar 10, 2024
@tgyuuAn tgyuuAn marked this pull request as ready for review March 10, 2024 07:13
Copy link
Collaborator

@H0ngJu H0ngJu left a comment

Choose a reason for hiding this comment

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

생각보닀 μ½”λ“œκ°€ κ°„κ²°ν•΄μ„œ λ†€λžμŠ΅λ‹ˆλ‹€

μ²˜μŒμ— 문제만 봀을 λ•ŒλŠ”, 완전탐색 밖에 생각이 μ•ˆλ‚¬λŠ”λ° μˆ˜λ„ μ½”λ“œλ₯Ό μ½μ–΄λ³΄λ‹ˆ μ œν•œμ‹œκ°„μ— κ±Έλ¦°λ‹€λŠ” μ΄μŠˆκ°€ μžˆμ—ˆλ„€μš” .. '높은 μ‹œκ°„ μ œν•œ -> 이뢄탐색이지 μ•Šμ„κΉŒ?' μΈμ‚¬μ΄νŠΈλ₯Ό μ–»μ–΄κ°‘λ‹ˆλ‹€!

μ°Έκ³  링크도 잘 μ½μ–΄λ³΄μ•˜μŠ΅λ‹ˆλ‹€ :) μˆ˜κ³ ν•˜μ…¨μŠ΅λ‹ˆλ‹€! 면접도 ν™”μ΄νŒ…μ΄μ—μ˜€ πŸ’ͺ

@tgyuuAn
Copy link
Member Author

tgyuuAn commented Mar 13, 2024

생각보닀 μ½”λ“œκ°€ κ°„κ²°ν•΄μ„œ λ†€λžμŠ΅λ‹ˆλ‹€

μ²˜μŒμ— 문제만 봀을 λ•ŒλŠ”, 완전탐색 밖에 생각이 μ•ˆλ‚¬λŠ”λ° μˆ˜λ„ μ½”λ“œλ₯Ό μ½μ–΄λ³΄λ‹ˆ μ œν•œμ‹œκ°„μ— κ±Έλ¦°λ‹€λŠ” μ΄μŠˆκ°€ μžˆμ—ˆλ„€μš” .. '높은 μ‹œκ°„ μ œν•œ -> 이뢄탐색이지 μ•Šμ„κΉŒ?' μΈμ‚¬μ΄νŠΈλ₯Ό μ–»μ–΄κ°‘λ‹ˆλ‹€!

μ°Έκ³  링크도 잘 μ½μ–΄λ³΄μ•˜μŠ΅λ‹ˆλ‹€ :) μˆ˜κ³ ν•˜μ…¨μŠ΅λ‹ˆλ‹€! 면접도 ν™”μ΄νŒ…μ΄μ—μ˜€ πŸ’ͺ

νŒŒμ΄μ¬μ€ 1μ΄ˆμ— 2천만번 연산은 정말 μ€‘μš”ν•œ μ •λ³΄λ‹ˆ κΌ­ μ–»μ–΄κ°€μ„Έμš”!

ν”„λ‘œκ·Έλž˜λ¨ΈμŠ€λŠ” λ”°λ‘œ λͺ…μ‹œλ˜μ–΄μžˆμ§€ μ•ŠλŠ” 이상 주둜 5초,

백쀀은 λ”°λ‘œ λͺ…μ‹œλ˜μ–΄μžˆμ§€ μ•ŠμœΌλ©΄ λ¬Έμ œμ—μ„œ 주어진 μ‹œκ°„μ— *3λ°°ν•œκ±°μ— +2μ΄ˆν•œ κ°’μž…λ‹ˆλ‹€.

(1μ΄ˆκ°€ 주어지면 νŒŒμ΄μ¬μœΌλ‘œλŠ” 5초λ₯Ό μ“Έ 수 μžˆμ–΄μš”)

Copy link
Collaborator

@pknujsp pknujsp left a comment

Choose a reason for hiding this comment

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

닀리 길이가 μ—„μ²­ κΈΈκ³ , μ§€λ‚˜κ°ˆ 수 μžˆλŠ” μ‚¬λžŒ μˆ˜λ„ λ§Žμ•„μ„œ

배열을 λ°˜λ³΅ν•΄μ„œ ν›‘μœΌλ©΄μ„œ ν•˜λ‚˜ν•˜λ‚˜ 경우λ₯Ό μ²΄ν¬ν•˜λŠ” 건
말이 μ•ˆλ κ²ƒ κ°™μ•„μ„œ 이진 νƒμƒ‰μœΌλ‘œ ν’€μ–΄μ•Ό ν•˜λŠ” λŠλ‚Œμ΄μ—ˆλŠ”λ° λ§žμ•˜λ„€μš”.

λΉ„μŠ·ν•œ λ¬Έμ œλ“€μ„ ν’€λ©΄μ„œ μ–΄λŠμ •λ„ 문제 νŒ¨ν„΄μ΄ μ΅ν˜€μ§„ κ±° κ°™μŠ΅λ‹ˆλ‹€.

νƒœκ·œλ‹˜ μ½”λ“œλž‘ 거의 같은데
이진 탐색 μ‹œ λ²”μœ„ μ‘°μ •, 쑰건문만 쑰금 λ‹€λ₯΄λ„€μš”

def solution(stones, k):
    left, right = 0, 200_000_000
    
    while left <= right:
        mid = (left + right) // 2
        length = 0
        passed = True
        
        for v in stones:
            if v <= mid:
                length += 1
                if length >= k:
                    passed = False
                    break
            elif length:
                length = 0
        
        if passed:
            left = mid + 1
        else:
            right = mid - 1
            
    return left

@Munbin-Lee
Copy link
Member

#include <string>
#include <vector>
#include <algorithm>

using namespace std;

int solution(vector<int> stones, int k) {
    int n = stones.size();
    vector<int> tree(n * 2);
    
    for (int i = 0; i < n; i++) {
        tree[n + i] = stones[i];
    }
    
    for (int i = n - 1; i; i--) {
        tree[i] = max(tree[i * 2], tree[i * 2 + 1]);
    }
    
    auto range = [&](int begin, int end) {
        int res = -1;
        begin += n;
        end += n;
        
        while (begin != end) {
            if (begin % 2) res = max(res, tree[begin++]);
            if (end % 2) res = max(res, tree[--end]);
            begin /= 2;
            end /= 2;
        }
        
        return res;
    };
    
    int answer = *max_element(stones.begin(), stones.end());
    
    for (int i = 0; i + k <= n; i++) {
        answer = min(answer, range(i, i + k));
    }
    
    return answer;
}

μ„Έκ·Έλ¨ΌνŠΈ νŠΈλ¦¬λ‘œλŠ” 무쑰건 ν’€λ¦°λ‹€ -> νˆ¬λ¨ΈμΉ˜μΈλ°μš”? -> λ‹€λ₯Έ 방법이 μ•ˆ λ– μ˜€λ¦„

ν•΄μ„œ μ„Έκ·Έλ¨ΌνŠΈ 트리둜 ν’€μ–΄λ³΄μ•˜μŠ΅λ‹ˆλ‹€ ν•˜ν•˜

μ„Έκ·Έλ¨ΌνŠΈ 트리의 νŠΉμ§•

range, queryκ°€ λͺ¨λ‘ O(log(N)) μ²˜λ¦¬κ°€ λœλ‹€

-> νŠΉμ • κ΅¬κ°„μ˜ μ΅œλŒ“κ°’μ„ log(N)λ§Œμ— ꡬ할 수 있음

νƒœκ·œλ‹˜ μ½”λ“œ 보고 λ‚˜λ‹ˆκΉŒ λ­”κ°€ ν—ˆνƒˆν•˜λ„€μš”..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
tgyuuAn ν•œ μ€„λ‘œλŠ” μ†Œκ°œν•  수 μ—†λŠ” λ‚¨μž. 리뷰 μ™„λ£Œ βœ”οΈ
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants