챌린지 목록으로 돌아가기
보통 0명 해결 정답률 0%

Find Right-Triangle Triples

List all integer right triangles with a specified perimeter.

배우게 될 내용

  • List all integer right triangles for a perimeter
  • Avoid duplicates by ordering sides
  • Prune the search with bounds

문제

번역본 보기

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.

예시

예시 1
입력:
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.

예시 2
입력:
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에 맞게 수정되었습니다. 위의 설명과 제한 조건을 따르세요.

다른 사람의 풀이 보기

커뮤니티 솔루션

아직 공개된 풀이가 없습니다. 문제를 풀고 첫 풀이를 공유하세요.