Skip to content

Commit

Permalink
🤖 Updated LeetCode Problems : Fri,16 Feb, 2024 04:39 PM
Browse files Browse the repository at this point in the history
  • Loading branch information
crypticsy committed Feb 16, 2024
1 parent 6baab4a commit 35e593d
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 3 deletions.
38 changes: 38 additions & 0 deletions LeetCode 75/1004 Max Consecutive Ones III.py
Original file line number Diff line number Diff line change
@@ -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
34 changes: 34 additions & 0 deletions LeetCode 75/1207 Unique Number of Occurrences.py
Original file line number Diff line number Diff line change
@@ -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()))

36 changes: 36 additions & 0 deletions LeetCode 75/2215 Find the Difference of Two Arrays.py
Original file line number Diff line number Diff line change
@@ -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)]

6 changes: 3 additions & 3 deletions LeetCode 75/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@
<tr>
<td> Max Consecutive Ones III </td>
<td> Medium </td>
<td> </td>
<td> <a href="./1004 Max Consecutive Ones III.py target="_blank">Solved</a> </td>
</tr>
<tr>
<td> Longest Subarray of 1's After Deleting One Element </td>
Expand Down Expand Up @@ -151,12 +151,12 @@
<tr>
<td> Find the Difference of Two Arrays </td>
<td> Easy </td>
<td> </td>
<td> <a href="./2215 Find the Difference of Two Arrays.py" target="_blank">Solved</a> </td>
</tr>
<tr>
<td> Unique Number of Occurrences </td>
<td> Easy </td>
<td> </td>
<td> <a href="./1207 Unique Number of Occurrences.py" target="_blank">Solved</a> </td>
</tr>
<tr>
<td> Determine if Two Strings Are Close </td>
Expand Down

0 comments on commit 35e593d

Please sign in to comment.