-
-
Notifications
You must be signed in to change notification settings - Fork 301
[Blossssom] WEEK 01 solutions #2009
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
Changes from all commits
a0fc481
d905857
9f3340b
a3db8f6
672830c
5e6afc6
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 nums - 정수 배열 | ||
| * @returns - 배열에 같은 값이 2번 이상 반복 되면 true 아님 false | ||
| * | ||
| * @description | ||
| * 1. Set으로 길이 비교 후 길이에 따라 반환 O(n) -> Set 생성 시 입력된 배열 요소 순회 | ||
| * 2. Map으로 간단하게 체크 만약 has 시 true 반환 O(n) | ||
| * 3. Map, Set 없이 객체로 체크 O(n) | ||
| */ | ||
|
|
||
| function containsDuplicate(nums: number[]): boolean { | ||
| return nums.length !== new Set(nums).size ? true : false; | ||
| } | ||
|
|
||
| // function containsDuplicate(nums: number[]): boolean { | ||
| // const map = new Map(); | ||
| // for (const num of nums) { | ||
| // if (map.has(num)) { | ||
| // return true; | ||
| // } | ||
| // map.set(num, 1); | ||
| // } | ||
| // return false; | ||
| // } | ||
|
|
||
| // function containsDuplicate(nums: number[]): boolean { | ||
| // const obj: Record<number, number> = {}; | ||
|
|
||
| // for (const num of nums) { | ||
| // obj[num] = (obj[num] || 0) + 1; | ||
| // } | ||
|
|
||
| // return Object.keys.length !== nums.length ? true : false; | ||
| // } | ||
|
|
||
| const exam01 = containsDuplicate([1, 2, 3, 1]); | ||
| const exam02 = containsDuplicate([1, 2, 3, 4]); | ||
|
|
||
| console.log(exam01, exam02); | ||
|
|
|
Contributor
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. 👍 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| /** | ||
| * @description - 같은 날 인접한 두 집에 침입 시 자동으로 신고됨 | ||
| * @param nums - 각 집의 금액을 나타내는 정수 배열 | ||
| * @returns - 경찰에 신고되지 않고 훔칠 수 있는 최대 금액 | ||
| * | ||
| * @description | ||
| * - 1. tabulation 방식 - O(n) | ||
| */ | ||
| // function rob(nums: number[]): number { | ||
| // if (nums.length < 2) { | ||
| // return nums[0]; | ||
| // } | ||
|
|
||
| // const dpTable: Array<number> = Array.from({ length: nums.length }, () => 0); | ||
| // dpTable[0] = nums[0]; | ||
| // dpTable[1] = Math.max(nums[0], nums[1]); | ||
|
|
||
| // for (let i = 2; i < nums.length; i++) { | ||
| // dpTable[i] = Math.max(nums[i] + dpTable[i - 2], dpTable[i - 1]); | ||
| // } | ||
|
|
||
| // return Math.max(...dpTable); | ||
| // } | ||
|
|
||
| function rob(nums: number[]): number { | ||
| const memo: Array<number> = Array.from({ length: nums.length }, () => -1); | ||
|
|
||
| function solve(i: number): number { | ||
| if (i < 0) { | ||
| return 0; | ||
| } | ||
|
|
||
| if (memo[i] !== -1) { | ||
| return memo[i]; | ||
| } | ||
|
|
||
| const robCurrent = nums[i] + solve(i - 2); | ||
| const skipCurrent = solve(i - 1); | ||
| memo[i] = Math.max(robCurrent, skipCurrent); | ||
| return memo[i]; | ||
| } | ||
| return solve(nums.length - 1); | ||
| } | ||
|
|
|
Contributor
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. 👍 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| /** | ||
| * @param nums - 정렬되지 않은 정수 배열 | ||
| * @returns - 가장 긴 연속 요소 길이 | ||
| * | ||
| * @description | ||
| * 1. 객체의 정렬 활용 방식 - O(n log n) -> 결국 key 정렬 시 n log n 발생 | ||
| * 2. 정렬 없이 key find 방식 - O(n) -> 시작점일 경우만 판단, 모든 num에 대해 돌지 않음 | ||
| */ | ||
|
|
||
| // function longestConsecutive(nums: number[]): number { | ||
| // const obj = nums.reduce((acc, cur) => { | ||
| // acc[cur] = (acc[cur] || 0) + 1; | ||
| // return acc; | ||
| // }, {}); | ||
| // let cnt = 1; | ||
| // let answer = 0; | ||
|
|
||
| // const keys = Object.keys(obj); | ||
|
|
||
| // for (let i = 1; i < keys.length; i++) { | ||
| // if (Number(keys[i]) === Number(keys[i - 1]) + 1) { | ||
| // cnt++; | ||
| // } else { | ||
| // answer = Math.max(answer, cnt); | ||
| // cnt = 1; | ||
| // } | ||
| // } | ||
|
|
||
| // answer = Math.max(answer, cnt); | ||
|
|
||
| // return answer; | ||
| // } | ||
|
|
||
| function longestConsecutive(nums: number[]): number { | ||
| const numSet = new Set(nums); | ||
| let answer = 0; | ||
|
|
||
| for (const num of numSet) { | ||
| if (!numSet.has(num - 1)) { | ||
| let current = num; | ||
| let cnt = 1; | ||
|
|
||
| while (numSet.has(current + 1)) { | ||
| current++; | ||
| cnt++; | ||
| } | ||
|
|
||
| answer = Math.max(answer, cnt); | ||
| } | ||
| } | ||
| return answer; | ||
| } | ||
|
|
|
Contributor
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. 👍 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| /** | ||
| * @param nums - 정수 배열 | ||
| * @param k - 자주 등장한 요소의 길이 | ||
| * @returns - 자주 등장한 요소들 [] | ||
| */ | ||
| function topKFrequent(nums: number[], k: number): number[] { | ||
| const map = new Map(); | ||
| for (const num of nums) { | ||
| if (map.has(num)) { | ||
| map.set(num, map.get(num) + 1); | ||
| } else { | ||
| map.set(num, 1); | ||
| } | ||
| } | ||
|
|
||
| const sorted = Array.from(map) | ||
| .sort((a, b) => b[1] - a[1]) | ||
| .slice(0, k) | ||
| .map((v) => v[0]); | ||
| return sorted; | ||
| } | ||
|
|
||
| topKFrequent([1, 1, 1, 2, 2, 3], 2); | ||
|
|
|
Contributor
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. 저도 처음에 반복문을 사용했습니다! 👍 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| /** | ||
| * @param nums - 정수 배열 | ||
| * @param target - nums 값을 더해 나올 값 | ||
| * @returns - target을 만드는 index 값 | ||
| * | ||
| * @description | ||
| * 1. 동일한 요소 두번 사용 금지 | ||
| * 2. 각 입력에 대해 명확한 하나의 솔루션이 있다고 가정 | ||
| * | ||
| * @answer1 | ||
| * - O(n^2) | ||
| * | ||
| * @answer2 | ||
| * - O(n) | ||
| */ | ||
|
|
||
| // 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 []; | ||
| // } | ||
|
|
||
| function twoSum(nums: number[], target: number): number[] { | ||
| const map = new Map(); | ||
|
|
||
| for (let i = 0; i < nums.length; i++) { | ||
| if (map.has(target - nums[i])) { | ||
| return [map.get(target - nums[i]), i]; | ||
| } | ||
| map.set(nums[i], i); | ||
| } | ||
| return []; | ||
| } | ||
|
|
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.
다음 주차에는 상단에 시간 복잡도와 공간 복잡도 함께 기입 부탁드려요!
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.
넵 더 자세히 적어서 기입하는 습관을 들여야겠네요 ㅎㅎㅎ 리뷰 달아주셔서 너무 감사합니다!