-
Notifications
You must be signed in to change notification settings - Fork 0
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
8-oesnuj #27
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
μ°μ μμνκ° κ°μ₯ λ§μ΄ μ°μ΄λ κ³³μ΄λΌ νλ©΄... μμ "그리λ"λ "μ΅λ¨κ²½λ‘(λ€μ΅μ€νΈλΌ)"λΌ μκ°λλ€μ.
λ§μΉ¨ νλ‘κ·Έλλ¨Έμ€μλ ν(heap)μΌλ‘ λ°λ‘ λͺ¨μλμ κ²λ μμΌλ ν λ² μ₯~?
using namespace std; | ||
|
||
int main() { | ||
ios::sync_with_stdio(false); cin.tie(NULL); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
μΌμ
NULL
λ§κ³ nullptr
μ¨μ£ΌμΈμ!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
λ§μΉ¨ μ κ° μ΅κ·Όμ νμλ λ¬Έμ λ λμ 0 그리λμΈλ° νΉμ try...??
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
μ΅μ ν! μ΅μ λΉμ©μ ꡬν λ μ¬μ©νλ κΈ°μ΅μ΄ μμ΄μ! μ΄μ°μν μκ°μ μ‘°κΈ λ°°μ΄ μ μ΄ μμ΄μ μμΈν κ°λ μ μ λͺ°λλλ°, λ€μ΅μ€νΈλΌ λ¬Έμ λ₯Ό νλ©΄μ λ€μ 곡λΆνμκ±°λ μ! μ€μλμ΄ μ²¨λΆν΄ μ£Όμ μ¬μ΄νΈ 보면μλ μ’ κ³΅λΆλ₯Ό ν΄λ΄€λλ° 'μ°μ μμ ν'λ₯Ό ꡬνν λ κ°μ₯ λ§μ΄ μ΄λ κ΅¬μ‘°κ° heapμ΄λΌλ κ±Έ μκ²λμμ΄μ! μ λ μ°μ μμ ν λ¬Έμ ν λ² νμ΄λ΄μΌ κ² μ΄μπ
μ΅μ νμ λ§μ‘±νλ μνκ° λλλ‘ μ½μ κ³Ό μμ νλ κ³Όμ μ 보면μ ν μλ£κ΅¬μ‘°κ° μ΄λ€μμΌλ‘ ꡬνλλ κ±΄μ§ μκ² λ κ² κ°μ΅λλ€~ νμ§λ§ μ½λλ μ΄λ €μμγ γ μ’ λ 곡λΆκ° νμν κ² κ°μ΅λλ€...π
λλκΉ λνλμ΄ 6λ²μ§Έ PRλ‘ μ¬λ¦¬μ ¨λ λ¬Έμ μ μ μ¬νλ€λ λλμ λ°μμ΄μ 보λκΉ κ·Έλ¦¬λ λ¬Έμ μλλ°, μ΅μκ°μ μ°Ύλ κ±°λΌ κ·Έλ¬λλ΄μ! κ΅ν©λ λκΈμ 보λ μ°μ μμ νλ 그리λμ μ΅λ¨ κ²½λ‘μλ λ§μ΄ μ°μ΄λλ΄μ! μκ°ν΄λ³΄λ λͺ¨λ μ΅μ, κ°μ₯ μ΅μ μ΄ λλ κ²μ μ°Ύλ κ·Έλ° λλμ΄λ λ§μ΄ μ°μ΄λλ΄μ νμλ λ¬Έμ λ€μ΄ λ€λ₯Έ μλ£κ΅¬μ‘°λ₯Ό μ΄μ©ν΄μλ ν μ μλ€λ μ κΈ°νκ³ λ€λ₯Έ λ°©λ²μΌλ‘λ λ§μ΄ νμ΄λ΄μΌ κ² μ΄μ!
μ΅μ νμ μ΄μ©ν΄ κ°μ₯ μμ λ μΉ΄λ λ¬Άμμ λ°λ³΅μ μΌλ‘ ν©μΉλ λ°©μμΌλ‘ μ½λλ₯Ό μ μ§ κ² κ°μμ! μ½λμ μ£Όμμ΄ λ§μ μ½λλ₯Ό μ΄ν΄νλλ° λ§μ λμμ΄ λμμ΅λλ€.
PRμκ³ νμ
¨μ΄μ~
π λ¬Έμ λ§ν¬
λ°±μ€ | μΉ΄λ μ λ ¬νκΈ°
βοΈ μμλ μκ°
30λΆ
β¨ μλ μ½λ
μΉ΄λ λ¬Άμμ ν©μ³κ°λ©΄μ λΉκ΅λ₯Ό μννλλ° μ΅μμ λΉκ΅λ‘ ν©μΉλ€λ©΄ μ΅μ λͺλ²μ λΉκ΅κ° νμνμ§ λ¬»λ λ¬Έμ
μ¦, μ°λ¦¬λ "νμ¬ μ‘΄μ¬νλ μΉ΄λ λ¬Άμ μ€μμ, κ°μ₯ μ μ 2κ°μ λ¬Άμμ μ°μ μ μΌλ‘ κ³μ°" νλ κ²μ΄ μ λ΅μ΄λΌλ κ²μ νμ νμ΅λλ€.
ex) 5 4 3 2 1 μΉ΄λ λ¬Άμμ΄ μ΄λ κ² κ΅¬μ±λμ΄ μλ€λ©΄
(1+2) + (3+3) + (6+4 )+ (10+5) <= μ΄κ² κ°μ₯ μ΅μμ λΉκ΅λ‘ ν©μΉλ κ²½μ°μ λλ€.
μ΄μ μΉ΄λ λ¬Άμμ λΉκ΅νμκ° λ€μ μΉ΄λ λΉκ΅νμμ μν₯μ μ£ΌκΈ° λλ¬Έμ κ°μ₯ μ μ μΉ΄λ λ¬Άμμ κ³μ ν©μ³μΌν©λλ€!!
βνμ΄ κ³Όμ
Nκ°μ μΉ΄λ λ¬Άμμμ κ°μ₯ μμ λ κ°λ₯Ό κ³¨λΌ ν©μΉλ€ -> ν©μΉ λ μΉ΄λλ₯Ό μΉ΄λ λ¬Άμμ λ£λλ€ -> λ°λ³΅
Nκ°μ μΉ΄λ λ¬Άμμμ μ΅μκ° 2κ°λ₯Ό κΊΌλ΄κΈ° μν΄ μ¬μ©ν κ²μ΄ μ΅μνμ΄λ€.
μ΄λ μ°μ μμ νλ₯Ό μ¬μ©ν΄μ μ΅μ νμ μ¬μ©νλ€.
Note
μΉ΄λ λ¬Άμλ€μ μ°μ μμ νμ λ€μ΄μλ€. popμ 2λ²ν΄μ κ°μ₯ μ μ λ¬Άμ 2κ°λ₯Ό λΉΌμ€λ€.
μ΄ λ λ¬Άμμ λνμ¬ λ€μ μ°μ μμ νμ pushν΄μ€λ€.
λ λ¬Άμμ ν©μ΄ λ λ¬Άμμ λΉκ΅νμ μ΄λ―λ‘ resultμ λν΄μ£Όκ³ λ€μ 1λ² κ³Όμ μΌλ‘ λμκ°λ€.
νμ 1κ°μ κ°μ΄ λ¨μ λ κΉμ§ μ 1 ~ 3 κ³Όμ μ λ°λ³΅νλ€. (2κ°κ° λμμ 1κ°κ° λ€μ λ€μ΄κ°λ μΈμ κ° 1κ°κ° λ¨μ)
λ°λ³΅μ΄ λλ¬μλ resultμ κ°μ΄ λ΅μ΄λ€.
π€μ°μ μμν
μ°μ μμ νλ λ€μ΄μ€λ μμμ μκ΄ μμ΄ μ°μ μμκ° λμ λ°μ΄ν°κ° λ¨Όμ λκ°λ μλ£κ΅¬μ‘°
μ°μ μμ νλ νμ μ¬μ©νμ¬ κ΅¬ννλ κ²μ΄ κ°μ₯ ν¨μ¨μ μ λλ€.
(μ΅μ)νμ μμ μ΄μ§ νΈλ¦¬ κ΅¬μ‘°λ‘ λΆλͺ¨λ Έλλ μμλ Έλλ³΄λ€ λ°λμ κ°μ΄ μμ ꡬ쑰λ₯Ό μ μ§νλ©° λ°μ΄ν°λ₯Ό μ μ₯ν©λλ€.
ν©λλ€.
λ°λΌμ κ°μ νμ μ μ₯νλ©΄ 루νΈ(μ΅μμ)λ Έλλ μ μΌ μμ κ°μ΄ λ©λλ€.
popμ νλ©΄ μ΄ μ΅μμ λ Έλκ° κΊΌλ΄μ§κ³ λ€μ μ΅μκ°μ΄ μ΅μμλ Έλλ‘ κ°λ©΄μ νΈλ¦¬κ° λ€μ μ λ ¬? μ±μμ§κ² λ©λλ€.
μ΅λνμ λΆλͺ¨λ Έλ > μμλ Έλλ₯Ό μ μ§νλ©΄μ μ μ₯
push μμλ ν΄λΉ κ°μ΄ μ μ ν μμΉμ λ Έλλ‘ κ°μΌνκΈ° λλ¬Έμ μμ볡μ‘λ O(log N)
pop μμλ 루νΈλ Έλκ° λΉ μ§κ³ κ·Έ μμΉλ‘ λ€μ κ°μ΄ μ±μμ ΈμΌνκΈ° λλ¬Έμ μκ°λ³΅μ‘λ O(log N)
ν μλ£κ΅¬μ‘° μ°Έκ³
π μ 체μ½λ
π μλ‘κ² μκ²λ λ΄μ©
μμ μ΄μ§ νΈλ¦¬κ΅¬μ‘°λ₯Ό νμ©ν ν μλ£κ΅¬μ‘°λ₯Ό 곡λΆνκ³ νμ΄λ΄€μ΅λλ€.
μλ‘μ΄ μλ£κ΅¬μ‘°λ₯Ό 곡λΆν λλ§λ€ ν λ¬Έμ κ° μκΈ°λ€μ