Compare commits
10 commits
7763227f07
...
e522ef06bd
| Author | SHA1 | Date | |
|---|---|---|---|
| e522ef06bd | |||
| 839160479e | |||
| f981ba7f45 | |||
| 47ddfc37e5 | |||
| e9aef2c95d | |||
| 74ea899d2c | |||
| b96b438b8a | |||
| 9d128a5f16 | |||
| 2cfc402fdd | |||
| cd0bb8d030 |
10 changed files with 202 additions and 0 deletions
23
easy/linked_list_cycle.py
Normal file
23
easy/linked_list_cycle.py
Normal file
|
|
@ -0,0 +1,23 @@
|
||||||
|
# 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
|
||||||
|
|
||||||
|
|
@ -0,0 +1,11 @@
|
||||||
|
# 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
|
||||||
|
|
||||||
16
easy/maximum_difference_between_even_and_odd_frequency_i.py
Normal file
16
easy/maximum_difference_between_even_and_odd_frequency_i.py
Normal file
|
|
@ -0,0 +1,16 @@
|
||||||
|
# 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)
|
||||||
|
|
||||||
14
easy/maximum_difference_between_increasing_elements.py
Normal file
14
easy/maximum_difference_between_increasing_elements.py
Normal file
|
|
@ -0,0 +1,14 @@
|
||||||
|
# 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
|
||||||
|
|
||||||
24
easy/maximum_difference_by_remapping_a_digit.py
Normal file
24
easy/maximum_difference_by_remapping_a_digit.py
Normal file
|
|
@ -0,0 +1,24 @@
|
||||||
|
# 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))
|
||||||
|
|
||||||
24
hard/k_th_smallest_in_lexicographical_order.py
Normal file
24
hard/k_th_smallest_in_lexicographical_order.py
Normal file
|
|
@ -0,0 +1,24 @@
|
||||||
|
# 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
|
||||||
|
|
||||||
33
medium/binary_tree_right_side_view.py
Normal file
33
medium/binary_tree_right_side_view.py
Normal file
|
|
@ -0,0 +1,33 @@
|
||||||
|
# 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
|
||||||
|
|
||||||
20
medium/lexicographical_numbers.py
Normal file
20
medium/lexicographical_numbers.py
Normal file
|
|
@ -0,0 +1,20 @@
|
||||||
|
# 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
|
||||||
|
|
@ -0,0 +1,17 @@
|
||||||
|
# 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)
|
||||||
|
|
||||||
|
|
@ -0,0 +1,20 @@
|
||||||
|
# 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