Skip to content

Commit

Permalink
solve(programmers): LV4_매출 하락 최소화_py
Browse files Browse the repository at this point in the history
# id: 문제 id를 숫자로 작성
# categories : 해당 문제의 유형을 ,로 구분하여 작성
# tags : 해당 문제의 태그를 ,로 구분하여 작성
# time : 해당 문제 풀이에 걸린 시간을 분단위 숫자로 작성
# try : 해당 문제에 몇번의 시도를 했는지 숫자로 작성
# help: 해당 문제에 외부의 도움을 받았는지 true/false로 작성
# url : 해당 문제의 url을 작성
id: 72416
categories: [DFS,DP]
tags: [2021 KAKAO BLIND RECRUITMENT]
time: 240
try: 5
help: true
url: https://school.programmers.co.kr/learn/courses/30/lessons/72416
  • Loading branch information
gogumaC committed Aug 14, 2024
1 parent ccd72a9 commit f448f89
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions src/programmers/LV4_매출_하락_최소화.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import sys
from collections import deque

team=[]
new_s=[]
dp=[]

sys.setrecursionlimit(300100)

def dfs(current,is_join):

if dp[current][is_join]!=None:
return dp[current][is_join]

if current not in team:
dp[current][0]=0
dp[current][1]=new_s[current]
return dp[current][is_join]

sums=0
#나 포함 가는경우
for f in team[current]:
sums+=min(dfs(f,0),dfs(f,1))
dp[current][1]=sums+new_s[current]

# 나빼고 가는경우
min_res=sys.maxsize
for f in team[current]:
res=sums-min(dfs(f,0),dfs(f,1))+dfs(f,1)
min_res=min(min_res,res)
dp[current][0]=min_res

return dp[current][is_join]


def solution(sales, links):
global team,new_s,dp
team={}
new_s=[0]+sales[:]
dp=[[None,None] for i in range(len(new_s))]
for (a,b) in links:
if a not in team:
team[a]=[]
team[a].append(b)

a=dfs(1,1)
b=dfs(1,0)

answer = min(a,b)
return answer

# test_s=[i for i in range(1,300001)]
# test_l=[[i,i+1] for i in range(1,300000)]

# solution(test_s,test_l)

0 comments on commit f448f89

Please sign in to comment.