【LeetCode】HOT 100(20)

Introduction to the question list:

Selected 100 most popular questions on LeetCode, suitable for beginners who are new to algorithms and data structures and those who want to improve efficiently in a short period of time, master these 100 questions, and you already have the ability to learn in code The basic ability to pass through the world.

Table of contents

Introduction to the question list:

Topic: 621. Task Scheduler - Leetcode

The interface of the topic:

Problem-solving ideas:

code:

It's over! ! ! !

Title: 581. Shortest Unordered Contiguous Subarray - Leetcode

The interface of the topic:

Problem-solving ideas:

code:

It's over! ! ! !

Write at the end:


Title: 621. Task Scheduler - Leetcode

The interface of the topic:

class Solution {
public:
    int leastInterval(vector<char>& tasks, int n) { 

    }
};

Problem-solving ideas:

The idea of ​​this question is this:

Store the number of tasks through a 26-large array (because tasks only have A~Z)

Find the maximum number of tasks by sorting

Find at least how long: A->X->X->A->X->X->A

Find the last task with several tasks: A->X->X->A->X->X->A->?

If the number of tasks exceeds the number of intervals, the number of tasks is returned directly: Example:

For example, there are two intervals, but one task has four tasks, and all tasks must be completed.

Then just return the number of tasks directly.

code show as below:

code:

class Solution {
public:
    int leastInterval(vector<char>& tasks, int n) { 
        //如果间隔是0,或者只有一个(零个)任务,就直接返回
        if(tasks.size() <= 1 || n < 1) return tasks.size();

        //通过一个26大的数组存储任务数量(因为任务只有A~Z)
        vector<int> v(26, 0);
        for(int i = 0; i < tasks.size(); i++) {
            v[tasks[i] - 'A']++;
        }

        //通过排序找到最大的任务数量
        sort(v.begin(), v.end());
        int maxCnt = v[25];
        //求出至少有多长:A->X->X->A->X->X->A
        int maxVal = (maxCnt - 1) * (n + 1) + 1; 

        //求最后一个任务还带着几个任务:A->X->X->A->X->X->A->?
        int i = 24;
        while(i >= 0 && v[i] == maxCnt) {
            i--;
            maxVal++;
        }

        //如果任务的数量超过了间隔的数量,就直接返回任务数量
        return max(maxVal, (int)tasks.size());
    }
};

It's over! ! ! !

Title: 581. Shortest Unordered Contiguous Subarray - Leetcode

The interface of the topic:

class Solution {
public:
    int findUnsortedSubarray(vector<int>& nums) {

    }
};

Problem-solving ideas:

I use the O(N) algorithm for this question.

In fact, it uses double pointers,

One for the right boundary, one for the left boundary,

The specific idea is this:

1. The pointer used to find the right boundary moves from left to right, and records the maximum value encountered.

Going all the way to the right, the position before the last maximum value encountered is the right boundary;

2. The pointer used to find the left boundary moves from right to left, and records the minimum value encountered.

Going all the way to the left, the position before the last minimum value encountered is the left boundary.

code show as below:

code:

class Solution {
public:
    int findUnsortedSubarray(vector<int>& nums) {
        int n = nums.size();
        int rMax = INT_MIN, right = -1;
        int lMax = INT_MAX, left = -1;
        for(int i = 0; i < n; i++) {
            if(rMax > nums[i]) {
                right = i;
            }
            else {
                rMax = nums[i];
            }
            if(lMax < nums[n - i - 1]) {
                left = n - i - 1;
            }
            else {
                lMax = nums[n - i - 1];
            }
        }
        return right == -1 ? 0 : right - left + 1;
    }
};

It's over! ! ! !

Write at the end:

The above is the content of this article, thank you for reading.

If you feel that you have gained something, you can give the blogger a like .

If there are omissions or mistakes in the content of the article, please private message the blogger or point it out in the comment area~

Guess you like

Origin blog.csdn.net/Locky136/article/details/131300237