Skip to content

Commit 8b0a5b9

Browse files
chore: add LeetCode daily solution
1 parent c708015 commit 8b0a5b9

File tree

5 files changed

+69
-0
lines changed

5 files changed

+69
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Count Square Sum Triples (Easy)
2+
3+
**Problem ID:** 1925
4+
**Date:** 2025-12-08
5+
**Link:** https://leetcode.com/problems/count-square-sum-triples/
6+
7+
## Approach
8+
9+
To solve the problem of counting square sum triples (a, b, c) such that \( a^2 + b^2 = c^2 \) with the constraints \( 1 \leq a, b, c \leq n \), we can follow a systematic approach:
10+
11+
### Main Idea
12+
The core idea is to identify all valid triples (a, b, c) that satisfy the Pythagorean theorem within the given range. We can achieve this by iterating through possible values of c and checking for pairs (a, b) that fulfill the equation.
13+
14+
### Steps
15+
1. **Iterate Over c**: Loop through all integers from 1 to n for the variable c.
16+
2. **Calculate c²**: For each c, compute \( c^2 \).
17+
3. **Find Pairs (a, b)**: For each possible integer a from 1 to c (since a and b can be at most c), calculate \( b^2 \) as \( c^2 - a^2 \).
18+
4. **Check Validity**: If \( b^2 \) is a perfect square, and \( b \) (which is the square root of \( b^2 \)) is also less than or equal to n, then we have found a valid pair (a, b).
19+
5. **Count Unique Triples**: Since the order of (a, b) matters, count both (a, b, c) and (b, a, c) if a ≠ b.
20+
21+
### Data Structures
22+
- **Integer Variables**: To store the values of a, b, and c.
23+
- **Counter**: An integer to keep track of the number of valid triples found.
24+
25+
### Complexity
26+
- **Time Complexity**: The outer loop runs n times (for c), and the inner loop runs at most c times (for a). Thus, the overall complexity is approximately \( O(n^2) \), which is efficient given the constraint \( n \leq 250 \).
27+
- **Space Complexity**: O(1) since we are only using a fixed number of variables for counting and calculations.
28+
29+
### Conclusion
30+
This approach efficiently counts the valid square sum triples by leveraging the properties of Pythagorean triples and iterating through potential values systematically. The method ensures that we do not miss any combinations and adheres to the constraints provided.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public int countTriples(int n) {
3+
int count = 0;
4+
for (int a = 1; a <= n; a++) {
5+
for (int b = a; b <= n; b++) {
6+
int c2 = a * a + b * b;
7+
int c = (int) Math.sqrt(c2);
8+
if (c * c == c2 && c <= n) {
9+
count += (a == b) ? 1 : 2; // (a, b, c) and (b, a, c) are different if a != b
10+
}
11+
}
12+
}
13+
return count;
14+
}
15+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
var countTriples = function(n) {
2+
let count = 0;
3+
for (let a = 1; a <= n; a++) {
4+
for (let b = a; b <= n; b++) {
5+
const c2 = a * a + b * b;
6+
const c = Math.sqrt(c2);
7+
if (c % 1 === 0 && c <= n) {
8+
count += (a === b) ? 1 : 2; // (a, b, c) and (b, a, c) are distinct if a != b
9+
}
10+
}
11+
}
12+
return count;
13+
};
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution:
2+
def countTriples(self, n: int) -> int:
3+
count = 0
4+
for a in range(1, n + 1):
5+
for b in range(a, n + 1):
6+
c_squared = a * a + b * b
7+
c = int(c_squared**0.5)
8+
if c * c == c_squared and c <= n:
9+
count += 1 if a == b else 2
10+
return count

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,3 +298,4 @@ Through completing the Blind 75 and NeetCode 150, you will have mastered:
298298
- 2025-12-05 — [Count Partitions with Even Sum Difference](https://leetcode.com/problems/count-partitions-with-even-sum-difference/) (Easy) → `Easy/2025-12-05-3432-Count-Partitions-with-Even-Sum-Difference`
299299
- 2025-12-06 — [Count Partitions With Max-Min Difference at Most K](https://leetcode.com/problems/count-partitions-with-max-min-difference-at-most-k/) (Medium) → `Medium/2025-12-06-3578-Count-Partitions-With-Max-Min-Difference-at-Most-K`
300300
- 2025-12-07 — [Count Odd Numbers in an Interval Range](https://leetcode.com/problems/count-odd-numbers-in-an-interval-range/) (Easy) → `Easy/2025-12-07-1523-Count-Odd-Numbers-in-an-Interval-Range`
301+
- 2025-12-08 — [Count Square Sum Triples](https://leetcode.com/problems/count-square-sum-triples/) (Easy) → `Easy/2025-12-08-1925-Count-Square-Sum-Triples`

0 commit comments

Comments
 (0)