Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d633830

Browse files
committed
Lv4 파이썬 완료
1 parent 4b7d84c commit d633830

16 files changed

+370
-0
lines changed

‎Programmers/Lv4/Lv4_3xn타일링.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
def solution(n):
2+
answer = 0
3+
d = [0] * (n+1)
4+
d[0] = 1
5+
for i in range(2,n+1,2):
6+
d[i] = d[i-2] * 3
7+
for j in range(i-4,-1,-2):
8+
d[i] += d[j]*2
9+
d[i] %= 1000000007
10+
return d[n]

‎Programmers/Lv4/Lv4_N-Queen.py‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
arr = [[False for x in range(13)] for y in range(13)]
2+
check_col = [False for x in range(13)]
3+
check_dig = [False for x in range(26)]
4+
check_dig2 = [False for x in range(26)]
5+
6+
def check(row, col, n):
7+
if check_col[col]: # |
8+
return False
9+
if check_dig[row+col]: # 왼쪽위 대각선
10+
return False
11+
if check_dig2[row-col+n]: # /
12+
return False
13+
return True
14+
15+
def calc(n, row):
16+
if row == n:
17+
return 1
18+
cnt = 0
19+
for col in range(n):
20+
if check(row, col, n):
21+
check_dig[row+col] = True
22+
check_dig2[row-col+n] = True
23+
check_col[col] = True
24+
arr[row][col] = True
25+
cnt += calc(n, row+1)
26+
check_dig[row + col] = False
27+
check_dig2[row - col + n] = False
28+
check_col[col] = False
29+
arr[row][col] = False
30+
return cnt
31+
32+
def solution(n):
33+
return calc(n,0)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
def solution(maps):
2+
answer = 0
3+
ey = len(maps)
4+
ex = len(maps[0])
5+
q = [[0, 0]]
6+
7+
while q:
8+
answer += 1
9+
size = len(q)
10+
for j in range(size):
11+
top = q.pop(0)
12+
x = top[1]
13+
y = top[0]
14+
if maps[y][x] == 0:
15+
continue
16+
if x == ex - 1 and y == ey - 1:
17+
return answer
18+
maps[y][x] = 0
19+
20+
if x + 1 < ex and maps[y][x + 1] == 1:
21+
q.append([y, x + 1])
22+
if x - 1 > -1 and maps[y][x - 1] == 1:
23+
q.append([y, x - 1])
24+
if y + 1 < ey and maps[y + 1][x] == 1:
25+
q.append([y + 1, x])
26+
if y - 1 > -1 and maps[y - 1][x] == 1:
27+
q.append([y - 1, x])
28+
return -1
29+
30+
print(solution( [[1, 0, 1, 1, 1], [1, 0, 1, 0, 1], [1, 0, 1, 1, 1], [1, 1, 1, 0, 1], [0, 0, 0, 0, 1]]))

‎Programmers/Lv4/Lv4_단어퍼즐.py‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# DP
2+
# 맨 뒷자리부터 한글자씩 늘려가며 확인
3+
def solution(strs, t):
4+
answer = 0
5+
size = len(t)
6+
s = []
7+
d = [99999999 for x in range(size+1)]
8+
9+
for i in strs: # 단어 조각을 삽입
10+
s.append(i)
11+
12+
d[size] = 0
13+
for i in range(size-1, -1, -1):
14+
temp = ""
15+
for j in range(i, size):
16+
temp += t[j]
17+
if s.count(temp) != 0: # 있으면
18+
if d[j+1] != 99999999:
19+
d[i] = min(d[i], d[j+1] + 1)
20+
if j > i + 5:
21+
break
22+
if d[0] != 99999999:
23+
answer = d[0]
24+
else:
25+
answer = -1
26+
return answer
27+
print(solution( ["ab", "na", "n", "a", "bn"], "nabnabn"))

‎Programmers/Lv4/Lv4_도둑질.py‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
def solution(money):
2+
answer = 0
3+
d = [0 for x in range(len(money))]
4+
5+
# 첫번째 집 털기
6+
d[0] = money[0]
7+
d[1] = money[0]
8+
9+
for i in range(2, len(money)-1):
10+
d[i] = max(d[i-2] + money[i], d[i-1])
11+
answer = d[len(money)-2]
12+
13+
# 첫번째 집X -> 두번째 집부터 털기
14+
d[0] = 0
15+
d[1] = money[1]
16+
for i in range(2, len(money)):
17+
d[i] = max(d[i - 2] + money[i], d[i-1])
18+
answer = max(answer, d[len(money)-1])
19+
return answer
20+
21+
print(solution([1, 2, 3, 1]))

‎Programmers/Lv4/Lv4_사칙연산.py‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
def solution(arr):
2+
d = [[[0 for x in range(2)] for y in range(201)] for z in range(201)]
3+
oper = []
4+
5+
# 숫자로 변환
6+
for i in range(len(arr)):
7+
if arr[i] == "+":
8+
oper.append(1001)
9+
elif arr[i] == "-":
10+
oper.append(1002)
11+
else:
12+
oper.append(int(arr[i]))
13+
d[i][i][0] = oper[i]
14+
d[i][i][1] = oper[i]
15+
16+
for i in range(2,len(arr),2): # 첫 숫자, 연산자 이후로 끝까지 (숫자, 연산자) 세트로 증가 -> 범위의 종료 지점 설정
17+
for j in range(0,len(arr)-i,2): # 처음부터 len-i 길이를 전부 검사 -> 범위의 시작 시점 부터 종료 지점까지 검사
18+
tmax = 0
19+
tmin = 0
20+
max_n = -9999999
21+
min_n = 9999999
22+
for k in range(j+1,i+j,2): # 연산 시작
23+
if oper[k] == 1001: # +
24+
tmax = d[j][k - 1][0] + d[k + 1][i + j][0]
25+
tmin = d[j][k - 1][1] + d[k + 1][i + j][1]
26+
if tmax > max_n:
27+
max_n = tmax
28+
if tmin < min_n:
29+
min_n = tmin
30+
elif oper[k] == 1002: # -
31+
tmax = d[j][k - 1][0] - d[k + 1][i + j][1]
32+
tmin = d[j][k - 1][1] - d[k + 1][i + j][0]
33+
if tmax > max_n:
34+
max_n = tmax
35+
if tmin < min_n:
36+
min_n = tmin
37+
d[j][j + i][0] = max_n
38+
d[j][j + i][1] = min_n
39+
return d[0][len(arr) - 1][0]
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
def solution(K, travel):
2+
d = [0 for x in range(100001)]
3+
4+
for i in range(len(travel)): # i번째 경로까지 j분 이하의 시간으로 얻을 수 있는 최대 모금
5+
for j in range(K, -1, -1):
6+
d[j] = -999999 # 최소값으로 초기화
7+
if j >= travel[i][0]:
8+
d[j] = max(d[j], d[j-travel[i][0]] + travel[i][1])
9+
if j >= travel[i][2]:
10+
d[j] = max(d[j], d[j-travel[i][2]] + travel[i][3])
11+
12+
return d[K]
13+
print(solution(1650,[[500, 200, 200, 100], [800, 370, 300, 120], [700, 250, 300, 90]]))
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
def solution(n, cores):
2+
low = 0
3+
high = 100000
4+
mid = (low + high) >> 1
5+
6+
while low < high:
7+
count = 0
8+
temp = 0
9+
10+
for a in cores:
11+
count += (mid // a) + 1
12+
if mid % a == 0:
13+
temp += 1
14+
count -= 1
15+
if count >= n:
16+
high = mid
17+
elif count + temp < n:
18+
low = mid + 1
19+
else:
20+
break
21+
mid = (high + low) >> 1
22+
23+
for i in range(len(cores)):
24+
if mid % cores[i] == 0:
25+
count += 1
26+
if count == n:
27+
return i + 1

‎Programmers/Lv4/Lv4_숫자블록.py‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import math
2+
def get_num(target): # 자신을 제외한 최대 약수를 반환
3+
end = int(math.sqrt(target))
4+
if end*end<target:
5+
end += 1
6+
temp = 2
7+
8+
for i in range(2,end+1):
9+
temp = i
10+
if target%i==0:
11+
break
12+
13+
if temp * temp > target:
14+
return 1;
15+
return target//temp
16+
17+
18+
def solution(begin, end):
19+
answer = []
20+
start_block = 1
21+
end_block = end>>1
22+
23+
if begin==1:
24+
answer.append(0)
25+
begin += 1
26+
27+
for i in range(begin,end+1):
28+
answer.append(get_num(i))
29+
return answer
30+
31+
print(solution(1, 10))
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
def solution(sticker):
2+
answer = 0
3+
d = [0 for x in range(len(sticker))]
4+
5+
if len(sticker) == 1:
6+
return sticker[0]
7+
elif len(sticker) == 2:
8+
return max(sticker[0], sticker[1])
9+
else:
10+
# 첫번째 스티커 사용
11+
d[0] = sticker[0]
12+
d[1] = sticker[0]
13+
for i in range(2, len(sticker)-1):
14+
d[i] = max(d[i-2] + sticker[i], d[i-1])
15+
answer = d[len(sticker) - 2]
16+
17+
# 첫번째 스티커 사용 x
18+
d[0] = 0
19+
d[1] = sticker[1]
20+
for i in range(2,len(sticker)):
21+
d[i] = max(d[i-2] + sticker[i], d[i-1])
22+
answer = max(answer, d[len(sticker) - 1])
23+
return answer

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /