diff --git a/main.ipynb b/main.ipynb new file mode 100644 index 0000000..0a91670 --- /dev/null +++ b/main.ipynb @@ -0,0 +1,68 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "32e3594e", + "metadata": {}, + "outputs": [ + { + "ename": "IndexError", + "evalue": "list index out of range", + "output_type": "error", + "traceback": [ + "\u001b[31m---------------------------------------------------------------------------\u001b[39m", + "\u001b[31mIndexError\u001b[39m Traceback (most recent call last)", + "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[9]\u001b[39m\u001b[32m, line 6\u001b[39m\n\u001b[32m 2\u001b[39m \n\u001b[32m 3\u001b[39m count = []\n\u001b[32m 4\u001b[39m \n\u001b[32m 5\u001b[39m \u001b[38;5;28;01mfor\u001b[39;00m num \u001b[38;5;28;01min\u001b[39;00m nums:\n\u001b[32m----> \u001b[39m\u001b[32m6\u001b[39m count[num] += \u001b[32m1\u001b[39m\n\u001b[32m 7\u001b[39m \n\u001b[32m 8\u001b[39m \u001b[38;5;66;03m# 2. Делаем префиксную сумму\u001b[39;00m\n\u001b[32m 9\u001b[39m \u001b[38;5;28;01mfor\u001b[39;00m i \u001b[38;5;28;01min\u001b[39;00m range(\u001b[32m1\u001b[39m, \u001b[32m101\u001b[39m):\n", + "\u001b[31mIndexError\u001b[39m: list index out of range" + ] + } + ], + "source": [ + "nums = [8,1,2,2,3,123,43,5,3,1,5,8,3,965,8,546,456,34,12,3]\n", + "\n", + "count = []\n", + "\n", + "for num in nums:\n", + " if num in count:\n", + " \n", + " count[num] += 1\n", + " \n", + "for i in range(1, 101):\n", + " count[i] += count[i-1]\n", + "\n", + "result = []\n", + "for num in nums:\n", + " if num == 0:\n", + " result.append(0)\n", + " else:\n", + " result.append(count[num - 1])\n", + "\n", + "\n", + "\n", + "print(result)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "LeetCode_tasks", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.14.2" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/solutions/lc1.py b/solutions/lc1.py new file mode 100644 index 0000000..23a84e2 --- /dev/null +++ b/solutions/lc1.py @@ -0,0 +1,9 @@ +class Solution: + def twoSum(self, nums: List[int], target: int) -> List[int]: + seen = {} + for i, x in enumerate(nums): + need = target - x + + if need in seen: + return [seen[need], i] + seen[x] = i diff --git a/solutions/lc1365.py b/solutions/lc1365.py new file mode 100644 index 0000000..c026bec --- /dev/null +++ b/solutions/lc1365.py @@ -0,0 +1,16 @@ +class Solution: + def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]: + count = {} + for num in nums: + count[num] = count.get(num, 0) + 1 + + sorted_nums = sorted(count) + + less_count = {} + current = 0 + + for num in sorted_nums: + less_count[num] = current + current += count[num] + + return [less_count[num] for num in nums] diff --git a/solutions/lc2540.py b/solutions/lc2540.py new file mode 100644 index 0000000..e61cc81 --- /dev/null +++ b/solutions/lc2540.py @@ -0,0 +1,9 @@ +class Solution: + def getCommon(self, nums1: List[int], nums2: List[int]) -> int: + s1 = set(nums1) + + for num in nums2: + if num in s1: + return num + else: + return -1