Redian新闻
>
Bergen郡NJ的AMEX的smal business大家都去哪里消费? (转载)
avatar
Bergen郡NJ的AMEX的smal business大家都去哪里消费? (转载)# Money - 海外理财
f*m
1
题目:
在网上搜了一个code,如下,做了一些小修改,可是总过不了online judge.大家能发一
个能通过judge的(n^2) code吗?谢谢。
vector > fourSum(vector &num, int target) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int n = num.size();
vector > result;
map > sums;
for (int i = 0; i < n; ++i) {
// 'sums' hastable holds all possible sums a[k] + a[l]
// where k and l are both less than i
for (int j = i + 1; j < n; ++j) {
int current = num[i] + num[j];
int rest = target - current;
// Now we need to find if there're different numbers k and l
// such that a[k] + a[l] == rest and k < i and l < i
// but we have 'sums' hashtable prepared for that
if (sums.count(rest) != 0) {
// found it
vector t;
t.push_back(sums[rest].first);
t.push_back(sums[rest].second);
t.push_back(num[i]);
t.push_back(num[j]);

}
}
// now let's put in 'sums' hashtable all possible sums
// a[i] + a[k] where k < i
for (int k = 0; k < i; ++k) {
sums[num[i] + num[k]] = pair (i, k);
}
}
return result;
}
avatar
s*r
2
【 以下文字转载自 NewJersey 讨论区 】
发信人: superpower (执著的创业者), 信区: NewJersey
标 题: Bergen郡NJ的AMEX的smal business大家都去哪里消费?
发信站: BBS 未名空间站 (Fri Nov 29 04:12:54 2013, 美东)
有6张AMEX卡。
最好是在加油站买油卡,其次是食品店买食品,实在不行的好只好下馆子吃掉了。
大家有啥推荐么?
谢谢!
avatar
l*t
3
/*accepted*/
bool equalVector(vector v1, vector v2){
for(int ii = 0; ii < v1.size(); ++ii){
if (v1[ii] != v2[ii]) return false;
}
return true;
}
bool compareVector(vector v1, vector v2){
for(int ii = 0; ii < v1.size(); ++ii){
if (v1[ii] < v2[ii]) return true;
else if (v1[ii] > v2[ii]) return false;
}
return false;
}
class Solution {
public:
vector > fourSum(vector &num, int target) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector > result;
if (num.size() < 4) return result;
typedef vector::iterator It;
vector v(num);
sort(v.begin(),v.end());
const int shift = v[0];
const int shift4 = shift*4;
for(int ii = 0; ii < num.size(); ++ii){
v[ii] -= shift;
}
sort(v.begin(),v.end());
target -= shift4;
for(It itA = v.begin(); itA != v.end(); ++itA){
if (*itA > target ) break;
for(It itB = itA+1; itB != v.end(); ++itB){
int const sumAB = *itA + *itB;
if (sumAB > target) break;
for(It itC = itB+1; itC != v.end(); ++itC){
int const sumABC = sumAB + *itC;
if (sumABC > target) break;
int subTarget = target - sumABC;
if (binary_search(itC+1, v.end(), subTarget)){
int temp[] = {*itA+shift, *itB+shift, *itC+shift,
subTarget+shift};
result.push_back(vector(temp,temp+4));
}
}
}
}
sort(result.begin(),result.end(),compareVector);
vector >::iterator it = unique(result.begin(),result.end
(), equalVector);
result.resize(it - result.begin());
return result;
}
};
avatar
r*e
4
the lowerbound of 4sum runtime is O(n^3).
avatar
f*i
6
Like this? I can't open that link.
1. Calculate all 2 sum, create hash table, o(n^2) time and space.
2. 2 sum in o(n^2) space. Also o(n^2) time.
avatar
f*m
7
对,用hashtable存two number sum的结果
avatar
d*x
8
看起来似乎是没有不用hashtable的n^2解法

【在 f*********m 的大作中提到】
: 对,用hashtable存two number sum的结果
avatar
e*o
9
我记得leetcode里面要求每个组合是unique的。
如果用hashtable 的话
如果两个two_sum 是 (-2, -1); (1, 2);
那么 (-2, 1); (-1, 2)的和也是0;
然而这个重复了。
我看到的做好的是 O(N^2 logn)
avatar
f*m
10
题目:
在网上搜了一个code,如下,做了一些小修改,可是总过不了online judge.大家能发一
个能通过judge的(n^2) code吗?谢谢。
vector > fourSum(vector &num, int target) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int n = num.size();
vector > result;
map > sums;
for (int i = 0; i < n; ++i) {
// 'sums' hastable holds all possible sums a[k] + a[l]
// where k and l are both less than i
for (int j = i + 1; j < n; ++j) {
int current = num[i] + num[j];
int rest = target - current;
// Now we need to find if there're different numbers k and l
// such that a[k] + a[l] == rest and k < i and l < i
// but we have 'sums' hashtable prepared for that
if (sums.count(rest) != 0) {
// found it
vector t;
t.push_back(sums[rest].first);
t.push_back(sums[rest].second);
t.push_back(num[i]);
t.push_back(num[j]);

}
}
// now let's put in 'sums' hashtable all possible sums
// a[i] + a[k] where k < i
for (int k = 0; k < i; ++k) {
sums[num[i] + num[k]] = pair (i, k);
}
}
return result;
}
avatar
l*t
11
/*accepted*/
bool equalVector(vector v1, vector v2){
for(int ii = 0; ii < v1.size(); ++ii){
if (v1[ii] != v2[ii]) return false;
}
return true;
}
bool compareVector(vector v1, vector v2){
for(int ii = 0; ii < v1.size(); ++ii){
if (v1[ii] < v2[ii]) return true;
else if (v1[ii] > v2[ii]) return false;
}
return false;
}
class Solution {
public:
vector > fourSum(vector &num, int target) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector > result;
if (num.size() < 4) return result;
typedef vector::iterator It;
vector v(num);
sort(v.begin(),v.end());
const int shift = v[0];
const int shift4 = shift*4;
for(int ii = 0; ii < num.size(); ++ii){
v[ii] -= shift;
}
sort(v.begin(),v.end());
target -= shift4;
for(It itA = v.begin(); itA != v.end(); ++itA){
if (*itA > target ) break;
for(It itB = itA+1; itB != v.end(); ++itB){
int const sumAB = *itA + *itB;
if (sumAB > target) break;
for(It itC = itB+1; itC != v.end(); ++itC){
int const sumABC = sumAB + *itC;
if (sumABC > target) break;
int subTarget = target - sumABC;
if (binary_search(itC+1, v.end(), subTarget)){
int temp[] = {*itA+shift, *itB+shift, *itC+shift,
subTarget+shift};
result.push_back(vector(temp,temp+4));
}
}
}
}
sort(result.begin(),result.end(),compareVector);
vector >::iterator it = unique(result.begin(),result.end
(), equalVector);
result.resize(it - result.begin());
return result;
}
};
avatar
r*e
12
the lowerbound of 4sum runtime is O(n^3).
avatar
f*i
14
Like this? I can't open that link.
1. Calculate all 2 sum, create hash table, o(n^2) time and space.
2. 2 sum in o(n^2) space. Also o(n^2) time.
avatar
f*m
15
对,用hashtable存two number sum的结果
avatar
d*x
16
看起来似乎是没有不用hashtable的n^2解法

【在 f*********m 的大作中提到】
: 对,用hashtable存two number sum的结果
avatar
e*o
17
我记得leetcode里面要求每个组合是unique的。
如果用hashtable 的话
如果两个two_sum 是 (-2, -1); (1, 2);
那么 (-2, 1); (-1, 2)的和也是0;
然而这个重复了。
我看到的做好的是 O(N^2 logn)
avatar
l*o
18
有比O(n^3)更好的解法吗?好像输出的最大size是O(n^3)。
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。