Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug in hint and add explanations #36

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

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.

* `return k`
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.

* `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`
* `current_missing` represents how many missing numbers between `arr[index + 1]` and `arr[index]`
* `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