【leetcode】第 179 场周赛

230 篇文章 0 订阅
9 篇文章 0 订阅

赛后反思

这场有点可惜,差一点能完成flag。

第1题:超级简单题,主要是题意理解。

第2题:难在题目理解,编程上是一些小trick。

第3题:树递归的题目,但是从叶子向根递归,题目出得不错,当时没有想到。

第4题:看到题就没有思路,暂时先不分析。

缺点&改进:

1.做题量太少,很多类型的题只做过基础版本,还没遇到比较灵活的,一旦遇到就显得脑子转不过来。

2.继续减少低级错误。

3.对于较难题思路不是很清晰,写的时候往往分不清设计变量的含义和目的。

 

题目

1.【easy】1374. Generate a String With Characters That Have Odd Counts

Given an integer nreturn a string with n characters such that each character in such string occurs an odd number of times.

The returned string must contain only lowercase English letters. If there are multiples valid strings, return any of them.  

Example 1:

Input: n = 4
Output: "pppz"
Explanation: "pppz" is a valid string since the character 'p' occurs three times and the character 'z' occurs once. Note that there are many other valid strings such as "ohhh" and "love".

Example 2:

Input: n = 2
Output: "xy"
Explanation: "xy" is a valid string since the characters 'x' and 'y' occur once. Note that there are many other valid strings such as "ag" and "ur".

Example 3:

Input: n = 7
Output: "holasss"

Constraints:

  • 1 <= n <= 500

题目链接:https://leetcode-cn.com/problems/generate-a-string-with-characters-that-have-odd-counts/

思路

写几个例子能很快找到数学规律:

1)奇数 = 1+1+奇数

2)偶数 = 1+奇数

只要根据这个规律就能随意构造string,再加一个点边界样例判断。

class Solution {
public:
    string generateTheString(int n) {
        if(n==0)return "";
        string res = "a";
        if(n==1) return res;
        if(n%2==0){
            for(int i=0; i<n-1;++i) res+="b";
        }
        else{
            res+="b";
            for(int i=0; i<n-2; ++i) res+="c";
        }
        return res;
    }
};

 

2.【medium】1375. Bulb Switcher III

There is a room with n bulbs, numbered from 1 to n, arranged in a row from left to right. Initially, all the bulbs are turned off.

At moment k (for k from 0 to n - 1), we turn on the light[k] bulb. A bulb change color to blue only if it is on and all the previous bulbs (to the left) are turned on too.

Return the number of moments in which all turned on bulbs are blue.

Example 1:

 

Input: light = [2,1,3,5,4]
Output: 3
Explanation: All bulbs turned on, are blue at the moment 1, 2 and 4.

Example 2:

Input: light = [3,2,4,1,5]
Output: 2
Explanation: All bulbs turned on, are blue at the moment 3, and 4 (index-0).

Example 3:

Input: light = [4,1,2,3]
Output: 1
Explanation: All bulbs turned on, are blue at the moment 3 (index-0).
Bulb 4th changes to blue at the moment 3.

Example 4:

Input: light = [2,1,4,3,6,5]
Output: 3

Example 5:

Input: light = [1,2,3,4,5,6]
Output: 6

Constraints:

  • n == light.length
  • 1 <= n <= 5 * 10^4
  • light is a permutation of  [1, 2, ..., n]

题目链接:https://leetcode-cn.com/problems/bulb-switcher-iii/

思路

首先需要理解题意:所有等变蓝仅出现在 所有亮着的灯前面的灯也都亮着。样例3就是一个典型例子。

每次亮灯都需要检查一下是否具备变蓝的条件,为了加快判断,需要一些存储变量:

1)bool数组,记录等是否点亮;

2)int变量,记录当前亮着的灯数量;

2)int变量,记录当前连续亮着的等的最大位置。如果新点亮的灯位置不是和最大位置相邻,则肯定不满足条件;如果相邻,则向后找到新的亮灯的最大位置更新,对比新位置代表的连续亮灯数是否和总灯数一样——一样则变蓝,不一样说明后面还有亮灯 不满足变蓝条件。

class Solution {
public:
    int numTimesAllBlue(vector<int>& light) {
        int len = light.size();
        if(len==0) return 0;
        bool lig[len] = {false};
        int res = 0;
        int open = -1;
        int num = 0;
        for(int i=0; i<len; ++i){
            ++num;
            int change = 0;
            int no = light[i]-1;
            bool blue = true;
            lig[no] = true;
            if(open == no-1){
                blue = true;
                open = no;
                change = no+1;
                for(int j=no+1;j<len;++j){
                    if(lig[j]){
                        open = j;
                        ++change;
                    }else break;
                }
                if(change>=num){
                    ++res;
                }
            }
        }
        return res;
    }
};

 

3.【medium】1376. Time Needed to Inform All Employees

A company has n employees with a unique ID for each employee from 0 to n - 1. The head of the company has is the one with headID.

Each employee has one direct manager given in the manager array where manager[i] is the direct manager of the i-th employee, manager[headID] = -1. Also it's guaranteed that the subordination relationships have a tree structure.

The head of the company wants to inform all the employees of the company of an urgent piece of news. He will inform his direct subordinates and they will inform their subordinates and so on until all employees know about the urgent news.

The i-th employee needs informTime[i] minutes to inform all of his direct subordinates (i.e After informTime[i] minutes, all his direct subordinates can start spreading the news).

Return the number of minutes needed to inform all the employees about the urgent news.

Example 1:

Input: n = 1, headID = 0, manager = [-1], informTime = [0]
Output: 0
Explanation: The head of the company is the only employee in the company.

Example 2:

Input: n = 6, headID = 2, manager = [2,2,-1,2,2,2], informTime = [0,0,1,0,0,0]
Output: 1
Explanation: The head of the company with id = 2 is the direct manager of all the employees in the company and needs 1 minute to inform them all.
The tree structure of the employees in the company is shown.

Example 3:

Input: n = 7, headID = 6, manager = [1,2,3,4,5,6,-1], informTime = [0,6,5,4,3,2,1]
Output: 21
Explanation: The head has id = 6. He will inform employee with id = 5 in 1 minute.
The employee with id = 5 will inform the employee with id = 4 in 2 minutes.
The employee with id = 4 will inform the employee with id = 3 in 3 minutes.
The employee with id = 3 will inform the employee with id = 2 in 4 minutes.
The employee with id = 2 will inform the employee with id = 1 in 5 minutes.
The employee with id = 1 will inform the employee with id = 0 in 6 minutes.
Needed time = 1 + 2 + 3 + 4 + 5 + 6 = 21.

Example 4:

Input: n = 15, headID = 0, manager = [-1,0,0,1,1,2,2,3,3,4,4,5,5,6,6], informTime = [1,1,1,1,1,1,1,0,0,0,0,0,0,0,0]
Output: 3
Explanation: The first minute the head will inform employees 1 and 2.
The second minute they will inform employees 3, 4, 5 and 6.
The third minute they will inform the rest of employees.

Example 5:

Input: n = 4, headID = 2, manager = [3,3,-1,2], informTime = [0,0,162,914]
Output: 1076

Constraints:

  • 1 <= n <= 10^5
  • 0 <= headID < n
  • manager.length == n
  • 0 <= manager[i] < n
  • manager[headID] == -1
  • informTime.length == n
  • 0 <= informTime[i] <= 1000
  • informTime[i] == 0 if employee i has no subordinates.
  • It is guaranteed that all the employees can be informed.

题目链接:https://leetcode-cn.com/problems/time-needed-to-inform-all-employees/

思路

比赛的时候一直想要从leader开始向下面的员工递归,但给出的数据是从员工找leader的,所以虽然答案正确,但时间不通过。

再次分析这道题,题目主要信息是:

1)每个员工只有一个leader,所以不存在重复通知的情况;

2)没有下属的员工不能记他作为leader的耗时;

3)同一层的leader同时对下属进行通知。

可以看出,这题是计算树的每条路径耗时的最大值。

但是不太一样的是无法从根向下递归,只能从叶子向上递归。因此需要建立一个数组,用来建立从根向下到某一点的耗时,以便从其他叶子节点递归回去的时候,不用递归到根。

class Solution {
public:
    int res = 0;
    int numOfMinutes(int n, int headID, vector<int>& manager, vector<int>& informTime) {
        if(n<=1 || manager.size()!=n || informTime.size()!=n) return res;
        int time[n];
        memset(time, -1, n*sizeof(int));
        time[headID] = 0;
        for(int i=0; i<n; ++i){
            if(i!=headID && time[i]==-1){
                trace(i, manager, informTime, time);
            }
        }
        return res;
    }
    int trace(int i, vector<int>& manager, vector<int>& informTime, int time[]){
        int head = manager[i];
        int know;
        if(time[head]>-1) {
            know = time[head] + informTime[head];
        }else{
            know = trace(head, manager, informTime, time);
        }
        time[i] = know;
        res = max(res, know);
        return know + informTime[i];
    }
};

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值