-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a84c006
commit 52d87e2
Showing
26 changed files
with
290 additions
and
240 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
2 changes: 1 addition & 1 deletion
2
...nning Sum of 1d Array/running-sum.test.ts → ...unning Sum of 1d Array/runningSum.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
}; |
18 changes: 0 additions & 18 deletions
18
src/page-15/1550. Three Consecutive Odds/three-consecutive-odds.ts
This file was deleted.
Oops, something went wrong.
2 changes: 1 addition & 1 deletion
2
...utive Odds/three-consecutive-odds.test.ts → ...ecutive Odds/threeConsecutiveOdds.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
src/page-15/1550. Three Consecutive Odds/threeConsecutiveOdds.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
}; |
39 changes: 0 additions & 39 deletions
39
src/page-15/1556. Thousand Separator/thousand-separator.ts
This file was deleted.
Oops, something went wrong.
32 changes: 32 additions & 0 deletions
32
src/page-15/1556. Thousand Separator/thousandSeparator.bench.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
}; |
This file was deleted.
Oops, something went wrong.
2 changes: 1 addition & 1 deletion
2
... Matrix Diagonal Sum/diagonal-sum.test.ts → .... Matrix Diagonal Sum/diagonalSum.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
}; |
40 changes: 0 additions & 40 deletions
40
src/page-15/1582. Special Positions in a Binary Matrix/num-special.test.ts
This file was deleted.
Oops, something went wrong.
30 changes: 0 additions & 30 deletions
30
src/page-15/1582. Special Positions in a Binary Matrix/num-special.ts
This file was deleted.
Oops, something went wrong.
21 changes: 21 additions & 0 deletions
21
src/page-15/1582. Special Positions in a Binary Matrix/numSpecial.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}); | ||
}); |
Oops, something went wrong.