Skip to content

Commit 185fb88

Browse files
author
gyeong-hyeon-kim
committed
Solved 3 problems
1 parent ab93268 commit 185fb88

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

level-2/문자열-압축.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//https://github.com/codeisneverodd/programmers-coding-test
2+
//완벽한 정답이 아닙니다.
3+
function solution(s) {
4+
var answer = 0;
5+
let lengthArr = []
6+
for(let i=1; i<=s.length; i++) lengthArr.push(compressedString(s, i).length)
7+
answer = Math.min(...lengthArr)
8+
return answer;
9+
}
10+
function compressedString(str, unitNum){
11+
let count = 1
12+
let result = ['']
13+
for(let repeat=0; repeat<=str.length/unitNum; repeat++) {
14+
const slicedGroup = str.slice(unitNum * repeat, unitNum * repeat+ unitNum)
15+
if (result[result.length - 1] === slicedGroup) {
16+
count++
17+
} else {
18+
if(count > 1) result[result.length - 1] = count + result[result.length - 1]
19+
result.push(slicedGroup)
20+
count = 1
21+
}
22+
}
23+
return result.join('')
24+
}

level-2/영어-끝말잇기.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//https://github.com/codeisneverodd/programmers-coding-test
2+
//완벽한 정답이 아닙니다.
3+
function solution(n, words) {
4+
var answer = [];
5+
let turn = 1
6+
for(let i=1; i<words.length; i++){
7+
let pass = (words[i][0] === words[i-1][words[i-1].length-1]) && !(words.slice(0, i).includes(words[i]))
8+
if(i % n === 0) turn++
9+
if(!pass) return [i % n + 1, turn]
10+
}
11+
return [0,0];
12+
}
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//https://github.com/codeisneverodd/programmers-coding-test
2+
//완벽한 정답이 아닙니다.
3+
function solution(rows, columns, queries) {
4+
var answer = [];
5+
let matrix = new Array(rows)
6+
for(let i=0; i<rows; i++) matrix[i] = new Array(columns)
7+
for(let i=0; i<rows; i++) for(let j=0; j<columns; j++) matrix[i][j] = i * columns + j + 1
8+
for(const query of queries){
9+
let order = []
10+
const [row1,col1,row2,col2] = [query[0]-1, query[1]-1,query[2]-1,query[3]-1]
11+
//fill order (row1->row2, col1->col2, row2->row2, col2->col1)
12+
for(let i=row1; i<=row2; i++) order.push(matrix[i][col1])
13+
for(let i=col1+1; i<=col2; i++) order.push(matrix[row2][i])
14+
for(let i=row2-1; i>=row1; i--) order.push(matrix[i][col2])
15+
for(let i=col2-1; i>col1; i--) order.push(matrix[row1][i])
16+
//rotate clockwise
17+
order.push(order.shift())
18+
answer.push(Math.min(...order))
19+
//change value in matrix
20+
for(let i=row1; i<=row2; i++) matrix[i][col1] = order.shift()
21+
for(let i=col1+1; i<=col2; i++) matrix[row2][i] = order.shift()
22+
for(let i=row2-1; i>=row1; i--) matrix[i][col2] = order.shift()
23+
for(let i=col2-1; i>col1; i--) matrix[row1][i] = order.shift()
24+
}
25+
return answer;
26+
}

0 commit comments

Comments
 (0)