Skip to content

[Evan] Week 1 Assignment Submission #41

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions best-time-to-buy-and-sell-stock/sounmind.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
function maxProfit(prices: number[]): number {
let minPrice = Number.MAX_SAFE_INTEGER;
let maxProfit = 0;

for (let i = 0; i < prices.length; i++) {
if (prices[i] < minPrice) {
minPrice = prices[i];
}

const potentialProfit = prices[i] - minPrice;

if (maxProfit < potentialProfit) {
maxProfit = potentialProfit;
}
}

return maxProfit;
}
3 changes: 3 additions & 0 deletions contains-duplicate/sounmind.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function containsDuplicate(nums: number[]): boolean {
return nums.length !== new Set(nums).size;
}
24 changes: 24 additions & 0 deletions two-sum/sounmind.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
function twoSum(nums: number[], target: number): number[] {
// Pair each element with its index
const numsWithIndex = nums.map((value, index) => ({ value, index }));

// Sort the array based on the values
numsWithIndex.sort((a, b) => a.value - b.value);

let left = 0;
let right = numsWithIndex.length - 1;

while (left < right) {
const sum = numsWithIndex[left].value + numsWithIndex[right].value;

if (sum === target) {
return [numsWithIndex[left].index, numsWithIndex[right].index];
} else if (sum < target) {
left++;
} else {
right--;
}
}

return []; // In case there is no solution
}
25 changes: 25 additions & 0 deletions valid-anagram/sounmind.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
function isAnagram(s: string, t: string): boolean {
if (s.length !== t.length) {
return false;
}

const frequencyMap = {};

for (const letter of s) {
if (frequencyMap[letter]) {
frequencyMap[letter] += 1;
} else {
frequencyMap[letter] = 1;
}
}

for (const letter of t) {
if (frequencyMap[letter]) {
frequencyMap[letter] -= 1;
} else {
return false;
}
}

return true;
}
16 changes: 16 additions & 0 deletions valid-palindrome/sounmind.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
function isPalindrome(s: string): boolean {
const filteredString = s.replace(/[^a-zA-Z0-9]/g, "").toUpperCase();

let [left, right] = [0, filteredString.length - 1];

while (left <= right) {
if (filteredString[left] !== filteredString[right]) {
return false;
}

left += 1;
right -= 1;
}

return true;
}