Skip to content

Commit

Permalink
add 539
Browse files Browse the repository at this point in the history
  • Loading branch information
cshanbo committed Mar 26, 2017
1 parent cae96a0 commit 5e650b5
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions 539.min_time_difference.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class Solution {
public:
int findMinDifference(vector<string>& times) {
int n = times.size();
sort(times.begin(), times.end());
int mindiff = INT_MAX;
for (int i = 0; i < times.size(); i++) {
int diff = abs(timeDiff(times[(i - 1 + n) % n], times[i]));
diff = min(diff, 1440 - diff);
mindiff = min(mindiff, diff);
}
return mindiff;
}

private:
int timeDiff(string t1, string t2) {
int h1 = stoi(t1.substr(0, 2));
int m1 = stoi(t1.substr(3, 2));
int h2 = stoi(t2.substr(0, 2));
int m2 = stoi(t2.substr(3, 2));
return (h2 - h1) * 60 + (m2 - m1);
}
};

0 comments on commit 5e650b5

Please sign in to comment.