Leetcode
in Study Blog on Coding Interview
Two sum
- Hashmap
- Add two numbers
- Linked List
- Longest Substring Without Repeating Characters
- Two pointers
- Median of Two Sorted Arrays
- Median
- Binary Search
- Longest Palindromic Substring
- DP
- Java vs Python
- Regular Expression Matching
- Recursion
- Container With Most Water
- Two pointers
- 3 Sum
- 2 sum
- Hashmap
- sort
- Letter Combinations of a Phone Number
- Hashmap
- Recursion
- Remove Nth Node From End of List
- Symmetric
- Two pointers
- Valid Parentheses
- Stack
- Hashmap for pair
- Generate Parentheses
- DFS
- Stack
- DP
- Merge k Sorted Lists
- Priority Queue
from Queue import PriorityQueue
- Python
sorted(list)
- Priority Queue
- Reverse Nodes in k-Group
- Reverse list
- 6 pointers: dummy, jump, l, r, prev, cur
- Next Permutation
- Find Pattern:
nums[i-1] < nums[i]
- Decreasing List
- Find Pattern:
- Longest Valid Parentheses
- Find Pattern: If ‘)’ more than ‘(‘, reset
- Stack
- Two traverse
- Search in Rotated Sorted Array
- Binary Search
- Mind
left <= mid
sincemid = (left + right)//2
- Find First and Last Position of Element in Sorted Array
- Binary Search
- Find left most:
left = mid + 1, right = mid
python = while left < right: mid = (left + right) // 2 if nums[mid] < target: left = mid + 1 else: right = mid
- Find right most:
left = mid, right = mid - 1
python = while left < right: mid = (left + right + 1) // 2 if nums[mid] > target: right = mid - 1 else: left = mid
- Search Insert Position
- Binary Search
- Combination Sum
- Backtracking/DFS
def dfs(nums, target, path, res): if target < 0: return elif target == 0: res.append(path) else: for i in range(len(nums)): dfs(nums[i:], target - nums[i], path + [nums[i]], res)
- Backtracking/DFS
- First Missing Positive
- Find Pattern:
[1,2,...,n+1]
- Hash
nums[nums[i]%n] += n
- Find Pattern:
- Trapping Rain Water
- DP: store leftMax and rightMax
- Two pointers
- Jump Game II
- Two pointers: left for n steps, right for n+1 steps
- Permutations
- DFS
- Rotate Image
- List transportation with
zip()
rotate = flip + trans
- List transportation with
- Group Anagrams
- permutations have the same characters:
sorted()
+dict
- permutations have the same characters:
- Maximum Subarray
- Divide and Conquer
- DP
- Jump Game
- two pointers
- from end to start: where is the last reachable point
- Merge Intervals
- Graph
sort()
+max()
- Unique Paths
- DP
- Math $C_m^n$
- Minimum Path Sum
- DP
- Climbing Stairs
- DP
- Edit Distance
- DP
- Search a 2D Matrix
- Binary Search: $m \times n$ sorted array
- if element not in list, find the left boundary:
mid = (left + right)//2
,right -= 1
- Sort Colors
- count sort
- *Dutch National Flag Problem
- Minimum Window Substring
- Sliding window, two pointers
- Subsets
- Backtracking
- Bitmask
- DP
- Word Search
- Backtracking / DFS
OA
- Merge Sort: Counting Inversions
#!/bin/python3
import math
import os
import random
import re
import sys
#
# Complete the 'countInversions' function below.
#
# The function is expected to return a LONG_INTEGER.
# The function accepts INTEGER_ARRAY arr as parameter.
#
def countInversions(arr):
# Write your code here
temp_arr = [0]*len(arr)
return mergeSort(arr, temp_arr, 0, len(arr) - 1)
def mergeSort(arr, temp_arr, left, right):
inv_cnt = 0
if left < right:
mid = (left + right) // 2
inv_cnt += mergeSort(arr, temp_arr, left, mid)
inv_cnt += mergeSort(arr, temp_arr, mid + 1, right)
inv_cnt += merge(arr, temp_arr, left, mid, right)
return inv_cnt
def merge(arr, temp_arr, left, mid, right):
i, j, k = left, mid+1, left
inv_cnt = 0
while i <= mid and j <= right:
if arr[i] > arr[j]:
inv_cnt += mid - i + 1
temp_arr[k] = arr[j]
j += 1
else:
temp_arr[k] = arr[i]
i += 1
k += 1
while i <= mid:
temp_arr[k] = arr[i]
i += 1
k += 1
while j <= right:
temp_arr[k] = arr[j]
j += 1
k += 1
for x in range(left, right + 1):
arr[x] = temp_arr[x]
return inv_cnt
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
t = int(input().strip())
for t_itr in range(t):
n = int(input().strip())
arr = list(map(int, input().rstrip().split()))
result = countInversions(arr)
fptr.write(str(result) + '\n')
fptr.close()
Stars and Bars: ```python= def stars_and_bars(s, startIndex, endIndex): bars = {}
cnt = 0 for i in range(len(s)): if s[i] == “|”: bars[i] = cnt cnt += 1 lst = list(bars) startIndex, endIndex = [x-1 for x in startIndex],
[x-1 for x in endIndex] start_bar, end_bar = [], []for i in range(len(startIndex)): start_bar.append(binary_start(lst, startIndex[i])) for i in range(len(endIndex)): end_bar.append(binary_end(lst, endIndex[i]))
print(bars) print(start_bar, end_bar)
res = 0 for i in range(len(start_bar)):
res += max(0, (end_bar[i] - start_bar[i] - 1) - \ (bars[end_bar[i]] - bars[start_bar[i]] - 1))
return res
def binary_start(nums, target): left, right = 0, len(nums) -1
while left < right:
mid = (left+right)//2
if nums[mid] == target:
return nums[mid]
elif nums[mid] > target:
right = mid
else:
left = mid + 1
return nums[right]
def binary_end(nums, target): left, right = 0, len(nums) -1
while left < right:
mid = (left+right+1)//2
if nums[mid] == target:
return nums[mid]
elif nums[mid] > target:
right = mid - 1
else:
left = mid
return nums[left] ```