daeunni / for_codingtest Goto Github PK
View Code? Open in Web Editor NEWAlgorithm archive for future ๐ฅ
Algorithm archive for future ๐ฅ
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๊ฐ์ ๋ฒฝ์ ์์ฑํ๋ ์ฝ๋๊ฐ ๋๋์ง ์ ๋ชจ๋ฅด๊ฒ ๋ค.
"""
MaxProductOfThree
- ๋ฆฌ์คํธ ๋ด ์์ 3๊ฐ ๊ณฑ์ ์ต๋๊ฐ ์ฐพ๊ธฐ
- sorting์ ํ์ฉํด ๋ชจ๋ ๊ฒฝ์ฐ์ ์์ ์ ์ฉ๋๋ <์ ๋๊ท์น>์ ์ฐพ๋ ๋ฌธ์ !
"""
def solution(A):
A.sort() # ์ค๋ฆ์ฐจ์ ์ ๋ ฌ
return max(A[-1]*A[-2]*A[-3], A[0]*A[1]*A[-1])
Max ๊ฐ์ ๋ง๋๋ ๊ฒฝ์ฐ
๋ฐฐ์ด์ ์์ ๊ตฌ์ฑ ๊ฒฝ์ฐ์ ์
"""
๋ฐ์ด๋ฌ์ค
- 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๋ฅผ ์ ์ฉํ๋ ๋ฐฉ๋ฒ ์๊ฐํด๋ณด๊ธฐ.. !
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].
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๋ก ์ ๊ทผ
- ์๋ฃ์ ์ผ๋ ค๋จน๊ธฐ ๋ฌธ์ ๋ 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:]))
๋์ฒด ๋ญ๊ฐ ๋ค๋ฅธ๊ฑฐ์ผ ~~
"""
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)
ํ์ฌ : 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]
์ด๊ฑธ ์์ ํด๋ณด์ !
์๊ฐ ๋ณต์ก๋๋ฅผ ๊ฐ์ ํ๋ ค๋ฉด ์ด๋ป๊ฒ ํด์ผํ ๊น....
ํ์ฌ๋ 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
# 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])
์ด ์ฝ๋๋ฅผ ์ด๋ป๊ฒ ๊ฐ์ ํ ์ง ์ฐพ์๋ณด๋ค๊ฐ, ์ธํฐ๋ท์์ ๋ค์๊ณผ ๊ฐ์ ํ์ด๋ค์ ์ฐพ์๋ค.
์์ง... ์ด๋ป๊ฒ ์ ๋ฐ ์๊ฐ์ ํ ์ ์๋๊ฑธ๊น...
์๋ฃ์ ์ผ๋ ค๋จน๊ธฐ์ ๋ถ๋ช ์์ฒญ ๋์ผํด๋ณด์ด๋๋ฐ ์ ๋ด ์ฝ๋๋ ์๋๋๊ฐ....
# ์ฌ๊ทํจ์ ๋ฌธ์ ์๋ ๊ผญ ์ด ์ฝ๋๋ฅผ ์จ์ฃผ์ !!
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)
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.