Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
153 changes: 152 additions & 1 deletion blind75/medium.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -503,10 +503,161 @@
"solution.lengthOfLongestSubstring(s)"
]
},
{
"cell_type": "markdown",
"id": "659393fc-54b2-42d8-813a-2b5f84c8189f",
"metadata": {},
"source": [
"## 424. Longest Repeating Character Replacement\n",
"\n",
"[Leetcode Link](https://leetcode.com/problems/longest-repeating-character-replacement/description/?envType=problem-list-v2&envId=oizxjoit&)\n",
"\n",
"You are given a string s and an integer k. You can choose any character of the string and change it to any other uppercase English character. You can perform this operation at most k times.\n",
"\n",
"`Return` the length of the longest substring containing the same letter you can get after performing the above operations.\n",
"\n",
"Example 1:\n",
"\n",
"Input: s = \"ABAB\", k = 2\n",
"\n",
"Output: 4\n",
"\n",
"Explanation: Replace the two 'A's with two 'B's or vice versa."
]
},
{
"cell_type": "code",
"execution_count": 144,
"id": "bd423996-95b0-4b11-8a03-d8c56824fe40",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'A': 1}\n",
"{'A': 1, 'B': 1}\n",
"A\n",
"{'A': 2, 'B': 1}\n",
"B\n",
"{'A': 2, 'B': 2}\n",
"B\n",
"{'A': 1, 'B': 3}\n",
"A\n",
"{'A': 2, 'B': 3}\n"
]
},
{
"data": {
"text/plain": [
"6"
]
},
"execution_count": 144,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def characterReplacement(s: str, k: int) -> int:\n",
" i,j = 0,0\n",
"\n",
" count_map = {}\n",
" count_most_freq_char = 0\n",
" longest = 0\n",
" while j <len(s):\n",
" if (j-i +1) - count_most_freq_char <=k:\n",
" if s[j] in count_map.keys():\n",
" print(s[j])\n",
" count_map[s[j]] = count_map[s[j]] + 1\n",
" else:\n",
" count_map[s[j]] = 1\n",
" print(count_map)\n",
" j+=1\n",
" else:\n",
" count_map[s[i]] -= 1\n",
" i+=1\n",
" longest = max(longest, (j-i+1))\n",
" count_most_freq_char = max(list(count_map.values()))\n",
" return longest\n",
"\n",
"\n",
"s = 'AABBCCD'\n",
"s = 'ABABBA'\n",
"k = 2\n",
"\n",
"characterReplacement(s,k)"
]
},
{
"cell_type": "code",
"execution_count": 142,
"id": "09ea70f4-53cd-4972-a1a6-d9806d118be8",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{5: 5}"
]
},
"execution_count": 142,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = {}\n",
"a[5] = 5\n",
"a"
]
},
{
"cell_type": "code",
"execution_count": 139,
"id": "c949151e-6bce-4f80-b0bd-ec1f5d3434a2",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"5"
]
},
"execution_count": 139,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a.get(5)"
]
},
{
"cell_type": "code",
"execution_count": 140,
"id": "91b406f5-823c-49b8-a33e-7456c789daac",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"5"
]
},
"execution_count": 140,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a[5]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "15d65949-0569-48d6-86d5-7b39ca31595e",
"id": "0578ee15-f208-4d5c-8540-7112157df22b",
"metadata": {},
"outputs": [],
"source": []
Expand Down
107 changes: 107 additions & 0 deletions patterns/prefix_sum.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "98c2df75-d2fe-409d-8da4-ab5b53087055",
"metadata": {},
"source": [
"# Prefix Sum Pattern"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "aa2b63d9-5873-4ad8-b7c1-c19a72d7d58c",
"metadata": {},
"outputs": [],
"source": [
"from typing import List"
]
},
{
"cell_type": "markdown",
"id": "4152f640-b05b-40b0-8e36-391d790c219b",
"metadata": {},
"source": [
"## 303. Range Sum Query - Immutable\n",
"\n",
"[Leetcode Link](https://leetcode.com/problems/range-sum-query-immutable/description/)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "acda99b4-2c64-4078-a7a2-5c5c67309dcb",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1 -1 -3\n"
]
}
],
"source": [
"class NumArray:\n",
"\n",
" def __init__(self, nums: List[int]):\n",
" self.nums = nums\n",
"\n",
" for i in range(len(self.nums)):\n",
" if i == 0:\n",
" continue\n",
" else:\n",
" self.nums[i] += self.nums[i-1] \n",
" def sumRange(self, left: int, right: int) -> int:\n",
" return self.nums[right] - (self.nums[left-1] if (left-1) >=0 else 0)\n",
"\n",
"\n",
"\n",
"# Your NumArray object will be instantiated and called as such:\n",
"nums = [-2,0,3,-5,2,-1]\n",
"obj = NumArray(nums)\n",
"\n",
"left, right = 0,2\n",
"param_2 = obj.sumRange(left,right)\n",
"\n",
"left, right = 2,5\n",
"param_3 = obj.sumRange(left,right)\n",
"\n",
"left, right = 0, 5\n",
"param_4 = obj.sumRange(left,right)\n",
"\n",
"print(param_2, param_3, param_4)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f992a73f-0ea4-4709-a508-a0fe20211f5c",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.10.14"
}
},
"nbformat": 4,
"nbformat_minor": 5
}