2022-08-27-Tesla笔试-87分

踩坑:会有性能分

图片说明
图片说明

// 第一题
// from collections import Counter
// def solution(A):
//     c = Counter(A)
//     for i, cnt in c.items():
//         if cnt%2 >0:
//             return False
//     return True

// 第2题
int solution2(string &S, vector<int> &C) {
    int cost=0;
    for(unsigned int i=0;i<S.size();){
        unsigned int j=i;
        int maxCost = C[i];
        while(j<S.size()&&S[i]==S[j]){
            cost += C[j];
            if(C[j]>maxCost)
                maxCost = C[j];
            j++;
        }
        cost -= maxCost;
        i=j;
    }
    return cost;
}


// 第3题
#include <functional>
#include <memory>
#include<iostream>
using namespace std;
#include <vector>
int dfs(const string s, const vector<int>& A, const vector<vector<int> >& children, int& maxLen, int no){
    int l1=0,l2=0,t;
    for(auto cno: children[no]){
        t=dfs(s,A,children,maxLen,cno);
        if(s[cno]==s[no])
            continue;
        if(t>=l1){
            l2=l1;
            l1=t;
        }else if(t>l2)
            l2=t;
    }
    maxLen=max(maxLen, l1+l2+1);
    return l1+1;
}

int solution3(string &S, vector<int> &A) {
    int n=S.length();
    if(n<2) return n;
    vector<vector<int> > children(n);
    for(int i=1;i<n;i++){
        children[A[i]].push_back(i);
    }
    int maxLen=0;
    dfs(S,A,children,maxLen,0);
    return maxLen;
}

int main()
{
    string s="abbab";
    vector<int> a(5);
    a[0]=-1;
    a[1]=a[2]=a[3]=0;
    a[4]=2;
    // cout<< solution3(s,a);

    a.resize(4);
    a[0]=-1;
    a[1]=2;
    a[2]=0;
    a[3]=1;
    s="abab";
    cout<< solution3(s,a);
    return 0;
}
#特斯拉##笔试##23秋招##23届##软件开发#
全部评论
第三问 可以用全局变量存maxlen,dfs下一层之间比较(字符串长度-下一个子节点)和maxlen,如果小于直接剪枝,能过75%performance,task score有86,可以看我空间
1 回复 分享
发布于 2022-08-27 23:48 江西
您好,我想问问笔试开摄像头吗?
1 回复 分享
发布于 2022-08-27 23:18 福建
厉害,87分很不错了
1 回复 分享
发布于 2022-08-27 18:57 陕西
直接就能看到分数吗?为什么我刚刚做的没有看到
点赞 回复 分享
发布于 2023-05-19 00:52 上海
你好 请问笔试限制语言吗
点赞 回复 分享
发布于 2023-02-15 14:03 广东
请问笔试有时长限制吗?邮件似乎没写
点赞 回复 分享
发布于 2022-09-18 00:46 北京
强啊 第一题理解错意思了 这样例太简单了没发现错误
点赞 回复 分享
发布于 2022-08-28 13:41 浙江

相关推荐

notbeentak...:孩子,说实话,选择很重要,可能你换一个方向会好很多,但是现在时间不太够了,除非准备春招
点赞 评论 收藏
分享
评论
6
17
分享

创作者周榜

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