当前位置:网站首页>2078. Two houses with different colors and the farthest distance
2078. Two houses with different colors and the farthest distance
2022-07-06 16:07:00 【mrbone9】
Address :
Power button https://leetcode-cn.com/problems/two-furthest-houses-with-different-colors/
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)
link :https://leetcode-cn.com/problems/two-furthest-houses-with-different-colors
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Ideas :
The first method is, of course, violence
Consider another approach , For a set of numbers
Contrast direction :[0] ->[len-1]
If the first element is different from the last element , So we found The maximum value in this direction , otherwise , Compare the penultimate 2 individual , The first 3 individual ...
The other direction is :[len-1] ->[0]
The value found in these two directions is the maximum
Method 1 、 Two way comparison
#define max(a,b) ( (a) > (b) ? (a) : (b) )
int maxDistance(int* colors, int colorsSize){
int max1 = 0, max2 = 0;
int i,j;
for(i=0; i<colorsSize; i++)
{
//printf("1...colors[%d]=%d, colors[%d]=%d\n", i, colors[i], colorsSize-1, colors[colorsSize - 1]);
if(colors[i] != colors[colorsSize - 1])
{
max1 = colorsSize - 1 - i;
break;
}
}
for(i=colorsSize - 1; i>=0; i--)
{
//printf("2...colors[%d]=%d, colors[%d]=%d\n", i, colors[i], 0, colors[0]);
if(colors[i] != colors[0])
{
max2 = i;
break;
}
}
printf("max1=%d, max2=%d\n", max1, max2);
return max(max1, max2);
}
边栏推荐
- Basic Q & A of introductory C language
- Ball Dropping
- Interval sum ----- discretization
- Information security - Analysis of security orchestration automation and response (soar) technology
- C language learning notes
- If you want to apply for a programmer, your resume should be written like this [essence summary]
- 渗透测试 ( 4 ) --- Meterpreter 命令详解
- Nodejs crawler
- Find 3-friendly Integers
- STM32 how to use stlink download program: light LED running light (Library version)
猜你喜欢
【练习-7】Crossword Answers
【练习-5】(Uva 839)Not so Mobile(天平)
Penetration test (3) -- Metasploit framework (MSF)
B - 代码派对(女生赛)
差分(一维,二维,三维) 蓝桥杯三体攻击
Determine the Photo Position
Matlab comprehensive exercise: application in signal and system
Penetration test (7) -- vulnerability scanning tool Nessus
渗透测试 ( 5 ) --- 扫描之王 nmap、渗透测试工具实战技巧合集
Penetration test (1) -- necessary tools, navigation
随机推荐
Write web games in C language
Interesting drink
[exercise-6] (PTA) divide and conquer
Data storage in memory & loading into memory to make the program run
【练习-3】(Uva 442)Matrix Chain Multiplication(矩阵链乘)
Penetration test (3) -- Metasploit framework (MSF)
The concept of C language array
栈的经典应用—括号匹配问题
SSM框架常用配置文件
CEP used by Flink
Analysis of protobuf format of real-time barrage and historical barrage at station B
Alice and Bob (2021牛客暑期多校训练营1)
Ball Dropping
Penetration test (4) -- detailed explanation of meterpreter command
Optimization method of path problem before dynamic planning
[exercise -11] 4 values why sum is 0 (and 4 values of 0)
【练习-10】 Unread Messages(未读消息)
HDU - 6024 building shops (girls' competition)
Record of force deduction and question brushing
Web based photo digital printing website