Skip to content

Commit 8b97c7b

Browse files
add one random question
1 parent ca5fd5f commit 8b97c7b

File tree

1 file changed

+121
-12
lines changed

1 file changed

+121
-12
lines changed

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

Lines changed: 121 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -66,20 +66,129 @@ public static void main(String... args) {
6666
//List<List<Integer>> result = optimalUtilization(7, foregroundAppList, backgroundAppList);
6767
//CommonUtils.printListList(result);
6868

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);
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+
int[][] image = {
83+
{1, 1, 1, 1, 1, 1, 1},
84+
{1, 1, 1, 1, 1, 1, 1},
85+
{1, 1, 1, 0, 0, 0, 1},
86+
{1, 1, 1, 0, 0, 0, 1},
87+
{1, 1, 1, 1, 1, 1, 1}
88+
};
89+
90+
int[][] image2 = {
91+
{0, 0, 0, 1},
92+
{0, 0, 0, 1},
93+
{1, 1, 1, 1}
94+
};
95+
//should return 0,0 1,2
96+
97+
int[][] image3 = {
98+
{1, 1, 1, 0, 0, 0, 0},
99+
{1, 1, 1, 0, 0, 0, 0}
100+
};//should return 0,3 1,6
101+
102+
int[][] image4 = {{0}};
103+
104+
//this is for follow up question, see description below
105+
int[][] image5 = {
106+
{1, 1, 1, 1, 1, 1, 1},
107+
{1, 1, 1, 1, 1, 1, 1},
108+
{1, 1, 1, 0, 0, 0, 1},
109+
{1, 0, 1, 0, 0, 0, 1},
110+
{1, 0, 1, 1, 1, 1, 1},
111+
{1, 0, 1, 0, 0, 1, 1},
112+
{1, 1, 1, 0, 0, 1, 1},
113+
{1, 1, 1, 1, 1, 1, 1},
114+
};
115+
//should return
116+
// [[[2,3],[3,5]],
117+
// [[3,1],[5,1]],
118+
// [[5,3],[6,4]]]
119+
120+
int[][] result = findEdges(image4);
121+
System.out.println("here");
122+
for (int[] list : result) {
123+
for (int num : list) {
124+
System.out.println(num);
125+
}
126+
}
127+
System.out.println("ended");
81128
}
82129

130+
/**
131+
* Imagine we have an image. We'll represent this image as a simple 2D array where every pixel is a 1 or a 0.
132+
* The image you get is known to have a single rectangle of 0s on a background of 1s.
133+
* Write a function that takes in the image and returns the coordinates of the rectangle of 0's --
134+
* either top-left and bottom-right; or top-left, width, and height.
135+
*
136+
* Sample output:
137+
* x: 3, y: 2, width: 3, height: 2
138+
* 2,3 3,5 -- row,column of the top-left and bottom-right corners
139+
* 3,2 5,3 -- x,y of the top-left and bottom-right corners (as long as you stay consistent, either format is fine)
140+
*
141+
* Follow up:
142+
* What if there could be multiple such rectangles in there? How do you design an algorithm to return all of them?
143+
* What's the time and space complexity of your algorithm?
144+
* E.g. see image5 above*/
145+
static int[][] findEdges(int[][] image) {
146+
int[][] result = new int[2][2];
147+
result[0][0] = -1;
148+
result[0][1] = -1;
149+
result[1][0] = -1;
150+
result[1][1] = -1;
151+
if (image == null || image.length == 0) {
152+
return result;
153+
}
154+
int m = image.length;
155+
int n = image[0].length;
156+
System.out.println(" m = " + m + " n = " + n);
157+
int i = 0;
158+
int j = 0;
159+
for (i = 0; i < m; i++) {
160+
System.out.println("In first for loop: i = " + i + " j = " + j);
161+
for (j = 0; j < n; j++) {
162+
System.out.println("In second for loop: i = " + i + " j = " + j);
163+
if (image[i][j] == 0) {
164+
System.out.println(" i = " + i + " j = " + j);
165+
result[0][0] = i;
166+
result[0][1] = j;
167+
break;
168+
}
169+
}
170+
if (result[0][0] != -1) {
171+
break;
172+
}
173+
}
174+
175+
while (i < m && image[i][j] == 0) {
176+
i++;
177+
}
178+
179+
System.out.println("i = " + i);
180+
i--;
181+
while (j < n && image[i][j] == 0) {
182+
j++;
183+
}
184+
185+
result[1][0] = i;
186+
result[1][1] = --j;
187+
return result;
188+
}
189+
190+
191+
83192
/**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
84193
* 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.
85194
*

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