[Leetcode] 2215. Find the Difference of Two Arrays
🗓️ Daily LeetCoding Challenge May, Day 3 class Solution: def findDifference(self, nums1: List[int], nums2: List[int]) -> List[List[int]]: nums1 = set(nums1) nums2 = set(nums2) return nums1 - nums2, nums2 - nums1
2023.05.03
[Leetcode] 1491. Average Salary Excluding the Minimum and Maximum Salary
🗓️ Daily LeetCoding Challenge May, Day 1 class Solution: def average(self, salary: List[int]) -> float: return (sum(salary) - min(salary) - max(salary)) / (len(salary) - 2)
2023.05.02
[Leetcode] 1822. Sign of the Product of an Array
🗓️ Daily LeetCoding Challenge May, Day 2 class Solution: def arraySign(self, nums: List[int]) -> int: if 0 in nums: return 0 len_n = 0 for n in nums: if n < 0: len_n = len_n +1 return -1 if len_n % 2 else 1
2023.05.02
[Leetcode] 121. Best Time to Buy and Sell Stock
Time Complexity: O(N) Space Complexity: O(N) (Worst -> len(maxlist) = n/2 class Solution: def maxProfit(self, prices: List[int]) -> int: if len(prices) 0: minlist.append(prices[n2] - prices[n1]) n1 = i n2 = i + 1 elif prices[i+1] > prices[n2]: n2 = i + 1 if prices[n2]..
2023.05.02
[Leetcode 334] 334. Increasing Triplet Subsequence
class Solution: def increasingTriplet(self, nums: List[int]) -> bool: n1 = n2 = float('inf') for n in nums: if n < n1: n1 = n elif n1 < n < n2: n2 = n elif n1 < n2 < n: return True return False
2023.05.02
[Leetcode] 162. Find Peak Element
정렬이 되어 있지 않은 배열의 이분 탐색 옆으로 한칸씩만 이동하면 마지막 mid가 업데이트 되지 않는다. class Solution: def findPeakElement(self, nums: List[int]) -> int: left, right = 0, len(nums) - 1 while left nums[mid + 1]: right = mid else: left = mid + 1 return left
2023.05.02
[Leetcode] 153. Find Minimum in Rotated Sorted Array
class Solution: def findMin(self, nums: List[int]) -> int: left, right = 0, len(nums) - 1 mid = -1 while left nums[mid]: return nums[mid] return nums[mid]
2023.05.02
[Leetcode] 74. Search a 2D Matrix
class Solution: def searchMatrix(self, matrix: List[List[int]], target: int) -> bool: for line in matrix: left, right = 0, len(line) - 1 while left
2023.05.01

 

 

class Solution:
    def findDifference(self, nums1: List[int], nums2: List[int]) -> List[List[int]]:
        nums1 = set(nums1)
        nums2 = set(nums2)

        return nums1 - nums2, nums2 - nums1

🗓️ Daily LeetCoding Challenge May, Day 1

class Solution:
    def average(self, salary: List[int]) -> float:
        return (sum(salary) - min(salary) - max(salary)) / (len(salary) - 2)

 

class Solution:
    def arraySign(self, nums: List[int]) -> int:

        if 0 in nums:
            return 0

        len_n = 0
        for n in nums:
            if n < 0:
                len_n = len_n +1

        return -1 if len_n % 2 else 1

Time Complexity: O(N)
Space Complexity: O(N) (Worst -> len(maxlist) = n/2

class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        if len(prices) < 2:
            return 0

        n1, n2 = 0, 1
        minlist = []
        for i in range(len(prices) - 1):
            if prices[i] < prices[n1]:
                if prices[n2] - prices[n1] > 0:
                    minlist.append(prices[n2] - prices[n1])
                n1 = i
                n2 = i + 1
            elif prices[i+1] > prices[n2]:
                n2 = i + 1
        if prices[n2] - prices[n1] > 0:
            minlist.append(prices[n2] - prices[n1])
        return max(minlist) if minlist else 0
class Solution:
    def increasingTriplet(self, nums: List[int]) -> bool:
        n1 = n2 = float('inf')

        for n in nums:
            if n < n1:
                n1 = n
            elif n1 < n < n2:
                n2 = n
            elif n1 < n2 < n:
                return True
        return False
정렬이 되어 있지 않은 배열의 이분 탐색
옆으로 한칸씩만 이동하면 마지막 mid가 업데이트 되지 않는다.

 

class Solution:
    def findPeakElement(self, nums: List[int]) -> int:
        left, right = 0, len(nums) - 1

        while left < right:
            mid = left + (right - left)//2
            if nums[mid] > nums[mid + 1]:
                right = mid
            else:
                left = mid + 1
        return left
class Solution:
    def findMin(self, nums: List[int]) -> int:
        left, right = 0, len(nums) - 1
        mid = -1

        while left < right:
            mid = left + (right - left) // 2
            if nums[mid] < nums[right]: 
                right = mid
            elif nums[mid] < nums[left]:
                left = mid
            else:
                while mid < right:
                    prev = mid
                    mid = mid + 1
                    if nums[prev] > nums[mid]:
                        return nums[mid]

        return nums[mid]
class Solution:
    def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
        for line in matrix:
            left, right = 0, len(line) - 1

            while left <= right:
                mid = left + (right - left) // 2

                if line[mid] == target:
                    return True

                if target < line[mid]:
                    right = mid - 1
                else:
                    left = mid + 1
        return False

while left < right: 인 경우

원소가 하나인 경우를 탐색하지 못한다.