# DFS&BFS
from collections import deque
class Node:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
def dfs(node):
if not node:
return None
dfs(node.left)
print(node.val)
dfs(node.right)
def bfs(root):
if not root:
return None
q = deque()
q.append(root)
while q:
node = q.popleft()
print(node.val)
if node.left:
q.append(node.left)
if node.right:
q.append(node.right)
root = Node(4)
root.left = Node(3)
root.right = Node(5)
root.left.left = Node(1)
root.left.right = Node(2)
dfs(root)
bfs(root)
# DP
def rob(houses):
h1 = houses[0]
h2 = max(houses[0], houses[1])
for i in range(2, len(houses)):
h3 = max(houses[i]+h1, h2)
h1 = h2
h2 = h3
return h2
def rob_circle(houses):
return max(rob(houses[1:]), rob(houses[:-1]))
def maxSubArray(nums):
curr_sum = nums[0]
max_sum = nums[0]
for i in range(1, len(nums)):
curr_sum = max(curr_sum+nums[i], nums[i])
if curr_sum > max_sum:
max_sum = curr_sum
return max_sum
def maxSubArray_circle(nums):
curr_max = total_max =nums[0]
curr_min = total_min =nums[0]
totals = nums[0]
for i in range(1, len(nums)):
curr_max = max(curr_max+nums[i], nums[i])
if curr_max > total_max:
total_max = curr_max
curr_min = min(curr_min + nums[i], nums[i])
if curr_min < total_min:
total_min = curr_min
totals += nums[i]
return max(total_max, totals-total_min) if total_max > 0 else total_max
# Linked List
class Node:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
def merge(list1, list2):
new_head = dummy_head = Node()
while list1 and list2:
if list1.val < list2.val:
dummy_head.next = list1
list1 = list1.next
else:
dummy_head.next = list2
list2 = list2.next
dummy_head = dummy_head.next
if list1:
dummy_head = list1
if list2:
dummy_head = list2
return new_head.next
list1 = Node(1)
list1.next = Node(2)
list1.next.next = Node(4)
list2 = Node(1)
list2.next = Node(3)
list2.next.next = Node(4)
merged_list = merge(list1, list2)
head = merged_list
while head:
print(head.val)
head = head.next
# Binary Search
def binary_search(nums, target):
left = 0
right = len(nums)-1
while left <= right:
mid = (left+right)//2
if target < nums[mid]:
right = mid-1
elif target > nums[mid]:
left = mid+1
else:
return mid
return left
# Quick Sort
def quick_sort(arr):
if len(arr) <= 1:
return arr
pivot = arr[0]
left = []
right = []
for i in range(1, len(arr)):
if arr[i] < pivot:
left.append(arr[i])
else:
right.append(arr[i])
return quick_sort(left)+[pivot]+quick_sort(right)
interview
python
Heroku offers a student program in partnership with GitHub. Students can access Heroku's resources by getting the GitHub Studnet Developer Pack. It provides a credit of $13 USD per month for 12 months. Please follow the instructions below step by step to enable it. 1. GitHub Student Developer Pack Click here to apply the GitHub Studnet Developer Pack from GitHub Education. An email account from the school, i.e., with .edu, is required to verify your student identity. 2. Heroku for GitHub Students Offer Register a Heroku account before enrolling in the Heroku Students Offer. After logging into Heroku, please click here to apply for the Studnets Offer.
May 4, 2023act is a tool to run github action locally. By using act, we can get feedback from CI faster. No need to commit/push any changes to github for checking your code is fulfilling the CI requirements or not. Installation MacOS Using homebrew to install act brew install act Install Docker
Dec 5, 2022電腦是由硬體和軟體所構成的,而主要負責運算的部分是作業系統的核心 - Kernel。當使用者下了命令之後,Kernel 就會接收這個命令並且再交由 CPU 進行處理。那 Kernel 是如何接收這些命令的呢 ? 可以看到下面這張圖,使用者和 Kernel 會靠著 Shell 作為一個使用者的介面來進行溝通,也就是說使用者下達了命令後,Shell 會將這些命令轉成 Kernel 可以理解的程式碼,再傳送給 Kernel 好讓 Kernel 可以正確地控制硬體工作。  BASH (Bourne Again SHell) BASH 是 Linux 預設的 Shell,雖然 Shell 有很多種,但是 BASH 會被作為 Linux 預設的 Shell 是因為他的功能非常強大,下面列出幾個主要的優點 : 命令紀錄 (history)
Jul 3, 2022Elasticsearch 除了提供搜尋的功能外,也提供了資料統計的功能,也就是本篇要介紹的聚合。聚合提供了多種分析的方式來滿足大多數的資料統計需求,例如 : 一個月內最大筆金額的訂單是哪一個 ? 這次促銷活動期間賣最差的商品是哪一項 ? 今年度每月的平均業績是多少 ? 而聚合主要的功能有以下四個 : Metric Aggregation (指標型聚合) Bucket Aggregation (桶型聚合)
Aug 25, 2021or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up