[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 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:
- Each given array will have at least 1 number. There will be at least two non-empty arrays.
- The total number of the integers in all the
m
arrays will be in the range of [2, 10000]. - 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]的更多相关文章
- 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 ...
- [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 ...
- 【LeetCode】624. Maximum Distance in Arrays 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 大根堆+小根堆 保存已有的最大最小 日期 题目地址:h ...
- 624. Maximum Distance in Arrays
Problem statement Given m arrays, and each array is sorted in ascending order. Now you can pick up t ...
- 624. Maximum Distance in Arrays二重数组中的最大差值距离
[抄题]: Given m arrays, and each array is sorted in ascending order. Now you can pick up two integers ...
- [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 ...
- LeetCode Maximum Distance in Arrays
原题链接在这里:https://leetcode.com/problems/maximum-distance-in-arrays/description/ 题目: Given m arrays, an ...
- [LeetCode] Minimize Max Distance to Gas Station 最小化去加油站的最大距离
On a horizontal number line, we have gas stations at positions stations[0], stations[1], ..., statio ...
- [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 ...
随机推荐
- Java7中的ForkJoin并发框架初探(中)——JDK中实现简要分析
原文发表于 2013 年 8 月 28 日 由 三石 根据前文描述的Doug Lea的理论基础,在JDK1.7中已经给出了Fork Join的实现.在Java SE 7的API中,多了ForkJoin ...
- 【JAVAWEB学习笔记】13_servlet
JavaWeb核心之Servlet 教学目标 案例一.完成用户登录功能 案例二.记录成功登录系统的人次 一.Servlet简介 1.什么是Servlet Servlet 运行在服务端的Java小程序, ...
- wget-文件下载工具
阅读目录:详解wget命令-文件下载工具 安装wget 命令格式 命令功能 参数指南 启动参数 记录和输入文件参数 下载参数 目录参数 http选项参数 ftp选项参数 递归下载参数 递归下载中的包含 ...
- Java计算1-100的和(要求尽量考虑代码优化)
1.递归算法 public static void main(String[] args) { System.out.println(add(1)); } private static int add ...
- python利用selenium和safari浏览器驱动实现新浪微博自动点赞 Demo
import time from selenium import webdriver browser = webdriver.Safari() browser.get('http://weibo.co ...
- MyBatis_Generator插件的安装以及简单使用
MyBatis_Generator_1.3.1.zip 1 下载安装包 安装包名称:MyBatis_Generator_1.3.1.zip 2 在Eclipse上进行安装 l ...
- python——爬虫&问题解决&思考(三)
继续上一篇文章的内容,上一篇文章中,将爬虫调度器已经写好了,调度器是整个爬虫程序的"大脑",也可以称之为指挥中心.而现在,我们要做的就是去将调度器中用到的其他组件写好.首先是url ...
- SQL SERVER大话存储结构(6)_数据库数据文件
数据库文件有两大类:数据文件跟日志文件,每一个数据库至少各有一个数据文件或者日志文件,数据文件用来存储数据,日志文件用来存储数据库的事务修改情况,可用于恢复数据库使用. 这里分 ...
- cs6安装
[按下面步骤操作 无需序列号] ·打上补丁 永远无需序列号 (现在网上没什么能用的序列号) ·1双击CS软件安装,选择试用 ·2创建ID 输入自己邮箱和密码,姓名可以随意输入 ·3安装完成后 运行一次 ...
- 面向对象的SOLID原则白话篇
面向对象的SOLID原则 简介 缩写 全称 中文 S The Single Responsibility Principle 单一责任原则 O The Open Closed Principle 开放 ...