商汤科技2020.08.20后端开发笔试(1h AK)

第一题:找Good,每一个字符都只能使用一次,
思路:贪心策略,给已经拼过的Good的字符设置一个访问标记即可;
样例:
输入:
Goo23good Gooddd
123 GoodoodGGoooddjfhjdGGooo3dkdggggGoood0123

输出:
2
5

代码:
#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
#define pii pair<int, int>
#define pll pair<ll, ll>
#define mp make_pair
#define ios ios::sync_with_stdio(false),cin.tie(0);

template <typename T>
inline void read(T &x) {char ch=getchar(); int f=1; x=0; while(!isdigit(ch)){if(ch=='-')f *= -1; ch=getchar();} while(isdigit(ch)) {x = (x<<1) + (x<<3) + (ch^48); ch=getchar();} x*=f;}
ll qpow(ll x, ll y) { ll a=1, b=x; while(y){if(y&0x1) a*=b; b*=b; y>>=1;} return a;}

const int maxn = 100005;
bool visit[maxn];

int main(void)
{
#ifndef ONLINE_JUDGE
    freopen("a.txt", "r", stdin);
#endif

    string s;
    while (getline(cin, s)) {
        memset(visit, 0, sizeof(visit));
        int n = s.size();
        int count = 0;
        for(int i = 0; i < n; ++i) {
            if(s[i] != 'G') continue;

            int onum = 0;
            for(int j = i+1; j < n; ++j) {
                if(visit[j]) continue;

                if(s[j] == 'o') {
                    ++onum;
                    if(onum <= 2) visit[j] = true;
                } else if(onum >= 2 && s[j] == 'd') {
                    visit[j] = true;
                    ++count;
                    break;
                }
            }
        }
        cout << count << endl;
    }
    return 0;
} 

第二题:矩阵中,找最长的严格递增路径
思路:记忆化搜索,f[i][j]表示从[i,j]开始的最长的长度,遍历二维数组,如果已经计算过改点的值,直接返回即可
样例:
输入:
3 3
9 1 4
6 2 8
5 5 7

输出:
5
代码:
#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
#define pii pair<int, int>
#define pll pair<ll, ll>
#define mp make_pair
#define ios ios::sync_with_stdio(false),cin.tie(0);

template <typename T>
inline void read(T &x) {char ch=getchar(); int f=1; x=0; while(!isdigit(ch)){if(ch=='-')f *= -1; ch=getchar();} while(isdigit(ch)) {x = (x<<1) + (x<<3) + (ch^48); ch=getchar();} x*=f;}
ll qpow(ll x, ll y) { ll a=1, b=x; while(y){if(y&0x1) a*=b; b*=b; y>>=1;} return a;}

int m;
int n;
vector<vector<int>> dirs = {{1,0},{0,1},{-1,0},{0,-1}};

int dfs(vector<vector<int>>& arr, vector<vector<int>>& f, int x, int y) {
    if(x < 1 || y < 1 || x > m || y > n) return 0;
    if(f[x][y] > 0) return f[x][y];

    int curMax = 0;
    for(auto d : dirs) {
        int i = x + d[0];
        int j = y + d[1];

        if(i < 1 || j < 1 || i > m || j > n || arr[i][j] <= arr[x][y]) continue;
        curMax = max(curMax, dfs(arr, f, i, j));
    }
    f[x][y] = curMax + 1;
    return f[x][y];
}

int main(void)
{
#ifndef ONLINE_JUDGE
    freopen("b.txt", "r", stdin);
#endif

    while(cin >> m >> n) {
        vector<vector<int>> arr(m+1, vector<int>(n+1, 0));
        vector<vector<int>> f(m+1, vector<int>(n+1, 0));
        for(int i = 1; i <= m; ++i) {
            for(int j = 1; j <= n; ++j) cin >> arr[i][j];
        }
        int ans = 0;
        for(int i = 1; i <= m; ++i) {
            for(int j = 1; j <= n; ++j) {
                if(f[i][j] == 0) dfs(arr, f, i, j);
                ans = max(ans, f[i][j]);
            }
        }
        cout << ans << endl;
    }
    return 0;
} 

第三题:n个区间,求最少删除多少个区间,可以让它们彼此不重叠
思路:本质就是一个最长递增子序列长度,但是首先要预处理数据,把区间段排序成升序,然后找最长递增的子序列即可。
答案就是 n - maxLen, 即:最少删除的区间
样例:
输入:
[ [1,2], [2,3], [3,4], [1,3] ]

输出:
3
代码:
class Solution {
public:
    int eraseOverlapIntervals(vector<vector<int> >& intervals) {
        int n = intervals.size();
        if(n <= 1) return 0;
        sort(intervals.begin(), intervals.end(), [](const vector<int>& l, const vector<int>& r) {
            if(l[0] == r[0]) return l[1] < r[1];
            return l[0] < r[0];
        });

        vector<int> f(n, 1);
        int ans = 1;
        for(int i = 1; i < n; ++i) {
            for(int j = 0; j < i; ++j) {
                if(intervals[i][0] >= intervals[j][1]) {
                    f[i] = max(f[i], f[j] + 1);
                    ans = max(ans, f[i]);
                }
            }
        }
        return n - ans;
    }
};

#秋招##笔试题目##C/C++##商汤科技#
全部评论
楼主面试了吗?我20号参加笔试,现在还在测评中,想问问是不是凉了
点赞 回复 分享
发布于 2020-08-30 08:24
咋都这么厉害啊,好像又没戏了
点赞 回复 分享
发布于 2020-08-20 22:23
等一手贴答案
点赞 回复 分享
发布于 2020-08-20 22:08
第一题什么意思??
点赞 回复 分享
发布于 2020-08-20 21:44
人均Ak哈哈
点赞 回复 分享
发布于 2020-08-20 21:44
2.8/3.0 感觉这次选择题好难
点赞 回复 分享
发布于 2020-08-20 21:42
我第一题只能过20%😂
点赞 回复 分享
发布于 2020-08-20 21:21
终于AK一次,要哭了
点赞 回复 分享
发布于 2020-08-20 21:14
楼主你好,请问你是什么岗位?开发的话,是Java方向还是C++方向?或者其他语言方向~
点赞 回复 分享
发布于 2020-08-20 21:07

相关推荐

刚刷到字节跳动官方发的消息,确实被这波阵仗吓了一跳。在大家还在纠结今年行情是不是又“寒冬”的时候,字节直接甩出了史上规模最大的转正实习计划——ByteIntern。咱们直接看几个最硬的数,别被花里胡哨的宣传词绕晕了。首先是“量大”。全球招7000多人是什么概念?这几乎是把很多中型互联网公司的总人数都给招进来了。最关键的是,这次的资源分配非常精准:研发岗给了4800多个Offer,占比直接超过六成。说白了,字节今年还是要死磕技术,尤其是产品和AI领域,这对于咱们写代码的同学来说,绝对是今年最厚的一块肥肉。其次是大家最关心的“转正率”。官方直接白纸黑字写了:整体转正率超过50%。这意味着只要你进去了,不划水、正常干,每两个人里就有一个能直接拿校招Offer。对于2027届(2026年9月到2027年8月毕业)的同学来说,这不仅是实习,这简直就是通往大厂的快捷通道。不过,我也得泼盆冷水。坑位多,不代表门槛低。字节的实习面试出了名的爱考算法和工程实操,尤其是今年重点倾斜AI方向,如果你简历里有和AI相关的项目,优势还是有的。而且,转正率50%也意味着剩下那50%的人是陪跑的,进去之后的考核压力肯定不小。一句话总结:&nbsp;27届的兄弟们,别犹豫了。今年字节这是铁了心要抢提前批的人才,现在投递就是占坑。与其等到明年秋招去千军万马挤独木桥,不如现在进去先占个工位,把转正名额攥在手里。
喵_coding:别逗了 50%转正率 仔细想想 就是转正与不转正
字节7000实习来了,你...
点赞 评论 收藏
分享
03-03 19:02
已编辑
东华理工大学 Node.js
点赞 评论 收藏
分享
评论
4
13
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务