TIL #63

loci·2024년 7월 2일
0

TIL

목록 보기
61/103

숫자 변환하기
x에 n을 더하거나 2를 곱하거나 x를 곱할 수 있을때 최소의 연산횟수를 구해야 한다.

bfs로 풀어야 한다고 하는데 아직 bfs나 dfs의 개념이 부족해 좀 더 공부해야겠다.


코드

class Solution {
    fun solution(x: Int, y: Int, n: Int): Int {
        if (x == y) return 0

        val visited = mutableSetOf<Int>()
        val queue: Queue<Pair<Int, Int>> = LinkedList()
        queue.add(Pair(x, 0))  // (현재 숫자, 연산 횟수)

        while (queue.isNotEmpty()) {
            val (current, operations) = queue.poll()

            if (current == y) return operations
            if (current > y) continue

            // 이미 방문한 숫자는 건너뜀
            if (current in visited) continue
            visited.add(current)

            // 가능한 연산들을 큐에 추가
            queue.add(Pair(current + n, operations + 1))
            queue.add(Pair(current * 2, operations + 1))
            queue.add(Pair(current * 3, operations + 1))
        }

        return -1  // y에 도달할 수 없는 경우
    }
}

editText의 모양을 바꾸려면 background에 shape을 넣어줘야한다.

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true">
        <shape android:shape="rectangle">
            <stroke android:width="1dp" android:color="#5555FF" />
            <corners android:radius="4dp" />
            <solid android:color="#FFFFFF" />
        </shape>
    </item>

    <item android:state_focused="false">
        <shape android:shape="rectangle">
            <stroke android:width="1dp" android:color="#EEEEEE" />
            <corners android:radius="10dp" />
            <solid android:color="#FFFFFF" />
        </shape>

    </item>
</selector>
profile
편리한 개발자

0개의 댓글