This question is marked as hard in leetcode. Initially, I think it is easy to solve and give an answer quickly, however, it can not pass by a very big number with the prompt: "Time Limit Exceeded", that means my algorithm do no work in a high efficiency.
Then I tried to find the regular pattern of this question. The regular pattern is as follows:
First, I divide the number by the sum of different numbers, such as 532, I divided it as 500+30+2, and calculate the number of one in (0,500], (0,30], (0,2]. However, this is a wrong solution, since I calculate duplicate numbers, (0,500], (0,30] all include (0,2]. Then I have to find another solution.
Second, I found the mistake in my last solution, that missed the number from (500, 532], then I divide the number 532 into three sets, 500, (500, 530], (530, 532]. It is pretty close to correct solution, however, I still missed another situation, that is if the number is 1111.
Finally, I get the correct solution, that is first I divide the number into different intervals which is the same with last solution, 500, (500, 530], (530, 532]. we still need to consider the situation if there is one in the number, such as 1101. I add another variable to store how many ones in the number. so the solution is as follows:
Then I tried to find the regular pattern of this question. The regular pattern is as follows:
First, I divide the number by the sum of different numbers, such as 532, I divided it as 500+30+2, and calculate the number of one in (0,500], (0,30], (0,2]. However, this is a wrong solution, since I calculate duplicate numbers, (0,500], (0,30] all include (0,2]. Then I have to find another solution.
Second, I found the mistake in my last solution, that missed the number from (500, 532], then I divide the number 532 into three sets, 500, (500, 530], (530, 532]. It is pretty close to correct solution, however, I still missed another situation, that is if the number is 1111.
Finally, I get the correct solution, that is first I divide the number into different intervals which is the same with last solution, 500, (500, 530], (530, 532]. we still need to consider the situation if there is one in the number, such as 1101. I add another variable to store how many ones in the number. so the solution is as follows:
Comments
Post a Comment