当前位置:网站首页>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

边栏推荐
- The first in China! CICA technology database antdb appears at the performance test tool conference of China Academy of communications technology
- 2022 recommended prefabricated construction industry research report industry development prospect market analysis white paper (the attachment is a link to the network disk, and the report is continuo
- CCTV revealed that xumengtao won the black Technology: there was a virtual coach???
- Domain name hack
- 2022 recommended cloud computing industry research report investment strategy industry development prospect market analysis (the attachment is a link to the online disk, and the report is continuously
- Openfpga wishes you a happy Lantern Festival!
- Analysis report on the investment market of the development planning prospect of the recommended rare earth industry research industry in 2022 (the attachment is a link to the online disk, and the rep
- Will the order of where conditions in MySQL affect the union index? Will where 1 =1 affect the use of the index? Does where 1 =1 affect the use of indexes?
- 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
- Tcapulusdb Jun · industry news collection (III)
猜你喜欢

Creation of Arduino uno development environment

The first in China! CICA technology database antdb appears at the performance test tool conference of China Academy of communications technology

Use VS to create a static link library Lib and use

Research on heuristic intelligent task scheduling

Rich material libraries make modeling easy and efficient for developers

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

Slot
![[high concurrency] deeply analyze the callable interface](/img/17/93056547aa1a2b342e1b159b7c41c2.jpg)
[high concurrency] deeply analyze the callable interface

2022 recommended trend toy blind box industry research report industry development prospect market analysis white paper (the attachment is a link to the network disk, and the report is continuously up

After nine years of testing, the salary for interviewing Huawei is 10000. Huawei employees: the company doesn't have such a low salary position
随机推荐
Annual inventory review of Alibaba cloud's observable practices in 2021
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
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
机器人强化学习——第一人称 VS 第三人称
Use VS to create a static link library Lib and use
3 frequently tested SQL data analysis questions (including data and code)
Robot reinforcement learning - transferring end-to-end videomotor control from simulation to realworld (curl 2017)
Testing grpc service with grpcui
HTTP Caching Protocol practice
Analysis report on the investment market of the development planning prospect of the recommended rare earth industry research industry in 2022 (the attachment is a link to the online disk, and the rep
[high concurrency] deeply analyze the callable interface
C语言用 printf 打印 《爱心》《火星撞地球》等,不断更新
What if the hard disk fails to recognize how to recover data
Parsing rshub document auto generation API
Ghost in the Log4Shell
嵌入式RTOS
Research on heuristic intelligent task scheduling
Open source demo| you draw and I guess -- make your life more interesting
Ti Click: quickly set up tidb online laboratory through browser | ti- team interview can be conducted immediately
Alphacode made its debut! The programming version of "Alpha dog" competed quietly and defeated half of the programmers