-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharrayManipulation3.js
158 lines (131 loc) · 3.49 KB
/
arrayManipulation3.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
'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)
let vals = new Array(n).fill(0)
for (let x = 0; x < queries.length; x++) {
// console.log(queries[x][0], queries[x][2])
vals[queries[x][0] - 1] += queries[x][2]
if (typeof vals[queries[x][1]] === 'number') {
vals[queries[x][1]] -= queries[x][2]
}
else {
break
}
//console.log(vals[queries[x][1]])
}
// console.log(vals)
let prev = 0
for (let x = 0; x < vals.length; x++)
{
prev += vals[x]
// console.log(prev)
vals[x] = prev
}
let max = 0
for (let x = 0; x < vals.length; x++) {
if (vals[x] > max) {
max = vals[x]
}
}
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}
//