Given a time represented in the format "HH:MM", form the next closest time by reusing the current digits. There is no limit on how many times a digit can be reused.
You may assume the given input string is always valid. For example, "01:34", "12:09" are all valid. "1:34", "12:9" are all invalid.
class Solution:
def nextClosestTime(self, time: str) -> str:
# time 에서 숫자만 떼서 저장 => t = [19, 34]
tmp = time.split(':')
t = [int(tmp[0]), int(tmp[1])]
# time 의 모든 숫자를 저장 => nums = [1, 9, 3, 4]
nums = []
for i in time:
if i != ":":
nums.append(int(i))
# 모든 조합 찾기
every = []
for i in range(len(nums)):
for j in range(len(nums)):
# 중복 제거 + 60 보다 작은 수만 사용
if nums[i]*10 + nums[j] not in every and nums[i]*10 + nums[j] < 60:
every.append(nums[i]*10 + nums[j])
# 순서대로 정렬
every.sort()
# 일단 minute 먼저 확인
for m in every:
if m > t[1]: # 주어진 minute 보다 크면 바로 return (every 가 정렬된 상태라 가장 최솟값임)
if m // 10 == 0:
return tmp[0] + ":" + "0" + str(m)
return tmp[0] + ":" + str(m)
# hour 잡아주기
for h in every:
# 주어진 hour 보다 큰 값을 찾기 +_minute 은 최솟값인 every[0] 이용
if h > t[0] and h < 24:
strRes = ""
if h // 10 == 0:
strRes += "0"
strRes += str(h) + ":"
if every[0] // 10 == 0:
strRes += "0"
strRes += str(every[0])
return strRes
# 주어진 hour 보다 큰 값이 없을 경우 -> 00 시부터 새로 봐야함
# => 가장 최솟값인 every[0] 사용해서 hour, minute 지정
strRes = ""
if every[0] // 10 == 0:
strRes += "0"
strRes += str(every[0]) + ":"
if every[0] // 10 == 0:
strRes += "0"
strRes += str(every[0])
return strRes
좀 조잡..
You have n bulbs in a row numbered from 1 to n. Initially, all the bulbs are turned off. We turn on exactly one bulb every day until all bulbs are on after n days.
You are given an array bulbs of length n where bulbs[i] = x means that on the (i+1)th day, we will turn on the bulb at position x where i is 0-indexed and x is 1-indexed.
Given an integer k, return the minimum day number such that there exists two turned on bulbs that have exactly k bulbs between them that are all turned off. If there isn't such day, return -1.
이건 걍 문제만 읽었는데 문제도 이해가 안됨ㅎ
class Solution:
def kEmptySlots(self, bulbs: List[int], k: int) -> int:
days = [0] * len(bulbs)
# enumerate => (index, value) | enumerate( , 1) => index + 1 값이 됨
for day, position in enumerate(bulbs, 1):
days[position - 1] = day
ans = float('inf')
left, right = 0, k+1
while right < len(days):
for i in range(left + 1, right):
if days[i] < days[left] or days[i] < days[right]:
left, right = i, i+k+1
break
else:
ans = min(ans, max(days[left], days[right]))
left, right = right, right+k+1
return ans if ans < float('inf') else -1
전에 풀었던 Sliding Window 를 이용
루션이도 이해가 안되네요^^