Skip to content

Commit a55ea47

Browse files
author
Sherali Obidov
committed
bfs way
1 parent c6370c7 commit a55ea47

2 files changed

Lines changed: 115 additions & 35 deletions

File tree

‎src/AmazonDemo/Sample.java‎

Lines changed: 115 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,52 +7,67 @@
77
*/
88
public class Sample {
99

10-
List<List<Integer>> nearestXsteakHouses(int totalSteakhouses, List<List<Integer>> allLocations, int numSteakhouses) {
11-
if (numSteakhouses <= 0) {
10+
List<List<Integer>> closestXdestinations(int numDestinations, List<List<Integer>> allLocations, int numDeliveries) {
11+
if (numDeliveries <= 0) {
1212
return new ArrayList<>();
1313
}
14-
if (totalSteakhouses <= numSteakhouses) {
14+
if (numDestinations <= numDeliveries) {
1515
return allLocations;
1616
}
17-
List<List<Integer>> result = new ArrayList<>();
18-
dijkstra(allLocations, numSteakhouses, result);
19-
return result;
17+
return findNearestOnes(allLocations, numDeliveries);
2018
}
2119

22-
private void dijkstra(List<List<Integer>> allLocations, int numSteakhouses, List<List<Integer>> result) {
23-
int x = 0;
24-
int y = 0;
25-
List<Integer> customer = new ArrayList<>();
26-
customer.add(x);
27-
customer.add(y);
28-
Map<Double, List<List<Integer>>> dis = new TreeMap<>();
20+
/**
21+
* first calculates distances from starting point to all other
22+
* positions and returns nearest k positions.
23+
*/
24+
private List<List<Integer>> findNearestOnes(List<List<Integer>> allLocations, int numDeliveries) {
25+
// start position of truck hardcoded here
26+
List<Integer> startPosition = new ArrayList<>();
27+
startPosition.add(0);
28+
startPosition.add(0);
29+
List<List<Integer>> result = new ArrayList<>();
30+
31+
Map<Double, List<List<Integer>>> distances = new TreeMap<>();
2932
for (List<Integer> location : allLocations) {
30-
double distance = calculateDist(location, customer);
31-
List<List<Integer>> value = dis.getOrDefault(distance, new ArrayList<>());
33+
double distance = calculateDist(location, startPosition);
34+
List<List<Integer>> value = distances.getOrDefault(distance, new ArrayList<>());
3235
value.add(location);
33-
dis.put(distance, value);
36+
distances.put(distance, value);
3437
}
35-
for (Double key : dis.keySet()) {
36-
if (numSteakhouses >= dis.get(key).size()) {
37-
result.addAll(dis.get(key));
38-
numSteakhouses -= dis.get(key).size();
38+
for (Double key : distances.keySet()) {
39+
if (numDeliveries >= distances.get(key).size()) {
40+
result.addAll(distances.get(key));
41+
numDeliveries -= distances.get(key).size();
3942
} else {
40-
for (int i = 0; i < numSteakhouses; i++) {
41-
result.add(dis.get(key).get(i));
43+
for (int i = 0; i < numDeliveries; i++) {
44+
result.add(distances.get(key).get(i));
4245
}
43-
return;
46+
return result;
4447
}
45-
if (numSteakhouses <= 0)
46-
return;
48+
if (numDeliveries <= 0)
49+
return result;
4750
}
4851

52+
return result;
4953
}
5054

55+
/**
56+
* calculates distance between two points in plain
57+
* */
5158
private Double calculateDist(List<Integer> location, List<Integer> customer) {
5259
return Math.hypot(customer.get(0) - location.get(0), customer.get(1) - location.get(1));
5360
}
5461

55-
int minimumDistance(int numRows, int numColumns, List<List<Integer>> area) {
62+
public static void main(String[] args) {
63+
List<List<Integer>> lists = new ArrayList<>();
64+
lists.add(Arrays.asList(1, 0, 0));
65+
lists.add(Arrays.asList(1, 0, 0));
66+
lists.add(Arrays.asList(1, 9, 1));
67+
System.out.println(new Sample().minimumDistance2(0, 0, lists));
68+
}
69+
70+
int minimumDistance2(int numRows, int numColumns, List<List<Integer>> area) {
5671
if (area == null || area.size() == 0)
5772
return 0;
5873
int[] min = new int[] { Integer.MAX_VALUE };
@@ -67,16 +82,86 @@ private void route(List<List<Integer>> area, int i, int j, int[] min, int distan
6782
min[0] = Math.min(min[0], distance);
6883
return;
6984
}
70-
if (area.get(i).get(j) == 0)
85+
if (area.get(i).get(j) <= 0)
7186
return;
72-
if (area.get(i).get(j) == -1)
73-
return;
74-
7587
int val = area.get(i).get(j);
88+
area.get(i).set(j, -1);
7689
route(area, i + 1, j, min, distance + 1);
7790
route(area, i - 1, j, min, distance + 1);
7891
route(area, i, j + 1, min, distance + 1);
7992
route(area, i, j - 1, min, distance + 1);
8093
area.get(i).set(j, val);
8194
}
95+
96+
int minimumDistance(int numRows, int numColumns, List<List<Integer>> area) {
97+
if (area == null || area.size() == 0)
98+
return 0;
99+
return bfs(area);
100+
}
101+
102+
/**
103+
* Implementation of breadth first search. It terminates early in case target destination is found.
104+
*
105+
* @returns -1 if we cant reach to target point from start position.
106+
*/
107+
private int bfs(List<List<Integer>> area) {
108+
boolean[][] visited = new boolean[area.size()][area.get(0).size()];
109+
int[][] distance = new int[area.size()][area.get(0).size()];
110+
Queue<Pair> q = new LinkedList<>();
111+
q.add(new Pair(0, 0)); // start position hardcoded
112+
visited[0][0] = true;
113+
while (!q.isEmpty()) {
114+
Pair current = q.remove();
115+
if (area.get(current.i).get(current.j) == 9) {// target found
116+
return distance[current.i][current.j];
117+
}
118+
List<Pair> neighbors = getNeighbors(current, area);
119+
for (Pair nei : neighbors) {
120+
if (!visited[nei.i][nei.j]) {
121+
q.add(nei);
122+
visited[nei.i][nei.j] = true;
123+
distance[nei.i][nei.j] = distance[current.i][current.j] + 1; // increment distance
124+
}
125+
}
126+
}
127+
return -1;
128+
}
129+
130+
/**
131+
* helper method to find neighbors
132+
*
133+
* @returns List of Pair available neighbor cells
134+
*/
135+
private List<Pair> getNeighbors(Pair current, List<List<Integer>> area) {
136+
List<Pair> list = new ArrayList<>();
137+
int i = current.i;
138+
int j = current.j;
139+
int n = area.size();
140+
int m = area.get(0).size();
141+
if (j > 0 && area.get(i).get(j - 1) != 0) {
142+
list.add(new Pair(i, j - 1));
143+
}
144+
if (j < m - 1 && area.get(i).get(j + 1) != 0) {
145+
list.add(new Pair(i, j + 1));
146+
}
147+
if (i > 0 && area.get(i - 1).get(j) != 0) {
148+
list.add(new Pair(i - 1, j));
149+
}
150+
if (i < n - 1 && area.get(i + 1).get(j) != 0) {
151+
list.add(new Pair(i + 1, j));
152+
}
153+
return list;
154+
}
155+
156+
/**
157+
* Helper data class to hold coordinates of locations
158+
*/
159+
private class Pair {
160+
int i, j;
161+
162+
public Pair(int i, int j) {
163+
this.i = i;
164+
this.j = j;
165+
}
166+
}
82167
}

‎src/problems/hard/FreqStack.java‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@
22

33
import java.util.*;
44

5-
/**
6-
*
7-
*/
8-
95
public class FreqStack {
106
PriorityQueue<Pair> q;
117
Map<Integer, Pair> map;
@@ -53,7 +49,6 @@ public boolean equals(Object o) {
5349
return false;
5450

5551
Pair pair = (Pair) o;
56-
5752
return val == pair.val;
5853
}
5954

0 commit comments

Comments
 (0)