2020. 7. 23. 11:00ㆍ03. Resources/Python
Day_15
Intro
Q22. avoidObstacles
You are given an array of integers representing coordinates of obstacles situated on a straight line.
Assume that you are jumping from the point with coordinate 0 to the right. You are allowed only to make jumps of the same length represented by some integer.
Find the minimal length of the jump enough to avoid all the obstacles.
제출 코드
from math import gcd
def solution(arr):
def lcm(x,y):
return x*y // gcd(x,y)
while True:
arr.append(lcm(arr.pop(),arr.pop()))
if len(arr) == 1:
return arr[0]
def avoidObstacles(inputArray):
inputArray = [5, 3, 6, 7, 9]
inputSort = sorted(inputArray)
DiffNum = []
for i in range(len(inputSort)-1):
if inputSort[i] + 1 != inputSort[i+1]:
DiffNum.append(inputSort[i] + 1)
DiffDiffNum = []
for i in range(len(DiffNum)):
if i == 0:
DiffDiffNum.append(DiffNum[i])
else:
DiffDiffNum.append(DiffNum[i] - DiffNum[i-1])
MinLen = solution(DiffDiffNum)
return MinLen
제출 결과
2/12 tests passed.
Click the "Run Tests" button to see output, console logs, and detailed error messages for sample or custom test cases. This information is hidden when clicking the "Submit" button in order to prevent hard-coding solutions to the hidden test cases.
Sample tests: 2/6
Hidden tests: 0/6
Score: 33/300
각 값의 차이를 구하고, 그 차이 값의 최소공배수를 구해서 넘어가는 방식으로 코드 작성을 해봤는데, 아예 틀린 방법이었나보다.
작성 흐름
from math import gcd
# inputArray = [5, 3, 6, 7, 9]
inputArray = [1, 4, 10, 6, 2]
# 값을 정렬
inputSort = sorted(inputArray)
DiffNum = []
# 비어있는 부분 찾기
for i in range(len(inputSort)-1):
if inputSort[i] + 1 != inputSort[i+1]:
DiffNum.append(inputSort[i] + 1)
print(inputSort)
print(DiffNum)
# 비어있는 부분의 간격 계산
DiffDiffNum = []
for i in range(len(DiffNum)):
if i == 0:
DiffDiffNum.append(DiffNum[i])
else:
DiffDiffNum.append(DiffNum[i] - DiffNum[i-1])
print(DiffDiffNum)
# 간격들의 최소공배수 구하기
def solution(arr):
def lcm(x,y):
return x*y // gcd(x,y)
while True:
arr.append(lcm(arr.pop(),arr.pop()))
if len(arr) == 1:
return arr[0]
MinLen = solution(DiffDiffNum)
print(MinLen)
'03. Resources > Python' 카테고리의 다른 글
[Python 문제풀이] 17일차 (0) | 2020.07.25 |
---|---|
[Python 문제풀이] 16일차 (0) | 2020.07.24 |
[Python 문제풀이] 14일차 (0) | 2020.07.22 |
[Python 문제풀이] 13일차 (0) | 2020.07.21 |
[Python 문제풀이] 12일차 (0) | 2020.07.20 |