Calvin is driving his favorite vehicle on the 101 freeway. He notices that the check engine light of his vehicle is on, and he wants to service it immediately to avoid any risks. Luckily, a service lane runs parallel to the highway. The length of the highway and the service lane is 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 kth 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 through kth segment.

  • If width[k] is 2, the bike and car can pass through kth segment.

  • If width[k] is 3, any of the bike, car or truck can pass through kth 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


题解:

 import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*; public class Solution {
static int Service_Lane(int[] lane,int enter,int exit){
int minimal = Integer.MAX_VALUE;
for(int i = enter;i<=exit;i++)
minimal = Math.min(minimal, lane[i]);
return minimal;
} public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n;
n = in.nextInt();
int t;
t = in.nextInt();
int[] lane = new int[n];
for(int i = 0;i < n;i++){
lane[i] = in.nextInt();
}
for(int i = 0;i < t;i++){
int enter = in.nextInt();
int exit = in.nextInt();
int mini = Service_Lane(lane, enter, exit);
if(mini >= 3)
System.out.println(3);
else if(mini >= 2)
System.out.println(2);
else
System.out.println(1);
}
}
}

【HackerRank】Service Lane的更多相关文章

  1. 【BZOJ1969】[Ahoi2005]LANE 航线规划 离线+树链剖分+线段树

    [BZOJ1969][Ahoi2005]LANE 航线规划 Description 对Samuel星球的探险已经取得了非常巨大的成就,于是科学家们将目光投向了Samuel星球所在的星系——一个巨大的由 ...

  2. 【angularJS】Service服务

    AngularJS 中的服务是一个函数或对象.可以创建自己的服务,或使用内建服务. 内置服务 AngularJS 内建了30 多个服务. 1.  $location 服务,它可以返回当前页面的 URL ...

  3. 【笔记】Service的使用

    一.创建Service 1.创建一个myService类,来继承Service.重写其中的方法,包括:onCreate(),onStartCommend(),onDestroy(),onBind()方 ...

  4. 【Android 】Service 全面总结

    1.Service的种类 按运行地点分类: 类别 区别  优点 缺点   应用 本地服务(Local) 该服务依附在主进程上,  服务依附在主进程上而不是独立的进程,这样在一定程度上节约了资源,另外L ...

  5. 【转】Service Intent must be explicit的解决方法

    原文网址:http://blog.csdn.net/shenzhonglaoxu/article/details/42675287 今天在学习android的Service组件的时候,在Android ...

  6. 【mysql】service mysql start出错,mysql启动不了,解决mysql: unrecognized service错误

    service MySQL start出错,mysql启动不了,解决mysql: unrecognized service错误的方法如下: [root@ctohome.com ~]# service ...

  7. 【HackerRank】How Many Substrings?

    https://www.hackerrank.com/challenges/how-many-substrings/problem 题解 似乎是被毒瘤澜澜放弃做T3的一道题(因为ASDFZ有很多人做过 ...

  8. 【mybatis】service层中一个方法中使用mybatis进行数据库的 多个修改操作,可能是update也可能是delete操作,但是sql语句命名执行并且在控制台打印出来了,但是数据库中未更新到数据【事务的问题】

    问题描述: service层中一个方法中使用mybatis进行数据库的 多个修改操作,可能是update也可能是delete操作,但是sql语句命名执行并且在控制台打印出来了,但是数据库中未更新到数据 ...

  9. 【HackerRank】Running Time of Quicksort

    题目链接:Running Time of Quicksort Challenge In practice, how much faster is Quicksort (in-place) than I ...

随机推荐

  1. 2017-5-14 湘潭市赛 Strange Optimization

    Strange Optimization Accepted : Submit : Time Limit : MS Memory Limit : KB Strange Optimization Bobo ...

  2. C# WEB 不显示目录结构

    <system.webServer> <directoryBrowse enabled="false" /> </system.webServer&g ...

  3. python 开发技巧(0)-- 各个系统的python安装

    window安装 Python的安装: 1.进入Python官方网站下载安装包 点击导航栏的 Downloads 会自动识别你的windows系统  你会看到  Python3.6.2 和 Pytho ...

  4. es6中顶层对象属性≠全局属性

    先思考一下下面代码的输出结果是什么 const a = { x:1, fn:()=>this.x+=1 } const x = 1 a.fn() console.log(a.x,x) 正确答案为 ...

  5. PHP运算符===和==的区别

    区别: ===是恒等计算符   同时检查表达式的值与类型 ==是比较运算符号  不会检查条件式的表达式的类型 转自:  http://jingyan.baidu.com/article/03b2f78 ...

  6. Cocoapod安装使用和常见问题

    1.cocoapod的按照,cocoapod是运行在ruby环境下的,在ruby环境的 ,像cocoapod这样的开源项目时放在放在rubygems服务器上面的,但国内访问https://rubyge ...

  7. 浅谈.net平台下深拷贝和浅拷贝

    在.net类库中,对象克隆广泛存在于各种类型的实现中,凡是实现了ICloneable接口的类型都具备克隆其对象实例的能力.所以本文讲述的深拷贝和浅拷贝也是在实现ICloneable接口的基础上进行的 ...

  8. Android-ViewPagerIndicator框架使用——TitlePageIndicator

    前言:TitlePageIndicator这个就是效果比较好. 一:定义布局文件simple_titles: <LinearLayout xmlns:android="http://s ...

  9. HBuilder 连接 夜神安卓模拟器

    由于HBuilder的自动扫描机制无法直接连上夜神模拟器,需要通过cmd命令窗口手动处理,才能将两者建立连接. 首先,启动HBuilder和夜神模拟器 然后,打开cmd命令提示符 cd进入夜神模拟器b ...

  10. poj 1515+poj 1438(边双连通)

    题目链接:http://poj.org/problem?id=1515 思路:题目的意思是说将一个无向图改成有向图,使其成为强连通,输出所有的边.我们可以求无向图的边双连通分量,对于同一个双连通分量, ...