Skip to content

Commit 43973d3

Browse files
committed
NeetCode Practice 150 -> Arrays & Hashing section completed
1 parent 10a99c7 commit 43973d3

File tree

5 files changed

+128
-0
lines changed

5 files changed

+128
-0
lines changed

neetcode_practice_150/01_arrays-and-hashing/05_top-k-frequent-elements.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,9 @@
44
* @return {number[]}
55
*/
66
const topKFrequent = (nums, k) => {
7+
const repo = {};
8+
for (let n of nums) repo[n] = (repo[n] || 0) + 1;
9+
return Object.keys(repo).sort((a, b) => repo[b] - repo[a]).slice(0, k);
710
};
11+
12+
console.log(topKFrequent([1,1,1,2,2,3], 2));
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number[]}
4+
*/
5+
const productExceptSelf = (nums) => {
6+
const productOfPrev = [nums[0]];
7+
const productOfNext = [nums[nums.length - 1]];
8+
for (let i = 1; i < nums.length; i++) {
9+
productOfPrev.push(productOfPrev[i - 1] * nums[i]);
10+
productOfNext.push(productOfNext[i - 1] * nums[nums.length - 1 - i]);
11+
}
12+
for (let i = 0; i < nums.length; i++)
13+
nums[i] =
14+
(i === 0 ? 1 : productOfPrev[i - 1]) *
15+
(i === nums.length - 1 ? 1 : productOfNext[nums.length - 2 - i]);
16+
17+
return nums;
18+
};
19+
20+
console.log(productExceptSelf([1, 2, 3, 4])); // [24,12,8,6]
21+
// console.log(productExceptSelf([-1, 1, 0, -3, 3])); // [0,0,9,0,0]
22+
// console.log(productExceptSelf([1, 2])); // [2,1]
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* @param {character[][]} board
3+
* @return {boolean}
4+
*/
5+
const isValidSudoku = (board) => {
6+
let square = {};
7+
for (let i = 0; i < 9; i++) {
8+
let row = new Set();
9+
for (let j = 0; j < 9; j++) {
10+
const rowNum = board[i][j];
11+
if (rowNum !== '.') {
12+
const cellKey = `${Math.floor(i / 3)}-${Math.floor(j / 3)}`;
13+
if (row.has(rowNum) || square[cellKey]?.has(rowNum)) return false;
14+
else {
15+
row.add(rowNum);
16+
square[cellKey] = square[cellKey] || new Set();
17+
square[cellKey].add(rowNum);
18+
}
19+
}
20+
}
21+
}
22+
23+
for (let i = 0; i < 9; i++) {
24+
let col = new Set();
25+
for (let j = 0; j < 9; j++) {
26+
const colNum = board[j][i];
27+
if (colNum !== '.' && col.has(colNum)) return false;
28+
else col.add(colNum);
29+
}
30+
}
31+
32+
return true;
33+
};
34+
35+
console.log(
36+
isValidSudoku([
37+
['5', '3', '.', '.', '7', '.', '.', '.', '.'],
38+
['6', '.', '.', '1', '9', '5', '.', '.', '.'],
39+
['.', '9', '8', '.', '.', '.', '.', '6', '.'],
40+
['8', '.', '.', '.', '6', '.', '.', '.', '3'],
41+
['4', '.', '.', '8', '.', '3', '.', '.', '1'],
42+
['7', '.', '.', '.', '2', '.', '.', '.', '6'],
43+
['.', '6', '.', '.', '.', '.', '2', '8', '.'],
44+
['.', '.', '.', '4', '1', '9', '.', '.', '5'],
45+
['.', '.', '.', '.', '8', '.', '.', '7', '9'],
46+
])
47+
); // true
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class Solution {
2+
/*
3+
* @param strs: a list of strings
4+
* @return: encodes a list of strings to a single string.
5+
*/
6+
encode(strs) {
7+
let result = '';
8+
for (let str of strs) result += `<${str.length}>${str}`;
9+
return result;
10+
}
11+
12+
/*
13+
* @param str: A string
14+
* @return: dcodes a single string to a list of strings
15+
*/
16+
decode(str) {
17+
let result = [];
18+
let i = 0;
19+
while (i < str.length) {
20+
let j = str.indexOf('>', i);
21+
let len = parseInt(str.slice(i + 1, j));
22+
if (len > 0) result.push(str.slice(j + 1, j + len + 1));
23+
i = j + len + 1;
24+
}
25+
return result;
26+
}
27+
}
28+
29+
const solution = new Solution();
30+
console.log(solution.encode(['lint', 'code', 'love', 'you'])); // <4>lint<4>code<4>love<3>you
31+
console.log(solution.decode('<4>lint<4>code<4>love<3>you')); // ['lint', 'code', 'love', 'you']
32+
console.log(solution.encode([''])); // <0>
33+
console.log(solution.decode('<0>')); // ['']
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
const longestConsecutive = (nums) => {
6+
const bucket = new Set(nums);
7+
let max = 0;
8+
for (let n of nums) {
9+
if (!bucket.has(n - 1)) {
10+
let curr = n,
11+
currLen = 1;
12+
while (bucket.has(++curr)) currLen++;
13+
max = Math.max(max, currLen);
14+
}
15+
}
16+
return max;
17+
};
18+
19+
console.log(longestConsecutive([100, 4, 200, 1, 3, 2])); // 4
20+
console.log(longestConsecutive([0, 3, 7, 2, 5, 8, 4, 6, 0, 1])); // 9
21+
console.log(longestConsecutive([1, 2, 0, 1])); // 3

0 commit comments

Comments
 (0)