Straight forward, but we have to consider negative ranges.
/**
* @param {number} x
* @param {number} n
* @return {number}
*/
var myPow = function(x, n) {
if(n === 0) {
return 1;
}
var N = n;
var X = x;
var result = 1;
if(n < 0) {
X = 1 / x;
N = -n;
}
for(var i = 0; i < N; i ++) {
result = result * X;
}
return result;
};
Approach 2: Fast Power Algorithm Recursive
divide n so you immediately cut the computation time in half to logarithmic one.
/**
* @param {number} x
* @param {number} n
* @return {number}
*/
var myPow = function(x, n) {
function fastPow(x, n) {
if(n < 1) {
return 1;
}
var half = fastPow(x, Math.floor(n / 2));
if(n % 2 == 0) {
return half * half;
}
else {
return half * half * x;
}
}
var X = x;
var N = n;
if(n < 0) {
X = 1 / x;
N = -n;
}
return fastPow(X, N);
};
According MDN all three methods are very similar, and at the end they produce very similar result.
they all accept this object as first parameter.
then apply accepts and array of arguments vs bind and call accept a sequence of arguments.
Function.prototype.apply()
The apply() method calls a function with a given this value, and arguments provided as an array
Function.prototype.bind()
The bind() method creates a new function that, when called, has its this keyword set to the provided value, with a given sequence of arguments preceding any provided when the new function is called
We could assume that the whole word could be the common one so we set prefix = ‘flower’
Then we would compare with the rest words and keep removing last character until prefix becomes empty (meaning no common substring was found) or until we have the common substring.
prefix
flower
flow
flight
flower
flower
flower
flower
flowe
flowe
flowe
flowe
flow
flow
flow
flow
flo
flo
flo
flo
fl
fl
fl
fl
/**
* @param {string[]} strs
* @return {string}
*/
var longestCommonPrefix = function(strs) {
var prefix = strs[0];
for(var i = 1; i < strs.length; i ++ ) {
while(strs[i].indexOf(prefix) != 0) {
prefix = prefix.substring(0, prefix.length - 1);
if(prefix == "")
return '';
}
}
return prefix;
};
what wejust did: – set up prefix to be the whole 1st word strs[0]
– compare prefix with the second word (strs[1]) and if there is no match, remove the last symbol and keep going until it finds match.
Complexity Analysis
Time complexity : O(S)O(S) , where S is the sum of all characters in all strings.In the worst case all nn strings are the same. The algorithm compares the string S1S1 with the other strings [S_2 \ldots S_n][S2…Sn] There are SS character comparisons, where SS is the sum of all characters in the input array.
Space complexity : O(1)O(1). We only used constant extra space.
Solution 2: Vertical scanning
Similar but optimized for cases like the one above where we have very short common substring, and we don’t want to scan the whole word.
prefix
flower
flow
flight
f
f
f
f
fl
fl
fl
fl
flo
flo
flo
flo
/**
* @param {string[]} strs
* @return {string}
*/
var longestCommonPrefix = function(strs) {
var prefix;
for(var i = 0; i < strs[0].length; i ++ ) {
var c = strs[0][i];
for(var j = 0; j < strs.length; j++) {
if(strs[j][i] != c) {
return strs[0].substring(0, i);
}
}
}
return strs[0];
};
what wejust did: – Iterate through the words like they are in column.
– compare each character (starting with the first one) between all words. When we find a mismatch, remove the last (mismatched) character and return truncates strs[0]
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.
Symbol Value
I 1
V 5
X 10
L 50
C 100
D 500
M 1000
For example, 2 is written as II in Roman numeral, just two one’s added together. 12 is written as XII, which is simply X + II. The number 27 is written as XXVII, which is XX + V + II.
Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used:
I can be placed before V (5) and X (10) to make 4 and 9.
X can be placed before L (50) and C (100) to make 40 and 90.
C can be placed before D (500) and M (1000) to make 400 and 900.
Given a roman numeral, convert it to an integer.
Example 1:
Input:
s = "III"
Output:
3
Example 2:
Input:
s = "IV"
Output:
4
Example 3:
Input:
s = "IX"
Output:
9
Example 4:
Input:
s = "LVIII"
Output:
58
Explanation:
L = 50, V= 5, III = 3.
Example 5:
Input:
s = "MCMXCIV"
Output:
1994
Explanation:
M = 1000, CM = 900, XC = 90 and IV = 4.
Constraints:
1 <= s.length <= 15
s contains only the characters ('I', 'V', 'X', 'L', 'C', 'D', 'M').
It is guaranteed that s is a valid roman numeral in the range [1, 3999].
/**
* @param {string} s
* @return {number}
*/
var romanToInt = function(s) {
var len = s.length;
var i = 0;
var map = {
'I': 1,
'V': 5,
'X': 10,
'L': 50,
'C': 100,
'D': 500,
'M': 1000
}
var sum = 0;
while(i < len) {
var currentVal = map[ s[i] ];
var nextVal = map[ s[i + 1] ];
if( currentVal < nextVal) {
sum += nextVal - currentVal;
i ++;
}
else {
sum += currentVal;
}
i ++;
}
return sum;
};
Solution 2: Left to right (or right to left) pass improved
/**
* @param {string} s
* @return {number}
*/
var romanToInt = function(s) {
var len = s.length;
var i = 0;
var map = {
'I': 1,
'IV': 4,
'V': 5,
'IX': 9,
'X': 10,
'XL': 40,
'L': 50,
'XC': 90,
'C': 100,
'CD': 400,
'D': 500,
'CM': 900,
'M': 1000
}
var sum = 0;
while(i < len) {
var currentVal = map[ s[i] ];
var nextVal = map[ s[i + 1] ];
if( currentVal < nextVal) {
var sumbol = s[i] + s[i+1];
sum += map[sumbol];
i ++;
}
else {
sum += currentVal;
}
i ++;
}
return sum;
};
Solution3: Right to left pass
In the “subtraction” cases, such as XC, we’ve been updating our running sum as follows:
sum += value(C) - value(X)
However, notice that this is mathematically equivalent to the following:
sum += value(C)
sum -= value(X)
Utilizing this means that we can process one symbol each time we go around the main loop. We still need to determine whether or not our current symbol should be added or subtracted by looking at the neighbour though.
This way we could start from the most right symbol an initialize the sym with it, since every most right symbol will always be added to the sum.
/**
* @param {string} s
* @return {number}
*/
var romanToInt = function(s) {
var len = s.length;
var i = len - 1;
var map = {
'I': 1,
'V': 5,
'X': 10,
'L': 50,
'C': 100,
'D': 500,
'M': 1000
}
var sum = map[ s[i] ];
i --;
while(i > -1) {
var currentVal = map[ s[i] ];
var prevVal = map[ s[i + 1] ];
if( currentVal < prevVal) {
sum -= currentVal;
}
else {
sum += currentVal;
}
i --;
}
return sum;
};
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of the line i is at (i, ai) and (i, 0). Find two lines, which, together with the x-axis forms a container, such that the container contains the most water.
Notice that you may not slant the container.
Example 1:
Input:
height = [1,8,6,2,5,4,8,3,7]
Output:
49
Explanation:
The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In this case, the max area of water (blue section) the container can contain is 49.
A better than brute force solution is to use a variation of “sliding doors” algorithm.
Let’s consider this case: [1,3,4,3]. The area with most water will be the one with highest height and length.
To find it we set up two pointers: one at position 0, and one at the end of the array. The amount of water that could be collected here is min(leftPointerValue, rightPointerValue) * length,
where length is rightPointer – leftPointer. Which is 4.
Now it’s clear that if rightPointerValue > leftPointerValue there is no point of keep moving rightPointer because we won’t get any bigger amount of water since it will always be limited by the leftPointerValue (height) and the length will always be smaller than the previous length.
So in this case we will move the leftPointer forward to evaluate the next case.
Here the amount of the water collected is min(leftPointerValue, rightPointerValue) * length which is min(3, 3) * 3 = 9.
Nex we continue evaluating all cases till leftPointer = rightPointer (length = 0), and we didn’t find bigger amount of water collected, so the answer we found on the second evaluation is the right answer: 9.
/**
* @param {number[]} height
* @return {number}
*/
var maxArea = function(height) {
var maxArea = 0;
var pLeft = 0;
var pRight = height.length - 1;
var len = pRight - pLeft;
while(len > 0) {
var pLeftVal = height[pLeft];
var pRightVal = height[pRight];
if(pLeftVal > pRightVal) {
maxArea = Math.max( len * pRightVal, maxArea );
pRight --;
}
else {
maxArea = Math.max( len * pLeftVal, maxArea );
pLeft ++;
}
len --;
}
return maxArea;
};
var height = [1,8,6,2,5,4,8,3,7];
console.log( maxArea(height) );
A robot is located at the top-left corner of a m x n grid (marked ‘Start’ in the diagram below).
The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked ‘Finish’ in the diagram below).
How many possible unique paths are there?
Note:m and n will be at most 100.
Example 1:
Input:
m = 3, n = 2
Output:
3
Explanation:
From the top-left corner, there are a total of 3 ways to reach the bottom-right corner:
1. Right -> Right -> Down
2. Right -> Down -> Right
3. Down -> Right -> Right
Since we can move only right or down on every cell in the first row we will have only one place from where we can come and this is the cell before. And same for the first vertical row.
Then after we figured out that there is only one way to reach each cell in the first row and the first column (which is from the cell before) we could start calculating possible ways to go to the next cells.
Let’s look at the cell in the second row and second column.There are actually two possible ways to go there: from the cell above, and the cell before, so 2 possible ways. (figure below).
The cell in the third column on the second row: same 1 way from the cell above, and from the cell before. But since there are already 2 ways to reach the cell before the total ways to reach this cell is: 1 + 2 = 3.
The solution:
/**
* @param {number} m
* @param {number} n
* @return {number}
*/
var uniquePaths = function(m, n) {
var memo = [];
for(var i=0;i < n; i ++) {
for(var j = 0; j < m; j ++) {
var index = (i * m) + j;
if(i == 0) {
memo[index] = 1;
}
else if(j == 0) {
memo[index] = 1;
}
else {
var up = index - m;
var left = index - 1;
memo[index] = memo[up] + memo[left];
}
}
}
return memo[memo.length - 1];
}
console.log(uniquePaths(7,3));
Unique paths with obstacles.
/**
* @param {number[][]} obstacleGrid
* @return {number}
*/
var uniquePathsWithObstacles = function(obstacleGrid) {
var m = obstacleGrid[0].length;
var n = obstacleGrid.length;
var row = 0;
if(obstacleGrid[0][0] == 1)
return 0;
var memo = [];
for(var i=0;i < n; i ++) {
for(var j = 0; j < m; j ++) {
var index = (i * m) + j;
if(i == 0) {
if(obstacleGrid[i][j] == 1 || (j > 0 && memo[index -1] == 0))
memo[index] = 0;
else
memo[index] = 1;
}
else if(j == 0) {
if(obstacleGrid[i][j] == 1 || (i > 0 && memo[index - m] == 0))
memo[index] = 0;
else
memo[index] = 1;
}
else {
var up = index - m;
var left = index - 1;
if(obstacleGrid[i][j] == 1)
memo[index] = 0;
else
memo[index] = memo[up] + memo[left];
}
row += memo[index] ? 0 : 1;
}
if(row == m)
return 0;
row = 0;
}
return memo[memo.length - 1];
};
var grid = [
[0,0,0],
[0,1,0],
[0,0,0]
];
console.log(uniquePathsWithObstacles(grid));
what we just did: – we calculated the length of the first list to be 5 and the second 6 (first and the second loop) – the third loop is doing two things: – first since the difference between the shorter and the longer list is 1 we move the cursor to the second element of the longer list (lines 59-61) – after we position the longer list cursor at the second element we could start comparing (line 64)
If we execute the function we will see this result:
long list, short list 0 4
long list, short list 1 1
long list, short list 8 8
And the third element is exactly where the intersection is.
Approach 3: Traverse both lists and when reaching the end of each one, move the pointer to the opposite list and traverse again till intersection is found.
Time complexity : O(m+n)O(m+n).
Space complexity : O(m)O(m) or O(n)O(n).
This basically is the same concept as in the example above, just written in a bit more elegant way.
function ListNode(val) {
this.val = val;
this.next = null;
}
headA = new ListNode(4);
headA.next = new ListNode(1);
headB = new ListNode(5);
headB.next = new ListNode(0);
headB.next.next = new ListNode(1);
headA.next.next = headB.next.next.next = new ListNode(8);
headB.next.next.next.next = headA.next.next.next = new ListNode(4);
headB.next.next.next.next.next = headA.next.next.next.next = new ListNode(5);
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} headA
* @param {ListNode} headB
* @return {ListNode}
*/
var getIntersectionNode = function(headA, headB) {
let nodeA = headA;
let nodeB = headB;
let swapA = false;
let swapB = false;
var i = 0;
while(nodeA != null && nodeB!=null ) {
// node A
if(!swapA && nodeA.next == null) {
nodeA = headB;
swapA = true;
}
else {
nodeA = nodeA.next;
}
// node B
if(!swapB && nodeB.next == null) {
nodeB = headA;
swapB = true;
}
else {
nodeB = nodeB.next;
}
if(nodeA === nodeB)
return nodeA.val;
}
};
console.log (getIntersectionNode(headA, headB) );
what we just did:
– traverse listA and listB till we reach the end of each one (lines 47 and 55) .
– once we reach the end of each list we point the cursor to the opposite list (lines 43 and 51)
Approach 4: Hash Table
Traverse list A and store the address / reference to each node in a hash set. Then check every node bi in list B: if bi appears in the hash set, then bi is the intersection node.
Complexity Analysis
Time complexity : O(m+n)O(m+n).
Space complexity : O(m)O(m) or O(n)O(n).
function ListNode(val) {
this.val = val;
this.next = null;
}
// link-list A: [4,1,8,4,5]
// link-list B: [5,0,1,8,4,5]
headA = new ListNode(4);
headA.next = new ListNode(1);
headB = new ListNode(5);
headB.next = new ListNode(0);
headB.next.next = new ListNode(1);
headA.next.next = headB.next.next.next = new ListNode(8);
headB.next.next.next.next = headA.next.next.next = new ListNode(4);
headB.next.next.next.next.next = headA.next.next.next.next = new ListNode(5);
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} headA
* @param {ListNode} headB
* @return {ListNode}
*/
var getIntersectionNode = function(headA, headB) {
let hashMap = {};
let node = headA;
while(node != null ) {
hashMap[node.val] = node;
node = node.next;
}
node = headB;
while(node != null) {
let val = node.val;
if(hashMap[val] == node) {
return val;
}
node = node.next;
}
};
console.log ("result: ", getIntersectionNode(headA, headB) );