Determining Mobile Phone Charging Time Based on Rates
Last Updated :
05 Dec, 2023
Given an initial charge amount C in a mobile phone, it undergoes varying charging rates per minute, as specified within specific ranges ‘Rate’ based on the current charge level. These charging rate ranges are organized in Sorted Order, where each range is defined by three values: a starting point (inclusive), an ending point (non-inclusive), and an associated charging rate (See Example for more clarity). The collective span of all the ranges within the ‘Rate‘ array encompasses values from 0 to 100, covering all intermediary ranges. Your task is to calculate the time required to charge the mobile phone to a specified charge level (T).
Examples:
Input: C = 30, T = 90, Rate = [[0, 30, 4], [30, 60, 10], [60, 90, 5], [90, 100, 10]]
Output: 9
Explanation: Initially mobile charged C = 30 so, to reach 30 to 60 it will charge 10 unit per min which will take 3 min, then to reach 60 to 90 it will charge 5 unit per min which will take 6 min, So in total it will take 3+6=9 min.
Input: C = 70, T = 95, Rate = [[0, 30, 4], [30, 60, 10], [60, 90, 5], [90, 100, 10]]
Output: 4.5
Explanation: Initially mobile charged C = 70 so, to reach 70 to 90 it will charge 5 unit per min which will take 4 min, then to reach 90 to 95 it will charge 10 unit per min which will take 0.5 min, So in total it will take 4+0.5 = 4.5 min .
Approach: To solve the problem follow the below idea:
Iterate through the charging rate ranges (Rates), checking if the current charge level (C) has met the target charge level (T) to exit the loop early. If not, it verifies if C falls within a specific range, computes the needed charge and corresponding time to reach the range’s end using the provided charging rate, and continuously adds up these times to calculate the overall charging duration. Ultimately, it returns the total time required for the entire charging process.
Below is the implementation of the above approach.
C++
#include <iostream>
#include <vector>
using namespace std;
double chargeNeeded( int C, int T,
vector<vector< int > >& Rate)
{
double Time = 0;
for ( const vector< int >& range : Rate) {
int start = range[0];
int end = range[1];
int charge_per_minute = range[2];
if (C == T) {
break ;
}
else if (C < end) {
int charge_needed = min(end, T) - C;
double time_required
= static_cast < double >(charge_needed)
/ charge_per_minute;
Time += time_required;
C += charge_needed;
}
}
return Time;
}
int main()
{
int C = 30;
int T = 90;
vector<vector< int > > Rate = { { 0, 30, 4 },
{ 30, 60, 10 },
{ 60, 90, 5 },
{ 90, 100, 10 } };
cout << chargeNeeded(C, T, Rate) << endl;
return 0;
}
|
Java
import java.util.ArrayList;
import java.util.List;
public class ChargingTime {
static int chargeNeeded( int C, int T,
List<List<Integer>> Rate) {
int Time = 0 ;
for (List<Integer> range : Rate) {
int start = range.get( 0 );
int end = range.get( 1 );
int chargePerMinute = range.get( 2 );
if (C == T) {
break ;
}
else if (C < end) {
int chargeNeeded = Math.min(end, T) - C;
double timeRequired = ( double ) chargeNeeded / chargePerMinute;
Time += timeRequired;
C += chargeNeeded;
}
}
return Time;
}
public static void main(String[] args) {
int C = 30 ;
int T = 90 ;
List<List<Integer>> Rate = new ArrayList<>();
Rate.add(List.of( 0 , 30 , 4 ));
Rate.add(List.of( 30 , 60 , 10 ));
Rate.add(List.of( 60 , 90 , 5 ));
Rate.add(List.of( 90 , 100 , 10 ));
System.out.println(chargeNeeded(C, T, Rate));
}
}
|
Python3
def chargeNeeded(C, T, Rate):
Time = 0
for start, end, charge_per_minute in Rate:
if C = = T:
break
elif C < end:
charge_needed = min (end, T) - C
time_required = charge_needed / charge_per_minute
Time + = time_required
C + = charge_needed
return Time
C = 30
T = 90
Rate = [[ 0 , 30 , 4 ], [ 30 , 60 , 10 ], [ 60 , 90 , 5 ], [ 90 , 100 , 10 ]]
print (chargeNeeded(C, T, Rate))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static double ChargeNeeded( int C, int T, List<List< int >> Rate)
{
double Time = 0;
foreach (List< int > range in Rate)
{
int start = range[0];
int end = range[1];
int chargePerMinute = range[2];
if (C == T)
{
break ;
}
else if (C < end)
{
int chargeNeeded = Math.Min(end, T) - C;
double timeRequired = ( double )chargeNeeded / chargePerMinute;
Time += timeRequired;
C += chargeNeeded;
}
}
return Time;
}
static void Main()
{
int C = 30;
int T = 90;
List<List< int >> Rate = new List<List< int >>()
{
new List< int > {0, 30, 4},
new List< int > {30, 60, 10},
new List< int > {60, 90, 5},
new List< int > {90, 100, 10}
};
Console.WriteLine(ChargeNeeded(C, T, Rate));
}
}
|
Javascript
function GFG(C, T, Rate) {
let Time = 0;
for (let i = 0; i < Rate.length; i++) {
const [start, end, chargePerMinute] = Rate[i];
if (C === T) {
break ;
}
else if (C < end) {
const GFG = Math.min(end, T) - C;
const timeRequired = GFG / chargePerMinute;
Time += timeRequired;
C += GFG;
}
}
return Time;
}
const C = 30;
const T = 90;
const Rate = [[0, 30, 4], [30, 60, 10], [60, 90, 5], [90, 100, 10]];
console.log(GFG(C, T, Rate));
|
Time Complexity: O(N), where n is the number of different Rates.
Auxiliary Space: O(1).
Share your thoughts in the comments
Please Login to comment...