Skip to content

Commit 613bc46

Browse files
committed
valid triangle done
best meeting point brute force approach done
1 parent 5d20974 commit 613bc46

File tree

2 files changed

+218
-7
lines changed

2 files changed

+218
-7
lines changed

src/main/java/com/leetcode/arrays/ValidTriangleNumber.java

+77-7
Original file line numberDiff line numberDiff line change
@@ -21,27 +21,97 @@
2121
* 2,2,3
2222
* <p>
2323
* Note:
24-
* The length of the given array won't exceed 1000.
25-
* The integers in the given array are in the range of [0, 1000].
24+
* - The length of the given array won't exceed 1000.
25+
* - The integers in the given array are in the range of [0, 1000].
26+
* - Triangle Property: Sum of any 2 sides must be greater than the 3rd side.
2627
*
2728
* @author rampatra
2829
* @since 2019-08-07
2930
*/
3031
public class ValidTriangleNumber {
3132

32-
public static int triangleNumber(int[] nums) {
33-
int l = 0;
34-
int r = nums.length - 1;
33+
/**
34+
* Time complexity : O(n^2 log n). In worst case, the inner loop will take n log n (binary search applied n times).
35+
* Space complexity : O(log n). Sorting takes O(log n) space.
36+
* Runtime: <a href="https://leetcode.com/submissions/detail/250225175/">13 ms</a>.
37+
*
38+
* @param nums
39+
* @return
40+
*/
41+
public static int triangleNumberUsingBinarySearch(int[] nums) {
3542
int noOfTriangles = 0;
3643

3744
Arrays.sort(nums);
3845

39-
// todo
46+
for (int i = 0; i < nums.length - 2; i++) {
47+
int k = i + 2;
48+
for (int j = i + 1; j < nums.length - 1; j++) {
49+
k = binarySearch(nums, k, nums.length - 1, nums[i] + nums[j]);
50+
if (k - j - 1 > 0) {
51+
noOfTriangles += k - j - 1;
52+
}
53+
}
54+
}
55+
56+
return noOfTriangles;
57+
}
58+
59+
private static int binarySearch(int[] nums, int low, int high, int num) {
60+
while (low <= high) {
61+
int mid = (low + high) / 2;
62+
if (nums[mid] < num) {
63+
low = mid + 1;
64+
} else {
65+
high = mid - 1;
66+
}
67+
}
68+
69+
return low;
70+
}
71+
72+
/**
73+
* The concept is simple. For each pair (i,j), find the value of k such that nums[i] + nums[j] > nums[k] (as per
74+
* triangle property). Once we find k then we can form k- j - 1 triangles.
75+
*
76+
* Time Complexity: O(n^2) Loop of k and j will be executed O(n^2) times in total, because, we do
77+
* not reinitialize the value of k for a new value of j chosen(for the same i). Thus, the complexity
78+
* will be O(n^2 + n^2) = O(n^2).
79+
* Space Complexity: O(log n). Sorting takes O(log n) space.
80+
* Runtime: <a href="https://leetcode.com/submissions/detail/250239099/">5 ms</a>.
81+
*
82+
* @param nums
83+
* @return
84+
*/
85+
public static int triangleNumber(int[] nums) {
86+
int noOfTriangles = 0;
87+
Arrays.sort(nums);
88+
89+
for (int i = 0; i < nums.length - 2; i++) {
90+
int k = i + 2;
91+
for (int j = i + 1; j < nums.length - 1; j++) {
92+
while (k < nums.length && nums[i] + nums[j] > nums[k]) {
93+
k++;
94+
}
95+
if (k - j - 1 > 0) {
96+
noOfTriangles += k - j - 1;
97+
}
98+
}
99+
}
40100

41101
return noOfTriangles;
42102
}
43103

44104
public static void main(String[] args) {
105+
assertEquals(0, triangleNumberUsingBinarySearch(new int[]{}));
106+
assertEquals(0, triangleNumberUsingBinarySearch(new int[]{1}));
107+
assertEquals(3, triangleNumberUsingBinarySearch(new int[]{2, 2, 3, 4}));
108+
assertEquals(0, triangleNumberUsingBinarySearch(new int[]{0, 1, 0, 1}));
109+
assertEquals(7, triangleNumberUsingBinarySearch(new int[]{1, 2, 3, 4, 5, 6}));
110+
111+
assertEquals(0, triangleNumber(new int[]{}));
112+
assertEquals(0, triangleNumber(new int[]{1}));
45113
assertEquals(3, triangleNumber(new int[]{2, 2, 3, 4}));
114+
assertEquals(0, triangleNumber(new int[]{0, 1, 0, 1}));
115+
assertEquals(7, triangleNumber(new int[]{1, 2, 3, 4, 5, 6}));
46116
}
47-
}
117+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
package com.leetcode.math;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
7+
import static org.junit.jupiter.api.Assertions.assertEquals;
8+
9+
/**
10+
* Level: Hard
11+
* Link: https://leetcode.com/problems/best-meeting-point/
12+
* Description:
13+
* A group of two or more people wants to meet and minimize the total travel distance. You are given a 2D grid
14+
* of values 0 or 1, where each 1 marks the home of someone in the group. The distance is calculated using
15+
* Manhattan Distance, where distance(p1, p2) = |p2.x - p1.x| + |p2.y - p1.y|.
16+
*
17+
* Example:
18+
*
19+
* Input:
20+
*
21+
* 1 - 0 - 0 - 0 - 1
22+
* | | | | |
23+
* 0 - 0 - 0 - 0 - 0
24+
* | | | | |
25+
* 0 - 0 - 1 - 0 - 0
26+
*
27+
* Output: 6
28+
*
29+
* Explanation: Given three people living at (0,0), (0,4), and (2,2):
30+
* The point (0,2) is an ideal meeting point, as the total travel distance
31+
* of 2+2+2=6 is minimal. So, return 6.
32+
*
33+
* @author rampatra
34+
* @since 2019-08-07
35+
*/
36+
public class BestMeetingPoint {
37+
38+
/**
39+
* Time Complexity: O(k * i * j)
40+
* Space Complexity: O(1)
41+
* where,
42+
* k = no of homes
43+
* i = rows in grid
44+
* j = columns in grid
45+
*
46+
* So, if i = j = k then you can see that it has a O(n^3) time complexity.
47+
*
48+
* @param grid
49+
* @return
50+
*/
51+
public static int minTotalDistanceBrutForce(int[][] grid) {
52+
int minDistance = Integer.MAX_VALUE;
53+
List<List<Integer>> homeCoordinates = new ArrayList<>();
54+
55+
for (int i = 0; i < grid.length; i++) {
56+
for (int j = 0; j < grid[0].length; j++) {
57+
if (grid[i][j] == 1) {
58+
homeCoordinates.add(Arrays.asList(i, j));
59+
}
60+
}
61+
}
62+
63+
for (int i = 0; i < grid.length; i++) {
64+
for (int j = 0; j < grid[0].length; j++) {
65+
int distance = 0;
66+
for (int k = 0; k < homeCoordinates.size(); k++) {
67+
distance += Math.abs(homeCoordinates.get(k).get(0) - i) + Math.abs(homeCoordinates.get(k).get(1) - j);
68+
}
69+
minDistance = Math.min(minDistance, distance);
70+
}
71+
}
72+
73+
return minDistance;
74+
}
75+
76+
public static int minTotalDistance(int[][] grid) {
77+
return -1; // todo
78+
}
79+
80+
public static void main(String[] args) {
81+
assertEquals(6, minTotalDistanceBrutForce(new int[][]{
82+
{1,0,0,0,1},
83+
{0,0,0,0,0},
84+
{0,0,1,0,0}
85+
}));
86+
87+
assertEquals(4, minTotalDistanceBrutForce(new int[][]{
88+
{1,0,0,0,1},
89+
{0,0,0,0,0},
90+
{0,0,0,0,0}
91+
}));
92+
93+
assertEquals(1, minTotalDistanceBrutForce(new int[][]{
94+
{1,1,0,0,0},
95+
{0,0,0,0,0},
96+
{0,0,0,0,0}
97+
}));
98+
99+
assertEquals(0, minTotalDistanceBrutForce(new int[][]{
100+
{1,0,0,0,0},
101+
{0,0,0,0,0},
102+
{0,0,0,0,0}
103+
}));
104+
105+
assertEquals(0, minTotalDistanceBrutForce(new int[][]{
106+
{0,0,0,0,0},
107+
{0,0,0,0,0},
108+
{0,0,0,0,0}
109+
}));
110+
111+
assertEquals(6, minTotalDistance(new int[][]{
112+
{1,0,0,0,1},
113+
{0,0,0,0,0},
114+
{0,0,1,0,0}
115+
}));
116+
117+
assertEquals(4, minTotalDistance(new int[][]{
118+
{1,0,0,0,1},
119+
{0,0,0,0,0},
120+
{0,0,0,0,0}
121+
}));
122+
123+
assertEquals(1, minTotalDistance(new int[][]{
124+
{1,1,0,0,0},
125+
{0,0,0,0,0},
126+
{0,0,0,0,0}
127+
}));
128+
129+
assertEquals(0, minTotalDistance(new int[][]{
130+
{1,0,0,0,0},
131+
{0,0,0,0,0},
132+
{0,0,0,0,0}
133+
}));
134+
135+
assertEquals(0, minTotalDistance(new int[][]{
136+
{0,0,0,0,0},
137+
{0,0,0,0,0},
138+
{0,0,0,0,0}
139+
}));
140+
}
141+
}

0 commit comments

Comments
 (0)