diff --git a/src/page-14/1480. Running Sum of 1d Array/running-sum.ts b/src/page-14/1480. Running Sum of 1d Array/running-sum.ts deleted file mode 100644 index ea7d4df..0000000 --- a/src/page-14/1480. Running Sum of 1d Array/running-sum.ts +++ /dev/null @@ -1,14 +0,0 @@ -type RunningSum = (nums: number[]) => number[]; - -export const runningSum: RunningSum = (nums) => { - const result = []; - - let count = 0; - - for (let i = 0; i < nums.length; i++) { - count += nums[i]; - result.push(count); - } - - return result; -}; diff --git a/src/page-14/1480. Running Sum of 1d Array/running-sum.test.ts b/src/page-14/1480. Running Sum of 1d Array/runningSum.test.ts similarity index 87% rename from src/page-14/1480. Running Sum of 1d Array/running-sum.test.ts rename to src/page-14/1480. Running Sum of 1d Array/runningSum.test.ts index 58e4562..e735594 100644 --- a/src/page-14/1480. Running Sum of 1d Array/running-sum.test.ts +++ b/src/page-14/1480. Running Sum of 1d Array/runningSum.test.ts @@ -1,4 +1,4 @@ -import { runningSum } from './running-sum'; +import { runningSum } from './runningSum'; describe('1480. Running Sum of 1d Array', () => { test('runningSum', () => { diff --git a/src/page-14/1480. Running Sum of 1d Array/runningSum.ts b/src/page-14/1480. Running Sum of 1d Array/runningSum.ts new file mode 100644 index 0000000..2cf55bf --- /dev/null +++ b/src/page-14/1480. Running Sum of 1d Array/runningSum.ts @@ -0,0 +1,19 @@ +type RunningSum = (nums: number[]) => number[]; + +export const runningSum: RunningSum = (nums) => { + // Initialize an array to store the running sums + const result: number[] = []; + + let sum = 0; + + // Iterate through the nums array + for (let i = 0; i < nums.length; i++) { + // Add the current element to the running sum + sum += nums[i]; + + // Store the current running sum in the result array + result.push(sum); + } + + return result; +}; diff --git a/src/page-15/1550. Three Consecutive Odds/three-consecutive-odds.ts b/src/page-15/1550. Three Consecutive Odds/three-consecutive-odds.ts deleted file mode 100644 index 7b17e1d..0000000 --- a/src/page-15/1550. Three Consecutive Odds/three-consecutive-odds.ts +++ /dev/null @@ -1,18 +0,0 @@ -type ThreeConsecutiveOdds = (arr: number[]) => boolean; - -export const threeConsecutiveOdds: ThreeConsecutiveOdds = (arr) => { - let count = 0; - - for (let index = 0; index < arr.length; index++) { - const num = arr[index]; - - if (num % 2 === 1) { - count += 1; - if (count === 3) return true; - } else { - count = 0; - } - } - - return false; -}; diff --git a/src/page-15/1550. Three Consecutive Odds/three-consecutive-odds.test.ts b/src/page-15/1550. Three Consecutive Odds/threeConsecutiveOdds.test.ts similarity index 78% rename from src/page-15/1550. Three Consecutive Odds/three-consecutive-odds.test.ts rename to src/page-15/1550. Three Consecutive Odds/threeConsecutiveOdds.test.ts index ae060c3..cb23633 100644 --- a/src/page-15/1550. Three Consecutive Odds/three-consecutive-odds.test.ts +++ b/src/page-15/1550. Three Consecutive Odds/threeConsecutiveOdds.test.ts @@ -1,4 +1,4 @@ -import { threeConsecutiveOdds } from './three-consecutive-odds'; +import { threeConsecutiveOdds } from './threeConsecutiveOdds'; describe('1550. Three Consecutive Odds', () => { test('threeConsecutiveOdds', () => { diff --git a/src/page-15/1550. Three Consecutive Odds/threeConsecutiveOdds.ts b/src/page-15/1550. Three Consecutive Odds/threeConsecutiveOdds.ts new file mode 100644 index 0000000..3c876ff --- /dev/null +++ b/src/page-15/1550. Three Consecutive Odds/threeConsecutiveOdds.ts @@ -0,0 +1,25 @@ +type ThreeConsecutiveOdds = (arr: number[]) => boolean; + +/** + * Accepted + */ +export const threeConsecutiveOdds: ThreeConsecutiveOdds = (arr) => { + // Counter for consecutive odd numbers + let count = 0; + + for (let i = 0; i < arr.length; i++) { + if (arr[i] % 2 === 1) { + // If current number is odd, increase the count + count += 1; + + // Check if we have found three consecutive odds + if (count === 3) return true; + } else { + // If current number is even, reset the count + count = 0; + } + } + + // If we finish the loop without finding three consecutive odds + return false; +}; diff --git a/src/page-15/1556. Thousand Separator/thousand-separator.ts b/src/page-15/1556. Thousand Separator/thousand-separator.ts deleted file mode 100644 index cdc9268..0000000 --- a/src/page-15/1556. Thousand Separator/thousand-separator.ts +++ /dev/null @@ -1,39 +0,0 @@ -type ThousandSeparator = (n: number) => string; - -// Positive Lookahead (?=(\d{3})+(?!\d)) -// - 1st Capturing Group (\d{3})+ -// - Negative Lookahead (?!\d) -export const thousandSeparator: ThousandSeparator = (n) => - n.toString().replace(/\B(?=(\d{3})+(?!\d))/g, '.'); - -export const thousandSeparator2: ThousandSeparator = (n) => n.toLocaleString().replace(/,/g, '.'); - -export const thousandSeparator3: ThousandSeparator = (n) => { - const arr = String(n).split('').reverse(); - const result = []; - - let count = 0; - - for (let i = 0; i < arr.length; i++) { - count += 1; - - if (count % 3 === 0) { - if (count === arr.length) { - result.push(arr[i]); - } else { - result.push(arr[i], '.'); - } - } else { - result.push(arr[i]); - } - } - - return result.reverse().join(''); -}; - -export const thousandSeparator4: ThousandSeparator = (n) => { - return String(n) - .split('') - .reverse() - .reduce((acc, cur) => (acc.length % 4 === 3 ? `${cur}.${acc}` : cur + acc)); -}; diff --git a/src/page-15/1556. Thousand Separator/thousandSeparator.bench.ts b/src/page-15/1556. Thousand Separator/thousandSeparator.bench.ts new file mode 100644 index 0000000..f5869d9 --- /dev/null +++ b/src/page-15/1556. Thousand Separator/thousandSeparator.bench.ts @@ -0,0 +1,32 @@ +import { bench } from 'vitest'; + +import { + thousandSeparator, + thousandSeparator2, + thousandSeparator3, + thousandSeparator4, +} from './thousandSeparator'; + +describe('1556. Thousand Separator', () => { + bench('thousandSeparator', () => { + thousandSeparator(987); + thousandSeparator(1234); + }); + + // slowest + bench('thousandSeparator2', () => { + thousandSeparator2(987); + thousandSeparator2(1234); + }); + + // fastest + bench('thousandSeparator3', () => { + thousandSeparator3(987); + thousandSeparator3(1234); + }); + + bench('thousandSeparator4', () => { + thousandSeparator4(987); + thousandSeparator4(1234); + }); +}); diff --git a/src/page-15/1556. Thousand Separator/thousand-separator.test.ts b/src/page-15/1556. Thousand Separator/thousandSeparator.test.ts similarity index 60% rename from src/page-15/1556. Thousand Separator/thousand-separator.test.ts rename to src/page-15/1556. Thousand Separator/thousandSeparator.test.ts index c3256db..ed68d84 100644 --- a/src/page-15/1556. Thousand Separator/thousand-separator.test.ts +++ b/src/page-15/1556. Thousand Separator/thousandSeparator.test.ts @@ -3,34 +3,26 @@ import { thousandSeparator2, thousandSeparator3, thousandSeparator4, -} from './thousand-separator'; +} from './thousandSeparator'; describe('1556. Thousand Separator', () => { test('thousandSeparator', () => { expect(thousandSeparator(987)).toBe('987'); expect(thousandSeparator(1234)).toBe('1.234'); - expect(thousandSeparator(123456789)).toBe('123.456.789'); - expect(thousandSeparator(987)).toBe('987'); }); test('thousandSeparator2', () => { expect(thousandSeparator2(987)).toBe('987'); expect(thousandSeparator2(1234)).toBe('1.234'); - expect(thousandSeparator2(123456789)).toBe('123.456.789'); - expect(thousandSeparator2(987)).toBe('987'); }); test('thousandSeparator3', () => { expect(thousandSeparator3(987)).toBe('987'); expect(thousandSeparator3(1234)).toBe('1.234'); - expect(thousandSeparator3(123456789)).toBe('123.456.789'); - expect(thousandSeparator3(987)).toBe('987'); }); test('thousandSeparator4', () => { expect(thousandSeparator4(987)).toBe('987'); expect(thousandSeparator4(1234)).toBe('1.234'); - expect(thousandSeparator4(123456789)).toBe('123.456.789'); - expect(thousandSeparator4(987)).toBe('987'); }); }); diff --git a/src/page-15/1556. Thousand Separator/thousandSeparator.ts b/src/page-15/1556. Thousand Separator/thousandSeparator.ts new file mode 100644 index 0000000..38cd76a --- /dev/null +++ b/src/page-15/1556. Thousand Separator/thousandSeparator.ts @@ -0,0 +1,48 @@ +type ThousandSeparator = (n: number) => string; + +/** + * Accepted + */ +export const thousandSeparator: ThousandSeparator = (n) => { + return n.toString().replace(/\B(?=(\d{3})+(?!\d))/g, '.'); +}; + +/** + * Accepted + */ +export const thousandSeparator2: ThousandSeparator = (n) => { + return n.toLocaleString().replace(/,/g, '.'); +}; + +/** + * Accepted + */ +export const thousandSeparator3: ThousandSeparator = (n) => { + const str = n.toString(); + + let result = ''; + + // Iterate backwards through the string + for (let i = str.length - 1, count = 0; i >= 0; i--) { + result = str[i] + result; // Build the result string from right to left + count += 1; + + // Insert dot after every three digits, unless it's the last digit + if (count % 3 === 0 && i !== 0) { + result = `.${result}`; // Insert dot + } + } + + return result; +}; + +/** + * Accepted + */ +export const thousandSeparator4: ThousandSeparator = (n) => { + return n + .toString() + .split('') + .reverse() + .reduce((acc, cur) => (acc.length % 4 === 3 ? `${cur}.${acc}` : cur + acc)); +}; diff --git a/src/page-15/1572. Matrix Diagonal Sum/diagonal-sum.ts b/src/page-15/1572. Matrix Diagonal Sum/diagonal-sum.ts deleted file mode 100644 index d6c5edb..0000000 --- a/src/page-15/1572. Matrix Diagonal Sum/diagonal-sum.ts +++ /dev/null @@ -1,18 +0,0 @@ -type DiagonalSum = (mat: number[][]) => number; - -export const diagonalSum: DiagonalSum = (mat) => { - let result = 0; - - for (let i = 0; i < mat.length; i++) { - // primary diagonal (從左上到右下) + secondary diagonal (從右上到左下) - result += mat[i][i] + mat[i][mat.length - i - 1]; - } - - // 如果矩陣是單數 - if (mat.length % 2 === 1) { - // 減去重複計算的中心點的數值 - result -= mat[(mat.length - 1) / 2][(mat.length - 1) / 2]; - } - - return result; -}; diff --git a/src/page-15/1572. Matrix Diagonal Sum/diagonal-sum.test.ts b/src/page-15/1572. Matrix Diagonal Sum/diagonalSum.test.ts similarity index 89% rename from src/page-15/1572. Matrix Diagonal Sum/diagonal-sum.test.ts rename to src/page-15/1572. Matrix Diagonal Sum/diagonalSum.test.ts index 6cd0721..5f48326 100644 --- a/src/page-15/1572. Matrix Diagonal Sum/diagonal-sum.test.ts +++ b/src/page-15/1572. Matrix Diagonal Sum/diagonalSum.test.ts @@ -1,4 +1,4 @@ -import { diagonalSum } from './diagonal-sum'; +import { diagonalSum } from './diagonalSum'; describe('1572. Matrix Diagonal Sum', () => { test('diagonalSum', () => { diff --git a/src/page-15/1572. Matrix Diagonal Sum/diagonalSum.ts b/src/page-15/1572. Matrix Diagonal Sum/diagonalSum.ts new file mode 100644 index 0000000..a79df27 --- /dev/null +++ b/src/page-15/1572. Matrix Diagonal Sum/diagonalSum.ts @@ -0,0 +1,24 @@ +type DiagonalSum = (mat: number[][]) => number; + +/** + * Accepted + */ +export const diagonalSum: DiagonalSum = (mat) => { + const n = mat.length; + + let sum = 0; + + // Iterate over each row of the matrix + for (let i = 0; i < n; i++) { + // Primary diagonal + Secondary diagonal + sum += mat[i][i] + mat[i][n - i - 1]; + } + + // Adjust sum if matrix length is odd + if (n % 2 === 1) { + const mid = (n - 1) / 2; + sum -= mat[mid][mid]; // Subtract the middle element counted twice + } + + return sum; +}; diff --git a/src/page-15/1582. Special Positions in a Binary Matrix/num-special.test.ts b/src/page-15/1582. Special Positions in a Binary Matrix/num-special.test.ts deleted file mode 100644 index 4671488..0000000 --- a/src/page-15/1582. Special Positions in a Binary Matrix/num-special.test.ts +++ /dev/null @@ -1,40 +0,0 @@ -import { numSpecial } from './num-special'; - -describe('1582. Special Positions in a Binary Matrix', () => { - test('numSpecial', () => { - expect( - numSpecial([ - [1, 0, 0], - [0, 0, 1], - [1, 0, 0], - ]), - ).toBe(1); - - expect( - numSpecial([ - [1, 0, 0], - [0, 1, 0], - [0, 0, 1], - ]), - ).toBe(3); - - expect( - numSpecial([ - [0, 0, 0, 1], - [1, 0, 0, 0], - [0, 1, 1, 0], - [0, 0, 0, 0], - ]), - ).toBe(2); - - expect( - numSpecial([ - [0, 0, 0, 0, 0], - [1, 0, 0, 0, 0], - [0, 1, 0, 0, 0], - [0, 0, 1, 0, 0], - [0, 0, 0, 1, 1], - ]), - ).toBe(3); - }); -}); diff --git a/src/page-15/1582. Special Positions in a Binary Matrix/num-special.ts b/src/page-15/1582. Special Positions in a Binary Matrix/num-special.ts deleted file mode 100644 index 423479c..0000000 --- a/src/page-15/1582. Special Positions in a Binary Matrix/num-special.ts +++ /dev/null @@ -1,30 +0,0 @@ -type NumSpecial = (mat: number[][]) => number; - -export const numSpecial: NumSpecial = (mat) => { - const rows = mat.length; - const cols = mat[0].length; - - const row = Array.from({ length: rows }).fill(0) as number[]; - const col = Array.from({ length: cols }).fill(0) as number[]; - - for (let i = 0; i < rows; i++) { - for (let j = 0; j < cols; j++) { - if (mat[i][j] === 1) { - row[i] += 1; - col[j] += 1; - } - } - } - - let count = 0; - - for (let i = 0; i < rows; i++) { - for (let j = 0; j < cols; j++) { - if (mat[i][j] === 1 && row[i] === 1 && col[j] === 1) { - count += 1; - } - } - } - - return count; -}; diff --git a/src/page-15/1582. Special Positions in a Binary Matrix/numSpecial.test.ts b/src/page-15/1582. Special Positions in a Binary Matrix/numSpecial.test.ts new file mode 100644 index 0000000..a2e6229 --- /dev/null +++ b/src/page-15/1582. Special Positions in a Binary Matrix/numSpecial.test.ts @@ -0,0 +1,21 @@ +import { numSpecial } from './numSpecial'; + +describe('1582. Special Positions in a Binary Matrix', () => { + test('numSpecial', () => { + expect( + numSpecial([ + [1, 0, 0], + [0, 0, 1], + [1, 0, 0], + ]), + ).toBe(1); + + expect( + numSpecial([ + [1, 0, 0], + [0, 1, 0], + [0, 0, 1], + ]), + ).toBe(3); + }); +}); diff --git a/src/page-15/1582. Special Positions in a Binary Matrix/numSpecial.ts b/src/page-15/1582. Special Positions in a Binary Matrix/numSpecial.ts new file mode 100644 index 0000000..56176ed --- /dev/null +++ b/src/page-15/1582. Special Positions in a Binary Matrix/numSpecial.ts @@ -0,0 +1,34 @@ +type NumSpecial = (mat: number[][]) => number; + +/** + * Accepted + */ +export const numSpecial: NumSpecial = (mat) => { + const m = mat.length; + const n = mat[0].length; + const rows = Array(m).fill(0); + const cols = Array(n).fill(0); + + // Count number of 1s in each row and each column + for (let i = 0; i < m; i++) { + for (let j = 0; j < n; j++) { + if (mat[i][j] === 1) { + rows[i] += 1; + cols[j] += 1; + } + } + } + + let specialCount = 0; + + // Check for special positions + for (let i = 0; i < m; i++) { + for (let j = 0; j < n; j++) { + if (mat[i][j] === 1 && rows[i] === 1 && cols[j] === 1) { + specialCount += 1; + } + } + } + + return specialCount; +}; diff --git a/src/page-15/1588. Sum of All Odd Length Subarrays/sum-odd-length-subarrays.ts b/src/page-15/1588. Sum of All Odd Length Subarrays/sum-odd-length-subarrays.ts deleted file mode 100644 index 4a004f2..0000000 --- a/src/page-15/1588. Sum of All Odd Length Subarrays/sum-odd-length-subarrays.ts +++ /dev/null @@ -1,24 +0,0 @@ -type SumOddLengthSubarrays = (arr: number[]) => number; - -// 1 4 2 5 3 - 1 5 7 12 15 - 1 7 15 -// 4 2 5 3 - 4 6 11 14 - 4 11 -// 2 5 3 - 2 7 10 - 2 10 -// 5 3 - 5 8 - 5 -// 3 - 3 - 3 -export const sumOddLengthSubarrays: SumOddLengthSubarrays = (arr) => { - let result = 0; - - for (let i = 0, sum = 0; i < arr.length; i += 1, sum = 0) { - for (let j = i; j < arr.length; j++) { - // accumulator - sum += arr[j]; - - // odd length - if ((j - i + 1) % 2 === 1) { - result += sum; - } - } - } - - return result; -}; diff --git a/src/page-15/1588. Sum of All Odd Length Subarrays/sum-odd-length-subarrays.test.ts b/src/page-15/1588. Sum of All Odd Length Subarrays/sumOddLengthSubarrays.test.ts similarity index 80% rename from src/page-15/1588. Sum of All Odd Length Subarrays/sum-odd-length-subarrays.test.ts rename to src/page-15/1588. Sum of All Odd Length Subarrays/sumOddLengthSubarrays.test.ts index b1a3509..5aacfb8 100644 --- a/src/page-15/1588. Sum of All Odd Length Subarrays/sum-odd-length-subarrays.test.ts +++ b/src/page-15/1588. Sum of All Odd Length Subarrays/sumOddLengthSubarrays.test.ts @@ -1,4 +1,4 @@ -import { sumOddLengthSubarrays } from './sum-odd-length-subarrays'; +import { sumOddLengthSubarrays } from './sumOddLengthSubarrays'; describe('1588. Sum of All Odd Length Subarrays', () => { test('sumOddLengthSubarrays', () => { diff --git a/src/page-15/1588. Sum of All Odd Length Subarrays/sumOddLengthSubarrays.ts b/src/page-15/1588. Sum of All Odd Length Subarrays/sumOddLengthSubarrays.ts new file mode 100644 index 0000000..df7f450 --- /dev/null +++ b/src/page-15/1588. Sum of All Odd Length Subarrays/sumOddLengthSubarrays.ts @@ -0,0 +1,25 @@ +type SumOddLengthSubarrays = (arr: number[]) => number; + +/** + * Accepted + */ +export const sumOddLengthSubarrays: SumOddLengthSubarrays = (arr) => { + const n = arr.length; + + let sum = 0; + + for (let i = 0; i < n; i++) { + // Number of subarrays that include arr[i] + const leftCount = i + 1; + const rightCount = n - i; + const totalSubarrays = leftCount * rightCount; + + // Odd length subarrays are those where length is 1, 3, 5, ..., up to min(leftCount, rightCount) + const oddSubarraysCount = Math.floor(totalSubarrays / 2) + (totalSubarrays % 2); + + // Sum of elements in odd length subarrays that include arr[i] + sum += oddSubarraysCount * arr[i]; + } + + return sum; +}; diff --git a/src/page-15/1592. Rearrange Spaces Between Words/reorder-spaces.ts b/src/page-15/1592. Rearrange Spaces Between Words/reorder-spaces.ts deleted file mode 100644 index 61bfaf3..0000000 --- a/src/page-15/1592. Rearrange Spaces Between Words/reorder-spaces.ts +++ /dev/null @@ -1,23 +0,0 @@ -type ReorderSpaces = (text: string) => string; - -export const reorderSpaces: ReorderSpaces = (text) => { - if (!/\s/g.test(text)) return text; - - const wordLength = text.split(' ').filter(Boolean).length; - const spaceLength = text.split(' ').length - 1; - - const words = text.split(' ').filter(Boolean); - const spaces = Array.from({ length: spaceLength }, () => ' '); - - const divideLength = Math.floor(spaceLength / (wordLength - 1)); - - const dividedspaces = Array.from(Array(Math.ceil(spaces.length / divideLength)), (v, i) => - spaces.slice(i * divideLength, i * divideLength + divideLength), - ).map((i) => i.join('')); - - for (let j = 0; j < dividedspaces.length; j++) { - words.splice(j * 2 + 1, 0, dividedspaces[j]); - } - - return words.join(''); -}; diff --git a/src/page-15/1592. Rearrange Spaces Between Words/reorder-spaces.test.ts b/src/page-15/1592. Rearrange Spaces Between Words/reorderSpaces.test.ts similarity index 50% rename from src/page-15/1592. Rearrange Spaces Between Words/reorder-spaces.test.ts rename to src/page-15/1592. Rearrange Spaces Between Words/reorderSpaces.test.ts index 06251d4..b5befda 100644 --- a/src/page-15/1592. Rearrange Spaces Between Words/reorder-spaces.test.ts +++ b/src/page-15/1592. Rearrange Spaces Between Words/reorderSpaces.test.ts @@ -1,13 +1,8 @@ -import { reorderSpaces } from './reorder-spaces'; +import { reorderSpaces } from './reorderSpaces'; describe('1592. Rearrange Spaces Between Words', () => { test('reorderSpaces', () => { expect(reorderSpaces(' this is a sentence ')).toBe('this is a sentence'); expect(reorderSpaces(' practice makes perfect')).toBe('practice makes perfect '); - expect(reorderSpaces('hello world')).toBe('hello world'); - expect(reorderSpaces(' walks udp package into bar a')).toBe( - 'walks udp package into bar a ', - ); - expect(reorderSpaces('a')).toBe('a'); }); }); diff --git a/src/page-15/1592. Rearrange Spaces Between Words/reorderSpaces.ts b/src/page-15/1592. Rearrange Spaces Between Words/reorderSpaces.ts new file mode 100644 index 0000000..02281aa --- /dev/null +++ b/src/page-15/1592. Rearrange Spaces Between Words/reorderSpaces.ts @@ -0,0 +1,27 @@ +type ReorderSpaces = (text: string) => string; + +/** + * Accepted + */ +export const reorderSpaces: ReorderSpaces = (text) => { + // Trim leading and trailing spaces, then split the text into words based on one or more spaces + const words = text.trim().split(/\s+/); + + // Count the total number of spaces in the original text + const totalSpaces = text.split('').filter((char) => char === ' ').length; + + // Get the number of words + const numWords = words.length; + + // If there is only one word, return the word followed by all the spaces + if (numWords === 1) return words[0] + ' '.repeat(totalSpaces); + + // Calculate the number of spaces to put between each pair of words + const spaceBetween = Math.floor(totalSpaces / (numWords - 1)); + + // Calculate the remaining spaces that can't be evenly distributed + const extraSpaces = totalSpaces % (numWords - 1); + + // Join the words with the calculated spaces between them and add any extra spaces at the end + return words.join(' '.repeat(spaceBetween)) + ' '.repeat(extraSpaces); +}; diff --git a/src/page-15/1598. Crawler Log Folder/minOperations.bench.ts b/src/page-15/1598. Crawler Log Folder/minOperations.bench.ts new file mode 100644 index 0000000..eb93ca8 --- /dev/null +++ b/src/page-15/1598. Crawler Log Folder/minOperations.bench.ts @@ -0,0 +1,18 @@ +import { bench } from 'vitest'; + +import { minOperations, minOperations2 } from './minOperations'; + +describe('1598. Crawler Log Folder', () => { + bench('minOperations', () => { + minOperations(['d1/', 'd2/', '../', 'd21/', './']); + minOperations(['d1/', 'd2/', './', 'd3/', '../', 'd31/']); + minOperations(['d1/', '../', '../', '../']); + }); + + // fastest + bench('minOperations2', () => { + minOperations2(['d1/', 'd2/', '../', 'd21/', './']); + minOperations2(['d1/', 'd2/', './', 'd3/', '../', 'd31/']); + minOperations2(['d1/', '../', '../', '../']); + }); +}); diff --git a/src/page-15/1598. Crawler Log Folder/min-operations.test.ts b/src/page-15/1598. Crawler Log Folder/minOperations.test.ts similarity index 89% rename from src/page-15/1598. Crawler Log Folder/min-operations.test.ts rename to src/page-15/1598. Crawler Log Folder/minOperations.test.ts index 02c34eb..7f6af38 100644 --- a/src/page-15/1598. Crawler Log Folder/min-operations.test.ts +++ b/src/page-15/1598. Crawler Log Folder/minOperations.test.ts @@ -1,4 +1,4 @@ -import { minOperations, minOperations2 } from './min-operations'; +import { minOperations, minOperations2 } from './minOperations'; describe('1598. Crawler Log Folder', () => { test('minOperations', () => { diff --git a/src/page-15/1598. Crawler Log Folder/min-operations.ts b/src/page-15/1598. Crawler Log Folder/minOperations.ts similarity index 55% rename from src/page-15/1598. Crawler Log Folder/min-operations.ts rename to src/page-15/1598. Crawler Log Folder/minOperations.ts index 315d0ce..358acad 100644 --- a/src/page-15/1598. Crawler Log Folder/min-operations.ts +++ b/src/page-15/1598. Crawler Log Folder/minOperations.ts @@ -1,7 +1,10 @@ type MinOperations = (logs: string[]) => number; +/** + * Accepted + */ export const minOperations: MinOperations = (logs) => { - const stack = [] as string[]; + const stack: string[] = []; for (const log of logs) { if (log === '../') { @@ -14,26 +17,19 @@ export const minOperations: MinOperations = (logs) => { return stack.length; }; +/** + * Accepted + */ export const minOperations2: MinOperations = (logs) => { let depth = 0; - for (let i = 0; i < logs.length; i++) { - if (logs[i] === '../') { + for (const log of logs) { + if (log === '../') { if (depth > 0) depth -= 1; - } else if (logs[i] !== './') { + } else if (log !== './') { depth += 1; } } return depth; - - // reducer - // return logs.reduce((acc, cur) => { - // if (cur === '../') { - // if (acc > 0) acc -= 1; - // } else if (cur !== './') { - // acc += 1; - // } - // return acc; - // }, 0); };