You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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 (intnum : list) {
124
+
System.out.println(num);
125
+
}
126
+
}
127
+
System.out.println("ended");
81
128
}
82
129
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
+
staticint[][] findEdges(int[][] image) {
146
+
int[][] result = newint[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
+
returnresult;
153
+
}
154
+
intm = image.length;
155
+
intn = image[0].length;
156
+
System.out.println(" m = " + m + " n = " + n);
157
+
inti = 0;
158
+
intj = 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
+
returnresult;
188
+
}
189
+
190
+
191
+
83
192
/**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
193
* 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.
0 commit comments