Maximum Perimeter Triangle

Sort by

recency

|

111 Discussions

|

  • + 0 comments
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.InputStreamReader;
    import java.io.OutputStreamWriter;
    import java.util.Arrays;
    import java.util.stream.Stream;
    
    import static java.util.stream.Collectors.joining;
    
    public class MaximumPerimeterTriangle {
    
        public static void main(String[] args) {
            try (var br = new BufferedReader(new InputStreamReader(System.in));
                 var bw = new BufferedWriter(new OutputStreamWriter(System.out))) {
                int n = Integer.parseInt(br.readLine().trim());
                int [] sticks = Stream.of(br.readLine().trim().split(" "))
                        .mapToInt(Integer::parseInt).sorted().toArray();
                int[] result = maximumPerimeterTriangle(n, sticks);
                bw.write(Arrays.stream(result).mapToObj(String::valueOf).collect(joining(" ")));
                bw.newLine();
                bw.flush();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        private static int[] maximumPerimeterTriangle(int n, int[] sticks) {
            for (int i = n - 1; i >= 2; i--) {
                if (sticks[i] < sticks[i - 1] + sticks[i - 2]) {
                    return new int[]{sticks[i - 2], sticks[i - 1], sticks[i]};
                }
            }
            return new int[]{-1};
        }
    }
    
  • + 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?