File tree Expand file tree Collapse file tree 19 files changed +494
-1
lines changed
longest-common-subsequence
longest-consecutive-sequence Expand file tree Collapse file tree 19 files changed +494
-1
lines changed Original file line number Diff line number Diff line change 85
85
- name : Check filename rules
86
86
if : ${{ steps.pr-labels.outputs.has_maintenance != 'true' }}
87
87
run : |
88
- files=$(git diff --name-only ${{ github.event.pull_request.base.sha }} ${{ github.sha }} | tr -d '"')
88
+ files=$(git diff --name-only ${{ github.event.pull_request.base.sha }} ${{ github.event.pull_request.head. sha }} | tr -d '"')
89
89
pr_author="${{ github.event.pull_request.user.login }}"
90
90
success=true
91
91
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ /**
3
+ * μκ° λ³΅μ‘λ: O(N)
4
+ * κ³΅κ° λ³΅μ‘λ: O(N)
5
+ */
6
+ public boolean containsDuplicate (int [] nums ) {
7
+ Set <Integer > set = new HashSet <>();
8
+
9
+ for (int num : nums ) {
10
+ if (set .contains (num )) return true ;
11
+ set .add (num );
12
+ }
13
+
14
+ return false ;
15
+ }
16
+ }
Original file line number Diff line number Diff line change
1
+ import java .util .HashSet ;
2
+ import java .util .Set ;
3
+
4
+ class SolutionGotprgmer {
5
+ // ν΄λΉ λ¬Έμ λ μ΄λ ν μ«μκ° 2κ°μ΄μ μ‘΄μ¬ν κ²½μ° trueλ₯Ό κ·Έλ μ§ μμ κ²½μ°, falseλ₯Ό λ°ννλ λ¬Έμ μ΄λ€.
6
+ // setμ μ¬μ©ν΄μ setμ μ΄λ―Έ κ°μ΄ μ‘΄μ¬νλ€λ©΄ κ°μκ° 2 μ΄μμ΄λ―λ‘ true κ·Έλ μ§ μμΌλ©΄ falseλ₯Ό μΆλ ₯νλ€.
7
+
8
+ // κ° μ«μλ€μ μ μ₯ν΄μ setμΌλ‘ κ΄λ¦¬ -> distinctNums
9
+ // numsμ κ° μ«μμΈ checkNumμ distinctNumsμ λ£μ΄μ€λ€.
10
+ // λ§μ½ checkNumμ΄ μ΄λ―Έ distinctNumsμ μ‘΄μ¬νλ€λ©΄ ansλ₯Ό trueλ‘ λ§λ€μ΄μ£Όκ³ λ΅μ μΆλ ₯νλ€.
11
+
12
+
13
+ // μκ°λ³΅μ‘λ -> O(n)
14
+ // 곡κ°λ³΅μ‘λ -> O(n)
15
+ static Set <Integer > distinctNums ;
16
+ public boolean containsDuplicate (int [] nums ) {
17
+ distinctNums = new HashSet <>();
18
+ boolean ans = false ;
19
+ for (int checkNum : nums ) {
20
+ if (distinctNums .contains (checkNum )) {
21
+ ans = true ;
22
+ break ;
23
+ };
24
+ distinctNums .add (checkNum );
25
+ }
26
+ return ans ;
27
+ }
28
+
29
+
30
+ }
Original file line number Diff line number Diff line change
1
+ '''
2
+ # Leetcode 217. Contains Duplicate
3
+
4
+ use set to store distinct elements ποΈ
5
+
6
+ ## Time and Space Complexity
7
+
8
+ ```
9
+ TC: O(n)
10
+ SC: O(n)
11
+ ```
12
+
13
+ ### TC is O(n):
14
+ - iterating through the list just once to convert it to a set.
15
+
16
+ ### SC is O(n):
17
+ - creating a set to store the distinct elements of the list.
18
+ '''
19
+
20
+ class Solution :
21
+ def containsDuplicate (self , nums : List [int ]) -> bool :
22
+ return len (nums ) != len (set (nums ))
23
+
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
+ class Solution {
2
+ public int rob (int [] nums ) {
3
+ //λ°°μ΄ κΈΈμ΄ 0μ΄λ©΄ νΈ μ μλ μ§μ΄ μμ.
4
+ if (nums .length == 0 ) return 0 ;
5
+ //λ°°μ΄ κΈΈμ΄κ° 1μ΄λ©΄ ν μ§λ§ νΈ μ μμ.
6
+ if (nums .length == 1 ) return nums [0 ];
7
+
8
+ //λμ κ³νλ²μΌλ‘ νμ΄
9
+ int [] dp = new int [nums .length ];
10
+ dp [0 ] = nums [0 ];
11
+ dp [1 ] = Math .max (nums [0 ], nums [1 ]);
12
+
13
+ //λ°°μ΄ ν¬κΈ°κ° 2μ΄μμΌ κ²½μ° μ΅λ κΈμ‘μ λ²μ νμ₯
14
+ for (int i = 2 ; i < nums .length ; i ++) {
15
+ dp [i ] = Math .max (dp [i - 2 ] + nums [i ], dp [i - 1 ]);
16
+ }
17
+ return dp [nums .length - 1 ];
18
+ }
19
+ }
Original file line number Diff line number Diff line change
1
+ '''
2
+ # Leetcode 198. House Robber
3
+
4
+ use **dynamic programming** to solve this problem. (bottom-up approach) π§©
5
+
6
+ choose bottom-up approach for less space complexity.
7
+
8
+ ## DP relation
9
+
10
+ ```
11
+ dp[i] = max(dp[i - 1], dp[i - 2] + nums[i])
12
+ ```
13
+
14
+ - **dp[i - 1]:** skip and take the value from the previous house
15
+ - **dp[i - 2]:** rob the current house, add its value to the maximum money from two houses before
16
+
17
+ ## Time and Space Complexity
18
+
19
+ ```
20
+ TC: O(n)
21
+ SC: O(n)
22
+ ```
23
+
24
+ ### TC is O(n):
25
+ - iterating through the list just once to calculate the maximum money. = O(n)
26
+
27
+ ### SC is O(n):
28
+ - using a list to store the maximum money at each house. = O(n)
29
+
30
+ '''
31
+
32
+ class Solution :
33
+ def rob (self , nums : List [int ]) -> int :
34
+ if len (nums ) == 1 :
35
+ return nums [0 ]
36
+
37
+ dp = [0 ] * len (nums )
38
+ dp [0 ] = nums [0 ]
39
+ dp [1 ] = max (nums [0 ], nums [1 ])
40
+
41
+ for i in range (2 , len (nums )):
42
+ dp [i ] = max (dp [i - 1 ], dp [i - 2 ] + nums [i ])
43
+
44
+ return dp [- 1 ]
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
+ import java .util .*;
2
+
3
+ class Solution {
4
+ public int longestConsecutive (int [] nums ) {
5
+ if (nums == null || nums .length == 0 ) return 0 ;
6
+
7
+ //λͺ¨λ μμ HashSet μ½μ
8
+ HashSet <Integer > set = new HashSet <>();
9
+ for (int num : nums ) {
10
+ set .add (num );
11
+ }
12
+
13
+ int longest = 0 ;
14
+
15
+ // μμμ§μ 체ν¬
16
+ for (int num : nums ) {
17
+ //λ°°μ΄ μμλ³΄λ€ 1 μμ μ κ° μλ κ²½μ° μλ‘μ΄ μμ μ§μ μ΄ λ¨
18
+ if (!set .contains (num - 1 )) {
19
+ int start = num ;
20
+ int currentLength = 1 ;
21
+
22
+ // 1μ© μ¦κ°μν€λ©΄μ μ°μλ μμ κ°μ νμ
23
+ while (set .contains (start + 1 )) {
24
+ start ++;
25
+ currentLength ++;
26
+ }
27
+ // κΈ°μ‘΄ longestμ νμ¬ μ°μλ μλ₯Ό λΉκ΅
28
+ longest = Math .max (longest , currentLength );
29
+ }
30
+ }
31
+
32
+ return longest ;
33
+ }
34
+ }
You canβt perform that action at this time.
0 commit comments