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

3sum 문제 최적화 관련 질문입니다. #43

Closed
PudgeKim opened this issue Oct 22, 2020 · 1 comment
Closed

3sum 문제 최적화 관련 질문입니다. #43

PudgeKim opened this issue Oct 22, 2020 · 1 comment

Comments

@PudgeKim
Copy link

PudgeKim commented Oct 22, 2020

우선 이 질문은 책의 오류나 오타 등에 관한 질문이 아니라서 해도되는건지 잘 모르겠습니다.
만약 안되는거라면 확인 후에 삭제하겠습니다.

class Solution:
def threeSum(self, nums: List[int]) -> List[List[int]]:
if len(nums) < 3:
return []

    nums.sort()
    res = []
    for i in range(len(nums)-2):
    if i > 0 and nums[i] == nums[i-1]:
            continue
        left, right = i+1, len(nums)-1
        while left < right:
            tot = nums[i]+nums[left]+nums[right]
            if tot == 0 and sorted([nums[i], nums[left], nums[right]]) not in res:
                res.append(sorted([nums[i], nums[left], nums[right]]))
                left += 1
                right -= 1
            elif tot < 0:
                left += 1
            elif tot > 0:
                right -= 1
            else: # tot이 0이고 res에 이미 중복된 배열이 있을 때 무한루프를 방지하기위함
                left += 1
                right -= 1
    return res

이건 제가 구현한 풀이이고 릿코드에 제출하면 time limit이 나오는데 제가 봤을 때 시간복잡도가 책이랑 차이가 왜 많이 나는지
잘 모르겠는데 이유를 알 수 있을까요? 파이참에서 오류 케이스를 돌려본 결과 답이 나오긴하는걸보면 알고리즘은 맞는거같은데 어떤거때매 시간복잡도 차이가 많이 나는건지 잘 모르겠습니다.

@PudgeKim
Copy link
Author

해결되었습니다

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant