Skip to content

Improved tasks 30-54 #100

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,47 @@
// #2025_04_01_Time_13_ms_(97.44%)_Space_63.70_MB_(46.03%)

function findSubstring(s: string, words: string[]): number[] {
let ans: number[] = [];
let n1 = words[0].length;
let n2 = s.length;
let map1 = new Map<string, number>();
let ans: number[] = []
let n1 = words[0].length
let n2 = s.length
let map1 = new Map<string, number>()

for (let ch of words) {
map1.set(ch, (map1.get(ch) ?? 0) + 1);
map1.set(ch, (map1.get(ch) ?? 0) + 1)
}

for (let i = 0; i < n1; i++) {
let left = i;
let j = i;
let c = 0;
let map2 = new Map<string, number>();
let left = i
let j = i
let c = 0
let map2 = new Map<string, number>()

while (j + n1 <= n2) {
let word1 = s.substring(j, j + n1);
j += n1;
let word1 = s.substring(j, j + n1)
j += n1

if (map1.has(word1)) {
map2.set(word1, (map2.get(word1) ?? 0) + 1);
c++;
map2.set(word1, (map2.get(word1) ?? 0) + 1)
c++

while ((map2.get(word1) ?? 0) > (map1.get(word1) ?? 0)) {
let word2 = s.substring(left, left + n1);
map2.set(word2, (map2.get(word2) ?? 0) - 1);
left += n1;
c--;
let word2 = s.substring(left, left + n1)
map2.set(word2, (map2.get(word2) ?? 0) - 1)
left += n1
c--
}

if (c === words.length) {
ans.push(left);
ans.push(left)
}
} else {
map2.clear();
c = 0;
left = j;
map2.clear()
c = 0
left = j
}
}
}
return ans;
return ans
}

export { findSubstring }
24 changes: 12 additions & 12 deletions src/main/ts/g0001_0100/s0036_valid_sudoku/solution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,28 @@
// #Top_Interview_150_Matrix #2025_04_01_Time_1_ms_(99.60%)_Space_58.94_MB_(52.85%)

function isValidSudoku(board: string[][]): boolean {
let rowSet: number[] = new Array(9).fill(0);
let colSet: number[] = new Array(9).fill(0);
let boxSet: number[] = new Array(9).fill(0);
let rowSet: number[] = new Array(9).fill(0)
let colSet: number[] = new Array(9).fill(0)
let boxSet: number[] = new Array(9).fill(0)

for (let i = 0; i < 9; i++) {
for (let j = 0; j < 9; j++) {
if (board[i][j] === '.') {
continue;
continue
}
let val = board[i][j].charCodeAt(0) - '0'.charCodeAt(0);
let boxIndex = Math.floor(i / 3) * 3 + Math.floor(j / 3);
let val = board[i][j].charCodeAt(0) - '0'.charCodeAt(0)
let boxIndex = Math.floor(i / 3) * 3 + Math.floor(j / 3)

if ((rowSet[i] & (1 << val)) || (colSet[j] & (1 << val)) || (boxSet[boxIndex] & (1 << val))) {
return false;
if (rowSet[i] & (1 << val) || colSet[j] & (1 << val) || boxSet[boxIndex] & (1 << val)) {
return false
}

rowSet[i] |= 1 << val;
colSet[j] |= 1 << val;
boxSet[boxIndex] |= 1 << val;
rowSet[i] |= 1 << val
colSet[j] |= 1 << val
boxSet[boxIndex] |= 1 << val
}
}
return true;
return true
}

export { isValidSudoku }
16 changes: 8 additions & 8 deletions src/main/ts/g0001_0100/s0050_powx_n/solution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@
// #2025_04_01_Time_0_ms_(100.00%)_Space_57.31_MB_(7.22%)

function myPow(x: number, n: number): number {
let nn = BigInt(n);
let res = 1.0;
let nn = BigInt(n)
let res = 1.0
if (n < 0) {
nn = -nn;
nn = -nn
}
while (nn > 0) {
if (nn % 2n === 1n) {
nn--;
res *= x;
nn--
res *= x
} else {
x *= x;
nn /= 2n;
x *= x
nn /= 2n
}
}
return n < 0 ? 1.0 / res : res;
return n < 0 ? 1.0 / res : res
}

export { myPow }
14 changes: 7 additions & 7 deletions src/main/ts/g0001_0100/s0052_n_queens_ii/solution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@
function totalNQueens(n: number): number {
function solve(r: number, cols: boolean[], diag: boolean[], antiDiag: boolean[]): number {
if (r === n) {
return 1;
return 1
}
let count = 0;
let count = 0
for (let c = 0; c < n; c++) {
if (!cols[c] && !diag[r + c] && !antiDiag[r - c + n - 1]) {
cols[c] = diag[r + c] = antiDiag[r - c + n - 1] = true;
count += solve(r + 1, cols, diag, antiDiag);
cols[c] = diag[r + c] = antiDiag[r - c + n - 1] = false;
cols[c] = diag[r + c] = antiDiag[r - c + n - 1] = true
count += solve(r + 1, cols, diag, antiDiag)
cols[c] = diag[r + c] = antiDiag[r - c + n - 1] = false
}
}
return count;
return count
}
return solve(0, new Array(n).fill(false), new Array(2 * n - 1).fill(false), new Array(2 * n - 1).fill(false));
return solve(0, new Array(n).fill(false), new Array(2 * n - 1).fill(false), new Array(2 * n - 1).fill(false))
}

export { totalNQueens }
27 changes: 14 additions & 13 deletions src/main/ts/g0001_0100/s0054_spiral_matrix/solution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,35 @@
// #Top_Interview_150_Matrix #2025_04_01_Time_0_ms_(100.00%)_Space_55.71_MB_(30.13%)

function spiralOrder(matrix: number[][]): number[] {
const result: number[] = [];
let r = 0, c = 0;
let bigR = matrix.length - 1;
let bigC = matrix[0].length - 1;
const result: number[] = []
let r = 0,
c = 0
let bigR = matrix.length - 1
let bigC = matrix[0].length - 1

while (r <= bigR && c <= bigC) {
for (let i = c; i <= bigC; i++) {
result.push(matrix[r][i]);
result.push(matrix[r][i])
}
r++;
r++

for (let i = r; i <= bigR; i++) {
result.push(matrix[i][bigC]);
result.push(matrix[i][bigC])
}
bigC--;
bigC--

for (let i = bigC; i >= c && r <= bigR; i--) {
result.push(matrix[bigR][i]);
result.push(matrix[bigR][i])
}
bigR--;
bigR--

for (let i = bigR; i >= r && c <= bigC; i--) {
result.push(matrix[i][c]);
result.push(matrix[i][c])
}
c++;
c++
}

return result;
return result
}

export { spiralOrder }
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ function longestConsecutive(nums: number[]): number {
let maxLen = 0
for (let num of sset) {
// check its start of the sequence
if (!sset.has(num-1)) {
let len = 0;
while (sset.has(num+len)) {
if (!sset.has(num - 1)) {
let len = 0
while (sset.has(num + len)) {
len += 1
}
maxLen = Math.max(maxLen, len)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ function findKthLargest(nums: number[], k: number): number {
const countingLen = 2e4 + 1
const counting = new Int32Array(countingLen)
for (const num of nums) {
counting[num + 1e4]++;
counting[num + 1e4]++
}
for (let i = countingLen - 1; i >= 0; i--) {
k -= counting[i]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import { longestCommonPrefix } from 'src/main/ts/g0001_0100/s0014_longest_common
import { expect, test } from 'vitest'

test('longestCommonPrefix', () => {
expect(longestCommonPrefix(['flower','flow','flight'])).toEqual('fl')
expect(longestCommonPrefix(['flower', 'flow', 'flight'])).toEqual('fl')
})

test('longestCommonPrefix2', () => {
expect(longestCommonPrefix(['dog','racecar','car'])).toEqual('')
expect(longestCommonPrefix(['dog', 'racecar', 'car'])).toEqual('')
})

test('longestCommonPrefix3', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { removeDuplicates } from 'src/main/ts/g0001_0100/s0026_remove_duplicates
import { expect, test } from 'vitest'

test('removeDuplicates', () => {
expect(removeDuplicates([1,1,2])).toEqual(2)
expect(removeDuplicates([1, 1, 2])).toEqual(2)
})

test('removeDuplicates2', () => {
expect(removeDuplicates([0,0,1,1,1,2,2,3,3,4])).toEqual(5)
expect(removeDuplicates([0, 0, 1, 1, 1, 2, 2, 3, 3, 4])).toEqual(5)
})
4 changes: 2 additions & 2 deletions src/test/ts/g0001_0100/s0027_remove_element/solution.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import { removeElement } from 'src/main/ts/g0001_0100/s0027_remove_element/solut
import { expect, test } from 'vitest'

test('removeElement', () => {
expect(removeElement([3,2,2,3], 3)).toEqual(2)
expect(removeElement([3, 2, 2, 3], 3)).toEqual(2)
})

test('removeElement2', () => {
expect(removeElement([0,1,2,2,3,0,4,2], 2)).toEqual(5)
expect(removeElement([0, 1, 2, 2, 3, 0, 4, 2], 2)).toEqual(5)
})

test('removeElement3', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import { findSubstring } from 'src/main/ts/g0001_0100/s0030_substring_with_conca
import { expect, test } from 'vitest'

test('findSubstring', () => {
expect(findSubstring('barfoothefoobarman', ['foo','bar'])).toEqual([0,9])
expect(findSubstring('barfoothefoobarman', ['foo', 'bar'])).toEqual([0, 9])
})

test('findSubstring2', () => {
expect(findSubstring('wordgoodgoodgoodbestword', ['word','good','best','word'])).toEqual([])
expect(findSubstring('wordgoodgoodgoodbestword', ['word', 'good', 'best', 'word'])).toEqual([])
})

test('findSubstring3', () => {
expect(findSubstring('barfoofoobarthefoobarman', ['bar','foo','the'])).toEqual([6,9,12])
expect(findSubstring('barfoofoobarthefoobarman', ['bar', 'foo', 'the'])).toEqual([6, 9, 12])
})
44 changes: 26 additions & 18 deletions src/test/ts/g0001_0100/s0036_valid_sudoku/solution.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,33 @@ import { isValidSudoku } from 'src/main/ts/g0001_0100/s0036_valid_sudoku/solutio
import { expect, test } from 'vitest'

test('isValidSudoku', () => {
expect(isValidSudoku([['5','3','.','.','7','.','.','.','.']
,['6','.','.','1','9','5','.','.','.']
,['.','9','8','.','.','.','.','6','.']
,['8','.','.','.','6','.','.','.','3']
,['4','.','.','8','.','3','.','.','1']
,['7','.','.','.','2','.','.','.','6']
,['.','6','.','.','.','.','2','8','.']
,['.','.','.','4','1','9','.','.','5']
,['.','.','.','.','8','.','.','7','9']])).toEqual(true)
expect(
isValidSudoku([
['5', '3', '.', '.', '7', '.', '.', '.', '.'],
['6', '.', '.', '1', '9', '5', '.', '.', '.'],
['.', '9', '8', '.', '.', '.', '.', '6', '.'],
['8', '.', '.', '.', '6', '.', '.', '.', '3'],
['4', '.', '.', '8', '.', '3', '.', '.', '1'],
['7', '.', '.', '.', '2', '.', '.', '.', '6'],
['.', '6', '.', '.', '.', '.', '2', '8', '.'],
['.', '.', '.', '4', '1', '9', '.', '.', '5'],
['.', '.', '.', '.', '8', '.', '.', '7', '9'],
]),
).toEqual(true)
})

test('isValidSudoku2', () => {
expect(isValidSudoku([['8','3','.','.','7','.','.','.','.']
,['6','.','.','1','9','5','.','.','.']
,['.','9','8','.','.','.','.','6','.']
,['8','.','.','.','6','.','.','.','3']
,['4','.','.','8','.','3','.','.','1']
,['7','.','.','.','2','.','.','.','6']
,['.','6','.','.','.','.','2','8','.']
,['.','.','.','4','1','9','.','.','5']
,['.','.','.','.','8','.','.','7','9']])).toEqual(false)
expect(
isValidSudoku([
['8', '3', '.', '.', '7', '.', '.', '.', '.'],
['6', '.', '.', '1', '9', '5', '.', '.', '.'],
['.', '9', '8', '.', '.', '.', '.', '6', '.'],
['8', '.', '.', '.', '6', '.', '.', '.', '3'],
['4', '.', '.', '8', '.', '3', '.', '.', '1'],
['7', '.', '.', '.', '2', '.', '.', '.', '6'],
['.', '6', '.', '.', '.', '.', '2', '8', '.'],
['.', '.', '.', '4', '1', '9', '.', '.', '5'],
['.', '.', '.', '.', '8', '.', '.', '7', '9'],
]),
).toEqual(false)
})
6 changes: 3 additions & 3 deletions src/test/ts/g0001_0100/s0050_powx_n/solution.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import { myPow } from 'src/main/ts/g0001_0100/s0050_powx_n/solution'
import { expect, test } from 'vitest'

test('myPow', () => {
expect(myPow(2.00000, 10)).toEqual(1024.00000)
expect(myPow(2.0, 10)).toEqual(1024.0)
})

test('myPow2', () => {
expect(myPow(2.10000, 3)).toEqual(9.261000000000001)
expect(myPow(2.1, 3)).toEqual(9.261000000000001)
})

test('myPow3', () => {
expect(myPow(2.00000, -2)).toEqual(0.25000)
expect(myPow(2.0, -2)).toEqual(0.25)
})
16 changes: 14 additions & 2 deletions src/test/ts/g0001_0100/s0054_spiral_matrix/solution.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,21 @@ import { spiralOrder } from 'src/main/ts/g0001_0100/s0054_spiral_matrix/solution
import { expect, test } from 'vitest'

test('spiralOrder', () => {
expect(spiralOrder([[1,2,3],[4,5,6],[7,8,9]])).toEqual([1,2,3,6,9,8,7,4,5])
expect(
spiralOrder([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
]),
).toEqual([1, 2, 3, 6, 9, 8, 7, 4, 5])
})

test('spiralOrder2', () => {
expect(spiralOrder([[1,2,3,4],[5,6,7,8],[9,10,11,12]])).toEqual([1,2,3,4,8,12,11,10,9,5,6,7])
expect(
spiralOrder([
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
]),
).toEqual([1, 2, 3, 4, 8, 12, 11, 10, 9, 5, 6, 7])
})