-
Notifications
You must be signed in to change notification settings - Fork 4
[Bona1122] 25.01.23 #36
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
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| // 방법 1: 매번 slice로 새 배열과 객체를 생성 | ||
| function solution1(want, number, discount) { | ||
| let answer = 0 | ||
| const map = want.reduce((map, name, idx) => { | ||
| map[name] = number[idx] | ||
| return map | ||
| }, {}) | ||
|
|
||
| const check = (sale) => { | ||
| for (let item in map) { | ||
| if (map[item] !== sale[item]) return false | ||
| } | ||
| return true | ||
| } | ||
|
|
||
| for (let i = 0; i <= discount.length - 10; i++) { | ||
| const sliced = discount.slice(i, i + 10) | ||
| const sale = sliced.reduce((map, name) => { | ||
| map[name] = (map[name] || 0) + 1 | ||
| return map | ||
| }, {}) | ||
|
|
||
| if (check(sale)) answer++ | ||
| } | ||
|
|
||
| return answer | ||
| } | ||
|
|
||
| // 방법 2: 슬라이딩 윈도우 | ||
| function solution2(want, number, discount) { | ||
| const need = want.reduce((obj, name, idx) => { | ||
| obj[name] = number[idx] | ||
| return obj | ||
| }, {}) | ||
|
|
||
| // 초기 윈도우(첫 10일) 설정 | ||
| const sale = {} | ||
| for (let i = 0; i < 10; i++) { | ||
| sale[discount[i]] = (sale[discount[i]] || 0) + 1 | ||
| } | ||
|
|
||
| const check = () => { | ||
| for (let item in need) { | ||
| if (need[item] !== sale[item]) return false | ||
| } | ||
| return true | ||
| } | ||
|
|
||
| let answer = check() ? 1 : 0 | ||
|
|
||
| // 슬라이딩 윈도우 이동 | ||
| for (let i = 10; i < discount.length; i++) { | ||
| sale[discount[i]] = (sale[discount[i]] || 0) + 1 // 새로운 것 추가 | ||
| sale[discount[i - 10]]-- // 이전 것 제거 | ||
|
|
||
| if (check()) answer++ | ||
| } | ||
|
|
||
| return answer | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| // 완주자 목록으로 해시맵 생성 | ||
| const solution = (participant, completion) => { | ||
| const completionMap = completion.reduce((map, person) => { | ||
| map[person] = (map[person] || 0) + 1 | ||
| return map | ||
| }, {}) | ||
|
|
||
| return participant.find((person) => { | ||
| if (completionMap[person]) { | ||
| completionMap[person]-- | ||
| return false | ||
| } | ||
| return true | ||
| }) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| // 방법 1: 객체기반 접근(각 숫자의 좌표 직접 매핑 - 하드코딩) | ||
| function solution1(numbers, hand) { | ||
| const keypad = { | ||
| 1: [0, 0], | ||
| 2: [0, 1], | ||
| 3: [0, 2], | ||
| 4: [1, 0], | ||
| 5: [1, 1], | ||
| 6: [1, 2], | ||
| 7: [2, 0], | ||
| 8: [2, 1], | ||
| 9: [2, 2], | ||
| "*": [3, 0], | ||
| 0: [3, 1], | ||
| "#": [3, 2], | ||
| } | ||
|
|
||
| let left = keypad["*"] | ||
| let right = keypad["#"] | ||
|
|
||
| const getDist = ([y1, x1], [y2, x2]) => Math.abs(y1 - y2) + Math.abs(x1 - x2) | ||
|
|
||
| return numbers | ||
| .map((num) => { | ||
| const target = keypad[num] | ||
|
|
||
| // 왼쪽 열이면, 왼손 | ||
| if (target[1] === 0) { | ||
| left = target | ||
| return "L" | ||
| } | ||
| // 오른쪽 열이면, 오른손 | ||
| if (target[1] === 2) { | ||
| right = target | ||
| return "R" | ||
| } | ||
|
|
||
| // 가운데열의 경우, 거리 비교 | ||
| const leftDist = getDist(left, target) | ||
| const rightDist = getDist(right, target) | ||
|
|
||
| if (leftDist === rightDist) { | ||
| if (hand === "right") { | ||
| right = target | ||
| return "R" | ||
| } | ||
| left = target | ||
| return "L" | ||
| } | ||
|
|
||
| if (leftDist < rightDist) { | ||
| left = target | ||
| return "L" | ||
| } else { | ||
| right = target | ||
| return "R" | ||
| } | ||
| }) | ||
| .join("") | ||
| } | ||
|
|
||
| // 방법 2: 2차원 배열로 실제 키패드 모양 표현 후, 좌표 매핑 | ||
| function solution2(numbers, hand) { | ||
| // 키패드를 2차원 배열로 정의 | ||
| const keypad = [ | ||
| [1, 2, 3], | ||
| [4, 5, 6], | ||
| [7, 8, 9], | ||
| ["*", 0, "#"], | ||
| ] | ||
|
|
||
| // 키패드를 좌표로 매핑 | ||
| const getPos = new Map( | ||
| keypad.flatMap((row, y) => row.map((num, x) => [num, [y, x]])) | ||
| ) | ||
|
|
||
| let left = getPos.get("*") | ||
| let right = getPos.get("#") | ||
|
|
||
| const getDist = ([y1, x1], [y2, x2]) => Math.abs(y1 - y2) + Math.abs(x1 - x2) | ||
|
|
||
| return numbers | ||
| .map((num) => { | ||
| const target = getPos.get(num) | ||
|
|
||
| // 왼쪽 열이면, 왼손 | ||
| if (target[1] === 0) { | ||
| left = target | ||
| return "L" | ||
| } | ||
| // 오른쪽 열이면, 오른손 | ||
| if (target[1] === 2) { | ||
| right = target | ||
| return "R" | ||
| } | ||
|
|
||
| // 가운데 열 | ||
| const leftDist = getDist(left, target) | ||
| const rightDist = getDist(right, target) | ||
|
|
||
| if (leftDist === rightDist) { | ||
| if (hand === "right") { | ||
| right = target | ||
| return "R" | ||
| } | ||
| left = target | ||
| return "L" | ||
| } | ||
|
|
||
| if (leftDist < rightDist) { | ||
| left = target | ||
| return "L" | ||
| } else { | ||
| right = target | ||
| return "R" | ||
| } | ||
| }) | ||
| .join("") | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| const solution = (name, yearning, photo) => { | ||
| const map = name.reduce((map, name, idx) => { | ||
| map[name] = yearning[idx] | ||
| return map | ||
| }, {}) | ||
|
|
||
| return photo.map((p) => p.reduce((acc, cur) => (acc += map[cur] || 0), 0)) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| function solution(record) { | ||
| const userInfo = {} | ||
| const result = [] | ||
| const MESSAGES = { | ||
| Enter: "님이 들어왔습니다.", | ||
| Leave: "님이 나갔습니다.", | ||
| } | ||
|
|
||
| record.forEach((r) => { | ||
| const [action, id, name] = r.split(" ") | ||
| if (name) userInfo[id] = name | ||
| if (action !== "Change") { | ||
| result.push([id, MESSAGES[action]]) | ||
| } | ||
| }) | ||
|
|
||
| return result.map(([id, message]) => userInfo[id] + message) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| // 1. 해시맵을 이용한 풀이 | ||
| const solution1 = (nums) => { | ||
| let max = 0 | ||
| const can = nums.length / 2 | ||
| const map = nums.reduce((map, num) => { | ||
| if (map[num] === undefined) { | ||
| max++ | ||
| map[num] = 1 | ||
| } | ||
| return map | ||
| }, {}) | ||
| return can < max ? can : max | ||
|
Collaborator
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. Math.min(can, max) 이 방법도 좋을거 같아요! |
||
| } | ||
|
|
||
| // 2. Set을 이용한 풀이 | ||
| const solution2 = (nums) => { | ||
| const can = nums.length / 2 | ||
| const max = [...new Set(nums)].length | ||
|
|
||
| return can < max ? can : max | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
슬라이딩 윈도우 이번에 찾아보면서 알았는데,
매번 새로운 배열을 생성하지 않고 기존 데이터를 업데이트 하는 방식이 확실히 효율적인거같네요...!
알고갑니다!