반응형

https://leetcode.com/problems/find-polygon-with-the-largest-perimeter

 

LeetCode - The World's Leading Online Programming Learning Platform

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

 

nums를 정렬하고, 순차적으로 순회하며 prefix sum을 구해줍니다.

nums[i] < prefix sum 이라면, 최댓값을 업데이트해줍니다.

class Solution {
public:
    long long largestPerimeter(vector<int>& nums) {
        sort(nums.begin(), nums.end());
        long long mx = -1, cur = nums[0];
        for(int i=1; i<nums.size(); i++) {
            if(cur > nums[i]) mx = max(mx, cur + nums[i]);
            cur += nums[i];
        }
        return mx;
    }
};
반응형

+ Recent posts