Skip to content

Commit

Permalink
162nd Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Shyam-Chen committed Jul 13, 2024
1 parent 73a2a8c commit cb008d7
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 7 deletions.
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,15 @@ Ace Coding Interview with 75 Qs
[11]: ./src/page-1/11.%20Container%20With%20Most%20Water/maxArea.ts
[1679]: ./src/page-16/1679.%20Max%20Number%20of%20K-Sum%20Pairs/maxOperations.ts

| Sliding Window | | |
| ------------------------------------------------------------- | --------------- | ------ |
| 643. Maximum Average Subarray I | [Solution][643] | Easy |
| 1456. Maximum Number of Vowels in a Substring of Given Length | Solution | Medium |
| 1004. Max Consecutive Ones III | Solution | Medium |
| 1493. Longest Subarray of 1's After Deleting One Element | Solution | Medium |
| Sliding Window | | |
| ------------------------------------------------------------- | ---------------- | ------ |
| 643. Maximum Average Subarray I | [Solution][643] | Easy |
| 1456. Maximum Number of Vowels in a Substring of Given Length | [Solution][1456] | Medium |
| 1004. Max Consecutive Ones III | Solution | Medium |
| 1493. Longest Subarray of 1's After Deleting One Element | Solution | Medium |

[643]: ./src/page-6/643.%20Maximum%20Average%20Subarray%20I/findMaxAverage.ts
[1456]: ./src/page-14/1456.%20Maximum%20Number%20of%20Vowels%20in%20a%20Substring%20of%20Given%20Length/maxVowels.ts

| Prefix Sum | | |
| ------------------------------- | -------- | ---- |
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { maxVowels } from './maxVowels';

describe('1456. Maximum Number of Vowels in a Substring of Given Length', () => {
test('maxVowels', () => {
expect(maxVowels('abciiidef', 3)).toBe(3);
expect(maxVowels('aeiou', 2)).toBe(2);
expect(maxVowels('leetcode', 3)).toBe(2);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
type MaxVowels = (s: string, k: number) => number;

/**
* Accepted
*/
export const maxVowels: MaxVowels = (s, k) => {
const vowels = new Set(['a', 'e', 'i', 'o', 'u']);

let maxVowels = 0;
let currentVowels = 0;

for (let i = 0; i < s.length; i++) {
// Check if the current character is a vowel
if (vowels.has(s[i])) currentVowels += 1;

// If the window size exceeds k, slide the window
if (i >= k && vowels.has(s[i - k])) currentVowels -= 1;

// Update maxVowels
if (i >= k - 1) maxVowels = Math.max(maxVowels, currentVowels);
}

return maxVowels;
};
5 changes: 4 additions & 1 deletion src/page-6/643. Maximum Average Subarray I/findMaxAverage.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
type FindMaxAverage = (nums: number[], k: number) => number;

/**
* Accepted
*/
export const findMaxAverage: FindMaxAverage = (nums, k) => {
const n = nums.length;

Expand All @@ -12,7 +15,7 @@ export const findMaxAverage: FindMaxAverage = (nums, k) => {

// Slide the window over the array from the start to the end
for (let i = k; i < n; i++) {
sum = sum - nums[i - k] + nums[i];
sum = sum - nums[i - k] + nums[i]; // Slide the window to the right
maxSum = Math.max(maxSum, sum);
}

Expand Down

0 comments on commit cb008d7

Please sign in to comment.