Skip to content

Commit 018c3d9

Browse files
committed
Next tasks
1 parent be3a707 commit 018c3d9

File tree

6 files changed

+209
-0
lines changed

6 files changed

+209
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { isMatch } from './index';
2+
3+
describe('isMatch', () => {
4+
const testCases = [
5+
{
6+
name: 'Case 1',
7+
input: { s: 'aa', p: 'a' },
8+
expected: false,
9+
},
10+
{
11+
name: 'Case 2',
12+
input: { s: 'aa', p: 'a*' },
13+
expected: true,
14+
},
15+
{
16+
name: 'Case 3',
17+
input: { s: 'ab', p: '.*' },
18+
expected: true,
19+
},
20+
{
21+
name: 'Case 4',
22+
input: { s: 'aab', p: 'c*a*b' },
23+
expected: true,
24+
},
25+
{
26+
name: 'Case 5',
27+
input: { s: 'mississippi', p: 'mis*is*p*.' },
28+
expected: false,
29+
},
30+
];
31+
32+
for (const testCase of testCases) {
33+
test(testCase.name, () => {
34+
expect(isMatch(testCase.input.s, testCase.input.p)).toBe(
35+
testCase.expected,
36+
);
37+
});
38+
}
39+
});
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Description:
2+
// Given an input string s and a pattern p, implement regular expression matching with support for '.' and '*' where:
3+
// '.' Matches any single character.
4+
// '*' Matches zero or more of the preceding element.
5+
//The matching should cover the entire input string (not partial).
6+
//
7+
// Constraints:
8+
// 1 <= s.length <= 20
9+
// 1 <= p.length <= 20
10+
// s contains only lowercase English letters.
11+
// p contains only lowercase English letters, '.', and '*'.
12+
//It is guaranteed for each appearance of the character '*', there will be a previous valid character to match.
13+
14+
export const isMatch = (s: string, p: string): boolean => {
15+
const columns = s.length;
16+
const rows = p.length;
17+
const arr = Array.from({ length: columns + 1 }, () =>
18+
Array(rows + 1).fill(false),
19+
);
20+
21+
arr[0][0] = true;
22+
23+
for (let i = 0; i <= columns; ++i) {
24+
for (let j = 1; j <= rows; ++j) {
25+
if (p[j - 1] === '*') {
26+
arr[i][j] = arr[i][j - 2];
27+
if (i && (p[j - 2] === '.' || p[j - 2] === s[i - 1])) {
28+
arr[i][j] |= arr[i - 1][j];
29+
}
30+
} else if (i && (p[j - 1] === '.' || p[j - 1] === s[i - 1])) {
31+
arr[i][j] = arr[i - 1][j - 1];
32+
}
33+
}
34+
}
35+
return Boolean(arr[columns][rows]);
36+
};
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { maxArea } from './index';
2+
3+
describe('maxArea', () => {
4+
const testCases = [
5+
{
6+
name: 'Case 1',
7+
input: [1, 8, 6, 2, 5, 4, 8, 3, 7],
8+
expected: 49,
9+
},
10+
{
11+
name: 'Case 2',
12+
input: [1, 1],
13+
expected: 1,
14+
},
15+
];
16+
17+
for (const testCase of testCases) {
18+
test(testCase.name, () => {
19+
expect(maxArea(testCase.input)).toBe(testCase.expected);
20+
});
21+
}
22+
});
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Description:
2+
// You are given an integer array height of length n. There are n vertical lines drawn such that the two endpoints of the ith line are (i, 0) and (i, height[i]).
3+
//
4+
// Find two lines that together with the x-axis form a container, such that the container contains the most water.
5+
//
6+
// Return the maximum amount of water a container can store.
7+
//
8+
// Notice that you may not slant the container.
9+
//
10+
// Constraints:
11+
// n == height.length
12+
// 2 <= n <= 105
13+
// 0 <= height[i] <= 104
14+
15+
export const maxArea = (height: number[]): number => {
16+
let lp = 0;
17+
let rp = height.length - 1;
18+
let area = 0;
19+
20+
for (let i = 0; i <= height.length; i++) {
21+
const delta = rp - lp;
22+
const min = Math.min(height[rp], height[lp]);
23+
const newArea = min * delta;
24+
25+
if (newArea > area) {
26+
area = newArea;
27+
}
28+
29+
height[rp] > height[lp] ? lp++ : rp--;
30+
}
31+
32+
return area;
33+
};

12. Integer to Roman/index.test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { intToRoman } from './index';
2+
3+
describe('intToRoman', () => {
4+
const testCases = [
5+
{
6+
name: 'Case 1',
7+
input: 3749,
8+
expected: 'MMMDCCXLIX',
9+
},
10+
{
11+
name: 'Case 2',
12+
input: 58,
13+
expected: 'LVIII',
14+
},
15+
{
16+
name: 'Case 3',
17+
input: 1994,
18+
expected: 'MCMXCIV',
19+
},
20+
];
21+
22+
for (const testCase of testCases) {
23+
test(testCase.name, () => {
24+
expect(intToRoman(testCase.input)).toBe(testCase.expected);
25+
});
26+
}
27+
});

12. Integer to Roman/index.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Description:
2+
// Seven different symbols represent Roman numerals with the following values:
3+
//
4+
// Symbol Value
5+
// I 1
6+
// V 5
7+
// X 10
8+
// L 50
9+
// C 100
10+
// D 500
11+
// M 1000
12+
// Roman numerals are formed by appending the conversions of decimal place values from highest to lowest. Converting a decimal place value into a Roman numeral has the following rules:
13+
// If the value does not start with 4 or 9, select the symbol of the maximal value that can be subtracted from the input,
14+
// append that symbol to the result, subtract its value, and convert the remainder to a Roman numeral.
15+
//
16+
// If the value starts with 4 or 9 use the subtractive form representing one symbol subtracted from the following symbol,
17+
// for example, 4 is 1 (I) less than 5 (V): IV and 9 is 1 (I) less than 10 (X): IX.
18+
// Only the following subtractive forms are used: 4 (IV), 9 (IX), 40 (XL), 90 (XC), 400 (CD) and 900 (CM).
19+
// Only powers of 10 (I, X, C, M) can be appended consecutively at most 3 times to represent multiples of 10.
20+
//
21+
// You cannot append 5 (V), 50 (L), or 500 (D) multiple times. If you need to append a symbol 4 times use the subtractive form.
22+
// Given an integer, convert it to a Roman numeral.
23+
//
24+
// Constraints:
25+
// 1 <= num <= 3999
26+
27+
export const intToRoman = (num: number): string => {
28+
const cs: string[] = [
29+
'M',
30+
'CM',
31+
'D',
32+
'CD',
33+
'C',
34+
'XC',
35+
'L',
36+
'XL',
37+
'X',
38+
'IX',
39+
'V',
40+
'IV',
41+
'I',
42+
];
43+
const vs: number[] = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1];
44+
const ans: string[] = [];
45+
for (let i = 0; i < vs.length; ++i) {
46+
while (num >= vs[i]) {
47+
num -= vs[i];
48+
ans.push(cs[i]);
49+
}
50+
}
51+
return ans.join('');
52+
};

0 commit comments

Comments
 (0)