数组 C[I] = A[I] + B[I] / 1,000,000.
例如 A 和 B:
A[0] = 0 B[0] = 500,000
A[1] = 1 B[1] = 500,000
A[2] = 2 B[2] = 0
A[3] = 2 B[3] = 0
A[4] = 3 B[4] = 0
A[5] = 5 B[5] = 20,000
则 C:
C[0] = 0.5
C[1] = 1.5
C[2] = 2.0
C[3] = 2.0
C[4] = 3.0
C[5] = 5.02
寻找一对下标(P, Q) 满足 0 ≤ P < Q < N 且 C[P] * C[Q] ≥ C[P] + C[Q].
上面的数组中满足条件的有:
(1, 4), 1.5 * 3.0 = 4.5 ≥ 4.5 = 1.5 + 3.0,
(1, 5), 1.5 * 5.02 = 7.53 ≥ 6.52 = 1.5 + 5.02,
(2, 3), 2.0 * 2.0 = 4.0 ≥ 4.0 = 2.0 + 2.0,
(2, 4) and (3, 4), 2.0 * 3.0 = 6.0 ≥ 5.0 = 2.0 + 3.0.
(2, 5) and (3, 5), 2.0 * 5.02 = 10.04 ≥ 7.02 = 2.0 + 5.02.
(4, 5), 3.0 * 5.02 = 15.06 ≥ 8.02 = 3.0 + 5.02.
现要求写个方法:
class Solution { public int solution(int[] A, int[] B); }
给出数组 A and B, 分别包含N个非负整数, 返回满足条件的下标对个数.
如果满足条件的下标对超过 1,000,000,000, 则返回 1,000,000,000.
例如,给出:
A[0] = 0 B[0] = 500,000
A[1] = 1 B[1] = 500,000
A[2] = 2 B[2] = 0
A[3] = 2 B[3] = 0
A[4] = 3 B[4] = 0
A[5] = 5 B[5] = 20,000
此方法应返回 8.
假设:
N 是整数,取值范围 [0..100,000];
数组A中的元素取值范围 [0..1,000];
数组B中的元素取值范围 [0..999,999];
数组C中的元素为非递减.
复杂度要求:
最坏的情况下,时间复杂度 O(N);
最坏的情况下,空间复杂度 O(1), 不包括参数的存储空间.
相关分类