DEV Community

Ertugrul
Ertugrul

Posted on • Edited on

πŸ—“ Daily LeetCode Progress – Day 1

Problems Solved:

  • #1 Two Sum
  • #217 Contains Duplicate

πŸ’‘ What I Learned

Today, I focused on practicing basic array operations in both Python and C++.
Key takeaways:

  • Python: Learned how to use set for quick duplicate checks.
  • C++: Reinforced understanding of vector for arrays and unordered_set for constant-time lookups.

πŸ“ Problem #1 – Two Sum

Goal: Find indices of two numbers in the array that add up to a given target.

Python Solution:

class Solution(object):
    def twoSum(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: List[int]
        """
        for z in range(0, len(nums)):
            for i in range(z + 1, len(nums)):
                if (nums[z] + nums[i]) == target:
                    return [z, i]
Enter fullscreen mode Exit fullscreen mode

Logic:

  1. Loop through all elements with index z.
  2. For each element, check all following elements with index i.
  3. If their sum equals the target, return the two indices.
  4. Time complexity: O(nΒ²) because of the nested loop.

C++ Solution:

class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        int x = 0;
        int max = nums.size();
        for (x; x < max; x++) {
            for (int y = x + 1; y < max; y++) {
                if ((nums[x] + nums[y]) == target) {
                    return {x, y};
                }
            }
        }
        return {};
    }
};
Enter fullscreen mode Exit fullscreen mode

Logic:

  1. Iterate over nums with index x.
  2. Inner loop starts from x + 1 to avoid repeating pairs.
  3. If nums[x] + nums[y] equals target, return the pair {x, y}.
  4. Time complexity: O(nΒ²), same as Python version.

πŸ“ Problem #217 – Contains Duplicate

Goal: Return true if any number appears more than once in the array.

Python Solution:

class Solution(object):
    def containsDuplicate(self, nums):
        """
        :type nums: List[int]
        :rtype: bool
        """
        return len(nums) != len(set(nums))
Enter fullscreen mode Exit fullscreen mode

Logic:

  1. Convert nums into a set (removes duplicates).
  2. If the size changes, there was at least one duplicate.
  3. Time complexity: O(n) on average because set operations are constant time.

C++ Solution:

class Solution {
public:
    bool containsDuplicate(vector<int>& nums) {
        unordered_set<int> seen;
        for (int num : nums) {
            if (seen.count(num)) {
                return true;
            }
            seen.insert(num);
        }
        return false;
    }
};
Enter fullscreen mode Exit fullscreen mode

Logic:

  1. Use an unordered_set to store numbers seen so far.
  2. For each number:
  • If it already exists in seen, return true.
  • Otherwise, insert it into the set.
    1. Time complexity: O(n) on average because unordered_set lookups and insertions are constant time.

πŸ“Έ Achievements

  • Two Sum (Python & C++):

TS python

TS cpp

  • Contains Duplicate (Python & C++):

CD python

CD cpp


πŸ“¦ Complexity Recap

  • Two Sum: O(nΒ²) (brute force nested loop)
  • Contains Duplicate: O(n) (hash set lookups)

πŸ“’ Join the Journey

I’ve just started this LeetCode adventure, solving problems in both Python and C++ while sharing my learnings.
If you’re interested in algorithms, data structures, or just want to keep up with my progress – stay tuned and join me on this journey!

πŸ”— Connect with me:

Top comments (0)