From e39c2e5e6caac828c36bb6d6ba533476b188be25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=84=A0=EB=AF=BC?= Date: Mon, 26 Aug 2024 23:59:04 +0900 Subject: [PATCH 1/3] Add week 3 solutions: two-sum --- two-sum/gitsunmin.ts | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 two-sum/gitsunmin.ts diff --git a/two-sum/gitsunmin.ts b/two-sum/gitsunmin.ts new file mode 100644 index 000000000..23863c12d --- /dev/null +++ b/two-sum/gitsunmin.ts @@ -0,0 +1,16 @@ +/** + * https://leetcode.com/problems/two-sum + * time complexity : O(n) + * space complexity : O(n) + */ +function twoSum(nums: number[], target: number): number[] { + const m = new Map(); + + for (let i = 0; i < nums.length; i++) { + if (m.has(nums[i])) + return [m.get(nums[i]), i]; + m.set(target - nums[i], i); + } + + return [-1, -1]; +}; From 65ce8e5cb807cd34d60258d13efeedc032a9be75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=84=A0=EB=AF=BC?= Date: Tue, 27 Aug 2024 19:54:38 +0900 Subject: [PATCH 2/3] Add week 3 solutions: climbing-stairs --- climbing-stairs/gitsunmin.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 climbing-stairs/gitsunmin.ts diff --git a/climbing-stairs/gitsunmin.ts b/climbing-stairs/gitsunmin.ts new file mode 100644 index 000000000..e87319fa4 --- /dev/null +++ b/climbing-stairs/gitsunmin.ts @@ -0,0 +1,17 @@ +/** + * https://leetcode.com/problems/climbing-stairs + * time complexity : O(n) + * space complexity : O(1) + */ + +export const upStairs = (n: number): number => { + let [l, r] = [1, 2]; + for (let i = 3; i <= n; i++) [l, r] = [r, l + r]; + + return r; +}; + +export function climbStairs(n: number): number { + if (n <= 2) return n; + return upStairs(n); +}; From 788607c6f9b017f7cb4d61a8f5a04f9abe8bf0d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=84=A0=EB=AF=BC?= Date: Fri, 30 Aug 2024 23:51:30 +0900 Subject: [PATCH 3/3] Add week 3 solutions: product-of-array-except-self --- product-of-array-except-self/gitsunmin.ts | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 product-of-array-except-self/gitsunmin.ts diff --git a/product-of-array-except-self/gitsunmin.ts b/product-of-array-except-self/gitsunmin.ts new file mode 100644 index 000000000..3d06fa5c3 --- /dev/null +++ b/product-of-array-except-self/gitsunmin.ts @@ -0,0 +1,23 @@ +/** + * https://leetcode.com/problems/product-of-array-except-self + * time complexity : O(n) + * space complexity : O(1) + */ +function productExceptSelf(nums: number[]): number[] { + const n = nums.length; + const answer = new Array(n).fill(1); + + let leftProduct = 1; + for (let i = 0; i < n; i++) { + answer[i] = leftProduct; + leftProduct *= nums[i]; + } + + let rightProduct = 1; + for (let i = n - 1; i >= 0; i--) { + answer[i] *= rightProduct; + rightProduct *= nums[i]; + } + + return answer; +}