【HackerRank】Service Lane
N
units. The service lane consists of N
segments of unit length, where each segment can have different widths.Calvin can enter into and exit from any segment. Let's call the entry segment as index i
and the exit segment as index j
. Assume that the exit segment lies after the entry segment(j
>i
) and i
≥ 0. Calvin has to pass through all segments from index i
to indexj
(both inclusive).
Calvin has three types of vehicles - bike, car and truck, represented by 1, 2 and 3 respectively. These numbers also denote the width of the vehicle. We are given an array width[]
of length N
, where width[k]
represents the width of k
th segment of our service lane. It is guaranteed that while servicing he can pass through at most 1000 segments, including entry and exit segments.
-
If
width[k]
is 1, only the bike can pass throughk
th segment. -
If
width[k]
is 2, the bike and car can pass throughk
th segment. -
If
width[k]
is 3, any of the bike, car or truck can pass throughk
th segment.
Given the entry and exit point of Calvin's vehicle in the service lane, output the type of largest vehicle which can pass through the service lane (including the entry & exit segment)
Input Format
The first line of input contains two integers - N
& T
, where N
is the length of the freeway, and T
is the number of test cases. The next line has N
space separated integers which represents the width
array.
T
test cases follow. Each test case contains two integers - i
& j
, where i
is the index of segment through which Calvin enters the service lane and j
is the index of the lane segment where he exits.
Output Format
For each test case, print the number that represents the largest vehicle type that can pass through the service lane.
Note
Calvin has to pass through all segments from index i
to indexj
(both inclusive).
Constraints
2 <= N <= 100000
1 <= T <= 1000
0 <= i < j < N
2 <= j-i+1 <= min(N,1000)
1 <= width[k] <= 3, where 0 <= k < N
题解:
1 import java.io.*; 2 import java.util.*; 3 import java.text.*; 4 import java.math.*; 5 import java.util.regex.*; 6 7 public class Solution { 8 static int Service_Lane(int[] lane,int enter,int exit){ 9 int minimal = Integer.MAX_VALUE; 10 for(int i = enter;i<=exit;i++) 11 minimal = Math.min(minimal, lane[i]); 12 return minimal; 13 } 14 15 public static void main(String[] args) { 16 Scanner in = new Scanner(System.in); 17 int n; 18 n = in.nextInt(); 19 int t; 20 t = in.nextInt(); 21 int[] lane = new int[n]; 22 for(int i = 0;i < n;i++){ 23 lane[i] = in.nextInt(); 24 } 25 for(int i = 0;i < t;i++){ 26 int enter = in.nextInt(); 27 int exit = in.nextInt(); 28 int mini = Service_Lane(lane, enter, exit); 29 if(mini >= 3) 30 System.out.println(3); 31 else if(mini >= 2) 32 System.out.println(2); 33 else 34 System.out.println(1); 35 } 36 } 37 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了