-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask_max.php
52 lines (48 loc) · 1.64 KB
/
task_max.php
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php
/**
Author - Eneh James
Description - PHP code snipet to calculate the maximum number of tasks a military unit can accomplish in limited time, 'X'. Using the greedy algorithm.
Date/Time - 12/01/2018 / 21:12:00
**/
define("X", 12); //Constant 'X' holding limited time value in minutes
$A = array(7, 6, 5, 3, 4, 2, 1); //Integer array 'A' holding the time value it will take to complete different tasks
$currentTime = 0; //Declaration and Initialization of currentTime variable
$numberOfThings = 0; //Declaration and Initialization of numberOfThings variable
sort($A); //Sort A array in non-decreasing order
for($i=0;$i<count($A);$i++) { //Iteration block starts here
$currentTime += $A[$i]; //Add completion time of the current to-do item to currentTime variable
if($currentTime > X) //Test if currentTime is greater than X
break; //Breaks out of the loop if currentTime is greater than X
$numberOfThings++; //Increment numberOfThings variable by one (1)
} //Iteration block stops here
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Maximum Tasks</title>
<style type="text/css">
*{
font: 14px normal Helvetica, Arial, sans-serif;
color: #4F5155;
}
body {
background-color: #fff;
margin: 50px;
}
strong {
color: #ff4000;
font-size: 16px;
font-weight: bold;
}
span {
font-weight: bold;
font-style: italic;
}
</style>
</head>
<body>
<span>The maximum number of tasks to be completed by the military unit, in limited time of
<?php echo X . " minutes"; ?> is: </span><strong><?php echo $numberOfThings; ?></strong>
</body>
</html>