[파이썬] 27159 노 땡스! n = int(input()) li = sorted(list(map(int, input().split()))) prev = li[0] ans, temp = 0, [li[0]] for i in range(1, n): cur = li[i] if prev + 1 == cur: temp.append(cur) prev = cur else: ans += temp[0] temp = [cur] prev = cur print(ans + temp[0]) Algorithm/boj 2023.01.27
[파이썬] 1331 나이트 투어 def convert(s): return [ord(s[0]) - 65, int(s[1]) - 1] visited = [[0] * 6 for _ in range(6)] cor = convert(input()) start = cor[:] visited[cor[0]][cor[1]] = 2 for _ in range(35): cur = convert(input()) x_diff, y_diff = abs(cor[0] - cur[0]), abs(cor[1] - cur[1]) if visited[cur[0]][cur[1]] or x_diff in [0, 3] or x_diff + y_diff != 3: print('Invalid') exit() visited[cur[0]][cur[1]] = 1 cor = cur x_.. Algorithm/boj 2023.01.25
[파이썬] 1816 암호 키 def prime(n): li = [0, 0] + [1] * n for i in range(2, n + 1): if li[i]: for j in range(2 * i, n + 1, i): li[j] = 0 return li li = prime(1000000) for _ in range(int(input())): n = int(input()) flag = 1 for i in range(2, min(1000001, int(n ** 0.5) + 1)): if li[i] and not n % i: flag = 0 break print('YES' if flag else 'NO') 1. 에라토스테네스의 체로 소수 구하기 2. 소수로 나누어 떨어지면 NO 끝까지 통과하면 YES Algorithm/boj 2023.01.17
[파이썬] 1501 단어 읽기 from functools import reduce d = {} def sort_word(word): if len(word) == 1: return word return word[0] + word[-1] + ''.join(sorted(word[1:-1])) n = int(input()) for _ in range(n): word = sort_word(input()) d[word] = d.get(word, 0) + 1 for _ in range(int(input())): sentence = input().split() li = [] for s in sentence: word = sort_word(s) li.append(d[word] if word in d else 0) print(reduce(lambda .. Algorithm/boj 2023.01.08
혼자 놀기의 달인 ( Level 2, Python, 연습문제 ) def dfs(cards, cur, visited, count): if not visited[cur]: visited[cur] = count return dfs(cards, cards[cur] - 1, visited, count) return def solution(cards): visited = [0] * len(cards) count = 1 for c in cards: if not visited[c - 1]: dfs(cards, c - 1, visited, count) count += 1 check = {} for v in visited: check[v] = check.get(v, 0) + 1 ans = sorted(list(check.values()), reverse=True) return ans[.. Algorithm/programmers 2022.12.12
귤 고르기 ( Level 2, Python, 연습문제 ) def solution(k, tangerine): d = {} for t in tangerine: d[t] = d.get(t, 0) + 1 li = sorted(list(dict.values(d)), reverse=True) ans = 0 while k > 0: if li[ans] < k: k -= li[ans] ans += 1 else: break return ans + 1 1. 개수 카운팅 후 딕셔너리에 저장 2. 개수 내림차순 정렬 3. k만큼 카운팅한 후 리턴 Algorithm/programmers 2022.12.08
점 찍기 ( Level 2, Python, 연습문제 ) def solution(k, d): ans = 0 for i in range(0, d + 1, k): ans += int((d ** 2 - i ** 2) ** 0.5) // k + 1 return ans 수학 Algorithm/programmers 2022.12.08
[파이썬] 12100 2048(Easy) from itertools import product from copy import deepcopy def combine(li): i, count = 1, 0 res = [] while i count: res.append(li[-1]) return res def up(board): res = [[0] * n for _ in range(n)] for i in range(n): temp = [] for j in range(n): if board[j][i] > 0: .. Algorithm/boj 2022.11.30
[파이썬] 2570 비숍2 import sys input = sys.stdin.readline def bimatch(num): if visited[num]: return False visited[num] = 1 for g in graph[num]: if selected[g] == -1 or bimatch(selected[g]): selected[g] = num return True return False n, m = int(input()), int(input()) board = [[0] * n for _ in range(n)] for _ in range(m): x, y = map(int, input().split()) board[x - 1][y - 1] = 1 check_board = [[0] * n for _ in range(n.. Algorithm/boj 2022.11.25
[파이썬] 1017 소수 쌍 def get_prime(n): li = [0, 0] + [1] * n for i in range(2, n + 1): if li[i]: for j in range(2 * i, n + 1, i): li[j] = 0 return li def bimatch(num): if visited[num]: return False visited[num] = 1 for g in graph[num]: if selected[g] == -1 or bimatch(selected[g]): selected[g] = num visited[g] = 1 return True return False n = int(input()) li = list(map(int, input().split())) prime = get_prime(2000) o.. Algorithm/boj 2022.11.23