Skip to content

[Algorithm] MaxProductOfThreeΒ #143

Description

@hwangJi-dev

πŸ’¬Β λ¬Έμ œ

https://app.codility.com/programmers/lessons/6-sorting/max_product_of_three/


πŸ’¬Β Idea

  • μ²˜μŒμ— λ‹¨μˆœνžˆ μ •λ ¬ν•œ λ’€ κ°€μž₯ 큰 3개의 수만 κ³±ν•˜λ©΄ λœλ‹€κ³  μƒκ°ν–ˆμ—ˆλ‹€. κ·ΈλŸ¬λ‚˜ μŒμˆ˜κ°€ ν¬ν•¨λ˜λŠ” 것을 λ†“μ³μ„œ 정확도가 λ–¨μ–΄μ‘Œλ‹€..
  • λ”°λΌμ„œ μŒμˆ˜κ°€ ν¬ν•¨λœ 경우 κ°€μž₯ μž‘μ€ 두 μŒμˆ˜μ™€ μ–‘μˆ˜ 쀑 κ°€μž₯ 큰 수λ₯Ό κ³±ν•˜λ©΄ μ΅œλŒ“κ°’μ΄ λ‚˜μ˜¬ 수 μžˆμœΌλ―€λ‘œ ν•΄λ‹Ή κ²½μš°μ™€ μ–‘μˆ˜ 3개의 곱을 λΉ„κ΅ν•˜μ—¬ κ°€μž₯ 큰 수λ₯Ό λ„μΆœν•΄μ£Όμ—ˆλ‹€.

πŸ’¬Β ν’€μ΄

publicfunc solution6(_ A :inout[Int])->Int{
A =A.sorted()letcontainMinusMaxValue=A[0]* A[1]* A[A.count -1]letMaxValue=A[A.count -3]* A[A.count -2]* A[A.count -1]return containMinusMaxValue > MaxValue ? containMinusMaxValue : MaxValue
}

μ†Œμš”μ‹œκ°„ : 10λΆ„

μ‹œκ°„ λ³΅μž‘λ„ : O(N * log(N))

ν‰κ°€ν‘œ : https://app.codility.com/demo/results/trainingU26PE4-8TS/

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions