| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | ||||||
| 2 | 3 | 4 | 5 | 6 | 7 | 8 |
| 9 | 10 | 11 | 12 | 13 | 14 | 15 |
| 16 | 17 | 18 | 19 | 20 | 21 | 22 |
| 23 | 24 | 25 | 26 | 27 | 28 | 29 |
| 30 |
- 스택
- DFS
- 웹 프론트엔드
- 백준
- 파이썬
- 너비 우선 탐색
- 구현
- web
- BASIC
- level2
- 프로그래머스
- 문자열
- DP
- JavaScript
- 그래프 이론
- 프로그래머스스쿨
- 정렬
- 딕셔너리
- 알고리즘
- 그래프이론
- lv2
- 브루트포스 알고리즘
- 자료구조
- CSS
- 자바스크립트
- 그리디 알고리즘
- 다이나믹 프로그래밍
- 그래프 탐색
- BFS
- programmers
- Today
- Total
목록전체 글 (85)
DevLog:-)
문제 코드 import sys n = int(sys.stdin.readline()) s = list(map(int,sys.stdin.readline().strip().split())) M = max(s) for i in range(n): s[i]=s[i]/M*100 print(sum(s)/n)
문제 코드 설계 BFS와 DFS를 이용해 풀 수 있는 문제다 먼저 이중 for 문을 돌고 이때 1이면서 방문한 적이 없는 칸을 만나면 bsq 함수로 넘어가게 했다. 그리고 bsq를 돌면서 주변의 1을 다 탐색하게 했다. 1을 발견할 때마다 c 변수의 값을 올려 해당 단지에 속하는 집의 수를 카운트했다. bsq 함수가 하나 끝날 때마다 아파트 단지 한 개를 구했다는 것이기 때문에 단지의 수를 담는 변수인 count에 +1을 해줬다. 단지별 집의 수를 오름차순으로 정렬 출력해야 하기 때문에 bsq 함수에서 집의 수를 카운트했던 c를 함수 반환값으로 해서 바로 배열에 넣어 주었다.
문제 코드 import sys from collections import deque #60m r,c = map(int,sys.stdin.readline().strip().split()) graph = [] visited = [[False]*c for _ in range(r)] for _ in range(r): graph.append(list(map(int,sys.stdin.readline().strip()))) dx =[1,-1,0,0] dy =[0,0,1,-1] visited[0][0]=True q= deque() q.append((0,0,1)) f=0 while(q): cx,cy,count= q.popleft() for i in range(4): nx = cx+dx[i] ny = cy+dy[i] if..
문제 코드 import sys from collections import deque N,M,V = map(int,sys.stdin.readline().strip().split()) graph =[[] for _ in range(N+1)] def dfs(cur_v): visited.append(cur_v) for v in graph[cur_v]: if(v not in visited): dfs(v) return visited def bfs(start_v): visited=[start_v] q = deque() q.append(start_v) while(q): cur_v =q.popleft() for v in graph[cur_v]: if(v not in visited): visited.append(v) q...
문제코드BFSimport sysfrom collections import dequen = int(sys.stdin.readline())m = int(sys.stdin.readline())graph=[[] for _ in range(n+1)]def bfs(start_v): visited=[start_v] q =deque() q.append(start_v) while(q): cur_v=q.popleft() for v in graph[cur_v]: if(v not in visited): visited.append(v) q.append(v) re..
문제 Number of Islands - LeetCode Can you solve this real interview question? Number of Islands - Given an m x n 2D binary grid grid which represents a map of '1's (land) and '0's (water), return the number of islands. An island is surrounded by water and is formed by connecting adjacent l leetcode.com 접근 방법 1.한 칸씩 확인한다.→ 이중for문 2.육지의 경우 주변을 다 탐색한다. → bfs or dfs 3.탐색한 육지는 다시 탐색하지 않도록 기록한다. →visite..
+visited 전역변수 사용코드 graph = { 'A':['B','D','E'], 'B':['A','C','D'], 'C':['B'], 'D':['A','B'], 'E':['A'] } visited =[] def dfs(cur_v): visited.append(cur_v) for v in graph[cur_v]: if v not in visited: dfs(v) dfs('A') print(visited) A를 시작으로 DFS탐색 경로 출력 +visited 지역변수 사용코드 graph = { 'A':['B','D','E'], 'B':['A','C','D'], 'C':['B'], 'D':['A','B'], 'E':['A'] } def dfs(graph,cur_v,visited=[]): visited.ap..
from collections import deque graph = { 'A':['B','D','E'], 'B':['A','C','D'], 'C':['B'], 'D':['A','B'], 'E':['A'] } def bfs(graph, start_v): visited=[start_v] #1.visit처리set queue = deque(start_v) #1.큐초기화set while(queue): cur_v = queue.popleft() #2.popleft for v in graph[cur_v]: #2.열결된 정점 확인 if(v not in visited): #3.조건 부합확인 visited.append(v) #3.visit처리 queue.append(v) #3.q처리 return visited print(..
문제 코드 import sys n = int(sys.stdin.readline()) R=[] for i in range(n): r,g,b = map(int,sys.stdin.readline().strip().split()) #strip왼쪽 공백 제거 if(i==0): RGB=[r,g,b] continue RGB = [min(RGB[1], RGB[2]) + r, min(RGB[0], RGB[2]) + g, min(RGB[0], RGB[1]) + b] print(min(RGB)) 집을 빨강,초록,파랑 중 하나의 색으로 칠해야하는데 이때 집마다 빨강,초록,파랑색을 칠하는 비용이 제공된다. i번째 집은 i+1번째 집과 색이 무조건 달라야하는 것이 특징이다. 이 문제는 DP로 풀이가 가능하다. 풀이방법 1번째 집..
문제 코드 import sys def x(s,n): if(s ==')'): return 2*n elif(s==']'): return 3*n def check(s): stack = [] for i in s: sum =0 if i =='(': stack.append(')') elif i =='[': stack.append(']') #열린 괄호 들어오면 stack에 닫힌 괄호 넣음 elif not stack: return 0 elif stack[-1] == i: stack.pop() if i ==')': stack.append('2') else: stack.append('3') elif stack[-1] != ')'and stack[-1]!=']': if(i not in stack): return 0 whil..