Skip to content

Commit 0b2df9c

Browse files
committed
Decompress Run-Length Encoded List
1 parent 3f20d6d commit 0b2df9c

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed

.ipynb_checkpoints/lets code-checkpoint.ipynb

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1115,6 +1115,53 @@
11151115
"print(sol.selfDividingNumbers(1,22))"
11161116
]
11171117
},
1118+
{
1119+
"cell_type": "code",
1120+
"execution_count": 3,
1121+
"metadata": {},
1122+
"outputs": [
1123+
{
1124+
"name": "stdout",
1125+
"output_type": "stream",
1126+
"text": [
1127+
"[2, 4, 4, 4]\n"
1128+
]
1129+
}
1130+
],
1131+
"source": [
1132+
"\"\"\"\n",
1133+
"1313. Decompress Run-Length Encoded List\n",
1134+
"\n",
1135+
"We are given a list nums of integers representing a list compressed with run-length encoding.\n",
1136+
"\n",
1137+
"Consider each adjacent pair of elements [freq, val] = [nums[2*i], nums[2*i+1]] (with i >= 0). For each such pair, there are freq elements with value val concatenated in a sublist. Concatenate all the sublists from left to right to generate the decompressed list.\n",
1138+
"\n",
1139+
"Return the decompressed list.\n",
1140+
"\n",
1141+
"Input: nums = [1,2,3,4]\n",
1142+
"Output: [2,4,4,4]\n",
1143+
"\n",
1144+
"Explanation: The first pair [1,2] means we have freq = 1 and val = 2 so we generate the array [2].\n",
1145+
"The second pair [3,4] means we have freq = 3 and val = 4 so we generate [4,4,4].\n",
1146+
"At the end the concatenation [2] + [4,4,4] is [2,4,4,4].\n",
1147+
"\"\"\"\n",
1148+
"from typing import List\n",
1149+
"class Solution:\n",
1150+
" def decompressRLElist(self, nums: List[int]) -> List[int]:\n",
1151+
" outList = []\n",
1152+
" for i in range(0,len(nums),2):\n",
1153+
"# print(nums[i],nums[i+1])\n",
1154+
" freq = nums[i] \n",
1155+
" val = nums[i+1]\n",
1156+
" for i in range(freq):\n",
1157+
" outList.append(val)\n",
1158+
" return outList\n",
1159+
" \n",
1160+
" \n",
1161+
"sol = Solution();\n",
1162+
"print(sol.decompressRLElist([1,2,3,4]))"
1163+
]
1164+
},
11181165
{
11191166
"cell_type": "code",
11201167
"execution_count": null,

lets code.ipynb

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1115,6 +1115,53 @@
11151115
"print(sol.selfDividingNumbers(1,22))"
11161116
]
11171117
},
1118+
{
1119+
"cell_type": "code",
1120+
"execution_count": 3,
1121+
"metadata": {},
1122+
"outputs": [
1123+
{
1124+
"name": "stdout",
1125+
"output_type": "stream",
1126+
"text": [
1127+
"[2, 4, 4, 4]\n"
1128+
]
1129+
}
1130+
],
1131+
"source": [
1132+
"\"\"\"\n",
1133+
"1313. Decompress Run-Length Encoded List\n",
1134+
"\n",
1135+
"We are given a list nums of integers representing a list compressed with run-length encoding.\n",
1136+
"\n",
1137+
"Consider each adjacent pair of elements [freq, val] = [nums[2*i], nums[2*i+1]] (with i >= 0). For each such pair, there are freq elements with value val concatenated in a sublist. Concatenate all the sublists from left to right to generate the decompressed list.\n",
1138+
"\n",
1139+
"Return the decompressed list.\n",
1140+
"\n",
1141+
"Input: nums = [1,2,3,4]\n",
1142+
"Output: [2,4,4,4]\n",
1143+
"\n",
1144+
"Explanation: The first pair [1,2] means we have freq = 1 and val = 2 so we generate the array [2].\n",
1145+
"The second pair [3,4] means we have freq = 3 and val = 4 so we generate [4,4,4].\n",
1146+
"At the end the concatenation [2] + [4,4,4] is [2,4,4,4].\n",
1147+
"\"\"\"\n",
1148+
"from typing import List\n",
1149+
"class Solution:\n",
1150+
" def decompressRLElist(self, nums: List[int]) -> List[int]:\n",
1151+
" outList = []\n",
1152+
" for i in range(0,len(nums),2):\n",
1153+
"# print(nums[i],nums[i+1])\n",
1154+
" freq = nums[i] \n",
1155+
" val = nums[i+1]\n",
1156+
" for i in range(freq):\n",
1157+
" outList.append(val)\n",
1158+
" return outList\n",
1159+
" \n",
1160+
" \n",
1161+
"sol = Solution();\n",
1162+
"print(sol.decompressRLElist([1,2,3,4]))"
1163+
]
1164+
},
11181165
{
11191166
"cell_type": "code",
11201167
"execution_count": null,

0 commit comments

Comments
 (0)