Maximizing Distance from Origin in a 1D Plane (Learn Something New in This Problem)
IntuitionThe problem involves determining the furthest possible distance from the origin after processing a series of moves on a 1D plane. Each move can be:'L'
# Intuition\nThe problem involves determining the furthest possible distance from the origin after processing a series of moves on a 1D plane. Each move can be:\n\n\'L\' (move left),\n\'R\' (move right), or\n\'_\' (an empty move that can be used to either increase the distance leftward or rightward).\nTo maximize the distance:\n\nWe need to account for the net movement caused by \'L\' and \'R\'.\nUse the \'_\' moves to further increase the distance in the direction that maximizes the overall distance.\n# Approach\nTrack the net movement:\nInitialize a variable diff to track the cumulative difference caused by \'L\' and \'R\'.\n\n\'L\' decreases diff by 1.\n\'R\' increases diff by 1.\nCount the spaces:\nUse another variable space to count the number of \'_\'. These moves can be strategically used to increase the absolute distance.\n\nMaximize the distance:\nThe furthest possible distance is the sum of:\n\nThe absolute value of the net difference (Math.abs(diff)), and\nThe total number of empty moves (space), since each \'_\' can be added to maximize the distance.\nReturn the result.\n# Complexity\n- Time complexity:\nO(n): We iterate through the string moves once, where n is the length of the string.\n- Space complexity:\nO(1): We use only two variables (diff and space) to store the counts. The space used is constant, regardless of the input size.\n# Code\n```javascript []\n/**\n * @param {string} moves\n * @return {number}\n */\nvar furthestDistanceFromOrigin = function(moves) {\n    let diff = 0;\n    let space = 0;\n    let movesArr = Array.from(moves)    //Covert string to an array\n    movesArr.map((i)=>{\n        if(i == \'_\') space++;\n        else if (i == \'L\') diff--;\n        else if (i == \'R\') diff++;\n    })\n    let absDiff = Math.abs(diff)\n    return absDiff + space;\n};\n```
['JavaScript']