Time complexity 给定2个数组的和分数-O(n)?
给你2个整数数组Time complexity 给定2个数组的和分数-O(n)?,time-complexity,complexity-theory,Time Complexity,Complexity Theory,给你2个整数数组 A=[1, 2, 1] B=[2, 3, 3] so fractions are: 1/2, 2/3, 1/3 A是分子,B是分母。 所以分数是:1/2,2/3,1/3 查找总和为1的所有对 示例:这里我们有2/3+1/3=1,所以count=1 返回1 返回模10^9+7,因为输入可能很大 我在O(n^2)中做了一次,然后计算2的加法,检查它是否为1,并更新计数器 在
A=[1, 2, 1]
B=[2, 3, 3]
so fractions are: 1/2, 2/3, 1/3
A是分子,B是分母。
所以分数是:1/2,2/3,1/3
查找总和为1的所有对
示例:这里我们有2/3+1/3=1,所以count=1
返回1
返回模10^9+7,因为输入可能很大
我在O(n^2)中做了一次,然后计算2的加法,检查它是否为1,并更新计数器
在O(n)中是否可能
任何语言idm示例:
function solution(integer array A, integer array B){
return integer_counter;
}
这是一个使用字典的C#解决方案
public static int SumOfFraction(int[] numerator, int[] denominator) {
int count = 0;
Dictionary < int, List < int >> keyValuePairs = new Dictionary < int, List < int >> ();
for (int i = 0; i < denominator.Length; i++) {
if (keyValuePairs.ContainsKey(denominator[i])) {
keyValuePairs[denominator[i]].Add(numerator[i]);
} else {
keyValuePairs.Add(denominator[i], new List < int > {
numerator[i]
});
}
}
foreach(var keypair in keyValuePairs) {
if (keypair.Key == keypair.Value.Sum()) {
count++;
}
}
return count;
}
公共静态int-SumOfFraction(int[]分子,int[]分母){
整数计数=0;
Dictionary>keyValuePairs=新建Dictionary>();
for(int i=0;i<分母长度;i++){
if(keyValuePairs.ContainsKey(分母[i])){
keyValuePairs[分母[i]]。添加(分子[i]);
}否则{
添加(分母[i],新列表{
分子[i]
});
}
}
foreach(keyValuePairs中的var密钥对){
if(keypair.Key==keypair.Value.Sum()){
计数++;
}
}
返回计数;
}
Cpp解决方案:
#include <cassert>
#include <cstdint>
#include <unordered_set>
#include <utility>
#include <vector>
#include <iostream>
using namespace std;
//From https://stackoverflow.com/questions/15160889/how-can-i-make-an-unordered-set-of-pairs-of-integers-in-c
struct IntPairHash {
size_t operator()(const pair<uint32_t, uint32_t> &p) const {
assert(sizeof(size_t)>=8); //Ensure that std::size_t, the type of the hash, is large enough
//Shift first integer over to make room for the second integer. The two are
//then packed side by side.
return (((uint64_t)p.first)<<32) | ((uint64_t)p.second);
}
};
size_t
countPairs(const vector<int>& v1, const vector<int>& v2) {
unordered_set< std::pair<int, int>, IntPairHash> searchSet;
size_t count = 0;
for (size_t i = 0; i < v1.size(); i++) {
int complement = v2[i] - v1[i];
if (searchSet.find({complement, v2[i]}) != searchSet.end()) {
count++;
}
searchSet.insert({v1[i], v2[i]});
}
return count;
}
int main() {
cout << countPairs({1,2,1}, {2,3,3});
}
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
//从https://stackoverflow.com/questions/15160889/how-can-i-make-an-unordered-set-of-pairs-of-integers-in-c
结构IntPairHash{
大小\u t运算符()(常数对和p)常数{
assert(sizeof(size\u t)>=8);//确保散列的类型std::size\u t足够大
//将第一个整数移位,为第二个整数腾出空间
//然后并排打包。
return((uint64_t)p.first)创建一个哈希表,在其中使用键1-n
存储分数n
。每当计算新分数时,首先查看它是否已与键匹配。否则,将其作为键1-n
的新元素插入。是否确实正确调用了O(n^2)
?它应该是O(2n)
根据您所说的。我看不到嵌套的for loop比率一旦排序,它可以在O(n)中完成。或者,使用哈希,它可以在O(n)中完成@ycx在每个索引处,我查看整个数组,将每个索引与im处的索引进行比较,并检查总和是否为1。因此,每个索引都有一个循环,并嵌套以检查每个索引与所有values@DetectivePikachu这就是我所做的。j=i+1部分。这是O(n)吗?我认为这是O(n^2)的改进,但仍然是O(n^2)