๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm/Kotlin

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค • ์ฝ”ํ‹€๋ฆฐ] ์นด๋“œ ๋ญ‰์น˜ #159994

by ํ‚ค์œค 2023. 12. 19.

#159994

๐ŸŽ„ Question ?

https://school.programmers.co.kr/learn/courses/30/lessons/159994

class Solution {
    fun solution(cards1: Array<String>, cards2: Array<String>, goal: Array<String>): String {
        var answer: String = ""
        return answer
    }
}

๐Ÿงฉ Thought Process

  1. card1๊ณผ card2์˜ ์ฒซ๋ฒˆ์งธ ์ž๋ฃŒํ˜•๋ถ€ํ„ฐ ์ฐจ๊ทผ์ฐจ๊ทผ goal ์˜ ์ฒซ๋ฒˆ์งธ ์ž๋ฃŒํ˜•๊ณผ ๋น„๊ตํ•ด ์ค€๋‹ค.
  2. ๋งž๋Š” ์ž๋ฃŒํ˜•์˜ ์ธ๋ฑ์Šค๋ฅผ 1๋งŒํผ ์˜ฌ๋ ค์ค€๋‹ค.
  3. ๋งŒ์•ฝ ๋‘˜๋‹ค ๋ฌธ์ž์—ด์ด ์ผ์น˜ํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด No ๋ฌธ์ž์—ด์„ ๋ฆฌํ„ดํ•ด์ค€๋‹ค.

๐ŸŽ€ Answer

class Solution {
    fun solution(cards1: Array<String>, cards2: Array<String>, goal: Array<String>): String {
        var answer: String = ""
        var n = 0
        var m = 0
        for (i in goal.indices) {
            if (goal[i] == cards1[n]) {
                n = checkRange(n, cards1.size)
                
            } else if (goal[i] == cards2[m]) {
                m = checkRange(m, cards2.size)
            } else {
                return "No"
            }
        }
        return "Yes"
    }
    
    fun checkRange(n: Int, m: Int): Int {
        if (n<m-1) {
                    return n+1
                } else {
                    return 0
                }
    }
}

 

๐ŸŽ Result

๐Ÿ† Comment