【Brush questions diary】17.11. Word distance

Continue to create, accelerate growth! This is the first day of my participation in the "Nuggets Daily New Plan · June Update Challenge", click to view the details of the event

The 48th article of this brushing diary is titled: 17.11. Word distance , medium

1. Topic description:

It's been a long time, let's continue to review the questions today. I've been busy recently. I found that the busier people are, the less growth they have. I think it's because I have less time to think deeply about myself, and less time to talk to myself.

Continue to brush questions, exercise thinking

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

Let’s take a closer look at what this topic tells us:

  • We need to find the closest distance between two words in a given bunch of words
  • The two words given will be repeated many times in this pile of words, and the number of repetitions is random

So we know, in a crowd, what is the closest distance between two people, next to each other, or is it the closest when they are next to each other?

Just like the above group of words, we want to afind nicethe closest distance between and two words , we will find a way to find the smallest distance when these two words are adjacent in various situations.

Then, I can think of using double pointers to deal with this problem.

Or what the above picture expresses

  • We define a p1 to represent the default current position of word1, -1 when initialized, and define a p2 to represent the default current position of word2
  • Traverse the given words, when the current word is consistent with word1, assign the position to p1
  • Similarly, assign the current position of word2 to p2
  • When it is verified that both p1 and p2 are greater than or equal to, the difference between them is calculated until the traversal of words is completed, and the minimum value of all the differences can be taken.

But here we need to pay attention to initialize p1 , p2 The current position is -1 There is no need to explain too much here, but we need to initialize a number that is relatively large and can be specified as the length of words

The purpose of this is to keep the logic clear, avoid calculation errors, and ensure that the minimum value obtained must be the actual value when the minimum value is compared for the first time.

3. Coding

According to the above logic and analysis, we can translate it into the following code

The encoding is as follows:

func findClosest(words []string, word1 string, word2 string) int {

    var p1,p2 = -1,-1

    // 此处需要注意定义 res 的时候,可以大一点,可以为整个 words 的长度,这是为了避免咱们在计算两个单词最小值的时候,计算出错

    var res = len(words)

    for index,w := range words {

        if w == word1 {
            p1 = index
        }

        if w == word2 {
            p2 = index
        }

        if p1 >=0 && p2 >= 0{
            res = min(help(p1,p2) , res)
        }
    }
    return res
}
func help (a,b int) int {
    if a > b{
        return a-b
    }
    return b-a
}
func min (a,b int) int {
    if a > b {
        return b
    }
    return a
}
复制代码

4. Summary:

Here we can see that with this double pointer implementation, we only traverse the words array once, so our time complexity here is O(n)

The space complexity is relatively clear, and the space consumption we introduce is O(1)

Original title address: Interview question 17.11. Word distance

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/7102412502171910175