File tree Expand file tree Collapse file tree 2 files changed +5
-67
lines changed
longest-consecutive-sequence Expand file tree Collapse file tree 2 files changed +5
-67
lines changed Original file line number Diff line number Diff line change 1+ // 점화식의 최대값을 구하는 방법
2+ // 1. 현재 위치의 최대 값은 한칸 전 집까지만 털었던가(두칸 연속 겹치면 안된다는 룰을 지키면서)
3+ // 2. 두칸 전 집까지 털고 + 현재집을 털었을 때다
14// 공간복잡도를 줄이는법. 배열로 관리 안하기
25class Solution {
36 public int rob (int [] nums ) {
@@ -13,21 +16,3 @@ public int rob(int[] nums) {
1316 return prev1 ;
1417 }
1518}
16-
17- class Solution {
18- public int rob (int [] nums ) {
19- // 점화식의 최대값을 구하는 방법
20- // 1. 현재 위치의 최대 값은 한칸 전 집까지만 털었던가(두칸 연속 겹치면 안된다는 룰을 지키면서)
21- // 2. 두칸 전 집까지 털고 + 현재집을 털었을 때다
22- if (nums .length == 1 ) {
23- return nums [0 ];
24- }
25- int [] dp = new int [nums .length ];
26- dp [0 ] = nums [0 ];
27- dp [1 ] = Math .max (nums [0 ], nums [1 ]);
28- for (int i = 2 ; i < nums .length ; i ++) {
29- dp [i ] = Math .max (nums [i ] + dp [i - 2 ], dp [i - 1 ]);
30- }
31- return dp [nums .length - 1 ];
32- }
33- }
Original file line number Diff line number Diff line change 1+ // 이중 변환 필요 없음
2+ // 정렬이 들어가면 O(nlogn) 아래로 줄일 수 없음
13// 중복여부만 제거하고 포함여부로 판단 O(N)
24class Solution {
35 public int longestConsecutive (int [] nums ) {
@@ -28,52 +30,3 @@ public int longestConsecutive(int[] nums) {
2830 return maxLength ;
2931 }
3032}
31- // 정렬이 들어가면 O(nlogn) 아래로 줄일 수 없음
32- class Solution {
33- public int longestConsecutive (int [] nums ) {
34- if (nums .length == 0 ) return 0 ;
35- TreeSet <Integer > set = new TreeSet <>();
36- for (int num : nums ) {
37- set .add (num );
38- }
39- int max = 1 ;
40- int consecutiveCount = 1 ;
41- int prev = set .pollFirst ();
42- while (!set .isEmpty ()) {
43- int next = set .pollFirst ();
44- if (next - prev == 1 ) {
45- consecutiveCount ++;
46- } else {
47- max = Math .max (consecutiveCount , max );
48- consecutiveCount = 1 ;
49- }
50- prev = next ;
51- }
52- return Math .max (max , consecutiveCount );
53- }
54- }
55- // 이중 변환 필요 없음
56- class Solution {
57- public int longestConsecutive (int [] nums ) {
58- if (nums .length == 0 ) return 0 ;
59- HashSet <Integer > set = new HashSet <>();
60- for (int num : nums ) {
61- set .add (num );
62- }
63- PriorityQueue <Integer > pq = new PriorityQueue <>(set );
64- int max = 1 ;
65- int consecutiveCount = 1 ;
66- int prev = pq .poll ();
67- while (!pq .isEmpty ()) {
68- int next = pq .poll ();
69- if (next - prev == 1 ) {
70- consecutiveCount ++;
71- } else {
72- max = Math .max (consecutiveCount , max );
73- consecutiveCount = 1 ;
74- }
75- prev = next ;
76- }
77- return Math .max (max , consecutiveCount );
78- }
79- }
You can’t perform that action at this time.
0 commit comments