ปานกลาง 1 ข้อที่แก้แล้ว อัตราผ่าน 100.0%

Right-Triangle Product

Find the largest side product of an integer right triangle with a given perimeter.

สิ่งที่คุณจะได้เรียนรู้

  • Search integer right triangles by perimeter
  • Reduce three loops to two
  • Check the Pythagorean condition

โจทย์

ดูฉบับแปล

Find positive integers a < b < c with a² + b² = c² and a + b + c = perimeter. Return the largest product a × b × c among qualifying triangles, or −1 if there are none. This generalizes the fixed-perimeter source task and explicitly handles multiple answers.

Implement pythagorean_product(perimeter).

Input

perimeter: The required sum of the three sides.

Output

Return the largest product, or −1 when no triangle qualifies.

เงื่อนไข

3 ≤ perimeter ≤ 100,000. Products fit signed 64-bit integers.

ตัวอย่าง

ตัวอย่างที่ 1
อินพุต:
perimeter = 30

ผลลัพธ์ที่คาดหวัง:
expected = 780
อินพุต:
long long perimeter = 30;

ผลลัพธ์ที่คาดหวัง:
long long expected = 780;
อินพุต:
long perimeter = 30;

ผลลัพธ์ที่คาดหวัง:
long expected = 780;

คำอธิบาย

The triangle is (5, 12, 13): 25 + 144 = 169 and 5 + 12 + 13 = 30. Its product is 780.

ตัวอย่างที่ 2
อินพุต:
perimeter = 11

ผลลัพธ์ที่คาดหวัง:
expected = -1
อินพุต:
long long perimeter = 11;

ผลลัพธ์ที่คาดหวัง:
long long expected = -1;
อินพุต:
long perimeter = 11;

ผลลัพธ์ที่คาดหวัง:
long expected = -1;

คำอธิบาย

No increasing positive integer right-triangle sides sum to 11.

แนวทางการแก้

ลองทำเองก่อน แล้วค่อยเปิดคำใบ้ทีละข้อ

คำใบ้ 1

Only certain perimeters can form a right triangle, so first narrow the search using the side-sum constraint.

คำใบ้ 2

For each possible shortest side, derive the other two sides from the perimeter and check the right-triangle condition.

คำใบ้ 3

More than one triangle may fit; keep the largest product and return no-solution when nothing valid appears.

ความซับซ้อนที่คาดหวัง

O(perimeter^2) time, O(1) extra space

แหล่งอ้างอิง

โจทย์นี้ปรับให้เหมาะกับ CodeUtility กรุณาทำตามคำชี้แจงและข้อจำกัดด้านบน

คำตอบที่ผ่านดีที่สุดของแต่ละคน คำตอบที่เร็วกว่า ใช้หน่วยความจำน้อยกว่า และได้คะแนนดีกว่าจะอยู่ด้านบน

# นักโค้ด ภาษา เวลา หน่วยความจำ คะแนนจาก AI คำตอบ
1 ННикита Python 957 ms - 4/5 ดูโค้ด

เรียนรู้จากคนอื่น

แนวทางจากชุมชน

สปอยล์: มีคำตอบเต็ม 1 ข้อด้านล่าง ลองแก้ด้วยตัวเองก่อน คุณยังสามารถดูโค้ดของนักพัฒนาคนอื่นและรีวิว AI ได้ เข้าสู่ระบบและแก้โจทย์เพื่อร่วมพูดคุย