File tree 5 files changed +173
-0
lines changed
longest-consecutive-sequence
5 files changed +173
-0
lines changed Original file line number Diff line number Diff line change
1
+ /*
2
+ set를 통해 중복제거를 한 후 기존 nums와 길이 비교
3
+
4
+ nums의 길이 N
5
+
6
+ TC : O(N)
7
+ set를 만드는 데 전체 순회하며 N 시간 소모
8
+
9
+ SC : O(N)
10
+ set 만들 때 N의 메모리 할당
11
+ */
12
+ #include < vector>
13
+ #include < unordered_set>
14
+ using namespace std ;
15
+
16
+ class Solution {
17
+ public:
18
+ bool containsDuplicate (vector<int >& nums) {
19
+ unordered_set<int > us (nums.begin (), nums.end ());
20
+ if (nums.size () == us.size ())
21
+ return false ;
22
+ else
23
+ return true ;
24
+ }
25
+ };
Original file line number Diff line number Diff line change
1
+ /*
2
+ 풀이 :
3
+ 현재 집의 순서를 n이라고 했을 때 값을 업데이트하기 전 cur에는 n - 1 집까지 최대 훔칠 수 있는 돈, prv에는 n - 2집까지 최대 훔칠 수 있는 돈이 저장
4
+ cur를 max(n - 2까지 돈 + n의 돈, n - 1까지의 돈)로 n까지 오면서 훔칠 수 있는 최대 돈으로 업데이트,
5
+ prv는 n - 1까지 훔칠 수 있는 최대 돈으로 업데이트
6
+
7
+ nums의 갯수 : N
8
+
9
+ TC : O(N)
10
+
11
+ SC : O(1)
12
+ 배열없이 상수 변수 3개만 추가로 사용
13
+ */
14
+
15
+ #include < vector>
16
+ using namespace std ;
17
+
18
+ class Solution {
19
+ public:
20
+ int rob (vector<int >& nums) {
21
+ int prv = 0 , cur = 0 ;
22
+ int tmp;
23
+ for (auto & num : nums)
24
+ {
25
+ tmp = cur;
26
+ cur = max (prv + num, cur);
27
+ prv = tmp;
28
+ }
29
+ return cur;
30
+ }
31
+ };
Original file line number Diff line number Diff line change
1
+ /*
2
+ 풀이 :
3
+ 해시테이블에 nums를 담은 뒤 num - 1이 존재하지 않는 num에 대해서만 (중간점에서는 계산하지 않기 위해)
4
+ 길이를 증가시켜 나가며 연속된 수의 개수를 구하고 cur의 max값 ans를 구한다
5
+
6
+ nums의 갯수 N
7
+ TC : O(N)
8
+ 이중 for문이지만 내부 for문은 num - 1이 없을때만 연속된 해시테이블 내부 값에 대해서 수행하기 때문에 O(N)
9
+
10
+ SC : O(N)
11
+ 해시테이블의 크기는 N에 비례
12
+ */
13
+
14
+ #include < vector>
15
+ #include < unordered_set>
16
+ using namespace std ;
17
+
18
+ class Solution {
19
+ public:
20
+ int longestConsecutive (vector<int >& nums) {
21
+ int cur;
22
+ int ans = 0 ;
23
+ unordered_set<int > us (nums.begin (), nums.end ());
24
+
25
+ for (auto & num : us)
26
+ {
27
+ if (us.find (num - 1 ) == us.end ())
28
+ {
29
+ cur = 1 ;
30
+ for (int i = 1 ; us.find (num + i) != us.end (); i++)
31
+ cur++;
32
+ ans = max (ans, cur);
33
+ }
34
+ }
35
+ return ans;
36
+ }
37
+ };
Original file line number Diff line number Diff line change
1
+ /*
2
+ 풀이 :
3
+ 해시테이블에 숫자 : 빈도로 저장 후 freq 이중배열에 index를 frequency로 삼아 저장한다
4
+ 이중배열의 뒤에서부터 탐색하면서 k개를 result에 삽입
5
+
6
+ nums 개수 N
7
+ TC : O(N)
8
+ 전체 개수 N에 대해 for문 각각 돌아서
9
+ SC : O(N)
10
+ 해시테이블과 이중배열 모두 N에 비례
11
+ */
12
+
13
+ #include < vector>
14
+ #include < unordered_map>
15
+ using namespace std ;
16
+
17
+ class Solution {
18
+ public:
19
+ vector<int > topKFrequent (vector<int >& nums, int k) {
20
+ unordered_map<int , int > umap;
21
+ for (auto & num : nums)
22
+ {
23
+ umap[num]++;
24
+ }
25
+
26
+ vector<vector<int >> freq (nums.size () + 1 );
27
+ for (auto & pair : umap)
28
+ {
29
+ freq[pair.second ].push_back (pair.first );
30
+ }
31
+
32
+ vector<int > result;
33
+ for (int i = nums.size (); i > 0 && result.size () < k; i--)
34
+ {
35
+ for (auto & num : freq[i])
36
+ {
37
+ result.push_back (num);
38
+ if (result.size () == k)
39
+ break ;
40
+ }
41
+ }
42
+ return result;
43
+ }
44
+ };
Original file line number Diff line number Diff line change
1
+ /*
2
+ 풀이 :
3
+ 타깃을 뺸 complement 값을 구하고 해시테이블(unordered_map)에 존재하면 리턴
4
+ 없으면 해시테이블에 값 : 인덱스 형태로 저장
5
+
6
+ nums의 size: N
7
+ TC : O(N)
8
+ size만큼 for문 반복
9
+ SC : O(N)
10
+ size만큼 해시테이블에 추가
11
+ */
12
+
13
+ #include < vector>
14
+ #include < unordered_map>
15
+ using namespace std ;
16
+
17
+ class Solution {
18
+ public:
19
+ vector<int > twoSum (vector<int >& nums, int target) {
20
+ unordered_map<int , int > umap;
21
+ vector<int > result;
22
+ for (int i = 0 ; i < nums.size (); i++)
23
+ {
24
+ int complement = target - nums[i];
25
+ if (umap.find (complement) == umap.end ())
26
+ umap[nums[i]] = i;
27
+ else
28
+ {
29
+ result.push_back (i);
30
+ result.push_back (umap[complement]);
31
+ return result;
32
+ }
33
+ }
34
+ return result;
35
+ }
36
+ };
You can’t perform that action at this time.
0 commit comments