Skip to content

Commit 15dc44a

Browse files
add two random questions
1 parent 71aa0c1 commit 15dc44a

File tree

1 file changed

+120
-1
lines changed

1 file changed

+120
-1
lines changed

src/main/java/com/fishercoder/solutions/_99999RandomQuestions.java

Lines changed: 120 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.fishercoder.solutions;
22

3+
import com.fishercoder.common.utils.CommonUtils;
34
import com.google.gson.JsonArray;
45
import com.google.gson.JsonElement;
56
import com.google.gson.JsonObject;
@@ -45,10 +46,128 @@ public static void main(String... args) {
4546
//List<Integer> result = cellCompete(new int[]{1, 1, 1, 0, 1, 1, 1, 1}, 2);
4647
//CommonUtils.printList(result);
4748

48-
System.out.println(generalizedGCD(5, new int[] {2, 4, 6, 8, 10}));
49+
//System.out.println(generalizedGCD(5, new int[] {2, 4, 6, 8, 10}));
50+
51+
52+
//List<List<Integer>> allLocations = new ArrayList<>();
53+
//allLocations.add(List.of(1, 2));
54+
//allLocations.add(List.of(3, 4));
55+
//allLocations.add(List.of(1, -1));
56+
//allLocations.add(List.of(-1, 1));
57+
//List<List<Integer>> result = nearestVegetarianRestaurant(3, allLocations, 2);
58+
//CommonUtils.printListList(result);
59+
60+
//List<List<Integer>> foregroundAppList = new ArrayList<>();
61+
//foregroundAppList.add(List.of(1, 2));
62+
//foregroundAppList.add(List.of(2, 4));
63+
//foregroundAppList.add(List.of(3, 6));
64+
//List<List<Integer>> backgroundAppList = new ArrayList<>();
65+
//backgroundAppList.add(List.of(1, 2));
66+
//List<List<Integer>> result = optimalUtilization(7, foregroundAppList, backgroundAppList);
67+
//CommonUtils.printListList(result);
68+
69+
List<List<Integer>> foregroundAppList = new ArrayList<>();
70+
foregroundAppList.add(List.of(1, 3));
71+
foregroundAppList.add(List.of(2, 5));
72+
foregroundAppList.add(List.of(3, 7));
73+
foregroundAppList.add(List.of(4, 10));
74+
List<List<Integer>> backgroundAppList = new ArrayList<>();
75+
backgroundAppList.add(List.of(1, 2));
76+
backgroundAppList.add(List.of(2, 3));
77+
backgroundAppList.add(List.of(3, 4));
78+
backgroundAppList.add(List.of(4, 5));
79+
List<List<Integer>> result = optimalUtilization(10, foregroundAppList, backgroundAppList);
80+
CommonUtils.printListList(result);
81+
}
82+
83+
/**An engineer works on a system that divides application to a mixed cluster of computing devices. Each application is identified by an Integer ID, requires
84+
* a fixed non-zero amount of memory to execute, and is defined to be either a foreground or background application. IDs are guaranteed to be unique within their own application type, but not across types.
85+
*
86+
* Each device should be assigned two applications at once, one foreground application and one background application. Devices have limited amounts of memory and cannot execute applications that require more memory
87+
* than the available memory. The goal of the system is to maximize the total utilization of the memory of a given device.
88+
* A foreground/background application pair is considered to be optimal if there does not exist another pair that uses more memory than this pair, and also has a total less than or equal to the total memory of the device.
89+
* For example, if the device has a total of 10MB memory, a foreground/background pair using a sum total of 9MB memory would be optimal if there does not exist a pair that uses a sum total of 10 MB, but would not
90+
* be optimal if such a pair did exist.
91+
*
92+
* Write an algorithm to help this engineer find the sets of foreground/background app pairs that optimally utilize the given device for a given list of foreground applications and a given list of background applications.
93+
*
94+
* Example 1:
95+
* deviceCapacity: 7
96+
* foregroundAppList: [[1,2], [2,4],[3,6]]
97+
* backgroundAppList: [[1,2]]
98+
*
99+
* Output:
100+
* [[2,1]]
101+
*
102+
* Explanation:
103+
* There are only three combinations: [1,1,][2,1] and [3,1], which use of a total of 4, 6 and 8 MB of memory, since 6 is the largest use that does not exceed 7, [2,1] becomes the only optimal pair.*/
104+
static List<List<Integer>> optimalUtilization(int deviceCapacity,
105+
List<List<Integer>> foregroundAppList, List<List<Integer>> backgroundAppList) {
106+
TreeMap<Integer, List<List<Integer>>> memorySumToAppMap =
107+
new TreeMap<>(Collections.reverseOrder());
108+
for (List<Integer> foregroundApp : foregroundAppList) {
109+
for (List<Integer> backgroundApp : backgroundAppList) {
110+
int memorySum = foregroundApp.get(1) + backgroundApp.get(1);
111+
if (!memorySumToAppMap.containsKey(memorySum)) {
112+
memorySumToAppMap.put(memorySum, new ArrayList<>());
113+
}
114+
List<Integer> appPair = new ArrayList<>();
115+
appPair.add(foregroundApp.get(0));
116+
appPair.add(backgroundApp.get(0));
117+
memorySumToAppMap.get(memorySum).add(appPair);
118+
}
119+
}
120+
List<List<Integer>> result = new ArrayList<>();
121+
for (int memorySum : memorySumToAppMap.keySet()) {
122+
if (memorySum > deviceCapacity) {
123+
continue;
124+
} else {
125+
result = memorySumToAppMap.get(memorySum);
126+
break;
127+
}
128+
}
129+
return result;
130+
}
49131

132+
/**Build a robot to help return nearest X restaurants given an array representing the locations of N vegetarian restaurants.
133+
*
134+
* Note:
135+
* The customer begins at location: [0, 0]
136+
* numRestaurants < totalRestaurants
137+
* the distance from the customer's current location to a recommended veg restaurant location(x, y) is the sqare root of x2 + y2.
138+
* If there are ties, then return any of the locations as long as you satisfy returning X nearby veg restaurants.
139+
* */
140+
static List<List<Integer>> nearestVegetarianRestaurant(int totalRestaurants,
141+
List<List<Integer>> allLocations, int numRestaurants) {
142+
TreeMap<Double, List<List<Integer>>> treeMap = new TreeMap<>();
143+
for (List<Integer> location : allLocations) {
144+
double distance =
145+
Math.sqrt(Math.pow(location.get(0), 2) + Math.pow(location.get(1), 2));
146+
if (!treeMap.containsKey(distance)) {
147+
treeMap.put(distance, new ArrayList<>());
148+
}
149+
treeMap.get(distance).add(location);
150+
}
151+
List<List<Integer>> result = new ArrayList<>();
152+
for (Double distance : treeMap.keySet()) {
153+
if (numRestaurants > 0) {
154+
List<List<Integer>> locations = treeMap.get(distance);
155+
for (List<Integer> location : locations) {
156+
if (numRestaurants <= 0) {
157+
break;
158+
} else {
159+
result.add(location);
160+
numRestaurants--;
161+
}
162+
}
163+
} else {
164+
break;
165+
}
166+
}
167+
return result;
50168
}
51169

170+
52171
static int generalizedGCD(int num, int[] arr) {
53172
int gCD = 0;
54173
for (int i = 0; i < arr.length - 1; i++) {

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