@@ -437,9 +437,9 @@ def maxSum4NonAdjacent(nums):
437437 return dp1
438438
439439
440- def test__9 ():
441- print ( maxSum4NonAdjacent ([2 , 4 , 6 , 2 , 5 ]))
442- print ( maxSum4NonAdjacent ([10 , - 1 , 1 , 2 ]))
440+ def test_9 ():
441+ assert maxSum4NonAdjacent ([2 , 4 , 6 , 2 , 5 ]) == 13
442+ assert maxSum4NonAdjacent ([10 , - 1 , 1 , 2 ]) == 12
443443
444444
445445"""
@@ -1344,6 +1344,121 @@ def test_23():
13441344 assert getMinSteps (matrix , (3 , 0 ), (0 , 0 )) == 7
13451345
13461346
1347+ """
1348+ question 24
1349+ Implement locking in a binary tree. A binary tree node can be locked or
1350+ unlocked only if all of its descendants or ancestors are not locked.
1351+
1352+ Design a binary tree node class with the following methods:
1353+
1354+ is_locked, which returns whether the node is locked
1355+ lock, which attempts to lock the node. If it cannot be locked, then it should
1356+ return false. Otherwise, it should lock it and return true.
1357+
1358+ unlock, which unlocks the node. If it cannot be unlocked, then it should
1359+ return false. Otherwise, it should unlock it and return true.
1360+
1361+ You may augment the node to add parent pointers or any other property you
1362+ would like. You may assume the class is used in a single-threaded program,
1363+ so there is no need for actual locks or mutexes. Each method should run in
1364+ O(h), where h is the height of the tree.
1365+ -------------------
1366+
1367+ Based on the description of the problem, the lock/unlock operation can be
1368+ called from any node but all its accestors or decendents needs to be in the
1369+ same status: unlocked or locked. That means the entire binary tree needs to
1370+ behavior consistently. The entire binary tree is either locked or unlocked.
1371+
1372+ Store the locked/unlocked status in the root node. When try to lock/unlock
1373+ from any node, travel to the root node and check the status and act accordingly.
1374+ We need to add parent pointer to the tree nodes, so traveling from any node to
1375+ the root node takes O(h) time.
1376+ """
1377+
1378+
1379+ def test_24 ():
1380+ pass
1381+
1382+
1383+ """
1384+ question 25
1385+ Implement regular expression matching with the following special characters:
1386+
1387+ . (period) which matches any single character
1388+ * (asterisk) which matches zero or more of the preceding element
1389+ That is, implement a function that takes in a string and a valid regular
1390+ expression and returns whether or not the string matches the regular expression.
1391+
1392+ For example, given the regular expression "ra." and the string "ray", your
1393+ function should return true. The same regular expression on the string
1394+ "raymond" should return false.
1395+
1396+ Given the regular expression ".*at" and the string "chat", your function should
1397+ return true. The same regular expression on the string "chats" should return false.
1398+ -------------------
1399+
1400+ 1. recursion and backtracking
1401+ input: pattern, text
1402+ f(i1, i2):
1403+ for ., i1++, i2++
1404+ for *, i1++, i2: i2, i2+1,...i2+n
1405+ others: if pattern[i1] == text[i2]: i1++, i2++; otherwise return
1406+
1407+ 2. In solution #1 we might calculate the same f(i1, i2) multiple times.
1408+ To reduce the duplication, use DP.
1409+
1410+ populate the DP matrix row by row, only process the up-right half
1411+ c h s a t s a
1412+ c y n n n n n n
1413+ . y y y y y y
1414+ * y y y y y
1415+ t n y n n
1416+ . n y y
1417+
1418+ for any cell, except (0,0), two possible directions to reach here:
1419+ 1. from up-left
1420+ 2. from left, only when current char in the pattern is '.'.
1421+
1422+ time O(N*M), space O(M)
1423+ """
1424+
1425+
1426+ def regexMatched (pattern : str , text : str ) -> bool :
1427+ N , M = len (pattern ), len (text )
1428+ if N > M :
1429+ return False
1430+ pre = [0 ] * M
1431+ cur = [0 ] * M
1432+ for row in range (0 , N ):
1433+ p = pattern [row ]
1434+ for col in range (row , M ):
1435+ t = text [col ]
1436+ # reach from up-left
1437+ if (col == 0 or pre [col - 1 ]) and (p == "*" or p == t or p == "." ):
1438+ cur [col ] = 1
1439+ # reach from left
1440+ elif (col == 0 or cur [col - 1 ]) and p == "." :
1441+ cur [col ] = 1
1442+ # no valid path in this row, no chance to match, fail early
1443+ if sum (cur ) == 0 :
1444+ return False
1445+ pre = cur
1446+ cur = [0 ] * M
1447+ return pre [M - 1 ] == 1
1448+
1449+
1450+ def test_25 ():
1451+ assert regexMatched ("c.*t." , "chsatsa" )
1452+ assert regexMatched (".*at" , "chat" )
1453+ assert regexMatched (".*at" , "sdfdedat" )
1454+ assert not regexMatched ("chi." , "chat" )
1455+ assert regexMatched ("ch*." , "chatsesd" )
1456+ assert regexMatched ("*." , "blablabla" )
1457+ assert regexMatched (".a*" , "blablablaay" )
1458+ assert not regexMatched ("*a." , "blablablaay" )
1459+
1460+
1461+ test_25 ()
13471462"""
13481463question 32 TODO
13491464This problem was asked by Jane Street.
0 commit comments