본문 바로가기
python-algorithm

leetcode 1679. Max Number of K-Sum Pairs

by 무적김두칠 2022. 5. 4.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution:
    def maxOperations(self, nums: List[int], k: int-> int:
        ans=0
        nums.sort()
        start=0
        end=len(nums)-1
        while end>start :
            if nums[end] + nums[start] > k :
                end-=1
            elif nums[end] + nums[start] < k :
                start +=1
            else :
                ans+=1
                end -= 1
                start += 1
        return ans
cs

 

전형적인 투 포인터 문제죠 

반응형

댓글