-
Notifications
You must be signed in to change notification settings - Fork 4
/
7-may.js
124 lines (101 loc) · 2.23 KB
/
7-may.js
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
// minimum and maximum.
function min(initializer, curr) {
return curr < initializer ? curr : initializer;
}
function max(initializer, curr) {
return curr > initializer ? curr : initializer;
}
let arr = [2, 4, 62, 1, 0, 8, 9, -1]
// console.log(arr.reduce(min))
// console.log(arr.reduce(max))
// reverse
function reverse(arr) {
let chintu = 0;
let mintu = arr.length - 1;
while(chintu < mintu) {
swap(arr, chintu, mintu);
chintu++;
mintu--;
}
}
function swap(arr, chintu, mintu) {
let temp = arr[chintu];
arr[chintu] = arr[mintu];
arr[mintu] = temp;
}
// reverse(arr)
// console.log(arr)
function count(initializer, curr) {
if(curr === 0) {
initializer + 1;
}
return initializer;
}
function sortZeroOneCount(arr) {
let countZero = arr.reduce(count);
let result = [];
for(let i=1;i<=countZero;i++) {
result.push(0);
}
for(let i=1;i<=arr.length - countZero;i++) {
result.push(1);
}
return result;
}
function sortZeroOne(arr) {
let chintu =0;
for(let mintu=0; mintu<arr.length; mintu++) {
if(arr[mintu] === 0) {
swap(arr, chintu, mintu)
chintu++;
}
}
}
// let arr1 = [1, 1, 1, 1, 1]
// sortZeroOne(arr1)
// console.log(arr1)
function maxProfit(arr) {
let maxProfit = 0;
let priceTillNow = arr[0];
for(let value of arr) {
if(value > priceTillNow) {
maxProfit+=value - priceTillNow;
}
priceTillNow = value;
}
return maxProfit;
}
console.log(maxProfit([100, 180, 260, 310, 40, 535, 695]))
// 1, 2, 5, 4, 3, 6, 7
// i = 3
// j = 5
console.log(checkSubarray([1, 2, 5, 4, 3, 6, 7]))
function checkSubarray(arr) {
let n = arr.length;
let i;
for(i=1;i < n && arr[i-1] < arr[i];i++) {}
if(i === n) {
return true;
}
let j = i;
while(j < n && arr[j-1] >= arr[j]) {
if(i > 1 && arr[i-2] > arr[j]) {
return false;
}
j++;
}
if(j === n) {
return true;
}
let k = j;
if(arr[i-1] > arr[k]) {
return false;
}
while(k < n) {
if(arr[k-1] > arr[k]) {
return false;
}
k++;
}
return true;
}