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
| # There is a fence with n posts, each post can be painted with one of the k colors. | |
| # You have to paint all the posts such that no more than two adjacent fence posts have the same color. | |
| # Return the total number of ways you can paint the fence. | |
| from functools import lru_cache | |
| class Solution: | |
| @lru_cache(maxsize=None) | |
| def ways(self, a, b, p, n, k): | |
| if p == 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: | |
| """ | |
| @param: strs: a list of strings | |
| @return: encodes a list of strings to a single string. | |
| """ | |
| def encode(self, strs): | |
| _delim = "!@#$%^&*" | |
| delim = _delim | |
| check = "".join(strs) | |
| i = 1 |
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
| t = int(input()) | |
| def getSmallest(word): | |
| n = len(word) | |
| if n == 1: | |
| return word | |
| nchunk = getSmallest(word[1:]) | |
| return min(word[0] + nchunk, word[0] * 2 + nchunk) | |
| for tt in range(1, t + 1): |
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 Counter | |
| import heapq | |
| class Node: | |
| def __init__(self, char = None, left = None, right = None): | |
| self.char = char | |
| self.left = left | |
| self.right = right | |
| def __gt__(self, 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
| # modexp(b, n, M) => (b ^ n) % M | |
| def modexp(b, n, M): | |
| if n == 0: | |
| return 1 % M | |
| k = modexp(b, n >> 1, M) ** 2 | |
| if n & 1: | |
| return (b * k) % M | |
| else: | |
| return k % M |
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
| r = input() | |
| if float(r) <= 0: | |
| print(0) | |
| else: | |
| print("{:.2f}".format(eval("3.14 * {} * {}".format(r, r)))) |
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
| def isPerfectSquare(num): | |
| beg = 0 | |
| end = num | |
| while beg <= end: | |
| mid = (beg + end) // 2 | |
| if mid * mid == num: | |
| return True | |
| elif beg == end: | |
| break | |
| elif mid * mid > num: |
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
| t = int(input()) | |
| for _ in range(t): | |
| n = int(input()) | |
| men = set(range(1, n + 1)) | |
| curr = 1 | |
| while len(men) > 1: | |
| curr = 1 + curr % n | |
| while curr not in men: | |
| curr = 1 + curr % 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
| t = int(input()) | |
| for _ in range(t): | |
| n = int(input()) | |
| arr = input().split() | |
| arr = [int(el) for el in arr] | |
| total = sum(arr) | |
| sums = [0] | |
| mindiff = float('inf') | |
| for el in arr: |
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
| t = int(input()) | |
| def numWays(n, r): | |
| ways = 0 | |
| if r == 1: | |
| return 1 | |
| if n == 0: | |
| return ways | |
| for i in range(1, n - r + 2): | |
| ways += numWays(n - i, r - 1) |