Skip to content

Commit f5a5233

Browse files
bogektEugene Borys
authored and
Eugene Borys
committed
Initial commit
0 parents  commit f5a5233

9 files changed

+428
-0
lines changed

.gitignore

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
lerna-debug.log*
8+
9+
# Diagnostic reports (https://nodejs.org/api/report.html)
10+
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
11+
12+
# Runtime data
13+
pids
14+
*.pid
15+
*.seed
16+
*.pid.lock
17+
18+
# Directory for instrumented libs generated by jscoverage/JSCover
19+
lib-cov
20+
21+
# Coverage directory used by tools like istanbul
22+
coverage
23+
*.lcov
24+
25+
# nyc test coverage
26+
.nyc_output
27+
28+
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
29+
.grunt
30+
31+
# Bower dependency directory (https://bower.io/)
32+
bower_components
33+
34+
# node-waf configuration
35+
.lock-wscript
36+
37+
# Compiled binary addons (https://nodejs.org/api/addons.html)
38+
build/Release
39+
40+
# Dependency directories
41+
node_modules/
42+
jspm_packages/
43+
44+
# TypeScript v1 declaration files
45+
typings/
46+
47+
# TypeScript cache
48+
*.tsbuildinfo
49+
50+
# Optional npm cache directory
51+
.npm
52+
53+
# Optional eslint cache
54+
.eslintcache
55+
56+
# Microbundle cache
57+
.rpt2_cache/
58+
.rts2_cache_cjs/
59+
.rts2_cache_es/
60+
.rts2_cache_umd/
61+
62+
# Optional REPL history
63+
.node_repl_history
64+
65+
# Output of 'npm pack'
66+
*.tgz
67+
68+
# Yarn Integrity file
69+
.yarn-integrity
70+
71+
# dotenv environment variables file
72+
.env
73+
.env.test
74+
75+
# parcel-bundler cache (https://parceljs.org/)
76+
.cache
77+
78+
# Next.js build output
79+
.next
80+
81+
# Nuxt.js build / generate output
82+
.nuxt
83+
dist
84+
85+
# Gatsby files
86+
.cache/
87+
# Comment in the public line in if your project uses Gatsby and *not* Next.js
88+
# https://nextjs.org/blog/next-9-1#public-directory-support
89+
# public
90+
91+
# vuepress build output
92+
.vuepress/dist
93+
94+
# Serverless directories
95+
.serverless/
96+
97+
# FuseBox cache
98+
.fusebox/
99+
100+
# DynamoDB Local files
101+
.dynamodb/
102+
103+
# TernJS port file
104+
.tern-port

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2020 Eugene Borys
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# algo-expert
2+
Problems solutions to https://algoexpert.io

largestRange.js

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
function largestRange(array) {
2+
// Write your code here.
3+
const set = new Set(array)
4+
let i = 0
5+
let start = -1;
6+
let end = -1;
7+
let totalLength = 0
8+
let length = 0
9+
10+
while (totalLength < set.size || i < array.length) {
11+
if (
12+
start === end
13+
||
14+
array[i] < start && array[i] > end
15+
) {
16+
let testStart = array[i]
17+
let testEnd = array[i]
18+
while (set.has(testStart - 1)) testStart--
19+
while (set.has(testEnd + 1)) testEnd++
20+
if (testEnd - testStart + 1 === set.size)
21+
return [testStart, testEnd]
22+
if (length < testEnd - testStart + 1) {
23+
length = testEnd - testStart + 1
24+
start = testStart
25+
end = testEnd
26+
}
27+
totalLength += end - start + 1
28+
} else {
29+
totalLength++
30+
}
31+
i++
32+
}
33+
34+
return [start, end]
35+
}
36+
37+
38+
const test = true
39+
if (typeof test === 'boolean' && test) {
40+
const { expect } = require('../leetCode/utils.js');
41+
expect(largestRange([1, 11, 3, 0, 15, 5, 2, 4, 10, 7, 12, 6]) /*?*/, [0,7]) //?. $
42+
expect(largestRange([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) /*?*/, [0,9]) //?. $
43+
expect(largestRange([1, 1, 1, 3, 4], [3,4]) /*?*/, [3,4]) //?. $
44+
}

largestRange2.js

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
function largestRange(array) {
2+
// Write your code here.
3+
const explored = array.reduce((acc, v) => (acc[v] = true, acc), {})
4+
let start = -1
5+
let end = -1
6+
let bestLength = 0
7+
8+
for (let i = 0; i < array.length; i++) {
9+
// is already explored
10+
if (!explored[array[i]]) continue;
11+
// find range for an item
12+
let testStart = array[i]
13+
let testEnd = array[i]
14+
let testLength = 1
15+
while (testStart - 1 in explored)
16+
explored[--testStart] = false, testLength++
17+
while (testEnd + 1 in explored)
18+
explored[++testEnd] = false, testLength++
19+
// is array an all range
20+
if (testLength === Object.keys(explored).length)
21+
return [testStart, testEnd]
22+
// is new length is the best
23+
if (testLength > bestLength) {
24+
bestLength = testLength
25+
start = testStart
26+
end = testEnd
27+
}
28+
}
29+
30+
return [start, end]
31+
}
32+
33+
const test = true
34+
if (typeof test === 'boolean' && test) {
35+
const { expect } = require('../leetCode/utils.js');
36+
expect(largestRange([1]) /*?*/, [1, 1]) //?. $
37+
expect(largestRange([1, 11, 3, 0, 15, 5, 2, 4, 10, 7, 12, 6]) /*?*/, [0, 7]) //?. $
38+
expect(largestRange([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) /*?*/, [0, 9]) //?. $
39+
expect(largestRange([1, 1, 1, 3, 4], [3, 4]) /*?*/, [3, 4]) //?. $
40+
}

maxProfitWithKTransactions.js

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// [
2+
// [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] k = 0
3+
// [0, 24, 24, 24, 24, 24, 35, 35, 39, 39, 40, 40] k = 1
4+
// [0, 24, 24, 24, 24, 29, 48, 48, 61, 61, 62, 62] k = 2
5+
// [0, 24, 24, 24, 24, 29, 48, 48, 74, 74, 75, 75] k = 3
6+
// [0, 24, 24, 24, 24, 29, 48, 48, 74, 74, 84, 84] k = 4
7+
// ]
8+
// 25-1; max max max max 36-1 max 40-1 max 41-1 max k = 1
9+
// <k*2 max max 24+5 24+24 max 35+26 max 35+27 max k = 2
10+
// <k*3 35+26 max 48+26 max 48+27 max k = 3
11+
// <k*4 74+10 max k = 4
12+
// prices[j] + max(prices((i - 1)*2, j) => profits[i - 1][index(p)] - p),
13+
// profits[i][j - 1],
14+
// profits[i - 1][j],
15+
// ) = max(90 - 3 + 6, 63) = 93
16+
// where prices(i, j) = sliced i..j prices array
17+
// where index = index of min value
18+
function maxProfitWithKTransactions(prices, k) {
19+
if (!prices || prices.length < 2) return 0
20+
if (!k || k < 1) return 0
21+
22+
const profits = new Array(k + 1).fill().map(() => new Array(prices.length).fill(0))
23+
for (let i = 1; i <= k; i++)
24+
for (let j = 1; j < prices.length; j++)
25+
profits[i][j] = Math.max(
26+
prices[j] + Math.max(
27+
...prices
28+
.slice((i - 1)*2, j)
29+
.map((p, l) => profits[i - 1][l + (i - 1)*2] - p)
30+
),
31+
profits[i][j - 1],
32+
profits[i - 1][j],
33+
)
34+
35+
36+
return profits[k][prices.length - 1]
37+
}
38+
39+
// Do not edit the line below.
40+
exports.maxProfitWithKTransactions = maxProfitWithKTransactions;
41+
42+
const test = true
43+
if (typeof test === 'boolean' && test) {
44+
const { expect } = require('../leetCode/utils.js');
45+
let k
46+
expect(maxProfitWithKTransactions([]) /*?*/, 0) //?. $
47+
expect(maxProfitWithKTransactions([5, 7]) /*?*/, 0) //?. $
48+
k = 1
49+
expect(maxProfitWithKTransactions([5], k) /*?*/, 0) //?
50+
expect(maxProfitWithKTransactions([5, 3], k) /*?*/, 0) //?. $
51+
expect(maxProfitWithKTransactions([5, 11, 3, 50, 60, 90], k) /*?*/, 87) //?. $
52+
expect(maxProfitWithKTransactions([3, 2, 5, 7, 1, 3, 7], k) /*?*/, 6) //?. $
53+
expect(maxProfitWithKTransactions([1, 25, 24, 23, 12, 17, 36, 14, 40, 31, 41, 5], k) /*?*/, 40) //?. $
54+
k = 2
55+
expect(maxProfitWithKTransactions([5, 11, 3, 50, 60, 90], k) /*?*/, 93) //?. $
56+
expect(maxProfitWithKTransactions([1, 25, 24, 23, 12, 17, 36, 14, 40, 31, 41, 5], k) /*?*/, 62) //?.s $
57+
k = 3
58+
expect(maxProfitWithKTransactions([5, 11, 3, 50, 60, 90], k) /*?*/, 93) //?. $
59+
k = 4
60+
expect(maxProfitWithKTransactions([1, 25, 24, 23, 12, 17, 36, 14, 40, 31, 41, 5], k) /*?*/, 84) //?.s $
61+
}

maxProfitWithKTransactions2.js

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
function maxProfitWithKTransactions(prices, k, debug) {
2+
if (!prices || prices.length < 2) return 0
3+
if (!k || k < 1) return 0
4+
5+
const profits = [new Array(prices.length).fill(0)]
6+
for (let t = 1; t <= k; t++) {
7+
const oldProfitsWithoutPrices = prices.map((p, i) => profits[t - 1][i] - p)
8+
profits[t] = new Array(prices.length).fill(0)
9+
for (let d = 0; d < prices.length; d++)
10+
profits[t][d] = d < (t - 1)*2 + 1
11+
? profits[t-1][d]
12+
: Math.max(
13+
prices[d] + Math.max(...oldProfitsWithoutPrices.slice((t - 1)*2, d)),
14+
profits[t][d - 1],
15+
)
16+
debug && console.log(t, profits[t])
17+
delete profits[t - 1]
18+
}
19+
20+
return profits[k][prices.length - 1]
21+
}
22+
23+
// Do not edit the line below.
24+
exports.maxProfitWithKTransactions = maxProfitWithKTransactions;
25+
26+
const test = true
27+
if (typeof test === 'boolean' && test) {
28+
const { expect } = require('../leetCode/utils.js');
29+
let k
30+
expect(maxProfitWithKTransactions([]) /*?*/, 0) //?. $
31+
expect(maxProfitWithKTransactions([5, 7]) /*?*/, 0) //?. $
32+
k = 1
33+
expect(maxProfitWithKTransactions([5], k) /*?*/, 0) //?
34+
expect(maxProfitWithKTransactions([5, 3], k) /*?*/, 0) //?. $
35+
expect(maxProfitWithKTransactions([5, 11, 3, 50, 60, 90], k) /*?*/, 87) //?. $
36+
expect(maxProfitWithKTransactions([3, 2, 5, 7, 1, 3, 7], k) /*?*/, 6) //?. $
37+
expect(maxProfitWithKTransactions([1, 25, 24, 23, 12, 17, 36, 14, 40, 31, 41, 5], k) /*?*/, 40) //?. $
38+
k = 2
39+
expect(maxProfitWithKTransactions([5, 11, 3, 50, 60, 90], k) /*?*/, 93) //?. $
40+
expect(maxProfitWithKTransactions([1, 25, 24, 23, 12, 17, 36, 14, 40, 31, 41, 5], k) /*?*/, 62) //?.s $
41+
k = 3
42+
expect(maxProfitWithKTransactions([1, 10], k, 1) /*?*/, 9) //?. $
43+
expect(maxProfitWithKTransactions([5, 11, 3, 50, 60, 90], k) /*?*/, 93) //?. $
44+
k = 4
45+
expect(maxProfitWithKTransactions([1, 25, 24, 23, 12, 17, 36, 14, 40, 31, 41, 5], k
46+
) /*?*/, 84) //?.s $
47+
}

maxProfitWithKTransactions3.js

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
function maxProfitWithKTransactions(prices, k, debug) {
2+
if (!prices || prices.length < 2) return 0
3+
if (!k || k < 1) return 0
4+
5+
const profits = [new Array(prices.length).fill(0)]
6+
for (let t = 1; t <= k; t++) {
7+
let maxThusFar = -Infinity
8+
profits[t] = new Array(prices.length).fill(0)
9+
for (let d = 0; d < prices.length; d++)
10+
profits[t][d] = d >= (t - 1)*2 + 1
11+
? Math.max(
12+
prices[d] + (
13+
maxThusFar = Math.max(
14+
profits[t - 1][d - 1] - prices[d - 1],
15+
maxThusFar,
16+
)
17+
),
18+
profits[t][d - 1],
19+
)
20+
: profits[t-1][d]
21+
debug && console.log(t, profits[t])
22+
delete profits[t - 1]
23+
}
24+
25+
return profits[k][prices.length - 1]
26+
}
27+
28+
// Do not edit the line below.
29+
exports.maxProfitWithKTransactions = maxProfitWithKTransactions;
30+
31+
const test = true
32+
if (typeof test === 'boolean' && test) {
33+
const { expect } = require('../leetCode/utils.js');
34+
let k
35+
expect(maxProfitWithKTransactions([]) /*?*/, 0) //?. $
36+
expect(maxProfitWithKTransactions([5, 7]) /*?*/, 0) //?. $
37+
k = 1
38+
expect(maxProfitWithKTransactions([5], k) /*?*/, 0) //?
39+
expect(maxProfitWithKTransactions([5, 3], k) /*?*/, 0) //?. $
40+
expect(maxProfitWithKTransactions([5, 11, 3, 50, 60, 90], k) /*?*/, 87) //?. $
41+
expect(maxProfitWithKTransactions([3, 2, 5, 7, 1, 3, 7], k) /*?*/, 6) //?. $
42+
expect(maxProfitWithKTransactions([1, 25, 24, 23, 12, 17, 36, 14, 40, 31, 41, 5], k) /*?*/, 40) //?. $
43+
k = 2
44+
expect(maxProfitWithKTransactions([5, 11, 3, 50, 60, 90], k) /*?*/, 93) //?. $
45+
expect(maxProfitWithKTransactions([1, 25, 24, 23, 12, 17, 36, 14, 40, 31, 41, 5], k) /*?*/, 62) //?.s $
46+
k = 3
47+
expect(maxProfitWithKTransactions([1, 10], k, 1) /*?*/, 9) //?. $
48+
expect(maxProfitWithKTransactions([5, 11, 3, 50, 60, 90], k) /*?*/, 93) //?. $
49+
k = 4
50+
expect(maxProfitWithKTransactions([1, 25, 24, 23, 12, 17, 36, 14, 40, 31, 41, 5], k
51+
) /*?*/, 84) //?.s $
52+
}

0 commit comments

Comments
 (0)