Candy [leetcode] O (n) time complexity, O (1) space complexity method, candyleetcode

Source: Internet
Author: User

Candy [leetcode] O (n) time complexity, O (1) space complexity method, candyleetcode

The relationship between ratings [I + 1] and ratings [I] is as follows:

1. Equal. When the value is equal, ratings [I + 1] corresponds to 1 candy.

2. ratings [I + 1]> ratings [I]. In this case, you need to find the incremental Sequence starting with ratings [I.

3. ratings [I + 1] <ratings [I]. In this case, you need to find the descending sequence starting with ratings [I.

For any incremental sequence [2 3 4 5 6], the number of candy is [1 2 3 4 X].

For any descent sequence [6 5 4 3 2], the corresponding number of Sweets is [X 4 3 2 1].

X indicates the number of sweets corresponding to the elements in the ascending and descending serial communication. It should be a large value in the length of the ascending sequence and the length of the descending sequence

The Code is as follows:

    int candy(vector<int> &ratings) {        if (ratings.size() == 0) return 0;        int sum = 0;        int candyNum = 1;        for (int i = 0; i < ratings.size() - 1;)        {            if (ratings[i] == ratings[i + 1])             {                  //if is the same rating, reset candy num. ie: 1 3 3, for the 2nd 3, candy num is 1                sum += candyNum;//add current candy num                i++;                candyNum = 1;//set next candy num to 1            }            else if (ratings[i] < ratings[i + 1])            {                   // find ascending sequence, until i is the end of sequence. ie: 1 2 3 1, ratings[i] is 3                for (;i < ratings.size() - 1 && ratings[i] < ratings[i + 1]; i++) sum += (candyNum++);            }            else if (ratings[i] > ratings[i + 1])            {                  // find descending sequence, until i is the end of sequence. ie: 3 2 1 3, rating[i] is 1                int decCount = 1;                for (; i < ratings.size() - 1 && ratings[i] > ratings[i + 1]; i++) sum += (decCount++);                sum += max(candyNum, decCount);//add first element of the sequence                //remove last element of the sequence, as i is the end of sequence, and i's candy num shouldn't be calculated into sum                sum --;                candyNum = 1;            }        }        sum += candyNum;        return sum;     }



How to sort n integers? time complexity O (n) and space complexity O (1)

Question: How to sort n non-repeated integer sequences. It is known that the number ranges from 0 to 65535, and time complexity O (n) is required ), space complexity O (1) Analysis: You can apply for an array A with A size of 65536. The x subscript of the array represents the number x, and A [x] represents the number of times x appears in the integer sequence. After scanning the integer sequence, the sorting of the integer sequence can be completed. The time complexity is O (n), which should be a known range, an array with a size of 65536 applied, and the size is a constant, so the space complexity is O (1) code :?? 1: # include 2 :# define SIZE 65536 3: void rangeSort (int * array, int len) 4: {5: int data [SIZE]; 6: int I = 0; 7: int j = 0; 8: memset (data, 0, SIZE * sizeof (int); 9: for (I = 0; I

What is space complexity and time complexity? What are O (n ^ 2) and O (n?

1. Time Complexity
(1) Time Frequency the time required for executing an algorithm cannot be calculated theoretically. You must run the test on the computer before you can understand it. However, it is impossible and unnecessary for us to test each algorithm on the machine. We only need to know which algorithm takes more time and which algorithm takes less time. In addition, the time spent by an algorithm is proportional to the number of statements executed in the algorithm. In an algorithm, when the number of statements executed is large, it takes more time. The number of statement executions in an algorithm is called the statement frequency or time frequency. As T (n ).
(2) time complexity in the Time Frequency just mentioned, n is called the scale of the problem. When n is constantly changing, the time frequency T (n) will also change. But sometimes we want to know what the rule is when it changes. Therefore, we introduce the concept of time complexity. In general, the number of repeated executions of the basic operation in an algorithm is a function of the problem scale n. It is represented by T (n). If an auxiliary function f (n) exists ), so that when n approaches infinity, the limit value of T (n)/f (n) is a constant not equal to zero, then f (n) is T (n). It is recorded as T (n) = O (f (n), and O (f (n) is the progressive time complexity of the algorithm.
In different algorithms, if the number of statement executions in the algorithm is a constant, the time complexity is O (1). In addition, the time complexity may be the same when the time frequency is different, for example, the frequencies of T (n) = n2 + 3n + 4 and T (n) = 4n2 + 2n + 1 are different, but the time complexity is the same, they are all O (n2 ). In ascending order of magnitude, common time Complexities include: constant order O (1), logarithm order O (log2n), linear order O (n), linear logarithm order O (nlog2n ), square order O (n2), cubic order O (n3 ),..., k to the power of O (nk), exponential order O (2n ). As the problem scale n increases, the time complexity increases and the algorithm execution efficiency decreases. 2. The space complexity is similar to the time complexity. The space complexity refers to the measurement of the storage space required for an algorithm to be executed in a computer. Note: S (n) = O (f (n) We generally discuss the scale of auxiliary storage units except for normal memory usage. The discussion method is similar to the time complexity.
(3) Evaluation of progressive time complexity the time performance of an algorithm mainly evaluates the time performance of an algorithm by the magnitude of the algorithm's time complexity (that is, the approximate time complexity of the algorithm.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.