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

google面经求人品

🔗
bobzhang2004 2015-12-7 06:25:40 | 只看该作者
全局:
写了下最大乘积的代码,但不知道为什么O(n)的方法怎么证明?
  1. public class MaxProduct {
  2.         public static void main(String[] args) {
  3.                 System.out.println("1 " + maxProduct(100));
  4.                 System.out.println("2 " + maxProductN(100));
  5.         }
  6.        
  7.         // O(n^2);
  8.         public static long maxProduct(int n) {
  9.                 if (n == 0) {
  10.                         return 0;
  11.                 }
  12.                 long[] dp = new long[n + 1];
  13.                 dp[1] = 1;
  14.                 for (int i = 2; i <= n; i++) {
  15.                         long res = i;
  16.                         for (int j = 1; j < i; j++) {
  17.                                 res = Math.max(res, (i - j) * dp[j]);
  18.                         }
  19.                         dp[i] = res;
  20.                 }
  21.                 return dp[n];
  22.         }
  23.         public static long maxProductN(int n) {
  24.                 if (n == 0) {
  25.                         return 0;
  26.                 }
  27.                 long[] dp = new long[n + 4];
  28.                 dp[1] = 1;
  29.                 dp[2] = 2;
  30.                 dp[3] = 3;
  31.                 for (int i = 4; i <= n; i++) {
  32.                         dp[i] = Math.max(2 * dp[i - 2], 3 * dp[i - 3]);
  33.                 }
  34.                 return dp[n];
  35.         }

  36. }
复制代码
回复

使用道具 举报

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

使用道具 举报

🔗
luofeidream 2015-12-7 07:20:37 | 只看该作者
全局:
楼主请问字典那道题,googl或者goeogle算不算1个typo?如果这样的话字典树就不能用了吧
回复

使用道具 举报

🔗
bobzhang2004 2015-12-7 08:10:42 | 只看该作者
全局:
kakaChen 发表于 2015-10-30 21:23
我是用的字典树,然后逐个往下找,如果有一个不match,就拿别的替换继续向下找。

写了下第二轮的函数,欢迎指教,感觉这里用trie和map在时间复杂度上没有区别啊,只是空间上有优势而已。
  1. public class TypeOne {

  2.         public static void main(String[] args) {
  3.                 String[] strs = {"google", "facebook", "amazon"};
  4.                 TypeOne to = new TypeOne(strs);
  5.                 System.out.println(to.containsOneType("google"));
  6.                 System.out.println(to.containsOneType("geogle"));
  7.                 System.out.println(to.containsOneType("geogla"));
  8.         }
  9.         private Trie trie;
  10.         public TypeOne(String[] strs) {
  11.                 trie = new Trie();
  12.                 for (String str : strs) {
  13.                         trie.insert(str);
  14.                 }
  15.         }
  16.        
  17.         public boolean containsOneType(String str) {
  18.                 for (int i = 0; i < str.length(); i++) {
  19.                         for (char c = 'a'; c <= 'z'; c++) {
  20.                                 if (c == str.charAt(i)) {
  21.                                         continue;
  22.                                 }
  23.                                 String newStr = str.substring(0, i) + c + str.substring(i + 1);
  24.                                 if (trie.search(newStr)) {
  25.                                         return true;
  26.                                 }
  27.                         }
  28.                 }
  29.                
  30.                 return false;
  31.         }
  32.        
  33.         class Trie {
  34.                 class TrieNode {
  35.                         char c;
  36.                         boolean isLeaf = false;
  37.                         HashMap<Character, TrieNode> children = new HashMap<Character, TrieNode>();

  38.                         public TrieNode() {
  39.                         }

  40.                         public TrieNode(char c) {
  41.                                 this.c = c;
  42.                         }
  43.                 }

  44.                 private TrieNode root;

  45.                 public Trie() {
  46.                         root = new TrieNode();
  47.                 }

  48.                 public void insert(String word) {
  49.                         TrieNode cur = null;
  50.                         HashMap<Character, TrieNode> curChildren = root.children;
  51.                         for (int i = 0; i < word.length(); i++) {
  52.                                 char c = word.charAt(i);
  53.                                 if (curChildren.containsKey(c)) {
  54.                                         cur = curChildren.get(c);
  55.                                 } else {
  56.                                         TrieNode tmp = new TrieNode(c);
  57.                                         curChildren.put(c, tmp);
  58.                                         cur = tmp;
  59.                                 }
  60.                                 curChildren = cur.children;
  61.                                 if (i == word.length() - 1) {
  62.                                         cur.isLeaf = true;
  63.                                 }
  64.                         }
  65.                 }

  66.                 public boolean search(String word) {
  67.                         TrieNode node = searchNode(word);
  68.                         return node != null && node.isLeaf;
  69.                 }

  70.                 public TrieNode searchNode(String word) {
  71.                         TrieNode cur = null;
  72.                         HashMap<Character, TrieNode> curChildren = root.children;
  73.                         for (int i = 0; i < word.length(); i++) {
  74.                                 char c = word.charAt(i);
  75.                                 if (curChildren.containsKey(c)) {
  76.                                         cur = curChildren.get(c);
  77.                                 } else {
  78.                                         return null;
  79.                                 }
  80.                                 curChildren = cur.children;
  81.                         }

  82.                         return cur;
  83.                 }
  84.                
  85.         }
  86. }
复制代码
回复

使用道具 举报

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

本版积分规则

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