# A. Largest Local Values in a Matrix (opens new window)

Brute force, since the given matrix is small. For each position, find the largest number from the surrounding 9 cells (inclusive).

DETAILS
def largestLocal(self, A: List[List[int]]) -> List[List[int]]:
        n = len(A)
        ans = [[0] * (n - 2) for _ in range(n - 2)]
        
        for i in range(n - 2):
            for j in range(n - 2):
                ans[i][j] = max(ans[i][j], max(A[i][j:j + 3]))
                ans[i][j] = max(ans[i][j], max(A[i + 1][j:j + 3]))
                ans[i][j] = max(ans[i][j], max(A[i + 2][j:j + 3]))
        
        return ans

# B. Node With Highest Edge Score (opens new window)

For each pair (a, b), add b to a's value.

DETAILS
def edgeScore(self, A: List[int]) -> int:
        n = len(A)
        ans = [[0, i] for i in range(n)]
        
        for i, a in enumerate(A):
            ans[a][0] += i

        ans.sort(key = lambda x: [-x[0], x[1]])
        
        return ans[0][1]

# C. Construct Smallest Number From DI String (opens new window)

Stack

DETAILS
def smallestNumber(self, pattern: str) -> str:
        ans = [1]
        for p in pattern: 
            if p == 'I': 
                cur = ans[-1] + 1
                while cur in ans: 
                    cur += 1
                ans.append(cur)
            else: 
                ans.append(ans[-1])
                for i in range(len(ans) - 1, 0, -1): 
                    if ans[i - 1] == ans[i]: 
                        ans[i - 1] += 1
        return ''.join(map(str, ans))

# D. Count Special Integers (opens new window)

DETAILS
def countSpecialNumbers(self, N: int) -> int:
        L = list(map(int, str(N + 1)))
        n, s = len(L), set()
        res = sum(9 * perm(9, i) for i in range(n - 1))
        
        for i, x in enumerate(L):
            for y in range(i == 0, x):
                if y not in s:
                    res += perm(9 - i, n - i - 1)
            if x in s: break
            s.add(x)
        return res