👋 Empowering Algorithms : Google, Amazon Interview 👋

1108
May 28, 2024
May 28, 2024

image

  • Introduction:

    • Explore the intricacies of the Two Pointer Technique, a formidable strategy in algorithm design.
    • Harness the power of dual pointers to navigate through arrays, strings, and beyond.
  • Target Sum Challenge (Asked in Google Interviews):

    • Task: Identify the minimum number of elements required to add up to a specific target sum.

    • Utilize Two Pointer Technique to minimize complexity and maximize efficiency.

    • Code Example:

      class Solution {
      public:
          int minElements(vector<int>& nums, int target) {
              int left = 0, right = nums.size() - 1;
              long long currentSum = 0;
      
              for (int num : nums) {
                  currentSum += num;
              }
      
              long long diff = abs(target - currentSum);
      
              int count = 0;
              while (diff > 0) {
                  if (diff >= nums[right]) {
                      diff -= nums[right];
                      right--;
                  } else {
                      diff = 0;
                  }
                  count++;
              }
      
              return count;
          }
      };
  • Longest Substring Without Repeating Characters (Asked in Amazon Interviews):

    • Task: Find the longest substring without repeating characters.

    • Employ Two Pointer Technique to traverse through the string and identify substrings.

    • Code Example:

      class Solution {
      public:
          int lengthOfLongestSubstring(string s) {
              unordered_map<char, int> charIndex;
              int left = 0, maxLength = 0;
      
              for (int right = 0; right < s.length(); ++right) {
                  if (charIndex.find(s[right]) != charIndex.end() && charIndex[s[right]] >= left) {
                      left = charIndex[s[right]] + 1;
                  }
                  charIndex[s[right]] = right;
                  maxLength = max(maxLength, right - left + 1);
              }
      
              return maxLength;
          }
      };

      Got it! Here's a challenging approach to tackle the Longest Substring with At Most K Distinct Characters problem:

Approach: Sliding Window with Frequency Map

  1. Initialize Variables:

    • Initialize left and right pointers to traverse the string.
    • Initialize an empty map (charFreq) to track the frequency of characters within the window.
    • Initialize a variable maxLength to store the length of the longest substring.
  2. Sliding Window Algorithm:

    • Move the right pointer to the right until the window contains more than k distinct characters.
    • While traversing, update the character frequencies in the charFreq map.
  3. Track Maximum Length:

    • While the window contains at most k distinct characters, update the maxLength with the length of the current window.
  4. Move Left Pointer:

    • Move the left pointer to the right until the window contains less than k distinct characters.
    • While moving, decrement the frequency of characters in the charFreq map and remove characters with frequency 0.
  5. Repeat:

    • Repeat steps 2-4 until the right pointer reaches the end of the string.
  6. Return Result:

    • Return the maxLength as the length of the longest substring with at most k distinct characters.

Complexity Analysis:

  • Time Complexity: O(N), where N is the length of the input string s.
  • Space Complexity: O(K), where K is the number of distinct characters in the string s.

This approach efficiently help you to solves the problem using the sliding window technique combined with a frequency map, providing an optimal solution for LeetCode challenges and coding interviews.

Comments (2)