Programmers / 3단계 / 무인도 여행 / python
·
코딩테스트/programmers (python)
코딩테스트 연습 - 무인도 여행 | 프로그래머스 스쿨 (programmers.co.kr)  코드from collections import dequedef solution(maps): N, M = len(maps), len(maps[0]) visited = [[0] * M for _ in range(N)] queue = deque() answer = [] for i in range(N): for j in range(M): if maps[i][j] =='X' or visited[i][j]: continue queue.append((i,j)) visited[i][..
백준 / 1439번 / 뒤집기 / python 파이썬
·
코딩테스트/백준 (python)
문제 : https://www.acmicpc.net/problem/1439  코드s = input()count = 0for i in range(len(s)-1): if s[i] != s[i+1]: count += 1print((count + 1) // 2)  연속된 숫자가 변하는 횟수에서 1을 더한 후 2로 나눈 몫이 구하는 뒤집은 최소 횟수가 된다.
Programmers / 3단계 / 섬 연결하기 / python / Greedy / Kruskal 알고리즘
·
코딩테스트/programmers (python)
https://school.programmers.co.kr/learn/courses/30/lessons/42861  코드def solution(n, costs): answer = 0 costs.sort(key = lambda x: x[2]) s = set([costs[0][0]]) while len(s) != n: for v in costs: if v[0] in s and v[1] in s: continue if v[0] in s or v[1] in s: s.update([v[0], v[1]]) answer += v[2] ..
백준 / 10815번 / 숫자 카드 / python 파이썬
·
코딩테스트/백준 (python)
문제 : https://www.acmicpc.net/problem/10815  코드num = int(input())num_list = list(map(int, input().split()))snum = int(input())snum_list = list(map(int, input().split()))dic = {}for s in snum_list: dic[s] = 0for n in num_list: if n in dic: dic[n] = 1for d in dic: print(dic[d], end=" ") 시간 초과를 방지하기 위해 딕셔너리를 사용해 상근이 이미 가지고 있는 숫자이면 딕셔너리의 값을 1로 변경하여 출력한다.
백준 / 25206번 / 너의 평점은 / python 파이썬
·
코딩테스트/백준 (python)
문제 : https://www.acmicpc.net/problem/25206  코드 score = {'A+':4.5, 'A0':4.0, 'B+':3.5, 'B0':3.0, 'C+':2.5, 'C0':2.0, 'D+':1.5, 'D0':1.0, 'F':0}count = 0result = 0for i in range(20): inp = input().split(' ') if inp[2] != 'P': result += float(inp[1]) * score[inp[2]] count += float(inp[1])print('%.6f' %(result / count))    알게된 것
백준 / 14425번 / 문자열 집합 / python 파이썬
·
코딩테스트/백준 (python)
문제 : https://www.acmicpc.net/problem/14425 코드 n, m = map(int, input().split())n_string = set()for i in range(n): n_string.add(input())count = 0for i in range(m): string = input() if string in n_string: count += 1print(count) 알게된 것문제에서 집합 S에 같은 문자열이 여러 번 주어지는 경우가 없다고 해서 리스트를 이용해서 풀었는데 해당 문자열이 S에 있는지 확인할 때 사용하는 in은 자료구조에 따라 시간 복잡도가 다르다. set()을 이용하면 더욱 빠르게 풀 수 있다. set은 hash table 구조..
백준 / 2566번 / 최댓값 / python 파이썬
·
코딩테스트/백준 (python)
문제 : https://www.acmicpc.net/problem/2566  코드import sysinput = sys.stdin.readlineans = -1for i in range(9): row = list(map(int, input().split())) max_num = max(row) if ans
백준 / 1157번 / 단어 공부 / python 파이썬
·
코딩테스트/백준 (python)
문제 : https://www.acmicpc.net/problem/1157  코드 import sysfrom collections import Counterword = sys.stdin.readline().strip()word = word.upper()count = Counter(word)m = [k for k,v in count.items() if max(count.values()) == v]if len(m) == 1: print("".join(m))else: print('?')Counter를 사용해 딕셔너리로 푼 코드이다. 다른 풀이word = input().upper()word_list = list(set(word))cnt = []for i in word_list: count = wor..