回复: 5
跳转到指定楼层
上一主题 下一主题
收起左侧

门冲点面

🔗
匿名用户-ODVOK  2023-9-26 12:06:01 |倒序浏览

2023(7-9月) 码农类General 本科 全职@doordash - 猎头 - 技术电面  | 😐 Neutral 😐 Average | Pass | 在职跳槽

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
target e5 yoe6 hr reach out

// # A DashMart is a warehouse run by DoorDash that houses items found in
// # convenience stores, grocery stores, and restaurants. We have a city with open
// # roads, blocked-off roads, and DashMarts.
// #
// # City planners want you to identify how far a location is from its closest
// # DashMart.
// #
// # You can only travel over open roads (up, down, left, right).
// #
// # Locations are given in [row, col] format.
// #
// # Example:
// #
// # [
// # # 0 1 2 3 4 5 6 7 8
// # ['X', ' ', ' ', 'D', ' ', ' ', 'X', ' ', 'X'], # 0
// # ['X', ' ', 'X', 'X', ' ', ' ', ' ', ' ', 'X'], # 1
// # [' ', ' ', ' ', 'D', 'X', 'X', ' ', 'X', ' '], # 2
// # [' ', ' ', ' ', 'D', ' ', 'X', ' ', ' ', ' '], # 3
// # [' ', ' ', ' ', ' ', ' ', 'X', ' ', ' ', 'X'], # 4
// # [' ', ' ', ' ', ' ', 'X', ' ', ' ', 'X', 'X'] # 5
// # ]
// #
// # ' ' represents an open road that you can travel over in any direction (up, down, left, or right).
// # 'X' represents an blocked road that you cannot travel through.
// # 'D' represents a DashMart.
// #
// # # list of pairs [row, col]
// # locations = [
// # [2, 2],
// # [4, 0],
// # [0, 4],
// # [2, 6],
// # ]
// #
// # answer = [1, 4, 1, 5]
// #
// # Implement Function:
// # - get_closest_dashmart(city, locations)
// #
// # Provided:
// # - city: List[str]
// # - locations: List[List[int]]
// #
// # Return:
// # - answer: List[int]

需要注意的是墙也算valid location

上一篇:TikTok第四轮面试疑问
下一篇:MathWorks EDG intern 面经
地里匿名用户
🔗
匿名用户-6CP96  2023-9-26 13:06:26 来自APP
请问楼主面试之后多久收到结果呢
回复

使用道具 举报

全局:
最短路径,还挺简单的
回复

使用道具 举报

🔗
BigOhio 2023-9-28 07:23:45 | 只看该作者
全局:
这个是leetcode的题吧。类似于286
回复

使用道具 举报

地里匿名用户
🔗
匿名用户-AZZDT  2023-10-6 08:44:19 来自APP
典型bfs,这么简单啊
回复

使用道具 举报

地里匿名用户
🔗
匿名用户-BB9AG  2024-9-27 04:49:46
  1. public class DashMart {

  2.     private static final int[][] DIRECTIONS = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};  // Right, Down, Left, Up

  3.     public static int[] closestDashMart(char[][] city, int[][] locations) {
  4.         int rows = city.length;
  5.         int cols = city[0].length;
  6.         int[] results = new int[locations.length];
  7.         int[][] distances = new int[rows][cols];
  8.         boolean[][] visited = new boolean[rows][cols];
  9.         Queue<int[]> queue = new LinkedList<>();

  10.         // Initialize BFS from each DashMart location
  11.         for (int i = 0; i < rows; i++) {
  12.             for (int j = 0; j < cols; j++) {
  13.                 if (city[i][j] == 'D') {
  14.                     queue.add(new int[] {i, j});
  15.                     visited[i][j] = true;
  16.                     distances[i][j] = 0;
  17.                 }
  18.             }
  19.         }

  20.         // Perform BFS to calculate minimum distances to a DashMart
  21.         while (!queue.isEmpty()) {
  22.             int[] current = queue.poll();
  23.             int row = current[0];
  24.             int col = current[1];

  25.             for (int[] direction : DIRECTIONS) {
  26.                 int newRow = row + direction[0];
  27.                 int newCol = col + direction[1];

  28.                 if (newRow >= 0 && newRow < rows && newCol >= 0 && newCol < cols && !visited[newRow][newCol] && city[newRow][newCol] == ' ') {
  29.                     visited[newRow][newCol] = true;
  30.                     distances[newRow][newCol] = distances[row][col] + 1;
  31.                     queue.add(new int[]{newRow, newCol});
  32.                 }
  33.             }
  34.         }

  35.         // Retrieve distances for given locations
  36.         for (int i = 0; i < locations.length; i++) {
  37.             int locRow = locations[i][0];
  38.             int locCol = locations[i][1];
  39.             if (locRow < rows && locCol < cols && visited[locRow][locCol]) {
  40.                 results[i] = distances[locRow][locCol];
  41.             } else {
  42.                 results[i] = -1; // Unreachable or out of bounds
  43.             }
  44.         }

  45.         return results;
  46.     }

  47.     public static void main(String[] args) {
  48.         char[][] city = {
  49.                 {'X', ' ', ' ', 'D', ' ', ' ', 'X', ' ', 'X'},
  50.                 {'X', ' ', 'X', 'X', ' ', ' ', ' ', ' ', 'X'},
  51.                 {' ', ' ', ' ', 'D', 'X', 'X', ' ', 'X', ' '},
  52.                 {' ', ' ', ' ', 'D', ' ', 'X', ' ', ' ', ' '},
  53.                 {' ', ' ', ' ', ' ', ' ', 'X', ' ', ' ', 'X'},
  54.                 {' ', ' ', ' ', ' ', 'X', ' ', ' ', 'X', 'X'}
  55.         };

  56.         int[][] locations = {
  57.                 {2, 2},
  58.                 {4, 0},
  59.                 {0, 4},
  60.                 {2, 6}
  61.         };

  62.         int[] results = closestDashMart(city, locations);
  63.         for (int result : results) {
  64.             System.out.println(result);
  65.         }
  66.     }
  67. }
复制代码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表