Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Data structures and algorithms section #12

Open
VictorApaez opened this issue Feb 9, 2023 · 1 comment
Open

Data structures and algorithms section #12

VictorApaez opened this issue Feb 9, 2023 · 1 comment
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@VictorApaez
Copy link

It would help if you considered adding a data structures and algorithms section. Here we can post any Leetcode questions we solve or questions encountered during interviews. The solution can include a detailed description, time and space complexity, integrated tests, and resources (videos, articles, documentation, etc.)

Here is an idea for the format:

LeetCode 1. Two Sum

Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

You can return the answer in any order.

Example:

Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
Solution

Solution:

/**
 * @param {number[]} nums
 * @param {number} target
 * @return {number[]}
 */
let twoSum = function(nums, target) {
    let table = {};
    for (let i = 0; i < nums.length; i++) {
        if (table[target - nums[i]] !== undefined) {
            return [table[target - nums[i]], i];
        } else {
            table[nums[i]] = i;
    }
  }
};

Explanation:

Complexity:

Time complexity: O(n).
Space complexity: O(n).

@github-actions
Copy link

github-actions bot commented Feb 9, 2023

Thank you for submitting your first issue

@Brandon-Alvarez-03 Brandon-Alvarez-03 added enhancement New feature or request good first issue Good for newcomers labels Feb 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants