Given two sorted arrays nums1 and nums2 of size m and n respectively, return the median of the two sorted arrays.

The overall run time complexity should be O(log (m+n)).

 

Example 1:

Input: nums1 = [1,3], nums2 = [2] Output: 2.00000 Explanation: merged array = [1,2,3] and median is 2.

Example 2:

Input: nums1 = [1,2], nums2 = [3,4] Output: 2.50000 Explanation: merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5.

Example 3:

Input: nums1 = [0,0], nums2 = [0,0] Output: 0.00000

Example 4:

Input: nums1 = [], nums2 = [1] Output: 1.00000

Example 5:

Input: nums1 = [2], nums2 = [] Output: 2.00000

 

Constraints:

  • nums1.length == m
  • nums2.length == n
  • 0 <= m <= 1000
  • 0 <= n <= 1000
  • 1 <= m + n <= 2000
  • -106 <= nums1[i], nums2[i] <= 106
class Solution {
public:
    double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
        double ans = 0;
        int sumSize = nums1.size() + nums2.size();
        int mid = floor(sumSize / 2);
        int curMin = 1000, preMin = -1;
        for (int i = 0; i < sumSize; i++)
        {
            preMin = curMin;
            if (nums1.size() > 0 && nums2.size() > 0)
            {
                if (nums1[0] < nums2[0])
                {
                    curMin = nums1[0];
                    nums1.erase(nums1.begin());
                }
                else
                {
                    curMin = nums2[0];
                    nums2.erase(nums2.begin());
                }
            }
            else
            {
                if (nums1.size() > 0)
                {
                    curMin = nums1[0];
                    nums1.erase(nums1.begin());
                }
                else
                {
                    curMin = nums2[0];
                    nums2.erase(nums2.begin());
                }
            }
            if (i == mid)
            {
                ans = (double)curMin;
                if (sumSize % 2 == 0)
                {
                    ans = (ans + (double)preMin) / 2;
                }
                break;
            }
        }
        return ans;
    }
};

'Challenge' 카테고리의 다른 글

Check for pair with a given sum  (0) 2021.09.13
[LeetCode] Longest Palindromic Substring  (0) 2021.09.12
[LeetCode] Longest Substring Without Repeating Characters  (0) 2021.09.11
[LeetCode] Add Two Numbers  (0) 2021.09.02
[LeetCode] Two Sum  (0) 2021.08.31

Given a string s, find the length of the longest substring without repeating characters.

 

Example 1:

Input: s = "abcabcbb" Output: 3 Explanation: The answer is "abc", with the length of 3.

Example 2:

Input: s = "bbbbb" Output: 1 Explanation: The answer is "b", with the length of 1.

Example 3:

Input: s = "pwwkew" Output: 3 Explanation: The answer is "wke", with the length of 3. Notice that the answer must be a substring, "pwke" is a subsequence and not a substring.

Example 4:

Input: s = "" Output: 0

 

Constraints:

  • 0 <= s.length <= 5 * 104
  • s consists of English letters, digits, symbols and spaces.
class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        int ans = 0;
        unordered_map<char, int> unord;
        for (int i = 0, j = 0; j < s.length(); j++)
        {
            if (unord.find(s[j]) != unord.end())
            {
                i = max(unord[s[j]], i);
            }
            
            ans = max(ans, j - i + 1);
            unord[s[j]] = j + 1;
        }
        return ans;
    }
};

'Challenge' 카테고리의 다른 글

Check for pair with a given sum  (0) 2021.09.13
[LeetCode] Longest Palindromic Substring  (0) 2021.09.12
[LeetCode] Median of Two Sorted Arrays  (0) 2021.09.11
[LeetCode] Add Two Numbers  (0) 2021.09.02
[LeetCode] Two Sum  (0) 2021.08.31

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 1:

Input: nums = [2,7,11,15], target = 9 Output: [0,1] Output: Because nums[0] + nums[1] == 9, we return [0, 1].

Example 2:

Input: nums = [3,2,4], target = 6 Output: [1,2]

Example 3:

Input: nums = [3,3], target = 6 Output: [0,1]

 

Constraints:

  • 2 <= nums.length <= 104
  • -109 <= nums[i] <= 109
  • -109 <= target <= 109
  • Only one valid answer exists.

 

Follow-up: Can you come up with an algorithm that is less than O(n2) time complexity?

 

class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        vector<int> result;
        unordered_map<int, int> unmap;
        for (int i = 0; i < nums.size(); i++)
        {
            if (!unmap[i])
            {
                unmap[i] = nums[i];
            }
        }
        for (auto k: unmap)
        {
            auto it = find(nums.begin(), nums.end(), (target - k.second));
            if (it != nums.end())
            {
                int index = it - nums.begin();
                if (index != k.first)
                {
                    if (k.first < index)
                    {
                        result.push_back(index);
                        result.push_back(k.first);
                    }
                    else
                    {
                        result.push_back(index);
                        result.push_back(k.first);
                    }
                    break;
                }
            }
        }
        return result;
    }
};

 

+ Recent posts