-
Notifications
You must be signed in to change notification settings - Fork 98
2022年03月28日(월) jaewon1676 풀이 5문제 추가, 파일명 변경 #7
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
5 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
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 |
---|---|---|
|
@@ -13,4 +13,26 @@ function solution(s, n) { | |
} | ||
}).join('') | ||
return answer; | ||
} | ||
} | ||
|
||
//정답 2 - jaewon1676 | ||
function solution(s, n) { | ||
return s.split("").map((el) => { | ||
if (el == " ") return el; | ||
let tmp = el.charCodeAt() | ||
return el.toLowerCase().charCodeAt()+n > 122 | ||
? String.fromCharCode(tmp+n-26) | ||
: String.fromCharCode(tmp+n) | ||
}).join('') | ||
} | ||
/* 문자열 -> 아스키코드 : s.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. 주석을 정말 잘 달아주셨네요! 감사합니다 😁 |
||
아스키코드 -> 문자열 : String.fromCharCode() | ||
풀이과정 | ||
1. 배열을 문자열로 쪼개서 map 메서드로 하나씩 순회한다., 문자열이 비어있으면 리턴한다. | ||
2. 알파벳을 통일 하기 위해 소문자 아스키코드로 변환한다. | ||
대문자 아스키코드는 65가 A, 90이 Z이다. | ||
소문자 아스키코드는 97이 a, 122가 z이다. | ||
3. 변환한 아스키코드 + n이 122이상이면 알파벳 단어 길이 25 + 알파벳 앞으로 이동 1만큼 뺴준고 | ||
그렇지 않으면 그대로 더한 후 해당 아스키코드의 알파벳으로 변환해준다. | ||
*/ | ||
|
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
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
58 changes: 58 additions & 0 deletions
level-2/[1차]-캐시.js
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,58 @@ | ||
//https://github.com/codeisneverodd/programmers-coding-test | ||
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. 허걱 공백 제거 감사합니다! 😄 |
||
//완벽한 정답이 아닙니다. | ||
//정답 1 - codeisneverodd | ||
function solution(cacheSize, cities) { | ||
var answer = 0; | ||
let cache = [] | ||
if (cacheSize === 0) return 5 * cities.length | ||
for (const city of cities) { | ||
const cityLC = city.toLowerCase() | ||
if (cache.includes(cityLC)) { | ||
cache.splice(cache.indexOf(cityLC), 1) | ||
cache.unshift(cityLC) | ||
answer += 1 | ||
} else { | ||
if (cache.length >= cacheSize) cache.pop() | ||
cache.unshift(cityLC) | ||
answer += 5 | ||
} | ||
} | ||
return answer; | ||
} | ||
|
||
//정답 2 - jaewon1676 | ||
function solution(cacheSize, cities) { | ||
var answer = 0; | ||
let cache = []; | ||
|
||
//캐시 크기가 0인 경우는 따로 처리 | ||
if (cacheSize === 0) return cities.length * 5; | ||
|
||
while (cities.length != 0) { | ||
// 맨 앞의 배열의 요소 히나를 소문자로 변환해서 city에 넣는다. | ||
const city = cities.shift().toLowerCase(); | ||
// cities의 요소 city가 캐시 안에 있는지 비교한다. (hit or miss) | ||
if (cache.includes(city)) { | ||
// 캐시 안에 있으면 그 위치에 있는 캐시를 빼주고, | ||
cache.splice(cache.indexOf(city), 1); | ||
// 맨 뒤로 push 해준다. | ||
cache.push(city); | ||
// cache hit | ||
answer += 1; | ||
} else { // 캐시 크기가 꽉 차있으면 캐시 맨 앞에 요소를 하나 빼준다. | ||
if (cache.length === cacheSize) { | ||
cache.shift(); | ||
} | ||
// 새로운 캐시 맨 뒤로 push | ||
cache.push(city); | ||
// cache miss | ||
answer += 5; | ||
} | ||
} | ||
return answer; | ||
} | ||
/* LRU 알고리즘 | ||
n이 배열 안에 있으면 배열 안의 n을 빼주고, 새 n을 배열의 맨 뒤로 push 한다. | ||
n이 배열 안에 없으면 n을 배열의 맨 뒤로 push 한다. 이때 배열의 크기 여유가 없으면 | ||
배열에서 가장 오래된 요소를 하나 뺴준다. (arr.shift()) */ | ||
|
21 changes: 0 additions & 21 deletions
level-2/[1차]-캐시.js
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.