Leetcode 1743 - Restore the Array From Adjacent Pairs

題目

Problem#

有一個整數陣列 nums 裏頭有 $n$ 個 unique 的元素。題目給你一個 adjacentPairs[i] = [ui, vi] 陣列有 $n-1$ 個元素,代表 uivinums 相鄰。保證所有 nums 相鄰的數字一定出現在 adjacentPairs 中,可能以任意順序出現。
回傳原始的 nums 陣列,如果有多種解,回傳任意一種。

測資限制#

  • nums 長度: $2 \le n \le 10^5$
  • adjacentPairs 長度: $== n-1$
  • $-10^5 \le \text{nums[i]}, u_i, v_i \le 10^5$

Read More

Leetcode 1535 - Find the Winner of an Array Game

題目

Problem#

給你一個數字不重複的整數陣列 arr 和一個整數 k,每次比較陣列第一個(arr[0])與第二個元素(arr[1])的大小,贏的數字會在 index 0,輸的數字則移動到 arr 的最後,其他數字依序往前移動一格。當有個數字連續贏 k 場時比賽結束,回傳贏的數字。

測資限制#

  • $2 \le \text{len(arr)} \le 10^5$
  • $1 \le \text{arr[i]} \le 10^6$
  • $1 \le k \le 10^9$
  • 保證每場比賽一定會有贏者

Read More

Leetcode 1503 - Last Moment Before All Ants Fall Out of a Plank

題目

Problem#

木棍長 n 個單位,上頭有一群螞蟻在走,各自朝著左方與右方,每秒移動一個單位。每當螞蟻相遇時,會各自往反方向走。當螞蟻走出 0n 時會掉出去,給你朝左與朝右的螞蟻初始位置,問你經過幾秒後,所有螞蟻都掉出木棍?

測資限制#

  • $1 \le n \le 10^4$

Read More