File tree Expand file tree Collapse file tree 1 file changed +24
-0
lines changed
longest-consecutive-sequence Expand file tree Collapse file tree 1 file changed +24
-0
lines changed Original file line number Diff line number Diff line change 1+ class Solution {
2+ func longestConsecutive( _ nums: [ Int ] ) -> Int {
3+ guard !nums. isEmpty else { return 0 }
4+ /*
5+ μ€λ³΅ μ κ±° ν μν
ν λ°°μ΄λ‘ λ§λλκ²μ N + NlogN + N μΌλ‘ μμλλ€..
6+ κ·Έλμ μ λ ¬λ§ νκ³ λ‘μ§μμ μ€λ³΅ μ«μλ κ·Έλ₯ 무μνκ² λ§λλ λ°©μμΌλ‘ μλλ₯Ό κ°μ
7+ λ¬Έμ μμ O(N)μΌλ‘ μμ±νμ λΌλ μ μ½μλ μ μ½μ΄ μμλλ°, O(N)μ λμ ν λ μ€λ₯΄μ§ μλλ€.
8+ */
9+ let nums = nums. sorted ( ) //Array(Set(nums).sorted())
10+ var answer = 1
11+ var count = 1
12+ for index in 0 ..< nums. count- 1 {
13+ if nums [ index] - nums[ index + 1 ] == - 1 {
14+ count += 1
15+ }
16+ else if nums [ index] == nums [ index + 1 ] { continue }
17+ else {
18+ answer = max ( answer, count)
19+ count = 1
20+ }
21+ }
22+ return max ( answer, count)
23+ }
24+ }
You canβt perform that action at this time.
0 commit comments