LeetCode Entry

3024. Type of Triangle

19.05.2025 easy 2025 kotlin rust

Triangle type by lengths

3024. Type of Triangle easy blog post substack youtube 1.webp

Join me on Telegram

https://t.me/leetcode_daily_unstoppable/993

Problem TLDR

Triangle type by lengths #easy

Intuition

Was surprisingly hard to work all the corner cases.

Approach

  • a = max(), b = min(), c = sum() - a - b

Complexity

  • Time complexity: \(O(brain)\)

  • Space complexity: \(O(1)\)

Code


    fun triangleType(n: IntArray) =
        listOf("none", "equilateral", "isosceles", "scalene")[
        if (2 * n.max() >= n.sum()) 0 else n.toSet().size]


    pub fn triangle_type(mut n: Vec<i32>) -> String {
        n.sort();
        (if n[2] >= n[0] + n[1] { "none" } else
        if n[0] == n[2] { "equilateral" } else
        if n[0] == n[1] || n[1] == n[2] { "isosceles" } else { "scalene" }).into()
    }


    string triangleType(vector<int>& nums) {
        int f[101]={}, m = 0, mf = 0, s = 0;
        for (int x: nums) mf = max(mf, ++f[x]), m = max(m, x), s += x;
        return array{"none", "scalene", "isosceles", "equilateral"}[2 * m >= s ? 0 : mf];
    }