We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
My Java solution with o(n log n) time complexity and o(1) space complexity:
publicstaticList<Integer>maximumPerimeterTriangle(List<Integer>sticks){// find 3 vals that produce the max sum; vals are sorted, and first two sum is greater than 3rd val//sort array ascendingCollections.sort(sticks);//check each max len and if it follows the triangle inequality theoremfor(inti=sticks.size()-1;i>1;i--){intmaxLen=sticks.get(i);intmidLen=sticks.get(i-1);intminLen=sticks.get(i-2);if(minLen+midLen>maxLen)returnArrays.asList(minLen,midLen,maxLen);}returnArrays.asList(-1);}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Maximum Perimeter Triangle
You are viewing a single comment's thread. Return to all comments →
My Java solution with o(n log n) time complexity and o(1) space complexity: