-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinimumtimetofinishtasks withoutskippingtwoconsecutive.java
More file actions
140 lines (112 loc) · 3.16 KB
/
Copy pathMinimumtimetofinishtasks withoutskippingtwoconsecutive.java
File metadata and controls
140 lines (112 loc) · 3.16 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
Minimum time to finish tasks without skipping two consecutive
Given the time taken by n tasks. Find the minimum time needed to finish the tasks such that skipping of tasks is allowed, but can not skip two consecutive tasks.
Examples :
Input : arr[] = {10, 5, 7, 10}
Output : 12
We can skip first and last task and
finish these task in 12 min.
// Java program to find minimum time to
// finish tasks such that no two
// consecutive tasks are skipped.
import java.io.*;
class GFG {
// arr[] represents time taken by n
// given tasks
static int minTime(int arr[], int n)
{
// Corner Cases
if (n <= 0)
return 0;
// Initialize value for the case
// when there is only one task in
// task list.
// First task is included
int incl = arr[0];
// First task is excluded
int excl = 0;
// Process remaining n-1 tasks
for (int i = 1; i < n; i++)
{
// Time taken if current task is
// included. There are two
// possibilities
// (a) Previous task is also included
// (b) Previous task is not included
int incl_new = arr[i] + Math.min(excl,
incl);
// Time taken when current task is not
// included. There is only one
// possibility that previous task is
// also included.
int excl_new = incl;
// Update incl and excl for next
// iteration
incl = incl_new;
excl = excl_new;
}
// Return minimum of two values for
// last task
return Math.min(incl, excl);
}
// Driver code
public static void main(String[] args)
{
int arr1[] = {10, 5, 2, 7, 10};
int n1 = arr1.length;
System.out.println(minTime(arr1, n1));
int arr2[] = {10, 5, 7, 10};
int n2 = arr2.length;
System.out.println(minTime(arr2, n2));
int arr3[] = {10, 5, 2, 4, 8, 6, 7, 10};
int n3 = arr3.length;
System.out.println(minTime(arr3, n3));
}
}
// This code is contributed by Prerna Saini
// C++ program to find minimum time to finish tasks
// such that no two consecutive tasks are skipped.
#include <bits/stdc++.h>
using namespace std;
// arr[] represents time taken by n given tasks
int minTime(int arr[], int n)
{
// Corner Cases
if (n <= 0)
return 0;
// Initialize value for the case when there
// is only one task in task list.
int incl = arr[0]; // First task is included
int excl = 0; // First task is excluded
// Process remaining n-1 tasks
for (int i=1; i<n; i++)
{
// Time taken if current task is included
// There are two possibilities
// (a) Previous task is also included
// (b) Previous task is not included
int incl_new = arr[i] + min(excl, incl);
// Time taken when current task is not
// included. There is only one possibility
// that previous task is also included.
int excl_new = incl;
// Update incl and excl for next iteration
incl = incl_new;
excl = excl_new;
}
// Return minimum of two values for last task
return min(incl, excl);
}
// Driver code
int main()
{
int arr1[] = {10, 5, 2, 7, 10};
int n1 = sizeof(arr1)/sizeof(arr1[0]);
cout << minTime(arr1, n1) << endl;
int arr2[] = {10, 5, 7, 10};
int n2 = sizeof(arr2)/sizeof(arr2[0]);
cout << minTime(arr2, n2) << endl;
int arr3[] = {10, 5, 2, 4, 8, 6, 7, 10};
int n3 = sizeof(arr3)/sizeof(arr3[0]);
cout << minTime(arr3, n3) << endl;
return 0;
}