Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created November 21, 2016 02:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jianminchen/7ef0dcb49662f277fda36742da5fa57c to your computer and use it in GitHub Desktop.
Save jianminchen/7ef0dcb49662f277fda36742da5fa57c to your computer and use it in GitHub Desktop.
Minimum Cost - study code - HackerRank - woman codesprint - study code - close to my idea
// C++11
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int n;
cin >> n;
vector<long long> p(n);
for (int i = 0; i < n; ++i)
cin >> p[i];
long long mn = -1;
vector<pair<long long, int> > po;
for (int i = 0; i < n; ++i)
po.push_back(make_pair(p[i], i));
sort(po.begin(), po.end());
for (int i = 0; i < n; ++i) {
int j;
for (j = i + 1; j < n && po[j].second >= po[i].second; ++j);
if (j < n && (mn == -1 || mn > po[j].first - po[i].first))
mn = po[j].first - po[i].first;
}
cout << mn << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment