MaxProfit::solution()   B
last analyzed

Complexity

Conditions 5
Paths 2

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 19
rs 8.8571
cc 5
eloc 11
nc 2
nop 1
1
<?php
2
3
namespace Lesson07;
4
5
class MaxProfit
6
{
7
    public function solution($A)
8
    {
9
        $N = count($A);
10
        $maxProfit = 0;
11
        if ($N) {
12
            $minPrice = $A[0];
13
            for ($i = 1; $i < $N; $i++) {
14
                if ($A[$i] < $minPrice) {
15
                    $minPrice = $A[$i];
16
                }
17
18
                if ($A[$i] - $minPrice > $maxProfit) {
19
                    $maxProfit = $A[$i] - $minPrice;
20
                }
21
            }
22
        }
23
24
        return $maxProfit;
25
    }
26
}
27