Skip to content

Commit

Permalink
150th Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Shyam-Chen committed Jun 15, 2024
1 parent a84c006 commit 52d87e2
Show file tree
Hide file tree
Showing 26 changed files with 290 additions and 240 deletions.
14 changes: 0 additions & 14 deletions src/page-14/1480. Running Sum of 1d Array/running-sum.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { runningSum } from './running-sum';
import { runningSum } from './runningSum';

describe('1480. Running Sum of 1d Array', () => {
test('runningSum', () => {
Expand Down
19 changes: 19 additions & 0 deletions src/page-14/1480. Running Sum of 1d Array/runningSum.ts
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 src/page-15/1550. Three Consecutive Odds/three-consecutive-odds.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { threeConsecutiveOdds } from './three-consecutive-odds';
import { threeConsecutiveOdds } from './threeConsecutiveOdds';

describe('1550. Three Consecutive Odds', () => {
test('threeConsecutiveOdds', () => {
Expand Down
25 changes: 25 additions & 0 deletions src/page-15/1550. Three Consecutive Odds/threeConsecutiveOdds.ts
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 src/page-15/1556. Thousand Separator/thousand-separator.ts

This file was deleted.

32 changes: 32 additions & 0 deletions src/page-15/1556. Thousand Separator/thousandSeparator.bench.ts
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);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
48 changes: 48 additions & 0 deletions src/page-15/1556. Thousand Separator/thousandSeparator.ts
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));
};
18 changes: 0 additions & 18 deletions src/page-15/1572. Matrix Diagonal Sum/diagonal-sum.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { diagonalSum } from './diagonal-sum';
import { diagonalSum } from './diagonalSum';

describe('1572. Matrix Diagonal Sum', () => {
test('diagonalSum', () => {
Expand Down
24 changes: 24 additions & 0 deletions src/page-15/1572. Matrix Diagonal Sum/diagonalSum.ts
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;
};

This file was deleted.

This file was deleted.

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);
});
});
Loading

0 comments on commit 52d87e2

Please sign in to comment.