難度: Easy
類型: Array, Binary Search, Divide and Conquer
CPP程式下載: 13.cpp
==========================
羅馬數字轉整數
另外有 12. Integer to Roman
一開始原本想這題怎麼難度只有 Easy, 不過寫到後來, 真的不難。
==========================
前情題要:
難度: Easy
類型: Array, Binary Search, Divide and Conquer
CPP程式下載: 13.cpp
==========================
羅馬數字轉整數
另外有 12. Integer to Roman
一開始原本想這題怎麼難度只有 Easy, 不過寫到後來, 真的不難。
==========================
前情題要:
4. Median of Two Sorted Arrays
難度: Hard
類型: Array, Binary Search, Divide and Conquer
CPP程式下載: 4.cpp
==========================
算出兩個已排序矩陣合併後的中間值
其實這題並不難, 不知道為何難度是 Hard
==========================
前情題要:
Runtime: 0 ms, Beats: 100%
Memory: 95.20 MB, Beats: 47.31%
3. Longest Substring Without Repeating Characters
=========================
找一個字串中, 最長且沒有重複字元的連續子字串長度多少。
Runtime: 6 ms, Beats: 71.54%
Memory: 11.32 MB, Beats: 89.00%
3_2.cpp:
Runtime: 0 ms, Beats: 100%
Memory: 10.84 MB, Beats: 94.25%
難度: Medium
類型: Array, Hash Table
==========================
結果: (2_2.cpp 的為例)
Runtime: 0 ms, Beats: 100%
Memory: 77.02 MB, Beats: 73.08%
難度: Easy
類型: Array, Hash Table
==========================
Hash Table 的題目, LeetCode 的第一題, 與某一線外商的 coding interview 的介紹影片題目相近。
==========================
前情題要:
簡單說就是找到兩個 nums verctor<int> 中的index, 他們對應的值相加等於 target
==========================
思考方式與複雜度:
1. 一樣從簡單直覺開始, 就兩個數字, 第一個數字的 index i從 0~nums_size; 另一個數字的 index j 從 i+1~nums_size; 找到兩個相加等於 target 就回傳 {i,j}。這樣的 time O(N^2), 但由於不需要多餘的memory complexity O(1)
2. 另一個比較快的方式, 用 unordered_map, 因為題目有寫唯一解, 所以就不必用到 multimap。每一個 nums 的element值, 都檢查有沒有它的 complement 值在 unordered_map 裡, 有的話就是找到答案, 把該 index 和在 unordered_map 裡的 index 組起來回傳答案。如果沒找到, 就把這個值的 complement 當作 key, index當作value, 存進 unordered_map 中, 如果最後一個 element 都找不到 complement, 就回傳空集合{}, 同時記得debug, 因為題目說有唯一解。這個時間複雜度O(N), 空間複雜度也是O(N)。
=======================
結果:
Accepted 63 / 63 testcases passed
1. 第一種方法
Runtime: 34 ms, Beats: 41.30%
Memory: 13.99 MB, Beats: 92.87%
2. 第二種方法
Runtime: 0 ms, Beats: 100%
Memory: 14.73 MB, Beats: 50.22%
2448. 讓陣列的值全部香等所需的最低花費
==========================
2448. Minimum Cost to Make Array Equal
難度: Hard
類型: Matrix
CPP程式下載: 2448.cpp
==========================
覺得 minimum cost 類型的題目很生疏, 挑幾題來練習, 難度還是Hard
==========================
3139. 讓矩陣每個元素的值都相同的最小花費 (Beats 100%)
=====================
3139. Minimum Cost to Equalize Array
Constraints:
1 <= nums.length <= 1051 <= nums[i] <= 1061 <= cost1 <= 1061 <= cost2 <= 106=====================
結果:
636 / 636 testcases passed
Runtime: 0 ms, Beats: 100%
Memory: 94.34 MB, Beats: 86.44%