LeetCode Solutions – Lucas Dev https://lucasgdev.com Wed, 03 Jul 2024 13:37:30 +0000 en-US hourly 1 https://lucasgdev.com/wp-content/uploads/2024/05/cropped-LD-removebg-preview-32x32.png LeetCode Solutions – Lucas Dev https://lucasgdev.com 32 32 LeetCode #1929 – Concatenation of Array https://lucasgdev.com/leetcode-1929-concatenation-of-array/?utm_source=rss&utm_medium=rss&utm_campaign=leetcode-1929-concatenation-of-array Wed, 03 Jul 2024 13:37:30 +0000 https://lucasgdev.com/?p=412 Let’s solve LeetCode 1929 – Concatenation of Array problem. It’s from the Arrays section on LeetCode, and as usual, we’ll tackle it using C# today.

Before we jump into the code, let’s understand the problem statement.

This problem challenges you to create a new array by “duplicating” a given integer array. The resulting array should have twice the length of the original, with each element from the original array appearing twice consecutively (That’s the key to solve this challenge).

Understanding the Problem:
  • We are given an integer array nums of length n.
  • We need to create a new array ans of length 2n (twice the length of the original).
  • The elements in the new array ans should be arranged such that ans[i] == nums[i] and ans[i + n] == nums[i] for all indices i from 0 to n-1.

In simpler terms, the new array ans is the concatenation of two copies of the original array nums.

Alright, let’s start designing our solution:

1) We need to create a new array ans that can hold twice the number of elements as the original array nums.

public class Solution {
    public int[] GetConcatenation(int[] nums) {
        int[] ans = new int[nums.Length*2];
    }
}

2) We need to iterate through each element in the original array nums and copy it to the new array ans at two specific positions ( i and i + nums.Length).

public class Solution {
    public int[] GetConcatenation(int[] nums) {
        int[] ans = new int[nums.Length*2];
        
        for(int i = 0; i < nums.Length; i++){
            ans[i] = nums[i];
            ans[i + nums.Length] = nums[i];
        }
    }
}
  • The line ans[i] = nums[i] copies the element at index i from nums to the corresponding index i in the ans array. This ensures the first half of the ans array is a copy of the original nums.
  • The line ans[i + nums.Length] = nums[i] is the key part for concatenation. It copies the same element at index i from nums to an index that is nums.Length positions ahead of the current index i in the ans array. This effectively places a copy of each element from nums in the second half of the ans array.

3) Now we just need to return ans after the loop!

public class Solution {
    public int[] GetConcatenation(int[] nums) {
        int[] ans = new int[nums.Length*2];
        
        for(int i = 0; i < nums.Length; i++){
            ans[i] = nums[i];
            ans[i + nums.Length] = nums[i];
        }
        
        return ans;
    }
}

This solution iterates through the original array, copies each element to its corresponding position in the first half of the new array, and then copies it again to the calculated position in the second half, resulting in the desired concatenation.

This is the result for this solution:

]]>
LeetCode #1 – Two Sum https://lucasgdev.com/leetcode-1-two-sum/?utm_source=rss&utm_medium=rss&utm_campaign=leetcode-1-two-sum Thu, 16 May 2024 15:41:55 +0000 https://lucasgdev.com/?p=346 Let’s solve LeetCode 1 – Two Sum problem. It’s from the Arrays section on LeetCode, and we’ll tackle it using C# today.

There are often multiple ways to solve an algorithm. However, it’s crucial to consider the time and space complexity of your code. This is where Big O notation comes in. Understanding Big O notation is essential for developers to write efficient, scalable, and performant code.

Before we jump into the code, let’s understand the problem statement.

While the problem description itself is straightforward, finding the optimal solution can be tricky. You might initially consider using nested loops, which would result in a time complexity of O(n^2). This is not ideal for larger datasets due to scalability concerns. Fortunately, there are more efficient approaches available!

To solve this efficiently, let’s create a dictionary, and store each number of the array as its key, and the number’s index as its value. Our dictionary will look like this:

public class Solution {
    public int[] TwoSum(int[] nums, int target) {
        IDictionary<int, int> dict = new Dictionary<int, int>();
    }
}

We’ll create an array to store the solution: the indexes of the two numbers that sum to the target:

public class Solution {
    public int[] TwoSum(int[] nums, int target) {
        IDictionary<int, int> dict = new Dictionary<int, int>();
        var result = new int[2];
    }
}

This step might seem a bit tricky, so follow along closely.

We’ll iterate through the nums array. For each number, we’ll check if the dictionary contains a key equal to the difference between the target sum (target) and the current number of the loop. Remember, the dictionary keys are the numbers from the nums array.

Here’s why we check this: if the difference exists in the dictionary, it means the current number and the value stored for that key (which is the difference) add up to the target sum. These are the two numbers we’re looking for! And then we just have to assign these two numbers to the array we created earlier:

public class Solution {
    public int[] TwoSum(int[] nums, int target) {
        IDictionary<int, int> dict = new Dictionary<int, int>();
        var result = new int[2];

        for(int i = 0; i < nums.Length; i++){
            if(dict.ContainsKey(target - nums[i])){
                result[0] = dict[target - nums[i]];
                result[1] = i;
            }        
        }
    }
}

Since the dictionary is initially empty, we need to populate it with the numbers from the nums array and their corresponding indexes. This is crucial because the if statement we are using checks for the difference between the target sum and a number from the array. By storing the numbers and their indexes in the dictionary, we can efficiently look up the complement (the difference). So, below the if statement we need to add:

public class Solution {
    public int[] TwoSum(int[] nums, int target) {
        IDictionary<int, int> dict = new Dictionary<int, int>();
        var result = new int[2];

        for(int i = 0; i < nums.Length; i++){
            if(dict.ContainsKey(target - nums[i])){
                result[0] = dict[target - nums[i]];
                result[1] = i;
            }
            dict[nums[i]] = i;     
        }
    }
}

Now, we just need to return the result array after the loop!

public class Solution {
    public int[] TwoSum(int[] nums, int target) {
        IDictionary<int, int> dict = new Dictionary<int, int>();
        var result = new int[2];

        for(int i = 0; i < nums.Length; i++){
            if(dict.ContainsKey(target - nums[i])){
                result[0] = dict[target - nums[i]];
                result[1] = i;
            }
            dict[nums[i]] = i;            
        }
        return result;
    }
}

With this solution, our runtime beats 94.16% of users with C#!

]]>