Created
August 10, 2022 08:14
-
-
Save piusayowale/682dc93d183091545e3531b3e6308f09 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| pair<bool, tuple<int, int, int>> isTriangle(vector<int> sticks,int it){ | |
| /* triangle inequality theorem */ | |
| /* a + b > c; */ | |
| /* a + c > b */ | |
| /* b + c > a */ | |
| auto it_begin = sticks.begin() + it; | |
| int a = *it_begin; | |
| int b = *(it_begin + 1); | |
| int c = *(it_begin + 2); | |
| if(a + b > c && a + c > b && b + c > a){ | |
| return {true, make_tuple(a, b, c)}; | |
| } | |
| return {false, {}}; | |
| } | |
| vector<int> maximumPerimeterTriangle(vector<int> sticks) { | |
| vector<tuple<int, int, int>> sides; | |
| sort(sticks.rbegin(), sticks.rend()); | |
| for(int i = 0; i < sticks.size() - 2; i++){ | |
| auto ch = isTriangle(sticks, i); | |
| if(ch.first == true){ | |
| sides.push_back(ch.second); | |
| } | |
| } | |
| if(sides.empty()){ | |
| vector<int> ret; | |
| ret.push_back(-1); | |
| return ret; | |
| } | |
| return vector<int>{get<2>(sides[0]),get<1>(sides[0]), get<0>(sides[0])}; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment