-
[Leetcode] 16. 3Sum Closest (In Python)[Python] 파이썬/Leetcode_Medium 2021. 1. 12. 19:27
간략한 코드들은 github.com/Eastar-DS/Python 의 Leetcode 폴더에도 있습니다
Eastar-DS/Python
Contribute to Eastar-DS/Python development by creating an account on GitHub.
github.com
주어진 array에서 세 수의 합이 target과 가장 가까운 값을 갖도록 찾아내는 문제.
밑의 주석처리한 코드를 보면 처음엔 에라이 for문 다돌려버려! 하고 작성했는데 Time Limit Exceeded가 떠버렸다ㅋㅋ.
그래서 3Sum을 풀때와 마찬가지로 i를 기준으로 l(left)과 r(right)를 i의 바로 오른쪽과 끝으로 설정한뒤
diff변수에 세수의 합과 target과의 차를 넣어주면서 비교한다.
문제의 출처는 leetcode.com/problems/3sum-closest/입니다.
모르시는게 있으시다면 언제든지 댓글로 질문을 남겨주세요. 저에게 큰 공부가 됩니다. 감사합니다.
'[Python] 파이썬 > Leetcode_Medium' 카테고리의 다른 글
[Leetcode] 19. Remove Nth Node From End of List (In Python) (0) 2021.01.14 [Leetcode] 18. 4Sum (In Python) (0) 2021.01.13 [Leetcode] 17. Letter Combinations of a Phone Number (In Python) (0) 2021.01.12 [Leetcode] 15. 3Sum (In Python) (0) 2021.01.11 [Leetcode] 2. Add Two Numbers (In Python) (0) 2021.01.02