Leetcode 2540. Minimum Common Value
I am a developer from Nashville, TN. I specialize in the .NET tech stack. I have created many projects in Blazor WASM, Xamarin, MAUI, etc.
Search for a command to run...
I am a developer from Nashville, TN. I specialize in the .NET tech stack. I have created many projects in Blazor WASM, Xamarin, MAUI, etc.
No comments yet. Be the first to comment.
A series documenting my journey to improving my ability to solve LeetCode problems through YouTube videos, study plans, articles, etc. Using my own words for later reference.
Approach First check to make sure there's enough elements in the original matrix to meet the new row and column requirements. I did this by creating a new list to store all of the original values (which we will then use later to fill the new matrix) ...
Intuition Get the length of the subsequence (if any) and subtract it from the length of t to find out how many characters we have to append (if any) Approach Keep a count of the length of the subsequence while iterating through s and then subtract th...
Intuition Sort the array and check if the start and end of the array match. If not, increment or decrement accordingly. Approach First sort the array. Then use two pointers. One at the beginning, the other at the end. Check to see if the value at the...
Intuition Find position of ch (if any) then use two pointers to swap characters until string is reveresed from beginning to index of ch Approach Start by checking each character for ch. If we find it, that's where our right pointer will start. Our le...
Intuition Use two pointers to iterate through the string checking each adjacent character to see if it is "bad" or "good". Approach First if the string is 1 or fewer characters we know it is good so we can just return it. If that's not the case, we c...
Since both arrays are sorted, we can find the minimum common value by iterating from left to right with two pointers.
Initialize both pointers to 0 to start at the beginning of the array. While both pointers are within bounds of the array, check to see if they are pointing to the same value. If so, we have our answer. If not, increment the smaller value.
Because these arrays are sorted and we started at the beginning I.E. with the smallest values, we know the current smallest values don't match and therefore we must check if the next smallest value does match.
Finally if we get through both arrays without finding a match, return -1 since there is no common value.
Time complexity:
O(n)O(n)O(n) since at worst case we would have to go through all nnn values of the arrays.
Space complexity:
O(1)O(1)O(1) since we are not using any extra space (besides the pointers).
public class Solution
{
public int GetCommon(int[] nums1, int[] nums2)
{
//create two pointers, one for each array
int num1 = 0;
int num2 = 0;
//loop while both pointers are in range
while(num1 < nums1.Length && num2 < nums2.Length)
{
//if the number at both pointers is the same, we found our result
if(nums1[num1] == nums2[num2])
{
return nums1[num1];
}
//if num1 is smaller, increment it by 1
else if(nums1[num1] < nums2[num2])
{
num1++;
}
//otherwise if num2 is smaller, increment it by 1
else
{
num2++;
}
}
//if end of loop is reached, no result was found
return -1;
}
}