Skip to content

Commit d326c8d

Browse files
author
baochau.dinh
committed
js-concepts: minDistance voz
1 parent e8f040d commit d326c8d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+1644
-28
lines changed

.DS_Store

16 KB
Binary file not shown.

.vscode/c_cpp_properties.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"configurations": [
3+
{
4+
"name": "Mac",
5+
"includePath": [
6+
"${workspaceFolder}/**"
7+
],
8+
"defines": [],
9+
"macFrameworkPath": [
10+
"/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks"
11+
],
12+
"compilerPath": "/usr/bin/clang",
13+
"cStandard": "c17",
14+
"cppStandard": "c++17",
15+
"intelliSenseMode": "macos-clang-arm64"
16+
}
17+
],
18+
"version": 4
19+
}

Algo101/.DS_Store

8 KB
Binary file not shown.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// you can write to stdout for debugging purposes, e.g.
2+
// console.log('this is a debug message');
3+
4+
function solution(X, Y, colors) {
5+
// write your code in JavaScript (Node.js 8.9.4)
6+
if (X.length === 0 || Y.length === 0 || colors.length === 0) return 0;
7+
if (X.length !== Y.length || X.length !== colors.length) return 0;
8+
9+
const numberOfPoints = X.length;
10+
11+
// calculate distance from O to given points
12+
const distances = new Array(numberOfPoints).map(x => 0);
13+
for (let i = 0; i < numberOfPoints; i++) {
14+
distances[i] = {
15+
color: colors[i],
16+
value: Math.pow(X[i], 2) + Math.pow(Y[i], 2),
17+
};
18+
}
19+
20+
// Sort distances
21+
distances.sort((a, b) => a.value - b.value);
22+
23+
// Calculate frequency
24+
let freqDistance = {};
25+
for (let i = 0; i < distances.length; i++) {
26+
freqDistance[distances[i].value] = {
27+
freq: freqDistance[distances[i].value] ? freqDistance[distances[i].value].freq + 1 : 1,
28+
weight: freqDistance[distances[i].value]
29+
? distances[i].color === 'G'
30+
? freqDistance[distances[i].value].weight + 1
31+
: freqDistance[distances[i].value].weight - 1
32+
: distances[i].color === 'G'
33+
? 1
34+
: -1
35+
};
36+
}
37+
38+
let result = 0;
39+
let count = 0;
40+
let currFreq = 0;
41+
for (const distance in freqDistance) {
42+
count += freqDistance[distance].weight;
43+
currFreq += freqDistance[distance].freq;
44+
if (count === 0) result = currFreq;
45+
}
46+
47+
return result;
48+
}
49+
50+
console.log(solution([4, 0, 2, -2], [4, 1, 2, -3], 'RGRR'));

Algo101/CodilityTraining/countDiv.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Problem: https://app.codility.com/c/run/training92BUEY-XCH/
3+
*/
4+
5+
function solution (A, B, K) {
6+
if (A === B === 0) return 0;
7+
if (K === 1) return B - A + 1;
8+
9+
let result = 0;
10+
for (let step = A; step <= B; step++) {
11+
if (step % K === 0) {
12+
result++;
13+
step += K - 1;
14+
}
15+
}
16+
17+
return result;
18+
}
19+
20+
console.log(solution(6, 10, 2));
21+
22+
function solutionV2 (A, B, K) {
23+
if (A === B === 0) return 0;
24+
return Math.floor(B/K) - Math.ceil(A/K) + 1;
25+
}
26+
27+
console.log(solutionV2(6, 10, 2));
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
function solution(A) {
2+
if (A.length === 0) return 0;
3+
const markedArray = new Array(1000000).fill(false);
4+
5+
for (let i = 0; i < A.length; i++) {
6+
if (A[i] > 0) markedArray[A[i]] = true;
7+
}
8+
9+
for (let i = 1; i < markedArray.length; i++) {
10+
if (markedArray[i] === false) return i;
11+
}
12+
13+
return null;
14+
}
15+
16+
console.log(solution([-2, 2, 0, 4, 5]));
17+
18+
function solutionV2(A) {
19+
if (A.length === 0) return 0;
20+
let currItem = 1;
21+
22+
A.sort((a, b) => a - b);
23+
24+
for (let i = 0; i < A.length; i++) {
25+
if (A[i] > 0 && A[i] === currItem) {
26+
currItem = A[i] + 1;
27+
}
28+
}
29+
30+
return currItem;
31+
}
32+
33+
console.log(solutionV2([-2, 2, 0, 4, 5]))
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/**
2+
* This problem was asked by Google
3+
*
4+
* Given a sorted array of integers, square the elements of the array and
5+
* give the output in sorted order
6+
*
7+
* Ex: [-9, -2, 0, 2, 3]
8+
* Output: [0, 4, 4, 9, 81]
9+
*/
10+
11+
class Solution {
12+
constructor(input) {
13+
this.input = input || [];
14+
}
15+
16+
answer() { // O(nlogn)
17+
const shadowCopyInput = [...this.input];
18+
let squareNumbers = shadowCopyInput.map((number) => number * number); // O(n);
19+
20+
return squareNumbers.sort((a, b) => a - b); // O(nlogn);
21+
}
22+
23+
optimizedAnswer() {
24+
const shadowCopyInput = [...this.input];
25+
const result = [];
26+
let first = 0, second = shadowCopyInput.length - 1;
27+
28+
/**
29+
* [-9, -2, 0, 2, 3]
30+
* first second
31+
*
32+
*/
33+
while (first <= second) {
34+
const firstSquare = Math.pow(shadowCopyInput[first], 2);
35+
const secondSquare = Math.pow(shadowCopyInput[second], 2);
36+
if (firstSquare < secondSquare) {
37+
result.unshift(firstSquare);
38+
first++;
39+
} else {
40+
result.unshift(secondSquare);
41+
second--;
42+
}
43+
}
44+
45+
return result;
46+
}
47+
48+
logger() { // O(nlogn)
49+
const result = this.answer();
50+
console.log('result: ', result);
51+
return;
52+
}
53+
54+
optimizedLogger() {
55+
const result = this.optimizedAnswer();
56+
console.log('result: ', result);
57+
return;
58+
}
59+
}
60+
61+
const solution = new Solution([-9, -2, 0, 2, 3]);
62+
solution.logger(); // O(nlogn)
63+
solution.optimizedLogger();
64+
65+

Algo101/Day1/problem1.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* Tìm số fibonacci thứ Nth
3+
*/
4+
5+
function findFib (n) {
6+
if (n === 0) return 0;
7+
if (n === 1 || n === 2) return 1;
8+
9+
let first = 1, second = 1;
10+
for (let i = 2; i <= n; i++) {
11+
second = first + second;
12+
first = second - first;
13+
}
14+
15+
return first;
16+
}
17+
18+
console.log(findFib(1000));
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function isSubsequence(a, b) {
2+
if (a.length === 0 || b.length === 0) return false
3+
4+
let first = 0, second = 0;
5+
while (first < a.length && second < b.length) {
6+
if (a[first] === b[second]) {
7+
first++;
8+
second++;
9+
} else {
10+
first++;
11+
}
12+
}
13+
if (second < b.length) return false;
14+
return true;
15+
}
16+
17+
console.log(isSubsequence('aabb', 'ba'));

Algo101/Dynamic-Programming/lcs.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
function solveLCS (a, b) {
2+
function LCS(n, m) {
3+
if (n * m === 0) return 0;
4+
5+
if (a[n-1] === b[m-1]) return LCS(n-1, m-1) + 1;
6+
return Math.max(LCS(n-1, m), LCS(n, m-1));
7+
}
8+
9+
return LCS(a.length, b.length);
10+
}
11+
12+
function dpLCS (a, b) {
13+
if (a.length * b.length === 0) return 0;
14+
15+
16+
}
17+
18+
// 2-Dimension array
19+
let newArr = new Array(3).fill(0).map(item => new Array(3))
20+
21+
console.log('new array: ', newArr);
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
function findMaxPalindrome (A) {
2+
if (A.length === 0) return 0;
3+
4+
let p = new Array(A.length + 2).fill(1).map(x => new Array(A.length + 2).fill(1));
5+
6+
let first = 1, second = A.length;
7+
while (first < A.length / 2 && second >= A.length / 2) {
8+
if (A[first] === A[second]) {
9+
p[first][second] = 2 + p[first + 1][second - 1];
10+
first++;
11+
second--;
12+
} else {
13+
p[first][second] = Math.max(p[first][second - 1], p[first + 1][second]);
14+
first++;
15+
second--;
16+
}
17+
}
18+
19+
return p
20+
}
21+
22+
console.log(findMaxPalindrome('abba'));
23+
24+
function isPalindrome(str) {
25+
return str === str.split("").reverse().join("");
26+
}
27+
28+
function solveLPS(seq, i, j) {
29+
30+
// case: only one character
31+
if (i === j) return 1;
32+
33+
// case: only two same characters
34+
if (seq[i] === seq[j] && i + 1 === j) {
35+
return 2;
36+
}
37+
38+
// case: first and last characters match
39+
if (seq[i] === seq[j]) {
40+
return solveLPS(seq, i + 1, j - 1) + 2;
41+
}
42+
43+
return Math.max(solveLPS(seq, i, j - 1), solveLPS(seq, i + 1, j));
44+
}
45+
46+
console.log(solveLPS('abba', 0, 3));
47+
48+
/**
49+
*
50+
* L(0, 5)
51+
/ \
52+
/ \
53+
L(1,5) L(0,4)
54+
/ \ / \
55+
/ \ / \
56+
L(2,5) L(1,4) L(1,4) L(0,3)
57+
58+
*/
59+
60+
function solveDpLPS(seq) {
61+
let length = seq.length;
62+
let i, j, col;
63+
64+
let dpTable = new Array(length).fill(0).map(x => new Array(length).fill(0));
65+
66+
for (let i = 0; i < length; i++) {
67+
dpTable[i][i] = 1;
68+
}
69+
70+
for (col = 2; col <= length; col++) {
71+
for (i = 0; i < length - col + 1; i++) {
72+
j = i + col - 1;
73+
74+
if (seq[i] === seq[j] && col === 2) {
75+
dpTable[i][j] = 2;
76+
} else if (seq[i] === seq[j]) {
77+
dpTable[i][j] = dpTable[i + 1][j - 1] + 2;
78+
} else {
79+
dpTable[i][j] = Math.max(dpTable[i][j - 1], dpTable[i + 1][j]);
80+
}
81+
}
82+
}
83+
84+
return dpTable[0][length - 1];
85+
}
86+
87+
console.log(solveDpLPS('ABBA'));

Algo101/Multithreads/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
const worker = new Worker('./worker.js');
2+
worker.postMessage('Hello, world!');

Algo101/Multithreads/worker.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
self.onmessage = msg => console.log(msg.data);

Algo101/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
## A general way to solve algorithm problems
2+
3+
#### Step 1: Define problem
4+
5+
#### Step 2: Representation
6+
7+
#### Step 3: Approach/Strategy

Algo101/Seaports/heap.js

Whitespace-only changes.

Algo101/Seaports/normalizeTimeData.js

Whitespace-only changes.

Algo101/Seaports/queue.js

Whitespace-only changes.

0 commit comments

Comments
 (0)