From 41ac42fd6d91971c55f9ca88321bafa487a04888 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Wed, 2 Apr 2025 07:47:42 +0300 Subject: [PATCH] Improved tasks 30-54 --- .../solution.ts | 44 +++++++++---------- .../g0001_0100/s0036_valid_sudoku/solution.ts | 24 +++++----- .../ts/g0001_0100/s0050_powx_n/solution.ts | 16 +++---- .../g0001_0100/s0052_n_queens_ii/solution.ts | 14 +++--- .../s0054_spiral_matrix/solution.ts | 27 ++++++------ .../solution.ts | 6 +-- .../solution.ts | 2 +- .../solution.test.ts | 4 +- .../solution.test.ts | 4 +- .../s0027_remove_element/solution.test.ts | 4 +- .../solution.test.ts | 6 +-- .../s0036_valid_sudoku/solution.test.ts | 44 +++++++++++-------- .../g0001_0100/s0050_powx_n/solution.test.ts | 6 +-- .../s0054_spiral_matrix/solution.test.ts | 16 ++++++- 14 files changed, 119 insertions(+), 98 deletions(-) diff --git a/src/main/ts/g0001_0100/s0030_substring_with_concatenation_of_all_words/solution.ts b/src/main/ts/g0001_0100/s0030_substring_with_concatenation_of_all_words/solution.ts index d62f490..2470100 100644 --- a/src/main/ts/g0001_0100/s0030_substring_with_concatenation_of_all_words/solution.ts +++ b/src/main/ts/g0001_0100/s0030_substring_with_concatenation_of_all_words/solution.ts @@ -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(); + let ans: number[] = [] + let n1 = words[0].length + let n2 = s.length + let map1 = new Map() 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(); + let left = i + let j = i + let c = 0 + let map2 = new Map() 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 } diff --git a/src/main/ts/g0001_0100/s0036_valid_sudoku/solution.ts b/src/main/ts/g0001_0100/s0036_valid_sudoku/solution.ts index 42a4760..315eee3 100644 --- a/src/main/ts/g0001_0100/s0036_valid_sudoku/solution.ts +++ b/src/main/ts/g0001_0100/s0036_valid_sudoku/solution.ts @@ -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 } diff --git a/src/main/ts/g0001_0100/s0050_powx_n/solution.ts b/src/main/ts/g0001_0100/s0050_powx_n/solution.ts index 66ab82b..ec47d5f 100644 --- a/src/main/ts/g0001_0100/s0050_powx_n/solution.ts +++ b/src/main/ts/g0001_0100/s0050_powx_n/solution.ts @@ -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 } diff --git a/src/main/ts/g0001_0100/s0052_n_queens_ii/solution.ts b/src/main/ts/g0001_0100/s0052_n_queens_ii/solution.ts index 0bf35b2..e0ff9b0 100644 --- a/src/main/ts/g0001_0100/s0052_n_queens_ii/solution.ts +++ b/src/main/ts/g0001_0100/s0052_n_queens_ii/solution.ts @@ -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 } diff --git a/src/main/ts/g0001_0100/s0054_spiral_matrix/solution.ts b/src/main/ts/g0001_0100/s0054_spiral_matrix/solution.ts index a1b56f0..3cfb163 100644 --- a/src/main/ts/g0001_0100/s0054_spiral_matrix/solution.ts +++ b/src/main/ts/g0001_0100/s0054_spiral_matrix/solution.ts @@ -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 } diff --git a/src/main/ts/g0101_0200/s0128_longest_consecutive_sequence/solution.ts b/src/main/ts/g0101_0200/s0128_longest_consecutive_sequence/solution.ts index 4519a5d..6a78603 100644 --- a/src/main/ts/g0101_0200/s0128_longest_consecutive_sequence/solution.ts +++ b/src/main/ts/g0101_0200/s0128_longest_consecutive_sequence/solution.ts @@ -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) diff --git a/src/main/ts/g0201_0300/s0215_kth_largest_element_in_an_array/solution.ts b/src/main/ts/g0201_0300/s0215_kth_largest_element_in_an_array/solution.ts index 87fa919..ab5f7da 100644 --- a/src/main/ts/g0201_0300/s0215_kth_largest_element_in_an_array/solution.ts +++ b/src/main/ts/g0201_0300/s0215_kth_largest_element_in_an_array/solution.ts @@ -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] diff --git a/src/test/ts/g0001_0100/s0014_longest_common_prefix/solution.test.ts b/src/test/ts/g0001_0100/s0014_longest_common_prefix/solution.test.ts index 6e12553..62b1934 100644 --- a/src/test/ts/g0001_0100/s0014_longest_common_prefix/solution.test.ts +++ b/src/test/ts/g0001_0100/s0014_longest_common_prefix/solution.test.ts @@ -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', () => { diff --git a/src/test/ts/g0001_0100/s0026_remove_duplicates_from_sorted_array/solution.test.ts b/src/test/ts/g0001_0100/s0026_remove_duplicates_from_sorted_array/solution.test.ts index 24c5a76..b8e0f40 100644 --- a/src/test/ts/g0001_0100/s0026_remove_duplicates_from_sorted_array/solution.test.ts +++ b/src/test/ts/g0001_0100/s0026_remove_duplicates_from_sorted_array/solution.test.ts @@ -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) }) diff --git a/src/test/ts/g0001_0100/s0027_remove_element/solution.test.ts b/src/test/ts/g0001_0100/s0027_remove_element/solution.test.ts index fdf08c6..08f06fd 100644 --- a/src/test/ts/g0001_0100/s0027_remove_element/solution.test.ts +++ b/src/test/ts/g0001_0100/s0027_remove_element/solution.test.ts @@ -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', () => { diff --git a/src/test/ts/g0001_0100/s0030_substring_with_concatenation_of_all_words/solution.test.ts b/src/test/ts/g0001_0100/s0030_substring_with_concatenation_of_all_words/solution.test.ts index 4c4fe7d..03206ff 100644 --- a/src/test/ts/g0001_0100/s0030_substring_with_concatenation_of_all_words/solution.test.ts +++ b/src/test/ts/g0001_0100/s0030_substring_with_concatenation_of_all_words/solution.test.ts @@ -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]) }) diff --git a/src/test/ts/g0001_0100/s0036_valid_sudoku/solution.test.ts b/src/test/ts/g0001_0100/s0036_valid_sudoku/solution.test.ts index db4ff72..36659a9 100644 --- a/src/test/ts/g0001_0100/s0036_valid_sudoku/solution.test.ts +++ b/src/test/ts/g0001_0100/s0036_valid_sudoku/solution.test.ts @@ -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) }) diff --git a/src/test/ts/g0001_0100/s0050_powx_n/solution.test.ts b/src/test/ts/g0001_0100/s0050_powx_n/solution.test.ts index 1279265..40375e2 100644 --- a/src/test/ts/g0001_0100/s0050_powx_n/solution.test.ts +++ b/src/test/ts/g0001_0100/s0050_powx_n/solution.test.ts @@ -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) }) diff --git a/src/test/ts/g0001_0100/s0054_spiral_matrix/solution.test.ts b/src/test/ts/g0001_0100/s0054_spiral_matrix/solution.test.ts index 0a5f01e..bb2901e 100644 --- a/src/test/ts/g0001_0100/s0054_spiral_matrix/solution.test.ts +++ b/src/test/ts/g0001_0100/s0054_spiral_matrix/solution.test.ts @@ -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]) })