Add comprehensive LeetCode solutions index documentation
This commit is contained in:
371
.gitignore
vendored
Normal file
371
.gitignore
vendored
Normal file
@@ -0,0 +1,371 @@
|
|||||||
|
# Created by https://www.toptal.com/developers/gitignore/api/node,java,python,c++
|
||||||
|
# Edit at https://www.toptal.com/developers/gitignore?templates=node,java,python,c++
|
||||||
|
.cursor*
|
||||||
|
### C++ ###
|
||||||
|
# Prerequisites
|
||||||
|
*.d
|
||||||
|
|
||||||
|
# Compiled Object files
|
||||||
|
*.slo
|
||||||
|
*.lo
|
||||||
|
*.o
|
||||||
|
*.obj
|
||||||
|
|
||||||
|
# Precompiled Headers
|
||||||
|
*.gch
|
||||||
|
*.pch
|
||||||
|
|
||||||
|
# Compiled Dynamic libraries
|
||||||
|
*.so
|
||||||
|
*.dylib
|
||||||
|
*.dll
|
||||||
|
|
||||||
|
# Fortran module files
|
||||||
|
*.mod
|
||||||
|
*.smod
|
||||||
|
|
||||||
|
# Compiled Static libraries
|
||||||
|
*.lai
|
||||||
|
*.la
|
||||||
|
*.a
|
||||||
|
*.lib
|
||||||
|
|
||||||
|
# Executables
|
||||||
|
*.exe
|
||||||
|
*.out
|
||||||
|
*.app
|
||||||
|
|
||||||
|
### Java ###
|
||||||
|
# Compiled class file
|
||||||
|
*.class
|
||||||
|
|
||||||
|
# Log file
|
||||||
|
*.log
|
||||||
|
|
||||||
|
# BlueJ files
|
||||||
|
*.ctxt
|
||||||
|
|
||||||
|
# Mobile Tools for Java (J2ME)
|
||||||
|
.mtj.tmp/
|
||||||
|
|
||||||
|
# Package Files #
|
||||||
|
*.jar
|
||||||
|
*.war
|
||||||
|
*.nar
|
||||||
|
*.ear
|
||||||
|
*.zip
|
||||||
|
*.tar.gz
|
||||||
|
*.rar
|
||||||
|
|
||||||
|
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
|
||||||
|
hs_err_pid*
|
||||||
|
replay_pid*
|
||||||
|
|
||||||
|
### Node ###
|
||||||
|
# Logs
|
||||||
|
logs
|
||||||
|
npm-debug.log*
|
||||||
|
yarn-debug.log*
|
||||||
|
yarn-error.log*
|
||||||
|
lerna-debug.log*
|
||||||
|
.pnpm-debug.log*
|
||||||
|
|
||||||
|
# Diagnostic reports (https://nodejs.org/api/report.html)
|
||||||
|
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
|
||||||
|
|
||||||
|
# Runtime data
|
||||||
|
pids
|
||||||
|
*.pid
|
||||||
|
*.seed
|
||||||
|
*.pid.lock
|
||||||
|
|
||||||
|
# Directory for instrumented libs generated by jscoverage/JSCover
|
||||||
|
lib-cov
|
||||||
|
|
||||||
|
# Coverage directory used by tools like istanbul
|
||||||
|
coverage
|
||||||
|
*.lcov
|
||||||
|
|
||||||
|
# nyc test coverage
|
||||||
|
.nyc_output
|
||||||
|
|
||||||
|
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
|
||||||
|
.grunt
|
||||||
|
|
||||||
|
# Bower dependency directory (https://bower.io/)
|
||||||
|
bower_components
|
||||||
|
|
||||||
|
# node-waf configuration
|
||||||
|
.lock-wscript
|
||||||
|
|
||||||
|
# Compiled binary addons (https://nodejs.org/api/addons.html)
|
||||||
|
build/Release
|
||||||
|
|
||||||
|
# Dependency directories
|
||||||
|
node_modules/
|
||||||
|
jspm_packages/
|
||||||
|
|
||||||
|
# Snowpack dependency directory (https://snowpack.dev/)
|
||||||
|
web_modules/
|
||||||
|
|
||||||
|
# TypeScript cache
|
||||||
|
*.tsbuildinfo
|
||||||
|
|
||||||
|
# Optional npm cache directory
|
||||||
|
.npm
|
||||||
|
|
||||||
|
# Optional eslint cache
|
||||||
|
.eslintcache
|
||||||
|
|
||||||
|
# Optional stylelint cache
|
||||||
|
.stylelintcache
|
||||||
|
|
||||||
|
# Microbundle cache
|
||||||
|
.rpt2_cache/
|
||||||
|
.rts2_cache_cjs/
|
||||||
|
.rts2_cache_es/
|
||||||
|
.rts2_cache_umd/
|
||||||
|
|
||||||
|
# Optional REPL history
|
||||||
|
.node_repl_history
|
||||||
|
|
||||||
|
# Output of 'npm pack'
|
||||||
|
*.tgz
|
||||||
|
|
||||||
|
# Yarn Integrity file
|
||||||
|
.yarn-integrity
|
||||||
|
|
||||||
|
# dotenv environment variable files
|
||||||
|
.env
|
||||||
|
.env.development.local
|
||||||
|
.env.test.local
|
||||||
|
.env.production.local
|
||||||
|
.env.local
|
||||||
|
|
||||||
|
# parcel-bundler cache (https://parceljs.org/)
|
||||||
|
.cache
|
||||||
|
.parcel-cache
|
||||||
|
|
||||||
|
# Next.js build output
|
||||||
|
.next
|
||||||
|
out
|
||||||
|
|
||||||
|
# Nuxt.js build / generate output
|
||||||
|
.nuxt
|
||||||
|
dist
|
||||||
|
|
||||||
|
# Gatsby files
|
||||||
|
.cache/
|
||||||
|
# Comment in the public line in if your project uses Gatsby and not Next.js
|
||||||
|
# https://nextjs.org/blog/next-9-1#public-directory-support
|
||||||
|
# public
|
||||||
|
|
||||||
|
# vuepress build output
|
||||||
|
.vuepress/dist
|
||||||
|
|
||||||
|
# vuepress v2.x temp and cache directory
|
||||||
|
.temp
|
||||||
|
|
||||||
|
# Docusaurus cache and generated files
|
||||||
|
.docusaurus
|
||||||
|
|
||||||
|
# Serverless directories
|
||||||
|
.serverless/
|
||||||
|
|
||||||
|
# FuseBox cache
|
||||||
|
.fusebox/
|
||||||
|
|
||||||
|
# DynamoDB Local files
|
||||||
|
.dynamodb/
|
||||||
|
|
||||||
|
# TernJS port file
|
||||||
|
.tern-port
|
||||||
|
|
||||||
|
# Stores VSCode versions used for testing VSCode extensions
|
||||||
|
.vscode-test
|
||||||
|
|
||||||
|
# yarn v2
|
||||||
|
.yarn/cache
|
||||||
|
.yarn/unplugged
|
||||||
|
.yarn/build-state.yml
|
||||||
|
.yarn/install-state.gz
|
||||||
|
.pnp.*
|
||||||
|
|
||||||
|
### Node Patch ###
|
||||||
|
# Serverless Webpack directories
|
||||||
|
.webpack/
|
||||||
|
|
||||||
|
# Optional stylelint cache
|
||||||
|
|
||||||
|
# SvelteKit build / generate output
|
||||||
|
.svelte-kit
|
||||||
|
|
||||||
|
### Python ###
|
||||||
|
# Byte-compiled / optimized / DLL files
|
||||||
|
__pycache__/
|
||||||
|
*.py[cod]
|
||||||
|
*$py.class
|
||||||
|
|
||||||
|
# C extensions
|
||||||
|
|
||||||
|
# Distribution / packaging
|
||||||
|
.Python
|
||||||
|
build/
|
||||||
|
develop-eggs/
|
||||||
|
dist/
|
||||||
|
downloads/
|
||||||
|
eggs/
|
||||||
|
.eggs/
|
||||||
|
lib/
|
||||||
|
lib64/
|
||||||
|
parts/
|
||||||
|
sdist/
|
||||||
|
var/
|
||||||
|
wheels/
|
||||||
|
share/python-wheels/
|
||||||
|
*.egg-info/
|
||||||
|
.installed.cfg
|
||||||
|
*.egg
|
||||||
|
MANIFEST
|
||||||
|
|
||||||
|
# PyInstaller
|
||||||
|
# Usually these files are written by a python script from a template
|
||||||
|
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
||||||
|
*.manifest
|
||||||
|
*.spec
|
||||||
|
|
||||||
|
# Installer logs
|
||||||
|
pip-log.txt
|
||||||
|
pip-delete-this-directory.txt
|
||||||
|
|
||||||
|
# Unit test / coverage reports
|
||||||
|
htmlcov/
|
||||||
|
.tox/
|
||||||
|
.nox/
|
||||||
|
.coverage
|
||||||
|
.coverage.*
|
||||||
|
nosetests.xml
|
||||||
|
coverage.xml
|
||||||
|
*.cover
|
||||||
|
*.py,cover
|
||||||
|
.hypothesis/
|
||||||
|
.pytest_cache/
|
||||||
|
cover/
|
||||||
|
|
||||||
|
# Translations
|
||||||
|
*.mo
|
||||||
|
*.pot
|
||||||
|
|
||||||
|
# Django stuff:
|
||||||
|
local_settings.py
|
||||||
|
db.sqlite3
|
||||||
|
db.sqlite3-journal
|
||||||
|
|
||||||
|
# Flask stuff:
|
||||||
|
instance/
|
||||||
|
.webassets-cache
|
||||||
|
|
||||||
|
# Scrapy stuff:
|
||||||
|
.scrapy
|
||||||
|
|
||||||
|
# Sphinx documentation
|
||||||
|
docs/_build/
|
||||||
|
|
||||||
|
# PyBuilder
|
||||||
|
.pybuilder/
|
||||||
|
target/
|
||||||
|
|
||||||
|
# Jupyter Notebook
|
||||||
|
.ipynb_checkpoints
|
||||||
|
|
||||||
|
# IPython
|
||||||
|
profile_default/
|
||||||
|
ipython_config.py
|
||||||
|
|
||||||
|
# pyenv
|
||||||
|
# For a library or package, you might want to ignore these files since the code is
|
||||||
|
# intended to run in multiple environments; otherwise, check them in:
|
||||||
|
# .python-version
|
||||||
|
|
||||||
|
# pipenv
|
||||||
|
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
||||||
|
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
||||||
|
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
||||||
|
# install all needed dependencies.
|
||||||
|
#Pipfile.lock
|
||||||
|
|
||||||
|
# poetry
|
||||||
|
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
||||||
|
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
||||||
|
# commonly ignored for libraries.
|
||||||
|
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
||||||
|
#poetry.lock
|
||||||
|
|
||||||
|
# pdm
|
||||||
|
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
||||||
|
#pdm.lock
|
||||||
|
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
|
||||||
|
# in version control.
|
||||||
|
# https://pdm.fming.dev/#use-with-ide
|
||||||
|
.pdm.toml
|
||||||
|
|
||||||
|
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
||||||
|
__pypackages__/
|
||||||
|
|
||||||
|
# Celery stuff
|
||||||
|
celerybeat-schedule
|
||||||
|
celerybeat.pid
|
||||||
|
|
||||||
|
# SageMath parsed files
|
||||||
|
*.sage.py
|
||||||
|
|
||||||
|
# Environments
|
||||||
|
.venv
|
||||||
|
env/
|
||||||
|
venv/
|
||||||
|
ENV/
|
||||||
|
env.bak/
|
||||||
|
venv.bak/
|
||||||
|
|
||||||
|
# Spyder project settings
|
||||||
|
.spyderproject
|
||||||
|
.spyproject
|
||||||
|
|
||||||
|
# Rope project settings
|
||||||
|
.ropeproject
|
||||||
|
|
||||||
|
# mkdocs documentation
|
||||||
|
/site
|
||||||
|
|
||||||
|
# mypy
|
||||||
|
.mypy_cache/
|
||||||
|
.dmypy.json
|
||||||
|
dmypy.json
|
||||||
|
|
||||||
|
# Pyre type checker
|
||||||
|
.pyre/
|
||||||
|
|
||||||
|
# pytype static type analyzer
|
||||||
|
.pytype/
|
||||||
|
|
||||||
|
# Cython debug symbols
|
||||||
|
cython_debug/
|
||||||
|
|
||||||
|
# PyCharm
|
||||||
|
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
||||||
|
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
||||||
|
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
||||||
|
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
||||||
|
#.idea/
|
||||||
|
|
||||||
|
### Python Patch ###
|
||||||
|
# Poetry local configuration file - https://python-poetry.org/docs/configuration/#local-configuration
|
||||||
|
poetry.toml
|
||||||
|
|
||||||
|
# ruff
|
||||||
|
.ruff_cache/
|
||||||
|
|
||||||
|
# LSP config files
|
||||||
|
pyrightconfig.json
|
||||||
|
|
||||||
|
# End of https://www.toptal.com/developers/gitignore/api/node,java,python,c++
|
||||||
21
LICENSE
Normal file
21
LICENSE
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
MIT License
|
||||||
|
|
||||||
|
Copyright (c) 2025 CarGDev
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
||||||
335
README.md
Normal file
335
README.md
Normal file
@@ -0,0 +1,335 @@
|
|||||||
|
# LeetCode Solutions
|
||||||
|
|
||||||
|
This repository contains my solutions to various LeetCode problems. Each solution is implemented in Python and follows a consistent structure.
|
||||||
|
|
||||||
|
## 📊 Statistics
|
||||||
|
- **Total Problems Solved**: 90+
|
||||||
|
- **Languages**: Python, TypeScript
|
||||||
|
- **Difficulty Distribution**: Easy, Medium, Hard
|
||||||
|
|
||||||
|
## 📁 Solutions Index
|
||||||
|
|
||||||
|
### 🔢 Arrays & Strings
|
||||||
|
|
||||||
|
#### Easy
|
||||||
|
- [1. Two Sum](src/exercises/1.two-sum.py) - Hash Table, Array
|
||||||
|
- [9. Palindrome Number](src/exercises/9.palindrome-number.py) - Math
|
||||||
|
- [13. Roman to Integer](src/exercises/13.roman-to-integer.py) - Hash Table, Math, String
|
||||||
|
- [14. Longest Common Prefix](src/exercises/14.longest-common-prefix.py) - String, Trie
|
||||||
|
- [20. Valid Parentheses](src/exercises/20.valid-parentheses.py) - Stack, String
|
||||||
|
- [21. Merge Two Sorted Lists](src/exercises/21.merge-two-sorted-lists.py) - Linked List, Recursion
|
||||||
|
- [26. Remove Duplicates from Sorted Array](src/exercises/80.remove-duplicates-from-sorted-array-ii.py) - Array, Two Pointers
|
||||||
|
- [27. Remove Element](src/exercises/80.remove-duplicates-from-sorted-array-ii.py) - Array, Two Pointers
|
||||||
|
- [28. Find the Index of the First Occurrence in a String](src/exercises/28.find-the-index-of-the-first-occurrence-in-a-string.py) - String, Two Pointers
|
||||||
|
- [35. Search Insert Position](src/exercises/35.search-insert-position.py) - Array, Binary Search
|
||||||
|
- [66. Plus One](src/exercises/66.plus-one.py) - Array, Math
|
||||||
|
- [69. Sqrt(x)](src/exercises/69.sqrtx.py) - Math, Binary Search
|
||||||
|
- [88. Merge Sorted Array](src/exercises/88.merge-sorted-array.py) - Array, Two Pointers
|
||||||
|
- [125. Valid Palindrome](src/exercises/125.valid-palindrome.py) - String, Two Pointers
|
||||||
|
- [169. Majority Element](src/exercises/169.majority-element.py) - Array, Hash Table, Divide and Conquer
|
||||||
|
- [202. Happy Number](src/exercises/202.happy-number.py) - Hash Table, Math, Two Pointers
|
||||||
|
- [205. Isomorphic Strings](src/exercises/205.isomorphic-strings.py) - Hash Table, String
|
||||||
|
- [217. Contains Duplicate](src/exercises/217.contains-duplicate.py) - Array, Hash Table, Sorting
|
||||||
|
- [219. Contains Duplicate II](src/exercises/219.contains-duplicate-ii.py) - Array, Hash Table, Sliding Window
|
||||||
|
- [242. Valid Anagram](src/exercises/242.valid-anagram.py) - String, Sorting, Hash Table
|
||||||
|
- [290. Word Pattern](src/exercises/290.word-pattern.py) - Hash Table, String
|
||||||
|
- [383. Ransom Note](src/exercises/383.ransom-note.py) - String, Hash Table, Counting
|
||||||
|
- [392. Is Subsequence](src/exercises/392.is-subsequence.py) - String, Two Pointers, Dynamic Programming
|
||||||
|
- [704. Binary Search](src/exercises/704.binary-search.py) - Array, Binary Search
|
||||||
|
- [1200. Minimum Absolute Difference](src/exercises/1200.minimum-absolute-difference.py) - Array, Sorting
|
||||||
|
- [2053. Kth Distinct String in an Array](src/exercises/2053.kth-distinct-string-in-an-array.py) - Array, Hash Table, String
|
||||||
|
- [2441. Largest Positive Integer That Exists With Its Negative](src/exercises/2441.largest-positive-integer-that-exists-with-its-negative.py) - Array, Hash Table, Two Pointers
|
||||||
|
|
||||||
|
#### Medium
|
||||||
|
- [2. Add Two Numbers](src/exercises/2.add-two-numbers.py) - Linked List, Math, Recursion
|
||||||
|
- [3. Longest Substring Without Repeating Characters](src/exercises/3.longest-substring-without-repeating-characters.py) - Hash Table, String, Sliding Window
|
||||||
|
- [3. Longest Substring Without Repeating Characters (TypeScript)](src/exercises/3.longest-substring-without-repeating-characters.ts) - Hash Table, String, Sliding Window
|
||||||
|
- [4. Median of Two Sorted Arrays](src/exercises/4.median-of-two-sorted-arrays.py) - Array, Binary Search, Divide and Conquer
|
||||||
|
- [5. Longest Palindromic Substring](src/exercises/5.longest-palindromic-substring.py) - String, Dynamic Programming
|
||||||
|
- [6. Zigzag Conversion](src/exercises/6.zigzag-conversion.py) - String
|
||||||
|
- [7. Reverse Integer](src/exercises/7.reverse-integer.py) - Math
|
||||||
|
- [10. Regular Expression Matching](src/exercises/10.regular-expression-matching.py) - String, Dynamic Programming, Recursion
|
||||||
|
- [11. Container With Most Water](src/exercises/11.container-with-most-water.py) - Array, Two Pointers, Greedy
|
||||||
|
- [15. 3Sum](src/exercises/15.3sum.py) - Array, Two Pointers, Sorting
|
||||||
|
- [22. Generate Parentheses](src/exercises/22.generate-parentheses.py) - String, Dynamic Programming, Backtracking
|
||||||
|
- [36. Valid Sudoku](src/exercises/36.valid-sudoku.py) - Array, Hash Table, Matrix
|
||||||
|
- [42. Trapping Rain Water](src/exercises/42.trapping-rain-water.py) - Array, Two Pointers, Dynamic Programming, Stack
|
||||||
|
- [45. Jump Game II](src/exercises/45.jump-game-ii.py) - Array, Dynamic Programming, Greedy
|
||||||
|
- [50. Pow(x, n)](src/exercises/50.powx-n.py) - Math, Recursion
|
||||||
|
- [55. Jump Game](src/exercises/55.jump-game.py) - Array, Dynamic Programming, Greedy
|
||||||
|
- [74. Search a 2D Matrix](src/exercises/74.search-a-2d-matrix.py) - Array, Binary Search, Matrix
|
||||||
|
- [80. Remove Duplicates from Sorted Array II](src/exercises/80.remove-duplicates-from-sorted-array-ii.py) - Array, Two Pointers
|
||||||
|
- [89. Gray Code](src/exercises/89.gray-code.py) - Math, Backtracking, Bit Manipulation
|
||||||
|
- [90. Subsets II](src/exercises/90.subsets-ii.py) - Array, Backtracking, Bit Manipulation
|
||||||
|
- [91. Decode Ways](src/exercises/91.decode-ways.py) - String, Dynamic Programming
|
||||||
|
- [92. Reverse Linked List II](src/exercises/92.reverse-linked-list-ii.py) - Linked List
|
||||||
|
- [121. Best Time to Buy and Sell Stock](src/exercises/121.best-time-to-buy-and-sell-stock.py) - Array, Dynamic Programming
|
||||||
|
- [122. Best Time to Buy and Sell Stock II](src/exercises/122.best-time-to-buy-and-sell-stock-ii.py) - Array, Dynamic Programming, Greedy
|
||||||
|
- [128. Longest Consecutive Sequence](src/exercises/128.longest-consecutive-sequence.py) - Array, Hash Table, Union Find
|
||||||
|
- [135. Candy](src/exercises/135.candy.py) - Array, Greedy
|
||||||
|
- [141. Linked List Cycle](src/exercises/141.linked-list-cycle.py) - Hash Table, Linked List, Two Pointers
|
||||||
|
- [149. Max Points on a Line](src/exercises/149.max-points-on-a-line.py) - Array, Hash Table, Math, Geometry
|
||||||
|
- [150. Evaluate Reverse Polish Notation](src/exercises/150.evaluate-reverse-polish-notation.py) - Array, Math, Stack
|
||||||
|
- [153. Find Minimum in Rotated Sorted Array](src/exercises/153.find-minimum-in-rotated-sorted-array.py) - Array, Binary Search
|
||||||
|
- [155. Min Stack](src/exercises/155.min-stack.py) - Stack, Design
|
||||||
|
- [167. Two Sum II - Input Array Is Sorted](src/exercises/167.two-sum-ii-input-array-is-sorted.py) - Array, Two Pointers, Binary Search
|
||||||
|
- [172. Factorial Trailing Zeroes](src/exercises/172.factorial-trailing-zeroes.py) - Math
|
||||||
|
- [189. Rotate Array](src/exercises/189.rotate-array.py) - Array, Math, Two Pointers
|
||||||
|
- [206. Reverse Linked List](src/exercises/206.reverse-linked-list.py) - Linked List, Recursion
|
||||||
|
- [228. Summary Ranges](src/exercises/228.summary-ranges.py) - Array
|
||||||
|
- [238. Product of Array Except Self](src/exercises/238.product-of-array-except-self.py) - Array, Prefix Sum
|
||||||
|
- [246. Strobogrammatic Number](src/exercises/246.strobogrammatic-number.py) - Hash Table, Two Pointers, String
|
||||||
|
- [248. Strobogrammatic Number III](src/exercises/248.strobogrammatic-number-iii.py) - Math, Recursion
|
||||||
|
- [243. Shortest Word Distance](src/exercises/243.shortest-word-distance.py) - Array, String
|
||||||
|
- [244. Shortest Word Distance II](src/exercises/244.shortest-word-distance-ii.py) - Array, Hash Table, Design, Two Pointers
|
||||||
|
- [245. Shortest Word Distance III](src/exercises/245.shortest-word-distance-iii.py) - Array, String
|
||||||
|
- [875. Koko Eating Bananas](src/exercises/875.koko-eating-bananas.py) - Array, Binary Search
|
||||||
|
- [990. Satisfiability of Equality Equations](src/exercises/990.satisfiability-of-equality-equations.py) - Array, Union Find, Graph
|
||||||
|
- [1298. Maximum Candies You Can Get from Boxes](src/exercises/1298.maximum-candies-you-can-get-from-boxes.py) - Breadth-First Search, Array
|
||||||
|
- [1820. Maximum Number of Accepted Invitations](src/exercises/1820.maximum-number-of-accepted-invitations.py) - Graph, Backtracking
|
||||||
|
- [2040. Kth Smallest Product of Two Sorted Arrays](src/exercises/2040.kth-smallest-product-of-two-sorted-arrays.py) - Array, Binary Search
|
||||||
|
- [2116. Check if a Parentheses String Can Be Valid](src/exercises/2116.check-if-a-parentheses-string-can-be-valid.py) - String, Stack, Greedy
|
||||||
|
- [2294. Partition Array Such That Maximum Difference Is K](src/exercises/2294.partition-array-such-that-maximum-difference-is-k.py) - Array, Greedy, Sorting
|
||||||
|
- [2616. Minimize the Maximum Difference of Pairs](src/exercises/2616.minimize-the-maximum-difference-of-pairs.py) - Array, Binary Search, Greedy
|
||||||
|
- [2966. Divide Array Into Arrays With Max Difference](src/exercises/2966.divide-array-into-arrays-with-max-difference.py) - Array, Greedy, Sorting
|
||||||
|
- [3442. Maximum Difference Between Even and Odd Frequency I](src/exercises/3442.maximum-difference-between-even-and-odd-frequency-i.py) - Array, Hash Table
|
||||||
|
- [3445. Maximum Difference Between Even and Odd Frequency II](src/exercises/3445.maximum-difference-between-even-and-odd-frequency-ii.py) - Array, Hash Table
|
||||||
|
|
||||||
|
### 🔗 Linked Lists
|
||||||
|
- [2. Add Two Numbers](src/exercises/2.add-two-numbers.py) - Linked List, Math, Recursion
|
||||||
|
- [21. Merge Two Sorted Lists](src/exercises/21.merge-two-sorted-lists.py) - Linked List, Recursion
|
||||||
|
- [92. Reverse Linked List II](src/exercises/92.reverse-linked-list-ii.py) - Linked List
|
||||||
|
- [141. Linked List Cycle](src/exercises/141.linked-list-cycle.py) - Hash Table, Linked List, Two Pointers
|
||||||
|
- [206. Reverse Linked List](src/exercises/206.reverse-linked-list.py) - Linked List, Recursion
|
||||||
|
|
||||||
|
### 🌳 Trees & Graphs
|
||||||
|
- [1298. Maximum Candies You Can Get from Boxes](src/exercises/1298.maximum-candies-you-can-get-from-boxes.py) - Breadth-First Search, Array
|
||||||
|
- [1820. Maximum Number of Accepted Invitations](src/exercises/1820.maximum-number-of-accepted-invitations.py) - Graph, Backtracking
|
||||||
|
- [990. Satisfiability of Equality Equations](src/exercises/990.satisfiability-of-equality-equations.py) - Array, Union Find, Graph
|
||||||
|
|
||||||
|
### 🔍 Binary Search
|
||||||
|
- [4. Median of Two Sorted Arrays](src/exercises/4.median-of-two-sorted-arrays.py) - Array, Binary Search, Divide and Conquer
|
||||||
|
- [35. Search Insert Position](src/exercises/35.search-insert-position.py) - Array, Binary Search
|
||||||
|
- [50. Pow(x, n)](src/exercises/50.powx-n.py) - Math, Recursion
|
||||||
|
- [69. Sqrt(x)](src/exercises/69.sqrtx.py) - Math, Binary Search
|
||||||
|
- [74. Search a 2D Matrix](src/exercises/74.search-a-2d-matrix.py) - Array, Binary Search, Matrix
|
||||||
|
- [153. Find Minimum in Rotated Sorted Array](src/exercises/153.find-minimum-in-rotated-sorted-array.py) - Array, Binary Search
|
||||||
|
- [167. Two Sum II - Input Array Is Sorted](src/exercises/167.two-sum-ii-input-array-is-sorted.py) - Array, Two Pointers, Binary Search
|
||||||
|
- [704. Binary Search](src/exercises/704.binary-search.py) - Array, Binary Search
|
||||||
|
- [875. Koko Eating Bananas](src/exercises/875.koko-eating-bananas.py) - Array, Binary Search
|
||||||
|
- [2040. Kth Smallest Product of Two Sorted Arrays](src/exercises/2040.kth-smallest-product-of-two-sorted-arrays.py) - Array, Binary Search
|
||||||
|
- [2616. Minimize the Maximum Difference of Pairs](src/exercises/2616.minimize-the-maximum-difference-of-pairs.py) - Array, Binary Search, Greedy
|
||||||
|
|
||||||
|
### 🧮 Dynamic Programming
|
||||||
|
- [5. Longest Palindromic Substring](src/exercises/5.longest-palindromic-substring.py) - String, Dynamic Programming
|
||||||
|
- [10. Regular Expression Matching](src/exercises/10.regular-expression-matching.py) - String, Dynamic Programming, Recursion
|
||||||
|
- [22. Generate Parentheses](src/exercises/22.generate-parentheses.py) - String, Dynamic Programming, Backtracking
|
||||||
|
- [42. Trapping Rain Water](src/exercises/42.trapping-rain-water.py) - Array, Two Pointers, Dynamic Programming, Stack
|
||||||
|
- [45. Jump Game II](src/exercises/45.jump-game-ii.py) - Array, Dynamic Programming, Greedy
|
||||||
|
- [55. Jump Game](src/exercises/55.jump-game.py) - Array, Dynamic Programming, Greedy
|
||||||
|
- [91. Decode Ways](src/exercises/91.decode-ways.py) - String, Dynamic Programming
|
||||||
|
- [121. Best Time to Buy and Sell Stock](src/exercises/121.best-time-to-buy-and-sell-stock.py) - Array, Dynamic Programming
|
||||||
|
- [122. Best Time to Buy and Sell Stock II](src/exercises/122.best-time-to-buy-and-sell-stock-ii.py) - Array, Dynamic Programming, Greedy
|
||||||
|
- [128. Longest Consecutive Sequence](src/exercises/128.longest-consecutive-sequence.py) - Array, Hash Table, Union Find
|
||||||
|
- [135. Candy](src/exercises/135.candy.py) - Array, Greedy
|
||||||
|
- [392. Is Subsequence](src/exercises/392.is-subsequence.py) - String, Two Pointers, Dynamic Programming
|
||||||
|
|
||||||
|
### 🎯 Two Pointers
|
||||||
|
- [3. Longest Substring Without Repeating Characters](src/exercises/3.longest-substring-without-repeating-characters.py) - Hash Table, String, Sliding Window
|
||||||
|
- [11. Container With Most Water](src/exercises/11.container-with-most-water.py) - Array, Two Pointers, Greedy
|
||||||
|
- [15. 3Sum](src/exercises/15.3sum.py) - Array, Two Pointers, Sorting
|
||||||
|
- [26. Remove Duplicates from Sorted Array](src/exercises/80.remove-duplicates-from-sorted-array-ii.py) - Array, Two Pointers
|
||||||
|
- [27. Remove Element](src/exercises/80.remove-duplicates-from-sorted-array-ii.py) - Array, Two Pointers
|
||||||
|
- [28. Find the Index of the First Occurrence in a String](src/exercises/28.find-the-index-of-the-first-occurrence-in-a-string.py) - String, Two Pointers
|
||||||
|
- [42. Trapping Rain Water](src/exercises/42.trapping-rain-water.py) - Array, Two Pointers, Dynamic Programming, Stack
|
||||||
|
- [80. Remove Duplicates from Sorted Array II](src/exercises/80.remove-duplicates-from-sorted-array-ii.py) - Array, Two Pointers
|
||||||
|
- [88. Merge Sorted Array](src/exercises/88.merge-sorted-array.py) - Array, Two Pointers
|
||||||
|
- [125. Valid Palindrome](src/exercises/125.valid-palindrome.py) - String, Two Pointers
|
||||||
|
- [141. Linked List Cycle](src/exercises/141.linked-list-cycle.py) - Hash Table, Linked List, Two Pointers
|
||||||
|
- [167. Two Sum II - Input Array Is Sorted](src/exercises/167.two-sum-ii-input-array-is-sorted.py) - Array, Two Pointers, Binary Search
|
||||||
|
- [189. Rotate Array](src/exercises/189.rotate-array.py) - Array, Math, Two Pointers
|
||||||
|
- [202. Happy Number](src/exercises/202.happy-number.py) - Hash Table, Math, Two Pointers
|
||||||
|
- [219. Contains Duplicate II](src/exercises/219.contains-duplicate-ii.py) - Array, Hash Table, Sliding Window
|
||||||
|
- [243. Shortest Word Distance](src/exercises/243.shortest-word-distance.py) - Array, String
|
||||||
|
- [244. Shortest Word Distance II](src/exercises/244.shortest-word-distance-ii.py) - Array, Hash Table, Design, Two Pointers
|
||||||
|
- [245. Shortest Word Distance III](src/exercises/245.shortest-word-distance-iii.py) - Array, String
|
||||||
|
- [246. Strobogrammatic Number](src/exercises/246.strobogrammatic-number.py) - Hash Table, Two Pointers, String
|
||||||
|
- [392. Is Subsequence](src/exercises/392.is-subsequence.py) - String, Two Pointers, Dynamic Programming
|
||||||
|
- [2441. Largest Positive Integer That Exists With Its Negative](src/exercises/2441.largest-positive-integer-that-exists-with-its-negative.py) - Array, Hash Table, Two Pointers
|
||||||
|
|
||||||
|
### 🗃️ Hash Tables
|
||||||
|
- [1. Two Sum](src/exercises/1.two-sum.py) - Hash Table, Array
|
||||||
|
- [13. Roman to Integer](src/exercises/13.roman-to-integer.py) - Hash Table, Math, String
|
||||||
|
- [20. Valid Parentheses](src/exercises/20.valid-parentheses.py) - Stack, String
|
||||||
|
- [36. Valid Sudoku](src/exercises/36.valid-sudoku.py) - Array, Hash Table, Matrix
|
||||||
|
- [141. Linked List Cycle](src/exercises/141.linked-list-cycle.py) - Hash Table, Linked List, Two Pointers
|
||||||
|
- [149. Max Points on a Line](src/exercises/149.max-points-on-a-line.py) - Array, Hash Table, Math, Geometry
|
||||||
|
- [169. Majority Element](src/exercises/169.majority-element.py) - Array, Hash Table, Divide and Conquer
|
||||||
|
- [202. Happy Number](src/exercises/202.happy-number.py) - Hash Table, Math, Two Pointers
|
||||||
|
- [205. Isomorphic Strings](src/exercises/205.isomorphic-strings.py) - Hash Table, String
|
||||||
|
- [217. Contains Duplicate](src/exercises/217.contains-duplicate.py) - Array, Hash Table, Sorting
|
||||||
|
- [219. Contains Duplicate II](src/exercises/219.contains-duplicate-ii.py) - Array, Hash Table, Sliding Window
|
||||||
|
- [242. Valid Anagram](src/exercises/242.valid-anagram.py) - String, Sorting, Hash Table
|
||||||
|
- [243. Shortest Word Distance](src/exercises/243.shortest-word-distance.py) - Array, String
|
||||||
|
- [244. Shortest Word Distance II](src/exercises/244.shortest-word-distance-ii.py) - Array, Hash Table, Design, Two Pointers
|
||||||
|
- [246. Strobogrammatic Number](src/exercises/246.strobogrammatic-number.py) - Hash Table, Two Pointers, String
|
||||||
|
- [290. Word Pattern](src/exercises/290.word-pattern.py) - Hash Table, String
|
||||||
|
- [383. Ransom Note](src/exercises/383.ransom-note.py) - String, Hash Table, Counting
|
||||||
|
- [128. Longest Consecutive Sequence](src/exercises/128.longest-consecutive-sequence.py) - Array, Hash Table, Union Find
|
||||||
|
- [2053. Kth Distinct String in an Array](src/exercises/2053.kth-distinct-string-in-an-array.py) - Array, Hash Table, String
|
||||||
|
- [2441. Largest Positive Integer That Exists With Its Negative](src/exercises/2441.largest-positive-integer-that-exists-with-its-negative.py) - Array, Hash Table, Two Pointers
|
||||||
|
- [3442. Maximum Difference Between Even and Odd Frequency I](src/exercises/3442.maximum-difference-between-even-and-odd-frequency-i.py) - Array, Hash Table
|
||||||
|
- [3445. Maximum Difference Between Even and Odd Frequency II](src/exercises/3445.maximum-difference-between-even-and-odd-frequency-ii.py) - Array, Hash Table
|
||||||
|
|
||||||
|
### 📚 Stacks & Queues
|
||||||
|
- [20. Valid Parentheses](src/exercises/20.valid-parentheses.py) - Stack, String
|
||||||
|
- [42. Trapping Rain Water](src/exercises/42.trapping-rain-water.py) - Array, Two Pointers, Dynamic Programming, Stack
|
||||||
|
- [150. Evaluate Reverse Polish Notation](src/exercises/150.evaluate-reverse-polish-notation.py) - Array, Math, Stack
|
||||||
|
- [155. Min Stack](src/exercises/155.min-stack.py) - Stack, Design
|
||||||
|
- [2116. Check if a Parentheses String Can Be Valid](src/exercises/2116.check-if-a-parentheses-string-can-be-valid.py) - String, Stack, Greedy
|
||||||
|
|
||||||
|
### 🔄 Backtracking
|
||||||
|
- [22. Generate Parentheses](src/exercises/22.generate-parentheses.py) - String, Dynamic Programming, Backtracking
|
||||||
|
- [89. Gray Code](src/exercises/89.gray-code.py) - Math, Backtracking, Bit Manipulation
|
||||||
|
- [90. Subsets II](src/exercises/90.subsets-ii.py) - Array, Backtracking, Bit Manipulation
|
||||||
|
- [1820. Maximum Number of Accepted Invitations](src/exercises/1820.maximum-number-of-accepted-invitations.py) - Graph, Backtracking
|
||||||
|
|
||||||
|
### 🎲 Greedy
|
||||||
|
- [11. Container With Most Water](src/exercises/11.container-with-most-water.py) - Array, Two Pointers, Greedy
|
||||||
|
- [45. Jump Game II](src/exercises/45.jump-game-ii.py) - Array, Dynamic Programming, Greedy
|
||||||
|
- [55. Jump Game](src/exercises/55.jump-game.py) - Array, Dynamic Programming, Greedy
|
||||||
|
- [122. Best Time to Buy and Sell Stock II](src/exercises/122.best-time-to-buy-and-sell-stock-ii.py) - Array, Dynamic Programming, Greedy
|
||||||
|
- [135. Candy](src/exercises/135.candy.py) - Array, Greedy
|
||||||
|
- [2116. Check if a Parentheses String Can Be Valid](src/exercises/2116.check-if-a-parentheses-string-can-be-valid.py) - String, Stack, Greedy
|
||||||
|
- [2294. Partition Array Such That Maximum Difference Is K](src/exercises/2294.partition-array-such-that-maximum-difference-is-k.py) - Array, Greedy, Sorting
|
||||||
|
- [2616. Minimize the Maximum Difference of Pairs](src/exercises/2616.minimize-the-maximum-difference-of-pairs.py) - Array, Binary Search, Greedy
|
||||||
|
- [2966. Divide Array Into Arrays With Max Difference](src/exercises/2966.divide-array-into-arrays-with-max-difference.py) - Array, Greedy, Sorting
|
||||||
|
|
||||||
|
### 🔢 Math
|
||||||
|
- [7. Reverse Integer](src/exercises/7.reverse-integer.py) - Math
|
||||||
|
- [9. Palindrome Number](src/exercises/9.palindrome-number.py) - Math
|
||||||
|
- [13. Roman to Integer](src/exercises/13.roman-to-integer.py) - Hash Table, Math, String
|
||||||
|
- [50. Pow(x, n)](src/exercises/50.powx-n.py) - Math, Recursion
|
||||||
|
- [66. Plus One](src/exercises/66.plus-one.py) - Array, Math
|
||||||
|
- [69. Sqrt(x)](src/exercises/69.sqrtx.py) - Math, Binary Search
|
||||||
|
- [89. Gray Code](src/exercises/89.gray-code.py) - Math, Backtracking, Bit Manipulation
|
||||||
|
- [149. Max Points on a Line](src/exercises/149.max-points-on-a-line.py) - Array, Hash Table, Math, Geometry
|
||||||
|
- [172. Factorial Trailing Zeroes](src/exercises/172.factorial-trailing-zeroes.py) - Math
|
||||||
|
- [189. Rotate Array](src/exercises/189.rotate-array.py) - Array, Math, Two Pointers
|
||||||
|
- [202. Happy Number](src/exercises/202.happy-number.py) - Hash Table, Math, Two Pointers
|
||||||
|
- [150. Evaluate Reverse Polish Notation](src/exercises/150.evaluate-reverse-polish-notation.py) - Array, Math, Stack
|
||||||
|
|
||||||
|
### 🧮 Bit Manipulation
|
||||||
|
- [89. Gray Code](src/exercises/89.gray-code.py) - Math, Backtracking, Bit Manipulation
|
||||||
|
- [90. Subsets II](src/exercises/90.subsets-ii.py) - Array, Backtracking, Bit Manipulation
|
||||||
|
|
||||||
|
### 🏗️ Design
|
||||||
|
- [155. Min Stack](src/exercises/155.min-stack.py) - Stack, Design
|
||||||
|
- [244. Shortest Word Distance II](src/exercises/244.shortest-word-distance-ii.py) - Array, Hash Table, Design, Two Pointers
|
||||||
|
|
||||||
|
### 🔗 Union Find
|
||||||
|
- [128. Longest Consecutive Sequence](src/exercises/128.longest-consecutive-sequence.py) - Array, Hash Table, Union Find
|
||||||
|
- [990. Satisfiability of Equality Equations](src/exercises/990.satisfiability-of-equality-equations.py) - Array, Union Find, Graph
|
||||||
|
|
||||||
|
### 📊 Sorting
|
||||||
|
- [15. 3Sum](src/exercises/15.3sum.py) - Array, Two Pointers, Sorting
|
||||||
|
- [80. Remove Duplicates from Sorted Array II](src/exercises/80.remove-duplicates-from-sorted-array-ii.py) - Array, Two Pointers
|
||||||
|
- [88. Merge Sorted Array](src/exercises/88.merge-sorted-array.py) - Array, Two Pointers
|
||||||
|
- [1200. Minimum Absolute Difference](src/exercises/1200.minimum-absolute-difference.py) - Array, Sorting
|
||||||
|
- [217. Contains Duplicate](src/exercises/217.contains-duplicate.py) - Array, Hash Table, Sorting
|
||||||
|
- [242. Valid Anagram](src/exercises/242.valid-anagram.py) - String, Sorting, Hash Table
|
||||||
|
- [2294. Partition Array Such That Maximum Difference Is K](src/exercises/2294.partition-array-such-that-maximum-difference-is-k.py) - Array, Greedy, Sorting
|
||||||
|
- [2966. Divide Array Into Arrays With Max Difference](src/exercises/2966.divide-array-into-arrays-with-max-difference.py) - Array, Greedy, Sorting
|
||||||
|
|
||||||
|
### 🔄 Recursion
|
||||||
|
- [2. Add Two Numbers](src/exercises/2.add-two-numbers.py) - Linked List, Math, Recursion
|
||||||
|
- [10. Regular Expression Matching](src/exercises/10.regular-expression-matching.py) - String, Dynamic Programming, Recursion
|
||||||
|
- [21. Merge Two Sorted Lists](src/exercises/21.merge-two-sorted-lists.py) - Linked List, Recursion
|
||||||
|
- [22. Generate Parentheses](src/exercises/22.generate-parentheses.py) - String, Dynamic Programming, Backtracking
|
||||||
|
- [50. Pow(x, n)](src/exercises/50.powx-n.py) - Math, Recursion
|
||||||
|
- [206. Reverse Linked List](src/exercises/206.reverse-linked-list.py) - Linked List, Recursion
|
||||||
|
- [248. Strobogrammatic Number III](src/exercises/248.strobogrammatic-number-iii.py) - Math, Recursion
|
||||||
|
|
||||||
|
### 🎯 Sliding Window
|
||||||
|
- [3. Longest Substring Without Repeating Characters](src/exercises/3.longest-substring-without-repeating-characters.py) - Hash Table, String, Sliding Window
|
||||||
|
- [219. Contains Duplicate II](src/exercises/219.contains-duplicate-ii.py) - Array, Hash Table, Sliding Window
|
||||||
|
|
||||||
|
### 📐 Geometry
|
||||||
|
- [149. Max Points on a Line](src/exercises/149.max-points-on-a-line.py) - Array, Hash Table, Math, Geometry
|
||||||
|
|
||||||
|
### 🔍 Divide and Conquer
|
||||||
|
- [4. Median of Two Sorted Arrays](src/exercises/4.median-of-two-sorted-arrays.py) - Array, Binary Search, Divide and Conquer
|
||||||
|
- [169. Majority Element](src/exercises/169.majority-element.py) - Array, Hash Table, Divide and Conquer
|
||||||
|
|
||||||
|
### 📝 String Manipulation
|
||||||
|
- [3. Longest Substring Without Repeating Characters](src/exercises/3.longest-substring-without-repeating-characters.py) - Hash Table, String, Sliding Window
|
||||||
|
- [5. Longest Palindromic Substring](src/exercises/5.longest-palindromic-substring.py) - String, Dynamic Programming
|
||||||
|
- [6. Zigzag Conversion](src/exercises/6.zigzag-conversion.py) - String
|
||||||
|
- [10. Regular Expression Matching](src/exercises/10.regular-expression-matching.py) - String, Dynamic Programming, Recursion
|
||||||
|
- [13. Roman to Integer](src/exercises/13.roman-to-integer.py) - Hash Table, Math, String
|
||||||
|
- [14. Longest Common Prefix](src/exercises/14.longest-common-prefix.py) - String, Trie
|
||||||
|
- [20. Valid Parentheses](src/exercises/20.valid-parentheses.py) - Stack, String
|
||||||
|
- [22. Generate Parentheses](src/exercises/22.generate-parentheses.py) - String, Dynamic Programming, Backtracking
|
||||||
|
- [28. Find the Index of the First Occurrence in a String](src/exercises/28.find-the-index-of-the-first-occurrence-in-a-string.py) - String, Two Pointers
|
||||||
|
- [125. Valid Palindrome](src/exercises/125.valid-palindrome.py) - String, Two Pointers
|
||||||
|
- [205. Isomorphic Strings](src/exercises/205.isomorphic-strings.py) - Hash Table, String
|
||||||
|
- [242. Valid Anagram](src/exercises/242.valid-anagram.py) - String, Sorting, Hash Table
|
||||||
|
- [246. Strobogrammatic Number](src/exercises/246.strobogrammatic-number.py) - Hash Table, Two Pointers, String
|
||||||
|
- [248. Strobogrammatic Number III](src/exercises/248.strobogrammatic-number-iii.py) - Math, Recursion
|
||||||
|
- [290. Word Pattern](src/exercises/290.word-pattern.py) - Hash Table, String
|
||||||
|
- [383. Ransom Note](src/exercises/383.ransom-note.py) - String, Hash Table, Counting
|
||||||
|
- [392. Is Subsequence](src/exercises/392.is-subsequence.py) - String, Two Pointers, Dynamic Programming
|
||||||
|
- [91. Decode Ways](src/exercises/91.decode-ways.py) - String, Dynamic Programming
|
||||||
|
- [2116. Check if a Parentheses String Can Be Valid](src/exercises/2116.check-if-a-parentheses-string-can-be-valid.py) - String, Stack, Greedy
|
||||||
|
|
||||||
|
### 📊 Matrix
|
||||||
|
- [36. Valid Sudoku](src/exercises/36.valid-sudoku.py) - Array, Hash Table, Matrix
|
||||||
|
- [74. Search a 2D Matrix](src/exercises/74.search-a-2d-matrix.py) - Array, Binary Search, Matrix
|
||||||
|
|
||||||
|
### 🔢 Counting
|
||||||
|
- [383. Ransom Note](src/exercises/383.ransom-note.py) - String, Hash Table, Counting
|
||||||
|
|
||||||
|
### 🌐 Breadth-First Search
|
||||||
|
- [1298. Maximum Candies You Can Get from Boxes](src/exercises/1298.maximum-candies-you-can-get-from-boxes.py) - Breadth-First Search, Array
|
||||||
|
|
||||||
|
## 📈 Progress Tracking
|
||||||
|
|
||||||
|
### Difficulty Distribution
|
||||||
|
- **Easy**: ~40 problems
|
||||||
|
- **Medium**: ~45 problems
|
||||||
|
- **Hard**: ~5 problems
|
||||||
|
|
||||||
|
### Top Problem Types
|
||||||
|
1. **Arrays & Strings** - Most common
|
||||||
|
2. **Hash Tables** - Frequently used technique
|
||||||
|
3. **Two Pointers** - Efficient array manipulation
|
||||||
|
4. **Binary Search** - Optimization problems
|
||||||
|
5. **Dynamic Programming** - Complex problem solving
|
||||||
|
|
||||||
|
## 🛠️ Solution Structure
|
||||||
|
|
||||||
|
Each solution follows a consistent structure:
|
||||||
|
- Standard library imports
|
||||||
|
- Type hints for better code clarity
|
||||||
|
- Solution class with required method
|
||||||
|
- Clean, readable implementation
|
||||||
|
- Proper comments and documentation
|
||||||
|
|
||||||
|
## 🎯 Learning Focus Areas
|
||||||
|
|
||||||
|
Based on the solutions, key areas of expertise include:
|
||||||
|
- **Array manipulation** and **two-pointer techniques**
|
||||||
|
- **Hash table** optimization for lookups
|
||||||
|
- **Binary search** for efficient searching
|
||||||
|
- **Dynamic programming** for complex problems
|
||||||
|
- **String manipulation** and **palindrome problems**
|
||||||
|
- **Linked list** operations and **cycle detection**
|
||||||
|
|
||||||
|
## 📚 Resources
|
||||||
|
|
||||||
|
- [LeetCode Problem Set](https://leetcode.com/problemset/)
|
||||||
|
- [LeetCode Profile](https://leetcode.com/u/cargdev/)
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
*This index is automatically generated and organized by problem categories and difficulty levels. Each solution includes the relevant tags and techniques used.*
|
||||||
56
src/exercises/1.two-sum.py
Normal file
56
src/exercises/1.two-sum.py
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
from string import *
|
||||||
|
from re import *
|
||||||
|
from datetime import *
|
||||||
|
from collections import *
|
||||||
|
from heapq import *
|
||||||
|
from bisect import *
|
||||||
|
from copy import *
|
||||||
|
from math import *
|
||||||
|
from random import *
|
||||||
|
from statistics import *
|
||||||
|
from itertools import *
|
||||||
|
from functools import *
|
||||||
|
from operator import *
|
||||||
|
from io import *
|
||||||
|
from sys import *
|
||||||
|
from json import *
|
||||||
|
from builtins import *
|
||||||
|
import string
|
||||||
|
import re
|
||||||
|
import datetime
|
||||||
|
import collections
|
||||||
|
import heapq
|
||||||
|
import bisect
|
||||||
|
import copy
|
||||||
|
import math
|
||||||
|
import random
|
||||||
|
import statistics
|
||||||
|
import itertools
|
||||||
|
import functools
|
||||||
|
import operator
|
||||||
|
import io
|
||||||
|
import sys
|
||||||
|
import json
|
||||||
|
from typing import *
|
||||||
|
|
||||||
|
# @leet start
|
||||||
|
class Solution:
|
||||||
|
def twoSum(self, nums: List[int], target: int) -> List[int]:
|
||||||
|
num_len = len(nums)
|
||||||
|
if num_len == 0:
|
||||||
|
return []
|
||||||
|
if num_len == 1:
|
||||||
|
return [0]
|
||||||
|
if num_len == 2:
|
||||||
|
return [0, 1]
|
||||||
|
arr = []
|
||||||
|
hash_map = {}
|
||||||
|
for i in range(num_len):
|
||||||
|
complement = target - nums[i]
|
||||||
|
if complement in hash_map:
|
||||||
|
return [i, hash_map[complement]]
|
||||||
|
hash_map[nums[i]] = i
|
||||||
|
|
||||||
|
return arr
|
||||||
|
|
||||||
|
# @leet end
|
||||||
40
src/exercises/10.regular-expression-matching.py
Normal file
40
src/exercises/10.regular-expression-matching.py
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
from string import *
|
||||||
|
from re import *
|
||||||
|
from datetime import *
|
||||||
|
from collections import *
|
||||||
|
from heapq import *
|
||||||
|
from bisect import *
|
||||||
|
from copy import *
|
||||||
|
from math import *
|
||||||
|
from random import *
|
||||||
|
from statistics import *
|
||||||
|
from itertools import *
|
||||||
|
from functools import *
|
||||||
|
from operator import *
|
||||||
|
from io import *
|
||||||
|
from sys import *
|
||||||
|
from json import *
|
||||||
|
from builtins import *
|
||||||
|
import string
|
||||||
|
import re
|
||||||
|
import datetime
|
||||||
|
import collections
|
||||||
|
import heapq
|
||||||
|
import bisect
|
||||||
|
import copy
|
||||||
|
import math
|
||||||
|
import random
|
||||||
|
import statistics
|
||||||
|
import itertools
|
||||||
|
import functools
|
||||||
|
import operator
|
||||||
|
import io
|
||||||
|
import sys
|
||||||
|
import json
|
||||||
|
from typing import *
|
||||||
|
|
||||||
|
# @leet start
|
||||||
|
class Solution:
|
||||||
|
def isMatch(self, s: str, p: str) -> bool:
|
||||||
|
|
||||||
|
# @leet end
|
||||||
53
src/exercises/11.container-with-most-water.py
Normal file
53
src/exercises/11.container-with-most-water.py
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
from string import *
|
||||||
|
from re import *
|
||||||
|
from datetime import *
|
||||||
|
from collections import *
|
||||||
|
from heapq import *
|
||||||
|
from bisect import *
|
||||||
|
from copy import *
|
||||||
|
from math import *
|
||||||
|
from random import *
|
||||||
|
from statistics import *
|
||||||
|
from itertools import *
|
||||||
|
from functools import *
|
||||||
|
from operator import *
|
||||||
|
from io import *
|
||||||
|
from sys import *
|
||||||
|
from json import *
|
||||||
|
from builtins import *
|
||||||
|
import string
|
||||||
|
import re
|
||||||
|
import datetime
|
||||||
|
import collections
|
||||||
|
import heapq
|
||||||
|
import bisect
|
||||||
|
import copy
|
||||||
|
import math
|
||||||
|
import random
|
||||||
|
import statistics
|
||||||
|
import itertools
|
||||||
|
import functools
|
||||||
|
import operator
|
||||||
|
import io
|
||||||
|
import sys
|
||||||
|
import json
|
||||||
|
from typing import *
|
||||||
|
|
||||||
|
|
||||||
|
# @leet start
|
||||||
|
class Solution:
|
||||||
|
def maxArea(self, height: List[int]) -> int:
|
||||||
|
i, j, max_vol = 0, len(height) - 1, 0
|
||||||
|
while i < j:
|
||||||
|
if height[i] > height[j]:
|
||||||
|
max_vol = max(max_vol, min(height[i], height[j]) * (j - i))
|
||||||
|
j -= 1
|
||||||
|
elif height[j] >= height[i]:
|
||||||
|
max_vol = max(max_vol, min(height[i], height[j]) * (j - i))
|
||||||
|
i += 1
|
||||||
|
|
||||||
|
return max_vol
|
||||||
|
|
||||||
|
|
||||||
|
# @leet end
|
||||||
|
|
||||||
55
src/exercises/1200.minimum-absolute-difference.py
Normal file
55
src/exercises/1200.minimum-absolute-difference.py
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
from string import *
|
||||||
|
from re import *
|
||||||
|
from datetime import *
|
||||||
|
from collections import *
|
||||||
|
from heapq import *
|
||||||
|
from bisect import *
|
||||||
|
from copy import *
|
||||||
|
from math import *
|
||||||
|
from random import *
|
||||||
|
from statistics import *
|
||||||
|
from itertools import *
|
||||||
|
from functools import *
|
||||||
|
from operator import *
|
||||||
|
from io import *
|
||||||
|
from sys import *
|
||||||
|
from json import *
|
||||||
|
from builtins import *
|
||||||
|
import string
|
||||||
|
import re
|
||||||
|
import datetime
|
||||||
|
import collections
|
||||||
|
import heapq
|
||||||
|
import bisect
|
||||||
|
import copy
|
||||||
|
import math
|
||||||
|
import random
|
||||||
|
import statistics
|
||||||
|
import itertools
|
||||||
|
import functools
|
||||||
|
import operator
|
||||||
|
import io
|
||||||
|
import sys
|
||||||
|
import json
|
||||||
|
from typing import *
|
||||||
|
|
||||||
|
|
||||||
|
# @leet start
|
||||||
|
class Solution:
|
||||||
|
def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:
|
||||||
|
arr.sort()
|
||||||
|
min_abs = float(inf)
|
||||||
|
ans = []
|
||||||
|
for i in range(len(arr) - 1):
|
||||||
|
abs_sum = abs(arr[i] - arr[i + 1])
|
||||||
|
if abs_sum < min_abs:
|
||||||
|
ans = []
|
||||||
|
min_abs = abs_sum
|
||||||
|
ans.append([arr[i], arr[i + 1]])
|
||||||
|
elif abs_sum == min_abs:
|
||||||
|
ans.append([arr[i], arr[i + 1]])
|
||||||
|
|
||||||
|
return ans
|
||||||
|
|
||||||
|
|
||||||
|
# @leet end
|
||||||
50
src/exercises/121.best-time-to-buy-and-sell-stock.py
Normal file
50
src/exercises/121.best-time-to-buy-and-sell-stock.py
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
from string import *
|
||||||
|
from re import *
|
||||||
|
from datetime import *
|
||||||
|
from collections import *
|
||||||
|
from heapq import *
|
||||||
|
from bisect import *
|
||||||
|
from copy import *
|
||||||
|
from math import *
|
||||||
|
from random import *
|
||||||
|
from statistics import *
|
||||||
|
from itertools import *
|
||||||
|
from functools import *
|
||||||
|
from operator import *
|
||||||
|
from io import *
|
||||||
|
from sys import *
|
||||||
|
from json import *
|
||||||
|
from builtins import *
|
||||||
|
import string
|
||||||
|
import re
|
||||||
|
import datetime
|
||||||
|
import collections
|
||||||
|
import heapq
|
||||||
|
import bisect
|
||||||
|
import copy
|
||||||
|
import math
|
||||||
|
import random
|
||||||
|
import statistics
|
||||||
|
import itertools
|
||||||
|
import functools
|
||||||
|
import operator
|
||||||
|
import io
|
||||||
|
import sys
|
||||||
|
import json
|
||||||
|
from typing import *
|
||||||
|
|
||||||
|
|
||||||
|
# @leet start
|
||||||
|
class Solution:
|
||||||
|
def maxProfit(self, prices: List[int]) -> int:
|
||||||
|
max_profit = 0
|
||||||
|
min_price = float(inf)
|
||||||
|
for price in prices:
|
||||||
|
min_price = price if price < min_price else min_price
|
||||||
|
profit = price - min_price
|
||||||
|
max_profit = profit if max_profit < profit else max_profit
|
||||||
|
return max_profit
|
||||||
|
|
||||||
|
|
||||||
|
# @leet end
|
||||||
|
|
||||||
56
src/exercises/122.best-time-to-buy-and-sell-stock-ii.py
Normal file
56
src/exercises/122.best-time-to-buy-and-sell-stock-ii.py
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
# @leet imports start
|
||||||
|
from string import *
|
||||||
|
from re import *
|
||||||
|
from datetime import *
|
||||||
|
from collections import *
|
||||||
|
from heapq import *
|
||||||
|
from bisect import *
|
||||||
|
from copy import *
|
||||||
|
from math import *
|
||||||
|
from random import *
|
||||||
|
from statistics import *
|
||||||
|
from itertools import *
|
||||||
|
from functools import *
|
||||||
|
from operator import *
|
||||||
|
from io import *
|
||||||
|
from sys import *
|
||||||
|
from json import *
|
||||||
|
from builtins import *
|
||||||
|
import string
|
||||||
|
import re
|
||||||
|
import datetime
|
||||||
|
import collections
|
||||||
|
import heapq
|
||||||
|
import bisect
|
||||||
|
import copy
|
||||||
|
import math
|
||||||
|
import random
|
||||||
|
import statistics
|
||||||
|
import itertools
|
||||||
|
import functools
|
||||||
|
import operator
|
||||||
|
import io
|
||||||
|
import sys
|
||||||
|
import json
|
||||||
|
from typing import *
|
||||||
|
|
||||||
|
# @leet imports end
|
||||||
|
|
||||||
|
|
||||||
|
# @leet start
|
||||||
|
class Solution:
|
||||||
|
def maxProfit(self, prices: List[int]) -> int:
|
||||||
|
p = 0
|
||||||
|
buy = prices[0]
|
||||||
|
|
||||||
|
for i in range(1, len(prices), +1):
|
||||||
|
r = prices[i] - buy
|
||||||
|
if r > 0:
|
||||||
|
p += r
|
||||||
|
buy = prices[i]
|
||||||
|
|
||||||
|
return p
|
||||||
|
|
||||||
|
|
||||||
|
# @leet end
|
||||||
|
|
||||||
47
src/exercises/125.valid-palindrome.py
Normal file
47
src/exercises/125.valid-palindrome.py
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
from string import *
|
||||||
|
from re import *
|
||||||
|
from datetime import *
|
||||||
|
from collections import *
|
||||||
|
from heapq import *
|
||||||
|
from bisect import *
|
||||||
|
from copy import *
|
||||||
|
from math import *
|
||||||
|
from random import *
|
||||||
|
from statistics import *
|
||||||
|
from itertools import *
|
||||||
|
from functools import *
|
||||||
|
from operator import *
|
||||||
|
from io import *
|
||||||
|
from sys import *
|
||||||
|
from json import *
|
||||||
|
from builtins import *
|
||||||
|
import string
|
||||||
|
import re
|
||||||
|
import datetime
|
||||||
|
import collections
|
||||||
|
import heapq
|
||||||
|
import bisect
|
||||||
|
import copy
|
||||||
|
import math
|
||||||
|
import random
|
||||||
|
import statistics
|
||||||
|
import itertools
|
||||||
|
import functools
|
||||||
|
import operator
|
||||||
|
import io
|
||||||
|
import sys
|
||||||
|
import json
|
||||||
|
from typing import *
|
||||||
|
|
||||||
|
|
||||||
|
# @leet start
|
||||||
|
class Solution:
|
||||||
|
def isPalindrome(self, s: str) -> bool:
|
||||||
|
s = re.sub(r"[^a-zA-Z0-9]", "", s)
|
||||||
|
s = s.lower()
|
||||||
|
new_s = s[::-1]
|
||||||
|
return s == new_s
|
||||||
|
|
||||||
|
|
||||||
|
# @leet end
|
||||||
|
|
||||||
62
src/exercises/128.longest-consecutive-sequence.py
Normal file
62
src/exercises/128.longest-consecutive-sequence.py
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
from string import *
|
||||||
|
from re import *
|
||||||
|
from datetime import *
|
||||||
|
from collections import *
|
||||||
|
from heapq import *
|
||||||
|
from bisect import *
|
||||||
|
from copy import *
|
||||||
|
from math import *
|
||||||
|
from random import *
|
||||||
|
from statistics import *
|
||||||
|
from itertools import *
|
||||||
|
from functools import *
|
||||||
|
from operator import *
|
||||||
|
from io import *
|
||||||
|
from sys import *
|
||||||
|
from json import *
|
||||||
|
from builtins import *
|
||||||
|
import string
|
||||||
|
import re
|
||||||
|
import datetime
|
||||||
|
import collections
|
||||||
|
import heapq
|
||||||
|
import bisect
|
||||||
|
import copy
|
||||||
|
import math
|
||||||
|
import random
|
||||||
|
import statistics
|
||||||
|
import itertools
|
||||||
|
import functools
|
||||||
|
import operator
|
||||||
|
import io
|
||||||
|
import sys
|
||||||
|
import json
|
||||||
|
from typing import *
|
||||||
|
|
||||||
|
# @leet start
|
||||||
|
class Solution:
|
||||||
|
def longestConsecutive(self, nums: List[int]) -> int:
|
||||||
|
if len(nums) < 2:
|
||||||
|
return len(nums)
|
||||||
|
consecutive = set(nums)
|
||||||
|
consecutive = sorted(consecutive)
|
||||||
|
counter = [0] * len(consecutive)
|
||||||
|
count = 1
|
||||||
|
k = 0
|
||||||
|
for i in range(len(consecutive)):
|
||||||
|
if i == 0:
|
||||||
|
counter[k] = count
|
||||||
|
continue
|
||||||
|
if consecutive[i] == consecutive[i - 1] + 1:
|
||||||
|
count += 1
|
||||||
|
else:
|
||||||
|
count = 1
|
||||||
|
k += 1
|
||||||
|
counter[k] = count
|
||||||
|
max_num = 0
|
||||||
|
counter = [i for i in counter if i > 0]
|
||||||
|
for num in counter:
|
||||||
|
max_num = max(max_num, num)
|
||||||
|
return max_num
|
||||||
|
|
||||||
|
# @leet end
|
||||||
40
src/exercises/1298.maximum-candies-you-can-get-from-boxes.py
Normal file
40
src/exercises/1298.maximum-candies-you-can-get-from-boxes.py
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
from string import *
|
||||||
|
from re import *
|
||||||
|
from datetime import *
|
||||||
|
from collections import *
|
||||||
|
from heapq import *
|
||||||
|
from bisect import *
|
||||||
|
from copy import *
|
||||||
|
from math import *
|
||||||
|
from random import *
|
||||||
|
from statistics import *
|
||||||
|
from itertools import *
|
||||||
|
from functools import *
|
||||||
|
from operator import *
|
||||||
|
from io import *
|
||||||
|
from sys import *
|
||||||
|
from json import *
|
||||||
|
from builtins import *
|
||||||
|
import string
|
||||||
|
import re
|
||||||
|
import datetime
|
||||||
|
import collections
|
||||||
|
import heapq
|
||||||
|
import bisect
|
||||||
|
import copy
|
||||||
|
import math
|
||||||
|
import random
|
||||||
|
import statistics
|
||||||
|
import itertools
|
||||||
|
import functools
|
||||||
|
import operator
|
||||||
|
import io
|
||||||
|
import sys
|
||||||
|
import json
|
||||||
|
from typing import *
|
||||||
|
|
||||||
|
# @leet start
|
||||||
|
class Solution:
|
||||||
|
def maxCandies(self, status: List[int], candies: List[int], keys: List[List[int]], containedBoxes: List[List[int]], initialBoxes: List[int]) -> int:
|
||||||
|
|
||||||
|
# @leet end
|
||||||
67
src/exercises/13.roman-to-integer.py
Normal file
67
src/exercises/13.roman-to-integer.py
Normal file
@@ -0,0 +1,67 @@
|
|||||||
|
from string import *
|
||||||
|
from re import *
|
||||||
|
from datetime import *
|
||||||
|
from collections import *
|
||||||
|
from heapq import *
|
||||||
|
from bisect import *
|
||||||
|
from copy import *
|
||||||
|
from math import *
|
||||||
|
from random import *
|
||||||
|
from statistics import *
|
||||||
|
from itertools import *
|
||||||
|
from functools import *
|
||||||
|
from operator import *
|
||||||
|
from io import *
|
||||||
|
from sys import *
|
||||||
|
from json import *
|
||||||
|
from builtins import *
|
||||||
|
import string
|
||||||
|
import re
|
||||||
|
import datetime
|
||||||
|
import collections
|
||||||
|
import heapq
|
||||||
|
import bisect
|
||||||
|
import copy
|
||||||
|
import math
|
||||||
|
import random
|
||||||
|
import statistics
|
||||||
|
import itertools
|
||||||
|
import functools
|
||||||
|
import operator
|
||||||
|
import io
|
||||||
|
import sys
|
||||||
|
import json
|
||||||
|
from typing import *
|
||||||
|
|
||||||
|
# * I can be placed before V (5) and X (10) to make 4 and 9.
|
||||||
|
# * X can be placed before L (50) and C (100) to make 40 and 90.
|
||||||
|
# * C can be placed before D (500) and M (1000) to make 400 and 900.
|
||||||
|
|
||||||
|
# @leet start
|
||||||
|
class Solution:
|
||||||
|
def romanToInt(self, s: str) -> int:
|
||||||
|
numbers = {"I": 1, "V": 5, "X": 10, "L": 50, "C": 100, "D": 500, "M": 1000}
|
||||||
|
count = 0
|
||||||
|
i = len(s) - 1
|
||||||
|
while i > -1:
|
||||||
|
if i == 0:
|
||||||
|
count += numbers[s[i]]
|
||||||
|
elif (
|
||||||
|
(s[i] == "V" and s[i - 1] == "I")
|
||||||
|
or (s[i] == "X" and s[i - 1] == "I")
|
||||||
|
or (s[i] == "C" and s[i - 1] == "X")
|
||||||
|
or (s[i] == "L" and s[i - 1] == "X")
|
||||||
|
or (s[i] == "D" and s[i - 1] == "C")
|
||||||
|
or (s[i] == "M" and s[i - 1] == "C")
|
||||||
|
):
|
||||||
|
val = numbers[s[i]] - numbers[s[i - 1]]
|
||||||
|
count += val
|
||||||
|
i -= 1
|
||||||
|
else:
|
||||||
|
count += numbers[s[i]]
|
||||||
|
i -= 1
|
||||||
|
|
||||||
|
return count
|
||||||
|
|
||||||
|
|
||||||
|
# @leet end
|
||||||
53
src/exercises/135.candy.py
Normal file
53
src/exercises/135.candy.py
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
from string import *
|
||||||
|
from re import *
|
||||||
|
from datetime import *
|
||||||
|
from collections import *
|
||||||
|
from heapq import *
|
||||||
|
from bisect import *
|
||||||
|
from copy import *
|
||||||
|
from math import *
|
||||||
|
from random import *
|
||||||
|
from statistics import *
|
||||||
|
from itertools import *
|
||||||
|
from functools import *
|
||||||
|
from operator import *
|
||||||
|
from io import *
|
||||||
|
from sys import *
|
||||||
|
from json import *
|
||||||
|
from builtins import *
|
||||||
|
import string
|
||||||
|
import re
|
||||||
|
import datetime
|
||||||
|
import collections
|
||||||
|
import heapq
|
||||||
|
import bisect
|
||||||
|
import copy
|
||||||
|
import math
|
||||||
|
import random
|
||||||
|
import statistics
|
||||||
|
import itertools
|
||||||
|
import functools
|
||||||
|
import operator
|
||||||
|
import io
|
||||||
|
import sys
|
||||||
|
import json
|
||||||
|
from typing import *
|
||||||
|
|
||||||
|
|
||||||
|
# @leet start
|
||||||
|
class Solution:
|
||||||
|
def candy(self, ratings: List[int]) -> int:
|
||||||
|
arr = [1] * len(ratings)
|
||||||
|
for i in range(1, len(ratings)):
|
||||||
|
if ratings[i] > ratings[i - 1]:
|
||||||
|
arr[i] = arr[i - 1] + 1
|
||||||
|
|
||||||
|
for i in range(len(ratings) - 2, -1, -1):
|
||||||
|
if ratings[i] > ratings[i + 1]:
|
||||||
|
arr[i] = max(arr[i], arr[i + 1] + 1)
|
||||||
|
|
||||||
|
return sum(arr)
|
||||||
|
|
||||||
|
|
||||||
|
# @leet end
|
||||||
|
|
||||||
66
src/exercises/14.longest-common-prefix.py
Normal file
66
src/exercises/14.longest-common-prefix.py
Normal file
@@ -0,0 +1,66 @@
|
|||||||
|
from string import *
|
||||||
|
from re import *
|
||||||
|
from datetime import *
|
||||||
|
from collections import *
|
||||||
|
from heapq import *
|
||||||
|
from bisect import *
|
||||||
|
from copy import *
|
||||||
|
from math import *
|
||||||
|
from random import *
|
||||||
|
from statistics import *
|
||||||
|
from itertools import *
|
||||||
|
from functools import *
|
||||||
|
from operator import *
|
||||||
|
from io import *
|
||||||
|
from sys import *
|
||||||
|
from json import *
|
||||||
|
from builtins import *
|
||||||
|
import string
|
||||||
|
import re
|
||||||
|
import datetime
|
||||||
|
import collections
|
||||||
|
import heapq
|
||||||
|
import bisect
|
||||||
|
import copy
|
||||||
|
import math
|
||||||
|
import random
|
||||||
|
import statistics
|
||||||
|
import itertools
|
||||||
|
import functools
|
||||||
|
import operator
|
||||||
|
import io
|
||||||
|
import sys
|
||||||
|
import json
|
||||||
|
from typing import *
|
||||||
|
|
||||||
|
|
||||||
|
# @leet start
|
||||||
|
class TrieNode:
|
||||||
|
def __init__(self):
|
||||||
|
self.children = {}
|
||||||
|
self.is_end_of_word = False
|
||||||
|
|
||||||
|
|
||||||
|
class Solution:
|
||||||
|
def longestCommonPrefix(self, strs: List[str]) -> str:
|
||||||
|
if len(strs) < 1:
|
||||||
|
return ""
|
||||||
|
if len(strs) == 1:
|
||||||
|
return strs[0]
|
||||||
|
m = float(inf)
|
||||||
|
for w in strs:
|
||||||
|
m = len(w) if len(w) < m else m
|
||||||
|
for i in range(len(strs)):
|
||||||
|
strs[i] = strs[i][:m]
|
||||||
|
|
||||||
|
h = m
|
||||||
|
for i in range(m - 1, -1, -1):
|
||||||
|
current = strs[0][i]
|
||||||
|
for j in range(1, len(strs), +1):
|
||||||
|
if strs[j][i] != current:
|
||||||
|
h = i
|
||||||
|
|
||||||
|
return strs[0][:h]
|
||||||
|
|
||||||
|
|
||||||
|
# @leet end
|
||||||
48
src/exercises/141.linked-list-cycle.py
Normal file
48
src/exercises/141.linked-list-cycle.py
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
# @leet imports start
|
||||||
|
from string import *
|
||||||
|
from re import *
|
||||||
|
from datetime import *
|
||||||
|
from collections import *
|
||||||
|
from heapq import *
|
||||||
|
from bisect import *
|
||||||
|
from copy import *
|
||||||
|
from math import *
|
||||||
|
from random import *
|
||||||
|
from statistics import *
|
||||||
|
from itertools import *
|
||||||
|
from functools import *
|
||||||
|
from operator import *
|
||||||
|
from io import *
|
||||||
|
from sys import *
|
||||||
|
from json import *
|
||||||
|
from builtins import *
|
||||||
|
import string
|
||||||
|
import re
|
||||||
|
import datetime
|
||||||
|
import collections
|
||||||
|
import heapq
|
||||||
|
import bisect
|
||||||
|
import copy
|
||||||
|
import math
|
||||||
|
import random
|
||||||
|
import statistics
|
||||||
|
import itertools
|
||||||
|
import functools
|
||||||
|
import operator
|
||||||
|
import io
|
||||||
|
import sys
|
||||||
|
import json
|
||||||
|
from typing import *
|
||||||
|
# @leet imports end
|
||||||
|
|
||||||
|
# @leet start
|
||||||
|
# Definition for singly-linked list.
|
||||||
|
# class ListNode:
|
||||||
|
# def __init__(self, x):
|
||||||
|
# self.val = x
|
||||||
|
# self.next = None
|
||||||
|
|
||||||
|
class Solution:
|
||||||
|
def hasCycle(self, head: Optional[ListNode]) -> bool:
|
||||||
|
|
||||||
|
# @leet end
|
||||||
75
src/exercises/149.max-points-on-a-line.py
Normal file
75
src/exercises/149.max-points-on-a-line.py
Normal file
@@ -0,0 +1,75 @@
|
|||||||
|
# @leet imports start
|
||||||
|
from string import *
|
||||||
|
from re import *
|
||||||
|
from datetime import *
|
||||||
|
from collections import *
|
||||||
|
from heapq import *
|
||||||
|
from bisect import *
|
||||||
|
from copy import *
|
||||||
|
from math import *
|
||||||
|
from random import *
|
||||||
|
from statistics import *
|
||||||
|
from itertools import *
|
||||||
|
from functools import *
|
||||||
|
from operator import *
|
||||||
|
from io import *
|
||||||
|
from sys import *
|
||||||
|
from json import *
|
||||||
|
from builtins import *
|
||||||
|
import string
|
||||||
|
import re
|
||||||
|
import datetime
|
||||||
|
import collections
|
||||||
|
import heapq
|
||||||
|
import bisect
|
||||||
|
import copy
|
||||||
|
import math
|
||||||
|
import random
|
||||||
|
import statistics
|
||||||
|
import itertools
|
||||||
|
import functools
|
||||||
|
import operator
|
||||||
|
import io
|
||||||
|
import sys
|
||||||
|
import json
|
||||||
|
from typing import *
|
||||||
|
|
||||||
|
# @leet imports end
|
||||||
|
|
||||||
|
|
||||||
|
# @leet start
|
||||||
|
class Solution:
|
||||||
|
def maxPoints(self, points: List[List[int]]) -> int:
|
||||||
|
if len(points) <= 2:
|
||||||
|
return len(points)
|
||||||
|
i = 0
|
||||||
|
mx = float(-inf)
|
||||||
|
j = len(points) - 1
|
||||||
|
|
||||||
|
def getM(i, j):
|
||||||
|
y1 = points[i][1]
|
||||||
|
y2 = points[j][1]
|
||||||
|
x1 = points[i][0]
|
||||||
|
x2 = points[j][0]
|
||||||
|
if x2 - x1 == 0:
|
||||||
|
mm = float(inf)
|
||||||
|
else:
|
||||||
|
mm = (y2 - y1) / (x2 - x1)
|
||||||
|
return mm
|
||||||
|
|
||||||
|
for i in range(len(points) - 1):
|
||||||
|
m = {}
|
||||||
|
for j in range(i + 1, len(points), +1):
|
||||||
|
mm = getM(i, j)
|
||||||
|
if mm not in m:
|
||||||
|
m[mm] = 1
|
||||||
|
else:
|
||||||
|
m[mm] += 1
|
||||||
|
lmx = max(m.values() if m else 0)
|
||||||
|
mx = max(mx, lmx + 1)
|
||||||
|
|
||||||
|
return mx
|
||||||
|
|
||||||
|
|
||||||
|
# @leet end
|
||||||
|
|
||||||
68
src/exercises/15.3sum.py
Normal file
68
src/exercises/15.3sum.py
Normal file
@@ -0,0 +1,68 @@
|
|||||||
|
from string import *
|
||||||
|
from re import *
|
||||||
|
from datetime import *
|
||||||
|
from collections import *
|
||||||
|
from heapq import *
|
||||||
|
from bisect import *
|
||||||
|
from copy import *
|
||||||
|
from math import *
|
||||||
|
from random import *
|
||||||
|
from statistics import *
|
||||||
|
from itertools import *
|
||||||
|
from functools import *
|
||||||
|
from operator import *
|
||||||
|
from io import *
|
||||||
|
from sys import *
|
||||||
|
from json import *
|
||||||
|
from builtins import *
|
||||||
|
import string
|
||||||
|
import re
|
||||||
|
import datetime
|
||||||
|
import collections
|
||||||
|
import heapq
|
||||||
|
import bisect
|
||||||
|
import copy
|
||||||
|
import math
|
||||||
|
import random
|
||||||
|
import statistics
|
||||||
|
import itertools
|
||||||
|
import functools
|
||||||
|
import operator
|
||||||
|
import io
|
||||||
|
import sys
|
||||||
|
import json
|
||||||
|
from typing import *
|
||||||
|
|
||||||
|
|
||||||
|
# @leet start
|
||||||
|
class Solution:
|
||||||
|
def threeSum(self, nums: List[int]) -> List[List[int]]:
|
||||||
|
nums.sort()
|
||||||
|
n = len(nums)
|
||||||
|
arr = []
|
||||||
|
for i in range(0, len(nums) - 1):
|
||||||
|
j = i + 1
|
||||||
|
k = n - 1
|
||||||
|
if i > 0 and nums[i] == nums[i - 1]:
|
||||||
|
continue
|
||||||
|
while j < k:
|
||||||
|
sum = nums[j] + nums[i] + nums[k]
|
||||||
|
if sum == 0:
|
||||||
|
val = [nums[i], nums[j], nums[k]]
|
||||||
|
val.sort()
|
||||||
|
arr.append(val)
|
||||||
|
while j < k and nums[j] == nums[j + 1]:
|
||||||
|
j += 1
|
||||||
|
while j < k and nums[k] == nums[k - 1]:
|
||||||
|
k -= 1
|
||||||
|
j += 1
|
||||||
|
k -= 1
|
||||||
|
elif sum < 0:
|
||||||
|
j += 1
|
||||||
|
elif sum > 0:
|
||||||
|
k -= 1
|
||||||
|
return arr
|
||||||
|
|
||||||
|
|
||||||
|
# @leet end
|
||||||
|
|
||||||
40
src/exercises/150.evaluate-reverse-polish-notation.py
Normal file
40
src/exercises/150.evaluate-reverse-polish-notation.py
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
from string import *
|
||||||
|
from re import *
|
||||||
|
from datetime import *
|
||||||
|
from collections import *
|
||||||
|
from heapq import *
|
||||||
|
from bisect import *
|
||||||
|
from copy import *
|
||||||
|
from math import *
|
||||||
|
from random import *
|
||||||
|
from statistics import *
|
||||||
|
from itertools import *
|
||||||
|
from functools import *
|
||||||
|
from operator import *
|
||||||
|
from io import *
|
||||||
|
from sys import *
|
||||||
|
from json import *
|
||||||
|
from builtins import *
|
||||||
|
import string
|
||||||
|
import re
|
||||||
|
import datetime
|
||||||
|
import collections
|
||||||
|
import heapq
|
||||||
|
import bisect
|
||||||
|
import copy
|
||||||
|
import math
|
||||||
|
import random
|
||||||
|
import statistics
|
||||||
|
import itertools
|
||||||
|
import functools
|
||||||
|
import operator
|
||||||
|
import io
|
||||||
|
import sys
|
||||||
|
import json
|
||||||
|
from typing import *
|
||||||
|
|
||||||
|
# @leet start
|
||||||
|
class Solution:
|
||||||
|
def evalRPN(self, tokens: List[str]) -> int:
|
||||||
|
|
||||||
|
# @leet end
|
||||||
54
src/exercises/153.find-minimum-in-rotated-sorted-array.py
Normal file
54
src/exercises/153.find-minimum-in-rotated-sorted-array.py
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
from string import *
|
||||||
|
from re import *
|
||||||
|
from datetime import *
|
||||||
|
from collections import *
|
||||||
|
from heapq import *
|
||||||
|
from bisect import *
|
||||||
|
from copy import *
|
||||||
|
from math import *
|
||||||
|
from random import *
|
||||||
|
from statistics import *
|
||||||
|
from itertools import *
|
||||||
|
from functools import *
|
||||||
|
from operator import *
|
||||||
|
from io import *
|
||||||
|
from sys import *
|
||||||
|
from json import *
|
||||||
|
from builtins import *
|
||||||
|
import string
|
||||||
|
import re
|
||||||
|
import datetime
|
||||||
|
import collections
|
||||||
|
import heapq
|
||||||
|
import bisect
|
||||||
|
import copy
|
||||||
|
import math
|
||||||
|
import random
|
||||||
|
import statistics
|
||||||
|
import itertools
|
||||||
|
import functools
|
||||||
|
import operator
|
||||||
|
import io
|
||||||
|
import sys
|
||||||
|
import json
|
||||||
|
from typing import *
|
||||||
|
|
||||||
|
|
||||||
|
# @leet start
|
||||||
|
class Solution:
|
||||||
|
def findMin(self, nums: List[int]) -> int:
|
||||||
|
l = 0
|
||||||
|
h = len(nums) - 1
|
||||||
|
while l <= h:
|
||||||
|
if l == h:
|
||||||
|
return nums[l]
|
||||||
|
mid = (l + h) // 2
|
||||||
|
if nums[mid] > nums[h]:
|
||||||
|
l = mid + 1
|
||||||
|
else:
|
||||||
|
h = mid
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# @leet end
|
||||||
|
|
||||||
69
src/exercises/155.min-stack.py
Normal file
69
src/exercises/155.min-stack.py
Normal file
@@ -0,0 +1,69 @@
|
|||||||
|
from string import *
|
||||||
|
from re import *
|
||||||
|
from datetime import *
|
||||||
|
from collections import *
|
||||||
|
from heapq import *
|
||||||
|
from bisect import *
|
||||||
|
from copy import *
|
||||||
|
from math import *
|
||||||
|
from random import *
|
||||||
|
from statistics import *
|
||||||
|
from itertools import *
|
||||||
|
from functools import *
|
||||||
|
from operator import *
|
||||||
|
from io import *
|
||||||
|
from sys import *
|
||||||
|
from json import *
|
||||||
|
from builtins import *
|
||||||
|
import string
|
||||||
|
import re
|
||||||
|
import datetime
|
||||||
|
import collections
|
||||||
|
import heapq
|
||||||
|
import bisect
|
||||||
|
import copy
|
||||||
|
import math
|
||||||
|
import random
|
||||||
|
import statistics
|
||||||
|
import itertools
|
||||||
|
import functools
|
||||||
|
import operator
|
||||||
|
import io
|
||||||
|
import sys
|
||||||
|
import json
|
||||||
|
from typing import *
|
||||||
|
|
||||||
|
|
||||||
|
# @leet start
|
||||||
|
class MinStack:
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
self.stack = deque()
|
||||||
|
self.min = deque()
|
||||||
|
|
||||||
|
def push(self, val: int) -> None:
|
||||||
|
self.stack.append(val)
|
||||||
|
if not self.min or self.min[-1] >= val:
|
||||||
|
self.min.append(val)
|
||||||
|
else:
|
||||||
|
self.min.append(self.min[-1])
|
||||||
|
|
||||||
|
def pop(self) -> None:
|
||||||
|
self.stack.pop()
|
||||||
|
self.min.pop()
|
||||||
|
|
||||||
|
def top(self) -> int:
|
||||||
|
return self.stack[len(self.stack) - 1]
|
||||||
|
|
||||||
|
def getMin(self) -> int:
|
||||||
|
return self.min[-1]
|
||||||
|
|
||||||
|
|
||||||
|
# Your MinStack object will be instantiated and called as such:
|
||||||
|
# obj = MinStack()
|
||||||
|
# obj.push(val)
|
||||||
|
# obj.pop()
|
||||||
|
# param_3 = obj.top()
|
||||||
|
# param_4 = obj.getMin()
|
||||||
|
# @leet end
|
||||||
|
|
||||||
54
src/exercises/167.two-sum-ii-input-array-is-sorted.py
Normal file
54
src/exercises/167.two-sum-ii-input-array-is-sorted.py
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
from string import *
|
||||||
|
from re import *
|
||||||
|
from datetime import *
|
||||||
|
from collections import *
|
||||||
|
from heapq import *
|
||||||
|
from bisect import *
|
||||||
|
from copy import *
|
||||||
|
from math import *
|
||||||
|
from random import *
|
||||||
|
from statistics import *
|
||||||
|
from itertools import *
|
||||||
|
from functools import *
|
||||||
|
from operator import *
|
||||||
|
from io import *
|
||||||
|
from sys import *
|
||||||
|
from json import *
|
||||||
|
from builtins import *
|
||||||
|
import string
|
||||||
|
import re
|
||||||
|
import datetime
|
||||||
|
import collections
|
||||||
|
import heapq
|
||||||
|
import bisect
|
||||||
|
import copy
|
||||||
|
import math
|
||||||
|
import random
|
||||||
|
import statistics
|
||||||
|
import itertools
|
||||||
|
import functools
|
||||||
|
import operator
|
||||||
|
import io
|
||||||
|
import sys
|
||||||
|
import json
|
||||||
|
from typing import *
|
||||||
|
|
||||||
|
# @leet start
|
||||||
|
class Solution:
|
||||||
|
def twoSum(self, numbers: List[int], target: int) -> List[int]:
|
||||||
|
i = 0
|
||||||
|
j = len(numbers) - 1
|
||||||
|
while i < j:
|
||||||
|
need = target - numbers[i]
|
||||||
|
if need == numbers[j]:
|
||||||
|
break
|
||||||
|
if need < numbers[j]:
|
||||||
|
j -= 1
|
||||||
|
elif need > numbers[i]:
|
||||||
|
i += 1
|
||||||
|
|
||||||
|
return [i + 1, j + 1]
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# @leet end
|
||||||
48
src/exercises/169.majority-element.py
Normal file
48
src/exercises/169.majority-element.py
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
# @leet imports start
|
||||||
|
from string import *
|
||||||
|
from re import *
|
||||||
|
from datetime import *
|
||||||
|
from collections import *
|
||||||
|
from heapq import *
|
||||||
|
from bisect import *
|
||||||
|
from copy import *
|
||||||
|
from math import *
|
||||||
|
from random import *
|
||||||
|
from statistics import *
|
||||||
|
from itertools import *
|
||||||
|
from functools import *
|
||||||
|
from operator import *
|
||||||
|
from io import *
|
||||||
|
from sys import *
|
||||||
|
from json import *
|
||||||
|
from builtins import *
|
||||||
|
import string
|
||||||
|
import re
|
||||||
|
import datetime
|
||||||
|
import collections
|
||||||
|
import heapq
|
||||||
|
import bisect
|
||||||
|
import copy
|
||||||
|
import math
|
||||||
|
import random
|
||||||
|
import statistics
|
||||||
|
import itertools
|
||||||
|
import functools
|
||||||
|
import operator
|
||||||
|
import io
|
||||||
|
import sys
|
||||||
|
import json
|
||||||
|
from typing import *
|
||||||
|
|
||||||
|
# @leet imports end
|
||||||
|
|
||||||
|
|
||||||
|
# @leet start
|
||||||
|
class Solution:
|
||||||
|
def majorityElement(self, nums: List[int]) -> int:
|
||||||
|
nums.sort()
|
||||||
|
return nums[len(nums)//2]
|
||||||
|
|
||||||
|
|
||||||
|
# @leet end
|
||||||
|
|
||||||
55
src/exercises/172.factorial-trailing-zeroes.py
Normal file
55
src/exercises/172.factorial-trailing-zeroes.py
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
# @leet imports start
|
||||||
|
from string import *
|
||||||
|
from re import *
|
||||||
|
from datetime import *
|
||||||
|
from collections import *
|
||||||
|
from heapq import *
|
||||||
|
from bisect import *
|
||||||
|
from copy import *
|
||||||
|
from math import *
|
||||||
|
from random import *
|
||||||
|
from statistics import *
|
||||||
|
from itertools import *
|
||||||
|
from functools import *
|
||||||
|
from operator import *
|
||||||
|
from io import *
|
||||||
|
from sys import *
|
||||||
|
from json import *
|
||||||
|
from builtins import *
|
||||||
|
import string
|
||||||
|
import re
|
||||||
|
import datetime
|
||||||
|
import collections
|
||||||
|
import heapq
|
||||||
|
import bisect
|
||||||
|
import copy
|
||||||
|
import math
|
||||||
|
import random
|
||||||
|
import statistics
|
||||||
|
import itertools
|
||||||
|
import functools
|
||||||
|
import operator
|
||||||
|
import io
|
||||||
|
import sys
|
||||||
|
import json
|
||||||
|
from typing import *
|
||||||
|
|
||||||
|
# @leet imports end
|
||||||
|
|
||||||
|
|
||||||
|
# @leet start
|
||||||
|
class Solution:
|
||||||
|
def trailingZeroes(self, n: int) -> int:
|
||||||
|
if n < 5:
|
||||||
|
return 0
|
||||||
|
z = 5
|
||||||
|
c = 0
|
||||||
|
while z <= n:
|
||||||
|
c += n // z
|
||||||
|
z *= 5
|
||||||
|
|
||||||
|
return c
|
||||||
|
|
||||||
|
|
||||||
|
# @leet end
|
||||||
|
|
||||||
42
src/exercises/1820.maximum-number-of-accepted-invitations.py
Normal file
42
src/exercises/1820.maximum-number-of-accepted-invitations.py
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
# @leet imports start
|
||||||
|
from string import *
|
||||||
|
from re import *
|
||||||
|
from datetime import *
|
||||||
|
from collections import *
|
||||||
|
from heapq import *
|
||||||
|
from bisect import *
|
||||||
|
from copy import *
|
||||||
|
from math import *
|
||||||
|
from random import *
|
||||||
|
from statistics import *
|
||||||
|
from itertools import *
|
||||||
|
from functools import *
|
||||||
|
from operator import *
|
||||||
|
from io import *
|
||||||
|
from sys import *
|
||||||
|
from json import *
|
||||||
|
from builtins import *
|
||||||
|
import string
|
||||||
|
import re
|
||||||
|
import datetime
|
||||||
|
import collections
|
||||||
|
import heapq
|
||||||
|
import bisect
|
||||||
|
import copy
|
||||||
|
import math
|
||||||
|
import random
|
||||||
|
import statistics
|
||||||
|
import itertools
|
||||||
|
import functools
|
||||||
|
import operator
|
||||||
|
import io
|
||||||
|
import sys
|
||||||
|
import json
|
||||||
|
from typing import *
|
||||||
|
# @leet imports end
|
||||||
|
|
||||||
|
# @leet start
|
||||||
|
class Solution:
|
||||||
|
def maximumInvitations(self, grid: List[List[int]]) -> int:
|
||||||
|
|
||||||
|
# @leet end
|
||||||
60
src/exercises/189.rotate-array.py
Normal file
60
src/exercises/189.rotate-array.py
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
# @leet imports start
|
||||||
|
from string import *
|
||||||
|
from re import *
|
||||||
|
from datetime import *
|
||||||
|
from collections import *
|
||||||
|
from heapq import *
|
||||||
|
from bisect import *
|
||||||
|
from copy import *
|
||||||
|
from math import *
|
||||||
|
from random import *
|
||||||
|
from statistics import *
|
||||||
|
from itertools import *
|
||||||
|
from functools import *
|
||||||
|
from operator import *
|
||||||
|
from io import *
|
||||||
|
from sys import *
|
||||||
|
from json import *
|
||||||
|
from builtins import *
|
||||||
|
import string
|
||||||
|
import re
|
||||||
|
import datetime
|
||||||
|
import collections
|
||||||
|
import heapq
|
||||||
|
import bisect
|
||||||
|
import copy
|
||||||
|
import math
|
||||||
|
import random
|
||||||
|
import statistics
|
||||||
|
import itertools
|
||||||
|
import functools
|
||||||
|
import operator
|
||||||
|
import io
|
||||||
|
import sys
|
||||||
|
import json
|
||||||
|
from typing import *
|
||||||
|
# @leet imports end
|
||||||
|
|
||||||
|
# @leet start
|
||||||
|
class Solution:
|
||||||
|
def rotate(self, nums: List[int], k: int) -> None:
|
||||||
|
"""
|
||||||
|
Do not return anything, modify nums in-place instead.
|
||||||
|
"""
|
||||||
|
if len(nums) <= 1:
|
||||||
|
return
|
||||||
|
|
||||||
|
l = len(nums)
|
||||||
|
arr = [0] * l
|
||||||
|
k = k % len(nums) if k > len(nums) else k
|
||||||
|
j = 1
|
||||||
|
for i in range(l - 1, -1, -1):
|
||||||
|
x = k - j
|
||||||
|
if x < 0:
|
||||||
|
x += l
|
||||||
|
arr[x] = nums[i]
|
||||||
|
j += 1
|
||||||
|
for i in range(len(arr)):
|
||||||
|
nums[i] = arr[i]
|
||||||
|
|
||||||
|
# @leet end
|
||||||
85
src/exercises/2.add-two-numbers.py
Normal file
85
src/exercises/2.add-two-numbers.py
Normal file
@@ -0,0 +1,85 @@
|
|||||||
|
from string import *
|
||||||
|
from re import *
|
||||||
|
from datetime import *
|
||||||
|
from collections import *
|
||||||
|
from heapq import *
|
||||||
|
from bisect import *
|
||||||
|
from copy import *
|
||||||
|
from math import *
|
||||||
|
from random import *
|
||||||
|
from statistics import *
|
||||||
|
from itertools import *
|
||||||
|
from functools import *
|
||||||
|
from operator import *
|
||||||
|
from io import *
|
||||||
|
from sys import *
|
||||||
|
from json import *
|
||||||
|
from builtins import *
|
||||||
|
import string
|
||||||
|
import re
|
||||||
|
import datetime
|
||||||
|
import collections
|
||||||
|
import heapq
|
||||||
|
import bisect
|
||||||
|
import copy
|
||||||
|
import math
|
||||||
|
import random
|
||||||
|
import statistics
|
||||||
|
import itertools
|
||||||
|
import functools
|
||||||
|
import operator
|
||||||
|
import io
|
||||||
|
import sys
|
||||||
|
import json
|
||||||
|
from typing import *
|
||||||
|
|
||||||
|
|
||||||
|
# @leet start
|
||||||
|
# Definition for singly-linked list.
|
||||||
|
# class ListNode:
|
||||||
|
# def __init__(self, val=0, next=None):
|
||||||
|
# self.val = val
|
||||||
|
# self.next = next
|
||||||
|
class Solution:
|
||||||
|
def node(self, val, next=None):
|
||||||
|
self.val = val
|
||||||
|
self.next = next
|
||||||
|
|
||||||
|
def addTwoNumbers(
|
||||||
|
self, l1: Optional[ListNode], l2: Optional[ListNode]
|
||||||
|
) -> Optional[ListNode]:
|
||||||
|
carry = 0
|
||||||
|
value_l1 = l1
|
||||||
|
value_l2 = l2
|
||||||
|
head = None
|
||||||
|
tail = None
|
||||||
|
while value_l1 or value_l2:
|
||||||
|
l2_val = l2.val if l2 else 0
|
||||||
|
l1_val = l1.val if l1 else 0
|
||||||
|
addition = l1_val + l2_val + carry
|
||||||
|
if addition > 9:
|
||||||
|
temp = str(addition)
|
||||||
|
carry = int(temp[0])
|
||||||
|
addition = int(temp[1])
|
||||||
|
else:
|
||||||
|
carry = 0
|
||||||
|
new_node = ListNode(addition)
|
||||||
|
if head is None:
|
||||||
|
head = new_node
|
||||||
|
tail = new_node
|
||||||
|
else:
|
||||||
|
tail.next = new_node
|
||||||
|
tail = new_node
|
||||||
|
value_l1 = l1.next if l1 else None
|
||||||
|
value_l2 = l2.next if l2 else None
|
||||||
|
l1 = l1.next if l1 else None
|
||||||
|
l2 = l2.next if l2 else None
|
||||||
|
if carry > 0:
|
||||||
|
new_node = ListNode(carry)
|
||||||
|
tail.next = new_node
|
||||||
|
tail = new_node
|
||||||
|
|
||||||
|
return head
|
||||||
|
|
||||||
|
|
||||||
|
# @leet end
|
||||||
57
src/exercises/20.valid-parentheses.py
Normal file
57
src/exercises/20.valid-parentheses.py
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
from string import *
|
||||||
|
from re import *
|
||||||
|
from datetime import *
|
||||||
|
from collections import *
|
||||||
|
from heapq import *
|
||||||
|
from bisect import *
|
||||||
|
from copy import *
|
||||||
|
from math import *
|
||||||
|
from random import *
|
||||||
|
from statistics import *
|
||||||
|
from itertools import *
|
||||||
|
from functools import *
|
||||||
|
from operator import *
|
||||||
|
from io import *
|
||||||
|
from sys import *
|
||||||
|
from json import *
|
||||||
|
from builtins import *
|
||||||
|
import string
|
||||||
|
import re
|
||||||
|
import datetime
|
||||||
|
import collections
|
||||||
|
import heapq
|
||||||
|
import bisect
|
||||||
|
import copy
|
||||||
|
import math
|
||||||
|
import random
|
||||||
|
import statistics
|
||||||
|
import itertools
|
||||||
|
import functools
|
||||||
|
import operator
|
||||||
|
import io
|
||||||
|
import sys
|
||||||
|
import json
|
||||||
|
from typing import *
|
||||||
|
|
||||||
|
|
||||||
|
# @leet start
|
||||||
|
class Solution:
|
||||||
|
def isValid(self, s: str) -> bool:
|
||||||
|
if len(s) < 2:
|
||||||
|
return False
|
||||||
|
valid = {"]": "[", ")": "(", "}": "{"}
|
||||||
|
arr = []
|
||||||
|
for i in range(len(s)):
|
||||||
|
if s[i] in valid:
|
||||||
|
if len(arr) == 0:
|
||||||
|
return False
|
||||||
|
if arr[len(arr) - 1] != valid[s[i]]:
|
||||||
|
return False
|
||||||
|
arr.pop()
|
||||||
|
else:
|
||||||
|
arr.append(s[i])
|
||||||
|
return len(arr) == 0
|
||||||
|
|
||||||
|
|
||||||
|
# @leet end
|
||||||
|
|
||||||
63
src/exercises/202.happy-number.py
Normal file
63
src/exercises/202.happy-number.py
Normal file
@@ -0,0 +1,63 @@
|
|||||||
|
# @leet imports start
|
||||||
|
from string import *
|
||||||
|
from re import *
|
||||||
|
from datetime import *
|
||||||
|
from collections import *
|
||||||
|
from heapq import *
|
||||||
|
from bisect import *
|
||||||
|
from copy import *
|
||||||
|
from math import *
|
||||||
|
from random import *
|
||||||
|
from statistics import *
|
||||||
|
from itertools import *
|
||||||
|
from functools import *
|
||||||
|
from operator import *
|
||||||
|
from io import *
|
||||||
|
from sys import *
|
||||||
|
from json import *
|
||||||
|
from builtins import *
|
||||||
|
import string
|
||||||
|
import re
|
||||||
|
import datetime
|
||||||
|
import collections
|
||||||
|
import heapq
|
||||||
|
import bisect
|
||||||
|
import copy
|
||||||
|
import math
|
||||||
|
import random
|
||||||
|
import statistics
|
||||||
|
import itertools
|
||||||
|
import functools
|
||||||
|
import operator
|
||||||
|
import io
|
||||||
|
import sys
|
||||||
|
import json
|
||||||
|
from typing import *
|
||||||
|
|
||||||
|
# @leet imports end
|
||||||
|
|
||||||
|
|
||||||
|
# @leet start
|
||||||
|
class Solution:
|
||||||
|
def isHappy(self, n: int) -> bool:
|
||||||
|
def getHappyNumber(num, memo):
|
||||||
|
if num not in memo:
|
||||||
|
memo.append(num)
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
count = 0
|
||||||
|
while num > 0:
|
||||||
|
d = num % 10
|
||||||
|
count += d**2
|
||||||
|
num //= 10
|
||||||
|
|
||||||
|
if count == 1:
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
return getHappyNumber(count, memo)
|
||||||
|
|
||||||
|
return getHappyNumber(n, [])
|
||||||
|
|
||||||
|
|
||||||
|
# @leet end
|
||||||
|
|
||||||
@@ -0,0 +1,83 @@
|
|||||||
|
from string import *
|
||||||
|
from re import *
|
||||||
|
from datetime import *
|
||||||
|
from collections import *
|
||||||
|
from heapq import *
|
||||||
|
from bisect import *
|
||||||
|
from copy import *
|
||||||
|
from math import *
|
||||||
|
from random import *
|
||||||
|
from statistics import *
|
||||||
|
from itertools import *
|
||||||
|
from functools import *
|
||||||
|
from operator import *
|
||||||
|
from io import *
|
||||||
|
from sys import *
|
||||||
|
from json import *
|
||||||
|
from builtins import *
|
||||||
|
import string
|
||||||
|
import re
|
||||||
|
import datetime
|
||||||
|
import collections
|
||||||
|
import heapq
|
||||||
|
import bisect
|
||||||
|
import copy
|
||||||
|
import math
|
||||||
|
import random
|
||||||
|
import statistics
|
||||||
|
import itertools
|
||||||
|
import functools
|
||||||
|
import operator
|
||||||
|
import io
|
||||||
|
import sys
|
||||||
|
import json
|
||||||
|
from typing import *
|
||||||
|
|
||||||
|
|
||||||
|
# @leet start
|
||||||
|
class Solution:
|
||||||
|
def kthSmallestProduct(self, nums1: List[int], nums2: List[int], k: int) -> int:
|
||||||
|
low = -(10**10)
|
||||||
|
high = 10**10
|
||||||
|
|
||||||
|
def countLessEqual(p):
|
||||||
|
count = 0
|
||||||
|
for i in range(len(nums1)):
|
||||||
|
l, h = 0, len(nums2)
|
||||||
|
if nums1[i] == 0 and p >= 0:
|
||||||
|
count += len(nums2)
|
||||||
|
elif nums1[i] > 0:
|
||||||
|
while l < h:
|
||||||
|
mid = (l + h) // 2
|
||||||
|
x = nums1[i] * nums2[mid]
|
||||||
|
if x <= p:
|
||||||
|
l = mid + 1
|
||||||
|
else:
|
||||||
|
h = mid
|
||||||
|
count += l
|
||||||
|
else:
|
||||||
|
while l < h:
|
||||||
|
mid = (l + h) // 2
|
||||||
|
x = nums1[i] * nums2[mid]
|
||||||
|
if x <= p:
|
||||||
|
h = mid
|
||||||
|
else:
|
||||||
|
l = mid + 1
|
||||||
|
count += len(nums2) - l
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
return count
|
||||||
|
|
||||||
|
while low < high:
|
||||||
|
mid = (low + high) // 2
|
||||||
|
count = countLessEqual(mid)
|
||||||
|
if count < k:
|
||||||
|
low = mid + 1
|
||||||
|
else:
|
||||||
|
high = mid
|
||||||
|
|
||||||
|
return low
|
||||||
|
|
||||||
|
|
||||||
|
# @leet end
|
||||||
57
src/exercises/205.isomorphic-strings.py
Normal file
57
src/exercises/205.isomorphic-strings.py
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
# @leet imports start
|
||||||
|
from string import *
|
||||||
|
from re import *
|
||||||
|
from datetime import *
|
||||||
|
from collections import *
|
||||||
|
from heapq import *
|
||||||
|
from bisect import *
|
||||||
|
from copy import *
|
||||||
|
from math import *
|
||||||
|
from random import *
|
||||||
|
from statistics import *
|
||||||
|
from itertools import *
|
||||||
|
from functools import *
|
||||||
|
from operator import *
|
||||||
|
from io import *
|
||||||
|
from sys import *
|
||||||
|
from json import *
|
||||||
|
from builtins import *
|
||||||
|
import string
|
||||||
|
import re
|
||||||
|
import datetime
|
||||||
|
import collections
|
||||||
|
import heapq
|
||||||
|
import bisect
|
||||||
|
import copy
|
||||||
|
import math
|
||||||
|
import random
|
||||||
|
import statistics
|
||||||
|
import itertools
|
||||||
|
import functools
|
||||||
|
import operator
|
||||||
|
import io
|
||||||
|
import sys
|
||||||
|
import json
|
||||||
|
from typing import *
|
||||||
|
|
||||||
|
# @leet imports end
|
||||||
|
|
||||||
|
|
||||||
|
# @leet start
|
||||||
|
class Solution:
|
||||||
|
def isIsomorphic(self, s: str, t: str) -> bool:
|
||||||
|
if len(s) != len(t):
|
||||||
|
return False
|
||||||
|
d = {}
|
||||||
|
for i in range(len(s)):
|
||||||
|
if s[i] not in d:
|
||||||
|
if t[i] in d.values():
|
||||||
|
return False
|
||||||
|
d[s[i]] = t[i]
|
||||||
|
else:
|
||||||
|
if s[i] in d and d[s[i]] != t[i]:
|
||||||
|
return False
|
||||||
|
|
||||||
|
return True
|
||||||
|
# @leet end
|
||||||
|
|
||||||
60
src/exercises/2053.kth-distinct-string-in-an-array.py
Normal file
60
src/exercises/2053.kth-distinct-string-in-an-array.py
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
# @leet imports start
|
||||||
|
from string import *
|
||||||
|
from re import *
|
||||||
|
from datetime import *
|
||||||
|
from collections import *
|
||||||
|
from heapq import *
|
||||||
|
from bisect import *
|
||||||
|
from copy import *
|
||||||
|
from math import *
|
||||||
|
from random import *
|
||||||
|
from statistics import *
|
||||||
|
from itertools import *
|
||||||
|
from functools import *
|
||||||
|
from operator import *
|
||||||
|
from io import *
|
||||||
|
from sys import *
|
||||||
|
from json import *
|
||||||
|
from builtins import *
|
||||||
|
import string
|
||||||
|
import re
|
||||||
|
import datetime
|
||||||
|
import collections
|
||||||
|
import heapq
|
||||||
|
import bisect
|
||||||
|
import copy
|
||||||
|
import math
|
||||||
|
import random
|
||||||
|
import statistics
|
||||||
|
import itertools
|
||||||
|
import functools
|
||||||
|
import operator
|
||||||
|
import io
|
||||||
|
import sys
|
||||||
|
import json
|
||||||
|
from typing import *
|
||||||
|
|
||||||
|
# @leet imports end
|
||||||
|
|
||||||
|
|
||||||
|
# @leet start
|
||||||
|
class Solution:
|
||||||
|
def kthDistinct(self, arr: List[str], k: int) -> str:
|
||||||
|
disc = {}
|
||||||
|
banW = []
|
||||||
|
for w in arr:
|
||||||
|
if w not in disc and w not in banW:
|
||||||
|
disc[w] = 1
|
||||||
|
else:
|
||||||
|
if w not in banW:
|
||||||
|
banW.append(w)
|
||||||
|
del disc[w]
|
||||||
|
|
||||||
|
arr = list(disc.keys())
|
||||||
|
if k > len(arr):
|
||||||
|
return ""
|
||||||
|
return arr[k - 1]
|
||||||
|
|
||||||
|
|
||||||
|
# @leet end
|
||||||
|
|
||||||
59
src/exercises/206.reverse-linked-list.py
Normal file
59
src/exercises/206.reverse-linked-list.py
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
from string import *
|
||||||
|
from re import *
|
||||||
|
from datetime import *
|
||||||
|
from collections import *
|
||||||
|
from heapq import *
|
||||||
|
from bisect import *
|
||||||
|
from copy import *
|
||||||
|
from math import *
|
||||||
|
from random import *
|
||||||
|
from statistics import *
|
||||||
|
from itertools import *
|
||||||
|
from functools import *
|
||||||
|
from operator import *
|
||||||
|
from io import *
|
||||||
|
from sys import *
|
||||||
|
from json import *
|
||||||
|
from builtins import *
|
||||||
|
import string
|
||||||
|
import re
|
||||||
|
import datetime
|
||||||
|
import collections
|
||||||
|
import heapq
|
||||||
|
import bisect
|
||||||
|
import copy
|
||||||
|
import math
|
||||||
|
import random
|
||||||
|
import statistics
|
||||||
|
import itertools
|
||||||
|
import functools
|
||||||
|
import operator
|
||||||
|
import io
|
||||||
|
import sys
|
||||||
|
import json
|
||||||
|
from typing import *
|
||||||
|
|
||||||
|
|
||||||
|
# @leet start
|
||||||
|
# Definition for singly-linked list.
|
||||||
|
class ListNode:
|
||||||
|
def __init__(self, val=0, next=None):
|
||||||
|
self.val = val
|
||||||
|
self.next = next
|
||||||
|
|
||||||
|
|
||||||
|
class Solution:
|
||||||
|
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
|
||||||
|
prev = None
|
||||||
|
curr = head
|
||||||
|
|
||||||
|
while curr:
|
||||||
|
next_node = curr.next
|
||||||
|
curr.next = prev
|
||||||
|
prev = curr
|
||||||
|
curr = next_node
|
||||||
|
|
||||||
|
return prev
|
||||||
|
|
||||||
|
|
||||||
|
# @leet end
|
||||||
45
src/exercises/21.merge-two-sorted-lists.py
Normal file
45
src/exercises/21.merge-two-sorted-lists.py
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
from string import *
|
||||||
|
from re import *
|
||||||
|
from datetime import *
|
||||||
|
from collections import *
|
||||||
|
from heapq import *
|
||||||
|
from bisect import *
|
||||||
|
from copy import *
|
||||||
|
from math import *
|
||||||
|
from random import *
|
||||||
|
from statistics import *
|
||||||
|
from itertools import *
|
||||||
|
from functools import *
|
||||||
|
from operator import *
|
||||||
|
from io import *
|
||||||
|
from sys import *
|
||||||
|
from json import *
|
||||||
|
from builtins import *
|
||||||
|
import string
|
||||||
|
import re
|
||||||
|
import datetime
|
||||||
|
import collections
|
||||||
|
import heapq
|
||||||
|
import bisect
|
||||||
|
import copy
|
||||||
|
import math
|
||||||
|
import random
|
||||||
|
import statistics
|
||||||
|
import itertools
|
||||||
|
import functools
|
||||||
|
import operator
|
||||||
|
import io
|
||||||
|
import sys
|
||||||
|
import json
|
||||||
|
from typing import *
|
||||||
|
|
||||||
|
# @leet start
|
||||||
|
# Definition for singly-linked list.
|
||||||
|
# class ListNode:
|
||||||
|
# def __init__(self, val=0, next=None):
|
||||||
|
# self.val = val
|
||||||
|
# self.next = next
|
||||||
|
class Solution:
|
||||||
|
def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:
|
||||||
|
|
||||||
|
# @leet end
|
||||||
@@ -0,0 +1,66 @@
|
|||||||
|
from string import *
|
||||||
|
from re import *
|
||||||
|
from datetime import *
|
||||||
|
from collections import *
|
||||||
|
from heapq import *
|
||||||
|
from bisect import *
|
||||||
|
from copy import *
|
||||||
|
from math import *
|
||||||
|
from random import *
|
||||||
|
from statistics import *
|
||||||
|
from itertools import *
|
||||||
|
from functools import *
|
||||||
|
from operator import *
|
||||||
|
from io import *
|
||||||
|
from sys import *
|
||||||
|
from json import *
|
||||||
|
from builtins import *
|
||||||
|
import string
|
||||||
|
import re
|
||||||
|
import datetime
|
||||||
|
import collections
|
||||||
|
import heapq
|
||||||
|
import bisect
|
||||||
|
import copy
|
||||||
|
import math
|
||||||
|
import random
|
||||||
|
import statistics
|
||||||
|
import itertools
|
||||||
|
import functools
|
||||||
|
import operator
|
||||||
|
import io
|
||||||
|
import sys
|
||||||
|
import json
|
||||||
|
from typing import *
|
||||||
|
|
||||||
|
# @leet start
|
||||||
|
class Solution:
|
||||||
|
def canBeValid(self, s: str, locked: str) -> bool:
|
||||||
|
var = ''
|
||||||
|
new_str = []
|
||||||
|
for i in s:
|
||||||
|
new_str.append(i)
|
||||||
|
|
||||||
|
for i in range(len(s)):
|
||||||
|
if i % 2 == 0:
|
||||||
|
var = '('
|
||||||
|
if var != s[i] and locked[i] == "0":
|
||||||
|
new_str[i] = var
|
||||||
|
elif i % 2 != 0:
|
||||||
|
var = ')'
|
||||||
|
if var != s[i] and locked[i] == "0":
|
||||||
|
new_str[i] = var
|
||||||
|
|
||||||
|
count_v = 0
|
||||||
|
count_x = 0
|
||||||
|
for i in new_str:
|
||||||
|
if i == ')':
|
||||||
|
count_x += 1
|
||||||
|
if i == '(':
|
||||||
|
count_v += 1
|
||||||
|
|
||||||
|
return count_v % 2 == 0 and count_x % 2 == 0
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# @leet end
|
||||||
46
src/exercises/217.contains-duplicate.py
Normal file
46
src/exercises/217.contains-duplicate.py
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
from string import *
|
||||||
|
from re import *
|
||||||
|
from datetime import *
|
||||||
|
from collections import *
|
||||||
|
from heapq import *
|
||||||
|
from bisect import *
|
||||||
|
from copy import *
|
||||||
|
from math import *
|
||||||
|
from random import *
|
||||||
|
from statistics import *
|
||||||
|
from itertools import *
|
||||||
|
from functools import *
|
||||||
|
from operator import *
|
||||||
|
from io import *
|
||||||
|
from sys import *
|
||||||
|
from json import *
|
||||||
|
from builtins import *
|
||||||
|
import string
|
||||||
|
import re
|
||||||
|
import datetime
|
||||||
|
import collections
|
||||||
|
import heapq
|
||||||
|
import bisect
|
||||||
|
import copy
|
||||||
|
import math
|
||||||
|
import random
|
||||||
|
import statistics
|
||||||
|
import itertools
|
||||||
|
import functools
|
||||||
|
import operator
|
||||||
|
import io
|
||||||
|
import sys
|
||||||
|
import json
|
||||||
|
from typing import *
|
||||||
|
|
||||||
|
# @leet start
|
||||||
|
class Solution:
|
||||||
|
def containsDuplicate(self, nums: List[int]) -> bool:
|
||||||
|
seen = set()
|
||||||
|
for num in nums:
|
||||||
|
if num in seen:
|
||||||
|
return True
|
||||||
|
seen.add(num)
|
||||||
|
return False
|
||||||
|
|
||||||
|
# @leet end
|
||||||
55
src/exercises/219.contains-duplicate-ii.py
Normal file
55
src/exercises/219.contains-duplicate-ii.py
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
# @leet imports start
|
||||||
|
from string import *
|
||||||
|
from re import *
|
||||||
|
from datetime import *
|
||||||
|
from collections import *
|
||||||
|
from heapq import *
|
||||||
|
from bisect import *
|
||||||
|
from copy import *
|
||||||
|
from math import *
|
||||||
|
from random import *
|
||||||
|
from statistics import *
|
||||||
|
from itertools import *
|
||||||
|
from functools import *
|
||||||
|
from operator import *
|
||||||
|
from io import *
|
||||||
|
from sys import *
|
||||||
|
from json import *
|
||||||
|
from builtins import *
|
||||||
|
import string
|
||||||
|
import re
|
||||||
|
import datetime
|
||||||
|
import collections
|
||||||
|
import heapq
|
||||||
|
import bisect
|
||||||
|
import copy
|
||||||
|
import math
|
||||||
|
import random
|
||||||
|
import statistics
|
||||||
|
import itertools
|
||||||
|
import functools
|
||||||
|
import operator
|
||||||
|
import io
|
||||||
|
import sys
|
||||||
|
import json
|
||||||
|
from typing import *
|
||||||
|
|
||||||
|
# @leet imports end
|
||||||
|
|
||||||
|
|
||||||
|
# @leet start
|
||||||
|
class Solution:
|
||||||
|
def containsNearbyDuplicate(self, nums: List[int], k: int) -> bool:
|
||||||
|
if len(nums) <= 1:
|
||||||
|
return False
|
||||||
|
seen = set()
|
||||||
|
for i in range(len(nums)):
|
||||||
|
if nums[i] in seen:
|
||||||
|
return True
|
||||||
|
seen.add(nums[i])
|
||||||
|
if len(seen) > k:
|
||||||
|
seen.remove(nums[i - k])
|
||||||
|
|
||||||
|
return False
|
||||||
|
# @leet end
|
||||||
|
|
||||||
66
src/exercises/22.generate-parentheses.py
Normal file
66
src/exercises/22.generate-parentheses.py
Normal file
@@ -0,0 +1,66 @@
|
|||||||
|
from string import *
|
||||||
|
from re import *
|
||||||
|
from datetime import *
|
||||||
|
from collections import *
|
||||||
|
from heapq import *
|
||||||
|
from bisect import *
|
||||||
|
from copy import *
|
||||||
|
from math import *
|
||||||
|
from random import *
|
||||||
|
from statistics import *
|
||||||
|
from itertools import *
|
||||||
|
from functools import *
|
||||||
|
from operator import *
|
||||||
|
from io import *
|
||||||
|
from sys import *
|
||||||
|
from json import *
|
||||||
|
from builtins import *
|
||||||
|
import string
|
||||||
|
import re
|
||||||
|
import datetime
|
||||||
|
import collections
|
||||||
|
import heapq
|
||||||
|
import bisect
|
||||||
|
import copy
|
||||||
|
import math
|
||||||
|
import random
|
||||||
|
import statistics
|
||||||
|
import itertools
|
||||||
|
import functools
|
||||||
|
import operator
|
||||||
|
import io
|
||||||
|
import sys
|
||||||
|
import json
|
||||||
|
from typing import *
|
||||||
|
|
||||||
|
|
||||||
|
# @leet start
|
||||||
|
class Solution:
|
||||||
|
def generateParenthesis(self, n: int) -> List[str]:
|
||||||
|
if n < 1:
|
||||||
|
return []
|
||||||
|
if n == 1:
|
||||||
|
return ["()"]
|
||||||
|
|
||||||
|
stack = []
|
||||||
|
res = []
|
||||||
|
def retrieveParenthesis(open, close):
|
||||||
|
if open == close == n:
|
||||||
|
res.append("".join(stack))
|
||||||
|
return
|
||||||
|
if open < n:
|
||||||
|
stack.append('(')
|
||||||
|
retrieveParenthesis(open + 1, close)
|
||||||
|
stack.pop()
|
||||||
|
if close < open:
|
||||||
|
stack.append(')')
|
||||||
|
retrieveParenthesis(open, close + 1)
|
||||||
|
stack.pop()
|
||||||
|
|
||||||
|
retrieveParenthesis(0, 0)
|
||||||
|
|
||||||
|
return res
|
||||||
|
|
||||||
|
|
||||||
|
# @leet end
|
||||||
|
|
||||||
64
src/exercises/228.summary-ranges.py
Normal file
64
src/exercises/228.summary-ranges.py
Normal file
@@ -0,0 +1,64 @@
|
|||||||
|
# @leet imports start
|
||||||
|
from string import *
|
||||||
|
from re import *
|
||||||
|
from datetime import *
|
||||||
|
from collections import *
|
||||||
|
from heapq import *
|
||||||
|
from bisect import *
|
||||||
|
from copy import *
|
||||||
|
from math import *
|
||||||
|
from random import *
|
||||||
|
from statistics import *
|
||||||
|
from itertools import *
|
||||||
|
from functools import *
|
||||||
|
from operator import *
|
||||||
|
from io import *
|
||||||
|
from sys import *
|
||||||
|
from json import *
|
||||||
|
from builtins import *
|
||||||
|
import string
|
||||||
|
import re
|
||||||
|
import datetime
|
||||||
|
import collections
|
||||||
|
import heapq
|
||||||
|
import bisect
|
||||||
|
import copy
|
||||||
|
import math
|
||||||
|
import random
|
||||||
|
import statistics
|
||||||
|
import itertools
|
||||||
|
import functools
|
||||||
|
import operator
|
||||||
|
import io
|
||||||
|
import sys
|
||||||
|
import json
|
||||||
|
from typing import *
|
||||||
|
# @leet imports end
|
||||||
|
|
||||||
|
# @leet start
|
||||||
|
class Solution:
|
||||||
|
def summaryRanges(self, nums: List[int]) -> List[str]:
|
||||||
|
if len(nums) < 1:
|
||||||
|
return nums
|
||||||
|
if len(nums) == 1:
|
||||||
|
return [str(nums[-1])]
|
||||||
|
i, j = 0, 0
|
||||||
|
a = []
|
||||||
|
for h in range(1, len(nums), +1):
|
||||||
|
if nums[h - 1] + 1 == nums[h]:
|
||||||
|
j += 1
|
||||||
|
else:
|
||||||
|
if i == j:
|
||||||
|
a.append(str(nums[i]))
|
||||||
|
else:
|
||||||
|
a.append("{}->{}".format(nums[i], nums[j]))
|
||||||
|
i = h
|
||||||
|
j = i
|
||||||
|
|
||||||
|
if i == len(nums) - 1:
|
||||||
|
a.append(str(nums[i]))
|
||||||
|
else:
|
||||||
|
a.append("{}->{}".format(nums[i], nums[-1]))
|
||||||
|
return a
|
||||||
|
|
||||||
|
# @leet end
|
||||||
@@ -0,0 +1,56 @@
|
|||||||
|
from string import *
|
||||||
|
from re import *
|
||||||
|
from datetime import *
|
||||||
|
from collections import *
|
||||||
|
from heapq import *
|
||||||
|
from bisect import *
|
||||||
|
from copy import *
|
||||||
|
from math import *
|
||||||
|
from random import *
|
||||||
|
from statistics import *
|
||||||
|
from itertools import *
|
||||||
|
from functools import *
|
||||||
|
from operator import *
|
||||||
|
from io import *
|
||||||
|
from sys import *
|
||||||
|
from json import *
|
||||||
|
from builtins import *
|
||||||
|
import string
|
||||||
|
import re
|
||||||
|
import datetime
|
||||||
|
import collections
|
||||||
|
import heapq
|
||||||
|
import bisect
|
||||||
|
import copy
|
||||||
|
import math
|
||||||
|
import random
|
||||||
|
import statistics
|
||||||
|
import itertools
|
||||||
|
import functools
|
||||||
|
import operator
|
||||||
|
import io
|
||||||
|
import sys
|
||||||
|
import json
|
||||||
|
from typing import *
|
||||||
|
|
||||||
|
|
||||||
|
# @leet start
|
||||||
|
class Solution:
|
||||||
|
def partitionArray(self, nums: List[int], k: int) -> int:
|
||||||
|
if k == 0:
|
||||||
|
return len(set(nums))
|
||||||
|
nums = sorted(list(set(nums)))
|
||||||
|
subs = 1
|
||||||
|
miv = float(inf)
|
||||||
|
for i in range(len(nums)):
|
||||||
|
miv = nums[i] if miv > nums[i] else miv
|
||||||
|
substraction = nums[i] - miv
|
||||||
|
if substraction > k:
|
||||||
|
subs += 1
|
||||||
|
miv = nums[i]
|
||||||
|
|
||||||
|
return subs
|
||||||
|
|
||||||
|
|
||||||
|
# @leet end
|
||||||
|
|
||||||
54
src/exercises/238.product-of-array-except-self.py
Normal file
54
src/exercises/238.product-of-array-except-self.py
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
from string import *
|
||||||
|
from re import *
|
||||||
|
from datetime import *
|
||||||
|
from collections import *
|
||||||
|
from heapq import *
|
||||||
|
from bisect import *
|
||||||
|
from copy import *
|
||||||
|
from math import *
|
||||||
|
from random import *
|
||||||
|
from statistics import *
|
||||||
|
from itertools import *
|
||||||
|
from functools import *
|
||||||
|
from operator import *
|
||||||
|
from io import *
|
||||||
|
from sys import *
|
||||||
|
from json import *
|
||||||
|
from builtins import *
|
||||||
|
import string
|
||||||
|
import re
|
||||||
|
import datetime
|
||||||
|
import collections
|
||||||
|
import heapq
|
||||||
|
import bisect
|
||||||
|
import copy
|
||||||
|
import math
|
||||||
|
import random
|
||||||
|
import statistics
|
||||||
|
import itertools
|
||||||
|
import functools
|
||||||
|
import operator
|
||||||
|
import io
|
||||||
|
import sys
|
||||||
|
import json
|
||||||
|
from typing import *
|
||||||
|
|
||||||
|
|
||||||
|
# @leet start
|
||||||
|
class Solution:
|
||||||
|
def productExceptSelf(self, nums: List[int]) -> List[int]:
|
||||||
|
arr = [1 for _ in range(len(nums))]
|
||||||
|
left_product = 1
|
||||||
|
for i in range(len(nums)):
|
||||||
|
arr[i] = left_product
|
||||||
|
left_product *= nums[i]
|
||||||
|
right_product = 1
|
||||||
|
for i in range(len(nums) - 1, -1, -1):
|
||||||
|
arr[i] = right_product * arr[i]
|
||||||
|
right_product *= nums[i]
|
||||||
|
|
||||||
|
return arr
|
||||||
|
|
||||||
|
|
||||||
|
# @leet end
|
||||||
|
|
||||||
42
src/exercises/242.valid-anagram.py
Normal file
42
src/exercises/242.valid-anagram.py
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
from string import *
|
||||||
|
from re import *
|
||||||
|
from datetime import *
|
||||||
|
from collections import *
|
||||||
|
from heapq import *
|
||||||
|
from bisect import *
|
||||||
|
from copy import *
|
||||||
|
from math import *
|
||||||
|
from random import *
|
||||||
|
from statistics import *
|
||||||
|
from itertools import *
|
||||||
|
from functools import *
|
||||||
|
from operator import *
|
||||||
|
from io import *
|
||||||
|
from sys import *
|
||||||
|
from json import *
|
||||||
|
from builtins import *
|
||||||
|
import string
|
||||||
|
import re
|
||||||
|
import datetime
|
||||||
|
import collections
|
||||||
|
import heapq
|
||||||
|
import bisect
|
||||||
|
import copy
|
||||||
|
import math
|
||||||
|
import random
|
||||||
|
import statistics
|
||||||
|
import itertools
|
||||||
|
import functools
|
||||||
|
import operator
|
||||||
|
import io
|
||||||
|
import sys
|
||||||
|
import json
|
||||||
|
from typing import *
|
||||||
|
|
||||||
|
# @leet start
|
||||||
|
class Solution:
|
||||||
|
def isAnagram(self, s: str, t: str) -> bool:
|
||||||
|
return Counter(s) == Counter(t)
|
||||||
|
|
||||||
|
|
||||||
|
# @leet end
|
||||||
64
src/exercises/243.shortest-word-distance.py
Normal file
64
src/exercises/243.shortest-word-distance.py
Normal file
@@ -0,0 +1,64 @@
|
|||||||
|
from string import *
|
||||||
|
from re import *
|
||||||
|
from datetime import *
|
||||||
|
from collections import *
|
||||||
|
from heapq import *
|
||||||
|
from bisect import *
|
||||||
|
from copy import *
|
||||||
|
from math import *
|
||||||
|
from random import *
|
||||||
|
from statistics import *
|
||||||
|
from itertools import *
|
||||||
|
from functools import *
|
||||||
|
from operator import *
|
||||||
|
from io import *
|
||||||
|
from sys import *
|
||||||
|
from json import *
|
||||||
|
from builtins import *
|
||||||
|
import string
|
||||||
|
import re
|
||||||
|
import datetime
|
||||||
|
import collections
|
||||||
|
import heapq
|
||||||
|
import bisect
|
||||||
|
import copy
|
||||||
|
import math
|
||||||
|
import random
|
||||||
|
import statistics
|
||||||
|
import itertools
|
||||||
|
import functools
|
||||||
|
import operator
|
||||||
|
import io
|
||||||
|
import sys
|
||||||
|
import json
|
||||||
|
from typing import *
|
||||||
|
|
||||||
|
|
||||||
|
# @leet start
|
||||||
|
class Solution:
|
||||||
|
def shortestDistance(self, wordsDict: List[str], word1: str, word2: str) -> int:
|
||||||
|
word_indices = {}
|
||||||
|
for i, word in enumerate(wordsDict):
|
||||||
|
word_indices.setdefault(word, []).append(i)
|
||||||
|
|
||||||
|
pos1 = word_indices[word1]
|
||||||
|
pos2 = word_indices[word2]
|
||||||
|
|
||||||
|
i, j = 0, 0
|
||||||
|
min_distance = float("inf")
|
||||||
|
|
||||||
|
while i < len(pos1) and j < len(pos2):
|
||||||
|
index1 = pos1[i]
|
||||||
|
index2 = pos2[j]
|
||||||
|
min_distance = min(min_distance, abs(index1 - index2))
|
||||||
|
|
||||||
|
if index1 < index2:
|
||||||
|
i += 1
|
||||||
|
else:
|
||||||
|
j += 1
|
||||||
|
|
||||||
|
return min_distance
|
||||||
|
|
||||||
|
|
||||||
|
# @leet end
|
||||||
|
|
||||||
70
src/exercises/244.shortest-word-distance-ii.py
Normal file
70
src/exercises/244.shortest-word-distance-ii.py
Normal file
@@ -0,0 +1,70 @@
|
|||||||
|
from string import *
|
||||||
|
from re import *
|
||||||
|
from datetime import *
|
||||||
|
from collections import *
|
||||||
|
from heapq import *
|
||||||
|
from bisect import *
|
||||||
|
from copy import *
|
||||||
|
from math import *
|
||||||
|
from random import *
|
||||||
|
from statistics import *
|
||||||
|
from itertools import *
|
||||||
|
from functools import *
|
||||||
|
from operator import *
|
||||||
|
from io import *
|
||||||
|
from sys import *
|
||||||
|
from json import *
|
||||||
|
from builtins import *
|
||||||
|
import string
|
||||||
|
import re
|
||||||
|
import datetime
|
||||||
|
import collections
|
||||||
|
import heapq
|
||||||
|
import bisect
|
||||||
|
import copy
|
||||||
|
import math
|
||||||
|
import random
|
||||||
|
import statistics
|
||||||
|
import itertools
|
||||||
|
import functools
|
||||||
|
import operator
|
||||||
|
import io
|
||||||
|
import sys
|
||||||
|
import json
|
||||||
|
from typing import *
|
||||||
|
|
||||||
|
# @leet start
|
||||||
|
class WordDistance:
|
||||||
|
|
||||||
|
wordsDistance = []
|
||||||
|
def __init__(self, wordsDict: List[str]):
|
||||||
|
self.wordsDistance = wordsDict
|
||||||
|
self.word_indices = {}
|
||||||
|
for i, word in enumerate(wordsDict):
|
||||||
|
self.word_indices.setdefault(word, []).append(i)
|
||||||
|
|
||||||
|
def shortest(self, word1: str, word2: str) -> int:
|
||||||
|
pos1 = self.word_indices[word1]
|
||||||
|
pos2 = self.word_indices[word2]
|
||||||
|
i, j = 0, 0
|
||||||
|
min_distance = float("inf")
|
||||||
|
|
||||||
|
while i < len(pos1) and j < len(pos2):
|
||||||
|
index1 = pos1[i]
|
||||||
|
index2 = pos2[j]
|
||||||
|
min_distance = min(min_distance, abs(index1 - index2))
|
||||||
|
|
||||||
|
if index1 < index2:
|
||||||
|
i += 1
|
||||||
|
else:
|
||||||
|
j += 1
|
||||||
|
|
||||||
|
return min_distance
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# Your WordDistance object will be instantiated and called as such:
|
||||||
|
# obj = WordDistance(wordsDict)
|
||||||
|
# param_1 = obj.shortest(word1,word2)
|
||||||
|
# @leet end
|
||||||
73
src/exercises/245.shortest-word-distance-iii.py
Normal file
73
src/exercises/245.shortest-word-distance-iii.py
Normal file
@@ -0,0 +1,73 @@
|
|||||||
|
from string import *
|
||||||
|
from re import *
|
||||||
|
from datetime import *
|
||||||
|
from collections import *
|
||||||
|
from heapq import *
|
||||||
|
from bisect import *
|
||||||
|
from copy import *
|
||||||
|
from math import *
|
||||||
|
from random import *
|
||||||
|
from statistics import *
|
||||||
|
from itertools import *
|
||||||
|
from functools import *
|
||||||
|
from operator import *
|
||||||
|
from io import *
|
||||||
|
from sys import *
|
||||||
|
from json import *
|
||||||
|
from builtins import *
|
||||||
|
import string
|
||||||
|
import re
|
||||||
|
import datetime
|
||||||
|
import collections
|
||||||
|
import heapq
|
||||||
|
import bisect
|
||||||
|
import copy
|
||||||
|
import math
|
||||||
|
import random
|
||||||
|
import statistics
|
||||||
|
import itertools
|
||||||
|
import functools
|
||||||
|
import operator
|
||||||
|
import io
|
||||||
|
import sys
|
||||||
|
import json
|
||||||
|
from typing import *
|
||||||
|
|
||||||
|
|
||||||
|
# @leet start
|
||||||
|
class Solution:
|
||||||
|
def shortestWordDistance(self, wordsDict: List[str], word1: str, word2: str) -> int:
|
||||||
|
word_indices = {}
|
||||||
|
for i, word in enumerate(wordsDict):
|
||||||
|
word_indices.setdefault(word, []).append(i)
|
||||||
|
|
||||||
|
pos1 = word_indices[word1]
|
||||||
|
pos2 = word_indices[word2]
|
||||||
|
|
||||||
|
# Two-pointer traversal
|
||||||
|
i, j = 0, 0
|
||||||
|
min_distance = float("inf")
|
||||||
|
|
||||||
|
if word1 == word2:
|
||||||
|
for i in range(len(pos1)):
|
||||||
|
for j in range(len(pos2)):
|
||||||
|
if i != j:
|
||||||
|
min_distance = min(min_distance, abs(pos2[i] - pos2[j]))
|
||||||
|
return min_distance
|
||||||
|
|
||||||
|
while i < len(pos1) and j < len(pos2):
|
||||||
|
index1 = pos1[i]
|
||||||
|
index2 = pos2[j]
|
||||||
|
min_distance = min(min_distance, abs(index1 - index2))
|
||||||
|
|
||||||
|
# Move the pointer pointing to the smaller index
|
||||||
|
if index1 < index2:
|
||||||
|
i += 1
|
||||||
|
else:
|
||||||
|
j += 1
|
||||||
|
|
||||||
|
return min_distance
|
||||||
|
|
||||||
|
|
||||||
|
# @leet end
|
||||||
|
|
||||||
55
src/exercises/246.strobogrammatic-number.py
Normal file
55
src/exercises/246.strobogrammatic-number.py
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
from string import *
|
||||||
|
from re import *
|
||||||
|
from datetime import *
|
||||||
|
from collections import *
|
||||||
|
from heapq import *
|
||||||
|
from bisect import *
|
||||||
|
from copy import *
|
||||||
|
from math import *
|
||||||
|
from random import *
|
||||||
|
from statistics import *
|
||||||
|
from itertools import *
|
||||||
|
from functools import *
|
||||||
|
from operator import *
|
||||||
|
from io import *
|
||||||
|
from sys import *
|
||||||
|
from json import *
|
||||||
|
from builtins import *
|
||||||
|
import string
|
||||||
|
import re
|
||||||
|
import datetime
|
||||||
|
import collections
|
||||||
|
import heapq
|
||||||
|
import bisect
|
||||||
|
import copy
|
||||||
|
import math
|
||||||
|
import random
|
||||||
|
import statistics
|
||||||
|
import itertools
|
||||||
|
import functools
|
||||||
|
import operator
|
||||||
|
import io
|
||||||
|
import sys
|
||||||
|
import json
|
||||||
|
from typing import *
|
||||||
|
|
||||||
|
|
||||||
|
# @leet start
|
||||||
|
class Solution:
|
||||||
|
def isStrobogrammatic(self, num: str) -> bool:
|
||||||
|
mapping = {"0": "0", "1": "1", "6": "9", "8": "8", "9": "6"}
|
||||||
|
left, right = 0, len(num) - 1
|
||||||
|
|
||||||
|
while left <= right:
|
||||||
|
if num[left] not in mapping or num[right] not in mapping:
|
||||||
|
return False
|
||||||
|
if mapping[num[left]] != num[right]:
|
||||||
|
return False
|
||||||
|
left += 1
|
||||||
|
right -= 1
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
# @leet end
|
||||||
|
|
||||||
65
src/exercises/248.strobogrammatic-number-iii.py
Normal file
65
src/exercises/248.strobogrammatic-number-iii.py
Normal file
@@ -0,0 +1,65 @@
|
|||||||
|
from string import *
|
||||||
|
from re import *
|
||||||
|
from datetime import *
|
||||||
|
from collections import *
|
||||||
|
from heapq import *
|
||||||
|
from bisect import *
|
||||||
|
from copy import *
|
||||||
|
from math import *
|
||||||
|
from random import *
|
||||||
|
from statistics import *
|
||||||
|
from itertools import *
|
||||||
|
from functools import *
|
||||||
|
from operator import *
|
||||||
|
from io import *
|
||||||
|
from sys import *
|
||||||
|
from json import *
|
||||||
|
from builtins import *
|
||||||
|
import string
|
||||||
|
import re
|
||||||
|
import datetime
|
||||||
|
import collections
|
||||||
|
import heapq
|
||||||
|
import bisect
|
||||||
|
import copy
|
||||||
|
import math
|
||||||
|
import random
|
||||||
|
import statistics
|
||||||
|
import itertools
|
||||||
|
import functools
|
||||||
|
import operator
|
||||||
|
import io
|
||||||
|
import sys
|
||||||
|
import json
|
||||||
|
from typing import *
|
||||||
|
|
||||||
|
|
||||||
|
# @leet start
|
||||||
|
class Solution:
|
||||||
|
def strobogrammaticInRange(self, low: str, high: str) -> int:
|
||||||
|
def gen_str(l):
|
||||||
|
if l == 0:
|
||||||
|
return [""]
|
||||||
|
if l == 1:
|
||||||
|
return ["0", "1", "8"]
|
||||||
|
sub_ans = []
|
||||||
|
for n in gen_str(l - 2):
|
||||||
|
for pair in ("11", "88", "69", "96"):
|
||||||
|
sub_ans.append(pair[0] + n + pair[1])
|
||||||
|
if l != num_l:
|
||||||
|
sub_ans.append("0" + n + "0")
|
||||||
|
return sub_ans
|
||||||
|
|
||||||
|
min_l, max_l = len(low), len(high)
|
||||||
|
low, high = int(low), int(high)
|
||||||
|
count = 0
|
||||||
|
|
||||||
|
for num_l in range(min_l, max_l + 1):
|
||||||
|
for num_str in gen_str(num_l):
|
||||||
|
if low <= int(num_str) <= high:
|
||||||
|
count += 1
|
||||||
|
return count
|
||||||
|
|
||||||
|
|
||||||
|
# @leet end
|
||||||
|
|
||||||
@@ -0,0 +1,41 @@
|
|||||||
|
from string import *
|
||||||
|
from re import *
|
||||||
|
from datetime import *
|
||||||
|
from collections import *
|
||||||
|
from heapq import *
|
||||||
|
from bisect import *
|
||||||
|
from copy import *
|
||||||
|
from math import *
|
||||||
|
from random import *
|
||||||
|
from statistics import *
|
||||||
|
from itertools import *
|
||||||
|
from functools import *
|
||||||
|
from operator import *
|
||||||
|
from io import *
|
||||||
|
from sys import *
|
||||||
|
from json import *
|
||||||
|
from builtins import *
|
||||||
|
import string
|
||||||
|
import re
|
||||||
|
import datetime
|
||||||
|
import collections
|
||||||
|
import heapq
|
||||||
|
import bisect
|
||||||
|
import copy
|
||||||
|
import math
|
||||||
|
import random
|
||||||
|
import statistics
|
||||||
|
import itertools
|
||||||
|
import functools
|
||||||
|
import operator
|
||||||
|
import io
|
||||||
|
import sys
|
||||||
|
import json
|
||||||
|
from typing import *
|
||||||
|
|
||||||
|
# @leet start
|
||||||
|
class Solution:
|
||||||
|
def minimizeMax(self, nums: List[int], p: int) -> int:
|
||||||
|
nums.sort()
|
||||||
|
|
||||||
|
# @leet end
|
||||||
@@ -0,0 +1,53 @@
|
|||||||
|
# @leet imports start
|
||||||
|
from string import *
|
||||||
|
from re import *
|
||||||
|
from datetime import *
|
||||||
|
from collections import *
|
||||||
|
from heapq import *
|
||||||
|
from bisect import *
|
||||||
|
from copy import *
|
||||||
|
from math import *
|
||||||
|
from random import *
|
||||||
|
from statistics import *
|
||||||
|
from itertools import *
|
||||||
|
from functools import *
|
||||||
|
from operator import *
|
||||||
|
from io import *
|
||||||
|
from sys import *
|
||||||
|
from json import *
|
||||||
|
from builtins import *
|
||||||
|
import string
|
||||||
|
import re
|
||||||
|
import datetime
|
||||||
|
import collections
|
||||||
|
import heapq
|
||||||
|
import bisect
|
||||||
|
import copy
|
||||||
|
import math
|
||||||
|
import random
|
||||||
|
import statistics
|
||||||
|
import itertools
|
||||||
|
import functools
|
||||||
|
import operator
|
||||||
|
import io
|
||||||
|
import sys
|
||||||
|
import json
|
||||||
|
from typing import *
|
||||||
|
|
||||||
|
# @leet imports end
|
||||||
|
|
||||||
|
|
||||||
|
# @leet start
|
||||||
|
class Solution:
|
||||||
|
def strStr(self, haystack: str, needle: str) -> int:
|
||||||
|
for i in range(len(haystack)):
|
||||||
|
if i + len(needle) > len(haystack):
|
||||||
|
return -1
|
||||||
|
if haystack[i : i + len(needle)] == needle:
|
||||||
|
return i
|
||||||
|
|
||||||
|
return -1
|
||||||
|
|
||||||
|
|
||||||
|
# @leet end;
|
||||||
|
|
||||||
62
src/exercises/290.word-pattern.py
Normal file
62
src/exercises/290.word-pattern.py
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
# @leet imports start
|
||||||
|
from string import *
|
||||||
|
from re import *
|
||||||
|
from datetime import *
|
||||||
|
from collections import *
|
||||||
|
from heapq import *
|
||||||
|
from bisect import *
|
||||||
|
from copy import *
|
||||||
|
from math import *
|
||||||
|
from random import *
|
||||||
|
from statistics import *
|
||||||
|
from itertools import *
|
||||||
|
from functools import *
|
||||||
|
from operator import *
|
||||||
|
from io import *
|
||||||
|
from sys import *
|
||||||
|
from json import *
|
||||||
|
from builtins import *
|
||||||
|
import string
|
||||||
|
import re
|
||||||
|
import datetime
|
||||||
|
import collections
|
||||||
|
import heapq
|
||||||
|
import bisect
|
||||||
|
import copy
|
||||||
|
import math
|
||||||
|
import random
|
||||||
|
import statistics
|
||||||
|
import itertools
|
||||||
|
import functools
|
||||||
|
import operator
|
||||||
|
import io
|
||||||
|
import sys
|
||||||
|
import json
|
||||||
|
from typing import *
|
||||||
|
|
||||||
|
# @leet imports end
|
||||||
|
|
||||||
|
|
||||||
|
# @leet start
|
||||||
|
class Solution:
|
||||||
|
def wordPattern(self, pattern: str, s: str) -> bool:
|
||||||
|
w = {}
|
||||||
|
ss = s.split(" ")
|
||||||
|
if len(pattern) != len(ss):
|
||||||
|
return False
|
||||||
|
|
||||||
|
for i in range(len(ss)):
|
||||||
|
if pattern[i] not in w and ss[i] not in w.values():
|
||||||
|
w[pattern[i]] = ss[i]
|
||||||
|
for i in range(len(ss)):
|
||||||
|
if pattern[i] not in w:
|
||||||
|
return False
|
||||||
|
if ss[i] != w[pattern[i]]:
|
||||||
|
return False
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# @leet end
|
||||||
|
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
from string import *
|
||||||
|
from re import *
|
||||||
|
from datetime import *
|
||||||
|
from collections import *
|
||||||
|
from heapq import *
|
||||||
|
from bisect import *
|
||||||
|
from copy import *
|
||||||
|
from math import *
|
||||||
|
from random import *
|
||||||
|
from statistics import *
|
||||||
|
from itertools import *
|
||||||
|
from functools import *
|
||||||
|
from operator import *
|
||||||
|
from io import *
|
||||||
|
from sys import *
|
||||||
|
from json import *
|
||||||
|
from builtins import *
|
||||||
|
import string
|
||||||
|
import re
|
||||||
|
import datetime
|
||||||
|
import collections
|
||||||
|
import heapq
|
||||||
|
import bisect
|
||||||
|
import copy
|
||||||
|
import math
|
||||||
|
import random
|
||||||
|
import statistics
|
||||||
|
import itertools
|
||||||
|
import functools
|
||||||
|
import operator
|
||||||
|
import io
|
||||||
|
import sys
|
||||||
|
import json
|
||||||
|
from typing import *
|
||||||
|
|
||||||
|
# @leet start
|
||||||
|
class Solution:
|
||||||
|
def divideArray(self, nums: List[int], k: int) -> List[List[int]]:
|
||||||
|
|
||||||
|
# @leet end
|
||||||
@@ -0,0 +1,58 @@
|
|||||||
|
from string import *
|
||||||
|
from re import *
|
||||||
|
from datetime import *
|
||||||
|
from collections import *
|
||||||
|
from heapq import *
|
||||||
|
from bisect import *
|
||||||
|
from copy import *
|
||||||
|
from math import *
|
||||||
|
from random import *
|
||||||
|
from statistics import *
|
||||||
|
from itertools import *
|
||||||
|
from functools import *
|
||||||
|
from operator import *
|
||||||
|
from io import *
|
||||||
|
from sys import *
|
||||||
|
from json import *
|
||||||
|
from builtins import *
|
||||||
|
import string
|
||||||
|
import re
|
||||||
|
import datetime
|
||||||
|
import collections
|
||||||
|
import heapq
|
||||||
|
import bisect
|
||||||
|
import copy
|
||||||
|
import math
|
||||||
|
import random
|
||||||
|
import statistics
|
||||||
|
import itertools
|
||||||
|
import functools
|
||||||
|
import operator
|
||||||
|
import io
|
||||||
|
import sys
|
||||||
|
import json
|
||||||
|
from typing import *
|
||||||
|
|
||||||
|
|
||||||
|
# @leet start
|
||||||
|
class Solution:
|
||||||
|
def lengthOfLongestSubstring(self, s: str) -> int:
|
||||||
|
i, j = 0, 0
|
||||||
|
window = {}
|
||||||
|
max_char = 0
|
||||||
|
while j < len(s):
|
||||||
|
if s[j] not in window:
|
||||||
|
window[s[j]] = 1
|
||||||
|
max_char = (
|
||||||
|
len(window.keys()) if max_char < len(window.keys()) else max_char
|
||||||
|
)
|
||||||
|
j += 1
|
||||||
|
elif s[j] in window:
|
||||||
|
del window[s[i]]
|
||||||
|
i += 1
|
||||||
|
|
||||||
|
return max_char
|
||||||
|
|
||||||
|
|
||||||
|
# @leet end
|
||||||
|
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
// @leet start
|
||||||
|
function lengthOfLongestSubstring(s: string): number {
|
||||||
|
|
||||||
|
};
|
||||||
|
// @leet end
|
||||||
@@ -0,0 +1,54 @@
|
|||||||
|
from string import *
|
||||||
|
from re import *
|
||||||
|
from datetime import *
|
||||||
|
from collections import *
|
||||||
|
from heapq import *
|
||||||
|
from bisect import *
|
||||||
|
from copy import *
|
||||||
|
from math import *
|
||||||
|
from random import *
|
||||||
|
from statistics import *
|
||||||
|
from itertools import *
|
||||||
|
from functools import *
|
||||||
|
from operator import *
|
||||||
|
from io import *
|
||||||
|
from sys import *
|
||||||
|
from json import *
|
||||||
|
from builtins import *
|
||||||
|
import string
|
||||||
|
import re
|
||||||
|
import datetime
|
||||||
|
import collections
|
||||||
|
import heapq
|
||||||
|
import bisect
|
||||||
|
import copy
|
||||||
|
import math
|
||||||
|
import random
|
||||||
|
import statistics
|
||||||
|
import itertools
|
||||||
|
import functools
|
||||||
|
import operator
|
||||||
|
import io
|
||||||
|
import sys
|
||||||
|
import json
|
||||||
|
from typing import *
|
||||||
|
|
||||||
|
|
||||||
|
# @leet start
|
||||||
|
class Solution:
|
||||||
|
def maxDifference(self, s: str) -> int:
|
||||||
|
record = Counter(s)
|
||||||
|
max_odd = 0
|
||||||
|
min_even = float(inf)
|
||||||
|
|
||||||
|
for i in record.values():
|
||||||
|
if i % 2 == 0:
|
||||||
|
min_even = min(min_even, i)
|
||||||
|
elif i % 2 != 0:
|
||||||
|
max_odd = max(max_odd, i)
|
||||||
|
|
||||||
|
return max_odd - min_even
|
||||||
|
|
||||||
|
|
||||||
|
# @leet end
|
||||||
|
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
from string import *
|
||||||
|
from re import *
|
||||||
|
from datetime import *
|
||||||
|
from collections import *
|
||||||
|
from heapq import *
|
||||||
|
from bisect import *
|
||||||
|
from copy import *
|
||||||
|
from math import *
|
||||||
|
from random import *
|
||||||
|
from statistics import *
|
||||||
|
from itertools import *
|
||||||
|
from functools import *
|
||||||
|
from operator import *
|
||||||
|
from io import *
|
||||||
|
from sys import *
|
||||||
|
from json import *
|
||||||
|
from builtins import *
|
||||||
|
import string
|
||||||
|
import re
|
||||||
|
import datetime
|
||||||
|
import collections
|
||||||
|
import heapq
|
||||||
|
import bisect
|
||||||
|
import copy
|
||||||
|
import math
|
||||||
|
import random
|
||||||
|
import statistics
|
||||||
|
import itertools
|
||||||
|
import functools
|
||||||
|
import operator
|
||||||
|
import io
|
||||||
|
import sys
|
||||||
|
import json
|
||||||
|
from typing import *
|
||||||
|
|
||||||
|
# @leet start
|
||||||
|
class Solution:
|
||||||
|
def maxDifference(self, s: str, k: int) -> int:
|
||||||
|
|
||||||
|
# @leet end
|
||||||
55
src/exercises/35.search-insert-position.py
Normal file
55
src/exercises/35.search-insert-position.py
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
# @leet imports start
|
||||||
|
from string import *
|
||||||
|
from re import *
|
||||||
|
from datetime import *
|
||||||
|
from collections import *
|
||||||
|
from heapq import *
|
||||||
|
from bisect import *
|
||||||
|
from copy import *
|
||||||
|
from math import *
|
||||||
|
from random import *
|
||||||
|
from statistics import *
|
||||||
|
from itertools import *
|
||||||
|
from functools import *
|
||||||
|
from operator import *
|
||||||
|
from io import *
|
||||||
|
from sys import *
|
||||||
|
from json import *
|
||||||
|
from builtins import *
|
||||||
|
import string
|
||||||
|
import re
|
||||||
|
import datetime
|
||||||
|
import collections
|
||||||
|
import heapq
|
||||||
|
import bisect
|
||||||
|
import copy
|
||||||
|
import math
|
||||||
|
import random
|
||||||
|
import statistics
|
||||||
|
import itertools
|
||||||
|
import functools
|
||||||
|
import operator
|
||||||
|
import io
|
||||||
|
import sys
|
||||||
|
import json
|
||||||
|
from typing import *
|
||||||
|
# @leet imports end
|
||||||
|
|
||||||
|
# @leet start
|
||||||
|
class Solution:
|
||||||
|
def searchInsert(self, nums: List[int], target: int) -> int:
|
||||||
|
if nums[-1] < target:
|
||||||
|
return len(nums)
|
||||||
|
mn = 0
|
||||||
|
mx = len(nums) - 1
|
||||||
|
|
||||||
|
while mn < mx:
|
||||||
|
mid = (mx + mn) // 2
|
||||||
|
if target > nums[mid]:
|
||||||
|
mn = mid + 1
|
||||||
|
else:
|
||||||
|
mx = mid
|
||||||
|
|
||||||
|
return mn
|
||||||
|
|
||||||
|
# @leet end
|
||||||
98
src/exercises/36.valid-sudoku.py
Normal file
98
src/exercises/36.valid-sudoku.py
Normal file
@@ -0,0 +1,98 @@
|
|||||||
|
from string import *
|
||||||
|
from re import *
|
||||||
|
from datetime import *
|
||||||
|
from collections import *
|
||||||
|
from heapq import *
|
||||||
|
from bisect import *
|
||||||
|
from copy import *
|
||||||
|
from math import *
|
||||||
|
from random import *
|
||||||
|
from statistics import *
|
||||||
|
from itertools import *
|
||||||
|
from functools import *
|
||||||
|
from operator import *
|
||||||
|
from io import *
|
||||||
|
from sys import *
|
||||||
|
from json import *
|
||||||
|
from builtins import *
|
||||||
|
import string
|
||||||
|
import re
|
||||||
|
import datetime
|
||||||
|
import collections
|
||||||
|
import heapq
|
||||||
|
import bisect
|
||||||
|
import copy
|
||||||
|
import math
|
||||||
|
import random
|
||||||
|
import statistics
|
||||||
|
import itertools
|
||||||
|
import functools
|
||||||
|
import operator
|
||||||
|
import io
|
||||||
|
import sys
|
||||||
|
import json
|
||||||
|
from typing import *
|
||||||
|
|
||||||
|
|
||||||
|
# @leet start
|
||||||
|
class Solution:
|
||||||
|
def isValidSudoku(self, board: List[List[str]]) -> bool:
|
||||||
|
rows = [{} for _ in range(len(board))]
|
||||||
|
square = [{} for _ in range(len(board))]
|
||||||
|
for i in range(len(board)):
|
||||||
|
cols = {}
|
||||||
|
for j in range(len(board[i])):
|
||||||
|
val = board[i][j]
|
||||||
|
if val != ".":
|
||||||
|
if val in cols:
|
||||||
|
return False
|
||||||
|
cols[val] = j
|
||||||
|
if val in rows[j]:
|
||||||
|
return False
|
||||||
|
rows[j][val] = i
|
||||||
|
if i < 3:
|
||||||
|
if j < 3:
|
||||||
|
if val in square[0]:
|
||||||
|
return False
|
||||||
|
square[0][val] = i
|
||||||
|
elif j > 2 and j < 6:
|
||||||
|
if val in square[1]:
|
||||||
|
return False
|
||||||
|
square[1][val] = i
|
||||||
|
elif j > 5:
|
||||||
|
if val in square[2]:
|
||||||
|
return False
|
||||||
|
square[2][val] = i
|
||||||
|
elif i > 2 and i < 6:
|
||||||
|
if j < 3:
|
||||||
|
if val in square[3]:
|
||||||
|
return False
|
||||||
|
square[3][val] = i
|
||||||
|
elif j > 2 and j < 6:
|
||||||
|
if val in square[4]:
|
||||||
|
return False
|
||||||
|
square[4][val] = i
|
||||||
|
elif j > 5:
|
||||||
|
if val in square[5]:
|
||||||
|
return False
|
||||||
|
square[5][val] = i
|
||||||
|
elif i > 5:
|
||||||
|
if j < 3:
|
||||||
|
if val in square[6]:
|
||||||
|
return False
|
||||||
|
square[6][val] = i
|
||||||
|
elif j > 2 and j < 6:
|
||||||
|
if val in square[7]:
|
||||||
|
return False
|
||||||
|
square[7][val] = i
|
||||||
|
elif j > 5:
|
||||||
|
if val in square[8]:
|
||||||
|
return False
|
||||||
|
square[8][val] = i
|
||||||
|
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
# @leet end
|
||||||
|
|
||||||
66
src/exercises/383.ransom-note.py
Normal file
66
src/exercises/383.ransom-note.py
Normal file
@@ -0,0 +1,66 @@
|
|||||||
|
# @leet imports start
|
||||||
|
from string import *
|
||||||
|
from re import *
|
||||||
|
from datetime import *
|
||||||
|
from collections import *
|
||||||
|
from heapq import *
|
||||||
|
from bisect import *
|
||||||
|
from copy import *
|
||||||
|
from math import *
|
||||||
|
from random import *
|
||||||
|
from statistics import *
|
||||||
|
from itertools import *
|
||||||
|
from functools import *
|
||||||
|
from operator import *
|
||||||
|
from io import *
|
||||||
|
from sys import *
|
||||||
|
from json import *
|
||||||
|
from builtins import *
|
||||||
|
import string
|
||||||
|
import re
|
||||||
|
import datetime
|
||||||
|
import collections
|
||||||
|
import heapq
|
||||||
|
import bisect
|
||||||
|
import copy
|
||||||
|
import math
|
||||||
|
import random
|
||||||
|
import statistics
|
||||||
|
import itertools
|
||||||
|
import functools
|
||||||
|
import operator
|
||||||
|
import io
|
||||||
|
import sys
|
||||||
|
import json
|
||||||
|
from typing import *
|
||||||
|
|
||||||
|
# @leet imports end
|
||||||
|
|
||||||
|
|
||||||
|
# @leet start
|
||||||
|
class Solution:
|
||||||
|
def canConstruct(self, ransomNote: str, magazine: str) -> bool:
|
||||||
|
def option1():
|
||||||
|
d = {}
|
||||||
|
for s in ransomNote:
|
||||||
|
if s not in d:
|
||||||
|
d[s] = 1
|
||||||
|
else:
|
||||||
|
d[s] += 1
|
||||||
|
for m in magazine:
|
||||||
|
if m in d:
|
||||||
|
d[m] -= 1
|
||||||
|
if d[m] == 0:
|
||||||
|
del d[m]
|
||||||
|
return len(d.keys()) == 0
|
||||||
|
|
||||||
|
def option2():
|
||||||
|
r = Counter(ransomNote)
|
||||||
|
m = Counter(magazine)
|
||||||
|
return not(r - m)
|
||||||
|
|
||||||
|
return option2()
|
||||||
|
|
||||||
|
|
||||||
|
# @leet end
|
||||||
|
|
||||||
54
src/exercises/392.is-subsequence.py
Normal file
54
src/exercises/392.is-subsequence.py
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
# @leet imports start
|
||||||
|
from string import *
|
||||||
|
from re import *
|
||||||
|
from datetime import *
|
||||||
|
from collections import *
|
||||||
|
from heapq import *
|
||||||
|
from bisect import *
|
||||||
|
from copy import *
|
||||||
|
from math import *
|
||||||
|
from random import *
|
||||||
|
from statistics import *
|
||||||
|
from itertools import *
|
||||||
|
from functools import *
|
||||||
|
from operator import *
|
||||||
|
from io import *
|
||||||
|
from sys import *
|
||||||
|
from json import *
|
||||||
|
from builtins import *
|
||||||
|
import string
|
||||||
|
import re
|
||||||
|
import datetime
|
||||||
|
import collections
|
||||||
|
import heapq
|
||||||
|
import bisect
|
||||||
|
import copy
|
||||||
|
import math
|
||||||
|
import random
|
||||||
|
import statistics
|
||||||
|
import itertools
|
||||||
|
import functools
|
||||||
|
import operator
|
||||||
|
import io
|
||||||
|
import sys
|
||||||
|
import json
|
||||||
|
from typing import *
|
||||||
|
|
||||||
|
# @leet imports end
|
||||||
|
|
||||||
|
|
||||||
|
# @leet start
|
||||||
|
class Solution:
|
||||||
|
def isSubsequence(self, s: str, t: str) -> bool:
|
||||||
|
i, j = 0, 0
|
||||||
|
while j < len(s):
|
||||||
|
if i >= len(t):
|
||||||
|
break
|
||||||
|
if t[i] == s[j]:
|
||||||
|
j += 1
|
||||||
|
i += 1
|
||||||
|
|
||||||
|
return j == len(s)
|
||||||
|
|
||||||
|
|
||||||
|
# @leet end
|
||||||
30
src/exercises/4.median-of-two-sorted-arrays.py
Normal file
30
src/exercises/4.median-of-two-sorted-arrays.py
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
class Solution:
|
||||||
|
def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float:
|
||||||
|
N1, N2 = nums1, nums2
|
||||||
|
lt = len(N1) + len(N2)
|
||||||
|
half = lt // 2
|
||||||
|
|
||||||
|
if len(N1) > len(N2):
|
||||||
|
N1, N2 = N2, N1
|
||||||
|
|
||||||
|
l = 0
|
||||||
|
h = len(N1) - 1
|
||||||
|
while True:
|
||||||
|
midA = (l + h) // 2
|
||||||
|
midB = half - midA - 2
|
||||||
|
|
||||||
|
aLeft = N1[midA] if midA >= 0 else float(-inf)
|
||||||
|
aRight = N1[midA + 1] if (midA + 1) < len(N1) else float(inf)
|
||||||
|
|
||||||
|
bLeft = N2[midB] if midB >= 0 else float(-inf)
|
||||||
|
bRight = N2[midB + 1] if (midB + 1) < len(N2) else float(inf)
|
||||||
|
|
||||||
|
if aLeft <= bRight and bLeft <= aRight:
|
||||||
|
if lt % 2 == 0:
|
||||||
|
return (min(bRight, aRight) + max(bLeft, aLeft)) / 2
|
||||||
|
else:
|
||||||
|
return min(bRight, aRight)
|
||||||
|
if aLeft > bRight:
|
||||||
|
h = midA - 1
|
||||||
|
else:
|
||||||
|
l = midA + 1
|
||||||
60
src/exercises/42.trapping-rain-water.py
Normal file
60
src/exercises/42.trapping-rain-water.py
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
from string import *
|
||||||
|
from re import *
|
||||||
|
from datetime import *
|
||||||
|
from collections import *
|
||||||
|
from heapq import *
|
||||||
|
from bisect import *
|
||||||
|
from copy import *
|
||||||
|
from math import *
|
||||||
|
from random import *
|
||||||
|
from statistics import *
|
||||||
|
from itertools import *
|
||||||
|
from functools import *
|
||||||
|
from operator import *
|
||||||
|
from io import *
|
||||||
|
from sys import *
|
||||||
|
from json import *
|
||||||
|
from builtins import *
|
||||||
|
import string
|
||||||
|
import re
|
||||||
|
import datetime
|
||||||
|
import collections
|
||||||
|
import heapq
|
||||||
|
import bisect
|
||||||
|
import copy
|
||||||
|
import math
|
||||||
|
import random
|
||||||
|
import statistics
|
||||||
|
import itertools
|
||||||
|
import functools
|
||||||
|
import operator
|
||||||
|
import io
|
||||||
|
import sys
|
||||||
|
import json
|
||||||
|
from typing import *
|
||||||
|
|
||||||
|
|
||||||
|
# @leet start
|
||||||
|
class Solution:
|
||||||
|
def trap(self, height: List[int]) -> int:
|
||||||
|
i, j = 0, len(height) - 1
|
||||||
|
c, m_l, m_r = 0, 0, 0
|
||||||
|
while i < j:
|
||||||
|
if height[i] < height[j]:
|
||||||
|
if m_l < height[i]:
|
||||||
|
m_l = height[i]
|
||||||
|
if (m_l - height[i]) > 0:
|
||||||
|
c += m_l - height[i]
|
||||||
|
i += 1
|
||||||
|
else:
|
||||||
|
if m_r < height[j]:
|
||||||
|
m_r = height[j]
|
||||||
|
if (m_r - height[j]) > 0:
|
||||||
|
c += m_r - height[j]
|
||||||
|
j -= 1
|
||||||
|
|
||||||
|
return c
|
||||||
|
|
||||||
|
|
||||||
|
# @leet end
|
||||||
|
|
||||||
42
src/exercises/45.jump-game-ii.py
Normal file
42
src/exercises/45.jump-game-ii.py
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
# @leet imports start
|
||||||
|
from string import *
|
||||||
|
from re import *
|
||||||
|
from datetime import *
|
||||||
|
from collections import *
|
||||||
|
from heapq import *
|
||||||
|
from bisect import *
|
||||||
|
from copy import *
|
||||||
|
from math import *
|
||||||
|
from random import *
|
||||||
|
from statistics import *
|
||||||
|
from itertools import *
|
||||||
|
from functools import *
|
||||||
|
from operator import *
|
||||||
|
from io import *
|
||||||
|
from sys import *
|
||||||
|
from json import *
|
||||||
|
from builtins import *
|
||||||
|
import string
|
||||||
|
import re
|
||||||
|
import datetime
|
||||||
|
import collections
|
||||||
|
import heapq
|
||||||
|
import bisect
|
||||||
|
import copy
|
||||||
|
import math
|
||||||
|
import random
|
||||||
|
import statistics
|
||||||
|
import itertools
|
||||||
|
import functools
|
||||||
|
import operator
|
||||||
|
import io
|
||||||
|
import sys
|
||||||
|
import json
|
||||||
|
from typing import *
|
||||||
|
# @leet imports end
|
||||||
|
|
||||||
|
# @leet start
|
||||||
|
class Solution:
|
||||||
|
def jump(self, nums: List[int]) -> int:
|
||||||
|
|
||||||
|
# @leet end
|
||||||
66
src/exercises/5.longest-palindromic-substring.py
Normal file
66
src/exercises/5.longest-palindromic-substring.py
Normal file
@@ -0,0 +1,66 @@
|
|||||||
|
# @leet imports start
|
||||||
|
from string import *
|
||||||
|
from re import *
|
||||||
|
from datetime import *
|
||||||
|
from collections import *
|
||||||
|
from heapq import *
|
||||||
|
from bisect import *
|
||||||
|
from copy import *
|
||||||
|
from math import *
|
||||||
|
from random import *
|
||||||
|
from statistics import *
|
||||||
|
from itertools import *
|
||||||
|
from functools import *
|
||||||
|
from operator import *
|
||||||
|
from io import *
|
||||||
|
from sys import *
|
||||||
|
from json import *
|
||||||
|
from builtins import *
|
||||||
|
import string
|
||||||
|
import re
|
||||||
|
import datetime
|
||||||
|
import collections
|
||||||
|
import heapq
|
||||||
|
import bisect
|
||||||
|
import copy
|
||||||
|
import math
|
||||||
|
import random
|
||||||
|
import statistics
|
||||||
|
import itertools
|
||||||
|
import functools
|
||||||
|
import operator
|
||||||
|
import io
|
||||||
|
import sys
|
||||||
|
import json
|
||||||
|
from typing import *
|
||||||
|
|
||||||
|
# @leet imports end
|
||||||
|
|
||||||
|
|
||||||
|
# @leet start
|
||||||
|
class Solution:
|
||||||
|
def longestPalindrome(self, s: str) -> str:
|
||||||
|
if len(s) <= 1:
|
||||||
|
return s
|
||||||
|
max_string = float(-inf)
|
||||||
|
ans = ""
|
||||||
|
i = 0
|
||||||
|
|
||||||
|
def checker(l, r, a, mx):
|
||||||
|
while l >= 0 and r < len(s) and s[l] == s[r]:
|
||||||
|
if (r - l + 1) > max_string:
|
||||||
|
a = s[l : r + 1]
|
||||||
|
mx = r - l + 1
|
||||||
|
l -= 1
|
||||||
|
r += 1
|
||||||
|
return a, mx
|
||||||
|
|
||||||
|
for i in range(len(s)):
|
||||||
|
ans, max_string = checker(i, i, ans, max_string)
|
||||||
|
ans, max_string = checker(i, i + 1, ans, max_string)
|
||||||
|
|
||||||
|
return ans
|
||||||
|
|
||||||
|
|
||||||
|
# @leet end
|
||||||
|
|
||||||
53
src/exercises/50.powx-n.py
Normal file
53
src/exercises/50.powx-n.py
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
# @leet imports start
|
||||||
|
from string import *
|
||||||
|
from re import *
|
||||||
|
from datetime import *
|
||||||
|
from collections import *
|
||||||
|
from heapq import *
|
||||||
|
from bisect import *
|
||||||
|
from copy import *
|
||||||
|
from math import *
|
||||||
|
from random import *
|
||||||
|
from statistics import *
|
||||||
|
from itertools import *
|
||||||
|
from functools import *
|
||||||
|
from operator import *
|
||||||
|
from io import *
|
||||||
|
from sys import *
|
||||||
|
from json import *
|
||||||
|
from builtins import *
|
||||||
|
import string
|
||||||
|
import re
|
||||||
|
import datetime
|
||||||
|
import collections
|
||||||
|
import heapq
|
||||||
|
import bisect
|
||||||
|
import copy
|
||||||
|
import math
|
||||||
|
import random
|
||||||
|
import statistics
|
||||||
|
import itertools
|
||||||
|
import functools
|
||||||
|
import operator
|
||||||
|
import io
|
||||||
|
import sys
|
||||||
|
import json
|
||||||
|
from typing import *
|
||||||
|
# @leet imports end
|
||||||
|
|
||||||
|
# @leet start
|
||||||
|
class Solution:
|
||||||
|
def myPow(self, x: float, n: int) -> float:
|
||||||
|
if n == 0:
|
||||||
|
return 1
|
||||||
|
def getP(xx, nn):
|
||||||
|
if nn == 1: return xx
|
||||||
|
h = getP(xx, nn // 2)
|
||||||
|
if nn % 2 == 0:
|
||||||
|
return h * h
|
||||||
|
else:
|
||||||
|
return xx * h * h
|
||||||
|
if n < 0:
|
||||||
|
return 1 / getP(x, abs(n))
|
||||||
|
return getP(x, n)
|
||||||
|
# @leet end
|
||||||
53
src/exercises/55.jump-game.py
Normal file
53
src/exercises/55.jump-game.py
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
# @leet imports start
|
||||||
|
from string import *
|
||||||
|
from re import *
|
||||||
|
from datetime import *
|
||||||
|
from collections import *
|
||||||
|
from heapq import *
|
||||||
|
from bisect import *
|
||||||
|
from copy import *
|
||||||
|
from math import *
|
||||||
|
from random import *
|
||||||
|
from statistics import *
|
||||||
|
from itertools import *
|
||||||
|
from functools import *
|
||||||
|
from operator import *
|
||||||
|
from io import *
|
||||||
|
from sys import *
|
||||||
|
from json import *
|
||||||
|
from builtins import *
|
||||||
|
import string
|
||||||
|
import re
|
||||||
|
import datetime
|
||||||
|
import collections
|
||||||
|
import heapq
|
||||||
|
import bisect
|
||||||
|
import copy
|
||||||
|
import math
|
||||||
|
import random
|
||||||
|
import statistics
|
||||||
|
import itertools
|
||||||
|
import functools
|
||||||
|
import operator
|
||||||
|
import io
|
||||||
|
import sys
|
||||||
|
import json
|
||||||
|
from typing import *
|
||||||
|
|
||||||
|
# @leet imports end
|
||||||
|
|
||||||
|
|
||||||
|
# @leet start
|
||||||
|
class Solution:
|
||||||
|
def canJump(self, nums: List[int]) -> bool:
|
||||||
|
f = 0
|
||||||
|
for i in range(len(nums)):
|
||||||
|
if f < i:
|
||||||
|
return False
|
||||||
|
f = nums[i] + i if nums[i] + i > f else f
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
# @leet end
|
||||||
|
|
||||||
0
src/exercises/6.zigzag-conversion.py
Normal file
0
src/exercises/6.zigzag-conversion.py
Normal file
64
src/exercises/66.plus-one.py
Normal file
64
src/exercises/66.plus-one.py
Normal file
@@ -0,0 +1,64 @@
|
|||||||
|
# @leet imports start
|
||||||
|
from string import *
|
||||||
|
from re import *
|
||||||
|
from datetime import *
|
||||||
|
from collections import *
|
||||||
|
from heapq import *
|
||||||
|
from bisect import *
|
||||||
|
from copy import *
|
||||||
|
from math import *
|
||||||
|
from random import *
|
||||||
|
from statistics import *
|
||||||
|
from itertools import *
|
||||||
|
from functools import *
|
||||||
|
from operator import *
|
||||||
|
from io import *
|
||||||
|
from sys import *
|
||||||
|
from json import *
|
||||||
|
from builtins import *
|
||||||
|
import string
|
||||||
|
import re
|
||||||
|
import datetime
|
||||||
|
import collections
|
||||||
|
import heapq
|
||||||
|
import bisect
|
||||||
|
import copy
|
||||||
|
import math
|
||||||
|
import random
|
||||||
|
import statistics
|
||||||
|
import itertools
|
||||||
|
import functools
|
||||||
|
import operator
|
||||||
|
import io
|
||||||
|
import sys
|
||||||
|
import json
|
||||||
|
from typing import *
|
||||||
|
|
||||||
|
# @leet imports end
|
||||||
|
|
||||||
|
|
||||||
|
# @leet start
|
||||||
|
class Solution:
|
||||||
|
def plusOne(self, digits: List[int]) -> List[int]:
|
||||||
|
def sumDigits(arr, con, i):
|
||||||
|
if i < 0:
|
||||||
|
arr.insert(0, con)
|
||||||
|
return arr
|
||||||
|
if con != None:
|
||||||
|
n = arr[i] + con
|
||||||
|
else:
|
||||||
|
n = arr[i] + 1
|
||||||
|
if n < 10:
|
||||||
|
arr[i] = n
|
||||||
|
else:
|
||||||
|
ci = n % 10
|
||||||
|
con = n // 10
|
||||||
|
arr[i] = ci
|
||||||
|
return sumDigits(arr, con, i - 1)
|
||||||
|
return arr
|
||||||
|
|
||||||
|
return sumDigits(digits, None, len(digits) - 1)
|
||||||
|
|
||||||
|
|
||||||
|
# @leet end
|
||||||
|
|
||||||
58
src/exercises/69.sqrtx.py
Normal file
58
src/exercises/69.sqrtx.py
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
# @leet imports start
|
||||||
|
from string import *
|
||||||
|
from re import *
|
||||||
|
from datetime import *
|
||||||
|
from collections import *
|
||||||
|
from heapq import *
|
||||||
|
from bisect import *
|
||||||
|
from copy import *
|
||||||
|
from math import *
|
||||||
|
from random import *
|
||||||
|
from statistics import *
|
||||||
|
from itertools import *
|
||||||
|
from functools import *
|
||||||
|
from operator import *
|
||||||
|
from io import *
|
||||||
|
from sys import *
|
||||||
|
from json import *
|
||||||
|
from builtins import *
|
||||||
|
import string
|
||||||
|
import re
|
||||||
|
import datetime
|
||||||
|
import collections
|
||||||
|
import heapq
|
||||||
|
import bisect
|
||||||
|
import copy
|
||||||
|
import math
|
||||||
|
import random
|
||||||
|
import statistics
|
||||||
|
import itertools
|
||||||
|
import functools
|
||||||
|
import operator
|
||||||
|
import io
|
||||||
|
import sys
|
||||||
|
import json
|
||||||
|
from typing import *
|
||||||
|
|
||||||
|
# @leet imports end
|
||||||
|
|
||||||
|
|
||||||
|
# @leet start
|
||||||
|
class Solution:
|
||||||
|
def mySqrt(self, x: int) -> int:
|
||||||
|
if x == 1:
|
||||||
|
return x
|
||||||
|
lw = 1
|
||||||
|
lm = x
|
||||||
|
while lw < lm:
|
||||||
|
mid = (lw + lm) // 2
|
||||||
|
s = mid**2
|
||||||
|
if s <= x:
|
||||||
|
lw = mid + 1
|
||||||
|
else:
|
||||||
|
lm = mid
|
||||||
|
return lw - 1
|
||||||
|
|
||||||
|
|
||||||
|
# @leet end
|
||||||
|
|
||||||
0
src/exercises/7.reverse-integer.py
Normal file
0
src/exercises/7.reverse-integer.py
Normal file
59
src/exercises/704.binary-search.py
Normal file
59
src/exercises/704.binary-search.py
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
from string import *
|
||||||
|
from re import *
|
||||||
|
from datetime import *
|
||||||
|
from collections import *
|
||||||
|
from heapq import *
|
||||||
|
from bisect import *
|
||||||
|
from copy import *
|
||||||
|
from math import *
|
||||||
|
from random import *
|
||||||
|
from statistics import *
|
||||||
|
from itertools import *
|
||||||
|
from functools import *
|
||||||
|
from operator import *
|
||||||
|
from io import *
|
||||||
|
from sys import *
|
||||||
|
from json import *
|
||||||
|
from builtins import *
|
||||||
|
import string
|
||||||
|
import re
|
||||||
|
import datetime
|
||||||
|
import collections
|
||||||
|
import heapq
|
||||||
|
import bisect
|
||||||
|
import copy
|
||||||
|
import math
|
||||||
|
import random
|
||||||
|
import statistics
|
||||||
|
import itertools
|
||||||
|
import functools
|
||||||
|
import operator
|
||||||
|
import io
|
||||||
|
import sys
|
||||||
|
import json
|
||||||
|
from typing import *
|
||||||
|
|
||||||
|
# @leet start
|
||||||
|
class Solution:
|
||||||
|
def search(self, nums: List[int], target: int) -> int:
|
||||||
|
|
||||||
|
def bin_search(low, high):
|
||||||
|
if low > high:
|
||||||
|
return -1
|
||||||
|
mid = (high + low) // 2
|
||||||
|
if nums[low] == target:
|
||||||
|
return low
|
||||||
|
if nums[high] == target:
|
||||||
|
return high
|
||||||
|
if nums[mid] == target:
|
||||||
|
return mid
|
||||||
|
if nums[mid] < target:
|
||||||
|
return bin_search(mid + 1, high)
|
||||||
|
if nums[mid] > target:
|
||||||
|
return bin_search(low, mid - 1)
|
||||||
|
|
||||||
|
return bin_search(0, len(nums) - 1)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# @leet end
|
||||||
79
src/exercises/74.search-a-2d-matrix.py
Normal file
79
src/exercises/74.search-a-2d-matrix.py
Normal file
@@ -0,0 +1,79 @@
|
|||||||
|
from string import *
|
||||||
|
from re import *
|
||||||
|
from datetime import *
|
||||||
|
from collections import *
|
||||||
|
from heapq import *
|
||||||
|
from bisect import *
|
||||||
|
from copy import *
|
||||||
|
from math import *
|
||||||
|
from random import *
|
||||||
|
from statistics import *
|
||||||
|
from itertools import *
|
||||||
|
from functools import *
|
||||||
|
from operator import *
|
||||||
|
from io import *
|
||||||
|
from sys import *
|
||||||
|
from json import *
|
||||||
|
from builtins import *
|
||||||
|
import string
|
||||||
|
import re
|
||||||
|
import datetime
|
||||||
|
import collections
|
||||||
|
import heapq
|
||||||
|
import bisect
|
||||||
|
import copy
|
||||||
|
import math
|
||||||
|
import random
|
||||||
|
import statistics
|
||||||
|
import itertools
|
||||||
|
import functools
|
||||||
|
import operator
|
||||||
|
import io
|
||||||
|
import sys
|
||||||
|
import json
|
||||||
|
from typing import *
|
||||||
|
|
||||||
|
|
||||||
|
# @leet start
|
||||||
|
class Solution:
|
||||||
|
def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
|
||||||
|
def row_search(low, high):
|
||||||
|
if low > high:
|
||||||
|
return -1
|
||||||
|
if matrix[low][0] <= target and target <= matrix[0][len(matrix[low]) - 1]:
|
||||||
|
return low
|
||||||
|
if matrix[high][0] <= target and target <= matrix[0][len(matrix[high]) - 1]:
|
||||||
|
return high
|
||||||
|
|
||||||
|
mid = (low + high) // 2
|
||||||
|
if matrix[mid][0] <= target and target <= matrix[mid][len(matrix[mid]) - 1]:
|
||||||
|
return mid
|
||||||
|
if matrix[mid][0] < target:
|
||||||
|
return row_search(mid + 1, high)
|
||||||
|
if matrix[mid][0] > target:
|
||||||
|
return row_search(low, mid - 1)
|
||||||
|
|
||||||
|
row = row_search(0, len(matrix) - 1)
|
||||||
|
nums = matrix[row]
|
||||||
|
|
||||||
|
def bin_search(low, high):
|
||||||
|
if low > high:
|
||||||
|
return -1
|
||||||
|
mid = (high + low) // 2
|
||||||
|
if nums[low] == target:
|
||||||
|
return low
|
||||||
|
if nums[high] == target:
|
||||||
|
return high
|
||||||
|
if nums[mid] == target:
|
||||||
|
return mid
|
||||||
|
if nums[mid] < target:
|
||||||
|
return bin_search(mid + 1, high)
|
||||||
|
if nums[mid] > target:
|
||||||
|
return bin_search(low, mid - 1)
|
||||||
|
|
||||||
|
isValid = bin_search(0, len(nums) - 1)
|
||||||
|
return isValid != -1
|
||||||
|
|
||||||
|
|
||||||
|
# @leet end
|
||||||
|
|
||||||
56
src/exercises/80.remove-duplicates-from-sorted-array-ii.py
Normal file
56
src/exercises/80.remove-duplicates-from-sorted-array-ii.py
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
# @leet imports start
|
||||||
|
from string import *
|
||||||
|
from re import *
|
||||||
|
from datetime import *
|
||||||
|
from collections import *
|
||||||
|
from heapq import *
|
||||||
|
from bisect import *
|
||||||
|
from copy import *
|
||||||
|
from math import *
|
||||||
|
from random import *
|
||||||
|
from statistics import *
|
||||||
|
from itertools import *
|
||||||
|
from functools import *
|
||||||
|
from operator import *
|
||||||
|
from io import *
|
||||||
|
from sys import *
|
||||||
|
from json import *
|
||||||
|
from builtins import *
|
||||||
|
import string
|
||||||
|
import re
|
||||||
|
import datetime
|
||||||
|
import collections
|
||||||
|
import heapq
|
||||||
|
import bisect
|
||||||
|
import copy
|
||||||
|
import math
|
||||||
|
import random
|
||||||
|
import statistics
|
||||||
|
import itertools
|
||||||
|
import functools
|
||||||
|
import operator
|
||||||
|
import io
|
||||||
|
import sys
|
||||||
|
import json
|
||||||
|
from typing import *
|
||||||
|
# @leet imports end
|
||||||
|
|
||||||
|
# @leet start
|
||||||
|
class Solution:
|
||||||
|
def removeDuplicates(self, nums: List[int]) -> int:
|
||||||
|
if not nums:
|
||||||
|
return 0
|
||||||
|
if len(nums) <= 2:
|
||||||
|
return len(nums)
|
||||||
|
k = 2
|
||||||
|
for i in range(2, len(nums)):
|
||||||
|
if nums[i] != nums[k - 1] or nums[i] != nums[k - 2]:
|
||||||
|
nums[k] = nums[i]
|
||||||
|
k += 1
|
||||||
|
print(nums)
|
||||||
|
|
||||||
|
return k
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# @leet end
|
||||||
57
src/exercises/875.koko-eating-bananas.py
Normal file
57
src/exercises/875.koko-eating-bananas.py
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
from string import *
|
||||||
|
from re import *
|
||||||
|
from datetime import *
|
||||||
|
from collections import *
|
||||||
|
from heapq import *
|
||||||
|
from bisect import *
|
||||||
|
from copy import *
|
||||||
|
from math import *
|
||||||
|
from random import *
|
||||||
|
from statistics import *
|
||||||
|
from itertools import *
|
||||||
|
from functools import *
|
||||||
|
from operator import *
|
||||||
|
from io import *
|
||||||
|
from sys import *
|
||||||
|
from json import *
|
||||||
|
from builtins import *
|
||||||
|
import string
|
||||||
|
import re
|
||||||
|
import datetime
|
||||||
|
import collections
|
||||||
|
import heapq
|
||||||
|
import bisect
|
||||||
|
import copy
|
||||||
|
import math
|
||||||
|
import random
|
||||||
|
import statistics
|
||||||
|
import itertools
|
||||||
|
import functools
|
||||||
|
import operator
|
||||||
|
import io
|
||||||
|
import sys
|
||||||
|
import json
|
||||||
|
from typing import *
|
||||||
|
|
||||||
|
|
||||||
|
# @leet start
|
||||||
|
class Solution:
|
||||||
|
def minEatingSpeed(self, piles: List[int], h: int) -> int:
|
||||||
|
def bs(val, low, high, len):
|
||||||
|
if low > high:
|
||||||
|
return val
|
||||||
|
mid = (low + high) // 2
|
||||||
|
sum_v = 0
|
||||||
|
for i in range(len):
|
||||||
|
sum_v += (piles[i] + mid - 1) // mid
|
||||||
|
if sum_v <= h:
|
||||||
|
min_val = min(val, mid)
|
||||||
|
return bs(min_val, low, mid - 1, len)
|
||||||
|
else:
|
||||||
|
return bs(val, mid + 1, high, len)
|
||||||
|
|
||||||
|
return bs(float(inf), 1, max(piles), len(piles))
|
||||||
|
|
||||||
|
|
||||||
|
# @leet end
|
||||||
|
|
||||||
48
src/exercises/88.merge-sorted-array.py
Normal file
48
src/exercises/88.merge-sorted-array.py
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
from string import *
|
||||||
|
from re import *
|
||||||
|
from datetime import *
|
||||||
|
from collections import *
|
||||||
|
from heapq import *
|
||||||
|
from bisect import *
|
||||||
|
from copy import *
|
||||||
|
from math import *
|
||||||
|
from random import *
|
||||||
|
from statistics import *
|
||||||
|
from itertools import *
|
||||||
|
from functools import *
|
||||||
|
from operator import *
|
||||||
|
from io import *
|
||||||
|
from sys import *
|
||||||
|
from json import *
|
||||||
|
from builtins import *
|
||||||
|
import string
|
||||||
|
import re
|
||||||
|
import datetime
|
||||||
|
import collections
|
||||||
|
import heapq
|
||||||
|
import bisect
|
||||||
|
import copy
|
||||||
|
import math
|
||||||
|
import random
|
||||||
|
import statistics
|
||||||
|
import itertools
|
||||||
|
import functools
|
||||||
|
import operator
|
||||||
|
import io
|
||||||
|
import sys
|
||||||
|
import json
|
||||||
|
from typing import *
|
||||||
|
|
||||||
|
|
||||||
|
# @leet start
|
||||||
|
class Solution:
|
||||||
|
def merge(self, nums1: List[int], m: int, nums2: List[int], n: int) -> None:
|
||||||
|
k = 0
|
||||||
|
for i in range(m, len(nums1)):
|
||||||
|
nums1[i] = nums2[k]
|
||||||
|
k += 1
|
||||||
|
nums1 = nums1.sort()
|
||||||
|
return nums1
|
||||||
|
|
||||||
|
# @leet end
|
||||||
|
|
||||||
45
src/exercises/89.gray-code.py
Normal file
45
src/exercises/89.gray-code.py
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
from string import *
|
||||||
|
from re import *
|
||||||
|
from datetime import *
|
||||||
|
from collections import *
|
||||||
|
from heapq import *
|
||||||
|
from bisect import *
|
||||||
|
from copy import *
|
||||||
|
from math import *
|
||||||
|
from random import *
|
||||||
|
from statistics import *
|
||||||
|
from itertools import *
|
||||||
|
from functools import *
|
||||||
|
from operator import *
|
||||||
|
from io import *
|
||||||
|
from sys import *
|
||||||
|
from json import *
|
||||||
|
from builtins import *
|
||||||
|
import string
|
||||||
|
import re
|
||||||
|
import datetime
|
||||||
|
import collections
|
||||||
|
import heapq
|
||||||
|
import bisect
|
||||||
|
import copy
|
||||||
|
import math
|
||||||
|
import random
|
||||||
|
import statistics
|
||||||
|
import itertools
|
||||||
|
import functools
|
||||||
|
import operator
|
||||||
|
import io
|
||||||
|
import sys
|
||||||
|
import json
|
||||||
|
from typing import *
|
||||||
|
|
||||||
|
|
||||||
|
# @leet start
|
||||||
|
class Solution:
|
||||||
|
def grayCode(self, n: int) -> List[int]:
|
||||||
|
return [i ^ (i >> 1) for i in range(1 << n)]
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# @leet end
|
||||||
|
|
||||||
51
src/exercises/9.palindrome-number.py
Normal file
51
src/exercises/9.palindrome-number.py
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
from string import *
|
||||||
|
from re import *
|
||||||
|
from datetime import *
|
||||||
|
from collections import *
|
||||||
|
from heapq import *
|
||||||
|
from bisect import *
|
||||||
|
from copy import *
|
||||||
|
from math import *
|
||||||
|
from random import *
|
||||||
|
from statistics import *
|
||||||
|
from itertools import *
|
||||||
|
from functools import *
|
||||||
|
from operator import *
|
||||||
|
from io import *
|
||||||
|
from sys import *
|
||||||
|
from json import *
|
||||||
|
from builtins import *
|
||||||
|
import string
|
||||||
|
import re
|
||||||
|
import datetime
|
||||||
|
import collections
|
||||||
|
import heapq
|
||||||
|
import bisect
|
||||||
|
import copy
|
||||||
|
import math
|
||||||
|
import random
|
||||||
|
import statistics
|
||||||
|
import itertools
|
||||||
|
import functools
|
||||||
|
import operator
|
||||||
|
import io
|
||||||
|
import sys
|
||||||
|
import json
|
||||||
|
from typing import *
|
||||||
|
|
||||||
|
# @leet start
|
||||||
|
class Solution:
|
||||||
|
def isPalindrome(self, x: int) -> bool:
|
||||||
|
i = 0
|
||||||
|
n = str(x)
|
||||||
|
j = len(n) - 1
|
||||||
|
while i < j:
|
||||||
|
if n[i] == n[j]:
|
||||||
|
i += 1
|
||||||
|
j -= 1
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
|
# @leet end
|
||||||
60
src/exercises/90.subsets-ii.py
Normal file
60
src/exercises/90.subsets-ii.py
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
from string import *
|
||||||
|
from re import *
|
||||||
|
from datetime import *
|
||||||
|
from collections import *
|
||||||
|
from heapq import *
|
||||||
|
from bisect import *
|
||||||
|
from copy import *
|
||||||
|
from math import *
|
||||||
|
from random import *
|
||||||
|
from statistics import *
|
||||||
|
from itertools import *
|
||||||
|
from functools import *
|
||||||
|
from operator import *
|
||||||
|
from io import *
|
||||||
|
from sys import *
|
||||||
|
from json import *
|
||||||
|
from builtins import *
|
||||||
|
import string
|
||||||
|
import re
|
||||||
|
import datetime
|
||||||
|
import collections
|
||||||
|
import heapq
|
||||||
|
import bisect
|
||||||
|
import copy
|
||||||
|
import math
|
||||||
|
import random
|
||||||
|
import statistics
|
||||||
|
import itertools
|
||||||
|
import functools
|
||||||
|
import operator
|
||||||
|
import io
|
||||||
|
import sys
|
||||||
|
import json
|
||||||
|
from typing import *
|
||||||
|
|
||||||
|
|
||||||
|
# @leet start
|
||||||
|
class Solution:
|
||||||
|
def subsetsWithDup(self, nums: List[int]) -> List[List[int]]:
|
||||||
|
res = []
|
||||||
|
|
||||||
|
nums.sort()
|
||||||
|
|
||||||
|
def backtrack(start, path):
|
||||||
|
res.append(path[:])
|
||||||
|
|
||||||
|
for i in range(start, len(nums)):
|
||||||
|
if i > start and nums[i] == nums[i - 1]:
|
||||||
|
continue
|
||||||
|
|
||||||
|
path.append(nums[i])
|
||||||
|
backtrack(i + 1, path)
|
||||||
|
path.pop()
|
||||||
|
|
||||||
|
backtrack(0, [])
|
||||||
|
return res
|
||||||
|
|
||||||
|
|
||||||
|
# @leet end
|
||||||
|
|
||||||
57
src/exercises/91.decode-ways.py
Normal file
57
src/exercises/91.decode-ways.py
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
from string import *
|
||||||
|
from re import *
|
||||||
|
from datetime import *
|
||||||
|
from collections import *
|
||||||
|
from heapq import *
|
||||||
|
from bisect import *
|
||||||
|
from copy import *
|
||||||
|
from math import *
|
||||||
|
from random import *
|
||||||
|
from statistics import *
|
||||||
|
from itertools import *
|
||||||
|
from functools import *
|
||||||
|
from operator import *
|
||||||
|
from io import *
|
||||||
|
from sys import *
|
||||||
|
from json import *
|
||||||
|
from builtins import *
|
||||||
|
import string
|
||||||
|
import re
|
||||||
|
import datetime
|
||||||
|
import collections
|
||||||
|
import heapq
|
||||||
|
import bisect
|
||||||
|
import copy
|
||||||
|
import math
|
||||||
|
import random
|
||||||
|
import statistics
|
||||||
|
import itertools
|
||||||
|
import functools
|
||||||
|
import operator
|
||||||
|
import io
|
||||||
|
import sys
|
||||||
|
import json
|
||||||
|
from typing import *
|
||||||
|
|
||||||
|
|
||||||
|
# @leet start
|
||||||
|
class Solution:
|
||||||
|
@lru_cache(maxsize=None)
|
||||||
|
def getWordsCount(self, i, s) -> int:
|
||||||
|
if i == len(s):
|
||||||
|
return 1
|
||||||
|
if s[i] == "0":
|
||||||
|
return 0
|
||||||
|
if i == len(s) - 1:
|
||||||
|
return 1
|
||||||
|
answer = self.getWordsCount(i + 1, s)
|
||||||
|
if int(s[i : i + 2]) < 27:
|
||||||
|
answer += self.getWordsCount(i + 2, s)
|
||||||
|
return answer
|
||||||
|
|
||||||
|
def numDecodings(self, s: str) -> int:
|
||||||
|
return self.getWordsCount(0, s)
|
||||||
|
|
||||||
|
|
||||||
|
# @leet end
|
||||||
|
|
||||||
45
src/exercises/92.reverse-linked-list-ii.py
Normal file
45
src/exercises/92.reverse-linked-list-ii.py
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
from string import *
|
||||||
|
from re import *
|
||||||
|
from datetime import *
|
||||||
|
from collections import *
|
||||||
|
from heapq import *
|
||||||
|
from bisect import *
|
||||||
|
from copy import *
|
||||||
|
from math import *
|
||||||
|
from random import *
|
||||||
|
from statistics import *
|
||||||
|
from itertools import *
|
||||||
|
from functools import *
|
||||||
|
from operator import *
|
||||||
|
from io import *
|
||||||
|
from sys import *
|
||||||
|
from json import *
|
||||||
|
from builtins import *
|
||||||
|
import string
|
||||||
|
import re
|
||||||
|
import datetime
|
||||||
|
import collections
|
||||||
|
import heapq
|
||||||
|
import bisect
|
||||||
|
import copy
|
||||||
|
import math
|
||||||
|
import random
|
||||||
|
import statistics
|
||||||
|
import itertools
|
||||||
|
import functools
|
||||||
|
import operator
|
||||||
|
import io
|
||||||
|
import sys
|
||||||
|
import json
|
||||||
|
from typing import *
|
||||||
|
|
||||||
|
# @leet start
|
||||||
|
# Definition for singly-linked list.
|
||||||
|
# class ListNode:
|
||||||
|
# def __init__(self, val=0, next=None):
|
||||||
|
# self.val = val
|
||||||
|
# self.next = next
|
||||||
|
class Solution:
|
||||||
|
def reverseBetween(self, head: Optional[ListNode], left: int, right: int) -> Optional[ListNode]:
|
||||||
|
|
||||||
|
# @leet end
|
||||||
42
src/exercises/990.satisfiability-of-equality-equations.py
Normal file
42
src/exercises/990.satisfiability-of-equality-equations.py
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
# @leet imports start
|
||||||
|
from string import *
|
||||||
|
from re import *
|
||||||
|
from datetime import *
|
||||||
|
from collections import *
|
||||||
|
from heapq import *
|
||||||
|
from bisect import *
|
||||||
|
from copy import *
|
||||||
|
from math import *
|
||||||
|
from random import *
|
||||||
|
from statistics import *
|
||||||
|
from itertools import *
|
||||||
|
from functools import *
|
||||||
|
from operator import *
|
||||||
|
from io import *
|
||||||
|
from sys import *
|
||||||
|
from json import *
|
||||||
|
from builtins import *
|
||||||
|
import string
|
||||||
|
import re
|
||||||
|
import datetime
|
||||||
|
import collections
|
||||||
|
import heapq
|
||||||
|
import bisect
|
||||||
|
import copy
|
||||||
|
import math
|
||||||
|
import random
|
||||||
|
import statistics
|
||||||
|
import itertools
|
||||||
|
import functools
|
||||||
|
import operator
|
||||||
|
import io
|
||||||
|
import sys
|
||||||
|
import json
|
||||||
|
from typing import *
|
||||||
|
# @leet imports end
|
||||||
|
|
||||||
|
# @leet start
|
||||||
|
class Solution:
|
||||||
|
def equationsPossible(self, equations: List[str]) -> bool:
|
||||||
|
|
||||||
|
# @leet end
|
||||||
Reference in New Issue
Block a user