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 42c81ca

Browse files
new exercises
1 parent 40ebc2c commit 42c81ca

File tree

8 files changed

+674
-0
lines changed

8 files changed

+674
-0
lines changed

‎.gitignore‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@ node_modules
22
.idea/
33
coverage/
44
**/dist/
5+
_book/
6+
.DS_Store
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/* eslint-disable */
2+
3+
/**
4+
* binary search O(log n)
5+
* @param {number[]} nums
6+
* @param {number} target
7+
* @return {number}
8+
*/
9+
var searchInsert = function(nums, target, start = 0) {
10+
if (!nums.length) return start;
11+
const i = parseInt(nums.length / 2, 10);
12+
13+
if (nums[i] === target) {
14+
return start + i;
15+
} else if (target > nums[i]) {
16+
const newArray = nums.slice(i + 1);
17+
if (!newArray.length) return start + i + 1;
18+
return searchInsert(newArray, target, start + i + 1);
19+
} else {
20+
return searchInsert(nums.slice(0, i), target, start);
21+
}
22+
};
23+
24+
// ---
25+
26+
const assert = require('assert');
27+
function test() {
28+
assert.equal(searchInsert([], 0), 0);
29+
assert.equal(searchInsert([], 1), 0);
30+
31+
assert.equal(searchInsert([1], 0), 0);
32+
assert.equal(searchInsert([1], 2), 1);
33+
34+
assert.equal(searchInsert([1,3,5,6], 5), 2);
35+
assert.equal(searchInsert([1,3,5,6], 2), 1);
36+
assert.equal(searchInsert([1,3,5,6], 7), 4);
37+
assert.equal(searchInsert([1,3,5,6], 0), 0);
38+
39+
assert.equal(searchInsert([1,3,5], 5), 2);
40+
41+
}
42+
test();
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
/* eslint-disable */
2+
3+
/**
4+
* https://leetcode.com/articles/regular-expression-matching/
5+
* Pattern:
6+
* '.' Matches any single character.
7+
* '*' Matches zero or more of the preceding element.
8+
*
9+
* @param {string} s
10+
* @param {string} p
11+
* @return {boolean}
12+
*/
13+
var isMatch = function(s, p) {
14+
let si = s.length - 1;
15+
16+
for(let pi = p.length - 1; pi > 0; pi--) {
17+
switch(p[pi]) {
18+
case '.':
19+
if (si > 0) { si--; }
20+
else { return false; }
21+
break;
22+
case '*':
23+
const char = p[--pi];
24+
while(si > 0 && (char === '.' || char === s[si])) {
25+
si--;
26+
}
27+
break;
28+
default:
29+
if (si > 0 && p[pi] === s[si]) {
30+
si--;
31+
} else {
32+
return false;
33+
}
34+
}
35+
}
36+
37+
return si <= 0;
38+
};
39+
40+
const assert = require('assert');
41+
function test() {
42+
assert.equal(isMatch('aa', 'aa'), true);
43+
assert.equal(isMatch('aa', 'a'), false);
44+
45+
assert.equal(isMatch('aa', 'a.'), true);
46+
assert.equal(isMatch('aaa', 'a.'), false);
47+
assert.equal(isMatch('aaa', 'a..'), true);
48+
49+
assert.equal(isMatch('', 'a*'), true);
50+
assert.equal(isMatch('a', 'a*'), true);
51+
assert.equal(isMatch('aa', 'a*'), true);
52+
assert.equal(isMatch('aab', 'a*'), false);
53+
assert.equal(isMatch('aab', 'a*b'), true);
54+
55+
assert.equal(isMatch('aab', 'c*a*b'), true);
56+
assert.equal(isMatch('caab', 'c*a*b'), true);
57+
58+
assert.equal(isMatch('abc', '.*'), true);
59+
60+
assert.equal(isMatch('mississippi', 'mis*is*p*.'), false);
61+
assert.equal(isMatch('mississippi', 'mis*is*ip*.'), true);
62+
63+
assert.equal(isMatch('ab', '.*..'), true);
64+
assert.equal(isMatch('abcz', '.*z'), true);
65+
assert.equal(isMatch('abcz', '.*x'), false);
66+
assert.equal(isMatch('zabc', 'z.*'), true);
67+
assert.equal(isMatch('zabc', 'x.*'), false);
68+
assert.equal(isMatch('zabcz', 'z.*z'), true);
69+
assert.equal(isMatch('zabcx', 'z.*z'), false);
70+
}
71+
test();
72+
73+
74+
75+
/////
76+
77+
var isMatch3 = function(s, p) {
78+
let pi = 0;
79+
80+
for(let si = 0; si < s.length; si++) {
81+
switch(p[pi]) {
82+
case '.':
83+
if (pi < p.length) { pi++; }
84+
else { return false; }
85+
break;
86+
case '*':
87+
const last = p[pi - 1];
88+
if(last === s[si]) {
89+
break; // break switch
90+
}
91+
pi++; // continue to the default case
92+
default:
93+
if (pi < p.length && p[pi] === s[si]) {
94+
pi++;
95+
continue;
96+
} else {
97+
return false;
98+
}
99+
}
100+
}
101+
102+
return true;
103+
};
104+
105+
var isMatch2 = function(s, p) {
106+
let pi = p.length - 1;
107+
108+
for(let si = s.length - 1; si > 0; si--) {
109+
switch(p[pi]) {
110+
case '.':
111+
if (pi > 0) { pi--; }
112+
else { return false; }
113+
break;
114+
case '*':
115+
const last = p[--pi];
116+
while(si > 0 && last === s[si]) {
117+
si--;
118+
}
119+
break;
120+
default:
121+
if (pi > 0 && p[pi] === s[si]) {
122+
pi--;
123+
continue;
124+
} else {
125+
return false;
126+
}
127+
}
128+
}
129+
130+
return true;
131+
};
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
/* eslint-disable */
2+
3+
/**
4+
* Runtime O(10^n)
5+
* https://leetcode.com/problems/next-permutation/
6+
* @param {number[]} nums
7+
* @return {void} Do not return anything, modify nums in-place instead.
8+
*/
9+
var nextPermutation2 = function(nums) {
10+
const current = parseInt(nums.join(''), 10);
11+
const max = Math.pow(10, nums.length);
12+
13+
for(let next = current + 1; next < max; next++) {
14+
if(isPermutation(nums, next)) {
15+
setNumbersInArray(nums, next);
16+
return;
17+
}
18+
}
19+
};
20+
21+
function isPermutation(array, number) {
22+
return array.sort().join('') === numberToArray(number, array.length).sort().join('');
23+
}
24+
25+
function numberToArray(num, pad = 0) {
26+
return num.toString().padStart(pad, "0").split('').map(s => +s)
27+
}
28+
29+
function setNumbersInArray(to, number) {
30+
const from = numberToArray(number, to.length);
31+
32+
for(let i = 0; i < to.length; i++) {
33+
to[i] = from[i];
34+
}
35+
}
36+
37+
38+
/*
39+
// swap last with previous and check if is bigger
40+
// if not, swap last with previous - 1 recursively and check if bigger
41+
// sort in asc order the rest and check if bigger
42+
43+
123
44+
132
45+
213
46+
231
47+
312
48+
321
49+
---
50+
123
51+
*/
52+
53+
// generate all numbers - discard no matching: O(10^n)
54+
//
55+
56+
57+
// starting from last find a bigger number than current
58+
59+
60+
function nextPermutation(nums) {
61+
62+
// try to find next
63+
for(let i = nums.length - 2; i >= 0; i--) {
64+
let smallestBigger;
65+
66+
for(let j = i + 1; j < nums.length; j++) {
67+
if (nums[j] > nums[i]) {
68+
smallestBigger = Math.min(nums[j], smallestBigger || nums[j]);
69+
}
70+
}
71+
72+
if (smallestBigger) {
73+
const k = nums.lastIndexOf(smallestBigger);
74+
[nums[i], nums[k]] = [nums[k], nums[i]]; // swap
75+
// sort asc starting from i
76+
sort(nums, i + 1);
77+
return;
78+
}
79+
}
80+
81+
sort(nums);
82+
}
83+
84+
/**
85+
* Sort in-place from start on
86+
* @param {*} array
87+
* @param {*} start
88+
*/
89+
function sort(array, start = 0) {
90+
for(let i = start; i < array.length; i++) {
91+
for (let j = i + 1; j < array.length; j++) {
92+
if (array[i] > array[j]) {
93+
[array[i], array[j]] = [array[j], array[i]];
94+
}
95+
}
96+
}
97+
}
98+
99+
// -----
100+
const assert = require('assert');
101+
function test() {
102+
let a;
103+
104+
a = [100, 4,3,2];
105+
sort(a, 1);
106+
assert.deepEqual(a, [100, 2, 3, 4]);
107+
108+
a = [1, 2, 3];
109+
nextPermutation(a);
110+
assert.deepStrictEqual(a, [1, 3, 2]);
111+
112+
a = [2, 3, 1];
113+
nextPermutation(a);
114+
assert.deepStrictEqual(a, [3, 1, 2]);
115+
116+
a = [3, 2, 1];
117+
nextPermutation(a);
118+
assert.deepEqual(a, [1, 2, 3]);
119+
120+
a = [0,0,4,2,1,0];
121+
nextPermutation(a);
122+
assert.deepStrictEqual(a, [0,1,0,0,2,4]);
123+
124+
// Time Limit Exceeded on leetcode for O(n^10) algorithm
125+
a = [2,2,7,5,4,3,2,2,1];
126+
nextPermutation(a);
127+
assert.deepStrictEqual(a, [2,3,1,2,2,2,4,5,7]);
128+
129+
a = [5,4,7,5,3,2];
130+
nextPermutation(a);
131+
assert.deepEqual(a, [5,5,2,3,4,7]);
132+
}
133+
test();

‎lab/exercises/medium/permute.js‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/* eslint-disable */
2+
3+
/**
4+
* https://www.youtube.com/watch?v=AfxHGNRtFac
5+
* https://www.geeksforgeeks.org/write-a-c-program-to-print-all-permutations-of-a-given-string/
6+
* https://leetcode.com/articles/permutations/
7+
* @param {number[]} nums
8+
* @return {number[][]}
9+
*/
10+
var permute = function (nums) {
11+
var result = [];
12+
backtrack(nums, result);
13+
return result;
14+
};
15+
16+
function backtrack(nums, result, first = 0) {
17+
// console.log(Array(first).fill(" ").join(""), JSON.stringify({nums, first}));
18+
if (first === nums.length - 1) {
19+
// console.log(nums);
20+
result.push(nums.slice());
21+
} else {
22+
for (let i = first; i < nums.length; i++) {
23+
swap(nums, first, i);
24+
backtrack(nums, result, first + 1);
25+
swap(nums, first, i);
26+
}
27+
}
28+
};
29+
30+
function swap(array, i, j) {
31+
[array[i], array[j]] = [array[j], array[i]]
32+
}
33+
34+
// ---
35+
36+
const assert = require('assert');
37+
38+
function test() {
39+
// assert.deepEqual(permute([1]), [
40+
// [1]
41+
// ]);
42+
// assert.deepEqual(permute([1, 2]), [
43+
// [1, 2],
44+
// [2, 1]
45+
// ]);
46+
assert.deepEqual(permute([1, 2, 3]), [
47+
[1, 2, 3],
48+
[1, 3, 2],
49+
[2, 1, 3],
50+
[2, 3, 1],
51+
[3, 2, 1],
52+
[3, 1, 2]
53+
]);
54+
}
55+
test();

0 commit comments

Comments
(0)

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