online_judge
, python3
給定一個只有 0 和 1 的陣列,求 0 和 1 數量相等的最大子陣列。
又是一題 hash table(好啦,其實是前綴和),求各前綴的 0,1 數量差,若有兩個前綴帶有相同 0,1 數量差,則這兩個前綴相減結果會是一個 0 和 1 數量相等的子陣列。 而 hash table 可用於記錄各數量 0,1 的對應前綴。 順帶一提,因為是要找最大子陣列,hash table 只要記錄每個 0,1 數量差最早找到的前綴就可以了(越後期找到的,距離肯定越短啊)。
Given a binary array, find the maximum length of a contiguous subarray with equal number of 0 and 1.
Example 1:
Input: [0,1]
Output: 2
Explanation: [0, 1] is the longest contiguous subarray with equal number of 0 and 1.
Example 2:
Input: [0,1,0]
Output: 2
Explanation: [0, 1] (or [1, 0]) is a longest contiguous subarray with equal number of 0 and 1.
Note: The length of the given binary array will not exceed 50,000.
class Solution:
def findMaxLength(self, nums):
prefix_sum = {}
prefix_sum[0] = -1
diff = 0
ans = 0
for i in range(len(nums)):
if(nums[i]==0):
diff-=1
else:
diff+=1
if diff in prefix_sum:
ans = max(i - prefix_sum[diff],ans)
else:
prefix_sum[diff] = i
return ans
a = Solution()
print (a.findMaxLength([0,1]))
print (a.findMaxLength([0,1,0]))
print (a.findMaxLength([]))
print (a.findMaxLength([0,0,0,0]))
print (a.findMaxLength([0,1,1,1,1,0,0]))
Learn More →
tmux 的全稱是 Terminal Multiplexer,直譯過來就是終端(Terminal)多工器(Multiplexer),這個工具可以在單一 screen 之下 create, attach, detach 多個終端,熟悉之後十分好用。只是和 vim,vi 系列相同,所有操作皆能且只能使用鍵盤,不像 GNOME Terminal 一樣有直觀的 GUI 介面,算不上友善的學習曲線使人又愛又恨。關於 tmux 的基本概念和入門組合鍵,G. T. Wang 的文章提供了很不錯的繁體中文資料,本篇的重點在於如何配合 shell script 和 tmux 來快速建立一個順手的工作區。
Sep 18, 2024轉眼就在日本工作快半年了,想說就來整理一下求職當下的記錄吧。先說一下小弟我的狀況,原本其實是沒有來日本工作的計劃,打算就老老實實GG輪班救台灣的。只是剛好修個日文課,剛好老師介紹下參加了幾場面試,誤打誤撞的就拿到了還算可以的內定。瞄著日本的綠卡傻傻的就跑來了。所以說人生啊,緣分真的很重要。而計劃,永遠趕不上變化。
May 31, 2024Introduction
Jun 21, 2023Revision record version comment author v0.1.0 document built Marco Ma
Jan 20, 2021or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up