Compare commits
No commits in common. "e522ef06bdb4f06859dc77087c831b0a6fe472ab" and "7763227f07b098f4e9d8106a109f080053d4964c" have entirely different histories.
e522ef06bd
...
7763227f07
10 changed files with 0 additions and 202 deletions
|
|
@ -1,23 +0,0 @@
|
|||
# https://leetcode.com/problems/linked-list-cycle
|
||||
|
||||
from typing import Optional
|
||||
|
||||
class ListNode:
|
||||
val: int
|
||||
next: Optional["ListNode"]
|
||||
|
||||
class Solution:
|
||||
def hasCycle(self, head: Optional[ListNode]) -> bool:
|
||||
first: Optional[ListNode] = head
|
||||
second: Optional[ListNode] = head
|
||||
|
||||
while first is not None and \
|
||||
first.next is not None and \
|
||||
second is not None:
|
||||
|
||||
first = first.next.next
|
||||
second = second.next
|
||||
if first == second:
|
||||
return True
|
||||
return False
|
||||
|
||||
|
|
@ -1,11 +0,0 @@
|
|||
# https://leetcode.com/problems/maximum-difference-between-adjacent-elements-in-a-circular-array
|
||||
|
||||
from typing import List
|
||||
|
||||
class Solution:
|
||||
def maxAdjacentDistance(self, nums: List[int]) -> int:
|
||||
result = 0
|
||||
for i in range(len(nums)):
|
||||
result = max(abs(nums[i] - nums[i - 1]), result)
|
||||
return result
|
||||
|
||||
|
|
@ -1,16 +0,0 @@
|
|||
# https://leetcode.com/problems/maximum-difference-between-even-and-odd-frequency-i
|
||||
|
||||
from typing import Counter
|
||||
|
||||
class Solution:
|
||||
def maxDifference(self, s: str) -> int:
|
||||
c = Counter(s)
|
||||
max_odd = float("-inf")
|
||||
min_even = float("inf")
|
||||
for k in c:
|
||||
if c[k] % 2 == 1:
|
||||
max_odd = max(max_odd, c[k])
|
||||
else:
|
||||
min_even = min(min_even, c[k])
|
||||
return int(max_odd - min_even)
|
||||
|
||||
|
|
@ -1,14 +0,0 @@
|
|||
# https://leetcode.com/problems/maximum-difference-between-increasing-elements
|
||||
|
||||
from typing import List
|
||||
|
||||
class Solution:
|
||||
def maximumDifference(self, nums: List[int]) -> int:
|
||||
result = -1
|
||||
curr_min = nums[0]
|
||||
for i in range(1, len(nums)):
|
||||
if nums[i] > curr_min:
|
||||
result = max(result, nums[i] - curr_min)
|
||||
curr_min = min(curr_min, nums[i])
|
||||
return result
|
||||
|
||||
|
|
@ -1,24 +0,0 @@
|
|||
# https://leetcode.com/problems/maximum-difference-by-remapping-a-digit
|
||||
|
||||
from typing import List, Optional
|
||||
|
||||
class Solution:
|
||||
def minMaxDifference(self, num: int) -> int:
|
||||
n: List[str] = list(str(num))
|
||||
max_num: List[str] = []
|
||||
min_num: List[str] = []
|
||||
max_target: Optional[str] = None
|
||||
min_target: str = n[0]
|
||||
for i in range(len(n)):
|
||||
if n[i] == min_target:
|
||||
min_num.append('0')
|
||||
else:
|
||||
min_num.append(n[i])
|
||||
if max_target is None and n[i] != '9':
|
||||
max_target = n[i]
|
||||
if max_target == n[i]:
|
||||
max_num.append('9')
|
||||
else:
|
||||
max_num.append(n[i])
|
||||
return int(''.join(max_num)) - int(''.join(min_num))
|
||||
|
||||
|
|
@ -1,24 +0,0 @@
|
|||
# https://leetcode.com/problems/k-th-smallest-in-lexicographical-order
|
||||
|
||||
class Solution:
|
||||
def findKthNumber(self, n: int, k: int) -> int:
|
||||
def max_step(curr: int) -> int:
|
||||
step = 0
|
||||
first, last = curr, curr
|
||||
while first <= n:
|
||||
step += min(n, last) - first + 1
|
||||
first *= 10
|
||||
last = last * 10 + 9
|
||||
return step
|
||||
curr = 1
|
||||
k -= 1
|
||||
while k > 0:
|
||||
step = max_step(curr)
|
||||
if step <= k:
|
||||
k -= step
|
||||
curr += 1
|
||||
else:
|
||||
k -= 1
|
||||
curr *= 10
|
||||
return curr
|
||||
|
||||
|
|
@ -1,33 +0,0 @@
|
|||
# https://leetcode.com/problems/binary-tree-right-side-view
|
||||
|
||||
from collections import deque
|
||||
from typing import Deque, List, Optional, Set, Tuple
|
||||
|
||||
class TreeNode:
|
||||
def __init__(self, val=0, left=None, right=None):
|
||||
self.val = val
|
||||
self.left = left
|
||||
self.right = right
|
||||
|
||||
class Solution:
|
||||
def rightSideView(self, root: Optional[TreeNode]) -> List[int]:
|
||||
result: List[int] = []
|
||||
q: Deque[Tuple[TreeNode, int]] = deque()
|
||||
levels: Set[int] = set()
|
||||
|
||||
if root is None:
|
||||
return result
|
||||
|
||||
q.append((root, 0))
|
||||
while len(q) > 0:
|
||||
(curr, curr_level) = q.popleft()
|
||||
if curr_level not in levels:
|
||||
levels.add(curr_level)
|
||||
result.append(curr.val)
|
||||
if curr.right is not None:
|
||||
q.append((curr.right, curr_level + 1))
|
||||
if curr.left is not None:
|
||||
q.append((curr.left, curr_level + 1))
|
||||
|
||||
return result
|
||||
|
||||
|
|
@ -1,20 +0,0 @@
|
|||
# https://leetcode.com/problems/lexicographical-numbers
|
||||
|
||||
from typing import List
|
||||
|
||||
class Solution:
|
||||
def lexicalOrder(self, n: int) -> List[int]:
|
||||
result: List[int] = []
|
||||
s: List[int] = [int(x) for x in list(str(n))]
|
||||
def iterate(curr: int, length: int, curr_index: int):
|
||||
if curr_index >= len(s):
|
||||
return
|
||||
for num in range(1 if curr_index == 0 else 0, 10):
|
||||
new_curr = (curr * 10) + num
|
||||
if new_curr > n:
|
||||
break
|
||||
result.append(new_curr)
|
||||
iterate(new_curr, length + 1, curr_index + 1)
|
||||
|
||||
iterate(0, 0, 0)
|
||||
return result
|
||||
|
|
@ -1,17 +0,0 @@
|
|||
# https://leetcode.com/problems/lexicographically-minimum-string-after-removing-stars
|
||||
|
||||
import heapq
|
||||
|
||||
class Solution:
|
||||
def clearStars(self, s: str) -> str:
|
||||
result = list(s)
|
||||
h = []
|
||||
for i in range(len(s)):
|
||||
if s[i] == '*':
|
||||
(_, _, origin) = heapq.heappop(h)
|
||||
result[origin] = ''
|
||||
result[i] = ''
|
||||
else:
|
||||
heapq.heappush(h, (s[i], -i, i))
|
||||
return ''.join(result)
|
||||
|
||||
|
|
@ -1,20 +0,0 @@
|
|||
# https://leetcode.com/problems/max-difference-you-can-get-from-changing-an-integer
|
||||
|
||||
class Solution:
|
||||
def maxDiff(self, num: int) -> int:
|
||||
n = str(num)
|
||||
max_num: str = n
|
||||
min_num: str = n
|
||||
for digit in max_num:
|
||||
if digit != '9':
|
||||
max_num = max_num.replace(digit, '9')
|
||||
break
|
||||
for i in range(len(min_num)):
|
||||
if i == 0 and min_num[i] != '1':
|
||||
min_num = min_num.replace(min_num[i], '1')
|
||||
break
|
||||
if i != 0 and min_num[i] != '0' and min_num[i] != min_num[0]:
|
||||
min_num = min_num.replace(min_num[i], '0')
|
||||
break
|
||||
return int(max_num) - int(min_num)
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue