Skip to content

Commit

Permalink
151st Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Shyam-Chen committed Jun 16, 2024
1 parent 52d87e2 commit c0ac049
Show file tree
Hide file tree
Showing 13 changed files with 127 additions and 93 deletions.
24 changes: 11 additions & 13 deletions src/page-16/1652. Defuse the Bomb/decrypt.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
type Decrypt = (code: number[], k: number) => number[];

/**
* Accepted
*/
export const decrypt: Decrypt = (code, k) => {
const result = Array.from({ length: code.length }, () => 0);
const n = code.length;
const result = Array(n).fill(0);

if (k === 0) return result;

let sum = 0;

for (let i = 0; i < code.length; i++) {
for (let i = 0; i < n; i++) {
for (let j = 1; j <= Math.abs(k); j++) {
let p = 0;

if (k > 0) {
p = (i + j + code.length) % code.length;
}

if (k < 0) {
p = (i - j + code.length) % code.length;
}

sum += code[p];
let index = 0;
if (k > 0) index = (i + j) % n;
if (k < 0) index = (i - j + n) % n;
sum += code[index];
}

result[i] = sum;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { OrderedStream } from './ordered-stream';
import { OrderedStream } from './OrderedStream';

describe('1656. Design an Ordered Stream', () => {
test('OrderedStream', () => {
Expand All @@ -10,6 +10,6 @@ describe('1656. Design an Ordered Stream', () => {
expect(os.insert(5, 'eeeee')).toStrictEqual([]);
expect(os.insert(4, 'ddddd')).toStrictEqual(['ddddd', 'eeeee']);

expect(os.list).toStrictEqual(['aaaaa', 'bbbbb', 'ccccc', 'ddddd', 'eeeee']);
expect(os.stream).toStrictEqual(['aaaaa', 'bbbbb', 'ccccc', 'ddddd', 'eeeee']);
});
});
24 changes: 24 additions & 0 deletions src/page-16/1656. Design an Ordered Stream/OrderedStream.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* Accepted
*/
export class OrderedStream {
stream: string[];
ptr = 0;

constructor(n: number) {
this.stream = Array(n);
}

insert(idKey: number, value: string): string[] {
const chunk: string[] = [];

this.stream[idKey - 1] = value;

while (this.stream[this.ptr]) {
chunk.push(this.stream[this.ptr]);
this.ptr += 1;
}

return chunk;
}
}
22 changes: 0 additions & 22 deletions src/page-16/1656. Design an Ordered Stream/ordered-stream.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import { interpret } from './interpret';
import { interpret, interpret2 } from './interpret';

describe('1678. Goal Parser Interpretation', () => {
test('interpret', () => {
expect(interpret('G()(al)')).toBe('Goal');
expect(interpret('G()()()()(al)')).toBe('Gooooal');
expect(interpret('(al)G(al)()()G')).toBe('alGalooG');
});

test('interpret2', () => {
expect(interpret2('G()(al)')).toBe('Goal');
expect(interpret2('G()()()()(al)')).toBe('Gooooal');
expect(interpret2('(al)G(al)()()G')).toBe('alGalooG');
});
});
36 changes: 34 additions & 2 deletions src/page-16/1678. Goal Parser Interpretation/interpret.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,36 @@
type Interpret = (command: string) => string;

export const interpret: Interpret = (command) =>
command.replace(/\(\)/g, 'o').replace(/\(al\)/g, 'al');
/**
* Accepted
*/
export const interpret: Interpret = (command) => {
return command.replace(/\(\)/g, 'o').replace(/\(al\)/g, 'al');
};

/**
* Accepted
*/
export const interpret2: Interpret = (command) => {
let result = '';
let i = 0;

while (i < command.length) {
if (command[i] === 'G') {
result += 'G';
i += 1;
} else if (command[i] === '(' && command[i + 1] === ')') {
result += 'o';
i += 2;
} else if (
command[i] === '(' &&
command[i + 1] === 'a' &&
command[i + 2] === 'l' &&
command[i + 3] === ')'
) {
result += 'al';
i += 4;
}
}

return result;
};
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { sumOfUnique, sumOfUnique2 } from './sum-of-unique';
import { sumOfUnique, sumOfUnique2 } from './sumOfUnique';

describe('1748. Sum of Unique Elements', () => {
test('sumOfUnique', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
type SumOfUnique = (nums: number[]) => number;

/**
* Accepted
*/
export const sumOfUnique: SumOfUnique = (nums) => {
const arr = [];
const arr: number[] = [];

for (let i = 0; i < nums.length; i++) {
const num = nums[i];
Expand All @@ -14,22 +17,22 @@ export const sumOfUnique: SumOfUnique = (nums) => {
return arr.reduce((acc, cur) => acc + cur, 0);
};

/**
* Accepted
*/
export const sumOfUnique2: SumOfUnique = (nums) => {
const dict = new Map();
const map = new Map<number, number>();

for (let i = 0; i < nums.length; i++) {
const num = nums[i];
const count = dict.get(num) || 0;
dict.set(num, count + 1);
for (const num of nums) {
const count = map.get(num) || 0;
map.set(num, count + 1);
}

let result = 0;
let sum = 0;

dict.forEach((val, key) => {
if (val === 1) {
result += key;
}
});
for (const [key, value] of map) {
if (value === 1) sum += key;
}

return result;
return sum;
};
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,5 @@ describe('1752. Check if Array Is Sorted and Rotated', () => {
expect(check([3, 4, 5, 1, 2])).toBe(true);
expect(check([2, 1, 3, 4])).toBe(false);
expect(check([1, 2, 3])).toBe(true);
expect(check([1, 1, 1])).toBe(true);
expect(check([2, 1])).toBe(true);
});
});
13 changes: 10 additions & 3 deletions src/page-16/1752. Check if Array Is Sorted and Rotated/check.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
type Check = (nums: number[]) => boolean;

/**
* Accepted
*/
export const check: Check = (nums) => {
const n = nums.length;

let count = 0;

for (let i = 0; i < nums.length; i++) {
if (nums[i] > nums[(i + 1) % nums.length]) {
for (let i = 0; i < n; i++) {
if (nums[i] > nums[(i + 1) % n]) {
count += 1;
}

if (count > 1) return false;
}

return count <= 1;
return true;
};

This file was deleted.

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

describe('1758. Minimum Changes To Make Alternating Binary String', () => {
test('minOperations', () => {
expect(minOperations('0100')).toBe(1);
expect(minOperations('10')).toBe(0);
expect(minOperations('1111')).toBe(2);
});

test('minOperations2', () => {
expect(minOperations2('0100')).toBe(1);
expect(minOperations2('10')).toBe(0);
expect(minOperations2('1111')).toBe(2);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
type MinOperations = (s: string) => number;

/**
* Accepted
*/
export const minOperations: MinOperations = (s) => {
let countPattern1 = 0; // Count for pattern '010101...'
let countPattern2 = 0; // Count for pattern '101010...'

for (let i = 0; i < s.length; i++) {
// Character expected in pattern 1
const expectedCharPattern1 = i % 2 === 0 ? '0' : '1';
if (s[i] !== expectedCharPattern1) countPattern1 += 1;

// Character expected in pattern 2
const expectedCharPattern2 = i % 2 === 0 ? '1' : '0';
if (s[i] !== expectedCharPattern2) countPattern2 += 1;
}

return Math.min(countPattern1, countPattern2);
};

0 comments on commit c0ac049

Please sign in to comment.