본문 바로가기

python-algorithm1422

백준 10866 덱 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 from collections import deque import sys ans=deque() n=int(input()) for i in range(n): tmp=list(sys.stdin.readline().split()) if tmp[0]=='push_back' : ans.append(int(tmp[1])) if tmp[0] == 'push_front': ans.appendleft(int(tmp[1])) if tmp[0]=='front' : if len(ans)==0: print(-1) else: print(ans[0]) if tmp[0]=='back' : if len.. 2021. 8. 24.
백준 10845 큐 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 from collections import deque import sys ans=deque() n=int(input()) for i in range(n): tmp=list(sys.stdin.readline().split()) if tmp[0]=='push' : ans.append(int(tmp[1])) if tmp[0]=='front' : if len(ans)==0: print(-1) else: print(ans[0]) if tmp[0]=='back' : if len(ans)==0: print(-1) else: print(ans[-1]) if tmp[0]=='size': print(len(ans)) if.. 2021. 8. 24.
백준 10824 스택 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 from collections import deque import sys ans=deque() n=int(input()) for i in range(n): tmp=list(sys.stdin.readline().split()) if tmp[0]=='push' : ans.append(int(tmp[1])) if tmp[0]=='top' : if len(ans)==0: print(-1) else: print(ans[-1]) if tmp[0]=='size': print(len(ans)) if tmp[0]=='empty': if len(ans)==0 : print(1) else :print (0) if tmp[0]=='pop':.. 2021. 8. 24.
백준 2164 카드 2 1 2 3 4 5 6 7 8 from collections import deque n=int(input()) tmp=deque([i+1 for i in range(n)]) while len(tmp)>1: tmp.popleft() tmpNum=tmp.popleft() tmp.append(tmpNum) print(tmp[0]) cs 1 2 3 4 5 6 7 from collections import deque n=int(input()) tmp=deque([i+1 for i in range(n)]) while len(tmp)>1: tmp.popleft() tmp.rotate(-1) print(tmp[0]) cs 제일 처음 생각했던건 리스트를 써서 pop을 두번하자 였는데 시간초과 나오더라구요 그래서 colle.. 2021. 8. 24.
백준 10814 나이순 정렬 1 2 3 4 5 6 7 8 9 10 import sys n=int(sys.stdin.readline()) tmp=[] for i in range(n): a,b=sys.stdin.readline().split() a=int(a) tmp.append((a,b,i)) tmp=sorted(tmp,key= lambda x: (x[0],x[2]) ) for i in tmp: print(i[0],i[1]) cs 이 문제도 크게 어렵진 않구 다만 들어온 순서 고려하는 부분이 line 7, line8 에 각각 있습니다 2021. 8. 23.
백준 11650 좌표 정렬하기 1 2 3 4 5 6 7 8 9 pythotmp=[] n=int(input()) for i in range(n): a,b=map(int,input().split()) tmp.append((a,b)) tmp=sorted(tmp, key= lambda x: (x[0],x[1])) for i in tmp: print (i[0],i[1]) cs 요거는 lambda 이용해서 sort 두번 하면됩니다! 2021. 8. 23.
백준 2702 초6 수학 1 2 3 4 5 6 7 8 9 10 def gcd(a,b): if(b==0): return a else: return gcd(b,a%b) n=int(input()) for i in range(n): a,b=map(int,input().split()) print(a*b//gcd(a,b),gcd(a,b)) cs 나름 학교다닐때 모의고사보면 수리영역 1등급이였는데 나이먹고 수학관련 문제보니 제목이 초6수학인데도 어지럽네요 여튼 GCD , Greatest Common Divisor 최대공약수를 찾으면 최소공배수까지 쉽게 구할수있죠 2021. 8. 19.
백준 3040 백설 공주와 일곱 난쟁이 1 2 3 4 5 6 7 8 9 10 11 12 tmp=[] for i in range(9): tmp.append(int(input())) for i in range(9): for j in range(i+1,9): if sum(tmp)-tmp[i]-tmp[j]==100: x,y=i,j break tmp.pop(x) tmp.pop(y-1) for i in tmp: print(i) cs x번째 index를 pop하면 리스트의 길이가 하나 줄어드니 y-1번째 index를 pop해야함. list. pop("index") list.remove("value") pop과 remove의 간단한 차이입니다. 2021. 8. 19.
Leetcode Add Strings 1 2 3 4 class Solution: def addStrings(self, num1: str, num2: str) -> str: ans=int(num1)+int(num2) return str(ans) Colored by Color Scripter cs type casting 하면 됩니다 쉬워요 2021. 8. 19.
Leetcode 4. Median of Two Sorted Arrays 1 2 3 4 5 6 7 pythoclass Solution: def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float: for i in nums2:nums1.append(i) nums1.sort() chk=len(nums1) if chk%2==1 : return nums1[chk//2] else: return (nums1[chk//2]+nums1[chk//2-1])/2 cs 2021. 8. 19.
Leetcode 1822. Sign of the Product of an Array 1 2 3 4 5 6 7 8 pythonclass Solution: def arraySign(self, nums: List[int]) -> int: cnt=1 for i in nums: cnt*=i if cnt>0 : return 1 elif cnt ==0 : return 0 else : return -1 cs 문제 자체가 크게 어렵지 않아요 입력받은 리스트의 하나하나 곱한것의 부호 에 따라서 리턴해주면 되는 문제입니다. 2021. 8. 19.
Leetcode 344. Reverse String 1 2 3 4 5 6 class Solution: def reverseString(self, s: List[str]) -> None: """ Do not return anything, modify s in-place instead. """ s.reverse() Colored by Color Scripter cs 내장함수 reverse() 쓰면됩니당 2021. 8. 17.