-
Notifications
You must be signed in to change notification settings - Fork 126
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
[Jeehay28] WEEK 05 #858
base: main
Are you sure you want to change the base?
[Jeehay28] WEEK 05 #858
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/** | ||
* @param {number[]} prices | ||
* @return {number} | ||
*/ | ||
|
||
// TC : O(n) | ||
// SC : O(1) | ||
|
||
var maxProfit = function (prices) { | ||
if (prices.length === 1) { | ||
return 0; | ||
} | ||
|
||
// Two variables (profitMax and priceMin) are used to store the maximum profit and minimum price seen, which require O(1) space. | ||
let profitMax = 0; | ||
let priceMin = prices[0]; | ||
|
||
for (const price of prices) { | ||
const profit = price - priceMin; | ||
profitMax = Math.max(profit, profitMax); | ||
priceMin = Math.min(price, priceMin); | ||
} | ||
|
||
return profitMax; | ||
}; | ||
|
||
// Why Constants Are Ignored in Big-O | ||
// In Big-O notation, O(2) is simplified to O(1) because constants are irrelevant in asymptotic analysis. | ||
// Big-O focuses on how resource usage scales with input size, not fixed values. | ||
|
||
// Using 2 variables: O(1) | ||
// Using 10 variables: O(1) | ||
// Using 100 variables: O(1) | ||
|
||
// What Space Complexity Looks Like for Larger Growth | ||
// O(n): Memory grows linearly with the input size (e.g., storing an array of n elements). | ||
// O(n^2): Memory grows quadratically (e.g., a 2D matrix with n*n elements). | ||
// 𝑂(log 𝑛): Memory grows logarithmically (e.g., recursive calls in binary search). | ||
// O(1): Fixed memory usage, regardless of input size (e.g., using a fixed number of variables). | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// Guided approach | ||
// TC : O(n*k), where n is the number of strings, and k is the average length of each string. | ||
// SC : O(n*k) | ||
// overal time complexity improved : from O(n * klogk) to O(n * k) | ||
|
||
/** | ||
* Time Complexity Breakdown: | ||
* | ||
* Step | Time Complexity | Explanation | ||
* --------------------------------------- | ------------------- | ---------------------------------------- | ||
* Outer loop over strings (`for` loop) | O(n) | Iterate over each string in the input array `strs`. | ||
* Create key (`createKey`) | O(k) per string | For each string, count character frequencies, with k being the length of the string. | ||
* Map operations (`set` and `get`) | O(1) per string | Inserting and retrieving values from a Map. | ||
* Result array | O(n * k) | Storing grouped anagrams in the result array. | ||
* | ||
* Overall Time Complexity: | O(n * k) | Total time complexity considering all steps. | ||
* | ||
* Space Complexity Breakdown: | ||
* | ||
* Step | Space Complexity | Explanation | ||
* --------------------------------------- | ------------------- | ----------------------------------------- | ||
* Map to store grouped anagrams | O(n * k) | Map stores n groups with each group having at most k characters. | ||
* Auxiliary space for `createKey` | O(1) | The frequency array used to count characters (constant size of 26). | ||
* Space for the result array | O(n * k) | Result array storing n groups of up to k elements. | ||
* | ||
* Overall Space Complexity: | O(n * k) | Total space complexity considering all storage. | ||
*/ | ||
|
||
/** | ||
* @param {string[]} strs | ||
* @return {string[][]} | ||
*/ | ||
|
||
var groupAnagrams = function (strs) { | ||
const createKey = (str) => { | ||
const arr = new Array(26).fill(0); | ||
|
||
for (const ch of str) { | ||
const idx = ch.charCodeAt() - "a".charCodeAt(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 알파벳만 사용할때 유용한 것 같습니다! js에서는 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. charCodeAt()은 문자열에서 특정 문자의 Unicode 값을 숫자로 반환하는 메소드입니다. |
||
arr[idx] += 1; | ||
} | ||
|
||
return arr.join("#"); | ||
}; | ||
|
||
let map = new Map(); | ||
|
||
for (const str of strs) { | ||
const key = createKey(str); | ||
map.set(key, [...(map.get(key) || []), str]); | ||
} | ||
|
||
return Array.from(map.values(map)); | ||
}; | ||
|
||
// *My own approach | ||
|
||
// Time Complexity | ||
// 1. Sorting Each String: | ||
// Sorting a string takes O(k*logk), where k is the length of the string. | ||
// Since we sort each string in the input array of size n, the total cost for sorting is O(n*klogk). | ||
|
||
// 2. Hash Map Operations: | ||
// Inserting into the hash map is O(1) on average. Over n strings, the cost remains O(n). | ||
|
||
// Overall Time Complexity: | ||
// O(n*klogk), where n is the number of strings and k is the average length of a string. | ||
|
||
// /** | ||
// * @param {string[]} strs | ||
// * @return {string[][]} | ||
// */ | ||
|
||
// var groupAnagrams = function (strs) { | ||
// // helper function | ||
// const sorted = (str) => { | ||
// return str.split("").sort().join(""); | ||
// }; | ||
|
||
// let obj = {}; | ||
|
||
// for (const str of strs) { | ||
// const key = sorted(str); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. lower-case alphabet 만 입력으로 주어지기 때문에 sort를 했을 때 같은 anagram이라면 같은 키임을 활용하셨군요! 👍 |
||
// obj[key] = [...(obj[key] || []), str]; | ||
// } | ||
|
||
// return Object.values(obj); | ||
// }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
빅O 노테이션에 대해서 정리해주셨군요 감사합니다 :)