-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
43 lines (42 loc) · 1.1 KB
/
index.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
/**
* Represents a class for finding the maximum subarray using Depth-First Search (DFS) algorithm.
*/
class MaximumSubarrayDFS {
constructor(data) {
this.data = data
}
/**
* Finds the maximum subarray in the given data.
* @returns {Object} The result object containing the maximum subarray information.
*/
findMaxSubarray() {
let maxSum = -Infinity
let maxStartIndex = 0
let maxEndIndex = 0
let currentSum = 0
let startIndex = 0
for (let i = 0; i < this.data.length; i++) {
const closingPrice = parseFloat(this.data[i][4])
currentSum += closingPrice
if (currentSum > maxSum) {
maxSum = currentSum
maxStartIndex = startIndex
maxEndIndex = i
}
if (currentSum < 0) {
currentSum = 0
startIndex = i + 1
}
}
const result = {
price: parseFloat(this.data[maxEndIndex][4]),
timestamp: parseInt(this.data[maxEndIndex][0]),
direction: maxSum > 0 ? 'bullish' : 'bearish'
}
return result
}
}
/**
* Exports the MaximumSubarrayDFS class.
*/
export default MaximumSubarrayDFS