Initial migration

This commit is contained in:
pro100ton 2024-11-02 14:12:10 +03:00
commit d27d3e74e6
11 changed files with 260 additions and 0 deletions

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
.venv/

View file

@ -0,0 +1,52 @@
from typing import List
test_array = [1, 8, 6, 2, 5, 4, 8, 3, 7]
# Brute force method
def max_area(height: List[int]) -> int:
heightest_water = 0
for first_position, first_height in enumerate(height):
for second_position, second_height in enumerate(height):
if first_position == second_position:
continue
else:
shortest_line = (
first_height if first_height < second_height else second_height
)
length = abs(first_position - second_position)
tmp_area = shortest_line * length
if tmp_area > heightest_water:
heightest_water = tmp_area
return heightest_water
# O(n) solution
# Here we are using two pointer method that shifts to each other depending on what
# pointer line is taller
def maxArea(height: List[int]) -> int:
# Initializing two pointers
res = 0
l, r = 0, len(height) - 1
# Program will work until pointers meet each other
while l < r:
# Calculating area between two pointers
# First multiplier is length between two lines
# Second multiplier is height of shortest lines of two
area = (r - l) * min(height[l], height[r])
# Set result for max of current max value and calculated area
res = max(res, area)
# Now after we calculated area we need to shift our left or right pointer
# If height of left line is more than heigh of right line
if height[l] < height[r]:
# That means that left pointer should be shifted
l += 1
elif height[l] > height[r]:
# Otherwise right pointer should be shifted
r -= 1
else:
# If they are equal => we dont care what pointer should be moved
r -= 1
# l +=1 both
return res

View file

@ -0,0 +1,24 @@
Link to [task](https://leetcode.com/problems/container-with-most-water/)
You are given an integer array height of length n. There are n vertical lines drawn such that the two endpoints of the ith line are (i, 0) and (i, height[i]).
Find two lines that together with the x-axis form a container, such that the container contains the most water.
Return the maximum amount of water a container can store.
Notice that you may not slant the container.
Example 1:
Input: height = [1,8,6,2,5,4,8,3,7]
Output: 49
Explanation: The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In this case, the max area of water (blue section) the container can contain is 49.
Example 2:
Input: height = [1,1]
Output: 1
Constraints:
n == height.length
2 <= n <= 105
0 <= height[i] <= 104

7
poetry.lock generated Normal file
View file

@ -0,0 +1,7 @@
# This file is automatically @generated by Poetry 1.6.1 and should not be changed by hand.
package = []
[metadata]
lock-version = "2.0"
python-versions = "^3.11"
content-hash = "81b2fa642d7f2d1219cf80112ace12d689d053d81be7f7addb98144d56fc0fb2"

14
pyproject.toml Normal file
View file

@ -0,0 +1,14 @@
[tool.poetry]
name = "leetcode-practice"
version = "0.1.0"
description = ""
authors = ["pro100ton <pro100ton@gmail.com>"]
readme = "README.md"
[tool.poetry.dependencies]
python = "^3.8"
[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"

4
pyrightconfig.json Normal file
View file

@ -0,0 +1,4 @@
{
"venv": ".venv",
"venvPath": "/Users/antonsalimov/Projects/Learning/leetcode_practice"
}

View file

@ -0,0 +1,47 @@
"""
Given a signed 32-bit integer x, return x with its digits reversed. If reversing x
causes the value to go outside the signed 32-bit integer range [-231, 231 - 1],
then return 0.
Assume the environment does not allow you to store 64-bit integers (signed or unsigned).
Example 1:
Input: x = 123
Output: 321
Example 2:
Input: x = -123
Output: -321
Example 3:
Input: x = 120
Output: 21
"""
def reverse(x: int) -> int:
MAX_INT = 2 ** 31 - 1
MIN_INT = -2 ** 31
# Create flag of number negativity
is_negative = True if x < 0 else False
# Split number to list of digits
splitted_number = [int(num) for num in str(abs(x))]
# Reverse list
reversed_list = splitted_number[::-1]
# Create string from reversed list
answer = "".join(str(num) for num in reversed_list)
# Strip first zeroes from number
answer.lstrip("0")
# Convert string number to int number
number_answer = int(answer) * -1 if is_negative else int(answer)
# Check constraints
if number_answer > MAX_INT or number_answer < MIN_INT:
return 0
else:
return number_answer
print(reverse(-901000))
print(reverse(1534236469))
print(reverse(-65090))

View file

@ -0,0 +1,48 @@
import math
# Top Solution
class Solution:
def reverse(self, x: int) -> int:
MAX_INT = 2**31 - 1 # 2,147,483,647
MIN_INT = -(2**31) # -2,147,483,648
reverse = 0
while x != 0:
if reverse > MAX_INT / 10 or reverse < MIN_INT / 10:
return 0
digit = x % 10 if x > 0 else x % -10
reverse = reverse * 10 + digit
x = math.trunc(x / 10)
return reverse
# Some other Solution
class SolutionTwo:
def reverse(self, x: int) -> int:
flag = -1 if x < 0 else 1
s = str(abs(x))
x = int(s[::-1])
return x * flag if x < 2**31 else 0
class SolutionThree:
def reverse(self, x: int) -> int:
max_int = pow(2, 31) - 1
min_int = pow(-2, 31)
flag = -1 if x < 0 else 1
ans = 0
x = abs(x)
while x > 0:
digit = x % 10
if ans > max_int / 10:
return 0
ans = (ans * 10) + digit
x //= 10
return ans * flag

30
two_sums/task.md Normal file
View file

@ -0,0 +1,30 @@
Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
You can return the answer in any order.
Example 1:
Input: nums = [2,7,11,15], target = 9
Output: [0,1]
Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].
Example 2:
Input: nums = [3,2,4], target = 6
Output: [1,2]
Example 3:
Input: nums = [3,3], target = 6
Output: [0,1]
Constraints:
2 <= nums.length <= 104
-109 <= nums[i] <= 109
-109 <= target <= 109
Only one valid answer exists.

View file

@ -0,0 +1,19 @@
from typing import List
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
numMap = {}
n = len(nums)
# Build the hash table
for i in range(n):
numMap[nums[i]] = i
# Find the complement
for i in range(n):
complement = target - nums[i]
if complement in numMap and numMap[complement] != i:
return [i, numMap[complement]]
return [] # No solution found

13
two_sums/two_sums_my.py Normal file
View file

@ -0,0 +1,13 @@
from typing import List, Optional
class Solution:
def twoSum(self, nums: List[int], target: int) -> Optional[List[int]]:
for first_index, first_number in enumerate(nums):
for second_index, second_number in enumerate(nums):
if first_index == second_index:
pass
elif first_number + second_number == target:
return [first_index, second_index]
else:
continue