Skip to content

Commit

Permalink
commit
Browse files Browse the repository at this point in the history
  • Loading branch information
abhishekghoshh committed Jan 15, 2025
1 parent 2508703 commit 11a633d
Show file tree
Hide file tree
Showing 7 changed files with 559 additions and 170 deletions.
3 changes: 3 additions & 0 deletions resources/sliding-window.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
- [Longest Substring With K Unique Characters / Variable Size Sliding Window](/src/com/problems/slidingwindow/LargestSubstringWithKUniqueCharacters.java)
- [Longest Substring With Without Repeating Characters / Variable Size Sliding Window](/src/com/problems/slidingwindow/LongestSubstringWithoutRepeatingCharacters.java)
- [Fruit Into Baskets / Pick Toys / An Interesting Sliding Window Problem](/src/com/problems/slidingwindow/FruitIntoBaskets.java)
- [Minimum Number of Flips to Make the Binary String Alternating](/src/com/problems/slidingwindow/MinimumNumberOfFlipsToMakeTheBinaryStringAlternating.java)
- [Maximum Number of Vowels in a Substring of Given Length](/src/com/problems/slidingwindow/MaximumNumberOfVowelsInASubstringOfGivenLength.java)
- [Minimum Window Substring / Variable Size Sliding Window](/src/com/problems/slidingwindow/MinimumWindowSubstring.java)
- [Minimum Window Subsequence](/src/com/problems/slidingwindow/MinimumWindowSubsequence.java)
- [Maximum Sum of Distinct Subarrays With Length K](/src/com/problems/slidingwindow/MaximumSumOfDistinctSubarraysWithLengthK.java)
Expand All @@ -16,6 +18,7 @@
- [Number of Substrings Containing All Three Characters](/src/com/problems/slidingwindow/NumberOfSubstringsContainingAllThreeCharacters.java)
- [Max Consecutive Ones III](/src/com/problems/slidingwindow/MaxConsecutiveOnes3.java)
- [Longest Repeating Character Replacement](/src/com/problems/slidingwindow/LongestRepeatingCharacterReplacement.java)
- [Permutation in String](/src/com/problems/slidingwindow/PermutationInString.java)
- [Binary Subarrays With Sum](/src/com/problems/slidingwindow/BinarySubarraysWithSum.java)
- [Count Number of Nice Subarrays](/src/com/problems/slidingwindow/CountNumberOfNiceSubarrays.java)
- [Maximum Points You Can Obtain from Cards](/src/com/problems/slidingwindow/MaximumPointsYouCanObtainFromCards.java)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,23 @@
* https://leetcode.com/problems/frequency-of-the-most-frequent-element/description/
*
* Solution link :
* Neetcode : https://www.youtube.com/watch?v=vgBrQ0NM5vE
* Aryan Mittal : https://www.youtube.com/watch?v=e0AFPpKcjw0
* Neetcode
* https://www.youtube.com/watch?v=vgBrQ0NM5vE
* Aryan Mittal :
* https://www.youtube.com/watch?v=e0AFPpKcjw0
*
* https://takeuforward.org/arrays/find-the-highest-lowest-frequency-element/
* https://neetcode.io/solutions/frequency-of-the-most-frequent-element
*/

public class FrequencyOfTheMostFrequentElement {
public static void main(String[] args) {
type1();
}

// using the sliding window approach
// time complexity O(nlog(n) + 2n)
// the intuition is kind of greedy
// todo using the sliding window approach
// time complexity O(nlog(n) + 2n)
// the intuition is kind of greedy
// lets say if the nums array is [1,1,1,3,3,5]
// and we are operating on 5 currently, and we need to choose other
// numbers to convert them into 5, if we change 1 into 5 it will take 4 unit
Expand All @@ -30,7 +34,7 @@ public static void main(String[] args) {
// let's say we are on j th element, we will assume that the left side numbers
// will be converted into this, if the left point is on i
// so the total range is (j-i+1)
// and our criteria will be nums[j]*(j-i+1) - range-sum(j-i+1) <= k
// and our criteria will be (nums[j] * (j - i + 1)) - range-sum(j - i + 1) <= k
// if it is less than equal to k then it is possible to convert all the numbers in that range
// else we have to increment left till it does not fit into the criteria
// we will also need to carry the range sum
Expand All @@ -44,20 +48,23 @@ private static void type1() {
public static int maxFrequency1(int[] nums, int k) {
// we need to sort at first
Arrays.sort(nums);
int n = nums.length;

long sum = 0;
int max = 0;
int n = nums.length, left = 0, right = 0;
while (right < n) {
// we will go from 0 to n and choose current number to be target number for all the left side numbers
for (int right = 0, left = 0; right < n; right++) {
// adding the current element in the range sum
long num = nums[right];
sum += num;
sum += num; // updating the sum
// if num * range - range sum > k then we can not transform all the numbers to the current num
// so we will decrease from left and shrink the window
while (num * (right - left + 1) - sum > k) {
// decrementing the left most element from that range
sum -= nums[left++];
}
// checking the max frequency
max = Math.max(max, right - left + 1);
right++;
}
return max;
}
Expand Down
Loading

0 comments on commit 11a633d

Please sign in to comment.