[lintcode the-smallest-difference]最小差(python)

题目链接:http://www.lintcode.com/zh-cn/problem/the-smallest-difference/

给定两个整数数组(第一个是数组 A,第二个是数组 B),在数组 A 中取 A[i],数组 B 中取 B[j],A[i] 和 B[j]两者的差越小越好(|A[i] - B[j]|)。返回最小差。

排好序后用两个指针分别扫描两个数组,每次更新他们的差值的绝对值。并且依据他们两个数字的大小来决定谁来移动指针。

 1 class Solution:
 2     # @param A, B: Two lists of integer
 3     # @return: An integer
 4     def smallestDifference(self, A, B):
 5         # write your code here
 6         A.sort()
 7         B.sort()
 8         i = 0
 9         j = 0
10         ret = 2147483647
11         while i < len(A) and j < len(B):
12             ret = min(ret, abs(A[i]-B[j]))
13             if A[i] > B[j]:
14                 j += 1
15             elif A[i] < B[j]:
16                 i += 1
17             elif A[i] == B[j]:
18                 ret = 0
19                 break
20         return ret
时间: 2024-08-01 10:44:33

[lintcode the-smallest-difference]最小差(python)的相关文章

Smallest Difference (poj 2718 暴力枚举)

Language: Default Smallest Difference Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5034   Accepted: 1382 Description Given a number of distinct decimal digits, you can form one integer by choosing a non-empty subset of these digits an

The Smallest Difference

Given two array of integers(the first array is array A, the second array is array B), now we are going to find a element in array A which is A[i], and another element in array B which is B[j], so that the difference between A[i] and B[j] (|A[i] - B[j

POJ 2718 Smallest Difference(贪心 or next_permutation暴力枚举)

Smallest Difference Description Given a number of distinct decimal digits, you can form one integer by choosing a non-empty subset of these digits and writing them in some order. The remaining digits can be written down in some order to form a second

lintcode-medium-The Smallest Difference

Given two array of integers(the first array is array A, the second array is array B), now we are going to find a element in array A which is A[i], and another element in array B which is B[j], so that the difference between A[i] and B[j] (|A[i] - B[j

POJ2718 Smallest Difference 【贪心+枚举】

Smallest Difference Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4639   Accepted: 1290 Description Given a number of distinct decimal digits, you can form one integer by choosing a non-empty subset of these digits and writing them in

【POJ - 2718】Smallest Difference(搜索 )

-->Smallest Difference 直接写中文了 Descriptions: 给定若干位十进制数,你可以通过选择一个非空子集并以某种顺序构建一个数.剩余元素可以用相同规则构建第二个数.除非构造的数恰好为0,否则不能以0打头. 举例来说,给定数字0,1,2,4,6与7,你可以写出10和2467.当然写法多样:210和764,204和176,等等.最后一对数差的绝对值为28,实际上没有其他对拥有更小的差. Input  输入第一行的数表示随后测试用例的数量.对于每组测试用例,有一行至少两个

poj 2718 Smallest Difference(穷竭搜索dfs)

Description Given a number of distinct decimal digits, you can form one integer by choosing a non-empty subset of these digits and writing them in some order. The remaining digits can be written down in some order to form a second integer. Unless the

poj 2718 Smallest Difference

题目大意:给你几个数字,选出一些数字组成一个整数,另外的组成另一个整数,求这两个数差的绝对值的最小值 Input The first line of input contains the number of cases to follow. For each case, there is one line of input containing at least two but no more than 10 decimal digits. (The decimal digits are 0,

[Lintcode 3sum]三数之和(python,二分)

题目链接:http://www.lintcode.com/zh-cn/problem/3sum/?rand=true# 用这个OJ练练python…这个题意和解法就不多说了,O(n^2lgn)就行了,关键是!!python的语法… 要想给tuple排序,如果直接sort的话会自动转成list,这个时候要再转回来. 1 class Solution: 2 """ 3 @param numbersbers : Give an array numbersbers of n inte

Smallest Difference

Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5364 Accepted: 1456 Description Given a number of distinct decimal digits, you can form one integer by choosing a non-empty subset of these digits and writing them in some order. The remainin