-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
160 lines (134 loc) · 4.09 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
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
let result;
let highlight;
window.onload = () => {
result = document.getElementById("result");
highlight = document.getElementById("highlight");
}
function update(textArea) {
let [cleanText, numbers] = extractData(textArea.value);
updateText(textArea, cleanText);
let [index1, index2, sum] = calculateMaxSum(numbers);
result.innerText = sum;
if (sum === BigInt(0)) {
clearHighlight();
return;
}
updateHighlight(highlight, cleanText, index1, index2);
}
function extractData(rawText) {
let cleanText = "";
let numbers = [];
let numberString = "";
for (let char of rawText) {
switch (char) {
case " ":
if (numberString !== "-" && numberString !== "") {
numbers.push(BigInt(numberString));
cleanText += `${numberString} `;
numberString = "";
} else {
cleanText += `${numberString} `;
numberString = "";
}
break
case "-":
if (numberString === "") {
numberString = "-";
} else if (numberString === "-") {
cleanText += "- ";
} else {
numbers.push(BigInt(numberString));
cleanText += `${numberString} `;
numberString = "-";
}
break
case "0":
case "1":
case "2":
case "3":
case "4":
case "5":
case "6":
case "7":
case "8":
case "9":
numberString += char;
}
}
if (numberString !== "" && numberString !== "-") {
numbers.push(BigInt(numberString));
cleanText += numberString;
} else {
cleanText += numberString;
}
return [cleanText, numbers];
}
function updateText(textArea, cleanText) {
let selectionStart = textArea.selectionStart;
textArea.value = cleanText;
textArea.selectionStart = selectionStart;
textArea.selectionEnd = selectionStart;
}
function calculateMaxSum(numbers) {
console.log(numbers)
let maxSum = BigInt(0);
let chainSum = BigInt(0);
let index1 = 0;
let index2 = 0;
let bestIndex1 = 0;
let bestIndex2 = 0;
for (let i = 0; i < numbers.length; i++) {
let number = numbers[i];
let nextVale = chainSum + number;
if (nextVale <= 0) {
// start new chain
chainSum = BigInt(0);
index1 = i + 1;
} else {
// extend chain
index2 = i;
chainSum = nextVale;
}
// compare among best current chain
if (chainSum > maxSum) {
bestIndex1 = index1;
bestIndex2 = index2;
maxSum = chainSum;
}
}
return [bestIndex1, bestIndex2, maxSum];
}
function clearHighlight() {
highlight.replaceChildren();
}
function updateHighlight(highlight, cleanText, index1, index2) {
let preContent = document.createElement("span");
let highlightContent = document.createElement("span");
highlightContent.classList.add("highlight-content");
let index = 0;
let outsideNumber = true;
let modifyingElement = preContent;
for (let char of cleanText) {
if (char === " ") {
if (!outsideNumber) {
index += 1;
if (index > index2) {
// post content not needed
break;
}
}
outsideNumber = true;
} else if (char !== "-") {
// since the start of a chain is never minus we don't have to handle minus explicitly
if (outsideNumber && index == index1) {
modifyingElement = highlightContent;
}
outsideNumber = false;
}
modifyingElement.innerText += char;
}
highlight.replaceChildren(preContent, highlightContent);
}
function scrollHighlight(textArea) {
highlight.scrollTop = textArea.scrollTop;
}