Find minimum path to remove obstacle
Create Date: June 16, 2019 at 11:30 AM         | Tag: QUESTIONS         | Author Name: Sun, Charles |
a bot needs to remove an obstacle in the lot. there are trenches (0), which the bot cannot pass it. 9 stands for obstacle, 1 stands for flat path (the bot can pass).
This is a typical backtrack with more conditions. It is close to hard level. not lucky.
public class MinimumPathRemoveObstacle {
public static int findMinimum(int numRows, int numColumns, List<List<Integer>> lot) {
if (lot == null || lot.size() == 0) { return 0; }
boolean[][] used = new boolean[numRows][numColumns];
List<Integer> res = new ArrayList<Integer>();
dfs(lot, res, 0, 0, used, numRows, numColumns);
//mistake 1: I shouldn't use -1 in the test because it always goes -1. Didn't notice this typo during the exam.
int min = Integer.MAX_VALUE;
for (int i = 0; i < res.size(); i++) {
min = Math.min(min, res.get(i));
}
return min == Integer.MAX_VALUE ? -1: min;
}
private static void dfs(List<List<Integer>> lot, List<Integer> res, int i, int j,
boolean[][] used, int numRows, int numColumns) {
if (i >= numRows || j >= numColumns) { return; }
if (lot.get(i).get(j) == 9) {
//mistake 2: should add i and j, not multiply them
res.add(i + j);
return;
}
if (lot.get(i).get(j) == 0) {
return;
}
used[i][j] = true;
if (i < numRows - 1 && !used[i + 1][j]) { dfs(lot, res, i + 1, j, used, numRows, numColumns); }
if (j < numColumns - 1 && !used[i][j + 1]) { dfs(lot, res, i, j + 1, used, numRows, numColumns); }
if (i > 0 && !used[i - 1][j]) { dfs(lot, res, i - 1, j, used, numRows, numColumns); }
if (j > 0 && !used[i][j - 1]) { dfs(lot, res, i, j - 1, used, numRows, numColumns); }
//mistake 3: forgot to reset the used array.
used[i][j] = false;
}
}
unit test:
class MinimumPathRemoveObstacleTest {
@Test
void testFindMinimum() {
int numRows = 3, numColumns = 3;
List<List<Integer>> lot = new ArrayList<List<Integer>>();
lot.add(Arrays.asList(1,0,0));
lot.add(Arrays.asList(1,0,1));
lot.add(Arrays.asList(1,9,0));
int res = MinimumPathRemoveObstacle.findMinimum(numRows, numColumns, lot);
System.out.println(res);
assertEquals(3, res);
List<List<Integer>> lot2 = new ArrayList<List<Integer>>();
lot2.add(Arrays.asList(1,0,0));
lot2.add(Arrays.asList(1,9,1));
lot2.add(Arrays.asList(1,0,0));
int res2 = MinimumPathRemoveObstacle.findMinimum(numRows, numColumns, lot2);
System.out.println(res2);
assertEquals(2, res2);
}
}
New Comment
Minimum load time
Create Date: June 16, 2019 at 11:26 AM         | Tag: QUESTIONS         | Author Name: Sun, Charles |
each item has loading time. calcualte the minimum loading time. forgot the details, but it like a fabonacci sequence. The loading time saved in a list.
my solution is convert list to an array -> sort array -> calculate the fabonacci sequence.
public class MinimumLoad {
public static int findMinimum(List<Integer> load) {
if (load == null) { return 0; }
int n = load.size();
if (n == 0) { return 0; }
int[] temp = new int[n];
for (int i = 0; i < n; i++) {
temp[i] = load.get(i);
}
Arrays.sort(temp);
int sum = temp[0], total = 0;
for (int i = 1; i < n; i++) {
sum += temp[i];
total += sum;
}
return total;
}
}
unit test:
class MinimumLoadTest {
@Test
void testFindMinimum() {
List<Integer> input = new ArrayList<Integer>();
input.add(4);
input.add(12);
input.add(6);
input.add(8);
int res = MinimumLoad.findMinimum(input);
System.out.print(res);
assertEquals(58, res);
}
}
New Comment
Saving attachment using microsoft graph
Create Date: February 08, 2019 at 10:38 AM         | Tag: MICROSOFT GRAPH         | Author Name: Sun, Charles |
Microsoft Graph - Saving file attachments through C#?
Once you have particular Microsoft Graph message, you can e.g. pass it to a method as parameter. Then you need to make another request to get attachments by message Id, iterate through attachments and cast it to FileAttachment
to get access to the ContentBytes
property and finally save this byte array to the file.
private static async Task SaveAttachments(Message message)
{
var attachments =
await _client.Me.MailFolders.Inbox.Messages[message.Id].Attachments.Request().GetAsync();
foreach (var attachment in attachments.CurrentPage)
{
if (attachment.GetType() == typeof(FileAttachment))
{
var item = (FileAttachment)attachment; // Cast from Attachment
var folder = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
var filePath = Path.Combine(folder, item.Name);
System.IO.File.WriteAllBytes(filePath, item.ContentBytes);
}
}
}
note that, if the use didn't have enough permission to save file (in my case it is c drive), just grant permission or modify the saving location.
New Comment