[LeetCode]Best Meeting Point

第一次一次过的hard题目,其实觉得难度应该算是中等难题。就是先找纵方向的中点,再找横方向的中点,思路类似一维的情况

public class Solution {
    public int minTotalDistance(int[][] grid) {
        int row = grid.length;
        if (row == 0) {
            return 0;
        }
        int col = grid[0].length;
        ArrayList<Integer> list_r = new ArrayList<Integer>();
        ArrayList<Integer> list_c = new ArrayList<Integer>();
        for (int r = 0; r < row; r++) {
            for (int c = 0; c < col; c++) {
                if (grid[r][c] == 1) {
                    list_r.add(r);
                }
            }
        }
        for (int c = 0; c < col; c++) {
            for (int r = 0; r < row; r++) {
                if (grid[r][c] == 1) {
                    list_c.add(c);
                }
            }
        }
        int result_r = list_r.get(list_r.size() / 2);
        int result_c = list_c.get(list_c.size() / 2);
        int result = 0;
        for (int r = 0; r < row; r++) {
            for (int c = 0; c < col; c++) {
                if (grid[r][c] == 1) {
                    result += Math.abs(r - result_r) + Math.abs(c - result_c);
                }
            }
        }
        return result;
    }
}
时间: 2024-10-21 08:09:08

[LeetCode]Best Meeting Point的相关文章

[LeetCode] Best Meeting Point 最佳开会地点

A group of two or more people wants to meet and minimize the total travel distance. You are given a 2D grid of values 0 or 1, where each 1 marks the home of someone in the group. The distance is calculated using Manhattan Distance, where distance(p1,

LeetCode 252. Meeting Rooms (会议室)

Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si < ei), determine if a person could attend all meetings. For example,Given [[0, 30],[5, 10],[15, 20]],return false. 题目标签:sort 这道题目给了我们一个array的会议时间,让我们

[LeetCode#252] Meeting Rooms

Problem: Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si < ei), determine if a person could attend all meetings. For example,Given [[0, 30],[5, 10],[15, 20]],return false. Analysis: The problem is

[LeetCode#253] Meeting Rooms II

Problem: Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si < ei), find the minimum number of conference rooms required. For example,Given [[0, 30],[5, 10],[15, 20]],return 2. Analysis: This problem l

Facebook题库汇总

 [leetcode]273. Integer to English Words 整数转英文单词  [leetcode]301. Remove Invalid Parentheses 去除无效括号  [leetcode]15. 3Sum三数之和  [leetcode]253. Meeting Rooms II 会议室II  [leetcode]621. Task Scheduler任务调度  [leetcode]121. Best Time to Buy and Sell Stock 最佳炒股时

Leetcode Meeting Rooms

Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si < ei), determine if a person could attend all meetings. For example,Given [[0, 30],[5, 10],[15, 20]],return false. 解题思路: 先对start sort, O(nlgn), 然后比较S

[LeetCode] Meeting Rooms II 会议室之二

Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si < ei), find the minimum number of conference rooms required. For example,Given [[0, 30],[5, 10],[15, 20]],return 2. 这道题是之前那道Meeting Rooms的拓展,那道题只让我们是

LeetCode 296. Best Meeting Point

原题链接在这里:https://leetcode.com/problems/best-meeting-point/ 题目: A group of two or more people wants to meet and minimize the total travel distance. You are given a 2D grid of values 0 or 1, where each 1 marks the home of someone in the group. The dista

Leetcode 252, 253. Meeting Rooms

Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si < ei), determine if a person could attend all meetings. For example,Given [[0, 30],[5, 10],[15, 20]],return false. 所有meeting time按照start排序之后,逐个检查当前的s