Giter Site home page Giter Site logo

for_codingtest's Introduction

for_codingtest's People

Contributors

daeunni avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

for_codingtest's Issues

(DFS) ์—ฐ๊ตฌ์†Œ : ๋ฌด์ž‘์œ„ 3๊ฐœ์˜ ๋ฒฝ ์ƒ์„ฑ์— ๋Œ€ํ•ด

for i in range(n):
    for j in range(m):
        if data[i][j] == 0:  # ๋นˆ๊ณต๊ฐ„์ด๋ฉด
            data[i][j] = 1
            count += 1

            dfs(count)

            data[i][j] = 0  # ์—ฌ๊ธฐ๋Š” ๋ฌด์Šจ ์˜๋ฏธ์ธ์ง€ ์ž˜ ๋ชจ๋ฅด๊ฒ ์–ด,,,
            count -= 1

์œ„ ์ฝ”๋“œ๊ฐ€ ์™œ ๋ฌด์ž‘์œ„๋กœ 3๊ฐœ์˜ ๋ฒฝ์„ ์ƒ์„ฑํ•˜๋Š” ์ฝ”๋“œ๊ฐ€ ๋˜๋Š”์ง€ ์ž˜ ๋ชจ๋ฅด๊ฒ ๋‹ค.

(Sorting) MaxProductOfThree : ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜์— ์ ์šฉ๋˜๋Š” ์ ˆ๋Œ€๊ทœ์น™์„ ์ฐพ์•„์•ผํ•˜๋Š” ๋ฌธ์ œ

"""
 MaxProductOfThree
  - ๋ฆฌ์ŠคํŠธ ๋‚ด ์›์†Œ 3๊ฐœ ๊ณฑ์˜ ์ตœ๋Œ“๊ฐ’ ์ฐพ๊ธฐ 
  - sorting์„ ํ™œ์šฉํ•ด ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜์— ์ ์šฉ๋˜๋Š” <์ ˆ๋Œ€๊ทœ์น™>์„ ์ฐพ๋Š” ๋ฌธ์ œ !
"""

def solution(A):
  A.sort()         # ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ 
  return max(A[-1]*A[-2]*A[-3], A[0]*A[1]*A[-1])

๊ฒฝ์šฐ์˜ ์ˆ˜

  1. Max ๊ฐ’์„ ๋งŒ๋“œ๋Š” ๊ฒฝ์šฐ

    • ์Œ์ˆ˜ * ์Œ์ˆ˜ * ์–‘์ˆ˜
    • ์–‘์ˆ˜ * ์–‘์ˆ˜ * ์–‘์ˆ˜
  2. ๋ฐฐ์—ด์˜ ์›์†Œ ๊ตฌ์„ฑ ๊ฒฝ์šฐ์˜ ์ˆ˜

    • ๋ชจ๋‘ ์–‘์ˆ˜์ธ ๋ฐฐ์—ด
    • ๋ชจ๋‘ ์Œ์ˆ˜์ธ ๋ฐฐ์—ด
    • ์–‘์ˆ˜ + ์Œ์ˆ˜์ธ ๋ฐฐ์—ด

(BFS) ๋ฐ”์ด๋Ÿฌ์Šค : ๋‹จ๋ฐฉํ–ฅ๊ทธ๋ž˜ํ”„ ์ €์žฅ๋ฐฉ์‹ > ์ธ์ ‘ํ–‰๋ ฌ๋กœ ๋ณ€ํ™˜์— ๋Œ€ํ•ด

"""
๋ฐ”์ด๋Ÿฌ์Šค 
  - BFS๋ฅผ ์ด์šฉํ•˜๋Š” ๊ฒƒ ๊ฐ™์•„ (์‹œ์ž‘๋…ธ๋“œ๋Š” ํ•ญ์ƒ 1)
"""
from collections import deque 

c = int(input())     # ์ปดํ“จํ„ฐ ์ˆ˜ 
pair = int(input())  # ์—ฐ๊ฒฐ๋œ ์Œ์˜ ์ˆ˜ 
result = 0 


# ์—ฐ๊ฒฐ๋œ ์Œ์˜ ์ˆ˜ ๋‹จ๋ฐฉํ–ฅ๊ทธ๋ž˜ํ”„์ฒ˜๋Ÿผ ์ €์žฅ >>>> (๊ทธ๋ž˜ํ”„ ์ž…๋ ฅ ๋ฐฉ์‹์„ ๋ฐ”๊ฟ”์•ผํ•  ๊ฒƒ ๊ฐ™๋‹ค. 9, 1๊ฐ™์€ ๊ฒฝ์šฐ ํ˜„์žฌ count๋ฅผ ๋ชปํ•˜๊ณ  ์žˆ์Œ) 
# >>> matrix ํ˜•ํƒœ๋กœ ๋ฐ”๊ฟ”๋ณด๊ธฐ 

graph = [[] for _ in range(c + 1)]
for _ in range(pair):
  a, b = map(int, input().split())    # a > b 
  graph[a].append(b)                        # ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ ํ˜•ํƒœ๋กœ ์ €์žฅ 

  # ๊ทธ๋ƒฅ ๋ฐ˜๋Œ€๋„ ๊ณ ๋ คํ•ด์ฃผ๋ฉด ์•ˆ๋˜๋ƒ ์ด๋ ‡๊ฒŒ 
  graph[b].append(a)


## BFS๋กœ 1๊ณผ ์ธ์ ‘ํ•œ ์Œ ์ฐพ๊ธฐ 
q = deque([1])            # ์‹œ์ž‘๋…ธ๋“œ = 1 

# ๋ฐฉ๋ฌธ์—ฌ๋ถ€ ํ™•์ธ 
visited = [False for i in range(c+1)]    

while q : 
  now = q.popleft()
  # print(now)
  result += 1
  visited[now-1] = True

  # ์ธ์ ‘๋…ธ๋“œ ํƒ๋ฐฉ 
  for next in graph[now]: 
    if visited[next-1] == False :
      q.append(next)
      visited[next-1] == True       # ๋ฐฉ๋ฌธ์ฒดํฌ 

print(result-1)

์œ„ ์ฝ”๋“œ๋Š” ํ…Œ์ŠคํŠธ์ผ€์ด์Šค๋Š” ์ž‘๋™ํ•˜๋Š”๋ฐ, ๋‹จ๋ฐฉํ–ฅ๊ทธ๋ž˜ํ”„ ์ €์žฅ๋ฐฉ์‹์œผ๋กœ ๊ทธ๋ž˜ํ”„๋ฅผ ์ €์žฅํ•ด์„œ ํ†ต๊ณผํ•˜์ง€ ๋ชปํ•œ ์ฝ”๋“œ์ด๋‹ค.
์ด๋ฅผ ์ธ์ ‘ํ–‰๋ ฌ๋กœ ์ €์žฅํ•˜๋„๋ก ๋ณ€๊ฒฝํ•˜์—ฌ BFS๋ฅผ ์ ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ• ์ƒ๊ฐํ•ด๋ณด๊ธฐ.. !

(MaxSlice) MaxSliceSum : ๋‹ค์ด๋‚˜๋ฏน ํ”„๋กœ๊ทธ๋ž˜๋ฐ์œผ๋กœ ๋‹ค์‹œํ’€๊ธฐ

๋ฌธ์ œ

A non-empty array A consisting of N integers is given. A pair of integers (P, Q), such that 0 โ‰ค P โ‰ค Q < N, is called a slice of array A. The sum of a slice (P, Q) is the total of A[P] + A[P+1] + ... + A[Q].

Write a function:

class Solution { public int solution(int[] A); }

that, given an array A consisting of N integers, returns the maximum sum of any slice of A.

For example, given array A such that:

A[0] = 3 A[1] = 2 A[2] = -6
A[3] = 4 A[4] = 0
the function should return 5 because:

(3, 4) is a slice of A that has sum 4,
(2, 2) is a slice of A that has sum โˆ’6,
(0, 1) is a slice of A that has sum 5,
no other slice of A has sum greater than (0, 1).
Write an efficient algorithm for the following assumptions:

N is an integer within the range [1..1,000,000];
each element of array A is an integer within the range [โˆ’1,000,000..1,000,000];
the result will be an integer within the range [โˆ’2,147,483,648..2,147,483,647].

์ดˆ๊ธฐ๋‹ต์•ˆ (69%)

def solution(A):

  # ์˜ˆ์™ธ 1) ๋นˆ ๋ฆฌ์ŠคํŠธ์ผ ๋•Œ 
  if len(A) == 0 : 
    return 0

  max_start = A[0]    # ์šฐ์„  ์ฒ˜์Œ์œผ๋กœ ์ดˆ๊ธฐํ™”
  max_slice = A[0]    # ์˜ˆ์™ธ 2) ์›์†Œ๊ฐ€ ํ•˜๋‚˜๋ผ ์ด ๋’ค์—๊ฐ€ ์—†์„ ๋•Œ๋ฅผ ๋Œ€๋น„ํ•ด ์ดˆ๊นƒ๊ฐ’์„ ์ด๋ ‡๊ฒŒ ์„ค์ • 
  present = 0 

  # ๊ฐ ์›์†Œ๋ณ„ ๋ฐ˜๋ณต๋ฌธ 
  for i in A : 
    present = 0 
    
    if i > max_start :
      max_start = i    # max_start๊ฐ’ ์—…๋ฐ์ดํŠธ 

    # ์ง„์งœ ์ด์ค‘for๋ฌธ..? 
    for j in A[A.index(max_start):]:    # j์— max_start๋„ ํฌํ•จํ•˜๊ฒŒ๋จ
      present += j     # ๋ˆ„์ ํ•ฉ 

      if present > max_slice : 
        max_slice = present

  return max_slice

# ์•„ ์ด ๊ฒฝ์šฐ์—๋Š” ์•ž ๊ฐ’์„ ๋นผ์•ผํ•˜๋Š”๋ฐ.. ์ฒ˜์Œ๊ฐ’์œผ๋กœ ์ดˆ๊ธฐํ™”ํ•˜๋ฉด ์•ˆ๋˜๋Š”๊ฑด๊ฐ€?? 
A = [-5, 2, 3]
solution(A)

(DFS) ์ ๋ก์ƒ‰์•ฝ : ์™œ ๋‚ด ์ฝ”๋“œ๋Š” ์•ˆ๋˜๋Š”์ง€ ์ฐพ๊ณ ์‹ถ์–ด......

๋‚ด ์ฝ”๋“œ

"""
์ ๋ก์ƒ‰์•ฝ 
  - ์Œ๋ฃŒ์ˆ˜ ์–ผ๋ ค๋จน๊ธฐ ๋ฌธ์ œ์™€ ๋น„์Šทํ•˜๊ฒŒ ์ƒํ•˜์ขŒ์šฐ dfs๋กœ ์ ‘๊ทผ
  - ์Œ๋ฃŒ์ˆ˜ ์–ผ๋ ค๋จน๊ธฐ ๋ฌธ์ œ๋Š” 1์˜ ์˜์—ญ๋งŒ ํŒŒ์•…ํ–ˆ๋‹ค๋ฉด, ์ด ์œ ํ˜•์€ R, G, B์˜ ์œ ํ˜•์„ ๊ฐ๊ฐ ํŒŒ์•…ํ•ด์•ผํ•จ 
  - ์ƒ‰๋งน์ธ ์‚ฌ๋žŒ๊ณผ ์•„๋‹Œ์‚ฌ๋žŒ์˜ ๊ฐ’์„ ๊ตฌ๋ถ„ํ•ด์„œ ์ถœ๋ ฅํ•ด์ค˜์•ผ ํ–ˆ์Œ
"""

n = int(input())
graph = []
count = []

for i in range(n):
  graph.append(list(map(str, input())))       # ๊ทธ๋ž˜ํ”„ ์„ธ๋กœ์ค„ ๊ฐœ์ˆ˜๋งŒํผ ์ž…๋ ฅ > ์ž…๋ ฅ์‹œ ๋„์–ด์“ฐ๊ธฐ ํ•˜์ง€๋ง๊ธฐ!!!


# x, y ์ขŒํ‘œ๋กœ ๊ทธ๋ž˜ํ”„ ๋‚ด์—์„œ ์›€์ง์ด๊ธฐ 
def dfs(x, y, color):

  if x < 0  or x > n-1 or y < 0 or y > n-1 : 
    return False

  ## ํŠน์ • color์™€ ๊ฐ™์œผ๋ฉด dfs ์ˆ˜ํ–‰ > ๋ฐฉ๋ฌธ์ฒ˜๋ฆฌ๋ฅผ ์–ด๋–ป๊ฒŒ ํ• ๊นŒ? 
  if graph[x][y] == color:

    ## ********๋ฐฉ๋ฌธ์ฒ˜๋ฆฌ ์ฒดํ‚น์„ ๋‹ค๋ฅด๊ฒŒํ•˜๊ธฐ ******* 
    if graph[x][y] == 'R' or graph[x][y] == 'G':
      graph[x][y] = 'RG'
    else:
      graph[x][y] = 'v'    

    dfs(x-1, y, color)
    dfs(x, y-1, color)
    dfs(x+1, y, color)
    dfs(x, y+1, color)

    return True        # ์ด์–ด์ ธ์žˆ๋Š” ์Œ์„ ์ฐพ์•˜์„ ๋•Œ 

  else:
    return False

for color in ['R', 'G', 'B', 'RG']:
  temp = 0
  for i in range(n):
    for j in range(n):
      if dfs(i, j, color) == True:     # ํŠน์ • color ์ง€์ •ํ•ด์„œ ํ™•์ธ 
        temp += 1

  count.append(temp)       # ์ƒ‰๊น”๋ณ„ count๋ฅผ ์ ์žฌ 

print(sum(count[:3]), sum(count[2:]))  # ์ƒ‰๋งน ์•„๋‹Œ์‚ฌ๋žŒ, ์ƒ‰๋งน์ธ์‚ฌ๋žŒ 

์ •๋‹ต์ฝ”๋“œ

n = int(input())
picture = []
count = []

for i in range(n):
  picture.append(list(map(str, input())))    


def dfs(x,y,color):
    if x < 0 or  x > n-1 or y < 0 or y > n-1:
        return False

    ## ์ƒ‰๊น”๋ณ„๋กœ count 
    if picture[x][y] == color:

        ## ์ฒดํ‚น์„ ๋‹ค๋ฅด๊ฒŒ ํ•จ 
        if picture[x][y] == 'R' or picture[x][y] =='G':
            picture[x][y] = 'RG'
        else:
            picture[x][y] = 'v'

        dfs(x-1,y,color)
        dfs(x+1,y,color)
        dfs(x,y-1,color)
        dfs(x,y+1,color)

        return True
    else:
        return False

for color in ['R','G','B','RG']:       # ์ƒ‰๊น”๋ณ„๋กœ ์˜์—ญ ๊ฐœ์ˆ˜ ์„ธ๊ธฐ 
    temp = 0

    for i in range(n):
        for j in range(n):

            if dfs(i,j,color) == True:
                temp += 1

    count.append(temp)

print(sum(count[:3]), sum(count[2:]))

๋Œ€์ฒด ๋ญ๊ฐ€ ๋‹ค๋ฅธ๊ฑฐ์•ผ ~~

(MaxSlice) MaxProfit : ๋ฌธ์ œ ์ƒํ™ฉ์„ ์—…๋ฐ์ดํŠธํ•  ๋ณ€์ˆ˜๋ฅผ ์ •ํ•ด ๊ฐ„๋‹จํžˆ ๋งŒ๋“ค๊ธฐ

์—…๋ฐ์ดํŠธํ•  ๋ณ€์ˆ˜๋ฅผ ์ •ํ•˜๊ธฐ

"""
MaxProfit
  - ๋ฆฌ์ŠคํŠธ ๋‚ด์—์„œ ์ฃผ์‹ ๊ฐ€๊ฒฉ์˜ ๊ฐ€์žฅ ํฐ ์ด์ต์„ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ
  - ์ธ๋ฑ์Šค์˜ ์ˆœ์„œ๋Š” ์œ ์ง€ํ•˜๋ฉด์„œ, ํšจ์œจ์ ์œผ๋กœ ์ด์ต์ด ํฐ ๊ฒฝ์šฐ๋ฅผ ํƒ์ƒ‰ํ•˜๋Š”๊ฒŒ ์–ด๋ ค์šด ๋ฌธ์ œ

1) ๋ฌด์กฐ๊ฑด << []-์ž‘์€๊ฐ’ >> ์„ ๋นผ์•ผ ์ตœ๋Œ€ ์ด์ต์„ ์–ป์„ ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์€ ์ž๋ช…ํ•˜๋‹ค
2) ์ˆœ์„œ๋Œ€๋กœ ๋ฆฌ์ŠคํŠธ ์›์†Œ๋ฅผ ์ˆœํšŒํ•˜๋ฉด์„œ 1) min๊ฐ’   2) profit๊ฐ’ ์„ ์—…๋ฐ์ดํŠธํ•˜๋Š” ๋ฐฉ์‹์„ ์ด์šฉํ•œ๋‹ค ! 
"""

A = [23171, 21011, 21123, 21366, 21013, 21367]

def solution(A):

  # ์˜ˆ์™ธ 1) element ์ˆ˜๊ฐ€ ์ ์–ด ์ด์ต์„ ๊ณ„์‚ฐํ•  ์ˆ˜ ์—†์„ ๋•Œ 
  if len(A) < 2:
    return 0

  # ์ตœ์†Ÿ๊ฐ’์€ ์šฐ์„  ์ฒซ๋ฒˆ์งธ ์›์†Œ๋กœ ์žก๋Š”๋‹ค
  min = A[0]
  profit = 0

  for price in A : 
    present = price - min     # ํ˜„์žฌ์˜ ์ด์ต 

    # ํ˜„์žฌ ์ด์ต์ด ๋” ํฌ๋ฉด ์—…๋ฐ์ดํŠธ 
    if profit < present : 
      profit = present

    # ์ตœ์†Ÿ๊ฐ’ ๋” ์ž‘์œผ๋ฉด ์—…๋ฐ์ดํŠธ 
    if min > price : 
      min = price
    
    # >> ์ด๋Ÿฌํ•œ ๋ฐฉ์‹์œผ๋กœ ํ•˜๋ฉด ์ธ๋ฑ์Šค๋„ ์œ ์ง€๋œ๋‹ค. 

  return profit

solution(A)

(๋ฐฑํŠธ๋ž˜ํ‚น) N-queens ๋ฌธ์ œ

ํ˜„์žฌ : 23.01.28
๊ฑฐ์˜ ๋‹ค ์˜จ ๊ฒƒ ๊ฐ™์€๋ฐ ์–ด๋””๊ฐ€ ๋ฌธ์ œ์ผ๊นŒ? ๊ผญ ํ˜ผ์ž ํž˜์œผ๋กœ ํ•ด๊ฒฐํ•ด๋ณด๊ณ  ์‹ถ๋‹ค...

def check(col, ori_col):
  if col == ori_col or col == ori_col+1 or col == ori_col-1 :
    return False    # ํƒˆ๋ฝ (์ƒํ•˜์ขŒ์šฐ๋Œ€๊ฐ์„  ์œ„์น˜)
  else : 
    return True 

def Nqueens(depth):
  global count, ori_col, cur_count  

  if depth > n :      # ์ข…๋ฃŒ์กฐ๊ฑด 
    return 

  # ํŠน์ • depth์˜ col number๋ณ„ ๋ฐ˜๋ณต๋ฌธ 
  for idx in range(n) : 

    if depth == n and cur_count == n : 
      count += 1 
      return 

    # depth๊ฐ€ n๋ณด๋‹ค ์ž‘์œผ๋ฉด 
    if check(field[idx], ori_col):     # ์ƒํ•˜์ขŒ์šฐ ๋Œ€๊ฐ์„  ์œ„์น˜ X
      depth += 1
      ori_col = field[idx]
      cur_count += 1               # ์ด raw์˜ queen์˜ ์œ„์น˜๋Š” ์ •ํ•ด์กŒ๋‹ค. 

      Nqueens(depth)

      depth -= 1                 # ๋ฐฑํŠธ๋ž˜ํ‚น ์ด๋ ‡๊ฒŒ ํ•ด๋„ ๋˜๋‚˜? 
    
      cur_count -= 1

    # if cur_count < depth : 
    #   return 

if __name__ == '__main__':
  # n = int(input())
  n = 8 
  count = 0
  ori_col = 0 
  cur_count = 0
  field = list(range(n))      # field ์„ค์ • (์ค‘๋ณต ํ—ˆ์šฉ X) 
  Nqueens(0)
  print(count)     # ๊ฒฝ์šฐ์˜ ์ˆ˜ 

(ํ) ํ”„๋ฆฐํ„ฐ ํ : ์ค‘๋ณต ๊ณ ๋ คํ•ด ์ฝ”๋“œ ๋ฐœ์ „์‹œํ‚ค๊ธฐ

ํ˜„์žฌ ๋‚ด ์ฝ”๋“œ

ํ‹€๋ฆฐ๊ฑฐ ์•„๋‹Œ๋ฐ, ์ค‘๋ณต๋œ ๊ฒฝ์šฐ๋ฅผ ๊ณ ๋ คํ•˜์ง€ ์•Š์•„ ๋ฌธ์ œ๊ฐ€ ์ƒ๊ฒผ๋‹ค.
https://www.acmicpc.net/problem/1966

import sys 
from collections import deque 
# input = sys.stdin.readline
t = int(input())
result = []

for _ in range(t):
  n, m = map(int, input().split())    # m : ๊ถ๊ธˆํ•œ ์›์†Œ๊ฐ€ queue์—์„œ ๋ช‡๋ฒˆ์งธ ๋†“์—ฌ์žˆ๋Š”์ง€ ๋‚˜ํƒ€๋ƒ„
  imp = None
  output = []
  imp = deque(map(int, input().split()))

  # ๊ถ๊ธˆํ•œ ์›์†Œ 
  find = imp[m]

  # ํ๊ฐ€ ๋นŒ ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณต 
  while imp : 
    if max(imp) == imp[0] : 
      output.append(imp.popleft())
    else:
      imp.append(imp.popleft())   # ์™ผ์ชฝ์—์„œ ๋นผ์„œ ๊ฐ€์žฅ ๋’ค์— ์žฌ๋ฐฐ์น˜ 

  result.append(output.index(find) + 1)      # ๋ช‡๋ฒˆ์งธ๋กœ ๋‚˜์™”๋Š”์ง€ 

for i in result :
  print(i)

๋ฆฌํ„ด๊ฐ’์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋‚˜์˜จ๋‹ค.. ์ฆ‰ 1์ด ์ค‘๋ณต๋  ๋•Œ ๊ทธ๋ƒฅ ๋‹จ์ˆœํžˆ index๋กœ findํ•ด์„œ ๋ฌธ์ œ๊ฐ€ ์ƒ๊ธฐ๋Š” ๊ฒƒ์ด๋‹ค..

1
6 0
1 1 9 1 1 1
imp: deque([1, 1, 9, 1, 1, 1])
find: 1
์ค‘๊ฐ„ imp : deque([1, 9, 1, 1, 1, 1])
์ค‘๊ฐ„ imp : deque([9, 1, 1, 1, 1, 1])
์ค‘๊ฐ„ imp : deque([1, 1, 1, 1, 1])
์ค‘๊ฐ„ imp : deque([1, 1, 1, 1])
์ค‘๊ฐ„ imp : deque([1, 1, 1])
์ค‘๊ฐ„ imp : deque([1, 1])
์ค‘๊ฐ„ imp : deque([1])
์ค‘๊ฐ„ imp : deque([])
output: [9, 1, 1, 1, 1, 1]

์ด๊ฑธ ์ˆ˜์ •ํ•ด๋ณด์ž !

(Leader) EquiLeader ๋ฌธ์ œ : ์‹œ๊ฐ„๋ณต์žก๋„ ๊ฐœ์„ ํ•˜๋ ค๋ฉด?

๋‚ด ์ฝ”๋“œ

์‹œ๊ฐ„ ๋ณต์žก๋„๋ฅผ ๊ฐœ์„ ํ•˜๋ ค๋ฉด ์–ด๋–ป๊ฒŒ ํ•ด์•ผํ• ๊นŒ....

ํ˜„์žฌ๋Š” O(N^2) ๋ณต์žก๋„๋ฅผ ๊ฐ€์ง„๋‹ค

"""
Dominator
  - ๋ฆฌ์ŠคํŠธ ๋‚ด์—์„œ ์ ˆ๋ฐ˜ ์ด์ƒ(+๊ณผ๋ฐ˜์ˆ˜) ๋ฐœ์ƒํ•˜๋Š” ์›์†Œ์˜ ์ธ๋ฑ์Šค ๋ฆฌํ„ด 
  - Counter์˜ most_common(1) ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•œ๋‹ค!
"""

from collections import Counter 

def solution(A):
  count_s = 0

  # ์˜ˆ์™ธ 1) ๋ฆฌ์ŠคํŠธ๊ฐ€ ๋น„์–ด์žˆ์„ ๋•Œ 
  if len(A) == 0 :
    return 0

  # s๋ฅผ ์ผ์ผํžˆ ์ˆœํšŒํ•˜๋ฉด์„œ ์ž˜๋ผ๊ฐ€๊ธฐ..? > ์—ฌ๊ธฐ์„œ ์‹œ๊ฐ„๋ณต์žก๋„๊ฐ€ ์ปค์ง„ ๊ฒƒ ๊ฐ™์•„
  for s in range(len(A)-1):
    left, right = A[:s+1], A[s+1:]

    # ๋‘ ๋ถ€๋ถ„์˜ leader๊ฐ€ ๊ฐ™์œผ๋ฉด?
    left_count = Counter(left).most_common(1)[0]
    right_count = Counter(right).most_common(1)[0]

    # ๊ณผ๋ฐ˜์ˆ˜ ๋„˜๋Š”์ง€ check 
    if left_count[1] > len(left)//2 and right_count[1] > len(right)//2:
      if left_count[0] == right_count[0] :
        count_s += 1
    
  return count_s

(Prefix Sum) Passing car๋ฌธ์ œ : ์‹œ๊ฐ„๋ณต์žก๋„ ๊ด€๋ จ

๋‚ด ์ฝ”๋“œ - ์‹œ๊ฐ„ ๋ณต์žก๋„ O(N**2)

# from more_itertools import locate      # ์ด ํ•จ์ˆ˜๋Š” ์ฝ”ํ…Œ์—์„œ ์ง€์›ํ•˜์ง€ ์•Š์Œ 

def solution(A):
  result = 0

  # 0์˜ ์ธ๋ฑ์Šค, 1์˜ ์ธ๋ฑ์Šค ๊ตฌ๋ถ„ 
  zero = [i for i, x in enumerate(A) if x==0]
  one = [i for i, x in enumerate(A) if x==1]
  
  # ๊ทผ๋ฐ ์ด๊ฑด ๊ทธ๋ƒฅ ์ˆœํšŒ๋งŒ ํ•˜๋Š”๊ฑฐ๋‹ˆ๊นŒ ๋ณ„๋กœ ์—ฐ์‚ฐ๋Ÿ‰์ด ์•ˆ๋“ค์ง€ ์•Š์„๊นŒ? 
  for z in zero:
    for o in one:
      if z<o:
        result += 1
        
  # ์ดˆ๊ณผํ•˜๋Š” ์˜ˆ์™ธ ๊ฒฝ์šฐ (๊ฒฝ์šฐ์˜์ˆ˜ count)
  if result > 1000000000:
    return -1

  return result 

solution([0, 1, 0, 1, 1])

์ด ์ฝ”๋“œ๋ฅผ ์–ด๋–ป๊ฒŒ ๊ฐœ์„ ํ• ์ง€ ์ฐพ์•„๋ณด๋‹ค๊ฐ€, ์ธํ„ฐ๋„ท์—์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํ’€์ด๋“ค์„ ์ฐพ์•˜๋‹ค.


์ •๋‹ต์ฝ”๋“œ

image

์™œ์ง€... ์–ด๋–ป๊ฒŒ ์ €๋Ÿฐ ์ƒ๊ฐ์„ ํ•  ์ˆ˜ ์žˆ๋Š”๊ฑธ๊นŒ...

(DFS) ์„ฌ์˜ ๊ฐœ์ˆ˜ : ์—„์ฒญ ๋™์ผํ•ด๋ณด์ด๋Š”๋ฐ ์™œ ๋‚ด ์ฝ”๋“œ๋Š” ์•ˆ๋ ๊นŒ

์Œ๋ฃŒ์ˆ˜ ์–ผ๋ ค๋จน๊ธฐ์™€ ๋ถ„๋ช… ์—„์ฒญ ๋™์ผํ•ด๋ณด์ด๋Š”๋ฐ ์™œ ๋‚ด ์ฝ”๋“œ๋Š” ์•ˆ๋˜๋Š”๊ฐ€....

# ์žฌ๊ท€ํ•จ์ˆ˜ ๋ฌธ์ œ์—๋Š” ๊ผญ ์ด ์ฝ”๋“œ๋ฅผ ์จ์ฃผ์ž !! 
import sys
sys.setrecursionlimit(10 ** 6)


def dfs(x, y):

  # ์˜์—ญ ๋ฒ—์–ด๋‚˜๋ฉด ์ข…๋ฃŒ
  if x < 0 or x >= w or y < 0 or y >= h:
    return False
  
  if graph[x][y] == 0 : 
    return False

  # ์•„์ง ๋•…(1)์„ ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์•˜๋‹ค๋ฉด 
  if graph[x][y] == 1 :     
    graph[x][y] = 0 

    # ์žฌ๊ท€์  ํ˜ธ์ถœ (์ƒํ•˜์ขŒ์šฐ) + **๋Œ€๊ฐ์„ ๋„ ์ถ”๊ฐ€ํ•ด์•ผํ•จ**
    dfs(x-1, y)
    dfs(x, y-1)
    dfs(x+1, y)
    dfs(x, y+1)

    ## ๋Œ€๊ฐ์„  ์ถ”๊ฐ€
    dfs(x-1, y+1)
    dfs(x+1, y+1)
    dfs(x-1, y-1)
    dfs(x+1, y-1)

    return True

  return False

# ์—ฐ์†์ ์œผ๋กœ input ๋ฐ›๊ธฐ 
while True :
  w, h = map(int, input().split())    # ๊ฐ€๋กœ * ์„ธ๋กœ 
  count = 0 

  # ๋‘˜๋‹ค 0์ด๋ฉด ์ค‘๋‹จ 
  if w == 0 and h == 0 :
    break
  
  graph = []
  for _ in range(h):     # ์„ธ๋กœ๋งŒํผ ์ž…๋ ฅ 
    graph.append(list(map(int, input().split())))     # map ์ž…๋ ฅ (2์ฐจ์› ๋ฆฌ์ŠคํŠธ)

  for i in range(h):
    for j in range(w):
      if dfs(i, j) == True:
        count += 1

  print(count)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.