Skip to content

Commit

Permalink
feat(solutions): add leetcode 54
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabb-c committed Apr 13, 2024
1 parent 09c744e commit 7260957
Show file tree
Hide file tree
Showing 8 changed files with 226 additions and 23 deletions.
22 changes: 13 additions & 9 deletions src/arrays/1-two-sum/tests/index.bench.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import { bench, describe, expect } from "vitest";
import { bench, describe } from "vitest";

import type { InputTestData } from "@typings/input-data";
import { twoSum } from "..";
import { TEST_DATA } from "./test-data";

const TEST_DATA: InputTestData<{ nums: number[]; target: number }, number[]> = {
input: { nums: [2, 7, 11, 15], target: 9 },
expected: [0, 1],
};

describe("Two Sum (benchmark)", () => {
for (const {
const {
input: { nums, target },
expected,
} of TEST_DATA) {
bench(`should find the indices of two numbers that add up to ${target}`, () => {
expect(twoSum(nums, target)).toEqual(expected);
});
}
} = TEST_DATA;

bench(`should find the indices of two numbers that add up to ${target}`, () => {
twoSum(nums, target);
});
});
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { bench, describe, expect } from "vitest";
import { bench, describe } from "vitest";

import type { InputTestData } from "@typings/input-data";
import { maxProfit } from "..";
import { TEST_DATA } from "./test-data";

const TEST_DATA: InputTestData<number[], number> = { input: [7, 1, 5, 3, 6, 4], expected: 5 };

describe("Best Time to Buy and Sell Stock (benchmark)", () => {
for (const { input, expected } of TEST_DATA) {
bench("should find the maximum profit", () => {
expect(maxProfit(input)).toEqual(expected);
});
}
const { input } = TEST_DATA;
bench("should find the maximum profit", () => {
maxProfit(input);
});
});
18 changes: 11 additions & 7 deletions src/arrays/238-product-of-array-except-self/tests/index.bench.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { bench, describe, expect } from "vitest";
import { bench, describe } from "vitest";

import type { InputTestData } from "@typings/input-data";
import { productExceptSelf } from "..";
import { TEST_DATA } from "./test-data";

const TEST_DATA: InputTestData<number[], number[]> = {
input: [1, 2, 3, 4],
expected: [24, 12, 8, 6],
};

describe("Product of Array Except Self (benchmark)", () => {
for (const { input, expected } of TEST_DATA) {
bench(`should return the product of array except self for input [${input.join(",")}]`, () => {
expect(productExceptSelf(input)).toEqual(expected);
});
}
const { input } = TEST_DATA;
bench("should return the product of array except self", () => {
productExceptSelf(input);
});
});
22 changes: 22 additions & 0 deletions src/arrays/54-spiral-matrix/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# 51. Spiral Matrix

Given an `m x n matrix`, return *all elements of the* `matrix` *in spiral order*.

### Examples

```
Input: matrix = [[1,2,3],[4,5,6],[7,8,9]]
Output: [1,2,3,6,9,8,7,4,5]
```

```
Input: matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]
Output: [1,2,3,4,8,12,11,10,9,5,6,7]
```

### Constraints

- m == matrix.length
- n == matrix[i].length
- 1 <= m, n <= 10
- -100 <= matrix[i][j] <= 100
51 changes: 51 additions & 0 deletions src/arrays/54-spiral-matrix/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import type { Matrix } from "@typings/matrix";

/**
* Returns the elements of the input matrix in spiral order.
*
* @param {Matrix<number>} matrix - The input matrix.
* @returns {number[]} The elements of the matrix in spiral order.
*
* @timeComplexity O(m * n) - Linear time complexity, where m is the number of rows and n is the number of columns in the matrix.
* @spaceComplexity O(1) - Constant space complexity, as the result array is not included in the space analysis.
*
* @description
* This function traverses the input matrix in a spiral order by repeatedly moving through its boundary elements.
* It maintains four boundary variables representing the outermost row and column indices of the matrix.
* It iteratively moves from left to right, top to bottom, right to left, and bottom to top, updating the boundaries
* after each traversal step until all elements have been visited.
*/
export const spiralOrder = (matrix: Matrix<number>): number[] => {
// Initialize the result array
const res: number[] = [];

// Initialize boundary variables
let left = 0;
let right = matrix[0].length;
let top = 0;
let bottom = matrix.length;

// Traverse the matrix in a spiral order
while (left < right && top < bottom) {
// Traverse top row
for (let i = left; i < right; i++) res.push(matrix[top][i]);
top++;

// Traverse right column
for (let i = top; i < bottom; i++) res.push(matrix[i][right - 1]);
right--;

// Check if there are still elements to traverse
if (!(left < right && top < bottom)) break;

// Traverse bottom row
for (let i = right - 1; i > left - 1; i--) res.push(matrix[bottom - 1][i]);
bottom--;

// Traverse left column
for (let i = bottom - 1; i > top - 1; i--) res.push(matrix[i][left]);
left++;
}

return res;
};
21 changes: 21 additions & 0 deletions src/arrays/54-spiral-matrix/tests/index.bench.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { bench, describe } from "vitest";

import type { InputTestData } from "@typings/input-data";
import type { Matrix } from "@typings/matrix";
import { spiralOrder } from "..";

const TEST_DATA: InputTestData<Matrix<number>, number[]> = {
input: [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
],
expected: [1, 2, 3, 6, 9, 8, 7, 4, 5],
};

describe("Spiral Matrix (bench)", () => {
const { input } = TEST_DATA;
bench("should return the elements of the matrix in spiral order", () => {
spiralOrder(input);
});
});
17 changes: 17 additions & 0 deletions src/arrays/54-spiral-matrix/tests/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { describe, expect, it } from "vitest";

import { spiralOrder } from "..";
import { TEST_DATA } from "./test-data";

describe("Spiral Matrix", () => {
for (const { input, expected } of TEST_DATA) {
it("should return the elements of the matrix in spiral order", () => {
// Validate inputs
expect(input).toBeDefined();
expect(expected).toBeDefined();

// Validate output
expect(spiralOrder(input)).toEqual(expected);
});
}
});
83 changes: 83 additions & 0 deletions src/arrays/54-spiral-matrix/tests/test-data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import type { InputTestData } from "@typings/input-data"; // Assuming you have a types file
import type { Matrix } from "@typings/matrix";

export const TEST_DATA: InputTestData<Matrix<number>, number[]>[] = [
{
// Square matrix with increasing numbers in each row
input: [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
],
expected: [1, 2, 3, 6, 9, 8, 7, 4, 5],
},
{
// Rectangular matrix with increasing numbers in each row
input: [
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
],
expected: [1, 2, 3, 4, 8, 12, 11, 10, 9, 5, 6, 7],
},
{
// Single-element matrix
input: [[1]],
expected: [1],
},
{
// Single-row matrix
input: [[1, 2, 3]],
expected: [1, 2, 3],
},
{
// Single-column matrix
input: [[1], [2], [3]],
expected: [1, 2, 3],
},
{
// Square matrix with decreasing numbers in each row
input: [
[9, 8, 7],
[6, 5, 4],
[3, 2, 1],
],
expected: [9, 8, 7, 4, 1, 2, 3, 6, 5],
},
{
// Rectangular matrix with decreasing numbers in each row
input: [
[12, 11, 10, 9],
[8, 7, 6, 5],
[4, 3, 2, 1],
],
expected: [12, 11, 10, 9, 5, 1, 2, 3, 4, 8, 7, 6],
},
{
// Matrix with negative numbers
input: [
[-1, -2, -3],
[-4, -5, -6],
[-7, -8, -9],
],
expected: [-1, -2, -3, -6, -9, -8, -7, -4, -5],
},
{
// Matrix with positive and negative numbers
input: [
[1, -2, 3],
[-4, 5, -6],
[7, -8, 9],
],
expected: [1, -2, 3, -6, 9, -8, 7, -4, 5],
},
{
// Matrix with zeros
input: [
[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
],
expected: [0, 0, 0, 0, 0, 0, 0, 0, 0],
},
];

0 comments on commit 7260957

Please sign in to comment.