Skip to content

Commit 89fffe7

Browse files
authored
Merge pull request #2002 from YuuuuuuYu/main
[YuuuuuuYu] WEEK 01 solutions
2 parents 25c4666 + 95e992a commit 89fffe7

File tree

5 files changed

+157
-0
lines changed

5 files changed

+157
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* Runtime: 14ms
3+
* Time Complexity: O(n)
4+
*
5+
* Memory: 93.59MB
6+
* Space Complexity: O(n)
7+
*
8+
* Approach: HashSet์„ ์‚ฌ์šฉํ•˜์—ฌ ์ค‘๋ณต ๊ฒ€์‚ฌ
9+
* - ๋ฐฐ์—ด์„ ์ˆœํšŒํ•˜๋ฉด์„œ ๊ฐ ์›์†Œ๋ฅผ HashSet์— ์ €์žฅ
10+
* - ์ด๋ฏธ ์กด์žฌํ•˜๋Š” ์›์†Œ๋ฅผ ๋ฐœ๊ฒฌํ•˜๋ฉด ์ฆ‰์‹œ true ๋ฐ˜ํ™˜
11+
*/
12+
class Solution {
13+
public boolean containsDuplicate(int[] nums) {
14+
Set<Integer> set = new HashSet<>();
15+
for (int num: nums) {
16+
if (set.contains(num)) {
17+
return true;
18+
}
19+
set.add(num);
20+
}
21+
return false;
22+
}
23+
}
24+
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Runtime: 0ms
3+
* Time Complexity: O(n)
4+
*
5+
* Memory: 42.72MB
6+
* Space Complexity: O(1)
7+
*
8+
* Approach: ๋‘ ๊ฐ€์ง€ ๋ณ€์ˆ˜๋งŒ ์‚ฌ์šฉํ•œ DP ์ ‘๊ทผ๋ฒ•
9+
* - withoutPrevHouse: ์ด์ „ ์ง‘์„ ํ„ธ์ง€ ์•Š์•˜์„ ๋•Œ์˜ ์ตœ๋Œ€ ๊ธˆ์•ก
10+
* - withPrevHouse: ์ด์ „ ์ง‘๊นŒ์ง€ ๊ณ ๋ คํ•œ ์ตœ๋Œ€ ๊ธˆ์•ก
11+
* - ๊ฐ ์ง‘์—์„œ "ํ„ธ๊ฑฐ๋‚˜ ์•ˆ ํ„ธ๊ฑฐ๋‚˜" ๋‘ ๊ฐ€์ง€ ์„ ํƒ์ง€ ์ค‘ ์ตœ๋Œ€๊ฐ’ ์„ ํƒ
12+
* 1) ํ˜„์žฌ ์ง‘์„ ์•ˆ ํ„ธ๋ฉด: ์ด์ „ ์ง‘๊นŒ์ง€์˜ ์ตœ๋Œ€๊ฐ’ ์œ ์ง€
13+
* 2) ํ˜„์žฌ ์ง‘์„ ํ„ธ๋ฉด: ์ „์ „ ์ง‘๊นŒ์ง€์˜ ์ตœ๋Œ€๊ฐ’ + ํ˜„์žฌ ์ง‘ ๊ธˆ์•ก
14+
*/
15+
class Solution {
16+
public int rob(int[] nums) {
17+
int withoutPrevHouse = 0;
18+
int withPrevHouse = 0;
19+
20+
for (int money: nums) {
21+
int maxMoney = Math.max(withPrevHouse, withoutPrevHouse+money);
22+
withoutPrevHouse = withPrevHouse;
23+
withPrevHouse = maxMoney;
24+
}
25+
26+
return withPrevHouse;
27+
}
28+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* Runtime: 26ms
3+
* Time Complexity: O(n)
4+
* - HashSet ๊ตฌ์„ฑ: O(n)
5+
* - ๋ชจ๋“  ์›์†Œ ์ˆœํšŒ: ์ตœ์•… O(n), ํ‰๊ท ์ ์œผ๋กœ ๋” ๋น ๋ฆ„ (์กฐ๊ธฐ ์ข…๋ฃŒ)
6+
* - ์—ฐ์† ์ˆ˜์—ด ํƒ์ƒ‰: O(n)
7+
*
8+
* Memory: 95.11MB
9+
* Space Complexity: O(n)
10+
*
11+
* Approach: HashSet์„ ์ด์šฉํ•˜์—ฌ ์ค‘๋ณต ์ œ๊ฑฐ ํ›„ ์—ฐ์† ์ˆ˜์—ด ๊ธธ์ด ํƒ์ƒ‰
12+
* - ์—ฐ์† ์ˆ˜์—ด์˜ ์‹œ์ž‘์ ๋งŒ ํƒ์ƒ‰ํ•˜์—ฌ ์ค‘๋ณต ์ž‘์—… ๋ฐฉ์ง€ (num-1์ด ์—†๋Š” ๊ฒฝ์šฐ)
13+
* - ๊ฐ ์‹œ์ž‘์ ์—์„œ ์—ฐ์†๋œ ๋‹ค์Œ ์ˆซ์ž๋“ค์„ ์ˆœ์ฐจ์ ์œผ๋กœ ํƒ์ƒ‰
14+
* - nums ์‚ฌ์ด์ฆˆ์— ๋„๋‹ฌ ์‹œ ์กฐ๊ธฐ ์ข…๋ฃŒํ•˜์—ฌ ๋ถˆํ•„์š”ํ•œ ํƒ์ƒ‰ ๋ฐฉ์ง€
15+
*/
16+
class Solution {
17+
public int longestConsecutive(int[] nums) {
18+
if (nums.length == 0) return 0;
19+
20+
Set<Integer> set = new HashSet<>();
21+
for (int num: nums) {
22+
set.add(num);
23+
}
24+
25+
int longestLength = 0;
26+
for (int num: set) {
27+
if (!set.contains(num-1)) {
28+
int currentNum = num;
29+
int currentLength = 1;
30+
31+
while (set.contains(currentNum+1)) {
32+
currentNum++;
33+
currentLength++;
34+
}
35+
36+
longestLength = longestLength < currentLength ? currentLength : longestLength;
37+
if (longestLength == set.size()) {
38+
return longestLength;
39+
}
40+
}
41+
}
42+
43+
return longestLength;
44+
}
45+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Runtime: 15ms
3+
* Time Complexity: O(n log n)
4+
* - ๋นˆ๋„ ๊ณ„์‚ฐ: O(n)
5+
* - ์ •๋ ฌ: O(u log u) โ‰ค O(n log n)
6+
* - k๊ฐœ ์ถ”์ถœ: O(k)
7+
*
8+
* Memory: 47.73MB
9+
* Space Complexity: O(n)
10+
* - HashMap: O(u) โ‰ค O(n)
11+
* - List: O(u) โ‰ค O(n)
12+
*
13+
* Approach: HashMap์œผ๋กœ ๋นˆ๋„ ๊ณ„์‚ฐ ํ›„ ์ •๋ ฌํ•˜์—ฌ top k ์ถ”์ถœ
14+
* - ๋ฐฐ์—ด ์ˆœํšŒํ•˜์—ฌ ๊ฐ ์›์†Œ์˜ ๋นˆ๋„ ๊ณ„์‚ฐ
15+
* - ๋นˆ๋„ ๊ธฐ์ค€ ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ
16+
* - ์ƒ์œ„ k๊ฐœ ์›์†Œ ์ถ”์ถœ
17+
*/
18+
class Solution {
19+
public int[] topKFrequent(int[] nums, int k) {
20+
Map<Integer, Integer> frequentMap = new HashMap<>();
21+
for (int num: nums) {
22+
frequentMap.put(num, frequentMap.getOrDefault(num, 0)+1);
23+
}
24+
25+
List<Map.Entry<Integer, Integer>> list = new ArrayList<>(frequentMap.entrySet());
26+
list.sort((a, b) -> b.getValue() - a.getValue());
27+
28+
int[] result = new int[k];
29+
for (int i=0; i<k; i++) {
30+
result[i] = list.get(i).getKey();
31+
}
32+
33+
return result;
34+
}
35+
}

โ€Žtwo-sum/YuuuuuuYu.javaโ€Ž

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Runtime: 2ms
3+
* Time Complexity: O(n)
4+
*
5+
* Memory: 47.41MB
6+
* Space Complexity: O(n)
7+
*
8+
* Approach: HashMap์„ ์‚ฌ์šฉํ•˜์—ฌ ์ง์„ ์ด๋ฃจ๋Š” ๊ฐ’(pair) ๊ฒ€์‚ฌ
9+
* - ๋ฐฐ์—ด์„ ์ˆœํšŒํ•˜๋ฉด์„œ ๊ฐ ์›์†Œ์˜ ์ง์„ ๊ณ„์‚ฐ
10+
* - ์ง์ด HashMap์— ์กด์žฌํ•˜๋Š”์ง€ ๊ฒ€์‚ฌ
11+
*/
12+
class Solution {
13+
public int[] twoSum(int[] nums, int target) {
14+
Map <Integer, Integer> map = new HashMap<>();
15+
for (int i=0; i<nums.length; i++) {
16+
int pair = target-nums[i];
17+
if (map.containsKey(pair)) {
18+
return new int[]{i, map.get(pair)};
19+
}
20+
map.put(nums[i], i);
21+
}
22+
23+
return new int[]{};
24+
}
25+
}

0 commit comments

Comments
ย (0)