File tree Expand file tree Collapse file tree 17 files changed +364
-4
lines changed
longest-common-subsequence
longest-consecutive-sequence Expand file tree Collapse file tree 17 files changed +364
-4
lines changed Original file line number Diff line number Diff line change @@ -52,3 +52,8 @@ elixir:
52
52
- changed-files :
53
53
- any-glob-to-any-file :
54
54
- " **/*.exs"
55
+
56
+ rust :
57
+ - changed-files :
58
+ - any-glob-to-any-file :
59
+ - " **/*.rs"
Original file line number Diff line number Diff line change 1
1
## 답안 제출 문제
2
2
3
3
<!--
4
- 자신의 수준이나 일정에 맞게 금주에 푸시기로 정한 문제들만 나열해주세요.
5
- 코드 검토자들이 PR 승인 여부를 결정할 때 도움이 됩니다.
4
+ 자신의 수준이나 일정에 맞게 👉 금주에 푸시기로 정한 문제들👈만 나열해주세요.
5
+ 리뷰어들이 검토와 PR 승인 여부를 결정할 때 큰 도움이 됩니다.
6
6
-->
7
7
8
8
- [ ] 문제 1
9
9
- [ ] 문제 2
10
10
- [ ] 문제 3
11
+ <!-- - [ ] 문제 4 풀고싶지 않은 문제는 이렇게 주석처리 해 주셔도 좋아요 -->
11
12
12
13
## 체크 리스트
13
14
14
- - [ ] PR을 프로젝트에 추가하고 Week를 현재 주차로 설정해주세요.
15
+ - [ ] 우측 메뉴에서 PR을 ** Projects** 에 추가해주세요.
16
+ - [ ] ** Projects** 의 오른쪽 버튼(▼)을 눌러 확장한 뒤, ** Week** 를 현재 주차로 설정해주세요.
15
17
- [ ] 바로 앞에 PR을 열어주신 분을 코드 검토자로 지정해주세요.
16
- - [ ] 문제를 모두 푸시면 프로젝트에서 Status를 ` In Review ` 로 설정해주세요.
18
+ - [ ] 문제를 모두 푸시면 프로젝트에서 ** Status ** 를 ` In Review ` 로 설정해주세요.
17
19
- [ ] 코드 검토자 1분 이상으로부터 승인을 받으셨다면 PR을 병합해주세요.
Original file line number Diff line number Diff line change
1
+ // Time complexity, O(n)
2
+ // Space complexity, O(n)
3
+ // 풀이
4
+ // nums 배열을 순회하면서 hashMap에 num을 key로, 존재 여부를 value로 저장한다.
5
+ // 만약 이미 존재하는 key라면 true를 반환하고, 순회를 전부 했는데도 중복이 없다면 false를 반환한다.
6
+ func containsDuplicate (nums []int ) bool {
7
+ hashMap := map [int ]bool {}
8
+ for _ , num := range nums {
9
+ if hashMap [num ] {
10
+ return true
11
+ }
12
+ hashMap [num ] = true
13
+ }
14
+ return false
15
+ }
Original file line number Diff line number Diff line change
1
+ import java .util .HashSet ;
2
+ import java .util .Set ;
3
+
4
+ class Solution {
5
+ public boolean containsDuplicate (int [] nums ) {
6
+ Set <Integer > numSet = new HashSet ();
7
+
8
+ for (int num : nums ) {
9
+ numSet .add (num );
10
+ }
11
+
12
+ return numSet .size () != nums .length ;
13
+ }
14
+ }
Original file line number Diff line number Diff line change
1
+ """
2
+ # Time Complexity: O(N)
3
+ - N번 순회
4
+ # Space Compelexity: O(N)
5
+ - 최악의 경우 (중복된 값이 없을 경우) N개 저장
6
+ """
7
+ class Solution :
8
+ def containsDuplicate (self , nums : List [int ]) -> bool :
9
+ num_dict = {}
10
+ for num in nums :
11
+ if num not in num_dict :
12
+ num_dict [num ] = True
13
+ else :
14
+ return True
15
+ return False
Original file line number Diff line number Diff line change
1
+ // time complexity: O(n)
2
+ // space complexity: O(1)
3
+ func rob (nums []int ) int {
4
+ prev := 0
5
+ curr := 0
6
+
7
+ for _ , num := range nums {
8
+ temp := curr
9
+ curr = max (prev + num , curr )
10
+ prev = temp
11
+ }
12
+
13
+ return curr
14
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public int rob (int [] nums ) {
3
+ int [] sums = new int [nums .length ];
4
+
5
+ sums [0 ] = nums [0 ];
6
+
7
+ if (nums .length > 1 ) {
8
+ sums [1 ] = nums [1 ];
9
+ }
10
+
11
+ if (nums .length > 2 ) {
12
+ sums [2 ] = nums [0 ] + nums [2 ];
13
+ }
14
+
15
+ if (nums .length > 3 ) {
16
+ for (int i = 3 ; i < nums .length ; i ++) {
17
+ sums [i ] = Math .max (nums [i ] + sums [i - 2 ], nums [i ] + sums [i - 3 ]);
18
+ }
19
+ }
20
+
21
+ int max = 0 ;
22
+ for (int sum : sums ) {
23
+ max = Math .max (sum , max );
24
+ }
25
+
26
+ return max ;
27
+ }
28
+ }
Original file line number Diff line number Diff line change
1
+ """
2
+ # Time Complexity: O(N)
3
+ - N개의 개수를 가지는 dp 리스트를 만들고, 이를 순회
4
+ # Space Compelexity: O(N)
5
+ - N개의 dp 리스트 저장
6
+ """
7
+ class Solution :
8
+ def rob (self , nums : List [int ]) -> int :
9
+ if len (nums ) == 1 :
10
+ return nums [0 ]
11
+
12
+ dp = [0 for _ in range (len (nums ))]
13
+ dp [0 ] = nums [0 ]
14
+ dp [1 ] = max (nums [0 ], nums [1 ])
15
+
16
+ for i in range (len (nums ) - 2 ):
17
+ dp [i + 2 ] = max (dp [i ] + nums [i + 2 ], dp [i + 1 ])
18
+
19
+ return max (dp [- 2 ], dp [- 1 ])
Original file line number Diff line number Diff line change
1
+ """
2
+ # Time Complexity: O(N)
3
+ - lce_dict 생성: N
4
+ - N개의 key에 대하여 순회하면서 값 확인: N
5
+ # Space Compelexity: O(k)
6
+ - 중복되지 않은 key k개 저장
7
+ """
8
+ class Solution :
9
+ def longestConsecutive (self , nums : List [int ]) -> int :
10
+ if not nums :
11
+ return 0
12
+
13
+ lce_dict = {}
14
+ for num in nums :
15
+ lce_dict [num ] = True
16
+
17
+ answer = 0
18
+ for num in nums :
19
+ cur_lce = 1
20
+ if lce_dict .pop (num , None ) is None :
21
+ continue
22
+
23
+ down_num = num - 1
24
+ while down_num in lce_dict :
25
+ cur_lce += 1
26
+ lce_dict .pop (down_num )
27
+ down_num -= 1
28
+
29
+ up_num = num + 1
30
+ while up_num in lce_dict :
31
+ cur_lce += 1
32
+ lce_dict .pop (up_num )
33
+ up_num += 1
34
+
35
+ answer = answer if answer > cur_lce else cur_lce
36
+
37
+ return answer
Original file line number Diff line number Diff line change
1
+ // time complexity: O(n)
2
+ // space complexity: O(n)
3
+ // 풀이
4
+ // 1. map에 nums의 모든 요소를 저장한다.
5
+ // 2. map을 순회하면서 num-1이 존재하는지 확인한다.
6
+ // 3. num-1이 존재하면 continue
7
+ // 4. num-1이 존재하지 않으면 consecutiveCount를 1로 초기화하고 num+1이 존재하는지 (연속적으로 숫자가 증가하는게 있는지) 확인한다.
8
+ // 5. num+1이 존재하면 consecutiveCount를 1 증가시키고 num을 1 증가시켜 다음 수를 찾는다.
9
+ // 6. num+1이 존재하지 않으면 현재까지의 consecutiveCount와 maxConsecutiveCount를 비교하여 maxConsecutiveCount를 갱신한다.
10
+ func longestConsecutive (nums []int ) int {
11
+ numMap := make (map [int ]bool )
12
+
13
+ for _ , num := range nums {
14
+ numMap [num ] = true
15
+ }
16
+
17
+ maxConsecutiveCount := 0
18
+
19
+ for num := range numMap {
20
+ if numMap [num - 1 ] {
21
+ continue
22
+ }
23
+ consecutiveCount := 1
24
+ for numMap [num + 1 ] {
25
+ num ++
26
+ consecutiveCount ++
27
+ }
28
+ if consecutiveCount > maxConsecutiveCount {
29
+ maxConsecutiveCount = consecutiveCount
30
+ }
31
+ }
32
+
33
+ return maxConsecutiveCount
34
+ }
You can’t perform that action at this time.
0 commit comments