본문 바로가기

python-algorithm1422

leetcode 1816. Truncate Sentence 1 2 3 4 5 6 7 class Solution: def truncateSentence(self, s: str, k: int) -> str: sList=s.split() ans=sList[0] for i in range(1, min(k,len(sList))): ans=ans+' '+sList[i] return ans Colored by Color Scripter cs 2022. 3. 7.
leetcode 1688. Count of Matches in Tournament 1 2 3 4 5 6 7 class Solution: def numberOfMatches(self, n: int) -> int: ans=0 while n>1: ans+=n//2+n%2 n//=2 return ans cs 2022. 3. 7.
백준 9316 Hello Judge 1 2 3 n=int(input()) for i in range(1,n+1): print("Hello World, Judge %d!"%(i)) cs 2022. 3. 7.
Leetcode 2006. Count Number of Pairs With Absolute Difference K 1 2 3 4 5 6 7 class Solution: def countKDifference(self, nums: List[int], k: int) -> int: cnt=0 for i in range(len(nums)): for j in range(i+1,len(nums)): if abs (nums[i]-nums[j])==k: cnt+=1 return cnt Colored by Color Scripter cs 2022. 3. 7.
leetcode 2194. Cells in a Range on an Excel Sheet 1 2 3 4 5 6 7 8 9 10 11 12 class Solution: def cellsInRange(self, s: str) -> List[str]: ans=[] s1,s2=s.split(':') start=ord(s1[0]) end=ord(s2[0]) sheetStart=int(s1[1]) sheetEnd=int(s2[1]) for i in range(start,end+1): for j in range(sheetStart,sheetEnd+1): ans.append(chr(i)+str(j)) return ans Colored by Color Scripter cs 2022. 3. 7.
leetcode 2160. Minimum Sum of Four Digit Number After Splitting Digits 1 2 3 4 5 6 7 8 9 10 class Solution: def minimumSum(self, num: int) -> int: tmp1='' tmp2='' numStr=list(str(num)) numStr.sort() for i in range(len(numStr)): if i%2==0: tmp1+=numStr[i] else :tmp2+=numStr[i] return int(tmp1)+int(tmp2) cs 2022. 3. 7.
leetcode 2114. Maximum Number of Words Found in Sentences 1 2 3 4 5 6 class Solution: def mostWordsFound(self, sentences: List[str]) -> int: start=0 for i in sentences: start=max(start,i.count(' ')) return start+1 Colored by Color Scripter cs 2022. 3. 7.
leetcode 2011. Final Value of Variable After Performing Operations 1 2 3 4 5 6 7 class Solution: def finalValueAfterOperations(self, operations: List[str]) -> int: start=0 for i in operations: if '--' in i : start-=1 elif '++' in i : start+=1 return start Colored by Color Scripter cs 처음에는 prefix, suffix 구현인줄알았는데 아니네요 단순 사칙연산 2022. 3. 7.
백준 1920 수찾기 1 2 3 4 5 6 7 8 import sys n=int(sys.stdin.readline()) nList=list(map(int, sys.stdin.readline().split())) m=int(sys.stdin.readline()) mList=list(map(int, sys.stdin.readline().split())) for i in mList: if i in nList: print(1) else: print(0) Colored by Color Scripter cs 2022. 3. 7.
백준 24116 알고리즘 수업 - 피보나치 수 1 1 2 3 4 5 6 7 8 9 10 11 12 def fib(n): f=[[0,1],[1,0]] for i in range(2,n): f.append([f[i-1][0] + f[i-1][1] ,f[i-1][0] ] ) return sum(f[-1]) def fibonacci(n) : f=[0,1,1] for i in range(3,n+1): f.append( f[i - 1] + f[i - 2]) # 코드2 return f[n] n=int(input()) print(fib(n),n-2) Colored by Color Scripter cs 2022. 2. 7.
백준 2755 이번학기 평점은 몇점? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 from math import floor scores={'A+': 4.3, 'A0': 4.0, 'A-': 3.7, 'B+': 3.3, 'B0': 3.0, 'B-': 2.7, 'C+': 2.3, 'C0': 2.0, 'C-': 1.7, 'D+': 1.3, 'D0': 1.0, 'D-': 0.7, 'F': 0.0} n=int(input()) ans=0 sumScore=0 for i in range(n): name,score,grade=input().split() sumScore+=int(score) ans+=int(score)*scores[grade] tmpScore=(ans/sumScore)*1000 if tmpScor.. 2022. 1. 28.
백준 4562 No Brainer 1 2 3 4 5 n=int(input()) for i in range(n): a,b=map(int, input().split()) if a 2022. 1. 27.