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

Elements of Programming Interviews 白班编程记录,求挑刺求反馈

 
🔗
 楼主| 大木虫 2018-11-30 23:19:25 | 只看该作者
全局:
我的心理素质还是有一些不过关,等公司面试结果期间无心做题,天天老惦记着公司能不能要我,这样不是很好。我要练习move on。
回复

使用道具 举报

🔗
 楼主| 大木虫 2018-11-30 23:20:21 | 只看该作者
全局:
Number of Islands
总结了一个近似模板的写法,DFS/BFS图类
  1. class Solution {
  2. public:
  3.     int numIslands(vector<vector<char>>& grid) {
  4.         /* 0. MISC */
  5.         
  6.         /* 1. prep */
  7.         int count = 0;
  8.         
  9.         /* 2. key algo */
  10.         for(int i = 0; i < grid.size(); ++i){
  11.             for(int j = 0; j < grid[i].size(); ++j){
  12.                 if(grid[i][j] == '1'){
  13.                     ++count;
  14.                     BFS(grid, {i, j});
  15.                 }
  16.             }
  17.         }
  18.         
  19.         /* 3. answer */
  20.         return count;
  21.     }
  22.    
  23.     void BFS(vector<vector<char> >& grid, const pair<int, int>& loc){
  24.         /* 0. MISC */
  25.         
  26.         /* 1. prep */
  27.         queue<pair<int, int> > bfsQueue;
  28.         bfsQueue.emplace(loc);
  29.         grid[loc.first][loc.second] = 0;
  30.         
  31.         vector<pair<int, int> > directions = {
  32.             pair<int, int>(-1, 0),
  33.             pair<int, int>(1, 0),
  34.             pair<int, int>(0, 1),
  35.             pair<int, int>(0, -1)
  36.         };
  37.         
  38.         /* 2. key algo */
  39.         while(!bfsQueue.empty()){
  40.             auto node = bfsQueue.front();
  41.             bfsQueue.pop();
  42.             
  43.             for(auto dir: directions){
  44.                 pair<int, int> child(node.first + dir.first, node.second + dir.second);
  45.                
  46.                 if(!InRange(grid, child))continue;
  47.                 if(grid[child.first][child.second] == '0')continue;
  48.                
  49.                 grid[child.first][child.second] = '0';
  50.                 bfsQueue.emplace(move(child));
  51.             }
  52.         }
  53.         
  54.         /* 3. answer */
  55.         /* no answer to return*/        
  56.     }
  57.    
  58.     string Encode(const pair<int, int>& loc){
  59.         return loc.first + " " + loc.second;
  60.     }
  61.    
  62.     bool InRange(const vector<vector<char> >& grid, const pair<int, int>& loc){
  63.         return  loc.first >= 0 && loc.first < grid.size() &&
  64.                 loc.second >= 0 && loc.second < grid[loc.first].size();
  65.     }
  66.    
  67. };
复制代码
回复

使用道具 举报

🔗
 楼主| 大木虫 2018-12-1 00:19:08 | 只看该作者
全局:
297. Serialize and Deserialize Binary Tree
这次的重点是写了迭代的解法,用stack,写了一个小时左右

  1. /**
  2. * Definition for a binary tree node.
  3. * struct TreeNode {
  4. *     int val;
  5. *     TreeNode *left;
  6. *     TreeNode *right;
  7. *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
  8. * };
  9. */
  10. class Codec {
  11. public:

  12.     // Encodes a tree to a single string.
  13.     string serialize(TreeNode* root) {
  14.         /* 0. MISC */
  15.         if(!root)return "N ";
  16.         
  17.         /* 1. prep */
  18.         string answer;
  19.         stack<TreeNode*> dfsStack;
  20.         dfsStack.emplace(root);
  21.         
  22.         /* 2. key algo */
  23.         while(!dfsStack.empty()){
  24.             auto node = dfsStack.top();
  25.             dfsStack.pop();
  26.             
  27.             if(node){
  28.                 answer += to_string(node->val) + " ";
  29.             }else answer += "N ";
  30.             
  31.             if(node){
  32.                 dfsStack.emplace(node->right);
  33.                 dfsStack.emplace(node->left);
  34.             }
  35.         }
  36.         
  37.         /* 3. answer */
  38.         return answer;
  39.     }

  40.     // Decodes your encoded data to tree.
  41.     TreeNode* deserialize(string data) {
  42.         /* 0. MISC */
  43.         if(data == "N ")return NULL;
  44.         
  45.         /* 1. prep */
  46.         stack<pair<TreeNode*, int>> treeStack;
  47.         int index = 0;
  48.         TreeNode* answer = NULL;
  49.         bool start = true;
  50.         
  51.         /* 2. key algo */
  52.         while(index < data.size()){
  53.             string valStr = NextVal(data, index);
  54.             
  55.             TreeNode* node;
  56.                
  57.             if(valStr != "N")node = new TreeNode(stoi(valStr));
  58.             else node = NULL;
  59.             
  60.             if(start){
  61.                 answer = node;
  62.                 start = false;
  63.             }
  64.             
  65.             if(!treeStack.empty()){
  66.                 if(treeStack.top().second == 0){
  67.                     treeStack.top().second++;
  68.                     treeStack.top().first->left = node;
  69.                 }else{
  70.                     auto parent = treeStack.top();
  71.                     (parent.first)->right = node;
  72.                     treeStack.pop();
  73.                 }
  74.             }
  75.             
  76.             if(node)treeStack.emplace(node, 0);            
  77.         }
  78.         
  79.         /* 3. answer */
  80.         return answer;
  81.     }
  82.    
  83.     string NextVal(const string& data, int& index){
  84.         int tmp = index;
  85.         index = data.find(" ", index);
  86.         return data.substr(tmp, index++ - tmp);
  87.     }
  88. };

  89. // Your Codec object will be instantiated and called as such:
  90. // Codec codec;
  91. // codec.deserialize(codec.serialize(root));
复制代码
回复

使用道具 举报

🔗
 楼主| 大木虫 2018-12-1 03:18:24 | 只看该作者
全局:
317. Shortest Distance from All Buildings (hard)
直观的BFS题目,写起来比较冗长,55分钟AC

  1. class Solution {
  2. public:
  3.     int shortestDistance(vector<vector<int>>& grid){
  4.         /* 0. MISC */
  5.         
  6.         /* 1. prep */
  7.         vector<vector<int> > totalDistGrid(grid.size(), vector<int>(grid[0].size(), 0));
  8.         vector<vector<int> > reachableBuildings(grid.size(), vector<int>(grid[0].size(), 0));
  9.         int numBuilding = CountBuilding(grid);
  10.         
  11.         /* 2. key algo */
  12.         for(int i = 0; i < grid.size(); ++i){
  13.             for(int j = 0; j < grid[i].size(); ++j){
  14.                 if(grid[i][j] == 1){
  15.                     UpdateGridBFS(grid, totalDistGrid, reachableBuildings, {i, j});
  16.                 }
  17.             }
  18.         }
  19.         
  20.         /* 3. answer */
  21.         return MinQualifiedElement(grid, totalDistGrid, reachableBuildings, numBuilding);
  22.     }
  23.    
  24.     void UpdateGridBFS( const vector<vector<int>>& grid,
  25.                         vector<vector<int> >& totalDistGrid,
  26.                         vector<vector<int> >& reachableBuildings,
  27.                         const pair<int, int>& loc){
  28.         /* 0. MISC */
  29.         
  30.         /* 1. prep */
  31.         vector<vector<int> > distGrid(grid.size(), vector<int>(grid[0].size(), -1));
  32.         vector<vector<int> > visited(grid.size(), vector<int>(grid[0].size(), 0));
  33.         
  34.         queue<pair<int, int> > bfsQueue;
  35.         bfsQueue.emplace(loc);
  36.         visited[loc.first][loc.second] = 1;
  37.         distGrid[loc.first][loc.second] = 0;
  38.         
  39.         vector<pair<int, int> > directions =
  40.         {
  41.             pair<int, int>(-1, 0),
  42.             pair<int, int>(1, 0),
  43.             pair<int, int>(0, -1),
  44.             pair<int, int>(0, 1)
  45.         };
  46.         
  47.         /* 2. key algo */
  48.         while(!bfsQueue.empty()){
  49.             auto node = bfsQueue.front();
  50.             bfsQueue.pop();
  51.             
  52.             for(auto dir: directions){
  53.                 pair<int, int> neighbor(node.first + dir.first, node.second + dir.second);
  54.                 if(!InRange(grid, neighbor))continue;
  55.                 if(grid[neighbor.first][neighbor.second] != 0)continue;
  56.                 if(visited[neighbor.first][neighbor.second] != 0)continue;
  57.                
  58.                 visited[neighbor.first][neighbor.second] = 1;
  59.                 distGrid[neighbor.first][neighbor.second] = distGrid[node.first][node.second] + 1;
  60.                
  61.                 bfsQueue.emplace(neighbor);
  62.             }            
  63.         }
  64.         
  65.         /* 3. answer */
  66.         MatrixAdd(reachableBuildings, move(visited));  
  67.         MatrixAdd(totalDistGrid, move(distGrid));  
  68.     }
  69.    
  70.     void MatrixAdd(vector<vector<int>>& base, const vector<vector<int>>& addition){
  71.         if(base.empty() || addition.empty())return;
  72.         if(base.size() != addition.size() || base[0].size() != addition[0].size())return;
  73.         
  74.         for(int i = 0; i < base.size(); ++i){
  75.             for(int j = 0; j < base[i].size(); ++j){
  76.                 base[i][j] += addition[i][j];
  77.             }
  78.         }
  79.     }
  80.    
  81.     int MinQualifiedElement(const vector<vector<int>>& grid,
  82.                             const vector<vector<int>>& distGrid,
  83.                             const vector<vector<int>>& buildingCountGrid,
  84.                             int numBuilding){
  85.         int ans = -1;
  86.         for(int i = 0; i < distGrid.size(); ++i){
  87.             for(int j = 0; j < distGrid[i].size(); ++j){
  88.                 if(buildingCountGrid[i][j] != numBuilding)continue;
  89.                 if(grid[i][j] != 0)continue;
  90.                 if(ans == -1)ans = distGrid[i][j];
  91.                 else ans = min(ans, distGrid[i][j]);
  92.             }
  93.         }
  94.         return ans;
  95.     }
  96.    
  97.     bool InRange(const vector<vector<int>>& grid, const pair<int, int>& loc){
  98.         return  loc.first >= 0 && loc.first < grid.size() &&
  99.                 loc.second >= 0 && loc.second < grid[loc.first].size();
  100.     }
  101.    
  102.     int CountBuilding(const vector<vector<int>>& grid){
  103.         int ans = 0;
  104.         for(auto row: grid){
  105.             for(int n: row){
  106.                 if(n == 1)++ans;
  107.             }
  108.         }
  109.         return ans;
  110.     }
  111.    
  112.     void Print2DMatrix(const vector<vector<int> >& matrix){
  113.         cout << "matrix: " << endl;
  114.         for(auto row: matrix){
  115.             for(int n: row)
  116.                 cout << n << " ";
  117.             cout << endl;
  118.         }            
  119.     }

  120. };
复制代码
回复

使用道具 举报

🔗
bubblebobabee 2018-12-1 04:02:16 | 只看该作者
全局:
给楼主点赞。能坚持这么久的人在战拖区其实寥寥无几……祝楼主拿到心仪的大offer!

评分

参与人数 1大米 +3 收起 理由
大木虫 + 3 谢谢!

查看全部评分

回复

使用道具 举报

🔗
 楼主| 大木虫 2018-12-1 04:21:33 | 只看该作者
全局:
279. Perfect Squares
写了两种解法:DP, BFS,总共用时21分钟

DP 解法(44ms)
  1. class Solution {
  2. public:
  3.     int numSquares(int n) {
  4.         /* 0. MISC */
  5.         
  6.         /* 1. prep */
  7.         vector<int> dpMatrix(n + 1, -1);
  8.         dpMatrix[0] = 0;
  9.         
  10.         /* 2. key algo */
  11.         for(int i = 0; i < dpMatrix.size(); ++i){
  12.             for(int j = 1; i + j * j <= n; ++j){
  13.                 int nextStep = i + j * j;
  14.                 if(dpMatrix[nextStep] == -1)
  15.                     dpMatrix[nextStep] = dpMatrix[i] + 1;
  16.                 else dpMatrix[nextStep] = min(dpMatrix[nextStep], dpMatrix[i] + 1);
  17.             }
  18.         }
  19.         
  20.         /* 3. answer */
  21.         return dpMatrix.back();
  22.     }
  23. };
复制代码


BFS 解法 (4ms)
  1. class Solution {
  2. public:
  3.     int numSquares(int n) {
  4.         /* 0. MISC */
  5.         
  6.         /* 1. prep */
  7.         queue<int> bfsQueue;
  8.         vector<int> memo(n + 1, -1);
  9.         
  10.         memo[0] = 0;
  11.         bfsQueue.emplace(0);
  12.             
  13.         /* 2. key algo */
  14.         while(!bfsQueue.empty()){
  15.             int node = bfsQueue.front();
  16.             bfsQueue.pop();
  17.             int val = memo[node];
  18.             
  19.             for(int i = 1; node + i * i <= n; ++i){
  20.                 int child = node + i * i;
  21.                
  22.                 if(child == n)return val + 1;
  23.                
  24.                 if(memo[child] != -1)continue;
  25.                
  26.                 memo[child] = val + 1;
  27.                 bfsQueue.emplace(child);
  28.             }
  29.         }
  30.             
  31.         /* 3. answer */
  32.         return -1; /* error case */
  33.     }
  34. };
复制代码

回复

使用道具 举报

🔗
 楼主| 大木虫 2018-12-4 05:22:53 | 只看该作者
全局:
679. 24 Game (hard)
开始佛系写题了,能写几道写几道吧
这题不难,就是繁琐
  1. class Solution {
  2. public:
  3.     bool judgePoint24(vector<int>& nums) {
  4.         /* 0. MISC */
  5.         
  6.         /* 1. prep */
  7.         vector<float> numFloat = {nums[0], nums[1], nums[2], nums[3]};
  8.         bool answer = false;
  9.         
  10.         /* 2. key algo */
  11.         Permute24(numFloat, answer, 0);
  12.         
  13.         /* 3. answer */
  14.         return answer;
  15.     }
  16.    
  17.     void Permute24(vector<float>& nums, bool& answer, int index){
  18.         static int count = 0;
  19.         if(answer)return;
  20.         if(index == nums.size() - 1){
  21.             answer |= Get24(nums);
  22.             return;
  23.         }
  24.         for(int i = index; i < nums.size(); ++i){
  25.             swap(nums[index], nums[i]);
  26.             Permute24(nums, answer, index + 1);
  27.             swap(nums[index], nums[i]);
  28.         }
  29.     }
  30.    
  31.     bool Get24(const vector<float>& nums){
  32.         string opCollection = "+-*/";
  33.         
  34.         bool ans = false;
  35.         
  36.         for(char op1: opCollection){
  37.             for(char op2: opCollection){
  38.                 for(char op3: opCollection){
  39.                     string opStr;
  40.                     opStr += op1;
  41.                     opStr += op2;
  42.                     opStr += op3;

  43.                     ans = SetPriority24(nums, opStr);
  44.                     if(ans)return ans;
  45.                 }
  46.             }
  47.         }
  48.         
  49.         return ans;
  50.     }
  51.    
  52.     bool SetPriority24(const vector<float>& nums, string opStr){
  53.         vector<string> prioritySet = {"00", "10", "20", "01", "11", "21"};
  54.         for(string priority: prioritySet){
  55.             float result = Result(nums, opStr, priority);
  56.             
  57.             if(abs(result - 24.0) < 0.00001){
  58.                 return true;
  59.             }

  60.         }
  61.         return false;
  62.     }
  63.    
  64.     float Result(const vector<float>& nums, string opStr, const string& priority){
  65.         vector<float> stage1, stage2;
  66.         string op1, op2;
  67.         float result;        
  68.         
  69.         for(int i = 0; i < nums.size(); ++i){
  70.             if(i == priority[0] - '0'){
  71.                 stage1.emplace_back(Calc(nums[i], nums[i+1], opStr[i]));
  72.                 opStr[i] = '0';
  73.                 i++;
  74.             }
  75.             else {
  76.                 stage1.emplace_back(nums[i]);
  77.             }
  78.         }
  79.         op1 = ConstructOp(opStr);
  80.         
  81.         for(int i = 0; i < stage1.size(); ++i){
  82.             if(i == priority[1] - '0'){
  83.                 stage2.emplace_back(Calc(stage1[i], stage1[i+1], op1[i]));
  84.                 op1[i] = '0';
  85.                 ++i;
  86.             }
  87.             else {
  88.                 stage2.emplace_back(stage1[i]);
  89.             }
  90.         }

  91.         op2 = ConstructOp(op1);
  92.         
  93.         float ans = Calc(stage2[0], stage2[1], op2[0]);
  94.         
  95.         return ans;
  96.     }
  97.    
  98.     string ConstructOp(const string& opStr){
  99.         string ans;
  100.         for(char c: opStr){
  101.             if(c != '0')ans += c;
  102.         }
  103.         return ans;
  104.     }
  105.    
  106.     float Calc(float num1, float num2, char op){
  107.         if(op == '+')return num1 + num2;
  108.         if(op == '-')return num1 - num2;
  109.         if(op == '*')return num1 * num2;
  110.         if(op == '/' && num2 != 0)return num1 / num2;
  111.         return 1;
  112.     }
  113.    
  114.     template <class T>
  115.     void printVec(const vector<T>& vec){
  116.         for(T element: vec)cout << element << " ";
  117.         cout << endl;
  118.     }
  119.    
  120. };
复制代码
回复

使用道具 举报

🔗
 楼主| 大木虫 2018-12-4 10:09:24 | 只看该作者
全局:
295. Find Median from Data Stream
继续佛系,这题原来在EPI上写过,现在在LC上写一遍
  1. class MedianFinder {
  2. public:
  3.     /** initialize your data structure here. */
  4.     MedianFinder() {
  5.         
  6.     }
  7.    
  8.     void addNum(int num) {
  9.         minHeap.emplace(num);
  10.         maxHeap.emplace(minHeap.top());
  11.         minHeap.pop();
  12.         if(maxHeap.size() - minHeap.size() > 1){
  13.             minHeap.emplace(maxHeap.top());
  14.             maxHeap.pop();
  15.         }

  16.     }
  17.    
  18.     double findMedian() {
  19.         if(minHeap.size() == maxHeap.size())
  20.             return (minHeap.top() + maxHeap.top()) / (double)2;
  21.         else return maxHeap.top();
  22.     }
  23.    
  24.     priority_queue<int, vector<int>, greater<int> > minHeap;
  25.     priority_queue<int> maxHeap;
  26. };

  27. /**
  28. * Your MedianFinder object will be instantiated and called as such:
  29. * MedianFinder obj = new MedianFinder();
  30. * obj.addNum(num);
  31. * double param_2 = obj.findMedian();
  32. */
复制代码
回复

使用道具 举报

🔗
 楼主| 大木虫 2018-12-4 10:10:14 | 只看该作者
全局:
感觉最近老在挑最水的hard写。。。然后标榜自己写了一堆hard。。。这样自我膨胀是不行的
回复

使用道具 举报

🔗
ly_67 2018-12-4 12:42:37 | 只看该作者
全局:
大木虫 发表于 2018-8-3 08:54
今天给一个门卫大叔话15分钟讲明白了二分查找,给另一个门卫大哥花30分钟讲了 Leetcode Edit Distance 的  ...

太6了,这是什么骚操作
回复

使用道具 举报

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

本版积分规则

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