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

65-9kyo-hwang #224

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open

65-9kyo-hwang #224

wants to merge 8 commits into from

Conversation

9kyo-hwang
Copy link
Collaborator

πŸ”— 문제 링크

λ””νŽœμŠ€ κ²Œμž„

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

1μ‹œκ°„

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

μ΅œλŒ€ $1,000,000$ λΌμš΄λ“œμ— 걸쳐 적듀이 λ“€μ–΄μ˜¨λ‹€. 그리고 μ΅œλŒ€ $500,000$λΌμš΄λ“œλ₯Ό λ¬΄μ κΆŒμ„ μ‚¬μš©ν•΄ 병사 μ†Œλͺ¨λ₯Ό 0으둜 λ§Œλ“€ 수 μžˆλ‹€.

λ°±νŠΈλž˜ν‚Ή 등을 μ΄μš©ν•΄ λͺ¨λ“  경우의 수λ₯Ό κ΅¬ν•˜κΈ°μ—λŠ” μˆ«μžκ°€ λ„ˆλ¬΄ 크닀. λΌμš΄λ“œκ°€ μ΅œλŒ€ $1,000,000$이기 λ•Œλ¬Έμ— $O(N^2)$ 풀이법도 λΆˆκ°€λŠ₯ν•˜λ‹€. 즉 $O(N logN)$ 풀이법을 μ‚¬μš©ν•΄μ•Ό ν•œλ‹€.

μ§κ΄€μ μœΌλ‘œ μƒκ°ν–ˆμ„ λ•ŒλŠ”, λ¬΄μ κΆŒμ€ κ°€λŠ₯ν•œ "적의 μˆ˜κ°€ λ§Žμ€ λΌμš΄λ“œ"에 μ‚¬μš©ν•˜λŠ” 것이 μ’‹λ‹€. κ·ΈλŸ¬λ‚˜ μ΄λŸ¬ν•œ 방법은 "μ„ ν˜•νƒμƒ‰"으둠 λΆˆκ°€λŠ₯ν•˜κ³ , κ·Έλ ‡λ‹€κ³  μœ„μ—μ„œ μ–ΈκΈ‰ν•œ κ²ƒμ²˜λŸΌ λͺ¨λ“  경우의 수λ₯Ό λ‹€ 확인할 μˆ˜λŠ” μ—†λ‹€.

λ”°λΌμ„œ μ—¬κΈ°μ„œλŠ” μš°μ„ μˆœμœ„ 큐λ₯Ό μ‚¬μš©ν•œλ‹€. μ›λ¦¬λŠ” κ°„λ‹¨ν•˜λ‹€.

  1. 일단은 맀 λΌμš΄λ“œλ§ˆλ‹€ λ“±μž₯ν•˜λŠ” 적 μˆ˜μ— 맞게 병사λ₯Ό μ†Œλͺ¨ν•œλ‹€. 그리고 λ™μ‹œμ— 적 수λ₯Ό MaxHeap에 κΈ°λ‘ν•œλ‹€.
  2. λ§Œμ•½ 남은 병사가 μ‘΄μž¬ν•œλ‹€λ©΄ μ΄μ–΄μ„œ λΌμš΄λ“œλ₯Ό μ§„ν–‰ν•œλ‹€.
  3. 남은 병사가 ν•˜λ‚˜λ„ μ—†λ‹€λ©΄ μ•„λž˜ λ‘œμ§μ„ μˆ˜ν–‰ν•œλ‹€.
    i. λ§Œμ•½ 더 이상 λ¬΄μ κΆŒμ„ μ‚¬μš©ν•  수 μ—†λ‹€λ©΄ λΌμš΄λ“œλ₯Ό μ€‘λ‹¨ν•œλ‹€.
    ii. μ•„λ‹ˆλΌλ©΄ MaxHeapμ—μ„œ μ§€λ‚œ λΌμš΄λ“œ 쀑 κ°€μž₯ 적이 λ§Žμ•˜λ˜ λΌμš΄λ“œμ˜ 적 수λ₯Ό popν•΄ κ·Έ 수 만큼 병사 수λ₯Ό λ³΅μ›μ‹œν‚€κ³  남은 무적ꢌ 횟수λ₯Ό 1 μ°¨κ°μ‹œν‚¨λ‹€.

이런 μ‹μœΌλ‘œ MaxHeap을 μ‚¬μš©ν•˜λ©΄ $O(logN)$ μ‹œκ°„λ³΅μž‘λ„λ‘œ μ§€λ‚œ λΌμš΄λ“œ 쀑 κ°€μž₯ 적이 λ§Žμ€ λΌμš΄λ“œ 정보λ₯Ό μ–»μ–΄μ˜¬ 수 있게 λœλ‹€.

제λͺ©μ„-μž…λ ₯ν•΄μ£Όμ„Έμš”_ (2)

#include <string>
#include <vector>
#include <queue>

using namespace std;

int solution(int N, int K, vector<int> InEnemies) 
{
    priority_queue<int> NumofEnemiesBlocked;  // MaxHeap
    int Round = 0;
    
    for(int Enemy : InEnemies)
    {
        N -= Enemy;
        NumofEnemiesBlocked.emplace(Enemy);
        
        if(N < 0)  // λ¬΄μ κΆŒμ„ μ‚¬μš©ν•΄μ•Ό 함!
        {
            if(K == 0)  // 더 이상 λΌμš΄λ“œ 진행 λΆˆκ°€
            {
                break;
            }
            
            N += NumofEnemiesBlocked.top();  // 이전 λΌμš΄λ“œ 쀑 κ°€μž₯ 적이 λ§Žμ•˜λ˜ λΌμš΄λ“œ 정보 μΆ”μΆœ
            NumofEnemiesBlocked.pop();
            K--;
        }
        
        Round++;
    }
    
    return Round;
}

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

와케 그리디 μœ ν˜•μ€ μ•ˆν’€λ¦¬λŠ”μ§€ λͺ¨λ₯΄κ² λ‹€.
μ˜ˆμ „μ—λ„ μ½”ν…Œλ³Ό λ•Œ 그리디/μš°μ„ μˆœμœ„ν μ“°λŠ” 문제 λͺ» ν’€μ—ˆμ—ˆλŠ”λ°, 이것도 ν•œμ°Έ μ‚½μ§ˆν•˜λ‹€κ°€ 겨우 ν•΄κ²°ν–ˆλ‹€ 흠...

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.

문제 λ‹€ ν’€κ³ , κ΅ν™©λ‹˜ μ½”λ“œ λ³΄λ‹ˆ round λ³€μˆ˜λͺ…이 더 λͺ…ν™•ν•΄μ„œ λ°”λ‘œ λƒ…λ‹€ μˆ˜μ •ν–ˆμŠ΅λ‹ˆλ‹€ γ…‹γ…‹γ…‹γ…‹

import heapq 
def solution(n, k, enemy):
    answer = len(enemy)
    pq = []
    my = n
    card = k

    for round in range(len(enemy)):
        my -= enemy[round]
        heapq.heappush(pq, -enemy[round])
        if my < 0:
            if card > 0:
                my += heapq.heappop(pq) * -1 
                card -= 1
            else :
                return round
    return answer

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.

μ˜€λžœλ§Œμ— λ³΄λŠ” μ΅œλŒ€νž™ μŠ₯μ‚­~

from heapq import *

def solution(n, k, enemy):
    round = 0

    fight = []

    for e in enemy:
        round += 1
        n -= e
        heappush(fight, -e)

        while n < 0 and k > 0:
            k -= 1
            n -= heappop(fight)
        
        if n < 0 :
            round -= 1
            break

    return round

@9kyo-hwang 9kyo-hwang mentioned this pull request Oct 1, 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.

3 participants