-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharrayManipulation2.js
175 lines (145 loc) · 3.86 KB
/
arrayManipulation2.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
'use strict'
const assert = require('assert');
// https://www.hackerrank.com/challenges/crush/problem
const fs = require('fs')
process.stdin.resume()
process.stdin.setEncoding('utf-8')
let inputString = ''
let currentLine = 0
/*
* Complete the contacts function below.
*/
function arrayManipulation(n, queries) {
// console.log(n, queries)
const arr = new Array(n).fill(0)
function* generator_queries(queries) {
let index = 0
while (index < queries.length) {
yield queries[index]
index += 1
}
}
function* execute(query) {
console.log(query); //0123456789
// const executed = []
let maximunValue = 0
for (let x = (query[0] - 1); x < query[1]; x++) {
const val = arr[x] + query[2]
arr[x] = val
// executed.push(val)
if (val > maximunValue) {
maximunValue = val
}
}
yield {
// executed,
maximunValue
}
}
let done = 0
let max = 0
for (let query of generator_queries(queries)) {
if (done === 0) {
max = 0
}
const executed = execute(query).next()
done += 1
// console.log('executed', executed.value.executed)
// console.log('executed', executed.value.maximunValue)
if (executed.value.maximunValue > max) {
max = executed.value.maximunValue
}
}
// console.log(arr)
return max
}
function readLine() {
return inputString[currentLine++]
}
function main() {
const nm = readLine().split(' ');
const n = parseInt(nm[0], 10);
const m = parseInt(nm[1], 10);
let queries = Array(m);
for (let i = 0; i < m; i++) {
queries[i] = readLine().split(' ').map(queriesTemp => parseInt(queriesTemp, 10));
}
let result = arrayManipulation(n, queries);
console.log(result)
// assert.equal(result, 2497169732) // 200 2497169732
process.exit(0)
}
let reader = fs.createReadStream('inputArrayManipulation2.txt')
reader.on('data', function (chunk) {
inputString += chunk.toString()
})
reader.on("end", () => {
inputString = inputString.replace(/\s*$/, '')
.split('\n')
.map(str => str.replace(/\s*$/, ''));
main()
})
// totalHourglasses ========================= 1
// row 1, column 1, column 2, column 3
// row 2, column 2
// row 3, column 1, column 2, column 3
//
// startingColumn = startingColumn = 1
//
// totalHourglasses ========================= 2
// row 1, column 2, column 3, column 4
// row 2, column 3
// row 3, column 2, column 3, column 4
//
// startingColumn = startingColumn = 1
//
// totalHourglasses ========================= 3
// row 1, column 3, column 4, column 5
// row 2, column 4
// row 3, column 3, column 4, column 5
//
// startingColumn = startingColumn = 1
//
// totalHourglasses ========================= 4
// row 1, column 4, column 5, column 6
// row 2, column 5
// row 3, column 4, column 5, column 6
//
// startingColumn = startingColumn = 1
//
// if (startingColumn === totalStackColumns) { startingColumn = 1}
// rowTop = rowTop + 1
// rowMid = rowMid + 1
// rowBottom = rowBottom + 1
// currentlyRow = currentlyRow + 1
// if currentlyRow === totalStacks then it is done
// totalHourglasses ========================= 5
// row 2, column 1, column 2, column 3
// row 3, column 2
// row 4, column 1, column 2, column 3
//
// startingColumn = startingColumn = 1
//
// totalHourglasses ========================= 6
// row 2, column 2, column 3, column 4
// row 3, column 3
// row 4, column 2, column 3, column 4
//
// startingColumn = startingColumn = 1
//
// totalHourglasses ========================= 7
// row 2, column 3, column 4, column 5
// row 3, column 4
// row 4, column 3, column 4, column 5
//
// startingColumn = startingColumn = 1
//
// totalHourglasses ========================= 8
// row 2, column 4, column 5, column 6
// row 3, column 5
// row 4, column 4, column 5, column 6
//
// startingColumn = startingColumn = 1
//
// if (startingColumn === totalStackColumns) { startingColumn = 1}
//