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 | |
| #a = [14, 16, 10, 19, 21, 1, 4, 8, 3, 2, 7, 6] | |
| #target = 15 | |
| a = [1, 1, 1] | |
| target = 3 | |
| def subarray_sum(arr: List[int], target: int) -> List[int]: | |
| i = j = 0 | |
| s = 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
| class Solution: | |
| def lengthOfLongestSubstring(self, s: str) -> int: | |
| start = end = 0 | |
| current_chars = set() | |
| length = max_length = 0 | |
| while end < len(s): | |
| if s[end] not in current_chars: | |
| current_chars.add(s[end]) | |
| length += 1 | |
| if length > max_length: |
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 reverse_number(n): | |
| multiplier = 1 | |
| result = 0 | |
| while n != 0: | |
| if n > 0: | |
| last = n % 10 | |
| n = n // 10 | |
| else: | |
| last = n % 10 - 10 | |
| n = n // 10 + 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
| def delete_nth(order,max_e): | |
| result = [] | |
| for index in range(len(order)): | |
| if result.count(order[index]) < max_e: | |
| result.append(order[index]) | |
| return 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
| class Solution: | |
| def decodeString(self, s: str) -> str: | |
| n_times = '' | |
| text_so_far = '' | |
| stack = [] | |
| for c in s: | |
| if c.isdigit(): | |
| n_times += c |
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 trap(self, height: List[int]) -> int: | |
| i, j = 0, len(height) - 1 | |
| max_h_i, max_h_j = 0, 0 | |
| sum_h = 0 | |
| while i < j: | |
| if height[i] < max_h_i: | |
| sum_h += max_h_i - height[i] | |
| else: |
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 find_next_top(self, height, current_i, left_bound, right_bound, current_top_value, direction): | |
| sum_height = 0 | |
| while left_bound <= current_i < right_bound and height[current_i] < current_top_value: | |
| sum_height += current_top_value - height[current_i] | |
| current_i += direction | |
| return current_i, sum_height | |
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 canFinish(self, numCourses: int, prerequisites: List[List[int]]) -> bool: | |
| reqs = defaultdict(list) | |
| for course, req in prerequisites: | |
| reqs[course].append(req) | |
| visited = set() | |
| def detect_cycle(i, visiting): | |
| visiting.add(i) | |
| for j in reqs[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 find_size(self, s: str) -> int: | |
| balance = 0 | |
| size = 0 | |
| for c in s: | |
| if c not in "()": | |
| size += 1 | |
| elif c == '(': | |
| balance += 1 | |
| elif c == ')': |
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 deque | |
| ONE = '1' | |
| class Solution: | |
| def numIslands(self, grid): | |
| width = len(grid) | |
| height = len(grid[0]) if width != 0 else 0 | |
| drc = [(-1, 0), (0, 1), (1, 0) , (0, -1)] | |