当前位置:网站首页>A consistent friend
A consistent friend
2022-06-09 20:53:00 【Eager to learn】
On the plateau n A town , The number is 1-n, They connect on the same road , Every town has an altitude h[i] rice . Travel from Xiaoming city 1 Start , Move from one town to another every day , But Xiao Ming moves up to the top every day x rice ,x That is Xiao Ming's physical strength . in other words : If v,u adjacent , If h[u] -h[v] > x, So Xiao Ming can't start from v Move to v, But you can walk from high altitude to low altitude , From u Move to v. Now Xiao Ming takes his friends to , He has m A friend , Every friend has a physical strength value xi, Xiao Ming hopes to find a friend who keeps pace with him , If his friend has the same physical strength as Xiao Ming , Then the town he can reach is exactly the same as Xiao Ming . Now Xiao Ming wants to find the number of friends who meet this situation .
Input :
The first row has three integers ,n,m,x Indicates the number of cities on the plateau , The number of friends and Xiao Ming's physical strength
The second line n It's an integer , Indicates that the number is from 1 To n The altitude of the city hi(1<=hi<=1000000000)
The third line m It's an integer , Indicates Xiao Ming's friend's physical strength value
example :
n = 5
m = 5
x = 6
h = [5, 7, 6, 10, 9]
y = [1, 8, 2, 9, 1]
Output
[2, 4]
def max_len(h, y, x):
"""" When climbing mountains , You can go down the mountain no matter how much physical strength you have , We just have to think about going up the mountain , The highest altitude you can climb left_height, And the height you can't climb over for the first time right_height If you can climb over , Then as long as the physical strength is greater than Xiao Ming's physical strength , otherwise , Physical strength left_height On right_height In order to keep pace with each other """
# Initialize the maximum height of the climb left_height, The height that cannot be reached for the first time right_height
left_height = -1
right_height = 0
for i in range(1, len(h)):
if h[i] - h[i - 1] <= x:
left_height = max(left_height, 0 if h[i] - h[i - 1] < 0 else h[i] - h[i - 1])
else:
right_height = h[i] - h[i - 1]
break
if not right_height: # All down the mountain
print([i + 1 for i in range(len(y)) if y[i] > x])
else:
print([i + 1 for i in range(len(y)) if left_height <= y[i] < right_height])
边栏推荐
猜你喜欢

C language implementation of simple calculator

浏览器无法打开百度,别的可以正常打开

dump. Pcapng packet parsing

Just learning embedded, I want to ask what is interrupt and what is the concept of interrupt

Integer ordered array lookup -- traversal and half / bisection

Analysis of source code data anti leakage solution

二叉搜索树

【运维有小邓】AD域文件权限管理

Problems and solutions of HMI online download failure

BI 如何让SaaS产品具有 “安全感”和“敏锐感”(上)
随机推荐
C#静态类的扩展应用
LeetCode 526. 优美的排列***
dump. Pcapng packet parsing
Redis knowledge points
学习使用php实现无限极评论和无限极转二级评论解决方案
KubeVirt网络源码分析(3)- 虚拟机热迁移网络
部署 Kubernetes + KubeVirt 以及 KubeVirt的基本使用
CVPR2022 Oral | 用于实时地图视图语义分割的跨视图Transformer
排序-快速排序
堆(优先队列)
Set up ngrok server, realize intranet penetration service, and realize online access from external network to internal network
Application of anonymous function in C #
Inheritance relationship in C #
Ceisum 3D scene demo
ClickHouse 数据插入、更新与删除操作 SQL
minikube config set driver kvm2
minikube config set driver kvm2
C#中的里氏替换原则
【运维有小邓】AD域文件权限管理
分享 4 种 JS 深拷贝的方法