๐Ÿง ํˆฌ ํฌ์ธํ„ฐ (two - pointer)

2๊ฐœ์˜ ํฌ์ธํ„ฐ๋ฅผ ์ด์šฉํ•ด ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ์‹œ๊ฐ„๋ณต์žก๋„๋ฅผ ์ตœ์ ํ™”ํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ๊ฐ„๋‹จํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉํ•œ๋‹ค.

์ฃผ๋กœ n์˜ ์ตœ๋Œ“๊ฐ’์ด ํฌ๊ฒŒ ์žกํ˜€์žˆ๊ณ  O(nlog n)์˜ ์‹œ๊ฐ„๋ณต์žก๋„๋กœ ํ•ด๊ฒฐ์ด ํž˜๋“ค ๋•Œ, O(n)์˜ ์‹œ๊ฐ„๋ณต์žก๋„๋กœ ๊ตฌํ˜„ํ•ด์•ผ ํ•œ๋‹ค.
์ด๋•Œ, ์ž์ฃผ ์‚ฌ์šฉ๋˜๋Š” ๊ฒƒ์ด ๋ฐ”๋กœ ํˆฌ ํฌ์ธํ„ฐ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋‹ค.

ํˆฌ ํฌ์ธํ„ฐ ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ์‹œ์ž‘ ์ธ๋ฑ์Šค์™€ ์ข…๋ฃŒ ์ธ๋ฑ์Šค๋ฅผ ์ง€์ •ํ•ด ์„œ๋กœ ์กฐ๊ฑด์— ๋”ฐ๋ผ ์ ‘๊ทผํ•˜๋„๋ก ํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

 

 

๐Ÿง ์Šฌ๋ผ์ด๋”ฉ ์œˆ๋„์šฐ

์Šฌ๋ผ์ด๋”ฉ ์œˆ๋„์šฐ ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ 2๊ฐœ์˜ ํฌ์ธํ„ฐ๋กœ ๋ฒ”์œ„๋ฅผ ์ง€์ •ํ•œ ํ›„
๋ฒ”์œ„(window)๋ฅผ ์œ ์ง€ํ•œ ์ฑ„๋กœ ์ด๋™(sliding)ํ•˜๋ฉฐ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋‹ค.

ํˆฌํฌ์ธํ„ฐ ์•Œ๊ณ ๋ฆฌ์ฆ˜๊ณผ ๋งค์šฐ ๋น„์Šทํ•˜๋‹ค.

 

 

 

๐Ÿง ๋ฐฑ์ค€ 17609 (ํšŒ๋ฌธ)    Gold V 




๐Ÿคซ  solution_17609(์‹œ๊ฐ„์ดˆ๊ณผ)

string_list = [input() for _ in range(int(input()))]

def palindrome(string):
    # ํšŒ๋ฌธ์ธ ๊ฒฝ์šฐ
    if string == string[::-1]:
        print(0)
    # ํšŒ๋ฌธ์ด ์•„๋‹Œ๊ฒฝ์šฐ
    else:
        ans = 2
        for i in range(len(string)):
            new_string = string[:i] + string[i+1:]
            if new_string == new_string[::-1]:
                ans = 1
        print(ans)

for i in string_list:
    palindrome(i)

 

๐Ÿคซ  solution_17609

# ์œ ์‚ฌ ํšŒ๋ฌธ ํŒ๋‹จ ํ•จ์ˆ˜
def palindrome_like(string, left, right):
    while left < right:
        if string[left] == string[right]:
            left += 1
            right -= 1
        else:
            return False
    return True
# ํšŒ๋ฌธ ํ•จ์ˆ˜
def palindrome(string, left, right):
    while left < right:
        if string[left] == string[right]:
            left += 1
            right -= 1
        else:
            pseudo1 = palindrome_like(string, left+1, right)
            pseudo2 = palindrome_like(string, left, right-1)

            if pseudo1 or pseudo2:
                return 1
            else:
                return 2
    return 0

for string in list([input() for _ in range(int(input()))]):
    print(palindrome(string, 0, len(string) - 1))

 

 

 

๐Ÿง ๋ฐฑ์ค€ 1253 (Good Number)    Gold IV 

  


๐Ÿคซ  Algorithm 

# ํ•ด๋‹น ์›์†Œ๋ฅผ ์ œ์™ธํ•œ ๋ฆฌ์ŠคํŠธ์—์„œ ํˆฌ ํฌ์ธํ„ฐ(Two-Pointer)๋ฅผ ํ†ตํ•ด 
# ๋‘ ์›์†Œ์˜ ํ•ฉ์ด ์„ ํƒํ•œ ์›์†Œ์™€ ๊ฐ™์€์ง€ ๋น„๊ตํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค.

1. num ๋ฆฌ์ŠคํŠธ๋ฅผ ์ •๋ ฌํ•œ๋‹ค.

2. 0๋ถ€ํ„ฐ N - 1 ๊นŒ์ง€ ๋ฐ˜๋ณต๋ฌธ์„ ํ†ตํ•ด ํŠน์ • ์›์†Œ(num[i])๋ฅผ ์„ ํƒํ•˜๊ณ , ํ•ด๋‹น ์›์†Œ๋ฅผ ์ œ์™ธํ•œ except_num ๋ฆฌ์ŠคํŠธ๋ฅผ ์ƒ์„ฑ

3. except_num ๋ฆฌ์ŠคํŠธ์—์„œ ํˆฌ ํฌ์ธํ„ฐ๋ฅผ ํ†ตํ•ด ๋‘ ์›์†Œ์˜ ํ•ฉ(sum)์ด num[i] ์ธ์ง€ ๋น„๊ตํ•œ๋‹ค.
  t < num[i]์ด๋ฉด left๋ฅผ ์ฆ๊ฐ€
  t > num[i]์ด๋ฉด right๋ฅผ ๊ฐ์†Œ

4. 2, 3 ๊ณผ์ •์„ ๋ฐ˜๋ณตํ•œ๋‹ค.




๐Ÿคซ  solution_1253

N = int(input())
num = list(map(int, input().split()))
num.sort()
cnt = 0

for i in range(N):
    except_num = num[:i] + num[i + 1:]
    left, right = 0, len(except_num) - 1
    while left < right:
        sum = except_num[left] + except_num[right]
        if sum == num[i]:
            cnt += 1
            break
        if sum < num[i]:
            left += 1  # t ๋ฅผ ์ฆ๊ฐ€์‹œ์ผœ์•ผ ํ•˜๋ฏ€๋กœ left ์ฆ๊ฐ€
        else:
            right -= 1  # t ๋ฅผ ๊ฐ์†Œ์‹œ์ผœ์•ผ ํ•˜๋ฏ€๋กœ right ๊ฐ์†Œ
print(cnt)

 

 

 

๐Ÿง ๋ฐฑ์ค€ 11003 (์ตœ์†Ÿ๊ฐ’ ์ฐพ๊ธฐ,  ์Šฌ๋ผ์ด๋”ฉ ์œˆ๋„์šฐ ์ด์šฉ)    Platinum V 

  


๐Ÿคซ  Algorithm 

ํŠน์ • ์ƒˆ๋กœ์šด ์›์†Œ๊ฐ€ ์ถ”๊ฐ€๋  ๋•Œ ์ด ์›์†Œ๋ณด๋‹ค ํฐ ๊ฐ’์€ ์œ ์ง€๋  ํ•„์š”๊ฐ€ ์—†๋‹ค.
(์ธ๋ฑ์Šค๋กœ ๋ณผ ๋•Œ์—๋„ ์ƒˆ๋กœ ์ถ”๊ฐ€๋˜๋Š” ์›์†Œ๊ฐ€ ๊ฐ€์žฅ ์˜ค๋ž˜ ๋‚จ๊ธฐ ๋•Œ๋ฌธ.)

๋ฑ์œผ๋กœ ๊ตฌ์„ฑํ•˜๊ณ  ๋ฑ์˜ ์•ž์— ํ•ญ์ƒ ์ตœ์†Œ๊ฐ’์ด ์˜ค๋„๋กํ•˜๊ธฐ ์œ„ํ•ด
์‚ฝ์ž…์€ append๋ฅผ ์ด์šฉํ•ด ๊ฐ’๊ณผ ์ธ๋ฑ์Šค๋กœ ์ง„ํ–‰ํ•œ๋‹ค.

1. ์‚ฝ์ž…ํ•˜๊ธฐ์ „์— ๋ฑ์„ ๋’ค์—์„œ while๋กœ ์‚ฝ์ž…ํ•˜๋ ค๋Š” ์š”์†Œ๋ณด๋‹ค ํฐ๊ฐ’์„ ๋ชจ๋‘ ์ œ๊ฑฐ.
(์‚ฝ์ž…ํ•œ๋‹ค๋ฉด ๋ฑ์€ ํ•ญ์ƒ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ๋œ ์ƒํƒœ๋ฅผ ์ž๋™์ ์œผ๋กœ ์œ ์ง€ํ•  ๊ฒƒ์ด๋‹ค.)
(์ด๋Š” ์ •๋ ฌ๋กœ ์ธํ•œ ์‹œ๊ฐ„๋ณต์žก๋„๋ฅผ ์—†์• ์ค˜ ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋‹ค.)

2. ์ถ”๊ฐ€์ ์œผ๋กœ ์Šฌ๋ผ์ด๋”ฉ ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚œ ์š”์†Œ๋“ค์„ ์ƒ๊ฐํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™๋‹ค.
์‚ฝ์ž…ํ•˜๊ธฐ์ „์— ๋ฑ์„ ์•ž์—์„œ๋ถ€ํ„ฐ ๋ณด๋ฉฐ ์ธ๋ฑ์Šค ๋ฒ”์œ„๊ฐ€ ๋ฒ—์–ด๋‚œ ์š”์†Œ๋“ค์„ ๋นผ์ค€๋‹ค.

i) ๋’ค์—์„œ๋ถ€ํ„ฐ ํ™•์ธํ•˜๋ฉฐ ์‚ฝ์ž…ํ•˜๋ ค๋Š” ์š”์†Œ๋ณด๋‹ค ํฐ ๊ฐ’๋“ค์„ ์ œ๊ฑฐ
ii) ์•ž์—์„œ๋ถ€ํ„ฐ ๋ณด๋ฉฐ ์Šฌ๋ผ์ด๋”ฉ ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚œ ์š”์†Œ๋“ค์„ ์ œ๊ฑฐ
iii) ํ˜„์žฌ ์š”์†Œ ์‚ฝ์ž…
๋ชจ๋“  ์—ฐ์‚ฐ์ด O(1)์œผ๋กœ ์ „์ฒด ๋ฐฐ์—ด์„ ํ™•์ธํ•˜๋Š”๋ฐ O(n)์œผ๋กœ ํ•ด๊ฒฐ๊ฐ€๋Šฅํ•˜๋‹ค.




๐Ÿคซ  solution_11003

from collections import deque

N, L = map(int, input().split())
A = list(map(int, input().split()))
dq = deque()


for i in range(N):
    first, end = i - L, i
    # ํ˜„์žฌ ๋“ค์–ด์˜จ ๊ฐ’๋ณด๋‹ค ํฐ ๊ฐ’๋“ค์„ ์ œ๊ฑฐํ•˜์—ฌ ์‹œ๊ฐ„๋ณต์žก๋„๋ฅผ ์ค„์ž„
    while dq and dq[-1][0] > A[end]:  # ๋ฑ์˜ ๋งˆ์ง€๋ง‰ ์œ„์น˜์—์„œ ํ˜„์žฌ๊ฐ’๋ณด๋‹ค ํฐ ๊ฐ’ ์ œ๊ฑฐ
        dq.pop()
    dq.append((A[end], end))  # A, A_idx, ๋งˆ์ง€๋ง‰ ์œ„์น˜์— ํ˜„์žฌ๊ฐ’ ์ €์žฅ
    if dq[0][1] <= first:
        dq.popleft()
    print(dq[0][0], end = " ")

 

 

+ Recent posts