File tree 1 file changed +42
-0
lines changed
1 file changed +42
-0
lines changed Original file line number Diff line number Diff line change
1
+ // Time: O(n)
2
+ // Space: O(1)
3
+
4
+ // one pass, array
5
+ class Solution {
6
+ public:
7
+ int findNonMinOrMax (vector<int >& nums) {
8
+ int mx = numeric_limits<int >::min (), mn = numeric_limits<int >::max ();
9
+ int result = -1 ;
10
+ for (const auto & x : nums) {
11
+ if (mn < x && x < mx) {
12
+ return x;
13
+ }
14
+ if (x < mn) {
15
+ result = mn;
16
+ mn = x;
17
+ }
18
+ if (x > mx) {
19
+ result = mx;
20
+ mx = x;
21
+ }
22
+ }
23
+ return mn < result && result < mx ? result : -1 ;
24
+ }
25
+ };
26
+
27
+ // Time: O(n)
28
+ // Space: O(1)
29
+ // array
30
+ class Solution2 {
31
+ public:
32
+ int findNonMinOrMax (vector<int >& nums) {
33
+ const auto & mx = *max_element (cbegin (nums), cend (nums));
34
+ const auto & mn = *min_element (cbegin (nums), cend (nums));
35
+ for (const auto & x : nums) {
36
+ if (mn < x && x < mx) {
37
+ return x;
38
+ }
39
+ }
40
+ return -1 ;
41
+ }
42
+ };
You can’t perform that action at this time.
0 commit comments