Skip to content

[soobing] WEEK 01 Solutions #1186

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
Apr 5, 2025
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
14 changes: 14 additions & 0 deletions best-time-to-buy-and-sell-stock/soobing.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
function maxProfit(prices: number[]): number {
let minPrice = Infinity;
let maxProfit = 0;
for (let i = 0; i < prices.length; i++) {
if (prices[i] < minPrice) {
minPrice = prices[i];
}

if (prices[i] - minPrice > maxProfit) {
maxProfit = prices[i] - minPrice;
}
}
return maxProfit;
}
10 changes: 10 additions & 0 deletions contains-duplicate/soobing.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
function containsDuplicate(nums: number[]): boolean {
const map = new Map();
for (let i = 0; i < nums.length; i++) {
if (map.get(nums[i])) {
return true;
} else map.set(nums[i], 1);
}

return false;
}
9 changes: 9 additions & 0 deletions maximum-subarray/soobing.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
function maxSubArray(nums: number[]): number {
let currentSum = nums[0];
let maxSum = nums[0];
for (let i = 1; i < nums.length; i++) {
currentSum = Math.max(nums[i], currentSum + nums[i]);
maxSum = Math.max(maxSum, currentSum);
}
return maxSum;
}
19 changes: 19 additions & 0 deletions product-of-array-except-self/soobing.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
function productExceptSelf(nums: number[]): number[] {
const left = Array(nums.length).fill(1);
const right = Array(nums.length).fill(1);
const result = Array(nums.length);

for (let i = 1; i < nums.length; i++) {
left[i] = left[i - 1] * nums[i - 1];
}

for (let i = nums.length - 2; i >= 0; i--) {
right[i] = right[i + 1] * nums[i + 1];
}

for (let i = 0; i < nums.length; i++) {
result[i] = left[i] * right[i];
}

return result;
}
27 changes: 27 additions & 0 deletions two-sum/soobing.ts
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

다양하게 풀어보는 점이 좋았습니다!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

헉 스케줄이 있는지 몰랐습니다...!! 알려주셔서 감사해요!!

Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// 1. Brute force
function twoSum(nums: number[], target: number): number[] {
for (let i = 0; i < nums.length - 1; i++) {
for (let j = i + 1; j < nums.length; j++) {
if (nums[i] + nums[j] === target) {
return [i, j];
}
}
}
return [];
}

// 2. Hashmap
function twoSum(nums: number[], target: number): number[] {
const map = new Map();
for (let i = 0; i < nums.length; i++) {
map.set(nums[i], i);
}

for (let i = 0; i < nums.length; i++) {
const targetIndex = map.get(target - nums[i]);
if (targetIndex && targetIndex !== i) {
return [i, targetIndex];
}
}
return [];
}