Trains the technique from
LeetCode 611Valid Triangle NumberThis is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.
Same function shape, different story and different numbers.
A workshop has a pile of steel struts; nums[i] is the length of the strut in slot i, and a length of 0 marks a slot holding an offcut that is too short to use. The struts arrive in no particular order.
Three struts can be bolted into a rigid triangular brace exactly when each of their lengths is strictly less than the sum of the other two, which is what gives the brace a positive area. A flat, collapsed arrangement does not count.
Count the triples of slots (i, j, k) with i < j < k whose three struts form a rigid brace. Struts of equal length in different slots are different struts, so each triple of slots is counted separately.
Example 1
All four triples of slots qualify: 6, 3, 4 has every length under the sum of the other two, and so do 6, 3, 6 and 6, 4, 6 and 3, 4, 6.
Example 2
The only triple has 2 + 3 equal to 5, so the arrangement is flat rather than rigid and nothing is counted.
Example 3
Slot 0 holds an offcut of length 0, and 0 + 1 does not exceed 1, so the only triple fails.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def triangle_number(nums: list[int]) -> int:public int triangleNumber(int[] nums)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.