Skip to content

Commit 11db574

Browse files
committed
update
1 parent 4056fb3 commit 11db574

File tree

7 files changed

+164
-0
lines changed

7 files changed

+164
-0
lines changed

src/01-matrix.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
function updateMatrix(mat: number[][]): number[][] {
2+
const M = mat.length;
3+
const N = mat[0].length;
4+
const dp = Array.from({ length: M }, () =>
5+
Array.from({ length: N }, () => Number.MAX_SAFE_INTEGER)
6+
);
7+
8+
for (let i = 0; i < M; i++) {
9+
for (let j = 0; j < N; j++) {
10+
if (mat[i][j] === 0) {
11+
dp[i][j] = 0;
12+
}
13+
14+
if (i + 1 < M) dp[i + 1][j] = Math.min(dp[i][j] + 1, dp[i + 1][j]);
15+
if (j + 1 < N) dp[i][j + 1] = Math.min(dp[i][j] + 1, dp[i][j + 1]);
16+
}
17+
}
18+
19+
for (let i = M - 1; i >= 0; i--) {
20+
for (let j = N - 1; j >= 0; j--) {
21+
if (i - 1 >= 0) dp[i - 1][j] = Math.min(dp[i][j] + 1, dp[i - 1][j]);
22+
if (j - 1 >= 0) dp[i][j - 1] = Math.min(dp[i][j] + 1, dp[i][j - 1]);
23+
}
24+
}
25+
return dp;
26+
}

src/best-sightseeing-pair.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
function maxScoreSightseeingPair(A: number[]): number {
2+
const N = A.length;
3+
4+
// find the max A[i] + A[j] + i - j;
5+
// |----------> (A[i] + i) + (A[j] - j);
6+
7+
// i < j
8+
let ret = 0;
9+
let i = 0,
10+
j = 1;
11+
12+
while (j < N) {
13+
const localRes = A[i] + A[j] + i - j;
14+
ret = Math.max(ret, localRes);
15+
16+
if (A[i] + i < A[j] + j) i = j;
17+
// now we found the pos j is the best point pos[k]+k in current
18+
// updadte the i pos
19+
20+
j++;
21+
}
22+
return ret;
23+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function maxProfit(prices: number[]): number {
2+
const N = prices.length;
3+
// [7,1,5,3,6,4] --> [-6,4,-2,3,-2]
4+
let ret = 0;
5+
for (let i = 0; i < N - 1; i++) {
6+
const profit = prices[i + 1] - prices[i];
7+
if (profit > 0) ret += profit;
8+
}
9+
return ret;
10+
}

src/merge-sorted-array.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
Do not return anything, modify nums1 in-place instead.
3+
*/
4+
function merge(nums1: number[], m: number, nums2: number[], n: number): void {
5+
let pos1 = m - 1,
6+
pos2 = n - 1,
7+
pos3 = nums1.length - 1;
8+
9+
while (pos3 >= 0) {
10+
if (pos1 < 0) {
11+
nums1[pos3] = nums2[pos2];
12+
pos2--;
13+
pos3--;
14+
continue;
15+
}
16+
if (pos2 < 0) {
17+
nums1[pos3] = nums1[pos1];
18+
pos1--;
19+
pos3--;
20+
continue;
21+
}
22+
23+
if (nums1[pos1] > nums2[pos2]) {
24+
nums1[pos3] = nums1[pos1];
25+
pos1--;
26+
pos3--;
27+
} else {
28+
nums1[pos3] = nums2[pos2];
29+
pos2--;
30+
pos3--;
31+
}
32+
}
33+
}

src/multiply-strings.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
function multiply(num1: string, num2: string): string {
2+
if (num1 === "0" || num2 === "0") return "0";
3+
4+
const M = num1.length,
5+
N = num2.length;
6+
const ret = Array.from({ length: M + N }, () => 0);
7+
8+
const getNum = (digit: string): number =>
9+
digit.charCodeAt(0) - "0".charCodeAt(0);
10+
11+
for (let i = 0; i < M; i++) {
12+
for (let j = 0; j < N; j++) {
13+
const localRes = getNum(num1[i]) * getNum(num2[j]);
14+
const carry = Math.floor(localRes / 10);
15+
const digit = localRes % 10;
16+
ret[i + j] += carry;
17+
ret[i + j + 1] += digit;
18+
}
19+
}
20+
for (let i = M + N - 1; i > 0; i--) {
21+
const carry = Math.floor(ret[i] / 10);
22+
ret[i] %= 10;
23+
ret[i - 1] += carry;
24+
}
25+
if (ret.length !== 0 && ret[0] === 0) ret.shift();
26+
27+
return ret.join("");
28+
}

src/squares-of-a-sorted-array.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
function sortedSquares(nums: number[]): number[] {
2+
const N = nums.length;
3+
const positiveIdx = nums.findIndex((val) => val >= 0);
4+
5+
const negative = nums.slice(0, positiveIdx).reverse();
6+
const positive = nums.slice(positiveIdx);
7+
8+
const ret = [];
9+
10+
let pos1 = 0,
11+
pos2 = 0;
12+
for (let i = 0; i < N; i++) {
13+
if (pos2 === positive.length) {
14+
ret.push(negative[pos1] ** 2);
15+
pos1++;
16+
continue;
17+
}
18+
19+
if (pos1 === negative.length) {
20+
ret.push(positive[pos2] ** 2);
21+
pos2++;
22+
continue;
23+
}
24+
25+
if (Math.abs(negative[pos1]) < Math.abs(positive[pos2])) {
26+
ret.push(negative[pos1] ** 2);
27+
pos1++;
28+
} else {
29+
ret.push(positive[pos2] ** 2);
30+
pos2++;
31+
}
32+
}
33+
return ret;
34+
}

src/two-sum.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function twoSum(nums: number[], target: number): number[] {
2+
const N = nums.length;
3+
const map: Map<number, number> = new Map(nums.map((val, idx) => [val, idx])); // map array to entries
4+
5+
for (let i = 0; i < N; i++) {
6+
const t = target - nums[i];
7+
if (map.has(t) && map.get(t) !== i) return [i, map.get(t)];
8+
}
9+
return [];
10+
}

0 commit comments

Comments
 (0)