Skip to main content

17 posts tagged with "array"

View All Tags

· One min read

https://leetcode.com/problems/shortest-distance-to-a-character/description/

Solution 1.

/**
* @param {string} s
* @param {character} c
* @return {number[]}
*/
var shortestToChar = function (s, c) {
const targets = [];
for (let i = 0; i < s.length; i++) {
if (s[i] === c) {
targets.push(i);
}
}

let targetIndex = 0;
const result = [];
for (let j = 0; j < s.length; j++) {
const nextIndex = targetIndex + 1;
if (!isNaN(targets[nextIndex])) {
result.push(
Math.min(
Math.abs(j - targets[targetIndex]),
Math.abs(j - targets[nextIndex]),
),
);

if (j - targets[nextIndex] === 0) {
targetIndex++;
}
} else {
if (s[j] === c) {
result.push(0);
} else {
result.push(Math.abs(j - targets[targetIndex]));
}
}
}

return result;
};

Runtime 131 ms Beats 21.28%

Memory 44.5 MB Beats 54.4%

· One min read

https://leetcode.com/problems/unique-morse-code-words/description/

Solution 1.

/**
* @param {string[]} words
* @return {number}
*/
var uniqueMorseRepresentations = function (words) {
if (words.length === 1) {
return 1;
}

const wordMorse = [];

let currentMorse = '';
for (const word of words) {
currentMorse = '';
for (let i = 0; i < word.length; i++) {
currentMorse += morse[word[i].charCodeAt(0) - 97];
}
wordMorse.push(currentMorse);
}

return new Set(wordMorse).size;
};

const morse = [
'.-',
'-...',
'-.-.',
'-..',
'.',
'..-.',
'--.',
'....',
'..',
'.---',
'-.-',
'.-..',
'--',
'-.',
'---',
'.--.',
'--.-',
'.-.',
'...',
'-',
'..-',
'...-',
'.--',
'-..-',
'-.--',
'--..',
];

Runtime 103 ms Beats 50.94%

Memory 42.9 MB Beats 82.45%

· One min read

https://leetcode.com/problems/move-zeroes/description/

Solution 1

/**
* @param {number[]} nums
* @return {void} Do not return anything, modify nums in-place instead.
*/
var moveZeroes = function (nums) {
let zeroCount = 0;

for (let i = 0; i < nums.length; i++) {
if (nums[i] !== 0) {
const zero = nums[zeroCount];
nums[zeroCount] = nums[i];
nums[i] = zero;
zeroCount++;
}
}

return nums;
};

Runtime 147 ms Beats 64.1%

Memory 47 MB Beats 27.90%

· One min read

https://leetcode.com/problems/minimum-index-sum-of-two-lists/

/**
* @param {string[]} list1
* @param {string[]} list2
* @return {string[]}
*/
var findRestaurant = function (list1, list2) {
const hashTable = {};

list1.forEach((el, index) => {
const indexOfEl2 = list2.findIndex((el2) => el2 === el);
if (indexOfEl2 !== -1) {
hashTable[el] = indexOfEl2 + index;
}
});

const size = Object.keys(hashTable).length;
if (size === 0) {
return [];
}

const minIndexSum = Math.min(
...Object.values(hashTable).map((value) => value),
);

return Object.keys(hashTable).filter((key) => hashTable[key] === minIndexSum);
};

Runtime: 309 ms, faster than 13.28% of JavaScript online submissions for Minimum Index Sum of Two Lists.

Memory Usage: 49.2 MB, less than 54.39% of JavaScript online submissions for Minimum Index Sum of Two Lists.

· One min read
/**
* @param {string[]} words
* @return {string[]}
*/
var findWords = function (words) {
const firstRow = 'qwertyuiop';
const secondRow = 'asdfghjkl';
const thirdRow = 'zxcvbnm';

const result = [];

for (const word of words) {
const lowerCaseWord = word.toLocaleLowerCase();
const a = lowerCaseWord.split('').every((word) => firstRow.includes(word));
const b = lowerCaseWord.split('').every((word) => secondRow.includes(word));
const c = lowerCaseWord.split('').every((word) => thirdRow.includes(word));

if (a || b || c) {
result.push(word);
}
}

return result;
};

Runtime: 58 ms, faster than 98.18% of JavaScript online submissions for Keyboard Row.

Memory Usage: 42.1 MB, less than 29.55% of JavaScript online submissions for Keyboard Row.

· One min read

https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/

Solution 1

/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {number[]} nums
* @return {TreeNode}
*/
var sortedArrayToBST = function (nums) {
if (nums.length === 0) {
return null;
}
return createBST(nums, 0, nums.length - 1);
};

const createBST = (nums, low, high) => {
if (low > high) {
return null;
}

let mid = Math.floor((high + low) / 2);

let node = new TreeNode(nums[mid]);
node.left = createBST(nums, low, mid - 1);
node.right = createBST(nums, mid + 1, high);
return node;
};

· One min read

https://leetcode.com/problems/pascals-triangle/

Solution 1

/**
* @param {number} numRows
* @return {number[][]}
*/
var generate = function (numRows) {
if (numRows === 1) {
return [[1]];
}

if (numRows === 2) {
return [[1], [1, 1]];
}

let rows = [[1], [1, 1]];

for (let i = 2; i < numRows; i++) {
let prevRow = rows[i - 1];

let currentRow = [1];
for (let j = 0; j < prevRow.length - 1; j++) {
currentRow.push(prevRow[j] + prevRow[j + 1]);
}
currentRow.push(1);

rows.push(currentRow);
}
return rows;
};

· One min read

https://leetcode.com/problems/pascals-triangle-ii/

Solution 1

/**
* @param {number} rowIndex
* @return {number[]}
*/
var getRow = function (rowIndex) {
if (rowIndex === 0) {
return [1];
}

if (rowIndex === 1) {
return [1, 1];
}

let rows = [[1], [1, 1]];

for (let i = 2; i <= rowIndex; i++) {
let prevRow = rows[i - 1];

let currentRow = [1];

for (let j = 0; j < prevRow.length - 1; j++) {
currentRow.push(prevRow[j] + prevRow[j + 1]);
}
currentRow.push(1);

rows.push(currentRow);
}

return rows[rowIndex];
};

· One min read

https://leetcode.com/problems/single-number/

Solution 1

/**
* @param {number[]} nums
* @return {number}
*/
var singleNumber = function (nums) {
let hashMap = {};

for (let i = 0; i < nums.length; i++) {
const currentNum = nums[i];
if (hashMap[currentNum]) {
hashMap[currentNum]++;
} else {
hashMap[currentNum] = 1;
}
}

return Number(getKeybyValue(hashMap, 1));
};

const getKeybyValue = (object, value) => {
return Object.keys(object).find((key) => object[key] === value);
};