Skip to content

Commit 2cb2fc9

Browse files
authored
Merge pull request #629 from kdh-92/main
[๊ถŒ๋™ํ˜„] Week 1
2 parents f4eb476 + 324485e commit 2cb2fc9

File tree

5 files changed

+272
-0
lines changed

5 files changed

+272
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class Solution {
2+
public boolean containsDuplicate(int[] nums) {
3+
/**
4+
* Constraints
5+
* - 1 <= nums[] <= 10^5
6+
* - -10^9 <= nums[i] <= 10^9
7+
*
8+
* Output
9+
* - true : ์ค‘๋ณต ๊ฐ’ ์กด์žฌ
10+
* - false : ๋ชจ๋“  ๊ฐ’์ด ์œ ์ผ
11+
*/
12+
13+
// ํ•ด๊ฒฐ๋ฒ• 1 (HashMap ๋ฐฉ์‹ - HashSet ์œ ์‚ฌ)
14+
// ์‹œ๊ฐ„๋ณต์žก๋„: O(N), ๊ณต๊ฐ„ ๋ณต์žก๋„ : O(N)
15+
Map<Integer, Integer> countMap = new HashMap<>();
16+
17+
for (int num: nums) {
18+
int count = countMap.getOrDefault(num, 0) + 1;
19+
if (count > 1) return true;
20+
countMap.put(num, count);
21+
}
22+
23+
return false;
24+
25+
// ํ•ด๊ฒฐ๋ฒ• 2 (์ •๋ ฌ)
26+
// ์‹œ๊ฐ„ ๋ณต์žก๋„ : O(N log N), ๊ณต๊ฐ„ ๋ณต์žก๋„ : O(1)
27+
Arrays.sort(nums);
28+
29+
for (int i = 0; i < nums.length - 1; i++) {
30+
if (nums[i] == nums[i + 1]) return true;
31+
}
32+
33+
return false;
34+
}
35+
}

โ€Žhouse-robber/kdh-92.javaโ€Ž

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
public int rob(int[] nums) {
3+
// (1) dp
4+
// int n = nums.length;
5+
6+
// if (n == 1) return nums[0];
7+
8+
// int[] dp = new int[n];
9+
10+
// dp[0] = nums[0];
11+
// dp[1] = Math.max(nums[0], nums[1]);
12+
13+
// for (int i = 2; i < n; i++) {
14+
// dp[i] = Math.max(dp[i - 1], nums[i] + dp[i - 2]);
15+
// }
16+
17+
// return dp[n - 1];
18+
19+
// (2) ์ธ์ ‘ ๊ฐ’ ๋น„๊ต
20+
int prev = 0, curr = 0;
21+
for (int num : nums) {
22+
int temp = curr;
23+
curr = Math.max(num + prev, curr);
24+
prev = temp;
25+
}
26+
27+
return curr;
28+
}
29+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/**
2+
* Constraints:
3+
* - 0 <= nums.length <= 105
4+
* - -109 <= nums[i] <= 109
5+
*
6+
* Output
7+
* - ๊ฐ€์žฅ ๊ธด ๊ธธ์ด
8+
*
9+
* ํ’€์ด ํŠน์ด์ 
10+
* - ๋‘˜๋‹ค ์‹œ๊ฐ„๋ณต์žก๋„๋Š” O(N)์œผ๋กœ ์ƒ๊ฐ๋˜๋Š”๋ฐ Runtime์˜ ๊ฒฐ๊ณผ ์ฐจ์ด๊ฐ€ ๊ฝค ํฌ๊ฒŒ ๋‚˜์˜จ ์ 
11+
*/
12+
13+
class Solution {
14+
public int longestConsecutive(int[] nums) {
15+
// (1) Set & num -1
16+
// ์‹œ๊ฐ„๋ณต์žก๋„ : O(N)
17+
// Runtime : 1267ms Beats 5.15%
18+
// Memory : 63.30MB Beats 62.58%
19+
// Set<Integer> uniqueNums = Arrays.stream(nums).boxed().collect(Collectors.toSet());
20+
// int result = 0;
21+
22+
// for (int num : nums) {
23+
// if (uniqueNums.contains(num - 1)) continue;
24+
// int length = 1;
25+
// while (uniqueNums.contains(num + length)) length += 1;
26+
// result = Math.max(length, result);
27+
// }
28+
29+
// return result;
30+
31+
// (2) HashMap
32+
// ์‹œ๊ฐ„๋ณต์žก๋„ : O(N)
33+
// Runtime : 38ms Beats 46.54%
34+
// Memory : 66.45MB Beats 51.28%
35+
HashMap<Integer, Boolean> hm = new HashMap<>();
36+
37+
for(int i=0; i<nums.length; i++){
38+
hm.put(nums[i], false);
39+
}
40+
41+
for(int key : hm.keySet()){
42+
if(hm.containsKey(key - 1)){
43+
hm.put(key, true);
44+
}
45+
}
46+
47+
int max = 0;
48+
for(int key : hm.keySet()){
49+
int k =1;
50+
if(hm.get(key)){
51+
while(hm.containsKey(key + k)){
52+
k++;
53+
}
54+
}
55+
56+
max = Math.max(max, k);
57+
}
58+
59+
return max;
60+
}
61+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/**
2+
* Constraints
3+
* 1 <= nums.length <= 10^5
4+
* -10^4 <= nums[i] <= 10^4
5+
* k is in the range [1, the number of unique elements in the array].
6+
* It is guaranteed that the answer is unique.
7+
*
8+
* Output
9+
* - int ๋ฐฐ์—ด
10+
*/
11+
12+
class Solution {
13+
public int[] topKFrequent(int[] nums, int k) {
14+
// (1) HashMap + PriorityQueue
15+
// ์‹œ๊ฐ„๋ณต์žก๋„ : O(N log N)
16+
// Runtime : 15ms Beats 38.03%
17+
// Memory : 48.93MB Beats 20.01%
18+
Map<Integer, Integer> frequencyMap = new HashMap<>();
19+
20+
for (int num : nums) {
21+
frequencyMap.put(num, frequencyMap.getOrDefault(num, 0) + 1);
22+
}
23+
24+
PriorityQueue<Map.Entry<Integer, Integer>> minHeap = new PriorityQueue<>(Comparator.comparingInt(Map.Entry::getValue));
25+
26+
for (Map.Entry<Integer, Integer> entry : frequencyMap.entrySet()) {
27+
minHeap.offer(entry);
28+
if (minHeap.size() > k) {
29+
minHeap.poll();
30+
}
31+
}
32+
33+
int[] result = new int[k];
34+
35+
for (int i = 0; i < k; i++) {
36+
result[i] = minHeap.poll().getKey();
37+
}
38+
39+
return result;
40+
41+
// (2) Stream
42+
// ์‹œ๊ฐ„๋ณต์žก๋„ : O(N log N)
43+
// Runtime : 19ms Beats 8.16%
44+
// Memory : 49.00MB Beats 20.01%
45+
// Stream์— ์ต์ˆ™ํ•ด์ง€๊ธฐ ์œ„ํ•ด ๊ณต๋ถ€์šฉ
46+
return Arrays.stream(nums)
47+
.boxed()
48+
.collect(Collectors.groupingBy(num -> num, Collectors.summingInt(num -> 1)))
49+
.entrySet().stream()
50+
.sorted((a, b) -> b.getValue() - a.getValue())
51+
.limit(k)
52+
.mapToInt(Map.Entry::getKey)
53+
.toArray();
54+
55+
// (3) Array List
56+
// ์‹œ๊ฐ„๋ณต์žก๋„ : O(N)
57+
// Runtime : 13ms Beats 75.77%
58+
// Memory : 48.44MB Beats 61.68%
59+
Map<Integer, Integer> frequencyMap = new HashMap<>();
60+
for (int num : nums) {
61+
frequencyMap.put(num, frequencyMap.getOrDefault(num, 0) + 1);
62+
}
63+
64+
List<Integer>[] buckets = new List[nums.length + 1];
65+
for (int key : frequencyMap.keySet()) {
66+
int freq = frequencyMap.get(key);
67+
if (buckets[freq] == null) {
68+
buckets[freq] = new ArrayList<>();
69+
}
70+
buckets[freq].add(key);
71+
}
72+
73+
List<Integer> result = new ArrayList<>();
74+
for (int i = buckets.length - 1; i >= 0 && result.size() < k; i--) {
75+
if (buckets[i] != null) {
76+
result.addAll(buckets[i]);
77+
}
78+
}
79+
80+
return result.stream().mapToInt(Integer::intValue).toArray();
81+
}
82+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/**
2+
* Constraints
3+
* - 1 <= s.length <= 2 * 10^5
4+
* - s consists only of printable ASCII characters.
5+
*
6+
* Output
7+
* - true : ์ขŒ์šฐ ๋™์ผ
8+
* - false : ์ขŒ์šฐ ๋น„๋™์ผ
9+
*/
10+
11+
12+
class Solution {
13+
public boolean isPalindrome(String s) {
14+
15+
// ํ•ด๊ฒฐ๋ฒ• 1 (ํˆฌํฌ์ธํ„ฐ)
16+
// ์‹œ๊ฐ„๋ณต์žก๋„: O(N), ๊ณต๊ฐ„ ๋ณต์žก๋„ : O(1)
17+
// 2ms & Beats 99.05%
18+
int left = 0, right = s.length() - 1;
19+
20+
while (left < right) {
21+
// (1)
22+
if (!Character.isLetterOrDigit(s.charAt(left))) {
23+
left++;
24+
}
25+
else if (!Character.isLetterOrDigit(s.charAt(right))) {
26+
right--;
27+
}
28+
else {
29+
if (Character.toLowerCase(s.charAt(left)) != Character.toLowerCase(s.charAt(right))) {
30+
return false;
31+
}
32+
33+
left++;
34+
right--;
35+
}
36+
37+
38+
// (2)
39+
while (left < right && !Character.isLetterOrDigit(s.charAt(left))) {
40+
left++;
41+
}
42+
while (left < right && !Character.isLetterOrDigit(s.charAt(right))) {
43+
right--;
44+
}
45+
if (Character.toLowerCase(s.charAt(left)) != Character.toLowerCase(s.charAt(right))) {
46+
return false;
47+
}
48+
left++;
49+
right--;
50+
}
51+
52+
return true;
53+
54+
// ํ•ด๊ฒฐ๋ฒ• 2 (Stream API)
55+
// ์‹œ๊ฐ„ ๋ณต์žก๋„ : O(N), ๊ณต๊ฐ„ ๋ณต์žก๋„ : O(N)
56+
// 133ms & Beats 5.58%
57+
String filtered = s.chars()
58+
.filter(Character::isLetterOrDigit)
59+
.mapToObj(c -> String.valueOf((char) Character.toLowerCase(c)))
60+
.reduce("", String::concat);
61+
62+
return IntStream.range(0, filtered.length() / 2)
63+
.allMatch(i -> filtered.charAt(i) == filtered.charAt(filtered.length() - 1 - i));
64+
}
65+
}

0 commit comments

Comments
ย (0)