반응형

LeetCode/Medium 16

33. Search in Rotated Sorted Array

Python class Solution: def search(self, nums: List[int], target: int) -> int: left = 0 right = len(nums) - 1 while left 중간 오른쪽에 있음 left = mid + 1 else:#아니면 중간 왼쪽에 있음 right = mid - 1 else: #오른쪽이 가운데보다 작으면 if target nums[right]:#target이 가운데보다 작거나 맨 오른쪽보다 크면 -> 중간 왼쪽에 있음 right = mid - 1 else:#아니면 중간 오른쪽에 있음 left = mid + 1 return -1 # 첫번째 방법 # try: # return nums.index(target)..

LeetCode/Medium 2022.05.17

134. Gas Station

Python class Solution: def canCompleteCircuit(self, gas: List[int], cost: List[int]) -> int: tank = 0#현재 i부터 비용 계산 total = 0#전체 비용 다 계산 -> +시 index리턴, -시 -1리턴 index= 0#start_index for i in range(len(gas)): tank += gas[i] - cost[i] total += gas[i] - cost[i] if tank < 0: tank = 0 index = i + 1#현재 index에선 tank가 음수가 되니 다음 인덱스로 높여준다 if total < 0: return -1 return index # 시간초과 # len_gas = len(gas) # f..

LeetCode/Medium 2022.04.18

45. Jump Game II

Python - 코드 생각이 어려움 class Solution: def jump(self, nums: List[int]) -> int: if len(nums) visited_count:#현재 방문으로 체크된 곳이 현재 값보다 작으면 바꿔줌 visited_count = nums[i] if pre_visited_count == 0:#그 이전에 방문했던 카운트가 다 떨어졌으면 진짜 점프 해야 됨 jump += 1 pre_visited_count = visited_count return jump Java class Solution { public int jump(int[] nums) { if (nums.length nowCount) { nowCount = nums[i]; } if (preCount == 0) {..

LeetCode/Medium 2022.04.10
728x90
반응형