Skip to content

Commit cfa9a82

Browse files
committed
W-345
1 parent ea95dc2 commit cfa9a82

File tree

1 file changed

+243
-0
lines changed

1 file changed

+243
-0
lines changed
Lines changed: 243 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,243 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 102,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"from typing import List\n",
10+
"\n",
11+
"class Solution:\n",
12+
" def circularGameLosers(self, n: int, k: int) -> List[int]:\n",
13+
" d = {i:0 for i in range(1, n+1)}\n",
14+
" d[1] = 1\n",
15+
" holding = 1\n",
16+
" i = 1\n",
17+
" while True:\n",
18+
" if d[holding] > 1: break\n",
19+
" holding = (holding+(i*k))%n\n",
20+
" if holding==0:\n",
21+
" d[n]+=1\n",
22+
" holding = n\n",
23+
" else:\n",
24+
" d[holding]+=1\n",
25+
" i+=1\n",
26+
" return sorted([k for k,v in d.items() if v==0])\n",
27+
" "
28+
]
29+
},
30+
{
31+
"cell_type": "code",
32+
"execution_count": 103,
33+
"metadata": {},
34+
"outputs": [
35+
{
36+
"data": {
37+
"text/plain": [
38+
"[3]"
39+
]
40+
},
41+
"execution_count": 103,
42+
"metadata": {},
43+
"output_type": "execute_result"
44+
}
45+
],
46+
"source": [
47+
"n = 5; k = 2\n",
48+
"n = 4; k = 4\n",
49+
"n = 1; k = 1\n",
50+
"n = 3; k = 1\n",
51+
"Solution().circularGameLosers(n,k)"
52+
]
53+
},
54+
{
55+
"cell_type": "code",
56+
"execution_count": 151,
57+
"metadata": {},
58+
"outputs": [],
59+
"source": [
60+
"class Solution:\n",
61+
" def doesValidArrayExist(self, derived: List[int]) -> bool:\n",
62+
" n = len(derived)\n",
63+
" original = [0] * n\n",
64+
" for i in range(n):\n",
65+
" if i == n - 1:\n",
66+
" original[i] = derived[i] ^ derived[0]\n",
67+
" else:\n",
68+
" original[i] = derived[i] ^ derived[i + 1]\n",
69+
" return original, derived\n",
70+
" for i in original:\n",
71+
" if i == 0 or i == 1:\n",
72+
" continue\n",
73+
" else:\n",
74+
" return False\n",
75+
" return True"
76+
]
77+
},
78+
{
79+
"cell_type": "code",
80+
"execution_count": 156,
81+
"metadata": {},
82+
"outputs": [
83+
{
84+
"data": {
85+
"text/plain": [
86+
"([0, 0], [1, 1])"
87+
]
88+
},
89+
"execution_count": 156,
90+
"metadata": {},
91+
"output_type": "execute_result"
92+
}
93+
],
94+
"source": [
95+
"derived = [1,1]\n",
96+
"#derived = [1,1,0]\n",
97+
"#derived = [1,0]\n",
98+
"Solution().doesValidArrayExist(derived)"
99+
]
100+
},
101+
{
102+
"cell_type": "code",
103+
"execution_count": 211,
104+
"metadata": {},
105+
"outputs": [],
106+
"source": [
107+
"from functools import cache\n",
108+
"\n",
109+
"class Solution:\n",
110+
" def maxMoves(self, grid):\n",
111+
" m,n = len(grid), len(grid[0])\n",
112+
" \n",
113+
" @cache\n",
114+
" def dfs(row, col):\n",
115+
" max_len = 0\n",
116+
" for dr in [-1, 0, 1]:\n",
117+
" adj_row = row + dr\n",
118+
" adj_col = col + 1\n",
119+
" if 0 <= adj_row < m and 0 <= adj_col < n and grid[adj_row][adj_col] > grid[row][col]:\n",
120+
" max_len = max(max_len, dfs(adj_row, adj_col) + 1)\n",
121+
"\n",
122+
" return max_len\n",
123+
"\n",
124+
" \n",
125+
" return max(dfs(row, 0) for row in range(m))\n",
126+
" "
127+
]
128+
},
129+
{
130+
"cell_type": "code",
131+
"execution_count": 213,
132+
"metadata": {},
133+
"outputs": [
134+
{
135+
"data": {
136+
"text/plain": [
137+
"3"
138+
]
139+
},
140+
"execution_count": 213,
141+
"metadata": {},
142+
"output_type": "execute_result"
143+
}
144+
],
145+
"source": [
146+
"grid = [[2,4,3,5],[5,4,9,3],[3,4,2,11],[10,9,13,15]]\n",
147+
"#grid = [[3,2,4],[2,1,9],[1,1,7]]\n",
148+
"Solution().maxMoves(grid)"
149+
]
150+
},
151+
{
152+
"cell_type": "code",
153+
"execution_count": null,
154+
"metadata": {},
155+
"outputs": [],
156+
"source": []
157+
},
158+
{
159+
"cell_type": "code",
160+
"execution_count": null,
161+
"metadata": {},
162+
"outputs": [],
163+
"source": []
164+
},
165+
{
166+
"cell_type": "code",
167+
"execution_count": 205,
168+
"metadata": {},
169+
"outputs": [],
170+
"source": [
171+
"class Solution:\n",
172+
" def countCompleteComponents(self, n: int, edges: List[List[int]]) -> int:\n",
173+
" \n",
174+
" graph = [[] for _ in range(n)]\n",
175+
" visited = [False] * n\n",
176+
" components = 0\n",
177+
"\n",
178+
" # Build the adjacency list representation of the graph\n",
179+
" for edge in edges:\n",
180+
" u, v = edge\n",
181+
" graph[u].append(v)\n",
182+
" graph[v].append(u)\n",
183+
"\n",
184+
" def dfs(node):\n",
185+
" visited[node] = True\n",
186+
" for neighbor in graph[node]:\n",
187+
" if not visited[neighbor]:\n",
188+
" dfs(neighbor)\n",
189+
"\n",
190+
" for node in range(n):\n",
191+
" if not visited[node]:\n",
192+
" dfs(node)\n",
193+
" components += 1\n",
194+
"\n",
195+
" return components"
196+
]
197+
},
198+
{
199+
"cell_type": "code",
200+
"execution_count": 206,
201+
"metadata": {},
202+
"outputs": [
203+
{
204+
"data": {
205+
"text/plain": [
206+
"2"
207+
]
208+
},
209+
"execution_count": 206,
210+
"metadata": {},
211+
"output_type": "execute_result"
212+
}
213+
],
214+
"source": [
215+
"n = 6; edges = [[0,1],[0,2],[1,2],[3,4],[3,5]]\n",
216+
"#n = 6; edges = [[0,1],[0,2],[1,2],[3,4]]\n",
217+
"Solution().countCompleteComponents(n,edges)"
218+
]
219+
}
220+
],
221+
"metadata": {
222+
"kernelspec": {
223+
"display_name": "base",
224+
"language": "python",
225+
"name": "python3"
226+
},
227+
"language_info": {
228+
"codemirror_mode": {
229+
"name": "ipython",
230+
"version": 3
231+
},
232+
"file_extension": ".py",
233+
"mimetype": "text/x-python",
234+
"name": "python",
235+
"nbconvert_exporter": "python",
236+
"pygments_lexer": "ipython3",
237+
"version": "3.9.13"
238+
},
239+
"orig_nbformat": 4
240+
},
241+
"nbformat": 4,
242+
"nbformat_minor": 2
243+
}

0 commit comments

Comments
 (0)