-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🤖 Updated LeetCode Problems : Fri,16 Feb, 2024 04:39 PM
- Loading branch information
Showing
4 changed files
with
111 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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())) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters