Maximum Perimeter Triangle

Sort by

recency

|

110 Discussions

|

  • + 0 comments

    You know what? I am getting pissed off with this test taking service. I pressed the submit button, my solution passed EVERY TEST but, I received an error pop-up saying the service is having trouble submitting my Java solution. This happened several times. I have my VPN disabled. What the hell do you want from me?

  • + 0 comments

    Rust best solution

    If you’re looking for solutions to the 3-month preparation kit in either Python or Rust, you can find them below: my solutions

    fn maximum_perimeter_triangle(sticks: &[i32]) -> Vec<i32> {
        //Time complexity: O(n*log(n))
        //Space complexity (ignoring input): O(n)
        let mut sorted_sticks = sticks.to_vec();
        sorted_sticks.sort_unstable_by(|a, b| b.cmp(a));
        for index in 0..(sorted_sticks.len() - 2) {
            if sorted_sticks[index] < sorted_sticks[index + 1] + sorted_sticks[index + 2] {
                let mut triangle = vec![
                    sorted_sticks[index],
                    sorted_sticks[index + 1],
                    sorted_sticks[index + 2],
                ];
                triangle.sort_unstable();
    
                return triangle;
            }
        }
    
        vec![-1]
    }
    
  • + 0 comments

    Python best solution

    If you’re looking for solutions to the 3-month preparation kit in either Python or Rust, you can find them below: my solutions

        #Time complexity: O(n*log(n))
        #Space complexity (ignoring input): O(1)
        sticks.sort(reverse=True)
        for index in range(0, len(sticks) - 2):
            if sticks[index] < sticks[index + 1] + sticks[index + 2]:
                triangle = [sticks[index], sticks[index + 1], sticks[index + 2]]
                triangle.sort()
                return triangle
    
        return [-1]
    
  • + 1 comment

    Can anyone explain why is there a need to sort the sticks array first?

    • + 0 comments

      You don't NEED to, but it makes the comparisons more efficient since you already know at every step in your logic which is the longest leg of the triangle and which is the shortest. Also, the longest triangle will probably be made up of the longest sticks, so by sorting you are getting to the longest sticks most efficiently.

  • + 0 comments

    A non-degenerate triangle is one where the sum of the lengths of any two sides is greater than the length of the remaining side.