[코테 풀이] Largest Positive Integer That Exists With Its Negative

시내·2024년 6월 29일

Q_2441) Largest Positive Integer That Exists With Its Negative

출처 : https://leetcode.com/problems/largest-positive-integer-that-exists-with-its-negative/

class Solution {
    public int findMaxK(int[] nums)  {
        List<Integer> arr = new ArrayList<>();
        for (int n : nums) arr.add(n);
        Collections.sort(arr);
        int i = 0, j = arr.size() - 1;
        while (i < arr.size()) {
            if (arr.get(i) == arr.get(j) * (-1)) return arr.get(j);
            else if (i == j) {
                i++;
                j = arr.size() - 1;
            } else {
                j--;
            }
        }
        return -1;
    }
}
profile
contact 📨 ksw08215@gmail.com

0개의 댓글