commit d27d3e74e6452589d8ce1ec147cfa376550f1dfa Author: pro100ton Date: Sat Nov 2 14:12:10 2024 +0300 Initial migration diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..def4cc9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.venv/ + diff --git a/container_with_most_water/main.py b/container_with_most_water/main.py new file mode 100644 index 0000000..9767ec5 --- /dev/null +++ b/container_with_most_water/main.py @@ -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 + diff --git a/container_with_most_water/task.md b/container_with_most_water/task.md new file mode 100644 index 0000000..d226c5a --- /dev/null +++ b/container_with_most_water/task.md @@ -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 diff --git a/poetry.lock b/poetry.lock new file mode 100644 index 0000000..24cf95d --- /dev/null +++ b/poetry.lock @@ -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" diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..57cf6b4 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,14 @@ +[tool.poetry] +name = "leetcode-practice" +version = "0.1.0" +description = "" +authors = ["pro100ton "] +readme = "README.md" + +[tool.poetry.dependencies] +python = "^3.8" + + +[build-system] +requires = ["poetry-core"] +build-backend = "poetry.core.masonry.api" diff --git a/pyrightconfig.json b/pyrightconfig.json new file mode 100644 index 0000000..dbeb49d --- /dev/null +++ b/pyrightconfig.json @@ -0,0 +1,4 @@ +{ + "venv": ".venv", + "venvPath": "/Users/antonsalimov/Projects/Learning/leetcode_practice" +} diff --git a/reversed_integer/my_solution.py b/reversed_integer/my_solution.py new file mode 100644 index 0000000..22f2590 --- /dev/null +++ b/reversed_integer/my_solution.py @@ -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)) diff --git a/reversed_integer/other_solutions.py b/reversed_integer/other_solutions.py new file mode 100644 index 0000000..618eb65 --- /dev/null +++ b/reversed_integer/other_solutions.py @@ -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 diff --git a/two_sums/task.md b/two_sums/task.md new file mode 100644 index 0000000..07e7a05 --- /dev/null +++ b/two_sums/task.md @@ -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. + diff --git a/two_sums/two_sums_correct.py b/two_sums/two_sums_correct.py new file mode 100644 index 0000000..1af58dd --- /dev/null +++ b/two_sums/two_sums_correct.py @@ -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 diff --git a/two_sums/two_sums_my.py b/two_sums/two_sums_my.py new file mode 100644 index 0000000..85eb766 --- /dev/null +++ b/two_sums/two_sums_my.py @@ -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