zoukankan      html  css  js  c++  java
  • 296. Best Meeting Point

    最后更新

    二刷
    12-Jan-2017

    M难度的。。

    如果用BFS来做,就变成H难度的了。

    首先统计每行有几个人,每列有几个人。

    然后看哪一行使得纵向距离最短,哪一列使得横向距离最短就行了。。

    Time: O(mn)
    Space: O(m + n)

    public class Solution {
        public int minTotalDistance(int[][] grid) {
            if (grid.length == 0) return 0;
            
            int[] row = new int[grid.length];
            int[] col = new int[grid[0].length];
            
            for (int i = 0; i < grid.length; i++) {
                for (int j = 0; j < grid[0].length; j++) {
                    if (grid[i][j] == 1) {
                        row[i] ++;
                        col[j] ++;
                    }
                }
            }
            
            // which row
            int horizonLength = Integer.MAX_VALUE;
            for (int i = 0; i < grid.length; i++) {
                int tempTotal = 0;
                for (int j = 0; j < row.length; j++) {
                    tempTotal += Math.abs(i - j) * row[j];
                }
                horizonLength = Math.min(tempTotal, horizonLength);
            }
            // which column
            int verticalLength = Integer.MAX_VALUE;
            for (int i = 0; i < grid[0].length; i++) {
                int tempTotal = 0;
                for (int j = 0; j < col.length; j++) {
                    tempTotal += Math.abs(i - j) * col[j];
                }
                verticalLength = Math.min(tempTotal, verticalLength);
            }
            
            return verticalLength + horizonLength;
        }
    }
    

    一刷
    17-Oct-2016

    难得秒一次H的。。难道我真的不是纯弱智?

    不过似乎是很简单的一个。。

    主要是要分别算。

    横纵方向分别以每条线作为集合点,看哪条线的路程最短,两个方向最后相加就行了。。

    public class Solution 
    {
        public int minTotalDistance(int[][] grid) 
        {
            
            int[] horizon = new int[grid[0].length];
            int[] vertic = new int[grid.length];
            
            for(int i = 0; i < grid.length;i++)
            {
                for(int j = 0; j < grid[0].length;j++)
                {
                    if(grid[i][j] == 1)
                    {
                        vertic[i]++;
                        horizon[j]++;
                    }
                }
            }
            
            int min = Integer.MAX_VALUE;
    
            for(int i = 0; i < grid.length;i++)
            {
                int temp = 0;
                for(int j = 0;j<grid.length;j++)
                {
                    temp+= Math.abs(j-i)*vertic[j];
                }
                min = Math.min(min,temp);
            }
            
            int res = Integer.MAX_VALUE;
            for(int i = 0; i < grid[0].length;i++)
            {
                int temp = 0;
                for(int j = 0;j<grid[0].length;j++)
                {
                    temp+= Math.abs(j-i)*horizon[j];
                }
                res = Math.min(res,temp);
            }
            
            return res+min;
        }
    }
    
  • 相关阅读:
    安卓给DatePicker设置选择日期后的监听
    Linux端口相关一些命令
    安卓使用Zxing创建二维码
    vue中this.$router.push()路由跳转和传参
    C# 获取请求头中包含指定元素的值
    各种JSON格式数据
    SQL 中 char、nchar、varchar、nvarchar 的区别
    vue中表单修饰符
    vue 中的export 、 export default 和 new Vue({})
    String or binary data would be truncated.
  • 原文地址:https://www.cnblogs.com/reboot329/p/5971979.html
Copyright © 2011-2022 走看看