File tree 6 files changed +128
-0
lines changed
6 files changed +128
-0
lines changed Original file line number Diff line number Diff line change
1
+ import java .util .*;
2
+
3
+ class Solution {
4
+ public int leastInterval (char [] tasks , int n ) {
5
+ // 각 작업의 빈도 수 계산
6
+ int [] freqs = new int [26 ];
7
+ for (char c : tasks ) freqs [c - 'A' ]++;
8
+
9
+ // 최대 빈도수를 우선 순위로 두는 우선순위 큐
10
+ Queue <Integer > pq = new PriorityQueue <>((a , b ) -> b - a );
11
+ for (int f : freqs )
12
+ if (f > 0 ) pq .add (f );
13
+
14
+ int result = 0 ;
15
+
16
+ // 작업을 처리
17
+ while (!pq .isEmpty ()) {
18
+ int intervals = 0 ;
19
+ List <Integer > list = new ArrayList <>();
20
+
21
+ // 최대 n + 1개의 작업을 처리
22
+ for (int i = 0 ; i <= n ; i ++) {
23
+ if (!pq .isEmpty ()) {
24
+ int frequency = pq .poll ();
25
+ intervals ++;
26
+ result ++;
27
+ if (frequency > 1 ) list .add (frequency - 1 );
28
+ }
29
+ }
30
+
31
+ // 남아 있는 작업들을 다시 큐에 넣음
32
+ pq .addAll (list );
33
+
34
+ // 남은 작업이 있으면 대기 시간이 발생
35
+ if (!pq .isEmpty ()) result += n + 1 - intervals ;
36
+ }
37
+
38
+ return result ;
39
+ }
40
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public int canCompleteCircuit (int [] gas , int [] cost ) {
3
+ //방문 가능한 입력값인지 필터링
4
+ if (Arrays .stream (gas ).sum ()<Arrays .stream (cost ).sum ())
5
+ return -1 ;
6
+
7
+ int start =0 ,fuel =0 ;
8
+
9
+ //전체 주유소를 순회하면서 성립되지 않는 위치를 찾는다.
10
+ //남은 기름으로 출발점이 안 되는 주요소가 있다면 이미 지나친 지점도 전부 출발점이 될수 없으므로
11
+ //출발점을 다음지점으로 밀어낸다.
12
+ for (int i =0 ;i <gas .length ;i ++){
13
+ if (fuel +gas [i ]-cost [i ]<0 ){
14
+ start =i +1 ;
15
+ fuel =0 ;
16
+ }else {
17
+ //남은 기름을 계속 누적한다.
18
+ fuel +=gas [i ]-cost [i ];
19
+ }
20
+ }
21
+ return start ;
22
+
23
+
24
+ }
25
+ }
Original file line number Diff line number Diff line change
1
+ import java .util .*;
2
+
3
+ class Solution {
4
+ public int majorityElement (int [] nums ) {
5
+ Map <Integer , Integer > countsMap = new HashMap <>();
6
+
7
+ // 숫자의 빈도를 계산
8
+ for (int num : nums ) {
9
+ countsMap .put (num , countsMap .getOrDefault (num , 0 ) + 1 );
10
+ }
11
+
12
+ // 빈도를 기준으로 내림차순 정렬
13
+ Map <Integer , Integer > reverseSortedMap = new LinkedHashMap <>();
14
+ countsMap .entrySet ()
15
+ .stream ()
16
+ .sorted (Map .Entry .<Integer , Integer >comparingByValue (Comparator .reverseOrder ()))
17
+ .forEachOrdered (x -> reverseSortedMap .put (x .getKey (), x .getValue ()));
18
+
19
+ // 첫 번째 엔트리가 과반수를 차지하는지 확인
20
+ Map .Entry <Integer , Integer > entry = reverseSortedMap .entrySet ().iterator ().next ();
21
+
22
+ if (entry .getValue () > nums .length / 2 ) return entry .getKey ();
23
+
24
+ return -1 ;
25
+ }
26
+ }
Original file line number Diff line number Diff line change
1
+
Original file line number Diff line number Diff line change
1
+
2
+ class Solution {
3
+ public int rob (int [] nums ) {
4
+
5
+ if (n <0 )return 0 ;
6
+
7
+ return Math .max (
8
+ rob (nums ,n -1 ),
9
+ rob (nums ,n -2 )+nums [n ]
10
+ );
11
+ }
12
+
13
+ public int rob (int []nums ){
14
+ return rob (nums ,nums .length -1 );
15
+ }
16
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public int solution (int [] money ) {
3
+ if (money .length == 1 ) return money [0 ];
4
+
5
+ int [][] dp = new int [2 ][money .length ];
6
+
7
+ dp [0 ][0 ] = 0 ; // Including the first house
8
+ dp [1 ][0 ] = 0 ; // Excluding the first house
9
+ dp [0 ][1 ] = 0 ; // Cannot take both first and second house
10
+ dp [1 ][1 ] = money [1 ]; // Starting from the second house
11
+
12
+ for (int i = 2 ; i < money .length ; i ++) {
13
+ dp [0 ][i ] = Math .max (dp [0 ][i - 1 ], dp [0 ][i - 2 ] + money [i ]);
14
+ dp [1 ][i ] = Math .max (dp [1 ][i - 1 ], dp [1 ][i - 2 ] + money [i ]);
15
+ }
16
+
17
+ // Return the maximum of either robbing from the first house or starting from the second house
18
+ return Math .max (money [0 ]+dp [0 ][money .length - 2 ], dp [1 ][money .length - 1 ]);
19
+ }
20
+ }
You can’t perform that action at this time.
0 commit comments