A post from a golfer kept me awake at three o'clock in the middle of the night...

1. Cause

  The cause of the incident was a post from a golfer in the planet. I woke up at 3:00 and went to the bathroom and planned to continue sleeping. Suddenly, my phone rang. I saw a message from a golfer in the planet. It was probably something like this Message: "Why is this code wrong? I asked chatgpt and he didn't know."

2. Suggestions

  After reading this code, I first have a few thoughts:

1. Brackets and indentation

  First of all, the indentation and brackets of this code are very tiring to read, so that others can’t stand it when asking questions, including when checking errors by yourself, so we should try our best to be clear when writing code, otherwise we can check for errors by ourselves Sometimes I subconsciously want to ask others for help.

2. Carefully examine the questions

  I also misunderstood this question at the beginning. I thought that the sum of the last k consecutive integers must be equal to value to return true (the original intention is actually the last k integers, and each integer must be equal to value to return true ).

3. Independent thinking

  For this problem, there are no data samples, so you should debug it yourself, think independently, and try to think by yourself first, because I saw that the posting time is 1 o'clock in the middle of the night, and the thinking at night is the most confusing. It is best to Get a good night's sleep first, and then look at it in the morning, it will definitely be much clearer.
  What's more, this is not a difficult problem. It is best to debug and solve such a simple problem by yourself, otherwise you will become dependent when you encounter difficulties later.

4. A good way to get up early

  Every morning before getting up, I look at the planet, see the problems of golfers, and add some thoughts of my own. During the process of thinking, I wake up, and then I want to record this matter quickly, so that I can develop the habit of getting up early. Good habit, wonderful!

5. Will chatgpt replace humans?

  Recently, too many outsiders are struggling with this issue. I don’t think there is any need to worry about it. I asked if chatGpt will do it? It doesn't work either. In fact, you need to constantly guide it. With enough guidance, I believe he will still be able to do so. The premise is that the person who guides must be able to do so. So are you still worried that ChatGpt will replace human beings? It's just a tool, and it's not programmers who can make good use of this tool!

Three, solve

  Finally talk about this question.
  Given an integer data stream, it is required to implement a data structure to check whether the last k integers are equal to the given value value;
insert image description here
  for this problem, it is a classic queue problem, which can be solved with a sliding window. First, from the data structure angle to think about the problem.

1. Data structure

class DataStream {
    
    
    int count;                 // 累计多少个数
    int samecount;             // 累计有多少个数的等于value
    int val;                   // 缓存构造函数的传参 val
    int K;                     // 缓存构造函数的传参 k
    queue< int > q;
}

  count represents the length of the current sliding window; samecount represents the number of elements whose value is value in the sliding window; val and K are used to cache the passed parameters value and k.

2. Initialization

  Just write the constructor like this, and the four statements are relatively simple.

    DataStream(int value, int k) {
    
                 
        count = 0;
        samecount = 0;
        val = value;
        K = k;         
    }

3, Judgment

  The judgment process is divided into three steps:
   (1) Put the number at the end of the queue, and if the value is equal to value, choose to increment the samecount;
   (2) Judge whether the current sliding window is smaller than K, if so, return directly; otherwise, samecount and count Equal representation: all values ​​are value, set the return value to true; it cannot be returned at this time, so it is cached in ret first; (3
   ) Keep the length of the sliding window at K-1, so that the next data is received After entering, the value of the window can always be kept as K; while the element at the head of the queue is popped up, the value of samecount is updated;

    bool consec(int num) {
    
    
        bool ret = false;
        
        // 1. 将数字加入队列尾部
        {
    
    
            q.push(num);                     
            ++count;                         
            samecount += (val == num) ? 1:0;
        }
        // 2. 实际判定过程
        {
    
    
            if(count < K) {
    
    
                return false;                // 小于必然返回false
            }
            if(samecount == count) {
    
    
                ret = true;
            }
        }
        // 3. 将数字从队列头部剔除
        {
    
    
            num = q.front();
            samecount -= (val == num) ? 1:0;
            --count;
            q.pop();
        }

        return ret;
    }

  To update the relevant content of the knowledge planet, you can refer to: Hero Algorithm League for more information.

Guess you like

Origin blog.csdn.net/WhereIsHeroFrom/article/details/130191494