↓Skip to main content
  1. LeetCode/

LeetCode 3899: Angles of a Triangle

·2 mins· ·
LeetCode Medium Math Geometry
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2026-04-12

  • Submissions: Wrong Answer → Accepted

Approach
#

  1. Triangle inequality: positive area ⇔ any two sides sum to more than the third (strictly; equality makes the points collinear with zero area).
  2. Law of cosines: cos A = (b² + c² - a²) / (2bc), A = acos(cos A), converted to degrees.
  3. Output in non-decreasing order.

My Attempts
#

Attempt 1 computed the angles correctly but returned them in the order of a, b, c without sorting. For sides = [4, 4, 1] it returned [82.8, 82.8, 14.4] instead of [14.4, 82.8, 82.8]. Adding sorted passed.

Solution
#

from math import acos, degrees

class Solution:
    def internalAngles(self, sides: List[int]) -> List[float]:
        a, b, c = sorted(sides)
        if a + b <= c:
            return []                              # zero area or not a triangle
        def angle(opp: int, x: int, y: int) -> float:
            cos_v = (x * x + y * y - opp * opp) / (2 * x * y)
            return degrees(acos(max(-1.0, min(1.0, cos_v))))   # guard float error outside [-1, 1]
        return sorted([angle(a, b, c), angle(b, a, c), angle(c, a, b)])

Sorting first means the triangle inequality only needs the two shortest sides compared with the longest.

Complexity
#

Time O(1), space O(1).

Mistakes to Avoid
#

  • Sort the output.
  • The triangle inequality is strict.
  • Floating-point error can push acos’s argument slightly outside [-1, 1]; clamping is safer.

Interview Explanation
#

I sort the sides; it is a triangle only if the two shortest sum to strictly more than the longest. Then the law of cosines gives each angle, converted to degrees, and the output is sorted. Time O(1).

Related

LeetCode 1401: Circle and Rectangle Overlapping
·2 mins
LeetCode Medium Math Geometry
LeetCode 1401 note: the rectangle’s nearest point to the center is the center clamped into the rectangle; they overlap when that point is within the radius
LeetCode 973: K Closest Points to Origin
·1 min
LeetCode Medium Array Math Divide-and-Conquer Geometry
LeetCode 973 note: as in Top-K, keep a size-k max-heap (negated distances) holding the k closest points
LeetCode 976: Largest Perimeter Triangle
·2 mins
LeetCode Easy Math Greedy Sorting Geometry Array
LeetCode 976 note: after sorting, check consecutive triples from the largest down; the first with a + b > c gives the largest perimeter
LeetCode 633: Sum of Square Numbers
·2 mins
LeetCode Medium Two-Pointers Math Binary-Search
LeetCode 633 note: converge a from 0 and b from isqrt(c); shrink b when the sum is too big and grow a when it is too small; watch for 0, a == b, and floating-point square roots
LeetCode 3824: Minimum K to Reduce Array Within Limit
·2 mins
LeetCode Medium Binary-Search Math
LeetCode 3824 note: binary search on k with a monotonic check; the upper bound must reach n because there are at least n operations
LeetCode 3648: Minimum Sensors to Cover Grid
·2 mins
LeetCode Medium Math
LeetCode 3648 note: each sensor covers a (2k + 1) × (2k + 1) square, so take a ceiling division in each direction and multiply