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 minimum_cost_array(red, blue, blue_cost): | |
| assert len(red) == len(blue), f"red and blue lines have different sizes" | |
| result = [0] | |
| is_blue = False | |
| for r, b in zip(red, blue): | |
| cost_blue = b if is_blue else b + blue_cost | |
| cost_red = r | |
| if cost_blue <= cost_red: |
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 functools import lru_cache | |
| class Solution: | |
| def change(self, amount: int, coins: List[int]) -> int: | |
| scoins = sorted(coins, reverse=True) | |
| @lru_cache | |
| def dfs(amt, index): | |
| if amt == 0: return 1 | |
| if amt < 0: return 0 |
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 functools import lru_cache | |
| class Solution: | |
| def change(self, amount: int, coins: List[int]) -> int: | |
| @lru_cache | |
| def dfs(amt): | |
| if amt == 0: return 1 | |
| if amt < 0: return 0 | |
| return sum(dfs(amt - coins[i]) for i in range(len(coins))) |
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
| # for any natural number return the ZECK - | |
| # Given N - return non consecutive list of fibonacci numbers that sum up to N | |
| # 0 1 1 2 3 5 8 13 21 34 55 89 | |
| # 10 -> [8 2] | |
| # 62 -> [2, 5, 55], |
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 heapq | |
| class Solution: | |
| def kClosest(self, points: List[List[int]], k: int) -> List[List[int]]: | |
| points_gen = ((-(x*x+y*y), (x, y)) for x, y in points) | |
| result = [] | |
| for point in points_gen: | |
| heapq.heappush(result, point) | |
| if len(result) > k: | |
| heapq.heappop(result) |
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 math | |
| class Solution: | |
| def productExceptSelf(self, nums: List[int]) -> List[int]: | |
| result = [0] * len(nums) | |
| product = 1 | |
| for i in range(len(nums) -1, -1, -1): | |
| product *= nums[i] | |
| result[i] = product | |
| product = 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
| class Solution: | |
| def climbStairs(self, n: int) -> int: | |
| dp = [1] * n | |
| for i in range(1, n): | |
| n_ways_previous = dp[i-1] | |
| n_ways_previous_2 = dp[i-2] if i - 2 >= 0 else 1 | |
| dp[i] = n_ways_previous + n_ways_previous_2 | |
| return dp[-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 typing import List | |
| class Solution: | |
| def insertInterval(self, intervals: List[List[int]], newInterval: List[int]) -> List[List[int]]: | |
| def overlap(interval1, interval2): | |
| # print(f"interval1 {interval1}, interval2 {interval2}") | |
| return interval2[1] >= interval1[0] and interval2[0] <= interval1[1] | |
| result = [] | |
| i = 0 | |
| while i < len(intervals) and not overlap(intervals[i], newInterval) and intervals[i] < newInterval: |
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 functools import lru_cache | |
| class Solution: | |
| def longestCommonSubsequence(self, word1: str, word2: str) -> int: | |
| @lru_cache(None) | |
| def dfs(i, j): | |
| if i == len(word1) or j == len(word2): | |
| return 0 |
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 functools import lru_cache | |
| class Solution: | |
| def subset_sum(self, target: int, nums: List[int]): | |
| visited = set() | |
| @lru_cache(None) | |
| def dfs(target_sum): | |
| # print(f"target_sum = {target_sum} nums = {nums}") |