Monday, February 16, 2026

Going to office - minimize the travel cost (Super Easy)

Index of "Algorithms: Design and Analysis"
<<< Previous    Next >>>

All Tracks > Basic Programming > Operators > Basics of Operators

Solve on HackerEarth

Problem
Alice has the following two types of taxis:

Online taxi: It can be booked by using an online application from phones 
Classic taxi: It can be booked anywhere on the road
The online taxis cost 
 for the first 
 km and 
 for every km afterward. The classic taxis travel at a speed of 
 km per minute. The cost of classic taxis are 
, 
, and 
 that represents the base fare, cost for every minute that is spent in the taxi, and cost for each kilometer that you ride.

You are going to the office from your home. Your task is to minimize the cost that you are required to pay. The distance from your home to the office is D. You are required to select whether you want to use online or classic taxis to go to your office. If both the taxis cost the same, then you must use an online taxi.

Input format

First line: Single integer 
 that denotes the distance from your house to the office
Next line: Three integers 
 , 
 , and 
  
Next line: Four integers 
 , 
 , 
 , and 
Output format

If you select an online taxi to travel, then print a string 'Online Taxi'. Otherwise, select 'Classic Taxi'. You can print this string in a new, single line.

Constraints

1 
 
 
 109

Sample Input
13
6 7 4
4 2 1 2
Sample Output
Online Taxi
Time Limit: 1
Memory Limit: 64
Source Limit:
Explanation
If Felix choose to use Online Taxi, it will cost Felix a total of 

While the classic taxi will cost Felix a total of 

Therefore Felix will choose Online Taxi over Classic Taxi

My Code


D = int(input())

oc, of, od = map(int, input().split())

ca, cb, cm, cd = map(int, input().split())

def online_cost(D, oc, of, od):
    if D <= of:
        return oc
    
    else:
        cost = oc

        rem_dist = D - of
        cost += (rem_dist * od)

        return cost

def offline_cost(D, ca, cb, cm, cd):
    cost = cb

    t = D/ca
    cost += ((t * cm) + (D * cd))
    return cost

on_cost = online_cost(D, oc, of, od)
off_cost = offline_cost(D, ca, cb, cm, cd)

if off_cost < on_cost:
    print("Classic Taxi")
else:
    print("Online Taxi")

ChatGPT's Code

D = int(input())

Oc, Of, Od = map(int, input().split())
Cs, Cb, Cm, Cd = map(int, input().split())

# Online Taxi Cost
if D <= Of:
    online_cost = Oc
else:
    online_cost = Oc + (D - Of) * Od

# Classic Taxi Cost
time = D / Cs
classic_cost = Cb + (time * Cm) + (D * Cd)

# Compare (If equal, choose Online Taxi)
if online_cost <= classic_cost:
    print("Online Taxi")
else:
    print("Classic Taxi")

Note: Both are accepted.

No comments:

Post a Comment