SeangG
def _(): _()
SeangG
전체 방문자
오늘
어제
  • 분류 전체보기 (37)
    • Programming Language (0)
      • Python (0)
      • Web (0)
    • Algorithm (34)
    • Art (0)
      • 3D Modeling (0)
      • Pixel (0)
      • Picture (0)
    • Game (0)
    • Project (3)
      • Problems (3)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

  • Tree
  • Fast Refresh
  • string
  • WSL
  • Queue
  • react.js
  • 로마 숫자
  • dfs
  • 문자열
  • BFS
  • Python
  • github oauth
  • leetcode
  • next.js
  • graph
  • Leecode
  • spring boot
  • Daily Question
  • LeedCode
  • 매핑 테이블

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
SeangG

def _(): _()

Algorithm

[LeetCode] 2441. Largest Positive Integer That Exists With Its Negative (Daily Question)

2024. 5. 2. 10:46

문제


Given an integer array nums that does not contain any zeros, find the largest positive integer k such that -k also exists in the array.

Return the positive integer k. If there is no such integer, return -1.

 

정수 리스트 nums가 주어졌을 때 음수도 들어있는 양수의 최대값을 찾는 문제이다.

 

Example 1:
Input: nums = [-1,2,-3,3] Output: 3 Explanation: 3 is the only valid k we can find in the array.

Example 2:
Input: nums = [-1,10,6,7,-7,1] Output: 7 Explanation: Both 1 and 7 have their corresponding negative values in the array. 7 has a larger value.

Example 3:
Input: nums = [-10,8,6,7,-2,-3] Output: -1 Explanation: There is no a single valid k, we return -1.

 

풀이


아이디어

- 포인터 사용

 

 

코드

class Solution:
    def findMaxK(self, nums: List[int]) -> int:
        p1 = 0
        p2 = len(nums)-1
        nums.sort()
        while p1 < p2:
            if -nums[p1] == nums[p2]: return nums[p2]
            elif -nums[p1] < nums[p2]: p2 -= 1
            else: p1 += 1
            if nums[p1] >= 0: break
        return -1

 


 

후기


포인터를 다룰줄 안다면 쉽게 풀 수 있는 문제였다.

 

'Algorithm' 카테고리의 다른 글

[LeetCode] 2816. Double a Number Represented as a Linked List (Daily Question)  (0) 2024.05.07
[LeetCode] 165. Compare Version Numbers (Daily Question)  (0) 2024.05.03
[LeetCode] 2000. Reverse Prefix of Word (Daily Question)  (1) 2024.05.01
[LeetCode] 2997. Minimum Number of Operations to Make Array XOR Equal to K (Daily Question)  (0) 2024.04.29
[LeetCode] 514. Freedom Trail (Daily Question)  (0) 2024.04.27
    'Algorithm' 카테고리의 다른 글
    • [LeetCode] 2816. Double a Number Represented as a Linked List (Daily Question)
    • [LeetCode] 165. Compare Version Numbers (Daily Question)
    • [LeetCode] 2000. Reverse Prefix of Word (Daily Question)
    • [LeetCode] 2997. Minimum Number of Operations to Make Array XOR Equal to K (Daily Question)
    SeangG
    SeangG

    티스토리툴바