Skip to content

Commit

Permalink
157th Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Shyam-Chen committed Jun 23, 2024
1 parent 996bc1d commit 6760527
Show file tree
Hide file tree
Showing 19 changed files with 95 additions and 66 deletions.
20 changes: 0 additions & 20 deletions src/page-17/1822. Sign of the Product of an Array/array-sign.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
import { arraySign, arraySign2 } from './array-sign';
import { arraySign } from './arraySign';

describe('1822. Sign of the Product of an Array', () => {
test('arraySign', () => {
expect(arraySign([-1, -2, -3, -4, 3, 2, 1])).toBe(1);
expect(arraySign([1, 5, 0, 2, -3])).toBe(0);
expect(arraySign([-1, 1, -1, 1, -1])).toBe(-1);
});

test('arraySign2', () => {
expect(arraySign2([-1, -2, -3, -4, 3, 2, 1])).toBe(1);
expect(arraySign2([1, 5, 0, 2, -3])).toBe(0);
expect(arraySign2([-1, 1, -1, 1, -1])).toBe(-1);
});
});
15 changes: 15 additions & 0 deletions src/page-17/1822. Sign of the Product of an Array/arraySign.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
type ArraySign = (nums: number[]) => number;

/**
* Accepted
*/
export const arraySign: ArraySign = (nums) => {
let result = 1;

for (const num of nums) {
if (num === 0) return 0;
if (num < 0) result *= -1;
}

return result;
};

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { minOperations } from './min-operations';
import { minOperations } from './minOperations';

describe('1827. Minimum Operations to Make the Array Increasing', () => {
test('minOperations', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
type MinOperations = (nums: number[]) => number;

/**
* Accepted
*/
export const minOperations: MinOperations = (nums) => {
let operations = 0;

for (let i = 1; i < nums.length; i++) {
if (nums[i] <= nums[i - 1]) {
const neededIncrement = nums[i - 1] - nums[i] + 1;
nums[i] += neededIncrement;
operations += neededIncrement;
}
}

return operations;
};
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { checkIfPangram } from './check-if-pangram';
import { checkIfPangram } from './checkIfPangram';

describe('1832. Check if the Sentence Is Pangram', () => {
test('checkIfPangram', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
type CheckIfPangram = (sentence: string) => boolean;

/**
* Accepted
*/
export const checkIfPangram: CheckIfPangram = (sentence) => {
return new Set([...sentence]).size === 26;
};
9 changes: 0 additions & 9 deletions src/page-17/1837. Sum of Digits in Base K/sum-base.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { sumBase } from './sum-base';
import { sumBase, sumBase2 } from './sumBase';

describe('1837. Sum of Digits in Base K', () => {
test('sumBase', () => {
expect(sumBase(34, 6)).toBe(9);
expect(sumBase(10, 10)).toBe(1);
});

test('sumBase2', () => {
expect(sumBase2(34, 6)).toBe(9);
expect(sumBase2(10, 10)).toBe(1);
});
});
28 changes: 28 additions & 0 deletions src/page-17/1837. Sum of Digits in Base K/sumBase.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
type SumBase = (n: number, k: number) => number;

/**
* Accepted
*/
export const sumBase: SumBase = (n, k) => {
return n
.toString(k)
.split('')
.map((val) => Number(val))
.reduce((acc, cur) => acc + cur);
};

/**
* Accepted
*/
export const sumBase2: SumBase = (n, k) => {
let sum = 0;
let current = n;

// Convert n to base k
while (current > 0) {
sum += current % k; // Add the current digit to the sum
current = Math.floor(current / k); // Update current by integer division
}

return sum;
};
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { replaceDigits } from './replace-digits';
import { replaceDigits } from './replaceDigits';

describe('1844. Replace All Digits with Characters', () => {
test('replaceDigits', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
type ReplaceDigits = (s: string) => string;

/**
* Accepted
*/
export const replaceDigits: ReplaceDigits = (s) => {
const result = Array.from(s);

// For every **odd** index `i`, you want to replace the digit `s[i]` with `shift(s[i-1], s[i])`.
for (let i = 1; i < result.length; i += 2) {
result[i] = String.fromCharCode(result[i - 1].charCodeAt(0) + Number(result[i]));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getMinDistance } from './get-min-distance';
import { getMinDistance } from './getMinDistance';

describe('1848. Minimum Distance to the Target Element', () => {
test('getMinDistance', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
type GetMinDistance = (nums: number[], target: number, start: number) => number;

/**
* Accepted
*/
export const getMinDistance: GetMinDistance = (nums, target, start) => {
let result = Number.POSITIVE_INFINITY;
let minDiff = Number.POSITIVE_INFINITY;

for (let i = 0; i < nums.length; i++) {
if (nums[i] === target) {
result = Math.min(result, Math.abs(i - start));
minDiff = Math.min(minDiff, Math.abs(i - start));
}
}

return result;
return minDiff;
};
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { maximumPopulation } from './maximum-population';
import { maximumPopulation } from './maximumPopulation';

describe('1854. Maximum Population Year', () => {
test('maximumPopulation', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
type MaximumPopulation = (logs: number[][]) => number;

/**
* Accepted
*/
export const maximumPopulation: MaximumPopulation = (logs) => {
const map = new Map();
const map = new Map<number, number>();

for (const log of logs) {
const [birth, death] = log;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { sortSentence } from './sort-sentence';
import { sortSentence } from './sortSentence';

describe('1859. Sorting the Sentence', () => {
test('sortSentence', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
type SortSentence = (s: string) => string;

/**
* Accepted
*/
export const sortSentence: SortSentence = (s) => {
return s
.split(' ')
.sort((a, b) => Number(a.charAt(a.length - 1)) - Number(b.charAt(b.length - 1)))
.map((word) => word.slice(0, word.length - 1))
.sort((a, b) => Number(a[a.length - 1]) - Number(b[b.length - 1]))
.map((word) => word.slice(0, -1))
.join(' ');
};

0 comments on commit 6760527

Please sign in to comment.