expect(result).toEqual([-1, 3, -1, 2, 4]);
});
it('shift previous postions from the left to a smaller value or store -1', () => {
// arrange
const nums = [3, 4, 5, 2, 1, 4, 5];
// act
const result = arrayPreviousLess(nums);
// log
console.log("result: ", result);
// assert
expect(result).toEqual([-1, 3, 4, -1, -1, 1, 4]);
});
it('shift previous postions from the left to a smaller value or store -1', () => {
// arrange
const nums = [5, 4, 3, 2, 1];
// act
const result = arrayPreviousLess(nums);
// log
console.log("result: ", result);
// assert
expect(result).toEqual([-1, -1, -1, -1, -1]);
});
});
function arrayPreviousLess(nums) {
// write code here.
let stack = []
let result = []
nums.map((num, i) => {
while(stack.length > 0 && stack[stack.length-1] > num)
stack.pop()
if(stack.length == 0){
result.push(-1)
stack.push(num)
}
else{
result.push(stack[stack.length-1])
stack.push(num)
}
})
return result
}
/**
* Test Suite
*/
describe('arrayPreviousLess()', () => {
it('shift previous postions from the left to a smaller value or store -1', () => {
// arrange
const nums = [3, 5, 2, 4, 5];
// act
const result = arrayPreviousLess(nums);
// log
console.log("result: ", result);
// assert