Notice
Recent Posts
Recent Comments
Link
«   2026/04   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30
Archives
Today
Total
관리 메뉴

hwooo

LeetCode (C/C++) 164. Maximum Gap 본문

Study/Algorithm

LeetCode (C/C++) 164. Maximum Gap

hwooo 2024. 10. 16. 14:33

https://leetcode.com/problems/maximum-gap/description/


풀이

오름차순으로 정렬 후, 오른쪽 원소와의 차를 비교하여 max gap을 찾는다.


코드

class Solution {
public:
    int maximumGap(vector<int>& nums) {
        sort(nums.begin(), nums.end());
        
        int maxGap = 0;
        for (int i = 0; i < nums.size() - 1; i++)
            maxGap = max(maxGap, nums[i + 1] - nums[i]);
        
        return maxGap;
    }
};