This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution: | |
def minScore(self, values: List[int], n, used, usedctr) -> int: | |
if usedctr == n - 2: | |
return 0 | |
if used in self.cache: | |
return self.cache[used] | |
minScore = float('inf') | |
for i in range(n): | |
if not used & (1 << i + 1): | |
l = (n + i - 1) % n |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
void main() | |
{ | |
int n = 10; | |
int hollow = 1; | |
int isSpace; | |
printf("Edge length? "); | |
scanf("%d", &n); | |
printf("Is Hollow?\n 0. No\n 1. Yes\n"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution: | |
def knapsack(self, knapsack, target, i, n): | |
if target < 0: | |
return -1 | |
if i >= n: | |
return 0 | |
key = (i, target) | |
if key in self.cache: | |
return self.cache[key] | |
a = 1 + self.knapsack(knapsack, target - knapsack[i], i + 1, n) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from collections import defaultdict | |
class Solution: | |
def makeSeg(self, arr, i, j): | |
seg = self.seg | |
if (i, j) in seg: | |
return seg[(i, j)] | |
if i == j: | |
seg[(i, j)] = arr[i] | |
return arr[i] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
def partition(arr, k): | |
n = len(arr) | |
i = -1 | |
for j in range(n): | |
if arr[j] <= k: | |
i += 1 | |
arr[i], arr[j] = arr[j], arr[i] | |
return i |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from collections import defaultdict | |
class Solution: | |
def DFS(self, graph, node, v, s): | |
v.add(node) | |
for j in graph[node]: | |
if j not in v: | |
self.DFS(graph, j, v, s) | |
s.append(node) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution: | |
def first_missing_positive(self, a): | |
a = set(a) | |
a.add(0) | |
for i in range(1, max(a) + 2): | |
if i not in a: | |
return i |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution: | |
def product_exclude_itself(self, nums): | |
n = len(nums) | |
l = [1] | |
r = [1] | |
for i in range(n): | |
l.append(l[-1] * nums[i]) | |
r.append(r[-1] * nums[n - i - 1]) | |
return [l[i] * r[n - i - 1] for i in range(n)] | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution: | |
def isLCP(self, strs, k): | |
return len(set([s[:k] for s in strs])) == 1 | |
def longest_common_prefix(self, strs): | |
n = len(strs) | |
if n == 0: | |
return "" | |
beg = 0 | |
end = min([len(s) for s in strs]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution: | |
def getPos(self, board, i, j): | |
pos = set(map(str, range(1, 10))) | |
ci, cj = i - i % 3, j - j % 3 | |
for a in range(3): | |
for b in range(3): | |
if board[ci + a][cj + b] != "." and board[ci + a][cj + b] in pos: | |
pos.remove(board[ci + a][cj + b]) | |
for a in range(9): | |
if board[i][a] in pos: |