Skip to content

Commit b63432b

Browse files
authored
Merge pull request #977 from donghyeon95/main
[donghyeon95] Week8
2 parents 6292f47 + 5ed7b79 commit b63432b

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import java.util.HashMap;
2+
import java.util.Map;
3+
import java.util.Set;
4+
5+
class Solution {
6+
public int characterReplacement(String s, int k) {
7+
// K개까지는 용인되는 최대 길이를 구해라
8+
// 2 포인터 => 최대 O(2*N) = O(N)
9+
// 용인 되는 길이면 second++, 용인되는 길이가 아니면 first++;
10+
11+
String[] strings = s.split("");
12+
HashMap<String, Integer> strCnt = new HashMap<>();
13+
int result = 0;
14+
int first = 0;
15+
int second = 0;
16+
17+
while(first<s.length()){
18+
strCnt.put(strings[first], strCnt.getOrDefault(strings[first], 0)+1);
19+
System.out.println(" second: " + second + " first: " + first );
20+
if (getOtherCnt(strCnt) <= k) {
21+
result = Math.max(result, first-second+1);
22+
first++;
23+
}
24+
else {
25+
strCnt.put(strings[second], strCnt.getOrDefault(strings[second],1)-1);
26+
strCnt.put(strings[first], strCnt.getOrDefault(strings[first], 0)-1);
27+
second++;
28+
}
29+
}
30+
31+
return result;
32+
}
33+
34+
public int getOtherCnt(HashMap<String, Integer> strCnt) {
35+
int total = 0;
36+
int max = 0;
37+
for (int a: strCnt.values()) {
38+
total += a;
39+
max = Math.max(max, a);
40+
}
41+
return total - max;
42+
}
43+
}
44+
45+

number-of-1-bits/donghyeon95.java

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public int hammingWeight(int n) {
3+
int mask = 1;
4+
int result = 0;
5+
while(n>0) {
6+
if ((n & mask) == 1) {
7+
result++;
8+
}
9+
n = n >> 1;
10+
}
11+
12+
return result;
13+
}
14+
}
15+

0 commit comments

Comments
 (0)