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

心塞的fb电面跪经

🔗
kamibear 2017-2-5 02:58:57 | 只看该作者
全局:
第二题不是 count and say 吗?

补充内容 (2017-2-5 03:08):
记错了..并不是
回复

使用道具 举报

🔗
kamibear 2017-2-5 03:06:13 | 只看该作者
全局:
LeoJY 发表于 2017-2-5 02:49
二分的话最差应该nlgn吧? 假如所有数字都不一样,找上界下界都是lgn,然后要找n次。。。

所有数字都不一样的 那就是O1 的时间吗?
最坏还是On 吧
回复

使用道具 举报

🔗
zhhan1990 2017-2-5 04:21:43 | 只看该作者
全局:
二分最差是nlogN但是第二个N最差情况时是2,所以最后还是n
回复

使用道具 举报

🔗
 楼主| LeoJY 2017-2-5 04:48:49 | 只看该作者
全局:
zhhan1990 发表于 2017-2-4 15:21
二分最差是nlogN但是第二个N最差情况时是2,所以最后还是n

大n为啥最差是2啊……没理解你的意思……
回复

使用道具 举报

🔗
Zhenying 2017-2-14 15:04:40 | 只看该作者
全局:
感谢楼主分享。按照楼主的思路,第二题是不是这样做的?

  1. class Solution {
  2. public:
  3.     vector<pair<int, int>> countNums(vector<int> &nums) {
  4.         vector<pair<int, int>> res;
  5.         int st = 0;
  6.         while (st < (int)nums.size()) {
  7.             int ed = findLastOf(nums, st, nums[st]);
  8.             res.push_back( {nums[st], ed - st + 1} );
  9.             st = ed + 1;
  10.         }
  11.         return res;
  12.     }

  13. private:
  14.     int findLastOf(const vector<int> &nums, int st, int target) {
  15.         int left = st;
  16.         int diff = 1;
  17.         while (left + diff < (int)nums.size()) {
  18.             if (nums[left + diff] <= target) {
  19.                 left += diff;
  20.                 diff *= 2;
  21.             } else {
  22.                 break;
  23.             }
  24.         }

  25.         int right = min((int)nums.size() - 1, left + diff);
  26.         return binarySearch(nums, left, right, target);
  27.     }

  28.     int binarySearch(const vector<int> &nums, int left, int right, int target) {
  29.         while (left + 1 < right) {
  30.             int mid = left + (right - left) / 2;
  31.             if (nums[mid] <= target) {
  32.                 left = mid;
  33.             } else {
  34.                 right = mid;
  35.             }
  36.         }

  37.         if (nums[right] == target)
  38.             return right;
  39.         return left;
  40.     }
  41. };
复制代码
回复

使用道具 举报

🔗
lhh_NJU 2017-2-19 15:53:23 | 只看该作者
全局:
觉得楼主想法还是挺精妙的, 不过我觉得这个题目没啥意思, 因为直接一遍读过去一样就是average case O(n)..
回复

使用道具 举报

🔗
 楼主| LeoJY 2017-2-19 22:11:53 | 只看该作者
全局:
lhh_NJU 发表于 2017-2-19 02:53
觉得楼主想法还是挺精妙的, 不过我觉得这个题目没啥意思, 因为直接一遍读过去一样就是average case O(n)..

现在是worst case on呀
回复

使用道具 举报

🔗
bigbearlake 2017-3-1 16:32:32 | 只看该作者
全局:
第二题
  1. public class DecodeVector {

  2.     class Pair {
  3.         int c;
  4.         int count;
  5.         public Pair(int c, int count) {
  6.             this.c = c;
  7.             this.count = count;
  8.         }
  9.     }

  10.     public List<Pair> getCount(int[] arr) {
  11.         List<Pair> list = new ArrayList<>();
  12.         if (arr == null || arr.length == 0) {
  13.             return list;
  14.         }

  15.         int start = 0;
  16.         while (start < arr.length) {
  17.             int end = findLast(start, arr);
  18.             list.add(new Pair(arr[start], end - start + 1));
  19.             start = end + 1;
  20.         }

  21.         return list;
  22.     }

  23.     private int findLast(int start, int[] arr) {
  24.         int c = arr[start];
  25.         int diff = 1;
  26.         while (start + diff < arr.length && arr[start + diff] == c) {
  27.             diff <<= 1;
  28.         }
  29.         if (start + diff >= arr.length) {
  30.             return arr.length - 1;
  31.         }

  32.         int end = start + diff;
  33.         while (start + 1 < end) {
  34.             int mid = (end - start) / 2 + start;
  35.             if (arr[mid] == c) {
  36.                 start = mid;
  37.             } else {
  38.                 end = mid;
  39.             }
  40.         }
  41.         if (arr[end] == c) {
  42.             return end;
  43.         } else {
  44.             return start;
  45.         }
  46.     }


  47.     public static void main(String[] args) {
  48.         int[] arr = {1,1,1,1,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3,3,3};
  49.         DecodeVector d = new DecodeVector();
  50.         List<Pair> res = d.getCount(arr);
  51.         for (Pair p : res) {
  52.             System.out.println(p.c + ": " + p.count);
  53.         }
  54.     }
  55. }
复制代码
回复

使用道具 举报

🔗
Youknowwho 2017-6-3 19:44:42 | 只看该作者
全局:
这个 worse case 负责度是 O(NlgN) 吧
回复

使用道具 举报

🔗
fay1224 2017-6-3 22:09:25 | 只看该作者
全局:
恭喜楼主onsite 你约了哪一天了吗
回复

使用道具 举报

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

本版积分规则

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