Skip to content

Fix bug in hint and add explanations#36

Closed
jericahuang wants to merge 6 commits intoAda-Developers-Academy:mainfrom
jericahuang:main
Closed

Fix bug in hint and add explanations#36
jericahuang wants to merge 6 commits intoAda-Developers-Academy:mainfrom
jericahuang:main

Conversation

@jericahuang
Copy link
Contributor

Resulting function that passes all tests:

def kth_missing_positive_number(numbers, k):
  if k < numbers[0]:
    return k
  index = 0
  k -= numbers[0]-1
  while index < len(numbers) - 1:
    current_missing = numbers[index + 1] - numbers[index] - 1
    if k <= current_missing:
      return numbers[index] + k
    k = k - current_missing
    index += 1
  return numbers[-1]+k

Copy link
Contributor

@kelsey-steven-ada kelsey-steven-ada left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for updating this!

Copy link
Contributor

@anselrognlie anselrognlie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. I just have a couple suggestions that to me helps the clarity.

1. Set `index` to `0`
1. `while index < len(arr) - 1`
2. `k = k - arr[0] - 1`
* `k` will be decremented to represent the number of remaining missing nums. This line accounts for missing nums before `arr[0]`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggest calling out where the 1 is coming from.

Suggested change
* `k` will be decremented to represent the number of remaining missing nums. This line accounts for missing nums before `arr[0]`.
* `k` will be decremented to represent the number of remaining missing nums. This line accounts for missing nums before `arr[0]`, which we expect to start from 1, the smallest positive integer.

4. `while index < len(arr) - 1:`
* `current_missing = arr[index + 1] - arr[index] - 1`
* `if k <= current_missing`
* `current_missing` represents how many missing numbers between `arr[index + 1]` and `arr[index]`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggest accounting for where the 1 is coming from.

Suggested change
* `current_missing` represents how many missing numbers between `arr[index + 1]` and `arr[index]`
* `current_missing` represents how many missing numbers there are between `arr[index + 1]` and `arr[index]`, which should differ by only 1 if there are no missing numbers.

* `if k <= current_missing:`
* We found the original kth missing number. The value is `arr[index] + k`. We return it and exit the loop.
* `return arr[index] + k`
* `k = k - current_missing`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We mention that k gets decremented above, but I think noting why again here is helpful.

Suggested change
* `k = k - current_missing`
* `k = k - current_missing`
* Reduce the number of missing numbers we are looking for by the number of missing numbers we just found.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

Comments