본문 바로가기

python-algorithm1422

백준 22993 서든어택 3 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 import sys n=int(sys.stdin.readline()) tmp1=(list(map(int, sys.stdin.readline().split()))) jun=tmp1[0] tmp1.pop(0) tmp1.sort() for i in tmp1: if i 2021. 8. 26.
백준 16471 작은 수 내기 1 2 3 4 5 6 7 8 9 10 11 12 13 14 import sys n=int(sys.stdin.readline()) tmp1=sorted(list(map(int, sys.stdin.readline().split()))) tmp2=sorted(list(map(int, sys.stdin.readline().split()))) tmp1.reverse() cnt=0 for i in tmp1: if i>=tmp2[-1]: pass else: cnt+=1 tmp2.pop() if cnt> n//2: print("YES") else: print("NO") Colored by Color Scripter cs 우선 카드를 정렬을 하구 주언이 가지고있는 카드중 제일큰 카드가 사장이 가지고있는 카드 중 제일 큰.. 2021. 8. 26.
백준 16435 스네이크버드 1 2 3 4 5 6 7 8 import sys n,l=map(int, sys.stdin.readline().split()) h=list(map(int, sys.stdin.readline().split())) h.sort() for i in h: if l>=i: l+=1 else:break print(l) Colored by Color Scripter cs 그리디 알고리즘이구요 스네이크버드의 길이를 최대화 하고싶다-> 작은것부터 먹어야함 2021. 8. 26.
백준 14469 소가 길을 건너간 이유 3 1 2 3 4 5 6 7 8 9 10 11 12 import sys n=int(sys.stdin.readline()) tmp=[] for i in range(n): a,b=map(int, sys.stdin.readline().split()) tmp.append((a,b)) tmp.sort(key= lambda x: (x[0],x[1])) ans=0 for i in tmp: if ans (5,7), -> (8,3) 첫번째 2,1 경우 대기 없으니까 if문으로 갑니다 ans=2+1 두번째 5,7 경우 3초까지만 소가 있었으니까 대기 또 없죠 대기 없으니까 if문으로 갑니다. ans=5+7 세번째 8,3 대기 있죠 두번째에서 12초까지 기다려야하니까 그럼 ans=12 +3 2021. 8. 26.
백준 11256 사탕 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 import sys t=int(sys.stdin.readline()) for _ in range(t): j,n=map(int, sys.stdin.readline().split()) tmp=[] for a in range(n): r,c=map(int, sys.stdin.readline().split()) tmp.append(r*c) tmp.sort() tmp.reverse() # print(tmp) cnt=1 tmpCandy=0 for k in tmp: tmpCandy+=k # print(tmpCandy) if tmpCandy 2021. 8. 26.
백준 9237 이장님 초대 1 2 3 4 5 6 7 8 import sys n=int(sys.stdin.readline()) tmp=list(map(int, sys.stdin.readline().split())) tmp.sort() tmp.reverse() for i in range(n): tmp[i]+=i+1 print(max(tmp)+1) Colored by Color Scripter cs 우선 가장 오래걸리는 나무부터 심어야 하니까 나무 심는 순서를 내림차순으로 정렬 하고, LIne4-5 하루에 하나씩 심는 것 for 문 LIne 8 은다 심어지고 하루 있다가 보여준다는 내용이 명시돼있으므로 최종값에 +1 2021. 8. 26.
Leetcode 1877. Minimize Maximum Pair Sum in Array 1 2 3 4 5 6 7 class Solution: def minPairSum(self, nums: List[int]) -> int: nums.sort() tmp=[] for i in range(len(nums)//2): tmp.append(nums[i]+nums[-(i+1)]) return max(tmp) cs 문제 전제 조건 처럼 쌍을 만들려면 nums 리스트를 오름차순으로 정렬해주고 양끝을 쌍으로 묶어줘야합니다. 그 쌍의 합을 tmp 리스트에 넣어주고 그 중에서 최댓값을 리턴하면됩니다 2021. 8. 26.
Leetcode 561. Array Partition I 1 2 3 4 5 6 7 class Solution: def arrayPairSum(self, nums: List[int]) -> int: nums.sort() ans=0 for i in range(0,len(nums),2): ans+=nums[i] return ans Colored by Color Scripter cs 원래는 그리디알고리즘으로 찾는게 맞는데.. 규칙이 보여서 쉽게 구현했습니다 2021. 8. 26.
백준 2828 사과 담기 게임 1 2 3 4 5 6 7 8 9 10 11 12 13 14 import sys n,m= map(int, sys.stdin.readline().split()) j=int(sys.stdin.readline()) left=1 ans=0 for i in range(j): apple=int(sys.stdin.readline()) if apple left+m -1: ans+= apple-(left+m-1) left=apple-m+1 print(ans) Colored by Color Scripter cs 이게 백준문제는 문제 해석이 조금 어려워서 바구니의 왼쪽 끝을 기준으로 잡으면 되고 첫번째 예제의 첫번째 케이스를 곰곰히 생각해보면 바구니의 사이즈가 1이고 왼쪽 1칸만 차지하고 있으니 2번째 칸에 떨어지는 사과는 .. 2021. 8. 25.
백준 2108 통계학 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 import sys from collections import Counter tmp=[] n=int(sys.stdin.readline()) for i in range(n): tmp.append(int(sys.stdin.readline())) tmp.sort() print( int (round (sum(tmp)/n,0))) print(tmp[n//2]) f = Counter(tmp) b = f.most_common() if len(tmp) > 1: if b[0][1] == b[1][1]: print(b[1][0]) else: print(b[0][0]) else: print(tmp[0]) print(tmp[.. 2021. 8. 24.
백준 10773 제로 1 2 3 4 5 6 7 8 import sys tmp=[] n=int(sys.stdin.readline()) for i in range(n): tmpNum=int(sys.stdin.readline()) if tmpNum==0: tmp.pop() else: tmp.append(tmpNum) print(sum(tmp)) cs 문제 보면 딱 드는 생각이 스택 떠오르시면 쉽게 푸실듯 2021. 8. 24.
백준 11651 좌표 정렬하기 2 1 2 3 4 5 6 7 8 import sys n=int(sys.stdin.readline()) tmp=[] for i in range(n): a,b=map(int, sys.stdin.readline().split()) tmp.append((a,b)) tmp=sorted(tmp, key= lambda x: (x[1],x[0])) for i in tmp: print(i[0],i[1]) cs lambda로 sort하시면됩니다 2021. 8. 24.