-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #449 from avnisinngh/five_add
adding leetcode solutions
- Loading branch information
Showing
2 changed files
with
35 additions
and
0 deletions.
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
LeetCode/2958. Length of Longest Subarray With at Most K Frequency.java
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,17 @@ | ||
import java.util.HashMap; | ||
|
||
class Solution { | ||
public int maxSubarrayLength(int[] nums, int k) { | ||
HashMap<Integer, Integer> map = new HashMap<>(); | ||
int ans = 0; | ||
for(int left = 0, right = 0; right < nums.length; right++) { | ||
map.put(nums[right], map.getOrDefault(nums[right], 0) + 1); | ||
while(map.get(nums[right]) > k && left >=0){ | ||
map.put(nums[left], map.get(nums[left]) - 1); | ||
left++; | ||
} | ||
ans = Math.max(ans, right - left + 1); | ||
} | ||
return ans; | ||
} | ||
} |
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,18 @@ | ||
class Solution { | ||
public int numSubarrayProductLessThanK(int[] nums, int k) { | ||
int product = 1; | ||
int count = 0; | ||
int left = 0, right = 0; | ||
while(right < nums.length){ | ||
product *= nums[right]; | ||
while(product >= k && left <= right){ | ||
product /= nums[left++]; | ||
} | ||
if(product < k){ | ||
count += right - left + 1; | ||
} | ||
right++; | ||
} | ||
return count; | ||
} | ||
} |