aboutsummaryrefslogtreecommitdiff
path: root/largest_perimeter_triangle.cpp
blob: 64b368280e1e95e62dc1ba59968260b49f7ca3f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include "leetcode.h"

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

int main() {
  Solution obj;
  vector<int> nums = {2, 1, 2};
  cout << obj.largestPerimeter(nums);
}