Skip to content

Commit 07a1ce0

Browse files
committed
save changes
1 parent c43a7bb commit 07a1ce0

File tree

5 files changed

+192
-0
lines changed

5 files changed

+192
-0
lines changed

random/3sum.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number[][]}
4+
*/
5+
const threeSum = (nums) => {
6+
nums.sort((a, b) => a - b);
7+
const triplets = [];
8+
for (let i = 0; i < nums.length - 2; i++) {
9+
if (i > 0 && nums[i - 1] === nums[i]) continue;
10+
let l = i + 1,
11+
r = nums.length - 1;
12+
while (l < r) {
13+
const sum = nums[i] + nums[l] + nums[r];
14+
if (sum === 0) {
15+
triplets.push([nums[i], nums[l], nums[r]]);
16+
while (l < r && nums[l] === nums[l + 1]) l++;
17+
while (l < r && nums[r] === nums[r + 1]) r--;
18+
l++, r--;
19+
} else if (sum < 0) l++;
20+
else r--;
21+
}
22+
}
23+
24+
return triplets;
25+
};
26+
27+
// TEST
28+
console.log(threeSum([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])); // []
29+
console.log(threeSum([0, 0, 0, 0])); // [[0, 0, 0]]
30+
console.log(threeSum([-1, 0, 1, 2, -1, -4])); // [[0, 1, 2], [0, 3, 4]]
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
const longestConsecutive = (nums) => {
6+
const map = {};
7+
let max = 0;
8+
for (let num of nums) {
9+
if (num in map) continue;
10+
const left = map[num - 1] || 0;
11+
const right = map[num + 1] || 0;
12+
const sum = left + right + 1;
13+
map[num] = sum;
14+
map[num - left] = sum;
15+
map[num + right] = sum;
16+
max = Math.max(max, sum);
17+
}
18+
console.log(map);
19+
return max;
20+
};
21+
22+
// TEST
23+
console.log(longestConsecutive([100, 4, 200, 1, 3, 2])); // 4
24+
console.log(longestConsecutive([0, 3, 7, 2, 5, 8, 4, 6, 0, 1])); // 9
25+
console.log(longestConsecutive([1, 2, 0, 1])); // 3
26+
console.log(longestConsecutive([1, 2, 0, 1, 3, 4, 5, 6, 7, 8, 9, 10])); // 10

random/string-encode-and-decode.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
class Solution {
2+
#hStart = '[[';
3+
#hEnd = ']]';
4+
/**
5+
* @param {string[]} strs
6+
* @returns {string}
7+
*/
8+
encode(strs) {
9+
let encoded = '';
10+
for (const str of strs) {
11+
// encoded += this.#hStart + str.length + this.#hEnd;
12+
encoded += this.#hStart;
13+
for (let j = 0; j < str.length; j++) {
14+
encoded += c.charCodeAt(0);
15+
if (j < str.length - 1) encoded += ':';
16+
}
17+
encoded += this.#hEnd;
18+
}
19+
return encoded;
20+
}
21+
22+
/**
23+
* @param {string} str
24+
* @returns {string[]}
25+
*/
26+
decode(str) {
27+
let decoded = [];
28+
for (let i = 0; i < str.length; i++) {
29+
let s = 0,
30+
e = 0,
31+
sFound = false,
32+
eFound = false;
33+
while (s < str.length) {
34+
if (str.charAt(s) !== this.#hStart.charAt(0)) s++;
35+
else if (str.slice(s, this.#hStart.length) === this.#hStart) {
36+
sFound = true;
37+
break;
38+
} else s += this.#hStart.length;
39+
}
40+
e = s + 1;
41+
while (e < str.length) {
42+
if (str.charAt(e) !== this.#hEnd.charAt(0)) e++;
43+
else if (str.slice(e, this.#hEnd.length) === this.#hEnd) {
44+
eFound = true;
45+
break;
46+
} else e += this.#hEnd.length;
47+
}
48+
if (sFound && eFound) {
49+
let decodedStr = '';
50+
for (let i = s + this.#hStart.length; i < e; i++) {
51+
let idxOfSeperator =
52+
}
53+
}
54+
}
55+
}
56+
}

random/two-sum.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @param {number[]} nums
3+
* @param {number} target
4+
* @return {number[]}
5+
*/
6+
const twoSum = (nums, target) => {
7+
const map = {};
8+
for (let i = 0; i < nums.length; i++) {
9+
const num = nums[i], remain = target - num;
10+
if (remain in map) return [map[remain], i];
11+
else map[num] = i;
12+
}
13+
return [];
14+
};
15+
16+
// TEST
17+
console.log(twoSum([2, 7, 11, 15], 9)); // [0, 1]
18+
console.log(twoSum([3, 2, 4], 6)); // [1, 2]
19+
console.log(twoSum([3, 3], 6)); // [0, 1]
20+
console.log(twoSum([3, 2, 3], 6)); // [0, 2]
21+
console.log(twoSum([4, 4, 3, 1, 2], 8)); // [0, 4]

random/valid-sudoku.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/**
2+
* @param {character[][]} board
3+
* @return {boolean}
4+
*/
5+
const isValidSudoku = (board) => {
6+
const rows = Array(9)
7+
.fill()
8+
.map(() => Array(9).fill(0));
9+
const cols = Array(9)
10+
.fill()
11+
.map(() => Array(9).fill(0));
12+
const boxes = Array(9)
13+
.fill()
14+
.map(() => Array(9).fill(0));
15+
16+
for (let i = 0; i < 9; i++) {
17+
for (let j = 0; j < 9; j++) {
18+
const rowCell = board[i][j];
19+
const colCell = board[j][i];
20+
const boxCell = board[3 * Math.floor(i / 3) + Math.floor(j / 3)][3 * (i % 3) + (j % 3)];
21+
22+
if (rowCell !== '.') {
23+
if (rows[i][rowCell - 1]) return false;
24+
else rows[i][rowCell - 1]++;
25+
}
26+
if (colCell !== '.') {
27+
if (cols[i][colCell - 1]) return false;
28+
else cols[i][colCell - 1]++;
29+
}
30+
if (boxCell !== '.') {
31+
if (boxes[i][boxCell - 1]) return false;
32+
else boxes[i][boxCell - 1]++;
33+
}
34+
}
35+
}
36+
37+
console.log('ROWS: ');
38+
for (const row of rows) console.log(row.join(' '));
39+
console.log('COLS: ');
40+
for (const col of cols) console.log(col.join(' '));
41+
console.log('BOXES: ');
42+
for (const box of boxes) console.log(box.join(' '));
43+
44+
return true;
45+
};
46+
47+
let board = [
48+
['5', '3', '.', '.', '7', '.', '.', '.', '.'],
49+
['6', '.', '.', '1', '9', '5', '.', '.', '.'],
50+
['.', '9', '8', '.', '.', '.', '.', '6', '.'],
51+
['8', '.', '.', '.', '6', '.', '.', '.', '3'],
52+
['4', '.', '.', '8', '.', '3', '.', '.', '1'],
53+
['7', '.', '.', '.', '2', '.', '.', '.', '6'],
54+
['.', '6', '.', '.', '.', '.', '2', '8', '.'],
55+
['.', '.', '.', '4', '1', '9', '.', '.', '5'],
56+
['.', '.', '.', '.', '8', '.', '.', '7', '9'],
57+
];
58+
59+
console.log(isValidSudoku(board));

0 commit comments

Comments
 (0)