Skip to content

Instantly share code, notes, and snippets.

@greggjaskiewicz
Created April 8, 2016 20:33
Show Gist options
  • Save greggjaskiewicz/af8fbe9968d8b9a52d937372af2c7d65 to your computer and use it in GitHub Desktop.
Save greggjaskiewicz/af8fbe9968d8b9a52d937372af2c7d65 to your computer and use it in GitHub Desktop.
#import <Foundation/Foundation.h>
NSString *testString = @"00:01:07,400-234-090\n00:05:01,701-080-080\n00:05:00,400-234-090";
NSTimeInterval durationToSeconds(NSString *duration)
{
NSDateFormatter *df = [NSDateFormatter new];
df.dateFormat = @"hh:mm:ss";
NSDate *reference = [df dateFromString:@"00:00:00"];
NSDate *foox = [df dateFromString:duration];
NSTimeInterval seconds = [foox timeIntervalSinceDate:reference];
return seconds;
}
int calculateCostFromDuration(NSTimeInterval duration)
{
int cost = 0;
if (duration > (5*60))
{
float d = duration/60.0;
float ceil = ceilf(d);
cost = ((int)ceil) * 150;
}
else
{
cost = (int)duration*3;
}
return cost;
}
int solution(NSString *S)
{
int totalCost = 0;
NSMutableDictionary *lengthByPhoneNumber = [NSMutableDictionary dictionary];
NSMutableDictionary *costByPhoneNumber = [NSMutableDictionary dictionary];
NSArray *lines = [S componentsSeparatedByString:@"\n"];
for(NSString *line in lines)
{
NSArray *elements = [line componentsSeparatedByString:@","];
if (elements.count == 2)
{
NSTimeInterval length = durationToSeconds(elements[0]);
NSString *number = elements[1];
if (lengthByPhoneNumber[number] != nil)
{
NSNumber *currentLenght = lengthByPhoneNumber[number];
length = length + [currentLenght integerValue];
}
lengthByPhoneNumber[number] = @((int)length);
int cost;
cost = calculateCostFromDuration(length);
if (costByPhoneNumber[number] != nil)
{
NSNumber *costNumber = costByPhoneNumber[number];
cost = cost + [costNumber intValue];
}
costByPhoneNumber[number] = @(cost);
}
}
// find longest call number
NSString *longestCallNumber;
int longestCallValue = 0;
for(NSString *number in [lengthByPhoneNumber allKeys])
{
NSNumber *lenghtNumber = lengthByPhoneNumber[number];
if ([lenghtNumber intValue] > longestCallValue)
{
longestCallNumber = number;
longestCallValue = [lenghtNumber intValue];
}
}
if (lengthByPhoneNumber.count > 1 && longestCallNumber != nil)
{
[lengthByPhoneNumber removeObjectForKey:longestCallNumber];
[costByPhoneNumber removeObjectForKey:longestCallNumber];
}
// add cost of all bar last element in the array
for(NSString *number in [costByPhoneNumber allKeys])
{
NSNumber *cost = costByPhoneNumber[number];
totalCost = totalCost + [cost intValue];
}
return totalCost;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment