Skip to content

Commit 87c4d29

Browse files
add 611
1 parent 11c4cd3 commit 87c4d29

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Your ideas/fixes/algorithms are more than welcome!
2121
| # | Title | Solutions | Time | Space | Difficulty | Tag | Notes
2222
|-----|----------------|---------------|---------------|---------------|-------------|--------------|-----
2323
|617|[Merge Two Binary Trees](https://leetcode.com/problems/merge-two-binary-trees/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_617.java) | O(n) |O(h) | Easy | Tree, Recursion
24+
|611|[Valid Triangle Number](https://leetcode.com/problems/valid-triangle-number/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_611.java) | O(n^2logn) |O(logn) | Medium | Binary Search
2425
|609|[Find Duplicate File in System](https://leetcode.com/problems/find-duplicate-file-in-system/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_609.java) | O(n*x) (x is the average length of each string) |O(n*x) | Medium | HashMap
2526
|606|[Construct String from Binary Tree](https://leetcode.com/problems/construct-string-from-binary-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_606.java) | O(n) |O(n) | Easy | Tree, Recursion
2627
|605|[Can Place Flowers](https://leetcode.com/problems/can-place-flowers/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_605.java) | O(n) |O(1) | Easy | Array
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* 611. Valid Triangle Number
7+
*
8+
* Given an array consists of non-negative integers, your task is to count the number of triplets chosen from the array that can make triangles if we take them as side lengths of a triangle.
9+
10+
Example 1:
11+
Input: [2,2,3,4]
12+
Output: 3
13+
14+
Explanation:
15+
Valid combinations are:
16+
2,3,4 (using the first 2)
17+
2,3,4 (using the second 2)
18+
2,2,3
19+
20+
Note:
21+
The length of the given array won't exceed 1000.
22+
The integers in the given array are in the range of [0, 1000].
23+
*/
24+
public class _611 {
25+
/**Rule: among three sides, we need to find whether the longest of the three is smaller than the sum of the two shorter one.
26+
* If so, then these three could form a valid triangle.*/
27+
28+
public int triangleNumber(int[] nums) {
29+
if (nums == null || nums.length == 0) return 0;
30+
Arrays.sort(nums);
31+
int triplets = 0;
32+
for (int i = 2; i < nums.length; i++) {
33+
int left = 0, right = i-1;
34+
while (left < right) {
35+
if (nums[i] < nums[left] + nums[right]) {
36+
triplets += (right - left);
37+
right--;
38+
} else {
39+
left++;
40+
}
41+
}
42+
}
43+
return triplets;
44+
}
45+
46+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._611;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
/**
10+
* Created by stevesun on 6/11/17.
11+
*/
12+
public class _611Test {
13+
private static _611 test;
14+
private static int[] nums;
15+
16+
@BeforeClass
17+
public static void setup(){
18+
test = new _611();
19+
}
20+
21+
@Test
22+
public void test1(){
23+
nums = new int[]{2,2,3,4};
24+
assertEquals(3, test.triangleNumber(nums));
25+
}
26+
27+
@Test
28+
public void test2(){
29+
nums = new int[]{85,88,57,88,61,71,24,12,11,37,37,72,43,22,65,79,34,41,37,74,28,25,73,44,11,84,29,33,21,87,87,21,14,47,45,31,40,33,85,99,47,29,13,82,56,33,47,11,80,59,16,16,57,53,37,20,90,84,97,39,50,22,44,54,67,69,46,58,11,29,89,10,89,30,12,55,30,32,23,74,69,19,48,72,42,12,19,88,39,32,98,89,99,19,78,35,45,16,22,73,86,76,39,50,40,85,85,61,71,97,48,39,21,39,35,11,58,94,37,50,13,78,67,23,24,53,98,41,85,90,85,92,31,11,46,88,83,68,87,33,13,43,95,39,33,40,47,95,28,52,91,60,19,76,80,75,51,64,67,33,43,47,15,80,36,78,33,98,18,15,77,100,60,28,51,86,30,67,48,43,62,23,82,26,93,99,19,93,21,22,34,100,46,94,42,58,31,27,81,47,90,53,85,69,41,29,73,26,23,54,59,83,99,19,28,96,15,56,40,32,32,42,85,43,71,36,98,74,38,91,11,79,62,24,34,29,10,31,54,59,97,35,23,92,72,96,52,45,57,39,69,53,89,46,44,66,43,60,62,28,53,72,82,63,92,39,20,56,72,25,36,33,60,92,29,40,21,53,45,53,80,56,42,67,79,11,39,11,77,82,97,91,55,96,73,48,26,11,90,40,28,21,35,99,25,73,39,62,66,50,72,58,26,91,96,88,98,25,99,27,26,26,40,43,80,21,99,25,94,61,32,49,40,67,91,99,17,99,76,34,21,100,14,98,68,83,54,21,52,84,37,87,100,69,43,76,31,31,39,29,90,82,37,26,36,11,37,95,26,43,29,66,70,99,88,44,37,96,83,38,44,50,48,10,98,67,58,66,74,91,24,19,82,94,94,58,89,73,92,80,19,76,60,23,54,78,55,46,20,27,88,23,93,16,60,97,67,78,86,83,73,48,32,39,83,49,58,82,50,99,67,39,79,48,76,82,72,92,32,98,97,82,44,37,11,50,94,51,89,100,46,95,10,99,39,68,81,34,61,78,95,54,85,31,74,38,95,85,88,45,78,35,82,42,19,12,89,24,42,88,57,79,82,13,22,64,66,42,73,44,54,68,70,15,27,44,72,44,49,42,40,26,50,33,24,56,79,68,100,41,42,92,89,27,50,100,35,54,89,98,32,80,15,90,66,53,27,77,82,97,58,95,70,24,79,27,29,54,90,25,76,26,90,79,67,31,85,15,74,22,59,20,71,62,15,48,31,48,91,89,19,51,17,60,13,65,44,89,19,45,72,84,94,98,21,89,24,10,19,14,26,68,100,44,26,21,65,14,79,93,48,29,60,47,84,54,13,89,45,91,29,15,65,52,39,59,13,25,59,91,22,74,77,62,95,28,86,30,24,61,53,12,16,89,79,64,36,66,47,40,13,94,28,51,64,79,38,28,54,99,11,69,22,42,89,36,62,96,32,50,24,45,90,46,13,31,84,39,84,51,34,97,13,49,10,43,42,47,86,33,76,72,39,26,77,70,100,93,64,20,80,76,48,56,32,38,56,67,15,14,14,11,61,44,31,57,39,65,71,16,68,75,47,81,55,16,80,63,10,73,81,20,20,20,34,37,40,43,44,13,89,31,75,54,37,62,76,63,94,65,82,51,69,56,76,45,12,55,25,29,65,49,16,43,48,65,20,39,81,81,61,85,73,51,81,30,18,62,54,85,85,80,59,63,37,58,32,48,84,64,50,47,99,49,62,69,60,100,67,50,16,22,65,97,58,23,70,27,14,91,84,38,91,37,31,53,100,45,29,64,75,94,70,89,78,81,70,18,79,70,83,70,61,15,42,70,77,45,36,59,65,71,41,82,89,64,48,67,89,28,20,10,99,23,27,65,77,32,96,98,59,64,99,21,77,65,43,37,62,64,78,91,57,98,75,62,92,61,27,40,51,66,55,60,44,83,71,38,34,32,72,22,45,89,26,96,87,59,92,14,94,80,44,36,94,36,21,18,95,80,88,95,87,86,53,14,55,10,24,13,11,10,61,47,76,94,75,85,25,22,22,76,40,43,69,87,30,66,43,74,70,30,94,32,47,88,95,67,25,59,64,12,20,44,48,84,21,34,63,79,21,13,33,71,16,45,37,34,39,24,64,67,42,64,65,55,89,96,72,58,54,17,43,30,54,27,82,66,17,43,41,38,84,37,93,70,75,53,81,35,87,70,77,52,92,90,19,45,33,58,20,77,88,37,49,82,22,24,63,47};
30+
assertEquals(105489315, test.triangleNumber(nums));
31+
}
32+
33+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy