본문 바로가기

python-algorithm1422

Hacker rank A Very Big Sum 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 29 30 31 #!/bin/python3 import math import os import random import re import sys # # Complete the 'aVeryBigSum' function below. # # The function is expected to return a LONG_INTEGER. # The function accepts LONG_INTEGER_ARRAY ar as parameter. # def aVeryBigSum(ar): # Write your code here return sum(ar) if __name__ == '__ma.. 2021. 10. 26.
Hacker rank Compare the Triplets 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 29 30 31 32 33 34 35 36 37 38 39 40 #!/bin/python3 import math import os import random import re import sys # # Complete the 'compareTriplets' function below. # # The function is expected to return an INTEGER_ARRAY. # The function accepts following parameters: # 1. INTEGER_ARRAY a # 2. INTEGER_ARRAY b # def compareTriplet.. 2021. 10. 26.
Hacker rank Simple Array Sum 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 29 30 31 #!/bin/python3 import math import os import random import re import sys # # Complete the 'simpleArraySum' function below. # # The function is expected to return an INTEGER. # The function accepts INTEGER_ARRAY ar as parameter. # def simpleArraySum(ar): # Write your code here return sum(ar) if __name__ == '__main_.. 2021. 10. 26.
Hacker rank Solve Me First 1 2 3 4 5 6 7 8 def solveMeFirst(a,b): # Hint: Type return a+b below return a+b num1 = int(input()) num2 = int(input()) res = solveMeFirst(num1,num2) print(res) cs 2021. 10. 26.
Leetcode 136. Single Number 1 2 3 4 pythonclass Solution: def singleNumber(self, nums: List[int]) -> int: for i in nums: if nums.count(i)==1: return i cs 2021. 9. 24.
Leetcode 1614. Maximum Nesting Depth of the Parentheses 참고: leetcode r0bertz 1 2 3 4 5 6 7 8 9 class Solution: def maxDepth(self, s: str) -> int: ans=cnt=0 for i in s: if i =="(" : cnt+=1 ans=max(ans,cnt) elif i==")" : cnt-=1 return ans cs 2021. 8. 31.
Leetcode 1979. Find Greatest Common Divisor of Array 1 2 3 4 5 6 7 8 class Solution: def gcd(a,b): if(b==0): return a else: return gcd(b,a%b) def findGCD(self, nums: List[int]) -> int: return ( gcd(max(nums),min(nums))) cs gcd구하는 함수는 재귀로 짜고 nums에서 가장 큰 값, 가장 작은 값의 gcd를 return 하면됩니다 주의할점은 a가 b보다 크거나 같아야됩니다 2021. 8. 31.
Leetcode 1588. Sum of All Odd Length Subarrays 1 2 3 4 5 6 7 class Solution: def sumOddLengthSubarrays(self, arr: List[int]) -> int: ans=0 for i in range(len(arr)): for j in range(i, len(arr),2): ans+=sum(arr[i:j+1]) return ans Colored by Color Scripter cs 2021. 8. 31.
Leetcode 1720. Decode XORed Array 1 2 3 4 5 6 class Solution: def decode(self, encoded: List[int], first: int) -> List[int]: ans=[first] for i in encoded: ans.append(i^ans[-1]) return ans Colored by Color Scripter cs XOR -> Exclusive OR 문제구요 학부때 배웠던 Logical circuit 생각해보시면 쉽게 풀수있는데 파이썬에서는 " ^ " operator가 XOR 입니다. 아마 다른언어에서는 power의 개념으로 쓰이는 operator죠 2021. 8. 31.
Leetcode 682. Baseball Game 1 2 3 4 5 6 7 8 9 class Solution: def calPoints(self, ops: List[str]) -> int: tmp=[] for i in ops: if i=='C': tmp.pop() elif i=='D':tmp.append (tmp[-1]*2) elif i=="+":tmp.append(tmp[-1]+tmp[-2]) else : tmp.append(int(i)) return (sum(tmp)) cs 2021. 8. 30.
Leetcode 1441 Build an Array with Stack Operations 1 2 3 4 5 6 7 8 9 10 11 class Solution: def buildArray(self, target: List[int], n: int) -> List[str]: myn=max(target) tmp= [i+1 for i in range(myn)] ans=[] for i in tmp: if i in target: ans.append("Push") else: ans.append("Push") ans.append("Pop") return ans Colored by Color Scripter cs n으로 만들어진 List를 바탕으로 target을 만들어야하는데 문제에서 언급한 Push, Pop 으로 만들면되는데 주의할것이 target의 가장 큰 값을 for문의 range에 넣으면됩니다 2021. 8. 30.
백준 3135 라디오 1 2 3 4 5 6 7 8 9 import sys a,b=map(int, sys.stdin.readline().split()) n=int(sys.stdin.readline()) tmp=[] for i in range(n): tmp.append(abs(b- int(sys.stdin.readline()))) heu=min(tmp) if abs(a-b)>heu: print(heu+1) else : print(abs(a-b)) cs 첫번째 , 두번째 버튼을 눌러서 가는게 더 빠른가 즐겨찾기를 통해 가는게 더 빠른가 비교하는 로직을 통해 구현하시면됩니당 2021. 8. 30.