본문 바로가기

알고리즘

프로그래머스 삼각 달팽이 (코틀린)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
class Solution {
   fun solution(n : Int): ArrayList<Int> {
    var num = 2 //현재 번호
    val result = arrayListOf<Int>()
    var currentLayer = 2 //현재 층
    var index = 1
    for(i in 0 until lenOfN(n)){
        result.add(0)
    }
    result[0= 1
    var currentMove = "왼쪽 밑" //"왼쪽 밑", "오른쪽", "왼쪽 위"
    for(i in 1 until lenOfN(n)) {
        when(currentMove) {
            "왼쪽 밑" -> {
                if (index + currentLayer < result.size && result[index + currentLayer] == 0) {
                    currentMove = "왼쪽 밑"
                    result[index] = num
                    index += currentLayer
                    currentLayer++
                    num++
                }
                else if (index + 1 < result.size && result[index + 1== 0) {
                    currentMove = "오른쪽"
                    result[index] = num
                    index++
                    num++
                }
                else if (index - currentLayer > 0 && result[index - currentLayer] == 0) {
                    currentMove = "왼쪽 위"
                    result[index] = num
                    index -= currentLayer
                    currentLayer--
                    num++
                }
            }
            "오른쪽" -> {
                if (index + 1 < result.size && result[index + 1== 0) {
                    currentMove = "오른쪽"
                    result[index] = num
                    index++
                    num++
                }
                else if (index - currentLayer > 0 && result[index - currentLayer] == 0) {
                    currentMove = "왼쪽 위"
                    result[index] = num
                    index -= currentLayer
                    currentLayer--
                    num++
                }
                else if (index + currentLayer < result.size && result[index + currentLayer] == 0) {
                    currentMove = "왼쪽 밑"
                    result[index] = num
                    index += currentLayer
                    currentLayer++
                    num++
                }
            }
            "왼쪽 위" -> {
                if (index - currentLayer > 0 && result[index - currentLayer] == 0) {
                    currentMove = "왼쪽 위"
                    result[index] = num
                    index -= currentLayer
                    currentLayer--
                    num++
                }
                else if (index + currentLayer < result.size && result[index + currentLayer] == 0) {
                    currentMove = "왼쪽 밑"
                    result[index] = num
                    index += currentLayer
                    currentLayer++
                    num++
                }
                else if (index + 1 < result.size && result[index + 1== 0) {
                    currentMove = "오른쪽"
                    result[index] = num
                    index++
                    num++
                }
            }
        }
    }
    for(i in 0 until result.size) {
        if(result[i] == 0) result[i] = num
    }
    return result
}
 
    fun lenOfN(n : Int) : Int{
        if(n == 1return 1
        return n + lenOfN(n-1)
    }
}
cs

어떤 방식으로 풀어야 할지 감을 잡는게 중요한 것 같다.

여러 알고리즘 풀이를 보며 어떤 방식으로 풀어야 할지 익숙해져보자!