Skip to content

Commit 5dda7d3

Browse files
committed
Add all
1 parent 232aace commit 5dda7d3

File tree

3 files changed

+155
-0
lines changed

3 files changed

+155
-0
lines changed

2024/meta/prep.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1341,6 +1341,86 @@ def dfs(x, y, index):
13411341

13421342
return max_island
13431343

1344+
############# 339. Nested List Weight Sum ############
1345+
# """
1346+
# This is the interface that allows for creating nested lists.
1347+
# You should not implement it, or speculate about its implementation
1348+
# """
1349+
#class NestedInteger:
1350+
# def __init__(self, value=None):
1351+
# """
1352+
# If value is not specified, initializes an empty list.
1353+
# Otherwise initializes a single integer equal to value.
1354+
# """
1355+
#
1356+
# def isInteger(self):
1357+
# """
1358+
# @return True if this NestedInteger holds a single integer, rather than a nested list.
1359+
# :rtype bool
1360+
# """
1361+
#
1362+
# def add(self, elem):
1363+
# """
1364+
# Set this NestedInteger to hold a nested list and adds a nested integer elem to it.
1365+
# :rtype void
1366+
# """
1367+
#
1368+
# def setInteger(self, value):
1369+
# """
1370+
# Set this NestedInteger to hold a single integer equal to value.
1371+
# :rtype void
1372+
# """
1373+
#
1374+
# def getInteger(self):
1375+
# """
1376+
# @return the single integer that this NestedInteger holds, if it holds a single integer
1377+
# Return None if this NestedInteger holds a nested list
1378+
# :rtype int
1379+
# """
1380+
#
1381+
# def getList(self):
1382+
# """
1383+
# @return the nested list that this NestedInteger holds, if it holds a nested list
1384+
# Return None if this NestedInteger holds a single integer
1385+
# :rtype List[NestedInteger]
1386+
# """
1387+
1388+
class Solution:
1389+
def depthSum(self, nestedList: List[NestedInteger]) -> int:
1390+
def dfs(ls, depth):
1391+
s = 0
1392+
for element in ls:
1393+
if element.isInteger():
1394+
s += depth * element.getInteger()
1395+
else:
1396+
s += dfs(element.getList(), depth+1)
1397+
return s
1398+
1399+
return dfs(nestedList, 1)
1400+
1401+
1402+
############### 498. Diagonal Traverse ###############
1403+
from collections import defaultdict
1404+
class Solution:
1405+
def findDiagonalOrder(self, mat: List[List[int]]) -> List[int]:
1406+
"""
1407+
2 key observations:
1408+
1. Diagonals are defined by the sum of indicies in a 2 dimensional array
1409+
2. The snake phenomena can be achieved by reversing every other diagonal level, therefore check if divisible by 2
1410+
"""
1411+
d = defaultdict(list)
1412+
for i in range(len(mat)):
1413+
for j in range(len(mat[i])):
1414+
d[i+j].append(mat[i][j])
1415+
ans = []
1416+
for entry in d.items():
1417+
if entry[0] % 2 == 0:
1418+
ans.extend(entry[1][::-1])
1419+
else:
1420+
ans.extend(entry[1])
1421+
return ans
1422+
1423+
13441424

13451425
############# 269. Alien Dictionary ############
13461426
from collections import defaultdict, deque
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
2+
# """
3+
# This is the interface that allows for creating nested lists.
4+
# You should not implement it, or speculate about its implementation
5+
# """
6+
#class NestedInteger:
7+
# def __init__(self, value=None):
8+
# """
9+
# If value is not specified, initializes an empty list.
10+
# Otherwise initializes a single integer equal to value.
11+
# """
12+
#
13+
# def isInteger(self):
14+
# """
15+
# @return True if this NestedInteger holds a single integer, rather than a nested list.
16+
# :rtype bool
17+
# """
18+
#
19+
# def add(self, elem):
20+
# """
21+
# Set this NestedInteger to hold a nested list and adds a nested integer elem to it.
22+
# :rtype void
23+
# """
24+
#
25+
# def setInteger(self, value):
26+
# """
27+
# Set this NestedInteger to hold a single integer equal to value.
28+
# :rtype void
29+
# """
30+
#
31+
# def getInteger(self):
32+
# """
33+
# @return the single integer that this NestedInteger holds, if it holds a single integer
34+
# Return None if this NestedInteger holds a nested list
35+
# :rtype int
36+
# """
37+
#
38+
# def getList(self):
39+
# """
40+
# @return the nested list that this NestedInteger holds, if it holds a nested list
41+
# Return None if this NestedInteger holds a single integer
42+
# :rtype List[NestedInteger]
43+
# """
44+
45+
class Solution:
46+
def depthSum(self, nestedList: List[NestedInteger]) -> int:
47+
def dfs(ls, depth):
48+
s = 0
49+
for element in ls:
50+
if element.isInteger():
51+
s += depth * element.getInteger()
52+
else:
53+
s += dfs(element.getList(), depth+1)
54+
return s
55+
56+
return dfs(nestedList, 1)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from collections import defaultdict
2+
class Solution:
3+
def findDiagonalOrder(self, mat: List[List[int]]) -> List[int]:
4+
"""
5+
2 key observations:
6+
1. Diagonals are defined by the sum of indicies in a 2 dimensional array
7+
2. The snake phenomena can be achieved by reversing every other diagonal level, therefore check if divisible by 2
8+
"""
9+
d = defaultdict(list)
10+
for i in range(len(mat)):
11+
for j in range(len(mat[i])):
12+
d[i+j].append(mat[i][j])
13+
ans = []
14+
for entry in d.items():
15+
if entry[0] % 2 == 0:
16+
ans.extend(entry[1][::-1])
17+
else:
18+
ans.extend(entry[1])
19+
return ans

0 commit comments

Comments
 (0)