AlgoDaily Solution

1var assert = require('assert');
2
3function nextLargerNumber(nums) {
4 const result = [];
5 const stack = [];
6 for (let j = 0; j < nums.length; j++) {
7 result.push(-1);
8 }
9 for (let i = 0; i < nums.length * 2; i++) {
10 let num = nums[i % nums.length];
11 while (stack.length && nums[stack[stack.length - 1]] < num) {
12 result[stack.pop()] = num;
13 }
14 if (i < nums.length) {
15 stack.push(i);
16 }
17 }
18 return result;
19}
20
21try {
22 assert.deepEqual(nextLargerNumber([3, 1, 3, 4]), [4, 3, 4, -1]);
23
24 console.log(
25 'PASSED: assert.deepEqual(nextLargerNumber([3, 1, 3, 4]), [4, 3, 4, -1])'
26 );
27} catch (err) {
28 console.log(err);
29}
Community Solutions
Community solutions are only available for premium users.
Access all course materials today
The rest of this tutorial's contents are only available for premium members. Please explore your options at the link below.
xxxxxxxxxx
17
var assert = require('assert');
function nextLargerNumber(nums) {
// implement this function
return nums;
}
try {
assert.deepEqual(nextLargerNumber([3, 1, 3, 4]), [4, 3, 4, -1]);
console.log(
'PASSED: assert.deepEqual(nextLargerNumber([3, 1, 3, 4]), [4, 3, 4, -1])'
);
} catch (err) {
console.log(err);
}
OUTPUT
Results will appear here.