Redian新闻
>
买哪种减肥药带回国送人比较好
avatar
买哪种减肥药带回国送人比较好# Fashion - 美丽时尚
c*g
1
网上看到一道题 求O(n)的解法. n是document中的word数
You have a dictionary, D, that stores the positions of words in a document
by mapping words (strings) to positions in the document (arrays of ints.)
You also have a list of words, L. Your job is to find the shortest sequence
of words in the document that contains all the words in L.
E.g., if the document is "a b a c d x b a", then
D["a"] = [0 2 7]
D["b"] = [1 6]
...
If we are given that L=["a", "c", "x"]
Then we should return the start and end point of the shortest sequence that
contains all words in L, which is (2, 5). The best solution is in O(n)
complexity.
avatar
a*q
2
准备回国,买哪种减肥药带回国送人比较好?
在哪里可以买到。谢谢。
avatar
n*y
4
alli...
costco..
avatar
b*7
5
算法思想:可以使用|L|大小的最小堆,同时维护堆的最大值,则range为(heap.top(),
max{heap}),最多迭代n-|L|次,得出最优range。
时间复杂度O(|L|) + O(n*log|L|) 由于|L|最多为128,故为O(n)
此算法前提为:D(c)从小到大排序
code:
typedef pair::iterator,vector::iterator> Itemtype;
struct ItemCompare{
bool operator()(const Itemtype & left, const Itemtype & right)
{
return *(left.first) > * (right.first);
}
};
pair minrange(unordered_map > & D, vector &
L)
{
pair result = make_pair(-1,-1);
vector temp;
int maxpos = -1;
for(auto it = L.begin(); it != L.end(); ++it)
{
auto findit = D.find(*it);
if(findit == D.end() || findit->second.empty()) return result;
temp.push_back(make_pair(findit->second.begin(),findit->second.end()
));
maxpos = max(maxpos, findit->second[0]);
}
priority_queue, ItemCompare> minheap(temp.
begin(),temp.end());
int minpos = *(minheap.top().first);
result = make_pair(minpos,maxpos);
while(true)
{
Itemtype minelem = minheap.top();
minheap.pop();
minelem.first++;
if(minelem.first == minelem.second)
break;
minheap.push(minelem);
maxpos = max(maxpos,*(minelem.first));
minpos = *(minheap.top().first);
if(maxpos - minpos < result.second - result.first)
result = make_pair(minpos,maxpos);
}
return result;
}
类似的题:
http://www.careercup.com/question?id=16759664
avatar
D*a
6
alli,costco or walmart or cvs
avatar
c*g
7
Yes, I have found the similar problem on leetcode "Minimum Window Substring"
But there is a little bit different. Here they provide the dictionary D
which shows the indexes each character appears. I couldn't think a way to
utilize it in order to get O(n) solution.

【在 r*****s 的大作中提到】
: leetcode上面要不然有原题,要不然有近似的题
: http://leetcode.com/onlinejudge#question_30

avatar
A*k
8
推荐绿茶的,costco也有卖,效果好,损伤小
avatar
a*q
9
谢谢,看来大家都喜欢Alli呀。
一会儿就去买。
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。