Last active
August 29, 2022 09:05
-
-
Save eric100lin/85bba0a50a859a0f07888a8d2bbda464 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Witness of The Tall People | |
Hi, here's your problem today. This problem was recently asked by Google: | |
There are n people lined up, and each have a height represented as an integer. A murder has happened right in front of them, and only people who are taller than everyone in front of them are able to see what has happened. How many witnesses are there? | |
Example: | |
Input: [3, 6, 3, 4, 1] | |
Output: 3 | |
Explanation: Only [6, 4, 1] were able to see in front of them. | |
# | |
# | |
# # | |
#### | |
#### | |
##### | |
36341 x (murder scene) | |
''' | |
def witnesses(heights): | |
max_height = float('-inf') | |
witnesses = 0 | |
for idx in range(len(heights)-1, -1, -1): | |
if heights[idx] > max_height: | |
witnesses += 1 | |
max_height = heights[idx] | |
return witnesses | |
print(witnesses([3, 6, 3, 4, 1])) | |
# 3 | |
print(witnesses([3, 4, 4, 4, 1])) | |
# 2 | |
print(witnesses([-1, -3, -7, -2])) | |
# 2 |
you should have common sense that height can never be negative
humm, right for height but still a valid test case if the story change a bit
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Awesome! Glad I could help!