当前位置:网站首页>Two houses with different colors and the farthest distance
Two houses with different colors and the farthest distance
2022-06-29 05:53:00 【·Starry Sea】
subject
There are... In the street n The houses are neatly lined up , Every house is painted in beautiful colors . I'll give you a subscript from 0 Start with a length of n Array of integers for colors , among colors[i] It means the first one i The color of the house .
return Two buildings Color Different Between houses Maximum distance .
The first i This house and the second j The distance between the houses is abs(i - j) , among abs(x) yes x The absolute value of .
Example 1:
Input :colors = [1,1,1,6,1,1,1]
Output :3
explain : Above picture , Color 1 The logo is blue , Color 6 The logo is red .
The two houses with different colors and the farthest distance are houses 0 And the house 3 .
house 0 The color of is color 1 , house 3 The color of is color 6 . The distance between the two houses is abs(0 - 3) = 3 .
Be careful , house 3 And the house 6 It can also produce the best answer .
Example 2:
Input :colors = [1,8,3,8,3]
Output :4
explain : Above picture , Color 1 The logo is blue , Color 8 The logo is yellow , Color 3 The logo is green .
The two houses with different colors and the farthest distance are houses 0 And the house 4 .
house 0 The color of is color 1 , house 4 The color of is color 3 . The distance between the two houses is abs(0 - 4) = 4 .
Example 3:
Input :colors = [0,1]
Output :1
explain : The two houses with different colors and the farthest distance are houses 0 And the house 1 .
house 0 The color of is color 0 , house 1 The color of is color 1 . The distance between the two houses is abs(0 - 1) = 1 .
Tips :
n == colors.length
2 <= n <= 100
0 <= colors[i] <= 100
The generated test data meets At least There is 2 A house of different colors
source : Power button (LeetCode)
Their thinking
First, find out how many different colors there are , Then traverse from the front to find the position of the color , Then we traverse the tail to find the first position of a color different from this color , This should be the color 1 To color 2 The maximum distance .
class Solution:
def maxDistance(self, colors: List[int]) -> int:
color=set(colors)
MAX=0
for i in color:
for j in range(len(colors)):
if i==colors[j]:
break
for k in range(len(colors)-1,-1,-1):
if colors[k]!=i:
break
if k-j>MAX:
MAX=k-j
return MAX

边栏推荐
- Analysis of ArrayList set in teacher Yang's class
- 2-nitro-5,10,15,20-tetra (4-methylphenyl) porphyrin copper (no2tmpp) Cu) /2-nitro-5,10,15,20-tetra (4-methylphenyl) porphyrin (no2tmpp) H2) Qiyue porphyrin supply
- Research Report on the recommended lithography industry in 2022 industry development prospect market investment analysis (the attachment is a link to the network disk, and the report is continuously u
- Common methods for describing 3D models of objects and their advantages and disadvantages
- The first in China! CICA technology database antdb appears at the performance test tool conference of China Academy of communications technology
- Stack -- 739 Daily temperature
- Robot reinforcement learning - transferring end-to-end videomotor control from simulation to realworld (curl 2017)
- Analysis report on the investment market of the development planning prospect of the recommended NFT industry research industry in 2022 (the attachment is a link to the online disk, and the report is
- 嵌入式RTOS
- STI, one controller
猜你喜欢

2022 recommended quantum industry research industry development planning prospect investment market analysis report (the attachment is a link to the online disk, and the report is continuously updated

机器人强化学习——Transferring End-to-End Visuomotor Control from Simulation to RealWorld (CoRL 2017)

Pytest (7) -yield and termination function

HTTP Caching Protocol practice

JS messagechannel transport

HTTP Caching Protocol practice

2022 recommended property management industry research report industry development prospect market investment analysis (the attachment is the link to the online disk, and the report is continuously up

Annual inventory review of Alibaba cloud's observable practices in 2021

Hustoj SPJ example

Ti Click: quickly set up tidb online laboratory through browser | ti- team interview can be conducted immediately
随机推荐
Mongodb basic knowledge summary
Analysis report on the investment market of the development planning prospect of the recommended NFT industry research industry in 2022 (the attachment is a link to the online disk, and the report is
Summary of redis basic knowledge points
Modularization and modular specification commonjs
2022 recommended high-speed rail industry research report investment strategy industry development prospect market analysis (the attachment is a link to the online disk, and the report is continuously
Analysis report on the investment market situation of the development planning prospect of the recommended chip industry research industry in 2022 (the attachment is a link to the network disk, and th
[CV] wuenda machine learning course notes Chapter 13
Would like to ask, which is the better choice for securities companies? I don't understand. Is it safe to open an account online now?
2022-01 Microsoft vulnerability notification
Testing grpc service with grpcui
ICLR is going to have a big discussion on the deep generation model. Max welling and the winner of the AAAI million dollar award are here. Bengio is one of the organizers
VLAN experiment
Awk of shell script
Robot reinforcement learning - transferring end-to-end videomotor control from simulation to realworld (curl 2017)
[C language series] - initial C language (4)
The win11 file resource manager has an explicit Caton, and Microsoft promises to improve the performance in 2022
AttributeError: module ‘torch. nn. Parameter 'has no attribute' uninitializedparameter 'solution
Conditional test, if and case conditional test statements of shell script
Use VS to create a static link library Lib and use
data management plan