【leetcode】第 178 場周賽

比賽鏈接:https://leetcode-cn.com/contest/weekly-contest-178/

賽後總結

這場比賽打得不太順,可能是和最近寫代碼思路很卡有關?這周刷的是回溯法,刷到二維平面還處在很難受的階段。

不過提升是緩慢的,量變積累質變嘛。

第一題:總體思路還是清晰的,用了大概十分鐘。還可以更快更熟練。

第二題:這道題不難,和第一題的思路類似,只是過程複雜一些。選擇容器和使用的時候耗時最久。

第三題:這題本該很快拿下,但是思路不夠清晰,想得簡單了。遍歷的兩層嵌套不熟,所以沒有往那個方向想,一直在糾結如何用一層遍歷實現。

flag:階段目標是在穩定2題的10次內升到3題。

不足和改進

1.思路轉代碼不夠快。平時做題要有時間觀念。

2.容器選擇和使用不夠熟。今早分析清楚,多練習。

3.樹的兩層遍歷嵌套不熟。多刷題。

4.又緊張了。平時做題給自己定時。

優點

1.穩住了2題。

2.在狀態不好的情況下,沒有放棄。

 

題目分析

1.【easy】5344. How Many Numbers Are Smaller Than the Current Number

Given the array nums, for each nums[i] find out how many numbers in the array are smaller than it. That is, for each nums[i] you have to count the number of valid j's such that j != i and nums[j] < nums[i].

Return the answer in an array.

Example 1:

Input: nums = [8,1,2,2,3]
Output: [4,0,1,1,3]
Explanation: 
For nums[0]=8 there exist four smaller numbers than it (1, 2, 2 and 3). 
For nums[1]=1 does not exist any smaller number than it.
For nums[2]=2 there exist one smaller number than it (1). 
For nums[3]=2 there exist one smaller number than it (1). 
For nums[4]=3 there exist three smaller numbers than it (1, 2 and 2).

Example 2:

Input: nums = [6,5,4,8]
Output: [2,1,0,3]

Example 3:

Input: nums = [7,7,7,7]
Output: [0,0,0,0]

Constraints:

  • 2 <= nums.length <= 500
  • 0 <= nums[i] <= 100

題目鏈接:https://leetcode-cn.com/problems/how-many-numbers-are-smaller-than-the-current-number/

思路

總體思路是:統計->排序->輸出。

因爲本題是根據key來排序的,所以可以用map來輔助。

class Solution {
public:
    vector<int> smallerNumbersThanCurrent(vector<int>& nums) {
        vector<int> res;
        if(nums.size()==0) return res;
        map<int, vector<int>> rec;
        for(int i=0; i<nums.size();++i){
            rec[nums[i]].push_back(i);
        }
        int k = 0;
        res = vector<int>(nums.size(), 0);
        for(auto iter=rec.begin(); iter!=rec.end();++iter){
            int cnt = 0;
            for(int i=0;i<iter->second.size();++i){
                res[(iter->second)[i]] = k;
                ++cnt;
            }
            k+=cnt;
        }
        return res;
    }
};

 

2.【medium】5345. Rank Teams by Votes

In a special ranking system, each voter gives a rank from highest to lowest to all teams participated in the competition.

The ordering of teams is decided by who received the most position-one votes. If two or more teams tie in the first position, we consider the second position to resolve the conflict, if they tie again, we continue this process until the ties are resolved. If two or more teams are still tied after considering all positions, we rank them alphabetically based on their team letter.

Given an array of strings votes which is the votes of all voters in the ranking systems. Sort all teams according to the ranking system described above.

Return a string of all teams sorted by the ranking system.

Example 1:

Input: votes = ["ABC","ACB","ABC","ACB","ACB"]
Output: "ACB"
Explanation: Team A was ranked first place by 5 voters. No other team was voted as first place so team A is the first team.
Team B was ranked second by 2 voters and was ranked third by 3 voters.
Team C was ranked second by 3 voters and was ranked third by 2 voters.
As most of the voters ranked C second, team C is the second team and team B is the third.

Example 2:

Input: votes = ["WXYZ","XYZW"]
Output: "XWYZ"
Explanation: X is the winner due to tie-breaking rule. X has same votes as W for the first position but X has one vote as second position while W doesn't have any votes as second position. 

Example 3:

Input: votes = ["ZMNAGUEDSJYLBOPHRQICWFXTVK"]
Output: "ZMNAGUEDSJYLBOPHRQICWFXTVK"
Explanation: Only one voter so his votes are used for the ranking.

Example 4:

Input: votes = ["BCA","CAB","CBA","ABC","ACB","BAC"]
Output: "ABC"
Explanation: 
Team A was ranked first by 2 voters, second by 2 voters and third by 2 voters.
Team B was ranked first by 2 voters, second by 2 voters and third by 2 voters.
Team C was ranked first by 2 voters, second by 2 voters and third by 2 voters.
There is a tie and we rank teams ascending by their IDs.

Example 5:

Input: votes = ["M","M","M","M"]
Output: "M"
Explanation: Only team M in the competition so it has the first rank.

Constraints:

  • 1 <= votes.length <= 1000
  • 1 <= votes[i].length <= 26
  • votes[i].length == votes[j].length for 0 <= i, j < votes.length.
  • votes[i][j] is an English upper-case letter.
  • All characters of votes[i] are unique.
  • All the characters that occur in votes[0] also occur in votes[j] where 1 <= j < votes.length.

題目鏈接:https://leetcode-cn.com/problems/rank-teams-by-votes/

思路

總體思路和第一題類似:統計->排序->輸出。

這邊是用velue來排序的,所以不能用map,可以用priority_queue創建pair和自定義比較函數。

也可以在統計完之後使用排序算法排序。

class Solution {
public:
    struct cmp{
        bool operator()(pair<char, vector<int>> a, pair<char, vector<int>> b){
            int len = a.second.size();
            for(int i=0; i<len; ++i){
                if(a.second[i]!=b.second[i]) return a.second[i] < b.second[i];
            }
            return a.first > b.first;
        }
    };
    string rankTeams(vector<string>& votes) {
        int num = votes.size();
        if(num==0) return "";
        int len = votes[0].size();
        priority_queue< pair<char, vector<int>> , vector<pair<char, vector<int>>>, cmp> rec;
        unordered_map<char, vector<int>> m;
        
        for(int i=0; i<len; ++i){
            m[votes[0][i]] = vector<int>(len, 0);
        }
        for(int i=0; i<num; ++i){
            for(int j=0; j<len; ++j){
                ++m[votes[i][j]][j];
            }
        }
        for(auto iter=m.begin(); iter!=m.end(); ++iter){
            rec.push(make_pair(iter->first, iter->second));
        }
        
        string res;
        while(!rec.empty()){
            res += (rec.top()).first;
            rec.pop();
        }
        return res;
    }
};

 

3.【medium】5346. Linked List in Binary Tree

Given a binary tree root and a linked list with head as the first node. 

Return True if all the elements in the linked list starting from the head correspond to some downward path connected in the binary tree otherwise return False.

In this context downward path means a path that starts at some node and goes downwards.

Example 1:

Input: head = [4,2,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3]
Output: true
Explanation: Nodes in blue form a subpath in the binary Tree.  

Example 2:

Input: head = [1,4,2,6], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3]
Output: true

Example 3:

Input: head = [1,4,2,6,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3]
Output: false
Explanation: There is no path in the binary tree that contains all the elements of the linked list from head

Constraints:

  • 1 <= node.val <= 100 for each node in the linked list and binary tree.
  • The given linked list will contain between 1 and 100 nodes.
  • The given binary tree will contain between 1 and 2500 nodes.

題目鏈接:https://leetcode-cn.com/problems/linked-list-in-binary-tree/

思路

兩層遍歷嵌套:第一層找滿足條件的根節點;第二層找以此爲是否能構成路徑。

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    bool isSubPath(ListNode* head, TreeNode* root) {
        if(!root && head) return false;
        if(!head) return true;
        if(root->val==head->val){
            bool l = trace(head->next, root->left);
            bool r = trace(head->next, root->right);
            if (l || r) return true;
        }
        return isSubPath(head, root->left) || isSubPath(head, root->right);
    }
    bool trace(ListNode* head, TreeNode* root){
        if(!head) return true;
        if(!root && head) return false;
        if(head->val==root->val) return trace(head->next, root->left) || trace(head->next, root->right);
        else return false;
    }
};

優化

對第一層遍歷街上KMP算法的思路,記錄next點,能加快運行效率。

這個挖個坑,等第一輪刷完之後回來看kmp。

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