Skip to content

Commit 2071e84

Browse files
chore: add LeetCode daily solution
1 parent db7b844 commit 2071e84

File tree

5 files changed

+85
-0
lines changed

5 files changed

+85
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Valid Triangle Number (Medium)
2+
3+
**Problem ID:** 611
4+
**Date:** 2025-09-26
5+
**Link:** https://leetcode.com/problems/valid-triangle-number/
6+
7+
## Approach
8+
9+
To solve the "Valid Triangle Number" problem, we can utilize a combination of sorting and the two-pointer technique. The main idea is based on the triangle inequality theorem, which states that for three sides to form a triangle, the sum of the lengths of any two sides must be greater than the length of the third side.
10+
11+
### Approach:
12+
13+
1. **Sorting the Array**: Start by sorting the input array `nums`. This allows us to easily apply the triangle inequality conditions since for any triplet `(a, b, c)` where `a <= b <= c`, it suffices to check if `a + b > c`.
14+
15+
2. **Iterating Through Triplets**: Use a loop to fix the largest side of the triangle (let’s call it `c`) starting from the third element and moving towards the end of the sorted array. For each fixed `c`, we will find pairs `(a, b)` such that `a + b > c`.
16+
17+
3. **Two-Pointer Technique**:
18+
- Initialize two pointers: one (`left`) starting from the beginning of the array and the other (`right`) just before the current `c`.
19+
- Check if the sum of the elements at these two pointers (`nums[left] + nums[right]`) is greater than `nums[c]`. If it is, then all pairs from `left` to `right` with the current `right` are valid, since the array is sorted and any element between `left` and `right` will also satisfy the condition. Count these pairs and decrement `right`.
20+
- If the sum is not greater, increment `left` to try a larger sum.
21+
22+
4. **Counting Valid Triplets**: Keep a cumulative count of valid triplet combinations as you iterate through the array.
23+
24+
### Data Structures:
25+
- A sorted array to facilitate the two-pointer approach.
26+
- Simple integer counters to keep track of valid triplet counts.
27+
28+
### Complexity:
29+
- **Time Complexity**: The sorting step takes \(O(n \log n)\), and the two-pointer traversal for each element takes \(O(n)\). Thus, the overall time complexity is \(O(n^2)\).
30+
- **Space Complexity**: The space complexity is \(O(1)\) if we ignore the input storage, as we are using only a constant amount of extra space.
31+
32+
This approach efficiently counts the number of valid triangle triplets in the array, leveraging sorting and the two-pointer technique to minimize unnecessary checks.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public int triangleNumber(int[] nums) {
3+
int count = 0;
4+
int n = nums.length;
5+
Arrays.sort(nums);
6+
7+
for (int i = 0; i < n - 2; i++) {
8+
int k = i + 2;
9+
for (int j = i + 1; j < n; j++) {
10+
while (k < n && nums[i] + nums[j] > nums[k]) {
11+
k++;
12+
}
13+
count += k - j - 1;
14+
}
15+
}
16+
17+
return count;
18+
}
19+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
var triangleNumber = function(nums) {
2+
nums.sort((a, b) => a - b);
3+
let count = 0;
4+
const n = nums.length;
5+
6+
for (let i = 0; i < n - 2; i++) {
7+
let k = i + 2;
8+
for (let j = i + 1; j < n; j++) {
9+
while (k < n && nums[i] + nums[j] > nums[k]) {
10+
k++;
11+
}
12+
count += k - j - 1;
13+
}
14+
}
15+
16+
return count;
17+
};
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution:
2+
def triangleNumber(self, nums: List[int]) -> int:
3+
nums.sort()
4+
count = 0
5+
n = len(nums)
6+
7+
for i in range(n - 1, 1, -1):
8+
left, right = 0, i - 1
9+
while left < right:
10+
if nums[left] + nums[right] > nums[i]:
11+
count += right - left
12+
right -= 1
13+
else:
14+
left += 1
15+
16+
return count

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,3 +158,4 @@ Each problem includes:
158158
- 2025-09-23 — [Compare Version Numbers](https://leetcode.com/problems/compare-version-numbers/) (Medium) → `Medium/2025-09-23-165-Compare-Version-Numbers`
159159
- 2025-09-24 — [Fraction to Recurring Decimal](https://leetcode.com/problems/fraction-to-recurring-decimal/) (Medium) → `Medium/2025-09-24-166-Fraction-to-Recurring-Decimal`
160160
- 2025-09-25 — [Triangle](https://leetcode.com/problems/triangle/) (Medium) → `Medium/2025-09-25-120-Triangle`
161+
- 2025-09-26 — [Valid Triangle Number](https://leetcode.com/problems/valid-triangle-number/) (Medium) → `Medium/2025-09-26-611-Valid-Triangle-Number`

0 commit comments

Comments
 (0)