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
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion 08/kth-missing-positive-number.checkpoint.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ Implement `kth_missing_positive_number`. Use these tests to guide your solution
```python
def kth_missing_positive_number(numbers, k):
'''
INPUT: List of positive numbers in increating order & a positive integer k
INPUT: List of positive integers in increasing order & a positive integer k

OUTPUT: The kth missing number
'''

Expand Down
13 changes: 9 additions & 4 deletions 08/kth-missing-positive-number.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,15 +206,20 @@ Write the logical steps here.
Example Steps for an O(n) solution:

1. Check for valid input
1. if `k < arr[0]`
1. if `k < arr[0]:`
* `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.

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.

1. return the last element of the list plus the remaining value of `k`.
* `index = index + 1`
5. return the last element of the list plus the remaining value of `k`.

### !end-explanation

Expand Down