【Review Diary】2028. Find Missing Observational Data

Offer arrives, dig friends to pick up! I am participating in the 2022 Spring Recruitment Check-In Event, click to view the event details .

【Review Diary】2028. Find Missing Observational Data

The 12th article of this writing diary is titled: 2028. Find the missing observational data contest , medium

1. Topic description:

After reading the question for a while, the first reaction is that this is a mathematical question. After reading it carefully, it is indeed another mathematical question , but it needs to be implemented by programming. Let's analyze it together.

2. Thought analysis:

1. What idea does this question examine? What is your thinking?

According to the information given in the title, we can know that there are these important points:

  • The elements in the given rolls array and the element data range we need to supplement are both 1 - 6
  • Now we know the mean value and the number of n+m to calculate the missing data , or we can find the entire observation data
  • Find the array corresponding to n, there may be many versions , we only need to output one version.

We can follow the example to reason:

Example: rolls = [1,5,6], mean = 3, n = 4

2028. Find Missing Observations

According to the above simulation, we know that the core point is to calculate the sum remainingSum that can give n numbers . If the remainingSum is less than n or the remainingSum is greater than 6*n , it does not satisfy our meaning.

When we get the remainingSum that meets the requirements, we can assign the values ​​corresponding to the n numbers, and the logic of the assignment is also very simple

When the required remainingSum is obtained, remainingSum /n must be less than or equal to 6, and if it is equal, there must be no remainder

Then, our allocation principle is to divide the remainder obtained by remainingSum % n into multiple points and append them to each average.

3. Coding

According to the above logic and analysis, we can translate it into the following code, which can be processed according to our mathematical method.

The encoding is as follows:

func missingRolls(rolls []int, mean int, n int) []int {
    remainSum := mean * (len(rolls) + n )
    for _,num := range rolls {
        remainSum = remainSum - num
    }

    // 如果剩余的数据不在 n - 6*n 之间,那么就是找不到缺失的数据
    if remainSum < n || remainSum > 6 * n {
        return nil
    }

    // 开始构造数据,此处的 remainSum 一定在  n - 6*n 之间 , 则 remainSum / n 一定是 shang 小于 6 ,再加上 yushu
    // 则我们可以构造这么一个版本:yushu 个 shang+1 , 和 (n-yushu)个 shang 即可
    shang,yushu := remainSum/n , remainSum %n

    res := make([]int, n)
    for i:=0;i<n;i++{
        res[i] = shang
        if i < yushu{
            res[i]++
        }
    }

    return res
}
复制代码

The actual coding is indeed a translation of the above idea. The translated code is as above, mainly to consider the range of data that can be carried in n, and how to assign the value of n data

4. Summary:

It is not difficult to understand, here our loop is n+m, so the time complexity is O(n+m), and the space complexity is O(1)

Original title address: 2028. Finding missing observations

I am here today, what I have learned, if there are any deviations, please correct me

Welcome to like, follow, favorite

Friends, your support and encouragement are the motivation for me to persist in sharing and improve quality

Okay, here it is this time

Technology is open, and our mentality should be open. Embrace change, live in the sun, and move forward.

I'm the little devil boy Nezha , welcome to like, follow and collect, see you next time~

Guess you like

Origin juejin.im/post/7079621946685521933