알고리즘/알고리즘_swea 41

[Python][SWEA] 5215. 햄버거 다이어트 D3

SW Expert AcademySW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!swexpertacademy.com 🗒️파이썬 코드 풀이def dfs(n,T_score,kcal): global ans if kcal > L : return if n == N : ans = max(ans,T_score) return dfs(n + 1, T_score + lst[n][0], kcal + lst[n][1] ) dfs(n + 1, T_score, kcal)T = int(input())for tc in range(1,T+1): N,L = list(map(int,input().split())) # N 재료수, L 제한 칼로..

[Python][SWEA] 1979. 어디에 단어가 들어갈 수 있을까 D2

SW Expert AcademySW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!swexpertacademy.com🗒️파이썬 코드 풀이T = int(input())for tc in range(1,T+1): N,K = map(int,input().split()) lst = [list(map(int,input().split())) + [0] for _ in range(N)] lst.append([0]*(N+1)) ans = 0 v = [] for i in range(N+1): sum = 0 for j in range(N+1): if lst[i][j] == 1 : sum += 1 ..

[Python][SWEA] 1204. [S/W 문제해결 기본] 1일차 - 최빈수 구하기

🗒️ 파이썬 코드 풀이T = int(input())for tc in range(1,T+1): N = int(input()) lst = list(map(int,input().split())) score = [0] * 101 for ls in lst: score[ls] += 1 mx = max(score) mx_idx = 0 for i in range(len(score)): if mx == score[i]: mx_idx = max(mx_idx,i) print(f"#{tc} {mx_idx}") 1. 0~100까지 길이의 리스트를 만들어준다.  각각의 인덱스 번호가 점수가 되는 것이다.ex) 0 인덱스 = 0 점 2. 입력..

[Python][SWEA] 1954. 달팽이 숫자 D2

🗒️ 파이썬 코드 풀이T = int(input())for tc in range(1,T+1): N = int(input()) lst = [[0]*N for _ in range(N)] di,dj = [0,1,0,-1],[1,0,-1,0] i = j = k = 0 lst[i][j] = 1 cnt = 2 while cnt  1. di,dj 방향으로 문제를 푸는 방식이다. 2. 반복횟수는 cnt의 값이 N*N까지로 설정한다. 3. ni,nj의 값을  동-남-서-북 방향으로 진행 시킨다.  (처음 시작은 동쪽부터) 4. 범위는 0~N 까지이고 lst[ni][nj] = 0 인 지역만 가능하다. i,j 값은 조건에 통과된 ni,nj로 바꿔주고 새로운 lst[i][j]의 값에 cn..

[Python][SWEA] 20728. 공평한 분배 2 D3

🗒️ 파이썬 코드 풀이T = int(input())for tc in range(1,T+1): N,K = map(int,input().split()) lst = list(map(int,input().split())) lst.sort() candle_mn = lst[K-1]-lst[0] for i in range(1,len(lst)-K+1): tmp = lst[i:i+K] candle_mn = min(candle_mn,(tmp[K-1]-tmp[0])) print(f"#{tc} {candle_mn}") 1. 투포인터 알고리즘 방식으로 진행한다. 2. 우선 lst를 오름차순으로 정렬해준다. 3. candle_mn (최소값)을 초기값으로 설정한다. 4. ..

[Python][SWEA] 1859. 백만 장자 프로젝트 D2

🗒️ 파이썬 코드 풀이T = int(input())for tc in range(1,T+1): N = int(input()) lst = list(map(int,input().split())) u_lst = lst[::-1] max = u_lst[0] ans = 0 for i in range(1,len(u_lst)): if u_lst[i] > max : max = u_lst[i] else : ans += (max-u_lst[i]) print(f"#{tc} {ans}") 1.(문제점)0 인덱스에서 순차적으로 하면, 최대값이 계속 갱신될 수 있다는 생각을 하게된다.최대값이 갱신되면, 과거의 최대값은 무용지물이 ..

[Python][SWEA] 10580. 전봇대 D3

🗒️ 파이썬 코드 풀이from itertools import combinationsT = int(input())for tc in range(1, T+1): N = int(input()) lst = [list(map(int,input().split())) for _ in range(N)] connection = 0 combi = list(combinations(lst,2)) for c in combi: if c[0][0] > c[1][0] and c[0][1] c[1][1] : connection += 1 print(f"#{tc} {connection}") 1. (a1,b1) 과 (a2,b2) 이렇게 전선의 위치가 있을때 a1 2. itertools를 사용해..