From 35e593d1d7b2b07d5d8fd9a85a527b45e142bd34 Mon Sep 17 00:00:00 2001 From: crypticsy Date: Fri, 16 Feb 2024 16:39:19 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=A4=96=20Updated=20LeetCode=20Problems=20?= =?UTF-8?q?:=20Fri,16=20Feb,=202024=2004:39=20PM?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- LeetCode 75/1004 Max Consecutive Ones III.py | 38 +++++++++++++++++++ .../1207 Unique Number of Occurrences.py | 34 +++++++++++++++++ .../2215 Find the Difference of Two Arrays.py | 36 ++++++++++++++++++ LeetCode 75/README.md | 6 +-- 4 files changed, 111 insertions(+), 3 deletions(-) create mode 100644 LeetCode 75/1004 Max Consecutive Ones III.py create mode 100644 LeetCode 75/1207 Unique Number of Occurrences.py create mode 100644 LeetCode 75/2215 Find the Difference of Two Arrays.py diff --git a/LeetCode 75/1004 Max Consecutive Ones III.py b/LeetCode 75/1004 Max Consecutive Ones III.py new file mode 100644 index 0000000..cc81972 --- /dev/null +++ b/LeetCode 75/1004 Max Consecutive Ones III.py @@ -0,0 +1,38 @@ +# 1004. Max Consecutive Ones III + +# Given a binary array nums and an integer k, return the maximum number of consecutive 1's in the array if you can flip at most k 0's. + + +# Example 1: + +# Input: nums = [1,1,1,0,0,0,1,1,1,1,0], k = 2 +# Output: 6 +# Explanation: [1,1,1,0,0,1,1,1,1,1,1] +# Bolded numbers were flipped from 0 to 1. The longest subarray is underlined. +# Example 2: + +# Input: nums = [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1], k = 3 +# Output: 10 +# Explanation: [0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1] +# Bolded numbers were flipped from 0 to 1. The longest subarray is underlined. + + +# Constraints: + +# 1 <= nums.length <= 105 +# nums[i] is either 0 or 1. +# 0 <= k <= nums.length + + + +class Solution: + def longestOnes(self, nums: list[int], k: int) -> int: + l, mx = 0, 0 + for r, n in enumerate(nums): + k -= 1 - n + if k < 0: + k += 1 - nums[l] + l += 1 + else: + mx = max(mx, r - l + 1) + return mx \ No newline at end of file diff --git a/LeetCode 75/1207 Unique Number of Occurrences.py b/LeetCode 75/1207 Unique Number of Occurrences.py new file mode 100644 index 0000000..9c46041 --- /dev/null +++ b/LeetCode 75/1207 Unique Number of Occurrences.py @@ -0,0 +1,34 @@ +# 1207. Unique Number of Occurrences + +# Given an array of integers arr, return true if the number of occurrences of each value in the array is unique or false otherwise. + + +# Example 1: + +# Input: arr = [1,2,2,1,1,3] +# Output: true +# Explanation: The value 1 has 3 occurrences, 2 has 2 and 3 has 1. No two values have the same number of occurrences. +# Example 2: + +# Input: arr = [1,2] +# Output: false +# Example 3: + +# Input: arr = [-3,0,1,-3,1,1,1,-3,10,0] +# Output: true + + +# Constraints: + +# 1 <= arr.length <= 1000 +# -1000 <= arr[i] <= 1000 + + +class Solution: + def uniqueOccurrences(self, arr: list[int]) -> bool: + freq = {} + for i in arr: + freq[i] = freq.get(i, 0) + 1 + + return len(freq.values()) == len(set(freq.values())) + \ No newline at end of file diff --git a/LeetCode 75/2215 Find the Difference of Two Arrays.py b/LeetCode 75/2215 Find the Difference of Two Arrays.py new file mode 100644 index 0000000..bbfbb97 --- /dev/null +++ b/LeetCode 75/2215 Find the Difference of Two Arrays.py @@ -0,0 +1,36 @@ +# 2215. Find the Difference of Two Arrays + +# Given two 0-indexed integer arrays nums1 and nums2, return a list answer of size 2 where: + +# answer[0] is a list of all distinct integers in nums1 which are not present in nums2. +# answer[1] is a list of all distinct integers in nums2 which are not present in nums1. +# Note that the integers in the lists may be returned in any order. + + +# Example 1: + +# Input: nums1 = [1,2,3], nums2 = [2,4,6] +# Output: [[1,3],[4,6]] +# Explanation: +# For nums1, nums1[1] = 2 is present at index 0 of nums2, whereas nums1[0] = 1 and nums1[2] = 3 are not present in nums2. Therefore, answer[0] = [1,3]. +# For nums2, nums2[0] = 2 is present at index 1 of nums1, whereas nums2[1] = 4 and nums2[2] = 6 are not present in nums2. Therefore, answer[1] = [4,6]. +# Example 2: + +# Input: nums1 = [1,2,3,3], nums2 = [1,1,2,2] +# Output: [[3],[]] +# Explanation: +# For nums1, nums1[2] and nums1[3] are not present in nums2. Since nums1[2] == nums1[3], their value is only included once and answer[0] = [3]. +# Every integer in nums2 is present in nums1. Therefore, answer[1] = []. + + +# Constraints: + +# 1 <= nums1.length, nums2.length <= 1000 +# -1000 <= nums1[i], nums2[i] <= 1000 + + +class Solution: + def findDifference(self, nums1: list[int], nums2: list[int]) -> list[list[int]]: + set_nums1, set_nums2 = set(nums1), set(nums2) + return [list(set_nums1 - set_nums2), list(set_nums2 - set_nums1)] + \ No newline at end of file diff --git a/LeetCode 75/README.md b/LeetCode 75/README.md index 49d5093..b54fa63 100644 --- a/LeetCode 75/README.md +++ b/LeetCode 75/README.md @@ -109,7 +109,7 @@ Max Consecutive Ones III Medium - + Solved Longest Subarray of 1's After Deleting One Element @@ -151,12 +151,12 @@ Find the Difference of Two Arrays Easy - + Solved Unique Number of Occurrences Easy - + Solved Determine if Two Strings Are Close