let's solve more as a compensation
This commit is contained in:
parent
aa5073c881
commit
f7c7d36031
15
easy/distribute-candies.py
Normal file
15
easy/distribute-candies.py
Normal file
@ -0,0 +1,15 @@
|
||||
#!/usr/bin/python
|
||||
# yigid balaban <fyb@fybx.dev>
|
||||
# neetcode 2024
|
||||
# easy / distribute candies
|
||||
|
||||
def solution(candyType: list[int]) -> int:
|
||||
howManyTypes = len(set(candyType))
|
||||
permitAmount = len(candyType) // 2
|
||||
return permitAmount if howManyTypes >= permitAmount else howManyTypes
|
||||
|
||||
|
||||
print(solution([1,1,2,2,3,3]) == 3)
|
||||
print(solution([1,1,2,3]) == 2)
|
||||
print(solution([6,6,6,6]) == 1)
|
||||
|
35
easy/find-the-index-of-the-first-occurence-in-string.py
Normal file
35
easy/find-the-index-of-the-first-occurence-in-string.py
Normal file
@ -0,0 +1,35 @@
|
||||
#!/usr/bin/python
|
||||
# yigid balaban <fyb@fybx.dev>
|
||||
# neetcode 2024
|
||||
# easy / find the first occurence in a string
|
||||
|
||||
# TC: O(n), MC: O(1)
|
||||
def solution(haystack: str, needle: str) -> int:
|
||||
ph, pn = 0, 0
|
||||
position = -1
|
||||
while ph < len(haystack):
|
||||
print(ph, haystack[ph], pn, needle[pn])
|
||||
if haystack[ph] == needle[pn]:
|
||||
if position == -1:
|
||||
position = ph
|
||||
pn += 1
|
||||
if pn == len(needle):
|
||||
break
|
||||
else:
|
||||
if pn != 0:
|
||||
ph = position
|
||||
position, pn = -1, 0
|
||||
ph += 1
|
||||
return position if pn == len(needle) else -1
|
||||
|
||||
|
||||
print(solution('sadbutsad', 'sad') == 0)
|
||||
print(solution('leetcode', 'lecola') == -1)
|
||||
print(solution('leetcode', 'etc') == 2)
|
||||
print(solution('a', 'a') == 0)
|
||||
print(solution('bbbab', 'ab') == 3)
|
||||
print(solution('bbbaaaab', 'ab') == 6)
|
||||
print(solution('aa', 'aaa') == -1)
|
||||
print(solution('a', 'aaa') == -1)
|
||||
print(solution('mississippi', 'issip') == 4)
|
||||
|
13
easy/palindrome-number.py
Normal file
13
easy/palindrome-number.py
Normal file
@ -0,0 +1,13 @@
|
||||
#!/usr/bin/python
|
||||
# yigid balaban <fyb@fybx.dev>
|
||||
# neetcode 2024
|
||||
# easy / palindrome number
|
||||
|
||||
def solution(x: int) -> bool:
|
||||
return str(x) == str(x)[::-1]
|
||||
|
||||
|
||||
print(solution(121) == True)
|
||||
print(solution(0) == True)
|
||||
print(solution(-121) == False)
|
||||
print(solution(10) == False)
|
Loading…
x
Reference in New Issue
Block a user