12
返回列表 发新帖
楼主: 匿名
跳转到指定楼层
上一主题 下一主题
收起左侧

Google SDE coding 面经 (新题,求idea)

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

使用道具 举报

🔗
JoyForce 2021-1-17 14:24:52 | 只看该作者
全局:
选举那道题我的解法:
  1. public int solution(
  2.         int num_states,
  3.         int[] delegates,
  4.         int[] votes_president_A,
  5.         int[] votes_president_B,
  6.         int[] votes_Undecided)
  7. {
  8.         // return minumber number people vote A
  9.         var target = delegates.Sum();
  10.         var dp = new int[target+1];
  11.         Array.Fill(dp, int.MaxValue);
  12.         dp[0] = 0;
  13.         var votes = new int[num_states];
  14.         for (var i = 0; i < num_states; ++i) {
  15.                 // total number of votes
  16.                 var total = votes_president_A[i] + votes_president_B[i] + votes_Undecided[i];
  17.                 // votes required to win for A
  18.                 votes[i] = total / 2 + 1 - votes_president_A[i];
  19.         }
  20.        
  21.         // dp[j] = min(dp[j-delegates[i]] + votes[i], dp[j])   0 <= i < num_states
  22.         for (var i = 0; i < num_states; ++i) {
  23.                 for (var j = target; j >= 0; --j) {
  24.                         if (j >= delegates[i] &&
  25.                                 dp[j - delegates[i]] != int.MaxValue &&
  26.                                 dp[j - delegates[i]] + votes[i] < dp[j]) {
  27.                                 dp[j] = dp[j - delegates[i]] + votes[i];
  28.                         }
  29.                 }
  30.         }
  31.        
  32.         // Find the minimum votes for delegates larger than half
  33.         var result = int.MaxValue;
  34.         for (var i = target/2+1; i <= target; ++i) {
  35.                 result = Math.Min(result, dp[i]);
  36.         }
  37.        
  38.         return result == int.MaxValue ? -1 : result;
  39. }
复制代码
回复

使用道具 举报

地里匿名用户
🔗
匿名用户-D9UCI  2021-1-22 16:17:27
花擦 好牛逼 多谢分享!!
回复

使用道具 举报

🔗
shchen0808 2021-2-8 04:12:07 | 只看该作者
全局:
0-1 背包问题,用C++实现了一下,如果A不可能赢一个州的选举人票(比如,B已经赢了该州),直接跳过。
没有编译和测试,欢迎大家指出问题。


  1. /*
  2. check if A can win election
  3. find min votes needed for A to win
  4. calculate min votes needed to win each state
  5. calculate min votes needed to win majority of electors
  6. 0/1 knapsack problem
  7. Time Complexity: O(m * n)
  8. Space Complexity: O(m * n)
  9. m: number of states
  10. n: total number of electors
  11. */
  12. int minVotesToWin(int numStates,
  13. const vector<int>& electors,
  14. const vector<int>& votesA,
  15. const vector<int>& votesB,
  16. const vector<int>& votesUndecided) {
  17.         // validate parameters
  18.         if (numStates <= 0 || numStates != electors.size() || numStates != votesA.size() || …) {
  19.                 throw invalid_arguments(“”);
  20.         }

  21.         int totalElectors = accumulate(electors.begin(), electors.end(), 0);
  22.         int electorsA = 0;
  23.         vector<int> minVotes(numStates, INT_MAX);
  24.         for (int i = 0; i < numStates; ++i) {
  25.                 int votes = votesA[i] + votesB[i] + votesUndecided[i];
  26.                 int majority = votes / 2 + 1;
  27.                 if (votesA[i] + votesUndecided[i] >= majority) {
  28.                         electorsA += electors[i];
  29.                         minVotes[i] = majority - votesA[i];
  30.                 }
  31.         }
  32.         if (electorsA < totalElectors / 2 + 1) {
  33.                 return INT_MAX;
  34.         }

  35.         // 0-1 knapsack
  36.         // table[i][j] : min votes needed to win j electors from first i states
  37.         // table[i][j] = max(table[i-1][j], table[i-1][j-minVotes[i-1]] + minVotes[i-1])
  38.         vector<vector<int>> table(numStates, vector<int>(totalElectors + 1, INT_MAX));
  39.         for (int i = 0; i < numStates; ++i) {
  40.                 table[i][0] = 0;
  41.         }
  42.         for (int j = 0; j <= totalVotes; ++j) {
  43.                 table[0][j] = 0;
  44.         }
  45.         for (int i = 1; i < numStates; ++i) {
  46.                 for (int j = 1; j <= totalElectors; ++j) {
  47.                         int votes1 = INT_MAX, votes2 = INT_MAX;
  48.                         if (j >= electors[i] && minVotes[i-1] != INT_MAX) {
  49.                                 votes1 = table[i-1][j-electors[i-1]] + minVotes[i-1];
  50.                         }
  51.                         votes2 = table[i-1][j];
  52.                         table[i][j] = min(votes1, votes2);
  53.                 }
  54.         }
  55.         int result = INT_MAX;
  56.         for (int j = totalVotes / 2 + 1; j <= totalVotes; ++j) {
  57.                 result = min(result, minVotes[j]);
  58.         }
  59.         return result;
  60. }
复制代码
回复

使用道具 举报

🔗
侵权 2021-5-24 10:10:30 | 只看该作者
全局:
请问undecided是啥意思啊?delagate是这个state一共可以投几票?
回复

使用道具 举报

🔗
侵权 2021-5-24 10:35:23 | 只看该作者
全局:
JoyForce 发表于 2021-1-17 14:24
选举那道题我的解法:
[mw_shl_code=java,true]public int solution(
        int num_states,

很clear,谢谢!
回复

使用道具 举报

🔗
侵权 2021-5-24 10:37:15 | 只看该作者
全局:
JoyForce 发表于 2021-1-17 14:24
选举那道题我的解法:
[mw_shl_code=java,true]public int solution(
        int num_states,

有一个小问题 是不是没考虑votes[i] < 0的情况?
回复

使用道具 举报

🔗
侵权 2021-5-24 10:49:37 | 只看该作者
全局:
JoyForce 发表于 2021-1-17 14:24
选举那道题我的解法:
[mw_shl_code=java,true]public int solution(
        int num_states,

如平票咋办?比如ab各一半,那这个state其实就没有任何贡献给任一方
回复

使用道具 举报

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

本版积分规则

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