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

GOOGLE ONSITE一道最近高频DP

 
🔗
lsy1991121 2017-3-1 05:31:09 | 只看该作者
全局:
Sliding Window解法:
  1.         public static List<String> lengthOfReward(String s) {
  2.                 List<String> res = new ArrayList<>();
  3.                 Set<String> set = new HashSet<>();
  4.                 int right = 0;
  5.                 int left = 0;
  6.                
  7.                 int countA = 0;
  8.                 int countL = 0;
  9.                
  10.                 while (right < s.length() - 1) {
  11.                         char c = s.charAt(right);
  12.                         if (c == 'A') countA++;
  13.                         else if (c == 'L') countL++;
  14.                        
  15.                         while (left < right && (countA > 1 || countL > 3)) {
  16.                                 char toDelete = s.charAt(left++);
  17.                                 if (toDelete == 'A') countA--;
  18.                                 if (toDelete == 'L') countL--;
  19.                         }
  20.                        
  21.                         String cand = s.substring(left, right + 1);
  22.                        
  23.                         if (!set.contains(cand)) {
  24.                                 res.add(cand);
  25.                                 set.add(cand);
  26.                         }
  27.                        
  28.                         right++;
  29.                 }
  30.                
  31.                 return res;
  32.         }
复制代码
回复

使用道具 举报

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

使用道具 举报

🔗
say543 2017-3-1 15:38:42 | 只看该作者
全局:
jedihy 发表于 2017-2-28 00:53
follow up的输入只有n的。


只有n是说 input 是data stream ? 不理解...
回复

使用道具 举报

🔗
xiaoxiao0801 2017-3-16 08:47:25 | 只看该作者
全局:
18844549105 发表于 2017-2-14 17:24
随手写的,不知道对不对.

楼主我超同意你。不过我更新的f[n][0][0]=f[n-1][0][0]+f[n-1][0][1]+f[n-1][0][2],求指教!
回复

使用道具 举报

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

使用道具 举报

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

使用道具 举报

🔗
codepike 2017-3-17 10:13:53 | 只看该作者
全局:
  1. int count(int n) {
  2.         // igore "A" first, and consider "O" and "L" only
  3.         // create a 2D array dp[i][j]
  4.         // dp[i][0] the number of rewardable string with length of i and the last letter is "O"
  5.         // dp[i][1] the number of rewardable string with length of i and the last letter is "L"
  6.         vector<vector<int>> dp(n+1, vector<int>{1,0});
  7.         dp[1] = {1,1};

  8.         for (auto i = 2; i <= n; ++i) {
  9.                 dp[i][0] = dp[i-1][0] + dp[i-1][1];  // the ith letter is O
  10.                 dp[i][1] = dp[i-1][0] + dp[i-2][0];  // the ith letter is L
  11.         }

  12.         int res = dp[n][0] + dp[n][1];   // case when there is no "A"
  13.         // consider all the cases with one A
  14.         // there are i letters to the left of A and n-i-1 letter to the right of A
  15.         for (int i = 0; i < n; ++i) {
  16.                 res += (dp[i][0]+dp[i][1])*(dp[n-i-1][0] + dp[n-i-1][1]);
  17.         }
  18.        
  19.         return res;
  20. }
复制代码

评分

参与人数 2大米 +15 收起 理由
mayingjie116 + 5 给你点个赞!
kennethinsnow + 10 回答的很好!

查看全部评分

回复

使用道具 举报

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

使用道具 举报

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

使用道具 举报

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

使用道具 举报

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

本版积分规则

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