Problem Statement
Calculate how much it would cost to ship a package weighing weight pounds from sourceZipCode to destZipCode. You are given a String[], zones, which defines the locations of various zip codes. Each element of zones is a single space delimited list of five-digit integers. Element i (zero-indexed) of zones contains all the zip codes located in zone i. You are also given a int[], rates, where the ith element (zero-indexed) is the shipping cost per pound of a package sent between two zones whose difference is i. The difference between zone z1 and zone z2 is defined as the absolute value of z1 minus z2.
Definition
- Class:
- ShippingZones
- Method:
- getCost
- Parameters:
- int, int, int, String[], int[]
- Returns:
- int
- Method signature:
- int getCost(int weight, int sourceZipCode, int destZipCode, String[] zones, int[] rates)
- (be sure your method is public)
Constraints
- weight will be between 1 and 100, inclusive.
- sourceZipCode and destZipCode will each be between 10000 and 99999, inclusive.
- zones will contain between 1 and 50 elements, inclusive.
- Each element of zones will contain between 5 and 47 characters, inclusive.
- Each element of zones will be a single space delimited list of one or more five-digit integers, with no leading or trailing spaces.
- zones will contain no duplicate integers.
- Both sourceZipCode and destZipCode will exist in zones.
- rates will contain the same number of elements as zones.
- Each element of rates will be between 1 and 10, inclusive.
Examples
50
48105
79915
{"94305", "79915 80201", "48105", "13122"}{1, 2, 3, 4}Returns: 100
In this example, a 50 pound package is shipped from zip code 48105 to zip code 79915. 48105 is in element 2 of zones, so that means 48105 is located in zone 2. 79915 is located in zone 1. The difference between zones is 1, and element 1 of rates is 2. So the shipping cost is 50 * 2 = 100.2
94305
94305
{"94305 94309", "53022 23222", "10020 00022"}{5, 7, 9}Returns: 10
The source and destination zip codes are the same, so the difference in zones is 0. It will cost 2 * 5 = 10.100
48105
48104
{"93202", "48105 48103 48104 88000"}{9, 8}Returns: 900
The source and destination zip codes are different, but they are located in the same zone.1
10000
99999
{"10000 20000", "30000 40000", "50000 60000", "70000 80000", "90000 99999"}{1, 2, 3, 4, 5}Returns: 5
The difference in zones between 10000 and 99999 is 4.