-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathminimum-loss.cpp
More file actions
37 lines (32 loc) · 945 Bytes
/
minimum-loss.cpp
File metadata and controls
37 lines (32 loc) · 945 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// Author: btjanaka (Bryon Tjanaka)
// Problem: (HackerRank) minimum-loss
// Title: Minimum Loss
// Link: https://www.hackerrank.com/challenges/minimum-loss/problem
// Idea: As we go through the array, use an ordered set to keep track of numbers
// we have seen so far. We can then search in this set in O(log n) time for the
// first number that is >= to the current one and then compute the loss. Note
// that one implementation of the ordered set is the C++ set.
// Difficulty: medium
// Tags: binary-search
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
cin.tie(NULL);
ios_base::sync_with_stdio(false);
int n;
cin >> n;
set<ll> nums;
ll minLoss = LLONG_MAX;
for (int i = 0; i < n; ++i) {
ll x;
cin >> x;
auto itr = nums.lower_bound(x);
if (itr != nums.end()) {
minLoss = min(minLoss, *itr - x);
}
nums.insert(x);
}
cout << minLoss << endl;
return 0;
}