Skip to content
Open
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
39 changes: 39 additions & 0 deletions questions/ascending-houses.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Ascending Houses

## Question
You want to visit your friends house, however their street does not have any addresses! Instead, the height of the houses are in sorted order, from the lowest house starting index `0` and the tallest house on the last index.
Copy link
Member

Choose a reason for hiding this comment

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

grammar: "friend's"


Suppose you are given a list, `height[]`, of each house on the street, and your friend gives you the height of his house `h`. What is the index of where your friend lives, starting from the smallest house?

Note: His house is guranteed to be in `height[]`

## Sample Inputs
Input 1
```python
height[] = [3, 5, 7, 9, 11, 12, 16, 23, 30]
h = 7

output = 2
```
Input 2
```python
height[] = [1, 5, 19, 31, 40]
h = 1

output = 0
```

## Sample solution using Binary Search
```python
def ascending_houses(height[]: int[], h: int):
l,r = 0, len(height) - 1
while l < r:
mid = l + (r - l) // 2
if h < height[mid]:
r = mid - 1
elif h > height[mid]:
l = mid + 1
else:
return mid
```