楼主: shidaohang
跳转到指定楼层
上一主题 下一主题
收起左侧

Stanford Design and Analysis of Algorithm I 第二期 Week#6 自由讨论

🔗
seekinghelp 2012-7-29 22:43:21 | 只看该作者

Problem Set-6

全局:
Problem Set-6
Question 1
Suppose we use a hash function h to hash n distinct keys into an array T of length m. Assuming simple uniform hashing --- that is, with each key mapped independently and uniformly to a random bucket --- what is the expected number of keys that get mapped to the first bucket? More precisely, what is the expected cardinality of the set {k:h(k)=1}.
Your Answer                Score        Explanation
n/m                2.00        Use linearity of expectation, with one indicator variable for each key. The probability that one key hashes to the first bucket is 1/m, and by linearity of expectation the total expected number of keys that hash to the first bucket is just n/m.
Total                2.00 / 2.00        
Question 2
You are given a binary tree (via a pointer to its root) with n nodes, which may or may not be a binary search tree. How much time is necessary and sufficient to check whether or not the tree satisfies the search tree property?
Your Answer                Score        Explanation
Θ(n)                2.00        For the lower bound, if there is a violation of the search tree property, you might need to examine all of the nodes to find it (in the worst case).
Total                2.00 / 2.00        
Question 3
You are given a binary tree (via a pointer to its root) with n nodes. As in lecture, let size(x) denote the number of nodes in the subtree rooted at the node x. How much time is necessary and sufficient to compute size(x) for every node x of the tree?
Your Answer                Score        Explanation
Θ(n)                2.00        For the lower bound, note that a linear number of quantities need to be computed. For the upper bound, recursively compute the sizes of the left and right subtrees, and use the formula size(x) = 1 + size(y) + size(z) from lecture.
Total                2.00 / 2.00        
Question 4
Which of the following is not a property that you expect a well-designed hash function to have?
Your Answer                Score        Explanation
The hash function should "spread out" every data set (across the buckets/slots of the hash table).                2.00        As discussed in lecture, unfortunately, there is no such hash function.
Total                2.00 / 2.00        
Question 5
Suppose we relax the third invariant of red-black trees to the property that there are no three reds in a row. That is, if a node and its parent are both red, then both of its children must be black. Call these relaxed red-black trees. Which of the following statements is not true?
Your Answer                Score        Explanation
Every binary search tree can be turned into a relaxed red-black tree (via some coloring of the nodes as black or red).                2.00        A chain with four nodes is a counterexample.
Total                2.00 / 2.00        
回复

使用道具 举报

🔗
smgjzh 2012-8-1 12:21:29 | 只看该作者
全局:
贪得无厌的问一下:
Final 有人帮助下不?最后两周都没时间看,匆匆扫了遍,完全没信心!
回复

使用道具 举报

全局:
smgjzh 发表于 2012-8-1 12:21
贪得无厌的问一下:
Final 有人帮助下不?最后两周都没时间看,匆匆扫了遍,完全没信心!

final很简单,只要你认真做了前几周的problem set,final上的题就完全没有问题。而且涉及最后两周的问题都是概念性的,没有计算,比较好突击。祝好运!
回复

使用道具 举报

🔗
gongluan 2012-8-5 21:01:14 | 只看该作者
全局:
第一题

  1. #include <iostream>
  2. #include <ext/hash_set>
  3. #include <iterator>
  4. #include <algorithm>

  5. using namespace std;
  6. using namespace __gnu_cxx;

  7. int main()
  8. {
  9.     hash_set<int> hs;
  10.     istream_iterator<int> iter(cin), eos;
  11.     while (iter != eos)
  12.         hs.insert(*iter++);
  13.     size_t count = 0;
  14.     for (int i = 2500; i <= 4000; i++) {
  15.         hash_set<int>::const_iterator iter = hs.begin();
  16.         while (iter != hs.end()) {
  17.             if (*iter * 2 != i && hs.find(i - *iter) != hs.end()) {
  18.                 count++;
  19.                 break;
  20.             }
  21.             ++iter;
  22.         }
  23.     }
  24.     cout << count << endl;
  25.     return 0;
  26. }
复制代码

第二题

  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>

  4. using namespace std;

  5. int main()
  6. {
  7.     vector<int> lower_heap;
  8.     vector<int> higher_heap;
  9.     int small_value = 0;
  10.     int large_value = 0;
  11.     int sum = 0;
  12.     int val;
  13.     cin >> val;
  14.     lower_heap.push_back(val);
  15.     sum += val;
  16.     while (cin >> val) {
  17.         if (val < lower_heap[0]) {
  18.             lower_heap.push_back(val);
  19.             push_heap(lower_heap.begin(), lower_heap.end());
  20.             if (lower_heap.size() - higher_heap.size() >= 2) {
  21.                 pop_heap(lower_heap.begin(), lower_heap.end());
  22.                 higher_heap.push_back(*(lower_heap.end() - 1));
  23.                 push_heap(higher_heap.begin(), higher_heap.end(), greater<int>());
  24.                 lower_heap.pop_back();
  25.             }
  26.         } else {
  27.             higher_heap.push_back(val);
  28.             push_heap(higher_heap.begin(), higher_heap.end(), greater<int>());
  29.             if (higher_heap.size() - lower_heap.size() >= 2) {
  30.                 pop_heap(higher_heap.begin(), higher_heap.end(), greater<int>());
  31.                 lower_heap.push_back(*(higher_heap.end() - 1));
  32.                 push_heap(lower_heap.begin(), lower_heap.end());
  33.                 higher_heap.pop_back();
  34.             }
  35.         }
  36.         if (lower_heap.size() >= higher_heap.size()) {
  37.             sum += lower_heap[0];
  38.         } else if (lower_heap.size() < higher_heap.size()) {
  39.             sum += higher_heap[0];
  40.         }
  41.     }
  42.     cout << sum % 10000 << endl;
  43.     return 0;
  44. }

复制代码


回复

使用道具 举报

🔗
Shuang7 2013-3-5 23:26:41 | 只看该作者
全局:
终于全部搞定,median那道题居然是因为数据读取出了问题导致最终计算结果不对,还是程序里缺少assert啊!
release版基本都是一秒以内出结果~
更多图片 小图 大图
组图打开中,请稍候......
回复

使用道具 举报

🔗
cloudsyk3 2013-8-10 10:31:15 | 只看该作者
全局:
这一轮的题做了些变动,
The file contains 1 million integers, both positive and negative (there might be some repetitions!).


Your task is to compute the number of target values t in the interval [-10000,10000] (inclusive) such that there are distinct numbers x,y in the input file that satisfy x+y=t. (NOTE: ensuring distinctness requires a one-line addition to the algorithm from lecture.)
回复

使用道具 举报

🔗
zhujl1991 2013-8-11 14:20:24 | 只看该作者
全局:
cloudsyk3 发表于 2013-8-10 10:31
这一轮的题做了些变动,
The file contains 1 million integers, both positive and negative (there migh ...

请问algo1_programming_prob_2sum.txt文件我为什么打开之后是没有空格、没换行的一连串的数字呢?
回复

使用道具 举报

🔗
frog 2013-8-14 03:50:20 | 只看该作者
全局:
zhujl1991 发表于 2013-8-11 14:20
请问algo1_programming_prob_2sum.txt文件我为什么打开之后是没有空格、没换行的一连串的数字呢?

不影响程序按回车读入Array【】;
试试随便选中几行粘到别的文件里,这些数组就自动换行了。
回复

使用道具 举报

🔗
frog 2013-8-14 03:51:15 | 只看该作者
全局:
Programming-6 #1 有点变动,我用Java写的Code跑了整整两个小时才出结果,论坛里有牛人自建hashmap用了40秒跑完1Million的数据,佩服佩服。

最终的考试只给一次提交的机会,有没有同学分享一下题目呀?不用给答案,让我们看看考题就多谢多谢啦。
回复

使用道具 举报

🔗
zhujl1991 2013-8-14 20:38:03 | 只看该作者
全局:
frog 发表于 2013-8-14 03:50
不影响程序按回车读入Array【】;
试试随便选中几行粘到别的文件里,这些数组就自动换行了。

嗯。我知道。。我就是想问问为什么会这样。。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表