[Hackerrank] service lane

Source: Internet
Author: User
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 NUnits. The service lane consists NSegments 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 indexiAnd the exit segment as indexj. Assume that the exit segment lies after the entry segment (j>i) Andi≥0.Calvin has to pass through all segments from IndexiTo Indexj(Both random ).

 

Calvin has three types of vehicles-bike, car and truck, represented1,2And3Respectively. These numbers also denote the width of the vehicle. We are given an arraywidth[]Of LengthN, Wherewidth[k]Represents the widthkThSegment of our service lane. it is guaranteed that while servicing he can pass through at most 1000 segments, including entry and exit segments.

  • Ifwidth[k]Is 1, only the bike can pass throughkThSegment.

  • Ifwidth[k]Is 2, the bike and car can pass throughkThSegment.

  • Ifwidth[k]Is 3, any of the bike, car or truck can pass throughkThSegment.

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, WhereNIs the length of the freeway, andTIs the number of test cases. The next line hasNSpace separated integers which representswidthArray.

TTest Cases Follow. Each test case contains two integers-i&j, WhereiIs the index of segment through which Calvin enters the service lane andjIs 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 IndexiTo Indexj(Both random ).

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

 

Question:

 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             else34                 System.out.println(1);35         }36    }37 }

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.