Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0df4a19

Browse files
Merge pull request #7 from jaewon1676/main
2022年03月28日(월) jaewon1676 풀이 5문제 추가, 파일명 변경
2 parents 889cb74 + 025311c commit 0df4a19

File tree

6 files changed

+147
-25
lines changed

6 files changed

+147
-25
lines changed

‎level-1/K번째수.js‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,17 @@ function solution(array, commands) {
88
return arrCmd[cmd[2] - 1]
99
})
1010
return answer;
11-
}
11+
}
12+
13+
//정답 2 - jaewon1676
14+
function solution(array, commands) {
15+
var result = [];
16+
var temp = [];
17+
for(var i=0; i<commands.length; i++){
18+
temp = array.slice(commands[i][0] - 1, commands[i][1]).sort((a,b) => {return a - b})
19+
console.log(temp)
20+
result.push(temp[commands[i][2]-1])
21+
}
22+
return result
23+
}
24+

‎level-1/시저-암호.js‎

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,26 @@ function solution(s, n) {
1313
}
1414
}).join('')
1515
return answer;
16-
}
16+
}
17+
18+
//정답 2 - jaewon1676
19+
function solution(s, n) {
20+
return s.split("").map((el) => {
21+
if (el == " ") return el;
22+
let tmp = el.charCodeAt()
23+
return el.toLowerCase().charCodeAt()+n > 122
24+
? String.fromCharCode(tmp+n-26)
25+
: String.fromCharCode(tmp+n)
26+
}).join('')
27+
}
28+
/* 문자열 -> 아스키코드 : s.charCodeAt()
29+
아스키코드 -> 문자열 : String.fromCharCode()
30+
풀이과정
31+
1. 배열을 문자열로 쪼개서 map 메서드로 하나씩 순회한다., 문자열이 비어있으면 리턴한다.
32+
2. 알파벳을 통일 하기 위해 소문자 아스키코드로 변환한다.
33+
대문자 아스키코드는 65가 A, 90이 Z이다.
34+
소문자 아스키코드는 97이 a, 122가 z이다.
35+
3. 변환한 아스키코드 + n이 122이상이면 알파벳 단어 길이 25 + 알파벳 앞으로 이동 1만큼 뺴준고
36+
그렇지 않으면 그대로 더한 후 해당 아스키코드의 알파벳으로 변환해준다.
37+
*/
38+

‎level-1/제일-작은-수-제거하기.js‎

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,41 @@ function solution(arr) {
77
arr.splice(index, 1)
88
answer = arr.length === 0 ? [-1] : arr
99
return answer;
10-
}
10+
}
11+
12+
//정답 2 - jaewon1676
13+
function solution(arr) {
14+
var answer = [];
15+
16+
let min = Math.min(...arr)
17+
while(1){
18+
for (let i=0; i<arr.length; i++){
19+
if (min == arr[i]){
20+
arr.splice(i, 1)
21+
if (arr.length == 0){
22+
return [-1]
23+
}
24+
else {
25+
return arr;
26+
}
27+
}
28+
}
29+
}
30+
return -1
31+
}
32+
/* 풀이과정
33+
1. 배열의 최솟값을 min 변수에 담아줍니다.
34+
2. 배열을 순회하며 최솟값을 찾아 splice 메서드를 사용해 최솟값의 자리만 제거해줍니다.
35+
3. 최솟값 제거 후 배열 크기가 0이면 -1, 그렇지 않으면 배열 그대로 반환합니다. */
36+
37+
//정답 3 - jaewon1676
38+
function solution(arr) {
39+
arr.splice(arr.indexOf(Math.min(...arr)),1);
40+
if (arr.length < 1) return [-1];
41+
return arr;
42+
}
43+
/* 풀이과정 arr [4,3,2,1] 일경우
44+
Math.min(...arr)은 1
45+
arr.indexOf(1), 1이 있는 index가 3이므로 3 반환.
46+
arr.splice(3, 1) arr의 3번째 index만 제거 해준다.
47+
arr의 크기가 1보다 작으면 -1, 그렇지 않으면 그대로 반환. */

‎level-1/폰켓몬.js‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,17 @@ function solution(nums) {
88
const setLen = numSet.size
99
answer = numLen / 2 >= setLen ? setLen : numLen / 2
1010
return answer;
11-
}
11+
}
12+
13+
//정답 2 - jaewon1676
14+
function solution(nums) {
15+
let max = nums.length / 2; // N / 2
16+
let set = [...new Set(nums)] // 중복을 없앤다.
17+
return set.length > max ? max : set.length
18+
}
19+
/* 풀이 과정
20+
1. 많은 종류의 폰켓몬을 포함해서 N/2마리 선택해야한다.
21+
2. 같은 숫자는 같은 종류이므로 set을 활용해 중복을 없애고 진행한다.
22+
3. 최대로 고를 수 있는 폰켓몬 수는 N / 2마리가 set의 길이보다 크냐 작냐에 따라
23+
두가지 경우의 수로 좁혀진다. */
24+

‎level-2/[1차]-캐시.js‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
//https://github.com/codeisneverodd/programmers-coding-test
2+
//완벽한 정답이 아닙니다.
3+
//정답 1 - codeisneverodd
4+
function solution(cacheSize, cities) {
5+
var answer = 0;
6+
let cache = []
7+
if (cacheSize === 0) return 5 * cities.length
8+
for (const city of cities) {
9+
const cityLC = city.toLowerCase()
10+
if (cache.includes(cityLC)) {
11+
cache.splice(cache.indexOf(cityLC), 1)
12+
cache.unshift(cityLC)
13+
answer += 1
14+
} else {
15+
if (cache.length >= cacheSize) cache.pop()
16+
cache.unshift(cityLC)
17+
answer += 5
18+
}
19+
}
20+
return answer;
21+
}
22+
23+
//정답 2 - jaewon1676
24+
function solution(cacheSize, cities) {
25+
var answer = 0;
26+
let cache = [];
27+
28+
//캐시 크기가 0인 경우는 따로 처리
29+
if (cacheSize === 0) return cities.length * 5;
30+
31+
while (cities.length != 0) {
32+
// 맨 앞의 배열의 요소 히나를 소문자로 변환해서 city에 넣는다.
33+
const city = cities.shift().toLowerCase();
34+
// cities의 요소 city가 캐시 안에 있는지 비교한다. (hit or miss)
35+
if (cache.includes(city)) {
36+
// 캐시 안에 있으면 그 위치에 있는 캐시를 빼주고,
37+
cache.splice(cache.indexOf(city), 1);
38+
// 맨 뒤로 push 해준다.
39+
cache.push(city);
40+
// cache hit
41+
answer += 1;
42+
} else { // 캐시 크기가 꽉 차있으면 캐시 맨 앞에 요소를 하나 빼준다.
43+
if (cache.length === cacheSize) {
44+
cache.shift();
45+
}
46+
// 새로운 캐시 맨 뒤로 push
47+
cache.push(city);
48+
// cache miss
49+
answer += 5;
50+
}
51+
}
52+
return answer;
53+
}
54+
/* LRU 알고리즘
55+
n이 배열 안에 있으면 배열 안의 n을 빼주고, 새 n을 배열의 맨 뒤로 push 한다.
56+
n이 배열 안에 없으면 n을 배열의 맨 뒤로 push 한다. 이때 배열의 크기 여유가 없으면
57+
배열에서 가장 오래된 요소를 하나 뺴준다. (arr.shift()) */
58+

‎level-2/[1차]-캐시.js ‎

Lines changed: 0 additions & 21 deletions
This file was deleted.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /