当前位置:网站首页>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);
}
边栏推荐
- b站 实时弹幕和历史弹幕 Protobuf 格式解析
- Opencv learning log 15 count the number of solder joints and output
- PySide6 信号、槽
- Common configuration files of SSM framework
- 信息安全-威胁检测-NAT日志接入威胁检测平台详细设计
- Information security - security professional name | CVE | rce | POC | Vul | 0day
- Ball Dropping
- 渗透测试 ( 3 ) --- Metasploit Framework ( MSF )
- 【高老师UML软件建模基础】20级云班课习题答案合集
- Interesting drink
猜你喜欢
Essai de pénétration (1) - - outils nécessaires, navigation
洛谷P1102 A-B数对(二分,map,双指针)
[analysis of teacher Gao's software needs] collection of exercises and answers for level 20 cloud class
Quick to typescript Guide
Differential (one-dimensional, two-dimensional, three-dimensional) Blue Bridge Cup three body attack
快速转 TypeScript 指南
Information security - threat detection engine - common rule engine base performance comparison
基于web的照片数码冲印网站
【练习-7】Crossword Answers
Ball Dropping
随机推荐
渗透测试 ( 4 ) --- Meterpreter 命令详解
nodejs爬虫
TCP's three handshakes and four waves
【练习-10】 Unread Messages(未读消息)
F - Birthday Cake(山东省赛)
Essai de pénétration (1) - - outils nécessaires, navigation
Pyside6 signal, slot
[exercise -10] unread messages
渗透测试 ( 2 ) --- 渗透测试系统、靶机、GoogleHacking、kali工具
C basic grammar
【练习-2】(Uva 712) S-Trees (S树)
最全编程语言在线 API 文档
Opencv learning log 15 count the number of solder joints and output
洛谷P1102 A-B数对(二分,map,双指针)
b站 實時彈幕和曆史彈幕 Protobuf 格式解析
Interesting drink
B - 代码派对(女生赛)
0-1 knapsack problem (I)
Research Report on shell heater industry - market status analysis and development prospect forecast
E. Breaking the Wall