【leetcode】第 178 场周赛

230 篇文章 0 订阅
14 篇文章 0 订阅

比赛链接: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。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值