전체 글

전체 글

    [프로그래머스] 다리를 지나는 트럭

    📌문제 https://programmers.co.kr/learn/courses/30/lessons/42583 🎖️난이도 Level 2 ✔️풀이 # sol (시간 초과 에러 해결! ==> len()함수 사용 x, 변수로 대체) def solution(bridge_length, weight, truck_weights): sec = 0 on_bridge = [0] * bridge_length weight_sum = 0 # sum() 시간초과 해결하기 위해 변수 추가 while on_bridge: # 0조차도 다 없어지기 전까지 (빈배열이 아니면) 무한 반복 sec += 1 # 한번 다리(큐)를 조작할 때마다 시간은 1초씩 추가 tmp_pop = on_bridge.pop(0) weight_sum -= tmp_p..

    [프로그래머스] 프린터

    📌문제 https://programmers.co.kr/learn/courses/30/lessons/42587 🎖️난이도 Level 2 ✔️풀이 # sol1 (내 location(인덱스)를 이동시키면서 반복하다가 맨앞 & max이면 return order) def solution(priorities,location): order = 0 while priorities: if priorities[0] == max(priorities): # max() 써도 시간 초과 안남 order += 1 priorities.pop(0) if location == 0: return order else: location -= 1 else: priorities.append(priorities.pop(0)) if location ..

    [프로그래머스] 기능개발

    📌문제 https://programmers.co.kr/learn/courses/30/lessons/42586 🎖️난이도 Level 2 ✔️풀이 # sol1 def solution(progresses, speeds): import math answer = [] l = len(progresses) left_days = [math.ceil((100-progresses[i])/speeds[i]) for i in range(l)] baepo_num = 1 tmp_max = left_days[0] for item in left_days[1:]: if item > tmp_max: # 같아도 continue answer.append(baepo_num) tmp_max = item baepo_num = 1 continue..

    [프로그래머스] 위장

    📌문제 https://programmers.co.kr/learn/courses/30/lessons/42578 🎖️난이도 Level 2 ✔️풀이 # sol1 def solution(clothes): sorted_list = sorted(clothes, key = lambda x : x[1]) dic = {} category = '' for item in sorted_list: if item[1] == category: dic[category].append(item[0]) # 같은 종류면 그냥 뒤에 이어서 추가만 continue category = item[1] dic[category] = [] # 다를때만 새로운 종류 발견했을 때이므로, 이때만 새롭게 빈 배열로 초기화! dic[category].appen..

    [프로그래머스] 전화번호 목록

    📌문제 https://programmers.co.kr/learn/courses/30/lessons/42577 🎖️난이도 Level 2 ✔️풀이 # sol1 def solution(phone_book): phone_book.sort() # sort() !! # 2중 for문 안써줘야 효율성 향상! for i in range(len(phone_book)-1): if phone_book[i] in phone_book[i+1] and phone_book[i+1].split(phone_book[i])[0] == "": return False return True return True # sol2) s.startswith() def solution(phone_book): phone_book.sort() for i ..

    [프로그래머스] 완주하지 못한 선수

    📌문제 https://programmers.co.kr/learn/courses/30/lessons/42576 🎖️난이도 Level 1 ✔️풀이 # sol1 def solution(participant, completion): participant.sort() completion.sort() for p, c in zip(participant, completion): if p != c: return p return participant[-1] # participant가 completion보다 요소가 1개 더 많으므로, 맨 마지막 전까지 전부 p == c 일 경우! # 즉, 마지막이 답! # sol2) collections.Counter().keys() 이용 import collections def solut..

    [CodeUp] 6098. 성실한 개미

    📌문제 https://codeup.kr/problem.php?id=6098 🎖️난이도 - ✔️풀이 # sol # 미로 상자 입력 miro = [list(map(int, input().split())) for i in range(10)] # 개미 이동 x, y = 1, 1 while True: miro[x][y] = 9 r = miro[x][y+1] # 오른쪽 d = miro[x+1][y] # 아래쪽 if r == 0: # 오른쪽으로 이동 y += 1 elif r == 1: # 아래쪽으로 이동 if d == 1: # 더이상 갈 수 없을 때 break elif d == 2: # 아래쪽에 먹이 miro[x+1][y] = 9 break x += 1 else: # r == 2 (오른쪽에 먹이) miro[x][y+..

    [CodeUp] 6096. 바둑알 십자 뒤집기

    📌문제 https://codeup.kr/problem.php?id=6096 🎖️난이도 - ✔️풀이 # sol1 # 기존 바둑알 데이터 입력 pan = [list(map(int, input().split())) for i in range(19)] n = int(input()) # 십자 뒤집기 for i in range(n): x, y = map(int, input().split()) for t in range(19): pan[x-1][t] = (pan[x-1][t] + 1) % 2 pan[t][y-1] = (pan[t][y-1] + 1) % 2 # 결과 출력 for i in range(19): for j in range(19): print(pan[i][j], end = ' ') print() # sol2)..

    [T-academy] 최대공약수, 최소공배수, 소인수분해, 소수판별법

    [ 최대공약수, 최소공배수 ] 최대공약수 : a, b 중 더 작은 수까지만 체크해도 됨. 거꾸로 체크해 나가기 최소공배수 유클리드 호제법 GCD(a, b) = GCD(b, a%b) # 최대공약수(gcd) sol1 def gcd(a, b): ret = 0 for i in range(min(a, b)): # 1부터 체크 if a % i == 0 and b % i == 0: ret = i return ret # 최대공약수(gcd) sol2 => fast(가장 큰 수부터 체크해서 조건 만족시 ret) def gcd(a, b): for i in range(min(a, b), 0, -1): # min(a, b)부터 거꾸로 체크 if a % i == 0 and b % i == 0: return i # 최대공약수(gc..

    [백준] 9455. 박스

    📌문제 https://www.acmicpc.net/problem/9455 🎖️난이도 ✔️풀이 #define _CRT_SECURE_NO_WARNINGS #include #include #include #include #include #include #include #include #include #include // operator typeid using namespace std; int main(void) { int T(0); int m, n; int map[100][100]; cin >> T; for (int i = 0; i > m >> n; map[100][100] = { -1, }; for (int j = 0; j < m; j++) { for (int k = 0; k ..