JSFiddle - React, Tailwind, and code Playground
JavaScript
function throwError(message) {
if (Error)
throw new Error(message);
else
throw message;
} // end throwError()
function assert(condition,message) {
if (!condition) {
message = message || 'assert() failed: '+condition+'.';
throwError(message);
} // end if
} // end assert()
function solve(arrayList) {
// validate
// 1: validate that arrayList is an array
// 2: validate that each element of arrayList is an array
// 3: validate that each element of each array is an array
// 4: validate that each array that is an element of each array of arrayList has exactly two elements, first and second
// 5: validate that each innermost element is a Number
// 6: validate that all seconds are within 1..N
// 7: validate that we don't have duplicate pairs in the same array
// 1
if (typeof(arrayList) !== 'object' || arrayList.constructor !== Array) throwError('arrayList is non-Array.');
for (var ai = 0; ai < arrayList.length; ++ai) {
var array = arrayList[ai];
// 2
if (typeof(array) !== 'object' || array.constructor !== Array) throwError('array '+(ai+1)+' is non-Array.');
var firstMap = {};
for (var pi = 0; pi < array.length; ++pi) {
var pair = array[pi];
// 3
if (typeof(pair) !== 'object' || pair.constructor !== Array) throwError('pair '+(pi+1)+' of array '+(ai+1)+' is non-Array.');
// 4
if (pair.length !== 2) throwError('pair '+(pi+1)+' of array '+(ai+1)+' is not a pair.');
// 5
if (typeof(pair[0]) !== 'number') throwError('pair '+(pi+1)+' of array '+(ai+1)+' has non-Number first.');
if (typeof(pair[1]) !== 'number') throwError('pair '+(pi+1)+' of array '+(ai+1)+' has non-Number second.');
// 6
if (pair[1] < 1) throwError('invalid second number '+pair[1]+': less than 1.');
if (pair[1] > arrayList.length) throwError('invalid second number '+pair[1]+': greater than N='+arrayList.length+'.');
// 7
if (!firstMap[pair[0]]) { firstMap[pair[0]] = []; for (var x = arrayList.length-1; x >= 0; --x) firstMap[pair[0]][x]...