-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d8efba9
commit bcde19d
Showing
12 changed files
with
140 additions
and
40 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
9 changes: 0 additions & 9 deletions
9
src/page-14/1431. Kids With the Greatest Number of Candies/kids-with-candies.spec.ts
This file was deleted.
Oops, something went wrong.
12 changes: 0 additions & 12 deletions
12
src/page-14/1431. Kids With the Greatest Number of Candies/kids-with-candies.ts
This file was deleted.
Oops, something went wrong.
9 changes: 9 additions & 0 deletions
9
src/page-14/1431. Kids With the Greatest Number of Candies/kidsWithCandies.spec.ts
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,9 @@ | ||
import { kidsWithCandies } from './kidsWithCandies'; | ||
|
||
describe('1431. Kids With the Greatest Number of Candies', () => { | ||
it('kidsWithCandies', () => { | ||
expect(kidsWithCandies([2, 3, 5, 1, 3], 3)).toStrictEqual([true, true, true, false, true]); | ||
expect(kidsWithCandies([4, 2, 1, 1, 2], 1)).toStrictEqual([true, false, false, false, false]); | ||
expect(kidsWithCandies([12, 1, 12], 10)).toStrictEqual([true, false, true]); | ||
}); | ||
}); |
9 changes: 9 additions & 0 deletions
9
src/page-14/1431. Kids With the Greatest Number of Candies/kidsWithCandies.ts
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,9 @@ | ||
type KidsWithCandies = (candies: number[], extraCandies: number) => boolean[]; | ||
|
||
/** | ||
* Accepted | ||
*/ | ||
export const kidsWithCandies: KidsWithCandies = (candies, extraCandies) => { | ||
const maxCandies = Math.max(...candies); | ||
return candies.map((candy) => candy + extraCandies >= maxCandies); | ||
}; |
15 changes: 0 additions & 15 deletions
15
src/page-17/1768. Merge Strings Alternately/merge-alternately.spec.ts
This file was deleted.
Oops, something went wrong.
15 changes: 15 additions & 0 deletions
15
src/page-17/1768. Merge Strings Alternately/mergeAlternately.spec.ts
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,15 @@ | ||
import { mergeAlternately, mergeAlternately2 } from './mergeAlternately'; | ||
|
||
describe('1768. Merge Strings Alternately', () => { | ||
it('mergeAlternately', () => { | ||
expect(mergeAlternately('abc', 'pqr')).toBe('apbqcr'); | ||
expect(mergeAlternately('ab', 'pqrs')).toBe('apbqrs'); | ||
expect(mergeAlternately('abcd', 'pq')).toBe('apbqcd'); | ||
}); | ||
|
||
it('mergeAlternately2', () => { | ||
expect(mergeAlternately2('abc', 'pqr')).toBe('apbqcr'); | ||
expect(mergeAlternately2('ab', 'pqrs')).toBe('apbqrs'); | ||
expect(mergeAlternately2('abcd', 'pq')).toBe('apbqcd'); | ||
}); | ||
}); |
File renamed without changes.
8 changes: 8 additions & 0 deletions
8
src/page-4/345. Reverse Vowels of a String/reverseVowels.spec.ts
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,8 @@ | ||
import { reverseVowels } from './reverseVowels'; | ||
|
||
describe('345. Reverse Vowels of a String', () => { | ||
it('reverseVowels', () => { | ||
expect(reverseVowels('hello')).toBe('holle'); | ||
expect(reverseVowels('leetcode')).toBe('leotcede'); | ||
}); | ||
}); |
36 changes: 36 additions & 0 deletions
36
src/page-4/345. Reverse Vowels of a String/reverseVowels.ts
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 @@ | ||
type ReverseVowels = (s: string) => string; | ||
|
||
export const reverseVowels: ReverseVowels = (s) => { | ||
// Helper function to check if a character is a vowel | ||
function isVowel(char: string): boolean { | ||
return 'aeiouAEIOU'.includes(char); | ||
} | ||
|
||
// Convert the string to an array to allow mutable operations | ||
const chars = s.split(''); | ||
|
||
let [left, right] = [0, chars.length - 1]; | ||
|
||
// Use two pointers to find vowels from both ends of the string | ||
while (left < right) { | ||
// Move the left pointer to the next vowel | ||
while (left < right && !isVowel(chars[left])) { | ||
left += 1; | ||
} | ||
|
||
// Move the right pointer to the previous vowel | ||
while (left < right && !isVowel(chars[right])) { | ||
right -= 1; | ||
} | ||
|
||
// Swap the vowels | ||
if (left < right) { | ||
[chars[left], chars[right]] = [chars[right], chars[left]]; | ||
left += 1; | ||
right -= 1; | ||
} | ||
} | ||
|
||
// Convert the array back to a string and return it | ||
return chars.join(''); | ||
}; |
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,8 @@ | ||
import { canPlaceFlowers } from './canPlaceFlowers'; | ||
|
||
describe('605. Can Place Flowers', () => { | ||
it('canPlaceFlowers', () => { | ||
expect(canPlaceFlowers([1, 0, 0, 0, 1], 1)).toBe(true); | ||
expect(canPlaceFlowers([1, 0, 0, 0, 1], 2)).toBe(false); | ||
}); | ||
}); |
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,48 @@ | ||
type CanPlaceFlowers = (flowerbed: number[], n: number) => boolean; | ||
|
||
export const canPlaceFlowers: CanPlaceFlowers = (flowerbed, n) => { | ||
// Counter to track the number of flowers planted | ||
let count = 0; | ||
|
||
// Current index in the flowerbed array | ||
let i = 0; | ||
|
||
// Length of the flowerbed array | ||
const length = flowerbed.length; | ||
|
||
// Traverse the flowerbed array | ||
while (i < length) { | ||
// Check if the current position is empty (0) | ||
if (flowerbed[i] === 0) { | ||
// Check if the previous position is empty or out of bounds (left boundary) | ||
const prevEmpty = i === 0 || flowerbed[i - 1] === 0; | ||
|
||
// Check if the next position is empty or out of bounds (right boundary) | ||
const nextEmpty = i === length - 1 || flowerbed[i + 1] === 0; | ||
|
||
// If both previous and next positions are empty (or out of bounds), we can plant a flower here | ||
if (prevEmpty && nextEmpty) { | ||
// Place a flower at the current position | ||
flowerbed[i] = 1; | ||
|
||
// Increment the flower counter | ||
count += 1; | ||
|
||
// If we have planted the required number of flowers, return true | ||
if (count >= n) return true; | ||
|
||
// Move to the position after the next one to avoid adjacent planting | ||
i += 2; | ||
} else { | ||
// Move to the next position if planting is not possible here | ||
i += 1; | ||
} | ||
} else { | ||
// Skip the next position if the current one is already occupied | ||
i += 2; | ||
} | ||
} | ||
|
||
// After traversing the entire flowerbed, check if we have planted enough flowers | ||
return count >= n; | ||
}; |