Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 33ef7b5

Browse files
feat(js): add modular solution and documentation for hard challenge 5 - Line Sweep Closest Points
1 parent 33193f9 commit 33ef7b5

File tree

3 files changed

+188
-0
lines changed

3 files changed

+188
-0
lines changed
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
# Optimal Binary Search Tree
2+
3+
## English
4+
5+
The optimal binary search tree problem is a classic dynamic programming problem that aims to minimize the expected search cost based on access frequencies. It has applications in compiler design and database indexing.
6+
7+
This challenge involves implementing a dynamic programming algorithm to construct an optimal binary search tree minimizing the average search cost.
8+
9+
### Relevant Code Snippet
10+
11+
```javascript
12+
class OptimalBST {
13+
constructor(keys, freq) {
14+
this.keys = keys;
15+
this.freq = freq;
16+
this.n = keys.length;
17+
this.dp = Array.from({ length: this.n }, () => new Array(this.n).fill(0));
18+
}
19+
20+
optimalCost() {
21+
const prefixSum = new Array(this.n + 1).fill(0);
22+
for (let i = 0; i < this.n; i++) {
23+
prefixSum[i + 1] = prefixSum[i] + this.freq[i];
24+
}
25+
26+
const getSum = (i, j) => prefixSum[j + 1] - prefixSum[i];
27+
28+
const costForRoot = (i, j, r) => {
29+
const left = r > i ? this.dp[i][r - 1] : 0;
30+
const right = r < j ? this.dp[r + 1][j] : 0;
31+
return left + right + getSum(i, j);
32+
};
33+
34+
for (let i = 0; i < this.n; i++) {
35+
this.dp[i][i] = this.freq[i];
36+
}
37+
38+
for (let length = 2; length <= this.n; length++) {
39+
for (let i = 0; i <= this.n - length; i++) {
40+
const j = i + length - 1;
41+
let minCost = Infinity;
42+
for (let r = i; r <= j; r++) {
43+
const cost = costForRoot(i, j, r);
44+
if (cost < minCost) {
45+
minCost = cost;
46+
}
47+
}
48+
this.dp[i][j] = minCost;
49+
}
50+
}
51+
52+
return this.dp[0][this.n - 1];
53+
}
54+
}
55+
```
56+
57+
### History
58+
59+
The optimal binary search tree problem has been widely studied in computer science and has practical applications in compiler design and database indexing.
60+
61+
---
62+
63+
## Español
64+
65+
Árbol Binario de Búsqueda Óptimo
66+
67+
El problema del árbol binario de búsqueda óptimo es un problema clásico de programación dinámica que busca minimizar el costo esperado de búsqueda basado en las frecuencias de acceso. Tiene aplicaciones en diseño de compiladores e indexación de bases de datos.
68+
69+
Este reto consiste en implementar un algoritmo de programación dinámica para construir un árbol binario de búsqueda óptimo que minimice el costo promedio de búsqueda.
70+
71+
### Fragmento de Código Relevante
72+
73+
```javascript
74+
class OptimalBST {
75+
constructor(keys, freq) {
76+
this.keys = keys;
77+
this.freq = freq;
78+
this.n = keys.length;
79+
this.dp = Array.from({ length: this.n }, () => new Array(this.n).fill(0));
80+
}
81+
82+
optimalCost() {
83+
const prefixSum = new Array(this.n + 1).fill(0);
84+
for (let i = 0; i < this.n; i++) {
85+
prefixSum[i + 1] = prefixSum[i] + this.freq[i];
86+
}
87+
88+
const getSum = (i, j) => prefixSum[j + 1] - prefixSum[i];
89+
90+
const costForRoot = (i, j, r) => {
91+
const left = r > i ? this.dp[i][r - 1] : 0;
92+
const right = r < j ? this.dp[r + 1][j] : 0;
93+
return left + right + getSum(i, j);
94+
};
95+
96+
for (let i = 0; i < this.n; i++) {
97+
this.dp[i][i] = this.freq[i];
98+
}
99+
100+
for (let length = 2; length <= this.n; length++) {
101+
for (let i = 0; i <= this.n - length; i++) {
102+
const j = i + length - 1;
103+
let minCost = Infinity;
104+
for (let r = i; r <= j; r++) {
105+
const cost = costForRoot(i, j, r);
106+
if (cost < minCost) {
107+
minCost = cost;
108+
}
109+
}
110+
this.dp[i][j] = minCost;
111+
}
112+
}
113+
114+
return this.dp[0][this.n - 1];
115+
}
116+
}
117+
```
118+
119+
### Historia
120+
121+
El problema del árbol binario de búsqueda óptimo ha sido ampliamente estudiado en ciencias de la computación y tiene aplicaciones prácticas en diseño de compiladores e indexación de bases de datos.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// OptimalBST.js - Optimal Binary Search Tree using Dynamic Programming in JavaScript
2+
3+
class OptimalBST {
4+
constructor(keys, freq) {
5+
this.keys = keys;
6+
this.freq = freq;
7+
this.n = keys.length;
8+
this.dp = Array.from({ length: this.n }, () => new Array(this.n).fill(0));
9+
}
10+
11+
optimalCost() {
12+
const prefixSum = new Array(this.n + 1).fill(0);
13+
for (let i = 0; i < this.n; i++) {
14+
prefixSum[i + 1] = prefixSum[i] + this.freq[i];
15+
}
16+
17+
const getSum = (i, j) => prefixSum[j + 1] - prefixSum[i];
18+
19+
const costForRoot = (i, j, r) => {
20+
const left = r > i ? this.dp[i][r - 1] : 0;
21+
const right = r < j ? this.dp[r + 1][j] : 0;
22+
return left + right + getSum(i, j);
23+
};
24+
25+
for (let i = 0; i < this.n; i++) {
26+
this.dp[i][i] = this.freq[i];
27+
}
28+
29+
for (let length = 2; length <= this.n; length++) {
30+
for (let i = 0; i <= this.n - length; i++) {
31+
const j = i + length - 1;
32+
let minCost = Infinity;
33+
for (let r = i; r <= j; r++) {
34+
const cost = costForRoot(i, j, r);
35+
if (cost < minCost) {
36+
minCost = cost;
37+
}
38+
}
39+
this.dp[i][j] = minCost;
40+
}
41+
}
42+
43+
return this.dp[0][this.n - 1];
44+
}
45+
}
46+
47+
export default OptimalBST;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
Challenge:
3+
Given search frequencies for different elements, implement a dynamic programming algorithm to construct an optimal binary search tree minimizing the average search cost.
4+
5+
This solution follows DRY principles and is implemented in JavaScript.
6+
*/
7+
8+
import OptimalBST from "./OptimalBST.js";
9+
10+
function main() {
11+
const keys = [10, 12, 20];
12+
const freq = [34, 8, 50];
13+
14+
const optimalBST = new OptimalBST(keys, freq);
15+
const minCost = optimalBST.optimalCost();
16+
17+
console.log("Minimum cost of Optimal BST:", minCost);
18+
}
19+
20+
main();

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /