Given m arrays, and each array is sorted in ascending order. Now you can pick up two integers from two different arrays (each array picks one) and calculate the distance. We define the distance between two integers a and b to be their absolute difference |a-b|. Your task is to find the maximum distance.

Example 1:

Input:
[[1,2,3],
[4,5],
[1,2,3]]
Output: 4
Explanation:
One way to reach the maximum distance 4 is to pick 1 in the first or third array and pick 5 in the second array.

Note:

  1. Each given array will have at least 1 number. There will be at least two non-empty arrays.
  2. The total number of the integers in all the m arrays will be in the range of [2, 10000].
  3. The integers in the m arrays will be in the range of [-10000, 10000].

思路:

将本组的最小与其他组最大依次做差,再将本组最大与其他组最小依次做差。 另外发现i不能写成i<n 要写成i<n-1

否则超时,很蹊跷。。

  int maxDistance(vector<vector<int> >& arrays)
{
int n = arrays.size();
int ret = ;
for(int i=;i<n-;i++)
{
int isize = arrays[i].size();
for(int j =i+;j<n;j++)
{
int jsize = arrays[j].size(); ret = max(ret,abs(arrays[i][] -arrays[jsize-]);
ret= max(ret,abs(arrays[j][] - arrays[isize-]);
}
}
return ret;
}

[leetcode-624-Maximum Distance in Arrays]的更多相关文章

  1. LeetCode 624. Maximum Distance in Arrays (在数组中的最大距离)$

    Given m arrays, and each array is sorted in ascending order. Now you can pick up two integers from t ...

  2. [LeetCode] 624. Maximum Distance in Arrays 数组中的最大距离

    Given m arrays, and each array is sorted in ascending order. Now you can pick up two integers from t ...

  3. 【LeetCode】624. Maximum Distance in Arrays 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 大根堆+小根堆 保存已有的最大最小 日期 题目地址:h ...

  4. 624. Maximum Distance in Arrays

    Problem statement Given m arrays, and each array is sorted in ascending order. Now you can pick up t ...

  5. 624. Maximum Distance in Arrays二重数组中的最大差值距离

    [抄题]: Given m arrays, and each array is sorted in ascending order. Now you can pick up two integers ...

  6. [LeetCode] Maximum Distance in Arrays 数组中的最大距离

    Given m arrays, and each array is sorted in ascending order. Now you can pick up two integers from t ...

  7. LeetCode Maximum Distance in Arrays

    原题链接在这里:https://leetcode.com/problems/maximum-distance-in-arrays/description/ 题目: Given m arrays, an ...

  8. [LeetCode] Minimize Max Distance to Gas Station 最小化去加油站的最大距离

    On a horizontal number line, we have gas stations at positions stations[0], stations[1], ..., statio ...

  9. [LeetCode] 849. Maximize Distance to Closest Person_Easy tag: BFS

    In a row of seats, 1 represents a person sitting in that seat, and 0 represents that the seat is emp ...

随机推荐

  1. JS执行效率与性能提升方案

    如果是追加字符串,最好使用s+=anotherStr操作,而不是要使用s=s+anotherStr.如果要连接多个字符串,应该少使用+=,如 s+=a;s+=b;s+=c;应该写成s+=a + b + ...

  2. jQuery选择器的分类

    jQuery选择器的分类 jQuery中有很多分类,大类分为四类,四类里面又分为很多小类,下面就为大家一一介绍,这些选择器的使用和好处,Me用的是jQuery1.8.3的版本 选择器都有哪四类?? 1 ...

  3. HTML5 进阶系列:canvas 动态图表

    前言 canvas 强大的功能让它成为了 HTML5 中非常重要的部分,至于它是什么,这里就不需要我多作介绍了.而可视化图表,则是 canvas 强大功能的表现之一. 现在已经有了很多成熟的图表插件都 ...

  4. 省市联动_简单的Demo,适用于各种二级菜单联动

    最近搞了一个功能,是查询页面需要用到二级菜单联动,获取到选中的属性value传入到后台. 平常都是用AJAX或者JQuery ,通过XML或者JSON的方式,这样的话需要调用数据库,像典型得到省市联动 ...

  5. struts2.1.6教程六、使用标签

    1.基础表单标签 准备工作:建立struts2tag项目,搭建好struts2的开发环境.在html我们常用的基础表单标签主要有文本域.密码域.提交.重置四种.它们在strust2中可以通过标签来生成 ...

  6. (原创)foef注意事项

    原创内容,转载请标明原文地址 我们先来看下下面的代码 //获取文件大小 DWORD Get_File_Size(const char* m_FilePath) { unsigned long size ...

  7. 改进Android语音对讲系统的方法

    本文属于Android局域网内的语音对讲项目系列,<实时Android语音对讲系统架构>阐述了局域网内Android语音对讲功能的框架,本文在此基础上进行了优化,包括音频的录制.播放,通信 ...

  8. Java集合类从属关系

    Java的集合分为了四类:List Set Queue Map,每类都有不同的实现,有基于数组实现的,有基于链表实现的,有基于xx树实现的,不同的实现虽在功能上可以相互替代但都有各自的应用场景,如基于 ...

  9. Java中设计模式之装饰者模式-3

    装饰者模式: 动态地将责任附加到对象上.若要扩展功能,装饰者提供了比继承更加有弹性的替代方案. 装饰者核心:实现功能组合 继承与组合区别: 继承 继承是给一个类添加行为的比较有效的途径.通过使用继承, ...

  10. Nginx上部署HTTPS

    Nginx上部署HTTPS依赖OpenSSL库和包含文件,即须先安装好libssl-dev,且ln -s /usr/lib/x86_64-linux-gnu/libssl.so  /usr/lib/, ...