Notice
Recent Posts
Recent Comments
Today
Total
01-10 18:14
Link
๊ด€๋ฆฌ ๋ฉ”๋‰ด

Partially Committed

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์นด๋“œ๋ญ‰์น˜ (JAVA) ๋ณธ๋ฌธ

๐Ÿ”ฅ Algorithm || ๋ฌธ์ œํ’€์ด/PS

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์นด๋“œ๋ญ‰์น˜ (JAVA)

WonderJay 2023. 2. 20. 19:53
728x90
๋ฐ˜์‘ํ˜•
SMALL

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

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”.

programmers.co.kr

์š”์ฆ˜ ๋ฐฑ์ค€์—์„œ๋งŒ ๋ฌธ์ œ๋ฅผ ํ’€๊ณ  ์žˆ๋Š”๋ฐ ์™ธ๋ถ€ IDE ๊ฐ€ ํ—ˆ์šฉ๋˜์ง€ ์•Š๋Š” ์ฝ”๋”ฉํ…Œ์ŠคํŠธ๋ฅผ ๋Œ€๋น„ํ•˜์—ฌ,

 

์˜ค๋žœ๋งŒ์— ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ํ”Œ๋žซํผ์—์„œ ๊ฐ€์žฅ ์ตœ์‹  ๋ฌธ์ œ๋ฅผ ํ•˜๋‚˜ ๊ณจ๋ผ์„œ ํ’€์–ด๋ณด์•˜๋‹ค.

 

String ์ด ๋‹ด๊ธด cards1, cards2, goal ๋ฐฐ์—ด์ด ์ฃผ์–ด์กŒ์„ ๋•Œ

 

cards1, cards2 ๋ฅผ ๊ฐ๊ฐ ์ˆœ์„œ๋Œ€๋กœ ํ•œ ์žฅ์”ฉ ์‚ฌ์šฉํ–ˆ์„ ๋•Œ goal ๋ฐฐ์—ด์„ ์™„์„ฑํ•  ์ˆ˜ ์žˆ๋Š”์ง€ ํ™•์ธํ•ด์•ผ ํ•œ๋‹ค.

 

๋ง์ด ์กฐ๊ธˆ ๋ฒˆ์žกํ•œ๋ฐ, ์˜ˆ์‹œ๋ฅผ ๋ณด๋ฉด ์ดํ•ดํ•˜๊ธฐ ์‰ฝ๋‹ค.

 

(๋ฌธ์ œ ์„ค๋ช… ไธญ )

์ฒซ ๋ฒˆ์งธ ์นด๋“œ ๋ญ‰์น˜์— ์ˆœ์„œ๋Œ€๋กœ ["i", "drink", "water"], ๋‘ ๋ฒˆ์งธ ์นด๋“œ ๋ญ‰์น˜์— ์ˆœ์„œ๋Œ€๋กœ ["want", "to"]๊ฐ€ ์ ํ˜€์žˆ์„ ๋•Œ ["i", "want", "to", "drink", "water"] ์ˆœ์„œ์˜ ๋‹จ์–ด ๋ฐฐ์—ด์„ ๋งŒ๋“ค๋ ค๊ณ  ํ•œ๋‹ค๋ฉด ์ฒซ ๋ฒˆ์งธ ์นด๋“œ ๋ญ‰์น˜์—์„œ "i"๋ฅผ ์‚ฌ์šฉํ•œ ํ›„ ๋‘ ๋ฒˆ์งธ ์นด๋“œ ๋ญ‰์น˜์—์„œ "want"์™€ "to"๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์ฒซ ๋ฒˆ์งธ ์นด๋“œ๋ญ‰์น˜์— "drink"์™€ "water"๋ฅผ ์ฐจ๋ก€๋Œ€๋กœ ์‚ฌ์šฉํ•˜๋ฉด ์›ํ•˜๋Š” ์ˆœ์„œ์˜ ๋‹จ์–ด ๋ฐฐ์—ด์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 

 

ํˆฌ ํฌ์ธํ„ฐ ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ์‰ฝ๊ฒŒ ํ’€ ์ˆ˜ ์žˆ์—ˆ๋‹ค. 

import java.util.*;

class Solution {
    public String solution(String[] cards1, String[] cards2, String[] goal) {
        int i = 0;
        int j = 0;
        int k = 0;
        while(k<goal.length){
            if(i < cards1.length&&goal[k].equals(cards1[i])){
                k++;
                i++;
            } else if(j < cards2.length&&goal[k].equals(cards2[j])){
                k++;
                j++;
            } else break;
        }
        
        return k==goal.length ? "Yes" : "No";
    }
}

 

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ํ”Œ๋žซํผ์—์„œ ์˜ค๋žœ๋งŒ์— ์ž๋ฐ”๋กœ ๋ฌธ์ œ๋ฅผ ํ’€์–ด๋ณธ ๋’ค ๋Š๋‚€ ์ ์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค

 

1. import ๊นŒ์ง€ ์Šค์Šค๋กœ ํ•ด์ค˜์•ผ ํ•˜๋Š” ์ง€ ๊นœ๋นกํ–ˆ๋‹ค.. ๐Ÿ˜ข

2. ๋ฉ”์„œ๋“œ ์ด๋ฆ„์ด ํ—ท๊ฐˆ๋ ค์„œ ๊ณ ์ƒํ–ˆ๋‹ค. isEqual ์ธ์ง€ equal ์ธ์ง€ equals ์ธ์ง€ ํ—ท๊ฐˆ๋ ค์„œ ๋‚œ๊ฐํ–ˆ๋‹ค.

3. ์ž๋™์™„์„ฑ์ด ์•ˆ๋˜๋‹ˆ๊นŒ ๋งŽ์ด ๋‹ต๋‹ตํ–ˆ๋‹ค..

 

์•ž์œผ๋กœ ์ž๋ฐ”๋ฅผ ๋งŽ์ด ์‚ฌ์šฉํ•˜๊ฒŒ ๋  ๊ฒƒ ๊ฐ™์•„์„œ,

 

์•Œ๊ณ ๋ฆฌ์ฆ˜๋„ ์ตœ๋Œ€ํ•œ ์ž๋ฐ”๋กœ ํ’€๋ฉด์„œ ์–ธ์–ด์— ์ต์ˆ™ํ•ด์ง€๋ ค๊ณ  ๋…ธ๋ ฅํ•˜๊ณ  ์žˆ๋Š”๋ฐ

 

์ธํ…”๋ฆฌ์ œ์ด IDE ์—์„œ ์ œ๊ณตํ•˜๋Š” ์ž๋™์™„์„ฑ๊ธฐ๋Šฅ์„ ๋„ˆ๋ฌด ์• ์šฉํ•˜๋‹ค๋ณด๋‹ˆ

 

๋””ํ…Œ์ผํ•œ ๋ฉ”์„œ๋“œ ์ด๋ฆ„ ๋“ฑ์„ ์ž˜ ๋ชฐ๋ผ์„œ ๋งŒ์•ฝ ์ž๋™์™„์„ฑ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•˜์ง€ ์•Š๋Š” ์™ธ๋ถ€ ์ฝ”ํ…Œ ํ™˜๊ฒฝ์ด์—ˆ๋‹ค๋ฉด

 

๊ต‰์žฅํžˆ ๋‚œ๊ฐํ–ˆ์„ ๊ฒƒ ๊ฐ™๋‹ค..

 

์•Œ๊ณ ๋ฆฌ์ฆ˜ ํ’€๋•Œ๋Š” ์ž๋™์™„์„ฑ์„ ์‚ฌ์šฉํ•˜์ง€ ๋ง๊นŒ..

 

728x90
๋ฐ˜์‘ํ˜•
LIST
Comments