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

阅后即焚的新鲜店面

🔗
 楼主| Mark6 2016-5-19 04:55:37 | 只看该作者
全局:
您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限 或 查看其他获取积分的方式
Unlock interview details and practice with AI
Curated Interview Questions from Top Companies
回复

使用道具 举报

🔗
jy_121 2016-5-19 05:26:36 | 只看该作者
全局:
请问最后一问的DP方程应该怎样写呢?谢谢
回复

使用道具 举报

🔗
dili7743 2016-5-19 10:22:16 | 只看该作者
全局:
jy_121 发表于 2016-5-19 05:26
请问最后一问的DP方程应该怎样写呢?谢谢

C++代码:

  1. /*
  2. * Given an array. Check whether 2 power the minimum of the array is larger then the maximum of the array.
  3. * follow up 1: (minTakesFront)     If you can take out the front elements in the array, how many steps it takes to make the rest of the array satisfying the above condition?
  4. * follow up 2: (minTakesBothEnds)  what if you can take out elements from both ends of the arrary? Then what is the minimum steps to find a subarray that satisfying the above condition?
  5. */

  6. #include <iostream>
  7. #include <string>
  8. #include <vector>
  9. #include <algorithm>
  10. #include <limits>

  11. using namespace std;

  12. bool checkPow(const int& minValue, const int& maxValue) {
  13.     errno = 0;
  14.     double pow2 = std::pow( minValue, 2 );
  15.     if ( errno == 0 ) {
  16.         //  std::pow succeeded (without overflow)
  17.         if (pow2 > maxValue) {
  18.             return true;
  19.         }
  20.     } else {
  21.         //  some error (probably overflow) with std::pow.
  22.         return false;
  23.     }   
  24.     return false;
  25. }

  26. int minTakesFront(const vector<int>& nums) {
  27.         int minValue = numeric_limits<int>::max();
  28.         int maxValue = numeric_limits<int>::min();
  29.         int s = static_cast<int>(nums.size());
  30.         int i = s - 1;
  31.         for (; i >= 0 ; --i) {   
  32.                 minValue = min(nums[i], minValue);
  33.                 maxValue = max(nums[i], maxValue);
  34.                 if (!checkPow(minValue, maxValue)) {
  35.                         break;
  36.                 }
  37.         }
  38.         if (i == s - 1) {
  39.                 return -1;
  40.         }
  41.         return i + 1;
  42. }

  43. // return minimum steps to make a subarray inside the passed in array such that 2 power the minimum is larger the maximum
  44. // or -1 if there is no such subarray
  45. int minTakesBothEnds(const vector<int>& nums) {
  46.         int s = static_cast<int>(nums.size());
  47.        
  48.         if (s == 0) {
  49.             return -1;
  50.         }
  51.        
  52.         vector<vector<int>> f(s, vector<int>(s, 0));
  53.         for (int i = 0; i < s; ++i) {
  54.             if (checkPow(nums[i], nums[i])) {
  55.                 f[i][i] = 0;
  56.             } else {
  57.                 f[i][i] = 1;
  58.             }
  59.         }
  60.        
  61.         for (int i = s - 1; i >= 0; --i) {
  62.                 int minValue = nums[i];
  63.                 int maxValue = nums[i];
  64.                 for (int j = i + 1; j < s; ++j) {
  65.                         minValue = min(nums[j], minValue);
  66.                         maxValue = max(nums[j], maxValue);
  67.                         if (checkPow(minValue, maxValue)) {
  68.                                 f[i][j] = 0;
  69.                         } else {
  70.                                 f[i][j] = min(f[i + 1][j], f[i][j - 1]) + 1;
  71.                         }
  72.                 }
  73.         }
  74.        
  75.         //if steps equal to size of the array, all elements have been taken out
  76.         if (f[0][s - 1] == s) {
  77.             return -1;
  78.         }
  79.        
  80.         return f[0][s - 1];
  81. }

  82. int main()
  83. {
  84.   vector<int> test1 = {5};
  85.   vector<int> test2 = {1};
  86.   vector<int> test3 = {5, 1, 3};
  87.   vector<int> test4 = {1, 1, 1};
  88.   vector<int> test5 = {5, 3, 1};
  89.   vector<int> test6 = {2, 5, 3, 1};
  90.   vector<int> test7 = {-2};
  91.   vector<int> test8 = {1, 5, 3};
  92.   
  93.   cout << "test1 front result : " << minTakesFront(test1) << endl;
  94.   cout << "test2 front result : " << minTakesFront(test2) << endl;
  95.   cout << "test3 front result : " << minTakesFront(test3) << endl;
  96.   cout << "test4 front result : " << minTakesFront(test4) << endl;
  97.   cout << "test5 front result : " << minTakesFront(test5) << endl;
  98.   cout << "test6 front result : " << minTakesFront(test6) << endl;
  99.   cout << "test7 front result : " << minTakesFront(test7) << endl;
  100.   cout << "test8 front result : " << minTakesFront(test8) << endl;
  101.   cout << endl;
  102.   cout << "test1 both result : " << minTakesBothEnds(test1) << endl;
  103.   cout << "test2 both result : " << minTakesBothEnds(test2) << endl;
  104.   cout << "test3 both result : " << minTakesBothEnds(test3) << endl;
  105.   cout << "test4 both result : " << minTakesBothEnds(test4) << endl;
  106.   cout << "test5 both result : " << minTakesBothEnds(test5) << endl;
  107.   cout << "test6 both result : " << minTakesBothEnds(test6) << endl;
  108.   cout << "test7 both result : " << minTakesBothEnds(test7) << endl;
  109.   cout << "test8 both result : " << minTakesBothEnds(test8) << endl;
  110. }
复制代码
回复

使用道具 举报

🔗
jy_121 2016-5-19 11:13:47 | 只看该作者
全局:

谢谢!我一开始还想f[i][i] = -1时后边怎么累加呢。。
回复

使用道具 举报

🔗
zhuhai_ZFC 2016-7-18 12:53:54 | 只看该作者
全局:
您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限 或 查看其他获取积分的方式
Unlock interview details and practice with AI
Curated Interview Questions from Top Companies
回复

使用道具 举报

🔗
sfsttz 2016-7-18 23:27:53 | 只看该作者
全局:
您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限 或 查看其他获取积分的方式
Unlock interview details and practice with AI
Curated Interview Questions from Top Companies
回复

使用道具 举报

🔗
omega094 2016-7-20 22:45:48 | 只看该作者
全局:
这个。。。。。。
删除第一个元素的操作次数难道不是直接看数组里面有多少个数是小于 pow(max(arr), 0.5) 呢。。。。。。 =_=#
(假设数组全是正数, 不过如果有负数的话也是一个思路吧。。。稍微处理下就可以。。)


补充内容 (2016-7-21 02:58):
Nevermind 我看错了。。。原来是“删除第一个元素”。。
回复

使用道具 举报

🔗
何打发123 2016-7-22 09:56:48 | 只看该作者
全局:
您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限 或 查看其他获取积分的方式
Unlock interview details and practice with AI
Curated Interview Questions from Top Companies
回复

使用道具 举报

🔗
pawprinter 2016-9-6 10:31:59 | 只看该作者
全局:
第三题,对于状态 S(i, j),先检测arr(i, j)其中的最大值和最小值是否满足,如果满足,那么S(i, j) = 0, 如果不满足,那么需要根据S(i, j - 1)和S(i + 1, j)来确定最小值
回复

使用道具 举报

全局:
您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限 或 查看其他获取积分的方式
Unlock interview details and practice with AI
Curated Interview Questions from Top Companies
回复

使用道具 举报

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

本版积分规则

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