문제
Find every triple of positive integers a < b < c for which a² + b² = c² and a + b + c = perimeter. Return each triple as [a, b, c], ordered by increasing a and then b. Do not include permutations of the same sides.
Implement pythagorean_triplets(perimeter).
Input
perimeter: The required sum of all three sides.
Output
Return a list of triples in the specified order.
제약 조건
3 ≤ perimeter ≤ 100,000. Return an empty list if no triangle qualifies.
예시
입력:
perimeter = 30
예상 출력:
expected = [
[5, 12, 13]
]
입력:
long long perimeter = 30;
예상 출력:
std::vector<std::vector<long long>> expected = {
{5, 12, 13}
};
입력:
long perimeter = 30;
예상 출력:
long[][] expected = new long[][] {
new long[] {5, 12, 13}
};
설명
5 + 12 + 13 = 30 and 5² + 12² = 13². This is the only qualifying triple.
입력:
perimeter = 11
예상 출력:
expected = []
입력:
long long perimeter = 11;
예상 출력:
std::vector<std::vector<long long>> expected = {};
입력:
long perimeter = 11;
예상 출력:
long[][] expected = new long[][] {};
설명
No increasing positive integer right-triangle triple sums to 11.
접근 방법
먼저 스스로 풀어 본 뒤 힌트를 하나씩 열어 보세요.
힌트 1
A valid triple must satisfy both the Pythagorean equation and the perimeter sum.
힌트 2
Use the perimeter to limit the search instead of trying every ordering of sides.
힌트 3
Remember to keep only a < b < c, and skip duplicates from symmetric side choices.
예상 복잡도
O(n^2) time, O(1) extra space
참고 자료
이 문제는 CodeUtility에 맞게 수정되었습니다. 위의 설명과 제한 조건을 따르세요.