You parked your car in a parking lot and want to compute the total cost of the ticket. The billing rules are as follows:
- The entrance fee of the car parking lot is 2;
- The first full or partial hour costs 3;
- Each successive full or partial hour (after the first) costs 4.
You entered the car parking lot at time E and left at time L. In this task, times are represented as strings in the format "HH:MM" (where "HH" is a two-digit number between 0 and 23, which stands for hours, and "MM" is a two-digit number between 0 and 59, which stands for minutes).
Write a function:
class Solution { public int solution(String E, String L); }
that, given strings E and L specifying points in time in the format "HH:MM", returns the total cost of the parking bill from your entry at time E to your exit at time L. You can assume that E describes a time before L on the same day.
For example, given "10:00" and "13:21" your function should return 17, because the entrance fee equals 2, the first hour costs 3 and there are two more full hours and part of a further hour, so the total cost is 2 + 3 + (3 * 4) = 17. Given "09:42" and "11:42" your function should return 9, because the entrance fee equals 2, the first hour costs 3 and the second hour costs 4, so the total cost is 2 + 3 + 4 = 9.
Assume that:
- strings E and L follow the format "HH:MM" strictly;
- string E describes a time before L on the same day.
In your solution, focus on correctness. The performance of your solution will not be the focus of the assessment.
import Foundation
import Glibc
// you can write to stdout for debugging purposes, e.g.
// print("this is a debug message")
public func solution(_ E : inout String, _ L : inout String) -> Int {
let e = E.components(separatedBy: ":").map({ Int($0)! })
let l = L.components(separatedBy: ":").map({ Int($0)! })
return 2 + 3 + (l[0] - e[0] - 1) * 4 + (l[1] > 0 ? 4 : 0)
}
import Foundation
import Glibc
// you can write to stdout for debugging purposes, e.g.
// print("this is a debug message")
public func solution(_ E : inout String, _ L : inout String) -> Int {
let e = E.components(separatedBy: ":").map({ Int($0)! })
let l = L.components(separatedBy: ":").map({ Int($0)! })
return 2 + 3 + (l[0] - e[0] - 1) * 4 + (l[1] > 0 ? 4 : 0)
}
import Foundation
import Glibc
// you can write to stdout for debugging purposes, e.g.
// print("this is a debug message")
public func solution(_ E : inout String, _ L : inout String) -> Int {
let e = E.components(separatedBy: ":").map({ Int($0)! })
let l = L.components(separatedBy: ":").map({ Int($0)! })
return 2 + 3 + (l[0] - e[0] - 1) * 4 + (l[1] > 0 ? 4 : 0)
}
import Foundation
import Glibc
// you can write to stdout for debugging purposes, e.g.
// print("this is a debug message")
public func solution(_ E : inout String, _ L : inout String) -> Int {
let e = E.components(separatedBy: ":").map({ Int($0)! })
let l = L.components(separatedBy: ":").map({ Int($0)! })
let startHour = e[0]
let startMinute = e[1]
let endHour = l[0]
let endMinute = l[1]
var time = 0
if startHour < endHour {
if startMinute > 0 {
time += 60 - startMinute
} else {
time += 60
}
time += (endHour - startHour - 1) * 60 + endMinute
} else {
time += endMinute
}
if time == 0 {
return 2
} else if time <= 60 {
return 5
} else {
return 2 + 3 + ((time - 60) / 60) * 4 + (time % 60 > 0 ? 4 : 0)
}
}
import Foundation
import Glibc
// you can write to stdout for debugging purposes, e.g.
// print("this is a debug message")
public func solution(_ E : inout String, _ L : inout String) -> Int {
let e = E.components(separatedBy: ":").map({ Int($0)! })
let l = L.components(separatedBy: ":").map({ Int($0)! })
let startHour = e[0]
let startMinute = e[1]
let endHour = l[0]
let endMinute = l[1]
var time = 0
if startHour < endHour {
if startMinute > 0 {
time += 60 - startMinute
} else {
time += 60
}
time += (endHour - startHour - 1) * 60 + endMinute
} else {
time += endMinute
}
if time == 0 {
return 2
} else if time <= 60 {
return 5
} else {
return 2 + 3 + ((time - 60) / 60) * 4 + (time % 60 > 0 ? 4 : 0)
}
}
import Foundation
import Glibc
// you can write to stdout for debugging purposes, e.g.
// print("this is a debug message")
public func solution(_ E : inout String, _ L : inout String) -> Int {
let e = E.components(separatedBy: ":").map({ Int($0)! })
let l = L.components(separatedBy: ":").map({ Int($0)! })
let startHour = e[0]
let startMinute = e[1]
let endHour = l[0]
let endMinute = l[1]
var time = 0
if startHour < endHour {
if startMinute > 0 {
time += 60 - startMinute
} else {
time += 60
}
time += (endHour - startHour - 1) * 60 + endMinute
} else {
time += endMinute
}
if time == 0 {
return 2
} else if time <= 60 {
return 5
} else {
return 2 + 3 + ((time - 60) / 60) * 4 + (time % 60 > 0 ? 4 : 0)
}
}
import Foundation
import Glibc
// you can write to stdout for debugging purposes, e.g.
// print("this is a debug message")
public func solution(_ E : inout String, _ L : inout String) -> Int {
let e = E.components(separatedBy: ":").map({ Int($0)! })
let l = L.components(separatedBy: ":").map({ Int($0)! })
let startHour = e[0]
let startMinute = e[1]
let endHour = l[0]
let endMinute = l[1]
var time = 0
if startHour < endHour {
if startMinute > 0 {
time += 60 - startMinute
} else {
time += 60
}
time += (endHour - startHour - 1) * 60 + endMinute
} else {
time += endMinute
}
if time == 0 {
return 2
} else if time <= 60 {
return 5
} else {
return 2 + 3 + ((time - 60) / 60) * 4 + (time % 60 > 0 ? 4 : 0)
}
}
The solution obtained perfect score.