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

Akin Capital - Campus: C++ Jr Dev #3- 2017

全局:

2017(4-6月) 码农类General 硕士 全职@akunacapital - 网上海投 - 在线笔试  | | Pass | 应届毕业生

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
  1. #include <iostream>
  2. #include <vector>
  3. #include <map>
  4. #include <unordered_map>
  5. using namespace std;

  6. class Solution {
  7. public:
  8.         pair<int, int> p1(vector<int>& nums) {
  9.                 if (nums.empty()) {
  10.                         return make_pair(0, 0);
  11.                 }
  12.                 int sum = 0, m = 0;
  13.                 for (int i = 0, n = nums.size(); i < n; i++) {
  14.                         if (i % 2 == 0) {
  15.                                 sum += nums.at(i);
  16.                                 m++;
  17.                         }
  18.                 }
  19.                 return make_pair(sum, floor((double)sum / (double)m));
  20.         }

  21.         vector<int> p2(vector<int>& nums, int k) {
  22.                 if (nums.empty() or k <= 0) {
  23.                         return {};
  24.                 }
  25.                 map<int, int> h;
  26.                 for (const auto &i : nums) {
  27.                         h[i]++;
  28.                 }
  29.                 vector<int> t;
  30.                 for (const auto &i : h) {
  31.                         if (i.second == k) {
  32.                                 t.push_back(i.first);
  33.                         }
  34.                 }
  35.                 for (const auto &i : t) {
  36.                         h.erase(i);
  37.                 }
  38.                 vector<int> result;
  39.                 for (const auto &i : h) {
  40.                         for (int j = 0; j < i.second; j++) {
  41.                                 result.push_back(i.first);
  42.                         }
  43.                 }
  44.                 return result;
  45.         }

  46.         bool p3(pair<int, int> A, pair<int, int> B, pair<int, int> P, pair<int, int> Q) {
  47.                 pair<int, int> a, b, c, d;
  48.                 a = make_pair(P.first - A.first, P.second - A.second);
  49.                 b = make_pair(Q.first - A.first, Q.second - A.second);
  50.                 c = make_pair(P.first - B.first, P.second - B.second);
  51.                 d = make_pair(Q.first - B.first, Q.second - B.second);
  52.                 int x = a.first * b.second - a.second * b.first, y = c.first * d.second - c.second * d.first;
  53.                 return (x < 0 and y < 0) or (x > 0 and y > 0);
  54.         }

  55.         int p4(vector<int>& nums) {
  56.                 unordered_map<int, int> h;
  57.                 for (const auto &i : nums) {
  58.                         h[i]++;
  59.                 }
  60.                 map<int, vector<int>> treemap;
  61.                 for (const auto &i : h) {
  62.                         treemap[i.second].push_back(i.first);
  63.                 }
  64.                 return prev(prev(end(treemap)))->second.front();
  65.         }

  66.         int p5(vector<int>& nums, int k) {
  67.                 int a = INT_MAX, b = INT_MAX;
  68.                 for (const auto &i : nums) {
  69.                         if (a == INT_MAX) {
  70.                                 a = i;
  71.                                 continue;
  72.                         }
  73.                         if (b == INT_MAX) {
  74.                                 int d1 = abs(a - k), d2 = abs(i - k);
  75.                                 if (d1 <= d2) {
  76.                                         b = i;
  77.                                         continue;
  78.                                 }
  79.                                 b = a;
  80.                                 a = i;
  81.                                 continue;
  82.                         }
  83.                         int d1 = abs(a - k), d2 = abs(b - k), d3 = abs(i - k);
  84.                         if (d3 <= d1) {
  85.                                 b = a;
  86.                                 a = i;
  87.                                 continue;
  88.                         }
  89.                         if (d3 <= d2) {
  90.                                 b = i;
  91.                                 continue;
  92.                         }
  93.                 }
  94.                 return a + b;
  95.         }
  96. };

  97. int main(void) {
  98.         Solution solution;

  99.         vector<int> nums1 = {1, -2, 2, 3, -3, 4, -5};
  100.         pair<int, int> result1 = solution.p1(nums1);
  101.         cout << result1.first << ',' << result1.second << '\t';
  102.         nums1 = {1, 2, 3};
  103.         result1 = solution.p1(nums1);
  104.         cout << result1.first << ',' << result1.second << '\n';

  105.         vector<int> nums2 = {4, 4, 4, 3, 2, 1};
  106.         int n2 = 3;
  107.         vector<int> result2 = solution.p2(nums2, n2);
  108.         for (const auto &i : result2) {
  109.                 cout << i << '\t';
  110.         }
  111.         cout << '\n';

  112.         pair<int, int> A, B, P, Q;
  113.         A = make_pair(1, -1);
  114.         B = make_pair(1, 1);
  115.         P = make_pair(0, 0);
  116.         Q = make_pair(2, 0);
  117.         cout << boolalpha << solution.p3(A, B, P, Q) << '\t';
  118.         A = make_pair(1, 1);
  119.         B = make_pair(1, -1);
  120.         P = make_pair(2, 0);
  121.         Q = make_pair(0, 0);
  122.         cout << boolalpha << solution.p3(A, B, P, Q) << '\t';
  123.         A = make_pair(1, -1);
  124.         B = make_pair(1, 1);
  125.         P = make_pair(2, 0);
  126.         Q = make_pair(2, 2);
  127.         cout << boolalpha << solution.p3(A, B, P, Q) << '\t';
  128.         A = make_pair(1, -1);
  129.         B = make_pair(1, 1);
  130.         P = make_pair(1, 0);
  131.         Q = make_pair(1, 2);
  132.         cout << boolalpha << solution.p3(A, B, P, Q) << '\t';
  133.         A = make_pair(-1, 1);
  134.         B = make_pair(1, 1);
  135.         P = make_pair(-2, 1);
  136.         Q = make_pair(1, 1);
  137.         cout << boolalpha << solution.p3(A, B, P, Q) << '\t';
  138.         A = make_pair(-1, 0);
  139.         B = make_pair(1, 0);
  140.         P = make_pair(1, 0);
  141.         Q = make_pair(1, 1);
  142.         cout << boolalpha << solution.p3(A, B, P, Q) << '\t';
  143.         A = make_pair(-1, 0);
  144.         B = make_pair(1, 0);
  145.         P = make_pair(-1, 1);
  146.         Q = make_pair(1, 1);
  147.         cout << boolalpha << solution.p3(A, B, P, Q) << '\t';
  148.         cout << '\n';

  149.         vector<int> nums4 = {1, -2, 2, -3, -3, 4, -5, -2, -4};
  150.         cout << solution.p4(nums4) << '\t';
  151.         nums4 = {0, 1, 2, 2, 2, 2, 1, 0, 5, 1};
  152.         cout << solution.p4(nums4) << '\t';

  153.         vector<int> nums5 = {1, -2, 2, -3, -3, 4, -5, -2, -4};
  154.         int n4 = -1;
  155.         cout << solution.p5(nums5, n4) << '\t';
  156.         nums5 = {-2, -2, -1, -1, 1, 1, 2, 3};
  157.         n4 = 0;
  158.         cout << solution.p5(nums5, n4) << '\t';
  159.         nums5 = {-15, -14, -9, -28, -17, 0, 6, 7, -6, -29};
  160.         n4 = 5;
  161.         nums5 = {21, 6, 27, 18};
  162.         n4 = 15;
  163.         cout << solution.p5(nums5, n4) << '\t';
  164.         cout << solution.p5(nums5, n4) << '\n';

  165.         cout << "\nPassed All\n";
  166.         return 0;
  167. }
复制代码

补充内容 (2017-4-19 06:4
您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限查看其他获取积分的方式
Unlock interview details and practice with AI
Curated Interview Questions from Top Companies
6:43):

过掉全部test

上一篇:求ebay search组面经
下一篇:wish wish wish
🔗
 楼主| LeetCodeOJ 2017-4-19 06:43:24 | 只看该作者
全局:
过掉全部test
回复

使用道具 举报

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

本版积分规则

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