hulu筆試

 

1417. 稱重問題
將金幣儘量分成均勻三堆。如果正好平分就正常操作,餘一個就是(3k,3k)上秤,但是最壞的情況是要找的金幣在3k+1裏,餘兩個就是(3k+1,3k+1)上秤,最壞的情況還是在3k+1裏。

class Solution {
public:
    int minimumtimes(int n) {
        // Write your code here
        if (n<=1) return 0;
        int res=0;
        while (n>1) {
            int tmp=n%3;
            n=(n/3) + (tmp==0?0:1);
            res+=1;
        } 
        return res;
    }
};

1003. 二叉樹剪枝
類似後序遍歷,先處理左右子樹,如果有全0的直接設置成NULL,不需要從根節點向下多次判斷,也不用特意返回其他flag。

class Solution {
public:
    TreeNode * has_one(TreeNode * root) {
        if (root==NULL) return NULL;
        root->left=has_one(root->left);
        root->right=has_one(root->right);
        if (root->left==NULL && root->right==NULL && root->val==0) return NULL;
        else return root;
        
    }
    TreeNode * pruneTree(TreeNode * root) {
        // Write your code here
        if (root==NULL) return NULL;
        return has_one(root);
    }
};

1559. 取數求和
注意有可能溢出,還要在每一個可能超過1000000007的地方取餘。

class Solution {
public:
    int takeTheElementAndQueryTheSum(vector<int> &arr) {
        // Write your code here
        if (arr.size()<=1) return -1;
        long long res=0;
        long prefix=arr[0];
        for (int i=1;i<arr.size();i++) {
            int tmp=(prefix*arr[i])%1000000007;
            res=(res+tmp)%1000000007;
            prefix=(prefix+arr[i])%1000000007;
        }
        return res;
    }
};

997. 打印組織結構圖 

class people {
public:
    string ups;
    string title;
    string year;
    vector<string> downs;
    //自定義構造函數必須記得同時定義默認無參構造函數
    people(){
        ups="";
        title="";
        year="";
        vector<string> tmp;
        downs=tmp;
    }
    people(string _ups, string _title, string _year, vector<string> _downs):
    ups(_ups),title(_title),year(_year),downs(_downs) {}
};
class Solution {
public:
    void DFShelper(string p, int level, vector<string> &res, map<string,people> &nodes) {
        string tmp="";
        for (int k=0;k<level;k++) tmp=tmp+"-";
        tmp=tmp+p;
        tmp=tmp+" (";
        tmp=tmp+nodes[p].title;
        tmp=tmp+") ";
        tmp=tmp+nodes[p].year;
        res.push_back(tmp);
        //孩子節點還要排序
        sort(nodes[p].downs.begin(),nodes[p].downs.end());
        for (int i=0;i<nodes[p].downs.size(); i++) {
            DFShelper(nodes[p].downs[i],level+1,res,nodes);
        }
    }
    vector<string> getOrganization(vector<vector<string>> &relationship) {
        // Write your code here
        map<string,people> nodes;
        string leader="";
        //先創建節點
        for (int i=0;i<relationship.size();i++) {
            vector<string> tmp;
            people p=people(relationship[i][1],relationship[i][2],relationship[i][3],tmp);
            nodes[relationship[i][0]]=p;
            //找到根節點
            if (relationship[i][1]=="NULL") leader=relationship[i][0];
        }
        //
        for (int i=0;i<relationship.size();i++) {
            string up=relationship[i][1];
            nodes[up].downs.push_back(relationship[i][0]);
        }
        
        vector<string> res;
        int level=0;
        //看題目要用深搜
        DFShelper(leader,level,res,nodes);
        return res;
    }
};

996. 最大斜率直線
如果暴力求解會超時。可以考慮,如果所有點按x軸座標大小排序,最大斜率的點一定是相鄰的兩個點。假設對於x,y,z三個點,如果xz的斜率大於xy的斜率,那麼yz斜率一定大於xz斜率。所以先排序就好。

struct point_index {
    int index;
    Point p;
};
bool cmp(point_index a, point_index b) {
    return a.p.x<b.p.x;
};
class Solution {
public:
    /**
     * @param points: The points set
     * @return: Return the point pair
     */
    vector<int> getPoints(vector<Point> &points) {
        // Write your code here
        vector<int> res;
        if (points.size()==2) {res.push_back(0); res.push_back(1); return res;}
        vector<point_index> ps;
        for (int i=0;i<points.size();i++) {
            point_index tmp;
            tmp.index=i;
            tmp.p=points[i];
            ps.push_back(tmp);
        }
        sort(ps.begin(),ps.end(),cmp);
        float max_angle=INT_MIN;
        int index1=0;
        int index2=0;
        for (int i=0;i<ps.size()-1;i++) {
            int x0=ps[i].p.x;
            int y0=ps[i].p.y;
            int x1=ps[i+1].p.x;
            int y1=ps[i+1].p.y;
            float angle;
            angle=(y1-y0)/(x1-x0);
            if (angle>max_angle) {
                max_angle=angle;
                index1=ps[i].index;
                index2=ps[i+1].index;
            }
        }
        if (index1>index2) swap(index1,index2);
        res.push_back(index1);
        res.push_back(index2);
        return res;
    }
};

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章