Solution record: Likou 2441. The largest positive integer that exists simultaneously with the corresponding negative number (violent solution)

Given an array of integers that does not contain any zeros nums, find the largest positive integer that exists in the array with both itself and the corresponding negative number k. Returns a positive integer , or returns if no such integer exists . k-1

Example 1:

Input: nums = [-1,2,-3,3]
 Output: 3
 Explanation: 3 is the only k in the array that meets the requirements of the topic.

Example 2:

Input: nums = [-1,10,6,7,-7,1]
 Output: 7
 Explanation: There are negative numbers corresponding to 1 and 7 in the array, and the value of 7 is greater.

Example 3:

Input: nums = [-10,8,6,7,-2,-3]
 Output: -1
 Explanation: There is no k that meets the requirements of the title, and -1 is returned.
class Solution:
    def findMaxK(self, nums: List[int]) -> int:
        result =  0
        for i in range(len(nums)):
            for j in range(i , len(nums)):
                if nums[i] + nums[j] == 0:
                    result = max(result , abs(nums[i]))
        return result if result != 0 else -1

Guess you like

Origin blog.csdn.net/weixin_45314061/article/details/130659724